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