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