Skip to content

Commit

Permalink
Call waitpid() after ptrace(PTRACE_ATTACH)
Browse files Browse the repository at this point in the history
man ptrace says:

    PTRACE_ATTACH
        Attach to the process specified in pid, making it a tracee
        of the calling process. The tracee is sent a SIGSTOP, but
        will not necessarily have stopped by the completion of this
        call; use waitpid(2) to wait for the tracee to stop.

This means there is a race condition: for a short time after
ptrace(PTRACE_ATTACH) we cannot access memory of traced process.
  • Loading branch information
mephi42 committed Nov 18, 2015
1 parent b14ea7c commit 26589bb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions collector/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ open_pid (int pid)
{
char name[1024];
DumpState *s;
int status;

if (ptrace (PTRACE_ATTACH, pid, 0, 0)) {
log ("cannot ptrace %d: %s\n", pid, strerror (errno));
return NULL;
}
if (waitpid (pid, &status, 0) < 0) {
log ("waitpid(%d) failed: %s\n", pid, strerror (errno));
ptrace (PTRACE_DETACH, pid, 0, 0);
return NULL;
}
if (!WIFSTOPPED(status)) {
log ("waitpid(%d) returned unexpected status %d\n", pid, status);
ptrace (PTRACE_DETACH, pid, 0, 0);
return NULL;
}

snprintf (name, 1024, "/proc/%d/mem", pid);
s = calloc (sizeof (DumpState), 1);
Expand Down

0 comments on commit 26589bb

Please sign in to comment.