I am using ptrace to intercept system calls. Everything seems to be working fine except for the fact I'm intercepting 16 calls to execve (8 for pre system call, and 8 post system call).
I have seen working examples without it, but I'm trying to use the flag PTRACE_O_TRACESYSGOOD
.
Other answers to ptrace problems indicate I should only see one pre/post + one signal, but they're not using PTRACE_O_TRACESYSGOOD
.
My output looks like:
Intercepted rt_sigprocmask[14]
Syscall returned with value 0
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value 0
Tracer: Received signal: 5
Intercepted brk[12]
...
The rest of the output matches what strace
outputs.
Every "Intercepted" and "Syscall returned" corresponds to one waitid()
call. A minimalist example code to reproduce this:
#include <sys/types.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/ptrace.h>
#include <sys/reg.h> /* For constants ORIG_EAX, etc */
#include <string.h>
#include <sys/wait.h>
#include <sys/syscall.h> /* For SYS_write, etc */
#include <unistd.h>
#include <stdio.h>
int main(){
pid_t pid = fork();
// Child.
if(pid == 0){
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
// Wait for parent to be ready.
raise(SIGSTOP);
execlp("pwd", "pwd", NULL);
return 0;
}
// Tracer.
else{
struct user_regs_struct regs;
bool isPre = true;
int status;
// Wait for child to stop itself.
waitpid(pid, &status, 0);
ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACESYSGOOD);
while(true){
ptrace(PTRACE_SYSCALL, pid, 0, 0);
pid = waitpid(pid, &status, 0);
// Check if tracee has exited.
if (WIFEXITED(status)){
return 0;
}
// This is a stop caused by a system call exit-pre/exit-post.
if(WIFSTOPPED(status) && WSTOPSIG(status) == (SIGTRAP |0x80) ){
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
if(isPre){
printf("Intercepted syscall: %llu\n", regs.orig_rax);
isPre = ! isPre;
}else{
printf("Done with system call!\n");
isPre = ! isPre;
}
}else{
printf("Tracer: Received signal: %d\n", WSTOPSIG(status));
}
}
}
return 0;
}
I fear I'm misundersting execve
, or PTRACE_O_TRACESYSGOOD
.
I'm running this on Lubuntu 16.04 with kernel version 4.10.0-37-generic.
edit: Fixed return values for system calls.