Skip to content

Commit 64a4096

Browse files
oleg-nesterovtorvalds
authored andcommitted
ptrace: ptrace_detach() can no longer race with SIGKILL
ptrace_detach() re-checks ->ptrace under tasklist lock and calls release_task() if __ptrace_detach() returns true. This was needed because the __TASK_TRACED tracee could be killed/untraced, and it could even pass exit_notify() before we take tasklist_lock. But this is no longer possible after 9899d11 "ptrace: ensure arch_ptrace/ptrace_request can never race with SIGKILL". We can turn these checks into WARN_ON() and remove release_task(). While at it, document the setting of child->exit_code. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Pavel Labath <labath@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b72c186 commit 64a4096

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

‎kernel/ptrace.c‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,6 @@ static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
456456

457457
static int ptrace_detach(struct task_struct *child, unsigned int data)
458458
{
459-
bool dead = false;
460-
461459
if (!valid_signal(data))
462460
return -EIO;
463461

@@ -467,18 +465,19 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
467465

468466
write_lock_irq(&tasklist_lock);
469467
/*
470-
* This child can be already killed. Make sure de_thread() or
471-
* our sub-thread doing do_wait() didn't do release_task() yet.
468+
* We rely on ptrace_freeze_traced(). It can't be killed and
469+
* untraced by another thread, it can't be a zombie.
472470
*/
473-
if (child->ptrace) {
474-
child->exit_code = data;
475-
dead = __ptrace_detach(current, child);
476-
}
471+
WARN_ON(!child->ptrace || child->exit_state);
472+
/*
473+
* tasklist_lock avoids the race with wait_task_stopped(), see
474+
* the comment in ptrace_resume().
475+
*/
476+
child->exit_code = data;
477+
__ptrace_detach(current, child);
477478
write_unlock_irq(&tasklist_lock);
478479

479480
proc_ptrace_connector(child, PTRACE_DETACH);
480-
if (unlikely(dead))
481-
release_task(child);
482481

483482
return 0;
484483
}

0 commit comments

Comments
 (0)