]>
Commit | Line | Data |
---|---|---|
62b5f7d0 DH |
1 | /* |
2 | * Intel Memory Protection Keys management | |
3 | * Copyright (c) 2015, Intel Corporation. | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | */ | |
14 | #include <linux/mm_types.h> /* mm_struct, vma, etc... */ | |
15 | #include <linux/pkeys.h> /* PKEY_* */ | |
16 | #include <uapi/asm-generic/mman-common.h> | |
17 | ||
18 | #include <asm/cpufeature.h> /* boot_cpu_has, ... */ | |
19 | #include <asm/mmu_context.h> /* vma_pkey() */ | |
20 | #include <asm/fpu/internal.h> /* fpregs_active() */ | |
21 | ||
22 | int __execute_only_pkey(struct mm_struct *mm) | |
23 | { | |
e8c24d3a DH |
24 | bool need_to_set_mm_pkey = false; |
25 | int execute_only_pkey = mm->context.execute_only_pkey; | |
62b5f7d0 DH |
26 | int ret; |
27 | ||
e8c24d3a DH |
28 | /* Do we need to assign a pkey for mm's execute-only maps? */ |
29 | if (execute_only_pkey == -1) { | |
30 | /* Go allocate one to use, which might fail */ | |
31 | execute_only_pkey = mm_pkey_alloc(mm); | |
32 | if (execute_only_pkey < 0) | |
33 | return -1; | |
34 | need_to_set_mm_pkey = true; | |
35 | } | |
36 | ||
62b5f7d0 DH |
37 | /* |
38 | * We do not want to go through the relatively costly | |
39 | * dance to set PKRU if we do not need to. Check it | |
40 | * first and assume that if the execute-only pkey is | |
41 | * write-disabled that we do not have to set it | |
42 | * ourselves. We need preempt off so that nobody | |
43 | * can make fpregs inactive. | |
44 | */ | |
45 | preempt_disable(); | |
e8c24d3a DH |
46 | if (!need_to_set_mm_pkey && |
47 | fpregs_active() && | |
48 | !__pkru_allows_read(read_pkru(), execute_only_pkey)) { | |
62b5f7d0 | 49 | preempt_enable(); |
e8c24d3a | 50 | return execute_only_pkey; |
62b5f7d0 DH |
51 | } |
52 | preempt_enable(); | |
e8c24d3a DH |
53 | |
54 | /* | |
55 | * Set up PKRU so that it denies access for everything | |
56 | * other than execution. | |
57 | */ | |
58 | ret = arch_set_user_pkey_access(current, execute_only_pkey, | |
62b5f7d0 DH |
59 | PKEY_DISABLE_ACCESS); |
60 | /* | |
61 | * If the PKRU-set operation failed somehow, just return | |
62 | * 0 and effectively disable execute-only support. | |
63 | */ | |
e8c24d3a DH |
64 | if (ret) { |
65 | mm_set_pkey_free(mm, execute_only_pkey); | |
66 | return -1; | |
67 | } | |
62b5f7d0 | 68 | |
e8c24d3a DH |
69 | /* We got one, store it and use it from here on out */ |
70 | if (need_to_set_mm_pkey) | |
71 | mm->context.execute_only_pkey = execute_only_pkey; | |
72 | return execute_only_pkey; | |
62b5f7d0 DH |
73 | } |
74 | ||
75 | static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma) | |
76 | { | |
77 | /* Do this check first since the vm_flags should be hot */ | |
78 | if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC) | |
79 | return false; | |
e8c24d3a | 80 | if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey) |
62b5f7d0 DH |
81 | return false; |
82 | ||
83 | return true; | |
84 | } | |
85 | ||
86 | /* | |
87 | * This is only called for *plain* mprotect calls. | |
88 | */ | |
89 | int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey) | |
90 | { | |
91 | /* | |
92 | * Is this an mprotect_pkey() call? If so, never | |
93 | * override the value that came from the user. | |
94 | */ | |
95 | if (pkey != -1) | |
96 | return pkey; | |
97 | /* | |
98 | * Look for a protection-key-drive execute-only mapping | |
99 | * which is now being given permissions that are not | |
100 | * execute-only. Move it back to the default pkey. | |
101 | */ | |
102 | if (vma_is_pkey_exec_only(vma) && | |
103 | (prot & (PROT_READ|PROT_WRITE))) { | |
104 | return 0; | |
105 | } | |
106 | /* | |
107 | * The mapping is execute-only. Go try to get the | |
108 | * execute-only protection key. If we fail to do that, | |
109 | * fall through as if we do not have execute-only | |
110 | * support. | |
111 | */ | |
112 | if (prot == PROT_EXEC) { | |
113 | pkey = execute_only_pkey(vma->vm_mm); | |
114 | if (pkey > 0) | |
115 | return pkey; | |
116 | } | |
117 | /* | |
118 | * This is a vanilla, non-pkey mprotect (or we failed to | |
119 | * setup execute-only), inherit the pkey from the VMA we | |
120 | * are working on. | |
121 | */ | |
122 | return vma_pkey(vma); | |
123 | } |