Skip to content

Commit 6b8253f

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 Link to v4: https://lore.kernel.org/all/[email protected]/ Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b Signed-off-by: Arnaud Lecomte <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent 6426e00 commit 6b8253f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

kernel/bpf/stackmap.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
247247
}
248248

249249
static long __bpf_get_stackid(struct bpf_map *map,
250-
struct perf_callchain_entry *trace, u64 flags)
250+
struct perf_callchain_entry *trace, u64 flags, u32 max_depth)
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;
@@ -263,6 +263,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
263263

264264
trace_nr = trace->nr - skip;
265265
trace_len = trace_nr * sizeof(u64);
266+
trace_nr = min(trace_nr, max_depth - skip);
267+
266268
ips = trace->ip + skip;
267269
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
268270
id = hash & (smap->n_buckets - 1);
@@ -322,19 +324,17 @@ static long __bpf_get_stackid(struct bpf_map *map,
322324
BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
323325
u64, flags)
324326
{
325-
u32 max_depth = map->value_size / stack_map_data_size(map);
326-
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
327+
u32 elem_size = stack_map_data_size(map);
327328
bool user = flags & BPF_F_USER_STACK;
328329
struct perf_callchain_entry *trace;
329330
bool kernel = !user;
331+
u32 max_depth;
330332

331333
if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
332334
BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
333335
return -EINVAL;
334336

335-
max_depth += skip;
336-
if (max_depth > sysctl_perf_event_max_stack)
337-
max_depth = sysctl_perf_event_max_stack;
337+
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
338338

339339
trace = get_perf_callchain(regs, 0, kernel, user, max_depth,
340340
false, false);
@@ -343,7 +343,7 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
343343
/* couldn't fetch the stack trace */
344344
return -EFAULT;
345345

346-
return __bpf_get_stackid(map, trace, flags);
346+
return __bpf_get_stackid(map, trace, flags, max_depth);
347347
}
348348

349349
const struct bpf_func_proto bpf_get_stackid_proto = {
@@ -375,6 +375,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
375375
bool kernel, user;
376376
__u64 nr_kernel;
377377
int ret;
378+
u32 elem_size, max_depth;
378379

379380
/* perf_sample_data doesn't have callchain, use bpf_get_stackid */
380381
if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
@@ -393,12 +394,13 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
393394
return -EFAULT;
394395

395396
nr_kernel = count_kernel_ip(trace);
396-
397+
elem_size = stack_map_data_size(map);
397398
if (kernel) {
398399
__u64 nr = trace->nr;
399400

400401
trace->nr = nr_kernel;
401-
ret = __bpf_get_stackid(map, trace, flags);
402+
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
403+
ret = __bpf_get_stackid(map, trace, flags, max_depth);
402404

403405
/* restore nr */
404406
trace->nr = nr;
@@ -410,7 +412,8 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
410412
return -EFAULT;
411413

412414
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
413-
ret = __bpf_get_stackid(map, trace, flags);
415+
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
416+
ret = __bpf_get_stackid(map, trace, flags, max_depth);
414417
}
415418
return ret;
416419
}

0 commit comments

Comments
 (0)