Skip to content

Commit 09115ff

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. Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b Fixes: ee2a098 ("bpf: Adjust BPF stack helper functions to accommodate skip > 0") Acked-by: Yonghong Song <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Arnaud Lecomte <[email protected]>
1 parent 77514dd commit 09115ff

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

kernel/bpf/stackmap.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
251251
{
252252
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
253253
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
254+
u32 hash, id, trace_nr, trace_len, i, max_depth;
254255
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
255-
u32 hash, id, trace_nr, trace_len, i;
256256
bool user = flags & BPF_F_USER_STACK;
257257
u64 *ips;
258258
bool hash_matches;
@@ -261,7 +261,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
261261
/* skipping more than usable stack trace */
262262
return -EFAULT;
263263

264-
trace_nr = trace->nr - skip;
264+
max_depth = stack_map_calculate_max_depth(map->value_size, stack_map_data_size(map), flags);
265+
trace_nr = min_t(u32, trace->nr - skip, max_depth - skip);
265266
trace_len = trace_nr * sizeof(u64);
266267
ips = trace->ip + skip;
267268
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
@@ -390,15 +391,11 @@ 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+
__u64 nr = trace->nr; /* save original */
393395

394396
if (kernel) {
395-
__u64 nr = trace->nr;
396-
397397
trace->nr = nr_kernel;
398398
ret = __bpf_get_stackid(map, trace, flags);
399-
400-
/* restore nr */
401-
trace->nr = nr;
402399
} else { /* user */
403400
u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
404401

@@ -409,6 +406,10 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
409406
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
410407
ret = __bpf_get_stackid(map, trace, flags);
411408
}
409+
410+
/* restore nr */
411+
trace->nr = nr;
412+
412413
return ret;
413414
}
414415

0 commit comments

Comments
 (0)