]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf tools: Add bounds check to cpu__get_node()
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 4 Jun 2026 21:14:23 +0000 (18:14 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 4 Jun 2026 22:17:36 +0000 (19:17 -0300)
cpu__get_node() accesses cpunode_map[cpu.cpu] without checking against
max_cpu_num, the allocation size of cpunode_map.  Callers such as
builtin-kmem.c:evsel__process_alloc_event() pass sample->cpu from
perf.data events, which may exceed the host's CPU count when analyzing
cross-machine recordings.

Add a bounds check against max_cpu_num before indexing, returning -1
for out-of-range values.  This is a central fix that protects all
callers.

Fixes: 86895b480a2f ("perf stat: Add --per-node agregation support")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/cpumap.c

index b1e5c29c6e3ec8df6c451a443e500ab8e545f825..d3432622b2adc994943aec32f31ef0a40c66fad8 100644 (file)
@@ -576,6 +576,10 @@ int cpu__get_node(struct perf_cpu cpu)
                return -1;
        }
 
+       /* cpunode_map allocated for max_cpu_num entries; input may be untrusted */
+       if (cpu.cpu < 0 || cpu.cpu >= max_cpu_num.cpu)
+               return -1;
+
        return cpunode_map[cpu.cpu];
 }