fsgsbase_restore.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fsgsbase_restore.c, test ptrace vs fsgsbase
  4. * Copyright (c) 2020 Andy Lutomirski
  5. *
  6. * This test case simulates a tracer redirecting tracee execution to
  7. * a function and then restoring tracee state using PTRACE_GETREGS and
  8. * PTRACE_SETREGS. This is similar to what gdb does when doing
  9. * 'p func()'. The catch is that this test has the called function
  10. * modify a segment register. This makes sure that ptrace correctly
  11. * restores segment state when using PTRACE_SETREGS.
  12. *
  13. * This is not part of fsgsbase.c, because that test is 64-bit only.
  14. */
  15. #define _GNU_SOURCE
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <sys/syscall.h>
  21. #include <unistd.h>
  22. #include <err.h>
  23. #include <sys/user.h>
  24. #include <asm/prctl.h>
  25. #include <sys/prctl.h>
  26. #include <asm/ldt.h>
  27. #include <sys/mman.h>
  28. #include <stddef.h>
  29. #include <sys/ptrace.h>
  30. #include <sys/wait.h>
  31. #include <stdint.h>
  32. #define EXPECTED_VALUE 0x1337f00d
  33. #ifdef __x86_64__
  34. # define SEG "%gs"
  35. #else
  36. # define SEG "%fs"
  37. #endif
  38. /*
  39. * Defined in clang_helpers_[32|64].S, because unlike gcc, clang inline asm does
  40. * not support segmentation prefixes.
  41. */
  42. unsigned int dereference_seg_base(void);
  43. static void init_seg(void)
  44. {
  45. unsigned int *target = mmap(
  46. NULL, sizeof(unsigned int),
  47. PROT_READ | PROT_WRITE,
  48. MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
  49. if (target == MAP_FAILED)
  50. err(1, "mmap");
  51. *target = EXPECTED_VALUE;
  52. printf("\tsegment base address = 0x%lx\n", (unsigned long)target);
  53. struct user_desc desc = {
  54. .entry_number = 0,
  55. .base_addr = (unsigned int)(uintptr_t)target,
  56. .limit = sizeof(unsigned int) - 1,
  57. .seg_32bit = 1,
  58. .contents = 0, /* Data, grow-up */
  59. .read_exec_only = 0,
  60. .limit_in_pages = 0,
  61. .seg_not_present = 0,
  62. .useable = 0
  63. };
  64. if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) == 0) {
  65. printf("\tusing LDT slot 0\n");
  66. asm volatile ("mov %0, %" SEG :: "rm" ((unsigned short)0x7));
  67. } else {
  68. /* No modify_ldt for us (configured out, perhaps) */
  69. struct user_desc *low_desc = mmap(
  70. NULL, sizeof(desc),
  71. PROT_READ | PROT_WRITE,
  72. MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
  73. memcpy(low_desc, &desc, sizeof(desc));
  74. low_desc->entry_number = -1;
  75. /* 32-bit set_thread_area */
  76. long ret;
  77. asm volatile ("int $0x80"
  78. : "=a" (ret), "+m" (*low_desc)
  79. : "a" (243), "b" (low_desc)
  80. #ifdef __x86_64__
  81. : "r8", "r9", "r10", "r11"
  82. #endif
  83. );
  84. memcpy(&desc, low_desc, sizeof(desc));
  85. munmap(low_desc, sizeof(desc));
  86. if (ret != 0) {
  87. printf("[NOTE]\tcould not create a segment -- can't test anything\n");
  88. exit(0);
  89. }
  90. printf("\tusing GDT slot %d\n", desc.entry_number);
  91. unsigned short sel = (unsigned short)((desc.entry_number << 3) | 0x3);
  92. asm volatile ("mov %0, %" SEG :: "rm" (sel));
  93. }
  94. }
  95. static void tracee_zap_segment(void)
  96. {
  97. /*
  98. * The tracer will redirect execution here. This is meant to
  99. * work like gdb's 'p func()' feature. The tricky bit is that
  100. * we modify a segment register in order to make sure that ptrace
  101. * can correctly restore segment registers.
  102. */
  103. printf("\tTracee: in tracee_zap_segment()\n");
  104. /*
  105. * Write a nonzero selector with base zero to the segment register.
  106. * Using a null selector would defeat the test on AMD pre-Zen2
  107. * CPUs, as such CPUs don't clear the base when loading a null
  108. * selector.
  109. */
  110. unsigned short sel;
  111. asm volatile ("mov %%ss, %0\n\t"
  112. "mov %0, %" SEG
  113. : "=rm" (sel));
  114. pid_t pid = getpid(), tid = syscall(SYS_gettid);
  115. printf("\tTracee is going back to sleep\n");
  116. syscall(SYS_tgkill, pid, tid, SIGSTOP);
  117. /* Should not get here. */
  118. while (true) {
  119. printf("[FAIL]\tTracee hit unreachable code\n");
  120. pause();
  121. }
  122. }
  123. int main()
  124. {
  125. printf("\tSetting up a segment\n");
  126. init_seg();
  127. unsigned int val = dereference_seg_base();
  128. if (val != EXPECTED_VALUE) {
  129. printf("[FAIL]\tseg[0] == %x; should be %x\n", val, EXPECTED_VALUE);
  130. return 1;
  131. }
  132. printf("[OK]\tThe segment points to the right place.\n");
  133. pid_t chld = fork();
  134. if (chld < 0)
  135. err(1, "fork");
  136. if (chld == 0) {
  137. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0, 0);
  138. if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
  139. err(1, "PTRACE_TRACEME");
  140. pid_t pid = getpid(), tid = syscall(SYS_gettid);
  141. printf("\tTracee will take a nap until signaled\n");
  142. syscall(SYS_tgkill, pid, tid, SIGSTOP);
  143. printf("\tTracee was resumed. Will re-check segment.\n");
  144. val = dereference_seg_base();
  145. if (val != EXPECTED_VALUE) {
  146. printf("[FAIL]\tseg[0] == %x; should be %x\n", val, EXPECTED_VALUE);
  147. exit(1);
  148. }
  149. printf("[OK]\tThe segment points to the right place.\n");
  150. exit(0);
  151. }
  152. int status;
  153. /* Wait for SIGSTOP. */
  154. if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
  155. err(1, "waitpid");
  156. struct user_regs_struct regs;
  157. if (ptrace(PTRACE_GETREGS, chld, NULL, &regs) != 0)
  158. err(1, "PTRACE_GETREGS");
  159. #ifdef __x86_64__
  160. printf("\tChild GS=0x%lx, GSBASE=0x%lx\n", (unsigned long)regs.gs, (unsigned long)regs.gs_base);
  161. #else
  162. printf("\tChild FS=0x%lx\n", (unsigned long)regs.xfs);
  163. #endif
  164. struct user_regs_struct regs2 = regs;
  165. #ifdef __x86_64__
  166. regs2.rip = (unsigned long)tracee_zap_segment;
  167. regs2.rsp -= 128; /* Don't clobber the redzone. */
  168. #else
  169. regs2.eip = (unsigned long)tracee_zap_segment;
  170. #endif
  171. printf("\tTracer: redirecting tracee to tracee_zap_segment()\n");
  172. if (ptrace(PTRACE_SETREGS, chld, NULL, &regs2) != 0)
  173. err(1, "PTRACE_GETREGS");
  174. if (ptrace(PTRACE_CONT, chld, NULL, NULL) != 0)
  175. err(1, "PTRACE_GETREGS");
  176. /* Wait for SIGSTOP. */
  177. if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
  178. err(1, "waitpid");
  179. printf("\tTracer: restoring tracee state\n");
  180. if (ptrace(PTRACE_SETREGS, chld, NULL, &regs) != 0)
  181. err(1, "PTRACE_GETREGS");
  182. if (ptrace(PTRACE_DETACH, chld, NULL, NULL) != 0)
  183. err(1, "PTRACE_GETREGS");
  184. /* Wait for SIGSTOP. */
  185. if (waitpid(chld, &status, 0) != chld)
  186. err(1, "waitpid");
  187. if (WIFSIGNALED(status)) {
  188. printf("[FAIL]\tTracee crashed\n");
  189. return 1;
  190. }
  191. if (!WIFEXITED(status)) {
  192. printf("[FAIL]\tTracee stopped for an unexpected reason: %d\n", status);
  193. return 1;
  194. }
  195. int exitcode = WEXITSTATUS(status);
  196. if (exitcode != 0) {
  197. printf("[FAIL]\tTracee reported failure\n");
  198. return 1;
  199. }
  200. printf("[OK]\tAll is well.\n");
  201. return 0;
  202. }