Root Cause Analysis
1. Raced Location and Triggering Code Path
The memory location reported in the KCSAN splat is 0xffff888101d7116c:
- In the active kernel build,
struct task_struct slab objects are 64-byte (0x40) cacheline-aligned. Task 3294's task_struct is allocated at 0xffff888101d71140.
- The
flags field of struct task_struct (include/linux/sched.h) resides at byte offset 0x2c (bit offset 352):
$$\text{Address} = \text{0xffff888101d71140} + \text{0x2c} = \text{0xffff888101d7116c}$$
- The read-write access occurs in
kernel/capability.c:343 within ns_capable_common():
capable = security_capable(current_cred(), ns, cap, opts);
if (capable == 0) {
current->flags |= PF_SUPERPRIV;
return true;
}
This executes an unmarked 4-byte read-modify-write operation setting PF_SUPERPRIV on current->flags.
2. Mechanism of the Same-Task "Self-Race" Artifact
Both accesses in the KCSAN report specify by task 3294 on cpu 0 (in_task() == true and task_pid_nr(current) == 3294). The race is a false-positive artifact of KCSAN runtime handling under CONFIG_KCSAN_INTERRUPT_WATCHER=y and PREEMPT(lazy):
-
Watchpoint Setup without Disabling Preemption or Interrupts:
When ns_capable_common() performs current->flags |= PF_SUPERPRIV, KCSAN's instrumentation invokes check_access(). Because should_watch() evaluates to true, it calls kcsan_setup_watchpoint() (kernel/kcsan/core.c:528).
- With
CONFIG_KCSAN_INTERRUPT_WATCHER=y enabled, interrupt_watcher is true, so kcsan_setup_watchpoint() does not call local_irq_save() nor does it disable preemption; it only increments ctx->disable_scoped.
- The watchpoint on
0xffff888101d7116c is inserted into watchpoints[], and the thread enters delay_access() -> udelay() (kernel/kcsan/core.c:622).
-
Interrupt and Preemption / Exit Re-entry:
During the busy-wait in udelay(), hardware interrupts remain enabled. A hardware interrupt (e.g., timer interrupt) occurs on CPU 0.
- Once hardirq/softirq processing finishes, the interrupt context is left (
in_task() becomes true again for task 3294).
- Under
PREEMPT(lazy), on the interrupt return path (irqentry_exit_cond_resched() / preempt_schedule_irq()), kernel scheduling or entry/exit routines perform an instrumented read of current->flags (for example checking flags such as PF_EXITING, PF_MEMALLOC, etc.).
- Since
ctx->disable_count == 0, KCSAN instrumentation is fully active and executes find_watchpoint(). This matches the active watchpoint previously inserted by task 3294 on its own ¤t->flags.
kcsan_found_watchpoint() successfully consumes the watchpoint via try_consume_watchpoint() and records the access information into other_infos[watchpoint_idx].
- When the interrupted context resumes in
udelay(), consume_watchpoint(watchpoint) in kcsan_setup_watchpoint() returns false, triggering kcsan_report_known_origin() for a data race where both accesses belong to task 3294 on CPU 0.
3. Why selinux_inode_permission Appears in the Bug Title with Zero Stack Frames
An inspection of selinux_inode_permission() and its inlined callees (current_sid(), selinux_task(), task_avdcache_permnoaudit(), inode_security_rcu(), task_avdcache_search(), and task_avdcache_update()) reveals that it never reads or writes current->flags.
The appearance of selinux_inode_permission in the report title is caused by an out-of-bounds/stale array read in kernel/kcsan/report.c:
other_infos (kernel/kcsan/report.c:45) is a statically allocated array indexed by watchpoint slot (watchpoint - watchpoints).
- When a previous, unrelated watchpoint report is released via
release_report() (kernel/kcsan/report.c:502), KCSAN resets other_info->ai.size = 0, but does not zero out other_info->stack_entries[].
- When
prepare_report_producer() records the new watchpoint consumer via stack_trace_save(), if stack capture unwinds 0 frames or if all captured frames are skipped as runtime infrastructure, other_info->num_stack_entries equals other_skipnr (num_stack_entries - other_skipnr == 0).
- At
kernel/kcsan/report.c:455, print_stack_trace(other_info->stack_entries + other_skipnr, other_info->num_stack_entries - other_skipnr, ...) prints 0 entries, producing the empty space in the report.
- However, at
kernel/kcsan/report.c:416:
other_frame = other_info->stack_entries[other_skipnr];
There is no bounds check ensuring other_skipnr < other_info->num_stack_entries. It reads past num_stack_entries, retrieving whatever stale return address was left in other_infos[watchpoint_idx].stack_entries[other_skipnr] by an earlier watchpoint event that traversed selinux_inode_permission.
print_report() uses other_frame to format the report header:
pr_err("BUG: KCSAN: %s in %ps / %ps\n", ...);
producing "data-race in ns_capable / selinux_inode_permission".
4. Concurrency Invariants on task_struct::flags
According to the kernel threading model documented in include/linux/sched.h:1838-1848:
- Per-task flags (
tsk->flags, PF_*) are strictly owned and written by current (the task itself).
- Asynchronous cross-task modifications are prohibited and instead handled via
atomic_flags (PFA_*).
- Because only
current modifies its own tsk->flags, task-local RMW operations such as current->flags |= PF_SUPERPRIV do not race with other threads in real-world kernel operation.