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