Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_execve.c revision 1.22
      1  1.22  christos /*	$NetBSD: netbsd32_execve.c,v 1.22 2005/05/31 00:41:09 christos Exp $	*/
      2   1.1       mrg 
      3   1.1       mrg /*
      4   1.1       mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5   1.1       mrg  * All rights reserved.
      6   1.1       mrg  *
      7   1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1       mrg  * modification, are permitted provided that the following conditions
      9   1.1       mrg  * are met:
     10   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15   1.1       mrg  * 3. The name of the author may not be used to endorse or promote products
     16   1.1       mrg  *    derived from this software without specific prior written permission.
     17   1.1       mrg  *
     18   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1       mrg  * SUCH DAMAGE.
     29   1.1       mrg  */
     30   1.6     lukem 
     31   1.6     lukem #include <sys/cdefs.h>
     32  1.22  christos __KERNEL_RCSID(0, "$NetBSD: netbsd32_execve.c,v 1.22 2005/05/31 00:41:09 christos Exp $");
     33   1.1       mrg 
     34   1.3       mrg #if defined(_KERNEL_OPT)
     35   1.1       mrg #include "opt_ktrace.h"
     36   1.1       mrg #endif
     37   1.1       mrg 
     38   1.1       mrg #include <sys/param.h>
     39   1.1       mrg #include <sys/systm.h>
     40   1.1       mrg #include <sys/malloc.h>
     41   1.1       mrg #include <sys/mount.h>
     42   1.1       mrg #include <sys/stat.h>
     43   1.1       mrg #include <sys/wait.h>
     44   1.1       mrg #include <sys/ktrace.h>
     45   1.1       mrg #include <sys/vnode.h>
     46   1.1       mrg #include <sys/file.h>
     47   1.1       mrg #include <sys/filedesc.h>
     48   1.1       mrg #include <sys/namei.h>
     49   1.1       mrg 
     50   1.1       mrg #include <uvm/uvm_extern.h>
     51   1.1       mrg 
     52  1.14   thorpej #include <sys/sa.h>
     53   1.1       mrg #include <sys/syscallargs.h>
     54   1.1       mrg #include <sys/proc.h>
     55   1.1       mrg #include <sys/acct.h>
     56   1.1       mrg #include <sys/exec.h>
     57   1.1       mrg 
     58   1.1       mrg #include <compat/netbsd32/netbsd32.h>
     59   1.1       mrg #include <compat/netbsd32/netbsd32_syscall.h>
     60   1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     61   1.1       mrg 
     62  1.21      elad #include <sys/verified_exec.h>
     63  1.21      elad 
     64   1.1       mrg /* this is provided by kern/kern_exec.c */
     65  1.11   thorpej extern u_int exec_maxhdrsz;
     66   1.7  jdolecek #if defined(LKM) || defined(_LKM)
     67   1.1       mrg extern struct lock exec_lock;
     68   1.7  jdolecek #endif
     69   1.1       mrg 
     70  1.19     perry /*
     71   1.1       mrg  * Need to completly reimplement this syscall due to argument copying.
     72   1.1       mrg  */
     73   1.1       mrg /* ARGSUSED */
     74   1.1       mrg int
     75  1.14   thorpej netbsd32_execve(l, v, retval)
     76  1.14   thorpej 	struct lwp *l;
     77   1.1       mrg 	void *v;
     78   1.1       mrg 	register_t *retval;
     79   1.1       mrg {
     80   1.1       mrg 	struct netbsd32_execve_args /* {
     81   1.1       mrg 		syscallarg(const netbsd32_charp) path;
     82   1.1       mrg 		syscallarg(netbsd32_charpp) argp;
     83   1.1       mrg 		syscallarg(netbsd32_charpp) envp;
     84   1.1       mrg 	} */ *uap = v;
     85   1.1       mrg 	struct sys_execve_args ua;
     86   1.1       mrg 	caddr_t sg;
     87  1.14   thorpej 	struct proc *p = l->l_proc;
     88   1.1       mrg 
     89   1.1       mrg 	NETBSD32TOP_UAP(path, const char);
     90   1.1       mrg 	NETBSD32TOP_UAP(argp, char *);
     91   1.1       mrg 	NETBSD32TOP_UAP(envp, char *);
     92   1.8  christos 	sg = stackgap_init(p, 0);
     93  1.17      fvdl 	CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
     94   1.1       mrg 
     95  1.14   thorpej 	return netbsd32_execve2(l, &ua, retval);
     96   1.1       mrg }
     97   1.1       mrg 
     98   1.1       mrg int
     99  1.14   thorpej netbsd32_execve2(l, uap, retval)
    100  1.14   thorpej 	struct lwp *l;
    101   1.1       mrg 	struct sys_execve_args *uap;
    102   1.1       mrg 	register_t *retval;
    103   1.1       mrg {
    104   1.1       mrg 	/* Function args */
    105  1.14   thorpej 	struct proc *p = l->l_proc;
    106   1.1       mrg 	int error, i;
    107   1.1       mrg 	struct exec_package pack;
    108   1.1       mrg 	struct nameidata nid;
    109   1.1       mrg 	struct vattr attr;
    110   1.1       mrg 	struct ucred *cred = p->p_ucred;
    111   1.1       mrg 	char *argp;
    112   1.1       mrg 	netbsd32_charp const *cpp;
    113   1.1       mrg 	char *dp;
    114   1.1       mrg 	netbsd32_charp sp;
    115   1.1       mrg 	long argc, envc;
    116   1.1       mrg 	size_t len;
    117   1.1       mrg 	char *stack;
    118   1.1       mrg 	struct ps_strings arginfo;
    119   1.1       mrg 	struct vmspace *vm;
    120   1.1       mrg 	char **tmpfap;
    121   1.1       mrg 	int szsigcode;
    122   1.1       mrg 	struct exec_vmcmd *base_vcp = NULL;
    123   1.1       mrg 
    124   1.1       mrg 	/*
    125   1.1       mrg 	 * Init the namei data to point the file user's program name.
    126   1.1       mrg 	 * This is done here rather than in check_exec(), so that it's
    127   1.1       mrg 	 * possible to override this settings if any of makecmd/probe
    128   1.1       mrg 	 * functions call check_exec() recursively - for example,
    129   1.1       mrg 	 * see exec_script_makecmds().
    130   1.1       mrg 	 */
    131  1.17      fvdl 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    132   1.1       mrg 
    133   1.1       mrg 	/*
    134   1.1       mrg 	 * initialize the fields of the exec package.
    135   1.1       mrg 	 */
    136   1.1       mrg 	pack.ep_name = SCARG(uap, path);
    137   1.1       mrg 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
    138   1.1       mrg 	pack.ep_hdrlen = exec_maxhdrsz;
    139   1.1       mrg 	pack.ep_hdrvalid = 0;
    140   1.1       mrg 	pack.ep_ndp = &nid;
    141   1.1       mrg 	pack.ep_emul_arg = NULL;
    142   1.1       mrg 	pack.ep_vmcmds.evs_cnt = 0;
    143   1.1       mrg 	pack.ep_vmcmds.evs_used = 0;
    144   1.1       mrg 	pack.ep_vap = &attr;
    145   1.1       mrg 	pack.ep_flags = 0;
    146   1.1       mrg 
    147   1.7  jdolecek #if defined(LKM) || defined(_LKM)
    148   1.1       mrg 	lockmgr(&exec_lock, LK_SHARED, NULL);
    149   1.7  jdolecek #endif
    150   1.1       mrg 
    151   1.1       mrg 	/* see if we can run it. */
    152  1.21      elad #ifdef VERIFIED_EXEC
    153  1.21      elad 	if ((error = check_exec(p, &pack, VERIEXEC_DIRECT)) != 0)
    154  1.21      elad #else
    155  1.17      fvdl 	if ((error = check_exec(p, &pack)) != 0)
    156  1.21      elad #endif
    157   1.1       mrg 		goto freehdr;
    158   1.1       mrg 
    159   1.1       mrg 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    160   1.1       mrg 
    161   1.1       mrg 	/* allocate an argument buffer */
    162  1.20      yamt 	argp = (char *) uvm_km_alloc(exec_map, NCARGS, 0,
    163  1.20      yamt 	    UVM_KMF_PAGEABLE|UVM_KMF_WAITVA);
    164   1.1       mrg #ifdef DIAGNOSTIC
    165   1.1       mrg 	if (argp == (vaddr_t) 0)
    166   1.1       mrg 		panic("netbsd32_execve: argp == NULL");
    167   1.1       mrg #endif
    168   1.1       mrg 	dp = argp;
    169   1.1       mrg 	argc = 0;
    170   1.1       mrg 
    171   1.1       mrg 	/* copy the fake args list, if there's one, freeing it as we go */
    172   1.1       mrg 	if (pack.ep_flags & EXEC_HASARGL) {
    173   1.1       mrg 		tmpfap = pack.ep_fa;
    174   1.1       mrg 		while (*tmpfap != NULL) {
    175   1.1       mrg 			char *cp;
    176   1.1       mrg 
    177   1.1       mrg 			cp = *tmpfap;
    178   1.1       mrg 			while (*cp)
    179   1.1       mrg 				*dp++ = *cp++;
    180   1.1       mrg 			dp++;
    181   1.1       mrg 
    182   1.1       mrg 			FREE(*tmpfap, M_EXEC);
    183   1.1       mrg 			tmpfap++; argc++;
    184   1.1       mrg 		}
    185   1.1       mrg 		FREE(pack.ep_fa, M_EXEC);
    186   1.1       mrg 		pack.ep_flags &= ~EXEC_HASARGL;
    187   1.1       mrg 	}
    188   1.1       mrg 
    189   1.1       mrg 	/* Now get argv & environment */
    190  1.22  christos 	if (!(cpp = (const netbsd32_charp *)SCARG(uap, argp))) {
    191   1.1       mrg 		error = EINVAL;
    192   1.1       mrg 		goto bad;
    193   1.1       mrg 	}
    194   1.1       mrg 
    195   1.1       mrg 	if (pack.ep_flags & EXEC_SKIPARG)
    196   1.1       mrg 		cpp++;
    197   1.1       mrg 
    198   1.1       mrg 	while (1) {
    199   1.1       mrg 		len = argp + ARG_MAX - dp;
    200   1.1       mrg 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    201   1.1       mrg 			goto bad;
    202   1.1       mrg 		if (!sp)
    203   1.1       mrg 			break;
    204  1.19     perry 		if ((error = copyinstr((char *)(u_long)sp, dp,
    205   1.1       mrg 				       len, &len)) != 0) {
    206   1.1       mrg 			if (error == ENAMETOOLONG)
    207   1.1       mrg 				error = E2BIG;
    208   1.1       mrg 			goto bad;
    209   1.1       mrg 		}
    210   1.1       mrg 		dp += len;
    211   1.1       mrg 		cpp++;
    212   1.1       mrg 		argc++;
    213   1.1       mrg 	}
    214   1.1       mrg 
    215   1.1       mrg 	envc = 0;
    216   1.1       mrg 	/* environment need not be there */
    217  1.22  christos 	if ((cpp = (const netbsd32_charp *)SCARG(uap, envp)) != NULL ) {
    218   1.1       mrg 		while (1) {
    219   1.1       mrg 			len = argp + ARG_MAX - dp;
    220   1.1       mrg 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    221   1.1       mrg 				goto bad;
    222   1.1       mrg 			if (!sp)
    223   1.1       mrg 				break;
    224  1.19     perry 			if ((error = copyinstr((char *)(u_long)sp,
    225   1.1       mrg 					       dp, len, &len)) != 0) {
    226   1.1       mrg 				if (error == ENAMETOOLONG)
    227   1.1       mrg 					error = E2BIG;
    228   1.1       mrg 				goto bad;
    229   1.1       mrg 			}
    230   1.1       mrg 			dp += len;
    231   1.1       mrg 			cpp++;
    232   1.1       mrg 			envc++;
    233   1.1       mrg 		}
    234   1.1       mrg 	}
    235   1.1       mrg 
    236   1.1       mrg 	dp = (char *) ALIGN(dp);
    237   1.1       mrg 
    238   1.1       mrg 	szsigcode = pack.ep_es->es_emul->e_esigcode -
    239   1.1       mrg 	    pack.ep_es->es_emul->e_sigcode;
    240   1.1       mrg 
    241   1.1       mrg 	/* Now check if args & environ fit into new stack */
    242   1.1       mrg 	if (pack.ep_flags & EXEC_32)
    243   1.1       mrg 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    244   1.1       mrg 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
    245   1.1       mrg 		    szsigcode + sizeof(struct ps_strings)) - argp;
    246   1.1       mrg 	else
    247   1.1       mrg 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    248   1.1       mrg 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
    249   1.1       mrg 		    szsigcode + sizeof(struct ps_strings)) - argp;
    250   1.1       mrg 
    251   1.1       mrg 	len = ALIGN(len);	/* make the stack "safely" aligned */
    252   1.1       mrg 
    253   1.1       mrg 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    254   1.1       mrg 		error = ENOMEM;
    255   1.1       mrg 		goto bad;
    256   1.1       mrg 	}
    257   1.1       mrg 
    258   1.1       mrg 	/* adjust "active stack depth" for process VSZ */
    259   1.1       mrg 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    260   1.1       mrg 
    261   1.1       mrg 	/*
    262   1.1       mrg 	 * Do whatever is necessary to prepare the address space
    263   1.1       mrg 	 * for remapping.  Note that this might replace the current
    264   1.1       mrg 	 * vmspace with another!
    265   1.1       mrg 	 */
    266  1.14   thorpej 	uvmspace_exec(l, VM_MIN_ADDRESS, (vaddr_t)pack.ep_minsaddr);
    267   1.1       mrg 
    268   1.1       mrg 	/* Now map address space */
    269   1.1       mrg 	vm = p->p_vmspace;
    270   1.1       mrg 	vm->vm_taddr = (char *) pack.ep_taddr;
    271   1.1       mrg 	vm->vm_tsize = btoc(pack.ep_tsize);
    272   1.1       mrg 	vm->vm_daddr = (char *) pack.ep_daddr;
    273   1.1       mrg 	vm->vm_dsize = btoc(pack.ep_dsize);
    274   1.1       mrg 	vm->vm_ssize = btoc(pack.ep_ssize);
    275   1.1       mrg 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
    276   1.1       mrg 	vm->vm_minsaddr = (char *) pack.ep_minsaddr;
    277   1.1       mrg 
    278   1.1       mrg 	/* create the new process's VM space by running the vmcmds */
    279   1.1       mrg #ifdef DIAGNOSTIC
    280   1.1       mrg 	if (pack.ep_vmcmds.evs_used == 0)
    281   1.1       mrg 		panic("netbsd32_execve: no vmcmds");
    282   1.1       mrg #endif
    283   1.1       mrg 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    284   1.1       mrg 		struct exec_vmcmd *vcp;
    285   1.1       mrg 
    286   1.1       mrg 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    287   1.1       mrg 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    288   1.1       mrg #ifdef DIAGNOSTIC
    289   1.1       mrg 			if (base_vcp == NULL)
    290   1.1       mrg 				panic("netbsd32_execve: relative vmcmd with no base");
    291   1.1       mrg 			if (vcp->ev_flags & VMCMD_BASE)
    292   1.1       mrg 				panic("netbsd32_execve: illegal base & relative vmcmd");
    293   1.1       mrg #endif
    294   1.1       mrg 			vcp->ev_addr += base_vcp->ev_addr;
    295   1.1       mrg 		}
    296  1.17      fvdl 		error = (*vcp->ev_proc)(p, vcp);
    297   1.1       mrg #ifdef DEBUG
    298   1.1       mrg 		if (error) {
    299   1.2       mrg 			int j;
    300   1.2       mrg 
    301   1.2       mrg 			for (j = 0; j <= i; j++)
    302   1.2       mrg 				printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", j,
    303   1.2       mrg 				       vcp[j-i].ev_addr, vcp[j-i].ev_len,
    304   1.2       mrg 				       vcp[j-i].ev_offset);
    305   1.1       mrg 		}
    306   1.1       mrg #endif
    307   1.1       mrg 		if (vcp->ev_flags & VMCMD_BASE)
    308   1.1       mrg 			base_vcp = vcp;
    309   1.1       mrg 	}
    310   1.1       mrg 
    311   1.1       mrg 	/* free the vmspace-creation commands, and release their references */
    312   1.1       mrg 	kill_vmcmds(&pack.ep_vmcmds);
    313   1.1       mrg 
    314   1.1       mrg 	/* if an error happened, deallocate and punt */
    315   1.1       mrg 	if (error) {
    316   1.1       mrg #ifdef DEBUG
    317   1.1       mrg 		printf("netbsd32_execve: vmcmd %i failed: %d\n", i-1, error);
    318   1.1       mrg #endif
    319   1.1       mrg 		goto exec_abort;
    320   1.1       mrg 	}
    321   1.1       mrg 
    322   1.1       mrg 	/* remember information about the process */
    323   1.1       mrg 	arginfo.ps_nargvstr = argc;
    324   1.1       mrg 	arginfo.ps_nenvstr = envc;
    325   1.1       mrg 
    326   1.1       mrg 	stack = (char *) (vm->vm_minsaddr - len);
    327   1.1       mrg 	/* Now copy argc, args & environ to new stack */
    328  1.17      fvdl 	error = (*pack.ep_es->es_copyargs)(p, &pack, &arginfo,
    329   1.5  christos 	    &stack, argp);
    330   1.5  christos 	if (error) {
    331   1.1       mrg #ifdef DEBUG
    332   1.1       mrg 		printf("netbsd32_execve: copyargs failed\n");
    333   1.1       mrg #endif
    334   1.1       mrg 		goto exec_abort;
    335   1.1       mrg 	}
    336   1.5  christos 	/* restore the stack back to its original point */
    337   1.5  christos 	stack = (char *) (vm->vm_minsaddr - len);
    338   1.1       mrg 
    339   1.1       mrg 	/* fill process ps_strings info */
    340  1.10      fvdl 	p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr -
    341  1.10      fvdl 	    sizeof(struct ps_strings));
    342   1.1       mrg 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    343   1.1       mrg 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    344   1.1       mrg 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    345   1.1       mrg 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    346   1.1       mrg 
    347   1.1       mrg 	/* copy out the process's ps_strings structure */
    348   1.1       mrg 	if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
    349   1.1       mrg #ifdef DEBUG
    350   1.1       mrg 		printf("netbsd32_execve: ps_strings copyout failed\n");
    351   1.1       mrg #endif
    352   1.1       mrg 		goto exec_abort;
    353   1.1       mrg 	}
    354   1.1       mrg 
    355   1.1       mrg 	/* copy out the process's signal trapoline code */
    356   1.1       mrg 	if (szsigcode) {
    357   1.1       mrg 		if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
    358   1.1       mrg 		    p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
    359   1.1       mrg 		    szsigcode)) {
    360   1.1       mrg #ifdef DEBUG
    361   1.1       mrg 			printf("netbsd32_execve: sig trampoline copyout failed\n");
    362   1.1       mrg #endif
    363   1.1       mrg 			goto exec_abort;
    364   1.1       mrg 		}
    365   1.1       mrg #ifdef PMAP_NEED_PROCWR
    366   1.1       mrg 		/* This is code. Let the pmap do what is needed. */
    367  1.13    martin 		pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
    368   1.1       mrg #endif
    369   1.1       mrg 	}
    370   1.1       mrg 
    371   1.1       mrg 	stopprofclock(p);	/* stop profiling */
    372  1.17      fvdl 	fdcloseexec(p);		/* handle close on exec */
    373   1.1       mrg 	execsigs(p);		/* reset catched signals */
    374  1.14   thorpej 	l->l_ctxlink = NULL;	/* reset ucontext link */
    375   1.1       mrg 
    376   1.1       mrg 	/* set command name & other accounting info */
    377   1.1       mrg 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    378   1.1       mrg 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
    379   1.1       mrg 	p->p_comm[len] = 0;
    380   1.1       mrg 	p->p_acflag &= ~AFORK;
    381   1.1       mrg 
    382   1.1       mrg 	/* record proc's vnode, for use by procfs and others */
    383   1.1       mrg         if (p->p_textvp)
    384   1.1       mrg                 vrele(p->p_textvp);
    385   1.1       mrg 	VREF(pack.ep_vp);
    386   1.1       mrg 	p->p_textvp = pack.ep_vp;
    387   1.1       mrg 
    388   1.1       mrg 	p->p_flag |= P_EXEC;
    389   1.1       mrg 	if (p->p_flag & P_PPWAIT) {
    390   1.1       mrg 		p->p_flag &= ~P_PPWAIT;
    391   1.1       mrg 		wakeup((caddr_t) p->p_pptr);
    392   1.1       mrg 	}
    393   1.1       mrg 
    394   1.1       mrg 	/*
    395   1.1       mrg 	 * deal with set[ug]id.
    396   1.4   thorpej 	 * MNT_NOSUID has already been used to disable s[ug]id.
    397   1.1       mrg 	 */
    398   1.4   thorpej 	if ((p->p_flag & P_TRACED) == 0 &&
    399   1.4   thorpej 
    400   1.4   thorpej 	    (((attr.va_mode & S_ISUID) != 0 &&
    401   1.4   thorpej 	      p->p_ucred->cr_uid != attr.va_uid) ||
    402   1.4   thorpej 
    403   1.4   thorpej 	     ((attr.va_mode & S_ISGID) != 0 &&
    404   1.4   thorpej 	      p->p_ucred->cr_gid != attr.va_gid))) {
    405   1.4   thorpej 		/*
    406   1.4   thorpej 		 * Mark the process as SUGID before we do
    407   1.4   thorpej 		 * anything that might block.
    408   1.4   thorpej 		 */
    409   1.4   thorpej 		p_sugid(p);
    410   1.4   thorpej 
    411   1.1       mrg 		p->p_ucred = crcopy(cred);
    412   1.1       mrg #ifdef KTRACE
    413   1.1       mrg 		/*
    414   1.1       mrg 		 * If process is being ktraced, turn off - unless
    415   1.1       mrg 		 * root set it.
    416   1.1       mrg 		 */
    417   1.1       mrg 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
    418   1.1       mrg 			ktrderef(p);
    419   1.1       mrg #endif
    420   1.1       mrg 		if (attr.va_mode & S_ISUID)
    421   1.1       mrg 			p->p_ucred->cr_uid = attr.va_uid;
    422   1.1       mrg 		if (attr.va_mode & S_ISGID)
    423   1.1       mrg 			p->p_ucred->cr_gid = attr.va_gid;
    424   1.1       mrg 	} else
    425   1.1       mrg 		p->p_flag &= ~P_SUGID;
    426   1.1       mrg 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    427   1.1       mrg 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    428   1.1       mrg 
    429   1.1       mrg 	doexechooks(p);
    430   1.1       mrg 
    431  1.20      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
    432   1.1       mrg 
    433   1.1       mrg 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    434   1.1       mrg 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    435  1.17      fvdl 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    436   1.1       mrg 	vput(pack.ep_vp);
    437   1.1       mrg 
    438   1.1       mrg 	/* setup new registers and do misc. setup. */
    439  1.14   thorpej 	(*pack.ep_es->es_emul->e_setregs)(l, &pack, (u_long) stack);
    440   1.9  jdolecek 	if (pack.ep_es->es_setregs)
    441  1.14   thorpej 		(*pack.ep_es->es_setregs)(l, &pack, (u_long) stack);
    442   1.1       mrg 
    443   1.1       mrg 	if (p->p_flag & P_TRACED)
    444   1.1       mrg 		psignal(p, SIGTRAP);
    445   1.1       mrg 
    446   1.1       mrg 	free(pack.ep_hdr, M_EXEC);
    447   1.1       mrg 
    448   1.1       mrg 	/*
    449   1.1       mrg 	 * Call emulation specific exec hook. This can setup setup per-process
    450   1.1       mrg 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    451   1.1       mrg 	 *
    452   1.1       mrg 	 * If we are executing process of different emulation than the
    453   1.1       mrg 	 * original forked process, call e_proc_exit() of the old emulation
    454   1.1       mrg 	 * first, then e_proc_exec() of new emulation. If the emulation is
    455   1.1       mrg 	 * same, the exec hook code should deallocate any old emulation
    456   1.1       mrg 	 * resources held previously by this process.
    457   1.1       mrg 	 */
    458   1.1       mrg 	if (p->p_emul && p->p_emul->e_proc_exit
    459   1.1       mrg 	    && p->p_emul != pack.ep_es->es_emul)
    460   1.1       mrg 		(*p->p_emul->e_proc_exit)(p);
    461   1.1       mrg 
    462   1.1       mrg 	/*
    463   1.1       mrg 	 * Call exec hook. Emulation code may NOT store reference to anything
    464   1.1       mrg 	 * from &pack.
    465   1.1       mrg 	 */
    466   1.1       mrg         if (pack.ep_es->es_emul->e_proc_exec)
    467   1.1       mrg                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
    468   1.1       mrg 
    469   1.1       mrg 	/* update p_emul, the old value is no longer needed */
    470   1.1       mrg 	p->p_emul = pack.ep_es->es_emul;
    471   1.1       mrg 
    472  1.18  drochner 	/* ...and the same for p_execsw */
    473  1.18  drochner 	p->p_execsw = pack.ep_es;
    474  1.18  drochner 
    475  1.18  drochner #ifdef __HAVE_SYSCALL_INTERN
    476  1.18  drochner 	(*p->p_emul->e_syscall_intern)(p);
    477  1.18  drochner #endif
    478   1.1       mrg #ifdef KTRACE
    479   1.1       mrg 	if (KTRPOINT(p, KTR_EMUL))
    480  1.17      fvdl 		ktremul(p);
    481   1.1       mrg #endif
    482   1.1       mrg 
    483   1.7  jdolecek #if defined(LKM) || defined(_LKM)
    484   1.1       mrg 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    485   1.7  jdolecek #endif
    486   1.1       mrg 
    487   1.1       mrg 	return (EJUSTRETURN);
    488   1.1       mrg 
    489   1.1       mrg bad:
    490   1.1       mrg 	/* free the vmspace-creation commands, and release their references */
    491   1.1       mrg 	kill_vmcmds(&pack.ep_vmcmds);
    492   1.1       mrg 	/* kill any opened file descriptor, if necessary */
    493   1.1       mrg 	if (pack.ep_flags & EXEC_HASFD) {
    494   1.1       mrg 		pack.ep_flags &= ~EXEC_HASFD;
    495  1.17      fvdl 		(void) fdrelease(p, pack.ep_fd);
    496   1.1       mrg 	}
    497   1.1       mrg 	/* close and put the exec'd file */
    498   1.1       mrg 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    499  1.17      fvdl 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    500   1.1       mrg 	vput(pack.ep_vp);
    501   1.1       mrg 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    502  1.20      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
    503   1.1       mrg 
    504   1.1       mrg freehdr:
    505   1.7  jdolecek #if defined(LKM) || defined(_LKM)
    506   1.1       mrg 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    507   1.7  jdolecek #endif
    508   1.1       mrg 
    509   1.1       mrg 	free(pack.ep_hdr, M_EXEC);
    510   1.1       mrg 	return error;
    511   1.1       mrg 
    512   1.1       mrg exec_abort:
    513   1.7  jdolecek #if defined(LKM) || defined(_LKM)
    514   1.1       mrg 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    515   1.7  jdolecek #endif
    516   1.1       mrg 
    517   1.1       mrg 	/*
    518   1.1       mrg 	 * the old process doesn't exist anymore.  exit gracefully.
    519   1.1       mrg 	 * get rid of the (new) address space we have created, if any, get rid
    520   1.1       mrg 	 * of our namei data and vnode, and exit noting failure
    521   1.1       mrg 	 */
    522   1.1       mrg 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    523   1.1       mrg 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    524   1.1       mrg 	if (pack.ep_emul_arg)
    525   1.1       mrg 		FREE(pack.ep_emul_arg, M_TEMP);
    526   1.1       mrg 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    527   1.1       mrg 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    528  1.17      fvdl 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    529   1.1       mrg 	vput(pack.ep_vp);
    530  1.20      yamt 	uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
    531   1.1       mrg 	free(pack.ep_hdr, M_EXEC);
    532  1.14   thorpej 	exit1(l, W_EXITCODE(error, SIGABRT));
    533   1.1       mrg 
    534   1.1       mrg 	/* NOTREACHED */
    535   1.1       mrg 	return 0;
    536   1.1       mrg }
    537