xref: /titanic_51/usr/src/uts/common/os/exec.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a3d40fb8Scasper  * Common Development and Distribution License (the "License").
6a3d40fb8Scasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220baeff3dSrab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/policy.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <sys/acct.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
517c478bd9Sstevel@tonic-gate #include <sys/vm.h>
527c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
537c478bd9Sstevel@tonic-gate #include <sys/exec.h>
547c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
557c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
567c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
597c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
607c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
617c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
627c478bd9Sstevel@tonic-gate #include <sys/stack.h>
637c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
647c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
657c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
667c478bd9Sstevel@tonic-gate #include <sys/pool.h>
677c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <c2/audit.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <vm/hat.h>
727c478bd9Sstevel@tonic-gate #include <vm/anon.h>
737c478bd9Sstevel@tonic-gate #include <vm/as.h>
747c478bd9Sstevel@tonic-gate #include <vm/seg.h>
757c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	PRIV_RESET		0x01	/* needs to reset privs */
787c478bd9Sstevel@tonic-gate #define	PRIV_SETID		0x02	/* needs to change uids */
797c478bd9Sstevel@tonic-gate #define	PRIV_SETUGID		0x04	/* is setuid/setgid/forced privs */
807c478bd9Sstevel@tonic-gate #define	PRIV_INCREASE		0x08	/* child runs with more privs */
81*45916cd2Sjpk #define	MAC_FLAGS		0x10	/* need to adjust MAC flags */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *);
847c478bd9Sstevel@tonic-gate static int hold_execsw(struct execsw *);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate uint_t auxv_hwcap = 0;	/* auxv AT_SUN_HWCAP value; determined on the fly */
877c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
887c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32 = 0;	/* 32-bit version of auxv_hwcap */
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int exec_lpg_disable = 0;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	PSUIDFLAGS		(SNOCD|SUGID)
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * exec() - wrapper around exece providing NULL environment pointer
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate int
997c478bd9Sstevel@tonic-gate exec(const char *fname, const char **argp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	return (exece(fname, argp, NULL));
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * exece() - system call wrapper around exec_common()
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate int
1087c478bd9Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	int error;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	error = exec_common(fname, argp, envp);
1137c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : 0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate exec_common(const char *fname, const char **argp, const char **envp)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
1207c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1217c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1227c478bd9Sstevel@tonic-gate 	struct user *up = PTOU(p);
1237c478bd9Sstevel@tonic-gate 	long execsz;		/* temporary count of exec size */
1247c478bd9Sstevel@tonic-gate 	int i;
1257c478bd9Sstevel@tonic-gate 	int error;
1267c478bd9Sstevel@tonic-gate 	char exec_file[MAXCOMLEN+1];
1277c478bd9Sstevel@tonic-gate 	struct pathname pn;
1287c478bd9Sstevel@tonic-gate 	struct pathname resolvepn;
1297c478bd9Sstevel@tonic-gate 	struct uarg args;
1307c478bd9Sstevel@tonic-gate 	struct execa ua;
1317c478bd9Sstevel@tonic-gate 	k_sigset_t savedmask;
1327c478bd9Sstevel@tonic-gate 	lwpdir_t *lwpdir = NULL;
1337c478bd9Sstevel@tonic-gate 	lwpdir_t **tidhash;
1347c478bd9Sstevel@tonic-gate 	lwpdir_t *old_lwpdir = NULL;
1357c478bd9Sstevel@tonic-gate 	uint_t old_lwpdir_sz;
1367c478bd9Sstevel@tonic-gate 	lwpdir_t **old_tidhash;
1377c478bd9Sstevel@tonic-gate 	uint_t old_tidhash_sz;
1387c478bd9Sstevel@tonic-gate 	lwpent_t *lep;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * exec() is not supported for the /proc agent lwp.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (curthread == p->p_agenttp)
1447c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_basic_exec(CRED())) != 0)
1477c478bd9Sstevel@tonic-gate 		return (error);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Inform /proc that an exec() has started.
1517c478bd9Sstevel@tonic-gate 	 * Hold signals that are ignored by default so that we will
1527c478bd9Sstevel@tonic-gate 	 * not be interrupted by a signal that will be ignored after
1537c478bd9Sstevel@tonic-gate 	 * successful completion of gexec().
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1567c478bd9Sstevel@tonic-gate 	prexecstart();
1577c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
1587c478bd9Sstevel@tonic-gate 	savedmask = curthread->t_hold;
1597c478bd9Sstevel@tonic-gate 	sigorset(&curthread->t_hold, &ignoredefault);
1607c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * Look up path name and remember last component for later.
1647c478bd9Sstevel@tonic-gate 	 * To help coreadm expand its %d token, we attempt to save
1657c478bd9Sstevel@tonic-gate 	 * the directory containing the executable in p_execdir. The
1667c478bd9Sstevel@tonic-gate 	 * first call to lookuppn() may fail and return EINVAL because
1677c478bd9Sstevel@tonic-gate 	 * dirvpp is non-NULL. In that case, we make a second call to
1687c478bd9Sstevel@tonic-gate 	 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
1697c478bd9Sstevel@tonic-gate 	 * but coreadm is allowed to expand %d to the empty string and
1707c478bd9Sstevel@tonic-gate 	 * there are other cases in which that failure may occur.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1737c478bd9Sstevel@tonic-gate 		goto out;
1747c478bd9Sstevel@tonic-gate 	pn_alloc(&resolvepn);
1757c478bd9Sstevel@tonic-gate 	if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
1767c478bd9Sstevel@tonic-gate 		pn_free(&resolvepn);
1777c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1787c478bd9Sstevel@tonic-gate 		if (error != EINVAL)
1797c478bd9Sstevel@tonic-gate 			goto out;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		dir = NULL;
1827c478bd9Sstevel@tonic-gate 		if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1837c478bd9Sstevel@tonic-gate 			goto out;
1847c478bd9Sstevel@tonic-gate 		pn_alloc(&resolvepn);
1857c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
1867c478bd9Sstevel@tonic-gate 		    &vp)) != 0) {
1877c478bd9Sstevel@tonic-gate 			pn_free(&resolvepn);
1887c478bd9Sstevel@tonic-gate 			pn_free(&pn);
1897c478bd9Sstevel@tonic-gate 			goto out;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
1937c478bd9Sstevel@tonic-gate 		if (dir != NULL)
1947c478bd9Sstevel@tonic-gate 			VN_RELE(dir);
1957c478bd9Sstevel@tonic-gate 		error = ENOENT;
1967c478bd9Sstevel@tonic-gate 		pn_free(&resolvepn);
1977c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1987c478bd9Sstevel@tonic-gate 		goto out;
1997c478bd9Sstevel@tonic-gate 	}
200a3d40fb8Scasper 
201a3d40fb8Scasper 	/*
202a3d40fb8Scasper 	 * We do not allow executing files in attribute directories.
203a3d40fb8Scasper 	 * We test this by determining whether the resolved path
204a3d40fb8Scasper 	 * contains a "/" when we're in an attribute directory;
205a3d40fb8Scasper 	 * only if the pathname does not contain a "/" the resolved path
206a3d40fb8Scasper 	 * points to a file in the current working (attribute) directory.
207a3d40fb8Scasper 	 */
208a3d40fb8Scasper 	if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
209a3d40fb8Scasper 	    strchr(resolvepn.pn_path, '/') == NULL) {
210a3d40fb8Scasper 		if (dir != NULL)
211a3d40fb8Scasper 			VN_RELE(dir);
212a3d40fb8Scasper 		error = EACCES;
213a3d40fb8Scasper 		pn_free(&resolvepn);
214a3d40fb8Scasper 		pn_free(&pn);
215a3d40fb8Scasper 		VN_RELE(vp);
216a3d40fb8Scasper 		goto out;
217a3d40fb8Scasper 	}
218a3d40fb8Scasper 
2197c478bd9Sstevel@tonic-gate 	bzero(exec_file, MAXCOMLEN+1);
2207c478bd9Sstevel@tonic-gate 	(void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
2217c478bd9Sstevel@tonic-gate 	bzero(&args, sizeof (args));
2227c478bd9Sstevel@tonic-gate 	args.pathname = resolvepn.pn_path;
2237c478bd9Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
2247c478bd9Sstevel@tonic-gate 	pn_free(&pn);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Specific exec handlers, or policies determined via
2287c478bd9Sstevel@tonic-gate 	 * /etc/system may override the historical default.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	args.stk_prot = PROT_ZFOD;
2317c478bd9Sstevel@tonic-gate 	args.dat_prot = PROT_ZFOD;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	CPU_STATS_ADD_K(sys, sysexec, 1);
2347c478bd9Sstevel@tonic-gate 	DTRACE_PROC1(exec, char *, args.pathname);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	ua.fname = fname;
2377c478bd9Sstevel@tonic-gate 	ua.argp = argp;
2387c478bd9Sstevel@tonic-gate 	ua.envp = envp;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
2417c478bd9Sstevel@tonic-gate 	    exec_file, p->p_cred)) != 0) {
2427c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
2437c478bd9Sstevel@tonic-gate 		if (dir != NULL)
2447c478bd9Sstevel@tonic-gate 			VN_RELE(dir);
2457c478bd9Sstevel@tonic-gate 		pn_free(&resolvepn);
2467c478bd9Sstevel@tonic-gate 		goto fail;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Free floating point registers (sun4u only)
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	ASSERT(lwp != NULL);
2537c478bd9Sstevel@tonic-gate 	lwp_freeregs(lwp, 1);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2560baeff3dSrab 	 * Free thread and process context ops.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	if (curthread->t_ctx)
2597c478bd9Sstevel@tonic-gate 		freectx(curthread, 1);
2600baeff3dSrab 	if (p->p_pctx)
2610baeff3dSrab 		freepctx(p, 1);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Remember file name for accounting; clear any cached DTrace predicate.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	up->u_acflag &= ~AFORK;
2677c478bd9Sstevel@tonic-gate 	bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
2687c478bd9Sstevel@tonic-gate 	curthread->t_predcache = NULL;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Clear contract template state
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	lwp_ctmpl_clear(lwp);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Save the directory in which we found the executable for expanding
2777c478bd9Sstevel@tonic-gate 	 * the %d token used in core file patterns.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2807c478bd9Sstevel@tonic-gate 	tmpvp = p->p_execdir;
2817c478bd9Sstevel@tonic-gate 	p->p_execdir = dir;
2827c478bd9Sstevel@tonic-gate 	if (p->p_execdir != NULL)
2837c478bd9Sstevel@tonic-gate 		VN_HOLD(p->p_execdir);
2847c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (tmpvp != NULL)
2877c478bd9Sstevel@tonic-gate 		VN_RELE(tmpvp);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Reset stack state to the user stack, clear set of signals
2917c478bd9Sstevel@tonic-gate 	 * caught on the signal stack, and reset list of signals that
2927c478bd9Sstevel@tonic-gate 	 * restart system calls; the new program's environment should
2937c478bd9Sstevel@tonic-gate 	 * not be affected by detritus from the old program.  Any
2947c478bd9Sstevel@tonic-gate 	 * pending held signals remain held, so don't clear t_hold.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2977c478bd9Sstevel@tonic-gate 	lwp->lwp_oldcontext = 0;
2987c478bd9Sstevel@tonic-gate 	lwp->lwp_ustack = 0;
2997c478bd9Sstevel@tonic-gate 	lwp->lwp_old_stk_ctl = 0;
3007c478bd9Sstevel@tonic-gate 	sigemptyset(&up->u_signodefer);
3017c478bd9Sstevel@tonic-gate 	sigemptyset(&up->u_sigonstack);
3027c478bd9Sstevel@tonic-gate 	sigemptyset(&up->u_sigresethand);
3037c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
3047c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
3057c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * Make saved resource limit == current resource limit.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	for (i = 0; i < RLIM_NLIMITS; i++) {
3117c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/
3127c478bd9Sstevel@tonic-gate 		if (RLIM_SAVED(i)) {
3137c478bd9Sstevel@tonic-gate 			(void) rctl_rlimit_get(rctlproc_legacy[i], p,
3147c478bd9Sstevel@tonic-gate 			    &up->u_saved_rlimit[i]);
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * If the action was to catch the signal, then the action
3207c478bd9Sstevel@tonic-gate 	 * must be reset to SIG_DFL.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	sigdefault(p);
3237c478bd9Sstevel@tonic-gate 	p->p_flag &= ~(SNOWAIT|SJCTL);
3247c478bd9Sstevel@tonic-gate 	p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
3257c478bd9Sstevel@tonic-gate 	up->u_signal[SIGCLD - 1] = SIG_DFL;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * Delete the dot4 sigqueues/signotifies.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	sigqfree(p);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_pflock);
3357c478bd9Sstevel@tonic-gate 	p->p_prof.pr_base = NULL;
3367c478bd9Sstevel@tonic-gate 	p->p_prof.pr_size = 0;
3377c478bd9Sstevel@tonic-gate 	p->p_prof.pr_off = 0;
3387c478bd9Sstevel@tonic-gate 	p->p_prof.pr_scale = 0;
3397c478bd9Sstevel@tonic-gate 	p->p_prof.pr_samples = 0;
3407c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_pflock);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedctl == NULL);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #if defined(__sparc)
3457c478bd9Sstevel@tonic-gate 	if (p->p_utraps != NULL)
3467c478bd9Sstevel@tonic-gate 		utrap_free(p);
3477c478bd9Sstevel@tonic-gate #endif	/* __sparc */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Close all close-on-exec files.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	close_exec(P_FINFO(p));
3537c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
3547c478bd9Sstevel@tonic-gate 	setregs(&args);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* Mark this as an executable vnode */
3577c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
3587c478bd9Sstevel@tonic-gate 	vp->v_flag |= VVMEXEC;
3597c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
3627c478bd9Sstevel@tonic-gate 	if (dir != NULL)
3637c478bd9Sstevel@tonic-gate 		VN_RELE(dir);
3647c478bd9Sstevel@tonic-gate 	pn_free(&resolvepn);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Allocate a new lwp directory and lwpid hash table if necessary.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
3707c478bd9Sstevel@tonic-gate 		lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
3717c478bd9Sstevel@tonic-gate 		lwpdir->ld_next = lwpdir + 1;
3727c478bd9Sstevel@tonic-gate 		tidhash = kmem_zalloc(2 * sizeof (lwpdir_t *), KM_SLEEP);
3737c478bd9Sstevel@tonic-gate 		if (p->p_lwpdir != NULL)
3747c478bd9Sstevel@tonic-gate 			lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
3757c478bd9Sstevel@tonic-gate 		else
3767c478bd9Sstevel@tonic-gate 			lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3807c478bd9Sstevel@tonic-gate 	prbarrier(p);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Reset lwp id to the default value of 1.
3847c478bd9Sstevel@tonic-gate 	 * This is a single-threaded process now
3857c478bd9Sstevel@tonic-gate 	 * and lwp #1 is lwp_wait()able by default.
3867c478bd9Sstevel@tonic-gate 	 * The t_unpark flag should not be inherited.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
3897c478bd9Sstevel@tonic-gate 	curthread->t_tid = 1;
3907c478bd9Sstevel@tonic-gate 	curthread->t_unpark = 0;
3917c478bd9Sstevel@tonic-gate 	curthread->t_proc_flag |= TP_TWAIT;
3927c478bd9Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_DAEMON;	/* daemons shouldn't exec */
3937c478bd9Sstevel@tonic-gate 	p->p_lwpdaemon = 0;			/* but oh well ... */
3947c478bd9Sstevel@tonic-gate 	p->p_lwpid = 1;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Install the newly-allocated lwp directory and lwpid hash table
3987c478bd9Sstevel@tonic-gate 	 * and insert the current thread into the new hash table.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (lwpdir != NULL) {
4017c478bd9Sstevel@tonic-gate 		old_lwpdir = p->p_lwpdir;
4027c478bd9Sstevel@tonic-gate 		old_lwpdir_sz = p->p_lwpdir_sz;
4037c478bd9Sstevel@tonic-gate 		old_tidhash = p->p_tidhash;
4047c478bd9Sstevel@tonic-gate 		old_tidhash_sz = p->p_tidhash_sz;
4057c478bd9Sstevel@tonic-gate 		p->p_lwpdir = p->p_lwpfree = lwpdir;
4067c478bd9Sstevel@tonic-gate 		p->p_lwpdir_sz = 2;
4077c478bd9Sstevel@tonic-gate 		p->p_tidhash = tidhash;
4087c478bd9Sstevel@tonic-gate 		p->p_tidhash_sz = 2;
4097c478bd9Sstevel@tonic-gate 		lep->le_thread = curthread;
4107c478bd9Sstevel@tonic-gate 		lep->le_lwpid = curthread->t_tid;
4117c478bd9Sstevel@tonic-gate 		lep->le_start = curthread->t_start;
4127c478bd9Sstevel@tonic-gate 		lwp_hash_in(p, lep);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Restore the saved signal mask and
4167c478bd9Sstevel@tonic-gate 	 * inform /proc that the exec() has finished.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4197c478bd9Sstevel@tonic-gate 	prexecend();
4207c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4217c478bd9Sstevel@tonic-gate 	if (old_lwpdir) {
4227c478bd9Sstevel@tonic-gate 		kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
4237c478bd9Sstevel@tonic-gate 		kmem_free(old_tidhash, old_tidhash_sz * sizeof (lwpdir_t *));
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
4267c478bd9Sstevel@tonic-gate 	DTRACE_PROC(exec__success);
4277c478bd9Sstevel@tonic-gate 	return (0);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate fail:
4307c478bd9Sstevel@tonic-gate 	DTRACE_PROC1(exec__failure, int, error);
4317c478bd9Sstevel@tonic-gate out:		/* error return */
4327c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4337c478bd9Sstevel@tonic-gate 	curthread->t_hold = savedmask;
4347c478bd9Sstevel@tonic-gate 	prexecend();
4357c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4367c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);
4377c478bd9Sstevel@tonic-gate 	return (error);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Perform generic exec duties and switchout to object-file specific
4437c478bd9Sstevel@tonic-gate  * handler.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate int
4467c478bd9Sstevel@tonic-gate gexec(
4477c478bd9Sstevel@tonic-gate 	struct vnode **vpp,
4487c478bd9Sstevel@tonic-gate 	struct execa *uap,
4497c478bd9Sstevel@tonic-gate 	struct uarg *args,
4507c478bd9Sstevel@tonic-gate 	struct intpdata *idatap,
4517c478bd9Sstevel@tonic-gate 	int level,
4527c478bd9Sstevel@tonic-gate 	long *execsz,
4537c478bd9Sstevel@tonic-gate 	caddr_t exec_file,
4547c478bd9Sstevel@tonic-gate 	struct cred *cred)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4577c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
4587c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
4597c478bd9Sstevel@tonic-gate 	int error = 0;
4607c478bd9Sstevel@tonic-gate 	int suidflags = 0;
4617c478bd9Sstevel@tonic-gate 	ssize_t resid;
4627c478bd9Sstevel@tonic-gate 	uid_t uid, gid;
4637c478bd9Sstevel@tonic-gate 	struct vattr vattr;
4647c478bd9Sstevel@tonic-gate 	char magbuf[MAGIC_BYTES];
4657c478bd9Sstevel@tonic-gate 	int setid;
4667c478bd9Sstevel@tonic-gate 	cred_t *oldcred, *newcred = NULL;
4677c478bd9Sstevel@tonic-gate 	int privflags = 0;
468cc4b03b5Scasper 	int setidfl;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * If the SNOCD or SUGID flag is set, turn it off and remember the
4727c478bd9Sstevel@tonic-gate 	 * previous setting so we can restore it if we encounter an error.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
4757c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
4767c478bd9Sstevel@tonic-gate 		suidflags = pp->p_flag & PSUIDFLAGS;
4777c478bd9Sstevel@tonic-gate 		pp->p_flag &= ~PSUIDFLAGS;
4787c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if ((error = execpermissions(*vpp, &vattr, args)) != 0)
4827c478bd9Sstevel@tonic-gate 		goto bad;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* need to open vnode for stateful file systems like rfs */
4857c478bd9Sstevel@tonic-gate 	if ((error = VOP_OPEN(vpp, FREAD, CRED())) != 0)
4867c478bd9Sstevel@tonic-gate 		goto bad;
4877c478bd9Sstevel@tonic-gate 	vp = *vpp;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Note: to support binary compatibility with SunOS a.out
4917c478bd9Sstevel@tonic-gate 	 * executables, we read in the first four bytes, as the
4927c478bd9Sstevel@tonic-gate 	 * magic number is in bytes 2-3.
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
4957c478bd9Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
4967c478bd9Sstevel@tonic-gate 		goto bad;
4977c478bd9Sstevel@tonic-gate 	if (resid != 0)
4987c478bd9Sstevel@tonic-gate 		goto bad;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if ((eswp = findexec_by_hdr(magbuf)) == NULL)
5017c478bd9Sstevel@tonic-gate 		goto bad;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if (level == 0 &&
5047c478bd9Sstevel@tonic-gate 	    (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) {
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		newcred = cred = crdup(cred);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/* If we can, drop the PA bit */
5097c478bd9Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0)
5107c478bd9Sstevel@tonic-gate 			priv_adjust_PA(cred);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		if (privflags & PRIV_SETID) {
5137c478bd9Sstevel@tonic-gate 			cred->cr_uid = uid;
5147c478bd9Sstevel@tonic-gate 			cred->cr_gid = gid;
5157c478bd9Sstevel@tonic-gate 			cred->cr_suid = uid;
5167c478bd9Sstevel@tonic-gate 			cred->cr_sgid = gid;
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 
519*45916cd2Sjpk 		if (privflags & MAC_FLAGS) {
520*45916cd2Sjpk 			if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
521*45916cd2Sjpk 				CR_FLAGS(cred) &= ~NET_MAC_AWARE;
522*45916cd2Sjpk 			CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
523*45916cd2Sjpk 		}
524*45916cd2Sjpk 
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Implement the privilege updates:
5277c478bd9Sstevel@tonic-gate 		 *
5287c478bd9Sstevel@tonic-gate 		 * Restrict with L:
5297c478bd9Sstevel@tonic-gate 		 *
5307c478bd9Sstevel@tonic-gate 		 *	I' = I & L
5317c478bd9Sstevel@tonic-gate 		 *
5327c478bd9Sstevel@tonic-gate 		 *	E' = P' = (I' + F) & A
5337c478bd9Sstevel@tonic-gate 		 *
5347c478bd9Sstevel@tonic-gate 		 * But if running under ptrace, we cap I with P.
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		if ((privflags & PRIV_RESET) != 0) {
5377c478bd9Sstevel@tonic-gate 			if ((privflags & PRIV_INCREASE) != 0 &&
5387c478bd9Sstevel@tonic-gate 			    (pp->p_proc_flag & P_PR_PTRACE) != 0)
5397c478bd9Sstevel@tonic-gate 				priv_intersect(&CR_OPPRIV(cred),
5407c478bd9Sstevel@tonic-gate 						    &CR_IPRIV(cred));
5417c478bd9Sstevel@tonic-gate 			priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
5427c478bd9Sstevel@tonic-gate 			CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
5437c478bd9Sstevel@tonic-gate 			priv_adjust_PA(cred);
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* SunOS 4.x buy-back */
5487c478bd9Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
5497c478bd9Sstevel@tonic-gate 	    (vattr.va_mode & (VSUID|VSGID))) {
5507c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
5517c478bd9Sstevel@tonic-gate 		    "!%s, uid %d: setuid execution not allowed, dev=%lx",
5527c478bd9Sstevel@tonic-gate 		    exec_file, cred->cr_uid, vp->v_vfsp->vfs_dev);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * execsetid() told us whether or not we had to change the
5577c478bd9Sstevel@tonic-gate 	 * credentials of the process.  In privflags, it told us
5587c478bd9Sstevel@tonic-gate 	 * whether we gained any privileges or executed a set-uid executable.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE));
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Use /etc/system variable to determine if the stack
5647c478bd9Sstevel@tonic-gate 	 * should be marked as executable by default.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	if (noexec_user_stack)
5677c478bd9Sstevel@tonic-gate 		args->stk_prot &= ~PROT_EXEC;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Traditionally, the setid flags told the sub processes whether
5737c478bd9Sstevel@tonic-gate 	 * the file just executed was set-uid or set-gid; this caused
5747c478bd9Sstevel@tonic-gate 	 * some confusion as the 'setid' flag did not match the SUGID
5757c478bd9Sstevel@tonic-gate 	 * process flag which is only set when the uids/gids do not match.
5767c478bd9Sstevel@tonic-gate 	 * A script set-gid/set-uid to the real uid/gid would start with
5777c478bd9Sstevel@tonic-gate 	 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
5787c478bd9Sstevel@tonic-gate 	 * Now we flag those cases where the calling process cannot
5797c478bd9Sstevel@tonic-gate 	 * be trusted to influence the newly exec'ed process, either
5807c478bd9Sstevel@tonic-gate 	 * because it runs with more privileges or when the uids/gids
5817c478bd9Sstevel@tonic-gate 	 * do in fact not match.
5827c478bd9Sstevel@tonic-gate 	 * This also makes the runtime linker agree with the on exec
5837c478bd9Sstevel@tonic-gate 	 * values of SNOCD and SUGID.
5847c478bd9Sstevel@tonic-gate 	 */
585cc4b03b5Scasper 	setidfl = 0;
586cc4b03b5Scasper 	if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
587cc4b03b5Scasper 	    !supgroupmember(cred->cr_gid, cred))) {
588cc4b03b5Scasper 		setidfl |= EXECSETID_UGIDS;
589cc4b03b5Scasper 	}
590cc4b03b5Scasper 	if (setid & PRIV_SETUGID)
591cc4b03b5Scasper 		setidfl |= EXECSETID_SETID;
592cc4b03b5Scasper 	if (setid & PRIV_INCREASE)
593cc4b03b5Scasper 		setidfl |= EXECSETID_PRIVS;
594cc4b03b5Scasper 
5957c478bd9Sstevel@tonic-gate 	error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
596cc4b03b5Scasper 		setidfl, exec_file, cred);
5977c478bd9Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
5987c478bd9Sstevel@tonic-gate 	if (error != 0) {
5997c478bd9Sstevel@tonic-gate 		if (newcred != NULL)
6007c478bd9Sstevel@tonic-gate 			crfree(newcred);
6017c478bd9Sstevel@tonic-gate 		goto bad;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (level == 0) {
6057c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
6067c478bd9Sstevel@tonic-gate 		if (newcred != NULL) {
6077c478bd9Sstevel@tonic-gate 			/*
6087c478bd9Sstevel@tonic-gate 			 * Free the old credentials, and set the new ones.
6097c478bd9Sstevel@tonic-gate 			 * Do this for both the process and the (single) thread.
6107c478bd9Sstevel@tonic-gate 			 */
6117c478bd9Sstevel@tonic-gate 			crfree(pp->p_cred);
6127c478bd9Sstevel@tonic-gate 			pp->p_cred = cred;	/* cred already held for proc */
6137c478bd9Sstevel@tonic-gate 			crhold(cred);		/* hold new cred for thread */
6147c478bd9Sstevel@tonic-gate 			/*
6157c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
6167c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
6177c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
6187c478bd9Sstevel@tonic-gate 			 */
6197c478bd9Sstevel@tonic-gate 			oldcred = curthread->t_cred;
6207c478bd9Sstevel@tonic-gate 			curthread->t_cred = cred;
6217c478bd9Sstevel@tonic-gate 			crfree(oldcred);
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		/*
6247c478bd9Sstevel@tonic-gate 		 * On emerging from a successful exec(), the saved
6257c478bd9Sstevel@tonic-gate 		 * uid and gid equal the effective uid and gid.
6267c478bd9Sstevel@tonic-gate 		 */
6277c478bd9Sstevel@tonic-gate 		cred->cr_suid = cred->cr_uid;
6287c478bd9Sstevel@tonic-gate 		cred->cr_sgid = cred->cr_gid;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * If the real and effective ids do not match, this
6327c478bd9Sstevel@tonic-gate 		 * is a setuid process that should not dump core.
6337c478bd9Sstevel@tonic-gate 		 * The group comparison is tricky; we prevent the code
6347c478bd9Sstevel@tonic-gate 		 * from flagging SNOCD when executing with an effective gid
6357c478bd9Sstevel@tonic-gate 		 * which is a supplementary group.
6367c478bd9Sstevel@tonic-gate 		 */
6377c478bd9Sstevel@tonic-gate 		if (cred->cr_ruid != cred->cr_uid ||
6387c478bd9Sstevel@tonic-gate 		    (cred->cr_rgid != cred->cr_gid &&
6397c478bd9Sstevel@tonic-gate 		    !supgroupmember(cred->cr_gid, cred)) ||
6407c478bd9Sstevel@tonic-gate 		    (privflags & PRIV_INCREASE) != 0)
6417c478bd9Sstevel@tonic-gate 			suidflags = PSUIDFLAGS;
6427c478bd9Sstevel@tonic-gate 		else
6437c478bd9Sstevel@tonic-gate 			suidflags = 0;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
6467c478bd9Sstevel@tonic-gate 		if (suidflags) {
6477c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
6487c478bd9Sstevel@tonic-gate 			pp->p_flag |= suidflags;
6497c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
6527c478bd9Sstevel@tonic-gate 			/*
6537c478bd9Sstevel@tonic-gate 			 * If process is traced via /proc, arrange to
6547c478bd9Sstevel@tonic-gate 			 * invalidate the associated /proc vnode.
6557c478bd9Sstevel@tonic-gate 			 */
6567c478bd9Sstevel@tonic-gate 			if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
6577c478bd9Sstevel@tonic-gate 				args->traceinval = 1;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_PTRACE)
6607c478bd9Sstevel@tonic-gate 			psignal(pp, SIGTRAP);
6617c478bd9Sstevel@tonic-gate 		if (args->traceinval)
6627c478bd9Sstevel@tonic-gate 			prinvalidate(&pp->p_user);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	return (0);
6667c478bd9Sstevel@tonic-gate bad:
6677c478bd9Sstevel@tonic-gate 	if (error == 0)
6687c478bd9Sstevel@tonic-gate 		error = ENOEXEC;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (suidflags) {
6717c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
6727c478bd9Sstevel@tonic-gate 		pp->p_flag |= suidflags;
6737c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	return (error);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate extern char *execswnames[];
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate struct execsw *
6817c478bd9Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	int i, j;
6847c478bd9Sstevel@tonic-gate 	char *ename;
6857c478bd9Sstevel@tonic-gate 	char *magicp;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	mutex_enter(&execsw_lock);
6887c478bd9Sstevel@tonic-gate 	for (i = 0; i < nexectype; i++) {
6897c478bd9Sstevel@tonic-gate 		if (execswnames[i] == NULL) {
6907c478bd9Sstevel@tonic-gate 			ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
6917c478bd9Sstevel@tonic-gate 			(void) strcpy(ename, name);
6927c478bd9Sstevel@tonic-gate 			execswnames[i] = ename;
6937c478bd9Sstevel@tonic-gate 			/*
6947c478bd9Sstevel@tonic-gate 			 * Set the magic number last so that we
6957c478bd9Sstevel@tonic-gate 			 * don't need to hold the execsw_lock in
6967c478bd9Sstevel@tonic-gate 			 * findexectype().
6977c478bd9Sstevel@tonic-gate 			 */
6987c478bd9Sstevel@tonic-gate 			magicp = kmem_alloc(magic_size, KM_SLEEP);
6997c478bd9Sstevel@tonic-gate 			for (j = 0; j < magic_size; j++)
7007c478bd9Sstevel@tonic-gate 				magicp[j] = magic[j];
7017c478bd9Sstevel@tonic-gate 			execsw[i].exec_magic = magicp;
7027c478bd9Sstevel@tonic-gate 			mutex_exit(&execsw_lock);
7037c478bd9Sstevel@tonic-gate 			return (&execsw[i]);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 	mutex_exit(&execsw_lock);
7077c478bd9Sstevel@tonic-gate 	return (NULL);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * Find the exec switch table entry with the corresponding magic string.
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate struct execsw *
7147c478bd9Sstevel@tonic-gate findexecsw(char *magic)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7197c478bd9Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7207c478bd9Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
7217c478bd9Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
7227c478bd9Sstevel@tonic-gate 			return (eswp);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	return (NULL);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * Find the execsw[] index for the given exec header string by looking for the
7297c478bd9Sstevel@tonic-gate  * magic string at a specified offset and length for each kind of executable
7307c478bd9Sstevel@tonic-gate  * file format until one matches.  If no execsw[] entry is found, try to
7317c478bd9Sstevel@tonic-gate  * autoload a module for this magic string.
7327c478bd9Sstevel@tonic-gate  */
7337c478bd9Sstevel@tonic-gate struct execsw *
7347c478bd9Sstevel@tonic-gate findexec_by_hdr(char *header)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7397c478bd9Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7407c478bd9Sstevel@tonic-gate 		if (header && eswp->exec_maglen != 0 &&
7417c478bd9Sstevel@tonic-gate 		    bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
7427c478bd9Sstevel@tonic-gate 			    eswp->exec_maglen) == 0) {
7437c478bd9Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
7447c478bd9Sstevel@tonic-gate 				return (NULL);
7457c478bd9Sstevel@tonic-gate 			return (eswp);
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Find the execsw[] index for the given magic string.  If no execsw[] entry
7537c478bd9Sstevel@tonic-gate  * is found, try to autoload a module for this magic string.
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate struct execsw *
7567c478bd9Sstevel@tonic-gate findexec_by_magic(char *magic)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
7617c478bd9Sstevel@tonic-gate 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
7627c478bd9Sstevel@tonic-gate 		if (magic && eswp->exec_maglen != 0 &&
7637c478bd9Sstevel@tonic-gate 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
7647c478bd9Sstevel@tonic-gate 			if (hold_execsw(eswp) != 0)
7657c478bd9Sstevel@tonic-gate 				return (NULL);
7667c478bd9Sstevel@tonic-gate 			return (eswp);
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	return (NULL);	/* couldn't find the type */
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	char *name;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	rw_enter(eswp->exec_lock, RW_READER);
7787c478bd9Sstevel@tonic-gate 	while (!LOADED_EXEC(eswp)) {
7797c478bd9Sstevel@tonic-gate 		rw_exit(eswp->exec_lock);
7807c478bd9Sstevel@tonic-gate 		name = execswnames[eswp-execsw];
7817c478bd9Sstevel@tonic-gate 		ASSERT(name);
7827c478bd9Sstevel@tonic-gate 		if (modload("exec", name) == -1)
7837c478bd9Sstevel@tonic-gate 			return (-1);
7847c478bd9Sstevel@tonic-gate 		rw_enter(eswp->exec_lock, RW_READER);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	return (0);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate static int
7907c478bd9Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
7937c478bd9Sstevel@tonic-gate 	uid_t uid, gid;
7947c478bd9Sstevel@tonic-gate 	cred_t *cr = pp->p_cred;
7957c478bd9Sstevel@tonic-gate 	int privflags = 0;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/*
7987c478bd9Sstevel@tonic-gate 	 * Remember credentials.
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	uid = cr->cr_uid;
8017c478bd9Sstevel@tonic-gate 	gid = cr->cr_gid;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/* Will try to reset the PRIV_AWARE bit later. */
8047c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
8057c478bd9Sstevel@tonic-gate 		privflags |= PRIV_RESET;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
8087c478bd9Sstevel@tonic-gate 		/*
8097c478bd9Sstevel@tonic-gate 		 * Set-uid root execution only allowed if the limit set
8107c478bd9Sstevel@tonic-gate 		 * holds all unsafe privileges.
8117c478bd9Sstevel@tonic-gate 		 */
8127c478bd9Sstevel@tonic-gate 		if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 ||
8137c478bd9Sstevel@tonic-gate 		    priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) {
8147c478bd9Sstevel@tonic-gate 			uid = vattrp->va_uid;
8157c478bd9Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		if (vattrp->va_mode & VSGID) {
8187c478bd9Sstevel@tonic-gate 			gid = vattrp->va_gid;
8197c478bd9Sstevel@tonic-gate 			privflags |= PRIV_SETUGID;
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * Do we need to change our credential anyway?
8257c478bd9Sstevel@tonic-gate 	 * This is the case when E != I or P != I, as
8267c478bd9Sstevel@tonic-gate 	 * we need to do the assignments (with F empty and A full)
8277c478bd9Sstevel@tonic-gate 	 * Or when I is not a subset of L; in that case we need to
8287c478bd9Sstevel@tonic-gate 	 * enforce L.
8297c478bd9Sstevel@tonic-gate 	 *
8307c478bd9Sstevel@tonic-gate 	 *		I' = L & I
8317c478bd9Sstevel@tonic-gate 	 *
8327c478bd9Sstevel@tonic-gate 	 *		E' = P' = (I' + F) & A
8337c478bd9Sstevel@tonic-gate 	 * or
8347c478bd9Sstevel@tonic-gate 	 *		E' = P' = I'
8357c478bd9Sstevel@tonic-gate 	 */
8367c478bd9Sstevel@tonic-gate 	if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
8377c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
8387c478bd9Sstevel@tonic-gate 	    !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
8397c478bd9Sstevel@tonic-gate 		privflags |= PRIV_RESET;
8407c478bd9Sstevel@tonic-gate 
841*45916cd2Sjpk 	/* If MAC-aware flag(s) are on, need to update cred to remove. */
842*45916cd2Sjpk 	if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
843*45916cd2Sjpk 	    (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
844*45916cd2Sjpk 		privflags |= MAC_FLAGS;
845*45916cd2Sjpk 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * When we introduce the "forced" set then we will need
8487c478bd9Sstevel@tonic-gate 	 * to set PRIV_INCREASE here if I not a subset of P.
8497c478bd9Sstevel@tonic-gate 	 * If the "allowed" set is introduced we will need to do
8507c478bd9Sstevel@tonic-gate 	 * a similar thing; however, it seems more reasonable to
8517c478bd9Sstevel@tonic-gate 	 * have the allowed set reduce "L": script language interpreters
8527c478bd9Sstevel@tonic-gate 	 * would typically have an allowed set of "all".
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/*
8567c478bd9Sstevel@tonic-gate 	 * Set setuid/setgid protections if no ptrace() compatibility.
8577c478bd9Sstevel@tonic-gate 	 * For privileged processes, honor setuid/setgid even in
8587c478bd9Sstevel@tonic-gate 	 * the presence of ptrace() compatibility.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
8617c478bd9Sstevel@tonic-gate 	    PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
8627c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != uid ||
8637c478bd9Sstevel@tonic-gate 	    cr->cr_gid != gid ||
8647c478bd9Sstevel@tonic-gate 	    cr->cr_suid != uid ||
8657c478bd9Sstevel@tonic-gate 	    cr->cr_sgid != gid)) {
8667c478bd9Sstevel@tonic-gate 		*uidp = uid;
8677c478bd9Sstevel@tonic-gate 		*gidp = gid;
8687c478bd9Sstevel@tonic-gate 		privflags |= PRIV_SETID;
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 	return (privflags);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate int
8747c478bd9Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	int error;
8777c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
8807c478bd9Sstevel@tonic-gate 	if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred))
8817c478bd9Sstevel@tonic-gate 		return (error);
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * Check the access mode.
8847c478bd9Sstevel@tonic-gate 	 * If VPROC, ask /proc if the file is an object file.
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred)) != 0 ||
8877c478bd9Sstevel@tonic-gate 	    !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
8887c478bd9Sstevel@tonic-gate 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
8897c478bd9Sstevel@tonic-gate 	    (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
8907c478bd9Sstevel@tonic-gate 		if (error == 0)
8917c478bd9Sstevel@tonic-gate 			error = EACCES;
8927c478bd9Sstevel@tonic-gate 		return (error);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
8967c478bd9Sstevel@tonic-gate 	    (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred))) {
8977c478bd9Sstevel@tonic-gate 		/*
8987c478bd9Sstevel@tonic-gate 		 * If process is under ptrace(2) compatibility,
8997c478bd9Sstevel@tonic-gate 		 * fail the exec(2).
9007c478bd9Sstevel@tonic-gate 		 */
9017c478bd9Sstevel@tonic-gate 		if (p->p_proc_flag & P_PR_PTRACE)
9027c478bd9Sstevel@tonic-gate 			goto bad;
9037c478bd9Sstevel@tonic-gate 		/*
9047c478bd9Sstevel@tonic-gate 		 * Process is traced via /proc.
9057c478bd9Sstevel@tonic-gate 		 * Arrange to invalidate the /proc vnode.
9067c478bd9Sstevel@tonic-gate 		 */
9077c478bd9Sstevel@tonic-gate 		args->traceinval = 1;
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 	return (0);
9107c478bd9Sstevel@tonic-gate bad:
9117c478bd9Sstevel@tonic-gate 	if (error == 0)
9127c478bd9Sstevel@tonic-gate 		error = ENOEXEC;
9137c478bd9Sstevel@tonic-gate 	return (error);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /*
9177c478bd9Sstevel@tonic-gate  * Map a section of an executable file into the user's
9187c478bd9Sstevel@tonic-gate  * address space.
9197c478bd9Sstevel@tonic-gate  */
9207c478bd9Sstevel@tonic-gate int
9217c478bd9Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
9227c478bd9Sstevel@tonic-gate     off_t offset, int prot, int page, uint_t szc)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	int error = 0;
9257c478bd9Sstevel@tonic-gate 	off_t oldoffset;
9267c478bd9Sstevel@tonic-gate 	caddr_t zfodbase, oldaddr;
9277c478bd9Sstevel@tonic-gate 	size_t end, oldlen;
9287c478bd9Sstevel@tonic-gate 	size_t zfoddiff;
9297c478bd9Sstevel@tonic-gate 	label_t ljb;
9307c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	oldaddr = addr;
9337c478bd9Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
9347c478bd9Sstevel@tonic-gate 	if (len) {
9357c478bd9Sstevel@tonic-gate 		oldlen = len;
9367c478bd9Sstevel@tonic-gate 		len += ((size_t)oldaddr - (size_t)addr);
9377c478bd9Sstevel@tonic-gate 		oldoffset = offset;
9387c478bd9Sstevel@tonic-gate 		offset = (off_t)((uintptr_t)offset & PAGEMASK);
9397c478bd9Sstevel@tonic-gate 		if (page) {
9407c478bd9Sstevel@tonic-gate 			spgcnt_t  prefltmem, availm, npages;
9417c478bd9Sstevel@tonic-gate 			int preread;
9427c478bd9Sstevel@tonic-gate 			uint_t mflag = MAP_PRIVATE | MAP_FIXED;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
9457c478bd9Sstevel@tonic-gate 				mflag |= MAP_TEXT;
9467c478bd9Sstevel@tonic-gate 			} else {
9477c478bd9Sstevel@tonic-gate 				mflag |= MAP_INITDATA;
9487c478bd9Sstevel@tonic-gate 			}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
9517c478bd9Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
9527c478bd9Sstevel@tonic-gate 				error = ENOMEM;
9537c478bd9Sstevel@tonic-gate 				goto bad;
9547c478bd9Sstevel@tonic-gate 			}
9557c478bd9Sstevel@tonic-gate 			if (error = VOP_MAP(vp, (offset_t)offset,
9567c478bd9Sstevel@tonic-gate 			    p->p_as, &addr, len, prot, PROT_ALL,
9577c478bd9Sstevel@tonic-gate 			    mflag, CRED()))
9587c478bd9Sstevel@tonic-gate 				goto bad;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 			/*
9617c478bd9Sstevel@tonic-gate 			 * If the segment can fit, then we prefault
9627c478bd9Sstevel@tonic-gate 			 * the entire segment in.  This is based on the
9637c478bd9Sstevel@tonic-gate 			 * model that says the best working set of a
9647c478bd9Sstevel@tonic-gate 			 * small program is all of its pages.
9657c478bd9Sstevel@tonic-gate 			 */
9667c478bd9Sstevel@tonic-gate 			npages = (spgcnt_t)btopr(len);
9677c478bd9Sstevel@tonic-gate 			prefltmem = freemem - desfree;
9687c478bd9Sstevel@tonic-gate 			preread =
9697c478bd9Sstevel@tonic-gate 			    (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 			/*
9727c478bd9Sstevel@tonic-gate 			 * If we aren't prefaulting the segment,
9737c478bd9Sstevel@tonic-gate 			 * increment "deficit", if necessary to ensure
9747c478bd9Sstevel@tonic-gate 			 * that pages will become available when this
9757c478bd9Sstevel@tonic-gate 			 * process starts executing.
9767c478bd9Sstevel@tonic-gate 			 */
9777c478bd9Sstevel@tonic-gate 			availm = freemem - lotsfree;
9787c478bd9Sstevel@tonic-gate 			if (preread == 0 && npages > availm &&
9797c478bd9Sstevel@tonic-gate 			    deficit < lotsfree) {
9807c478bd9Sstevel@tonic-gate 				deficit += MIN((pgcnt_t)(npages - availm),
9817c478bd9Sstevel@tonic-gate 				    lotsfree - deficit);
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 			if (preread) {
9857c478bd9Sstevel@tonic-gate 				TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
9867c478bd9Sstevel@tonic-gate 				    "execmap preread:freemem %d size %lu",
9877c478bd9Sstevel@tonic-gate 				    freemem, len);
9887c478bd9Sstevel@tonic-gate 				(void) as_fault(p->p_as->a_hat, p->p_as,
9897c478bd9Sstevel@tonic-gate 				    (caddr_t)addr, len, F_INVAL, S_READ);
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 		} else {
9927c478bd9Sstevel@tonic-gate 			if (valid_usr_range(addr, len, prot, p->p_as,
9937c478bd9Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
9947c478bd9Sstevel@tonic-gate 				error = ENOMEM;
9957c478bd9Sstevel@tonic-gate 				goto bad;
9967c478bd9Sstevel@tonic-gate 			}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 			if (error = as_map(p->p_as, addr, len,
9997c478bd9Sstevel@tonic-gate 			    segvn_create, zfod_argsp))
10007c478bd9Sstevel@tonic-gate 				goto bad;
10017c478bd9Sstevel@tonic-gate 			/*
10027c478bd9Sstevel@tonic-gate 			 * Read in the segment in one big chunk.
10037c478bd9Sstevel@tonic-gate 			 */
10047c478bd9Sstevel@tonic-gate 			if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
10057c478bd9Sstevel@tonic-gate 			    oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
10067c478bd9Sstevel@tonic-gate 			    (rlim64_t)0, CRED(), (ssize_t *)0))
10077c478bd9Sstevel@tonic-gate 				goto bad;
10087c478bd9Sstevel@tonic-gate 			/*
10097c478bd9Sstevel@tonic-gate 			 * Now set protections.
10107c478bd9Sstevel@tonic-gate 			 */
10117c478bd9Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
10127c478bd9Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)addr,
10137c478bd9Sstevel@tonic-gate 				    len, prot);
10147c478bd9Sstevel@tonic-gate 			}
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if (zfodlen) {
10197c478bd9Sstevel@tonic-gate 		end = (size_t)addr + len;
10207c478bd9Sstevel@tonic-gate 		zfodbase = (caddr_t)roundup(end, PAGESIZE);
10217c478bd9Sstevel@tonic-gate 		zfoddiff = (uintptr_t)zfodbase - end;
10227c478bd9Sstevel@tonic-gate 		if (zfoddiff) {
10237c478bd9Sstevel@tonic-gate 			if (on_fault(&ljb)) {
10247c478bd9Sstevel@tonic-gate 				no_fault();
10257c478bd9Sstevel@tonic-gate 				error = EFAULT;
10267c478bd9Sstevel@tonic-gate 				goto bad;
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 			uzero((void *)end, zfoddiff);
10297c478bd9Sstevel@tonic-gate 			no_fault();
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 		if (zfodlen > zfoddiff) {
10327c478bd9Sstevel@tonic-gate 			struct segvn_crargs crargs =
10337c478bd9Sstevel@tonic-gate 			    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			zfodlen -= zfoddiff;
10367c478bd9Sstevel@tonic-gate 			if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
10377c478bd9Sstevel@tonic-gate 			    p->p_as->a_userlimit) != RANGE_OKAY) {
10387c478bd9Sstevel@tonic-gate 				error = ENOMEM;
10397c478bd9Sstevel@tonic-gate 				goto bad;
10407c478bd9Sstevel@tonic-gate 			}
10417c478bd9Sstevel@tonic-gate 			crargs.szc = szc;
10427c478bd9Sstevel@tonic-gate 			if (error = as_map(p->p_as, (caddr_t)zfodbase,
10437c478bd9Sstevel@tonic-gate 			    zfodlen, segvn_create, &crargs))
10447c478bd9Sstevel@tonic-gate 				goto bad;
10457c478bd9Sstevel@tonic-gate 			if (prot != PROT_ZFOD) {
10467c478bd9Sstevel@tonic-gate 				(void) as_setprot(p->p_as, (caddr_t)zfodbase,
10477c478bd9Sstevel@tonic-gate 				    zfodlen, prot);
10487c478bd9Sstevel@tonic-gate 			}
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 	return (0);
10527c478bd9Sstevel@tonic-gate bad:
10537c478bd9Sstevel@tonic-gate 	return (error);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate void
10577c478bd9Sstevel@tonic-gate setexecenv(struct execenv *ep)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
10607c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
10617c478bd9Sstevel@tonic-gate 	struct vnode *vp;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	p->p_bssbase = ep->ex_bssbase;
10647c478bd9Sstevel@tonic-gate 	p->p_brkbase = ep->ex_brkbase;
10657c478bd9Sstevel@tonic-gate 	p->p_brksize = ep->ex_brksize;
10667c478bd9Sstevel@tonic-gate 	if (p->p_exec)
10677c478bd9Sstevel@tonic-gate 		VN_RELE(p->p_exec);	/* out with the old */
10687c478bd9Sstevel@tonic-gate 	vp = p->p_exec = ep->ex_vp;
10697c478bd9Sstevel@tonic-gate 	if (vp != NULL)
10707c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);		/* in with the new */
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_sp = 0;
10737c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_size = 0;
10747c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate int
10787c478bd9Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	struct vnode *vp = *vpp;
10817c478bd9Sstevel@tonic-gate 	file_t *fp;
10827c478bd9Sstevel@tonic-gate 	int error = 0;
10837c478bd9Sstevel@tonic-gate 	int filemode = FREAD;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);		/* open reference */
10867c478bd9Sstevel@tonic-gate 	if (error = falloc(NULL, filemode, &fp, fdp)) {
10877c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
10887c478bd9Sstevel@tonic-gate 		*fdp = -1;	/* just in case falloc changed value */
10897c478bd9Sstevel@tonic-gate 		return (error);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 	if (error = VOP_OPEN(&vp, filemode, CRED())) {
10927c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
10937c478bd9Sstevel@tonic-gate 		setf(*fdp, NULL);
10947c478bd9Sstevel@tonic-gate 		unfalloc(fp);
10957c478bd9Sstevel@tonic-gate 		*fdp = -1;
10967c478bd9Sstevel@tonic-gate 		return (error);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	*vpp = vp;		/* vnode should not have changed */
10997c478bd9Sstevel@tonic-gate 	fp->f_vnode = vp;
11007c478bd9Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
11017c478bd9Sstevel@tonic-gate 	setf(*fdp, fp);
11027c478bd9Sstevel@tonic-gate 	return (0);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate int
11067c478bd9Sstevel@tonic-gate execclose(int fd)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	return (closeandsetf(fd, NULL));
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * noexec stub function.
11147c478bd9Sstevel@tonic-gate  */
11157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11167c478bd9Sstevel@tonic-gate int
11177c478bd9Sstevel@tonic-gate noexec(
11187c478bd9Sstevel@tonic-gate     struct vnode *vp,
11197c478bd9Sstevel@tonic-gate     struct execa *uap,
11207c478bd9Sstevel@tonic-gate     struct uarg *args,
11217c478bd9Sstevel@tonic-gate     struct intpdata *idatap,
11227c478bd9Sstevel@tonic-gate     int level,
11237c478bd9Sstevel@tonic-gate     long *execsz,
11247c478bd9Sstevel@tonic-gate     int setid,
11257c478bd9Sstevel@tonic-gate     caddr_t exec_file,
11267c478bd9Sstevel@tonic-gate     struct cred *cred)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
11297c478bd9Sstevel@tonic-gate 	return (ENOEXEC);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate /*
11337c478bd9Sstevel@tonic-gate  * Support routines for building a user stack.
11347c478bd9Sstevel@tonic-gate  *
11357c478bd9Sstevel@tonic-gate  * execve(path, argv, envp) must construct a new stack with the specified
11367c478bd9Sstevel@tonic-gate  * arguments and environment variables (see exec_args() for a description
11377c478bd9Sstevel@tonic-gate  * of the user stack layout).  To do this, we copy the arguments and
11387c478bd9Sstevel@tonic-gate  * environment variables from the old user address space into the kernel,
11397c478bd9Sstevel@tonic-gate  * free the old as, create the new as, and copy our buffered information
11407c478bd9Sstevel@tonic-gate  * to the new stack.  Our kernel buffer has the following structure:
11417c478bd9Sstevel@tonic-gate  *
11427c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
11437c478bd9Sstevel@tonic-gate  *	| string offsets	|
11447c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
11457c478bd9Sstevel@tonic-gate  *	|			|
11467c478bd9Sstevel@tonic-gate  *	| STK_AVAIL() space	|
11477c478bd9Sstevel@tonic-gate  *	|			|
11487c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
11497c478bd9Sstevel@tonic-gate  *	| strings		|
11507c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
11517c478bd9Sstevel@tonic-gate  *
11527c478bd9Sstevel@tonic-gate  * When we add a string, we store the string's contents (including the null
11537c478bd9Sstevel@tonic-gate  * terminator) at stk_strp, and we store the offset of the string relative to
11547c478bd9Sstevel@tonic-gate  * stk_base at --stk_offp.  At strings are added, stk_strp increases and
11557c478bd9Sstevel@tonic-gate  * stk_offp decreases.  The amount of space remaining, STK_AVAIL(), is just
11567c478bd9Sstevel@tonic-gate  * the difference between these pointers.  If we run out of space, we return
11577c478bd9Sstevel@tonic-gate  * an error and exec_args() starts all over again with a buffer twice as large.
11587c478bd9Sstevel@tonic-gate  * When we're all done, the kernel buffer looks like this:
11597c478bd9Sstevel@tonic-gate  *
11607c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base + stk_size
11617c478bd9Sstevel@tonic-gate  *	| argv[0] offset	|
11627c478bd9Sstevel@tonic-gate  *	+-----------------------+
11637c478bd9Sstevel@tonic-gate  *	| ...			|
11647c478bd9Sstevel@tonic-gate  *	+-----------------------+
11657c478bd9Sstevel@tonic-gate  *	| argv[argc-1] offset	|
11667c478bd9Sstevel@tonic-gate  *	+-----------------------+
11677c478bd9Sstevel@tonic-gate  *	| envp[0] offset	|
11687c478bd9Sstevel@tonic-gate  *	+-----------------------+
11697c478bd9Sstevel@tonic-gate  *	| ...			|
11707c478bd9Sstevel@tonic-gate  *	+-----------------------+
11717c478bd9Sstevel@tonic-gate  *	| envp[envc-1] offset	|
11727c478bd9Sstevel@tonic-gate  *	+-----------------------+
11737c478bd9Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
11747c478bd9Sstevel@tonic-gate  *	+-----------------------+
11757c478bd9Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
11767c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_offp
11777c478bd9Sstevel@tonic-gate  *	|			|
11787c478bd9Sstevel@tonic-gate  *	| STK_AVAIL() space	|
11797c478bd9Sstevel@tonic-gate  *	|			|
11807c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_strp
11817c478bd9Sstevel@tonic-gate  *	| AT_SUN_EXECNAME offset|
11827c478bd9Sstevel@tonic-gate  *	+-----------------------+
11837c478bd9Sstevel@tonic-gate  *	| AT_SUN_PLATFORM offset|
11847c478bd9Sstevel@tonic-gate  *	+-----------------------+
11857c478bd9Sstevel@tonic-gate  *	| envp[envc-1] string	|
11867c478bd9Sstevel@tonic-gate  *	+-----------------------+
11877c478bd9Sstevel@tonic-gate  *	| ...			|
11887c478bd9Sstevel@tonic-gate  *	+-----------------------+
11897c478bd9Sstevel@tonic-gate  *	| envp[0] string	|
11907c478bd9Sstevel@tonic-gate  *	+-----------------------+
11917c478bd9Sstevel@tonic-gate  *	| argv[argc-1] string	|
11927c478bd9Sstevel@tonic-gate  *	+-----------------------+
11937c478bd9Sstevel@tonic-gate  *	| ...			|
11947c478bd9Sstevel@tonic-gate  *	+-----------------------+
11957c478bd9Sstevel@tonic-gate  *	| argv[0] string	|
11967c478bd9Sstevel@tonic-gate  *	+-----------------------+ <--- stk_base
11977c478bd9Sstevel@tonic-gate  */
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate #define	STK_AVAIL(args)		((char *)(args)->stk_offp - (args)->stk_strp)
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate  * Add a string to the stack.
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate static int
12057c478bd9Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	int error;
12087c478bd9Sstevel@tonic-gate 	size_t len;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	if (STK_AVAIL(args) < sizeof (int))
12117c478bd9Sstevel@tonic-gate 		return (E2BIG);
12127c478bd9Sstevel@tonic-gate 	*--args->stk_offp = args->stk_strp - args->stk_base;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (segflg == UIO_USERSPACE) {
12157c478bd9Sstevel@tonic-gate 		error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
12167c478bd9Sstevel@tonic-gate 		if (error != 0)
12177c478bd9Sstevel@tonic-gate 			return (error);
12187c478bd9Sstevel@tonic-gate 	} else {
12197c478bd9Sstevel@tonic-gate 		len = strlen(sp) + 1;
12207c478bd9Sstevel@tonic-gate 		if (len > STK_AVAIL(args))
12217c478bd9Sstevel@tonic-gate 			return (E2BIG);
12227c478bd9Sstevel@tonic-gate 		bcopy(sp, args->stk_strp, len);
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	args->stk_strp += len;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	return (0);
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate static int
12317c478bd9Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate 	int error;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (args->from_model == DATAMODEL_NATIVE) {
12367c478bd9Sstevel@tonic-gate 		ulong_t ptr;
12377c478bd9Sstevel@tonic-gate 		error = fulword(src, &ptr);
12387c478bd9Sstevel@tonic-gate 		*dst = (caddr_t)ptr;
12397c478bd9Sstevel@tonic-gate 	} else {
12407c478bd9Sstevel@tonic-gate 		uint32_t ptr;
12417c478bd9Sstevel@tonic-gate 		error = fuword32(src, &ptr);
12427c478bd9Sstevel@tonic-gate 		*dst = (caddr_t)(uintptr_t)ptr;
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	return (error);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate static int
12487c478bd9Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE)
12517c478bd9Sstevel@tonic-gate 		return (sulword(addr, (ulong_t)value));
12527c478bd9Sstevel@tonic-gate 	else
12537c478bd9Sstevel@tonic-gate 		return (suword32(addr, (uint32_t)(uintptr_t)value));
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate static int
12577c478bd9Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	char *sp;
12607c478bd9Sstevel@tonic-gate 	int argc, error;
12617c478bd9Sstevel@tonic-gate 	int argv_empty = 0;
12627c478bd9Sstevel@tonic-gate 	size_t ptrsize = args->from_ptrsize;
12637c478bd9Sstevel@tonic-gate 	size_t size, pad;
12647c478bd9Sstevel@tonic-gate 	char *argv = (char *)uap->argp;
12657c478bd9Sstevel@tonic-gate 	char *envp = (char *)uap->envp;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	/*
12687c478bd9Sstevel@tonic-gate 	 * Copy interpreter's name and argument to argv[0] and argv[1].
12697c478bd9Sstevel@tonic-gate 	 */
12707c478bd9Sstevel@tonic-gate 	if (intp != NULL && intp->intp_name != NULL) {
12717c478bd9Sstevel@tonic-gate 		if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
12727c478bd9Sstevel@tonic-gate 			return (error);
12737c478bd9Sstevel@tonic-gate 		if (intp->intp_arg != NULL &&
12747c478bd9Sstevel@tonic-gate 		    (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
12757c478bd9Sstevel@tonic-gate 			return (error);
12767c478bd9Sstevel@tonic-gate 		if (args->fname != NULL)
12777c478bd9Sstevel@tonic-gate 			error = stk_add(args, args->fname, UIO_SYSSPACE);
12787c478bd9Sstevel@tonic-gate 		else
12797c478bd9Sstevel@tonic-gate 			error = stk_add(args, uap->fname, UIO_USERSPACE);
12807c478bd9Sstevel@tonic-gate 		if (error)
12817c478bd9Sstevel@tonic-gate 			return (error);
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 		/*
12847c478bd9Sstevel@tonic-gate 		 * Check for an empty argv[].
12857c478bd9Sstevel@tonic-gate 		 */
12867c478bd9Sstevel@tonic-gate 		if (stk_getptr(args, argv, &sp))
12877c478bd9Sstevel@tonic-gate 			return (EFAULT);
12887c478bd9Sstevel@tonic-gate 		if (sp == NULL)
12897c478bd9Sstevel@tonic-gate 			argv_empty = 1;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		argv += ptrsize;		/* ignore original argv[0] */
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	if (argv_empty == 0) {
12957c478bd9Sstevel@tonic-gate 		/*
12967c478bd9Sstevel@tonic-gate 		 * Add argv[] strings to the stack.
12977c478bd9Sstevel@tonic-gate 		 */
12987c478bd9Sstevel@tonic-gate 		for (;;) {
12997c478bd9Sstevel@tonic-gate 			if (stk_getptr(args, argv, &sp))
13007c478bd9Sstevel@tonic-gate 				return (EFAULT);
13017c478bd9Sstevel@tonic-gate 			if (sp == NULL)
13027c478bd9Sstevel@tonic-gate 				break;
13037c478bd9Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
13047c478bd9Sstevel@tonic-gate 				return (error);
13057c478bd9Sstevel@tonic-gate 			argv += ptrsize;
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 	argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
13097c478bd9Sstevel@tonic-gate 	args->arglen = args->stk_strp - args->stk_base;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * Add environ[] strings to the stack.
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	if (envp != NULL) {
13157c478bd9Sstevel@tonic-gate 		for (;;) {
13167c478bd9Sstevel@tonic-gate 			if (stk_getptr(args, envp, &sp))
13177c478bd9Sstevel@tonic-gate 				return (EFAULT);
13187c478bd9Sstevel@tonic-gate 			if (sp == NULL)
13197c478bd9Sstevel@tonic-gate 				break;
13207c478bd9Sstevel@tonic-gate 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
13217c478bd9Sstevel@tonic-gate 				return (error);
13227c478bd9Sstevel@tonic-gate 			envp += ptrsize;
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 	args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
13267c478bd9Sstevel@tonic-gate 	args->ne = args->na - argc;
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * Add AT_SUN_PLATFORM and AT_SUN_EXECNAME strings to the stack.
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
13327c478bd9Sstevel@tonic-gate 		if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
13337c478bd9Sstevel@tonic-gate 			return (error);
13347c478bd9Sstevel@tonic-gate 		if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
13357c478bd9Sstevel@tonic-gate 			return (error);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	/*
13397c478bd9Sstevel@tonic-gate 	 * Compute the size of the stack.  This includes all the pointers,
13407c478bd9Sstevel@tonic-gate 	 * the space reserved for the aux vector, and all the strings.
13417c478bd9Sstevel@tonic-gate 	 * The total number of pointers is args->na (which is argc + envc)
13427c478bd9Sstevel@tonic-gate 	 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
13437c478bd9Sstevel@tonic-gate 	 * after the last argument (i.e. argv[argc]); (3) the NULL after the
13447c478bd9Sstevel@tonic-gate 	 * last environment variable (i.e. envp[envc]); and (4) the NULL after
13457c478bd9Sstevel@tonic-gate 	 * all the strings, at the very top of the stack.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	size = (args->na + 4) * args->to_ptrsize + args->auxsize +
13487c478bd9Sstevel@tonic-gate 	    (args->stk_strp - args->stk_base);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * Pad the string section with zeroes to align the stack size.
13527c478bd9Sstevel@tonic-gate 	 */
13537c478bd9Sstevel@tonic-gate 	pad = P2NPHASE(size, args->stk_align);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	if (STK_AVAIL(args) < pad)
13567c478bd9Sstevel@tonic-gate 		return (E2BIG);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	args->usrstack_size = size + pad;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	while (pad-- != 0)
13617c478bd9Sstevel@tonic-gate 		*args->stk_strp++ = 0;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	args->nc = args->stk_strp - args->stk_base;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	return (0);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate static int
13697c478bd9Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	size_t ptrsize = args->to_ptrsize;
13727c478bd9Sstevel@tonic-gate 	ssize_t pslen;
13737c478bd9Sstevel@tonic-gate 	char *kstrp = args->stk_base;
13747c478bd9Sstevel@tonic-gate 	char *ustrp = usrstack - args->nc - ptrsize;
13757c478bd9Sstevel@tonic-gate 	char *usp = usrstack - args->usrstack_size;
13767c478bd9Sstevel@tonic-gate 	int *offp = (int *)(args->stk_base + args->stk_size);
13777c478bd9Sstevel@tonic-gate 	int envc = args->ne;
13787c478bd9Sstevel@tonic-gate 	int argc = args->na - envc;
13797c478bd9Sstevel@tonic-gate 	int i;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * Record argc for /proc.
13837c478bd9Sstevel@tonic-gate 	 */
13847c478bd9Sstevel@tonic-gate 	up->u_argc = argc;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/*
13877c478bd9Sstevel@tonic-gate 	 * Put argc on the stack.  Note that even though it's an int,
13887c478bd9Sstevel@tonic-gate 	 * it always consumes ptrsize bytes (for alignment).
13897c478bd9Sstevel@tonic-gate 	 */
13907c478bd9Sstevel@tonic-gate 	if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
13917c478bd9Sstevel@tonic-gate 		return (-1);
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/*
13947c478bd9Sstevel@tonic-gate 	 * Add argc space (ptrsize) to usp and record argv for /proc.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	up->u_argv = (uintptr_t)(usp += ptrsize);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/*
13997c478bd9Sstevel@tonic-gate 	 * Put the argv[] pointers on the stack.
14007c478bd9Sstevel@tonic-gate 	 */
14017c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++, usp += ptrsize)
14027c478bd9Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
14037c478bd9Sstevel@tonic-gate 			return (-1);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	/*
14067c478bd9Sstevel@tonic-gate 	 * Copy arguments to u_psargs.
14077c478bd9Sstevel@tonic-gate 	 */
14087c478bd9Sstevel@tonic-gate 	pslen = MIN(args->arglen, PSARGSZ) - 1;
14097c478bd9Sstevel@tonic-gate 	for (i = 0; i < pslen; i++)
14107c478bd9Sstevel@tonic-gate 		up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
14117c478bd9Sstevel@tonic-gate 	while (i < PSARGSZ)
14127c478bd9Sstevel@tonic-gate 		up->u_psargs[i++] = '\0';
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	/*
14157c478bd9Sstevel@tonic-gate 	 * Add space for argv[]'s NULL terminator (ptrsize) to usp and
14167c478bd9Sstevel@tonic-gate 	 * record envp for /proc.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	up->u_envp = (uintptr_t)(usp += ptrsize);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	/*
14217c478bd9Sstevel@tonic-gate 	 * Put the envp[] pointers on the stack.
14227c478bd9Sstevel@tonic-gate 	 */
14237c478bd9Sstevel@tonic-gate 	for (i = 0; i < envc; i++, usp += ptrsize)
14247c478bd9Sstevel@tonic-gate 		if (stk_putptr(args, usp, &ustrp[*--offp]))
14257c478bd9Sstevel@tonic-gate 			return (-1);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	/*
14287c478bd9Sstevel@tonic-gate 	 * Add space for envp[]'s NULL terminator (ptrsize) to usp and
14297c478bd9Sstevel@tonic-gate 	 * remember where the stack ends, which is also where auxv begins.
14307c478bd9Sstevel@tonic-gate 	 */
14317c478bd9Sstevel@tonic-gate 	args->stackend = usp += ptrsize;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	/*
14347c478bd9Sstevel@tonic-gate 	 * Put all the argv[], envp[], and auxv strings on the stack.
14357c478bd9Sstevel@tonic-gate 	 */
14367c478bd9Sstevel@tonic-gate 	if (copyout(args->stk_base, ustrp, args->nc))
14377c478bd9Sstevel@tonic-gate 		return (-1);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	/*
14407c478bd9Sstevel@tonic-gate 	 * Fill in the aux vector now that we know the user stack addresses
14417c478bd9Sstevel@tonic-gate 	 * for the AT_SUN_PLATFORM and AT_SUN_EXECNAME strings.
14427c478bd9Sstevel@tonic-gate 	 */
14437c478bd9Sstevel@tonic-gate 	if (auxvpp != NULL && *auxvpp != NULL) {
14447c478bd9Sstevel@tonic-gate 		if (args->to_model == DATAMODEL_NATIVE) {
14457c478bd9Sstevel@tonic-gate 			auxv_t **a = (auxv_t **)auxvpp;
14467c478bd9Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
14477c478bd9Sstevel@tonic-gate 			ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
14487c478bd9Sstevel@tonic-gate 		} else {
14497c478bd9Sstevel@tonic-gate 			auxv32_t **a = (auxv32_t **)auxvpp;
14507c478bd9Sstevel@tonic-gate 			ADDAUX(*a,
14517c478bd9Sstevel@tonic-gate 			    AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
14527c478bd9Sstevel@tonic-gate 			ADDAUX(*a,
14537c478bd9Sstevel@tonic-gate 			    AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]);
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 	}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	return (0);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate #ifdef DEBUG
14617c478bd9Sstevel@tonic-gate int mpss_brkpgszsel = 0;
14627c478bd9Sstevel@tonic-gate int mpss_stkpgszsel = 0;
14637c478bd9Sstevel@tonic-gate #endif
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate /*
14667c478bd9Sstevel@tonic-gate  * Initialize a new user stack with the specified arguments and environment.
14677c478bd9Sstevel@tonic-gate  * The initial user stack layout is as follows:
14687c478bd9Sstevel@tonic-gate  *
14697c478bd9Sstevel@tonic-gate  *	User Stack
14707c478bd9Sstevel@tonic-gate  *	+---------------+ <--- curproc->p_usrstack
14717c478bd9Sstevel@tonic-gate  *	| NULL		|
14727c478bd9Sstevel@tonic-gate  *	+---------------+
14737c478bd9Sstevel@tonic-gate  *	|		|
14747c478bd9Sstevel@tonic-gate  *	| auxv strings	|
14757c478bd9Sstevel@tonic-gate  *	|		|
14767c478bd9Sstevel@tonic-gate  *	+---------------+
14777c478bd9Sstevel@tonic-gate  *	|		|
14787c478bd9Sstevel@tonic-gate  *	| envp strings	|
14797c478bd9Sstevel@tonic-gate  *	|		|
14807c478bd9Sstevel@tonic-gate  *	+---------------+
14817c478bd9Sstevel@tonic-gate  *	|		|
14827c478bd9Sstevel@tonic-gate  *	| argv strings	|
14837c478bd9Sstevel@tonic-gate  *	|		|
14847c478bd9Sstevel@tonic-gate  *	+---------------+ <--- ustrp
14857c478bd9Sstevel@tonic-gate  *	|		|
14867c478bd9Sstevel@tonic-gate  *	| aux vector	|
14877c478bd9Sstevel@tonic-gate  *	|		|
14887c478bd9Sstevel@tonic-gate  *	+---------------+ <--- auxv
14897c478bd9Sstevel@tonic-gate  *	| NULL		|
14907c478bd9Sstevel@tonic-gate  *	+---------------+
14917c478bd9Sstevel@tonic-gate  *	| envp[envc-1]	|
14927c478bd9Sstevel@tonic-gate  *	+---------------+
14937c478bd9Sstevel@tonic-gate  *	| ...		|
14947c478bd9Sstevel@tonic-gate  *	+---------------+
14957c478bd9Sstevel@tonic-gate  *	| envp[0]	|
14967c478bd9Sstevel@tonic-gate  *	+---------------+ <--- envp[]
14977c478bd9Sstevel@tonic-gate  *	| NULL		|
14987c478bd9Sstevel@tonic-gate  *	+---------------+
14997c478bd9Sstevel@tonic-gate  *	| argv[argc-1]	|
15007c478bd9Sstevel@tonic-gate  *	+---------------+
15017c478bd9Sstevel@tonic-gate  *	| ...		|
15027c478bd9Sstevel@tonic-gate  *	+---------------+
15037c478bd9Sstevel@tonic-gate  *	| argv[0]	|
15047c478bd9Sstevel@tonic-gate  *	+---------------+ <--- argv[]
15057c478bd9Sstevel@tonic-gate  *	| argc		|
15067c478bd9Sstevel@tonic-gate  *	+---------------+ <--- stack base
15077c478bd9Sstevel@tonic-gate  */
15087c478bd9Sstevel@tonic-gate int
15097c478bd9Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	size_t size;
15127c478bd9Sstevel@tonic-gate 	int error;
15137c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
15147c478bd9Sstevel@tonic-gate 	user_t *up = PTOU(p);
15157c478bd9Sstevel@tonic-gate 	char *usrstack;
15167c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	struct as *as;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	args->from_model = p->p_model;
15217c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
15227c478bd9Sstevel@tonic-gate 		args->from_ptrsize = sizeof (long);
15237c478bd9Sstevel@tonic-gate 	} else {
15247c478bd9Sstevel@tonic-gate 		args->from_ptrsize = sizeof (int32_t);
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (args->to_model == DATAMODEL_NATIVE) {
15287c478bd9Sstevel@tonic-gate 		args->to_ptrsize = sizeof (long);
15297c478bd9Sstevel@tonic-gate 		args->ncargs = NCARGS;
15307c478bd9Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN;
15317c478bd9Sstevel@tonic-gate 		usrstack = (char *)USRSTACK;
15327c478bd9Sstevel@tonic-gate 	} else {
15337c478bd9Sstevel@tonic-gate 		args->to_ptrsize = sizeof (int32_t);
15347c478bd9Sstevel@tonic-gate 		args->ncargs = NCARGS32;
15357c478bd9Sstevel@tonic-gate 		args->stk_align = STACK_ALIGN32;
15367c478bd9Sstevel@tonic-gate 		usrstack = (char *)USRSTACK32;
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate #if defined(__sparc)
15427c478bd9Sstevel@tonic-gate 	/*
15437c478bd9Sstevel@tonic-gate 	 * Make sure user register windows are empty before
15447c478bd9Sstevel@tonic-gate 	 * attempting to make a new stack.
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
15477c478bd9Sstevel@tonic-gate #endif
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	for (size = PAGESIZE; ; size *= 2) {
15507c478bd9Sstevel@tonic-gate 		args->stk_size = size;
15517c478bd9Sstevel@tonic-gate 		args->stk_base = kmem_alloc(size, KM_SLEEP);
15527c478bd9Sstevel@tonic-gate 		args->stk_strp = args->stk_base;
15537c478bd9Sstevel@tonic-gate 		args->stk_offp = (int *)(args->stk_base + size);
15547c478bd9Sstevel@tonic-gate 		error = stk_copyin(uap, args, intp, auxvpp);
15557c478bd9Sstevel@tonic-gate 		if (error == 0)
15567c478bd9Sstevel@tonic-gate 			break;
15577c478bd9Sstevel@tonic-gate 		kmem_free(args->stk_base, size);
15587c478bd9Sstevel@tonic-gate 		if (error != E2BIG && error != ENAMETOOLONG)
15597c478bd9Sstevel@tonic-gate 			return (error);
15607c478bd9Sstevel@tonic-gate 		if (size >= args->ncargs)
15617c478bd9Sstevel@tonic-gate 			return (E2BIG);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	size = args->usrstack_size;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
15677c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE(size, args->stk_align) == 0);
15687c478bd9Sstevel@tonic-gate 	ASSERT((ssize_t)STK_AVAIL(args) >= 0);
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if (size > args->ncargs) {
15717c478bd9Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
15727c478bd9Sstevel@tonic-gate 		return (E2BIG);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * Leave only the current lwp and force the other lwps to exit.
15777c478bd9Sstevel@tonic-gate 	 * If another lwp beat us to the punch by calling exit(), bail out.
15787c478bd9Sstevel@tonic-gate 	 */
15797c478bd9Sstevel@tonic-gate 	if ((error = exitlwps(0)) != 0) {
15807c478bd9Sstevel@tonic-gate 		kmem_free(args->stk_base, args->stk_size);
15817c478bd9Sstevel@tonic-gate 		return (error);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	/*
15857c478bd9Sstevel@tonic-gate 	 * Revoke any doors created by the process.
15867c478bd9Sstevel@tonic-gate 	 */
15877c478bd9Sstevel@tonic-gate 	if (p->p_door_list)
15887c478bd9Sstevel@tonic-gate 		door_exit();
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * Release schedctl data structures.
15927c478bd9Sstevel@tonic-gate 	 */
15937c478bd9Sstevel@tonic-gate 	if (p->p_pagep)
15947c478bd9Sstevel@tonic-gate 		schedctl_proc_cleanup();
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/*
15977c478bd9Sstevel@tonic-gate 	 * Clean up any DTrace helpers for the process.
15987c478bd9Sstevel@tonic-gate 	 */
15997c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_helpers != NULL) {
16007c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_helpers_cleanup != NULL);
16017c478bd9Sstevel@tonic-gate 		(*dtrace_helpers_cleanup)();
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16057c478bd9Sstevel@tonic-gate 	/*
16067c478bd9Sstevel@tonic-gate 	 * Cleanup the DTrace provider associated with this process.
16077c478bd9Sstevel@tonic-gate 	 */
16087c478bd9Sstevel@tonic-gate 	if (p->p_dtrace_probes) {
16097c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_fasttrap_exec_ptr != NULL);
16107c478bd9Sstevel@tonic-gate 		dtrace_fasttrap_exec_ptr(p);
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/*
16157c478bd9Sstevel@tonic-gate 	 * discard the lwpchan cache.
16167c478bd9Sstevel@tonic-gate 	 */
16177c478bd9Sstevel@tonic-gate 	if (p->p_lcp != NULL)
16187c478bd9Sstevel@tonic-gate 		lwpchan_destroy_cache(1);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * Delete the POSIX timers.
16227c478bd9Sstevel@tonic-gate 	 */
16237c478bd9Sstevel@tonic-gate 	if (p->p_itimer != NULL)
16247c478bd9Sstevel@tonic-gate 		timer_exit();
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
16277c478bd9Sstevel@tonic-gate 	if (audit_active)
16287c478bd9Sstevel@tonic-gate 		audit_exec(args->stk_base, args->stk_base + args->arglen,
16297c478bd9Sstevel@tonic-gate 		    args->na - args->ne, args->ne);
16307c478bd9Sstevel@tonic-gate #endif
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/*
16337c478bd9Sstevel@tonic-gate 	 * Ensure that we don't change resource associations while we
16347c478bd9Sstevel@tonic-gate 	 * change address spaces.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16377c478bd9Sstevel@tonic-gate 	pool_barrier_enter();
16387c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/*
16417c478bd9Sstevel@tonic-gate 	 * Destroy the old address space and create a new one.
16427c478bd9Sstevel@tonic-gate 	 * From here on, any errors are fatal to the exec()ing process.
16437c478bd9Sstevel@tonic-gate 	 * On error we return -1, which means the caller must SIGKILL
16447c478bd9Sstevel@tonic-gate 	 * the process.
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	relvm();
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16497c478bd9Sstevel@tonic-gate 	pool_barrier_exit();
16507c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	up->u_execsw = args->execswp;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	p->p_brkbase = NULL;
16557c478bd9Sstevel@tonic-gate 	p->p_brksize = 0;
16567c478bd9Sstevel@tonic-gate 	p->p_stksize = 0;
16577c478bd9Sstevel@tonic-gate 	p->p_model = args->to_model;
16587c478bd9Sstevel@tonic-gate 	p->p_usrstack = usrstack;
16597c478bd9Sstevel@tonic-gate 	p->p_stkprot = args->stk_prot;
16607c478bd9Sstevel@tonic-gate 	p->p_datprot = args->dat_prot;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Reset resource controls such that all controls are again active as
16647c478bd9Sstevel@tonic-gate 	 * well as appropriate to the potentially new address model for the
16657c478bd9Sstevel@tonic-gate 	 * process.
16667c478bd9Sstevel@tonic-gate 	 */
16677c478bd9Sstevel@tonic-gate 	e.rcep_p.proc = p;
16687c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
16697c478bd9Sstevel@tonic-gate 	rctl_set_reset(p->p_rctls, p, &e);
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if (exec_lpg_disable == 0) {
16727c478bd9Sstevel@tonic-gate #ifdef DEBUG
16737c478bd9Sstevel@tonic-gate 		uint_t pgsizes = page_num_pagesizes();
16747c478bd9Sstevel@tonic-gate 		uint_t szc;
16757c478bd9Sstevel@tonic-gate #endif
16767c478bd9Sstevel@tonic-gate 		p->p_brkpageszc = args->brkpageszc;
16777c478bd9Sstevel@tonic-gate 		p->p_stkpageszc = args->stkpageszc;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 		if (p->p_brkpageszc == 0) {
16807c478bd9Sstevel@tonic-gate 			p->p_brkpageszc = page_szc(map_pgsz(MAPPGSZ_HEAP,
16817c478bd9Sstevel@tonic-gate 			    p, 0, 0, NULL));
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 		if (p->p_stkpageszc == 0) {
16847c478bd9Sstevel@tonic-gate 			p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK,
16857c478bd9Sstevel@tonic-gate 			    p, 0, 0, NULL));
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate #ifdef DEBUG
16897c478bd9Sstevel@tonic-gate 		if (mpss_brkpgszsel != 0) {
16907c478bd9Sstevel@tonic-gate 			if (mpss_brkpgszsel == -1) {
16917c478bd9Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 8) % pgsizes;
16927c478bd9Sstevel@tonic-gate 			} else {
16937c478bd9Sstevel@tonic-gate 				szc = mpss_brkpgszsel % pgsizes;
16947c478bd9Sstevel@tonic-gate 			}
16957c478bd9Sstevel@tonic-gate 			p->p_brkpageszc = szc;
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		if (mpss_stkpgszsel != 0) {
16997c478bd9Sstevel@tonic-gate 			if (mpss_stkpgszsel == -1) {
17007c478bd9Sstevel@tonic-gate 				szc = ((uint_t)gethrtime() >> 7) % pgsizes;
17017c478bd9Sstevel@tonic-gate 			} else {
17027c478bd9Sstevel@tonic-gate 				szc = mpss_stkpgszsel % pgsizes;
17037c478bd9Sstevel@tonic-gate 			}
17047c478bd9Sstevel@tonic-gate 			p->p_stkpageszc = szc;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate #endif
17087c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
17097c478bd9Sstevel@tonic-gate 		p->p_flag |= SAUTOLPG;	/* kernel controls page sizes */
17107c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	} else {
17137c478bd9Sstevel@tonic-gate 		p->p_brkpageszc = 0;
17147c478bd9Sstevel@tonic-gate 		p->p_stkpageszc = 0;
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	exec_set_sp(size);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	as = as_alloc();
17207c478bd9Sstevel@tonic-gate 	p->p_as = as;
17217c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
17227c478bd9Sstevel@tonic-gate 		as->a_userlimit = (caddr_t)USERLIMIT32;
17237c478bd9Sstevel@tonic-gate 	(void) hat_setup(as->a_hat, HAT_ALLOC);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	/*
17267c478bd9Sstevel@tonic-gate 	 * Finally, write out the contents of the new stack.
17277c478bd9Sstevel@tonic-gate 	 */
17287c478bd9Sstevel@tonic-gate 	error = stk_copyout(args, usrstack, auxvpp, up);
17297c478bd9Sstevel@tonic-gate 	kmem_free(args->stk_base, args->stk_size);
17307c478bd9Sstevel@tonic-gate 	return (error);
17317c478bd9Sstevel@tonic-gate }
1732