kern_pax.c revision 1.53 1 /* $NetBSD: kern_pax.c,v 1.53 2016/05/25 20:07:54 christos Exp $ */
2
3 /*
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Maxime Villard.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2006 Elad Efrat <elad (at) NetBSD.org>
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.53 2016/05/25 20:07:54 christos Exp $");
61
62 #include "opt_pax.h"
63
64 #include <sys/param.h>
65 #include <sys/proc.h>
66 #include <sys/exec.h>
67 #include <sys/exec_elf.h>
68 #include <sys/pax.h>
69 #include <sys/sysctl.h>
70 #include <sys/kmem.h>
71 #include <sys/mman.h>
72 #include <sys/fileassoc.h>
73 #include <sys/syslog.h>
74 #include <sys/vnode.h>
75 #include <sys/queue.h>
76 #include <sys/bitops.h>
77 #include <sys/kauth.h>
78 #include <sys/cprng.h>
79
80 #ifdef PAX_ASLR_DEBUG
81 #define PAX_DPRINTF(_fmt, args...) \
82 do if (pax_aslr_debug) uprintf("%s: " _fmt "\n", __func__, ##args); \
83 while (/*CONSTCOND*/0)
84 #else
85 #define PAX_DPRINTF(_fmt, args...) do {} while (/*CONSTCOND*/0)
86 #endif
87
88 #ifdef PAX_ASLR
89 #include <sys/mman.h>
90
91 int pax_aslr_enabled = 1;
92 int pax_aslr_global = PAX_ASLR;
93
94 #ifndef PAX_ASLR_DELTA_MMAP_LSB
95 #define PAX_ASLR_DELTA_MMAP_LSB PGSHIFT
96 #endif
97 #ifndef PAX_ASLR_DELTA_MMAP_LEN
98 #define PAX_ASLR_DELTA_MMAP_LEN ((sizeof(void *) * NBBY) / 2)
99 #endif
100 #ifndef PAX_ASLR_DELTA_MMAP_LEN32
101 #define PAX_ASLR_DELTA_MMAP_LEN32 ((sizeof(uint32_t) * NBBY) / 2)
102 #endif
103 #ifndef PAX_ASLR_DELTA_STACK_LSB
104 #define PAX_ASLR_DELTA_STACK_LSB PGSHIFT
105 #endif
106 #ifndef PAX_ASLR_DELTA_STACK_LEN
107 #define PAX_ASLR_DELTA_STACK_LEN ((sizeof(void *) * NBBY) / 4)
108 #endif
109 #ifndef PAX_ASLR_DELTA_STACK_LEN32
110 #define PAX_ASLR_DELTA_STACK_LEN32 ((sizeof(uint32_t) * NBBY) / 4)
111 #endif
112 #define PAX_ASLR_MAX_STACK_WASTE 8
113
114 static bool pax_aslr_elf_flags_active(uint32_t);
115 #endif /* PAX_ASLR */
116
117 #ifdef PAX_MPROTECT
118 static int pax_mprotect_enabled = 1;
119 static int pax_mprotect_global = PAX_MPROTECT;
120 static int pax_mprotect_ptrace = 1;
121 static bool pax_mprotect_elf_flags_active(uint32_t);
122 #endif /* PAX_MPROTECT */
123 #ifdef PAX_MPROTECT_DEBUG
124 int pax_mprotect_debug;
125 #endif
126
127 #ifdef PAX_SEGVGUARD
128 #ifndef PAX_SEGVGUARD_EXPIRY
129 #define PAX_SEGVGUARD_EXPIRY (2 * 60)
130 #endif
131 #ifndef PAX_SEGVGUARD_SUSPENSION
132 #define PAX_SEGVGUARD_SUSPENSION (10 * 60)
133 #endif
134 #ifndef PAX_SEGVGUARD_MAXCRASHES
135 #define PAX_SEGVGUARD_MAXCRASHES 5
136 #endif
137
138 #ifdef PAX_ASLR_DEBUG
139 int pax_aslr_debug;
140 /* flag set means disable */
141 int pax_aslr_flags;
142 uint32_t pax_aslr_rand;
143 #define PAX_ASLR_STACK 0x01
144 #define PAX_ASLR_STACK_GAP 0x02
145 #define PAX_ASLR_MMAP 0x04
146 #define PAX_ASLR_EXEC_OFFSET 0x08
147 #define PAX_ASLR_RTLD_OFFSET 0x10
148 #define PAX_ASLR_FIXED 0x20
149 #endif
150
151 static int pax_segvguard_enabled = 1;
152 static int pax_segvguard_global = PAX_SEGVGUARD;
153 static int pax_segvguard_expiry = PAX_SEGVGUARD_EXPIRY;
154 static int pax_segvguard_suspension = PAX_SEGVGUARD_SUSPENSION;
155 static int pax_segvguard_maxcrashes = PAX_SEGVGUARD_MAXCRASHES;
156
157 static fileassoc_t segvguard_id;
158
159 struct pax_segvguard_uid_entry {
160 uid_t sue_uid;
161 size_t sue_ncrashes;
162 time_t sue_expiry;
163 time_t sue_suspended;
164 LIST_ENTRY(pax_segvguard_uid_entry) sue_list;
165 };
166
167 struct pax_segvguard_entry {
168 LIST_HEAD(, pax_segvguard_uid_entry) segv_uids;
169 };
170
171 static bool pax_segvguard_elf_flags_active(uint32_t);
172 static void pax_segvguard_cleanup_cb(void *);
173 #endif /* PAX_SEGVGUARD */
174
175 SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup")
176 {
177 const struct sysctlnode *rnode = NULL, *cnode;
178
179 sysctl_createv(clog, 0, NULL, &rnode,
180 CTLFLAG_PERMANENT,
181 CTLTYPE_NODE, "pax",
182 SYSCTL_DESCR("PaX (exploit mitigation) features."),
183 NULL, 0, NULL, 0,
184 CTL_SECURITY, CTL_CREATE, CTL_EOL);
185
186 cnode = rnode;
187
188 #ifdef PAX_MPROTECT
189 rnode = cnode;
190 sysctl_createv(clog, 0, &rnode, &rnode,
191 CTLFLAG_PERMANENT,
192 CTLTYPE_NODE, "mprotect",
193 SYSCTL_DESCR("mprotect(2) W^X restrictions."),
194 NULL, 0, NULL, 0,
195 CTL_CREATE, CTL_EOL);
196 sysctl_createv(clog, 0, &rnode, NULL,
197 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
198 CTLTYPE_INT, "enabled",
199 SYSCTL_DESCR("Restrictions enabled."),
200 NULL, 0, &pax_mprotect_enabled, 0,
201 CTL_CREATE, CTL_EOL);
202 sysctl_createv(clog, 0, &rnode, NULL,
203 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
204 CTLTYPE_INT, "global",
205 SYSCTL_DESCR("When enabled, unless explicitly "
206 "specified, apply restrictions to "
207 "all processes."),
208 NULL, 0, &pax_mprotect_global, 0,
209 CTL_CREATE, CTL_EOL);
210 sysctl_createv(clog, 0, &rnode, NULL,
211 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
212 CTLTYPE_INT, "ptrace",
213 SYSCTL_DESCR("When enabled, allow ptrace(2) to "
214 "override mprotect permissions on traced "
215 "processes"),
216 NULL, 0, &pax_mprotect_ptrace, 0,
217 CTL_CREATE, CTL_EOL);
218 #ifdef PAX_MPROTECT_DEBUG
219 sysctl_createv(clog, 0, &rnode, NULL,
220 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
221 CTLTYPE_INT, "debug",
222 SYSCTL_DESCR("print mprotect changes."),
223 NULL, 0, &pax_mprotect_debug, 0,
224 CTL_CREATE, CTL_EOL);
225 #endif
226 #endif /* PAX_MPROTECT */
227
228 #ifdef PAX_SEGVGUARD
229 rnode = cnode;
230 sysctl_createv(clog, 0, &rnode, &rnode,
231 CTLFLAG_PERMANENT,
232 CTLTYPE_NODE, "segvguard",
233 SYSCTL_DESCR("PaX segvguard."),
234 NULL, 0, NULL, 0,
235 CTL_CREATE, CTL_EOL);
236 sysctl_createv(clog, 0, &rnode, NULL,
237 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
238 CTLTYPE_INT, "enabled",
239 SYSCTL_DESCR("segvguard enabled."),
240 NULL, 0, &pax_segvguard_enabled, 0,
241 CTL_CREATE, CTL_EOL);
242 sysctl_createv(clog, 0, &rnode, NULL,
243 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
244 CTLTYPE_INT, "global",
245 SYSCTL_DESCR("segvguard all programs."),
246 NULL, 0, &pax_segvguard_global, 0,
247 CTL_CREATE, CTL_EOL);
248 sysctl_createv(clog, 0, &rnode, NULL,
249 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
250 CTLTYPE_INT, "expiry_timeout",
251 SYSCTL_DESCR("Entry expiry timeout (in seconds)."),
252 NULL, 0, &pax_segvguard_expiry, 0,
253 CTL_CREATE, CTL_EOL);
254 sysctl_createv(clog, 0, &rnode, NULL,
255 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
256 CTLTYPE_INT, "suspend_timeout",
257 SYSCTL_DESCR("Entry suspension timeout (in seconds)."),
258 NULL, 0, &pax_segvguard_suspension, 0,
259 CTL_CREATE, CTL_EOL);
260 sysctl_createv(clog, 0, &rnode, NULL,
261 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
262 CTLTYPE_INT, "max_crashes",
263 SYSCTL_DESCR("Max number of crashes before expiry."),
264 NULL, 0, &pax_segvguard_maxcrashes, 0,
265 CTL_CREATE, CTL_EOL);
266 #endif /* PAX_SEGVGUARD */
267
268 #ifdef PAX_ASLR
269 rnode = cnode;
270 sysctl_createv(clog, 0, &rnode, &rnode,
271 CTLFLAG_PERMANENT,
272 CTLTYPE_NODE, "aslr",
273 SYSCTL_DESCR("Address Space Layout Randomization."),
274 NULL, 0, NULL, 0,
275 CTL_CREATE, CTL_EOL);
276 sysctl_createv(clog, 0, &rnode, NULL,
277 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
278 CTLTYPE_INT, "enabled",
279 SYSCTL_DESCR("Restrictions enabled."),
280 NULL, 0, &pax_aslr_enabled, 0,
281 CTL_CREATE, CTL_EOL);
282 sysctl_createv(clog, 0, &rnode, NULL,
283 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
284 CTLTYPE_INT, "global",
285 SYSCTL_DESCR("When enabled, unless explicitly "
286 "specified, apply to all processes."),
287 NULL, 0, &pax_aslr_global, 0,
288 CTL_CREATE, CTL_EOL);
289 #ifdef PAX_ASLR_DEBUG
290 sysctl_createv(clog, 0, &rnode, NULL,
291 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
292 CTLTYPE_INT, "debug",
293 SYSCTL_DESCR("Pring ASLR selected addresses."),
294 NULL, 0, &pax_aslr_debug, 0,
295 CTL_CREATE, CTL_EOL);
296 sysctl_createv(clog, 0, &rnode, NULL,
297 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
298 CTLTYPE_INT, "flags",
299 SYSCTL_DESCR("Disable/Enable select ASLR features."),
300 NULL, 0, &pax_aslr_flags, 0,
301 CTL_CREATE, CTL_EOL);
302 sysctl_createv(clog, 0, &rnode, NULL,
303 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
304 CTLTYPE_INT, "rand",
305 SYSCTL_DESCR("Use the given fixed random value"),
306 NULL, 0, &pax_aslr_rand, 0,
307 CTL_CREATE, CTL_EOL);
308 #endif
309 sysctl_createv(clog, 0, &rnode, NULL,
310 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
311 CTLTYPE_INT, "mmap_len",
312 SYSCTL_DESCR("Number of bits randomized for "
313 "mmap(2) calls."),
314 NULL, PAX_ASLR_DELTA_MMAP_LEN, NULL, 0,
315 CTL_CREATE, CTL_EOL);
316 sysctl_createv(clog, 0, &rnode, NULL,
317 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
318 CTLTYPE_INT, "stack_len",
319 SYSCTL_DESCR("Number of bits randomized for "
320 "the stack."),
321 NULL, PAX_ASLR_DELTA_STACK_LEN, NULL, 0,
322 CTL_CREATE, CTL_EOL);
323 sysctl_createv(clog, 0, &rnode, NULL,
324 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
325 CTLTYPE_INT, "exec_len",
326 SYSCTL_DESCR("Number of bits randomized for "
327 "the PIE exec base."),
328 NULL, PAX_ASLR_DELTA_EXEC_LEN, NULL, 0,
329 CTL_CREATE, CTL_EOL);
330
331 #endif /* PAX_ASLR */
332 }
333
334 /*
335 * Initialize PaX.
336 */
337 void
338 pax_init(void)
339 {
340 #ifdef PAX_SEGVGUARD
341 int error;
342
343 error = fileassoc_register("segvguard", pax_segvguard_cleanup_cb,
344 &segvguard_id);
345 if (error) {
346 panic("pax_init: segvguard_id: error=%d\n", error);
347 }
348 #endif /* PAX_SEGVGUARD */
349 #ifdef PAX_ASLR
350 /* Adjust maximum stack by the size we can consume for ASLR */
351 extern rlim_t maxsmap;
352 maxsmap = MAXSSIZ - (MAXSSIZ / PAX_ASLR_MAX_STACK_WASTE);
353 // XXX: compat32 is not handled.
354 #endif
355 }
356
357 void
358 pax_set_flags(struct exec_package *epp, struct proc *p)
359 {
360 p->p_pax = epp->ep_pax_flags;
361
362 if (pax_mprotect_ptrace == 0)
363 return;
364 /*
365 * If we are running under the debugger, turn off MPROTECT so
366 * the debugger can insert/delete breakpoints
367 */
368 if (p->p_slflag & PSL_TRACED)
369 p->p_pax &= ~P_PAX_MPROTECT;
370 }
371
372 void
373 pax_setup_elf_flags(struct exec_package *epp, uint32_t elf_flags)
374 {
375 uint32_t flags = 0;
376
377 #ifdef PAX_ASLR
378 if (pax_aslr_elf_flags_active(elf_flags)) {
379 flags |= P_PAX_ASLR;
380 }
381 #endif
382 #ifdef PAX_MPROTECT
383 if (pax_mprotect_elf_flags_active(elf_flags)) {
384 flags |= P_PAX_MPROTECT;
385 }
386 #endif
387 #ifdef PAX_SEGVGUARD
388 if (pax_segvguard_elf_flags_active(elf_flags)) {
389 flags |= P_PAX_GUARD;
390 }
391 #endif
392
393 epp->ep_pax_flags = flags;
394 }
395
396 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
397 static inline bool
398 pax_flags_active(uint32_t flags, uint32_t opt)
399 {
400 if (!(flags & opt))
401 return false;
402 return true;
403 }
404 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
405
406 #ifdef PAX_MPROTECT
407 static bool
408 pax_mprotect_elf_flags_active(uint32_t flags)
409 {
410 if (!pax_mprotect_enabled)
411 return false;
412 if (pax_mprotect_global && (flags & ELF_NOTE_PAX_NOMPROTECT) != 0) {
413 /* Mprotect explicitly disabled */
414 return false;
415 }
416 if (!pax_mprotect_global && (flags & ELF_NOTE_PAX_MPROTECT) == 0) {
417 /* Mprotect not requested */
418 return false;
419 }
420 return true;
421 }
422
423 void
424 pax_mprotect_adjust(
425 #ifdef PAX_MPROTECT_DEBUG
426 const char *file, size_t line,
427 #endif
428 struct lwp *l, vm_prot_t *prot, vm_prot_t *maxprot)
429 {
430 uint32_t flags;
431
432 flags = l->l_proc->p_pax;
433 if (!pax_flags_active(flags, P_PAX_MPROTECT))
434 return;
435
436 if ((*prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) != VM_PROT_EXECUTE) {
437 #ifdef PAX_MPROTECT_DEBUG
438 struct proc *p = l->l_proc;
439 if ((*prot & VM_PROT_EXECUTE) && pax_mprotect_debug) {
440 printf("%s: %s,%zu: %d.%d (%s): -x\n",
441 __func__, file, line,
442 p->p_pid, l->l_lid, p->p_comm);
443 }
444 #endif
445 *prot &= ~VM_PROT_EXECUTE;
446 *maxprot &= ~VM_PROT_EXECUTE;
447 } else {
448 #ifdef PAX_MPROTECT_DEBUG
449 struct proc *p = l->l_proc;
450 if ((*prot & VM_PROT_WRITE) && pax_mprotect_debug) {
451 printf("%s: %s,%zu: %d.%d (%s): -w\n",
452 __func__, file, line,
453 p->p_pid, l->l_lid, p->p_comm);
454 }
455 #endif
456 *prot &= ~VM_PROT_WRITE;
457 *maxprot &= ~VM_PROT_WRITE;
458 }
459 }
460
461 /*
462 * Bypass MPROTECT for traced processes
463 */
464 int
465 pax_mprotect_prot(struct lwp *l)
466 {
467 uint32_t flags;
468
469 flags = l->l_proc->p_pax;
470 if (!pax_flags_active(flags, P_PAX_MPROTECT))
471 return 0;
472 if (pax_mprotect_ptrace < 2)
473 return 0;
474 return UVM_EXTRACT_PROT_ALL;
475 }
476
477
478 #endif /* PAX_MPROTECT */
479
480 #ifdef PAX_ASLR
481 static bool
482 pax_aslr_elf_flags_active(uint32_t flags)
483 {
484 if (!pax_aslr_enabled)
485 return false;
486 if (pax_aslr_global && (flags & ELF_NOTE_PAX_NOASLR) != 0) {
487 /* ASLR explicitly disabled */
488 return false;
489 }
490 if (!pax_aslr_global && (flags & ELF_NOTE_PAX_ASLR) == 0) {
491 /* ASLR not requested */
492 return false;
493 }
494 return true;
495 }
496
497 static bool
498 pax_aslr_epp_active(struct exec_package *epp)
499 {
500 if (__predict_false((epp->ep_flags & (EXEC_32|EXEC_TOPDOWN_VM)) == 0))
501 return false;
502 return pax_flags_active(epp->ep_pax_flags, P_PAX_ASLR);
503 }
504
505 static bool
506 pax_aslr_active(struct lwp *l)
507 {
508 return pax_flags_active(l->l_proc->p_pax, P_PAX_ASLR);
509 }
510
511 void
512 pax_aslr_init_vm(struct lwp *l, struct vmspace *vm, struct exec_package *ep)
513 {
514 if (!pax_aslr_active(l))
515 return;
516
517 if (__predict_false((ep->ep_flags & (EXEC_32|EXEC_TOPDOWN_VM)) == 0))
518 return;
519
520 #ifdef PAX_ASLR_DEBUG
521 if (pax_aslr_flags & PAX_ASLR_MMAP)
522 return;
523 #endif
524
525 uint32_t len = (ep->ep_flags & EXEC_32) ?
526 PAX_ASLR_DELTA_MMAP_LEN32 : PAX_ASLR_DELTA_MMAP_LEN;
527
528 uint32_t rand = cprng_fast32();
529 #ifdef PAX_ASLR_DEBUG
530 if (pax_aslr_flags & PAX_ASLR_FIXED)
531 rand = pax_aslr_rand;
532 #endif
533 vm->vm_aslr_delta_mmap = PAX_ASLR_DELTA(rand,
534 PAX_ASLR_DELTA_MMAP_LSB, len);
535
536 PAX_DPRINTF("delta_mmap=%#jx/%u",
537 (uintmax_t)vm->vm_aslr_delta_mmap, len);
538 }
539
540 void
541 pax_aslr_mmap(struct lwp *l, vaddr_t *addr, vaddr_t orig_addr, int f)
542 {
543 if (!pax_aslr_active(l))
544 return;
545 #ifdef PAX_ASLR_DEBUG
546 char buf[256];
547
548 if (pax_aslr_flags & PAX_ASLR_MMAP)
549 return;
550
551 if (pax_aslr_debug)
552 snprintb(buf, sizeof(buf), MAP_FMT, f);
553 else
554 buf[0] = '\0';
555 #endif
556
557 if (!(f & MAP_FIXED) && ((orig_addr == 0) || !(f & MAP_ANON))) {
558 PAX_DPRINTF("applying to %#jx orig_addr=%#jx f=%s",
559 (uintmax_t)*addr, (uintmax_t)orig_addr, buf);
560 if (!(l->l_proc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
561 *addr += l->l_proc->p_vmspace->vm_aslr_delta_mmap;
562 else
563 *addr -= l->l_proc->p_vmspace->vm_aslr_delta_mmap;
564 PAX_DPRINTF("result %#jx", (uintmax_t)*addr);
565 } else {
566 PAX_DPRINTF("not applying to %#jx orig_addr=%#jx f=%s",
567 (uintmax_t)*addr, (uintmax_t)orig_addr, buf);
568 }
569 }
570
571 static vaddr_t
572 pax_aslr_offset(vaddr_t align)
573 {
574 size_t pax_align, l2, delta;
575 uint32_t rand;
576 vaddr_t offset;
577
578 pax_align = align == 0 ? PGSHIFT : align;
579 l2 = ilog2(pax_align);
580
581 rand = cprng_fast32();
582 #ifdef PAX_ASLR_DEBUG
583 if (pax_aslr_flags & PAX_ASLR_FIXED)
584 rand = pax_aslr_rand;
585 #endif
586
587 #define PAX_TRUNC(a, b) ((a) & ~((b) - 1))
588
589 delta = PAX_ASLR_DELTA(rand, l2, PAX_ASLR_DELTA_EXEC_LEN);
590 offset = PAX_TRUNC(delta, pax_align) + PAGE_SIZE;
591
592 PAX_DPRINTF("rand=%#x l2=%#zx pax_align=%#zx delta=%#zx offset=%#jx",
593 rand, l2, pax_align, delta, (uintmax_t)offset);
594
595 return offset;
596 }
597
598 vaddr_t
599 pax_aslr_exec_offset(struct exec_package *epp, vaddr_t align)
600 {
601 if (!pax_aslr_epp_active(epp))
602 goto out;
603
604 #ifdef PAX_ASLR_DEBUG
605 if (pax_aslr_flags & PAX_ASLR_EXEC_OFFSET)
606 goto out;
607 #endif
608 return pax_aslr_offset(align) + PAGE_SIZE;
609 out:
610 return MAX(align, PAGE_SIZE);
611 }
612
613 voff_t
614 pax_aslr_rtld_offset(struct exec_package *epp, vaddr_t align, int use_topdown)
615 {
616 voff_t offset;
617
618 if (!pax_aslr_epp_active(epp))
619 return 0;
620
621 #ifdef PAX_ASLR_DEBUG
622 if (pax_aslr_flags & PAX_ASLR_RTLD_OFFSET)
623 return 0;
624 #endif
625 offset = pax_aslr_offset(align);
626 if (use_topdown)
627 offset = -offset;
628
629 return offset;
630 }
631
632 void
633 pax_aslr_stack(struct exec_package *epp, u_long *max_stack_size)
634 {
635 if (!pax_aslr_epp_active(epp))
636 return;
637 #ifdef PAX_ASLR_DEBUG
638 if (pax_aslr_flags & PAX_ASLR_STACK)
639 return;
640 #endif
641
642 uint32_t len = (epp->ep_flags & EXEC_32) ?
643 PAX_ASLR_DELTA_STACK_LEN32 : PAX_ASLR_DELTA_STACK_LEN;
644 uint32_t rand = cprng_fast32();
645 #ifdef PAX_ASLR_DEBUG
646 if (pax_aslr_flags & PAX_ASLR_FIXED)
647 rand = pax_aslr_rand;
648 #endif
649 u_long d = PAX_ASLR_DELTA(rand, PAX_ASLR_DELTA_STACK_LSB, len);
650 d &= (*max_stack_size / PAX_ASLR_MAX_STACK_WASTE) - 1;
651 u_long newminsaddr = (u_long)STACK_GROW(epp->ep_minsaddr, d);
652 PAX_DPRINTF("old minsaddr=%#jx delta=%#lx new minsaddr=%#lx",
653 (uintmax_t)epp->ep_minsaddr, d, newminsaddr);
654 epp->ep_minsaddr = (vaddr_t)newminsaddr;
655 *max_stack_size -= d;
656 }
657
658 uint32_t
659 pax_aslr_stack_gap(struct exec_package *epp)
660 {
661 if (!pax_aslr_epp_active(epp))
662 return 0;
663
664 #ifdef PAX_ASLR_DEBUG
665 if (pax_aslr_flags & PAX_ASLR_STACK_GAP)
666 return 0;
667 #endif
668
669 uint32_t rand = cprng_fast32();
670 #ifdef PAX_ASLR_DEBUG
671 if (pax_aslr_flags & PAX_ASLR_FIXED)
672 rand = pax_aslr_rand;
673 #endif
674 rand %= PAGE_SIZE;
675 PAX_DPRINTF("stack gap=%#x\n", rand);
676 return rand;
677 }
678 #endif /* PAX_ASLR */
679
680 #ifdef PAX_SEGVGUARD
681 static bool
682 pax_segvguard_elf_flags_active(uint32_t flags)
683 {
684 if (!pax_segvguard_enabled)
685 return false;
686 if (pax_segvguard_global && (flags & ELF_NOTE_PAX_NOGUARD) != 0) {
687 /* Segvguard explicitly disabled */
688 return false;
689 }
690 if (!pax_segvguard_global && (flags & ELF_NOTE_PAX_GUARD) == 0) {
691 /* Segvguard not requested */
692 return false;
693 }
694 return true;
695 }
696
697 static void
698 pax_segvguard_cleanup_cb(void *v)
699 {
700 struct pax_segvguard_entry *p = v;
701 struct pax_segvguard_uid_entry *up;
702
703 if (p == NULL) {
704 return;
705 }
706 while ((up = LIST_FIRST(&p->segv_uids)) != NULL) {
707 LIST_REMOVE(up, sue_list);
708 kmem_free(up, sizeof(*up));
709 }
710 kmem_free(p, sizeof(*p));
711 }
712
713 /*
714 * Called when a process of image vp generated a segfault.
715 */
716 int
717 pax_segvguard(struct lwp *l, struct vnode *vp, const char *name,
718 bool crashed)
719 {
720 struct pax_segvguard_entry *p;
721 struct pax_segvguard_uid_entry *up;
722 struct timeval tv;
723 uid_t uid;
724 uint32_t flags;
725 bool have_uid;
726
727 flags = l->l_proc->p_pax;
728 if (!pax_flags_active(flags, P_PAX_GUARD))
729 return 0;
730
731 if (vp == NULL)
732 return EFAULT;
733
734 /* Check if we already monitor the file. */
735 p = fileassoc_lookup(vp, segvguard_id);
736
737 /* Fast-path if starting a program we don't know. */
738 if (p == NULL && !crashed)
739 return 0;
740
741 microtime(&tv);
742
743 /*
744 * If a program we don't know crashed, we need to create a new entry
745 * for it.
746 */
747 if (p == NULL) {
748 p = kmem_alloc(sizeof(*p), KM_SLEEP);
749 fileassoc_add(vp, segvguard_id, p);
750 LIST_INIT(&p->segv_uids);
751
752 /*
753 * Initialize a new entry with "crashes so far" of 1.
754 * The expiry time is when we purge the entry if it didn't
755 * reach the limit.
756 */
757 up = kmem_alloc(sizeof(*up), KM_SLEEP);
758 up->sue_uid = kauth_cred_getuid(l->l_cred);
759 up->sue_ncrashes = 1;
760 up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
761 up->sue_suspended = 0;
762 LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
763 return 0;
764 }
765
766 /*
767 * A program we "know" either executed or crashed again.
768 * See if it's a culprit we're familiar with.
769 */
770 uid = kauth_cred_getuid(l->l_cred);
771 have_uid = false;
772 LIST_FOREACH(up, &p->segv_uids, sue_list) {
773 if (up->sue_uid == uid) {
774 have_uid = true;
775 break;
776 }
777 }
778
779 /*
780 * It's someone else. Add an entry for him if we crashed.
781 */
782 if (!have_uid) {
783 if (crashed) {
784 up = kmem_alloc(sizeof(*up), KM_SLEEP);
785 up->sue_uid = uid;
786 up->sue_ncrashes = 1;
787 up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
788 up->sue_suspended = 0;
789 LIST_INSERT_HEAD(&p->segv_uids, up, sue_list);
790 }
791 return 0;
792 }
793
794 if (crashed) {
795 /* Check if timer on previous crashes expired first. */
796 if (up->sue_expiry < tv.tv_sec) {
797 log(LOG_INFO, "PaX Segvguard: [%s] Suspension"
798 " expired.\n", name ? name : "unknown");
799 up->sue_ncrashes = 1;
800 up->sue_expiry = tv.tv_sec + pax_segvguard_expiry;
801 up->sue_suspended = 0;
802 return 0;
803 }
804
805 up->sue_ncrashes++;
806
807 if (up->sue_ncrashes >= pax_segvguard_maxcrashes) {
808 log(LOG_ALERT, "PaX Segvguard: [%s] Suspending "
809 "execution for %d seconds after %zu crashes.\n",
810 name ? name : "unknown", pax_segvguard_suspension,
811 up->sue_ncrashes);
812
813 /* Suspend this program for a while. */
814 up->sue_suspended = tv.tv_sec + pax_segvguard_suspension;
815 up->sue_ncrashes = 0;
816 up->sue_expiry = 0;
817 }
818 } else {
819 /* Are we supposed to be suspended? */
820 if (up->sue_suspended > tv.tv_sec) {
821 log(LOG_ALERT, "PaX Segvguard: [%s] Preventing "
822 "execution due to repeated segfaults.\n", name ?
823 name : "unknown");
824 return EPERM;
825 }
826 }
827
828 return 0;
829 }
830 #endif /* PAX_SEGVGUARD */
831