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 2/2] selftests/bpf: Test raw_tp test_run ctx padding for tail calls
Date: Sat,  5 Dec 2026 14:39:34 +0530	[thread overview]
Message-ID: <20261205090934.3813585-2-r41k0u@ubuntu.com> (raw)
In-Reply-To: <20261205090934.3813585-1-r41k0u@ubuntu.com>

Add tailcall_raw_tp_ctx: a raw_tp caller that only tail-calls
(max_ctx_offset 0) and a target that reads 96 bytes of ctx and
returns 42 only if everything past the 8 bytes supplied by test_run
reads as zero. Without the ctx padding in bpf_prog_test_run_raw_tp()
the target reads out of bounds and almost surely returns 1 instead.

Signed-off-by: Pragyansh Chaturvedi <r41k0u@ubuntu.com>
---
 .../selftests/bpf/prog_tests/tailcalls.c      | 38 +++++++++++++++++++
 .../selftests/bpf/progs/tailcall_raw_tp_ctx.c | 37 ++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/tailcall_raw_tp_ctx.c

diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
index 88d223110ec..be3bb858494 100644
--- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c
+++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c
@@ -17,6 +17,42 @@
 #include "tailcall_callback.skel.h"
 #include "tailcall_bpf2bpf2.skel.h"
 #include "tailcall_bpf2bpf_fexit.skel.h"
+#include "tailcall_raw_tp_ctx.skel.h"
+
+/* test_tailcall_raw_tp_ctx checks that a raw_tp program run through
+ * BPF_PROG_TEST_RUN gets a zero-padded ctx when it can be reached by a
+ * tail call.  The caller supplies 8 bytes of ctx and tail-calls a
+ * target that reads 96 bytes; without the padding the target reads
+ * out of bounds and (almost surely) non-zero memory, failing the
+ * retval check.
+ */
+static void test_tailcall_raw_tp_ctx(void)
+{
+	struct tailcall_raw_tp_ctx *skel;
+	__u64 ctx[12] = {};
+	int err, key = 0, prog_fd;
+
+	skel = tailcall_raw_tp_ctx__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+
+	prog_fd = bpf_program__fd(skel->progs.target);
+	err = bpf_map_update_elem(bpf_map__fd(skel->maps.jmp_table), &key,
+				  &prog_fd, BPF_ANY);
+	if (!ASSERT_OK(err, "jmp_table"))
+		goto out;
+
+	/* raw_tp test_run does not accept repeat */
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.ctx_in = ctx,
+		.ctx_size_in = 8,
+	);
+	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.caller), &topts);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(topts.retval, 42, "ctx padded with zeros");
+out:
+	tailcall_raw_tp_ctx__destroy(skel);
+}
 
 /* test_tailcall_1 checks basic functionality by patching multiple locations
  * in a single program for a single tail call slot with nop->jmp, jmp->nop
@@ -2138,4 +2174,6 @@ void test_tailcalls(void)
 		test_tailcall_bpf2bpf_fexit_links();
 	if (test__start_subtest("tailcall_large_stack"))
 		test_tailcall_large_stack();
+	if (test__start_subtest("tailcall_raw_tp_ctx"))
+		test_tailcall_raw_tp_ctx();
 }
diff --git a/tools/testing/selftests/bpf/progs/tailcall_raw_tp_ctx.c b/tools/testing/selftests/bpf/progs/tailcall_raw_tp_ctx.c
new file mode 100644
index 00000000000..e8380d19892
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tailcall_raw_tp_ctx.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} jmp_table SEC(".maps");
+
+SEC("raw_tracepoint/sched_switch")
+int caller(void *ctx)
+{
+	bpf_tail_call(ctx, &jmp_table, 0);
+	return 0;
+}
+
+SEC("raw_tracepoint/sched_switch")
+int target(void *ctx)
+{
+	__u64 *a = ctx;
+
+	/* The test runs the caller with an 8-byte ctx.  Everything past
+	 * the first u64 must read as zero: without the test_run ctx
+	 * padding, these reads are out of bounds and leak whatever
+	 * memory follows the small copy.
+	 */
+#pragma clang loop unroll(full)
+	for (int i = 1; i < 12; i++) {
+		if (a[i])
+			return 1;
+	}
+	return 42;
+}
+
+char LICENSE[] SEC("license") = "GPL";
-- 
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 ` [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls Pragyansh Chaturvedi
2026-12-05  9:09   ` Pragyansh Chaturvedi [this message]
2026-12-05  9:35     ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " 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-2-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