Skip to content

Commit bad6fbf

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 Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b Signed-off-by: Arnaud Lecomte <[email protected]>
1 parent 9f77e8d commit bad6fbf

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
@@ -246,7 +246,7 @@ get_callchain_entry_for_task(struct task_struct *task, u32 max_depth)
246246
}
247247

248248
static long __bpf_get_stackid(struct bpf_map *map,
249-
struct perf_callchain_entry *trace, u64 flags)
249+
struct perf_callchain_entry *trace, u64 flags, u32 max_depth)
250250
{
251251
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
252252
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
@@ -262,6 +262,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
262262

263263
trace_nr = trace->nr - skip;
264264
trace_len = trace_nr * sizeof(u64);
265+
trace_nr = min(trace_nr, max_depth - skip);
266+
265267
ips = trace->ip + skip;
266268
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
267269
id = hash & (smap->n_buckets - 1);
@@ -321,19 +323,17 @@ static long __bpf_get_stackid(struct bpf_map *map,
321323
BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
322324
u64, flags)
323325
{
324-
u32 max_depth = map->value_size / stack_map_data_size(map);
325-
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
326+
u32 elem_size = stack_map_data_size(map);
326327
bool user = flags & BPF_F_USER_STACK;
327328
struct perf_callchain_entry *trace;
328329
bool kernel = !user;
330+
u32 max_depth;
329331

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

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

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

345-
return __bpf_get_stackid(map, trace, flags);
345+
return __bpf_get_stackid(map, trace, flags, max_depth);
346346
}
347347

348348
const struct bpf_func_proto bpf_get_stackid_proto = {
@@ -374,6 +374,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
374374
bool kernel, user;
375375
__u64 nr_kernel;
376376
int ret;
377+
u32 elem_size, max_depth;
377378

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

394395
nr_kernel = count_kernel_ip(trace);
395-
396+
elem_size = stack_map_data_size(map);
396397
if (kernel) {
397398
__u64 nr = trace->nr;
398399

399400
trace->nr = nr_kernel;
400-
ret = __bpf_get_stackid(map, trace, flags);
401+
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
402+
ret = __bpf_get_stackid(map, trace, flags, max_depth);
401403

402404
/* restore nr */
403405
trace->nr = nr;
@@ -409,7 +411,8 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
409411
return -EFAULT;
410412

411413
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
412-
ret = __bpf_get_stackid(map, trace, flags);
414+
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
415+
ret = __bpf_get_stackid(map, trace, flags, max_depth);
413416
}
414417
return ret;
415418
}

0 commit comments

Comments
 (0)