Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.273
      1 /*	$NetBSD: kern_exec.c,v 1.273 2008/06/04 12:16:06 ad 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.273 2008/06/04 12:16:06 ad 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(l->l_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 	pack.ep_pax_flags = 0;
    496 
    497 #ifdef LKM
    498 	rw_enter(&exec_lock, RW_READER);
    499 #endif
    500 
    501 	/* see if we can run it. */
    502 	if ((error = check_exec(l, &pack)) != 0) {
    503 		if (error != ENOENT) {
    504 			DPRINTF(("execve: check exec failed %d\n", error));
    505 		}
    506 		goto freehdr;
    507 	}
    508 
    509 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    510 
    511 	/* allocate an argument buffer */
    512 	argp = (char *) uvm_km_alloc(exec_map, NCARGS, 0,
    513 	    UVM_KMF_PAGEABLE|UVM_KMF_WAITVA);
    514 #ifdef DIAGNOSTIC
    515 	if (argp == NULL)
    516 		panic("execve: argp == NULL");
    517 #endif
    518 	dp = argp;
    519 	argc = 0;
    520 
    521 	/* copy the fake args list, if there's one, freeing it as we go */
    522 	if (pack.ep_flags & EXEC_HASARGL) {
    523 		tmpfap = pack.ep_fa;
    524 		while (tmpfap->fa_arg != NULL) {
    525 			const char *cp;
    526 
    527 			cp = tmpfap->fa_arg;
    528 			while (*cp)
    529 				*dp++ = *cp++;
    530 			dp++;
    531 
    532 			kmem_free(tmpfap->fa_arg, tmpfap->fa_len);
    533 			tmpfap++; argc++;
    534 		}
    535 		kmem_free(pack.ep_fa, pack.ep_fa_len);
    536 		pack.ep_flags &= ~EXEC_HASARGL;
    537 	}
    538 
    539 	/* Now get argv & environment */
    540 	if (args == NULL) {
    541 		DPRINTF(("execve: null args\n"));
    542 		error = EINVAL;
    543 		goto bad;
    544 	}
    545 	/* 'i' will index the argp/envp element to be retrieved */
    546 	i = 0;
    547 	if (pack.ep_flags & EXEC_SKIPARG)
    548 		i++;
    549 
    550 	while (1) {
    551 		len = argp + ARG_MAX - dp;
    552 		if ((error = (*fetch_element)(args, i, &sp)) != 0) {
    553 			DPRINTF(("execve: fetch_element args %d\n", error));
    554 			goto bad;
    555 		}
    556 		if (!sp)
    557 			break;
    558 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    559 			DPRINTF(("execve: copyinstr args %d\n", error));
    560 			if (error == ENAMETOOLONG)
    561 				error = E2BIG;
    562 			goto bad;
    563 		}
    564 		ktrexecarg(dp, len - 1);
    565 		dp += len;
    566 		i++;
    567 		argc++;
    568 	}
    569 
    570 	envc = 0;
    571 	/* environment need not be there */
    572 	if (envs != NULL) {
    573 		i = 0;
    574 		while (1) {
    575 			len = argp + ARG_MAX - dp;
    576 			if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
    577 				DPRINTF(("execve: fetch_element env %d\n", error));
    578 				goto bad;
    579 			}
    580 			if (!sp)
    581 				break;
    582 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    583 				DPRINTF(("execve: copyinstr env %d\n", error));
    584 				if (error == ENAMETOOLONG)
    585 					error = E2BIG;
    586 				goto bad;
    587 			}
    588 			ktrexecenv(dp, len - 1);
    589 			dp += len;
    590 			i++;
    591 			envc++;
    592 		}
    593 	}
    594 
    595 	dp = (char *) ALIGN(dp);
    596 
    597 	szsigcode = pack.ep_esch->es_emul->e_esigcode -
    598 	    pack.ep_esch->es_emul->e_sigcode;
    599 
    600 #ifdef __MACHINE_STACK_GROWS_UP
    601 /* See big comment lower down */
    602 #define	RTLD_GAP	32
    603 #else
    604 #define	RTLD_GAP	0
    605 #endif
    606 
    607 	/* Now check if args & environ fit into new stack */
    608 	if (pack.ep_flags & EXEC_32)
    609 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
    610 		    sizeof(int) + sizeof(int) + dp + RTLD_GAP +
    611 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
    612 		    - argp;
    613 	else
    614 		len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
    615 		    sizeof(char *) + sizeof(int) + dp + RTLD_GAP +
    616 		    szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
    617 		    - argp;
    618 
    619 #ifdef PAX_ASLR
    620 	if (pax_aslr_active(l))
    621 		len += (arc4random() % PAGE_SIZE);
    622 #endif /* PAX_ASLR */
    623 
    624 #ifdef STACKLALIGN	/* arm, etc. */
    625 	len = STACKALIGN(len);	/* make the stack "safely" aligned */
    626 #else
    627 	len = ALIGN(len);	/* make the stack "safely" aligned */
    628 #endif
    629 
    630 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    631 		DPRINTF(("execve: stack limit exceeded %zu\n", len));
    632 		error = ENOMEM;
    633 		goto bad;
    634 	}
    635 
    636 	/* Get rid of other LWPs. */
    637 	if (p->p_nlwps > 1) {
    638 		mutex_enter(p->p_lock);
    639 		exit_lwps(l);
    640 		mutex_exit(p->p_lock);
    641 	}
    642 	KDASSERT(p->p_nlwps == 1);
    643 
    644 	/* Destroy any lwpctl info. */
    645 	if (p->p_lwpctl != NULL)
    646 		lwp_ctl_exit();
    647 
    648 	/* This is now LWP 1 */
    649 	l->l_lid = 1;
    650 	p->p_nlwpid = 1;
    651 
    652 	/* Remove POSIX timers */
    653 	timers_free(p, TIMERS_POSIX);
    654 
    655 	/* adjust "active stack depth" for process VSZ */
    656 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    657 
    658 	/*
    659 	 * Do whatever is necessary to prepare the address space
    660 	 * for remapping.  Note that this might replace the current
    661 	 * vmspace with another!
    662 	 */
    663 	uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
    664 
    665 	/* record proc's vnode, for use by procfs and others */
    666         if (p->p_textvp)
    667                 vrele(p->p_textvp);
    668 	VREF(pack.ep_vp);
    669 	p->p_textvp = pack.ep_vp;
    670 
    671 	/* Now map address space */
    672 	vm = p->p_vmspace;
    673 	vm->vm_taddr = (void *)pack.ep_taddr;
    674 	vm->vm_tsize = btoc(pack.ep_tsize);
    675 	vm->vm_daddr = (void*)pack.ep_daddr;
    676 	vm->vm_dsize = btoc(pack.ep_dsize);
    677 	vm->vm_ssize = btoc(pack.ep_ssize);
    678 	vm->vm_maxsaddr = (void *)pack.ep_maxsaddr;
    679 	vm->vm_minsaddr = (void *)pack.ep_minsaddr;
    680 
    681 #ifdef PAX_ASLR
    682 	pax_aslr_init(l, vm);
    683 #endif /* PAX_ASLR */
    684 
    685 	/* create the new process's VM space by running the vmcmds */
    686 #ifdef DIAGNOSTIC
    687 	if (pack.ep_vmcmds.evs_used == 0)
    688 		panic("execve: no vmcmds");
    689 #endif
    690 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    691 		struct exec_vmcmd *vcp;
    692 
    693 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    694 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    695 #ifdef DIAGNOSTIC
    696 			if (base_vcp == NULL)
    697 				panic("execve: relative vmcmd with no base");
    698 			if (vcp->ev_flags & VMCMD_BASE)
    699 				panic("execve: illegal base & relative vmcmd");
    700 #endif
    701 			vcp->ev_addr += base_vcp->ev_addr;
    702 		}
    703 		error = (*vcp->ev_proc)(l, vcp);
    704 #ifdef DEBUG_EXEC
    705 		if (error) {
    706 			size_t j;
    707 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
    708 			for (j = 0; j <= i; j++)
    709 				uprintf(
    710 			"vmcmd[%zu] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
    711 				    j, vp[j].ev_addr, vp[j].ev_len,
    712 				    vp[j].ev_offset, vp[j].ev_prot,
    713 				    vp[j].ev_flags);
    714 		}
    715 #endif /* DEBUG_EXEC */
    716 		if (vcp->ev_flags & VMCMD_BASE)
    717 			base_vcp = vcp;
    718 	}
    719 
    720 	/* free the vmspace-creation commands, and release their references */
    721 	kill_vmcmds(&pack.ep_vmcmds);
    722 
    723 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    724 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
    725 	vput(pack.ep_vp);
    726 
    727 	/* if an error happened, deallocate and punt */
    728 	if (error) {
    729 		DPRINTF(("execve: vmcmd %zu failed: %d\n", i - 1, error));
    730 		goto exec_abort;
    731 	}
    732 
    733 	/* remember information about the process */
    734 	arginfo.ps_nargvstr = argc;
    735 	arginfo.ps_nenvstr = envc;
    736 
    737 	/* set command name & other accounting info */
    738 	i = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    739 	(void)memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, i);
    740 	p->p_comm[i] = '\0';
    741 
    742 	dp = PNBUF_GET();
    743 	/*
    744 	 * If the path starts with /, we don't need to do any work.
    745 	 * This handles the majority of the cases.
    746 	 * In the future perhaps we could canonicalize it?
    747 	 */
    748 	if (pathbuf[0] == '/')
    749 		(void)strlcpy(pack.ep_path = dp, pathbuf, MAXPATHLEN);
    750 #ifdef notyet
    751 	/*
    752 	 * Although this works most of the time [since the entry was just
    753 	 * entered in the cache] we don't use it because it theoretically
    754 	 * can fail and it is not the cleanest interface, because there
    755 	 * could be races. When the namei cache is re-written, this can
    756 	 * be changed to use the appropriate function.
    757 	 */
    758 	else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p)))
    759 		pack.ep_path = dp;
    760 #endif
    761 	else {
    762 #ifdef notyet
    763 		printf("Cannot get path for pid %d [%s] (error %d)",
    764 		    (int)p->p_pid, p->p_comm, error);
    765 #endif
    766 		pack.ep_path = NULL;
    767 		PNBUF_PUT(dp);
    768 	}
    769 
    770 	stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
    771 		STACK_PTHREADSPACE + sizeof(struct ps_strings) + szsigcode),
    772 		len - (sizeof(struct ps_strings) + szsigcode));
    773 
    774 #ifdef __MACHINE_STACK_GROWS_UP
    775 	/*
    776 	 * The copyargs call always copies into lower addresses
    777 	 * first, moving towards higher addresses, starting with
    778 	 * the stack pointer that we give.  When the stack grows
    779 	 * down, this puts argc/argv/envp very shallow on the
    780 	 * stack, right at the first user stack pointer.
    781 	 * When the stack grows up, the situation is reversed.
    782 	 *
    783 	 * Normally, this is no big deal.  But the ld_elf.so _rtld()
    784 	 * function expects to be called with a single pointer to
    785 	 * a region that has a few words it can stash values into,
    786 	 * followed by argc/argv/envp.  When the stack grows down,
    787 	 * it's easy to decrement the stack pointer a little bit to
    788 	 * allocate the space for these few words and pass the new
    789 	 * stack pointer to _rtld.  When the stack grows up, however,
    790 	 * a few words before argc is part of the signal trampoline, XXX
    791 	 * so we have a problem.
    792 	 *
    793 	 * Instead of changing how _rtld works, we take the easy way
    794 	 * out and steal 32 bytes before we call copyargs.
    795 	 * This extra space was allowed for when 'len' was calculated.
    796 	 */
    797 	stack += RTLD_GAP;
    798 #endif /* __MACHINE_STACK_GROWS_UP */
    799 
    800 	/* Now copy argc, args & environ to new stack */
    801 	error = (*pack.ep_esch->es_copyargs)(l, &pack, &arginfo, &stack, argp);
    802 	if (pack.ep_path) {
    803 		PNBUF_PUT(pack.ep_path);
    804 		pack.ep_path = NULL;
    805 	}
    806 	if (error) {
    807 		DPRINTF(("execve: copyargs failed %d\n", error));
    808 		goto exec_abort;
    809 	}
    810 	/* Move the stack back to original point */
    811 	stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
    812 
    813 	/* fill process ps_strings info */
    814 	p->p_psstr = (struct ps_strings *)
    815 	    STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, STACK_PTHREADSPACE),
    816 	    sizeof(struct ps_strings));
    817 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    818 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    819 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    820 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    821 
    822 	/* copy out the process's ps_strings structure */
    823 	if ((error = copyout(aip, (char *)p->p_psstr,
    824 	    sizeof(arginfo))) != 0) {
    825 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
    826 		       aip, (char *)p->p_psstr, (long)sizeof(arginfo)));
    827 		goto exec_abort;
    828 	}
    829 
    830 	fd_closeexec();		/* handle close on exec */
    831 	execsigs(p);		/* reset catched signals */
    832 
    833 	l->l_ctxlink = NULL;	/* reset ucontext link */
    834 
    835 
    836 	p->p_acflag &= ~AFORK;
    837 	mutex_enter(p->p_lock);
    838 	p->p_flag |= PK_EXEC;
    839 	mutex_exit(p->p_lock);
    840 
    841 	/*
    842 	 * Stop profiling.
    843 	 */
    844 	if ((p->p_stflag & PST_PROFIL) != 0) {
    845 		mutex_spin_enter(&p->p_stmutex);
    846 		stopprofclock(p);
    847 		mutex_spin_exit(&p->p_stmutex);
    848 	}
    849 
    850 	/*
    851 	 * It's OK to test PS_PPWAIT unlocked here, as other LWPs have
    852 	 * exited and exec()/exit() are the only places it will be cleared.
    853 	 */
    854 	if ((p->p_sflag & PS_PPWAIT) != 0) {
    855 		mutex_enter(proc_lock);
    856 		mutex_enter(p->p_lock);
    857 		p->p_sflag &= ~PS_PPWAIT;
    858 		cv_broadcast(&p->p_pptr->p_waitcv);
    859 		mutex_exit(p->p_lock);
    860 		mutex_exit(proc_lock);
    861 	}
    862 
    863 	/*
    864 	 * Deal with set[ug]id.  MNT_NOSUID has already been used to disable
    865 	 * s[ug]id.  It's OK to check for PSL_TRACED here as we have blocked
    866 	 * out additional references on the process for the moment.
    867 	 */
    868 	if ((p->p_slflag & PSL_TRACED) == 0 &&
    869 
    870 	    (((attr.va_mode & S_ISUID) != 0 &&
    871 	      kauth_cred_geteuid(l->l_cred) != attr.va_uid) ||
    872 
    873 	     ((attr.va_mode & S_ISGID) != 0 &&
    874 	      kauth_cred_getegid(l->l_cred) != attr.va_gid))) {
    875 		/*
    876 		 * Mark the process as SUGID before we do
    877 		 * anything that might block.
    878 		 */
    879 		proc_crmod_enter();
    880 		proc_crmod_leave(NULL, NULL, true);
    881 
    882 		/* Make sure file descriptors 0..2 are in use. */
    883 		if ((error = fd_checkstd()) != 0) {
    884 			DPRINTF(("execve: fdcheckstd failed %d\n", error));
    885 			goto exec_abort;
    886 		}
    887 
    888 		/*
    889 		 * Copy the credential so other references don't see our
    890 		 * changes.
    891 		 */
    892 		l->l_cred = kauth_cred_copy(l->l_cred);
    893 #ifdef KTRACE
    894 		/*
    895 		 * If the persistent trace flag isn't set, turn off.
    896 		 */
    897 		if (p->p_tracep) {
    898 			mutex_enter(&ktrace_lock);
    899 			if (!(p->p_traceflag & KTRFAC_PERSISTENT))
    900 				ktrderef(p);
    901 			mutex_exit(&ktrace_lock);
    902 		}
    903 #endif
    904 		if (attr.va_mode & S_ISUID)
    905 			kauth_cred_seteuid(l->l_cred, attr.va_uid);
    906 		if (attr.va_mode & S_ISGID)
    907 			kauth_cred_setegid(l->l_cred, attr.va_gid);
    908 	} else {
    909 		if (kauth_cred_geteuid(l->l_cred) ==
    910 		    kauth_cred_getuid(l->l_cred) &&
    911 		    kauth_cred_getegid(l->l_cred) ==
    912 		    kauth_cred_getgid(l->l_cred))
    913 			p->p_flag &= ~PK_SUGID;
    914 	}
    915 
    916 	/*
    917 	 * Copy the credential so other references don't see our changes.
    918 	 * Test to see if this is necessary first, since in the common case
    919 	 * we won't need a private reference.
    920 	 */
    921 	if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
    922 	    kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
    923 		l->l_cred = kauth_cred_copy(l->l_cred);
    924 		kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
    925 		kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
    926 	}
    927 
    928 	/* Update the master credentials. */
    929 	if (l->l_cred != p->p_cred) {
    930 		kauth_cred_t ocred;
    931 
    932 		kauth_cred_hold(l->l_cred);
    933 		mutex_enter(p->p_lock);
    934 		ocred = p->p_cred;
    935 		p->p_cred = l->l_cred;
    936 		mutex_exit(p->p_lock);
    937 		kauth_cred_free(ocred);
    938 	}
    939 
    940 #if defined(__HAVE_RAS)
    941 	/*
    942 	 * Remove all RASs from the address space.
    943 	 */
    944 	ras_purgeall();
    945 #endif
    946 
    947 	doexechooks(p);
    948 
    949 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
    950 
    951 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    952 
    953 	/* notify others that we exec'd */
    954 	KNOTE(&p->p_klist, NOTE_EXEC);
    955 
    956 	/* setup new registers and do misc. setup. */
    957 	(*pack.ep_esch->es_emul->e_setregs)(l, &pack, (u_long) stack);
    958 	if (pack.ep_esch->es_setregs)
    959 		(*pack.ep_esch->es_setregs)(l, &pack, (u_long) stack);
    960 
    961 	/* map the process's signal trampoline code */
    962 	if (exec_sigcode_map(p, pack.ep_esch->es_emul)) {
    963 		DPRINTF(("execve: map sigcode failed %d\n", error));
    964 		goto exec_abort;
    965 	}
    966 
    967 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
    968 
    969 	/* The emulation root will usually have been found when we looked
    970 	 * for the elf interpreter (or similar), if not look now. */
    971 	if (pack.ep_esch->es_emul->e_path != NULL && pack.ep_emul_root == NULL)
    972 		emul_find_root(l, &pack);
    973 
    974 	/* Any old emulation root got removed by fdcloseexec */
    975 	rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER);
    976 	p->p_cwdi->cwdi_edir = pack.ep_emul_root;
    977 	rw_exit(&p->p_cwdi->cwdi_lock);
    978 	pack.ep_emul_root = NULL;
    979 	if (pack.ep_interp != NULL)
    980 		vrele(pack.ep_interp);
    981 
    982 	/*
    983 	 * Call emulation specific exec hook. This can setup per-process
    984 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    985 	 *
    986 	 * If we are executing process of different emulation than the
    987 	 * original forked process, call e_proc_exit() of the old emulation
    988 	 * first, then e_proc_exec() of new emulation. If the emulation is
    989 	 * same, the exec hook code should deallocate any old emulation
    990 	 * resources held previously by this process.
    991 	 */
    992 	if (p->p_emul && p->p_emul->e_proc_exit
    993 	    && p->p_emul != pack.ep_esch->es_emul)
    994 		(*p->p_emul->e_proc_exit)(p);
    995 
    996 	/*
    997 	 * Call exec hook. Emulation code may NOT store reference to anything
    998 	 * from &pack.
    999 	 */
   1000         if (pack.ep_esch->es_emul->e_proc_exec)
   1001                 (*pack.ep_esch->es_emul->e_proc_exec)(p, &pack);
   1002 
   1003 	/* update p_emul, the old value is no longer needed */
   1004 	p->p_emul = pack.ep_esch->es_emul;
   1005 
   1006 	/* ...and the same for p_execsw */
   1007 	p->p_execsw = pack.ep_esch;
   1008 
   1009 #ifdef __HAVE_SYSCALL_INTERN
   1010 	(*p->p_emul->e_syscall_intern)(p);
   1011 #endif
   1012 	ktremul();
   1013 
   1014 	/* Allow new references from the debugger/procfs. */
   1015 	rw_exit(&p->p_reflock);
   1016 #ifdef LKM
   1017 	rw_exit(&exec_lock);
   1018 #endif
   1019 
   1020 	mutex_enter(proc_lock);
   1021 
   1022 	if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
   1023 		KSI_INIT_EMPTY(&ksi);
   1024 		ksi.ksi_signo = SIGTRAP;
   1025 		ksi.ksi_lid = l->l_lid;
   1026 		kpsignal(p, &ksi, NULL);
   1027 	}
   1028 
   1029 	if (p->p_sflag & PS_STOPEXEC) {
   1030 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
   1031 		p->p_pptr->p_nstopchild++;
   1032 		p->p_pptr->p_waited = 0;
   1033 		mutex_enter(p->p_lock);
   1034 		ksiginfo_queue_init(&kq);
   1035 		sigclearall(p, &contsigmask, &kq);
   1036 		lwp_lock(l);
   1037 		l->l_stat = LSSTOP;
   1038 		p->p_stat = SSTOP;
   1039 		p->p_nrlwps--;
   1040 		mutex_exit(p->p_lock);
   1041 		mutex_exit(proc_lock);
   1042 		mi_switch(l);
   1043 		ksiginfo_queue_drain(&kq);
   1044 		KERNEL_LOCK(l->l_biglocks, l);
   1045 	} else {
   1046 		mutex_exit(proc_lock);
   1047 	}
   1048 
   1049 	PNBUF_PUT(pathbuf);
   1050 	return (EJUSTRETURN);
   1051 
   1052  bad:
   1053 	/* free the vmspace-creation commands, and release their references */
   1054 	kill_vmcmds(&pack.ep_vmcmds);
   1055 	/* kill any opened file descriptor, if necessary */
   1056 	if (pack.ep_flags & EXEC_HASFD) {
   1057 		pack.ep_flags &= ~EXEC_HASFD;
   1058 		fd_close(pack.ep_fd);
   1059 	}
   1060 	/* close and put the exec'd file */
   1061 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   1062 	VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred);
   1063 	vput(pack.ep_vp);
   1064 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
   1065 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
   1066 
   1067  freehdr:
   1068 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
   1069 	if (pack.ep_emul_root != NULL)
   1070 		vrele(pack.ep_emul_root);
   1071 	if (pack.ep_interp != NULL)
   1072 		vrele(pack.ep_interp);
   1073 
   1074  clrflg:
   1075 	PNBUF_PUT(pathbuf);
   1076 	rw_exit(&p->p_reflock);
   1077 #ifdef LKM
   1078 	rw_exit(&exec_lock);
   1079 #endif
   1080 
   1081 	return error;
   1082 
   1083  exec_abort:
   1084 	PNBUF_PUT(pathbuf);
   1085 	rw_exit(&p->p_reflock);
   1086 #ifdef LKM
   1087 	rw_exit(&exec_lock);
   1088 #endif
   1089 
   1090 	/*
   1091 	 * the old process doesn't exist anymore.  exit gracefully.
   1092 	 * get rid of the (new) address space we have created, if any, get rid
   1093 	 * of our namei data and vnode, and exit noting failure
   1094 	 */
   1095 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
   1096 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
   1097 	if (pack.ep_emul_arg)
   1098 		FREE(pack.ep_emul_arg, M_TEMP);
   1099 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
   1100 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
   1101 	kmem_free(pack.ep_hdr, pack.ep_hdrlen);
   1102 	if (pack.ep_emul_root != NULL)
   1103 		vrele(pack.ep_emul_root);
   1104 	if (pack.ep_interp != NULL)
   1105 		vrele(pack.ep_interp);
   1106 
   1107 	/* Acquire the sched-state mutex (exit1() will release it). */
   1108 	mutex_enter(p->p_lock);
   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(proc_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(proc_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