Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20251025192941.1500-1-contact@arnaud-lcm.com

Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b

authored by

Arnaud Lecomte and committed by
Andrii Nakryiko
23f852da e17d62fe

+8 -7
+8 -7
kernel/bpf/stackmap.c
··· 251 251 { 252 252 struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map); 253 253 struct stack_map_bucket *bucket, *new_bucket, *old_bucket; 254 + u32 hash, id, trace_nr, trace_len, i, max_depth; 254 255 u32 skip = flags & BPF_F_SKIP_FIELD_MASK; 255 - u32 hash, id, trace_nr, trace_len, i; 256 256 bool user = flags & BPF_F_USER_STACK; 257 257 u64 *ips; 258 258 bool hash_matches; ··· 261 261 /* skipping more than usable stack trace */ 262 262 return -EFAULT; 263 263 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); 265 266 trace_len = trace_nr * sizeof(u64); 266 267 ips = trace->ip + skip; 267 268 hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0); ··· 391 390 return -EFAULT; 392 391 393 392 nr_kernel = count_kernel_ip(trace); 393 + __u64 nr = trace->nr; /* save original */ 394 394 395 395 if (kernel) { 396 - __u64 nr = trace->nr; 397 - 398 396 trace->nr = nr_kernel; 399 397 ret = __bpf_get_stackid(map, trace, flags); 400 - 401 - /* restore nr */ 402 - trace->nr = nr; 403 398 } else { /* user */ 404 399 u64 skip = flags & BPF_F_SKIP_FIELD_MASK; 405 400 ··· 406 409 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip; 407 410 ret = __bpf_get_stackid(map, trace, flags); 408 411 } 412 + 413 + /* restore nr */ 414 + trace->nr = nr; 415 + 409 416 return ret; 410 417 } 411 418