Skip to content

Commit 8f08a3b

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. For build_id mode, we use sizeof(struct bpf_stack_build_id) to determine capacity, and for normal mode we use sizeof(u64). Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b Tested-by: [email protected] Signed-off-by: Arnaud Lecomte <[email protected]>
1 parent 6217ef6 commit 8f08a3b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/bpf/stackmap.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static long __bpf_get_stackid(struct bpf_map *map,
230230
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
231231
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
232232
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
233-
u32 hash, id, trace_nr, trace_len, i;
233+
u32 hash, id, trace_nr, trace_len, i, max_depth;
234234
bool user = flags & BPF_F_USER_STACK;
235235
u64 *ips;
236236
bool hash_matches;
@@ -241,6 +241,16 @@ static long __bpf_get_stackid(struct bpf_map *map,
241241

242242
trace_nr = trace->nr - skip;
243243
trace_len = trace_nr * sizeof(u64);
244+
245+
/* Clamp the trace to max allowed depth */
246+
if (stack_map_use_build_id(map))
247+
max_depth = smap->map.value_size / sizeof(struct bpf_stack_build_id);
248+
else
249+
max_depth = smap->map.value_size / sizeof(u64);
250+
251+
if (trace_nr > max_depth)
252+
trace_nr = max_depth;
253+
244254
ips = trace->ip + skip;
245255
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
246256
id = hash & (smap->n_buckets - 1);

0 commit comments

Comments
 (0)