xref: /freebsd/sys/kern/kern_exec.c (revision a854ed98931b2e365eddd24cd2a1bb53a3f1828f)
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 
81654f6be1SBruce Evans static register_t *exec_copyout_strings __P((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 
9899ac3bc8SPeter Wemm /*
99aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
100aa855a59SPeter Wemm  * double pointer here.
101df8bae1dSRodney W. Grimes  */
102aa855a59SPeter Wemm static const struct execsw **execsw;
10326f9a767SRodney W. Grimes 
104d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
105ad7507e2SSteven Wallace struct execve_args {
106ad7507e2SSteven Wallace         char    *fname;
107ad7507e2SSteven Wallace         char    **argv;
108ad7507e2SSteven Wallace         char    **envv;
109ad7507e2SSteven Wallace };
110d2d3e875SBruce Evans #endif
111ad7507e2SSteven Wallace 
11226f9a767SRodney W. Grimes /*
11326f9a767SRodney W. Grimes  * execve() system call.
114116734c4SMatthew Dillon  *
115116734c4SMatthew Dillon  * MPSAFE
11626f9a767SRodney W. Grimes  */
11726f9a767SRodney W. Grimes int
118b40ce416SJulian Elischer execve(td, uap)
119b40ce416SJulian Elischer 	struct thread *td;
12026f9a767SRodney W. Grimes 	register struct execve_args *uap;
121df8bae1dSRodney W. Grimes {
122b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
12326f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
124b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
125654f6be1SBruce Evans 	register_t *stack_base;
126bb56ec4aSPoul-Henning Kamp 	int error, len, i;
127c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
12826f9a767SRodney W. Grimes 	struct vattr attr;
129d323ddf3SMatthew Dillon 	int (*img_first) __P((struct image_params *));
130fbd26f75SJohn Baldwin 	struct pargs *pa;
13126f9a767SRodney W. Grimes 
132c52007c2SDavid Greenman 	imgp = &image_params;
133df8bae1dSRodney W. Grimes 
1349ca45e81SDag-Erling Smørgrav 	/*
1359ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
1369ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
1379ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
1389ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
1399ca45e81SDag-Erling Smørgrav 	 * privileges.
1409ca45e81SDag-Erling Smørgrav 	 */
1419ca45e81SDag-Erling Smørgrav 	mtx_lock(&Giant);
1429ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
1439ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
14491f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
1459ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
1469ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
1479ca45e81SDag-Erling Smørgrav 
148b40ce416SJulian Elischer /* XXXKSE */
149b40ce416SJulian Elischer /* !!!!!!!! we need abort all the other threads of this process before we */
150b40ce416SJulian Elischer /* proceed beyond his point! */
151b40ce416SJulian Elischer 
152df8bae1dSRodney W. Grimes 	/*
153c52007c2SDavid Greenman 	 * Initialize part of the common data
154df8bae1dSRodney W. Grimes 	 */
155c52007c2SDavid Greenman 	imgp->proc = p;
156c52007c2SDavid Greenman 	imgp->uap = uap;
157c52007c2SDavid Greenman 	imgp->attr = &attr;
158c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1595cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
160c52007c2SDavid Greenman 	imgp->entry_addr = 0;
161c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
162c52007c2SDavid Greenman 	imgp->interpreted = 0;
163c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
164e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1651616db3cSJohn Dyson 	imgp->vp = NULL;
1661616db3cSJohn Dyson 	imgp->firstpage = NULL;
1674fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
168b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
16926f9a767SRodney W. Grimes 
17026f9a767SRodney W. Grimes 	/*
17126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
17226f9a767SRodney W. Grimes 	 *	environment strings
17326f9a767SRodney W. Grimes 	 */
1741616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
175c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
17626f9a767SRodney W. Grimes 		error = ENOMEM;
17726f9a767SRodney W. Grimes 		goto exec_fail;
17826f9a767SRodney W. Grimes 	}
179c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
180c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1811616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
18226f9a767SRodney W. Grimes 
18326f9a767SRodney W. Grimes 	/*
18426f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
18526f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
18626f9a767SRodney W. Grimes 	 */
18726f9a767SRodney W. Grimes 	ndp = &nd;
188b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
189b40ce416SJulian Elischer 	    UIO_USERSPACE, uap->fname, td);
19026f9a767SRodney W. Grimes 
19126f9a767SRodney W. Grimes interpret:
19226f9a767SRodney W. Grimes 
19326f9a767SRodney W. Grimes 	error = namei(ndp);
19426f9a767SRodney W. Grimes 	if (error) {
1951616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1961616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
19726f9a767SRodney W. Grimes 		goto exec_fail;
19826f9a767SRodney W. Grimes 	}
19926f9a767SRodney W. Grimes 
200c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
2019c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
20226f9a767SRodney W. Grimes 
20326f9a767SRodney W. Grimes 	/*
2049022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
2059022e4adSDavid Greenman 	 */
206c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
2078677f509SDavid Greenman 	if (error) {
208b40ce416SJulian Elischer 		VOP_UNLOCK(imgp->vp, 0, td);
20926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
2108677f509SDavid Greenman 	}
21126f9a767SRodney W. Grimes 
2121616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
213b40ce416SJulian Elischer 	VOP_UNLOCK(imgp->vp, 0, td);
2146d5a0a8cSDavid Greenman 	if (error)
21526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
21626f9a767SRodney W. Grimes 
21726f9a767SRodney W. Grimes 	/*
218d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
219d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
220d323ddf3SMatthew Dillon 	 *	scripts differently.
22126f9a767SRodney W. Grimes 	 */
222d323ddf3SMatthew Dillon 	error = -1;
223d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
224d323ddf3SMatthew Dillon 		error = img_first(imgp);
225d323ddf3SMatthew Dillon 
226d323ddf3SMatthew Dillon 	/*
227d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
228d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
229d323ddf3SMatthew Dillon 	 *	and an error otherwise.
230d323ddf3SMatthew Dillon 	 */
231d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
232d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
233d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
234d323ddf3SMatthew Dillon 			continue;
235d323ddf3SMatthew Dillon 		}
236c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
237d323ddf3SMatthew Dillon 	}
238d323ddf3SMatthew Dillon 
239d323ddf3SMatthew Dillon 	if (error) {
24026f9a767SRodney W. Grimes 		if (error == -1)
241d323ddf3SMatthew Dillon 			error = ENOEXEC;
24226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
243d323ddf3SMatthew Dillon 	}
244d323ddf3SMatthew Dillon 
245d323ddf3SMatthew Dillon 	/*
246d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
247d323ddf3SMatthew Dillon 	 * activate the interpreter.
248d323ddf3SMatthew Dillon 	 */
249c52007c2SDavid Greenman 	if (imgp->interpreted) {
2501616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
251762e6b85SEivind Eklund 		/* free name buffer and old vnode */
252762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
253f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
25426f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
255b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
256b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
25726f9a767SRodney W. Grimes 		goto interpret;
25826f9a767SRodney W. Grimes 	}
25926f9a767SRodney W. Grimes 
26026f9a767SRodney W. Grimes 	/*
26126f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
26226f9a767SRodney W. Grimes 	 */
263c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
26426f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
26526f9a767SRodney W. Grimes 
26626f9a767SRodney W. Grimes 	/*
2671e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2681e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2691e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
27026f9a767SRodney W. Grimes 	 */
2711e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
272c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2731e1e0b44SSøren Schmidt 	else
274c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
27526f9a767SRodney W. Grimes 
276a78e8d2aSDavid Greenman 	/*
277a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
278a78e8d2aSDavid Greenman 	 * be shared after an exec.
279a78e8d2aSDavid Greenman 	 */
280426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
281a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
282a78e8d2aSDavid Greenman 		struct filedesc *tmp;
283a78e8d2aSDavid Greenman 
284b40ce416SJulian Elischer 		tmp = fdcopy(td);
285426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
286b40ce416SJulian Elischer 		fdfree(td);
287a78e8d2aSDavid Greenman 		p->p_fd = tmp;
288426da3bcSAlfred Perlstein 	} else
289426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
290a78e8d2aSDavid Greenman 
291333ea485SGuido van Rooij 	/*
292333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
2938688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
294b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
295333ea485SGuido van Rooij 	 * reset.
296333ea485SGuido van Rooij 	 */
297333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
298333ea485SGuido van Rooij 		struct procsig *newprocsig;
299333ea485SGuido van Rooij 
300333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
301333ea485SGuido van Rooij 		       M_SUBPROC, M_WAITOK);
302333ea485SGuido van Rooij 		bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
303333ea485SGuido van Rooij 		p->p_procsig->ps_refcnt--;
304333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
305333ea485SGuido van Rooij 		p->p_procsig->ps_refcnt = 1;
306b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
307b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
308333ea485SGuido van Rooij 
309b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
310b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
311333ea485SGuido van Rooij 	}
312fdf4e8b3SWarner Losh 	/* Stop profiling */
313fdf4e8b3SWarner Losh 	stopprofclock(p);
314fdf4e8b3SWarner Losh 
31526f9a767SRodney W. Grimes 	/* close files on exec */
316b40ce416SJulian Elischer 	fdcloseexec(td);
31726f9a767SRodney W. Grimes 
31826f9a767SRodney W. Grimes 	/* reset caught signals */
31926f9a767SRodney W. Grimes 	execsigs(p);
32026f9a767SRodney W. Grimes 
32126f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
32226f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
32326f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
32426f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
32526f9a767SRodney W. Grimes 
32626f9a767SRodney W. Grimes 	/*
32724b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
328dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
32926f9a767SRodney W. Grimes 	 */
330e65897c3SJohn Baldwin 	PROC_LOCK(p);
33126f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
33226f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
33326f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
33426f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
33526f9a767SRodney W. Grimes 	}
33626f9a767SRodney W. Grimes 
33726f9a767SRodney W. Grimes 	/*
338a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
339a78e8d2aSDavid Greenman 	 *
340a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
341a78e8d2aSDavid Greenman 	 * the process is being traced.
342c52007c2SDavid Greenman 	 */
343b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
344b1fc0ec1SRobert Watson 	newcred = NULL;
345b1fc0ec1SRobert Watson 	if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) ||
346b1fc0ec1SRobert Watson 	     ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) &&
347a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
348c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
349e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
350c52007c2SDavid Greenman 		/*
351c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
352b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
353b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
35426f9a767SRodney W. Grimes 		 */
355b85db196SPeter Wemm 		setsugid(p);
356b1fc0ec1SRobert Watson 		if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) {
35779deba82SMatthew Dillon 			struct vnode *vtmp;
35879deba82SMatthew Dillon 
35979deba82SMatthew Dillon 			if ((vtmp = p->p_tracep) != NULL) {
360c52007c2SDavid Greenman 				p->p_tracep = NULL;
36179deba82SMatthew Dillon 				p->p_traceflag = 0;
36279deba82SMatthew Dillon 				vrele(vtmp);
36379deba82SMatthew Dillon 			}
36426f9a767SRodney W. Grimes 		}
365c52007c2SDavid Greenman 		/*
366c52007c2SDavid Greenman 		 * Set the new credentials.
367c52007c2SDavid Greenman 		 */
368b1fc0ec1SRobert Watson 		newcred = crdup(oldcred);
369c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
370b1fc0ec1SRobert Watson 			change_euid(newcred, attr.va_uid);
371c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
372b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
373b40ce416SJulian Elischer 		setugidsafety(td);
374c52007c2SDavid Greenman 	} else {
375b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
376b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
377c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
378e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
37926f9a767SRodney W. Grimes 	}
38026f9a767SRodney W. Grimes 
38126f9a767SRodney W. Grimes 	/*
382c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
383b1fc0ec1SRobert Watson 	 *
384b1fc0ec1SRobert Watson 	 * XXX: It's not clear that the existing behavior is
385b8c526dfSAlexander Langer 	 * POSIX-compliant.  A number of sources indicate that the saved
386b1fc0ec1SRobert Watson 	 * uid/gid should only be updated if the new ruid is not equal to
387b1fc0ec1SRobert Watson 	 * the old ruid, or the new euid is not equal to the old euid and
388b1fc0ec1SRobert Watson 	 * the new euid is not equal to the old ruid.  The FreeBSD code
389b1fc0ec1SRobert Watson 	 * always updates the saved uid/gid.  Also, this code uses the new
390b1fc0ec1SRobert Watson 	 * (replaced) euid and egid as the source, which may or may not be
391b1fc0ec1SRobert Watson 	 * the right ones to use.
39226f9a767SRodney W. Grimes 	 */
3937cb8e4d2SRobert Watson 	if (newcred == NULL) {
394b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
395b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
396b1fc0ec1SRobert Watson 			newcred = crdup(oldcred);
397b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
398b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
399b1fc0ec1SRobert Watson 		}
4007cb8e4d2SRobert Watson 	} else {
4017cb8e4d2SRobert Watson 		change_svuid(newcred, newcred->cr_uid);
4027cb8e4d2SRobert Watson 		change_svgid(newcred, newcred->cr_gid);
4037cb8e4d2SRobert Watson 	}
4047cb8e4d2SRobert Watson 
405b1fc0ec1SRobert Watson 	if (newcred != NULL) {
406b1fc0ec1SRobert Watson 		PROC_LOCK(p);
407b1fc0ec1SRobert Watson 		p->p_ucred = newcred;
408b1fc0ec1SRobert Watson 		PROC_UNLOCK(p);
409b1fc0ec1SRobert Watson 		crfree(oldcred);
410b1fc0ec1SRobert Watson 	}
41126f9a767SRodney W. Grimes 
41226f9a767SRodney W. Grimes 	/*
4132a531c80SDavid Greenman 	 * Store the vp for use in procfs
4142a531c80SDavid Greenman 	 */
4152a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
4162a531c80SDavid Greenman 		vrele(p->p_textvp);
4172a531c80SDavid Greenman 	VREF(ndp->ni_vp);
4182a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
4192a531c80SDavid Greenman 
4202a531c80SDavid Greenman 	/*
4219ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
4229ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
423cb679c38SJonathan Lemon 	 */
424e65897c3SJohn Baldwin 	PROC_LOCK(p);
425cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
4269ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
427cb679c38SJonathan Lemon 
428cb679c38SJonathan Lemon 	/*
42926f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
43026f9a767SRodney W. Grimes 	 * can be set before the program executes.
43126f9a767SRodney W. Grimes 	 */
432e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
4332a024a2bSSean Eric Fagan 
43426f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
43526f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
43626f9a767SRodney W. Grimes 
43726f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
43826f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
43926f9a767SRodney W. Grimes 
440b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
441fbd26f75SJohn Baldwin 	pa = p->p_args;
442b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
443fbd26f75SJohn Baldwin 	PROC_UNLOCK(p);
444fbd26f75SJohn Baldwin 	if (pa != NULL && --pa->ar_ref == 0)
445fbd26f75SJohn Baldwin 		FREE(pa, M_PARGS);
446b9df5231SPoul-Henning Kamp 
447e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
448e913ca22SDoug Rabson 	setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
449e913ca22SDoug Rabson 	    imgp->ps_strings);
450e913ca22SDoug Rabson 
451b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
452b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
453b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
454fbd26f75SJohn Baldwin 		MALLOC(pa, struct pargs *, sizeof(struct pargs) + i,
455b9df5231SPoul-Henning Kamp 		    M_PARGS, M_WAITOK);
456fbd26f75SJohn Baldwin 		pa->ar_ref = 1;
457fbd26f75SJohn Baldwin 		pa->ar_length = i;
458fbd26f75SJohn Baldwin 		bcopy(imgp->stringbase, pa->ar_args, i);
459e65897c3SJohn Baldwin 		PROC_LOCK(p);
460fbd26f75SJohn Baldwin 		p->p_args = pa;
461e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
462fbd26f75SJohn Baldwin 	}
463b9df5231SPoul-Henning Kamp 
4641616db3cSJohn Dyson exec_fail_dealloc:
4651616db3cSJohn Dyson 
46626f9a767SRodney W. Grimes 	/*
46726f9a767SRodney W. Grimes 	 * free various allocated resources
46826f9a767SRodney W. Grimes 	 */
4691616db3cSJohn Dyson 	if (imgp->firstpage)
4701616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
47126f9a767SRodney W. Grimes 
472c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
4731616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
4741616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
4751616db3cSJohn Dyson 
4769c0fed3dSDoug Rabson 	if (imgp->vp) {
477762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
4789c0fed3dSDoug Rabson 		vrele(imgp->vp);
479a3cf6ebaSDavid Greenman 	}
48026f9a767SRodney W. Grimes 
4811616db3cSJohn Dyson 	if (error == 0)
482116734c4SMatthew Dillon 		goto done2;
4831616db3cSJohn Dyson 
48426f9a767SRodney W. Grimes exec_fail:
4859ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
4869ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
4879ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
4889ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
4899ca45e81SDag-Erling Smørgrav 
490c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
49126f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
492b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
49326f9a767SRodney W. Grimes 		/* NOT REACHED */
494116734c4SMatthew Dillon 		error = 0;
49526f9a767SRodney W. Grimes 	}
496116734c4SMatthew Dillon done2:
497116734c4SMatthew Dillon 	mtx_unlock(&Giant);
498116734c4SMatthew Dillon 	return (error);
49926f9a767SRodney W. Grimes }
50026f9a767SRodney W. Grimes 
5011616db3cSJohn Dyson int
5021616db3cSJohn Dyson exec_map_first_page(imgp)
5031616db3cSJohn Dyson 	struct image_params *imgp;
5041616db3cSJohn Dyson {
505d8aad40cSJohn Baldwin 	int rv, i;
50695461b45SJohn Dyson 	int initial_pagein;
50795461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
5081616db3cSJohn Dyson 	vm_object_t object;
5091616db3cSJohn Dyson 
5100cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5111616db3cSJohn Dyson 
5121616db3cSJohn Dyson 	if (imgp->firstpage) {
5131616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
5141616db3cSJohn Dyson 	}
5151616db3cSJohn Dyson 
5169ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
5171616db3cSJohn Dyson 
51895461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
5191616db3cSJohn Dyson 
52095461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
52195461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
52295461b45SJohn Dyson 		if (initial_pagein > object->size)
52395461b45SJohn Dyson 			initial_pagein = object->size;
52495461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
525d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
52695461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
52795461b45SJohn Dyson 					break;
52895461b45SJohn Dyson 				if (ma[i]->valid)
52995461b45SJohn Dyson 					break;
530e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
53195461b45SJohn Dyson 			} else {
53295461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
53395461b45SJohn Dyson 				if (ma[i] == NULL)
53495461b45SJohn Dyson 					break;
53595461b45SJohn Dyson 			}
53695461b45SJohn Dyson 		}
53795461b45SJohn Dyson 		initial_pagein = i;
5381616db3cSJohn Dyson 
53995461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
54095461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
54195461b45SJohn Dyson 
542eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
543ecbb00a2SDoug Rabson 			if (ma[0]) {
54495461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
5458f9110f6SJohn Dyson 				vm_page_free(ma[0]);
546ecbb00a2SDoug Rabson 			}
5471616db3cSJohn Dyson 			return EIO;
5481616db3cSJohn Dyson 		}
5491616db3cSJohn Dyson 	}
5501616db3cSJohn Dyson 
55195461b45SJohn Dyson 	vm_page_wire(ma[0]);
552e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
5531616db3cSJohn Dyson 
554d1693e17SPeter Wemm 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
55595461b45SJohn Dyson 	imgp->firstpage = ma[0];
5561616db3cSJohn Dyson 
5571616db3cSJohn Dyson 	return 0;
5581616db3cSJohn Dyson }
5591616db3cSJohn Dyson 
5601616db3cSJohn Dyson void
5611616db3cSJohn Dyson exec_unmap_first_page(imgp)
5621616db3cSJohn Dyson 	struct image_params *imgp;
5631616db3cSJohn Dyson {
5640cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
56523955314SAlfred Perlstein 
5661616db3cSJohn Dyson 	if (imgp->firstpage) {
567d1693e17SPeter Wemm 		pmap_kremove((vm_offset_t) imgp->image_header);
56873007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
5691616db3cSJohn Dyson 		imgp->firstpage = NULL;
5701616db3cSJohn Dyson 	}
5711616db3cSJohn Dyson }
5721616db3cSJohn Dyson 
57326f9a767SRodney W. Grimes /*
57426f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
57526f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
57626f9a767SRodney W. Grimes  *	automatically in trap.c.
57726f9a767SRodney W. Grimes  */
57826f9a767SRodney W. Grimes int
579c52007c2SDavid Greenman exec_new_vmspace(imgp)
580c52007c2SDavid Greenman 	struct image_params *imgp;
58126f9a767SRodney W. Grimes {
58226f9a767SRodney W. Grimes 	int error;
5836f5dafeaSAlan Cox 	struct execlist *ep;
584c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
5859a024fc5SRobert Drehmel 	vm_offset_t stack_addr = USRSTACK - maxssiz;
586af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
58726f9a767SRodney W. Grimes 
5880cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5890cddd8f0SMatthew Dillon 
590c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
59126f9a767SRodney W. Grimes 
592af9ec885SJohn Dyson 	/*
5936f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
5946f5dafeaSAlan Cox 	 */
5956f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
5966f5dafeaSAlan Cox 		(*ep->function)(imgp->proc);
5976f5dafeaSAlan Cox 
5986f5dafeaSAlan Cox 	/*
599af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
600af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
601af9ec885SJohn Dyson 	 * not disrupted
602af9ec885SJohn Dyson 	 */
603af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
6043d903220SDoug Rabson 		if (vmspace->vm_shm)
605c52007c2SDavid Greenman 			shmexit(imgp->proc);
606b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
6079c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
608af9ec885SJohn Dyson 	} else {
609492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
610492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
611af9ec885SJohn Dyson 		map = &vmspace->vm_map;
612af9ec885SJohn Dyson 	}
61326f9a767SRodney W. Grimes 
61426f9a767SRodney W. Grimes 	/* Allocate a new stack */
6159a024fc5SRobert Drehmel 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
6169a024fc5SRobert Drehmel 	    VM_PROT_ALL, VM_PROT_ALL, 0);
6172267af78SJulian Elischer 	if (error)
6182267af78SJulian Elischer 		return (error);
6192267af78SJulian Elischer 
62063c47a5cSDoug Rabson #ifdef __ia64__
62163c47a5cSDoug Rabson 	{
62263c47a5cSDoug Rabson 		/*
62363c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
62463c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
62563c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
62663c47a5cSDoug Rabson 		 */
62763c47a5cSDoug Rabson 		vm_offset_t bsaddr;
628cbc89bfbSPaul Saab 		bsaddr = USRSTACK - 2*maxssiz;
62963c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
63063c47a5cSDoug Rabson 				    4*PAGE_SIZE, 0,
63163c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
632079b7badSJulian Elischer 		FIRST_THREAD_IN_PROC(imgp->proc)->td_md.md_bspstore = bsaddr;
63363c47a5cSDoug Rabson 	}
63463c47a5cSDoug Rabson #endif
63563c47a5cSDoug Rabson 
6362267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
6372267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
6382267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
6392267af78SJulian Elischer 	 */
640cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
641cbc89bfbSPaul Saab 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
64226f9a767SRodney W. Grimes 
64326f9a767SRodney W. Grimes 	return(0);
64426f9a767SRodney W. Grimes }
64526f9a767SRodney W. Grimes 
64626f9a767SRodney W. Grimes /*
64726f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
64826f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
64926f9a767SRodney W. Grimes  */
65026f9a767SRodney W. Grimes int
651c52007c2SDavid Greenman exec_extract_strings(imgp)
652c52007c2SDavid Greenman 	struct image_params *imgp;
65326f9a767SRodney W. Grimes {
65426f9a767SRodney W. Grimes 	char	**argv, **envv;
65526f9a767SRodney W. Grimes 	char	*argp, *envp;
656ecbb00a2SDoug Rabson 	int	error;
657ecbb00a2SDoug Rabson 	size_t	length;
65826f9a767SRodney W. Grimes 
65926f9a767SRodney W. Grimes 	/*
66026f9a767SRodney W. Grimes 	 * extract arguments first
66126f9a767SRodney W. Grimes 	 */
66226f9a767SRodney W. Grimes 
663c52007c2SDavid Greenman 	argv = imgp->uap->argv;
66426f9a767SRodney W. Grimes 
6650fd1a014SDavid Greenman 	if (argv) {
666aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
6675cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
6685cf3d12cSAndrey A. Chernov 			return (EFAULT);
6695cf3d12cSAndrey A. Chernov 		if (argp)
6705cf3d12cSAndrey A. Chernov 			argv++;
6715cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
6725cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
6735cf3d12cSAndrey A. Chernov 		if (argp) {
6745cf3d12cSAndrey A. Chernov 			do {
67526f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
67626f9a767SRodney W. Grimes 					return (EFAULT);
677c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
678c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
6790fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
68026f9a767SRodney W. Grimes 						return(E2BIG);
6810fd1a014SDavid Greenman 					return (error);
6820fd1a014SDavid Greenman 				}
683c52007c2SDavid Greenman 				imgp->stringspace -= length;
684c52007c2SDavid Greenman 				imgp->stringp += length;
685c52007c2SDavid Greenman 				imgp->argc++;
686aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
68726f9a767SRodney W. Grimes 		}
6880fd1a014SDavid Greenman 	}
68926f9a767SRodney W. Grimes 
690b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
691b9df5231SPoul-Henning Kamp 
69226f9a767SRodney W. Grimes 	/*
69326f9a767SRodney W. Grimes 	 * extract environment strings
69426f9a767SRodney W. Grimes 	 */
69526f9a767SRodney W. Grimes 
696c52007c2SDavid Greenman 	envv = imgp->uap->envv;
69726f9a767SRodney W. Grimes 
6980fd1a014SDavid Greenman 	if (envv) {
699aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
70026f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
70126f9a767SRodney W. Grimes 				return (EFAULT);
702c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
703c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
7040fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
70526f9a767SRodney W. Grimes 					return(E2BIG);
7060fd1a014SDavid Greenman 				return (error);
7070fd1a014SDavid Greenman 			}
708c52007c2SDavid Greenman 			imgp->stringspace -= length;
709c52007c2SDavid Greenman 			imgp->stringp += length;
710c52007c2SDavid Greenman 			imgp->envc++;
71126f9a767SRodney W. Grimes 		}
7120fd1a014SDavid Greenman 	}
71326f9a767SRodney W. Grimes 
71426f9a767SRodney W. Grimes 	return (0);
71526f9a767SRodney W. Grimes }
71626f9a767SRodney W. Grimes 
71726f9a767SRodney W. Grimes /*
71826f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
71926f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
72026f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
72126f9a767SRodney W. Grimes  */
722654f6be1SBruce Evans register_t *
723c52007c2SDavid Greenman exec_copyout_strings(imgp)
724c52007c2SDavid Greenman 	struct image_params *imgp;
72526f9a767SRodney W. Grimes {
72626f9a767SRodney W. Grimes 	int argc, envc;
72726f9a767SRodney W. Grimes 	char **vectp;
72826f9a767SRodney W. Grimes 	char *stringp, *destp;
729654f6be1SBruce Evans 	register_t *stack_base;
73093f6448cSDavid Greenman 	struct ps_strings *arginfo;
731d66a5066SPeter Wemm 	int szsigcode;
73226f9a767SRodney W. Grimes 
73326f9a767SRodney W. Grimes 	/*
73426f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
735d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
73626f9a767SRodney W. Grimes 	 */
7374c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
738d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
739d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
7401ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
7411ed012f9SPeter Wemm 
74226f9a767SRodney W. Grimes 	/*
743d66a5066SPeter Wemm 	 * install sigcode
744d66a5066SPeter Wemm 	 */
745d66a5066SPeter Wemm 	if (szsigcode)
746d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
747d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
748d66a5066SPeter Wemm 
749d66a5066SPeter Wemm 	/*
750e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
751e1743d02SSøren Schmidt 	 * on the stack.
752e1743d02SSøren Schmidt 	 */
753b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
754e1743d02SSøren Schmidt 		/*
755b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
756b9a22da4STakanori Watanabe 		 * lower compatibility.
757b9a22da4STakanori Watanabe 		 */
758b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
759b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
760b9a22da4STakanori Watanabe 		/*
761b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
762b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
763b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
764e1743d02SSøren Schmidt 		 */
765e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
766b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
767b9a22da4STakanori Watanabe 
768b9a22da4STakanori Watanabe 	} else
769e1743d02SSøren Schmidt 		/*
770b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
771b9a22da4STakanori Watanabe 		 * the arg and env vector sets
77226f9a767SRodney W. Grimes 		 */
773e1743d02SSøren Schmidt 		vectp = (char **)
774e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
77526f9a767SRodney W. Grimes 
77626f9a767SRodney W. Grimes 	/*
77726f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
77826f9a767SRodney W. Grimes 	 */
779654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
78026f9a767SRodney W. Grimes 
781c52007c2SDavid Greenman 	stringp = imgp->stringbase;
782c52007c2SDavid Greenman 	argc = imgp->argc;
783c52007c2SDavid Greenman 	envc = imgp->envc;
78426f9a767SRodney W. Grimes 
78593f6448cSDavid Greenman 	/*
7863aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
78793f6448cSDavid Greenman 	 */
788c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
78993f6448cSDavid Greenman 
79093f6448cSDavid Greenman 	/*
7913aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
7923aed948bSDavid Greenman 	 */
793aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
7943aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
7953aed948bSDavid Greenman 
7963aed948bSDavid Greenman 	/*
7973aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
79893f6448cSDavid Greenman 	 */
79926f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
800aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
8013aed948bSDavid Greenman 		while (*stringp++ != 0)
8023aed948bSDavid Greenman 			destp++;
8033aed948bSDavid Greenman 		destp++;
80426f9a767SRodney W. Grimes 	}
80526f9a767SRodney W. Grimes 
8061a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
8076ab46d52SBruce Evans 	suword(vectp++, 0);
80826f9a767SRodney W. Grimes 
809aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
8103aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
81193f6448cSDavid Greenman 
81293f6448cSDavid Greenman 	/*
8133aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
81493f6448cSDavid Greenman 	 */
81526f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
816aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
8173aed948bSDavid Greenman 		while (*stringp++ != 0)
8183aed948bSDavid Greenman 			destp++;
8193aed948bSDavid Greenman 		destp++;
82026f9a767SRodney W. Grimes 	}
82126f9a767SRodney W. Grimes 
82226f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
8236ab46d52SBruce Evans 	suword(vectp, 0);
82426f9a767SRodney W. Grimes 
82526f9a767SRodney W. Grimes 	return (stack_base);
82626f9a767SRodney W. Grimes }
82726f9a767SRodney W. Grimes 
82826f9a767SRodney W. Grimes /*
82926f9a767SRodney W. Grimes  * Check permissions of file to execute.
830cf64863aSRobert Watson  *	Called with imgp->vp locked.
83126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
83226f9a767SRodney W. Grimes  */
833c8a79999SPeter Wemm int
834c52007c2SDavid Greenman exec_check_permissions(imgp)
835c52007c2SDavid Greenman 	struct image_params *imgp;
83626f9a767SRodney W. Grimes {
837c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
838c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
839c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
840a854ed98SJohn Baldwin 	struct thread *td;
84126f9a767SRodney W. Grimes 	int error;
84226f9a767SRodney W. Grimes 
843a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
84426f9a767SRodney W. Grimes 	/* Get file attributes */
845a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
84626f9a767SRodney W. Grimes 	if (error)
84726f9a767SRodney W. Grimes 		return (error);
84826f9a767SRodney W. Grimes 
84926f9a767SRodney W. Grimes 	/*
85026f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
85126f9a767SRodney W. Grimes 	 *	file resides on.
85226f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
85326f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
85426f9a767SRodney W. Grimes 	 *	file really is executable.
85526f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
85626f9a767SRodney W. Grimes 	 */
857c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
85826f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
859a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
86026f9a767SRodney W. Grimes 		return (EACCES);
86126f9a767SRodney W. Grimes 
86226f9a767SRodney W. Grimes 	/*
86326f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
86426f9a767SRodney W. Grimes 	 */
86526f9a767SRodney W. Grimes 	if (attr->va_size == 0)
86626f9a767SRodney W. Grimes 		return (ENOEXEC);
86726f9a767SRodney W. Grimes 
86826f9a767SRodney W. Grimes 	/*
86926f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
87026f9a767SRodney W. Grimes 	 */
871a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
87226f9a767SRodney W. Grimes 	if (error)
87326f9a767SRodney W. Grimes 		return (error);
87426f9a767SRodney W. Grimes 
8756d5a0a8cSDavid Greenman 	/*
8766d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
8776d5a0a8cSDavid Greenman 	 * if there are any.
8786d5a0a8cSDavid Greenman 	 */
8796d5a0a8cSDavid Greenman 	if (vp->v_writecount)
8806d5a0a8cSDavid Greenman 		return (ETXTBSY);
8816d5a0a8cSDavid Greenman 
8826d5a0a8cSDavid Greenman 	/*
8836d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
8846d5a0a8cSDavid Greenman 	 * general case).
8856d5a0a8cSDavid Greenman 	 */
886a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
88726f9a767SRodney W. Grimes 	return (error);
888df8bae1dSRodney W. Grimes }
889aa855a59SPeter Wemm 
890aa855a59SPeter Wemm /*
891aa855a59SPeter Wemm  * Exec handler registration
892aa855a59SPeter Wemm  */
893aa855a59SPeter Wemm int
894aa855a59SPeter Wemm exec_register(execsw_arg)
895aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
896aa855a59SPeter Wemm {
897aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
898aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
899aa855a59SPeter Wemm 
900aa855a59SPeter Wemm 	if (execsw)
901aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
902aa855a59SPeter Wemm 			count++;
903aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
904aa855a59SPeter Wemm 	if (newexecsw == NULL)
905aa855a59SPeter Wemm 		return ENOMEM;
906aa855a59SPeter Wemm 	xs = newexecsw;
907aa855a59SPeter Wemm 	if (execsw)
908aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
909aa855a59SPeter Wemm 			*xs++ = *es;
910aa855a59SPeter Wemm 	*xs++ = execsw_arg;
911aa855a59SPeter Wemm 	*xs = NULL;
912aa855a59SPeter Wemm 	if (execsw)
913aa855a59SPeter Wemm 		free(execsw, M_TEMP);
914aa855a59SPeter Wemm 	execsw = newexecsw;
915aa855a59SPeter Wemm 	return 0;
916aa855a59SPeter Wemm }
917aa855a59SPeter Wemm 
918aa855a59SPeter Wemm int
919aa855a59SPeter Wemm exec_unregister(execsw_arg)
920aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
921aa855a59SPeter Wemm {
922aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
923aa855a59SPeter Wemm 	int count = 1;
924aa855a59SPeter Wemm 
925aa855a59SPeter Wemm 	if (execsw == NULL)
926aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
927aa855a59SPeter Wemm 
928aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
929aa855a59SPeter Wemm 		if (*es == execsw_arg)
930aa855a59SPeter Wemm 			break;
931aa855a59SPeter Wemm 	}
932aa855a59SPeter Wemm 	if (*es == NULL)
933aa855a59SPeter Wemm 		return ENOENT;
934aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
935aa855a59SPeter Wemm 		if (*es != execsw_arg)
936aa855a59SPeter Wemm 			count++;
937aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
938aa855a59SPeter Wemm 	if (newexecsw == NULL)
939aa855a59SPeter Wemm 		return ENOMEM;
940aa855a59SPeter Wemm 	xs = newexecsw;
941aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
942aa855a59SPeter Wemm 		if (*es != execsw_arg)
943aa855a59SPeter Wemm 			*xs++ = *es;
944aa855a59SPeter Wemm 	*xs = NULL;
945aa855a59SPeter Wemm 	if (execsw)
946aa855a59SPeter Wemm 		free(execsw, M_TEMP);
947aa855a59SPeter Wemm 	execsw = newexecsw;
948aa855a59SPeter Wemm 	return 0;
949aa855a59SPeter Wemm }
95021d56e9cSAlfred Perlstein 
95121d56e9cSAlfred Perlstein int
95221d56e9cSAlfred Perlstein at_exec(function)
95321d56e9cSAlfred Perlstein 	execlist_fn function;
95421d56e9cSAlfred Perlstein {
95521d56e9cSAlfred Perlstein 	struct execlist *ep;
95621d56e9cSAlfred Perlstein 
95721d56e9cSAlfred Perlstein #ifdef INVARIANTS
95821d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
95921d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
96021d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
96121d56e9cSAlfred Perlstein 		    function);
96221d56e9cSAlfred Perlstein #endif
96321d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
96421d56e9cSAlfred Perlstein 	if (ep == NULL)
96521d56e9cSAlfred Perlstein 		return (ENOMEM);
96621d56e9cSAlfred Perlstein 	ep->function = function;
96721d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
96821d56e9cSAlfred Perlstein 	return (0);
96921d56e9cSAlfred Perlstein }
97021d56e9cSAlfred Perlstein 
97121d56e9cSAlfred Perlstein /*
97221d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
97321d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
97421d56e9cSAlfred Perlstein  */
97521d56e9cSAlfred Perlstein int
97621d56e9cSAlfred Perlstein rm_at_exec(function)
97721d56e9cSAlfred Perlstein 	execlist_fn function;
97821d56e9cSAlfred Perlstein {
97921d56e9cSAlfred Perlstein 	struct execlist *ep;
98021d56e9cSAlfred Perlstein 
98121d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
98221d56e9cSAlfred Perlstein 		if (ep->function == function) {
98321d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
98421d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
98521d56e9cSAlfred Perlstein 			return(1);
98621d56e9cSAlfred Perlstein 		}
98721d56e9cSAlfred Perlstein 	}
98821d56e9cSAlfred Perlstein 	return (0);
98921d56e9cSAlfred Perlstein }
99021d56e9cSAlfred Perlstein 
991