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