pkey_exec_prot.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020, Sandipan Das, IBM Corp.
  4. *
  5. * Test if applying execute protection on pages using memory
  6. * protection keys 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 "pkeys.h"
  15. #define PPC_INST_NOP 0x60000000
  16. #define PPC_INST_TRAP 0x7fe00008
  17. #define PPC_INST_BLR 0x4e800020
  18. static volatile sig_atomic_t fault_pkey, fault_code, fault_type;
  19. static volatile sig_atomic_t remaining_faults;
  20. static volatile unsigned int *fault_addr;
  21. static unsigned long pgsize, numinsns;
  22. static unsigned int *insns;
  23. static void trap_handler(int signum, siginfo_t *sinfo, void *ctx)
  24. {
  25. /* Check if this fault originated from the expected address */
  26. if (sinfo->si_addr != (void *) fault_addr)
  27. sigsafe_err("got a fault for an unexpected address\n");
  28. _exit(1);
  29. }
  30. static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
  31. {
  32. int signal_pkey;
  33. signal_pkey = siginfo_pkey(sinfo);
  34. fault_code = sinfo->si_code;
  35. /* Check if this fault originated from the expected address */
  36. if (sinfo->si_addr != (void *) fault_addr) {
  37. sigsafe_err("got a fault for an unexpected address\n");
  38. _exit(1);
  39. }
  40. /* Check if too many faults have occurred for a single test case */
  41. if (!remaining_faults) {
  42. sigsafe_err("got too many faults for the same address\n");
  43. _exit(1);
  44. }
  45. /* Restore permissions in order to continue */
  46. switch (fault_code) {
  47. case SEGV_ACCERR:
  48. if (mprotect(insns, pgsize, PROT_READ | PROT_WRITE)) {
  49. sigsafe_err("failed to set access permissions\n");
  50. _exit(1);
  51. }
  52. break;
  53. case SEGV_PKUERR:
  54. if (signal_pkey != fault_pkey) {
  55. sigsafe_err("got a fault for an unexpected pkey\n");
  56. _exit(1);
  57. }
  58. switch (fault_type) {
  59. case PKEY_DISABLE_ACCESS:
  60. pkey_set_rights(fault_pkey, PKEY_UNRESTRICTED);
  61. break;
  62. case PKEY_DISABLE_EXECUTE:
  63. /*
  64. * Reassociate the exec-only pkey with the region
  65. * to be able to continue. Unlike AMR, we cannot
  66. * set IAMR directly from userspace to restore the
  67. * permissions.
  68. */
  69. if (mprotect(insns, pgsize, PROT_EXEC)) {
  70. sigsafe_err("failed to set execute permissions\n");
  71. _exit(1);
  72. }
  73. break;
  74. default:
  75. sigsafe_err("got a fault with an unexpected type\n");
  76. _exit(1);
  77. }
  78. break;
  79. default:
  80. sigsafe_err("got a fault with an unexpected code\n");
  81. _exit(1);
  82. }
  83. remaining_faults--;
  84. }
  85. static int test(void)
  86. {
  87. struct sigaction segv_act, trap_act;
  88. unsigned long rights;
  89. int pkey, ret, i;
  90. ret = pkeys_unsupported();
  91. if (ret)
  92. return ret;
  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. /* Allocate a pkey that restricts execution */
  129. rights = PKEY_DISABLE_EXECUTE;
  130. pkey = sys_pkey_alloc(0, rights);
  131. FAIL_IF(pkey < 0);
  132. /*
  133. * Pick the first instruction's address from the executable
  134. * region.
  135. */
  136. fault_addr = insns;
  137. /* The following two cases will avoid SEGV_PKUERR */
  138. fault_type = -1;
  139. fault_pkey = -1;
  140. /*
  141. * Read an instruction word from the address when AMR bits
  142. * are not set i.e. the pkey permits both read and write
  143. * access.
  144. *
  145. * This should not generate a fault as having PROT_EXEC
  146. * implies PROT_READ on GNU systems. The pkey currently
  147. * restricts execution only based on the IAMR bits. The
  148. * AMR bits are cleared.
  149. */
  150. remaining_faults = 0;
  151. FAIL_IF(sys_pkey_mprotect(insns, pgsize, PROT_EXEC, pkey) != 0);
  152. printf("read from %p, pkey permissions are %s\n", fault_addr,
  153. pkey_rights(rights));
  154. i = *fault_addr;
  155. FAIL_IF(remaining_faults != 0);
  156. /*
  157. * Write an instruction word to the address when AMR bits
  158. * are not set i.e. the pkey permits both read and write
  159. * access.
  160. *
  161. * This should generate an access fault as having just
  162. * PROT_EXEC also restricts writes. The pkey currently
  163. * restricts execution only based on the IAMR bits. The
  164. * AMR bits are cleared.
  165. */
  166. remaining_faults = 1;
  167. FAIL_IF(sys_pkey_mprotect(insns, pgsize, PROT_EXEC, pkey) != 0);
  168. printf("write to %p, pkey permissions are %s\n", fault_addr,
  169. pkey_rights(rights));
  170. *fault_addr = PPC_INST_TRAP;
  171. FAIL_IF(remaining_faults != 0 || fault_code != SEGV_ACCERR);
  172. /* The following three cases will generate SEGV_PKUERR */
  173. rights |= PKEY_DISABLE_ACCESS;
  174. fault_type = PKEY_DISABLE_ACCESS;
  175. fault_pkey = pkey;
  176. /*
  177. * Read an instruction word from the address when AMR bits
  178. * are set i.e. the pkey permits neither read nor write
  179. * access.
  180. *
  181. * This should generate a pkey fault based on AMR bits only
  182. * as having PROT_EXEC implicitly allows reads.
  183. */
  184. remaining_faults = 1;
  185. FAIL_IF(sys_pkey_mprotect(insns, pgsize, PROT_EXEC, pkey) != 0);
  186. pkey_set_rights(pkey, rights);
  187. printf("read from %p, pkey permissions are %s\n", fault_addr,
  188. pkey_rights(rights));
  189. i = *fault_addr;
  190. FAIL_IF(remaining_faults != 0 || fault_code != SEGV_PKUERR);
  191. /*
  192. * Write an instruction word to the address when AMR bits
  193. * are set i.e. the pkey permits neither read nor write
  194. * access.
  195. *
  196. * This should generate two faults. First, a pkey fault
  197. * based on AMR bits and then an access fault since
  198. * PROT_EXEC does not allow writes.
  199. */
  200. remaining_faults = 2;
  201. FAIL_IF(sys_pkey_mprotect(insns, pgsize, PROT_EXEC, pkey) != 0);
  202. pkey_set_rights(pkey, rights);
  203. printf("write to %p, pkey permissions are %s\n", fault_addr,
  204. pkey_rights(rights));
  205. *fault_addr = PPC_INST_NOP;
  206. FAIL_IF(remaining_faults != 0 || fault_code != SEGV_ACCERR);
  207. /* Free the current pkey */
  208. sys_pkey_free(pkey);
  209. rights = 0;
  210. do {
  211. /*
  212. * Allocate pkeys with all valid combinations of read,
  213. * write and execute restrictions.
  214. */
  215. pkey = sys_pkey_alloc(0, rights);
  216. FAIL_IF(pkey < 0);
  217. /*
  218. * Jump to the executable region. AMR bits may or may not
  219. * be set but they should not affect execution.
  220. *
  221. * This should generate pkey faults based on IAMR bits which
  222. * may be set to restrict execution.
  223. *
  224. * The first iteration also checks if the overwrite of the
  225. * first instruction word from a trap to a no-op succeeded.
  226. */
  227. fault_pkey = pkey;
  228. fault_type = -1;
  229. remaining_faults = 0;
  230. if (rights & PKEY_DISABLE_EXECUTE) {
  231. fault_type = PKEY_DISABLE_EXECUTE;
  232. remaining_faults = 1;
  233. }
  234. FAIL_IF(sys_pkey_mprotect(insns, pgsize, PROT_EXEC, pkey) != 0);
  235. printf("execute at %p, pkey permissions are %s\n", fault_addr,
  236. pkey_rights(rights));
  237. asm volatile("mtctr %0; bctrl" : : "r"(insns));
  238. FAIL_IF(remaining_faults != 0);
  239. if (rights & PKEY_DISABLE_EXECUTE)
  240. FAIL_IF(fault_code != SEGV_PKUERR);
  241. /* Free the current pkey */
  242. sys_pkey_free(pkey);
  243. /* Find next valid combination of pkey rights */
  244. rights = next_pkey_rights(rights);
  245. } while (rights);
  246. /* Cleanup */
  247. munmap((void *) insns, pgsize);
  248. return 0;
  249. }
  250. int main(void)
  251. {
  252. return test_harness(test, "pkey_exec_prot");
  253. }