From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 09/27] perf session: Validate HEADER_ATTR attr.size before swapping
Date: Thu, 21 May 2026 02:04:07 +0000 [thread overview]
Message-ID: <20260521020407.DA3A81F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260521011027.622268-10-acme@kernel.org>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] A Time-of-Check to Time-of-Use (TOCTOU) vulnerability allows bypassing the new `attr.size` validation for foreign-endian files.
- [Critical] A 32-bit integer overflow in `nr_ids` calculation leads to a heap buffer overflow in `perf_evsel__alloc_id()`.
- [High] ABI0 pipe events cause zero-length copies and ID array miscalculations in `perf_event__repipe_attr()`.
--
commit 705d678c5ac3163affe4c2f837e830af1d98f4b7
Author: Arnaldo Carvalho de Melo <acme@redhat.com>
perf session: Validate HEADER_ATTR attr.size before swapping
This commit hardens PERF_RECORD_HEADER_ATTR handling against crafted
perf.data files by validating attr.size before swapping. It also ensures
only min(attr.size, sizeof(struct perf_event_attr)) bytes are copied into
a local attr and fixes ABI0 pipe/inject events handling.
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index f30e48eb3fc32..967c3d8ff12c8 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -4770,9 +4770,15 @@ static int read_attr(int fd, struct perf_header *ph,
[ ... ]
> /* read perf_file_section, ids are read in caller */
> ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
> + if (ret <= 0) {
> + if (ret == 0)
> + errno = EIO;
> + return -1;
> + }
>
> - return ret <= 0 ? -1 : 0;
> + return 0;
> }
[Severity: Critical]
This is a pre-existing issue, but does reading f_attr.ids.size here allow an
integer overflow later?
When processing a regular perf.data file, read_attr() reads the
attacker-controlled f_attr.ids.size. In perf_session__read_header(), this is
divided by 8 to compute nr_ids (an int).
If f_attr.ids.size is e.g. 0x100000008, nr_ids becomes 0x20000001.
perf_evsel__alloc_id() allocates memory based on
ncpus * nthreads * sizeof(u64) (1 * 0x20000001 * 8).
On 32-bit platforms where size_t is 32 bits, this multiplication overflows,
resulting in an 8-byte allocation. The subsequent loop:
for (j = 0; j < nr_ids; j++)
will execute 0x20000001 times, writing attacker-controlled IDs out-of-bounds
onto the heap until EOF is reached.
[ ... ]
> @@ -5108,13 +5155,46 @@ int perf_event__process_attr(const struct perf_tool *tool __maybe_unused,
[ ... ]
> - evsel = evsel__new(&event->attr.attr);
> + /*
> + * attr_size = footprint of the attr in the event — determines
> + * where the ID array starts. For ABI0, assume VER0 (64 bytes).
> + *
> + * copy_size = how much we copy into our local struct, capped at
> + * sizeof(attr) so a newer producer's larger attr doesn't
> + * overflow. Fields beyond copy_size are zeroed.
> + *
> + * Do NOT write attr_size back to the event — native-endian
> + * files use MAP_SHARED (read-only), writing would SIGSEGV.
> + * The swap path handles ABI0 in perf_event__attr_swap()
> + * which writes to the writable MAP_PRIVATE copy instead.
> + */
> + attr_size = raw_attr_size ?: PERF_ATTR_SIZE_VER0;
> + copy_size = min(attr_size, sizeof(attr));
> + memcpy(&attr, &event->attr.attr, copy_size);
[Severity: High]
This isn't a bug introduced by this patch, but does the failure to update
event->attr.attr.size cause issues in perf_event__repipe_attr()?
While the patch fixed perf_event__process_attr() to handle ABI0 events
without mutating event->attr.attr.size, perf_event__repipe_attr() appears
to be neglected.
Because event->attr.attr.size remains 0 for native-endian ABI0 events,
perf_event__repipe_attr() performs a zero-byte memcpy(), miscalculates n_ids
to encompass the entire payload, and computes an ID array offset of 0.
Could this corrupt the perf inject output by reading the first 64 bytes of
the attribute structure as ID array elements?
> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
> index d14d8055b2d10..e45d51e898a8e 100644
> --- a/tools/perf/util/session.c
> +++ b/tools/perf/util/session.c
> @@ -613,8 +613,39 @@ do { \
> static int perf_event__hdr_attr_swap(union perf_event *event,
> bool sample_id_all __maybe_unused)
> {
> + u32 attr_size, payload_size;
> size_t size;
>
> + /*
> + * Validate attr.size (still foreign-endian) before calling
> + * perf_event__attr_swap(), which uses it via bswap_safe()
> + * to decide which fields to swap. A crafted attr.size
> + * larger than the event payload would swap past the event
> + * boundary and corrupt adjacent memory.
> + *
> + * header.size alignment is already validated by
> + * perf_session__process_event(). The min_size table
> + * guarantees header.size >= sizeof(header) +
> + * PERF_ATTR_SIZE_VER0, so attr.size is safe to access.
> + */
> + attr_size = bswap_32(event->attr.attr.size);
[Severity: Critical]
This is a pre-existing issue, but does validating event->attr.attr.size here
leave a Time-of-Check to Time-of-Use (TOCTOU) vulnerability?
When processing foreign-endian perf.data files, the file is mapped with
MAP_PRIVATE. If an attacker crafts an event such that its 8-byte header is
at the end of a page and the attr payload begins on the next page,
fetch_mmaped_event() will swap the header, triggering a Copy-On-Write (COW)
on the first page only.
The second page remains shared. perf_event__hdr_attr_swap() then validates
event->attr.attr.size from the shared page.
Next, perf_event__attr_swap() writes to attr->type (triggering COW on the
second page) and reads attr->size again. If the attacker concurrently
modifies attr.size in the file after validation but before the COW, the new
malicious size is copied into the private page and used, leading to an
out-of-bounds memory swap and memory corruption.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260521011027.622268-1-acme@kernel.org?part=9
next prev parent reply other threads:[~2026-05-21 2:04 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 [this message]
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 ` [PATCH 12/27] perf cpumap: Reject RANGE_CPUS with start_cpu > end_cpu Arnaldo Carvalho de Melo
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=20260521020407.DA3A81F000E9@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox