Skip to content

Commit c6c70f4

Browse files
oleg-nesterovebiederm
authored andcommitted
exit: fix the setns() && PR_SET_CHILD_SUBREAPER interaction
find_new_reaper() checks same_thread_group(reaper, child_reaper) to prevent the cross-namespace reparenting but this is not enough if the exiting parent was injected by setns() + fork(). Suppose we have a process P in the root namespace and some namespace X. P does setns() to enter the X namespace, and forks the child C. C forks a grandchild G and exits. The grandchild G should be re-parented to X->child_reaper, but in this case the ->real_parent chain does not lead to ->child_reaper, so it will be wrongly reparanted to P's sub-reaper or a global init. Signed-off-by: Oleg Nesterov <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent 1328c72 commit c6c70f4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

kernel/exit.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,15 +578,18 @@ static struct task_struct *find_new_reaper(struct task_struct *father,
578578
return thread;
579579

580580
if (father->signal->has_child_subreaper) {
581+
unsigned int ns_level = task_pid(father)->level;
581582
/*
582583
* Find the first ->is_child_subreaper ancestor in our pid_ns.
583-
* We start from father to ensure we can not look into another
584-
* namespace, this is safe because all its threads are dead.
584+
* We can't check reaper != child_reaper to ensure we do not
585+
* cross the namespaces, the exiting parent could be injected
586+
* by setns() + fork().
587+
* We check pid->level, this is slightly more efficient than
588+
* task_active_pid_ns(reaper) != task_active_pid_ns(father).
585589
*/
586-
for (reaper = father;
587-
!same_thread_group(reaper, child_reaper);
590+
for (reaper = father->real_parent;
591+
task_pid(reaper)->level == ns_level;
588592
reaper = reaper->real_parent) {
589-
/* call_usermodehelper() descendants need this check */
590593
if (reaper == &init_task)
591594
break;
592595
if (!reaper->signal->is_child_subreaper)

0 commit comments

Comments
 (0)