mov_ss_trap.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * mov_ss_trap.c: Exercise the bizarre side effects of a watchpoint on MOV SS
  4. *
  5. * This does MOV SS from a watchpointed address followed by various
  6. * types of kernel entries. A MOV SS that hits a watchpoint will queue
  7. * up a #DB trap but will not actually deliver that trap. The trap
  8. * will be delivered after the next instruction instead. The CPU's logic
  9. * seems to be:
  10. *
  11. * - Any fault: drop the pending #DB trap.
  12. * - INT $N, INT3, INTO, SYSCALL, SYSENTER: enter the kernel and then
  13. * deliver #DB.
  14. * - ICEBP: enter the kernel but do not deliver the watchpoint trap
  15. * - breakpoint: only one #DB is delivered (phew!)
  16. *
  17. * There are plenty of ways for a kernel to handle this incorrectly. This
  18. * test tries to exercise all the cases.
  19. *
  20. * This should mostly cover CVE-2018-1087 and CVE-2018-8897.
  21. */
  22. #define _GNU_SOURCE
  23. #include <stdlib.h>
  24. #include <sys/ptrace.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <sys/user.h>
  28. #include <sys/syscall.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <stddef.h>
  32. #include <stdio.h>
  33. #include <err.h>
  34. #include <string.h>
  35. #include <setjmp.h>
  36. #include <sys/prctl.h>
  37. #include "helpers.h"
  38. #if __x86_64__
  39. # define REG_IP REG_RIP
  40. #else
  41. # define REG_IP REG_EIP
  42. #endif
  43. unsigned short ss;
  44. extern unsigned char breakpoint_insn[];
  45. sigjmp_buf jmpbuf;
  46. static void enable_watchpoint(void)
  47. {
  48. pid_t parent = getpid();
  49. int status;
  50. pid_t child = fork();
  51. if (child < 0)
  52. err(1, "fork");
  53. if (child) {
  54. if (waitpid(child, &status, 0) != child)
  55. err(1, "waitpid for child");
  56. } else {
  57. unsigned long dr0, dr1, dr7;
  58. dr0 = (unsigned long)&ss;
  59. dr1 = (unsigned long)breakpoint_insn;
  60. dr7 = ((1UL << 1) | /* G0 */
  61. (3UL << 16) | /* RW0 = read or write */
  62. (1UL << 18) | /* LEN0 = 2 bytes */
  63. (1UL << 3)); /* G1, RW1 = insn */
  64. if (ptrace(PTRACE_ATTACH, parent, NULL, NULL) != 0)
  65. err(1, "PTRACE_ATTACH");
  66. if (waitpid(parent, &status, 0) != parent)
  67. err(1, "waitpid for child");
  68. if (ptrace(PTRACE_POKEUSER, parent, (void *)offsetof(struct user, u_debugreg[0]), dr0) != 0)
  69. err(1, "PTRACE_POKEUSER DR0");
  70. if (ptrace(PTRACE_POKEUSER, parent, (void *)offsetof(struct user, u_debugreg[1]), dr1) != 0)
  71. err(1, "PTRACE_POKEUSER DR1");
  72. if (ptrace(PTRACE_POKEUSER, parent, (void *)offsetof(struct user, u_debugreg[7]), dr7) != 0)
  73. err(1, "PTRACE_POKEUSER DR7");
  74. printf("\tDR0 = %lx, DR1 = %lx, DR7 = %lx\n", dr0, dr1, dr7);
  75. if (ptrace(PTRACE_DETACH, parent, NULL, NULL) != 0)
  76. err(1, "PTRACE_DETACH");
  77. exit(0);
  78. }
  79. }
  80. static char const * const signames[] = {
  81. [SIGSEGV] = "SIGSEGV",
  82. [SIGBUS] = "SIBGUS",
  83. [SIGTRAP] = "SIGTRAP",
  84. [SIGILL] = "SIGILL",
  85. };
  86. static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
  87. {
  88. ucontext_t *ctx = ctx_void;
  89. printf("\tGot SIGTRAP with RIP=%lx, EFLAGS.RF=%d\n",
  90. (unsigned long)ctx->uc_mcontext.gregs[REG_IP],
  91. !!(ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_RF));
  92. }
  93. static void handle_and_return(int sig, siginfo_t *si, void *ctx_void)
  94. {
  95. ucontext_t *ctx = ctx_void;
  96. printf("\tGot %s with RIP=%lx\n", signames[sig],
  97. (unsigned long)ctx->uc_mcontext.gregs[REG_IP]);
  98. }
  99. static void handle_and_longjmp(int sig, siginfo_t *si, void *ctx_void)
  100. {
  101. ucontext_t *ctx = ctx_void;
  102. printf("\tGot %s with RIP=%lx\n", signames[sig],
  103. (unsigned long)ctx->uc_mcontext.gregs[REG_IP]);
  104. siglongjmp(jmpbuf, 1);
  105. }
  106. int main()
  107. {
  108. unsigned long nr;
  109. asm volatile ("mov %%ss, %[ss]" : [ss] "=m" (ss));
  110. printf("\tSS = 0x%hx, &SS = 0x%p\n", ss, &ss);
  111. if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == 0)
  112. printf("\tPR_SET_PTRACER_ANY succeeded\n");
  113. printf("\tSet up a watchpoint\n");
  114. sethandler(SIGTRAP, sigtrap, 0);
  115. enable_watchpoint();
  116. printf("[RUN]\tRead from watched memory (should get SIGTRAP)\n");
  117. asm volatile ("mov %[ss], %[tmp]" : [tmp] "=r" (nr) : [ss] "m" (ss));
  118. printf("[RUN]\tMOV SS; INT3\n");
  119. asm volatile ("mov %[ss], %%ss; int3" :: [ss] "m" (ss));
  120. printf("[RUN]\tMOV SS; INT 3\n");
  121. asm volatile ("mov %[ss], %%ss; .byte 0xcd, 0x3" :: [ss] "m" (ss));
  122. printf("[RUN]\tMOV SS; CS CS INT3\n");
  123. asm volatile ("mov %[ss], %%ss; .byte 0x2e, 0x2e; int3" :: [ss] "m" (ss));
  124. printf("[RUN]\tMOV SS; CSx14 INT3\n");
  125. asm volatile ("mov %[ss], %%ss; .fill 14,1,0x2e; int3" :: [ss] "m" (ss));
  126. printf("[RUN]\tMOV SS; INT 4\n");
  127. sethandler(SIGSEGV, handle_and_return, SA_RESETHAND);
  128. asm volatile ("mov %[ss], %%ss; int $4" :: [ss] "m" (ss));
  129. #ifdef __i386__
  130. printf("[RUN]\tMOV SS; INTO\n");
  131. sethandler(SIGSEGV, handle_and_return, SA_RESETHAND);
  132. nr = -1;
  133. asm volatile ("add $1, %[tmp]; mov %[ss], %%ss; into"
  134. : [tmp] "+r" (nr) : [ss] "m" (ss));
  135. #endif
  136. if (sigsetjmp(jmpbuf, 1) == 0) {
  137. printf("[RUN]\tMOV SS; ICEBP\n");
  138. /* Some emulators (e.g. QEMU TCG) don't emulate ICEBP. */
  139. sethandler(SIGILL, handle_and_longjmp, SA_RESETHAND);
  140. asm volatile ("mov %[ss], %%ss; .byte 0xf1" :: [ss] "m" (ss));
  141. }
  142. if (sigsetjmp(jmpbuf, 1) == 0) {
  143. printf("[RUN]\tMOV SS; CLI\n");
  144. sethandler(SIGSEGV, handle_and_longjmp, SA_RESETHAND);
  145. asm volatile ("mov %[ss], %%ss; cli" :: [ss] "m" (ss));
  146. }
  147. if (sigsetjmp(jmpbuf, 1) == 0) {
  148. printf("[RUN]\tMOV SS; #PF\n");
  149. sethandler(SIGSEGV, handle_and_longjmp, SA_RESETHAND);
  150. asm volatile ("mov %[ss], %%ss; mov (-1), %[tmp]"
  151. : [tmp] "=r" (nr) : [ss] "m" (ss));
  152. }
  153. /*
  154. * INT $1: if #DB has DPL=3 and there isn't special handling,
  155. * then the kernel will die.
  156. */
  157. if (sigsetjmp(jmpbuf, 1) == 0) {
  158. printf("[RUN]\tMOV SS; INT 1\n");
  159. sethandler(SIGSEGV, handle_and_longjmp, SA_RESETHAND);
  160. asm volatile ("mov %[ss], %%ss; int $1" :: [ss] "m" (ss));
  161. }
  162. #ifdef __x86_64__
  163. /*
  164. * In principle, we should test 32-bit SYSCALL as well, but
  165. * the calling convention is so unpredictable that it's
  166. * not obviously worth the effort.
  167. */
  168. if (sigsetjmp(jmpbuf, 1) == 0) {
  169. printf("[RUN]\tMOV SS; SYSCALL\n");
  170. sethandler(SIGILL, handle_and_longjmp, SA_RESETHAND);
  171. nr = SYS_getpid;
  172. /*
  173. * Toggle the high bit of RSP to make it noncanonical to
  174. * strengthen this test on non-SMAP systems.
  175. */
  176. asm volatile ("btc $63, %%rsp\n\t"
  177. "mov %[ss], %%ss; syscall\n\t"
  178. "btc $63, %%rsp"
  179. : "+a" (nr) : [ss] "m" (ss)
  180. : "rcx"
  181. #ifdef __x86_64__
  182. , "r11"
  183. #endif
  184. );
  185. }
  186. #endif
  187. printf("[RUN]\tMOV SS; breakpointed NOP\n");
  188. asm volatile ("mov %[ss], %%ss; breakpoint_insn: nop" :: [ss] "m" (ss));
  189. /*
  190. * Invoking SYSENTER directly breaks all the rules. Just handle
  191. * the SIGSEGV.
  192. */
  193. if (sigsetjmp(jmpbuf, 1) == 0) {
  194. printf("[RUN]\tMOV SS; SYSENTER\n");
  195. stack_t stack = {
  196. .ss_sp = malloc(sizeof(char) * SIGSTKSZ),
  197. .ss_size = SIGSTKSZ,
  198. };
  199. if (sigaltstack(&stack, NULL) != 0)
  200. err(1, "sigaltstack");
  201. sethandler(SIGSEGV, handle_and_longjmp, SA_RESETHAND | SA_ONSTACK);
  202. nr = SYS_getpid;
  203. free(stack.ss_sp);
  204. /* Clear EBP first to make sure we segfault cleanly. */
  205. asm volatile ("xorl %%ebp, %%ebp; mov %[ss], %%ss; SYSENTER" : "+a" (nr)
  206. : [ss] "m" (ss) : "flags", "rcx"
  207. #ifdef __x86_64__
  208. , "r11"
  209. #endif
  210. );
  211. /* We're unreachable here. SYSENTER forgets RIP. */
  212. }
  213. if (sigsetjmp(jmpbuf, 1) == 0) {
  214. printf("[RUN]\tMOV SS; INT $0x80\n");
  215. sethandler(SIGSEGV, handle_and_longjmp, SA_RESETHAND);
  216. nr = 20; /* compat getpid */
  217. asm volatile ("mov %[ss], %%ss; int $0x80"
  218. : "+a" (nr) : [ss] "m" (ss)
  219. : "flags"
  220. #ifdef __x86_64__
  221. , "r8", "r9", "r10", "r11"
  222. #endif
  223. );
  224. }
  225. printf("[OK]\tI aten't dead\n");
  226. return 0;
  227. }