entry_from_vm86.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. "vmcode_umip:\n\t"
  87. /* addressing via displacements */
  88. "smsw (2052)\n\t"
  89. "sidt (2054)\n\t"
  90. "sgdt (2060)\n\t"
  91. /* addressing via registers */
  92. "mov $2066, %bx\n\t"
  93. "smsw (%bx)\n\t"
  94. "mov $2068, %bx\n\t"
  95. "sidt (%bx)\n\t"
  96. "mov $2074, %bx\n\t"
  97. "sgdt (%bx)\n\t"
  98. /* register operands, only for smsw */
  99. "smsw %ax\n\t"
  100. "mov %ax, (2080)\n\t"
  101. "int3\n\t"
  102. ".size vmcode, . - vmcode\n\t"
  103. "end_vmcode:\n\t"
  104. ".code32\n\t"
  105. ".popsection"
  106. );
  107. extern unsigned char vmcode[], end_vmcode[];
  108. extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
  109. vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_umip[];
  110. /* Returns false if the test was skipped. */
  111. static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
  112. unsigned int rettype, unsigned int retarg,
  113. const char *text)
  114. {
  115. long ret;
  116. printf("[RUN]\t%s from vm86 mode\n", text);
  117. v86->regs.eip = eip;
  118. ret = vm86(VM86_ENTER, v86);
  119. if (ret == -1 && (errno == ENOSYS || errno == EPERM)) {
  120. printf("[SKIP]\tvm86 %s\n",
  121. errno == ENOSYS ? "not supported" : "not allowed");
  122. return false;
  123. }
  124. if (VM86_TYPE(ret) == VM86_INTx) {
  125. char trapname[32];
  126. int trapno = VM86_ARG(ret);
  127. if (trapno == 13)
  128. strcpy(trapname, "GP");
  129. else if (trapno == 5)
  130. strcpy(trapname, "BR");
  131. else if (trapno == 14)
  132. strcpy(trapname, "PF");
  133. else
  134. sprintf(trapname, "%d", trapno);
  135. printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
  136. } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
  137. printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
  138. } else if (VM86_TYPE(ret) == VM86_TRAP) {
  139. printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
  140. VM86_ARG(ret));
  141. } else if (VM86_TYPE(ret) == VM86_SIGNAL) {
  142. printf("[INFO]\tExited vm86 mode due to a signal\n");
  143. } else if (VM86_TYPE(ret) == VM86_STI) {
  144. printf("[INFO]\tExited vm86 mode due to STI\n");
  145. } else {
  146. printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
  147. VM86_TYPE(ret), VM86_ARG(ret));
  148. }
  149. if (rettype == -1 ||
  150. (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
  151. printf("[OK]\tReturned correctly\n");
  152. } else {
  153. printf("[FAIL]\tIncorrect return reason\n");
  154. nerrs++;
  155. }
  156. return true;
  157. }
  158. void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem)
  159. {
  160. struct table_desc {
  161. unsigned short limit;
  162. unsigned long base;
  163. } __attribute__((packed));
  164. /* Initialize variables with arbitrary values */
  165. struct table_desc gdt1 = { .base = 0x3c3c3c3c, .limit = 0x9999 };
  166. struct table_desc gdt2 = { .base = 0x1a1a1a1a, .limit = 0xaeae };
  167. struct table_desc idt1 = { .base = 0x7b7b7b7b, .limit = 0xf1f1 };
  168. struct table_desc idt2 = { .base = 0x89898989, .limit = 0x1313 };
  169. unsigned short msw1 = 0x1414, msw2 = 0x2525, msw3 = 3737;
  170. /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */
  171. do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests");
  172. /* Results from displacement-only addressing */
  173. msw1 = *(unsigned short *)(test_mem + 2052);
  174. memcpy(&idt1, test_mem + 2054, sizeof(idt1));
  175. memcpy(&gdt1, test_mem + 2060, sizeof(gdt1));
  176. /* Results from register-indirect addressing */
  177. msw2 = *(unsigned short *)(test_mem + 2066);
  178. memcpy(&idt2, test_mem + 2068, sizeof(idt2));
  179. memcpy(&gdt2, test_mem + 2074, sizeof(gdt2));
  180. /* Results when using register operands */
  181. msw3 = *(unsigned short *)(test_mem + 2080);
  182. printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1);
  183. printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n",
  184. idt1.limit, idt1.base);
  185. printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n",
  186. gdt1.limit, gdt1.base);
  187. if (msw1 != msw2 || msw1 != msw3)
  188. printf("[FAIL]\tAll the results of SMSW should be the same.\n");
  189. else
  190. printf("[PASS]\tAll the results from SMSW are identical.\n");
  191. if (memcmp(&gdt1, &gdt2, sizeof(gdt1)))
  192. printf("[FAIL]\tAll the results of SGDT should be the same.\n");
  193. else
  194. printf("[PASS]\tAll the results from SGDT are identical.\n");
  195. if (memcmp(&idt1, &idt2, sizeof(idt1)))
  196. printf("[FAIL]\tAll the results of SIDT should be the same.\n");
  197. else
  198. printf("[PASS]\tAll the results from SIDT are identical.\n");
  199. }
  200. int main(void)
  201. {
  202. struct vm86plus_struct v86;
  203. unsigned char *addr = mmap((void *)load_addr, 4096,
  204. PROT_READ | PROT_WRITE | PROT_EXEC,
  205. MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
  206. if (addr != (unsigned char *)load_addr)
  207. err(1, "mmap");
  208. memcpy(addr, vmcode, end_vmcode - vmcode);
  209. addr[2048] = 2;
  210. addr[2050] = 3;
  211. memset(&v86, 0, sizeof(v86));
  212. v86.regs.cs = load_addr / 16;
  213. v86.regs.ss = load_addr / 16;
  214. v86.regs.ds = load_addr / 16;
  215. v86.regs.es = load_addr / 16;
  216. assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */
  217. /* #BR -- should deliver SIG??? */
  218. do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR");
  219. /*
  220. * SYSENTER -- should cause #GP or #UD depending on CPU.
  221. * Expected return type -1 means that we shouldn't validate
  222. * the vm86 return value. This will avoid problems on non-SEP
  223. * CPUs.
  224. */
  225. sethandler(SIGILL, sighandler, 0);
  226. do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER");
  227. clearhandler(SIGILL);
  228. /*
  229. * SYSCALL would be a disaster in VM86 mode. Fortunately,
  230. * there is no kernel that both enables SYSCALL and sets
  231. * EFER.SCE, so it's #UD on all systems. But vm86 is
  232. * buggy (or has a "feature"), so the SIGILL will actually
  233. * be delivered.
  234. */
  235. sethandler(SIGILL, sighandler, 0);
  236. do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL");
  237. clearhandler(SIGILL);
  238. /* STI with VIP set */
  239. v86.regs.eflags |= X86_EFLAGS_VIP;
  240. v86.regs.eflags &= ~X86_EFLAGS_IF;
  241. do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set");
  242. /* INT3 -- should cause #BP */
  243. do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
  244. /* INT80 -- should exit with "INTx 0x80" */
  245. v86.regs.eax = (unsigned int)-1;
  246. do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
  247. /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */
  248. do_umip_tests(&v86, addr);
  249. /* Execute a null pointer */
  250. v86.regs.cs = 0;
  251. v86.regs.ss = 0;
  252. sethandler(SIGSEGV, sighandler, 0);
  253. got_signal = 0;
  254. if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
  255. !got_signal) {
  256. printf("[FAIL]\tDid not receive SIGSEGV\n");
  257. nerrs++;
  258. }
  259. clearhandler(SIGSEGV);
  260. /* Make sure nothing explodes if we fork. */
  261. if (fork() > 0)
  262. return 0;
  263. return (nerrs == 0 ? 0 : 1);
  264. }