Skip to contentSkip to conversation

fork: fix copy_process(CLONE_PARENT) race with the exiting ->real_parent

PATCH Applied Eddy Wu 4 participants 5 messages Nov 7, 2020 – Nov 9, 2020

Subsystems (4)
lore
Share & tools
lore

Eddy Wu

author
#0 lore raw

PATCH 1/1 fork: fix copy_process(CLONE_PARENT) race with the exiting ->real_parent

current->group_leader->exit_signal may change during copy_process() if current->real_parent exits, move the assignment inside tasklist_lock to avoid the race.

---
 kernel/fork.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/fork.c +5 -5
index da8d360fb032..7abda2a888a9 100644
@@ -2142,14 +2142,9 @@ static __latent_entropy struct task_struct *copy_process(
 	/* ok, now we should be set up.. */
 	p->pid = pid_nr(pid);
 	if (clone_flags & CLONE_THREAD) {
-		p->exit_signal = -1;
 		p->group_leader = current->group_leader;
 		p->tgid = current->tgid;
 	} else {
-		if (clone_flags & CLONE_PARENT)
-			p->exit_signal = current->group_leader->exit_signal;
-		else
-			p->exit_signal = args->exit_signal;
 		p->group_leader = p;
 		p->tgid = p->pid;
 	}
@@ -2193,9 +2188,14 @@ static __latent_entropy struct task_struct *copy_process(
 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
 		p->real_parent = current->real_parent;
 		p->parent_exec_id = current->parent_exec_id;
+		if (clone_flags & CLONE_THREAD)
+			p->exit_signal = -1;
+		else
+			p->exit_signal = current->group_leader->exit_signal;
 	} else {
 		p->real_parent = current;
 		p->parent_exec_id = current->self_exec_id;
+		p->exit_signal = args->exit_signal;
 	}
 
 	klp_copy_process(p);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Eddy Wu

current->group_leader->exit_signal may change during copy_process() if current->real_parent exits, move the assignment inside tasklist_lock to avoid the race.

Applied. Thanks,

Linus

#2 lore raw

.. oh, and I suspect it should have been marked for stable.

I added Oleg's ack (implicit in an earlier thread), but didn't add a stable tag.

It's commit b4e00444cab4 ("fork: fix copy_process(CLONE_PARENT) race with the exiting ->real_parent") in my tree.

I'm not sure how serious it is. Yeah, the race can cause the wrong exit signal in theory, but I think you almost have to do it with cooperating processes, at which point you could have just done it intentionally in the first place.

But it does look like a real data race, and the fix looks small and obvious enough that I think it's stable material.

Oleg?

Linus

Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Eddy Wu

current->group_leader->exit_signal may change during copy_process() if current->real_parent exits, move the assignment inside tasklist_lock to avoid the race.

Applied. Thanks,

Linus

#3 lore raw
Quoted 7 lines · Linus Torvalds

.. oh, and I suspect it should have been marked for stable.

I added Oleg's ack (implicit in an earlier thread), but didn't add a stable tag.

It's commit b4e00444cab4 ("fork: fix copy_process(CLONE_PARENT) race with the exiting ->real_parent") in my tree.

Quoted 7 lines

I'm not sure how serious it is. Yeah, the race can cause the wrong exit signal in theory, but I think you almost have to do it with cooperating processes, at which point you could have just done it intentionally in the first place.

But it does look like a real data race, and the fix looks small and obvious enough that I think it's stable material.

Now queued up to stable trees, thanks!

greg k-h

#4 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

But it does look like a real data race, and the fix looks small and obvious enough that I think it's stable material.

Agreed, the patch looks fine for -stable. I don't think that cgroup_can_fork() could ever use thread_group_leader() or anything else which checks ->exit_code...

And I see that Greg has already sent the patches with the correct modifications for the case when we need "clone_flags & CSIGNAL" rather than args->exit_signal.

Thanks Greg,

Oleg.