xref: /freebsd/sys/kern/kern_exec.c (revision b40ce4165d5eb3a5de1515245055350ae3dbab8e)
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 
68654f6be1SBruce Evans static register_t *exec_copyout_strings __P((struct image_params *));
69df8bae1dSRodney W. Grimes 
709701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
719701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS;
729701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
73486bddb0SDoug Rabson 
749701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
759701cd40SJohn Baldwin static u_long usrstack = USRSTACK;
769701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
7799ac3bc8SPeter Wemm 
78b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
79b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
809701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
81b9df5231SPoul-Henning Kamp 
82a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
83a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
84a8704f89SPoul-Henning Kamp 
8599ac3bc8SPeter Wemm /*
86aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
87aa855a59SPeter Wemm  * double pointer here.
88df8bae1dSRodney W. Grimes  */
89aa855a59SPeter Wemm static const struct execsw **execsw;
9026f9a767SRodney W. Grimes 
91d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
92ad7507e2SSteven Wallace struct execve_args {
93ad7507e2SSteven Wallace         char    *fname;
94ad7507e2SSteven Wallace         char    **argv;
95ad7507e2SSteven Wallace         char    **envv;
96ad7507e2SSteven Wallace };
97d2d3e875SBruce Evans #endif
98ad7507e2SSteven Wallace 
9926f9a767SRodney W. Grimes /*
10026f9a767SRodney W. Grimes  * execve() system call.
101116734c4SMatthew Dillon  *
102116734c4SMatthew Dillon  * MPSAFE
10326f9a767SRodney W. Grimes  */
10426f9a767SRodney W. Grimes int
105b40ce416SJulian Elischer execve(td, uap)
106b40ce416SJulian Elischer 	struct thread *td;
10726f9a767SRodney W. Grimes 	register struct execve_args *uap;
108df8bae1dSRodney W. Grimes {
109b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
11026f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
111b1fc0ec1SRobert Watson 	struct ucred *newcred, *oldcred;
112654f6be1SBruce Evans 	register_t *stack_base;
113bb56ec4aSPoul-Henning Kamp 	int error, len, i;
114c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
11526f9a767SRodney W. Grimes 	struct vattr attr;
116d323ddf3SMatthew Dillon 	int (*img_first) __P((struct image_params *));
117fbd26f75SJohn Baldwin 	struct pargs *pa;
11826f9a767SRodney W. Grimes 
119c52007c2SDavid Greenman 	imgp = &image_params;
120df8bae1dSRodney W. Grimes 
121b40ce416SJulian Elischer /* XXXKSE */
122b40ce416SJulian Elischer /* !!!!!!!! we need abort all the other threads of this process before we */
123b40ce416SJulian Elischer /* proceed beyond his point! */
124b40ce416SJulian Elischer 
125df8bae1dSRodney W. Grimes 	/*
126c52007c2SDavid Greenman 	 * Initialize part of the common data
127df8bae1dSRodney W. Grimes 	 */
128c52007c2SDavid Greenman 	imgp->proc = p;
129c52007c2SDavid Greenman 	imgp->uap = uap;
130c52007c2SDavid Greenman 	imgp->attr = &attr;
131c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1325cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
133c52007c2SDavid Greenman 	imgp->entry_addr = 0;
134c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
135c52007c2SDavid Greenman 	imgp->interpreted = 0;
136c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
137e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1381616db3cSJohn Dyson 	imgp->vp = NULL;
1391616db3cSJohn Dyson 	imgp->firstpage = NULL;
1404fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
141b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
14226f9a767SRodney W. Grimes 
143116734c4SMatthew Dillon 	mtx_lock(&Giant);
144116734c4SMatthew Dillon 
14526f9a767SRodney W. Grimes 	/*
14626f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
14726f9a767SRodney W. Grimes 	 *	environment strings
14826f9a767SRodney W. Grimes 	 */
1491616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
150c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
15126f9a767SRodney W. Grimes 		error = ENOMEM;
15226f9a767SRodney W. Grimes 		goto exec_fail;
15326f9a767SRodney W. Grimes 	}
154c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
155c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1561616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
15726f9a767SRodney W. Grimes 
15826f9a767SRodney W. Grimes 	/*
15926f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
16026f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
16126f9a767SRodney W. Grimes 	 */
16226f9a767SRodney W. Grimes 	ndp = &nd;
163b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
164b40ce416SJulian Elischer 	    UIO_USERSPACE, uap->fname, td);
16526f9a767SRodney W. Grimes 
16626f9a767SRodney W. Grimes interpret:
16726f9a767SRodney W. Grimes 
16826f9a767SRodney W. Grimes 	error = namei(ndp);
16926f9a767SRodney W. Grimes 	if (error) {
1701616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1711616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
17226f9a767SRodney W. Grimes 		goto exec_fail;
17326f9a767SRodney W. Grimes 	}
17426f9a767SRodney W. Grimes 
175c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1769c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
17726f9a767SRodney W. Grimes 
17826f9a767SRodney W. Grimes 	/*
1799022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1809022e4adSDavid Greenman 	 */
181c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1828677f509SDavid Greenman 	if (error) {
183b40ce416SJulian Elischer 		VOP_UNLOCK(imgp->vp, 0, td);
18426f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1858677f509SDavid Greenman 	}
18626f9a767SRodney W. Grimes 
1871616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
188b40ce416SJulian Elischer 	VOP_UNLOCK(imgp->vp, 0, td);
1896d5a0a8cSDavid Greenman 	if (error)
19026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
19126f9a767SRodney W. Grimes 
19226f9a767SRodney W. Grimes 	/*
193d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
194d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
195d323ddf3SMatthew Dillon 	 *	scripts differently.
19626f9a767SRodney W. Grimes 	 */
197d323ddf3SMatthew Dillon 	error = -1;
198d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
199d323ddf3SMatthew Dillon 		error = img_first(imgp);
200d323ddf3SMatthew Dillon 
201d323ddf3SMatthew Dillon 	/*
202d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
203d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
204d323ddf3SMatthew Dillon 	 *	and an error otherwise.
205d323ddf3SMatthew Dillon 	 */
206d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
207d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
208d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
209d323ddf3SMatthew Dillon 			continue;
210d323ddf3SMatthew Dillon 		}
211c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
212d323ddf3SMatthew Dillon 	}
213d323ddf3SMatthew Dillon 
214d323ddf3SMatthew Dillon 	if (error) {
21526f9a767SRodney W. Grimes 		if (error == -1)
216d323ddf3SMatthew Dillon 			error = ENOEXEC;
21726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
218d323ddf3SMatthew Dillon 	}
219d323ddf3SMatthew Dillon 
220d323ddf3SMatthew Dillon 	/*
221d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
222d323ddf3SMatthew Dillon 	 * activate the interpreter.
223d323ddf3SMatthew Dillon 	 */
224c52007c2SDavid Greenman 	if (imgp->interpreted) {
2251616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
226762e6b85SEivind Eklund 		/* free name buffer and old vnode */
227762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
228f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
22926f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
230b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
231b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
23226f9a767SRodney W. Grimes 		goto interpret;
23326f9a767SRodney W. Grimes 	}
23426f9a767SRodney W. Grimes 
23526f9a767SRodney W. Grimes 	/*
23626f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
23726f9a767SRodney W. Grimes 	 */
238c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
23926f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
24026f9a767SRodney W. Grimes 
24126f9a767SRodney W. Grimes 	/*
2421e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2431e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2441e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
24526f9a767SRodney W. Grimes 	 */
2461e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
247c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2481e1e0b44SSøren Schmidt 	else
249c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
25026f9a767SRodney W. Grimes 
251a78e8d2aSDavid Greenman 	/*
252a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
253a78e8d2aSDavid Greenman 	 * be shared after an exec.
254a78e8d2aSDavid Greenman 	 */
255a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
256a78e8d2aSDavid Greenman 		struct filedesc *tmp;
257a78e8d2aSDavid Greenman 
258b40ce416SJulian Elischer 		tmp = fdcopy(td);
259b40ce416SJulian Elischer 		fdfree(td);
260a78e8d2aSDavid Greenman 		p->p_fd = tmp;
261a78e8d2aSDavid Greenman 	}
262a78e8d2aSDavid Greenman 
263333ea485SGuido van Rooij 	/*
264333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
265333ea485SGuido van Rooij 	 * be shared after an exec. The new proces gets a copy of the old
266b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
267333ea485SGuido van Rooij 	 * reset.
268333ea485SGuido van Rooij 	 */
269333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
270333ea485SGuido van Rooij 		struct procsig *newprocsig;
271333ea485SGuido van Rooij 
272333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
273333ea485SGuido van Rooij 		       M_SUBPROC, M_WAITOK);
274333ea485SGuido van Rooij 		bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
275333ea485SGuido van Rooij 		p->p_procsig->ps_refcnt--;
276333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
277333ea485SGuido van Rooij 		p->p_procsig->ps_refcnt = 1;
278b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
279b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
280333ea485SGuido van Rooij 
281b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
282b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
283333ea485SGuido van Rooij 	}
284fdf4e8b3SWarner Losh 	/* Stop profiling */
285fdf4e8b3SWarner Losh 	stopprofclock(p);
286fdf4e8b3SWarner Losh 
28726f9a767SRodney W. Grimes 	/* close files on exec */
288b40ce416SJulian Elischer 	fdcloseexec(td);
28926f9a767SRodney W. Grimes 
29026f9a767SRodney W. Grimes 	/* reset caught signals */
29126f9a767SRodney W. Grimes 	execsigs(p);
29226f9a767SRodney W. Grimes 
29326f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
29426f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
29526f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
29626f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
29726f9a767SRodney W. Grimes 
29826f9a767SRodney W. Grimes 	/*
29924b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
300dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
30126f9a767SRodney W. Grimes 	 */
302e65897c3SJohn Baldwin 	PROC_LOCK(p);
30326f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
30426f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
30526f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
30626f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
30726f9a767SRodney W. Grimes 	}
30826f9a767SRodney W. Grimes 
30926f9a767SRodney W. Grimes 	/*
310b1fc0ec1SRobert Watson 	 * XXX: Note, the whole execve() is incredibly racey right now
311b1fc0ec1SRobert Watson 	 * with regards to debugging and privilege/credential management.
312b1fc0ec1SRobert Watson 	 * In particular, it's possible to race during exec() to attach
313b1fc0ec1SRobert Watson 	 * debugging to a process that will gain privilege.
314b1fc0ec1SRobert Watson 	 *
315b1fc0ec1SRobert Watson 	 * This MUST be fixed prior to any release.
316b1fc0ec1SRobert Watson 	 */
317b1fc0ec1SRobert Watson 
318b1fc0ec1SRobert Watson 	/*
319a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
320a78e8d2aSDavid Greenman 	 *
321a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
322a78e8d2aSDavid Greenman 	 * the process is being traced.
323c52007c2SDavid Greenman 	 */
324b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
325b1fc0ec1SRobert Watson 	newcred = NULL;
326b1fc0ec1SRobert Watson 	if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) ||
327b1fc0ec1SRobert Watson 	     ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) &&
328a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
329c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
330e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
331c52007c2SDavid Greenman 		/*
332c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
333b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
334b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
33526f9a767SRodney W. Grimes 		 */
336b85db196SPeter Wemm 		setsugid(p);
337b1fc0ec1SRobert Watson 		if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) {
33826f9a767SRodney W. Grimes 			p->p_traceflag = 0;
33926f9a767SRodney W. Grimes 			vrele(p->p_tracep);
340c52007c2SDavid Greenman 			p->p_tracep = NULL;
34126f9a767SRodney W. Grimes 		}
342c52007c2SDavid Greenman 		/*
343c52007c2SDavid Greenman 		 * Set the new credentials.
344c52007c2SDavid Greenman 		 */
345b1fc0ec1SRobert Watson 		newcred = crdup(oldcred);
346c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
347b1fc0ec1SRobert Watson 			change_euid(newcred, attr.va_uid);
348c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
349b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
350b40ce416SJulian Elischer 		setugidsafety(td);
351c52007c2SDavid Greenman 	} else {
352b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
353b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
354c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
355e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
35626f9a767SRodney W. Grimes 	}
35726f9a767SRodney W. Grimes 
35826f9a767SRodney W. Grimes 	/*
359c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
360b1fc0ec1SRobert Watson 	 *
361b1fc0ec1SRobert Watson 	 * XXX: It's not clear that the existing behavior is
362b8c526dfSAlexander Langer 	 * POSIX-compliant.  A number of sources indicate that the saved
363b1fc0ec1SRobert Watson 	 * uid/gid should only be updated if the new ruid is not equal to
364b1fc0ec1SRobert Watson 	 * the old ruid, or the new euid is not equal to the old euid and
365b1fc0ec1SRobert Watson 	 * the new euid is not equal to the old ruid.  The FreeBSD code
366b1fc0ec1SRobert Watson 	 * always updates the saved uid/gid.  Also, this code uses the new
367b1fc0ec1SRobert Watson 	 * (replaced) euid and egid as the source, which may or may not be
368b1fc0ec1SRobert Watson 	 * the right ones to use.
36926f9a767SRodney W. Grimes 	 */
3707cb8e4d2SRobert Watson 	if (newcred == NULL) {
371b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
372b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
373b1fc0ec1SRobert Watson 			newcred = crdup(oldcred);
374b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
375b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
376b1fc0ec1SRobert Watson 		}
3777cb8e4d2SRobert Watson 	} else {
3787cb8e4d2SRobert Watson 		change_svuid(newcred, newcred->cr_uid);
3797cb8e4d2SRobert Watson 		change_svgid(newcred, newcred->cr_gid);
3807cb8e4d2SRobert Watson 	}
3817cb8e4d2SRobert Watson 
382b1fc0ec1SRobert Watson 	if (newcred != NULL) {
383b1fc0ec1SRobert Watson 		PROC_LOCK(p);
384b1fc0ec1SRobert Watson 		p->p_ucred = newcred;
385b1fc0ec1SRobert Watson 		PROC_UNLOCK(p);
386b1fc0ec1SRobert Watson 		crfree(oldcred);
387b1fc0ec1SRobert Watson 	}
38826f9a767SRodney W. Grimes 
38926f9a767SRodney W. Grimes 	/*
3902a531c80SDavid Greenman 	 * Store the vp for use in procfs
3912a531c80SDavid Greenman 	 */
3922a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
3932a531c80SDavid Greenman 		vrele(p->p_textvp);
3942a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3952a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3962a531c80SDavid Greenman 
3972a531c80SDavid Greenman 	/*
398cb679c38SJonathan Lemon 	 * notify others that we exec'd
399cb679c38SJonathan Lemon 	 */
400e65897c3SJohn Baldwin 	PROC_LOCK(p);
401cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
402cb679c38SJonathan Lemon 
403cb679c38SJonathan Lemon 	/*
40426f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
40526f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
40626f9a767SRodney W. Grimes 	 */
407e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
4082a024a2bSSean Eric Fagan 
40926f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
41026f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
41126f9a767SRodney W. Grimes 
41226f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
41326f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
41426f9a767SRodney W. Grimes 
4154fe88fe6SJohn Polstra 	/* Set values passed into the program in registers. */
416b40ce416SJulian Elischer 	setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
4174fe88fe6SJohn Polstra 	    imgp->ps_strings);
41826f9a767SRodney W. Grimes 
419b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
420fbd26f75SJohn Baldwin 	pa = p->p_args;
421b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
422fbd26f75SJohn Baldwin 	PROC_UNLOCK(p);
423fbd26f75SJohn Baldwin 	if (pa != NULL && --pa->ar_ref == 0)
424fbd26f75SJohn Baldwin 		FREE(pa, M_PARGS);
425b9df5231SPoul-Henning Kamp 
426b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
427b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
428b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
429fbd26f75SJohn Baldwin 		MALLOC(pa, struct pargs *, sizeof(struct pargs) + i,
430b9df5231SPoul-Henning Kamp 		    M_PARGS, M_WAITOK);
431fbd26f75SJohn Baldwin 		pa->ar_ref = 1;
432fbd26f75SJohn Baldwin 		pa->ar_length = i;
433fbd26f75SJohn Baldwin 		bcopy(imgp->stringbase, pa->ar_args, i);
434e65897c3SJohn Baldwin 		PROC_LOCK(p);
435fbd26f75SJohn Baldwin 		p->p_args = pa;
436e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
437fbd26f75SJohn Baldwin 	}
438b9df5231SPoul-Henning Kamp 
4391616db3cSJohn Dyson exec_fail_dealloc:
4401616db3cSJohn Dyson 
44126f9a767SRodney W. Grimes 	/*
44226f9a767SRodney W. Grimes 	 * free various allocated resources
44326f9a767SRodney W. Grimes 	 */
4441616db3cSJohn Dyson 	if (imgp->firstpage)
4451616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
44626f9a767SRodney W. Grimes 
447c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
4481616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
4491616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
4501616db3cSJohn Dyson 
4519c0fed3dSDoug Rabson 	if (imgp->vp) {
452762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
4539c0fed3dSDoug Rabson 		vrele(imgp->vp);
454a3cf6ebaSDavid Greenman 	}
45526f9a767SRodney W. Grimes 
4561616db3cSJohn Dyson 	if (error == 0)
457116734c4SMatthew Dillon 		goto done2;
4581616db3cSJohn Dyson 
45926f9a767SRodney W. Grimes exec_fail:
460c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
46126f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
462b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
46326f9a767SRodney W. Grimes 		/* NOT REACHED */
464116734c4SMatthew Dillon 		error = 0;
46526f9a767SRodney W. Grimes 	}
466116734c4SMatthew Dillon done2:
467116734c4SMatthew Dillon 	mtx_unlock(&Giant);
468116734c4SMatthew Dillon 	return(error);
46926f9a767SRodney W. Grimes }
47026f9a767SRodney W. Grimes 
4711616db3cSJohn Dyson int
4721616db3cSJohn Dyson exec_map_first_page(imgp)
4731616db3cSJohn Dyson 	struct image_params *imgp;
4741616db3cSJohn Dyson {
475d8aad40cSJohn Baldwin 	int rv, i;
47695461b45SJohn Dyson 	int initial_pagein;
47795461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
4781616db3cSJohn Dyson 	vm_object_t object;
4791616db3cSJohn Dyson 
4800cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
4811616db3cSJohn Dyson 
4821616db3cSJohn Dyson 	if (imgp->firstpage) {
4831616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
4841616db3cSJohn Dyson 	}
4851616db3cSJohn Dyson 
4869ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
4871616db3cSJohn Dyson 
48895461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
4891616db3cSJohn Dyson 
49095461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
49195461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
49295461b45SJohn Dyson 		if (initial_pagein > object->size)
49395461b45SJohn Dyson 			initial_pagein = object->size;
49495461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
495d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
49695461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
49795461b45SJohn Dyson 					break;
49895461b45SJohn Dyson 				if (ma[i]->valid)
49995461b45SJohn Dyson 					break;
500e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
50195461b45SJohn Dyson 			} else {
50295461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
50395461b45SJohn Dyson 				if (ma[i] == NULL)
50495461b45SJohn Dyson 					break;
50595461b45SJohn Dyson 			}
50695461b45SJohn Dyson 		}
50795461b45SJohn Dyson 		initial_pagein = i;
5081616db3cSJohn Dyson 
50995461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
51095461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
51195461b45SJohn Dyson 
512eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
513ecbb00a2SDoug Rabson 			if (ma[0]) {
51495461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
5158f9110f6SJohn Dyson 				vm_page_free(ma[0]);
516ecbb00a2SDoug Rabson 			}
5171616db3cSJohn Dyson 			return EIO;
5181616db3cSJohn Dyson 		}
5191616db3cSJohn Dyson 	}
5201616db3cSJohn Dyson 
52195461b45SJohn Dyson 	vm_page_wire(ma[0]);
522e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
5231616db3cSJohn Dyson 
52495461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
52595461b45SJohn Dyson 	imgp->firstpage = ma[0];
5261616db3cSJohn Dyson 
5271616db3cSJohn Dyson 	return 0;
5281616db3cSJohn Dyson }
5291616db3cSJohn Dyson 
5301616db3cSJohn Dyson void
5311616db3cSJohn Dyson exec_unmap_first_page(imgp)
5321616db3cSJohn Dyson 	struct image_params *imgp;
5331616db3cSJohn Dyson {
5340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
53523955314SAlfred Perlstein 
5361616db3cSJohn Dyson 	if (imgp->firstpage) {
5371616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
53873007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
5391616db3cSJohn Dyson 		imgp->firstpage = NULL;
5401616db3cSJohn Dyson 	}
5411616db3cSJohn Dyson }
5421616db3cSJohn Dyson 
54326f9a767SRodney W. Grimes /*
54426f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
54526f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
54626f9a767SRodney W. Grimes  *	automatically in trap.c.
54726f9a767SRodney W. Grimes  */
54826f9a767SRodney W. Grimes int
549c52007c2SDavid Greenman exec_new_vmspace(imgp)
550c52007c2SDavid Greenman 	struct image_params *imgp;
55126f9a767SRodney W. Grimes {
55226f9a767SRodney W. Grimes 	int error;
553c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
5542267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
555af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
55626f9a767SRodney W. Grimes 
5570cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5580cddd8f0SMatthew Dillon 
559c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
56026f9a767SRodney W. Grimes 
561af9ec885SJohn Dyson 	/*
562af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
563af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
564af9ec885SJohn Dyson 	 * not disrupted
565af9ec885SJohn Dyson 	 */
566af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
5673d903220SDoug Rabson 		if (vmspace->vm_shm)
568c52007c2SDavid Greenman 			shmexit(imgp->proc);
569b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
5709c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
571af9ec885SJohn Dyson 	} else {
572492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
573492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
574af9ec885SJohn Dyson 		map = &vmspace->vm_map;
575af9ec885SJohn Dyson 	}
57626f9a767SRodney W. Grimes 
57726f9a767SRodney W. Grimes 	/* Allocate a new stack */
5782267af78SJulian Elischer 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
5792267af78SJulian Elischer 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
5802267af78SJulian Elischer 	if (error)
5812267af78SJulian Elischer 		return (error);
5822267af78SJulian Elischer 
58363c47a5cSDoug Rabson #ifdef __ia64__
58463c47a5cSDoug Rabson 	{
58563c47a5cSDoug Rabson 		/*
58663c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
58763c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
58863c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
58963c47a5cSDoug Rabson 		 */
59063c47a5cSDoug Rabson 		vm_offset_t bsaddr;
59163c47a5cSDoug Rabson 		bsaddr = USRSTACK - 2*MAXSSIZ;
59263c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
59363c47a5cSDoug Rabson 				    4*PAGE_SIZE, 0,
59463c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
595b40ce416SJulian Elischer 		imgp->proc->p_thread.td_md.md_bspstore = bsaddr;
59663c47a5cSDoug Rabson 	}
59763c47a5cSDoug Rabson #endif
59863c47a5cSDoug Rabson 
5992267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
6002267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
6012267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
6022267af78SJulian Elischer 	 */
6032267af78SJulian Elischer 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
6042267af78SJulian Elischer 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
60526f9a767SRodney W. Grimes 
60626f9a767SRodney W. Grimes 	return(0);
60726f9a767SRodney W. Grimes }
60826f9a767SRodney W. Grimes 
60926f9a767SRodney W. Grimes /*
61026f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
61126f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
61226f9a767SRodney W. Grimes  */
61326f9a767SRodney W. Grimes int
614c52007c2SDavid Greenman exec_extract_strings(imgp)
615c52007c2SDavid Greenman 	struct image_params *imgp;
61626f9a767SRodney W. Grimes {
61726f9a767SRodney W. Grimes 	char	**argv, **envv;
61826f9a767SRodney W. Grimes 	char	*argp, *envp;
619ecbb00a2SDoug Rabson 	int	error;
620ecbb00a2SDoug Rabson 	size_t	length;
62126f9a767SRodney W. Grimes 
62226f9a767SRodney W. Grimes 	/*
62326f9a767SRodney W. Grimes 	 * extract arguments first
62426f9a767SRodney W. Grimes 	 */
62526f9a767SRodney W. Grimes 
626c52007c2SDavid Greenman 	argv = imgp->uap->argv;
62726f9a767SRodney W. Grimes 
6280fd1a014SDavid Greenman 	if (argv) {
629aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
6305cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
6315cf3d12cSAndrey A. Chernov 			return (EFAULT);
6325cf3d12cSAndrey A. Chernov 		if (argp)
6335cf3d12cSAndrey A. Chernov 			argv++;
6345cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
6355cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
6365cf3d12cSAndrey A. Chernov 		if (argp) {
6375cf3d12cSAndrey A. Chernov 			do {
63826f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
63926f9a767SRodney W. Grimes 					return (EFAULT);
640c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
641c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
6420fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
64326f9a767SRodney W. Grimes 						return(E2BIG);
6440fd1a014SDavid Greenman 					return (error);
6450fd1a014SDavid Greenman 				}
646c52007c2SDavid Greenman 				imgp->stringspace -= length;
647c52007c2SDavid Greenman 				imgp->stringp += length;
648c52007c2SDavid Greenman 				imgp->argc++;
649aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
65026f9a767SRodney W. Grimes 		}
6510fd1a014SDavid Greenman 	}
65226f9a767SRodney W. Grimes 
653b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
654b9df5231SPoul-Henning Kamp 
65526f9a767SRodney W. Grimes 	/*
65626f9a767SRodney W. Grimes 	 * extract environment strings
65726f9a767SRodney W. Grimes 	 */
65826f9a767SRodney W. Grimes 
659c52007c2SDavid Greenman 	envv = imgp->uap->envv;
66026f9a767SRodney W. Grimes 
6610fd1a014SDavid Greenman 	if (envv) {
662aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
66326f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
66426f9a767SRodney W. Grimes 				return (EFAULT);
665c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
666c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
6670fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
66826f9a767SRodney W. Grimes 					return(E2BIG);
6690fd1a014SDavid Greenman 				return (error);
6700fd1a014SDavid Greenman 			}
671c52007c2SDavid Greenman 			imgp->stringspace -= length;
672c52007c2SDavid Greenman 			imgp->stringp += length;
673c52007c2SDavid Greenman 			imgp->envc++;
67426f9a767SRodney W. Grimes 		}
6750fd1a014SDavid Greenman 	}
67626f9a767SRodney W. Grimes 
67726f9a767SRodney W. Grimes 	return (0);
67826f9a767SRodney W. Grimes }
67926f9a767SRodney W. Grimes 
68026f9a767SRodney W. Grimes /*
68126f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
68226f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
68326f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
68426f9a767SRodney W. Grimes  */
685654f6be1SBruce Evans register_t *
686c52007c2SDavid Greenman exec_copyout_strings(imgp)
687c52007c2SDavid Greenman 	struct image_params *imgp;
68826f9a767SRodney W. Grimes {
68926f9a767SRodney W. Grimes 	int argc, envc;
69026f9a767SRodney W. Grimes 	char **vectp;
69126f9a767SRodney W. Grimes 	char *stringp, *destp;
692654f6be1SBruce Evans 	register_t *stack_base;
69393f6448cSDavid Greenman 	struct ps_strings *arginfo;
694d66a5066SPeter Wemm 	int szsigcode;
69526f9a767SRodney W. Grimes 
69626f9a767SRodney W. Grimes 	/*
69726f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
698d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
69926f9a767SRodney W. Grimes 	 */
7004c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
701d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
702d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
7031ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
7041ed012f9SPeter Wemm 
70526f9a767SRodney W. Grimes 	/*
706d66a5066SPeter Wemm 	 * install sigcode
707d66a5066SPeter Wemm 	 */
708d66a5066SPeter Wemm 	if (szsigcode)
709d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
710d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
711d66a5066SPeter Wemm 
712d66a5066SPeter Wemm 	/*
713e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
714e1743d02SSøren Schmidt 	 * on the stack.
715e1743d02SSøren Schmidt 	 */
716b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
717e1743d02SSøren Schmidt 		/*
718b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
719b9a22da4STakanori Watanabe 		 * lower compatibility.
720b9a22da4STakanori Watanabe 		 */
721b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
722b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
723b9a22da4STakanori Watanabe 		/*
724b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
725b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
726b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
727e1743d02SSøren Schmidt 		 */
728e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
729b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
730b9a22da4STakanori Watanabe 
731b9a22da4STakanori Watanabe 	} else
732e1743d02SSøren Schmidt 		/*
733b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
734b9a22da4STakanori Watanabe 		 * the arg and env vector sets
73526f9a767SRodney W. Grimes 		 */
736e1743d02SSøren Schmidt 		vectp = (char **)
737e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
73826f9a767SRodney W. Grimes 
73926f9a767SRodney W. Grimes 	/*
74026f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
74126f9a767SRodney W. Grimes 	 */
742654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
74326f9a767SRodney W. Grimes 
744c52007c2SDavid Greenman 	stringp = imgp->stringbase;
745c52007c2SDavid Greenman 	argc = imgp->argc;
746c52007c2SDavid Greenman 	envc = imgp->envc;
74726f9a767SRodney W. Grimes 
74893f6448cSDavid Greenman 	/*
7493aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
75093f6448cSDavid Greenman 	 */
751c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
75293f6448cSDavid Greenman 
75393f6448cSDavid Greenman 	/*
7543aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
7553aed948bSDavid Greenman 	 */
756aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
7573aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
7583aed948bSDavid Greenman 
7593aed948bSDavid Greenman 	/*
7603aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
76193f6448cSDavid Greenman 	 */
76226f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
763aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
7643aed948bSDavid Greenman 		while (*stringp++ != 0)
7653aed948bSDavid Greenman 			destp++;
7663aed948bSDavid Greenman 		destp++;
76726f9a767SRodney W. Grimes 	}
76826f9a767SRodney W. Grimes 
7691a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
7706ab46d52SBruce Evans 	suword(vectp++, 0);
77126f9a767SRodney W. Grimes 
772aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
7733aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
77493f6448cSDavid Greenman 
77593f6448cSDavid Greenman 	/*
7763aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
77793f6448cSDavid Greenman 	 */
77826f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
779aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
7803aed948bSDavid Greenman 		while (*stringp++ != 0)
7813aed948bSDavid Greenman 			destp++;
7823aed948bSDavid Greenman 		destp++;
78326f9a767SRodney W. Grimes 	}
78426f9a767SRodney W. Grimes 
78526f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
7866ab46d52SBruce Evans 	suword(vectp, 0);
78726f9a767SRodney W. Grimes 
78826f9a767SRodney W. Grimes 	return (stack_base);
78926f9a767SRodney W. Grimes }
79026f9a767SRodney W. Grimes 
79126f9a767SRodney W. Grimes /*
79226f9a767SRodney W. Grimes  * Check permissions of file to execute.
793cf64863aSRobert Watson  *	Called with imgp->vp locked.
79426f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
79526f9a767SRodney W. Grimes  */
796c8a79999SPeter Wemm int
797c52007c2SDavid Greenman exec_check_permissions(imgp)
798c52007c2SDavid Greenman 	struct image_params *imgp;
79926f9a767SRodney W. Grimes {
800c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
801c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
802c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
80326f9a767SRodney W. Grimes 	int error;
80426f9a767SRodney W. Grimes 
80526f9a767SRodney W. Grimes 	/* Get file attributes */
806b40ce416SJulian Elischer 	error = VOP_GETATTR(vp, attr, p->p_ucred, curthread); /* XXXKSE */
80726f9a767SRodney W. Grimes 	if (error)
80826f9a767SRodney W. Grimes 		return (error);
80926f9a767SRodney W. Grimes 
81026f9a767SRodney W. Grimes 	/*
81126f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
81226f9a767SRodney W. Grimes 	 *	file resides on.
81326f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
81426f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
81526f9a767SRodney W. Grimes 	 *	file really is executable.
81626f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
81726f9a767SRodney W. Grimes 	 */
818c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
81926f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
82026f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
82126f9a767SRodney W. Grimes 		return (EACCES);
82226f9a767SRodney W. Grimes 	}
82326f9a767SRodney W. Grimes 
82426f9a767SRodney W. Grimes 	/*
82526f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
82626f9a767SRodney W. Grimes 	 */
82726f9a767SRodney W. Grimes 	if (attr->va_size == 0)
82826f9a767SRodney W. Grimes 		return (ENOEXEC);
82926f9a767SRodney W. Grimes 
83026f9a767SRodney W. Grimes 	/*
83126f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
83226f9a767SRodney W. Grimes 	 */
833b40ce416SJulian Elischer 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, curthread); /* XXXKSE */
83426f9a767SRodney W. Grimes 	if (error)
83526f9a767SRodney W. Grimes 		return (error);
83626f9a767SRodney W. Grimes 
8376d5a0a8cSDavid Greenman 	/*
8386d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
8396d5a0a8cSDavid Greenman 	 * if there are any.
8406d5a0a8cSDavid Greenman 	 */
8416d5a0a8cSDavid Greenman 	if (vp->v_writecount)
8426d5a0a8cSDavid Greenman 		return (ETXTBSY);
8436d5a0a8cSDavid Greenman 
8446d5a0a8cSDavid Greenman 	/*
8456d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
8466d5a0a8cSDavid Greenman 	 * general case).
8476d5a0a8cSDavid Greenman 	 */
848b40ce416SJulian Elischer 	error = VOP_OPEN(vp, FREAD, p->p_ucred, curthread); /* XXXKSE */
84926f9a767SRodney W. Grimes 	if (error)
85026f9a767SRodney W. Grimes 		return (error);
85126f9a767SRodney W. Grimes 
85226f9a767SRodney W. Grimes 	return (0);
853df8bae1dSRodney W. Grimes }
854aa855a59SPeter Wemm 
855aa855a59SPeter Wemm /*
856aa855a59SPeter Wemm  * Exec handler registration
857aa855a59SPeter Wemm  */
858aa855a59SPeter Wemm int
859aa855a59SPeter Wemm exec_register(execsw_arg)
860aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
861aa855a59SPeter Wemm {
862aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
863aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
864aa855a59SPeter Wemm 
865aa855a59SPeter Wemm 	if (execsw)
866aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
867aa855a59SPeter Wemm 			count++;
868aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
869aa855a59SPeter Wemm 	if (newexecsw == NULL)
870aa855a59SPeter Wemm 		return ENOMEM;
871aa855a59SPeter Wemm 	xs = newexecsw;
872aa855a59SPeter Wemm 	if (execsw)
873aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
874aa855a59SPeter Wemm 			*xs++ = *es;
875aa855a59SPeter Wemm 	*xs++ = execsw_arg;
876aa855a59SPeter Wemm 	*xs = NULL;
877aa855a59SPeter Wemm 	if (execsw)
878aa855a59SPeter Wemm 		free(execsw, M_TEMP);
879aa855a59SPeter Wemm 	execsw = newexecsw;
880aa855a59SPeter Wemm 	return 0;
881aa855a59SPeter Wemm }
882aa855a59SPeter Wemm 
883aa855a59SPeter Wemm int
884aa855a59SPeter Wemm exec_unregister(execsw_arg)
885aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
886aa855a59SPeter Wemm {
887aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
888aa855a59SPeter Wemm 	int count = 1;
889aa855a59SPeter Wemm 
890aa855a59SPeter Wemm 	if (execsw == NULL)
891aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
892aa855a59SPeter Wemm 
893aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
894aa855a59SPeter Wemm 		if (*es == execsw_arg)
895aa855a59SPeter Wemm 			break;
896aa855a59SPeter Wemm 	}
897aa855a59SPeter Wemm 	if (*es == NULL)
898aa855a59SPeter Wemm 		return ENOENT;
899aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
900aa855a59SPeter Wemm 		if (*es != execsw_arg)
901aa855a59SPeter Wemm 			count++;
902aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
903aa855a59SPeter Wemm 	if (newexecsw == NULL)
904aa855a59SPeter Wemm 		return ENOMEM;
905aa855a59SPeter Wemm 	xs = newexecsw;
906aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
907aa855a59SPeter Wemm 		if (*es != execsw_arg)
908aa855a59SPeter Wemm 			*xs++ = *es;
909aa855a59SPeter Wemm 	*xs = NULL;
910aa855a59SPeter Wemm 	if (execsw)
911aa855a59SPeter Wemm 		free(execsw, M_TEMP);
912aa855a59SPeter Wemm 	execsw = newexecsw;
913aa855a59SPeter Wemm 	return 0;
914aa855a59SPeter Wemm }
915