From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 06/13] perf header: Sanity check HEADER_MEM_TOPOLOGY
Date: Fri, 10 Apr 2026 22:32:42 +0000 [thread overview]
Message-ID: <20260410223242.DD76FC19421@smtp.kernel.org> (raw)
In-Reply-To: <20260410220905.200051-7-acme@kernel.org>
Sashiko AI review found 2 potential issue(s):
- [Critical] Heap Buffer Overflow via Integer Truncation in do_read_bitmap()
- [Low] Inaccurate bounds check under-approximates minimum section size
--
commit 005d03d0f059f4f346c521c1c989a61ca017de89
Author: Arnaldo Carvalho de Melo <acme@redhat.com>
perf header: Sanity check HEADER_MEM_TOPOLOGY
This commit adds validation to process_mem_topology() to protect against
malformed perf.data files. It introduces an upper bound check on nr_nodes
using MAX_NUMA_NODES and verifies the section size is large enough before
allocating memory.
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
[ ... ]
> @@ -3308,6 +3308,18 @@ static int process_mem_topology(struct feat_fd *ff,
> if (do_read_u64(ff, &nr))
> return -1;
>
> + if (nr > MAX_NUMA_NODES) {
> + pr_err("Invalid HEADER_MEM_TOPOLOGY: nr_nodes (%llu) > %u\n",
> + (unsigned long long)nr, MAX_NUMA_NODES);
> + return -1;
> + }
> +
> + if (ff->size < 3 * sizeof(u64) + nr * 2 * sizeof(u64)) {
Does the per-node size calculation introduce a regression by
under-approximating the minimum section size?
Each memory node actually consumes at least 3 u64 fields because
do_read_bitmap() also reads a u64 size field before its payload:
process_mem_topology() {
...
_R(node)
_R(size)
...
if (do_read_bitmap(ff, &n.set, &n.size))
...
}
And do_read_bitmap() reads the size:
do_read_bitmap() {
...
ret = do_read_u64(ff, &size);
...
}
Should this check instead be nr * 3 * sizeof(u64) to account for the bitmap
size field?
> + pr_err("Invalid HEADER_MEM_TOPOLOGY: section too small (%zu) for %llu nodes\n",
> + ff->size, (unsigned long long)nr);
> + return -1;
> + }
> +
> nodes = calloc(nr, sizeof(*nodes));
While this patch hardens topology parsing, is there a regression further down
in the parsing logic that could cause an out-of-bounds write?
When process_mem_topology() calls do_read_bitmap(), a 64-bit size is read and
passed to bitmap_zalloc():
do_read_bitmap() {
u64 size, *p;
...
ret = do_read_u64(ff, &size);
if (ret)
return ret;
set = bitmap_zalloc(size);
...
}
If bitmap_zalloc() takes a 32-bit integer for the number of bits, a size
exceeding 32 bits would be truncated, resulting in a small allocation.
The subsequent loop then calculates its bounds using the full 64-bit value:
for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
ret = do_read_u64(ff, p + i);
...
}
Can this loop write past the end of the truncated allocation if a large size is
supplied?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260410220905.200051-1-acme@kernel.org?part=6
next prev parent reply other threads:[~2026-04-10 22:32 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-10 22:08 [PATCHES perf-tools-next v2 00/13] Sanity check perf.data headers Arnaldo Carvalho de Melo
2026-04-10 22:08 ` [PATCH 01/13] perf header: Validate nr_domains when reading HEADER_CPU_DOMAIN_INFO Arnaldo Carvalho de Melo
2026-04-10 22:08 ` [PATCH 02/13] perf header: Bump up the max number of command line args allowed Arnaldo Carvalho de Melo
2026-04-10 22:34 ` sashiko-bot
2026-04-10 22:08 ` [PATCH 03/13] perf header: Sanity check HEADER_NRCPUS and HEADER_CPU_DOMAIN_INFO Arnaldo Carvalho de Melo
2026-04-10 22:45 ` sashiko-bot
2026-04-10 22:08 ` [PATCH 04/13] perf header: Sanity check HEADER_CPU_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:38 ` sashiko-bot
2026-04-10 22:08 ` [PATCH 05/13] perf header: Sanity check HEADER_NUMA_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:28 ` sashiko-bot
2026-04-10 22:08 ` [PATCH 06/13] perf header: Sanity check HEADER_MEM_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:32 ` sashiko-bot [this message]
2026-04-10 22:08 ` [PATCH 07/13] perf header: Sanity check HEADER_PMU_MAPPINGS Arnaldo Carvalho de Melo
2026-04-10 22:33 ` sashiko-bot
2026-04-10 22:09 ` [PATCH 08/13] perf header: Sanity check HEADER_GROUP_DESC Arnaldo Carvalho de Melo
2026-04-10 22:28 ` sashiko-bot
2026-04-10 22:09 ` [PATCH 09/13] perf header: Sanity check HEADER_CACHE Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 10/13] perf header: Sanity check HEADER_HYBRID_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 11/13] perf header: Sanity check HEADER_PMU_CAPS Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 12/13] perf header: Sanity check HEADER_BPF_PROG_INFO Arnaldo Carvalho de Melo
2026-04-10 22:09 ` [PATCH 13/13] perf header: Add sanity checks to HEADER_BPF_BTF processing Arnaldo Carvalho de Melo
-- strict thread matches above, loose matches on Subject: below --
2026-04-10 0:39 [PATCHES perf-tools-next v1 00/13] Sanity check perf.data headers Arnaldo Carvalho de Melo
2026-04-10 0:39 ` [PATCH 06/13] perf header: Sanity check HEADER_MEM_TOPOLOGY Arnaldo Carvalho de Melo
2026-04-10 1:04 ` 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=20260410223242.DD76FC19421@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=acme@kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko@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