1 /* $NetBSD: exec_elf.c,v 1.93 2017/11/07 19:44:04 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.93 2017/11/07 19:44:04 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_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 if (offset < epp->ep_vm_minaddr) 138 offset = roundup(epp->ep_vm_minaddr, align); 139 KASSERT((offset & (align - 1)) == 0); 140 141 for (i = 0; i < eh->e_phnum; i++) 142 ph[i].p_vaddr += offset; 143 epp->ep_entryoffset = offset; 144 eh->e_entry += offset; 145 } 146 147 /* 148 * Copy arguments onto the stack in the normal way, but add some 149 * extra information in case of dynamic binding. 150 */ 151 int 152 elf_copyargs(struct lwp *l, struct exec_package *pack, 153 struct ps_strings *arginfo, char **stackp, void *argp) 154 { 155 size_t len, vlen; 156 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname; 157 struct elf_args *ap; 158 int error; 159 160 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 161 return error; 162 163 a = ai; 164 execname = NULL; 165 166 memset(ai, 0, sizeof(ai)); 167 168 /* 169 * Push extra arguments on the stack needed by dynamically 170 * linked binaries 171 */ 172 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 173 struct vattr *vap = pack->ep_vap; 174 175 a->a_type = AT_PHDR; 176 a->a_v = ap->arg_phaddr; 177 a++; 178 179 a->a_type = AT_PHENT; 180 a->a_v = ap->arg_phentsize; 181 a++; 182 183 a->a_type = AT_PHNUM; 184 a->a_v = ap->arg_phnum; 185 a++; 186 187 a->a_type = AT_PAGESZ; 188 a->a_v = PAGE_SIZE; 189 a++; 190 191 a->a_type = AT_BASE; 192 a->a_v = ap->arg_interp; 193 a++; 194 195 a->a_type = AT_FLAGS; 196 a->a_v = 0; 197 a++; 198 199 a->a_type = AT_ENTRY; 200 a->a_v = ap->arg_entry; 201 a++; 202 203 a->a_type = AT_EUID; 204 if (vap->va_mode & S_ISUID) 205 a->a_v = vap->va_uid; 206 else 207 a->a_v = kauth_cred_geteuid(l->l_cred); 208 a++; 209 210 a->a_type = AT_RUID; 211 a->a_v = kauth_cred_getuid(l->l_cred); 212 a++; 213 214 a->a_type = AT_EGID; 215 if (vap->va_mode & S_ISGID) 216 a->a_v = vap->va_gid; 217 else 218 a->a_v = kauth_cred_getegid(l->l_cred); 219 a++; 220 221 a->a_type = AT_RGID; 222 a->a_v = kauth_cred_getgid(l->l_cred); 223 a++; 224 225 a->a_type = AT_STACKBASE; 226 a->a_v = l->l_proc->p_stackbase; 227 a++; 228 229 execname = a; 230 a->a_type = AT_SUN_EXECNAME; 231 a++; 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 char *path = l->l_proc->p_path; 245 execname->a_v = (uintptr_t)(*stackp + vlen); 246 len = strlen(path) + 1; 247 if ((error = copyout(path, (*stackp + vlen), len)) != 0) 248 return error; 249 len = ALIGN(len); 250 251 if ((error = copyout(ai, *stackp, vlen)) != 0) 252 return error; 253 *stackp += vlen + len; 254 255 return 0; 256 } 257 258 /* 259 * elf_check_header(): 260 * 261 * Check header for validity; return 0 if ok, ENOEXEC if error 262 */ 263 int 264 elf_check_header(Elf_Ehdr *eh) 265 { 266 267 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 268 eh->e_ident[EI_CLASS] != ELFCLASS) { 269 DPRINTF("bad magic e_ident[EI_MAG0,EI_MAG3] %#x%x%x%x, " 270 "e_ident[EI_CLASS] %#x", eh->e_ident[EI_MAG0], 271 eh->e_ident[EI_MAG1], eh->e_ident[EI_MAG2], 272 eh->e_ident[EI_MAG3], eh->e_ident[EI_CLASS]); 273 return ENOEXEC; 274 } 275 276 switch (eh->e_machine) { 277 278 ELFDEFNNAME(MACHDEP_ID_CASES) 279 280 default: 281 DPRINTF("bad machine %#x", eh->e_machine); 282 return ENOEXEC; 283 } 284 285 if (ELF_EHDR_FLAGS_OK(eh) == 0) { 286 DPRINTF("bad flags %#x", eh->e_flags); 287 return ENOEXEC; 288 } 289 290 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) { 291 DPRINTF("bad shnum/phnum %#x/%#x", eh->e_shnum, eh->e_phnum); 292 return ENOEXEC; 293 } 294 295 return 0; 296 } 297 298 /* 299 * elf_load_psection(): 300 * 301 * Load a psection at the appropriate address 302 */ 303 static void 304 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, 305 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags) 306 { 307 u_long msize, psize, rm, rf; 308 long diff, offset; 309 int vmprot = 0; 310 311 /* 312 * If the user specified an address, then we load there. 313 */ 314 if (*addr == ELFDEFNNAME(NO_ADDR)) 315 *addr = ph->p_vaddr; 316 317 if (ph->p_align > 1) { 318 /* 319 * Make sure we are virtually aligned as we are supposed to be. 320 */ 321 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 322 KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 323 /* 324 * But make sure to not map any pages before the start of the 325 * psection by limiting the difference to within a page. 326 */ 327 diff &= PAGE_MASK; 328 } else 329 diff = 0; 330 331 vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 332 vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 333 vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 334 335 /* 336 * Adjust everything so it all starts on a page boundary. 337 */ 338 *addr -= diff; 339 offset = ph->p_offset - diff; 340 *size = ph->p_filesz + diff; 341 msize = ph->p_memsz + diff; 342 343 if (ph->p_align >= PAGE_SIZE) { 344 if ((ph->p_flags & PF_W) != 0) { 345 /* 346 * Because the pagedvn pager can't handle zero fill 347 * of the last data page if it's not page aligned we 348 * map the last page readvn. 349 */ 350 psize = trunc_page(*size); 351 } else { 352 psize = round_page(*size); 353 } 354 } else { 355 psize = *size; 356 } 357 358 if (psize > 0) { 359 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 360 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 361 offset, vmprot, flags); 362 flags &= VMCMD_RELATIVE; 363 } 364 if (psize < *size) { 365 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 366 *addr + psize, vp, offset + psize, vmprot, flags); 367 } 368 369 /* 370 * Check if we need to extend the size of the segment (does 371 * bss extend page the next page boundary)? 372 */ 373 rm = round_page(*addr + msize); 374 rf = round_page(*addr + *size); 375 376 if (rm != rf) { 377 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 378 0, vmprot, flags & VMCMD_RELATIVE); 379 *size = msize; 380 } 381 } 382 383 /* 384 * elf_load_interp(): 385 * 386 * Load an interpreter pointed to by path. 387 */ 388 static int 389 elf_load_interp(struct lwp *l, struct exec_package *epp, char *path, 390 struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last) 391 { 392 int error, i; 393 struct vnode *vp; 394 struct vattr attr; 395 Elf_Ehdr eh; 396 Elf_Phdr *ph = NULL; 397 const Elf_Phdr *base_ph; 398 const Elf_Phdr *last_ph; 399 u_long phsize; 400 Elf_Addr addr = *last; 401 struct proc *p; 402 bool use_topdown; 403 404 p = l->l_proc; 405 406 KASSERT(p->p_vmspace); 407 KASSERT(p->p_vmspace != proc0.p_vmspace); 408 409 #ifdef __USE_TOPDOWN_VM 410 use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM; 411 #else 412 use_topdown = false; 413 #endif 414 415 /* 416 * 1. open file 417 * 2. read filehdr 418 * 3. map text, data, and bss out of it using VM_* 419 */ 420 vp = epp->ep_interp; 421 if (vp == NULL) { 422 error = emul_find_interp(l, epp, path); 423 if (error != 0) 424 return error; 425 vp = epp->ep_interp; 426 } 427 /* We'll tidy this ourselves - otherwise we have locking issues */ 428 epp->ep_interp = NULL; 429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 430 431 /* 432 * Similarly, if it's not marked as executable, or it's not a regular 433 * file, we don't allow it to be used. 434 */ 435 if (vp->v_type != VREG) { 436 error = EACCES; 437 goto badunlock; 438 } 439 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 440 goto badunlock; 441 442 /* get attributes */ 443 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0) 444 goto badunlock; 445 446 /* 447 * Check mount point. Though we're not trying to exec this binary, 448 * we will be executing code from it, so if the mount point 449 * disallows execution or set-id-ness, we punt or kill the set-id. 450 */ 451 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 452 error = EACCES; 453 goto badunlock; 454 } 455 if (vp->v_mount->mnt_flag & MNT_NOSUID) 456 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 457 458 error = vn_marktext(vp); 459 if (error) 460 goto badunlock; 461 462 VOP_UNLOCK(vp); 463 464 if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 465 goto bad; 466 467 if ((error = elf_check_header(&eh)) != 0) 468 goto bad; 469 if (eh.e_type != ET_DYN || eh.e_phnum == 0) { 470 DPRINTF("bad interpreter type %#x", eh.e_type); 471 error = ENOEXEC; 472 goto bad; 473 } 474 475 phsize = eh.e_phnum * sizeof(Elf_Phdr); 476 ph = kmem_alloc(phsize, KM_SLEEP); 477 478 if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 479 goto bad; 480 481 #ifdef ELF_INTERP_NON_RELOCATABLE 482 /* 483 * Evil hack: Only MIPS should be non-relocatable, and the 484 * psections should have a high address (typically 0x5ffe0000). 485 * If it's now relocatable, it should be linked at 0 and the 486 * psections should have zeros in the upper part of the address. 487 * Otherwise, force the load at the linked address. 488 */ 489 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 490 *last = ELFDEFNNAME(NO_ADDR); 491 #endif 492 493 /* 494 * If no position to load the interpreter was set by a probe 495 * function, pick the same address that a non-fixed mmap(0, ..) 496 * would (i.e. something safely out of the way). 497 */ 498 if (*last == ELFDEFNNAME(NO_ADDR)) { 499 u_long limit = 0; 500 /* 501 * Find the start and ending addresses of the psections to 502 * be loaded. This will give us the size. 503 */ 504 for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) { 505 if (ph[i].p_type == PT_LOAD) { 506 u_long psize = ph[i].p_vaddr + ph[i].p_memsz; 507 if (base_ph == NULL) 508 base_ph = &ph[i]; 509 if (psize > limit) 510 limit = psize; 511 } 512 } 513 514 if (base_ph == NULL) { 515 DPRINTF("no interpreter loadable sections"); 516 error = ENOEXEC; 517 goto bad; 518 } 519 520 /* 521 * Now compute the size and load address. 522 */ 523 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 524 epp->ep_daddr, 525 round_page(limit) - trunc_page(base_ph->p_vaddr), 526 use_topdown); 527 addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align, 528 use_topdown); 529 } else { 530 addr = *last; /* may be ELF_LINK_ADDR */ 531 } 532 533 /* 534 * Load all the necessary sections 535 */ 536 for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) { 537 switch (ph[i].p_type) { 538 case PT_LOAD: { 539 u_long size; 540 int flags; 541 542 if (base_ph == NULL) { 543 /* 544 * First encountered psection is always the 545 * base psection. Make sure it's aligned 546 * properly (align down for topdown and align 547 * upwards for not topdown). 548 */ 549 base_ph = &ph[i]; 550 flags = VMCMD_BASE; 551 if (addr == ELF_LINK_ADDR) 552 addr = ph[i].p_vaddr; 553 if (use_topdown) 554 addr = ELF_TRUNC(addr, ph[i].p_align); 555 else 556 addr = ELF_ROUND(addr, ph[i].p_align); 557 } else { 558 u_long limit = round_page(last_ph->p_vaddr 559 + last_ph->p_memsz); 560 u_long base = trunc_page(ph[i].p_vaddr); 561 562 /* 563 * If there is a gap in between the psections, 564 * map it as inaccessible so nothing else 565 * mmap'ed will be placed there. 566 */ 567 if (limit != base) { 568 NEW_VMCMD2(vcset, vmcmd_map_zero, 569 base - limit, 570 limit - base_ph->p_vaddr, NULLVP, 571 0, VM_PROT_NONE, VMCMD_RELATIVE); 572 } 573 574 addr = ph[i].p_vaddr - base_ph->p_vaddr; 575 flags = VMCMD_RELATIVE; 576 } 577 last_ph = &ph[i]; 578 elf_load_psection(vcset, vp, &ph[i], &addr, 579 &size, flags); 580 /* 581 * If entry is within this psection then this 582 * must contain the .text section. *entryoff is 583 * relative to the base psection. 584 */ 585 if (eh.e_entry >= ph[i].p_vaddr && 586 eh.e_entry < (ph[i].p_vaddr + size)) { 587 *entryoff = eh.e_entry - base_ph->p_vaddr; 588 } 589 addr += size; 590 break; 591 } 592 593 default: 594 break; 595 } 596 } 597 598 kmem_free(ph, phsize); 599 /* 600 * This value is ignored if TOPDOWN. 601 */ 602 *last = addr; 603 vrele(vp); 604 return 0; 605 606 badunlock: 607 VOP_UNLOCK(vp); 608 609 bad: 610 if (ph != NULL) 611 kmem_free(ph, phsize); 612 vrele(vp); 613 return error; 614 } 615 616 /* 617 * exec_elf_makecmds(): Prepare an Elf binary's exec package 618 * 619 * First, set of the various offsets/lengths in the exec package. 620 * 621 * Then, mark the text image busy (so it can be demand paged) or error 622 * out if this is not possible. Finally, set up vmcmds for the 623 * text, data, bss, and stack segments. 624 */ 625 int 626 exec_elf_makecmds(struct lwp *l, struct exec_package *epp) 627 { 628 Elf_Ehdr *eh = epp->ep_hdr; 629 Elf_Phdr *ph, *pp; 630 Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0; 631 int error, i; 632 char *interp = NULL; 633 u_long phsize; 634 struct elf_args *ap; 635 bool is_dyn = false; 636 637 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) { 638 DPRINTF("small header %#x", epp->ep_hdrvalid); 639 return ENOEXEC; 640 } 641 if ((error = elf_check_header(eh)) != 0) 642 return error; 643 644 if (eh->e_type == ET_DYN) 645 /* PIE, and some libs have an entry point */ 646 is_dyn = true; 647 else if (eh->e_type != ET_EXEC) { 648 DPRINTF("bad type %#x", eh->e_type); 649 return ENOEXEC; 650 } 651 652 if (eh->e_phnum == 0) { 653 DPRINTF("no program headers"); 654 return ENOEXEC; 655 } 656 657 error = vn_marktext(epp->ep_vp); 658 if (error) 659 return error; 660 661 /* 662 * Allocate space to hold all the program headers, and read them 663 * from the file 664 */ 665 phsize = eh->e_phnum * sizeof(Elf_Phdr); 666 ph = kmem_alloc(phsize, KM_SLEEP); 667 668 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 669 0) 670 goto bad; 671 672 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 673 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 674 675 for (i = 0; i < eh->e_phnum; i++) { 676 pp = &ph[i]; 677 if (pp->p_type == PT_INTERP) { 678 if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) { 679 DPRINTF("bad interpreter namelen %#jx", 680 (uintmax_t)pp->p_filesz); 681 error = ENOEXEC; 682 goto bad; 683 } 684 interp = PNBUF_GET(); 685 if ((error = exec_read_from(l, epp->ep_vp, 686 pp->p_offset, interp, pp->p_filesz)) != 0) 687 goto bad; 688 /* Ensure interp is NUL-terminated and of the expected length */ 689 if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) { 690 DPRINTF("bad interpreter name"); 691 error = ENOEXEC; 692 goto bad; 693 } 694 break; 695 } 696 } 697 698 /* 699 * On the same architecture, we may be emulating different systems. 700 * See which one will accept this executable. 701 * 702 * Probe functions would normally see if the interpreter (if any) 703 * exists. Emulation packages may possibly replace the interpreter in 704 * interp with a changed path (/emul/xxx/<path>). 705 */ 706 pos = ELFDEFNNAME(NO_ADDR); 707 if (epp->ep_esch->u.elf_probe_func) { 708 vaddr_t startp = (vaddr_t)pos; 709 710 error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 711 &startp); 712 if (error) 713 goto bad; 714 pos = (Elf_Addr)startp; 715 } 716 717 if (is_dyn) 718 elf_placedynexec(epp, eh, ph); 719 720 /* 721 * Load all the necessary sections 722 */ 723 for (i = 0; i < eh->e_phnum; i++) { 724 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 725 u_long size = 0; 726 727 switch (ph[i].p_type) { 728 case PT_LOAD: 729 elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 730 &ph[i], &addr, &size, VMCMD_FIXED); 731 732 /* 733 * Consider this as text segment, if it is executable. 734 * If there is more than one text segment, pick the 735 * largest. 736 */ 737 if (ph[i].p_flags & PF_X) { 738 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) || 739 size > epp->ep_tsize) { 740 epp->ep_taddr = addr; 741 epp->ep_tsize = size; 742 } 743 end_text = addr + size; 744 } else { 745 epp->ep_daddr = addr; 746 epp->ep_dsize = size; 747 } 748 if (ph[i].p_offset == 0) { 749 computed_phdr = ph[i].p_vaddr + eh->e_phoff; 750 } 751 break; 752 753 case PT_SHLIB: 754 /* SCO has these sections. */ 755 case PT_INTERP: 756 /* Already did this one. */ 757 case PT_DYNAMIC: 758 case PT_NOTE: 759 break; 760 case PT_PHDR: 761 /* Note address of program headers (in text segment) */ 762 phdr = ph[i].p_vaddr; 763 break; 764 765 default: 766 /* 767 * Not fatal; we don't need to understand everything. 768 */ 769 break; 770 } 771 } 772 773 if (epp->ep_vmcmds.evs_used == 0) { 774 /* No VMCMD; there was no PT_LOAD section, or those 775 * sections were empty */ 776 DPRINTF("no vmcommands"); 777 error = ENOEXEC; 778 goto bad; 779 } 780 781 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 782 epp->ep_daddr = round_page(end_text); 783 epp->ep_dsize = 0; 784 } 785 786 /* 787 * Check if we found a dynamically linked binary and arrange to load 788 * its interpreter 789 */ 790 if (interp) { 791 u_int nused = epp->ep_vmcmds.evs_used; 792 u_long interp_offset = 0; 793 794 if ((error = elf_load_interp(l, epp, interp, 795 &epp->ep_vmcmds, &interp_offset, &pos)) != 0) { 796 goto bad; 797 } 798 if (epp->ep_vmcmds.evs_used == nused) { 799 /* elf_load_interp() has not set up any new VMCMD */ 800 DPRINTF("no vmcommands for interpreter"); 801 error = ENOEXEC; 802 goto bad; 803 } 804 805 ap = kmem_alloc(sizeof(*ap), KM_SLEEP); 806 ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr; 807 epp->ep_entryoffset = interp_offset; 808 epp->ep_entry = ap->arg_interp + interp_offset; 809 PNBUF_PUT(interp); 810 interp = NULL; 811 } else { 812 epp->ep_entry = eh->e_entry; 813 if (epp->ep_flags & EXEC_FORCEAUX) { 814 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP); 815 ap->arg_interp = (vaddr_t)NULL; 816 } else { 817 ap = NULL; 818 } 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