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