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