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