xref: /freebsd/sys/kern/kern_exec.c (revision ca46e90ef41e1ed70ff475e128b3ceb1ed490a57)
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;
10505ba50f5SJake Burkholder 
10605ba50f5SJake Burkholder 	p = curproc;
10705ba50f5SJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
10805ba50f5SJake Burkholder 	   sizeof(p->p_sysent->sv_psstrings)));
10905ba50f5SJake Burkholder }
11005ba50f5SJake Burkholder 
11105ba50f5SJake Burkholder static int
11205ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
11305ba50f5SJake Burkholder {
11405ba50f5SJake Burkholder 	struct proc *p;
11505ba50f5SJake Burkholder 
11605ba50f5SJake Burkholder 	p = curproc;
11705ba50f5SJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
11805ba50f5SJake Burkholder 	    sizeof(p->p_sysent->sv_usrstack)));
11905ba50f5SJake Burkholder }
12005ba50f5SJake Burkholder 
1215dadd17bSJake Burkholder static int
1225dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
1235dadd17bSJake Burkholder {
1245dadd17bSJake Burkholder 	struct proc *p;
1255dadd17bSJake Burkholder 
1265dadd17bSJake Burkholder 	p = curproc;
1275dadd17bSJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
1285dadd17bSJake Burkholder 	    sizeof(p->p_sysent->sv_stackprot)));
1295dadd17bSJake Burkholder }
1305dadd17bSJake Burkholder 
13199ac3bc8SPeter Wemm /*
132aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
133aa855a59SPeter Wemm  * double pointer here.
134df8bae1dSRodney W. Grimes  */
135aa855a59SPeter Wemm static const struct execsw **execsw;
13626f9a767SRodney W. Grimes 
137ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
138ca46e90eSBruce Evans struct execve_args {
139ca46e90eSBruce Evans         char    *fname;
140ca46e90eSBruce Evans         char    **argv;
141ca46e90eSBruce Evans         char    **envv;
142ca46e90eSBruce Evans };
143ca46e90eSBruce Evans #endif
144ca46e90eSBruce Evans 
145ca46e90eSBruce Evans /*
146ca46e90eSBruce Evans  * MPSAFE
147ca46e90eSBruce Evans  */
148ca46e90eSBruce Evans int
149ca46e90eSBruce Evans execve(td, uap)
150ca46e90eSBruce Evans 	struct thread *td;
151ca46e90eSBruce Evans 	struct execve_args /* {
152ca46e90eSBruce Evans 		char *fname;
153ca46e90eSBruce Evans 		char **argv;
154ca46e90eSBruce Evans 		char **envv;
155ca46e90eSBruce Evans 	} */ *uap;
156ca46e90eSBruce Evans {
157ca46e90eSBruce Evans 
158ca46e90eSBruce Evans 	return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL));
159ca46e90eSBruce Evans }
160ca46e90eSBruce Evans 
161ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
162ca46e90eSBruce Evans struct __mac_execve_args {
163ca46e90eSBruce Evans 	char	*fname;
164ca46e90eSBruce Evans 	char	**argv;
165ca46e90eSBruce Evans 	char	**envv;
166ca46e90eSBruce Evans 	struct mac	*mac_p;
167ca46e90eSBruce Evans };
168ca46e90eSBruce Evans #endif
169ca46e90eSBruce Evans 
170ca46e90eSBruce Evans /*
171ca46e90eSBruce Evans  * MPSAFE
172ca46e90eSBruce Evans  */
173ca46e90eSBruce Evans int
174ca46e90eSBruce Evans __mac_execve(td, uap)
175ca46e90eSBruce Evans 	struct thread *td;
176ca46e90eSBruce Evans 	struct __mac_execve_args /* {
177ca46e90eSBruce Evans 		char *fname;
178ca46e90eSBruce Evans 		char **argv;
179ca46e90eSBruce Evans 		char **envv;
180ca46e90eSBruce Evans 		struct mac *mac_p;
181ca46e90eSBruce Evans 	} */ *uap;
182ca46e90eSBruce Evans {
183ca46e90eSBruce Evans 
184ca46e90eSBruce Evans #ifdef MAC
185ca46e90eSBruce Evans 	return (kern_execve(td, uap->fname, uap->argv, uap->envv,
186ca46e90eSBruce Evans 	    uap->mac_p));
187ca46e90eSBruce Evans #else
188ca46e90eSBruce Evans 	return (ENOSYS);
189ca46e90eSBruce Evans #endif
190ca46e90eSBruce Evans }
191ca46e90eSBruce Evans 
19226f9a767SRodney W. Grimes /*
193450ffb44SRobert Watson  * In-kernel implementation of execve().  All arguments are assumed to be
194450ffb44SRobert Watson  * userspace pointers from the passed thread.
195116734c4SMatthew Dillon  *
196116734c4SMatthew Dillon  * MPSAFE
19726f9a767SRodney W. Grimes  */
198450ffb44SRobert Watson static int
199670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p)
200b40ce416SJulian Elischer 	struct thread *td;
201450ffb44SRobert Watson 	char *fname;
202450ffb44SRobert Watson 	char **argv;
203450ffb44SRobert Watson 	char **envv;
204670cb89bSRobert Watson 	struct mac *mac_p;
205df8bae1dSRodney W. Grimes {
206b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
20726f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
2089b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
2091419eacbSAlfred Perlstein 	struct uidinfo *euip;
210654f6be1SBruce Evans 	register_t *stack_base;
211bb56ec4aSPoul-Henning Kamp 	int error, len, i;
212c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
21326f9a767SRodney W. Grimes 	struct vattr attr;
2144d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
21569be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
21690af4afaSJohn Baldwin 	struct sigacts *oldsigacts, *newsigacts;
2176c84de02SJohn Baldwin #ifdef KTRACE
2186c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
219a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
2206c84de02SJohn Baldwin #endif
2216c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
222d06c0d4dSRobert Watson 	int credential_changing;
223619eb6e5SJeff Roberson 	int textset;
224ccafe7ebSRobert Watson #ifdef MAC
225eca8a663SRobert Watson 	struct label *interplabel = NULL;
226eca8a663SRobert Watson 	int will_transition;
227ccafe7ebSRobert Watson #endif
22826f9a767SRodney W. Grimes 
229c52007c2SDavid Greenman 	imgp = &image_params;
230df8bae1dSRodney W. Grimes 
2319ca45e81SDag-Erling Smørgrav 	/*
2329ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
2339ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
2349ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
2359ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
2369ca45e81SDag-Erling Smørgrav 	 * privileges.
2379ca45e81SDag-Erling Smørgrav 	 */
2389ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
2399ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
24091f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
2410e2a4d3aSDavid Xu 	if (p->p_flag & P_SA || p->p_numthreads > 1) {
2421279572aSDavid Xu 		if (thread_single(SINGLE_EXIT)) {
243e602ba25SJulian Elischer 			PROC_UNLOCK(p);
244e602ba25SJulian Elischer 			return (ERESTART);	/* Try again later. */
245e602ba25SJulian Elischer 		}
24649539972SJulian Elischer 		/*
24749539972SJulian Elischer 		 * If we get here all other threads are dead,
24849539972SJulian Elischer 		 * so unset the associated flags and lose KSE mode.
24949539972SJulian Elischer 		 */
2500e2a4d3aSDavid Xu 		p->p_flag &= ~P_SA;
25145f603e2SDavid Xu 		td->td_mailbox = NULL;
25249539972SJulian Elischer 		thread_single_end();
25349539972SJulian Elischer 	}
2549ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
2559ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
2569ca45e81SDag-Erling Smørgrav 
257df8bae1dSRodney W. Grimes 	/*
258c52007c2SDavid Greenman 	 * Initialize part of the common data
259df8bae1dSRodney W. Grimes 	 */
260c52007c2SDavid Greenman 	imgp->proc = p;
261450ffb44SRobert Watson 	imgp->userspace_argv = argv;
262450ffb44SRobert Watson 	imgp->userspace_envv = envv;
263670cb89bSRobert Watson 	imgp->execlabel = NULL;
264c52007c2SDavid Greenman 	imgp->attr = &attr;
265c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
2665cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
267c52007c2SDavid Greenman 	imgp->entry_addr = 0;
268c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
269c52007c2SDavid Greenman 	imgp->interpreted = 0;
270c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
271e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
2721616db3cSJohn Dyson 	imgp->vp = NULL;
2730b2ed1aeSJeff Roberson 	imgp->object = NULL;
2741616db3cSJohn Dyson 	imgp->firstpage = NULL;
2754fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
276b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
27726f9a767SRodney W. Grimes 
278670cb89bSRobert Watson #ifdef MAC
279eca8a663SRobert Watson 	error = mac_execve_enter(imgp, mac_p);
280670cb89bSRobert Watson 	if (error) {
281670cb89bSRobert Watson 		mtx_lock(&Giant);
282670cb89bSRobert Watson 		goto exec_fail;
283670cb89bSRobert Watson 	}
284670cb89bSRobert Watson #endif
285670cb89bSRobert Watson 
28626f9a767SRodney W. Grimes 	/*
28726f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
28826f9a767SRodney W. Grimes 	 *	environment strings
28926f9a767SRodney W. Grimes 	 */
290ca0387efSJake Burkholder 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX +
291ca0387efSJake Burkholder 	    PAGE_SIZE);
292c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
29326f9a767SRodney W. Grimes 		error = ENOMEM;
294b3afd20dSAlan Cox 		mtx_lock(&Giant);
29526f9a767SRodney W. Grimes 		goto exec_fail;
29626f9a767SRodney W. Grimes 	}
297c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
298c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
2991616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
30026f9a767SRodney W. Grimes 
30126f9a767SRodney W. Grimes 	/*
30226f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
30326f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
30426f9a767SRodney W. Grimes 	 */
30526f9a767SRodney W. Grimes 	ndp = &nd;
306b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
307450ffb44SRobert Watson 	    UIO_USERSPACE, fname, td);
30826f9a767SRodney W. Grimes 
309b3afd20dSAlan Cox 	mtx_lock(&Giant);
31026f9a767SRodney W. Grimes interpret:
31126f9a767SRodney W. Grimes 
31226f9a767SRodney W. Grimes 	error = namei(ndp);
31326f9a767SRodney W. Grimes 	if (error) {
3141616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3151616db3cSJohn Dyson 		    ARG_MAX + PAGE_SIZE);
31626f9a767SRodney W. Grimes 		goto exec_fail;
31726f9a767SRodney W. Grimes 	}
31826f9a767SRodney W. Grimes 
319c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
320450ffb44SRobert Watson 	imgp->fname = fname;
32126f9a767SRodney W. Grimes 
32226f9a767SRodney W. Grimes 	/*
3239022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
3249022e4adSDavid Greenman 	 */
325c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
326619eb6e5SJeff Roberson 	if (error)
32726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
328619eb6e5SJeff Roberson 
329619eb6e5SJeff Roberson 	if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
3300b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
33126f9a767SRodney W. Grimes 
332619eb6e5SJeff Roberson 	/*
333619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
334619eb6e5SJeff Roberson 	 * activating it.
335619eb6e5SJeff Roberson 	 *
336619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
337619eb6e5SJeff Roberson 	 * actually an executable image.
338619eb6e5SJeff Roberson 	 */
339619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
340619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
341619eb6e5SJeff Roberson 
3421616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
3436d5a0a8cSDavid Greenman 	if (error)
34426f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
34526f9a767SRodney W. Grimes 
34626f9a767SRodney W. Grimes 	/*
347d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
348d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
349d323ddf3SMatthew Dillon 	 *	scripts differently.
35026f9a767SRodney W. Grimes 	 */
351d323ddf3SMatthew Dillon 	error = -1;
352d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
353d323ddf3SMatthew Dillon 		error = img_first(imgp);
354d323ddf3SMatthew Dillon 
355d323ddf3SMatthew Dillon 	/*
356d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
357d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
358d323ddf3SMatthew Dillon 	 *	and an error otherwise.
359d323ddf3SMatthew Dillon 	 */
360d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
361d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
362d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
363d323ddf3SMatthew Dillon 			continue;
364d323ddf3SMatthew Dillon 		}
365c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
366d323ddf3SMatthew Dillon 	}
367d323ddf3SMatthew Dillon 
368d323ddf3SMatthew Dillon 	if (error) {
369619eb6e5SJeff Roberson 		if (error == -1) {
370619eb6e5SJeff Roberson 			if (textset == 0)
371619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
372d323ddf3SMatthew Dillon 			error = ENOEXEC;
373619eb6e5SJeff Roberson 		}
37426f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
375d323ddf3SMatthew Dillon 	}
376d323ddf3SMatthew Dillon 
377d323ddf3SMatthew Dillon 	/*
378d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
379d323ddf3SMatthew Dillon 	 * activate the interpreter.
380d323ddf3SMatthew Dillon 	 */
381c52007c2SDavid Greenman 	if (imgp->interpreted) {
3821616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
383619eb6e5SJeff Roberson 		/*
384619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
385619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
386619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
387619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
388619eb6e5SJeff Roberson 		 */
389619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
390762e6b85SEivind Eklund 		/* free name buffer and old vnode */
391762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
392670cb89bSRobert Watson #ifdef MAC
393eca8a663SRobert Watson 		interplabel = mac_vnode_label_alloc();
394eca8a663SRobert Watson 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
395670cb89bSRobert Watson #endif
396619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
3970b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
3980b2ed1aeSJeff Roberson 		imgp->object = NULL;
39926f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
400b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
401b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
40226f9a767SRodney W. Grimes 		goto interpret;
40326f9a767SRodney W. Grimes 	}
40426f9a767SRodney W. Grimes 
40526f9a767SRodney W. Grimes 	/*
40626f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
40726f9a767SRodney W. Grimes 	 */
4083ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
4093ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
4103ebc1248SPeter Wemm 	else
411c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
41226f9a767SRodney W. Grimes 
41326f9a767SRodney W. Grimes 	/*
4141e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
4151e1e0b44SSøren Schmidt 	 * let it do the stack setup.
4161e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
41726f9a767SRodney W. Grimes 	 */
4181e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
419c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
4201e1e0b44SSøren Schmidt 	else
421c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
42226f9a767SRodney W. Grimes 
423a78e8d2aSDavid Greenman 	/*
424a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
425a78e8d2aSDavid Greenman 	 * be shared after an exec.
426a78e8d2aSDavid Greenman 	 */
427426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
428a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
429a78e8d2aSDavid Greenman 		struct filedesc *tmp;
430a78e8d2aSDavid Greenman 
431c522c1bfSAlfred Perlstein 		tmp = fdcopy(td->td_proc->p_fd);
432426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
433b40ce416SJulian Elischer 		fdfree(td);
434a78e8d2aSDavid Greenman 		p->p_fd = tmp;
435426da3bcSAlfred Perlstein 	} else
436426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
437a78e8d2aSDavid Greenman 
438333ea485SGuido van Rooij 	/*
4399b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
4409b3b1c5fSJohn Baldwin 	 */
4419b3b1c5fSJohn Baldwin 	newcred = crget();
4421419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
4439b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
4449b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
4459b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
4469b3b1c5fSJohn Baldwin 
4479b3b1c5fSJohn Baldwin 	/* close files on exec */
4489b3b1c5fSJohn Baldwin 	fdcloseexec(td);
4499b3b1c5fSJohn Baldwin 
450619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
451619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
452619eb6e5SJeff Roberson 
4539b3b1c5fSJohn Baldwin 	/*
454333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
4558688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
456b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
457333ea485SGuido van Rooij 	 * reset.
458333ea485SGuido van Rooij 	 */
4599b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
46090af4afaSJohn Baldwin 	if (sigacts_shared(p->p_sigacts)) {
46190af4afaSJohn Baldwin 		oldsigacts = p->p_sigacts;
4629b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
46390af4afaSJohn Baldwin 		newsigacts = sigacts_alloc();
46490af4afaSJohn Baldwin 		sigacts_copy(newsigacts, oldsigacts);
4659b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
46690af4afaSJohn Baldwin 		p->p_sigacts = newsigacts;
46790af4afaSJohn Baldwin 	} else
46890af4afaSJohn Baldwin 		oldsigacts = NULL;
469333ea485SGuido van Rooij 
470fdf4e8b3SWarner Losh 	/* Stop profiling */
471fdf4e8b3SWarner Losh 	stopprofclock(p);
472fdf4e8b3SWarner Losh 
47326f9a767SRodney W. Grimes 	/* reset caught signals */
47426f9a767SRodney W. Grimes 	execsigs(p);
47526f9a767SRodney W. Grimes 
47626f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
47726f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
47826f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
47926f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
48026f9a767SRodney W. Grimes 
48126f9a767SRodney W. Grimes 	/*
48224b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
483dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
48426f9a767SRodney W. Grimes 	 */
48526f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
48626f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
48726f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
4887f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
48926f9a767SRodney W. Grimes 	}
49026f9a767SRodney W. Grimes 
49126f9a767SRodney W. Grimes 	/*
492a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
493a78e8d2aSDavid Greenman 	 *
494a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
495a78e8d2aSDavid Greenman 	 * the process is being traced.
496670cb89bSRobert Watson 	 *
497670cb89bSRobert Watson 	 * XXXMAC: For the time being, use NOSUID to also prohibit
498670cb89bSRobert Watson 	 * transitions on the file system.
499c52007c2SDavid Greenman 	 */
500b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
501d06c0d4dSRobert Watson 	credential_changing = 0;
502d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
503d06c0d4dSRobert Watson 	    attr.va_uid;
504d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
505d06c0d4dSRobert Watson 	    attr.va_gid;
506ccafe7ebSRobert Watson #ifdef MAC
507670cb89bSRobert Watson 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
508eca8a663SRobert Watson 	    interplabel, imgp);
509ccafe7ebSRobert Watson 	credential_changing |= will_transition;
510ccafe7ebSRobert Watson #endif
511d06c0d4dSRobert Watson 
512d06c0d4dSRobert Watson 	if (credential_changing &&
513a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
514c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
515c52007c2SDavid Greenman 		/*
516c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
517b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
518b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
51926f9a767SRodney W. Grimes 		 */
520b85db196SPeter Wemm 		setsugid(p);
5216c84de02SJohn Baldwin #ifdef KTRACE
522a5881ea5SJohn Baldwin 		if (p->p_tracevp != NULL && suser_cred(oldcred, PRISON_ROOT)) {
5236c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
52479deba82SMatthew Dillon 			p->p_traceflag = 0;
525a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
526a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
527a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
528a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
5296c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
53026f9a767SRodney W. Grimes 		}
5316c84de02SJohn Baldwin #endif
53228b325aaSDon Lewis 		/*
533c1e2d386SNate Lawson 		 * Close any file descriptors 0..2 that reference procfs,
534c1e2d386SNate Lawson 		 * then make sure file descriptors 0..2 are in use.
53528b325aaSDon Lewis 		 *
536c1e2d386SNate Lawson 		 * setugidsafety() may call closef() and then pfind()
537c1e2d386SNate Lawson 		 * which may grab the process lock.
53828b325aaSDon Lewis 		 * fdcheckstd() may call falloc() which may block to
53928b325aaSDon Lewis 		 * allocate memory, so temporarily drop the process lock.
54028b325aaSDon Lewis 		 */
54128b325aaSDon Lewis 		PROC_UNLOCK(p);
542c1e2d386SNate Lawson 		setugidsafety(td);
543e983a376SJacques Vidrine 		error = fdcheckstd(td);
54463c9e754SJohn Baldwin 		if (error != 0)
54569be5db9SAlfred Perlstein 			goto done1;
546e1b1aa3bSJohn Baldwin 		PROC_LOCK(p);
547c52007c2SDavid Greenman 		/*
548c52007c2SDavid Greenman 		 * Set the new credentials.
549c52007c2SDavid Greenman 		 */
5509b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
551c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
5521419eacbSAlfred Perlstein 			change_euid(newcred, euip);
553c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
554b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
555ccafe7ebSRobert Watson #ifdef MAC
556670cb89bSRobert Watson 		if (will_transition) {
557670cb89bSRobert Watson 			mac_execve_transition(oldcred, newcred, imgp->vp,
558eca8a663SRobert Watson 			    interplabel, imgp);
559670cb89bSRobert Watson 		}
560ccafe7ebSRobert Watson #endif
5619b3b1c5fSJohn Baldwin 		/*
5629b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
563ccafe7ebSRobert Watson 		 *
564ccafe7ebSRobert Watson 		 * XXXMAC: Note that the current logic will save the
565ccafe7ebSRobert Watson 		 * uid and gid if a MAC domain transition occurs, even
566ccafe7ebSRobert Watson 		 * though maybe it shouldn't.
5679b3b1c5fSJohn Baldwin 		 */
5689b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
5699b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
5709b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
5719b3b1c5fSJohn Baldwin 		newcred = NULL;
572c52007c2SDavid Greenman 	} else {
573b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
574b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
575c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
57626f9a767SRodney W. Grimes 		/*
577c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
578b1fc0ec1SRobert Watson 		 *
579b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
5809b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
5819b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
5829b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
5839b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
5849b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
5859b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
5869b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
58726f9a767SRodney W. Grimes 		 */
588b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
589b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
5909b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
591b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
592b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
593b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
5949b3b1c5fSJohn Baldwin 			newcred = NULL;
5959b3b1c5fSJohn Baldwin 		}
596b1fc0ec1SRobert Watson 	}
59726f9a767SRodney W. Grimes 
59826f9a767SRodney W. Grimes 	/*
599619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
600619eb6e5SJeff Roberson 	 * to locking the proc lock.
6012a531c80SDavid Greenman 	 */
6029b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
6032a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
6042a531c80SDavid Greenman 
6052a531c80SDavid Greenman 	/*
6069ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
6079ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
608cb679c38SJonathan Lemon 	 */
609cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
6109ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
611cb679c38SJonathan Lemon 
612cb679c38SJonathan Lemon 	/*
61326f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
61426f9a767SRodney W. Grimes 	 * can be set before the program executes.
61526f9a767SRodney W. Grimes 	 */
61626f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
61726f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
61826f9a767SRodney W. Grimes 
61926f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
62026f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
62126f9a767SRodney W. Grimes 
622b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
6239b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
624b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
625b9df5231SPoul-Henning Kamp 
626e1b1aa3bSJohn Baldwin 	/* Cache arguments if they fit inside our allowance */
627e1b1aa3bSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
628e1b1aa3bSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
629e1b1aa3bSJohn Baldwin 		p->p_args = newargs;
630e1b1aa3bSJohn Baldwin 		newargs = NULL;
631e1b1aa3bSJohn Baldwin 	}
632e1b1aa3bSJohn Baldwin 	PROC_UNLOCK(p);
633e1b1aa3bSJohn Baldwin 
634e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
6353ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
6363ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
6373ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
6383ebc1248SPeter Wemm 	else
639bafbd492SJake Burkholder 		exec_setregs(td, imgp->entry_addr,
640bafbd492SJake Burkholder 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
641e913ca22SDoug Rabson 
64269be5db9SAlfred Perlstein done1:
6439b3b1c5fSJohn Baldwin 	/*
6449b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
6459b3b1c5fSJohn Baldwin 	 */
6461419eacbSAlfred Perlstein 	uifree(euip);
6479b3b1c5fSJohn Baldwin 	if (newcred == NULL)
6489b3b1c5fSJohn Baldwin 		crfree(oldcred);
6499b3b1c5fSJohn Baldwin 	else
6509b3b1c5fSJohn Baldwin 		crfree(newcred);
6519b3b1c5fSJohn Baldwin 	/*
6529b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
6539b3b1c5fSJohn Baldwin 	 */
6549b3b1c5fSJohn Baldwin 	if (textvp != NULL)
6559b3b1c5fSJohn Baldwin 		vrele(textvp);
656619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
657619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
6586c84de02SJohn Baldwin #ifdef KTRACE
6599b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
6609b3b1c5fSJohn Baldwin 		vrele(tracevp);
661a5881ea5SJohn Baldwin 	if (tracecred != NULL)
662a5881ea5SJohn Baldwin 		crfree(tracecred);
6636c84de02SJohn Baldwin #endif
66469be5db9SAlfred Perlstein 	if (oldargs != NULL)
6659b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
66669be5db9SAlfred Perlstein 	if (newargs != NULL)
66769be5db9SAlfred Perlstein 		pargs_drop(newargs);
66890af4afaSJohn Baldwin 	if (oldsigacts != NULL)
66990af4afaSJohn Baldwin 		sigacts_free(oldsigacts);
670b9df5231SPoul-Henning Kamp 
6711616db3cSJohn Dyson exec_fail_dealloc:
6721616db3cSJohn Dyson 
67326f9a767SRodney W. Grimes 	/*
67426f9a767SRodney W. Grimes 	 * free various allocated resources
67526f9a767SRodney W. Grimes 	 */
6761616db3cSJohn Dyson 	if (imgp->firstpage)
6771616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
67826f9a767SRodney W. Grimes 
6799c0fed3dSDoug Rabson 	if (imgp->vp) {
680762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
681619eb6e5SJeff Roberson 		vput(imgp->vp);
682a3cf6ebaSDavid Greenman 	}
68326f9a767SRodney W. Grimes 
684a9a08882SJeff Roberson 	if (imgp->stringbase != NULL)
685a9a08882SJeff Roberson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
686a9a08882SJeff Roberson 		    ARG_MAX + PAGE_SIZE);
687a9a08882SJeff Roberson 
6880b2ed1aeSJeff Roberson 	if (imgp->object)
6890b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
6900b2ed1aeSJeff Roberson 
691d1989db5SRobert Drehmel 	if (error == 0) {
692d1989db5SRobert Drehmel 		/*
693d1989db5SRobert Drehmel 		 * Stop the process here if its stop event mask has
694d1989db5SRobert Drehmel 		 * the S_EXEC bit set.
695d1989db5SRobert Drehmel 		 */
696d1989db5SRobert Drehmel 		STOPEVENT(p, S_EXEC, 0);
697116734c4SMatthew Dillon 		goto done2;
698d1989db5SRobert Drehmel 	}
6991616db3cSJohn Dyson 
70026f9a767SRodney W. Grimes exec_fail:
7019ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
7029ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
7039ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
7049ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
7059ca45e81SDag-Erling Smørgrav 
706c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
70726f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
708670cb89bSRobert Watson #ifdef MAC
709670cb89bSRobert Watson 		mac_execve_exit(imgp);
710eca8a663SRobert Watson 		if (interplabel != NULL)
711eca8a663SRobert Watson 			mac_vnode_label_free(interplabel);
712670cb89bSRobert Watson #endif
713b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
71426f9a767SRodney W. Grimes 		/* NOT REACHED */
715116734c4SMatthew Dillon 		error = 0;
71626f9a767SRodney W. Grimes 	}
717116734c4SMatthew Dillon done2:
718670cb89bSRobert Watson #ifdef MAC
719670cb89bSRobert Watson 	mac_execve_exit(imgp);
720eca8a663SRobert Watson 	if (interplabel != NULL)
721eca8a663SRobert Watson 		mac_vnode_label_free(interplabel);
722670cb89bSRobert Watson #endif
723116734c4SMatthew Dillon 	mtx_unlock(&Giant);
724116734c4SMatthew Dillon 	return (error);
72526f9a767SRodney W. Grimes }
72626f9a767SRodney W. Grimes 
7271616db3cSJohn Dyson int
7281616db3cSJohn Dyson exec_map_first_page(imgp)
7291616db3cSJohn Dyson 	struct image_params *imgp;
7301616db3cSJohn Dyson {
731d8aad40cSJohn Baldwin 	int rv, i;
73295461b45SJohn Dyson 	int initial_pagein;
73395461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
7341616db3cSJohn Dyson 	vm_object_t object;
7351616db3cSJohn Dyson 
7360cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
7371616db3cSJohn Dyson 
7381616db3cSJohn Dyson 	if (imgp->firstpage) {
7391616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
7401616db3cSJohn Dyson 	}
7411616db3cSJohn Dyson 
7429ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
743fd0cc9a8SAlan Cox 	VM_OBJECT_LOCK(object);
74495461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
74595461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
74695461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
74795461b45SJohn Dyson 		if (initial_pagein > object->size)
74895461b45SJohn Dyson 			initial_pagein = object->size;
74995461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
750d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
7516ec2fca5SAlan Cox 				if (ma[i]->valid)
7526ec2fca5SAlan Cox 					break;
753ee113343SAlan Cox 				vm_page_lock_queues();
75406fa71cdSAlan Cox 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) {
75506fa71cdSAlan Cox 					vm_page_unlock_queues();
75606fa71cdSAlan Cox 					break;
75706fa71cdSAlan Cox 				}
758e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
759ee113343SAlan Cox 				vm_page_unlock_queues();
76095461b45SJohn Dyson 			} else {
761ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
762ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
76395461b45SJohn Dyson 				if (ma[i] == NULL)
76495461b45SJohn Dyson 					break;
76595461b45SJohn Dyson 			}
76695461b45SJohn Dyson 		}
76795461b45SJohn Dyson 		initial_pagein = i;
76895461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
76995461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
770ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
771ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
772ecbb00a2SDoug Rabson 			if (ma[0]) {
7736ec2fca5SAlan Cox 				vm_page_lock_queues();
7744fec79beSAlan Cox 				pmap_remove_all(ma[0]);
7758f9110f6SJohn Dyson 				vm_page_free(ma[0]);
77606fa71cdSAlan Cox 				vm_page_unlock_queues();
7776ec2fca5SAlan Cox 			}
77806fa71cdSAlan Cox 			VM_OBJECT_UNLOCK(object);
779a7cddfedSJake Burkholder 			return (EIO);
7801616db3cSJohn Dyson 		}
7811616db3cSJohn Dyson 	}
7826ec2fca5SAlan Cox 	vm_page_lock_queues();
78395461b45SJohn Dyson 	vm_page_wire(ma[0]);
784e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
78597646f56SAlan Cox 	vm_page_unlock_queues();
7866ec2fca5SAlan Cox 	VM_OBJECT_UNLOCK(object);
7871616db3cSJohn Dyson 
788ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
78995461b45SJohn Dyson 	imgp->firstpage = ma[0];
7901616db3cSJohn Dyson 
791a7cddfedSJake Burkholder 	return (0);
7921616db3cSJohn Dyson }
7931616db3cSJohn Dyson 
7941616db3cSJohn Dyson void
7951616db3cSJohn Dyson exec_unmap_first_page(imgp)
7961616db3cSJohn Dyson 	struct image_params *imgp;
7971616db3cSJohn Dyson {
79823955314SAlfred Perlstein 
7991616db3cSJohn Dyson 	if (imgp->firstpage) {
800ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
80197646f56SAlan Cox 		vm_page_lock_queues();
80273007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
80397646f56SAlan Cox 		vm_page_unlock_queues();
8041616db3cSJohn Dyson 		imgp->firstpage = NULL;
8051616db3cSJohn Dyson 	}
8061616db3cSJohn Dyson }
8071616db3cSJohn Dyson 
80826f9a767SRodney W. Grimes /*
80926f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
81026f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
81126f9a767SRodney W. Grimes  *	automatically in trap.c.
81226f9a767SRodney W. Grimes  */
81326f9a767SRodney W. Grimes int
81405ba50f5SJake Burkholder exec_new_vmspace(imgp, sv)
815c52007c2SDavid Greenman 	struct image_params *imgp;
81605ba50f5SJake Burkholder 	struct sysentvec *sv;
81726f9a767SRodney W. Grimes {
81826f9a767SRodney W. Grimes 	int error;
8195e20c11fSAlan Cox 	struct proc *p = imgp->proc;
8205e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
82105ba50f5SJake Burkholder 	vm_offset_t stack_addr;
82205ba50f5SJake Burkholder 	vm_map_t map;
82326f9a767SRodney W. Grimes 
8240cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
8250cddd8f0SMatthew Dillon 
826c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
82726f9a767SRodney W. Grimes 
82875b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exec, p);
8296f5dafeaSAlan Cox 
8306f5dafeaSAlan Cox 	/*
831c460ac3aSPeter Wemm 	 * Here is as good a place as any to do any resource limit cleanups.
832c460ac3aSPeter Wemm 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
833c460ac3aSPeter Wemm 	 * data size limit may need to be changed to a value that makes
834c460ac3aSPeter Wemm 	 * sense for the 32 bit binary.
835c460ac3aSPeter Wemm 	 */
836c460ac3aSPeter Wemm 	if (sv->sv_fixlimits)
837c460ac3aSPeter Wemm 		sv->sv_fixlimits(imgp);
838c460ac3aSPeter Wemm 
839c460ac3aSPeter Wemm 	/*
840af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
841af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
842af9ec885SJohn Dyson 	 * not disrupted
843af9ec885SJohn Dyson 	 */
84405ba50f5SJake Burkholder 	map = &vmspace->vm_map;
84505ba50f5SJake Burkholder 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
84605ba50f5SJake Burkholder 	    vm_map_max(map) == sv->sv_maxuser) {
8473db161e0SMatthew Dillon 		shmexit(vmspace);
8482d21129dSAlan Cox 		vm_page_lock_queues();
84905ba50f5SJake Burkholder 		pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
85005ba50f5SJake Burkholder 		    vm_map_max(map));
8512d21129dSAlan Cox 		vm_page_unlock_queues();
85205ba50f5SJake Burkholder 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
853af9ec885SJohn Dyson 	} else {
85405ba50f5SJake Burkholder 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
8555e20c11fSAlan Cox 		vmspace = p->p_vmspace;
85605ba50f5SJake Burkholder 		map = &vmspace->vm_map;
857af9ec885SJohn Dyson 	}
85826f9a767SRodney W. Grimes 
85926f9a767SRodney W. Grimes 	/* Allocate a new stack */
860fd75d710SMarcel Moolenaar 	stack_addr = sv->sv_usrstack - maxssiz;
86105ba50f5SJake Burkholder 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
862fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
8632267af78SJulian Elischer 	if (error)
8642267af78SJulian Elischer 		return (error);
8652267af78SJulian Elischer 
86663c47a5cSDoug Rabson #ifdef __ia64__
867fd75d710SMarcel Moolenaar 	/* Allocate a new register stack */
868bab1f052SMarcel Moolenaar 	stack_addr = IA64_BACKINGSTORE;
869fd75d710SMarcel Moolenaar 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
870fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
871fd75d710SMarcel Moolenaar 	if (error)
872fd75d710SMarcel Moolenaar 		return (error);
87363c47a5cSDoug Rabson #endif
87463c47a5cSDoug Rabson 
8752267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
8762267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
8772267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
8782267af78SJulian Elischer 	 */
879cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
88005ba50f5SJake Burkholder 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
88126f9a767SRodney W. Grimes 
88226f9a767SRodney W. Grimes 	return (0);
88326f9a767SRodney W. Grimes }
88426f9a767SRodney W. Grimes 
88526f9a767SRodney W. Grimes /*
88626f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
88726f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
88826f9a767SRodney W. Grimes  */
88926f9a767SRodney W. Grimes int
890c52007c2SDavid Greenman exec_extract_strings(imgp)
891c52007c2SDavid Greenman 	struct image_params *imgp;
89226f9a767SRodney W. Grimes {
89326f9a767SRodney W. Grimes 	char	**argv, **envv;
89426f9a767SRodney W. Grimes 	char	*argp, *envp;
895ecbb00a2SDoug Rabson 	int	error;
896ecbb00a2SDoug Rabson 	size_t	length;
89726f9a767SRodney W. Grimes 
89826f9a767SRodney W. Grimes 	/*
89926f9a767SRodney W. Grimes 	 * extract arguments first
90026f9a767SRodney W. Grimes 	 */
90126f9a767SRodney W. Grimes 
902450ffb44SRobert Watson 	argv = imgp->userspace_argv;
90326f9a767SRodney W. Grimes 
9040fd1a014SDavid Greenman 	if (argv) {
905aae0aa45SBruce Evans 		argp = (caddr_t)(intptr_t)fuword(argv);
9065cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t)-1)
9075cf3d12cSAndrey A. Chernov 			return (EFAULT);
9085cf3d12cSAndrey A. Chernov 		if (argp)
9095cf3d12cSAndrey A. Chernov 			argv++;
9105cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
9115cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
9125cf3d12cSAndrey A. Chernov 		if (argp) {
9135cf3d12cSAndrey A. Chernov 			do {
91426f9a767SRodney W. Grimes 				if (argp == (caddr_t)-1)
91526f9a767SRodney W. Grimes 					return (EFAULT);
916c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
917c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
9180fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
91926f9a767SRodney W. Grimes 						return (E2BIG);
9200fd1a014SDavid Greenman 					return (error);
9210fd1a014SDavid Greenman 				}
922c52007c2SDavid Greenman 				imgp->stringspace -= length;
923c52007c2SDavid Greenman 				imgp->stringp += length;
924c52007c2SDavid Greenman 				imgp->argc++;
925aae0aa45SBruce Evans 			} while ((argp = (caddr_t)(intptr_t)fuword(argv++)));
92626f9a767SRodney W. Grimes 		}
9270fd1a014SDavid Greenman 	}
92826f9a767SRodney W. Grimes 
929b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
930b9df5231SPoul-Henning Kamp 
93126f9a767SRodney W. Grimes 	/*
93226f9a767SRodney W. Grimes 	 * extract environment strings
93326f9a767SRodney W. Grimes 	 */
93426f9a767SRodney W. Grimes 
935450ffb44SRobert Watson 	envv = imgp->userspace_envv;
93626f9a767SRodney W. Grimes 
9370fd1a014SDavid Greenman 	if (envv) {
938aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
93926f9a767SRodney W. Grimes 			if (envp == (caddr_t)-1)
94026f9a767SRodney W. Grimes 				return (EFAULT);
941c52007c2SDavid Greenman 			if ((error = copyinstr(envp, 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->envc++;
95026f9a767SRodney W. Grimes 		}
9510fd1a014SDavid Greenman 	}
95226f9a767SRodney W. Grimes 
95326f9a767SRodney W. Grimes 	return (0);
95426f9a767SRodney W. Grimes }
95526f9a767SRodney W. Grimes 
95626f9a767SRodney W. Grimes /*
95726f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
95826f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
95926f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
96026f9a767SRodney W. Grimes  */
961654f6be1SBruce Evans register_t *
962c52007c2SDavid Greenman exec_copyout_strings(imgp)
963c52007c2SDavid Greenman 	struct image_params *imgp;
96426f9a767SRodney W. Grimes {
96526f9a767SRodney W. Grimes 	int argc, envc;
96626f9a767SRodney W. Grimes 	char **vectp;
96726f9a767SRodney W. Grimes 	char *stringp, *destp;
968654f6be1SBruce Evans 	register_t *stack_base;
96993f6448cSDavid Greenman 	struct ps_strings *arginfo;
970f3bec5d7SJake Burkholder 	struct proc *p;
971d66a5066SPeter Wemm 	int szsigcode;
97226f9a767SRodney W. Grimes 
97326f9a767SRodney W. Grimes 	/*
97426f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
975d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
97626f9a767SRodney W. Grimes 	 */
977f3bec5d7SJake Burkholder 	p = imgp->proc;
978f3bec5d7SJake Burkholder 	szsigcode = 0;
97905ba50f5SJake Burkholder 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
980f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
981f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
982d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
9831ed012f9SPeter Wemm 	    roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
9841ed012f9SPeter Wemm 
98526f9a767SRodney W. Grimes 	/*
986d66a5066SPeter Wemm 	 * install sigcode
987d66a5066SPeter Wemm 	 */
988d66a5066SPeter Wemm 	if (szsigcode)
989f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
990f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
991d66a5066SPeter Wemm 
992d66a5066SPeter Wemm 	/*
993e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
994e1743d02SSøren Schmidt 	 * on the stack.
995e1743d02SSøren Schmidt 	 */
996b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
997e1743d02SSøren Schmidt 		/*
998b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
999b9a22da4STakanori Watanabe 		 * lower compatibility.
1000b9a22da4STakanori Watanabe 		 */
1001ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1002ca0387efSJake Burkholder 		    (AT_COUNT * 2);
1003b9a22da4STakanori Watanabe 		/*
1004b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1005b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
1006b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
1007e1743d02SSøren Schmidt 		 */
1008e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
1009b9a22da4STakanori Watanabe 		    imgp->auxarg_size) * sizeof(char *));
1010b9a22da4STakanori Watanabe 
1011b9a22da4STakanori Watanabe 	} else
1012e1743d02SSøren Schmidt 		/*
1013b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1014b9a22da4STakanori Watanabe 		 * the arg and env vector sets
101526f9a767SRodney W. Grimes 		 */
1016ca0387efSJake Burkholder 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) *
1017ca0387efSJake Burkholder 		    sizeof(char *));
101826f9a767SRodney W. Grimes 
101926f9a767SRodney W. Grimes 	/*
102026f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
102126f9a767SRodney W. Grimes 	 */
1022654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
102326f9a767SRodney W. Grimes 
1024c52007c2SDavid Greenman 	stringp = imgp->stringbase;
1025c52007c2SDavid Greenman 	argc = imgp->argc;
1026c52007c2SDavid Greenman 	envc = imgp->envc;
102726f9a767SRodney W. Grimes 
102893f6448cSDavid Greenman 	/*
10293aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
103093f6448cSDavid Greenman 	 */
1031c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
103293f6448cSDavid Greenman 
103393f6448cSDavid Greenman 	/*
10343aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
10353aed948bSDavid Greenman 	 */
1036aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
10373aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
10383aed948bSDavid Greenman 
10393aed948bSDavid Greenman 	/*
10403aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
104193f6448cSDavid Greenman 	 */
104226f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
1043aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10443aed948bSDavid Greenman 		while (*stringp++ != 0)
10453aed948bSDavid Greenman 			destp++;
10463aed948bSDavid Greenman 		destp++;
104726f9a767SRodney W. Grimes 	}
104826f9a767SRodney W. Grimes 
10491a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
10506ab46d52SBruce Evans 	suword(vectp++, 0);
105126f9a767SRodney W. Grimes 
1052aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
10533aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
105493f6448cSDavid Greenman 
105593f6448cSDavid Greenman 	/*
10563aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
105793f6448cSDavid Greenman 	 */
105826f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
1059aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
10603aed948bSDavid Greenman 		while (*stringp++ != 0)
10613aed948bSDavid Greenman 			destp++;
10623aed948bSDavid Greenman 		destp++;
106326f9a767SRodney W. Grimes 	}
106426f9a767SRodney W. Grimes 
106526f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
10666ab46d52SBruce Evans 	suword(vectp, 0);
106726f9a767SRodney W. Grimes 
106826f9a767SRodney W. Grimes 	return (stack_base);
106926f9a767SRodney W. Grimes }
107026f9a767SRodney W. Grimes 
107126f9a767SRodney W. Grimes /*
107226f9a767SRodney W. Grimes  * Check permissions of file to execute.
1073cf64863aSRobert Watson  *	Called with imgp->vp locked.
107426f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
107526f9a767SRodney W. Grimes  */
1076c8a79999SPeter Wemm int
1077c52007c2SDavid Greenman exec_check_permissions(imgp)
1078c52007c2SDavid Greenman 	struct image_params *imgp;
107926f9a767SRodney W. Grimes {
1080c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
1081c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
1082a854ed98SJohn Baldwin 	struct thread *td;
108326f9a767SRodney W. Grimes 	int error;
108426f9a767SRodney W. Grimes 
1085a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
1086339b79b9SRobert Watson 
1087ec35c2afSRobert Watson 	/* Get file attributes */
1088ec35c2afSRobert Watson 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1089ec35c2afSRobert Watson 	if (error)
1090ec35c2afSRobert Watson 		return (error);
1091ec35c2afSRobert Watson 
1092339b79b9SRobert Watson #ifdef MAC
1093670cb89bSRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1094339b79b9SRobert Watson 	if (error)
1095339b79b9SRobert Watson 		return (error);
1096339b79b9SRobert Watson #endif
1097339b79b9SRobert Watson 
109826f9a767SRodney W. Grimes 	/*
109926f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
110026f9a767SRodney W. Grimes 	 *	file resides on.
110126f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
110226f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
110326f9a767SRodney W. Grimes 	 *	file really is executable.
110426f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
110526f9a767SRodney W. Grimes 	 */
1106c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
110726f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
1108a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
110926f9a767SRodney W. Grimes 		return (EACCES);
111026f9a767SRodney W. Grimes 
111126f9a767SRodney W. Grimes 	/*
111226f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
111326f9a767SRodney W. Grimes 	 */
111426f9a767SRodney W. Grimes 	if (attr->va_size == 0)
111526f9a767SRodney W. Grimes 		return (ENOEXEC);
111626f9a767SRodney W. Grimes 
111726f9a767SRodney W. Grimes 	/*
111826f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
111926f9a767SRodney W. Grimes 	 */
1120a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
112126f9a767SRodney W. Grimes 	if (error)
112226f9a767SRodney W. Grimes 		return (error);
112326f9a767SRodney W. Grimes 
11246d5a0a8cSDavid Greenman 	/*
11256d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
11266d5a0a8cSDavid Greenman 	 * if there are any.
11276d5a0a8cSDavid Greenman 	 */
11286d5a0a8cSDavid Greenman 	if (vp->v_writecount)
11296d5a0a8cSDavid Greenman 		return (ETXTBSY);
11306d5a0a8cSDavid Greenman 
11316d5a0a8cSDavid Greenman 	/*
11326d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
11336d5a0a8cSDavid Greenman 	 * general case).
11346d5a0a8cSDavid Greenman 	 */
1135a8d43c90SPoul-Henning Kamp 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
113626f9a767SRodney W. Grimes 	return (error);
1137df8bae1dSRodney W. Grimes }
1138aa855a59SPeter Wemm 
1139aa855a59SPeter Wemm /*
1140aa855a59SPeter Wemm  * Exec handler registration
1141aa855a59SPeter Wemm  */
1142aa855a59SPeter Wemm int
1143aa855a59SPeter Wemm exec_register(execsw_arg)
1144aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1145aa855a59SPeter Wemm {
1146aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1147aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1148aa855a59SPeter Wemm 
1149aa855a59SPeter Wemm 	if (execsw)
1150aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1151aa855a59SPeter Wemm 			count++;
1152a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1153aa855a59SPeter Wemm 	if (newexecsw == NULL)
1154a7cddfedSJake Burkholder 		return (ENOMEM);
1155aa855a59SPeter Wemm 	xs = newexecsw;
1156aa855a59SPeter Wemm 	if (execsw)
1157aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1158aa855a59SPeter Wemm 			*xs++ = *es;
1159aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1160aa855a59SPeter Wemm 	*xs = NULL;
1161aa855a59SPeter Wemm 	if (execsw)
1162aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1163aa855a59SPeter Wemm 	execsw = newexecsw;
1164a7cddfedSJake Burkholder 	return (0);
1165aa855a59SPeter Wemm }
1166aa855a59SPeter Wemm 
1167aa855a59SPeter Wemm int
1168aa855a59SPeter Wemm exec_unregister(execsw_arg)
1169aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1170aa855a59SPeter Wemm {
1171aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1172aa855a59SPeter Wemm 	int count = 1;
1173aa855a59SPeter Wemm 
1174aa855a59SPeter Wemm 	if (execsw == NULL)
1175aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1176aa855a59SPeter Wemm 
1177aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1178aa855a59SPeter Wemm 		if (*es == execsw_arg)
1179aa855a59SPeter Wemm 			break;
1180aa855a59SPeter Wemm 	}
1181aa855a59SPeter Wemm 	if (*es == NULL)
1182a7cddfedSJake Burkholder 		return (ENOENT);
1183aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1184aa855a59SPeter Wemm 		if (*es != execsw_arg)
1185aa855a59SPeter Wemm 			count++;
1186a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1187aa855a59SPeter Wemm 	if (newexecsw == NULL)
1188a7cddfedSJake Burkholder 		return (ENOMEM);
1189aa855a59SPeter Wemm 	xs = newexecsw;
1190aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1191aa855a59SPeter Wemm 		if (*es != execsw_arg)
1192aa855a59SPeter Wemm 			*xs++ = *es;
1193aa855a59SPeter Wemm 	*xs = NULL;
1194aa855a59SPeter Wemm 	if (execsw)
1195aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1196aa855a59SPeter Wemm 	execsw = newexecsw;
1197a7cddfedSJake Burkholder 	return (0);
1198aa855a59SPeter Wemm }
1199