kern_pax.c revision 1.29 1 1.29 maxv /* $NetBSD: kern_pax.c,v 1.29 2015/07/30 15:28:18 maxv Exp $ */
2 1.1 elad
3 1.29 maxv /*
4 1.29 maxv * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.29 maxv * All rights reserved.
6 1.29 maxv *
7 1.29 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.29 maxv * by Maxime Villard.
9 1.29 maxv *
10 1.29 maxv * Redistribution and use in source and binary forms, with or without
11 1.29 maxv * modification, are permitted provided that the following conditions
12 1.29 maxv * are met:
13 1.29 maxv * 1. Redistributions of source code must retain the above copyright
14 1.29 maxv * notice, this list of conditions and the following disclaimer.
15 1.29 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.29 maxv * notice, this list of conditions and the following disclaimer in the
17 1.29 maxv * documentation and/or other materials provided with the distribution.
18 1.29 maxv *
19 1.29 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.29 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.29 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.29 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.29 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.29 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.29 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.29 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.29 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.29 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.29 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.29 maxv */
31 1.29 maxv
32 1.29 maxv /*
33 1.1 elad * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
34 1.1 elad * All rights reserved.
35 1.1 elad *
36 1.1 elad * Redistribution and use in source and binary forms, with or without
37 1.1 elad * modification, are permitted provided that the following conditions
38 1.1 elad * are met:
39 1.1 elad * 1. Redistributions of source code must retain the above copyright
40 1.1 elad * notice, this list of conditions and the following disclaimer.
41 1.1 elad * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 elad * notice, this list of conditions and the following disclaimer in the
43 1.1 elad * documentation and/or other materials provided with the distribution.
44 1.12 elad * 3. The name of the author may not be used to endorse or promote products
45 1.1 elad * derived from this software without specific prior written permission.
46 1.1 elad *
47 1.1 elad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 elad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 elad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 elad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 elad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 elad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 elad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 elad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 elad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 elad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 elad */
58 1.1 elad
59 1.17 dsl #include <sys/cdefs.h>
60 1.29 maxv __KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.29 2015/07/30 15:28:18 maxv Exp $");
61 1.17 dsl
62 1.1 elad #include "opt_pax.h"
63 1.1 elad
64 1.1 elad #include <sys/param.h>
65 1.1 elad #include <sys/proc.h>
66 1.1 elad #include <sys/exec_elf.h>
67 1.1 elad #include <sys/pax.h>
68 1.1 elad #include <sys/sysctl.h>
69 1.25 rmind #include <sys/kmem.h>
70 1.8 elad #include <sys/fileassoc.h>
71 1.8 elad #include <sys/syslog.h>
72 1.8 elad #include <sys/vnode.h>
73 1.8 elad #include <sys/queue.h>
74 1.8 elad #include <sys/kauth.h>
75 1.26 tls #include <sys/cprng.h>
76 1.1 elad
77 1.29 maxv #ifdef PAX_ASLR_DEBUG
78 1.29 maxv #define PAX_DPRINTF(_fmt, args...) uprintf("%s: " _fmt "\n", __func__, ##args)
79 1.29 maxv #else
80 1.29 maxv #define PAX_DPRINTF(_fmt, args...) do {} while (/*CONSTCOND*/0)
81 1.29 maxv #endif
82 1.29 maxv
83 1.18 christos #ifdef PAX_ASLR
84 1.18 christos #include <sys/mman.h>
85 1.18 christos #include <sys/exec.h>
86 1.18 christos
87 1.18 christos int pax_aslr_enabled = 1;
88 1.18 christos int pax_aslr_global = PAX_ASLR;
89 1.18 christos
90 1.18 christos #ifndef PAX_ASLR_DELTA_MMAP_LSB
91 1.18 christos #define PAX_ASLR_DELTA_MMAP_LSB PGSHIFT
92 1.18 christos #endif
93 1.18 christos #ifndef PAX_ASLR_DELTA_MMAP_LEN
94 1.18 christos #define PAX_ASLR_DELTA_MMAP_LEN ((sizeof(void *) * NBBY) / 2)
95 1.18 christos #endif
96 1.18 christos #ifndef PAX_ASLR_DELTA_STACK_LSB
97 1.18 christos #define PAX_ASLR_DELTA_STACK_LSB PGSHIFT
98 1.18 christos #endif
99 1.18 christos #ifndef PAX_ASLR_DELTA_STACK_LEN
100 1.18 christos #define PAX_ASLR_DELTA_STACK_LEN 12
101 1.18 christos #endif
102 1.18 christos
103 1.29 maxv static bool pax_aslr_elf_flags_active(uint32_t);
104 1.18 christos #endif /* PAX_ASLR */
105 1.18 christos
106 1.7 elad #ifdef PAX_MPROTECT
107 1.6 yamt static int pax_mprotect_enabled = 1;
108 1.6 yamt static int pax_mprotect_global = PAX_MPROTECT;
109 1.29 maxv static bool pax_mprotect_elf_flags_active(uint32_t);
110 1.18 christos #endif /* PAX_MPROTECT */
111 1.8 elad
112 1.8 elad #ifdef PAX_SEGVGUARD
113 1.8 elad #ifndef PAX_SEGVGUARD_EXPIRY
114 1.8 elad #define PAX_SEGVGUARD_EXPIRY (2 * 60)
115 1.8 elad #endif
116 1.8 elad #ifndef PAX_SEGVGUARD_SUSPENSION
117 1.8 elad #define PAX_SEGVGUARD_SUSPENSION (10 * 60)
118 1.8 elad #endif
119 1.8 elad #ifndef PAX_SEGVGUARD_MAXCRASHES
120 1.8 elad #define PAX_SEGVGUARD_MAXCRASHES 5
121 1.8 elad #endif
122 1.8 elad
123 1.8 elad static int pax_segvguard_enabled = 1;
124 1.8 elad static int pax_segvguard_global = PAX_SEGVGUARD;
125 1.8 elad static int pax_segvguard_expiry = PAX_SEGVGUARD_EXPIRY;
126 1.8 elad static int pax_segvguard_suspension = PAX_SEGVGUARD_SUSPENSION;
127 1.8 elad static int pax_segvguard_maxcrashes = PAX_SEGVGUARD_MAXCRASHES;
128 1.8 elad
129 1.8 elad static fileassoc_t segvguard_id;
130 1.8 elad
131 1.8 elad struct pax_segvguard_uid_entry {
132 1.8 elad uid_t sue_uid;
133 1.8 elad size_t sue_ncrashes;
134 1.8 elad time_t sue_expiry;
135 1.8 elad time_t sue_suspended;
136 1.8 elad LIST_ENTRY(pax_segvguard_uid_entry) sue_list;
137 1.8 elad };
138 1.8 elad
139 1.8 elad struct pax_segvguard_entry {
140 1.8 elad LIST_HEAD(, pax_segvguard_uid_entry) segv_uids;
141 1.8 elad };
142 1.10 yamt
143 1.29 maxv static bool pax_segvguard_elf_flags_active(uint32_t);
144 1.10 yamt static void pax_segvguard_cb(void *);
145 1.8 elad #endif /* PAX_SEGVGUARD */
146 1.7 elad
147 1.7 elad /* PaX internal setspecific flags */
148 1.7 elad #define PAX_MPROTECT_EXPLICIT_ENABLE (void *)0x01
149 1.7 elad #define PAX_MPROTECT_EXPLICIT_DISABLE (void *)0x02
150 1.8 elad #define PAX_SEGVGUARD_EXPLICIT_ENABLE (void *)0x03
151 1.8 elad #define PAX_SEGVGUARD_EXPLICIT_DISABLE (void *)0x04
152 1.18 christos #define PAX_ASLR_EXPLICIT_ENABLE (void *)0x05
153 1.18 christos #define PAX_ASLR_EXPLICIT_DISABLE (void *)0x06
154 1.7 elad
155 1.1 elad SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
156 1.1 elad {
157 1.8 elad const struct sysctlnode *rnode = NULL, *cnode;
158 1.1 elad
159 1.27 pooka sysctl_createv(clog, 0, NULL, &rnode,
160 1.1 elad CTLFLAG_PERMANENT,
161 1.1 elad CTLTYPE_NODE, "pax",
162 1.1 elad SYSCTL_DESCR("PaX (exploit mitigation) features."),
163 1.1 elad NULL, 0, NULL, 0,
164 1.27 pooka CTL_SECURITY, CTL_CREATE, CTL_EOL);
165 1.1 elad
166 1.19 elad cnode = rnode;
167 1.19 elad
168 1.18 christos #ifdef PAX_MPROTECT
169 1.19 elad rnode = cnode;
170 1.1 elad sysctl_createv(clog, 0, &rnode, &rnode,
171 1.1 elad CTLFLAG_PERMANENT,
172 1.1 elad CTLTYPE_NODE, "mprotect",
173 1.1 elad SYSCTL_DESCR("mprotect(2) W^X restrictions."),
174 1.1 elad NULL, 0, NULL, 0,
175 1.1 elad CTL_CREATE, CTL_EOL);
176 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
177 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
178 1.1 elad CTLTYPE_INT, "enabled",
179 1.1 elad SYSCTL_DESCR("Restrictions enabled."),
180 1.2 elad NULL, 0, &pax_mprotect_enabled, 0,
181 1.1 elad CTL_CREATE, CTL_EOL);
182 1.1 elad sysctl_createv(clog, 0, &rnode, NULL,
183 1.2 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
184 1.4 elad CTLTYPE_INT, "global",
185 1.1 elad SYSCTL_DESCR("When enabled, unless explicitly "
186 1.5 cbiere "specified, apply restrictions to "
187 1.1 elad "all processes."),
188 1.2 elad NULL, 0, &pax_mprotect_global, 0,
189 1.1 elad CTL_CREATE, CTL_EOL);
190 1.7 elad #endif /* PAX_MPROTECT */
191 1.8 elad
192 1.18 christos #ifdef PAX_SEGVGUARD
193 1.8 elad rnode = cnode;
194 1.8 elad sysctl_createv(clog, 0, &rnode, &rnode,
195 1.8 elad CTLFLAG_PERMANENT,
196 1.8 elad CTLTYPE_NODE, "segvguard",
197 1.8 elad SYSCTL_DESCR("PaX segvguard."),
198 1.8 elad NULL, 0, NULL, 0,
199 1.8 elad CTL_CREATE, CTL_EOL);
200 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
201 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
202 1.8 elad CTLTYPE_INT, "enabled",
203 1.8 elad SYSCTL_DESCR("segvguard enabled."),
204 1.8 elad NULL, 0, &pax_segvguard_enabled, 0,
205 1.8 elad CTL_CREATE, CTL_EOL);
206 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
207 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
208 1.8 elad CTLTYPE_INT, "global",
209 1.8 elad SYSCTL_DESCR("segvguard all programs."),
210 1.8 elad NULL, 0, &pax_segvguard_global, 0,
211 1.8 elad CTL_CREATE, CTL_EOL);
212 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
213 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
214 1.8 elad CTLTYPE_INT, "expiry_timeout",
215 1.8 elad SYSCTL_DESCR("Entry expiry timeout (in seconds)."),
216 1.8 elad NULL, 0, &pax_segvguard_expiry, 0,
217 1.8 elad CTL_CREATE, CTL_EOL);
218 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
219 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
220 1.8 elad CTLTYPE_INT, "suspend_timeout",
221 1.8 elad SYSCTL_DESCR("Entry suspension timeout (in seconds)."),
222 1.8 elad NULL, 0, &pax_segvguard_suspension, 0,
223 1.8 elad CTL_CREATE, CTL_EOL);
224 1.8 elad sysctl_createv(clog, 0, &rnode, NULL,
225 1.8 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
226 1.8 elad CTLTYPE_INT, "max_crashes",
227 1.8 elad SYSCTL_DESCR("Max number of crashes before expiry."),
228 1.8 elad NULL, 0, &pax_segvguard_maxcrashes, 0,
229 1.8 elad CTL_CREATE, CTL_EOL);
230 1.8 elad #endif /* PAX_SEGVGUARD */
231 1.18 christos
232 1.18 christos #ifdef PAX_ASLR
233 1.18 christos rnode = cnode;
234 1.18 christos sysctl_createv(clog, 0, &rnode, &rnode,
235 1.18 christos CTLFLAG_PERMANENT,
236 1.18 christos CTLTYPE_NODE, "aslr",
237 1.18 christos SYSCTL_DESCR("Address Space Layout Randomization."),
238 1.18 christos NULL, 0, NULL, 0,
239 1.18 christos CTL_CREATE, CTL_EOL);
240 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
241 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
242 1.18 christos CTLTYPE_INT, "enabled",
243 1.18 christos SYSCTL_DESCR("Restrictions enabled."),
244 1.18 christos NULL, 0, &pax_aslr_enabled, 0,
245 1.18 christos CTL_CREATE, CTL_EOL);
246 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
247 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
248 1.18 christos CTLTYPE_INT, "global",
249 1.18 christos SYSCTL_DESCR("When enabled, unless explicitly "
250 1.18 christos "specified, apply to all processes."),
251 1.18 christos NULL, 0, &pax_aslr_global, 0,
252 1.18 christos CTL_CREATE, CTL_EOL);
253 1.18 christos sysctl_createv(clog, 0, &rnode, NULL,
254 1.18 christos CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
255 1.18 christos CTLTYPE_INT, "mmap_len",
256 1.18 christos SYSCTL_DESCR("Number of bits randomized for "
257 1.18 christos "mmap(2) calls."),
258 1.18 christos NULL, PAX_ASLR_DELTA_MMAP_LEN, NULL, 0,
259 1.18 christos CTL_CREATE, CTL_EOL);
260 1.19 elad sysctl_createv(clog, 0, &rnode, NULL,
261 1.19 elad CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
262 1.19 elad CTLTYPE_INT, "stack_len",
263 1.19 elad SYSCTL_DESCR("Number of bits randomized for "
264 1.19 elad "the stack."),
265 1.19 elad NULL, PAX_ASLR_DELTA_STACK_LEN, NULL, 0,
266 1.19 elad CTL_CREATE, CTL_EOL);
267 1.19 elad sysctl_createv(clog, 0, &rnode, NULL,
268 1.19 elad CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
269 1.19 elad CTLTYPE_INT, "exec_len",
270 1.19 elad SYSCTL_DESCR("Number of bits randomized for "
271 1.19 elad "the PIE exec base."),
272 1.19 elad NULL, PAX_ASLR_DELTA_EXEC_LEN, NULL, 0,
273 1.19 elad CTL_CREATE, CTL_EOL);
274 1.19 elad
275 1.18 christos #endif /* PAX_ASLR */
276 1.1 elad }
277 1.1 elad
278 1.7 elad /*
279 1.7 elad * Initialize PaX.
280 1.7 elad */
281 1.1 elad void
282 1.7 elad pax_init(void)
283 1.1 elad {
284 1.9 yamt #ifdef PAX_SEGVGUARD
285 1.9 yamt int error;
286 1.9 yamt
287 1.9 yamt error = fileassoc_register("segvguard", pax_segvguard_cb,
288 1.9 yamt &segvguard_id);
289 1.9 yamt if (error) {
290 1.9 yamt panic("pax_init: segvguard_id: error=%d\n", error);
291 1.9 yamt }
292 1.8 elad #endif /* PAX_SEGVGUARD */
293 1.1 elad }
294 1.1 elad
295 1.29 maxv void
296 1.29 maxv pax_setup_elf_flags(struct lwp *l, uint32_t elf_flags)
297 1.29 maxv {
298 1.29 maxv uint32_t flags = 0;
299 1.29 maxv
300 1.29 maxv #ifdef PAX_ASLR
301 1.29 maxv if (pax_aslr_elf_flags_active(elf_flags)) {
302 1.29 maxv flags |= P_PAX_ASLR;
303 1.29 maxv }
304 1.29 maxv #endif
305 1.29 maxv #ifdef PAX_MPROTECT
306 1.29 maxv if (pax_mprotect_elf_flags_active(elf_flags)) {
307 1.29 maxv flags |= P_PAX_MPROTECT;
308 1.29 maxv }
309 1.29 maxv #endif
310 1.29 maxv #ifdef PAX_SEGVGUARD
311 1.29 maxv if (pax_segvguard_elf_flags_active(elf_flags)) {
312 1.29 maxv flags |= P_PAX_GUARD;
313 1.29 maxv }
314 1.29 maxv #endif
315 1.29 maxv
316 1.29 maxv l->l_proc->p_pax = flags;
317 1.29 maxv }
318 1.29 maxv
319 1.29 maxv
320 1.7 elad #ifdef PAX_MPROTECT
321 1.29 maxv static bool
322 1.29 maxv pax_mprotect_elf_flags_active(uint32_t flags)
323 1.29 maxv {
324 1.29 maxv if (!pax_mprotect_enabled)
325 1.29 maxv return false;
326 1.29 maxv if (pax_mprotect_global && (flags & ELF_NOTE_PAX_NOMPROTECT) != 0) {
327 1.29 maxv /* Mprotect explicitly disabled */
328 1.29 maxv return false;
329 1.29 maxv }
330 1.29 maxv if (!pax_mprotect_global && (flags & ELF_NOTE_PAX_MPROTECT) == 0) {
331 1.29 maxv /* Mprotect not requested */
332 1.29 maxv return false;
333 1.29 maxv }
334 1.29 maxv return true;
335 1.29 maxv }
336 1.29 maxv
337 1.1 elad void
338 1.3 elad pax_mprotect(struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
339 1.1 elad {
340 1.29 maxv uint32_t flags;
341 1.7 elad
342 1.29 maxv flags = l->l_proc->p_pax;
343 1.29 maxv if (!(flags & P_PAX_MPROTECT))
344 1.1 elad return;
345 1.1 elad
346 1.3 elad if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
347 1.3 elad *prot &= ~VM_PROT_EXECUTE;
348 1.3 elad *maxprot &= ~VM_PROT_EXECUTE;
349 1.1 elad } else {
350 1.3 elad *prot &= ~VM_PROT_WRITE;
351 1.3 elad *maxprot &= ~VM_PROT_WRITE;
352 1.1 elad }
353 1.1 elad }
354 1.7 elad #endif /* PAX_MPROTECT */
355 1.8 elad
356 1.18 christos #ifdef PAX_ASLR
357 1.29 maxv static bool
358 1.29 maxv pax_aslr_elf_flags_active(uint32_t flags)
359 1.18 christos {
360 1.18 christos if (!pax_aslr_enabled)
361 1.18 christos return false;
362 1.29 maxv if (pax_aslr_global && (flags & ELF_NOTE_PAX_NOASLR) != 0) {
363 1.29 maxv /* ASLR explicitly disabled */
364 1.29 maxv return false;
365 1.29 maxv }
366 1.29 maxv if (!pax_aslr_global && (flags & ELF_NOTE_PAX_ASLR) == 0) {
367 1.29 maxv /* ASLR not requested */
368 1.29 maxv return false;
369 1.29 maxv }
370 1.29 maxv return true;
371 1.29 maxv }
372 1.18 christos
373 1.29 maxv bool
374 1.29 maxv pax_aslr_active(struct lwp *l)
375 1.29 maxv {
376 1.29 maxv uint32_t flags = l->l_proc->p_pax;
377 1.29 maxv if (!(flags & P_PAX_ASLR))
378 1.18 christos return false;
379 1.18 christos return true;
380 1.18 christos }
381 1.18 christos
382 1.18 christos void
383 1.18 christos pax_aslr_init(struct lwp *l, struct vmspace *vm)
384 1.18 christos {
385 1.18 christos if (!pax_aslr_active(l))
386 1.18 christos return;
387 1.18 christos
388 1.26 tls vm->vm_aslr_delta_mmap = PAX_ASLR_DELTA(cprng_fast32(),
389 1.18 christos PAX_ASLR_DELTA_MMAP_LSB, PAX_ASLR_DELTA_MMAP_LEN);
390 1.18 christos }
391 1.18 christos
392 1.18 christos void
393 1.18 christos pax_aslr(struct lwp *l, vaddr_t *addr, vaddr_t orig_addr, int f)
394 1.18 christos {
395 1.18 christos if (!pax_aslr_active(l))
396 1.18 christos return;
397 1.18 christos
398 1.18 christos if (!(f & MAP_FIXED) && ((orig_addr == 0) || !(f & MAP_ANON))) {
399 1.29 maxv PAX_DPRINTF("applying to 0x%lx orig_addr=0x%lx f=%x",
400 1.18 christos (unsigned long)*addr, (unsigned long)orig_addr, f);
401 1.18 christos if (!(l->l_proc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
402 1.18 christos *addr += l->l_proc->p_vmspace->vm_aslr_delta_mmap;
403 1.18 christos else
404 1.18 christos *addr -= l->l_proc->p_vmspace->vm_aslr_delta_mmap;
405 1.29 maxv PAX_DPRINTF("result 0x%lx", *addr);
406 1.29 maxv } else {
407 1.29 maxv PAX_DPRINTF("not applying to 0x%lx orig_addr=0x%lx f=%x",
408 1.29 maxv (unsigned long)*addr, (unsigned long)orig_addr, f);
409 1.18 christos }
410 1.18 christos }
411 1.18 christos
412 1.18 christos void
413 1.18 christos pax_aslr_stack(struct lwp *l, struct exec_package *epp, u_long *max_stack_size)
414 1.18 christos {
415 1.18 christos if (pax_aslr_active(l)) {
416 1.26 tls u_long d = PAX_ASLR_DELTA(cprng_fast32(),
417 1.18 christos PAX_ASLR_DELTA_STACK_LSB,
418 1.18 christos PAX_ASLR_DELTA_STACK_LEN);
419 1.29 maxv PAX_DPRINTF("stack 0x%lx d=0x%lx 0x%lx",
420 1.18 christos epp->ep_minsaddr, d, epp->ep_minsaddr - d);
421 1.18 christos epp->ep_minsaddr -= d;
422 1.18 christos *max_stack_size -= d;
423 1.24 christos if (epp->ep_ssize > *max_stack_size)
424 1.24 christos epp->ep_ssize = *max_stack_size;
425 1.18 christos }
426 1.18 christos }
427 1.18 christos #endif /* PAX_ASLR */
428 1.18 christos
429 1.8 elad #ifdef PAX_SEGVGUARD
430 1.29 maxv static bool
431 1.29 maxv pax_segvguard_elf_flags_active(uint32_t flags)
432 1.29 maxv {
433 1.29 maxv if (!pax_segvguard_enabled)
434 1.29 maxv return false;
435 1.29 maxv if (pax_segvguard_global && (flags & ELF_NOTE_PAX_NOGUARD) != 0) {
436 1.29 maxv /* Segvguard explicitly disabled */
437 1.29 maxv return false;
438 1.29 maxv }
439 1.29 maxv if (!pax_segvguard_global && (flags & ELF_NOTE_PAX_GUARD) == 0) {
440 1.29 maxv /* Segvguard not requested */
441 1.29 maxv return false;
442 1.29 maxv }
443 1.29 maxv return true;
444 1.29 maxv }
445 1.29 maxv
446 1.10 yamt static void
447 1.10 yamt pax_segvguard_cb(void *v)
448 1.8 elad {
449 1.25 rmind struct pax_segvguard_entry *p = v;
450 1.8 elad struct pax_segvguard_uid_entry *up;
451 1.8 elad
452 1.25 rmind if (p == NULL) {
453 1.8 elad return;
454 1.25 rmind }
455 1.10 yamt while ((up = LIST_FIRST(&p->segv_uids)) != NULL) {
456 1.10 yamt LIST_REMOVE(up, sue_list);
457 1.25 rmind kmem_free(up, sizeof(*up));
458 1.8 elad }
459 1.25 rmind kmem_free(p, sizeof(*p));
460 1.8 elad }
461 1.8 elad
462 1.8 elad /*
463 1.8 elad * Called when a process of image vp generated a segfault.
464 1.8 elad */
465 1.8 elad int
466 1.8 elad pax_segvguard(struct lwp *l, struct vnode *vp, const char *name,
467 1.14 thorpej bool crashed)
468 1.8 elad {
469 1.8 elad struct pax_segvguard_entry *p;
470 1.8 elad struct pax_segvguard_uid_entry *up;
471 1.8 elad struct timeval tv;
472 1.8 elad uid_t uid;
473 1.29 maxv uint32_t flags;
474 1.14 thorpej bool have_uid;
475 1.8 elad
476 1.29 maxv flags = l->l_proc->p_pax;
477 1.29 maxv if (!(flags & P_PAX_GUARD))
478 1.29 maxv return 0;
479 1.8 elad
480 1.9 yamt if (vp == NULL)
481 1.8 elad return (EFAULT);
482 1.8 elad
483 1.8 elad /* Check if we already monitor the file. */
484 1.8 elad p = fileassoc_lookup(vp, segvguard_id);
485 1.8 elad
486 1.8 elad /* Fast-path if starting a program we don't know. */
487 1.8 elad if (p == NULL && !crashed)
488 1.8 elad return (0);
489 1.8 elad
490 1.8 elad microtime(&tv);
491 1.8 elad
492 1.8 elad /*
493 1.8 elad * If a program we don't know crashed, we need to create a new entry
494 1.8 elad * for it.
495 1.8 elad */
496 1.8 elad if (p == NULL) {
497 1.25 rmind p = kmem_alloc(sizeof(*p), KM_SLEEP);
498 1.13 elad fileassoc_add(vp, segvguard_id, p);
499 1.8 elad LIST_INIT(&p->segv_uids);
500 1.8 elad
501 1.8 elad /*
502 1.8 elad * Initialize a new entry with "crashes so far" of 1.
503 1.8 elad * The expiry time is when we purge the entry if it didn't
504 1.8 elad * reach the limit.
505 1.8 elad */
506 1.25 rmind up = kmem_alloc(sizeof(*up), KM_SLEEP);
507 1.8 elad up->sue_uid = kauth_cred_getuid(l->l_cred);
508 1.8 elad up->sue_ncrashes = 1;
509 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
510 1.8 elad up->sue_suspended = 0;
511 1.8 elad
512 1.8 elad LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
513 1.8 elad
514 1.8 elad return (0);
515 1.8 elad }
516 1.8 elad
517 1.8 elad /*
518 1.8 elad * A program we "know" either executed or crashed again.
519 1.8 elad * See if it's a culprit we're familiar with.
520 1.8 elad */
521 1.8 elad uid = kauth_cred_getuid(l->l_cred);
522 1.15 thorpej have_uid = false;
523 1.8 elad LIST_FOREACH(up, &p->segv_uids, sue_list) {
524 1.8 elad if (up->sue_uid == uid) {
525 1.15 thorpej have_uid = true;
526 1.8 elad break;
527 1.8 elad }
528 1.8 elad }
529 1.8 elad
530 1.8 elad /*
531 1.8 elad * It's someone else. Add an entry for him if we crashed.
532 1.8 elad */
533 1.8 elad if (!have_uid) {
534 1.8 elad if (crashed) {
535 1.25 rmind up = kmem_alloc(sizeof(*up), KM_SLEEP);
536 1.8 elad up->sue_uid = uid;
537 1.8 elad up->sue_ncrashes = 1;
538 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
539 1.8 elad up->sue_suspended = 0;
540 1.8 elad
541 1.8 elad LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
542 1.8 elad }
543 1.8 elad return (0);
544 1.8 elad }
545 1.8 elad
546 1.8 elad if (crashed) {
547 1.8 elad /* Check if timer on previous crashes expired first. */
548 1.8 elad if (up->sue_expiry < tv.tv_sec) {
549 1.8 elad log(LOG_INFO, "PaX Segvguard: [%s] Suspension"
550 1.8 elad " expired.\n", name ? name : "unknown");
551 1.8 elad
552 1.8 elad up->sue_ncrashes = 1;
553 1.8 elad up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
554 1.8 elad up->sue_suspended = 0;
555 1.8 elad
556 1.8 elad return (0);
557 1.8 elad }
558 1.8 elad
559 1.8 elad up->sue_ncrashes++;
560 1.8 elad
561 1.8 elad if (up->sue_ncrashes >= pax_segvguard_maxcrashes) {
562 1.8 elad log(LOG_ALERT, "PaX Segvguard: [%s] Suspending "
563 1.8 elad "execution for %d seconds after %zu crashes.\n",
564 1.8 elad name ? name : "unknown", pax_segvguard_suspension,
565 1.8 elad up->sue_ncrashes);
566 1.8 elad
567 1.8 elad /* Suspend this program for a while. */
568 1.8 elad up->sue_suspended = tv.tv_sec + pax_segvguard_suspension;
569 1.8 elad up->sue_ncrashes = 0;
570 1.8 elad up->sue_expiry = 0;
571 1.8 elad }
572 1.8 elad } else {
573 1.8 elad /* Are we supposed to be suspended? */
574 1.8 elad if (up->sue_suspended > tv.tv_sec) {
575 1.8 elad log(LOG_ALERT, "PaX Segvguard: [%s] Preventing "
576 1.8 elad "execution due to repeated segfaults.\n", name ?
577 1.8 elad name : "unknown");
578 1.8 elad
579 1.8 elad return (EPERM);
580 1.8 elad }
581 1.8 elad }
582 1.8 elad
583 1.8 elad return (0);
584 1.8 elad }
585 1.8 elad #endif /* PAX_SEGVGUARD */
586