1 /* $NetBSD: exec_elf.c,v 1.72 2015/04/27 09:19:58 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.72 2015/04/27 09:19:58 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 /* 650 * XXX allow for executing shared objects. It seems silly 651 * but other ELF-based systems allow it as well. 652 */ 653 is_dyn = true; 654 else if (eh->e_type != ET_EXEC) 655 return ENOEXEC; 656 657 if (eh->e_phnum == 0) 658 return ENOEXEC; 659 660 error = vn_marktext(epp->ep_vp); 661 if (error) 662 return error; 663 664 /* 665 * Allocate space to hold all the program headers, and read them 666 * from the file 667 */ 668 phsize = eh->e_phnum * sizeof(Elf_Phdr); 669 ph = kmem_alloc(phsize, KM_SLEEP); 670 671 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 672 0) 673 goto bad; 674 675 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 676 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 677 678 for (i = 0; i < eh->e_phnum; i++) { 679 pp = &ph[i]; 680 if (pp->p_type == PT_INTERP) { 681 if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) { 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 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 defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 718 l->l_proc->p_pax = epp->ep_pax_flags; 719 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 720 721 if (is_dyn) 722 elf_placedynexec(l, epp, eh, ph); 723 724 /* 725 * Load all the necessary sections 726 */ 727 for (i = 0; i < eh->e_phnum; i++) { 728 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 729 u_long size = 0; 730 731 switch (ph[i].p_type) { 732 case PT_LOAD: 733 elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 734 &ph[i], &addr, &size, VMCMD_FIXED); 735 736 /* 737 * Consider this as text segment, if it is executable. 738 * If there is more than one text segment, pick the 739 * largest. 740 */ 741 if (ph[i].p_flags & PF_X) { 742 if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) || 743 size > epp->ep_tsize) { 744 epp->ep_taddr = addr; 745 epp->ep_tsize = size; 746 } 747 end_text = addr + size; 748 } else { 749 epp->ep_daddr = addr; 750 epp->ep_dsize = size; 751 } 752 if (ph[i].p_offset == 0) { 753 computed_phdr = ph[i].p_vaddr + eh->e_phoff; 754 } 755 break; 756 757 case PT_SHLIB: 758 /* SCO has these sections. */ 759 case PT_INTERP: 760 /* Already did this one. */ 761 case PT_DYNAMIC: 762 case PT_NOTE: 763 break; 764 case PT_PHDR: 765 /* Note address of program headers (in text segment) */ 766 phdr = ph[i].p_vaddr; 767 break; 768 769 default: 770 /* 771 * Not fatal; we don't need to understand everything. 772 */ 773 break; 774 } 775 } 776 777 if (epp->ep_vmcmds.evs_used == 0) { 778 /* No VMCMD; there was no PT_LOAD section, or those 779 * sections were empty */ 780 error = ENOEXEC; 781 goto bad; 782 } 783 784 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 785 epp->ep_daddr = round_page(end_text); 786 epp->ep_dsize = 0; 787 } 788 789 /* 790 * Check if we found a dynamically linked binary and arrange to load 791 * its interpreter 792 */ 793 if (interp) { 794 u_int nused = epp->ep_vmcmds.evs_used; 795 u_long interp_offset = 0; 796 797 if ((error = elf_load_interp(l, epp, interp, 798 &epp->ep_vmcmds, &interp_offset, &pos)) != 0) { 799 goto bad; 800 } 801 if (epp->ep_vmcmds.evs_used == nused) { 802 /* elf_load_interp() has not set up any new VMCMD */ 803 error = ENOEXEC; 804 goto bad; 805 } 806 807 ap = kmem_alloc(sizeof(*ap), KM_SLEEP); 808 ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr; 809 epp->ep_entryoffset = interp_offset; 810 epp->ep_entry = ap->arg_interp + interp_offset; 811 PNBUF_PUT(interp); 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 kmem_free(ph, phsize); 836 return (*epp->ep_esch->es_setup_stack)(l, epp); 837 838 bad: 839 if (interp) 840 PNBUF_PUT(interp); 841 exec_free_emul_arg(epp); 842 kmem_free(ph, phsize); 843 kill_vmcmds(&epp->ep_vmcmds); 844 return error; 845 } 846 847 int 848 netbsd_elf_signature(struct lwp *l, struct exec_package *epp, 849 Elf_Ehdr *eh) 850 { 851 size_t i; 852 Elf_Shdr *sh; 853 Elf_Nhdr *np; 854 size_t shsize, nsize; 855 int error; 856 int isnetbsd = 0; 857 char *ndata, *ndesc; 858 859 #ifdef DIAGNOSTIC 860 const char *badnote; 861 #define BADNOTE(n) badnote = (n) 862 #else 863 #define BADNOTE(n) 864 #endif 865 866 epp->ep_pax_flags = 0; 867 if (eh->e_shnum > ELF_MAXSHNUM || eh->e_shnum == 0) 868 return ENOEXEC; 869 870 shsize = eh->e_shnum * sizeof(Elf_Shdr); 871 sh = kmem_alloc(shsize, KM_SLEEP); 872 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize); 873 if (error) 874 goto out; 875 876 np = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP); 877 for (i = 0; i < eh->e_shnum; i++) { 878 Elf_Shdr *shp = &sh[i]; 879 880 if (shp->sh_type != SHT_NOTE || 881 shp->sh_size > ELF_MAXNOTESIZE || 882 shp->sh_size < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 883 continue; 884 885 error = exec_read_from(l, epp->ep_vp, shp->sh_offset, np, 886 shp->sh_size); 887 if (error) 888 continue; 889 890 /* Point to the note, skip the header */ 891 ndata = (char *)(np + 1); 892 893 /* 894 * Padding is present if necessary to ensure 4-byte alignment. 895 * The actual section size is therefore: 896 * header size + 4-byte aligned name + 4-byte aligned desc 897 * Ensure this size is consistent with what is indicated 898 * in sh_size. The first check avoids integer overflows. 899 * 900 * Binaries from before NetBSD 1.6 have two notes in the same 901 * note section. The second note was never used, so as long as 902 * the section is at least as big as it should be, it's ok. 903 * These binaries also have a second note section with a note of 904 * type ELF_NOTE_TYPE_NETBSD_TAG, which can be ignored as well. 905 */ 906 if (np->n_namesz > shp->sh_size || np->n_descsz > shp->sh_size) { 907 BADNOTE("note size limit"); 908 goto bad; 909 } 910 nsize = sizeof(*np) + roundup(np->n_namesz, 4) + 911 roundup(np->n_descsz, 4); 912 if (nsize > shp->sh_size) { 913 BADNOTE("note size"); 914 goto bad; 915 } 916 ndesc = ndata + roundup(np->n_namesz, 4); 917 918 switch (np->n_type) { 919 case ELF_NOTE_TYPE_NETBSD_TAG: 920 /* It is us */ 921 if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ && 922 np->n_descsz == ELF_NOTE_NETBSD_DESCSZ && 923 memcmp(ndata, ELF_NOTE_NETBSD_NAME, 924 ELF_NOTE_NETBSD_NAMESZ) == 0) { 925 memcpy(&epp->ep_osversion, ndesc, 926 ELF_NOTE_NETBSD_DESCSZ); 927 isnetbsd = 1; 928 break; 929 } 930 931 /* 932 * Ignore SuSE tags; SuSE's n_type is the same as NetBSD's 933 * one. 934 */ 935 if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ && 936 memcmp(ndata, ELF_NOTE_SUSE_NAME, 937 ELF_NOTE_SUSE_NAMESZ) == 0) 938 break; 939 BADNOTE("NetBSD tag"); 940 goto bad; 941 942 case ELF_NOTE_TYPE_PAX_TAG: 943 if (np->n_namesz == ELF_NOTE_PAX_NAMESZ && 944 np->n_descsz == ELF_NOTE_PAX_DESCSZ && 945 memcmp(ndata, ELF_NOTE_PAX_NAME, 946 ELF_NOTE_PAX_NAMESZ) == 0) { 947 memcpy(&epp->ep_pax_flags, ndesc, 948 sizeof(epp->ep_pax_flags)); 949 break; 950 } 951 BADNOTE("PaX tag"); 952 goto bad; 953 954 case ELF_NOTE_TYPE_MARCH_TAG: 955 /* Copy the machine arch into the package. */ 956 if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ 957 && memcmp(ndata, ELF_NOTE_MARCH_NAME, 958 ELF_NOTE_MARCH_NAMESZ) == 0) { 959 /* Do not truncate the buffer */ 960 if (np->n_descsz > sizeof(epp->ep_machine_arch)) { 961 BADNOTE("description size limit"); 962 goto bad; 963 } 964 /* 965 * Ensure ndesc is NUL-terminated and of the 966 * expected length. 967 */ 968 if (strnlen(ndesc, np->n_descsz) + 1 != 969 np->n_descsz) { 970 BADNOTE("description size"); 971 goto bad; 972 } 973 strlcpy(epp->ep_machine_arch, ndesc, 974 sizeof(epp->ep_machine_arch)); 975 break; 976 } 977 BADNOTE("march tag"); 978 goto bad; 979 980 case ELF_NOTE_TYPE_MCMODEL_TAG: 981 /* arch specific check for code model */ 982 #ifdef ELF_MD_MCMODEL_CHECK 983 if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ 984 && memcmp(ndata, ELF_NOTE_MCMODEL_NAME, 985 ELF_NOTE_MCMODEL_NAMESZ) == 0) { 986 ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz); 987 break; 988 } 989 BADNOTE("mcmodel tag"); 990 goto bad; 991 #endif 992 break; 993 994 case ELF_NOTE_TYPE_SUSE_VERSION_TAG: 995 break; 996 997 default: 998 BADNOTE("unknown tag"); 999 bad: 1000 #ifdef DIAGNOSTIC 1001 /* Ignore GNU tags */ 1002 if (np->n_namesz == ELF_NOTE_GNU_NAMESZ && 1003 memcmp(ndata, ELF_NOTE_GNU_NAME, 1004 ELF_NOTE_GNU_NAMESZ) == 0) 1005 break; 1006 1007 int ns = MIN(np->n_namesz, shp->sh_size - sizeof(*np)); 1008 printf("%s: Unknown elf note type %d (%s): " 1009 "[namesz=%d, descsz=%d name=%-*.*s]\n", 1010 epp->ep_kname, np->n_type, badnote, np->n_namesz, 1011 np->n_descsz, ns, ns, ndata); 1012 #endif 1013 break; 1014 } 1015 } 1016 kmem_free(np, ELF_MAXNOTESIZE); 1017 1018 error = isnetbsd ? 0 : ENOEXEC; 1019 out: 1020 kmem_free(sh, shsize); 1021 return error; 1022 } 1023 1024 int 1025 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 1026 vaddr_t *pos) 1027 { 1028 int error; 1029 1030 if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 1031 return error; 1032 #ifdef ELF_MD_PROBE_FUNC 1033 if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0) 1034 return error; 1035 #elif defined(ELF_INTERP_NON_RELOCATABLE) 1036 *pos = ELF_LINK_ADDR; 1037 #endif 1038 epp->ep_flags |= EXEC_FORCEAUX; 1039 return 0; 1040 } 1041 1042 void 1043 elf_free_emul_arg(void *arg) 1044 { 1045 struct elf_args *ap = arg; 1046 KASSERT(ap != NULL); 1047 kmem_free(ap, sizeof(*ap)); 1048 } 1049