[PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write()
From: Ian Rogers
Date: Mon Nov 23 2026 - 02:19:35 EST
off_cpu_raw has MAX_STACKS + 5 entries and bpf_map_lookup_elem() copies
up to MAX_STACKS frames into &off_cpu_raw[i + 2] (with i == 2, occupying
indices 4..35). When a stack trace fills all MAX_STACKS frames, the
unbounded while (off_cpu_raw[i + 2 + len]) loop reads off_cpu_raw[36]
(which holds the previous sample's cgroup_id) and then reads and writes
past the end of off_cpu_raw. Zero the stack buffer before lookup in case
bpf_map_lookup_elem() fails and bound len by MAX_STACKS.
Fixes: 74069a01609e ("perf record --off-cpu: Dump the remaining PERF_SAMPLE_ in sample_type from BPF's stack trace map")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/bpf_off_cpu.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c
index 71933a71c8d1..a3fdbc3c907f 100644
--- a/tools/perf/util/bpf_off_cpu.c
+++ b/tools/perf/util/bpf_off_cpu.c
@@ -399,10 +399,10 @@ int off_cpu_write(struct perf_session *session)
/* off_cpu_raw[i] is callchain->nr (updated later) */
off_cpu_raw[i + 1] = PERF_CONTEXT_USER;
- off_cpu_raw[i + 2] = 0;
+ memset(&off_cpu_raw[i + 2], 0, MAX_STACKS * sizeof(u64));
bpf_map_lookup_elem(stack, &key.stack_id, &off_cpu_raw[i + 2]);
- while (off_cpu_raw[i + 2 + len])
+ while (len < MAX_STACKS && off_cpu_raw[i + 2 + len])
len++;
off_cpu_raw[i] = len + 1;
--
2.56.1.750.gf70dc682cf-goog