From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Ian Rogers <irogers@google.com>
Subject: [PATCH v1] perf evsel: Add missing bounds checks in __evsel__parse_sample()
Date: Mon, 30 Nov 2026 23:14:34 -0800 [thread overview]
Message-ID: <20261201071435.824674-1-irogers@google.com> (raw)
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
reply other threads:[~2026-12-01 7:14 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261201071435.824674-1-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.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@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/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
all inboxes | Powered by JetHome®