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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 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/cmn_err.h> 47 #include <sys/systm.h> 48 #include <sys/elf.h> 49 #include <sys/vmsystm.h> 50 #include <sys/debug.h> 51 #include <sys/auxv.h> 52 #include <sys/exec.h> 53 #include <sys/prsystm.h> 54 #include <vm/as.h> 55 #include <vm/rm.h> 56 #include <vm/seg.h> 57 #include <vm/seg_vn.h> 58 #include <sys/modctl.h> 59 #include <sys/systeminfo.h> 60 #include <sys/vmparam.h> 61 #include <sys/machelf.h> 62 #include <sys/shm_impl.h> 63 #include <sys/archsystm.h> 64 #include <sys/fasttrap.h> 65 #include "elf_impl.h" 66 67 extern int at_flags; 68 69 #define ORIGIN_STR "ORIGIN" 70 #define ORIGIN_STR_SIZE 6 71 72 static int getelfhead(vnode_t *, cred_t *, Ehdr *, int *, int *, int *); 73 static int getelfphdr(vnode_t *, cred_t *, const Ehdr *, int, caddr_t *, 74 ssize_t *); 75 static int getelfshdr(vnode_t *, cred_t *, const Ehdr *, int, int, caddr_t *, 76 ssize_t *, caddr_t *, ssize_t *); 77 static size_t elfsize(Ehdr *, int, caddr_t, uintptr_t *); 78 static int mapelfexec(vnode_t *, Ehdr *, int, caddr_t, 79 Phdr **, Phdr **, Phdr **, Phdr **, Phdr *, 80 caddr_t *, caddr_t *, intptr_t *, size_t, long *, size_t *); 81 82 typedef enum { 83 STR_CTF, 84 STR_SYMTAB, 85 STR_DYNSYM, 86 STR_STRTAB, 87 STR_DYNSTR, 88 STR_SHSTRTAB, 89 STR_NUM 90 } shstrtype_t; 91 92 static const char *shstrtab_data[] = { 93 ".SUNW_ctf", 94 ".symtab", 95 ".dynsym", 96 ".strtab", 97 ".dynstr", 98 ".shstrtab" 99 }; 100 101 typedef struct shstrtab { 102 int sst_ndx[STR_NUM]; 103 int sst_cur; 104 } shstrtab_t; 105 106 static void 107 shstrtab_init(shstrtab_t *s) 108 { 109 bzero(&s->sst_ndx, sizeof (s->sst_ndx)); 110 s->sst_cur = 1; 111 } 112 113 static int 114 shstrtab_ndx(shstrtab_t *s, shstrtype_t type) 115 { 116 int ret; 117 118 if ((ret = s->sst_ndx[type]) != 0) 119 return (ret); 120 121 ret = s->sst_ndx[type] = s->sst_cur; 122 s->sst_cur += strlen(shstrtab_data[type]) + 1; 123 124 return (ret); 125 } 126 127 static size_t 128 shstrtab_size(const shstrtab_t *s) 129 { 130 return (s->sst_cur); 131 } 132 133 static void 134 shstrtab_dump(const shstrtab_t *s, char *buf) 135 { 136 int i, ndx; 137 138 *buf = '\0'; 139 for (i = 0; i < STR_NUM; i++) { 140 if ((ndx = s->sst_ndx[i]) != 0) 141 (void) strcpy(buf + ndx, shstrtab_data[i]); 142 } 143 } 144 145 static int 146 dtrace_safe_phdr(Phdr *phdrp, struct uarg *args, uintptr_t base) 147 { 148 ASSERT(phdrp->p_type == PT_SUNWDTRACE); 149 150 /* 151 * See the comment in fasttrap.h for information on how to safely 152 * update this program header. 153 */ 154 if (phdrp->p_memsz < PT_SUNWDTRACE_SIZE || 155 (phdrp->p_flags & (PF_R | PF_W | PF_X)) != (PF_R | PF_W | PF_X)) 156 return (-1); 157 158 args->thrptr = phdrp->p_vaddr + base; 159 160 return (0); 161 } 162 163 /*ARGSUSED*/ 164 int 165 elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap, 166 int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred) 167 { 168 caddr_t phdrbase = NULL; 169 caddr_t bssbase = 0; 170 caddr_t brkbase = 0; 171 size_t brksize = 0; 172 ssize_t dlnsize; 173 aux_entry_t *aux; 174 int error; 175 ssize_t resid; 176 int fd = -1; 177 intptr_t voffset; 178 Phdr *dyphdr = NULL; 179 Phdr *stphdr = NULL; 180 Phdr *uphdr = NULL; 181 Phdr *junk = NULL; 182 size_t len; 183 ssize_t phdrsize; 184 int postfixsize = 0; 185 int i, hsize; 186 Phdr *phdrp; 187 Phdr *dataphdrp = NULL; 188 Phdr *dtrphdr; 189 int hasu = 0; 190 int hasauxv = 0; 191 int hasdy = 0; 192 193 struct proc *p = ttoproc(curthread); 194 struct user *up = PTOU(p); 195 struct bigwad { 196 Ehdr ehdr; 197 aux_entry_t elfargs[__KERN_NAUXV_IMPL]; 198 char dl_name[MAXPATHLEN]; 199 char pathbuf[MAXPATHLEN]; 200 struct vattr vattr; 201 struct execenv exenv; 202 } *bigwad; /* kmem_alloc this behemoth so we don't blow stack */ 203 Ehdr *ehdrp; 204 int nshdrs, shstrndx, nphdrs; 205 char *dlnp; 206 char *pathbufp; 207 rlim64_t limit; 208 rlim64_t roundlimit; 209 210 ASSERT(p->p_model == DATAMODEL_ILP32 || p->p_model == DATAMODEL_LP64); 211 212 bigwad = kmem_alloc(sizeof (struct bigwad), KM_SLEEP); 213 ehdrp = &bigwad->ehdr; 214 dlnp = bigwad->dl_name; 215 pathbufp = bigwad->pathbuf; 216 217 /* 218 * Obtain ELF and program header information. 219 */ 220 if ((error = getelfhead(vp, CRED(), ehdrp, &nshdrs, &shstrndx, 221 &nphdrs)) != 0 || 222 (error = getelfphdr(vp, CRED(), ehdrp, nphdrs, &phdrbase, 223 &phdrsize)) != 0) 224 goto out; 225 226 /* 227 * Put data model that we're exec-ing to into the args passed to 228 * exec_args(), so it will know what it is copying to on new stack. 229 * Now that we know whether we are exec-ing a 32-bit or 64-bit 230 * executable, we can set execsz with the appropriate NCARGS. 231 */ 232 #ifdef _LP64 233 if (ehdrp->e_ident[EI_CLASS] == ELFCLASS32) { 234 args->to_model = DATAMODEL_ILP32; 235 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS32-1); 236 } else { 237 args->to_model = DATAMODEL_LP64; 238 args->stk_prot &= ~PROT_EXEC; 239 #if defined(__i386) || defined(__amd64) 240 args->dat_prot &= ~PROT_EXEC; 241 #endif 242 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS64-1); 243 } 244 #else /* _LP64 */ 245 args->to_model = DATAMODEL_ILP32; 246 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS-1); 247 #endif /* _LP64 */ 248 249 /* 250 * Determine aux size now so that stack can be built 251 * in one shot (except actual copyout of aux image), 252 * determine any non-default stack protections, 253 * and still have this code be machine independent. 254 */ 255 hsize = ehdrp->e_phentsize; 256 phdrp = (Phdr *)phdrbase; 257 for (i = nphdrs; i > 0; i--) { 258 switch (phdrp->p_type) { 259 case PT_INTERP: 260 hasauxv = hasdy = 1; 261 break; 262 case PT_PHDR: 263 hasu = 1; 264 break; 265 case PT_SUNWSTACK: 266 args->stk_prot = PROT_USER; 267 if (phdrp->p_flags & PF_R) 268 args->stk_prot |= PROT_READ; 269 if (phdrp->p_flags & PF_W) 270 args->stk_prot |= PROT_WRITE; 271 if (phdrp->p_flags & PF_X) 272 args->stk_prot |= PROT_EXEC; 273 break; 274 case PT_LOAD: 275 dataphdrp = phdrp; 276 break; 277 } 278 phdrp = (Phdr *)((caddr_t)phdrp + hsize); 279 } 280 281 if (ehdrp->e_type != ET_EXEC) { 282 dataphdrp = NULL; 283 hasauxv = 1; 284 } 285 286 /* Copy BSS permissions to args->dat_prot */ 287 if (dataphdrp != NULL) { 288 args->dat_prot = PROT_USER; 289 if (dataphdrp->p_flags & PF_R) 290 args->dat_prot |= PROT_READ; 291 if (dataphdrp->p_flags & PF_W) 292 args->dat_prot |= PROT_WRITE; 293 if (dataphdrp->p_flags & PF_X) 294 args->dat_prot |= PROT_EXEC; 295 } 296 297 /* 298 * If a auxvector will be required - reserve the space for 299 * it now. This may be increased by exec_args if there are 300 * ISA-specific types (included in __KERN_NAUXV_IMPL). 301 */ 302 if (hasauxv) { 303 /* 304 * If a AUX vector is being built - the base AUX 305 * entries are: 306 * 307 * AT_BASE 308 * AT_FLAGS 309 * AT_PAGESZ 310 * AT_SUN_LDSECURE 311 * AT_SUN_HWCAP 312 * AT_SUN_PLATFORM 313 * AT_SUN_EXECNAME 314 * AT_NULL 315 * 316 * total == 8 317 */ 318 if (hasdy && hasu) { 319 /* 320 * Has PT_INTERP & PT_PHDR - the auxvectors that 321 * will be built are: 322 * 323 * AT_PHDR 324 * AT_PHENT 325 * AT_PHNUM 326 * AT_ENTRY 327 * AT_LDDATA 328 * 329 * total = 5 330 */ 331 args->auxsize = (8 + 5) * sizeof (aux_entry_t); 332 } else if (hasdy) { 333 /* 334 * Has PT_INTERP but no PT_PHDR 335 * 336 * AT_EXECFD 337 * AT_LDDATA 338 * 339 * total = 2 340 */ 341 args->auxsize = (8 + 2) * sizeof (aux_entry_t); 342 } else { 343 args->auxsize = 8 * sizeof (aux_entry_t); 344 } 345 } else 346 args->auxsize = 0; 347 348 aux = bigwad->elfargs; 349 /* 350 * Move args to the user's stack. 351 */ 352 if ((error = exec_args(uap, args, idatap, (void **)&aux)) != 0) { 353 if (error == -1) { 354 error = ENOEXEC; 355 goto bad; 356 } 357 goto out; 358 } 359 360 /* 361 * If this is an ET_DYN executable (shared object), 362 * determine its memory size so that mapelfexec() can load it. 363 */ 364 if (ehdrp->e_type == ET_DYN) 365 len = elfsize(ehdrp, nphdrs, phdrbase, NULL); 366 else 367 len = 0; 368 369 dtrphdr = NULL; 370 371 if ((error = mapelfexec(vp, ehdrp, nphdrs, phdrbase, &uphdr, &dyphdr, 372 &stphdr, &dtrphdr, dataphdrp, &bssbase, &brkbase, &voffset, len, 373 execsz, &brksize)) != 0) 374 goto bad; 375 376 if (uphdr != NULL && dyphdr == NULL) 377 goto bad; 378 379 if (dtrphdr != NULL && dtrace_safe_phdr(dtrphdr, args, voffset) != 0) { 380 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, exec_file); 381 goto bad; 382 } 383 384 if (dyphdr != NULL) { 385 size_t len; 386 uintptr_t lddata; 387 char *p; 388 struct vnode *nvp; 389 390 dlnsize = dyphdr->p_filesz; 391 392 if (dlnsize > MAXPATHLEN || dlnsize <= 0) 393 goto bad; 394 395 /* 396 * Read in "interpreter" pathname. 397 */ 398 if ((error = vn_rdwr(UIO_READ, vp, dlnp, dyphdr->p_filesz, 399 (offset_t)dyphdr->p_offset, UIO_SYSSPACE, 0, (rlim64_t)0, 400 CRED(), &resid)) != 0) { 401 uprintf("%s: Cannot obtain interpreter pathname\n", 402 exec_file); 403 goto bad; 404 } 405 406 if (resid != 0 || dlnp[dlnsize - 1] != '\0') 407 goto bad; 408 409 /* 410 * Search for '$ORIGIN' token in interpreter path. 411 * If found, expand it. 412 */ 413 for (p = dlnp; p = strchr(p, '$'); ) { 414 uint_t len, curlen; 415 char *_ptr; 416 417 if (strncmp(++p, ORIGIN_STR, ORIGIN_STR_SIZE)) 418 continue; 419 420 curlen = 0; 421 len = p - dlnp - 1; 422 if (len) { 423 bcopy(dlnp, pathbufp, len); 424 curlen += len; 425 } 426 if (_ptr = strrchr(args->pathname, '/')) { 427 len = _ptr - args->pathname; 428 if ((curlen + len) > MAXPATHLEN) 429 break; 430 431 bcopy(args->pathname, &pathbufp[curlen], len); 432 curlen += len; 433 } else { 434 /* 435 * executable is a basename found in the 436 * current directory. So - just substitue 437 * '.' for ORIGIN. 438 */ 439 pathbufp[curlen] = '.'; 440 curlen++; 441 } 442 p += ORIGIN_STR_SIZE; 443 len = strlen(p); 444 445 if ((curlen + len) > MAXPATHLEN) 446 break; 447 bcopy(p, &pathbufp[curlen], len); 448 curlen += len; 449 pathbufp[curlen++] = '\0'; 450 bcopy(pathbufp, dlnp, curlen); 451 } 452 453 /* 454 * /usr/lib/ld.so.1 is known to be a symlink to /lib/ld.so.1 455 * (and /usr/lib/64/ld.so.1 is a symlink to /lib/64/ld.so.1). 456 * Just in case /usr is not mounted, change it now. 457 */ 458 if (strcmp(dlnp, USR_LIB_RTLD) == 0) 459 dlnp += 4; 460 error = lookupname(dlnp, UIO_SYSSPACE, FOLLOW, NULLVPP, &nvp); 461 if (error && dlnp != bigwad->dl_name) { 462 /* new kernel, old user-level */ 463 error = lookupname(dlnp -= 4, UIO_SYSSPACE, FOLLOW, 464 NULLVPP, &nvp); 465 } 466 if (error) { 467 uprintf("%s: Cannot find %s\n", exec_file, dlnp); 468 goto bad; 469 } 470 471 /* 472 * Setup the "aux" vector. 473 */ 474 if (uphdr) { 475 if (ehdrp->e_type == ET_DYN) { 476 /* don't use the first page */ 477 bigwad->exenv.ex_brkbase = (caddr_t)PAGESIZE; 478 bigwad->exenv.ex_bssbase = (caddr_t)PAGESIZE; 479 } else { 480 bigwad->exenv.ex_bssbase = bssbase; 481 bigwad->exenv.ex_brkbase = brkbase; 482 } 483 bigwad->exenv.ex_brksize = brksize; 484 bigwad->exenv.ex_magic = elfmagic; 485 bigwad->exenv.ex_vp = vp; 486 setexecenv(&bigwad->exenv); 487 488 ADDAUX(aux, AT_PHDR, uphdr->p_vaddr + voffset) 489 ADDAUX(aux, AT_PHENT, ehdrp->e_phentsize) 490 ADDAUX(aux, AT_PHNUM, nphdrs) 491 ADDAUX(aux, AT_ENTRY, ehdrp->e_entry + voffset) 492 } else { 493 if ((error = execopen(&vp, &fd)) != 0) { 494 VN_RELE(nvp); 495 goto bad; 496 } 497 498 ADDAUX(aux, AT_EXECFD, fd) 499 } 500 501 if ((error = execpermissions(nvp, &bigwad->vattr, args)) != 0) { 502 VN_RELE(nvp); 503 uprintf("%s: Cannot execute %s\n", exec_file, dlnp); 504 goto bad; 505 } 506 507 /* 508 * Now obtain the ELF header along with the entire program 509 * header contained in "nvp". 510 */ 511 kmem_free(phdrbase, phdrsize); 512 phdrbase = NULL; 513 if ((error = getelfhead(nvp, CRED(), ehdrp, &nshdrs, 514 &shstrndx, &nphdrs)) != 0 || 515 (error = getelfphdr(nvp, CRED(), ehdrp, nphdrs, &phdrbase, 516 &phdrsize)) != 0) { 517 VN_RELE(nvp); 518 uprintf("%s: Cannot read %s\n", exec_file, dlnp); 519 goto bad; 520 } 521 522 /* 523 * Determine memory size of the "interpreter's" loadable 524 * sections. This size is then used to obtain the virtual 525 * address of a hole, in the user's address space, large 526 * enough to map the "interpreter". 527 */ 528 if ((len = elfsize(ehdrp, nphdrs, phdrbase, &lddata)) == 0) { 529 VN_RELE(nvp); 530 uprintf("%s: Nothing to load in %s\n", exec_file, dlnp); 531 goto bad; 532 } 533 534 dtrphdr = NULL; 535 536 error = mapelfexec(nvp, ehdrp, nphdrs, phdrbase, &junk, &junk, 537 &junk, &dtrphdr, NULL, NULL, NULL, &voffset, len, execsz, 538 NULL); 539 if (error || junk != NULL) { 540 VN_RELE(nvp); 541 uprintf("%s: Cannot map %s\n", exec_file, dlnp); 542 goto bad; 543 } 544 545 /* 546 * We use the DTrace program header to initialize the 547 * architecture-specific user per-LWP location. The dtrace 548 * fasttrap provider requires ready access to per-LWP scratch 549 * space. We assume that there is only one such program header 550 * in the interpreter. 551 */ 552 if (dtrphdr != NULL && 553 dtrace_safe_phdr(dtrphdr, args, voffset) != 0) { 554 VN_RELE(nvp); 555 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, dlnp); 556 goto bad; 557 } 558 559 VN_RELE(nvp); 560 ADDAUX(aux, AT_SUN_LDDATA, voffset + lddata) 561 } 562 563 if (hasauxv) { 564 int auxf = AF_SUN_HWCAPVERIFY; 565 /* 566 * Note: AT_SUN_PLATFORM was filled in via exec_args() 567 */ 568 ADDAUX(aux, AT_BASE, voffset) 569 ADDAUX(aux, AT_FLAGS, at_flags) 570 ADDAUX(aux, AT_PAGESZ, PAGESIZE) 571 /* 572 * Linker flags. (security) 573 * p_flag not yet set at this time. 574 * We rely on gexec() to provide us with the information. 575 */ 576 ADDAUX(aux, AT_SUN_AUXFLAGS, 577 setid ? AF_SUN_SETUGID | auxf : auxf); 578 /* 579 * Hardware capability flag word (performance hints) 580 * Used for choosing faster library routines. 581 * (Potentially different between 32-bit and 64-bit ABIs) 582 */ 583 #if defined(_LP64) 584 if (args->to_model == DATAMODEL_NATIVE) 585 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap) 586 else 587 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap32) 588 #else 589 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap) 590 #endif 591 ADDAUX(aux, AT_NULL, 0) 592 postfixsize = (char *)aux - (char *)bigwad->elfargs; 593 ASSERT(postfixsize == args->auxsize); 594 ASSERT(postfixsize <= __KERN_NAUXV_IMPL * sizeof (aux_entry_t)); 595 } 596 597 /* 598 * For the 64-bit kernel, the limit is big enough that rounding it up 599 * to a page can overflow the 64-bit limit, so we check for btopr() 600 * overflowing here by comparing it with the unrounded limit in pages. 601 * If it hasn't overflowed, compare the exec size with the rounded up 602 * limit in pages. Otherwise, just compare with the unrounded limit. 603 */ 604 limit = btop(p->p_vmem_ctl); 605 roundlimit = btopr(p->p_vmem_ctl); 606 if ((roundlimit > limit && *execsz > roundlimit) || 607 (roundlimit < limit && *execsz > limit)) { 608 mutex_enter(&p->p_lock); 609 (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p, 610 RCA_SAFE); 611 mutex_exit(&p->p_lock); 612 error = ENOMEM; 613 goto bad; 614 } 615 616 bzero(up->u_auxv, sizeof (up->u_auxv)); 617 if (postfixsize) { 618 int num_auxv; 619 620 /* 621 * Copy the aux vector to the user stack. 622 */ 623 error = execpoststack(args, bigwad->elfargs, postfixsize); 624 if (error) 625 goto bad; 626 627 /* 628 * Copy auxv to the process's user structure for use by /proc. 629 */ 630 num_auxv = postfixsize / sizeof (aux_entry_t); 631 ASSERT(num_auxv <= sizeof (up->u_auxv) / sizeof (auxv_t)); 632 aux = bigwad->elfargs; 633 for (i = 0; i < num_auxv; i++) { 634 up->u_auxv[i].a_type = aux[i].a_type; 635 up->u_auxv[i].a_un.a_val = (aux_val_t)aux[i].a_un.a_val; 636 } 637 } 638 639 /* 640 * Pass back the starting address so we can set the program counter. 641 */ 642 args->entry = (uintptr_t)(ehdrp->e_entry + voffset); 643 644 if (!uphdr) { 645 if (ehdrp->e_type == ET_DYN) { 646 /* 647 * If we are executing a shared library which doesn't 648 * have a interpreter (probably ld.so.1) then 649 * we don't set the brkbase now. Instead we 650 * delay it's setting until the first call 651 * via grow.c::brk(). This permits ld.so.1 to 652 * initialize brkbase to the tail of the executable it 653 * loads (which is where it needs to be). 654 */ 655 bigwad->exenv.ex_brkbase = (caddr_t)0; 656 bigwad->exenv.ex_bssbase = (caddr_t)0; 657 bigwad->exenv.ex_brksize = 0; 658 } else { 659 bigwad->exenv.ex_brkbase = brkbase; 660 bigwad->exenv.ex_bssbase = bssbase; 661 bigwad->exenv.ex_brksize = brksize; 662 } 663 bigwad->exenv.ex_magic = elfmagic; 664 bigwad->exenv.ex_vp = vp; 665 setexecenv(&bigwad->exenv); 666 } 667 668 ASSERT(error == 0); 669 goto out; 670 671 bad: 672 if (fd != -1) /* did we open the a.out yet */ 673 (void) execclose(fd); 674 675 psignal(p, SIGKILL); 676 677 if (error == 0) 678 error = ENOEXEC; 679 out: 680 if (phdrbase != NULL) 681 kmem_free(phdrbase, phdrsize); 682 kmem_free(bigwad, sizeof (struct bigwad)); 683 return (error); 684 } 685 686 /* 687 * Compute the memory size requirement for the ELF file. 688 */ 689 static size_t 690 elfsize(Ehdr *ehdrp, int nphdrs, caddr_t phdrbase, uintptr_t *lddata) 691 { 692 size_t len; 693 Phdr *phdrp = (Phdr *)phdrbase; 694 int hsize = ehdrp->e_phentsize; 695 int first = 1; 696 int dfirst = 1; /* first data segment */ 697 uintptr_t loaddr = 0; 698 uintptr_t hiaddr = 0; 699 uintptr_t lo, hi; 700 int i; 701 702 for (i = nphdrs; i > 0; i--) { 703 if (phdrp->p_type == PT_LOAD) { 704 lo = phdrp->p_vaddr; 705 hi = lo + phdrp->p_memsz; 706 if (first) { 707 loaddr = lo; 708 hiaddr = hi; 709 first = 0; 710 } else { 711 if (loaddr > lo) 712 loaddr = lo; 713 if (hiaddr < hi) 714 hiaddr = hi; 715 } 716 717 /* 718 * save the address of the first data segment 719 * of a object - used for the AT_SUNW_LDDATA 720 * aux entry. 721 */ 722 if ((lddata != NULL) && dfirst && 723 (phdrp->p_flags & PF_W)) { 724 *lddata = lo; 725 dfirst = 0; 726 } 727 } 728 phdrp = (Phdr *)((caddr_t)phdrp + hsize); 729 } 730 731 len = hiaddr - (loaddr & PAGEMASK); 732 len = roundup(len, PAGESIZE); 733 734 return (len); 735 } 736 737 /* 738 * Read in the ELF header and program header table. 739 * SUSV3 requires: 740 * ENOEXEC File format is not recognized 741 * EINVAL Format recognized but execution not supported 742 */ 743 static int 744 getelfhead(vnode_t *vp, cred_t *credp, Ehdr *ehdr, int *nshdrs, int *shstrndx, 745 int *nphdrs) 746 { 747 int error; 748 ssize_t resid; 749 750 /* 751 * We got here by the first two bytes in ident, 752 * now read the entire ELF header. 753 */ 754 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)ehdr, 755 sizeof (Ehdr), (offset_t)0, UIO_SYSSPACE, 0, 756 (rlim64_t)0, credp, &resid)) != 0) 757 return (error); 758 759 /* 760 * Since a separate version is compiled for handling 32-bit and 761 * 64-bit ELF executables on a 64-bit kernel, the 64-bit version 762 * doesn't need to be able to deal with 32-bit ELF files. 763 */ 764 if (resid != 0 || 765 ehdr->e_ident[EI_MAG2] != ELFMAG2 || 766 ehdr->e_ident[EI_MAG3] != ELFMAG3) 767 return (ENOEXEC); 768 769 if ((ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) || 770 #if defined(_ILP32) || defined(_ELF32_COMPAT) 771 ehdr->e_ident[EI_CLASS] != ELFCLASS32 || 772 #else 773 ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 774 #endif 775 !elfheadcheck(ehdr->e_ident[EI_DATA], ehdr->e_machine, 776 ehdr->e_flags)) 777 return (EINVAL); 778 779 *nshdrs = ehdr->e_shnum; 780 *shstrndx = ehdr->e_shstrndx; 781 *nphdrs = ehdr->e_phnum; 782 783 /* 784 * If e_shnum, e_shstrndx, or e_phnum is its sentinel value, we need 785 * to read in the section header at index zero to acces the true 786 * values for those fields. 787 */ 788 if ((*nshdrs == 0 && ehdr->e_shoff != 0) || 789 *shstrndx == SHN_XINDEX || *nphdrs == PN_XNUM) { 790 Shdr shdr; 791 792 if (ehdr->e_shoff == 0) 793 return (EINVAL); 794 795 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&shdr, 796 sizeof (shdr), (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, 797 (rlim64_t)0, credp, &resid)) != 0) 798 return (error); 799 800 if (*nshdrs == 0) 801 *nshdrs = shdr.sh_size; 802 if (*shstrndx == SHN_XINDEX) 803 *shstrndx = shdr.sh_link; 804 if (*nphdrs == PN_XNUM && shdr.sh_info != 0) 805 *nphdrs = shdr.sh_info; 806 } 807 808 return (0); 809 } 810 811 #ifdef _ELF32_COMPAT 812 extern size_t elf_nphdr_max; 813 #else 814 size_t elf_nphdr_max = 1000; 815 #endif 816 817 static int 818 getelfphdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, int nphdrs, 819 caddr_t *phbasep, ssize_t *phsizep) 820 { 821 ssize_t resid, minsize; 822 int err; 823 824 /* 825 * Since we're going to be using e_phentsize to iterate down the 826 * array of program headers, it must be 8-byte aligned or else 827 * a we might cause a misaligned access. We use all members through 828 * p_flags on 32-bit ELF files and p_memsz on 64-bit ELF files so 829 * e_phentsize must be at least large enough to include those 830 * members. 831 */ 832 #if !defined(_LP64) || defined(_ELF32_COMPAT) 833 minsize = offsetof(Phdr, p_flags) + sizeof (((Phdr *)NULL)->p_flags); 834 #else 835 minsize = offsetof(Phdr, p_memsz) + sizeof (((Phdr *)NULL)->p_memsz); 836 #endif 837 if (ehdr->e_phentsize < minsize || (ehdr->e_phentsize & 3)) 838 return (EINVAL); 839 840 *phsizep = nphdrs * ehdr->e_phentsize; 841 842 if (*phsizep > sizeof (Phdr) * elf_nphdr_max) { 843 if ((*phbasep = kmem_alloc(*phsizep, KM_NOSLEEP)) == NULL) 844 return (ENOMEM); 845 } else { 846 *phbasep = kmem_alloc(*phsizep, KM_SLEEP); 847 } 848 849 if ((err = vn_rdwr(UIO_READ, vp, *phbasep, *phsizep, 850 (offset_t)ehdr->e_phoff, UIO_SYSSPACE, 0, (rlim64_t)0, 851 credp, &resid)) != 0) { 852 kmem_free(*phbasep, *phsizep); 853 *phbasep = NULL; 854 return (err); 855 } 856 857 return (0); 858 } 859 860 #ifdef _ELF32_COMPAT 861 extern size_t elf_nshdr_max; 862 extern size_t elf_shstrtab_max; 863 #else 864 size_t elf_nshdr_max = 10000; 865 size_t elf_shstrtab_max = 100 * 1024; 866 #endif 867 868 869 static int 870 getelfshdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, 871 int nshdrs, int shstrndx, caddr_t *shbasep, ssize_t *shsizep, 872 char **shstrbasep, ssize_t *shstrsizep) 873 { 874 ssize_t resid, minsize; 875 int err; 876 Shdr *shdr; 877 878 /* 879 * Since we're going to be using e_shentsize to iterate down the 880 * array of section headers, it must be 8-byte aligned or else 881 * a we might cause a misaligned access. We use all members through 882 * sh_entsize (on both 32- and 64-bit ELF files) so e_shentsize 883 * must be at least large enough to include that member. The index 884 * of the string table section must also be valid. 885 */ 886 minsize = offsetof(Shdr, sh_entsize) + sizeof (shdr->sh_entsize); 887 if (ehdr->e_shentsize < minsize || (ehdr->e_shentsize & 3) || 888 shstrndx >= nshdrs) 889 return (EINVAL); 890 891 *shsizep = nshdrs * ehdr->e_shentsize; 892 893 if (*shsizep > sizeof (Shdr) * elf_nshdr_max) { 894 if ((*shbasep = kmem_alloc(*shsizep, KM_NOSLEEP)) == NULL) 895 return (ENOMEM); 896 } else { 897 *shbasep = kmem_alloc(*shsizep, KM_SLEEP); 898 } 899 900 if ((err = vn_rdwr(UIO_READ, vp, *shbasep, *shsizep, 901 (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, (rlim64_t)0, 902 credp, &resid)) != 0) { 903 kmem_free(*shbasep, *shsizep); 904 return (err); 905 } 906 907 /* 908 * Pull the section string table out of the vnode; fail if the size 909 * is zero. 910 */ 911 shdr = (Shdr *)(*shbasep + shstrndx * ehdr->e_shentsize); 912 if ((*shstrsizep = shdr->sh_size) == 0) { 913 kmem_free(*shbasep, *shsizep); 914 return (EINVAL); 915 } 916 917 if (*shstrsizep > elf_shstrtab_max) { 918 if ((*shstrbasep = kmem_alloc(*shstrsizep, 919 KM_NOSLEEP)) == NULL) { 920 kmem_free(*shbasep, *shsizep); 921 return (ENOMEM); 922 } 923 } else { 924 *shstrbasep = kmem_alloc(*shstrsizep, KM_SLEEP); 925 } 926 927 if ((err = vn_rdwr(UIO_READ, vp, *shstrbasep, *shstrsizep, 928 (offset_t)shdr->sh_offset, UIO_SYSSPACE, 0, (rlim64_t)0, 929 credp, &resid)) != 0) { 930 kmem_free(*shbasep, *shsizep); 931 kmem_free(*shstrbasep, *shstrsizep); 932 return (err); 933 } 934 935 /* 936 * Make sure the strtab is null-terminated to make sure we 937 * don't run off the end of the table. 938 */ 939 (*shstrbasep)[*shstrsizep - 1] = '\0'; 940 941 return (0); 942 } 943 944 static int 945 mapelfexec( 946 vnode_t *vp, 947 Ehdr *ehdr, 948 int nphdrs, 949 caddr_t phdrbase, 950 Phdr **uphdr, 951 Phdr **dyphdr, 952 Phdr **stphdr, 953 Phdr **dtphdr, 954 Phdr *dataphdrp, 955 caddr_t *bssbase, 956 caddr_t *brkbase, 957 intptr_t *voffset, 958 size_t len, 959 long *execsz, 960 size_t *brksize) 961 { 962 Phdr *phdr; 963 int i, prot, error; 964 caddr_t addr; 965 size_t zfodsz; 966 int ptload = 0; 967 int page; 968 off_t offset; 969 int hsize = ehdr->e_phentsize; 970 971 if (ehdr->e_type == ET_DYN) { 972 /* 973 * Obtain the virtual address of a hole in the 974 * address space to map the "interpreter". 975 */ 976 map_addr(&addr, len, (offset_t)0, 1, 0); 977 if (addr == NULL) 978 return (ENOMEM); 979 *voffset = (intptr_t)addr; 980 } else { 981 *voffset = 0; 982 } 983 phdr = (Phdr *)phdrbase; 984 for (i = nphdrs; i > 0; i--) { 985 switch (phdr->p_type) { 986 case PT_LOAD: 987 if ((*dyphdr != NULL) && (*uphdr == NULL)) 988 return (0); 989 990 ptload = 1; 991 prot = PROT_USER; 992 if (phdr->p_flags & PF_R) 993 prot |= PROT_READ; 994 if (phdr->p_flags & PF_W) 995 prot |= PROT_WRITE; 996 if (phdr->p_flags & PF_X) 997 prot |= PROT_EXEC; 998 999 addr = (caddr_t)((uintptr_t)phdr->p_vaddr + *voffset); 1000 zfodsz = (size_t)phdr->p_memsz - phdr->p_filesz; 1001 1002 offset = phdr->p_offset; 1003 if (((uintptr_t)offset & PAGEOFFSET) == 1004 ((uintptr_t)addr & PAGEOFFSET) && 1005 (!(vp->v_flag & VNOMAP))) { 1006 page = 1; 1007 } else { 1008 page = 0; 1009 } 1010 1011 if (curproc->p_brkpageszc != 0 && phdr == dataphdrp && 1012 (prot & PROT_WRITE)) { 1013 /* 1014 * segvn only uses large pages for segments 1015 * that have the requested large page size 1016 * aligned base and size. To insure the part 1017 * of bss that starts at heap large page size 1018 * boundary gets mapped by large pages create 1019 * 2 bss segvn segments which is accomplished 1020 * by calling execmap twice. First execmap 1021 * will create the bss segvn segment that is 1022 * before the large page boundary and it will 1023 * be mapped with base pages. If bss start is 1024 * already large page aligned only 1 bss 1025 * segment will be created. The second bss 1026 * segment's size is large page size aligned 1027 * so that segvn uses large pages for that 1028 * segment and it also makes the heap that 1029 * starts right after bss to start at large 1030 * page boundary. 1031 */ 1032 uint_t szc = curproc->p_brkpageszc; 1033 size_t pgsz = page_get_pagesize(szc); 1034 caddr_t zaddr = addr + phdr->p_filesz; 1035 size_t zlen = P2NPHASE((uintptr_t)zaddr, pgsz); 1036 1037 ASSERT(pgsz > PAGESIZE); 1038 1039 if (error = execmap(vp, addr, phdr->p_filesz, 1040 zlen, phdr->p_offset, prot, page, szc)) 1041 goto bad; 1042 if (zfodsz > zlen) { 1043 zfodsz -= zlen; 1044 zaddr += zlen; 1045 zlen = P2ROUNDUP(zfodsz, pgsz); 1046 if (error = execmap(vp, zaddr, 0, zlen, 1047 phdr->p_offset, prot, page, szc)) 1048 goto bad; 1049 } 1050 if (brksize != NULL) 1051 *brksize = zlen - zfodsz; 1052 } else { 1053 if (error = execmap(vp, addr, phdr->p_filesz, 1054 zfodsz, phdr->p_offset, prot, page, 0)) 1055 goto bad; 1056 } 1057 1058 if (bssbase != NULL && addr >= *bssbase && 1059 phdr == dataphdrp) { 1060 *bssbase = addr + phdr->p_filesz; 1061 } 1062 if (brkbase != NULL && addr >= *brkbase) { 1063 *brkbase = addr + phdr->p_memsz; 1064 } 1065 1066 *execsz += btopr(phdr->p_memsz); 1067 break; 1068 1069 case PT_INTERP: 1070 if (ptload) 1071 goto bad; 1072 *dyphdr = phdr; 1073 break; 1074 1075 case PT_SHLIB: 1076 *stphdr = phdr; 1077 break; 1078 1079 case PT_PHDR: 1080 if (ptload) 1081 goto bad; 1082 *uphdr = phdr; 1083 break; 1084 1085 case PT_NULL: 1086 case PT_DYNAMIC: 1087 case PT_NOTE: 1088 break; 1089 1090 case PT_SUNWDTRACE: 1091 if (dtphdr != NULL) 1092 *dtphdr = phdr; 1093 break; 1094 1095 default: 1096 break; 1097 } 1098 phdr = (Phdr *)((caddr_t)phdr + hsize); 1099 } 1100 return (0); 1101 bad: 1102 if (error == 0) 1103 error = EINVAL; 1104 return (error); 1105 } 1106 1107 int 1108 elfnote(vnode_t *vp, offset_t *offsetp, int type, int descsz, void *desc, 1109 rlim64_t rlimit, cred_t *credp) 1110 { 1111 Note note; 1112 int error; 1113 1114 bzero(¬e, sizeof (note)); 1115 bcopy("CORE", note.name, 4); 1116 note.nhdr.n_type = type; 1117 /* 1118 * The System V ABI states that n_namesz must be the length of the 1119 * string that follows the Nhdr structure including the terminating 1120 * null. The ABI also specifies that sufficient padding should be 1121 * included so that the description that follows the name string 1122 * begins on a 4- or 8-byte boundary for 32- and 64-bit binaries 1123 * respectively. However, since this change was not made correctly 1124 * at the time of the 64-bit port, both 32- and 64-bit binaries 1125 * descriptions are only guaranteed to begin on a 4-byte boundary. 1126 */ 1127 note.nhdr.n_namesz = 5; 1128 note.nhdr.n_descsz = roundup(descsz, sizeof (Word)); 1129 1130 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, ¬e, 1131 sizeof (note), rlimit, credp)) 1132 return (error); 1133 1134 *offsetp += sizeof (note); 1135 1136 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, desc, 1137 note.nhdr.n_descsz, rlimit, credp)) 1138 return (error); 1139 1140 *offsetp += note.nhdr.n_descsz; 1141 return (0); 1142 } 1143 1144 /* 1145 * Copy the section data from one vnode to the section of another vnode. 1146 */ 1147 static void 1148 copy_scn(Shdr *src, vnode_t *src_vp, Shdr *dst, vnode_t *dst_vp, Off *doffset, 1149 void *buf, size_t size, cred_t *credp, rlim64_t rlimit) 1150 { 1151 ssize_t resid; 1152 size_t len, n = src->sh_size; 1153 offset_t off = 0; 1154 1155 while (n != 0) { 1156 len = MIN(size, n); 1157 if (vn_rdwr(UIO_READ, src_vp, buf, len, src->sh_offset + off, 1158 UIO_SYSSPACE, 0, (rlim64_t)0, credp, &resid) != 0 || 1159 resid >= len || 1160 core_write(dst_vp, UIO_SYSSPACE, *doffset + off, 1161 buf, len - resid, rlimit, credp) != 0) { 1162 dst->sh_size = 0; 1163 dst->sh_offset = 0; 1164 return; 1165 } 1166 1167 ASSERT(n >= len - resid); 1168 1169 n -= len - resid; 1170 off += len - resid; 1171 } 1172 1173 *doffset += src->sh_size; 1174 } 1175 1176 #ifdef _ELF32_COMPAT 1177 extern size_t elf_datasz_max; 1178 #else 1179 size_t elf_datasz_max = 1 * 1024 * 1024; 1180 #endif 1181 1182 /* 1183 * This function processes mappings that correspond to load objects to 1184 * examine their respective sections for elfcore(). It's called once with 1185 * v set to NULL to count the number of sections that we're going to need 1186 * and then again with v set to some allocated buffer that we fill in with 1187 * all the section data. 1188 */ 1189 static int 1190 process_scns(core_content_t content, proc_t *p, cred_t *credp, vnode_t *vp, 1191 Shdr *v, int nshdrs, rlim64_t rlimit, Off *doffsetp, int *nshdrsp) 1192 { 1193 vnode_t *lastvp = NULL; 1194 struct seg *seg; 1195 int i, j; 1196 void *data = NULL; 1197 size_t datasz = 0; 1198 shstrtab_t shstrtab; 1199 struct as *as = p->p_as; 1200 int error = 0; 1201 1202 if (v != NULL) 1203 shstrtab_init(&shstrtab); 1204 1205 i = 1; 1206 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 1207 uint_t prot; 1208 vnode_t *mvp; 1209 void *tmp = NULL; 1210 caddr_t saddr = seg->s_base; 1211 caddr_t naddr; 1212 caddr_t eaddr; 1213 size_t segsize; 1214 1215 Ehdr ehdr; 1216 int nshdrs, shstrndx, nphdrs; 1217 caddr_t shbase; 1218 ssize_t shsize; 1219 char *shstrbase; 1220 ssize_t shstrsize; 1221 1222 Shdr *shdr; 1223 const char *name; 1224 size_t sz; 1225 uintptr_t off; 1226 1227 int ctf_ndx = 0; 1228 int symtab_ndx = 0; 1229 1230 /* 1231 * Since we're just looking for text segments of load 1232 * objects, we only care about the protection bits; we don't 1233 * care about the actual size of the segment so we use the 1234 * reserved size. If the segment's size is zero, there's 1235 * something fishy going on so we ignore this segment. 1236 */ 1237 if (seg->s_ops != &segvn_ops || 1238 SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 || 1239 mvp == lastvp || mvp == NULL || mvp->v_type != VREG || 1240 (segsize = pr_getsegsize(seg, 1)) == 0) 1241 continue; 1242 1243 eaddr = saddr + segsize; 1244 prot = pr_getprot(seg, 1, &tmp, &saddr, &naddr, eaddr); 1245 pr_getprot_done(&tmp); 1246 1247 /* 1248 * Skip this segment unless the protection bits look like 1249 * what we'd expect for a text segment. 1250 */ 1251 if ((prot & (PROT_WRITE | PROT_EXEC)) != PROT_EXEC) 1252 continue; 1253 1254 if (getelfhead(mvp, credp, &ehdr, &nshdrs, &shstrndx, 1255 &nphdrs) != 0 || 1256 getelfshdr(mvp, credp, &ehdr, nshdrs, shstrndx, 1257 &shbase, &shsize, &shstrbase, &shstrsize) != 0) 1258 continue; 1259 1260 off = ehdr.e_shentsize; 1261 for (j = 1; j < nshdrs; j++, off += ehdr.e_shentsize) { 1262 Shdr *symtab = NULL, *strtab; 1263 1264 shdr = (Shdr *)(shbase + off); 1265 1266 if (shdr->sh_name >= shstrsize) 1267 continue; 1268 1269 name = shstrbase + shdr->sh_name; 1270 1271 if (strcmp(name, shstrtab_data[STR_CTF]) == 0) { 1272 if ((content & CC_CONTENT_CTF) == 0 || 1273 ctf_ndx != 0) 1274 continue; 1275 1276 if (shdr->sh_link > 0 && 1277 shdr->sh_link < nshdrs) { 1278 symtab = (Shdr *)(shbase + 1279 shdr->sh_link * ehdr.e_shentsize); 1280 } 1281 1282 if (v != NULL && i < nshdrs - 1) { 1283 if (shdr->sh_size > datasz && 1284 shdr->sh_size <= elf_datasz_max) { 1285 if (data != NULL) 1286 kmem_free(data, datasz); 1287 1288 datasz = shdr->sh_size; 1289 data = kmem_alloc(datasz, 1290 KM_SLEEP); 1291 } 1292 1293 v[i].sh_name = shstrtab_ndx(&shstrtab, 1294 STR_CTF); 1295 v[i].sh_addr = (Addr)(uintptr_t)saddr; 1296 v[i].sh_type = SHT_PROGBITS; 1297 v[i].sh_addralign = 4; 1298 *doffsetp = roundup(*doffsetp, 1299 v[i].sh_addralign); 1300 v[i].sh_offset = *doffsetp; 1301 v[i].sh_size = shdr->sh_size; 1302 if (symtab == NULL) { 1303 v[i].sh_link = 0; 1304 } else if (symtab->sh_type == 1305 SHT_SYMTAB && 1306 symtab_ndx != 0) { 1307 v[i].sh_link = 1308 symtab_ndx; 1309 } else { 1310 v[i].sh_link = i + 1; 1311 } 1312 1313 copy_scn(shdr, mvp, &v[i], vp, 1314 doffsetp, data, datasz, credp, 1315 rlimit); 1316 } 1317 1318 ctf_ndx = i++; 1319 1320 /* 1321 * We've already dumped the symtab. 1322 */ 1323 if (symtab != NULL && 1324 symtab->sh_type == SHT_SYMTAB && 1325 symtab_ndx != 0) 1326 continue; 1327 1328 } else if (strcmp(name, 1329 shstrtab_data[STR_SYMTAB]) == 0) { 1330 if ((content & CC_CONTENT_SYMTAB) == 0 || 1331 symtab != 0) 1332 continue; 1333 1334 symtab = shdr; 1335 } 1336 1337 if (symtab != NULL) { 1338 if ((symtab->sh_type != SHT_DYNSYM && 1339 symtab->sh_type != SHT_SYMTAB) || 1340 symtab->sh_link == 0 || 1341 symtab->sh_link >= nshdrs) 1342 continue; 1343 1344 strtab = (Shdr *)(shbase + 1345 symtab->sh_link * ehdr.e_shentsize); 1346 1347 if (strtab->sh_type != SHT_STRTAB) 1348 continue; 1349 1350 if (v != NULL && i < nshdrs - 2) { 1351 sz = MAX(symtab->sh_size, 1352 strtab->sh_size); 1353 if (sz > datasz && 1354 sz <= elf_datasz_max) { 1355 if (data != NULL) 1356 kmem_free(data, datasz); 1357 1358 datasz = sz; 1359 data = kmem_alloc(datasz, 1360 KM_SLEEP); 1361 } 1362 1363 if (symtab->sh_type == SHT_DYNSYM) { 1364 v[i].sh_name = shstrtab_ndx( 1365 &shstrtab, STR_DYNSYM); 1366 v[i + 1].sh_name = shstrtab_ndx( 1367 &shstrtab, STR_DYNSTR); 1368 } else { 1369 v[i].sh_name = shstrtab_ndx( 1370 &shstrtab, STR_SYMTAB); 1371 v[i + 1].sh_name = shstrtab_ndx( 1372 &shstrtab, STR_STRTAB); 1373 } 1374 1375 v[i].sh_type = symtab->sh_type; 1376 v[i].sh_addr = symtab->sh_addr; 1377 if (ehdr.e_type == ET_DYN || 1378 v[i].sh_addr == 0) 1379 v[i].sh_addr += 1380 (Addr)(uintptr_t)saddr; 1381 v[i].sh_addralign = 1382 symtab->sh_addralign; 1383 *doffsetp = roundup(*doffsetp, 1384 v[i].sh_addralign); 1385 v[i].sh_offset = *doffsetp; 1386 v[i].sh_size = symtab->sh_size; 1387 v[i].sh_link = i + 1; 1388 v[i].sh_entsize = symtab->sh_entsize; 1389 v[i].sh_info = symtab->sh_info; 1390 1391 copy_scn(symtab, mvp, &v[i], vp, 1392 doffsetp, data, datasz, credp, 1393 rlimit); 1394 1395 v[i + 1].sh_type = SHT_STRTAB; 1396 v[i + 1].sh_flags = SHF_STRINGS; 1397 v[i + 1].sh_addr = symtab->sh_addr; 1398 if (ehdr.e_type == ET_DYN || 1399 v[i + 1].sh_addr == 0) 1400 v[i + 1].sh_addr += 1401 (Addr)(uintptr_t)saddr; 1402 v[i + 1].sh_addralign = 1403 strtab->sh_addralign; 1404 *doffsetp = roundup(*doffsetp, 1405 v[i + 1].sh_addralign); 1406 v[i + 1].sh_offset = *doffsetp; 1407 v[i + 1].sh_size = strtab->sh_size; 1408 1409 copy_scn(strtab, mvp, &v[i + 1], vp, 1410 doffsetp, data, datasz, credp, 1411 rlimit); 1412 } 1413 1414 if (symtab->sh_type == SHT_SYMTAB) 1415 symtab_ndx = i; 1416 i += 2; 1417 } 1418 } 1419 1420 kmem_free(shstrbase, shstrsize); 1421 kmem_free(shbase, shsize); 1422 1423 lastvp = mvp; 1424 } 1425 1426 if (v == NULL) { 1427 if (i == 1) 1428 *nshdrsp = 0; 1429 else 1430 *nshdrsp = i + 1; 1431 goto done; 1432 } 1433 1434 if (i != nshdrs - 1) { 1435 cmn_err(CE_WARN, "elfcore: core dump failed for " 1436 "process %d; address space is changing", p->p_pid); 1437 error = EIO; 1438 goto done; 1439 } 1440 1441 v[i].sh_name = shstrtab_ndx(&shstrtab, STR_SHSTRTAB); 1442 v[i].sh_size = shstrtab_size(&shstrtab); 1443 v[i].sh_addralign = 1; 1444 *doffsetp = roundup(*doffsetp, v[i].sh_addralign); 1445 v[i].sh_offset = *doffsetp; 1446 v[i].sh_flags = SHF_STRINGS; 1447 v[i].sh_type = SHT_STRTAB; 1448 1449 if (v[i].sh_size > datasz) { 1450 if (data != NULL) 1451 kmem_free(data, datasz); 1452 1453 datasz = v[i].sh_size; 1454 data = kmem_alloc(datasz, 1455 KM_SLEEP); 1456 } 1457 1458 shstrtab_dump(&shstrtab, data); 1459 1460 if ((error = core_write(vp, UIO_SYSSPACE, *doffsetp, 1461 data, v[i].sh_size, rlimit, credp)) != 0) 1462 goto done; 1463 1464 *doffsetp += v[i].sh_size; 1465 1466 done: 1467 if (data != NULL) 1468 kmem_free(data, datasz); 1469 1470 return (error); 1471 } 1472 1473 int 1474 elfcore(vnode_t *vp, proc_t *p, cred_t *credp, rlim64_t rlimit, int sig, 1475 core_content_t content) 1476 { 1477 offset_t poffset, soffset; 1478 Off doffset; 1479 int error, i, nphdrs, nshdrs; 1480 int overflow = 0; 1481 struct seg *seg; 1482 struct as *as = p->p_as; 1483 union { 1484 Ehdr ehdr; 1485 Phdr phdr[1]; 1486 Shdr shdr[1]; 1487 } *bigwad; 1488 size_t bigsize; 1489 size_t phdrsz, shdrsz; 1490 Ehdr *ehdr; 1491 Phdr *v; 1492 caddr_t brkbase; 1493 size_t brksize; 1494 caddr_t stkbase; 1495 size_t stksize; 1496 int ntries = 0; 1497 1498 top: 1499 /* 1500 * Make sure we have everything we need (registers, etc.). 1501 * All other lwps have already stopped and are in an orderly state. 1502 */ 1503 ASSERT(p == ttoproc(curthread)); 1504 prstop(0, 0); 1505 1506 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1507 nphdrs = prnsegs(as, 0) + 2; /* two CORE note sections */ 1508 1509 /* 1510 * Count the number of section headers we're going to need. 1511 */ 1512 nshdrs = 0; 1513 if (content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)) { 1514 (void) process_scns(content, p, credp, NULL, NULL, NULL, 0, 1515 NULL, &nshdrs); 1516 } 1517 AS_LOCK_EXIT(as, &as->a_lock); 1518 1519 ASSERT(nshdrs == 0 || nshdrs > 1); 1520 1521 /* 1522 * The core file contents may required zero section headers, but if 1523 * we overflow the 16 bits allotted to the program header count in 1524 * the ELF header, we'll need that program header at index zero. 1525 */ 1526 if (nshdrs == 0 && nphdrs >= PN_XNUM) 1527 nshdrs = 1; 1528 1529 phdrsz = nphdrs * sizeof (Phdr); 1530 shdrsz = nshdrs * sizeof (Shdr); 1531 1532 bigsize = MAX(sizeof (*bigwad), MAX(phdrsz, shdrsz)); 1533 bigwad = kmem_alloc(bigsize, KM_SLEEP); 1534 1535 ehdr = &bigwad->ehdr; 1536 bzero(ehdr, sizeof (*ehdr)); 1537 1538 ehdr->e_ident[EI_MAG0] = ELFMAG0; 1539 ehdr->e_ident[EI_MAG1] = ELFMAG1; 1540 ehdr->e_ident[EI_MAG2] = ELFMAG2; 1541 ehdr->e_ident[EI_MAG3] = ELFMAG3; 1542 ehdr->e_ident[EI_CLASS] = ELFCLASS; 1543 ehdr->e_type = ET_CORE; 1544 1545 #if !defined(_LP64) || defined(_ELF32_COMPAT) 1546 1547 #if defined(__sparc) 1548 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 1549 ehdr->e_machine = EM_SPARC; 1550 #elif defined(__i386) || defined(__i386_COMPAT) 1551 ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 1552 ehdr->e_machine = EM_386; 1553 #else 1554 #error "no recognized machine type is defined" 1555 #endif 1556 1557 #else /* !defined(_LP64) || defined(_ELF32_COMPAT) */ 1558 1559 #if defined(__sparc) 1560 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 1561 ehdr->e_machine = EM_SPARCV9; 1562 #elif defined(__amd64) 1563 ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 1564 ehdr->e_machine = EM_AMD64; 1565 #else 1566 #error "no recognized 64-bit machine type is defined" 1567 #endif 1568 1569 #endif /* !defined(_LP64) || defined(_ELF32_COMPAT) */ 1570 1571 /* 1572 * If the count of program headers or section headers or the index 1573 * of the section string table can't fit in the mere 16 bits 1574 * shortsightedly allotted to them in the ELF header, we use the 1575 * extended formats and put the real values in the section header 1576 * as index 0. 1577 */ 1578 ehdr->e_version = EV_CURRENT; 1579 ehdr->e_ehsize = sizeof (Ehdr); 1580 1581 if (nphdrs >= PN_XNUM) 1582 ehdr->e_phnum = PN_XNUM; 1583 else 1584 ehdr->e_phnum = (unsigned short)nphdrs; 1585 1586 ehdr->e_phoff = sizeof (Ehdr); 1587 ehdr->e_phentsize = sizeof (Phdr); 1588 1589 if (nshdrs > 0) { 1590 if (nshdrs >= SHN_LORESERVE) 1591 ehdr->e_shnum = 0; 1592 else 1593 ehdr->e_shnum = (unsigned short)nshdrs; 1594 1595 if (nshdrs - 1 >= SHN_LORESERVE) 1596 ehdr->e_shstrndx = SHN_XINDEX; 1597 else 1598 ehdr->e_shstrndx = (unsigned short)(nshdrs - 1); 1599 1600 ehdr->e_shoff = ehdr->e_phoff + ehdr->e_phentsize * nphdrs; 1601 ehdr->e_shentsize = sizeof (Shdr); 1602 } 1603 1604 if (error = core_write(vp, UIO_SYSSPACE, (offset_t)0, ehdr, 1605 sizeof (Ehdr), rlimit, credp)) 1606 goto done; 1607 1608 poffset = sizeof (Ehdr); 1609 soffset = sizeof (Ehdr) + phdrsz; 1610 doffset = sizeof (Ehdr) + phdrsz + shdrsz; 1611 1612 v = &bigwad->phdr[0]; 1613 bzero(v, phdrsz); 1614 1615 setup_old_note_header(&v[0], p); 1616 v[0].p_offset = doffset = roundup(doffset, sizeof (Word)); 1617 doffset += v[0].p_filesz; 1618 1619 setup_note_header(&v[1], p); 1620 v[1].p_offset = doffset = roundup(doffset, sizeof (Word)); 1621 doffset += v[1].p_filesz; 1622 1623 mutex_enter(&p->p_lock); 1624 1625 brkbase = p->p_brkbase; 1626 brksize = p->p_brksize; 1627 1628 stkbase = p->p_usrstack - p->p_stksize; 1629 stksize = p->p_stksize; 1630 1631 mutex_exit(&p->p_lock); 1632 1633 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1634 i = 2; 1635 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 1636 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 1637 caddr_t saddr, naddr; 1638 void *tmp = NULL; 1639 extern struct seg_ops segspt_shmops; 1640 1641 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1642 uint_t prot; 1643 size_t size; 1644 int type; 1645 vnode_t *mvp; 1646 1647 prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 1648 prot &= PROT_READ | PROT_WRITE | PROT_EXEC; 1649 if ((size = (size_t)(naddr - saddr)) == 0) 1650 continue; 1651 if (i == nphdrs) { 1652 overflow++; 1653 continue; 1654 } 1655 v[i].p_type = PT_LOAD; 1656 v[i].p_vaddr = (Addr)(uintptr_t)saddr; 1657 v[i].p_memsz = size; 1658 if (prot & PROT_READ) 1659 v[i].p_flags |= PF_R; 1660 if (prot & PROT_WRITE) 1661 v[i].p_flags |= PF_W; 1662 if (prot & PROT_EXEC) 1663 v[i].p_flags |= PF_X; 1664 1665 /* 1666 * Figure out which mappings to include in the core. 1667 */ 1668 type = SEGOP_GETTYPE(seg, saddr); 1669 1670 if (saddr == stkbase && size == stksize) { 1671 if (!(content & CC_CONTENT_STACK)) 1672 goto exclude; 1673 1674 } else if (saddr == brkbase && size == brksize) { 1675 if (!(content & CC_CONTENT_HEAP)) 1676 goto exclude; 1677 1678 } else if (seg->s_ops == &segspt_shmops) { 1679 if (type & MAP_NORESERVE) { 1680 if (!(content & CC_CONTENT_DISM)) 1681 goto exclude; 1682 } else { 1683 if (!(content & CC_CONTENT_ISM)) 1684 goto exclude; 1685 } 1686 1687 } else if (seg->s_ops != &segvn_ops) { 1688 goto exclude; 1689 1690 } else if (type & MAP_SHARED) { 1691 if (shmgetid(p, saddr) != SHMID_NONE) { 1692 if (!(content & CC_CONTENT_SHM)) 1693 goto exclude; 1694 1695 } else if (SEGOP_GETVP(seg, seg->s_base, 1696 &mvp) != 0 || mvp == NULL || 1697 mvp->v_type != VREG) { 1698 if (!(content & CC_CONTENT_SHANON)) 1699 goto exclude; 1700 1701 } else { 1702 if (!(content & CC_CONTENT_SHFILE)) 1703 goto exclude; 1704 } 1705 1706 } else if (SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 || 1707 mvp == NULL || mvp->v_type != VREG) { 1708 if (!(content & CC_CONTENT_ANON)) 1709 goto exclude; 1710 1711 } else if (prot == (PROT_READ | PROT_EXEC)) { 1712 if (!(content & CC_CONTENT_TEXT)) 1713 goto exclude; 1714 1715 } else if (prot == PROT_READ) { 1716 if (!(content & CC_CONTENT_RODATA)) 1717 goto exclude; 1718 1719 } else { 1720 if (!(content & CC_CONTENT_DATA)) 1721 goto exclude; 1722 } 1723 1724 doffset = roundup(doffset, sizeof (Word)); 1725 v[i].p_offset = doffset; 1726 v[i].p_filesz = size; 1727 doffset += size; 1728 exclude: 1729 i++; 1730 } 1731 ASSERT(tmp == NULL); 1732 } 1733 AS_LOCK_EXIT(as, &as->a_lock); 1734 1735 if (overflow || i != nphdrs) { 1736 if (ntries++ == 0) { 1737 kmem_free(bigwad, bigsize); 1738 goto top; 1739 } 1740 cmn_err(CE_WARN, "elfcore: core dump failed for " 1741 "process %d; address space is changing", p->p_pid); 1742 error = EIO; 1743 goto done; 1744 } 1745 1746 if ((error = core_write(vp, UIO_SYSSPACE, poffset, 1747 v, phdrsz, rlimit, credp)) != 0) 1748 goto done; 1749 1750 if ((error = write_old_elfnotes(p, sig, vp, v[0].p_offset, rlimit, 1751 credp)) != 0) 1752 goto done; 1753 1754 if ((error = write_elfnotes(p, sig, vp, v[1].p_offset, rlimit, 1755 credp, content)) != 0) 1756 goto done; 1757 1758 for (i = 2; i < nphdrs; i++) { 1759 if (v[i].p_filesz == 0) 1760 continue; 1761 1762 /* 1763 * If dumping out this segment fails, rather than failing 1764 * the core dump entirely, we reset the size of the mapping 1765 * to zero to indicate that the data is absent from the core 1766 * file and or in the PF_SUNW_FAILURE flag to differentiate 1767 * this from mappings that were excluded due to the core file 1768 * content settings. 1769 */ 1770 if ((error = core_seg(p, vp, v[i].p_offset, 1771 (caddr_t)(uintptr_t)v[i].p_vaddr, v[i].p_filesz, 1772 rlimit, credp)) != 0) { 1773 1774 /* 1775 * Since the space reserved for the segment is now 1776 * unused, we stash the errno in the first four 1777 * bytes. This undocumented interface will let us 1778 * understand the nature of the failure. 1779 */ 1780 (void) core_write(vp, UIO_SYSSPACE, v[i].p_offset, 1781 &error, sizeof (error), rlimit, credp); 1782 1783 v[i].p_filesz = 0; 1784 v[i].p_flags |= PF_SUNW_FAILURE; 1785 if ((error = core_write(vp, UIO_SYSSPACE, 1786 poffset + sizeof (v[i]) * i, &v[i], sizeof (v[i]), 1787 rlimit, credp)) != 0) 1788 goto done; 1789 } 1790 } 1791 1792 if (nshdrs > 0) { 1793 bzero(&bigwad->shdr[0], shdrsz); 1794 1795 if (nshdrs >= SHN_LORESERVE) 1796 bigwad->shdr[0].sh_size = nshdrs; 1797 1798 if (nshdrs - 1 >= SHN_LORESERVE) 1799 bigwad->shdr[0].sh_link = nshdrs - 1; 1800 1801 if (nphdrs >= PN_XNUM) 1802 bigwad->shdr[0].sh_info = nphdrs; 1803 1804 if (nshdrs > 1) { 1805 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1806 if ((error = process_scns(content, p, credp, vp, 1807 &bigwad->shdr[0], nshdrs, rlimit, &doffset, 1808 NULL)) != 0) { 1809 AS_LOCK_EXIT(as, &as->a_lock); 1810 goto done; 1811 } 1812 AS_LOCK_EXIT(as, &as->a_lock); 1813 } 1814 1815 if ((error = core_write(vp, UIO_SYSSPACE, soffset, 1816 &bigwad->shdr[0], shdrsz, rlimit, credp)) != 0) 1817 goto done; 1818 } 1819 1820 done: 1821 kmem_free(bigwad, bigsize); 1822 return (error); 1823 } 1824 1825 #ifndef _ELF32_COMPAT 1826 1827 static struct execsw esw = { 1828 #ifdef _LP64 1829 elf64magicstr, 1830 #else /* _LP64 */ 1831 elf32magicstr, 1832 #endif /* _LP64 */ 1833 0, 1834 5, 1835 elfexec, 1836 elfcore 1837 }; 1838 1839 static struct modlexec modlexec = { 1840 &mod_execops, "exec module for elf", &esw 1841 }; 1842 1843 #ifdef _LP64 1844 extern int elf32exec(vnode_t *vp, execa_t *uap, uarg_t *args, 1845 intpdata_t *idatap, int level, long *execsz, 1846 int setid, caddr_t exec_file, cred_t *cred); 1847 extern int elf32core(vnode_t *vp, proc_t *p, cred_t *credp, 1848 rlim64_t rlimit, int sig, core_content_t content); 1849 1850 static struct execsw esw32 = { 1851 elf32magicstr, 1852 0, 1853 5, 1854 elf32exec, 1855 elf32core 1856 }; 1857 1858 static struct modlexec modlexec32 = { 1859 &mod_execops, "32-bit exec module for elf", &esw32 1860 }; 1861 #endif /* _LP64 */ 1862 1863 static struct modlinkage modlinkage = { 1864 MODREV_1, 1865 (void *)&modlexec, 1866 #ifdef _LP64 1867 (void *)&modlexec32, 1868 #endif /* _LP64 */ 1869 NULL 1870 }; 1871 1872 int 1873 _init(void) 1874 { 1875 return (mod_install(&modlinkage)); 1876 } 1877 1878 int 1879 _fini(void) 1880 { 1881 return (mod_remove(&modlinkage)); 1882 } 1883 1884 int 1885 _info(struct modinfo *modinfop) 1886 { 1887 return (mod_info(&modlinkage, modinfop)); 1888 } 1889 1890 #endif /* !_ELF32_COMPAT */ 1891