xref: /freebsd/sys/kern/kern_exec.c (revision df7c361e641333af4c5f7285466f12c0731bb63e)
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  */
26df8bae1dSRodney W. Grimes 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
301e9b3d91SPeter Wemm #include "opt_ktrace.h"
31339b79b9SRobert Watson #include "opt_mac.h"
321e9b3d91SPeter Wemm 
33df8bae1dSRodney W. Grimes #include <sys/param.h>
3426f9a767SRodney W. Grimes #include <sys/systm.h>
3575b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
36fb919e4dSMark Murray #include <sys/lock.h>
37fb919e4dSMark Murray #include <sys/mutex.h>
38d2d3e875SBruce Evans #include <sys/sysproto.h>
3926f9a767SRodney W. Grimes #include <sys/signalvar.h>
4026f9a767SRodney W. Grimes #include <sys/kernel.h>
41339b79b9SRobert Watson #include <sys/mac.h>
4226f9a767SRodney W. Grimes #include <sys/mount.h>
43797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
44079cc25bSDavid Greenman #include <sys/fcntl.h>
4526f9a767SRodney W. Grimes #include <sys/acct.h>
4626f9a767SRodney W. Grimes #include <sys/exec.h>
4726f9a767SRodney W. Grimes #include <sys/imgact.h>
48e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4926f9a767SRodney W. Grimes #include <sys/wait.h>
50333ea485SGuido van Rooij #include <sys/malloc.h>
51a794e791SBruce Evans #include <sys/proc.h>
522a024a2bSSean Eric Fagan #include <sys/pioctl.h>
53a794e791SBruce Evans #include <sys/namei.h>
541e1e0b44SSøren Schmidt #include <sys/sysent.h>
55797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5699ac3bc8SPeter Wemm #include <sys/sysctl.h>
57333ea485SGuido van Rooij #include <sys/user.h>
58a794e791SBruce Evans #include <sys/vnode.h>
59c781aea8SPeter Wemm #ifdef KTRACE
60c781aea8SPeter Wemm #include <sys/ktrace.h>
61c781aea8SPeter Wemm #endif
6226f9a767SRodney W. Grimes 
6326f9a767SRodney W. Grimes #include <vm/vm.h>
64efeaf95aSDavid Greenman #include <vm/vm_param.h>
65efeaf95aSDavid Greenman #include <vm/pmap.h>
661616db3cSJohn Dyson #include <vm/vm_page.h>
67efeaf95aSDavid Greenman #include <vm/vm_map.h>
6826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
69efeaf95aSDavid Greenman #include <vm/vm_extern.h>
701ebd0c59SDavid Greenman #include <vm/vm_object.h>
711616db3cSJohn Dyson #include <vm/vm_pager.h>
7226f9a767SRodney W. Grimes 
7326f9a767SRodney W. Grimes #include <machine/reg.h>
7426f9a767SRodney W. Grimes 
75b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
76b9df5231SPoul-Henning Kamp 
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 
839701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
8405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
8505ba50f5SJake Burkholder     NULL, 0, sysctl_kern_ps_strings, "LU", "");
86486bddb0SDoug Rabson 
879701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
8805ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
8905ba50f5SJake Burkholder     NULL, 0, sysctl_kern_usrstack, "LU", "");
9099ac3bc8SPeter Wemm 
915dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
925dadd17bSJake Burkholder     NULL, 0, sysctl_kern_stackprot, "I", "");
935dadd17bSJake Burkholder 
94b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
95c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
969701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
97b9df5231SPoul-Henning Kamp 
98a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
99a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
100a8704f89SPoul-Henning Kamp 
10105ba50f5SJake Burkholder static int
10205ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
10305ba50f5SJake Burkholder {
10405ba50f5SJake Burkholder 	struct proc *p;
105df7c361eSPeter Wemm 	int error;
10605ba50f5SJake Burkholder 
10705ba50f5SJake Burkholder 	p = curproc;
108df7c361eSPeter Wemm #if defined(__amd64__) || defined(__ia64__)
109df7c361eSPeter Wemm 	if (req->oldlen == sizeof(unsigned int)) {
110df7c361eSPeter Wemm 		unsigned int val;
111df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_psstrings;
112df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
113df7c361eSPeter Wemm 	} else
114df7c361eSPeter Wemm #endif
115df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
116df7c361eSPeter Wemm 		   sizeof(p->p_sysent->sv_psstrings));
117df7c361eSPeter Wemm 	return error;
11805ba50f5SJake Burkholder }
11905ba50f5SJake Burkholder 
12005ba50f5SJake Burkholder static int
12105ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
12205ba50f5SJake Burkholder {
12305ba50f5SJake Burkholder 	struct proc *p;
124df7c361eSPeter Wemm 	int error;
12505ba50f5SJake Burkholder 
12605ba50f5SJake Burkholder 	p = curproc;
127df7c361eSPeter Wemm #if defined(__amd64__) || defined(__ia64__)
128df7c361eSPeter Wemm 	if (req->oldlen == sizeof(unsigned int)) {
129df7c361eSPeter Wemm 		unsigned int val;
130df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_usrstack;
131df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
132df7c361eSPeter Wemm 	} else
133df7c361eSPeter Wemm #endif
134df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
135df7c361eSPeter Wemm 		    sizeof(p->p_sysent->sv_usrstack));
136df7c361eSPeter Wemm 	return error;
13705ba50f5SJake Burkholder }
13805ba50f5SJake Burkholder 
1395dadd17bSJake Burkholder static int
1405dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
1415dadd17bSJake Burkholder {
1425dadd17bSJake Burkholder 	struct proc *p;
1435dadd17bSJake Burkholder 
1445dadd17bSJake Burkholder 	p = curproc;
1455dadd17bSJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
1465dadd17bSJake Burkholder 	    sizeof(p->p_sysent->sv_stackprot)));
1475dadd17bSJake Burkholder }
1485dadd17bSJake Burkholder 
14999ac3bc8SPeter Wemm /*
150aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
151aa855a59SPeter Wemm  * double pointer here.
152df8bae1dSRodney W. Grimes  */
153aa855a59SPeter Wemm static const struct execsw **execsw;
15426f9a767SRodney W. Grimes 
155ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
156ca46e90eSBruce Evans struct execve_args {
157ca46e90eSBruce Evans         char    *fname;
158ca46e90eSBruce Evans         char    **argv;
159ca46e90eSBruce Evans         char    **envv;
160ca46e90eSBruce Evans };
161ca46e90eSBruce Evans #endif
162ca46e90eSBruce Evans 
163ca46e90eSBruce Evans /*
164ca46e90eSBruce Evans  * MPSAFE
165ca46e90eSBruce Evans  */
166ca46e90eSBruce Evans int
167ca46e90eSBruce Evans execve(td, uap)
168ca46e90eSBruce Evans 	struct thread *td;
169ca46e90eSBruce Evans 	struct execve_args /* {
170ca46e90eSBruce Evans 		char *fname;
171ca46e90eSBruce Evans 		char **argv;
172ca46e90eSBruce Evans 		char **envv;
173ca46e90eSBruce Evans 	} */ *uap;
174ca46e90eSBruce Evans {
175ca46e90eSBruce Evans 
176ca46e90eSBruce Evans 	return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL));
177ca46e90eSBruce Evans }
178ca46e90eSBruce Evans 
179ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
180ca46e90eSBruce Evans struct __mac_execve_args {
181ca46e90eSBruce Evans 	char	*fname;
182ca46e90eSBruce Evans 	char	**argv;
183ca46e90eSBruce Evans 	char	**envv;
184ca46e90eSBruce Evans 	struct mac	*mac_p;
185ca46e90eSBruce Evans };
186ca46e90eSBruce Evans #endif
187ca46e90eSBruce Evans 
188ca46e90eSBruce Evans /*
189ca46e90eSBruce Evans  * MPSAFE
190ca46e90eSBruce Evans  */
191ca46e90eSBruce Evans int
192ca46e90eSBruce Evans __mac_execve(td, uap)
193ca46e90eSBruce Evans 	struct thread *td;
194ca46e90eSBruce Evans 	struct __mac_execve_args /* {
195ca46e90eSBruce Evans 		char *fname;
196ca46e90eSBruce Evans 		char **argv;
197ca46e90eSBruce Evans 		char **envv;
198ca46e90eSBruce Evans 		struct mac *mac_p;
199ca46e90eSBruce Evans 	} */ *uap;
200ca46e90eSBruce Evans {
201ca46e90eSBruce Evans 
202ca46e90eSBruce Evans #ifdef MAC
203ca46e90eSBruce Evans 	return (kern_execve(td, uap->fname, uap->argv, uap->envv,
204ca46e90eSBruce Evans 	    uap->mac_p));
205ca46e90eSBruce Evans #else
206ca46e90eSBruce Evans 	return (ENOSYS);
207ca46e90eSBruce Evans #endif
208ca46e90eSBruce Evans }
209ca46e90eSBruce Evans 
21026f9a767SRodney W. Grimes /*
211450ffb44SRobert Watson  * In-kernel implementation of execve().  All arguments are assumed to be
212450ffb44SRobert Watson  * userspace pointers from the passed thread.
213116734c4SMatthew Dillon  *
214116734c4SMatthew Dillon  * MPSAFE
21526f9a767SRodney W. Grimes  */
216450ffb44SRobert Watson static int
217670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p)
218b40ce416SJulian Elischer 	struct thread *td;
219450ffb44SRobert Watson 	char *fname;
220450ffb44SRobert Watson 	char **argv;
221450ffb44SRobert Watson 	char **envv;
222670cb89bSRobert Watson 	struct mac *mac_p;
223df8bae1dSRodney W. Grimes {
224b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
22526f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
2269b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
2271419eacbSAlfred Perlstein 	struct uidinfo *euip;
228654f6be1SBruce Evans 	register_t *stack_base;
229bb56ec4aSPoul-Henning Kamp 	int error, len, i;
230c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
23126f9a767SRodney W. Grimes 	struct vattr attr;
2324d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
23369be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
23490af4afaSJohn Baldwin 	struct sigacts *oldsigacts, *newsigacts;
2356c84de02SJohn Baldwin #ifdef KTRACE
2366c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
237a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
2386c84de02SJohn Baldwin #endif
2396c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
240d06c0d4dSRobert Watson 	int credential_changing;
241619eb6e5SJeff Roberson 	int textset;
242ccafe7ebSRobert Watson #ifdef MAC
243eca8a663SRobert Watson 	struct label *interplabel = NULL;
244eca8a663SRobert Watson 	int will_transition;
245ccafe7ebSRobert Watson #endif
24626f9a767SRodney W. Grimes 
247c52007c2SDavid Greenman 	imgp = &image_params;
248df8bae1dSRodney W. Grimes 
2499ca45e81SDag-Erling Smørgrav 	/*
2509ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
2519ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
2529ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
2539ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
2549ca45e81SDag-Erling Smørgrav 	 * privileges.
2559ca45e81SDag-Erling Smørgrav 	 */
2569ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
2579ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
25891f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
2590e2a4d3aSDavid Xu 	if (p->p_flag & P_SA || p->p_numthreads > 1) {
2601279572aSDavid Xu 		if (thread_single(SINGLE_EXIT)) {
261e602ba25SJulian Elischer 			PROC_UNLOCK(p);
262e602ba25SJulian Elischer 			return (ERESTART);	/* Try again later. */
263e602ba25SJulian Elischer 		}
26449539972SJulian Elischer 		/*
26549539972SJulian Elischer 		 * If we get here all other threads are dead,
26649539972SJulian Elischer 		 * so unset the associated flags and lose KSE mode.
26749539972SJulian Elischer 		 */
2680e2a4d3aSDavid Xu 		p->p_flag &= ~P_SA;
26945f603e2SDavid Xu 		td->td_mailbox = NULL;
27049539972SJulian Elischer 		thread_single_end();
27149539972SJulian Elischer 	}
2729ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
2739ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
2749ca45e81SDag-Erling Smørgrav 
275df8bae1dSRodney W. Grimes 	/*
276c52007c2SDavid Greenman 	 * Initialize part of the common data
277df8bae1dSRodney W. Grimes 	 */
278c52007c2SDavid Greenman 	imgp->proc = p;
279450ffb44SRobert Watson 	imgp->userspace_argv = argv;
280450ffb44SRobert Watson 	imgp->userspace_envv = envv;
281670cb89bSRobert Watson 	imgp->execlabel = NULL;
282c52007c2SDavid Greenman 	imgp->attr = &attr;
283c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
2845cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
285c52007c2SDavid Greenman 	imgp->entry_addr = 0;
286c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
287c52007c2SDavid Greenman 	imgp->interpreted = 0;
288c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
289e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
2901616db3cSJohn Dyson 	imgp->vp = NULL;
2910b2ed1aeSJeff Roberson 	imgp->object = NULL;
2921616db3cSJohn Dyson 	imgp->firstpage = NULL;
2934fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
294b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
29526f9a767SRodney W. Grimes 
296670cb89bSRobert Watson #ifdef MAC
297eca8a663SRobert Watson 	error = mac_execve_enter(imgp, mac_p);
298670cb89bSRobert Watson 	if (error) {
299670cb89bSRobert Watson 		mtx_lock(&Giant);
300670cb89bSRobert Watson 		goto exec_fail;
301670cb89bSRobert Watson 	}
302670cb89bSRobert Watson #endif
303670cb89bSRobert Watson 
30426f9a767SRodney W. Grimes 	/*
30526f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
30626f9a767SRodney W. Grimes 	 *	environment strings
30726f9a767SRodney W. Grimes 	 */
308ca0387efSJake Burkholder 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX +
309ca0387efSJake Burkholder 	    PAGE_SIZE);
310c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
31126f9a767SRodney W. Grimes 		error = ENOMEM;
312b3afd20dSAlan Cox 		mtx_lock(&Giant);
31326f9a767SRodney W. Grimes 		goto exec_fail;
31426f9a767SRodney W. Grimes 	}
315c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
316c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
3171616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
31826f9a767SRodney W. Grimes 
31926f9a767SRodney W. Grimes 	/*
32026f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
32126f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
32226f9a767SRodney W. Grimes 	 */
32326f9a767SRodney W. Grimes 	ndp = &nd;
324b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
325450ffb44SRobert Watson 	    UIO_USERSPACE, fname, td);
32626f9a767SRodney W. Grimes 
327b3afd20dSAlan Cox 	mtx_lock(&Giant);
32826f9a767SRodney W. Grimes interpret:
32926f9a767SRodney W. Grimes 
33026f9a767SRodney W. Grimes 	error = namei(ndp);
33126f9a767SRodney W. Grimes 	if (error) {
3321616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3331616db3cSJohn Dyson 		    ARG_MAX + PAGE_SIZE);
33426f9a767SRodney W. Grimes 		goto exec_fail;
33526f9a767SRodney W. Grimes 	}
33626f9a767SRodney W. Grimes 
337c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
338450ffb44SRobert Watson 	imgp->fname = fname;
33926f9a767SRodney W. Grimes 
34026f9a767SRodney W. Grimes 	/*
3419022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
3429022e4adSDavid Greenman 	 */
343c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
344619eb6e5SJeff Roberson 	if (error)
34526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
346619eb6e5SJeff Roberson 
347619eb6e5SJeff Roberson 	if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
3480b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
34926f9a767SRodney W. Grimes 
350619eb6e5SJeff Roberson 	/*
351619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
352619eb6e5SJeff Roberson 	 * activating it.
353619eb6e5SJeff Roberson 	 *
354619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
355619eb6e5SJeff Roberson 	 * actually an executable image.
356619eb6e5SJeff Roberson 	 */
357619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
358619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
359619eb6e5SJeff Roberson 
3601616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
3616d5a0a8cSDavid Greenman 	if (error)
36226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes 	/*
365d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
366d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
367d323ddf3SMatthew Dillon 	 *	scripts differently.
36826f9a767SRodney W. Grimes 	 */
369d323ddf3SMatthew Dillon 	error = -1;
370d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
371d323ddf3SMatthew Dillon 		error = img_first(imgp);
372d323ddf3SMatthew Dillon 
373d323ddf3SMatthew Dillon 	/*
374d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
375d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
376d323ddf3SMatthew Dillon 	 *	and an error otherwise.
377d323ddf3SMatthew Dillon 	 */
378d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
379d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
380d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
381d323ddf3SMatthew Dillon 			continue;
382d323ddf3SMatthew Dillon 		}
383c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
384d323ddf3SMatthew Dillon 	}
385d323ddf3SMatthew Dillon 
386d323ddf3SMatthew Dillon 	if (error) {
387619eb6e5SJeff Roberson 		if (error == -1) {
388619eb6e5SJeff Roberson 			if (textset == 0)
389619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
390d323ddf3SMatthew Dillon 			error = ENOEXEC;
391619eb6e5SJeff Roberson 		}
39226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
393d323ddf3SMatthew Dillon 	}
394d323ddf3SMatthew Dillon 
395d323ddf3SMatthew Dillon 	/*
396d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
397d323ddf3SMatthew Dillon 	 * activate the interpreter.
398d323ddf3SMatthew Dillon 	 */
399c52007c2SDavid Greenman 	if (imgp->interpreted) {
4001616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
401619eb6e5SJeff Roberson 		/*
402619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
403619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
404619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
405619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
406619eb6e5SJeff Roberson 		 */
407619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
408762e6b85SEivind Eklund 		/* free name buffer and old vnode */
409762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
410670cb89bSRobert Watson #ifdef MAC
411eca8a663SRobert Watson 		interplabel = mac_vnode_label_alloc();
412eca8a663SRobert Watson 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
413670cb89bSRobert Watson #endif
414619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
4150b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
4160b2ed1aeSJeff Roberson 		imgp->object = NULL;
41726f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
418b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
419b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
42026f9a767SRodney W. Grimes 		goto interpret;
42126f9a767SRodney W. Grimes 	}
42226f9a767SRodney W. Grimes 
42326f9a767SRodney W. Grimes 	/*
42426f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
42526f9a767SRodney W. Grimes 	 */
4263ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
4273ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
4283ebc1248SPeter Wemm 	else
429c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
43026f9a767SRodney W. Grimes 
43126f9a767SRodney W. Grimes 	/*
4321e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
4331e1e0b44SSøren Schmidt 	 * let it do the stack setup.
4341e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
43526f9a767SRodney W. Grimes 	 */
436d6c847f3SBruce Evans 	if (p->p_sysent->sv_fixup != NULL)
437c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
4381e1e0b44SSøren Schmidt 	else
439c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
44026f9a767SRodney W. Grimes 
441a78e8d2aSDavid Greenman 	/*
442a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
443a78e8d2aSDavid Greenman 	 * be shared after an exec.
444a78e8d2aSDavid Greenman 	 */
445426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
446a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
447a78e8d2aSDavid Greenman 		struct filedesc *tmp;
448a78e8d2aSDavid Greenman 
449c522c1bfSAlfred Perlstein 		tmp = fdcopy(td->td_proc->p_fd);
450426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
451b40ce416SJulian Elischer 		fdfree(td);
452a78e8d2aSDavid Greenman 		p->p_fd = tmp;
453426da3bcSAlfred Perlstein 	} else
454426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
455a78e8d2aSDavid Greenman 
456333ea485SGuido van Rooij 	/*
4579b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
4589b3b1c5fSJohn Baldwin 	 */
4599b3b1c5fSJohn Baldwin 	newcred = crget();
4601419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
4619b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
4629b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
4639b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
4649b3b1c5fSJohn Baldwin 
4659b3b1c5fSJohn Baldwin 	/* close files on exec */
4669b3b1c5fSJohn Baldwin 	fdcloseexec(td);
4679b3b1c5fSJohn Baldwin 
468619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
469619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
470619eb6e5SJeff Roberson 
4719b3b1c5fSJohn Baldwin 	/*
472333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
4738688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
474b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
475333ea485SGuido van Rooij 	 * reset.
476333ea485SGuido van Rooij 	 */
4779b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
47890af4afaSJohn Baldwin 	if (sigacts_shared(p->p_sigacts)) {
47990af4afaSJohn Baldwin 		oldsigacts = p->p_sigacts;
4809b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
48190af4afaSJohn Baldwin 		newsigacts = sigacts_alloc();
48290af4afaSJohn Baldwin 		sigacts_copy(newsigacts, oldsigacts);
4839b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
48490af4afaSJohn Baldwin 		p->p_sigacts = newsigacts;
48590af4afaSJohn Baldwin 	} else
48690af4afaSJohn Baldwin 		oldsigacts = NULL;
487333ea485SGuido van Rooij 
488fdf4e8b3SWarner Losh 	/* Stop profiling */
489fdf4e8b3SWarner Losh 	stopprofclock(p);
490fdf4e8b3SWarner Losh 
49126f9a767SRodney W. Grimes 	/* reset caught signals */
49226f9a767SRodney W. Grimes 	execsigs(p);
49326f9a767SRodney W. Grimes 
49426f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
49526f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
49626f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
49726f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
49826f9a767SRodney W. Grimes 
49926f9a767SRodney W. Grimes 	/*
50024b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
501dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
50226f9a767SRodney W. Grimes 	 */
50326f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
50426f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
50526f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
5067f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
50726f9a767SRodney W. Grimes 	}
50826f9a767SRodney W. Grimes 
50926f9a767SRodney W. Grimes 	/*
510a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
511a78e8d2aSDavid Greenman 	 *
512a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
513a78e8d2aSDavid Greenman 	 * the process is being traced.
514670cb89bSRobert Watson 	 *
515670cb89bSRobert Watson 	 * XXXMAC: For the time being, use NOSUID to also prohibit
516670cb89bSRobert Watson 	 * transitions on the file system.
517c52007c2SDavid Greenman 	 */
518b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
519d06c0d4dSRobert Watson 	credential_changing = 0;
520d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
521d06c0d4dSRobert Watson 	    attr.va_uid;
522d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
523d06c0d4dSRobert Watson 	    attr.va_gid;
524ccafe7ebSRobert Watson #ifdef MAC
525670cb89bSRobert Watson 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
526eca8a663SRobert Watson 	    interplabel, imgp);
527ccafe7ebSRobert Watson 	credential_changing |= will_transition;
528ccafe7ebSRobert Watson #endif
529d06c0d4dSRobert Watson 
530d06c0d4dSRobert Watson 	if (credential_changing &&
531a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
532c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
533c52007c2SDavid Greenman 		/*
534c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
535b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
536b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
53726f9a767SRodney W. Grimes 		 */
538b85db196SPeter Wemm 		setsugid(p);
5396c84de02SJohn Baldwin #ifdef KTRACE
540a5881ea5SJohn Baldwin 		if (p->p_tracevp != NULL && suser_cred(oldcred, PRISON_ROOT)) {
5416c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
54279deba82SMatthew Dillon 			p->p_traceflag = 0;
543a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
544a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
545a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
546a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
5476c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
54826f9a767SRodney W. Grimes 		}
5496c84de02SJohn Baldwin #endif
55028b325aaSDon Lewis 		/*
551c1e2d386SNate Lawson 		 * Close any file descriptors 0..2 that reference procfs,
552c1e2d386SNate Lawson 		 * then make sure file descriptors 0..2 are in use.
55328b325aaSDon Lewis 		 *
554c1e2d386SNate Lawson 		 * setugidsafety() may call closef() and then pfind()
555c1e2d386SNate Lawson 		 * which may grab the process lock.
55628b325aaSDon Lewis 		 * fdcheckstd() may call falloc() which may block to
55728b325aaSDon Lewis 		 * allocate memory, so temporarily drop the process lock.
55828b325aaSDon Lewis 		 */
55928b325aaSDon Lewis 		PROC_UNLOCK(p);
560c1e2d386SNate Lawson 		setugidsafety(td);
561e983a376SJacques Vidrine 		error = fdcheckstd(td);
56263c9e754SJohn Baldwin 		if (error != 0)
56369be5db9SAlfred Perlstein 			goto done1;
564e1b1aa3bSJohn Baldwin 		PROC_LOCK(p);
565c52007c2SDavid Greenman 		/*
566c52007c2SDavid Greenman 		 * Set the new credentials.
567c52007c2SDavid Greenman 		 */
5689b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
569c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
5701419eacbSAlfred Perlstein 			change_euid(newcred, euip);
571c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
572b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
573ccafe7ebSRobert Watson #ifdef MAC
574670cb89bSRobert Watson 		if (will_transition) {
575670cb89bSRobert Watson 			mac_execve_transition(oldcred, newcred, imgp->vp,
576eca8a663SRobert Watson 			    interplabel, imgp);
577670cb89bSRobert Watson 		}
578ccafe7ebSRobert Watson #endif
5799b3b1c5fSJohn Baldwin 		/*
5809b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
581ccafe7ebSRobert Watson 		 *
582ccafe7ebSRobert Watson 		 * XXXMAC: Note that the current logic will save the
583ccafe7ebSRobert Watson 		 * uid and gid if a MAC domain transition occurs, even
584ccafe7ebSRobert Watson 		 * though maybe it shouldn't.
5859b3b1c5fSJohn Baldwin 		 */
5869b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
5879b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
5889b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
5899b3b1c5fSJohn Baldwin 		newcred = NULL;
590c52007c2SDavid Greenman 	} else {
591b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
592b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
593c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
59426f9a767SRodney W. Grimes 		/*
595c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
596b1fc0ec1SRobert Watson 		 *
597b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
5989b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
5999b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
6009b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
6019b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
6029b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
6039b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
6049b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
60526f9a767SRodney W. Grimes 		 */
606b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
607b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
6089b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
609b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
610b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
611b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
6129b3b1c5fSJohn Baldwin 			newcred = NULL;
6139b3b1c5fSJohn Baldwin 		}
614b1fc0ec1SRobert Watson 	}
61526f9a767SRodney W. Grimes 
61626f9a767SRodney W. Grimes 	/*
617619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
618619eb6e5SJeff Roberson 	 * to locking the proc lock.
6192a531c80SDavid Greenman 	 */
6209b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
6212a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
6222a531c80SDavid Greenman 
6232a531c80SDavid Greenman 	/*
6249ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
6259ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
626cb679c38SJonathan Lemon 	 */
627cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
6289ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
629cb679c38SJonathan Lemon 
630cb679c38SJonathan Lemon 	/*
63126f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
63226f9a767SRodney W. Grimes 	 * can be set before the program executes.
63326f9a767SRodney W. Grimes 	 */
63426f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
63526f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
63626f9a767SRodney W. Grimes 
63726f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
63826f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
63926f9a767SRodney W. Grimes 
640b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
6419b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
642b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
643b9df5231SPoul-Henning Kamp 
644e1b1aa3bSJohn Baldwin 	/* Cache arguments if they fit inside our allowance */
645e1b1aa3bSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
646e1b1aa3bSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
647e1b1aa3bSJohn Baldwin 		p->p_args = newargs;
648e1b1aa3bSJohn Baldwin 		newargs = NULL;
649e1b1aa3bSJohn Baldwin 	}
650e1b1aa3bSJohn Baldwin 	PROC_UNLOCK(p);
651e1b1aa3bSJohn Baldwin 
652e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
6533ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
6543ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
6553ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
6563ebc1248SPeter Wemm 	else
657bafbd492SJake Burkholder 		exec_setregs(td, imgp->entry_addr,
658bafbd492SJake Burkholder 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
659e913ca22SDoug Rabson 
66069be5db9SAlfred Perlstein done1:
6619b3b1c5fSJohn Baldwin 	/*
6629b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
6639b3b1c5fSJohn Baldwin 	 */
6641419eacbSAlfred Perlstein 	uifree(euip);
6659b3b1c5fSJohn Baldwin 	if (newcred == NULL)
6669b3b1c5fSJohn Baldwin 		crfree(oldcred);
6679b3b1c5fSJohn Baldwin 	else
6689b3b1c5fSJohn Baldwin 		crfree(newcred);
6699b3b1c5fSJohn Baldwin 	/*
6709b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
6719b3b1c5fSJohn Baldwin 	 */
6729b3b1c5fSJohn Baldwin 	if (textvp != NULL)
6739b3b1c5fSJohn Baldwin 		vrele(textvp);
674619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
675619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
6766c84de02SJohn Baldwin #ifdef KTRACE
6779b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
6789b3b1c5fSJohn Baldwin 		vrele(tracevp);
679a5881ea5SJohn Baldwin 	if (tracecred != NULL)
680a5881ea5SJohn Baldwin 		crfree(tracecred);
6816c84de02SJohn Baldwin #endif
68269be5db9SAlfred Perlstein 	if (oldargs != NULL)
6839b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
68469be5db9SAlfred Perlstein 	if (newargs != NULL)
68569be5db9SAlfred Perlstein 		pargs_drop(newargs);
68690af4afaSJohn Baldwin 	if (oldsigacts != NULL)
68790af4afaSJohn Baldwin 		sigacts_free(oldsigacts);
688b9df5231SPoul-Henning Kamp 
6891616db3cSJohn Dyson exec_fail_dealloc:
6901616db3cSJohn Dyson 
69126f9a767SRodney W. Grimes 	/*
69226f9a767SRodney W. Grimes 	 * free various allocated resources
69326f9a767SRodney W. Grimes 	 */
694d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
6951616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
69626f9a767SRodney W. Grimes 
697d6c847f3SBruce Evans 	if (imgp->vp != NULL) {
698762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
699619eb6e5SJeff Roberson 		vput(imgp->vp);
700a3cf6ebaSDavid Greenman 	}
70126f9a767SRodney W. Grimes 
702a9a08882SJeff Roberson 	if (imgp->stringbase != NULL)
703a9a08882SJeff Roberson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
704a9a08882SJeff Roberson 		    ARG_MAX + PAGE_SIZE);
705a9a08882SJeff Roberson 
706d6c847f3SBruce Evans 	if (imgp->object != NULL)
7070b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
7080b2ed1aeSJeff Roberson 
709d1989db5SRobert Drehmel 	if (error == 0) {
710d1989db5SRobert Drehmel 		/*
711d1989db5SRobert Drehmel 		 * Stop the process here if its stop event mask has
712d1989db5SRobert Drehmel 		 * the S_EXEC bit set.
713d1989db5SRobert Drehmel 		 */
714d1989db5SRobert Drehmel 		STOPEVENT(p, S_EXEC, 0);
715116734c4SMatthew Dillon 		goto done2;
716d1989db5SRobert Drehmel 	}
7171616db3cSJohn Dyson 
71826f9a767SRodney W. Grimes exec_fail:
7199ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
7209ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
7219ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
7229ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
7239ca45e81SDag-Erling Smørgrav 
724c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
72526f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
726670cb89bSRobert Watson #ifdef MAC
727670cb89bSRobert Watson 		mac_execve_exit(imgp);
728eca8a663SRobert Watson 		if (interplabel != NULL)
729eca8a663SRobert Watson 			mac_vnode_label_free(interplabel);
730670cb89bSRobert Watson #endif
731b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
73226f9a767SRodney W. Grimes 		/* NOT REACHED */
733116734c4SMatthew Dillon 		error = 0;
73426f9a767SRodney W. Grimes 	}
735116734c4SMatthew Dillon done2:
736670cb89bSRobert Watson #ifdef MAC
737670cb89bSRobert Watson 	mac_execve_exit(imgp);
738eca8a663SRobert Watson 	if (interplabel != NULL)
739eca8a663SRobert Watson 		mac_vnode_label_free(interplabel);
740670cb89bSRobert Watson #endif
741116734c4SMatthew Dillon 	mtx_unlock(&Giant);
742116734c4SMatthew Dillon 	return (error);
74326f9a767SRodney W. Grimes }
74426f9a767SRodney W. Grimes 
7451616db3cSJohn Dyson int
7461616db3cSJohn Dyson exec_map_first_page(imgp)
7471616db3cSJohn Dyson 	struct image_params *imgp;
7481616db3cSJohn Dyson {
749d8aad40cSJohn Baldwin 	int rv, i;
75095461b45SJohn Dyson 	int initial_pagein;
75195461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
7521616db3cSJohn Dyson 	vm_object_t object;
7531616db3cSJohn Dyson 
7540cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
7551616db3cSJohn Dyson 
756d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
7571616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
7581616db3cSJohn Dyson 
7599ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
760fd0cc9a8SAlan Cox 	VM_OBJECT_LOCK(object);
76195461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
76295461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
76395461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
76495461b45SJohn Dyson 		if (initial_pagein > object->size)
76595461b45SJohn Dyson 			initial_pagein = object->size;
76695461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
767d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
7686ec2fca5SAlan Cox 				if (ma[i]->valid)
7696ec2fca5SAlan Cox 					break;
770ee113343SAlan Cox 				vm_page_lock_queues();
77106fa71cdSAlan Cox 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) {
77206fa71cdSAlan Cox 					vm_page_unlock_queues();
77306fa71cdSAlan Cox 					break;
77406fa71cdSAlan Cox 				}
775e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
776ee113343SAlan Cox 				vm_page_unlock_queues();
77795461b45SJohn Dyson 			} else {
778ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
779ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
78095461b45SJohn Dyson 				if (ma[i] == NULL)
78195461b45SJohn Dyson 					break;
78295461b45SJohn Dyson 			}
78395461b45SJohn Dyson 		}
78495461b45SJohn Dyson 		initial_pagein = i;
78595461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
78695461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
787ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
788ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
789ecbb00a2SDoug Rabson 			if (ma[0]) {
7906ec2fca5SAlan Cox 				vm_page_lock_queues();
7914fec79beSAlan Cox 				pmap_remove_all(ma[0]);
7928f9110f6SJohn Dyson 				vm_page_free(ma[0]);
79306fa71cdSAlan Cox 				vm_page_unlock_queues();
7946ec2fca5SAlan Cox 			}
79506fa71cdSAlan Cox 			VM_OBJECT_UNLOCK(object);
796a7cddfedSJake Burkholder 			return (EIO);
7971616db3cSJohn Dyson 		}
7981616db3cSJohn Dyson 	}
7996ec2fca5SAlan Cox 	vm_page_lock_queues();
80095461b45SJohn Dyson 	vm_page_wire(ma[0]);
801e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
80297646f56SAlan Cox 	vm_page_unlock_queues();
8036ec2fca5SAlan Cox 	VM_OBJECT_UNLOCK(object);
8041616db3cSJohn Dyson 
805ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
80695461b45SJohn Dyson 	imgp->firstpage = ma[0];
8071616db3cSJohn Dyson 
808a7cddfedSJake Burkholder 	return (0);
8091616db3cSJohn Dyson }
8101616db3cSJohn Dyson 
8111616db3cSJohn Dyson void
8121616db3cSJohn Dyson exec_unmap_first_page(imgp)
8131616db3cSJohn Dyson 	struct image_params *imgp;
8141616db3cSJohn Dyson {
81523955314SAlfred Perlstein 
816d6c847f3SBruce Evans 	if (imgp->firstpage != NULL) {
817ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
81897646f56SAlan Cox 		vm_page_lock_queues();
81973007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
82097646f56SAlan Cox 		vm_page_unlock_queues();
8211616db3cSJohn Dyson 		imgp->firstpage = NULL;
8221616db3cSJohn Dyson 	}
8231616db3cSJohn Dyson }
8241616db3cSJohn Dyson 
82526f9a767SRodney W. Grimes /*
82626f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
82726f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
82826f9a767SRodney W. Grimes  *	automatically in trap.c.
82926f9a767SRodney W. Grimes  */
83026f9a767SRodney W. Grimes int
83105ba50f5SJake Burkholder exec_new_vmspace(imgp, sv)
832c52007c2SDavid Greenman 	struct image_params *imgp;
83305ba50f5SJake Burkholder 	struct sysentvec *sv;
83426f9a767SRodney W. Grimes {
83526f9a767SRodney W. Grimes 	int error;
8365e20c11fSAlan Cox 	struct proc *p = imgp->proc;
8375e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
83805ba50f5SJake Burkholder 	vm_offset_t stack_addr;
83905ba50f5SJake Burkholder 	vm_map_t map;
84026f9a767SRodney W. Grimes 
8410cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8420cddd8f0SMatthew Dillon 
843c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
84426f9a767SRodney W. Grimes 
84575b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exec, p);
8466f5dafeaSAlan Cox 
8476f5dafeaSAlan Cox 	/*
848c460ac3aSPeter Wemm 	 * Here is as good a place as any to do any resource limit cleanups.
849c460ac3aSPeter Wemm 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
850c460ac3aSPeter Wemm 	 * data size limit may need to be changed to a value that makes
851c460ac3aSPeter Wemm 	 * sense for the 32 bit binary.
852c460ac3aSPeter Wemm 	 */
853d6c847f3SBruce Evans 	if (sv->sv_fixlimits != NULL)
854c460ac3aSPeter Wemm 		sv->sv_fixlimits(imgp);
855c460ac3aSPeter Wemm 
856c460ac3aSPeter Wemm 	/*
857af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
858af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
859af9ec885SJohn Dyson 	 * not disrupted
860af9ec885SJohn Dyson 	 */
86105ba50f5SJake Burkholder 	map = &vmspace->vm_map;
86205ba50f5SJake Burkholder 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
86305ba50f5SJake Burkholder 	    vm_map_max(map) == sv->sv_maxuser) {
8643db161e0SMatthew Dillon 		shmexit(vmspace);
8652d21129dSAlan Cox 		vm_page_lock_queues();
86605ba50f5SJake Burkholder 		pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
86705ba50f5SJake Burkholder 		    vm_map_max(map));
8682d21129dSAlan Cox 		vm_page_unlock_queues();
86905ba50f5SJake Burkholder 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
870af9ec885SJohn Dyson 	} else {
87105ba50f5SJake Burkholder 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
8725e20c11fSAlan Cox 		vmspace = p->p_vmspace;
87305ba50f5SJake Burkholder 		map = &vmspace->vm_map;
874af9ec885SJohn Dyson 	}
87526f9a767SRodney W. Grimes 
87626f9a767SRodney W. Grimes 	/* Allocate a new stack */
877fd75d710SMarcel Moolenaar 	stack_addr = sv->sv_usrstack - maxssiz;
87805ba50f5SJake Burkholder 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
879fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
8802267af78SJulian Elischer 	if (error)
8812267af78SJulian Elischer 		return (error);
8822267af78SJulian Elischer 
88363c47a5cSDoug Rabson #ifdef __ia64__
884fd75d710SMarcel Moolenaar 	/* Allocate a new register stack */
885bab1f052SMarcel Moolenaar 	stack_addr = IA64_BACKINGSTORE;
886fd75d710SMarcel Moolenaar 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
887fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
888fd75d710SMarcel Moolenaar 	if (error)
889fd75d710SMarcel Moolenaar 		return (error);
89063c47a5cSDoug Rabson #endif
89163c47a5cSDoug Rabson 
8922267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
8932267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
8942267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
8952267af78SJulian Elischer 	 */
896cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
89705ba50f5SJake Burkholder 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
89826f9a767SRodney W. Grimes 
89926f9a767SRodney W. Grimes 	return (0);
90026f9a767SRodney W. Grimes }
90126f9a767SRodney W. Grimes 
90226f9a767SRodney W. Grimes /*
90326f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
90426f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
90526f9a767SRodney W. Grimes  */
90626f9a767SRodney W. Grimes int
907c52007c2SDavid Greenman exec_extract_strings(imgp)
908c52007c2SDavid Greenman 	struct image_params *imgp;
90926f9a767SRodney W. Grimes {
91026f9a767SRodney W. Grimes 	char	**argv, **envv;
91126f9a767SRodney W. Grimes 	char	*argp, *envp;
912ecbb00a2SDoug Rabson 	int	error;
913ecbb00a2SDoug Rabson 	size_t	length;
91426f9a767SRodney W. Grimes 
91526f9a767SRodney W. Grimes 	/*
91626f9a767SRodney W. Grimes 	 * extract arguments first
91726f9a767SRodney W. Grimes 	 */
91826f9a767SRodney W. Grimes 
919450ffb44SRobert Watson 	argv = imgp->userspace_argv;
92026f9a767SRodney W. Grimes 
9210fd1a014SDavid Greenman 	if (argv) {
922aae0aa45SBruce Evans 		argp = (caddr_t)(intptr_t)fuword(argv);
9235cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t)-1)
9245cf3d12cSAndrey A. Chernov 			return (EFAULT);
9255cf3d12cSAndrey A. Chernov 		if (argp)
9265cf3d12cSAndrey A. Chernov 			argv++;
9275cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
9285cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
9295cf3d12cSAndrey A. Chernov 		if (argp) {
9305cf3d12cSAndrey A. Chernov 			do {
93126f9a767SRodney W. Grimes 				if (argp == (caddr_t)-1)
93226f9a767SRodney W. Grimes 					return (EFAULT);
933c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
934c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
9350fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
93626f9a767SRodney W. Grimes 						return (E2BIG);
9370fd1a014SDavid Greenman 					return (error);
9380fd1a014SDavid Greenman 				}
939c52007c2SDavid Greenman 				imgp->stringspace -= length;
940c52007c2SDavid Greenman 				imgp->stringp += length;
941c52007c2SDavid Greenman 				imgp->argc++;
942aae0aa45SBruce Evans 			} while ((argp = (caddr_t)(intptr_t)fuword(argv++)));
94326f9a767SRodney W. Grimes 		}
9440fd1a014SDavid Greenman 	}
94526f9a767SRodney W. Grimes 
946b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
947b9df5231SPoul-Henning Kamp 
94826f9a767SRodney W. Grimes 	/*
94926f9a767SRodney W. Grimes 	 * extract environment strings
95026f9a767SRodney W. Grimes 	 */
95126f9a767SRodney W. Grimes 
952450ffb44SRobert Watson 	envv = imgp->userspace_envv;
95326f9a767SRodney W. Grimes 
9540fd1a014SDavid Greenman 	if (envv) {
955aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
95626f9a767SRodney W. Grimes 			if (envp == (caddr_t)-1)
95726f9a767SRodney W. Grimes 				return (EFAULT);
958c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
959c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
9600fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
96126f9a767SRodney W. Grimes 					return (E2BIG);
9620fd1a014SDavid Greenman 				return (error);
9630fd1a014SDavid Greenman 			}
964c52007c2SDavid Greenman 			imgp->stringspace -= length;
965c52007c2SDavid Greenman 			imgp->stringp += length;
966c52007c2SDavid Greenman 			imgp->envc++;
96726f9a767SRodney W. Grimes 		}
9680fd1a014SDavid Greenman 	}
96926f9a767SRodney W. Grimes 
97026f9a767SRodney W. Grimes 	return (0);
97126f9a767SRodney W. Grimes }
97226f9a767SRodney W. Grimes 
97326f9a767SRodney W. Grimes /*
97426f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
97526f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
97626f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
97726f9a767SRodney W. Grimes  */
978654f6be1SBruce Evans register_t *
979c52007c2SDavid Greenman exec_copyout_strings(imgp)
980c52007c2SDavid Greenman 	struct image_params *imgp;
98126f9a767SRodney W. Grimes {
98226f9a767SRodney W. Grimes 	int argc, envc;
98326f9a767SRodney W. Grimes 	char **vectp;
98426f9a767SRodney W. Grimes 	char *stringp, *destp;
985654f6be1SBruce Evans 	register_t *stack_base;
98693f6448cSDavid Greenman 	struct ps_strings *arginfo;
987f3bec5d7SJake Burkholder 	struct proc *p;
988d66a5066SPeter Wemm 	int szsigcode;
98926f9a767SRodney W. Grimes 
99026f9a767SRodney W. Grimes 	/*
99126f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
992d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
99326f9a767SRodney W. Grimes 	 */
994f3bec5d7SJake Burkholder 	p = imgp->proc;
995f3bec5d7SJake Burkholder 	szsigcode = 0;
99605ba50f5SJake Burkholder 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
997f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
998f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
999d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
10001ed012f9SPeter Wemm 	    roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
10011ed012f9SPeter Wemm 
100226f9a767SRodney W. Grimes 	/*
1003d66a5066SPeter Wemm 	 * install sigcode
1004d66a5066SPeter Wemm 	 */
1005d66a5066SPeter Wemm 	if (szsigcode)
1006f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1007f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
1008d66a5066SPeter Wemm 
1009d66a5066SPeter Wemm 	/*
1010e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
1011e1743d02SSøren Schmidt 	 * on the stack.
1012e1743d02SSøren Schmidt 	 */
1013b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
1014e1743d02SSøren Schmidt 		/*
1015b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1016b9a22da4STakanori Watanabe 		 * lower compatibility.
1017b9a22da4STakanori Watanabe 		 */
1018ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1019ca0387efSJake Burkholder 		    (AT_COUNT * 2);
1020b9a22da4STakanori Watanabe 		/*
1021b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1022b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
1023b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
1024e1743d02SSøren Schmidt 		 */
1025e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
1026b9a22da4STakanori Watanabe 		    imgp->auxarg_size) * sizeof(char *));
1027b9a22da4STakanori Watanabe 
1028b9a22da4STakanori Watanabe 	} else
1029e1743d02SSøren Schmidt 		/*
1030b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1031b9a22da4STakanori Watanabe 		 * the arg and env vector sets
103226f9a767SRodney W. Grimes 		 */
1033ca0387efSJake Burkholder 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) *
1034ca0387efSJake Burkholder 		    sizeof(char *));
103526f9a767SRodney W. Grimes 
103626f9a767SRodney W. Grimes 	/*
103726f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
103826f9a767SRodney W. Grimes 	 */
1039654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
104026f9a767SRodney W. Grimes 
1041c52007c2SDavid Greenman 	stringp = imgp->stringbase;
1042c52007c2SDavid Greenman 	argc = imgp->argc;
1043c52007c2SDavid Greenman 	envc = imgp->envc;
104426f9a767SRodney W. Grimes 
104593f6448cSDavid Greenman 	/*
10463aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
104793f6448cSDavid Greenman 	 */
1048c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
104993f6448cSDavid Greenman 
105093f6448cSDavid Greenman 	/*
10513aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
10523aed948bSDavid Greenman 	 */
1053aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
10543aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
10553aed948bSDavid Greenman 
10563aed948bSDavid Greenman 	/*
10573aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
105893f6448cSDavid Greenman 	 */
105926f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
1060aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10613aed948bSDavid Greenman 		while (*stringp++ != 0)
10623aed948bSDavid Greenman 			destp++;
10633aed948bSDavid Greenman 		destp++;
106426f9a767SRodney W. Grimes 	}
106526f9a767SRodney W. Grimes 
10661a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
10676ab46d52SBruce Evans 	suword(vectp++, 0);
106826f9a767SRodney W. Grimes 
1069aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
10703aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
107193f6448cSDavid Greenman 
107293f6448cSDavid Greenman 	/*
10733aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
107493f6448cSDavid Greenman 	 */
107526f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
1076aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10773aed948bSDavid Greenman 		while (*stringp++ != 0)
10783aed948bSDavid Greenman 			destp++;
10793aed948bSDavid Greenman 		destp++;
108026f9a767SRodney W. Grimes 	}
108126f9a767SRodney W. Grimes 
108226f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
10836ab46d52SBruce Evans 	suword(vectp, 0);
108426f9a767SRodney W. Grimes 
108526f9a767SRodney W. Grimes 	return (stack_base);
108626f9a767SRodney W. Grimes }
108726f9a767SRodney W. Grimes 
108826f9a767SRodney W. Grimes /*
108926f9a767SRodney W. Grimes  * Check permissions of file to execute.
1090cf64863aSRobert Watson  *	Called with imgp->vp locked.
109126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
109226f9a767SRodney W. Grimes  */
1093c8a79999SPeter Wemm int
1094c52007c2SDavid Greenman exec_check_permissions(imgp)
1095c52007c2SDavid Greenman 	struct image_params *imgp;
109626f9a767SRodney W. Grimes {
1097c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
1098c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
1099a854ed98SJohn Baldwin 	struct thread *td;
110026f9a767SRodney W. Grimes 	int error;
110126f9a767SRodney W. Grimes 
1102a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
1103339b79b9SRobert Watson 
1104ec35c2afSRobert Watson 	/* Get file attributes */
1105ec35c2afSRobert Watson 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1106ec35c2afSRobert Watson 	if (error)
1107ec35c2afSRobert Watson 		return (error);
1108ec35c2afSRobert Watson 
1109339b79b9SRobert Watson #ifdef MAC
1110670cb89bSRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1111339b79b9SRobert Watson 	if (error)
1112339b79b9SRobert Watson 		return (error);
1113339b79b9SRobert Watson #endif
1114339b79b9SRobert Watson 
111526f9a767SRodney W. Grimes 	/*
111626f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
111726f9a767SRodney W. Grimes 	 *	file resides on.
111826f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
111926f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
112026f9a767SRodney W. Grimes 	 *	file really is executable.
112126f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
112226f9a767SRodney W. Grimes 	 */
1123c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
112426f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
1125a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
112626f9a767SRodney W. Grimes 		return (EACCES);
112726f9a767SRodney W. Grimes 
112826f9a767SRodney W. Grimes 	/*
112926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
113026f9a767SRodney W. Grimes 	 */
113126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
113226f9a767SRodney W. Grimes 		return (ENOEXEC);
113326f9a767SRodney W. Grimes 
113426f9a767SRodney W. Grimes 	/*
113526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
113626f9a767SRodney W. Grimes 	 */
1137a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
113826f9a767SRodney W. Grimes 	if (error)
113926f9a767SRodney W. Grimes 		return (error);
114026f9a767SRodney W. Grimes 
11416d5a0a8cSDavid Greenman 	/*
11426d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
11436d5a0a8cSDavid Greenman 	 * if there are any.
11446d5a0a8cSDavid Greenman 	 */
11456d5a0a8cSDavid Greenman 	if (vp->v_writecount)
11466d5a0a8cSDavid Greenman 		return (ETXTBSY);
11476d5a0a8cSDavid Greenman 
11486d5a0a8cSDavid Greenman 	/*
11496d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
11506d5a0a8cSDavid Greenman 	 * general case).
11516d5a0a8cSDavid Greenman 	 */
1152a8d43c90SPoul-Henning Kamp 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
115326f9a767SRodney W. Grimes 	return (error);
1154df8bae1dSRodney W. Grimes }
1155aa855a59SPeter Wemm 
1156aa855a59SPeter Wemm /*
1157aa855a59SPeter Wemm  * Exec handler registration
1158aa855a59SPeter Wemm  */
1159aa855a59SPeter Wemm int
1160aa855a59SPeter Wemm exec_register(execsw_arg)
1161aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1162aa855a59SPeter Wemm {
1163aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1164aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1165aa855a59SPeter Wemm 
1166aa855a59SPeter Wemm 	if (execsw)
1167aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1168aa855a59SPeter Wemm 			count++;
1169a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1170aa855a59SPeter Wemm 	if (newexecsw == NULL)
1171a7cddfedSJake Burkholder 		return (ENOMEM);
1172aa855a59SPeter Wemm 	xs = newexecsw;
1173aa855a59SPeter Wemm 	if (execsw)
1174aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1175aa855a59SPeter Wemm 			*xs++ = *es;
1176aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1177aa855a59SPeter Wemm 	*xs = NULL;
1178aa855a59SPeter Wemm 	if (execsw)
1179aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1180aa855a59SPeter Wemm 	execsw = newexecsw;
1181a7cddfedSJake Burkholder 	return (0);
1182aa855a59SPeter Wemm }
1183aa855a59SPeter Wemm 
1184aa855a59SPeter Wemm int
1185aa855a59SPeter Wemm exec_unregister(execsw_arg)
1186aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1187aa855a59SPeter Wemm {
1188aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1189aa855a59SPeter Wemm 	int count = 1;
1190aa855a59SPeter Wemm 
1191aa855a59SPeter Wemm 	if (execsw == NULL)
1192aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1193aa855a59SPeter Wemm 
1194aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1195aa855a59SPeter Wemm 		if (*es == execsw_arg)
1196aa855a59SPeter Wemm 			break;
1197aa855a59SPeter Wemm 	}
1198aa855a59SPeter Wemm 	if (*es == NULL)
1199a7cddfedSJake Burkholder 		return (ENOENT);
1200aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1201aa855a59SPeter Wemm 		if (*es != execsw_arg)
1202aa855a59SPeter Wemm 			count++;
1203a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1204aa855a59SPeter Wemm 	if (newexecsw == NULL)
1205a7cddfedSJake Burkholder 		return (ENOMEM);
1206aa855a59SPeter Wemm 	xs = newexecsw;
1207aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1208aa855a59SPeter Wemm 		if (*es != execsw_arg)
1209aa855a59SPeter Wemm 			*xs++ = *es;
1210aa855a59SPeter Wemm 	*xs = NULL;
1211aa855a59SPeter Wemm 	if (execsw)
1212aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1213aa855a59SPeter Wemm 	execsw = newexecsw;
1214a7cddfedSJake Burkholder 	return (0);
1215aa855a59SPeter Wemm }
1216