mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next v8 1/3] bpf: refactor max_depth computation in bpf_get_stack()
@ 2025-09-05 13:46 Arnaud lecomte
  2025-09-05 13:47 ` [PATCH bpf-next v8 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud lecomte
  2025-09-05 13:48 ` [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud lecomte
  0 siblings, 2 replies; 6+ messages in thread
From: Arnaud lecomte @ 2025-09-05 13:46 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

From: Arnaud Lecomte <contact@arnaud-lcm.com>

A new helper function stack_map_calculate_max_depth() that
computes the max depth for a stackmap.

Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
---
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/20250903233910.29431-1-contact@arnaud-lcm.com/
---
 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	[flat|nested] 6+ messages in thread

* [PATCH bpf-next v8 2/3] bpf: clean-up bounds checking in __bpf_get_stack
  2025-09-05 13:46 [PATCH bpf-next v8 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud lecomte
@ 2025-09-05 13:47 ` Arnaud lecomte
  2025-09-05 13:48 ` [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud lecomte
  1 sibling, 0 replies; 6+ messages in thread
From: Arnaud lecomte @ 2025-09-05 13:47 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

From: Arnaud Lecomte <contact@arnaud-lcm.com>

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>
Acked-by: Song Liu <song@kernel.org>
Cc: 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;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
  2025-09-05 13:46 [PATCH bpf-next v8 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud lecomte
  2025-09-05 13:47 ` [PATCH bpf-next v8 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud lecomte
@ 2025-09-05 13:48 ` Arnaud lecomte
  2025-09-09 12:47   ` Lecomte, Arnaud
  2025-09-09 16:41   ` Song Liu
  1 sibling, 2 replies; 6+ messages in thread
From: Arnaud lecomte @ 2025-09-05 13:48 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

From: Arnaud Lecomte <contact@arnaud-lcm.com>

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")
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
---
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/20250903234325.30212-1-contact@arnaud-lcm.com/
---
 kernel/bpf/stackmap.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 9f3ae426ddc3..9b57b8307565 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,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;
 }
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
  2025-09-05 13:48 ` [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud lecomte
@ 2025-09-09 12:47   ` Lecomte, Arnaud
  2025-09-09 16:41   ` Song Liu
  1 sibling, 0 replies; 6+ messages in thread
From: Lecomte, Arnaud @ 2025-09-09 12:47 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

Hi,
Can you confirm you received this one Alexei as I got an undelivered 
email reply.
Cheers,
Arnaud

On 05/09/2025 14:48, Arnaud lecomte wrote:
> From: Arnaud Lecomte <contact@arnaud-lcm.com>
>
> 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")
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>
> ---
> 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/20250903234325.30212-1-contact@arnaud-lcm.com/
> ---
>   kernel/bpf/stackmap.c | 17 +++++++++++++----
>   1 file changed, 13 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 9f3ae426ddc3..9b57b8307565 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,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;
>   }
>   

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
  2025-09-05 13:48 ` [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud lecomte
  2025-09-09 12:47   ` Lecomte, Arnaud
@ 2025-09-09 16:41   ` Song Liu
  2025-09-10 20:01     ` Arnaud Lecomte
  1 sibling, 1 reply; 6+ messages in thread
From: Song Liu @ 2025-09-09 16:41 UTC (permalink / raw)
  To: Arnaud lecomte
  Cc: alexei.starovoitov, yonghong.song, andrii, ast, bpf, daniel,
	eddyz87, haoluo, john.fastabend, jolsa, kpsingh, linux-kernel,
	martin.lau, sdf, syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs

On Fri, Sep 5, 2025 at 9:48 AM Arnaud lecomte <contact@arnaud-lcm.com> wrote:
>
> From: Arnaud Lecomte <contact@arnaud-lcm.com>
>
> 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")
> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>

Acked-by: Song Liu <song@kernel.org>

With one nitpick below.

[...]
> @@ -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.

>
>         if (kernel) {
> -               __u64 nr = trace->nr;
> -
[...]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid()
  2025-09-09 16:41   ` Song Liu
@ 2025-09-10 20:01     ` Arnaud Lecomte
  0 siblings, 0 replies; 6+ messages in thread
From: Arnaud Lecomte @ 2025-09-10 20:01 UTC (permalink / raw)
  To: Song Liu
  Cc: alexei.starovoitov, yonghong.song, andrii, ast, bpf, daniel,
	eddyz87, haoluo, john.fastabend, jolsa, kpsingh, linux-kernel,
	martin.lau, sdf, syzbot+c9b724fbb41cf2538b7b, syzkaller-bugs


On 09/09/2025 17:41, Song Liu wrote:
> On Fri, Sep 5, 2025 at 9:48 AM Arnaud lecomte <contact@arnaud-lcm.com> wrote:
>> From: Arnaud Lecomte <contact@arnaud-lcm.com>
>>
>> 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")
>> Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
>> Acked-by: Yonghong Song <yonghong.song@linux.dev>
> Acked-by: Song Liu <song@kernel.org>
>
> With one nitpick below.
>
> [...]
>> @@ -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.
>
Good catch, thanks !
I should maybe wait for comments from other reviewers because raising
an other revision.
>>          if (kernel) {
>> -               __u64 nr = trace->nr;
>> -
> [...]
>
Thanks,
Arnaud


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-10 20:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-05 13:46 [PATCH bpf-next v8 1/3] bpf: refactor max_depth computation in bpf_get_stack() Arnaud lecomte
2025-09-05 13:47 ` [PATCH bpf-next v8 2/3] bpf: clean-up bounds checking in __bpf_get_stack Arnaud lecomte
2025-09-05 13:48 ` [PATCH bpf-next v8 3/3] bpf: fix stackmap overflow check in __bpf_get_stackid() Arnaud lecomte
2025-09-09 12:47   ` Lecomte, Arnaud
2025-09-09 16:41   ` Song Liu
2025-09-10 20:01     ` Arnaud Lecomte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®