BPF List
 help / color / mirror / Atom feed
* [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid()
@ 2026-10-16  2:32 Artem Dinaburg
  2026-10-16  2:39 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Artem Dinaburg @ 2026-10-16  2:32 UTC (permalink / raw)
  To: stable
  Cc: andrii, ast, bpf, contact, daniel, eddyz87, emil, ihor.solodrai,
	jolsa, linux-kernel, martin.lau, memxor, song, yonghong.song

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

[ Upstream commit 23f852daa4bab4d579110e034e4d513f7d490846 ]

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.

Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20251025192941.1500-1-contact@arnaud-lcm.com

Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
Assisted-by: LLM
[ Backport to 6.6.y: stack_map_calculate_max_depth() was introduced by
  e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into
  helper function"), which is not in 6.6.y; open-code the same
  computation in __bpf_get_stackid(). ]
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Hi,

Please consider this backport of the fix for CVE-2025-68378 to 6.6.y.

The only difference from upstream is that the
stack_map_calculate_max_depth() helper, which 6.6.y lacks, is
open-coded in __bpf_get_stackid().

The complete fix is already present in 6.12.y, 6.18.y and 7.2.y.

Build-tested only as part of a cumulative 6.6.y series on x86_64
(allmodconfig, CONFIG_WERROR=y); no runtime testing.

Thanks,
Artem Dinaburg

 kernel/bpf/stackmap.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -216,7 +216,8 @@
 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
-	u32 hash, id, trace_nr, trace_len;
+	u32 hash, id, trace_nr, trace_len, max_depth;
+	u32 curr_sysctl_max_stack;
 	bool user = flags & BPF_F_USER_STACK;
 	u64 *ips;
 	bool hash_matches;
@@ -225,7 +226,12 @@
 		/* skipping more than usable stack trace */
 		return -EFAULT;
 
-	trace_nr = trace->nr - skip;
+	curr_sysctl_max_stack = READ_ONCE(sysctl_perf_event_max_stack);
+	max_depth = map->value_size / stack_map_data_size(map);
+	max_depth += skip;
+	if (max_depth > curr_sysctl_max_stack)
+		max_depth = curr_sysctl_max_stack;
+	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);
@@ -354,15 +360,11 @@
 		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;
 
@@ -373,6 +375,10 @@
 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
 		ret = __bpf_get_stackid(map, trace, flags);
 	}
+
+	/* restore nr */
+	trace->nr = nr;
+
 	return ret;
 }
 

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

