Skip to contentSkip to conversation

fork/pid: Fix use-after-free in __task_pid_nr_ns

PATCH In review Qing Wang 3 participants 14 messages Jan 5 – Jan 7

Subsystems (4)
lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 fork/pid: Fix use-after-free in __task_pid_nr_ns

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.

---
 kernel/fork.c | 8 ++++++--
 kernel/pid.c  | 6 +++---
 2 files changed, 9 insertions(+), 5 deletions(-)
kernel/fork.c +6 -2
index b1f3915d5f8e..72b9b37a96c8 100644
@@ -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
index a31771bc89c1..1a012e033552 100644
@@ -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
maintainer
#1 lore raw
Quoted 21 lines · On Mon, 5 Jan 2026 12:36:27 +0800 Qing Wang <[email protected]> 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
Quoted 7 lines

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.

Quoted 4 lines

kernel/pid.c EXPORT_SYMBOL_GPL(find_vpid); · line 335

Quoted 10 lines
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;
 }

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?

author
#2 lore raw
Quoted 5 lines

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?

Thank you for your feedback. Regarding the "test-and-use" race condition you raised, I’ve thought about it extensively but haven’t found a better solution on the access side.

However, after re-examining the issue, I guess the root cause lies in the copy_process() flow itself, and we may not need complex handling at the access site:

1. The signal_struct is not fully managed by reference counting: In the normal (successful) path of copy_process(), the signal structure is indeed reference-counted, and its lifetime should be at least longer than the task’s. However, in the failure/cleanup path, signal is explicitly freed via free_signal_struct(), which prematurely ends its lifetime. At the same time, other subsystems (e.g., perf) might still hold references and attempt to access it—even if such access may be questionable.

2. A newly created task should not be visible to other CPUs during
creation: The perf subsystem copies the parent’s events
to the child during copy_process(). Later, when the parent closes or manipulates its own perf event, it may traverse child events and access child_ctx->task->signal. This means that a child process that has not yet been fully created can be referenced by other CPUs.

Based on this analysis, I propose two possible fixes—either one should resolve the issue:

1. Remove the explicit free_signal() in the cleanup path, and fully managed by reference counting for signal lifetime. Currently put_signal_struct() is only used in __put_task_struct(), so the lifetime of signal is longer than or equal to task.

2. Defer perf_event_init_task() until after copy_signal() succeeds, ensuring that if copy_process() failed perf events will be cleaned up before the signal. This guarantees that no perf event can access the signal.

I believe either approach would eliminate the issue. Could you please review whether this analysis and the proposed solutions are correct? Any guidance would be greatly appreciated.

#3 lore raw
In reply to Qing Wang
Quoted 9 lines · Qing Wang

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()

Quoted 7 lines
    perf_event_init_task()
    ...
    one copy failed
    free_signal_struct()            close(event_fd)
                                        perf_child_detach()
                                            __task_pid_nr_ns()
                                                access child task->signal

Sorry, this description very confusing to me... Is it Task B who does clone? Or another Task A does copy_process() ? Could you write a more clear changelog?

Quoted 16 lines

bad_fork_cleanup_signal:

(quoted diff) +5 -2
-	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 +0 -0
index a31771bc89c1..1a012e033552 100644

kernel/pid.c EXPORT_SYMBOL_GPL(find_vpid); · line 335

Quoted 10 lines
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;
 }

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +4 -3
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;
 
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }
author
#4 lore raw
Quoted 3 lines · Oleg Nesterov

Sorry, this description very confusing to me... Is it Task B who does clone? Or another Task A does copy_process() ? Could you write a more clear changelog?

The "<---...---clone" graph may have misled you. What I meant was that Task A is cloned from Task B.

The modified bug timeline with explanation:

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
    perf_event_init_task()

1. Task B create perf events by perf_event_open().
2. Task B clone Task A, and Task A have perf events copied from Task B in this clone().
3. Task A do one clone and fail to copy one(eg. copy_mm) in copy_process(), then goto cleanup free_signal_struct().
4. Task B do close(event_fd), and access Task A's signal after free_signal_struct() and before perf_event_init_task() in Task A.

Quoted 25 lines

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +1 -1
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;

kernel/events/core.c unclone_ctx(struct perf_event_context *c · line 1438

Quoted 10 lines
kernel/events/core.c +3 -2
 
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }

