^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * iopl.c - Test case for a Linux on Xen 64-bit bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2015 Andrew Lutomirski
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <setjmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int nerrs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct sigaction sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) memset(&sa, 0, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) sa.sa_sigaction = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) sa.sa_flags = SA_SIGINFO | flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sigemptyset(&sa.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (sigaction(sig, &sa, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) err(1, "sigaction");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void clearhandler(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct sigaction sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) memset(&sa, 0, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) sa.sa_handler = SIG_DFL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) sigemptyset(&sa.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (sigaction(sig, &sa, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) err(1, "sigaction");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static jmp_buf jmpbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) siglongjmp(jmpbuf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static bool try_outb(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (sigsetjmp(jmpbuf, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) asm volatile ("outb %%al, %w[port]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) : : [port] "Nd" (port), "a" (0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) clearhandler(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void expect_ok_outb(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!try_outb(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) printf("[FAIL]\toutb to 0x%02hx failed\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) printf("[OK]\toutb to 0x%02hx worked\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static void expect_gp_outb(unsigned short port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (try_outb(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) printf("[FAIL]\toutb to 0x%02hx worked\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) printf("[OK]\toutb to 0x%02hx failed\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define RET_FAULTED 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define RET_FAIL 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define RET_EMUL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int try_cli(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (sigsetjmp(jmpbuf, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return RET_FAULTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) asm volatile("cli; pushf; pop %[flags]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) : [flags] "=rm" (flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* X86_FLAGS_IF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!(flags & (1 << 9)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return RET_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return RET_EMUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) clearhandler(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int try_sti(bool irqs_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (sigsetjmp(jmpbuf, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return RET_FAULTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) asm volatile("sti; pushf; pop %[flags]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) : [flags] "=rm" (flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* X86_FLAGS_IF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (irqs_off && (flags & (1 << 9)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return RET_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return RET_EMUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) clearhandler(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void expect_gp_sti(bool irqs_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int ret = try_sti(irqs_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case RET_FAULTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printf("[OK]\tSTI faulted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case RET_EMUL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) printf("[OK]\tSTI NOPped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) printf("[FAIL]\tSTI worked\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Returns whether it managed to disable interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static bool test_cli(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int ret = try_cli();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case RET_FAULTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) printf("[OK]\tCLI faulted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) case RET_EMUL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) printf("[OK]\tCLI NOPped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) printf("[FAIL]\tCLI worked\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) CPU_SET(0, &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) err(1, "sched_setaffinity to CPU 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Probe for iopl support. Note that iopl(0) works even as nonroot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) switch(iopl(3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case -ENOSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) printf("[OK]\tiopl() nor supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) printf("[OK]\tiopl(3) failed (%d) -- try running as root\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Make sure that CLI/STI are blocked even with IOPL level 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) expect_gp_sti(test_cli());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) expect_ok_outb(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* Establish an I/O bitmap to test the restore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (ioperm(0x80, 1, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) err(1, "ioperm(0x80, 1, 1) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* Restore our original state prior to starting the fork test. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (iopl(0) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) err(1, "iopl(0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Verify that IOPL emulation is disabled and the I/O bitmap still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) expect_ok_outb(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) expect_gp_outb(0xed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* Drop the I/O bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (ioperm(0x80, 1, 0) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err(1, "ioperm(0x80, 1, 0) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pid_t child = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (child == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) err(1, "fork");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (child == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) printf("\tchild: set IOPL to 3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (iopl(3) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) err(1, "iopl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) printf("[RUN]\tchild: write to 0x80\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) asm volatile ("outb %%al, $0x80" : : "a" (0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (waitpid(child, &status, 0) != child ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) !WIFEXITED(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) printf("[FAIL]\tChild died\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } else if (WEXITSTATUS(status) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) printf("[FAIL]\tChild failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) printf("[OK]\tChild succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) printf("[RUN]\tparent: write to 0x80 (should fail)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) expect_gp_outb(0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) expect_gp_sti(test_cli());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* Test the capability checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) printf("\tiopl(3)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (iopl(3) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) err(1, "iopl(3)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) printf("\tDrop privileges\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (setresuid(1, 1, 1) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) printf("[WARN]\tDropping privileges failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) printf("[RUN]\tiopl(3) unprivileged but with IOPL==3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (iopl(3) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) printf("[FAIL]\tiopl(3) should work if iopl is already 3 even if unprivileged\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) printf("[RUN]\tiopl(0) unprivileged\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (iopl(0) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) printf("[FAIL]\tiopl(0) should work if iopl is already 3 even if unprivileged\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) printf("[RUN]\tiopl(3) unprivileged\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (iopl(3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) printf("[FAIL]\tiopl(3) should fail if when unprivileged if iopl==0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) nerrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) printf("[OK]\tFailed as expected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return nerrs ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }