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