exec_prot.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2022, Nicholas Miehlbradt, IBM Corporation
  4. * based on pkey_exec_prot.c
  5. *
  6. * Test if applying execute protection on pages works as expected.
  7. */
  8. #define _GNU_SOURCE
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <unistd.h>
  14. #include <sys/mman.h>
  15. #include "pkeys.h"
  16. #define PPC_INST_NOP 0x60000000
  17. #define PPC_INST_TRAP 0x7fe00008
  18. #define PPC_INST_BLR 0x4e800020
  19. static volatile sig_atomic_t fault_code;
  20. static volatile sig_atomic_t remaining_faults;
  21. static volatile unsigned int *fault_addr;
  22. static unsigned long pgsize, numinsns;
  23. static unsigned int *insns;
  24. static bool pkeys_supported;
  25. static bool is_fault_expected(int fault_code)
  26. {
  27. if (fault_code == SEGV_ACCERR)
  28. return true;
  29. /* Assume any pkey error is fine since pkey_exec_prot test covers them */
  30. if (fault_code == SEGV_PKUERR && pkeys_supported)
  31. return true;
  32. return false;
  33. }
  34. static void trap_handler(int signum, siginfo_t *sinfo, void *ctx)
  35. {
  36. /* Check if this fault originated from the expected address */
  37. if (sinfo->si_addr != (void *)fault_addr)
  38. sigsafe_err("got a fault for an unexpected address\n");
  39. _exit(1);
  40. }
  41. static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
  42. {
  43. fault_code = sinfo->si_code;
  44. /* Check if this fault originated from the expected address */
  45. if (sinfo->si_addr != (void *)fault_addr) {
  46. sigsafe_err("got a fault for an unexpected address\n");
  47. _exit(1);
  48. }
  49. /* Check if too many faults have occurred for a single test case */
  50. if (!remaining_faults) {
  51. sigsafe_err("got too many faults for the same address\n");
  52. _exit(1);
  53. }
  54. /* Restore permissions in order to continue */
  55. if (is_fault_expected(fault_code)) {
  56. if (mprotect(insns, pgsize, PROT_READ | PROT_WRITE | PROT_EXEC)) {
  57. sigsafe_err("failed to set access permissions\n");
  58. _exit(1);
  59. }
  60. } else {
  61. sigsafe_err("got a fault with an unexpected code\n");
  62. _exit(1);
  63. }
  64. remaining_faults--;
  65. }
  66. static int check_exec_fault(int rights)
  67. {
  68. /*
  69. * Jump to the executable region.
  70. *
  71. * The first iteration also checks if the overwrite of the
  72. * first instruction word from a trap to a no-op succeeded.
  73. */
  74. fault_code = -1;
  75. remaining_faults = 0;
  76. if (!(rights & PROT_EXEC))
  77. remaining_faults = 1;
  78. FAIL_IF(mprotect(insns, pgsize, rights) != 0);
  79. asm volatile("mtctr %0; bctrl" : : "r"(insns));
  80. FAIL_IF(remaining_faults != 0);
  81. if (!(rights & PROT_EXEC))
  82. FAIL_IF(!is_fault_expected(fault_code));
  83. return 0;
  84. }
  85. static int test(void)
  86. {
  87. struct sigaction segv_act, trap_act;
  88. int i;
  89. /* Skip the test if the CPU doesn't support Radix */
  90. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
  91. /* Check if pkeys are supported */
  92. pkeys_supported = pkeys_unsupported() == 0;
  93. /* Setup SIGSEGV handler */
  94. segv_act.sa_handler = 0;
  95. segv_act.sa_sigaction = segv_handler;
  96. FAIL_IF(sigprocmask(SIG_SETMASK, 0, &segv_act.sa_mask) != 0);
  97. segv_act.sa_flags = SA_SIGINFO;
  98. segv_act.sa_restorer = 0;
  99. FAIL_IF(sigaction(SIGSEGV, &segv_act, NULL) != 0);
  100. /* Setup SIGTRAP handler */
  101. trap_act.sa_handler = 0;
  102. trap_act.sa_sigaction = trap_handler;
  103. FAIL_IF(sigprocmask(SIG_SETMASK, 0, &trap_act.sa_mask) != 0);
  104. trap_act.sa_flags = SA_SIGINFO;
  105. trap_act.sa_restorer = 0;
  106. FAIL_IF(sigaction(SIGTRAP, &trap_act, NULL) != 0);
  107. /* Setup executable region */
  108. pgsize = getpagesize();
  109. numinsns = pgsize / sizeof(unsigned int);
  110. insns = (unsigned int *)mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
  111. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  112. FAIL_IF(insns == MAP_FAILED);
  113. /* Write the instruction words */
  114. for (i = 1; i < numinsns - 1; i++)
  115. insns[i] = PPC_INST_NOP;
  116. /*
  117. * Set the first instruction as an unconditional trap. If
  118. * the last write to this address succeeds, this should
  119. * get overwritten by a no-op.
  120. */
  121. insns[0] = PPC_INST_TRAP;
  122. /*
  123. * Later, to jump to the executable region, we use a branch
  124. * and link instruction (bctrl) which sets the return address
  125. * automatically in LR. Use that to return back.
  126. */
  127. insns[numinsns - 1] = PPC_INST_BLR;
  128. /*
  129. * Pick the first instruction's address from the executable
  130. * region.
  131. */
  132. fault_addr = insns;
  133. /*
  134. * Read an instruction word from the address when the page
  135. * is execute only. This should generate an access fault.
  136. */
  137. fault_code = -1;
  138. remaining_faults = 1;
  139. printf("Testing read on --x, should fault...");
  140. FAIL_IF(mprotect(insns, pgsize, PROT_EXEC) != 0);
  141. i = *fault_addr;
  142. FAIL_IF(remaining_faults != 0 || !is_fault_expected(fault_code));
  143. printf("ok!\n");
  144. /*
  145. * Write an instruction word to the address when the page
  146. * execute only. This should also generate an access fault.
  147. */
  148. fault_code = -1;
  149. remaining_faults = 1;
  150. printf("Testing write on --x, should fault...");
  151. FAIL_IF(mprotect(insns, pgsize, PROT_EXEC) != 0);
  152. *fault_addr = PPC_INST_NOP;
  153. FAIL_IF(remaining_faults != 0 || !is_fault_expected(fault_code));
  154. printf("ok!\n");
  155. printf("Testing exec on ---, should fault...");
  156. FAIL_IF(check_exec_fault(PROT_NONE));
  157. printf("ok!\n");
  158. printf("Testing exec on r--, should fault...");
  159. FAIL_IF(check_exec_fault(PROT_READ));
  160. printf("ok!\n");
  161. printf("Testing exec on -w-, should fault...");
  162. FAIL_IF(check_exec_fault(PROT_WRITE));
  163. printf("ok!\n");
  164. printf("Testing exec on rw-, should fault...");
  165. FAIL_IF(check_exec_fault(PROT_READ | PROT_WRITE));
  166. printf("ok!\n");
  167. printf("Testing exec on --x, should succeed...");
  168. FAIL_IF(check_exec_fault(PROT_EXEC));
  169. printf("ok!\n");
  170. printf("Testing exec on r-x, should succeed...");
  171. FAIL_IF(check_exec_fault(PROT_READ | PROT_EXEC));
  172. printf("ok!\n");
  173. printf("Testing exec on -wx, should succeed...");
  174. FAIL_IF(check_exec_fault(PROT_WRITE | PROT_EXEC));
  175. printf("ok!\n");
  176. printf("Testing exec on rwx, should succeed...");
  177. FAIL_IF(check_exec_fault(PROT_READ | PROT_WRITE | PROT_EXEC));
  178. printf("ok!\n");
  179. /* Cleanup */
  180. FAIL_IF(munmap((void *)insns, pgsize));
  181. return 0;
  182. }
  183. int main(void)
  184. {
  185. return test_harness(test, "exec_prot");
  186. }