exit: hold a reference to thread_pid across proc_flush_pid
PATCH 1/1 exit: hold a reference to thread_pid across proc_flush_pid
release_task() keeps a task's thread_pid across the point where it drops tasklist_lock and calls proc_flush_pid(). The commit named in Fixes removed the PID reference. It assumed that the PID cannot go away before this release_task() invocation calls free_pids().
That assumption does not cover references represented only by PIDTYPE links. Process B can still use exiting process A's PID as its session and process group ID. A is reaped with wait4(-1); PID-specific waits take their own PID reference and mask the bug. After A's reaper drops tasklist_lock, B can call setsid(), remove the final PIDTYPE links, and queue the PID from B's own free_pids() call. The RCU callback can then free the object before the reaper dereferences pid->inodes and pid->lock in proc_flush_pid().
A timing-only diagnostic forced this legal ordering on final v7.2. It only gated the real setsid(), RCU callback, and proc_flush_pid() operations; it did not change PID links or reference counts. Three of three fresh KASAN boots reported:
BUG: KASAN: slab-use-after-free in
proc_invalidate_siblings_dcache+0x3e2/0x3f0
Read of size 8 by task h7_pid_reaper/1921
CPU: 0 UID: 65534 PID: 1921 Comm: h7_pid_reaper
Call Trace:
proc_invalidate_siblings_dcache
release_task
wait_consider_task
__do_wait
do_wait
kernel_wait4
Freed by task 0:
kmem_cache_free
put_pid
delayed_put_pid
rcu_core
Last potentially related work creation:
__call_rcu_common
free_pids
ksys_setsidKASAN identified a 144-byte object from the pid cache and located the bad read 80 bytes into the freed object, matching pid->inodes. Restoring the balanced PID reference completed without a KASAN report on three of three fresh boots: the concurrent RCU callback reduced the count from two to one, proc_flush_pid() completed, and the balancing put_pid() performed the final free.
Public syzbot reports have independently observed proc_flush_pid() reading a freed pid-cache object, including a last reference released by a proc inode callback. Pin thread_pid explicitly so every last-reference path is excluded until proc_flush_pid() completes.
A tested source reproducer is available privately on request. No controlled read or write, information leak, or privilege escalation is claimed. The mainline patch applies directly to v6.19.y and newer; v6.16.y through v6.18.y need a context-adjusted backport.
--- kernel/exit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/exit.c +3 -3
@@ -261,8 +261,8 @@ void release_task(struct task_struct *p)pidfs_exit (p);cgroup_task_release (p); -/* Retrieve @thread_pid before __unhash_process() may set it to NULL. */ - thread_pid =task_pid (p); +/* Pin @thread_pid before __unhash_process() may set it to NULL. */ + thread_pid =get_pid (task_pid (p));write_lock_irq (&tasklist_lock);ptrace_release_task (p);
@@ -291,8 +291,8 @@ void release_task(struct task_struct *p) }write_unlock_irq (&tasklist_lock); -/* @thread_pid can't go away until free_pids() below */ proc_flush_pid (thread_pid); +put_pid (thread_pid);exit_cred_namespaces (p);add_device_randomness (&p->se.sum_exec_runtime,sizeof (p->se.sum_exec_runtime));
base-commit: cf72cbb39da84b6f02f90c07f33b102fc10b16f0
Signature · 1 line
2.54.0
Quoted 6 lines · Daehyeon Ko
release_task()keeps a task'sthread_pidacross the point where it dropstasklist_lockand callsproc_flush_pid(). The commit named in Fixes removed the PID reference. It assumed that the PID cannot go away before thisrelease_task()invocation callsfree_pids().
Quoted 8 lines
That assumption does not cover references represented only by PIDTYPE links. Process B can still use exiting process A's PID as its session and process group ID. A is reaped with wait4(-1); PID-specific waits take their own PID reference and mask the bug. After A's reaper drops
tasklist_lock, B can callsetsid(), remove the final PIDTYPE links, and queue the PID from B's ownfree_pids()call. The RCU callback can then free the object before the reaper dereferencespid->inodesandpid->lockinproc_flush_pid().
Thanks Daehyeon. Yes 0a36bad01731 was wrong and should be reverted.
But can you improve the changelog and send V2? I simply can't parse the explanation above.
But I guess the problem is clear. 0a36bad01731 assumed that the caller of release_task()path has a reference to thread_pid, and free_pids() will do put_pid(thread_pid).
However, __unhash_process()->detach_pid(pids)->__change_pid(pids) does not necessarily add pid to pids[], pid_has_task() can still be true.
IOW, release_task() doesn't necessarily has a reference, call_rcu(&pid->rcu, delayed_put_pid) is called by the caller of the "last" detach_pid() which makes pid->tasks[] lists empty.
Hmm... Yes, my explanation doesn't look clear too :/ Can you ask you LLM to turn it into something meaningful?
kernel/exit.c void release_task(struct task_struct *p) · line 265
Quoted 10 lines
kernel/exit.c +2
-2@@ -261,8 +261,8 @@ void release_task(struct task_struct *p)pidfs_exit (p);cgroup_task_release (p); -/* Retrieve @thread_pid before __unhash_process() may set it to NULL. */ - thread_pid =task_pid (p); +/* Pin @thread_pid before __unhash_process() may set it to NULL. */ + thread_pid =get_pid (task_pid (p));
It would be nice to improve the comment a bit if possible... "may set it to NULL." doesn't explain get_pid().
Thank you,
Oleg.