All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 5/7] perf tools: Fix int16_t truncation of max_cpu_num in set_max_cpu_num()
Date: Sat, 06 Jun 2026 20:19:15 +0000	[thread overview]
Message-ID: <20260606201916.266321F00899@smtp.kernel.org> (raw)
In-Reply-To: <20260606200601.1861227-6-acme@kernel.org>

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

Pre-existing issues:
- [Critical] Stack buffer overflow in `cpu_map__snprint` due to unsafe accumulation of `snprintf` return values.
- [Medium] Integer underflow in `get_max_num()` leads to out-of-bounds heap read when parsing an empty sysfs file.
--

commit 191c419f9d3237462ce91c226e3e3cccc5097d51
Author: Arnaldo Carvalho de Melo <acme@redhat.com>

perf tools: Fix int16_t truncation of max_cpu_num in set_max_cpu_num()

set_max_cpu_num() assigns the sysfs "possible" CPU count to
max_cpu_num.cpu which is int16_t (struct perf_cpu).  On systems
with >32767 possible CPUs the value silently truncates, potentially
wrapping negative.  This causes cpunode_map to be underallocated
and subsequent cpu__get_node() calls to read out of bounds.

> diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
> index d3432622b2adc..21fa781b03cc7 100644
> --- a/tools/perf/util/cpumap.c
> +++ b/tools/perf/util/cpumap.c
> @@ -494,6 +494,16 @@ static void set_max_cpu_num(void)
>  	if (ret)
>  		goto out;
>  

[Severity: Medium]
This isn't a bug introduced by this patch, but while reviewing
get_max_num() which is called just before this line, I noticed a
pre-existing issue where an integer underflow can occur.

If filename__read_str() reads an empty sysfs file (for example from a
mocked sysfs directory or malformed kernel state), it returns 0 and sets
num to 0.

tools/perf/util/cpumap.c:get_max_num() {
...
    buf[num] = '\0';

    /* start on the right, to find highest node num */
    while (--num) {
        if ((buf[num] == ',') || (buf[num] == '-')) {
...
}

When num is 0, the pre-decrement --num on a size_t underflows to SIZE_MAX.
This causes an immediate out-of-bounds memory read on the dynamically
allocated buf array. Can we add a check to ensure num is greater than 0
before entering the loop?

> +	/*
> +	 * struct perf_cpu.cpu is int16_t (libperf ABI) — clamp to avoid
> +	 * truncation to negative.  See tools/lib/perf/TODO for the ABI
> +	 * widening plan.
> +	 */

[ ... ]

While reviewing cpumap.c, I also noticed another pre-existing issue in
cpu_map__snprint():

tools/perf/util/cpumap.c:cpu_map__snprint() {
...
    if (start == end) {
        ret += snprintf(buf + ret, size - ret,
                        "%s%d", COMMA,
                        perf_cpu_map__cpu(map, start).cpu);
    } else {
...
}

[Severity: Critical]
This is a pre-existing issue, but does this loop unsafely accumulate
snprintf return values?

The standard snprintf function returns the number of characters that would
have been written if the buffer was large enough, not the actual bytes
written. If the constructed string length exceeds the provided buffer size
(such as the 1024-byte stack buffer passed by cpu_map__fprintf()), ret
will become strictly greater than size.

On the next iteration, the unsigned arithmetic size - ret will underflow
to a huge value, and buf + ret will point past the end of the stack buffer.

Subsequent snprintf calls could write entirely out of bounds on the stack,
leading to a stack buffer overflow.

Could this happen when processing an untrusted perf.data file with a
highly fragmented or large CPU map?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260606200601.1861227-1-acme@kernel.org?part=5

  reply	other threads:[~2026-06-06 20:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-06 20:05 [PATCH v1 0/7] perf tools: Fix NULL derefs, OOB reads, use-after-free, and memory leaks Arnaldo Carvalho de Melo
2026-06-06 20:05 ` [PATCH 1/7] perf mmap: Fix NULL deref in aio cleanup on alloc failure Arnaldo Carvalho de Melo
2026-06-06 20:05 ` [PATCH 2/7] perf stat: Introduce perf_env__get_cpu_topology() to guard NULL env->cpu Arnaldo Carvalho de Melo
2026-06-06 20:23   ` sashiko-bot
2026-06-06 21:47     ` Arnaldo Melo
2026-06-06 20:05 ` [PATCH 3/7] perf c2c: Fix use-after-free in he__get_c2c_hists() error path Arnaldo Carvalho de Melo
2026-06-06 20:05 ` [PATCH 4/7] perf timechart: Fix cpu2y() OOB read on untrusted CPU index Arnaldo Carvalho de Melo
2026-06-06 20:05 ` [PATCH 5/7] perf tools: Fix int16_t truncation of max_cpu_num in set_max_cpu_num() Arnaldo Carvalho de Melo
2026-06-06 20:19   ` sashiko-bot [this message]
2026-06-06 20:05 ` [PATCH 6/7] perf sched: Free callchain nodes in idle thread cleanup Arnaldo Carvalho de Melo
2026-06-06 20:05 ` [PATCH 7/7] libperf: Document struct perf_cpu int16_t ABI limitation Arnaldo Carvalho de Melo

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=20260606201916.266321F00899@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --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 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.