bpf: refactor max_depth computation in bpf_get_stack()
Subsystems (2)
PATCH v8 1/3 bpf: refactor max_depth computation in bpf_get_stack()
From: Arnaud Lecomte <[email protected]>
A new helper function stack_map_calculate_max_depth() that computes the max depth for a stackmap.
---
Changes in v2:
- Removed the checking 'map_size % map_elem_size' from stack_map_calculate_max_depth
- Changed stack_map_calculate_max_depth params name to be more generic
Changes in v3:
- Changed map size param to size in max depth helper
Changes in v4:
- Fixed indentation in max depth helper for args
Changes in v5:
- Bound back trace_nr to num_elem in __bpf_get_stack
- Make a copy of sysctl_perf_event_max_stack in stack_map_calculate_max_depth
Changes in v6:
- Restrained max_depth computation only when required
- Additional cleanup from Song in __bpf_get_stack
Changes in v7:
- Removed additional cleanup from v6
Link to v7: https://lore.kernel.org/all/[email protected]/
--- kernel/bpf/stackmap.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-)
kernel/bpf/stackmap.c +27 -11
@@ -42,6 +42,28 @@ static inline int stack_map_data_size(struct bpf_map *map) sizeof(struct bpf_stack_build_id) : sizeof(u64); } +/** +* stack_map_calculate_max_depth - Calculate maximum allowed stack trace depth +* @size: Size of the buffer/map value in bytes +* @elem_size: Size of each stack trace element +* @flags: BPF stack trace flags (BPF_F_USER_STACK, BPF_F_USER_BUILD_ID, ...) +* +* Return: Maximum number of stack trace entries that can be safely stored +*/ +static u32 stack_map_calculate_max_depth (u32 size,u32 elem_size,u64 flags) +{ +u32 skip = flags &BPF_F_SKIP_FIELD_MASK ; +u32 max_depth; +u32 curr_sysctl_max_stack =READ_ONCE (sysctl_perf_event_max_stack); + + max_depth = size / elem_size; + max_depth += skip; +if (max_depth > curr_sysctl_max_stack) +return curr_sysctl_max_stack; + +return max_depth; +} +static int prealloc_elems_and_freelist (struct bpf_stack_map *smap) { u64 elem_size = sizeof(struct stack_map_bucket) +
@@ -300,20 +322,17 @@ static long __bpf_get_stackid(struct bpf_map *map, BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map, u64, flags) { -u32 max_depth = map->value_size /stack_map_data_size (map); -u32 skip = flags &BPF_F_SKIP_FIELD_MASK ; +u32 elem_size =stack_map_data_size (map);bool user = flags &BPF_F_USER_STACK ;struct perf_callchain_entry *trace;bool kernel = !user; +u32 max_depth;if (unlikely (flags & ~(BPF_F_SKIP_FIELD_MASK |BPF_F_USER_STACK |BPF_F_FAST_STACK_CMP |BPF_F_REUSE_STACKID )))return -EINVAL ; - max_depth += skip; -if (max_depth > sysctl_perf_event_max_stack) - max_depth = sysctl_perf_event_max_stack; - + max_depth =stack_map_calculate_max_depth (map->value_size, elem_size, flags); trace =get_perf_callchain (regs,0 , kernel, user, max_depth, false, false);
@@ -406,8 +425,8 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,struct perf_callchain_entry *trace_in, void *buf, u32 size, u64 flags, bool may_fault) { - u32 trace_nr, copy_len, elem_size, num_elem, max_depth; bool user_build_id = flags & BPF_F_USER_BUILD_ID; +u32 trace_nr, copy_len, elem_size, max_depth;bool crosstask = task && task != current;u32 skip = flags &BPF_F_SKIP_FIELD_MASK ;bool user = flags &BPF_F_USER_STACK ;
@@ -438,10 +457,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, goto clear; } - num_elem = size / elem_size; - max_depth = num_elem + skip; -if (sysctl_perf_event_max_stack < max_depth) - max_depth = sysctl_perf_event_max_stack; + max_depth =stack_map_calculate_max_depth (size, elem_size, flags);if (may_fault)rcu_read_lock ();/* need RCU for perf's callchain below */
Signature · 1 line
2.47.3
PATCH v8 2/3 bpf: clean-up bounds checking in __bpf_get_stack
From: Arnaud Lecomte <[email protected]>
Clean-up bounds checking for trace->nr in__bpf_get_stack by limiting it only tomax_depth.
--- kernel/bpf/stackmap.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/bpf/stackmap.c +5 -4
@@ -462,13 +462,15 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task,if (may_fault)rcu_read_lock ();/* need RCU for perf's callchain below */ -if (trace_in) +if (trace_in) { trace = trace_in; -else if (kernel && task) + trace->nr =min_t (u32, trace->nr, max_depth); + }else if (kernel && task) { trace =get_callchain_entry_for_task (task, max_depth); -else + }else { trace =get_perf_callchain (regs,0 , kernel, user, max_depth, crosstask, false); + }if (unlikely (!trace) || trace->nr < skip) {if (may_fault)
@@ -477,7 +479,6 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, } trace_nr = trace->nr - skip; - trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem; copy_len = trace_nr * elem_size; ips = trace->ip + skip;
Signature · 1 line
2.47.3
PATCH v8 3/3 bpf: fix stackmap overflow check in __bpf_get_stackid()
In reply to Arnaud lecomteFrom: Arnaud Lecomte <[email protected]>
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
Changes in v6:
- Added back trace_len computation in __bpf_get_stackid
Changes in v7:
- Removed usefull trace->nr assignation in bpf_get_stackid_pe
- Added restoration of trace->nr for both kernel and user traces in bpf_get_stackid_pe
Link to v7: https://lore.kernel.org/all/[email protected]/
--- kernel/bpf/stackmap.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
kernel/bpf/stackmap.c +13 -4
@@ -369,6 +369,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx, {struct perf_event *event = ctx->event;struct perf_callchain_entry *trace; +u32 elem_size, max_depth;bool kernel, user;__u64 nr_kernel;int ret;
@@ -390,15 +391,16 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,return -EFAULT ; nr_kernel =count_kernel_ip (trace); + elem_size =stack_map_data_size (map); +__u64 nr = trace->nr;/* save original */ if (kernel) { -__u64 nr = trace->nr; - trace->nr = nr_kernel; + max_depth = +stack_map_calculate_max_depth (map->value_size, elem_size, flags); + trace->nr =min_t (u32, nr_kernel, max_depth); ret =__bpf_get_stackid (map, trace, flags); -/* restore nr */ - trace->nr = nr; }else {/* user */ u64 skip = flags &BPF_F_SKIP_FIELD_MASK ;
@@ -407,8 +409,15 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,return -EFAULT ; flags = (flags & ~BPF_F_SKIP_FIELD_MASK ) | skip; + max_depth = +stack_map_calculate_max_depth (map->value_size, elem_size, flags); + trace->nr =min_t (u32, trace->nr, max_depth); ret =__bpf_get_stackid (map, trace, flags); } + +/* restore nr */ + trace->nr = nr; +return ret; }
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
Hi,
Can you confirm you received this one Alexei as I got an undelivered email reply. Cheers,
Arnaud
Quoted 81 lines · Arnaud lecomte
From: Arnaud Lecomte <[email protected]>
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]Fixes ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")Signed-off-by Arnaud Lecomte <[email protected]>Acked-by Yonghong Song <[email protected]>---
Changes in v2:
- Fixedmax_depthnames across get stack idChanges in v4:
- Removed unnecessary empty line in__bpf_get_stackidChanges in v6:
- Added backtrace_lencomputation in__bpf_get_stackidChanges in v7:
- Removed usefulltrace->nrassignation inbpf_get_stackid_pe
- Added restoration oftrace->nrfor both kernel and user traces inbpf_get_stackid_peLink to v7: https://lore.kernel.org/all/[email protected]/
--- kernel/bpf/stackmap.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)kernel/bpf/stackmap.c +13
-4@@ -369,6 +369,7 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx, {struct perf_event *event = ctx->event;struct perf_callchain_entry *trace; +u32 elem_size, max_depth;bool kernel, user;__u64 nr_kernel;int ret;@@ -390,15 +391,16 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,return -EFAULT ; nr_kernel =count_kernel_ip (trace); + elem_size =stack_map_data_size (map); +__u64 nr = trace->nr;/* save original */ if (kernel) { -__u64 nr = trace->nr; - trace->nr = nr_kernel; + max_depth = +stack_map_calculate_max_depth (map->value_size, elem_size, flags); + trace->nr =min_t (u32, nr_kernel, max_depth); ret =__bpf_get_stackid (map, trace, flags); -/* restore nr */ - trace->nr = nr; }else {/* user */ u64 skip = flags &BPF_F_SKIP_FIELD_MASK ;@@ -407,8 +409,15 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,return -EFAULT ; flags = (flags & ~BPF_F_SKIP_FIELD_MASK ) | skip; + max_depth = +stack_map_calculate_max_depth (map->value_size, elem_size, flags); + trace->nr =min_t (u32, trace->nr, max_depth); ret =__bpf_get_stackid (map, trace, flags); } + +/* restore nr */ + trace->nr = nr; +return ret; }
Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
In reply to Arnaud lecomteQuoted 8 lines · Arnaud lecomte
From: Arnaud Lecomte <[email protected]>
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.
Quoted 5 lines
Reported-by [email protected]Fixes ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")Signed-off-by Arnaud Lecomte <[email protected]>Acked-by Yonghong Song <[email protected]>
With one nitpick below.
[...]
kernel/bpf/stackmap.c BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx, · line 395
Quoted 6 lines
kernel/bpf/stackmap.c +2
-0@@ -390,15 +391,16 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,return -EFAULT ; nr_kernel =count_kernel_ip (trace); + elem_size =stack_map_data_size (map); +__u64 nr = trace->nr;/* save original */
nit: I think all variable declarations should go to the beginning of the {} block.
I am surprised ./scripts/checkpatch.pl doesn't complain this.
Quoted 4 lines
kernel/bpf/stackmap.c +0
-2if (kernel) { -__u64 nr = trace->nr; -
[...]
Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
Quoted 21 lines · Song Liu
Quoted 7 lines · Arnaud lecomte
From: Arnaud Lecomte <[email protected]>
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.Quoted 5 lines
Reported-by [email protected]Fixes ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")Signed-off-by Arnaud Lecomte <[email protected]>Acked-by Yonghong Song <[email protected]>Acked-by Song Liu <[email protected]>With one nitpick below.
[...]
Quoted 3 lines
(quoted diff) +0
-0@@ -390,15 +391,16 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx, return -EFAULT;
Quoted 7 lines
kernel/bpf/stackmap.c BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx, · line 395
Quoted 3 lines
kernel/bpf/stackmap.c +2
-0nr_kernel =count_kernel_ip (trace); + elem_size =stack_map_data_size (map); +__u64 nr = trace->nr;/* save original */ nit: I think all variable declarations should go to the beginning of the {} block.
I am surprised./scripts/checkpatch.pldoesn't complain this.
Good catch, thanks !
I should maybe wait for comments from other reviewers because raising an other revision.
Quoted 5 lines
Quoted 3 lines
kernel/bpf/stackmap.c +0
-2if (kernel) { -__u64 nr = trace->nr; -[...]
Thanks,
Arnaud