KCSAN: data-race in kthread_is_per_cpu / ns_capable

Status: moderation: reported on 2026/11/18 00:03
Subsystems: kernel
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+2ceb8594ed2ef03e6ad8@syzkaller.appspotmail.com
First crash: 13h20m, last: 13h20m
✨ AI cause bisection: introduced by (AI job) :
commit 28aaa9c39945b7925a1cc1d513c8f21ed38f5e4f
Author: Christian Brauner <brauner@kernel.org>
Date: Thu Feb 26 09:43:55 2026 +0000

  kthread: consolidate kthread exit paths to prevent use-after-free

  
✨ AI-generated root cause summary:

KCSAN detected a data race on the 32-bit 'flags' field of struct task_struct between CPU 0 and CPU 1. Task 3297 on CPU 0 performs an unmarked read-modify-write (current->flags |= PF_SUPERPRIV) in ns_capable_common() without holding rq->lock. Concurrently, CPU 1 executes newidle load balancing (sched_balance_rq() -> detach_tasks() -> can_migrate_task()), where kthread_is_per_cpu() calls tsk_is_kthread() to perform an unmarked read of p->flags (checking PF_KTHREAD) on CPU 0's currently running task before can_migrate_task() checks whether the task is actively running. Neither access uses READ_ONCE(), WRITE_ONCE(), or data_race() annotations.

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

Root Cause Analysis: KCSAN Data Race in kthread_is_per_cpu / ns_capable

1. Raced Memory Location and Struct Fields

The data race reported by KCSAN occurs at memory address 0xffff8881045f802c, which corresponds to struct task_struct::flags (offset 0x2c, a 4-byte / 32-bit unsigned int field defined in include/linux/sched.h) of task 3297 (syz-executor), running on CPU 0.

2. Call Paths and Concurrency Breakdown

Write Side (CPU 0 / Task 3297):

Task 3297 is actively running on CPU 0 executing a system call (getsockopt -> do_ipt_get_ctl -> ns_capable -> ns_capable_common()):

/* kernel/capability.c:341-346 */
capable = security_capable(current_cred(), ns, cap, opts);
if (capable == 0) {
    current->flags |= PF_SUPERPRIV;
    return true;
}

At line 343, ns_capable_common() performs a plain read-modify-write operation setting PF_SUPERPRIV (0x00000100, bit 8) on current->flags. As a task modifying its own process flags, it does not acquire the runqueue lock (rq->lock).

Read Side (CPU 1 / Task 4076):

Concurrently, task 4076 is on CPU 1 entering the scheduler idle/balance path via sched_balance_newidle() -> sched_balance_rq().

  1. CPU 1 locks CPU 0's runqueue (rq_lock_irqsave(busiest, &rf)).
  2. It invokes detach_tasks(&env), which walks CPU 0's cfs_tasks list (struct list_head *tasks = &env->src_rq->cfs_tasks;). The list still includes task 3297 (CPU 0's currently running CFS task).
  3. detach_tasks() calls can_migrate_task(p, env) (kernel/sched/fair.c:11172).
  4. At line 11172, can_migrate_task() evaluates:
    /* Disregard percpu kthreads; they are where they need to be. */
    if (kthread_is_per_cpu(p))
        return 0;
    
    Note: This check runs before line 11213 where task_on_cpu(env->src_rq, p) is tested to skip actively executing tasks.
  5. kthread_is_per_cpu(p) (kernel/kthread.c:655) invokes tsk_is_kthread(p) (include/linux/kthread.h:21-26):
    static inline struct kthread *tsk_is_kthread(struct task_struct *p)
    {
        if (p->flags & PF_KTHREAD)
            return p->worker_private;
        return NULL;
    }
    
    At line 23, tsk_is_kthread() performs a plain, unmarked read of p->flags to test PF_KTHREAD (0x00200000, bit 21).

3. Root Cause Mechanism

Because can_migrate_task() checks kthread_is_per_cpu(p) before testing whether p is currently running on the source CPU (task_on_cpu(env->src_rq, p)), and because current->flags updates do not acquire rq->lock, CPU 1 performs an unsynchronized plain read of p->flags while CPU 0 performs an unsynchronized plain write (|= PF_SUPERPRIV) to the same 32-bit word. Neither side uses concurrency-marking primitives (such as READ_ONCE() / WRITE_ONCE() or data_race()), triggering KCSAN.

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
b549be09 bisect-cause ❓ KCSAN: data-race in kthread_is_per_cpu / ns_capable 2026/11/17 23:15 2026/11/17 23:15 2026/11/17 23:39 ca622473
503f2ff4 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in kthread_is_per_cpu / ns_capable 2026/11/17 23:13 2026/11/17 23:13 2026/11/17 23:33 ca622473
9366d7bb assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in kthread_is_per_cpu / ns_capable 2026/11/17 23:11 2026/11/17 23:11 2026/11/17 23:38 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in kthread_is_per_cpu / ns_capable

read to 0xffff8881045f802c of 4 bytes by task 4076 on cpu 1:
 tsk_is_kthread include/linux/kthread.h:23 [inline]
 kthread_is_per_cpu+0x17/0xa0 kernel/kthread.c:655
 can_migrate_task+0x45/0x240 kernel/sched/fair.c:11172
 detach_tasks kernel/sched/fair.c:11364 [inline]
 sched_balance_rq+0x18d1/0x2470 kernel/sched/fair.c:13832
 sched_balance_newidle kernel/sched/fair.c:14960 [inline]
 pick_task_fair+0x2d7/0x4e0 kernel/sched/fair.c:10308
 __pick_next_task kernel/sched/core.c:6224 [inline]
 pick_next_task kernel/sched/core.c:6773 [inline]
 __schedule+0x36e/0xd30 kernel/sched/core.c:7298
 __schedule_loop kernel/sched/core.c:7463 [inline]
 schedule+0x5b/0xa0 kernel/sched/core.c:7478
 futex_do_wait kernel/futex/waitwake.c:387 [inline]
 __futex_wait+0x124/0x2c0 kernel/futex/waitwake.c:722
 futex_wait+0x98/0x1c0 kernel/futex/waitwake.c:750
 do_futex+0x391/0x460 kernel/futex/syscalls.c:133
 __do_sys_futex kernel/futex/syscalls.c:210 [inline]
 __se_sys_futex+0x2f6/0x370 kernel/futex/syscalls.c:191
 __x64_sys_futex+0x78/0x90 kernel/futex/syscalls.c:191
 x64_sys_call+0x22e2/0x2550 arch/x86/include/generated/asm/syscalls_64.h:203
 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-write to 0xffff8881045f802c of 4 bytes by task 3297 on cpu 0:
 ns_capable_common kernel/capability.c:343 [inline]
 ns_capable+0x80/0xb0 kernel/capability.c:362
 do_ipt_get_ctl+0x76/0xcb0 net/ipv4/netfilter/ip_tables.c:1654
 nf_getsockopt+0x190/0x1b0 net/netfilter/nf_sockopt.c:116
 ip_getsockopt+0x137/0x180 net/ipv4/ip_sockglue.c:1785
 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: 0 UID: 0 PID: 3297 Comm: syz-executor Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 11:31 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in kthread_is_per_cpu / ns_capable
* Struck through repros no longer work on HEAD.