blob: ddc54949078aa1d5db6a8b1f4fb511c122368385 [file] [log] [blame]
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001/*
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
22int __execute_only_pkey(struct mm_struct *mm)
23{
Dave Hansene8c24d32016-07-29 09:30:15 -070024 bool need_to_set_mm_pkey = false;
25 int execute_only_pkey = mm->context.execute_only_pkey;
Dave Hansen62b5f7d2016-02-12 13:02:40 -080026 int ret;
27
Dave Hansene8c24d32016-07-29 09:30:15 -070028 /* 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
Dave Hansen62b5f7d2016-02-12 13:02:40 -080037 /*
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();
Dave Hansene8c24d32016-07-29 09:30:15 -070046 if (!need_to_set_mm_pkey &&
47 fpregs_active() &&
48 !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
Dave Hansen62b5f7d2016-02-12 13:02:40 -080049 preempt_enable();
Dave Hansene8c24d32016-07-29 09:30:15 -070050 return execute_only_pkey;
Dave Hansen62b5f7d2016-02-12 13:02:40 -080051 }
52 preempt_enable();
Dave Hansene8c24d32016-07-29 09:30:15 -070053
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,
Dave Hansen62b5f7d2016-02-12 13:02:40 -080059 PKEY_DISABLE_ACCESS);
60 /*
61 * If the PKRU-set operation failed somehow, just return
62 * 0 and effectively disable execute-only support.
63 */
Dave Hansene8c24d32016-07-29 09:30:15 -070064 if (ret) {
65 mm_set_pkey_free(mm, execute_only_pkey);
66 return -1;
67 }
Dave Hansen62b5f7d2016-02-12 13:02:40 -080068
Dave Hansene8c24d32016-07-29 09:30:15 -070069 /* 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;
Dave Hansen62b5f7d2016-02-12 13:02:40 -080073}
74
75static 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;
Dave Hansene8c24d32016-07-29 09:30:15 -070080 if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
Dave Hansen62b5f7d2016-02-12 13:02:40 -080081 return false;
82
83 return true;
84}
85
86/*
87 * This is only called for *plain* mprotect calls.
88 */
89int __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}
Dave Hansenacd547b2016-07-29 09:30:21 -0700124
125#define PKRU_AD_KEY(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
126
127/*
128 * Make the default PKRU value (at execve() time) as restrictive
129 * as possible. This ensures that any threads clone()'d early
130 * in the process's lifetime will not accidentally get access
131 * to data which is pkey-protected later on.
132 */
133u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
134 PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) |
135 PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
136 PKRU_AD_KEY(10) | PKRU_AD_KEY(11) | PKRU_AD_KEY(12) |
137 PKRU_AD_KEY(13) | PKRU_AD_KEY(14) | PKRU_AD_KEY(15);
138
139/*
140 * Called from the FPU code when creating a fresh set of FPU
141 * registers. This is called from a very specific context where
142 * we know the FPU regstiers are safe for use and we can use PKRU
143 * directly. The fact that PKRU is only available when we are
144 * using eagerfpu mode makes this possible.
145 */
146void copy_init_pkru_to_fpregs(void)
147{
148 u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
149 /*
150 * Any write to PKRU takes it out of the XSAVE 'init
151 * state' which increases context switch cost. Avoid
152 * writing 0 when PKRU was already 0.
153 */
154 if (!init_pkru_value_snapshot && !read_pkru())
155 return;
156 /*
157 * Override the PKRU state that came from 'init_fpstate'
158 * with the baseline from the process.
159 */
160 write_pkru(init_pkru_value_snapshot);
161}