From: sashiko-bot@kernel.org
To: "Chun-Tse Shao" <ctshao@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v7 2/2] perf evsel: Find process with busy PMUs for EBUSY
Date: Mon, 18 May 2026 23:34:26 +0000 [thread overview]
Message-ID: <20260518233427.301B2C2BCB7@smtp.kernel.org> (raw)
In-Reply-To: <20260518224819.1247382-2-ctshao@google.com>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Use-After-Free and state corruption due to strtok() on a stack-allocated buffer.
- [Medium] Terminal injection vulnerability via unsanitized process command lines.
- [Low] Improper string comparison bound can lead to false positive process identification.
--
commit a4a94ff69c8d665f6d09eaea15fdbbbd89954b18
Author: Chun-Tse Shao <ctshao@google.com>
perf evsel: Find process with busy PMUs for EBUSY
This commit parses fdinfo to match the PMU type of a perf event that
failed to open with EBUSY, and reports the conflicting processes.
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -3969,29 +3970,64 @@ static int dump_perf_event_processes(char *msg, size_t size)
> continue;
> /* Take care as readlink doesn't null terminate the string. */
> if (!strncmp(buf, "anon_inode:[perf_event]", link_size)) {
Is link_size the correct bound for this comparison?
If the symlink target is shorter than "anon_inode:[perf_event]" but exactly
matches the prefix (for example, if link_size is 4 and buf is "anon"), this
strncmp will return 0 and falsely identify the process. Should this also
check that link_size equals the exact length of the expected string?
[ ... ]
> + buf[fdinfo_size] = '\0';
> +
> + line = strtok(buf, "\n");
> + while (line != NULL) {
> + if (sscanf(line,
> + "perf_event_attr.type:\t%u",
> + &perf_event_type) == 1)
> + break;
> + line = strtok(NULL, "\n");
> }
buf is a stack-allocated array. Since strtok stores a pointer to the parsed
string in its internal static state, does breaking out of this loop early
leave the internal pointer pointing to popped stack memory?
If another part of the tool later calls strtok(NULL, ...) in the same thread,
could this cause a use-after-free or memory corruption as strtok writes null
bytes? Would it be safer to use strtok_r here to avoid clobbering the shared
parsing state?
[ ... ]
> + cmdline_size = read(cmdline_fd, buf, sizeof(buf) - 1);
> + close(cmdline_fd);
> + if (cmdline_size < 0)
> + continue;
> + buf[cmdline_size] = '\0';
> + for (ssize_t i = 0; i < cmdline_size; i++) {
> + if (buf[i] == '\0')
> + buf[i] = ' ';
> + }
> +
> + if (printed == 0)
> + printed += scnprintf(
> + msg, size,
> + "Possible processes:\n");
> +
> + printed += scnprintf(msg + printed, size - printed,
> + "%s %s\n", proc_entry->d_name, buf);
> + break;
Does this safely handle unprivileged process command lines?
While null bytes are replaced with spaces, other non-printable characters and
ANSI escape sequences are preserved. If a malicious user intentionally holds a
PMU and crafts a command line with escape sequences, could those sequences
execute in the terminal of the administrator running perf stat, potentially
allowing terminal output spoofing?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260518224819.1247382-1-ctshao@google.com?part=2
next prev parent reply other threads:[~2026-05-18 23:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 22:47 [PATCH v7 1/2] perf: Reveal PMU type in fdinfo Chun-Tse Shao
2026-05-18 22:47 ` [PATCH v7 2/2] perf evsel: Find process with busy PMUs for EBUSY Chun-Tse Shao
2026-05-18 23:34 ` sashiko-bot [this message]
2026-05-18 23:11 ` [PATCH v7 1/2] perf: Reveal PMU type in fdinfo 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=20260518233427.301B2C2BCB7@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=ctshao@google.com \
--cc=linux-perf-users@vger.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox