Netdev List
 help / color / mirror / Atom feed
From: Pragyansh Chaturvedi <r41k0u@ubuntu.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	eddyz87@gmail.com, varunrmallya@gmail.com, kc@kcsrk.info,
	yonghong.song@linux.dev, memxor@gmail.com, ngs@cse.iitm.ac.in,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Pragyansh Chaturvedi <r41k0u@ubuntu.com>
Subject: [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls
Date: Sat,  5 Dec 2026 14:39:33 +0530	[thread overview]
Message-ID: <20261205090934.3813585-1-r41k0u@ubuntu.com> (raw)
In-Reply-To: <axPUDKyHAFXUXdp_@x1c>

BPF_PROG_RUN for RAW_TRACEPOINT compares ctx_size_in with the
max_ctx_offset of the program that it runs. The target of a tail call
gets the same context:

  first:   bpf_tail_call(ctx, ...)      max_ctx_offset 0, ctx_size_in 8
  target:  r0 = *(u64 *)(ctx + 96)      out of bounds

The target is not known before the run. Pad the copy of the context
with zeros up to U16_MAX, no program can use more.

This is the raw_tp instance of commit 08c987904970 ("bpf: Give a
syscall program the largest context when it does tail calls"), which
applied the same fix to the syscall test_run path only. With KASAN
and BPF_JIT_KASAN the raw_tp path reports:

  BUG: KASAN: slab-out-of-bounds in bpf_prog_..._target+0x2a/0x259
  Read of size 8 at addr ffff88810486a818 by task rawtp_chain_leak/285
   bpf_prog_..._target+0x2a/0x259
   __bpf_prog_test_run_raw_tp+0x5b8/0x900
   bpf_prog_test_run_raw_tp+0x42f/0x7e0
  The buggy address belongs to the cache memdup_user-8 of size 8

The copy is accounted now. It comes from kmalloc-cg or vmalloc, not
from the caches of memdup_user().

Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")
Signed-off-by: Pragyansh Chaturvedi <r41k0u@ubuntu.com>
---
 net/bpf/test_run.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index 8b8c4e9ded9..31c4c656d39 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -782,6 +782,7 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
 	__u32 ctx_size_in = kattr->test.ctx_size_in;
 	struct bpf_raw_tp_test_run_info info;
 	int cpu = kattr->test.cpu, err = 0;
+	u32 size = ctx_size_in;
 	int current_cpu;
 
 	/* doesn't support data_in/out, ctx_out, duration, or repeat */
@@ -804,10 +805,18 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
 	if (prog->sleepable && (kattr->test.flags & BPF_F_TEST_RUN_ON_CPU))
 		return -EINVAL;
 
-	if (ctx_size_in) {
-		info.ctx = memdup_user(ctx_in, ctx_size_in);
-		if (IS_ERR(info.ctx))
-			return PTR_ERR(info.ctx);
+	/* the target of a tail call can use that much */
+	if (prog->aux->tail_call_reachable)
+		size = max_t(u32, size, U16_MAX);
+
+	if (size) {
+		info.ctx = kvzalloc(size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+		if (!info.ctx)
+			return -ENOMEM;
+		if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {
+			err = -EFAULT;
+			goto out;
+		}
 	} else {
 		info.ctx = NULL;
 	}
@@ -842,7 +851,8 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
 	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
 		err = -EFAULT;
 
-	kfree(info.ctx);
+out:
+	kvfree(info.ctx);
 	return err;
 }
 
-- 
2.53.0


  reply	other threads:[~2026-12-05  9:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  9:06 bpf: tail-call targets bypass max_ctx_offset checks (OOB read, KASAN splats) Pragyansh Chaturvedi
2026-12-05  9:09 ` Pragyansh Chaturvedi [this message]
2026-12-05  9:09   ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for tail calls Pragyansh Chaturvedi
2026-12-05  9:35     ` bot+bpf-ci
2026-12-05  9:35   ` [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does " bot+bpf-ci

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=20261205090934.3813585-1-r41k0u@ubuntu.com \
    --to=r41k0u@ubuntu.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kc@kcsrk.info \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=ngs@cse.iitm.ac.in \
    --cc=varunrmallya@gmail.com \
    --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