single_step_syscall.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * single_step_syscall.c - single-steps various x86 syscalls
  3. * Copyright (c) 2014-2015 Andrew Lutomirski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * This is a very simple series of tests that makes system calls with
  15. * the TF flag set. This exercises some nasty kernel code in the
  16. * SYSENTER case: SYSENTER does not clear TF, so SYSENTER with TF set
  17. * immediately issues #DB from CPL 0. This requires special handling in
  18. * the kernel.
  19. */
  20. #define _GNU_SOURCE
  21. #include <sys/time.h>
  22. #include <time.h>
  23. #include <stdlib.h>
  24. #include <sys/syscall.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <inttypes.h>
  29. #include <sys/mman.h>
  30. #include <sys/signal.h>
  31. #include <sys/ucontext.h>
  32. #include <asm/ldt.h>
  33. #include <err.h>
  34. #include <setjmp.h>
  35. #include <stddef.h>
  36. #include <stdbool.h>
  37. #include <sys/ptrace.h>
  38. #include <sys/user.h>
  39. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  40. int flags)
  41. {
  42. struct sigaction sa;
  43. memset(&sa, 0, sizeof(sa));
  44. sa.sa_sigaction = handler;
  45. sa.sa_flags = SA_SIGINFO | flags;
  46. sigemptyset(&sa.sa_mask);
  47. if (sigaction(sig, &sa, 0))
  48. err(1, "sigaction");
  49. }
  50. static volatile sig_atomic_t sig_traps;
  51. #ifdef __x86_64__
  52. # define REG_IP REG_RIP
  53. # define WIDTH "q"
  54. #else
  55. # define REG_IP REG_EIP
  56. # define WIDTH "l"
  57. #endif
  58. static unsigned long get_eflags(void)
  59. {
  60. unsigned long eflags;
  61. asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
  62. return eflags;
  63. }
  64. static void set_eflags(unsigned long eflags)
  65. {
  66. asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
  67. : : "rm" (eflags) : "flags");
  68. }
  69. #define X86_EFLAGS_TF (1UL << 8)
  70. static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
  71. {
  72. ucontext_t *ctx = (ucontext_t*)ctx_void;
  73. if (get_eflags() & X86_EFLAGS_TF) {
  74. set_eflags(get_eflags() & ~X86_EFLAGS_TF);
  75. printf("[WARN]\tSIGTRAP handler had TF set\n");
  76. _exit(1);
  77. }
  78. sig_traps++;
  79. if (sig_traps == 10000 || sig_traps == 10001) {
  80. printf("[WARN]\tHit %d SIGTRAPs with si_addr 0x%lx, ip 0x%lx\n",
  81. (int)sig_traps,
  82. (unsigned long)info->si_addr,
  83. (unsigned long)ctx->uc_mcontext.gregs[REG_IP]);
  84. }
  85. }
  86. static void check_result(void)
  87. {
  88. unsigned long new_eflags = get_eflags();
  89. set_eflags(new_eflags & ~X86_EFLAGS_TF);
  90. if (!sig_traps) {
  91. printf("[FAIL]\tNo SIGTRAP\n");
  92. exit(1);
  93. }
  94. if (!(new_eflags & X86_EFLAGS_TF)) {
  95. printf("[FAIL]\tTF was cleared\n");
  96. exit(1);
  97. }
  98. printf("[OK]\tSurvived with TF set and %d traps\n", (int)sig_traps);
  99. sig_traps = 0;
  100. }
  101. int main()
  102. {
  103. int tmp;
  104. sethandler(SIGTRAP, sigtrap, 0);
  105. printf("[RUN]\tSet TF and check nop\n");
  106. set_eflags(get_eflags() | X86_EFLAGS_TF);
  107. asm volatile ("nop");
  108. check_result();
  109. #ifdef __x86_64__
  110. printf("[RUN]\tSet TF and check syscall-less opportunistic sysret\n");
  111. set_eflags(get_eflags() | X86_EFLAGS_TF);
  112. extern unsigned char post_nop[];
  113. asm volatile ("pushf" WIDTH "\n\t"
  114. "pop" WIDTH " %%r11\n\t"
  115. "nop\n\t"
  116. "post_nop:"
  117. : : "c" (post_nop) : "r11");
  118. check_result();
  119. #endif
  120. printf("[RUN]\tSet TF and check int80\n");
  121. set_eflags(get_eflags() | X86_EFLAGS_TF);
  122. asm volatile ("int $0x80" : "=a" (tmp) : "a" (SYS_getpid));
  123. check_result();
  124. /*
  125. * This test is particularly interesting if fast syscalls use
  126. * SYSENTER: it triggers a nasty design flaw in SYSENTER.
  127. * Specifically, SYSENTER does not clear TF, so either SYSENTER
  128. * or the next instruction traps at CPL0. (Of course, Intel
  129. * mostly forgot to document exactly what happens here.) So we
  130. * get a CPL0 fault with usergs (on 64-bit kernels) and possibly
  131. * no stack. The only sane way the kernel can possibly handle
  132. * it is to clear TF on return from the #DB handler, but this
  133. * happens way too early to set TF in the saved pt_regs, so the
  134. * kernel has to do something clever to avoid losing track of
  135. * the TF bit.
  136. *
  137. * Needless to say, we've had bugs in this area.
  138. */
  139. syscall(SYS_getpid); /* Force symbol binding without TF set. */
  140. printf("[RUN]\tSet TF and check a fast syscall\n");
  141. set_eflags(get_eflags() | X86_EFLAGS_TF);
  142. syscall(SYS_getpid);
  143. check_result();
  144. /* Now make sure that another fast syscall doesn't set TF again. */
  145. printf("[RUN]\tFast syscall with TF cleared\n");
  146. fflush(stdout); /* Force a syscall */
  147. if (get_eflags() & X86_EFLAGS_TF) {
  148. printf("[FAIL]\tTF is now set\n");
  149. exit(1);
  150. }
  151. if (sig_traps) {
  152. printf("[FAIL]\tGot SIGTRAP\n");
  153. exit(1);
  154. }
  155. printf("[OK]\tNothing unexpected happened\n");
  156. return 0;
  157. }