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