From: sashiko-bot@kernel.org
To: "Thomas Falcon" <thomas.falcon@intel.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v5 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()
Date: Fri, 21 Aug 2026 00:29:23 +0000 [thread overview]
Message-ID: <20260821002923.70AB41F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260821001819.162277-2-thomas.falcon@intel.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unsafe use of strcat and integer underflows in memory event formatting helpers cause stack buffer overflows.
--
--- Patch [1]: [PATCH v5 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf() ---
commit 658933771fa5b1a578c37be7178d9227ed479ba1
Author: Thomas Falcon <thomas.falcon@intel.com>
perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf()
This commit fixes the size parameter passed to perf_mem__lvl_scnprintf()
by providing the remaining buffer size instead of the entire buffer size.
> --- 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 the remaining buffer size
here expose a stack buffer overflow in the formatting helpers?
When processing a maliciously crafted perf.data file with multiple flag bits
set, the string formatting can exhaust the 200-byte stack buffer decode[]
allocated in data_src__fprintf(). As the remaining size (sz - i) approaches
zero, perf_mem__lvl_scnprintf() receives a small or zero size.
Looking at perf_mem__lvl_scnprintf() and perf_mem__snp_scnprintf(), they
unconditionally decrement the size parameter:
perf_mem__lvl_scnprintf() {
...
sz -= 1; /* -1 for null termination */
out[0] = '\0';
...
}
If sz is 0, this causes an integer underflow to SIZE_MAX. Furthermore, the
helpers use unbounded strcat() when multiple bits are set:
perf_mem__lvl_scnprintf() {
...
if (printed++) {
strcat(out, " or ");
l += 4;
}
l += scnprintf(out + l, sz - l, ...);
...
}
The strcat() can write past the end of the buffer. Because l then exceeds
sz, sz - l underflows, which disables bounds checking for subsequent
scnprintf() calls.
Should these helpers be updated to return early if sz is 0, and use bounds
checking instead of strcat() to avoid corrupting the stack?
> 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/20260821001819.162277-1-thomas.falcon@intel.com?part=1
next prev parent reply other threads:[~2026-08-21 0:29 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-21 0:18 [PATCH v5 0/6] perf: Add support for memory region/range reporting Thomas Falcon
2026-08-21 0:18 ` [PATCH v5 1/6] perf mem: Fix size tracking for mem_lvl's in perf_script__meminfo_scnprintf() Thomas Falcon
2026-08-21 0:29 ` sashiko-bot [this message]
2026-08-21 0:18 ` [PATCH v5 2/6] perf mem: Add support for printing PERF_MEM_LVLNUM_L0 Thomas Falcon
2026-08-21 0:27 ` sashiko-bot
2026-08-21 0:18 ` [PATCH v5 3/6] perf header: Support memory ranges Thomas Falcon
2026-08-21 0:28 ` sashiko-bot
2026-08-21 0:18 ` [PATCH v5 4/6] perf tools: Show memory region in perf-c2c subcommand Thomas Falcon
2026-08-21 0:30 ` sashiko-bot
2026-08-21 0:18 ` [PATCH v5 5/6] perf tools: Show memory region in perf-script subcommand Thomas Falcon
2026-08-21 0:32 ` sashiko-bot
2026-08-21 0:18 ` [PATCH v5 6/6] perf c2c: print memory region data with stdio output Thomas Falcon
2026-08-21 0:28 ` 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=20260821002923.70AB41F00A3A@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