Skip to content

Commit 3245d6a

Browse files
oleg-nesterovtorvalds
authored andcommitted
exit: fix race between wait_consider_task() and wait_task_zombie()
wait_consider_task() checks EXIT_ZOMBIE after EXIT_DEAD/EXIT_TRACE and both checks can fail if we race with EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE change in between, gcc needs to reload p->exit_state after security_task_wait(). In this case ->notask_error will be wrongly cleared and do_wait() can hang forever if it was the last eligible child. Many thanks to Arne who carefully investigated the problem. Note: this bug is very old but it was pure theoretical until commit b3ab031 ("wait: completely ignore the EXIT_DEAD tasks"). Before this commit "-O2" was probably enough to guarantee that compiler won't read ->exit_state twice. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reported-by: Arne Goedeke <el@laramies.com> Tested-by: Arne Goedeke <el@laramies.com> Cc: <stable@vger.kernel.org> [3.15+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent eb4f73b commit 3245d6a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

kernel/exit.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,9 +1287,15 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
12871287
static int wait_consider_task(struct wait_opts *wo, int ptrace,
12881288
struct task_struct *p)
12891289
{
1290+
/*
1291+
* We can race with wait_task_zombie() from another thread.
1292+
* Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
1293+
* can't confuse the checks below.
1294+
*/
1295+
int exit_state = ACCESS_ONCE(p->exit_state);
12901296
int ret;
12911297

1292-
if (unlikely(p->exit_state == EXIT_DEAD))
1298+
if (unlikely(exit_state == EXIT_DEAD))
12931299
return 0;
12941300

12951301
ret = eligible_child(wo, p);
@@ -1310,7 +1316,7 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace,
13101316
return 0;
13111317
}
13121318

1313-
if (unlikely(p->exit_state == EXIT_TRACE)) {
1319+
if (unlikely(exit_state == EXIT_TRACE)) {
13141320
/*
13151321
* ptrace == 0 means we are the natural parent. In this case
13161322
* we should clear notask_error, debugger will notify us.
@@ -1337,7 +1343,7 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace,
13371343
}
13381344

13391345
/* slay zombie? */
1340-
if (p->exit_state == EXIT_ZOMBIE) {
1346+
if (exit_state == EXIT_ZOMBIE) {
13411347
/* we don't reap group leaders with subthreads */
13421348
if (!delay_group_leader(p)) {
13431349
/*

0 commit comments

Comments
 (0)