Skip to content

Commit

Permalink
Stop child to let parent set ptrace options once
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Feb 8, 2023
1 parent 72c0f98 commit 31b23a2
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/basset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#undef PTRACE_SYSCALL
#undef PTRACE_TRACEME

#include <csignal>
#include <iostream>

using std::cerr;
Expand All @@ -26,6 +27,27 @@ int main(int argc, char *argv[]) {

int wstatus;

if (wait(&wstatus) == -1) {
perror("cannot wait()");
return -1;
}

if (!WIFSTOPPED(wstatus) || WSTOPSIG(wstatus) != SIGSTOP) {
cerr << "unexpected state of child\n";
return -1;
}

if (ptrace(PTRACE_SETOPTIONS, pid, nullptr,
PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK |
PTRACE_O_TRACEEXEC | PTRACE_O_TRACESYSGOOD) == -1) {
perror("cannot ptrace(PTRACE_SETOPTIONS)");
return -1;
}

if (ptrace(PTRACE_SYSCALL, pid, nullptr, nullptr) == -1) {
perror("cannot ptrace(PTRACE_CONT)");
}

while (auto pid = wait(&wstatus)) {
if (pid == -1) {
perror("cannot wait()");
Expand All @@ -37,13 +59,6 @@ int main(int argc, char *argv[]) {
} else if (WIFSTOPPED(wstatus)) {
cerr << pid << " stopped\n";

if (ptrace(PTRACE_SETOPTIONS, pid, nullptr,
PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK |
PTRACE_O_TRACEVFORK | PTRACE_O_TRACEEXEC |
PTRACE_O_TRACESYSGOOD) == -1) {
perror("cannot ptrace(PTRACE_SETOPTIONS)");
}

ptrace_syscall_info data;

if (auto res =
Expand Down Expand Up @@ -85,6 +100,12 @@ int main(int argc, char *argv[]) {
return -1;
}

// allow parent to ptrace(PTRACE_SETOPTIONS)
if (raise(SIGSTOP) != 0) {
perror("cannot raise(SIGSTOP)");
return -1;
}

execvp(*argv, argv);
// on success, execve() does not return
perror("cannot execve()");
Expand Down

0 comments on commit 31b23a2

Please sign in to comment.