Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * entry_from_vm86.c - tests kernel entries from vm86 mode
0004  * Copyright (c) 2014-2015 Andrew Lutomirski
0005  *
0006  * This exercises a few paths that need to special-case vm86 mode.
0007  */
0008 
0009 #define _GNU_SOURCE
0010 
0011 #include <assert.h>
0012 #include <stdlib.h>
0013 #include <sys/syscall.h>
0014 #include <sys/signal.h>
0015 #include <sys/ucontext.h>
0016 #include <unistd.h>
0017 #include <stdio.h>
0018 #include <string.h>
0019 #include <inttypes.h>
0020 #include <sys/mman.h>
0021 #include <err.h>
0022 #include <stddef.h>
0023 #include <stdbool.h>
0024 #include <errno.h>
0025 #include <sys/vm86.h>
0026 
0027 static unsigned long load_addr = 0x10000;
0028 static int nerrs = 0;
0029 
0030 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
0031                int flags)
0032 {
0033     struct sigaction sa;
0034     memset(&sa, 0, sizeof(sa));
0035     sa.sa_sigaction = handler;
0036     sa.sa_flags = SA_SIGINFO | flags;
0037     sigemptyset(&sa.sa_mask);
0038     if (sigaction(sig, &sa, 0))
0039         err(1, "sigaction");
0040 }
0041 
0042 static void clearhandler(int sig)
0043 {
0044     struct sigaction sa;
0045     memset(&sa, 0, sizeof(sa));
0046     sa.sa_handler = SIG_DFL;
0047     sigemptyset(&sa.sa_mask);
0048     if (sigaction(sig, &sa, 0))
0049         err(1, "sigaction");
0050 }
0051 
0052 static sig_atomic_t got_signal;
0053 
0054 static void sighandler(int sig, siginfo_t *info, void *ctx_void)
0055 {
0056     ucontext_t *ctx = (ucontext_t*)ctx_void;
0057 
0058     if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM ||
0059         (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) {
0060         printf("[FAIL]\tSignal frame should not reflect vm86 mode\n");
0061         nerrs++;
0062     }
0063 
0064     const char *signame;
0065     if (sig == SIGSEGV)
0066         signame = "SIGSEGV";
0067     else if (sig == SIGILL)
0068         signame = "SIGILL";
0069     else
0070         signame = "unexpected signal";
0071 
0072     printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame,
0073            (unsigned long)ctx->uc_mcontext.gregs[REG_EFL],
0074            (unsigned short)ctx->uc_mcontext.gregs[REG_CS]);
0075 
0076     got_signal = 1;
0077 }
0078 
0079 asm (
0080     ".pushsection .rodata\n\t"
0081     ".type vmcode_bound, @object\n\t"
0082     "vmcode:\n\t"
0083     "vmcode_bound:\n\t"
0084     ".code16\n\t"
0085     "bound %ax, (2048)\n\t"
0086     "int3\n\t"
0087     "vmcode_sysenter:\n\t"
0088     "sysenter\n\t"
0089     "vmcode_syscall:\n\t"
0090     "syscall\n\t"
0091     "vmcode_sti:\n\t"
0092     "sti\n\t"
0093     "vmcode_int3:\n\t"
0094     "int3\n\t"
0095     "vmcode_int80:\n\t"
0096     "int $0x80\n\t"
0097     "vmcode_popf_hlt:\n\t"
0098     "push %ax\n\t"
0099     "popf\n\t"
0100     "hlt\n\t"
0101     "vmcode_umip:\n\t"
0102     /* addressing via displacements */
0103     "smsw (2052)\n\t"
0104     "sidt (2054)\n\t"
0105     "sgdt (2060)\n\t"
0106     /* addressing via registers */
0107     "mov $2066, %bx\n\t"
0108     "smsw (%bx)\n\t"
0109     "mov $2068, %bx\n\t"
0110     "sidt (%bx)\n\t"
0111     "mov $2074, %bx\n\t"
0112     "sgdt (%bx)\n\t"
0113     /* register operands, only for smsw */
0114     "smsw %ax\n\t"
0115     "mov %ax, (2080)\n\t"
0116     "int3\n\t"
0117     "vmcode_umip_str:\n\t"
0118     "str %eax\n\t"
0119     "vmcode_umip_sldt:\n\t"
0120     "sldt %eax\n\t"
0121     "int3\n\t"
0122     ".size vmcode, . - vmcode\n\t"
0123     "end_vmcode:\n\t"
0124     ".code32\n\t"
0125     ".popsection"
0126     );
0127 
0128 extern unsigned char vmcode[], end_vmcode[];
0129 extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
0130     vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_popf_hlt[],
0131     vmcode_umip[], vmcode_umip_str[], vmcode_umip_sldt[];
0132 
0133 /* Returns false if the test was skipped. */
0134 static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
0135             unsigned int rettype, unsigned int retarg,
0136             const char *text)
0137 {
0138     long ret;
0139 
0140     printf("[RUN]\t%s from vm86 mode\n", text);
0141     v86->regs.eip = eip;
0142     ret = vm86(VM86_ENTER, v86);
0143 
0144     if (ret == -1 && (errno == ENOSYS || errno == EPERM)) {
0145         printf("[SKIP]\tvm86 %s\n",
0146                errno == ENOSYS ? "not supported" : "not allowed");
0147         return false;
0148     }
0149 
0150     if (VM86_TYPE(ret) == VM86_INTx) {
0151         char trapname[32];
0152         int trapno = VM86_ARG(ret);
0153         if (trapno == 13)
0154             strcpy(trapname, "GP");
0155         else if (trapno == 5)
0156             strcpy(trapname, "BR");
0157         else if (trapno == 14)
0158             strcpy(trapname, "PF");
0159         else
0160             sprintf(trapname, "%d", trapno);
0161 
0162         printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
0163     } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
0164         printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
0165     } else if (VM86_TYPE(ret) == VM86_TRAP) {
0166         printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
0167                VM86_ARG(ret));
0168     } else if (VM86_TYPE(ret) == VM86_SIGNAL) {
0169         printf("[INFO]\tExited vm86 mode due to a signal\n");
0170     } else if (VM86_TYPE(ret) == VM86_STI) {
0171         printf("[INFO]\tExited vm86 mode due to STI\n");
0172     } else {
0173         printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
0174                VM86_TYPE(ret), VM86_ARG(ret));
0175     }
0176 
0177     if (rettype == -1 ||
0178         (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
0179         printf("[OK]\tReturned correctly\n");
0180     } else {
0181         printf("[FAIL]\tIncorrect return reason (started at eip = 0x%lx, ended at eip = 0x%lx)\n", eip, v86->regs.eip);
0182         nerrs++;
0183     }
0184 
0185     return true;
0186 }
0187 
0188 void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem)
0189 {
0190     struct table_desc {
0191         unsigned short limit;
0192         unsigned long base;
0193     } __attribute__((packed));
0194 
0195     /* Initialize variables with arbitrary values */
0196     struct table_desc gdt1 = { .base = 0x3c3c3c3c, .limit = 0x9999 };
0197     struct table_desc gdt2 = { .base = 0x1a1a1a1a, .limit = 0xaeae };
0198     struct table_desc idt1 = { .base = 0x7b7b7b7b, .limit = 0xf1f1 };
0199     struct table_desc idt2 = { .base = 0x89898989, .limit = 0x1313 };
0200     unsigned short msw1 = 0x1414, msw2 = 0x2525, msw3 = 3737;
0201 
0202     /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */
0203     do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests");
0204 
0205     /* Results from displacement-only addressing */
0206     msw1 = *(unsigned short *)(test_mem + 2052);
0207     memcpy(&idt1, test_mem + 2054, sizeof(idt1));
0208     memcpy(&gdt1, test_mem + 2060, sizeof(gdt1));
0209 
0210     /* Results from register-indirect addressing */
0211     msw2 = *(unsigned short *)(test_mem + 2066);
0212     memcpy(&idt2, test_mem + 2068, sizeof(idt2));
0213     memcpy(&gdt2, test_mem + 2074, sizeof(gdt2));
0214 
0215     /* Results when using register operands */
0216     msw3 = *(unsigned short *)(test_mem + 2080);
0217 
0218     printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1);
0219     printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n",
0220            idt1.limit, idt1.base);
0221     printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n",
0222            gdt1.limit, gdt1.base);
0223 
0224     if (msw1 != msw2 || msw1 != msw3)
0225         printf("[FAIL]\tAll the results of SMSW should be the same.\n");
0226     else
0227         printf("[PASS]\tAll the results from SMSW are identical.\n");
0228 
0229     if (memcmp(&gdt1, &gdt2, sizeof(gdt1)))
0230         printf("[FAIL]\tAll the results of SGDT should be the same.\n");
0231     else
0232         printf("[PASS]\tAll the results from SGDT are identical.\n");
0233 
0234     if (memcmp(&idt1, &idt2, sizeof(idt1)))
0235         printf("[FAIL]\tAll the results of SIDT should be the same.\n");
0236     else
0237         printf("[PASS]\tAll the results from SIDT are identical.\n");
0238 
0239     sethandler(SIGILL, sighandler, 0);
0240     do_test(vm86, vmcode_umip_str - vmcode, VM86_SIGNAL, 0,
0241         "STR instruction");
0242     clearhandler(SIGILL);
0243 
0244     sethandler(SIGILL, sighandler, 0);
0245     do_test(vm86, vmcode_umip_sldt - vmcode, VM86_SIGNAL, 0,
0246         "SLDT instruction");
0247     clearhandler(SIGILL);
0248 }
0249 
0250 int main(void)
0251 {
0252     struct vm86plus_struct v86;
0253     unsigned char *addr = mmap((void *)load_addr, 4096,
0254                    PROT_READ | PROT_WRITE | PROT_EXEC,
0255                    MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
0256     if (addr != (unsigned char *)load_addr)
0257         err(1, "mmap");
0258 
0259     memcpy(addr, vmcode, end_vmcode - vmcode);
0260     addr[2048] = 2;
0261     addr[2050] = 3;
0262 
0263     memset(&v86, 0, sizeof(v86));
0264 
0265     v86.regs.cs = load_addr / 16;
0266     v86.regs.ss = load_addr / 16;
0267     v86.regs.ds = load_addr / 16;
0268     v86.regs.es = load_addr / 16;
0269 
0270     /* Use the end of the page as our stack. */
0271     v86.regs.esp = 4096;
0272 
0273     assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */
0274 
0275     /* #BR -- should deliver SIG??? */
0276     do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR");
0277 
0278     /*
0279      * SYSENTER -- should cause #GP or #UD depending on CPU.
0280      * Expected return type -1 means that we shouldn't validate
0281      * the vm86 return value.  This will avoid problems on non-SEP
0282      * CPUs.
0283      */
0284     sethandler(SIGILL, sighandler, 0);
0285     do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER");
0286     clearhandler(SIGILL);
0287 
0288     /*
0289      * SYSCALL would be a disaster in VM86 mode.  Fortunately,
0290      * there is no kernel that both enables SYSCALL and sets
0291      * EFER.SCE, so it's #UD on all systems.  But vm86 is
0292      * buggy (or has a "feature"), so the SIGILL will actually
0293      * be delivered.
0294      */
0295     sethandler(SIGILL, sighandler, 0);
0296     do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL");
0297     clearhandler(SIGILL);
0298 
0299     /* STI with VIP set */
0300     v86.regs.eflags |= X86_EFLAGS_VIP;
0301     v86.regs.eflags &= ~X86_EFLAGS_IF;
0302     do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set");
0303 
0304     /* POPF with VIP set but IF clear: should not trap */
0305     v86.regs.eflags = X86_EFLAGS_VIP;
0306     v86.regs.eax = 0;
0307     do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP set and IF clear");
0308 
0309     /* POPF with VIP set and IF set: should trap */
0310     v86.regs.eflags = X86_EFLAGS_VIP;
0311     v86.regs.eax = X86_EFLAGS_IF;
0312     do_test(&v86, vmcode_popf_hlt - vmcode, VM86_STI, 0, "POPF with VIP and IF set");
0313 
0314     /* POPF with VIP clear and IF set: should not trap */
0315     v86.regs.eflags = 0;
0316     v86.regs.eax = X86_EFLAGS_IF;
0317     do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP clear and IF set");
0318 
0319     v86.regs.eflags = 0;
0320 
0321     /* INT3 -- should cause #BP */
0322     do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
0323 
0324     /* INT80 -- should exit with "INTx 0x80" */
0325     v86.regs.eax = (unsigned int)-1;
0326     do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
0327 
0328     /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */
0329     do_umip_tests(&v86, addr);
0330 
0331     /* Execute a null pointer */
0332     v86.regs.cs = 0;
0333     v86.regs.ss = 0;
0334     sethandler(SIGSEGV, sighandler, 0);
0335     got_signal = 0;
0336     if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
0337         !got_signal) {
0338         printf("[FAIL]\tDid not receive SIGSEGV\n");
0339         nerrs++;
0340     }
0341     clearhandler(SIGSEGV);
0342 
0343     /* Make sure nothing explodes if we fork. */
0344     if (fork() == 0)
0345         return 0;
0346 
0347     return (nerrs == 0 ? 0 : 1);
0348 }