entry_from_vm86.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * entry_from_vm86.c - tests kernel entries from vm86 mode
  4. * Copyright (c) 2014-2015 Andrew Lutomirski
  5. *
  6. * This exercises a few paths that need to special-case vm86 mode.
  7. */
  8. #define _GNU_SOURCE
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <sys/syscall.h>
  12. #include <sys/signal.h>
  13. #include <sys/ucontext.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <inttypes.h>
  18. #include <sys/mman.h>
  19. #include <err.h>
  20. #include <stddef.h>
  21. #include <stdbool.h>
  22. #include <errno.h>
  23. #include <sys/vm86.h>
  24. static unsigned long load_addr = 0x10000;
  25. static int nerrs = 0;
  26. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  27. int flags)
  28. {
  29. struct sigaction sa;
  30. memset(&sa, 0, sizeof(sa));
  31. sa.sa_sigaction = handler;
  32. sa.sa_flags = SA_SIGINFO | flags;
  33. sigemptyset(&sa.sa_mask);
  34. if (sigaction(sig, &sa, 0))
  35. err(1, "sigaction");
  36. }
  37. static void clearhandler(int sig)
  38. {
  39. struct sigaction sa;
  40. memset(&sa, 0, sizeof(sa));
  41. sa.sa_handler = SIG_DFL;
  42. sigemptyset(&sa.sa_mask);
  43. if (sigaction(sig, &sa, 0))
  44. err(1, "sigaction");
  45. }
  46. static sig_atomic_t got_signal;
  47. static void sighandler(int sig, siginfo_t *info, void *ctx_void)
  48. {
  49. ucontext_t *ctx = (ucontext_t*)ctx_void;
  50. if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM ||
  51. (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) {
  52. printf("[FAIL]\tSignal frame should not reflect vm86 mode\n");
  53. nerrs++;
  54. }
  55. const char *signame;
  56. if (sig == SIGSEGV)
  57. signame = "SIGSEGV";
  58. else if (sig == SIGILL)
  59. signame = "SIGILL";
  60. else
  61. signame = "unexpected signal";
  62. printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame,
  63. (unsigned long)ctx->uc_mcontext.gregs[REG_EFL],
  64. (unsigned short)ctx->uc_mcontext.gregs[REG_CS]);
  65. got_signal = 1;
  66. }
  67. asm (
  68. ".pushsection .rodata\n\t"
  69. ".type vmcode_bound, @object\n\t"
  70. "vmcode:\n\t"
  71. "vmcode_bound:\n\t"
  72. ".code16\n\t"
  73. "bound %ax, (2048)\n\t"
  74. "int3\n\t"
  75. "vmcode_sysenter:\n\t"
  76. "sysenter\n\t"
  77. "vmcode_syscall:\n\t"
  78. "syscall\n\t"
  79. "vmcode_sti:\n\t"
  80. "sti\n\t"
  81. "vmcode_int3:\n\t"
  82. "int3\n\t"
  83. "vmcode_int80:\n\t"
  84. "int $0x80\n\t"
  85. "vmcode_popf_hlt:\n\t"
  86. "push %ax\n\t"
  87. "popf\n\t"
  88. "hlt\n\t"
  89. "vmcode_umip:\n\t"
  90. /* addressing via displacements */
  91. "smsw (2052)\n\t"
  92. "sidt (2054)\n\t"
  93. "sgdt (2060)\n\t"
  94. /* addressing via registers */
  95. "mov $2066, %bx\n\t"
  96. "smsw (%bx)\n\t"
  97. "mov $2068, %bx\n\t"
  98. "sidt (%bx)\n\t"
  99. "mov $2074, %bx\n\t"
  100. "sgdt (%bx)\n\t"
  101. /* register operands, only for smsw */
  102. "smsw %ax\n\t"
  103. "mov %ax, (2080)\n\t"
  104. "int3\n\t"
  105. "vmcode_umip_str:\n\t"
  106. "str %eax\n\t"
  107. "vmcode_umip_sldt:\n\t"
  108. "sldt %eax\n\t"
  109. "int3\n\t"
  110. ".size vmcode, . - vmcode\n\t"
  111. "end_vmcode:\n\t"
  112. ".code32\n\t"
  113. ".popsection"
  114. );
  115. extern unsigned char vmcode[], end_vmcode[];
  116. extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
  117. vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_popf_hlt[],
  118. vmcode_umip[], vmcode_umip_str[], vmcode_umip_sldt[];
  119. /* Returns false if the test was skipped. */
  120. static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
  121. unsigned int rettype, unsigned int retarg,
  122. const char *text)
  123. {
  124. long ret;
  125. printf("[RUN]\t%s from vm86 mode\n", text);
  126. v86->regs.eip = eip;
  127. ret = vm86(VM86_ENTER, v86);
  128. if (ret == -1 && (errno == ENOSYS || errno == EPERM)) {
  129. printf("[SKIP]\tvm86 %s\n",
  130. errno == ENOSYS ? "not supported" : "not allowed");
  131. return false;
  132. }
  133. if (VM86_TYPE(ret) == VM86_INTx) {
  134. char trapname[32];
  135. int trapno = VM86_ARG(ret);
  136. if (trapno == 13)
  137. strcpy(trapname, "GP");
  138. else if (trapno == 5)
  139. strcpy(trapname, "BR");
  140. else if (trapno == 14)
  141. strcpy(trapname, "PF");
  142. else
  143. sprintf(trapname, "%d", trapno);
  144. printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
  145. } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
  146. printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
  147. } else if (VM86_TYPE(ret) == VM86_TRAP) {
  148. printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
  149. VM86_ARG(ret));
  150. } else if (VM86_TYPE(ret) == VM86_SIGNAL) {
  151. printf("[INFO]\tExited vm86 mode due to a signal\n");
  152. } else if (VM86_TYPE(ret) == VM86_STI) {
  153. printf("[INFO]\tExited vm86 mode due to STI\n");
  154. } else {
  155. printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
  156. VM86_TYPE(ret), VM86_ARG(ret));
  157. }
  158. if (rettype == -1 ||
  159. (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
  160. printf("[OK]\tReturned correctly\n");
  161. } else {
  162. printf("[FAIL]\tIncorrect return reason (started at eip = 0x%lx, ended at eip = 0x%lx)\n", eip, v86->regs.eip);
  163. nerrs++;
  164. }
  165. return true;
  166. }
  167. void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem)
  168. {
  169. struct table_desc {
  170. unsigned short limit;
  171. unsigned long base;
  172. } __attribute__((packed));
  173. /* Initialize variables with arbitrary values */
  174. struct table_desc gdt1 = { .base = 0x3c3c3c3c, .limit = 0x9999 };
  175. struct table_desc gdt2 = { .base = 0x1a1a1a1a, .limit = 0xaeae };
  176. struct table_desc idt1 = { .base = 0x7b7b7b7b, .limit = 0xf1f1 };
  177. struct table_desc idt2 = { .base = 0x89898989, .limit = 0x1313 };
  178. unsigned short msw1 = 0x1414, msw2 = 0x2525, msw3 = 3737;
  179. /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */
  180. do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests");
  181. /* Results from displacement-only addressing */
  182. msw1 = *(unsigned short *)(test_mem + 2052);
  183. memcpy(&idt1, test_mem + 2054, sizeof(idt1));
  184. memcpy(&gdt1, test_mem + 2060, sizeof(gdt1));
  185. /* Results from register-indirect addressing */
  186. msw2 = *(unsigned short *)(test_mem + 2066);
  187. memcpy(&idt2, test_mem + 2068, sizeof(idt2));
  188. memcpy(&gdt2, test_mem + 2074, sizeof(gdt2));
  189. /* Results when using register operands */
  190. msw3 = *(unsigned short *)(test_mem + 2080);
  191. printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1);
  192. printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n",
  193. idt1.limit, idt1.base);
  194. printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n",
  195. gdt1.limit, gdt1.base);
  196. if (msw1 != msw2 || msw1 != msw3)
  197. printf("[FAIL]\tAll the results of SMSW should be the same.\n");
  198. else
  199. printf("[PASS]\tAll the results from SMSW are identical.\n");
  200. if (memcmp(&gdt1, &gdt2, sizeof(gdt1)))
  201. printf("[FAIL]\tAll the results of SGDT should be the same.\n");
  202. else
  203. printf("[PASS]\tAll the results from SGDT are identical.\n");
  204. if (memcmp(&idt1, &idt2, sizeof(idt1)))
  205. printf("[FAIL]\tAll the results of SIDT should be the same.\n");
  206. else
  207. printf("[PASS]\tAll the results from SIDT are identical.\n");
  208. sethandler(SIGILL, sighandler, 0);
  209. do_test(vm86, vmcode_umip_str - vmcode, VM86_SIGNAL, 0,
  210. "STR instruction");
  211. clearhandler(SIGILL);
  212. sethandler(SIGILL, sighandler, 0);
  213. do_test(vm86, vmcode_umip_sldt - vmcode, VM86_SIGNAL, 0,
  214. "SLDT instruction");
  215. clearhandler(SIGILL);
  216. }
  217. int main(void)
  218. {
  219. struct vm86plus_struct v86;
  220. unsigned char *addr = mmap((void *)load_addr, 4096,
  221. PROT_READ | PROT_WRITE | PROT_EXEC,
  222. MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
  223. if (addr != (unsigned char *)load_addr)
  224. err(1, "mmap");
  225. memcpy(addr, vmcode, end_vmcode - vmcode);
  226. addr[2048] = 2;
  227. addr[2050] = 3;
  228. memset(&v86, 0, sizeof(v86));
  229. v86.regs.cs = load_addr / 16;
  230. v86.regs.ss = load_addr / 16;
  231. v86.regs.ds = load_addr / 16;
  232. v86.regs.es = load_addr / 16;
  233. /* Use the end of the page as our stack. */
  234. v86.regs.esp = 4096;
  235. assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */
  236. /* #BR -- should deliver SIG??? */
  237. do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR");
  238. /*
  239. * SYSENTER -- should cause #GP or #UD depending on CPU.
  240. * Expected return type -1 means that we shouldn't validate
  241. * the vm86 return value. This will avoid problems on non-SEP
  242. * CPUs.
  243. */
  244. sethandler(SIGILL, sighandler, 0);
  245. do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER");
  246. clearhandler(SIGILL);
  247. /*
  248. * SYSCALL would be a disaster in VM86 mode. Fortunately,
  249. * there is no kernel that both enables SYSCALL and sets
  250. * EFER.SCE, so it's #UD on all systems. But vm86 is
  251. * buggy (or has a "feature"), so the SIGILL will actually
  252. * be delivered.
  253. */
  254. sethandler(SIGILL, sighandler, 0);
  255. do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL");
  256. clearhandler(SIGILL);
  257. /* STI with VIP set */
  258. v86.regs.eflags |= X86_EFLAGS_VIP;
  259. v86.regs.eflags &= ~X86_EFLAGS_IF;
  260. do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set");
  261. /* POPF with VIP set but IF clear: should not trap */
  262. v86.regs.eflags = X86_EFLAGS_VIP;
  263. v86.regs.eax = 0;
  264. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP set and IF clear");
  265. /* POPF with VIP set and IF set: should trap */
  266. v86.regs.eflags = X86_EFLAGS_VIP;
  267. v86.regs.eax = X86_EFLAGS_IF;
  268. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_STI, 0, "POPF with VIP and IF set");
  269. /* POPF with VIP clear and IF set: should not trap */
  270. v86.regs.eflags = 0;
  271. v86.regs.eax = X86_EFLAGS_IF;
  272. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP clear and IF set");
  273. v86.regs.eflags = 0;
  274. /* INT3 -- should cause #BP */
  275. do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
  276. /* INT80 -- should exit with "INTx 0x80" */
  277. v86.regs.eax = (unsigned int)-1;
  278. do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
  279. /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */
  280. do_umip_tests(&v86, addr);
  281. /* Execute a null pointer */
  282. v86.regs.cs = 0;
  283. v86.regs.ss = 0;
  284. sethandler(SIGSEGV, sighandler, 0);
  285. got_signal = 0;
  286. if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
  287. !got_signal) {
  288. printf("[FAIL]\tDid not receive SIGSEGV\n");
  289. nerrs++;
  290. }
  291. clearhandler(SIGSEGV);
  292. /* Make sure nothing explodes if we fork. */
  293. if (fork() == 0)
  294. return 0;
  295. return (nerrs == 0 ? 0 : 1);
  296. }