xref: /freebsd/sys/kern/kern_exec.c (revision c8a7999933a3c8b9918feabe0c4798cc5fc862cb)
126f9a767SRodney W. Grimes /*
226f9a767SRodney W. Grimes  * Copyright (c) 1993, David Greenman
326f9a767SRodney W. Grimes  * All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  *
1426f9a767SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1726f9a767SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
25df8bae1dSRodney W. Grimes  *
26c8a79999SPeter Wemm  *	$Id: kern_exec.c,v 1.78 1998/02/25 13:08:06 bde Exp $
27df8bae1dSRodney W. Grimes  */
28df8bae1dSRodney W. Grimes 
29df8bae1dSRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/systm.h>
31d2d3e875SBruce Evans #include <sys/sysproto.h>
3226f9a767SRodney W. Grimes #include <sys/signalvar.h>
3326f9a767SRodney W. Grimes #include <sys/kernel.h>
3426f9a767SRodney W. Grimes #include <sys/mount.h>
35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
36079cc25bSDavid Greenman #include <sys/fcntl.h>
3726f9a767SRodney W. Grimes #include <sys/acct.h>
3826f9a767SRodney W. Grimes #include <sys/exec.h>
3926f9a767SRodney W. Grimes #include <sys/imgact.h>
40e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4126f9a767SRodney W. Grimes #include <sys/wait.h>
42a794e791SBruce Evans #include <sys/proc.h>
432a024a2bSSean Eric Fagan #include <sys/pioctl.h>
44a794e791SBruce Evans #include <sys/namei.h>
451e1e0b44SSøren Schmidt #include <sys/sysent.h>
46797f2d22SPoul-Henning Kamp #include <sys/shm.h>
4799ac3bc8SPeter Wemm #include <sys/sysctl.h>
48a794e791SBruce Evans #include <sys/vnode.h>
499caaadb6SDavid Greenman #include <sys/buf.h>
5026f9a767SRodney W. Grimes 
5126f9a767SRodney W. Grimes #include <vm/vm.h>
52efeaf95aSDavid Greenman #include <vm/vm_param.h>
53efeaf95aSDavid Greenman #include <vm/vm_prot.h>
54996c772fSJohn Dyson #include <sys/lock.h>
55efeaf95aSDavid Greenman #include <vm/pmap.h>
561616db3cSJohn Dyson #include <vm/vm_page.h>
57efeaf95aSDavid Greenman #include <vm/vm_map.h>
5826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
59efeaf95aSDavid Greenman #include <vm/vm_extern.h>
601ebd0c59SDavid Greenman #include <vm/vm_object.h>
61675ea6f0SBruce Evans #include <vm/vm_zone.h>
621616db3cSJohn Dyson #include <vm/vm_pager.h>
6326f9a767SRodney W. Grimes 
6426f9a767SRodney W. Grimes #include <machine/reg.h>
6526f9a767SRodney W. Grimes 
6687b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *));
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes /*
6999ac3bc8SPeter Wemm  * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts
7099ac3bc8SPeter Wemm  * of the sysctl code also assumes this, and sizeof(int) == sizeof(long).
7199ac3bc8SPeter Wemm  */
726120fef1SDavid Greenman static struct ps_strings *ps_strings = PS_STRINGS;
7399ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, "");
7499ac3bc8SPeter Wemm 
7599ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK;
7699ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, "");
7799ac3bc8SPeter Wemm 
7899ac3bc8SPeter Wemm /*
7926f9a767SRodney W. Grimes  * execsw_set is constructed for us by the linker.  Each of the items
8026f9a767SRodney W. Grimes  * is a pointer to a `const struct execsw', hence the double pointer here.
81df8bae1dSRodney W. Grimes  */
8287b6de2bSPoul-Henning Kamp static const struct execsw **execsw =
8387b6de2bSPoul-Henning Kamp 	(const struct execsw **)&execsw_set.ls_items[0];
8426f9a767SRodney W. Grimes 
85d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
86ad7507e2SSteven Wallace struct execve_args {
87ad7507e2SSteven Wallace         char    *fname;
88ad7507e2SSteven Wallace         char    **argv;
89ad7507e2SSteven Wallace         char    **envv;
90ad7507e2SSteven Wallace };
91d2d3e875SBruce Evans #endif
92ad7507e2SSteven Wallace 
9326f9a767SRodney W. Grimes /*
9426f9a767SRodney W. Grimes  * execve() system call.
9526f9a767SRodney W. Grimes  */
9626f9a767SRodney W. Grimes int
97cb226aaaSPoul-Henning Kamp execve(p, uap)
9826f9a767SRodney W. Grimes 	struct proc *p;
9926f9a767SRodney W. Grimes 	register struct execve_args *uap;
100df8bae1dSRodney W. Grimes {
10126f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
10226f9a767SRodney W. Grimes 	int *stack_base;
103bb56ec4aSPoul-Henning Kamp 	int error, len, i;
104c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10526f9a767SRodney W. Grimes 	struct vattr attr;
10626f9a767SRodney W. Grimes 
107c52007c2SDavid Greenman 	imgp = &image_params;
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	/*
110c52007c2SDavid Greenman 	 * Initialize part of the common data
111df8bae1dSRodney W. Grimes 	 */
112c52007c2SDavid Greenman 	imgp->proc = p;
113c52007c2SDavid Greenman 	imgp->uap = uap;
114c52007c2SDavid Greenman 	imgp->attr = &attr;
115c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1165cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
117c52007c2SDavid Greenman 	imgp->entry_addr = 0;
118c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
119c52007c2SDavid Greenman 	imgp->interpreted = 0;
120c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
121e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1221616db3cSJohn Dyson 	imgp->vp = NULL;
1231616db3cSJohn Dyson 	imgp->firstpage = NULL;
12426f9a767SRodney W. Grimes 
12526f9a767SRodney W. Grimes 	/*
12626f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
12726f9a767SRodney W. Grimes 	 *	environment strings
12826f9a767SRodney W. Grimes 	 */
1291616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
130c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
13126f9a767SRodney W. Grimes 		error = ENOMEM;
13226f9a767SRodney W. Grimes 		goto exec_fail;
13326f9a767SRodney W. Grimes 	}
134c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
135c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1361616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
13726f9a767SRodney W. Grimes 
13826f9a767SRodney W. Grimes 	/*
13926f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
14026f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
14126f9a767SRodney W. Grimes 	 */
14226f9a767SRodney W. Grimes 	ndp = &nd;
143b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1443ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
14526f9a767SRodney W. Grimes 
14626f9a767SRodney W. Grimes interpret:
14726f9a767SRodney W. Grimes 
14826f9a767SRodney W. Grimes 	error = namei(ndp);
14926f9a767SRodney W. Grimes 	if (error) {
1501616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1511616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
15226f9a767SRodney W. Grimes 		goto exec_fail;
15326f9a767SRodney W. Grimes 	}
15426f9a767SRodney W. Grimes 
155c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
15626f9a767SRodney W. Grimes 
15726f9a767SRodney W. Grimes 	/*
1589022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1599022e4adSDavid Greenman 	 */
160c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1618677f509SDavid Greenman 	if (error) {
1628677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
16326f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1648677f509SDavid Greenman 	}
16526f9a767SRodney W. Grimes 
1661616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1679caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1686d5a0a8cSDavid Greenman 	if (error)
16926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
17026f9a767SRodney W. Grimes 
17126f9a767SRodney W. Grimes 	/*
17226f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
17326f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
17426f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
17526f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
17626f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
17726f9a767SRodney W. Grimes 	 *	the interpreter.
17826f9a767SRodney W. Grimes 	 */
17926f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
18026f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
181c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
18226f9a767SRodney W. Grimes 		else
18326f9a767SRodney W. Grimes 			continue;
18426f9a767SRodney W. Grimes 		if (error == -1)
18526f9a767SRodney W. Grimes 			continue;
18626f9a767SRodney W. Grimes 		if (error)
18726f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
188c52007c2SDavid Greenman 		if (imgp->interpreted) {
1891616db3cSJohn Dyson 			exec_unmap_first_page(imgp);
19026f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
191f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
19299448ed1SJohn Dyson 			zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
19326f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
194b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
195c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
19626f9a767SRodney W. Grimes 			goto interpret;
19726f9a767SRodney W. Grimes 		}
19826f9a767SRodney W. Grimes 		break;
19926f9a767SRodney W. Grimes 	}
20026f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
20126f9a767SRodney W. Grimes 	if (error == -1) {
20226f9a767SRodney W. Grimes 		error = ENOEXEC;
20326f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
20426f9a767SRodney W. Grimes 	}
20526f9a767SRodney W. Grimes 
20626f9a767SRodney W. Grimes 	/*
20726f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
20826f9a767SRodney W. Grimes 	 */
209c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
21026f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes 	/*
2131e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2141e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2151e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
21626f9a767SRodney W. Grimes 	 */
2171e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
218c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2191e1e0b44SSøren Schmidt 	else
220c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
22126f9a767SRodney W. Grimes 
222a78e8d2aSDavid Greenman 	/*
223a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
224a78e8d2aSDavid Greenman 	 * be shared after an exec.
225a78e8d2aSDavid Greenman 	 */
226a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
227a78e8d2aSDavid Greenman 		struct filedesc *tmp;
228a78e8d2aSDavid Greenman 
229a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
230a78e8d2aSDavid Greenman 		fdfree(p);
231a78e8d2aSDavid Greenman 		p->p_fd = tmp;
232a78e8d2aSDavid Greenman 	}
233a78e8d2aSDavid Greenman 
23426f9a767SRodney W. Grimes 	/* close files on exec */
23526f9a767SRodney W. Grimes 	fdcloseexec(p);
23626f9a767SRodney W. Grimes 
23726f9a767SRodney W. Grimes 	/* reset caught signals */
23826f9a767SRodney W. Grimes 	execsigs(p);
23926f9a767SRodney W. Grimes 
24026f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
24126f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
24226f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
24326f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
24426f9a767SRodney W. Grimes 
24526f9a767SRodney W. Grimes 	/*
24624b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
24726f9a767SRodney W. Grimes 	 * it that it now has it's own resources back
24826f9a767SRodney W. Grimes 	 */
24926f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
25026f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
25126f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
25226f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
25326f9a767SRodney W. Grimes 	}
25426f9a767SRodney W. Grimes 
25526f9a767SRodney W. Grimes 	/*
256a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
257a78e8d2aSDavid Greenman 	 *
258a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
259a78e8d2aSDavid Greenman 	 * the process is being traced.
260c52007c2SDavid Greenman 	 */
261d021ae3dSGuido van Rooij 	if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid ||
262d021ae3dSGuido van Rooij 	     attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) &&
263a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
264c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
265c52007c2SDavid Greenman 		/*
266c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
26726f9a767SRodney W. Grimes 		 * root.
26826f9a767SRodney W. Grimes 		 */
269c52007c2SDavid Greenman 		if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
27026f9a767SRodney W. Grimes 			p->p_traceflag = 0;
27126f9a767SRodney W. Grimes 			vrele(p->p_tracep);
272c52007c2SDavid Greenman 			p->p_tracep = NULL;
27326f9a767SRodney W. Grimes 		}
274c52007c2SDavid Greenman 		/*
275c52007c2SDavid Greenman 		 * Set the new credentials.
276c52007c2SDavid Greenman 		 */
27726f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
278c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
27926f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
280c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
281d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
282d5f81602SSean Eric Fagan 		setsugid(p);
283c52007c2SDavid Greenman 	} else {
284e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
285e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
286c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
28726f9a767SRodney W. Grimes 	}
28826f9a767SRodney W. Grimes 
28926f9a767SRodney W. Grimes 	/*
290c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
29126f9a767SRodney W. Grimes 	 */
29226f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
29326f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
29426f9a767SRodney W. Grimes 
29526f9a767SRodney W. Grimes 	/*
2962a531c80SDavid Greenman 	 * Store the vp for use in procfs
2972a531c80SDavid Greenman 	 */
2982a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
2992a531c80SDavid Greenman 		vrele(p->p_textvp);
3002a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3012a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3022a531c80SDavid Greenman 
3032a531c80SDavid Greenman 	/*
30426f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
30526f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
30626f9a767SRodney W. Grimes 	 */
3072a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3082a024a2bSSean Eric Fagan 
30926f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
31026f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
31126f9a767SRodney W. Grimes 
31226f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
31326f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
31426f9a767SRodney W. Grimes 
31526f9a767SRodney W. Grimes 	/* Set entry address */
316c52007c2SDavid Greenman 	setregs(p, imgp->entry_addr, (u_long)stack_base);
31726f9a767SRodney W. Grimes 
3181616db3cSJohn Dyson exec_fail_dealloc:
3191616db3cSJohn Dyson 
32026f9a767SRodney W. Grimes 	/*
32126f9a767SRodney W. Grimes 	 * free various allocated resources
32226f9a767SRodney W. Grimes 	 */
3231616db3cSJohn Dyson 	if (imgp->firstpage)
3241616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
32526f9a767SRodney W. Grimes 
326c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3271616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3281616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3291616db3cSJohn Dyson 
330a3cf6ebaSDavid Greenman 	if (ndp->ni_vp) {
331f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
33299448ed1SJohn Dyson 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
333a3cf6ebaSDavid Greenman 	}
33426f9a767SRodney W. Grimes 
3351616db3cSJohn Dyson 	if (error == 0)
3361616db3cSJohn Dyson 		return (0);
3371616db3cSJohn Dyson 
33826f9a767SRodney W. Grimes exec_fail:
339c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
34026f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
34126f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
34226f9a767SRodney W. Grimes 		/* NOT REACHED */
34326f9a767SRodney W. Grimes 		return(0);
34426f9a767SRodney W. Grimes 	} else {
34526f9a767SRodney W. Grimes 		return(error);
34626f9a767SRodney W. Grimes 	}
34726f9a767SRodney W. Grimes }
34826f9a767SRodney W. Grimes 
3491616db3cSJohn Dyson int
3501616db3cSJohn Dyson exec_map_first_page(imgp)
3511616db3cSJohn Dyson 	struct image_params *imgp;
3521616db3cSJohn Dyson {
35395461b45SJohn Dyson 	int s, rv, i;
35495461b45SJohn Dyson 	int initial_pagein;
35595461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3561616db3cSJohn Dyson 	vm_object_t object;
3571616db3cSJohn Dyson 
3581616db3cSJohn Dyson 
3591616db3cSJohn Dyson 	if (imgp->firstpage) {
3601616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
3611616db3cSJohn Dyson 	}
3621616db3cSJohn Dyson 
3631616db3cSJohn Dyson 	object = imgp->vp->v_object;
3641616db3cSJohn Dyson 	s = splvm();
3651616db3cSJohn Dyson 
36695461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3671616db3cSJohn Dyson 
36895461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
36995461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
37095461b45SJohn Dyson 		if (initial_pagein > object->size)
37195461b45SJohn Dyson 			initial_pagein = object->size;
37295461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
37395461b45SJohn Dyson 			if (ma[i] = vm_page_lookup(object, i)) {
37495461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
37595461b45SJohn Dyson 					break;
37695461b45SJohn Dyson 				if (ma[i]->valid)
37795461b45SJohn Dyson 					break;
37895461b45SJohn Dyson 				ma[i]->flags |= PG_BUSY;
37995461b45SJohn Dyson 			} else {
38095461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
38195461b45SJohn Dyson 				if (ma[i] == NULL)
38295461b45SJohn Dyson 					break;
38395461b45SJohn Dyson 			}
38495461b45SJohn Dyson 		}
38595461b45SJohn Dyson 		initial_pagein = i;
3861616db3cSJohn Dyson 
38795461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
38895461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
38995461b45SJohn Dyson 
39095461b45SJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL)) {
39195461b45SJohn Dyson 			vm_page_protect(ma[0], VM_PROT_NONE);
39295461b45SJohn Dyson 			vm_page_deactivate(ma[0]);
39395461b45SJohn Dyson 			PAGE_WAKEUP(ma[0]);
3941616db3cSJohn Dyson 			splx(s);
3951616db3cSJohn Dyson 			return EIO;
3961616db3cSJohn Dyson 		}
3971616db3cSJohn Dyson 	}
3981616db3cSJohn Dyson 
39995461b45SJohn Dyson 	vm_page_wire(ma[0]);
40095461b45SJohn Dyson 	PAGE_WAKEUP(ma[0]);
4011616db3cSJohn Dyson 	splx(s);
4021616db3cSJohn Dyson 
40395461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
40495461b45SJohn Dyson 	imgp->firstpage = ma[0];
4051616db3cSJohn Dyson 
4061616db3cSJohn Dyson 	return 0;
4071616db3cSJohn Dyson }
4081616db3cSJohn Dyson 
4091616db3cSJohn Dyson void
4101616db3cSJohn Dyson exec_unmap_first_page(imgp)
4111616db3cSJohn Dyson 	struct image_params *imgp;
4121616db3cSJohn Dyson {
4131616db3cSJohn Dyson 	if (imgp->firstpage) {
4141616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
4151616db3cSJohn Dyson 		vm_page_unwire(imgp->firstpage);
4161616db3cSJohn Dyson 		imgp->firstpage = NULL;
4171616db3cSJohn Dyson 	}
4181616db3cSJohn Dyson }
4191616db3cSJohn Dyson 
42026f9a767SRodney W. Grimes /*
42126f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
42226f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
42326f9a767SRodney W. Grimes  *	automatically in trap.c.
42426f9a767SRodney W. Grimes  */
42526f9a767SRodney W. Grimes int
426c52007c2SDavid Greenman exec_new_vmspace(imgp)
427c52007c2SDavid Greenman 	struct image_params *imgp;
42826f9a767SRodney W. Grimes {
42926f9a767SRodney W. Grimes 	int error;
430c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
43126f9a767SRodney W. Grimes 	caddr_t	stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
432af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
43326f9a767SRodney W. Grimes 
434c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
43526f9a767SRodney W. Grimes 
436af9ec885SJohn Dyson 	/*
437af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
438af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
439af9ec885SJohn Dyson 	 * not disrupted
440af9ec885SJohn Dyson 	 */
441af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4423d903220SDoug Rabson 		if (vmspace->vm_shm)
443c52007c2SDavid Greenman 			shmexit(imgp->proc);
4449d3fbbb5SJohn Dyson 		pmap_remove_pages(&vmspace->vm_pmap, 0, USRSTACK);
445af9ec885SJohn Dyson 		vm_map_remove(map, 0, USRSTACK);
446af9ec885SJohn Dyson 	} else {
447492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
448492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
449af9ec885SJohn Dyson 		map = &vmspace->vm_map;
450af9ec885SJohn Dyson 	}
45126f9a767SRodney W. Grimes 
45226f9a767SRodney W. Grimes 	/* Allocate a new stack */
4531616db3cSJohn Dyson 	error = vm_map_insert(&vmspace->vm_map, NULL, 0,
4541616db3cSJohn Dyson 		(vm_offset_t) stack_addr, (vm_offset_t) USRSTACK,
4551616db3cSJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
45626f9a767SRodney W. Grimes 	if (error)
45726f9a767SRodney W. Grimes 		return (error);
45826f9a767SRodney W. Grimes 
45926f9a767SRodney W. Grimes 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
46026f9a767SRodney W. Grimes 
46126f9a767SRodney W. Grimes 	/* Initialize maximum stack address */
46226f9a767SRodney W. Grimes 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
46326f9a767SRodney W. Grimes 
46426f9a767SRodney W. Grimes 	return(0);
46526f9a767SRodney W. Grimes }
46626f9a767SRodney W. Grimes 
46726f9a767SRodney W. Grimes /*
46826f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
46926f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
47026f9a767SRodney W. Grimes  */
47126f9a767SRodney W. Grimes int
472c52007c2SDavid Greenman exec_extract_strings(imgp)
473c52007c2SDavid Greenman 	struct image_params *imgp;
47426f9a767SRodney W. Grimes {
47526f9a767SRodney W. Grimes 	char	**argv, **envv;
47626f9a767SRodney W. Grimes 	char	*argp, *envp;
4770fd1a014SDavid Greenman 	int	error, length;
47826f9a767SRodney W. Grimes 
47926f9a767SRodney W. Grimes 	/*
48026f9a767SRodney W. Grimes 	 * extract arguments first
48126f9a767SRodney W. Grimes 	 */
48226f9a767SRodney W. Grimes 
483c52007c2SDavid Greenman 	argv = imgp->uap->argv;
48426f9a767SRodney W. Grimes 
4850fd1a014SDavid Greenman 	if (argv) {
4865cf3d12cSAndrey A. Chernov 		argp = (caddr_t) fuword(argv);
4875cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
4885cf3d12cSAndrey A. Chernov 			return (EFAULT);
4895cf3d12cSAndrey A. Chernov 		if (argp)
4905cf3d12cSAndrey A. Chernov 			argv++;
4915cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
4925cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
4935cf3d12cSAndrey A. Chernov 		if (argp) {
4945cf3d12cSAndrey A. Chernov 			do {
49526f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
49626f9a767SRodney W. Grimes 					return (EFAULT);
497c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
498c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
4990fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
50026f9a767SRodney W. Grimes 						return(E2BIG);
5010fd1a014SDavid Greenman 					return (error);
5020fd1a014SDavid Greenman 				}
503c52007c2SDavid Greenman 				imgp->stringspace -= length;
504c52007c2SDavid Greenman 				imgp->stringp += length;
505c52007c2SDavid Greenman 				imgp->argc++;
5065cf3d12cSAndrey A. Chernov 			} while ((argp = (caddr_t) fuword(argv++)));
50726f9a767SRodney W. Grimes 		}
5080fd1a014SDavid Greenman 	}
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes 	/*
51126f9a767SRodney W. Grimes 	 * extract environment strings
51226f9a767SRodney W. Grimes 	 */
51326f9a767SRodney W. Grimes 
514c52007c2SDavid Greenman 	envv = imgp->uap->envv;
51526f9a767SRodney W. Grimes 
5160fd1a014SDavid Greenman 	if (envv) {
517bb56ec4aSPoul-Henning Kamp 		while ((envp = (caddr_t) fuword(envv++))) {
51826f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
51926f9a767SRodney W. Grimes 				return (EFAULT);
520c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
521c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5220fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
52326f9a767SRodney W. Grimes 					return(E2BIG);
5240fd1a014SDavid Greenman 				return (error);
5250fd1a014SDavid Greenman 			}
526c52007c2SDavid Greenman 			imgp->stringspace -= length;
527c52007c2SDavid Greenman 			imgp->stringp += length;
528c52007c2SDavid Greenman 			imgp->envc++;
52926f9a767SRodney W. Grimes 		}
5300fd1a014SDavid Greenman 	}
53126f9a767SRodney W. Grimes 
53226f9a767SRodney W. Grimes 	return (0);
53326f9a767SRodney W. Grimes }
53426f9a767SRodney W. Grimes 
53526f9a767SRodney W. Grimes /*
53626f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
53726f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
53826f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
53926f9a767SRodney W. Grimes  */
54026f9a767SRodney W. Grimes int *
541c52007c2SDavid Greenman exec_copyout_strings(imgp)
542c52007c2SDavid Greenman 	struct image_params *imgp;
54326f9a767SRodney W. Grimes {
54426f9a767SRodney W. Grimes 	int argc, envc;
54526f9a767SRodney W. Grimes 	char **vectp;
54626f9a767SRodney W. Grimes 	char *stringp, *destp;
54726f9a767SRodney W. Grimes 	int *stack_base;
54893f6448cSDavid Greenman 	struct ps_strings *arginfo;
549d66a5066SPeter Wemm 	int szsigcode;
55026f9a767SRodney W. Grimes 
55126f9a767SRodney W. Grimes 	/*
55226f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
553d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
55426f9a767SRodney W. Grimes 	 */
55593f6448cSDavid Greenman 	arginfo = PS_STRINGS;
556d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
557d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
5581ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
5591ed012f9SPeter Wemm 
56026f9a767SRodney W. Grimes 	/*
561d66a5066SPeter Wemm 	 * install sigcode
562d66a5066SPeter Wemm 	 */
563d66a5066SPeter Wemm 	if (szsigcode)
564d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
565d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
566d66a5066SPeter Wemm 
567d66a5066SPeter Wemm 	/*
568e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
569e1743d02SSøren Schmidt 	 * on the stack.
570e1743d02SSøren Schmidt 	 */
571e1743d02SSøren Schmidt 	if (imgp->auxargs)
572e1743d02SSøren Schmidt 	/*
573e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
574e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
575e1743d02SSøren Schmidt 	 * ELF Auxargs data.
576e1743d02SSøren Schmidt 	 */
577e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
578e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
579e1743d02SSøren Schmidt 	else
580e1743d02SSøren Schmidt 	/*
58126f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
58226f9a767SRodney W. Grimes 	 * arg and env vector sets
58326f9a767SRodney W. Grimes 	 */
584e1743d02SSøren Schmidt 		vectp = (char **)
585e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
58626f9a767SRodney W. Grimes 
58726f9a767SRodney W. Grimes 	/*
58826f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
58926f9a767SRodney W. Grimes 	 */
59026f9a767SRodney W. Grimes 	stack_base = (int *)vectp;
59126f9a767SRodney W. Grimes 
592c52007c2SDavid Greenman 	stringp = imgp->stringbase;
593c52007c2SDavid Greenman 	argc = imgp->argc;
594c52007c2SDavid Greenman 	envc = imgp->envc;
59526f9a767SRodney W. Grimes 
59693f6448cSDavid Greenman 	/*
5973aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
59893f6448cSDavid Greenman 	 */
599c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
60093f6448cSDavid Greenman 
60193f6448cSDavid Greenman 	/*
6023aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6033aed948bSDavid Greenman 	 */
6041ed012f9SPeter Wemm 	suword(&arginfo->ps_argvstr, (int)vectp);
6053aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6063aed948bSDavid Greenman 
6073aed948bSDavid Greenman 	/*
6083aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
60993f6448cSDavid Greenman 	 */
61026f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
6113aed948bSDavid Greenman 		suword(vectp++, (int)destp);
6123aed948bSDavid Greenman 		while (*stringp++ != 0)
6133aed948bSDavid Greenman 			destp++;
6143aed948bSDavid Greenman 		destp++;
61526f9a767SRodney W. Grimes 	}
61626f9a767SRodney W. Grimes 
61726f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6186ab46d52SBruce Evans 	suword(vectp++, 0);
61926f9a767SRodney W. Grimes 
6201ed012f9SPeter Wemm 	suword(&arginfo->ps_envstr, (int)vectp);
6213aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
62293f6448cSDavid Greenman 
62393f6448cSDavid Greenman 	/*
6243aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
62593f6448cSDavid Greenman 	 */
62626f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
6273aed948bSDavid Greenman 		suword(vectp++, (int)destp);
6283aed948bSDavid Greenman 		while (*stringp++ != 0)
6293aed948bSDavid Greenman 			destp++;
6303aed948bSDavid Greenman 		destp++;
63126f9a767SRodney W. Grimes 	}
63226f9a767SRodney W. Grimes 
63326f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6346ab46d52SBruce Evans 	suword(vectp, 0);
63526f9a767SRodney W. Grimes 
63626f9a767SRodney W. Grimes 	return (stack_base);
63726f9a767SRodney W. Grimes }
63826f9a767SRodney W. Grimes 
63926f9a767SRodney W. Grimes /*
64026f9a767SRodney W. Grimes  * Check permissions of file to execute.
64126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
64226f9a767SRodney W. Grimes  */
643c8a79999SPeter Wemm int
644c52007c2SDavid Greenman exec_check_permissions(imgp)
645c52007c2SDavid Greenman 	struct image_params *imgp;
64626f9a767SRodney W. Grimes {
647c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
648c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
649c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
65026f9a767SRodney W. Grimes 	int error;
65126f9a767SRodney W. Grimes 
65226f9a767SRodney W. Grimes 	/* Get file attributes */
653c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
65426f9a767SRodney W. Grimes 	if (error)
65526f9a767SRodney W. Grimes 		return (error);
65626f9a767SRodney W. Grimes 
65726f9a767SRodney W. Grimes 	/*
65826f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
65926f9a767SRodney W. Grimes 	 *	file resides on.
66026f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
66126f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
66226f9a767SRodney W. Grimes 	 *	file really is executable.
66326f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
66426f9a767SRodney W. Grimes 	 */
665c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
66626f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
66726f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
66826f9a767SRodney W. Grimes 		return (EACCES);
66926f9a767SRodney W. Grimes 	}
67026f9a767SRodney W. Grimes 
67126f9a767SRodney W. Grimes 	/*
67226f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
67326f9a767SRodney W. Grimes 	 */
67426f9a767SRodney W. Grimes 	if (attr->va_size == 0)
67526f9a767SRodney W. Grimes 		return (ENOEXEC);
67626f9a767SRodney W. Grimes 
67726f9a767SRodney W. Grimes 	/*
67826f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
67926f9a767SRodney W. Grimes 	 */
680c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
68126f9a767SRodney W. Grimes 	if (error)
68226f9a767SRodney W. Grimes 		return (error);
68326f9a767SRodney W. Grimes 
6846d5a0a8cSDavid Greenman 	/*
6856d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
6866d5a0a8cSDavid Greenman 	 * if there are any.
6876d5a0a8cSDavid Greenman 	 */
6886d5a0a8cSDavid Greenman 	if (vp->v_writecount)
6896d5a0a8cSDavid Greenman 		return (ETXTBSY);
6906d5a0a8cSDavid Greenman 
6916d5a0a8cSDavid Greenman 	/*
6926d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
6936d5a0a8cSDavid Greenman 	 * general case).
6946d5a0a8cSDavid Greenman 	 */
695c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
69626f9a767SRodney W. Grimes 	if (error)
69726f9a767SRodney W. Grimes 		return (error);
69826f9a767SRodney W. Grimes 
69926f9a767SRodney W. Grimes 	return (0);
700df8bae1dSRodney W. Grimes }
701