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 /* 2311537SCasper.Dik@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/param.h> 320Sstevel@tonic-gate #include <sys/sysmacros.h> 330Sstevel@tonic-gate #include <sys/systm.h> 340Sstevel@tonic-gate #include <sys/signal.h> 350Sstevel@tonic-gate #include <sys/cred_impl.h> 360Sstevel@tonic-gate #include <sys/policy.h> 370Sstevel@tonic-gate #include <sys/user.h> 380Sstevel@tonic-gate #include <sys/errno.h> 390Sstevel@tonic-gate #include <sys/file.h> 400Sstevel@tonic-gate #include <sys/vfs.h> 410Sstevel@tonic-gate #include <sys/vnode.h> 420Sstevel@tonic-gate #include <sys/mman.h> 430Sstevel@tonic-gate #include <sys/acct.h> 440Sstevel@tonic-gate #include <sys/cpuvar.h> 450Sstevel@tonic-gate #include <sys/proc.h> 460Sstevel@tonic-gate #include <sys/cmn_err.h> 470Sstevel@tonic-gate #include <sys/debug.h> 480Sstevel@tonic-gate #include <sys/pathname.h> 490Sstevel@tonic-gate #include <sys/vm.h> 504426Saguzovsk #include <sys/lgrp.h> 510Sstevel@tonic-gate #include <sys/vtrace.h> 520Sstevel@tonic-gate #include <sys/exec.h> 530Sstevel@tonic-gate #include <sys/exechdr.h> 540Sstevel@tonic-gate #include <sys/kmem.h> 550Sstevel@tonic-gate #include <sys/prsystm.h> 560Sstevel@tonic-gate #include <sys/modctl.h> 570Sstevel@tonic-gate #include <sys/vmparam.h> 586247Sraf #include <sys/door.h> 590Sstevel@tonic-gate #include <sys/schedctl.h> 600Sstevel@tonic-gate #include <sys/utrap.h> 610Sstevel@tonic-gate #include <sys/systeminfo.h> 620Sstevel@tonic-gate #include <sys/stack.h> 630Sstevel@tonic-gate #include <sys/rctl.h> 640Sstevel@tonic-gate #include <sys/dtrace.h> 650Sstevel@tonic-gate #include <sys/lwpchan_impl.h> 660Sstevel@tonic-gate #include <sys/pool.h> 670Sstevel@tonic-gate #include <sys/sdt.h> 682712Snn35248 #include <sys/brand.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 */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *); 850Sstevel@tonic-gate static int hold_execsw(struct execsw *); 860Sstevel@tonic-gate 870Sstevel@tonic-gate uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */ 880Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) 890Sstevel@tonic-gate uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */ 900Sstevel@tonic-gate #endif 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define PSUIDFLAGS (SNOCD|SUGID) 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * exec() - wrapper around exece providing NULL environment pointer 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate int 980Sstevel@tonic-gate exec(const char *fname, const char **argp) 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate return (exece(fname, argp, NULL)); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * exece() - system call wrapper around exec_common() 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate int 1070Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp) 1080Sstevel@tonic-gate { 1090Sstevel@tonic-gate int error; 1100Sstevel@tonic-gate 1112712Snn35248 error = exec_common(fname, argp, envp, EBA_NONE); 1120Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate int 1162712Snn35248 exec_common(const char *fname, const char **argp, const char **envp, 1172712Snn35248 int brand_action) 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL; 1200Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 1210Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1220Sstevel@tonic-gate struct user *up = PTOU(p); 1230Sstevel@tonic-gate long execsz; /* temporary count of exec size */ 1240Sstevel@tonic-gate int i; 1250Sstevel@tonic-gate int error; 1260Sstevel@tonic-gate char exec_file[MAXCOMLEN+1]; 1270Sstevel@tonic-gate struct pathname pn; 1280Sstevel@tonic-gate struct pathname resolvepn; 1290Sstevel@tonic-gate struct uarg args; 1300Sstevel@tonic-gate struct execa ua; 1310Sstevel@tonic-gate k_sigset_t savedmask; 1320Sstevel@tonic-gate lwpdir_t *lwpdir = NULL; 1339393SRoger.Faulkner@Sun.COM tidhash_t *tidhash; 1340Sstevel@tonic-gate lwpdir_t *old_lwpdir = NULL; 1350Sstevel@tonic-gate uint_t old_lwpdir_sz; 1369393SRoger.Faulkner@Sun.COM tidhash_t *old_tidhash; 1370Sstevel@tonic-gate uint_t old_tidhash_sz; 1389393SRoger.Faulkner@Sun.COM ret_tidhash_t *ret_tidhash; 1390Sstevel@tonic-gate lwpent_t *lep; 1406994Sedp boolean_t brandme = B_FALSE; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * exec() is not supported for the /proc agent lwp. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate if (curthread == p->p_agenttp) 1460Sstevel@tonic-gate return (ENOTSUP); 1470Sstevel@tonic-gate 1482712Snn35248 if (brand_action != EBA_NONE) { 1492712Snn35248 /* 1502712Snn35248 * Brand actions are not supported for processes that are not 1512712Snn35248 * running in a branded zone. 1522712Snn35248 */ 1532712Snn35248 if (!ZONE_IS_BRANDED(p->p_zone)) 1542712Snn35248 return (ENOTSUP); 1552712Snn35248 1562712Snn35248 if (brand_action == EBA_NATIVE) { 1572712Snn35248 /* Only branded processes can be unbranded */ 1582712Snn35248 if (!PROC_IS_BRANDED(p)) 1592712Snn35248 return (ENOTSUP); 1602712Snn35248 } else { 1612712Snn35248 /* Only unbranded processes can be branded */ 1622712Snn35248 if (PROC_IS_BRANDED(p)) 1632712Snn35248 return (ENOTSUP); 1646994Sedp brandme = B_TRUE; 1652712Snn35248 } 1662712Snn35248 } else { 1672712Snn35248 /* 1682712Snn35248 * If this is a native zone, or if the process is already 1692712Snn35248 * branded, then we don't need to do anything. If this is 1702712Snn35248 * a native process in a branded zone, we need to brand the 1712712Snn35248 * process as it exec()s the new binary. 1722712Snn35248 */ 1732712Snn35248 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p)) 1746994Sedp brandme = B_TRUE; 1752712Snn35248 } 1762712Snn35248 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * Inform /proc that an exec() has started. 1790Sstevel@tonic-gate * Hold signals that are ignored by default so that we will 1800Sstevel@tonic-gate * not be interrupted by a signal that will be ignored after 1810Sstevel@tonic-gate * successful completion of gexec(). 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate mutex_enter(&p->p_lock); 1840Sstevel@tonic-gate prexecstart(); 1850Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 1860Sstevel@tonic-gate savedmask = curthread->t_hold; 1870Sstevel@tonic-gate sigorset(&curthread->t_hold, &ignoredefault); 1880Sstevel@tonic-gate mutex_exit(&p->p_lock); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Look up path name and remember last component for later. 1920Sstevel@tonic-gate * To help coreadm expand its %d token, we attempt to save 1930Sstevel@tonic-gate * the directory containing the executable in p_execdir. The 1940Sstevel@tonic-gate * first call to lookuppn() may fail and return EINVAL because 1950Sstevel@tonic-gate * dirvpp is non-NULL. In that case, we make a second call to 1960Sstevel@tonic-gate * lookuppn() with dirvpp set to NULL; p_execdir will be NULL, 1970Sstevel@tonic-gate * but coreadm is allowed to expand %d to the empty string and 1980Sstevel@tonic-gate * there are other cases in which that failure may occur. 1990Sstevel@tonic-gate */ 2000Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 2010Sstevel@tonic-gate goto out; 2020Sstevel@tonic-gate pn_alloc(&resolvepn); 2030Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) { 2040Sstevel@tonic-gate pn_free(&resolvepn); 2050Sstevel@tonic-gate pn_free(&pn); 2060Sstevel@tonic-gate if (error != EINVAL) 2070Sstevel@tonic-gate goto out; 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate dir = NULL; 2100Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 2110Sstevel@tonic-gate goto out; 2120Sstevel@tonic-gate pn_alloc(&resolvepn); 2130Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP, 2140Sstevel@tonic-gate &vp)) != 0) { 2150Sstevel@tonic-gate pn_free(&resolvepn); 2160Sstevel@tonic-gate pn_free(&pn); 2170Sstevel@tonic-gate goto out; 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate if (vp == NULL) { 2210Sstevel@tonic-gate if (dir != NULL) 2220Sstevel@tonic-gate VN_RELE(dir); 2230Sstevel@tonic-gate error = ENOENT; 2240Sstevel@tonic-gate pn_free(&resolvepn); 2250Sstevel@tonic-gate pn_free(&pn); 2260Sstevel@tonic-gate goto out; 2270Sstevel@tonic-gate } 2281043Scasper 2296134Scasper if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) { 2306134Scasper if (dir != NULL) 2316134Scasper VN_RELE(dir); 2326134Scasper pn_free(&resolvepn); 2336134Scasper pn_free(&pn); 2346134Scasper VN_RELE(vp); 2356134Scasper goto out; 2366134Scasper } 2376134Scasper 2381043Scasper /* 2391043Scasper * We do not allow executing files in attribute directories. 2401043Scasper * We test this by determining whether the resolved path 2411043Scasper * contains a "/" when we're in an attribute directory; 2421043Scasper * only if the pathname does not contain a "/" the resolved path 2431043Scasper * points to a file in the current working (attribute) directory. 2441043Scasper */ 2451043Scasper if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 && 2461043Scasper strchr(resolvepn.pn_path, '/') == NULL) { 2471043Scasper if (dir != NULL) 2481043Scasper VN_RELE(dir); 2491043Scasper error = EACCES; 2501043Scasper pn_free(&resolvepn); 2511043Scasper pn_free(&pn); 2521043Scasper VN_RELE(vp); 2531043Scasper goto out; 2541043Scasper } 2551043Scasper 2560Sstevel@tonic-gate bzero(exec_file, MAXCOMLEN+1); 2570Sstevel@tonic-gate (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN); 2580Sstevel@tonic-gate bzero(&args, sizeof (args)); 2590Sstevel@tonic-gate args.pathname = resolvepn.pn_path; 2600Sstevel@tonic-gate /* don't free resolvepn until we are done with args */ 2610Sstevel@tonic-gate pn_free(&pn); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Specific exec handlers, or policies determined via 2650Sstevel@tonic-gate * /etc/system may override the historical default. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate args.stk_prot = PROT_ZFOD; 2680Sstevel@tonic-gate args.dat_prot = PROT_ZFOD; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate CPU_STATS_ADD_K(sys, sysexec, 1); 2710Sstevel@tonic-gate DTRACE_PROC1(exec, char *, args.pathname); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate ua.fname = fname; 2740Sstevel@tonic-gate ua.argp = argp; 2750Sstevel@tonic-gate ua.envp = envp; 2760Sstevel@tonic-gate 2772712Snn35248 /* If necessary, brand this process before we start the exec. */ 2786994Sedp if (brandme) 2792712Snn35248 brand_setbrand(p); 2802712Snn35248 2810Sstevel@tonic-gate if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz, 2822712Snn35248 exec_file, p->p_cred, brand_action)) != 0) { 2836994Sedp if (brandme) 2846994Sedp brand_clearbrand(p); 2850Sstevel@tonic-gate VN_RELE(vp); 2860Sstevel@tonic-gate if (dir != NULL) 2870Sstevel@tonic-gate VN_RELE(dir); 2880Sstevel@tonic-gate pn_free(&resolvepn); 2890Sstevel@tonic-gate goto fail; 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Free floating point registers (sun4u only) 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate ASSERT(lwp != NULL); 2960Sstevel@tonic-gate lwp_freeregs(lwp, 1); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2991217Srab * Free thread and process context ops. 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate if (curthread->t_ctx) 3020Sstevel@tonic-gate freectx(curthread, 1); 3031217Srab if (p->p_pctx) 3041217Srab freepctx(p, 1); 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate /* 3070Sstevel@tonic-gate * Remember file name for accounting; clear any cached DTrace predicate. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate up->u_acflag &= ~AFORK; 3100Sstevel@tonic-gate bcopy(exec_file, up->u_comm, MAXCOMLEN+1); 3110Sstevel@tonic-gate curthread->t_predcache = NULL; 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * Clear contract template state 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate lwp_ctmpl_clear(lwp); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Save the directory in which we found the executable for expanding 3200Sstevel@tonic-gate * the %d token used in core file patterns. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate mutex_enter(&p->p_lock); 3230Sstevel@tonic-gate tmpvp = p->p_execdir; 3240Sstevel@tonic-gate p->p_execdir = dir; 3250Sstevel@tonic-gate if (p->p_execdir != NULL) 3260Sstevel@tonic-gate VN_HOLD(p->p_execdir); 3270Sstevel@tonic-gate mutex_exit(&p->p_lock); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate if (tmpvp != NULL) 3300Sstevel@tonic-gate VN_RELE(tmpvp); 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate /* 3330Sstevel@tonic-gate * Reset stack state to the user stack, clear set of signals 3340Sstevel@tonic-gate * caught on the signal stack, and reset list of signals that 3350Sstevel@tonic-gate * restart system calls; the new program's environment should 3360Sstevel@tonic-gate * not be affected by detritus from the old program. Any 3370Sstevel@tonic-gate * pending held signals remain held, so don't clear t_hold. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate mutex_enter(&p->p_lock); 3400Sstevel@tonic-gate lwp->lwp_oldcontext = 0; 3410Sstevel@tonic-gate lwp->lwp_ustack = 0; 3420Sstevel@tonic-gate lwp->lwp_old_stk_ctl = 0; 3430Sstevel@tonic-gate sigemptyset(&up->u_signodefer); 3440Sstevel@tonic-gate sigemptyset(&up->u_sigonstack); 3450Sstevel@tonic-gate sigemptyset(&up->u_sigresethand); 3460Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0; 3470Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0; 3480Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * Make saved resource limit == current resource limit. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate for (i = 0; i < RLIM_NLIMITS; i++) { 3540Sstevel@tonic-gate /*CONSTCOND*/ 3550Sstevel@tonic-gate if (RLIM_SAVED(i)) { 3560Sstevel@tonic-gate (void) rctl_rlimit_get(rctlproc_legacy[i], p, 3570Sstevel@tonic-gate &up->u_saved_rlimit[i]); 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* 3620Sstevel@tonic-gate * If the action was to catch the signal, then the action 3630Sstevel@tonic-gate * must be reset to SIG_DFL. 3640Sstevel@tonic-gate */ 3650Sstevel@tonic-gate sigdefault(p); 3660Sstevel@tonic-gate p->p_flag &= ~(SNOWAIT|SJCTL); 3670Sstevel@tonic-gate p->p_flag |= (SEXECED|SMSACCT|SMSFORK); 3680Sstevel@tonic-gate up->u_signal[SIGCLD - 1] = SIG_DFL; 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Delete the dot4 sigqueues/signotifies. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate sigqfree(p); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate mutex_exit(&p->p_lock); 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate mutex_enter(&p->p_pflock); 3780Sstevel@tonic-gate p->p_prof.pr_base = NULL; 3790Sstevel@tonic-gate p->p_prof.pr_size = 0; 3800Sstevel@tonic-gate p->p_prof.pr_off = 0; 3810Sstevel@tonic-gate p->p_prof.pr_scale = 0; 3820Sstevel@tonic-gate p->p_prof.pr_samples = 0; 3830Sstevel@tonic-gate mutex_exit(&p->p_pflock); 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL); 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate #if defined(__sparc) 3880Sstevel@tonic-gate if (p->p_utraps != NULL) 3890Sstevel@tonic-gate utrap_free(p); 3900Sstevel@tonic-gate #endif /* __sparc */ 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Close all close-on-exec files. 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate close_exec(P_FINFO(p)); 3960Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up); 3972712Snn35248 3986994Sedp /* Unbrand ourself if necessary. */ 3996994Sedp if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE)) 4006994Sedp brand_clearbrand(p); 4012712Snn35248 4020Sstevel@tonic-gate setregs(&args); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* Mark this as an executable vnode */ 4050Sstevel@tonic-gate mutex_enter(&vp->v_lock); 4060Sstevel@tonic-gate vp->v_flag |= VVMEXEC; 4070Sstevel@tonic-gate mutex_exit(&vp->v_lock); 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate VN_RELE(vp); 4100Sstevel@tonic-gate if (dir != NULL) 4110Sstevel@tonic-gate VN_RELE(dir); 4120Sstevel@tonic-gate pn_free(&resolvepn); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* 4150Sstevel@tonic-gate * Allocate a new lwp directory and lwpid hash table if necessary. 4160Sstevel@tonic-gate */ 4170Sstevel@tonic-gate if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) { 4180Sstevel@tonic-gate lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP); 4190Sstevel@tonic-gate lwpdir->ld_next = lwpdir + 1; 4209393SRoger.Faulkner@Sun.COM tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP); 4210Sstevel@tonic-gate if (p->p_lwpdir != NULL) 4220Sstevel@tonic-gate lep = p->p_lwpdir[curthread->t_dslot].ld_entry; 4230Sstevel@tonic-gate else 4240Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4272712Snn35248 if (PROC_IS_BRANDED(p)) 4282712Snn35248 BROP(p)->b_exec(); 4292712Snn35248 4300Sstevel@tonic-gate mutex_enter(&p->p_lock); 4310Sstevel@tonic-gate prbarrier(p); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Reset lwp id to the default value of 1. 4350Sstevel@tonic-gate * This is a single-threaded process now 4360Sstevel@tonic-gate * and lwp #1 is lwp_wait()able by default. 4370Sstevel@tonic-gate * The t_unpark flag should not be inherited. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0); 4400Sstevel@tonic-gate curthread->t_tid = 1; 4414426Saguzovsk kpreempt_disable(); 4424426Saguzovsk ASSERT(curthread->t_lpl != NULL); 4434426Saguzovsk p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid; 4444426Saguzovsk kpreempt_enable(); 4454426Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) { 4464426Saguzovsk lgrp_update_trthr_migrations(1); 4474426Saguzovsk } 4480Sstevel@tonic-gate curthread->t_unpark = 0; 4490Sstevel@tonic-gate curthread->t_proc_flag |= TP_TWAIT; 4500Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */ 4510Sstevel@tonic-gate p->p_lwpdaemon = 0; /* but oh well ... */ 4520Sstevel@tonic-gate p->p_lwpid = 1; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /* 4550Sstevel@tonic-gate * Install the newly-allocated lwp directory and lwpid hash table 4560Sstevel@tonic-gate * and insert the current thread into the new hash table. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate if (lwpdir != NULL) { 4590Sstevel@tonic-gate old_lwpdir = p->p_lwpdir; 4600Sstevel@tonic-gate old_lwpdir_sz = p->p_lwpdir_sz; 4610Sstevel@tonic-gate old_tidhash = p->p_tidhash; 4620Sstevel@tonic-gate old_tidhash_sz = p->p_tidhash_sz; 4630Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = lwpdir; 4640Sstevel@tonic-gate p->p_lwpdir_sz = 2; 4650Sstevel@tonic-gate lep->le_thread = curthread; 4660Sstevel@tonic-gate lep->le_lwpid = curthread->t_tid; 4670Sstevel@tonic-gate lep->le_start = curthread->t_start; 4689393SRoger.Faulkner@Sun.COM lwp_hash_in(p, lep, tidhash, 2, 0); 4699393SRoger.Faulkner@Sun.COM p->p_tidhash = tidhash; 4709393SRoger.Faulkner@Sun.COM p->p_tidhash_sz = 2; 4710Sstevel@tonic-gate } 4729393SRoger.Faulkner@Sun.COM ret_tidhash = p->p_ret_tidhash; 4739393SRoger.Faulkner@Sun.COM p->p_ret_tidhash = NULL; 4742712Snn35248 4750Sstevel@tonic-gate /* 4760Sstevel@tonic-gate * Restore the saved signal mask and 4770Sstevel@tonic-gate * inform /proc that the exec() has finished. 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate curthread->t_hold = savedmask; 4800Sstevel@tonic-gate prexecend(); 4810Sstevel@tonic-gate mutex_exit(&p->p_lock); 4820Sstevel@tonic-gate if (old_lwpdir) { 4830Sstevel@tonic-gate kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t)); 4849393SRoger.Faulkner@Sun.COM kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t)); 4859393SRoger.Faulkner@Sun.COM } 4869393SRoger.Faulkner@Sun.COM while (ret_tidhash != NULL) { 4879393SRoger.Faulkner@Sun.COM ret_tidhash_t *next = ret_tidhash->rth_next; 4889393SRoger.Faulkner@Sun.COM kmem_free(ret_tidhash->rth_tidhash, 4899393SRoger.Faulkner@Sun.COM ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t)); 4909393SRoger.Faulkner@Sun.COM kmem_free(ret_tidhash, sizeof (*ret_tidhash)); 4919393SRoger.Faulkner@Sun.COM ret_tidhash = next; 4920Sstevel@tonic-gate } 4932712Snn35248 4940Sstevel@tonic-gate ASSERT(error == 0); 4950Sstevel@tonic-gate DTRACE_PROC(exec__success); 4960Sstevel@tonic-gate return (0); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate fail: 4990Sstevel@tonic-gate DTRACE_PROC1(exec__failure, int, error); 5000Sstevel@tonic-gate out: /* error return */ 5010Sstevel@tonic-gate mutex_enter(&p->p_lock); 5020Sstevel@tonic-gate curthread->t_hold = savedmask; 5030Sstevel@tonic-gate prexecend(); 5040Sstevel@tonic-gate mutex_exit(&p->p_lock); 5050Sstevel@tonic-gate ASSERT(error != 0); 5060Sstevel@tonic-gate return (error); 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * Perform generic exec duties and switchout to object-file specific 5120Sstevel@tonic-gate * handler. 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate int 5150Sstevel@tonic-gate gexec( 5160Sstevel@tonic-gate struct vnode **vpp, 5170Sstevel@tonic-gate struct execa *uap, 5180Sstevel@tonic-gate struct uarg *args, 5190Sstevel@tonic-gate struct intpdata *idatap, 5200Sstevel@tonic-gate int level, 5210Sstevel@tonic-gate long *execsz, 5220Sstevel@tonic-gate caddr_t exec_file, 5232712Snn35248 struct cred *cred, 5242712Snn35248 int brand_action) 5250Sstevel@tonic-gate { 526*11736SDonghai.Qiao@Sun.COM struct vnode *vp, *execvp = NULL; 5270Sstevel@tonic-gate proc_t *pp = ttoproc(curthread); 5280Sstevel@tonic-gate struct execsw *eswp; 5290Sstevel@tonic-gate int error = 0; 5300Sstevel@tonic-gate int suidflags = 0; 5310Sstevel@tonic-gate ssize_t resid; 5320Sstevel@tonic-gate uid_t uid, gid; 5330Sstevel@tonic-gate struct vattr vattr; 5340Sstevel@tonic-gate char magbuf[MAGIC_BYTES]; 5350Sstevel@tonic-gate int setid; 5360Sstevel@tonic-gate cred_t *oldcred, *newcred = NULL; 5370Sstevel@tonic-gate int privflags = 0; 5381335Scasper int setidfl; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* 5410Sstevel@tonic-gate * If the SNOCD or SUGID flag is set, turn it off and remember the 5420Sstevel@tonic-gate * previous setting so we can restore it if we encounter an error. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate if (level == 0 && (pp->p_flag & PSUIDFLAGS)) { 5450Sstevel@tonic-gate mutex_enter(&pp->p_lock); 5460Sstevel@tonic-gate suidflags = pp->p_flag & PSUIDFLAGS; 5470Sstevel@tonic-gate pp->p_flag &= ~PSUIDFLAGS; 5480Sstevel@tonic-gate mutex_exit(&pp->p_lock); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate if ((error = execpermissions(*vpp, &vattr, args)) != 0) 552*11736SDonghai.Qiao@Sun.COM goto bad_noclose; 5530Sstevel@tonic-gate 554*11736SDonghai.Qiao@Sun.COM /* need to open vnode for stateful file systems */ 5555331Samw if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0) 556*11736SDonghai.Qiao@Sun.COM goto bad_noclose; 5570Sstevel@tonic-gate vp = *vpp; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * Note: to support binary compatibility with SunOS a.out 5610Sstevel@tonic-gate * executables, we read in the first four bytes, as the 5620Sstevel@tonic-gate * magic number is in bytes 2-3. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf), 5650Sstevel@tonic-gate (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 5660Sstevel@tonic-gate goto bad; 5670Sstevel@tonic-gate if (resid != 0) 5680Sstevel@tonic-gate goto bad; 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate if ((eswp = findexec_by_hdr(magbuf)) == NULL) 5710Sstevel@tonic-gate goto bad; 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (level == 0 && 5740Sstevel@tonic-gate (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) { 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate newcred = cred = crdup(cred); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* If we can, drop the PA bit */ 5790Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0) 5800Sstevel@tonic-gate priv_adjust_PA(cred); 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (privflags & PRIV_SETID) { 5830Sstevel@tonic-gate cred->cr_uid = uid; 5840Sstevel@tonic-gate cred->cr_gid = gid; 5850Sstevel@tonic-gate cred->cr_suid = uid; 5860Sstevel@tonic-gate cred->cr_sgid = gid; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5891676Sjpk if (privflags & MAC_FLAGS) { 5901676Sjpk if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT)) 5911676Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE; 5921676Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT; 5931676Sjpk } 5941676Sjpk 5950Sstevel@tonic-gate /* 5960Sstevel@tonic-gate * Implement the privilege updates: 5970Sstevel@tonic-gate * 5980Sstevel@tonic-gate * Restrict with L: 5990Sstevel@tonic-gate * 6000Sstevel@tonic-gate * I' = I & L 6010Sstevel@tonic-gate * 6020Sstevel@tonic-gate * E' = P' = (I' + F) & A 6030Sstevel@tonic-gate * 6040Sstevel@tonic-gate * But if running under ptrace, we cap I with P. 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0) { 6070Sstevel@tonic-gate if ((privflags & PRIV_INCREASE) != 0 && 6080Sstevel@tonic-gate (pp->p_proc_flag & P_PR_PTRACE) != 0) 6090Sstevel@tonic-gate priv_intersect(&CR_OPPRIV(cred), 6105753Sgww &CR_IPRIV(cred)); 6110Sstevel@tonic-gate priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred)); 6120Sstevel@tonic-gate CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred); 6130Sstevel@tonic-gate priv_adjust_PA(cred); 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* SunOS 4.x buy-back */ 6180Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) && 6190Sstevel@tonic-gate (vattr.va_mode & (VSUID|VSGID))) { 6209068Sjason@ansipunx.net char path[MAXNAMELEN]; 6219068Sjason@ansipunx.net refstr_t *mntpt = NULL; 6229068Sjason@ansipunx.net int ret = -1; 6239068Sjason@ansipunx.net 6249068Sjason@ansipunx.net bzero(path, sizeof (path)); 6259068Sjason@ansipunx.net zone_hold(pp->p_zone); 6269068Sjason@ansipunx.net 6279068Sjason@ansipunx.net ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path, 6289068Sjason@ansipunx.net sizeof (path), cred); 6299068Sjason@ansipunx.net 6309068Sjason@ansipunx.net /* fallback to mountpoint if a path can't be found */ 6319068Sjason@ansipunx.net if ((ret != 0) || (ret == 0 && path[0] == '\0')) 6329068Sjason@ansipunx.net mntpt = vfs_getmntpoint(vp->v_vfsp); 6339068Sjason@ansipunx.net 6349068Sjason@ansipunx.net if (mntpt == NULL) 6359068Sjason@ansipunx.net zcmn_err(pp->p_zone->zone_id, CE_NOTE, 6369068Sjason@ansipunx.net "!uid %d: setuid execution not allowed, " 6379068Sjason@ansipunx.net "file=%s", cred->cr_uid, path); 6389068Sjason@ansipunx.net else 6399068Sjason@ansipunx.net zcmn_err(pp->p_zone->zone_id, CE_NOTE, 6409068Sjason@ansipunx.net "!uid %d: setuid execution not allowed, " 6419068Sjason@ansipunx.net "fs=%s, file=%s", cred->cr_uid, 6429068Sjason@ansipunx.net ZONE_PATH_TRANSLATE(refstr_value(mntpt), 6439068Sjason@ansipunx.net pp->p_zone), exec_file); 6449068Sjason@ansipunx.net 6459068Sjason@ansipunx.net if (!INGLOBALZONE(pp)) { 6469068Sjason@ansipunx.net /* zone_rootpath always has trailing / */ 6479068Sjason@ansipunx.net if (mntpt == NULL) 6489068Sjason@ansipunx.net cmn_err(CE_NOTE, "!zone: %s, uid: %d " 6499068Sjason@ansipunx.net "setuid execution not allowed, file=%s%s", 6509068Sjason@ansipunx.net pp->p_zone->zone_name, cred->cr_uid, 6519068Sjason@ansipunx.net pp->p_zone->zone_rootpath, path + 1); 6529068Sjason@ansipunx.net else 6539068Sjason@ansipunx.net cmn_err(CE_NOTE, "!zone: %s, uid: %d " 6549068Sjason@ansipunx.net "setuid execution not allowed, fs=%s, " 6559068Sjason@ansipunx.net "file=%s", pp->p_zone->zone_name, 6569068Sjason@ansipunx.net cred->cr_uid, refstr_value(mntpt), 6579068Sjason@ansipunx.net exec_file); 6589068Sjason@ansipunx.net } 6599068Sjason@ansipunx.net 6609068Sjason@ansipunx.net if (mntpt != NULL) 6619068Sjason@ansipunx.net refstr_rele(mntpt); 6629068Sjason@ansipunx.net 6639068Sjason@ansipunx.net zone_rele(pp->p_zone); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * execsetid() told us whether or not we had to change the 6680Sstevel@tonic-gate * credentials of the process. In privflags, it told us 6690Sstevel@tonic-gate * whether we gained any privileges or executed a set-uid executable. 6700Sstevel@tonic-gate */ 6710Sstevel@tonic-gate setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE)); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Use /etc/system variable to determine if the stack 6750Sstevel@tonic-gate * should be marked as executable by default. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate if (noexec_user_stack) 6780Sstevel@tonic-gate args->stk_prot &= ~PROT_EXEC; 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */ 6814528Spaulsan args->ex_vp = vp; 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * Traditionally, the setid flags told the sub processes whether 6850Sstevel@tonic-gate * the file just executed was set-uid or set-gid; this caused 6860Sstevel@tonic-gate * some confusion as the 'setid' flag did not match the SUGID 6870Sstevel@tonic-gate * process flag which is only set when the uids/gids do not match. 6880Sstevel@tonic-gate * A script set-gid/set-uid to the real uid/gid would start with 6890Sstevel@tonic-gate * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH. 6900Sstevel@tonic-gate * Now we flag those cases where the calling process cannot 6910Sstevel@tonic-gate * be trusted to influence the newly exec'ed process, either 6920Sstevel@tonic-gate * because it runs with more privileges or when the uids/gids 6930Sstevel@tonic-gate * do in fact not match. 6940Sstevel@tonic-gate * This also makes the runtime linker agree with the on exec 6950Sstevel@tonic-gate * values of SNOCD and SUGID. 6960Sstevel@tonic-gate */ 6971335Scasper setidfl = 0; 6981335Scasper if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid && 6991335Scasper !supgroupmember(cred->cr_gid, cred))) { 7001335Scasper setidfl |= EXECSETID_UGIDS; 7011335Scasper } 7021335Scasper if (setid & PRIV_SETUGID) 7031335Scasper setidfl |= EXECSETID_SETID; 7041335Scasper if (setid & PRIV_INCREASE) 7051335Scasper setidfl |= EXECSETID_PRIVS; 7061335Scasper 707*11736SDonghai.Qiao@Sun.COM execvp = pp->p_exec; 708*11736SDonghai.Qiao@Sun.COM if (execvp) 709*11736SDonghai.Qiao@Sun.COM VN_HOLD(execvp); 710*11736SDonghai.Qiao@Sun.COM 7110Sstevel@tonic-gate error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz, 7125753Sgww setidfl, exec_file, cred, brand_action); 7130Sstevel@tonic-gate rw_exit(eswp->exec_lock); 7140Sstevel@tonic-gate if (error != 0) { 7150Sstevel@tonic-gate if (newcred != NULL) 7160Sstevel@tonic-gate crfree(newcred); 717*11736SDonghai.Qiao@Sun.COM if (execvp) 718*11736SDonghai.Qiao@Sun.COM VN_RELE(execvp); 7190Sstevel@tonic-gate goto bad; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if (level == 0) { 723*11736SDonghai.Qiao@Sun.COM if (execvp != NULL) { 724*11736SDonghai.Qiao@Sun.COM /* 725*11736SDonghai.Qiao@Sun.COM * Close the previous executable only if we are 726*11736SDonghai.Qiao@Sun.COM * at level 0. 727*11736SDonghai.Qiao@Sun.COM */ 728*11736SDonghai.Qiao@Sun.COM (void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0, 729*11736SDonghai.Qiao@Sun.COM cred, NULL); 730*11736SDonghai.Qiao@Sun.COM } 731*11736SDonghai.Qiao@Sun.COM 7320Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 7330Sstevel@tonic-gate if (newcred != NULL) { 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * Free the old credentials, and set the new ones. 7360Sstevel@tonic-gate * Do this for both the process and the (single) thread. 7370Sstevel@tonic-gate */ 7380Sstevel@tonic-gate crfree(pp->p_cred); 7390Sstevel@tonic-gate pp->p_cred = cred; /* cred already held for proc */ 7400Sstevel@tonic-gate crhold(cred); /* hold new cred for thread */ 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred 7430Sstevel@tonic-gate * must always be either NULL, or point to a valid, 7440Sstevel@tonic-gate * allocated cred structure. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate oldcred = curthread->t_cred; 7470Sstevel@tonic-gate curthread->t_cred = cred; 7480Sstevel@tonic-gate crfree(oldcred); 74911537SCasper.Dik@Sun.COM 75011569SCasper.Dik@Sun.COM if (priv_basic_test >= 0 && 75111537SCasper.Dik@Sun.COM !PRIV_ISASSERT(&CR_IPRIV(newcred), 75211537SCasper.Dik@Sun.COM priv_basic_test)) { 75311537SCasper.Dik@Sun.COM pid_t pid = pp->p_pid; 75411537SCasper.Dik@Sun.COM char *fn = PTOU(pp)->u_comm; 75511537SCasper.Dik@Sun.COM 75611537SCasper.Dik@Sun.COM cmn_err(CE_WARN, "%s[%d]: exec: basic_test " 75711537SCasper.Dik@Sun.COM "privilege removed from E/I", fn, pid); 75811537SCasper.Dik@Sun.COM } 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * On emerging from a successful exec(), the saved 7620Sstevel@tonic-gate * uid and gid equal the effective uid and gid. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate cred->cr_suid = cred->cr_uid; 7650Sstevel@tonic-gate cred->cr_sgid = cred->cr_gid; 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate /* 7680Sstevel@tonic-gate * If the real and effective ids do not match, this 7690Sstevel@tonic-gate * is a setuid process that should not dump core. 7700Sstevel@tonic-gate * The group comparison is tricky; we prevent the code 7710Sstevel@tonic-gate * from flagging SNOCD when executing with an effective gid 7720Sstevel@tonic-gate * which is a supplementary group. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate if (cred->cr_ruid != cred->cr_uid || 7750Sstevel@tonic-gate (cred->cr_rgid != cred->cr_gid && 7760Sstevel@tonic-gate !supgroupmember(cred->cr_gid, cred)) || 7770Sstevel@tonic-gate (privflags & PRIV_INCREASE) != 0) 7780Sstevel@tonic-gate suidflags = PSUIDFLAGS; 7790Sstevel@tonic-gate else 7800Sstevel@tonic-gate suidflags = 0; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 7830Sstevel@tonic-gate if (suidflags) { 7840Sstevel@tonic-gate mutex_enter(&pp->p_lock); 7850Sstevel@tonic-gate pp->p_flag |= suidflags; 7860Sstevel@tonic-gate mutex_exit(&pp->p_lock); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) { 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * If process is traced via /proc, arrange to 7910Sstevel@tonic-gate * invalidate the associated /proc vnode. 7920Sstevel@tonic-gate */ 7930Sstevel@tonic-gate if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE)) 7940Sstevel@tonic-gate args->traceinval = 1; 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate if (pp->p_proc_flag & P_PR_PTRACE) 7970Sstevel@tonic-gate psignal(pp, SIGTRAP); 7980Sstevel@tonic-gate if (args->traceinval) 7990Sstevel@tonic-gate prinvalidate(&pp->p_user); 8000Sstevel@tonic-gate } 801*11736SDonghai.Qiao@Sun.COM if (execvp) 802*11736SDonghai.Qiao@Sun.COM VN_RELE(execvp); 8030Sstevel@tonic-gate return (0); 804*11736SDonghai.Qiao@Sun.COM 8050Sstevel@tonic-gate bad: 806*11736SDonghai.Qiao@Sun.COM (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL); 807*11736SDonghai.Qiao@Sun.COM 808*11736SDonghai.Qiao@Sun.COM bad_noclose: 8090Sstevel@tonic-gate if (error == 0) 8100Sstevel@tonic-gate error = ENOEXEC; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if (suidflags) { 8130Sstevel@tonic-gate mutex_enter(&pp->p_lock); 8140Sstevel@tonic-gate pp->p_flag |= suidflags; 8150Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate return (error); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate extern char *execswnames[]; 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate struct execsw * 8230Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size) 8240Sstevel@tonic-gate { 8250Sstevel@tonic-gate int i, j; 8260Sstevel@tonic-gate char *ename; 8270Sstevel@tonic-gate char *magicp; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate mutex_enter(&execsw_lock); 8300Sstevel@tonic-gate for (i = 0; i < nexectype; i++) { 8310Sstevel@tonic-gate if (execswnames[i] == NULL) { 8320Sstevel@tonic-gate ename = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8330Sstevel@tonic-gate (void) strcpy(ename, name); 8340Sstevel@tonic-gate execswnames[i] = ename; 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * Set the magic number last so that we 8370Sstevel@tonic-gate * don't need to hold the execsw_lock in 8380Sstevel@tonic-gate * findexectype(). 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate magicp = kmem_alloc(magic_size, KM_SLEEP); 8410Sstevel@tonic-gate for (j = 0; j < magic_size; j++) 8420Sstevel@tonic-gate magicp[j] = magic[j]; 8430Sstevel@tonic-gate execsw[i].exec_magic = magicp; 8440Sstevel@tonic-gate mutex_exit(&execsw_lock); 8450Sstevel@tonic-gate return (&execsw[i]); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate mutex_exit(&execsw_lock); 8490Sstevel@tonic-gate return (NULL); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate /* 8530Sstevel@tonic-gate * Find the exec switch table entry with the corresponding magic string. 8540Sstevel@tonic-gate */ 8550Sstevel@tonic-gate struct execsw * 8560Sstevel@tonic-gate findexecsw(char *magic) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate struct execsw *eswp; 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 8610Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 8620Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 && 8630Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) 8640Sstevel@tonic-gate return (eswp); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate return (NULL); 8670Sstevel@tonic-gate } 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /* 8700Sstevel@tonic-gate * Find the execsw[] index for the given exec header string by looking for the 8710Sstevel@tonic-gate * magic string at a specified offset and length for each kind of executable 8720Sstevel@tonic-gate * file format until one matches. If no execsw[] entry is found, try to 8730Sstevel@tonic-gate * autoload a module for this magic string. 8740Sstevel@tonic-gate */ 8750Sstevel@tonic-gate struct execsw * 8760Sstevel@tonic-gate findexec_by_hdr(char *header) 8770Sstevel@tonic-gate { 8780Sstevel@tonic-gate struct execsw *eswp; 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 8810Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 8820Sstevel@tonic-gate if (header && eswp->exec_maglen != 0 && 8830Sstevel@tonic-gate bcmp(&header[eswp->exec_magoff], eswp->exec_magic, 8845753Sgww eswp->exec_maglen) == 0) { 8850Sstevel@tonic-gate if (hold_execsw(eswp) != 0) 8860Sstevel@tonic-gate return (NULL); 8870Sstevel@tonic-gate return (eswp); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate return (NULL); /* couldn't find the type */ 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * Find the execsw[] index for the given magic string. If no execsw[] entry 8950Sstevel@tonic-gate * is found, try to autoload a module for this magic string. 8960Sstevel@tonic-gate */ 8970Sstevel@tonic-gate struct execsw * 8980Sstevel@tonic-gate findexec_by_magic(char *magic) 8990Sstevel@tonic-gate { 9000Sstevel@tonic-gate struct execsw *eswp; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 9030Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 9040Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 && 9050Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) { 9060Sstevel@tonic-gate if (hold_execsw(eswp) != 0) 9070Sstevel@tonic-gate return (NULL); 9080Sstevel@tonic-gate return (eswp); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate return (NULL); /* couldn't find the type */ 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate static int 9150Sstevel@tonic-gate hold_execsw(struct execsw *eswp) 9160Sstevel@tonic-gate { 9170Sstevel@tonic-gate char *name; 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER); 9200Sstevel@tonic-gate while (!LOADED_EXEC(eswp)) { 9210Sstevel@tonic-gate rw_exit(eswp->exec_lock); 9220Sstevel@tonic-gate name = execswnames[eswp-execsw]; 9230Sstevel@tonic-gate ASSERT(name); 9240Sstevel@tonic-gate if (modload("exec", name) == -1) 9250Sstevel@tonic-gate return (-1); 9260Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate return (0); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate static int 9320Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate proc_t *pp = ttoproc(curthread); 9350Sstevel@tonic-gate uid_t uid, gid; 9360Sstevel@tonic-gate cred_t *cr = pp->p_cred; 9370Sstevel@tonic-gate int privflags = 0; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * Remember credentials. 9410Sstevel@tonic-gate */ 9420Sstevel@tonic-gate uid = cr->cr_uid; 9430Sstevel@tonic-gate gid = cr->cr_gid; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* Will try to reset the PRIV_AWARE bit later. */ 9460Sstevel@tonic-gate if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE) 9470Sstevel@tonic-gate privflags |= PRIV_RESET; 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) { 9500Sstevel@tonic-gate /* 9510Sstevel@tonic-gate * Set-uid root execution only allowed if the limit set 9520Sstevel@tonic-gate * holds all unsafe privileges. 9530Sstevel@tonic-gate */ 9540Sstevel@tonic-gate if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 || 9550Sstevel@tonic-gate priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) { 9560Sstevel@tonic-gate uid = vattrp->va_uid; 9570Sstevel@tonic-gate privflags |= PRIV_SETUGID; 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate if (vattrp->va_mode & VSGID) { 9600Sstevel@tonic-gate gid = vattrp->va_gid; 9610Sstevel@tonic-gate privflags |= PRIV_SETUGID; 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * Do we need to change our credential anyway? 9670Sstevel@tonic-gate * This is the case when E != I or P != I, as 9680Sstevel@tonic-gate * we need to do the assignments (with F empty and A full) 9690Sstevel@tonic-gate * Or when I is not a subset of L; in that case we need to 9700Sstevel@tonic-gate * enforce L. 9710Sstevel@tonic-gate * 9720Sstevel@tonic-gate * I' = L & I 9730Sstevel@tonic-gate * 9740Sstevel@tonic-gate * E' = P' = (I' + F) & A 9750Sstevel@tonic-gate * or 9760Sstevel@tonic-gate * E' = P' = I' 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) || 9790Sstevel@tonic-gate !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) || 9800Sstevel@tonic-gate !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr))) 9810Sstevel@tonic-gate privflags |= PRIV_RESET; 9820Sstevel@tonic-gate 9831676Sjpk /* If MAC-aware flag(s) are on, need to update cred to remove. */ 9841676Sjpk if ((CR_FLAGS(cr) & NET_MAC_AWARE) || 9851676Sjpk (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT)) 9861676Sjpk privflags |= MAC_FLAGS; 9871676Sjpk 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * When we introduce the "forced" set then we will need 9900Sstevel@tonic-gate * to set PRIV_INCREASE here if I not a subset of P. 9910Sstevel@tonic-gate * If the "allowed" set is introduced we will need to do 9920Sstevel@tonic-gate * a similar thing; however, it seems more reasonable to 9930Sstevel@tonic-gate * have the allowed set reduce "L": script language interpreters 9940Sstevel@tonic-gate * would typically have an allowed set of "all". 9950Sstevel@tonic-gate */ 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * Set setuid/setgid protections if no ptrace() compatibility. 9990Sstevel@tonic-gate * For privileged processes, honor setuid/setgid even in 10000Sstevel@tonic-gate * the presence of ptrace() compatibility. 10010Sstevel@tonic-gate */ 10020Sstevel@tonic-gate if (((pp->p_proc_flag & P_PR_PTRACE) == 0 || 10030Sstevel@tonic-gate PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) && 10040Sstevel@tonic-gate (cr->cr_uid != uid || 10050Sstevel@tonic-gate cr->cr_gid != gid || 10060Sstevel@tonic-gate cr->cr_suid != uid || 10070Sstevel@tonic-gate cr->cr_sgid != gid)) { 10080Sstevel@tonic-gate *uidp = uid; 10090Sstevel@tonic-gate *gidp = gid; 10100Sstevel@tonic-gate privflags |= PRIV_SETID; 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate return (privflags); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate int 10160Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args) 10170Sstevel@tonic-gate { 10180Sstevel@tonic-gate int error; 10190Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE; 10225331Samw if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL)) 10230Sstevel@tonic-gate return (error); 10240Sstevel@tonic-gate /* 10250Sstevel@tonic-gate * Check the access mode. 10260Sstevel@tonic-gate * If VPROC, ask /proc if the file is an object file. 10270Sstevel@tonic-gate */ 10285331Samw if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 || 10290Sstevel@tonic-gate !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) || 10300Sstevel@tonic-gate (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 || 10310Sstevel@tonic-gate (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) { 10320Sstevel@tonic-gate if (error == 0) 10330Sstevel@tonic-gate error = EACCES; 10340Sstevel@tonic-gate return (error); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) && 10385331Samw (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) { 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * If process is under ptrace(2) compatibility, 10410Sstevel@tonic-gate * fail the exec(2). 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate if (p->p_proc_flag & P_PR_PTRACE) 10440Sstevel@tonic-gate goto bad; 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * Process is traced via /proc. 10470Sstevel@tonic-gate * Arrange to invalidate the /proc vnode. 10480Sstevel@tonic-gate */ 10490Sstevel@tonic-gate args->traceinval = 1; 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate return (0); 10520Sstevel@tonic-gate bad: 10530Sstevel@tonic-gate if (error == 0) 10540Sstevel@tonic-gate error = ENOEXEC; 10550Sstevel@tonic-gate return (error); 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate /* 10590Sstevel@tonic-gate * Map a section of an executable file into the user's 10600Sstevel@tonic-gate * address space. 10610Sstevel@tonic-gate */ 10620Sstevel@tonic-gate int 10630Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen, 10640Sstevel@tonic-gate off_t offset, int prot, int page, uint_t szc) 10650Sstevel@tonic-gate { 10660Sstevel@tonic-gate int error = 0; 10670Sstevel@tonic-gate off_t oldoffset; 10680Sstevel@tonic-gate caddr_t zfodbase, oldaddr; 10690Sstevel@tonic-gate size_t end, oldlen; 10700Sstevel@tonic-gate size_t zfoddiff; 10710Sstevel@tonic-gate label_t ljb; 10720Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate oldaddr = addr; 10750Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 10760Sstevel@tonic-gate if (len) { 10770Sstevel@tonic-gate oldlen = len; 10780Sstevel@tonic-gate len += ((size_t)oldaddr - (size_t)addr); 10790Sstevel@tonic-gate oldoffset = offset; 10800Sstevel@tonic-gate offset = (off_t)((uintptr_t)offset & PAGEMASK); 10810Sstevel@tonic-gate if (page) { 10820Sstevel@tonic-gate spgcnt_t prefltmem, availm, npages; 10830Sstevel@tonic-gate int preread; 10840Sstevel@tonic-gate uint_t mflag = MAP_PRIVATE | MAP_FIXED; 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) { 10870Sstevel@tonic-gate mflag |= MAP_TEXT; 10880Sstevel@tonic-gate } else { 10890Sstevel@tonic-gate mflag |= MAP_INITDATA; 10900Sstevel@tonic-gate } 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as, 10930Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 10940Sstevel@tonic-gate error = ENOMEM; 10950Sstevel@tonic-gate goto bad; 10960Sstevel@tonic-gate } 10970Sstevel@tonic-gate if (error = VOP_MAP(vp, (offset_t)offset, 10980Sstevel@tonic-gate p->p_as, &addr, len, prot, PROT_ALL, 10995331Samw mflag, CRED(), NULL)) 11000Sstevel@tonic-gate goto bad; 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate /* 11030Sstevel@tonic-gate * If the segment can fit, then we prefault 11040Sstevel@tonic-gate * the entire segment in. This is based on the 11050Sstevel@tonic-gate * model that says the best working set of a 11060Sstevel@tonic-gate * small program is all of its pages. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate npages = (spgcnt_t)btopr(len); 11090Sstevel@tonic-gate prefltmem = freemem - desfree; 11100Sstevel@tonic-gate preread = 11110Sstevel@tonic-gate (npages < prefltmem && len < PGTHRESH) ? 1 : 0; 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate /* 11140Sstevel@tonic-gate * If we aren't prefaulting the segment, 11150Sstevel@tonic-gate * increment "deficit", if necessary to ensure 11160Sstevel@tonic-gate * that pages will become available when this 11170Sstevel@tonic-gate * process starts executing. 11180Sstevel@tonic-gate */ 11190Sstevel@tonic-gate availm = freemem - lotsfree; 11200Sstevel@tonic-gate if (preread == 0 && npages > availm && 11210Sstevel@tonic-gate deficit < lotsfree) { 11220Sstevel@tonic-gate deficit += MIN((pgcnt_t)(npages - availm), 11230Sstevel@tonic-gate lotsfree - deficit); 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate if (preread) { 11270Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD, 11280Sstevel@tonic-gate "execmap preread:freemem %d size %lu", 11290Sstevel@tonic-gate freemem, len); 11300Sstevel@tonic-gate (void) as_fault(p->p_as->a_hat, p->p_as, 11310Sstevel@tonic-gate (caddr_t)addr, len, F_INVAL, S_READ); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate } else { 11340Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as, 11350Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 11360Sstevel@tonic-gate error = ENOMEM; 11370Sstevel@tonic-gate goto bad; 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate if (error = as_map(p->p_as, addr, len, 11410Sstevel@tonic-gate segvn_create, zfod_argsp)) 11420Sstevel@tonic-gate goto bad; 11430Sstevel@tonic-gate /* 11440Sstevel@tonic-gate * Read in the segment in one big chunk. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr, 11470Sstevel@tonic-gate oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0, 11480Sstevel@tonic-gate (rlim64_t)0, CRED(), (ssize_t *)0)) 11490Sstevel@tonic-gate goto bad; 11500Sstevel@tonic-gate /* 11510Sstevel@tonic-gate * Now set protections. 11520Sstevel@tonic-gate */ 11530Sstevel@tonic-gate if (prot != PROT_ZFOD) { 11540Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)addr, 11550Sstevel@tonic-gate len, prot); 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate if (zfodlen) { 11612712Snn35248 struct as *as = curproc->p_as; 11622712Snn35248 struct seg *seg; 11632712Snn35248 uint_t zprot = 0; 11642712Snn35248 11650Sstevel@tonic-gate end = (size_t)addr + len; 11660Sstevel@tonic-gate zfodbase = (caddr_t)roundup(end, PAGESIZE); 11670Sstevel@tonic-gate zfoddiff = (uintptr_t)zfodbase - end; 11680Sstevel@tonic-gate if (zfoddiff) { 11692712Snn35248 /* 11702712Snn35248 * Before we go to zero the remaining space on the last 11712712Snn35248 * page, make sure we have write permission. 11722712Snn35248 */ 11732712Snn35248 11742712Snn35248 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 11752712Snn35248 seg = as_segat(curproc->p_as, (caddr_t)end); 11762712Snn35248 if (seg != NULL) 11772712Snn35248 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1, 11782712Snn35248 &zprot); 11792712Snn35248 AS_LOCK_EXIT(as, &as->a_lock); 11802712Snn35248 11812712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) { 11822712Snn35248 (void) as_setprot(as, (caddr_t)end, 11832712Snn35248 zfoddiff - 1, zprot | PROT_WRITE); 11842712Snn35248 } 11852712Snn35248 11860Sstevel@tonic-gate if (on_fault(&ljb)) { 11870Sstevel@tonic-gate no_fault(); 11882712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) 11892712Snn35248 (void) as_setprot(as, (caddr_t)end, 11905753Sgww zfoddiff - 1, zprot); 11910Sstevel@tonic-gate error = EFAULT; 11920Sstevel@tonic-gate goto bad; 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate uzero((void *)end, zfoddiff); 11950Sstevel@tonic-gate no_fault(); 11962712Snn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) 11972712Snn35248 (void) as_setprot(as, (caddr_t)end, 11982712Snn35248 zfoddiff - 1, zprot); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate if (zfodlen > zfoddiff) { 12010Sstevel@tonic-gate struct segvn_crargs crargs = 12020Sstevel@tonic-gate SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL); 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate zfodlen -= zfoddiff; 12050Sstevel@tonic-gate if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as, 12060Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 12070Sstevel@tonic-gate error = ENOMEM; 12080Sstevel@tonic-gate goto bad; 12090Sstevel@tonic-gate } 12102991Ssusans if (szc > 0) { 12112991Ssusans /* 12122991Ssusans * ASSERT alignment because the mapelfexec() 12132991Ssusans * caller for the szc > 0 case extended zfod 12142991Ssusans * so it's end is pgsz aligned. 12152991Ssusans */ 12162991Ssusans size_t pgsz = page_get_pagesize(szc); 12172991Ssusans ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz)); 12182991Ssusans 12192991Ssusans if (IS_P2ALIGNED(zfodbase, pgsz)) { 12202991Ssusans crargs.szc = szc; 12212991Ssusans } else { 12222991Ssusans crargs.szc = AS_MAP_HEAP; 12232991Ssusans } 12242991Ssusans } else { 12252991Ssusans crargs.szc = AS_MAP_NO_LPOOB; 12262991Ssusans } 12270Sstevel@tonic-gate if (error = as_map(p->p_as, (caddr_t)zfodbase, 12280Sstevel@tonic-gate zfodlen, segvn_create, &crargs)) 12290Sstevel@tonic-gate goto bad; 12300Sstevel@tonic-gate if (prot != PROT_ZFOD) { 12310Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)zfodbase, 12320Sstevel@tonic-gate zfodlen, prot); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate return (0); 12370Sstevel@tonic-gate bad: 12380Sstevel@tonic-gate return (error); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate void 12420Sstevel@tonic-gate setexecenv(struct execenv *ep) 12430Sstevel@tonic-gate { 12440Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 12450Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 12460Sstevel@tonic-gate struct vnode *vp; 12470Sstevel@tonic-gate 12480Sstevel@tonic-gate p->p_bssbase = ep->ex_bssbase; 12490Sstevel@tonic-gate p->p_brkbase = ep->ex_brkbase; 12500Sstevel@tonic-gate p->p_brksize = ep->ex_brksize; 12510Sstevel@tonic-gate if (p->p_exec) 12520Sstevel@tonic-gate VN_RELE(p->p_exec); /* out with the old */ 12530Sstevel@tonic-gate vp = p->p_exec = ep->ex_vp; 12540Sstevel@tonic-gate if (vp != NULL) 12550Sstevel@tonic-gate VN_HOLD(vp); /* in with the new */ 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0; 12580Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0; 12590Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate int 12630Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp) 12640Sstevel@tonic-gate { 12650Sstevel@tonic-gate struct vnode *vp = *vpp; 12660Sstevel@tonic-gate file_t *fp; 12670Sstevel@tonic-gate int error = 0; 12680Sstevel@tonic-gate int filemode = FREAD; 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate VN_HOLD(vp); /* open reference */ 12710Sstevel@tonic-gate if (error = falloc(NULL, filemode, &fp, fdp)) { 12720Sstevel@tonic-gate VN_RELE(vp); 12730Sstevel@tonic-gate *fdp = -1; /* just in case falloc changed value */ 12740Sstevel@tonic-gate return (error); 12750Sstevel@tonic-gate } 12765331Samw if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) { 12770Sstevel@tonic-gate VN_RELE(vp); 12780Sstevel@tonic-gate setf(*fdp, NULL); 12790Sstevel@tonic-gate unfalloc(fp); 12800Sstevel@tonic-gate *fdp = -1; 12810Sstevel@tonic-gate return (error); 12820Sstevel@tonic-gate } 12830Sstevel@tonic-gate *vpp = vp; /* vnode should not have changed */ 12840Sstevel@tonic-gate fp->f_vnode = vp; 12850Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 12860Sstevel@tonic-gate setf(*fdp, fp); 12870Sstevel@tonic-gate return (0); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate int 12910Sstevel@tonic-gate execclose(int fd) 12920Sstevel@tonic-gate { 12930Sstevel@tonic-gate return (closeandsetf(fd, NULL)); 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate /* 12980Sstevel@tonic-gate * noexec stub function. 12990Sstevel@tonic-gate */ 13000Sstevel@tonic-gate /*ARGSUSED*/ 13010Sstevel@tonic-gate int 13020Sstevel@tonic-gate noexec( 13030Sstevel@tonic-gate struct vnode *vp, 13040Sstevel@tonic-gate struct execa *uap, 13050Sstevel@tonic-gate struct uarg *args, 13060Sstevel@tonic-gate struct intpdata *idatap, 13070Sstevel@tonic-gate int level, 13080Sstevel@tonic-gate long *execsz, 13090Sstevel@tonic-gate int setid, 13100Sstevel@tonic-gate caddr_t exec_file, 13110Sstevel@tonic-gate struct cred *cred) 13120Sstevel@tonic-gate { 13130Sstevel@tonic-gate cmn_err(CE_WARN, "missing exec capability for %s", uap->fname); 13140Sstevel@tonic-gate return (ENOEXEC); 13150Sstevel@tonic-gate } 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate /* 13180Sstevel@tonic-gate * Support routines for building a user stack. 13190Sstevel@tonic-gate * 13200Sstevel@tonic-gate * execve(path, argv, envp) must construct a new stack with the specified 13210Sstevel@tonic-gate * arguments and environment variables (see exec_args() for a description 13220Sstevel@tonic-gate * of the user stack layout). To do this, we copy the arguments and 13230Sstevel@tonic-gate * environment variables from the old user address space into the kernel, 13240Sstevel@tonic-gate * free the old as, create the new as, and copy our buffered information 13250Sstevel@tonic-gate * to the new stack. Our kernel buffer has the following structure: 13260Sstevel@tonic-gate * 13270Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size 13280Sstevel@tonic-gate * | string offsets | 13290Sstevel@tonic-gate * +-----------------------+ <--- stk_offp 13300Sstevel@tonic-gate * | | 13310Sstevel@tonic-gate * | STK_AVAIL() space | 13320Sstevel@tonic-gate * | | 13330Sstevel@tonic-gate * +-----------------------+ <--- stk_strp 13340Sstevel@tonic-gate * | strings | 13350Sstevel@tonic-gate * +-----------------------+ <--- stk_base 13360Sstevel@tonic-gate * 13370Sstevel@tonic-gate * When we add a string, we store the string's contents (including the null 13380Sstevel@tonic-gate * terminator) at stk_strp, and we store the offset of the string relative to 13390Sstevel@tonic-gate * stk_base at --stk_offp. At strings are added, stk_strp increases and 13400Sstevel@tonic-gate * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just 13410Sstevel@tonic-gate * the difference between these pointers. If we run out of space, we return 13420Sstevel@tonic-gate * an error and exec_args() starts all over again with a buffer twice as large. 13430Sstevel@tonic-gate * When we're all done, the kernel buffer looks like this: 13440Sstevel@tonic-gate * 13450Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size 13460Sstevel@tonic-gate * | argv[0] offset | 13470Sstevel@tonic-gate * +-----------------------+ 13480Sstevel@tonic-gate * | ... | 13490Sstevel@tonic-gate * +-----------------------+ 13500Sstevel@tonic-gate * | argv[argc-1] offset | 13510Sstevel@tonic-gate * +-----------------------+ 13520Sstevel@tonic-gate * | envp[0] offset | 13530Sstevel@tonic-gate * +-----------------------+ 13540Sstevel@tonic-gate * | ... | 13550Sstevel@tonic-gate * +-----------------------+ 13560Sstevel@tonic-gate * | envp[envc-1] offset | 13570Sstevel@tonic-gate * +-----------------------+ 13580Sstevel@tonic-gate * | AT_SUN_PLATFORM offset| 13590Sstevel@tonic-gate * +-----------------------+ 13600Sstevel@tonic-gate * | AT_SUN_EXECNAME offset| 13610Sstevel@tonic-gate * +-----------------------+ <--- stk_offp 13620Sstevel@tonic-gate * | | 13630Sstevel@tonic-gate * | STK_AVAIL() space | 13640Sstevel@tonic-gate * | | 13650Sstevel@tonic-gate * +-----------------------+ <--- stk_strp 13660Sstevel@tonic-gate * | AT_SUN_EXECNAME offset| 13670Sstevel@tonic-gate * +-----------------------+ 13680Sstevel@tonic-gate * | AT_SUN_PLATFORM offset| 13690Sstevel@tonic-gate * +-----------------------+ 13700Sstevel@tonic-gate * | envp[envc-1] string | 13710Sstevel@tonic-gate * +-----------------------+ 13720Sstevel@tonic-gate * | ... | 13730Sstevel@tonic-gate * +-----------------------+ 13740Sstevel@tonic-gate * | envp[0] string | 13750Sstevel@tonic-gate * +-----------------------+ 13760Sstevel@tonic-gate * | argv[argc-1] string | 13770Sstevel@tonic-gate * +-----------------------+ 13780Sstevel@tonic-gate * | ... | 13790Sstevel@tonic-gate * +-----------------------+ 13800Sstevel@tonic-gate * | argv[0] string | 13810Sstevel@tonic-gate * +-----------------------+ <--- stk_base 13820Sstevel@tonic-gate */ 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp) 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* 13870Sstevel@tonic-gate * Add a string to the stack. 13880Sstevel@tonic-gate */ 13890Sstevel@tonic-gate static int 13900Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg) 13910Sstevel@tonic-gate { 13920Sstevel@tonic-gate int error; 13930Sstevel@tonic-gate size_t len; 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate if (STK_AVAIL(args) < sizeof (int)) 13960Sstevel@tonic-gate return (E2BIG); 13970Sstevel@tonic-gate *--args->stk_offp = args->stk_strp - args->stk_base; 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate if (segflg == UIO_USERSPACE) { 14000Sstevel@tonic-gate error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len); 14010Sstevel@tonic-gate if (error != 0) 14020Sstevel@tonic-gate return (error); 14030Sstevel@tonic-gate } else { 14040Sstevel@tonic-gate len = strlen(sp) + 1; 14050Sstevel@tonic-gate if (len > STK_AVAIL(args)) 14060Sstevel@tonic-gate return (E2BIG); 14070Sstevel@tonic-gate bcopy(sp, args->stk_strp, len); 14080Sstevel@tonic-gate } 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate args->stk_strp += len; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate return (0); 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate static int 14160Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst) 14170Sstevel@tonic-gate { 14180Sstevel@tonic-gate int error; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate if (args->from_model == DATAMODEL_NATIVE) { 14210Sstevel@tonic-gate ulong_t ptr; 14220Sstevel@tonic-gate error = fulword(src, &ptr); 14230Sstevel@tonic-gate *dst = (caddr_t)ptr; 14240Sstevel@tonic-gate } else { 14250Sstevel@tonic-gate uint32_t ptr; 14260Sstevel@tonic-gate error = fuword32(src, &ptr); 14270Sstevel@tonic-gate *dst = (caddr_t)(uintptr_t)ptr; 14280Sstevel@tonic-gate } 14290Sstevel@tonic-gate return (error); 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate static int 14330Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value) 14340Sstevel@tonic-gate { 14350Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) 14360Sstevel@tonic-gate return (sulword(addr, (ulong_t)value)); 14370Sstevel@tonic-gate else 14380Sstevel@tonic-gate return (suword32(addr, (uint32_t)(uintptr_t)value)); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate static int 14420Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 14430Sstevel@tonic-gate { 14440Sstevel@tonic-gate char *sp; 14450Sstevel@tonic-gate int argc, error; 14460Sstevel@tonic-gate int argv_empty = 0; 14470Sstevel@tonic-gate size_t ptrsize = args->from_ptrsize; 14480Sstevel@tonic-gate size_t size, pad; 14490Sstevel@tonic-gate char *argv = (char *)uap->argp; 14500Sstevel@tonic-gate char *envp = (char *)uap->envp; 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate /* 14530Sstevel@tonic-gate * Copy interpreter's name and argument to argv[0] and argv[1]. 14540Sstevel@tonic-gate */ 14550Sstevel@tonic-gate if (intp != NULL && intp->intp_name != NULL) { 14560Sstevel@tonic-gate if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0) 14570Sstevel@tonic-gate return (error); 14580Sstevel@tonic-gate if (intp->intp_arg != NULL && 14590Sstevel@tonic-gate (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0) 14600Sstevel@tonic-gate return (error); 14610Sstevel@tonic-gate if (args->fname != NULL) 14620Sstevel@tonic-gate error = stk_add(args, args->fname, UIO_SYSSPACE); 14630Sstevel@tonic-gate else 14640Sstevel@tonic-gate error = stk_add(args, uap->fname, UIO_USERSPACE); 14650Sstevel@tonic-gate if (error) 14660Sstevel@tonic-gate return (error); 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate /* 14690Sstevel@tonic-gate * Check for an empty argv[]. 14700Sstevel@tonic-gate */ 14710Sstevel@tonic-gate if (stk_getptr(args, argv, &sp)) 14720Sstevel@tonic-gate return (EFAULT); 14730Sstevel@tonic-gate if (sp == NULL) 14740Sstevel@tonic-gate argv_empty = 1; 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate argv += ptrsize; /* ignore original argv[0] */ 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate if (argv_empty == 0) { 14800Sstevel@tonic-gate /* 14810Sstevel@tonic-gate * Add argv[] strings to the stack. 14820Sstevel@tonic-gate */ 14830Sstevel@tonic-gate for (;;) { 14840Sstevel@tonic-gate if (stk_getptr(args, argv, &sp)) 14850Sstevel@tonic-gate return (EFAULT); 14860Sstevel@tonic-gate if (sp == NULL) 14870Sstevel@tonic-gate break; 14880Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 14890Sstevel@tonic-gate return (error); 14900Sstevel@tonic-gate argv += ptrsize; 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 14940Sstevel@tonic-gate args->arglen = args->stk_strp - args->stk_base; 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate /* 14970Sstevel@tonic-gate * Add environ[] strings to the stack. 14980Sstevel@tonic-gate */ 14990Sstevel@tonic-gate if (envp != NULL) { 15000Sstevel@tonic-gate for (;;) { 15010Sstevel@tonic-gate if (stk_getptr(args, envp, &sp)) 15020Sstevel@tonic-gate return (EFAULT); 15030Sstevel@tonic-gate if (sp == NULL) 15040Sstevel@tonic-gate break; 15050Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 15060Sstevel@tonic-gate return (error); 15070Sstevel@tonic-gate envp += ptrsize; 15080Sstevel@tonic-gate } 15090Sstevel@tonic-gate } 15100Sstevel@tonic-gate args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 15110Sstevel@tonic-gate args->ne = args->na - argc; 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate /* 15142712Snn35248 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and 15152712Snn35248 * AT_SUN_EMULATOR strings to the stack. 15160Sstevel@tonic-gate */ 15170Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) { 15180Sstevel@tonic-gate if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0) 15190Sstevel@tonic-gate return (error); 15200Sstevel@tonic-gate if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0) 15210Sstevel@tonic-gate return (error); 15222712Snn35248 if (args->brandname != NULL && 15236247Sraf (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0) 15242712Snn35248 return (error); 15252712Snn35248 if (args->emulator != NULL && 15266247Sraf (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0) 15272712Snn35248 return (error); 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate /* 15310Sstevel@tonic-gate * Compute the size of the stack. This includes all the pointers, 15320Sstevel@tonic-gate * the space reserved for the aux vector, and all the strings. 15330Sstevel@tonic-gate * The total number of pointers is args->na (which is argc + envc) 15340Sstevel@tonic-gate * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL 15350Sstevel@tonic-gate * after the last argument (i.e. argv[argc]); (3) the NULL after the 15360Sstevel@tonic-gate * last environment variable (i.e. envp[envc]); and (4) the NULL after 15370Sstevel@tonic-gate * all the strings, at the very top of the stack. 15380Sstevel@tonic-gate */ 15390Sstevel@tonic-gate size = (args->na + 4) * args->to_ptrsize + args->auxsize + 15400Sstevel@tonic-gate (args->stk_strp - args->stk_base); 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate /* 15430Sstevel@tonic-gate * Pad the string section with zeroes to align the stack size. 15440Sstevel@tonic-gate */ 15450Sstevel@tonic-gate pad = P2NPHASE(size, args->stk_align); 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate if (STK_AVAIL(args) < pad) 15480Sstevel@tonic-gate return (E2BIG); 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate args->usrstack_size = size + pad; 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate while (pad-- != 0) 15530Sstevel@tonic-gate *args->stk_strp++ = 0; 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate args->nc = args->stk_strp - args->stk_base; 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate return (0); 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate static int 15610Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up) 15620Sstevel@tonic-gate { 15630Sstevel@tonic-gate size_t ptrsize = args->to_ptrsize; 15640Sstevel@tonic-gate ssize_t pslen; 15650Sstevel@tonic-gate char *kstrp = args->stk_base; 15660Sstevel@tonic-gate char *ustrp = usrstack - args->nc - ptrsize; 15670Sstevel@tonic-gate char *usp = usrstack - args->usrstack_size; 15680Sstevel@tonic-gate int *offp = (int *)(args->stk_base + args->stk_size); 15690Sstevel@tonic-gate int envc = args->ne; 15700Sstevel@tonic-gate int argc = args->na - envc; 15710Sstevel@tonic-gate int i; 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * Record argc for /proc. 15750Sstevel@tonic-gate */ 15760Sstevel@tonic-gate up->u_argc = argc; 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate /* 15790Sstevel@tonic-gate * Put argc on the stack. Note that even though it's an int, 15800Sstevel@tonic-gate * it always consumes ptrsize bytes (for alignment). 15810Sstevel@tonic-gate */ 15820Sstevel@tonic-gate if (stk_putptr(args, usp, (char *)(uintptr_t)argc)) 15830Sstevel@tonic-gate return (-1); 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate /* 15860Sstevel@tonic-gate * Add argc space (ptrsize) to usp and record argv for /proc. 15870Sstevel@tonic-gate */ 15880Sstevel@tonic-gate up->u_argv = (uintptr_t)(usp += ptrsize); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate /* 15910Sstevel@tonic-gate * Put the argv[] pointers on the stack. 15920Sstevel@tonic-gate */ 15930Sstevel@tonic-gate for (i = 0; i < argc; i++, usp += ptrsize) 15940Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp])) 15950Sstevel@tonic-gate return (-1); 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate /* 15980Sstevel@tonic-gate * Copy arguments to u_psargs. 15990Sstevel@tonic-gate */ 16000Sstevel@tonic-gate pslen = MIN(args->arglen, PSARGSZ) - 1; 16010Sstevel@tonic-gate for (i = 0; i < pslen; i++) 16020Sstevel@tonic-gate up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]); 16030Sstevel@tonic-gate while (i < PSARGSZ) 16040Sstevel@tonic-gate up->u_psargs[i++] = '\0'; 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate /* 16070Sstevel@tonic-gate * Add space for argv[]'s NULL terminator (ptrsize) to usp and 16080Sstevel@tonic-gate * record envp for /proc. 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate up->u_envp = (uintptr_t)(usp += ptrsize); 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate /* 16130Sstevel@tonic-gate * Put the envp[] pointers on the stack. 16140Sstevel@tonic-gate */ 16150Sstevel@tonic-gate for (i = 0; i < envc; i++, usp += ptrsize) 16160Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp])) 16170Sstevel@tonic-gate return (-1); 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate /* 16200Sstevel@tonic-gate * Add space for envp[]'s NULL terminator (ptrsize) to usp and 16210Sstevel@tonic-gate * remember where the stack ends, which is also where auxv begins. 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate args->stackend = usp += ptrsize; 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate /* 16260Sstevel@tonic-gate * Put all the argv[], envp[], and auxv strings on the stack. 16270Sstevel@tonic-gate */ 16280Sstevel@tonic-gate if (copyout(args->stk_base, ustrp, args->nc)) 16290Sstevel@tonic-gate return (-1); 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate /* 16320Sstevel@tonic-gate * Fill in the aux vector now that we know the user stack addresses 16332712Snn35248 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and 16342712Snn35248 * AT_SUN_EMULATOR strings. 16350Sstevel@tonic-gate */ 16360Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) { 16370Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) { 16380Sstevel@tonic-gate auxv_t **a = (auxv_t **)auxvpp; 16390Sstevel@tonic-gate ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp]) 16400Sstevel@tonic-gate ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp]) 16412712Snn35248 if (args->brandname != NULL) 16422712Snn35248 ADDAUX(*a, 16432712Snn35248 AT_SUN_BRANDNAME, (long)&ustrp[*--offp]) 16442712Snn35248 if (args->emulator != NULL) 16452712Snn35248 ADDAUX(*a, 16462712Snn35248 AT_SUN_EMULATOR, (long)&ustrp[*--offp]) 16470Sstevel@tonic-gate } else { 16480Sstevel@tonic-gate auxv32_t **a = (auxv32_t **)auxvpp; 16490Sstevel@tonic-gate ADDAUX(*a, 16500Sstevel@tonic-gate AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp]) 16510Sstevel@tonic-gate ADDAUX(*a, 16522712Snn35248 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]) 16532712Snn35248 if (args->brandname != NULL) 16542712Snn35248 ADDAUX(*a, AT_SUN_BRANDNAME, 16552712Snn35248 (int)(uintptr_t)&ustrp[*--offp]) 16562712Snn35248 if (args->emulator != NULL) 16572712Snn35248 ADDAUX(*a, AT_SUN_EMULATOR, 16582712Snn35248 (int)(uintptr_t)&ustrp[*--offp]) 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate return (0); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate /* 16660Sstevel@tonic-gate * Initialize a new user stack with the specified arguments and environment. 16670Sstevel@tonic-gate * The initial user stack layout is as follows: 16680Sstevel@tonic-gate * 16690Sstevel@tonic-gate * User Stack 16700Sstevel@tonic-gate * +---------------+ <--- curproc->p_usrstack 16713177Sdp78419 * | | 16723177Sdp78419 * | slew | 16733177Sdp78419 * | | 16743177Sdp78419 * +---------------+ 16750Sstevel@tonic-gate * | NULL | 16760Sstevel@tonic-gate * +---------------+ 16770Sstevel@tonic-gate * | | 16780Sstevel@tonic-gate * | auxv strings | 16790Sstevel@tonic-gate * | | 16800Sstevel@tonic-gate * +---------------+ 16810Sstevel@tonic-gate * | | 16820Sstevel@tonic-gate * | envp strings | 16830Sstevel@tonic-gate * | | 16840Sstevel@tonic-gate * +---------------+ 16850Sstevel@tonic-gate * | | 16860Sstevel@tonic-gate * | argv strings | 16870Sstevel@tonic-gate * | | 16880Sstevel@tonic-gate * +---------------+ <--- ustrp 16890Sstevel@tonic-gate * | | 16900Sstevel@tonic-gate * | aux vector | 16910Sstevel@tonic-gate * | | 16920Sstevel@tonic-gate * +---------------+ <--- auxv 16930Sstevel@tonic-gate * | NULL | 16940Sstevel@tonic-gate * +---------------+ 16950Sstevel@tonic-gate * | envp[envc-1] | 16960Sstevel@tonic-gate * +---------------+ 16970Sstevel@tonic-gate * | ... | 16980Sstevel@tonic-gate * +---------------+ 16990Sstevel@tonic-gate * | envp[0] | 17000Sstevel@tonic-gate * +---------------+ <--- envp[] 17010Sstevel@tonic-gate * | NULL | 17020Sstevel@tonic-gate * +---------------+ 17030Sstevel@tonic-gate * | argv[argc-1] | 17040Sstevel@tonic-gate * +---------------+ 17050Sstevel@tonic-gate * | ... | 17060Sstevel@tonic-gate * +---------------+ 17070Sstevel@tonic-gate * | argv[0] | 17080Sstevel@tonic-gate * +---------------+ <--- argv[] 17090Sstevel@tonic-gate * | argc | 17100Sstevel@tonic-gate * +---------------+ <--- stack base 17110Sstevel@tonic-gate */ 17120Sstevel@tonic-gate int 17130Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 17140Sstevel@tonic-gate { 17150Sstevel@tonic-gate size_t size; 17160Sstevel@tonic-gate int error; 17170Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 17180Sstevel@tonic-gate user_t *up = PTOU(p); 17190Sstevel@tonic-gate char *usrstack; 17200Sstevel@tonic-gate rctl_entity_p_t e; 17210Sstevel@tonic-gate struct as *as; 17222991Ssusans extern int use_stk_lpg; 17233177Sdp78419 size_t sp_slew; 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate args->from_model = p->p_model; 17260Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) { 17270Sstevel@tonic-gate args->from_ptrsize = sizeof (long); 17280Sstevel@tonic-gate } else { 17290Sstevel@tonic-gate args->from_ptrsize = sizeof (int32_t); 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) { 17330Sstevel@tonic-gate args->to_ptrsize = sizeof (long); 17340Sstevel@tonic-gate args->ncargs = NCARGS; 17350Sstevel@tonic-gate args->stk_align = STACK_ALIGN; 17367838SRoger.Faulkner@Sun.COM if (args->addr32) 17377838SRoger.Faulkner@Sun.COM usrstack = (char *)USRSTACK64_32; 17387838SRoger.Faulkner@Sun.COM else 17397838SRoger.Faulkner@Sun.COM usrstack = (char *)USRSTACK; 17400Sstevel@tonic-gate } else { 17410Sstevel@tonic-gate args->to_ptrsize = sizeof (int32_t); 17420Sstevel@tonic-gate args->ncargs = NCARGS32; 17430Sstevel@tonic-gate args->stk_align = STACK_ALIGN32; 17440Sstevel@tonic-gate usrstack = (char *)USRSTACK32; 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate 17470Sstevel@tonic-gate ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0); 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate #if defined(__sparc) 17500Sstevel@tonic-gate /* 17510Sstevel@tonic-gate * Make sure user register windows are empty before 17520Sstevel@tonic-gate * attempting to make a new stack. 17530Sstevel@tonic-gate */ 17540Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 17550Sstevel@tonic-gate #endif 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate for (size = PAGESIZE; ; size *= 2) { 17580Sstevel@tonic-gate args->stk_size = size; 17590Sstevel@tonic-gate args->stk_base = kmem_alloc(size, KM_SLEEP); 17600Sstevel@tonic-gate args->stk_strp = args->stk_base; 17610Sstevel@tonic-gate args->stk_offp = (int *)(args->stk_base + size); 17620Sstevel@tonic-gate error = stk_copyin(uap, args, intp, auxvpp); 17630Sstevel@tonic-gate if (error == 0) 17640Sstevel@tonic-gate break; 17650Sstevel@tonic-gate kmem_free(args->stk_base, size); 17660Sstevel@tonic-gate if (error != E2BIG && error != ENAMETOOLONG) 17670Sstevel@tonic-gate return (error); 17680Sstevel@tonic-gate if (size >= args->ncargs) 17690Sstevel@tonic-gate return (E2BIG); 17700Sstevel@tonic-gate } 17710Sstevel@tonic-gate 17720Sstevel@tonic-gate size = args->usrstack_size; 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate ASSERT(error == 0); 17750Sstevel@tonic-gate ASSERT(P2PHASE(size, args->stk_align) == 0); 17760Sstevel@tonic-gate ASSERT((ssize_t)STK_AVAIL(args) >= 0); 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate if (size > args->ncargs) { 17790Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 17800Sstevel@tonic-gate return (E2BIG); 17810Sstevel@tonic-gate } 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate /* 17840Sstevel@tonic-gate * Leave only the current lwp and force the other lwps to exit. 17850Sstevel@tonic-gate * If another lwp beat us to the punch by calling exit(), bail out. 17860Sstevel@tonic-gate */ 17870Sstevel@tonic-gate if ((error = exitlwps(0)) != 0) { 17880Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 17890Sstevel@tonic-gate return (error); 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate /* 17930Sstevel@tonic-gate * Revoke any doors created by the process. 17940Sstevel@tonic-gate */ 17950Sstevel@tonic-gate if (p->p_door_list) 17960Sstevel@tonic-gate door_exit(); 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate /* 17990Sstevel@tonic-gate * Release schedctl data structures. 18000Sstevel@tonic-gate */ 18010Sstevel@tonic-gate if (p->p_pagep) 18020Sstevel@tonic-gate schedctl_proc_cleanup(); 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate /* 18050Sstevel@tonic-gate * Clean up any DTrace helpers for the process. 18060Sstevel@tonic-gate */ 18070Sstevel@tonic-gate if (p->p_dtrace_helpers != NULL) { 18080Sstevel@tonic-gate ASSERT(dtrace_helpers_cleanup != NULL); 18090Sstevel@tonic-gate (*dtrace_helpers_cleanup)(); 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate mutex_enter(&p->p_lock); 18130Sstevel@tonic-gate /* 18140Sstevel@tonic-gate * Cleanup the DTrace provider associated with this process. 18150Sstevel@tonic-gate */ 18160Sstevel@tonic-gate if (p->p_dtrace_probes) { 18170Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr != NULL); 18180Sstevel@tonic-gate dtrace_fasttrap_exec_ptr(p); 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate mutex_exit(&p->p_lock); 18210Sstevel@tonic-gate 18220Sstevel@tonic-gate /* 18230Sstevel@tonic-gate * discard the lwpchan cache. 18240Sstevel@tonic-gate */ 18250Sstevel@tonic-gate if (p->p_lcp != NULL) 18260Sstevel@tonic-gate lwpchan_destroy_cache(1); 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate /* 18290Sstevel@tonic-gate * Delete the POSIX timers. 18300Sstevel@tonic-gate */ 18310Sstevel@tonic-gate if (p->p_itimer != NULL) 18320Sstevel@tonic-gate timer_exit(); 18330Sstevel@tonic-gate 18349870SRoger.Faulkner@Sun.COM /* 18359870SRoger.Faulkner@Sun.COM * Delete the ITIMER_REALPROF interval timer. 18369870SRoger.Faulkner@Sun.COM * The other ITIMER_* interval timers are specified 18379870SRoger.Faulkner@Sun.COM * to be inherited across exec(). 18389870SRoger.Faulkner@Sun.COM */ 18399870SRoger.Faulkner@Sun.COM delete_itimer_realprof(); 18409870SRoger.Faulkner@Sun.COM 18410Sstevel@tonic-gate if (audit_active) 18420Sstevel@tonic-gate audit_exec(args->stk_base, args->stk_base + args->arglen, 18430Sstevel@tonic-gate args->na - args->ne, args->ne); 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate /* 18460Sstevel@tonic-gate * Ensure that we don't change resource associations while we 18470Sstevel@tonic-gate * change address spaces. 18480Sstevel@tonic-gate */ 18490Sstevel@tonic-gate mutex_enter(&p->p_lock); 18500Sstevel@tonic-gate pool_barrier_enter(); 18510Sstevel@tonic-gate mutex_exit(&p->p_lock); 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate /* 18540Sstevel@tonic-gate * Destroy the old address space and create a new one. 18550Sstevel@tonic-gate * From here on, any errors are fatal to the exec()ing process. 18560Sstevel@tonic-gate * On error we return -1, which means the caller must SIGKILL 18570Sstevel@tonic-gate * the process. 18580Sstevel@tonic-gate */ 18590Sstevel@tonic-gate relvm(); 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate mutex_enter(&p->p_lock); 18620Sstevel@tonic-gate pool_barrier_exit(); 18630Sstevel@tonic-gate mutex_exit(&p->p_lock); 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate up->u_execsw = args->execswp; 18660Sstevel@tonic-gate 18670Sstevel@tonic-gate p->p_brkbase = NULL; 18680Sstevel@tonic-gate p->p_brksize = 0; 18692991Ssusans p->p_brkpageszc = 0; 18700Sstevel@tonic-gate p->p_stksize = 0; 18712991Ssusans p->p_stkpageszc = 0; 18720Sstevel@tonic-gate p->p_model = args->to_model; 18730Sstevel@tonic-gate p->p_usrstack = usrstack; 18740Sstevel@tonic-gate p->p_stkprot = args->stk_prot; 18750Sstevel@tonic-gate p->p_datprot = args->dat_prot; 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate /* 18780Sstevel@tonic-gate * Reset resource controls such that all controls are again active as 18790Sstevel@tonic-gate * well as appropriate to the potentially new address model for the 18800Sstevel@tonic-gate * process. 18810Sstevel@tonic-gate */ 18820Sstevel@tonic-gate e.rcep_p.proc = p; 18830Sstevel@tonic-gate e.rcep_t = RCENTITY_PROCESS; 18840Sstevel@tonic-gate rctl_set_reset(p->p_rctls, p, &e); 18850Sstevel@tonic-gate 18862991Ssusans /* Too early to call map_pgsz for the heap */ 18872991Ssusans if (use_stk_lpg) { 18882991Ssusans p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0)); 18892991Ssusans } 18900Sstevel@tonic-gate 18912991Ssusans mutex_enter(&p->p_lock); 18922991Ssusans p->p_flag |= SAUTOLPG; /* kernel controls page sizes */ 18932991Ssusans mutex_exit(&p->p_lock); 18940Sstevel@tonic-gate 18953177Sdp78419 /* 18963177Sdp78419 * Some platforms may choose to randomize real stack start by adding a 18973177Sdp78419 * small slew (not more than a few hundred bytes) to the top of the 18983177Sdp78419 * stack. This helps avoid cache thrashing when identical processes 18993177Sdp78419 * simultaneously share caches that don't provide enough associativity 19003177Sdp78419 * (e.g. sun4v systems). In this case stack slewing makes the same hot 19013177Sdp78419 * stack variables in different processes to live in different cache 19023177Sdp78419 * sets increasing effective associativity. 19033177Sdp78419 */ 19043177Sdp78419 sp_slew = exec_get_spslew(); 19053177Sdp78419 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0); 19063177Sdp78419 exec_set_sp(size + sp_slew); 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate as = as_alloc(); 19090Sstevel@tonic-gate p->p_as = as; 19102768Ssl108498 as->a_proc = p; 19117838SRoger.Faulkner@Sun.COM if (p->p_model == DATAMODEL_ILP32 || args->addr32) 19120Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT32; 19130Sstevel@tonic-gate (void) hat_setup(as->a_hat, HAT_ALLOC); 19144528Spaulsan hat_join_srd(as->a_hat, args->ex_vp); 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate /* 19170Sstevel@tonic-gate * Finally, write out the contents of the new stack. 19180Sstevel@tonic-gate */ 19193177Sdp78419 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up); 19200Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 19210Sstevel@tonic-gate return (error); 19220Sstevel@tonic-gate } 1923