xref: /netbsd-src/sys/kern/exec_elf.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: exec_elf.c,v 1.101 2020/01/12 18:30:58 ad 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.101 2020/01/12 18:30:58 ad 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 	struct vattr attr;
425 	Elf_Ehdr eh;
426 	Elf_Phdr *ph = NULL;
427 	const Elf_Phdr *base_ph;
428 	const Elf_Phdr *last_ph;
429 	u_long phsize;
430 	Elf_Addr addr = *last;
431 	struct proc *p;
432 	bool use_topdown;
433 
434 	p = l->l_proc;
435 
436 	KASSERT(p->p_vmspace);
437 	KASSERT(p->p_vmspace != proc0.p_vmspace);
438 
439 #ifdef __USE_TOPDOWN_VM
440 	use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
441 #else
442 	use_topdown = false;
443 #endif
444 
445 	/*
446 	 * 1. open file
447 	 * 2. read filehdr
448 	 * 3. map text, data, and bss out of it using VM_*
449 	 */
450 	vp = epp->ep_interp;
451 	if (vp == NULL) {
452 		error = emul_find_interp(l, epp, path);
453 		if (error != 0)
454 			return error;
455 		vp = epp->ep_interp;
456 	}
457 	/* We'll tidy this ourselves - otherwise we have locking issues */
458 	epp->ep_interp = NULL;
459 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
460 
461 	/*
462 	 * Similarly, if it's not marked as executable, or it's not a regular
463 	 * file, we don't allow it to be used.
464 	 */
465 	if (vp->v_type != VREG) {
466 		error = EACCES;
467 		goto bad;
468 	}
469 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
470 		goto bad;
471 
472 	/* get attributes */
473 	/* XXX VOP_GETATTR() is the only thing that needs LK_EXCLUSIVE ^ */
474 	if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
475 		goto bad;
476 
477 	/*
478 	 * Check mount point.  Though we're not trying to exec this binary,
479 	 * we will be executing code from it, so if the mount point
480 	 * disallows execution or set-id-ness, we punt or kill the set-id.
481 	 */
482 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
483 		error = EACCES;
484 		goto bad;
485 	}
486 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
487 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
488 
489 	error = vn_marktext(vp);
490 	if (error)
491 		goto bad;
492 
493 	error = exec_read(l, vp, 0, &eh, sizeof(eh), IO_NODELOCKED);
494 	if (error != 0)
495 		goto bad;
496 
497 	if ((error = elf_check_header(&eh)) != 0)
498 		goto bad;
499 	if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
500 		DPRINTF("bad interpreter type %#x", eh.e_type);
501 		error = ENOEXEC;
502 		goto bad;
503 	}
504 
505 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
506 	ph = kmem_alloc(phsize, KM_SLEEP);
507 
508 	error = exec_read(l, vp, eh.e_phoff, ph, phsize, IO_NODELOCKED);
509 	if (error != 0)
510 		goto bad;
511 
512 #ifdef ELF_INTERP_NON_RELOCATABLE
513 	/*
514 	 * Evil hack:  Only MIPS should be non-relocatable, and the
515 	 * psections should have a high address (typically 0x5ffe0000).
516 	 * If it's now relocatable, it should be linked at 0 and the
517 	 * psections should have zeros in the upper part of the address.
518 	 * Otherwise, force the load at the linked address.
519 	 */
520 	if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
521 		*last = ELFDEFNNAME(NO_ADDR);
522 #endif
523 
524 	/*
525 	 * If no position to load the interpreter was set by a probe
526 	 * function, pick the same address that a non-fixed mmap(0, ..)
527 	 * would (i.e. something safely out of the way).
528 	 */
529 	if (*last == ELFDEFNNAME(NO_ADDR)) {
530 		u_long limit = 0;
531 		/*
532 		 * Find the start and ending addresses of the psections to
533 		 * be loaded.  This will give us the size.
534 		 */
535 		for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
536 			if (ph[i].p_type == PT_LOAD) {
537 				u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
538 				if (base_ph == NULL)
539 					base_ph = &ph[i];
540 				if (psize > limit)
541 					limit = psize;
542 			}
543 		}
544 
545 		if (base_ph == NULL) {
546 			DPRINTF("no interpreter loadable sections");
547 			error = ENOEXEC;
548 			goto bad;
549 		}
550 
551 		/*
552 		 * Now compute the size and load address.
553 		 */
554 		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
555 		    epp->ep_daddr,
556 		    round_page(limit) - trunc_page(base_ph->p_vaddr),
557 		    use_topdown);
558 		addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align,
559 		    use_topdown);
560 	} else {
561 		addr = *last; /* may be ELF_LINK_ADDR */
562 	}
563 
564 	/*
565 	 * Load all the necessary sections
566 	 */
567 	for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
568 		switch (ph[i].p_type) {
569 		case PT_LOAD: {
570 			u_long size;
571 			int flags;
572 
573 			if (base_ph == NULL) {
574 				/*
575 				 * First encountered psection is always the
576 				 * base psection.  Make sure it's aligned
577 				 * properly (align down for topdown and align
578 				 * upwards for not topdown).
579 				 */
580 				base_ph = &ph[i];
581 				flags = VMCMD_BASE;
582 				if (addr == ELF_LINK_ADDR)
583 					addr = ph[i].p_vaddr;
584 				if (use_topdown)
585 					addr = ELF_TRUNC(addr, ph[i].p_align);
586 				else
587 					addr = ELF_ROUND(addr, ph[i].p_align);
588 			} else {
589 				u_long limit = round_page(last_ph->p_vaddr
590 				    + last_ph->p_memsz);
591 				u_long base = trunc_page(ph[i].p_vaddr);
592 
593 				/*
594 				 * If there is a gap in between the psections,
595 				 * map it as inaccessible so nothing else
596 				 * mmap'ed will be placed there.
597 				 */
598 				if (limit != base) {
599 					NEW_VMCMD2(vcset, vmcmd_map_zero,
600 					    base - limit,
601 					    limit - base_ph->p_vaddr, NULLVP,
602 					    0, VM_PROT_NONE, VMCMD_RELATIVE);
603 				}
604 
605 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
606 				flags = VMCMD_RELATIVE;
607 			}
608 			last_ph = &ph[i];
609 			if ((error = elf_load_psection(vcset, vp, &ph[i], &addr,
610 			    &size, flags)) != 0)
611 				goto bad;
612 			/*
613 			 * If entry is within this psection then this
614 			 * must contain the .text section.  *entryoff is
615 			 * relative to the base psection.
616 			 */
617 			if (eh.e_entry >= ph[i].p_vaddr &&
618 			    eh.e_entry < (ph[i].p_vaddr + size)) {
619 				*entryoff = eh.e_entry - base_ph->p_vaddr;
620 			}
621 			addr += size;
622 			break;
623 		}
624 
625 		default:
626 			break;
627 		}
628 	}
629 
630 	kmem_free(ph, phsize);
631 	/*
632 	 * This value is ignored if TOPDOWN.
633 	 */
634 	*last = addr;
635 	vput(vp);
636 	return 0;
637 
638 bad:
639 	if (ph != NULL)
640 		kmem_free(ph, phsize);
641 	vput(vp);
642 	return error;
643 }
644 
645 /*
646  * exec_elf_makecmds(): Prepare an Elf binary's exec package
647  *
648  * First, set of the various offsets/lengths in the exec package.
649  *
650  * Then, mark the text image busy (so it can be demand paged) or error
651  * out if this is not possible.  Finally, set up vmcmds for the
652  * text, data, bss, and stack segments.
653  */
654 int
655 exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
656 {
657 	Elf_Ehdr *eh = epp->ep_hdr;
658 	Elf_Phdr *ph, *pp;
659 	Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
660 	int error, i;
661 	char *interp = NULL;
662 	u_long phsize;
663 	struct elf_args *ap;
664 	bool is_dyn = false;
665 
666 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) {
667 		DPRINTF("small header %#x", epp->ep_hdrvalid);
668 		return ENOEXEC;
669 	}
670 	if ((error = elf_check_header(eh)) != 0)
671 		return error;
672 
673 	if (eh->e_type == ET_DYN)
674 		/* PIE, and some libs have an entry point */
675 		is_dyn = true;
676 	else if (eh->e_type != ET_EXEC) {
677 		DPRINTF("bad type %#x", eh->e_type);
678 		return ENOEXEC;
679 	}
680 
681 	if (eh->e_phnum == 0) {
682 		DPRINTF("no program headers");
683 		return ENOEXEC;
684 	}
685 
686 	/* XXX only LK_EXCLUSIVE to match all others - allow spinning */
687 	vn_lock(epp->ep_vp, LK_EXCLUSIVE | LK_RETRY);
688 	error = vn_marktext(epp->ep_vp);
689 	if (error) {
690 		VOP_UNLOCK(epp->ep_vp);
691 		return error;
692 	}
693 
694 	/*
695 	 * Allocate space to hold all the program headers, and read them
696 	 * from the file
697 	 */
698 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
699 	ph = kmem_alloc(phsize, KM_SLEEP);
700 
701 	error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
702 	    IO_NODELOCKED);
703 	if (error != 0) {
704 		VOP_UNLOCK(epp->ep_vp);
705 		goto bad;
706 	}
707 
708 	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
709 	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
710 
711 	for (i = 0; i < eh->e_phnum; i++) {
712 		pp = &ph[i];
713 		if (pp->p_type == PT_INTERP) {
714 			if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
715 				DPRINTF("bad interpreter namelen %#jx",
716 				    (uintmax_t)pp->p_filesz);
717 				error = ENOEXEC;
718 				VOP_UNLOCK(epp->ep_vp);
719 				goto bad;
720 			}
721 			interp = PNBUF_GET();
722 			error = exec_read(l, epp->ep_vp, pp->p_offset, interp,
723 			    pp->p_filesz, IO_NODELOCKED);
724 			if (error != 0) {
725 				VOP_UNLOCK(epp->ep_vp);
726 				goto bad;
727 			}
728 			/* Ensure interp is NUL-terminated and of the expected length */
729 			if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
730 				DPRINTF("bad interpreter name");
731 				error = ENOEXEC;
732 				VOP_UNLOCK(epp->ep_vp);
733 				goto bad;
734 			}
735 			break;
736 		}
737 	}
738 
739 	/*
740 	 * On the same architecture, we may be emulating different systems.
741 	 * See which one will accept this executable.
742 	 *
743 	 * Probe functions would normally see if the interpreter (if any)
744 	 * exists. Emulation packages may possibly replace the interpreter in
745 	 * interp with a changed path (/emul/xxx/<path>).
746 	 */
747 	pos = ELFDEFNNAME(NO_ADDR);
748 	if (epp->ep_esch->u.elf_probe_func) {
749 		vaddr_t startp = (vaddr_t)pos;
750 
751 		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
752 							  &startp);
753 		if (error) {
754 			VOP_UNLOCK(epp->ep_vp);
755 			goto bad;
756 		}
757 		pos = (Elf_Addr)startp;
758 	}
759 
760 	if (is_dyn && (error = elf_placedynexec(epp, eh, ph)) != 0) {
761 		VOP_UNLOCK(epp->ep_vp);
762 		goto bad;
763 	}
764 
765 	/*
766 	 * Load all the necessary sections
767 	 */
768 	for (i = 0; i < eh->e_phnum; i++) {
769 		Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
770 		u_long size = 0;
771 
772 		switch (ph[i].p_type) {
773 		case PT_LOAD:
774 			if ((error = elf_load_psection(&epp->ep_vmcmds,
775 			    epp->ep_vp, &ph[i], &addr, &size, VMCMD_FIXED))
776 			    != 0) {
777 				VOP_UNLOCK(epp->ep_vp);
778 				goto bad;
779 			}
780 
781 			/*
782 			 * Consider this as text segment, if it is executable.
783 			 * If there is more than one text segment, pick the
784 			 * largest.
785 			 */
786 			if (ph[i].p_flags & PF_X) {
787 				if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
788 				    size > epp->ep_tsize) {
789 					epp->ep_taddr = addr;
790 					epp->ep_tsize = size;
791 				}
792 				end_text = addr + size;
793 			} else {
794 				epp->ep_daddr = addr;
795 				epp->ep_dsize = size;
796 			}
797 			if (ph[i].p_offset == 0) {
798 				computed_phdr = ph[i].p_vaddr + eh->e_phoff;
799 			}
800 			break;
801 
802 		case PT_SHLIB:
803 			/* SCO has these sections. */
804 		case PT_INTERP:
805 			/* Already did this one. */
806 		case PT_DYNAMIC:
807 		case PT_NOTE:
808 			break;
809 		case PT_PHDR:
810 			/* Note address of program headers (in text segment) */
811 			phdr = ph[i].p_vaddr;
812 			break;
813 
814 		default:
815 			/*
816 			 * Not fatal; we don't need to understand everything.
817 			 */
818 			break;
819 		}
820 	}
821 
822 	/* Now done with the vnode. */
823 	VOP_UNLOCK(epp->ep_vp);
824 
825 	if (epp->ep_vmcmds.evs_used == 0) {
826 		/* No VMCMD; there was no PT_LOAD section, or those
827 		 * sections were empty */
828 		DPRINTF("no vmcommands");
829 		error = ENOEXEC;
830 		goto bad;
831 	}
832 
833 	if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
834 		epp->ep_daddr = round_page(end_text);
835 		epp->ep_dsize = 0;
836 	}
837 
838 	/*
839 	 * Check if we found a dynamically linked binary and arrange to load
840 	 * its interpreter
841 	 */
842 	if (interp) {
843 		u_int nused = epp->ep_vmcmds.evs_used;
844 		u_long interp_offset = 0;
845 
846 		if ((error = elf_load_interp(l, epp, interp,
847 		    &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
848 			goto bad;
849 		}
850 		if (epp->ep_vmcmds.evs_used == nused) {
851 			/* elf_load_interp() has not set up any new VMCMD */
852 			DPRINTF("no vmcommands for interpreter");
853 			error = ENOEXEC;
854 			goto bad;
855 		}
856 
857 		ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
858 		ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
859 		epp->ep_entryoffset = interp_offset;
860 		epp->ep_entry = ap->arg_interp + interp_offset;
861 		PNBUF_PUT(interp);
862 		interp = NULL;
863 	} else {
864 		epp->ep_entry = eh->e_entry;
865 		if (epp->ep_flags & EXEC_FORCEAUX) {
866 			ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
867 			ap->arg_interp = (vaddr_t)NULL;
868 		} else {
869 			ap = NULL;
870 		}
871 	}
872 
873 	if (ap) {
874 		ap->arg_phaddr = phdr ? phdr : computed_phdr;
875 		ap->arg_phentsize = eh->e_phentsize;
876 		ap->arg_phnum = eh->e_phnum;
877 		ap->arg_entry = eh->e_entry;
878 		epp->ep_emul_arg = ap;
879 		epp->ep_emul_arg_free = elf_free_emul_arg;
880 	}
881 
882 #ifdef ELF_MAP_PAGE_ZERO
883 	/* Dell SVR4 maps page zero, yeuch! */
884 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
885 	    epp->ep_vp, 0, VM_PROT_READ);
886 #endif
887 
888 	error = (*epp->ep_esch->es_setup_stack)(l, epp);
889 	if (error)
890 		goto bad;
891 
892 	kmem_free(ph, phsize);
893 	return 0;
894 
895 bad:
896 	if (interp)
897 		PNBUF_PUT(interp);
898 	exec_free_emul_arg(epp);
899 	kmem_free(ph, phsize);
900 	kill_vmcmds(&epp->ep_vmcmds);
901 	return error;
902 }
903 
904 int
905 netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
906     Elf_Ehdr *eh)
907 {
908 	size_t i;
909 	Elf_Phdr *ph;
910 	size_t phsize;
911 	char *nbuf;
912 	int error;
913 	int isnetbsd = 0;
914 
915 	epp->ep_pax_flags = 0;
916 
917 	if (eh->e_phnum > ELF_MAXPHNUM || eh->e_phnum == 0) {
918 		DPRINTF("no signature %#x", eh->e_phnum);
919 		return ENOEXEC;
920 	}
921 
922 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
923 	ph = kmem_alloc(phsize, KM_SLEEP);
924 	error = exec_read(l, epp->ep_vp, eh->e_phoff, ph, phsize,
925 	    IO_NODELOCKED);
926 	if (error)
927 		goto out;
928 
929 	nbuf = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
930 	for (i = 0; i < eh->e_phnum; i++) {
931 		const char *nptr;
932 		size_t nlen;
933 
934 		if (ph[i].p_type != PT_NOTE ||
935 		    ph[i].p_filesz > ELF_MAXNOTESIZE)
936 			continue;
937 
938 		nlen = ph[i].p_filesz;
939 		error = exec_read(l, epp->ep_vp, ph[i].p_offset, nbuf, nlen,
940 		    IO_NODELOCKED);
941 		if (error)
942 			continue;
943 
944 		nptr = nbuf;
945 		while (nlen > 0) {
946 			const Elf_Nhdr *np;
947 			const char *ndata, *ndesc;
948 
949 			/* note header */
950 			np = (const Elf_Nhdr *)nptr;
951 			if (nlen < sizeof(*np)) {
952 				break;
953 			}
954 			nptr += sizeof(*np);
955 			nlen -= sizeof(*np);
956 
957 			/* note name */
958 			ndata = nptr;
959 			if (nlen < roundup(np->n_namesz, 4)) {
960 				break;
961 			}
962 			nptr += roundup(np->n_namesz, 4);
963 			nlen -= roundup(np->n_namesz, 4);
964 
965 			/* note description */
966 			ndesc = nptr;
967 			if (nlen < roundup(np->n_descsz, 4)) {
968 				break;
969 			}
970 			nptr += roundup(np->n_descsz, 4);
971 			nlen -= roundup(np->n_descsz, 4);
972 
973 			isnetbsd |= netbsd_elf_note(epp, np, ndata, ndesc);
974 		}
975 	}
976 	kmem_free(nbuf, ELF_MAXNOTESIZE);
977 
978 	error = isnetbsd ? 0 : ENOEXEC;
979 #ifdef DEBUG_ELF
980 	if (error)
981 		DPRINTF("not netbsd");
982 #endif
983 out:
984 	kmem_free(ph, phsize);
985 	return error;
986 }
987 
988 int
989 netbsd_elf_note(struct exec_package *epp,
990 		const Elf_Nhdr *np, const char *ndata, const char *ndesc)
991 {
992 	int isnetbsd = 0;
993 
994 #ifdef DIAGNOSTIC
995 	const char *badnote;
996 #define BADNOTE(n) badnote = (n)
997 #else
998 #define BADNOTE(n)
999 #endif
1000 
1001 	switch (np->n_type) {
1002 	case ELF_NOTE_TYPE_NETBSD_TAG:
1003 		/* It is us */
1004 		if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
1005 		    np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
1006 		    memcmp(ndata, ELF_NOTE_NETBSD_NAME,
1007 		    ELF_NOTE_NETBSD_NAMESZ) == 0) {
1008 			memcpy(&epp->ep_osversion, ndesc,
1009 			    ELF_NOTE_NETBSD_DESCSZ);
1010 			isnetbsd = 1;
1011 			break;
1012 		}
1013 
1014 		/*
1015 		 * Ignore SuSE tags; SuSE's n_type is the same the
1016 		 * NetBSD one.
1017 		 */
1018 		if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
1019 		    memcmp(ndata, ELF_NOTE_SUSE_NAME,
1020 		    ELF_NOTE_SUSE_NAMESZ) == 0)
1021 			break;
1022 		/*
1023 		 * Ignore old GCC
1024 		 */
1025 		if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
1026 		    memcmp(ndata, ELF_NOTE_OGCC_NAME,
1027 		    ELF_NOTE_OGCC_NAMESZ) == 0)
1028 			break;
1029 		BADNOTE("NetBSD tag");
1030 		goto bad;
1031 
1032 	case ELF_NOTE_TYPE_PAX_TAG:
1033 		if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
1034 		    np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
1035 		    memcmp(ndata, ELF_NOTE_PAX_NAME,
1036 		    ELF_NOTE_PAX_NAMESZ) == 0) {
1037 			uint32_t flags;
1038 			memcpy(&flags, ndesc, sizeof(flags));
1039 			/* Convert the flags and insert them into
1040 			 * the exec package. */
1041 			pax_setup_elf_flags(epp, flags);
1042 			break;
1043 		}
1044 		BADNOTE("PaX tag");
1045 		goto bad;
1046 
1047 	case ELF_NOTE_TYPE_MARCH_TAG:
1048 		/* Copy the machine arch into the package. */
1049 		if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
1050 		    && memcmp(ndata, ELF_NOTE_MARCH_NAME,
1051 			    ELF_NOTE_MARCH_NAMESZ) == 0) {
1052 			/* Do not truncate the buffer */
1053 			if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
1054 				BADNOTE("description size limit");
1055 				goto bad;
1056 			}
1057 			/*
1058 			 * Ensure ndesc is NUL-terminated and of the
1059 			 * expected length.
1060 			 */
1061 			if (strnlen(ndesc, np->n_descsz) + 1 !=
1062 			    np->n_descsz) {
1063 				BADNOTE("description size");
1064 				goto bad;
1065 			}
1066 			strlcpy(epp->ep_machine_arch, ndesc,
1067 			    sizeof(epp->ep_machine_arch));
1068 			break;
1069 		}
1070 		BADNOTE("march tag");
1071 		goto bad;
1072 
1073 	case ELF_NOTE_TYPE_MCMODEL_TAG:
1074 		/* arch specific check for code model */
1075 #ifdef ELF_MD_MCMODEL_CHECK
1076 		if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
1077 		    && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
1078 			    ELF_NOTE_MCMODEL_NAMESZ) == 0) {
1079 			ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
1080 			break;
1081 		}
1082 		BADNOTE("mcmodel tag");
1083 		goto bad;
1084 #endif
1085 		break;
1086 
1087 	case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
1088 		break;
1089 
1090 	case ELF_NOTE_TYPE_GO_BUILDID_TAG:
1091 		break;
1092 
1093 	case ELF_NOTE_TYPE_NETBSD_EMUL_TAG:
1094 		/* Ancient NetBSD version tag */
1095 		break;
1096 
1097 	default:
1098 		BADNOTE("unknown tag");
1099 bad:
1100 #ifdef DIAGNOSTIC
1101 		/* Ignore GNU tags */
1102 		if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
1103 		    memcmp(ndata, ELF_NOTE_GNU_NAME,
1104 		    ELF_NOTE_GNU_NAMESZ) == 0)
1105 		    break;
1106 
1107 		int ns = (int)np->n_namesz;
1108 		printf("%s: Unknown elf note type %d (%s): "
1109 		    "[namesz=%d, descsz=%d name=%-*.*s]\n",
1110 		    epp->ep_kname, np->n_type, badnote, np->n_namesz,
1111 		    np->n_descsz, ns, ns, ndata);
1112 #endif
1113 		break;
1114 	}
1115 
1116 	return isnetbsd;
1117 }
1118 
1119 int
1120 netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
1121     vaddr_t *pos)
1122 {
1123 	int error;
1124 
1125 	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
1126 		return error;
1127 #ifdef ELF_MD_PROBE_FUNC
1128 	if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
1129 		return error;
1130 #elif defined(ELF_INTERP_NON_RELOCATABLE)
1131 	*pos = ELF_LINK_ADDR;
1132 #endif
1133 	epp->ep_flags |= EXEC_FORCEAUX;
1134 	return 0;
1135 }
1136 
1137 void
1138 elf_free_emul_arg(void *arg)
1139 {
1140 	struct elf_args *ap = arg;
1141 	KASSERT(ap != NULL);
1142 	kmem_free(ap, sizeof(*ap));
1143 }
1144