ptrace_syscall.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #define _GNU_SOURCE
  2. #include <sys/ptrace.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/syscall.h>
  6. #include <sys/user.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <err.h>
  12. #include <string.h>
  13. #include <asm/ptrace-abi.h>
  14. #include <sys/auxv.h>
  15. /* Bitness-agnostic defines for user_regs_struct fields. */
  16. #ifdef __x86_64__
  17. # define user_syscall_nr orig_rax
  18. # define user_arg0 rdi
  19. # define user_arg1 rsi
  20. # define user_arg2 rdx
  21. # define user_arg3 r10
  22. # define user_arg4 r8
  23. # define user_arg5 r9
  24. # define user_ip rip
  25. # define user_ax rax
  26. #else
  27. # define user_syscall_nr orig_eax
  28. # define user_arg0 ebx
  29. # define user_arg1 ecx
  30. # define user_arg2 edx
  31. # define user_arg3 esi
  32. # define user_arg4 edi
  33. # define user_arg5 ebp
  34. # define user_ip eip
  35. # define user_ax eax
  36. #endif
  37. static int nerrs = 0;
  38. struct syscall_args32 {
  39. uint32_t nr, arg0, arg1, arg2, arg3, arg4, arg5;
  40. };
  41. #ifdef __i386__
  42. extern void sys32_helper(struct syscall_args32 *, void *);
  43. extern void int80_and_ret(void);
  44. #endif
  45. /*
  46. * Helper to invoke int80 with controlled regs and capture the final regs.
  47. */
  48. static void do_full_int80(struct syscall_args32 *args)
  49. {
  50. #ifdef __x86_64__
  51. register unsigned long bp asm("bp") = args->arg5;
  52. asm volatile ("int $0x80"
  53. : "+a" (args->nr),
  54. "+b" (args->arg0), "+c" (args->arg1), "+d" (args->arg2),
  55. "+S" (args->arg3), "+D" (args->arg4), "+r" (bp));
  56. args->arg5 = bp;
  57. #else
  58. sys32_helper(args, int80_and_ret);
  59. #endif
  60. }
  61. #ifdef __i386__
  62. static void (*vsyscall32)(void);
  63. /*
  64. * Nasty helper to invoke AT_SYSINFO (i.e. __kernel_vsyscall) with
  65. * controlled regs and capture the final regs. This is so nasty that it
  66. * crashes my copy of gdb :)
  67. */
  68. static void do_full_vsyscall32(struct syscall_args32 *args)
  69. {
  70. sys32_helper(args, vsyscall32);
  71. }
  72. #endif
  73. static siginfo_t wait_trap(pid_t chld)
  74. {
  75. siginfo_t si;
  76. if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
  77. err(1, "waitid");
  78. if (si.si_pid != chld)
  79. errx(1, "got unexpected pid in event\n");
  80. if (si.si_code != CLD_TRAPPED)
  81. errx(1, "got unexpected event type %d\n", si.si_code);
  82. return si;
  83. }
  84. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  85. int flags)
  86. {
  87. struct sigaction sa;
  88. memset(&sa, 0, sizeof(sa));
  89. sa.sa_sigaction = handler;
  90. sa.sa_flags = SA_SIGINFO | flags;
  91. sigemptyset(&sa.sa_mask);
  92. if (sigaction(sig, &sa, 0))
  93. err(1, "sigaction");
  94. }
  95. static void setsigign(int sig, int flags)
  96. {
  97. struct sigaction sa;
  98. memset(&sa, 0, sizeof(sa));
  99. sa.sa_sigaction = (void *)SIG_IGN;
  100. sa.sa_flags = flags;
  101. sigemptyset(&sa.sa_mask);
  102. if (sigaction(sig, &sa, 0))
  103. err(1, "sigaction");
  104. }
  105. static void clearhandler(int sig)
  106. {
  107. struct sigaction sa;
  108. memset(&sa, 0, sizeof(sa));
  109. sa.sa_handler = SIG_DFL;
  110. sigemptyset(&sa.sa_mask);
  111. if (sigaction(sig, &sa, 0))
  112. err(1, "sigaction");
  113. }
  114. #ifdef __x86_64__
  115. # define REG_BP REG_RBP
  116. #else
  117. # define REG_BP REG_EBP
  118. #endif
  119. static void empty_handler(int sig, siginfo_t *si, void *ctx_void)
  120. {
  121. }
  122. static void test_sys32_regs(void (*do_syscall)(struct syscall_args32 *))
  123. {
  124. struct syscall_args32 args = {
  125. .nr = 224, /* gettid */
  126. .arg0 = 10, .arg1 = 11, .arg2 = 12,
  127. .arg3 = 13, .arg4 = 14, .arg5 = 15,
  128. };
  129. do_syscall(&args);
  130. if (args.nr != getpid() ||
  131. args.arg0 != 10 || args.arg1 != 11 || args.arg2 != 12 ||
  132. args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
  133. printf("[FAIL]\tgetpid() failed to preserve regs\n");
  134. nerrs++;
  135. } else {
  136. printf("[OK]\tgetpid() preserves regs\n");
  137. }
  138. sethandler(SIGUSR1, empty_handler, 0);
  139. args.nr = 37; /* kill */
  140. args.arg0 = getpid();
  141. args.arg1 = SIGUSR1;
  142. do_syscall(&args);
  143. if (args.nr != 0 ||
  144. args.arg0 != getpid() || args.arg1 != SIGUSR1 || args.arg2 != 12 ||
  145. args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
  146. printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preserve regs\n");
  147. nerrs++;
  148. } else {
  149. printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
  150. }
  151. clearhandler(SIGUSR1);
  152. }
  153. static void test_ptrace_syscall_restart(void)
  154. {
  155. printf("[RUN]\tptrace-induced syscall restart\n");
  156. pid_t chld = fork();
  157. if (chld < 0)
  158. err(1, "fork");
  159. if (chld == 0) {
  160. if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
  161. err(1, "PTRACE_TRACEME");
  162. printf("\tChild will make one syscall\n");
  163. raise(SIGSTOP);
  164. syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
  165. _exit(0);
  166. }
  167. int status;
  168. /* Wait for SIGSTOP. */
  169. if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
  170. err(1, "waitpid");
  171. struct user_regs_struct regs;
  172. printf("[RUN]\tSYSEMU\n");
  173. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  174. err(1, "PTRACE_SYSEMU");
  175. wait_trap(chld);
  176. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  177. err(1, "PTRACE_GETREGS");
  178. if (regs.user_syscall_nr != SYS_gettid ||
  179. regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
  180. regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
  181. regs.user_arg4 != 14 || regs.user_arg5 != 15) {
  182. printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  183. nerrs++;
  184. } else {
  185. printf("[OK]\tInitial nr and args are correct\n");
  186. }
  187. printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
  188. (unsigned long)regs.user_ip);
  189. /*
  190. * This does exactly what it appears to do if syscall is int80 or
  191. * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
  192. * magical. It needs to work so that ptrace and syscall restart
  193. * work as expected.
  194. */
  195. regs.user_ax = regs.user_syscall_nr;
  196. regs.user_ip -= 2;
  197. if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
  198. err(1, "PTRACE_SETREGS");
  199. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  200. err(1, "PTRACE_SYSEMU");
  201. wait_trap(chld);
  202. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  203. err(1, "PTRACE_GETREGS");
  204. if (regs.user_syscall_nr != SYS_gettid ||
  205. regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
  206. regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
  207. regs.user_arg4 != 14 || regs.user_arg5 != 15) {
  208. printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  209. nerrs++;
  210. } else {
  211. printf("[OK]\tRestarted nr and args are correct\n");
  212. }
  213. printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
  214. (unsigned long)regs.user_ip);
  215. regs.user_ax = SYS_getpid;
  216. regs.user_arg0 = 20;
  217. regs.user_arg1 = 21;
  218. regs.user_arg2 = 22;
  219. regs.user_arg3 = 23;
  220. regs.user_arg4 = 24;
  221. regs.user_arg5 = 25;
  222. regs.user_ip -= 2;
  223. if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
  224. err(1, "PTRACE_SETREGS");
  225. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  226. err(1, "PTRACE_SYSEMU");
  227. wait_trap(chld);
  228. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  229. err(1, "PTRACE_GETREGS");
  230. if (regs.user_syscall_nr != SYS_getpid ||
  231. regs.user_arg0 != 20 || regs.user_arg1 != 21 || regs.user_arg2 != 22 ||
  232. regs.user_arg3 != 23 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
  233. printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  234. nerrs++;
  235. } else {
  236. printf("[OK]\tReplacement nr and args are correct\n");
  237. }
  238. if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
  239. err(1, "PTRACE_CONT");
  240. if (waitpid(chld, &status, 0) != chld)
  241. err(1, "waitpid");
  242. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  243. printf("[FAIL]\tChild failed\n");
  244. nerrs++;
  245. } else {
  246. printf("[OK]\tChild exited cleanly\n");
  247. }
  248. }
  249. static void test_restart_under_ptrace(void)
  250. {
  251. printf("[RUN]\tkernel syscall restart under ptrace\n");
  252. pid_t chld = fork();
  253. if (chld < 0)
  254. err(1, "fork");
  255. if (chld == 0) {
  256. if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
  257. err(1, "PTRACE_TRACEME");
  258. printf("\tChild will take a nap until signaled\n");
  259. setsigign(SIGUSR1, SA_RESTART);
  260. raise(SIGSTOP);
  261. syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
  262. _exit(0);
  263. }
  264. int status;
  265. /* Wait for SIGSTOP. */
  266. if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
  267. err(1, "waitpid");
  268. struct user_regs_struct regs;
  269. printf("[RUN]\tSYSCALL\n");
  270. if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
  271. err(1, "PTRACE_SYSCALL");
  272. wait_trap(chld);
  273. /* We should be stopped at pause(2) entry. */
  274. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  275. err(1, "PTRACE_GETREGS");
  276. if (regs.user_syscall_nr != SYS_pause ||
  277. regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
  278. regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
  279. regs.user_arg4 != 0 || regs.user_arg5 != 0) {
  280. printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  281. nerrs++;
  282. } else {
  283. printf("[OK]\tInitial nr and args are correct\n");
  284. }
  285. /* Interrupt it. */
  286. kill(chld, SIGUSR1);
  287. /* Advance. We should be stopped at exit. */
  288. printf("[RUN]\tSYSCALL\n");
  289. if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
  290. err(1, "PTRACE_SYSCALL");
  291. wait_trap(chld);
  292. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  293. err(1, "PTRACE_GETREGS");
  294. if (regs.user_syscall_nr != SYS_pause ||
  295. regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
  296. regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
  297. regs.user_arg4 != 0 || regs.user_arg5 != 0) {
  298. printf("[FAIL]\tArgs after SIGUSR1 are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  299. nerrs++;
  300. } else {
  301. printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
  302. (long)regs.user_ax);
  303. }
  304. /* Poke the regs back in. This must not break anything. */
  305. if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
  306. err(1, "PTRACE_SETREGS");
  307. /* Catch the (ignored) SIGUSR1. */
  308. if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
  309. err(1, "PTRACE_CONT");
  310. if (waitpid(chld, &status, 0) != chld)
  311. err(1, "waitpid");
  312. if (!WIFSTOPPED(status)) {
  313. printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
  314. nerrs++;
  315. } else {
  316. printf("[OK]\tChild got SIGUSR1\n");
  317. }
  318. /* The next event should be pause(2) again. */
  319. printf("[RUN]\tStep again\n");
  320. if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
  321. err(1, "PTRACE_SYSCALL");
  322. wait_trap(chld);
  323. /* We should be stopped at pause(2) entry. */
  324. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  325. err(1, "PTRACE_GETREGS");
  326. if (regs.user_syscall_nr != SYS_pause ||
  327. regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
  328. regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
  329. regs.user_arg4 != 0 || regs.user_arg5 != 0) {
  330. printf("[FAIL]\tpause did not restart (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
  331. nerrs++;
  332. } else {
  333. printf("[OK]\tpause(2) restarted correctly\n");
  334. }
  335. /* Kill it. */
  336. kill(chld, SIGKILL);
  337. if (waitpid(chld, &status, 0) != chld)
  338. err(1, "waitpid");
  339. }
  340. int main()
  341. {
  342. printf("[RUN]\tCheck int80 return regs\n");
  343. test_sys32_regs(do_full_int80);
  344. #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
  345. vsyscall32 = (void *)getauxval(AT_SYSINFO);
  346. printf("[RUN]\tCheck AT_SYSINFO return regs\n");
  347. test_sys32_regs(do_full_vsyscall32);
  348. #endif
  349. test_ptrace_syscall_restart();
  350. test_restart_under_ptrace();
  351. return 0;
  352. }