xref: /netbsd-src/sys/kern/kern_exec.c (revision e1e74f372d4580af0ad34c25f04bfd9fa29bb82c)
1 /*	$NetBSD: kern_exec.c,v 1.269 2008/02/24 21:46:04 christos Exp $	*/
2 
3 /*-
4  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
5  * Copyright (C) 1992 Wolfgang Solfrank.
6  * Copyright (C) 1992 TooLs GmbH.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.269 2008/02/24 21:46:04 christos Exp $");
37 
38 #include "opt_ktrace.h"
39 #include "opt_syscall_debug.h"
40 #include "opt_compat_netbsd.h"
41 #include "veriexec.h"
42 #include "opt_pax.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/kmem.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/file.h>
55 #include <sys/acct.h>
56 #include <sys/exec.h>
57 #include <sys/ktrace.h>
58 #include <sys/resourcevar.h>
59 #include <sys/wait.h>
60 #include <sys/mman.h>
61 #include <sys/ras.h>
62 #include <sys/signalvar.h>
63 #include <sys/stat.h>
64 #include <sys/syscall.h>
65 #include <sys/kauth.h>
66 #include <sys/lwpctl.h>
67 #include <sys/pax.h>
68 #include <sys/cpu.h>
69 
70 #include <sys/syscallargs.h>
71 #if NVERIEXEC > 0
72 #include <sys/verified_exec.h>
73 #endif /* NVERIEXEC > 0 */
74 
75 #include <uvm/uvm_extern.h>
76 
77 #include <machine/reg.h>
78 
79 #include <compat/common/compat_util.h>
80 
81 static int exec_sigcode_map(struct proc *, const struct emul *);
82 
83 #ifdef DEBUG_EXEC
84 #define DPRINTF(a) uprintf a
85 #else
86 #define DPRINTF(a)
87 #endif /* DEBUG_EXEC */
88 
89 /*
90  * Exec function switch:
91  *
92  * Note that each makecmds function is responsible for loading the
93  * exec package with the necessary functions for any exec-type-specific
94  * handling.
95  *
96  * Functions for specific exec types should be defined in their own
97  * header file.
98  */
99 extern const struct execsw	execsw_builtin[];
100 extern int			nexecs_builtin;
101 static const struct execsw	**execsw = NULL;
102 static int			nexecs;
103 
104 u_int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
105 
106 #ifdef LKM
107 /* list of supported emulations */
108 static
109 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
110 struct emul_entry {
111 	LIST_ENTRY(emul_entry)	el_list;
112 	const struct emul	*el_emul;
113 	int			ro_entry;
114 };
115 
116 /* list of dynamically loaded execsw entries */
117 static
118 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
119 struct exec_entry {
120 	LIST_ENTRY(exec_entry)	ex_list;
121 	const struct execsw	*es;
122 };
123 
124 /* structure used for building execw[] */
125 struct execsw_entry {
126 	struct execsw_entry	*next;
127 	const struct execsw	*es;
128 };
129 #endif /* LKM */
130 
131 #ifdef SYSCALL_DEBUG
132 extern const char * const syscallnames[];
133 #endif
134 
135 #ifdef COMPAT_16
136 extern char	sigcode[], esigcode[];
137 struct uvm_object *emul_netbsd_object;
138 #endif
139 
140 #ifndef __HAVE_SYSCALL_INTERN
141 void	syscall(void);
142 #endif
143 
144 /* NetBSD emul struct */
145 const struct emul emul_netbsd = {
146 	"netbsd",
147 	NULL,		/* emulation path */
148 #ifndef __HAVE_MINIMAL_EMUL
149 	EMUL_HAS_SYS___syscall,
150 	NULL,
151 	SYS_syscall,
152 	SYS_NSYSENT,
153 #endif
154 	sysent,
155 #ifdef SYSCALL_DEBUG
156 	syscallnames,
157 #else
158 	NULL,
159 #endif
160 	sendsig,
161 	trapsignal,
162 	NULL,
163 #ifdef COMPAT_16
164 	sigcode,
165 	esigcode,
166 	&emul_netbsd_object,
167 #else
168 	NULL,
169 	NULL,
170 	NULL,
171 #endif
172 	setregs,
173 	NULL,
174 	NULL,
175 	NULL,
176 	NULL,
177 	NULL,
178 #ifdef __HAVE_SYSCALL_INTERN
179 	syscall_intern,
180 #else
181 	syscall,
182 #endif
183 	NULL,
184 	NULL,
185 
186 	uvm_default_mapaddr,
187 	NULL,
188 	sizeof(ucontext_t),
189 	startlwp,
190 };
191 
192 #ifdef LKM
193 /*
194  * Exec lock. Used to control access to execsw[] structures.
195  * This must not be static so that netbsd32 can access it, too.
196  */
197 krwlock_t exec_lock;
198 
199 static void link_es(struct execsw_entry **, const struct execsw *);
200 #endif /* LKM */
201 
202 static kmutex_t sigobject_lock;
203 
204 /*
205  * check exec:
206  * given an "executable" described in the exec package's namei info,
207  * see what we can do with it.
208  *
209  * ON ENTRY:
210  *	exec package with appropriate namei info
211  *	lwp pointer of exec'ing lwp
212  *	NO SELF-LOCKED VNODES
213  *
214  * ON EXIT:
215  *	error:	nothing held, etc.  exec header still allocated.
216  *	ok:	filled exec package, executable's vnode (unlocked).
217  *
218  * EXEC SWITCH ENTRY:
219  * 	Locked vnode to check, exec package, proc.
220  *
221  * EXEC SWITCH EXIT:
222  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
223  *	error:	destructive:
224  *			everything deallocated execept exec header.
225  *		non-destructive:
226  *			error code, executable's vnode (unlocked),
227  *			exec header unmodified.
228  */
229 int
230 /*ARGSUSED*/
231 check_exec(struct lwp *l, struct exec_package *epp)
232 {
233 	int		error, i;
234 	struct vnode	*vp;
235 	struct nameidata *ndp;
236 	size_t		resid;
237 
238 	ndp = epp->ep_ndp;
239 	ndp->ni_cnd.cn_nameiop = LOOKUP;
240 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME | TRYEMULROOT;
241 	/* first get the vnode */
242 	if ((error = namei(ndp)) != 0)
243 		return error;
244 	epp->ep_vp = vp = ndp->ni_vp;
245 
246 	/* check access and type */
247 	if (vp->v_type != VREG) {
248 		error = EACCES;
249 		goto bad1;
250 	}
251 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
252 		goto bad1;
253 
254 	/* get attributes */
255 	if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0)
256 		goto bad1;
257 
258 	/* Check mount point */
259 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
260 		error = EACCES;
261 		goto bad1;
262 	}
263 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
264 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
265 
266 	/* try to open it */
267 	if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0)
268 		goto bad1;
269 
270 	/* unlock vp, since we need it unlocked from here on out. */
271 	VOP_UNLOCK(vp, 0);
272 
273 #if NVERIEXEC > 0
274 	error = veriexec_verify(l, vp, ndp->ni_cnd.cn_pnbuf,
275 	    epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT,
276 	    NULL);
277 	if (error)
278 		goto bad2;
279 #endif /* NVERIEXEC > 0 */
280 
281 #ifdef PAX_SEGVGUARD
282 	error = pax_segvguard(l, vp, ndp->ni_cnd.cn_pnbuf, false);
283 	if (error)
284 		goto bad2;
285 #endif /* PAX_SEGVGUARD */
286 
287 	/* now we have the file, get the exec header */
288 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
289 			UIO_SYSSPACE, 0, l->l_cred, &resid, NULL);
290 	if (error)
291 		goto bad2;
292 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
293 
294 	/*
295 	 * Set up default address space limits.  Can be overridden
296 	 * by individual exec packages.
297 	 *
298 	 * XXX probably should be all done in the exec packages.
299 	 */
300 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
301 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
302 	/*
303 	 * set up the vmcmds for creation of the process
304 	 * address space
305 	 */
306 	error = ENOEXEC;
307 	for (i = 0; i < nexecs; i++) {
308 		int newerror;
309 
310 		epp->ep_esch = execsw[i];
311 		newerror = (*execsw[i]->es_makecmds)(l, epp);
312 
313 		if (!newerror) {
314 			/* Seems ok: check that entry point is sane */
315 			if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
316 				error = ENOEXEC;
317 				break;
318 			}
319 
320 			/* check limits */
321 			if ((epp->ep_tsize > MAXTSIZ) ||
322 			    (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit
323 						    [RLIMIT_DATA].rlim_cur)) {
324 				error = ENOMEM;
325 				break;
326 			}
327 			return 0;
328 		}
329 
330 		if (epp->ep_emul_root != NULL) {
331 			vrele(epp->ep_emul_root);
332 			epp->ep_emul_root = NULL;
333 		}
334 		if (epp->ep_interp != NULL) {
335 			vrele(epp->ep_interp);
336 			epp->ep_interp = NULL;
337 		}
338 
339 		/* make sure the first "interesting" error code is saved. */
340 		if (error == ENOEXEC)
341 			error = newerror;
342 
343 		if (epp->ep_flags & EXEC_DESTR)
344 			/* Error from "#!" code, tidied up by recursive call */
345 			return error;
346 	}
347 
348 	/* not found, error */
349 
350 	/*
351 	 * free any vmspace-creation commands,
352 	 * and release their references
353 	 */
354 	kill_vmcmds(&epp->ep_vmcmds);
355 
356 bad2:
357 	/*
358 	 * close and release the vnode, restore the old one, free the
359 	 * pathname buf, and punt.
360 	 */
361 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
362 	VOP_CLOSE(vp, FREAD, l->l_cred);
363 	vput(vp);
364 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
365 	return error;
366 
367 bad1:
368 	/*
369 	 * free the namei pathname buffer, and put the vnode
370 	 * (which we don't yet have open).
371 	 */
372 	vput(vp);				/* was still locked */
373 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
374 	return error;
375 }
376 
377 #ifdef __MACHINE_STACK_GROWS_UP
378 #define STACK_PTHREADSPACE NBPG
379 #else
380 #define STACK_PTHREADSPACE 0
381 #endif
382 
383 static int
384 execve_fetch_element(char * const *array, size_t index, char **value)
385 {
386 	return copyin(array + index, value, sizeof(*value));
387 }
388 
389 /*
390  * exec system call
391  */
392 /* ARGSUSED */
393 int
394 sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval)
395 {
396 	/* {
397 		syscallarg(const char *)	path;
398 		syscallarg(char * const *)	argp;
399 		syscallarg(char * const *)	envp;
400 	} */
401 
402 	return execve1(l, SCARG(uap, path), SCARG(uap, argp),
403 	    SCARG(uap, envp), execve_fetch_element);
404 }
405 
406 int
407 execve1(struct lwp *l, const char *path, char * const *args,
408     char * const *envs, execve_fetch_element_t fetch_element)
409 {
410 	int			error;
411 	struct exec_package	pack;
412 	struct nameidata	nid;
413 	struct vattr		attr;
414 	struct proc		*p;
415 	char			*argp;
416 	char			*dp, *sp;
417 	long			argc, envc;
418 	size_t			i, len;
419 	char			*stack;
420 	struct ps_strings	arginfo;
421 	struct ps_strings	*aip = &arginfo;
422 	struct vmspace		*vm;
423 	struct exec_fakearg	*tmpfap;
424 	int			szsigcode;
425 	struct exec_vmcmd	*base_vcp;
426 	ksiginfo_t		ksi;
427 	ksiginfoq_t		kq;
428 	char			*pathbuf;
429 	size_t			pathbuflen;
430 
431 	p = l->l_proc;
432 
433 	/*
434 	 * Check if we have exceeded our number of processes limit.
435 	 * This is so that we handle the case where a root daemon
436 	 * forked, ran setuid to become the desired user and is trying
437 	 * to exec. The obvious place to do the reference counting check
438 	 * is setuid(), but we don't do the reference counting check there
439 	 * like other OS's do because then all the programs that use setuid()
440 	 * must be modified to check the return code of setuid() and exit().
441 	 * It is dangerous to make setuid() fail, because it fails open and
442 	 * the program will continue to run as root. If we make it succeed
443 	 * and return an error code, again we are not enforcing the limit.
444 	 * The best place to enforce the limit is here, when the process tries
445 	 * to execute a new image, because eventually the process will need
446 	 * to call exec in order to do something useful.
447 	 */
448 
449 	if ((p->p_flag & PK_SUGID) &&
450 	    chgproccnt(kauth_cred_getuid(p->p_cred), 0) >
451 	    p->p_rlimit[RLIMIT_NPROC].rlim_cur)
452 		return EAGAIN;
453 
454 	/*
455 	 * Drain existing references and forbid new ones.  The process
456 	 * should be left alone until we're done here.  This is necessary
457 	 * to avoid race conditions - e.g. in ptrace() - that might allow
458 	 * a local user to illicitly obtain elevated privileges.
459 	 */
460 	rw_enter(&p->p_reflock, RW_WRITER);
461 
462 	base_vcp = NULL;
463 	/*
464 	 * Init the namei data to point the file user's program name.
465 	 * This is done here rather than in check_exec(), so that it's
466 	 * possible to override this settings if any of makecmd/probe
467 	 * functions call check_exec() recursively - for example,
468 	 * see exec_script_makecmds().
469 	 */
470 	pathbuf = PNBUF_GET();
471 	error = copyinstr(path, pathbuf, MAXPATHLEN, &pathbuflen);
472 	if (error) {
473 		DPRINTF(("execve: copyinstr path %d", error));
474 		goto clrflg;
475 	}
476 
477 	NDINIT(&nid, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_SYSSPACE, pathbuf);
478 
479 	/*
480 	 * initialize the fields of the exec package.
481 	 */
482 	pack.ep_name = path;
483 	pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP);
484 	pack.ep_hdrlen = exec_maxhdrsz;
485 	pack.ep_hdrvalid = 0;
486 	pack.ep_ndp = &nid;
487 	pack.ep_emul_arg = NULL;
488 	pack.ep_vmcmds.evs_cnt = 0;
489 	pack.ep_vmcmds.evs_used = 0;
490 	pack.ep_vap = &attr;
491 	pack.ep_flags = 0;
492 	pack.ep_emul_root = NULL;
493 	pack.ep_interp = NULL;
494 	pack.ep_esch = NULL;
495 
496 #ifdef LKM
497 	rw_enter(&exec_lock, RW_READER);
498 #endif
499 
500 	/* see if we can run it. */
501 	if ((error = check_exec(l, &pack)) != 0) {
502 		if (error != ENOENT) {
503 			DPRINTF(("execve: check exec failed %d\n", error));
504 		}
505 		goto freehdr;
506 	}
507 
508 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
509 
510 	/* allocate an argument buffer */
511 	argp = (char *) uvm_km_alloc(exec_map, NCARGS, 0,
512 	    UVM_KMF_PAGEABLE|UVM_KMF_WAITVA);
513 #ifdef DIAGNOSTIC
514 	if (argp == NULL)
515 		panic("execve: argp == NULL");
516 #endif
517 	dp = argp;
518 	argc = 0;
519 
520 	/* copy the fake args list, if there's one, freeing it as we go */
521 	if (pack.ep_flags & EXEC_HASARGL) {
522 		tmpfap = pack.ep_fa;
523 		while (tmpfap->fa_arg != NULL) {
524 			const char *cp;
525 
526 			cp = tmpfap->fa_arg;
527 			while (*cp)
528 				*dp++ = *cp++;
529 			dp++;
530 
531 			kmem_free(tmpfap->fa_arg, tmpfap->fa_len);
532 			tmpfap++; argc++;
533 		}
534 		kmem_free(pack.ep_fa, pack.ep_fa_len);
535 		pack.ep_flags &= ~EXEC_HASARGL;
536 	}
537 
538 	/* Now get argv & environment */
539 	if (args == NULL) {
540 		DPRINTF(("execve: null args\n"));
541 		error = EINVAL;
542 		goto bad;
543 	}
544 	/* 'i' will index the argp/envp element to be retrieved */
545 	i = 0;
546 	if (pack.ep_flags & EXEC_SKIPARG)
547 		i++;
548 
549 	while (1) {
550 		len = argp + ARG_MAX - dp;
551 		if ((error = (*fetch_element)(args, i, &sp)) != 0) {
552 			DPRINTF(("execve: fetch_element args %d\n", error));
553 			goto bad;
554 		}
555 		if (!sp)
556 			break;
557 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
558 			DPRINTF(("execve: copyinstr args %d\n", error));
559 			if (error == ENAMETOOLONG)
560 				error = E2BIG;
561 			goto bad;
562 		}
563 		ktrexecarg(dp, len - 1);
564 		dp += len;
565 		i++;
566 		argc++;
567 	}
568 
569 	envc = 0;
570 	/* environment need not be there */
571 	if (envs != NULL) {
572 		i = 0;
573 		while (1) {
574 			len = argp + ARG_MAX - dp;
575 			if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
576 				DPRINTF(("execve: fetch_element env %d\n", error));
577 				goto bad;
578 			}
579 			if (!sp)
580 				break;
581 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
582 				DPRINTF(("execve: copyinstr env %d\n", error));
583 				if (error == ENAMETOOLONG)
584 					error = E2BIG;
585 				goto bad;
586 			}
587 			ktrexecenv(dp, len - 1);
588 			dp += len;
589 			i++;
590 			envc++;
591 		}
592 	}
593 
594 	dp = (char *) ALIGN(dp);
595 
596 	szsigcode = pack.ep_esch->es_emul->e_esigcode -
597 	    pack.ep_esch->es_emul->e_sigcode;
598 
599 #ifdef __MACHINE_STACK_GROWS_UP
600 /* See big comment lower down */
601 #define	RTLD_GAP	32
602 #else
603 #define	RTLD_GAP	0
604 #endif
605 
606 	/* Now check if args & environ fit into new stack */
607 	if (pack.ep_flags & EXEC_32)
608 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
609 		    sizeof(int) + sizeof(int) + dp + RTLD_GAP +
610 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
611 		    - argp;
612 	else
613 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
614 		    sizeof(char *) + sizeof(int) + dp + RTLD_GAP +
615 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
616 		    - argp;
617 
618 #ifdef PAX_ASLR
619 	if (pax_aslr_active(l))
620 		len += (arc4random() % PAGE_SIZE);
621 #endif /* PAX_ASLR */
622 
623 #ifdef STACKLALIGN	/* arm, etc. */
624 	len = STACKALIGN(len);	/* make the stack "safely" aligned */
625 #else
626 	len = ALIGN(len);	/* make the stack "safely" aligned */
627 #endif
628 
629 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
630 		DPRINTF(("execve: stack limit exceeded %zu\n", len));
631 		error = ENOMEM;
632 		goto bad;
633 	}
634 
635 	/* Get rid of other LWPs. */
636 	if (p->p_nlwps > 1) {
637 		mutex_enter(&p->p_smutex);
638 		exit_lwps(l);
639 		mutex_exit(&p->p_smutex);
640 	}
641 	KDASSERT(p->p_nlwps == 1);
642 
643 	/* Destroy any lwpctl info. */
644 	if (p->p_lwpctl != NULL)
645 		lwp_ctl_exit();
646 
647 	/* This is now LWP 1 */
648 	l->l_lid = 1;
649 	p->p_nlwpid = 1;
650 
651 	/* Remove POSIX timers */
652 	timers_free(p, TIMERS_POSIX);
653 
654 	/* adjust "active stack depth" for process VSZ */
655 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
656 
657 	/*
658 	 * Do whatever is necessary to prepare the address space
659 	 * for remapping.  Note that this might replace the current
660 	 * vmspace with another!
661 	 */
662 	uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
663 
664 	/* record proc's vnode, for use by procfs and others */
665         if (p->p_textvp)
666                 vrele(p->p_textvp);
667 	VREF(pack.ep_vp);
668 	p->p_textvp = pack.ep_vp;
669 
670 	/* Now map address space */
671 	vm = p->p_vmspace;
672 	vm->vm_taddr = (void *)pack.ep_taddr;
673 	vm->vm_tsize = btoc(pack.ep_tsize);
674 	vm->vm_daddr = (void*)pack.ep_daddr;
675 	vm->vm_dsize = btoc(pack.ep_dsize);
676 	vm->vm_ssize = btoc(pack.ep_ssize);
677 	vm->vm_maxsaddr = (void *)pack.ep_maxsaddr;
678 	vm->vm_minsaddr = (void *)pack.ep_minsaddr;
679 
680 #ifdef PAX_ASLR
681 	pax_aslr_init(l, vm);
682 #endif /* PAX_ASLR */
683 
684 	/* create the new process's VM space by running the vmcmds */
685 #ifdef DIAGNOSTIC
686 	if (pack.ep_vmcmds.evs_used == 0)
687 		panic("execve: no vmcmds");
688 #endif
689 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
690 		struct exec_vmcmd *vcp;
691 
692 		vcp = &pack.ep_vmcmds.evs_cmds[i];
693 		if (vcp->ev_flags & VMCMD_RELATIVE) {
694 #ifdef DIAGNOSTIC
695 			if (base_vcp == NULL)
696 				panic("execve: relative vmcmd with no base");
697 			if (vcp->ev_flags & VMCMD_BASE)
698 				panic("execve: illegal base & relative vmcmd");
699 #endif
700 			vcp->ev_addr += base_vcp->ev_addr;
701 		}
702 		error = (*vcp->ev_proc)(l, vcp);
703 #ifdef DEBUG_EXEC
704 		if (error) {
705 			size_t j;
706 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
707 			for (j = 0; j <= i; j++)
708 				uprintf(
709 			"vmcmd[%zu] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
710 				    j, vp[j].ev_addr, vp[j].ev_len,
711 				    vp[j].ev_offset, vp[j].ev_prot,
712 				    vp[j].ev_flags);
713 		}
714 #endif /* DEBUG_EXEC */
715 		if (vcp->ev_flags & VMCMD_BASE)
716 			base_vcp = vcp;
717 	}
718 
719 	/* free the vmspace-creation commands, and release their references */
720 	kill_vmcmds(&pack.ep_vmcmds);
721 
722 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
723 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
724 	vput(pack.ep_vp);
725 
726 	/* if an error happened, deallocate and punt */
727 	if (error) {
728 		DPRINTF(("execve: vmcmd %zu failed: %d\n", i - 1, error));
729 		goto exec_abort;
730 	}
731 
732 	/* remember information about the process */
733 	arginfo.ps_nargvstr = argc;
734 	arginfo.ps_nenvstr = envc;
735 
736 	/* set command name & other accounting info */
737 	i = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
738 	(void)memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, i);
739 	p->p_comm[i] = '\0';
740 
741 	dp = PNBUF_GET();
742 	/*
743 	 * If the path starts with /, we don't need to do any work.
744 	 * This handles the majority of the cases.
745 	 * In the future perhaps we could canonicalize it?
746 	 */
747 	if (pathbuf[0] == '/')
748 		(void)strlcpy(pack.ep_path = dp, pathbuf, MAXPATHLEN);
749 #ifdef notyet
750 	/*
751 	 * Although this works most of the time [since the entry was just
752 	 * entered in the cache] we don't use it because it theoretically
753 	 * can fail and it is not the cleanest interface, because there
754 	 * could be races. When the namei cache is re-written, this can
755 	 * be changed to use the appropriate function.
756 	 */
757 	else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p)))
758 		pack.ep_path = dp;
759 #endif
760 	else {
761 #ifdef notyet
762 		printf("Cannot get path for pid %d [%s] (error %d)",
763 		    (int)p->p_pid, p->p_comm, error);
764 #endif
765 		pack.ep_path = NULL;
766 		PNBUF_PUT(dp);
767 	}
768 
769 	stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
770 		STACK_PTHREADSPACE + sizeof(struct ps_strings) + szsigcode),
771 		len - (sizeof(struct ps_strings) + szsigcode));
772 
773 #ifdef __MACHINE_STACK_GROWS_UP
774 	/*
775 	 * The copyargs call always copies into lower addresses
776 	 * first, moving towards higher addresses, starting with
777 	 * the stack pointer that we give.  When the stack grows
778 	 * down, this puts argc/argv/envp very shallow on the
779 	 * stack, right at the first user stack pointer.
780 	 * When the stack grows up, the situation is reversed.
781 	 *
782 	 * Normally, this is no big deal.  But the ld_elf.so _rtld()
783 	 * function expects to be called with a single pointer to
784 	 * a region that has a few words it can stash values into,
785 	 * followed by argc/argv/envp.  When the stack grows down,
786 	 * it's easy to decrement the stack pointer a little bit to
787 	 * allocate the space for these few words and pass the new
788 	 * stack pointer to _rtld.  When the stack grows up, however,
789 	 * a few words before argc is part of the signal trampoline, XXX
790 	 * so we have a problem.
791 	 *
792 	 * Instead of changing how _rtld works, we take the easy way
793 	 * out and steal 32 bytes before we call copyargs.
794 	 * This extra space was allowed for when 'len' was calculated.
795 	 */
796 	stack += RTLD_GAP;
797 #endif /* __MACHINE_STACK_GROWS_UP */
798 
799 	/* Now copy argc, args & environ to new stack */
800 	error = (*pack.ep_esch->es_copyargs)(l, &pack, &arginfo, &stack, argp);
801 	if (pack.ep_path) {
802 		PNBUF_PUT(pack.ep_path);
803 		pack.ep_path = NULL;
804 	}
805 	if (error) {
806 		DPRINTF(("execve: copyargs failed %d\n", error));
807 		goto exec_abort;
808 	}
809 	/* Move the stack back to original point */
810 	stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
811 
812 	/* fill process ps_strings info */
813 	p->p_psstr = (struct ps_strings *)
814 	    STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, STACK_PTHREADSPACE),
815 	    sizeof(struct ps_strings));
816 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
817 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
818 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
819 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
820 
821 	/* copy out the process's ps_strings structure */
822 	if ((error = copyout(aip, (char *)p->p_psstr,
823 	    sizeof(arginfo))) != 0) {
824 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
825 		       aip, (char *)p->p_psstr, (long)sizeof(arginfo)));
826 		goto exec_abort;
827 	}
828 
829 	fdcloseexec(l);		/* handle close on exec */
830 	execsigs(p);		/* reset catched signals */
831 
832 	l->l_ctxlink = NULL;	/* reset ucontext link */
833 
834 
835 	p->p_acflag &= ~AFORK;
836 	mutex_enter(&p->p_mutex);
837 	p->p_flag |= PK_EXEC;
838 	mutex_exit(&p->p_mutex);
839 
840 	/*
841 	 * Stop profiling.
842 	 */
843 	if ((p->p_stflag & PST_PROFIL) != 0) {
844 		mutex_spin_enter(&p->p_stmutex);
845 		stopprofclock(p);
846 		mutex_spin_exit(&p->p_stmutex);
847 	}
848 
849 	/*
850 	 * It's OK to test PS_PPWAIT unlocked here, as other LWPs have
851 	 * exited and exec()/exit() are the only places it will be cleared.
852 	 */
853 	if ((p->p_sflag & PS_PPWAIT) != 0) {
854 		mutex_enter(&proclist_lock);
855 		mutex_enter(&p->p_smutex);
856 		p->p_sflag &= ~PS_PPWAIT;
857 		cv_broadcast(&p->p_pptr->p_waitcv);
858 		mutex_exit(&p->p_smutex);
859 		mutex_exit(&proclist_lock);
860 	}
861 
862 	/*
863 	 * Deal with set[ug]id.  MNT_NOSUID has already been used to disable
864 	 * s[ug]id.  It's OK to check for PSL_TRACED here as we have blocked
865 	 * out additional references on the process for the moment.
866 	 */
867 	if ((p->p_slflag & PSL_TRACED) == 0 &&
868 
869 	    (((attr.va_mode & S_ISUID) != 0 &&
870 	      kauth_cred_geteuid(l->l_cred) != attr.va_uid) ||
871 
872 	     ((attr.va_mode & S_ISGID) != 0 &&
873 	      kauth_cred_getegid(l->l_cred) != attr.va_gid))) {
874 		/*
875 		 * Mark the process as SUGID before we do
876 		 * anything that might block.
877 		 */
878 		proc_crmod_enter();
879 		proc_crmod_leave(NULL, NULL, true);
880 
881 		/* Make sure file descriptors 0..2 are in use. */
882 		if ((error = fdcheckstd(l)) != 0) {
883 			DPRINTF(("execve: fdcheckstd failed %d\n", error));
884 			goto exec_abort;
885 		}
886 
887 		/*
888 		 * Copy the credential so other references don't see our
889 		 * changes.
890 		 */
891 		l->l_cred = kauth_cred_copy(l->l_cred);
892 #ifdef KTRACE
893 		/*
894 		 * If the persistent trace flag isn't set, turn off.
895 		 */
896 		if (p->p_tracep) {
897 			mutex_enter(&ktrace_lock);
898 			if (!(p->p_traceflag & KTRFAC_PERSISTENT))
899 				ktrderef(p);
900 			mutex_exit(&ktrace_lock);
901 		}
902 #endif
903 		if (attr.va_mode & S_ISUID)
904 			kauth_cred_seteuid(l->l_cred, attr.va_uid);
905 		if (attr.va_mode & S_ISGID)
906 			kauth_cred_setegid(l->l_cred, attr.va_gid);
907 	} else {
908 		if (kauth_cred_geteuid(l->l_cred) ==
909 		    kauth_cred_getuid(l->l_cred) &&
910 		    kauth_cred_getegid(l->l_cred) ==
911 		    kauth_cred_getgid(l->l_cred))
912 			p->p_flag &= ~PK_SUGID;
913 	}
914 
915 	/*
916 	 * Copy the credential so other references don't see our changes.
917 	 * Test to see if this is necessary first, since in the common case
918 	 * we won't need a private reference.
919 	 */
920 	if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
921 	    kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
922 		l->l_cred = kauth_cred_copy(l->l_cred);
923 		kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
924 		kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
925 	}
926 
927 	/* Update the master credentials. */
928 	if (l->l_cred != p->p_cred) {
929 		kauth_cred_t ocred;
930 
931 		kauth_cred_hold(l->l_cred);
932 		mutex_enter(&p->p_mutex);
933 		ocred = p->p_cred;
934 		p->p_cred = l->l_cred;
935 		mutex_exit(&p->p_mutex);
936 		kauth_cred_free(ocred);
937 	}
938 
939 #if defined(__HAVE_RAS)
940 	/*
941 	 * Remove all RASs from the address space.
942 	 */
943 	ras_purgeall();
944 #endif
945 
946 	doexechooks(p);
947 
948 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
949 
950 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
951 
952 	/* notify others that we exec'd */
953 	KNOTE(&p->p_klist, NOTE_EXEC);
954 
955 	/* setup new registers and do misc. setup. */
956 	(*pack.ep_esch->es_emul->e_setregs)(l, &pack, (u_long) stack);
957 	if (pack.ep_esch->es_setregs)
958 		(*pack.ep_esch->es_setregs)(l, &pack, (u_long) stack);
959 
960 	/* map the process's signal trampoline code */
961 	if (exec_sigcode_map(p, pack.ep_esch->es_emul)) {
962 		DPRINTF(("execve: map sigcode failed %d\n", error));
963 		goto exec_abort;
964 	}
965 
966 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
967 
968 	/* The emulation root will usually have been found when we looked
969 	 * for the elf interpreter (or similar), if not look now. */
970 	if (pack.ep_esch->es_emul->e_path != NULL && pack.ep_emul_root == NULL)
971 		emul_find_root(l, &pack);
972 
973 	/* Any old emulation root got removed by fdcloseexec */
974 	rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER);
975 	p->p_cwdi->cwdi_edir = pack.ep_emul_root;
976 	rw_exit(&p->p_cwdi->cwdi_lock);
977 	pack.ep_emul_root = NULL;
978 	if (pack.ep_interp != NULL)
979 		vrele(pack.ep_interp);
980 
981 	/*
982 	 * Call emulation specific exec hook. This can setup per-process
983 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
984 	 *
985 	 * If we are executing process of different emulation than the
986 	 * original forked process, call e_proc_exit() of the old emulation
987 	 * first, then e_proc_exec() of new emulation. If the emulation is
988 	 * same, the exec hook code should deallocate any old emulation
989 	 * resources held previously by this process.
990 	 */
991 	if (p->p_emul && p->p_emul->e_proc_exit
992 	    && p->p_emul != pack.ep_esch->es_emul)
993 		(*p->p_emul->e_proc_exit)(p);
994 
995 	/*
996 	 * Call exec hook. Emulation code may NOT store reference to anything
997 	 * from &pack.
998 	 */
999         if (pack.ep_esch->es_emul->e_proc_exec)
1000                 (*pack.ep_esch->es_emul->e_proc_exec)(p, &pack);
1001 
1002 	/* update p_emul, the old value is no longer needed */
1003 	p->p_emul = pack.ep_esch->es_emul;
1004 
1005 	/* ...and the same for p_execsw */
1006 	p->p_execsw = pack.ep_esch;
1007 
1008 #ifdef __HAVE_SYSCALL_INTERN
1009 	(*p->p_emul->e_syscall_intern)(p);
1010 #endif
1011 	ktremul();
1012 
1013 	/* Allow new references from the debugger/procfs. */
1014 	rw_exit(&p->p_reflock);
1015 #ifdef LKM
1016 	rw_exit(&exec_lock);
1017 #endif
1018 
1019 	mutex_enter(&proclist_mutex);
1020 
1021 	if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
1022 		KSI_INIT_EMPTY(&ksi);
1023 		ksi.ksi_signo = SIGTRAP;
1024 		ksi.ksi_lid = l->l_lid;
1025 		kpsignal(p, &ksi, NULL);
1026 	}
1027 
1028 	if (p->p_sflag & PS_STOPEXEC) {
1029 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
1030 		p->p_pptr->p_nstopchild++;
1031 		p->p_pptr->p_waited = 0;
1032 		mutex_enter(&p->p_smutex);
1033 		ksiginfo_queue_init(&kq);
1034 		sigclearall(p, &contsigmask, &kq);
1035 		lwp_lock(l);
1036 		l->l_stat = LSSTOP;
1037 		p->p_stat = SSTOP;
1038 		p->p_nrlwps--;
1039 		mutex_exit(&p->p_smutex);
1040 		mutex_exit(&proclist_mutex);
1041 		mi_switch(l);
1042 		ksiginfo_queue_drain(&kq);
1043 		KERNEL_LOCK(l->l_biglocks, l);
1044 	} else {
1045 		mutex_exit(&proclist_mutex);
1046 	}
1047 
1048 	PNBUF_PUT(pathbuf);
1049 	return (EJUSTRETURN);
1050 
1051  bad:
1052 	/* free the vmspace-creation commands, and release their references */
1053 	kill_vmcmds(&pack.ep_vmcmds);
1054 	/* kill any opened file descriptor, if necessary */
1055 	if (pack.ep_flags & EXEC_HASFD) {
1056 		pack.ep_flags &= ~EXEC_HASFD;
1057 		(void) fdrelease(l, pack.ep_fd);
1058 	}
1059 	/* close and put the exec'd file */
1060 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1061 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
1062 	vput(pack.ep_vp);
1063 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1064 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
1065 
1066  freehdr:
1067 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
1068 	if (pack.ep_emul_root != NULL)
1069 		vrele(pack.ep_emul_root);
1070 	if (pack.ep_interp != NULL)
1071 		vrele(pack.ep_interp);
1072 
1073  clrflg:
1074 	PNBUF_PUT(pathbuf);
1075 	rw_exit(&p->p_reflock);
1076 #ifdef LKM
1077 	rw_exit(&exec_lock);
1078 #endif
1079 
1080 	return error;
1081 
1082  exec_abort:
1083 	PNBUF_PUT(pathbuf);
1084 	rw_exit(&p->p_reflock);
1085 #ifdef LKM
1086 	rw_exit(&exec_lock);
1087 #endif
1088 
1089 	/*
1090 	 * the old process doesn't exist anymore.  exit gracefully.
1091 	 * get rid of the (new) address space we have created, if any, get rid
1092 	 * of our namei data and vnode, and exit noting failure
1093 	 */
1094 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
1095 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
1096 	if (pack.ep_emul_arg)
1097 		FREE(pack.ep_emul_arg, M_TEMP);
1098 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1099 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
1100 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
1101 	if (pack.ep_emul_root != NULL)
1102 		vrele(pack.ep_emul_root);
1103 	if (pack.ep_interp != NULL)
1104 		vrele(pack.ep_interp);
1105 
1106 	/* Acquire the sched-state mutex (exit1() will release it). */
1107 	KERNEL_LOCK(1, NULL);	/* XXXSMP */
1108 	mutex_enter(&p->p_smutex);
1109 	exit1(l, W_EXITCODE(error, SIGABRT));
1110 
1111 	/* NOTREACHED */
1112 	return 0;
1113 }
1114 
1115 
1116 int
1117 copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo,
1118     char **stackp, void *argp)
1119 {
1120 	char	**cpp, *dp, *sp;
1121 	size_t	len;
1122 	void	*nullp;
1123 	long	argc, envc;
1124 	int	error;
1125 
1126 	cpp = (char **)*stackp;
1127 	nullp = NULL;
1128 	argc = arginfo->ps_nargvstr;
1129 	envc = arginfo->ps_nenvstr;
1130 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
1131 		return error;
1132 
1133 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
1134 	sp = argp;
1135 
1136 	/* XXX don't copy them out, remap them! */
1137 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1138 
1139 	for (; --argc >= 0; sp += len, dp += len)
1140 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
1141 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
1142 			return error;
1143 
1144 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
1145 		return error;
1146 
1147 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
1148 
1149 	for (; --envc >= 0; sp += len, dp += len)
1150 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
1151 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
1152 			return error;
1153 
1154 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
1155 		return error;
1156 
1157 	*stackp = (char *)cpp;
1158 	return 0;
1159 }
1160 
1161 #ifdef LKM
1162 /*
1163  * Find an emulation of given name in list of emulations.
1164  * Needs to be called with the exec_lock held.
1165  */
1166 const struct emul *
1167 emul_search(const char *name)
1168 {
1169 	struct emul_entry *it;
1170 
1171 	LIST_FOREACH(it, &el_head, el_list) {
1172 		if (strcmp(name, it->el_emul->e_name) == 0)
1173 			return it->el_emul;
1174 	}
1175 
1176 	return NULL;
1177 }
1178 
1179 /*
1180  * Add an emulation to list, if it's not there already.
1181  */
1182 int
1183 emul_register(const struct emul *emul, int ro_entry)
1184 {
1185 	struct emul_entry	*ee;
1186 	int			error;
1187 
1188 	error = 0;
1189 	rw_enter(&exec_lock, RW_WRITER);
1190 
1191 	if (emul_search(emul->e_name)) {
1192 		error = EEXIST;
1193 		goto out;
1194 	}
1195 
1196 	ee = kmem_alloc(sizeof(*ee), KM_SLEEP);
1197 	ee->el_emul = emul;
1198 	ee->ro_entry = ro_entry;
1199 	LIST_INSERT_HEAD(&el_head, ee, el_list);
1200 
1201  out:
1202 	rw_exit(&exec_lock);
1203 	return error;
1204 }
1205 
1206 /*
1207  * Remove emulation with name 'name' from list of supported emulations.
1208  */
1209 int
1210 emul_unregister(const char *name)
1211 {
1212 	const struct proclist_desc *pd;
1213 	struct emul_entry	*it;
1214 	int			i, error;
1215 	struct proc		*ptmp;
1216 
1217 	error = 0;
1218 	rw_enter(&exec_lock, RW_WRITER);
1219 
1220 	LIST_FOREACH(it, &el_head, el_list) {
1221 		if (strcmp(it->el_emul->e_name, name) == 0)
1222 			break;
1223 	}
1224 
1225 	if (!it) {
1226 		error = ENOENT;
1227 		goto out;
1228 	}
1229 
1230 	if (it->ro_entry) {
1231 		error = EBUSY;
1232 		goto out;
1233 	}
1234 
1235 	/* test if any execw[] entry is still using this */
1236 	for(i=0; i < nexecs; i++) {
1237 		if (execsw[i]->es_emul == it->el_emul) {
1238 			error = EBUSY;
1239 			goto out;
1240 		}
1241 	}
1242 
1243 	/*
1244 	 * Test if any process is running under this emulation - since
1245 	 * emul_unregister() is running quite sendomly, it's better
1246 	 * to do expensive check here than to use any locking.
1247 	 */
1248 	mutex_enter(&proclist_lock);
1249 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
1250 		PROCLIST_FOREACH(ptmp, pd->pd_list) {
1251 			if (ptmp->p_emul == it->el_emul) {
1252 				error = EBUSY;
1253 				break;
1254 			}
1255 		}
1256 	}
1257 	mutex_exit(&proclist_lock);
1258 
1259 	if (error)
1260 		goto out;
1261 
1262 
1263 	/* entry is not used, remove it */
1264 	LIST_REMOVE(it, el_list);
1265 	kmem_free(it, sizeof(*it));
1266 
1267  out:
1268 	rw_exit(&exec_lock);
1269 	return error;
1270 }
1271 
1272 /*
1273  * Add execsw[] entry.
1274  */
1275 int
1276 exec_add(struct execsw *esp, const char *e_name)
1277 {
1278 	struct exec_entry	*it;
1279 	int			error;
1280 
1281 	error = 0;
1282 	rw_enter(&exec_lock, RW_WRITER);
1283 
1284 	if (!esp->es_emul) {
1285 		esp->es_emul = emul_search(e_name);
1286 		if (!esp->es_emul) {
1287 			error = ENOENT;
1288 			goto out;
1289 		}
1290 	}
1291 
1292 	LIST_FOREACH(it, &ex_head, ex_list) {
1293 		/* assume tuple (makecmds, probe_func, emulation) is unique */
1294 		if (it->es->es_makecmds == esp->es_makecmds
1295 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
1296 		    && it->es->es_emul == esp->es_emul) {
1297 			error = EEXIST;
1298 			goto out;
1299 		}
1300 	}
1301 
1302 	/* if we got here, the entry doesn't exist yet */
1303 	it = kmem_alloc(sizeof(*it), KM_SLEEP);
1304 	it->es = esp;
1305 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
1306 
1307 	/* update execsw[] */
1308 	exec_init(0);
1309 
1310  out:
1311 	rw_exit(&exec_lock);
1312 	return error;
1313 }
1314 
1315 /*
1316  * Remove execsw[] entry.
1317  */
1318 int
1319 exec_remove(const struct execsw *esp)
1320 {
1321 	struct exec_entry	*it;
1322 	int			error;
1323 
1324 	error = 0;
1325 	rw_enter(&exec_lock, RW_WRITER);
1326 
1327 	LIST_FOREACH(it, &ex_head, ex_list) {
1328 		/* assume tuple (makecmds, probe_func, emulation) is unique */
1329 		if (it->es->es_makecmds == esp->es_makecmds
1330 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
1331 		    && it->es->es_emul == esp->es_emul)
1332 			break;
1333 	}
1334 	if (!it) {
1335 		error = ENOENT;
1336 		goto out;
1337 	}
1338 
1339 	/* remove item from list and free resources */
1340 	LIST_REMOVE(it, ex_list);
1341 	kmem_free(it, sizeof(*it));
1342 
1343 	/* update execsw[] */
1344 	exec_init(0);
1345 
1346  out:
1347 	rw_exit(&exec_lock);
1348 	return error;
1349 }
1350 
1351 static void
1352 link_es(struct execsw_entry **listp, const struct execsw *esp)
1353 {
1354 	struct execsw_entry *et, *e1;
1355 
1356 	et = (struct execsw_entry *) malloc(sizeof(struct execsw_entry),
1357 			M_TEMP, M_WAITOK);
1358 	et->next = NULL;
1359 	et->es = esp;
1360 	if (*listp == NULL) {
1361 		*listp = et;
1362 		return;
1363 	}
1364 
1365 	switch(et->es->es_prio) {
1366 	case EXECSW_PRIO_FIRST:
1367 		/* put new entry as the first */
1368 		et->next = *listp;
1369 		*listp = et;
1370 		break;
1371 	case EXECSW_PRIO_ANY:
1372 		/* put new entry after all *_FIRST and *_ANY entries */
1373 		for(e1 = *listp; e1->next
1374 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
1375 			e1 = e1->next);
1376 		et->next = e1->next;
1377 		e1->next = et;
1378 		break;
1379 	case EXECSW_PRIO_LAST:
1380 		/* put new entry as the last one */
1381 		for(e1 = *listp; e1->next; e1 = e1->next);
1382 		e1->next = et;
1383 		break;
1384 	default:
1385 #ifdef DIAGNOSTIC
1386 		panic("execw[] entry with unknown priority %d found",
1387 			et->es->es_prio);
1388 #else
1389 		free(et, M_TEMP);
1390 #endif
1391 		break;
1392 	}
1393 }
1394 
1395 /*
1396  * Initialize exec structures. If init_boot is true, also does necessary
1397  * one-time initialization (it's called from main() that way).
1398  * Once system is multiuser, this should be called with exec_lock held,
1399  * i.e. via exec_{add|remove}().
1400  */
1401 int
1402 exec_init(int init_boot)
1403 {
1404 	const struct execsw	**new_es, * const *old_es;
1405 	struct execsw_entry	*list, *e1;
1406 	struct exec_entry	*e2;
1407 	int			i, es_sz;
1408 
1409 	if (init_boot) {
1410 		/* do one-time initializations */
1411 		rw_init(&exec_lock);
1412 		mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE);
1413 
1414 		/* register compiled-in emulations */
1415 		for(i=0; i < nexecs_builtin; i++) {
1416 			if (execsw_builtin[i].es_emul)
1417 				emul_register(execsw_builtin[i].es_emul, 1);
1418 		}
1419 #ifdef DIAGNOSTIC
1420 		if (i == 0)
1421 			panic("no emulations found in execsw_builtin[]");
1422 #endif
1423 	}
1424 
1425 	/*
1426 	 * Build execsw[] array from builtin entries and entries added
1427 	 * at runtime.
1428 	 */
1429 	list = NULL;
1430 	for(i=0; i < nexecs_builtin; i++)
1431 		link_es(&list, &execsw_builtin[i]);
1432 
1433 	/* Add dynamically loaded entries */
1434 	es_sz = nexecs_builtin;
1435 	LIST_FOREACH(e2, &ex_head, ex_list) {
1436 		link_es(&list, e2->es);
1437 		es_sz++;
1438 	}
1439 
1440 	/*
1441 	 * Now that we have sorted all execw entries, create new execsw[]
1442 	 * and free no longer needed memory in the process.
1443 	 */
1444 	new_es = kmem_alloc(es_sz * sizeof(struct execsw *), KM_SLEEP);
1445 	for(i=0; list; i++) {
1446 		new_es[i] = list->es;
1447 		e1 = list->next;
1448 		free(list, M_TEMP);
1449 		list = e1;
1450 	}
1451 
1452 	/*
1453 	 * New execsw[] array built, now replace old execsw[] and free
1454 	 * used memory.
1455 	 */
1456 	old_es = execsw;
1457 	if (old_es)
1458 		/*XXXUNCONST*/
1459 		kmem_free(__UNCONST(old_es), nexecs * sizeof(struct execsw *));
1460 	execsw = new_es;
1461 	nexecs = es_sz;
1462 
1463 	/*
1464 	 * Figure out the maximum size of an exec header.
1465 	 */
1466 	exec_maxhdrsz = 0;
1467 	for (i = 0; i < nexecs; i++) {
1468 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1469 			exec_maxhdrsz = execsw[i]->es_hdrsz;
1470 	}
1471 
1472 	return 0;
1473 }
1474 #endif
1475 
1476 #ifndef LKM
1477 /*
1478  * Simplified exec_init() for kernels without LKMs. Only initialize
1479  * exec_maxhdrsz and execsw[].
1480  */
1481 int
1482 exec_init(int init_boot)
1483 {
1484 	int i;
1485 
1486 #ifdef DIAGNOSTIC
1487 	if (!init_boot)
1488 		panic("exec_init(): called with init_boot == 0");
1489 #endif
1490 
1491 	/* do one-time initializations */
1492 	nexecs = nexecs_builtin;
1493 	execsw = kmem_alloc(nexecs * sizeof(struct execsw *), KM_SLEEP);
1494 
1495 	/*
1496 	 * Fill in execsw[] and figure out the maximum size of an exec header.
1497 	 */
1498 	exec_maxhdrsz = 0;
1499 	for(i=0; i < nexecs; i++) {
1500 		execsw[i] = &execsw_builtin[i];
1501 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1502 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1503 	}
1504 
1505 	return 0;
1506 
1507 }
1508 #endif /* !LKM */
1509 
1510 static int
1511 exec_sigcode_map(struct proc *p, const struct emul *e)
1512 {
1513 	vaddr_t va;
1514 	vsize_t sz;
1515 	int error;
1516 	struct uvm_object *uobj;
1517 
1518 	sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
1519 
1520 	if (e->e_sigobject == NULL || sz == 0) {
1521 		return 0;
1522 	}
1523 
1524 	/*
1525 	 * If we don't have a sigobject for this emulation, create one.
1526 	 *
1527 	 * sigobject is an anonymous memory object (just like SYSV shared
1528 	 * memory) that we keep a permanent reference to and that we map
1529 	 * in all processes that need this sigcode. The creation is simple,
1530 	 * we create an object, add a permanent reference to it, map it in
1531 	 * kernel space, copy out the sigcode to it and unmap it.
1532 	 * We map it with PROT_READ|PROT_EXEC into the process just
1533 	 * the way sys_mmap() would map it.
1534 	 */
1535 
1536 	uobj = *e->e_sigobject;
1537 	if (uobj == NULL) {
1538 		mutex_enter(&sigobject_lock);
1539 		if ((uobj = *e->e_sigobject) == NULL) {
1540 			uobj = uao_create(sz, 0);
1541 			(*uobj->pgops->pgo_reference)(uobj);
1542 			va = vm_map_min(kernel_map);
1543 			if ((error = uvm_map(kernel_map, &va, round_page(sz),
1544 			    uobj, 0, 0,
1545 			    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1546 			    UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
1547 				printf("kernel mapping failed %d\n", error);
1548 				(*uobj->pgops->pgo_detach)(uobj);
1549 				mutex_exit(&sigobject_lock);
1550 				return (error);
1551 			}
1552 			memcpy((void *)va, e->e_sigcode, sz);
1553 #ifdef PMAP_NEED_PROCWR
1554 			pmap_procwr(&proc0, va, sz);
1555 #endif
1556 			uvm_unmap(kernel_map, va, va + round_page(sz));
1557 			*e->e_sigobject = uobj;
1558 		}
1559 		mutex_exit(&sigobject_lock);
1560 	}
1561 
1562 	/* Just a hint to uvm_map where to put it. */
1563 	va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
1564 	    round_page(sz));
1565 
1566 #ifdef __alpha__
1567 	/*
1568 	 * Tru64 puts /sbin/loader at the end of user virtual memory,
1569 	 * which causes the above calculation to put the sigcode at
1570 	 * an invalid address.  Put it just below the text instead.
1571 	 */
1572 	if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) {
1573 		va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz);
1574 	}
1575 #endif
1576 
1577 	(*uobj->pgops->pgo_reference)(uobj);
1578 	error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
1579 			uobj, 0, 0,
1580 			UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
1581 				    UVM_ADV_RANDOM, 0));
1582 	if (error) {
1583 		(*uobj->pgops->pgo_detach)(uobj);
1584 		return (error);
1585 	}
1586 	p->p_sigctx.ps_sigcode = (void *)va;
1587 	return (0);
1588 }
1589