Skip to content

Commit b72c186

Browse files
oleg-nesterovtorvalds
authored andcommitted
ptrace: fix race between ptrace_resume() and wait_task_stopped()
ptrace_resume() is called when the tracee is still __TASK_TRACED. We set tracee->exit_code and then wake_up_state() changes tracee->state. If the tracer's sub-thread does wait() in between, task_stopped_code(ptrace => T) wrongly looks like another report from tracee. This confuses debugger, and since wait_task_stopped() clears ->exit_code the tracee can miss a signal. Test-case: #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #include <sys/ptrace.h> #include <pthread.h> #include <assert.h> int pid; void *waiter(void *arg) { int stat; for (;;) { assert(pid == wait(&stat)); assert(WIFSTOPPED(stat)); if (WSTOPSIG(stat) == SIGHUP) continue; assert(WSTOPSIG(stat) == SIGCONT); printf("ERR! extra/wrong report:%x\n", stat); } } int main(void) { pthread_t thread; pid = fork(); if (!pid) { assert(ptrace(PTRACE_TRACEME, 0,0,0) == 0); for (;;) kill(getpid(), SIGHUP); } assert(pthread_create(&thread, NULL, waiter, NULL) == 0); for (;;) ptrace(PTRACE_CONT, pid, 0, SIGCONT); return 0; } Note for stable: the bug is very old, but without 9899d11 "ptrace: ensure arch_ptrace/ptrace_request can never race with SIGKILL" the fix should use lock_task_sighand(child). Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reported-by: Pavel Labath <labath@google.com> Tested-by: Pavel Labath <labath@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8de560d commit b72c186

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎kernel/ptrace.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ static int ptrace_peek_siginfo(struct task_struct *child,
697697
static int ptrace_resume(struct task_struct *child, long request,
698698
unsigned long data)
699699
{
700+
bool need_siglock;
701+
700702
if (!valid_signal(data))
701703
return -EIO;
702704

@@ -724,8 +726,26 @@ static int ptrace_resume(struct task_struct *child, long request,
724726
user_disable_single_step(child);
725727
}
726728

729+
/*
730+
* Change ->exit_code and ->state under siglock to avoid the race
731+
* with wait_task_stopped() in between; a non-zero ->exit_code will
732+
* wrongly look like another report from tracee.
733+
*
734+
* Note that we need siglock even if ->exit_code == data and/or this
735+
* status was not reported yet, the new status must not be cleared by
736+
* wait_task_stopped() after resume.
737+
*
738+
* If data == 0 we do not care if wait_task_stopped() reports the old
739+
* status and clears the code too; this can't race with the tracee, it
740+
* takes siglock after resume.
741+
*/
742+
need_siglock = data && !thread_group_empty(current);
743+
if (need_siglock)
744+
spin_lock_irq(&child->sighand->siglock);
727745
child->exit_code = data;
728746
wake_up_state(child, __TASK_TRACED);
747+
if (need_siglock)
748+
spin_unlock_irq(&child->sighand->siglock);
729749

730750
return 0;
731751
}

0 commit comments

Comments
 (0)