* [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack()
@ 2025-09-03 23:39 Arnaud Lecomte
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
2025-09-04 22:41 ` [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Song Liu
0 siblings, 2 replies; 8+ messages in thread
From: Arnaud Lecomte @ 2025-09-03 23:39 UTC (permalink / raw)
To: alexei.starovoitov, yonghong.song, song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs, Arnaud Lecomte
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 v6: https://lore.kernel.org/all/20250903135323.97847-1-contact@arnaud-lcm.com/
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/stackmap.c | 38 +++++++++++++++++++++++++++-----------
1 file changed, 27 insertions(+), 11 deletions(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 3615c06b7dfa..ed707bc07173 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -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 */
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack
2025-09-03 23:39 [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud Lecomte
@ 2025-09-03 23:40 ` Arnaud Lecomte
2025-09-03 23:43 ` [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
2025-09-04 22:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Song Liu
2025-09-04 22:41 ` [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Song Liu
1 sibling, 2 replies; 8+ messages in thread
From: Arnaud Lecomte @ 2025-09-03 23:40 UTC (permalink / raw)
To: alexei.starovoitov, yonghong.song, song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs, Arnaud Lecomte
Clean-up bounds checking for trace->nr in
__bpf_get_stack by limiting it only to
max_depth.
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Cc: Song Lui <song@kernel.org>
---
kernel/bpf/stackmap.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index ed707bc07173..9f3ae426ddc3 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -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;
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
@ 2025-09-03 23:43 ` Arnaud Lecomte
2025-09-04 22:45 ` Song Liu
2025-09-04 22:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Song Liu
1 sibling, 1 reply; 8+ messages in thread
From: Arnaud Lecomte @ 2025-09-03 23:43 UTC (permalink / raw)
To: alexei.starovoitov, yonghong.song, song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs, Arnaud Lecomte
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
Changs in v6:
- Added back trace_len computation in __bpf_get_stackid
Link to v6: https://lore.kernel.org/all/20250903135348.97884-1-contact@arnaud-lcm.com/
Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/stackmap.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 9f3ae426ddc3..29e05c9ff1bd 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -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,11 +391,15 @@ 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);
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 */
@@ -407,6 +412,9 @@ 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);
}
return ret;
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
2025-09-03 23:43 ` [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
@ 2025-09-04 22:40 ` Song Liu
2025-09-04 22:52 ` Lecomte, Arnaud
1 sibling, 1 reply; 8+ messages in thread
From: Song Liu @ 2025-09-04 22:40 UTC (permalink / raw)
To: Arnaud Lecomte, alexei.starovoitov, yonghong.song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs
On 9/3/25 4:40 PM, Arnaud Lecomte wrote:
> Clean-up bounds checking for trace->nr in
> __bpf_get_stack by limiting it only to
> max_depth.
>
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Cc: Song Lui <song@kernel.org>
Typo in my name, which is "Song Liu".
This looks right.
Acked-by: Song Liu <song@kernel.org>
> ---
> kernel/bpf/stackmap.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index ed707bc07173..9f3ae426ddc3 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -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;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack()
2025-09-03 23:39 [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud Lecomte
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
@ 2025-09-04 22:41 ` Song Liu
1 sibling, 0 replies; 8+ messages in thread
From: Song Liu @ 2025-09-04 22:41 UTC (permalink / raw)
To: Arnaud Lecomte, alexei.starovoitov, yonghong.song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs
On 9/3/25 4:39 PM, Arnaud Lecomte wrote:
> 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 v6: https://lore.kernel.org/all/20250903135323.97847-1-contact@arnaud-lcm.com/
>
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
> ---
> kernel/bpf/stackmap.c | 38 +++++++++++++++++++++++++++-----------
> 1 file changed, 27 insertions(+), 11 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 3615c06b7dfa..ed707bc07173 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -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 */
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
2025-09-03 23:43 ` [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
@ 2025-09-04 22:45 ` Song Liu
2025-09-04 22:53 ` Lecomte, Arnaud
0 siblings, 1 reply; 8+ messages in thread
From: Song Liu @ 2025-09-04 22:45 UTC (permalink / raw)
To: Arnaud Lecomte, alexei.starovoitov, yonghong.song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs
On 9/3/25 4:43 PM, Arnaud Lecomte wrote:
> 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
>
> Changs in v6:
> - Added back trace_len computation in __bpf_get_stackid
>
> Link to v6: https://lore.kernel.org/all/20250903135348.97884-1-contact@arnaud-lcm.com/
>
> Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
> Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
For future patches, please keep the "Changes in vX.." at the end of
your commit log and after a "---". IOW, something like
Acked-by: Yonghong Song <yonghong.song@linux.dev>
---
changes in v2:
...
---
kernel/bpf/stackmap.c | 8 ++++++++
In this way, the "changes in vXX" part will be removed by git-am.
> ---
> kernel/bpf/stackmap.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 9f3ae426ddc3..29e05c9ff1bd 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -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,11 +391,15 @@ 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);
>
> if (kernel) {
> __u64 nr = trace->nr;
>
> trace->nr = nr_kernel;
this trace->nr = is useless.
> + 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 */
> @@ -407,6 +412,9 @@ 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);
I missed this part earlier. Here we need to restore trace->nr, just like
we did
in the "if (kernel)" branch.
Thanks,
Song
> }
> return ret;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack
2025-09-04 22:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Song Liu
@ 2025-09-04 22:52 ` Lecomte, Arnaud
0 siblings, 0 replies; 8+ messages in thread
From: Lecomte, Arnaud @ 2025-09-04 22:52 UTC (permalink / raw)
To: Song Liu, alexei.starovoitov, yonghong.song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs
On 05/09/2025 00:40, Song Liu wrote:
>
> On 9/3/25 4:40 PM, Arnaud Lecomte wrote:
>> Clean-up bounds checking for trace->nr in
>> __bpf_get_stack by limiting it only to
>> max_depth.
>>
>> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
>> Cc: Song Lui <song@kernel.org>
>
> Typo in my name, which is "Song Liu".
>
> This looks right.
>
> Acked-by: Song Liu <song@kernel.org>
>
Oops sorry !
>> ---
>> kernel/bpf/stackmap.c | 9 +++++----
>> 1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
>> index ed707bc07173..9f3ae426ddc3 100644
>> --- a/kernel/bpf/stackmap.c
>> +++ b/kernel/bpf/stackmap.c
>> @@ -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;
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
2025-09-04 22:45 ` Song Liu
@ 2025-09-04 22:53 ` Lecomte, Arnaud
0 siblings, 0 replies; 8+ messages in thread
From: Lecomte, Arnaud @ 2025-09-04 22:53 UTC (permalink / raw)
To: Song Liu, alexei.starovoitov, yonghong.song
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, martin.lau, sdf,
syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs
On 05/09/2025 00:45, Song Liu wrote:
>
> On 9/3/25 4:43 PM, Arnaud Lecomte wrote:
>> 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
>>
>> Changs in v6:
>> - Added back trace_len computation in __bpf_get_stackid
>>
>> Link to v6:
>> https://lore.kernel.org/all/20250903135348.97884-1-contact@arnaud-lcm.com/
>>
>> Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
>> Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
>> Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to
>> accommodate skip > 0")
>> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
>> Acked-by: Yonghong Song <yonghong.song@linux.dev>
>
> For future patches, please keep the "Changes in vX.." at the end of
Good to know, thanks !
>
> your commit log and after a "---". IOW, something like
>
>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
>
> ---
>
> changes in v2:
>
> ...
>
> ---
>
> kernel/bpf/stackmap.c | 8 ++++++++
>
>
> In this way, the "changes in vXX" part will be removed by git-am.
>
>> ---
>> kernel/bpf/stackmap.c | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
>> index 9f3ae426ddc3..29e05c9ff1bd 100644
>> --- a/kernel/bpf/stackmap.c
>> +++ b/kernel/bpf/stackmap.c
>> @@ -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,11 +391,15 @@ 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);
>> if (kernel) {
>> __u64 nr = trace->nr;
>> trace->nr = nr_kernel;
>
> this trace->nr = is useless.
>
>> + 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 */
>> @@ -407,6 +412,9 @@ 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);
>
> I missed this part earlier. Here we need to restore trace->nr, just
> like we did
>
> in the "if (kernel)" branch.
>
Make sense, thanks !
> Thanks,
>
> Song
>
>> }
>> return ret;
>
Thanks,
Arnaud
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-09-04 22:53 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-03 23:39 [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud Lecomte
2025-09-03 23:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud Lecomte
2025-09-03 23:43 ` [PATCH bpf-next v7 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
2025-09-04 22:45 ` Song Liu
2025-09-04 22:53 ` Lecomte, Arnaud
2025-09-04 22:40 ` [PATCH bpf-next v7 2/3] bpf: clean-up bounds checking in __bpf_get_stack Song Liu
2025-09-04 22:52 ` Lecomte, Arnaud
2025-09-04 22:41 ` [PATCH bpf-next v7 1/3] bpf: refactor max_depth computation in bpf_get_stack() Song Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox