All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Namhyung Kim" <namhyung@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode
Date: Mon, 16 Nov 2026 06:14:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-216248@kernel.org> (raw)
In-Reply-To: <20261116055731.707164-7-namhyung@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] perf namespaces: nsinfo__init_locked() returns 0 when stat()
  fails
--

commit e6cbb7bd6fc00579ed776b5b31e5e5cfe0cc41a2
Author: Namhyung Kim <namhyung@kernel.org>

perf tools: Verify thread status and mount namespace inode

This commit verifies that /proc/<pid>/task/<tid>/status exists with a
matching Tgid and checks the mount namespace inode via fstat() before
entering a thread's mount namespace.

> diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
> index afd8ad7f843ec..860b90e8384ed 100644
> --- a/tools/perf/util/namespaces.c
> +++ b/tools/perf/util/namespaces.c

[ ... ]

> @@ -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;

[Severity: Low]
Does nsinfo__init_locked() return 0 instead of -1 when stat() fails?

Moving nsinfo__get_nspid() ahead of the stat() checks sets rv to 0 on
success.  If stat(newns, &new_stat) then fails (for example with EACCES
because /proc/<pid>/task/<tid>/ns/mnt requires PTRACE_MODE_READ_FSCREDS
while status is world-readable), jumping to out returns 0 to callers of
nsinfo__init().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116055731.707164-1-namhyung@kernel.org?part=6

  reply	other threads:[~2026-11-16  6:14 UTC|newest]

Thread overview: 23+ 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  6:09   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-16  6:02   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-16  6:16   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-16  6:17   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-16  6:11   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-11-16  6:14   ` sashiko-bot [this message]
2026-11-16  5:57 ` [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-16  6:38   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-16  6:16   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-11-16  6:34   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-16  6:16   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
2026-11-16  6:24   ` sashiko-bot

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=sashiko-outbox-216248@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.