Skip to content

Commit 80327eb

Browse files
listoutKernel Patches Daemon
authored andcommitted
bpf: Clamp trace length in __bpf_get_stack to fix OOB write
syzbot reported a stack-out-of-bounds write in __bpf_get_stack() triggered via bpf_get_stack() when capturing a kernel stack trace. After the recent refactor that introduced stack_map_calculate_max_depth(), the code in stack_map_get_build_id_offset() (and related helpers) stopped clamping the number of trace entries (`trace_nr`) to the number of elements that fit into the stack map value (`num_elem`). As a result, if the captured stack contained more frames than the map value can hold, the subsequent memcpy() would write past the end of the buffer, triggering a KASAN report like: BUG: KASAN: stack-out-of-bounds in __bpf_get_stack+0x... Write of size N at addr ... by task syz-executor... Restore the missing clamp by limiting `trace_nr` to `num_elem` before computing the copy length. This mirrors the pre-refactor logic and ensures we never copy more bytes than the destination buffer can hold. No functional change intended beyond reintroducing the missing bound check. Reported-by: [email protected] Fixes: e17d62f ("bpf: Refactor stack map trace depth calculation into helper function") Signed-off-by: Brahmajit Das <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent f882b4c commit 80327eb

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

kernel/bpf/stackmap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
426426
struct perf_callchain_entry *trace_in,
427427
void *buf, u32 size, u64 flags, bool may_fault)
428428
{
429-
u32 trace_nr, copy_len, elem_size, max_depth;
429+
u32 trace_nr, copy_len, elem_size, num_elem, max_depth;
430430
bool user_build_id = flags & BPF_F_USER_BUILD_ID;
431431
bool crosstask = task && task != current;
432432
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
@@ -480,6 +480,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,
480480
}
481481

482482
trace_nr = trace->nr - skip;
483+
num_elem = size / elem_size;
484+
trace_nr = min_t(u32, trace_nr, num_elem);
483485
copy_len = trace_nr * elem_size;
484486

485487
ips = trace->ip + skip;

0 commit comments

Comments
 (0)