BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Ren Wei" <weir@nebusec.ai>, <bpf@vger.kernel.org>
Cc: <daniel@iogearbox.net>, <andrii@kernel.org>, <eddyz87@gmail.com>,
	<memxor@gmail.com>, <martin.lau@linux.dev>, <song@kernel.org>,
	<yonghong.song@linux.dev>, <jolsa@kernel.org>,
	<emil@etsalapatis.com>, <ihor.solodrai@linux.dev>,
	<revest@chromium.org>, <dingyihan@uniontech.com>,
	<bigeasy@linutronix.de>, <paul.chaignon@gmail.com>,
	<vega@nebusec.ai>, <sashiko-bot@kernel.org>, <rakukuip@gmail.com>
Subject: Re: [PATCH bpf 1/2] bpf: Fix scratch buffer cursor overflow in bpf_bprintf_prepare()
Date: Tue, 24 Nov 2026 03:47:22 +0000	[thread overview]
Message-ID: <DN34T5GCESWY.12OL1L7C45XQV@gmail.com> (raw)
In-Reply-To: <4b2d82631c485580b1f31da9bf9c7cccdf2626e4.1794974103.git.rakukuip@gmail.com>

On Wed, Nov 18, 2026 at 06:42 PM Ren Wei <weir@nebusec.ai> wrote:
> 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().

The write lands in buf[] of the same struct. The caller owns it.
"%s%pI6%lld" with 490 chars in %s:

     0  +----------------+
        | bin_args[512]  |    0  %s, 491 bytes
        |                |  491  %pI6, tmp_buf += 39 + 1
   512  +----------------+
        | buf[1024]      |  532  %lld, 8 bytes
  1536  +----------------+

tmp_buf makes one big jump, KSYM_SYMBOL_LEN at most, 666 bytes on 64-bit.
After it snprintf() gets size > INT_MAX and returns 0.
Each other arg adds 8 bytes at most, plus padding.
Only kernels without
commit e2bb9e01d589 ("bpf: Remove trace_printk_lock")
write into the next buffer. Pls fix the commit log.

> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260619205934.1312876-1-memxor@gmail.com

That's Kumar's series. The report is:
https://lore.kernel.org/bpf/20260619211930.5AFC51F000E9@smtp.kernel.org/

Ibrahim reported the %pI4/%pI6 half in March. Pls credit him too:
https://lore.kernel.org/bpf/20260319124901.9207-1-zeroxjacks@gmail.com/

>  			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) {

Remove this check instead.
The raw address goes to cur_ip[] on the stack, not to tmp_buf.
The shortest strings need 8 bytes for IPv4 and 33 bytes for IPv6.
So the new check after snprintf() returns the same -ENOSPC.

> -			if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
> +			if (tmp_buf_end - tmp_buf < (ptrdiff_t)sizeof_cur_arg) {

No need for the cast. Make the variable int.

Both changes on top of the series, helpers.c compiles, I didn't run it:

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6ff0709..7fe702e 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -859,7 +859,7 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 	bool get_buffers = (data->get_bin_args && num_args) || data->get_buf;
 	char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end;
 	struct bpf_bprintf_buffers *buffers = NULL;
-	size_t sizeof_cur_arg, sizeof_cur_ip;
+	int sizeof_cur_arg, sizeof_cur_ip;
 	int err, i, num_spec = 0;
 	u64 cur_arg;
 	char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX";
@@ -987,11 +987,6 @@ 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 < (ptrdiff_t)sizeof_cur_ip) {
-				err = -ENOSPC;
-				goto out;
-			}
-
 			unsafe_ptr = (char *)(long)raw_args[num_spec];
 			err = copy_from_kernel_nofault(cur_ip, unsafe_ptr,
 						       sizeof_cur_ip);
@@ -1084,7 +1079,7 @@ fmt_str:
 nocopy_fmt:
 		if (tmp_buf) {
 			tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
-			if (tmp_buf_end - tmp_buf < (ptrdiff_t)sizeof_cur_arg) {
+			if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
 				err = -ENOSPC;
 				goto out;
 			}

pw-bot: cr

  reply	other threads:[~2026-11-24  3:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-24  3:47   ` Alexei Starovoitov [this message]
2026-11-19  2:42 ` [PATCH bpf 2/2] selftests/bpf: Test bpf_bprintf_prepare() scratch buffer overflow Ren Wei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DN34T5GCESWY.12OL1L7C45XQV@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dingyihan@uniontech.com \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=paul.chaignon@gmail.com \
    --cc=rakukuip@gmail.com \
    --cc=revest@chromium.org \
    --cc=sashiko-bot@kernel.org \
    --cc=song@kernel.org \
    --cc=vega@nebusec.ai \
    --cc=weir@nebusec.ai \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox