10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51043Scasper * Common Development and Distribution License (the "License").
61043Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216247Sraf
220Sstevel@tonic-gate /*
2312273SCasper.Dik@Sun.COM * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/signal.h>
340Sstevel@tonic-gate #include <sys/cred_impl.h>
350Sstevel@tonic-gate #include <sys/policy.h>
360Sstevel@tonic-gate #include <sys/user.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/file.h>
390Sstevel@tonic-gate #include <sys/vfs.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/mman.h>
420Sstevel@tonic-gate #include <sys/acct.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/pathname.h>
480Sstevel@tonic-gate #include <sys/vm.h>
494426Saguzovsk #include <sys/lgrp.h>
500Sstevel@tonic-gate #include <sys/vtrace.h>
510Sstevel@tonic-gate #include <sys/exec.h>
520Sstevel@tonic-gate #include <sys/exechdr.h>
530Sstevel@tonic-gate #include <sys/kmem.h>
540Sstevel@tonic-gate #include <sys/prsystm.h>
550Sstevel@tonic-gate #include <sys/modctl.h>
560Sstevel@tonic-gate #include <sys/vmparam.h>
576247Sraf #include <sys/door.h>
580Sstevel@tonic-gate #include <sys/schedctl.h>
590Sstevel@tonic-gate #include <sys/utrap.h>
600Sstevel@tonic-gate #include <sys/systeminfo.h>
610Sstevel@tonic-gate #include <sys/stack.h>
620Sstevel@tonic-gate #include <sys/rctl.h>
630Sstevel@tonic-gate #include <sys/dtrace.h>
640Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
650Sstevel@tonic-gate #include <sys/pool.h>
660Sstevel@tonic-gate #include <sys/sdt.h>
672712Snn35248 #include <sys/brand.h>
6812273SCasper.Dik@Sun.COM #include <sys/klpd.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate #include <c2/audit.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate #include <vm/hat.h>
730Sstevel@tonic-gate #include <vm/anon.h>
740Sstevel@tonic-gate #include <vm/as.h>
750Sstevel@tonic-gate #include <vm/seg.h>
760Sstevel@tonic-gate #include <vm/seg_vn.h>
770Sstevel@tonic-gate
780Sstevel@tonic-gate #define PRIV_RESET 0x01 /* needs to reset privs */
790Sstevel@tonic-gate #define PRIV_SETID 0x02 /* needs to change uids */
800Sstevel@tonic-gate #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */
810Sstevel@tonic-gate #define PRIV_INCREASE 0x08 /* child runs with more privs */
821676Sjpk #define MAC_FLAGS 0x10 /* need to adjust MAC flags */
8312273SCasper.Dik@Sun.COM #define PRIV_FORCED 0x20 /* has forced privileges */
840Sstevel@tonic-gate
8512273SCasper.Dik@Sun.COM static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *,
8612273SCasper.Dik@Sun.COM priv_set_t *, cred_t *, const char *);
870Sstevel@tonic-gate static int hold_execsw(struct execsw *);
880Sstevel@tonic-gate
890Sstevel@tonic-gate uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */
900Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
910Sstevel@tonic-gate uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate
940Sstevel@tonic-gate #define PSUIDFLAGS (SNOCD|SUGID)
950Sstevel@tonic-gate
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * exece() - system call wrapper around exec_common()
980Sstevel@tonic-gate */
990Sstevel@tonic-gate int
exece(const char * fname,const char ** argp,const char ** envp)1000Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate int error;
1030Sstevel@tonic-gate
1042712Snn35248 error = exec_common(fname, argp, envp, EBA_NONE);
1050Sstevel@tonic-gate return (error ? (set_errno(error)) : 0);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate int
exec_common(const char * fname,const char ** argp,const char ** envp,int brand_action)1092712Snn35248 exec_common(const char *fname, const char **argp, const char **envp,
1102712Snn35248 int brand_action)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
1130Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
1140Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
1150Sstevel@tonic-gate struct user *up = PTOU(p);
1160Sstevel@tonic-gate long execsz; /* temporary count of exec size */
1170Sstevel@tonic-gate int i;
1180Sstevel@tonic-gate int error;
1190Sstevel@tonic-gate char exec_file[MAXCOMLEN+1];
1200Sstevel@tonic-gate struct pathname pn;
1210Sstevel@tonic-gate struct pathname resolvepn;
1220Sstevel@tonic-gate struct uarg args;
1230Sstevel@tonic-gate struct execa ua;
1240Sstevel@tonic-gate k_sigset_t savedmask;
1250Sstevel@tonic-gate lwpdir_t *lwpdir = NULL;
1269393SRoger.Faulkner@Sun.COM tidhash_t *tidhash;
1270Sstevel@tonic-gate lwpdir_t *old_lwpdir = NULL;
1280Sstevel@tonic-gate uint_t old_lwpdir_sz;
1299393SRoger.Faulkner@Sun.COM tidhash_t *old_tidhash;
1300Sstevel@tonic-gate uint_t old_tidhash_sz;
1319393SRoger.Faulkner@Sun.COM ret_tidhash_t *ret_tidhash;
1320Sstevel@tonic-gate lwpent_t *lep;
1336994Sedp boolean_t brandme = B_FALSE;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * exec() is not supported for the /proc agent lwp.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate if (curthread == p->p_agenttp)
1390Sstevel@tonic-gate return (ENOTSUP);
1400Sstevel@tonic-gate
1412712Snn35248 if (brand_action != EBA_NONE) {
1422712Snn35248 /*
1432712Snn35248 * Brand actions are not supported for processes that are not
1442712Snn35248 * running in a branded zone.
1452712Snn35248 */
1462712Snn35248 if (!ZONE_IS_BRANDED(p->p_zone))
1472712Snn35248 return (ENOTSUP);
1482712Snn35248
1492712Snn35248 if (brand_action == EBA_NATIVE) {
1502712Snn35248 /* Only branded processes can be unbranded */
1512712Snn35248 if (!PROC_IS_BRANDED(p))
1522712Snn35248 return (ENOTSUP);
1532712Snn35248 } else {
1542712Snn35248 /* Only unbranded processes can be branded */
1552712Snn35248 if (PROC_IS_BRANDED(p))
1562712Snn35248 return (ENOTSUP);
1576994Sedp brandme = B_TRUE;
1582712Snn35248 }
1592712Snn35248 } else {
1602712Snn35248 /*
1612712Snn35248 * If this is a native zone, or if the process is already
1622712Snn35248 * branded, then we don't need to do anything. If this is
1632712Snn35248 * a native process in a branded zone, we need to brand the
1642712Snn35248 * process as it exec()s the new binary.
1652712Snn35248 */
1662712Snn35248 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p))
1676994Sedp brandme = B_TRUE;
1682712Snn35248 }
1692712Snn35248
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Inform /proc that an exec() has started.
1720Sstevel@tonic-gate * Hold signals that are ignored by default so that we will
1730Sstevel@tonic-gate * not be interrupted by a signal that will be ignored after
1740Sstevel@tonic-gate * successful completion of gexec().
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate mutex_enter(&p->p_lock);
1770Sstevel@tonic-gate prexecstart();
1780Sstevel@tonic-gate schedctl_finish_sigblock(curthread);
1790Sstevel@tonic-gate savedmask = curthread->t_hold;
1800Sstevel@tonic-gate sigorset(&curthread->t_hold, &ignoredefault);
1810Sstevel@tonic-gate mutex_exit(&p->p_lock);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * Look up path name and remember last component for later.
1850Sstevel@tonic-gate * To help coreadm expand its %d token, we attempt to save
1860Sstevel@tonic-gate * the directory containing the executable in p_execdir. The
1870Sstevel@tonic-gate * first call to lookuppn() may fail and return EINVAL because
1880Sstevel@tonic-gate * dirvpp is non-NULL. In that case, we make a second call to
1890Sstevel@tonic-gate * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
1900Sstevel@tonic-gate * but coreadm is allowed to expand %d to the empty string and
1910Sstevel@tonic-gate * there are other cases in which that failure may occur.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1940Sstevel@tonic-gate goto out;
1950Sstevel@tonic-gate pn_alloc(&resolvepn);
1960Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
1970Sstevel@tonic-gate pn_free(&resolvepn);
1980Sstevel@tonic-gate pn_free(&pn);
1990Sstevel@tonic-gate if (error != EINVAL)
2000Sstevel@tonic-gate goto out;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate dir = NULL;
2030Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
2040Sstevel@tonic-gate goto out;
2050Sstevel@tonic-gate pn_alloc(&resolvepn);
2060Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
2070Sstevel@tonic-gate &vp)) != 0) {
2080Sstevel@tonic-gate pn_free(&resolvepn);
2090Sstevel@tonic-gate pn_free(&pn);
2100Sstevel@tonic-gate goto out;
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate if (vp == NULL) {
2140Sstevel@tonic-gate if (dir != NULL)
2150Sstevel@tonic-gate VN_RELE(dir);
2160Sstevel@tonic-gate error = ENOENT;
2170Sstevel@tonic-gate pn_free(&resolvepn);
2180Sstevel@tonic-gate pn_free(&pn);
2190Sstevel@tonic-gate goto out;
2200Sstevel@tonic-gate }
2211043Scasper
2226134Scasper if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) {
2236134Scasper if (dir != NULL)
2246134Scasper VN_RELE(dir);
2256134Scasper pn_free(&resolvepn);
2266134Scasper pn_free(&pn);
2276134Scasper VN_RELE(vp);
2286134Scasper goto out;
2296134Scasper }
2306134Scasper
2311043Scasper /*
2321043Scasper * We do not allow executing files in attribute directories.
2331043Scasper * We test this by determining whether the resolved path
2341043Scasper * contains a "/" when we're in an attribute directory;
2351043Scasper * only if the pathname does not contain a "/" the resolved path
2361043Scasper * points to a file in the current working (attribute) directory.
2371043Scasper */
2381043Scasper if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
2391043Scasper strchr(resolvepn.pn_path, '/') == NULL) {
2401043Scasper if (dir != NULL)
2411043Scasper VN_RELE(dir);
2421043Scasper error = EACCES;
2431043Scasper pn_free(&resolvepn);
2441043Scasper pn_free(&pn);
2451043Scasper VN_RELE(vp);
2461043Scasper goto out;
2471043Scasper }
2481043Scasper
2490Sstevel@tonic-gate bzero(exec_file, MAXCOMLEN+1);
2500Sstevel@tonic-gate (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
2510Sstevel@tonic-gate bzero(&args, sizeof (args));
2520Sstevel@tonic-gate args.pathname = resolvepn.pn_path;
2530Sstevel@tonic-gate /* don't free resolvepn until we are done with args */
2540Sstevel@tonic-gate pn_free(&pn);
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /*
25712273SCasper.Dik@Sun.COM * If we're running in a profile shell, then call pfexecd.
25812273SCasper.Dik@Sun.COM */
25912273SCasper.Dik@Sun.COM if ((CR_FLAGS(p->p_cred) & PRIV_PFEXEC) != 0) {
26012273SCasper.Dik@Sun.COM error = pfexec_call(p->p_cred, &resolvepn, &args.pfcred,
26112273SCasper.Dik@Sun.COM &args.scrubenv);
26212273SCasper.Dik@Sun.COM
26312273SCasper.Dik@Sun.COM /* Returning errno in case we're not allowed to execute. */
26412273SCasper.Dik@Sun.COM if (error > 0) {
26512273SCasper.Dik@Sun.COM if (dir != NULL)
26612273SCasper.Dik@Sun.COM VN_RELE(dir);
26712273SCasper.Dik@Sun.COM pn_free(&resolvepn);
26812273SCasper.Dik@Sun.COM VN_RELE(vp);
26912273SCasper.Dik@Sun.COM goto out;
27012273SCasper.Dik@Sun.COM }
27112273SCasper.Dik@Sun.COM
27212273SCasper.Dik@Sun.COM /* Don't change the credentials when using old ptrace. */
27312273SCasper.Dik@Sun.COM if (args.pfcred != NULL &&
27412273SCasper.Dik@Sun.COM (p->p_proc_flag & P_PR_PTRACE) != 0) {
27512273SCasper.Dik@Sun.COM crfree(args.pfcred);
27612273SCasper.Dik@Sun.COM args.pfcred = NULL;
27712273SCasper.Dik@Sun.COM args.scrubenv = B_FALSE;
27812273SCasper.Dik@Sun.COM }
27912273SCasper.Dik@Sun.COM }
28012273SCasper.Dik@Sun.COM
28112273SCasper.Dik@Sun.COM /*
2820Sstevel@tonic-gate * Specific exec handlers, or policies determined via
2830Sstevel@tonic-gate * /etc/system may override the historical default.
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate args.stk_prot = PROT_ZFOD;
2860Sstevel@tonic-gate args.dat_prot = PROT_ZFOD;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate CPU_STATS_ADD_K(sys, sysexec, 1);
2890Sstevel@tonic-gate DTRACE_PROC1(exec, char *, args.pathname);
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate ua.fname = fname;
2920Sstevel@tonic-gate ua.argp = argp;
2930Sstevel@tonic-gate ua.envp = envp;
2940Sstevel@tonic-gate
2952712Snn35248 /* If necessary, brand this process before we start the exec. */
2966994Sedp if (brandme)
2972712Snn35248 brand_setbrand(p);
2982712Snn35248
2990Sstevel@tonic-gate if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
3002712Snn35248 exec_file, p->p_cred, brand_action)) != 0) {
3016994Sedp if (brandme)
302*13045SVamsi.Krishna@Sun.COM brand_clearbrand(p, B_FALSE);
3030Sstevel@tonic-gate VN_RELE(vp);
3040Sstevel@tonic-gate if (dir != NULL)
3050Sstevel@tonic-gate VN_RELE(dir);
3060Sstevel@tonic-gate pn_free(&resolvepn);
3070Sstevel@tonic-gate goto fail;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate * Free floating point registers (sun4u only)
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate ASSERT(lwp != NULL);
3140Sstevel@tonic-gate lwp_freeregs(lwp, 1);
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate /*
3171217Srab * Free thread and process context ops.
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate if (curthread->t_ctx)
3200Sstevel@tonic-gate freectx(curthread, 1);
3211217Srab if (p->p_pctx)
3221217Srab freepctx(p, 1);
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * Remember file name for accounting; clear any cached DTrace predicate.
3260Sstevel@tonic-gate */
3270Sstevel@tonic-gate up->u_acflag &= ~AFORK;
3280Sstevel@tonic-gate bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
3290Sstevel@tonic-gate curthread->t_predcache = NULL;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * Clear contract template state
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate lwp_ctmpl_clear(lwp);
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate * Save the directory in which we found the executable for expanding
3380Sstevel@tonic-gate * the %d token used in core file patterns.
3390Sstevel@tonic-gate */
3400Sstevel@tonic-gate mutex_enter(&p->p_lock);
3410Sstevel@tonic-gate tmpvp = p->p_execdir;
3420Sstevel@tonic-gate p->p_execdir = dir;
3430Sstevel@tonic-gate if (p->p_execdir != NULL)
3440Sstevel@tonic-gate VN_HOLD(p->p_execdir);
3450Sstevel@tonic-gate mutex_exit(&p->p_lock);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate if (tmpvp != NULL)
3480Sstevel@tonic-gate VN_RELE(tmpvp);
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate * Reset stack state to the user stack, clear set of signals
3520Sstevel@tonic-gate * caught on the signal stack, and reset list of signals that
3530Sstevel@tonic-gate * restart system calls; the new program's environment should
3540Sstevel@tonic-gate * not be affected by detritus from the old program. Any
3550Sstevel@tonic-gate * pending held signals remain held, so don't clear t_hold.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate mutex_enter(&p->p_lock);
3580Sstevel@tonic-gate lwp->lwp_oldcontext = 0;
3590Sstevel@tonic-gate lwp->lwp_ustack = 0;
3600Sstevel@tonic-gate lwp->lwp_old_stk_ctl = 0;
3610Sstevel@tonic-gate sigemptyset(&up->u_signodefer);
3620Sstevel@tonic-gate sigemptyset(&up->u_sigonstack);
3630Sstevel@tonic-gate sigemptyset(&up->u_sigresethand);
3640Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
3650Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
3660Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * Make saved resource limit == current resource limit.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate for (i = 0; i < RLIM_NLIMITS; i++) {
3720Sstevel@tonic-gate /*CONSTCOND*/
3730Sstevel@tonic-gate if (RLIM_SAVED(i)) {
3740Sstevel@tonic-gate (void) rctl_rlimit_get(rctlproc_legacy[i], p,
3750Sstevel@tonic-gate &up->u_saved_rlimit[i]);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate * If the action was to catch the signal, then the action
3810Sstevel@tonic-gate * must be reset to SIG_DFL.
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate sigdefault(p);
3840Sstevel@tonic-gate p->p_flag &= ~(SNOWAIT|SJCTL);
3850Sstevel@tonic-gate p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
3860Sstevel@tonic-gate up->u_signal[SIGCLD - 1] = SIG_DFL;
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Delete the dot4 sigqueues/signotifies.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate sigqfree(p);
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate mutex_exit(&p->p_lock);
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate mutex_enter(&p->p_pflock);
3960Sstevel@tonic-gate p->p_prof.pr_base = NULL;
3970Sstevel@tonic-gate p->p_prof.pr_size = 0;
3980Sstevel@tonic-gate p->p_prof.pr_off = 0;
3990Sstevel@tonic-gate p->p_prof.pr_scale = 0;
4000Sstevel@tonic-gate p->p_prof.pr_samples = 0;
4010Sstevel@tonic-gate mutex_exit(&p->p_pflock);
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL);
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate #if defined(__sparc)
4060Sstevel@tonic-gate if (p->p_utraps != NULL)
4070Sstevel@tonic-gate utrap_free(p);
4080Sstevel@tonic-gate #endif /* __sparc */
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate * Close all close-on-exec files.
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate close_exec(P_FINFO(p));
4140Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
4152712Snn35248
4166994Sedp /* Unbrand ourself if necessary. */
4176994Sedp if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE))
418*13045SVamsi.Krishna@Sun.COM brand_clearbrand(p, B_FALSE);
4192712Snn35248
4200Sstevel@tonic-gate setregs(&args);
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate /* Mark this as an executable vnode */
4230Sstevel@tonic-gate mutex_enter(&vp->v_lock);
4240Sstevel@tonic-gate vp->v_flag |= VVMEXEC;
4250Sstevel@tonic-gate mutex_exit(&vp->v_lock);
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate VN_RELE(vp);
4280Sstevel@tonic-gate if (dir != NULL)
4290Sstevel@tonic-gate VN_RELE(dir);
4300Sstevel@tonic-gate pn_free(&resolvepn);
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * Allocate a new lwp directory and lwpid hash table if necessary.
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
4360Sstevel@tonic-gate lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
4370Sstevel@tonic-gate lwpdir->ld_next = lwpdir + 1;
4389393SRoger.Faulkner@Sun.COM tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP);
4390Sstevel@tonic-gate if (p->p_lwpdir != NULL)
4400Sstevel@tonic-gate lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
4410Sstevel@tonic-gate else
4420Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate
4452712Snn35248 if (PROC_IS_BRANDED(p))
4462712Snn35248 BROP(p)->b_exec();
4472712Snn35248
4480Sstevel@tonic-gate mutex_enter(&p->p_lock);
4490Sstevel@tonic-gate prbarrier(p);
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate * Reset lwp id to the default value of 1.
4530Sstevel@tonic-gate * This is a single-threaded process now
4540Sstevel@tonic-gate * and lwp #1 is lwp_wait()able by default.
4550Sstevel@tonic-gate * The t_unpark flag should not be inherited.
4560Sstevel@tonic-gate */
4570Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
4580Sstevel@tonic-gate curthread->t_tid = 1;
4594426Saguzovsk kpreempt_disable();
4604426Saguzovsk ASSERT(curthread->t_lpl != NULL);
4614426Saguzovsk p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid;
4624426Saguzovsk kpreempt_enable();
4634426Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) {
4644426Saguzovsk lgrp_update_trthr_migrations(1);
4654426Saguzovsk }
4660Sstevel@tonic-gate curthread->t_unpark = 0;
4670Sstevel@tonic-gate curthread->t_proc_flag |= TP_TWAIT;
4680Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */
4690Sstevel@tonic-gate p->p_lwpdaemon = 0; /* but oh well ... */
4700Sstevel@tonic-gate p->p_lwpid = 1;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate * Install the newly-allocated lwp directory and lwpid hash table
4740Sstevel@tonic-gate * and insert the current thread into the new hash table.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate if (lwpdir != NULL) {
4770Sstevel@tonic-gate old_lwpdir = p->p_lwpdir;
4780Sstevel@tonic-gate old_lwpdir_sz = p->p_lwpdir_sz;
4790Sstevel@tonic-gate old_tidhash = p->p_tidhash;
4800Sstevel@tonic-gate old_tidhash_sz = p->p_tidhash_sz;
4810Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = lwpdir;
4820Sstevel@tonic-gate p->p_lwpdir_sz = 2;
4830Sstevel@tonic-gate lep->le_thread = curthread;
4840Sstevel@tonic-gate lep->le_lwpid = curthread->t_tid;
4850Sstevel@tonic-gate lep->le_start = curthread->t_start;
4869393SRoger.Faulkner@Sun.COM lwp_hash_in(p, lep, tidhash, 2, 0);
4879393SRoger.Faulkner@Sun.COM p->p_tidhash = tidhash;
4889393SRoger.Faulkner@Sun.COM p->p_tidhash_sz = 2;
4890Sstevel@tonic-gate }
4909393SRoger.Faulkner@Sun.COM ret_tidhash = p->p_ret_tidhash;
4919393SRoger.Faulkner@Sun.COM p->p_ret_tidhash = NULL;
4922712Snn35248
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate * Restore the saved signal mask and
4950Sstevel@tonic-gate * inform /proc that the exec() has finished.
4960Sstevel@tonic-gate */
4970Sstevel@tonic-gate curthread->t_hold = savedmask;
4980Sstevel@tonic-gate prexecend();
4990Sstevel@tonic-gate mutex_exit(&p->p_lock);
5000Sstevel@tonic-gate if (old_lwpdir) {
5010Sstevel@tonic-gate kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
5029393SRoger.Faulkner@Sun.COM kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t));
5039393SRoger.Faulkner@Sun.COM }
5049393SRoger.Faulkner@Sun.COM while (ret_tidhash != NULL) {
5059393SRoger.Faulkner@Sun.COM ret_tidhash_t *next = ret_tidhash->rth_next;
5069393SRoger.Faulkner@Sun.COM kmem_free(ret_tidhash->rth_tidhash,
5079393SRoger.Faulkner@Sun.COM ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
5089393SRoger.Faulkner@Sun.COM kmem_free(ret_tidhash, sizeof (*ret_tidhash));
5099393SRoger.Faulkner@Sun.COM ret_tidhash = next;
5100Sstevel@tonic-gate }
5112712Snn35248
5120Sstevel@tonic-gate ASSERT(error == 0);
5130Sstevel@tonic-gate DTRACE_PROC(exec__success);
5140Sstevel@tonic-gate return (0);
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate fail:
5170Sstevel@tonic-gate DTRACE_PROC1(exec__failure, int, error);
5180Sstevel@tonic-gate out: /* error return */
5190Sstevel@tonic-gate mutex_enter(&p->p_lock);
5200Sstevel@tonic-gate curthread->t_hold = savedmask;
5210Sstevel@tonic-gate prexecend();
5220Sstevel@tonic-gate mutex_exit(&p->p_lock);
5230Sstevel@tonic-gate ASSERT(error != 0);
5240Sstevel@tonic-gate return (error);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate * Perform generic exec duties and switchout to object-file specific
5300Sstevel@tonic-gate * handler.
5310Sstevel@tonic-gate */
5320Sstevel@tonic-gate int
gexec(struct vnode ** vpp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,caddr_t exec_file,struct cred * cred,int brand_action)5330Sstevel@tonic-gate gexec(
5340Sstevel@tonic-gate struct vnode **vpp,
5350Sstevel@tonic-gate struct execa *uap,
5360Sstevel@tonic-gate struct uarg *args,
5370Sstevel@tonic-gate struct intpdata *idatap,
5380Sstevel@tonic-gate int level,
5390Sstevel@tonic-gate long *execsz,
5400Sstevel@tonic-gate caddr_t exec_file,
5412712Snn35248 struct cred *cred,
5422712Snn35248 int brand_action)
5430Sstevel@tonic-gate {
54411736SDonghai.Qiao@Sun.COM struct vnode *vp, *execvp = NULL;
5450Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
5460Sstevel@tonic-gate struct execsw *eswp;
5470Sstevel@tonic-gate int error = 0;
5480Sstevel@tonic-gate int suidflags = 0;
5490Sstevel@tonic-gate ssize_t resid;
5500Sstevel@tonic-gate uid_t uid, gid;
5510Sstevel@tonic-gate struct vattr vattr;
5520Sstevel@tonic-gate char magbuf[MAGIC_BYTES];
5530Sstevel@tonic-gate int setid;
5540Sstevel@tonic-gate cred_t *oldcred, *newcred = NULL;
5550Sstevel@tonic-gate int privflags = 0;
5561335Scasper int setidfl;
55712273SCasper.Dik@Sun.COM priv_set_t fset;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate * If the SNOCD or SUGID flag is set, turn it off and remember the
5610Sstevel@tonic-gate * previous setting so we can restore it if we encounter an error.
5620Sstevel@tonic-gate */
5630Sstevel@tonic-gate if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
5640Sstevel@tonic-gate mutex_enter(&pp->p_lock);
5650Sstevel@tonic-gate suidflags = pp->p_flag & PSUIDFLAGS;
5660Sstevel@tonic-gate pp->p_flag &= ~PSUIDFLAGS;
5670Sstevel@tonic-gate mutex_exit(&pp->p_lock);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate if ((error = execpermissions(*vpp, &vattr, args)) != 0)
57111736SDonghai.Qiao@Sun.COM goto bad_noclose;
5720Sstevel@tonic-gate
57311736SDonghai.Qiao@Sun.COM /* need to open vnode for stateful file systems */
5745331Samw if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0)
57511736SDonghai.Qiao@Sun.COM goto bad_noclose;
5760Sstevel@tonic-gate vp = *vpp;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate * Note: to support binary compatibility with SunOS a.out
5800Sstevel@tonic-gate * executables, we read in the first four bytes, as the
5810Sstevel@tonic-gate * magic number is in bytes 2-3.
5820Sstevel@tonic-gate */
5830Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
5840Sstevel@tonic-gate (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
5850Sstevel@tonic-gate goto bad;
5860Sstevel@tonic-gate if (resid != 0)
5870Sstevel@tonic-gate goto bad;
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate if ((eswp = findexec_by_hdr(magbuf)) == NULL)
5900Sstevel@tonic-gate goto bad;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate if (level == 0 &&
59312273SCasper.Dik@Sun.COM (privflags = execsetid(vp, &vattr, &uid, &gid, &fset,
59412273SCasper.Dik@Sun.COM args->pfcred == NULL ? cred : args->pfcred, args->pathname)) != 0) {
59512273SCasper.Dik@Sun.COM
59612273SCasper.Dik@Sun.COM /* Pfcred is a credential with a ref count of 1 */
5970Sstevel@tonic-gate
59812273SCasper.Dik@Sun.COM if (args->pfcred != NULL) {
59912273SCasper.Dik@Sun.COM privflags |= PRIV_INCREASE|PRIV_RESET;
60012273SCasper.Dik@Sun.COM newcred = cred = args->pfcred;
60112273SCasper.Dik@Sun.COM } else {
60212273SCasper.Dik@Sun.COM newcred = cred = crdup(cred);
60312273SCasper.Dik@Sun.COM }
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate /* If we can, drop the PA bit */
6060Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0)
6070Sstevel@tonic-gate priv_adjust_PA(cred);
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate if (privflags & PRIV_SETID) {
6100Sstevel@tonic-gate cred->cr_uid = uid;
6110Sstevel@tonic-gate cred->cr_gid = gid;
6120Sstevel@tonic-gate cred->cr_suid = uid;
6130Sstevel@tonic-gate cred->cr_sgid = gid;
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate
6161676Sjpk if (privflags & MAC_FLAGS) {
6171676Sjpk if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
6181676Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE;
6191676Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
6201676Sjpk }
6211676Sjpk
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate * Implement the privilege updates:
6240Sstevel@tonic-gate *
6250Sstevel@tonic-gate * Restrict with L:
6260Sstevel@tonic-gate *
6270Sstevel@tonic-gate * I' = I & L
6280Sstevel@tonic-gate *
6290Sstevel@tonic-gate * E' = P' = (I' + F) & A
6300Sstevel@tonic-gate *
63112273SCasper.Dik@Sun.COM * But if running under ptrace, we cap I and F with P.
6320Sstevel@tonic-gate */
63312273SCasper.Dik@Sun.COM if ((privflags & (PRIV_RESET|PRIV_FORCED)) != 0) {
6340Sstevel@tonic-gate if ((privflags & PRIV_INCREASE) != 0 &&
63512273SCasper.Dik@Sun.COM (pp->p_proc_flag & P_PR_PTRACE) != 0) {
6360Sstevel@tonic-gate priv_intersect(&CR_OPPRIV(cred),
6375753Sgww &CR_IPRIV(cred));
63812273SCasper.Dik@Sun.COM priv_intersect(&CR_OPPRIV(cred), &fset);
63912273SCasper.Dik@Sun.COM }
6400Sstevel@tonic-gate priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
6410Sstevel@tonic-gate CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
64212273SCasper.Dik@Sun.COM if (privflags & PRIV_FORCED) {
64312273SCasper.Dik@Sun.COM priv_set_PA(cred);
64412273SCasper.Dik@Sun.COM priv_union(&fset, &CR_EPRIV(cred));
64512273SCasper.Dik@Sun.COM priv_union(&fset, &CR_PPRIV(cred));
64612273SCasper.Dik@Sun.COM }
6470Sstevel@tonic-gate priv_adjust_PA(cred);
6480Sstevel@tonic-gate }
64912273SCasper.Dik@Sun.COM } else if (level == 0 && args->pfcred != NULL) {
65012273SCasper.Dik@Sun.COM newcred = cred = args->pfcred;
65112273SCasper.Dik@Sun.COM privflags |= PRIV_INCREASE;
65212273SCasper.Dik@Sun.COM /* pfcred is not forced to adhere to these settings */
65312273SCasper.Dik@Sun.COM priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
65412273SCasper.Dik@Sun.COM CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
65512273SCasper.Dik@Sun.COM priv_adjust_PA(cred);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /* SunOS 4.x buy-back */
6590Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
6600Sstevel@tonic-gate (vattr.va_mode & (VSUID|VSGID))) {
6619068Sjason@ansipunx.net char path[MAXNAMELEN];
6629068Sjason@ansipunx.net refstr_t *mntpt = NULL;
6639068Sjason@ansipunx.net int ret = -1;
6649068Sjason@ansipunx.net
6659068Sjason@ansipunx.net bzero(path, sizeof (path));
6669068Sjason@ansipunx.net zone_hold(pp->p_zone);
6679068Sjason@ansipunx.net
6689068Sjason@ansipunx.net ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path,
6699068Sjason@ansipunx.net sizeof (path), cred);
6709068Sjason@ansipunx.net
6719068Sjason@ansipunx.net /* fallback to mountpoint if a path can't be found */
6729068Sjason@ansipunx.net if ((ret != 0) || (ret == 0 && path[0] == '\0'))
6739068Sjason@ansipunx.net mntpt = vfs_getmntpoint(vp->v_vfsp);
6749068Sjason@ansipunx.net
6759068Sjason@ansipunx.net if (mntpt == NULL)
6769068Sjason@ansipunx.net zcmn_err(pp->p_zone->zone_id, CE_NOTE,
6779068Sjason@ansipunx.net "!uid %d: setuid execution not allowed, "
6789068Sjason@ansipunx.net "file=%s", cred->cr_uid, path);
6799068Sjason@ansipunx.net else
6809068Sjason@ansipunx.net zcmn_err(pp->p_zone->zone_id, CE_NOTE,
6819068Sjason@ansipunx.net "!uid %d: setuid execution not allowed, "
6829068Sjason@ansipunx.net "fs=%s, file=%s", cred->cr_uid,
6839068Sjason@ansipunx.net ZONE_PATH_TRANSLATE(refstr_value(mntpt),
6849068Sjason@ansipunx.net pp->p_zone), exec_file);
6859068Sjason@ansipunx.net
6869068Sjason@ansipunx.net if (!INGLOBALZONE(pp)) {
6879068Sjason@ansipunx.net /* zone_rootpath always has trailing / */
6889068Sjason@ansipunx.net if (mntpt == NULL)
6899068Sjason@ansipunx.net cmn_err(CE_NOTE, "!zone: %s, uid: %d "
6909068Sjason@ansipunx.net "setuid execution not allowed, file=%s%s",
6919068Sjason@ansipunx.net pp->p_zone->zone_name, cred->cr_uid,
6929068Sjason@ansipunx.net pp->p_zone->zone_rootpath, path + 1);
6939068Sjason@ansipunx.net else
6949068Sjason@ansipunx.net cmn_err(CE_NOTE, "!zone: %s, uid: %d "
6959068Sjason@ansipunx.net "setuid execution not allowed, fs=%s, "
6969068Sjason@ansipunx.net "file=%s", pp->p_zone->zone_name,
6979068Sjason@ansipunx.net cred->cr_uid, refstr_value(mntpt),
6989068Sjason@ansipunx.net exec_file);
6999068Sjason@ansipunx.net }
7009068Sjason@ansipunx.net
7019068Sjason@ansipunx.net if (mntpt != NULL)
7029068Sjason@ansipunx.net refstr_rele(mntpt);
7039068Sjason@ansipunx.net
7049068Sjason@ansipunx.net zone_rele(pp->p_zone);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate * execsetid() told us whether or not we had to change the
7090Sstevel@tonic-gate * credentials of the process. In privflags, it told us
7100Sstevel@tonic-gate * whether we gained any privileges or executed a set-uid executable.
7110Sstevel@tonic-gate */
71212273SCasper.Dik@Sun.COM setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE|PRIV_FORCED));
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate * Use /etc/system variable to determine if the stack
7160Sstevel@tonic-gate * should be marked as executable by default.
7170Sstevel@tonic-gate */
7180Sstevel@tonic-gate if (noexec_user_stack)
7190Sstevel@tonic-gate args->stk_prot &= ~PROT_EXEC;
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
7224528Spaulsan args->ex_vp = vp;
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate * Traditionally, the setid flags told the sub processes whether
7260Sstevel@tonic-gate * the file just executed was set-uid or set-gid; this caused
7270Sstevel@tonic-gate * some confusion as the 'setid' flag did not match the SUGID
7280Sstevel@tonic-gate * process flag which is only set when the uids/gids do not match.
7290Sstevel@tonic-gate * A script set-gid/set-uid to the real uid/gid would start with
7300Sstevel@tonic-gate * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
7310Sstevel@tonic-gate * Now we flag those cases where the calling process cannot
7320Sstevel@tonic-gate * be trusted to influence the newly exec'ed process, either
7330Sstevel@tonic-gate * because it runs with more privileges or when the uids/gids
7340Sstevel@tonic-gate * do in fact not match.
7350Sstevel@tonic-gate * This also makes the runtime linker agree with the on exec
7360Sstevel@tonic-gate * values of SNOCD and SUGID.
7370Sstevel@tonic-gate */
7381335Scasper setidfl = 0;
7391335Scasper if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
7401335Scasper !supgroupmember(cred->cr_gid, cred))) {
7411335Scasper setidfl |= EXECSETID_UGIDS;
7421335Scasper }
7431335Scasper if (setid & PRIV_SETUGID)
7441335Scasper setidfl |= EXECSETID_SETID;
74512273SCasper.Dik@Sun.COM if (setid & PRIV_FORCED)
7461335Scasper setidfl |= EXECSETID_PRIVS;
7471335Scasper
74811736SDonghai.Qiao@Sun.COM execvp = pp->p_exec;
74911736SDonghai.Qiao@Sun.COM if (execvp)
75011736SDonghai.Qiao@Sun.COM VN_HOLD(execvp);
75111736SDonghai.Qiao@Sun.COM
7520Sstevel@tonic-gate error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
7535753Sgww setidfl, exec_file, cred, brand_action);
7540Sstevel@tonic-gate rw_exit(eswp->exec_lock);
7550Sstevel@tonic-gate if (error != 0) {
75611736SDonghai.Qiao@Sun.COM if (execvp)
75711736SDonghai.Qiao@Sun.COM VN_RELE(execvp);
75811974SDonghai.Qiao@Sun.COM /*
75911974SDonghai.Qiao@Sun.COM * If this process's p_exec has been set to the vp of
76011974SDonghai.Qiao@Sun.COM * the executable by exec_func, we will return without
76111974SDonghai.Qiao@Sun.COM * calling VOP_CLOSE because proc_exit will close it
76211974SDonghai.Qiao@Sun.COM * on exit.
76311974SDonghai.Qiao@Sun.COM */
76411974SDonghai.Qiao@Sun.COM if (pp->p_exec == vp)
76511974SDonghai.Qiao@Sun.COM goto bad_noclose;
76611974SDonghai.Qiao@Sun.COM else
76711974SDonghai.Qiao@Sun.COM goto bad;
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate if (level == 0) {
77112273SCasper.Dik@Sun.COM uid_t oruid;
77212273SCasper.Dik@Sun.COM
77311736SDonghai.Qiao@Sun.COM if (execvp != NULL) {
77411736SDonghai.Qiao@Sun.COM /*
77511736SDonghai.Qiao@Sun.COM * Close the previous executable only if we are
77611736SDonghai.Qiao@Sun.COM * at level 0.
77711736SDonghai.Qiao@Sun.COM */
77811736SDonghai.Qiao@Sun.COM (void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0,
77911736SDonghai.Qiao@Sun.COM cred, NULL);
78011736SDonghai.Qiao@Sun.COM }
78111736SDonghai.Qiao@Sun.COM
7820Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
78312273SCasper.Dik@Sun.COM
78412273SCasper.Dik@Sun.COM oruid = pp->p_cred->cr_ruid;
78512273SCasper.Dik@Sun.COM
7860Sstevel@tonic-gate if (newcred != NULL) {
7870Sstevel@tonic-gate /*
7880Sstevel@tonic-gate * Free the old credentials, and set the new ones.
7890Sstevel@tonic-gate * Do this for both the process and the (single) thread.
7900Sstevel@tonic-gate */
7910Sstevel@tonic-gate crfree(pp->p_cred);
7920Sstevel@tonic-gate pp->p_cred = cred; /* cred already held for proc */
7930Sstevel@tonic-gate crhold(cred); /* hold new cred for thread */
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred
7960Sstevel@tonic-gate * must always be either NULL, or point to a valid,
7970Sstevel@tonic-gate * allocated cred structure.
7980Sstevel@tonic-gate */
7990Sstevel@tonic-gate oldcred = curthread->t_cred;
8000Sstevel@tonic-gate curthread->t_cred = cred;
8010Sstevel@tonic-gate crfree(oldcred);
80211537SCasper.Dik@Sun.COM
80311569SCasper.Dik@Sun.COM if (priv_basic_test >= 0 &&
80411537SCasper.Dik@Sun.COM !PRIV_ISASSERT(&CR_IPRIV(newcred),
80511537SCasper.Dik@Sun.COM priv_basic_test)) {
80611537SCasper.Dik@Sun.COM pid_t pid = pp->p_pid;
80711537SCasper.Dik@Sun.COM char *fn = PTOU(pp)->u_comm;
80811537SCasper.Dik@Sun.COM
80911537SCasper.Dik@Sun.COM cmn_err(CE_WARN, "%s[%d]: exec: basic_test "
81011537SCasper.Dik@Sun.COM "privilege removed from E/I", fn, pid);
81111537SCasper.Dik@Sun.COM }
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate /*
8140Sstevel@tonic-gate * On emerging from a successful exec(), the saved
8150Sstevel@tonic-gate * uid and gid equal the effective uid and gid.
8160Sstevel@tonic-gate */
8170Sstevel@tonic-gate cred->cr_suid = cred->cr_uid;
8180Sstevel@tonic-gate cred->cr_sgid = cred->cr_gid;
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate /*
8210Sstevel@tonic-gate * If the real and effective ids do not match, this
8220Sstevel@tonic-gate * is a setuid process that should not dump core.
8230Sstevel@tonic-gate * The group comparison is tricky; we prevent the code
8240Sstevel@tonic-gate * from flagging SNOCD when executing with an effective gid
8250Sstevel@tonic-gate * which is a supplementary group.
8260Sstevel@tonic-gate */
8270Sstevel@tonic-gate if (cred->cr_ruid != cred->cr_uid ||
8280Sstevel@tonic-gate (cred->cr_rgid != cred->cr_gid &&
8290Sstevel@tonic-gate !supgroupmember(cred->cr_gid, cred)) ||
8300Sstevel@tonic-gate (privflags & PRIV_INCREASE) != 0)
8310Sstevel@tonic-gate suidflags = PSUIDFLAGS;
8320Sstevel@tonic-gate else
8330Sstevel@tonic-gate suidflags = 0;
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
83612273SCasper.Dik@Sun.COM if (newcred != NULL && oruid != newcred->cr_ruid) {
83712273SCasper.Dik@Sun.COM /* Note that the process remains in the same zone. */
83812273SCasper.Dik@Sun.COM mutex_enter(&pidlock);
83912273SCasper.Dik@Sun.COM upcount_dec(oruid, crgetzoneid(newcred));
84012273SCasper.Dik@Sun.COM upcount_inc(newcred->cr_ruid, crgetzoneid(newcred));
84112273SCasper.Dik@Sun.COM mutex_exit(&pidlock);
84212273SCasper.Dik@Sun.COM }
8430Sstevel@tonic-gate if (suidflags) {
8440Sstevel@tonic-gate mutex_enter(&pp->p_lock);
8450Sstevel@tonic-gate pp->p_flag |= suidflags;
8460Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate * If process is traced via /proc, arrange to
8510Sstevel@tonic-gate * invalidate the associated /proc vnode.
8520Sstevel@tonic-gate */
8530Sstevel@tonic-gate if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
8540Sstevel@tonic-gate args->traceinval = 1;
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate if (pp->p_proc_flag & P_PR_PTRACE)
8570Sstevel@tonic-gate psignal(pp, SIGTRAP);
8580Sstevel@tonic-gate if (args->traceinval)
8590Sstevel@tonic-gate prinvalidate(&pp->p_user);
8600Sstevel@tonic-gate }
86111736SDonghai.Qiao@Sun.COM if (execvp)
86211736SDonghai.Qiao@Sun.COM VN_RELE(execvp);
8630Sstevel@tonic-gate return (0);
86411736SDonghai.Qiao@Sun.COM
8650Sstevel@tonic-gate bad:
86611736SDonghai.Qiao@Sun.COM (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
86711736SDonghai.Qiao@Sun.COM
86811736SDonghai.Qiao@Sun.COM bad_noclose:
86911974SDonghai.Qiao@Sun.COM if (newcred != NULL)
87011974SDonghai.Qiao@Sun.COM crfree(newcred);
8710Sstevel@tonic-gate if (error == 0)
8720Sstevel@tonic-gate error = ENOEXEC;
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate if (suidflags) {
8750Sstevel@tonic-gate mutex_enter(&pp->p_lock);
8760Sstevel@tonic-gate pp->p_flag |= suidflags;
8770Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate return (error);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate extern char *execswnames[];
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate struct execsw *
allocate_execsw(char * name,char * magic,size_t magic_size)8850Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate int i, j;
8880Sstevel@tonic-gate char *ename;
8890Sstevel@tonic-gate char *magicp;
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate mutex_enter(&execsw_lock);
8920Sstevel@tonic-gate for (i = 0; i < nexectype; i++) {
8930Sstevel@tonic-gate if (execswnames[i] == NULL) {
8940Sstevel@tonic-gate ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8950Sstevel@tonic-gate (void) strcpy(ename, name);
8960Sstevel@tonic-gate execswnames[i] = ename;
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate * Set the magic number last so that we
8990Sstevel@tonic-gate * don't need to hold the execsw_lock in
9000Sstevel@tonic-gate * findexectype().
9010Sstevel@tonic-gate */
9020Sstevel@tonic-gate magicp = kmem_alloc(magic_size, KM_SLEEP);
9030Sstevel@tonic-gate for (j = 0; j < magic_size; j++)
9040Sstevel@tonic-gate magicp[j] = magic[j];
9050Sstevel@tonic-gate execsw[i].exec_magic = magicp;
9060Sstevel@tonic-gate mutex_exit(&execsw_lock);
9070Sstevel@tonic-gate return (&execsw[i]);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate mutex_exit(&execsw_lock);
9110Sstevel@tonic-gate return (NULL);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate * Find the exec switch table entry with the corresponding magic string.
9160Sstevel@tonic-gate */
9170Sstevel@tonic-gate struct execsw *
findexecsw(char * magic)9180Sstevel@tonic-gate findexecsw(char *magic)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate struct execsw *eswp;
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9230Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9240Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
9250Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
9260Sstevel@tonic-gate return (eswp);
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate return (NULL);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate * Find the execsw[] index for the given exec header string by looking for the
9330Sstevel@tonic-gate * magic string at a specified offset and length for each kind of executable
9340Sstevel@tonic-gate * file format until one matches. If no execsw[] entry is found, try to
9350Sstevel@tonic-gate * autoload a module for this magic string.
9360Sstevel@tonic-gate */
9370Sstevel@tonic-gate struct execsw *
findexec_by_hdr(char * header)9380Sstevel@tonic-gate findexec_by_hdr(char *header)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate struct execsw *eswp;
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9430Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9440Sstevel@tonic-gate if (header && eswp->exec_maglen != 0 &&
9450Sstevel@tonic-gate bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
9465753Sgww eswp->exec_maglen) == 0) {
9470Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
9480Sstevel@tonic-gate return (NULL);
9490Sstevel@tonic-gate return (eswp);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate return (NULL); /* couldn't find the type */
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate * Find the execsw[] index for the given magic string. If no execsw[] entry
9570Sstevel@tonic-gate * is found, try to autoload a module for this magic string.
9580Sstevel@tonic-gate */
9590Sstevel@tonic-gate struct execsw *
findexec_by_magic(char * magic)9600Sstevel@tonic-gate findexec_by_magic(char *magic)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate struct execsw *eswp;
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9650Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9660Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
9670Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
9680Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
9690Sstevel@tonic-gate return (NULL);
9700Sstevel@tonic-gate return (eswp);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate return (NULL); /* couldn't find the type */
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate static int
hold_execsw(struct execsw * eswp)9770Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate char *name;
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
9820Sstevel@tonic-gate while (!LOADED_EXEC(eswp)) {
9830Sstevel@tonic-gate rw_exit(eswp->exec_lock);
9840Sstevel@tonic-gate name = execswnames[eswp-execsw];
9850Sstevel@tonic-gate ASSERT(name);
9860Sstevel@tonic-gate if (modload("exec", name) == -1)
9870Sstevel@tonic-gate return (-1);
9880Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate return (0);
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate static int
execsetid(struct vnode * vp,struct vattr * vattrp,uid_t * uidp,uid_t * gidp,priv_set_t * fset,cred_t * cr,const char * pathname)99412273SCasper.Dik@Sun.COM execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp,
99512273SCasper.Dik@Sun.COM priv_set_t *fset, cred_t *cr, const char *pathname)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
9980Sstevel@tonic-gate uid_t uid, gid;
9990Sstevel@tonic-gate int privflags = 0;
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate /*
10020Sstevel@tonic-gate * Remember credentials.
10030Sstevel@tonic-gate */
10040Sstevel@tonic-gate uid = cr->cr_uid;
10050Sstevel@tonic-gate gid = cr->cr_gid;
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate /* Will try to reset the PRIV_AWARE bit later. */
10080Sstevel@tonic-gate if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
10090Sstevel@tonic-gate privflags |= PRIV_RESET;
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
10120Sstevel@tonic-gate /*
101312273SCasper.Dik@Sun.COM * If it's a set-uid root program we perform the
101412273SCasper.Dik@Sun.COM * forced privilege look-aside. This has three possible
101512273SCasper.Dik@Sun.COM * outcomes:
101612273SCasper.Dik@Sun.COM * no look aside information -> treat as before
101712273SCasper.Dik@Sun.COM * look aside in Limit set -> apply forced privs
101812273SCasper.Dik@Sun.COM * look aside not in Limit set -> ignore set-uid root
101912273SCasper.Dik@Sun.COM *
102012273SCasper.Dik@Sun.COM * Ordinary set-uid root execution only allowed if the limit
102112273SCasper.Dik@Sun.COM * set holds all unsafe privileges.
10220Sstevel@tonic-gate */
102312273SCasper.Dik@Sun.COM if (vattrp->va_mode & VSUID) {
102412273SCasper.Dik@Sun.COM if (vattrp->va_uid == 0) {
102512273SCasper.Dik@Sun.COM int res = get_forced_privs(cr, pathname, fset);
102612273SCasper.Dik@Sun.COM
102712273SCasper.Dik@Sun.COM switch (res) {
102812273SCasper.Dik@Sun.COM case -1:
102912273SCasper.Dik@Sun.COM if (priv_issubset(&priv_unsafe,
103012273SCasper.Dik@Sun.COM &CR_LPRIV(cr))) {
103112273SCasper.Dik@Sun.COM uid = vattrp->va_uid;
103212273SCasper.Dik@Sun.COM privflags |= PRIV_SETUGID;
103312273SCasper.Dik@Sun.COM }
103412273SCasper.Dik@Sun.COM break;
103512273SCasper.Dik@Sun.COM case 0:
103612273SCasper.Dik@Sun.COM privflags |= PRIV_FORCED|PRIV_INCREASE;
103712273SCasper.Dik@Sun.COM break;
103812273SCasper.Dik@Sun.COM default:
103912273SCasper.Dik@Sun.COM break;
104012273SCasper.Dik@Sun.COM }
104112273SCasper.Dik@Sun.COM } else {
104212273SCasper.Dik@Sun.COM uid = vattrp->va_uid;
104312273SCasper.Dik@Sun.COM privflags |= PRIV_SETUGID;
104412273SCasper.Dik@Sun.COM }
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate if (vattrp->va_mode & VSGID) {
10470Sstevel@tonic-gate gid = vattrp->va_gid;
10480Sstevel@tonic-gate privflags |= PRIV_SETUGID;
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate /*
10530Sstevel@tonic-gate * Do we need to change our credential anyway?
10540Sstevel@tonic-gate * This is the case when E != I or P != I, as
10550Sstevel@tonic-gate * we need to do the assignments (with F empty and A full)
10560Sstevel@tonic-gate * Or when I is not a subset of L; in that case we need to
10570Sstevel@tonic-gate * enforce L.
10580Sstevel@tonic-gate *
10590Sstevel@tonic-gate * I' = L & I
10600Sstevel@tonic-gate *
10610Sstevel@tonic-gate * E' = P' = (I' + F) & A
10620Sstevel@tonic-gate * or
10630Sstevel@tonic-gate * E' = P' = I'
10640Sstevel@tonic-gate */
10650Sstevel@tonic-gate if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
10660Sstevel@tonic-gate !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
10670Sstevel@tonic-gate !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
10680Sstevel@tonic-gate privflags |= PRIV_RESET;
10690Sstevel@tonic-gate
107012273SCasper.Dik@Sun.COM /* Child has more privileges than parent */
107112273SCasper.Dik@Sun.COM if (!priv_issubset(&CR_IPRIV(cr), &CR_PPRIV(cr)))
107212273SCasper.Dik@Sun.COM privflags |= PRIV_INCREASE;
107312273SCasper.Dik@Sun.COM
10741676Sjpk /* If MAC-aware flag(s) are on, need to update cred to remove. */
10751676Sjpk if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
10761676Sjpk (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
10771676Sjpk privflags |= MAC_FLAGS;
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate * Set setuid/setgid protections if no ptrace() compatibility.
10800Sstevel@tonic-gate * For privileged processes, honor setuid/setgid even in
10810Sstevel@tonic-gate * the presence of ptrace() compatibility.
10820Sstevel@tonic-gate */
10830Sstevel@tonic-gate if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
10840Sstevel@tonic-gate PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
10850Sstevel@tonic-gate (cr->cr_uid != uid ||
10860Sstevel@tonic-gate cr->cr_gid != gid ||
10870Sstevel@tonic-gate cr->cr_suid != uid ||
10880Sstevel@tonic-gate cr->cr_sgid != gid)) {
10890Sstevel@tonic-gate *uidp = uid;
10900Sstevel@tonic-gate *gidp = gid;
10910Sstevel@tonic-gate privflags |= PRIV_SETID;
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate return (privflags);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate int
execpermissions(struct vnode * vp,struct vattr * vattrp,struct uarg * args)10970Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate int error;
11000Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
11010Sstevel@tonic-gate
11020Sstevel@tonic-gate vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
11035331Samw if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL))
11040Sstevel@tonic-gate return (error);
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate * Check the access mode.
11070Sstevel@tonic-gate * If VPROC, ask /proc if the file is an object file.
11080Sstevel@tonic-gate */
11095331Samw if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 ||
11100Sstevel@tonic-gate !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
11110Sstevel@tonic-gate (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
11120Sstevel@tonic-gate (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
11130Sstevel@tonic-gate if (error == 0)
11140Sstevel@tonic-gate error = EACCES;
11150Sstevel@tonic-gate return (error);
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
11195331Samw (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) {
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate * If process is under ptrace(2) compatibility,
11220Sstevel@tonic-gate * fail the exec(2).
11230Sstevel@tonic-gate */
11240Sstevel@tonic-gate if (p->p_proc_flag & P_PR_PTRACE)
11250Sstevel@tonic-gate goto bad;
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate * Process is traced via /proc.
11280Sstevel@tonic-gate * Arrange to invalidate the /proc vnode.
11290Sstevel@tonic-gate */
11300Sstevel@tonic-gate args->traceinval = 1;
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate return (0);
11330Sstevel@tonic-gate bad:
11340Sstevel@tonic-gate if (error == 0)
11350Sstevel@tonic-gate error = ENOEXEC;
11360Sstevel@tonic-gate return (error);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate /*
11400Sstevel@tonic-gate * Map a section of an executable file into the user's
11410Sstevel@tonic-gate * address space.
11420Sstevel@tonic-gate */
11430Sstevel@tonic-gate int
execmap(struct vnode * vp,caddr_t addr,size_t len,size_t zfodlen,off_t offset,int prot,int page,uint_t szc)11440Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
11450Sstevel@tonic-gate off_t offset, int prot, int page, uint_t szc)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate int error = 0;
11480Sstevel@tonic-gate off_t oldoffset;
11490Sstevel@tonic-gate caddr_t zfodbase, oldaddr;
11500Sstevel@tonic-gate size_t end, oldlen;
11510Sstevel@tonic-gate size_t zfoddiff;
11520Sstevel@tonic-gate label_t ljb;
11530Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate oldaddr = addr;
11560Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
11570Sstevel@tonic-gate if (len) {
11580Sstevel@tonic-gate oldlen = len;
11590Sstevel@tonic-gate len += ((size_t)oldaddr - (size_t)addr);
11600Sstevel@tonic-gate oldoffset = offset;
11610Sstevel@tonic-gate offset = (off_t)((uintptr_t)offset & PAGEMASK);
11620Sstevel@tonic-gate if (page) {
11630Sstevel@tonic-gate spgcnt_t prefltmem, availm, npages;
11640Sstevel@tonic-gate int preread;
11650Sstevel@tonic-gate uint_t mflag = MAP_PRIVATE | MAP_FIXED;
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
11680Sstevel@tonic-gate mflag |= MAP_TEXT;
11690Sstevel@tonic-gate } else {
11700Sstevel@tonic-gate mflag |= MAP_INITDATA;
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
11740Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
11750Sstevel@tonic-gate error = ENOMEM;
11760Sstevel@tonic-gate goto bad;
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate if (error = VOP_MAP(vp, (offset_t)offset,
11790Sstevel@tonic-gate p->p_as, &addr, len, prot, PROT_ALL,
11805331Samw mflag, CRED(), NULL))
11810Sstevel@tonic-gate goto bad;
11820Sstevel@tonic-gate
11830Sstevel@tonic-gate /*
11840Sstevel@tonic-gate * If the segment can fit, then we prefault
11850Sstevel@tonic-gate * the entire segment in. This is based on the
11860Sstevel@tonic-gate * model that says the best working set of a
11870Sstevel@tonic-gate * small program is all of its pages.
11880Sstevel@tonic-gate */
11890Sstevel@tonic-gate npages = (spgcnt_t)btopr(len);
11900Sstevel@tonic-gate prefltmem = freemem - desfree;
11910Sstevel@tonic-gate preread =
11920Sstevel@tonic-gate (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate /*
11950Sstevel@tonic-gate * If we aren't prefaulting the segment,
11960Sstevel@tonic-gate * increment "deficit", if necessary to ensure
11970Sstevel@tonic-gate * that pages will become available when this
11980Sstevel@tonic-gate * process starts executing.
11990Sstevel@tonic-gate */
12000Sstevel@tonic-gate availm = freemem - lotsfree;
12010Sstevel@tonic-gate if (preread == 0 && npages > availm &&
12020Sstevel@tonic-gate deficit < lotsfree) {
12030Sstevel@tonic-gate deficit += MIN((pgcnt_t)(npages - availm),
12040Sstevel@tonic-gate lotsfree - deficit);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate if (preread) {
12080Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
12090Sstevel@tonic-gate "execmap preread:freemem %d size %lu",
12100Sstevel@tonic-gate freemem, len);
12110Sstevel@tonic-gate (void) as_fault(p->p_as->a_hat, p->p_as,
12120Sstevel@tonic-gate (caddr_t)addr, len, F_INVAL, S_READ);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate } else {
12150Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
12160Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
12170Sstevel@tonic-gate error = ENOMEM;
12180Sstevel@tonic-gate goto bad;
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate if (error = as_map(p->p_as, addr, len,
12220Sstevel@tonic-gate segvn_create, zfod_argsp))
12230Sstevel@tonic-gate goto bad;
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate * Read in the segment in one big chunk.
12260Sstevel@tonic-gate */
12270Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
12280Sstevel@tonic-gate oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
12290Sstevel@tonic-gate (rlim64_t)0, CRED(), (ssize_t *)0))
12300Sstevel@tonic-gate goto bad;
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate * Now set protections.
12330Sstevel@tonic-gate */
12340Sstevel@tonic-gate if (prot != PROT_ZFOD) {
12350Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)addr,
12360Sstevel@tonic-gate len, prot);
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate if (zfodlen) {
12422712Snn35248 struct as *as = curproc->p_as;
12432712Snn35248 struct seg *seg;
12442712Snn35248 uint_t zprot = 0;
12452712Snn35248
12460Sstevel@tonic-gate end = (size_t)addr + len;
12470Sstevel@tonic-gate zfodbase = (caddr_t)roundup(end, PAGESIZE);
12480Sstevel@tonic-gate zfoddiff = (uintptr_t)zfodbase - end;
12490Sstevel@tonic-gate if (zfoddiff) {
12502712Snn35248 /*
12512712Snn35248 * Before we go to zero the remaining space on the last
12522712Snn35248 * page, make sure we have write permission.
12532712Snn35248 */
12542712Snn35248
12552712Snn35248 AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
12562712Snn35248 seg = as_segat(curproc->p_as, (caddr_t)end);
12572712Snn35248 if (seg != NULL)
12582712Snn35248 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1,
12592712Snn35248 &zprot);
12602712Snn35248 AS_LOCK_EXIT(as, &as->a_lock);
12612712Snn35248
12622712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) {
12632712Snn35248 (void) as_setprot(as, (caddr_t)end,
12642712Snn35248 zfoddiff - 1, zprot | PROT_WRITE);
12652712Snn35248 }
12662712Snn35248
12670Sstevel@tonic-gate if (on_fault(&ljb)) {
12680Sstevel@tonic-gate no_fault();
12692712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0)
12702712Snn35248 (void) as_setprot(as, (caddr_t)end,
12715753Sgww zfoddiff - 1, zprot);
12720Sstevel@tonic-gate error = EFAULT;
12730Sstevel@tonic-gate goto bad;
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate uzero((void *)end, zfoddiff);
12760Sstevel@tonic-gate no_fault();
12772712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0)
12782712Snn35248 (void) as_setprot(as, (caddr_t)end,
12792712Snn35248 zfoddiff - 1, zprot);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate if (zfodlen > zfoddiff) {
12820Sstevel@tonic-gate struct segvn_crargs crargs =
12830Sstevel@tonic-gate SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate zfodlen -= zfoddiff;
12860Sstevel@tonic-gate if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
12870Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
12880Sstevel@tonic-gate error = ENOMEM;
12890Sstevel@tonic-gate goto bad;
12900Sstevel@tonic-gate }
12912991Ssusans if (szc > 0) {
12922991Ssusans /*
12932991Ssusans * ASSERT alignment because the mapelfexec()
12942991Ssusans * caller for the szc > 0 case extended zfod
12952991Ssusans * so it's end is pgsz aligned.
12962991Ssusans */
12972991Ssusans size_t pgsz = page_get_pagesize(szc);
12982991Ssusans ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz));
12992991Ssusans
13002991Ssusans if (IS_P2ALIGNED(zfodbase, pgsz)) {
13012991Ssusans crargs.szc = szc;
13022991Ssusans } else {
13032991Ssusans crargs.szc = AS_MAP_HEAP;
13042991Ssusans }
13052991Ssusans } else {
13062991Ssusans crargs.szc = AS_MAP_NO_LPOOB;
13072991Ssusans }
13080Sstevel@tonic-gate if (error = as_map(p->p_as, (caddr_t)zfodbase,
13090Sstevel@tonic-gate zfodlen, segvn_create, &crargs))
13100Sstevel@tonic-gate goto bad;
13110Sstevel@tonic-gate if (prot != PROT_ZFOD) {
13120Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)zfodbase,
13130Sstevel@tonic-gate zfodlen, prot);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate return (0);
13180Sstevel@tonic-gate bad:
13190Sstevel@tonic-gate return (error);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate void
setexecenv(struct execenv * ep)13230Sstevel@tonic-gate setexecenv(struct execenv *ep)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
13260Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
13270Sstevel@tonic-gate struct vnode *vp;
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate p->p_bssbase = ep->ex_bssbase;
13300Sstevel@tonic-gate p->p_brkbase = ep->ex_brkbase;
13310Sstevel@tonic-gate p->p_brksize = ep->ex_brksize;
13320Sstevel@tonic-gate if (p->p_exec)
13330Sstevel@tonic-gate VN_RELE(p->p_exec); /* out with the old */
13340Sstevel@tonic-gate vp = p->p_exec = ep->ex_vp;
13350Sstevel@tonic-gate if (vp != NULL)
13360Sstevel@tonic-gate VN_HOLD(vp); /* in with the new */
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
13390Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
13400Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate int
execopen(struct vnode ** vpp,int * fdp)13440Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate struct vnode *vp = *vpp;
13470Sstevel@tonic-gate file_t *fp;
13480Sstevel@tonic-gate int error = 0;
13490Sstevel@tonic-gate int filemode = FREAD;
13500Sstevel@tonic-gate
13510Sstevel@tonic-gate VN_HOLD(vp); /* open reference */
13520Sstevel@tonic-gate if (error = falloc(NULL, filemode, &fp, fdp)) {
13530Sstevel@tonic-gate VN_RELE(vp);
13540Sstevel@tonic-gate *fdp = -1; /* just in case falloc changed value */
13550Sstevel@tonic-gate return (error);
13560Sstevel@tonic-gate }
13575331Samw if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) {
13580Sstevel@tonic-gate VN_RELE(vp);
13590Sstevel@tonic-gate setf(*fdp, NULL);
13600Sstevel@tonic-gate unfalloc(fp);
13610Sstevel@tonic-gate *fdp = -1;
13620Sstevel@tonic-gate return (error);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate *vpp = vp; /* vnode should not have changed */
13650Sstevel@tonic-gate fp->f_vnode = vp;
13660Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
13670Sstevel@tonic-gate setf(*fdp, fp);
13680Sstevel@tonic-gate return (0);
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate int
execclose(int fd)13720Sstevel@tonic-gate execclose(int fd)
13730Sstevel@tonic-gate {
13740Sstevel@tonic-gate return (closeandsetf(fd, NULL));
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate * noexec stub function.
13800Sstevel@tonic-gate */
13810Sstevel@tonic-gate /*ARGSUSED*/
13820Sstevel@tonic-gate int
noexec(struct vnode * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,int setid,caddr_t exec_file,struct cred * cred)13830Sstevel@tonic-gate noexec(
13840Sstevel@tonic-gate struct vnode *vp,
13850Sstevel@tonic-gate struct execa *uap,
13860Sstevel@tonic-gate struct uarg *args,
13870Sstevel@tonic-gate struct intpdata *idatap,
13880Sstevel@tonic-gate int level,
13890Sstevel@tonic-gate long *execsz,
13900Sstevel@tonic-gate int setid,
13910Sstevel@tonic-gate caddr_t exec_file,
13920Sstevel@tonic-gate struct cred *cred)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
13950Sstevel@tonic-gate return (ENOEXEC);
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate * Support routines for building a user stack.
14000Sstevel@tonic-gate *
14010Sstevel@tonic-gate * execve(path, argv, envp) must construct a new stack with the specified
14020Sstevel@tonic-gate * arguments and environment variables (see exec_args() for a description
14030Sstevel@tonic-gate * of the user stack layout). To do this, we copy the arguments and
14040Sstevel@tonic-gate * environment variables from the old user address space into the kernel,
14050Sstevel@tonic-gate * free the old as, create the new as, and copy our buffered information
14060Sstevel@tonic-gate * to the new stack. Our kernel buffer has the following structure:
14070Sstevel@tonic-gate *
14080Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
14090Sstevel@tonic-gate * | string offsets |
14100Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
14110Sstevel@tonic-gate * | |
14120Sstevel@tonic-gate * | STK_AVAIL() space |
14130Sstevel@tonic-gate * | |
14140Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
14150Sstevel@tonic-gate * | strings |
14160Sstevel@tonic-gate * +-----------------------+ <--- stk_base
14170Sstevel@tonic-gate *
14180Sstevel@tonic-gate * When we add a string, we store the string's contents (including the null
14190Sstevel@tonic-gate * terminator) at stk_strp, and we store the offset of the string relative to
14200Sstevel@tonic-gate * stk_base at --stk_offp. At strings are added, stk_strp increases and
14210Sstevel@tonic-gate * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just
14220Sstevel@tonic-gate * the difference between these pointers. If we run out of space, we return
14230Sstevel@tonic-gate * an error and exec_args() starts all over again with a buffer twice as large.
14240Sstevel@tonic-gate * When we're all done, the kernel buffer looks like this:
14250Sstevel@tonic-gate *
14260Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
14270Sstevel@tonic-gate * | argv[0] offset |
14280Sstevel@tonic-gate * +-----------------------+
14290Sstevel@tonic-gate * | ... |
14300Sstevel@tonic-gate * +-----------------------+
14310Sstevel@tonic-gate * | argv[argc-1] offset |
14320Sstevel@tonic-gate * +-----------------------+
14330Sstevel@tonic-gate * | envp[0] offset |
14340Sstevel@tonic-gate * +-----------------------+
14350Sstevel@tonic-gate * | ... |
14360Sstevel@tonic-gate * +-----------------------+
14370Sstevel@tonic-gate * | envp[envc-1] offset |
14380Sstevel@tonic-gate * +-----------------------+
14390Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
14400Sstevel@tonic-gate * +-----------------------+
14410Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
14420Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
14430Sstevel@tonic-gate * | |
14440Sstevel@tonic-gate * | STK_AVAIL() space |
14450Sstevel@tonic-gate * | |
14460Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
14470Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
14480Sstevel@tonic-gate * +-----------------------+
14490Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
14500Sstevel@tonic-gate * +-----------------------+
14510Sstevel@tonic-gate * | envp[envc-1] string |
14520Sstevel@tonic-gate * +-----------------------+
14530Sstevel@tonic-gate * | ... |
14540Sstevel@tonic-gate * +-----------------------+
14550Sstevel@tonic-gate * | envp[0] string |
14560Sstevel@tonic-gate * +-----------------------+
14570Sstevel@tonic-gate * | argv[argc-1] string |
14580Sstevel@tonic-gate * +-----------------------+
14590Sstevel@tonic-gate * | ... |
14600Sstevel@tonic-gate * +-----------------------+
14610Sstevel@tonic-gate * | argv[0] string |
14620Sstevel@tonic-gate * +-----------------------+ <--- stk_base
14630Sstevel@tonic-gate */
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp)
14660Sstevel@tonic-gate
14670Sstevel@tonic-gate /*
14680Sstevel@tonic-gate * Add a string to the stack.
14690Sstevel@tonic-gate */
14700Sstevel@tonic-gate static int
stk_add(uarg_t * args,const char * sp,enum uio_seg segflg)14710Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate int error;
14740Sstevel@tonic-gate size_t len;
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate if (STK_AVAIL(args) < sizeof (int))
14770Sstevel@tonic-gate return (E2BIG);
14780Sstevel@tonic-gate *--args->stk_offp = args->stk_strp - args->stk_base;
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate if (segflg == UIO_USERSPACE) {
14810Sstevel@tonic-gate error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
14820Sstevel@tonic-gate if (error != 0)
14830Sstevel@tonic-gate return (error);
14840Sstevel@tonic-gate } else {
14850Sstevel@tonic-gate len = strlen(sp) + 1;
14860Sstevel@tonic-gate if (len > STK_AVAIL(args))
14870Sstevel@tonic-gate return (E2BIG);
14880Sstevel@tonic-gate bcopy(sp, args->stk_strp, len);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate args->stk_strp += len;
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate return (0);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate static int
stk_getptr(uarg_t * args,char * src,char ** dst)14970Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate int error;
15000Sstevel@tonic-gate
15010Sstevel@tonic-gate if (args->from_model == DATAMODEL_NATIVE) {
15020Sstevel@tonic-gate ulong_t ptr;
15030Sstevel@tonic-gate error = fulword(src, &ptr);
15040Sstevel@tonic-gate *dst = (caddr_t)ptr;
15050Sstevel@tonic-gate } else {
15060Sstevel@tonic-gate uint32_t ptr;
15070Sstevel@tonic-gate error = fuword32(src, &ptr);
15080Sstevel@tonic-gate *dst = (caddr_t)(uintptr_t)ptr;
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate return (error);
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate static int
stk_putptr(uarg_t * args,char * addr,char * value)15140Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
15150Sstevel@tonic-gate {
15160Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE)
15170Sstevel@tonic-gate return (sulword(addr, (ulong_t)value));
15180Sstevel@tonic-gate else
15190Sstevel@tonic-gate return (suword32(addr, (uint32_t)(uintptr_t)value));
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate
15220Sstevel@tonic-gate static int
stk_copyin(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)15230Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate char *sp;
15260Sstevel@tonic-gate int argc, error;
15270Sstevel@tonic-gate int argv_empty = 0;
15280Sstevel@tonic-gate size_t ptrsize = args->from_ptrsize;
15290Sstevel@tonic-gate size_t size, pad;
15300Sstevel@tonic-gate char *argv = (char *)uap->argp;
15310Sstevel@tonic-gate char *envp = (char *)uap->envp;
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate /*
15340Sstevel@tonic-gate * Copy interpreter's name and argument to argv[0] and argv[1].
15350Sstevel@tonic-gate */
15360Sstevel@tonic-gate if (intp != NULL && intp->intp_name != NULL) {
15370Sstevel@tonic-gate if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
15380Sstevel@tonic-gate return (error);
15390Sstevel@tonic-gate if (intp->intp_arg != NULL &&
15400Sstevel@tonic-gate (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
15410Sstevel@tonic-gate return (error);
15420Sstevel@tonic-gate if (args->fname != NULL)
15430Sstevel@tonic-gate error = stk_add(args, args->fname, UIO_SYSSPACE);
15440Sstevel@tonic-gate else
15450Sstevel@tonic-gate error = stk_add(args, uap->fname, UIO_USERSPACE);
15460Sstevel@tonic-gate if (error)
15470Sstevel@tonic-gate return (error);
15480Sstevel@tonic-gate
15490Sstevel@tonic-gate /*
15500Sstevel@tonic-gate * Check for an empty argv[].
15510Sstevel@tonic-gate */
15520Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
15530Sstevel@tonic-gate return (EFAULT);
15540Sstevel@tonic-gate if (sp == NULL)
15550Sstevel@tonic-gate argv_empty = 1;
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate argv += ptrsize; /* ignore original argv[0] */
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate
15600Sstevel@tonic-gate if (argv_empty == 0) {
15610Sstevel@tonic-gate /*
15620Sstevel@tonic-gate * Add argv[] strings to the stack.
15630Sstevel@tonic-gate */
15640Sstevel@tonic-gate for (;;) {
15650Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
15660Sstevel@tonic-gate return (EFAULT);
15670Sstevel@tonic-gate if (sp == NULL)
15680Sstevel@tonic-gate break;
15690Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
15700Sstevel@tonic-gate return (error);
15710Sstevel@tonic-gate argv += ptrsize;
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
15750Sstevel@tonic-gate args->arglen = args->stk_strp - args->stk_base;
15760Sstevel@tonic-gate
15770Sstevel@tonic-gate /*
15780Sstevel@tonic-gate * Add environ[] strings to the stack.
15790Sstevel@tonic-gate */
15800Sstevel@tonic-gate if (envp != NULL) {
15810Sstevel@tonic-gate for (;;) {
158212273SCasper.Dik@Sun.COM char *tmp = args->stk_strp;
15830Sstevel@tonic-gate if (stk_getptr(args, envp, &sp))
15840Sstevel@tonic-gate return (EFAULT);
15850Sstevel@tonic-gate if (sp == NULL)
15860Sstevel@tonic-gate break;
15870Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
15880Sstevel@tonic-gate return (error);
158912273SCasper.Dik@Sun.COM if (args->scrubenv && strncmp(tmp, "LD_", 3) == 0) {
159012273SCasper.Dik@Sun.COM /* Undo the copied string */
159112273SCasper.Dik@Sun.COM args->stk_strp = tmp;
159212273SCasper.Dik@Sun.COM *(args->stk_offp++) = NULL;
159312273SCasper.Dik@Sun.COM }
15940Sstevel@tonic-gate envp += ptrsize;
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
15980Sstevel@tonic-gate args->ne = args->na - argc;
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate /*
16012712Snn35248 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and
16022712Snn35248 * AT_SUN_EMULATOR strings to the stack.
16030Sstevel@tonic-gate */
16040Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
16050Sstevel@tonic-gate if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
16060Sstevel@tonic-gate return (error);
16070Sstevel@tonic-gate if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
16080Sstevel@tonic-gate return (error);
16092712Snn35248 if (args->brandname != NULL &&
16106247Sraf (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0)
16112712Snn35248 return (error);
16122712Snn35248 if (args->emulator != NULL &&
16136247Sraf (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0)
16142712Snn35248 return (error);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate * Compute the size of the stack. This includes all the pointers,
16190Sstevel@tonic-gate * the space reserved for the aux vector, and all the strings.
16200Sstevel@tonic-gate * The total number of pointers is args->na (which is argc + envc)
16210Sstevel@tonic-gate * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
16220Sstevel@tonic-gate * after the last argument (i.e. argv[argc]); (3) the NULL after the
16230Sstevel@tonic-gate * last environment variable (i.e. envp[envc]); and (4) the NULL after
16240Sstevel@tonic-gate * all the strings, at the very top of the stack.
16250Sstevel@tonic-gate */
16260Sstevel@tonic-gate size = (args->na + 4) * args->to_ptrsize + args->auxsize +
16270Sstevel@tonic-gate (args->stk_strp - args->stk_base);
16280Sstevel@tonic-gate
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate * Pad the string section with zeroes to align the stack size.
16310Sstevel@tonic-gate */
16320Sstevel@tonic-gate pad = P2NPHASE(size, args->stk_align);
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate if (STK_AVAIL(args) < pad)
16350Sstevel@tonic-gate return (E2BIG);
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate args->usrstack_size = size + pad;
16380Sstevel@tonic-gate
16390Sstevel@tonic-gate while (pad-- != 0)
16400Sstevel@tonic-gate *args->stk_strp++ = 0;
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate args->nc = args->stk_strp - args->stk_base;
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate return (0);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate static int
stk_copyout(uarg_t * args,char * usrstack,void ** auxvpp,user_t * up)16480Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
16490Sstevel@tonic-gate {
16500Sstevel@tonic-gate size_t ptrsize = args->to_ptrsize;
16510Sstevel@tonic-gate ssize_t pslen;
16520Sstevel@tonic-gate char *kstrp = args->stk_base;
16530Sstevel@tonic-gate char *ustrp = usrstack - args->nc - ptrsize;
16540Sstevel@tonic-gate char *usp = usrstack - args->usrstack_size;
16550Sstevel@tonic-gate int *offp = (int *)(args->stk_base + args->stk_size);
16560Sstevel@tonic-gate int envc = args->ne;
16570Sstevel@tonic-gate int argc = args->na - envc;
16580Sstevel@tonic-gate int i;
16590Sstevel@tonic-gate
16600Sstevel@tonic-gate /*
16610Sstevel@tonic-gate * Record argc for /proc.
16620Sstevel@tonic-gate */
16630Sstevel@tonic-gate up->u_argc = argc;
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate * Put argc on the stack. Note that even though it's an int,
16670Sstevel@tonic-gate * it always consumes ptrsize bytes (for alignment).
16680Sstevel@tonic-gate */
16690Sstevel@tonic-gate if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
16700Sstevel@tonic-gate return (-1);
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate * Add argc space (ptrsize) to usp and record argv for /proc.
16740Sstevel@tonic-gate */
16750Sstevel@tonic-gate up->u_argv = (uintptr_t)(usp += ptrsize);
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate /*
16780Sstevel@tonic-gate * Put the argv[] pointers on the stack.
16790Sstevel@tonic-gate */
16800Sstevel@tonic-gate for (i = 0; i < argc; i++, usp += ptrsize)
16810Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
16820Sstevel@tonic-gate return (-1);
16830Sstevel@tonic-gate
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate * Copy arguments to u_psargs.
16860Sstevel@tonic-gate */
16870Sstevel@tonic-gate pslen = MIN(args->arglen, PSARGSZ) - 1;
16880Sstevel@tonic-gate for (i = 0; i < pslen; i++)
16890Sstevel@tonic-gate up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
16900Sstevel@tonic-gate while (i < PSARGSZ)
16910Sstevel@tonic-gate up->u_psargs[i++] = '\0';
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate * Add space for argv[]'s NULL terminator (ptrsize) to usp and
16950Sstevel@tonic-gate * record envp for /proc.
16960Sstevel@tonic-gate */
16970Sstevel@tonic-gate up->u_envp = (uintptr_t)(usp += ptrsize);
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate * Put the envp[] pointers on the stack.
17010Sstevel@tonic-gate */
17020Sstevel@tonic-gate for (i = 0; i < envc; i++, usp += ptrsize)
17030Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
17040Sstevel@tonic-gate return (-1);
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate * Add space for envp[]'s NULL terminator (ptrsize) to usp and
17080Sstevel@tonic-gate * remember where the stack ends, which is also where auxv begins.
17090Sstevel@tonic-gate */
17100Sstevel@tonic-gate args->stackend = usp += ptrsize;
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate /*
17130Sstevel@tonic-gate * Put all the argv[], envp[], and auxv strings on the stack.
17140Sstevel@tonic-gate */
17150Sstevel@tonic-gate if (copyout(args->stk_base, ustrp, args->nc))
17160Sstevel@tonic-gate return (-1);
17170Sstevel@tonic-gate
17180Sstevel@tonic-gate /*
17190Sstevel@tonic-gate * Fill in the aux vector now that we know the user stack addresses
17202712Snn35248 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and
17212712Snn35248 * AT_SUN_EMULATOR strings.
17220Sstevel@tonic-gate */
17230Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
17240Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
17250Sstevel@tonic-gate auxv_t **a = (auxv_t **)auxvpp;
17260Sstevel@tonic-gate ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
17270Sstevel@tonic-gate ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
17282712Snn35248 if (args->brandname != NULL)
17292712Snn35248 ADDAUX(*a,
17302712Snn35248 AT_SUN_BRANDNAME, (long)&ustrp[*--offp])
17312712Snn35248 if (args->emulator != NULL)
17322712Snn35248 ADDAUX(*a,
17332712Snn35248 AT_SUN_EMULATOR, (long)&ustrp[*--offp])
17340Sstevel@tonic-gate } else {
17350Sstevel@tonic-gate auxv32_t **a = (auxv32_t **)auxvpp;
17360Sstevel@tonic-gate ADDAUX(*a,
17370Sstevel@tonic-gate AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
17380Sstevel@tonic-gate ADDAUX(*a,
17392712Snn35248 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp])
17402712Snn35248 if (args->brandname != NULL)
17412712Snn35248 ADDAUX(*a, AT_SUN_BRANDNAME,
17422712Snn35248 (int)(uintptr_t)&ustrp[*--offp])
17432712Snn35248 if (args->emulator != NULL)
17442712Snn35248 ADDAUX(*a, AT_SUN_EMULATOR,
17452712Snn35248 (int)(uintptr_t)&ustrp[*--offp])
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate }
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate return (0);
17500Sstevel@tonic-gate }
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate * Initialize a new user stack with the specified arguments and environment.
17540Sstevel@tonic-gate * The initial user stack layout is as follows:
17550Sstevel@tonic-gate *
17560Sstevel@tonic-gate * User Stack
17570Sstevel@tonic-gate * +---------------+ <--- curproc->p_usrstack
17583177Sdp78419 * | |
17593177Sdp78419 * | slew |
17603177Sdp78419 * | |
17613177Sdp78419 * +---------------+
17620Sstevel@tonic-gate * | NULL |
17630Sstevel@tonic-gate * +---------------+
17640Sstevel@tonic-gate * | |
17650Sstevel@tonic-gate * | auxv strings |
17660Sstevel@tonic-gate * | |
17670Sstevel@tonic-gate * +---------------+
17680Sstevel@tonic-gate * | |
17690Sstevel@tonic-gate * | envp strings |
17700Sstevel@tonic-gate * | |
17710Sstevel@tonic-gate * +---------------+
17720Sstevel@tonic-gate * | |
17730Sstevel@tonic-gate * | argv strings |
17740Sstevel@tonic-gate * | |
17750Sstevel@tonic-gate * +---------------+ <--- ustrp
17760Sstevel@tonic-gate * | |
17770Sstevel@tonic-gate * | aux vector |
17780Sstevel@tonic-gate * | |
17790Sstevel@tonic-gate * +---------------+ <--- auxv
17800Sstevel@tonic-gate * | NULL |
17810Sstevel@tonic-gate * +---------------+
17820Sstevel@tonic-gate * | envp[envc-1] |
17830Sstevel@tonic-gate * +---------------+
17840Sstevel@tonic-gate * | ... |
17850Sstevel@tonic-gate * +---------------+
17860Sstevel@tonic-gate * | envp[0] |
17870Sstevel@tonic-gate * +---------------+ <--- envp[]
17880Sstevel@tonic-gate * | NULL |
17890Sstevel@tonic-gate * +---------------+
17900Sstevel@tonic-gate * | argv[argc-1] |
17910Sstevel@tonic-gate * +---------------+
17920Sstevel@tonic-gate * | ... |
17930Sstevel@tonic-gate * +---------------+
17940Sstevel@tonic-gate * | argv[0] |
17950Sstevel@tonic-gate * +---------------+ <--- argv[]
17960Sstevel@tonic-gate * | argc |
17970Sstevel@tonic-gate * +---------------+ <--- stack base
17980Sstevel@tonic-gate */
17990Sstevel@tonic-gate int
exec_args(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)18000Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
18010Sstevel@tonic-gate {
18020Sstevel@tonic-gate size_t size;
18030Sstevel@tonic-gate int error;
18040Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
18050Sstevel@tonic-gate user_t *up = PTOU(p);
18060Sstevel@tonic-gate char *usrstack;
18070Sstevel@tonic-gate rctl_entity_p_t e;
18080Sstevel@tonic-gate struct as *as;
18092991Ssusans extern int use_stk_lpg;
18103177Sdp78419 size_t sp_slew;
18110Sstevel@tonic-gate
18120Sstevel@tonic-gate args->from_model = p->p_model;
18130Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
18140Sstevel@tonic-gate args->from_ptrsize = sizeof (long);
18150Sstevel@tonic-gate } else {
18160Sstevel@tonic-gate args->from_ptrsize = sizeof (int32_t);
18170Sstevel@tonic-gate }
18180Sstevel@tonic-gate
18190Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
18200Sstevel@tonic-gate args->to_ptrsize = sizeof (long);
18210Sstevel@tonic-gate args->ncargs = NCARGS;
18220Sstevel@tonic-gate args->stk_align = STACK_ALIGN;
18237838SRoger.Faulkner@Sun.COM if (args->addr32)
18247838SRoger.Faulkner@Sun.COM usrstack = (char *)USRSTACK64_32;
18257838SRoger.Faulkner@Sun.COM else
18267838SRoger.Faulkner@Sun.COM usrstack = (char *)USRSTACK;
18270Sstevel@tonic-gate } else {
18280Sstevel@tonic-gate args->to_ptrsize = sizeof (int32_t);
18290Sstevel@tonic-gate args->ncargs = NCARGS32;
18300Sstevel@tonic-gate args->stk_align = STACK_ALIGN32;
18310Sstevel@tonic-gate usrstack = (char *)USRSTACK32;
18320Sstevel@tonic-gate }
18330Sstevel@tonic-gate
18340Sstevel@tonic-gate ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate #if defined(__sparc)
18370Sstevel@tonic-gate /*
18380Sstevel@tonic-gate * Make sure user register windows are empty before
18390Sstevel@tonic-gate * attempting to make a new stack.
18400Sstevel@tonic-gate */
18410Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL);
18420Sstevel@tonic-gate #endif
18430Sstevel@tonic-gate
18440Sstevel@tonic-gate for (size = PAGESIZE; ; size *= 2) {
18450Sstevel@tonic-gate args->stk_size = size;
18460Sstevel@tonic-gate args->stk_base = kmem_alloc(size, KM_SLEEP);
18470Sstevel@tonic-gate args->stk_strp = args->stk_base;
18480Sstevel@tonic-gate args->stk_offp = (int *)(args->stk_base + size);
18490Sstevel@tonic-gate error = stk_copyin(uap, args, intp, auxvpp);
18500Sstevel@tonic-gate if (error == 0)
18510Sstevel@tonic-gate break;
18520Sstevel@tonic-gate kmem_free(args->stk_base, size);
18530Sstevel@tonic-gate if (error != E2BIG && error != ENAMETOOLONG)
18540Sstevel@tonic-gate return (error);
18550Sstevel@tonic-gate if (size >= args->ncargs)
18560Sstevel@tonic-gate return (E2BIG);
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate
18590Sstevel@tonic-gate size = args->usrstack_size;
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate ASSERT(error == 0);
18620Sstevel@tonic-gate ASSERT(P2PHASE(size, args->stk_align) == 0);
18630Sstevel@tonic-gate ASSERT((ssize_t)STK_AVAIL(args) >= 0);
18640Sstevel@tonic-gate
18650Sstevel@tonic-gate if (size > args->ncargs) {
18660Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
18670Sstevel@tonic-gate return (E2BIG);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate
18700Sstevel@tonic-gate /*
18710Sstevel@tonic-gate * Leave only the current lwp and force the other lwps to exit.
18720Sstevel@tonic-gate * If another lwp beat us to the punch by calling exit(), bail out.
18730Sstevel@tonic-gate */
18740Sstevel@tonic-gate if ((error = exitlwps(0)) != 0) {
18750Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
18760Sstevel@tonic-gate return (error);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate
18790Sstevel@tonic-gate /*
18800Sstevel@tonic-gate * Revoke any doors created by the process.
18810Sstevel@tonic-gate */
18820Sstevel@tonic-gate if (p->p_door_list)
18830Sstevel@tonic-gate door_exit();
18840Sstevel@tonic-gate
18850Sstevel@tonic-gate /*
18860Sstevel@tonic-gate * Release schedctl data structures.
18870Sstevel@tonic-gate */
18880Sstevel@tonic-gate if (p->p_pagep)
18890Sstevel@tonic-gate schedctl_proc_cleanup();
18900Sstevel@tonic-gate
18910Sstevel@tonic-gate /*
18920Sstevel@tonic-gate * Clean up any DTrace helpers for the process.
18930Sstevel@tonic-gate */
18940Sstevel@tonic-gate if (p->p_dtrace_helpers != NULL) {
18950Sstevel@tonic-gate ASSERT(dtrace_helpers_cleanup != NULL);
18960Sstevel@tonic-gate (*dtrace_helpers_cleanup)();
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate
18990Sstevel@tonic-gate mutex_enter(&p->p_lock);
19000Sstevel@tonic-gate /*
19010Sstevel@tonic-gate * Cleanup the DTrace provider associated with this process.
19020Sstevel@tonic-gate */
19030Sstevel@tonic-gate if (p->p_dtrace_probes) {
19040Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr != NULL);
19050Sstevel@tonic-gate dtrace_fasttrap_exec_ptr(p);
19060Sstevel@tonic-gate }
19070Sstevel@tonic-gate mutex_exit(&p->p_lock);
19080Sstevel@tonic-gate
19090Sstevel@tonic-gate /*
19100Sstevel@tonic-gate * discard the lwpchan cache.
19110Sstevel@tonic-gate */
19120Sstevel@tonic-gate if (p->p_lcp != NULL)
19130Sstevel@tonic-gate lwpchan_destroy_cache(1);
19140Sstevel@tonic-gate
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate * Delete the POSIX timers.
19170Sstevel@tonic-gate */
19180Sstevel@tonic-gate if (p->p_itimer != NULL)
19190Sstevel@tonic-gate timer_exit();
19200Sstevel@tonic-gate
19219870SRoger.Faulkner@Sun.COM /*
19229870SRoger.Faulkner@Sun.COM * Delete the ITIMER_REALPROF interval timer.
19239870SRoger.Faulkner@Sun.COM * The other ITIMER_* interval timers are specified
19249870SRoger.Faulkner@Sun.COM * to be inherited across exec().
19259870SRoger.Faulkner@Sun.COM */
19269870SRoger.Faulkner@Sun.COM delete_itimer_realprof();
19279870SRoger.Faulkner@Sun.COM
192811861SMarek.Pospisil@Sun.COM if (AU_AUDITING())
19290Sstevel@tonic-gate audit_exec(args->stk_base, args->stk_base + args->arglen,
193012273SCasper.Dik@Sun.COM args->na - args->ne, args->ne, args->pfcred);
19310Sstevel@tonic-gate
19320Sstevel@tonic-gate /*
19330Sstevel@tonic-gate * Ensure that we don't change resource associations while we
19340Sstevel@tonic-gate * change address spaces.
19350Sstevel@tonic-gate */
19360Sstevel@tonic-gate mutex_enter(&p->p_lock);
19370Sstevel@tonic-gate pool_barrier_enter();
19380Sstevel@tonic-gate mutex_exit(&p->p_lock);
19390Sstevel@tonic-gate
19400Sstevel@tonic-gate /*
19410Sstevel@tonic-gate * Destroy the old address space and create a new one.
19420Sstevel@tonic-gate * From here on, any errors are fatal to the exec()ing process.
19430Sstevel@tonic-gate * On error we return -1, which means the caller must SIGKILL
19440Sstevel@tonic-gate * the process.
19450Sstevel@tonic-gate */
19460Sstevel@tonic-gate relvm();
19470Sstevel@tonic-gate
19480Sstevel@tonic-gate mutex_enter(&p->p_lock);
19490Sstevel@tonic-gate pool_barrier_exit();
19500Sstevel@tonic-gate mutex_exit(&p->p_lock);
19510Sstevel@tonic-gate
19520Sstevel@tonic-gate up->u_execsw = args->execswp;
19530Sstevel@tonic-gate
19540Sstevel@tonic-gate p->p_brkbase = NULL;
19550Sstevel@tonic-gate p->p_brksize = 0;
19562991Ssusans p->p_brkpageszc = 0;
19570Sstevel@tonic-gate p->p_stksize = 0;
19582991Ssusans p->p_stkpageszc = 0;
19590Sstevel@tonic-gate p->p_model = args->to_model;
19600Sstevel@tonic-gate p->p_usrstack = usrstack;
19610Sstevel@tonic-gate p->p_stkprot = args->stk_prot;
19620Sstevel@tonic-gate p->p_datprot = args->dat_prot;
19630Sstevel@tonic-gate
19640Sstevel@tonic-gate /*
19650Sstevel@tonic-gate * Reset resource controls such that all controls are again active as
19660Sstevel@tonic-gate * well as appropriate to the potentially new address model for the
19670Sstevel@tonic-gate * process.
19680Sstevel@tonic-gate */
19690Sstevel@tonic-gate e.rcep_p.proc = p;
19700Sstevel@tonic-gate e.rcep_t = RCENTITY_PROCESS;
19710Sstevel@tonic-gate rctl_set_reset(p->p_rctls, p, &e);
19720Sstevel@tonic-gate
19732991Ssusans /* Too early to call map_pgsz for the heap */
19742991Ssusans if (use_stk_lpg) {
19752991Ssusans p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0));
19762991Ssusans }
19770Sstevel@tonic-gate
19782991Ssusans mutex_enter(&p->p_lock);
19792991Ssusans p->p_flag |= SAUTOLPG; /* kernel controls page sizes */
19802991Ssusans mutex_exit(&p->p_lock);
19810Sstevel@tonic-gate
19823177Sdp78419 /*
19833177Sdp78419 * Some platforms may choose to randomize real stack start by adding a
19843177Sdp78419 * small slew (not more than a few hundred bytes) to the top of the
19853177Sdp78419 * stack. This helps avoid cache thrashing when identical processes
19863177Sdp78419 * simultaneously share caches that don't provide enough associativity
19873177Sdp78419 * (e.g. sun4v systems). In this case stack slewing makes the same hot
19883177Sdp78419 * stack variables in different processes to live in different cache
19893177Sdp78419 * sets increasing effective associativity.
19903177Sdp78419 */
19913177Sdp78419 sp_slew = exec_get_spslew();
19923177Sdp78419 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0);
19933177Sdp78419 exec_set_sp(size + sp_slew);
19940Sstevel@tonic-gate
19950Sstevel@tonic-gate as = as_alloc();
19960Sstevel@tonic-gate p->p_as = as;
19972768Ssl108498 as->a_proc = p;
19987838SRoger.Faulkner@Sun.COM if (p->p_model == DATAMODEL_ILP32 || args->addr32)
19990Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT32;
20000Sstevel@tonic-gate (void) hat_setup(as->a_hat, HAT_ALLOC);
20014528Spaulsan hat_join_srd(as->a_hat, args->ex_vp);
20020Sstevel@tonic-gate
20030Sstevel@tonic-gate /*
20040Sstevel@tonic-gate * Finally, write out the contents of the new stack.
20050Sstevel@tonic-gate */
20063177Sdp78419 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up);
20070Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
20080Sstevel@tonic-gate return (error);
20090Sstevel@tonic-gate }
2010