I think it doesn't work, as I explained in my previous reply to Andrew:

A newly created task should not be visible to other CPUs during
creation: The perf subsystem copies the parent’s events
to the child during copy_process(). Later, when the parent closes its own perf event, it may traverse child events and access child_ctx->task->signal. This means that a child process that has not yet been fully created can be referenced by other CPUs.

author
#5 lore raw
In reply to Oleg Nesterov
Quoted 25 lines · Oleg Nesterov

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +1 -1
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;

kernel/events/core.c unclone_ctx(struct perf_event_context *c · line 1438

Quoted 10 lines
kernel/events/core.c +3 -2
 
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }

Sorry, please ignore my previous reply. I've reconsidered your code, and using pid_alive() to check the validity of tsk->signal is actually correct. The pid is assigned after copy_signal(), so if a task has a PID, its tsk->signal memory is guaranteed to be valid.

#6 lore raw
Quoted 28 lines · Qing Wang
Quoted 26 lines · Oleg Nesterov

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +1 -1
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;
 
Quoted 14 lines

kernel/events/core.c unclone_ctx(struct perf_event_context *c · line 1438

Quoted 9 lines
kernel/events/core.c +3 -2
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }

Sorry, please ignore my previous reply. I've reconsidered your code, and using pid_alive() to check the validity of tsk->signal is actually correct. The pid is assigned after copy_signal(), so if a task has a PID, its tsk->signal memory is guaranteed to be valid.

Yes, if the child wasn't fully created then init_task_pid(child) was not called so pid_alive(p) can't be true.

OK, if you agree with this approach, can you make V2? Or do you prefer another approach?

The patch above is not 100% correct wrt "avoid -1 ...", but it seems that this can be fixed.

Oleg.

author
#7 lore raw
In reply to Oleg Nesterov
Quoted 25 lines · Oleg Nesterov

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +1 -1
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;

kernel/events/core.c unclone_ctx(struct perf_event_context *c · line 1438

Quoted 10 lines
kernel/events/core.c +3 -2
 
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }

Could we put the checking 'pid_alive(task)' into __task_pid_nr_ns()? Because there is another similar use case here.

arch/s390/kernel/perf_cpum_sf.c
619,9: pid = __task_pid_nr_ns(tsk, type, event->ns);

---

kernel/pid.c +1 -1
index a31771bc89c1..e8826731fa47 100644
@@ -515,7 +515,7 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
        rcu_read_lock();
        if (!ns)
                ns = task_active_pid_ns(current);
-       if (ns)
+       if (ns && pid_alive(task))
                nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
        rcu_read_unlock();
#8 lore raw
Quoted 2 lines · Qing Wang

Could we put the checking 'pid_alive(task)' into __task_pid_nr_ns()?

I don't think so... see below.

Quoted 5 lines

Because there is another similar use case here.

arch/s390/kernel/perf_cpum_sf.c
619,9: pid = __task_pid_nr_ns(tsk, type, event->ns);

This case is not similar. This tsk was found by find_task_by_pid_ns(), it must be fully initialized.

So I don't think it makes sense to add the additional check into __task_pid_nr_ns().

kernel/pid.c pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, · line 518

Quoted 8 lines
kernel/pid.c +1 -1
@@ -515,7 +515,7 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
        rcu_read_lock();
        if (!ns)
                ns = task_active_pid_ns(current);
-       if (ns)
+       if (ns && pid_alive(task))

This reminds me... the 2nd "if (ns) check must die. I'll ping Cristian. See https://lore.kernel.org/all/[email protected]/

Oleg.

author
#9 lore raw
Quoted 5 lines · Oleg Nesterov

This case is not similar. This tsk was found by find_task_by_pid_ns(), it must be fully initialized.

So I don't think it makes sense to add the additional check into __task_pid_nr_ns().

I agree with this. Let's make an new patch.

Quoted 11 lines

kernel/pid.c pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, · line 518

Quoted 8 lines
kernel/pid.c +1 -1
@@ -515,7 +515,7 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
        rcu_read_lock();
        if (!ns)
                ns = task_active_pid_ns(current);
-       if (ns)
+       if (ns && pid_alive(task))

