xref: /freebsd/sys/kern/kern_exec.c (revision d323ddf3171ff6652c94cb9f79105f48f5ea8819)
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  *
26c3aac50fSPeter Wemm  * $FreeBSD$
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>
44aa855a59SPeter Wemm #include <sys/malloc.h>
45a794e791SBruce Evans #include <sys/namei.h>
461e1e0b44SSøren Schmidt #include <sys/sysent.h>
47797f2d22SPoul-Henning Kamp #include <sys/shm.h>
4899ac3bc8SPeter Wemm #include <sys/sysctl.h>
49a794e791SBruce Evans #include <sys/vnode.h>
5026f9a767SRodney W. Grimes 
5126f9a767SRodney W. Grimes #include <vm/vm.h>
52efeaf95aSDavid Greenman #include <vm/vm_param.h>
53996c772fSJohn Dyson #include <sys/lock.h>
54efeaf95aSDavid Greenman #include <vm/pmap.h>
551616db3cSJohn Dyson #include <vm/vm_page.h>
56efeaf95aSDavid Greenman #include <vm/vm_map.h>
5726f9a767SRodney W. Grimes #include <vm/vm_kern.h>
58efeaf95aSDavid Greenman #include <vm/vm_extern.h>
591ebd0c59SDavid Greenman #include <vm/vm_object.h>
60675ea6f0SBruce Evans #include <vm/vm_zone.h>
611616db3cSJohn Dyson #include <vm/vm_pager.h>
6226f9a767SRodney W. Grimes 
6326f9a767SRodney W. Grimes #include <machine/reg.h>
6426f9a767SRodney W. Grimes 
65b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
66b9df5231SPoul-Henning Kamp 
67654f6be1SBruce Evans static register_t *exec_copyout_strings __P((struct image_params *));
68df8bae1dSRodney W. Grimes 
69486bddb0SDoug Rabson static long ps_strings = PS_STRINGS;
70486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, "");
71486bddb0SDoug Rabson 
72486bddb0SDoug Rabson static long usrstack = USRSTACK;
73486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, "");
7499ac3bc8SPeter Wemm 
75b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
76b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
77b9df5231SPoul-Henning Kamp     &ps_arg_cache_limit, "");
78b9df5231SPoul-Henning Kamp 
79a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
80a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
81a8704f89SPoul-Henning Kamp 
8299ac3bc8SPeter Wemm /*
83aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
84aa855a59SPeter Wemm  * double pointer here.
85df8bae1dSRodney W. Grimes  */
86aa855a59SPeter Wemm static const struct execsw **execsw;
8726f9a767SRodney W. Grimes 
88d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
89ad7507e2SSteven Wallace struct execve_args {
90ad7507e2SSteven Wallace         char    *fname;
91ad7507e2SSteven Wallace         char    **argv;
92ad7507e2SSteven Wallace         char    **envv;
93ad7507e2SSteven Wallace };
94d2d3e875SBruce Evans #endif
95ad7507e2SSteven Wallace 
9626f9a767SRodney W. Grimes /*
9726f9a767SRodney W. Grimes  * execve() system call.
9826f9a767SRodney W. Grimes  */
9926f9a767SRodney W. Grimes int
100cb226aaaSPoul-Henning Kamp execve(p, uap)
10126f9a767SRodney W. Grimes 	struct proc *p;
10226f9a767SRodney W. Grimes 	register struct execve_args *uap;
103df8bae1dSRodney W. Grimes {
10426f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
105654f6be1SBruce Evans 	register_t *stack_base;
106bb56ec4aSPoul-Henning Kamp 	int error, len, i;
107c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10826f9a767SRodney W. Grimes 	struct vattr attr;
109d323ddf3SMatthew Dillon 	int (*img_first) __P((struct image_params *));
11026f9a767SRodney W. Grimes 
111c52007c2SDavid Greenman 	imgp = &image_params;
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	/*
114c52007c2SDavid Greenman 	 * Initialize part of the common data
115df8bae1dSRodney W. Grimes 	 */
116c52007c2SDavid Greenman 	imgp->proc = p;
117c52007c2SDavid Greenman 	imgp->uap = uap;
118c52007c2SDavid Greenman 	imgp->attr = &attr;
119c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1205cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
121c52007c2SDavid Greenman 	imgp->entry_addr = 0;
122c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
123c52007c2SDavid Greenman 	imgp->interpreted = 0;
124c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
125e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1261616db3cSJohn Dyson 	imgp->vp = NULL;
1271616db3cSJohn Dyson 	imgp->firstpage = NULL;
1284fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
12926f9a767SRodney W. Grimes 
13026f9a767SRodney W. Grimes 	/*
13126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
13226f9a767SRodney W. Grimes 	 *	environment strings
13326f9a767SRodney W. Grimes 	 */
1341616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
135c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
13626f9a767SRodney W. Grimes 		error = ENOMEM;
13726f9a767SRodney W. Grimes 		goto exec_fail;
13826f9a767SRodney W. Grimes 	}
139c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
140c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1411616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
14226f9a767SRodney W. Grimes 
14326f9a767SRodney W. Grimes 	/*
14426f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
14526f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
14626f9a767SRodney W. Grimes 	 */
14726f9a767SRodney W. Grimes 	ndp = &nd;
148b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1493ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
15026f9a767SRodney W. Grimes 
15126f9a767SRodney W. Grimes interpret:
15226f9a767SRodney W. Grimes 
15326f9a767SRodney W. Grimes 	error = namei(ndp);
15426f9a767SRodney W. Grimes 	if (error) {
1551616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1561616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
15726f9a767SRodney W. Grimes 		goto exec_fail;
15826f9a767SRodney W. Grimes 	}
15926f9a767SRodney W. Grimes 
160c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1619c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
16226f9a767SRodney W. Grimes 
16326f9a767SRodney W. Grimes 	/*
1649022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1659022e4adSDavid Greenman 	 */
166c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1678677f509SDavid Greenman 	if (error) {
1688677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
16926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1708677f509SDavid Greenman 	}
17126f9a767SRodney W. Grimes 
1721616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1739caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1746d5a0a8cSDavid Greenman 	if (error)
17526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
17626f9a767SRodney W. Grimes 
17726f9a767SRodney W. Grimes 	/*
178d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
179d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
180d323ddf3SMatthew Dillon 	 *	scripts differently.
18126f9a767SRodney W. Grimes 	 */
182d323ddf3SMatthew Dillon 	error = -1;
183d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
184d323ddf3SMatthew Dillon 		error = img_first(imgp);
185d323ddf3SMatthew Dillon 
186d323ddf3SMatthew Dillon 	/*
187d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
188d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
189d323ddf3SMatthew Dillon 	 *	and an error otherwise.
190d323ddf3SMatthew Dillon 	 */
191d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
192d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
193d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
194d323ddf3SMatthew Dillon 			continue;
195d323ddf3SMatthew Dillon 		}
196c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
197d323ddf3SMatthew Dillon 	}
198d323ddf3SMatthew Dillon 
199d323ddf3SMatthew Dillon 	if (error) {
20026f9a767SRodney W. Grimes 		if (error == -1)
201d323ddf3SMatthew Dillon 			error = ENOEXEC;
20226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
203d323ddf3SMatthew Dillon 	}
204d323ddf3SMatthew Dillon 
205d323ddf3SMatthew Dillon 	/*
206d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
207d323ddf3SMatthew Dillon 	 * activate the interpreter.
208d323ddf3SMatthew Dillon 	 */
209c52007c2SDavid Greenman 	if (imgp->interpreted) {
2101616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
211762e6b85SEivind Eklund 		/* free name buffer and old vnode */
212762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
213f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
21426f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
215b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
216c52007c2SDavid Greenman 		    UIO_SYSSPACE, imgp->interpreter_name, p);
21726f9a767SRodney W. Grimes 		goto interpret;
21826f9a767SRodney W. Grimes 	}
21926f9a767SRodney W. Grimes 
22026f9a767SRodney W. Grimes 	/*
22126f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
22226f9a767SRodney W. Grimes 	 */
223c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
22426f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
22526f9a767SRodney W. Grimes 
22626f9a767SRodney W. Grimes 	/*
2271e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2281e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2291e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
23026f9a767SRodney W. Grimes 	 */
2311e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
232c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2331e1e0b44SSøren Schmidt 	else
234c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
23526f9a767SRodney W. Grimes 
236a78e8d2aSDavid Greenman 	/*
237a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
238a78e8d2aSDavid Greenman 	 * be shared after an exec.
239a78e8d2aSDavid Greenman 	 */
240a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
241a78e8d2aSDavid Greenman 		struct filedesc *tmp;
242a78e8d2aSDavid Greenman 
243a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
244a78e8d2aSDavid Greenman 		fdfree(p);
245a78e8d2aSDavid Greenman 		p->p_fd = tmp;
246a78e8d2aSDavid Greenman 	}
247a78e8d2aSDavid Greenman 
248fdf4e8b3SWarner Losh 	/* Stop profiling */
249fdf4e8b3SWarner Losh 	stopprofclock(p);
250fdf4e8b3SWarner Losh 
25126f9a767SRodney W. Grimes 	/* close files on exec */
25226f9a767SRodney W. Grimes 	fdcloseexec(p);
25326f9a767SRodney W. Grimes 
25426f9a767SRodney W. Grimes 	/* reset caught signals */
25526f9a767SRodney W. Grimes 	execsigs(p);
25626f9a767SRodney W. Grimes 
25726f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
25826f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
25926f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
26026f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
26126f9a767SRodney W. Grimes 
26226f9a767SRodney W. Grimes 	/*
26324b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
264dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
26526f9a767SRodney W. Grimes 	 */
26626f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
26726f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
26826f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
26926f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
27026f9a767SRodney W. Grimes 	}
27126f9a767SRodney W. Grimes 
27226f9a767SRodney W. Grimes 	/*
273a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
274a78e8d2aSDavid Greenman 	 *
275a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
276a78e8d2aSDavid Greenman 	 * the process is being traced.
277c52007c2SDavid Greenman 	 */
2788aef1712SMatthew Dillon 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
2798aef1712SMatthew Dillon 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
280a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
281c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
282c52007c2SDavid Greenman 		/*
283c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
28426f9a767SRodney W. Grimes 		 * root.
28526f9a767SRodney W. Grimes 		 */
286f711d546SPoul-Henning Kamp 		if (p->p_tracep && suser(p)) {
28726f9a767SRodney W. Grimes 			p->p_traceflag = 0;
28826f9a767SRodney W. Grimes 			vrele(p->p_tracep);
289c52007c2SDavid Greenman 			p->p_tracep = NULL;
29026f9a767SRodney W. Grimes 		}
291c52007c2SDavid Greenman 		/*
292c52007c2SDavid Greenman 		 * Set the new credentials.
293c52007c2SDavid Greenman 		 */
29426f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
295c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
29626f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
297c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
298d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
299d5f81602SSean Eric Fagan 		setsugid(p);
3005e266442SWarner Losh 		setugidsafety(p);
301c52007c2SDavid Greenman 	} else {
302e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
303e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
304c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
30526f9a767SRodney W. Grimes 	}
30626f9a767SRodney W. Grimes 
30726f9a767SRodney W. Grimes 	/*
308c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
30926f9a767SRodney W. Grimes 	 */
31026f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
31126f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
31226f9a767SRodney W. Grimes 
31326f9a767SRodney W. Grimes 	/*
3142a531c80SDavid Greenman 	 * Store the vp for use in procfs
3152a531c80SDavid Greenman 	 */
3162a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
3172a531c80SDavid Greenman 		vrele(p->p_textvp);
3182a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3192a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3202a531c80SDavid Greenman 
3212a531c80SDavid Greenman 	/*
322cb679c38SJonathan Lemon 	 * notify others that we exec'd
323cb679c38SJonathan Lemon 	 */
324cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
325cb679c38SJonathan Lemon 
326cb679c38SJonathan Lemon 	/*
32726f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
32826f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
32926f9a767SRodney W. Grimes 	 */
3302a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3312a024a2bSSean Eric Fagan 
33226f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
33326f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
33426f9a767SRodney W. Grimes 
33526f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
33626f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
33726f9a767SRodney W. Grimes 
3384fe88fe6SJohn Polstra 	/* Set values passed into the program in registers. */
3394fe88fe6SJohn Polstra 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
3404fe88fe6SJohn Polstra 	    imgp->ps_strings);
34126f9a767SRodney W. Grimes 
342b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
343b9df5231SPoul-Henning Kamp 	if (p->p_args && --p->p_args->ar_ref == 0)
344b9df5231SPoul-Henning Kamp 		FREE(p->p_args, M_PARGS);
345b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
346b9df5231SPoul-Henning Kamp 
347b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
348b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
349b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
350b9df5231SPoul-Henning Kamp 		MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i,
351b9df5231SPoul-Henning Kamp 		    M_PARGS, M_WAITOK);
352b9df5231SPoul-Henning Kamp 		p->p_args->ar_ref = 1;
353b9df5231SPoul-Henning Kamp 		p->p_args->ar_length = i;
354b9df5231SPoul-Henning Kamp 		bcopy(imgp->stringbase, p->p_args->ar_args, i);
355b9df5231SPoul-Henning Kamp 	}
356b9df5231SPoul-Henning Kamp 
3571616db3cSJohn Dyson exec_fail_dealloc:
3581616db3cSJohn Dyson 
35926f9a767SRodney W. Grimes 	/*
36026f9a767SRodney W. Grimes 	 * free various allocated resources
36126f9a767SRodney W. Grimes 	 */
3621616db3cSJohn Dyson 	if (imgp->firstpage)
3631616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
36426f9a767SRodney W. Grimes 
365c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3661616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3671616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3681616db3cSJohn Dyson 
3699c0fed3dSDoug Rabson 	if (imgp->vp) {
370762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
3719c0fed3dSDoug Rabson 		vrele(imgp->vp);
372a3cf6ebaSDavid Greenman 	}
37326f9a767SRodney W. Grimes 
3741616db3cSJohn Dyson 	if (error == 0)
3751616db3cSJohn Dyson 		return (0);
3761616db3cSJohn Dyson 
37726f9a767SRodney W. Grimes exec_fail:
378c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
37926f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
38026f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
38126f9a767SRodney W. Grimes 		/* NOT REACHED */
38226f9a767SRodney W. Grimes 		return(0);
38326f9a767SRodney W. Grimes 	} else {
38426f9a767SRodney W. Grimes 		return(error);
38526f9a767SRodney W. Grimes 	}
38626f9a767SRodney W. Grimes }
38726f9a767SRodney W. Grimes 
3881616db3cSJohn Dyson int
3891616db3cSJohn Dyson exec_map_first_page(imgp)
3901616db3cSJohn Dyson 	struct image_params *imgp;
3911616db3cSJohn Dyson {
39295461b45SJohn Dyson 	int s, rv, i;
39395461b45SJohn Dyson 	int initial_pagein;
39495461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3951616db3cSJohn Dyson 	vm_object_t object;
3961616db3cSJohn Dyson 
3971616db3cSJohn Dyson 
3981616db3cSJohn Dyson 	if (imgp->firstpage) {
3991616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
4001616db3cSJohn Dyson 	}
4011616db3cSJohn Dyson 
4021616db3cSJohn Dyson 	object = imgp->vp->v_object;
4031616db3cSJohn Dyson 	s = splvm();
4041616db3cSJohn Dyson 
40595461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
4061616db3cSJohn Dyson 
40795461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
40895461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
40995461b45SJohn Dyson 		if (initial_pagein > object->size)
41095461b45SJohn Dyson 			initial_pagein = object->size;
41195461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
412d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
41395461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
41495461b45SJohn Dyson 					break;
41595461b45SJohn Dyson 				if (ma[i]->valid)
41695461b45SJohn Dyson 					break;
417e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
41895461b45SJohn Dyson 			} else {
41995461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
42095461b45SJohn Dyson 				if (ma[i] == NULL)
42195461b45SJohn Dyson 					break;
42295461b45SJohn Dyson 			}
42395461b45SJohn Dyson 		}
42495461b45SJohn Dyson 		initial_pagein = i;
4251616db3cSJohn Dyson 
42695461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
42795461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
42895461b45SJohn Dyson 
429eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
430ecbb00a2SDoug Rabson 			if (ma[0]) {
43195461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
4328f9110f6SJohn Dyson 				vm_page_free(ma[0]);
433ecbb00a2SDoug Rabson 			}
4341616db3cSJohn Dyson 			splx(s);
4351616db3cSJohn Dyson 			return EIO;
4361616db3cSJohn Dyson 		}
4371616db3cSJohn Dyson 	}
4381616db3cSJohn Dyson 
43995461b45SJohn Dyson 	vm_page_wire(ma[0]);
440e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
4411616db3cSJohn Dyson 	splx(s);
4421616db3cSJohn Dyson 
44395461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
44495461b45SJohn Dyson 	imgp->firstpage = ma[0];
4451616db3cSJohn Dyson 
4461616db3cSJohn Dyson 	return 0;
4471616db3cSJohn Dyson }
4481616db3cSJohn Dyson 
4491616db3cSJohn Dyson void
4501616db3cSJohn Dyson exec_unmap_first_page(imgp)
4511616db3cSJohn Dyson 	struct image_params *imgp;
4521616db3cSJohn Dyson {
4531616db3cSJohn Dyson 	if (imgp->firstpage) {
4541616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
45573007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
4561616db3cSJohn Dyson 		imgp->firstpage = NULL;
4571616db3cSJohn Dyson 	}
4581616db3cSJohn Dyson }
4591616db3cSJohn Dyson 
46026f9a767SRodney W. Grimes /*
46126f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
46226f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
46326f9a767SRodney W. Grimes  *	automatically in trap.c.
46426f9a767SRodney W. Grimes  */
46526f9a767SRodney W. Grimes int
466c52007c2SDavid Greenman exec_new_vmspace(imgp)
467c52007c2SDavid Greenman 	struct image_params *imgp;
46826f9a767SRodney W. Grimes {
46926f9a767SRodney W. Grimes 	int error;
470c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
4712267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
472af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
47326f9a767SRodney W. Grimes 
474c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
47526f9a767SRodney W. Grimes 
476af9ec885SJohn Dyson 	/*
477af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
478af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
479af9ec885SJohn Dyson 	 * not disrupted
480af9ec885SJohn Dyson 	 */
481af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4823d903220SDoug Rabson 		if (vmspace->vm_shm)
483c52007c2SDavid Greenman 			shmexit(imgp->proc);
484b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
4859c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
486af9ec885SJohn Dyson 	} else {
487492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
488492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
489af9ec885SJohn Dyson 		map = &vmspace->vm_map;
490af9ec885SJohn Dyson 	}
49126f9a767SRodney W. Grimes 
49226f9a767SRodney W. Grimes 	/* Allocate a new stack */
4932267af78SJulian Elischer 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
4942267af78SJulian Elischer 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
4952267af78SJulian Elischer 	if (error)
4962267af78SJulian Elischer 		return (error);
4972267af78SJulian Elischer 
4982267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
4992267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
5002267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
5012267af78SJulian Elischer 	 */
5022267af78SJulian Elischer 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
5032267af78SJulian Elischer 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
50426f9a767SRodney W. Grimes 
50526f9a767SRodney W. Grimes 	return(0);
50626f9a767SRodney W. Grimes }
50726f9a767SRodney W. Grimes 
50826f9a767SRodney W. Grimes /*
50926f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
51026f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
51126f9a767SRodney W. Grimes  */
51226f9a767SRodney W. Grimes int
513c52007c2SDavid Greenman exec_extract_strings(imgp)
514c52007c2SDavid Greenman 	struct image_params *imgp;
51526f9a767SRodney W. Grimes {
51626f9a767SRodney W. Grimes 	char	**argv, **envv;
51726f9a767SRodney W. Grimes 	char	*argp, *envp;
518ecbb00a2SDoug Rabson 	int	error;
519ecbb00a2SDoug Rabson 	size_t	length;
52026f9a767SRodney W. Grimes 
52126f9a767SRodney W. Grimes 	/*
52226f9a767SRodney W. Grimes 	 * extract arguments first
52326f9a767SRodney W. Grimes 	 */
52426f9a767SRodney W. Grimes 
525c52007c2SDavid Greenman 	argv = imgp->uap->argv;
52626f9a767SRodney W. Grimes 
5270fd1a014SDavid Greenman 	if (argv) {
528aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
5295cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
5305cf3d12cSAndrey A. Chernov 			return (EFAULT);
5315cf3d12cSAndrey A. Chernov 		if (argp)
5325cf3d12cSAndrey A. Chernov 			argv++;
5335cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
5345cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
5355cf3d12cSAndrey A. Chernov 		if (argp) {
5365cf3d12cSAndrey A. Chernov 			do {
53726f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
53826f9a767SRodney W. Grimes 					return (EFAULT);
539c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
540c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
5410fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
54226f9a767SRodney W. Grimes 						return(E2BIG);
5430fd1a014SDavid Greenman 					return (error);
5440fd1a014SDavid Greenman 				}
545c52007c2SDavid Greenman 				imgp->stringspace -= length;
546c52007c2SDavid Greenman 				imgp->stringp += length;
547c52007c2SDavid Greenman 				imgp->argc++;
548aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
54926f9a767SRodney W. Grimes 		}
5500fd1a014SDavid Greenman 	}
55126f9a767SRodney W. Grimes 
552b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
553b9df5231SPoul-Henning Kamp 
55426f9a767SRodney W. Grimes 	/*
55526f9a767SRodney W. Grimes 	 * extract environment strings
55626f9a767SRodney W. Grimes 	 */
55726f9a767SRodney W. Grimes 
558c52007c2SDavid Greenman 	envv = imgp->uap->envv;
55926f9a767SRodney W. Grimes 
5600fd1a014SDavid Greenman 	if (envv) {
561aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
56226f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
56326f9a767SRodney W. Grimes 				return (EFAULT);
564c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
565c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5660fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
56726f9a767SRodney W. Grimes 					return(E2BIG);
5680fd1a014SDavid Greenman 				return (error);
5690fd1a014SDavid Greenman 			}
570c52007c2SDavid Greenman 			imgp->stringspace -= length;
571c52007c2SDavid Greenman 			imgp->stringp += length;
572c52007c2SDavid Greenman 			imgp->envc++;
57326f9a767SRodney W. Grimes 		}
5740fd1a014SDavid Greenman 	}
57526f9a767SRodney W. Grimes 
57626f9a767SRodney W. Grimes 	return (0);
57726f9a767SRodney W. Grimes }
57826f9a767SRodney W. Grimes 
57926f9a767SRodney W. Grimes /*
58026f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
58126f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
58226f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
58326f9a767SRodney W. Grimes  */
584654f6be1SBruce Evans register_t *
585c52007c2SDavid Greenman exec_copyout_strings(imgp)
586c52007c2SDavid Greenman 	struct image_params *imgp;
58726f9a767SRodney W. Grimes {
58826f9a767SRodney W. Grimes 	int argc, envc;
58926f9a767SRodney W. Grimes 	char **vectp;
59026f9a767SRodney W. Grimes 	char *stringp, *destp;
591654f6be1SBruce Evans 	register_t *stack_base;
59293f6448cSDavid Greenman 	struct ps_strings *arginfo;
593d66a5066SPeter Wemm 	int szsigcode;
59426f9a767SRodney W. Grimes 
59526f9a767SRodney W. Grimes 	/*
59626f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
597d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
59826f9a767SRodney W. Grimes 	 */
5994c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
600d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
601d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
6021ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
6031ed012f9SPeter Wemm 
60426f9a767SRodney W. Grimes 	/*
605d66a5066SPeter Wemm 	 * install sigcode
606d66a5066SPeter Wemm 	 */
607d66a5066SPeter Wemm 	if (szsigcode)
608d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
609d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
610d66a5066SPeter Wemm 
611d66a5066SPeter Wemm 	/*
612e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
613e1743d02SSøren Schmidt 	 * on the stack.
614e1743d02SSøren Schmidt 	 */
615e1743d02SSøren Schmidt 	if (imgp->auxargs)
616e1743d02SSøren Schmidt 	/*
617e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
618e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
619e1743d02SSøren Schmidt 	 * ELF Auxargs data.
620e1743d02SSøren Schmidt 	 */
621e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
622e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
623e1743d02SSøren Schmidt 	else
624e1743d02SSøren Schmidt 	/*
62526f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
62626f9a767SRodney W. Grimes 	 * arg and env vector sets
62726f9a767SRodney W. Grimes 	 */
628e1743d02SSøren Schmidt 		vectp = (char **)
629e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
63026f9a767SRodney W. Grimes 
63126f9a767SRodney W. Grimes 	/*
63226f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
63326f9a767SRodney W. Grimes 	 */
634654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
63526f9a767SRodney W. Grimes 
636c52007c2SDavid Greenman 	stringp = imgp->stringbase;
637c52007c2SDavid Greenman 	argc = imgp->argc;
638c52007c2SDavid Greenman 	envc = imgp->envc;
63926f9a767SRodney W. Grimes 
64093f6448cSDavid Greenman 	/*
6413aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
64293f6448cSDavid Greenman 	 */
643c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
64493f6448cSDavid Greenman 
64593f6448cSDavid Greenman 	/*
6463aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6473aed948bSDavid Greenman 	 */
648aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6493aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6503aed948bSDavid Greenman 
6513aed948bSDavid Greenman 	/*
6523aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
65393f6448cSDavid Greenman 	 */
65426f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
655aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6563aed948bSDavid Greenman 		while (*stringp++ != 0)
6573aed948bSDavid Greenman 			destp++;
6583aed948bSDavid Greenman 		destp++;
65926f9a767SRodney W. Grimes 	}
66026f9a767SRodney W. Grimes 
66126f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6626ab46d52SBruce Evans 	suword(vectp++, 0);
66326f9a767SRodney W. Grimes 
664aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
6653aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
66693f6448cSDavid Greenman 
66793f6448cSDavid Greenman 	/*
6683aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
66993f6448cSDavid Greenman 	 */
67026f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
671aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6723aed948bSDavid Greenman 		while (*stringp++ != 0)
6733aed948bSDavid Greenman 			destp++;
6743aed948bSDavid Greenman 		destp++;
67526f9a767SRodney W. Grimes 	}
67626f9a767SRodney W. Grimes 
67726f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6786ab46d52SBruce Evans 	suword(vectp, 0);
67926f9a767SRodney W. Grimes 
68026f9a767SRodney W. Grimes 	return (stack_base);
68126f9a767SRodney W. Grimes }
68226f9a767SRodney W. Grimes 
68326f9a767SRodney W. Grimes /*
68426f9a767SRodney W. Grimes  * Check permissions of file to execute.
68526f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
68626f9a767SRodney W. Grimes  */
687c8a79999SPeter Wemm int
688c52007c2SDavid Greenman exec_check_permissions(imgp)
689c52007c2SDavid Greenman 	struct image_params *imgp;
69026f9a767SRodney W. Grimes {
691c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
692c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
693c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
69426f9a767SRodney W. Grimes 	int error;
69526f9a767SRodney W. Grimes 
69626f9a767SRodney W. Grimes 	/* Get file attributes */
697c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
69826f9a767SRodney W. Grimes 	if (error)
69926f9a767SRodney W. Grimes 		return (error);
70026f9a767SRodney W. Grimes 
70126f9a767SRodney W. Grimes 	/*
70226f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
70326f9a767SRodney W. Grimes 	 *	file resides on.
70426f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
70526f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
70626f9a767SRodney W. Grimes 	 *	file really is executable.
70726f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
70826f9a767SRodney W. Grimes 	 */
709c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
71026f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
71126f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
71226f9a767SRodney W. Grimes 		return (EACCES);
71326f9a767SRodney W. Grimes 	}
71426f9a767SRodney W. Grimes 
71526f9a767SRodney W. Grimes 	/*
71626f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
71726f9a767SRodney W. Grimes 	 */
71826f9a767SRodney W. Grimes 	if (attr->va_size == 0)
71926f9a767SRodney W. Grimes 		return (ENOEXEC);
72026f9a767SRodney W. Grimes 
72126f9a767SRodney W. Grimes 	/*
72226f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
72326f9a767SRodney W. Grimes 	 */
724c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
72526f9a767SRodney W. Grimes 	if (error)
72626f9a767SRodney W. Grimes 		return (error);
72726f9a767SRodney W. Grimes 
7286d5a0a8cSDavid Greenman 	/*
7296d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
7306d5a0a8cSDavid Greenman 	 * if there are any.
7316d5a0a8cSDavid Greenman 	 */
7326d5a0a8cSDavid Greenman 	if (vp->v_writecount)
7336d5a0a8cSDavid Greenman 		return (ETXTBSY);
7346d5a0a8cSDavid Greenman 
7356d5a0a8cSDavid Greenman 	/*
7366d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
7376d5a0a8cSDavid Greenman 	 * general case).
7386d5a0a8cSDavid Greenman 	 */
739c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
74026f9a767SRodney W. Grimes 	if (error)
74126f9a767SRodney W. Grimes 		return (error);
74226f9a767SRodney W. Grimes 
74326f9a767SRodney W. Grimes 	return (0);
744df8bae1dSRodney W. Grimes }
745aa855a59SPeter Wemm 
746aa855a59SPeter Wemm /*
747aa855a59SPeter Wemm  * Exec handler registration
748aa855a59SPeter Wemm  */
749aa855a59SPeter Wemm int
750aa855a59SPeter Wemm exec_register(execsw_arg)
751aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
752aa855a59SPeter Wemm {
753aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
754aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
755aa855a59SPeter Wemm 
756aa855a59SPeter Wemm 	if (execsw)
757aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
758aa855a59SPeter Wemm 			count++;
759aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
760aa855a59SPeter Wemm 	if (newexecsw == NULL)
761aa855a59SPeter Wemm 		return ENOMEM;
762aa855a59SPeter Wemm 	xs = newexecsw;
763aa855a59SPeter Wemm 	if (execsw)
764aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
765aa855a59SPeter Wemm 			*xs++ = *es;
766aa855a59SPeter Wemm 	*xs++ = execsw_arg;
767aa855a59SPeter Wemm 	*xs = NULL;
768aa855a59SPeter Wemm 	if (execsw)
769aa855a59SPeter Wemm 		free(execsw, M_TEMP);
770aa855a59SPeter Wemm 	execsw = newexecsw;
771aa855a59SPeter Wemm 	return 0;
772aa855a59SPeter Wemm }
773aa855a59SPeter Wemm 
774aa855a59SPeter Wemm int
775aa855a59SPeter Wemm exec_unregister(execsw_arg)
776aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
777aa855a59SPeter Wemm {
778aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
779aa855a59SPeter Wemm 	int count = 1;
780aa855a59SPeter Wemm 
781aa855a59SPeter Wemm 	if (execsw == NULL)
782aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
783aa855a59SPeter Wemm 
784aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
785aa855a59SPeter Wemm 		if (*es == execsw_arg)
786aa855a59SPeter Wemm 			break;
787aa855a59SPeter Wemm 	}
788aa855a59SPeter Wemm 	if (*es == NULL)
789aa855a59SPeter Wemm 		return ENOENT;
790aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
791aa855a59SPeter Wemm 		if (*es != execsw_arg)
792aa855a59SPeter Wemm 			count++;
793aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
794aa855a59SPeter Wemm 	if (newexecsw == NULL)
795aa855a59SPeter Wemm 		return ENOMEM;
796aa855a59SPeter Wemm 	xs = newexecsw;
797aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
798aa855a59SPeter Wemm 		if (*es != execsw_arg)
799aa855a59SPeter Wemm 			*xs++ = *es;
800aa855a59SPeter Wemm 	*xs = NULL;
801aa855a59SPeter Wemm 	if (execsw)
802aa855a59SPeter Wemm 		free(execsw, M_TEMP);
803aa855a59SPeter Wemm 	execsw = newexecsw;
804aa855a59SPeter Wemm 	return 0;
805aa855a59SPeter Wemm }
806