ptrace-pkey.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 invalid_amr;
  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 invalid_iamr;
  48. unsigned long invalid_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 child(struct shared_info *info)
  55. {
  56. unsigned long reg;
  57. bool disable_execute = true;
  58. int pkey1, pkey2, pkey3;
  59. int ret;
  60. /* Wait until parent fills out the initial register values. */
  61. ret = wait_parent(&info->child_sync);
  62. if (ret)
  63. return ret;
  64. /* Get some pkeys so that we can change their bits in the AMR. */
  65. pkey1 = sys_pkey_alloc(0, PKEY_DISABLE_EXECUTE);
  66. if (pkey1 < 0) {
  67. pkey1 = sys_pkey_alloc(0, 0);
  68. CHILD_FAIL_IF(pkey1 < 0, &info->child_sync);
  69. disable_execute = false;
  70. }
  71. pkey2 = sys_pkey_alloc(0, 0);
  72. CHILD_FAIL_IF(pkey2 < 0, &info->child_sync);
  73. pkey3 = sys_pkey_alloc(0, 0);
  74. CHILD_FAIL_IF(pkey3 < 0, &info->child_sync);
  75. info->amr1 |= 3ul << pkeyshift(pkey1);
  76. info->amr2 |= 3ul << pkeyshift(pkey2);
  77. /*
  78. * invalid amr value where we try to force write
  79. * things which are deined by a uamor setting.
  80. */
  81. info->invalid_amr = info->amr2 | (~0x0UL & ~info->expected_uamor);
  82. /*
  83. * if PKEY_DISABLE_EXECUTE succeeded we should update the expected_iamr
  84. */
  85. if (disable_execute)
  86. info->expected_iamr |= 1ul << pkeyshift(pkey1);
  87. else
  88. info->expected_iamr &= ~(1ul << pkeyshift(pkey1));
  89. /*
  90. * We allocated pkey2 and pkey 3 above. Clear the IAMR bits.
  91. */
  92. info->expected_iamr &= ~(1ul << pkeyshift(pkey2));
  93. info->expected_iamr &= ~(1ul << pkeyshift(pkey3));
  94. /*
  95. * Create an IAMR value different from expected value.
  96. * Kernel will reject an IAMR and UAMOR change.
  97. */
  98. info->invalid_iamr = info->expected_iamr | (1ul << pkeyshift(pkey1) | 1ul << pkeyshift(pkey2));
  99. info->invalid_uamor = info->expected_uamor & ~(0x3ul << pkeyshift(pkey1));
  100. printf("%-30s AMR: %016lx pkey1: %d pkey2: %d pkey3: %d\n",
  101. user_write, info->amr1, pkey1, pkey2, pkey3);
  102. set_amr(info->amr1);
  103. /* Wait for parent to read our AMR value and write a new one. */
  104. ret = prod_parent(&info->child_sync);
  105. CHILD_FAIL_IF(ret, &info->child_sync);
  106. ret = wait_parent(&info->child_sync);
  107. if (ret)
  108. return ret;
  109. reg = mfspr(SPRN_AMR);
  110. printf("%-30s AMR: %016lx\n", user_read, reg);
  111. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  112. /*
  113. * Wait for parent to try to write an invalid AMR value.
  114. */
  115. ret = prod_parent(&info->child_sync);
  116. CHILD_FAIL_IF(ret, &info->child_sync);
  117. ret = wait_parent(&info->child_sync);
  118. if (ret)
  119. return ret;
  120. reg = mfspr(SPRN_AMR);
  121. printf("%-30s AMR: %016lx\n", user_read, reg);
  122. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  123. /*
  124. * Wait for parent to try to write an IAMR and a UAMOR value. We can't
  125. * verify them, but we can verify that the AMR didn't change.
  126. */
  127. ret = prod_parent(&info->child_sync);
  128. CHILD_FAIL_IF(ret, &info->child_sync);
  129. ret = wait_parent(&info->child_sync);
  130. if (ret)
  131. return ret;
  132. reg = mfspr(SPRN_AMR);
  133. printf("%-30s AMR: %016lx\n", user_read, reg);
  134. CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
  135. /* Now let parent now that we are finished. */
  136. ret = prod_parent(&info->child_sync);
  137. CHILD_FAIL_IF(ret, &info->child_sync);
  138. return TEST_PASS;
  139. }
  140. static int parent(struct shared_info *info, pid_t pid)
  141. {
  142. unsigned long regs[3];
  143. int ret, status;
  144. /*
  145. * Get the initial values for AMR, IAMR and UAMOR and communicate them
  146. * to the child.
  147. */
  148. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  149. PARENT_SKIP_IF_UNSUPPORTED(ret, &info->child_sync);
  150. PARENT_FAIL_IF(ret, &info->child_sync);
  151. info->amr1 = info->amr2 = regs[0];
  152. info->expected_iamr = regs[1];
  153. info->expected_uamor = regs[2];
  154. /* Wake up child so that it can set itself up. */
  155. ret = prod_child(&info->child_sync);
  156. PARENT_FAIL_IF(ret, &info->child_sync);
  157. ret = wait_child(&info->child_sync);
  158. if (ret)
  159. return ret;
  160. /* Verify that we can read the pkey registers from the child. */
  161. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  162. PARENT_FAIL_IF(ret, &info->child_sync);
  163. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  164. ptrace_read_running, regs[0], regs[1], regs[2]);
  165. PARENT_FAIL_IF(regs[0] != info->amr1, &info->child_sync);
  166. PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
  167. PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
  168. /* Write valid AMR value in child. */
  169. ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->amr2, 1);
  170. PARENT_FAIL_IF(ret, &info->child_sync);
  171. printf("%-30s AMR: %016lx\n", ptrace_write_running, info->amr2);
  172. /* Wake up child so that it can verify it changed. */
  173. ret = prod_child(&info->child_sync);
  174. PARENT_FAIL_IF(ret, &info->child_sync);
  175. ret = wait_child(&info->child_sync);
  176. if (ret)
  177. return ret;
  178. /* Write invalid AMR value in child. */
  179. ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->invalid_amr, 1);
  180. PARENT_FAIL_IF(ret, &info->child_sync);
  181. printf("%-30s AMR: %016lx\n", ptrace_write_running, info->invalid_amr);
  182. /* Wake up child so that it can verify it didn't change. */
  183. ret = prod_child(&info->child_sync);
  184. PARENT_FAIL_IF(ret, &info->child_sync);
  185. ret = wait_child(&info->child_sync);
  186. if (ret)
  187. return ret;
  188. /* Try to write to IAMR. */
  189. regs[0] = info->amr1;
  190. regs[1] = info->invalid_iamr;
  191. ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 2);
  192. PARENT_FAIL_IF(!ret, &info->child_sync);
  193. printf("%-30s AMR: %016lx IAMR: %016lx\n",
  194. ptrace_write_running, regs[0], regs[1]);
  195. /* Try to write to IAMR and UAMOR. */
  196. regs[2] = info->invalid_uamor;
  197. ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 3);
  198. PARENT_FAIL_IF(!ret, &info->child_sync);
  199. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  200. ptrace_write_running, regs[0], regs[1], regs[2]);
  201. /* Verify that all registers still have their expected values. */
  202. ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
  203. PARENT_FAIL_IF(ret, &info->child_sync);
  204. printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
  205. ptrace_read_running, regs[0], regs[1], regs[2]);
  206. PARENT_FAIL_IF(regs[0] != info->amr2, &info->child_sync);
  207. PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
  208. PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
  209. /* Wake up child so that it can verify AMR didn't change and wrap up. */
  210. ret = prod_child(&info->child_sync);
  211. PARENT_FAIL_IF(ret, &info->child_sync);
  212. ret = wait(&status);
  213. if (ret != pid) {
  214. printf("Child's exit status not captured\n");
  215. ret = TEST_PASS;
  216. } else if (!WIFEXITED(status)) {
  217. printf("Child exited abnormally\n");
  218. ret = TEST_FAIL;
  219. } else
  220. ret = WEXITSTATUS(status) ? TEST_FAIL : TEST_PASS;
  221. return ret;
  222. }
  223. static int ptrace_pkey(void)
  224. {
  225. struct shared_info *info;
  226. int shm_id;
  227. int ret;
  228. pid_t pid;
  229. shm_id = shmget(IPC_PRIVATE, sizeof(*info), 0777 | IPC_CREAT);
  230. info = shmat(shm_id, NULL, 0);
  231. ret = init_child_sync(&info->child_sync);
  232. if (ret)
  233. return ret;
  234. pid = fork();
  235. if (pid < 0) {
  236. perror("fork() failed");
  237. ret = TEST_FAIL;
  238. } else if (pid == 0)
  239. ret = child(info);
  240. else
  241. ret = parent(info, pid);
  242. shmdt(info);
  243. if (pid) {
  244. destroy_child_sync(&info->child_sync);
  245. shmctl(shm_id, IPC_RMID, NULL);
  246. }
  247. return ret;
  248. }
  249. int main(int argc, char *argv[])
  250. {
  251. return test_harness(ptrace_pkey, "ptrace_pkey");
  252. }