Re: [PATCH] fork/pid: Fix use-after-free in __task_pid_nr_ns

From: Andrew Morton
Date: Mon Jan 05 2026 - 17:46:49 EST


On Mon, 5 Jan 2026 12:36:27 +0800 Qing Wang <wangqing7171@xxxxxxxxx> wrote:

> 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/8108
>
> The race condition occurs between the failure path of copy_process() and
> getting the PIDTYPE_TGID via __task_pid_nr_ns().
>
> Bug timeline:
> Task B
> perf_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
>
> This is fixed by:
> 1. Setting task->signal = NULL in the failure cleanup path of copy_process.
> 2. Adding a null check for task->signal before accessing PIDTYPE_TGID from
> task->signal.
>
> Note: This bug was reported by syzbot without a reproducer.
> The fix is based on code inspection and race condition analysis.

Thanks.

>
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -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;
> }

It might be helpful to have a comment here telling readers how
task->signal can be zero.

Also, what in here prevents task->signal from being zeroed after we've
tested it and before we dereference it?