pkeys.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PowerPC Memory Protection Keys management
  4. *
  5. * Copyright 2017, Ram Pai, IBM Corporation.
  6. */
  7. #include <asm/mman.h>
  8. #include <asm/mmu_context.h>
  9. #include <asm/mmu.h>
  10. #include <asm/setup.h>
  11. #include <asm/smp.h>
  12. #include <asm/firmware.h>
  13. #include <linux/pkeys.h>
  14. #include <linux/of_fdt.h>
  15. int num_pkey; /* Max number of pkeys supported */
  16. /*
  17. * Keys marked in the reservation list cannot be allocated by userspace
  18. */
  19. u32 reserved_allocation_mask __ro_after_init;
  20. /* Bits set for the initially allocated keys */
  21. static u32 initial_allocation_mask __ro_after_init;
  22. /*
  23. * Even if we allocate keys with sys_pkey_alloc(), we need to make sure
  24. * other thread still find the access denied using the same keys.
  25. */
  26. u64 default_amr __ro_after_init = ~0x0UL;
  27. u64 default_iamr __ro_after_init = 0x5555555555555555UL;
  28. u64 default_uamor __ro_after_init;
  29. EXPORT_SYMBOL(default_amr);
  30. /*
  31. * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
  32. * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
  33. */
  34. static int execute_only_key = 2;
  35. static bool pkey_execute_disable_supported;
  36. #define AMR_BITS_PER_PKEY 2
  37. #define AMR_RD_BIT 0x1UL
  38. #define AMR_WR_BIT 0x2UL
  39. #define IAMR_EX_BIT 0x1UL
  40. #define PKEY_REG_BITS (sizeof(u64) * 8)
  41. #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
  42. static int __init dt_scan_storage_keys(unsigned long node,
  43. const char *uname, int depth,
  44. void *data)
  45. {
  46. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  47. const __be32 *prop;
  48. int *pkeys_total = (int *) data;
  49. /* We are scanning "cpu" nodes only */
  50. if (type == NULL || strcmp(type, "cpu") != 0)
  51. return 0;
  52. prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
  53. if (!prop)
  54. return 0;
  55. *pkeys_total = be32_to_cpu(prop[0]);
  56. return 1;
  57. }
  58. static int __init scan_pkey_feature(void)
  59. {
  60. int ret;
  61. int pkeys_total = 0;
  62. /*
  63. * Pkey is not supported with Radix translation.
  64. */
  65. if (early_radix_enabled())
  66. return 0;
  67. ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
  68. if (ret == 0) {
  69. /*
  70. * Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
  71. * tree. We make this exception since some version of skiboot forgot to
  72. * expose this property on power8/9.
  73. */
  74. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  75. unsigned long pvr = mfspr(SPRN_PVR);
  76. if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
  77. PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
  78. pkeys_total = 32;
  79. }
  80. }
  81. #ifdef CONFIG_PPC_MEM_KEYS
  82. /*
  83. * Adjust the upper limit, based on the number of bits supported by
  84. * arch-neutral code.
  85. */
  86. pkeys_total = min_t(int, pkeys_total,
  87. ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
  88. #endif
  89. return pkeys_total;
  90. }
  91. void __init pkey_early_init_devtree(void)
  92. {
  93. int pkeys_total, i;
  94. #ifdef CONFIG_PPC_MEM_KEYS
  95. /*
  96. * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
  97. * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
  98. * Ensure that the bits a distinct.
  99. */
  100. BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
  101. (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
  102. /*
  103. * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
  104. * in the vmaflag. Make sure that is really the case.
  105. */
  106. BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
  107. __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
  108. != (sizeof(u64) * BITS_PER_BYTE));
  109. #endif
  110. /*
  111. * Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
  112. */
  113. if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
  114. return;
  115. /* scan the device tree for pkey feature */
  116. pkeys_total = scan_pkey_feature();
  117. if (!pkeys_total)
  118. goto out;
  119. /* Allow all keys to be modified by default */
  120. default_uamor = ~0x0UL;
  121. cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
  122. /*
  123. * The device tree cannot be relied to indicate support for
  124. * execute_disable support. Instead we use a PVR check.
  125. */
  126. if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
  127. pkey_execute_disable_supported = false;
  128. else
  129. pkey_execute_disable_supported = true;
  130. #ifdef CONFIG_PPC_4K_PAGES
  131. /*
  132. * The OS can manage only 8 pkeys due to its inability to represent them
  133. * in the Linux 4K PTE. Mark all other keys reserved.
  134. */
  135. num_pkey = min(8, pkeys_total);
  136. #else
  137. num_pkey = pkeys_total;
  138. #endif
  139. if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
  140. /*
  141. * Insufficient number of keys to support
  142. * execute only key. Mark it unavailable.
  143. */
  144. execute_only_key = -1;
  145. } else {
  146. /*
  147. * Mark the execute_only_pkey as not available for
  148. * user allocation via pkey_alloc.
  149. */
  150. reserved_allocation_mask |= (0x1 << execute_only_key);
  151. /*
  152. * Deny READ/WRITE for execute_only_key.
  153. * Allow execute in IAMR.
  154. */
  155. default_amr |= (0x3ul << pkeyshift(execute_only_key));
  156. default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
  157. /*
  158. * Clear the uamor bits for this key.
  159. */
  160. default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
  161. }
  162. if (unlikely(num_pkey <= 3)) {
  163. /*
  164. * Insufficient number of keys to support
  165. * KUAP/KUEP feature.
  166. */
  167. disable_kuep = true;
  168. disable_kuap = true;
  169. WARN(1, "Disabling kernel user protection due to low (%d) max supported keys\n", num_pkey);
  170. } else {
  171. /* handle key which is used by kernel for KAUP */
  172. reserved_allocation_mask |= (0x1 << 3);
  173. /*
  174. * Mark access for kup_key in default amr so that
  175. * we continue to operate with that AMR in
  176. * copy_to/from_user().
  177. */
  178. default_amr &= ~(0x3ul << pkeyshift(3));
  179. default_iamr &= ~(0x1ul << pkeyshift(3));
  180. default_uamor &= ~(0x3ul << pkeyshift(3));
  181. }
  182. /*
  183. * Allow access for only key 0. And prevent any other modification.
  184. */
  185. default_amr &= ~(0x3ul << pkeyshift(0));
  186. default_iamr &= ~(0x1ul << pkeyshift(0));
  187. default_uamor &= ~(0x3ul << pkeyshift(0));
  188. /*
  189. * key 0 is special in that we want to consider it an allocated
  190. * key which is preallocated. We don't allow changing AMR bits
  191. * w.r.t key 0. But one can pkey_free(key0)
  192. */
  193. initial_allocation_mask |= (0x1 << 0);
  194. /*
  195. * key 1 is recommended not to be used. PowerISA(3.0) page 1015,
  196. * programming note.
  197. */
  198. reserved_allocation_mask |= (0x1 << 1);
  199. default_uamor &= ~(0x3ul << pkeyshift(1));
  200. /*
  201. * Prevent the usage of OS reserved keys. Update UAMOR
  202. * for those keys. Also mark the rest of the bits in the
  203. * 32 bit mask as reserved.
  204. */
  205. for (i = num_pkey; i < 32 ; i++) {
  206. reserved_allocation_mask |= (0x1 << i);
  207. default_uamor &= ~(0x3ul << pkeyshift(i));
  208. }
  209. /*
  210. * Prevent the allocation of reserved keys too.
  211. */
  212. initial_allocation_mask |= reserved_allocation_mask;
  213. pr_info("Enabling pkeys with max key count %d\n", num_pkey);
  214. out:
  215. /*
  216. * Setup uamor on boot cpu
  217. */
  218. mtspr(SPRN_UAMOR, default_uamor);
  219. return;
  220. }
  221. #ifdef CONFIG_PPC_KUEP
  222. void setup_kuep(bool disabled)
  223. {
  224. if (disabled)
  225. return;
  226. /*
  227. * On hash if PKEY feature is not enabled, disable KUAP too.
  228. */
  229. if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
  230. return;
  231. if (smp_processor_id() == boot_cpuid) {
  232. pr_info("Activating Kernel Userspace Execution Prevention\n");
  233. cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUEP;
  234. }
  235. /*
  236. * Radix always uses key0 of the IAMR to determine if an access is
  237. * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
  238. * fetch.
  239. */
  240. mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
  241. isync();
  242. }
  243. #endif
  244. #ifdef CONFIG_PPC_KUAP
  245. void setup_kuap(bool disabled)
  246. {
  247. if (disabled)
  248. return;
  249. /*
  250. * On hash if PKEY feature is not enabled, disable KUAP too.
  251. */
  252. if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
  253. return;
  254. if (smp_processor_id() == boot_cpuid) {
  255. pr_info("Activating Kernel Userspace Access Prevention\n");
  256. cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUAP;
  257. }
  258. /*
  259. * Set the default kernel AMR values on all cpus.
  260. */
  261. mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
  262. isync();
  263. }
  264. #endif
  265. #ifdef CONFIG_PPC_MEM_KEYS
  266. void pkey_mm_init(struct mm_struct *mm)
  267. {
  268. if (!mmu_has_feature(MMU_FTR_PKEY))
  269. return;
  270. mm_pkey_allocation_map(mm) = initial_allocation_mask;
  271. mm->context.execute_only_pkey = execute_only_key;
  272. }
  273. static inline void init_amr(int pkey, u8 init_bits)
  274. {
  275. u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
  276. u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
  277. current->thread.regs->amr = old_amr | new_amr_bits;
  278. }
  279. static inline void init_iamr(int pkey, u8 init_bits)
  280. {
  281. u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
  282. u64 old_iamr = current_thread_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
  283. if (!likely(pkey_execute_disable_supported))
  284. return;
  285. current->thread.regs->iamr = old_iamr | new_iamr_bits;
  286. }
  287. /*
  288. * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
  289. * specified in @init_val.
  290. */
  291. int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  292. unsigned long init_val)
  293. {
  294. u64 new_amr_bits = 0x0ul;
  295. u64 new_iamr_bits = 0x0ul;
  296. u64 pkey_bits, uamor_pkey_bits;
  297. /*
  298. * Check whether the key is disabled by UAMOR.
  299. */
  300. pkey_bits = 0x3ul << pkeyshift(pkey);
  301. uamor_pkey_bits = (default_uamor & pkey_bits);
  302. /*
  303. * Both the bits in UAMOR corresponding to the key should be set
  304. */
  305. if (uamor_pkey_bits != pkey_bits)
  306. return -EINVAL;
  307. if (init_val & PKEY_DISABLE_EXECUTE) {
  308. if (!pkey_execute_disable_supported)
  309. return -EINVAL;
  310. new_iamr_bits |= IAMR_EX_BIT;
  311. }
  312. init_iamr(pkey, new_iamr_bits);
  313. /* Set the bits we need in AMR: */
  314. if (init_val & PKEY_DISABLE_ACCESS)
  315. new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
  316. else if (init_val & PKEY_DISABLE_WRITE)
  317. new_amr_bits |= AMR_WR_BIT;
  318. init_amr(pkey, new_amr_bits);
  319. return 0;
  320. }
  321. int execute_only_pkey(struct mm_struct *mm)
  322. {
  323. return mm->context.execute_only_pkey;
  324. }
  325. static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
  326. {
  327. /* Do this check first since the vm_flags should be hot */
  328. if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
  329. return false;
  330. return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
  331. }
  332. /*
  333. * This should only be called for *plain* mprotect calls.
  334. */
  335. int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
  336. int pkey)
  337. {
  338. /*
  339. * If the currently associated pkey is execute-only, but the requested
  340. * protection is not execute-only, move it back to the default pkey.
  341. */
  342. if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
  343. return 0;
  344. /*
  345. * The requested protection is execute-only. Hence let's use an
  346. * execute-only pkey.
  347. */
  348. if (prot == PROT_EXEC) {
  349. pkey = execute_only_pkey(vma->vm_mm);
  350. if (pkey > 0)
  351. return pkey;
  352. }
  353. /* Nothing to override. */
  354. return vma_pkey(vma);
  355. }
  356. static bool pkey_access_permitted(int pkey, bool write, bool execute)
  357. {
  358. int pkey_shift;
  359. u64 amr;
  360. pkey_shift = pkeyshift(pkey);
  361. if (execute)
  362. return !(current_thread_iamr() & (IAMR_EX_BIT << pkey_shift));
  363. amr = current_thread_amr();
  364. if (write)
  365. return !(amr & (AMR_WR_BIT << pkey_shift));
  366. return !(amr & (AMR_RD_BIT << pkey_shift));
  367. }
  368. bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
  369. {
  370. if (!mmu_has_feature(MMU_FTR_PKEY))
  371. return true;
  372. return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
  373. }
  374. /*
  375. * We only want to enforce protection keys on the current thread because we
  376. * effectively have no access to AMR/IAMR for other threads or any way to tell
  377. * which AMR/IAMR in a threaded process we could use.
  378. *
  379. * So do not enforce things if the VMA is not from the current mm, or if we are
  380. * in a kernel thread.
  381. */
  382. bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
  383. bool execute, bool foreign)
  384. {
  385. if (!mmu_has_feature(MMU_FTR_PKEY))
  386. return true;
  387. /*
  388. * Do not enforce our key-permissions on a foreign vma.
  389. */
  390. if (foreign || vma_is_foreign(vma))
  391. return true;
  392. return pkey_access_permitted(vma_pkey(vma), write, execute);
  393. }
  394. void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
  395. {
  396. if (!mmu_has_feature(MMU_FTR_PKEY))
  397. return;
  398. /* Duplicate the oldmm pkey state in mm: */
  399. mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
  400. mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
  401. }
  402. #endif /* CONFIG_PPC_MEM_KEYS */