xref: /freebsd/sys/kern/kern_exec.c (revision 532c491b2bbc9e47b6655cadfff6cd556cd62fb9)
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 
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>
546004362eSDavid Schultz #include <sys/resourcevar.h>
5559c8bc40SAlan Cox #include <sys/sf_buf.h>
5684e0b075SDavid Xu #include <sys/syscallsubr.h>
571e1e0b44SSøren Schmidt #include <sys/sysent.h>
58797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5999ac3bc8SPeter Wemm #include <sys/sysctl.h>
60a794e791SBruce Evans #include <sys/vnode.h>
61c781aea8SPeter Wemm #ifdef KTRACE
62c781aea8SPeter Wemm #include <sys/ktrace.h>
63c781aea8SPeter Wemm #endif
6426f9a767SRodney W. Grimes 
6526f9a767SRodney W. Grimes #include <vm/vm.h>
66efeaf95aSDavid Greenman #include <vm/vm_param.h>
67efeaf95aSDavid Greenman #include <vm/pmap.h>
681616db3cSJohn Dyson #include <vm/vm_page.h>
69efeaf95aSDavid Greenman #include <vm/vm_map.h>
7026f9a767SRodney W. Grimes #include <vm/vm_kern.h>
71efeaf95aSDavid Greenman #include <vm/vm_extern.h>
721ebd0c59SDavid Greenman #include <vm/vm_object.h>
731616db3cSJohn Dyson #include <vm/vm_pager.h>
7426f9a767SRodney W. Grimes 
75ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
76ebccf1e3SJoseph Koshy #include <sys/pmckern.h>
77ebccf1e3SJoseph Koshy #endif
78ebccf1e3SJoseph Koshy 
7926f9a767SRodney W. Grimes #include <machine/reg.h>
8026f9a767SRodney W. Grimes 
81b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
82b9df5231SPoul-Henning Kamp 
8305ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
8405ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
855dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
86610ecfe0SMaxim Sobolev static int do_execve(struct thread *td, struct image_args *args,
87610ecfe0SMaxim Sobolev     struct mac *mac_p);
8805ba50f5SJake Burkholder 
899701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9005ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
9105ba50f5SJake Burkholder     NULL, 0, sysctl_kern_ps_strings, "LU", "");
92486bddb0SDoug Rabson 
939701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
9405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
9505ba50f5SJake Burkholder     NULL, 0, sysctl_kern_usrstack, "LU", "");
9699ac3bc8SPeter Wemm 
975dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
985dadd17bSJake Burkholder     NULL, 0, sysctl_kern_stackprot, "I", "");
995dadd17bSJake Burkholder 
100b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
101c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
1029701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
103b9df5231SPoul-Henning Kamp 
10405ba50f5SJake Burkholder static int
10505ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
10605ba50f5SJake Burkholder {
10705ba50f5SJake Burkholder 	struct proc *p;
108df7c361eSPeter Wemm 	int error;
10905ba50f5SJake Burkholder 
11005ba50f5SJake Burkholder 	p = curproc;
111a7bc3102SPeter Wemm #ifdef SCTL_MASK32
112a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
113df7c361eSPeter Wemm 		unsigned int val;
114df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_psstrings;
115df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
116df7c361eSPeter Wemm 	} else
117df7c361eSPeter Wemm #endif
118df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
119df7c361eSPeter Wemm 		   sizeof(p->p_sysent->sv_psstrings));
120df7c361eSPeter Wemm 	return error;
12105ba50f5SJake Burkholder }
12205ba50f5SJake Burkholder 
12305ba50f5SJake Burkholder static int
12405ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
12505ba50f5SJake Burkholder {
12605ba50f5SJake Burkholder 	struct proc *p;
127df7c361eSPeter Wemm 	int error;
12805ba50f5SJake Burkholder 
12905ba50f5SJake Burkholder 	p = curproc;
130a7bc3102SPeter Wemm #ifdef SCTL_MASK32
131a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
132df7c361eSPeter Wemm 		unsigned int val;
133df7c361eSPeter Wemm 		val = (unsigned int)p->p_sysent->sv_usrstack;
134df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &val, sizeof(val));
135df7c361eSPeter Wemm 	} else
136df7c361eSPeter Wemm #endif
137df7c361eSPeter Wemm 		error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
138df7c361eSPeter Wemm 		    sizeof(p->p_sysent->sv_usrstack));
139df7c361eSPeter Wemm 	return error;
14005ba50f5SJake Burkholder }
14105ba50f5SJake Burkholder 
1425dadd17bSJake Burkholder static int
1435dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
1445dadd17bSJake Burkholder {
1455dadd17bSJake Burkholder 	struct proc *p;
1465dadd17bSJake Burkholder 
1475dadd17bSJake Burkholder 	p = curproc;
1485dadd17bSJake Burkholder 	return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
1495dadd17bSJake Burkholder 	    sizeof(p->p_sysent->sv_stackprot)));
1505dadd17bSJake Burkholder }
1515dadd17bSJake Burkholder 
15299ac3bc8SPeter Wemm /*
153aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
154aa855a59SPeter Wemm  * double pointer here.
155df8bae1dSRodney W. Grimes  */
156aa855a59SPeter Wemm static const struct execsw **execsw;
15726f9a767SRodney W. Grimes 
158ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_
159ca46e90eSBruce Evans struct execve_args {
160ca46e90eSBruce Evans 	char    *fname;
161ca46e90eSBruce Evans 	char    **argv;
162ca46e90eSBruce Evans 	char    **envv;
163ca46e90eSBruce Evans };
164ca46e90eSBruce Evans #endif
165ca46e90eSBruce Evans 
166ca46e90eSBruce Evans /*
167ca46e90eSBruce Evans  * MPSAFE
168ca46e90eSBruce Evans  */
169ca46e90eSBruce Evans int
170ca46e90eSBruce Evans execve(td, uap)
171ca46e90eSBruce Evans 	struct thread *td;
172ca46e90eSBruce Evans 	struct execve_args /* {
173ca46e90eSBruce Evans 		char *fname;
174ca46e90eSBruce Evans 		char **argv;
175ca46e90eSBruce Evans 		char **envv;
176ca46e90eSBruce Evans 	} */ *uap;
177ca46e90eSBruce Evans {
178610ecfe0SMaxim Sobolev 	int error;
179610ecfe0SMaxim Sobolev 	struct image_args args;
180ca46e90eSBruce Evans 
181610ecfe0SMaxim Sobolev 	error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
182610ecfe0SMaxim Sobolev 	    uap->argv, uap->envv);
183610ecfe0SMaxim Sobolev 
184610ecfe0SMaxim Sobolev 	if (error == 0)
185610ecfe0SMaxim Sobolev 		error = kern_execve(td, &args, NULL);
186610ecfe0SMaxim Sobolev 
187610ecfe0SMaxim Sobolev 	exec_free_args(&args);
188610ecfe0SMaxim Sobolev 
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 
221610ecfe0SMaxim Sobolev 	if (error == 0)
222610ecfe0SMaxim Sobolev 		error = kern_execve(td, &args, uap->mac_p);
223610ecfe0SMaxim Sobolev 
224610ecfe0SMaxim Sobolev 	exec_free_args(&args);
225610ecfe0SMaxim Sobolev 
226610ecfe0SMaxim Sobolev 	return (error);
227ca46e90eSBruce Evans #else
228ca46e90eSBruce Evans 	return (ENOSYS);
229ca46e90eSBruce Evans #endif
230ca46e90eSBruce Evans }
231ca46e90eSBruce Evans 
23284e0b075SDavid Xu int
233610ecfe0SMaxim Sobolev kern_execve(td, args, mac_p)
234906ac69dSDavid Xu 	struct thread *td;
235610ecfe0SMaxim Sobolev 	struct image_args *args;
236906ac69dSDavid Xu 	struct mac *mac_p;
237906ac69dSDavid Xu {
238906ac69dSDavid Xu 	struct proc *p = td->td_proc;
239906ac69dSDavid Xu 	int error;
240906ac69dSDavid Xu 
241906ac69dSDavid Xu 	if (p->p_flag & P_HADTHREADS) {
242906ac69dSDavid Xu 		PROC_LOCK(p);
243906ac69dSDavid Xu 		if (thread_single(SINGLE_BOUNDARY)) {
244906ac69dSDavid Xu 			PROC_UNLOCK(p);
245906ac69dSDavid Xu 			return (ERESTART);	/* Try again later. */
246906ac69dSDavid Xu 		}
247906ac69dSDavid Xu 		PROC_UNLOCK(p);
248906ac69dSDavid Xu 	}
249906ac69dSDavid Xu 
250610ecfe0SMaxim Sobolev 	error = do_execve(td, args, mac_p);
251906ac69dSDavid Xu 
252906ac69dSDavid Xu 	if (p->p_flag & P_HADTHREADS) {
253906ac69dSDavid Xu 		PROC_LOCK(p);
254906ac69dSDavid Xu 		/*
255906ac69dSDavid Xu 		 * If success, we upgrade to SINGLE_EXIT state to
256906ac69dSDavid Xu 		 * force other threads to suicide.
257906ac69dSDavid Xu 		 */
258906ac69dSDavid Xu 		if (error == 0)
259906ac69dSDavid Xu 			thread_single(SINGLE_EXIT);
260906ac69dSDavid Xu 		else
261906ac69dSDavid Xu 			thread_single_end();
262906ac69dSDavid Xu 		PROC_UNLOCK(p);
263906ac69dSDavid Xu 	}
264906ac69dSDavid Xu 
265906ac69dSDavid Xu 	return (error);
266906ac69dSDavid Xu }
267906ac69dSDavid Xu 
26826f9a767SRodney W. Grimes /*
269450ffb44SRobert Watson  * In-kernel implementation of execve().  All arguments are assumed to be
270450ffb44SRobert Watson  * userspace pointers from the passed thread.
271116734c4SMatthew Dillon  *
272116734c4SMatthew Dillon  * MPSAFE
27326f9a767SRodney W. Grimes  */
274450ffb44SRobert Watson static int
275610ecfe0SMaxim Sobolev do_execve(td, args, mac_p)
276b40ce416SJulian Elischer 	struct thread *td;
277610ecfe0SMaxim Sobolev 	struct image_args *args;
278670cb89bSRobert Watson 	struct mac *mac_p;
279df8bae1dSRodney W. Grimes {
280b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
28126f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
2829b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
2831419eacbSAlfred Perlstein 	struct uidinfo *euip;
284654f6be1SBruce Evans 	register_t *stack_base;
285bb56ec4aSPoul-Henning Kamp 	int error, len, i;
286c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
28726f9a767SRodney W. Grimes 	struct vattr attr;
2884d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
28969be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
29090af4afaSJohn Baldwin 	struct sigacts *oldsigacts, *newsigacts;
2916c84de02SJohn Baldwin #ifdef KTRACE
2926c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
293a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
2946c84de02SJohn Baldwin #endif
2956c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
296d06c0d4dSRobert Watson 	int credential_changing;
297269576c8SJeff Roberson 	int vfslocked;
298619eb6e5SJeff Roberson 	int textset;
299ccafe7ebSRobert Watson #ifdef MAC
300eca8a663SRobert Watson 	struct label *interplabel = NULL;
301eca8a663SRobert Watson 	int will_transition;
302ccafe7ebSRobert Watson #endif
30326f9a767SRodney W. Grimes 
304532c491bSJeff Roberson 	vfslocked = 0;
305c52007c2SDavid Greenman 	imgp = &image_params;
306df8bae1dSRodney W. Grimes 
3079ca45e81SDag-Erling Smørgrav 	/*
3089ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
3099ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
3109ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
3119ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
3129ca45e81SDag-Erling Smørgrav 	 * privileges.
3139ca45e81SDag-Erling Smørgrav 	 */
3149ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
3159ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
31691f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
3179ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
3189ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
3199ca45e81SDag-Erling Smørgrav 
320df8bae1dSRodney W. Grimes 	/*
321c52007c2SDavid Greenman 	 * Initialize part of the common data
322df8bae1dSRodney W. Grimes 	 */
323c52007c2SDavid Greenman 	imgp->proc = p;
324670cb89bSRobert Watson 	imgp->execlabel = NULL;
325c52007c2SDavid Greenman 	imgp->attr = &attr;
326c52007c2SDavid Greenman 	imgp->entry_addr = 0;
327c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
328c52007c2SDavid Greenman 	imgp->interpreted = 0;
32990dc539bSMaxim Sobolev 	imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
330e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
3311616db3cSJohn Dyson 	imgp->vp = NULL;
3320b2ed1aeSJeff Roberson 	imgp->object = NULL;
3331616db3cSJohn Dyson 	imgp->firstpage = NULL;
3344fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
335b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
336610ecfe0SMaxim Sobolev 	imgp->args = args;
33726f9a767SRodney W. Grimes 
338670cb89bSRobert Watson #ifdef MAC
339eca8a663SRobert Watson 	error = mac_execve_enter(imgp, mac_p);
340532c491bSJeff Roberson 	if (error)
341670cb89bSRobert Watson 		goto exec_fail;
342670cb89bSRobert Watson #endif
343670cb89bSRobert Watson 
34459c8bc40SAlan Cox 	imgp->image_header = NULL;
34526f9a767SRodney W. Grimes 
34626f9a767SRodney W. Grimes 	/*
34726f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
34826f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
34926f9a767SRodney W. Grimes 	 */
35026f9a767SRodney W. Grimes 	ndp = &nd;
351269576c8SJeff Roberson 	NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
352610ecfe0SMaxim Sobolev 	    UIO_SYSSPACE, args->fname, td);
35326f9a767SRodney W. Grimes 
35426f9a767SRodney W. Grimes interpret:
35526f9a767SRodney W. Grimes 	error = namei(ndp);
356610ecfe0SMaxim Sobolev 	if (error)
35726f9a767SRodney W. Grimes 		goto exec_fail;
35826f9a767SRodney W. Grimes 
359269576c8SJeff Roberson 	vfslocked = NDHASGIANT(ndp);
360c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
36126f9a767SRodney W. Grimes 
36226f9a767SRodney W. Grimes 	/*
3639022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
3649022e4adSDavid Greenman 	 */
365c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
366619eb6e5SJeff Roberson 	if (error)
36726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
368619eb6e5SJeff Roberson 
3698516dd18SPoul-Henning Kamp 	imgp->object = imgp->vp->v_object;
3708516dd18SPoul-Henning Kamp 	if (imgp->object != NULL)
3710b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
37226f9a767SRodney W. Grimes 
373619eb6e5SJeff Roberson 	/*
374619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
375619eb6e5SJeff Roberson 	 * activating it.
376619eb6e5SJeff Roberson 	 *
377619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
378619eb6e5SJeff Roberson 	 * actually an executable image.
379619eb6e5SJeff Roberson 	 */
380619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
381619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
382619eb6e5SJeff Roberson 
3831616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
3846d5a0a8cSDavid Greenman 	if (error)
38526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
38626f9a767SRodney W. Grimes 
38726f9a767SRodney W. Grimes 	/*
388d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
389d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
390d323ddf3SMatthew Dillon 	 *	scripts differently.
39126f9a767SRodney W. Grimes 	 */
392d323ddf3SMatthew Dillon 	error = -1;
393d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
394d323ddf3SMatthew Dillon 		error = img_first(imgp);
395d323ddf3SMatthew Dillon 
396d323ddf3SMatthew Dillon 	/*
397d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
398d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
399d323ddf3SMatthew Dillon 	 *	and an error otherwise.
400d323ddf3SMatthew Dillon 	 */
401d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
402d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
403d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
404d323ddf3SMatthew Dillon 			continue;
405d323ddf3SMatthew Dillon 		}
406c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
407d323ddf3SMatthew Dillon 	}
408d323ddf3SMatthew Dillon 
409d323ddf3SMatthew Dillon 	if (error) {
410619eb6e5SJeff Roberson 		if (error == -1) {
411619eb6e5SJeff Roberson 			if (textset == 0)
412619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
413d323ddf3SMatthew Dillon 			error = ENOEXEC;
414619eb6e5SJeff Roberson 		}
41526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
416d323ddf3SMatthew Dillon 	}
417d323ddf3SMatthew Dillon 
418d323ddf3SMatthew Dillon 	/*
419d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
420d323ddf3SMatthew Dillon 	 * activate the interpreter.
421d323ddf3SMatthew Dillon 	 */
422c52007c2SDavid Greenman 	if (imgp->interpreted) {
4231616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
424619eb6e5SJeff Roberson 		/*
425619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
426619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
427619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
428619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
429619eb6e5SJeff Roberson 		 */
430619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
431762e6b85SEivind Eklund 		/* free name buffer and old vnode */
432762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
433670cb89bSRobert Watson #ifdef MAC
434eca8a663SRobert Watson 		interplabel = mac_vnode_label_alloc();
435eca8a663SRobert Watson 		mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
436670cb89bSRobert Watson #endif
437619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
4380b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
4390b2ed1aeSJeff Roberson 		imgp->object = NULL;
440269576c8SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
441532c491bSJeff Roberson 		vfslocked = 0;
44226f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
443269576c8SJeff Roberson 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
444b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
44526f9a767SRodney W. Grimes 		goto interpret;
44626f9a767SRodney W. Grimes 	}
44726f9a767SRodney W. Grimes 
44826f9a767SRodney W. Grimes 	/*
44926f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
45026f9a767SRodney W. Grimes 	 */
4513ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
4523ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
4533ebc1248SPeter Wemm 	else
454c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
45526f9a767SRodney W. Grimes 
45626f9a767SRodney W. Grimes 	/*
4571e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
4581e1e0b44SSøren Schmidt 	 * let it do the stack setup.
4591e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
46026f9a767SRodney W. Grimes 	 */
461d6c847f3SBruce Evans 	if (p->p_sysent->sv_fixup != NULL)
462c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
4631e1e0b44SSøren Schmidt 	else
464610ecfe0SMaxim Sobolev 		suword(--stack_base, imgp->args->argc);
46526f9a767SRodney W. Grimes 
466a78e8d2aSDavid Greenman 	/*
467a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
468a78e8d2aSDavid Greenman 	 * be shared after an exec.
469a78e8d2aSDavid Greenman 	 */
470c113083cSPoul-Henning Kamp 	fdunshare(p, td);
471a78e8d2aSDavid Greenman 
472333ea485SGuido van Rooij 	/*
4739b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
4749b3b1c5fSJohn Baldwin 	 */
4759b3b1c5fSJohn Baldwin 	newcred = crget();
4761419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
477610ecfe0SMaxim Sobolev 	i = imgp->args->begin_envv - imgp->args->begin_argv;
4789b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
4799b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
4809b3b1c5fSJohn Baldwin 
4819b3b1c5fSJohn Baldwin 	/* close files on exec */
4829b3b1c5fSJohn Baldwin 	fdcloseexec(td);
4839b3b1c5fSJohn Baldwin 
484619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
485619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
486619eb6e5SJeff Roberson 
4879b3b1c5fSJohn Baldwin 	/*
488333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
4898688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
490b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
491333ea485SGuido van Rooij 	 * reset.
492333ea485SGuido van Rooij 	 */
4939b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
49490af4afaSJohn Baldwin 	if (sigacts_shared(p->p_sigacts)) {
49590af4afaSJohn Baldwin 		oldsigacts = p->p_sigacts;
4969b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
49790af4afaSJohn Baldwin 		newsigacts = sigacts_alloc();
49890af4afaSJohn Baldwin 		sigacts_copy(newsigacts, oldsigacts);
4999b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
50090af4afaSJohn Baldwin 		p->p_sigacts = newsigacts;
50190af4afaSJohn Baldwin 	} else
50290af4afaSJohn Baldwin 		oldsigacts = NULL;
503333ea485SGuido van Rooij 
504fdf4e8b3SWarner Losh 	/* Stop profiling */
505fdf4e8b3SWarner Losh 	stopprofclock(p);
506fdf4e8b3SWarner Losh 
50726f9a767SRodney W. Grimes 	/* reset caught signals */
50826f9a767SRodney W. Grimes 	execsigs(p);
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
51126f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
51226f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
51326f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
51426f9a767SRodney W. Grimes 
51526f9a767SRodney W. Grimes 	/*
51624b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
517dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
51826f9a767SRodney W. Grimes 	 */
51926f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
52026f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
52126f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
5227f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
52326f9a767SRodney W. Grimes 	}
52426f9a767SRodney W. Grimes 
52526f9a767SRodney W. Grimes 	/*
526a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
527a78e8d2aSDavid Greenman 	 *
528a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
529a78e8d2aSDavid Greenman 	 * the process is being traced.
530670cb89bSRobert Watson 	 *
531670cb89bSRobert Watson 	 * XXXMAC: For the time being, use NOSUID to also prohibit
532670cb89bSRobert Watson 	 * transitions on the file system.
533c52007c2SDavid Greenman 	 */
534b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
535d06c0d4dSRobert Watson 	credential_changing = 0;
536d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
537d06c0d4dSRobert Watson 	    attr.va_uid;
538d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
539d06c0d4dSRobert Watson 	    attr.va_gid;
540ccafe7ebSRobert Watson #ifdef MAC
541670cb89bSRobert Watson 	will_transition = mac_execve_will_transition(oldcred, imgp->vp,
542eca8a663SRobert Watson 	    interplabel, imgp);
543ccafe7ebSRobert Watson 	credential_changing |= will_transition;
544ccafe7ebSRobert Watson #endif
545d06c0d4dSRobert Watson 
546d06c0d4dSRobert Watson 	if (credential_changing &&
547a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
548c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
549c52007c2SDavid Greenman 		/*
550c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
551b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
552b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
55326f9a767SRodney W. Grimes 		 */
554b85db196SPeter Wemm 		setsugid(p);
5556c84de02SJohn Baldwin #ifdef KTRACE
55656f21b9dSColin Percival 		if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) {
5576c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
55879deba82SMatthew Dillon 			p->p_traceflag = 0;
559a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
560a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
561a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
562a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
5636c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
56426f9a767SRodney W. Grimes 		}
5656c84de02SJohn Baldwin #endif
56628b325aaSDon Lewis 		/*
567c1e2d386SNate Lawson 		 * Close any file descriptors 0..2 that reference procfs,
568c1e2d386SNate Lawson 		 * then make sure file descriptors 0..2 are in use.
56928b325aaSDon Lewis 		 *
570c1e2d386SNate Lawson 		 * setugidsafety() may call closef() and then pfind()
571c1e2d386SNate Lawson 		 * which may grab the process lock.
57228b325aaSDon Lewis 		 * fdcheckstd() may call falloc() which may block to
57328b325aaSDon Lewis 		 * allocate memory, so temporarily drop the process lock.
57428b325aaSDon Lewis 		 */
57528b325aaSDon Lewis 		PROC_UNLOCK(p);
576c1e2d386SNate Lawson 		setugidsafety(td);
577e983a376SJacques Vidrine 		error = fdcheckstd(td);
57863c9e754SJohn Baldwin 		if (error != 0)
57969be5db9SAlfred Perlstein 			goto done1;
580e1b1aa3bSJohn Baldwin 		PROC_LOCK(p);
581c52007c2SDavid Greenman 		/*
582c52007c2SDavid Greenman 		 * Set the new credentials.
583c52007c2SDavid Greenman 		 */
5849b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
585c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
5861419eacbSAlfred Perlstein 			change_euid(newcred, euip);
587c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
588b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
589ccafe7ebSRobert Watson #ifdef MAC
590670cb89bSRobert Watson 		if (will_transition) {
591670cb89bSRobert Watson 			mac_execve_transition(oldcred, newcred, imgp->vp,
592eca8a663SRobert Watson 			    interplabel, imgp);
593670cb89bSRobert Watson 		}
594ccafe7ebSRobert Watson #endif
5959b3b1c5fSJohn Baldwin 		/*
5969b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
597ccafe7ebSRobert Watson 		 *
598ccafe7ebSRobert Watson 		 * XXXMAC: Note that the current logic will save the
599ccafe7ebSRobert Watson 		 * uid and gid if a MAC domain transition occurs, even
600ccafe7ebSRobert Watson 		 * though maybe it shouldn't.
6019b3b1c5fSJohn Baldwin 		 */
6029b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
6039b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
6049b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
6059b3b1c5fSJohn Baldwin 		newcred = NULL;
606c52007c2SDavid Greenman 	} else {
607b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
608b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
609c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
61026f9a767SRodney W. Grimes 		/*
611c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
612b1fc0ec1SRobert Watson 		 *
613b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
6149b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
6159b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
6169b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
6179b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
6189b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
6199b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
6209b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
62126f9a767SRodney W. Grimes 		 */
622b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
623b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
6249b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
625b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
626b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
627b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
6289b3b1c5fSJohn Baldwin 			newcred = NULL;
6299b3b1c5fSJohn Baldwin 		}
630b1fc0ec1SRobert Watson 	}
63126f9a767SRodney W. Grimes 
63226f9a767SRodney W. Grimes 	/*
633619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
634619eb6e5SJeff Roberson 	 * to locking the proc lock.
6352a531c80SDavid Greenman 	 */
6369b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
6372a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
6382a531c80SDavid Greenman 
6392a531c80SDavid Greenman 	/*
6409ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
6419ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
642cb679c38SJonathan Lemon 	 */
643ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
6449ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
645cb679c38SJonathan Lemon 
646cb679c38SJonathan Lemon 	/*
64726f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
64826f9a767SRodney W. Grimes 	 * can be set before the program executes.
649906ac69dSDavid Xu 	 * Use tdsignal to deliver signal to current thread, use
650906ac69dSDavid Xu 	 * psignal may cause the signal to be delivered to wrong thread
651906ac69dSDavid Xu 	 * because that thread will exit, remember we are going to enter
652906ac69dSDavid Xu 	 * single thread mode.
65326f9a767SRodney W. Grimes 	 */
65426f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
655906ac69dSDavid Xu 		tdsignal(td, SIGTRAP, SIGTARGET_TD);
65626f9a767SRodney W. Grimes 
65726f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
65826f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
65926f9a767SRodney W. Grimes 
660b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
6619b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
662b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
663b9df5231SPoul-Henning Kamp 
664e1b1aa3bSJohn Baldwin 	/* Cache arguments if they fit inside our allowance */
665e1b1aa3bSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
666610ecfe0SMaxim Sobolev 		bcopy(imgp->args->begin_argv, newargs->ar_args, i);
667e1b1aa3bSJohn Baldwin 		p->p_args = newargs;
668e1b1aa3bSJohn Baldwin 		newargs = NULL;
669e1b1aa3bSJohn Baldwin 	}
670ebccf1e3SJoseph Koshy 
671ebccf1e3SJoseph Koshy #ifdef	HWPMC_HOOKS
672ebccf1e3SJoseph Koshy 	/*
673ebccf1e3SJoseph Koshy 	 * Check if the process is using PMCs and if so do exec() time
674ebccf1e3SJoseph Koshy 	 * processing.  This processing needs to happen AFTER the
675ebccf1e3SJoseph Koshy 	 * P_INEXEC flag is cleared.
676ebccf1e3SJoseph Koshy 	 *
677ebccf1e3SJoseph Koshy 	 * The proc lock needs to be released before taking the PMC
678ebccf1e3SJoseph Koshy 	 * SX.
679ebccf1e3SJoseph Koshy 	 */
680ebccf1e3SJoseph Koshy 	if (PMC_PROC_IS_USING_PMCS(p)) {
681e1b1aa3bSJohn Baldwin 		PROC_UNLOCK(p);
682ebccf1e3SJoseph Koshy 		PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC,
683ebccf1e3SJoseph Koshy 		    (void *) &credential_changing);
684ebccf1e3SJoseph Koshy 	} else
685ebccf1e3SJoseph Koshy 		PROC_UNLOCK(p);
686ebccf1e3SJoseph Koshy #else  /* !HWPMC_HOOKS */
687ebccf1e3SJoseph Koshy 	PROC_UNLOCK(p);
688ebccf1e3SJoseph Koshy #endif
689e1b1aa3bSJohn Baldwin 
690e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
6913ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
6923ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
6933ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
6943ebc1248SPeter Wemm 	else
695bafbd492SJake Burkholder 		exec_setregs(td, imgp->entry_addr,
696bafbd492SJake Burkholder 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
697e913ca22SDoug Rabson 
69869be5db9SAlfred Perlstein done1:
6999b3b1c5fSJohn Baldwin 	/*
7009b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
7019b3b1c5fSJohn Baldwin 	 */
7021419eacbSAlfred Perlstein 	uifree(euip);
7039b3b1c5fSJohn Baldwin 	if (newcred == NULL)
7049b3b1c5fSJohn Baldwin 		crfree(oldcred);
7059b3b1c5fSJohn Baldwin 	else
7069b3b1c5fSJohn Baldwin 		crfree(newcred);
7079b3b1c5fSJohn Baldwin 	/*
7089b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
7099b3b1c5fSJohn Baldwin 	 */
7109b3b1c5fSJohn Baldwin 	if (textvp != NULL)
7119b3b1c5fSJohn Baldwin 		vrele(textvp);
712619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
713619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
7146c84de02SJohn Baldwin #ifdef KTRACE
7159b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
7169b3b1c5fSJohn Baldwin 		vrele(tracevp);
717a5881ea5SJohn Baldwin 	if (tracecred != NULL)
718a5881ea5SJohn Baldwin 		crfree(tracecred);
7196c84de02SJohn Baldwin #endif
72069be5db9SAlfred Perlstein 	if (oldargs != NULL)
7219b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
72269be5db9SAlfred Perlstein 	if (newargs != NULL)
72369be5db9SAlfred Perlstein 		pargs_drop(newargs);
72490af4afaSJohn Baldwin 	if (oldsigacts != NULL)
72590af4afaSJohn Baldwin 		sigacts_free(oldsigacts);
726b9df5231SPoul-Henning Kamp 
7271616db3cSJohn Dyson exec_fail_dealloc:
7281616db3cSJohn Dyson 
72926f9a767SRodney W. Grimes 	/*
73026f9a767SRodney W. Grimes 	 * free various allocated resources
73126f9a767SRodney W. Grimes 	 */
732d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
7331616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
73426f9a767SRodney W. Grimes 
735d6c847f3SBruce Evans 	if (imgp->vp != NULL) {
736762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
737619eb6e5SJeff Roberson 		vput(imgp->vp);
738a3cf6ebaSDavid Greenman 	}
73926f9a767SRodney W. Grimes 
740d6c847f3SBruce Evans 	if (imgp->object != NULL)
7410b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
7420b2ed1aeSJeff Roberson 
743d1989db5SRobert Drehmel 	if (error == 0) {
744d1989db5SRobert Drehmel 		/*
745d1989db5SRobert Drehmel 		 * Stop the process here if its stop event mask has
746d1989db5SRobert Drehmel 		 * the S_EXEC bit set.
747d1989db5SRobert Drehmel 		 */
748d1989db5SRobert Drehmel 		STOPEVENT(p, S_EXEC, 0);
749116734c4SMatthew Dillon 		goto done2;
750d1989db5SRobert Drehmel 	}
7511616db3cSJohn Dyson 
75226f9a767SRodney W. Grimes exec_fail:
7539ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
7549ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
7559ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
7569ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
7579ca45e81SDag-Erling Smørgrav 
758c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
75926f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
760670cb89bSRobert Watson #ifdef MAC
761670cb89bSRobert Watson 		mac_execve_exit(imgp);
762eca8a663SRobert Watson 		if (interplabel != NULL)
763eca8a663SRobert Watson 			mac_vnode_label_free(interplabel);
764670cb89bSRobert Watson #endif
765532c491bSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
766b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
76726f9a767SRodney W. Grimes 		/* NOT REACHED */
768116734c4SMatthew Dillon 		error = 0;
76926f9a767SRodney W. Grimes 	}
770116734c4SMatthew Dillon done2:
771670cb89bSRobert Watson #ifdef MAC
772670cb89bSRobert Watson 	mac_execve_exit(imgp);
773eca8a663SRobert Watson 	if (interplabel != NULL)
774eca8a663SRobert Watson 		mac_vnode_label_free(interplabel);
775670cb89bSRobert Watson #endif
776269576c8SJeff Roberson 	VFS_UNLOCK_GIANT(vfslocked);
777116734c4SMatthew Dillon 	return (error);
77826f9a767SRodney W. Grimes }
77926f9a767SRodney W. Grimes 
7801616db3cSJohn Dyson int
7811616db3cSJohn Dyson exec_map_first_page(imgp)
7821616db3cSJohn Dyson 	struct image_params *imgp;
7831616db3cSJohn Dyson {
784d8aad40cSJohn Baldwin 	int rv, i;
78595461b45SJohn Dyson 	int initial_pagein;
78695461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
7871616db3cSJohn Dyson 	vm_object_t object;
7881616db3cSJohn Dyson 
789d6c847f3SBruce Evans 	if (imgp->firstpage != NULL)
7901616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
7911616db3cSJohn Dyson 
7928516dd18SPoul-Henning Kamp 	object = imgp->vp->v_object;
7934d7d2993SJeff Roberson 	if (object == NULL)
7944d7d2993SJeff Roberson 		return (EACCES);
795fd0cc9a8SAlan Cox 	VM_OBJECT_LOCK(object);
79695461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
79795461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
79895461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
79995461b45SJohn Dyson 		if (initial_pagein > object->size)
80095461b45SJohn Dyson 			initial_pagein = object->size;
80195461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
802d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
8036ec2fca5SAlan Cox 				if (ma[i]->valid)
8046ec2fca5SAlan Cox 					break;
805ee113343SAlan Cox 				vm_page_lock_queues();
80606fa71cdSAlan Cox 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) {
80706fa71cdSAlan Cox 					vm_page_unlock_queues();
80806fa71cdSAlan Cox 					break;
80906fa71cdSAlan Cox 				}
810e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
811ee113343SAlan Cox 				vm_page_unlock_queues();
81295461b45SJohn Dyson 			} else {
813ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
814ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
81595461b45SJohn Dyson 				if (ma[i] == NULL)
81695461b45SJohn Dyson 					break;
81795461b45SJohn Dyson 			}
81895461b45SJohn Dyson 		}
81995461b45SJohn Dyson 		initial_pagein = i;
82095461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
82195461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
822ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
823ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
824ecbb00a2SDoug Rabson 			if (ma[0]) {
8256ec2fca5SAlan Cox 				vm_page_lock_queues();
8264fec79beSAlan Cox 				pmap_remove_all(ma[0]);
8278f9110f6SJohn Dyson 				vm_page_free(ma[0]);
82806fa71cdSAlan Cox 				vm_page_unlock_queues();
8296ec2fca5SAlan Cox 			}
83006fa71cdSAlan Cox 			VM_OBJECT_UNLOCK(object);
831a7cddfedSJake Burkholder 			return (EIO);
8321616db3cSJohn Dyson 		}
8331616db3cSJohn Dyson 	}
8346ec2fca5SAlan Cox 	vm_page_lock_queues();
835148b3f62SAlan Cox 	vm_page_hold(ma[0]);
836e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
83797646f56SAlan Cox 	vm_page_unlock_queues();
8386ec2fca5SAlan Cox 	VM_OBJECT_UNLOCK(object);
8391616db3cSJohn Dyson 
84059c8bc40SAlan Cox 	imgp->firstpage = sf_buf_alloc(ma[0], 0);
84159c8bc40SAlan Cox 	imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
8421616db3cSJohn Dyson 
843a7cddfedSJake Burkholder 	return (0);
8441616db3cSJohn Dyson }
8451616db3cSJohn Dyson 
8461616db3cSJohn Dyson void
8471616db3cSJohn Dyson exec_unmap_first_page(imgp)
8481616db3cSJohn Dyson 	struct image_params *imgp;
8491616db3cSJohn Dyson {
85059c8bc40SAlan Cox 	vm_page_t m;
85123955314SAlfred Perlstein 
852d6c847f3SBruce Evans 	if (imgp->firstpage != NULL) {
85359c8bc40SAlan Cox 		m = sf_buf_page(imgp->firstpage);
85459c8bc40SAlan Cox 		sf_buf_free(imgp->firstpage);
8551616db3cSJohn Dyson 		imgp->firstpage = NULL;
85659c8bc40SAlan Cox 		vm_page_lock_queues();
85759c8bc40SAlan Cox 		vm_page_unhold(m);
85859c8bc40SAlan Cox 		vm_page_unlock_queues();
8591616db3cSJohn Dyson 	}
8601616db3cSJohn Dyson }
8611616db3cSJohn Dyson 
86226f9a767SRodney W. Grimes /*
86326f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
86426f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
86526f9a767SRodney W. Grimes  *	automatically in trap.c.
86626f9a767SRodney W. Grimes  */
86726f9a767SRodney W. Grimes int
86805ba50f5SJake Burkholder exec_new_vmspace(imgp, sv)
869c52007c2SDavid Greenman 	struct image_params *imgp;
87005ba50f5SJake Burkholder 	struct sysentvec *sv;
87126f9a767SRodney W. Grimes {
87226f9a767SRodney W. Grimes 	int error;
8735e20c11fSAlan Cox 	struct proc *p = imgp->proc;
8745e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
87505ba50f5SJake Burkholder 	vm_offset_t stack_addr;
87605ba50f5SJake Burkholder 	vm_map_t map;
87726f9a767SRodney W. Grimes 
878c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
87926f9a767SRodney W. Grimes 
880a5bdcb2aSPeter Wemm 	/* Called with Giant held, do not depend on it! */
88175b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exec, p);
8826f5dafeaSAlan Cox 
8836f5dafeaSAlan Cox 	/*
884c460ac3aSPeter Wemm 	 * Here is as good a place as any to do any resource limit cleanups.
885c460ac3aSPeter Wemm 	 * This is needed if a 64 bit binary exec's a 32 bit binary - the
886c460ac3aSPeter Wemm 	 * data size limit may need to be changed to a value that makes
887c460ac3aSPeter Wemm 	 * sense for the 32 bit binary.
888c460ac3aSPeter Wemm 	 */
889d6c847f3SBruce Evans 	if (sv->sv_fixlimits != NULL)
890c460ac3aSPeter Wemm 		sv->sv_fixlimits(imgp);
891c460ac3aSPeter Wemm 
892c460ac3aSPeter Wemm 	/*
893af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
894af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
895af9ec885SJohn Dyson 	 * not disrupted
896af9ec885SJohn Dyson 	 */
89705ba50f5SJake Burkholder 	map = &vmspace->vm_map;
89805ba50f5SJake Burkholder 	if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
89905ba50f5SJake Burkholder 	    vm_map_max(map) == sv->sv_maxuser) {
9003db161e0SMatthew Dillon 		shmexit(vmspace);
90105ba50f5SJake Burkholder 		pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
90205ba50f5SJake Burkholder 		    vm_map_max(map));
90305ba50f5SJake Burkholder 		vm_map_remove(map, vm_map_min(map), vm_map_max(map));
904af9ec885SJohn Dyson 	} else {
90505ba50f5SJake Burkholder 		vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
9065e20c11fSAlan Cox 		vmspace = p->p_vmspace;
90705ba50f5SJake Burkholder 		map = &vmspace->vm_map;
908af9ec885SJohn Dyson 	}
90926f9a767SRodney W. Grimes 
91026f9a767SRodney W. Grimes 	/* Allocate a new stack */
911fd75d710SMarcel Moolenaar 	stack_addr = sv->sv_usrstack - maxssiz;
91205ba50f5SJake Burkholder 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
913fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
9142267af78SJulian Elischer 	if (error)
9152267af78SJulian Elischer 		return (error);
9162267af78SJulian Elischer 
91763c47a5cSDoug Rabson #ifdef __ia64__
918fd75d710SMarcel Moolenaar 	/* Allocate a new register stack */
919bab1f052SMarcel Moolenaar 	stack_addr = IA64_BACKINGSTORE;
920fd75d710SMarcel Moolenaar 	error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
921fd75d710SMarcel Moolenaar 	    sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
922fd75d710SMarcel Moolenaar 	if (error)
923fd75d710SMarcel Moolenaar 		return (error);
92463c47a5cSDoug Rabson #endif
92563c47a5cSDoug Rabson 
9262267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
9272267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
9282267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
9292267af78SJulian Elischer 	 */
930cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
93105ba50f5SJake Burkholder 	vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
93226f9a767SRodney W. Grimes 
93326f9a767SRodney W. Grimes 	return (0);
93426f9a767SRodney W. Grimes }
93526f9a767SRodney W. Grimes 
93626f9a767SRodney W. Grimes /*
93726f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
93826f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
93926f9a767SRodney W. Grimes  */
94026f9a767SRodney W. Grimes int
941610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname,
942610ecfe0SMaxim Sobolev     enum uio_seg segflg, char **argv, char **envv)
94326f9a767SRodney W. Grimes {
94426f9a767SRodney W. Grimes 	char *argp, *envp;
945ecbb00a2SDoug Rabson 	int error;
946ecbb00a2SDoug Rabson 	size_t length;
94726f9a767SRodney W. Grimes 
948610ecfe0SMaxim Sobolev 	error = 0;
949610ecfe0SMaxim Sobolev 
950610ecfe0SMaxim Sobolev 	bzero(args, sizeof(*args));
951610ecfe0SMaxim Sobolev 	if (argv == NULL)
952610ecfe0SMaxim Sobolev 		return (EFAULT);
95326f9a767SRodney W. Grimes 	/*
954610ecfe0SMaxim Sobolev 	 * Allocate temporary demand zeroed space for argument and
95590dc539bSMaxim Sobolev 	 *	environment strings:
95690dc539bSMaxim Sobolev 	 *
95790dc539bSMaxim Sobolev 	 * o ARG_MAX for argument and environment;
95890dc539bSMaxim Sobolev 	 * o MAXSHELLCMDLEN for the name of interpreters.
95926f9a767SRodney W. Grimes 	 */
96090dc539bSMaxim Sobolev 	args->buf = (char *) kmem_alloc_wait(exec_map,
96190dc539bSMaxim Sobolev 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
962610ecfe0SMaxim Sobolev 	if (args->buf == NULL)
963610ecfe0SMaxim Sobolev 		return (ENOMEM);
964610ecfe0SMaxim Sobolev 	args->begin_argv = args->buf;
965610ecfe0SMaxim Sobolev 	args->endp = args->begin_argv;
966610ecfe0SMaxim Sobolev 	args->stringspace = ARG_MAX;
96726f9a767SRodney W. Grimes 
968610ecfe0SMaxim Sobolev 	args->fname = args->buf + ARG_MAX;
96926f9a767SRodney W. Grimes 
970610ecfe0SMaxim Sobolev 	/*
971610ecfe0SMaxim Sobolev 	 * Copy the file name.
972610ecfe0SMaxim Sobolev 	 */
973610ecfe0SMaxim Sobolev 	error = (segflg == UIO_SYSSPACE) ?
974610ecfe0SMaxim Sobolev 	    copystr(fname, args->fname, PATH_MAX, &length) :
975610ecfe0SMaxim Sobolev 	    copyinstr(fname, args->fname, PATH_MAX, &length);
976c30af532SMaxim Sobolev 	if (error != 0)
9770fd1a014SDavid Greenman 		return (error);
97826f9a767SRodney W. Grimes 
979610ecfe0SMaxim Sobolev 	/*
980610ecfe0SMaxim Sobolev 	 * extract arguments first
981610ecfe0SMaxim Sobolev 	 */
982610ecfe0SMaxim Sobolev 	while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
983610ecfe0SMaxim Sobolev 		if (argp == (caddr_t) -1)
984610ecfe0SMaxim Sobolev 			return (EFAULT);
985610ecfe0SMaxim Sobolev 		if ((error = copyinstr(argp, args->endp,
986610ecfe0SMaxim Sobolev 		    args->stringspace, &length))) {
987610ecfe0SMaxim Sobolev 			if (error == ENAMETOOLONG)
988610ecfe0SMaxim Sobolev 				return (E2BIG);
989610ecfe0SMaxim Sobolev 			return (error);
990610ecfe0SMaxim Sobolev 		}
991610ecfe0SMaxim Sobolev 		args->stringspace -= length;
992610ecfe0SMaxim Sobolev 		args->endp += length;
993610ecfe0SMaxim Sobolev 		args->argc++;
994610ecfe0SMaxim Sobolev 	}
995610ecfe0SMaxim Sobolev 
996610ecfe0SMaxim Sobolev 	args->begin_envv = args->endp;
997b9df5231SPoul-Henning Kamp 
99826f9a767SRodney W. Grimes 	/*
99926f9a767SRodney W. Grimes 	 * extract environment strings
100026f9a767SRodney W. Grimes 	 */
10010fd1a014SDavid Greenman 	if (envv) {
1002aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
100326f9a767SRodney W. Grimes 			if (envp == (caddr_t)-1)
100426f9a767SRodney W. Grimes 				return (EFAULT);
1005610ecfe0SMaxim Sobolev 			if ((error = copyinstr(envp, args->endp,
1006610ecfe0SMaxim Sobolev 			    args->stringspace, &length))) {
10070fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
100826f9a767SRodney W. Grimes 					return (E2BIG);
10090fd1a014SDavid Greenman 				return (error);
10100fd1a014SDavid Greenman 			}
1011610ecfe0SMaxim Sobolev 			args->stringspace -= length;
1012610ecfe0SMaxim Sobolev 			args->endp += length;
1013610ecfe0SMaxim Sobolev 			args->envc++;
101426f9a767SRodney W. Grimes 		}
10150fd1a014SDavid Greenman 	}
101626f9a767SRodney W. Grimes 
101726f9a767SRodney W. Grimes 	return (0);
101826f9a767SRodney W. Grimes }
101926f9a767SRodney W. Grimes 
1020610ecfe0SMaxim Sobolev void
1021610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args)
1022610ecfe0SMaxim Sobolev {
1023610ecfe0SMaxim Sobolev 
1024610ecfe0SMaxim Sobolev 	if (args->buf) {
102590dc539bSMaxim Sobolev 		kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
102690dc539bSMaxim Sobolev 		    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1027610ecfe0SMaxim Sobolev 		args->buf = NULL;
1028610ecfe0SMaxim Sobolev 	}
1029610ecfe0SMaxim Sobolev }
1030610ecfe0SMaxim Sobolev 
103126f9a767SRodney W. Grimes /*
103226f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
103326f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
103426f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
103526f9a767SRodney W. Grimes  */
1036654f6be1SBruce Evans register_t *
1037c52007c2SDavid Greenman exec_copyout_strings(imgp)
1038c52007c2SDavid Greenman 	struct image_params *imgp;
103926f9a767SRodney W. Grimes {
104026f9a767SRodney W. Grimes 	int argc, envc;
104126f9a767SRodney W. Grimes 	char **vectp;
104226f9a767SRodney W. Grimes 	char *stringp, *destp;
1043654f6be1SBruce Evans 	register_t *stack_base;
104493f6448cSDavid Greenman 	struct ps_strings *arginfo;
1045f3bec5d7SJake Burkholder 	struct proc *p;
1046d66a5066SPeter Wemm 	int szsigcode;
104726f9a767SRodney W. Grimes 
104826f9a767SRodney W. Grimes 	/*
104926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
1050d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
105126f9a767SRodney W. Grimes 	 */
1052f3bec5d7SJake Burkholder 	p = imgp->proc;
1053f3bec5d7SJake Burkholder 	szsigcode = 0;
105405ba50f5SJake Burkholder 	arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1055f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
1056f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
1057d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1058610ecfe0SMaxim Sobolev 	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
10591ed012f9SPeter Wemm 
106026f9a767SRodney W. Grimes 	/*
1061d66a5066SPeter Wemm 	 * install sigcode
1062d66a5066SPeter Wemm 	 */
1063d66a5066SPeter Wemm 	if (szsigcode)
1064f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1065f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
1066d66a5066SPeter Wemm 
1067d66a5066SPeter Wemm 	/*
1068e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
1069e1743d02SSøren Schmidt 	 * on the stack.
1070e1743d02SSøren Schmidt 	 */
1071b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
1072e1743d02SSøren Schmidt 		/*
1073b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1074b9a22da4STakanori Watanabe 		 * lower compatibility.
1075b9a22da4STakanori Watanabe 		 */
1076ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1077ca0387efSJake Burkholder 		    (AT_COUNT * 2);
1078b9a22da4STakanori Watanabe 		/*
1079b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1080b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
1081b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
1082e1743d02SSøren Schmidt 		 */
1083610ecfe0SMaxim Sobolev 		vectp = (char **)(destp - (imgp->args->argc +
1084610ecfe0SMaxim Sobolev 		    imgp->args->envc + 2 + imgp->auxarg_size) *
1085610ecfe0SMaxim Sobolev 		    sizeof(char *));
1086b9a22da4STakanori Watanabe 
1087610ecfe0SMaxim Sobolev 	} else {
1088e1743d02SSøren Schmidt 		/*
1089b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
1090b9a22da4STakanori Watanabe 		 * the arg and env vector sets
109126f9a767SRodney W. Grimes 		 */
1092610ecfe0SMaxim Sobolev 		vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1093ca0387efSJake Burkholder 		    sizeof(char *));
1094610ecfe0SMaxim Sobolev 	}
109526f9a767SRodney W. Grimes 
109626f9a767SRodney W. Grimes 	/*
109726f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
109826f9a767SRodney W. Grimes 	 */
1099654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
110026f9a767SRodney W. Grimes 
1101610ecfe0SMaxim Sobolev 	stringp = imgp->args->begin_argv;
1102610ecfe0SMaxim Sobolev 	argc = imgp->args->argc;
1103610ecfe0SMaxim Sobolev 	envc = imgp->args->envc;
110426f9a767SRodney W. Grimes 
110593f6448cSDavid Greenman 	/*
11063aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
110793f6448cSDavid Greenman 	 */
1108610ecfe0SMaxim Sobolev 	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
110993f6448cSDavid Greenman 
111093f6448cSDavid Greenman 	/*
11113aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
11123aed948bSDavid Greenman 	 */
1113aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
11143aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
11153aed948bSDavid Greenman 
11163aed948bSDavid Greenman 	/*
11173aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
111893f6448cSDavid Greenman 	 */
111926f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
1120aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
11213aed948bSDavid Greenman 		while (*stringp++ != 0)
11223aed948bSDavid Greenman 			destp++;
11233aed948bSDavid Greenman 		destp++;
112426f9a767SRodney W. Grimes 	}
112526f9a767SRodney W. Grimes 
11261a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
11276ab46d52SBruce Evans 	suword(vectp++, 0);
112826f9a767SRodney W. Grimes 
1129aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
11303aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
113193f6448cSDavid Greenman 
113293f6448cSDavid Greenman 	/*
11333aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
113493f6448cSDavid Greenman 	 */
113526f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
1136aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
11373aed948bSDavid Greenman 		while (*stringp++ != 0)
11383aed948bSDavid Greenman 			destp++;
11393aed948bSDavid Greenman 		destp++;
114026f9a767SRodney W. Grimes 	}
114126f9a767SRodney W. Grimes 
114226f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
11436ab46d52SBruce Evans 	suword(vectp, 0);
114426f9a767SRodney W. Grimes 
114526f9a767SRodney W. Grimes 	return (stack_base);
114626f9a767SRodney W. Grimes }
114726f9a767SRodney W. Grimes 
114826f9a767SRodney W. Grimes /*
114926f9a767SRodney W. Grimes  * Check permissions of file to execute.
1150cf64863aSRobert Watson  *	Called with imgp->vp locked.
115126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
115226f9a767SRodney W. Grimes  */
1153c8a79999SPeter Wemm int
1154c52007c2SDavid Greenman exec_check_permissions(imgp)
1155c52007c2SDavid Greenman 	struct image_params *imgp;
115626f9a767SRodney W. Grimes {
1157c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
1158c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
1159a854ed98SJohn Baldwin 	struct thread *td;
116026f9a767SRodney W. Grimes 	int error;
116126f9a767SRodney W. Grimes 
1162a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
1163339b79b9SRobert Watson 
1164ec35c2afSRobert Watson 	/* Get file attributes */
1165ec35c2afSRobert Watson 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1166ec35c2afSRobert Watson 	if (error)
1167ec35c2afSRobert Watson 		return (error);
1168ec35c2afSRobert Watson 
1169339b79b9SRobert Watson #ifdef MAC
1170670cb89bSRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1171339b79b9SRobert Watson 	if (error)
1172339b79b9SRobert Watson 		return (error);
1173339b79b9SRobert Watson #endif
1174339b79b9SRobert Watson 
117526f9a767SRodney W. Grimes 	/*
117626f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
117726f9a767SRodney W. Grimes 	 *	file resides on.
117826f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
117926f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
118026f9a767SRodney W. Grimes 	 *	file really is executable.
118126f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
118226f9a767SRodney W. Grimes 	 */
1183c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
118426f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
1185a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
118626f9a767SRodney W. Grimes 		return (EACCES);
118726f9a767SRodney W. Grimes 
118826f9a767SRodney W. Grimes 	/*
118926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
119026f9a767SRodney W. Grimes 	 */
119126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
119226f9a767SRodney W. Grimes 		return (ENOEXEC);
119326f9a767SRodney W. Grimes 
119426f9a767SRodney W. Grimes 	/*
119526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
119626f9a767SRodney W. Grimes 	 */
1197a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
119826f9a767SRodney W. Grimes 	if (error)
119926f9a767SRodney W. Grimes 		return (error);
120026f9a767SRodney W. Grimes 
12016d5a0a8cSDavid Greenman 	/*
12026d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
12036d5a0a8cSDavid Greenman 	 * if there are any.
12046d5a0a8cSDavid Greenman 	 */
12056d5a0a8cSDavid Greenman 	if (vp->v_writecount)
12066d5a0a8cSDavid Greenman 		return (ETXTBSY);
12076d5a0a8cSDavid Greenman 
12086d5a0a8cSDavid Greenman 	/*
12096d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
12106d5a0a8cSDavid Greenman 	 * general case).
12116d5a0a8cSDavid Greenman 	 */
1212a8d43c90SPoul-Henning Kamp 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
121326f9a767SRodney W. Grimes 	return (error);
1214df8bae1dSRodney W. Grimes }
1215aa855a59SPeter Wemm 
1216aa855a59SPeter Wemm /*
1217aa855a59SPeter Wemm  * Exec handler registration
1218aa855a59SPeter Wemm  */
1219aa855a59SPeter Wemm int
1220aa855a59SPeter Wemm exec_register(execsw_arg)
1221aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1222aa855a59SPeter Wemm {
1223aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1224aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1225aa855a59SPeter Wemm 
1226aa855a59SPeter Wemm 	if (execsw)
1227aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1228aa855a59SPeter Wemm 			count++;
1229a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1230aa855a59SPeter Wemm 	if (newexecsw == NULL)
1231a7cddfedSJake Burkholder 		return (ENOMEM);
1232aa855a59SPeter Wemm 	xs = newexecsw;
1233aa855a59SPeter Wemm 	if (execsw)
1234aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1235aa855a59SPeter Wemm 			*xs++ = *es;
1236aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1237aa855a59SPeter Wemm 	*xs = NULL;
1238aa855a59SPeter Wemm 	if (execsw)
1239aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1240aa855a59SPeter Wemm 	execsw = newexecsw;
1241a7cddfedSJake Burkholder 	return (0);
1242aa855a59SPeter Wemm }
1243aa855a59SPeter Wemm 
1244aa855a59SPeter Wemm int
1245aa855a59SPeter Wemm exec_unregister(execsw_arg)
1246aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1247aa855a59SPeter Wemm {
1248aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1249aa855a59SPeter Wemm 	int count = 1;
1250aa855a59SPeter Wemm 
1251aa855a59SPeter Wemm 	if (execsw == NULL)
1252aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1253aa855a59SPeter Wemm 
1254aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1255aa855a59SPeter Wemm 		if (*es == execsw_arg)
1256aa855a59SPeter Wemm 			break;
1257aa855a59SPeter Wemm 	}
1258aa855a59SPeter Wemm 	if (*es == NULL)
1259a7cddfedSJake Burkholder 		return (ENOENT);
1260aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1261aa855a59SPeter Wemm 		if (*es != execsw_arg)
1262aa855a59SPeter Wemm 			count++;
1263a163d034SWarner Losh 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1264aa855a59SPeter Wemm 	if (newexecsw == NULL)
1265a7cddfedSJake Burkholder 		return (ENOMEM);
1266aa855a59SPeter Wemm 	xs = newexecsw;
1267aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1268aa855a59SPeter Wemm 		if (*es != execsw_arg)
1269aa855a59SPeter Wemm 			*xs++ = *es;
1270aa855a59SPeter Wemm 	*xs = NULL;
1271aa855a59SPeter Wemm 	if (execsw)
1272aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1273aa855a59SPeter Wemm 	execsw = newexecsw;
1274a7cddfedSJake Burkholder 	return (0);
1275aa855a59SPeter Wemm }
1276