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