Commit d73437ade6b00e559b73f805e272446e2afdd3b3
Committed by
Greg Kroah-Hartman
1 parent
0324896e2e
exit: fix race between wait_consider_task() and wait_task_zombie()
commit 3245d6acab981a2388ffb877c7ecc97e763c59d4 upstream. 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 b3ab03160dfa ("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> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Showing 1 changed file with 9 additions and 3 deletions Side-by-side Diff
kernel/exit.c
... | ... | @@ -1302,9 +1302,15 @@ |
1302 | 1302 | static int wait_consider_task(struct wait_opts *wo, int ptrace, |
1303 | 1303 | struct task_struct *p) |
1304 | 1304 | { |
1305 | + /* | |
1306 | + * We can race with wait_task_zombie() from another thread. | |
1307 | + * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition | |
1308 | + * can't confuse the checks below. | |
1309 | + */ | |
1310 | + int exit_state = ACCESS_ONCE(p->exit_state); | |
1305 | 1311 | int ret; |
1306 | 1312 | |
1307 | - if (unlikely(p->exit_state == EXIT_DEAD)) | |
1313 | + if (unlikely(exit_state == EXIT_DEAD)) | |
1308 | 1314 | return 0; |
1309 | 1315 | |
1310 | 1316 | ret = eligible_child(wo, p); |
... | ... | @@ -1325,7 +1331,7 @@ |
1325 | 1331 | return 0; |
1326 | 1332 | } |
1327 | 1333 | |
1328 | - if (unlikely(p->exit_state == EXIT_TRACE)) { | |
1334 | + if (unlikely(exit_state == EXIT_TRACE)) { | |
1329 | 1335 | /* |
1330 | 1336 | * ptrace == 0 means we are the natural parent. In this case |
1331 | 1337 | * we should clear notask_error, debugger will notify us. |
... | ... | @@ -1352,7 +1358,7 @@ |
1352 | 1358 | } |
1353 | 1359 | |
1354 | 1360 | /* slay zombie? */ |
1355 | - if (p->exit_state == EXIT_ZOMBIE) { | |
1361 | + if (exit_state == EXIT_ZOMBIE) { | |
1356 | 1362 | /* we don't reap group leaders with subthreads */ |
1357 | 1363 | if (!delay_group_leader(p)) { |
1358 | 1364 | /* |