Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.143
      1 /*	$NetBSD: kern_exec.c,v 1.143 2001/07/15 20:49:40 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 "opt_ktrace.h"
     36 #include "opt_syscall_debug.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/filedesc.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/mount.h>
     44 #include <sys/malloc.h>
     45 #include <sys/namei.h>
     46 #include <sys/vnode.h>
     47 #include <sys/file.h>
     48 #include <sys/acct.h>
     49 #include <sys/exec.h>
     50 #include <sys/ktrace.h>
     51 #include <sys/resourcevar.h>
     52 #include <sys/wait.h>
     53 #include <sys/mman.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/stat.h>
     56 #include <sys/syscall.h>
     57 
     58 #include <sys/syscallargs.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 
     62 #include <machine/cpu.h>
     63 #include <machine/reg.h>
     64 
     65 #ifdef DEBUG_EXEC
     66 #define DPRINTF(a) uprintf a
     67 #else
     68 #define DPRINTF(a)
     69 #endif /* DEBUG_EXEC */
     70 
     71 /*
     72  * Exec function switch:
     73  *
     74  * Note that each makecmds function is responsible for loading the
     75  * exec package with the necessary functions for any exec-type-specific
     76  * handling.
     77  *
     78  * Functions for specific exec types should be defined in their own
     79  * header file.
     80  */
     81 extern const struct execsw	execsw_builtin[];
     82 extern int			nexecs_builtin;
     83 static const struct execsw	**execsw = NULL;
     84 static int			nexecs;
     85 
     86 int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
     87 
     88 #ifdef LKM
     89 /* list of supported emulations */
     90 static
     91 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
     92 struct emul_entry {
     93 	LIST_ENTRY(emul_entry)	el_list;
     94 	const struct emul	*el_emul;
     95 	int			ro_entry;
     96 };
     97 
     98 /* list of dynamically loaded execsw entries */
     99 static
    100 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
    101 struct exec_entry {
    102 	LIST_ENTRY(exec_entry)	ex_list;
    103 	const struct execsw	*es;
    104 };
    105 
    106 /* structure used for building execw[] */
    107 struct execsw_entry {
    108 	struct execsw_entry	*next;
    109 	const struct execsw	*es;
    110 };
    111 #endif /* LKM */
    112 
    113 /* NetBSD emul struct */
    114 extern char	sigcode[], esigcode[];
    115 #ifdef SYSCALL_DEBUG
    116 extern const char * const syscallnames[];
    117 #endif
    118 #ifdef __HAVE_SYSCALL_INTERN
    119 void syscall_intern(struct proc *);
    120 #else
    121 void syscall(void);
    122 #endif
    123 
    124 const struct emul emul_netbsd = {
    125 	"netbsd",
    126 	NULL,		/* emulation path */
    127 #ifndef __HAVE_MINIMAL_EMUL
    128 	EMUL_HAS_SYS___syscall,
    129 	NULL,
    130 	SYS_syscall,
    131 	SYS_MAXSYSCALL,
    132 #endif
    133 	sysent,
    134 #ifdef SYSCALL_DEBUG
    135 	syscallnames,
    136 #else
    137 	NULL,
    138 #endif
    139 	sendsig,
    140 	trapsignal,
    141 	sigcode,
    142 	esigcode,
    143 	NULL,
    144 	NULL,
    145 	NULL,
    146 #ifdef __HAVE_SYSCALL_INTERN
    147 	syscall_intern,
    148 #else
    149 	syscall,
    150 #endif
    151 };
    152 
    153 /*
    154  * Exec lock. Used to control access to execsw[] structures.
    155  * This must not be static so that netbsd32 can access it, too.
    156  */
    157 struct lock exec_lock;
    158 
    159 #ifdef LKM
    160 static const struct emul * emul_search(const char *);
    161 static void link_es(struct execsw_entry **, const struct execsw *);
    162 #endif /* LKM */
    163 
    164 /*
    165  * check exec:
    166  * given an "executable" described in the exec package's namei info,
    167  * see what we can do with it.
    168  *
    169  * ON ENTRY:
    170  *	exec package with appropriate namei info
    171  *	proc pointer of exec'ing proc
    172  *	NO SELF-LOCKED VNODES
    173  *
    174  * ON EXIT:
    175  *	error:	nothing held, etc.  exec header still allocated.
    176  *	ok:	filled exec package, executable's vnode (unlocked).
    177  *
    178  * EXEC SWITCH ENTRY:
    179  * 	Locked vnode to check, exec package, proc.
    180  *
    181  * EXEC SWITCH EXIT:
    182  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
    183  *	error:	destructive:
    184  *			everything deallocated execept exec header.
    185  *		non-destructive:
    186  *			error code, executable's vnode (unlocked),
    187  *			exec header unmodified.
    188  */
    189 int
    190 check_exec(struct proc *p, struct exec_package *epp)
    191 {
    192 	int		error, i;
    193 	struct vnode	*vp;
    194 	struct nameidata *ndp;
    195 	size_t		resid;
    196 
    197 	ndp = epp->ep_ndp;
    198 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    199 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    200 	/* first get the vnode */
    201 	if ((error = namei(ndp)) != 0)
    202 		return error;
    203 	epp->ep_vp = vp = ndp->ni_vp;
    204 
    205 	/* check access and type */
    206 	if (vp->v_type != VREG) {
    207 		error = EACCES;
    208 		goto bad1;
    209 	}
    210 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
    211 		goto bad1;
    212 
    213 	/* get attributes */
    214 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
    215 		goto bad1;
    216 
    217 	/* Check mount point */
    218 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    219 		error = EACCES;
    220 		goto bad1;
    221 	}
    222 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    223 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    224 
    225 	/* try to open it */
    226 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
    227 		goto bad1;
    228 
    229 	/* unlock vp, since we need it unlocked from here on out. */
    230 	VOP_UNLOCK(vp, 0);
    231 
    232 	/* now we have the file, get the exec header */
    233 	uvn_attach(vp, VM_PROT_READ);
    234 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    235 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
    236 	if (error)
    237 		goto bad2;
    238 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    239 
    240 	/*
    241 	 * Set up default address space limits.  Can be overridden
    242 	 * by individual exec packages.
    243 	 *
    244 	 * XXX probably shoul be all done in the exec pakages.
    245 	 */
    246 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    247 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
    248 	/*
    249 	 * set up the vmcmds for creation of the process
    250 	 * address space
    251 	 */
    252 	error = ENOEXEC;
    253 	for (i = 0; i < nexecs && error != 0; i++) {
    254 		int newerror;
    255 
    256 		epp->ep_esch = execsw[i];
    257 		newerror = (*execsw[i]->es_check)(p, epp);
    258 		/* make sure the first "interesting" error code is saved. */
    259 		if (!newerror || error == ENOEXEC)
    260 			error = newerror;
    261 
    262 		/* if es_check call was successful, update epp->ep_es */
    263 		if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
    264 			epp->ep_es = execsw[i];
    265 
    266 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    267 			return error;
    268 	}
    269 	if (!error) {
    270 		/* check that entry point is sane */
    271 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    272 			error = ENOEXEC;
    273 
    274 		/* check limits */
    275 		if ((epp->ep_tsize > MAXTSIZ) ||
    276 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
    277 			error = ENOMEM;
    278 
    279 		if (!error)
    280 			return (0);
    281 	}
    282 
    283 	/*
    284 	 * free any vmspace-creation commands,
    285 	 * and release their references
    286 	 */
    287 	kill_vmcmds(&epp->ep_vmcmds);
    288 
    289 bad2:
    290 	/*
    291 	 * close and release the vnode, restore the old one, free the
    292 	 * pathname buf, and punt.
    293 	 */
    294 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    295 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
    296 	vput(vp);
    297 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    298 	return error;
    299 
    300 bad1:
    301 	/*
    302 	 * free the namei pathname buffer, and put the vnode
    303 	 * (which we don't yet have open).
    304 	 */
    305 	vput(vp);				/* was still locked */
    306 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    307 	return error;
    308 }
    309 
    310 /*
    311  * exec system call
    312  */
    313 /* ARGSUSED */
    314 int
    315 sys_execve(struct proc *p, void *v, register_t *retval)
    316 {
    317 	struct sys_execve_args /* {
    318 		syscallarg(const char *)	path;
    319 		syscallarg(char * const *)	argp;
    320 		syscallarg(char * const *)	envp;
    321 	} */ *uap = v;
    322 	int			error, i;
    323 	struct exec_package	pack;
    324 	struct nameidata	nid;
    325 	struct vattr		attr;
    326 	struct ucred		*cred;
    327 	char			*argp;
    328 	char * const		*cpp;
    329 	char			*dp, *sp;
    330 	long			argc, envc;
    331 	size_t			len;
    332 	char			*stack;
    333 	struct ps_strings	arginfo;
    334 	struct vmspace		*vm;
    335 	char			**tmpfap;
    336 	int			szsigcode;
    337 	struct exec_vmcmd	*base_vcp;
    338 
    339 	cred = p->p_ucred;
    340 	base_vcp = NULL;
    341 	/*
    342 	 * Init the namei data to point the file user's program name.
    343 	 * This is done here rather than in check_exec(), so that it's
    344 	 * possible to override this settings if any of makecmd/probe
    345 	 * functions call check_exec() recursively - for example,
    346 	 * see exec_script_makecmds().
    347 	 */
    348 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    349 
    350 	/*
    351 	 * initialize the fields of the exec package.
    352 	 */
    353 	pack.ep_name = SCARG(uap, path);
    354 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
    355 	pack.ep_hdrlen = exec_maxhdrsz;
    356 	pack.ep_hdrvalid = 0;
    357 	pack.ep_ndp = &nid;
    358 	pack.ep_emul_arg = NULL;
    359 	pack.ep_vmcmds.evs_cnt = 0;
    360 	pack.ep_vmcmds.evs_used = 0;
    361 	pack.ep_vap = &attr;
    362 	pack.ep_flags = 0;
    363 
    364 	lockmgr(&exec_lock, LK_SHARED, NULL);
    365 
    366 	/* see if we can run it. */
    367 	if ((error = check_exec(p, &pack)) != 0)
    368 		goto freehdr;
    369 
    370 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    371 
    372 	/* allocate an argument buffer */
    373 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
    374 #ifdef DIAGNOSTIC
    375 	if (argp == (vaddr_t) 0)
    376 		panic("execve: argp == NULL");
    377 #endif
    378 	dp = argp;
    379 	argc = 0;
    380 
    381 	/* copy the fake args list, if there's one, freeing it as we go */
    382 	if (pack.ep_flags & EXEC_HASARGL) {
    383 		tmpfap = pack.ep_fa;
    384 		while (*tmpfap != NULL) {
    385 			char *cp;
    386 
    387 			cp = *tmpfap;
    388 			while (*cp)
    389 				*dp++ = *cp++;
    390 			dp++;
    391 
    392 			FREE(*tmpfap, M_EXEC);
    393 			tmpfap++; argc++;
    394 		}
    395 		FREE(pack.ep_fa, M_EXEC);
    396 		pack.ep_flags &= ~EXEC_HASARGL;
    397 	}
    398 
    399 	/* Now get argv & environment */
    400 	if (!(cpp = SCARG(uap, argp))) {
    401 		error = EINVAL;
    402 		goto bad;
    403 	}
    404 
    405 	if (pack.ep_flags & EXEC_SKIPARG)
    406 		cpp++;
    407 
    408 	while (1) {
    409 		len = argp + ARG_MAX - dp;
    410 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    411 			goto bad;
    412 		if (!sp)
    413 			break;
    414 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    415 			if (error == ENAMETOOLONG)
    416 				error = E2BIG;
    417 			goto bad;
    418 		}
    419 		dp += len;
    420 		cpp++;
    421 		argc++;
    422 	}
    423 
    424 	envc = 0;
    425 	/* environment need not be there */
    426 	if ((cpp = SCARG(uap, envp)) != NULL ) {
    427 		while (1) {
    428 			len = argp + ARG_MAX - dp;
    429 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    430 				goto bad;
    431 			if (!sp)
    432 				break;
    433 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    434 				if (error == ENAMETOOLONG)
    435 					error = E2BIG;
    436 				goto bad;
    437 			}
    438 			dp += len;
    439 			cpp++;
    440 			envc++;
    441 		}
    442 	}
    443 
    444 	dp = (char *) ALIGN(dp);
    445 
    446 	szsigcode = pack.ep_es->es_emul->e_esigcode -
    447 	    pack.ep_es->es_emul->e_sigcode;
    448 
    449 	/* Now check if args & environ fit into new stack */
    450 	if (pack.ep_flags & EXEC_32)
    451 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    452 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
    453 		    szsigcode + sizeof(struct ps_strings)) - argp;
    454 	else
    455 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    456 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
    457 		    szsigcode + sizeof(struct ps_strings)) - argp;
    458 
    459 	len = ALIGN(len);	/* make the stack "safely" aligned */
    460 
    461 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    462 		error = ENOMEM;
    463 		goto bad;
    464 	}
    465 
    466 	/* adjust "active stack depth" for process VSZ */
    467 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    468 
    469 	/*
    470 	 * Do whatever is necessary to prepare the address space
    471 	 * for remapping.  Note that this might replace the current
    472 	 * vmspace with another!
    473 	 */
    474 	uvmspace_exec(p, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
    475 
    476 	/* Now map address space */
    477 	vm = p->p_vmspace;
    478 	vm->vm_taddr = (char *) pack.ep_taddr;
    479 	vm->vm_tsize = btoc(pack.ep_tsize);
    480 	vm->vm_daddr = (char *) pack.ep_daddr;
    481 	vm->vm_dsize = btoc(pack.ep_dsize);
    482 	vm->vm_ssize = btoc(pack.ep_ssize);
    483 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    484 	vm->vm_minsaddr = (char *) pack.ep_minsaddr;
    485 
    486 	/* create the new process's VM space by running the vmcmds */
    487 #ifdef DIAGNOSTIC
    488 	if (pack.ep_vmcmds.evs_used == 0)
    489 		panic("execve: no vmcmds");
    490 #endif
    491 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    492 		struct exec_vmcmd *vcp;
    493 
    494 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    495 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    496 #ifdef DIAGNOSTIC
    497 			if (base_vcp == NULL)
    498 				panic("execve: relative vmcmd with no base");
    499 			if (vcp->ev_flags & VMCMD_BASE)
    500 				panic("execve: illegal base & relative vmcmd");
    501 #endif
    502 			vcp->ev_addr += base_vcp->ev_addr;
    503 		}
    504 		error = (*vcp->ev_proc)(p, vcp);
    505 #ifdef DEBUG_EXEC
    506 		if (error) {
    507 			int j;
    508 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
    509 			for (j = 0; j <= i; j++)
    510 				uprintf(
    511 			    "vmcmd[%d] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
    512 				    j, vp[j].ev_addr, vp[j].ev_len,
    513 				    vp[j].ev_offset, vp[j].ev_prot,
    514 				    vp[j].ev_flags);
    515 		}
    516 #endif /* DEBUG_EXEC */
    517 		if (vcp->ev_flags & VMCMD_BASE)
    518 			base_vcp = vcp;
    519 	}
    520 
    521 	/* free the vmspace-creation commands, and release their references */
    522 	kill_vmcmds(&pack.ep_vmcmds);
    523 
    524 	/* if an error happened, deallocate and punt */
    525 	if (error) {
    526 		DPRINTF(("execve: vmcmd %i failed: %d\n", i - 1, error));
    527 		goto exec_abort;
    528 	}
    529 
    530 	/* remember information about the process */
    531 	arginfo.ps_nargvstr = argc;
    532 	arginfo.ps_nenvstr = envc;
    533 
    534 	stack = (char *) (vm->vm_minsaddr - len);
    535 	/* Now copy argc, args & environ to new stack */
    536 	if (!(*pack.ep_es->es_copyargs)(&pack, &arginfo, stack, argp)) {
    537 		DPRINTF(("execve: copyargs failed\n"));
    538 		goto exec_abort;
    539 	}
    540 
    541 	/* fill process ps_strings info */
    542 	p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr
    543 		- sizeof(struct ps_strings));
    544 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    545 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    546 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    547 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    548 
    549 	/* copy out the process's ps_strings structure */
    550 	if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
    551 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
    552 		       &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo)));
    553 		goto exec_abort;
    554 	}
    555 
    556 	/* copy out the process's signal trapoline code */
    557 	if (szsigcode) {
    558 		if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
    559 		    p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
    560 		    szsigcode)) {
    561 			DPRINTF(("execve: sig trampoline copyout failed\n"));
    562 			goto exec_abort;
    563 		}
    564 #ifdef PMAP_NEED_PROCWR
    565 		/* This is code. Let the pmap do what is needed. */
    566 		pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
    567 #endif
    568 	}
    569 
    570 	stopprofclock(p);	/* stop profiling */
    571 	fdcloseexec(p);		/* handle close on exec */
    572 	execsigs(p);		/* reset catched signals */
    573 	p->p_ctxlink = NULL;	/* reset ucontext link */
    574 
    575 	/* set command name & other accounting info */
    576 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    577 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
    578 	p->p_comm[len] = 0;
    579 	p->p_acflag &= ~AFORK;
    580 
    581 	/* record proc's vnode, for use by procfs and others */
    582         if (p->p_textvp)
    583                 vrele(p->p_textvp);
    584 	VREF(pack.ep_vp);
    585 	p->p_textvp = pack.ep_vp;
    586 
    587 	p->p_flag |= P_EXEC;
    588 	if (p->p_flag & P_PPWAIT) {
    589 		p->p_flag &= ~P_PPWAIT;
    590 		wakeup((caddr_t) p->p_pptr);
    591 	}
    592 
    593 	/*
    594 	 * deal with set[ug]id.
    595 	 * MNT_NOSUID has already been used to disable s[ug]id.
    596 	 */
    597 	if ((p->p_flag & P_TRACED) == 0 &&
    598 
    599 	    (((attr.va_mode & S_ISUID) != 0 &&
    600 	      p->p_ucred->cr_uid != attr.va_uid) ||
    601 
    602 	     ((attr.va_mode & S_ISGID) != 0 &&
    603 	      p->p_ucred->cr_gid != attr.va_gid))) {
    604 		/*
    605 		 * Mark the process as SUGID before we do
    606 		 * anything that might block.
    607 		 */
    608 		p_sugid(p);
    609 
    610 		p->p_ucred = crcopy(cred);
    611 #ifdef KTRACE
    612 		/*
    613 		 * If process is being ktraced, turn off - unless
    614 		 * root set it.
    615 		 */
    616 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
    617 			ktrderef(p);
    618 #endif
    619 		if (attr.va_mode & S_ISUID)
    620 			p->p_ucred->cr_uid = attr.va_uid;
    621 		if (attr.va_mode & S_ISGID)
    622 			p->p_ucred->cr_gid = attr.va_gid;
    623 	} else
    624 		p->p_flag &= ~P_SUGID;
    625 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    626 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    627 
    628 	doexechooks(p);
    629 
    630 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    631 
    632 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    633 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    634 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    635 	vput(pack.ep_vp);
    636 
    637 	/* setup new registers and do misc. setup. */
    638 	(*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
    639 
    640 	if (p->p_flag & P_TRACED)
    641 		psignal(p, SIGTRAP);
    642 
    643 	free(pack.ep_hdr, M_EXEC);
    644 
    645 	/*
    646 	 * Call emulation specific exec hook. This can setup setup per-process
    647 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    648 	 *
    649 	 * If we are executing process of different emulation than the
    650 	 * original forked process, call e_proc_exit() of the old emulation
    651 	 * first, then e_proc_exec() of new emulation. If the emulation is
    652 	 * same, the exec hook code should deallocate any old emulation
    653 	 * resources held previously by this process.
    654 	 */
    655 	if (p->p_emul && p->p_emul->e_proc_exit
    656 	    && p->p_emul != pack.ep_es->es_emul)
    657 		(*p->p_emul->e_proc_exit)(p);
    658 
    659 	/*
    660 	 * Call exec hook. Emulation code may NOT store reference to anything
    661 	 * from &pack.
    662 	 */
    663         if (pack.ep_es->es_emul->e_proc_exec)
    664                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
    665 
    666 	/* update p_emul, the old value is no longer needed */
    667 	p->p_emul = pack.ep_es->es_emul;
    668 #ifdef __HAVE_SYSCALL_INTERN
    669 	(*p->p_emul->e_syscall_intern)(p);
    670 #endif
    671 #ifdef KTRACE
    672 	if (KTRPOINT(p, KTR_EMUL))
    673 		ktremul(p);
    674 #endif
    675 
    676 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    677 
    678 	return (EJUSTRETURN);
    679 
    680  bad:
    681 	/* free the vmspace-creation commands, and release their references */
    682 	kill_vmcmds(&pack.ep_vmcmds);
    683 	/* kill any opened file descriptor, if necessary */
    684 	if (pack.ep_flags & EXEC_HASFD) {
    685 		pack.ep_flags &= ~EXEC_HASFD;
    686 		(void) fdrelease(p, pack.ep_fd);
    687 	}
    688 	/* close and put the exec'd file */
    689 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    690 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    691 	vput(pack.ep_vp);
    692 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    693 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    694 
    695  freehdr:
    696 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    697 
    698 	free(pack.ep_hdr, M_EXEC);
    699 	return error;
    700 
    701  exec_abort:
    702 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    703 
    704 	/*
    705 	 * the old process doesn't exist anymore.  exit gracefully.
    706 	 * get rid of the (new) address space we have created, if any, get rid
    707 	 * of our namei data and vnode, and exit noting failure
    708 	 */
    709 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    710 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    711 	if (pack.ep_emul_arg)
    712 		FREE(pack.ep_emul_arg, M_TEMP);
    713 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    714 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    715 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    716 	vput(pack.ep_vp);
    717 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    718 	free(pack.ep_hdr, M_EXEC);
    719 	exit1(p, W_EXITCODE(0, SIGABRT));
    720 	exit1(p, -1);
    721 
    722 	/* NOTREACHED */
    723 	return 0;
    724 }
    725 
    726 
    727 void *
    728 copyargs(struct exec_package *pack, struct ps_strings *arginfo,
    729 	void *stack, void *argp)
    730 {
    731 	char	**cpp, *dp, *sp;
    732 	size_t	len;
    733 	void	*nullp;
    734 	long	argc, envc;
    735 
    736 	cpp = stack;
    737 	nullp = NULL;
    738 	argc = arginfo->ps_nargvstr;
    739 	envc = arginfo->ps_nenvstr;
    740 	if (copyout(&argc, cpp++, sizeof(argc)))
    741 		return NULL;
    742 
    743 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
    744 	sp = argp;
    745 
    746 	/* XXX don't copy them out, remap them! */
    747 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    748 
    749 	for (; --argc >= 0; sp += len, dp += len)
    750 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    751 		    copyoutstr(sp, dp, ARG_MAX, &len))
    752 			return NULL;
    753 
    754 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    755 		return NULL;
    756 
    757 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    758 
    759 	for (; --envc >= 0; sp += len, dp += len)
    760 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    761 		    copyoutstr(sp, dp, ARG_MAX, &len))
    762 			return NULL;
    763 
    764 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    765 		return NULL;
    766 
    767 	return cpp;
    768 }
    769 
    770 #ifdef LKM
    771 /*
    772  * Find an emulation of given name in list of emulations.
    773  */
    774 static const struct emul *
    775 emul_search(const char *name)
    776 {
    777 	struct emul_entry *it;
    778 
    779 	LIST_FOREACH(it, &el_head, el_list) {
    780 		if (strcmp(name, it->el_emul->e_name) == 0)
    781 			return it->el_emul;
    782 	}
    783 
    784 	return NULL;
    785 }
    786 
    787 /*
    788  * Add an emulation to list, if it's not there already.
    789  */
    790 int
    791 emul_register(const struct emul *emul, int ro_entry)
    792 {
    793 	struct emul_entry	*ee;
    794 	int			error;
    795 
    796 	error = 0;
    797 	lockmgr(&exec_lock, LK_SHARED, NULL);
    798 
    799 	if (emul_search(emul->e_name)) {
    800 		error = EEXIST;
    801 		goto out;
    802 	}
    803 
    804 	MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
    805 		M_EXEC, M_WAITOK);
    806 	ee->el_emul = emul;
    807 	ee->ro_entry = ro_entry;
    808 	LIST_INSERT_HEAD(&el_head, ee, el_list);
    809 
    810  out:
    811 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    812 	return error;
    813 }
    814 
    815 /*
    816  * Remove emulation with name 'name' from list of supported emulations.
    817  */
    818 int
    819 emul_unregister(const char *name)
    820 {
    821 	const struct proclist_desc *pd;
    822 	struct emul_entry	*it;
    823 	int			i, error;
    824 	struct proc		*ptmp;
    825 
    826 	error = 0;
    827 	lockmgr(&exec_lock, LK_SHARED, NULL);
    828 
    829 	LIST_FOREACH(it, &el_head, el_list) {
    830 		if (strcmp(it->el_emul->e_name, name) == 0)
    831 			break;
    832 	}
    833 
    834 	if (!it) {
    835 		error = ENOENT;
    836 		goto out;
    837 	}
    838 
    839 	if (it->ro_entry) {
    840 		error = EBUSY;
    841 		goto out;
    842 	}
    843 
    844 	/* test if any execw[] entry is still using this */
    845 	for(i=0; i < nexecs; i++) {
    846 		if (execsw[i]->es_emul == it->el_emul) {
    847 			error = EBUSY;
    848 			goto out;
    849 		}
    850 	}
    851 
    852 	/*
    853 	 * Test if any process is running under this emulation - since
    854 	 * emul_unregister() is running quite sendomly, it's better
    855 	 * to do expensive check here than to use any locking.
    856 	 */
    857 	proclist_lock_read();
    858 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
    859 		LIST_FOREACH(ptmp, pd->pd_list, p_list) {
    860 			if (ptmp->p_emul == it->el_emul) {
    861 				error = EBUSY;
    862 				break;
    863 			}
    864 		}
    865 	}
    866 	proclist_unlock_read();
    867 
    868 	if (error)
    869 		goto out;
    870 
    871 
    872 	/* entry is not used, remove it */
    873 	LIST_REMOVE(it, el_list);
    874 	FREE(it, M_EXEC);
    875 
    876  out:
    877 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    878 	return error;
    879 }
    880 
    881 /*
    882  * Add execsw[] entry.
    883  */
    884 int
    885 exec_add(struct execsw *esp, const char *e_name)
    886 {
    887 	struct exec_entry	*it;
    888 	int			error;
    889 
    890 	error = 0;
    891 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
    892 
    893 	if (!esp->es_emul) {
    894 		esp->es_emul = emul_search(e_name);
    895 		if (!esp->es_emul) {
    896 			error = ENOENT;
    897 			goto out;
    898 		}
    899 	}
    900 
    901 	LIST_FOREACH(it, &ex_head, ex_list) {
    902 		/* assume tuple (makecmds, probe_func, emulation) is unique */
    903 		if (it->es->es_check == esp->es_check
    904 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
    905 		    && it->es->es_emul == esp->es_emul) {
    906 			error = EEXIST;
    907 			goto out;
    908 		}
    909 	}
    910 
    911 	/* if we got here, the entry doesn't exist yet */
    912 	MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
    913 		M_EXEC, M_WAITOK);
    914 	it->es = esp;
    915 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
    916 
    917 	/* update execsw[] */
    918 	exec_init(0);
    919 
    920  out:
    921 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    922 	return error;
    923 }
    924 
    925 /*
    926  * Remove execsw[] entry.
    927  */
    928 int
    929 exec_remove(const struct execsw *esp)
    930 {
    931 	struct exec_entry	*it;
    932 	int			error;
    933 
    934 	error = 0;
    935 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
    936 
    937 	LIST_FOREACH(it, &ex_head, ex_list) {
    938 		/* assume tuple (makecmds, probe_func, emulation) is unique */
    939 		if (it->es->es_check == esp->es_check
    940 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
    941 		    && it->es->es_emul == esp->es_emul)
    942 			break;
    943 	}
    944 	if (!it) {
    945 		error = ENOENT;
    946 		goto out;
    947 	}
    948 
    949 	/* remove item from list and free resources */
    950 	LIST_REMOVE(it, ex_list);
    951 	FREE(it, M_EXEC);
    952 
    953 	/* update execsw[] */
    954 	exec_init(0);
    955 
    956  out:
    957 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    958 	return error;
    959 }
    960 
    961 static void
    962 link_es(struct execsw_entry **listp, const struct execsw *esp)
    963 {
    964 	struct execsw_entry *et, *e1;
    965 
    966 	MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
    967 			M_TEMP, M_WAITOK);
    968 	et->next = NULL;
    969 	et->es = esp;
    970 	if (*listp == NULL) {
    971 		*listp = et;
    972 		return;
    973 	}
    974 
    975 	switch(et->es->es_prio) {
    976 	case EXECSW_PRIO_FIRST:
    977 		/* put new entry as the first */
    978 		et->next = *listp;
    979 		*listp = et;
    980 		break;
    981 	case EXECSW_PRIO_ANY:
    982 		/* put new entry after all *_FIRST and *_ANY entries */
    983 		for(e1 = *listp; e1->next
    984 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
    985 			e1 = e1->next);
    986 		et->next = e1->next;
    987 		e1->next = et;
    988 		break;
    989 	case EXECSW_PRIO_LAST:
    990 		/* put new entry as the last one */
    991 		for(e1 = *listp; e1->next; e1 = e1->next);
    992 		e1->next = et;
    993 		break;
    994 	default:
    995 #ifdef DIAGNOSTIC
    996 		panic("execw[] entry with unknown priority %d found\n",
    997 			et->es->es_prio);
    998 #endif
    999 		break;
   1000 	}
   1001 }
   1002 
   1003 /*
   1004  * Initialize exec structures. If init_boot is true, also does necessary
   1005  * one-time initialization (it's called from main() that way).
   1006  * Once system is multiuser, this should be called with exec_lock hold,
   1007  * i.e. via exec_{add|remove}().
   1008  */
   1009 int
   1010 exec_init(int init_boot)
   1011 {
   1012 	const struct execsw	**new_es, * const *old_es;
   1013 	struct execsw_entry	*list, *e1;
   1014 	struct exec_entry	*e2;
   1015 	int			i, es_sz;
   1016 
   1017 	if (init_boot) {
   1018 		/* do one-time initializations */
   1019 		lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
   1020 
   1021 		/* register compiled-in emulations */
   1022 		for(i=0; i < nexecs_builtin; i++) {
   1023 			if (execsw_builtin[i].es_emul)
   1024 				emul_register(execsw_builtin[i].es_emul, 1);
   1025 		}
   1026 #ifdef DIAGNOSTIC
   1027 		if (i == 0)
   1028 			panic("no emulations found in execsw_builtin[]\n");
   1029 #endif
   1030 	}
   1031 
   1032 	/*
   1033 	 * Build execsw[] array from builtin entries and entries added
   1034 	 * at runtime.
   1035 	 */
   1036 	list = NULL;
   1037 	for(i=0; i < nexecs_builtin; i++)
   1038 		link_es(&list, &execsw_builtin[i]);
   1039 
   1040 	/* Add dynamically loaded entries */
   1041 	es_sz = nexecs_builtin;
   1042 	LIST_FOREACH(e2, &ex_head, ex_list) {
   1043 		link_es(&list, e2->es);
   1044 		es_sz++;
   1045 	}
   1046 
   1047 	/*
   1048 	 * Now that we have sorted all execw entries, create new execsw[]
   1049 	 * and free no longer needed memory in the process.
   1050 	 */
   1051 	new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
   1052 	for(i=0; list; i++) {
   1053 		new_es[i] = list->es;
   1054 		e1 = list->next;
   1055 		FREE(list, M_TEMP);
   1056 		list = e1;
   1057 	}
   1058 
   1059 	/*
   1060 	 * New execsw[] array built, now replace old execsw[] and free
   1061 	 * used memory.
   1062 	 */
   1063 	old_es = execsw;
   1064 	execsw = new_es;
   1065 	nexecs = es_sz;
   1066 	if (old_es)
   1067 		free((void *)old_es, M_EXEC);
   1068 
   1069 	/*
   1070 	 * Figure out the maximum size of an exec header.
   1071 	 */
   1072 	exec_maxhdrsz = 0;
   1073 	for (i = 0; i < nexecs; i++) {
   1074 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
   1075 			exec_maxhdrsz = execsw[i]->es_hdrsz;
   1076 	}
   1077 
   1078 	return 0;
   1079 }
   1080 #endif
   1081 
   1082 #ifndef LKM
   1083 /*
   1084  * Simplified exec_init() for kernels without LKMs. Only initialize
   1085  * exec_maxhdrsz and execsw[].
   1086  */
   1087 int
   1088 exec_init(int init_boot)
   1089 {
   1090 	int i;
   1091 
   1092 #ifdef DIAGNOSTIC
   1093 	if (!init_boot)
   1094 		panic("exec_init(): called with init_boot == 0");
   1095 #endif
   1096 
   1097 	/* do one-time initializations */
   1098 	lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
   1099 
   1100 	nexecs = nexecs_builtin;
   1101 	execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
   1102 
   1103 	/*
   1104 	 * Fill in execsw[] and figure out the maximum size of an exec header.
   1105 	 */
   1106 	exec_maxhdrsz = 0;
   1107 	for(i=0; i < nexecs; i++) {
   1108 		execsw[i] = &execsw_builtin[i];
   1109 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
   1110 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
   1111 	}
   1112 
   1113 	return 0;
   1114 
   1115 }
   1116 #endif /* !LKM */
   1117