mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>, Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-perf-users@vger.kernel.org
Subject: [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode
Date: Sun, 15 Nov 2026 21:57:26 -0800	[thread overview]
Message-ID: <20261116055731.707164-7-namhyung@kernel.org> (raw)
In-Reply-To: <20261116055731.707164-1-namhyung@kernel.org>

Verify /proc/<pid>/task/<tid>/status exists and has Tgid == pid before
reading or entering a thread's namespaces to guard against PID/TID
recycling.

Also record the mount namespace inode (mntns_ino) during initialization
and verify via fstat() that the opened namespace file descriptor still
refers to the same mount namespace.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/namespaces.c | 139 +++++++++++++++++++++++++++--------
 tools/perf/util/namespaces.h |   4 +
 2 files changed, 114 insertions(+), 29 deletions(-)

diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index afd8ad7f843ec09d..860b90e8384ed1f2 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -65,6 +65,7 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 	char *statln = NULL;
 	size_t linesz = 0;
 	char *nspid;
+	bool got_tgid = false;
 
 	f = fopen(path, "r");
 	if (f == NULL)
@@ -75,6 +76,7 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 		if (strstr(statln, "Tgid:") != NULL) {
 			*tgid = (pid_t)strtol(strrchr(statln, '\t'), NULL, 10);
 			*nstgid = *tgid;
+			got_tgid = true;
 		}
 
 		if (strstr(statln, "NStgid:") != NULL) {
@@ -91,7 +93,20 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 
 	fclose(f);
 	free(statln);
-	return 0;
+	return got_tgid ? 0 : -1;
+}
+
+static void nsinfo__clear_need_setns_locked(struct nsinfo *nsi)
+{
+	RC_CHK_ACCESS(nsi)->inited = true;
+	RC_CHK_ACCESS(nsi)->need_setns = false;
+	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+}
+
+static void nsinfo__set_exited_locked(struct nsinfo *nsi)
+{
+	RC_CHK_ACCESS(nsi)->exited = true;
+	nsinfo__clear_need_setns_locked(nsi);
 }
 
 static int nsinfo__init_locked(struct nsinfo *nsi)
@@ -101,16 +116,41 @@ static int nsinfo__init_locked(struct nsinfo *nsi)
 	char *newns = NULL;
 	struct stat old_stat;
 	struct stat new_stat;
+	pid_t tgid = 0, nstgid = 0;
+	bool in_pidns = false;
 	int rv = -1;
 
+	if (RC_CHK_ACCESS(nsi)->exited)
+		return -1;
+
 	RC_CHK_ACCESS(nsi)->inited = true;
 
+	/*
+	 * Verify that the thread (pid, tid) still exists in /proc and its Tgid
+	 * matches pid (guarding against PID/TID recycling), while resolving the
+	 * innermost tgid if the process is in a different PID namespace.
+	 */
+	if (snprintf(spath, PATH_MAX, "/proc/%d/task/%d/status",
+		     nsinfo__pid(nsi), nsinfo__tid(nsi)) >= PATH_MAX)
+		goto out;
+
+	rv = nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, spath);
+	if (rv < 0 || tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited_locked(nsi);
+		rv = -1;
+		goto out;
+	}
+
+	RC_CHK_ACCESS(nsi)->tgid = tgid;
+	RC_CHK_ACCESS(nsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nsi)->in_pidns = in_pidns;
+
 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
-		return rv;
+		return -1;
 
 	if (asprintf(&newns, "/proc/%d/task/%d/ns/mnt",
 		     nsinfo__pid(nsi), nsinfo__tid(nsi)) == -1)
-		return rv;
+		return -1;
 
 	if (stat(oldns, &old_stat) < 0)
 		goto out;
@@ -124,19 +164,10 @@ static int nsinfo__init_locked(struct nsinfo *nsi)
 	if (old_stat.st_ino != new_stat.st_ino) {
 		RC_CHK_ACCESS(nsi)->need_setns = true;
 		RC_CHK_ACCESS(nsi)->mntns_path = newns;
+		RC_CHK_ACCESS(nsi)->mntns_ino = new_stat.st_ino;
 		newns = NULL;
 	}
 
-	/* If we're dealing with a process that is in a different PID namespace,
-	 * attempt to work out the innermost tgid for the process.
-	 */
-	if (snprintf(spath, PATH_MAX, "/proc/%d/task/%d/status",
-		     nsinfo__pid(nsi), nsinfo__tid(nsi)) >= PATH_MAX)
-		goto out;
-
-	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
-			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
-
 out:
 	free(newns);
 	return rv;
@@ -157,7 +188,7 @@ int nsinfo__init(struct nsinfo *nsi)
 
 static void nsinfo__ensure_init_locked(struct nsinfo *nsi)
 {
-	if (!RC_CHK_ACCESS(nsi)->inited)
+	if (!RC_CHK_ACCESS(nsi)->inited && !RC_CHK_ACCESS(nsi)->exited)
 		nsinfo__init_locked(nsi);
 }
 
@@ -219,7 +250,9 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	RC_CHK_ACCESS(nnsi)->tid = RC_CHK_ACCESS(nsi)->tid;
 	RC_CHK_ACCESS(nnsi)->tgid = RC_CHK_ACCESS(nsi)->tgid;
 	RC_CHK_ACCESS(nnsi)->nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	RC_CHK_ACCESS(nnsi)->mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
 	RC_CHK_ACCESS(nnsi)->inited = RC_CHK_ACCESS(nsi)->inited;
