entry_from_vm86.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * entry_from_vm86.c - tests kernel entries from vm86 mode
  3. * Copyright (c) 2014-2015 Andrew Lutomirski
  4. *
  5. * This exercises a few paths that need to special-case vm86 mode.
  6. *
  7. * GPL v2.
  8. */
  9. #define _GNU_SOURCE
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <sys/syscall.h>
  13. #include <sys/signal.h>
  14. #include <sys/ucontext.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <sys/mman.h>
  20. #include <err.h>
  21. #include <stddef.h>
  22. #include <stdbool.h>
  23. #include <errno.h>
  24. #include <sys/vm86.h>
  25. static unsigned long load_addr = 0x10000;
  26. static int nerrs = 0;
  27. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  28. int flags)
  29. {
  30. struct sigaction sa;
  31. memset(&sa, 0, sizeof(sa));
  32. sa.sa_sigaction = handler;
  33. sa.sa_flags = SA_SIGINFO | flags;
  34. sigemptyset(&sa.sa_mask);
  35. if (sigaction(sig, &sa, 0))
  36. err(1, "sigaction");
  37. }
  38. static void clearhandler(int sig)
  39. {
  40. struct sigaction sa;
  41. memset(&sa, 0, sizeof(sa));
  42. sa.sa_handler = SIG_DFL;
  43. sigemptyset(&sa.sa_mask);
  44. if (sigaction(sig, &sa, 0))
  45. err(1, "sigaction");
  46. }
  47. static sig_atomic_t got_signal;
  48. static void sighandler(int sig, siginfo_t *info, void *ctx_void)
  49. {
  50. ucontext_t *ctx = (ucontext_t*)ctx_void;
  51. if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM ||
  52. (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) {
  53. printf("[FAIL]\tSignal frame should not reflect vm86 mode\n");
  54. nerrs++;
  55. }
  56. const char *signame;
  57. if (sig == SIGSEGV)
  58. signame = "SIGSEGV";
  59. else if (sig == SIGILL)
  60. signame = "SIGILL";
  61. else
  62. signame = "unexpected signal";
  63. printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame,
  64. (unsigned long)ctx->uc_mcontext.gregs[REG_EFL],
  65. (unsigned short)ctx->uc_mcontext.gregs[REG_CS]);
  66. got_signal = 1;
  67. }
  68. asm (
  69. ".pushsection .rodata\n\t"
  70. ".type vmcode_bound, @object\n\t"
  71. "vmcode:\n\t"
  72. "vmcode_bound:\n\t"
  73. ".code16\n\t"
  74. "bound %ax, (2048)\n\t"
  75. "int3\n\t"
  76. "vmcode_sysenter:\n\t"
  77. "sysenter\n\t"
  78. "vmcode_syscall:\n\t"
  79. "syscall\n\t"
  80. "vmcode_sti:\n\t"
  81. "sti\n\t"
  82. "vmcode_int3:\n\t"
  83. "int3\n\t"
  84. "vmcode_int80:\n\t"
  85. "int $0x80\n\t"
  86. ".size vmcode, . - vmcode\n\t"
  87. "end_vmcode:\n\t"
  88. ".code32\n\t"
  89. ".popsection"
  90. );
  91. extern unsigned char vmcode[], end_vmcode[];
  92. extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
  93. vmcode_sti[], vmcode_int3[], vmcode_int80[];
  94. /* Returns false if the test was skipped. */
  95. static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
  96. unsigned int rettype, unsigned int retarg,
  97. const char *text)
  98. {
  99. long ret;
  100. printf("[RUN]\t%s from vm86 mode\n", text);
  101. v86->regs.eip = eip;
  102. ret = vm86(VM86_ENTER, v86);
  103. if (ret == -1 && errno == ENOSYS) {
  104. printf("[SKIP]\tvm86 not supported\n");
  105. return false;
  106. }
  107. if (VM86_TYPE(ret) == VM86_INTx) {
  108. char trapname[32];
  109. int trapno = VM86_ARG(ret);
  110. if (trapno == 13)
  111. strcpy(trapname, "GP");
  112. else if (trapno == 5)
  113. strcpy(trapname, "BR");
  114. else if (trapno == 14)
  115. strcpy(trapname, "PF");
  116. else
  117. sprintf(trapname, "%d", trapno);
  118. printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
  119. } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
  120. printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
  121. } else if (VM86_TYPE(ret) == VM86_TRAP) {
  122. printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
  123. VM86_ARG(ret));
  124. } else if (VM86_TYPE(ret) == VM86_SIGNAL) {
  125. printf("[INFO]\tExited vm86 mode due to a signal\n");
  126. } else if (VM86_TYPE(ret) == VM86_STI) {
  127. printf("[INFO]\tExited vm86 mode due to STI\n");
  128. } else {
  129. printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
  130. VM86_TYPE(ret), VM86_ARG(ret));
  131. }
  132. if (rettype == -1 ||
  133. (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
  134. printf("[OK]\tReturned correctly\n");
  135. } else {
  136. printf("[FAIL]\tIncorrect return reason\n");
  137. nerrs++;
  138. }
  139. return true;
  140. }
  141. int main(void)
  142. {
  143. struct vm86plus_struct v86;
  144. unsigned char *addr = mmap((void *)load_addr, 4096,
  145. PROT_READ | PROT_WRITE | PROT_EXEC,
  146. MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
  147. if (addr != (unsigned char *)load_addr)
  148. err(1, "mmap");
  149. memcpy(addr, vmcode, end_vmcode - vmcode);
  150. addr[2048] = 2;
  151. addr[2050] = 3;
  152. memset(&v86, 0, sizeof(v86));
  153. v86.regs.cs = load_addr / 16;
  154. v86.regs.ss = load_addr / 16;
  155. v86.regs.ds = load_addr / 16;
  156. v86.regs.es = load_addr / 16;
  157. assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */
  158. /* #BR -- should deliver SIG??? */
  159. do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR");
  160. /*
  161. * SYSENTER -- should cause #GP or #UD depending on CPU.
  162. * Expected return type -1 means that we shouldn't validate
  163. * the vm86 return value. This will avoid problems on non-SEP
  164. * CPUs.
  165. */
  166. sethandler(SIGILL, sighandler, 0);
  167. do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER");
  168. clearhandler(SIGILL);
  169. /*
  170. * SYSCALL would be a disaster in VM86 mode. Fortunately,
  171. * there is no kernel that both enables SYSCALL and sets
  172. * EFER.SCE, so it's #UD on all systems. But vm86 is
  173. * buggy (or has a "feature"), so the SIGILL will actually
  174. * be delivered.
  175. */
  176. sethandler(SIGILL, sighandler, 0);
  177. do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL");
  178. clearhandler(SIGILL);
  179. /* STI with VIP set */
  180. v86.regs.eflags |= X86_EFLAGS_VIP;
  181. v86.regs.eflags &= ~X86_EFLAGS_IF;
  182. do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set");
  183. /* INT3 -- should cause #BP */
  184. do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
  185. /* INT80 -- should exit with "INTx 0x80" */
  186. v86.regs.eax = (unsigned int)-1;
  187. do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
  188. /* Execute a null pointer */
  189. v86.regs.cs = 0;
  190. v86.regs.ss = 0;
  191. sethandler(SIGSEGV, sighandler, 0);
  192. got_signal = 0;
  193. if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
  194. !got_signal) {
  195. printf("[FAIL]\tDid not receive SIGSEGV\n");
  196. nerrs++;
  197. }
  198. clearhandler(SIGSEGV);
  199. return (nerrs == 0 ? 0 : 1);
  200. }