exec_elf32.c revision 1.130
1/* $NetBSD: exec_elf32.c,v 1.130 2007/12/28 17:14:50 elad 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39/* 40 * Copyright (c) 1996 Christopher G. Demetriou 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66#include <sys/cdefs.h> 67__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.130 2007/12/28 17:14:50 elad Exp $"); 68 69/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 70#ifndef ELFSIZE 71#define ELFSIZE 32 72#endif 73 74#ifdef _KERNEL_OPT 75#include "opt_pax.h" 76#endif /* _KERNEL_OPT */ 77 78#include <sys/param.h> 79#include <sys/proc.h> 80#include <sys/malloc.h> 81#include <sys/namei.h> 82#include <sys/vnode.h> 83#include <sys/exec.h> 84#include <sys/exec_elf.h> 85#include <sys/syscall.h> 86#include <sys/signalvar.h> 87#include <sys/mount.h> 88#include <sys/stat.h> 89#include <sys/kauth.h> 90#include <sys/bitops.h> 91 92#include <sys/cpu.h> 93#include <machine/reg.h> 94 95#include <compat/common/compat_util.h> 96 97#include <sys/pax.h> 98 99extern const struct emul emul_netbsd; 100 101#define elf_check_header ELFNAME(check_header) 102#define elf_copyargs ELFNAME(copyargs) 103#define elf_load_file ELFNAME(load_file) 104#define elf_load_psection ELFNAME(load_psection) 105#define exec_elf_makecmds ELFNAME2(exec,makecmds) 106#define netbsd_elf_signature ELFNAME2(netbsd,signature) 107#define netbsd_elf_probe ELFNAME2(netbsd,probe) 108 109int elf_load_file(struct lwp *, struct exec_package *, char *, 110 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 111void elf_load_psection(struct exec_vmcmd_set *, struct vnode *, 112 const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 113 114int netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *); 115int netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *, 116 vaddr_t *); 117 118/* round up and down to page boundaries. */ 119#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 120#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 121 122#define MAXPHNUM 50 123 124#ifdef PAX_ASLR 125/* 126 * We don't move this code in kern_pax.c because it is compiled twice. 127 */ 128static void 129pax_aslr_elf(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh, 130 Elf_Phdr *ph) 131{ 132 size_t pax_align = 0, pax_offset, i; 133 uint32_t r; 134 135 if (!pax_aslr_active(l)) 136 return; 137 138 /* 139 * find align XXX: not all sections might have the same 140 * alignment 141 */ 142 for (i = 0; i < eh->e_phnum; i++) 143 if (ph[i].p_type == PT_LOAD) { 144 pax_align = ph[i].p_align; 145 break; 146 } 147 148 r = arc4random(); 149 150 if (pax_align == 0) 151 pax_align = PGSHIFT; 152#ifdef DEBUG_ASLR 153 uprintf("r=0x%x a=0x%x p=0x%x Delta=0x%lx\n", r, 154 ilog2(pax_align), PGSHIFT, PAX_ASLR_DELTA(r, 155 ilog2(pax_align), PAX_ASLR_DELTA_EXEC_LEN)); 156#endif 157 pax_offset = ELF_TRUNC(PAX_ASLR_DELTA(r, 158 ilog2(pax_align), PAX_ASLR_DELTA_EXEC_LEN), pax_align); 159 160 for (i = 0; i < eh->e_phnum; i++) 161 ph[i].p_vaddr += pax_offset; 162 eh->e_entry += pax_offset; 163#ifdef DEBUG_ASLR 164 uprintf("pax offset=0x%x entry=0x%x\n", 165 pax_offset, eh->e_entry); 166#endif 167} 168#endif /* PAX_ASLR */ 169 170/* 171 * Copy arguments onto the stack in the normal way, but add some 172 * extra information in case of dynamic binding. 173 */ 174int 175elf_copyargs(struct lwp *l, struct exec_package *pack, 176 struct ps_strings *arginfo, char **stackp, void *argp) 177{ 178 size_t len, vlen; 179 AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname; 180 struct elf_args *ap; 181 int error; 182 183 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 184 return error; 185 186 a = ai; 187 execname = NULL; 188 189 /* 190 * Push extra arguments on the stack needed by dynamically 191 * linked binaries 192 */ 193 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 194 struct vattr *vap = pack->ep_vap; 195 196 a->a_type = AT_PHDR; 197 a->a_v = ap->arg_phaddr; 198 a++; 199 200 a->a_type = AT_PHENT; 201 a->a_v = ap->arg_phentsize; 202 a++; 203 204 a->a_type = AT_PHNUM; 205 a->a_v = ap->arg_phnum; 206 a++; 207 208 a->a_type = AT_PAGESZ; 209 a->a_v = PAGE_SIZE; 210 a++; 211 212 a->a_type = AT_BASE; 213 a->a_v = ap->arg_interp; 214 a++; 215 216 a->a_type = AT_FLAGS; 217 a->a_v = 0; 218 a++; 219 220 a->a_type = AT_ENTRY; 221 a->a_v = ap->arg_entry; 222 a++; 223 224 a->a_type = AT_EUID; 225 if (vap->va_mode & S_ISUID) 226 a->a_v = vap->va_uid; 227 else 228 a->a_v = kauth_cred_geteuid(l->l_cred); 229 a++; 230 231 a->a_type = AT_RUID; 232 a->a_v = kauth_cred_getuid(l->l_cred); 233 a++; 234 235 a->a_type = AT_EGID; 236 if (vap->va_mode & S_ISGID) 237 a->a_v = vap->va_gid; 238 else 239 a->a_v = kauth_cred_getegid(l->l_cred); 240 a++; 241 242 a->a_type = AT_RGID; 243 a->a_v = kauth_cred_getgid(l->l_cred); 244 a++; 245 246 if (pack->ep_path) { 247 execname = a; 248 a->a_type = AT_SUN_EXECNAME; 249 a++; 250 } 251 252 free(ap, M_TEMP); 253 pack->ep_emul_arg = NULL; 254 } 255 256 a->a_type = AT_NULL; 257 a->a_v = 0; 258 a++; 259 260 vlen = (a - ai) * sizeof(AuxInfo); 261 262 if (execname) { 263 char *path = pack->ep_path; 264 execname->a_v = (intptr_t)(*stackp + vlen); 265 len = strlen(path) + 1; 266 if ((error = copyout(path, (*stackp + vlen), len)) != 0) 267 return error; 268 len = ALIGN(len); 269 } else 270 len = 0; 271 272 if ((error = copyout(ai, *stackp, vlen)) != 0) 273 return error; 274 *stackp += vlen + len; 275 276 return 0; 277} 278 279/* 280 * elf_check_header(): 281 * 282 * Check header for validity; return 0 of ok ENOEXEC if error 283 */ 284int 285elf_check_header(Elf_Ehdr *eh, int type) 286{ 287 288 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 289 eh->e_ident[EI_CLASS] != ELFCLASS) 290 return ENOEXEC; 291 292 switch (eh->e_machine) { 293 294 ELFDEFNNAME(MACHDEP_ID_CASES) 295 296 default: 297 return ENOEXEC; 298 } 299 300 if (ELF_EHDR_FLAGS_OK(eh) == 0) 301 return ENOEXEC; 302 303 if (eh->e_type != type) 304 return ENOEXEC; 305 306 if (eh->e_shnum > 32768 || eh->e_phnum > 128) 307 return ENOEXEC; 308 309 return 0; 310} 311 312/* 313 * elf_load_psection(): 314 * 315 * Load a psection at the appropriate address 316 */ 317void 318elf_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 */ 403int 404elf_load_file(struct lwp *l, struct exec_package *epp, char *path, 405 struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 406 Elf_Addr *last) 407{ 408 int error, i; 409 struct vnode *vp; 410 struct vattr attr; 411 Elf_Ehdr eh; 412 Elf_Phdr *ph = NULL; 413 const Elf_Phdr *ph0; 414 const Elf_Phdr *base_ph; 415 const Elf_Phdr *last_ph; 416 u_long phsize; 417 Elf_Addr addr = *last; 418 struct proc *p; 419 420 p = l->l_proc; 421 422 /* 423 * 1. open file 424 * 2. read filehdr 425 * 3. map text, data, and bss out of it using VM_* 426 */ 427 vp = epp->ep_interp; 428 if (vp == NULL) { 429 error = emul_find_interp(l, epp, path); 430 if (error != 0) 431 return error; 432 vp = epp->ep_interp; 433 } 434 /* We'll tidy this ourselves - otherwise we have locking issues */ 435 epp->ep_interp = NULL; 436 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 437 438 /* 439 * Similarly, if it's not marked as executable, or it's not a regular 440 * file, we don't allow it to be used. 441 */ 442 if (vp->v_type != VREG) { 443 error = EACCES; 444 goto badunlock; 445 } 446 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 447 goto badunlock; 448 449 /* get attributes */ 450 if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0) 451 goto badunlock; 452 453 /* 454 * Check mount point. Though we're not trying to exec this binary, 455 * we will be executing code from it, so if the mount point 456 * disallows execution or set-id-ness, we punt or kill the set-id. 457 */ 458 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 459 error = EACCES; 460 goto badunlock; 461 } 462 if (vp->v_mount->mnt_flag & MNT_NOSUID) 463 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 464 465#ifdef notyet /* XXX cgd 960926 */ 466 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 467#endif 468 469 error = vn_marktext(vp); 470 if (error) 471 goto badunlock; 472 473 VOP_UNLOCK(vp, 0); 474 475 if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 476 goto bad; 477 478 if ((error = elf_check_header(&eh, ET_DYN)) != 0) 479 goto bad; 480 481 if (eh.e_phnum > MAXPHNUM) 482 goto bad; 483 484 phsize = eh.e_phnum * sizeof(Elf_Phdr); 485 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 486 487 if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 488 goto bad; 489 490#ifdef ELF_INTERP_NON_RELOCATABLE 491 /* 492 * Evil hack: Only MIPS should be non-relocatable, and the 493 * psections should have a high address (typically 0x5ffe0000). 494 * If it's now relocatable, it should be linked at 0 and the 495 * psections should have zeros in the upper part of the address. 496 * Otherwise, force the load at the linked address. 497 */ 498 if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 499 *last = ELFDEFNNAME(NO_ADDR); 500#endif 501 502 /* 503 * If no position to load the interpreter was set by a probe 504 * function, pick the same address that a non-fixed mmap(0, ..) 505 * would (i.e. something safely out of the way). 506 */ 507 if (*last == ELFDEFNNAME(NO_ADDR)) { 508 u_long limit = 0; 509 /* 510 * Find the start and ending addresses of the psections to 511 * be loaded. This will give us the size. 512 */ 513 for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 514 i++, ph0++) { 515 if (ph0->p_type == PT_LOAD) { 516 u_long psize = ph0->p_vaddr + ph0->p_memsz; 517 if (base_ph == NULL) 518 base_ph = ph0; 519 if (psize > limit) 520 limit = psize; 521 } 522 } 523 524 if (base_ph == NULL) { 525 error = ENOEXEC; 526 goto bad; 527 } 528 529 /* 530 * Now compute the size and load address. 531 */ 532 addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 533 epp->ep_daddr, 534 round_page(limit) - trunc_page(base_ph->p_vaddr)); 535 } else 536 addr = *last; /* may be ELF_LINK_ADDR */ 537 538 /* 539 * Load all the necessary sections 540 */ 541 for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 542 i < eh.e_phnum; i++, ph0++) { 543 switch (ph0->p_type) { 544 case PT_LOAD: { 545 u_long size; 546 int prot = 0; 547 int flags; 548 549 if (base_ph == NULL) { 550 /* 551 * First encountered psection is always the 552 * base psection. Make sure it's aligned 553 * properly (align down for topdown and align 554 * upwards for not topdown). 555 */ 556 base_ph = ph0; 557 flags = VMCMD_BASE; 558 if (addr == ELF_LINK_ADDR) 559 addr = ph0->p_vaddr; 560 if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 561 addr = ELF_TRUNC(addr, ph0->p_align); 562 else 563 addr = ELF_ROUND(addr, ph0->p_align); 564 } else { 565 u_long limit = round_page(last_ph->p_vaddr 566 + last_ph->p_memsz); 567 u_long base = trunc_page(ph0->p_vaddr); 568 569 /* 570 * If there is a gap in between the psections, 571 * map it as inaccessible so nothing else 572 * mmap'ed will be placed there. 573 */ 574 if (limit != base) { 575 NEW_VMCMD2(vcset, vmcmd_map_zero, 576 base - limit, 577 limit - base_ph->p_vaddr, NULLVP, 578 0, VM_PROT_NONE, VMCMD_RELATIVE); 579 } 580 581 addr = ph0->p_vaddr - base_ph->p_vaddr; 582 flags = VMCMD_RELATIVE; 583 } 584 last_ph = ph0; 585 elf_load_psection(vcset, vp, &ph[i], &addr, 586 &size, &prot, flags); 587 /* 588 * If entry is within this psection then this 589 * must contain the .text section. *entryoff is 590 * relative to the base psection. 591 */ 592 if (eh.e_entry >= ph0->p_vaddr && 593 eh.e_entry < (ph0->p_vaddr + size)) { 594 *entryoff = eh.e_entry - base_ph->p_vaddr; 595 } 596 addr += size; 597 break; 598 } 599 600 case PT_DYNAMIC: 601 case PT_PHDR: 602 break; 603 604 case PT_NOTE: 605 break; 606 607 default: 608 break; 609 } 610 } 611 612 free(ph, M_TEMP); 613 /* 614 * This value is ignored if TOPDOWN. 615 */ 616 *last = addr; 617 vrele(vp); 618 return 0; 619 620badunlock: 621 VOP_UNLOCK(vp, 0); 622 623bad: 624 if (ph != NULL) 625 free(ph, M_TEMP); 626#ifdef notyet /* XXX cgd 960926 */ 627 (maybe) VOP_CLOSE it 628#endif 629 vrele(vp); 630 return error; 631} 632 633/* 634 * exec_elf_makecmds(): Prepare an Elf binary's exec package 635 * 636 * First, set of the various offsets/lengths in the exec package. 637 * 638 * Then, mark the text image busy (so it can be demand paged) or error 639 * out if this is not possible. Finally, set up vmcmds for the 640 * text, data, bss, and stack segments. 641 */ 642int 643exec_elf_makecmds(struct lwp *l, struct exec_package *epp) 644{ 645 Elf_Ehdr *eh = epp->ep_hdr; 646 Elf_Phdr *ph, *pp; 647 Elf_Addr phdr = 0, pos = 0; 648 int error, i, nload; 649 char *interp = NULL; 650 u_long phsize; 651 struct proc *p; 652 bool is_dyn; 653 654 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 655 return ENOEXEC; 656 657 is_dyn = elf_check_header(eh, ET_DYN) == 0; 658 /* 659 * XXX allow for executing shared objects. It seems silly 660 * but other ELF-based systems allow it as well. 661 */ 662 if (elf_check_header(eh, ET_EXEC) != 0 && !is_dyn) 663 return ENOEXEC; 664 665 if (eh->e_phnum > MAXPHNUM) 666 return ENOEXEC; 667 668 error = vn_marktext(epp->ep_vp); 669 if (error) 670 return error; 671 672 /* 673 * Allocate space to hold all the program headers, and read them 674 * from the file 675 */ 676 p = l->l_proc; 677 phsize = eh->e_phnum * sizeof(Elf_Phdr); 678 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 679 680 if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 681 0) 682 goto bad; 683 684 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 685 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 686 687 for (i = 0; i < eh->e_phnum; i++) { 688 pp = &ph[i]; 689 if (pp->p_type == PT_INTERP) { 690 if (pp->p_filesz >= MAXPATHLEN) 691 goto bad; 692 interp = PNBUF_GET(); 693 interp[0] = '\0'; 694 if ((error = exec_read_from(l, epp->ep_vp, 695 pp->p_offset, interp, pp->p_filesz)) != 0) 696 goto bad; 697 break; 698 } 699 } 700 701 /* 702 * On the same architecture, we may be emulating different systems. 703 * See which one will accept this executable. 704 * 705 * Probe functions would normally see if the interpreter (if any) 706 * exists. Emulation packages may possibly replace the interpreter in 707 * interp[] with a changed path (/emul/xxx/<path>). 708 */ 709 pos = ELFDEFNNAME(NO_ADDR); 710 if (epp->ep_esch->u.elf_probe_func) { 711 vaddr_t startp = (vaddr_t)pos; 712 713 error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 714 &startp); 715 if (error) 716 goto bad; 717 pos = (Elf_Addr)startp; 718 } 719 720#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR) 721 if (epp->ep_pax_flags) 722 pax_adjust(l, epp->ep_pax_flags); 723#endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */ 724 725#ifdef PAX_ASLR 726 if (is_dyn) 727 pax_aslr_elf(l, epp, eh, ph); 728#endif /* PAX_ASLR */ 729 730 /* 731 * Load all the necessary sections 732 */ 733 for (i = nload = 0; i < eh->e_phnum; i++) { 734 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 735 u_long size = 0; 736 int prot = 0; 737 738 pp = &ph[i]; 739 740 switch (ph[i].p_type) { 741 case PT_LOAD: 742 /* 743 * XXX 744 * Can handle only 2 sections: text and data 745 */ 746 if (nload++ == 2) 747 goto bad; 748 elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 749 &ph[i], &addr, &size, &prot, VMCMD_FIXED); 750 751 /* 752 * Decide whether it's text or data by looking 753 * at the entry point. 754 */ 755 if (eh->e_entry >= addr && 756 eh->e_entry < (addr + size)) { 757 epp->ep_taddr = addr; 758 epp->ep_tsize = size; 759 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 760 epp->ep_daddr = addr; 761 epp->ep_dsize = size; 762 } 763 } else { 764 epp->ep_daddr = addr; 765 epp->ep_dsize = size; 766 } 767 break; 768 769 case PT_SHLIB: 770 /* SCO has these sections. */ 771 case PT_INTERP: 772 /* Already did this one. */ 773 case PT_DYNAMIC: 774 break; 775 case PT_NOTE: 776 break; 777 case PT_PHDR: 778 /* Note address of program headers (in text segment) */ 779 phdr = pp->p_vaddr; 780 break; 781 782 default: 783 /* 784 * Not fatal; we don't need to understand everything. 785 */ 786 break; 787 } 788 } 789 790 /* 791 * Check if we found a dynamically linked binary and arrange to load 792 * its interpreter 793 */ 794 if (interp) { 795 struct elf_args *ap; 796 int j = epp->ep_vmcmds.evs_used; 797 u_long interp_offset; 798 799 MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 800 M_TEMP, M_WAITOK); 801 if ((error = elf_load_file(l, epp, interp, 802 &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 803 FREE(ap, M_TEMP); 804 goto bad; 805 } 806 ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr; 807 epp->ep_entry = ap->arg_interp + interp_offset; 808 ap->arg_phaddr = phdr; 809 810 ap->arg_phentsize = eh->e_phentsize; 811 ap->arg_phnum = eh->e_phnum; 812 ap->arg_entry = eh->e_entry; 813 814 epp->ep_emul_arg = ap; 815 816 PNBUF_PUT(interp); 817 } else 818 epp->ep_entry = eh->e_entry; 819 820#ifdef ELF_MAP_PAGE_ZERO 821 /* Dell SVR4 maps page zero, yeuch! */ 822 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 823 epp->ep_vp, 0, VM_PROT_READ); 824#endif 825 free(ph, M_TEMP); 826 return (*epp->ep_esch->es_setup_stack)(l, epp); 827 828bad: 829 if (interp) 830 PNBUF_PUT(interp); 831 free(ph, M_TEMP); 832 kill_vmcmds(&epp->ep_vmcmds); 833 return ENOEXEC; 834} 835 836int 837netbsd_elf_signature(struct lwp *l, struct exec_package *epp, 838 Elf_Ehdr *eh) 839{ 840 size_t i; 841 Elf_Phdr *ph; 842 size_t phsize; 843 int error; 844 int isnetbsd = 0; 845 char *ndata; 846 847 epp->ep_pax_flags = 0; 848 if (eh->e_phnum > MAXPHNUM) 849 return ENOEXEC; 850 851 phsize = eh->e_phnum * sizeof(Elf_Phdr); 852 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 853 error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize); 854 if (error) 855 goto out; 856 857 for (i = 0; i < eh->e_phnum; i++) { 858 Elf_Phdr *ephp = &ph[i]; 859 Elf_Nhdr *np; 860 861 if (ephp->p_type != PT_NOTE || 862 ephp->p_filesz > 1024 || 863 ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 864 continue; 865 866 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 867 error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np, 868 ephp->p_filesz); 869 if (error) 870 goto next; 871 872 ndata = (char *)(np + 1); 873 switch (np->n_type) { 874 case ELF_NOTE_TYPE_NETBSD_TAG: 875 if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 876 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 877 memcmp(ndata, ELF_NOTE_NETBSD_NAME, 878 ELF_NOTE_NETBSD_NAMESZ)) 879 goto next; 880 isnetbsd = 1; 881 break; 882 883 case ELF_NOTE_TYPE_PAX_TAG: 884 if (np->n_namesz != ELF_NOTE_PAX_NAMESZ || 885 np->n_descsz != ELF_NOTE_PAX_DESCSZ || 886 memcmp(ndata, ELF_NOTE_PAX_NAME, 887 ELF_NOTE_PAX_NAMESZ)) 888 goto next; 889 (void)memcpy(&epp->ep_pax_flags, 890 ndata + ELF_NOTE_PAX_NAMESZ, 891 sizeof(epp->ep_pax_flags)); 892 break; 893 894 default: 895 break; 896 } 897 898next: 899 free(np, M_TEMP); 900 continue; 901 } 902 903 error = isnetbsd ? 0 : ENOEXEC; 904out: 905 free(ph, M_TEMP); 906 return error; 907} 908 909int 910netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 911 vaddr_t *pos) 912{ 913 int error; 914 915 if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 916 return error; 917#ifdef ELF_INTERP_NON_RELOCATABLE 918 *pos = ELF_LINK_ADDR; 919#endif 920 return 0; 921} 922