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