* [PATCH v1] perf evsel: Add missing bounds checks in __evsel__parse_sample()
@ 2026-12-01 7:14 Ian Rogers
0 siblings, 0 replies; only message in thread
From: Ian Rogers @ 2026-12-01 7:14 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim
Cc: Jiri Olsa, Adrian Hunter, James Clark, linux-perf-users,
linux-kernel, Ian Rogers
perf_event__check_size() only validates event->header.size against
evsel->sample_size, which covers the fixed-size PERF_SAMPLE_MASK fields
up to PERF_SAMPLE_PERIOD (plus PERF_SAMPLE_IDENTIFIER). Every optional
field parsed after that point in __evsel__parse_sample() must validate
remaining payload bounds before dereferencing array.
The PERF_SAMPLE_PHYS_ADDR, PERF_SAMPLE_CGROUP,
PERF_SAMPLE_DATA_PAGE_SIZE, and PERF_SAMPLE_CODE_PAGE_SIZE branches
dereference *array without calling OVERFLOW_CHECK_u64(array), allowing
a truncated or malformed PERF_RECORD_SAMPLE event to read past the end
of the event buffer.
Add the missing OVERFLOW_CHECK_u64(array) checks to all four fields and
add truncated-sample regression tests for them in sample-parsing.c.
Fixes: 3b0a5daa0610 ("perf tools: Support new sample type for physical address")
Fixes: ba78c1c5461c ("perf tools: Basic support for CGROUP event")
Fixes: 542b88fd1276 ("perf record: Support new sample type for data page size")
Fixes: c1de7f3d84ca ("perf record: Add support for PERF_SAMPLE_CODE_PAGE_SIZE")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/sample-parsing.c | 59 +++++++++++++++++++++++++++++++
tools/perf/util/evsel.c | 4 +++
2 files changed, 63 insertions(+)
diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
index bd30f6d4c31b..acbd353d3588 100644
--- a/tools/perf/tests/sample-parsing.c
+++ b/tools/perf/tests/sample-parsing.c
@@ -510,6 +510,61 @@ static int test_truncated_swapped_raw(u16 event_size, u32 raw_size)
return 0;
}
+static int test_truncated_u64_field(u64 sample_type)
+{
+ struct perf_event_attr attr = {
+ .sample_type = sample_type,
+ };
+ struct {
+ struct perf_event_header header;
+ u64 canary;
+ } input = {
+ .header = {
+ .type = PERF_RECORD_SAMPLE,
+ .size = sizeof(input.header),
+ },
+ .canary = 0xdeadbeefcafe1234ULL,
+ };
+ struct perf_sample sample;
+ struct evsel *evsel;
+ int err;
+
+ evsel = evsel__new(&attr);
+ if (!evsel)
+ return -1;
+
+ evsel->sample_size = __evsel__sample_size(attr.sample_type);
+ err = __evsel__parse_sample(evsel, (union perf_event *)&input,
+ &sample, /*needs_swap=*/false);
+ perf_sample__exit(&sample);
+ evsel__put(evsel);
+
+ if (err != -EFAULT) {
+ pr_debug("truncated sample (%#"PRIx64") returned %d, expected -EFAULT\n",
+ sample_type, err);
+ return -1;
+ }
+ return 0;
+}
+
+static int test_truncated_u64_fields(void)
+{
+ const u64 types[] = {
+ PERF_SAMPLE_PHYS_ADDR,
+ PERF_SAMPLE_CGROUP,
+ PERF_SAMPLE_DATA_PAGE_SIZE,
+ PERF_SAMPLE_CODE_PAGE_SIZE,
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(types); i++) {
+ int err = test_truncated_u64_field(types[i]);
+
+ if (err)
+ return err;
+ }
+ return 0;
+}
+
/**
* test__sample_parsing - test sample parsing.
*
@@ -530,6 +585,10 @@ static int test__sample_parsing(struct test_suite *test __maybe_unused, int subt
if (err)
return err;
+ err = test_truncated_u64_fields();
+ if (err)
+ return err;
+
/* The declared RAW payload extends past an otherwise aligned event. */
err = test_truncated_swapped_raw(sizeof(struct perf_event_header) +
sizeof(u64), 16);
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index d060fca8090f..90269b8d1745 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3872,24 +3872,28 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,
data->phys_addr = 0;
if (type & PERF_SAMPLE_PHYS_ADDR) {
+ OVERFLOW_CHECK_u64(array);
data->phys_addr = *array;
array++;
}
data->cgroup = 0;
if (type & PERF_SAMPLE_CGROUP) {
+ OVERFLOW_CHECK_u64(array);
data->cgroup = *array;
array++;
}
data->data_page_size = 0;
if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
+ OVERFLOW_CHECK_u64(array);
data->data_page_size = *array;
array++;
}
data->code_page_size = 0;
if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
+ OVERFLOW_CHECK_u64(array);
data->code_page_size = *array;
array++;
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-01 7:14 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 7:14 [PATCH v1] perf evsel: Add missing bounds checks in __evsel__parse_sample() Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®