This reminds me... the 2nd "if (ns) check must die. I'll ping Cristian. See https://lore.kernel.org/all/[email protected]/

I viewed this link. Your patches is not merged on master.

#10 lore raw
In reply to Oleg Nesterov

On a second thought...

sched_fork() is called before perf_event_init_task(). So perhaps sync_child_event() could also check task->__state != TASK_NEW before perf_event_read_event() ?

Not sure, I know nothing about perf. Would be nice if perf experts can take a look.

Oleg.

Quoted 85 lines · Oleg Nesterov
Quoted 9 lines · Qing Wang

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()

Quoted 7 lines
    perf_event_init_task()
    ...
    one copy failed
    free_signal_struct()            close(event_fd)
                                        perf_child_detach()
                                            __task_pid_nr_ns()
                                                access child task->signal

Sorry, this description very confusing to me... Is it Task B who does clone? Or another Task A does copy_process() ? Could you write a more clear changelog?

Quoted 16 lines

bad_fork_cleanup_signal:

(quoted diff) +5 -2
-	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 +0 -0
index a31771bc89c1..1a012e033552 100644

kernel/pid.c EXPORT_SYMBOL_GPL(find_vpid); · line 335

Quoted 10 lines
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;
 }

At first glance this is racy. Can't task->signal be freed right after the check?

And... Can't we make another fix? If copy_process() fails and does free_signal_struct(), the child has not been added to rcu protected lists and init_task_pid(child) was not called yet.

So perhaps something like the patch below can work?

Oleg.
---

kernel/events/core.c +4 -3
@@ -1422,16 +1422,17 @@ unclone_ctx(struct perf_event_context *c
 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
 				enum pid_type type)
 {
-	u32 nr;
+	u32 nr = 0;
 	/*
 	 * only top level events have the pid namespace they were created in
 	 */
 	if (event->parent)
 		event = event->parent;
 
-	nr = __task_pid_nr_ns(p, type, event->ns);
+	if (pid_alive(p))
+		nr = __task_pid_nr_ns(p, type, event->ns);
 	/* avoid -1 if it is idle thread or runs in another ns */
-	if (!nr && !pid_alive(p))
+	if (!nr)
 		nr = -1;
 	return nr;
 }
author
#11 lore raw
Quoted 10 lines · Oleg Nesterov

On a second thought...

sched_fork() is called before perf_event_init_task(). So perhaps sync_child_event() could also check task->__state != TASK_NEW before perf_event_read_event() ?

Not sure, I know nothing about perf. Would be nice if perf experts can take a look.

Oleg.

I agree with your idea. But we don't need to fix this issue anymore, because after reviewing the current mainline code, I found that it has already been resolved(c418d8b4d7a4 "perf/core: Fix missing read event generation on task exit") by moving sync_child_event() from perf_child_detach() into perf_event_exit_event().

Here https://patch.msgid.link/[email protected]

As a result, perf_event_read_event() no longer occurs on the problematic path reported (i.e., the close()->perf_release() path).

Qing.

#12 lore raw
Quoted 11 lines · Qing Wang

I agree with your idea. But we don't need to fix this issue anymore, because after reviewing the current mainline code, I found that it has already been resolved(c418d8b4d7a4 "perf/core: Fix missing read event generation on task exit") by moving sync_child_event() from perf_child_detach() into perf_event_exit_event().

Here https://patch.msgid.link/[email protected]

As a result, perf_event_read_event() no longer occurs on the problematic path reported (i.e., the close()->perf_release() path).

Great, thanks. So we can forget this problem ;)

Oleg.

#13 lore raw
In reply to Oleg Nesterov
Quoted 9 lines · Oleg Nesterov

On a second thought...

sched_fork() is called before perf_event_init_task(). So perhaps sync_child_event() could also check task->__state != TASK_NEW before perf_event_read_event() ?

Not sure, I know nothing about perf. Would be nice if perf experts can take a look.

Or something else, but we can't rely on pid_alive() or ->signal != NULL checks.

perf_event_init_task() is called soon after dup_task_struct(), so pid_alive() is true and child->signal == current->signal.

Lets forget about use-after-free. What if perf_child_detach() paths call __task_pid_nr_ns() before copy_signal/etc ? In this case perf_event_pid/perf_event_tid will return the pids of the forking process, not the child's pids.

Oleg.