summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Rogers <irogers@google.com>2026-05-20 12:05:32 -0700
committerArnaldo Carvalho de Melo <acme@redhat.com>2026-05-20 16:39:40 -0300
commite454fef5d2c6e3fc89bd87a2da491dae3977a9ed (patch)
treeca22352ccf45763ccd9d62d53f56dee84d32cb23
parentb3ab668761786f9df9e19dd2805e657af2f19c41 (diff)
perf synthetic-events: Bound check when synthesizing mmap2 and build_id events
Add robust boundary checks when synthesizing mmap2 and build_id events to ensure that filename fields do not overflow the fixed-size stack allocations or the synthesized event structures. Verify that the filename fits safely within the allocated boundaries of the mmap2 event structure, and prevent potential heap/stack overflow corruptions from excessively long or corrupted kernel filenames. Signed-off-by: Ian Rogers <irogers@google.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexandre Ghiti <alex@ghiti.fr> Cc: Andi Kleen <ak@linux.intel.com> Cc: Andrew Jones <ajones@ventanamicro.com> Cc: Anup Patel <anup@brainfault.org> Cc: Athira Rajeev <atrajeev@linux.ibm.com> Cc: Blake Jones <blakejones@google.com> Cc: Chen Ni <nichen@iscas.ac.cn> Cc: Chun-Tse Shao <ctshao@google.com> Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Cc: Derek Foreman <derek.foreman@collabora.com> Cc: Dmitriy Vyukov <dvyukov@google.com> Cc: Dr. David Alan Gilbert <linux@treblig.org> Cc: Howard Chu <howardchu95@gmail.com> Cc: Hrishikesh Suresh <hrishikesh123s@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Krzysztof Ɓopatowski <krzysztof.m.lopatowski@gmail.com> Cc: Leo Yan <leo.yan@arm.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Paul Walmsley <pjw@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Quan Zhou <zhouquan@iscas.ac.cn> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Swapnil Sapkal <swapnil.sapkal@amd.com> Cc: Thomas Falcon <thomas.falcon@intel.com> Cc: Tianyou Li <tianyou.li@intel.com> Cc: Yujie Liu <yujie.liu@intel.com> Cc: tanze <tanze@kylinos.cn> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/util/synthetic-events.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index fd1d4c0345d6..d665b0f94b32 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -2268,14 +2268,20 @@ int perf_event__synthesize_build_id(const struct perf_tool *tool,
const char *filename)
{
union perf_event ev;
- size_t len;
+ size_t len, filename_len = strlen(filename);
u64 sample_type = sample->evsel ? sample->evsel->core.attr.sample_type : 0;
void *array = &ev;
int ret;
- len = sizeof(ev.build_id) + strlen(filename) + 1;
+ if (filename_len >= PATH_MAX)
+ return -EINVAL;
+
+ len = sizeof(ev.build_id) + filename_len + 1;
len = PERF_ALIGN(len, sizeof(u64));
+ if (len + MAX_ID_HDR_ENTRIES * sizeof(__u64) > sizeof(ev))
+ return -E2BIG;
+
memset(&ev, 0, len);
ev.build_id.size = bid->size;
@@ -2314,14 +2320,21 @@ int perf_event__synthesize_mmap2_build_id(const struct perf_tool *tool,
const char *filename)
{
union perf_event ev;
+ size_t filename_len = strlen(filename);
size_t ev_len;
u64 sample_type = sample->evsel ? sample->evsel->core.attr.sample_type : 0;
void *array;
int ret;
- ev_len = sizeof(ev.mmap2) - sizeof(ev.mmap2.filename) + strlen(filename) + 1;
+ if (filename_len >= sizeof(ev.mmap2.filename))
+ return -EINVAL;
+
+ ev_len = sizeof(ev.mmap2) - sizeof(ev.mmap2.filename) + filename_len + 1;
ev_len = PERF_ALIGN(ev_len, sizeof(u64));
+ if (ev_len + MAX_ID_HDR_ENTRIES * sizeof(__u64) > sizeof(ev))
+ return -E2BIG;
+
memset(&ev, 0, ev_len);
ev.mmap2.header.type = PERF_RECORD_MMAP2;