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