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