xref: /onnv-gate/usr/src/uts/common/os/exec.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
30*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/param.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate #include <sys/signal.h>
38*0Sstevel@tonic-gate #include <sys/cred_impl.h>
39*0Sstevel@tonic-gate #include <sys/policy.h>
40*0Sstevel@tonic-gate #include <sys/user.h>
41*0Sstevel@tonic-gate #include <sys/errno.h>
42*0Sstevel@tonic-gate #include <sys/file.h>
43*0Sstevel@tonic-gate #include <sys/vfs.h>
44*0Sstevel@tonic-gate #include <sys/vnode.h>
45*0Sstevel@tonic-gate #include <sys/mman.h>
46*0Sstevel@tonic-gate #include <sys/acct.h>
47*0Sstevel@tonic-gate #include <sys/cpuvar.h>
48*0Sstevel@tonic-gate #include <sys/proc.h>
49*0Sstevel@tonic-gate #include <sys/cmn_err.h>
50*0Sstevel@tonic-gate #include <sys/debug.h>
51*0Sstevel@tonic-gate #include <sys/pathname.h>
52*0Sstevel@tonic-gate #include <sys/vm.h>
53*0Sstevel@tonic-gate #include <sys/vtrace.h>
54*0Sstevel@tonic-gate #include <sys/exec.h>
55*0Sstevel@tonic-gate #include <sys/exechdr.h>
56*0Sstevel@tonic-gate #include <sys/kmem.h>
57*0Sstevel@tonic-gate #include <sys/prsystm.h>
58*0Sstevel@tonic-gate #include <sys/modctl.h>
59*0Sstevel@tonic-gate #include <sys/vmparam.h>
60*0Sstevel@tonic-gate #include <sys/schedctl.h>
61*0Sstevel@tonic-gate #include <sys/utrap.h>
62*0Sstevel@tonic-gate #include <sys/systeminfo.h>
63*0Sstevel@tonic-gate #include <sys/stack.h>
64*0Sstevel@tonic-gate #include <sys/rctl.h>
65*0Sstevel@tonic-gate #include <sys/dtrace.h>
66*0Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
67*0Sstevel@tonic-gate #include <sys/pool.h>
68*0Sstevel@tonic-gate #include <sys/sdt.h>
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #include <c2/audit.h>
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate #include <vm/hat.h>
73*0Sstevel@tonic-gate #include <vm/anon.h>
74*0Sstevel@tonic-gate #include <vm/as.h>
75*0Sstevel@tonic-gate #include <vm/seg.h>
76*0Sstevel@tonic-gate #include <vm/seg_vn.h>
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #define	PRIV_RESET		0x01	/* needs to reset privs */
79*0Sstevel@tonic-gate #define	PRIV_SETID		0x02	/* needs to change uids */
80*0Sstevel@tonic-gate #define	PRIV_SETUGID		0x04	/* is setuid/setgid/forced privs */
81*0Sstevel@tonic-gate #define	PRIV_INCREASE		0x08	/* child runs with more privs */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *);
84*0Sstevel@tonic-gate static int hold_execsw(struct execsw *);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate uint_t auxv_hwcap = 0;	/* auxv AT_SUN_HWCAP value; determined on the fly */
87*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
88*0Sstevel@tonic-gate uint_t auxv_hwcap32 = 0;	/* 32-bit version of auxv_hwcap */
89*0Sstevel@tonic-gate #endif
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
92*0Sstevel@tonic-gate extern void ldt_free(proc_t *p);
93*0Sstevel@tonic-gate extern void ldt_load(void);
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate int exec_lpg_disable = 0;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate #define	PSUIDFLAGS		(SNOCD|SUGID)
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * exec() - wrapper around exece providing NULL environment pointer
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate int
104*0Sstevel@tonic-gate exec(const char *fname, const char **argp)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	return (exece(fname, argp, NULL));
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * exece() - system call wrapper around exec_common()
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate int
113*0Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	int error;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	error = exec_common(fname, argp, envp);
118*0Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate int
122*0Sstevel@tonic-gate exec_common(const char *fname, const char **argp, const char **envp)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
125*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
126*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
127*0Sstevel@tonic-gate 	struct user *up = PTOU(p);
128*0Sstevel@tonic-gate 	long execsz;		/* temporary count of exec size */
129*0Sstevel@tonic-gate 	int i;
130*0Sstevel@tonic-gate 	int error;
131*0Sstevel@tonic-gate 	char exec_file[MAXCOMLEN+1];
132*0Sstevel@tonic-gate 	struct pathname pn;
133*0Sstevel@tonic-gate 	struct pathname resolvepn;
134*0Sstevel@tonic-gate 	struct uarg args;
135*0Sstevel@tonic-gate 	struct execa ua;
136*0Sstevel@tonic-gate 	k_sigset_t savedmask;
137*0Sstevel@tonic-gate 	lwpdir_t *lwpdir = NULL;
138*0Sstevel@tonic-gate 	lwpdir_t **tidhash;
139*0Sstevel@tonic-gate 	lwpdir_t *old_lwpdir = NULL;
140*0Sstevel@tonic-gate 	uint_t old_lwpdir_sz;
141*0Sstevel@tonic-gate 	lwpdir_t **old_tidhash;
142*0Sstevel@tonic-gate 	uint_t old_tidhash_sz;
143*0Sstevel@tonic-gate 	lwpent_t *lep;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * exec() is not supported for the /proc agent lwp.
147*0Sstevel@tonic-gate 	 */
148*0Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
149*0Sstevel@tonic-gate 		return (ENOTSUP);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if ((error = secpolicy_basic_exec(CRED())) != 0)
152*0Sstevel@tonic-gate 		return (error);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * Inform /proc that an exec() has started.
156*0Sstevel@tonic-gate 	 * Hold signals that are ignored by default so that we will
157*0Sstevel@tonic-gate 	 * not be interrupted by a signal that will be ignored after
158*0Sstevel@tonic-gate 	 * successful completion of gexec().
159*0Sstevel@tonic-gate 	 */
160*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
161*0Sstevel@tonic-gate 	prexecstart();
162*0Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
163*0Sstevel@tonic-gate 	savedmask = curthread->t_hold;
164*0Sstevel@tonic-gate 	sigorset(&curthread->t_hold, &ignoredefault);
165*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * Look up path name and remember last component for later.
169*0Sstevel@tonic-gate 	 * To help coreadm expand its %d token, we attempt to save
170*0Sstevel@tonic-gate 	 * the directory containing the executable in p_execdir. The
171*0Sstevel@tonic-gate 	 * first call to lookuppn() may fail and return EINVAL because
172*0Sstevel@tonic-gate 	 * dirvpp is non-NULL. In that case, we make a second call to
173*0Sstevel@tonic-gate 	 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
174*0Sstevel@tonic-gate 	 * but coreadm is allowed to expand %d to the empty string and
175*0Sstevel@tonic-gate 	 * there are other cases in which that failure may occur.
176*0Sstevel@tonic-gate 	 */
177*0Sstevel@tonic-gate 	if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
178*0Sstevel@tonic-gate 		goto out;
179*0Sstevel@tonic-gate 	pn_alloc(&resolvepn);
180*0Sstevel@tonic-gate 	if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
181*0Sstevel@tonic-gate 		pn_free(&resolvepn);
182*0Sstevel@tonic-gate 		pn_free(&pn);
183*0Sstevel@tonic-gate 		if (error != EINVAL)
184*0Sstevel@tonic-gate 			goto out;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		dir = NULL;
187*0Sstevel@tonic-gate 		if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
188*0Sstevel@tonic-gate 			goto out;
189*0Sstevel@tonic-gate 		pn_alloc(&resolvepn);
190*0Sstevel@tonic-gate 		if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
191*0Sstevel@tonic-gate 		    &vp)) != 0) {
192*0Sstevel@tonic-gate 			pn_free(&resolvepn);
193*0Sstevel@tonic-gate 			pn_free(&pn);
194*0Sstevel@tonic-gate 			goto out;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	if (vp == NULL) {
198*0Sstevel@tonic-gate 		if (dir != NULL)
199*0Sstevel@tonic-gate 			VN_RELE(dir);
200*0Sstevel@tonic-gate 		error = ENOENT;
201*0Sstevel@tonic-gate 		pn_free(&resolvepn);
202*0Sstevel@tonic-gate 		pn_free(&pn);
203*0Sstevel@tonic-gate 		goto out;
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 	bzero(exec_file, MAXCOMLEN+1);
206*0Sstevel@tonic-gate 	(void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
207*0Sstevel@tonic-gate 	bzero(&args, sizeof (args));
208*0Sstevel@tonic-gate 	args.pathname = resolvepn.pn_path;
209*0Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
210*0Sstevel@tonic-gate 	pn_free(&pn);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/*
213*0Sstevel@tonic-gate 	 * Specific exec handlers, or policies determined via
214*0Sstevel@tonic-gate 	 * /etc/system may override the historical default.
215*0Sstevel@tonic-gate 	 */
216*0Sstevel@tonic-gate 	args.stk_prot = PROT_ZFOD;
217*0Sstevel@tonic-gate 	args.dat_prot = PROT_ZFOD;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	CPU_STATS_ADD_K(sys, sysexec, 1);
220*0Sstevel@tonic-gate 	DTRACE_PROC1(exec, char *, args.pathname);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	ua.fname = fname;
223*0Sstevel@tonic-gate 	ua.argp = argp;
224*0Sstevel@tonic-gate 	ua.envp = envp;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
227*0Sstevel@tonic-gate 	    exec_file, p->p_cred)) != 0) {
228*0Sstevel@tonic-gate 		VN_RELE(vp);
229*0Sstevel@tonic-gate 		if (dir != NULL)
230*0Sstevel@tonic-gate 			VN_RELE(dir);
231*0Sstevel@tonic-gate 		pn_free(&resolvepn);
232*0Sstevel@tonic-gate 		goto fail;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * Free floating point registers (sun4u only)
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	ASSERT(lwp != NULL);
239*0Sstevel@tonic-gate 	lwp_freeregs(lwp, 1);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	/*
242*0Sstevel@tonic-gate 	 * Free device context
243*0Sstevel@tonic-gate 	 */
244*0Sstevel@tonic-gate 	if (curthread->t_ctx)
245*0Sstevel@tonic-gate 		freectx(curthread, 1);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	/*
248*0Sstevel@tonic-gate 	 * Remember file name for accounting; clear any cached DTrace predicate.
249*0Sstevel@tonic-gate 	 */
250*0Sstevel@tonic-gate 	up->u_acflag &= ~AFORK;
251*0Sstevel@tonic-gate 	bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
252*0Sstevel@tonic-gate 	curthread->t_predcache = NULL;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/*
255*0Sstevel@tonic-gate 	 * Clear contract template state
256*0Sstevel@tonic-gate 	 */
257*0Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	/*
260*0Sstevel@tonic-gate 	 * Save the directory in which we found the executable for expanding
261*0Sstevel@tonic-gate 	 * the %d token used in core file patterns.
262*0Sstevel@tonic-gate 	 */
263*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
264*0Sstevel@tonic-gate 	tmpvp = p->p_execdir;
265*0Sstevel@tonic-gate 	p->p_execdir = dir;
266*0Sstevel@tonic-gate 	if (p->p_execdir != NULL)
267*0Sstevel@tonic-gate 		VN_HOLD(p->p_execdir);
268*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	if (tmpvp != NULL)
271*0Sstevel@tonic-gate 		VN_RELE(tmpvp);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/*
274*0Sstevel@tonic-gate 	 * Reset stack state to the user stack, clear set of signals
275*0Sstevel@tonic-gate 	 * caught on the signal stack, and reset list of signals that
276*0Sstevel@tonic-gate 	 * restart system calls; the new program's environment should
277*0Sstevel@tonic-gate 	 * not be affected by detritus from the old program.  Any
278*0Sstevel@tonic-gate 	 * pending held signals remain held, so don't clear t_hold.
279*0Sstevel@tonic-gate 	 */
280*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
281*0Sstevel@tonic-gate 	lwp->lwp_oldcontext = 0;
282*0Sstevel@tonic-gate 	lwp->lwp_ustack = 0;
283*0Sstevel@tonic-gate 	lwp->lwp_old_stk_ctl = 0;
284*0Sstevel@tonic-gate 	sigemptyset(&up->u_signodefer);
285*0Sstevel@tonic-gate 	sigemptyset(&up->u_sigonstack);
286*0Sstevel@tonic-gate 	sigemptyset(&up->u_sigresethand);
287*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
288*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
289*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * Make saved resource limit == current resource limit.
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	for (i = 0; i < RLIM_NLIMITS; i++) {
295*0Sstevel@tonic-gate 		/*CONSTCOND*/
296*0Sstevel@tonic-gate 		if (RLIM_SAVED(i)) {
297*0Sstevel@tonic-gate 			(void) rctl_rlimit_get(rctlproc_legacy[i], p,
298*0Sstevel@tonic-gate 			    &up->u_saved_rlimit[i]);
299*0Sstevel@tonic-gate 		}
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/*
303*0Sstevel@tonic-gate 	 * If the action was to catch the signal, then the action
304*0Sstevel@tonic-gate 	 * must be reset to SIG_DFL.
305*0Sstevel@tonic-gate 	 */
306*0Sstevel@tonic-gate 	sigdefault(p);
307*0Sstevel@tonic-gate 	p->p_flag &= ~(SNOWAIT|SJCTL);
308*0Sstevel@tonic-gate 	p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
309*0Sstevel@tonic-gate 	up->u_signal[SIGCLD - 1] = SIG_DFL;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	/*
312*0Sstevel@tonic-gate 	 * Delete the dot4 sigqueues/signotifies.
313*0Sstevel@tonic-gate 	 */
314*0Sstevel@tonic-gate 	sigqfree(p);
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
319*0Sstevel@tonic-gate 	p->p_prof.pr_base = NULL;
320*0Sstevel@tonic-gate 	p->p_prof.pr_size = 0;
321*0Sstevel@tonic-gate 	p->p_prof.pr_off = 0;
322*0Sstevel@tonic-gate 	p->p_prof.pr_scale = 0;
323*0Sstevel@tonic-gate 	p->p_prof.pr_samples = 0;
324*0Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	ASSERT(curthread->t_schedctl == NULL);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
329*0Sstevel@tonic-gate 	/* If the process uses a private LDT then change it to default */
330*0Sstevel@tonic-gate 	if (p->p_ldt)
331*0Sstevel@tonic-gate 		ldt_free(p);
332*0Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate #if defined(__amd64)
335*0Sstevel@tonic-gate 	/*
336*0Sstevel@tonic-gate 	 * Make sure the process has the correct LDT descriptor for its data
337*0Sstevel@tonic-gate 	 * model.
338*0Sstevel@tonic-gate 	 */
339*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64)
340*0Sstevel@tonic-gate 		p->p_ldt_desc = ldt0_default64_desc;
341*0Sstevel@tonic-gate 	else
342*0Sstevel@tonic-gate 		p->p_ldt_desc = ldt0_default_desc;
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	/*
345*0Sstevel@tonic-gate 	 * Ensure the change of LDT is propagated into the LDTR.
346*0Sstevel@tonic-gate 	 */
347*0Sstevel@tonic-gate 	kpreempt_disable();
348*0Sstevel@tonic-gate 	ldt_load();
349*0Sstevel@tonic-gate 	kpreempt_enable();
350*0Sstevel@tonic-gate #endif /* __amd64 */
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate #if defined(__sparc)
353*0Sstevel@tonic-gate 	if (p->p_utraps != NULL)
354*0Sstevel@tonic-gate 		utrap_free(p);
355*0Sstevel@tonic-gate #endif	/* __sparc */
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/*
358*0Sstevel@tonic-gate 	 * Close all close-on-exec files.
359*0Sstevel@tonic-gate 	 */
360*0Sstevel@tonic-gate 	close_exec(P_FINFO(p));
361*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
362*0Sstevel@tonic-gate 	setregs(&args);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	/* Mark this as an executable vnode */
365*0Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
366*0Sstevel@tonic-gate 	vp->v_flag |= VVMEXEC;
367*0Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	VN_RELE(vp);
370*0Sstevel@tonic-gate 	if (dir != NULL)
371*0Sstevel@tonic-gate 		VN_RELE(dir);
372*0Sstevel@tonic-gate 	pn_free(&resolvepn);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	/*
375*0Sstevel@tonic-gate 	 * Allocate a new lwp directory and lwpid hash table if necessary.
376*0Sstevel@tonic-gate 	 */
377*0Sstevel@tonic-gate 	if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
378*0Sstevel@tonic-gate 		lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
379*0Sstevel@tonic-gate 		lwpdir->ld_next = lwpdir + 1;
380*0Sstevel@tonic-gate 		tidhash = kmem_zalloc(2 * sizeof (lwpdir_t *), KM_SLEEP);
381*0Sstevel@tonic-gate 		if (p->p_lwpdir != NULL)
382*0Sstevel@tonic-gate 			lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
383*0Sstevel@tonic-gate 		else
384*0Sstevel@tonic-gate 			lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
388*0Sstevel@tonic-gate 	prbarrier(p);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	/*
391*0Sstevel@tonic-gate 	 * Reset lwp id to the default value of 1.
392*0Sstevel@tonic-gate 	 * This is a single-threaded process now
393*0Sstevel@tonic-gate 	 * and lwp #1 is lwp_wait()able by default.
394*0Sstevel@tonic-gate 	 * The t_unpark flag should not be inherited.
395*0Sstevel@tonic-gate 	 */
396*0Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
397*0Sstevel@tonic-gate 	curthread->t_tid = 1;
398*0Sstevel@tonic-gate 	curthread->t_unpark = 0;
399*0Sstevel@tonic-gate 	curthread->t_proc_flag |= TP_TWAIT;
400*0Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_DAEMON;	/* daemons shouldn't exec */
401*0Sstevel@tonic-gate 	p->p_lwpdaemon = 0;			/* but oh well ... */
402*0Sstevel@tonic-gate 	p->p_lwpid = 1;
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	/*
405*0Sstevel@tonic-gate 	 * Install the newly-allocated lwp directory and lwpid hash table
406*0Sstevel@tonic-gate 	 * and insert the current thread into the new hash table.
407*0Sstevel@tonic-gate 	 */
408*0Sstevel@tonic-gate 	if (lwpdir != NULL) {
409*0Sstevel@tonic-gate 		old_lwpdir = p->p_lwpdir;
410*0Sstevel@tonic-gate 		old_lwpdir_sz = p->p_lwpdir_sz;
411*0Sstevel@tonic-gate 		old_tidhash = p->p_tidhash;
412*0Sstevel@tonic-gate 		old_tidhash_sz = p->p_tidhash_sz;
413*0Sstevel@tonic-gate 		p->p_lwpdir = p->p_lwpfree = lwpdir;
414*0Sstevel@tonic-gate 		p->p_lwpdir_sz = 2;
415*0Sstevel@tonic-gate 		p->p_tidhash = tidhash;
416*0Sstevel@tonic-gate 		p->p_tidhash_sz = 2;
417*0Sstevel@tonic-gate 		lep->le_thread = curthread;
418*0Sstevel@tonic-gate 		lep->le_lwpid = curthread->t_tid;
419*0Sstevel@tonic-gate 		lep->le_start = curthread->t_start;
420*0Sstevel@tonic-gate 		lwp_hash_in(p, lep);
421*0Sstevel@tonic-gate 	}
422*0Sstevel@tonic-gate 	/*
423*0Sstevel@tonic-gate 	 * Restore the saved signal mask and
424*0Sstevel@tonic-gate 	 * inform /proc that the exec() has finished.
425*0Sstevel@tonic-gate 	 */
426*0Sstevel@tonic-gate 	curthread->t_hold = savedmask;
427*0Sstevel@tonic-gate 	prexecend();
428*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
429*0Sstevel@tonic-gate 	if (old_lwpdir) {
430*0Sstevel@tonic-gate 		kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
431*0Sstevel@tonic-gate 		kmem_free(old_tidhash, old_tidhash_sz * sizeof (lwpdir_t *));
432*0Sstevel@tonic-gate 	}
433*0Sstevel@tonic-gate 	ASSERT(error == 0);
434*0Sstevel@tonic-gate 	DTRACE_PROC(exec__success);
435*0Sstevel@tonic-gate 	return (0);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate fail:
438*0Sstevel@tonic-gate 	DTRACE_PROC1(exec__failure, int, error);
439*0Sstevel@tonic-gate out:		/* error return */
440*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
441*0Sstevel@tonic-gate 	curthread->t_hold = savedmask;
442*0Sstevel@tonic-gate 	prexecend();
443*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
444*0Sstevel@tonic-gate 	ASSERT(error != 0);
445*0Sstevel@tonic-gate 	return (error);
446*0Sstevel@tonic-gate }
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate /*
450*0Sstevel@tonic-gate  * Perform generic exec duties and switchout to object-file specific
451*0Sstevel@tonic-gate  * handler.
452*0Sstevel@tonic-gate  */
453*0Sstevel@tonic-gate int
454*0Sstevel@tonic-gate gexec(
455*0Sstevel@tonic-gate 	struct vnode **vpp,
456*0Sstevel@tonic-gate 	struct execa *uap,
457*0Sstevel@tonic-gate 	struct uarg *args,
458*0Sstevel@tonic-gate 	struct intpdata *idatap,
459*0Sstevel@tonic-gate 	int level,
460*0Sstevel@tonic-gate 	long *execsz,
461*0Sstevel@tonic-gate 	caddr_t exec_file,
462*0Sstevel@tonic-gate 	struct cred *cred)
463*0Sstevel@tonic-gate {
464*0Sstevel@tonic-gate 	struct vnode *vp;
465*0Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
466*0Sstevel@tonic-gate 	struct execsw *eswp;
467*0Sstevel@tonic-gate 	int error = 0;
468*0Sstevel@tonic-gate 	int suidflags = 0;
469*0Sstevel@tonic-gate 	ssize_t resid;
470*0Sstevel@tonic-gate 	uid_t uid, gid;
471*0Sstevel@tonic-gate 	struct vattr vattr;
472*0Sstevel@tonic-gate 	char magbuf[MAGIC_BYTES];
473*0Sstevel@tonic-gate 	int setid;
474*0Sstevel@tonic-gate 	cred_t *oldcred, *newcred = NULL;
475*0Sstevel@tonic-gate 	int privflags = 0;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	/*
478*0Sstevel@tonic-gate 	 * If the SNOCD or SUGID flag is set, turn it off and remember the
479*0Sstevel@tonic-gate 	 * previous setting so we can restore it if we encounter an error.
480*0Sstevel@tonic-gate 	 */
481*0Sstevel@tonic-gate 	if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
482*0Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
483*0Sstevel@tonic-gate 		suidflags = pp->p_flag & PSUIDFLAGS;
484*0Sstevel@tonic-gate 		pp->p_flag &= ~PSUIDFLAGS;
485*0Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if ((error = execpermissions(*vpp, &vattr, args)) != 0)
489*0Sstevel@tonic-gate 		goto bad;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	/* need to open vnode for stateful file systems like rfs */
492*0Sstevel@tonic-gate 	if ((error = VOP_OPEN(vpp, FREAD, CRED())) != 0)
493*0Sstevel@tonic-gate 		goto bad;
494*0Sstevel@tonic-gate 	vp = *vpp;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	/*
497*0Sstevel@tonic-gate 	 * Note: to support binary compatibility with SunOS a.out
498*0Sstevel@tonic-gate 	 * executables, we read in the first four bytes, as the
499*0Sstevel@tonic-gate 	 * magic number is in bytes 2-3.
500*0Sstevel@tonic-gate 	 */
501*0Sstevel@tonic-gate 	if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
502*0Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
503*0Sstevel@tonic-gate 		goto bad;
504*0Sstevel@tonic-gate 	if (resid != 0)
505*0Sstevel@tonic-gate 		goto bad;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	if ((eswp = findexec_by_hdr(magbuf)) == NULL)
508*0Sstevel@tonic-gate 		goto bad;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if (level == 0 &&
511*0Sstevel@tonic-gate 	    (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) {
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 		newcred = cred = crdup(cred);
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 		/* If we can, drop the PA bit */
516*0Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0)
517*0Sstevel@tonic-gate 			priv_adjust_PA(cred);
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 		if (privflags & PRIV_SETID) {
520*0Sstevel@tonic-gate 			cred->cr_uid = uid;
521*0Sstevel@tonic-gate 			cred->cr_gid = gid;
522*0Sstevel@tonic-gate 			cred->cr_suid = uid;
523*0Sstevel@tonic-gate 			cred->cr_sgid = gid;
524*0Sstevel@tonic-gate 		}
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 		/*
527*0Sstevel@tonic-gate 		 * Implement the privilege updates:
528*0Sstevel@tonic-gate 		 *
529*0Sstevel@tonic-gate 		 * Restrict with L:
530*0Sstevel@tonic-gate 		 *
531*0Sstevel@tonic-gate 		 *	I' = I & L
532*0Sstevel@tonic-gate 		 *
533*0Sstevel@tonic-gate 		 *	E' = P' = (I' + F) & A
534*0Sstevel@tonic-gate 		 *
535*0Sstevel@tonic-gate 		 * But if running under ptrace, we cap I with P.
536*0Sstevel@tonic-gate 		 */
537*0Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0) {
538*0Sstevel@tonic-gate 			if ((privflags & PRIV_INCREASE) != 0 &&
539*0Sstevel@tonic-gate 			    (pp->p_proc_flag & P_PR_PTRACE) != 0)
540*0Sstevel@tonic-gate 				priv_intersect(&CR_OPPRIV(cred),
541*0Sstevel@tonic-gate 						    &CR_IPRIV(cred));
542*0Sstevel@tonic-gate 			priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
543*0Sstevel@tonic-gate 			CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
544*0Sstevel@tonic-gate 			priv_adjust_PA(cred);
545*0Sstevel@tonic-gate 		}
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	/* SunOS 4.x buy-back */
549*0Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
550*0Sstevel@tonic-gate 	    (vattr.va_mode & (VSUID|VSGID))) {
551*0Sstevel@tonic-gate 		cmn_err(CE_NOTE,
552*0Sstevel@tonic-gate 		    "!%s, uid %d: setuid execution not allowed, dev=%lx",
553*0Sstevel@tonic-gate 		    exec_file, cred->cr_uid, vp->v_vfsp->vfs_dev);
554*0Sstevel@tonic-gate 	}
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/*
557*0Sstevel@tonic-gate 	 * execsetid() told us whether or not we had to change the
558*0Sstevel@tonic-gate 	 * credentials of the process.  In privflags, it told us
559*0Sstevel@tonic-gate 	 * whether we gained any privileges or executed a set-uid executable.
560*0Sstevel@tonic-gate 	 */
561*0Sstevel@tonic-gate 	setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE));
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	/*
564*0Sstevel@tonic-gate 	 * Use /etc/system variable to determine if the stack
565*0Sstevel@tonic-gate 	 * should be marked as executable by default.
566*0Sstevel@tonic-gate 	 */
567*0Sstevel@tonic-gate 	if (noexec_user_stack)
568*0Sstevel@tonic-gate 		args->stk_prot &= ~PROT_EXEC;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	/*
573*0Sstevel@tonic-gate 	 * Traditionally, the setid flags told the sub processes whether
574*0Sstevel@tonic-gate 	 * the file just executed was set-uid or set-gid; this caused
575*0Sstevel@tonic-gate 	 * some confusion as the 'setid' flag did not match the SUGID
576*0Sstevel@tonic-gate 	 * process flag which is only set when the uids/gids do not match.
577*0Sstevel@tonic-gate 	 * A script set-gid/set-uid to the real uid/gid would start with
578*0Sstevel@tonic-gate 	 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
579*0Sstevel@tonic-gate 	 * Now we flag those cases where the calling process cannot
580*0Sstevel@tonic-gate 	 * be trusted to influence the newly exec'ed process, either
581*0Sstevel@tonic-gate 	 * because it runs with more privileges or when the uids/gids
582*0Sstevel@tonic-gate 	 * do in fact not match.
583*0Sstevel@tonic-gate 	 * This also makes the runtime linker agree with the on exec
584*0Sstevel@tonic-gate 	 * values of SNOCD and SUGID.
585*0Sstevel@tonic-gate 	 */
586*0Sstevel@tonic-gate 	error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
587*0Sstevel@tonic-gate 		(setid & PRIV_INCREASE) != 0 ||
588*0Sstevel@tonic-gate 		cred->cr_uid != cred->cr_ruid ||
589*0Sstevel@tonic-gate 		(cred->cr_rgid != cred->cr_gid &&
590*0Sstevel@tonic-gate 		!supgroupmember(cred->cr_gid, cred)), exec_file, cred);
591*0Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
592*0Sstevel@tonic-gate 	if (error != 0) {
593*0Sstevel@tonic-gate 		if (newcred != NULL)
594*0Sstevel@tonic-gate 			crfree(newcred);
595*0Sstevel@tonic-gate 		goto bad;
596*0Sstevel@tonic-gate 	}
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	if (level == 0) {
599*0Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
600*0Sstevel@tonic-gate 		if (newcred != NULL) {
601*0Sstevel@tonic-gate 			/*
602*0Sstevel@tonic-gate 			 * Free the old credentials, and set the new ones.
603*0Sstevel@tonic-gate 			 * Do this for both the process and the (single) thread.
604*0Sstevel@tonic-gate 			 */
605*0Sstevel@tonic-gate 			crfree(pp->p_cred);
606*0Sstevel@tonic-gate 			pp->p_cred = cred;	/* cred already held for proc */
607*0Sstevel@tonic-gate 			crhold(cred);		/* hold new cred for thread */
608*0Sstevel@tonic-gate 			/*
609*0Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
610*0Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
611*0Sstevel@tonic-gate 			 * allocated cred structure.
612*0Sstevel@tonic-gate 			 */
613*0Sstevel@tonic-gate 			oldcred = curthread->t_cred;
614*0Sstevel@tonic-gate 			curthread->t_cred = cred;
615*0Sstevel@tonic-gate 			crfree(oldcred);
616*0Sstevel@tonic-gate 		}
617*0Sstevel@tonic-gate 		/*
618*0Sstevel@tonic-gate 		 * On emerging from a successful exec(), the saved
619*0Sstevel@tonic-gate 		 * uid and gid equal the effective uid and gid.
620*0Sstevel@tonic-gate 		 */
621*0Sstevel@tonic-gate 		cred->cr_suid = cred->cr_uid;
622*0Sstevel@tonic-gate 		cred->cr_sgid = cred->cr_gid;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 		/*
625*0Sstevel@tonic-gate 		 * If the real and effective ids do not match, this
626*0Sstevel@tonic-gate 		 * is a setuid process that should not dump core.
627*0Sstevel@tonic-gate 		 * The group comparison is tricky; we prevent the code
628*0Sstevel@tonic-gate 		 * from flagging SNOCD when executing with an effective gid
629*0Sstevel@tonic-gate 		 * which is a supplementary group.
630*0Sstevel@tonic-gate 		 */
631*0Sstevel@tonic-gate 		if (cred->cr_ruid != cred->cr_uid ||
632*0Sstevel@tonic-gate 		    (cred->cr_rgid != cred->cr_gid &&
633*0Sstevel@tonic-gate 		    !supgroupmember(cred->cr_gid, cred)) ||
634*0Sstevel@tonic-gate 		    (privflags & PRIV_INCREASE) != 0)
635*0Sstevel@tonic-gate 			suidflags = PSUIDFLAGS;
636*0Sstevel@tonic-gate 		else
637*0Sstevel@tonic-gate 			suidflags = 0;
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
640*0Sstevel@tonic-gate 		if (suidflags) {
641*0Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
642*0Sstevel@tonic-gate 			pp->p_flag |= suidflags;
643*0Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
644*0Sstevel@tonic-gate 		}
645*0Sstevel@tonic-gate 		if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
646*0Sstevel@tonic-gate 			/*
647*0Sstevel@tonic-gate 			 * If process is traced via /proc, arrange to
648*0Sstevel@tonic-gate 			 * invalidate the associated /proc vnode.
649*0Sstevel@tonic-gate 			 */
650*0Sstevel@tonic-gate 			if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
651*0Sstevel@tonic-gate 				args->traceinval = 1;
652*0Sstevel@tonic-gate 		}
653*0Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_PTRACE)
654*0Sstevel@tonic-gate 			psignal(pp, SIGTRAP);
655*0Sstevel@tonic-gate 		if (args->traceinval)
656*0Sstevel@tonic-gate 			prinvalidate(&pp->p_user);
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	return (0);
660*0Sstevel@tonic-gate bad:
661*0Sstevel@tonic-gate 	if (error == 0)
662*0Sstevel@tonic-gate 		error = ENOEXEC;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	if (suidflags) {
665*0Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
666*0Sstevel@tonic-gate 		pp->p_flag |= suidflags;
667*0Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
668*0Sstevel@tonic-gate 	}
669*0Sstevel@tonic-gate 	return (error);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate extern char *execswnames[];
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate struct execsw *
675*0Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
676*0Sstevel@tonic-gate {
677*0Sstevel@tonic-gate 	int i, j;
678*0Sstevel@tonic-gate 	char *ename;
679*0Sstevel@tonic-gate 	char *magicp;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	mutex_enter(&execsw_lock);
682*0Sstevel@tonic-gate 	for (i = 0; i < nexectype; i++) {
683*0Sstevel@tonic-gate 		if (execswnames[i] == NULL) {
684*0Sstevel@tonic-gate 			ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
685*0Sstevel@tonic-gate 			(void) strcpy(ename, name);
686*0Sstevel@tonic-gate 			execswnames[i] = ename;
687*0Sstevel@tonic-gate 			/*
688*0Sstevel@tonic-gate 			 * Set the magic number last so that we
689*0Sstevel@tonic-gate 			 * don't need to hold the execsw_lock in
690*0Sstevel@tonic-gate 			 * findexectype().
691*0Sstevel@tonic-gate 			 */
692*0Sstevel@tonic-gate 			magicp = kmem_alloc(magic_size, KM_SLEEP);
693*0Sstevel@tonic-gate 			for (j = 0; j < magic_size; j++)
694*0Sstevel@tonic-gate 				magicp[j] = magic[j];
695*0Sstevel@tonic-gate 			execsw[i].exec_magic = magicp;
696*0Sstevel@tonic-gate 			mutex_exit(&execsw_lock);
697*0Sstevel@tonic-gate 			return (&execsw[i]);
698*0Sstevel@tonic-gate 		}
699*0Sstevel@tonic-gate 	}
700*0Sstevel@tonic-gate 	mutex_exit(&execsw_lock);
701*0Sstevel@tonic-gate 	return (NULL);
702*0Sstevel@tonic-gate }
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate /*
705*0Sstevel@tonic-gate  * Find the exec switch table entry with the corresponding magic string.
706*0Sstevel@tonic-gate  */
707*0Sstevel@tonic-gate struct execsw *
708*0Sstevel@tonic-gate findexecsw(char *magic)
709*0Sstevel@tonic-gate {
710*0Sstevel@tonic-gate 	struct execsw *eswp;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
713*0Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
714*0Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
715*0Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
716*0Sstevel@tonic-gate 			return (eswp);
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate 	return (NULL);
719*0Sstevel@tonic-gate }
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate /*
722*0Sstevel@tonic-gate  * Find the execsw[] index for the given exec header string by looking for the
723*0Sstevel@tonic-gate  * magic string at a specified offset and length for each kind of executable
724*0Sstevel@tonic-gate  * file format until one matches.  If no execsw[] entry is found, try to
725*0Sstevel@tonic-gate  * autoload a module for this magic string.
726*0Sstevel@tonic-gate  */
727*0Sstevel@tonic-gate struct execsw *
728*0Sstevel@tonic-gate findexec_by_hdr(char *header)
729*0Sstevel@tonic-gate {
730*0Sstevel@tonic-gate 	struct execsw *eswp;
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
733*0Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
734*0Sstevel@tonic-gate 		if (header && eswp->exec_maglen != 0 &&
735*0Sstevel@tonic-gate 		    bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
736*0Sstevel@tonic-gate 			    eswp->exec_maglen) == 0) {
737*0Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
738*0Sstevel@tonic-gate 				return (NULL);
739*0Sstevel@tonic-gate 			return (eswp);
740*0Sstevel@tonic-gate 		}
741*0Sstevel@tonic-gate 	}
742*0Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
743*0Sstevel@tonic-gate }
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate /*
746*0Sstevel@tonic-gate  * Find the execsw[] index for the given magic string.  If no execsw[] entry
747*0Sstevel@tonic-gate  * is found, try to autoload a module for this magic string.
748*0Sstevel@tonic-gate  */
749*0Sstevel@tonic-gate struct execsw *
750*0Sstevel@tonic-gate findexec_by_magic(char *magic)
751*0Sstevel@tonic-gate {
752*0Sstevel@tonic-gate 	struct execsw *eswp;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
755*0Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
756*0Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
757*0Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
758*0Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
759*0Sstevel@tonic-gate 				return (NULL);
760*0Sstevel@tonic-gate 			return (eswp);
761*0Sstevel@tonic-gate 		}
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
764*0Sstevel@tonic-gate }
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate static int
767*0Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
768*0Sstevel@tonic-gate {
769*0Sstevel@tonic-gate 	char *name;
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	rw_enter(eswp->exec_lock, RW_READER);
772*0Sstevel@tonic-gate 	while (!LOADED_EXEC(eswp)) {
773*0Sstevel@tonic-gate 		rw_exit(eswp->exec_lock);
774*0Sstevel@tonic-gate 		name = execswnames[eswp-execsw];
775*0Sstevel@tonic-gate 		ASSERT(name);
776*0Sstevel@tonic-gate 		if (modload("exec", name) == -1)
777*0Sstevel@tonic-gate 			return (-1);
778*0Sstevel@tonic-gate 		rw_enter(eswp->exec_lock, RW_READER);
779*0Sstevel@tonic-gate 	}
780*0Sstevel@tonic-gate 	return (0);
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate static int
784*0Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp)
785*0Sstevel@tonic-gate {
786*0Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
787*0Sstevel@tonic-gate 	uid_t uid, gid;
788*0Sstevel@tonic-gate 	cred_t *cr = pp->p_cred;
789*0Sstevel@tonic-gate 	int privflags = 0;
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	/*
792*0Sstevel@tonic-gate 	 * Remember credentials.
793*0Sstevel@tonic-gate 	 */
794*0Sstevel@tonic-gate 	uid = cr->cr_uid;
795*0Sstevel@tonic-gate 	gid = cr->cr_gid;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/* Will try to reset the PRIV_AWARE bit later. */
798*0Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
799*0Sstevel@tonic-gate 		privflags |= PRIV_RESET;
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
802*0Sstevel@tonic-gate 		/*
803*0Sstevel@tonic-gate 		 * Set-uid root execution only allowed if the limit set
804*0Sstevel@tonic-gate 		 * holds all unsafe privileges.
805*0Sstevel@tonic-gate 		 */
806*0Sstevel@tonic-gate 		if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 ||
807*0Sstevel@tonic-gate 		    priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) {
808*0Sstevel@tonic-gate 			uid = vattrp->va_uid;
809*0Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
810*0Sstevel@tonic-gate 		}
811*0Sstevel@tonic-gate 		if (vattrp->va_mode & VSGID) {
812*0Sstevel@tonic-gate 			gid = vattrp->va_gid;
813*0Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
814*0Sstevel@tonic-gate 		}
815*0Sstevel@tonic-gate 	}
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	/*
818*0Sstevel@tonic-gate 	 * Do we need to change our credential anyway?
819*0Sstevel@tonic-gate 	 * This is the case when E != I or P != I, as
820*0Sstevel@tonic-gate 	 * we need to do the assignments (with F empty and A full)
821*0Sstevel@tonic-gate 	 * Or when I is not a subset of L; in that case we need to
822*0Sstevel@tonic-gate 	 * enforce L.
823*0Sstevel@tonic-gate 	 *
824*0Sstevel@tonic-gate 	 *		I' = L & I
825*0Sstevel@tonic-gate 	 *
826*0Sstevel@tonic-gate 	 *		E' = P' = (I' + F) & A
827*0Sstevel@tonic-gate 	 * or
828*0Sstevel@tonic-gate 	 *		E' = P' = I'
829*0Sstevel@tonic-gate 	 */
830*0Sstevel@tonic-gate 	if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
831*0Sstevel@tonic-gate 	    !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
832*0Sstevel@tonic-gate 	    !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
833*0Sstevel@tonic-gate 		privflags |= PRIV_RESET;
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	/*
836*0Sstevel@tonic-gate 	 * When we introduce the "forced" set then we will need
837*0Sstevel@tonic-gate 	 * to set PRIV_INCREASE here if I not a subset of P.
838*0Sstevel@tonic-gate 	 * If the "allowed" set is introduced we will need to do
839*0Sstevel@tonic-gate 	 * a similar thing; however, it seems more reasonable to
840*0Sstevel@tonic-gate 	 * have the allowed set reduce "L": script language interpreters
841*0Sstevel@tonic-gate 	 * would typically have an allowed set of "all".
842*0Sstevel@tonic-gate 	 */
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	/*
845*0Sstevel@tonic-gate 	 * Set setuid/setgid protections if no ptrace() compatibility.
846*0Sstevel@tonic-gate 	 * For privileged processes, honor setuid/setgid even in
847*0Sstevel@tonic-gate 	 * the presence of ptrace() compatibility.
848*0Sstevel@tonic-gate 	 */
849*0Sstevel@tonic-gate 	if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
850*0Sstevel@tonic-gate 	    PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
851*0Sstevel@tonic-gate 	    (cr->cr_uid != uid ||
852*0Sstevel@tonic-gate 	    cr->cr_gid != gid ||
853*0Sstevel@tonic-gate 	    cr->cr_suid != uid ||
854*0Sstevel@tonic-gate 	    cr->cr_sgid != gid)) {
855*0Sstevel@tonic-gate 		*uidp = uid;
856*0Sstevel@tonic-gate 		*gidp = gid;
857*0Sstevel@tonic-gate 		privflags |= PRIV_SETID;
858*0Sstevel@tonic-gate 	}
859*0Sstevel@tonic-gate 	return (privflags);
860*0Sstevel@tonic-gate }
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate int
863*0Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
864*0Sstevel@tonic-gate {
865*0Sstevel@tonic-gate 	int error;
866*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
869*0Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred))
870*0Sstevel@tonic-gate 		return (error);
871*0Sstevel@tonic-gate 	/*
872*0Sstevel@tonic-gate 	 * Check the access mode.
873*0Sstevel@tonic-gate 	 * If VPROC, ask /proc if the file is an object file.
874*0Sstevel@tonic-gate 	 */
875*0Sstevel@tonic-gate 	if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred)) != 0 ||
876*0Sstevel@tonic-gate 	    !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
877*0Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
878*0Sstevel@tonic-gate 	    (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
879*0Sstevel@tonic-gate 		if (error == 0)
880*0Sstevel@tonic-gate 			error = EACCES;
881*0Sstevel@tonic-gate 		return (error);
882*0Sstevel@tonic-gate 	}
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 	if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
885*0Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred))) {
886*0Sstevel@tonic-gate 		/*
887*0Sstevel@tonic-gate 		 * If process is under ptrace(2) compatibility,
888*0Sstevel@tonic-gate 		 * fail the exec(2).
889*0Sstevel@tonic-gate 		 */
890*0Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_PTRACE)
891*0Sstevel@tonic-gate 			goto bad;
892*0Sstevel@tonic-gate 		/*
893*0Sstevel@tonic-gate 		 * Process is traced via /proc.
894*0Sstevel@tonic-gate 		 * Arrange to invalidate the /proc vnode.
895*0Sstevel@tonic-gate 		 */
896*0Sstevel@tonic-gate 		args->traceinval = 1;
897*0Sstevel@tonic-gate 	}
898*0Sstevel@tonic-gate 	return (0);
899*0Sstevel@tonic-gate bad:
900*0Sstevel@tonic-gate 	if (error == 0)
901*0Sstevel@tonic-gate 		error = ENOEXEC;
902*0Sstevel@tonic-gate 	return (error);
903*0Sstevel@tonic-gate }
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate /*
906*0Sstevel@tonic-gate  * Map a section of an executable file into the user's
907*0Sstevel@tonic-gate  * address space.
908*0Sstevel@tonic-gate  */
909*0Sstevel@tonic-gate int
910*0Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
911*0Sstevel@tonic-gate     off_t offset, int prot, int page, uint_t szc)
912*0Sstevel@tonic-gate {
913*0Sstevel@tonic-gate 	int error = 0;
914*0Sstevel@tonic-gate 	off_t oldoffset;
915*0Sstevel@tonic-gate 	caddr_t zfodbase, oldaddr;
916*0Sstevel@tonic-gate 	size_t end, oldlen;
917*0Sstevel@tonic-gate 	size_t zfoddiff;
918*0Sstevel@tonic-gate 	label_t ljb;
919*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	oldaddr = addr;
922*0Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
923*0Sstevel@tonic-gate 	if (len) {
924*0Sstevel@tonic-gate 		oldlen = len;
925*0Sstevel@tonic-gate 		len += ((size_t)oldaddr - (size_t)addr);
926*0Sstevel@tonic-gate 		oldoffset = offset;
927*0Sstevel@tonic-gate 		offset = (off_t)((uintptr_t)offset & PAGEMASK);
928*0Sstevel@tonic-gate 		if (page) {
929*0Sstevel@tonic-gate 			spgcnt_t  prefltmem, availm, npages;
930*0Sstevel@tonic-gate 			int preread;
931*0Sstevel@tonic-gate 			uint_t mflag = MAP_PRIVATE | MAP_FIXED;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
934*0Sstevel@tonic-gate 				mflag |= MAP_TEXT;
935*0Sstevel@tonic-gate 			} else {
936*0Sstevel@tonic-gate 				mflag |= MAP_INITDATA;
937*0Sstevel@tonic-gate 			}
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
940*0Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
941*0Sstevel@tonic-gate 				error = ENOMEM;
942*0Sstevel@tonic-gate 				goto bad;
943*0Sstevel@tonic-gate 			}
944*0Sstevel@tonic-gate 			if (error = VOP_MAP(vp, (offset_t)offset,
945*0Sstevel@tonic-gate 			    p->p_as, &addr, len, prot, PROT_ALL,
946*0Sstevel@tonic-gate 			    mflag, CRED()))
947*0Sstevel@tonic-gate 				goto bad;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 			/*
950*0Sstevel@tonic-gate 			 * If the segment can fit, then we prefault
951*0Sstevel@tonic-gate 			 * the entire segment in.  This is based on the
952*0Sstevel@tonic-gate 			 * model that says the best working set of a
953*0Sstevel@tonic-gate 			 * small program is all of its pages.
954*0Sstevel@tonic-gate 			 */
955*0Sstevel@tonic-gate 			npages = (spgcnt_t)btopr(len);
956*0Sstevel@tonic-gate 			prefltmem = freemem - desfree;
957*0Sstevel@tonic-gate 			preread =
958*0Sstevel@tonic-gate 			    (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 			/*
961*0Sstevel@tonic-gate 			 * If we aren't prefaulting the segment,
962*0Sstevel@tonic-gate 			 * increment "deficit", if necessary to ensure
963*0Sstevel@tonic-gate 			 * that pages will become available when this
964*0Sstevel@tonic-gate 			 * process starts executing.
965*0Sstevel@tonic-gate 			 */
966*0Sstevel@tonic-gate 			availm = freemem - lotsfree;
967*0Sstevel@tonic-gate 			if (preread == 0 && npages > availm &&
968*0Sstevel@tonic-gate 			    deficit < lotsfree) {
969*0Sstevel@tonic-gate 				deficit += MIN((pgcnt_t)(npages - availm),
970*0Sstevel@tonic-gate 				    lotsfree - deficit);
971*0Sstevel@tonic-gate 			}
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 			if (preread) {
974*0Sstevel@tonic-gate 				TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
975*0Sstevel@tonic-gate 				    "execmap preread:freemem %d size %lu",
976*0Sstevel@tonic-gate 				    freemem, len);
977*0Sstevel@tonic-gate 				(void) as_fault(p->p_as->a_hat, p->p_as,
978*0Sstevel@tonic-gate 				    (caddr_t)addr, len, F_INVAL, S_READ);
979*0Sstevel@tonic-gate 			}
980*0Sstevel@tonic-gate 		} else {
981*0Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
982*0Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
983*0Sstevel@tonic-gate 				error = ENOMEM;
984*0Sstevel@tonic-gate 				goto bad;
985*0Sstevel@tonic-gate 			}
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 			if (error = as_map(p->p_as, addr, len,
988*0Sstevel@tonic-gate 			    segvn_create, zfod_argsp))
989*0Sstevel@tonic-gate 				goto bad;
990*0Sstevel@tonic-gate 			/*
991*0Sstevel@tonic-gate 			 * Read in the segment in one big chunk.
992*0Sstevel@tonic-gate 			 */
993*0Sstevel@tonic-gate 			if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
994*0Sstevel@tonic-gate 			    oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
995*0Sstevel@tonic-gate 			    (rlim64_t)0, CRED(), (ssize_t *)0))
996*0Sstevel@tonic-gate 				goto bad;
997*0Sstevel@tonic-gate 			/*
998*0Sstevel@tonic-gate 			 * Now set protections.
999*0Sstevel@tonic-gate 			 */
1000*0Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
1001*0Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)addr,
1002*0Sstevel@tonic-gate 				    len, prot);
1003*0Sstevel@tonic-gate 			}
1004*0Sstevel@tonic-gate 		}
1005*0Sstevel@tonic-gate 	}
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 	if (zfodlen) {
1008*0Sstevel@tonic-gate 		end = (size_t)addr + len;
1009*0Sstevel@tonic-gate 		zfodbase = (caddr_t)roundup(end, PAGESIZE);
1010*0Sstevel@tonic-gate 		zfoddiff = (uintptr_t)zfodbase - end;
1011*0Sstevel@tonic-gate 		if (zfoddiff) {
1012*0Sstevel@tonic-gate 			if (on_fault(&ljb)) {
1013*0Sstevel@tonic-gate 				no_fault();
1014*0Sstevel@tonic-gate 				error = EFAULT;
1015*0Sstevel@tonic-gate 				goto bad;
1016*0Sstevel@tonic-gate 			}
1017*0Sstevel@tonic-gate 			uzero((void *)end, zfoddiff);
1018*0Sstevel@tonic-gate 			no_fault();
1019*0Sstevel@tonic-gate 		}
1020*0Sstevel@tonic-gate 		if (zfodlen > zfoddiff) {
1021*0Sstevel@tonic-gate 			struct segvn_crargs crargs =
1022*0Sstevel@tonic-gate 			    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 			zfodlen -= zfoddiff;
1025*0Sstevel@tonic-gate 			if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
1026*0Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
1027*0Sstevel@tonic-gate 				error = ENOMEM;
1028*0Sstevel@tonic-gate 				goto bad;
1029*0Sstevel@tonic-gate 			}
1030*0Sstevel@tonic-gate 			crargs.szc = szc;
1031*0Sstevel@tonic-gate 			if (error = as_map(p->p_as, (caddr_t)zfodbase,
1032*0Sstevel@tonic-gate 			    zfodlen, segvn_create, &crargs))
1033*0Sstevel@tonic-gate 				goto bad;
1034*0Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
1035*0Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)zfodbase,
1036*0Sstevel@tonic-gate 				    zfodlen, prot);
1037*0Sstevel@tonic-gate 			}
1038*0Sstevel@tonic-gate 		}
1039*0Sstevel@tonic-gate 	}
1040*0Sstevel@tonic-gate 	return (0);
1041*0Sstevel@tonic-gate bad:
1042*0Sstevel@tonic-gate 	return (error);
1043*0Sstevel@tonic-gate }
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate void
1046*0Sstevel@tonic-gate setexecenv(struct execenv *ep)
1047*0Sstevel@tonic-gate {
1048*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1049*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1050*0Sstevel@tonic-gate 	struct vnode *vp;
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 	p->p_bssbase = ep->ex_bssbase;
1053*0Sstevel@tonic-gate 	p->p_brkbase = ep->ex_brkbase;
1054*0Sstevel@tonic-gate 	p->p_brksize = ep->ex_brksize;
1055*0Sstevel@tonic-gate 	if (p->p_exec)
1056*0Sstevel@tonic-gate 		VN_RELE(p->p_exec);	/* out with the old */
1057*0Sstevel@tonic-gate 	vp = p->p_exec = ep->ex_vp;
1058*0Sstevel@tonic-gate 	if (vp != NULL)
1059*0Sstevel@tonic-gate 		VN_HOLD(vp);		/* in with the new */
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
1062*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
1063*0Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
1064*0Sstevel@tonic-gate }
1065*0Sstevel@tonic-gate 
1066*0Sstevel@tonic-gate int
1067*0Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
1068*0Sstevel@tonic-gate {
1069*0Sstevel@tonic-gate 	struct vnode *vp = *vpp;
1070*0Sstevel@tonic-gate 	file_t *fp;
1071*0Sstevel@tonic-gate 	int error = 0;
1072*0Sstevel@tonic-gate 	int filemode = FREAD;
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	VN_HOLD(vp);		/* open reference */
1075*0Sstevel@tonic-gate 	if (error = falloc(NULL, filemode, &fp, fdp)) {
1076*0Sstevel@tonic-gate 		VN_RELE(vp);
1077*0Sstevel@tonic-gate 		*fdp = -1;	/* just in case falloc changed value */
1078*0Sstevel@tonic-gate 		return (error);
1079*0Sstevel@tonic-gate 	}
1080*0Sstevel@tonic-gate 	if (error = VOP_OPEN(&vp, filemode, CRED())) {
1081*0Sstevel@tonic-gate 		VN_RELE(vp);
1082*0Sstevel@tonic-gate 		setf(*fdp, NULL);
1083*0Sstevel@tonic-gate 		unfalloc(fp);
1084*0Sstevel@tonic-gate 		*fdp = -1;
1085*0Sstevel@tonic-gate 		return (error);
1086*0Sstevel@tonic-gate 	}
1087*0Sstevel@tonic-gate 	*vpp = vp;		/* vnode should not have changed */
1088*0Sstevel@tonic-gate 	fp->f_vnode = vp;
1089*0Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
1090*0Sstevel@tonic-gate 	setf(*fdp, fp);
1091*0Sstevel@tonic-gate 	return (0);
1092*0Sstevel@tonic-gate }
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate int
1095*0Sstevel@tonic-gate execclose(int fd)
1096*0Sstevel@tonic-gate {
1097*0Sstevel@tonic-gate 	return (closeandsetf(fd, NULL));
1098*0Sstevel@tonic-gate }
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate /*
1102*0Sstevel@tonic-gate  * noexec stub function.
1103*0Sstevel@tonic-gate  */
1104*0Sstevel@tonic-gate /*ARGSUSED*/
1105*0Sstevel@tonic-gate int
1106*0Sstevel@tonic-gate noexec(
1107*0Sstevel@tonic-gate     struct vnode *vp,
1108*0Sstevel@tonic-gate     struct execa *uap,
1109*0Sstevel@tonic-gate     struct uarg *args,
1110*0Sstevel@tonic-gate     struct intpdata *idatap,
1111*0Sstevel@tonic-gate     int level,
1112*0Sstevel@tonic-gate     long *execsz,
1113*0Sstevel@tonic-gate     int setid,
1114*0Sstevel@tonic-gate     caddr_t exec_file,
1115*0Sstevel@tonic-gate     struct cred *cred)
1116*0Sstevel@tonic-gate {
1117*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
1118*0Sstevel@tonic-gate 	return (ENOEXEC);
1119*0Sstevel@tonic-gate }
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate /*
1122*0Sstevel@tonic-gate  * Support routines for building a user stack.
1123*0Sstevel@tonic-gate  *
1124*0Sstevel@tonic-gate  * execve(path, argv, envp) must construct a new stack with the specified
1125*0Sstevel@tonic-gate  * arguments and environment variables (see exec_args() for a description
1126*0Sstevel@tonic-gate  * of the user stack layout).  To do this, we copy the arguments and
1127*0Sstevel@tonic-gate  * environment variables from the old user address space into the kernel,
1128*0Sstevel@tonic-gate  * free the old as, create the new as, and copy our buffered information
1129*0Sstevel@tonic-gate  * to the new stack.  Our kernel buffer has the following structure:
1130*0Sstevel@tonic-gate  *
1131*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
1132*0Sstevel@tonic-gate  *	| string offsets	|
1133*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
1134*0Sstevel@tonic-gate  *	|			|
1135*0Sstevel@tonic-gate  *	| STK_AVAIL() space	|
1136*0Sstevel@tonic-gate  *	|			|
1137*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
1138*0Sstevel@tonic-gate  *	| strings		|
1139*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
1140*0Sstevel@tonic-gate  *
1141*0Sstevel@tonic-gate  * When we add a string, we store the string's contents (including the null
1142*0Sstevel@tonic-gate  * terminator) at stk_strp, and we store the offset of the string relative to
1143*0Sstevel@tonic-gate  * stk_base at --stk_offp.  At strings are added, stk_strp increases and
1144*0Sstevel@tonic-gate  * stk_offp decreases.  The amount of space remaining, STK_AVAIL(), is just
1145*0Sstevel@tonic-gate  * the difference between these pointers.  If we run out of space, we return
1146*0Sstevel@tonic-gate  * an error and exec_args() starts all over again with a buffer twice as large.
1147*0Sstevel@tonic-gate  * When we're all done, the kernel buffer looks like this:
1148*0Sstevel@tonic-gate  *
1149*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
1150*0Sstevel@tonic-gate  *	| argv[0] offset	|
1151*0Sstevel@tonic-gate  *	+-----------------------+
1152*0Sstevel@tonic-gate  *	| ...			|
1153*0Sstevel@tonic-gate  *	+-----------------------+
1154*0Sstevel@tonic-gate  *	| argv[argc-1] offset	|
1155*0Sstevel@tonic-gate  *	+-----------------------+
1156*0Sstevel@tonic-gate  *	| envp[0] offset	|
1157*0Sstevel@tonic-gate  *	+-----------------------+
1158*0Sstevel@tonic-gate  *	| ...			|
1159*0Sstevel@tonic-gate  *	+-----------------------+
1160*0Sstevel@tonic-gate  *	| envp[envc-1] offset	|
1161*0Sstevel@tonic-gate  *	+-----------------------+
1162*0Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
1163*0Sstevel@tonic-gate  *	+-----------------------+
1164*0Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
1165*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
1166*0Sstevel@tonic-gate  *	|			|
1167*0Sstevel@tonic-gate  *	| STK_AVAIL() space	|
1168*0Sstevel@tonic-gate  *	|			|
1169*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
1170*0Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
1171*0Sstevel@tonic-gate  *	+-----------------------+
1172*0Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
1173*0Sstevel@tonic-gate  *	+-----------------------+
1174*0Sstevel@tonic-gate  *	| envp[envc-1] string	|
1175*0Sstevel@tonic-gate  *	+-----------------------+
1176*0Sstevel@tonic-gate  *	| ...			|
1177*0Sstevel@tonic-gate  *	+-----------------------+
1178*0Sstevel@tonic-gate  *	| envp[0] string	|
1179*0Sstevel@tonic-gate  *	+-----------------------+
1180*0Sstevel@tonic-gate  *	| argv[argc-1] string	|
1181*0Sstevel@tonic-gate  *	+-----------------------+
1182*0Sstevel@tonic-gate  *	| ...			|
1183*0Sstevel@tonic-gate  *	+-----------------------+
1184*0Sstevel@tonic-gate  *	| argv[0] string	|
1185*0Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
1186*0Sstevel@tonic-gate  */
1187*0Sstevel@tonic-gate 
1188*0Sstevel@tonic-gate #define	STK_AVAIL(args)		((char *)(args)->stk_offp - (args)->stk_strp)
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate /*
1191*0Sstevel@tonic-gate  * Add a string to the stack.
1192*0Sstevel@tonic-gate  */
1193*0Sstevel@tonic-gate static int
1194*0Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
1195*0Sstevel@tonic-gate {
1196*0Sstevel@tonic-gate 	int error;
1197*0Sstevel@tonic-gate 	size_t len;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 	if (STK_AVAIL(args) < sizeof (int))
1200*0Sstevel@tonic-gate 		return (E2BIG);
1201*0Sstevel@tonic-gate 	*--args->stk_offp = args->stk_strp - args->stk_base;
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate 	if (segflg == UIO_USERSPACE) {
1204*0Sstevel@tonic-gate 		error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
1205*0Sstevel@tonic-gate 		if (error != 0)
1206*0Sstevel@tonic-gate 			return (error);
1207*0Sstevel@tonic-gate 	} else {
1208*0Sstevel@tonic-gate 		len = strlen(sp) + 1;
1209*0Sstevel@tonic-gate 		if (len > STK_AVAIL(args))
1210*0Sstevel@tonic-gate 			return (E2BIG);
1211*0Sstevel@tonic-gate 		bcopy(sp, args->stk_strp, len);
1212*0Sstevel@tonic-gate 	}
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 	args->stk_strp += len;
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 	return (0);
1217*0Sstevel@tonic-gate }
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate static int
1220*0Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
1221*0Sstevel@tonic-gate {
1222*0Sstevel@tonic-gate 	int error;
1223*0Sstevel@tonic-gate 
1224*0Sstevel@tonic-gate 	if (args->from_model == DATAMODEL_NATIVE) {
1225*0Sstevel@tonic-gate 		ulong_t ptr;
1226*0Sstevel@tonic-gate 		error = fulword(src, &ptr);
1227*0Sstevel@tonic-gate 		*dst = (caddr_t)ptr;
1228*0Sstevel@tonic-gate 	} else {
1229*0Sstevel@tonic-gate 		uint32_t ptr;
1230*0Sstevel@tonic-gate 		error = fuword32(src, &ptr);
1231*0Sstevel@tonic-gate 		*dst = (caddr_t)(uintptr_t)ptr;
1232*0Sstevel@tonic-gate 	}
1233*0Sstevel@tonic-gate 	return (error);
1234*0Sstevel@tonic-gate }
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate static int
1237*0Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
1238*0Sstevel@tonic-gate {
1239*0Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE)
1240*0Sstevel@tonic-gate 		return (sulword(addr, (ulong_t)value));
1241*0Sstevel@tonic-gate 	else
1242*0Sstevel@tonic-gate 		return (suword32(addr, (uint32_t)(uintptr_t)value));
1243*0Sstevel@tonic-gate }
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate static int
1246*0Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
1247*0Sstevel@tonic-gate {
1248*0Sstevel@tonic-gate 	char *sp;
1249*0Sstevel@tonic-gate 	int argc, error;
1250*0Sstevel@tonic-gate 	int argv_empty = 0;
1251*0Sstevel@tonic-gate 	size_t ptrsize = args->from_ptrsize;
1252*0Sstevel@tonic-gate 	size_t size, pad;
1253*0Sstevel@tonic-gate 	char *argv = (char *)uap->argp;
1254*0Sstevel@tonic-gate 	char *envp = (char *)uap->envp;
1255*0Sstevel@tonic-gate 
1256*0Sstevel@tonic-gate 	/*
1257*0Sstevel@tonic-gate 	 * Copy interpreter's name and argument to argv[0] and argv[1].
1258*0Sstevel@tonic-gate 	 */
1259*0Sstevel@tonic-gate 	if (intp != NULL && intp->intp_name != NULL) {
1260*0Sstevel@tonic-gate 		if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
1261*0Sstevel@tonic-gate 			return (error);
1262*0Sstevel@tonic-gate 		if (intp->intp_arg != NULL &&
1263*0Sstevel@tonic-gate 		    (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
1264*0Sstevel@tonic-gate 			return (error);
1265*0Sstevel@tonic-gate 		if (args->fname != NULL)
1266*0Sstevel@tonic-gate 			error = stk_add(args, args->fname, UIO_SYSSPACE);
1267*0Sstevel@tonic-gate 		else
1268*0Sstevel@tonic-gate 			error = stk_add(args, uap->fname, UIO_USERSPACE);
1269*0Sstevel@tonic-gate 		if (error)
1270*0Sstevel@tonic-gate 			return (error);
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate 		/*
1273*0Sstevel@tonic-gate 		 * Check for an empty argv[].
1274*0Sstevel@tonic-gate 		 */
1275*0Sstevel@tonic-gate 		if (stk_getptr(args, argv, &sp))
1276*0Sstevel@tonic-gate 			return (EFAULT);
1277*0Sstevel@tonic-gate 		if (sp == NULL)
1278*0Sstevel@tonic-gate 			argv_empty = 1;
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate 		argv += ptrsize;		/* ignore original argv[0] */
1281*0Sstevel@tonic-gate 	}
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 	if (argv_empty == 0) {
1284*0Sstevel@tonic-gate 		/*
1285*0Sstevel@tonic-gate 		 * Add argv[] strings to the stack.
1286*0Sstevel@tonic-gate 		 */
1287*0Sstevel@tonic-gate 		for (;;) {
1288*0Sstevel@tonic-gate 			if (stk_getptr(args, argv, &sp))
1289*0Sstevel@tonic-gate 				return (EFAULT);
1290*0Sstevel@tonic-gate 			if (sp == NULL)
1291*0Sstevel@tonic-gate 				break;
1292*0Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
1293*0Sstevel@tonic-gate 				return (error);
1294*0Sstevel@tonic-gate 			argv += ptrsize;
1295*0Sstevel@tonic-gate 		}
1296*0Sstevel@tonic-gate 	}
1297*0Sstevel@tonic-gate 	argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
1298*0Sstevel@tonic-gate 	args->arglen = args->stk_strp - args->stk_base;
1299*0Sstevel@tonic-gate 
1300*0Sstevel@tonic-gate 	/*
1301*0Sstevel@tonic-gate 	 * Add environ[] strings to the stack.
1302*0Sstevel@tonic-gate 	 */
1303*0Sstevel@tonic-gate 	if (envp != NULL) {
1304*0Sstevel@tonic-gate 		for (;;) {
1305*0Sstevel@tonic-gate 			if (stk_getptr(args, envp, &sp))
1306*0Sstevel@tonic-gate 				return (EFAULT);
1307*0Sstevel@tonic-gate 			if (sp == NULL)
1308*0Sstevel@tonic-gate 				break;
1309*0Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
1310*0Sstevel@tonic-gate 				return (error);
1311*0Sstevel@tonic-gate 			envp += ptrsize;
1312*0Sstevel@tonic-gate 		}
1313*0Sstevel@tonic-gate 	}
1314*0Sstevel@tonic-gate 	args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
1315*0Sstevel@tonic-gate 	args->ne = args->na - argc;
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate 	/*
1318*0Sstevel@tonic-gate 	 * Add AT_SUN_PLATFORM and AT_SUN_EXECNAME strings to the stack.
1319*0Sstevel@tonic-gate 	 */
1320*0Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
1321*0Sstevel@tonic-gate 		if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
1322*0Sstevel@tonic-gate 			return (error);
1323*0Sstevel@tonic-gate 		if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
1324*0Sstevel@tonic-gate 			return (error);
1325*0Sstevel@tonic-gate 	}
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate 	/*
1328*0Sstevel@tonic-gate 	 * Compute the size of the stack.  This includes all the pointers,
1329*0Sstevel@tonic-gate 	 * the space reserved for the aux vector, and all the strings.
1330*0Sstevel@tonic-gate 	 * The total number of pointers is args->na (which is argc + envc)
1331*0Sstevel@tonic-gate 	 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
1332*0Sstevel@tonic-gate 	 * after the last argument (i.e. argv[argc]); (3) the NULL after the
1333*0Sstevel@tonic-gate 	 * last environment variable (i.e. envp[envc]); and (4) the NULL after
1334*0Sstevel@tonic-gate 	 * all the strings, at the very top of the stack.
1335*0Sstevel@tonic-gate 	 */
1336*0Sstevel@tonic-gate 	size = (args->na + 4) * args->to_ptrsize + args->auxsize +
1337*0Sstevel@tonic-gate 	    (args->stk_strp - args->stk_base);
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate 	/*
1340*0Sstevel@tonic-gate 	 * Pad the string section with zeroes to align the stack size.
1341*0Sstevel@tonic-gate 	 */
1342*0Sstevel@tonic-gate 	pad = P2NPHASE(size, args->stk_align);
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 	if (STK_AVAIL(args) < pad)
1345*0Sstevel@tonic-gate 		return (E2BIG);
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 	args->usrstack_size = size + pad;
1348*0Sstevel@tonic-gate 
1349*0Sstevel@tonic-gate 	while (pad-- != 0)
1350*0Sstevel@tonic-gate 		*args->stk_strp++ = 0;
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 	args->nc = args->stk_strp - args->stk_base;
1353*0Sstevel@tonic-gate 
1354*0Sstevel@tonic-gate 	return (0);
1355*0Sstevel@tonic-gate }
1356*0Sstevel@tonic-gate 
1357*0Sstevel@tonic-gate static int
1358*0Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
1359*0Sstevel@tonic-gate {
1360*0Sstevel@tonic-gate 	size_t ptrsize = args->to_ptrsize;
1361*0Sstevel@tonic-gate 	ssize_t pslen;
1362*0Sstevel@tonic-gate 	char *kstrp = args->stk_base;
1363*0Sstevel@tonic-gate 	char *ustrp = usrstack - args->nc - ptrsize;
1364*0Sstevel@tonic-gate 	char *usp = usrstack - args->usrstack_size;
1365*0Sstevel@tonic-gate 	int *offp = (int *)(args->stk_base + args->stk_size);
1366*0Sstevel@tonic-gate 	int envc = args->ne;
1367*0Sstevel@tonic-gate 	int argc = args->na - envc;
1368*0Sstevel@tonic-gate 	int i;
1369*0Sstevel@tonic-gate 
1370*0Sstevel@tonic-gate 	/*
1371*0Sstevel@tonic-gate 	 * Record argc for /proc.
1372*0Sstevel@tonic-gate 	 */
1373*0Sstevel@tonic-gate 	up->u_argc = argc;
1374*0Sstevel@tonic-gate 
1375*0Sstevel@tonic-gate 	/*
1376*0Sstevel@tonic-gate 	 * Put argc on the stack.  Note that even though it's an int,
1377*0Sstevel@tonic-gate 	 * it always consumes ptrsize bytes (for alignment).
1378*0Sstevel@tonic-gate 	 */
1379*0Sstevel@tonic-gate 	if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
1380*0Sstevel@tonic-gate 		return (-1);
1381*0Sstevel@tonic-gate 
1382*0Sstevel@tonic-gate 	/*
1383*0Sstevel@tonic-gate 	 * Add argc space (ptrsize) to usp and record argv for /proc.
1384*0Sstevel@tonic-gate 	 */
1385*0Sstevel@tonic-gate 	up->u_argv = (uintptr_t)(usp += ptrsize);
1386*0Sstevel@tonic-gate 
1387*0Sstevel@tonic-gate 	/*
1388*0Sstevel@tonic-gate 	 * Put the argv[] pointers on the stack.
1389*0Sstevel@tonic-gate 	 */
1390*0Sstevel@tonic-gate 	for (i = 0; i < argc; i++, usp += ptrsize)
1391*0Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
1392*0Sstevel@tonic-gate 			return (-1);
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 	/*
1395*0Sstevel@tonic-gate 	 * Copy arguments to u_psargs.
1396*0Sstevel@tonic-gate 	 */
1397*0Sstevel@tonic-gate 	pslen = MIN(args->arglen, PSARGSZ) - 1;
1398*0Sstevel@tonic-gate 	for (i = 0; i < pslen; i++)
1399*0Sstevel@tonic-gate 		up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
1400*0Sstevel@tonic-gate 	while (i < PSARGSZ)
1401*0Sstevel@tonic-gate 		up->u_psargs[i++] = '\0';
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate 	/*
1404*0Sstevel@tonic-gate 	 * Add space for argv[]'s NULL terminator (ptrsize) to usp and
1405*0Sstevel@tonic-gate 	 * record envp for /proc.
1406*0Sstevel@tonic-gate 	 */
1407*0Sstevel@tonic-gate 	up->u_envp = (uintptr_t)(usp += ptrsize);
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 	/*
1410*0Sstevel@tonic-gate 	 * Put the envp[] pointers on the stack.
1411*0Sstevel@tonic-gate 	 */
1412*0Sstevel@tonic-gate 	for (i = 0; i < envc; i++, usp += ptrsize)
1413*0Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
1414*0Sstevel@tonic-gate 			return (-1);
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate 	/*
1417*0Sstevel@tonic-gate 	 * Add space for envp[]'s NULL terminator (ptrsize) to usp and
1418*0Sstevel@tonic-gate 	 * remember where the stack ends, which is also where auxv begins.
1419*0Sstevel@tonic-gate 	 */
1420*0Sstevel@tonic-gate 	args->stackend = usp += ptrsize;
1421*0Sstevel@tonic-gate 
1422*0Sstevel@tonic-gate 	/*
1423*0Sstevel@tonic-gate 	 * Put all the argv[], envp[], and auxv strings on the stack.
1424*0Sstevel@tonic-gate 	 */
1425*0Sstevel@tonic-gate 	if (copyout(args->stk_base, ustrp, args->nc))
1426*0Sstevel@tonic-gate 		return (-1);
1427*0Sstevel@tonic-gate 
1428*0Sstevel@tonic-gate 	/*
1429*0Sstevel@tonic-gate 	 * Fill in the aux vector now that we know the user stack addresses
1430*0Sstevel@tonic-gate 	 * for the AT_SUN_PLATFORM and AT_SUN_EXECNAME strings.
1431*0Sstevel@tonic-gate 	 */
1432*0Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
1433*0Sstevel@tonic-gate 		if (args->to_model == DATAMODEL_NATIVE) {
1434*0Sstevel@tonic-gate 			auxv_t **a = (auxv_t **)auxvpp;
1435*0Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
1436*0Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
1437*0Sstevel@tonic-gate 		} else {
1438*0Sstevel@tonic-gate 			auxv32_t **a = (auxv32_t **)auxvpp;
1439*0Sstevel@tonic-gate 			ADDAUX(*a,
1440*0Sstevel@tonic-gate 			    AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
1441*0Sstevel@tonic-gate 			ADDAUX(*a,
1442*0Sstevel@tonic-gate 			    AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]);
1443*0Sstevel@tonic-gate 		}
1444*0Sstevel@tonic-gate 	}
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 	return (0);
1447*0Sstevel@tonic-gate }
1448*0Sstevel@tonic-gate 
1449*0Sstevel@tonic-gate #ifdef DEBUG
1450*0Sstevel@tonic-gate int mpss_brkpgszsel = 0;
1451*0Sstevel@tonic-gate int mpss_stkpgszsel = 0;
1452*0Sstevel@tonic-gate #endif
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate /*
1455*0Sstevel@tonic-gate  * Initialize a new user stack with the specified arguments and environment.
1456*0Sstevel@tonic-gate  * The initial user stack layout is as follows:
1457*0Sstevel@tonic-gate  *
1458*0Sstevel@tonic-gate  *	User Stack
1459*0Sstevel@tonic-gate  *	+---------------+ <--- curproc->p_usrstack
1460*0Sstevel@tonic-gate  *	| NULL		|
1461*0Sstevel@tonic-gate  *	+---------------+
1462*0Sstevel@tonic-gate  *	|		|
1463*0Sstevel@tonic-gate  *	| auxv strings	|
1464*0Sstevel@tonic-gate  *	|		|
1465*0Sstevel@tonic-gate  *	+---------------+
1466*0Sstevel@tonic-gate  *	|		|
1467*0Sstevel@tonic-gate  *	| envp strings	|
1468*0Sstevel@tonic-gate  *	|		|
1469*0Sstevel@tonic-gate  *	+---------------+
1470*0Sstevel@tonic-gate  *	|		|
1471*0Sstevel@tonic-gate  *	| argv strings	|
1472*0Sstevel@tonic-gate  *	|		|
1473*0Sstevel@tonic-gate  *	+---------------+ <--- ustrp
1474*0Sstevel@tonic-gate  *	|		|
1475*0Sstevel@tonic-gate  *	| aux vector	|
1476*0Sstevel@tonic-gate  *	|		|
1477*0Sstevel@tonic-gate  *	+---------------+ <--- auxv
1478*0Sstevel@tonic-gate  *	| NULL		|
1479*0Sstevel@tonic-gate  *	+---------------+
1480*0Sstevel@tonic-gate  *	| envp[envc-1]	|
1481*0Sstevel@tonic-gate  *	+---------------+
1482*0Sstevel@tonic-gate  *	| ...		|
1483*0Sstevel@tonic-gate  *	+---------------+
1484*0Sstevel@tonic-gate  *	| envp[0]	|
1485*0Sstevel@tonic-gate  *	+---------------+ <--- envp[]
1486*0Sstevel@tonic-gate  *	| NULL		|
1487*0Sstevel@tonic-gate  *	+---------------+
1488*0Sstevel@tonic-gate  *	| argv[argc-1]	|
1489*0Sstevel@tonic-gate  *	+---------------+
1490*0Sstevel@tonic-gate  *	| ...		|
1491*0Sstevel@tonic-gate  *	+---------------+
1492*0Sstevel@tonic-gate  *	| argv[0]	|
1493*0Sstevel@tonic-gate  *	+---------------+ <--- argv[]
1494*0Sstevel@tonic-gate  *	| argc		|
1495*0Sstevel@tonic-gate  *	+---------------+ <--- stack base
1496*0Sstevel@tonic-gate  */
1497*0Sstevel@tonic-gate int
1498*0Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
1499*0Sstevel@tonic-gate {
1500*0Sstevel@tonic-gate 	size_t size;
1501*0Sstevel@tonic-gate 	int error;
1502*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1503*0Sstevel@tonic-gate 	user_t *up = PTOU(p);
1504*0Sstevel@tonic-gate 	char *usrstack;
1505*0Sstevel@tonic-gate 	rctl_entity_p_t e;
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate 	struct as *as;
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 	args->from_model = p->p_model;
1510*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
1511*0Sstevel@tonic-gate 		args->from_ptrsize = sizeof (long);
1512*0Sstevel@tonic-gate 	} else {
1513*0Sstevel@tonic-gate 		args->from_ptrsize = sizeof (int32_t);
1514*0Sstevel@tonic-gate 	}
1515*0Sstevel@tonic-gate 
1516*0Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE) {
1517*0Sstevel@tonic-gate 		args->to_ptrsize = sizeof (long);
1518*0Sstevel@tonic-gate 		args->ncargs = NCARGS;
1519*0Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN;
1520*0Sstevel@tonic-gate 		usrstack = (char *)USRSTACK;
1521*0Sstevel@tonic-gate 	} else {
1522*0Sstevel@tonic-gate 		args->to_ptrsize = sizeof (int32_t);
1523*0Sstevel@tonic-gate 		args->ncargs = NCARGS32;
1524*0Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN32;
1525*0Sstevel@tonic-gate 		usrstack = (char *)USRSTACK32;
1526*0Sstevel@tonic-gate 	}
1527*0Sstevel@tonic-gate 
1528*0Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate #if defined(__sparc)
1531*0Sstevel@tonic-gate 	/*
1532*0Sstevel@tonic-gate 	 * Make sure user register windows are empty before
1533*0Sstevel@tonic-gate 	 * attempting to make a new stack.
1534*0Sstevel@tonic-gate 	 */
1535*0Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
1536*0Sstevel@tonic-gate #endif
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 	for (size = PAGESIZE; ; size *= 2) {
1539*0Sstevel@tonic-gate 		args->stk_size = size;
1540*0Sstevel@tonic-gate 		args->stk_base = kmem_alloc(size, KM_SLEEP);
1541*0Sstevel@tonic-gate 		args->stk_strp = args->stk_base;
1542*0Sstevel@tonic-gate 		args->stk_offp = (int *)(args->stk_base + size);
1543*0Sstevel@tonic-gate 		error = stk_copyin(uap, args, intp, auxvpp);
1544*0Sstevel@tonic-gate 		if (error == 0)
1545*0Sstevel@tonic-gate 			break;
1546*0Sstevel@tonic-gate 		kmem_free(args->stk_base, size);
1547*0Sstevel@tonic-gate 		if (error != E2BIG && error != ENAMETOOLONG)
1548*0Sstevel@tonic-gate 			return (error);
1549*0Sstevel@tonic-gate 		if (size >= args->ncargs)
1550*0Sstevel@tonic-gate 			return (E2BIG);
1551*0Sstevel@tonic-gate 	}
1552*0Sstevel@tonic-gate 
1553*0Sstevel@tonic-gate 	size = args->usrstack_size;
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	ASSERT(error == 0);
1556*0Sstevel@tonic-gate 	ASSERT(P2PHASE(size, args->stk_align) == 0);
1557*0Sstevel@tonic-gate 	ASSERT((ssize_t)STK_AVAIL(args) >= 0);
1558*0Sstevel@tonic-gate 
1559*0Sstevel@tonic-gate 	if (size > args->ncargs) {
1560*0Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
1561*0Sstevel@tonic-gate 		return (E2BIG);
1562*0Sstevel@tonic-gate 	}
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 	/*
1565*0Sstevel@tonic-gate 	 * Leave only the current lwp and force the other lwps to exit.
1566*0Sstevel@tonic-gate 	 * If another lwp beat us to the punch by calling exit(), bail out.
1567*0Sstevel@tonic-gate 	 */
1568*0Sstevel@tonic-gate 	if ((error = exitlwps(0)) != 0) {
1569*0Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
1570*0Sstevel@tonic-gate 		return (error);
1571*0Sstevel@tonic-gate 	}
1572*0Sstevel@tonic-gate 
1573*0Sstevel@tonic-gate 	/*
1574*0Sstevel@tonic-gate 	 * Revoke any doors created by the process.
1575*0Sstevel@tonic-gate 	 */
1576*0Sstevel@tonic-gate 	if (p->p_door_list)
1577*0Sstevel@tonic-gate 		door_exit();
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	/*
1580*0Sstevel@tonic-gate 	 * Release schedctl data structures.
1581*0Sstevel@tonic-gate 	 */
1582*0Sstevel@tonic-gate 	if (p->p_pagep)
1583*0Sstevel@tonic-gate 		schedctl_proc_cleanup();
1584*0Sstevel@tonic-gate 
1585*0Sstevel@tonic-gate 	/*
1586*0Sstevel@tonic-gate 	 * Clean up any DTrace helpers for the process.
1587*0Sstevel@tonic-gate 	 */
1588*0Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
1589*0Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
1590*0Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
1591*0Sstevel@tonic-gate 	}
1592*0Sstevel@tonic-gate 
1593*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1594*0Sstevel@tonic-gate 	/*
1595*0Sstevel@tonic-gate 	 * Cleanup the DTrace provider associated with this process.
1596*0Sstevel@tonic-gate 	 */
1597*0Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
1598*0Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exec_ptr != NULL);
1599*0Sstevel@tonic-gate 		dtrace_fasttrap_exec_ptr(p);
1600*0Sstevel@tonic-gate 	}
1601*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1602*0Sstevel@tonic-gate 
1603*0Sstevel@tonic-gate 	/*
1604*0Sstevel@tonic-gate 	 * discard the lwpchan cache.
1605*0Sstevel@tonic-gate 	 */
1606*0Sstevel@tonic-gate 	if (p->p_lcp != NULL)
1607*0Sstevel@tonic-gate 		lwpchan_destroy_cache(1);
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate 	/*
1610*0Sstevel@tonic-gate 	 * Delete the POSIX timers.
1611*0Sstevel@tonic-gate 	 */
1612*0Sstevel@tonic-gate 	if (p->p_itimer != NULL)
1613*0Sstevel@tonic-gate 		timer_exit();
1614*0Sstevel@tonic-gate 
1615*0Sstevel@tonic-gate #ifdef C2_AUDIT
1616*0Sstevel@tonic-gate 	if (audit_active)
1617*0Sstevel@tonic-gate 		audit_exec(args->stk_base, args->stk_base + args->arglen,
1618*0Sstevel@tonic-gate 		    args->na - args->ne, args->ne);
1619*0Sstevel@tonic-gate #endif
1620*0Sstevel@tonic-gate 
1621*0Sstevel@tonic-gate 	/*
1622*0Sstevel@tonic-gate 	 * Ensure that we don't change resource associations while we
1623*0Sstevel@tonic-gate 	 * change address spaces.
1624*0Sstevel@tonic-gate 	 */
1625*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1626*0Sstevel@tonic-gate 	pool_barrier_enter();
1627*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1628*0Sstevel@tonic-gate 
1629*0Sstevel@tonic-gate 	/*
1630*0Sstevel@tonic-gate 	 * Destroy the old address space and create a new one.
1631*0Sstevel@tonic-gate 	 * From here on, any errors are fatal to the exec()ing process.
1632*0Sstevel@tonic-gate 	 * On error we return -1, which means the caller must SIGKILL
1633*0Sstevel@tonic-gate 	 * the process.
1634*0Sstevel@tonic-gate 	 */
1635*0Sstevel@tonic-gate 	relvm();
1636*0Sstevel@tonic-gate 
1637*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1638*0Sstevel@tonic-gate 	pool_barrier_exit();
1639*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1640*0Sstevel@tonic-gate 
1641*0Sstevel@tonic-gate 	up->u_execsw = args->execswp;
1642*0Sstevel@tonic-gate 
1643*0Sstevel@tonic-gate 	p->p_brkbase = NULL;
1644*0Sstevel@tonic-gate 	p->p_brksize = 0;
1645*0Sstevel@tonic-gate 	p->p_stksize = 0;
1646*0Sstevel@tonic-gate 	p->p_model = args->to_model;
1647*0Sstevel@tonic-gate 	p->p_usrstack = usrstack;
1648*0Sstevel@tonic-gate 	p->p_stkprot = args->stk_prot;
1649*0Sstevel@tonic-gate 	p->p_datprot = args->dat_prot;
1650*0Sstevel@tonic-gate 
1651*0Sstevel@tonic-gate 	/*
1652*0Sstevel@tonic-gate 	 * Reset resource controls such that all controls are again active as
1653*0Sstevel@tonic-gate 	 * well as appropriate to the potentially new address model for the
1654*0Sstevel@tonic-gate 	 * process.
1655*0Sstevel@tonic-gate 	 */
1656*0Sstevel@tonic-gate 	e.rcep_p.proc = p;
1657*0Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
1658*0Sstevel@tonic-gate 	rctl_set_reset(p->p_rctls, p, &e);
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate 	if (exec_lpg_disable == 0) {
1661*0Sstevel@tonic-gate #ifdef DEBUG
1662*0Sstevel@tonic-gate 		uint_t pgsizes = page_num_pagesizes();
1663*0Sstevel@tonic-gate 		uint_t szc;
1664*0Sstevel@tonic-gate #endif
1665*0Sstevel@tonic-gate 		p->p_brkpageszc = args->brkpageszc;
1666*0Sstevel@tonic-gate 		p->p_stkpageszc = args->stkpageszc;
1667*0Sstevel@tonic-gate 
1668*0Sstevel@tonic-gate 		if (p->p_brkpageszc == 0) {
1669*0Sstevel@tonic-gate 			p->p_brkpageszc = page_szc(map_pgsz(MAPPGSZ_HEAP,
1670*0Sstevel@tonic-gate 			    p, 0, 0, NULL));
1671*0Sstevel@tonic-gate 		}
1672*0Sstevel@tonic-gate 		if (p->p_stkpageszc == 0) {
1673*0Sstevel@tonic-gate 			p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK,
1674*0Sstevel@tonic-gate 			    p, 0, 0, NULL));
1675*0Sstevel@tonic-gate 		}
1676*0Sstevel@tonic-gate 
1677*0Sstevel@tonic-gate #ifdef DEBUG
1678*0Sstevel@tonic-gate 		if (mpss_brkpgszsel != 0) {
1679*0Sstevel@tonic-gate 			if (mpss_brkpgszsel == -1) {
1680*0Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 8) % pgsizes;
1681*0Sstevel@tonic-gate 			} else {
1682*0Sstevel@tonic-gate 				szc = mpss_brkpgszsel % pgsizes;
1683*0Sstevel@tonic-gate 			}
1684*0Sstevel@tonic-gate 			p->p_brkpageszc = szc;
1685*0Sstevel@tonic-gate 		}
1686*0Sstevel@tonic-gate 
1687*0Sstevel@tonic-gate 		if (mpss_stkpgszsel != 0) {
1688*0Sstevel@tonic-gate 			if (mpss_stkpgszsel == -1) {
1689*0Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 7) % pgsizes;
1690*0Sstevel@tonic-gate 			} else {
1691*0Sstevel@tonic-gate 				szc = mpss_stkpgszsel % pgsizes;
1692*0Sstevel@tonic-gate 			}
1693*0Sstevel@tonic-gate 			p->p_stkpageszc = szc;
1694*0Sstevel@tonic-gate 		}
1695*0Sstevel@tonic-gate 
1696*0Sstevel@tonic-gate #endif
1697*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1698*0Sstevel@tonic-gate 		p->p_flag |= SAUTOLPG;	/* kernel controls page sizes */
1699*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1700*0Sstevel@tonic-gate 
1701*0Sstevel@tonic-gate 	} else {
1702*0Sstevel@tonic-gate 		p->p_brkpageszc = 0;
1703*0Sstevel@tonic-gate 		p->p_stkpageszc = 0;
1704*0Sstevel@tonic-gate 	}
1705*0Sstevel@tonic-gate 
1706*0Sstevel@tonic-gate 	exec_set_sp(size);
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 	as = as_alloc();
1709*0Sstevel@tonic-gate 	p->p_as = as;
1710*0Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
1711*0Sstevel@tonic-gate 		as->a_userlimit = (caddr_t)USERLIMIT32;
1712*0Sstevel@tonic-gate 	(void) hat_setup(as->a_hat, HAT_ALLOC);
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 	/*
1715*0Sstevel@tonic-gate 	 * Finally, write out the contents of the new stack.
1716*0Sstevel@tonic-gate 	 */
1717*0Sstevel@tonic-gate 	error = stk_copyout(args, usrstack, auxvpp, up);
1718*0Sstevel@tonic-gate 	kmem_free(args->stk_base, args->stk_size);
1719*0Sstevel@tonic-gate 	return (error);
1720*0Sstevel@tonic-gate }
1721