KCSAN: data-race in sockopt_lock_sock / trace_call_bpf

Status: moderation: reported on 2026/11/21 00:00
Subsystems: trace bpf
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+374de11025bae4341b33@syzkaller.appspotmail.com
First crash: 3d18h, last: 3d04h
✨ AI-generated root cause summary:

KCSAN (with CONFIG_KCSAN_INTERRUPT_WATCHER=y) reports a same-CPU data race on task_struct::bpf_ctx between task context reading current->bpf_ctx via has_current_bpf_ctx() in sockopt_lock_sock() and interrupt/softirq context updating current->bpf_ctx via bpf_set_run_ctx() in bpf_prog_run_array() (called from trace_call_bpf()). Both has_current_bpf_ctx() and bpf_set_run_ctx()/bpf_reset_run_ctx() in include/linux/bpf.h access current->bpf_ctx using plain loads and stores rather than READ_ONCE()/WRITE_ONCE() (or data_race()). Because bpf_prog_run_array() saves and restores current->bpf_ctx before returning from the interrupt, the interrupted task always observes its original bpf_ctx value and the race is benign in practice.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 666539eecdf4:

Root Cause Analysis

The KCSAN report is triggered by an unannotated same-CPU concurrent access to current->bpf_ctx (struct task_struct::bpf_ctx, defined in include/linux/sched.h) between task context and softirq/interrupt context:

  1. Task-context plain read (include/linux/bpf.h:3422): During a getsockopt() syscall (do_ip_getsockopt() in net/ipv4/ip_sockglue.c), sockopt_lock_sock() (net/core/sock.c:1152) calls has_current_bpf_ctx() to determine whether it is running inside a BPF program:

    static inline bool has_current_bpf_ctx(void)
    {
        return !!current->bpf_ctx;
    }
    

    This performs a plain (non-READ_ONCE) load of current->bpf_ctx.

  2. Interrupt/softirq-context plain read-write (include/linux/bpf.h:2760): With CONFIG_KCSAN_INTERRUPT_WATCHER=y, kcsan_setup_watchpoint() sets a watchpoint on &current->bpf_ctx and stalls inside delay_access() (udelay()) with interrupts enabled. During this delay on CPU 1, an APIC timer interrupt fires and runs softirqs (handle_softirqs() -> rcu_core() -> __d_free() -> kmem_cache_free()). The trace_kmem_cache_free tracepoint invokes perf_trace_kmem_cache_free() -> perf_trace_run_bpf_submit() -> trace_call_bpf() (kernel/trace/bpf_trace.c:147) -> bpf_prog_run_array() (include/linux/bpf.h:2797), which calls bpf_set_run_ctx(&run_ctx.run_ctx):

    static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
    {
        struct bpf_run_ctx *old_ctx = NULL;
    
    #ifdef CONFIG_BPF_SYSCALL
        old_ctx = current->bpf_ctx;
        current->bpf_ctx = new_ctx;
    #endif
        return old_ctx;
    }
    

    Because current in interrupt/softirq context on CPU 1 still refers to the interrupted task (PID 3302), bpf_set_run_ctx() performs a plain read and plain write to the same &current->bpf_ctx address watched by KCSAN, causing KCSAN to report a data race.

Invariant & Lifecycle Details

  • current->bpf_ctx is only ever accessed by the task currently running on that CPU (or by interrupt/softirq/NMI handlers preempting it on the same CPU).
  • bpf_prog_run_array() (and all other bpf_set_run_ctx() callers) follows a strict stack-based save/restore discipline: it saves old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx) before running BPF programs and restores bpf_reset_run_ctx(old_run_ctx) (current->bpf_ctx = old_ctx) before returning from the interrupt/softirq.
  • Consequently, the interrupted task cannot observe the temporary new_ctx pointer set during interrupt execution. The defect is limited to missing READ_ONCE() / WRITE_ONCE() (or data_race()) annotations on current->bpf_ctx in has_current_bpf_ctx(), bpf_set_run_ctx(), and bpf_reset_run_ctx() in include/linux/bpf.h to prevent compiler load/store tearing and satisfy KCSAN under CONFIG_KCSAN_INTERRUPT_WATCHER=y.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
4eb9439b assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in sockopt_lock_sock / trace_call_bpf 2026/11/19 19:41 2026/11/19 19:41 2026/11/19 19:45 229865fb
e9dd4764 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in sockopt_lock_sock / trace_call_bpf 2026/11/19 19:41 2026/11/19 19:41 2026/11/19 20:01 229865fb

