xref: /freebsd/sys/kern/kern_exec.c (revision 3db161e07937ad130a89eb68afb1967c9ee5c7dd)
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 
291e9b3d91SPeter Wemm #include "opt_ktrace.h"
30339b79b9SRobert Watson #include "opt_mac.h"
311e9b3d91SPeter Wemm 
32df8bae1dSRodney W. Grimes #include <sys/param.h>
3326f9a767SRodney W. Grimes #include <sys/systm.h>
34fb919e4dSMark Murray #include <sys/lock.h>
35fb919e4dSMark Murray #include <sys/mutex.h>
36d2d3e875SBruce Evans #include <sys/sysproto.h>
3726f9a767SRodney W. Grimes #include <sys/signalvar.h>
3826f9a767SRodney W. Grimes #include <sys/kernel.h>
39339b79b9SRobert Watson #include <sys/mac.h>
4026f9a767SRodney W. Grimes #include <sys/mount.h>
41797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
42079cc25bSDavid Greenman #include <sys/fcntl.h>
4326f9a767SRodney W. Grimes #include <sys/acct.h>
4426f9a767SRodney W. Grimes #include <sys/exec.h>
4526f9a767SRodney W. Grimes #include <sys/imgact.h>
46e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4726f9a767SRodney W. Grimes #include <sys/wait.h>
48333ea485SGuido van Rooij #include <sys/malloc.h>
49a794e791SBruce Evans #include <sys/proc.h>
502a024a2bSSean Eric Fagan #include <sys/pioctl.h>
51a794e791SBruce Evans #include <sys/namei.h>
521e1e0b44SSøren Schmidt #include <sys/sysent.h>
53797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5499ac3bc8SPeter Wemm #include <sys/sysctl.h>
55333ea485SGuido van Rooij #include <sys/user.h>
56a794e791SBruce Evans #include <sys/vnode.h>
57c781aea8SPeter Wemm #ifdef KTRACE
58c781aea8SPeter Wemm #include <sys/ktrace.h>
59c781aea8SPeter Wemm #endif
6026f9a767SRodney W. Grimes 
6126f9a767SRodney W. Grimes #include <vm/vm.h>
62efeaf95aSDavid Greenman #include <vm/vm_param.h>
63efeaf95aSDavid Greenman #include <vm/pmap.h>
641616db3cSJohn Dyson #include <vm/vm_page.h>
65efeaf95aSDavid Greenman #include <vm/vm_map.h>
6626f9a767SRodney W. Grimes #include <vm/vm_kern.h>
67efeaf95aSDavid Greenman #include <vm/vm_extern.h>
681ebd0c59SDavid Greenman #include <vm/vm_object.h>
691616db3cSJohn Dyson #include <vm/vm_pager.h>
7026f9a767SRodney W. Grimes 
7126f9a767SRodney W. Grimes #include <machine/reg.h>
7226f9a767SRodney W. Grimes 
73b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
74b9df5231SPoul-Henning Kamp 
7521d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback");
7621d56e9cSAlfred Perlstein 
7705ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
7805ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
795dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
80450ffb44SRobert Watson static int kern_execve(struct thread *td, char *fname, char **argv,
81670cb89bSRobert Watson 	char **envv, struct mac *mac_p);
8205ba50f5SJake Burkholder 
8321d56e9cSAlfred Perlstein /*
8421d56e9cSAlfred Perlstein  * callout list for things to do at exec time
8521d56e9cSAlfred Perlstein  */
8621d56e9cSAlfred Perlstein struct execlist {
8721d56e9cSAlfred Perlstein 	execlist_fn function;
8821d56e9cSAlfred Perlstein 	TAILQ_ENTRY(execlist) next;
8921d56e9cSAlfred Perlstein };
9021d56e9cSAlfred Perlstein 
9121d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist);
9221d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list);
9321d56e9cSAlfred Perlstein 
949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9505ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
9605ba50f5SJake Burkholder     NULL, 0, sysctl_kern_ps_strings, "LU", "");
97486bddb0SDoug Rabson 
989701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9905ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
10005ba50f5SJake Burkholder     NULL, 0, sysctl_kern_usrstack, "LU", "");
10199ac3bc8SPeter Wemm 
1025dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
1035dadd17bSJake Burkholder     NULL, 0, sysctl_kern_stackprot, "I", "");
1045dadd17bSJake Burkholder 
105b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
106c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
1079701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
108b9df5231SPoul-Henning Kamp 
109a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
110a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
111a8704f89SPoul-Henning Kamp 
112911fc923SPeter Wemm #ifdef __ia64__
113911fc923SPeter Wemm /* XXX HACK */
114911fc923SPeter Wemm static int regstkpages = 256;
115911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, &regstkpages, 0, "");
116911fc923SPeter Wemm #endif
117911fc923SPeter Wemm 
11805ba50f5SJake Burkholder static int
11905ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
12005ba50f5SJake Burkholder {
12105ba50f5SJake Burkholder 	struct proc *p;
12205ba50f5SJake Burkholder 
12305ba50f5SJake Burkholder 	p = curproc;
12405ba50f5SJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
12505ba50f5SJake Burkholder 	   sizeof(p->p_sysent->sv_psstrings)));
12605ba50f5SJake Burkholder }
12705ba50f5SJake Burkholder 
12805ba50f5SJake Burkholder static int
12905ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
13005ba50f5SJake Burkholder {
13105ba50f5SJake Burkholder 	struct proc *p;
13205ba50f5SJake Burkholder 
13305ba50f5SJake Burkholder 	p = curproc;
13405ba50f5SJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
13505ba50f5SJake Burkholder 	    sizeof(p->p_sysent->sv_usrstack)));
13605ba50f5SJake Burkholder }
13705ba50f5SJake Burkholder 
1385dadd17bSJake Burkholder static int
1395dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
1405dadd17bSJake Burkholder {
1415dadd17bSJake Burkholder 	struct proc *p;
1425dadd17bSJake Burkholder 
1435dadd17bSJake Burkholder 	p = curproc;
1445dadd17bSJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
1455dadd17bSJake Burkholder 	    sizeof(p->p_sysent->sv_stackprot)));
1465dadd17bSJake Burkholder }
1475dadd17bSJake Burkholder 
14899ac3bc8SPeter Wemm /*
149aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
150aa855a59SPeter Wemm  * double pointer here.
151df8bae1dSRodney W. Grimes  */
152aa855a59SPeter Wemm static const struct execsw **execsw;
15326f9a767SRodney W. Grimes 
15426f9a767SRodney W. Grimes /*
155450ffb44SRobert Watson  * In-kernel implementation of execve().  All arguments are assumed to be
156450ffb44SRobert Watson  * userspace pointers from the passed thread.
157116734c4SMatthew Dillon  *
158116734c4SMatthew Dillon  * MPSAFE
15926f9a767SRodney W. Grimes  */
160450ffb44SRobert Watson static int
161670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p)
162b40ce416SJulian Elischer 	struct thread *td;
163450ffb44SRobert Watson 	char *fname;
164450ffb44SRobert Watson 	char **argv;
165450ffb44SRobert Watson 	char **envv;
166670cb89bSRobert Watson 	struct mac *mac_p;
167df8bae1dSRodney W. Grimes {
168b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
16926f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
1709b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
1711419eacbSAlfred Perlstein 	struct uidinfo *euip;
172654f6be1SBruce Evans 	register_t *stack_base;
173bb56ec4aSPoul-Henning Kamp 	int error, len, i;
174c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
17526f9a767SRodney W. Grimes 	struct vattr attr;
1764d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
17769be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
1789b3b1c5fSJohn Baldwin 	struct procsig *oldprocsig, *newprocsig;
1796c84de02SJohn Baldwin #ifdef KTRACE
1806c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
1816c84de02SJohn Baldwin #endif
1826c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
183d06c0d4dSRobert Watson 	int credential_changing;
184619eb6e5SJeff Roberson 	int textset;
185ccafe7ebSRobert Watson #ifdef MAC
186670cb89bSRobert Watson 	struct label interplabel;	/* label of the interpreted vnode */
187670cb89bSRobert Watson 	struct label execlabel;		/* optional label argument */
188670cb89bSRobert Watson 	int will_transition, interplabelvalid = 0;
189ccafe7ebSRobert Watson #endif
19026f9a767SRodney W. Grimes 
191c52007c2SDavid Greenman 	imgp = &image_params;
192df8bae1dSRodney W. Grimes 
1939ca45e81SDag-Erling Smørgrav 	/*
1949ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
1959ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
1969ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
1979ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
1989ca45e81SDag-Erling Smørgrav 	 * privileges.
1999ca45e81SDag-Erling Smørgrav 	 */
2009ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
2019ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
20291f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
20349539972SJulian Elischer 	if (p->p_flag & P_KSES) {
2041279572aSDavid Xu 		if (thread_single(SINGLE_EXIT)) {
205e602ba25SJulian Elischer 			PROC_UNLOCK(p);
206e602ba25SJulian Elischer 			return (ERESTART);	/* Try again later. */
207e602ba25SJulian Elischer 		}
20849539972SJulian Elischer 		/*
20949539972SJulian Elischer 		 * If we get here all other threads are dead,
21049539972SJulian Elischer 		 * so unset the associated flags and lose KSE mode.
21149539972SJulian Elischer 		 */
21249539972SJulian Elischer 		p->p_flag &= ~P_KSES;
21349539972SJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
21445f603e2SDavid Xu 		td->td_mailbox = NULL;
21545f603e2SDavid Xu 		td->td_kse->ke_mailbox = NULL;
21645f603e2SDavid Xu 		td->td_kse->ke_flags &= ~KEF_DOUPCALL;
21749539972SJulian Elischer 		thread_single_end();
21849539972SJulian Elischer 	}
2199ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
2209ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
2219ca45e81SDag-Erling Smørgrav 
222df8bae1dSRodney W. Grimes 	/*
223c52007c2SDavid Greenman 	 * Initialize part of the common data
224df8bae1dSRodney W. Grimes 	 */
225c52007c2SDavid Greenman 	imgp->proc = p;
226450ffb44SRobert Watson 	imgp->userspace_argv = argv;
227450ffb44SRobert Watson 	imgp->userspace_envv = envv;
228670cb89bSRobert Watson 	imgp->execlabel = NULL;
229c52007c2SDavid Greenman 	imgp->attr = &attr;
230c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
2315cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
232c52007c2SDavid Greenman 	imgp->entry_addr = 0;
233c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
234c52007c2SDavid Greenman 	imgp->interpreted = 0;
235c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
236e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
2371616db3cSJohn Dyson 	imgp->vp = NULL;
2380b2ed1aeSJeff Roberson 	imgp->object = NULL;
2391616db3cSJohn Dyson 	imgp->firstpage = NULL;
2404fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
241b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
24226f9a767SRodney W. Grimes 
243670cb89bSRobert Watson #ifdef MAC
244670cb89bSRobert Watson 	error = mac_execve_enter(imgp, mac_p, &execlabel);
245670cb89bSRobert Watson 	if (error) {
246670cb89bSRobert Watson 		mtx_lock(&Giant);
247670cb89bSRobert Watson 		goto exec_fail;
248670cb89bSRobert Watson 	}
249670cb89bSRobert Watson #endif
250670cb89bSRobert Watson 
25126f9a767SRodney W. Grimes 	/*
25226f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
25326f9a767SRodney W. Grimes 	 *	environment strings
25426f9a767SRodney W. Grimes 	 */
255ca0387efSJake Burkholder 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX +
256ca0387efSJake Burkholder 	    PAGE_SIZE);
257c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
25826f9a767SRodney W. Grimes 		error = ENOMEM;
259b3afd20dSAlan Cox 		mtx_lock(&Giant);
26026f9a767SRodney W. Grimes 		goto exec_fail;
26126f9a767SRodney W. Grimes 	}
262c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
263c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
2641616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
26526f9a767SRodney W. Grimes 
26626f9a767SRodney W. Grimes 	/*
26726f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
26826f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
26926f9a767SRodney W. Grimes 	 */
27026f9a767SRodney W. Grimes 	ndp = &nd;
271b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
272450ffb44SRobert Watson 	    UIO_USERSPACE, fname, td);
27326f9a767SRodney W. Grimes 
274b3afd20dSAlan Cox 	mtx_lock(&Giant);
27526f9a767SRodney W. Grimes interpret:
27626f9a767SRodney W. Grimes 
27726f9a767SRodney W. Grimes 	error = namei(ndp);
27826f9a767SRodney W. Grimes 	if (error) {
2791616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
2801616db3cSJohn Dyson 		    ARG_MAX + PAGE_SIZE);
28126f9a767SRodney W. Grimes 		goto exec_fail;
28226f9a767SRodney W. Grimes 	}
28326f9a767SRodney W. Grimes 
284c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
285450ffb44SRobert Watson 	imgp->fname = fname;
28626f9a767SRodney W. Grimes 
28726f9a767SRodney W. Grimes 	/*
2889022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
2899022e4adSDavid Greenman 	 */
290c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
291619eb6e5SJeff Roberson 	if (error)
29226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
293619eb6e5SJeff Roberson 
294619eb6e5SJeff Roberson 	if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
2950b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
29626f9a767SRodney W. Grimes 
297619eb6e5SJeff Roberson 	/*
298619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
299619eb6e5SJeff Roberson 	 * activating it.
300619eb6e5SJeff Roberson 	 *
301619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
302619eb6e5SJeff Roberson 	 * actually an executable image.
303619eb6e5SJeff Roberson 	 */
304619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
305619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
306619eb6e5SJeff Roberson 
3071616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
3086d5a0a8cSDavid Greenman 	if (error)
30926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
31026f9a767SRodney W. Grimes 
31126f9a767SRodney W. Grimes 	/*
312d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
313d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
314d323ddf3SMatthew Dillon 	 *	scripts differently.
31526f9a767SRodney W. Grimes 	 */
316d323ddf3SMatthew Dillon 	error = -1;
317d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
318d323ddf3SMatthew Dillon 		error = img_first(imgp);
319d323ddf3SMatthew Dillon 
320d323ddf3SMatthew Dillon 	/*
321d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
322d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
323d323ddf3SMatthew Dillon 	 *	and an error otherwise.
324d323ddf3SMatthew Dillon 	 */
325d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
326d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
327d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
328d323ddf3SMatthew Dillon 			continue;
329d323ddf3SMatthew Dillon 		}
330c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
331d323ddf3SMatthew Dillon 	}
332d323ddf3SMatthew Dillon 
333d323ddf3SMatthew Dillon 	if (error) {
334619eb6e5SJeff Roberson 		if (error == -1) {
335619eb6e5SJeff Roberson 			if (textset == 0)
336619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
337d323ddf3SMatthew Dillon 			error = ENOEXEC;
338619eb6e5SJeff Roberson 		}
33926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
340d323ddf3SMatthew Dillon 	}
341d323ddf3SMatthew Dillon 
342d323ddf3SMatthew Dillon 	/*
343d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
344d323ddf3SMatthew Dillon 	 * activate the interpreter.
345d323ddf3SMatthew Dillon 	 */
346c52007c2SDavid Greenman 	if (imgp->interpreted) {
3471616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
348619eb6e5SJeff Roberson 		/*
349619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
350619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
351619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
352619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
353619eb6e5SJeff Roberson 		 */
354619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
355762e6b85SEivind Eklund 		/* free name buffer and old vnode */
356762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
357670cb89bSRobert Watson #ifdef MAC
358670cb89bSRobert Watson 		mac_init_vnode_label(&interplabel);
359670cb89bSRobert Watson 		mac_copy_vnode_label(&ndp->ni_vp->v_label, &interplabel);
360670cb89bSRobert Watson 		interplabelvalid = 1;
361670cb89bSRobert Watson #endif
362619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
3630b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
3640b2ed1aeSJeff Roberson 		imgp->object = NULL;
36526f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
366b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
367b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
36826f9a767SRodney W. Grimes 		goto interpret;
36926f9a767SRodney W. Grimes 	}
37026f9a767SRodney W. Grimes 
37126f9a767SRodney W. Grimes 	/*
37226f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
37326f9a767SRodney W. Grimes 	 */
3743ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
3753ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
3763ebc1248SPeter Wemm 	else
377c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
37826f9a767SRodney W. Grimes 
37926f9a767SRodney W. Grimes 	/*
3801e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
3811e1e0b44SSøren Schmidt 	 * let it do the stack setup.
3821e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
38326f9a767SRodney W. Grimes 	 */
3841e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
385c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
3861e1e0b44SSøren Schmidt 	else
387c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
38826f9a767SRodney W. Grimes 
389a78e8d2aSDavid Greenman 	/*
390a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
391a78e8d2aSDavid Greenman 	 * be shared after an exec.
392a78e8d2aSDavid Greenman 	 */
393426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
394a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
395a78e8d2aSDavid Greenman 		struct filedesc *tmp;
396a78e8d2aSDavid Greenman 
397c522c1bfSAlfred Perlstein 		tmp = fdcopy(td->td_proc->p_fd);
398426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
399b40ce416SJulian Elischer 		fdfree(td);
400a78e8d2aSDavid Greenman 		p->p_fd = tmp;
401426da3bcSAlfred Perlstein 	} else
402426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
403a78e8d2aSDavid Greenman 
404333ea485SGuido van Rooij 	/*
4059b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
4069b3b1c5fSJohn Baldwin 	 */
4079b3b1c5fSJohn Baldwin 	newcred = crget();
4081419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
4099b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
4109b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
4119b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
4129b3b1c5fSJohn Baldwin 
4139b3b1c5fSJohn Baldwin 	/* close files on exec */
4149b3b1c5fSJohn Baldwin 	fdcloseexec(td);
4159b3b1c5fSJohn Baldwin 
416619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
417619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
418619eb6e5SJeff Roberson 
4199b3b1c5fSJohn Baldwin 	/*
420333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
4218688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
422b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
423333ea485SGuido van Rooij 	 * reset.
424333ea485SGuido van Rooij 	 */
4259b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
4269b3b1c5fSJohn Baldwin 	mp_fixme("procsig needs a lock");
427333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
4289b3b1c5fSJohn Baldwin 		oldprocsig = p->p_procsig;
4299b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
430333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
431333ea485SGuido van Rooij 		    M_SUBPROC, M_WAITOK);
4329b3b1c5fSJohn Baldwin 		bcopy(oldprocsig, newprocsig, sizeof(*newprocsig));
4339b3b1c5fSJohn Baldwin 		newprocsig->ps_refcnt = 1;
4349b3b1c5fSJohn Baldwin 		oldprocsig->ps_refcnt--;
4359b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
436333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
437b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
438b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
439333ea485SGuido van Rooij 
440b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
441b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
442333ea485SGuido van Rooij 	}
443fdf4e8b3SWarner Losh 	/* Stop profiling */
444fdf4e8b3SWarner Losh 	stopprofclock(p);
445fdf4e8b3SWarner Losh 
44626f9a767SRodney W. Grimes 	/* reset caught signals */
44726f9a767SRodney W. Grimes 	execsigs(p);
44826f9a767SRodney W. Grimes 
44926f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
45026f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
45126f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
45226f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
45326f9a767SRodney W. Grimes 
45426f9a767SRodney W. Grimes 	/*
45524b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
456dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
45726f9a767SRodney W. Grimes 	 */
45826f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
45926f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
46026f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
4617f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
46226f9a767SRodney W. Grimes 	}
46326f9a767SRodney W. Grimes 
46426f9a767SRodney W. Grimes 	/*
465a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
466a78e8d2aSDavid Greenman 	 *
467a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
468a78e8d2aSDavid Greenman 	 * the process is being traced.
469670cb89bSRobert Watson 	 *
470670cb89bSRobert Watson 	 * XXXMAC: For the time being, use NOSUID to also prohibit
471670cb89bSRobert Watson 	 * transitions on the file system.
472c52007c2SDavid Greenman 	 */
473b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
474d06c0d4dSRobert Watson 	credential_changing = 0;
475d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
476d06c0d4dSRobert Watson 	    attr.va_uid;
477d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
478d06c0d4dSRobert Watson 	    attr.va_gid;
479ccafe7ebSRobert Watson #ifdef MAC
480670cb89bSRobert Watson 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
481670cb89bSRobert Watson 	    interplabelvalid ? &interplabel : NULL, imgp);
482ccafe7ebSRobert Watson 	credential_changing |= will_transition;
483ccafe7ebSRobert Watson #endif
484d06c0d4dSRobert Watson 
485d06c0d4dSRobert Watson 	if (credential_changing &&
486a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
487c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
488c52007c2SDavid Greenman 		/*
489c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
490b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
491b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
49226f9a767SRodney W. Grimes 		 */
493b85db196SPeter Wemm 		setsugid(p);
4946c84de02SJohn Baldwin #ifdef KTRACE
49544731cabSJohn Baldwin 		if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) {
4966c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
49779deba82SMatthew Dillon 			p->p_traceflag = 0;
4989b3b1c5fSJohn Baldwin 			tracevp = p->p_tracep;
4999b3b1c5fSJohn Baldwin 			p->p_tracep = NULL;
5006c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
50126f9a767SRodney W. Grimes 		}
5026c84de02SJohn Baldwin #endif
50328b325aaSDon Lewis 		/*
504c1e2d386SNate Lawson 		 * Close any file descriptors 0..2 that reference procfs,
505c1e2d386SNate Lawson 		 * then make sure file descriptors 0..2 are in use.
50628b325aaSDon Lewis 		 *
507c1e2d386SNate Lawson 		 * setugidsafety() may call closef() and then pfind()
508c1e2d386SNate Lawson 		 * which may grab the process lock.
50928b325aaSDon Lewis 		 * fdcheckstd() may call falloc() which may block to
51028b325aaSDon Lewis 		 * allocate memory, so temporarily drop the process lock.
51128b325aaSDon Lewis 		 */
51228b325aaSDon Lewis 		PROC_UNLOCK(p);
513c1e2d386SNate Lawson 		setugidsafety(td);
514e983a376SJacques Vidrine 		error = fdcheckstd(td);
51563c9e754SJohn Baldwin 		if (error != 0)
51669be5db9SAlfred Perlstein 			goto done1;
517e1b1aa3bSJohn Baldwin 		PROC_LOCK(p);
518c52007c2SDavid Greenman 		/*
519c52007c2SDavid Greenman 		 * Set the new credentials.
520c52007c2SDavid Greenman 		 */
5219b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
522c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
5231419eacbSAlfred Perlstein 			change_euid(newcred, euip);
524c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
525b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
526ccafe7ebSRobert Watson #ifdef MAC
527670cb89bSRobert Watson 		if (will_transition) {
528670cb89bSRobert Watson 			mac_execve_transition(oldcred, newcred, imgp->vp,
529670cb89bSRobert Watson 			    interplabelvalid ? &interplabel : NULL, imgp);
530670cb89bSRobert Watson 		}
531ccafe7ebSRobert Watson #endif
5329b3b1c5fSJohn Baldwin 		/*
5339b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
534ccafe7ebSRobert Watson 		 *
535ccafe7ebSRobert Watson 		 * XXXMAC: Note that the current logic will save the
536ccafe7ebSRobert Watson 		 * uid and gid if a MAC domain transition occurs, even
537ccafe7ebSRobert Watson 		 * though maybe it shouldn't.
5389b3b1c5fSJohn Baldwin 		 */
5399b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
5409b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
5419b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
5429b3b1c5fSJohn Baldwin 		newcred = NULL;
543c52007c2SDavid Greenman 	} else {
544b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
545b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
546c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
54726f9a767SRodney W. Grimes 		/*
548c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
549b1fc0ec1SRobert Watson 		 *
550b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
5519b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
5529b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
5539b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
5549b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
5559b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
5569b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
5579b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
55826f9a767SRodney W. Grimes 		 */
559b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
560b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
5619b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
562b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
563b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
564b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
5659b3b1c5fSJohn Baldwin 			newcred = NULL;
5669b3b1c5fSJohn Baldwin 		}
567b1fc0ec1SRobert Watson 	}
56826f9a767SRodney W. Grimes 
56926f9a767SRodney W. Grimes 	/*
570619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
571619eb6e5SJeff Roberson 	 * to locking the proc lock.
5722a531c80SDavid Greenman 	 */
5739b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
5742a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
5752a531c80SDavid Greenman 
5762a531c80SDavid Greenman 	/*
5779ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
5789ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
579cb679c38SJonathan Lemon 	 */
580cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
5819ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
582cb679c38SJonathan Lemon 
583cb679c38SJonathan Lemon 	/*
58426f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
58526f9a767SRodney W. Grimes 	 * can be set before the program executes.
58626f9a767SRodney W. Grimes 	 */
58726f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
58826f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
58926f9a767SRodney W. Grimes 
59026f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
59126f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
59226f9a767SRodney W. Grimes 
593b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
5949b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
595b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
596b9df5231SPoul-Henning Kamp 
597e1b1aa3bSJohn Baldwin 	/* Cache arguments if they fit inside our allowance */
598e1b1aa3bSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
599e1b1aa3bSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
600e1b1aa3bSJohn Baldwin 		p->p_args = newargs;
601e1b1aa3bSJohn Baldwin 		newargs = NULL;
602e1b1aa3bSJohn Baldwin 	}
603e1b1aa3bSJohn Baldwin 	PROC_UNLOCK(p);
604e1b1aa3bSJohn Baldwin 
605e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
6063ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
6073ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
6083ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
6093ebc1248SPeter Wemm 	else
610bafbd492SJake Burkholder 		exec_setregs(td, imgp->entry_addr,
611bafbd492SJake Burkholder 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
612e913ca22SDoug Rabson 
61369be5db9SAlfred Perlstein done1:
6149b3b1c5fSJohn Baldwin 	/*
6159b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
6169b3b1c5fSJohn Baldwin 	 */
6171419eacbSAlfred Perlstein 	uifree(euip);
6189b3b1c5fSJohn Baldwin 	if (newcred == NULL)
6199b3b1c5fSJohn Baldwin 		crfree(oldcred);
6209b3b1c5fSJohn Baldwin 	else
6219b3b1c5fSJohn Baldwin 		crfree(newcred);
6229b3b1c5fSJohn Baldwin 	/*
6239b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
6249b3b1c5fSJohn Baldwin 	 */
6259b3b1c5fSJohn Baldwin 	if (textvp != NULL)
6269b3b1c5fSJohn Baldwin 		vrele(textvp);
627619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
628619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
6296c84de02SJohn Baldwin #ifdef KTRACE
6309b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
6319b3b1c5fSJohn Baldwin 		vrele(tracevp);
6326c84de02SJohn Baldwin #endif
63369be5db9SAlfred Perlstein 	if (oldargs != NULL)
6349b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
63569be5db9SAlfred Perlstein 	if (newargs != NULL)
63669be5db9SAlfred Perlstein 		pargs_drop(newargs);
637b9df5231SPoul-Henning Kamp 
6381616db3cSJohn Dyson exec_fail_dealloc:
6391616db3cSJohn Dyson 
64026f9a767SRodney W. Grimes 	/*
64126f9a767SRodney W. Grimes 	 * free various allocated resources
64226f9a767SRodney W. Grimes 	 */
6431616db3cSJohn Dyson 	if (imgp->firstpage)
6441616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
64526f9a767SRodney W. Grimes 
6469c0fed3dSDoug Rabson 	if (imgp->vp) {
647762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
648619eb6e5SJeff Roberson 		vput(imgp->vp);
649a3cf6ebaSDavid Greenman 	}
65026f9a767SRodney W. Grimes 
651a9a08882SJeff Roberson 	if (imgp->stringbase != NULL)
652a9a08882SJeff Roberson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
653a9a08882SJeff Roberson 		    ARG_MAX + PAGE_SIZE);
654a9a08882SJeff Roberson 
6550b2ed1aeSJeff Roberson 	if (imgp->object)
6560b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
6570b2ed1aeSJeff Roberson 
658d1989db5SRobert Drehmel 	if (error == 0) {
659d1989db5SRobert Drehmel 		/*
660d1989db5SRobert Drehmel 		 * Stop the process here if its stop event mask has
661d1989db5SRobert Drehmel 		 * the S_EXEC bit set.
662d1989db5SRobert Drehmel 		 */
663d1989db5SRobert Drehmel 		STOPEVENT(p, S_EXEC, 0);
664116734c4SMatthew Dillon 		goto done2;
665d1989db5SRobert Drehmel 	}
6661616db3cSJohn Dyson 
66726f9a767SRodney W. Grimes exec_fail:
6689ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
6699ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
6709ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
6719ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
6729ca45e81SDag-Erling Smørgrav 
673c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
67426f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
675670cb89bSRobert Watson #ifdef MAC
676670cb89bSRobert Watson 		mac_execve_exit(imgp);
677670cb89bSRobert Watson 		if (interplabelvalid)
678670cb89bSRobert Watson 			mac_destroy_vnode_label(&interplabel);
679670cb89bSRobert Watson #endif
680b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
68126f9a767SRodney W. Grimes 		/* NOT REACHED */
682116734c4SMatthew Dillon 		error = 0;
68326f9a767SRodney W. Grimes 	}
684116734c4SMatthew Dillon done2:
685670cb89bSRobert Watson #ifdef MAC
686670cb89bSRobert Watson 	mac_execve_exit(imgp);
687670cb89bSRobert Watson 	if (interplabelvalid)
688670cb89bSRobert Watson 		mac_destroy_vnode_label(&interplabel);
689670cb89bSRobert Watson #endif
690116734c4SMatthew Dillon 	mtx_unlock(&Giant);
691116734c4SMatthew Dillon 	return (error);
69226f9a767SRodney W. Grimes }
69326f9a767SRodney W. Grimes 
694450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_
695450ffb44SRobert Watson struct execve_args {
696450ffb44SRobert Watson         char    *fname;
697450ffb44SRobert Watson         char    **argv;
698450ffb44SRobert Watson         char    **envv;
699450ffb44SRobert Watson };
700450ffb44SRobert Watson #endif
701450ffb44SRobert Watson 
702450ffb44SRobert Watson /*
703450ffb44SRobert Watson  * MPSAFE
704450ffb44SRobert Watson  */
705450ffb44SRobert Watson int
706450ffb44SRobert Watson execve(td, uap)
707450ffb44SRobert Watson 	struct thread *td;
708450ffb44SRobert Watson 	struct execve_args /* {
709b80521feSAlfred Perlstein 		char *fname;
710b80521feSAlfred Perlstein 		char **argv;
711b80521feSAlfred Perlstein 		char **envv;
712450ffb44SRobert Watson 	} */ *uap;
713450ffb44SRobert Watson {
714450ffb44SRobert Watson 
715670cb89bSRobert Watson 	return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL));
716670cb89bSRobert Watson }
717670cb89bSRobert Watson 
718670cb89bSRobert Watson #ifndef _SYS_SYSPROTO_H_
719670cb89bSRobert Watson struct __mac_execve_args {
720670cb89bSRobert Watson 	char	*fname;
721670cb89bSRobert Watson 	char	**argv;
722670cb89bSRobert Watson 	char	**envv;
723670cb89bSRobert Watson 	struct mac	*mac_p;
724670cb89bSRobert Watson };
725670cb89bSRobert Watson #endif
726670cb89bSRobert Watson 
727670cb89bSRobert Watson /*
728670cb89bSRobert Watson  * MPSAFE
729670cb89bSRobert Watson  */
730670cb89bSRobert Watson int
731670cb89bSRobert Watson __mac_execve(td, uap)
732670cb89bSRobert Watson 	struct thread *td;
733670cb89bSRobert Watson 	struct __mac_execve_args /* {
734b80521feSAlfred Perlstein 		char *fname;
735b80521feSAlfred Perlstein 		char **argv;
736b80521feSAlfred Perlstein 		char **envv;
737b80521feSAlfred Perlstein 		struct mac *mac_p;
738670cb89bSRobert Watson 	} */ *uap;
739670cb89bSRobert Watson {
740670cb89bSRobert Watson 
7410c93266bSRobert Watson #ifdef MAC
742670cb89bSRobert Watson 	return (kern_execve(td, uap->fname, uap->argv, uap->envv,
743670cb89bSRobert Watson 	    uap->mac_p));
7440c93266bSRobert Watson #else
7450c93266bSRobert Watson 	return (ENOSYS);
7460c93266bSRobert Watson #endif
747450ffb44SRobert Watson }
748450ffb44SRobert Watson 
7491616db3cSJohn Dyson int
7501616db3cSJohn Dyson exec_map_first_page(imgp)
7511616db3cSJohn Dyson 	struct image_params *imgp;
7521616db3cSJohn Dyson {
753d8aad40cSJohn Baldwin 	int rv, i;
75495461b45SJohn Dyson 	int initial_pagein;
75595461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
7561616db3cSJohn Dyson 	vm_object_t object;
7571616db3cSJohn Dyson 
7580cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
7591616db3cSJohn Dyson 
7601616db3cSJohn Dyson 	if (imgp->firstpage) {
7611616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
7621616db3cSJohn Dyson 	}
7631616db3cSJohn Dyson 
7649ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
7651616db3cSJohn Dyson 
76695461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
7671616db3cSJohn Dyson 
76895461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
76995461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
77095461b45SJohn Dyson 		if (initial_pagein > object->size)
77195461b45SJohn Dyson 			initial_pagein = object->size;
77295461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
773d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
77495461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
77595461b45SJohn Dyson 					break;
77695461b45SJohn Dyson 				if (ma[i]->valid)
77795461b45SJohn Dyson 					break;
778ee113343SAlan Cox 				vm_page_lock_queues();
779e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
780ee113343SAlan Cox 				vm_page_unlock_queues();
78195461b45SJohn Dyson 			} else {
782ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
783ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
78495461b45SJohn Dyson 				if (ma[i] == NULL)
78595461b45SJohn Dyson 					break;
78695461b45SJohn Dyson 			}
78795461b45SJohn Dyson 		}
78895461b45SJohn Dyson 		initial_pagein = i;
7891616db3cSJohn Dyson 
79095461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
79195461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
79295461b45SJohn Dyson 
793ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
794ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
795ecbb00a2SDoug Rabson 			if (ma[0]) {
79697646f56SAlan Cox 				vm_page_lock_queues();
7974fec79beSAlan Cox 				pmap_remove_all(ma[0]);
7988f9110f6SJohn Dyson 				vm_page_free(ma[0]);
79997646f56SAlan Cox 				vm_page_unlock_queues();
800ecbb00a2SDoug Rabson 			}
801a7cddfedSJake Burkholder 			return (EIO);
8021616db3cSJohn Dyson 		}
8031616db3cSJohn Dyson 	}
80497646f56SAlan Cox 	vm_page_lock_queues();
80595461b45SJohn Dyson 	vm_page_wire(ma[0]);
806e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
80797646f56SAlan Cox 	vm_page_unlock_queues();
8081616db3cSJohn Dyson 
809ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
81095461b45SJohn Dyson 	imgp->firstpage = ma[0];
8111616db3cSJohn Dyson 
812a7cddfedSJake Burkholder 	return (0);
8131616db3cSJohn Dyson }
8141616db3cSJohn Dyson 
8151616db3cSJohn Dyson void
8161616db3cSJohn Dyson exec_unmap_first_page(imgp)
8171616db3cSJohn Dyson 	struct image_params *imgp;
8181616db3cSJohn Dyson {
8190cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
82023955314SAlfred Perlstein 
8211616db3cSJohn Dyson 	if (imgp->firstpage) {
822ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
82397646f56SAlan Cox 		vm_page_lock_queues();
82473007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
82597646f56SAlan Cox 		vm_page_unlock_queues();
8261616db3cSJohn Dyson 		imgp->firstpage = NULL;
8271616db3cSJohn Dyson 	}
8281616db3cSJohn Dyson }
8291616db3cSJohn Dyson 
83026f9a767SRodney W. Grimes /*
83126f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
83226f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
83326f9a767SRodney W. Grimes  *	automatically in trap.c.
83426f9a767SRodney W. Grimes  */
83526f9a767SRodney W. Grimes int
83605ba50f5SJake Burkholder exec_new_vmspace(imgp, sv)
837c52007c2SDavid Greenman 	struct image_params *imgp;
83805ba50f5SJake Burkholder 	struct sysentvec *sv;
83926f9a767SRodney W. Grimes {
84026f9a767SRodney W. Grimes 	int error;
8416f5dafeaSAlan Cox 	struct execlist *ep;
8425e20c11fSAlan Cox 	struct proc *p = imgp->proc;
8435e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
84405ba50f5SJake Burkholder 	vm_offset_t stack_addr;
84505ba50f5SJake Burkholder 	vm_map_t map;
84626f9a767SRodney W. Grimes 
8470cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8480cddd8f0SMatthew Dillon 
84905ba50f5SJake Burkholder 	stack_addr = sv->sv_usrstack - maxssiz;
8503ebc1248SPeter Wemm 
851c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
85226f9a767SRodney W. Grimes 
853af9ec885SJohn Dyson 	/*
8546f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
8556f5dafeaSAlan Cox 	 */
8566f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
8575e20c11fSAlan Cox 		(*ep->function)(p);
8586f5dafeaSAlan Cox 
8596f5dafeaSAlan Cox 	/*
860af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
861af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
862af9ec885SJohn Dyson 	 * not disrupted
863af9ec885SJohn Dyson 	 */
86405ba50f5SJake Burkholder 	map = &vmspace->vm_map;
86505ba50f5SJake Burkholder 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
86605ba50f5SJake Burkholder 	    vm_map_max(map) == sv->sv_maxuser) {
8673db161e0SMatthew Dillon 		shmexit(vmspace);
8682d21129dSAlan Cox 		vm_page_lock_queues();
86905ba50f5SJake Burkholder 		pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
87005ba50f5SJake Burkholder 		    vm_map_max(map));
8712d21129dSAlan Cox 		vm_page_unlock_queues();
87205ba50f5SJake Burkholder 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
873af9ec885SJohn Dyson 	} else {
87405ba50f5SJake Burkholder 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
8755e20c11fSAlan Cox 		vmspace = p->p_vmspace;
87605ba50f5SJake Burkholder 		map = &vmspace->vm_map;
877af9ec885SJohn Dyson 	}
87826f9a767SRodney W. Grimes 
87926f9a767SRodney W. Grimes 	/* Allocate a new stack */
88005ba50f5SJake Burkholder 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
88105ba50f5SJake Burkholder 	    sv->sv_stackprot, VM_PROT_ALL, 0);
8822267af78SJulian Elischer 	if (error)
8832267af78SJulian Elischer 		return (error);
8842267af78SJulian Elischer 
88563c47a5cSDoug Rabson #ifdef __ia64__
88663c47a5cSDoug Rabson 	{
88763c47a5cSDoug Rabson 		/*
88863c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
88963c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
89063c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
89163c47a5cSDoug Rabson 		 */
89263c47a5cSDoug Rabson 		vm_offset_t bsaddr;
89305ba50f5SJake Burkholder 		bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz;
89405ba50f5SJake Burkholder 		error = vm_map_find(map, 0, 0, &bsaddr,
89581f223caSJake Burkholder 		    regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
8965e20c11fSAlan Cox 		FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
89763c47a5cSDoug Rabson 	}
89863c47a5cSDoug Rabson #endif
89963c47a5cSDoug Rabson 
9002267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
9012267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
9022267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
9032267af78SJulian Elischer 	 */
904cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
90505ba50f5SJake Burkholder 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
90626f9a767SRodney W. Grimes 
90726f9a767SRodney W. Grimes 	return (0);
90826f9a767SRodney W. Grimes }
90926f9a767SRodney W. Grimes 
91026f9a767SRodney W. Grimes /*
91126f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
91226f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
91326f9a767SRodney W. Grimes  */
91426f9a767SRodney W. Grimes int
915c52007c2SDavid Greenman exec_extract_strings(imgp)
916c52007c2SDavid Greenman 	struct image_params *imgp;
91726f9a767SRodney W. Grimes {
91826f9a767SRodney W. Grimes 	char	**argv, **envv;
91926f9a767SRodney W. Grimes 	char	*argp, *envp;
920ecbb00a2SDoug Rabson 	int	error;
921ecbb00a2SDoug Rabson 	size_t	length;
92226f9a767SRodney W. Grimes 
92326f9a767SRodney W. Grimes 	/*
92426f9a767SRodney W. Grimes 	 * extract arguments first
92526f9a767SRodney W. Grimes 	 */
92626f9a767SRodney W. Grimes 
927450ffb44SRobert Watson 	argv = imgp->userspace_argv;
92826f9a767SRodney W. Grimes 
9290fd1a014SDavid Greenman 	if (argv) {
930aae0aa45SBruce Evans 		argp = (caddr_t)(intptr_t)fuword(argv);
9315cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t)-1)
9325cf3d12cSAndrey A. Chernov 			return (EFAULT);
9335cf3d12cSAndrey A. Chernov 		if (argp)
9345cf3d12cSAndrey A. Chernov 			argv++;
9355cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
9365cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
9375cf3d12cSAndrey A. Chernov 		if (argp) {
9385cf3d12cSAndrey A. Chernov 			do {
93926f9a767SRodney W. Grimes 				if (argp == (caddr_t)-1)
94026f9a767SRodney W. Grimes 					return (EFAULT);
941c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
942c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
9430fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
94426f9a767SRodney W. Grimes 						return (E2BIG);
9450fd1a014SDavid Greenman 					return (error);
9460fd1a014SDavid Greenman 				}
947c52007c2SDavid Greenman 				imgp->stringspace -= length;
948c52007c2SDavid Greenman 				imgp->stringp += length;
949c52007c2SDavid Greenman 				imgp->argc++;
950aae0aa45SBruce Evans 			} while ((argp = (caddr_t)(intptr_t)fuword(argv++)));
95126f9a767SRodney W. Grimes 		}
9520fd1a014SDavid Greenman 	}
95326f9a767SRodney W. Grimes 
954b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
955b9df5231SPoul-Henning Kamp 
95626f9a767SRodney W. Grimes 	/*
95726f9a767SRodney W. Grimes 	 * extract environment strings
95826f9a767SRodney W. Grimes 	 */
95926f9a767SRodney W. Grimes 
960450ffb44SRobert Watson 	envv = imgp->userspace_envv;
96126f9a767SRodney W. Grimes 
9620fd1a014SDavid Greenman 	if (envv) {
963aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
96426f9a767SRodney W. Grimes 			if (envp == (caddr_t)-1)
96526f9a767SRodney W. Grimes 				return (EFAULT);
966c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
967c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
9680fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
96926f9a767SRodney W. Grimes 					return (E2BIG);
9700fd1a014SDavid Greenman 				return (error);
9710fd1a014SDavid Greenman 			}
972c52007c2SDavid Greenman 			imgp->stringspace -= length;
973c52007c2SDavid Greenman 			imgp->stringp += length;
974c52007c2SDavid Greenman 			imgp->envc++;
97526f9a767SRodney W. Grimes 		}
9760fd1a014SDavid Greenman 	}
97726f9a767SRodney W. Grimes 
97826f9a767SRodney W. Grimes 	return (0);
97926f9a767SRodney W. Grimes }
98026f9a767SRodney W. Grimes 
98126f9a767SRodney W. Grimes /*
98226f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
98326f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
98426f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
98526f9a767SRodney W. Grimes  */
986654f6be1SBruce Evans register_t *
987c52007c2SDavid Greenman exec_copyout_strings(imgp)
988c52007c2SDavid Greenman 	struct image_params *imgp;
98926f9a767SRodney W. Grimes {
99026f9a767SRodney W. Grimes 	int argc, envc;
99126f9a767SRodney W. Grimes 	char **vectp;
99226f9a767SRodney W. Grimes 	char *stringp, *destp;
993654f6be1SBruce Evans 	register_t *stack_base;
99493f6448cSDavid Greenman 	struct ps_strings *arginfo;
995f3bec5d7SJake Burkholder 	struct proc *p;
996d66a5066SPeter Wemm 	int szsigcode;
99726f9a767SRodney W. Grimes 
99826f9a767SRodney W. Grimes 	/*
99926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
1000d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
100126f9a767SRodney W. Grimes 	 */
1002f3bec5d7SJake Burkholder 	p = imgp->proc;
1003f3bec5d7SJake Burkholder 	szsigcode = 0;
100405ba50f5SJake Burkholder 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1005f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
1006f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
1007d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
10081ed012f9SPeter Wemm 	    roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
10091ed012f9SPeter Wemm 
101026f9a767SRodney W. Grimes 	/*
1011d66a5066SPeter Wemm 	 * install sigcode
1012d66a5066SPeter Wemm 	 */
1013d66a5066SPeter Wemm 	if (szsigcode)
1014f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1015f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
1016d66a5066SPeter Wemm 
1017d66a5066SPeter Wemm 	/*
1018e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
1019e1743d02SSøren Schmidt 	 * on the stack.
1020e1743d02SSøren Schmidt 	 */
1021b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
1022e1743d02SSøren Schmidt 		/*
1023b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1024b9a22da4STakanori Watanabe 		 * lower compatibility.
1025b9a22da4STakanori Watanabe 		 */
1026ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1027ca0387efSJake Burkholder 		    (AT_COUNT * 2);
1028b9a22da4STakanori Watanabe 		/*
1029b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1030b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
1031b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
1032e1743d02SSøren Schmidt 		 */
1033e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
1034b9a22da4STakanori Watanabe 		    imgp->auxarg_size) * sizeof(char *));
1035b9a22da4STakanori Watanabe 
1036b9a22da4STakanori Watanabe 	} else
1037e1743d02SSøren Schmidt 		/*
1038b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1039b9a22da4STakanori Watanabe 		 * the arg and env vector sets
104026f9a767SRodney W. Grimes 		 */
1041ca0387efSJake Burkholder 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) *
1042ca0387efSJake Burkholder 		    sizeof(char *));
104326f9a767SRodney W. Grimes 
104426f9a767SRodney W. Grimes 	/*
104526f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
104626f9a767SRodney W. Grimes 	 */
1047654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
104826f9a767SRodney W. Grimes 
1049c52007c2SDavid Greenman 	stringp = imgp->stringbase;
1050c52007c2SDavid Greenman 	argc = imgp->argc;
1051c52007c2SDavid Greenman 	envc = imgp->envc;
105226f9a767SRodney W. Grimes 
105393f6448cSDavid Greenman 	/*
10543aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
105593f6448cSDavid Greenman 	 */
1056c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
105793f6448cSDavid Greenman 
105893f6448cSDavid Greenman 	/*
10593aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
10603aed948bSDavid Greenman 	 */
1061aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
10623aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
10633aed948bSDavid Greenman 
10643aed948bSDavid Greenman 	/*
10653aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
106693f6448cSDavid Greenman 	 */
106726f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
1068aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10693aed948bSDavid Greenman 		while (*stringp++ != 0)
10703aed948bSDavid Greenman 			destp++;
10713aed948bSDavid Greenman 		destp++;
107226f9a767SRodney W. Grimes 	}
107326f9a767SRodney W. Grimes 
10741a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
10756ab46d52SBruce Evans 	suword(vectp++, 0);
107626f9a767SRodney W. Grimes 
1077aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
10783aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
107993f6448cSDavid Greenman 
108093f6448cSDavid Greenman 	/*
10813aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
108293f6448cSDavid Greenman 	 */
108326f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
1084aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10853aed948bSDavid Greenman 		while (*stringp++ != 0)
10863aed948bSDavid Greenman 			destp++;
10873aed948bSDavid Greenman 		destp++;
108826f9a767SRodney W. Grimes 	}
108926f9a767SRodney W. Grimes 
109026f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
10916ab46d52SBruce Evans 	suword(vectp, 0);
109226f9a767SRodney W. Grimes 
109326f9a767SRodney W. Grimes 	return (stack_base);
109426f9a767SRodney W. Grimes }
109526f9a767SRodney W. Grimes 
109626f9a767SRodney W. Grimes /*
109726f9a767SRodney W. Grimes  * Check permissions of file to execute.
1098cf64863aSRobert Watson  *	Called with imgp->vp locked.
109926f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
110026f9a767SRodney W. Grimes  */
1101c8a79999SPeter Wemm int
1102c52007c2SDavid Greenman exec_check_permissions(imgp)
1103c52007c2SDavid Greenman 	struct image_params *imgp;
110426f9a767SRodney W. Grimes {
1105c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
1106c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
1107a854ed98SJohn Baldwin 	struct thread *td;
110826f9a767SRodney W. Grimes 	int error;
110926f9a767SRodney W. Grimes 
1110a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
1111339b79b9SRobert Watson 
1112339b79b9SRobert Watson #ifdef MAC
1113670cb89bSRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1114339b79b9SRobert Watson 	if (error)
1115339b79b9SRobert Watson 		return (error);
1116339b79b9SRobert Watson #endif
1117339b79b9SRobert Watson 
111826f9a767SRodney W. Grimes 	/* Get file attributes */
1119a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
112026f9a767SRodney W. Grimes 	if (error)
112126f9a767SRodney W. Grimes 		return (error);
112226f9a767SRodney W. Grimes 
112326f9a767SRodney W. Grimes 	/*
112426f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
112526f9a767SRodney W. Grimes 	 *	file resides on.
112626f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
112726f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
112826f9a767SRodney W. Grimes 	 *	file really is executable.
112926f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
113026f9a767SRodney W. Grimes 	 */
1131c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
113226f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
1133a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
113426f9a767SRodney W. Grimes 		return (EACCES);
113526f9a767SRodney W. Grimes 
113626f9a767SRodney W. Grimes 	/*
113726f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
113826f9a767SRodney W. Grimes 	 */
113926f9a767SRodney W. Grimes 	if (attr->va_size == 0)
114026f9a767SRodney W. Grimes 		return (ENOEXEC);
114126f9a767SRodney W. Grimes 
114226f9a767SRodney W. Grimes 	/*
114326f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
114426f9a767SRodney W. Grimes 	 */
1145a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
114626f9a767SRodney W. Grimes 	if (error)
114726f9a767SRodney W. Grimes 		return (error);
114826f9a767SRodney W. Grimes 
11496d5a0a8cSDavid Greenman 	/*
11506d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
11516d5a0a8cSDavid Greenman 	 * if there are any.
11526d5a0a8cSDavid Greenman 	 */
11536d5a0a8cSDavid Greenman 	if (vp->v_writecount)
11546d5a0a8cSDavid Greenman 		return (ETXTBSY);
11556d5a0a8cSDavid Greenman 
11566d5a0a8cSDavid Greenman 	/*
11576d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
11586d5a0a8cSDavid Greenman 	 * general case).
11596d5a0a8cSDavid Greenman 	 */
1160a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
116126f9a767SRodney W. Grimes 	return (error);
1162df8bae1dSRodney W. Grimes }
1163aa855a59SPeter Wemm 
1164aa855a59SPeter Wemm /*
1165aa855a59SPeter Wemm  * Exec handler registration
1166aa855a59SPeter Wemm  */
1167aa855a59SPeter Wemm int
1168aa855a59SPeter Wemm exec_register(execsw_arg)
1169aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1170aa855a59SPeter Wemm {
1171aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1172aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1173aa855a59SPeter Wemm 
1174aa855a59SPeter Wemm 	if (execsw)
1175aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1176aa855a59SPeter Wemm 			count++;
1177aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1178aa855a59SPeter Wemm 	if (newexecsw == NULL)
1179a7cddfedSJake Burkholder 		return (ENOMEM);
1180aa855a59SPeter Wemm 	xs = newexecsw;
1181aa855a59SPeter Wemm 	if (execsw)
1182aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1183aa855a59SPeter Wemm 			*xs++ = *es;
1184aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1185aa855a59SPeter Wemm 	*xs = NULL;
1186aa855a59SPeter Wemm 	if (execsw)
1187aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1188aa855a59SPeter Wemm 	execsw = newexecsw;
1189a7cddfedSJake Burkholder 	return (0);
1190aa855a59SPeter Wemm }
1191aa855a59SPeter Wemm 
1192aa855a59SPeter Wemm int
1193aa855a59SPeter Wemm exec_unregister(execsw_arg)
1194aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1195aa855a59SPeter Wemm {
1196aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1197aa855a59SPeter Wemm 	int count = 1;
1198aa855a59SPeter Wemm 
1199aa855a59SPeter Wemm 	if (execsw == NULL)
1200aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1201aa855a59SPeter Wemm 
1202aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1203aa855a59SPeter Wemm 		if (*es == execsw_arg)
1204aa855a59SPeter Wemm 			break;
1205aa855a59SPeter Wemm 	}
1206aa855a59SPeter Wemm 	if (*es == NULL)
1207a7cddfedSJake Burkholder 		return (ENOENT);
1208aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1209aa855a59SPeter Wemm 		if (*es != execsw_arg)
1210aa855a59SPeter Wemm 			count++;
1211aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1212aa855a59SPeter Wemm 	if (newexecsw == NULL)
1213a7cddfedSJake Burkholder 		return (ENOMEM);
1214aa855a59SPeter Wemm 	xs = newexecsw;
1215aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1216aa855a59SPeter Wemm 		if (*es != execsw_arg)
1217aa855a59SPeter Wemm 			*xs++ = *es;
1218aa855a59SPeter Wemm 	*xs = NULL;
1219aa855a59SPeter Wemm 	if (execsw)
1220aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1221aa855a59SPeter Wemm 	execsw = newexecsw;
1222a7cddfedSJake Burkholder 	return (0);
1223aa855a59SPeter Wemm }
122421d56e9cSAlfred Perlstein 
122521d56e9cSAlfred Perlstein int
122621d56e9cSAlfred Perlstein at_exec(function)
122721d56e9cSAlfred Perlstein 	execlist_fn function;
122821d56e9cSAlfred Perlstein {
122921d56e9cSAlfred Perlstein 	struct execlist *ep;
123021d56e9cSAlfred Perlstein 
123121d56e9cSAlfred Perlstein #ifdef INVARIANTS
123221d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
123321d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
123421d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
123521d56e9cSAlfred Perlstein 		    function);
123621d56e9cSAlfred Perlstein #endif
123721d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
123821d56e9cSAlfred Perlstein 	if (ep == NULL)
123921d56e9cSAlfred Perlstein 		return (ENOMEM);
124021d56e9cSAlfred Perlstein 	ep->function = function;
124121d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
124221d56e9cSAlfred Perlstein 	return (0);
124321d56e9cSAlfred Perlstein }
124421d56e9cSAlfred Perlstein 
124521d56e9cSAlfred Perlstein /*
124621d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
124721d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
124821d56e9cSAlfred Perlstein  */
124921d56e9cSAlfred Perlstein int
125021d56e9cSAlfred Perlstein rm_at_exec(function)
125121d56e9cSAlfred Perlstein 	execlist_fn function;
125221d56e9cSAlfred Perlstein {
125321d56e9cSAlfred Perlstein 	struct execlist *ep;
125421d56e9cSAlfred Perlstein 
125521d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
125621d56e9cSAlfred Perlstein 		if (ep->function == function) {
125721d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
125821d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
125921d56e9cSAlfred Perlstein 			return (1);
126021d56e9cSAlfred Perlstein 		}
126121d56e9cSAlfred Perlstein 	}
126221d56e9cSAlfred Perlstein 	return (0);
126321d56e9cSAlfred Perlstein }
1264