+	RC_CHK_ACCESS(nnsi)->exited = RC_CHK_ACCESS(nsi)->exited;
 	RC_CHK_ACCESS(nnsi)->need_setns = RC_CHK_ACCESS(nsi)->need_setns;
 	RC_CHK_ACCESS(nnsi)->in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
 	if (RC_CHK_ACCESS(nsi)->mntns_path) {
@@ -277,7 +310,7 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)
 
 	mutex_lock(nsinfo__lock(nsi));
 	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
-	ret = RC_CHK_ACCESS(nsi)->need_setns;
+	ret = !RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
 }
@@ -288,12 +321,47 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)
 		return;
 
 	mutex_lock(nsinfo__lock(nsi));
-	RC_CHK_ACCESS(nsi)->inited = true;
-	RC_CHK_ACCESS(nsi)->need_setns = false;
-	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+	nsinfo__clear_need_setns_locked(nsi);
+	mutex_unlock(nsinfo__lock(nsi));
+}
+
+void nsinfo__set_exited(struct nsinfo *nsi)
+{
+	if (!nsi)
+		return;
+
+	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__set_exited_locked(nsi);
 	mutex_unlock(nsinfo__lock(nsi));
 }
 
+bool nsinfo__check_proc(struct nsinfo *nsi)
+{
+	char spath[64];
+	pid_t tgid = 0, nstgid = 0;
+	bool in_pidns = false;
+	bool exited;
+
+	if (!nsi)
+		return false;
+
+	mutex_lock(nsinfo__lock(nsi));
+	exited = RC_CHK_ACCESS(nsi)->exited;
+	mutex_unlock(nsinfo__lock(nsi));
+	if (exited)
+		return false;
+
+	scnprintf(spath, sizeof(spath), "/proc/%d/task/%d/status",
+		  nsinfo__pid(nsi), nsinfo__tid(nsi));
+	if (nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, spath) < 0 ||
+	    tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited(nsi);
+		return false;
+	}
+
+	return true;
+}
+
 pid_t nsinfo__tgid(const struct nsinfo *nsi)
 {
 	pid_t ret;
@@ -349,6 +417,8 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 {
 	char curpath[PATH_MAX];
 	char *mntns_path = NULL;
+	ino_t mntns_ino = 0;
+	struct stat st;
 	int oldns = -1;
 	int newns = -1;
 	char *oldcwd = NULL;
@@ -358,19 +428,26 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 
 	nc->oldns = -1;
 	nc->newns = -1;
+	nc->oldcwd = NULL;
 
 	if (!nsi)
 		return;
 
 	mutex_lock(&RC_CHK_ACCESS(nsi)->lock);
 	nsinfo__ensure_init_locked(nsi);
-	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
+	if (!RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns &&
+	    RC_CHK_ACCESS(nsi)->mntns_path) {
 		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+		mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
+	}
 	mutex_unlock(&RC_CHK_ACCESS(nsi)->lock);
 
 	if (!mntns_path)
 		return;
 
+	if (!nsinfo__check_proc(nsi))
+		goto out;
+
 	if (!getcwd(curpath, sizeof(curpath)))
 		goto out;
 
@@ -378,14 +455,19 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 	if (!oldcwd)
 		goto out;
 
-	oldns = open("/proc/self/ns/mnt", O_RDONLY);
+	oldns = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
 	if (oldns < 0)
 		goto errout;
 
-	newns = open(mntns_path, O_RDONLY);
+	newns = open(mntns_path, O_RDONLY | O_CLOEXEC);
 	if (newns < 0)
 		goto errout;
 
+	if (fstat(newns, &st) < 0 || st.st_ino != mntns_ino) {
+		nsinfo__clear_need_setns(nsi);
+		goto errout;
+	}
+
 	if (setns(newns, CLONE_NEWNS) < 0)
 		goto errout;
 
@@ -407,17 +489,11 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 
 void nsinfo__mountns_exit(struct nscookie *nc)
 {
-	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
+	if (nc == NULL || !nc->oldcwd)
 		return;
 
-	setns(nc->oldns, CLONE_NEWNS);
-
-	if (nc->oldcwd) {
-		WARN_ON_ONCE(chdir(nc->oldcwd));
-		zfree(&nc->oldcwd);
-	}
-
 	if (nc->oldns > -1) {
+		setns(nc->oldns, CLONE_NEWNS);
 		close(nc->oldns);
 		nc->oldns = -1;
 	}
@@ -426,6 +502,11 @@ void nsinfo__mountns_exit(struct nscookie *nc)
 		close(nc->newns);
 		nc->newns = -1;
 	}
+
+	if (nc->oldcwd) {
+		WARN_ON_ONCE(chdir(nc->oldcwd));
+		zfree(&nc->oldcwd);
+	}
 }
 
 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index 42dcca13b002f153..7bba217aa3fde3d7 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -37,7 +37,9 @@ DECLARE_RC_STRUCT(nsinfo) {
 	pid_t			tid;
 	pid_t			tgid;
 	pid_t			nstgid;
+	ino_t			mntns_ino;
 	bool			inited;
+	bool			exited;
 	bool			need_setns;
 	bool			in_pidns;
 	char			*mntns_path;
@@ -60,6 +62,8 @@ void nsinfo__put(struct nsinfo *nsi);
 
 bool nsinfo__need_setns(const struct nsinfo *nsi);
 void nsinfo__clear_need_setns(struct nsinfo *nsi);
+void nsinfo__set_exited(struct nsinfo *nsi);
+bool nsinfo__check_proc(struct nsinfo *nsi);
 pid_t nsinfo__tgid(const struct nsinfo  *nsi);
 pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
 pid_t nsinfo__pid(const struct nsinfo  *nsi);
-- 
2.56.0.611.g1030f0b940-goog


  parent reply	other threads:[~2026-11-16  5:57 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-16  5:57 ` Namhyung Kim [this message]
2026-11-16  5:57 ` [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116055731.707164-7-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®