1 /* $NetBSD: exec_elf.c,v 1.85 2016/05/25 17:25:32 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas and Maxime Villard. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1996 Christopher G. Demetriou 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. The name of the author may not be used to endorse or promote products 45 * derived from this software without specific prior written permission 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 #include <sys/cdefs.h> 60 __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.85 2016/05/25 17:25:32 christos Exp $"); 61 62 #ifdef _KERNEL_OPT 63 #include "opt_pax.h" 64 #endif /* _KERNEL_OPT */ 65 66 #include <sys/param.h> 67 #include <sys/proc.h> 68 #include <sys/kmem.h> 69 #include <sys/namei.h> 70 #include <sys/vnode.h> 71 #include <sys/exec.h> 72 #include <sys/exec_elf.h> 73 #include <sys/syscall.h> 74 #include <sys/signalvar.h> 75 #include <sys/mount.h> 76 #include <sys/stat.h> 77 #include <sys/kauth.h> 78 #include <sys/bitops.h> 79 80 #include <sys/cpu.h> 81 #include <machine/reg.h> 82 83 #include <compat/common/compat_util.h> 84 85 #include <sys/pax.h> 86 #include <uvm/uvm_param.h> 87 88 extern struct emul emul_netbsd; 89 90 #define elf_check_header ELFNAME(check_header) 91 #define elf_copyargs ELFNAME(copyargs) 92 #define elf_load_interp ELFNAME(load_interp) 93 #define elf_load_psection ELFNAME(load_psection) 94 #define exec_elf_makecmds ELFNAME2(exec,makecmds) 95 #define netbsd_elf_signature ELFNAME2(netbsd,signature) 96 #define netbsd_elf_probe ELFNAME2(netbsd,probe) 97 #define coredump ELFNAMEEND(coredump) 98 #define elf_free_emul_arg ELFNAME(free_emul_arg) 99 100 static int 101 elf_load_interp(struct lwp *, struct exec_package *, char *, 102 struct exec_vmcmd_set *, u_long *, Elf_Addr *); 103 static void 104 elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *, 105 Elf_Addr *, u_long *, int); 106 107 int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *); 108 int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *, 109 vaddr_t *); 110 111 static void elf_free_emul_arg(void *); 112 113 /* round up and down to page boundaries. */ 114 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 115 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 116 117 static void 118 elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph) 119 { 120 Elf_Addr align, offset; 121 int i; 122 123 for (align = i = 0; i < eh->e_phnum; i++) 124 if (ph[i].p_type == PT_LOAD && ph[i].p_align > align) 125 align = ph[i].p_align; 126 127 offset = (Elf_Addr)pax_aslr_exec_offset(epp, align); 128 offset += epp->ep_vm_minaddr; 129 130 for (i = 0; i < eh->e_phnum; i++) 131 ph[i].p_vaddr += offset; 132 epp->ep_entryoffset = offset; 133 eh->e_entry += offset; 134 } 135 136 /* 137 * Copy arguments onto the stack in the normal way, but add some 138 * extra information in case of dynamic binding. 139 */ 140 int 141 elf_copyargs(struct lwp *l, struct exec_package *pack, 142 struct ps_strings *arginfo, char **stackp, void *argp) 143 { 144 size_t len, vlen; 145 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname; 146 struct elf_args *ap; 147 int error; 148 149 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 150 return error; 151 152 a = ai; 153 execname = NULL; 154 155 memset(ai, 0, sizeof(ai)); 156 157 /* 158 * Push extra arguments on the stack needed by dynamically 159 * linked binaries 160 */ 161 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 162 struct vattr *vap = pack->ep_vap; 163 164 a->a_type = AT_PHDR; 165 a->a_v = ap->arg_phaddr; 166 a++; 167 168 a->a_type = AT_PHENT; 169 a->a_v = ap->arg_phentsize; 170 a++; 171 172 a->a_type = AT_PHNUM; 173 a->a_v = ap->arg_phnum; 174 a++; 175 176 a->a_type = AT_PAGESZ; 177 a->a_v = PAGE_SIZE; 178 a++; 179 180 a->a_type = AT_BASE; 181 a->a_v = ap->arg_interp; 182 a++; 183 184 a->a_type = AT_FLAGS; 185 a->a_v = 0; 186 a++; 187 188 a->a_type = AT_ENTRY; 189 a->a_v = ap->arg_entry; 190 a++; 191 192 a->a_type = AT_EUID; 193 if (vap->va_mode & S_ISUID) 194 a->a_v = vap->va_uid; 195 else 196 a->a_v = kauth_cred_geteuid(l->l_cred); 197 a++; 198 199 a->a_type = AT_RUID; 200 a->a_v = kauth_cred_getuid(l->l_cred); 201 a++; 202 203 a->a_type = AT_EGID; 204 if (vap->va_mode & S_ISGID) 205 a->a_v = vap->va_gid; 206 else 207 a->a_v = kauth_cred_getegid(l->l_cred); 208 a++; 209 210 a->a_type = AT_RGID; 211 a->a_v = kauth_cred_getgid(l->l_cred); 212 a++; 213 214 a->a_type = AT_STACKBASE; 215 a->a_v = l->l_proc->p_stackbase; 216 a++; 217 218 if (pack->ep_path) { 219 execname = a; 220 a->a_type = AT_SUN_EXECNAME; 221 a++; 222 } 223 224 exec_free_emul_arg(pack); 225 } 226 227 a->a_type = AT_NULL; 228 a->a_v = 0; 229 a++; 230 231 vlen = (a - ai) * sizeof(ai[0]); 232 233 KASSERT(vlen <= sizeof(ai)); 234 235 if (execname) { 236 char *path = pack->ep_path; 237 execname->a_v = (uintptr_t)(*stackp + vlen); 238 len = strlen(path) + 1; 239 if ((error = copyout(path, (*stackp + vlen), len)) != 0) 240 return error; 241 len = ALIGN(len); 242 } else 243 len = 0; 244 245 if ((error = copyout(ai, *stackp, vlen)) != 0) 246 return error; 247 *stackp += vlen + len; 248 249 return 0; 250 } 251 252 /* 253 * elf_check_header(): 254 * 255 * Check header for validity; return 0 if ok, ENOEXEC if error 256 */ 257 int 258 elf_check_header(Elf_Ehdr *eh) 259 { 260 261 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 262 eh->e_ident[EI_CLASS] != ELFCLASS) 263 return ENOEXEC; 264 265 switch (eh->e_machine) { 266 267 ELFDEFNNAME(MACHDEP_ID_CASES) 268 269 default: 270 return ENOEXEC; 271 } 272 273 if (ELF_EHDR_FLAGS_OK(eh) == 0) 274 return ENOEXEC; 275 276 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) 277 return ENOEXEC; 278 279 return 0; 280 } 281 282 /* 283 * elf_load_psection(): 284 * 285 * Load a psection at the appropriate address 286 */ 287 static void 288 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, 289 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags) 290 { 291 u_long msize, psize, rm, rf; 292 long diff, offset; 293 int vmprot = 0; 294 295 /* 296 * If the user specified an address, then we load there. 297 */ 298 if (*addr == ELFDEFNNAME(NO_ADDR)) 299 *addr = ph->p_vaddr; 300 301 if (ph->p_align > 1) { 302 /* 303 * Make sure we are virtually aligned as we are supposed to be. 304 */ 305 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 306 KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 307 /* 308 * But make sure to not map any pages before the start of the 309 * psection by limiting the difference to within a page. 310 */ 311 diff &= PAGE_MASK; 312 } else 313 diff = 0; 314 315 vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 316 vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 317 vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 318 319 /* 320 * Adjust everything so it all starts on a page boundary. 321 */ 322 *addr -= diff; 323 offset = ph->p_offset - diff; 324 *size = ph->p_filesz + diff; 325 msize = ph->p_memsz + diff; 326 327 if (ph->p_align >= PAGE_SIZE) { 328 if ((ph->p_flags & PF_W) != 0) { 329 /* 330 * Because the pagedvn pager can't handle zero fill 331 * of the last data page if it's not page aligned we 332 * map the last page readvn. 333 */ 334 psize = trunc_page(*size); 335 } else { 336 psize = round_page(*size); 337 } 338 } else { 339 psize = *size; 340 } 341 342 if (psize > 0) { 343 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 344 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 345 offset, vmprot, flags); 346 flags &= VMCMD_RELATIVE; 347 } 348 if (psize < *size) { 349 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 350 *addr + psize, vp, offset + psize, vmprot, flags); 351 } 352 353 /* 354 * Check if we need to extend the size of the segment (does 355 * bss extend page the next page boundary)? 356 */ 357 rm = round_page(*addr + msize); 358 rf = round_page(*addr + *size); 359 360 if (rm != rf) { 361 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 362 0, vmprot, flags & VMCMD_RELATIVE); 363 *size = msize; 364 } 365 } 366 367 /* 368 * elf_load_interp(): 369 * 370 * Load an interpreter pointed to by path. 371 */ 372 static int 373 elf_load_interp(struct lwp *l, struct exec_package *epp, char *path, 374 struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last) 375 { 376 int error, i; 377 struct vnode *vp; 378 struct vattr attr; 379 Elf_Ehdr eh; 380 Elf_Phdr *ph = NULL; 381 const Elf_Phdr *base_ph; 382 const Elf_Phdr *last_ph; 383 u_long phsize; 384 Elf_Addr addr = *last; 385 struct proc *p; 386 bool use_topdown; 387 388 p = l->l_proc; 389 390 KASSERT(p->p_vmspace); 391 KASSERT(p->p_vmspace != proc0.p_vmspace); 392 393 #ifdef __USE_TOPDOWN_VM 394 use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM; 395 #else 396 use_topdown = false; 397 #endif 398 399 /* 400 * 1. open file 401 * 2. read filehdr 402 * 3. map text, data, and bss out of it using VM_* 403 */ 404 vp = epp->ep_interp; 405 if (vp == NULL) { 406 error = emul_find_interp(l, epp, path); 407 if (error != 0) 408 return error; 409 vp = epp->ep_interp; 410 } 411 /* We'll tidy this ourselves - otherwise we have locking issues */ 412 epp->ep_interp = NULL; 413 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 414 415 /* 416 * Similarly, if it's not marked as executable, or it's not a regular 417 * file, we don't allow it to be used. 418 */ 419 if (vp->v_type != VREG) { 420 error = EACCES; 421 goto badunlock; 422 } 423 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 424 goto badunlock; 425 426 /* get attributes */ 427 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0) 428 goto badunlock; 429 430 /* 431 * Check mount point. Though we're not trying to exec this binary, 432 * we will be executing code from it, so if the mount point 433 * disallows execution or set-id-ness, we punt or kill the set-id. 434 */ 435 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 436 error = EACCES; 437 goto badunlock; 438 } 439 if (vp->v_mount->mnt_flag & MNT_NOSUID) 440 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 441 442 error = vn_marktext(vp); 443 if (error) 444 goto badunlock; 445 446 VOP_UNLOCK(vp); 447 448 if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 449 goto bad; 450 451 if ((error = elf_check_header(&eh)) != 0) 452 goto bad; 453 if (eh.e_type != ET_DYN || eh.e_phnum == 0) { 454 error = ENOEXEC; 455 goto bad; 456 } 457 458 phsize = eh.e_phnum * sizeof(Elf_Phdr); 459 ph = kmem_alloc(phsize, KM_SLEEP); 460 461 if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 462 goto bad; 463 464 #ifdef ELF_INTERP_NON_RELOCATABLE 465 /* 466 * Evil hack: Only MIPS should be non-relocatable, and the 467 * psections should have a high address (typically 0x5ffe0000). 468 * If it's now relocatable, it should be linked at 0 and the 469 * psections should have zeros in the upper part of the address. 470 * Otherwise, force the load at the linked address. 471 */ 472 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 473 *last = ELFDEFNNAME(NO_ADDR); 474 #endif 475 476 /* 477 * If no position to load the interpreter was set by a probe 478 * function, pick the same address that a non-fixed mmap(0, ..) 479 * would (i.e. something safely out of the way). 480 */ 481 if (*last == ELFDEFNNAME(NO_ADDR)) { 482 u_long limit = 0; 483 /* 484 * Find the start and ending addresses of the psections to 485 * be loaded. This will give us the size. 486 */ 487 for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) { 488 if (ph[i].p_type == PT_LOAD) { 489 u_long psize = ph[i].p_vaddr + ph[i].p_memsz; 490 if (base_ph == NULL) 491 base_ph = &ph[i]; 492 if (psize > limit) 493 limit = psize; 494 } 495 } 496 497 if (base_ph == NULL) { 498 error = ENOEXEC; 499 goto bad; 500 } 501 502 /* 503 * Now compute the size and load address. 504 */ 505 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 506 epp->ep_daddr, 507 round_page(limit) - trunc_page(base_ph->p_vaddr), 508 use_topdown); 509 addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align, 510 use_topdown); 511 } else { 512 addr = *last; /* may be ELF_LINK_ADDR */ 513 } 514 515 /* 516 * Load all the necessary sections 517 */ 518 for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) { 519 switch (ph[i].p_type) { 520 case PT_LOAD: { 521 u_long size; 522 int flags; 523 524 if (base_ph == NULL) { 525 /* 526 * First encountered psection is always the 527 * base psection. Make sure it's aligned 528 * properly (align down for topdown and align 529 * upwards for not topdown). 530 */ 531 base_ph = &ph[i]; 532 flags = VMCMD_BASE; 533 if (addr == ELF_LINK_ADDR) 534 addr = ph[i].p_vaddr; 535 if (use_topdown) 536 addr = ELF_TRUNC(addr, ph[i].p_align); 537 else 538 addr = ELF_ROUND(addr, ph[i].p_align); 539 } else { 540 u_long limit = round_page(last_ph->p_vaddr 541 + last_ph->p_memsz); 542 u_long base = trunc_page(ph[i].p_vaddr); 543 544 /* 545 * If there is a gap in between the psections, 546 * map it as inaccessible so nothing else 547 * mmap'ed will be placed there. 548 */ 549 if (limit != base) { 550 NEW_VMCMD2(vcset, vmcmd_map_zero, 551 base - limit, 552 limit - base_ph->p_vaddr, NULLVP, 553 0, VM_PROT_NONE, VMCMD_RELATIVE); 554 } 555 556 addr = ph[i].p_vaddr - base_ph->p_vaddr; 557 flags = VMCMD_RELATIVE; 558 } 559 last_ph = &ph[i]; 560 elf_load_psection(vcset, vp, &ph[i], &addr, 561 &size, flags); 562 /* 563 * If entry is within this psection then this 564 * must contain the .text section. *entryoff is 565 * relative to the base psection. 566 */ 567 if (eh.e_entry >= ph[i].p_vaddr && 568 eh.e_entry < (ph[i].p_vaddr + size)) { 569 *entryoff = eh.e_entry - base_ph->p_vaddr; 570 } 571 addr += size; 572 break; 573 } 574 575 default: 576 break; 577 } 578 } 579 580 kmem_free(ph, phsize); 581 /* 582 * This value is ignored if TOPDOWN. 583 */ 584 *last = addr; 585 vrele(vp); 586 return 0; 587 588 badunlock: 589 VOP_UNLOCK(vp); 590 591 bad: 592 if (ph != NULL) 593 kmem_free(ph, phsize); 594 vrele(vp); 595 return error; 596 } 597 598 /* 599 * exec_elf_makecmds(): Prepare an Elf binary's exec package 600 * 601 * First, set of the various offsets/lengths in the exec package. 602 * 603 * Then, mark the text image busy (so it can be demand paged) or error 604 * out if this is not possible. Finally, set up vmcmds for the 605 * text, data, bss, and stack segments. 606 */ 607 int 608 exec_elf_makecmds(struct lwp *l, struct exec_package *epp) 609 { 610 Elf_Ehdr *eh = epp->ep_hdr; 611 Elf_Phdr *ph, *pp; 612 Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0; 613 int error, i; 614 char *interp = NULL; 615 u_long phsize; 616 struct elf_args *ap; 617 bool is_dyn = false; 618 619 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 620 return ENOEXEC; 621 if ((error = elf_check_header(eh)) != 0) 622 return error; 623 624 if (eh->e_type == ET_DYN) 625 /* PIE, and some libs have an entry point */ 626 is_dyn = true; 627 else if (eh->e_type != ET_EXEC) 628 return ENOEXEC; 629 630 if (eh->e_phnum == 0) 631 return ENOEXEC; 632 633 error = vn_marktext(epp->ep_vp); 634 if (error) 635 return error; 636 637 /* 638 * Allocate space to hold all the program headers, and read them 639 * from the file 640 */ 641 phsize = eh->e_phnum * sizeof(Elf_Phdr); 642 ph = kmem_alloc(phsize, KM_SLEEP); 643 644 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 645 0) 646 goto bad; 647 648 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 649 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 650 651 for (i = 0; i < eh->e_phnum; i++) { 652 pp = &ph[i]; 653 if (pp->p_type == PT_INTERP) { 654 if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) { 655 error = ENOEXEC; 656 goto bad; 657 } 658 interp = PNBUF_GET(); 659 if ((error = exec_read_from(l, epp->ep_vp, 660 pp->p_offset, interp, pp->p_filesz)) != 0) 661 goto bad; 662 /* Ensure interp is NUL-terminated and of the expected length */ 663 if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) { 664 error = ENOEXEC; 665 goto bad; 666 } 667 break; 668 } 669 } 670 671 /* 672 * On the same architecture, we may be emulating different systems. 673 * See which one will accept this executable. 674 * 675 * Probe functions would normally see if the interpreter (if any) 676 * exists. Emulation packages may possibly replace the interpreter in 677 * interp with a changed path (/emul/xxx/<path>). 678 */ 679 pos = ELFDEFNNAME(NO_ADDR); 680 if (epp->ep_esch->u.elf_probe_func) { 681 vaddr_t startp = (vaddr_t)pos; 682 683 error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 684 &startp); 685 if (error) 686 goto bad; 687 pos = (Elf_Addr)startp; 688 } 689 690 if (is_dyn) 691 elf_placedynexec(epp, eh, ph); 692 693 /* 694 * Load all the necessary sections 695 */ 696 for (i = 0; i < eh->e_phnum; i++) { 697 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 698 u_long size = 0; 699 700 switch (ph[i].p_type) { 701 case PT_LOAD: 702 elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 703 &ph[i], &addr, &size, VMCMD_FIXED); 704 705 /* 706 * Consider this as text segment, if it is executable. 707 * If there is more than one text segment, pick the 708 * largest. 709 */ 710 if (ph[i].p_flags & PF_X) { 711 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) || 712 size > epp->ep_tsize) { 713 epp->ep_taddr = addr; 714 epp->ep_tsize = size; 715 } 716 end_text = addr + size; 717 } else { 718 epp->ep_daddr = addr; 719 epp->ep_dsize = size; 720 } 721 if (ph[i].p_offset == 0) { 722 computed_phdr = ph[i].p_vaddr + eh->e_phoff; 723 } 724 break; 725 726 case PT_SHLIB: 727 /* SCO has these sections. */ 728 case PT_INTERP: 729 /* Already did this one. */ 730 case PT_DYNAMIC: 731 case PT_NOTE: 732 break; 733 case PT_PHDR: 734 /* Note address of program headers (in text segment) */ 735 phdr = ph[i].p_vaddr; 736 break; 737 738 default: 739 /* 740 * Not fatal; we don't need to understand everything. 741 */ 742 break; 743 } 744 } 745 746 if (epp->ep_vmcmds.evs_used == 0) { 747 /* No VMCMD; there was no PT_LOAD section, or those 748 * sections were empty */ 749 error = ENOEXEC; 750 goto bad; 751 } 752 753 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 754 epp->ep_daddr = round_page(end_text); 755 epp->ep_dsize = 0; 756 } 757 758 /* 759 * Check if we found a dynamically linked binary and arrange to load 760 * its interpreter 761 */ 762 if (interp) { 763 u_int nused = epp->ep_vmcmds.evs_used; 764 u_long interp_offset = 0; 765 766 if ((error = elf_load_interp(l, epp, interp, 767 &epp->ep_vmcmds, &interp_offset, &pos)) != 0) { 768 goto bad; 769 } 770 if (epp->ep_vmcmds.evs_used == nused) { 771 /* elf_load_interp() has not set up any new VMCMD */ 772 error = ENOEXEC; 773 goto bad; 774 } 775 776 ap = kmem_alloc(sizeof(*ap), KM_SLEEP); 777 ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr; 778 epp->ep_entryoffset = interp_offset; 779 epp->ep_entry = ap->arg_interp + interp_offset; 780 PNBUF_PUT(interp); 781 interp = NULL; 782 } else { 783 epp->ep_entry = eh->e_entry; 784 if (epp->ep_flags & EXEC_FORCEAUX) { 785 ap = kmem_alloc(sizeof(*ap), KM_SLEEP); 786 ap->arg_interp = (vaddr_t)NULL; 787 } else 788 ap = NULL; 789 } 790 791 if (ap) { 792 ap->arg_phaddr = phdr ? phdr : computed_phdr; 793 ap->arg_phentsize = eh->e_phentsize; 794 ap->arg_phnum = eh->e_phnum; 795 ap->arg_entry = eh->e_entry; 796 epp->ep_emul_arg = ap; 797 epp->ep_emul_arg_free = elf_free_emul_arg; 798 } 799 800 #ifdef ELF_MAP_PAGE_ZERO 801 /* Dell SVR4 maps page zero, yeuch! */ 802 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 803 epp->ep_vp, 0, VM_PROT_READ); 804 #endif 805 806 error = (*epp->ep_esch->es_setup_stack)(l, epp); 807 if (error) 808 goto bad; 809 810 kmem_free(ph, phsize); 811 return 0; 812 813 bad: 814 if (interp) 815 PNBUF_PUT(interp); 816 exec_free_emul_arg(epp); 817 kmem_free(ph, phsize); 818 kill_vmcmds(&epp->ep_vmcmds); 819 return error; 820 } 821 822 int 823 netbsd_elf_signature(struct lwp *l, struct exec_package *epp, 824 Elf_Ehdr *eh) 825 { 826 size_t i; 827 Elf_Shdr *sh; 828 Elf_Nhdr *np; 829 size_t shsize, nsize; 830 int error; 831 int isnetbsd = 0; 832 char *ndata, *ndesc; 833 834 #ifdef DIAGNOSTIC 835 const char *badnote; 836 #define BADNOTE(n) badnote = (n) 837 #else 838 #define BADNOTE(n) 839 #endif 840 841 epp->ep_pax_flags = 0; 842 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_shnum == 0) 843 return ENOEXEC; 844 845 shsize = eh->e_shnum * sizeof(Elf_Shdr); 846 sh = kmem_alloc(shsize, KM_SLEEP); 847 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize); 848 if (error) 849 goto out; 850 851 np = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP); 852 for (i = 0; i < eh->e_shnum; i++) { 853 Elf_Shdr *shp = &sh[i]; 854 855 if (shp->sh_type != SHT_NOTE || 856 shp->sh_size > ELF_MAXNOTESIZE || 857 shp->sh_size < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 858 continue; 859 860 error = exec_read_from(l, epp->ep_vp, shp->sh_offset, np, 861 shp->sh_size); 862 if (error) 863 continue; 864 865 /* Point to the note, skip the header */ 866 ndata = (char *)(np + 1); 867 868 /* 869 * Padding is present if necessary to ensure 4-byte alignment. 870 * The actual section size is therefore: 871 * header size + 4-byte aligned name + 4-byte aligned desc 872 * Ensure this size is consistent with what is indicated 873 * in sh_size. The first check avoids integer overflows. 874 * 875 * Binaries from before NetBSD 1.6 have two notes in the same 876 * note section. The second note was never used, so as long as 877 * the section is at least as big as it should be, it's ok. 878 * These binaries also have a second note section with a note of 879 * type ELF_NOTE_TYPE_NETBSD_TAG, which can be ignored as well. 880 */ 881 if (np->n_namesz > shp->sh_size || np->n_descsz > shp->sh_size) { 882 BADNOTE("note size limit"); 883 goto bad; 884 } 885 nsize = sizeof(*np) + roundup(np->n_namesz, 4) + 886 roundup(np->n_descsz, 4); 887 if (nsize > shp->sh_size) { 888 BADNOTE("note size"); 889 goto bad; 890 } 891 ndesc = ndata + roundup(np->n_namesz, 4); 892 893 switch (np->n_type) { 894 case ELF_NOTE_TYPE_NETBSD_TAG: 895 /* It is us */ 896 if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ && 897 np->n_descsz == ELF_NOTE_NETBSD_DESCSZ && 898 memcmp(ndata, ELF_NOTE_NETBSD_NAME, 899 ELF_NOTE_NETBSD_NAMESZ) == 0) { 900 memcpy(&epp->ep_osversion, ndesc, 901 ELF_NOTE_NETBSD_DESCSZ); 902 isnetbsd = 1; 903 break; 904 } 905 906 /* 907 * Ignore SuSE tags; SuSE's n_type is the same the 908 * NetBSD one. 909 */ 910 if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ && 911 memcmp(ndata, ELF_NOTE_SUSE_NAME, 912 ELF_NOTE_SUSE_NAMESZ) == 0) 913 break; 914 /* 915 * Ignore old GCC 916 */ 917 if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ && 918 memcmp(ndata, ELF_NOTE_OGCC_NAME, 919 ELF_NOTE_OGCC_NAMESZ) == 0) 920 break; 921 BADNOTE("NetBSD tag"); 922 goto bad; 923 924 case ELF_NOTE_TYPE_PAX_TAG: 925 if (np->n_namesz == ELF_NOTE_PAX_NAMESZ && 926 np->n_descsz == ELF_NOTE_PAX_DESCSZ && 927 memcmp(ndata, ELF_NOTE_PAX_NAME, 928 ELF_NOTE_PAX_NAMESZ) == 0) { 929 uint32_t flags; 930 memcpy(&flags, ndesc, sizeof(flags)); 931 /* Convert the flags and insert them into 932 * the exec package. */ 933 pax_setup_elf_flags(epp, flags); 934 break; 935 } 936 BADNOTE("PaX tag"); 937 goto bad; 938 939 case ELF_NOTE_TYPE_MARCH_TAG: 940 /* Copy the machine arch into the package. */ 941 if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ 942 && memcmp(ndata, ELF_NOTE_MARCH_NAME, 943 ELF_NOTE_MARCH_NAMESZ) == 0) { 944 /* Do not truncate the buffer */ 945 if (np->n_descsz > sizeof(epp->ep_machine_arch)) { 946 BADNOTE("description size limit"); 947 goto bad; 948 } 949 /* 950 * Ensure ndesc is NUL-terminated and of the 951 * expected length. 952 */ 953 if (strnlen(ndesc, np->n_descsz) + 1 != 954 np->n_descsz) { 955 BADNOTE("description size"); 956 goto bad; 957 } 958 strlcpy(epp->ep_machine_arch, ndesc, 959 sizeof(epp->ep_machine_arch)); 960 break; 961 } 962 BADNOTE("march tag"); 963 goto bad; 964 965 case ELF_NOTE_TYPE_MCMODEL_TAG: 966 /* arch specific check for code model */ 967 #ifdef ELF_MD_MCMODEL_CHECK 968 if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ 969 && memcmp(ndata, ELF_NOTE_MCMODEL_NAME, 970 ELF_NOTE_MCMODEL_NAMESZ) == 0) { 971 ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz); 972 break; 973 } 974 BADNOTE("mcmodel tag"); 975 goto bad; 976 #endif 977 break; 978 979 case ELF_NOTE_TYPE_SUSE_VERSION_TAG: 980 break; 981 982 case ELF_NOTE_TYPE_GO_BUILDID_TAG: 983 break; 984 985 default: 986 BADNOTE("unknown tag"); 987 bad: 988 #ifdef DIAGNOSTIC 989 /* Ignore GNU tags */ 990 if (np->n_namesz == ELF_NOTE_GNU_NAMESZ && 991 memcmp(ndata, ELF_NOTE_GNU_NAME, 992 ELF_NOTE_GNU_NAMESZ) == 0) 993 break; 994 995 int ns = MIN(np->n_namesz, shp->sh_size - sizeof(*np)); 996 printf("%s: Unknown elf note type %d (%s): " 997 "[namesz=%d, descsz=%d name=%-*.*s]\n", 998 epp->ep_kname, np->n_type, badnote, np->n_namesz, 999 np->n_descsz, ns, ns, ndata); 1000 #endif 1001 break; 1002 } 1003 } 1004 kmem_free(np, ELF_MAXNOTESIZE); 1005 1006 error = isnetbsd ? 0 : ENOEXEC; 1007 out: 1008 kmem_free(sh, shsize); 1009 return error; 1010 } 1011 1012 int 1013 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 1014 vaddr_t *pos) 1015 { 1016 int error; 1017 1018 if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 1019 return error; 1020 #ifdef ELF_MD_PROBE_FUNC 1021 if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0) 1022 return error; 1023 #elif defined(ELF_INTERP_NON_RELOCATABLE) 1024 *pos = ELF_LINK_ADDR; 1025 #endif 1026 epp->ep_flags |= EXEC_FORCEAUX; 1027 return 0; 1028 } 1029 1030 void 1031 elf_free_emul_arg(void *arg) 1032 { 1033 struct elf_args *ap = arg; 1034 KASSERT(ap != NULL); 1035 kmem_free(ap, sizeof(*ap)); 1036 } 1037