1 /*-
2 * Copyright (c) 2021 The FreeBSD Foundation
3 *
4 * This software was developed by Andrew Turner under sponsorship from
5 * the FreeBSD Foundation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * This manages pointer authentication. As it needs to enable the use of
31 * pointer authentication and change the keys we must built this with
32 * pointer authentication disabled.
33 */
34 #ifdef __ARM_FEATURE_PAC_DEFAULT
35 #error Must be built with pointer authentication disabled
36 #endif
37
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/libkern.h>
42 #include <sys/proc.h>
43 #include <sys/reboot.h>
44
45 #include <machine/armreg.h>
46 #include <machine/cpu.h>
47 #include <machine/reg.h>
48 #include <machine/vmparam.h>
49
50 #define SCTLR_PTRAUTH (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)
51
52 static bool __read_mostly enable_ptrauth = false;
53
54 /* Functions called from assembly. */
55 void ptrauth_start(void);
56 struct thread *ptrauth_switch(struct thread *);
57 void ptrauth_exit_el0(struct thread *);
58 void ptrauth_enter_el0(struct thread *);
59
60 static bool
ptrauth_disable(void)61 ptrauth_disable(void)
62 {
63 const char *family, *maker, *product;
64
65 family = kern_getenv("smbios.system.family");
66 maker = kern_getenv("smbios.system.maker");
67 product = kern_getenv("smbios.system.product");
68 if (family == NULL || maker == NULL || product == NULL)
69 return (false);
70
71 /*
72 * The Dev Kit appears to be configured to trap upon access to PAC
73 * registers, but the kernel boots at EL1 and so we have no way to
74 * inspect or change this configuration. As a workaround, simply
75 * disable PAC on this platform.
76 */
77 if (strcmp(maker, "Microsoft Corporation") == 0 &&
78 strcmp(family, "Surface") == 0 &&
79 strcmp(product, "Windows Dev Kit 2023") == 0)
80 return (true);
81
82 return (false);
83 }
84
85 void
ptrauth_init(void)86 ptrauth_init(void)
87 {
88 uint64_t isar1;
89 int pac_enable;
90
91 /*
92 * Allow the sysadmin to disable pointer authentication globally,
93 * e.g. on broken hardware.
94 */
95 pac_enable = 1;
96 TUNABLE_INT_FETCH("hw.pac.enable", &pac_enable);
97 if (!pac_enable) {
98 if (boothowto & RB_VERBOSE)
99 printf("Pointer authentication is disabled\n");
100 return;
101 }
102
103 if (!get_kernel_reg(ID_AA64ISAR1_EL1, &isar1))
104 return;
105
106 if (ptrauth_disable())
107 return;
108
109 /*
110 * This assumes if there is pointer authentication on the boot CPU
111 * it will also be available on any non-boot CPUs. If this is ever
112 * not the case we will have to add a quirk.
113 */
114 if (ID_AA64ISAR1_APA_VAL(isar1) > 0 ||
115 ID_AA64ISAR1_API_VAL(isar1) > 0) {
116 enable_ptrauth = true;
117 elf64_addr_mask.code |= PAC_ADDR_MASK;
118 elf64_addr_mask.data |= PAC_ADDR_MASK;
119 }
120 }
121
122 /* Copy the keys when forking a new process */
123 void
ptrauth_fork(struct thread * new_td,struct thread * orig_td)124 ptrauth_fork(struct thread *new_td, struct thread *orig_td)
125 {
126 if (!enable_ptrauth)
127 return;
128
129 memcpy(&new_td->td_md.md_ptrauth_user, &orig_td->td_md.md_ptrauth_user,
130 sizeof(new_td->td_md.md_ptrauth_user));
131 }
132
133 /* Generate new userspace keys when executing a new process */
134 void
ptrauth_exec(struct thread * td)135 ptrauth_exec(struct thread *td)
136 {
137 if (!enable_ptrauth)
138 return;
139
140 arc4rand(&td->td_md.md_ptrauth_user, sizeof(td->td_md.md_ptrauth_user),
141 0);
142 }
143
144 /*
145 * Copy the user keys when creating a new userspace thread until it's clear
146 * how the ABI expects the various keys to be assigned.
147 */
148 void
ptrauth_copy_thread(struct thread * new_td,struct thread * orig_td)149 ptrauth_copy_thread(struct thread *new_td, struct thread *orig_td)
150 {
151 if (!enable_ptrauth)
152 return;
153
154 memcpy(&new_td->td_md.md_ptrauth_user, &orig_td->td_md.md_ptrauth_user,
155 sizeof(new_td->td_md.md_ptrauth_user));
156 }
157
158 /* Generate new kernel keys when executing a new kernel thread */
159 void
ptrauth_thread_alloc(struct thread * td)160 ptrauth_thread_alloc(struct thread *td)
161 {
162 if (!enable_ptrauth)
163 return;
164
165 arc4rand(&td->td_md.md_ptrauth_kern, sizeof(td->td_md.md_ptrauth_kern),
166 0);
167 }
168
169 /*
170 * Load the userspace keys. We can't use WRITE_SPECIALREG as we need
171 * to set the architecture extension.
172 */
173 #define LOAD_KEY(space, name, reg) \
174 __asm __volatile( \
175 "msr "__XSTRING(MRS_REG_ALT_NAME(reg ## KeyLo_EL1))", %0 \n" \
176 "msr "__XSTRING(MRS_REG_ALT_NAME(reg ## KeyHi_EL1))", %1 \n" \
177 :: "r"(td->td_md.md_ptrauth_##space.name.pa_key_lo), \
178 "r"(td->td_md.md_ptrauth_##space.name.pa_key_hi))
179
180 void
ptrauth_thread0(struct thread * td)181 ptrauth_thread0(struct thread *td)
182 {
183 if (!enable_ptrauth)
184 return;
185
186 /* TODO: Generate a random number here */
187 memset(&td->td_md.md_ptrauth_kern, 0,
188 sizeof(td->td_md.md_ptrauth_kern));
189 LOAD_KEY(kern, apia, APIA);
190 /*
191 * No isb as this is called before ptrauth_start so can rely on
192 * the instruction barrier there.
193 */
194 }
195
196 /*
197 * Enable pointer authentication. After this point userspace and the kernel
198 * can sign return addresses, etc. based on their keys
199 *
200 * This assumes either all or no CPUs have pointer authentication support,
201 * and, if supported, all CPUs have the same algorithm.
202 */
203 void
ptrauth_start(void)204 ptrauth_start(void)
205 {
206 uint64_t sctlr;
207
208 if (!enable_ptrauth)
209 return;
210
211 /* Enable pointer authentication */
212 sctlr = READ_SPECIALREG(sctlr_el1);
213 sctlr |= SCTLR_PTRAUTH;
214 WRITE_SPECIALREG(sctlr_el1, sctlr);
215 isb();
216 }
217
218 #ifdef SMP
219 void
ptrauth_mp_start(uint64_t cpu)220 ptrauth_mp_start(uint64_t cpu)
221 {
222 struct ptrauth_key start_key;
223 uint64_t sctlr;
224
225 if (!enable_ptrauth)
226 return;
227
228 /*
229 * We need a key until we call sched_throw, however we don't have
230 * a thread until then. Create a key just for use within
231 * init_secondary and whatever it calls. As init_secondary never
232 * returns it is safe to do so from within it.
233 *
234 * As it's only used for a short length of time just use the cpu
235 * as the key.
236 */
237 start_key.pa_key_lo = cpu;
238 start_key.pa_key_hi = ~cpu;
239
240 __asm __volatile(
241 ".arch_extension pauth \n"
242 "msr "__XSTRING(APIAKeyLo_EL1_REG)", %0 \n"
243 "msr "__XSTRING(APIAKeyHi_EL1_REG)", %1 \n"
244 ".arch_extension nopauth \n"
245 :: "r"(start_key.pa_key_lo), "r"(start_key.pa_key_hi));
246
247 /* Enable pointer authentication */
248 sctlr = READ_SPECIALREG(sctlr_el1);
249 sctlr |= SCTLR_PTRAUTH;
250 WRITE_SPECIALREG(sctlr_el1, sctlr);
251 isb();
252 }
253 #endif
254
255 struct thread *
ptrauth_switch(struct thread * td)256 ptrauth_switch(struct thread *td)
257 {
258 if (enable_ptrauth) {
259 LOAD_KEY(kern, apia, APIA);
260 isb();
261 }
262
263 return (td);
264 }
265
266 /* Called when we are exiting uerspace and entering the kernel */
267 void
ptrauth_exit_el0(struct thread * td)268 ptrauth_exit_el0(struct thread *td)
269 {
270 if (!enable_ptrauth)
271 return;
272
273 LOAD_KEY(kern, apia, APIA);
274 isb();
275 }
276
277 /* Called when we are about to exit the kernel and enter userspace */
278 void
ptrauth_enter_el0(struct thread * td)279 ptrauth_enter_el0(struct thread *td)
280 {
281 if (!enable_ptrauth)
282 return;
283
284 LOAD_KEY(user, apia, APIA);
285 LOAD_KEY(user, apib, APIB);
286 LOAD_KEY(user, apda, APDA);
287 LOAD_KEY(user, apdb, APDB);
288 LOAD_KEY(user, apga, APGA);
289 /*
290 * No isb as this is called from the exception handler so can rely
291 * on the eret instruction to be the needed context synchronizing event.
292 */
293 }
294