From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
James Clark <james.clark@linaro.org>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Clark Williams <williams@redhat.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@redhat.com>,
sashiko-bot@kernel.org,
"Claude Opus 4.6 (1M context)" <noreply@anthropic.com>
Subject: [PATCH 12/27] perf cpumap: Reject RANGE_CPUS with start_cpu > end_cpu
Date: Wed, 20 May 2026 22:09:57 -0300 [thread overview]
Message-ID: <20260521011027.622268-13-acme@kernel.org> (raw)
In-Reply-To: <20260521011027.622268-1-acme@kernel.org>
From: Arnaldo Carvalho de Melo <acme@redhat.com>
cpu_map__from_range() computes nr_cpus as end_cpu - start_cpu + 1.
When a crafted perf.data has start_cpu > end_cpu, this wraps to a
huge value, causing perf_cpu_map__empty_new() to attempt a massive
allocation.
Return NULL when the range is inverted.
Also clamp any_cpu to boolean (0 or 1) since it is added to the
allocation count — a crafted value > 1 would inflate the map size.
Harden cpu_map__from_mask() to reject unsupported long_size values
(anything other than 4 or 8), preventing misinterpretation of the
mask data layout.
Snapshot mmap'd fields via READ_ONCE() into locals to prevent
TOCTOU re-reads — the data pointer references MAP_SHARED mmap'd
memory that could theoretically change between reads on a
FUSE-backed file:
- cpu_map__from_range(): snapshot start_cpu, end_cpu, any_cpu
- cpu_map__from_entries(): snapshot nr and each cpu[i] element
- cpu_map__from_mask(): snapshot long_size (before validation,
closing the check-then-read gap), mask_nr
- perf_record_cpu_map_data__read_one_mask(): add u16 long_size
parameter so callers pass the validated copy instead of
re-reading data->mask32_data.long_size from mmap'd memory
Reported-by: sashiko-bot@kernel.org # Running on a local machine
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/cpumap.c | 62 +++++++++++++++++++++++++++++-----------
1 file changed, 45 insertions(+), 17 deletions(-)
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 11922e1ded844a03..b1e5c29c6e3ec8df 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -10,6 +10,7 @@
#include <linux/bitmap.h>
#include "asm/bug.h"
+#include <linux/compiler.h>
#include <linux/ctype.h>
#include <linux/zalloc.h>
#include <internal/cpumap.h>
@@ -40,15 +41,16 @@ bool perf_record_cpu_map_data__test_bit(int i,
/* Read ith mask value from data into the given 64-bit sized bitmap */
static void perf_record_cpu_map_data__read_one_mask(const struct perf_record_cpu_map_data *data,
- int i, unsigned long *bitmap)
+ int i, unsigned long *bitmap,
+ u16 long_size)
{
#if __SIZEOF_LONG__ == 8
- if (data->mask32_data.long_size == 4)
+ if (long_size == 4)
bitmap[0] = data->mask32_data.mask[i];
else
bitmap[0] = data->mask64_data.mask[i];
#else
- if (data->mask32_data.long_size == 4) {
+ if (long_size == 4) {
bitmap[0] = data->mask32_data.mask[i];
bitmap[1] = 0;
} else {
@@ -64,24 +66,27 @@ static void perf_record_cpu_map_data__read_one_mask(const struct perf_record_cpu
}
static struct perf_cpu_map *cpu_map__from_entries(const struct perf_record_cpu_map_data *data)
{
+ /* Snapshot nr — data is mmap'd and could change between reads */
+ u16 nr = READ_ONCE(data->cpus_data.nr);
struct perf_cpu_map *map;
- map = perf_cpu_map__empty_new(data->cpus_data.nr);
+ map = perf_cpu_map__empty_new(nr);
if (!map)
return NULL;
- for (unsigned int i = 0; i < data->cpus_data.nr; i++) {
+ for (unsigned int i = 0; i < nr; i++) {
+ u16 cpu = READ_ONCE(data->cpus_data.cpu[i]);
/*
* Special treatment for -1, which is not real cpu number,
* and we need to use (int) -1 to initialize map[i],
* otherwise it would become 65535.
*/
- if (data->cpus_data.cpu[i] == (u16) -1) {
+ if (cpu == (u16) -1) {
RC_CHK_ACCESS(map)->map[i].cpu = -1;
- } else if (data->cpus_data.cpu[i] < INT16_MAX) {
- RC_CHK_ACCESS(map)->map[i].cpu = (int16_t) data->cpus_data.cpu[i];
+ } else if (cpu < INT16_MAX) {
+ RC_CHK_ACCESS(map)->map[i].cpu = (int16_t) cpu;
} else {
- pr_err("Invalid cpumap entry %u\n", data->cpus_data.cpu[i]);
+ pr_err("Invalid cpumap entry %u\n", cpu);
perf_cpu_map__put(map);
return NULL;
}
@@ -93,11 +98,21 @@ static struct perf_cpu_map *cpu_map__from_entries(const struct perf_record_cpu_m
static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_data *data)
{
DECLARE_BITMAP(local_copy, 64);
- int weight = 0, mask_nr = data->mask32_data.nr;
+ int weight = 0, mask_nr;
+ /* Snapshot before validation — data is mmap'd and could change */
+ u16 long_size = READ_ONCE(data->mask32_data.long_size);
struct perf_cpu_map *map;
+ /* long_size must be 4 or 8; other values overflow cpus_per_i below */
+ if (long_size != 4 && long_size != 8) {
+ pr_warning("WARNING: cpu_map mask: unsupported long_size %u\n", long_size);
+ return NULL;
+ }
+
+ mask_nr = READ_ONCE(data->mask32_data.nr);
+
for (int i = 0; i < mask_nr; i++) {
- perf_record_cpu_map_data__read_one_mask(data, i, local_copy);
+ perf_record_cpu_map_data__read_one_mask(data, i, local_copy, long_size);
weight += bitmap_weight(local_copy, 64);
}
@@ -106,11 +121,14 @@ static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_
return NULL;
for (int i = 0, j = 0; i < mask_nr; i++) {
- int cpus_per_i = (i * data->mask32_data.long_size * BITS_PER_BYTE);
+ int cpus_per_i = (i * long_size * BITS_PER_BYTE);
int cpu;
- perf_record_cpu_map_data__read_one_mask(data, i, local_copy);
+ perf_record_cpu_map_data__read_one_mask(data, i, local_copy, long_size);
for_each_set_bit(cpu, local_copy, 64) {
+ /* Guard against more set bits than the first pass counted */
+ if (j >= weight)
+ break;
if (cpu + cpus_per_i < INT16_MAX) {
RC_CHK_ACCESS(map)->map[j++].cpu = cpu + cpus_per_i;
} else {
@@ -126,18 +144,28 @@ static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_
static struct perf_cpu_map *cpu_map__from_range(const struct perf_record_cpu_map_data *data)
{
+ /* Snapshot fields — data is mmap'd and could change between reads */
+ u16 start_cpu = READ_ONCE(data->range_cpu_data.start_cpu);
+ u16 end_cpu = READ_ONCE(data->range_cpu_data.end_cpu);
+ u16 any_cpu = READ_ONCE(data->range_cpu_data.any_cpu);
struct perf_cpu_map *map;
unsigned int i = 0;
- map = perf_cpu_map__empty_new(data->range_cpu_data.end_cpu -
- data->range_cpu_data.start_cpu + 1 + data->range_cpu_data.any_cpu);
+ if (end_cpu < start_cpu) {
+ pr_warning("WARNING: cpu_map range: end_cpu %u < start_cpu %u\n",
+ end_cpu, start_cpu);
+ return NULL;
+ }
+
+ /* any_cpu is boolean (0 or 1), not a count — clamp to avoid inflated nr */
+ map = perf_cpu_map__empty_new(end_cpu - start_cpu + 1 + !!any_cpu);
if (!map)
return NULL;
- if (data->range_cpu_data.any_cpu)
+ if (any_cpu)
RC_CHK_ACCESS(map)->map[i++].cpu = -1;
- for (int cpu = data->range_cpu_data.start_cpu; cpu <= data->range_cpu_data.end_cpu;
+ for (int cpu = start_cpu; cpu <= end_cpu;
i++, cpu++) {
if (cpu < INT16_MAX) {
RC_CHK_ACCESS(map)->map[i].cpu = cpu;
--
2.54.0
next prev parent reply other threads:[~2026-05-21 1:11 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-21 1:09 [PATCHES 00/27] perf.data validation and hardening Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 01/27] perf session: Add minimum event size and alignment validation Arnaldo Carvalho de Melo
2026-05-21 1:59 ` sashiko-bot
2026-05-21 13:01 ` Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 02/27] perf tools: Fix event_contains() macro to verify full field extent Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 03/27] perf zstd: Fix compression error path in zstd_compress_stream_to_records() Arnaldo Carvalho de Melo
2026-05-21 1:49 ` sashiko-bot
2026-05-21 1:09 ` [PATCH 04/27] perf zstd: Fix multi-iteration decompression and error handling Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 05/27] perf session: Fix PERF_RECORD_READ swap and dump for variable-length events Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 06/27] perf session: Fix swap_sample_id_all() crash on crafted events Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 07/27] perf session: Add validated swap infrastructure with null-termination checks Arnaldo Carvalho de Melo
2026-05-21 1:52 ` sashiko-bot
2026-05-21 1:09 ` [PATCH 08/27] perf session: Use bounded copy for PERF_RECORD_TIME_CONV Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 09/27] perf session: Validate HEADER_ATTR attr.size before swapping Arnaldo Carvalho de Melo
2026-05-21 2:04 ` sashiko-bot
2026-05-21 1:09 ` [PATCH 10/27] perf session: Validate nr fields against event size on both swap and common paths Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 11/27] perf header: Byte-swap build ID event pid and bounds check section entries Arnaldo Carvalho de Melo
2026-05-21 1:09 ` Arnaldo Carvalho de Melo [this message]
2026-05-21 1:09 ` [PATCH 13/27] perf auxtrace: Harden auxtrace_error event handling Arnaldo Carvalho de Melo
2026-05-21 1:09 ` [PATCH 14/27] perf session: Add byte-swap and bounds check for PERF_RECORD_BPF_METADATA events Arnaldo Carvalho de Melo
2026-05-21 2:23 ` sashiko-bot
2026-05-21 1:10 ` [PATCH 15/27] perf header: Validate null-termination in PERF_RECORD_EVENT_UPDATE string fields Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 16/27] perf tools: Bounds check perf_event_attr fields against attr.size before printing Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 17/27] perf header: Propagate feature section processing errors Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 18/27] perf header: Validate f_attr.ids section before use in perf_session__read_header() Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 19/27] perf header: Validate feature section size and add read path bounds checking Arnaldo Carvalho de Melo
2026-05-21 2:34 ` sashiko-bot
2026-05-21 1:10 ` [PATCH 20/27] perf header: Sanity check HEADER_EVENT_DESC attr.size before swap Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 21/27] perf header: Validate bitmap size before allocating in do_read_bitmap() Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 22/27] perf session: Add byte-swap for PERF_RECORD_COMPRESSED2 events Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 23/27] perf tools: Harden compressed event processing Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 24/27] perf session: Check for decompression buffer size overflow Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 25/27] perf session: Bound nr_cpus_avail and validate sample CPU Arnaldo Carvalho de Melo
2026-05-21 2:43 ` sashiko-bot
2026-05-21 1:10 ` [PATCH 26/27] perf kwork: Bounds check work->cpu before indexing cpus_runtime[] Arnaldo Carvalho de Melo
2026-05-21 1:10 ` [PATCH 27/27] perf test: Add truncated perf.data robustness test Arnaldo Carvalho de Melo
2026-05-21 2:07 ` sashiko-bot
2026-05-21 12:57 ` 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=20260521011027.622268-13-acme@kernel.org \
--to=acme@kernel.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
--cc=noreply@anthropic.com \
--cc=sashiko-bot@kernel.org \
--cc=tglx@linutronix.de \
--cc=williams@redhat.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