* [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function
@ 2025-10-25 19:28 Arnaud Lecomte
2025-10-25 19:29 ` [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
2025-10-28 16:30 ` [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function patchwork-bot+netdevbpf
0 siblings, 2 replies; 4+ messages in thread
From: Arnaud Lecomte @ 2025-10-25 19:28 UTC (permalink / raw)
To: alexei.starovoitov, andrii.nakryiko, andrii
Cc: ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa, kpsingh,
linux-kernel, martin.lau, sdf, song, syzbot+c9b724fbb41cf2538b7b,
syzkaller-bugs, yonghong.song, Arnaud Lecomte
Extract the duplicated maximum allowed depth computation for stack
traces stored in BPF stacks from bpf_get_stackid() and __bpf_get_stack()
into a dedicated stack_map_calculate_max_depth() helper function.
This unifies the logic for:
- The max depth computation
- Enforcing the sysctl_perf_event_max_stack limit
No functional changes for existing code paths.
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
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
Changes in v9:
- Fixed incorrect removal of num_elem in get stack
Changes in v10:
- Squashed 2 previous patch 1 and 2
Link to v9:
https://lore.kernel.org/all/20250912233409.74900-1-contact@arnaud-lcm.com/
---
---
kernel/bpf/stackmap.c | 47 +++++++++++++++++++++++++++++--------------
1 file changed, 32 insertions(+), 15 deletions(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 4d53cdd1374c..5e9ad050333c 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, kernel, user, max_depth,
false, false);
@@ -406,7 +425,7 @@ 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;
+ u32 trace_nr, copy_len, elem_size, max_depth;
bool user_build_id = flags & BPF_F_USER_BUILD_ID;
bool crosstask = task && task != current;
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
@@ -438,21 +457,20 @@ 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 */
- 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, kernel, user, max_depth,
crosstask, false);
+ }
if (unlikely(!trace) || trace->nr < skip) {
if (may_fault)
@@ -461,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] 4+ messages in thread
* [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid()
2025-10-25 19:28 [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function Arnaud Lecomte
@ 2025-10-25 19:29 ` Arnaud Lecomte
2026-03-29 23:51 ` Guenter Roeck
2025-10-28 16:30 ` [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function patchwork-bot+netdevbpf
1 sibling, 1 reply; 4+ messages in thread
From: Arnaud Lecomte @ 2025-10-25 19:29 UTC (permalink / raw)
To: contact, alexei.starovoitov, andrii.nakryiko, andrii
Cc: ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa, kpsingh,
linux-kernel, martin.lau, sdf, song, syzbot+c9b724fbb41cf2538b7b,
syzkaller-bugs, yonghong.song
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: 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")
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
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
Changes in v9:
- Fixed variable declarations in bpf_get_stackid_pe
- Added the missing truncate of trace_nr in __bpf_getstackid
Changes in v10:
- Remove not required trace->nr = nr_kernel; in bpf_get_stackid_pe
Link to v9:
https://lore.kernel.org/all/20250912233558.75076-1-contact@arnaud-lcm.com/
---
---
kernel/bpf/stackmap.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 5e9ad050333c..2365541c81dd 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -251,8 +251,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
{
struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
+ u32 hash, id, trace_nr, trace_len, i, max_depth;
u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
- u32 hash, id, trace_nr, trace_len, i;
bool user = flags & BPF_F_USER_STACK;
u64 *ips;
bool hash_matches;
@@ -261,7 +261,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
/* skipping more than usable stack trace */
return -EFAULT;
- trace_nr = trace->nr - skip;
+ max_depth = stack_map_calculate_max_depth(map->value_size, stack_map_data_size(map), flags);
+ trace_nr = min_t(u32, trace->nr - skip, max_depth - skip);
trace_len = trace_nr * sizeof(u64);
ips = trace->ip + skip;
hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
@@ -390,15 +391,11 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
return -EFAULT;
nr_kernel = count_kernel_ip(trace);
+ __u64 nr = trace->nr; /* save original */
if (kernel) {
- __u64 nr = trace->nr;
-
trace->nr = nr_kernel;
ret = __bpf_get_stackid(map, trace, flags);
-
- /* restore nr */
- trace->nr = nr;
} else { /* user */
u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
@@ -409,6 +406,10 @@ BPF_CALL_3(bpf_get_stackid_pe, struct bpf_perf_event_data_kern *, ctx,
flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
ret = __bpf_get_stackid(map, trace, flags);
}
+
+ /* restore nr */
+ trace->nr = nr;
+
return ret;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function
2025-10-25 19:28 [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function Arnaud Lecomte
2025-10-25 19:29 ` [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
@ 2025-10-28 16:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-10-28 16:30 UTC (permalink / raw)
To: Arnaud Lecomte
Cc: alexei.starovoitov, andrii.nakryiko, andrii, ast, bpf, daniel,
eddyz87, haoluo, john.fastabend, jolsa, kpsingh, linux-kernel,
martin.lau, sdf, song, syzbot+c9b724fbb41cf2538b7b,
syzkaller-bugs, yonghong.song
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Sat, 25 Oct 2025 19:28:58 +0000 you wrote:
> Extract the duplicated maximum allowed depth computation for stack
> traces stored in BPF stacks from bpf_get_stackid() and __bpf_get_stack()
> into a dedicated stack_map_calculate_max_depth() helper function.
>
> This unifies the logic for:
> - The max depth computation
> - Enforcing the sysctl_perf_event_max_stack limit
>
> [...]
Here is the summary with links:
- [V10,RESEND,1/2] bpf: refactor stack map trace depth calculation into helper function
https://git.kernel.org/bpf/bpf-next/c/e17d62fedd10
- [V10,RESEND,2/2] bpf: fix stackmap overflow check in __bpf_get_stackid()
https://git.kernel.org/bpf/bpf-next/c/23f852daa4ba
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid()
2025-10-25 19:29 ` [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
@ 2026-03-29 23:51 ` Guenter Roeck
0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2026-03-29 23:51 UTC (permalink / raw)
To: Arnaud Lecomte
Cc: alexei.starovoitov, andrii.nakryiko, andrii, ast, bpf, daniel,
eddyz87, haoluo, john.fastabend, jolsa, kpsingh, linux-kernel,
martin.lau, sdf, song, syzbot+c9b724fbb41cf2538b7b,
syzkaller-bugs, yonghong.song
Hi,
On Sat, Oct 25, 2025 at 07:29:41PM +0000, 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.
>
> 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")
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
> Acked-by: Song Liu <song@kernel.org>
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> ---
> 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
>
> Changes in v9:
> - Fixed variable declarations in bpf_get_stackid_pe
> - Added the missing truncate of trace_nr in __bpf_getstackid
>
> Changes in v10:
> - Remove not required trace->nr = nr_kernel; in bpf_get_stackid_pe
>
> Link to v9:
> https://lore.kernel.org/all/20250912233558.75076-1-contact@arnaud-lcm.com/
> ---
> ---
> kernel/bpf/stackmap.c | 15 ++++++++-------
> 1 file changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 5e9ad050333c..2365541c81dd 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -251,8 +251,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
> {
> struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
> struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
> + u32 hash, id, trace_nr, trace_len, i, max_depth;
> u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
> - u32 hash, id, trace_nr, trace_len, i;
> bool user = flags & BPF_F_USER_STACK;
> u64 *ips;
> bool hash_matches;
> @@ -261,7 +261,8 @@ static long __bpf_get_stackid(struct bpf_map *map,
> /* skipping more than usable stack trace */
> return -EFAULT;
>
> - trace_nr = trace->nr - skip;
> + max_depth = stack_map_calculate_max_depth(map->value_size, stack_map_data_size(map), flags);
> + trace_nr = min_t(u32, trace->nr - skip, max_depth - skip);
I stumbled over this patch while researching a bpf related crash.
I assume that the problem it tries to solve is to prevent the
"skip > max_depth" condition.
From the context, it is guaranteed that trace->nr > skip, so we know that
trace->nr - skip is positive. If skip <= max_depth, the above then
constraints trace_nr to the difference, and there is no problem. However,
if skip > max_depth, "max_depth - skip" will be a large positive number,
effectively making trace->nr - skip unrestricted.
Is the condition "max_depth >= skip" guaranteed somewhere ? skip itself
seems to be provided by userspace, and stack_map_calculate_max_depth()
returns at most curr_sysctl_max_stack. What happens if skip is larger
than curr_sysctl_max_stack ?
My apologies for the noise if I am completely missing the point.
Thanks,
Guenter
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-03-29 23:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-25 19:28 [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function Arnaud Lecomte
2025-10-25 19:29 ` [PATCH V10 RESEND 2/2] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud Lecomte
2026-03-29 23:51 ` Guenter Roeck
2025-10-28 16:30 ` [PATCH V10 RESEND 1/2] bpf: refactor stack map trace depth calculation into helper function patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox