entry_from_vm86.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include "helpers.h"
  25. static unsigned long load_addr = 0x10000;
  26. static int nerrs = 0;
  27. static sig_atomic_t got_signal;
  28. static void sighandler(int sig, siginfo_t *info, void *ctx_void)
  29. {
  30. ucontext_t *ctx = (ucontext_t*)ctx_void;
  31. if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM ||
  32. (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) {
  33. printf("[FAIL]\tSignal frame should not reflect vm86 mode\n");
  34. nerrs++;
  35. }
  36. const char *signame;
  37. if (sig == SIGSEGV)
  38. signame = "SIGSEGV";
  39. else if (sig == SIGILL)
  40. signame = "SIGILL";
  41. else
  42. signame = "unexpected signal";
  43. printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame,
  44. (unsigned long)ctx->uc_mcontext.gregs[REG_EFL],
  45. (unsigned short)ctx->uc_mcontext.gregs[REG_CS]);
  46. got_signal = 1;
  47. }
  48. asm (
  49. ".pushsection .rodata\n\t"
  50. ".type vmcode_bound, @object\n\t"
  51. "vmcode:\n\t"
  52. "vmcode_bound:\n\t"
  53. ".code16\n\t"
  54. "bound %ax, (2048)\n\t"
  55. "int3\n\t"
  56. "vmcode_sysenter:\n\t"
  57. "sysenter\n\t"
  58. "vmcode_syscall:\n\t"
  59. "syscall\n\t"
  60. "vmcode_sti:\n\t"
  61. "sti\n\t"
  62. "vmcode_int3:\n\t"
  63. "int3\n\t"
  64. "vmcode_int80:\n\t"
  65. "int $0x80\n\t"
  66. "vmcode_popf_hlt:\n\t"
  67. "push %ax\n\t"
  68. "popf\n\t"
  69. "hlt\n\t"
  70. "vmcode_umip:\n\t"
  71. /* addressing via displacements */
  72. "smsw (2052)\n\t"
  73. "sidt (2054)\n\t"
  74. "sgdt (2060)\n\t"
  75. /* addressing via registers */
  76. "mov $2066, %bx\n\t"
  77. "smsw (%bx)\n\t"
  78. "mov $2068, %bx\n\t"
  79. "sidt (%bx)\n\t"
  80. "mov $2074, %bx\n\t"
  81. "sgdt (%bx)\n\t"
  82. /* register operands, only for smsw */
  83. "smsw %ax\n\t"
  84. "mov %ax, (2080)\n\t"
  85. "int3\n\t"
  86. "vmcode_umip_str:\n\t"
  87. "str %eax\n\t"
  88. "vmcode_umip_sldt:\n\t"
  89. "sldt %eax\n\t"
  90. "int3\n\t"
  91. ".size vmcode, . - vmcode\n\t"
  92. "end_vmcode:\n\t"
  93. ".code32\n\t"
  94. ".popsection"
  95. );
  96. extern unsigned char vmcode[], end_vmcode[];
  97. extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
  98. vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_popf_hlt[],
  99. vmcode_umip[], vmcode_umip_str[], vmcode_umip_sldt[];
  100. /* Returns false if the test was skipped. */
  101. static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
  102. unsigned int rettype, unsigned int retarg,
  103. const char *text)
  104. {
  105. long ret;
  106. printf("[RUN]\t%s from vm86 mode\n", text);
  107. v86->regs.eip = eip;
  108. ret = vm86(VM86_ENTER, v86);
  109. if (ret == -1 && (errno == ENOSYS || errno == EPERM)) {
  110. printf("[SKIP]\tvm86 %s\n",
  111. errno == ENOSYS ? "not supported" : "not allowed");
  112. return false;
  113. }
  114. if (VM86_TYPE(ret) == VM86_INTx) {
  115. char trapname[32];
  116. int trapno = VM86_ARG(ret);
  117. if (trapno == 13)
  118. strcpy(trapname, "GP");
  119. else if (trapno == 5)
  120. strcpy(trapname, "BR");
  121. else if (trapno == 14)
  122. strcpy(trapname, "PF");
  123. else
  124. sprintf(trapname, "%d", trapno);
  125. printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
  126. } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
  127. printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
  128. } else if (VM86_TYPE(ret) == VM86_TRAP) {
  129. printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
  130. VM86_ARG(ret));
  131. } else if (VM86_TYPE(ret) == VM86_SIGNAL) {
  132. printf("[INFO]\tExited vm86 mode due to a signal\n");
  133. } else if (VM86_TYPE(ret) == VM86_STI) {
  134. printf("[INFO]\tExited vm86 mode due to STI\n");
  135. } else {
  136. printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
  137. VM86_TYPE(ret), VM86_ARG(ret));
  138. }
  139. if (rettype == -1 ||
  140. (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
  141. printf("[OK]\tReturned correctly\n");
  142. } else {
  143. printf("[FAIL]\tIncorrect return reason (started at eip = 0x%lx, ended at eip = 0x%lx)\n", eip, v86->regs.eip);
  144. nerrs++;
  145. }
  146. return true;
  147. }
  148. void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem)
  149. {
  150. struct table_desc {
  151. unsigned short limit;
  152. unsigned long base;
  153. } __attribute__((packed));
  154. /* Initialize variables with arbitrary values */
  155. struct table_desc gdt1 = { .base = 0x3c3c3c3c, .limit = 0x9999 };
  156. struct table_desc gdt2 = { .base = 0x1a1a1a1a, .limit = 0xaeae };
  157. struct table_desc idt1 = { .base = 0x7b7b7b7b, .limit = 0xf1f1 };
  158. struct table_desc idt2 = { .base = 0x89898989, .limit = 0x1313 };
  159. unsigned short msw1 = 0x1414, msw2 = 0x2525, msw3 = 3737;
  160. /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */
  161. do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests");
  162. /* Results from displacement-only addressing */
  163. msw1 = *(unsigned short *)(test_mem + 2052);
  164. memcpy(&idt1, test_mem + 2054, sizeof(idt1));
  165. memcpy(&gdt1, test_mem + 2060, sizeof(gdt1));
  166. /* Results from register-indirect addressing */
  167. msw2 = *(unsigned short *)(test_mem + 2066);
  168. memcpy(&idt2, test_mem + 2068, sizeof(idt2));
  169. memcpy(&gdt2, test_mem + 2074, sizeof(gdt2));
  170. /* Results when using register operands */
  171. msw3 = *(unsigned short *)(test_mem + 2080);
  172. printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1);
  173. printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n",
  174. idt1.limit, idt1.base);
  175. printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n",
  176. gdt1.limit, gdt1.base);
  177. if (msw1 != msw2 || msw1 != msw3)
  178. printf("[FAIL]\tAll the results of SMSW should be the same.\n");
  179. else
  180. printf("[PASS]\tAll the results from SMSW are identical.\n");
  181. if (memcmp(&gdt1, &gdt2, sizeof(gdt1)))
  182. printf("[FAIL]\tAll the results of SGDT should be the same.\n");
  183. else
  184. printf("[PASS]\tAll the results from SGDT are identical.\n");
  185. if (memcmp(&idt1, &idt2, sizeof(idt1)))
  186. printf("[FAIL]\tAll the results of SIDT should be the same.\n");
  187. else
  188. printf("[PASS]\tAll the results from SIDT are identical.\n");
  189. sethandler(SIGILL, sighandler, 0);
  190. do_test(vm86, vmcode_umip_str - vmcode, VM86_SIGNAL, 0,
  191. "STR instruction");
  192. clearhandler(SIGILL);
  193. sethandler(SIGILL, sighandler, 0);
  194. do_test(vm86, vmcode_umip_sldt - vmcode, VM86_SIGNAL, 0,
  195. "SLDT instruction");
  196. clearhandler(SIGILL);
  197. }
  198. int main(void)
  199. {
  200. struct vm86plus_struct v86;
  201. unsigned char *addr = mmap((void *)load_addr, 4096,
  202. PROT_READ | PROT_WRITE | PROT_EXEC,
  203. MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
  204. if (addr != (unsigned char *)load_addr)
  205. err(1, "mmap");
  206. memcpy(addr, vmcode, end_vmcode - vmcode);
  207. addr[2048] = 2;
  208. addr[2050] = 3;
  209. memset(&v86, 0, sizeof(v86));
  210. v86.regs.cs = load_addr / 16;
  211. v86.regs.ss = load_addr / 16;
  212. v86.regs.ds = load_addr / 16;
  213. v86.regs.es = load_addr / 16;
  214. /* Use the end of the page as our stack. */
  215. v86.regs.esp = 4096;
  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. /* POPF with VIP set but IF clear: should not trap */
  243. v86.regs.eflags = X86_EFLAGS_VIP;
  244. v86.regs.eax = 0;
  245. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP set and IF clear");
  246. /* POPF with VIP set and IF set: should trap */
  247. v86.regs.eflags = X86_EFLAGS_VIP;
  248. v86.regs.eax = X86_EFLAGS_IF;
  249. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_STI, 0, "POPF with VIP and IF set");
  250. /* POPF with VIP clear and IF set: should not trap */
  251. v86.regs.eflags = 0;
  252. v86.regs.eax = X86_EFLAGS_IF;
  253. do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP clear and IF set");
  254. v86.regs.eflags = 0;
  255. /* INT3 -- should cause #BP */
  256. do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
  257. /* INT80 -- should exit with "INTx 0x80" */
  258. v86.regs.eax = (unsigned int)-1;
  259. do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
  260. /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */
  261. do_umip_tests(&v86, addr);
  262. /* Execute a null pointer */
  263. v86.regs.cs = 0;
  264. v86.regs.ss = 0;
  265. sethandler(SIGSEGV, sighandler, 0);
  266. got_signal = 0;
  267. if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
  268. !got_signal) {
  269. printf("[FAIL]\tDid not receive SIGSEGV\n");
  270. nerrs++;
  271. }
  272. clearhandler(SIGSEGV);
  273. /* Make sure nothing explodes if we fork. */
  274. if (fork() == 0)
  275. return 0;
  276. return (nerrs == 0 ? 0 : 1);
  277. }