fork/pid: Fix use-after-free in __task_pid_nr_ns
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 21 lines · Qing Wang
Syzbot reported a slab-use-after-free issue in
__task_pid_nr_ns:BUG: KASAN: slab-use-after-free in __task_pid_nr_ns+0x1e4/0x490... Read of size 8 at addr ffff88807f8058a8 by task syz.1.574/8108The race condition occurs between the failure path of
copy_process()and getting thePIDTYPE_TGIDvia__task_pid_nr_ns().Bug timeline:
Task Bperf_event_open()
Task A <---------------------------clone()copy_process()perf_event_init_task() ... one copy failed free_signal_struct() close(event_fd) perf_child_detach() __task_pid_nr_ns() access child task->signal
Quoted 7 lines
This is fixed by:
1. Settingtask->signal= NULL in the failure cleanup path ofcopy_process.
2. Adding a null check fortask->signalbefore accessingPIDTYPE_TGIDfromtask->signal.Note: This bug was reported by syzbot without a reproducer. The fix is based on code inspection and race condition analysis.
It seems like there is synchronization missing between the task->signal assignment and its check in task_pid_ptr? Aren't there other ways of checking if a task is dead? This change doesn't look right to me...
-Kees
Quoted 55 lines
Reported-by [email protected]Signed-off-by Qing Wang <[email protected]>--- kernel/fork.c | 8 ++++++-- kernel/pid.c | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-)kernel/fork.c +6
-2@@ -1975,6 +1975,7 @@ __latent_entropy struct task_struct *copy_process(struct file *pidfile =NULL ;const u64 clone_flags = args->flags;struct nsproxy *nsp = current->nsproxy; +struct signal_struct *free_sig =NULL ;/* * Don't allow sharing the root directory with processes in a different @@ -2501,8 +2502,11 @@ __latent_entropy struct task_struct *copy_process(mmput (p->mm); } bad_fork_cleanup_signal: -if (!(clone_flags &CLONE_THREAD )) -free_signal_struct (p->signal); +if (!(clone_flags &CLONE_THREAD )) { + free_sig = p->signal; + p->signal =NULL ; +free_signal_struct (free_sig); + } bad_fork_cleanup_sighand:__cleanup_sighand (p->sighand); bad_fork_cleanup_fs:kernel/pid.c +3
-3@@ -329,9 +329,9 @@ EXPORT_SYMBOL_GPL(find_vpid);static struct pid **task_pid_ptr (struct task_struct *task,enum pid_type type) { -return (type ==PIDTYPE_PID ) ? - &task->thread_pid : - &task->signal->pids[type]; +if (type ==PIDTYPE_PID ) +return &task->thread_pid; +return task->signal ? &task->signal->pids[type] :NULL ; }/* Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
Quoted 5 lines · Kees Cook
It seems like there is synchronization missing between the
task->signalassignment and its check intask_pid_ptr? Aren't there other ways of checking if a task is dead? This change doesn't look right to me...-Kees
Thanks for your reply. Oleg and I discussed this and concluded that this issue no longer exists.
Discussion: https://lore.kernel.org/all/[email protected]/#R
Qing.
Quoted 5 lines · Kees Cook
It seems like there is synchronization missing between the
task->signalassignment and its check intask_pid_ptr? Aren't there other ways of checking if a task is dead? This change doesn't look right to me...-Kees
Thanks for your reply. Oleg and I discussed this and concluded that this issue no longer exists.
Discussion: https://lore.kernel.org/all/[email protected]/#R
Qing.