Sample crash report:
==================================================================
BUG: KCSAN: data-race in sockopt_lock_sock / trace_call_bpf

read-write to 0xffff8881048be2a8 of 8 bytes by interrupt on cpu 1:
 bpf_set_run_ctx include/linux/bpf.h:2760 [inline]
 bpf_prog_run_array include/linux/bpf.h:2797 [inline]
 trace_call_bpf+0x13e/0x420 kernel/trace/bpf_trace.c:147
 perf_trace_run_bpf_submit+0x4e/0x110 kernel/events/core.c:11549
 do_perf_trace_kmem_cache_free include/trace/events/kmem.h:117 [inline]
 perf_trace_kmem_cache_free+0x131/0x170 include/trace/events/kmem.h:117
 __do_trace_kmem_cache_free include/trace/events/kmem.h:117 [inline]
 trace_kmem_cache_free include/trace/events/kmem.h:117 [inline]
 kmem_cache_free+0x350/0x3b0 mm/slub.c:6756
 __d_free+0x26/0x30 fs/dcache.c:349
 rcu_do_batch kernel/rcu/tree.c:2650 [inline]
 rcu_core+0x76e/0xcb0 kernel/rcu/tree.c:2919
 rcu_core_si+0xd/0x20 kernel/rcu/tree.c:2936
 handle_softirqs+0x109/0x310 kernel/softirq.c:669
 __do_softirq kernel/softirq.c:703 [inline]
 invoke_softirq kernel/softirq.c:542 [inline]
 __irq_exit_rcu+0x40/0xf0 kernel/softirq.c:786
 instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
 sysvec_apic_timer_interrupt+0x74/0x80 arch/x86/kernel/apic/apic.c:1062
 asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
 __preempt_count_add arch/x86/include/asm/preempt.h:98 [inline]
 delay_tsc+0x4b/0xa0 arch/x86/lib/delay.c:77
 udelay include/asm-generic/delay.h:64 [inline]
 delay_access kernel/kcsan/core.c:332 [inline]
 kcsan_setup_watchpoint+0x255/0x3f0 kernel/kcsan/core.c:622
 has_current_bpf_ctx include/linux/bpf.h:3422 [inline]
 sockopt_lock_sock+0x23/0x50 net/core/sock.c:1152
 do_ip_getsockopt+0xc66/0x1340 net/ipv4/ip_sockglue.c:1703
 ip_getsockopt+0x5c/0x180 net/ipv4/ip_sockglue.c:1773
 tcp_getsockopt+0xad/0xe0 net/ipv4/tcp.c:4885
 sock_common_getsockopt+0x60/0x70 net/core/sock.c:4096
 do_sock_getsockopt+0x2ce/0x350 net/socket.c:2505
 __sys_getsockopt net/socket.c:2536 [inline]
 __do_sys_getsockopt net/socket.c:2543 [inline]
 __se_sys_getsockopt net/socket.c:2540 [inline]
 __x64_sys_getsockopt+0x11d/0x1a0 net/socket.c:2540
 x64_sys_call+0x1032/0x2550 arch/x86/include/generated/asm/syscalls_64.h:56
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff8881048be2a8 of 8 bytes by task 3302 on cpu 1:
 has_current_bpf_ctx include/linux/bpf.h:3422 [inline]
 sockopt_lock_sock+0x23/0x50 net/core/sock.c:1152
 do_ip_getsockopt+0xc66/0x1340 net/ipv4/ip_sockglue.c:1703
 ip_getsockopt+0x5c/0x180 net/ipv4/ip_sockglue.c:1773
 tcp_getsockopt+0xad/0xe0 net/ipv4/tcp.c:4885
 sock_common_getsockopt+0x60/0x70 net/core/sock.c:4096
 do_sock_getsockopt+0x2ce/0x350 net/socket.c:2505
 __sys_getsockopt net/socket.c:2536 [inline]
 __do_sys_getsockopt net/socket.c:2543 [inline]
 __se_sys_getsockopt net/socket.c:2540 [inline]
 __x64_sys_getsockopt+0x11d/0x1a0 net/socket.c:2540
 x64_sys_call+0x1032/0x2550 arch/x86/include/generated/asm/syscalls_64.h:56
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 3302 Comm: syz-executor Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (2):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 20:55 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in sockopt_lock_sock / trace_call_bpf
2026/11/17 06:18 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in sockopt_lock_sock / trace_call_bpf
* Struck through repros no longer work on HEAD.