powerpc/pkeys: Deny read/write/execute by default
[sfrench/cifs-2.6.git] / arch / powerpc / mm / pkeys.c
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/setup.h>
10 #include <linux/pkeys.h>
11 #include <linux/of_device.h>
12
13 DEFINE_STATIC_KEY_TRUE(pkey_disabled);
14 bool pkey_execute_disable_supported;
15 int  pkeys_total;               /* Total pkeys as per device tree */
16 bool pkeys_devtree_defined;     /* pkey property exported by device tree */
17 u32  initial_allocation_mask;   /* Bits set for reserved keys */
18 u64  pkey_amr_mask;             /* Bits in AMR not to be touched */
19 u64  pkey_iamr_mask;            /* Bits in AMR not to be touched */
20 u64  pkey_uamor_mask;           /* Bits in UMOR not to be touched */
21
22 #define AMR_BITS_PER_PKEY 2
23 #define AMR_RD_BIT 0x1UL
24 #define AMR_WR_BIT 0x2UL
25 #define IAMR_EX_BIT 0x1UL
26 #define PKEY_REG_BITS (sizeof(u64)*8)
27 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
28
29 static void scan_pkey_feature(void)
30 {
31         u32 vals[2];
32         struct device_node *cpu;
33
34         cpu = of_find_node_by_type(NULL, "cpu");
35         if (!cpu)
36                 return;
37
38         if (of_property_read_u32_array(cpu,
39                         "ibm,processor-storage-keys", vals, 2))
40                 return;
41
42         /*
43          * Since any pkey can be used for data or execute, we will just treat
44          * all keys as equal and track them as one entity.
45          */
46         pkeys_total = be32_to_cpu(vals[0]);
47         pkeys_devtree_defined = true;
48 }
49
50 static inline bool pkey_mmu_enabled(void)
51 {
52         if (firmware_has_feature(FW_FEATURE_LPAR))
53                 return pkeys_total;
54         else
55                 return cpu_has_feature(CPU_FTR_PKEY);
56 }
57
58 int pkey_initialize(void)
59 {
60         int os_reserved, i;
61
62         /*
63          * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
64          * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
65          * Ensure that the bits a distinct.
66          */
67         BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
68                      (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
69
70         /*
71          * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
72          * in the vmaflag. Make sure that is really the case.
73          */
74         BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
75                      __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
76                                 != (sizeof(u64) * BITS_PER_BYTE));
77
78         /* scan the device tree for pkey feature */
79         scan_pkey_feature();
80
81         /*
82          * Let's assume 32 pkeys on P8 bare metal, if its not defined by device
83          * tree. We make this exception since skiboot forgot to expose this
84          * property on power8.
85          */
86         if (!pkeys_devtree_defined && !firmware_has_feature(FW_FEATURE_LPAR) &&
87                         cpu_has_feature(CPU_FTRS_POWER8))
88                 pkeys_total = 32;
89
90         /*
91          * Adjust the upper limit, based on the number of bits supported by
92          * arch-neutral code.
93          */
94         pkeys_total = min_t(int, pkeys_total,
95                         (ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT));
96
97         if (!pkey_mmu_enabled() || radix_enabled() || !pkeys_total)
98                 static_branch_enable(&pkey_disabled);
99         else
100                 static_branch_disable(&pkey_disabled);
101
102         if (static_branch_likely(&pkey_disabled))
103                 return 0;
104
105         /*
106          * The device tree cannot be relied to indicate support for
107          * execute_disable support. Instead we use a PVR check.
108          */
109         if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
110                 pkey_execute_disable_supported = false;
111         else
112                 pkey_execute_disable_supported = true;
113
114 #ifdef CONFIG_PPC_4K_PAGES
115         /*
116          * The OS can manage only 8 pkeys due to its inability to represent them
117          * in the Linux 4K PTE.
118          */
119         os_reserved = pkeys_total - 8;
120 #else
121         os_reserved = 0;
122 #endif
123         initial_allocation_mask  = (0x1 << 0) | (0x1 << 1);
124
125         /* register mask is in BE format */
126         pkey_amr_mask = ~0x0ul;
127         pkey_amr_mask &= ~(0x3ul << pkeyshift(0));
128
129         pkey_iamr_mask = ~0x0ul;
130         pkey_iamr_mask &= ~(0x3ul << pkeyshift(0));
131
132         pkey_uamor_mask = ~0x0ul;
133         pkey_uamor_mask &= ~(0x3ul << pkeyshift(0));
134
135         /* mark the rest of the keys as reserved and hence unavailable */
136         for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) {
137                 initial_allocation_mask |= (0x1 << i);
138                 pkey_uamor_mask &= ~(0x3ul << pkeyshift(i));
139         }
140
141         return 0;
142 }
143
144 arch_initcall(pkey_initialize);
145
146 void pkey_mm_init(struct mm_struct *mm)
147 {
148         if (static_branch_likely(&pkey_disabled))
149                 return;
150         mm_pkey_allocation_map(mm) = initial_allocation_mask;
151         /* -1 means unallocated or invalid */
152         mm->context.execute_only_pkey = -1;
153 }
154
155 static inline u64 read_amr(void)
156 {
157         return mfspr(SPRN_AMR);
158 }
159
160 static inline void write_amr(u64 value)
161 {
162         mtspr(SPRN_AMR, value);
163 }
164
165 static inline u64 read_iamr(void)
166 {
167         if (!likely(pkey_execute_disable_supported))
168                 return 0x0UL;
169
170         return mfspr(SPRN_IAMR);
171 }
172
173 static inline void write_iamr(u64 value)
174 {
175         if (!likely(pkey_execute_disable_supported))
176                 return;
177
178         mtspr(SPRN_IAMR, value);
179 }
180
181 static inline u64 read_uamor(void)
182 {
183         return mfspr(SPRN_UAMOR);
184 }
185
186 static inline void write_uamor(u64 value)
187 {
188         mtspr(SPRN_UAMOR, value);
189 }
190
191 static bool is_pkey_enabled(int pkey)
192 {
193         u64 uamor = read_uamor();
194         u64 pkey_bits = 0x3ul << pkeyshift(pkey);
195         u64 uamor_pkey_bits = (uamor & pkey_bits);
196
197         /*
198          * Both the bits in UAMOR corresponding to the key should be set or
199          * reset.
200          */
201         WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits));
202         return !!(uamor_pkey_bits);
203 }
204
205 static inline void init_amr(int pkey, u8 init_bits)
206 {
207         u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
208         u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
209
210         write_amr(old_amr | new_amr_bits);
211 }
212
213 static inline void init_iamr(int pkey, u8 init_bits)
214 {
215         u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
216         u64 old_iamr = read_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
217
218         write_iamr(old_iamr | new_iamr_bits);
219 }
220
221 static void pkey_status_change(int pkey, bool enable)
222 {
223         u64 old_uamor;
224
225         /* Reset the AMR and IAMR bits for this key */
226         init_amr(pkey, 0x0);
227         init_iamr(pkey, 0x0);
228
229         /* Enable/disable key */
230         old_uamor = read_uamor();
231         if (enable)
232                 old_uamor |= (0x3ul << pkeyshift(pkey));
233         else
234                 old_uamor &= ~(0x3ul << pkeyshift(pkey));
235         write_uamor(old_uamor);
236 }
237
238 void __arch_activate_pkey(int pkey)
239 {
240         pkey_status_change(pkey, true);
241 }
242
243 void __arch_deactivate_pkey(int pkey)
244 {
245         pkey_status_change(pkey, false);
246 }
247
248 /*
249  * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
250  * specified in @init_val.
251  */
252 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
253                                 unsigned long init_val)
254 {
255         u64 new_amr_bits = 0x0ul;
256         u64 new_iamr_bits = 0x0ul;
257
258         if (!is_pkey_enabled(pkey))
259                 return -EINVAL;
260
261         if (init_val & PKEY_DISABLE_EXECUTE) {
262                 if (!pkey_execute_disable_supported)
263                         return -EINVAL;
264                 new_iamr_bits |= IAMR_EX_BIT;
265         }
266         init_iamr(pkey, new_iamr_bits);
267
268         /* Set the bits we need in AMR: */
269         if (init_val & PKEY_DISABLE_ACCESS)
270                 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
271         else if (init_val & PKEY_DISABLE_WRITE)
272                 new_amr_bits |= AMR_WR_BIT;
273
274         init_amr(pkey, new_amr_bits);
275         return 0;
276 }
277
278 void thread_pkey_regs_save(struct thread_struct *thread)
279 {
280         if (static_branch_likely(&pkey_disabled))
281                 return;
282
283         /*
284          * TODO: Skip saving registers if @thread hasn't used any keys yet.
285          */
286         thread->amr = read_amr();
287         thread->iamr = read_iamr();
288         thread->uamor = read_uamor();
289 }
290
291 void thread_pkey_regs_restore(struct thread_struct *new_thread,
292                               struct thread_struct *old_thread)
293 {
294         if (static_branch_likely(&pkey_disabled))
295                 return;
296
297         if (old_thread->amr != new_thread->amr)
298                 write_amr(new_thread->amr);
299         if (old_thread->iamr != new_thread->iamr)
300                 write_iamr(new_thread->iamr);
301         if (old_thread->uamor != new_thread->uamor)
302                 write_uamor(new_thread->uamor);
303 }
304
305 void thread_pkey_regs_init(struct thread_struct *thread)
306 {
307         if (static_branch_likely(&pkey_disabled))
308                 return;
309
310         thread->amr = pkey_amr_mask;
311         thread->iamr = pkey_iamr_mask;
312         thread->uamor = pkey_uamor_mask;
313
314         write_uamor(pkey_uamor_mask);
315         write_amr(pkey_amr_mask);
316         write_iamr(pkey_iamr_mask);
317 }
318
319 static inline bool pkey_allows_readwrite(int pkey)
320 {
321         int pkey_shift = pkeyshift(pkey);
322
323         if (!is_pkey_enabled(pkey))
324                 return true;
325
326         return !(read_amr() & ((AMR_RD_BIT|AMR_WR_BIT) << pkey_shift));
327 }
328
329 int __execute_only_pkey(struct mm_struct *mm)
330 {
331         bool need_to_set_mm_pkey = false;
332         int execute_only_pkey = mm->context.execute_only_pkey;
333         int ret;
334
335         /* Do we need to assign a pkey for mm's execute-only maps? */
336         if (execute_only_pkey == -1) {
337                 /* Go allocate one to use, which might fail */
338                 execute_only_pkey = mm_pkey_alloc(mm);
339                 if (execute_only_pkey < 0)
340                         return -1;
341                 need_to_set_mm_pkey = true;
342         }
343
344         /*
345          * We do not want to go through the relatively costly dance to set AMR
346          * if we do not need to. Check it first and assume that if the
347          * execute-only pkey is readwrite-disabled than we do not have to set it
348          * ourselves.
349          */
350         if (!need_to_set_mm_pkey && !pkey_allows_readwrite(execute_only_pkey))
351                 return execute_only_pkey;
352
353         /*
354          * Set up AMR so that it denies access for everything other than
355          * execution.
356          */
357         ret = __arch_set_user_pkey_access(current, execute_only_pkey,
358                                           PKEY_DISABLE_ACCESS |
359                                           PKEY_DISABLE_WRITE);
360         /*
361          * If the AMR-set operation failed somehow, just return 0 and
362          * effectively disable execute-only support.
363          */
364         if (ret) {
365                 mm_pkey_free(mm, execute_only_pkey);
366                 return -1;
367         }
368
369         /* We got one, store it and use it from here on out */
370         if (need_to_set_mm_pkey)
371                 mm->context.execute_only_pkey = execute_only_pkey;
372         return execute_only_pkey;
373 }
374
375 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
376 {
377         /* Do this check first since the vm_flags should be hot */
378         if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
379                 return false;
380
381         return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
382 }
383
384 /*
385  * This should only be called for *plain* mprotect calls.
386  */
387 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
388                                   int pkey)
389 {
390         /*
391          * If the currently associated pkey is execute-only, but the requested
392          * protection is not execute-only, move it back to the default pkey.
393          */
394         if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
395                 return 0;
396
397         /*
398          * The requested protection is execute-only. Hence let's use an
399          * execute-only pkey.
400          */
401         if (prot == PROT_EXEC) {
402                 pkey = execute_only_pkey(vma->vm_mm);
403                 if (pkey > 0)
404                         return pkey;
405         }
406
407         /* Nothing to override. */
408         return vma_pkey(vma);
409 }
410
411 static bool pkey_access_permitted(int pkey, bool write, bool execute)
412 {
413         int pkey_shift;
414         u64 amr;
415
416         if (!pkey)
417                 return true;
418
419         if (!is_pkey_enabled(pkey))
420                 return true;
421
422         pkey_shift = pkeyshift(pkey);
423         if (execute && !(read_iamr() & (IAMR_EX_BIT << pkey_shift)))
424                 return true;
425
426         amr = read_amr(); /* Delay reading amr until absolutely needed */
427         return ((!write && !(amr & (AMR_RD_BIT << pkey_shift))) ||
428                 (write &&  !(amr & (AMR_WR_BIT << pkey_shift))));
429 }
430
431 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
432 {
433         if (static_branch_likely(&pkey_disabled))
434                 return true;
435
436         return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
437 }
438
439 /*
440  * We only want to enforce protection keys on the current thread because we
441  * effectively have no access to AMR/IAMR for other threads or any way to tell
442  * which AMR/IAMR in a threaded process we could use.
443  *
444  * So do not enforce things if the VMA is not from the current mm, or if we are
445  * in a kernel thread.
446  */
447 static inline bool vma_is_foreign(struct vm_area_struct *vma)
448 {
449         if (!current->mm)
450                 return true;
451
452         /* if it is not our ->mm, it has to be foreign */
453         if (current->mm != vma->vm_mm)
454                 return true;
455
456         return false;
457 }
458
459 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
460                                bool execute, bool foreign)
461 {
462         if (static_branch_likely(&pkey_disabled))
463                 return true;
464         /*
465          * Do not enforce our key-permissions on a foreign vma.
466          */
467         if (foreign || vma_is_foreign(vma))
468                 return true;
469
470         return pkey_access_permitted(vma_pkey(vma), write, execute);
471 }