xref: /freebsd/sys/kern/kern_exec.c (revision 5e20c11f19ffef3d123e34fb1b740e39e1c1f4d3)
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 
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;
1294d77a549SAlfred Perlstein 	int (*img_first)(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);
4448899023fSAlfred Perlstein 	pargs_drop(pa);
445b9df5231SPoul-Henning Kamp 
446e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
447e913ca22SDoug Rabson 	setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
448e913ca22SDoug Rabson 	    imgp->ps_strings);
449e913ca22SDoug Rabson 
450b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
451b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
452b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
4538899023fSAlfred Perlstein 		pa = pargs_alloc(i);
454fbd26f75SJohn Baldwin 		bcopy(imgp->stringbase, pa->ar_args, i);
455e65897c3SJohn Baldwin 		PROC_LOCK(p);
456fbd26f75SJohn Baldwin 		p->p_args = pa;
457e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
458fbd26f75SJohn Baldwin 	}
459b9df5231SPoul-Henning Kamp 
4601616db3cSJohn Dyson exec_fail_dealloc:
4611616db3cSJohn Dyson 
46226f9a767SRodney W. Grimes 	/*
46326f9a767SRodney W. Grimes 	 * free various allocated resources
46426f9a767SRodney W. Grimes 	 */
4651616db3cSJohn Dyson 	if (imgp->firstpage)
4661616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
46726f9a767SRodney W. Grimes 
468c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
4691616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
4701616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
4711616db3cSJohn Dyson 
4729c0fed3dSDoug Rabson 	if (imgp->vp) {
473762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
4749c0fed3dSDoug Rabson 		vrele(imgp->vp);
475a3cf6ebaSDavid Greenman 	}
47626f9a767SRodney W. Grimes 
4771616db3cSJohn Dyson 	if (error == 0)
478116734c4SMatthew Dillon 		goto done2;
4791616db3cSJohn Dyson 
48026f9a767SRodney W. Grimes exec_fail:
4819ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
4829ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
4839ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
4849ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
4859ca45e81SDag-Erling Smørgrav 
486c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
48726f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
488b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
48926f9a767SRodney W. Grimes 		/* NOT REACHED */
490116734c4SMatthew Dillon 		error = 0;
49126f9a767SRodney W. Grimes 	}
492116734c4SMatthew Dillon done2:
493116734c4SMatthew Dillon 	mtx_unlock(&Giant);
494116734c4SMatthew Dillon 	return (error);
49526f9a767SRodney W. Grimes }
49626f9a767SRodney W. Grimes 
4971616db3cSJohn Dyson int
4981616db3cSJohn Dyson exec_map_first_page(imgp)
4991616db3cSJohn Dyson 	struct image_params *imgp;
5001616db3cSJohn Dyson {
501d8aad40cSJohn Baldwin 	int rv, i;
50295461b45SJohn Dyson 	int initial_pagein;
50395461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
5041616db3cSJohn Dyson 	vm_object_t object;
5051616db3cSJohn Dyson 
5060cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5071616db3cSJohn Dyson 
5081616db3cSJohn Dyson 	if (imgp->firstpage) {
5091616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
5101616db3cSJohn Dyson 	}
5111616db3cSJohn Dyson 
5129ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
5131616db3cSJohn Dyson 
51495461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
5151616db3cSJohn Dyson 
51695461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
51795461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
51895461b45SJohn Dyson 		if (initial_pagein > object->size)
51995461b45SJohn Dyson 			initial_pagein = object->size;
52095461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
521d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
52295461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
52395461b45SJohn Dyson 					break;
52495461b45SJohn Dyson 				if (ma[i]->valid)
52595461b45SJohn Dyson 					break;
526e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
52795461b45SJohn Dyson 			} else {
52895461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
52995461b45SJohn Dyson 				if (ma[i] == NULL)
53095461b45SJohn Dyson 					break;
53195461b45SJohn Dyson 			}
53295461b45SJohn Dyson 		}
53395461b45SJohn Dyson 		initial_pagein = i;
5341616db3cSJohn Dyson 
53595461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
53695461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
53795461b45SJohn Dyson 
538eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
539ecbb00a2SDoug Rabson 			if (ma[0]) {
54095461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
5418f9110f6SJohn Dyson 				vm_page_free(ma[0]);
542ecbb00a2SDoug Rabson 			}
5431616db3cSJohn Dyson 			return EIO;
5441616db3cSJohn Dyson 		}
5451616db3cSJohn Dyson 	}
5461616db3cSJohn Dyson 
54795461b45SJohn Dyson 	vm_page_wire(ma[0]);
548e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
5491616db3cSJohn Dyson 
550ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
55195461b45SJohn Dyson 	imgp->firstpage = ma[0];
5521616db3cSJohn Dyson 
5531616db3cSJohn Dyson 	return 0;
5541616db3cSJohn Dyson }
5551616db3cSJohn Dyson 
5561616db3cSJohn Dyson void
5571616db3cSJohn Dyson exec_unmap_first_page(imgp)
5581616db3cSJohn Dyson 	struct image_params *imgp;
5591616db3cSJohn Dyson {
5600cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
56123955314SAlfred Perlstein 
5621616db3cSJohn Dyson 	if (imgp->firstpage) {
563ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
56473007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
5651616db3cSJohn Dyson 		imgp->firstpage = NULL;
5661616db3cSJohn Dyson 	}
5671616db3cSJohn Dyson }
5681616db3cSJohn Dyson 
56926f9a767SRodney W. Grimes /*
57026f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
57126f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
57226f9a767SRodney W. Grimes  *	automatically in trap.c.
57326f9a767SRodney W. Grimes  */
57426f9a767SRodney W. Grimes int
575c52007c2SDavid Greenman exec_new_vmspace(imgp)
576c52007c2SDavid Greenman 	struct image_params *imgp;
57726f9a767SRodney W. Grimes {
57826f9a767SRodney W. Grimes 	int error;
5796f5dafeaSAlan Cox 	struct execlist *ep;
5805e20c11fSAlan Cox 	struct proc *p = imgp->proc;
5815e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
5829a024fc5SRobert Drehmel 	vm_offset_t stack_addr = USRSTACK - maxssiz;
58326f9a767SRodney W. Grimes 
5840cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5850cddd8f0SMatthew Dillon 
586c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
58726f9a767SRodney W. Grimes 
588af9ec885SJohn Dyson 	/*
5896f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
5906f5dafeaSAlan Cox 	 */
5916f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
5925e20c11fSAlan Cox 		(*ep->function)(p);
5936f5dafeaSAlan Cox 
5946f5dafeaSAlan Cox 	/*
595af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
596af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
597af9ec885SJohn Dyson 	 * not disrupted
598af9ec885SJohn Dyson 	 */
599af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
6003d903220SDoug Rabson 		if (vmspace->vm_shm)
6015e20c11fSAlan Cox 			shmexit(p);
602b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
603cb100b25SAlan Cox 		vm_map_remove(&vmspace->vm_map, 0, VM_MAXUSER_ADDRESS);
604af9ec885SJohn Dyson 	} else {
6055e20c11fSAlan Cox 		vmspace_exec(p);
6065e20c11fSAlan Cox 		vmspace = p->p_vmspace;
607af9ec885SJohn Dyson 	}
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes 	/* Allocate a new stack */
6109a024fc5SRobert Drehmel 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
6119a024fc5SRobert Drehmel 	    VM_PROT_ALL, VM_PROT_ALL, 0);
6122267af78SJulian Elischer 	if (error)
6132267af78SJulian Elischer 		return (error);
6142267af78SJulian Elischer 
61563c47a5cSDoug Rabson #ifdef __ia64__
61663c47a5cSDoug Rabson 	{
61763c47a5cSDoug Rabson 		/*
61863c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
61963c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
62063c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
62163c47a5cSDoug Rabson 		 */
62263c47a5cSDoug Rabson 		vm_offset_t bsaddr;
623cbc89bfbSPaul Saab 		bsaddr = USRSTACK - 2*maxssiz;
62463c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
62563c47a5cSDoug Rabson 				    4*PAGE_SIZE, 0,
62663c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
6275e20c11fSAlan Cox 		FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
62863c47a5cSDoug Rabson 	}
62963c47a5cSDoug Rabson #endif
63063c47a5cSDoug Rabson 
6312267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
6322267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
6332267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
6342267af78SJulian Elischer 	 */
635cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
636cbc89bfbSPaul Saab 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes 	return(0);
63926f9a767SRodney W. Grimes }
64026f9a767SRodney W. Grimes 
64126f9a767SRodney W. Grimes /*
64226f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
64326f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
64426f9a767SRodney W. Grimes  */
64526f9a767SRodney W. Grimes int
646c52007c2SDavid Greenman exec_extract_strings(imgp)
647c52007c2SDavid Greenman 	struct image_params *imgp;
64826f9a767SRodney W. Grimes {
64926f9a767SRodney W. Grimes 	char	**argv, **envv;
65026f9a767SRodney W. Grimes 	char	*argp, *envp;
651ecbb00a2SDoug Rabson 	int	error;
652ecbb00a2SDoug Rabson 	size_t	length;
65326f9a767SRodney W. Grimes 
65426f9a767SRodney W. Grimes 	/*
65526f9a767SRodney W. Grimes 	 * extract arguments first
65626f9a767SRodney W. Grimes 	 */
65726f9a767SRodney W. Grimes 
658c52007c2SDavid Greenman 	argv = imgp->uap->argv;
65926f9a767SRodney W. Grimes 
6600fd1a014SDavid Greenman 	if (argv) {
661aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
6625cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
6635cf3d12cSAndrey A. Chernov 			return (EFAULT);
6645cf3d12cSAndrey A. Chernov 		if (argp)
6655cf3d12cSAndrey A. Chernov 			argv++;
6665cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
6675cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
6685cf3d12cSAndrey A. Chernov 		if (argp) {
6695cf3d12cSAndrey A. Chernov 			do {
67026f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
67126f9a767SRodney W. Grimes 					return (EFAULT);
672c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
673c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
6740fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
67526f9a767SRodney W. Grimes 						return(E2BIG);
6760fd1a014SDavid Greenman 					return (error);
6770fd1a014SDavid Greenman 				}
678c52007c2SDavid Greenman 				imgp->stringspace -= length;
679c52007c2SDavid Greenman 				imgp->stringp += length;
680c52007c2SDavid Greenman 				imgp->argc++;
681aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
68226f9a767SRodney W. Grimes 		}
6830fd1a014SDavid Greenman 	}
68426f9a767SRodney W. Grimes 
685b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
686b9df5231SPoul-Henning Kamp 
68726f9a767SRodney W. Grimes 	/*
68826f9a767SRodney W. Grimes 	 * extract environment strings
68926f9a767SRodney W. Grimes 	 */
69026f9a767SRodney W. Grimes 
691c52007c2SDavid Greenman 	envv = imgp->uap->envv;
69226f9a767SRodney W. Grimes 
6930fd1a014SDavid Greenman 	if (envv) {
694aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
69526f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
69626f9a767SRodney W. Grimes 				return (EFAULT);
697c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
698c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
6990fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
70026f9a767SRodney W. Grimes 					return(E2BIG);
7010fd1a014SDavid Greenman 				return (error);
7020fd1a014SDavid Greenman 			}
703c52007c2SDavid Greenman 			imgp->stringspace -= length;
704c52007c2SDavid Greenman 			imgp->stringp += length;
705c52007c2SDavid Greenman 			imgp->envc++;
70626f9a767SRodney W. Grimes 		}
7070fd1a014SDavid Greenman 	}
70826f9a767SRodney W. Grimes 
70926f9a767SRodney W. Grimes 	return (0);
71026f9a767SRodney W. Grimes }
71126f9a767SRodney W. Grimes 
71226f9a767SRodney W. Grimes /*
71326f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
71426f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
71526f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
71626f9a767SRodney W. Grimes  */
717654f6be1SBruce Evans register_t *
718c52007c2SDavid Greenman exec_copyout_strings(imgp)
719c52007c2SDavid Greenman 	struct image_params *imgp;
72026f9a767SRodney W. Grimes {
72126f9a767SRodney W. Grimes 	int argc, envc;
72226f9a767SRodney W. Grimes 	char **vectp;
72326f9a767SRodney W. Grimes 	char *stringp, *destp;
724654f6be1SBruce Evans 	register_t *stack_base;
72593f6448cSDavid Greenman 	struct ps_strings *arginfo;
726d66a5066SPeter Wemm 	int szsigcode;
72726f9a767SRodney W. Grimes 
72826f9a767SRodney W. Grimes 	/*
72926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
730d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
73126f9a767SRodney W. Grimes 	 */
7324c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
733d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
734d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
7351ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
7361ed012f9SPeter Wemm 
73726f9a767SRodney W. Grimes 	/*
738d66a5066SPeter Wemm 	 * install sigcode
739d66a5066SPeter Wemm 	 */
740d66a5066SPeter Wemm 	if (szsigcode)
741d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
742d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
743d66a5066SPeter Wemm 
744d66a5066SPeter Wemm 	/*
745e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
746e1743d02SSøren Schmidt 	 * on the stack.
747e1743d02SSøren Schmidt 	 */
748b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
749e1743d02SSøren Schmidt 		/*
750b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
751b9a22da4STakanori Watanabe 		 * lower compatibility.
752b9a22da4STakanori Watanabe 		 */
753b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
754b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
755b9a22da4STakanori Watanabe 		/*
756b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
757b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
758b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
759e1743d02SSøren Schmidt 		 */
760e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
761b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
762b9a22da4STakanori Watanabe 
763b9a22da4STakanori Watanabe 	} else
764e1743d02SSøren Schmidt 		/*
765b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
766b9a22da4STakanori Watanabe 		 * the arg and env vector sets
76726f9a767SRodney W. Grimes 		 */
768e1743d02SSøren Schmidt 		vectp = (char **)
769e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
77026f9a767SRodney W. Grimes 
77126f9a767SRodney W. Grimes 	/*
77226f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
77326f9a767SRodney W. Grimes 	 */
774654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
77526f9a767SRodney W. Grimes 
776c52007c2SDavid Greenman 	stringp = imgp->stringbase;
777c52007c2SDavid Greenman 	argc = imgp->argc;
778c52007c2SDavid Greenman 	envc = imgp->envc;
77926f9a767SRodney W. Grimes 
78093f6448cSDavid Greenman 	/*
7813aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
78293f6448cSDavid Greenman 	 */
783c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
78493f6448cSDavid Greenman 
78593f6448cSDavid Greenman 	/*
7863aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
7873aed948bSDavid Greenman 	 */
788aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
7893aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
7903aed948bSDavid Greenman 
7913aed948bSDavid Greenman 	/*
7923aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
79393f6448cSDavid Greenman 	 */
79426f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
795aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
7963aed948bSDavid Greenman 		while (*stringp++ != 0)
7973aed948bSDavid Greenman 			destp++;
7983aed948bSDavid Greenman 		destp++;
79926f9a767SRodney W. Grimes 	}
80026f9a767SRodney W. Grimes 
8011a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
8026ab46d52SBruce Evans 	suword(vectp++, 0);
80326f9a767SRodney W. Grimes 
804aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
8053aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
80693f6448cSDavid Greenman 
80793f6448cSDavid Greenman 	/*
8083aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
80993f6448cSDavid Greenman 	 */
81026f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
811aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
8123aed948bSDavid Greenman 		while (*stringp++ != 0)
8133aed948bSDavid Greenman 			destp++;
8143aed948bSDavid Greenman 		destp++;
81526f9a767SRodney W. Grimes 	}
81626f9a767SRodney W. Grimes 
81726f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
8186ab46d52SBruce Evans 	suword(vectp, 0);
81926f9a767SRodney W. Grimes 
82026f9a767SRodney W. Grimes 	return (stack_base);
82126f9a767SRodney W. Grimes }
82226f9a767SRodney W. Grimes 
82326f9a767SRodney W. Grimes /*
82426f9a767SRodney W. Grimes  * Check permissions of file to execute.
825cf64863aSRobert Watson  *	Called with imgp->vp locked.
82626f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
82726f9a767SRodney W. Grimes  */
828c8a79999SPeter Wemm int
829c52007c2SDavid Greenman exec_check_permissions(imgp)
830c52007c2SDavid Greenman 	struct image_params *imgp;
83126f9a767SRodney W. Grimes {
832c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
833c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
834a854ed98SJohn Baldwin 	struct thread *td;
83526f9a767SRodney W. Grimes 	int error;
83626f9a767SRodney W. Grimes 
837a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
83826f9a767SRodney W. Grimes 	/* Get file attributes */
839a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
84026f9a767SRodney W. Grimes 	if (error)
84126f9a767SRodney W. Grimes 		return (error);
84226f9a767SRodney W. Grimes 
84326f9a767SRodney W. Grimes 	/*
84426f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
84526f9a767SRodney W. Grimes 	 *	file resides on.
84626f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
84726f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
84826f9a767SRodney W. Grimes 	 *	file really is executable.
84926f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
85026f9a767SRodney W. Grimes 	 */
851c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
85226f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
853a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
85426f9a767SRodney W. Grimes 		return (EACCES);
85526f9a767SRodney W. Grimes 
85626f9a767SRodney W. Grimes 	/*
85726f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
85826f9a767SRodney W. Grimes 	 */
85926f9a767SRodney W. Grimes 	if (attr->va_size == 0)
86026f9a767SRodney W. Grimes 		return (ENOEXEC);
86126f9a767SRodney W. Grimes 
86226f9a767SRodney W. Grimes 	/*
86326f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
86426f9a767SRodney W. Grimes 	 */
865a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
86626f9a767SRodney W. Grimes 	if (error)
86726f9a767SRodney W. Grimes 		return (error);
86826f9a767SRodney W. Grimes 
8696d5a0a8cSDavid Greenman 	/*
8706d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
8716d5a0a8cSDavid Greenman 	 * if there are any.
8726d5a0a8cSDavid Greenman 	 */
8736d5a0a8cSDavid Greenman 	if (vp->v_writecount)
8746d5a0a8cSDavid Greenman 		return (ETXTBSY);
8756d5a0a8cSDavid Greenman 
8766d5a0a8cSDavid Greenman 	/*
8776d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
8786d5a0a8cSDavid Greenman 	 * general case).
8796d5a0a8cSDavid Greenman 	 */
880a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
88126f9a767SRodney W. Grimes 	return (error);
882df8bae1dSRodney W. Grimes }
883aa855a59SPeter Wemm 
884aa855a59SPeter Wemm /*
885aa855a59SPeter Wemm  * Exec handler registration
886aa855a59SPeter Wemm  */
887aa855a59SPeter Wemm int
888aa855a59SPeter Wemm exec_register(execsw_arg)
889aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
890aa855a59SPeter Wemm {
891aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
892aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
893aa855a59SPeter Wemm 
894aa855a59SPeter Wemm 	if (execsw)
895aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
896aa855a59SPeter Wemm 			count++;
897aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
898aa855a59SPeter Wemm 	if (newexecsw == NULL)
899aa855a59SPeter Wemm 		return ENOMEM;
900aa855a59SPeter Wemm 	xs = newexecsw;
901aa855a59SPeter Wemm 	if (execsw)
902aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
903aa855a59SPeter Wemm 			*xs++ = *es;
904aa855a59SPeter Wemm 	*xs++ = execsw_arg;
905aa855a59SPeter Wemm 	*xs = NULL;
906aa855a59SPeter Wemm 	if (execsw)
907aa855a59SPeter Wemm 		free(execsw, M_TEMP);
908aa855a59SPeter Wemm 	execsw = newexecsw;
909aa855a59SPeter Wemm 	return 0;
910aa855a59SPeter Wemm }
911aa855a59SPeter Wemm 
912aa855a59SPeter Wemm int
913aa855a59SPeter Wemm exec_unregister(execsw_arg)
914aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
915aa855a59SPeter Wemm {
916aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
917aa855a59SPeter Wemm 	int count = 1;
918aa855a59SPeter Wemm 
919aa855a59SPeter Wemm 	if (execsw == NULL)
920aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
921aa855a59SPeter Wemm 
922aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
923aa855a59SPeter Wemm 		if (*es == execsw_arg)
924aa855a59SPeter Wemm 			break;
925aa855a59SPeter Wemm 	}
926aa855a59SPeter Wemm 	if (*es == NULL)
927aa855a59SPeter Wemm 		return ENOENT;
928aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
929aa855a59SPeter Wemm 		if (*es != execsw_arg)
930aa855a59SPeter Wemm 			count++;
931aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
932aa855a59SPeter Wemm 	if (newexecsw == NULL)
933aa855a59SPeter Wemm 		return ENOMEM;
934aa855a59SPeter Wemm 	xs = newexecsw;
935aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
936aa855a59SPeter Wemm 		if (*es != execsw_arg)
937aa855a59SPeter Wemm 			*xs++ = *es;
938aa855a59SPeter Wemm 	*xs = NULL;
939aa855a59SPeter Wemm 	if (execsw)
940aa855a59SPeter Wemm 		free(execsw, M_TEMP);
941aa855a59SPeter Wemm 	execsw = newexecsw;
942aa855a59SPeter Wemm 	return 0;
943aa855a59SPeter Wemm }
94421d56e9cSAlfred Perlstein 
94521d56e9cSAlfred Perlstein int
94621d56e9cSAlfred Perlstein at_exec(function)
94721d56e9cSAlfred Perlstein 	execlist_fn function;
94821d56e9cSAlfred Perlstein {
94921d56e9cSAlfred Perlstein 	struct execlist *ep;
95021d56e9cSAlfred Perlstein 
95121d56e9cSAlfred Perlstein #ifdef INVARIANTS
95221d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
95321d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
95421d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
95521d56e9cSAlfred Perlstein 		    function);
95621d56e9cSAlfred Perlstein #endif
95721d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
95821d56e9cSAlfred Perlstein 	if (ep == NULL)
95921d56e9cSAlfred Perlstein 		return (ENOMEM);
96021d56e9cSAlfred Perlstein 	ep->function = function;
96121d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
96221d56e9cSAlfred Perlstein 	return (0);
96321d56e9cSAlfred Perlstein }
96421d56e9cSAlfred Perlstein 
96521d56e9cSAlfred Perlstein /*
96621d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
96721d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
96821d56e9cSAlfred Perlstein  */
96921d56e9cSAlfred Perlstein int
97021d56e9cSAlfred Perlstein rm_at_exec(function)
97121d56e9cSAlfred Perlstein 	execlist_fn function;
97221d56e9cSAlfred Perlstein {
97321d56e9cSAlfred Perlstein 	struct execlist *ep;
97421d56e9cSAlfred Perlstein 
97521d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
97621d56e9cSAlfred Perlstein 		if (ep->function == function) {
97721d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
97821d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
97921d56e9cSAlfred Perlstein 			return(1);
98021d56e9cSAlfred Perlstein 		}
98121d56e9cSAlfred Perlstein 	}
98221d56e9cSAlfred Perlstein 	return (0);
98321d56e9cSAlfred Perlstein }
98421d56e9cSAlfred Perlstein 
985