iopl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * iopl.c - Test case for a Linux on Xen 64-bit bug
  4. * Copyright (c) 2015 Andrew Lutomirski
  5. */
  6. #define _GNU_SOURCE
  7. #include <err.h>
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <signal.h>
  11. #include <setjmp.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <stdbool.h>
  19. #include <sched.h>
  20. #include <sys/io.h>
  21. static int nerrs = 0;
  22. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  23. int flags)
  24. {
  25. struct sigaction sa;
  26. memset(&sa, 0, sizeof(sa));
  27. sa.sa_sigaction = handler;
  28. sa.sa_flags = SA_SIGINFO | flags;
  29. sigemptyset(&sa.sa_mask);
  30. if (sigaction(sig, &sa, 0))
  31. err(1, "sigaction");
  32. }
  33. static jmp_buf jmpbuf;
  34. static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
  35. {
  36. siglongjmp(jmpbuf, 1);
  37. }
  38. int main(void)
  39. {
  40. cpu_set_t cpuset;
  41. CPU_ZERO(&cpuset);
  42. CPU_SET(0, &cpuset);
  43. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
  44. err(1, "sched_setaffinity to CPU 0");
  45. /* Probe for iopl support. Note that iopl(0) works even as nonroot. */
  46. if (iopl(3) != 0) {
  47. printf("[OK]\tiopl(3) failed (%d) -- try running as root\n",
  48. errno);
  49. return 0;
  50. }
  51. /* Restore our original state prior to starting the test. */
  52. if (iopl(0) != 0)
  53. err(1, "iopl(0)");
  54. pid_t child = fork();
  55. if (child == -1)
  56. err(1, "fork");
  57. if (child == 0) {
  58. printf("\tchild: set IOPL to 3\n");
  59. if (iopl(3) != 0)
  60. err(1, "iopl");
  61. printf("[RUN]\tchild: write to 0x80\n");
  62. asm volatile ("outb %%al, $0x80" : : "a" (0));
  63. return 0;
  64. } else {
  65. int status;
  66. if (waitpid(child, &status, 0) != child ||
  67. !WIFEXITED(status)) {
  68. printf("[FAIL]\tChild died\n");
  69. nerrs++;
  70. } else if (WEXITSTATUS(status) != 0) {
  71. printf("[FAIL]\tChild failed\n");
  72. nerrs++;
  73. } else {
  74. printf("[OK]\tChild succeeded\n");
  75. }
  76. }
  77. printf("[RUN]\tparent: write to 0x80 (should fail)\n");
  78. sethandler(SIGSEGV, sigsegv, 0);
  79. if (sigsetjmp(jmpbuf, 1) != 0) {
  80. printf("[OK]\twrite was denied\n");
  81. } else {
  82. asm volatile ("outb %%al, $0x80" : : "a" (0));
  83. printf("[FAIL]\twrite was allowed\n");
  84. nerrs++;
  85. }
  86. /* Test the capability checks. */
  87. printf("\tiopl(3)\n");
  88. if (iopl(3) != 0)
  89. err(1, "iopl(3)");
  90. printf("\tDrop privileges\n");
  91. if (setresuid(1, 1, 1) != 0) {
  92. printf("[WARN]\tDropping privileges failed\n");
  93. goto done;
  94. }
  95. printf("[RUN]\tiopl(3) unprivileged but with IOPL==3\n");
  96. if (iopl(3) != 0) {
  97. printf("[FAIL]\tiopl(3) should work if iopl is already 3 even if unprivileged\n");
  98. nerrs++;
  99. }
  100. printf("[RUN]\tiopl(0) unprivileged\n");
  101. if (iopl(0) != 0) {
  102. printf("[FAIL]\tiopl(0) should work if iopl is already 3 even if unprivileged\n");
  103. nerrs++;
  104. }
  105. printf("[RUN]\tiopl(3) unprivileged\n");
  106. if (iopl(3) == 0) {
  107. printf("[FAIL]\tiopl(3) should fail if when unprivileged if iopl==0\n");
  108. nerrs++;
  109. } else {
  110. printf("[OK]\tFailed as expected\n");
  111. }
  112. done:
  113. return nerrs ? 1 : 0;
  114. }