ptrace-syscall.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A ptrace test for testing PTRACE_SYSEMU, PTRACE_SETREGS and
  4. * PTRACE_GETREG. This test basically create a child process that executes
  5. * syscalls and the parent process check if it is being traced appropriated.
  6. *
  7. * This test is heavily based on tools/testing/selftests/x86/ptrace_syscall.c
  8. * test, and it was adapted to run on Powerpc by
  9. * Breno Leitao <[email protected]>
  10. */
  11. #define _GNU_SOURCE
  12. #include <sys/ptrace.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <sys/syscall.h>
  16. #include <sys/user.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <err.h>
  22. #include <string.h>
  23. #include <sys/auxv.h>
  24. #include "utils.h"
  25. /* Bitness-agnostic defines for user_regs_struct fields. */
  26. #define user_syscall_nr gpr[0]
  27. #define user_arg0 gpr[3]
  28. #define user_arg1 gpr[4]
  29. #define user_arg2 gpr[5]
  30. #define user_arg3 gpr[6]
  31. #define user_arg4 gpr[7]
  32. #define user_arg5 gpr[8]
  33. #define user_ip nip
  34. #define PTRACE_SYSEMU 0x1d
  35. static int nerrs;
  36. static void wait_trap(pid_t chld)
  37. {
  38. siginfo_t si;
  39. if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
  40. err(1, "waitid");
  41. if (si.si_pid != chld)
  42. errx(1, "got unexpected pid in event\n");
  43. if (si.si_code != CLD_TRAPPED)
  44. errx(1, "got unexpected event type %d\n", si.si_code);
  45. }
  46. static void test_ptrace_syscall_restart(void)
  47. {
  48. int status;
  49. struct pt_regs regs;
  50. pid_t chld;
  51. printf("[RUN]\tptrace-induced syscall restart\n");
  52. chld = fork();
  53. if (chld < 0)
  54. err(1, "fork");
  55. /*
  56. * Child process is running 4 syscalls after ptrace.
  57. *
  58. * 1) getpid()
  59. * 2) gettid()
  60. * 3) tgkill() -> Send SIGSTOP
  61. * 4) gettid() -> Where the tests will happen essentially
  62. */
  63. if (chld == 0) {
  64. if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
  65. err(1, "PTRACE_TRACEME");
  66. pid_t pid = getpid(), tid = syscall(SYS_gettid);
  67. printf("\tChild will make one syscall\n");
  68. syscall(SYS_tgkill, pid, tid, SIGSTOP);
  69. syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
  70. _exit(0);
  71. }
  72. /* Parent process below */
  73. /* Wait for SIGSTOP sent by tgkill above. */
  74. if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
  75. err(1, "waitpid");
  76. printf("[RUN]\tSYSEMU\n");
  77. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  78. err(1, "PTRACE_SYSEMU");
  79. wait_trap(chld);
  80. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  81. err(1, "PTRACE_GETREGS");
  82. /*
  83. * Ptrace trapped prior to executing the syscall, thus r3 still has
  84. * the syscall number instead of the sys_gettid() result
  85. */
  86. if (regs.user_syscall_nr != SYS_gettid ||
  87. regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
  88. regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
  89. regs.user_arg4 != 14 || regs.user_arg5 != 15) {
  90. printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
  91. (unsigned long)regs.user_syscall_nr,
  92. (unsigned long)regs.user_arg0,
  93. (unsigned long)regs.user_arg1,
  94. (unsigned long)regs.user_arg2,
  95. (unsigned long)regs.user_arg3,
  96. (unsigned long)regs.user_arg4,
  97. (unsigned long)regs.user_arg5);
  98. nerrs++;
  99. } else {
  100. printf("[OK]\tInitial nr and args are correct\n"); }
  101. printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
  102. (unsigned long)regs.user_ip);
  103. /*
  104. * Rewind to retry the same syscall again. This will basically test
  105. * the rewind process together with PTRACE_SETREGS and PTRACE_GETREGS.
  106. */
  107. regs.user_ip -= 4;
  108. if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
  109. err(1, "PTRACE_SETREGS");
  110. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  111. err(1, "PTRACE_SYSEMU");
  112. wait_trap(chld);
  113. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  114. err(1, "PTRACE_GETREGS");
  115. if (regs.user_syscall_nr != SYS_gettid ||
  116. regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
  117. regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
  118. regs.user_arg4 != 14 || regs.user_arg5 != 15) {
  119. printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
  120. (unsigned long)regs.user_syscall_nr,
  121. (unsigned long)regs.user_arg0,
  122. (unsigned long)regs.user_arg1,
  123. (unsigned long)regs.user_arg2,
  124. (unsigned long)regs.user_arg3,
  125. (unsigned long)regs.user_arg4,
  126. (unsigned long)regs.user_arg5);
  127. nerrs++;
  128. } else {
  129. printf("[OK]\tRestarted nr and args are correct\n");
  130. }
  131. printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
  132. (unsigned long)regs.user_ip);
  133. /*
  134. * Inject a new syscall (getpid) in the same place the previous
  135. * syscall (gettid), rewind and re-execute.
  136. */
  137. regs.user_syscall_nr = SYS_getpid;
  138. regs.user_arg0 = 20;
  139. regs.user_arg1 = 21;
  140. regs.user_arg2 = 22;
  141. regs.user_arg3 = 23;
  142. regs.user_arg4 = 24;
  143. regs.user_arg5 = 25;
  144. regs.user_ip -= 4;
  145. if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
  146. err(1, "PTRACE_SETREGS");
  147. if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
  148. err(1, "PTRACE_SYSEMU");
  149. wait_trap(chld);
  150. if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
  151. err(1, "PTRACE_GETREGS");
  152. /* Check that ptrace stopped at the new syscall that was
  153. * injected, and guarantee that it haven't executed, i.e, user_args
  154. * contain the arguments and not the syscall return value, for
  155. * instance.
  156. */
  157. if (regs.user_syscall_nr != SYS_getpid
  158. || regs.user_arg0 != 20 || regs.user_arg1 != 21
  159. || regs.user_arg2 != 22 || regs.user_arg3 != 23
  160. || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
  161. printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
  162. (unsigned long)regs.user_syscall_nr,
  163. (unsigned long)regs.user_arg0,
  164. (unsigned long)regs.user_arg1,
  165. (unsigned long)regs.user_arg2,
  166. (unsigned long)regs.user_arg3,
  167. (unsigned long)regs.user_arg4,
  168. (unsigned long)regs.user_arg5);
  169. nerrs++;
  170. } else {
  171. printf("[OK]\tReplacement nr and args are correct\n");
  172. }
  173. if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
  174. err(1, "PTRACE_CONT");
  175. if (waitpid(chld, &status, 0) != chld)
  176. err(1, "waitpid");
  177. /* Guarantee that the process executed properly, returning 0 */
  178. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  179. printf("[FAIL]\tChild failed\n");
  180. nerrs++;
  181. } else {
  182. printf("[OK]\tChild exited cleanly\n");
  183. }
  184. }
  185. int ptrace_syscall(void)
  186. {
  187. test_ptrace_syscall_restart();
  188. return nerrs;
  189. }
  190. int main(void)
  191. {
  192. return test_harness(ptrace_syscall, "ptrace_syscall");
  193. }