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