xref: /freebsd/sys/kern/kern_exec.c (revision 9b3b1c5fdf07ad8d088a5730514acf00f01252c0)
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>
31fb919e4dSMark Murray #include <sys/lock.h>
32fb919e4dSMark Murray #include <sys/mutex.h>
33d2d3e875SBruce Evans #include <sys/sysproto.h>
3426f9a767SRodney W. Grimes #include <sys/signalvar.h>
3526f9a767SRodney W. Grimes #include <sys/kernel.h>
3626f9a767SRodney W. Grimes #include <sys/mount.h>
37797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
38079cc25bSDavid Greenman #include <sys/fcntl.h>
3926f9a767SRodney W. Grimes #include <sys/acct.h>
4026f9a767SRodney W. Grimes #include <sys/exec.h>
4126f9a767SRodney W. Grimes #include <sys/imgact.h>
42e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4326f9a767SRodney W. Grimes #include <sys/wait.h>
44333ea485SGuido van Rooij #include <sys/malloc.h>
45a794e791SBruce Evans #include <sys/proc.h>
462a024a2bSSean Eric Fagan #include <sys/pioctl.h>
47a794e791SBruce Evans #include <sys/namei.h>
481e1e0b44SSøren Schmidt #include <sys/sysent.h>
49797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5099ac3bc8SPeter Wemm #include <sys/sysctl.h>
51333ea485SGuido van Rooij #include <sys/user.h>
52a794e791SBruce Evans #include <sys/vnode.h>
5326f9a767SRodney W. Grimes 
5426f9a767SRodney W. Grimes #include <vm/vm.h>
55efeaf95aSDavid Greenman #include <vm/vm_param.h>
56efeaf95aSDavid Greenman #include <vm/pmap.h>
571616db3cSJohn Dyson #include <vm/vm_page.h>
58efeaf95aSDavid Greenman #include <vm/vm_map.h>
5926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
60efeaf95aSDavid Greenman #include <vm/vm_extern.h>
611ebd0c59SDavid Greenman #include <vm/vm_object.h>
621616db3cSJohn Dyson #include <vm/vm_pager.h>
6326f9a767SRodney W. Grimes 
6426f9a767SRodney W. Grimes #include <machine/reg.h>
6526f9a767SRodney W. Grimes 
66b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
67b9df5231SPoul-Henning Kamp 
6821d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback");
6921d56e9cSAlfred Perlstein 
7021d56e9cSAlfred Perlstein /*
7121d56e9cSAlfred Perlstein  * callout list for things to do at exec time
7221d56e9cSAlfred Perlstein  */
7321d56e9cSAlfred Perlstein struct execlist {
7421d56e9cSAlfred Perlstein 	execlist_fn function;
7521d56e9cSAlfred Perlstein 	TAILQ_ENTRY(execlist) next;
7621d56e9cSAlfred Perlstein };
7721d56e9cSAlfred Perlstein 
7821d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist);
7921d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list);
8021d56e9cSAlfred Perlstein 
814d77a549SAlfred Perlstein static register_t *exec_copyout_strings(struct image_params *);
82df8bae1dSRodney W. Grimes 
839701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
849701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS;
859701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
86486bddb0SDoug Rabson 
879701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
889701cd40SJohn Baldwin static u_long usrstack = USRSTACK;
899701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
9099ac3bc8SPeter Wemm 
91b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
92c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
939701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
94b9df5231SPoul-Henning Kamp 
95a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
96a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
97a8704f89SPoul-Henning Kamp 
98911fc923SPeter Wemm #ifdef __ia64__
99911fc923SPeter Wemm /* XXX HACK */
100911fc923SPeter Wemm static int regstkpages = 256;
101911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, &regstkpages, 0, "");
102911fc923SPeter Wemm #endif
103911fc923SPeter Wemm 
10499ac3bc8SPeter Wemm /*
105aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
106aa855a59SPeter Wemm  * double pointer here.
107df8bae1dSRodney W. Grimes  */
108aa855a59SPeter Wemm static const struct execsw **execsw;
10926f9a767SRodney W. Grimes 
110d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
111ad7507e2SSteven Wallace struct execve_args {
112ad7507e2SSteven Wallace         char    *fname;
113ad7507e2SSteven Wallace         char    **argv;
114ad7507e2SSteven Wallace         char    **envv;
115ad7507e2SSteven Wallace };
116d2d3e875SBruce Evans #endif
117ad7507e2SSteven Wallace 
11826f9a767SRodney W. Grimes /*
11926f9a767SRodney W. Grimes  * execve() system call.
120116734c4SMatthew Dillon  *
121116734c4SMatthew Dillon  * MPSAFE
12226f9a767SRodney W. Grimes  */
12326f9a767SRodney W. Grimes int
124b40ce416SJulian Elischer execve(td, uap)
125b40ce416SJulian Elischer 	struct thread *td;
12626f9a767SRodney W. Grimes 	register struct execve_args *uap;
127df8bae1dSRodney W. Grimes {
128b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
12926f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
1309b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
131654f6be1SBruce Evans 	register_t *stack_base;
132bb56ec4aSPoul-Henning Kamp 	int error, len, i;
133c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
13426f9a767SRodney W. Grimes 	struct vattr attr;
1354d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
1369b3b1c5fSJohn Baldwin 	struct pargs *oldargs, *newargs = NULL;
1379b3b1c5fSJohn Baldwin 	struct procsig *oldprocsig, *newprocsig;
1389b3b1c5fSJohn Baldwin 	struct vnode *tracevp = NULL, *textvp = NULL;
13926f9a767SRodney W. Grimes 
140c52007c2SDavid Greenman 	imgp = &image_params;
141df8bae1dSRodney W. Grimes 
1429ca45e81SDag-Erling Smørgrav 	/*
1439ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
1449ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
1459ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
1469ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
1479ca45e81SDag-Erling Smørgrav 	 * privileges.
1489ca45e81SDag-Erling Smørgrav 	 */
1499ca45e81SDag-Erling Smørgrav 	mtx_lock(&Giant);
1509ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
1519ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
15291f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
1539ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
1549ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
1559ca45e81SDag-Erling Smørgrav 
156b40ce416SJulian Elischer /* XXXKSE */
157b40ce416SJulian Elischer /* !!!!!!!! we need abort all the other threads of this process before we */
158b40ce416SJulian Elischer /* proceed beyond his point! */
159b40ce416SJulian Elischer 
160df8bae1dSRodney W. Grimes 	/*
161c52007c2SDavid Greenman 	 * Initialize part of the common data
162df8bae1dSRodney W. Grimes 	 */
163c52007c2SDavid Greenman 	imgp->proc = p;
164c52007c2SDavid Greenman 	imgp->uap = uap;
165c52007c2SDavid Greenman 	imgp->attr = &attr;
166c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1675cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
168c52007c2SDavid Greenman 	imgp->entry_addr = 0;
169c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
170c52007c2SDavid Greenman 	imgp->interpreted = 0;
171c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
172e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1731616db3cSJohn Dyson 	imgp->vp = NULL;
1741616db3cSJohn Dyson 	imgp->firstpage = NULL;
1754fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
176b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
17726f9a767SRodney W. Grimes 
17826f9a767SRodney W. Grimes 	/*
17926f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
18026f9a767SRodney W. Grimes 	 *	environment strings
18126f9a767SRodney W. Grimes 	 */
1821616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
183c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
18426f9a767SRodney W. Grimes 		error = ENOMEM;
18526f9a767SRodney W. Grimes 		goto exec_fail;
18626f9a767SRodney W. Grimes 	}
187c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
188c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1891616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
19026f9a767SRodney W. Grimes 
19126f9a767SRodney W. Grimes 	/*
19226f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
19326f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
19426f9a767SRodney W. Grimes 	 */
19526f9a767SRodney W. Grimes 	ndp = &nd;
196b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
197b40ce416SJulian Elischer 	    UIO_USERSPACE, uap->fname, td);
19826f9a767SRodney W. Grimes 
19926f9a767SRodney W. Grimes interpret:
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 	error = namei(ndp);
20226f9a767SRodney W. Grimes 	if (error) {
2031616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
2041616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
20526f9a767SRodney W. Grimes 		goto exec_fail;
20626f9a767SRodney W. Grimes 	}
20726f9a767SRodney W. Grimes 
208c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
2099c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
21026f9a767SRodney W. Grimes 
21126f9a767SRodney W. Grimes 	/*
2129022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
2139022e4adSDavid Greenman 	 */
214c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
2158677f509SDavid Greenman 	if (error) {
216b40ce416SJulian Elischer 		VOP_UNLOCK(imgp->vp, 0, td);
21726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
2188677f509SDavid Greenman 	}
21926f9a767SRodney W. Grimes 
2201616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
221b40ce416SJulian Elischer 	VOP_UNLOCK(imgp->vp, 0, td);
2226d5a0a8cSDavid Greenman 	if (error)
22326f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
22426f9a767SRodney W. Grimes 
22526f9a767SRodney W. Grimes 	/*
226d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
227d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
228d323ddf3SMatthew Dillon 	 *	scripts differently.
22926f9a767SRodney W. Grimes 	 */
230d323ddf3SMatthew Dillon 	error = -1;
231d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
232d323ddf3SMatthew Dillon 		error = img_first(imgp);
233d323ddf3SMatthew Dillon 
234d323ddf3SMatthew Dillon 	/*
235d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
236d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
237d323ddf3SMatthew Dillon 	 *	and an error otherwise.
238d323ddf3SMatthew Dillon 	 */
239d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
240d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
241d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
242d323ddf3SMatthew Dillon 			continue;
243d323ddf3SMatthew Dillon 		}
244c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
245d323ddf3SMatthew Dillon 	}
246d323ddf3SMatthew Dillon 
247d323ddf3SMatthew Dillon 	if (error) {
24826f9a767SRodney W. Grimes 		if (error == -1)
249d323ddf3SMatthew Dillon 			error = ENOEXEC;
25026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
251d323ddf3SMatthew Dillon 	}
252d323ddf3SMatthew Dillon 
253d323ddf3SMatthew Dillon 	/*
254d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
255d323ddf3SMatthew Dillon 	 * activate the interpreter.
256d323ddf3SMatthew Dillon 	 */
257c52007c2SDavid Greenman 	if (imgp->interpreted) {
2581616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
259762e6b85SEivind Eklund 		/* free name buffer and old vnode */
260762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
261f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
26226f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
263b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
264b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
26526f9a767SRodney W. Grimes 		goto interpret;
26626f9a767SRodney W. Grimes 	}
26726f9a767SRodney W. Grimes 
26826f9a767SRodney W. Grimes 	/*
26926f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
27026f9a767SRodney W. Grimes 	 */
271c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
27226f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
27326f9a767SRodney W. Grimes 
27426f9a767SRodney W. Grimes 	/*
2751e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2761e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2771e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
27826f9a767SRodney W. Grimes 	 */
2791e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
280c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2811e1e0b44SSøren Schmidt 	else
282c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
28326f9a767SRodney W. Grimes 
284a78e8d2aSDavid Greenman 	/*
285a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
286a78e8d2aSDavid Greenman 	 * be shared after an exec.
287a78e8d2aSDavid Greenman 	 */
288426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
289a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
290a78e8d2aSDavid Greenman 		struct filedesc *tmp;
291a78e8d2aSDavid Greenman 
292b40ce416SJulian Elischer 		tmp = fdcopy(td);
293426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
294b40ce416SJulian Elischer 		fdfree(td);
295a78e8d2aSDavid Greenman 		p->p_fd = tmp;
296426da3bcSAlfred Perlstein 	} else
297426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
298a78e8d2aSDavid Greenman 
299333ea485SGuido van Rooij 	/*
3009b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
3019b3b1c5fSJohn Baldwin 	 */
3029b3b1c5fSJohn Baldwin 	newcred = crget();
3039b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
3049b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
3059b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
3069b3b1c5fSJohn Baldwin 
3079b3b1c5fSJohn Baldwin 	/* close files on exec */
3089b3b1c5fSJohn Baldwin 	fdcloseexec(td);
3099b3b1c5fSJohn Baldwin 
3109b3b1c5fSJohn Baldwin 	/*
311333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
3128688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
313b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
314333ea485SGuido van Rooij 	 * reset.
315333ea485SGuido van Rooij 	 */
3169b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
3179b3b1c5fSJohn Baldwin 	mp_fixme("procsig needs a lock");
318333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
3199b3b1c5fSJohn Baldwin 		oldprocsig = p->p_procsig;
3209b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
321333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
322333ea485SGuido van Rooij 		    M_SUBPROC, M_WAITOK);
3239b3b1c5fSJohn Baldwin 		bcopy(oldprocsig, newprocsig, sizeof(*newprocsig));
3249b3b1c5fSJohn Baldwin 		newprocsig->ps_refcnt = 1;
3259b3b1c5fSJohn Baldwin 		oldprocsig->ps_refcnt--;
3269b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
327333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
328b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
329b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
330333ea485SGuido van Rooij 
331b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
332b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
333333ea485SGuido van Rooij 	}
334fdf4e8b3SWarner Losh 	/* Stop profiling */
335fdf4e8b3SWarner Losh 	stopprofclock(p);
336fdf4e8b3SWarner Losh 
33726f9a767SRodney W. Grimes 	/* reset caught signals */
33826f9a767SRodney W. Grimes 	execsigs(p);
33926f9a767SRodney W. Grimes 
34026f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
34126f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
34226f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
34326f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
34426f9a767SRodney W. Grimes 
34526f9a767SRodney W. Grimes 	/*
34624b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
347dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
34826f9a767SRodney W. Grimes 	 */
34926f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
35026f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
35126f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
35226f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
35326f9a767SRodney W. Grimes 	}
35426f9a767SRodney W. Grimes 
35526f9a767SRodney W. Grimes 	/*
356a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
357a78e8d2aSDavid Greenman 	 *
358a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
359a78e8d2aSDavid Greenman 	 * the process is being traced.
360c52007c2SDavid Greenman 	 */
361b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
362b1fc0ec1SRobert Watson 	if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) ||
363b1fc0ec1SRobert Watson 	     ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) &&
364a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
365c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
366c52007c2SDavid Greenman 		/*
367c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
368b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
369b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
37026f9a767SRodney W. Grimes 		 */
371b85db196SPeter Wemm 		setsugid(p);
37244731cabSJohn Baldwin 		if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) {
37379deba82SMatthew Dillon 			p->p_traceflag = 0;
3749b3b1c5fSJohn Baldwin 			tracevp = p->p_tracep;
3759b3b1c5fSJohn Baldwin 			p->p_tracep = NULL;
37626f9a767SRodney W. Grimes 		}
377e983a376SJacques Vidrine 		/* Make sure file descriptors 0..2 are in use.  */
378e983a376SJacques Vidrine 		error = fdcheckstd(td);
379e983a376SJacques Vidrine 		if (error != 0)
380e983a376SJacques Vidrine 			goto exec_fail_dealloc;
381c52007c2SDavid Greenman 		/*
382c52007c2SDavid Greenman 		 * Set the new credentials.
383c52007c2SDavid Greenman 		 */
3849b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
385c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
386b1fc0ec1SRobert Watson 			change_euid(newcred, attr.va_uid);
387c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
388b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
389b40ce416SJulian Elischer 		setugidsafety(td);
3909b3b1c5fSJohn Baldwin 		/*
3919b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
3929b3b1c5fSJohn Baldwin 		 */
3939b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
3949b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
3959b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
3969b3b1c5fSJohn Baldwin 		newcred = NULL;
397c52007c2SDavid Greenman 	} else {
398b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
399b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
400c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
40126f9a767SRodney W. Grimes 		/*
402c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
403b1fc0ec1SRobert Watson 		 *
404b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
4059b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
4069b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
4079b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
4089b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
4099b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
4109b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
4119b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
41226f9a767SRodney W. Grimes 		 */
413b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
414b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
4159b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
416b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
417b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
418b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
4199b3b1c5fSJohn Baldwin 			newcred = NULL;
4209b3b1c5fSJohn Baldwin 		}
421b1fc0ec1SRobert Watson 	}
42226f9a767SRodney W. Grimes 
42326f9a767SRodney W. Grimes 	/*
4242a531c80SDavid Greenman 	 * Store the vp for use in procfs
4252a531c80SDavid Greenman 	 */
4269b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
4272a531c80SDavid Greenman 	VREF(ndp->ni_vp);
4282a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
4292a531c80SDavid Greenman 
4302a531c80SDavid Greenman 	/*
4319ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
4329ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
433cb679c38SJonathan Lemon 	 */
434cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
4359ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
436cb679c38SJonathan Lemon 
437cb679c38SJonathan Lemon 	/*
43826f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
43926f9a767SRodney W. Grimes 	 * can be set before the program executes.
44026f9a767SRodney W. Grimes 	 */
441e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
4422a024a2bSSean Eric Fagan 
44326f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
44426f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
44526f9a767SRodney W. Grimes 
44626f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
44726f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
44826f9a767SRodney W. Grimes 
449b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
4509b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
451b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
452b9df5231SPoul-Henning Kamp 
453e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
454e913ca22SDoug Rabson 	setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
455e913ca22SDoug Rabson 	    imgp->ps_strings);
456e913ca22SDoug Rabson 
457b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
458b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
4599b3b1c5fSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
4609b3b1c5fSJohn Baldwin 		p->p_args = newargs;
4619b3b1c5fSJohn Baldwin 		newargs = NULL;
462fbd26f75SJohn Baldwin 	}
4639b3b1c5fSJohn Baldwin 	PROC_UNLOCK(p);
4649b3b1c5fSJohn Baldwin 
4659b3b1c5fSJohn Baldwin 	/*
4669b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
4679b3b1c5fSJohn Baldwin 	 */
4689b3b1c5fSJohn Baldwin 	if (newcred == NULL)
4699b3b1c5fSJohn Baldwin 		crfree(oldcred);
4709b3b1c5fSJohn Baldwin 	else
4719b3b1c5fSJohn Baldwin 		crfree(newcred);
4729b3b1c5fSJohn Baldwin 	KASSERT(newargs == NULL, ("leaking p_args"));
4739b3b1c5fSJohn Baldwin 	/*
4749b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
4759b3b1c5fSJohn Baldwin 	 */
4769b3b1c5fSJohn Baldwin 	if (textvp != NULL)
4779b3b1c5fSJohn Baldwin 		vrele(textvp);
4789b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
4799b3b1c5fSJohn Baldwin 		vrele(tracevp);
4809b3b1c5fSJohn Baldwin 	pargs_drop(oldargs);
481b9df5231SPoul-Henning Kamp 
4821616db3cSJohn Dyson exec_fail_dealloc:
4831616db3cSJohn Dyson 
48426f9a767SRodney W. Grimes 	/*
48526f9a767SRodney W. Grimes 	 * free various allocated resources
48626f9a767SRodney W. Grimes 	 */
4871616db3cSJohn Dyson 	if (imgp->firstpage)
4881616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
48926f9a767SRodney W. Grimes 
490c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
4911616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
4921616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
4931616db3cSJohn Dyson 
4949c0fed3dSDoug Rabson 	if (imgp->vp) {
495762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
4969c0fed3dSDoug Rabson 		vrele(imgp->vp);
497a3cf6ebaSDavid Greenman 	}
49826f9a767SRodney W. Grimes 
4991616db3cSJohn Dyson 	if (error == 0)
500116734c4SMatthew Dillon 		goto done2;
5011616db3cSJohn Dyson 
50226f9a767SRodney W. Grimes exec_fail:
5039ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
5049ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
5059ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
5069ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
5079ca45e81SDag-Erling Smørgrav 
508c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
50926f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
510b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
51126f9a767SRodney W. Grimes 		/* NOT REACHED */
512116734c4SMatthew Dillon 		error = 0;
51326f9a767SRodney W. Grimes 	}
514116734c4SMatthew Dillon done2:
515116734c4SMatthew Dillon 	mtx_unlock(&Giant);
516116734c4SMatthew Dillon 	return (error);
51726f9a767SRodney W. Grimes }
51826f9a767SRodney W. Grimes 
5191616db3cSJohn Dyson int
5201616db3cSJohn Dyson exec_map_first_page(imgp)
5211616db3cSJohn Dyson 	struct image_params *imgp;
5221616db3cSJohn Dyson {
523d8aad40cSJohn Baldwin 	int rv, i;
52495461b45SJohn Dyson 	int initial_pagein;
52595461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
5261616db3cSJohn Dyson 	vm_object_t object;
5271616db3cSJohn Dyson 
5280cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5291616db3cSJohn Dyson 
5301616db3cSJohn Dyson 	if (imgp->firstpage) {
5311616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
5321616db3cSJohn Dyson 	}
5331616db3cSJohn Dyson 
5349ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
5351616db3cSJohn Dyson 
53695461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
5371616db3cSJohn Dyson 
53895461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
53995461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
54095461b45SJohn Dyson 		if (initial_pagein > object->size)
54195461b45SJohn Dyson 			initial_pagein = object->size;
54295461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
543d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
54495461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
54595461b45SJohn Dyson 					break;
54695461b45SJohn Dyson 				if (ma[i]->valid)
54795461b45SJohn Dyson 					break;
548e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
54995461b45SJohn Dyson 			} else {
55095461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
55195461b45SJohn Dyson 				if (ma[i] == NULL)
55295461b45SJohn Dyson 					break;
55395461b45SJohn Dyson 			}
55495461b45SJohn Dyson 		}
55595461b45SJohn Dyson 		initial_pagein = i;
5561616db3cSJohn Dyson 
55795461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
55895461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
55995461b45SJohn Dyson 
560eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
561ecbb00a2SDoug Rabson 			if (ma[0]) {
56295461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
5638f9110f6SJohn Dyson 				vm_page_free(ma[0]);
564ecbb00a2SDoug Rabson 			}
5651616db3cSJohn Dyson 			return EIO;
5661616db3cSJohn Dyson 		}
5671616db3cSJohn Dyson 	}
5681616db3cSJohn Dyson 
56995461b45SJohn Dyson 	vm_page_wire(ma[0]);
570e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
5711616db3cSJohn Dyson 
572ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
57395461b45SJohn Dyson 	imgp->firstpage = ma[0];
5741616db3cSJohn Dyson 
5751616db3cSJohn Dyson 	return 0;
5761616db3cSJohn Dyson }
5771616db3cSJohn Dyson 
5781616db3cSJohn Dyson void
5791616db3cSJohn Dyson exec_unmap_first_page(imgp)
5801616db3cSJohn Dyson 	struct image_params *imgp;
5811616db3cSJohn Dyson {
5820cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
58323955314SAlfred Perlstein 
5841616db3cSJohn Dyson 	if (imgp->firstpage) {
585ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
58673007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
5871616db3cSJohn Dyson 		imgp->firstpage = NULL;
5881616db3cSJohn Dyson 	}
5891616db3cSJohn Dyson }
5901616db3cSJohn Dyson 
59126f9a767SRodney W. Grimes /*
59226f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
59326f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
59426f9a767SRodney W. Grimes  *	automatically in trap.c.
59526f9a767SRodney W. Grimes  */
59626f9a767SRodney W. Grimes int
597c52007c2SDavid Greenman exec_new_vmspace(imgp)
598c52007c2SDavid Greenman 	struct image_params *imgp;
59926f9a767SRodney W. Grimes {
60026f9a767SRodney W. Grimes 	int error;
6016f5dafeaSAlan Cox 	struct execlist *ep;
6025e20c11fSAlan Cox 	struct proc *p = imgp->proc;
6035e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
6049a024fc5SRobert Drehmel 	vm_offset_t stack_addr = USRSTACK - maxssiz;
60526f9a767SRodney W. Grimes 
6060cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6070cddd8f0SMatthew Dillon 
608c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
60926f9a767SRodney W. Grimes 
610af9ec885SJohn Dyson 	/*
6116f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
6126f5dafeaSAlan Cox 	 */
6136f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
6145e20c11fSAlan Cox 		(*ep->function)(p);
6156f5dafeaSAlan Cox 
6166f5dafeaSAlan Cox 	/*
617af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
618af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
619af9ec885SJohn Dyson 	 * not disrupted
620af9ec885SJohn Dyson 	 */
621af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
6223d903220SDoug Rabson 		if (vmspace->vm_shm)
6235e20c11fSAlan Cox 			shmexit(p);
624b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
625cb100b25SAlan Cox 		vm_map_remove(&vmspace->vm_map, 0, VM_MAXUSER_ADDRESS);
626af9ec885SJohn Dyson 	} else {
6275e20c11fSAlan Cox 		vmspace_exec(p);
6285e20c11fSAlan Cox 		vmspace = p->p_vmspace;
629af9ec885SJohn Dyson 	}
63026f9a767SRodney W. Grimes 
63126f9a767SRodney W. Grimes 	/* Allocate a new stack */
6329a024fc5SRobert Drehmel 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
6339a024fc5SRobert Drehmel 	    VM_PROT_ALL, VM_PROT_ALL, 0);
6342267af78SJulian Elischer 	if (error)
6352267af78SJulian Elischer 		return (error);
6362267af78SJulian Elischer 
63763c47a5cSDoug Rabson #ifdef __ia64__
63863c47a5cSDoug Rabson 	{
63963c47a5cSDoug Rabson 		/*
64063c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
64163c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
64263c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
64363c47a5cSDoug Rabson 		 */
64463c47a5cSDoug Rabson 		vm_offset_t bsaddr;
645cbc89bfbSPaul Saab 		bsaddr = USRSTACK - 2*maxssiz;
64663c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
647911fc923SPeter Wemm 				    regstkpages * PAGE_SIZE, 0,
64863c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
6495e20c11fSAlan Cox 		FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
65063c47a5cSDoug Rabson 	}
65163c47a5cSDoug Rabson #endif
65263c47a5cSDoug Rabson 
6532267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
6542267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
6552267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
6562267af78SJulian Elischer 	 */
657cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
658cbc89bfbSPaul Saab 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
65926f9a767SRodney W. Grimes 
66026f9a767SRodney W. Grimes 	return(0);
66126f9a767SRodney W. Grimes }
66226f9a767SRodney W. Grimes 
66326f9a767SRodney W. Grimes /*
66426f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
66526f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
66626f9a767SRodney W. Grimes  */
66726f9a767SRodney W. Grimes int
668c52007c2SDavid Greenman exec_extract_strings(imgp)
669c52007c2SDavid Greenman 	struct image_params *imgp;
67026f9a767SRodney W. Grimes {
67126f9a767SRodney W. Grimes 	char	**argv, **envv;
67226f9a767SRodney W. Grimes 	char	*argp, *envp;
673ecbb00a2SDoug Rabson 	int	error;
674ecbb00a2SDoug Rabson 	size_t	length;
67526f9a767SRodney W. Grimes 
67626f9a767SRodney W. Grimes 	/*
67726f9a767SRodney W. Grimes 	 * extract arguments first
67826f9a767SRodney W. Grimes 	 */
67926f9a767SRodney W. Grimes 
680c52007c2SDavid Greenman 	argv = imgp->uap->argv;
68126f9a767SRodney W. Grimes 
6820fd1a014SDavid Greenman 	if (argv) {
683aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
6845cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
6855cf3d12cSAndrey A. Chernov 			return (EFAULT);
6865cf3d12cSAndrey A. Chernov 		if (argp)
6875cf3d12cSAndrey A. Chernov 			argv++;
6885cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
6895cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
6905cf3d12cSAndrey A. Chernov 		if (argp) {
6915cf3d12cSAndrey A. Chernov 			do {
69226f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
69326f9a767SRodney W. Grimes 					return (EFAULT);
694c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
695c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
6960fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
69726f9a767SRodney W. Grimes 						return(E2BIG);
6980fd1a014SDavid Greenman 					return (error);
6990fd1a014SDavid Greenman 				}
700c52007c2SDavid Greenman 				imgp->stringspace -= length;
701c52007c2SDavid Greenman 				imgp->stringp += length;
702c52007c2SDavid Greenman 				imgp->argc++;
703aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
70426f9a767SRodney W. Grimes 		}
7050fd1a014SDavid Greenman 	}
70626f9a767SRodney W. Grimes 
707b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
708b9df5231SPoul-Henning Kamp 
70926f9a767SRodney W. Grimes 	/*
71026f9a767SRodney W. Grimes 	 * extract environment strings
71126f9a767SRodney W. Grimes 	 */
71226f9a767SRodney W. Grimes 
713c52007c2SDavid Greenman 	envv = imgp->uap->envv;
71426f9a767SRodney W. Grimes 
7150fd1a014SDavid Greenman 	if (envv) {
716aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
71726f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
71826f9a767SRodney W. Grimes 				return (EFAULT);
719c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
720c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
7210fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
72226f9a767SRodney W. Grimes 					return(E2BIG);
7230fd1a014SDavid Greenman 				return (error);
7240fd1a014SDavid Greenman 			}
725c52007c2SDavid Greenman 			imgp->stringspace -= length;
726c52007c2SDavid Greenman 			imgp->stringp += length;
727c52007c2SDavid Greenman 			imgp->envc++;
72826f9a767SRodney W. Grimes 		}
7290fd1a014SDavid Greenman 	}
73026f9a767SRodney W. Grimes 
73126f9a767SRodney W. Grimes 	return (0);
73226f9a767SRodney W. Grimes }
73326f9a767SRodney W. Grimes 
73426f9a767SRodney W. Grimes /*
73526f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
73626f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
73726f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
73826f9a767SRodney W. Grimes  */
739654f6be1SBruce Evans register_t *
740c52007c2SDavid Greenman exec_copyout_strings(imgp)
741c52007c2SDavid Greenman 	struct image_params *imgp;
74226f9a767SRodney W. Grimes {
74326f9a767SRodney W. Grimes 	int argc, envc;
74426f9a767SRodney W. Grimes 	char **vectp;
74526f9a767SRodney W. Grimes 	char *stringp, *destp;
746654f6be1SBruce Evans 	register_t *stack_base;
74793f6448cSDavid Greenman 	struct ps_strings *arginfo;
748d66a5066SPeter Wemm 	int szsigcode;
74926f9a767SRodney W. Grimes 
75026f9a767SRodney W. Grimes 	/*
75126f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
752d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
75326f9a767SRodney W. Grimes 	 */
7544c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
755d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
756d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
7571ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
7581ed012f9SPeter Wemm 
75926f9a767SRodney W. Grimes 	/*
760d66a5066SPeter Wemm 	 * install sigcode
761d66a5066SPeter Wemm 	 */
762d66a5066SPeter Wemm 	if (szsigcode)
763d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
764d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
765d66a5066SPeter Wemm 
766d66a5066SPeter Wemm 	/*
767e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
768e1743d02SSøren Schmidt 	 * on the stack.
769e1743d02SSøren Schmidt 	 */
770b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
771e1743d02SSøren Schmidt 		/*
772b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
773b9a22da4STakanori Watanabe 		 * lower compatibility.
774b9a22da4STakanori Watanabe 		 */
775b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
776b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
777b9a22da4STakanori Watanabe 		/*
778b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
779b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
780b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
781e1743d02SSøren Schmidt 		 */
782e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
783b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
784b9a22da4STakanori Watanabe 
785b9a22da4STakanori Watanabe 	} else
786e1743d02SSøren Schmidt 		/*
787b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
788b9a22da4STakanori Watanabe 		 * the arg and env vector sets
78926f9a767SRodney W. Grimes 		 */
790e1743d02SSøren Schmidt 		vectp = (char **)
791e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
79226f9a767SRodney W. Grimes 
79326f9a767SRodney W. Grimes 	/*
79426f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
79526f9a767SRodney W. Grimes 	 */
796654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
79726f9a767SRodney W. Grimes 
798c52007c2SDavid Greenman 	stringp = imgp->stringbase;
799c52007c2SDavid Greenman 	argc = imgp->argc;
800c52007c2SDavid Greenman 	envc = imgp->envc;
80126f9a767SRodney W. Grimes 
80293f6448cSDavid Greenman 	/*
8033aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
80493f6448cSDavid Greenman 	 */
805c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
80693f6448cSDavid Greenman 
80793f6448cSDavid Greenman 	/*
8083aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
8093aed948bSDavid Greenman 	 */
810aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
8113aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
8123aed948bSDavid Greenman 
8133aed948bSDavid Greenman 	/*
8143aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
81593f6448cSDavid Greenman 	 */
81626f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
817aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
8183aed948bSDavid Greenman 		while (*stringp++ != 0)
8193aed948bSDavid Greenman 			destp++;
8203aed948bSDavid Greenman 		destp++;
82126f9a767SRodney W. Grimes 	}
82226f9a767SRodney W. Grimes 
8231a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
8246ab46d52SBruce Evans 	suword(vectp++, 0);
82526f9a767SRodney W. Grimes 
826aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
8273aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
82893f6448cSDavid Greenman 
82993f6448cSDavid Greenman 	/*
8303aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
83193f6448cSDavid Greenman 	 */
83226f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
833aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
8343aed948bSDavid Greenman 		while (*stringp++ != 0)
8353aed948bSDavid Greenman 			destp++;
8363aed948bSDavid Greenman 		destp++;
83726f9a767SRodney W. Grimes 	}
83826f9a767SRodney W. Grimes 
83926f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
8406ab46d52SBruce Evans 	suword(vectp, 0);
84126f9a767SRodney W. Grimes 
84226f9a767SRodney W. Grimes 	return (stack_base);
84326f9a767SRodney W. Grimes }
84426f9a767SRodney W. Grimes 
84526f9a767SRodney W. Grimes /*
84626f9a767SRodney W. Grimes  * Check permissions of file to execute.
847cf64863aSRobert Watson  *	Called with imgp->vp locked.
84826f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
84926f9a767SRodney W. Grimes  */
850c8a79999SPeter Wemm int
851c52007c2SDavid Greenman exec_check_permissions(imgp)
852c52007c2SDavid Greenman 	struct image_params *imgp;
85326f9a767SRodney W. Grimes {
854c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
855c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
856a854ed98SJohn Baldwin 	struct thread *td;
85726f9a767SRodney W. Grimes 	int error;
85826f9a767SRodney W. Grimes 
859a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
86026f9a767SRodney W. Grimes 	/* Get file attributes */
861a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
86226f9a767SRodney W. Grimes 	if (error)
86326f9a767SRodney W. Grimes 		return (error);
86426f9a767SRodney W. Grimes 
86526f9a767SRodney W. Grimes 	/*
86626f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
86726f9a767SRodney W. Grimes 	 *	file resides on.
86826f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
86926f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
87026f9a767SRodney W. Grimes 	 *	file really is executable.
87126f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
87226f9a767SRodney W. Grimes 	 */
873c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
87426f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
875a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
87626f9a767SRodney W. Grimes 		return (EACCES);
87726f9a767SRodney W. Grimes 
87826f9a767SRodney W. Grimes 	/*
87926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
88026f9a767SRodney W. Grimes 	 */
88126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
88226f9a767SRodney W. Grimes 		return (ENOEXEC);
88326f9a767SRodney W. Grimes 
88426f9a767SRodney W. Grimes 	/*
88526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
88626f9a767SRodney W. Grimes 	 */
887a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
88826f9a767SRodney W. Grimes 	if (error)
88926f9a767SRodney W. Grimes 		return (error);
89026f9a767SRodney W. Grimes 
8916d5a0a8cSDavid Greenman 	/*
8926d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
8936d5a0a8cSDavid Greenman 	 * if there are any.
8946d5a0a8cSDavid Greenman 	 */
8956d5a0a8cSDavid Greenman 	if (vp->v_writecount)
8966d5a0a8cSDavid Greenman 		return (ETXTBSY);
8976d5a0a8cSDavid Greenman 
8986d5a0a8cSDavid Greenman 	/*
8996d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
9006d5a0a8cSDavid Greenman 	 * general case).
9016d5a0a8cSDavid Greenman 	 */
902a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
90326f9a767SRodney W. Grimes 	return (error);
904df8bae1dSRodney W. Grimes }
905aa855a59SPeter Wemm 
906aa855a59SPeter Wemm /*
907aa855a59SPeter Wemm  * Exec handler registration
908aa855a59SPeter Wemm  */
909aa855a59SPeter Wemm int
910aa855a59SPeter Wemm exec_register(execsw_arg)
911aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
912aa855a59SPeter Wemm {
913aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
914aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
915aa855a59SPeter Wemm 
916aa855a59SPeter Wemm 	if (execsw)
917aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
918aa855a59SPeter Wemm 			count++;
919aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
920aa855a59SPeter Wemm 	if (newexecsw == NULL)
921aa855a59SPeter Wemm 		return ENOMEM;
922aa855a59SPeter Wemm 	xs = newexecsw;
923aa855a59SPeter Wemm 	if (execsw)
924aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
925aa855a59SPeter Wemm 			*xs++ = *es;
926aa855a59SPeter Wemm 	*xs++ = execsw_arg;
927aa855a59SPeter Wemm 	*xs = NULL;
928aa855a59SPeter Wemm 	if (execsw)
929aa855a59SPeter Wemm 		free(execsw, M_TEMP);
930aa855a59SPeter Wemm 	execsw = newexecsw;
931aa855a59SPeter Wemm 	return 0;
932aa855a59SPeter Wemm }
933aa855a59SPeter Wemm 
934aa855a59SPeter Wemm int
935aa855a59SPeter Wemm exec_unregister(execsw_arg)
936aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
937aa855a59SPeter Wemm {
938aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
939aa855a59SPeter Wemm 	int count = 1;
940aa855a59SPeter Wemm 
941aa855a59SPeter Wemm 	if (execsw == NULL)
942aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
943aa855a59SPeter Wemm 
944aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
945aa855a59SPeter Wemm 		if (*es == execsw_arg)
946aa855a59SPeter Wemm 			break;
947aa855a59SPeter Wemm 	}
948aa855a59SPeter Wemm 	if (*es == NULL)
949aa855a59SPeter Wemm 		return ENOENT;
950aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
951aa855a59SPeter Wemm 		if (*es != execsw_arg)
952aa855a59SPeter Wemm 			count++;
953aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
954aa855a59SPeter Wemm 	if (newexecsw == NULL)
955aa855a59SPeter Wemm 		return ENOMEM;
956aa855a59SPeter Wemm 	xs = newexecsw;
957aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
958aa855a59SPeter Wemm 		if (*es != execsw_arg)
959aa855a59SPeter Wemm 			*xs++ = *es;
960aa855a59SPeter Wemm 	*xs = NULL;
961aa855a59SPeter Wemm 	if (execsw)
962aa855a59SPeter Wemm 		free(execsw, M_TEMP);
963aa855a59SPeter Wemm 	execsw = newexecsw;
964aa855a59SPeter Wemm 	return 0;
965aa855a59SPeter Wemm }
96621d56e9cSAlfred Perlstein 
96721d56e9cSAlfred Perlstein int
96821d56e9cSAlfred Perlstein at_exec(function)
96921d56e9cSAlfred Perlstein 	execlist_fn function;
97021d56e9cSAlfred Perlstein {
97121d56e9cSAlfred Perlstein 	struct execlist *ep;
97221d56e9cSAlfred Perlstein 
97321d56e9cSAlfred Perlstein #ifdef INVARIANTS
97421d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
97521d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
97621d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
97721d56e9cSAlfred Perlstein 		    function);
97821d56e9cSAlfred Perlstein #endif
97921d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
98021d56e9cSAlfred Perlstein 	if (ep == NULL)
98121d56e9cSAlfred Perlstein 		return (ENOMEM);
98221d56e9cSAlfred Perlstein 	ep->function = function;
98321d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
98421d56e9cSAlfred Perlstein 	return (0);
98521d56e9cSAlfred Perlstein }
98621d56e9cSAlfred Perlstein 
98721d56e9cSAlfred Perlstein /*
98821d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
98921d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
99021d56e9cSAlfred Perlstein  */
99121d56e9cSAlfred Perlstein int
99221d56e9cSAlfred Perlstein rm_at_exec(function)
99321d56e9cSAlfred Perlstein 	execlist_fn function;
99421d56e9cSAlfred Perlstein {
99521d56e9cSAlfred Perlstein 	struct execlist *ep;
99621d56e9cSAlfred Perlstein 
99721d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
99821d56e9cSAlfred Perlstein 		if (ep->function == function) {
99921d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
100021d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
100121d56e9cSAlfred Perlstein 			return(1);
100221d56e9cSAlfred Perlstein 		}
100321d56e9cSAlfred Perlstein 	}
100421d56e9cSAlfred Perlstein 	return (0);
100521d56e9cSAlfred Perlstein }
100621d56e9cSAlfred Perlstein 
1007