/src/ptracetest2.c

http://github.com/Eelis/geordi · C · 40 lines · 30 code · 8 blank · 2 comment · 4 complexity · 5ee060b9285ab161b14feb9ba56efd6e MD5 · raw file

  1. #include <assert.h>
  2. #include <sys/ptrace.h>
  3. #include <linux/ptrace.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <syscall.h>
  9. #include <sys/reg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #ifdef __x86_64__
  13. #define SYSCALL_OFF (ORIG_RAX * 8)
  14. #else
  15. #define SYSCALL_OFF (ORIG_EAX * 4)
  16. #endif
  17. void checked (char const * const s, int const r) { if (r == -1) { perror(s); abort(); } }
  18. int main()
  19. {
  20. pid_t const child = fork();
  21. checked("fork", child);
  22. if(child == 0)
  23. {
  24. checked("ptrace", ptrace(PTRACE_TRACEME, 0, NULL, NULL));
  25. sleep(2);
  26. checked("execl", execl("/usr/bin/whoami", "whoami", NULL));
  27. }
  28. checked("ptrace", ptrace(PTRACE_SETOPTIONS, child, NULL, PTRACE_O_TRACESYSGOOD));
  29. // Will fail saying: ptrace: No such process
  30. // Conclusion: disregarding sleep-solutions, this means the parent cannot reliably set PTRACE_O_TRACESYSGOOD before the first wait().
  31. return 0;
  32. }