* [PATCH bpf 0/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare()
@ 2026-11-19 2:42 Ren Wei
2026-11-19 2:42 ` [PATCH bpf 1/2] " Ren Wei
2026-11-19 2:42 ` [PATCH bpf 2/2] selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow Ren Wei
0 siblings, 2 replies; 3+ messages in thread
From: Ren Wei @ 2026-11-19 2:42 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, revest, dingyihan,
bigeasy, paul.chaignon, vega, sashiko-bot, rakukuip, weir
From: Luxiao Xu <rakukuip@gmail.com>
Hi Linux kernel maintainers,
We found and validated an issue in kernel/bpf/helpers.c affecting
bpf_bprintf_prepare() and all BPF formatted-output helpers that rely on
it (bpf_snprintf(), bpf_trace_printk(), bpf_trace_vprintk(),
bpf_seq_printf(), bpf_stream_vprintk(), and scx_bpf_exit_bstr()).
This bug is tracked at: https://bugtracker.nebusec.ai/f/15629
Privilege requirements & reachability:
In our validation environment, unprivileged BPF is disabled at runtime
(kernel.unprivileged_bpf_disabled = 2). Loading the BPF raw tracepoint
program (SEC("raw_tp/sys_enter")) fails with EPERM (-1) for unprivileged
users (test_user) and requires root (UID 0) / CAP_BPF + CAP_PERFMON.
The reproducer therefore runs under UID 0.
Impact boundary (vulnerability & fix scope):
1. Vulnerability impact:
When %pB or %pI4/%pI6 truncates in the 512-byte per-CPU bin_args
scratch buffer, bpf_bprintf_prepare() advances tmp_buf by (err + 1)
using the full untruncated length returned by snprintf(). Once
tmp_buf moves past tmp_buf_end, subsequent specifiers compute a
negative signed difference (tmp_buf_end - tmp_buf), which converts
to a large unsigned size_t in later bounds checks and snprintf()
calls. Follow-on specifiers can write past bin_args into adjacent
per-CPU bpf_bprintf_buffers storage and trigger WARN_ON_ONCE(size >
INT_MAX) in vsnprintf() (panicking when panic_on_warn=1).
2. Effect of the fix on non-target paths and all callers:
- Load-time format verification (kernel/bpf/verifier.c:
check_bpf_snprintf_call(), where data->get_bin_args is false and
tmp_buf is NULL): completely unaffected. Both %pB and %pI4/%pI6
only execute the snprintf() bounds checks when tmp_buf is non-NULL.
- Valid in-bounds %pB / %pI4 / %pi4 / %pI6 / %pi6 calls: unchanged.
The MAX_BPRINTF_BIN_ARGS (512-byte) buffer capacity is unchanged.
Whenever the formatted string plus NUL fits within the remaining
space (err < tmp_buf_end - tmp_buf), execution produces the exact
same bin_args layout and formatted output as before.
- Truncated %pB / %pI4 / %pi4 / %pI6 / %pi6 calls: instead of
advancing tmp_buf past tmp_buf_end and leaving inconsistent cursor
state, bpf_bprintf_prepare() now returns -ENOSPC immediately and
releases the per-CPU buffers via bpf_bprintf_cleanup() at out:.
(Note: %pI4/%pI6 already returned -ENOSPC when fewer than 4 or 16
bytes remained.)
- Other format specifiers (%s/%pks/%pus, %c, integers, plain/symbol
pointers %p/%pK/%px/%ps/%pS): semantics are unchanged. %s continues
to use bpf_trace_copy_string(), which safely bounds copies to
tmp_buf_end - tmp_buf. Casting sizeof_cur_ip and sizeof_cur_arg to
ptrdiff_t in the remaining-space checks is a no-op whenever
tmp_buf <= tmp_buf_end and prevents signed/unsigned wraparound if a
negative pointer difference were ever observed.
- Runtime callers (bpf_snprintf(), bpf_trace_printk(),
bpf_trace_vprintk(), bpf_seq_printf(), bpf_stream_vprintk(), and
scx_bpf_exit_bstr()): all callers already check for negative return
values from bpf_bprintf_prepare() and bail out without invoking
bstr_printf()/seq_bprintf() or double-calling bpf_bprintf_cleanup().
This patch series resolves the issue in two commits:
- Patch 1/2 checks whether err >= tmp_buf_end - tmp_buf after snprintf()
in both the %pB and %pI4/%pI6 branches of bpf_bprintf_prepare(),
returning -ENOSPC when the formatted output does not fit, and casts
sizeof_cur_ip and sizeof_cur_arg to ptrdiff_t in remaining-space
comparisons to prevent signed/unsigned wraparound.
- Patch 2/2 adds regression test cases to tools/testing/selftests/bpf
(progs/test_snprintf.c and prog_tests/snprintf.c) covering %pi4, %pI6,
and %pB scratch buffer overflow and boundary conditions.
=== details below ===
Bug details:
In bpf_bprintf_prepare(), the %pB and %pI4/%pI6 branches pre-format
arguments into the 512-byte per-CPU bin_args scratch buffer via
snprintf(tmp_buf, tmp_buf_end - tmp_buf, ...) and unconditionally advance:
tmp_buf += err + 1;
However:
1. The %pB branch performs no remaining-space check at all.
2. The %pI4/%pI6 branch only checks:
if (tmp_buf_end - tmp_buf < sizeof_cur_ip)
where sizeof_cur_ip is 4 or 16 bytes, whereas the formatted IP string
is up to 16 bytes for IPv4 or 40 bytes for IPv6 (including NUL).
If snprintf() returns err >= tmp_buf_end - tmp_buf, tmp_buf jumps past
tmp_buf_end. Subsequent checks (such as tmp_buf_end - tmp_buf <
sizeof_cur_arg or tmp_buf_end == tmp_buf) either underflow when
converted to size_t or fail to match, allowing out-of-bounds writes past
bin_args and causing subsequent snprintf() calls to pass a wrapped size
to vsnprintf(), hitting WARN_ON_ONCE(size > INT_MAX).
Testing & Validation:
We validated both the baseline reproduction and the fix in an x86_64
QEMU guest (2 vCPUs, 2GB RAM, CONFIG_KASAN=y, CONFIG_BPF_EVENTS=y,
panic_on_warn=0) built from bpf/master (4b4eb7f44ee0).
1. Pre-fix verification:
Running poc_warn triggers the cursor overflow across %pI6, %pI4, and
%pB, followed by a WARNING in vsnprintf() (size > INT_MAX with
R09 = -19 = 0xffffffffffffffed) and out-of-bounds %lld writes:
- case=0 (%s 449B + %pI6 + %s): ret=499 tail=present
- case=1 (%s 500B + %pI6 + %s): ret=-28 (-ENOSPC, pre-existing <16 check)
- case=2 (%s 491B + %pI6 + %pI6): ret=511 + WARNING at lib/vsprintf.c:2875
- case=3 (%s 505B + %pi4/%pI4 + %pI4): ret=511
- case=4 (%s 505B + %pB + %pB): ret=511
- case=5 (%s 491B + %pI6 + %lld): ret=512 (wrote 8B past bin_args)
2. Post-fix verification:
- Running poc_warn returns -28 (-ENOSPC) cleanly for cases 1..5 while
preserving case=0 (ret=499 tail=present), with zero kernel warnings
or KASAN reports in dmesg.
- Running the updated snprintf selftest (snprintf_positive and
snprintf_negative) passes all assertions with zero dmesg warnings.
3. Style and patch checks:
- git diff --check HEAD~2..HEAD: 0 issues
- git apply --check on unpatched bpf/master: clean
- scripts/checkpatch.pl --strict: 0 errors
=== BEGIN poc_warn.bpf.c ===
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <linux/types.h>
#include <bpf/bpf_helpers.h>
#define CHUNK64 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
#define CHUNK42 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
extern const void schedule __ksym;
struct res_entry {
__s32 ret;
char buf[1024];
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 6);
__type(key, __u32);
__type(value, struct res_entry);
} results SEC(".maps");
static const char fill_448[] =
CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64;
static const char fill_490[] =
CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK42;
static const char fill_499[] =
CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
static const char fill_504[] =
CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK64 CHUNK42 "CCCCCCCCCCCCCC";
static const char tail_str[] = "TAIL_MARKER";
static const unsigned char ip6[16] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
static const unsigned char ip4[4] = { 255, 255, 255, 255 };
__u32 target_pid = 0;
SEC("raw_tp/sys_enter")
int test_bprintf_warn(void *ctx)
{
__u32 pid = bpf_get_current_pid_tgid() >> 32;
unsigned long long args[3];
struct res_entry *r;
__u32 k;
if (pid != target_pid)
return 0;
/* case 0: control %s (449B) + %pI6 (40B) + %s (12B) = 501B <= 512B */
k = 0;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_448;
args[1] = (unsigned long long)(long)ip6;
args[2] = (unsigned long long)(long)tail_str;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pI6%s", args, sizeof(args));
}
/* case 1: %s (500B) + %pI6 (12B left < 16) + %s -> -ENOSPC (-28) */
k = 1;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_499;
args[1] = (unsigned long long)(long)ip6;
args[2] = (unsigned long long)(long)tail_str;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pI6%s", args, sizeof(args));
}
/* case 2: %s (491B) + %pI6 (needs 40B, 21B left >= 16 -> jumps past end!) + %pI6 (WARN!) */
k = 2;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_490;
args[1] = (unsigned long long)(long)ip6;
args[2] = (unsigned long long)(long)ip6;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pI6%pI6", args, sizeof(args));
}
/* case 3: %s (505B) + %pI4 (needs 16B, 7B left >= 4 -> jumps past end!) + %pI4 (WARN!) */
k = 3;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_504;
args[1] = (unsigned long long)(long)ip4;
args[2] = (unsigned long long)(long)ip4;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pI4%pI4", args, sizeof(args));
}
/* case 4: %s (505B) + %pB (7B left -> jumps past end!) + %pB (WARN!) */
k = 4;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_504;
args[1] = (unsigned long long)(long)&schedule;
args[2] = (unsigned long long)(long)&schedule;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pB%pB", args, sizeof(args));
}
/* case 5: %s (491B) + %pI6 (21B left -> jumps -19B past end!) + %lld (writes 8B OOB!) */
k = 5;
r = bpf_map_lookup_elem(&results, &k);
if (r) {
args[0] = (unsigned long long)(long)fill_490;
args[1] = (unsigned long long)(long)ip6;
args[2] = 0x1122334455667788ULL;
barrier_var(args[0]);
r->ret = bpf_snprintf(r->buf, sizeof(r->buf), "%s%pI6%lld", args, sizeof(args));
}
return 0;
}
char LICENSE[] SEC("license") = "GPL";
=== END poc_warn.bpf.c ===
=== BEGIN poc_warn.c ===
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
struct res_entry {
int ret;
char buf[1024];
};
int main(int argc, char **argv)
{
const char *obj_path = argc > 1 ? argv[1] : "./poc_warn.bpf.o";
struct bpf_object *obj;
struct bpf_program *prog;
struct bpf_link *link;
struct bpf_map *map;
int map_fd, key;
obj = bpf_object__open_file(obj_path, NULL);
if (libbpf_get_error(obj)) {
fprintf(stderr, "open failed\n");
return 1;
}
if (bpf_object__load(obj)) {
fprintf(stderr, "load failed\n");
return 1;
}
map = bpf_object__find_map_by_name(obj, ".bss");
if (map) {
__u32 zero = 0;
__u32 pid = getpid();
bpf_map_update_elem(bpf_map__fd(map), &zero, &pid, BPF_ANY);
}
prog = bpf_object__find_program_by_name(obj, "test_bprintf_warn");
link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
if (libbpf_get_error(link)) {
fprintf(stderr, "attach failed: %ld\n", libbpf_get_error(link));
return 1;
}
syscall(SYS_getpid);
map = bpf_object__find_map_by_name(obj, "results");
map_fd = bpf_map__fd(map);
for (key = 0; key < 6; key++) {
struct res_entry e = {};
bpf_map_lookup_elem(map_fd, &key, &e);
printf("case=%d ret=%d tail=%s buf=%.32s\n", key, e.ret,
strstr(e.buf, "TAIL_MARKER") ? "present" : "missing", e.buf);
}
bpf_link__destroy(link);
bpf_object__close(obj);
return 0;
}
=== END poc_warn.c ===
=== BEGIN warning log ===
[ 319.177942] ------------[ cut here ]------------
[ 319.177950] size > INT_MAX
[ 319.177965] WARNING: lib/vsprintf.c:2875 at vsnprintf+0xfab/0x12a0, CPU#1: poc_warn/9504
[ 319.179703] Modules linked in:
[ 319.180253] CPU: 1 UID: 0 PID: 9504 Comm: poc_warn Not tainted 7.4.0-rc1-00171-g4b4eb7f44ee0 #1 PREEMPT(full)
[ 319.181229] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 319.182420] RIP: 0010:vsnprintf+0xfab/0x12a0
[ 319.182902] Code: 0f b6 74 24 18 4c 01 6c 24 40 e8 30 a6 01 f6 c7 44 24 74 00 00 00 00 c7 44 24 18 00 00 00 00 e9 15 f8 ff ff e8 16 a6 01 f6 90 <0f> 0b 90 31 db e9 7a f4 ff ff e8 06 a6 01 f6 90 0f 0b 90 bb ff ff
[ 319.184667] RSP: 0018:ffffc9000419fa28 EFLAGS: 00010293
[ 319.185245] RAX: 0000000000000000 RBX: fffff52000833f62 RCX: 0000000000000001
[ 319.185978] RDX: ffff8880262cddc0 RSI: ffffffff8bc3d6da RDI: 0000000000000007
[ 319.186663] RBP: ffffc9000419fb00 R08: 000000007fffffff R09: ffffffffffffffed
[ 319.187387] R10: ffff8880262cddc7 R11: 66663a666666663a R12: ffff88806a932833
[ 319.188128] R13: ffffc9000419fc80 R14: ffffffffffffffed R15: ffff88806a932833
[ 319.189247] FS: 0000000030fad380(0000) GS:ffff8880d62e4000(0000) knlGS:0000000000000000
[ 319.190229] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 319.190858] CR2: 00007fc8691e9000 CR3: 000000004b566000 CR4: 00000000000506f0
[ 319.191797] Call Trace:
[ 319.192173] <TASK>
[ 319.192444] ? __pfx_vsnprintf+0x10/0x10
[ 319.192868] ? __sys_bpf+0x218/0x4b40
[ 319.193272] ? widen_string+0xdc/0x2d0
[ 319.193742] snprintf+0xc7/0x100
[ 319.194111] ? __pfx_snprintf+0x10/0x10
[ 319.194527] ? string+0x2c1/0x4f0
[ 319.194936] ? copy_from_kernel_nofault_allowed+0xf8/0x130
[ 319.195514] bpf_bprintf_prepare+0x1181/0x13e0
[ 319.196009] ? __pfx_bpf_bprintf_prepare+0x10/0x10
[ 319.196512] ? __pfx_bstr_printf+0x10/0x10
[ 319.196957] ? __lock_acquire+0x5a5/0x1f70
[ 319.197434] bpf_snprintf+0x120/0x1e0
[ 319.197855] ? __pfx_bpf_snprintf+0x10/0x10
[ 319.198332] ? lock_acquire+0x1cb/0x370
[ 319.198782] ? bpf_trace_run2+0x5e4/0xd40
[ 319.199220] ? bpf_prog_5c039b4ee422ddd3_test_bprintf_warn+0x23c/0x557
[ 319.199981] bpf_prog_5c039b4ee422ddd3_test_bprintf_warn+0x2b3/0x557
[ 319.200648] bpf_trace_run2+0x332/0xd40
[ 319.201103] ? __pfx_bpf_trace_run2+0x10/0x10
[ 319.201594] ? __might_fault+0xbc/0x130
[ 319.202088] ? __might_fault+0x103/0x130
[ 319.202519] trace_syscall_enter+0xad/0x120
[ 319.202964] do_syscall_64+0x6c7/0x770
[ 319.203374] entry_SYSCALL_64_after_hwframe+0x77/0x7f
=== END warning log ===
Best regards,
Luxiao Xu
Luxiao Xu (2):
bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare()
selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow
kernel/bpf/helpers.c | 12 +++++++++--
.../selftests/bpf/prog_tests/snprintf.c | 6 ++++++
.../selftests/bpf/progs/test_snprintf.c | 21 +++++++++++++++++++
3 files changed, 37 insertions(+), 2 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH bpf 1/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare()
2026-11-19 2:42 [PATCH bpf 0/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare() Ren Wei
@ 2026-11-19 2:42 ` Ren Wei
2026-11-19 2:42 ` [PATCH bpf 2/2] selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow Ren Wei
1 sibling, 0 replies; 3+ messages in thread
From: Ren Wei @ 2026-11-19 2:42 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, revest, dingyihan,
bigeasy, paul.chaignon, vega, sashiko-bot, rakukuip, weir
From: Luxiao Xu <rakukuip@gmail.com>
In bpf_bprintf_prepare(), the %pB and %pI4/%pI6 branches pre-format their
arguments into the per-CPU bin_args scratch buffer using snprintf() and
then advance tmp_buf by (err + 1). However, snprintf() returns the full
length that would have been produced excluding the trailing NUL byte, not
the number of bytes actually stored.
The %pB branch performs no bounds check at all, and the %pI4/%pI6 branch
only checks that the remaining space is at least sizeof_cur_ip (4 or 16
bytes), which is smaller than formatted IP strings (up to 16 bytes for
IPv4 and 40 bytes for IPv6). When the remaining buffer space is smaller
than err + 1, tmp_buf advances past tmp_buf_end.
On subsequent format specifiers, the signed difference
(tmp_buf_end - tmp_buf) becomes negative and converts to a large
unsigned size_t in later bounds checks and snprintf() calls. This allows
subsequent arguments to bypass remaining-space checks, write past the
end of bin_args into adjacent per-CPU memory, and trigger the
WARN_ON_ONCE(size > INT_MAX) check in vsnprintf().
Check whether err >= tmp_buf_end - tmp_buf after snprintf() in both the
%pB and %pI4/%pI6 branches, and return -ENOSPC when the formatted output
does not fit. Also cast sizeof_cur_ip and sizeof_cur_arg to ptrdiff_t in
the remaining-space comparisons so they perform signed comparisons and
never wrap negative differences into large unsigned sizes.
Fixes: 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
Cc: stable@vger.kernel.org
Reported-by: VEGA <vega@nebusec.ai>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260619205934.1312876-1-memxor@gmail.com
Signed-off-by: Luxiao Xu <rakukuip@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
kernel/bpf/helpers.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6788489a2608..6ff07090468c 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -963,6 +963,10 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
(tmp_buf_end - tmp_buf),
"%pB",
(void *)(long)raw_args[num_spec]);
+ if (err >= tmp_buf_end - tmp_buf) {
+ err = -ENOSPC;
+ goto out;
+ }
tmp_buf += (err + 1);
}
@@ -983,7 +987,7 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
goto nocopy_fmt;
sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
- if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
+ if (tmp_buf_end - tmp_buf < (ptrdiff_t)sizeof_cur_ip) {
err = -ENOSPC;
goto out;
}
@@ -1002,6 +1006,10 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
ip_spec[3] = fmt[i];
err = snprintf(tmp_buf, tmp_buf_end - tmp_buf,
ip_spec, &cur_ip);
+ if (err >= tmp_buf_end - tmp_buf) {
+ err = -ENOSPC;
+ goto out;
+ }
tmp_buf += err + 1;
num_spec++;
@@ -1076,7 +1084,7 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
nocopy_fmt:
if (tmp_buf) {
tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
- if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
+ if (tmp_buf_end - tmp_buf < (ptrdiff_t)sizeof_cur_arg) {
err = -ENOSPC;
goto out;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH bpf 2/2] selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow
2026-11-19 2:42 [PATCH bpf 0/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare() Ren Wei
2026-11-19 2:42 ` [PATCH bpf 1/2] " Ren Wei
@ 2026-11-19 2:42 ` Ren Wei
1 sibling, 0 replies; 3+ messages in thread
From: Ren Wei @ 2026-11-19 2:42 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, revest, dingyihan,
bigeasy, paul.chaignon, vega, sashiko-bot, rakukuip, weir
From: Luxiao Xu <rakukuip@gmail.com>
Add test cases to test_snprintf verifying that bpf_snprintf() returns
-ENOSPC instead of overflowing the per-CPU bin_args scratch buffer when
%pi4, %pI6, or %pB formatted output exceeds the remaining space.
Also cover the boundary where bin_args is completely full before %pB and
cases where a truncated %pI6 is followed by %lld.
Signed-off-by: Luxiao Xu <rakukuip@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
.../selftests/bpf/prog_tests/snprintf.c | 6 ++++++
.../selftests/bpf/progs/test_snprintf.c | 21 +++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c
index ccfb02bd4e7c..dfcb68b36304 100644
--- a/tools/testing/selftests/bpf/prog_tests/snprintf.c
+++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
@@ -79,6 +79,12 @@ static void test_snprintf_positive(void)
ASSERT_EQ(skel->bss->nobuf_ret, EXP_NO_BUF_RET, "no_buf_ret");
+ ASSERT_EQ(skel->bss->ip4_ovf_ret, -ENOSPC, "ip4_ovf_ret");
+ ASSERT_EQ(skel->bss->ip6_ovf_ret, -ENOSPC, "ip6_ovf_ret");
+ ASSERT_EQ(skel->bss->sym_ovf_ret, -ENOSPC, "sym_ovf_ret");
+ ASSERT_EQ(skel->bss->sym_full_ret, -ENOSPC, "sym_full_ret");
+ ASSERT_EQ(skel->bss->ip6_tail_ovf_ret, -ENOSPC, "ip6_tail_ovf_ret");
+
cleanup:
test_snprintf__destroy(skel);
}
diff --git a/tools/testing/selftests/bpf/progs/test_snprintf.c b/tools/testing/selftests/bpf/progs/test_snprintf.c
index 48a0025cf2ca..3ef50868dced 100644
--- a/tools/testing/selftests/bpf/progs/test_snprintf.c
+++ b/tools/testing/selftests/bpf/progs/test_snprintf.c
@@ -32,6 +32,14 @@ long noarg_ret = 0;
long nobuf_ret = 0;
+char fill_511[512] = {};
+char ovf_out[64] = {};
+long ip4_ovf_ret = 0;
+long ip6_ovf_ret = 0;
+long sym_ovf_ret = 0;
+long sym_full_ret = 0;
+long ip6_tail_ovf_ret = 0;
+
extern const void schedule __ksym;
SEC("raw_tp/sys_enter")
@@ -70,6 +78,19 @@ int handler(const void *ctx)
noarg_ret = BPF_SNPRINTF(noarg_out, sizeof(noarg_out), "simple case");
/* No buffer */
nobuf_ret = BPF_SNPRINTF(NULL, 0, "only interested in length %d", 60);
+ /* Scratch buffer overflow checks */
+ __builtin_memset(fill_511, 'A', sizeof(fill_511) - 1);
+ ip4_ovf_ret = BPF_SNPRINTF(ovf_out, sizeof(ovf_out), "%s%pi4%pi4",
+ fill_511 + 7, &ex_ipv4, &ex_ipv4);
+ ip6_ovf_ret = BPF_SNPRINTF(ovf_out, sizeof(ovf_out), "%s%pI6%pI6",
+ fill_511 + 21, &ex_ipv6, &ex_ipv6);
+ sym_ovf_ret = BPF_SNPRINTF(ovf_out, sizeof(ovf_out), "%s%pB%pB",
+ fill_511 + 7, &schedule, &schedule);
+ sym_full_ret = BPF_SNPRINTF(ovf_out, sizeof(ovf_out), "%s%pB",
+ fill_511, &schedule);
+ ip6_tail_ovf_ret = BPF_SNPRINTF(ovf_out, sizeof(ovf_out), "%s%pI6%lld",
+ fill_511 + 21, &ex_ipv6,
+ 0x123456789abcdef0ULL);
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 2:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 2:42 [PATCH bpf 0/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare() Ren Wei
2026-11-19 2:42 ` [PATCH bpf 1/2] " Ren Wei
2026-11-19 2:42 ` [PATCH bpf 2/2] selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow Ren Wei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox