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