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 v4 06/11] perf tools: Verify thread status and mount namespace inode
Date: Wed, 25 Nov 2026 21:43:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-234203@kernel.org> (raw)
In-Reply-To: <20261125212851.1792988-7-namhyung@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf namespaces: strstr() in nsinfo__get_nspid() matches comm
  in status
- [Low] perf namespaces: unchecked open() in nsinfo__host_proc_get()
  unpins /proc
--

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

perf tools: Verify thread status and mount namespace inode

This commit verifies that a thread's /proc/<pid>/task/<tid>/status Tgid
matches pid and its mount namespace inode matches mntns_ino before entering
its mount namespace, while pinning a reference-counted host /proc file
descriptor across setns().

> diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
> index 940132d3458be..704f69663129d 100644
> --- a/tools/perf/util/namespaces.c
> +++ b/tools/perf/util/namespaces.c
[ ... ]
> @@ -59,22 +60,102 @@ void namespaces__free(struct namespaces *namespaces)
>  	free(namespaces);
>  }
>  
> -static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const char *path)
> +static DEFINE_MUTEX(host_proc_lock);
> +static int host_proc_fd __guarded_by(&host_proc_lock) = -1;
> +static unsigned int host_proc_refcnt __guarded_by(&host_proc_lock);
> +
> +static void nsinfo__host_proc_get(void)
> +{
> +	mutex_lock(&host_proc_lock);
> +	if (host_proc_refcnt++ == 0)
> +		host_proc_fd = open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
> +	mutex_unlock(&host_proc_lock);
> +}
> +
> +static void nsinfo__host_proc_put(void)
> +{
> +	mutex_lock(&host_proc_lock);
> +	if (host_proc_refcnt > 0 && --host_proc_refcnt == 0 &&
> +	    host_proc_fd >= 0) {
> +		close(host_proc_fd);
> +		host_proc_fd = -1;
> +	}
> +	mutex_unlock(&host_proc_lock);
> +}
> +
> +static int nsinfo__proc_open(const char *rel_path, int flags)
> +{
> +	char path[64];
> +	int ret;
> +
> +	mutex_lock(&host_proc_lock);
> +	if (host_proc_fd >= 0) {
> +		ret = openat(host_proc_fd, rel_path, flags);
> +	} else {
> +		scnprintf(path, sizeof(path), "/proc/%s", rel_path);
> +		ret = open(path, flags);
> +	}
> +	mutex_unlock(&host_proc_lock);
> +	return ret;
> +}

[Severity: Low]
Can nsinfo__mountns_enter() enter a target mount namespace without pinning
host /proc if open("/proc", ...) in nsinfo__host_proc_get() fails?

Because host_proc_refcnt is incremented unconditionally and
nsinfo__mountns_enter() does not check whether host_proc_fd >= 0, a failed
open leaves host_proc_fd == -1 while nsinfo__proc_open() falls back to
opening /proc/<rel_path> by path.  Once setns() succeeds, subsequent
nsinfo__proc_open(), nsinfo__proc_stat(), and nsinfo__proc_readlink() calls
take the fallback path and resolve /proc inside the target mount namespace
instead of the host /proc.

[ ... ]

> +static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns,
> +			     const char *rel_path)
>  {
>  	FILE *f = NULL;
>  	char *statln = NULL;
>  	size_t linesz = 0;
>  	char *nspid;
> +	int fd;
> +	bool got_tgid = false;
>  
> -	f = fopen(path, "r");
> -	if (f == NULL)
> +	fd = nsinfo__proc_open(rel_path, O_RDONLY | O_CLOEXEC);
> +	if (fd < 0)
>  		return -1;
> +	f = fdopen(fd, "r");
> +	if (f == NULL) {
> +		close(fd);
> +		return -1;
> +	}
>  
>  	while (getline(&statln, &linesz, f) != -1) {
>  		/* Use tgid if CONFIG_PID_NS is not defined. */
>  		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 +172,17 @@ 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;
> +}

[Severity: Medium]
Can strstr() in nsinfo__get_nspid() match the user-controlled comm in the
"Name:\t<comm>\n" line at the top of /proc/<pid>/task/<tid>/status?

If a non-leader thread with a recycled TID (<pid>) sets its comm to
"<pid>Tgid:NStgid:", both strstr() checks match on line 1, strtol() parses
<pid> after the "Name:\t" tab into *tgid, and the NStgid branch breaks out
of the loop before reading the real Tgid line, bypassing the
tgid != nsinfo__pid(nsi) recycling check in nsinfo__init_locked() and
nsinfo__check_proc().  Similarly, a live thread whose comm contains
"NStgid:" breaks out on line 1 with got_tgid == false and is falsely marked
exited.  Using strstarts(statln, "Tgid:\t") and
strstarts(statln, "NStgid:\t") would ensure only the actual status keys are
matched.

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

  reply	other threads:[~2026-11-25 21:43 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:28 [PATCH v4 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-11-25 21:43   ` sashiko-bot [this message]
2026-11-25 21:28 ` [PATCH v4 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-25 21:28 ` [PATCH v4 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
2026-11-25 21:52 ` [PATCH v4 00/11] perf tools: Enhance namespace and chroot handling sashiko-bot
2026-11-28  9:52 ` Athira Rajeev
2026-11-29 23:38   ` Namhyung Kim
2026-11-30 13:57     ` Athira Rajeev

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-234203@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.