xref: /freebsd/sys/kern/kern_exec.c (revision ae1078d657c7d7618e23659c11221d5be94f05bd)
19454b2d8SWarner Losh /*-
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 
304da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
311e9b3d91SPeter Wemm #include "opt_ktrace.h"
32339b79b9SRobert Watson #include "opt_mac.h"
331e9b3d91SPeter Wemm 
34df8bae1dSRodney W. Grimes #include <sys/param.h>
3526f9a767SRodney W. Grimes #include <sys/systm.h>
3675b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
37fb919e4dSMark Murray #include <sys/lock.h>
38fb919e4dSMark Murray #include <sys/mutex.h>
39d2d3e875SBruce Evans #include <sys/sysproto.h>
4026f9a767SRodney W. Grimes #include <sys/signalvar.h>
4126f9a767SRodney W. Grimes #include <sys/kernel.h>
42339b79b9SRobert Watson #include <sys/mac.h>
4326f9a767SRodney W. Grimes #include <sys/mount.h>
44797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
45079cc25bSDavid Greenman #include <sys/fcntl.h>
4626f9a767SRodney W. Grimes #include <sys/acct.h>
4726f9a767SRodney W. Grimes #include <sys/exec.h>
4826f9a767SRodney W. Grimes #include <sys/imgact.h>
49e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
5026f9a767SRodney W. Grimes #include <sys/wait.h>
51333ea485SGuido van Rooij #include <sys/malloc.h>
52a794e791SBruce Evans #include <sys/proc.h>
532a024a2bSSean Eric Fagan #include <sys/pioctl.h>
54a794e791SBruce Evans #include <sys/namei.h>
556004362eSDavid Schultz #include <sys/resourcevar.h>
5659c8bc40SAlan Cox #include <sys/sf_buf.h>
5784e0b075SDavid Xu #include <sys/syscallsubr.h>
581e1e0b44SSøren Schmidt #include <sys/sysent.h>
59797f2d22SPoul-Henning Kamp #include <sys/shm.h>
6099ac3bc8SPeter Wemm #include <sys/sysctl.h>
61a794e791SBruce Evans #include <sys/vnode.h>
62c781aea8SPeter Wemm #ifdef KTRACE
63c781aea8SPeter Wemm #include <sys/ktrace.h>
64c781aea8SPeter Wemm #endif
6526f9a767SRodney W. Grimes 
6626f9a767SRodney W. Grimes #include <vm/vm.h>
67efeaf95aSDavid Greenman #include <vm/vm_param.h>
68efeaf95aSDavid Greenman #include <vm/pmap.h>
691616db3cSJohn Dyson #include <vm/vm_page.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
7126f9a767SRodney W. Grimes #include <vm/vm_kern.h>
72efeaf95aSDavid Greenman #include <vm/vm_extern.h>
731ebd0c59SDavid Greenman #include <vm/vm_object.h>
741616db3cSJohn Dyson #include <vm/vm_pager.h>
7526f9a767SRodney W. Grimes 
76ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
77ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
78ebccf1e3SJoseph Koshy #endif
79ebccf1e3SJoseph Koshy 
8026f9a767SRodney W. Grimes #include <machine/reg.h>
8126f9a767SRodney W. Grimes 
82ae1078d6SWayne Salamon #include <security/audit/audit.h>
83ae1078d6SWayne Salamon 
84b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
85b9df5231SPoul-Henning Kamp 
8605ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
8705ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
885dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
89610ecfe0SMaxim Sobolev static int do_execve(struct thread *td, struct image_args *args,
90610ecfe0SMaxim Sobolev     struct mac *mac_p);
918917b8d2SJohn Baldwin static void exec_free_args(struct image_args *);
9205ba50f5SJake Burkholder 
939701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
9505ba50f5SJake Burkholder     NULL, 0, sysctl_kern_ps_strings, "LU", "");
96486bddb0SDoug Rabson 
979701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9805ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
9905ba50f5SJake Burkholder     NULL, 0, sysctl_kern_usrstack, "LU", "");
10099ac3bc8SPeter Wemm 
1015dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
1025dadd17bSJake Burkholder     NULL, 0, sysctl_kern_stackprot, "I", "");
1035dadd17bSJake Burkholder 
104b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
105c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
1069701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
107b9df5231SPoul-Henning Kamp 
10805ba50f5SJake Burkholder static int
10905ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
11005ba50f5SJake Burkholder {
11105ba50f5SJake Burkholder 	struct proc *p;
112df7c361eSPeter Wemm 	int error;
11305ba50f5SJake Burkholder 
11405ba50f5SJake Burkholder 	p = curproc;
115a7bc3102SPeter Wemm #ifdef SCTL_MASK32
116a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
117df7c361eSPeter Wemm 		unsigned int val;
118df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_psstrings;
119df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
120df7c361eSPeter Wemm 	} else
121df7c361eSPeter Wemm #endif
122df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
123df7c361eSPeter Wemm 		   sizeof(p->p_sysent->sv_psstrings));
124df7c361eSPeter Wemm 	return error;
12505ba50f5SJake Burkholder }
12605ba50f5SJake Burkholder 
12705ba50f5SJake Burkholder static int
12805ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
12905ba50f5SJake Burkholder {
13005ba50f5SJake Burkholder 	struct proc *p;
131df7c361eSPeter Wemm 	int error;
13205ba50f5SJake Burkholder 
13305ba50f5SJake Burkholder 	p = curproc;
134a7bc3102SPeter Wemm #ifdef SCTL_MASK32
135a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
136df7c361eSPeter Wemm 		unsigned int val;
137df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_usrstack;
138df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
139df7c361eSPeter Wemm 	} else
140df7c361eSPeter Wemm #endif
141df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
142df7c361eSPeter Wemm 		    sizeof(p->p_sysent->sv_usrstack));
143df7c361eSPeter Wemm 	return error;
14405ba50f5SJake Burkholder }
14505ba50f5SJake Burkholder 
1465dadd17bSJake Burkholder static int
1475dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
1485dadd17bSJake Burkholder {
1495dadd17bSJake Burkholder 	struct proc *p;
1505dadd17bSJake Burkholder 
1515dadd17bSJake Burkholder 	p = curproc;
1525dadd17bSJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
1535dadd17bSJake Burkholder 	    sizeof(p->p_sysent->sv_stackprot)));
1545dadd17bSJake Burkholder }
1555dadd17bSJake Burkholder 
15699ac3bc8SPeter Wemm /*
157aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
158aa855a59SPeter Wemm  * double pointer here.
159df8bae1dSRodney W. Grimes  */
160aa855a59SPeter Wemm static const struct execsw **execsw;
16126f9a767SRodney W. Grimes 
162ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
163ca46e90eSBruce Evans struct execve_args {
164ca46e90eSBruce Evans 	char    *fname;
165ca46e90eSBruce Evans 	char    **argv;
166ca46e90eSBruce Evans 	char    **envv;
167ca46e90eSBruce Evans };
168ca46e90eSBruce Evans #endif
169ca46e90eSBruce Evans 
170ca46e90eSBruce Evans /*
171ca46e90eSBruce Evans  * MPSAFE
172ca46e90eSBruce Evans  */
173ca46e90eSBruce Evans int
174ca46e90eSBruce Evans execve(td, uap)
175ca46e90eSBruce Evans 	struct thread *td;
176ca46e90eSBruce Evans 	struct execve_args /* {
177ca46e90eSBruce Evans 		char *fname;
178ca46e90eSBruce Evans 		char **argv;
179ca46e90eSBruce Evans 		char **envv;
180ca46e90eSBruce Evans 	} */ *uap;
181ca46e90eSBruce Evans {
182610ecfe0SMaxim Sobolev 	int error;
183610ecfe0SMaxim Sobolev 	struct image_args args;
184ca46e90eSBruce Evans 
185610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
186610ecfe0SMaxim Sobolev 	    uap->argv, uap->envv);
187610ecfe0SMaxim Sobolev 	if (error == 0)
188610ecfe0SMaxim Sobolev 		error = kern_execve(td, &args, NULL);
189610ecfe0SMaxim Sobolev 	return (error);
190ca46e90eSBruce Evans }
191ca46e90eSBruce Evans 
192ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
193ca46e90eSBruce Evans struct __mac_execve_args {
194ca46e90eSBruce Evans 	char	*fname;
195ca46e90eSBruce Evans 	char	**argv;
196ca46e90eSBruce Evans 	char	**envv;
197ca46e90eSBruce Evans 	struct mac	*mac_p;
198ca46e90eSBruce Evans };
199ca46e90eSBruce Evans #endif
200ca46e90eSBruce Evans 
201ca46e90eSBruce Evans /*
202ca46e90eSBruce Evans  * MPSAFE
203ca46e90eSBruce Evans  */
204ca46e90eSBruce Evans int
205ca46e90eSBruce Evans __mac_execve(td, uap)
206ca46e90eSBruce Evans 	struct thread *td;
207ca46e90eSBruce Evans 	struct __mac_execve_args /* {
208ca46e90eSBruce Evans 		char *fname;
209ca46e90eSBruce Evans 		char **argv;
210ca46e90eSBruce Evans 		char **envv;
211ca46e90eSBruce Evans 		struct mac *mac_p;
212ca46e90eSBruce Evans 	} */ *uap;
213ca46e90eSBruce Evans {
214ca46e90eSBruce Evans #ifdef MAC
215610ecfe0SMaxim Sobolev 	int error;
216610ecfe0SMaxim Sobolev 	struct image_args args;
217610ecfe0SMaxim Sobolev 
218610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
219610ecfe0SMaxim Sobolev 	    uap->argv, uap->envv);
220610ecfe0SMaxim Sobolev 	if (error == 0)
221610ecfe0SMaxim Sobolev 		error = kern_execve(td, &args, uap->mac_p);
222610ecfe0SMaxim Sobolev 	return (error);
223ca46e90eSBruce Evans #else
224ca46e90eSBruce Evans 	return (ENOSYS);
225ca46e90eSBruce Evans #endif
226ca46e90eSBruce Evans }
227ca46e90eSBruce Evans 
22833812c06SColin Percival /*
22933812c06SColin Percival  * XXX: kern_execve has the astonishing property of not always
23033812c06SColin Percival  * returning to the caller.  If sufficiently bad things happen during
23133812c06SColin Percival  * the call to do_execve(), it can end up calling exit1(); as a result,
23233812c06SColin Percival  * callers must avoid doing anything which they might need to undo
23333812c06SColin Percival  * (e.g., allocating memory).
23433812c06SColin Percival  */
23584e0b075SDavid Xu int
236610ecfe0SMaxim Sobolev kern_execve(td, args, mac_p)
237906ac69dSDavid Xu 	struct thread *td;
238610ecfe0SMaxim Sobolev 	struct image_args *args;
239906ac69dSDavid Xu 	struct mac *mac_p;
240906ac69dSDavid Xu {
241906ac69dSDavid Xu 	struct proc *p = td->td_proc;
242906ac69dSDavid Xu 	int error;
243906ac69dSDavid Xu 
244ae1078d6SWayne Salamon 	AUDIT_ARG(argv, args->begin_argv, args->argc,
245ae1078d6SWayne Salamon 	    args->begin_envv - args->begin_argv);
246ae1078d6SWayne Salamon 	AUDIT_ARG(envv, args->begin_envv, args->envc,
247ae1078d6SWayne Salamon 	    args->endp - args->begin_envv);
248906ac69dSDavid Xu 	if (p->p_flag & P_HADTHREADS) {
249906ac69dSDavid Xu 		PROC_LOCK(p);
250906ac69dSDavid Xu 		if (thread_single(SINGLE_BOUNDARY)) {
251906ac69dSDavid Xu 			PROC_UNLOCK(p);
25268ff3c24SStephan Uphoff 	       		exec_free_args(args);
253906ac69dSDavid Xu 			return (ERESTART);	/* Try again later. */
254906ac69dSDavid Xu 		}
255906ac69dSDavid Xu 		PROC_UNLOCK(p);
256906ac69dSDavid Xu 	}
257906ac69dSDavid Xu 
258610ecfe0SMaxim Sobolev 	error = do_execve(td, args, mac_p);
259906ac69dSDavid Xu 
260906ac69dSDavid Xu 	if (p->p_flag & P_HADTHREADS) {
261906ac69dSDavid Xu 		PROC_LOCK(p);
262906ac69dSDavid Xu 		/*
263906ac69dSDavid Xu 		 * If success, we upgrade to SINGLE_EXIT state to
264906ac69dSDavid Xu 		 * force other threads to suicide.
265906ac69dSDavid Xu 		 */
266906ac69dSDavid Xu 		if (error == 0)
267906ac69dSDavid Xu 			thread_single(SINGLE_EXIT);
268906ac69dSDavid Xu 		else
269906ac69dSDavid Xu 			thread_single_end();
270906ac69dSDavid Xu 		PROC_UNLOCK(p);
271906ac69dSDavid Xu 	}
272906ac69dSDavid Xu 
273906ac69dSDavid Xu 	return (error);
274906ac69dSDavid Xu }
275906ac69dSDavid Xu 
27626f9a767SRodney W. Grimes /*
277450ffb44SRobert Watson  * In-kernel implementation of execve().  All arguments are assumed to be
278450ffb44SRobert Watson  * userspace pointers from the passed thread.
279116734c4SMatthew Dillon  *
280116734c4SMatthew Dillon  * MPSAFE
28126f9a767SRodney W. Grimes  */
282450ffb44SRobert Watson static int
283610ecfe0SMaxim Sobolev do_execve(td, args, mac_p)
284b40ce416SJulian Elischer 	struct thread *td;
285610ecfe0SMaxim Sobolev 	struct image_args *args;
286670cb89bSRobert Watson 	struct mac *mac_p;
287df8bae1dSRodney W. Grimes {
288b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
28926f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
2909b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
2911419eacbSAlfred Perlstein 	struct uidinfo *euip;
292654f6be1SBruce Evans 	register_t *stack_base;
293bb56ec4aSPoul-Henning Kamp 	int error, len, i;
294c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
2959f5c1d19SDiomidis Spinellis 	struct vattr attr;
2964d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
29769be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
29890af4afaSJohn Baldwin 	struct sigacts *oldsigacts, *newsigacts;
2996c84de02SJohn Baldwin #ifdef KTRACE
3006c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
301a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
3026c84de02SJohn Baldwin #endif
3036c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
304d06c0d4dSRobert Watson 	int credential_changing;
305269576c8SJeff Roberson 	int vfslocked;
306619eb6e5SJeff Roberson 	int textset;
307ccafe7ebSRobert Watson #ifdef MAC
308eca8a663SRobert Watson 	struct label *interplabel = NULL;
309eca8a663SRobert Watson 	int will_transition;
310ccafe7ebSRobert Watson #endif
31115139246SJoseph Koshy #ifdef HWPMC_HOOKS
31215139246SJoseph Koshy 	struct pmckern_procexec pe;
31315139246SJoseph Koshy #endif
31426f9a767SRodney W. Grimes 
315532c491bSJeff Roberson 	vfslocked = 0;
316c52007c2SDavid Greenman 	imgp = &image_params;
317df8bae1dSRodney W. Grimes 
3189ca45e81SDag-Erling Smørgrav 	/*
3199ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
3209ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
3219ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
3229ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
3239ca45e81SDag-Erling Smørgrav 	 * privileges.
3249ca45e81SDag-Erling Smørgrav 	 */
3259ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
3269ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
32791f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
3289ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
3299ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
3309ca45e81SDag-Erling Smørgrav 
331df8bae1dSRodney W. Grimes 	/*
332c52007c2SDavid Greenman 	 * Initialize part of the common data
333df8bae1dSRodney W. Grimes 	 */
334c52007c2SDavid Greenman 	imgp->proc = p;
335670cb89bSRobert Watson 	imgp->execlabel = NULL;
336c52007c2SDavid Greenman 	imgp->attr = &attr;
337c52007c2SDavid Greenman 	imgp->entry_addr = 0;
338c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
339c52007c2SDavid Greenman 	imgp->interpreted = 0;
34090dc539bSMaxim Sobolev 	imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
341e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
3421616db3cSJohn Dyson 	imgp->vp = NULL;
3430b2ed1aeSJeff Roberson 	imgp->object = NULL;
3441616db3cSJohn Dyson 	imgp->firstpage = NULL;
3454fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
346b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
347610ecfe0SMaxim Sobolev 	imgp->args = args;
34826f9a767SRodney W. Grimes 
349670cb89bSRobert Watson #ifdef MAC
350eca8a663SRobert Watson 	error = mac_execve_enter(imgp, mac_p);
351532c491bSJeff Roberson 	if (error)
352670cb89bSRobert Watson 		goto exec_fail;
353670cb89bSRobert Watson #endif
354670cb89bSRobert Watson 
35559c8bc40SAlan Cox 	imgp->image_header = NULL;
35626f9a767SRodney W. Grimes 
35726f9a767SRodney W. Grimes 	/*
35826f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
35926f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
360ae1078d6SWayne Salamon 	 *
361ae1078d6SWayne Salamon 	 * XXXAUDIT: It would be desirable to also audit the name of the
362ae1078d6SWayne Salamon 	 * interpreter if this is an interpreted binary.
36326f9a767SRodney W. Grimes 	 */
36426f9a767SRodney W. Grimes 	ndp = &nd;
3654bb260adSRobert Watson 	NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE |
3664bb260adSRobert Watson 	    AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
36726f9a767SRodney W. Grimes 
36826f9a767SRodney W. Grimes interpret:
36926f9a767SRodney W. Grimes 	error = namei(ndp);
370610ecfe0SMaxim Sobolev 	if (error)
37126f9a767SRodney W. Grimes 		goto exec_fail;
37226f9a767SRodney W. Grimes 
373269576c8SJeff Roberson 	vfslocked = NDHASGIANT(ndp);
374c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
37526f9a767SRodney W. Grimes 
37626f9a767SRodney W. Grimes 	/*
3779022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
3789022e4adSDavid Greenman 	 */
379c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
380619eb6e5SJeff Roberson 	if (error)
38126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
382619eb6e5SJeff Roberson 
3838516dd18SPoul-Henning Kamp 	imgp->object = imgp->vp->v_object;
3848516dd18SPoul-Henning Kamp 	if (imgp->object != NULL)
3850b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
38626f9a767SRodney W. Grimes 
387619eb6e5SJeff Roberson 	/*
388619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
389619eb6e5SJeff Roberson 	 * activating it.
390619eb6e5SJeff Roberson 	 *
391619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
392619eb6e5SJeff Roberson 	 * actually an executable image.
393619eb6e5SJeff Roberson 	 */
394619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
395619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
396619eb6e5SJeff Roberson 
3971616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
3986d5a0a8cSDavid Greenman 	if (error)
39926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
40026f9a767SRodney W. Grimes 
40126f9a767SRodney W. Grimes 	/*
402d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
403d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
404d323ddf3SMatthew Dillon 	 *	scripts differently.
40526f9a767SRodney W. Grimes 	 */
406d323ddf3SMatthew Dillon 	error = -1;
407d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
408d323ddf3SMatthew Dillon 		error = img_first(imgp);
409d323ddf3SMatthew Dillon 
410d323ddf3SMatthew Dillon 	/*
411d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
412d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
413d323ddf3SMatthew Dillon 	 *	and an error otherwise.
414d323ddf3SMatthew Dillon 	 */
415d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
416d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
417d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
418d323ddf3SMatthew Dillon 			continue;
419d323ddf3SMatthew Dillon 		}
420c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
421d323ddf3SMatthew Dillon 	}
422d323ddf3SMatthew Dillon 
423d323ddf3SMatthew Dillon 	if (error) {
424619eb6e5SJeff Roberson 		if (error == -1) {
425619eb6e5SJeff Roberson 			if (textset == 0)
426619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
427d323ddf3SMatthew Dillon 			error = ENOEXEC;
428619eb6e5SJeff Roberson 		}
42926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
430d323ddf3SMatthew Dillon 	}
431d323ddf3SMatthew Dillon 
432d323ddf3SMatthew Dillon 	/*
433d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
434d323ddf3SMatthew Dillon 	 * activate the interpreter.
435d323ddf3SMatthew Dillon 	 */
436c52007c2SDavid Greenman 	if (imgp->interpreted) {
4371616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
438619eb6e5SJeff Roberson 		/*
439619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
440619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
441619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
442619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
443619eb6e5SJeff Roberson 		 */
444619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
445762e6b85SEivind Eklund 		/* free name buffer and old vnode */
446762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
447670cb89bSRobert Watson #ifdef MAC
448eca8a663SRobert Watson 		interplabel = mac_vnode_label_alloc();
449eca8a663SRobert Watson 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
450670cb89bSRobert Watson #endif
451619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
4520b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
4530b2ed1aeSJeff Roberson 		imgp->object = NULL;
454269576c8SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
455532c491bSJeff Roberson 		vfslocked = 0;
45626f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
457269576c8SJeff Roberson 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
458b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
45926f9a767SRodney W. Grimes 		goto interpret;
46026f9a767SRodney W. Grimes 	}
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes 	/*
46326f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
46426f9a767SRodney W. Grimes 	 */
4653ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
4663ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
4673ebc1248SPeter Wemm 	else
468c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
46926f9a767SRodney W. Grimes 
47026f9a767SRodney W. Grimes 	/*
4711e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
4721e1e0b44SSøren Schmidt 	 * let it do the stack setup.
4731e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
47426f9a767SRodney W. Grimes 	 */
475d6c847f3SBruce Evans 	if (p->p_sysent->sv_fixup != NULL)
476c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
4771e1e0b44SSøren Schmidt 	else
478610ecfe0SMaxim Sobolev 		suword(--stack_base, imgp->args->argc);
47926f9a767SRodney W. Grimes 
480a78e8d2aSDavid Greenman 	/*
481a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
482a78e8d2aSDavid Greenman 	 * be shared after an exec.
483a78e8d2aSDavid Greenman 	 */
484c113083cSPoul-Henning Kamp 	fdunshare(p, td);
485a78e8d2aSDavid Greenman 
486333ea485SGuido van Rooij 	/*
4879b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
4889b3b1c5fSJohn Baldwin 	 */
4899b3b1c5fSJohn Baldwin 	newcred = crget();
4901419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
491610ecfe0SMaxim Sobolev 	i = imgp->args->begin_envv - imgp->args->begin_argv;
4925997cae9SDon Lewis 	/* Cache arguments if they fit inside our allowance */
4935997cae9SDon Lewis 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
4949b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
4955997cae9SDon Lewis 		bcopy(imgp->args->begin_argv, newargs->ar_args, i);
4965997cae9SDon Lewis 	}
4979b3b1c5fSJohn Baldwin 
4989b3b1c5fSJohn Baldwin 	/* close files on exec */
499d302786cSTor Egge 	VOP_UNLOCK(imgp->vp, 0, td);
5009b3b1c5fSJohn Baldwin 	fdcloseexec(td);
501d302786cSTor Egge 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
5029b3b1c5fSJohn Baldwin 
503619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
504619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
505619eb6e5SJeff Roberson 
5069b3b1c5fSJohn Baldwin 	/*
507333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
5088688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
509b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
510333ea485SGuido van Rooij 	 * reset.
511333ea485SGuido van Rooij 	 */
5129b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
51390af4afaSJohn Baldwin 	if (sigacts_shared(p->p_sigacts)) {
51490af4afaSJohn Baldwin 		oldsigacts = p->p_sigacts;
5159b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
51690af4afaSJohn Baldwin 		newsigacts = sigacts_alloc();
51790af4afaSJohn Baldwin 		sigacts_copy(newsigacts, oldsigacts);
5189b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
51990af4afaSJohn Baldwin 		p->p_sigacts = newsigacts;
52090af4afaSJohn Baldwin 	} else
52190af4afaSJohn Baldwin 		oldsigacts = NULL;
522333ea485SGuido van Rooij 
523fdf4e8b3SWarner Losh 	/* Stop profiling */
524fdf4e8b3SWarner Losh 	stopprofclock(p);
525fdf4e8b3SWarner Losh 
52626f9a767SRodney W. Grimes 	/* reset caught signals */
52726f9a767SRodney W. Grimes 	execsigs(p);
52826f9a767SRodney W. Grimes 
52926f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
53026f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
53126f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
53226f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
53326f9a767SRodney W. Grimes 
53426f9a767SRodney W. Grimes 	/*
53524b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
536dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
53726f9a767SRodney W. Grimes 	 */
53826f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
53926f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
54026f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
5417f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
54226f9a767SRodney W. Grimes 	}
54326f9a767SRodney W. Grimes 
54426f9a767SRodney W. Grimes 	/*
545a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
546a78e8d2aSDavid Greenman 	 *
547a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
548a78e8d2aSDavid Greenman 	 * the process is being traced.
549670cb89bSRobert Watson 	 *
550670cb89bSRobert Watson 	 * XXXMAC: For the time being, use NOSUID to also prohibit
551670cb89bSRobert Watson 	 * transitions on the file system.
552c52007c2SDavid Greenman 	 */
553b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
554d06c0d4dSRobert Watson 	credential_changing = 0;
555d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
556d06c0d4dSRobert Watson 	    attr.va_uid;
557d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
558d06c0d4dSRobert Watson 	    attr.va_gid;
559ccafe7ebSRobert Watson #ifdef MAC
560670cb89bSRobert Watson 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
561eca8a663SRobert Watson 	    interplabel, imgp);
562ccafe7ebSRobert Watson 	credential_changing |= will_transition;
563ccafe7ebSRobert Watson #endif
564d06c0d4dSRobert Watson 
565d06c0d4dSRobert Watson 	if (credential_changing &&
566a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
567c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
568c52007c2SDavid Greenman 		/*
569c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
570b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
571b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
57226f9a767SRodney W. Grimes 		 */
573b85db196SPeter Wemm 		setsugid(p);
5746c84de02SJohn Baldwin #ifdef KTRACE
57556f21b9dSColin Percival 		if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) {
5766c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
57779deba82SMatthew Dillon 			p->p_traceflag = 0;
578a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
579a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
580a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
581a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
5826c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
58326f9a767SRodney W. Grimes 		}
5846c84de02SJohn Baldwin #endif
58528b325aaSDon Lewis 		/*
586c1e2d386SNate Lawson 		 * Close any file descriptors 0..2 that reference procfs,
587c1e2d386SNate Lawson 		 * then make sure file descriptors 0..2 are in use.
58828b325aaSDon Lewis 		 *
589c1e2d386SNate Lawson 		 * setugidsafety() may call closef() and then pfind()
590c1e2d386SNate Lawson 		 * which may grab the process lock.
59128b325aaSDon Lewis 		 * fdcheckstd() may call falloc() which may block to
59228b325aaSDon Lewis 		 * allocate memory, so temporarily drop the process lock.
59328b325aaSDon Lewis 		 */
59428b325aaSDon Lewis 		PROC_UNLOCK(p);
595c1e2d386SNate Lawson 		setugidsafety(td);
596d302786cSTor Egge 		VOP_UNLOCK(imgp->vp, 0, td);
597e983a376SJacques Vidrine 		error = fdcheckstd(td);
598d302786cSTor Egge 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
59963c9e754SJohn Baldwin 		if (error != 0)
60069be5db9SAlfred Perlstein 			goto done1;
601e1b1aa3bSJohn Baldwin 		PROC_LOCK(p);
602c52007c2SDavid Greenman 		/*
603c52007c2SDavid Greenman 		 * Set the new credentials.
604c52007c2SDavid Greenman 		 */
6059b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
606c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
6071419eacbSAlfred Perlstein 			change_euid(newcred, euip);
608c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
609b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
610ccafe7ebSRobert Watson #ifdef MAC
611670cb89bSRobert Watson 		if (will_transition) {
612670cb89bSRobert Watson 			mac_execve_transition(oldcred, newcred, imgp->vp,
613eca8a663SRobert Watson 			    interplabel, imgp);
614670cb89bSRobert Watson 		}
615ccafe7ebSRobert Watson #endif
6169b3b1c5fSJohn Baldwin 		/*
6179b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
618ccafe7ebSRobert Watson 		 *
619ccafe7ebSRobert Watson 		 * XXXMAC: Note that the current logic will save the
620ccafe7ebSRobert Watson 		 * uid and gid if a MAC domain transition occurs, even
621ccafe7ebSRobert Watson 		 * though maybe it shouldn't.
6229b3b1c5fSJohn Baldwin 		 */
6239b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
6249b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
6259b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
6269b3b1c5fSJohn Baldwin 		newcred = NULL;
627c52007c2SDavid Greenman 	} else {
628b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
629b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
630c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
63126f9a767SRodney W. Grimes 		/*
632c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
633b1fc0ec1SRobert Watson 		 *
634b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
6359b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
6369b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
6379b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
6389b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
6399b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
6409b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
6419b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
64226f9a767SRodney W. Grimes 		 */
643b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
644b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
6459b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
646b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
647b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
648b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
6499b3b1c5fSJohn Baldwin 			newcred = NULL;
6509b3b1c5fSJohn Baldwin 		}
651b1fc0ec1SRobert Watson 	}
65226f9a767SRodney W. Grimes 
65326f9a767SRodney W. Grimes 	/*
654619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
655619eb6e5SJeff Roberson 	 * to locking the proc lock.
6562a531c80SDavid Greenman 	 */
6579b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
6582a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
6592a531c80SDavid Greenman 
6602a531c80SDavid Greenman 	/*
6619ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
6629ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
663cb679c38SJonathan Lemon 	 */
664ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
6659ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
666cb679c38SJonathan Lemon 
667cb679c38SJonathan Lemon 	/*
66826f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
66926f9a767SRodney W. Grimes 	 * can be set before the program executes.
670906ac69dSDavid Xu 	 * Use tdsignal to deliver signal to current thread, use
671906ac69dSDavid Xu 	 * psignal may cause the signal to be delivered to wrong thread
672906ac69dSDavid Xu 	 * because that thread will exit, remember we are going to enter
673906ac69dSDavid Xu 	 * single thread mode.
67426f9a767SRodney W. Grimes 	 */
67526f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
6766d7b314bSDavid Xu 		tdsignal(p, td, SIGTRAP, NULL);
67726f9a767SRodney W. Grimes 
67826f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
67926f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
68026f9a767SRodney W. Grimes 
6815997cae9SDon Lewis 	/*
68234ea500bSDon Lewis 	 * Free any previous argument cache and replace it with
6835997cae9SDon Lewis 	 * the new argument cache, if any.
6845997cae9SDon Lewis 	 */
6859b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
686e1b1aa3bSJohn Baldwin 	p->p_args = newargs;
687e1b1aa3bSJohn Baldwin 	newargs = NULL;
688ebccf1e3SJoseph Koshy 
689ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
690ebccf1e3SJoseph Koshy 	/*
691f263522aSJoseph Koshy 	 * Check if system-wide sampling is in effect or if the
692f263522aSJoseph Koshy 	 * current process is using PMCs.  If so, do exec() time
693ebccf1e3SJoseph Koshy 	 * processing.  This processing needs to happen AFTER the
694ebccf1e3SJoseph Koshy 	 * P_INEXEC flag is cleared.
695ebccf1e3SJoseph Koshy 	 *
696ebccf1e3SJoseph Koshy 	 * The proc lock needs to be released before taking the PMC
697ebccf1e3SJoseph Koshy 	 * SX.
698ebccf1e3SJoseph Koshy 	 */
699f263522aSJoseph Koshy 	if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
700e1b1aa3bSJohn Baldwin 		PROC_UNLOCK(p);
70115139246SJoseph Koshy 		pe.pm_credentialschanged = credential_changing;
70215139246SJoseph Koshy 		pe.pm_entryaddr = imgp->entry_addr;
70315139246SJoseph Koshy 
70415139246SJoseph Koshy 		PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
705ebccf1e3SJoseph Koshy 	} else
706ebccf1e3SJoseph Koshy 		PROC_UNLOCK(p);
707ebccf1e3SJoseph Koshy #else  /* !HWPMC_HOOKS */
708ebccf1e3SJoseph Koshy 	PROC_UNLOCK(p);
709ebccf1e3SJoseph Koshy #endif
710e1b1aa3bSJohn Baldwin 
711e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
7123ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
7133ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
7143ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
7153ebc1248SPeter Wemm 	else
716bafbd492SJake Burkholder 		exec_setregs(td, imgp->entry_addr,
717bafbd492SJake Burkholder 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
718e913ca22SDoug Rabson 
7199f5c1d19SDiomidis Spinellis 	vfs_mark_atime(imgp->vp, td);
7206341095eSKen Smith 
72169be5db9SAlfred Perlstein done1:
7229b3b1c5fSJohn Baldwin 	/*
7239b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
7249b3b1c5fSJohn Baldwin 	 */
7251419eacbSAlfred Perlstein 	uifree(euip);
7269b3b1c5fSJohn Baldwin 	if (newcred == NULL)
7279b3b1c5fSJohn Baldwin 		crfree(oldcred);
7289b3b1c5fSJohn Baldwin 	else
7299b3b1c5fSJohn Baldwin 		crfree(newcred);
730d302786cSTor Egge 	VOP_UNLOCK(imgp->vp, 0, td);
7319b3b1c5fSJohn Baldwin 	/*
7329b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
7339b3b1c5fSJohn Baldwin 	 */
73468ce4375SJeff Roberson 	if (textvp != NULL) {
73568ce4375SJeff Roberson 		int tvfslocked;
73668ce4375SJeff Roberson 
73768ce4375SJeff Roberson 		tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
7389b3b1c5fSJohn Baldwin 		vrele(textvp);
73968ce4375SJeff Roberson 		VFS_UNLOCK_GIANT(tvfslocked);
74068ce4375SJeff Roberson 	}
741619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
742619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
7436c84de02SJohn Baldwin #ifdef KTRACE
7449b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
7459b3b1c5fSJohn Baldwin 		vrele(tracevp);
746a5881ea5SJohn Baldwin 	if (tracecred != NULL)
747a5881ea5SJohn Baldwin 		crfree(tracecred);
7486c84de02SJohn Baldwin #endif
749d302786cSTor Egge 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
75069be5db9SAlfred Perlstein 	if (oldargs != NULL)
7519b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
75269be5db9SAlfred Perlstein 	if (newargs != NULL)
75369be5db9SAlfred Perlstein 		pargs_drop(newargs);
75490af4afaSJohn Baldwin 	if (oldsigacts != NULL)
75590af4afaSJohn Baldwin 		sigacts_free(oldsigacts);
756b9df5231SPoul-Henning Kamp 
7571616db3cSJohn Dyson exec_fail_dealloc:
7581616db3cSJohn Dyson 
75926f9a767SRodney W. Grimes 	/*
76026f9a767SRodney W. Grimes 	 * free various allocated resources
76126f9a767SRodney W. Grimes 	 */
762d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
7631616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
76426f9a767SRodney W. Grimes 
765d6c847f3SBruce Evans 	if (imgp->vp != NULL) {
766762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
767619eb6e5SJeff Roberson 		vput(imgp->vp);
768a3cf6ebaSDavid Greenman 	}
76926f9a767SRodney W. Grimes 
770d6c847f3SBruce Evans 	if (imgp->object != NULL)
7710b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
7720b2ed1aeSJeff Roberson 
773d1989db5SRobert Drehmel 	if (error == 0) {
774d1989db5SRobert Drehmel 		/*
775d1989db5SRobert Drehmel 		 * Stop the process here if its stop event mask has
776d1989db5SRobert Drehmel 		 * the S_EXEC bit set.
777d1989db5SRobert Drehmel 		 */
778d1989db5SRobert Drehmel 		STOPEVENT(p, S_EXEC, 0);
779116734c4SMatthew Dillon 		goto done2;
780d1989db5SRobert Drehmel 	}
7811616db3cSJohn Dyson 
78226f9a767SRodney W. Grimes exec_fail:
7839ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
7849ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
7859ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
7869ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
7879ca45e81SDag-Erling Smørgrav 
788116734c4SMatthew Dillon done2:
789670cb89bSRobert Watson #ifdef MAC
790670cb89bSRobert Watson 	mac_execve_exit(imgp);
791eca8a663SRobert Watson 	if (interplabel != NULL)
792eca8a663SRobert Watson 		mac_vnode_label_free(interplabel);
793670cb89bSRobert Watson #endif
794269576c8SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
7958917b8d2SJohn Baldwin 	exec_free_args(args);
7968917b8d2SJohn Baldwin 
7978917b8d2SJohn Baldwin 	if (error && imgp->vmspace_destroyed) {
7988917b8d2SJohn Baldwin 		/* sorry, no more process anymore. exit gracefully */
7998917b8d2SJohn Baldwin 		exit1(td, W_EXITCODE(0, SIGABRT));
8008917b8d2SJohn Baldwin 		/* NOT REACHED */
8018917b8d2SJohn Baldwin 	}
802116734c4SMatthew Dillon 	return (error);
80326f9a767SRodney W. Grimes }
80426f9a767SRodney W. Grimes 
8051616db3cSJohn Dyson int
8061616db3cSJohn Dyson exec_map_first_page(imgp)
8071616db3cSJohn Dyson 	struct image_params *imgp;
8081616db3cSJohn Dyson {
809d8aad40cSJohn Baldwin 	int rv, i;
81095461b45SJohn Dyson 	int initial_pagein;
81195461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
8121616db3cSJohn Dyson 	vm_object_t object;
8131616db3cSJohn Dyson 
814d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
8151616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
8161616db3cSJohn Dyson 
8178516dd18SPoul-Henning Kamp 	object = imgp->vp->v_object;
8184d7d2993SJeff Roberson 	if (object == NULL)
8194d7d2993SJeff Roberson 		return (EACCES);
820fd0cc9a8SAlan Cox 	VM_OBJECT_LOCK(object);
82195461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
82295461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
82395461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
82495461b45SJohn Dyson 		if (initial_pagein > object->size)
82595461b45SJohn Dyson 			initial_pagein = object->size;
82695461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
827d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
8286ec2fca5SAlan Cox 				if (ma[i]->valid)
8296ec2fca5SAlan Cox 					break;
8308ad398d0SAlan Cox 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
83106fa71cdSAlan Cox 					break;
8328ad398d0SAlan Cox 				vm_page_lock_queues();
833e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
834ee113343SAlan Cox 				vm_page_unlock_queues();
83595461b45SJohn Dyson 			} else {
836ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
837ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
83895461b45SJohn Dyson 				if (ma[i] == NULL)
83995461b45SJohn Dyson 					break;
84095461b45SJohn Dyson 			}
84195461b45SJohn Dyson 		}
84295461b45SJohn Dyson 		initial_pagein = i;
84395461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
84495461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
845ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
846ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
847ecbb00a2SDoug Rabson 			if (ma[0]) {
8486ec2fca5SAlan Cox 				vm_page_lock_queues();
8498f9110f6SJohn Dyson 				vm_page_free(ma[0]);
85006fa71cdSAlan Cox 				vm_page_unlock_queues();
8516ec2fca5SAlan Cox 			}
85206fa71cdSAlan Cox 			VM_OBJECT_UNLOCK(object);
853a7cddfedSJake Burkholder 			return (EIO);
8541616db3cSJohn Dyson 		}
8551616db3cSJohn Dyson 	}
8566ec2fca5SAlan Cox 	vm_page_lock_queues();
857148b3f62SAlan Cox 	vm_page_hold(ma[0]);
858e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
85997646f56SAlan Cox 	vm_page_unlock_queues();
8606ec2fca5SAlan Cox 	VM_OBJECT_UNLOCK(object);
8611616db3cSJohn Dyson 
86259c8bc40SAlan Cox 	imgp->firstpage = sf_buf_alloc(ma[0], 0);
86359c8bc40SAlan Cox 	imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
8641616db3cSJohn Dyson 
865a7cddfedSJake Burkholder 	return (0);
8661616db3cSJohn Dyson }
8671616db3cSJohn Dyson 
8681616db3cSJohn Dyson void
8691616db3cSJohn Dyson exec_unmap_first_page(imgp)
8701616db3cSJohn Dyson 	struct image_params *imgp;
8711616db3cSJohn Dyson {
87259c8bc40SAlan Cox 	vm_page_t m;
87323955314SAlfred Perlstein 
874d6c847f3SBruce Evans 	if (imgp->firstpage != NULL) {
87559c8bc40SAlan Cox 		m = sf_buf_page(imgp->firstpage);
87659c8bc40SAlan Cox 		sf_buf_free(imgp->firstpage);
8771616db3cSJohn Dyson 		imgp->firstpage = NULL;
87859c8bc40SAlan Cox 		vm_page_lock_queues();
87959c8bc40SAlan Cox 		vm_page_unhold(m);
88059c8bc40SAlan Cox 		vm_page_unlock_queues();
8811616db3cSJohn Dyson 	}
8821616db3cSJohn Dyson }
8831616db3cSJohn Dyson 
88426f9a767SRodney W. Grimes /*
88526f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
88626f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
88726f9a767SRodney W. Grimes  *	automatically in trap.c.
88826f9a767SRodney W. Grimes  */
88926f9a767SRodney W. Grimes int
89005ba50f5SJake Burkholder exec_new_vmspace(imgp, sv)
891c52007c2SDavid Greenman 	struct image_params *imgp;
89205ba50f5SJake Burkholder 	struct sysentvec *sv;
89326f9a767SRodney W. Grimes {
89426f9a767SRodney W. Grimes 	int error;
8955e20c11fSAlan Cox 	struct proc *p = imgp->proc;
8965e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
89705ba50f5SJake Burkholder 	vm_offset_t stack_addr;
89805ba50f5SJake Burkholder 	vm_map_t map;
89926f9a767SRodney W. Grimes 
900c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
901993182e5SAlexander Leidinger 	imgp->sysent = sv;
90226f9a767SRodney W. Grimes 
903a5bdcb2aSPeter Wemm 	/* Called with Giant held, do not depend on it! */
904993182e5SAlexander Leidinger 	EVENTHANDLER_INVOKE(process_exec, p, imgp);
9056f5dafeaSAlan Cox 
9066f5dafeaSAlan Cox 	/*
907c460ac3aSPeter Wemm 	 * Here is as good a place as any to do any resource limit cleanups.
908c460ac3aSPeter Wemm 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
909c460ac3aSPeter Wemm 	 * data size limit may need to be changed to a value that makes
910c460ac3aSPeter Wemm 	 * sense for the 32 bit binary.
911c460ac3aSPeter Wemm 	 */
912d6c847f3SBruce Evans 	if (sv->sv_fixlimits != NULL)
9131471f287SPaul Saab 		sv->sv_fixlimits(p);
914c460ac3aSPeter Wemm 
915c460ac3aSPeter Wemm 	/*
916af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
917af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
918af9ec885SJohn Dyson 	 * not disrupted
919af9ec885SJohn Dyson 	 */
92005ba50f5SJake Burkholder 	map = &vmspace->vm_map;
92105ba50f5SJake Burkholder 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
92205ba50f5SJake Burkholder 	    vm_map_max(map) == sv->sv_maxuser) {
9233db161e0SMatthew Dillon 		shmexit(vmspace);
924b9eee07eSPeter Wemm 		pmap_remove_pages(vmspace_pmap(vmspace));
92505ba50f5SJake Burkholder 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
926af9ec885SJohn Dyson 	} else {
92705ba50f5SJake Burkholder 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
9285e20c11fSAlan Cox 		vmspace = p->p_vmspace;
92905ba50f5SJake Burkholder 		map = &vmspace->vm_map;
930af9ec885SJohn Dyson 	}
93126f9a767SRodney W. Grimes 
93226f9a767SRodney W. Grimes 	/* Allocate a new stack */
933fd75d710SMarcel Moolenaar 	stack_addr = sv->sv_usrstack - maxssiz;
93405ba50f5SJake Burkholder 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
935fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
9362267af78SJulian Elischer 	if (error)
9372267af78SJulian Elischer 		return (error);
9382267af78SJulian Elischer 
93963c47a5cSDoug Rabson #ifdef __ia64__
940fd75d710SMarcel Moolenaar 	/* Allocate a new register stack */
941bab1f052SMarcel Moolenaar 	stack_addr = IA64_BACKINGSTORE;
942fd75d710SMarcel Moolenaar 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
943fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
944fd75d710SMarcel Moolenaar 	if (error)
945fd75d710SMarcel Moolenaar 		return (error);
94663c47a5cSDoug Rabson #endif
94763c47a5cSDoug Rabson 
9482267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
9492267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
9502267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
9512267af78SJulian Elischer 	 */
952cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
95305ba50f5SJake Burkholder 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
95426f9a767SRodney W. Grimes 
95526f9a767SRodney W. Grimes 	return (0);
95626f9a767SRodney W. Grimes }
95726f9a767SRodney W. Grimes 
95826f9a767SRodney W. Grimes /*
95926f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
96026f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
96126f9a767SRodney W. Grimes  */
96226f9a767SRodney W. Grimes int
963610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname,
964610ecfe0SMaxim Sobolev     enum uio_seg segflg, char **argv, char **envv)
96526f9a767SRodney W. Grimes {
96626f9a767SRodney W. Grimes 	char *argp, *envp;
967ecbb00a2SDoug Rabson 	int error;
968ecbb00a2SDoug Rabson 	size_t length;
96926f9a767SRodney W. Grimes 
970610ecfe0SMaxim Sobolev 	error = 0;
971610ecfe0SMaxim Sobolev 
972610ecfe0SMaxim Sobolev 	bzero(args, sizeof(*args));
973610ecfe0SMaxim Sobolev 	if (argv == NULL)
974610ecfe0SMaxim Sobolev 		return (EFAULT);
97526f9a767SRodney W. Grimes 	/*
976610ecfe0SMaxim Sobolev 	 * Allocate temporary demand zeroed space for argument and
97790dc539bSMaxim Sobolev 	 *	environment strings:
97890dc539bSMaxim Sobolev 	 *
97990dc539bSMaxim Sobolev 	 * o ARG_MAX for argument and environment;
98090dc539bSMaxim Sobolev 	 * o MAXSHELLCMDLEN for the name of interpreters.
98126f9a767SRodney W. Grimes 	 */
98290dc539bSMaxim Sobolev 	args->buf = (char *) kmem_alloc_wait(exec_map,
98390dc539bSMaxim Sobolev 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
984610ecfe0SMaxim Sobolev 	if (args->buf == NULL)
985610ecfe0SMaxim Sobolev 		return (ENOMEM);
986610ecfe0SMaxim Sobolev 	args->begin_argv = args->buf;
987610ecfe0SMaxim Sobolev 	args->endp = args->begin_argv;
988610ecfe0SMaxim Sobolev 	args->stringspace = ARG_MAX;
98926f9a767SRodney W. Grimes 
990610ecfe0SMaxim Sobolev 	args->fname = args->buf + ARG_MAX;
99126f9a767SRodney W. Grimes 
992610ecfe0SMaxim Sobolev 	/*
993610ecfe0SMaxim Sobolev 	 * Copy the file name.
994610ecfe0SMaxim Sobolev 	 */
995610ecfe0SMaxim Sobolev 	error = (segflg == UIO_SYSSPACE) ?
996610ecfe0SMaxim Sobolev 	    copystr(fname, args->fname, PATH_MAX, &length) :
997610ecfe0SMaxim Sobolev 	    copyinstr(fname, args->fname, PATH_MAX, &length);
998c30af532SMaxim Sobolev 	if (error != 0)
99968ff3c24SStephan Uphoff 		goto err_exit;
100026f9a767SRodney W. Grimes 
1001610ecfe0SMaxim Sobolev 	/*
1002610ecfe0SMaxim Sobolev 	 * extract arguments first
1003610ecfe0SMaxim Sobolev 	 */
1004610ecfe0SMaxim Sobolev 	while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
100568ff3c24SStephan Uphoff 		if (argp == (caddr_t) -1) {
100668ff3c24SStephan Uphoff 			error = EFAULT;
100768ff3c24SStephan Uphoff 			goto err_exit;
100868ff3c24SStephan Uphoff 		}
1009610ecfe0SMaxim Sobolev 		if ((error = copyinstr(argp, args->endp,
1010610ecfe0SMaxim Sobolev 		    args->stringspace, &length))) {
1011610ecfe0SMaxim Sobolev 			if (error == ENAMETOOLONG)
101268ff3c24SStephan Uphoff 				error = E2BIG;
101368ff3c24SStephan Uphoff 			goto err_exit;
1014610ecfe0SMaxim Sobolev 		}
1015610ecfe0SMaxim Sobolev 		args->stringspace -= length;
1016610ecfe0SMaxim Sobolev 		args->endp += length;
1017610ecfe0SMaxim Sobolev 		args->argc++;
1018610ecfe0SMaxim Sobolev 	}
1019610ecfe0SMaxim Sobolev 
1020610ecfe0SMaxim Sobolev 	args->begin_envv = args->endp;
1021b9df5231SPoul-Henning Kamp 
102226f9a767SRodney W. Grimes 	/*
102326f9a767SRodney W. Grimes 	 * extract environment strings
102426f9a767SRodney W. Grimes 	 */
10250fd1a014SDavid Greenman 	if (envv) {
1026aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
102768ff3c24SStephan Uphoff 			if (envp == (caddr_t)-1) {
102868ff3c24SStephan Uphoff 				error = EFAULT;
102968ff3c24SStephan Uphoff 				goto err_exit;
103068ff3c24SStephan Uphoff 			}
1031610ecfe0SMaxim Sobolev 			if ((error = copyinstr(envp, args->endp,
1032610ecfe0SMaxim Sobolev 			    args->stringspace, &length))) {
10330fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
103468ff3c24SStephan Uphoff 					error = E2BIG;
103568ff3c24SStephan Uphoff 				goto err_exit;
10360fd1a014SDavid Greenman 			}
1037610ecfe0SMaxim Sobolev 			args->stringspace -= length;
1038610ecfe0SMaxim Sobolev 			args->endp += length;
1039610ecfe0SMaxim Sobolev 			args->envc++;
104026f9a767SRodney W. Grimes 		}
10410fd1a014SDavid Greenman 	}
104226f9a767SRodney W. Grimes 
104326f9a767SRodney W. Grimes 	return (0);
104468ff3c24SStephan Uphoff 
104568ff3c24SStephan Uphoff err_exit:
104668ff3c24SStephan Uphoff 	exec_free_args(args);
104768ff3c24SStephan Uphoff 	return (error);
104826f9a767SRodney W. Grimes }
104926f9a767SRodney W. Grimes 
10508917b8d2SJohn Baldwin static void
1051610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args)
1052610ecfe0SMaxim Sobolev {
1053610ecfe0SMaxim Sobolev 
1054610ecfe0SMaxim Sobolev 	if (args->buf) {
105590dc539bSMaxim Sobolev 		kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
105690dc539bSMaxim Sobolev 		    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1057610ecfe0SMaxim Sobolev 		args->buf = NULL;
1058610ecfe0SMaxim Sobolev 	}
1059610ecfe0SMaxim Sobolev }
1060610ecfe0SMaxim Sobolev 
106126f9a767SRodney W. Grimes /*
106226f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
106326f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
106426f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
106526f9a767SRodney W. Grimes  */
1066654f6be1SBruce Evans register_t *
1067c52007c2SDavid Greenman exec_copyout_strings(imgp)
1068c52007c2SDavid Greenman 	struct image_params *imgp;
106926f9a767SRodney W. Grimes {
107026f9a767SRodney W. Grimes 	int argc, envc;
107126f9a767SRodney W. Grimes 	char **vectp;
107226f9a767SRodney W. Grimes 	char *stringp, *destp;
1073654f6be1SBruce Evans 	register_t *stack_base;
107493f6448cSDavid Greenman 	struct ps_strings *arginfo;
1075f3bec5d7SJake Burkholder 	struct proc *p;
1076d66a5066SPeter Wemm 	int szsigcode;
107726f9a767SRodney W. Grimes 
107826f9a767SRodney W. Grimes 	/*
107926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
1080d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
108126f9a767SRodney W. Grimes 	 */
1082f3bec5d7SJake Burkholder 	p = imgp->proc;
1083f3bec5d7SJake Burkholder 	szsigcode = 0;
108405ba50f5SJake Burkholder 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1085f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
1086f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
1087d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1088610ecfe0SMaxim Sobolev 	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
10891ed012f9SPeter Wemm 
109026f9a767SRodney W. Grimes 	/*
1091d66a5066SPeter Wemm 	 * install sigcode
1092d66a5066SPeter Wemm 	 */
1093d66a5066SPeter Wemm 	if (szsigcode)
1094f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1095f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
1096d66a5066SPeter Wemm 
1097d66a5066SPeter Wemm 	/*
1098e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
1099e1743d02SSøren Schmidt 	 * on the stack.
1100e1743d02SSøren Schmidt 	 */
1101b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
1102e1743d02SSøren Schmidt 		/*
1103b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1104b9a22da4STakanori Watanabe 		 * lower compatibility.
1105b9a22da4STakanori Watanabe 		 */
1106ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1107ca0387efSJake Burkholder 		    (AT_COUNT * 2);
1108b9a22da4STakanori Watanabe 		/*
1109b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1110b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
1111b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
1112e1743d02SSøren Schmidt 		 */
1113610ecfe0SMaxim Sobolev 		vectp = (char **)(destp - (imgp->args->argc +
1114610ecfe0SMaxim Sobolev 		    imgp->args->envc + 2 + imgp->auxarg_size) *
1115610ecfe0SMaxim Sobolev 		    sizeof(char *));
1116b9a22da4STakanori Watanabe 
1117610ecfe0SMaxim Sobolev 	} else {
1118e1743d02SSøren Schmidt 		/*
1119b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1120b9a22da4STakanori Watanabe 		 * the arg and env vector sets
112126f9a767SRodney W. Grimes 		 */
1122610ecfe0SMaxim Sobolev 		vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1123ca0387efSJake Burkholder 		    sizeof(char *));
1124610ecfe0SMaxim Sobolev 	}
112526f9a767SRodney W. Grimes 
112626f9a767SRodney W. Grimes 	/*
112726f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
112826f9a767SRodney W. Grimes 	 */
1129654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
113026f9a767SRodney W. Grimes 
1131610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
1132610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
1133610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
113426f9a767SRodney W. Grimes 
113593f6448cSDavid Greenman 	/*
11363aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
113793f6448cSDavid Greenman 	 */
1138610ecfe0SMaxim Sobolev 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
113993f6448cSDavid Greenman 
114093f6448cSDavid Greenman 	/*
11413aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
11423aed948bSDavid Greenman 	 */
1143aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
11443aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
11453aed948bSDavid Greenman 
11463aed948bSDavid Greenman 	/*
11473aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
114893f6448cSDavid Greenman 	 */
114926f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
1150aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
11513aed948bSDavid Greenman 		while (*stringp++ != 0)
11523aed948bSDavid Greenman 			destp++;
11533aed948bSDavid Greenman 		destp++;
115426f9a767SRodney W. Grimes 	}
115526f9a767SRodney W. Grimes 
11561a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
11576ab46d52SBruce Evans 	suword(vectp++, 0);
115826f9a767SRodney W. Grimes 
1159aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
11603aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
116193f6448cSDavid Greenman 
116293f6448cSDavid Greenman 	/*
11633aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
116493f6448cSDavid Greenman 	 */
116526f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
1166aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
11673aed948bSDavid Greenman 		while (*stringp++ != 0)
11683aed948bSDavid Greenman 			destp++;
11693aed948bSDavid Greenman 		destp++;
117026f9a767SRodney W. Grimes 	}
117126f9a767SRodney W. Grimes 
117226f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
11736ab46d52SBruce Evans 	suword(vectp, 0);
117426f9a767SRodney W. Grimes 
117526f9a767SRodney W. Grimes 	return (stack_base);
117626f9a767SRodney W. Grimes }
117726f9a767SRodney W. Grimes 
117826f9a767SRodney W. Grimes /*
117926f9a767SRodney W. Grimes  * Check permissions of file to execute.
1180cf64863aSRobert Watson  *	Called with imgp->vp locked.
118126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
118226f9a767SRodney W. Grimes  */
1183c8a79999SPeter Wemm int
1184c52007c2SDavid Greenman exec_check_permissions(imgp)
1185c52007c2SDavid Greenman 	struct image_params *imgp;
118626f9a767SRodney W. Grimes {
1187c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
1188c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
1189a854ed98SJohn Baldwin 	struct thread *td;
119026f9a767SRodney W. Grimes 	int error;
119126f9a767SRodney W. Grimes 
1192a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
1193339b79b9SRobert Watson 
1194ec35c2afSRobert Watson 	/* Get file attributes */
1195ec35c2afSRobert Watson 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1196ec35c2afSRobert Watson 	if (error)
1197ec35c2afSRobert Watson 		return (error);
1198ec35c2afSRobert Watson 
1199339b79b9SRobert Watson #ifdef MAC
1200670cb89bSRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1201339b79b9SRobert Watson 	if (error)
1202339b79b9SRobert Watson 		return (error);
1203339b79b9SRobert Watson #endif
1204339b79b9SRobert Watson 
120526f9a767SRodney W. Grimes 	/*
120626f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
120726f9a767SRodney W. Grimes 	 *	file resides on.
120826f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
120926f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
121026f9a767SRodney W. Grimes 	 *	file really is executable.
121126f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
121226f9a767SRodney W. Grimes 	 */
1213c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
121426f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
1215a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
121626f9a767SRodney W. Grimes 		return (EACCES);
121726f9a767SRodney W. Grimes 
121826f9a767SRodney W. Grimes 	/*
121926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
122026f9a767SRodney W. Grimes 	 */
122126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
122226f9a767SRodney W. Grimes 		return (ENOEXEC);
122326f9a767SRodney W. Grimes 
122426f9a767SRodney W. Grimes 	/*
122526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
122626f9a767SRodney W. Grimes 	 */
1227a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
122826f9a767SRodney W. Grimes 	if (error)
122926f9a767SRodney W. Grimes 		return (error);
123026f9a767SRodney W. Grimes 
12316d5a0a8cSDavid Greenman 	/*
12326d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
12336d5a0a8cSDavid Greenman 	 * if there are any.
12346d5a0a8cSDavid Greenman 	 */
12356d5a0a8cSDavid Greenman 	if (vp->v_writecount)
12366d5a0a8cSDavid Greenman 		return (ETXTBSY);
12376d5a0a8cSDavid Greenman 
12386d5a0a8cSDavid Greenman 	/*
12396d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
12406d5a0a8cSDavid Greenman 	 * general case).
12416d5a0a8cSDavid Greenman 	 */
1242a8d43c90SPoul-Henning Kamp 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
124326f9a767SRodney W. Grimes 	return (error);
1244df8bae1dSRodney W. Grimes }
1245aa855a59SPeter Wemm 
1246aa855a59SPeter Wemm /*
1247aa855a59SPeter Wemm  * Exec handler registration
1248aa855a59SPeter Wemm  */
1249aa855a59SPeter Wemm int
1250aa855a59SPeter Wemm exec_register(execsw_arg)
1251aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1252aa855a59SPeter Wemm {
1253aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1254aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1255aa855a59SPeter Wemm 
1256aa855a59SPeter Wemm 	if (execsw)
1257aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1258aa855a59SPeter Wemm 			count++;
1259a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1260aa855a59SPeter Wemm 	if (newexecsw == NULL)
1261a7cddfedSJake Burkholder 		return (ENOMEM);
1262aa855a59SPeter Wemm 	xs = newexecsw;
1263aa855a59SPeter Wemm 	if (execsw)
1264aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1265aa855a59SPeter Wemm 			*xs++ = *es;
1266aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1267aa855a59SPeter Wemm 	*xs = NULL;
1268aa855a59SPeter Wemm 	if (execsw)
1269aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1270aa855a59SPeter Wemm 	execsw = newexecsw;
1271a7cddfedSJake Burkholder 	return (0);
1272aa855a59SPeter Wemm }
1273aa855a59SPeter Wemm 
1274aa855a59SPeter Wemm int
1275aa855a59SPeter Wemm exec_unregister(execsw_arg)
1276aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1277aa855a59SPeter Wemm {
1278aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1279aa855a59SPeter Wemm 	int count = 1;
1280aa855a59SPeter Wemm 
1281aa855a59SPeter Wemm 	if (execsw == NULL)
1282aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1283aa855a59SPeter Wemm 
1284aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1285aa855a59SPeter Wemm 		if (*es == execsw_arg)
1286aa855a59SPeter Wemm 			break;
1287aa855a59SPeter Wemm 	}
1288aa855a59SPeter Wemm 	if (*es == NULL)
1289a7cddfedSJake Burkholder 		return (ENOENT);
1290aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1291aa855a59SPeter Wemm 		if (*es != execsw_arg)
1292aa855a59SPeter Wemm 			count++;
1293a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1294aa855a59SPeter Wemm 	if (newexecsw == NULL)
1295a7cddfedSJake Burkholder 		return (ENOMEM);
1296aa855a59SPeter Wemm 	xs = newexecsw;
1297aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1298aa855a59SPeter Wemm 		if (*es != execsw_arg)
1299aa855a59SPeter Wemm 			*xs++ = *es;
1300aa855a59SPeter Wemm 	*xs = NULL;
1301aa855a59SPeter Wemm 	if (execsw)
1302aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1303aa855a59SPeter Wemm 	execsw = newexecsw;
1304a7cddfedSJake Burkholder 	return (0);
1305aa855a59SPeter Wemm }
1306