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