xref: /onnv-gate/usr/src/uts/common/os/exec.c (revision 1335:99d6f0945b8f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51043Scasper  * Common Development and Distribution License (the "License").
61043Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/cred_impl.h>
380Sstevel@tonic-gate #include <sys/policy.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/errno.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/vfs.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/mman.h>
450Sstevel@tonic-gate #include <sys/acct.h>
460Sstevel@tonic-gate #include <sys/cpuvar.h>
470Sstevel@tonic-gate #include <sys/proc.h>
480Sstevel@tonic-gate #include <sys/cmn_err.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/pathname.h>
510Sstevel@tonic-gate #include <sys/vm.h>
520Sstevel@tonic-gate #include <sys/vtrace.h>
530Sstevel@tonic-gate #include <sys/exec.h>
540Sstevel@tonic-gate #include <sys/exechdr.h>
550Sstevel@tonic-gate #include <sys/kmem.h>
560Sstevel@tonic-gate #include <sys/prsystm.h>
570Sstevel@tonic-gate #include <sys/modctl.h>
580Sstevel@tonic-gate #include <sys/vmparam.h>
590Sstevel@tonic-gate #include <sys/schedctl.h>
600Sstevel@tonic-gate #include <sys/utrap.h>
610Sstevel@tonic-gate #include <sys/systeminfo.h>
620Sstevel@tonic-gate #include <sys/stack.h>
630Sstevel@tonic-gate #include <sys/rctl.h>
640Sstevel@tonic-gate #include <sys/dtrace.h>
650Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
660Sstevel@tonic-gate #include <sys/pool.h>
670Sstevel@tonic-gate #include <sys/sdt.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <c2/audit.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <vm/hat.h>
720Sstevel@tonic-gate #include <vm/anon.h>
730Sstevel@tonic-gate #include <vm/as.h>
740Sstevel@tonic-gate #include <vm/seg.h>
750Sstevel@tonic-gate #include <vm/seg_vn.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	PRIV_RESET		0x01	/* needs to reset privs */
780Sstevel@tonic-gate #define	PRIV_SETID		0x02	/* needs to change uids */
790Sstevel@tonic-gate #define	PRIV_SETUGID		0x04	/* is setuid/setgid/forced privs */
800Sstevel@tonic-gate #define	PRIV_INCREASE		0x08	/* child runs with more privs */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *);
830Sstevel@tonic-gate static int hold_execsw(struct execsw *);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate uint_t auxv_hwcap = 0;	/* auxv AT_SUN_HWCAP value; determined on the fly */
860Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
870Sstevel@tonic-gate uint_t auxv_hwcap32 = 0;	/* 32-bit version of auxv_hwcap */
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int exec_lpg_disable = 0;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	PSUIDFLAGS		(SNOCD|SUGID)
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * exec() - wrapper around exece providing NULL environment pointer
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
980Sstevel@tonic-gate exec(const char *fname, const char **argp)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	return (exece(fname, argp, NULL));
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * exece() - system call wrapper around exec_common()
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate int
1070Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	int error;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	error = exec_common(fname, argp, envp);
1120Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int
1160Sstevel@tonic-gate exec_common(const char *fname, const char **argp, const char **envp)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
1190Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1200Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1210Sstevel@tonic-gate 	struct user *up = PTOU(p);
1220Sstevel@tonic-gate 	long execsz;		/* temporary count of exec size */
1230Sstevel@tonic-gate 	int i;
1240Sstevel@tonic-gate 	int error;
1250Sstevel@tonic-gate 	char exec_file[MAXCOMLEN+1];
1260Sstevel@tonic-gate 	struct pathname pn;
1270Sstevel@tonic-gate 	struct pathname resolvepn;
1280Sstevel@tonic-gate 	struct uarg args;
1290Sstevel@tonic-gate 	struct execa ua;
1300Sstevel@tonic-gate 	k_sigset_t savedmask;
1310Sstevel@tonic-gate 	lwpdir_t *lwpdir = NULL;
1320Sstevel@tonic-gate 	lwpdir_t **tidhash;
1330Sstevel@tonic-gate 	lwpdir_t *old_lwpdir = NULL;
1340Sstevel@tonic-gate 	uint_t old_lwpdir_sz;
1350Sstevel@tonic-gate 	lwpdir_t **old_tidhash;
1360Sstevel@tonic-gate 	uint_t old_tidhash_sz;
1370Sstevel@tonic-gate 	lwpent_t *lep;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/*
1400Sstevel@tonic-gate 	 * exec() is not supported for the /proc agent lwp.
1410Sstevel@tonic-gate 	 */
1420Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
1430Sstevel@tonic-gate 		return (ENOTSUP);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if ((error = secpolicy_basic_exec(CRED())) != 0)
1460Sstevel@tonic-gate 		return (error);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Inform /proc that an exec() has started.
1500Sstevel@tonic-gate 	 * Hold signals that are ignored by default so that we will
1510Sstevel@tonic-gate 	 * not be interrupted by a signal that will be ignored after
1520Sstevel@tonic-gate 	 * successful completion of gexec().
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1550Sstevel@tonic-gate 	prexecstart();
1560Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
1570Sstevel@tonic-gate 	savedmask = curthread->t_hold;
1580Sstevel@tonic-gate 	sigorset(&curthread->t_hold, &ignoredefault);
1590Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * Look up path name and remember last component for later.
1630Sstevel@tonic-gate 	 * To help coreadm expand its %d token, we attempt to save
1640Sstevel@tonic-gate 	 * the directory containing the executable in p_execdir. The
1650Sstevel@tonic-gate 	 * first call to lookuppn() may fail and return EINVAL because
1660Sstevel@tonic-gate 	 * dirvpp is non-NULL. In that case, we make a second call to
1670Sstevel@tonic-gate 	 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
1680Sstevel@tonic-gate 	 * but coreadm is allowed to expand %d to the empty string and
1690Sstevel@tonic-gate 	 * there are other cases in which that failure may occur.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1720Sstevel@tonic-gate 		goto out;
1730Sstevel@tonic-gate 	pn_alloc(&resolvepn);
1740Sstevel@tonic-gate 	if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
1750Sstevel@tonic-gate 		pn_free(&resolvepn);
1760Sstevel@tonic-gate 		pn_free(&pn);
1770Sstevel@tonic-gate 		if (error != EINVAL)
1780Sstevel@tonic-gate 			goto out;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		dir = NULL;
1810Sstevel@tonic-gate 		if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1820Sstevel@tonic-gate 			goto out;
1830Sstevel@tonic-gate 		pn_alloc(&resolvepn);
1840Sstevel@tonic-gate 		if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
1850Sstevel@tonic-gate 		    &vp)) != 0) {
1860Sstevel@tonic-gate 			pn_free(&resolvepn);
1870Sstevel@tonic-gate 			pn_free(&pn);
1880Sstevel@tonic-gate 			goto out;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	if (vp == NULL) {
1920Sstevel@tonic-gate 		if (dir != NULL)
1930Sstevel@tonic-gate 			VN_RELE(dir);
1940Sstevel@tonic-gate 		error = ENOENT;
1950Sstevel@tonic-gate 		pn_free(&resolvepn);
1960Sstevel@tonic-gate 		pn_free(&pn);
1970Sstevel@tonic-gate 		goto out;
1980Sstevel@tonic-gate 	}
1991043Scasper 
2001043Scasper 	/*
2011043Scasper 	 * We do not allow executing files in attribute directories.
2021043Scasper 	 * We test this by determining whether the resolved path
2031043Scasper 	 * contains a "/" when we're in an attribute directory;
2041043Scasper 	 * only if the pathname does not contain a "/" the resolved path
2051043Scasper 	 * points to a file in the current working (attribute) directory.
2061043Scasper 	 */
2071043Scasper 	if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
2081043Scasper 	    strchr(resolvepn.pn_path, '/') == NULL) {
2091043Scasper 		if (dir != NULL)
2101043Scasper 			VN_RELE(dir);
2111043Scasper 		error = EACCES;
2121043Scasper 		pn_free(&resolvepn);
2131043Scasper 		pn_free(&pn);
2141043Scasper 		VN_RELE(vp);
2151043Scasper 		goto out;
2161043Scasper 	}
2171043Scasper 
2180Sstevel@tonic-gate 	bzero(exec_file, MAXCOMLEN+1);
2190Sstevel@tonic-gate 	(void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
2200Sstevel@tonic-gate 	bzero(&args, sizeof (args));
2210Sstevel@tonic-gate 	args.pathname = resolvepn.pn_path;
2220Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
2230Sstevel@tonic-gate 	pn_free(&pn);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * Specific exec handlers, or policies determined via
2270Sstevel@tonic-gate 	 * /etc/system may override the historical default.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	args.stk_prot = PROT_ZFOD;
2300Sstevel@tonic-gate 	args.dat_prot = PROT_ZFOD;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	CPU_STATS_ADD_K(sys, sysexec, 1);
2330Sstevel@tonic-gate 	DTRACE_PROC1(exec, char *, args.pathname);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	ua.fname = fname;
2360Sstevel@tonic-gate 	ua.argp = argp;
2370Sstevel@tonic-gate 	ua.envp = envp;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
2400Sstevel@tonic-gate 	    exec_file, p->p_cred)) != 0) {
2410Sstevel@tonic-gate 		VN_RELE(vp);
2420Sstevel@tonic-gate 		if (dir != NULL)
2430Sstevel@tonic-gate 			VN_RELE(dir);
2440Sstevel@tonic-gate 		pn_free(&resolvepn);
2450Sstevel@tonic-gate 		goto fail;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * Free floating point registers (sun4u only)
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	ASSERT(lwp != NULL);
2520Sstevel@tonic-gate 	lwp_freeregs(lwp, 1);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2551217Srab 	 * Free thread and process context ops.
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	if (curthread->t_ctx)
2580Sstevel@tonic-gate 		freectx(curthread, 1);
2591217Srab 	if (p->p_pctx)
2601217Srab 		freepctx(p, 1);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Remember file name for accounting; clear any cached DTrace predicate.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	up->u_acflag &= ~AFORK;
2660Sstevel@tonic-gate 	bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
2670Sstevel@tonic-gate 	curthread->t_predcache = NULL;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * Clear contract template state
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Save the directory in which we found the executable for expanding
2760Sstevel@tonic-gate 	 * the %d token used in core file patterns.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2790Sstevel@tonic-gate 	tmpvp = p->p_execdir;
2800Sstevel@tonic-gate 	p->p_execdir = dir;
2810Sstevel@tonic-gate 	if (p->p_execdir != NULL)
2820Sstevel@tonic-gate 		VN_HOLD(p->p_execdir);
2830Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (tmpvp != NULL)
2860Sstevel@tonic-gate 		VN_RELE(tmpvp);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * Reset stack state to the user stack, clear set of signals
2900Sstevel@tonic-gate 	 * caught on the signal stack, and reset list of signals that
2910Sstevel@tonic-gate 	 * restart system calls; the new program's environment should
2920Sstevel@tonic-gate 	 * not be affected by detritus from the old program.  Any
2930Sstevel@tonic-gate 	 * pending held signals remain held, so don't clear t_hold.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2960Sstevel@tonic-gate 	lwp->lwp_oldcontext = 0;
2970Sstevel@tonic-gate 	lwp->lwp_ustack = 0;
2980Sstevel@tonic-gate 	lwp->lwp_old_stk_ctl = 0;
2990Sstevel@tonic-gate 	sigemptyset(&up->u_signodefer);
3000Sstevel@tonic-gate 	sigemptyset(&up->u_sigonstack);
3010Sstevel@tonic-gate 	sigemptyset(&up->u_sigresethand);
3020Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
3030Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
3040Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Make saved resource limit == current resource limit.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	for (i = 0; i < RLIM_NLIMITS; i++) {
3100Sstevel@tonic-gate 		/*CONSTCOND*/
3110Sstevel@tonic-gate 		if (RLIM_SAVED(i)) {
3120Sstevel@tonic-gate 			(void) rctl_rlimit_get(rctlproc_legacy[i], p,
3130Sstevel@tonic-gate 			    &up->u_saved_rlimit[i]);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/*
3180Sstevel@tonic-gate 	 * If the action was to catch the signal, then the action
3190Sstevel@tonic-gate 	 * must be reset to SIG_DFL.
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	sigdefault(p);
3220Sstevel@tonic-gate 	p->p_flag &= ~(SNOWAIT|SJCTL);
3230Sstevel@tonic-gate 	p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
3240Sstevel@tonic-gate 	up->u_signal[SIGCLD - 1] = SIG_DFL;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Delete the dot4 sigqueues/signotifies.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	sigqfree(p);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
3340Sstevel@tonic-gate 	p->p_prof.pr_base = NULL;
3350Sstevel@tonic-gate 	p->p_prof.pr_size = 0;
3360Sstevel@tonic-gate 	p->p_prof.pr_off = 0;
3370Sstevel@tonic-gate 	p->p_prof.pr_scale = 0;
3380Sstevel@tonic-gate 	p->p_prof.pr_samples = 0;
3390Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	ASSERT(curthread->t_schedctl == NULL);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate #if defined(__sparc)
3440Sstevel@tonic-gate 	if (p->p_utraps != NULL)
3450Sstevel@tonic-gate 		utrap_free(p);
3460Sstevel@tonic-gate #endif	/* __sparc */
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * Close all close-on-exec files.
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	close_exec(P_FINFO(p));
3520Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
3530Sstevel@tonic-gate 	setregs(&args);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/* Mark this as an executable vnode */
3560Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
3570Sstevel@tonic-gate 	vp->v_flag |= VVMEXEC;
3580Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	VN_RELE(vp);
3610Sstevel@tonic-gate 	if (dir != NULL)
3620Sstevel@tonic-gate 		VN_RELE(dir);
3630Sstevel@tonic-gate 	pn_free(&resolvepn);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * Allocate a new lwp directory and lwpid hash table if necessary.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
3690Sstevel@tonic-gate 		lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
3700Sstevel@tonic-gate 		lwpdir->ld_next = lwpdir + 1;
3710Sstevel@tonic-gate 		tidhash = kmem_zalloc(2 * sizeof (lwpdir_t *), KM_SLEEP);
3720Sstevel@tonic-gate 		if (p->p_lwpdir != NULL)
3730Sstevel@tonic-gate 			lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
3740Sstevel@tonic-gate 		else
3750Sstevel@tonic-gate 			lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3790Sstevel@tonic-gate 	prbarrier(p);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3820Sstevel@tonic-gate 	 * Reset lwp id to the default value of 1.
3830Sstevel@tonic-gate 	 * This is a single-threaded process now
3840Sstevel@tonic-gate 	 * and lwp #1 is lwp_wait()able by default.
3850Sstevel@tonic-gate 	 * The t_unpark flag should not be inherited.
3860Sstevel@tonic-gate 	 */
3870Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
3880Sstevel@tonic-gate 	curthread->t_tid = 1;
3890Sstevel@tonic-gate 	curthread->t_unpark = 0;
3900Sstevel@tonic-gate 	curthread->t_proc_flag |= TP_TWAIT;
3910Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_DAEMON;	/* daemons shouldn't exec */
3920Sstevel@tonic-gate 	p->p_lwpdaemon = 0;			/* but oh well ... */
3930Sstevel@tonic-gate 	p->p_lwpid = 1;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Install the newly-allocated lwp directory and lwpid hash table
3970Sstevel@tonic-gate 	 * and insert the current thread into the new hash table.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (lwpdir != NULL) {
4000Sstevel@tonic-gate 		old_lwpdir = p->p_lwpdir;
4010Sstevel@tonic-gate 		old_lwpdir_sz = p->p_lwpdir_sz;
4020Sstevel@tonic-gate 		old_tidhash = p->p_tidhash;
4030Sstevel@tonic-gate 		old_tidhash_sz = p->p_tidhash_sz;
4040Sstevel@tonic-gate 		p->p_lwpdir = p->p_lwpfree = lwpdir;
4050Sstevel@tonic-gate 		p->p_lwpdir_sz = 2;
4060Sstevel@tonic-gate 		p->p_tidhash = tidhash;
4070Sstevel@tonic-gate 		p->p_tidhash_sz = 2;
4080Sstevel@tonic-gate 		lep->le_thread = curthread;
4090Sstevel@tonic-gate 		lep->le_lwpid = curthread->t_tid;
4100Sstevel@tonic-gate 		lep->le_start = curthread->t_start;
4110Sstevel@tonic-gate 		lwp_hash_in(p, lep);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	/*
4140Sstevel@tonic-gate 	 * Restore the saved signal mask and
4150Sstevel@tonic-gate 	 * inform /proc that the exec() has finished.
4160Sstevel@tonic-gate 	 */
4170Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4180Sstevel@tonic-gate 	prexecend();
4190Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4200Sstevel@tonic-gate 	if (old_lwpdir) {
4210Sstevel@tonic-gate 		kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
4220Sstevel@tonic-gate 		kmem_free(old_tidhash, old_tidhash_sz * sizeof (lwpdir_t *));
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	ASSERT(error == 0);
4250Sstevel@tonic-gate 	DTRACE_PROC(exec__success);
4260Sstevel@tonic-gate 	return (0);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate fail:
4290Sstevel@tonic-gate 	DTRACE_PROC1(exec__failure, int, error);
4300Sstevel@tonic-gate out:		/* error return */
4310Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4320Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4330Sstevel@tonic-gate 	prexecend();
4340Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4350Sstevel@tonic-gate 	ASSERT(error != 0);
4360Sstevel@tonic-gate 	return (error);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate  * Perform generic exec duties and switchout to object-file specific
4420Sstevel@tonic-gate  * handler.
4430Sstevel@tonic-gate  */
4440Sstevel@tonic-gate int
4450Sstevel@tonic-gate gexec(
4460Sstevel@tonic-gate 	struct vnode **vpp,
4470Sstevel@tonic-gate 	struct execa *uap,
4480Sstevel@tonic-gate 	struct uarg *args,
4490Sstevel@tonic-gate 	struct intpdata *idatap,
4500Sstevel@tonic-gate 	int level,
4510Sstevel@tonic-gate 	long *execsz,
4520Sstevel@tonic-gate 	caddr_t exec_file,
4530Sstevel@tonic-gate 	struct cred *cred)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	struct vnode *vp;
4560Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
4570Sstevel@tonic-gate 	struct execsw *eswp;
4580Sstevel@tonic-gate 	int error = 0;
4590Sstevel@tonic-gate 	int suidflags = 0;
4600Sstevel@tonic-gate 	ssize_t resid;
4610Sstevel@tonic-gate 	uid_t uid, gid;
4620Sstevel@tonic-gate 	struct vattr vattr;
4630Sstevel@tonic-gate 	char magbuf[MAGIC_BYTES];
4640Sstevel@tonic-gate 	int setid;
4650Sstevel@tonic-gate 	cred_t *oldcred, *newcred = NULL;
4660Sstevel@tonic-gate 	int privflags = 0;
467*1335Scasper 	int setidfl;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * If the SNOCD or SUGID flag is set, turn it off and remember the
4710Sstevel@tonic-gate 	 * previous setting so we can restore it if we encounter an error.
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
4740Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
4750Sstevel@tonic-gate 		suidflags = pp->p_flag & PSUIDFLAGS;
4760Sstevel@tonic-gate 		pp->p_flag &= ~PSUIDFLAGS;
4770Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if ((error = execpermissions(*vpp, &vattr, args)) != 0)
4810Sstevel@tonic-gate 		goto bad;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/* need to open vnode for stateful file systems like rfs */
4840Sstevel@tonic-gate 	if ((error = VOP_OPEN(vpp, FREAD, CRED())) != 0)
4850Sstevel@tonic-gate 		goto bad;
4860Sstevel@tonic-gate 	vp = *vpp;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * Note: to support binary compatibility with SunOS a.out
4900Sstevel@tonic-gate 	 * executables, we read in the first four bytes, as the
4910Sstevel@tonic-gate 	 * magic number is in bytes 2-3.
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 	if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
4940Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
4950Sstevel@tonic-gate 		goto bad;
4960Sstevel@tonic-gate 	if (resid != 0)
4970Sstevel@tonic-gate 		goto bad;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if ((eswp = findexec_by_hdr(magbuf)) == NULL)
5000Sstevel@tonic-gate 		goto bad;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if (level == 0 &&
5030Sstevel@tonic-gate 	    (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) {
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		newcred = cred = crdup(cred);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		/* If we can, drop the PA bit */
5080Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0)
5090Sstevel@tonic-gate 			priv_adjust_PA(cred);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		if (privflags & PRIV_SETID) {
5120Sstevel@tonic-gate 			cred->cr_uid = uid;
5130Sstevel@tonic-gate 			cred->cr_gid = gid;
5140Sstevel@tonic-gate 			cred->cr_suid = uid;
5150Sstevel@tonic-gate 			cred->cr_sgid = gid;
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		/*
5190Sstevel@tonic-gate 		 * Implement the privilege updates:
5200Sstevel@tonic-gate 		 *
5210Sstevel@tonic-gate 		 * Restrict with L:
5220Sstevel@tonic-gate 		 *
5230Sstevel@tonic-gate 		 *	I' = I & L
5240Sstevel@tonic-gate 		 *
5250Sstevel@tonic-gate 		 *	E' = P' = (I' + F) & A
5260Sstevel@tonic-gate 		 *
5270Sstevel@tonic-gate 		 * But if running under ptrace, we cap I with P.
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0) {
5300Sstevel@tonic-gate 			if ((privflags & PRIV_INCREASE) != 0 &&
5310Sstevel@tonic-gate 			    (pp->p_proc_flag & P_PR_PTRACE) != 0)
5320Sstevel@tonic-gate 				priv_intersect(&CR_OPPRIV(cred),
5330Sstevel@tonic-gate 						    &CR_IPRIV(cred));
5340Sstevel@tonic-gate 			priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
5350Sstevel@tonic-gate 			CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
5360Sstevel@tonic-gate 			priv_adjust_PA(cred);
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* SunOS 4.x buy-back */
5410Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
5420Sstevel@tonic-gate 	    (vattr.va_mode & (VSUID|VSGID))) {
5430Sstevel@tonic-gate 		cmn_err(CE_NOTE,
5440Sstevel@tonic-gate 		    "!%s, uid %d: setuid execution not allowed, dev=%lx",
5450Sstevel@tonic-gate 		    exec_file, cred->cr_uid, vp->v_vfsp->vfs_dev);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * execsetid() told us whether or not we had to change the
5500Sstevel@tonic-gate 	 * credentials of the process.  In privflags, it told us
5510Sstevel@tonic-gate 	 * whether we gained any privileges or executed a set-uid executable.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE));
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * Use /etc/system variable to determine if the stack
5570Sstevel@tonic-gate 	 * should be marked as executable by default.
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	if (noexec_user_stack)
5600Sstevel@tonic-gate 		args->stk_prot &= ~PROT_EXEC;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * Traditionally, the setid flags told the sub processes whether
5660Sstevel@tonic-gate 	 * the file just executed was set-uid or set-gid; this caused
5670Sstevel@tonic-gate 	 * some confusion as the 'setid' flag did not match the SUGID
5680Sstevel@tonic-gate 	 * process flag which is only set when the uids/gids do not match.
5690Sstevel@tonic-gate 	 * A script set-gid/set-uid to the real uid/gid would start with
5700Sstevel@tonic-gate 	 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
5710Sstevel@tonic-gate 	 * Now we flag those cases where the calling process cannot
5720Sstevel@tonic-gate 	 * be trusted to influence the newly exec'ed process, either
5730Sstevel@tonic-gate 	 * because it runs with more privileges or when the uids/gids
5740Sstevel@tonic-gate 	 * do in fact not match.
5750Sstevel@tonic-gate 	 * This also makes the runtime linker agree with the on exec
5760Sstevel@tonic-gate 	 * values of SNOCD and SUGID.
5770Sstevel@tonic-gate 	 */
578*1335Scasper 	setidfl = 0;
579*1335Scasper 	if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
580*1335Scasper 	    !supgroupmember(cred->cr_gid, cred))) {
581*1335Scasper 		setidfl |= EXECSETID_UGIDS;
582*1335Scasper 	}
583*1335Scasper 	if (setid & PRIV_SETUGID)
584*1335Scasper 		setidfl |= EXECSETID_SETID;
585*1335Scasper 	if (setid & PRIV_INCREASE)
586*1335Scasper 		setidfl |= EXECSETID_PRIVS;
587*1335Scasper 
5880Sstevel@tonic-gate 	error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
589*1335Scasper 		setidfl, exec_file, cred);
5900Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
5910Sstevel@tonic-gate 	if (error != 0) {
5920Sstevel@tonic-gate 		if (newcred != NULL)
5930Sstevel@tonic-gate 			crfree(newcred);
5940Sstevel@tonic-gate 		goto bad;
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (level == 0) {
5980Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
5990Sstevel@tonic-gate 		if (newcred != NULL) {
6000Sstevel@tonic-gate 			/*
6010Sstevel@tonic-gate 			 * Free the old credentials, and set the new ones.
6020Sstevel@tonic-gate 			 * Do this for both the process and the (single) thread.
6030Sstevel@tonic-gate 			 */
6040Sstevel@tonic-gate 			crfree(pp->p_cred);
6050Sstevel@tonic-gate 			pp->p_cred = cred;	/* cred already held for proc */
6060Sstevel@tonic-gate 			crhold(cred);		/* hold new cred for thread */
6070Sstevel@tonic-gate 			/*
6080Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
6090Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
6100Sstevel@tonic-gate 			 * allocated cred structure.
6110Sstevel@tonic-gate 			 */
6120Sstevel@tonic-gate 			oldcred = curthread->t_cred;
6130Sstevel@tonic-gate 			curthread->t_cred = cred;
6140Sstevel@tonic-gate 			crfree(oldcred);
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 		/*
6170Sstevel@tonic-gate 		 * On emerging from a successful exec(), the saved
6180Sstevel@tonic-gate 		 * uid and gid equal the effective uid and gid.
6190Sstevel@tonic-gate 		 */
6200Sstevel@tonic-gate 		cred->cr_suid = cred->cr_uid;
6210Sstevel@tonic-gate 		cred->cr_sgid = cred->cr_gid;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		/*
6240Sstevel@tonic-gate 		 * If the real and effective ids do not match, this
6250Sstevel@tonic-gate 		 * is a setuid process that should not dump core.
6260Sstevel@tonic-gate 		 * The group comparison is tricky; we prevent the code
6270Sstevel@tonic-gate 		 * from flagging SNOCD when executing with an effective gid
6280Sstevel@tonic-gate 		 * which is a supplementary group.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		if (cred->cr_ruid != cred->cr_uid ||
6310Sstevel@tonic-gate 		    (cred->cr_rgid != cred->cr_gid &&
6320Sstevel@tonic-gate 		    !supgroupmember(cred->cr_gid, cred)) ||
6330Sstevel@tonic-gate 		    (privflags & PRIV_INCREASE) != 0)
6340Sstevel@tonic-gate 			suidflags = PSUIDFLAGS;
6350Sstevel@tonic-gate 		else
6360Sstevel@tonic-gate 			suidflags = 0;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
6390Sstevel@tonic-gate 		if (suidflags) {
6400Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
6410Sstevel@tonic-gate 			pp->p_flag |= suidflags;
6420Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 		if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
6450Sstevel@tonic-gate 			/*
6460Sstevel@tonic-gate 			 * If process is traced via /proc, arrange to
6470Sstevel@tonic-gate 			 * invalidate the associated /proc vnode.
6480Sstevel@tonic-gate 			 */
6490Sstevel@tonic-gate 			if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
6500Sstevel@tonic-gate 				args->traceinval = 1;
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_PTRACE)
6530Sstevel@tonic-gate 			psignal(pp, SIGTRAP);
6540Sstevel@tonic-gate 		if (args->traceinval)
6550Sstevel@tonic-gate 			prinvalidate(&pp->p_user);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (0);
6590Sstevel@tonic-gate bad:
6600Sstevel@tonic-gate 	if (error == 0)
6610Sstevel@tonic-gate 		error = ENOEXEC;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (suidflags) {
6640Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
6650Sstevel@tonic-gate 		pp->p_flag |= suidflags;
6660Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 	return (error);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate extern char *execswnames[];
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate struct execsw *
6740Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	int i, j;
6770Sstevel@tonic-gate 	char *ename;
6780Sstevel@tonic-gate 	char *magicp;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	mutex_enter(&execsw_lock);
6810Sstevel@tonic-gate 	for (i = 0; i < nexectype; i++) {
6820Sstevel@tonic-gate 		if (execswnames[i] == NULL) {
6830Sstevel@tonic-gate 			ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
6840Sstevel@tonic-gate 			(void) strcpy(ename, name);
6850Sstevel@tonic-gate 			execswnames[i] = ename;
6860Sstevel@tonic-gate 			/*
6870Sstevel@tonic-gate 			 * Set the magic number last so that we
6880Sstevel@tonic-gate 			 * don't need to hold the execsw_lock in
6890Sstevel@tonic-gate 			 * findexectype().
6900Sstevel@tonic-gate 			 */
6910Sstevel@tonic-gate 			magicp = kmem_alloc(magic_size, KM_SLEEP);
6920Sstevel@tonic-gate 			for (j = 0; j < magic_size; j++)
6930Sstevel@tonic-gate 				magicp[j] = magic[j];
6940Sstevel@tonic-gate 			execsw[i].exec_magic = magicp;
6950Sstevel@tonic-gate 			mutex_exit(&execsw_lock);
6960Sstevel@tonic-gate 			return (&execsw[i]);
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	mutex_exit(&execsw_lock);
7000Sstevel@tonic-gate 	return (NULL);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * Find the exec switch table entry with the corresponding magic string.
7050Sstevel@tonic-gate  */
7060Sstevel@tonic-gate struct execsw *
7070Sstevel@tonic-gate findexecsw(char *magic)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	struct execsw *eswp;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7120Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7130Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
7140Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
7150Sstevel@tonic-gate 			return (eswp);
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 	return (NULL);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * Find the execsw[] index for the given exec header string by looking for the
7220Sstevel@tonic-gate  * magic string at a specified offset and length for each kind of executable
7230Sstevel@tonic-gate  * file format until one matches.  If no execsw[] entry is found, try to
7240Sstevel@tonic-gate  * autoload a module for this magic string.
7250Sstevel@tonic-gate  */
7260Sstevel@tonic-gate struct execsw *
7270Sstevel@tonic-gate findexec_by_hdr(char *header)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	struct execsw *eswp;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7320Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7330Sstevel@tonic-gate 		if (header && eswp->exec_maglen != 0 &&
7340Sstevel@tonic-gate 		    bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
7350Sstevel@tonic-gate 			    eswp->exec_maglen) == 0) {
7360Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
7370Sstevel@tonic-gate 				return (NULL);
7380Sstevel@tonic-gate 			return (eswp);
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate /*
7450Sstevel@tonic-gate  * Find the execsw[] index for the given magic string.  If no execsw[] entry
7460Sstevel@tonic-gate  * is found, try to autoload a module for this magic string.
7470Sstevel@tonic-gate  */
7480Sstevel@tonic-gate struct execsw *
7490Sstevel@tonic-gate findexec_by_magic(char *magic)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	struct execsw *eswp;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7540Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7550Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
7560Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
7570Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
7580Sstevel@tonic-gate 				return (NULL);
7590Sstevel@tonic-gate 			return (eswp);
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate static int
7660Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	char *name;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	rw_enter(eswp->exec_lock, RW_READER);
7710Sstevel@tonic-gate 	while (!LOADED_EXEC(eswp)) {
7720Sstevel@tonic-gate 		rw_exit(eswp->exec_lock);
7730Sstevel@tonic-gate 		name = execswnames[eswp-execsw];
7740Sstevel@tonic-gate 		ASSERT(name);
7750Sstevel@tonic-gate 		if (modload("exec", name) == -1)
7760Sstevel@tonic-gate 			return (-1);
7770Sstevel@tonic-gate 		rw_enter(eswp->exec_lock, RW_READER);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	return (0);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate static int
7830Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
7860Sstevel@tonic-gate 	uid_t uid, gid;
7870Sstevel@tonic-gate 	cred_t *cr = pp->p_cred;
7880Sstevel@tonic-gate 	int privflags = 0;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * Remember credentials.
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	uid = cr->cr_uid;
7940Sstevel@tonic-gate 	gid = cr->cr_gid;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/* Will try to reset the PRIV_AWARE bit later. */
7970Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
7980Sstevel@tonic-gate 		privflags |= PRIV_RESET;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
8010Sstevel@tonic-gate 		/*
8020Sstevel@tonic-gate 		 * Set-uid root execution only allowed if the limit set
8030Sstevel@tonic-gate 		 * holds all unsafe privileges.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 		if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 ||
8060Sstevel@tonic-gate 		    priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) {
8070Sstevel@tonic-gate 			uid = vattrp->va_uid;
8080Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		if (vattrp->va_mode & VSGID) {
8110Sstevel@tonic-gate 			gid = vattrp->va_gid;
8120Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * Do we need to change our credential anyway?
8180Sstevel@tonic-gate 	 * This is the case when E != I or P != I, as
8190Sstevel@tonic-gate 	 * we need to do the assignments (with F empty and A full)
8200Sstevel@tonic-gate 	 * Or when I is not a subset of L; in that case we need to
8210Sstevel@tonic-gate 	 * enforce L.
8220Sstevel@tonic-gate 	 *
8230Sstevel@tonic-gate 	 *		I' = L & I
8240Sstevel@tonic-gate 	 *
8250Sstevel@tonic-gate 	 *		E' = P' = (I' + F) & A
8260Sstevel@tonic-gate 	 * or
8270Sstevel@tonic-gate 	 *		E' = P' = I'
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
8300Sstevel@tonic-gate 	    !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
8310Sstevel@tonic-gate 	    !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
8320Sstevel@tonic-gate 		privflags |= PRIV_RESET;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	/*
8350Sstevel@tonic-gate 	 * When we introduce the "forced" set then we will need
8360Sstevel@tonic-gate 	 * to set PRIV_INCREASE here if I not a subset of P.
8370Sstevel@tonic-gate 	 * If the "allowed" set is introduced we will need to do
8380Sstevel@tonic-gate 	 * a similar thing; however, it seems more reasonable to
8390Sstevel@tonic-gate 	 * have the allowed set reduce "L": script language interpreters
8400Sstevel@tonic-gate 	 * would typically have an allowed set of "all".
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Set setuid/setgid protections if no ptrace() compatibility.
8450Sstevel@tonic-gate 	 * For privileged processes, honor setuid/setgid even in
8460Sstevel@tonic-gate 	 * the presence of ptrace() compatibility.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
8490Sstevel@tonic-gate 	    PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
8500Sstevel@tonic-gate 	    (cr->cr_uid != uid ||
8510Sstevel@tonic-gate 	    cr->cr_gid != gid ||
8520Sstevel@tonic-gate 	    cr->cr_suid != uid ||
8530Sstevel@tonic-gate 	    cr->cr_sgid != gid)) {
8540Sstevel@tonic-gate 		*uidp = uid;
8550Sstevel@tonic-gate 		*gidp = gid;
8560Sstevel@tonic-gate 		privflags |= PRIV_SETID;
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 	return (privflags);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate int
8620Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	int error;
8650Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
8680Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred))
8690Sstevel@tonic-gate 		return (error);
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * Check the access mode.
8720Sstevel@tonic-gate 	 * If VPROC, ask /proc if the file is an object file.
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred)) != 0 ||
8750Sstevel@tonic-gate 	    !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
8760Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
8770Sstevel@tonic-gate 	    (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
8780Sstevel@tonic-gate 		if (error == 0)
8790Sstevel@tonic-gate 			error = EACCES;
8800Sstevel@tonic-gate 		return (error);
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
8840Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred))) {
8850Sstevel@tonic-gate 		/*
8860Sstevel@tonic-gate 		 * If process is under ptrace(2) compatibility,
8870Sstevel@tonic-gate 		 * fail the exec(2).
8880Sstevel@tonic-gate 		 */
8890Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_PTRACE)
8900Sstevel@tonic-gate 			goto bad;
8910Sstevel@tonic-gate 		/*
8920Sstevel@tonic-gate 		 * Process is traced via /proc.
8930Sstevel@tonic-gate 		 * Arrange to invalidate the /proc vnode.
8940Sstevel@tonic-gate 		 */
8950Sstevel@tonic-gate 		args->traceinval = 1;
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 	return (0);
8980Sstevel@tonic-gate bad:
8990Sstevel@tonic-gate 	if (error == 0)
9000Sstevel@tonic-gate 		error = ENOEXEC;
9010Sstevel@tonic-gate 	return (error);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate /*
9050Sstevel@tonic-gate  * Map a section of an executable file into the user's
9060Sstevel@tonic-gate  * address space.
9070Sstevel@tonic-gate  */
9080Sstevel@tonic-gate int
9090Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
9100Sstevel@tonic-gate     off_t offset, int prot, int page, uint_t szc)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate 	int error = 0;
9130Sstevel@tonic-gate 	off_t oldoffset;
9140Sstevel@tonic-gate 	caddr_t zfodbase, oldaddr;
9150Sstevel@tonic-gate 	size_t end, oldlen;
9160Sstevel@tonic-gate 	size_t zfoddiff;
9170Sstevel@tonic-gate 	label_t ljb;
9180Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	oldaddr = addr;
9210Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
9220Sstevel@tonic-gate 	if (len) {
9230Sstevel@tonic-gate 		oldlen = len;
9240Sstevel@tonic-gate 		len += ((size_t)oldaddr - (size_t)addr);
9250Sstevel@tonic-gate 		oldoffset = offset;
9260Sstevel@tonic-gate 		offset = (off_t)((uintptr_t)offset & PAGEMASK);
9270Sstevel@tonic-gate 		if (page) {
9280Sstevel@tonic-gate 			spgcnt_t  prefltmem, availm, npages;
9290Sstevel@tonic-gate 			int preread;
9300Sstevel@tonic-gate 			uint_t mflag = MAP_PRIVATE | MAP_FIXED;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
9330Sstevel@tonic-gate 				mflag |= MAP_TEXT;
9340Sstevel@tonic-gate 			} else {
9350Sstevel@tonic-gate 				mflag |= MAP_INITDATA;
9360Sstevel@tonic-gate 			}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
9390Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
9400Sstevel@tonic-gate 				error = ENOMEM;
9410Sstevel@tonic-gate 				goto bad;
9420Sstevel@tonic-gate 			}
9430Sstevel@tonic-gate 			if (error = VOP_MAP(vp, (offset_t)offset,
9440Sstevel@tonic-gate 			    p->p_as, &addr, len, prot, PROT_ALL,
9450Sstevel@tonic-gate 			    mflag, CRED()))
9460Sstevel@tonic-gate 				goto bad;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 			/*
9490Sstevel@tonic-gate 			 * If the segment can fit, then we prefault
9500Sstevel@tonic-gate 			 * the entire segment in.  This is based on the
9510Sstevel@tonic-gate 			 * model that says the best working set of a
9520Sstevel@tonic-gate 			 * small program is all of its pages.
9530Sstevel@tonic-gate 			 */
9540Sstevel@tonic-gate 			npages = (spgcnt_t)btopr(len);
9550Sstevel@tonic-gate 			prefltmem = freemem - desfree;
9560Sstevel@tonic-gate 			preread =
9570Sstevel@tonic-gate 			    (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 			/*
9600Sstevel@tonic-gate 			 * If we aren't prefaulting the segment,
9610Sstevel@tonic-gate 			 * increment "deficit", if necessary to ensure
9620Sstevel@tonic-gate 			 * that pages will become available when this
9630Sstevel@tonic-gate 			 * process starts executing.
9640Sstevel@tonic-gate 			 */
9650Sstevel@tonic-gate 			availm = freemem - lotsfree;
9660Sstevel@tonic-gate 			if (preread == 0 && npages > availm &&
9670Sstevel@tonic-gate 			    deficit < lotsfree) {
9680Sstevel@tonic-gate 				deficit += MIN((pgcnt_t)(npages - availm),
9690Sstevel@tonic-gate 				    lotsfree - deficit);
9700Sstevel@tonic-gate 			}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 			if (preread) {
9730Sstevel@tonic-gate 				TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
9740Sstevel@tonic-gate 				    "execmap preread:freemem %d size %lu",
9750Sstevel@tonic-gate 				    freemem, len);
9760Sstevel@tonic-gate 				(void) as_fault(p->p_as->a_hat, p->p_as,
9770Sstevel@tonic-gate 				    (caddr_t)addr, len, F_INVAL, S_READ);
9780Sstevel@tonic-gate 			}
9790Sstevel@tonic-gate 		} else {
9800Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
9810Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
9820Sstevel@tonic-gate 				error = ENOMEM;
9830Sstevel@tonic-gate 				goto bad;
9840Sstevel@tonic-gate 			}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 			if (error = as_map(p->p_as, addr, len,
9870Sstevel@tonic-gate 			    segvn_create, zfod_argsp))
9880Sstevel@tonic-gate 				goto bad;
9890Sstevel@tonic-gate 			/*
9900Sstevel@tonic-gate 			 * Read in the segment in one big chunk.
9910Sstevel@tonic-gate 			 */
9920Sstevel@tonic-gate 			if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
9930Sstevel@tonic-gate 			    oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
9940Sstevel@tonic-gate 			    (rlim64_t)0, CRED(), (ssize_t *)0))
9950Sstevel@tonic-gate 				goto bad;
9960Sstevel@tonic-gate 			/*
9970Sstevel@tonic-gate 			 * Now set protections.
9980Sstevel@tonic-gate 			 */
9990Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
10000Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)addr,
10010Sstevel@tonic-gate 				    len, prot);
10020Sstevel@tonic-gate 			}
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (zfodlen) {
10070Sstevel@tonic-gate 		end = (size_t)addr + len;
10080Sstevel@tonic-gate 		zfodbase = (caddr_t)roundup(end, PAGESIZE);
10090Sstevel@tonic-gate 		zfoddiff = (uintptr_t)zfodbase - end;
10100Sstevel@tonic-gate 		if (zfoddiff) {
10110Sstevel@tonic-gate 			if (on_fault(&ljb)) {
10120Sstevel@tonic-gate 				no_fault();
10130Sstevel@tonic-gate 				error = EFAULT;
10140Sstevel@tonic-gate 				goto bad;
10150Sstevel@tonic-gate 			}
10160Sstevel@tonic-gate 			uzero((void *)end, zfoddiff);
10170Sstevel@tonic-gate 			no_fault();
10180Sstevel@tonic-gate 		}
10190Sstevel@tonic-gate 		if (zfodlen > zfoddiff) {
10200Sstevel@tonic-gate 			struct segvn_crargs crargs =
10210Sstevel@tonic-gate 			    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 			zfodlen -= zfoddiff;
10240Sstevel@tonic-gate 			if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
10250Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
10260Sstevel@tonic-gate 				error = ENOMEM;
10270Sstevel@tonic-gate 				goto bad;
10280Sstevel@tonic-gate 			}
10290Sstevel@tonic-gate 			crargs.szc = szc;
10300Sstevel@tonic-gate 			if (error = as_map(p->p_as, (caddr_t)zfodbase,
10310Sstevel@tonic-gate 			    zfodlen, segvn_create, &crargs))
10320Sstevel@tonic-gate 				goto bad;
10330Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
10340Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)zfodbase,
10350Sstevel@tonic-gate 				    zfodlen, prot);
10360Sstevel@tonic-gate 			}
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	return (0);
10400Sstevel@tonic-gate bad:
10410Sstevel@tonic-gate 	return (error);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate void
10450Sstevel@tonic-gate setexecenv(struct execenv *ep)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
10480Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
10490Sstevel@tonic-gate 	struct vnode *vp;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	p->p_bssbase = ep->ex_bssbase;
10520Sstevel@tonic-gate 	p->p_brkbase = ep->ex_brkbase;
10530Sstevel@tonic-gate 	p->p_brksize = ep->ex_brksize;
10540Sstevel@tonic-gate 	if (p->p_exec)
10550Sstevel@tonic-gate 		VN_RELE(p->p_exec);	/* out with the old */
10560Sstevel@tonic-gate 	vp = p->p_exec = ep->ex_vp;
10570Sstevel@tonic-gate 	if (vp != NULL)
10580Sstevel@tonic-gate 		VN_HOLD(vp);		/* in with the new */
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
10610Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
10620Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate int
10660Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
10670Sstevel@tonic-gate {
10680Sstevel@tonic-gate 	struct vnode *vp = *vpp;
10690Sstevel@tonic-gate 	file_t *fp;
10700Sstevel@tonic-gate 	int error = 0;
10710Sstevel@tonic-gate 	int filemode = FREAD;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	VN_HOLD(vp);		/* open reference */
10740Sstevel@tonic-gate 	if (error = falloc(NULL, filemode, &fp, fdp)) {
10750Sstevel@tonic-gate 		VN_RELE(vp);
10760Sstevel@tonic-gate 		*fdp = -1;	/* just in case falloc changed value */
10770Sstevel@tonic-gate 		return (error);
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 	if (error = VOP_OPEN(&vp, filemode, CRED())) {
10800Sstevel@tonic-gate 		VN_RELE(vp);
10810Sstevel@tonic-gate 		setf(*fdp, NULL);
10820Sstevel@tonic-gate 		unfalloc(fp);
10830Sstevel@tonic-gate 		*fdp = -1;
10840Sstevel@tonic-gate 		return (error);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	*vpp = vp;		/* vnode should not have changed */
10870Sstevel@tonic-gate 	fp->f_vnode = vp;
10880Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
10890Sstevel@tonic-gate 	setf(*fdp, fp);
10900Sstevel@tonic-gate 	return (0);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate int
10940Sstevel@tonic-gate execclose(int fd)
10950Sstevel@tonic-gate {
10960Sstevel@tonic-gate 	return (closeandsetf(fd, NULL));
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * noexec stub function.
11020Sstevel@tonic-gate  */
11030Sstevel@tonic-gate /*ARGSUSED*/
11040Sstevel@tonic-gate int
11050Sstevel@tonic-gate noexec(
11060Sstevel@tonic-gate     struct vnode *vp,
11070Sstevel@tonic-gate     struct execa *uap,
11080Sstevel@tonic-gate     struct uarg *args,
11090Sstevel@tonic-gate     struct intpdata *idatap,
11100Sstevel@tonic-gate     int level,
11110Sstevel@tonic-gate     long *execsz,
11120Sstevel@tonic-gate     int setid,
11130Sstevel@tonic-gate     caddr_t exec_file,
11140Sstevel@tonic-gate     struct cred *cred)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate 	cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
11170Sstevel@tonic-gate 	return (ENOEXEC);
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate  * Support routines for building a user stack.
11220Sstevel@tonic-gate  *
11230Sstevel@tonic-gate  * execve(path, argv, envp) must construct a new stack with the specified
11240Sstevel@tonic-gate  * arguments and environment variables (see exec_args() for a description
11250Sstevel@tonic-gate  * of the user stack layout).  To do this, we copy the arguments and
11260Sstevel@tonic-gate  * environment variables from the old user address space into the kernel,
11270Sstevel@tonic-gate  * free the old as, create the new as, and copy our buffered information
11280Sstevel@tonic-gate  * to the new stack.  Our kernel buffer has the following structure:
11290Sstevel@tonic-gate  *
11300Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
11310Sstevel@tonic-gate  *	| string offsets	|
11320Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
11330Sstevel@tonic-gate  *	|			|
11340Sstevel@tonic-gate  *	| STK_AVAIL() space	|
11350Sstevel@tonic-gate  *	|			|
11360Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
11370Sstevel@tonic-gate  *	| strings		|
11380Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
11390Sstevel@tonic-gate  *
11400Sstevel@tonic-gate  * When we add a string, we store the string's contents (including the null
11410Sstevel@tonic-gate  * terminator) at stk_strp, and we store the offset of the string relative to
11420Sstevel@tonic-gate  * stk_base at --stk_offp.  At strings are added, stk_strp increases and
11430Sstevel@tonic-gate  * stk_offp decreases.  The amount of space remaining, STK_AVAIL(), is just
11440Sstevel@tonic-gate  * the difference between these pointers.  If we run out of space, we return
11450Sstevel@tonic-gate  * an error and exec_args() starts all over again with a buffer twice as large.
11460Sstevel@tonic-gate  * When we're all done, the kernel buffer looks like this:
11470Sstevel@tonic-gate  *
11480Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
11490Sstevel@tonic-gate  *	| argv[0] offset	|
11500Sstevel@tonic-gate  *	+-----------------------+
11510Sstevel@tonic-gate  *	| ...			|
11520Sstevel@tonic-gate  *	+-----------------------+
11530Sstevel@tonic-gate  *	| argv[argc-1] offset	|
11540Sstevel@tonic-gate  *	+-----------------------+
11550Sstevel@tonic-gate  *	| envp[0] offset	|
11560Sstevel@tonic-gate  *	+-----------------------+
11570Sstevel@tonic-gate  *	| ...			|
11580Sstevel@tonic-gate  *	+-----------------------+
11590Sstevel@tonic-gate  *	| envp[envc-1] offset	|
11600Sstevel@tonic-gate  *	+-----------------------+
11610Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
11620Sstevel@tonic-gate  *	+-----------------------+
11630Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
11640Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
11650Sstevel@tonic-gate  *	|			|
11660Sstevel@tonic-gate  *	| STK_AVAIL() space	|
11670Sstevel@tonic-gate  *	|			|
11680Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
11690Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
11700Sstevel@tonic-gate  *	+-----------------------+
11710Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
11720Sstevel@tonic-gate  *	+-----------------------+
11730Sstevel@tonic-gate  *	| envp[envc-1] string	|
11740Sstevel@tonic-gate  *	+-----------------------+
11750Sstevel@tonic-gate  *	| ...			|
11760Sstevel@tonic-gate  *	+-----------------------+
11770Sstevel@tonic-gate  *	| envp[0] string	|
11780Sstevel@tonic-gate  *	+-----------------------+
11790Sstevel@tonic-gate  *	| argv[argc-1] string	|
11800Sstevel@tonic-gate  *	+-----------------------+
11810Sstevel@tonic-gate  *	| ...			|
11820Sstevel@tonic-gate  *	+-----------------------+
11830Sstevel@tonic-gate  *	| argv[0] string	|
11840Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
11850Sstevel@tonic-gate  */
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate #define	STK_AVAIL(args)		((char *)(args)->stk_offp - (args)->stk_strp)
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate /*
11900Sstevel@tonic-gate  * Add a string to the stack.
11910Sstevel@tonic-gate  */
11920Sstevel@tonic-gate static int
11930Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
11940Sstevel@tonic-gate {
11950Sstevel@tonic-gate 	int error;
11960Sstevel@tonic-gate 	size_t len;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (STK_AVAIL(args) < sizeof (int))
11990Sstevel@tonic-gate 		return (E2BIG);
12000Sstevel@tonic-gate 	*--args->stk_offp = args->stk_strp - args->stk_base;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	if (segflg == UIO_USERSPACE) {
12030Sstevel@tonic-gate 		error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
12040Sstevel@tonic-gate 		if (error != 0)
12050Sstevel@tonic-gate 			return (error);
12060Sstevel@tonic-gate 	} else {
12070Sstevel@tonic-gate 		len = strlen(sp) + 1;
12080Sstevel@tonic-gate 		if (len > STK_AVAIL(args))
12090Sstevel@tonic-gate 			return (E2BIG);
12100Sstevel@tonic-gate 		bcopy(sp, args->stk_strp, len);
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	args->stk_strp += len;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	return (0);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate static int
12190Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	int error;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if (args->from_model == DATAMODEL_NATIVE) {
12240Sstevel@tonic-gate 		ulong_t ptr;
12250Sstevel@tonic-gate 		error = fulword(src, &ptr);
12260Sstevel@tonic-gate 		*dst = (caddr_t)ptr;
12270Sstevel@tonic-gate 	} else {
12280Sstevel@tonic-gate 		uint32_t ptr;
12290Sstevel@tonic-gate 		error = fuword32(src, &ptr);
12300Sstevel@tonic-gate 		*dst = (caddr_t)(uintptr_t)ptr;
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 	return (error);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate static int
12360Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE)
12390Sstevel@tonic-gate 		return (sulword(addr, (ulong_t)value));
12400Sstevel@tonic-gate 	else
12410Sstevel@tonic-gate 		return (suword32(addr, (uint32_t)(uintptr_t)value));
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate static int
12450Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	char *sp;
12480Sstevel@tonic-gate 	int argc, error;
12490Sstevel@tonic-gate 	int argv_empty = 0;
12500Sstevel@tonic-gate 	size_t ptrsize = args->from_ptrsize;
12510Sstevel@tonic-gate 	size_t size, pad;
12520Sstevel@tonic-gate 	char *argv = (char *)uap->argp;
12530Sstevel@tonic-gate 	char *envp = (char *)uap->envp;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	/*
12560Sstevel@tonic-gate 	 * Copy interpreter's name and argument to argv[0] and argv[1].
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	if (intp != NULL && intp->intp_name != NULL) {
12590Sstevel@tonic-gate 		if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
12600Sstevel@tonic-gate 			return (error);
12610Sstevel@tonic-gate 		if (intp->intp_arg != NULL &&
12620Sstevel@tonic-gate 		    (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
12630Sstevel@tonic-gate 			return (error);
12640Sstevel@tonic-gate 		if (args->fname != NULL)
12650Sstevel@tonic-gate 			error = stk_add(args, args->fname, UIO_SYSSPACE);
12660Sstevel@tonic-gate 		else
12670Sstevel@tonic-gate 			error = stk_add(args, uap->fname, UIO_USERSPACE);
12680Sstevel@tonic-gate 		if (error)
12690Sstevel@tonic-gate 			return (error);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 		/*
12720Sstevel@tonic-gate 		 * Check for an empty argv[].
12730Sstevel@tonic-gate 		 */
12740Sstevel@tonic-gate 		if (stk_getptr(args, argv, &sp))
12750Sstevel@tonic-gate 			return (EFAULT);
12760Sstevel@tonic-gate 		if (sp == NULL)
12770Sstevel@tonic-gate 			argv_empty = 1;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		argv += ptrsize;		/* ignore original argv[0] */
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	if (argv_empty == 0) {
12830Sstevel@tonic-gate 		/*
12840Sstevel@tonic-gate 		 * Add argv[] strings to the stack.
12850Sstevel@tonic-gate 		 */
12860Sstevel@tonic-gate 		for (;;) {
12870Sstevel@tonic-gate 			if (stk_getptr(args, argv, &sp))
12880Sstevel@tonic-gate 				return (EFAULT);
12890Sstevel@tonic-gate 			if (sp == NULL)
12900Sstevel@tonic-gate 				break;
12910Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
12920Sstevel@tonic-gate 				return (error);
12930Sstevel@tonic-gate 			argv += ptrsize;
12940Sstevel@tonic-gate 		}
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 	argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
12970Sstevel@tonic-gate 	args->arglen = args->stk_strp - args->stk_base;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/*
13000Sstevel@tonic-gate 	 * Add environ[] strings to the stack.
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 	if (envp != NULL) {
13030Sstevel@tonic-gate 		for (;;) {
13040Sstevel@tonic-gate 			if (stk_getptr(args, envp, &sp))
13050Sstevel@tonic-gate 				return (EFAULT);
13060Sstevel@tonic-gate 			if (sp == NULL)
13070Sstevel@tonic-gate 				break;
13080Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
13090Sstevel@tonic-gate 				return (error);
13100Sstevel@tonic-gate 			envp += ptrsize;
13110Sstevel@tonic-gate 		}
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 	args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
13140Sstevel@tonic-gate 	args->ne = args->na - argc;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13170Sstevel@tonic-gate 	 * Add AT_SUN_PLATFORM and AT_SUN_EXECNAME strings to the stack.
13180Sstevel@tonic-gate 	 */
13190Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
13200Sstevel@tonic-gate 		if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
13210Sstevel@tonic-gate 			return (error);
13220Sstevel@tonic-gate 		if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
13230Sstevel@tonic-gate 			return (error);
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * Compute the size of the stack.  This includes all the pointers,
13280Sstevel@tonic-gate 	 * the space reserved for the aux vector, and all the strings.
13290Sstevel@tonic-gate 	 * The total number of pointers is args->na (which is argc + envc)
13300Sstevel@tonic-gate 	 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
13310Sstevel@tonic-gate 	 * after the last argument (i.e. argv[argc]); (3) the NULL after the
13320Sstevel@tonic-gate 	 * last environment variable (i.e. envp[envc]); and (4) the NULL after
13330Sstevel@tonic-gate 	 * all the strings, at the very top of the stack.
13340Sstevel@tonic-gate 	 */
13350Sstevel@tonic-gate 	size = (args->na + 4) * args->to_ptrsize + args->auxsize +
13360Sstevel@tonic-gate 	    (args->stk_strp - args->stk_base);
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	/*
13390Sstevel@tonic-gate 	 * Pad the string section with zeroes to align the stack size.
13400Sstevel@tonic-gate 	 */
13410Sstevel@tonic-gate 	pad = P2NPHASE(size, args->stk_align);
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	if (STK_AVAIL(args) < pad)
13440Sstevel@tonic-gate 		return (E2BIG);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	args->usrstack_size = size + pad;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	while (pad-- != 0)
13490Sstevel@tonic-gate 		*args->stk_strp++ = 0;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	args->nc = args->stk_strp - args->stk_base;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	return (0);
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate static int
13570Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
13580Sstevel@tonic-gate {
13590Sstevel@tonic-gate 	size_t ptrsize = args->to_ptrsize;
13600Sstevel@tonic-gate 	ssize_t pslen;
13610Sstevel@tonic-gate 	char *kstrp = args->stk_base;
13620Sstevel@tonic-gate 	char *ustrp = usrstack - args->nc - ptrsize;
13630Sstevel@tonic-gate 	char *usp = usrstack - args->usrstack_size;
13640Sstevel@tonic-gate 	int *offp = (int *)(args->stk_base + args->stk_size);
13650Sstevel@tonic-gate 	int envc = args->ne;
13660Sstevel@tonic-gate 	int argc = args->na - envc;
13670Sstevel@tonic-gate 	int i;
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	/*
13700Sstevel@tonic-gate 	 * Record argc for /proc.
13710Sstevel@tonic-gate 	 */
13720Sstevel@tonic-gate 	up->u_argc = argc;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Put argc on the stack.  Note that even though it's an int,
13760Sstevel@tonic-gate 	 * it always consumes ptrsize bytes (for alignment).
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
13790Sstevel@tonic-gate 		return (-1);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	/*
13820Sstevel@tonic-gate 	 * Add argc space (ptrsize) to usp and record argv for /proc.
13830Sstevel@tonic-gate 	 */
13840Sstevel@tonic-gate 	up->u_argv = (uintptr_t)(usp += ptrsize);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	/*
13870Sstevel@tonic-gate 	 * Put the argv[] pointers on the stack.
13880Sstevel@tonic-gate 	 */
13890Sstevel@tonic-gate 	for (i = 0; i < argc; i++, usp += ptrsize)
13900Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
13910Sstevel@tonic-gate 			return (-1);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	/*
13940Sstevel@tonic-gate 	 * Copy arguments to u_psargs.
13950Sstevel@tonic-gate 	 */
13960Sstevel@tonic-gate 	pslen = MIN(args->arglen, PSARGSZ) - 1;
13970Sstevel@tonic-gate 	for (i = 0; i < pslen; i++)
13980Sstevel@tonic-gate 		up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
13990Sstevel@tonic-gate 	while (i < PSARGSZ)
14000Sstevel@tonic-gate 		up->u_psargs[i++] = '\0';
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	/*
14030Sstevel@tonic-gate 	 * Add space for argv[]'s NULL terminator (ptrsize) to usp and
14040Sstevel@tonic-gate 	 * record envp for /proc.
14050Sstevel@tonic-gate 	 */
14060Sstevel@tonic-gate 	up->u_envp = (uintptr_t)(usp += ptrsize);
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	/*
14090Sstevel@tonic-gate 	 * Put the envp[] pointers on the stack.
14100Sstevel@tonic-gate 	 */
14110Sstevel@tonic-gate 	for (i = 0; i < envc; i++, usp += ptrsize)
14120Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
14130Sstevel@tonic-gate 			return (-1);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	/*
14160Sstevel@tonic-gate 	 * Add space for envp[]'s NULL terminator (ptrsize) to usp and
14170Sstevel@tonic-gate 	 * remember where the stack ends, which is also where auxv begins.
14180Sstevel@tonic-gate 	 */
14190Sstevel@tonic-gate 	args->stackend = usp += ptrsize;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/*
14220Sstevel@tonic-gate 	 * Put all the argv[], envp[], and auxv strings on the stack.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	if (copyout(args->stk_base, ustrp, args->nc))
14250Sstevel@tonic-gate 		return (-1);
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	/*
14280Sstevel@tonic-gate 	 * Fill in the aux vector now that we know the user stack addresses
14290Sstevel@tonic-gate 	 * for the AT_SUN_PLATFORM and AT_SUN_EXECNAME strings.
14300Sstevel@tonic-gate 	 */
14310Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
14320Sstevel@tonic-gate 		if (args->to_model == DATAMODEL_NATIVE) {
14330Sstevel@tonic-gate 			auxv_t **a = (auxv_t **)auxvpp;
14340Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
14350Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
14360Sstevel@tonic-gate 		} else {
14370Sstevel@tonic-gate 			auxv32_t **a = (auxv32_t **)auxvpp;
14380Sstevel@tonic-gate 			ADDAUX(*a,
14390Sstevel@tonic-gate 			    AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
14400Sstevel@tonic-gate 			ADDAUX(*a,
14410Sstevel@tonic-gate 			    AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]);
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	return (0);
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate #ifdef DEBUG
14490Sstevel@tonic-gate int mpss_brkpgszsel = 0;
14500Sstevel@tonic-gate int mpss_stkpgszsel = 0;
14510Sstevel@tonic-gate #endif
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate /*
14540Sstevel@tonic-gate  * Initialize a new user stack with the specified arguments and environment.
14550Sstevel@tonic-gate  * The initial user stack layout is as follows:
14560Sstevel@tonic-gate  *
14570Sstevel@tonic-gate  *	User Stack
14580Sstevel@tonic-gate  *	+---------------+ <--- curproc->p_usrstack
14590Sstevel@tonic-gate  *	| NULL		|
14600Sstevel@tonic-gate  *	+---------------+
14610Sstevel@tonic-gate  *	|		|
14620Sstevel@tonic-gate  *	| auxv strings	|
14630Sstevel@tonic-gate  *	|		|
14640Sstevel@tonic-gate  *	+---------------+
14650Sstevel@tonic-gate  *	|		|
14660Sstevel@tonic-gate  *	| envp strings	|
14670Sstevel@tonic-gate  *	|		|
14680Sstevel@tonic-gate  *	+---------------+
14690Sstevel@tonic-gate  *	|		|
14700Sstevel@tonic-gate  *	| argv strings	|
14710Sstevel@tonic-gate  *	|		|
14720Sstevel@tonic-gate  *	+---------------+ <--- ustrp
14730Sstevel@tonic-gate  *	|		|
14740Sstevel@tonic-gate  *	| aux vector	|
14750Sstevel@tonic-gate  *	|		|
14760Sstevel@tonic-gate  *	+---------------+ <--- auxv
14770Sstevel@tonic-gate  *	| NULL		|
14780Sstevel@tonic-gate  *	+---------------+
14790Sstevel@tonic-gate  *	| envp[envc-1]	|
14800Sstevel@tonic-gate  *	+---------------+
14810Sstevel@tonic-gate  *	| ...		|
14820Sstevel@tonic-gate  *	+---------------+
14830Sstevel@tonic-gate  *	| envp[0]	|
14840Sstevel@tonic-gate  *	+---------------+ <--- envp[]
14850Sstevel@tonic-gate  *	| NULL		|
14860Sstevel@tonic-gate  *	+---------------+
14870Sstevel@tonic-gate  *	| argv[argc-1]	|
14880Sstevel@tonic-gate  *	+---------------+
14890Sstevel@tonic-gate  *	| ...		|
14900Sstevel@tonic-gate  *	+---------------+
14910Sstevel@tonic-gate  *	| argv[0]	|
14920Sstevel@tonic-gate  *	+---------------+ <--- argv[]
14930Sstevel@tonic-gate  *	| argc		|
14940Sstevel@tonic-gate  *	+---------------+ <--- stack base
14950Sstevel@tonic-gate  */
14960Sstevel@tonic-gate int
14970Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	size_t size;
15000Sstevel@tonic-gate 	int error;
15010Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
15020Sstevel@tonic-gate 	user_t *up = PTOU(p);
15030Sstevel@tonic-gate 	char *usrstack;
15040Sstevel@tonic-gate 	rctl_entity_p_t e;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	struct as *as;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	args->from_model = p->p_model;
15090Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
15100Sstevel@tonic-gate 		args->from_ptrsize = sizeof (long);
15110Sstevel@tonic-gate 	} else {
15120Sstevel@tonic-gate 		args->from_ptrsize = sizeof (int32_t);
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE) {
15160Sstevel@tonic-gate 		args->to_ptrsize = sizeof (long);
15170Sstevel@tonic-gate 		args->ncargs = NCARGS;
15180Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN;
15190Sstevel@tonic-gate 		usrstack = (char *)USRSTACK;
15200Sstevel@tonic-gate 	} else {
15210Sstevel@tonic-gate 		args->to_ptrsize = sizeof (int32_t);
15220Sstevel@tonic-gate 		args->ncargs = NCARGS32;
15230Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN32;
15240Sstevel@tonic-gate 		usrstack = (char *)USRSTACK32;
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate #if defined(__sparc)
15300Sstevel@tonic-gate 	/*
15310Sstevel@tonic-gate 	 * Make sure user register windows are empty before
15320Sstevel@tonic-gate 	 * attempting to make a new stack.
15330Sstevel@tonic-gate 	 */
15340Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
15350Sstevel@tonic-gate #endif
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	for (size = PAGESIZE; ; size *= 2) {
15380Sstevel@tonic-gate 		args->stk_size = size;
15390Sstevel@tonic-gate 		args->stk_base = kmem_alloc(size, KM_SLEEP);
15400Sstevel@tonic-gate 		args->stk_strp = args->stk_base;
15410Sstevel@tonic-gate 		args->stk_offp = (int *)(args->stk_base + size);
15420Sstevel@tonic-gate 		error = stk_copyin(uap, args, intp, auxvpp);
15430Sstevel@tonic-gate 		if (error == 0)
15440Sstevel@tonic-gate 			break;
15450Sstevel@tonic-gate 		kmem_free(args->stk_base, size);
15460Sstevel@tonic-gate 		if (error != E2BIG && error != ENAMETOOLONG)
15470Sstevel@tonic-gate 			return (error);
15480Sstevel@tonic-gate 		if (size >= args->ncargs)
15490Sstevel@tonic-gate 			return (E2BIG);
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	size = args->usrstack_size;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	ASSERT(error == 0);
15550Sstevel@tonic-gate 	ASSERT(P2PHASE(size, args->stk_align) == 0);
15560Sstevel@tonic-gate 	ASSERT((ssize_t)STK_AVAIL(args) >= 0);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	if (size > args->ncargs) {
15590Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
15600Sstevel@tonic-gate 		return (E2BIG);
15610Sstevel@tonic-gate 	}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	/*
15640Sstevel@tonic-gate 	 * Leave only the current lwp and force the other lwps to exit.
15650Sstevel@tonic-gate 	 * If another lwp beat us to the punch by calling exit(), bail out.
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	if ((error = exitlwps(0)) != 0) {
15680Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
15690Sstevel@tonic-gate 		return (error);
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/*
15730Sstevel@tonic-gate 	 * Revoke any doors created by the process.
15740Sstevel@tonic-gate 	 */
15750Sstevel@tonic-gate 	if (p->p_door_list)
15760Sstevel@tonic-gate 		door_exit();
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	/*
15790Sstevel@tonic-gate 	 * Release schedctl data structures.
15800Sstevel@tonic-gate 	 */
15810Sstevel@tonic-gate 	if (p->p_pagep)
15820Sstevel@tonic-gate 		schedctl_proc_cleanup();
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * Clean up any DTrace helpers for the process.
15860Sstevel@tonic-gate 	 */
15870Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
15880Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
15890Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
15930Sstevel@tonic-gate 	/*
15940Sstevel@tonic-gate 	 * Cleanup the DTrace provider associated with this process.
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
15970Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exec_ptr != NULL);
15980Sstevel@tonic-gate 		dtrace_fasttrap_exec_ptr(p);
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	/*
16030Sstevel@tonic-gate 	 * discard the lwpchan cache.
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	if (p->p_lcp != NULL)
16060Sstevel@tonic-gate 		lwpchan_destroy_cache(1);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	/*
16090Sstevel@tonic-gate 	 * Delete the POSIX timers.
16100Sstevel@tonic-gate 	 */
16110Sstevel@tonic-gate 	if (p->p_itimer != NULL)
16120Sstevel@tonic-gate 		timer_exit();
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate #ifdef C2_AUDIT
16150Sstevel@tonic-gate 	if (audit_active)
16160Sstevel@tonic-gate 		audit_exec(args->stk_base, args->stk_base + args->arglen,
16170Sstevel@tonic-gate 		    args->na - args->ne, args->ne);
16180Sstevel@tonic-gate #endif
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * Ensure that we don't change resource associations while we
16220Sstevel@tonic-gate 	 * change address spaces.
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16250Sstevel@tonic-gate 	pool_barrier_enter();
16260Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	/*
16290Sstevel@tonic-gate 	 * Destroy the old address space and create a new one.
16300Sstevel@tonic-gate 	 * From here on, any errors are fatal to the exec()ing process.
16310Sstevel@tonic-gate 	 * On error we return -1, which means the caller must SIGKILL
16320Sstevel@tonic-gate 	 * the process.
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	relvm();
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16370Sstevel@tonic-gate 	pool_barrier_exit();
16380Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	up->u_execsw = args->execswp;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	p->p_brkbase = NULL;
16430Sstevel@tonic-gate 	p->p_brksize = 0;
16440Sstevel@tonic-gate 	p->p_stksize = 0;
16450Sstevel@tonic-gate 	p->p_model = args->to_model;
16460Sstevel@tonic-gate 	p->p_usrstack = usrstack;
16470Sstevel@tonic-gate 	p->p_stkprot = args->stk_prot;
16480Sstevel@tonic-gate 	p->p_datprot = args->dat_prot;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	/*
16510Sstevel@tonic-gate 	 * Reset resource controls such that all controls are again active as
16520Sstevel@tonic-gate 	 * well as appropriate to the potentially new address model for the
16530Sstevel@tonic-gate 	 * process.
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	e.rcep_p.proc = p;
16560Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
16570Sstevel@tonic-gate 	rctl_set_reset(p->p_rctls, p, &e);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	if (exec_lpg_disable == 0) {
16600Sstevel@tonic-gate #ifdef DEBUG
16610Sstevel@tonic-gate 		uint_t pgsizes = page_num_pagesizes();
16620Sstevel@tonic-gate 		uint_t szc;
16630Sstevel@tonic-gate #endif
16640Sstevel@tonic-gate 		p->p_brkpageszc = args->brkpageszc;
16650Sstevel@tonic-gate 		p->p_stkpageszc = args->stkpageszc;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		if (p->p_brkpageszc == 0) {
16680Sstevel@tonic-gate 			p->p_brkpageszc = page_szc(map_pgsz(MAPPGSZ_HEAP,
16690Sstevel@tonic-gate 			    p, 0, 0, NULL));
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 		if (p->p_stkpageszc == 0) {
16720Sstevel@tonic-gate 			p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK,
16730Sstevel@tonic-gate 			    p, 0, 0, NULL));
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate #ifdef DEBUG
16770Sstevel@tonic-gate 		if (mpss_brkpgszsel != 0) {
16780Sstevel@tonic-gate 			if (mpss_brkpgszsel == -1) {
16790Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 8) % pgsizes;
16800Sstevel@tonic-gate 			} else {
16810Sstevel@tonic-gate 				szc = mpss_brkpgszsel % pgsizes;
16820Sstevel@tonic-gate 			}
16830Sstevel@tonic-gate 			p->p_brkpageszc = szc;
16840Sstevel@tonic-gate 		}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 		if (mpss_stkpgszsel != 0) {
16870Sstevel@tonic-gate 			if (mpss_stkpgszsel == -1) {
16880Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 7) % pgsizes;
16890Sstevel@tonic-gate 			} else {
16900Sstevel@tonic-gate 				szc = mpss_stkpgszsel % pgsizes;
16910Sstevel@tonic-gate 			}
16920Sstevel@tonic-gate 			p->p_stkpageszc = szc;
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate #endif
16960Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
16970Sstevel@tonic-gate 		p->p_flag |= SAUTOLPG;	/* kernel controls page sizes */
16980Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	} else {
17010Sstevel@tonic-gate 		p->p_brkpageszc = 0;
17020Sstevel@tonic-gate 		p->p_stkpageszc = 0;
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	exec_set_sp(size);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	as = as_alloc();
17080Sstevel@tonic-gate 	p->p_as = as;
17090Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
17100Sstevel@tonic-gate 		as->a_userlimit = (caddr_t)USERLIMIT32;
17110Sstevel@tonic-gate 	(void) hat_setup(as->a_hat, HAT_ALLOC);
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * Finally, write out the contents of the new stack.
17150Sstevel@tonic-gate 	 */
17160Sstevel@tonic-gate 	error = stk_copyout(args, usrstack, auxvpp, up);
17170Sstevel@tonic-gate 	kmem_free(args->stk_base, args->stk_size);
17180Sstevel@tonic-gate 	return (error);
17190Sstevel@tonic-gate }
1720