Skip to contentSkip to conversation

bpf: Fix stackmap overflow check in __bpf_get_stackid()

PATCH New Artem Dinaburg 2 participants 2 messages Oct 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Fix stackmap overflow check in __bpf_get_stackid()

From: Arnaud Lecomte <[email protected]>

[ 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 [email protected]

[ 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(). ]

---
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(-)
kernel/bpf/stackmap.c +13 -7
@@ -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;
 }
 
#1 lore raw
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 --

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 <[email protected]>

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.

Quoted 4 lines
kernel/bpf/stackmap.c +0 -0
index a330f38ae7335..04d873f1a552c 100644

[ ... ]

kernel/bpf/stackmap.c static long __bpf_get_stackid(struct bpf_map *map, · line 234

Quoted 11 lines
kernel/bpf/stackmap.c +6 -1
@@ -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?

Quoted 2 lines
kernel/bpf/stackmap.c +0 -0
 	trace_len = trace_nr * sizeof(u64);
 	ips = trace->ip + skip;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1