ioperm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ioperm.c - Test case for ioperm(2)
  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 void clearhandler(int sig)
  33. {
  34. struct sigaction sa;
  35. memset(&sa, 0, sizeof(sa));
  36. sa.sa_handler = SIG_DFL;
  37. sigemptyset(&sa.sa_mask);
  38. if (sigaction(sig, &sa, 0))
  39. err(1, "sigaction");
  40. }
  41. static jmp_buf jmpbuf;
  42. static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
  43. {
  44. siglongjmp(jmpbuf, 1);
  45. }
  46. static bool try_outb(unsigned short port)
  47. {
  48. sethandler(SIGSEGV, sigsegv, SA_RESETHAND);
  49. if (sigsetjmp(jmpbuf, 1) != 0) {
  50. return false;
  51. } else {
  52. asm volatile ("outb %%al, %w[port]"
  53. : : [port] "Nd" (port), "a" (0));
  54. return true;
  55. }
  56. clearhandler(SIGSEGV);
  57. }
  58. static void expect_ok(unsigned short port)
  59. {
  60. if (!try_outb(port)) {
  61. printf("[FAIL]\toutb to 0x%02hx failed\n", port);
  62. exit(1);
  63. }
  64. printf("[OK]\toutb to 0x%02hx worked\n", port);
  65. }
  66. static void expect_gp(unsigned short port)
  67. {
  68. if (try_outb(port)) {
  69. printf("[FAIL]\toutb to 0x%02hx worked\n", port);
  70. exit(1);
  71. }
  72. printf("[OK]\toutb to 0x%02hx failed\n", port);
  73. }
  74. int main(void)
  75. {
  76. cpu_set_t cpuset;
  77. CPU_ZERO(&cpuset);
  78. CPU_SET(0, &cpuset);
  79. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
  80. err(1, "sched_setaffinity to CPU 0");
  81. expect_gp(0x80);
  82. expect_gp(0xed);
  83. /*
  84. * Probe for ioperm support. Note that clearing ioperm bits
  85. * works even as nonroot.
  86. */
  87. printf("[RUN]\tenable 0x80\n");
  88. if (ioperm(0x80, 1, 1) != 0) {
  89. printf("[OK]\tioperm(0x80, 1, 1) failed (%d) -- try running as root\n",
  90. errno);
  91. return 0;
  92. }
  93. expect_ok(0x80);
  94. expect_gp(0xed);
  95. printf("[RUN]\tdisable 0x80\n");
  96. if (ioperm(0x80, 1, 0) != 0) {
  97. printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
  98. return 1;
  99. }
  100. expect_gp(0x80);
  101. expect_gp(0xed);
  102. /* Make sure that fork() preserves ioperm. */
  103. if (ioperm(0x80, 1, 1) != 0) {
  104. printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
  105. return 1;
  106. }
  107. pid_t child = fork();
  108. if (child == -1)
  109. err(1, "fork");
  110. if (child == 0) {
  111. printf("[RUN]\tchild: check that we inherited permissions\n");
  112. expect_ok(0x80);
  113. expect_gp(0xed);
  114. return 0;
  115. } else {
  116. int status;
  117. if (waitpid(child, &status, 0) != child ||
  118. !WIFEXITED(status)) {
  119. printf("[FAIL]\tChild died\n");
  120. nerrs++;
  121. } else if (WEXITSTATUS(status) != 0) {
  122. printf("[FAIL]\tChild failed\n");
  123. nerrs++;
  124. } else {
  125. printf("[OK]\tChild succeeded\n");
  126. }
  127. }
  128. /* Test the capability checks. */
  129. printf("\tDrop privileges\n");
  130. if (setresuid(1, 1, 1) != 0) {
  131. printf("[WARN]\tDropping privileges failed\n");
  132. return 0;
  133. }
  134. printf("[RUN]\tdisable 0x80\n");
  135. if (ioperm(0x80, 1, 0) != 0) {
  136. printf("[FAIL]\tioperm(0x80, 1, 0) failed (%d)", errno);
  137. return 1;
  138. }
  139. printf("[OK]\tit worked\n");
  140. printf("[RUN]\tenable 0x80 again\n");
  141. if (ioperm(0x80, 1, 1) == 0) {
  142. printf("[FAIL]\tit succeeded but should have failed.\n");
  143. return 1;
  144. }
  145. printf("[OK]\tit failed\n");
  146. return 0;
  147. }