Skip to content

Commit 6f6cbad

Browse files
ArnaudLcmKernel Patches Daemon
authored andcommitted
bpf: fix stackmap overflow check in __bpf_get_stackid()
Syzkaller reported a KASAN slab-out-of-bounds write in __bpf_get_stackid() when copying stack trace data. The issue occurs when the perf trace contains more stack entries than the stack map bucket can hold, leading to an out-of-bounds write in the bucket's data array. Changes in v2: - Fixed max_depth names across get stack id Changes in v4: - Removed unnecessary empty line in __bpf_get_stackid Changs in v6: - Added back trace_len computation in __bpf_get_stackid Link to v6: https://lore.kernel.org/all/[email protected]/ Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b Fixes: ee2a098 ("bpf: Adjust BPF stack helper functions to accommodate skip > 0") Signed-off-by: Arnaud Lecomte <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent 275a82e commit 6f6cbad

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kernel/bpf/stackmap.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
369369
{
370370
struct perf_event *event = ctx->event;
371371
struct perf_callchain_entry *trace;
372+
u32 elem_size, max_depth;
372373
bool kernel, user;
373374
__u64 nr_kernel;
374375
int ret;
@@ -390,11 +391,15 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
390391
return -EFAULT;
391392

392393
nr_kernel = count_kernel_ip(trace);
394+
elem_size = stack_map_data_size(map);
393395

394396
if (kernel) {
395397
__u64 nr = trace->nr;
396398

397399
trace->nr = nr_kernel;
400+
max_depth =
401+
stack_map_calculate_max_depth(map->value_size, elem_size, flags);
402+
trace->nr = min_t(u32, nr_kernel, max_depth);
398403
ret = __bpf_get_stackid(map, trace, flags);
399404

400405
/* restore nr */
@@ -407,6 +412,9 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
407412
return -EFAULT;
408413

409414
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
415+
max_depth =
416+
stack_map_calculate_max_depth(map->value_size, elem_size, flags);
417+
trace->nr = min_t(u32, trace->nr, max_depth);
410418
ret = __bpf_get_stackid(map, trace, flags);
411419
}
412420
return ret;

0 commit comments

Comments
 (0)