xref: /onnv-gate/usr/src/uts/common/os/exec.c (revision 5331:3047ad28a67b)
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 /*
224426Saguzovsk  * Copyright 2007 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>
524426Saguzovsk #include <sys/lgrp.h>
530Sstevel@tonic-gate #include <sys/vtrace.h>
540Sstevel@tonic-gate #include <sys/exec.h>
550Sstevel@tonic-gate #include <sys/exechdr.h>
560Sstevel@tonic-gate #include <sys/kmem.h>
570Sstevel@tonic-gate #include <sys/prsystm.h>
580Sstevel@tonic-gate #include <sys/modctl.h>
590Sstevel@tonic-gate #include <sys/vmparam.h>
600Sstevel@tonic-gate #include <sys/schedctl.h>
610Sstevel@tonic-gate #include <sys/utrap.h>
620Sstevel@tonic-gate #include <sys/systeminfo.h>
630Sstevel@tonic-gate #include <sys/stack.h>
640Sstevel@tonic-gate #include <sys/rctl.h>
650Sstevel@tonic-gate #include <sys/dtrace.h>
660Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
670Sstevel@tonic-gate #include <sys/pool.h>
680Sstevel@tonic-gate #include <sys/sdt.h>
692712Snn35248 #include <sys/brand.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <c2/audit.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include <vm/hat.h>
740Sstevel@tonic-gate #include <vm/anon.h>
750Sstevel@tonic-gate #include <vm/as.h>
760Sstevel@tonic-gate #include <vm/seg.h>
770Sstevel@tonic-gate #include <vm/seg_vn.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	PRIV_RESET		0x01	/* needs to reset privs */
800Sstevel@tonic-gate #define	PRIV_SETID		0x02	/* needs to change uids */
810Sstevel@tonic-gate #define	PRIV_SETUGID		0x04	/* is setuid/setgid/forced privs */
820Sstevel@tonic-gate #define	PRIV_INCREASE		0x08	/* child runs with more privs */
831676Sjpk #define	MAC_FLAGS		0x10	/* need to adjust MAC flags */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *);
860Sstevel@tonic-gate static int hold_execsw(struct execsw *);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate uint_t auxv_hwcap = 0;	/* auxv AT_SUN_HWCAP value; determined on the fly */
890Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
900Sstevel@tonic-gate uint_t auxv_hwcap32 = 0;	/* 32-bit version of auxv_hwcap */
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	PSUIDFLAGS		(SNOCD|SUGID)
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * exec() - wrapper around exece providing NULL environment pointer
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate int
990Sstevel@tonic-gate exec(const char *fname, const char **argp)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	return (exece(fname, argp, NULL));
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * exece() - system call wrapper around exec_common()
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate int
1080Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	int error;
1110Sstevel@tonic-gate 
1122712Snn35248 	error = exec_common(fname, argp, envp, EBA_NONE);
1130Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate int
1172712Snn35248 exec_common(const char *fname, const char **argp, const char **envp,
1182712Snn35248     int brand_action)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
1210Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1220Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1230Sstevel@tonic-gate 	struct user *up = PTOU(p);
1240Sstevel@tonic-gate 	long execsz;		/* temporary count of exec size */
1250Sstevel@tonic-gate 	int i;
1260Sstevel@tonic-gate 	int error;
1270Sstevel@tonic-gate 	char exec_file[MAXCOMLEN+1];
1280Sstevel@tonic-gate 	struct pathname pn;
1290Sstevel@tonic-gate 	struct pathname resolvepn;
1300Sstevel@tonic-gate 	struct uarg args;
1310Sstevel@tonic-gate 	struct execa ua;
1320Sstevel@tonic-gate 	k_sigset_t savedmask;
1330Sstevel@tonic-gate 	lwpdir_t *lwpdir = NULL;
1340Sstevel@tonic-gate 	lwpdir_t **tidhash;
1350Sstevel@tonic-gate 	lwpdir_t *old_lwpdir = NULL;
1360Sstevel@tonic-gate 	uint_t old_lwpdir_sz;
1370Sstevel@tonic-gate 	lwpdir_t **old_tidhash;
1380Sstevel@tonic-gate 	uint_t old_tidhash_sz;
1390Sstevel@tonic-gate 	lwpent_t *lep;
1402712Snn35248 	int brandme = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * exec() is not supported for the /proc agent lwp.
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
1460Sstevel@tonic-gate 		return (ENOTSUP);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((error = secpolicy_basic_exec(CRED())) != 0)
1490Sstevel@tonic-gate 		return (error);
1500Sstevel@tonic-gate 
1512712Snn35248 	if (brand_action != EBA_NONE) {
1522712Snn35248 		/*
1532712Snn35248 		 * Brand actions are not supported for processes that are not
1542712Snn35248 		 * running in a branded zone.
1552712Snn35248 		 */
1562712Snn35248 		if (!ZONE_IS_BRANDED(p->p_zone))
1572712Snn35248 			return (ENOTSUP);
1582712Snn35248 
1592712Snn35248 		if (brand_action == EBA_NATIVE) {
1602712Snn35248 			/* Only branded processes can be unbranded */
1612712Snn35248 			if (!PROC_IS_BRANDED(p))
1622712Snn35248 				return (ENOTSUP);
1632712Snn35248 		} else {
1642712Snn35248 			/* Only unbranded processes can be branded */
1652712Snn35248 			if (PROC_IS_BRANDED(p))
1662712Snn35248 				return (ENOTSUP);
1672712Snn35248 			brandme = 1;
1682712Snn35248 		}
1692712Snn35248 	} else {
1702712Snn35248 		/*
1712712Snn35248 		 * If this is a native zone, or if the process is already
1722712Snn35248 		 * branded, then we don't need to do anything.  If this is
1732712Snn35248 		 * a native process in a branded zone, we need to brand the
1742712Snn35248 		 * process as it exec()s the new binary.
1752712Snn35248 		 */
1762712Snn35248 		if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p))
1772712Snn35248 			brandme = 1;
1782712Snn35248 	}
1792712Snn35248 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Inform /proc that an exec() has started.
1820Sstevel@tonic-gate 	 * Hold signals that are ignored by default so that we will
1830Sstevel@tonic-gate 	 * not be interrupted by a signal that will be ignored after
1840Sstevel@tonic-gate 	 * successful completion of gexec().
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1870Sstevel@tonic-gate 	prexecstart();
1880Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
1890Sstevel@tonic-gate 	savedmask = curthread->t_hold;
1900Sstevel@tonic-gate 	sigorset(&curthread->t_hold, &ignoredefault);
1910Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Look up path name and remember last component for later.
1950Sstevel@tonic-gate 	 * To help coreadm expand its %d token, we attempt to save
1960Sstevel@tonic-gate 	 * the directory containing the executable in p_execdir. The
1970Sstevel@tonic-gate 	 * first call to lookuppn() may fail and return EINVAL because
1980Sstevel@tonic-gate 	 * dirvpp is non-NULL. In that case, we make a second call to
1990Sstevel@tonic-gate 	 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
2000Sstevel@tonic-gate 	 * but coreadm is allowed to expand %d to the empty string and
2010Sstevel@tonic-gate 	 * there are other cases in which that failure may occur.
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
2040Sstevel@tonic-gate 		goto out;
2050Sstevel@tonic-gate 	pn_alloc(&resolvepn);
2060Sstevel@tonic-gate 	if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
2070Sstevel@tonic-gate 		pn_free(&resolvepn);
2080Sstevel@tonic-gate 		pn_free(&pn);
2090Sstevel@tonic-gate 		if (error != EINVAL)
2100Sstevel@tonic-gate 			goto out;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		dir = NULL;
2130Sstevel@tonic-gate 		if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
2140Sstevel@tonic-gate 			goto out;
2150Sstevel@tonic-gate 		pn_alloc(&resolvepn);
2160Sstevel@tonic-gate 		if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
2170Sstevel@tonic-gate 		    &vp)) != 0) {
2180Sstevel@tonic-gate 			pn_free(&resolvepn);
2190Sstevel@tonic-gate 			pn_free(&pn);
2200Sstevel@tonic-gate 			goto out;
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	if (vp == NULL) {
2240Sstevel@tonic-gate 		if (dir != NULL)
2250Sstevel@tonic-gate 			VN_RELE(dir);
2260Sstevel@tonic-gate 		error = ENOENT;
2270Sstevel@tonic-gate 		pn_free(&resolvepn);
2280Sstevel@tonic-gate 		pn_free(&pn);
2290Sstevel@tonic-gate 		goto out;
2300Sstevel@tonic-gate 	}
2311043Scasper 
2321043Scasper 	/*
2331043Scasper 	 * We do not allow executing files in attribute directories.
2341043Scasper 	 * We test this by determining whether the resolved path
2351043Scasper 	 * contains a "/" when we're in an attribute directory;
2361043Scasper 	 * only if the pathname does not contain a "/" the resolved path
2371043Scasper 	 * points to a file in the current working (attribute) directory.
2381043Scasper 	 */
2391043Scasper 	if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
2401043Scasper 	    strchr(resolvepn.pn_path, '/') == NULL) {
2411043Scasper 		if (dir != NULL)
2421043Scasper 			VN_RELE(dir);
2431043Scasper 		error = EACCES;
2441043Scasper 		pn_free(&resolvepn);
2451043Scasper 		pn_free(&pn);
2461043Scasper 		VN_RELE(vp);
2471043Scasper 		goto out;
2481043Scasper 	}
2491043Scasper 
2500Sstevel@tonic-gate 	bzero(exec_file, MAXCOMLEN+1);
2510Sstevel@tonic-gate 	(void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
2520Sstevel@tonic-gate 	bzero(&args, sizeof (args));
2530Sstevel@tonic-gate 	args.pathname = resolvepn.pn_path;
2540Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
2550Sstevel@tonic-gate 	pn_free(&pn);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * Specific exec handlers, or policies determined via
2590Sstevel@tonic-gate 	 * /etc/system may override the historical default.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	args.stk_prot = PROT_ZFOD;
2620Sstevel@tonic-gate 	args.dat_prot = PROT_ZFOD;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	CPU_STATS_ADD_K(sys, sysexec, 1);
2650Sstevel@tonic-gate 	DTRACE_PROC1(exec, char *, args.pathname);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	ua.fname = fname;
2680Sstevel@tonic-gate 	ua.argp = argp;
2690Sstevel@tonic-gate 	ua.envp = envp;
2700Sstevel@tonic-gate 
2712712Snn35248 	/* If necessary, brand this process before we start the exec. */
2722712Snn35248 	if (brandme != 0)
2732712Snn35248 		brand_setbrand(p);
2742712Snn35248 
2750Sstevel@tonic-gate 	if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
2762712Snn35248 	    exec_file, p->p_cred, brand_action)) != 0) {
2772712Snn35248 		if (brandme != 0)
2782712Snn35248 			BROP(p)->b_proc_exit(p, lwp);
2790Sstevel@tonic-gate 		VN_RELE(vp);
2800Sstevel@tonic-gate 		if (dir != NULL)
2810Sstevel@tonic-gate 			VN_RELE(dir);
2820Sstevel@tonic-gate 		pn_free(&resolvepn);
2830Sstevel@tonic-gate 		goto fail;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Free floating point registers (sun4u only)
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 	ASSERT(lwp != NULL);
2900Sstevel@tonic-gate 	lwp_freeregs(lwp, 1);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2931217Srab 	 * Free thread and process context ops.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	if (curthread->t_ctx)
2960Sstevel@tonic-gate 		freectx(curthread, 1);
2971217Srab 	if (p->p_pctx)
2981217Srab 		freepctx(p, 1);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * Remember file name for accounting; clear any cached DTrace predicate.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate 	up->u_acflag &= ~AFORK;
3040Sstevel@tonic-gate 	bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
3050Sstevel@tonic-gate 	curthread->t_predcache = NULL;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * Clear contract template state
3090Sstevel@tonic-gate 	 */
3100Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/*
3130Sstevel@tonic-gate 	 * Save the directory in which we found the executable for expanding
3140Sstevel@tonic-gate 	 * the %d token used in core file patterns.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3170Sstevel@tonic-gate 	tmpvp = p->p_execdir;
3180Sstevel@tonic-gate 	p->p_execdir = dir;
3190Sstevel@tonic-gate 	if (p->p_execdir != NULL)
3200Sstevel@tonic-gate 		VN_HOLD(p->p_execdir);
3210Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (tmpvp != NULL)
3240Sstevel@tonic-gate 		VN_RELE(tmpvp);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Reset stack state to the user stack, clear set of signals
3280Sstevel@tonic-gate 	 * caught on the signal stack, and reset list of signals that
3290Sstevel@tonic-gate 	 * restart system calls; the new program's environment should
3300Sstevel@tonic-gate 	 * not be affected by detritus from the old program.  Any
3310Sstevel@tonic-gate 	 * pending held signals remain held, so don't clear t_hold.
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3340Sstevel@tonic-gate 	lwp->lwp_oldcontext = 0;
3350Sstevel@tonic-gate 	lwp->lwp_ustack = 0;
3360Sstevel@tonic-gate 	lwp->lwp_old_stk_ctl = 0;
3370Sstevel@tonic-gate 	sigemptyset(&up->u_signodefer);
3380Sstevel@tonic-gate 	sigemptyset(&up->u_sigonstack);
3390Sstevel@tonic-gate 	sigemptyset(&up->u_sigresethand);
3400Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
3410Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
3420Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Make saved resource limit == current resource limit.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	for (i = 0; i < RLIM_NLIMITS; i++) {
3480Sstevel@tonic-gate 		/*CONSTCOND*/
3490Sstevel@tonic-gate 		if (RLIM_SAVED(i)) {
3500Sstevel@tonic-gate 			(void) rctl_rlimit_get(rctlproc_legacy[i], p,
3510Sstevel@tonic-gate 			    &up->u_saved_rlimit[i]);
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * If the action was to catch the signal, then the action
3570Sstevel@tonic-gate 	 * must be reset to SIG_DFL.
3580Sstevel@tonic-gate 	 */
3590Sstevel@tonic-gate 	sigdefault(p);
3600Sstevel@tonic-gate 	p->p_flag &= ~(SNOWAIT|SJCTL);
3610Sstevel@tonic-gate 	p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
3620Sstevel@tonic-gate 	up->u_signal[SIGCLD - 1] = SIG_DFL;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * Delete the dot4 sigqueues/signotifies.
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	sigqfree(p);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
3720Sstevel@tonic-gate 	p->p_prof.pr_base = NULL;
3730Sstevel@tonic-gate 	p->p_prof.pr_size = 0;
3740Sstevel@tonic-gate 	p->p_prof.pr_off = 0;
3750Sstevel@tonic-gate 	p->p_prof.pr_scale = 0;
3760Sstevel@tonic-gate 	p->p_prof.pr_samples = 0;
3770Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	ASSERT(curthread->t_schedctl == NULL);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate #if defined(__sparc)
3820Sstevel@tonic-gate 	if (p->p_utraps != NULL)
3830Sstevel@tonic-gate 		utrap_free(p);
3840Sstevel@tonic-gate #endif	/* __sparc */
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * Close all close-on-exec files.
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	close_exec(P_FINFO(p));
3900Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
3912712Snn35248 
3922712Snn35248 	/* Unbrand ourself if requested. */
3932712Snn35248 	if (brand_action == EBA_NATIVE)
3942712Snn35248 		BROP(p)->b_proc_exit(p, lwp);
3952712Snn35248 	ASSERT((brand_action != EBA_NATIVE) || !PROC_IS_BRANDED(p));
3962712Snn35248 
3970Sstevel@tonic-gate 	setregs(&args);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/* Mark this as an executable vnode */
4000Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
4010Sstevel@tonic-gate 	vp->v_flag |= VVMEXEC;
4020Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	VN_RELE(vp);
4050Sstevel@tonic-gate 	if (dir != NULL)
4060Sstevel@tonic-gate 		VN_RELE(dir);
4070Sstevel@tonic-gate 	pn_free(&resolvepn);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * Allocate a new lwp directory and lwpid hash table if necessary.
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
4130Sstevel@tonic-gate 		lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
4140Sstevel@tonic-gate 		lwpdir->ld_next = lwpdir + 1;
4150Sstevel@tonic-gate 		tidhash = kmem_zalloc(2 * sizeof (lwpdir_t *), KM_SLEEP);
4160Sstevel@tonic-gate 		if (p->p_lwpdir != NULL)
4170Sstevel@tonic-gate 			lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
4180Sstevel@tonic-gate 		else
4190Sstevel@tonic-gate 			lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4222712Snn35248 	if (PROC_IS_BRANDED(p))
4232712Snn35248 		BROP(p)->b_exec();
4242712Snn35248 
4250Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4260Sstevel@tonic-gate 	prbarrier(p);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/*
4290Sstevel@tonic-gate 	 * Reset lwp id to the default value of 1.
4300Sstevel@tonic-gate 	 * This is a single-threaded process now
4310Sstevel@tonic-gate 	 * and lwp #1 is lwp_wait()able by default.
4320Sstevel@tonic-gate 	 * The t_unpark flag should not be inherited.
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
4350Sstevel@tonic-gate 	curthread->t_tid = 1;
4364426Saguzovsk 	kpreempt_disable();
4374426Saguzovsk 	ASSERT(curthread->t_lpl != NULL);
4384426Saguzovsk 	p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid;
4394426Saguzovsk 	kpreempt_enable();
4404426Saguzovsk 	if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) {
4414426Saguzovsk 		lgrp_update_trthr_migrations(1);
4424426Saguzovsk 	}
4430Sstevel@tonic-gate 	curthread->t_unpark = 0;
4440Sstevel@tonic-gate 	curthread->t_proc_flag |= TP_TWAIT;
4450Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_DAEMON;	/* daemons shouldn't exec */
4460Sstevel@tonic-gate 	p->p_lwpdaemon = 0;			/* but oh well ... */
4470Sstevel@tonic-gate 	p->p_lwpid = 1;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * Install the newly-allocated lwp directory and lwpid hash table
4510Sstevel@tonic-gate 	 * and insert the current thread into the new hash table.
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	if (lwpdir != NULL) {
4540Sstevel@tonic-gate 		old_lwpdir = p->p_lwpdir;
4550Sstevel@tonic-gate 		old_lwpdir_sz = p->p_lwpdir_sz;
4560Sstevel@tonic-gate 		old_tidhash = p->p_tidhash;
4570Sstevel@tonic-gate 		old_tidhash_sz = p->p_tidhash_sz;
4580Sstevel@tonic-gate 		p->p_lwpdir = p->p_lwpfree = lwpdir;
4590Sstevel@tonic-gate 		p->p_lwpdir_sz = 2;
4600Sstevel@tonic-gate 		p->p_tidhash = tidhash;
4610Sstevel@tonic-gate 		p->p_tidhash_sz = 2;
4620Sstevel@tonic-gate 		lep->le_thread = curthread;
4630Sstevel@tonic-gate 		lep->le_lwpid = curthread->t_tid;
4640Sstevel@tonic-gate 		lep->le_start = curthread->t_start;
4650Sstevel@tonic-gate 		lwp_hash_in(p, lep);
4660Sstevel@tonic-gate 	}
4672712Snn35248 
4680Sstevel@tonic-gate 	/*
4690Sstevel@tonic-gate 	 * Restore the saved signal mask and
4700Sstevel@tonic-gate 	 * inform /proc that the exec() has finished.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4730Sstevel@tonic-gate 	prexecend();
4740Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4750Sstevel@tonic-gate 	if (old_lwpdir) {
4760Sstevel@tonic-gate 		kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
4770Sstevel@tonic-gate 		kmem_free(old_tidhash, old_tidhash_sz * sizeof (lwpdir_t *));
4780Sstevel@tonic-gate 	}
4792712Snn35248 
4800Sstevel@tonic-gate 	ASSERT(error == 0);
4810Sstevel@tonic-gate 	DTRACE_PROC(exec__success);
4820Sstevel@tonic-gate 	return (0);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate fail:
4850Sstevel@tonic-gate 	DTRACE_PROC1(exec__failure, int, error);
4860Sstevel@tonic-gate out:		/* error return */
4870Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4880Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4890Sstevel@tonic-gate 	prexecend();
4900Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4910Sstevel@tonic-gate 	ASSERT(error != 0);
4920Sstevel@tonic-gate 	return (error);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * Perform generic exec duties and switchout to object-file specific
4980Sstevel@tonic-gate  * handler.
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate int
5010Sstevel@tonic-gate gexec(
5020Sstevel@tonic-gate 	struct vnode **vpp,
5030Sstevel@tonic-gate 	struct execa *uap,
5040Sstevel@tonic-gate 	struct uarg *args,
5050Sstevel@tonic-gate 	struct intpdata *idatap,
5060Sstevel@tonic-gate 	int level,
5070Sstevel@tonic-gate 	long *execsz,
5080Sstevel@tonic-gate 	caddr_t exec_file,
5092712Snn35248 	struct cred *cred,
5102712Snn35248 	int brand_action)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	struct vnode *vp;
5130Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
5140Sstevel@tonic-gate 	struct execsw *eswp;
5150Sstevel@tonic-gate 	int error = 0;
5160Sstevel@tonic-gate 	int suidflags = 0;
5170Sstevel@tonic-gate 	ssize_t resid;
5180Sstevel@tonic-gate 	uid_t uid, gid;
5190Sstevel@tonic-gate 	struct vattr vattr;
5200Sstevel@tonic-gate 	char magbuf[MAGIC_BYTES];
5210Sstevel@tonic-gate 	int setid;
5220Sstevel@tonic-gate 	cred_t *oldcred, *newcred = NULL;
5230Sstevel@tonic-gate 	int privflags = 0;
5241335Scasper 	int setidfl;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/*
5270Sstevel@tonic-gate 	 * If the SNOCD or SUGID flag is set, turn it off and remember the
5280Sstevel@tonic-gate 	 * previous setting so we can restore it if we encounter an error.
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
5310Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5320Sstevel@tonic-gate 		suidflags = pp->p_flag & PSUIDFLAGS;
5330Sstevel@tonic-gate 		pp->p_flag &= ~PSUIDFLAGS;
5340Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if ((error = execpermissions(*vpp, &vattr, args)) != 0)
5380Sstevel@tonic-gate 		goto bad;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* need to open vnode for stateful file systems like rfs */
541*5331Samw 	if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0)
5420Sstevel@tonic-gate 		goto bad;
5430Sstevel@tonic-gate 	vp = *vpp;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/*
5460Sstevel@tonic-gate 	 * Note: to support binary compatibility with SunOS a.out
5470Sstevel@tonic-gate 	 * executables, we read in the first four bytes, as the
5480Sstevel@tonic-gate 	 * magic number is in bytes 2-3.
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
5510Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
5520Sstevel@tonic-gate 		goto bad;
5530Sstevel@tonic-gate 	if (resid != 0)
5540Sstevel@tonic-gate 		goto bad;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if ((eswp = findexec_by_hdr(magbuf)) == NULL)
5570Sstevel@tonic-gate 		goto bad;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (level == 0 &&
5600Sstevel@tonic-gate 	    (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) {
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		newcred = cred = crdup(cred);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		/* If we can, drop the PA bit */
5650Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0)
5660Sstevel@tonic-gate 			priv_adjust_PA(cred);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 		if (privflags & PRIV_SETID) {
5690Sstevel@tonic-gate 			cred->cr_uid = uid;
5700Sstevel@tonic-gate 			cred->cr_gid = gid;
5710Sstevel@tonic-gate 			cred->cr_suid = uid;
5720Sstevel@tonic-gate 			cred->cr_sgid = gid;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 
5751676Sjpk 		if (privflags & MAC_FLAGS) {
5761676Sjpk 			if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
5771676Sjpk 				CR_FLAGS(cred) &= ~NET_MAC_AWARE;
5781676Sjpk 			CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
5791676Sjpk 		}
5801676Sjpk 
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * Implement the privilege updates:
5830Sstevel@tonic-gate 		 *
5840Sstevel@tonic-gate 		 * Restrict with L:
5850Sstevel@tonic-gate 		 *
5860Sstevel@tonic-gate 		 *	I' = I & L
5870Sstevel@tonic-gate 		 *
5880Sstevel@tonic-gate 		 *	E' = P' = (I' + F) & A
5890Sstevel@tonic-gate 		 *
5900Sstevel@tonic-gate 		 * But if running under ptrace, we cap I with P.
5910Sstevel@tonic-gate 		 */
5920Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0) {
5930Sstevel@tonic-gate 			if ((privflags & PRIV_INCREASE) != 0 &&
5940Sstevel@tonic-gate 			    (pp->p_proc_flag & P_PR_PTRACE) != 0)
5950Sstevel@tonic-gate 				priv_intersect(&CR_OPPRIV(cred),
5960Sstevel@tonic-gate 						    &CR_IPRIV(cred));
5970Sstevel@tonic-gate 			priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
5980Sstevel@tonic-gate 			CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
5990Sstevel@tonic-gate 			priv_adjust_PA(cred);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/* SunOS 4.x buy-back */
6040Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
6050Sstevel@tonic-gate 	    (vattr.va_mode & (VSUID|VSGID))) {
6060Sstevel@tonic-gate 		cmn_err(CE_NOTE,
6070Sstevel@tonic-gate 		    "!%s, uid %d: setuid execution not allowed, dev=%lx",
6080Sstevel@tonic-gate 		    exec_file, cred->cr_uid, vp->v_vfsp->vfs_dev);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * execsetid() told us whether or not we had to change the
6130Sstevel@tonic-gate 	 * credentials of the process.  In privflags, it told us
6140Sstevel@tonic-gate 	 * whether we gained any privileges or executed a set-uid executable.
6150Sstevel@tonic-gate 	 */
6160Sstevel@tonic-gate 	setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE));
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * Use /etc/system variable to determine if the stack
6200Sstevel@tonic-gate 	 * should be marked as executable by default.
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	if (noexec_user_stack)
6230Sstevel@tonic-gate 		args->stk_prot &= ~PROT_EXEC;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
6264528Spaulsan 	args->ex_vp = vp;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Traditionally, the setid flags told the sub processes whether
6300Sstevel@tonic-gate 	 * the file just executed was set-uid or set-gid; this caused
6310Sstevel@tonic-gate 	 * some confusion as the 'setid' flag did not match the SUGID
6320Sstevel@tonic-gate 	 * process flag which is only set when the uids/gids do not match.
6330Sstevel@tonic-gate 	 * A script set-gid/set-uid to the real uid/gid would start with
6340Sstevel@tonic-gate 	 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
6350Sstevel@tonic-gate 	 * Now we flag those cases where the calling process cannot
6360Sstevel@tonic-gate 	 * be trusted to influence the newly exec'ed process, either
6370Sstevel@tonic-gate 	 * because it runs with more privileges or when the uids/gids
6380Sstevel@tonic-gate 	 * do in fact not match.
6390Sstevel@tonic-gate 	 * This also makes the runtime linker agree with the on exec
6400Sstevel@tonic-gate 	 * values of SNOCD and SUGID.
6410Sstevel@tonic-gate 	 */
6421335Scasper 	setidfl = 0;
6431335Scasper 	if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
6441335Scasper 	    !supgroupmember(cred->cr_gid, cred))) {
6451335Scasper 		setidfl |= EXECSETID_UGIDS;
6461335Scasper 	}
6471335Scasper 	if (setid & PRIV_SETUGID)
6481335Scasper 		setidfl |= EXECSETID_SETID;
6491335Scasper 	if (setid & PRIV_INCREASE)
6501335Scasper 		setidfl |= EXECSETID_PRIVS;
6511335Scasper 
6520Sstevel@tonic-gate 	error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
6532712Snn35248 		setidfl, exec_file, cred, brand_action);
6540Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
6550Sstevel@tonic-gate 	if (error != 0) {
6560Sstevel@tonic-gate 		if (newcred != NULL)
6570Sstevel@tonic-gate 			crfree(newcred);
6580Sstevel@tonic-gate 		goto bad;
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (level == 0) {
6620Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
6630Sstevel@tonic-gate 		if (newcred != NULL) {
6640Sstevel@tonic-gate 			/*
6650Sstevel@tonic-gate 			 * Free the old credentials, and set the new ones.
6660Sstevel@tonic-gate 			 * Do this for both the process and the (single) thread.
6670Sstevel@tonic-gate 			 */
6680Sstevel@tonic-gate 			crfree(pp->p_cred);
6690Sstevel@tonic-gate 			pp->p_cred = cred;	/* cred already held for proc */
6700Sstevel@tonic-gate 			crhold(cred);		/* hold new cred for thread */
6710Sstevel@tonic-gate 			/*
6720Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
6730Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
6740Sstevel@tonic-gate 			 * allocated cred structure.
6750Sstevel@tonic-gate 			 */
6760Sstevel@tonic-gate 			oldcred = curthread->t_cred;
6770Sstevel@tonic-gate 			curthread->t_cred = cred;
6780Sstevel@tonic-gate 			crfree(oldcred);
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 		/*
6810Sstevel@tonic-gate 		 * On emerging from a successful exec(), the saved
6820Sstevel@tonic-gate 		 * uid and gid equal the effective uid and gid.
6830Sstevel@tonic-gate 		 */
6840Sstevel@tonic-gate 		cred->cr_suid = cred->cr_uid;
6850Sstevel@tonic-gate 		cred->cr_sgid = cred->cr_gid;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * If the real and effective ids do not match, this
6890Sstevel@tonic-gate 		 * is a setuid process that should not dump core.
6900Sstevel@tonic-gate 		 * The group comparison is tricky; we prevent the code
6910Sstevel@tonic-gate 		 * from flagging SNOCD when executing with an effective gid
6920Sstevel@tonic-gate 		 * which is a supplementary group.
6930Sstevel@tonic-gate 		 */
6940Sstevel@tonic-gate 		if (cred->cr_ruid != cred->cr_uid ||
6950Sstevel@tonic-gate 		    (cred->cr_rgid != cred->cr_gid &&
6960Sstevel@tonic-gate 		    !supgroupmember(cred->cr_gid, cred)) ||
6970Sstevel@tonic-gate 		    (privflags & PRIV_INCREASE) != 0)
6980Sstevel@tonic-gate 			suidflags = PSUIDFLAGS;
6990Sstevel@tonic-gate 		else
7000Sstevel@tonic-gate 			suidflags = 0;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
7030Sstevel@tonic-gate 		if (suidflags) {
7040Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
7050Sstevel@tonic-gate 			pp->p_flag |= suidflags;
7060Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
7090Sstevel@tonic-gate 			/*
7100Sstevel@tonic-gate 			 * If process is traced via /proc, arrange to
7110Sstevel@tonic-gate 			 * invalidate the associated /proc vnode.
7120Sstevel@tonic-gate 			 */
7130Sstevel@tonic-gate 			if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
7140Sstevel@tonic-gate 				args->traceinval = 1;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_PTRACE)
7170Sstevel@tonic-gate 			psignal(pp, SIGTRAP);
7180Sstevel@tonic-gate 		if (args->traceinval)
7190Sstevel@tonic-gate 			prinvalidate(&pp->p_user);
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	return (0);
7230Sstevel@tonic-gate bad:
7240Sstevel@tonic-gate 	if (error == 0)
7250Sstevel@tonic-gate 		error = ENOEXEC;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (suidflags) {
7280Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
7290Sstevel@tonic-gate 		pp->p_flag |= suidflags;
7300Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	return (error);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate extern char *execswnames[];
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate struct execsw *
7380Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	int i, j;
7410Sstevel@tonic-gate 	char *ename;
7420Sstevel@tonic-gate 	char *magicp;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	mutex_enter(&execsw_lock);
7450Sstevel@tonic-gate 	for (i = 0; i < nexectype; i++) {
7460Sstevel@tonic-gate 		if (execswnames[i] == NULL) {
7470Sstevel@tonic-gate 			ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7480Sstevel@tonic-gate 			(void) strcpy(ename, name);
7490Sstevel@tonic-gate 			execswnames[i] = ename;
7500Sstevel@tonic-gate 			/*
7510Sstevel@tonic-gate 			 * Set the magic number last so that we
7520Sstevel@tonic-gate 			 * don't need to hold the execsw_lock in
7530Sstevel@tonic-gate 			 * findexectype().
7540Sstevel@tonic-gate 			 */
7550Sstevel@tonic-gate 			magicp = kmem_alloc(magic_size, KM_SLEEP);
7560Sstevel@tonic-gate 			for (j = 0; j < magic_size; j++)
7570Sstevel@tonic-gate 				magicp[j] = magic[j];
7580Sstevel@tonic-gate 			execsw[i].exec_magic = magicp;
7590Sstevel@tonic-gate 			mutex_exit(&execsw_lock);
7600Sstevel@tonic-gate 			return (&execsw[i]);
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	mutex_exit(&execsw_lock);
7640Sstevel@tonic-gate 	return (NULL);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate  * Find the exec switch table entry with the corresponding magic string.
7690Sstevel@tonic-gate  */
7700Sstevel@tonic-gate struct execsw *
7710Sstevel@tonic-gate findexecsw(char *magic)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	struct execsw *eswp;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7760Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7770Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
7780Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
7790Sstevel@tonic-gate 			return (eswp);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	return (NULL);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * Find the execsw[] index for the given exec header string by looking for the
7860Sstevel@tonic-gate  * magic string at a specified offset and length for each kind of executable
7870Sstevel@tonic-gate  * file format until one matches.  If no execsw[] entry is found, try to
7880Sstevel@tonic-gate  * autoload a module for this magic string.
7890Sstevel@tonic-gate  */
7900Sstevel@tonic-gate struct execsw *
7910Sstevel@tonic-gate findexec_by_hdr(char *header)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	struct execsw *eswp;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7960Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7970Sstevel@tonic-gate 		if (header && eswp->exec_maglen != 0 &&
7980Sstevel@tonic-gate 		    bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
7990Sstevel@tonic-gate 			    eswp->exec_maglen) == 0) {
8000Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
8010Sstevel@tonic-gate 				return (NULL);
8020Sstevel@tonic-gate 			return (eswp);
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate /*
8090Sstevel@tonic-gate  * Find the execsw[] index for the given magic string.  If no execsw[] entry
8100Sstevel@tonic-gate  * is found, try to autoload a module for this magic string.
8110Sstevel@tonic-gate  */
8120Sstevel@tonic-gate struct execsw *
8130Sstevel@tonic-gate findexec_by_magic(char *magic)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	struct execsw *eswp;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
8180Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
8190Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
8200Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
8210Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
8220Sstevel@tonic-gate 				return (NULL);
8230Sstevel@tonic-gate 			return (eswp);
8240Sstevel@tonic-gate 		}
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate static int
8300Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	char *name;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	rw_enter(eswp->exec_lock, RW_READER);
8350Sstevel@tonic-gate 	while (!LOADED_EXEC(eswp)) {
8360Sstevel@tonic-gate 		rw_exit(eswp->exec_lock);
8370Sstevel@tonic-gate 		name = execswnames[eswp-execsw];
8380Sstevel@tonic-gate 		ASSERT(name);
8390Sstevel@tonic-gate 		if (modload("exec", name) == -1)
8400Sstevel@tonic-gate 			return (-1);
8410Sstevel@tonic-gate 		rw_enter(eswp->exec_lock, RW_READER);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	return (0);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate static int
8470Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
8500Sstevel@tonic-gate 	uid_t uid, gid;
8510Sstevel@tonic-gate 	cred_t *cr = pp->p_cred;
8520Sstevel@tonic-gate 	int privflags = 0;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/*
8550Sstevel@tonic-gate 	 * Remember credentials.
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	uid = cr->cr_uid;
8580Sstevel@tonic-gate 	gid = cr->cr_gid;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/* Will try to reset the PRIV_AWARE bit later. */
8610Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
8620Sstevel@tonic-gate 		privflags |= PRIV_RESET;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
8650Sstevel@tonic-gate 		/*
8660Sstevel@tonic-gate 		 * Set-uid root execution only allowed if the limit set
8670Sstevel@tonic-gate 		 * holds all unsafe privileges.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 ||
8700Sstevel@tonic-gate 		    priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) {
8710Sstevel@tonic-gate 			uid = vattrp->va_uid;
8720Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 		if (vattrp->va_mode & VSGID) {
8750Sstevel@tonic-gate 			gid = vattrp->va_gid;
8760Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * Do we need to change our credential anyway?
8820Sstevel@tonic-gate 	 * This is the case when E != I or P != I, as
8830Sstevel@tonic-gate 	 * we need to do the assignments (with F empty and A full)
8840Sstevel@tonic-gate 	 * Or when I is not a subset of L; in that case we need to
8850Sstevel@tonic-gate 	 * enforce L.
8860Sstevel@tonic-gate 	 *
8870Sstevel@tonic-gate 	 *		I' = L & I
8880Sstevel@tonic-gate 	 *
8890Sstevel@tonic-gate 	 *		E' = P' = (I' + F) & A
8900Sstevel@tonic-gate 	 * or
8910Sstevel@tonic-gate 	 *		E' = P' = I'
8920Sstevel@tonic-gate 	 */
8930Sstevel@tonic-gate 	if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
8940Sstevel@tonic-gate 	    !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
8950Sstevel@tonic-gate 	    !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
8960Sstevel@tonic-gate 		privflags |= PRIV_RESET;
8970Sstevel@tonic-gate 
8981676Sjpk 	/* If MAC-aware flag(s) are on, need to update cred to remove. */
8991676Sjpk 	if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
9001676Sjpk 	    (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
9011676Sjpk 		privflags |= MAC_FLAGS;
9021676Sjpk 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 * When we introduce the "forced" set then we will need
9050Sstevel@tonic-gate 	 * to set PRIV_INCREASE here if I not a subset of P.
9060Sstevel@tonic-gate 	 * If the "allowed" set is introduced we will need to do
9070Sstevel@tonic-gate 	 * a similar thing; however, it seems more reasonable to
9080Sstevel@tonic-gate 	 * have the allowed set reduce "L": script language interpreters
9090Sstevel@tonic-gate 	 * would typically have an allowed set of "all".
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Set setuid/setgid protections if no ptrace() compatibility.
9140Sstevel@tonic-gate 	 * For privileged processes, honor setuid/setgid even in
9150Sstevel@tonic-gate 	 * the presence of ptrace() compatibility.
9160Sstevel@tonic-gate 	 */
9170Sstevel@tonic-gate 	if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
9180Sstevel@tonic-gate 	    PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
9190Sstevel@tonic-gate 	    (cr->cr_uid != uid ||
9200Sstevel@tonic-gate 	    cr->cr_gid != gid ||
9210Sstevel@tonic-gate 	    cr->cr_suid != uid ||
9220Sstevel@tonic-gate 	    cr->cr_sgid != gid)) {
9230Sstevel@tonic-gate 		*uidp = uid;
9240Sstevel@tonic-gate 		*gidp = gid;
9250Sstevel@tonic-gate 		privflags |= PRIV_SETID;
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 	return (privflags);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate int
9310Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	int error;
9340Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
937*5331Samw 	if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL))
9380Sstevel@tonic-gate 		return (error);
9390Sstevel@tonic-gate 	/*
9400Sstevel@tonic-gate 	 * Check the access mode.
9410Sstevel@tonic-gate 	 * If VPROC, ask /proc if the file is an object file.
9420Sstevel@tonic-gate 	 */
943*5331Samw 	if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 ||
9440Sstevel@tonic-gate 	    !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
9450Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
9460Sstevel@tonic-gate 	    (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
9470Sstevel@tonic-gate 		if (error == 0)
9480Sstevel@tonic-gate 			error = EACCES;
9490Sstevel@tonic-gate 		return (error);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
953*5331Samw 	    (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) {
9540Sstevel@tonic-gate 		/*
9550Sstevel@tonic-gate 		 * If process is under ptrace(2) compatibility,
9560Sstevel@tonic-gate 		 * fail the exec(2).
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_PTRACE)
9590Sstevel@tonic-gate 			goto bad;
9600Sstevel@tonic-gate 		/*
9610Sstevel@tonic-gate 		 * Process is traced via /proc.
9620Sstevel@tonic-gate 		 * Arrange to invalidate the /proc vnode.
9630Sstevel@tonic-gate 		 */
9640Sstevel@tonic-gate 		args->traceinval = 1;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 	return (0);
9670Sstevel@tonic-gate bad:
9680Sstevel@tonic-gate 	if (error == 0)
9690Sstevel@tonic-gate 		error = ENOEXEC;
9700Sstevel@tonic-gate 	return (error);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * Map a section of an executable file into the user's
9750Sstevel@tonic-gate  * address space.
9760Sstevel@tonic-gate  */
9770Sstevel@tonic-gate int
9780Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
9790Sstevel@tonic-gate     off_t offset, int prot, int page, uint_t szc)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	int error = 0;
9820Sstevel@tonic-gate 	off_t oldoffset;
9830Sstevel@tonic-gate 	caddr_t zfodbase, oldaddr;
9840Sstevel@tonic-gate 	size_t end, oldlen;
9850Sstevel@tonic-gate 	size_t zfoddiff;
9860Sstevel@tonic-gate 	label_t ljb;
9870Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	oldaddr = addr;
9900Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
9910Sstevel@tonic-gate 	if (len) {
9920Sstevel@tonic-gate 		oldlen = len;
9930Sstevel@tonic-gate 		len += ((size_t)oldaddr - (size_t)addr);
9940Sstevel@tonic-gate 		oldoffset = offset;
9950Sstevel@tonic-gate 		offset = (off_t)((uintptr_t)offset & PAGEMASK);
9960Sstevel@tonic-gate 		if (page) {
9970Sstevel@tonic-gate 			spgcnt_t  prefltmem, availm, npages;
9980Sstevel@tonic-gate 			int preread;
9990Sstevel@tonic-gate 			uint_t mflag = MAP_PRIVATE | MAP_FIXED;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
10020Sstevel@tonic-gate 				mflag |= MAP_TEXT;
10030Sstevel@tonic-gate 			} else {
10040Sstevel@tonic-gate 				mflag |= MAP_INITDATA;
10050Sstevel@tonic-gate 			}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
10080Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
10090Sstevel@tonic-gate 				error = ENOMEM;
10100Sstevel@tonic-gate 				goto bad;
10110Sstevel@tonic-gate 			}
10120Sstevel@tonic-gate 			if (error = VOP_MAP(vp, (offset_t)offset,
10130Sstevel@tonic-gate 			    p->p_as, &addr, len, prot, PROT_ALL,
1014*5331Samw 			    mflag, CRED(), NULL))
10150Sstevel@tonic-gate 				goto bad;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 			/*
10180Sstevel@tonic-gate 			 * If the segment can fit, then we prefault
10190Sstevel@tonic-gate 			 * the entire segment in.  This is based on the
10200Sstevel@tonic-gate 			 * model that says the best working set of a
10210Sstevel@tonic-gate 			 * small program is all of its pages.
10220Sstevel@tonic-gate 			 */
10230Sstevel@tonic-gate 			npages = (spgcnt_t)btopr(len);
10240Sstevel@tonic-gate 			prefltmem = freemem - desfree;
10250Sstevel@tonic-gate 			preread =
10260Sstevel@tonic-gate 			    (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			/*
10290Sstevel@tonic-gate 			 * If we aren't prefaulting the segment,
10300Sstevel@tonic-gate 			 * increment "deficit", if necessary to ensure
10310Sstevel@tonic-gate 			 * that pages will become available when this
10320Sstevel@tonic-gate 			 * process starts executing.
10330Sstevel@tonic-gate 			 */
10340Sstevel@tonic-gate 			availm = freemem - lotsfree;
10350Sstevel@tonic-gate 			if (preread == 0 && npages > availm &&
10360Sstevel@tonic-gate 			    deficit < lotsfree) {
10370Sstevel@tonic-gate 				deficit += MIN((pgcnt_t)(npages - availm),
10380Sstevel@tonic-gate 				    lotsfree - deficit);
10390Sstevel@tonic-gate 			}
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 			if (preread) {
10420Sstevel@tonic-gate 				TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
10430Sstevel@tonic-gate 				    "execmap preread:freemem %d size %lu",
10440Sstevel@tonic-gate 				    freemem, len);
10450Sstevel@tonic-gate 				(void) as_fault(p->p_as->a_hat, p->p_as,
10460Sstevel@tonic-gate 				    (caddr_t)addr, len, F_INVAL, S_READ);
10470Sstevel@tonic-gate 			}
10480Sstevel@tonic-gate 		} else {
10490Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
10500Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
10510Sstevel@tonic-gate 				error = ENOMEM;
10520Sstevel@tonic-gate 				goto bad;
10530Sstevel@tonic-gate 			}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 			if (error = as_map(p->p_as, addr, len,
10560Sstevel@tonic-gate 			    segvn_create, zfod_argsp))
10570Sstevel@tonic-gate 				goto bad;
10580Sstevel@tonic-gate 			/*
10590Sstevel@tonic-gate 			 * Read in the segment in one big chunk.
10600Sstevel@tonic-gate 			 */
10610Sstevel@tonic-gate 			if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
10620Sstevel@tonic-gate 			    oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
10630Sstevel@tonic-gate 			    (rlim64_t)0, CRED(), (ssize_t *)0))
10640Sstevel@tonic-gate 				goto bad;
10650Sstevel@tonic-gate 			/*
10660Sstevel@tonic-gate 			 * Now set protections.
10670Sstevel@tonic-gate 			 */
10680Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
10690Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)addr,
10700Sstevel@tonic-gate 				    len, prot);
10710Sstevel@tonic-gate 			}
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (zfodlen) {
10762712Snn35248 		struct as *as = curproc->p_as;
10772712Snn35248 		struct seg *seg;
10782712Snn35248 		uint_t zprot = 0;
10792712Snn35248 
10800Sstevel@tonic-gate 		end = (size_t)addr + len;
10810Sstevel@tonic-gate 		zfodbase = (caddr_t)roundup(end, PAGESIZE);
10820Sstevel@tonic-gate 		zfoddiff = (uintptr_t)zfodbase - end;
10830Sstevel@tonic-gate 		if (zfoddiff) {
10842712Snn35248 			/*
10852712Snn35248 			 * Before we go to zero the remaining space on the last
10862712Snn35248 			 * page, make sure we have write permission.
10872712Snn35248 			 */
10882712Snn35248 
10892712Snn35248 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
10902712Snn35248 			seg = as_segat(curproc->p_as, (caddr_t)end);
10912712Snn35248 			if (seg != NULL)
10922712Snn35248 				SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1,
10932712Snn35248 				    &zprot);
10942712Snn35248 			AS_LOCK_EXIT(as, &as->a_lock);
10952712Snn35248 
10962712Snn35248 			if (seg != NULL && (zprot & PROT_WRITE) == 0) {
10972712Snn35248 				(void) as_setprot(as, (caddr_t)end,
10982712Snn35248 				    zfoddiff - 1, zprot | PROT_WRITE);
10992712Snn35248 			}
11002712Snn35248 
11010Sstevel@tonic-gate 			if (on_fault(&ljb)) {
11020Sstevel@tonic-gate 				no_fault();
11032712Snn35248 				if (seg != NULL && (zprot & PROT_WRITE) == 0)
11042712Snn35248 					(void) as_setprot(as, (caddr_t)end,
11052712Snn35248 					zfoddiff - 1, zprot);
11060Sstevel@tonic-gate 				error = EFAULT;
11070Sstevel@tonic-gate 				goto bad;
11080Sstevel@tonic-gate 			}
11090Sstevel@tonic-gate 			uzero((void *)end, zfoddiff);
11100Sstevel@tonic-gate 			no_fault();
11112712Snn35248 			if (seg != NULL && (zprot & PROT_WRITE) == 0)
11122712Snn35248 				(void) as_setprot(as, (caddr_t)end,
11132712Snn35248 				    zfoddiff - 1, zprot);
11140Sstevel@tonic-gate 		}
11150Sstevel@tonic-gate 		if (zfodlen > zfoddiff) {
11160Sstevel@tonic-gate 			struct segvn_crargs crargs =
11170Sstevel@tonic-gate 			    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 			zfodlen -= zfoddiff;
11200Sstevel@tonic-gate 			if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
11210Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
11220Sstevel@tonic-gate 				error = ENOMEM;
11230Sstevel@tonic-gate 				goto bad;
11240Sstevel@tonic-gate 			}
11252991Ssusans 			if (szc > 0) {
11262991Ssusans 				/*
11272991Ssusans 				 * ASSERT alignment because the mapelfexec()
11282991Ssusans 				 * caller for the szc > 0 case extended zfod
11292991Ssusans 				 * so it's end is pgsz aligned.
11302991Ssusans 				 */
11312991Ssusans 				size_t pgsz = page_get_pagesize(szc);
11322991Ssusans 				ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz));
11332991Ssusans 
11342991Ssusans 				if (IS_P2ALIGNED(zfodbase, pgsz)) {
11352991Ssusans 					crargs.szc = szc;
11362991Ssusans 				} else {
11372991Ssusans 					crargs.szc = AS_MAP_HEAP;
11382991Ssusans 				}
11392991Ssusans 			} else {
11402991Ssusans 				crargs.szc = AS_MAP_NO_LPOOB;
11412991Ssusans 			}
11420Sstevel@tonic-gate 			if (error = as_map(p->p_as, (caddr_t)zfodbase,
11430Sstevel@tonic-gate 			    zfodlen, segvn_create, &crargs))
11440Sstevel@tonic-gate 				goto bad;
11450Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
11460Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)zfodbase,
11470Sstevel@tonic-gate 				    zfodlen, prot);
11480Sstevel@tonic-gate 			}
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 	return (0);
11520Sstevel@tonic-gate bad:
11530Sstevel@tonic-gate 	return (error);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate void
11570Sstevel@tonic-gate setexecenv(struct execenv *ep)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
11600Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
11610Sstevel@tonic-gate 	struct vnode *vp;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	p->p_bssbase = ep->ex_bssbase;
11640Sstevel@tonic-gate 	p->p_brkbase = ep->ex_brkbase;
11650Sstevel@tonic-gate 	p->p_brksize = ep->ex_brksize;
11660Sstevel@tonic-gate 	if (p->p_exec)
11670Sstevel@tonic-gate 		VN_RELE(p->p_exec);	/* out with the old */
11680Sstevel@tonic-gate 	vp = p->p_exec = ep->ex_vp;
11690Sstevel@tonic-gate 	if (vp != NULL)
11700Sstevel@tonic-gate 		VN_HOLD(vp);		/* in with the new */
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
11730Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
11740Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate int
11780Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	struct vnode *vp = *vpp;
11810Sstevel@tonic-gate 	file_t *fp;
11820Sstevel@tonic-gate 	int error = 0;
11830Sstevel@tonic-gate 	int filemode = FREAD;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	VN_HOLD(vp);		/* open reference */
11860Sstevel@tonic-gate 	if (error = falloc(NULL, filemode, &fp, fdp)) {
11870Sstevel@tonic-gate 		VN_RELE(vp);
11880Sstevel@tonic-gate 		*fdp = -1;	/* just in case falloc changed value */
11890Sstevel@tonic-gate 		return (error);
11900Sstevel@tonic-gate 	}
1191*5331Samw 	if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) {
11920Sstevel@tonic-gate 		VN_RELE(vp);
11930Sstevel@tonic-gate 		setf(*fdp, NULL);
11940Sstevel@tonic-gate 		unfalloc(fp);
11950Sstevel@tonic-gate 		*fdp = -1;
11960Sstevel@tonic-gate 		return (error);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 	*vpp = vp;		/* vnode should not have changed */
11990Sstevel@tonic-gate 	fp->f_vnode = vp;
12000Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
12010Sstevel@tonic-gate 	setf(*fdp, fp);
12020Sstevel@tonic-gate 	return (0);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate int
12060Sstevel@tonic-gate execclose(int fd)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	return (closeandsetf(fd, NULL));
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate  * noexec stub function.
12140Sstevel@tonic-gate  */
12150Sstevel@tonic-gate /*ARGSUSED*/
12160Sstevel@tonic-gate int
12170Sstevel@tonic-gate noexec(
12180Sstevel@tonic-gate     struct vnode *vp,
12190Sstevel@tonic-gate     struct execa *uap,
12200Sstevel@tonic-gate     struct uarg *args,
12210Sstevel@tonic-gate     struct intpdata *idatap,
12220Sstevel@tonic-gate     int level,
12230Sstevel@tonic-gate     long *execsz,
12240Sstevel@tonic-gate     int setid,
12250Sstevel@tonic-gate     caddr_t exec_file,
12260Sstevel@tonic-gate     struct cred *cred)
12270Sstevel@tonic-gate {
12280Sstevel@tonic-gate 	cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
12290Sstevel@tonic-gate 	return (ENOEXEC);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * Support routines for building a user stack.
12340Sstevel@tonic-gate  *
12350Sstevel@tonic-gate  * execve(path, argv, envp) must construct a new stack with the specified
12360Sstevel@tonic-gate  * arguments and environment variables (see exec_args() for a description
12370Sstevel@tonic-gate  * of the user stack layout).  To do this, we copy the arguments and
12380Sstevel@tonic-gate  * environment variables from the old user address space into the kernel,
12390Sstevel@tonic-gate  * free the old as, create the new as, and copy our buffered information
12400Sstevel@tonic-gate  * to the new stack.  Our kernel buffer has the following structure:
12410Sstevel@tonic-gate  *
12420Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
12430Sstevel@tonic-gate  *	| string offsets	|
12440Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
12450Sstevel@tonic-gate  *	|			|
12460Sstevel@tonic-gate  *	| STK_AVAIL() space	|
12470Sstevel@tonic-gate  *	|			|
12480Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
12490Sstevel@tonic-gate  *	| strings		|
12500Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
12510Sstevel@tonic-gate  *
12520Sstevel@tonic-gate  * When we add a string, we store the string's contents (including the null
12530Sstevel@tonic-gate  * terminator) at stk_strp, and we store the offset of the string relative to
12540Sstevel@tonic-gate  * stk_base at --stk_offp.  At strings are added, stk_strp increases and
12550Sstevel@tonic-gate  * stk_offp decreases.  The amount of space remaining, STK_AVAIL(), is just
12560Sstevel@tonic-gate  * the difference between these pointers.  If we run out of space, we return
12570Sstevel@tonic-gate  * an error and exec_args() starts all over again with a buffer twice as large.
12580Sstevel@tonic-gate  * When we're all done, the kernel buffer looks like this:
12590Sstevel@tonic-gate  *
12600Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
12610Sstevel@tonic-gate  *	| argv[0] offset	|
12620Sstevel@tonic-gate  *	+-----------------------+
12630Sstevel@tonic-gate  *	| ...			|
12640Sstevel@tonic-gate  *	+-----------------------+
12650Sstevel@tonic-gate  *	| argv[argc-1] offset	|
12660Sstevel@tonic-gate  *	+-----------------------+
12670Sstevel@tonic-gate  *	| envp[0] offset	|
12680Sstevel@tonic-gate  *	+-----------------------+
12690Sstevel@tonic-gate  *	| ...			|
12700Sstevel@tonic-gate  *	+-----------------------+
12710Sstevel@tonic-gate  *	| envp[envc-1] offset	|
12720Sstevel@tonic-gate  *	+-----------------------+
12730Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
12740Sstevel@tonic-gate  *	+-----------------------+
12750Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
12760Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
12770Sstevel@tonic-gate  *	|			|
12780Sstevel@tonic-gate  *	| STK_AVAIL() space	|
12790Sstevel@tonic-gate  *	|			|
12800Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
12810Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
12820Sstevel@tonic-gate  *	+-----------------------+
12830Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
12840Sstevel@tonic-gate  *	+-----------------------+
12850Sstevel@tonic-gate  *	| envp[envc-1] string	|
12860Sstevel@tonic-gate  *	+-----------------------+
12870Sstevel@tonic-gate  *	| ...			|
12880Sstevel@tonic-gate  *	+-----------------------+
12890Sstevel@tonic-gate  *	| envp[0] string	|
12900Sstevel@tonic-gate  *	+-----------------------+
12910Sstevel@tonic-gate  *	| argv[argc-1] string	|
12920Sstevel@tonic-gate  *	+-----------------------+
12930Sstevel@tonic-gate  *	| ...			|
12940Sstevel@tonic-gate  *	+-----------------------+
12950Sstevel@tonic-gate  *	| argv[0] string	|
12960Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
12970Sstevel@tonic-gate  */
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate #define	STK_AVAIL(args)		((char *)(args)->stk_offp - (args)->stk_strp)
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate  * Add a string to the stack.
13030Sstevel@tonic-gate  */
13040Sstevel@tonic-gate static int
13050Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate 	int error;
13080Sstevel@tonic-gate 	size_t len;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	if (STK_AVAIL(args) < sizeof (int))
13110Sstevel@tonic-gate 		return (E2BIG);
13120Sstevel@tonic-gate 	*--args->stk_offp = args->stk_strp - args->stk_base;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (segflg == UIO_USERSPACE) {
13150Sstevel@tonic-gate 		error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
13160Sstevel@tonic-gate 		if (error != 0)
13170Sstevel@tonic-gate 			return (error);
13180Sstevel@tonic-gate 	} else {
13190Sstevel@tonic-gate 		len = strlen(sp) + 1;
13200Sstevel@tonic-gate 		if (len > STK_AVAIL(args))
13210Sstevel@tonic-gate 			return (E2BIG);
13220Sstevel@tonic-gate 		bcopy(sp, args->stk_strp, len);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	args->stk_strp += len;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	return (0);
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate static int
13310Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	int error;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	if (args->from_model == DATAMODEL_NATIVE) {
13360Sstevel@tonic-gate 		ulong_t ptr;
13370Sstevel@tonic-gate 		error = fulword(src, &ptr);
13380Sstevel@tonic-gate 		*dst = (caddr_t)ptr;
13390Sstevel@tonic-gate 	} else {
13400Sstevel@tonic-gate 		uint32_t ptr;
13410Sstevel@tonic-gate 		error = fuword32(src, &ptr);
13420Sstevel@tonic-gate 		*dst = (caddr_t)(uintptr_t)ptr;
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 	return (error);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate static int
13480Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
13490Sstevel@tonic-gate {
13500Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE)
13510Sstevel@tonic-gate 		return (sulword(addr, (ulong_t)value));
13520Sstevel@tonic-gate 	else
13530Sstevel@tonic-gate 		return (suword32(addr, (uint32_t)(uintptr_t)value));
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate static int
13570Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
13580Sstevel@tonic-gate {
13590Sstevel@tonic-gate 	char *sp;
13600Sstevel@tonic-gate 	int argc, error;
13610Sstevel@tonic-gate 	int argv_empty = 0;
13620Sstevel@tonic-gate 	size_t ptrsize = args->from_ptrsize;
13630Sstevel@tonic-gate 	size_t size, pad;
13640Sstevel@tonic-gate 	char *argv = (char *)uap->argp;
13650Sstevel@tonic-gate 	char *envp = (char *)uap->envp;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * Copy interpreter's name and argument to argv[0] and argv[1].
13690Sstevel@tonic-gate 	 */
13700Sstevel@tonic-gate 	if (intp != NULL && intp->intp_name != NULL) {
13710Sstevel@tonic-gate 		if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
13720Sstevel@tonic-gate 			return (error);
13730Sstevel@tonic-gate 		if (intp->intp_arg != NULL &&
13740Sstevel@tonic-gate 		    (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
13750Sstevel@tonic-gate 			return (error);
13760Sstevel@tonic-gate 		if (args->fname != NULL)
13770Sstevel@tonic-gate 			error = stk_add(args, args->fname, UIO_SYSSPACE);
13780Sstevel@tonic-gate 		else
13790Sstevel@tonic-gate 			error = stk_add(args, uap->fname, UIO_USERSPACE);
13800Sstevel@tonic-gate 		if (error)
13810Sstevel@tonic-gate 			return (error);
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		/*
13840Sstevel@tonic-gate 		 * Check for an empty argv[].
13850Sstevel@tonic-gate 		 */
13860Sstevel@tonic-gate 		if (stk_getptr(args, argv, &sp))
13870Sstevel@tonic-gate 			return (EFAULT);
13880Sstevel@tonic-gate 		if (sp == NULL)
13890Sstevel@tonic-gate 			argv_empty = 1;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		argv += ptrsize;		/* ignore original argv[0] */
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (argv_empty == 0) {
13950Sstevel@tonic-gate 		/*
13960Sstevel@tonic-gate 		 * Add argv[] strings to the stack.
13970Sstevel@tonic-gate 		 */
13980Sstevel@tonic-gate 		for (;;) {
13990Sstevel@tonic-gate 			if (stk_getptr(args, argv, &sp))
14000Sstevel@tonic-gate 				return (EFAULT);
14010Sstevel@tonic-gate 			if (sp == NULL)
14020Sstevel@tonic-gate 				break;
14030Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
14040Sstevel@tonic-gate 				return (error);
14050Sstevel@tonic-gate 			argv += ptrsize;
14060Sstevel@tonic-gate 		}
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 	argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
14090Sstevel@tonic-gate 	args->arglen = args->stk_strp - args->stk_base;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/*
14120Sstevel@tonic-gate 	 * Add environ[] strings to the stack.
14130Sstevel@tonic-gate 	 */
14140Sstevel@tonic-gate 	if (envp != NULL) {
14150Sstevel@tonic-gate 		for (;;) {
14160Sstevel@tonic-gate 			if (stk_getptr(args, envp, &sp))
14170Sstevel@tonic-gate 				return (EFAULT);
14180Sstevel@tonic-gate 			if (sp == NULL)
14190Sstevel@tonic-gate 				break;
14200Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
14210Sstevel@tonic-gate 				return (error);
14220Sstevel@tonic-gate 			envp += ptrsize;
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
14260Sstevel@tonic-gate 	args->ne = args->na - argc;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	/*
14292712Snn35248 	 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and
14302712Snn35248 	 * AT_SUN_EMULATOR strings to the stack.
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
14330Sstevel@tonic-gate 		if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
14340Sstevel@tonic-gate 			return (error);
14350Sstevel@tonic-gate 		if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
14360Sstevel@tonic-gate 			return (error);
14372712Snn35248 		if (args->brandname != NULL &&
14382712Snn35248 		    (error = stk_add(args, args->brandname,
14392712Snn35248 			UIO_SYSSPACE)) != 0)
14402712Snn35248 			return (error);
14412712Snn35248 		if (args->emulator != NULL &&
14422712Snn35248 		    (error = stk_add(args, args->emulator,
14432712Snn35248 			UIO_SYSSPACE)) != 0)
14442712Snn35248 			return (error);
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	/*
14480Sstevel@tonic-gate 	 * Compute the size of the stack.  This includes all the pointers,
14490Sstevel@tonic-gate 	 * the space reserved for the aux vector, and all the strings.
14500Sstevel@tonic-gate 	 * The total number of pointers is args->na (which is argc + envc)
14510Sstevel@tonic-gate 	 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
14520Sstevel@tonic-gate 	 * after the last argument (i.e. argv[argc]); (3) the NULL after the
14530Sstevel@tonic-gate 	 * last environment variable (i.e. envp[envc]); and (4) the NULL after
14540Sstevel@tonic-gate 	 * all the strings, at the very top of the stack.
14550Sstevel@tonic-gate 	 */
14560Sstevel@tonic-gate 	size = (args->na + 4) * args->to_ptrsize + args->auxsize +
14570Sstevel@tonic-gate 	    (args->stk_strp - args->stk_base);
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	/*
14600Sstevel@tonic-gate 	 * Pad the string section with zeroes to align the stack size.
14610Sstevel@tonic-gate 	 */
14620Sstevel@tonic-gate 	pad = P2NPHASE(size, args->stk_align);
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	if (STK_AVAIL(args) < pad)
14650Sstevel@tonic-gate 		return (E2BIG);
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	args->usrstack_size = size + pad;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	while (pad-- != 0)
14700Sstevel@tonic-gate 		*args->stk_strp++ = 0;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	args->nc = args->stk_strp - args->stk_base;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	return (0);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate static int
14780Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate 	size_t ptrsize = args->to_ptrsize;
14810Sstevel@tonic-gate 	ssize_t pslen;
14820Sstevel@tonic-gate 	char *kstrp = args->stk_base;
14830Sstevel@tonic-gate 	char *ustrp = usrstack - args->nc - ptrsize;
14840Sstevel@tonic-gate 	char *usp = usrstack - args->usrstack_size;
14850Sstevel@tonic-gate 	int *offp = (int *)(args->stk_base + args->stk_size);
14860Sstevel@tonic-gate 	int envc = args->ne;
14870Sstevel@tonic-gate 	int argc = args->na - envc;
14880Sstevel@tonic-gate 	int i;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * Record argc for /proc.
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	up->u_argc = argc;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	/*
14960Sstevel@tonic-gate 	 * Put argc on the stack.  Note that even though it's an int,
14970Sstevel@tonic-gate 	 * it always consumes ptrsize bytes (for alignment).
14980Sstevel@tonic-gate 	 */
14990Sstevel@tonic-gate 	if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
15000Sstevel@tonic-gate 		return (-1);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * Add argc space (ptrsize) to usp and record argv for /proc.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	up->u_argv = (uintptr_t)(usp += ptrsize);
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	/*
15080Sstevel@tonic-gate 	 * Put the argv[] pointers on the stack.
15090Sstevel@tonic-gate 	 */
15100Sstevel@tonic-gate 	for (i = 0; i < argc; i++, usp += ptrsize)
15110Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
15120Sstevel@tonic-gate 			return (-1);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	/*
15150Sstevel@tonic-gate 	 * Copy arguments to u_psargs.
15160Sstevel@tonic-gate 	 */
15170Sstevel@tonic-gate 	pslen = MIN(args->arglen, PSARGSZ) - 1;
15180Sstevel@tonic-gate 	for (i = 0; i < pslen; i++)
15190Sstevel@tonic-gate 		up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
15200Sstevel@tonic-gate 	while (i < PSARGSZ)
15210Sstevel@tonic-gate 		up->u_psargs[i++] = '\0';
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * Add space for argv[]'s NULL terminator (ptrsize) to usp and
15250Sstevel@tonic-gate 	 * record envp for /proc.
15260Sstevel@tonic-gate 	 */
15270Sstevel@tonic-gate 	up->u_envp = (uintptr_t)(usp += ptrsize);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * Put the envp[] pointers on the stack.
15310Sstevel@tonic-gate 	 */
15320Sstevel@tonic-gate 	for (i = 0; i < envc; i++, usp += ptrsize)
15330Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
15340Sstevel@tonic-gate 			return (-1);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/*
15370Sstevel@tonic-gate 	 * Add space for envp[]'s NULL terminator (ptrsize) to usp and
15380Sstevel@tonic-gate 	 * remember where the stack ends, which is also where auxv begins.
15390Sstevel@tonic-gate 	 */
15400Sstevel@tonic-gate 	args->stackend = usp += ptrsize;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * Put all the argv[], envp[], and auxv strings on the stack.
15440Sstevel@tonic-gate 	 */
15450Sstevel@tonic-gate 	if (copyout(args->stk_base, ustrp, args->nc))
15460Sstevel@tonic-gate 		return (-1);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	/*
15490Sstevel@tonic-gate 	 * Fill in the aux vector now that we know the user stack addresses
15502712Snn35248 	 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and
15512712Snn35248 	 * AT_SUN_EMULATOR strings.
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
15540Sstevel@tonic-gate 		if (args->to_model == DATAMODEL_NATIVE) {
15550Sstevel@tonic-gate 			auxv_t **a = (auxv_t **)auxvpp;
15560Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
15570Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
15582712Snn35248 			if (args->brandname != NULL)
15592712Snn35248 				ADDAUX(*a,
15602712Snn35248 				    AT_SUN_BRANDNAME, (long)&ustrp[*--offp])
15612712Snn35248 			if (args->emulator != NULL)
15622712Snn35248 				ADDAUX(*a,
15632712Snn35248 				    AT_SUN_EMULATOR, (long)&ustrp[*--offp])
15640Sstevel@tonic-gate 		} else {
15650Sstevel@tonic-gate 			auxv32_t **a = (auxv32_t **)auxvpp;
15660Sstevel@tonic-gate 			ADDAUX(*a,
15670Sstevel@tonic-gate 			    AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
15680Sstevel@tonic-gate 			ADDAUX(*a,
15692712Snn35248 			    AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp])
15702712Snn35248 			if (args->brandname != NULL)
15712712Snn35248 				ADDAUX(*a, AT_SUN_BRANDNAME,
15722712Snn35248 				    (int)(uintptr_t)&ustrp[*--offp])
15732712Snn35248 			if (args->emulator != NULL)
15742712Snn35248 				ADDAUX(*a, AT_SUN_EMULATOR,
15752712Snn35248 				    (int)(uintptr_t)&ustrp[*--offp])
15760Sstevel@tonic-gate 		}
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	return (0);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate /*
15830Sstevel@tonic-gate  * Initialize a new user stack with the specified arguments and environment.
15840Sstevel@tonic-gate  * The initial user stack layout is as follows:
15850Sstevel@tonic-gate  *
15860Sstevel@tonic-gate  *	User Stack
15870Sstevel@tonic-gate  *	+---------------+ <--- curproc->p_usrstack
15883177Sdp78419  *	|		|
15893177Sdp78419  *	| slew		|
15903177Sdp78419  *	|		|
15913177Sdp78419  *	+---------------+
15920Sstevel@tonic-gate  *	| NULL		|
15930Sstevel@tonic-gate  *	+---------------+
15940Sstevel@tonic-gate  *	|		|
15950Sstevel@tonic-gate  *	| auxv strings	|
15960Sstevel@tonic-gate  *	|		|
15970Sstevel@tonic-gate  *	+---------------+
15980Sstevel@tonic-gate  *	|		|
15990Sstevel@tonic-gate  *	| envp strings	|
16000Sstevel@tonic-gate  *	|		|
16010Sstevel@tonic-gate  *	+---------------+
16020Sstevel@tonic-gate  *	|		|
16030Sstevel@tonic-gate  *	| argv strings	|
16040Sstevel@tonic-gate  *	|		|
16050Sstevel@tonic-gate  *	+---------------+ <--- ustrp
16060Sstevel@tonic-gate  *	|		|
16070Sstevel@tonic-gate  *	| aux vector	|
16080Sstevel@tonic-gate  *	|		|
16090Sstevel@tonic-gate  *	+---------------+ <--- auxv
16100Sstevel@tonic-gate  *	| NULL		|
16110Sstevel@tonic-gate  *	+---------------+
16120Sstevel@tonic-gate  *	| envp[envc-1]	|
16130Sstevel@tonic-gate  *	+---------------+
16140Sstevel@tonic-gate  *	| ...		|
16150Sstevel@tonic-gate  *	+---------------+
16160Sstevel@tonic-gate  *	| envp[0]	|
16170Sstevel@tonic-gate  *	+---------------+ <--- envp[]
16180Sstevel@tonic-gate  *	| NULL		|
16190Sstevel@tonic-gate  *	+---------------+
16200Sstevel@tonic-gate  *	| argv[argc-1]	|
16210Sstevel@tonic-gate  *	+---------------+
16220Sstevel@tonic-gate  *	| ...		|
16230Sstevel@tonic-gate  *	+---------------+
16240Sstevel@tonic-gate  *	| argv[0]	|
16250Sstevel@tonic-gate  *	+---------------+ <--- argv[]
16260Sstevel@tonic-gate  *	| argc		|
16270Sstevel@tonic-gate  *	+---------------+ <--- stack base
16280Sstevel@tonic-gate  */
16290Sstevel@tonic-gate int
16300Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	size_t size;
16330Sstevel@tonic-gate 	int error;
16340Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
16350Sstevel@tonic-gate 	user_t *up = PTOU(p);
16360Sstevel@tonic-gate 	char *usrstack;
16370Sstevel@tonic-gate 	rctl_entity_p_t e;
16380Sstevel@tonic-gate 	struct as *as;
16392991Ssusans 	extern int use_stk_lpg;
16403177Sdp78419 	size_t sp_slew;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	args->from_model = p->p_model;
16430Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
16440Sstevel@tonic-gate 		args->from_ptrsize = sizeof (long);
16450Sstevel@tonic-gate 	} else {
16460Sstevel@tonic-gate 		args->from_ptrsize = sizeof (int32_t);
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE) {
16500Sstevel@tonic-gate 		args->to_ptrsize = sizeof (long);
16510Sstevel@tonic-gate 		args->ncargs = NCARGS;
16520Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN;
16530Sstevel@tonic-gate 		usrstack = (char *)USRSTACK;
16540Sstevel@tonic-gate 	} else {
16550Sstevel@tonic-gate 		args->to_ptrsize = sizeof (int32_t);
16560Sstevel@tonic-gate 		args->ncargs = NCARGS32;
16570Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN32;
16580Sstevel@tonic-gate 		usrstack = (char *)USRSTACK32;
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate #if defined(__sparc)
16640Sstevel@tonic-gate 	/*
16650Sstevel@tonic-gate 	 * Make sure user register windows are empty before
16660Sstevel@tonic-gate 	 * attempting to make a new stack.
16670Sstevel@tonic-gate 	 */
16680Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
16690Sstevel@tonic-gate #endif
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	for (size = PAGESIZE; ; size *= 2) {
16720Sstevel@tonic-gate 		args->stk_size = size;
16730Sstevel@tonic-gate 		args->stk_base = kmem_alloc(size, KM_SLEEP);
16740Sstevel@tonic-gate 		args->stk_strp = args->stk_base;
16750Sstevel@tonic-gate 		args->stk_offp = (int *)(args->stk_base + size);
16760Sstevel@tonic-gate 		error = stk_copyin(uap, args, intp, auxvpp);
16770Sstevel@tonic-gate 		if (error == 0)
16780Sstevel@tonic-gate 			break;
16790Sstevel@tonic-gate 		kmem_free(args->stk_base, size);
16800Sstevel@tonic-gate 		if (error != E2BIG && error != ENAMETOOLONG)
16810Sstevel@tonic-gate 			return (error);
16820Sstevel@tonic-gate 		if (size >= args->ncargs)
16830Sstevel@tonic-gate 			return (E2BIG);
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	size = args->usrstack_size;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	ASSERT(error == 0);
16890Sstevel@tonic-gate 	ASSERT(P2PHASE(size, args->stk_align) == 0);
16900Sstevel@tonic-gate 	ASSERT((ssize_t)STK_AVAIL(args) >= 0);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	if (size > args->ncargs) {
16930Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
16940Sstevel@tonic-gate 		return (E2BIG);
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	/*
16980Sstevel@tonic-gate 	 * Leave only the current lwp and force the other lwps to exit.
16990Sstevel@tonic-gate 	 * If another lwp beat us to the punch by calling exit(), bail out.
17000Sstevel@tonic-gate 	 */
17010Sstevel@tonic-gate 	if ((error = exitlwps(0)) != 0) {
17020Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
17030Sstevel@tonic-gate 		return (error);
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	/*
17070Sstevel@tonic-gate 	 * Revoke any doors created by the process.
17080Sstevel@tonic-gate 	 */
17090Sstevel@tonic-gate 	if (p->p_door_list)
17100Sstevel@tonic-gate 		door_exit();
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	/*
17130Sstevel@tonic-gate 	 * Release schedctl data structures.
17140Sstevel@tonic-gate 	 */
17150Sstevel@tonic-gate 	if (p->p_pagep)
17160Sstevel@tonic-gate 		schedctl_proc_cleanup();
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	/*
17190Sstevel@tonic-gate 	 * Clean up any DTrace helpers for the process.
17200Sstevel@tonic-gate 	 */
17210Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
17220Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
17230Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17270Sstevel@tonic-gate 	/*
17280Sstevel@tonic-gate 	 * Cleanup the DTrace provider associated with this process.
17290Sstevel@tonic-gate 	 */
17300Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
17310Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exec_ptr != NULL);
17320Sstevel@tonic-gate 		dtrace_fasttrap_exec_ptr(p);
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/*
17370Sstevel@tonic-gate 	 * discard the lwpchan cache.
17380Sstevel@tonic-gate 	 */
17390Sstevel@tonic-gate 	if (p->p_lcp != NULL)
17400Sstevel@tonic-gate 		lwpchan_destroy_cache(1);
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	/*
17430Sstevel@tonic-gate 	 * Delete the POSIX timers.
17440Sstevel@tonic-gate 	 */
17450Sstevel@tonic-gate 	if (p->p_itimer != NULL)
17460Sstevel@tonic-gate 		timer_exit();
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate #ifdef C2_AUDIT
17490Sstevel@tonic-gate 	if (audit_active)
17500Sstevel@tonic-gate 		audit_exec(args->stk_base, args->stk_base + args->arglen,
17510Sstevel@tonic-gate 		    args->na - args->ne, args->ne);
17520Sstevel@tonic-gate #endif
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/*
17550Sstevel@tonic-gate 	 * Ensure that we don't change resource associations while we
17560Sstevel@tonic-gate 	 * change address spaces.
17570Sstevel@tonic-gate 	 */
17580Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17590Sstevel@tonic-gate 	pool_barrier_enter();
17600Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/*
17630Sstevel@tonic-gate 	 * Destroy the old address space and create a new one.
17640Sstevel@tonic-gate 	 * From here on, any errors are fatal to the exec()ing process.
17650Sstevel@tonic-gate 	 * On error we return -1, which means the caller must SIGKILL
17660Sstevel@tonic-gate 	 * the process.
17670Sstevel@tonic-gate 	 */
17680Sstevel@tonic-gate 	relvm();
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17710Sstevel@tonic-gate 	pool_barrier_exit();
17720Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	up->u_execsw = args->execswp;
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	p->p_brkbase = NULL;
17770Sstevel@tonic-gate 	p->p_brksize = 0;
17782991Ssusans 	p->p_brkpageszc = 0;
17790Sstevel@tonic-gate 	p->p_stksize = 0;
17802991Ssusans 	p->p_stkpageszc = 0;
17810Sstevel@tonic-gate 	p->p_model = args->to_model;
17820Sstevel@tonic-gate 	p->p_usrstack = usrstack;
17830Sstevel@tonic-gate 	p->p_stkprot = args->stk_prot;
17840Sstevel@tonic-gate 	p->p_datprot = args->dat_prot;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	/*
17870Sstevel@tonic-gate 	 * Reset resource controls such that all controls are again active as
17880Sstevel@tonic-gate 	 * well as appropriate to the potentially new address model for the
17890Sstevel@tonic-gate 	 * process.
17900Sstevel@tonic-gate 	 */
17910Sstevel@tonic-gate 	e.rcep_p.proc = p;
17920Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
17930Sstevel@tonic-gate 	rctl_set_reset(p->p_rctls, p, &e);
17940Sstevel@tonic-gate 
17952991Ssusans 	/* Too early to call map_pgsz for the heap */
17962991Ssusans 	if (use_stk_lpg) {
17972991Ssusans 		p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0));
17982991Ssusans 	}
17990Sstevel@tonic-gate 
18002991Ssusans 	mutex_enter(&p->p_lock);
18012991Ssusans 	p->p_flag |= SAUTOLPG;	/* kernel controls page sizes */
18022991Ssusans 	mutex_exit(&p->p_lock);
18030Sstevel@tonic-gate 
18043177Sdp78419 	/*
18053177Sdp78419 	 * Some platforms may choose to randomize real stack start by adding a
18063177Sdp78419 	 * small slew (not more than a few hundred bytes) to the top of the
18073177Sdp78419 	 * stack. This helps avoid cache thrashing when identical processes
18083177Sdp78419 	 * simultaneously share caches that don't provide enough associativity
18093177Sdp78419 	 * (e.g. sun4v systems). In this case stack slewing makes the same hot
18103177Sdp78419 	 * stack variables in different processes to live in different cache
18113177Sdp78419 	 * sets increasing effective associativity.
18123177Sdp78419 	 */
18133177Sdp78419 	sp_slew = exec_get_spslew();
18143177Sdp78419 	ASSERT(P2PHASE(sp_slew, args->stk_align) == 0);
18153177Sdp78419 	exec_set_sp(size + sp_slew);
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 	as = as_alloc();
18180Sstevel@tonic-gate 	p->p_as = as;
18192768Ssl108498 	as->a_proc = p;
18200Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
18210Sstevel@tonic-gate 		as->a_userlimit = (caddr_t)USERLIMIT32;
18220Sstevel@tonic-gate 	(void) hat_setup(as->a_hat, HAT_ALLOC);
18234528Spaulsan 	hat_join_srd(as->a_hat, args->ex_vp);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	/*
18260Sstevel@tonic-gate 	 * Finally, write out the contents of the new stack.
18270Sstevel@tonic-gate 	 */
18283177Sdp78419 	error = stk_copyout(args, usrstack - sp_slew, auxvpp, up);
18290Sstevel@tonic-gate 	kmem_free(args->stk_base, args->stk_size);
18300Sstevel@tonic-gate 	return (error);
18310Sstevel@tonic-gate }
1832