1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 /* 29 * Copyright (c) 2019, Joyent, Inc. 30 * Copyright 2022 Oxide Computer Company 31 */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/thread.h> 36 #include <sys/sysmacros.h> 37 #include <sys/signal.h> 38 #include <sys/cred.h> 39 #include <sys/user.h> 40 #include <sys/errno.h> 41 #include <sys/vnode.h> 42 #include <sys/mman.h> 43 #include <sys/kmem.h> 44 #include <sys/proc.h> 45 #include <sys/pathname.h> 46 #include <sys/policy.h> 47 #include <sys/cmn_err.h> 48 #include <sys/systm.h> 49 #include <sys/elf.h> 50 #include <sys/vmsystm.h> 51 #include <sys/debug.h> 52 #include <sys/auxv.h> 53 #include <sys/exec.h> 54 #include <sys/prsystm.h> 55 #include <vm/as.h> 56 #include <vm/rm.h> 57 #include <vm/seg.h> 58 #include <vm/seg_vn.h> 59 #include <sys/modctl.h> 60 #include <sys/systeminfo.h> 61 #include <sys/vmparam.h> 62 #include <sys/machelf.h> 63 #include <sys/shm_impl.h> 64 #include <sys/archsystm.h> 65 #include <sys/fasttrap.h> 66 #include <sys/brand.h> 67 #include "elf_impl.h" 68 #include <sys/sdt.h> 69 #include <sys/siginfo.h> 70 #include <sys/random.h> 71 72 #include <core_shstrtab.h> 73 74 #if defined(__x86) 75 #include <sys/comm_page_util.h> 76 #include <sys/fp.h> 77 #endif /* defined(__x86) */ 78 79 80 extern int at_flags; 81 extern volatile size_t aslr_max_brk_skew; 82 83 #define ORIGIN_STR "ORIGIN" 84 #define ORIGIN_STR_SIZE 6 85 86 static int getelfhead(vnode_t *, cred_t *, Ehdr *, int *, int *, int *); 87 static int getelfphdr(vnode_t *, cred_t *, const Ehdr *, int, caddr_t *, 88 ssize_t *); 89 static int getelfshdr(vnode_t *, cred_t *, const Ehdr *, int, int, caddr_t *, 90 ssize_t *, caddr_t *, ssize_t *); 91 static size_t elfsize(Ehdr *, int, caddr_t, uintptr_t *); 92 static int mapelfexec(vnode_t *, Ehdr *, int, caddr_t, 93 Phdr **, Phdr **, Phdr **, Phdr **, Phdr *, 94 caddr_t *, caddr_t *, intptr_t *, intptr_t *, size_t, long *, size_t *); 95 96 static int 97 dtrace_safe_phdr(Phdr *phdrp, struct uarg *args, uintptr_t base) 98 { 99 ASSERT(phdrp->p_type == PT_SUNWDTRACE); 100 101 /* 102 * See the comment in fasttrap.h for information on how to safely 103 * update this program header. 104 */ 105 if (phdrp->p_memsz < PT_SUNWDTRACE_SIZE || 106 (phdrp->p_flags & (PF_R | PF_W | PF_X)) != (PF_R | PF_W | PF_X)) 107 return (-1); 108 109 args->thrptr = phdrp->p_vaddr + base; 110 111 return (0); 112 } 113 114 static int 115 handle_secflag_dt(proc_t *p, uint_t dt, uint_t val) 116 { 117 uint_t flag; 118 119 switch (dt) { 120 case DT_SUNW_ASLR: 121 flag = PROC_SEC_ASLR; 122 break; 123 default: 124 return (EINVAL); 125 } 126 127 if (val == 0) { 128 if (secflag_isset(p->p_secflags.psf_lower, flag)) 129 return (EPERM); 130 if ((secpolicy_psecflags(CRED(), p, p) != 0) && 131 secflag_isset(p->p_secflags.psf_inherit, flag)) 132 return (EPERM); 133 134 secflag_clear(&p->p_secflags.psf_effective, flag); 135 } else { 136 if (!secflag_isset(p->p_secflags.psf_upper, flag)) 137 return (EPERM); 138 139 if ((secpolicy_psecflags(CRED(), p, p) != 0) && 140 !secflag_isset(p->p_secflags.psf_inherit, flag)) 141 return (EPERM); 142 143 secflag_set(&p->p_secflags.psf_effective, flag); 144 } 145 146 return (0); 147 } 148 149 /* 150 * Map in the executable pointed to by vp. Returns 0 on success. 151 */ 152 int 153 mapexec_brand(vnode_t *vp, uarg_t *args, Ehdr *ehdr, Addr *uphdr_vaddr, 154 intptr_t *voffset, caddr_t exec_file, int *interp, caddr_t *bssbase, 155 caddr_t *brkbase, size_t *brksize, uintptr_t *lddatap) 156 { 157 size_t len; 158 struct vattr vat; 159 caddr_t phdrbase = NULL; 160 ssize_t phdrsize; 161 int nshdrs, shstrndx, nphdrs; 162 int error = 0; 163 Phdr *uphdr = NULL; 164 Phdr *junk = NULL; 165 Phdr *dynphdr = NULL; 166 Phdr *dtrphdr = NULL; 167 uintptr_t lddata; 168 long execsz; 169 intptr_t minaddr; 170 171 if (lddatap != NULL) 172 *lddatap = 0; 173 174 if (error = execpermissions(vp, &vat, args)) { 175 uprintf("%s: Cannot execute %s\n", exec_file, args->pathname); 176 return (error); 177 } 178 179 if ((error = getelfhead(vp, CRED(), ehdr, &nshdrs, &shstrndx, 180 &nphdrs)) != 0 || 181 (error = getelfphdr(vp, CRED(), ehdr, nphdrs, &phdrbase, 182 &phdrsize)) != 0) { 183 uprintf("%s: Cannot read %s\n", exec_file, args->pathname); 184 return (error); 185 } 186 187 if ((len = elfsize(ehdr, nphdrs, phdrbase, &lddata)) == 0) { 188 uprintf("%s: Nothing to load in %s", exec_file, args->pathname); 189 kmem_free(phdrbase, phdrsize); 190 return (ENOEXEC); 191 } 192 if (lddatap != NULL) 193 *lddatap = lddata; 194 195 if (error = mapelfexec(vp, ehdr, nphdrs, phdrbase, &uphdr, &dynphdr, 196 &junk, &dtrphdr, NULL, bssbase, brkbase, voffset, &minaddr, 197 len, &execsz, brksize)) { 198 uprintf("%s: Cannot map %s\n", exec_file, args->pathname); 199 kmem_free(phdrbase, phdrsize); 200 return (error); 201 } 202 203 /* 204 * Inform our caller if the executable needs an interpreter. 205 */ 206 *interp = (dynphdr == NULL) ? 0 : 1; 207 208 /* 209 * If this is a statically linked executable, voffset should indicate 210 * the address of the executable itself (it normally holds the address 211 * of the interpreter). 212 */ 213 if (ehdr->e_type == ET_EXEC && *interp == 0) 214 *voffset = minaddr; 215 216 if (uphdr != NULL) { 217 *uphdr_vaddr = uphdr->p_vaddr; 218 } else { 219 *uphdr_vaddr = (Addr)-1; 220 } 221 222 kmem_free(phdrbase, phdrsize); 223 return (error); 224 } 225 226 /*ARGSUSED*/ 227 int 228 elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap, 229 int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred, 230 int brand_action) 231 { 232 caddr_t phdrbase = NULL; 233 caddr_t bssbase = 0; 234 caddr_t brkbase = 0; 235 size_t brksize = 0; 236 ssize_t dlnsize; 237 aux_entry_t *aux; 238 int error; 239 ssize_t resid; 240 int fd = -1; 241 intptr_t voffset; 242 Phdr *intphdr = NULL; 243 Phdr *dynamicphdr = NULL; 244 Phdr *stphdr = NULL; 245 Phdr *uphdr = NULL; 246 Phdr *junk = NULL; 247 size_t len; 248 size_t i; 249 ssize_t phdrsize; 250 int postfixsize = 0; 251 int hsize; 252 Phdr *phdrp; 253 Phdr *dataphdrp = NULL; 254 Phdr *dtrphdr; 255 Phdr *capphdr = NULL; 256 Cap *cap = NULL; 257 ssize_t capsize; 258 int hasu = 0; 259 int hasauxv = 0; 260 int hasintp = 0; 261 int branded = 0; 262 263 struct proc *p = ttoproc(curthread); 264 struct user *up = PTOU(p); 265 struct bigwad { 266 Ehdr ehdr; 267 aux_entry_t elfargs[__KERN_NAUXV_IMPL]; 268 char dl_name[MAXPATHLEN]; 269 char pathbuf[MAXPATHLEN]; 270 struct vattr vattr; 271 struct execenv exenv; 272 } *bigwad; /* kmem_alloc this behemoth so we don't blow stack */ 273 Ehdr *ehdrp; 274 int nshdrs, shstrndx, nphdrs; 275 char *dlnp; 276 char *pathbufp; 277 rlim64_t limit; 278 rlim64_t roundlimit; 279 280 ASSERT(p->p_model == DATAMODEL_ILP32 || p->p_model == DATAMODEL_LP64); 281 282 bigwad = kmem_alloc(sizeof (struct bigwad), KM_SLEEP); 283 ehdrp = &bigwad->ehdr; 284 dlnp = bigwad->dl_name; 285 pathbufp = bigwad->pathbuf; 286 287 /* 288 * Obtain ELF and program header information. 289 */ 290 if ((error = getelfhead(vp, CRED(), ehdrp, &nshdrs, &shstrndx, 291 &nphdrs)) != 0 || 292 (error = getelfphdr(vp, CRED(), ehdrp, nphdrs, &phdrbase, 293 &phdrsize)) != 0) 294 goto out; 295 296 /* 297 * Prevent executing an ELF file that has no entry point. 298 */ 299 if (ehdrp->e_entry == 0) { 300 uprintf("%s: Bad entry point\n", exec_file); 301 goto bad; 302 } 303 304 /* 305 * Put data model that we're exec-ing to into the args passed to 306 * exec_args(), so it will know what it is copying to on new stack. 307 * Now that we know whether we are exec-ing a 32-bit or 64-bit 308 * executable, we can set execsz with the appropriate NCARGS. 309 */ 310 #ifdef _LP64 311 if (ehdrp->e_ident[EI_CLASS] == ELFCLASS32) { 312 args->to_model = DATAMODEL_ILP32; 313 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS32-1); 314 } else { 315 args->to_model = DATAMODEL_LP64; 316 args->stk_prot &= ~PROT_EXEC; 317 #if defined(__x86) 318 args->dat_prot &= ~PROT_EXEC; 319 #endif 320 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS64-1); 321 } 322 #else /* _LP64 */ 323 args->to_model = DATAMODEL_ILP32; 324 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS-1); 325 #endif /* _LP64 */ 326 327 /* 328 * We delay invoking the brand callback until we've figured out 329 * what kind of elf binary we're trying to run, 32-bit or 64-bit. 330 * We do this because now the brand library can just check 331 * args->to_model to see if the target is 32-bit or 64-bit without 332 * having do duplicate all the code above. 333 * 334 * The level checks associated with brand handling below are used to 335 * prevent a loop since the brand elfexec function typically comes back 336 * through this function. We must check <= here since the nested 337 * handling in the #! interpreter code will increment the level before 338 * calling gexec to run the final elfexec interpreter. 339 */ 340 if ((level <= INTP_MAXDEPTH) && 341 (brand_action != EBA_NATIVE) && (PROC_IS_BRANDED(p))) { 342 error = BROP(p)->b_elfexec(vp, uap, args, 343 idatap, level + 1, execsz, setid, exec_file, cred, 344 brand_action); 345 goto out; 346 } 347 348 /* 349 * Determine aux size now so that stack can be built 350 * in one shot (except actual copyout of aux image), 351 * determine any non-default stack protections, 352 * and still have this code be machine independent. 353 */ 354 hsize = ehdrp->e_phentsize; 355 phdrp = (Phdr *)phdrbase; 356 for (i = nphdrs; i > 0; i--) { 357 switch (phdrp->p_type) { 358 case PT_INTERP: 359 hasauxv = hasintp = 1; 360 break; 361 case PT_PHDR: 362 hasu = 1; 363 break; 364 case PT_SUNWSTACK: 365 args->stk_prot = PROT_USER; 366 if (phdrp->p_flags & PF_R) 367 args->stk_prot |= PROT_READ; 368 if (phdrp->p_flags & PF_W) 369 args->stk_prot |= PROT_WRITE; 370 if (phdrp->p_flags & PF_X) 371 args->stk_prot |= PROT_EXEC; 372 break; 373 case PT_LOAD: 374 dataphdrp = phdrp; 375 break; 376 case PT_SUNWCAP: 377 capphdr = phdrp; 378 break; 379 case PT_DYNAMIC: 380 dynamicphdr = phdrp; 381 break; 382 } 383 phdrp = (Phdr *)((caddr_t)phdrp + hsize); 384 } 385 386 if (ehdrp->e_type != ET_EXEC) { 387 dataphdrp = NULL; 388 hasauxv = 1; 389 } 390 391 /* Copy BSS permissions to args->dat_prot */ 392 if (dataphdrp != NULL) { 393 args->dat_prot = PROT_USER; 394 if (dataphdrp->p_flags & PF_R) 395 args->dat_prot |= PROT_READ; 396 if (dataphdrp->p_flags & PF_W) 397 args->dat_prot |= PROT_WRITE; 398 if (dataphdrp->p_flags & PF_X) 399 args->dat_prot |= PROT_EXEC; 400 } 401 402 /* 403 * If a auxvector will be required - reserve the space for 404 * it now. This may be increased by exec_args if there are 405 * ISA-specific types (included in __KERN_NAUXV_IMPL). 406 */ 407 if (hasauxv) { 408 /* 409 * If a AUX vector is being built - the base AUX 410 * entries are: 411 * 412 * AT_BASE 413 * AT_FLAGS 414 * AT_PAGESZ 415 * AT_SUN_AUXFLAGS 416 * AT_SUN_HWCAP 417 * AT_SUN_HWCAP2 418 * AT_SUN_HWCAP3 419 * AT_SUN_PLATFORM (added in stk_copyout) 420 * AT_SUN_EXECNAME (added in stk_copyout) 421 * AT_NULL 422 * 423 * total == 10 424 */ 425 if (hasintp && hasu) { 426 /* 427 * Has PT_INTERP & PT_PHDR - the auxvectors that 428 * will be built are: 429 * 430 * AT_PHDR 431 * AT_PHENT 432 * AT_PHNUM 433 * AT_ENTRY 434 * AT_LDDATA 435 * 436 * total = 5 437 */ 438 args->auxsize = (10 + 5) * sizeof (aux_entry_t); 439 } else if (hasintp) { 440 /* 441 * Has PT_INTERP but no PT_PHDR 442 * 443 * AT_EXECFD 444 * AT_LDDATA 445 * 446 * total = 2 447 */ 448 args->auxsize = (10 + 2) * sizeof (aux_entry_t); 449 } else { 450 args->auxsize = 10 * sizeof (aux_entry_t); 451 } 452 } else { 453 args->auxsize = 0; 454 } 455 456 /* 457 * If this binary is using an emulator, we need to add an 458 * AT_SUN_EMULATOR aux entry. 459 */ 460 if (args->emulator != NULL) 461 args->auxsize += sizeof (aux_entry_t); 462 463 /* 464 * On supported kernels (x86_64) make room in the auxv for the 465 * AT_SUN_COMMPAGE entry. This will go unpopulated on i86xpv systems 466 * which do not provide such functionality. 467 * 468 * Additionally cover the floating point information AT_SUN_FPSIZE and 469 * AT_SUN_FPTYPE. 470 */ 471 #if defined(__amd64) 472 args->auxsize += 3 * sizeof (aux_entry_t); 473 #endif /* defined(__amd64) */ 474 475 if ((brand_action != EBA_NATIVE) && (PROC_IS_BRANDED(p))) { 476 branded = 1; 477 /* 478 * We will be adding 4 entries to the aux vectors. One for 479 * the the brandname and 3 for the brand specific aux vectors. 480 */ 481 args->auxsize += 4 * sizeof (aux_entry_t); 482 } 483 484 /* If the binary has an explicit ASLR flag, it must be honoured */ 485 if ((dynamicphdr != NULL) && (dynamicphdr->p_filesz > 0)) { 486 const size_t dynfilesz = dynamicphdr->p_filesz; 487 const size_t dynoffset = dynamicphdr->p_offset; 488 Dyn *dyn, *dp; 489 490 if (dynoffset > MAXOFFSET_T || 491 dynfilesz > MAXOFFSET_T || 492 dynoffset + dynfilesz > MAXOFFSET_T) { 493 uprintf("%s: cannot read full .dynamic section\n", 494 exec_file); 495 error = EINVAL; 496 goto out; 497 } 498 499 #define DYN_STRIDE 100 500 for (i = 0; i < dynfilesz; i += sizeof (*dyn) * DYN_STRIDE) { 501 const size_t remdyns = (dynfilesz - i) / sizeof (*dyn); 502 const size_t ndyns = MIN(DYN_STRIDE, remdyns); 503 const size_t dynsize = ndyns * sizeof (*dyn); 504 505 dyn = kmem_alloc(dynsize, KM_SLEEP); 506 507 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)dyn, 508 (ssize_t)dynsize, (offset_t)(dynoffset + i), 509 UIO_SYSSPACE, 0, (rlim64_t)0, 510 CRED(), &resid)) != 0) { 511 uprintf("%s: cannot read .dynamic section\n", 512 exec_file); 513 goto out; 514 } 515 516 for (dp = dyn; dp < (dyn + ndyns); dp++) { 517 if (dp->d_tag == DT_SUNW_ASLR) { 518 if ((error = handle_secflag_dt(p, 519 DT_SUNW_ASLR, 520 dp->d_un.d_val)) != 0) { 521 uprintf("%s: error setting " 522 "security-flag from " 523 "DT_SUNW_ASLR: %d\n", 524 exec_file, error); 525 goto out; 526 } 527 } 528 } 529 530 kmem_free(dyn, dynsize); 531 } 532 } 533 534 /* Hardware/Software capabilities */ 535 if (capphdr != NULL && 536 (capsize = capphdr->p_filesz) > 0 && 537 capsize <= 16 * sizeof (*cap)) { 538 int ncaps = capsize / sizeof (*cap); 539 Cap *cp; 540 541 cap = kmem_alloc(capsize, KM_SLEEP); 542 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)cap, 543 capsize, (offset_t)capphdr->p_offset, 544 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) != 0) { 545 uprintf("%s: Cannot read capabilities section\n", 546 exec_file); 547 goto out; 548 } 549 for (cp = cap; cp < cap + ncaps; cp++) { 550 if (cp->c_tag == CA_SUNW_SF_1 && 551 (cp->c_un.c_val & SF1_SUNW_ADDR32)) { 552 if (args->to_model == DATAMODEL_LP64) 553 args->addr32 = 1; 554 break; 555 } 556 } 557 } 558 559 aux = bigwad->elfargs; 560 /* 561 * Move args to the user's stack. 562 * This can fill in the AT_SUN_PLATFORM and AT_SUN_EXECNAME aux entries. 563 */ 564 if ((error = exec_args(uap, args, idatap, (void **)&aux)) != 0) { 565 if (error == -1) { 566 error = ENOEXEC; 567 goto bad; 568 } 569 goto out; 570 } 571 /* we're single threaded after this point */ 572 573 /* 574 * If this is an ET_DYN executable (shared object), 575 * determine its memory size so that mapelfexec() can load it. 576 */ 577 if (ehdrp->e_type == ET_DYN) 578 len = elfsize(ehdrp, nphdrs, phdrbase, NULL); 579 else 580 len = 0; 581 582 dtrphdr = NULL; 583 584 if ((error = mapelfexec(vp, ehdrp, nphdrs, phdrbase, &uphdr, &intphdr, 585 &stphdr, &dtrphdr, dataphdrp, &bssbase, &brkbase, &voffset, NULL, 586 len, execsz, &brksize)) != 0) 587 goto bad; 588 589 if (uphdr != NULL && intphdr == NULL) 590 goto bad; 591 592 if (dtrphdr != NULL && dtrace_safe_phdr(dtrphdr, args, voffset) != 0) { 593 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, exec_file); 594 goto bad; 595 } 596 597 if (intphdr != NULL) { 598 size_t len; 599 uintptr_t lddata; 600 char *p; 601 struct vnode *nvp; 602 603 dlnsize = intphdr->p_filesz; 604 605 if (dlnsize > MAXPATHLEN || dlnsize <= 0) 606 goto bad; 607 608 /* 609 * Read in "interpreter" pathname. 610 */ 611 if ((error = vn_rdwr(UIO_READ, vp, dlnp, intphdr->p_filesz, 612 (offset_t)intphdr->p_offset, UIO_SYSSPACE, 0, (rlim64_t)0, 613 CRED(), &resid)) != 0) { 614 uprintf("%s: Cannot obtain interpreter pathname\n", 615 exec_file); 616 goto bad; 617 } 618 619 if (resid != 0 || dlnp[dlnsize - 1] != '\0') 620 goto bad; 621 622 /* 623 * Search for '$ORIGIN' token in interpreter path. 624 * If found, expand it. 625 */ 626 for (p = dlnp; p = strchr(p, '$'); ) { 627 uint_t len, curlen; 628 char *_ptr; 629 630 if (strncmp(++p, ORIGIN_STR, ORIGIN_STR_SIZE)) 631 continue; 632 633 /* 634 * We don't support $ORIGIN on setid programs to close 635 * a potential attack vector. 636 */ 637 if ((setid & EXECSETID_SETID) != 0) { 638 error = ENOEXEC; 639 goto bad; 640 } 641 642 curlen = 0; 643 len = p - dlnp - 1; 644 if (len) { 645 bcopy(dlnp, pathbufp, len); 646 curlen += len; 647 } 648 if (_ptr = strrchr(args->pathname, '/')) { 649 len = _ptr - args->pathname; 650 if ((curlen + len) > MAXPATHLEN) 651 break; 652 653 bcopy(args->pathname, &pathbufp[curlen], len); 654 curlen += len; 655 } else { 656 /* 657 * executable is a basename found in the 658 * current directory. So - just substitue 659 * '.' for ORIGIN. 660 */ 661 pathbufp[curlen] = '.'; 662 curlen++; 663 } 664 p += ORIGIN_STR_SIZE; 665 len = strlen(p); 666 667 if ((curlen + len) > MAXPATHLEN) 668 break; 669 bcopy(p, &pathbufp[curlen], len); 670 curlen += len; 671 pathbufp[curlen++] = '\0'; 672 bcopy(pathbufp, dlnp, curlen); 673 } 674 675 /* 676 * /usr/lib/ld.so.1 is known to be a symlink to /lib/ld.so.1 677 * (and /usr/lib/64/ld.so.1 is a symlink to /lib/64/ld.so.1). 678 * Just in case /usr is not mounted, change it now. 679 */ 680 if (strcmp(dlnp, USR_LIB_RTLD) == 0) 681 dlnp += 4; 682 error = lookupname(dlnp, UIO_SYSSPACE, FOLLOW, NULLVPP, &nvp); 683 if (error && dlnp != bigwad->dl_name) { 684 /* new kernel, old user-level */ 685 error = lookupname(dlnp -= 4, UIO_SYSSPACE, FOLLOW, 686 NULLVPP, &nvp); 687 } 688 if (error) { 689 uprintf("%s: Cannot find %s\n", exec_file, dlnp); 690 goto bad; 691 } 692 693 /* 694 * Setup the "aux" vector. 695 */ 696 if (uphdr) { 697 if (ehdrp->e_type == ET_DYN) { 698 /* don't use the first page */ 699 bigwad->exenv.ex_brkbase = (caddr_t)PAGESIZE; 700 bigwad->exenv.ex_bssbase = (caddr_t)PAGESIZE; 701 } else { 702 bigwad->exenv.ex_bssbase = bssbase; 703 bigwad->exenv.ex_brkbase = brkbase; 704 } 705 bigwad->exenv.ex_brksize = brksize; 706 bigwad->exenv.ex_magic = elfmagic; 707 bigwad->exenv.ex_vp = vp; 708 setexecenv(&bigwad->exenv); 709 710 ADDAUX(aux, AT_PHDR, uphdr->p_vaddr + voffset) 711 ADDAUX(aux, AT_PHENT, ehdrp->e_phentsize) 712 ADDAUX(aux, AT_PHNUM, nphdrs) 713 ADDAUX(aux, AT_ENTRY, ehdrp->e_entry + voffset) 714 } else { 715 if ((error = execopen(&vp, &fd)) != 0) { 716 VN_RELE(nvp); 717 goto bad; 718 } 719 720 ADDAUX(aux, AT_EXECFD, fd) 721 } 722 723 if ((error = execpermissions(nvp, &bigwad->vattr, args)) != 0) { 724 VN_RELE(nvp); 725 uprintf("%s: Cannot execute %s\n", exec_file, dlnp); 726 goto bad; 727 } 728 729 /* 730 * Now obtain the ELF header along with the entire program 731 * header contained in "nvp". 732 */ 733 kmem_free(phdrbase, phdrsize); 734 phdrbase = NULL; 735 if ((error = getelfhead(nvp, CRED(), ehdrp, &nshdrs, 736 &shstrndx, &nphdrs)) != 0 || 737 (error = getelfphdr(nvp, CRED(), ehdrp, nphdrs, &phdrbase, 738 &phdrsize)) != 0) { 739 VN_RELE(nvp); 740 uprintf("%s: Cannot read %s\n", exec_file, dlnp); 741 goto bad; 742 } 743 744 /* 745 * Determine memory size of the "interpreter's" loadable 746 * sections. This size is then used to obtain the virtual 747 * address of a hole, in the user's address space, large 748 * enough to map the "interpreter". 749 */ 750 if ((len = elfsize(ehdrp, nphdrs, phdrbase, &lddata)) == 0) { 751 VN_RELE(nvp); 752 uprintf("%s: Nothing to load in %s\n", exec_file, dlnp); 753 goto bad; 754 } 755 756 dtrphdr = NULL; 757 758 error = mapelfexec(nvp, ehdrp, nphdrs, phdrbase, &junk, &junk, 759 &junk, &dtrphdr, NULL, NULL, NULL, &voffset, NULL, len, 760 execsz, NULL); 761 if (error || junk != NULL) { 762 VN_RELE(nvp); 763 uprintf("%s: Cannot map %s\n", exec_file, dlnp); 764 goto bad; 765 } 766 767 /* 768 * We use the DTrace program header to initialize the 769 * architecture-specific user per-LWP location. The dtrace 770 * fasttrap provider requires ready access to per-LWP scratch 771 * space. We assume that there is only one such program header 772 * in the interpreter. 773 */ 774 if (dtrphdr != NULL && 775 dtrace_safe_phdr(dtrphdr, args, voffset) != 0) { 776 VN_RELE(nvp); 777 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, dlnp); 778 goto bad; 779 } 780 781 VN_RELE(nvp); 782 ADDAUX(aux, AT_SUN_LDDATA, voffset + lddata) 783 } 784 785 if (hasauxv) { 786 int auxf = AF_SUN_HWCAPVERIFY; 787 #if defined(__amd64) 788 size_t fpsize; 789 int fptype; 790 #endif /* defined(__amd64) */ 791 792 /* 793 * Note: AT_SUN_PLATFORM and AT_SUN_EXECNAME were filled in via 794 * exec_args() 795 */ 796 ADDAUX(aux, AT_BASE, voffset) 797 ADDAUX(aux, AT_FLAGS, at_flags) 798 ADDAUX(aux, AT_PAGESZ, PAGESIZE) 799 /* 800 * Linker flags. (security) 801 * p_flag not yet set at this time. 802 * We rely on gexec() to provide us with the information. 803 * If the application is set-uid but this is not reflected 804 * in a mismatch between real/effective uids/gids, then 805 * don't treat this as a set-uid exec. So we care about 806 * the EXECSETID_UGIDS flag but not the ...SETID flag. 807 */ 808 if ((setid &= ~EXECSETID_SETID) != 0) 809 auxf |= AF_SUN_SETUGID; 810 811 /* 812 * If we're running a native process from within a branded 813 * zone under pfexec then we clear the AF_SUN_SETUGID flag so 814 * that the native ld.so.1 is able to link with the native 815 * libraries instead of using the brand libraries that are 816 * installed in the zone. We only do this for processes 817 * which we trust because we see they are already running 818 * under pfexec (where uid != euid). This prevents a 819 * malicious user within the zone from crafting a wrapper to 820 * run native suid commands with unsecure libraries interposed. 821 */ 822 if ((brand_action == EBA_NATIVE) && (PROC_IS_BRANDED(p) && 823 (setid &= ~EXECSETID_SETID) != 0)) 824 auxf &= ~AF_SUN_SETUGID; 825 826 /* 827 * Record the user addr of the auxflags aux vector entry 828 * since brands may optionally want to manipulate this field. 829 */ 830 args->auxp_auxflags = 831 (char *)((char *)args->stackend + 832 ((char *)&aux->a_type - 833 (char *)bigwad->elfargs)); 834 ADDAUX(aux, AT_SUN_AUXFLAGS, auxf); 835 836 /* 837 * Hardware capability flag word (performance hints) 838 * Used for choosing faster library routines. 839 * (Potentially different between 32-bit and 64-bit ABIs) 840 */ 841 if (args->to_model == DATAMODEL_NATIVE) { 842 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap) 843 ADDAUX(aux, AT_SUN_HWCAP2, auxv_hwcap_2) 844 ADDAUX(aux, AT_SUN_HWCAP3, auxv_hwcap_3) 845 } else { 846 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap32) 847 ADDAUX(aux, AT_SUN_HWCAP2, auxv_hwcap32_2) 848 ADDAUX(aux, AT_SUN_HWCAP3, auxv_hwcap32_3) 849 } 850 851 if (branded) { 852 /* 853 * Reserve space for the brand-private aux vectors, 854 * and record the user addr of that space. 855 */ 856 args->auxp_brand = 857 (char *)((char *)args->stackend + 858 ((char *)&aux->a_type - 859 (char *)bigwad->elfargs)); 860 ADDAUX(aux, AT_SUN_BRAND_AUX1, 0) 861 ADDAUX(aux, AT_SUN_BRAND_AUX2, 0) 862 ADDAUX(aux, AT_SUN_BRAND_AUX3, 0) 863 } 864 865 /* 866 * Add the comm page auxv entry, mapping it in if needed. Also 867 * take care of the FPU entries. 868 */ 869 #if defined(__amd64) 870 if (args->commpage != (uintptr_t)NULL || 871 (args->commpage = (uintptr_t)comm_page_mapin()) != 872 (uintptr_t)NULL) { 873 ADDAUX(aux, AT_SUN_COMMPAGE, args->commpage) 874 } else { 875 /* 876 * If the comm page cannot be mapped, pad out the auxv 877 * to satisfy later size checks. 878 */ 879 ADDAUX(aux, AT_NULL, 0) 880 } 881 882 fptype = AT_386_FPINFO_NONE; 883 fpu_auxv_info(&fptype, &fpsize); 884 if (fptype != AT_386_FPINFO_NONE) { 885 ADDAUX(aux, AT_SUN_FPTYPE, fptype) 886 ADDAUX(aux, AT_SUN_FPSIZE, fpsize) 887 } else { 888 ADDAUX(aux, AT_NULL, 0) 889 ADDAUX(aux, AT_NULL, 0) 890 } 891 #endif /* defined(__amd64) */ 892 893 ADDAUX(aux, AT_NULL, 0) 894 postfixsize = (char *)aux - (char *)bigwad->elfargs; 895 896 /* 897 * We make assumptions above when we determine how many aux 898 * vector entries we will be adding. However, if we have an 899 * invalid elf file, it is possible that mapelfexec might 900 * behave differently (but not return an error), in which case 901 * the number of aux entries we actually add will be different. 902 * We detect that now and error out. 903 */ 904 if (postfixsize != args->auxsize) { 905 DTRACE_PROBE2(elfexec_badaux, int, postfixsize, 906 int, args->auxsize); 907 goto bad; 908 } 909 ASSERT(postfixsize <= __KERN_NAUXV_IMPL * sizeof (aux_entry_t)); 910 } 911 912 /* 913 * For the 64-bit kernel, the limit is big enough that rounding it up 914 * to a page can overflow the 64-bit limit, so we check for btopr() 915 * overflowing here by comparing it with the unrounded limit in pages. 916 * If it hasn't overflowed, compare the exec size with the rounded up 917 * limit in pages. Otherwise, just compare with the unrounded limit. 918 */ 919 limit = btop(p->p_vmem_ctl); 920 roundlimit = btopr(p->p_vmem_ctl); 921 if ((roundlimit > limit && *execsz > roundlimit) || 922 (roundlimit < limit && *execsz > limit)) { 923 mutex_enter(&p->p_lock); 924 (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p, 925 RCA_SAFE); 926 mutex_exit(&p->p_lock); 927 error = ENOMEM; 928 goto bad; 929 } 930 931 bzero(up->u_auxv, sizeof (up->u_auxv)); 932 up->u_commpagep = args->commpage; 933 if (postfixsize) { 934 int num_auxv; 935 936 /* 937 * Copy the aux vector to the user stack. 938 */ 939 error = execpoststack(args, bigwad->elfargs, postfixsize); 940 if (error) 941 goto bad; 942 943 /* 944 * Copy auxv to the process's user structure for use by /proc. 945 * If this is a branded process, the brand's exec routine will 946 * copy it's private entries to the user structure later. It 947 * relies on the fact that the blank entries are at the end. 948 */ 949 num_auxv = postfixsize / sizeof (aux_entry_t); 950 ASSERT(num_auxv <= sizeof (up->u_auxv) / sizeof (auxv_t)); 951 aux = bigwad->elfargs; 952 for (i = 0; i < num_auxv; i++) { 953 up->u_auxv[i].a_type = aux[i].a_type; 954 up->u_auxv[i].a_un.a_val = (aux_val_t)aux[i].a_un.a_val; 955 } 956 } 957 958 /* 959 * Pass back the starting address so we can set the program counter. 960 */ 961 args->entry = (uintptr_t)(ehdrp->e_entry + voffset); 962 963 if (!uphdr) { 964 if (ehdrp->e_type == ET_DYN) { 965 /* 966 * If we are executing a shared library which doesn't 967 * have a interpreter (probably ld.so.1) then 968 * we don't set the brkbase now. Instead we 969 * delay it's setting until the first call 970 * via grow.c::brk(). This permits ld.so.1 to 971 * initialize brkbase to the tail of the executable it 972 * loads (which is where it needs to be). 973 */ 974 bigwad->exenv.ex_brkbase = (caddr_t)0; 975 bigwad->exenv.ex_bssbase = (caddr_t)0; 976 bigwad->exenv.ex_brksize = 0; 977 } else { 978 bigwad->exenv.ex_brkbase = brkbase; 979 bigwad->exenv.ex_bssbase = bssbase; 980 bigwad->exenv.ex_brksize = brksize; 981 } 982 bigwad->exenv.ex_magic = elfmagic; 983 bigwad->exenv.ex_vp = vp; 984 setexecenv(&bigwad->exenv); 985 } 986 987 ASSERT(error == 0); 988 goto out; 989 990 bad: 991 if (fd != -1) /* did we open the a.out yet */ 992 (void) execclose(fd); 993 994 psignal(p, SIGKILL); 995 996 if (error == 0) 997 error = ENOEXEC; 998 out: 999 if (phdrbase != NULL) 1000 kmem_free(phdrbase, phdrsize); 1001 if (cap != NULL) 1002 kmem_free(cap, capsize); 1003 kmem_free(bigwad, sizeof (struct bigwad)); 1004 return (error); 1005 } 1006 1007 /* 1008 * Compute the memory size requirement for the ELF file. 1009 */ 1010 static size_t 1011 elfsize(Ehdr *ehdrp, int nphdrs, caddr_t phdrbase, uintptr_t *lddata) 1012 { 1013 size_t len; 1014 Phdr *phdrp = (Phdr *)phdrbase; 1015 int hsize = ehdrp->e_phentsize; 1016 int first = 1; 1017 int dfirst = 1; /* first data segment */ 1018 uintptr_t loaddr = 0; 1019 uintptr_t hiaddr = 0; 1020 uintptr_t lo, hi; 1021 int i; 1022 1023 for (i = nphdrs; i > 0; i--) { 1024 if (phdrp->p_type == PT_LOAD) { 1025 lo = phdrp->p_vaddr; 1026 hi = lo + phdrp->p_memsz; 1027 if (first) { 1028 loaddr = lo; 1029 hiaddr = hi; 1030 first = 0; 1031 } else { 1032 if (loaddr > lo) 1033 loaddr = lo; 1034 if (hiaddr < hi) 1035 hiaddr = hi; 1036 } 1037 1038 /* 1039 * save the address of the first data segment 1040 * of a object - used for the AT_SUNW_LDDATA 1041 * aux entry. 1042 */ 1043 if ((lddata != NULL) && dfirst && 1044 (phdrp->p_flags & PF_W)) { 1045 *lddata = lo; 1046 dfirst = 0; 1047 } 1048 } 1049 phdrp = (Phdr *)((caddr_t)phdrp + hsize); 1050 } 1051 1052 len = hiaddr - (loaddr & PAGEMASK); 1053 len = roundup(len, PAGESIZE); 1054 1055 return (len); 1056 } 1057 1058 /* 1059 * Read in the ELF header and program header table. 1060 * SUSV3 requires: 1061 * ENOEXEC File format is not recognized 1062 * EINVAL Format recognized but execution not supported 1063 */ 1064 static int 1065 getelfhead(vnode_t *vp, cred_t *credp, Ehdr *ehdr, int *nshdrs, int *shstrndx, 1066 int *nphdrs) 1067 { 1068 int error; 1069 ssize_t resid; 1070 1071 /* 1072 * We got here by the first two bytes in ident, 1073 * now read the entire ELF header. 1074 */ 1075 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)ehdr, 1076 sizeof (Ehdr), (offset_t)0, UIO_SYSSPACE, 0, 1077 (rlim64_t)0, credp, &resid)) != 0) 1078 return (error); 1079 1080 /* 1081 * Since a separate version is compiled for handling 32-bit and 1082 * 64-bit ELF executables on a 64-bit kernel, the 64-bit version 1083 * doesn't need to be able to deal with 32-bit ELF files. 1084 */ 1085 if (resid != 0 || 1086 ehdr->e_ident[EI_MAG2] != ELFMAG2 || 1087 ehdr->e_ident[EI_MAG3] != ELFMAG3) 1088 return (ENOEXEC); 1089 1090 if ((ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) || 1091 #if defined(_ILP32) || defined(_ELF32_COMPAT) 1092 ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 1093 #else 1094 ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 1095 #endif 1096 !elfheadcheck(ehdr->e_ident[EI_DATA], ehdr->e_machine, 1097 ehdr->e_flags)) 1098 return (EINVAL); 1099 1100 *nshdrs = ehdr->e_shnum; 1101 *shstrndx = ehdr->e_shstrndx; 1102 *nphdrs = ehdr->e_phnum; 1103 1104 /* 1105 * If e_shnum, e_shstrndx, or e_phnum is its sentinel value, we need 1106 * to read in the section header at index zero to acces the true 1107 * values for those fields. 1108 */ 1109 if ((*nshdrs == 0 && ehdr->e_shoff != 0) || 1110 *shstrndx == SHN_XINDEX || *nphdrs == PN_XNUM) { 1111 Shdr shdr; 1112 1113 if (ehdr->e_shoff == 0) 1114 return (EINVAL); 1115 1116 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&shdr, 1117 sizeof (shdr), (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, 1118 (rlim64_t)0, credp, &resid)) != 0) 1119 return (error); 1120 1121 if (*nshdrs == 0) 1122 *nshdrs = shdr.sh_size; 1123 if (*shstrndx == SHN_XINDEX) 1124 *shstrndx = shdr.sh_link; 1125 if (*nphdrs == PN_XNUM && shdr.sh_info != 0) 1126 *nphdrs = shdr.sh_info; 1127 } 1128 1129 return (0); 1130 } 1131 1132 #ifdef _ELF32_COMPAT 1133 extern size_t elf_nphdr_max; 1134 #else 1135 size_t elf_nphdr_max = 1000; 1136 #endif 1137 1138 static int 1139 getelfphdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, int nphdrs, 1140 caddr_t *phbasep, ssize_t *phsizep) 1141 { 1142 ssize_t resid, minsize; 1143 int err; 1144 1145 /* 1146 * Since we're going to be using e_phentsize to iterate down the 1147 * array of program headers, it must be 8-byte aligned or else 1148 * a we might cause a misaligned access. We use all members through 1149 * p_flags on 32-bit ELF files and p_memsz on 64-bit ELF files so 1150 * e_phentsize must be at least large enough to include those 1151 * members. 1152 */ 1153 #if !defined(_LP64) || defined(_ELF32_COMPAT) 1154 minsize = offsetof(Phdr, p_flags) + sizeof (((Phdr *)NULL)->p_flags); 1155 #else 1156 minsize = offsetof(Phdr, p_memsz) + sizeof (((Phdr *)NULL)->p_memsz); 1157 #endif 1158 if (ehdr->e_phentsize < minsize || (ehdr->e_phentsize & 3)) 1159 return (EINVAL); 1160 1161 *phsizep = nphdrs * ehdr->e_phentsize; 1162 1163 if (*phsizep > sizeof (Phdr) * elf_nphdr_max) { 1164 if ((*phbasep = kmem_alloc(*phsizep, KM_NOSLEEP)) == NULL) 1165 return (ENOMEM); 1166 } else { 1167 *phbasep = kmem_alloc(*phsizep, KM_SLEEP); 1168 } 1169 1170 if ((err = vn_rdwr(UIO_READ, vp, *phbasep, *phsizep, 1171 (offset_t)ehdr->e_phoff, UIO_SYSSPACE, 0, (rlim64_t)0, 1172 credp, &resid)) != 0) { 1173 kmem_free(*phbasep, *phsizep); 1174 *phbasep = NULL; 1175 return (err); 1176 } 1177 1178 return (0); 1179 } 1180 1181 #ifdef _ELF32_COMPAT 1182 extern size_t elf_nshdr_max; 1183 extern size_t elf_shstrtab_max; 1184 #else 1185 size_t elf_nshdr_max = 10000; 1186 size_t elf_shstrtab_max = 100 * 1024; 1187 #endif 1188 1189 1190 static int 1191 getelfshdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, 1192 int nshdrs, int shstrndx, caddr_t *shbasep, ssize_t *shsizep, 1193 char **shstrbasep, ssize_t *shstrsizep) 1194 { 1195 ssize_t resid, minsize; 1196 int err; 1197 Shdr *shdr; 1198 1199 /* 1200 * Since we're going to be using e_shentsize to iterate down the 1201 * array of section headers, it must be 8-byte aligned or else 1202 * a we might cause a misaligned access. We use all members through 1203 * sh_entsize (on both 32- and 64-bit ELF files) so e_shentsize 1204 * must be at least large enough to include that member. The index 1205 * of the string table section must also be valid. 1206 */ 1207 minsize = offsetof(Shdr, sh_entsize) + sizeof (shdr->sh_entsize); 1208 if (ehdr->e_shentsize < minsize || (ehdr->e_shentsize & 3) || 1209 shstrndx >= nshdrs) 1210 return (EINVAL); 1211 1212 *shsizep = nshdrs * ehdr->e_shentsize; 1213 1214 if (*shsizep > sizeof (Shdr) * elf_nshdr_max) { 1215 if ((*shbasep = kmem_alloc(*shsizep, KM_NOSLEEP)) == NULL) 1216 return (ENOMEM); 1217 } else { 1218 *shbasep = kmem_alloc(*shsizep, KM_SLEEP); 1219 } 1220 1221 if ((err = vn_rdwr(UIO_READ, vp, *shbasep, *shsizep, 1222 (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, (rlim64_t)0, 1223 credp, &resid)) != 0) { 1224 kmem_free(*shbasep, *shsizep); 1225 return (err); 1226 } 1227 1228 /* 1229 * Pull the section string table out of the vnode; fail if the size 1230 * is zero. 1231 */ 1232 shdr = (Shdr *)(*shbasep + shstrndx * ehdr->e_shentsize); 1233 if ((*shstrsizep = shdr->sh_size) == 0) { 1234 kmem_free(*shbasep, *shsizep); 1235 return (EINVAL); 1236 } 1237 1238 if (*shstrsizep > elf_shstrtab_max) { 1239 if ((*shstrbasep = kmem_alloc(*shstrsizep, 1240 KM_NOSLEEP)) == NULL) { 1241 kmem_free(*shbasep, *shsizep); 1242 return (ENOMEM); 1243 } 1244 } else { 1245 *shstrbasep = kmem_alloc(*shstrsizep, KM_SLEEP); 1246 } 1247 1248 if ((err = vn_rdwr(UIO_READ, vp, *shstrbasep, *shstrsizep, 1249 (offset_t)shdr->sh_offset, UIO_SYSSPACE, 0, (rlim64_t)0, 1250 credp, &resid)) != 0) { 1251 kmem_free(*shbasep, *shsizep); 1252 kmem_free(*shstrbasep, *shstrsizep); 1253 return (err); 1254 } 1255 1256 /* 1257 * Make sure the strtab is null-terminated to make sure we 1258 * don't run off the end of the table. 1259 */ 1260 (*shstrbasep)[*shstrsizep - 1] = '\0'; 1261 1262 return (0); 1263 } 1264 1265 static int 1266 mapelfexec( 1267 vnode_t *vp, 1268 Ehdr *ehdr, 1269 int nphdrs, 1270 caddr_t phdrbase, 1271 Phdr **uphdr, 1272 Phdr **intphdr, 1273 Phdr **stphdr, 1274 Phdr **dtphdr, 1275 Phdr *dataphdrp, 1276 caddr_t *bssbase, 1277 caddr_t *brkbase, 1278 intptr_t *voffset, 1279 intptr_t *minaddr, 1280 size_t len, 1281 long *execsz, 1282 size_t *brksize) 1283 { 1284 Phdr *phdr; 1285 int i, prot, error; 1286 caddr_t addr = NULL; 1287 size_t zfodsz; 1288 int ptload = 0; 1289 int page; 1290 off_t offset; 1291 int hsize = ehdr->e_phentsize; 1292 caddr_t mintmp = (caddr_t)-1; 1293 extern int use_brk_lpg; 1294 1295 if (ehdr->e_type == ET_DYN) { 1296 secflagset_t flags = 0; 1297 /* 1298 * Obtain the virtual address of a hole in the 1299 * address space to map the "interpreter". 1300 */ 1301 if (secflag_enabled(curproc, PROC_SEC_ASLR)) 1302 flags |= _MAP_RANDOMIZE; 1303 1304 map_addr(&addr, len, (offset_t)0, 1, flags); 1305 if (addr == NULL) 1306 return (ENOMEM); 1307 *voffset = (intptr_t)addr; 1308 1309 /* 1310 * Calculate the minimum vaddr so it can be subtracted out. 1311 * According to the ELF specification, since PT_LOAD sections 1312 * must be sorted by increasing p_vaddr values, this is 1313 * guaranteed to be the first PT_LOAD section. 1314 */ 1315 phdr = (Phdr *)phdrbase; 1316 for (i = nphdrs; i > 0; i--) { 1317 if (phdr->p_type == PT_LOAD) { 1318 *voffset -= (uintptr_t)phdr->p_vaddr; 1319 break; 1320 } 1321 phdr = (Phdr *)((caddr_t)phdr + hsize); 1322 } 1323 1324 } else { 1325 *voffset = 0; 1326 } 1327 phdr = (Phdr *)phdrbase; 1328 for (i = nphdrs; i > 0; i--) { 1329 switch (phdr->p_type) { 1330 case PT_LOAD: 1331 if ((*intphdr != NULL) && (*uphdr == NULL)) 1332 return (0); 1333 1334 ptload = 1; 1335 prot = PROT_USER; 1336 if (phdr->p_flags & PF_R) 1337 prot |= PROT_READ; 1338 if (phdr->p_flags & PF_W) 1339 prot |= PROT_WRITE; 1340 if (phdr->p_flags & PF_X) 1341 prot |= PROT_EXEC; 1342 1343 addr = (caddr_t)((uintptr_t)phdr->p_vaddr + *voffset); 1344 1345 /* 1346 * Keep track of the segment with the lowest starting 1347 * address. 1348 */ 1349 if (addr < mintmp) 1350 mintmp = addr; 1351 1352 zfodsz = (size_t)phdr->p_memsz - phdr->p_filesz; 1353 1354 offset = phdr->p_offset; 1355 if (((uintptr_t)offset & PAGEOFFSET) == 1356 ((uintptr_t)addr & PAGEOFFSET) && 1357 (!(vp->v_flag & VNOMAP))) { 1358 page = 1; 1359 } else { 1360 page = 0; 1361 } 1362 1363 /* 1364 * Set the heap pagesize for OOB when the bss size 1365 * is known and use_brk_lpg is not 0. 1366 */ 1367 if (brksize != NULL && use_brk_lpg && 1368 zfodsz != 0 && phdr == dataphdrp && 1369 (prot & PROT_WRITE)) { 1370 size_t tlen = P2NPHASE((uintptr_t)addr + 1371 phdr->p_filesz, PAGESIZE); 1372 1373 if (zfodsz > tlen) { 1374 curproc->p_brkpageszc = 1375 page_szc(map_pgsz(MAPPGSZ_HEAP, 1376 curproc, addr + phdr->p_filesz + 1377 tlen, zfodsz - tlen, 0)); 1378 } 1379 } 1380 1381 if (curproc->p_brkpageszc != 0 && phdr == dataphdrp && 1382 (prot & PROT_WRITE)) { 1383 uint_t szc = curproc->p_brkpageszc; 1384 size_t pgsz = page_get_pagesize(szc); 1385 caddr_t ebss = addr + phdr->p_memsz; 1386 /* 1387 * If we need extra space to keep the BSS an 1388 * integral number of pages in size, some of 1389 * that space may fall beyond p_brkbase, so we 1390 * need to set p_brksize to account for it 1391 * being (logically) part of the brk. 1392 */ 1393 size_t extra_zfodsz; 1394 1395 ASSERT(pgsz > PAGESIZE); 1396 1397 extra_zfodsz = P2NPHASE((uintptr_t)ebss, pgsz); 1398 1399 if (error = execmap(vp, addr, phdr->p_filesz, 1400 zfodsz + extra_zfodsz, phdr->p_offset, 1401 prot, page, szc)) 1402 goto bad; 1403 if (brksize != NULL) 1404 *brksize = extra_zfodsz; 1405 } else { 1406 if (error = execmap(vp, addr, phdr->p_filesz, 1407 zfodsz, phdr->p_offset, prot, page, 0)) 1408 goto bad; 1409 } 1410 1411 if (bssbase != NULL && addr >= *bssbase && 1412 phdr == dataphdrp) { 1413 *bssbase = addr + phdr->p_filesz; 1414 } 1415 if (brkbase != NULL && addr >= *brkbase) { 1416 *brkbase = addr + phdr->p_memsz; 1417 } 1418 1419 *execsz += btopr(phdr->p_memsz); 1420 break; 1421 1422 case PT_INTERP: 1423 if (ptload) 1424 goto bad; 1425 *intphdr = phdr; 1426 break; 1427 1428 case PT_SHLIB: 1429 *stphdr = phdr; 1430 break; 1431 1432 case PT_PHDR: 1433 if (ptload) 1434 goto bad; 1435 *uphdr = phdr; 1436 break; 1437 1438 case PT_NULL: 1439 case PT_DYNAMIC: 1440 case PT_NOTE: 1441 break; 1442 1443 case PT_SUNWDTRACE: 1444 if (dtphdr != NULL) 1445 *dtphdr = phdr; 1446 break; 1447 1448 default: 1449 break; 1450 } 1451 phdr = (Phdr *)((caddr_t)phdr + hsize); 1452 } 1453 1454 if (minaddr != NULL) { 1455 ASSERT(mintmp != (caddr_t)-1); 1456 *minaddr = (intptr_t)mintmp; 1457 } 1458 1459 if (brkbase != NULL && secflag_enabled(curproc, PROC_SEC_ASLR)) { 1460 size_t off; 1461 uintptr_t base = (uintptr_t)*brkbase; 1462 uintptr_t oend = base + *brksize; 1463 1464 ASSERT(ISP2(aslr_max_brk_skew)); 1465 1466 (void) random_get_pseudo_bytes((uint8_t *)&off, sizeof (off)); 1467 base += P2PHASE(off, aslr_max_brk_skew); 1468 base = P2ROUNDUP(base, PAGESIZE); 1469 *brkbase = (caddr_t)base; 1470 /* 1471 * Above, we set *brksize to account for the possibility we 1472 * had to grow the 'brk' in padding out the BSS to a page 1473 * boundary. 1474 * 1475 * We now need to adjust that based on where we now are 1476 * actually putting the brk. 1477 */ 1478 if (oend > base) 1479 *brksize = oend - base; 1480 else 1481 *brksize = 0; 1482 } 1483 1484 return (0); 1485 bad: 1486 if (error == 0) 1487 error = EINVAL; 1488 return (error); 1489 } 1490 1491 int 1492 elfnote(vnode_t *vp, offset_t *offsetp, int type, int descsz, void *desc, 1493 rlim64_t rlimit, cred_t *credp) 1494 { 1495 Note note; 1496 int error; 1497 1498 bzero(¬e, sizeof (note)); 1499 bcopy("CORE", note.name, 4); 1500 note.nhdr.n_type = type; 1501 /* 1502 * The System V ABI states that n_namesz must be the length of the 1503 * string that follows the Nhdr structure including the terminating 1504 * null. The ABI also specifies that sufficient padding should be 1505 * included so that the description that follows the name string 1506 * begins on a 4- or 8-byte boundary for 32- and 64-bit binaries 1507 * respectively. However, since this change was not made correctly 1508 * at the time of the 64-bit port, both 32- and 64-bit binaries 1509 * descriptions are only guaranteed to begin on a 4-byte boundary. 1510 */ 1511 note.nhdr.n_namesz = 5; 1512 note.nhdr.n_descsz = roundup(descsz, sizeof (Word)); 1513 1514 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, ¬e, 1515 sizeof (note), rlimit, credp)) 1516 return (error); 1517 1518 *offsetp += sizeof (note); 1519 1520 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, desc, 1521 note.nhdr.n_descsz, rlimit, credp)) 1522 return (error); 1523 1524 *offsetp += note.nhdr.n_descsz; 1525 return (0); 1526 } 1527 1528 /* 1529 * Copy the section data from one vnode to the section of another vnode. 1530 */ 1531 static void 1532 copy_scn(Shdr *src, vnode_t *src_vp, Shdr *dst, vnode_t *dst_vp, Off *doffset, 1533 void *buf, size_t size, cred_t *credp, rlim64_t rlimit) 1534 { 1535 ssize_t resid; 1536 size_t len, n = src->sh_size; 1537 offset_t off = 0; 1538 1539 while (n != 0) { 1540 len = MIN(size, n); 1541 if (vn_rdwr(UIO_READ, src_vp, buf, len, src->sh_offset + off, 1542 UIO_SYSSPACE, 0, (rlim64_t)0, credp, &resid) != 0 || 1543 resid >= len || 1544 core_write(dst_vp, UIO_SYSSPACE, *doffset + off, 1545 buf, len - resid, rlimit, credp) != 0) { 1546 dst->sh_size = 0; 1547 dst->sh_offset = 0; 1548 return; 1549 } 1550 1551 ASSERT(n >= len - resid); 1552 1553 n -= len - resid; 1554 off += len - resid; 1555 } 1556 1557 *doffset += src->sh_size; 1558 } 1559 1560 #ifdef _ELF32_COMPAT 1561 extern size_t elf_datasz_max; 1562 extern size_t elf_zeropg_sz; 1563 #else 1564 size_t elf_datasz_max = 1 * 1024 * 1024; 1565 size_t elf_zeropg_sz = 4 * 1024; 1566 #endif 1567 1568 /* 1569 * This function processes mappings that correspond to load objects to 1570 * examine their respective sections for elfcore(). It's called once with 1571 * v set to NULL to count the number of sections that we're going to need 1572 * and then again with v set to some allocated buffer that we fill in with 1573 * all the section data. 1574 */ 1575 static int 1576 process_scns(core_content_t content, proc_t *p, cred_t *credp, vnode_t *vp, 1577 Shdr *v, int nv, rlim64_t rlimit, Off *doffsetp, int *nshdrsp) 1578 { 1579 vnode_t *lastvp = NULL; 1580 struct seg *seg; 1581 int i, j; 1582 void *data = NULL; 1583 size_t datasz = 0; 1584 shstrtab_t shstrtab; 1585 struct as *as = p->p_as; 1586 int error = 0; 1587 1588 if (!shstrtab_init(&shstrtab)) { 1589 error = ENOMEM; 1590 goto done; 1591 } 1592 1593 i = 1; 1594 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 1595 uint_t prot; 1596 vnode_t *mvp; 1597 void *tmp = NULL; 1598 caddr_t saddr = seg->s_base; 1599 caddr_t naddr; 1600 caddr_t eaddr; 1601 size_t segsize; 1602 1603 Ehdr ehdr; 1604 int nshdrs, shstrndx, nphdrs; 1605 caddr_t shbase; 1606 ssize_t shsize; 1607 char *shstrbase; 1608 ssize_t shstrsize; 1609 1610 Shdr *shdr; 1611 const char *name; 1612 size_t sz; 1613 uintptr_t off; 1614 1615 int ctf_ndx = 0; 1616 int symtab_ndx = 0; 1617 1618 /* 1619 * Since we're just looking for text segments of load 1620 * objects, we only care about the protection bits; we don't 1621 * care about the actual size of the segment so we use the 1622 * reserved size. If the segment's size is zero, there's 1623 * something fishy going on so we ignore this segment. 1624 */ 1625 if (seg->s_ops != &segvn_ops || 1626 SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 || 1627 mvp == lastvp || mvp == NULL || mvp->v_type != VREG || 1628 (segsize = pr_getsegsize(seg, 1)) == 0) 1629 continue; 1630 1631 eaddr = saddr + segsize; 1632 prot = pr_getprot(seg, 1, &tmp, &saddr, &naddr, eaddr); 1633 pr_getprot_done(&tmp); 1634 1635 /* 1636 * Skip this segment unless the protection bits look like 1637 * what we'd expect for a text segment. 1638 */ 1639 if ((prot & (PROT_WRITE | PROT_EXEC)) != PROT_EXEC) 1640 continue; 1641 1642 if (getelfhead(mvp, credp, &ehdr, &nshdrs, &shstrndx, 1643 &nphdrs) != 0 || 1644 getelfshdr(mvp, credp, &ehdr, nshdrs, shstrndx, 1645 &shbase, &shsize, &shstrbase, &shstrsize) != 0) 1646 continue; 1647 1648 off = ehdr.e_shentsize; 1649 for (j = 1; j < nshdrs; j++, off += ehdr.e_shentsize) { 1650 Shdr *symtab = NULL, *strtab; 1651 size_t allocsz; 1652 1653 shdr = (Shdr *)(shbase + off); 1654 allocsz = MIN(shdr->sh_size, elf_datasz_max); 1655 1656 if (shdr->sh_name >= shstrsize) 1657 continue; 1658 1659 name = shstrbase + shdr->sh_name; 1660 1661 if (strcmp(name, shstrtab_data[STR_CTF]) == 0) { 1662 if ((content & CC_CONTENT_CTF) == 0 || 1663 ctf_ndx != 0) 1664 continue; 1665 1666 if (shdr->sh_link > 0 && 1667 shdr->sh_link < nshdrs) { 1668 symtab = (Shdr *)(shbase + 1669 shdr->sh_link * ehdr.e_shentsize); 1670 } 1671 1672 if (v != NULL && i < nv - 1) { 1673 if (allocsz > datasz) { 1674 if (data != NULL) 1675 kmem_free(data, datasz); 1676 1677 datasz = allocsz; 1678 data = kmem_alloc(datasz, 1679 KM_SLEEP); 1680 } 1681 1682 if (!shstrtab_ndx(&shstrtab, 1683 shstrtab_data[STR_CTF], 1684 &v[i].sh_name)) { 1685 error = ENOMEM; 1686 goto done; 1687 } 1688 v[i].sh_addr = (Addr)(uintptr_t)saddr; 1689 v[i].sh_type = SHT_PROGBITS; 1690 v[i].sh_addralign = 4; 1691 *doffsetp = roundup(*doffsetp, 1692 v[i].sh_addralign); 1693 v[i].sh_offset = *doffsetp; 1694 v[i].sh_size = shdr->sh_size; 1695 if (symtab == NULL) { 1696 v[i].sh_link = 0; 1697 } else if (symtab->sh_type == 1698 SHT_SYMTAB && 1699 symtab_ndx != 0) { 1700 v[i].sh_link = 1701 symtab_ndx; 1702 } else { 1703 v[i].sh_link = i + 1; 1704 } 1705 1706 copy_scn(shdr, mvp, &v[i], vp, 1707 doffsetp, data, datasz, credp, 1708 rlimit); 1709 } 1710 1711 ctf_ndx = i++; 1712 1713 /* 1714 * We've already dumped the symtab. 1715 */ 1716 if (symtab != NULL && 1717 symtab->sh_type == SHT_SYMTAB && 1718 symtab_ndx != 0) 1719 continue; 1720 1721 } else if (strcmp(name, 1722 shstrtab_data[STR_SYMTAB]) == 0) { 1723 if ((content & CC_CONTENT_SYMTAB) == 0 || 1724 symtab != 0) 1725 continue; 1726 1727 symtab = shdr; 1728 } else if (strncmp(name, ".debug_", 1729 strlen(".debug_")) == 0) { 1730 /* 1731 * The design of the above check is intentional. 1732 * In particular, we want to capture any 1733 * sections that begin with '.debug_' for a few 1734 * reasons: 1735 * 1736 * 1) Various revisions to the DWARF spec end up 1737 * changing the set of section headers that 1738 * exist. This ensures that we don't need to 1739 * change the kernel to get a new version. 1740 * 1741 * 2) Other software uses .debug_ sections for 1742 * things which aren't DWARF. This allows them 1743 * to be captured as well. 1744 */ 1745 if ((content & CC_CONTENT_DEBUG) == 0) 1746 continue; 1747 1748 if (v != NULL && i < nv - 1) { 1749 if (allocsz > datasz) { 1750 if (data != NULL) 1751 kmem_free(data, datasz); 1752 1753 datasz = allocsz; 1754 data = kmem_alloc(datasz, 1755 KM_SLEEP); 1756 } 1757 1758 if (!shstrtab_ndx(&shstrtab, 1759 name, &v[i].sh_name)) { 1760 error = ENOMEM; 1761 goto done; 1762 } 1763 v[i].sh_addr = (Addr)(uintptr_t)saddr; 1764 v[i].sh_type = shdr->sh_type; 1765 v[i].sh_addralign = shdr->sh_addralign; 1766 *doffsetp = roundup(*doffsetp, 1767 v[i].sh_addralign); 1768 v[i].sh_offset = *doffsetp; 1769 v[i].sh_size = shdr->sh_size; 1770 v[i].sh_link = 0; 1771 v[i].sh_entsize = shdr->sh_entsize; 1772 v[i].sh_info = shdr->sh_info; 1773 1774 copy_scn(shdr, mvp, &v[i], vp, 1775 doffsetp, data, datasz, credp, 1776 rlimit); 1777 } 1778 1779 i++; 1780 continue; 1781 } 1782 1783 if (symtab != NULL) { 1784 if ((symtab->sh_type != SHT_DYNSYM && 1785 symtab->sh_type != SHT_SYMTAB) || 1786 symtab->sh_link == 0 || 1787 symtab->sh_link >= nshdrs) 1788 continue; 1789 1790 strtab = (Shdr *)(shbase + 1791 symtab->sh_link * ehdr.e_shentsize); 1792 1793 if (strtab->sh_type != SHT_STRTAB) 1794 continue; 1795 1796 if (v != NULL && i < nv - 2) { 1797 sz = MAX(symtab->sh_size, 1798 strtab->sh_size); 1799 allocsz = MIN(sz, elf_datasz_max); 1800 if (allocsz > datasz) { 1801 if (data != NULL) 1802 kmem_free(data, datasz); 1803 1804 datasz = allocsz; 1805 data = kmem_alloc(datasz, 1806 KM_SLEEP); 1807 } 1808 1809 if (symtab->sh_type == SHT_DYNSYM) { 1810 if (!shstrtab_ndx(&shstrtab, 1811 shstrtab_data[STR_DYNSYM], 1812 &v[i].sh_name)) { 1813 error = ENOMEM; 1814 goto done; 1815 } 1816 if (!shstrtab_ndx(&shstrtab, 1817 shstrtab_data[STR_DYNSTR], 1818 &v[i + 1].sh_name)) { 1819 error = ENOMEM; 1820 goto done; 1821 } 1822 } else { 1823 if (!shstrtab_ndx(&shstrtab, 1824 shstrtab_data[STR_SYMTAB], 1825 &v[i].sh_name)) { 1826 error = ENOMEM; 1827 goto done; 1828 } 1829 if (!shstrtab_ndx(&shstrtab, 1830 shstrtab_data[STR_STRTAB], 1831 &v[i + 1].sh_name)) { 1832 error = ENOMEM; 1833 goto done; 1834 } 1835 } 1836 1837 v[i].sh_type = symtab->sh_type; 1838 v[i].sh_addr = symtab->sh_addr; 1839 if (ehdr.e_type == ET_DYN || 1840 v[i].sh_addr == 0) 1841 v[i].sh_addr += 1842 (Addr)(uintptr_t)saddr; 1843 v[i].sh_addralign = 1844 symtab->sh_addralign; 1845 *doffsetp = roundup(*doffsetp, 1846 v[i].sh_addralign); 1847 v[i].sh_offset = *doffsetp; 1848 v[i].sh_size = symtab->sh_size; 1849 v[i].sh_link = i + 1; 1850 v[i].sh_entsize = symtab->sh_entsize; 1851 v[i].sh_info = symtab->sh_info; 1852 1853 copy_scn(symtab, mvp, &v[i], vp, 1854 doffsetp, data, datasz, credp, 1855 rlimit); 1856 1857 v[i + 1].sh_type = SHT_STRTAB; 1858 v[i + 1].sh_flags = SHF_STRINGS; 1859 v[i + 1].sh_addr = symtab->sh_addr; 1860 if (ehdr.e_type == ET_DYN || 1861 v[i + 1].sh_addr == 0) 1862 v[i + 1].sh_addr += 1863 (Addr)(uintptr_t)saddr; 1864 v[i + 1].sh_addralign = 1865 strtab->sh_addralign; 1866 *doffsetp = roundup(*doffsetp, 1867 v[i + 1].sh_addralign); 1868 v[i + 1].sh_offset = *doffsetp; 1869 v[i + 1].sh_size = strtab->sh_size; 1870 1871 copy_scn(strtab, mvp, &v[i + 1], vp, 1872 doffsetp, data, datasz, credp, 1873 rlimit); 1874 } 1875 1876 if (symtab->sh_type == SHT_SYMTAB) 1877 symtab_ndx = i; 1878 i += 2; 1879 } 1880 } 1881 1882 kmem_free(shstrbase, shstrsize); 1883 kmem_free(shbase, shsize); 1884 1885 lastvp = mvp; 1886 } 1887 1888 if (v == NULL) { 1889 if (i == 1) 1890 *nshdrsp = 0; 1891 else 1892 *nshdrsp = i + 1; 1893 goto done; 1894 } 1895 1896 if (i != nv - 1) { 1897 cmn_err(CE_WARN, "elfcore: core dump failed for " 1898 "process %d; address space is changing", p->p_pid); 1899 error = EIO; 1900 goto done; 1901 } 1902 1903 if (!shstrtab_ndx(&shstrtab, shstrtab_data[STR_SHSTRTAB], 1904 &v[i].sh_name)) { 1905 error = ENOMEM; 1906 goto done; 1907 } 1908 v[i].sh_size = shstrtab_size(&shstrtab); 1909 v[i].sh_addralign = 1; 1910 *doffsetp = roundup(*doffsetp, v[i].sh_addralign); 1911 v[i].sh_offset = *doffsetp; 1912 v[i].sh_flags = SHF_STRINGS; 1913 v[i].sh_type = SHT_STRTAB; 1914 1915 if (v[i].sh_size > datasz) { 1916 if (data != NULL) 1917 kmem_free(data, datasz); 1918 1919 datasz = v[i].sh_size; 1920 data = kmem_alloc(datasz, 1921 KM_SLEEP); 1922 } 1923 1924 shstrtab_dump(&shstrtab, data); 1925 1926 if ((error = core_write(vp, UIO_SYSSPACE, *doffsetp, 1927 data, v[i].sh_size, rlimit, credp)) != 0) 1928 goto done; 1929 1930 *doffsetp += v[i].sh_size; 1931 1932 done: 1933 if (data != NULL) 1934 kmem_free(data, datasz); 1935 1936 shstrtab_fini(&shstrtab); 1937 1938 return (error); 1939 } 1940 1941 int 1942 elfcore(vnode_t *vp, proc_t *p, cred_t *credp, rlim64_t rlimit, int sig, 1943 core_content_t content) 1944 { 1945 offset_t poffset, soffset; 1946 Off doffset; 1947 int error, i, nphdrs, nshdrs; 1948 int overflow = 0; 1949 struct seg *seg; 1950 struct as *as = p->p_as; 1951 union { 1952 Ehdr ehdr; 1953 Phdr phdr[1]; 1954 Shdr shdr[1]; 1955 } *bigwad; 1956 size_t bigsize; 1957 size_t phdrsz, shdrsz; 1958 Ehdr *ehdr; 1959 Phdr *v; 1960 void *zeropg = NULL; 1961 caddr_t brkbase; 1962 size_t brksize; 1963 caddr_t stkbase; 1964 size_t stksize; 1965 int ntries = 0; 1966 klwp_t *lwp = ttolwp(curthread); 1967 1968 top: 1969 /* 1970 * Make sure we have everything we need (registers, etc.). 1971 * All other lwps have already stopped and are in an orderly state. 1972 */ 1973 ASSERT(p == ttoproc(curthread)); 1974 prstop(0, 0); 1975 1976 AS_LOCK_ENTER(as, RW_WRITER); 1977 nphdrs = prnsegs(as, 0) + 2; /* two CORE note sections */ 1978 1979 /* 1980 * Count the number of section headers we're going to need. 1981 */ 1982 nshdrs = 0; 1983 if (content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB | CC_CONTENT_DEBUG)) { 1984 (void) process_scns(content, p, credp, NULL, NULL, 0, 0, 1985 NULL, &nshdrs); 1986 } 1987 AS_LOCK_EXIT(as); 1988 1989 ASSERT(nshdrs == 0 || nshdrs > 1); 1990 1991 /* 1992 * The core file contents may required zero section headers, but if 1993 * we overflow the 16 bits allotted to the program header count in 1994 * the ELF header, we'll need that program header at index zero. 1995 */ 1996 if (nshdrs == 0 && nphdrs >= PN_XNUM) 1997 nshdrs = 1; 1998 1999 phdrsz = nphdrs * sizeof (Phdr); 2000 shdrsz = nshdrs * sizeof (Shdr); 2001 2002 bigsize = MAX(sizeof (*bigwad), MAX(phdrsz, shdrsz)); 2003 bigwad = kmem_alloc(bigsize, KM_SLEEP); 2004 2005 ehdr = &bigwad->ehdr; 2006 bzero(ehdr, sizeof (*ehdr)); 2007 2008 ehdr->e_ident[EI_MAG0] = ELFMAG0; 2009 ehdr->e_ident[EI_MAG1] = ELFMAG1; 2010 ehdr->e_ident[EI_MAG2] = ELFMAG2; 2011 ehdr->e_ident[EI_MAG3] = ELFMAG3; 2012 ehdr->e_ident[EI_CLASS] = ELFCLASS; 2013 ehdr->e_type = ET_CORE; 2014 2015 #if !defined(_LP64) || defined(_ELF32_COMPAT) 2016 2017 #if defined(__sparc) 2018 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 2019 ehdr->e_machine = EM_SPARC; 2020 #elif defined(__i386_COMPAT) 2021 ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 2022 ehdr->e_machine = EM_386; 2023 #else 2024 #error "no recognized machine type is defined" 2025 #endif 2026 2027 #else /* !defined(_LP64) || defined(_ELF32_COMPAT) */ 2028 2029 #if defined(__sparc) 2030 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 2031 ehdr->e_machine = EM_SPARCV9; 2032 #elif defined(__amd64) 2033 ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 2034 ehdr->e_machine = EM_AMD64; 2035 #else 2036 #error "no recognized 64-bit machine type is defined" 2037 #endif 2038 2039 #endif /* !defined(_LP64) || defined(_ELF32_COMPAT) */ 2040 2041 /* 2042 * If the count of program headers or section headers or the index 2043 * of the section string table can't fit in the mere 16 bits 2044 * shortsightedly allotted to them in the ELF header, we use the 2045 * extended formats and put the real values in the section header 2046 * as index 0. 2047 */ 2048 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 2049 ehdr->e_version = EV_CURRENT; 2050 ehdr->e_ehsize = sizeof (Ehdr); 2051 2052 if (nphdrs >= PN_XNUM) 2053 ehdr->e_phnum = PN_XNUM; 2054 else 2055 ehdr->e_phnum = (unsigned short)nphdrs; 2056 2057 ehdr->e_phoff = sizeof (Ehdr); 2058 ehdr->e_phentsize = sizeof (Phdr); 2059 2060 if (nshdrs > 0) { 2061 if (nshdrs >= SHN_LORESERVE) 2062 ehdr->e_shnum = 0; 2063 else 2064 ehdr->e_shnum = (unsigned short)nshdrs; 2065 2066 if (nshdrs - 1 >= SHN_LORESERVE) 2067 ehdr->e_shstrndx = SHN_XINDEX; 2068 else 2069 ehdr->e_shstrndx = (unsigned short)(nshdrs - 1); 2070 2071 ehdr->e_shoff = ehdr->e_phoff + ehdr->e_phentsize * nphdrs; 2072 ehdr->e_shentsize = sizeof (Shdr); 2073 } 2074 2075 if (error = core_write(vp, UIO_SYSSPACE, (offset_t)0, ehdr, 2076 sizeof (Ehdr), rlimit, credp)) 2077 goto done; 2078 2079 poffset = sizeof (Ehdr); 2080 soffset = sizeof (Ehdr) + phdrsz; 2081 doffset = sizeof (Ehdr) + phdrsz + shdrsz; 2082 2083 v = &bigwad->phdr[0]; 2084 bzero(v, phdrsz); 2085 2086 setup_old_note_header(&v[0], p); 2087 v[0].p_offset = doffset = roundup(doffset, sizeof (Word)); 2088 doffset += v[0].p_filesz; 2089 2090 setup_note_header(&v[1], p); 2091 v[1].p_offset = doffset = roundup(doffset, sizeof (Word)); 2092 doffset += v[1].p_filesz; 2093 2094 mutex_enter(&p->p_lock); 2095 2096 brkbase = p->p_brkbase; 2097 brksize = p->p_brksize; 2098 2099 stkbase = p->p_usrstack - p->p_stksize; 2100 stksize = p->p_stksize; 2101 2102 mutex_exit(&p->p_lock); 2103 2104 AS_LOCK_ENTER(as, RW_WRITER); 2105 i = 2; 2106 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 2107 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 2108 caddr_t saddr, naddr; 2109 void *tmp = NULL; 2110 extern struct seg_ops segspt_shmops; 2111 2112 if ((seg->s_flags & S_HOLE) != 0) { 2113 continue; 2114 } 2115 2116 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 2117 uint_t prot; 2118 size_t size; 2119 int type; 2120 vnode_t *mvp; 2121 2122 prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 2123 prot &= PROT_READ | PROT_WRITE | PROT_EXEC; 2124 if ((size = (size_t)(naddr - saddr)) == 0) 2125 continue; 2126 if (i == nphdrs) { 2127 overflow++; 2128 continue; 2129 } 2130 v[i].p_type = PT_LOAD; 2131 v[i].p_vaddr = (Addr)(uintptr_t)saddr; 2132 v[i].p_memsz = size; 2133 if (prot & PROT_READ) 2134 v[i].p_flags |= PF_R; 2135 if (prot & PROT_WRITE) 2136 v[i].p_flags |= PF_W; 2137 if (prot & PROT_EXEC) 2138 v[i].p_flags |= PF_X; 2139 2140 /* 2141 * Figure out which mappings to include in the core. 2142 */ 2143 type = SEGOP_GETTYPE(seg, saddr); 2144 2145 if (saddr == stkbase && size == stksize) { 2146 if (!(content & CC_CONTENT_STACK)) 2147 goto exclude; 2148 2149 } else if (saddr == brkbase && size == brksize) { 2150 if (!(content & CC_CONTENT_HEAP)) 2151 goto exclude; 2152 2153 } else if (seg->s_ops == &segspt_shmops) { 2154 if (type & MAP_NORESERVE) { 2155 if (!(content & CC_CONTENT_DISM)) 2156 goto exclude; 2157 } else { 2158 if (!(content & CC_CONTENT_ISM)) 2159 goto exclude; 2160 } 2161 2162 } else if (seg->s_ops != &segvn_ops) { 2163 goto exclude; 2164 2165 } else if (type & MAP_SHARED) { 2166 if (shmgetid(p, saddr) != SHMID_NONE) { 2167 if (!(content & CC_CONTENT_SHM)) 2168 goto exclude; 2169 2170 } else if (SEGOP_GETVP(seg, seg->s_base, 2171 &mvp) != 0 || mvp == NULL || 2172 mvp->v_type != VREG) { 2173 if (!(content & CC_CONTENT_SHANON)) 2174 goto exclude; 2175 2176 } else { 2177 if (!(content & CC_CONTENT_SHFILE)) 2178 goto exclude; 2179 } 2180 2181 } else if (SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 || 2182 mvp == NULL || mvp->v_type != VREG) { 2183 if (!(content & CC_CONTENT_ANON)) 2184 goto exclude; 2185 2186 } else if (prot == (PROT_READ | PROT_EXEC)) { 2187 if (!(content & CC_CONTENT_TEXT)) 2188 goto exclude; 2189 2190 } else if (prot == PROT_READ) { 2191 if (!(content & CC_CONTENT_RODATA)) 2192 goto exclude; 2193 2194 } else { 2195 if (!(content & CC_CONTENT_DATA)) 2196 goto exclude; 2197 } 2198 2199 doffset = roundup(doffset, sizeof (Word)); 2200 v[i].p_offset = doffset; 2201 v[i].p_filesz = size; 2202 doffset += size; 2203 exclude: 2204 i++; 2205 } 2206 ASSERT(tmp == NULL); 2207 } 2208 AS_LOCK_EXIT(as); 2209 2210 if (overflow || i != nphdrs) { 2211 if (ntries++ == 0) { 2212 kmem_free(bigwad, bigsize); 2213 overflow = 0; 2214 goto top; 2215 } 2216 cmn_err(CE_WARN, "elfcore: core dump failed for " 2217 "process %d; address space is changing", p->p_pid); 2218 error = EIO; 2219 goto done; 2220 } 2221 2222 if ((error = core_write(vp, UIO_SYSSPACE, poffset, 2223 v, phdrsz, rlimit, credp)) != 0) 2224 goto done; 2225 2226 if ((error = write_old_elfnotes(p, sig, vp, v[0].p_offset, rlimit, 2227 credp)) != 0) 2228 goto done; 2229 2230 if ((error = write_elfnotes(p, sig, vp, v[1].p_offset, rlimit, 2231 credp, content)) != 0) 2232 goto done; 2233 2234 for (i = 2; i < nphdrs; i++) { 2235 prkillinfo_t killinfo; 2236 sigqueue_t *sq; 2237 int sig, j; 2238 2239 if (v[i].p_filesz == 0) 2240 continue; 2241 2242 /* 2243 * If we hit a region that was mapped PROT_NONE then we cannot 2244 * continue dumping this normally as the kernel would be unable 2245 * to read from the page and that would result in us failing to 2246 * dump the page. As such, any region mapped PROT_NONE, we dump 2247 * as a zero-filled page such that this is still represented in 2248 * the map. 2249 * 2250 * If dumping out this segment fails, rather than failing 2251 * the core dump entirely, we reset the size of the mapping 2252 * to zero to indicate that the data is absent from the core 2253 * file and or in the PF_SUNW_FAILURE flag to differentiate 2254 * this from mappings that were excluded due to the core file 2255 * content settings. 2256 */ 2257 if ((v[i].p_flags & (PF_R | PF_W | PF_X)) == 0) { 2258 size_t towrite = v[i].p_filesz; 2259 size_t curoff = 0; 2260 2261 if (zeropg == NULL) { 2262 zeropg = kmem_zalloc(elf_zeropg_sz, KM_SLEEP); 2263 } 2264 2265 error = 0; 2266 while (towrite != 0) { 2267 size_t len = MIN(towrite, elf_zeropg_sz); 2268 2269 error = core_write(vp, UIO_SYSSPACE, 2270 v[i].p_offset + curoff, zeropg, len, rlimit, 2271 credp); 2272 if (error != 0) 2273 break; 2274 2275 towrite -= len; 2276 curoff += len; 2277 } 2278 2279 if (error == 0) 2280 continue; 2281 } else { 2282 error = core_seg(p, vp, v[i].p_offset, 2283 (caddr_t)(uintptr_t)v[i].p_vaddr, v[i].p_filesz, 2284 rlimit, credp); 2285 if (error == 0) 2286 continue; 2287 } 2288 2289 if ((sig = lwp->lwp_cursig) == 0) { 2290 /* 2291 * We failed due to something other than a signal. 2292 * Since the space reserved for the segment is now 2293 * unused, we stash the errno in the first four 2294 * bytes. This undocumented interface will let us 2295 * understand the nature of the failure. 2296 */ 2297 (void) core_write(vp, UIO_SYSSPACE, v[i].p_offset, 2298 &error, sizeof (error), rlimit, credp); 2299 2300 v[i].p_filesz = 0; 2301 v[i].p_flags |= PF_SUNW_FAILURE; 2302 if ((error = core_write(vp, UIO_SYSSPACE, 2303 poffset + sizeof (v[i]) * i, &v[i], sizeof (v[i]), 2304 rlimit, credp)) != 0) 2305 goto done; 2306 2307 continue; 2308 } 2309 2310 /* 2311 * We took a signal. We want to abort the dump entirely, but 2312 * we also want to indicate what failed and why. We therefore 2313 * use the space reserved for the first failing segment to 2314 * write our error (which, for purposes of compatability with 2315 * older core dump readers, we set to EINTR) followed by any 2316 * siginfo associated with the signal. 2317 */ 2318 bzero(&killinfo, sizeof (killinfo)); 2319 killinfo.prk_error = EINTR; 2320 2321 sq = sig == SIGKILL ? curproc->p_killsqp : lwp->lwp_curinfo; 2322 2323 if (sq != NULL) { 2324 bcopy(&sq->sq_info, &killinfo.prk_info, 2325 sizeof (sq->sq_info)); 2326 } else { 2327 killinfo.prk_info.si_signo = lwp->lwp_cursig; 2328 killinfo.prk_info.si_code = SI_NOINFO; 2329 } 2330 2331 #if (defined(_SYSCALL32_IMPL) || defined(_LP64)) 2332 /* 2333 * If this is a 32-bit process, we need to translate from the 2334 * native siginfo to the 32-bit variant. (Core readers must 2335 * always have the same data model as their target or must 2336 * be aware of -- and compensate for -- data model differences.) 2337 */ 2338 if (curproc->p_model == DATAMODEL_ILP32) { 2339 siginfo32_t si32; 2340 2341 siginfo_kto32((k_siginfo_t *)&killinfo.prk_info, &si32); 2342 bcopy(&si32, &killinfo.prk_info, sizeof (si32)); 2343 } 2344 #endif 2345 2346 (void) core_write(vp, UIO_SYSSPACE, v[i].p_offset, 2347 &killinfo, sizeof (killinfo), rlimit, credp); 2348 2349 /* 2350 * For the segment on which we took the signal, indicate that 2351 * its data now refers to a siginfo. 2352 */ 2353 v[i].p_filesz = 0; 2354 v[i].p_flags |= PF_SUNW_FAILURE | PF_SUNW_KILLED | 2355 PF_SUNW_SIGINFO; 2356 2357 /* 2358 * And for every other segment, indicate that its absence 2359 * is due to a signal. 2360 */ 2361 for (j = i + 1; j < nphdrs; j++) { 2362 v[j].p_filesz = 0; 2363 v[j].p_flags |= PF_SUNW_FAILURE | PF_SUNW_KILLED; 2364 } 2365 2366 /* 2367 * Finally, write out our modified program headers. 2368 */ 2369 if ((error = core_write(vp, UIO_SYSSPACE, 2370 poffset + sizeof (v[i]) * i, &v[i], 2371 sizeof (v[i]) * (nphdrs - i), rlimit, credp)) != 0) 2372 goto done; 2373 2374 break; 2375 } 2376 2377 if (nshdrs > 0) { 2378 bzero(&bigwad->shdr[0], shdrsz); 2379 2380 if (nshdrs >= SHN_LORESERVE) 2381 bigwad->shdr[0].sh_size = nshdrs; 2382 2383 if (nshdrs - 1 >= SHN_LORESERVE) 2384 bigwad->shdr[0].sh_link = nshdrs - 1; 2385 2386 if (nphdrs >= PN_XNUM) 2387 bigwad->shdr[0].sh_info = nphdrs; 2388 2389 if (nshdrs > 1) { 2390 AS_LOCK_ENTER(as, RW_WRITER); 2391 if ((error = process_scns(content, p, credp, vp, 2392 &bigwad->shdr[0], nshdrs, rlimit, &doffset, 2393 NULL)) != 0) { 2394 AS_LOCK_EXIT(as); 2395 goto done; 2396 } 2397 AS_LOCK_EXIT(as); 2398 } 2399 2400 if ((error = core_write(vp, UIO_SYSSPACE, soffset, 2401 &bigwad->shdr[0], shdrsz, rlimit, credp)) != 0) 2402 goto done; 2403 } 2404 2405 done: 2406 if (zeropg != NULL) { 2407 kmem_free(zeropg, elf_zeropg_sz); 2408 } 2409 kmem_free(bigwad, bigsize); 2410 return (error); 2411 } 2412 2413 #ifndef _ELF32_COMPAT 2414 2415 static struct execsw esw = { 2416 #ifdef _LP64 2417 elf64magicstr, 2418 #else /* _LP64 */ 2419 elf32magicstr, 2420 #endif /* _LP64 */ 2421 0, 2422 5, 2423 elfexec, 2424 elfcore 2425 }; 2426 2427 static struct modlexec modlexec = { 2428 &mod_execops, "exec module for elf", &esw 2429 }; 2430 2431 #ifdef _LP64 2432 extern int elf32exec(vnode_t *vp, execa_t *uap, uarg_t *args, 2433 intpdata_t *idatap, int level, long *execsz, 2434 int setid, caddr_t exec_file, cred_t *cred, 2435 int brand_action); 2436 extern int elf32core(vnode_t *vp, proc_t *p, cred_t *credp, 2437 rlim64_t rlimit, int sig, core_content_t content); 2438 2439 static struct execsw esw32 = { 2440 elf32magicstr, 2441 0, 2442 5, 2443 elf32exec, 2444 elf32core 2445 }; 2446 2447 static struct modlexec modlexec32 = { 2448 &mod_execops, "32-bit exec module for elf", &esw32 2449 }; 2450 #endif /* _LP64 */ 2451 2452 static struct modlinkage modlinkage = { 2453 MODREV_1, 2454 (void *)&modlexec, 2455 #ifdef _LP64 2456 (void *)&modlexec32, 2457 #endif /* _LP64 */ 2458 NULL 2459 }; 2460 2461 int 2462 _init(void) 2463 { 2464 return (mod_install(&modlinkage)); 2465 } 2466 2467 int 2468 _fini(void) 2469 { 2470 return (mod_remove(&modlinkage)); 2471 } 2472 2473 int 2474 _info(struct modinfo *modinfop) 2475 { 2476 return (mod_info(&modlinkage, modinfop)); 2477 } 2478 2479 #endif /* !_ELF32_COMPAT */ 2480