pkeys.c 12 KB

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