1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/debugfs.h>
15#include <linux/mm_types.h>
16#include <linux/pkeys.h>
17#include <uapi/asm-generic/mman-common.h>
18
19#include <asm/cpufeature.h>
20#include <asm/mmu_context.h>
21#include <asm/fpu/internal.h>
22
23int __execute_only_pkey(struct mm_struct *mm)
24{
25 bool need_to_set_mm_pkey = false;
26 int execute_only_pkey = mm->context.execute_only_pkey;
27 int ret;
28
29
30 if (execute_only_pkey == -1) {
31
32 execute_only_pkey = mm_pkey_alloc(mm);
33 if (execute_only_pkey < 0)
34 return -1;
35 need_to_set_mm_pkey = true;
36 }
37
38
39
40
41
42
43
44
45 if (!need_to_set_mm_pkey &&
46 !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
47 return execute_only_pkey;
48 }
49
50
51
52
53
54 ret = arch_set_user_pkey_access(current, execute_only_pkey,
55 PKEY_DISABLE_ACCESS);
56
57
58
59
60 if (ret) {
61 mm_set_pkey_free(mm, execute_only_pkey);
62 return -1;
63 }
64
65
66 if (need_to_set_mm_pkey)
67 mm->context.execute_only_pkey = execute_only_pkey;
68 return execute_only_pkey;
69}
70
71static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
72{
73
74 if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
75 return false;
76 if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
77 return false;
78
79 return true;
80}
81
82
83
84
85int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey)
86{
87
88
89
90
91 if (pkey != -1)
92 return pkey;
93
94
95
96
97
98
99
100 if (prot == PROT_EXEC) {
101 pkey = execute_only_pkey(vma->vm_mm);
102 if (pkey > 0)
103 return pkey;
104 } else if (vma_is_pkey_exec_only(vma)) {
105
106
107
108
109
110
111 return ARCH_DEFAULT_PKEY;
112 }
113
114
115
116
117
118
119 return vma_pkey(vma);
120}
121
122#define PKRU_AD_KEY(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
123
124
125
126
127
128
129
130u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
131 PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) |
132 PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
133 PKRU_AD_KEY(10) | PKRU_AD_KEY(11) | PKRU_AD_KEY(12) |
134 PKRU_AD_KEY(13) | PKRU_AD_KEY(14) | PKRU_AD_KEY(15);
135
136
137
138
139
140
141
142void copy_init_pkru_to_fpregs(void)
143{
144 u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
145
146
147
148
149 write_pkru(init_pkru_value_snapshot);
150}
151
152static ssize_t init_pkru_read_file(struct file *file, char __user *user_buf,
153 size_t count, loff_t *ppos)
154{
155 char buf[32];
156 unsigned int len;
157
158 len = sprintf(buf, "0x%x\n", init_pkru_value);
159 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
160}
161
162static ssize_t init_pkru_write_file(struct file *file,
163 const char __user *user_buf, size_t count, loff_t *ppos)
164{
165 struct pkru_state *pk;
166 char buf[32];
167 ssize_t len;
168 u32 new_init_pkru;
169
170 len = min(count, sizeof(buf) - 1);
171 if (copy_from_user(buf, user_buf, len))
172 return -EFAULT;
173
174
175 buf[len] = '\0';
176 if (kstrtouint(buf, 0, &new_init_pkru))
177 return -EINVAL;
178
179
180
181
182
183
184 if (new_init_pkru & (PKRU_AD_BIT|PKRU_WD_BIT))
185 return -EINVAL;
186
187 WRITE_ONCE(init_pkru_value, new_init_pkru);
188 pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
189 if (!pk)
190 return -EINVAL;
191 pk->pkru = new_init_pkru;
192 return count;
193}
194
195static const struct file_operations fops_init_pkru = {
196 .read = init_pkru_read_file,
197 .write = init_pkru_write_file,
198 .llseek = default_llseek,
199};
200
201static int __init create_init_pkru_value(void)
202{
203 debugfs_create_file("init_pkru", S_IRUSR | S_IWUSR,
204 arch_debugfs_dir, NULL, &fops_init_pkru);
205 return 0;
206}
207late_initcall(create_init_pkru_value);
208
209static __init int setup_init_pkru(char *opt)
210{
211 u32 new_init_pkru;
212
213 if (kstrtouint(opt, 0, &new_init_pkru))
214 return 1;
215
216 WRITE_ONCE(init_pkru_value, new_init_pkru);
217
218 return 1;
219}
220__setup("init_pkru=", setup_init_pkru);
221