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