ptrace-pkey.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Ptrace test for Memory Protection Key registers
  4. *
  5. * Copyright (C) 2015 Anshuman Khandual, IBM Corporation.
  6. * Copyright (C) 2018 IBM Corporation.
  7. */
  8. #include "ptrace.h"
  9. #include "child.h"
  10. #ifndef __NR_pkey_alloc
  11. #define __NR_pkey_alloc 384
  12. #endif
  13. #ifndef __NR_pkey_free
  14. #define __NR_pkey_free 385
  15. #endif
  16. #ifndef NT_PPC_PKEY
  17. #define NT_PPC_PKEY 0x110
  18. #endif
  19. #ifndef PKEY_DISABLE_EXECUTE
  20. #define PKEY_DISABLE_EXECUTE 0x4
  21. #endif
  22. #define AMR_BITS_PER_PKEY 2
  23. #define PKEY_REG_BITS (sizeof(u64) * 8)
  24. #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey + 1) * AMR_BITS_PER_PKEY))
  25. static const char user_read[] = "[User Read (Running)]";
  26. static const char user_write[] = "[User Write (Running)]";
  27. static const char ptrace_read_running[] = "[Ptrace Read (Running)]";
  28. static const char ptrace_write_running[] = "[Ptrace Write (Running)]";
  29. /* Information shared between the parent and the child. */
  30. struct shared_info {
  31. struct child_sync child_sync;
  32. /* AMR value the parent expects to read from the child. */
  33. unsigned long amr1;
  34. /* AMR value the parent is expected to write to the child. */
  35. unsigned long amr2;
  36. /* AMR value that ptrace should refuse to write to the child. */
  37. unsigned long amr3;
  38. /* IAMR value the parent expects to read from the child. */
  39. unsigned long expected_iamr;
  40. /* UAMOR value the parent expects to read from the child. */
  41. unsigned long expected_uamor;
  42. /*
  43. * IAMR and UAMOR values that ptrace should refuse to write to the child
  44. * (even though they're valid ones) because userspace doesn't have
  45. * access to those registers.
  46. */
  47. unsigned long new_iamr;
  48. unsigned long new_uamor;
  49. };
  50. static int sys_pkey_alloc(unsigned long flags, unsigned long init_access_rights)
  51. {
  52. return syscall(__NR_pkey_alloc, flags, init_access_rights);
  53. }
  54. static int sys_pkey_free(int pkey)
  55. {
  56. return syscall(__NR_pkey_free, pkey);
  57. }
  58. static int child(struct shared_info *info)
  59. {
  60. unsigned long reg;
  61. bool disable_execute = true;
  62. int pkey1, pkey2, pkey3;
  63. int ret;
  64. /* Wait until parent fills out the initial register values. */
  65. ret = wait_parent(&info->child_sync);
  66. if (ret)
  67. return ret;
  68. /* Get some pkeys so that we can change their bits in the AMR. */
  69. pkey1 = sys_pkey_alloc(0, PKEY_DISABLE_EXECUTE);
  70. if (pkey1 < 0) {
  71. pkey1 = sys_pkey_alloc(0, 0);
  72. CHILD_FAIL_IF(pkey1 < 0, &info->child_sync);
  73. disable_execute = false;
  74. }
  75. pkey2 = sys_pkey_alloc(0, 0);
  76. CHILD_FAIL_IF(pkey2 < 0, &info->child_sync);
  77. pkey3 = sys_pkey_alloc(0, 0);
  78. CHILD_FAIL_IF(pkey3 < 0, &info->child_sync);
  79. info->amr1 |= 3ul << pkeyshift(pkey1);
  80. info->amr2 |= 3ul << pkeyshift(pkey2);
  81. info->amr3 |= info->amr2 | 3ul << pkeyshift(pkey3);
  82. if (disable_execute)
  83. info->expected_iamr |= 1ul << pkeyshift(pkey1);
  84. else
  85. info->expected_iamr &= ~(1ul << pkeyshift(pkey1));
  86. info->expected_iamr &= ~(1ul << pkeyshift(pkey2) | 1ul << pkeyshift(pkey3));
  87. info->expected_uamor |= 3ul << pkeyshift(pkey1) |
  88. 3ul << pkeyshift(pkey2);
  89. info->new_iamr |= 1ul << pkeyshift(pkey1) | 1ul << pkeyshift(pkey2);
  90. info->new_uamor |= 3ul << pkeyshift(pkey1);
  91. /*
  92. * We won't use pkey3. We just want a plausible but invalid key to test
  93. * whether ptrace will let us write to AMR bits we are not supposed to.
  94. *
  95. * This also tests whether the kernel restores the UAMOR permissions
  96. * after a key is freed.
  97. */
  98. sys_pkey_free(pkey3);
  99. printf("%-30s AMR: %016lx pkey1: %d pkey2: %d pkey3: %d\n",
  100. user_write, info->amr1, pkey1, pkey2, pkey3);
  101. mtspr(SPRN_AMR, info->amr1);
  102. /* Wait for parent to read our AMR value and write a new one. */
  103. ret = prod_parent(&info->child_sync);
  104. CHILD_FAIL_IF(ret, &info->child_sync);
  105. ret = wait_parent(&info->child_sync);
  106. if (ret)
  107. return ret;
  108. reg = mfspr(SPRN_AMR);
  109. printf("%-30s AMR: %016lx\n", user_read, reg);
  110. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  111. /*
  112. * Wait for parent to try to write an invalid AMR value.
  113. */
  114. ret = prod_parent(&info->child_sync);
  115. CHILD_FAIL_IF(ret, &info->child_sync);
  116. ret = wait_parent(&info->child_sync);
  117. if (ret)
  118. return ret;
  119. reg = mfspr(SPRN_AMR);
  120. printf("%-30s AMR: %016lx\n", user_read, reg);
  121. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  122. /*
  123. * Wait for parent to try to write an IAMR and a UAMOR value. We can't
  124. * verify them, but we can verify that the AMR didn't change.
  125. */
  126. ret = prod_parent(&info->child_sync);
  127. CHILD_FAIL_IF(ret, &info->child_sync);
  128. ret = wait_parent(&info->child_sync);
  129. if (ret)
  130. return ret;
  131. reg = mfspr(SPRN_AMR);
  132. printf("%-30s AMR: %016lx\n", user_read, reg);
  133. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  134. /* Now let parent now that we are finished. */
  135. ret = prod_parent(&info->child_sync);
  136. CHILD_FAIL_IF(ret, &info->child_sync);
  137. return TEST_PASS;
  138. }
  139. static int parent(struct shared_info *info, pid_t pid)
  140. {
  141. unsigned long regs[3];
  142. int ret, status;
  143. /*
  144. * Get the initial values for AMR, IAMR and UAMOR and communicate them
  145. * to the child.
  146. */
  147. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  148. PARENT_SKIP_IF_UNSUPPORTED(ret, &info->child_sync);
  149. PARENT_FAIL_IF(ret, &info->child_sync);
  150. info->amr1 = info->amr2 = info->amr3 = regs[0];
  151. info->expected_iamr = info->new_iamr = regs[1];
  152. info->expected_uamor = info->new_uamor = regs[2];
  153. /* Wake up child so that it can set itself up. */
  154. ret = prod_child(&info->child_sync);
  155. PARENT_FAIL_IF(ret, &info->child_sync);
  156. ret = wait_child(&info->child_sync);
  157. if (ret)
  158. return ret;
  159. /* Verify that we can read the pkey registers from the child. */
  160. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  161. PARENT_FAIL_IF(ret, &info->child_sync);
  162. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  163. ptrace_read_running, regs[0], regs[1], regs[2]);
  164. PARENT_FAIL_IF(regs[0] != info->amr1, &info->child_sync);
  165. PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
  166. PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
  167. /* Write valid AMR value in child. */
  168. ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->amr2, 1);
  169. PARENT_FAIL_IF(ret, &info->child_sync);
  170. printf("%-30s AMR: %016lx\n", ptrace_write_running, info->amr2);
  171. /* Wake up child so that it can verify it changed. */
  172. ret = prod_child(&info->child_sync);
  173. PARENT_FAIL_IF(ret, &info->child_sync);
  174. ret = wait_child(&info->child_sync);
  175. if (ret)
  176. return ret;
  177. /* Write invalid AMR value in child. */
  178. ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->amr3, 1);
  179. PARENT_FAIL_IF(ret, &info->child_sync);
  180. printf("%-30s AMR: %016lx\n", ptrace_write_running, info->amr3);
  181. /* Wake up child so that it can verify it didn't change. */
  182. ret = prod_child(&info->child_sync);
  183. PARENT_FAIL_IF(ret, &info->child_sync);
  184. ret = wait_child(&info->child_sync);
  185. if (ret)
  186. return ret;
  187. /* Try to write to IAMR. */
  188. regs[0] = info->amr1;
  189. regs[1] = info->new_iamr;
  190. ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 2);
  191. PARENT_FAIL_IF(!ret, &info->child_sync);
  192. printf("%-30s AMR: %016lx IAMR: %016lx\n",
  193. ptrace_write_running, regs[0], regs[1]);
  194. /* Try to write to IAMR and UAMOR. */
  195. regs[2] = info->new_uamor;
  196. ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 3);
  197. PARENT_FAIL_IF(!ret, &info->child_sync);
  198. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  199. ptrace_write_running, regs[0], regs[1], regs[2]);
  200. /* Verify that all registers still have their expected values. */
  201. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  202. PARENT_FAIL_IF(ret, &info->child_sync);
  203. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  204. ptrace_read_running, regs[0], regs[1], regs[2]);
  205. PARENT_FAIL_IF(regs[0] != info->amr2, &info->child_sync);
  206. PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
  207. PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
  208. /* Wake up child so that it can verify AMR didn't change and wrap up. */
  209. ret = prod_child(&info->child_sync);
  210. PARENT_FAIL_IF(ret, &info->child_sync);
  211. ret = wait(&status);
  212. if (ret != pid) {
  213. printf("Child's exit status not captured\n");
  214. ret = TEST_PASS;
  215. } else if (!WIFEXITED(status)) {
  216. printf("Child exited abnormally\n");
  217. ret = TEST_FAIL;
  218. } else
  219. ret = WEXITSTATUS(status) ? TEST_FAIL : TEST_PASS;
  220. return ret;
  221. }
  222. static int ptrace_pkey(void)
  223. {
  224. struct shared_info *info;
  225. int shm_id;
  226. int ret;
  227. pid_t pid;
  228. shm_id = shmget(IPC_PRIVATE, sizeof(*info), 0777 | IPC_CREAT);
  229. info = shmat(shm_id, NULL, 0);
  230. ret = init_child_sync(&info->child_sync);
  231. if (ret)
  232. return ret;
  233. pid = fork();
  234. if (pid < 0) {
  235. perror("fork() failed");
  236. ret = TEST_FAIL;
  237. } else if (pid == 0)
  238. ret = child(info);
  239. else
  240. ret = parent(info, pid);
  241. shmdt(info);
  242. if (pid) {
  243. destroy_child_sync(&info->child_sync);
  244. shmctl(shm_id, IPC_RMID, NULL);
  245. }
  246. return ret;
  247. }
  248. int main(int argc, char *argv[])
  249. {
  250. return test_harness(ptrace_pkey, "ptrace_pkey");
  251. }