linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Thomas Falcon" <thomas.falcon@intel.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v8 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()
Date: Thu, 10 Sep 2026 19:55:11 +0000	[thread overview]
Message-ID: <20260910195512.3883C1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260910194324.98002-2-thomas.falcon@intel.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The helper functions for memory event formatting (`perf_mem__lvl_scnprintf`, `perf_mem__snp_scnprintf`, `perf_mem__tlb_scnprintf`) are vulnerable to severe stack buffer overflows and integer underflows due to unvalidated size decrements, unbounded `strcat` usage, and API contract violations.
--

commit 980e511e3799aa8f7f553d523c4cd00aa00aadf1
Author: Thomas Falcon <thomas.falcon@intel.com>

perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()

When printing memory info in perf script, the entire size of the
buffer is passed to perf_mem__lvl_scnprintf() instead of the remaining
size. Pass the remaining buffer size instead.

> diff --git a/tools/perf/util/mem-events.c b/tools/perf/util/mem-events.c
> index 0b49fce251fcc..4e490f9cd3488 100644
> --- a/tools/perf/util/mem-events.c
> +++ b/tools/perf/util/mem-events.c
> @@ -610,7 +610,7 @@ int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *
>  	i += scnprintf(out, sz, "|OP ");
>  	i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
>  	i += scnprintf(out + i, sz - i, "|LVL ");
> -	i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
> +	i += perf_mem__lvl_scnprintf(out + i, sz - i, mem_info);

[Severity: High]
This is a pre-existing issue, but does passing sz - i here expose
severe integer underflows and stack buffer overflows inside
perf_mem__lvl_scnprintf() and related functions?

The functions perf_mem__lvl_scnprintf(), perf_mem__snp_scnprintf(), and
perf_mem__tlb_scnprintf() begin with an unconditional decrement of sz:

tools/perf/util/mem-events.c:perf_mem__lvl_scnprintf() {
    ...
	sz -= 1; /* -1 for null termination */
	out[0] = '\0';
    ...

If the remaining space passed by the caller (sz - i) is small (e.g., 0),
this underflows to SIZE_MAX. If it is 1, sz becomes 0, and subsequent
length calculations like sz - l underflow.

Furthermore, these functions use unbounded strcat() calls which ignore the
buffer capacity entirely:

tools/perf/util/mem-events.c:perf_mem__lvl_scnprintf() {
    ...
			strcat(out, "Remote ");
			l += 7;
    ...
    na:
	strcat(out, "N/A");
	return 3;
    ...

Because strcat() doesn't respect sz, a maliciously crafted perf.data file
can cause this to overflow the 100-byte stack buffer used for the decode
array in builtin-script.c.

Finally, if the callee returns a length larger than the remaining size, the
caller's offset i is advanced past the buffer size.

Does this cause the caller's own sz - i calculations to underflow in the
subsequent format calls below?

>  	i += scnprintf(out + i, sz - i, "|SNP ");
>  	i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
>  	i += scnprintf(out + i, sz - i, "|TLB ");

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260910194324.98002-1-thomas.falcon@intel.com?part=1

  reply	other threads:[~2026-09-10 19:55 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 19:43 [PATCH v8 0/6] perf: Add support for memory region/range reporting Thomas Falcon
2026-09-10 19:43 ` [PATCH v8 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf() Thomas Falcon
2026-09-10 19:55   ` sashiko-bot [this message]
2026-09-10 19:43 ` [PATCH v8 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0 Thomas Falcon
2026-09-10 19:51   ` sashiko-bot
2026-09-10 19:43 ` [PATCH v8 3/6] perf header: Support memory ranges Thomas Falcon
2026-09-10 19:58   ` sashiko-bot
2026-09-10 19:43 ` [PATCH v8 4/6] perf tools: Show memory region in perf-c2c subcommand Thomas Falcon
2026-09-10 19:54   ` sashiko-bot
2026-09-11  0:50   ` Mi, Dapeng
2026-09-11 15:58     ` Falcon, Thomas
2026-09-10 19:43 ` [PATCH v8 5/6] perf tools: Show memory region in perf-script subcommand Thomas Falcon
2026-09-10 19:53   ` sashiko-bot
2026-09-11  0:51   ` Mi, Dapeng
2026-09-10 19:43 ` [PATCH v8 6/6] perf c2c: print memory region data with stdio output Thomas Falcon
2026-09-10 19:59   ` 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=20260910195512.3883C1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thomas.falcon@intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).