* Re: [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid()
  2026-10-16  2:32 Artem Dinaburg
@ 2026-10-16  2:39 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-10-16  2:39 UTC (permalink / raw)
  To: Artem Dinaburg; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: KASAN OOB write due to missing max_depth < skip check
--

commit 02cac1c9914fa38a62e3ae2d92fc6a6dde2513ff
Author: Arnaud Lecomte <contact@arnaud-lcm.com>

bpf: Fix stackmap overflow check in __bpf_get_stackid()

This backport attempts to fix a KASAN slab-out-of-bounds write by capping the
stack trace depth in __bpf_get_stackid(). Since the helper function
stack_map_calculate_max_depth() is not available in 6.6.y, the logic is
open-coded.

Link: https://lore.kernel.org/bpf/20251025192941.1500-1-contact@arnaud-lcm.com

> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index a330f38ae7335..04d873f1a552c 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
[ ... ]
> @@ -225,7 +226,12 @@ static long __bpf_get_stackid(struct bpf_map *map,
>  		/* skipping more than usable stack trace */
>  		return -EFAULT;
>  
> -	trace_nr = trace->nr - skip;
> +	curr_sysctl_max_stack = READ_ONCE(sysctl_perf_event_max_stack);
> +	max_depth = map->value_size / stack_map_data_size(map);
> +	max_depth += skip;
> +	if (max_depth > curr_sysctl_max_stack)
> +		max_depth = curr_sysctl_max_stack;
> +	trace_nr = min_t(u32, trace->nr - skip, max_depth - skip);

[Severity: High]
Does this code risk an unsigned integer underflow when calculating
max_depth - skip?

If sysctl_perf_event_max_stack is lowered concurrently to a value smaller
than skip (which can be up to 255), max_depth will be capped to this smaller
value. The expression max_depth - skip would then underflow to a large
positive value.

This would cause min_t() to select trace->nr - skip, neutralizing the intended
map capacity limit. If trace->nr is populated from pre-recorded perf event
data captured when the sysctl was higher, trace_nr could exceed the bucket
capacity and result in an out-of-bounds memcpy() write onto the heap.

It appears the upstream commit included a check to prevent this underflow,
such as returning -EFAULT if max_depth < skip. Should a similar check be
included in this open-coded backport?

>  	trace_len = trace_nr * sizeof(u64);
>  	ips = trace->ip + skip;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261016023225.19034-1-artem@trailofbits.com?part=1

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

* [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid()
@ 2026-10-21 17:16 Artem Dinaburg
  2026-10-21 17:28 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Artem Dinaburg @ 2026-10-21 17:16 UTC (permalink / raw)
  To: stable
  Cc: andrii, ast, bpf, contact, daniel, eddyz87, emil, ihor.solodrai,
	jolsa, linux-kernel, martin.lau, memxor, song, yonghong.song

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

[ Upstream commit 23f852daa4bab4d579110e034e4d513f7d490846 ]

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.

Fixes: ee2a098851bf ("bpf: Adjust BPF stack helper functions to accommodate skip > 0")
Reported-by: syzbot+c9b724fbb41cf2538b7b@syzkaller.appspotmail.com
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20251025192941.1500-1-contact@arnaud-lcm.com

Closes: https://syzkaller.appspot.com/bug?extid=c9b724fbb41cf2538b7b
Assisted-by: LLM
[ Backport to 6.6.y: stack_map_calculate_max_depth() was introduced by
  e17d62fedd10 ("bpf: Refactor stack map trace depth calculation into
  helper function"), which is not in 6.6.y; open-code the same
  computation in __bpf_get_stackid(). ]
Signed-off-by: Artem Dinaburg <artem@trailofbits.com>
---
Hi,

Please consider this backport of the fix for CVE-2025-68378 to 6.6.y.

The only difference from upstream is that the
stack_map_calculate_max_depth() helper, which 6.6.y lacks, is
open-coded in __bpf_get_stackid().

The complete fix is already present in 6.12.y, 6.18.y and 7.2.y.

Build-tested only as part of a cumulative 6.6.y series on x86_64
(allmodconfig, CONFIG_WERROR=y); no runtime testing.

Thanks,
Artem Dinaburg

 kernel/bpf/stackmap.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -216,7 +216,8 @@
 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
 	struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
 	u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
-	u32 hash, id, trace_nr, trace_len;
+	u32 hash, id, trace_nr, trace_len, max_depth;
+	u32 curr_sysctl_max_stack;
 	bool user = flags & BPF_F_USER_STACK;
 	u64 *ips;
 	bool hash_matches;
@@ -225,7 +226,12 @@
 		/* skipping more than usable stack trace */
 		return -EFAULT;
 
-	trace_nr = trace->nr - skip;
+	curr_sysctl_max_stack = READ_ONCE(sysctl_perf_event_max_stack);
+	max_depth = map->value_size / stack_map_data_size(map);
+	max_depth += skip;
+	if (max_depth > curr_sysctl_max_stack)
+		max_depth = curr_sysctl_max_stack;
+	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);
@@ -354,15 +360,11 @@
 		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;
 
@@ -373,6 +375,10 @@
 		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
 		ret = __bpf_get_stackid(map, trace, flags);
 	}
+
+	/* restore nr */
+	trace->nr = nr;
+
 	return ret;
 }
 

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

* Re: [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid()
  2026-10-21 17:16 [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid() Artem Dinaburg
@ 2026-10-21 17:28 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-10-21 17:28 UTC (permalink / raw)
  To: Artem Dinaburg; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] bpf: integer underflow and OOB write in __bpf_get_stackid()
--

commit 8a7616c005136eb52127894543c589eeca649242
Author: Arnaud Lecomte <contact@arnaud-lcm.com>

bpf: Fix stackmap overflow check in __bpf_get_stackid()

This commit bounds trace_nr in __bpf_get_stackid() to prevent a slab
out-of-bounds write into a stack map bucket when a perf callchain contains
more entries than the bucket can hold. It also restores trace->nr after
both branches of bpf_get_stackid_pe().

Link: https://lore.kernel.org/bpf/20251025192941.1500-1-contact@arnaud-lcm.com

> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
> index 458bb80b14d57..60ecf3e82d97b 100644
> --- a/kernel/bpf/stackmap.c
> +++ b/kernel/bpf/stackmap.c
> @@ -213,7 +213,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 skip = flags & BPF_F_SKIP_FIELD_MASK;
> -	u32 hash, id, trace_nr, trace_len;
> +	u32 hash, id, trace_nr, trace_len, max_depth;
> +	u32 curr_sysctl_max_stack;
>  	bool user = flags & BPF_F_USER_STACK;
>  	u64 *ips;
>  	bool hash_matches;
> @@ -222,7 +223,12 @@ static long __bpf_get_stackid(struct bpf_map *map,
>  		/* skipping more than usable stack trace */
>  		return -EFAULT;
>  
> -	trace_nr = trace->nr - skip;
> +	curr_sysctl_max_stack = READ_ONCE(sysctl_perf_event_max_stack);
> +	max_depth = map->value_size / stack_map_data_size(map);
> +	max_depth += skip;
> +	if (max_depth > curr_sysctl_max_stack)
> +		max_depth = curr_sysctl_max_stack;
> +	trace_nr = min_t(u32, trace->nr - skip, max_depth - skip);

[Severity: Critical]
Can max_depth - skip underflow here and bypass the bucket size check when
__bpf_get_stackid() is called from bpf_get_stackid_pe()?

When a perf event has PERF_SAMPLE_CALLCHAIN enabled, ctx->data->callchain is
populated by perf_callchain() -> get_perf_callchain() with add_mark = true.
In include/linux/perf_event.h:perf_callchain_store_context(), context markers
such as PERF_CONTEXT_KERNEL and PERF_CONTEXT_USER are appended to entry->ip
without incrementing ctx->nr:

include/linux/perf_event.h:perf_callchain_store_context() {
	if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
		struct perf_callchain_entry *entry = ctx->entry;
		entry->ip[entry->nr++] = ip;
		++ctx->contexts;
		return 0;
	} else {
		return -1;
	}
}

Because ctx->nr is bounded by sysctl_perf_event_max_stack while context
markers are counted separately up to sysctl_perf_event_max_contexts_per_stack,
trace->nr can legitimately exceed curr_sysctl_max_stack (for example, up to
curr_sysctl_max_stack + 2 on x86_64 or curr_sysctl_max_stack + 8 on powerpc
when interrupt or signal frame markers are present).

In kernel/bpf/stackmap.c:bpf_get_stackid_pe(), skip comes from
flags & BPF_F_SKIP_FIELD_MASK (up to 255), and for BPF_F_USER_STACK it also
adds nr_kernel:

kernel/bpf/stackmap.c:bpf_get_stackid_pe() {
...
	} else { /* user */
		u64 skip = flags & BPF_F_SKIP_FIELD_MASK;

		skip += nr_kernel;
		if (skip > BPF_F_SKIP_FIELD_MASK)
			return -EFAULT;

		flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
		ret = __bpf_get_stackid(map, trace, flags);
	}
...
}

If curr_sysctl_max_stack < skip < trace->nr, the trace->nr <= skip check in
__bpf_get_stackid() passes, and clamping max_depth to curr_sysctl_max_stack
makes max_depth smaller than skip. The u32 expression max_depth - skip then
wraps around to a large unsigned value, so min_t() selects trace->nr - skip
without clamping it to map->value_size / stack_map_data_size(map).

When trace->nr - skip is larger than the number of elements that fit in
new_bucket->data (for example, on a small stack map with a 1-entry bucket),
wouldn't stack_map_get_build_id_offset() or memcpy(new_bucket->data, ips,
trace_len) in __bpf_get_stackid() still write past the end of
new_bucket->data, and subsequently cause bpf_stackmap_copy() to overflow
value in memcpy() and underflow map->value_size - trace_len in memset()?

