17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5a3d40fb8Scasper * Common Development and Distribution License (the "License"). 6a3d40fb8Scasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21d4204c85Sraf 227c478bd9Sstevel@tonic-gate /* 23634e26ecSCasper H.S. Dik * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/param.h> 327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <sys/signal.h> 357c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h> 367c478bd9Sstevel@tonic-gate #include <sys/policy.h> 377c478bd9Sstevel@tonic-gate #include <sys/user.h> 387c478bd9Sstevel@tonic-gate #include <sys/errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/file.h> 407c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 417c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 427c478bd9Sstevel@tonic-gate #include <sys/mman.h> 437c478bd9Sstevel@tonic-gate #include <sys/acct.h> 447c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 457c478bd9Sstevel@tonic-gate #include <sys/proc.h> 467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 477c478bd9Sstevel@tonic-gate #include <sys/debug.h> 487c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 497c478bd9Sstevel@tonic-gate #include <sys/vm.h> 502cb27123Saguzovsk #include <sys/lgrp.h> 517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 527c478bd9Sstevel@tonic-gate #include <sys/exec.h> 537c478bd9Sstevel@tonic-gate #include <sys/exechdr.h> 547c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 557c478bd9Sstevel@tonic-gate #include <sys/prsystm.h> 567c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 577c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 58d4204c85Sraf #include <sys/door.h> 597c478bd9Sstevel@tonic-gate #include <sys/schedctl.h> 607c478bd9Sstevel@tonic-gate #include <sys/utrap.h> 617c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 627c478bd9Sstevel@tonic-gate #include <sys/stack.h> 637c478bd9Sstevel@tonic-gate #include <sys/rctl.h> 647c478bd9Sstevel@tonic-gate #include <sys/dtrace.h> 657c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h> 667c478bd9Sstevel@tonic-gate #include <sys/pool.h> 677c478bd9Sstevel@tonic-gate #include <sys/sdt.h> 689acbbeafSnn35248 #include <sys/brand.h> 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #include <c2/audit.h> 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #include <vm/hat.h> 737c478bd9Sstevel@tonic-gate #include <vm/anon.h> 747c478bd9Sstevel@tonic-gate #include <vm/as.h> 757c478bd9Sstevel@tonic-gate #include <vm/seg.h> 767c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #define PRIV_RESET 0x01 /* needs to reset privs */ 797c478bd9Sstevel@tonic-gate #define PRIV_SETID 0x02 /* needs to change uids */ 807c478bd9Sstevel@tonic-gate #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */ 817c478bd9Sstevel@tonic-gate #define PRIV_INCREASE 0x08 /* child runs with more privs */ 8245916cd2Sjpk #define MAC_FLAGS 0x10 /* need to adjust MAC flags */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *); 857c478bd9Sstevel@tonic-gate static int hold_execsw(struct execsw *); 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */ 887c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) 897c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */ 907c478bd9Sstevel@tonic-gate #endif 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate #define PSUIDFLAGS (SNOCD|SUGID) 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * exec() - wrapper around exece providing NULL environment pointer 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate int 987c478bd9Sstevel@tonic-gate exec(const char *fname, const char **argp) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate return (exece(fname, argp, NULL)); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * exece() - system call wrapper around exec_common() 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate int 1077c478bd9Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate int error; 1107c478bd9Sstevel@tonic-gate 1119acbbeafSnn35248 error = exec_common(fname, argp, envp, EBA_NONE); 1127c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate int 1169acbbeafSnn35248 exec_common(const char *fname, const char **argp, const char **envp, 1179acbbeafSnn35248 int brand_action) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL; 1207c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 1217c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1227c478bd9Sstevel@tonic-gate struct user *up = PTOU(p); 1237c478bd9Sstevel@tonic-gate long execsz; /* temporary count of exec size */ 1247c478bd9Sstevel@tonic-gate int i; 1257c478bd9Sstevel@tonic-gate int error; 1267c478bd9Sstevel@tonic-gate char exec_file[MAXCOMLEN+1]; 1277c478bd9Sstevel@tonic-gate struct pathname pn; 1287c478bd9Sstevel@tonic-gate struct pathname resolvepn; 1297c478bd9Sstevel@tonic-gate struct uarg args; 1307c478bd9Sstevel@tonic-gate struct execa ua; 1317c478bd9Sstevel@tonic-gate k_sigset_t savedmask; 1327c478bd9Sstevel@tonic-gate lwpdir_t *lwpdir = NULL; 1336eb30ec3SRoger A. Faulkner tidhash_t *tidhash; 1347c478bd9Sstevel@tonic-gate lwpdir_t *old_lwpdir = NULL; 1357c478bd9Sstevel@tonic-gate uint_t old_lwpdir_sz; 1366eb30ec3SRoger A. Faulkner tidhash_t *old_tidhash; 1377c478bd9Sstevel@tonic-gate uint_t old_tidhash_sz; 1386eb30ec3SRoger A. Faulkner ret_tidhash_t *ret_tidhash; 1397c478bd9Sstevel@tonic-gate lwpent_t *lep; 140fd9e7635Sedp boolean_t brandme = B_FALSE; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* 1437c478bd9Sstevel@tonic-gate * exec() is not supported for the /proc agent lwp. 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate if (curthread == p->p_agenttp) 1467c478bd9Sstevel@tonic-gate return (ENOTSUP); 1477c478bd9Sstevel@tonic-gate 1489acbbeafSnn35248 if (brand_action != EBA_NONE) { 1499acbbeafSnn35248 /* 1509acbbeafSnn35248 * Brand actions are not supported for processes that are not 1519acbbeafSnn35248 * running in a branded zone. 1529acbbeafSnn35248 */ 1539acbbeafSnn35248 if (!ZONE_IS_BRANDED(p->p_zone)) 1549acbbeafSnn35248 return (ENOTSUP); 1559acbbeafSnn35248 1569acbbeafSnn35248 if (brand_action == EBA_NATIVE) { 1579acbbeafSnn35248 /* Only branded processes can be unbranded */ 1589acbbeafSnn35248 if (!PROC_IS_BRANDED(p)) 1599acbbeafSnn35248 return (ENOTSUP); 1609acbbeafSnn35248 } else { 1619acbbeafSnn35248 /* Only unbranded processes can be branded */ 1629acbbeafSnn35248 if (PROC_IS_BRANDED(p)) 1639acbbeafSnn35248 return (ENOTSUP); 164fd9e7635Sedp brandme = B_TRUE; 1659acbbeafSnn35248 } 1669acbbeafSnn35248 } else { 1679acbbeafSnn35248 /* 1689acbbeafSnn35248 * If this is a native zone, or if the process is already 1699acbbeafSnn35248 * branded, then we don't need to do anything. If this is 1709acbbeafSnn35248 * a native process in a branded zone, we need to brand the 1719acbbeafSnn35248 * process as it exec()s the new binary. 1729acbbeafSnn35248 */ 1739acbbeafSnn35248 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p)) 174fd9e7635Sedp brandme = B_TRUE; 1759acbbeafSnn35248 } 1769acbbeafSnn35248 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * Inform /proc that an exec() has started. 1797c478bd9Sstevel@tonic-gate * Hold signals that are ignored by default so that we will 1807c478bd9Sstevel@tonic-gate * not be interrupted by a signal that will be ignored after 1817c478bd9Sstevel@tonic-gate * successful completion of gexec(). 1827c478bd9Sstevel@tonic-gate */ 1837c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1847c478bd9Sstevel@tonic-gate prexecstart(); 1857c478bd9Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 1867c478bd9Sstevel@tonic-gate savedmask = curthread->t_hold; 1877c478bd9Sstevel@tonic-gate sigorset(&curthread->t_hold, &ignoredefault); 1887c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * Look up path name and remember last component for later. 1927c478bd9Sstevel@tonic-gate * To help coreadm expand its %d token, we attempt to save 1937c478bd9Sstevel@tonic-gate * the directory containing the executable in p_execdir. The 1947c478bd9Sstevel@tonic-gate * first call to lookuppn() may fail and return EINVAL because 1957c478bd9Sstevel@tonic-gate * dirvpp is non-NULL. In that case, we make a second call to 1967c478bd9Sstevel@tonic-gate * lookuppn() with dirvpp set to NULL; p_execdir will be NULL, 1977c478bd9Sstevel@tonic-gate * but coreadm is allowed to expand %d to the empty string and 1987c478bd9Sstevel@tonic-gate * there are other cases in which that failure may occur. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 2017c478bd9Sstevel@tonic-gate goto out; 2027c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn); 2037c478bd9Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) { 2047c478bd9Sstevel@tonic-gate pn_free(&resolvepn); 2057c478bd9Sstevel@tonic-gate pn_free(&pn); 2067c478bd9Sstevel@tonic-gate if (error != EINVAL) 2077c478bd9Sstevel@tonic-gate goto out; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate dir = NULL; 2107c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0) 2117c478bd9Sstevel@tonic-gate goto out; 2127c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn); 2137c478bd9Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP, 2147c478bd9Sstevel@tonic-gate &vp)) != 0) { 2157c478bd9Sstevel@tonic-gate pn_free(&resolvepn); 2167c478bd9Sstevel@tonic-gate pn_free(&pn); 2177c478bd9Sstevel@tonic-gate goto out; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate if (vp == NULL) { 2217c478bd9Sstevel@tonic-gate if (dir != NULL) 2227c478bd9Sstevel@tonic-gate VN_RELE(dir); 2237c478bd9Sstevel@tonic-gate error = ENOENT; 2247c478bd9Sstevel@tonic-gate pn_free(&resolvepn); 2257c478bd9Sstevel@tonic-gate pn_free(&pn); 2267c478bd9Sstevel@tonic-gate goto out; 2277c478bd9Sstevel@tonic-gate } 228a3d40fb8Scasper 229ddf7fe95Scasper if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) { 230ddf7fe95Scasper if (dir != NULL) 231ddf7fe95Scasper VN_RELE(dir); 232ddf7fe95Scasper pn_free(&resolvepn); 233ddf7fe95Scasper pn_free(&pn); 234ddf7fe95Scasper VN_RELE(vp); 235ddf7fe95Scasper goto out; 236ddf7fe95Scasper } 237ddf7fe95Scasper 238a3d40fb8Scasper /* 239a3d40fb8Scasper * We do not allow executing files in attribute directories. 240a3d40fb8Scasper * We test this by determining whether the resolved path 241a3d40fb8Scasper * contains a "/" when we're in an attribute directory; 242a3d40fb8Scasper * only if the pathname does not contain a "/" the resolved path 243a3d40fb8Scasper * points to a file in the current working (attribute) directory. 244a3d40fb8Scasper */ 245a3d40fb8Scasper if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 && 246a3d40fb8Scasper strchr(resolvepn.pn_path, '/') == NULL) { 247a3d40fb8Scasper if (dir != NULL) 248a3d40fb8Scasper VN_RELE(dir); 249a3d40fb8Scasper error = EACCES; 250a3d40fb8Scasper pn_free(&resolvepn); 251a3d40fb8Scasper pn_free(&pn); 252a3d40fb8Scasper VN_RELE(vp); 253a3d40fb8Scasper goto out; 254a3d40fb8Scasper } 255a3d40fb8Scasper 2567c478bd9Sstevel@tonic-gate bzero(exec_file, MAXCOMLEN+1); 2577c478bd9Sstevel@tonic-gate (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN); 2587c478bd9Sstevel@tonic-gate bzero(&args, sizeof (args)); 2597c478bd9Sstevel@tonic-gate args.pathname = resolvepn.pn_path; 2607c478bd9Sstevel@tonic-gate /* don't free resolvepn until we are done with args */ 2617c478bd9Sstevel@tonic-gate pn_free(&pn); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * Specific exec handlers, or policies determined via 2657c478bd9Sstevel@tonic-gate * /etc/system may override the historical default. 2667c478bd9Sstevel@tonic-gate */ 2677c478bd9Sstevel@tonic-gate args.stk_prot = PROT_ZFOD; 2687c478bd9Sstevel@tonic-gate args.dat_prot = PROT_ZFOD; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate CPU_STATS_ADD_K(sys, sysexec, 1); 2717c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec, char *, args.pathname); 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate ua.fname = fname; 2747c478bd9Sstevel@tonic-gate ua.argp = argp; 2757c478bd9Sstevel@tonic-gate ua.envp = envp; 2767c478bd9Sstevel@tonic-gate 2779acbbeafSnn35248 /* If necessary, brand this process before we start the exec. */ 278fd9e7635Sedp if (brandme) 2799acbbeafSnn35248 brand_setbrand(p); 2809acbbeafSnn35248 2817c478bd9Sstevel@tonic-gate if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz, 2829acbbeafSnn35248 exec_file, p->p_cred, brand_action)) != 0) { 283fd9e7635Sedp if (brandme) 284fd9e7635Sedp brand_clearbrand(p); 2857c478bd9Sstevel@tonic-gate VN_RELE(vp); 2867c478bd9Sstevel@tonic-gate if (dir != NULL) 2877c478bd9Sstevel@tonic-gate VN_RELE(dir); 2887c478bd9Sstevel@tonic-gate pn_free(&resolvepn); 2897c478bd9Sstevel@tonic-gate goto fail; 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * Free floating point registers (sun4u only) 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate ASSERT(lwp != NULL); 2967c478bd9Sstevel@tonic-gate lwp_freeregs(lwp, 1); 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /* 2990baeff3dSrab * Free thread and process context ops. 3007c478bd9Sstevel@tonic-gate */ 3017c478bd9Sstevel@tonic-gate if (curthread->t_ctx) 3027c478bd9Sstevel@tonic-gate freectx(curthread, 1); 3030baeff3dSrab if (p->p_pctx) 3040baeff3dSrab freepctx(p, 1); 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * Remember file name for accounting; clear any cached DTrace predicate. 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate up->u_acflag &= ~AFORK; 3107c478bd9Sstevel@tonic-gate bcopy(exec_file, up->u_comm, MAXCOMLEN+1); 3117c478bd9Sstevel@tonic-gate curthread->t_predcache = NULL; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 3147c478bd9Sstevel@tonic-gate * Clear contract template state 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(lwp); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Save the directory in which we found the executable for expanding 3207c478bd9Sstevel@tonic-gate * the %d token used in core file patterns. 3217c478bd9Sstevel@tonic-gate */ 3227c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3237c478bd9Sstevel@tonic-gate tmpvp = p->p_execdir; 3247c478bd9Sstevel@tonic-gate p->p_execdir = dir; 3257c478bd9Sstevel@tonic-gate if (p->p_execdir != NULL) 3267c478bd9Sstevel@tonic-gate VN_HOLD(p->p_execdir); 3277c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate if (tmpvp != NULL) 3307c478bd9Sstevel@tonic-gate VN_RELE(tmpvp); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * Reset stack state to the user stack, clear set of signals 3347c478bd9Sstevel@tonic-gate * caught on the signal stack, and reset list of signals that 3357c478bd9Sstevel@tonic-gate * restart system calls; the new program's environment should 3367c478bd9Sstevel@tonic-gate * not be affected by detritus from the old program. Any 3377c478bd9Sstevel@tonic-gate * pending held signals remain held, so don't clear t_hold. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3407c478bd9Sstevel@tonic-gate lwp->lwp_oldcontext = 0; 3417c478bd9Sstevel@tonic-gate lwp->lwp_ustack = 0; 3427c478bd9Sstevel@tonic-gate lwp->lwp_old_stk_ctl = 0; 3437c478bd9Sstevel@tonic-gate sigemptyset(&up->u_signodefer); 3447c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigonstack); 3457c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigresethand); 3467c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0; 3477c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0; 3487c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * Make saved resource limit == current resource limit. 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate for (i = 0; i < RLIM_NLIMITS; i++) { 3547c478bd9Sstevel@tonic-gate /*CONSTCOND*/ 3557c478bd9Sstevel@tonic-gate if (RLIM_SAVED(i)) { 3567c478bd9Sstevel@tonic-gate (void) rctl_rlimit_get(rctlproc_legacy[i], p, 3577c478bd9Sstevel@tonic-gate &up->u_saved_rlimit[i]); 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * If the action was to catch the signal, then the action 3637c478bd9Sstevel@tonic-gate * must be reset to SIG_DFL. 3647c478bd9Sstevel@tonic-gate */ 3657c478bd9Sstevel@tonic-gate sigdefault(p); 3667c478bd9Sstevel@tonic-gate p->p_flag &= ~(SNOWAIT|SJCTL); 3677c478bd9Sstevel@tonic-gate p->p_flag |= (SEXECED|SMSACCT|SMSFORK); 3687c478bd9Sstevel@tonic-gate up->u_signal[SIGCLD - 1] = SIG_DFL; 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Delete the dot4 sigqueues/signotifies. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate sigqfree(p); 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate mutex_enter(&p->p_pflock); 3787c478bd9Sstevel@tonic-gate p->p_prof.pr_base = NULL; 3797c478bd9Sstevel@tonic-gate p->p_prof.pr_size = 0; 3807c478bd9Sstevel@tonic-gate p->p_prof.pr_off = 0; 3817c478bd9Sstevel@tonic-gate p->p_prof.pr_scale = 0; 3827c478bd9Sstevel@tonic-gate p->p_prof.pr_samples = 0; 3837c478bd9Sstevel@tonic-gate mutex_exit(&p->p_pflock); 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL); 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate #if defined(__sparc) 3887c478bd9Sstevel@tonic-gate if (p->p_utraps != NULL) 3897c478bd9Sstevel@tonic-gate utrap_free(p); 3907c478bd9Sstevel@tonic-gate #endif /* __sparc */ 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate /* 3937c478bd9Sstevel@tonic-gate * Close all close-on-exec files. 3947c478bd9Sstevel@tonic-gate */ 3957c478bd9Sstevel@tonic-gate close_exec(P_FINFO(p)); 3967c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up); 3979acbbeafSnn35248 398fd9e7635Sedp /* Unbrand ourself if necessary. */ 399fd9e7635Sedp if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE)) 400fd9e7635Sedp brand_clearbrand(p); 4019acbbeafSnn35248 4027c478bd9Sstevel@tonic-gate setregs(&args); 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* Mark this as an executable vnode */ 4057c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 4067c478bd9Sstevel@tonic-gate vp->v_flag |= VVMEXEC; 4077c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate VN_RELE(vp); 4107c478bd9Sstevel@tonic-gate if (dir != NULL) 4117c478bd9Sstevel@tonic-gate VN_RELE(dir); 4127c478bd9Sstevel@tonic-gate pn_free(&resolvepn); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * Allocate a new lwp directory and lwpid hash table if necessary. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) { 4187c478bd9Sstevel@tonic-gate lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP); 4197c478bd9Sstevel@tonic-gate lwpdir->ld_next = lwpdir + 1; 4206eb30ec3SRoger A. Faulkner tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP); 4217c478bd9Sstevel@tonic-gate if (p->p_lwpdir != NULL) 4227c478bd9Sstevel@tonic-gate lep = p->p_lwpdir[curthread->t_dslot].ld_entry; 4237c478bd9Sstevel@tonic-gate else 4247c478bd9Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4279acbbeafSnn35248 if (PROC_IS_BRANDED(p)) 4289acbbeafSnn35248 BROP(p)->b_exec(); 4299acbbeafSnn35248 4307c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 4317c478bd9Sstevel@tonic-gate prbarrier(p); 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Reset lwp id to the default value of 1. 4357c478bd9Sstevel@tonic-gate * This is a single-threaded process now 4367c478bd9Sstevel@tonic-gate * and lwp #1 is lwp_wait()able by default. 4377c478bd9Sstevel@tonic-gate * The t_unpark flag should not be inherited. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0); 4407c478bd9Sstevel@tonic-gate curthread->t_tid = 1; 4412cb27123Saguzovsk kpreempt_disable(); 4422cb27123Saguzovsk ASSERT(curthread->t_lpl != NULL); 4432cb27123Saguzovsk p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid; 4442cb27123Saguzovsk kpreempt_enable(); 4452cb27123Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) { 4462cb27123Saguzovsk lgrp_update_trthr_migrations(1); 4472cb27123Saguzovsk } 4487c478bd9Sstevel@tonic-gate curthread->t_unpark = 0; 4497c478bd9Sstevel@tonic-gate curthread->t_proc_flag |= TP_TWAIT; 4507c478bd9Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */ 4517c478bd9Sstevel@tonic-gate p->p_lwpdaemon = 0; /* but oh well ... */ 4527c478bd9Sstevel@tonic-gate p->p_lwpid = 1; 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * Install the newly-allocated lwp directory and lwpid hash table 4567c478bd9Sstevel@tonic-gate * and insert the current thread into the new hash table. 4577c478bd9Sstevel@tonic-gate */ 4587c478bd9Sstevel@tonic-gate if (lwpdir != NULL) { 4597c478bd9Sstevel@tonic-gate old_lwpdir = p->p_lwpdir; 4607c478bd9Sstevel@tonic-gate old_lwpdir_sz = p->p_lwpdir_sz; 4617c478bd9Sstevel@tonic-gate old_tidhash = p->p_tidhash; 4627c478bd9Sstevel@tonic-gate old_tidhash_sz = p->p_tidhash_sz; 4637c478bd9Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = lwpdir; 4647c478bd9Sstevel@tonic-gate p->p_lwpdir_sz = 2; 4657c478bd9Sstevel@tonic-gate lep->le_thread = curthread; 4667c478bd9Sstevel@tonic-gate lep->le_lwpid = curthread->t_tid; 4677c478bd9Sstevel@tonic-gate lep->le_start = curthread->t_start; 4686eb30ec3SRoger A. Faulkner lwp_hash_in(p, lep, tidhash, 2, 0); 4696eb30ec3SRoger A. Faulkner p->p_tidhash = tidhash; 4706eb30ec3SRoger A. Faulkner p->p_tidhash_sz = 2; 4717c478bd9Sstevel@tonic-gate } 4726eb30ec3SRoger A. Faulkner ret_tidhash = p->p_ret_tidhash; 4736eb30ec3SRoger A. Faulkner p->p_ret_tidhash = NULL; 4749acbbeafSnn35248 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Restore the saved signal mask and 4777c478bd9Sstevel@tonic-gate * inform /proc that the exec() has finished. 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask; 4807c478bd9Sstevel@tonic-gate prexecend(); 4817c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4827c478bd9Sstevel@tonic-gate if (old_lwpdir) { 4837c478bd9Sstevel@tonic-gate kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t)); 4846eb30ec3SRoger A. Faulkner kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t)); 4856eb30ec3SRoger A. Faulkner } 4866eb30ec3SRoger A. Faulkner while (ret_tidhash != NULL) { 4876eb30ec3SRoger A. Faulkner ret_tidhash_t *next = ret_tidhash->rth_next; 4886eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash->rth_tidhash, 4896eb30ec3SRoger A. Faulkner ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t)); 4906eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash, sizeof (*ret_tidhash)); 4916eb30ec3SRoger A. Faulkner ret_tidhash = next; 4927c478bd9Sstevel@tonic-gate } 4939acbbeafSnn35248 4947c478bd9Sstevel@tonic-gate ASSERT(error == 0); 4957c478bd9Sstevel@tonic-gate DTRACE_PROC(exec__success); 4967c478bd9Sstevel@tonic-gate return (0); 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate fail: 4997c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec__failure, int, error); 5007c478bd9Sstevel@tonic-gate out: /* error return */ 5017c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5027c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask; 5037c478bd9Sstevel@tonic-gate prexecend(); 5047c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5057c478bd9Sstevel@tonic-gate ASSERT(error != 0); 5067c478bd9Sstevel@tonic-gate return (error); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate /* 5117c478bd9Sstevel@tonic-gate * Perform generic exec duties and switchout to object-file specific 5127c478bd9Sstevel@tonic-gate * handler. 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate int 5157c478bd9Sstevel@tonic-gate gexec( 5167c478bd9Sstevel@tonic-gate struct vnode **vpp, 5177c478bd9Sstevel@tonic-gate struct execa *uap, 5187c478bd9Sstevel@tonic-gate struct uarg *args, 5197c478bd9Sstevel@tonic-gate struct intpdata *idatap, 5207c478bd9Sstevel@tonic-gate int level, 5217c478bd9Sstevel@tonic-gate long *execsz, 5227c478bd9Sstevel@tonic-gate caddr_t exec_file, 5239acbbeafSnn35248 struct cred *cred, 5249acbbeafSnn35248 int brand_action) 5257c478bd9Sstevel@tonic-gate { 5267c478bd9Sstevel@tonic-gate struct vnode *vp; 5277c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread); 5287c478bd9Sstevel@tonic-gate struct execsw *eswp; 5297c478bd9Sstevel@tonic-gate int error = 0; 5307c478bd9Sstevel@tonic-gate int suidflags = 0; 5317c478bd9Sstevel@tonic-gate ssize_t resid; 5327c478bd9Sstevel@tonic-gate uid_t uid, gid; 5337c478bd9Sstevel@tonic-gate struct vattr vattr; 5347c478bd9Sstevel@tonic-gate char magbuf[MAGIC_BYTES]; 5357c478bd9Sstevel@tonic-gate int setid; 5367c478bd9Sstevel@tonic-gate cred_t *oldcred, *newcred = NULL; 5377c478bd9Sstevel@tonic-gate int privflags = 0; 538cc4b03b5Scasper int setidfl; 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* 5417c478bd9Sstevel@tonic-gate * If the SNOCD or SUGID flag is set, turn it off and remember the 5427c478bd9Sstevel@tonic-gate * previous setting so we can restore it if we encounter an error. 5437c478bd9Sstevel@tonic-gate */ 5447c478bd9Sstevel@tonic-gate if (level == 0 && (pp->p_flag & PSUIDFLAGS)) { 5457c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 5467c478bd9Sstevel@tonic-gate suidflags = pp->p_flag & PSUIDFLAGS; 5477c478bd9Sstevel@tonic-gate pp->p_flag &= ~PSUIDFLAGS; 5487c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate if ((error = execpermissions(*vpp, &vattr, args)) != 0) 5527c478bd9Sstevel@tonic-gate goto bad; 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* need to open vnode for stateful file systems like rfs */ 555da6c28aaSamw if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0) 5567c478bd9Sstevel@tonic-gate goto bad; 5577c478bd9Sstevel@tonic-gate vp = *vpp; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Note: to support binary compatibility with SunOS a.out 5617c478bd9Sstevel@tonic-gate * executables, we read in the first four bytes, as the 5627c478bd9Sstevel@tonic-gate * magic number is in bytes 2-3. 5637c478bd9Sstevel@tonic-gate */ 5647c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf), 5657c478bd9Sstevel@tonic-gate (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) 5667c478bd9Sstevel@tonic-gate goto bad; 5677c478bd9Sstevel@tonic-gate if (resid != 0) 5687c478bd9Sstevel@tonic-gate goto bad; 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate if ((eswp = findexec_by_hdr(magbuf)) == NULL) 5717c478bd9Sstevel@tonic-gate goto bad; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate if (level == 0 && 5747c478bd9Sstevel@tonic-gate (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) { 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate newcred = cred = crdup(cred); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* If we can, drop the PA bit */ 5797c478bd9Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0) 5807c478bd9Sstevel@tonic-gate priv_adjust_PA(cred); 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate if (privflags & PRIV_SETID) { 5837c478bd9Sstevel@tonic-gate cred->cr_uid = uid; 5847c478bd9Sstevel@tonic-gate cred->cr_gid = gid; 5857c478bd9Sstevel@tonic-gate cred->cr_suid = uid; 5867c478bd9Sstevel@tonic-gate cred->cr_sgid = gid; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate 58945916cd2Sjpk if (privflags & MAC_FLAGS) { 59045916cd2Sjpk if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT)) 59145916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE; 59245916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT; 59345916cd2Sjpk } 59445916cd2Sjpk 5957c478bd9Sstevel@tonic-gate /* 5967c478bd9Sstevel@tonic-gate * Implement the privilege updates: 5977c478bd9Sstevel@tonic-gate * 5987c478bd9Sstevel@tonic-gate * Restrict with L: 5997c478bd9Sstevel@tonic-gate * 6007c478bd9Sstevel@tonic-gate * I' = I & L 6017c478bd9Sstevel@tonic-gate * 6027c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A 6037c478bd9Sstevel@tonic-gate * 6047c478bd9Sstevel@tonic-gate * But if running under ptrace, we cap I with P. 6057c478bd9Sstevel@tonic-gate */ 6067c478bd9Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0) { 6077c478bd9Sstevel@tonic-gate if ((privflags & PRIV_INCREASE) != 0 && 6087c478bd9Sstevel@tonic-gate (pp->p_proc_flag & P_PR_PTRACE) != 0) 6097c478bd9Sstevel@tonic-gate priv_intersect(&CR_OPPRIV(cred), 6107c478bd9Sstevel@tonic-gate &CR_IPRIV(cred)); 6117c478bd9Sstevel@tonic-gate priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred)); 6127c478bd9Sstevel@tonic-gate CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred); 6137c478bd9Sstevel@tonic-gate priv_adjust_PA(cred); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* SunOS 4.x buy-back */ 6187c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) && 6197c478bd9Sstevel@tonic-gate (vattr.va_mode & (VSUID|VSGID))) { 62018c39a06SJason King char path[MAXNAMELEN]; 62118c39a06SJason King refstr_t *mntpt = NULL; 62218c39a06SJason King int ret = -1; 62318c39a06SJason King 62418c39a06SJason King bzero(path, sizeof (path)); 62518c39a06SJason King zone_hold(pp->p_zone); 62618c39a06SJason King 62718c39a06SJason King ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path, 62818c39a06SJason King sizeof (path), cred); 62918c39a06SJason King 63018c39a06SJason King /* fallback to mountpoint if a path can't be found */ 63118c39a06SJason King if ((ret != 0) || (ret == 0 && path[0] == '\0')) 63218c39a06SJason King mntpt = vfs_getmntpoint(vp->v_vfsp); 63318c39a06SJason King 63418c39a06SJason King if (mntpt == NULL) 63518c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE, 63618c39a06SJason King "!uid %d: setuid execution not allowed, " 63718c39a06SJason King "file=%s", cred->cr_uid, path); 63818c39a06SJason King else 63918c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE, 64018c39a06SJason King "!uid %d: setuid execution not allowed, " 64118c39a06SJason King "fs=%s, file=%s", cred->cr_uid, 64218c39a06SJason King ZONE_PATH_TRANSLATE(refstr_value(mntpt), 64318c39a06SJason King pp->p_zone), exec_file); 64418c39a06SJason King 64518c39a06SJason King if (!INGLOBALZONE(pp)) { 64618c39a06SJason King /* zone_rootpath always has trailing / */ 64718c39a06SJason King if (mntpt == NULL) 64818c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d " 64918c39a06SJason King "setuid execution not allowed, file=%s%s", 65018c39a06SJason King pp->p_zone->zone_name, cred->cr_uid, 65118c39a06SJason King pp->p_zone->zone_rootpath, path + 1); 65218c39a06SJason King else 65318c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d " 65418c39a06SJason King "setuid execution not allowed, fs=%s, " 65518c39a06SJason King "file=%s", pp->p_zone->zone_name, 65618c39a06SJason King cred->cr_uid, refstr_value(mntpt), 65718c39a06SJason King exec_file); 65818c39a06SJason King } 65918c39a06SJason King 66018c39a06SJason King if (mntpt != NULL) 66118c39a06SJason King refstr_rele(mntpt); 66218c39a06SJason King 66318c39a06SJason King zone_rele(pp->p_zone); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * execsetid() told us whether or not we had to change the 6687c478bd9Sstevel@tonic-gate * credentials of the process. In privflags, it told us 6697c478bd9Sstevel@tonic-gate * whether we gained any privileges or executed a set-uid executable. 6707c478bd9Sstevel@tonic-gate */ 6717c478bd9Sstevel@tonic-gate setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE)); 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate /* 6747c478bd9Sstevel@tonic-gate * Use /etc/system variable to determine if the stack 6757c478bd9Sstevel@tonic-gate * should be marked as executable by default. 6767c478bd9Sstevel@tonic-gate */ 6777c478bd9Sstevel@tonic-gate if (noexec_user_stack) 6787c478bd9Sstevel@tonic-gate args->stk_prot &= ~PROT_EXEC; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */ 68105d3dc4bSpaulsan args->ex_vp = vp; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * Traditionally, the setid flags told the sub processes whether 6857c478bd9Sstevel@tonic-gate * the file just executed was set-uid or set-gid; this caused 6867c478bd9Sstevel@tonic-gate * some confusion as the 'setid' flag did not match the SUGID 6877c478bd9Sstevel@tonic-gate * process flag which is only set when the uids/gids do not match. 6887c478bd9Sstevel@tonic-gate * A script set-gid/set-uid to the real uid/gid would start with 6897c478bd9Sstevel@tonic-gate * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH. 6907c478bd9Sstevel@tonic-gate * Now we flag those cases where the calling process cannot 6917c478bd9Sstevel@tonic-gate * be trusted to influence the newly exec'ed process, either 6927c478bd9Sstevel@tonic-gate * because it runs with more privileges or when the uids/gids 6937c478bd9Sstevel@tonic-gate * do in fact not match. 6947c478bd9Sstevel@tonic-gate * This also makes the runtime linker agree with the on exec 6957c478bd9Sstevel@tonic-gate * values of SNOCD and SUGID. 6967c478bd9Sstevel@tonic-gate */ 697cc4b03b5Scasper setidfl = 0; 698cc4b03b5Scasper if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid && 699cc4b03b5Scasper !supgroupmember(cred->cr_gid, cred))) { 700cc4b03b5Scasper setidfl |= EXECSETID_UGIDS; 701cc4b03b5Scasper } 702cc4b03b5Scasper if (setid & PRIV_SETUGID) 703cc4b03b5Scasper setidfl |= EXECSETID_SETID; 704cc4b03b5Scasper if (setid & PRIV_INCREASE) 705cc4b03b5Scasper setidfl |= EXECSETID_PRIVS; 706cc4b03b5Scasper 7077c478bd9Sstevel@tonic-gate error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz, 7089acbbeafSnn35248 setidfl, exec_file, cred, brand_action); 7097c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock); 7107c478bd9Sstevel@tonic-gate if (error != 0) { 7117c478bd9Sstevel@tonic-gate if (newcred != NULL) 7127c478bd9Sstevel@tonic-gate crfree(newcred); 7137c478bd9Sstevel@tonic-gate goto bad; 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate if (level == 0) { 7177c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock); 7187c478bd9Sstevel@tonic-gate if (newcred != NULL) { 7197c478bd9Sstevel@tonic-gate /* 7207c478bd9Sstevel@tonic-gate * Free the old credentials, and set the new ones. 7217c478bd9Sstevel@tonic-gate * Do this for both the process and the (single) thread. 7227c478bd9Sstevel@tonic-gate */ 7237c478bd9Sstevel@tonic-gate crfree(pp->p_cred); 7247c478bd9Sstevel@tonic-gate pp->p_cred = cred; /* cred already held for proc */ 7257c478bd9Sstevel@tonic-gate crhold(cred); /* hold new cred for thread */ 7267c478bd9Sstevel@tonic-gate /* 7277c478bd9Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred 7287c478bd9Sstevel@tonic-gate * must always be either NULL, or point to a valid, 7297c478bd9Sstevel@tonic-gate * allocated cred structure. 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate oldcred = curthread->t_cred; 7327c478bd9Sstevel@tonic-gate curthread->t_cred = cred; 7337c478bd9Sstevel@tonic-gate crfree(oldcred); 734634e26ecSCasper H.S. Dik 735*d93c0b4cSCasper H.S. Dik if (priv_basic_test >= 0 && 736634e26ecSCasper H.S. Dik !PRIV_ISASSERT(&CR_IPRIV(newcred), 737634e26ecSCasper H.S. Dik priv_basic_test)) { 738634e26ecSCasper H.S. Dik pid_t pid = pp->p_pid; 739634e26ecSCasper H.S. Dik char *fn = PTOU(pp)->u_comm; 740634e26ecSCasper H.S. Dik 741634e26ecSCasper H.S. Dik cmn_err(CE_WARN, "%s[%d]: exec: basic_test " 742634e26ecSCasper H.S. Dik "privilege removed from E/I", fn, pid); 743634e26ecSCasper H.S. Dik } 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate /* 7467c478bd9Sstevel@tonic-gate * On emerging from a successful exec(), the saved 7477c478bd9Sstevel@tonic-gate * uid and gid equal the effective uid and gid. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate cred->cr_suid = cred->cr_uid; 7507c478bd9Sstevel@tonic-gate cred->cr_sgid = cred->cr_gid; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * If the real and effective ids do not match, this 7547c478bd9Sstevel@tonic-gate * is a setuid process that should not dump core. 7557c478bd9Sstevel@tonic-gate * The group comparison is tricky; we prevent the code 7567c478bd9Sstevel@tonic-gate * from flagging SNOCD when executing with an effective gid 7577c478bd9Sstevel@tonic-gate * which is a supplementary group. 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate if (cred->cr_ruid != cred->cr_uid || 7607c478bd9Sstevel@tonic-gate (cred->cr_rgid != cred->cr_gid && 7617c478bd9Sstevel@tonic-gate !supgroupmember(cred->cr_gid, cred)) || 7627c478bd9Sstevel@tonic-gate (privflags & PRIV_INCREASE) != 0) 7637c478bd9Sstevel@tonic-gate suidflags = PSUIDFLAGS; 7647c478bd9Sstevel@tonic-gate else 7657c478bd9Sstevel@tonic-gate suidflags = 0; 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock); 7687c478bd9Sstevel@tonic-gate if (suidflags) { 7697c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 7707c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags; 7717c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) { 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate * If process is traced via /proc, arrange to 7767c478bd9Sstevel@tonic-gate * invalidate the associated /proc vnode. 7777c478bd9Sstevel@tonic-gate */ 7787c478bd9Sstevel@tonic-gate if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE)) 7797c478bd9Sstevel@tonic-gate args->traceinval = 1; 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate if (pp->p_proc_flag & P_PR_PTRACE) 7827c478bd9Sstevel@tonic-gate psignal(pp, SIGTRAP); 7837c478bd9Sstevel@tonic-gate if (args->traceinval) 7847c478bd9Sstevel@tonic-gate prinvalidate(&pp->p_user); 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate return (0); 7887c478bd9Sstevel@tonic-gate bad: 7897c478bd9Sstevel@tonic-gate if (error == 0) 7907c478bd9Sstevel@tonic-gate error = ENOEXEC; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate if (suidflags) { 7937c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 7947c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags; 7957c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 7967c478bd9Sstevel@tonic-gate } 7977c478bd9Sstevel@tonic-gate return (error); 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate extern char *execswnames[]; 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate struct execsw * 8037c478bd9Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size) 8047c478bd9Sstevel@tonic-gate { 8057c478bd9Sstevel@tonic-gate int i, j; 8067c478bd9Sstevel@tonic-gate char *ename; 8077c478bd9Sstevel@tonic-gate char *magicp; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate mutex_enter(&execsw_lock); 8107c478bd9Sstevel@tonic-gate for (i = 0; i < nexectype; i++) { 8117c478bd9Sstevel@tonic-gate if (execswnames[i] == NULL) { 8127c478bd9Sstevel@tonic-gate ename = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8137c478bd9Sstevel@tonic-gate (void) strcpy(ename, name); 8147c478bd9Sstevel@tonic-gate execswnames[i] = ename; 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * Set the magic number last so that we 8177c478bd9Sstevel@tonic-gate * don't need to hold the execsw_lock in 8187c478bd9Sstevel@tonic-gate * findexectype(). 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate magicp = kmem_alloc(magic_size, KM_SLEEP); 8217c478bd9Sstevel@tonic-gate for (j = 0; j < magic_size; j++) 8227c478bd9Sstevel@tonic-gate magicp[j] = magic[j]; 8237c478bd9Sstevel@tonic-gate execsw[i].exec_magic = magicp; 8247c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock); 8257c478bd9Sstevel@tonic-gate return (&execsw[i]); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock); 8297c478bd9Sstevel@tonic-gate return (NULL); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * Find the exec switch table entry with the corresponding magic string. 8347c478bd9Sstevel@tonic-gate */ 8357c478bd9Sstevel@tonic-gate struct execsw * 8367c478bd9Sstevel@tonic-gate findexecsw(char *magic) 8377c478bd9Sstevel@tonic-gate { 8387c478bd9Sstevel@tonic-gate struct execsw *eswp; 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 8417c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 8427c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 && 8437c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) 8447c478bd9Sstevel@tonic-gate return (eswp); 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate return (NULL); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate /* 8507c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given exec header string by looking for the 8517c478bd9Sstevel@tonic-gate * magic string at a specified offset and length for each kind of executable 8527c478bd9Sstevel@tonic-gate * file format until one matches. If no execsw[] entry is found, try to 8537c478bd9Sstevel@tonic-gate * autoload a module for this magic string. 8547c478bd9Sstevel@tonic-gate */ 8557c478bd9Sstevel@tonic-gate struct execsw * 8567c478bd9Sstevel@tonic-gate findexec_by_hdr(char *header) 8577c478bd9Sstevel@tonic-gate { 8587c478bd9Sstevel@tonic-gate struct execsw *eswp; 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 8617c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 8627c478bd9Sstevel@tonic-gate if (header && eswp->exec_maglen != 0 && 8637c478bd9Sstevel@tonic-gate bcmp(&header[eswp->exec_magoff], eswp->exec_magic, 8647c478bd9Sstevel@tonic-gate eswp->exec_maglen) == 0) { 8657c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0) 8667c478bd9Sstevel@tonic-gate return (NULL); 8677c478bd9Sstevel@tonic-gate return (eswp); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */ 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate /* 8747c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given magic string. If no execsw[] entry 8757c478bd9Sstevel@tonic-gate * is found, try to autoload a module for this magic string. 8767c478bd9Sstevel@tonic-gate */ 8777c478bd9Sstevel@tonic-gate struct execsw * 8787c478bd9Sstevel@tonic-gate findexec_by_magic(char *magic) 8797c478bd9Sstevel@tonic-gate { 8807c478bd9Sstevel@tonic-gate struct execsw *eswp; 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) { 8837c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES); 8847c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 && 8857c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) { 8867c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0) 8877c478bd9Sstevel@tonic-gate return (NULL); 8887c478bd9Sstevel@tonic-gate return (eswp); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */ 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate static int 8957c478bd9Sstevel@tonic-gate hold_execsw(struct execsw *eswp) 8967c478bd9Sstevel@tonic-gate { 8977c478bd9Sstevel@tonic-gate char *name; 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER); 9007c478bd9Sstevel@tonic-gate while (!LOADED_EXEC(eswp)) { 9017c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock); 9027c478bd9Sstevel@tonic-gate name = execswnames[eswp-execsw]; 9037c478bd9Sstevel@tonic-gate ASSERT(name); 9047c478bd9Sstevel@tonic-gate if (modload("exec", name) == -1) 9057c478bd9Sstevel@tonic-gate return (-1); 9067c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate return (0); 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate static int 9127c478bd9Sstevel@tonic-gate execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp) 9137c478bd9Sstevel@tonic-gate { 9147c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread); 9157c478bd9Sstevel@tonic-gate uid_t uid, gid; 9167c478bd9Sstevel@tonic-gate cred_t *cr = pp->p_cred; 9177c478bd9Sstevel@tonic-gate int privflags = 0; 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate /* 9207c478bd9Sstevel@tonic-gate * Remember credentials. 9217c478bd9Sstevel@tonic-gate */ 9227c478bd9Sstevel@tonic-gate uid = cr->cr_uid; 9237c478bd9Sstevel@tonic-gate gid = cr->cr_gid; 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* Will try to reset the PRIV_AWARE bit later. */ 9267c478bd9Sstevel@tonic-gate if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE) 9277c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET; 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) { 9307c478bd9Sstevel@tonic-gate /* 9317c478bd9Sstevel@tonic-gate * Set-uid root execution only allowed if the limit set 9327c478bd9Sstevel@tonic-gate * holds all unsafe privileges. 9337c478bd9Sstevel@tonic-gate */ 9347c478bd9Sstevel@tonic-gate if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 || 9357c478bd9Sstevel@tonic-gate priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) { 9367c478bd9Sstevel@tonic-gate uid = vattrp->va_uid; 9377c478bd9Sstevel@tonic-gate privflags |= PRIV_SETUGID; 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate if (vattrp->va_mode & VSGID) { 9407c478bd9Sstevel@tonic-gate gid = vattrp->va_gid; 9417c478bd9Sstevel@tonic-gate privflags |= PRIV_SETUGID; 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate /* 9467c478bd9Sstevel@tonic-gate * Do we need to change our credential anyway? 9477c478bd9Sstevel@tonic-gate * This is the case when E != I or P != I, as 9487c478bd9Sstevel@tonic-gate * we need to do the assignments (with F empty and A full) 9497c478bd9Sstevel@tonic-gate * Or when I is not a subset of L; in that case we need to 9507c478bd9Sstevel@tonic-gate * enforce L. 9517c478bd9Sstevel@tonic-gate * 9527c478bd9Sstevel@tonic-gate * I' = L & I 9537c478bd9Sstevel@tonic-gate * 9547c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A 9557c478bd9Sstevel@tonic-gate * or 9567c478bd9Sstevel@tonic-gate * E' = P' = I' 9577c478bd9Sstevel@tonic-gate */ 9587c478bd9Sstevel@tonic-gate if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) || 9597c478bd9Sstevel@tonic-gate !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) || 9607c478bd9Sstevel@tonic-gate !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr))) 9617c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET; 9627c478bd9Sstevel@tonic-gate 96345916cd2Sjpk /* If MAC-aware flag(s) are on, need to update cred to remove. */ 96445916cd2Sjpk if ((CR_FLAGS(cr) & NET_MAC_AWARE) || 96545916cd2Sjpk (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT)) 96645916cd2Sjpk privflags |= MAC_FLAGS; 96745916cd2Sjpk 9687c478bd9Sstevel@tonic-gate /* 9697c478bd9Sstevel@tonic-gate * When we introduce the "forced" set then we will need 9707c478bd9Sstevel@tonic-gate * to set PRIV_INCREASE here if I not a subset of P. 9717c478bd9Sstevel@tonic-gate * If the "allowed" set is introduced we will need to do 9727c478bd9Sstevel@tonic-gate * a similar thing; however, it seems more reasonable to 9737c478bd9Sstevel@tonic-gate * have the allowed set reduce "L": script language interpreters 9747c478bd9Sstevel@tonic-gate * would typically have an allowed set of "all". 9757c478bd9Sstevel@tonic-gate */ 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate /* 9787c478bd9Sstevel@tonic-gate * Set setuid/setgid protections if no ptrace() compatibility. 9797c478bd9Sstevel@tonic-gate * For privileged processes, honor setuid/setgid even in 9807c478bd9Sstevel@tonic-gate * the presence of ptrace() compatibility. 9817c478bd9Sstevel@tonic-gate */ 9827c478bd9Sstevel@tonic-gate if (((pp->p_proc_flag & P_PR_PTRACE) == 0 || 9837c478bd9Sstevel@tonic-gate PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) && 9847c478bd9Sstevel@tonic-gate (cr->cr_uid != uid || 9857c478bd9Sstevel@tonic-gate cr->cr_gid != gid || 9867c478bd9Sstevel@tonic-gate cr->cr_suid != uid || 9877c478bd9Sstevel@tonic-gate cr->cr_sgid != gid)) { 9887c478bd9Sstevel@tonic-gate *uidp = uid; 9897c478bd9Sstevel@tonic-gate *gidp = gid; 9907c478bd9Sstevel@tonic-gate privflags |= PRIV_SETID; 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate return (privflags); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate int 9967c478bd9Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args) 9977c478bd9Sstevel@tonic-gate { 9987c478bd9Sstevel@tonic-gate int error; 9997c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE; 1002da6c28aaSamw if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL)) 10037c478bd9Sstevel@tonic-gate return (error); 10047c478bd9Sstevel@tonic-gate /* 10057c478bd9Sstevel@tonic-gate * Check the access mode. 10067c478bd9Sstevel@tonic-gate * If VPROC, ask /proc if the file is an object file. 10077c478bd9Sstevel@tonic-gate */ 1008da6c28aaSamw if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 || 10097c478bd9Sstevel@tonic-gate !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) || 10107c478bd9Sstevel@tonic-gate (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 || 10117c478bd9Sstevel@tonic-gate (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) { 10127c478bd9Sstevel@tonic-gate if (error == 0) 10137c478bd9Sstevel@tonic-gate error = EACCES; 10147c478bd9Sstevel@tonic-gate return (error); 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) && 1018da6c28aaSamw (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) { 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * If process is under ptrace(2) compatibility, 10217c478bd9Sstevel@tonic-gate * fail the exec(2). 10227c478bd9Sstevel@tonic-gate */ 10237c478bd9Sstevel@tonic-gate if (p->p_proc_flag & P_PR_PTRACE) 10247c478bd9Sstevel@tonic-gate goto bad; 10257c478bd9Sstevel@tonic-gate /* 10267c478bd9Sstevel@tonic-gate * Process is traced via /proc. 10277c478bd9Sstevel@tonic-gate * Arrange to invalidate the /proc vnode. 10287c478bd9Sstevel@tonic-gate */ 10297c478bd9Sstevel@tonic-gate args->traceinval = 1; 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate return (0); 10327c478bd9Sstevel@tonic-gate bad: 10337c478bd9Sstevel@tonic-gate if (error == 0) 10347c478bd9Sstevel@tonic-gate error = ENOEXEC; 10357c478bd9Sstevel@tonic-gate return (error); 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate 10387c478bd9Sstevel@tonic-gate /* 10397c478bd9Sstevel@tonic-gate * Map a section of an executable file into the user's 10407c478bd9Sstevel@tonic-gate * address space. 10417c478bd9Sstevel@tonic-gate */ 10427c478bd9Sstevel@tonic-gate int 10437c478bd9Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen, 10447c478bd9Sstevel@tonic-gate off_t offset, int prot, int page, uint_t szc) 10457c478bd9Sstevel@tonic-gate { 10467c478bd9Sstevel@tonic-gate int error = 0; 10477c478bd9Sstevel@tonic-gate off_t oldoffset; 10487c478bd9Sstevel@tonic-gate caddr_t zfodbase, oldaddr; 10497c478bd9Sstevel@tonic-gate size_t end, oldlen; 10507c478bd9Sstevel@tonic-gate size_t zfoddiff; 10517c478bd9Sstevel@tonic-gate label_t ljb; 10527c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate oldaddr = addr; 10557c478bd9Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 10567c478bd9Sstevel@tonic-gate if (len) { 10577c478bd9Sstevel@tonic-gate oldlen = len; 10587c478bd9Sstevel@tonic-gate len += ((size_t)oldaddr - (size_t)addr); 10597c478bd9Sstevel@tonic-gate oldoffset = offset; 10607c478bd9Sstevel@tonic-gate offset = (off_t)((uintptr_t)offset & PAGEMASK); 10617c478bd9Sstevel@tonic-gate if (page) { 10627c478bd9Sstevel@tonic-gate spgcnt_t prefltmem, availm, npages; 10637c478bd9Sstevel@tonic-gate int preread; 10647c478bd9Sstevel@tonic-gate uint_t mflag = MAP_PRIVATE | MAP_FIXED; 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) { 10677c478bd9Sstevel@tonic-gate mflag |= MAP_TEXT; 10687c478bd9Sstevel@tonic-gate } else { 10697c478bd9Sstevel@tonic-gate mflag |= MAP_INITDATA; 10707c478bd9Sstevel@tonic-gate } 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as, 10737c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 10747c478bd9Sstevel@tonic-gate error = ENOMEM; 10757c478bd9Sstevel@tonic-gate goto bad; 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate if (error = VOP_MAP(vp, (offset_t)offset, 10787c478bd9Sstevel@tonic-gate p->p_as, &addr, len, prot, PROT_ALL, 1079da6c28aaSamw mflag, CRED(), NULL)) 10807c478bd9Sstevel@tonic-gate goto bad; 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate /* 10837c478bd9Sstevel@tonic-gate * If the segment can fit, then we prefault 10847c478bd9Sstevel@tonic-gate * the entire segment in. This is based on the 10857c478bd9Sstevel@tonic-gate * model that says the best working set of a 10867c478bd9Sstevel@tonic-gate * small program is all of its pages. 10877c478bd9Sstevel@tonic-gate */ 10887c478bd9Sstevel@tonic-gate npages = (spgcnt_t)btopr(len); 10897c478bd9Sstevel@tonic-gate prefltmem = freemem - desfree; 10907c478bd9Sstevel@tonic-gate preread = 10917c478bd9Sstevel@tonic-gate (npages < prefltmem && len < PGTHRESH) ? 1 : 0; 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate /* 10947c478bd9Sstevel@tonic-gate * If we aren't prefaulting the segment, 10957c478bd9Sstevel@tonic-gate * increment "deficit", if necessary to ensure 10967c478bd9Sstevel@tonic-gate * that pages will become available when this 10977c478bd9Sstevel@tonic-gate * process starts executing. 10987c478bd9Sstevel@tonic-gate */ 10997c478bd9Sstevel@tonic-gate availm = freemem - lotsfree; 11007c478bd9Sstevel@tonic-gate if (preread == 0 && npages > availm && 11017c478bd9Sstevel@tonic-gate deficit < lotsfree) { 11027c478bd9Sstevel@tonic-gate deficit += MIN((pgcnt_t)(npages - availm), 11037c478bd9Sstevel@tonic-gate lotsfree - deficit); 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate if (preread) { 11077c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD, 11087c478bd9Sstevel@tonic-gate "execmap preread:freemem %d size %lu", 11097c478bd9Sstevel@tonic-gate freemem, len); 11107c478bd9Sstevel@tonic-gate (void) as_fault(p->p_as->a_hat, p->p_as, 11117c478bd9Sstevel@tonic-gate (caddr_t)addr, len, F_INVAL, S_READ); 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate } else { 11147c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as, 11157c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 11167c478bd9Sstevel@tonic-gate error = ENOMEM; 11177c478bd9Sstevel@tonic-gate goto bad; 11187c478bd9Sstevel@tonic-gate } 11197c478bd9Sstevel@tonic-gate 11207c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, addr, len, 11217c478bd9Sstevel@tonic-gate segvn_create, zfod_argsp)) 11227c478bd9Sstevel@tonic-gate goto bad; 11237c478bd9Sstevel@tonic-gate /* 11247c478bd9Sstevel@tonic-gate * Read in the segment in one big chunk. 11257c478bd9Sstevel@tonic-gate */ 11267c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr, 11277c478bd9Sstevel@tonic-gate oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0, 11287c478bd9Sstevel@tonic-gate (rlim64_t)0, CRED(), (ssize_t *)0)) 11297c478bd9Sstevel@tonic-gate goto bad; 11307c478bd9Sstevel@tonic-gate /* 11317c478bd9Sstevel@tonic-gate * Now set protections. 11327c478bd9Sstevel@tonic-gate */ 11337c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) { 11347c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)addr, 11357c478bd9Sstevel@tonic-gate len, prot); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate } 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate if (zfodlen) { 11419acbbeafSnn35248 struct as *as = curproc->p_as; 11429acbbeafSnn35248 struct seg *seg; 11439acbbeafSnn35248 uint_t zprot = 0; 11449acbbeafSnn35248 11457c478bd9Sstevel@tonic-gate end = (size_t)addr + len; 11467c478bd9Sstevel@tonic-gate zfodbase = (caddr_t)roundup(end, PAGESIZE); 11477c478bd9Sstevel@tonic-gate zfoddiff = (uintptr_t)zfodbase - end; 11487c478bd9Sstevel@tonic-gate if (zfoddiff) { 11499acbbeafSnn35248 /* 11509acbbeafSnn35248 * Before we go to zero the remaining space on the last 11519acbbeafSnn35248 * page, make sure we have write permission. 11529acbbeafSnn35248 */ 11539acbbeafSnn35248 11549acbbeafSnn35248 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 11559acbbeafSnn35248 seg = as_segat(curproc->p_as, (caddr_t)end); 11569acbbeafSnn35248 if (seg != NULL) 11579acbbeafSnn35248 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1, 11589acbbeafSnn35248 &zprot); 11599acbbeafSnn35248 AS_LOCK_EXIT(as, &as->a_lock); 11609acbbeafSnn35248 11619acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) { 11629acbbeafSnn35248 (void) as_setprot(as, (caddr_t)end, 11639acbbeafSnn35248 zfoddiff - 1, zprot | PROT_WRITE); 11649acbbeafSnn35248 } 11659acbbeafSnn35248 11667c478bd9Sstevel@tonic-gate if (on_fault(&ljb)) { 11677c478bd9Sstevel@tonic-gate no_fault(); 11689acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) 11699acbbeafSnn35248 (void) as_setprot(as, (caddr_t)end, 11709acbbeafSnn35248 zfoddiff - 1, zprot); 11717c478bd9Sstevel@tonic-gate error = EFAULT; 11727c478bd9Sstevel@tonic-gate goto bad; 11737c478bd9Sstevel@tonic-gate } 11747c478bd9Sstevel@tonic-gate uzero((void *)end, zfoddiff); 11757c478bd9Sstevel@tonic-gate no_fault(); 11769acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) 11779acbbeafSnn35248 (void) as_setprot(as, (caddr_t)end, 11789acbbeafSnn35248 zfoddiff - 1, zprot); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate if (zfodlen > zfoddiff) { 11817c478bd9Sstevel@tonic-gate struct segvn_crargs crargs = 11827c478bd9Sstevel@tonic-gate SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL); 11837c478bd9Sstevel@tonic-gate 11847c478bd9Sstevel@tonic-gate zfodlen -= zfoddiff; 11857c478bd9Sstevel@tonic-gate if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as, 11867c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) { 11877c478bd9Sstevel@tonic-gate error = ENOMEM; 11887c478bd9Sstevel@tonic-gate goto bad; 11897c478bd9Sstevel@tonic-gate } 1190ec25b48fSsusans if (szc > 0) { 1191ec25b48fSsusans /* 1192ec25b48fSsusans * ASSERT alignment because the mapelfexec() 1193ec25b48fSsusans * caller for the szc > 0 case extended zfod 1194ec25b48fSsusans * so it's end is pgsz aligned. 1195ec25b48fSsusans */ 1196ec25b48fSsusans size_t pgsz = page_get_pagesize(szc); 1197ec25b48fSsusans ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz)); 1198ec25b48fSsusans 1199ec25b48fSsusans if (IS_P2ALIGNED(zfodbase, pgsz)) { 12007c478bd9Sstevel@tonic-gate crargs.szc = szc; 1201ec25b48fSsusans } else { 1202ec25b48fSsusans crargs.szc = AS_MAP_HEAP; 1203ec25b48fSsusans } 1204ec25b48fSsusans } else { 1205ec25b48fSsusans crargs.szc = AS_MAP_NO_LPOOB; 1206ec25b48fSsusans } 12077c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, (caddr_t)zfodbase, 12087c478bd9Sstevel@tonic-gate zfodlen, segvn_create, &crargs)) 12097c478bd9Sstevel@tonic-gate goto bad; 12107c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) { 12117c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)zfodbase, 12127c478bd9Sstevel@tonic-gate zfodlen, prot); 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate } 12157c478bd9Sstevel@tonic-gate } 12167c478bd9Sstevel@tonic-gate return (0); 12177c478bd9Sstevel@tonic-gate bad: 12187c478bd9Sstevel@tonic-gate return (error); 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate void 12227c478bd9Sstevel@tonic-gate setexecenv(struct execenv *ep) 12237c478bd9Sstevel@tonic-gate { 12247c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 12257c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 12267c478bd9Sstevel@tonic-gate struct vnode *vp; 12277c478bd9Sstevel@tonic-gate 12287c478bd9Sstevel@tonic-gate p->p_bssbase = ep->ex_bssbase; 12297c478bd9Sstevel@tonic-gate p->p_brkbase = ep->ex_brkbase; 12307c478bd9Sstevel@tonic-gate p->p_brksize = ep->ex_brksize; 12317c478bd9Sstevel@tonic-gate if (p->p_exec) 12327c478bd9Sstevel@tonic-gate VN_RELE(p->p_exec); /* out with the old */ 12337c478bd9Sstevel@tonic-gate vp = p->p_exec = ep->ex_vp; 12347c478bd9Sstevel@tonic-gate if (vp != NULL) 12357c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* in with the new */ 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0; 12387c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0; 12397c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 12407c478bd9Sstevel@tonic-gate } 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate int 12437c478bd9Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp) 12447c478bd9Sstevel@tonic-gate { 12457c478bd9Sstevel@tonic-gate struct vnode *vp = *vpp; 12467c478bd9Sstevel@tonic-gate file_t *fp; 12477c478bd9Sstevel@tonic-gate int error = 0; 12487c478bd9Sstevel@tonic-gate int filemode = FREAD; 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* open reference */ 12517c478bd9Sstevel@tonic-gate if (error = falloc(NULL, filemode, &fp, fdp)) { 12527c478bd9Sstevel@tonic-gate VN_RELE(vp); 12537c478bd9Sstevel@tonic-gate *fdp = -1; /* just in case falloc changed value */ 12547c478bd9Sstevel@tonic-gate return (error); 12557c478bd9Sstevel@tonic-gate } 1256da6c28aaSamw if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) { 12577c478bd9Sstevel@tonic-gate VN_RELE(vp); 12587c478bd9Sstevel@tonic-gate setf(*fdp, NULL); 12597c478bd9Sstevel@tonic-gate unfalloc(fp); 12607c478bd9Sstevel@tonic-gate *fdp = -1; 12617c478bd9Sstevel@tonic-gate return (error); 12627c478bd9Sstevel@tonic-gate } 12637c478bd9Sstevel@tonic-gate *vpp = vp; /* vnode should not have changed */ 12647c478bd9Sstevel@tonic-gate fp->f_vnode = vp; 12657c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 12667c478bd9Sstevel@tonic-gate setf(*fdp, fp); 12677c478bd9Sstevel@tonic-gate return (0); 12687c478bd9Sstevel@tonic-gate } 12697c478bd9Sstevel@tonic-gate 12707c478bd9Sstevel@tonic-gate int 12717c478bd9Sstevel@tonic-gate execclose(int fd) 12727c478bd9Sstevel@tonic-gate { 12737c478bd9Sstevel@tonic-gate return (closeandsetf(fd, NULL)); 12747c478bd9Sstevel@tonic-gate } 12757c478bd9Sstevel@tonic-gate 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate /* 12787c478bd9Sstevel@tonic-gate * noexec stub function. 12797c478bd9Sstevel@tonic-gate */ 12807c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12817c478bd9Sstevel@tonic-gate int 12827c478bd9Sstevel@tonic-gate noexec( 12837c478bd9Sstevel@tonic-gate struct vnode *vp, 12847c478bd9Sstevel@tonic-gate struct execa *uap, 12857c478bd9Sstevel@tonic-gate struct uarg *args, 12867c478bd9Sstevel@tonic-gate struct intpdata *idatap, 12877c478bd9Sstevel@tonic-gate int level, 12887c478bd9Sstevel@tonic-gate long *execsz, 12897c478bd9Sstevel@tonic-gate int setid, 12907c478bd9Sstevel@tonic-gate caddr_t exec_file, 12917c478bd9Sstevel@tonic-gate struct cred *cred) 12927c478bd9Sstevel@tonic-gate { 12937c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "missing exec capability for %s", uap->fname); 12947c478bd9Sstevel@tonic-gate return (ENOEXEC); 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate /* 12987c478bd9Sstevel@tonic-gate * Support routines for building a user stack. 12997c478bd9Sstevel@tonic-gate * 13007c478bd9Sstevel@tonic-gate * execve(path, argv, envp) must construct a new stack with the specified 13017c478bd9Sstevel@tonic-gate * arguments and environment variables (see exec_args() for a description 13027c478bd9Sstevel@tonic-gate * of the user stack layout). To do this, we copy the arguments and 13037c478bd9Sstevel@tonic-gate * environment variables from the old user address space into the kernel, 13047c478bd9Sstevel@tonic-gate * free the old as, create the new as, and copy our buffered information 13057c478bd9Sstevel@tonic-gate * to the new stack. Our kernel buffer has the following structure: 13067c478bd9Sstevel@tonic-gate * 13077c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size 13087c478bd9Sstevel@tonic-gate * | string offsets | 13097c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp 13107c478bd9Sstevel@tonic-gate * | | 13117c478bd9Sstevel@tonic-gate * | STK_AVAIL() space | 13127c478bd9Sstevel@tonic-gate * | | 13137c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp 13147c478bd9Sstevel@tonic-gate * | strings | 13157c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base 13167c478bd9Sstevel@tonic-gate * 13177c478bd9Sstevel@tonic-gate * When we add a string, we store the string's contents (including the null 13187c478bd9Sstevel@tonic-gate * terminator) at stk_strp, and we store the offset of the string relative to 13197c478bd9Sstevel@tonic-gate * stk_base at --stk_offp. At strings are added, stk_strp increases and 13207c478bd9Sstevel@tonic-gate * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just 13217c478bd9Sstevel@tonic-gate * the difference between these pointers. If we run out of space, we return 13227c478bd9Sstevel@tonic-gate * an error and exec_args() starts all over again with a buffer twice as large. 13237c478bd9Sstevel@tonic-gate * When we're all done, the kernel buffer looks like this: 13247c478bd9Sstevel@tonic-gate * 13257c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size 13267c478bd9Sstevel@tonic-gate * | argv[0] offset | 13277c478bd9Sstevel@tonic-gate * +-----------------------+ 13287c478bd9Sstevel@tonic-gate * | ... | 13297c478bd9Sstevel@tonic-gate * +-----------------------+ 13307c478bd9Sstevel@tonic-gate * | argv[argc-1] offset | 13317c478bd9Sstevel@tonic-gate * +-----------------------+ 13327c478bd9Sstevel@tonic-gate * | envp[0] offset | 13337c478bd9Sstevel@tonic-gate * +-----------------------+ 13347c478bd9Sstevel@tonic-gate * | ... | 13357c478bd9Sstevel@tonic-gate * +-----------------------+ 13367c478bd9Sstevel@tonic-gate * | envp[envc-1] offset | 13377c478bd9Sstevel@tonic-gate * +-----------------------+ 13387c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset| 13397c478bd9Sstevel@tonic-gate * +-----------------------+ 13407c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset| 13417c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp 13427c478bd9Sstevel@tonic-gate * | | 13437c478bd9Sstevel@tonic-gate * | STK_AVAIL() space | 13447c478bd9Sstevel@tonic-gate * | | 13457c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp 13467c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset| 13477c478bd9Sstevel@tonic-gate * +-----------------------+ 13487c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset| 13497c478bd9Sstevel@tonic-gate * +-----------------------+ 13507c478bd9Sstevel@tonic-gate * | envp[envc-1] string | 13517c478bd9Sstevel@tonic-gate * +-----------------------+ 13527c478bd9Sstevel@tonic-gate * | ... | 13537c478bd9Sstevel@tonic-gate * +-----------------------+ 13547c478bd9Sstevel@tonic-gate * | envp[0] string | 13557c478bd9Sstevel@tonic-gate * +-----------------------+ 13567c478bd9Sstevel@tonic-gate * | argv[argc-1] string | 13577c478bd9Sstevel@tonic-gate * +-----------------------+ 13587c478bd9Sstevel@tonic-gate * | ... | 13597c478bd9Sstevel@tonic-gate * +-----------------------+ 13607c478bd9Sstevel@tonic-gate * | argv[0] string | 13617c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base 13627c478bd9Sstevel@tonic-gate */ 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp) 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate /* 13677c478bd9Sstevel@tonic-gate * Add a string to the stack. 13687c478bd9Sstevel@tonic-gate */ 13697c478bd9Sstevel@tonic-gate static int 13707c478bd9Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg) 13717c478bd9Sstevel@tonic-gate { 13727c478bd9Sstevel@tonic-gate int error; 13737c478bd9Sstevel@tonic-gate size_t len; 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < sizeof (int)) 13767c478bd9Sstevel@tonic-gate return (E2BIG); 13777c478bd9Sstevel@tonic-gate *--args->stk_offp = args->stk_strp - args->stk_base; 13787c478bd9Sstevel@tonic-gate 13797c478bd9Sstevel@tonic-gate if (segflg == UIO_USERSPACE) { 13807c478bd9Sstevel@tonic-gate error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len); 13817c478bd9Sstevel@tonic-gate if (error != 0) 13827c478bd9Sstevel@tonic-gate return (error); 13837c478bd9Sstevel@tonic-gate } else { 13847c478bd9Sstevel@tonic-gate len = strlen(sp) + 1; 13857c478bd9Sstevel@tonic-gate if (len > STK_AVAIL(args)) 13867c478bd9Sstevel@tonic-gate return (E2BIG); 13877c478bd9Sstevel@tonic-gate bcopy(sp, args->stk_strp, len); 13887c478bd9Sstevel@tonic-gate } 13897c478bd9Sstevel@tonic-gate 13907c478bd9Sstevel@tonic-gate args->stk_strp += len; 13917c478bd9Sstevel@tonic-gate 13927c478bd9Sstevel@tonic-gate return (0); 13937c478bd9Sstevel@tonic-gate } 13947c478bd9Sstevel@tonic-gate 13957c478bd9Sstevel@tonic-gate static int 13967c478bd9Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst) 13977c478bd9Sstevel@tonic-gate { 13987c478bd9Sstevel@tonic-gate int error; 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate if (args->from_model == DATAMODEL_NATIVE) { 14017c478bd9Sstevel@tonic-gate ulong_t ptr; 14027c478bd9Sstevel@tonic-gate error = fulword(src, &ptr); 14037c478bd9Sstevel@tonic-gate *dst = (caddr_t)ptr; 14047c478bd9Sstevel@tonic-gate } else { 14057c478bd9Sstevel@tonic-gate uint32_t ptr; 14067c478bd9Sstevel@tonic-gate error = fuword32(src, &ptr); 14077c478bd9Sstevel@tonic-gate *dst = (caddr_t)(uintptr_t)ptr; 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate return (error); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate static int 14137c478bd9Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value) 14147c478bd9Sstevel@tonic-gate { 14157c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) 14167c478bd9Sstevel@tonic-gate return (sulword(addr, (ulong_t)value)); 14177c478bd9Sstevel@tonic-gate else 14187c478bd9Sstevel@tonic-gate return (suword32(addr, (uint32_t)(uintptr_t)value)); 14197c478bd9Sstevel@tonic-gate } 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate static int 14227c478bd9Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 14237c478bd9Sstevel@tonic-gate { 14247c478bd9Sstevel@tonic-gate char *sp; 14257c478bd9Sstevel@tonic-gate int argc, error; 14267c478bd9Sstevel@tonic-gate int argv_empty = 0; 14277c478bd9Sstevel@tonic-gate size_t ptrsize = args->from_ptrsize; 14287c478bd9Sstevel@tonic-gate size_t size, pad; 14297c478bd9Sstevel@tonic-gate char *argv = (char *)uap->argp; 14307c478bd9Sstevel@tonic-gate char *envp = (char *)uap->envp; 14317c478bd9Sstevel@tonic-gate 14327c478bd9Sstevel@tonic-gate /* 14337c478bd9Sstevel@tonic-gate * Copy interpreter's name and argument to argv[0] and argv[1]. 14347c478bd9Sstevel@tonic-gate */ 14357c478bd9Sstevel@tonic-gate if (intp != NULL && intp->intp_name != NULL) { 14367c478bd9Sstevel@tonic-gate if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0) 14377c478bd9Sstevel@tonic-gate return (error); 14387c478bd9Sstevel@tonic-gate if (intp->intp_arg != NULL && 14397c478bd9Sstevel@tonic-gate (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0) 14407c478bd9Sstevel@tonic-gate return (error); 14417c478bd9Sstevel@tonic-gate if (args->fname != NULL) 14427c478bd9Sstevel@tonic-gate error = stk_add(args, args->fname, UIO_SYSSPACE); 14437c478bd9Sstevel@tonic-gate else 14447c478bd9Sstevel@tonic-gate error = stk_add(args, uap->fname, UIO_USERSPACE); 14457c478bd9Sstevel@tonic-gate if (error) 14467c478bd9Sstevel@tonic-gate return (error); 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate /* 14497c478bd9Sstevel@tonic-gate * Check for an empty argv[]. 14507c478bd9Sstevel@tonic-gate */ 14517c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp)) 14527c478bd9Sstevel@tonic-gate return (EFAULT); 14537c478bd9Sstevel@tonic-gate if (sp == NULL) 14547c478bd9Sstevel@tonic-gate argv_empty = 1; 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate argv += ptrsize; /* ignore original argv[0] */ 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate 14597c478bd9Sstevel@tonic-gate if (argv_empty == 0) { 14607c478bd9Sstevel@tonic-gate /* 14617c478bd9Sstevel@tonic-gate * Add argv[] strings to the stack. 14627c478bd9Sstevel@tonic-gate */ 14637c478bd9Sstevel@tonic-gate for (;;) { 14647c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp)) 14657c478bd9Sstevel@tonic-gate return (EFAULT); 14667c478bd9Sstevel@tonic-gate if (sp == NULL) 14677c478bd9Sstevel@tonic-gate break; 14687c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 14697c478bd9Sstevel@tonic-gate return (error); 14707c478bd9Sstevel@tonic-gate argv += ptrsize; 14717c478bd9Sstevel@tonic-gate } 14727c478bd9Sstevel@tonic-gate } 14737c478bd9Sstevel@tonic-gate argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 14747c478bd9Sstevel@tonic-gate args->arglen = args->stk_strp - args->stk_base; 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate /* 14777c478bd9Sstevel@tonic-gate * Add environ[] strings to the stack. 14787c478bd9Sstevel@tonic-gate */ 14797c478bd9Sstevel@tonic-gate if (envp != NULL) { 14807c478bd9Sstevel@tonic-gate for (;;) { 14817c478bd9Sstevel@tonic-gate if (stk_getptr(args, envp, &sp)) 14827c478bd9Sstevel@tonic-gate return (EFAULT); 14837c478bd9Sstevel@tonic-gate if (sp == NULL) 14847c478bd9Sstevel@tonic-gate break; 14857c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0) 14867c478bd9Sstevel@tonic-gate return (error); 14877c478bd9Sstevel@tonic-gate envp += ptrsize; 14887c478bd9Sstevel@tonic-gate } 14897c478bd9Sstevel@tonic-gate } 14907c478bd9Sstevel@tonic-gate args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp; 14917c478bd9Sstevel@tonic-gate args->ne = args->na - argc; 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate /* 14949acbbeafSnn35248 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and 14959acbbeafSnn35248 * AT_SUN_EMULATOR strings to the stack. 14967c478bd9Sstevel@tonic-gate */ 14977c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) { 14987c478bd9Sstevel@tonic-gate if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0) 14997c478bd9Sstevel@tonic-gate return (error); 15007c478bd9Sstevel@tonic-gate if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0) 15017c478bd9Sstevel@tonic-gate return (error); 15029acbbeafSnn35248 if (args->brandname != NULL && 1503d4204c85Sraf (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0) 15049acbbeafSnn35248 return (error); 15059acbbeafSnn35248 if (args->emulator != NULL && 1506d4204c85Sraf (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0) 15079acbbeafSnn35248 return (error); 15087c478bd9Sstevel@tonic-gate } 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate /* 15117c478bd9Sstevel@tonic-gate * Compute the size of the stack. This includes all the pointers, 15127c478bd9Sstevel@tonic-gate * the space reserved for the aux vector, and all the strings. 15137c478bd9Sstevel@tonic-gate * The total number of pointers is args->na (which is argc + envc) 15147c478bd9Sstevel@tonic-gate * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL 15157c478bd9Sstevel@tonic-gate * after the last argument (i.e. argv[argc]); (3) the NULL after the 15167c478bd9Sstevel@tonic-gate * last environment variable (i.e. envp[envc]); and (4) the NULL after 15177c478bd9Sstevel@tonic-gate * all the strings, at the very top of the stack. 15187c478bd9Sstevel@tonic-gate */ 15197c478bd9Sstevel@tonic-gate size = (args->na + 4) * args->to_ptrsize + args->auxsize + 15207c478bd9Sstevel@tonic-gate (args->stk_strp - args->stk_base); 15217c478bd9Sstevel@tonic-gate 15227c478bd9Sstevel@tonic-gate /* 15237c478bd9Sstevel@tonic-gate * Pad the string section with zeroes to align the stack size. 15247c478bd9Sstevel@tonic-gate */ 15257c478bd9Sstevel@tonic-gate pad = P2NPHASE(size, args->stk_align); 15267c478bd9Sstevel@tonic-gate 15277c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < pad) 15287c478bd9Sstevel@tonic-gate return (E2BIG); 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate args->usrstack_size = size + pad; 15317c478bd9Sstevel@tonic-gate 15327c478bd9Sstevel@tonic-gate while (pad-- != 0) 15337c478bd9Sstevel@tonic-gate *args->stk_strp++ = 0; 15347c478bd9Sstevel@tonic-gate 15357c478bd9Sstevel@tonic-gate args->nc = args->stk_strp - args->stk_base; 15367c478bd9Sstevel@tonic-gate 15377c478bd9Sstevel@tonic-gate return (0); 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate static int 15417c478bd9Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up) 15427c478bd9Sstevel@tonic-gate { 15437c478bd9Sstevel@tonic-gate size_t ptrsize = args->to_ptrsize; 15447c478bd9Sstevel@tonic-gate ssize_t pslen; 15457c478bd9Sstevel@tonic-gate char *kstrp = args->stk_base; 15467c478bd9Sstevel@tonic-gate char *ustrp = usrstack - args->nc - ptrsize; 15477c478bd9Sstevel@tonic-gate char *usp = usrstack - args->usrstack_size; 15487c478bd9Sstevel@tonic-gate int *offp = (int *)(args->stk_base + args->stk_size); 15497c478bd9Sstevel@tonic-gate int envc = args->ne; 15507c478bd9Sstevel@tonic-gate int argc = args->na - envc; 15517c478bd9Sstevel@tonic-gate int i; 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate /* 15547c478bd9Sstevel@tonic-gate * Record argc for /proc. 15557c478bd9Sstevel@tonic-gate */ 15567c478bd9Sstevel@tonic-gate up->u_argc = argc; 15577c478bd9Sstevel@tonic-gate 15587c478bd9Sstevel@tonic-gate /* 15597c478bd9Sstevel@tonic-gate * Put argc on the stack. Note that even though it's an int, 15607c478bd9Sstevel@tonic-gate * it always consumes ptrsize bytes (for alignment). 15617c478bd9Sstevel@tonic-gate */ 15627c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, (char *)(uintptr_t)argc)) 15637c478bd9Sstevel@tonic-gate return (-1); 15647c478bd9Sstevel@tonic-gate 15657c478bd9Sstevel@tonic-gate /* 15667c478bd9Sstevel@tonic-gate * Add argc space (ptrsize) to usp and record argv for /proc. 15677c478bd9Sstevel@tonic-gate */ 15687c478bd9Sstevel@tonic-gate up->u_argv = (uintptr_t)(usp += ptrsize); 15697c478bd9Sstevel@tonic-gate 15707c478bd9Sstevel@tonic-gate /* 15717c478bd9Sstevel@tonic-gate * Put the argv[] pointers on the stack. 15727c478bd9Sstevel@tonic-gate */ 15737c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++, usp += ptrsize) 15747c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp])) 15757c478bd9Sstevel@tonic-gate return (-1); 15767c478bd9Sstevel@tonic-gate 15777c478bd9Sstevel@tonic-gate /* 15787c478bd9Sstevel@tonic-gate * Copy arguments to u_psargs. 15797c478bd9Sstevel@tonic-gate */ 15807c478bd9Sstevel@tonic-gate pslen = MIN(args->arglen, PSARGSZ) - 1; 15817c478bd9Sstevel@tonic-gate for (i = 0; i < pslen; i++) 15827c478bd9Sstevel@tonic-gate up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]); 15837c478bd9Sstevel@tonic-gate while (i < PSARGSZ) 15847c478bd9Sstevel@tonic-gate up->u_psargs[i++] = '\0'; 15857c478bd9Sstevel@tonic-gate 15867c478bd9Sstevel@tonic-gate /* 15877c478bd9Sstevel@tonic-gate * Add space for argv[]'s NULL terminator (ptrsize) to usp and 15887c478bd9Sstevel@tonic-gate * record envp for /proc. 15897c478bd9Sstevel@tonic-gate */ 15907c478bd9Sstevel@tonic-gate up->u_envp = (uintptr_t)(usp += ptrsize); 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate /* 15937c478bd9Sstevel@tonic-gate * Put the envp[] pointers on the stack. 15947c478bd9Sstevel@tonic-gate */ 15957c478bd9Sstevel@tonic-gate for (i = 0; i < envc; i++, usp += ptrsize) 15967c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp])) 15977c478bd9Sstevel@tonic-gate return (-1); 15987c478bd9Sstevel@tonic-gate 15997c478bd9Sstevel@tonic-gate /* 16007c478bd9Sstevel@tonic-gate * Add space for envp[]'s NULL terminator (ptrsize) to usp and 16017c478bd9Sstevel@tonic-gate * remember where the stack ends, which is also where auxv begins. 16027c478bd9Sstevel@tonic-gate */ 16037c478bd9Sstevel@tonic-gate args->stackend = usp += ptrsize; 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate /* 16067c478bd9Sstevel@tonic-gate * Put all the argv[], envp[], and auxv strings on the stack. 16077c478bd9Sstevel@tonic-gate */ 16087c478bd9Sstevel@tonic-gate if (copyout(args->stk_base, ustrp, args->nc)) 16097c478bd9Sstevel@tonic-gate return (-1); 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate /* 16127c478bd9Sstevel@tonic-gate * Fill in the aux vector now that we know the user stack addresses 16139acbbeafSnn35248 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and 16149acbbeafSnn35248 * AT_SUN_EMULATOR strings. 16157c478bd9Sstevel@tonic-gate */ 16167c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) { 16177c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) { 16187c478bd9Sstevel@tonic-gate auxv_t **a = (auxv_t **)auxvpp; 16197c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp]) 16207c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp]) 16219acbbeafSnn35248 if (args->brandname != NULL) 16229acbbeafSnn35248 ADDAUX(*a, 16239acbbeafSnn35248 AT_SUN_BRANDNAME, (long)&ustrp[*--offp]) 16249acbbeafSnn35248 if (args->emulator != NULL) 16259acbbeafSnn35248 ADDAUX(*a, 16269acbbeafSnn35248 AT_SUN_EMULATOR, (long)&ustrp[*--offp]) 16277c478bd9Sstevel@tonic-gate } else { 16287c478bd9Sstevel@tonic-gate auxv32_t **a = (auxv32_t **)auxvpp; 16297c478bd9Sstevel@tonic-gate ADDAUX(*a, 16307c478bd9Sstevel@tonic-gate AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp]) 16317c478bd9Sstevel@tonic-gate ADDAUX(*a, 16329acbbeafSnn35248 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp]) 16339acbbeafSnn35248 if (args->brandname != NULL) 16349acbbeafSnn35248 ADDAUX(*a, AT_SUN_BRANDNAME, 16359acbbeafSnn35248 (int)(uintptr_t)&ustrp[*--offp]) 16369acbbeafSnn35248 if (args->emulator != NULL) 16379acbbeafSnn35248 ADDAUX(*a, AT_SUN_EMULATOR, 16389acbbeafSnn35248 (int)(uintptr_t)&ustrp[*--offp]) 16397c478bd9Sstevel@tonic-gate } 16407c478bd9Sstevel@tonic-gate } 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate return (0); 16437c478bd9Sstevel@tonic-gate } 16447c478bd9Sstevel@tonic-gate 16457c478bd9Sstevel@tonic-gate /* 16467c478bd9Sstevel@tonic-gate * Initialize a new user stack with the specified arguments and environment. 16477c478bd9Sstevel@tonic-gate * The initial user stack layout is as follows: 16487c478bd9Sstevel@tonic-gate * 16497c478bd9Sstevel@tonic-gate * User Stack 16507c478bd9Sstevel@tonic-gate * +---------------+ <--- curproc->p_usrstack 1651102033aaSdp78419 * | | 1652102033aaSdp78419 * | slew | 1653102033aaSdp78419 * | | 1654102033aaSdp78419 * +---------------+ 16557c478bd9Sstevel@tonic-gate * | NULL | 16567c478bd9Sstevel@tonic-gate * +---------------+ 16577c478bd9Sstevel@tonic-gate * | | 16587c478bd9Sstevel@tonic-gate * | auxv strings | 16597c478bd9Sstevel@tonic-gate * | | 16607c478bd9Sstevel@tonic-gate * +---------------+ 16617c478bd9Sstevel@tonic-gate * | | 16627c478bd9Sstevel@tonic-gate * | envp strings | 16637c478bd9Sstevel@tonic-gate * | | 16647c478bd9Sstevel@tonic-gate * +---------------+ 16657c478bd9Sstevel@tonic-gate * | | 16667c478bd9Sstevel@tonic-gate * | argv strings | 16677c478bd9Sstevel@tonic-gate * | | 16687c478bd9Sstevel@tonic-gate * +---------------+ <--- ustrp 16697c478bd9Sstevel@tonic-gate * | | 16707c478bd9Sstevel@tonic-gate * | aux vector | 16717c478bd9Sstevel@tonic-gate * | | 16727c478bd9Sstevel@tonic-gate * +---------------+ <--- auxv 16737c478bd9Sstevel@tonic-gate * | NULL | 16747c478bd9Sstevel@tonic-gate * +---------------+ 16757c478bd9Sstevel@tonic-gate * | envp[envc-1] | 16767c478bd9Sstevel@tonic-gate * +---------------+ 16777c478bd9Sstevel@tonic-gate * | ... | 16787c478bd9Sstevel@tonic-gate * +---------------+ 16797c478bd9Sstevel@tonic-gate * | envp[0] | 16807c478bd9Sstevel@tonic-gate * +---------------+ <--- envp[] 16817c478bd9Sstevel@tonic-gate * | NULL | 16827c478bd9Sstevel@tonic-gate * +---------------+ 16837c478bd9Sstevel@tonic-gate * | argv[argc-1] | 16847c478bd9Sstevel@tonic-gate * +---------------+ 16857c478bd9Sstevel@tonic-gate * | ... | 16867c478bd9Sstevel@tonic-gate * +---------------+ 16877c478bd9Sstevel@tonic-gate * | argv[0] | 16887c478bd9Sstevel@tonic-gate * +---------------+ <--- argv[] 16897c478bd9Sstevel@tonic-gate * | argc | 16907c478bd9Sstevel@tonic-gate * +---------------+ <--- stack base 16917c478bd9Sstevel@tonic-gate */ 16927c478bd9Sstevel@tonic-gate int 16937c478bd9Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp) 16947c478bd9Sstevel@tonic-gate { 16957c478bd9Sstevel@tonic-gate size_t size; 16967c478bd9Sstevel@tonic-gate int error; 16977c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 16987c478bd9Sstevel@tonic-gate user_t *up = PTOU(p); 16997c478bd9Sstevel@tonic-gate char *usrstack; 17007c478bd9Sstevel@tonic-gate rctl_entity_p_t e; 17017c478bd9Sstevel@tonic-gate struct as *as; 1702ec25b48fSsusans extern int use_stk_lpg; 1703102033aaSdp78419 size_t sp_slew; 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate args->from_model = p->p_model; 17067c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) { 17077c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (long); 17087c478bd9Sstevel@tonic-gate } else { 17097c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (int32_t); 17107c478bd9Sstevel@tonic-gate } 17117c478bd9Sstevel@tonic-gate 17127c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) { 17137c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (long); 17147c478bd9Sstevel@tonic-gate args->ncargs = NCARGS; 17157c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN; 1716a0de58d6SRoger A. Faulkner if (args->addr32) 1717a0de58d6SRoger A. Faulkner usrstack = (char *)USRSTACK64_32; 1718a0de58d6SRoger A. Faulkner else 17197c478bd9Sstevel@tonic-gate usrstack = (char *)USRSTACK; 17207c478bd9Sstevel@tonic-gate } else { 17217c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (int32_t); 17227c478bd9Sstevel@tonic-gate args->ncargs = NCARGS32; 17237c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN32; 17247c478bd9Sstevel@tonic-gate usrstack = (char *)USRSTACK32; 17257c478bd9Sstevel@tonic-gate } 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0); 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate #if defined(__sparc) 17307c478bd9Sstevel@tonic-gate /* 17317c478bd9Sstevel@tonic-gate * Make sure user register windows are empty before 17327c478bd9Sstevel@tonic-gate * attempting to make a new stack. 17337c478bd9Sstevel@tonic-gate */ 17347c478bd9Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 17357c478bd9Sstevel@tonic-gate #endif 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate for (size = PAGESIZE; ; size *= 2) { 17387c478bd9Sstevel@tonic-gate args->stk_size = size; 17397c478bd9Sstevel@tonic-gate args->stk_base = kmem_alloc(size, KM_SLEEP); 17407c478bd9Sstevel@tonic-gate args->stk_strp = args->stk_base; 17417c478bd9Sstevel@tonic-gate args->stk_offp = (int *)(args->stk_base + size); 17427c478bd9Sstevel@tonic-gate error = stk_copyin(uap, args, intp, auxvpp); 17437c478bd9Sstevel@tonic-gate if (error == 0) 17447c478bd9Sstevel@tonic-gate break; 17457c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, size); 17467c478bd9Sstevel@tonic-gate if (error != E2BIG && error != ENAMETOOLONG) 17477c478bd9Sstevel@tonic-gate return (error); 17487c478bd9Sstevel@tonic-gate if (size >= args->ncargs) 17497c478bd9Sstevel@tonic-gate return (E2BIG); 17507c478bd9Sstevel@tonic-gate } 17517c478bd9Sstevel@tonic-gate 17527c478bd9Sstevel@tonic-gate size = args->usrstack_size; 17537c478bd9Sstevel@tonic-gate 17547c478bd9Sstevel@tonic-gate ASSERT(error == 0); 17557c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(size, args->stk_align) == 0); 17567c478bd9Sstevel@tonic-gate ASSERT((ssize_t)STK_AVAIL(args) >= 0); 17577c478bd9Sstevel@tonic-gate 17587c478bd9Sstevel@tonic-gate if (size > args->ncargs) { 17597c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 17607c478bd9Sstevel@tonic-gate return (E2BIG); 17617c478bd9Sstevel@tonic-gate } 17627c478bd9Sstevel@tonic-gate 17637c478bd9Sstevel@tonic-gate /* 17647c478bd9Sstevel@tonic-gate * Leave only the current lwp and force the other lwps to exit. 17657c478bd9Sstevel@tonic-gate * If another lwp beat us to the punch by calling exit(), bail out. 17667c478bd9Sstevel@tonic-gate */ 17677c478bd9Sstevel@tonic-gate if ((error = exitlwps(0)) != 0) { 17687c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 17697c478bd9Sstevel@tonic-gate return (error); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate /* 17737c478bd9Sstevel@tonic-gate * Revoke any doors created by the process. 17747c478bd9Sstevel@tonic-gate */ 17757c478bd9Sstevel@tonic-gate if (p->p_door_list) 17767c478bd9Sstevel@tonic-gate door_exit(); 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate /* 17797c478bd9Sstevel@tonic-gate * Release schedctl data structures. 17807c478bd9Sstevel@tonic-gate */ 17817c478bd9Sstevel@tonic-gate if (p->p_pagep) 17827c478bd9Sstevel@tonic-gate schedctl_proc_cleanup(); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate /* 17857c478bd9Sstevel@tonic-gate * Clean up any DTrace helpers for the process. 17867c478bd9Sstevel@tonic-gate */ 17877c478bd9Sstevel@tonic-gate if (p->p_dtrace_helpers != NULL) { 17887c478bd9Sstevel@tonic-gate ASSERT(dtrace_helpers_cleanup != NULL); 17897c478bd9Sstevel@tonic-gate (*dtrace_helpers_cleanup)(); 17907c478bd9Sstevel@tonic-gate } 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 17937c478bd9Sstevel@tonic-gate /* 17947c478bd9Sstevel@tonic-gate * Cleanup the DTrace provider associated with this process. 17957c478bd9Sstevel@tonic-gate */ 17967c478bd9Sstevel@tonic-gate if (p->p_dtrace_probes) { 17977c478bd9Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr != NULL); 17987c478bd9Sstevel@tonic-gate dtrace_fasttrap_exec_ptr(p); 17997c478bd9Sstevel@tonic-gate } 18007c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate /* 18037c478bd9Sstevel@tonic-gate * discard the lwpchan cache. 18047c478bd9Sstevel@tonic-gate */ 18057c478bd9Sstevel@tonic-gate if (p->p_lcp != NULL) 18067c478bd9Sstevel@tonic-gate lwpchan_destroy_cache(1); 18077c478bd9Sstevel@tonic-gate 18087c478bd9Sstevel@tonic-gate /* 18097c478bd9Sstevel@tonic-gate * Delete the POSIX timers. 18107c478bd9Sstevel@tonic-gate */ 18117c478bd9Sstevel@tonic-gate if (p->p_itimer != NULL) 18127c478bd9Sstevel@tonic-gate timer_exit(); 18137c478bd9Sstevel@tonic-gate 1814e0cf54a5SRoger A. Faulkner /* 1815e0cf54a5SRoger A. Faulkner * Delete the ITIMER_REALPROF interval timer. 1816e0cf54a5SRoger A. Faulkner * The other ITIMER_* interval timers are specified 1817e0cf54a5SRoger A. Faulkner * to be inherited across exec(). 1818e0cf54a5SRoger A. Faulkner */ 1819e0cf54a5SRoger A. Faulkner delete_itimer_realprof(); 1820e0cf54a5SRoger A. Faulkner 18217c478bd9Sstevel@tonic-gate if (audit_active) 18227c478bd9Sstevel@tonic-gate audit_exec(args->stk_base, args->stk_base + args->arglen, 18237c478bd9Sstevel@tonic-gate args->na - args->ne, args->ne); 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate /* 18267c478bd9Sstevel@tonic-gate * Ensure that we don't change resource associations while we 18277c478bd9Sstevel@tonic-gate * change address spaces. 18287c478bd9Sstevel@tonic-gate */ 18297c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 18307c478bd9Sstevel@tonic-gate pool_barrier_enter(); 18317c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate /* 18347c478bd9Sstevel@tonic-gate * Destroy the old address space and create a new one. 18357c478bd9Sstevel@tonic-gate * From here on, any errors are fatal to the exec()ing process. 18367c478bd9Sstevel@tonic-gate * On error we return -1, which means the caller must SIGKILL 18377c478bd9Sstevel@tonic-gate * the process. 18387c478bd9Sstevel@tonic-gate */ 18397c478bd9Sstevel@tonic-gate relvm(); 18407c478bd9Sstevel@tonic-gate 18417c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 18427c478bd9Sstevel@tonic-gate pool_barrier_exit(); 18437c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate up->u_execsw = args->execswp; 18467c478bd9Sstevel@tonic-gate 18477c478bd9Sstevel@tonic-gate p->p_brkbase = NULL; 18487c478bd9Sstevel@tonic-gate p->p_brksize = 0; 1849ec25b48fSsusans p->p_brkpageszc = 0; 18507c478bd9Sstevel@tonic-gate p->p_stksize = 0; 1851ec25b48fSsusans p->p_stkpageszc = 0; 18527c478bd9Sstevel@tonic-gate p->p_model = args->to_model; 18537c478bd9Sstevel@tonic-gate p->p_usrstack = usrstack; 18547c478bd9Sstevel@tonic-gate p->p_stkprot = args->stk_prot; 18557c478bd9Sstevel@tonic-gate p->p_datprot = args->dat_prot; 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate /* 18587c478bd9Sstevel@tonic-gate * Reset resource controls such that all controls are again active as 18597c478bd9Sstevel@tonic-gate * well as appropriate to the potentially new address model for the 18607c478bd9Sstevel@tonic-gate * process. 18617c478bd9Sstevel@tonic-gate */ 18627c478bd9Sstevel@tonic-gate e.rcep_p.proc = p; 18637c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_PROCESS; 18647c478bd9Sstevel@tonic-gate rctl_set_reset(p->p_rctls, p, &e); 18657c478bd9Sstevel@tonic-gate 1866ec25b48fSsusans /* Too early to call map_pgsz for the heap */ 1867ec25b48fSsusans if (use_stk_lpg) { 1868ec25b48fSsusans p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0)); 18697c478bd9Sstevel@tonic-gate } 18707c478bd9Sstevel@tonic-gate 18717c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 18727c478bd9Sstevel@tonic-gate p->p_flag |= SAUTOLPG; /* kernel controls page sizes */ 18737c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 18747c478bd9Sstevel@tonic-gate 1875102033aaSdp78419 /* 1876102033aaSdp78419 * Some platforms may choose to randomize real stack start by adding a 1877102033aaSdp78419 * small slew (not more than a few hundred bytes) to the top of the 1878102033aaSdp78419 * stack. This helps avoid cache thrashing when identical processes 1879102033aaSdp78419 * simultaneously share caches that don't provide enough associativity 1880102033aaSdp78419 * (e.g. sun4v systems). In this case stack slewing makes the same hot 1881102033aaSdp78419 * stack variables in different processes to live in different cache 1882102033aaSdp78419 * sets increasing effective associativity. 1883102033aaSdp78419 */ 1884102033aaSdp78419 sp_slew = exec_get_spslew(); 1885102033aaSdp78419 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0); 1886102033aaSdp78419 exec_set_sp(size + sp_slew); 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate as = as_alloc(); 18897c478bd9Sstevel@tonic-gate p->p_as = as; 1890c6939658Ssl108498 as->a_proc = p; 1891a0de58d6SRoger A. Faulkner if (p->p_model == DATAMODEL_ILP32 || args->addr32) 18927c478bd9Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT32; 18937c478bd9Sstevel@tonic-gate (void) hat_setup(as->a_hat, HAT_ALLOC); 189405d3dc4bSpaulsan hat_join_srd(as->a_hat, args->ex_vp); 18957c478bd9Sstevel@tonic-gate 18967c478bd9Sstevel@tonic-gate /* 18977c478bd9Sstevel@tonic-gate * Finally, write out the contents of the new stack. 18987c478bd9Sstevel@tonic-gate */ 1899102033aaSdp78419 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up); 19007c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size); 19017c478bd9Sstevel@tonic-gate return (error); 19027c478bd9Sstevel@tonic-gate } 1903