2 * Intel Memory Protection Keys management
3 * Copyright (c) 2015, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/mm_types.h> /* mm_struct, vma, etc... */
15 #include <linux/pkeys.h> /* PKEY_* */
16 #include <uapi/asm-generic/mman-common.h>
18 #include <asm/cpufeature.h> /* boot_cpu_has, ... */
19 #include <asm/mmu_context.h> /* vma_pkey() */
20 #include <asm/fpu/internal.h> /* fpregs_active() */
22 int __execute_only_pkey(struct mm_struct
*mm
)
27 * We do not want to go through the relatively costly
28 * dance to set PKRU if we do not need to. Check it
29 * first and assume that if the execute-only pkey is
30 * write-disabled that we do not have to set it
31 * ourselves. We need preempt off so that nobody
32 * can make fpregs inactive.
35 if (fpregs_active() &&
36 !__pkru_allows_read(read_pkru(), PKEY_DEDICATED_EXECUTE_ONLY
)) {
38 return PKEY_DEDICATED_EXECUTE_ONLY
;
41 ret
= arch_set_user_pkey_access(current
, PKEY_DEDICATED_EXECUTE_ONLY
,
44 * If the PKRU-set operation failed somehow, just return
45 * 0 and effectively disable execute-only support.
50 return PKEY_DEDICATED_EXECUTE_ONLY
;
53 static inline bool vma_is_pkey_exec_only(struct vm_area_struct
*vma
)
55 /* Do this check first since the vm_flags should be hot */
56 if ((vma
->vm_flags
& (VM_READ
| VM_WRITE
| VM_EXEC
)) != VM_EXEC
)
58 if (vma_pkey(vma
) != PKEY_DEDICATED_EXECUTE_ONLY
)
65 * This is only called for *plain* mprotect calls.
67 int __arch_override_mprotect_pkey(struct vm_area_struct
*vma
, int prot
, int pkey
)
70 * Is this an mprotect_pkey() call? If so, never
71 * override the value that came from the user.
76 * Look for a protection-key-drive execute-only mapping
77 * which is now being given permissions that are not
78 * execute-only. Move it back to the default pkey.
80 if (vma_is_pkey_exec_only(vma
) &&
81 (prot
& (PROT_READ
|PROT_WRITE
))) {
85 * The mapping is execute-only. Go try to get the
86 * execute-only protection key. If we fail to do that,
87 * fall through as if we do not have execute-only
90 if (prot
== PROT_EXEC
) {
91 pkey
= execute_only_pkey(vma
->vm_mm
);
96 * This is a vanilla, non-pkey mprotect (or we failed to
97 * setup execute-only), inherit the pkey from the VMA we
100 return vma_pkey(vma
);