1 /* $NetBSD: kern_pax.c,v 1.61 2020/01/23 10:21:14 ad Exp $ */ 2 3 /* 4 * Copyright (c) 2015, 2020 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@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.61 2020/01/23 10:21:14 ad 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/syslog.h> 73 #include <sys/vnode.h> 74 #include <sys/queue.h> 75 #include <sys/bitops.h> 76 #include <sys/kauth.h> 77 #include <sys/cprng.h> 78 79 #ifdef PAX_ASLR_DEBUG 80 #define PAX_DPRINTF(_fmt, args...) \ 81 do if (pax_aslr_debug) uprintf("%s: " _fmt "\n", __func__, ##args); \ 82 while (/*CONSTCOND*/0) 83 #else 84 #define PAX_DPRINTF(_fmt, args...) do {} while (/*CONSTCOND*/0) 85 #endif 86 87 #ifdef PAX_ASLR 88 #include <sys/mman.h> 89 90 int pax_aslr_enabled = 1; 91 int pax_aslr_global = PAX_ASLR; 92 93 #ifndef PAX_ASLR_DELTA_MMAP_LSB 94 #define PAX_ASLR_DELTA_MMAP_LSB PGSHIFT 95 #endif 96 #ifndef PAX_ASLR_DELTA_MMAP_LEN 97 #define PAX_ASLR_DELTA_MMAP_LEN ((sizeof(void *) * NBBY) / 2) 98 #endif 99 #ifndef PAX_ASLR_DELTA_MMAP_LEN32 100 #define PAX_ASLR_DELTA_MMAP_LEN32 ((sizeof(uint32_t) * NBBY) / 2) 101 #endif 102 #ifndef PAX_ASLR_DELTA_STACK_LSB 103 #define PAX_ASLR_DELTA_STACK_LSB PGSHIFT 104 #endif 105 #ifndef PAX_ASLR_DELTA_STACK_LEN 106 #define PAX_ASLR_DELTA_STACK_LEN ((sizeof(void *) * NBBY) / 4) 107 #endif 108 #ifndef PAX_ASLR_DELTA_STACK_LEN32 109 #define PAX_ASLR_DELTA_STACK_LEN32 ((sizeof(uint32_t) * NBBY) / 4) 110 #endif 111 #define PAX_ASLR_MAX_STACK_WASTE 8 112 113 #ifdef PAX_ASLR_DEBUG 114 int pax_aslr_debug; 115 /* flag set means disable */ 116 int pax_aslr_flags; 117 uint32_t pax_aslr_rand; 118 #define PAX_ASLR_STACK 0x01 119 #define PAX_ASLR_STACK_GAP 0x02 120 #define PAX_ASLR_MMAP 0x04 121 #define PAX_ASLR_EXEC_OFFSET 0x08 122 #define PAX_ASLR_RTLD_OFFSET 0x10 123 #define PAX_ASLR_FIXED 0x20 124 #endif 125 126 static bool pax_aslr_elf_flags_active(uint32_t); 127 #endif /* PAX_ASLR */ 128 129 #ifdef PAX_MPROTECT 130 static int pax_mprotect_enabled = 1; 131 static int pax_mprotect_global = PAX_MPROTECT; 132 static int pax_mprotect_ptrace = 1; 133 static bool pax_mprotect_elf_flags_active(uint32_t); 134 #endif /* PAX_MPROTECT */ 135 #ifdef PAX_MPROTECT_DEBUG 136 int pax_mprotect_debug; 137 #endif 138 139 #ifdef PAX_SEGVGUARD 140 #ifndef PAX_SEGVGUARD_EXPIRY 141 #define PAX_SEGVGUARD_EXPIRY (2 * 60) 142 #endif 143 #ifndef PAX_SEGVGUARD_SUSPENSION 144 #define PAX_SEGVGUARD_SUSPENSION (10 * 60) 145 #endif 146 #ifndef PAX_SEGVGUARD_MAXCRASHES 147 #define PAX_SEGVGUARD_MAXCRASHES 5 148 #endif 149 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 struct pax_segvguard_uid_entry { 158 uid_t sue_uid; 159 size_t sue_ncrashes; 160 time_t sue_expiry; 161 time_t sue_suspended; 162 LIST_ENTRY(pax_segvguard_uid_entry) sue_list; 163 }; 164 165 struct pax_segvguard_entry { 166 LIST_HEAD(, pax_segvguard_uid_entry) segv_uids; 167 }; 168 169 static bool pax_segvguard_elf_flags_active(uint32_t); 170 #endif /* PAX_SEGVGUARD */ 171 172 SYSCTL_SETUP(sysctl_security_pax_setup, "sysctl security.pax setup") 173 { 174 const struct sysctlnode *rnode = NULL, *cnode; 175 176 sysctl_createv(clog, 0, NULL, &rnode, 177 CTLFLAG_PERMANENT, 178 CTLTYPE_NODE, "pax", 179 SYSCTL_DESCR("PaX (exploit mitigation) features."), 180 NULL, 0, NULL, 0, 181 CTL_SECURITY, CTL_CREATE, CTL_EOL); 182 183 cnode = rnode; 184 185 #ifdef PAX_MPROTECT 186 rnode = cnode; 187 sysctl_createv(clog, 0, &rnode, &rnode, 188 CTLFLAG_PERMANENT, 189 CTLTYPE_NODE, "mprotect", 190 SYSCTL_DESCR("mprotect(2) W^X restrictions."), 191 NULL, 0, NULL, 0, 192 CTL_CREATE, CTL_EOL); 193 sysctl_createv(clog, 0, &rnode, NULL, 194 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 195 CTLTYPE_INT, "enabled", 196 SYSCTL_DESCR("Restrictions enabled."), 197 NULL, 0, &pax_mprotect_enabled, 0, 198 CTL_CREATE, CTL_EOL); 199 sysctl_createv(clog, 0, &rnode, NULL, 200 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 201 CTLTYPE_INT, "global", 202 SYSCTL_DESCR("When enabled, unless explicitly " 203 "specified, apply restrictions to " 204 "all processes."), 205 NULL, 0, &pax_mprotect_global, 0, 206 CTL_CREATE, CTL_EOL); 207 sysctl_createv(clog, 0, &rnode, NULL, 208 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 209 CTLTYPE_INT, "ptrace", 210 SYSCTL_DESCR("When enabled, allow ptrace(2) to " 211 "override mprotect permissions on traced " 212 "processes"), 213 NULL, 0, &pax_mprotect_ptrace, 0, 214 CTL_CREATE, CTL_EOL); 215 #ifdef PAX_MPROTECT_DEBUG 216 sysctl_createv(clog, 0, &rnode, NULL, 217 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 218 CTLTYPE_INT, "debug", 219 SYSCTL_DESCR("print mprotect changes."), 220 NULL, 0, &pax_mprotect_debug, 0, 221 CTL_CREATE, CTL_EOL); 222 #endif 223 #endif /* PAX_MPROTECT */ 224 225 #ifdef PAX_SEGVGUARD 226 rnode = cnode; 227 sysctl_createv(clog, 0, &rnode, &rnode, 228 CTLFLAG_PERMANENT, 229 CTLTYPE_NODE, "segvguard", 230 SYSCTL_DESCR("PaX segvguard."), 231 NULL, 0, NULL, 0, 232 CTL_CREATE, CTL_EOL); 233 sysctl_createv(clog, 0, &rnode, NULL, 234 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 235 CTLTYPE_INT, "enabled", 236 SYSCTL_DESCR("segvguard enabled."), 237 NULL, 0, &pax_segvguard_enabled, 0, 238 CTL_CREATE, CTL_EOL); 239 sysctl_createv(clog, 0, &rnode, NULL, 240 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 241 CTLTYPE_INT, "global", 242 SYSCTL_DESCR("segvguard all programs."), 243 NULL, 0, &pax_segvguard_global, 0, 244 CTL_CREATE, CTL_EOL); 245 sysctl_createv(clog, 0, &rnode, NULL, 246 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 247 CTLTYPE_INT, "expiry_timeout", 248 SYSCTL_DESCR("Entry expiry timeout (in seconds)."), 249 NULL, 0, &pax_segvguard_expiry, 0, 250 CTL_CREATE, CTL_EOL); 251 sysctl_createv(clog, 0, &rnode, NULL, 252 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 253 CTLTYPE_INT, "suspend_timeout", 254 SYSCTL_DESCR("Entry suspension timeout (in seconds)."), 255 NULL, 0, &pax_segvguard_suspension, 0, 256 CTL_CREATE, CTL_EOL); 257 sysctl_createv(clog, 0, &rnode, NULL, 258 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 259 CTLTYPE_INT, "max_crashes", 260 SYSCTL_DESCR("Max number of crashes before expiry."), 261 NULL, 0, &pax_segvguard_maxcrashes, 0, 262 CTL_CREATE, CTL_EOL); 263 #endif /* PAX_SEGVGUARD */ 264 265 #ifdef PAX_ASLR 266 rnode = cnode; 267 sysctl_createv(clog, 0, &rnode, &rnode, 268 CTLFLAG_PERMANENT, 269 CTLTYPE_NODE, "aslr", 270 SYSCTL_DESCR("Address Space Layout Randomization."), 271 NULL, 0, NULL, 0, 272 CTL_CREATE, CTL_EOL); 273 sysctl_createv(clog, 0, &rnode, NULL, 274 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 275 CTLTYPE_INT, "enabled", 276 SYSCTL_DESCR("Restrictions enabled."), 277 NULL, 0, &pax_aslr_enabled, 0, 278 CTL_CREATE, CTL_EOL); 279 sysctl_createv(clog, 0, &rnode, NULL, 280 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 281 CTLTYPE_INT, "global", 282 SYSCTL_DESCR("When enabled, unless explicitly " 283 "specified, apply to all processes."), 284 NULL, 0, &pax_aslr_global, 0, 285 CTL_CREATE, CTL_EOL); 286 #ifdef PAX_ASLR_DEBUG 287 sysctl_createv(clog, 0, &rnode, NULL, 288 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 289 CTLTYPE_INT, "debug", 290 SYSCTL_DESCR("Print ASLR selected addresses."), 291 NULL, 0, &pax_aslr_debug, 0, 292 CTL_CREATE, CTL_EOL); 293 sysctl_createv(clog, 0, &rnode, NULL, 294 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 295 CTLTYPE_INT, "flags", 296 SYSCTL_DESCR("Disable/Enable select ASLR features."), 297 NULL, 0, &pax_aslr_flags, 0, 298 CTL_CREATE, CTL_EOL); 299 sysctl_createv(clog, 0, &rnode, NULL, 300 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 301 CTLTYPE_INT, "rand", 302 SYSCTL_DESCR("Use the given fixed random value"), 303 NULL, 0, &pax_aslr_rand, 0, 304 CTL_CREATE, CTL_EOL); 305 #endif 306 sysctl_createv(clog, 0, &rnode, NULL, 307 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 308 CTLTYPE_INT, "mmap_len", 309 SYSCTL_DESCR("Number of bits randomized for " 310 "mmap(2) calls."), 311 NULL, PAX_ASLR_DELTA_MMAP_LEN, NULL, 0, 312 CTL_CREATE, CTL_EOL); 313 sysctl_createv(clog, 0, &rnode, NULL, 314 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 315 CTLTYPE_INT, "stack_len", 316 SYSCTL_DESCR("Number of bits randomized for " 317 "the stack."), 318 NULL, PAX_ASLR_DELTA_STACK_LEN, NULL, 0, 319 CTL_CREATE, CTL_EOL); 320 sysctl_createv(clog, 0, &rnode, NULL, 321 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 322 CTLTYPE_INT, "exec_len", 323 SYSCTL_DESCR("Number of bits randomized for " 324 "the PIE exec base."), 325 NULL, PAX_ASLR_DELTA_EXEC_LEN, NULL, 0, 326 CTL_CREATE, CTL_EOL); 327 328 #endif /* PAX_ASLR */ 329 } 330 331 /* 332 * Initialize PaX. 333 */ 334 void 335 pax_init(void) 336 { 337 #ifdef PAX_ASLR 338 /* Adjust maximum stack by the size we can consume for ASLR */ 339 extern rlim_t maxsmap; 340 maxsmap = MAXSSIZ - (MAXSSIZ / PAX_ASLR_MAX_STACK_WASTE); 341 // XXX: compat32 is not handled. 342 #endif 343 } 344 345 void 346 pax_set_flags(struct exec_package *epp, struct proc *p) 347 { 348 p->p_pax = epp->ep_pax_flags; 349 350 #ifdef PAX_MPROTECT 351 if (pax_mprotect_ptrace == 0) 352 return; 353 /* 354 * If we are running under the debugger, turn off MPROTECT so 355 * the debugger can insert/delete breakpoints 356 */ 357 if (p->p_slflag & PSL_TRACED) 358 p->p_pax &= ~P_PAX_MPROTECT; 359 #endif 360 } 361 362 void 363 pax_setup_elf_flags(struct exec_package *epp, uint32_t elf_flags) 364 { 365 uint32_t flags = 0; 366 367 #ifdef PAX_ASLR 368 if (pax_aslr_elf_flags_active(elf_flags)) { 369 flags |= P_PAX_ASLR; 370 } 371 #endif 372 #ifdef PAX_MPROTECT 373 if (pax_mprotect_elf_flags_active(elf_flags)) { 374 flags |= P_PAX_MPROTECT; 375 } 376 #endif 377 #ifdef PAX_SEGVGUARD 378 if (pax_segvguard_elf_flags_active(elf_flags)) { 379 flags |= P_PAX_GUARD; 380 } 381 #endif 382 383 epp->ep_pax_flags = flags; 384 } 385 386 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 387 static inline bool 388 pax_flags_active(uint32_t flags, uint32_t opt) 389 { 390 if (!(flags & opt)) 391 return false; 392 return true; 393 } 394 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 395 396 #ifdef PAX_MPROTECT 397 static bool 398 pax_mprotect_elf_flags_active(uint32_t flags) 399 { 400 if (!pax_mprotect_enabled) 401 return false; 402 if (pax_mprotect_global && (flags & ELF_NOTE_PAX_NOMPROTECT) != 0) { 403 /* Mprotect explicitly disabled */ 404 return false; 405 } 406 if (!pax_mprotect_global && (flags & ELF_NOTE_PAX_MPROTECT) == 0) { 407 /* Mprotect not requested */ 408 return false; 409 } 410 return true; 411 } 412 413 vm_prot_t 414 pax_mprotect_maxprotect( 415 #ifdef PAX_MPROTECT_DEBUG 416 const char *file, size_t line, 417 #endif 418 struct lwp *l, vm_prot_t active, vm_prot_t extra, vm_prot_t maxprot) 419 { 420 uint32_t flags; 421 422 flags = l->l_proc->p_pax; 423 if (!pax_flags_active(flags, P_PAX_MPROTECT)) 424 return maxprot; 425 426 return (active|extra) & maxprot; 427 } 428 429 int 430 pax_mprotect_validate( 431 #ifdef PAX_MPROTECT_DEBUG 432 const char *file, size_t line, 433 #endif 434 struct lwp *l, vm_prot_t prot) 435 { 436 uint32_t flags; 437 438 flags = l->l_proc->p_pax; 439 if (!pax_flags_active(flags, P_PAX_MPROTECT)) 440 return 0; 441 442 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) == 443 (VM_PROT_WRITE|VM_PROT_EXECUTE)) { 444 #ifdef PAX_MPROTECT_DEBUG 445 struct proc *p = l->l_proc; 446 447 if (pax_mprotect_debug) 448 printf("%s: %s,%zu: %d.%d (%s): WX rejected\n", 449 __func__, file, line, 450 p->p_pid, l->l_lid, p->p_comm); 451 #endif 452 return EACCES; 453 } 454 return 0; 455 } 456 457 /* 458 * Bypass MPROTECT for traced processes 459 */ 460 int 461 pax_mprotect_prot(struct lwp *l) 462 { 463 uint32_t flags; 464 465 flags = l->l_proc->p_pax; 466 if (!pax_flags_active(flags, P_PAX_MPROTECT)) 467 return 0; 468 if (pax_mprotect_ptrace < 2) 469 return 0; 470 return UVM_EXTRACT_PROT_ALL; 471 } 472 473 474 #endif /* PAX_MPROTECT */ 475 476 #ifdef PAX_ASLR 477 static bool 478 pax_aslr_elf_flags_active(uint32_t flags) 479 { 480 if (!pax_aslr_enabled) 481 return false; 482 if (pax_aslr_global && (flags & ELF_NOTE_PAX_NOASLR) != 0) { 483 /* ASLR explicitly disabled */ 484 return false; 485 } 486 if (!pax_aslr_global && (flags & ELF_NOTE_PAX_ASLR) == 0) { 487 /* ASLR not requested */ 488 return false; 489 } 490 return true; 491 } 492 493 static bool 494 pax_aslr_epp_active(struct exec_package *epp) 495 { 496 if (__predict_false((epp->ep_flags & (EXEC_32|EXEC_TOPDOWN_VM)) == 0)) 497 return false; 498 return pax_flags_active(epp->ep_pax_flags, P_PAX_ASLR); 499 } 500 501 static bool 502 pax_aslr_active(struct lwp *l) 503 { 504 return pax_flags_active(l->l_proc->p_pax, P_PAX_ASLR); 505 } 506 507 void 508 pax_aslr_init_vm(struct lwp *l, struct vmspace *vm, struct exec_package *ep) 509 { 510 if (!pax_aslr_active(l)) 511 return; 512 513 if (__predict_false((ep->ep_flags & (EXEC_32|EXEC_TOPDOWN_VM)) == 0)) 514 return; 515 516 #ifdef PAX_ASLR_DEBUG 517 if (pax_aslr_flags & PAX_ASLR_MMAP) 518 return; 519 #endif 520 521 uint32_t len = (ep->ep_flags & EXEC_32) ? 522 PAX_ASLR_DELTA_MMAP_LEN32 : PAX_ASLR_DELTA_MMAP_LEN; 523 524 uint32_t rand = cprng_fast32(); 525 #ifdef PAX_ASLR_DEBUG 526 if (pax_aslr_flags & PAX_ASLR_FIXED) 527 rand = pax_aslr_rand; 528 #endif 529 vm->vm_aslr_delta_mmap = PAX_ASLR_DELTA(rand, 530 PAX_ASLR_DELTA_MMAP_LSB, len); 531 532 PAX_DPRINTF("delta_mmap=%#jx/%u", 533 (uintmax_t)vm->vm_aslr_delta_mmap, len); 534 } 535 536 void 537 pax_aslr_mmap(struct lwp *l, vaddr_t *addr, vaddr_t orig_addr, int f) 538 { 539 if (!pax_aslr_active(l)) 540 return; 541 #ifdef PAX_ASLR_DEBUG 542 char buf[256]; 543 544 if (pax_aslr_flags & PAX_ASLR_MMAP) 545 return; 546 547 if (pax_aslr_debug) 548 snprintb(buf, sizeof(buf), MAP_FMT, f); 549 else 550 buf[0] = '\0'; 551 #endif 552 553 if (!(f & MAP_FIXED) && ((orig_addr == 0) || !(f & MAP_ANON))) { 554 PAX_DPRINTF("applying to %#jx orig_addr=%#jx f=%s", 555 (uintmax_t)*addr, (uintmax_t)orig_addr, buf); 556 if (!(l->l_proc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)) 557 *addr += l->l_proc->p_vmspace->vm_aslr_delta_mmap; 558 else 559 *addr -= l->l_proc->p_vmspace->vm_aslr_delta_mmap; 560 PAX_DPRINTF("result %#jx", (uintmax_t)*addr); 561 } else { 562 PAX_DPRINTF("not applying to %#jx orig_addr=%#jx f=%s", 563 (uintmax_t)*addr, (uintmax_t)orig_addr, buf); 564 } 565 } 566 567 static vaddr_t 568 pax_aslr_offset(vaddr_t align) 569 { 570 size_t pax_align, l2, delta; 571 uint32_t rand; 572 vaddr_t offset; 573 574 pax_align = align == 0 ? PAGE_SIZE : align; 575 l2 = ilog2(pax_align); 576 577 rand = cprng_fast32(); 578 #ifdef PAX_ASLR_DEBUG 579 if (pax_aslr_flags & PAX_ASLR_FIXED) 580 rand = pax_aslr_rand; 581 #endif 582 583 #define PAX_TRUNC(a, b) ((a) & ~((b) - 1)) 584 585 delta = PAX_ASLR_DELTA(rand, l2, PAX_ASLR_DELTA_EXEC_LEN); 586 offset = PAX_TRUNC(delta, pax_align); 587 offset = MAX(offset, pax_align); 588 589 PAX_DPRINTF("rand=%#x l2=%#zx pax_align=%#zx delta=%#zx offset=%#jx", 590 rand, l2, pax_align, delta, (uintmax_t)offset); 591 592 return offset; 593 } 594 595 vaddr_t 596 pax_aslr_exec_offset(struct exec_package *epp, vaddr_t align) 597 { 598 if (!pax_aslr_epp_active(epp)) 599 goto out; 600 601 #ifdef PAX_ASLR_DEBUG 602 if (pax_aslr_flags & PAX_ASLR_EXEC_OFFSET) 603 goto out; 604 #endif 605 return pax_aslr_offset(align); 606 out: 607 return 0; 608 } 609 610 voff_t 611 pax_aslr_rtld_offset(struct exec_package *epp, vaddr_t align, int use_topdown) 612 { 613 voff_t offset; 614 615 if (!pax_aslr_epp_active(epp)) 616 return 0; 617 618 #ifdef PAX_ASLR_DEBUG 619 if (pax_aslr_flags & PAX_ASLR_RTLD_OFFSET) 620 return 0; 621 #endif 622 offset = pax_aslr_offset(align); 623 if (use_topdown) 624 offset = -offset; 625 626 return offset; 627 } 628 629 void 630 pax_aslr_stack(struct exec_package *epp, vsize_t *max_stack_size) 631 { 632 if (!pax_aslr_epp_active(epp)) 633 return; 634 #ifdef PAX_ASLR_DEBUG 635 if (pax_aslr_flags & PAX_ASLR_STACK) 636 return; 637 #endif 638 639 uint32_t len = (epp->ep_flags & EXEC_32) ? 640 PAX_ASLR_DELTA_STACK_LEN32 : PAX_ASLR_DELTA_STACK_LEN; 641 uint32_t rand = cprng_fast32(); 642 #ifdef PAX_ASLR_DEBUG 643 if (pax_aslr_flags & PAX_ASLR_FIXED) 644 rand = pax_aslr_rand; 645 #endif 646 u_long d = PAX_ASLR_DELTA(rand, PAX_ASLR_DELTA_STACK_LSB, len); 647 d &= (*max_stack_size / PAX_ASLR_MAX_STACK_WASTE) - 1; 648 u_long newminsaddr = (u_long)STACK_GROW(epp->ep_minsaddr, d); 649 PAX_DPRINTF("old minsaddr=%#jx delta=%#lx new minsaddr=%#lx", 650 (uintmax_t)epp->ep_minsaddr, d, newminsaddr); 651 epp->ep_minsaddr = (vaddr_t)newminsaddr; 652 *max_stack_size -= d; 653 } 654 655 uint32_t 656 pax_aslr_stack_gap(struct exec_package *epp) 657 { 658 if (!pax_aslr_epp_active(epp)) 659 return 0; 660 661 #ifdef PAX_ASLR_DEBUG 662 if (pax_aslr_flags & PAX_ASLR_STACK_GAP) 663 return 0; 664 #endif 665 666 uint32_t rand = cprng_fast32(); 667 #ifdef PAX_ASLR_DEBUG 668 if (pax_aslr_flags & PAX_ASLR_FIXED) 669 rand = pax_aslr_rand; 670 #endif 671 rand %= PAGE_SIZE; 672 PAX_DPRINTF("stack gap=%#x\n", rand); 673 return rand; 674 } 675 #endif /* PAX_ASLR */ 676 677 #ifdef PAX_SEGVGUARD 678 static bool 679 pax_segvguard_elf_flags_active(uint32_t flags) 680 { 681 if (!pax_segvguard_enabled) 682 return false; 683 if (pax_segvguard_global && (flags & ELF_NOTE_PAX_NOGUARD) != 0) { 684 /* Segvguard explicitly disabled */ 685 return false; 686 } 687 if (!pax_segvguard_global && (flags & ELF_NOTE_PAX_GUARD) == 0) { 688 /* Segvguard not requested */ 689 return false; 690 } 691 return true; 692 } 693 694 void 695 pax_segvguard_cleanup(struct vnode *vp) 696 { 697 struct pax_segvguard_entry *p = vp->v_segvguard; 698 struct pax_segvguard_uid_entry *up; 699 700 if (__predict_true(p == NULL)) { 701 return; 702 } 703 while ((up = LIST_FIRST(&p->segv_uids)) != NULL) { 704 LIST_REMOVE(up, sue_list); 705 kmem_free(up, sizeof(*up)); 706 } 707 kmem_free(p, sizeof(*p)); 708 vp->v_segvguard = NULL; 709 } 710 711 /* 712 * Called when a process of image vp generated a segfault. 713 * 714 * => exec_lock must be held by the caller 715 * => if "crashed" is true, exec_lock must be held for write 716 */ 717 int 718 pax_segvguard(struct lwp *l, struct vnode *vp, const char *name, 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 KASSERT(rw_lock_held(&exec_lock)); 728 KASSERT(!crashed || rw_write_held(&exec_lock)); 729 730 flags = l->l_proc->p_pax; 731 if (!pax_flags_active(flags, P_PAX_GUARD)) 732 return 0; 733 734 if (vp == NULL) 735 return EFAULT; 736 737 /* Fast-path if starting a program we don't know. */ 738 if ((p = vp->v_segvguard) == 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 vp->v_segvguard = 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