[PATCH 3/5] perf header: Sanity check HEADER_EVENT_DESC

Arnaldo Carvalho de Melo
2026-04-16T00:14:47Z

From: Arnaldo Carvalho de Melo <acme@redhat.com>

read_event_desc() reads nre (event count), sz (attr size), and nr
(IDs per event) from the file and uses them to control allocations
and loops without validating them against the section size.

A crafted perf.data could trigger large allocations or many loop
iterations before __do_read() eventually rejects the reads.

Add bounds checks:
- Reject sz == 0 or sz exceeding the section size.
- Check that nre events fit in the remaining section, using the
minimum per-event footprint of sz + sizeof(u32).
- Check that nr IDs fit in the remaining section before allocating.

Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Assisted-by: Claude Code:claude-opus-4-6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/header.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index c27ed90727ea6629..3302748bac786fdf 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2141,6 +2141,13 @@ static struct evsel *read_event_desc(struct feat_fd *ff)
if (do_read_u32(ff, &sz))
goto error;

+ /*
+ * The minimum section footprint per event is sz bytes for the attr
+ * plus a u32 for the id count, check that nre events fit.
+ */
+ if (sz == 0 || sz > ff->size || nre > (ff->size - ff->offset) / (sz + sizeof(u32)))
+ goto error;
+
/* buffer to hold on file attr struct */
buf = malloc(sz);
if (!buf)
@@ -2186,6 +2193,9 @@ static struct evsel *read_event_desc(struct feat_fd *ff)
if (!nr)
continue;

+ if (nr > (ff->size - ff->offset) / sizeof(*id))
+ goto error;
+
id = calloc(nr, sizeof(*id));
if (!id)
goto error;
--
2.53.0