Root Cause Analysis
1. Functions, Structs, and Fields Involved
2. Background and Invariant Violation
Historically, task_struct::cred was annotated with __rcu and updated using rcu_assign_pointer(). In commit b030ffaedc1d ("cred: clarify that task_struct::cred is only for the current task"), __rcu annotations and RCU barrier helpers were removed from task_struct::cred based on the design rule that subjective credentials are only accessed synchronously by current (and during initial child setup in fork or final process teardown). Consequently, commit_creds() was changed to perform a plain, unmarked C store:
/* kernel/cred.c:416-417 */
rcu_assign_pointer(task->real_cred, new);
task->cred = new;
However, kernel/cred.c retained the following assertion inside __put_cred():
/* kernel/cred.c:76-78 */
BUG_ON(atomic_long_read(&cred->usage) != 0);
BUG_ON(cred == current->cred);
BUG_ON(cred == current->real_cred);
While task_struct::cred is private to current, __put_cred() is not strictly executed by current for its own credentials. Credential structures are reference-counted and can reach a refcount of zero asynchronously in any context, including interrupt handlers, workqueues, and RCU callbacks.
3. How the Data Race is Triggered
- Task Context (CPU 0): Task 7254 (
dhcpcd) executes the setgroups system call and enters commit_creds() to install newly prepared credentials. It executes task->cred = new; (kernel/cred.c:417). Because interrupts are enabled during this system call, this store can be preempted by an interrupt.
- Interrupt / Softirq Context (CPU 0): An APIC timer interrupt occurs on CPU 0. During interrupt exit processing (
__irq_exit_rcu()), pending softirqs are processed (RCU_SOFTIRQ via rcu_core()).
- RCU Callback Execution: The RCU engine invokes
__put_task_struct_rcu_cb() to reclaim the task_struct of a previously terminated, unrelated process (tsk). This path invokes:
__put_task_struct(tsk) -> exit_creds(tsk) -> put_cred_many(cred, 2) -> __put_cred(cred)
- Preemption and Unsynchronized Read: In softirq context,
current points to whatever task was preempted on that CPUβin this case, task 7254. When __put_cred() executes line 77 (BUG_ON(cred == current->cred);), it evaluates current->cred via a plain, unsynchronized read of task 7254's task->cred.
- KCSAN Detection: KCSAN instrumented the store in
commit_creds(), established a watchpoint, and delayed CPU 0 within udelay(). When the APIC timer interrupt arrived and executed softirq RCU callbacks on the same CPU, __put_cred() read current->cred within that watchpoint window, prompting KCSAN to report a data race between __put_cred and commit_creds.
4. Remediation
Because current->cred is only meaningful to check when releasing credentials from synchronous task context, the assertion in __put_cred() should be bypassed when running in interrupt or out-of-task contexts (e.g., guarded with if (in_task()) { BUG_ON(cred == current->cred); ... }), or reads/writes of task->cred should be appropriately annotated with READ_ONCE() / WRITE_ONCE() or data-race suppression primitives.