iopl.c 2.9 KB

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