kernel/bpf/stackmap.c:__bpf_get_stackid() {
...
		new_bucket = (struct stack_map_bucket *)
			pcpu_freelist_pop(&smap->freelist);
		if (unlikely(!new_bucket))
			return -ENOMEM;
		memcpy(new_bucket->data, ips, trace_len);
	}
...
}

kernel/bpf/stackmap.c:bpf_stackmap_copy() {
...
	trace_len = bucket->nr * stack_map_data_size(map);
	memcpy(value, bucket->data, trace_len);
	memset(value + trace_len, 0, map->value_size - trace_len);
...
}

Also, when skip == curr_sysctl_max_stack and trace->nr > skip, max_depth -
skip evaluates to 0, which records an empty bucket with new_bucket->nr = 0;
and when skip < curr_sysctl_max_stack < max_depth, clamping max_depth to
curr_sysctl_max_stack before subtracting skip truncates frames that would
otherwise fit in the bucket.

Since trace is already unwound before __bpf_get_stackid() is called, should
__bpf_get_stackid() clamp trace->nr - skip directly against the bucket
capacity map->value_size / stack_map_data_size(map), matching how
__bpf_get_stack() clamps trace_nr against num_elem?

kernel/bpf/stackmap.c:__bpf_get_stack() {
...
	trace_nr = trace->nr - skip;
	trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem;
...
}

>  	trace_len = trace_nr * sizeof(u64);
>  	ips = trace->ip + skip;
>  	hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261021171625.206-1-artem@trailofbits.com?part=1

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

end of thread, other threads:[~2026-10-21 17:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-21 17:16 [PATCH 6.6.y] bpf: Fix stackmap overflow check in __bpf_get_stackid() Artem Dinaburg
2026-10-21 17:28 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-10-16  2:32 Artem Dinaburg
2026-10-16  2:39 ` sashiko-bot

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