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 /*
23134a1f4eSCasper H.S. Dik * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
28ebb8ac07SRobert Mustacchi /*
29386e9c9eSJerry Jelinek * Copyright 2014, Joyent, Inc. All rights reserved.
30ebb8ac07SRobert Mustacchi */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/policy.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <sys/acct.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
517c478bd9Sstevel@tonic-gate #include <sys/vm.h>
522cb27123Saguzovsk #include <sys/lgrp.h>
537c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
547c478bd9Sstevel@tonic-gate #include <sys/exec.h>
557c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
567c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
577c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
587c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
597c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
60d4204c85Sraf #include <sys/door.h>
617c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
637c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
647c478bd9Sstevel@tonic-gate #include <sys/stack.h>
657c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
667c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
677c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
687c478bd9Sstevel@tonic-gate #include <sys/pool.h>
697c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
709acbbeafSnn35248 #include <sys/brand.h>
71134a1f4eSCasper H.S. Dik #include <sys/klpd.h>
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #include <c2/audit.h>
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate #include <vm/hat.h>
767c478bd9Sstevel@tonic-gate #include <vm/anon.h>
777c478bd9Sstevel@tonic-gate #include <vm/as.h>
787c478bd9Sstevel@tonic-gate #include <vm/seg.h>
797c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate #define PRIV_RESET 0x01 /* needs to reset privs */
827c478bd9Sstevel@tonic-gate #define PRIV_SETID 0x02 /* needs to change uids */
837c478bd9Sstevel@tonic-gate #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */
847c478bd9Sstevel@tonic-gate #define PRIV_INCREASE 0x08 /* child runs with more privs */
8545916cd2Sjpk #define MAC_FLAGS 0x10 /* need to adjust MAC flags */
86134a1f4eSCasper H.S. Dik #define PRIV_FORCED 0x20 /* has forced privileges */
877c478bd9Sstevel@tonic-gate
88134a1f4eSCasper H.S. Dik static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *,
89134a1f4eSCasper H.S. Dik priv_set_t *, cred_t *, const char *);
907c478bd9Sstevel@tonic-gate static int hold_execsw(struct execsw *);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */
93ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_2 = 0; /* AT_SUN_HWCAP2 */
947c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
957c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */
96ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_2 = 0; /* 32-bit version of auxv_hwcap2 */
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate #define PSUIDFLAGS (SNOCD|SUGID)
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * exece() - system call wrapper around exec_common()
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate int
exece(const char * fname,const char ** argp,const char ** envp)1057c478bd9Sstevel@tonic-gate exece(const char *fname, const char **argp, const char **envp)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate int error;
1087c478bd9Sstevel@tonic-gate
1099acbbeafSnn35248 error = exec_common(fname, argp, envp, EBA_NONE);
1107c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate int
exec_common(const char * fname,const char ** argp,const char ** envp,int brand_action)1149acbbeafSnn35248 exec_common(const char *fname, const char **argp, const char **envp,
1159acbbeafSnn35248 int brand_action)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
1187c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
1197c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
1207c478bd9Sstevel@tonic-gate struct user *up = PTOU(p);
1217c478bd9Sstevel@tonic-gate long execsz; /* temporary count of exec size */
1227c478bd9Sstevel@tonic-gate int i;
1237c478bd9Sstevel@tonic-gate int error;
1247c478bd9Sstevel@tonic-gate char exec_file[MAXCOMLEN+1];
1257c478bd9Sstevel@tonic-gate struct pathname pn;
1267c478bd9Sstevel@tonic-gate struct pathname resolvepn;
1277c478bd9Sstevel@tonic-gate struct uarg args;
1287c478bd9Sstevel@tonic-gate struct execa ua;
1297c478bd9Sstevel@tonic-gate k_sigset_t savedmask;
1307c478bd9Sstevel@tonic-gate lwpdir_t *lwpdir = NULL;
1316eb30ec3SRoger A. Faulkner tidhash_t *tidhash;
1327c478bd9Sstevel@tonic-gate lwpdir_t *old_lwpdir = NULL;
1337c478bd9Sstevel@tonic-gate uint_t old_lwpdir_sz;
1346eb30ec3SRoger A. Faulkner tidhash_t *old_tidhash;
1357c478bd9Sstevel@tonic-gate uint_t old_tidhash_sz;
1366eb30ec3SRoger A. Faulkner ret_tidhash_t *ret_tidhash;
1377c478bd9Sstevel@tonic-gate lwpent_t *lep;
138fd9e7635Sedp boolean_t brandme = B_FALSE;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * exec() is not supported for the /proc agent lwp.
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate if (curthread == p->p_agenttp)
1447c478bd9Sstevel@tonic-gate return (ENOTSUP);
1457c478bd9Sstevel@tonic-gate
1469acbbeafSnn35248 if (brand_action != EBA_NONE) {
1479acbbeafSnn35248 /*
1489acbbeafSnn35248 * Brand actions are not supported for processes that are not
1499acbbeafSnn35248 * running in a branded zone.
1509acbbeafSnn35248 */
1519acbbeafSnn35248 if (!ZONE_IS_BRANDED(p->p_zone))
1529acbbeafSnn35248 return (ENOTSUP);
1539acbbeafSnn35248
1549acbbeafSnn35248 if (brand_action == EBA_NATIVE) {
1559acbbeafSnn35248 /* Only branded processes can be unbranded */
1569acbbeafSnn35248 if (!PROC_IS_BRANDED(p))
1579acbbeafSnn35248 return (ENOTSUP);
1589acbbeafSnn35248 } else {
1599acbbeafSnn35248 /* Only unbranded processes can be branded */
1609acbbeafSnn35248 if (PROC_IS_BRANDED(p))
1619acbbeafSnn35248 return (ENOTSUP);
162fd9e7635Sedp brandme = B_TRUE;
1639acbbeafSnn35248 }
1649acbbeafSnn35248 } else {
1659acbbeafSnn35248 /*
1669acbbeafSnn35248 * If this is a native zone, or if the process is already
1679acbbeafSnn35248 * branded, then we don't need to do anything. If this is
1689acbbeafSnn35248 * a native process in a branded zone, we need to brand the
1699acbbeafSnn35248 * process as it exec()s the new binary.
1709acbbeafSnn35248 */
1719acbbeafSnn35248 if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p))
172fd9e7635Sedp brandme = B_TRUE;
1739acbbeafSnn35248 }
1749acbbeafSnn35248
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * Inform /proc that an exec() has started.
1777c478bd9Sstevel@tonic-gate * Hold signals that are ignored by default so that we will
1787c478bd9Sstevel@tonic-gate * not be interrupted by a signal that will be ignored after
1797c478bd9Sstevel@tonic-gate * successful completion of gexec().
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1827c478bd9Sstevel@tonic-gate prexecstart();
1837c478bd9Sstevel@tonic-gate schedctl_finish_sigblock(curthread);
1847c478bd9Sstevel@tonic-gate savedmask = curthread->t_hold;
1857c478bd9Sstevel@tonic-gate sigorset(&curthread->t_hold, &ignoredefault);
1867c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * Look up path name and remember last component for later.
1907c478bd9Sstevel@tonic-gate * To help coreadm expand its %d token, we attempt to save
1917c478bd9Sstevel@tonic-gate * the directory containing the executable in p_execdir. The
1927c478bd9Sstevel@tonic-gate * first call to lookuppn() may fail and return EINVAL because
1937c478bd9Sstevel@tonic-gate * dirvpp is non-NULL. In that case, we make a second call to
1947c478bd9Sstevel@tonic-gate * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
1957c478bd9Sstevel@tonic-gate * but coreadm is allowed to expand %d to the empty string and
1967c478bd9Sstevel@tonic-gate * there are other cases in which that failure may occur.
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
1997c478bd9Sstevel@tonic-gate goto out;
2007c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn);
2017c478bd9Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
2027c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2037c478bd9Sstevel@tonic-gate pn_free(&pn);
2047c478bd9Sstevel@tonic-gate if (error != EINVAL)
2057c478bd9Sstevel@tonic-gate goto out;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate dir = NULL;
2087c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
2097c478bd9Sstevel@tonic-gate goto out;
2107c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn);
2117c478bd9Sstevel@tonic-gate if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
2127c478bd9Sstevel@tonic-gate &vp)) != 0) {
2137c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2147c478bd9Sstevel@tonic-gate pn_free(&pn);
2157c478bd9Sstevel@tonic-gate goto out;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate if (vp == NULL) {
2197c478bd9Sstevel@tonic-gate if (dir != NULL)
2207c478bd9Sstevel@tonic-gate VN_RELE(dir);
2217c478bd9Sstevel@tonic-gate error = ENOENT;
2227c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2237c478bd9Sstevel@tonic-gate pn_free(&pn);
2247c478bd9Sstevel@tonic-gate goto out;
2257c478bd9Sstevel@tonic-gate }
226a3d40fb8Scasper
227ddf7fe95Scasper if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) {
228ddf7fe95Scasper if (dir != NULL)
229ddf7fe95Scasper VN_RELE(dir);
230ddf7fe95Scasper pn_free(&resolvepn);
231ddf7fe95Scasper pn_free(&pn);
232ddf7fe95Scasper VN_RELE(vp);
233ddf7fe95Scasper goto out;
234ddf7fe95Scasper }
235ddf7fe95Scasper
236a3d40fb8Scasper /*
237a3d40fb8Scasper * We do not allow executing files in attribute directories.
238a3d40fb8Scasper * We test this by determining whether the resolved path
239a3d40fb8Scasper * contains a "/" when we're in an attribute directory;
240a3d40fb8Scasper * only if the pathname does not contain a "/" the resolved path
241a3d40fb8Scasper * points to a file in the current working (attribute) directory.
242a3d40fb8Scasper */
243a3d40fb8Scasper if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
244a3d40fb8Scasper strchr(resolvepn.pn_path, '/') == NULL) {
245a3d40fb8Scasper if (dir != NULL)
246a3d40fb8Scasper VN_RELE(dir);
247a3d40fb8Scasper error = EACCES;
248a3d40fb8Scasper pn_free(&resolvepn);
249a3d40fb8Scasper pn_free(&pn);
250a3d40fb8Scasper VN_RELE(vp);
251a3d40fb8Scasper goto out;
252a3d40fb8Scasper }
253a3d40fb8Scasper
2547c478bd9Sstevel@tonic-gate bzero(exec_file, MAXCOMLEN+1);
2557c478bd9Sstevel@tonic-gate (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
2567c478bd9Sstevel@tonic-gate bzero(&args, sizeof (args));
2577c478bd9Sstevel@tonic-gate args.pathname = resolvepn.pn_path;
2587c478bd9Sstevel@tonic-gate /* don't free resolvepn until we are done with args */
2597c478bd9Sstevel@tonic-gate pn_free(&pn);
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /*
262134a1f4eSCasper H.S. Dik * If we're running in a profile shell, then call pfexecd.
263134a1f4eSCasper H.S. Dik */
264134a1f4eSCasper H.S. Dik if ((CR_FLAGS(p->p_cred) & PRIV_PFEXEC) != 0) {
265134a1f4eSCasper H.S. Dik error = pfexec_call(p->p_cred, &resolvepn, &args.pfcred,
266134a1f4eSCasper H.S. Dik &args.scrubenv);
267134a1f4eSCasper H.S. Dik
268134a1f4eSCasper H.S. Dik /* Returning errno in case we're not allowed to execute. */
269134a1f4eSCasper H.S. Dik if (error > 0) {
270134a1f4eSCasper H.S. Dik if (dir != NULL)
271134a1f4eSCasper H.S. Dik VN_RELE(dir);
272134a1f4eSCasper H.S. Dik pn_free(&resolvepn);
273134a1f4eSCasper H.S. Dik VN_RELE(vp);
274134a1f4eSCasper H.S. Dik goto out;
275134a1f4eSCasper H.S. Dik }
276134a1f4eSCasper H.S. Dik
277134a1f4eSCasper H.S. Dik /* Don't change the credentials when using old ptrace. */
278134a1f4eSCasper H.S. Dik if (args.pfcred != NULL &&
279134a1f4eSCasper H.S. Dik (p->p_proc_flag & P_PR_PTRACE) != 0) {
280134a1f4eSCasper H.S. Dik crfree(args.pfcred);
281134a1f4eSCasper H.S. Dik args.pfcred = NULL;
282134a1f4eSCasper H.S. Dik args.scrubenv = B_FALSE;
283134a1f4eSCasper H.S. Dik }
284134a1f4eSCasper H.S. Dik }
285134a1f4eSCasper H.S. Dik
286134a1f4eSCasper H.S. Dik /*
2877c478bd9Sstevel@tonic-gate * Specific exec handlers, or policies determined via
2887c478bd9Sstevel@tonic-gate * /etc/system may override the historical default.
2897c478bd9Sstevel@tonic-gate */
2907c478bd9Sstevel@tonic-gate args.stk_prot = PROT_ZFOD;
2917c478bd9Sstevel@tonic-gate args.dat_prot = PROT_ZFOD;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate CPU_STATS_ADD_K(sys, sysexec, 1);
2947c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec, char *, args.pathname);
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate ua.fname = fname;
2977c478bd9Sstevel@tonic-gate ua.argp = argp;
2987c478bd9Sstevel@tonic-gate ua.envp = envp;
2997c478bd9Sstevel@tonic-gate
3009acbbeafSnn35248 /* If necessary, brand this process before we start the exec. */
301fd9e7635Sedp if (brandme)
3029acbbeafSnn35248 brand_setbrand(p);
3039acbbeafSnn35248
3047c478bd9Sstevel@tonic-gate if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
3059acbbeafSnn35248 exec_file, p->p_cred, brand_action)) != 0) {
306fd9e7635Sedp if (brandme)
307e9f7cbf0SVamsi Nagineni brand_clearbrand(p, B_FALSE);
3087c478bd9Sstevel@tonic-gate VN_RELE(vp);
3097c478bd9Sstevel@tonic-gate if (dir != NULL)
3107c478bd9Sstevel@tonic-gate VN_RELE(dir);
3117c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
3127c478bd9Sstevel@tonic-gate goto fail;
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate * Free floating point registers (sun4u only)
3177c478bd9Sstevel@tonic-gate */
3187c478bd9Sstevel@tonic-gate ASSERT(lwp != NULL);
3197c478bd9Sstevel@tonic-gate lwp_freeregs(lwp, 1);
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate /*
3220baeff3dSrab * Free thread and process context ops.
3237c478bd9Sstevel@tonic-gate */
3247c478bd9Sstevel@tonic-gate if (curthread->t_ctx)
3257c478bd9Sstevel@tonic-gate freectx(curthread, 1);
3260baeff3dSrab if (p->p_pctx)
3270baeff3dSrab freepctx(p, 1);
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate * Remember file name for accounting; clear any cached DTrace predicate.
3317c478bd9Sstevel@tonic-gate */
3327c478bd9Sstevel@tonic-gate up->u_acflag &= ~AFORK;
3337c478bd9Sstevel@tonic-gate bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
3347c478bd9Sstevel@tonic-gate curthread->t_predcache = NULL;
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate * Clear contract template state
3387c478bd9Sstevel@tonic-gate */
3397c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(lwp);
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * Save the directory in which we found the executable for expanding
3437c478bd9Sstevel@tonic-gate * the %d token used in core file patterns.
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
3467c478bd9Sstevel@tonic-gate tmpvp = p->p_execdir;
3477c478bd9Sstevel@tonic-gate p->p_execdir = dir;
3487c478bd9Sstevel@tonic-gate if (p->p_execdir != NULL)
3497c478bd9Sstevel@tonic-gate VN_HOLD(p->p_execdir);
3507c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate if (tmpvp != NULL)
3537c478bd9Sstevel@tonic-gate VN_RELE(tmpvp);
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate * Reset stack state to the user stack, clear set of signals
3577c478bd9Sstevel@tonic-gate * caught on the signal stack, and reset list of signals that
3587c478bd9Sstevel@tonic-gate * restart system calls; the new program's environment should
3597c478bd9Sstevel@tonic-gate * not be affected by detritus from the old program. Any
3607c478bd9Sstevel@tonic-gate * pending held signals remain held, so don't clear t_hold.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
3637c478bd9Sstevel@tonic-gate lwp->lwp_oldcontext = 0;
3647c478bd9Sstevel@tonic-gate lwp->lwp_ustack = 0;
3657c478bd9Sstevel@tonic-gate lwp->lwp_old_stk_ctl = 0;
3667c478bd9Sstevel@tonic-gate sigemptyset(&up->u_signodefer);
3677c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigonstack);
3687c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigresethand);
3697c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
3707c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
3717c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate * Make saved resource limit == current resource limit.
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate for (i = 0; i < RLIM_NLIMITS; i++) {
3777c478bd9Sstevel@tonic-gate /*CONSTCOND*/
3787c478bd9Sstevel@tonic-gate if (RLIM_SAVED(i)) {
3797c478bd9Sstevel@tonic-gate (void) rctl_rlimit_get(rctlproc_legacy[i], p,
3807c478bd9Sstevel@tonic-gate &up->u_saved_rlimit[i]);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate * If the action was to catch the signal, then the action
3867c478bd9Sstevel@tonic-gate * must be reset to SIG_DFL.
3877c478bd9Sstevel@tonic-gate */
3887c478bd9Sstevel@tonic-gate sigdefault(p);
3897c478bd9Sstevel@tonic-gate p->p_flag &= ~(SNOWAIT|SJCTL);
3907c478bd9Sstevel@tonic-gate p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
3917c478bd9Sstevel@tonic-gate up->u_signal[SIGCLD - 1] = SIG_DFL;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * Delete the dot4 sigqueues/signotifies.
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate sigqfree(p);
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate mutex_enter(&p->p_pflock);
4017c478bd9Sstevel@tonic-gate p->p_prof.pr_base = NULL;
4027c478bd9Sstevel@tonic-gate p->p_prof.pr_size = 0;
4037c478bd9Sstevel@tonic-gate p->p_prof.pr_off = 0;
4047c478bd9Sstevel@tonic-gate p->p_prof.pr_scale = 0;
4057c478bd9Sstevel@tonic-gate p->p_prof.pr_samples = 0;
4067c478bd9Sstevel@tonic-gate mutex_exit(&p->p_pflock);
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL);
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate #if defined(__sparc)
4117c478bd9Sstevel@tonic-gate if (p->p_utraps != NULL)
4127c478bd9Sstevel@tonic-gate utrap_free(p);
4137c478bd9Sstevel@tonic-gate #endif /* __sparc */
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * Close all close-on-exec files.
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate close_exec(P_FINFO(p));
4197c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
4209acbbeafSnn35248
421fd9e7635Sedp /* Unbrand ourself if necessary. */
422fd9e7635Sedp if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE))
423e9f7cbf0SVamsi Nagineni brand_clearbrand(p, B_FALSE);
4249acbbeafSnn35248
4257c478bd9Sstevel@tonic-gate setregs(&args);
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate /* Mark this as an executable vnode */
4287c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock);
4297c478bd9Sstevel@tonic-gate vp->v_flag |= VVMEXEC;
4307c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock);
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate VN_RELE(vp);
4337c478bd9Sstevel@tonic-gate if (dir != NULL)
4347c478bd9Sstevel@tonic-gate VN_RELE(dir);
4357c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * Allocate a new lwp directory and lwpid hash table if necessary.
4397c478bd9Sstevel@tonic-gate */
4407c478bd9Sstevel@tonic-gate if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
4417c478bd9Sstevel@tonic-gate lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
4427c478bd9Sstevel@tonic-gate lwpdir->ld_next = lwpdir + 1;
4436eb30ec3SRoger A. Faulkner tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP);
4447c478bd9Sstevel@tonic-gate if (p->p_lwpdir != NULL)
4457c478bd9Sstevel@tonic-gate lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
4467c478bd9Sstevel@tonic-gate else
4477c478bd9Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate
4509acbbeafSnn35248 if (PROC_IS_BRANDED(p))
4519acbbeafSnn35248 BROP(p)->b_exec();
4529acbbeafSnn35248
4537c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
4547c478bd9Sstevel@tonic-gate prbarrier(p);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * Reset lwp id to the default value of 1.
4587c478bd9Sstevel@tonic-gate * This is a single-threaded process now
4597c478bd9Sstevel@tonic-gate * and lwp #1 is lwp_wait()able by default.
4607c478bd9Sstevel@tonic-gate * The t_unpark flag should not be inherited.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
4637c478bd9Sstevel@tonic-gate curthread->t_tid = 1;
4642cb27123Saguzovsk kpreempt_disable();
4652cb27123Saguzovsk ASSERT(curthread->t_lpl != NULL);
4662cb27123Saguzovsk p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid;
4672cb27123Saguzovsk kpreempt_enable();
4682cb27123Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) {
4692cb27123Saguzovsk lgrp_update_trthr_migrations(1);
4702cb27123Saguzovsk }
4717c478bd9Sstevel@tonic-gate curthread->t_unpark = 0;
4727c478bd9Sstevel@tonic-gate curthread->t_proc_flag |= TP_TWAIT;
4737c478bd9Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */
4747c478bd9Sstevel@tonic-gate p->p_lwpdaemon = 0; /* but oh well ... */
4757c478bd9Sstevel@tonic-gate p->p_lwpid = 1;
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate * Install the newly-allocated lwp directory and lwpid hash table
4797c478bd9Sstevel@tonic-gate * and insert the current thread into the new hash table.
4807c478bd9Sstevel@tonic-gate */
4817c478bd9Sstevel@tonic-gate if (lwpdir != NULL) {
4827c478bd9Sstevel@tonic-gate old_lwpdir = p->p_lwpdir;
4837c478bd9Sstevel@tonic-gate old_lwpdir_sz = p->p_lwpdir_sz;
4847c478bd9Sstevel@tonic-gate old_tidhash = p->p_tidhash;
4857c478bd9Sstevel@tonic-gate old_tidhash_sz = p->p_tidhash_sz;
4867c478bd9Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = lwpdir;
4877c478bd9Sstevel@tonic-gate p->p_lwpdir_sz = 2;
4887c478bd9Sstevel@tonic-gate lep->le_thread = curthread;
4897c478bd9Sstevel@tonic-gate lep->le_lwpid = curthread->t_tid;
4907c478bd9Sstevel@tonic-gate lep->le_start = curthread->t_start;
4916eb30ec3SRoger A. Faulkner lwp_hash_in(p, lep, tidhash, 2, 0);
4926eb30ec3SRoger A. Faulkner p->p_tidhash = tidhash;
4936eb30ec3SRoger A. Faulkner p->p_tidhash_sz = 2;
4947c478bd9Sstevel@tonic-gate }
4956eb30ec3SRoger A. Faulkner ret_tidhash = p->p_ret_tidhash;
4966eb30ec3SRoger A. Faulkner p->p_ret_tidhash = NULL;
4979acbbeafSnn35248
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate * Restore the saved signal mask and
5007c478bd9Sstevel@tonic-gate * inform /proc that the exec() has finished.
5017c478bd9Sstevel@tonic-gate */
5027c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask;
5037c478bd9Sstevel@tonic-gate prexecend();
5047c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
5057c478bd9Sstevel@tonic-gate if (old_lwpdir) {
5067c478bd9Sstevel@tonic-gate kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
5076eb30ec3SRoger A. Faulkner kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t));
5086eb30ec3SRoger A. Faulkner }
5096eb30ec3SRoger A. Faulkner while (ret_tidhash != NULL) {
5106eb30ec3SRoger A. Faulkner ret_tidhash_t *next = ret_tidhash->rth_next;
5116eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash->rth_tidhash,
5126eb30ec3SRoger A. Faulkner ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
5136eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash, sizeof (*ret_tidhash));
5146eb30ec3SRoger A. Faulkner ret_tidhash = next;
5157c478bd9Sstevel@tonic-gate }
5169acbbeafSnn35248
5177c478bd9Sstevel@tonic-gate ASSERT(error == 0);
5187c478bd9Sstevel@tonic-gate DTRACE_PROC(exec__success);
5197c478bd9Sstevel@tonic-gate return (0);
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate fail:
5227c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec__failure, int, error);
5237c478bd9Sstevel@tonic-gate out: /* error return */
5247c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
5257c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask;
5267c478bd9Sstevel@tonic-gate prexecend();
5277c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
5287c478bd9Sstevel@tonic-gate ASSERT(error != 0);
5297c478bd9Sstevel@tonic-gate return (error);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate * Perform generic exec duties and switchout to object-file specific
5357c478bd9Sstevel@tonic-gate * handler.
5367c478bd9Sstevel@tonic-gate */
5377c478bd9Sstevel@tonic-gate int
gexec(struct vnode ** vpp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,caddr_t exec_file,struct cred * cred,int brand_action)5387c478bd9Sstevel@tonic-gate gexec(
5397c478bd9Sstevel@tonic-gate struct vnode **vpp,
5407c478bd9Sstevel@tonic-gate struct execa *uap,
5417c478bd9Sstevel@tonic-gate struct uarg *args,
5427c478bd9Sstevel@tonic-gate struct intpdata *idatap,
5437c478bd9Sstevel@tonic-gate int level,
5447c478bd9Sstevel@tonic-gate long *execsz,
5457c478bd9Sstevel@tonic-gate caddr_t exec_file,
5469acbbeafSnn35248 struct cred *cred,
5479acbbeafSnn35248 int brand_action)
5487c478bd9Sstevel@tonic-gate {
549a4aeef46SDonghai Qiao struct vnode *vp, *execvp = NULL;
5507c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
5517c478bd9Sstevel@tonic-gate struct execsw *eswp;
5527c478bd9Sstevel@tonic-gate int error = 0;
5537c478bd9Sstevel@tonic-gate int suidflags = 0;
5547c478bd9Sstevel@tonic-gate ssize_t resid;
5557c478bd9Sstevel@tonic-gate uid_t uid, gid;
5567c478bd9Sstevel@tonic-gate struct vattr vattr;
5577c478bd9Sstevel@tonic-gate char magbuf[MAGIC_BYTES];
5587c478bd9Sstevel@tonic-gate int setid;
5597c478bd9Sstevel@tonic-gate cred_t *oldcred, *newcred = NULL;
5607c478bd9Sstevel@tonic-gate int privflags = 0;
561cc4b03b5Scasper int setidfl;
562134a1f4eSCasper H.S. Dik priv_set_t fset;
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * If the SNOCD or SUGID flag is set, turn it off and remember the
5667c478bd9Sstevel@tonic-gate * previous setting so we can restore it if we encounter an error.
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
5697c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
5707c478bd9Sstevel@tonic-gate suidflags = pp->p_flag & PSUIDFLAGS;
5717c478bd9Sstevel@tonic-gate pp->p_flag &= ~PSUIDFLAGS;
5727c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate if ((error = execpermissions(*vpp, &vattr, args)) != 0)
576a4aeef46SDonghai Qiao goto bad_noclose;
5777c478bd9Sstevel@tonic-gate
578a4aeef46SDonghai Qiao /* need to open vnode for stateful file systems */
579da6c28aaSamw if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0)
580a4aeef46SDonghai Qiao goto bad_noclose;
5817c478bd9Sstevel@tonic-gate vp = *vpp;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate * Note: to support binary compatibility with SunOS a.out
5857c478bd9Sstevel@tonic-gate * executables, we read in the first four bytes, as the
5867c478bd9Sstevel@tonic-gate * magic number is in bytes 2-3.
5877c478bd9Sstevel@tonic-gate */
5887c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
5897c478bd9Sstevel@tonic-gate (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
5907c478bd9Sstevel@tonic-gate goto bad;
5917c478bd9Sstevel@tonic-gate if (resid != 0)
5927c478bd9Sstevel@tonic-gate goto bad;
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate if ((eswp = findexec_by_hdr(magbuf)) == NULL)
5957c478bd9Sstevel@tonic-gate goto bad;
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate if (level == 0 &&
598134a1f4eSCasper H.S. Dik (privflags = execsetid(vp, &vattr, &uid, &gid, &fset,
599134a1f4eSCasper H.S. Dik args->pfcred == NULL ? cred : args->pfcred, args->pathname)) != 0) {
6007c478bd9Sstevel@tonic-gate
601134a1f4eSCasper H.S. Dik /* Pfcred is a credential with a ref count of 1 */
602134a1f4eSCasper H.S. Dik
603134a1f4eSCasper H.S. Dik if (args->pfcred != NULL) {
604134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE|PRIV_RESET;
605134a1f4eSCasper H.S. Dik newcred = cred = args->pfcred;
606134a1f4eSCasper H.S. Dik } else {
6077c478bd9Sstevel@tonic-gate newcred = cred = crdup(cred);
608134a1f4eSCasper H.S. Dik }
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate /* If we can, drop the PA bit */
6117c478bd9Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0)
6127c478bd9Sstevel@tonic-gate priv_adjust_PA(cred);
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate if (privflags & PRIV_SETID) {
6157c478bd9Sstevel@tonic-gate cred->cr_uid = uid;
6167c478bd9Sstevel@tonic-gate cred->cr_gid = gid;
6177c478bd9Sstevel@tonic-gate cred->cr_suid = uid;
6187c478bd9Sstevel@tonic-gate cred->cr_sgid = gid;
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate
62145916cd2Sjpk if (privflags & MAC_FLAGS) {
62245916cd2Sjpk if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
62345916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE;
62445916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
62545916cd2Sjpk }
62645916cd2Sjpk
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate * Implement the privilege updates:
6297c478bd9Sstevel@tonic-gate *
6307c478bd9Sstevel@tonic-gate * Restrict with L:
6317c478bd9Sstevel@tonic-gate *
6327c478bd9Sstevel@tonic-gate * I' = I & L
6337c478bd9Sstevel@tonic-gate *
6347c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A
6357c478bd9Sstevel@tonic-gate *
636134a1f4eSCasper H.S. Dik * But if running under ptrace, we cap I and F with P.
6377c478bd9Sstevel@tonic-gate */
638134a1f4eSCasper H.S. Dik if ((privflags & (PRIV_RESET|PRIV_FORCED)) != 0) {
6397c478bd9Sstevel@tonic-gate if ((privflags & PRIV_INCREASE) != 0 &&
640134a1f4eSCasper H.S. Dik (pp->p_proc_flag & P_PR_PTRACE) != 0) {
6417c478bd9Sstevel@tonic-gate priv_intersect(&CR_OPPRIV(cred),
6427c478bd9Sstevel@tonic-gate &CR_IPRIV(cred));
643134a1f4eSCasper H.S. Dik priv_intersect(&CR_OPPRIV(cred), &fset);
644134a1f4eSCasper H.S. Dik }
645134a1f4eSCasper H.S. Dik priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
646134a1f4eSCasper H.S. Dik CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
647134a1f4eSCasper H.S. Dik if (privflags & PRIV_FORCED) {
648134a1f4eSCasper H.S. Dik priv_set_PA(cred);
649134a1f4eSCasper H.S. Dik priv_union(&fset, &CR_EPRIV(cred));
650134a1f4eSCasper H.S. Dik priv_union(&fset, &CR_PPRIV(cred));
651134a1f4eSCasper H.S. Dik }
652134a1f4eSCasper H.S. Dik priv_adjust_PA(cred);
653134a1f4eSCasper H.S. Dik }
654134a1f4eSCasper H.S. Dik } else if (level == 0 && args->pfcred != NULL) {
655134a1f4eSCasper H.S. Dik newcred = cred = args->pfcred;
656134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE;
657134a1f4eSCasper H.S. Dik /* pfcred is not forced to adhere to these settings */
6587c478bd9Sstevel@tonic-gate priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
6597c478bd9Sstevel@tonic-gate CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
6607c478bd9Sstevel@tonic-gate priv_adjust_PA(cred);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate /* SunOS 4.x buy-back */
6647c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
6657c478bd9Sstevel@tonic-gate (vattr.va_mode & (VSUID|VSGID))) {
66618c39a06SJason King char path[MAXNAMELEN];
66718c39a06SJason King refstr_t *mntpt = NULL;
66818c39a06SJason King int ret = -1;
66918c39a06SJason King
67018c39a06SJason King bzero(path, sizeof (path));
67118c39a06SJason King zone_hold(pp->p_zone);
67218c39a06SJason King
67318c39a06SJason King ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path,
67418c39a06SJason King sizeof (path), cred);
67518c39a06SJason King
67618c39a06SJason King /* fallback to mountpoint if a path can't be found */
67718c39a06SJason King if ((ret != 0) || (ret == 0 && path[0] == '\0'))
67818c39a06SJason King mntpt = vfs_getmntpoint(vp->v_vfsp);
67918c39a06SJason King
68018c39a06SJason King if (mntpt == NULL)
68118c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE,
68218c39a06SJason King "!uid %d: setuid execution not allowed, "
68318c39a06SJason King "file=%s", cred->cr_uid, path);
68418c39a06SJason King else
68518c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE,
68618c39a06SJason King "!uid %d: setuid execution not allowed, "
68718c39a06SJason King "fs=%s, file=%s", cred->cr_uid,
68818c39a06SJason King ZONE_PATH_TRANSLATE(refstr_value(mntpt),
68918c39a06SJason King pp->p_zone), exec_file);
69018c39a06SJason King
69118c39a06SJason King if (!INGLOBALZONE(pp)) {
69218c39a06SJason King /* zone_rootpath always has trailing / */
69318c39a06SJason King if (mntpt == NULL)
69418c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d "
69518c39a06SJason King "setuid execution not allowed, file=%s%s",
69618c39a06SJason King pp->p_zone->zone_name, cred->cr_uid,
69718c39a06SJason King pp->p_zone->zone_rootpath, path + 1);
69818c39a06SJason King else
69918c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d "
70018c39a06SJason King "setuid execution not allowed, fs=%s, "
70118c39a06SJason King "file=%s", pp->p_zone->zone_name,
70218c39a06SJason King cred->cr_uid, refstr_value(mntpt),
70318c39a06SJason King exec_file);
70418c39a06SJason King }
70518c39a06SJason King
70618c39a06SJason King if (mntpt != NULL)
70718c39a06SJason King refstr_rele(mntpt);
70818c39a06SJason King
70918c39a06SJason King zone_rele(pp->p_zone);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * execsetid() told us whether or not we had to change the
7147c478bd9Sstevel@tonic-gate * credentials of the process. In privflags, it told us
7157c478bd9Sstevel@tonic-gate * whether we gained any privileges or executed a set-uid executable.
7167c478bd9Sstevel@tonic-gate */
717134a1f4eSCasper H.S. Dik setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE|PRIV_FORCED));
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate * Use /etc/system variable to determine if the stack
7217c478bd9Sstevel@tonic-gate * should be marked as executable by default.
7227c478bd9Sstevel@tonic-gate */
7237c478bd9Sstevel@tonic-gate if (noexec_user_stack)
7247c478bd9Sstevel@tonic-gate args->stk_prot &= ~PROT_EXEC;
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
72705d3dc4bSpaulsan args->ex_vp = vp;
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate * Traditionally, the setid flags told the sub processes whether
7317c478bd9Sstevel@tonic-gate * the file just executed was set-uid or set-gid; this caused
7327c478bd9Sstevel@tonic-gate * some confusion as the 'setid' flag did not match the SUGID
7337c478bd9Sstevel@tonic-gate * process flag which is only set when the uids/gids do not match.
7347c478bd9Sstevel@tonic-gate * A script set-gid/set-uid to the real uid/gid would start with
7357c478bd9Sstevel@tonic-gate * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
7367c478bd9Sstevel@tonic-gate * Now we flag those cases where the calling process cannot
7377c478bd9Sstevel@tonic-gate * be trusted to influence the newly exec'ed process, either
7387c478bd9Sstevel@tonic-gate * because it runs with more privileges or when the uids/gids
7397c478bd9Sstevel@tonic-gate * do in fact not match.
7407c478bd9Sstevel@tonic-gate * This also makes the runtime linker agree with the on exec
7417c478bd9Sstevel@tonic-gate * values of SNOCD and SUGID.
7427c478bd9Sstevel@tonic-gate */
743cc4b03b5Scasper setidfl = 0;
744cc4b03b5Scasper if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
745cc4b03b5Scasper !supgroupmember(cred->cr_gid, cred))) {
746cc4b03b5Scasper setidfl |= EXECSETID_UGIDS;
747cc4b03b5Scasper }
748cc4b03b5Scasper if (setid & PRIV_SETUGID)
749cc4b03b5Scasper setidfl |= EXECSETID_SETID;
750134a1f4eSCasper H.S. Dik if (setid & PRIV_FORCED)
751cc4b03b5Scasper setidfl |= EXECSETID_PRIVS;
752cc4b03b5Scasper
753a4aeef46SDonghai Qiao execvp = pp->p_exec;
754a4aeef46SDonghai Qiao if (execvp)
755a4aeef46SDonghai Qiao VN_HOLD(execvp);
756a4aeef46SDonghai Qiao
7577c478bd9Sstevel@tonic-gate error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
7589acbbeafSnn35248 setidfl, exec_file, cred, brand_action);
7597c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock);
7607c478bd9Sstevel@tonic-gate if (error != 0) {
761a4aeef46SDonghai Qiao if (execvp)
762a4aeef46SDonghai Qiao VN_RELE(execvp);
76384ca7842SDonghai Qiao /*
76484ca7842SDonghai Qiao * If this process's p_exec has been set to the vp of
76584ca7842SDonghai Qiao * the executable by exec_func, we will return without
76684ca7842SDonghai Qiao * calling VOP_CLOSE because proc_exit will close it
76784ca7842SDonghai Qiao * on exit.
76884ca7842SDonghai Qiao */
76984ca7842SDonghai Qiao if (pp->p_exec == vp)
77084ca7842SDonghai Qiao goto bad_noclose;
77184ca7842SDonghai Qiao else
7727c478bd9Sstevel@tonic-gate goto bad;
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate if (level == 0) {
776134a1f4eSCasper H.S. Dik uid_t oruid;
777134a1f4eSCasper H.S. Dik
778a4aeef46SDonghai Qiao if (execvp != NULL) {
779a4aeef46SDonghai Qiao /*
780a4aeef46SDonghai Qiao * Close the previous executable only if we are
781a4aeef46SDonghai Qiao * at level 0.
782a4aeef46SDonghai Qiao */
783a4aeef46SDonghai Qiao (void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0,
784a4aeef46SDonghai Qiao cred, NULL);
785a4aeef46SDonghai Qiao }
786a4aeef46SDonghai Qiao
7877c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
788134a1f4eSCasper H.S. Dik
789134a1f4eSCasper H.S. Dik oruid = pp->p_cred->cr_ruid;
790134a1f4eSCasper H.S. Dik
7917c478bd9Sstevel@tonic-gate if (newcred != NULL) {
7927c478bd9Sstevel@tonic-gate /*
7937c478bd9Sstevel@tonic-gate * Free the old credentials, and set the new ones.
7947c478bd9Sstevel@tonic-gate * Do this for both the process and the (single) thread.
7957c478bd9Sstevel@tonic-gate */
7967c478bd9Sstevel@tonic-gate crfree(pp->p_cred);
7977c478bd9Sstevel@tonic-gate pp->p_cred = cred; /* cred already held for proc */
7987c478bd9Sstevel@tonic-gate crhold(cred); /* hold new cred for thread */
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred
8017c478bd9Sstevel@tonic-gate * must always be either NULL, or point to a valid,
8027c478bd9Sstevel@tonic-gate * allocated cred structure.
8037c478bd9Sstevel@tonic-gate */
8047c478bd9Sstevel@tonic-gate oldcred = curthread->t_cred;
8057c478bd9Sstevel@tonic-gate curthread->t_cred = cred;
8067c478bd9Sstevel@tonic-gate crfree(oldcred);
807634e26ecSCasper H.S. Dik
808d93c0b4cSCasper H.S. Dik if (priv_basic_test >= 0 &&
809634e26ecSCasper H.S. Dik !PRIV_ISASSERT(&CR_IPRIV(newcred),
810634e26ecSCasper H.S. Dik priv_basic_test)) {
811634e26ecSCasper H.S. Dik pid_t pid = pp->p_pid;
812634e26ecSCasper H.S. Dik char *fn = PTOU(pp)->u_comm;
813634e26ecSCasper H.S. Dik
814634e26ecSCasper H.S. Dik cmn_err(CE_WARN, "%s[%d]: exec: basic_test "
815634e26ecSCasper H.S. Dik "privilege removed from E/I", fn, pid);
816634e26ecSCasper H.S. Dik }
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * On emerging from a successful exec(), the saved
8207c478bd9Sstevel@tonic-gate * uid and gid equal the effective uid and gid.
8217c478bd9Sstevel@tonic-gate */
8227c478bd9Sstevel@tonic-gate cred->cr_suid = cred->cr_uid;
8237c478bd9Sstevel@tonic-gate cred->cr_sgid = cred->cr_gid;
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate * If the real and effective ids do not match, this
8277c478bd9Sstevel@tonic-gate * is a setuid process that should not dump core.
8287c478bd9Sstevel@tonic-gate * The group comparison is tricky; we prevent the code
8297c478bd9Sstevel@tonic-gate * from flagging SNOCD when executing with an effective gid
8307c478bd9Sstevel@tonic-gate * which is a supplementary group.
8317c478bd9Sstevel@tonic-gate */
8327c478bd9Sstevel@tonic-gate if (cred->cr_ruid != cred->cr_uid ||
8337c478bd9Sstevel@tonic-gate (cred->cr_rgid != cred->cr_gid &&
8347c478bd9Sstevel@tonic-gate !supgroupmember(cred->cr_gid, cred)) ||
8357c478bd9Sstevel@tonic-gate (privflags & PRIV_INCREASE) != 0)
8367c478bd9Sstevel@tonic-gate suidflags = PSUIDFLAGS;
8377c478bd9Sstevel@tonic-gate else
8387c478bd9Sstevel@tonic-gate suidflags = 0;
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
841134a1f4eSCasper H.S. Dik if (newcred != NULL && oruid != newcred->cr_ruid) {
842134a1f4eSCasper H.S. Dik /* Note that the process remains in the same zone. */
843134a1f4eSCasper H.S. Dik mutex_enter(&pidlock);
844134a1f4eSCasper H.S. Dik upcount_dec(oruid, crgetzoneid(newcred));
845134a1f4eSCasper H.S. Dik upcount_inc(newcred->cr_ruid, crgetzoneid(newcred));
846134a1f4eSCasper H.S. Dik mutex_exit(&pidlock);
847134a1f4eSCasper H.S. Dik }
8487c478bd9Sstevel@tonic-gate if (suidflags) {
8497c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
8507c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags;
8517c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
8547c478bd9Sstevel@tonic-gate /*
8557c478bd9Sstevel@tonic-gate * If process is traced via /proc, arrange to
8567c478bd9Sstevel@tonic-gate * invalidate the associated /proc vnode.
8577c478bd9Sstevel@tonic-gate */
8587c478bd9Sstevel@tonic-gate if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
8597c478bd9Sstevel@tonic-gate args->traceinval = 1;
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate if (pp->p_proc_flag & P_PR_PTRACE)
8627c478bd9Sstevel@tonic-gate psignal(pp, SIGTRAP);
8637c478bd9Sstevel@tonic-gate if (args->traceinval)
8647c478bd9Sstevel@tonic-gate prinvalidate(&pp->p_user);
8657c478bd9Sstevel@tonic-gate }
866a4aeef46SDonghai Qiao if (execvp)
867a4aeef46SDonghai Qiao VN_RELE(execvp);
8687c478bd9Sstevel@tonic-gate return (0);
869a4aeef46SDonghai Qiao
8707c478bd9Sstevel@tonic-gate bad:
871a4aeef46SDonghai Qiao (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
872a4aeef46SDonghai Qiao
873a4aeef46SDonghai Qiao bad_noclose:
87484ca7842SDonghai Qiao if (newcred != NULL)
87584ca7842SDonghai Qiao crfree(newcred);
8767c478bd9Sstevel@tonic-gate if (error == 0)
8777c478bd9Sstevel@tonic-gate error = ENOEXEC;
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate if (suidflags) {
8807c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
8817c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags;
8827c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate return (error);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate extern char *execswnames[];
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate struct execsw *
allocate_execsw(char * name,char * magic,size_t magic_size)8907c478bd9Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate int i, j;
8937c478bd9Sstevel@tonic-gate char *ename;
8947c478bd9Sstevel@tonic-gate char *magicp;
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate mutex_enter(&execsw_lock);
8977c478bd9Sstevel@tonic-gate for (i = 0; i < nexectype; i++) {
8987c478bd9Sstevel@tonic-gate if (execswnames[i] == NULL) {
8997c478bd9Sstevel@tonic-gate ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9007c478bd9Sstevel@tonic-gate (void) strcpy(ename, name);
9017c478bd9Sstevel@tonic-gate execswnames[i] = ename;
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate * Set the magic number last so that we
9047c478bd9Sstevel@tonic-gate * don't need to hold the execsw_lock in
9057c478bd9Sstevel@tonic-gate * findexectype().
9067c478bd9Sstevel@tonic-gate */
9077c478bd9Sstevel@tonic-gate magicp = kmem_alloc(magic_size, KM_SLEEP);
9087c478bd9Sstevel@tonic-gate for (j = 0; j < magic_size; j++)
9097c478bd9Sstevel@tonic-gate magicp[j] = magic[j];
9107c478bd9Sstevel@tonic-gate execsw[i].exec_magic = magicp;
9117c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock);
9127c478bd9Sstevel@tonic-gate return (&execsw[i]);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock);
9167c478bd9Sstevel@tonic-gate return (NULL);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate * Find the exec switch table entry with the corresponding magic string.
9217c478bd9Sstevel@tonic-gate */
9227c478bd9Sstevel@tonic-gate struct execsw *
findexecsw(char * magic)9237c478bd9Sstevel@tonic-gate findexecsw(char *magic)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate struct execsw *eswp;
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9287c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9297c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
9307c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
9317c478bd9Sstevel@tonic-gate return (eswp);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate return (NULL);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given exec header string by looking for the
9387c478bd9Sstevel@tonic-gate * magic string at a specified offset and length for each kind of executable
9397c478bd9Sstevel@tonic-gate * file format until one matches. If no execsw[] entry is found, try to
9407c478bd9Sstevel@tonic-gate * autoload a module for this magic string.
9417c478bd9Sstevel@tonic-gate */
9427c478bd9Sstevel@tonic-gate struct execsw *
findexec_by_hdr(char * header)9437c478bd9Sstevel@tonic-gate findexec_by_hdr(char *header)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate struct execsw *eswp;
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9487c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9497c478bd9Sstevel@tonic-gate if (header && eswp->exec_maglen != 0 &&
9507c478bd9Sstevel@tonic-gate bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
9517c478bd9Sstevel@tonic-gate eswp->exec_maglen) == 0) {
9527c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
9537c478bd9Sstevel@tonic-gate return (NULL);
9547c478bd9Sstevel@tonic-gate return (eswp);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given magic string. If no execsw[] entry
9627c478bd9Sstevel@tonic-gate * is found, try to autoload a module for this magic string.
9637c478bd9Sstevel@tonic-gate */
9647c478bd9Sstevel@tonic-gate struct execsw *
findexec_by_magic(char * magic)9657c478bd9Sstevel@tonic-gate findexec_by_magic(char *magic)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate struct execsw *eswp;
9687c478bd9Sstevel@tonic-gate
9697c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
9707c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
9717c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
9727c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
9737c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
9747c478bd9Sstevel@tonic-gate return (NULL);
9757c478bd9Sstevel@tonic-gate return (eswp);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate
9817c478bd9Sstevel@tonic-gate static int
hold_execsw(struct execsw * eswp)9827c478bd9Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate char *name;
9857c478bd9Sstevel@tonic-gate
9867c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
9877c478bd9Sstevel@tonic-gate while (!LOADED_EXEC(eswp)) {
9887c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock);
9897c478bd9Sstevel@tonic-gate name = execswnames[eswp-execsw];
9907c478bd9Sstevel@tonic-gate ASSERT(name);
9917c478bd9Sstevel@tonic-gate if (modload("exec", name) == -1)
9927c478bd9Sstevel@tonic-gate return (-1);
9937c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate return (0);
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate static int
execsetid(struct vnode * vp,struct vattr * vattrp,uid_t * uidp,uid_t * gidp,priv_set_t * fset,cred_t * cr,const char * pathname)999134a1f4eSCasper H.S. Dik execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp,
1000134a1f4eSCasper H.S. Dik priv_set_t *fset, cred_t *cr, const char *pathname)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
10037c478bd9Sstevel@tonic-gate uid_t uid, gid;
10047c478bd9Sstevel@tonic-gate int privflags = 0;
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate /*
10077c478bd9Sstevel@tonic-gate * Remember credentials.
10087c478bd9Sstevel@tonic-gate */
10097c478bd9Sstevel@tonic-gate uid = cr->cr_uid;
10107c478bd9Sstevel@tonic-gate gid = cr->cr_gid;
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate /* Will try to reset the PRIV_AWARE bit later. */
10137c478bd9Sstevel@tonic-gate if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
10147c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET;
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
10177c478bd9Sstevel@tonic-gate /*
1018134a1f4eSCasper H.S. Dik * If it's a set-uid root program we perform the
1019134a1f4eSCasper H.S. Dik * forced privilege look-aside. This has three possible
1020134a1f4eSCasper H.S. Dik * outcomes:
1021134a1f4eSCasper H.S. Dik * no look aside information -> treat as before
1022134a1f4eSCasper H.S. Dik * look aside in Limit set -> apply forced privs
1023134a1f4eSCasper H.S. Dik * look aside not in Limit set -> ignore set-uid root
1024134a1f4eSCasper H.S. Dik *
1025134a1f4eSCasper H.S. Dik * Ordinary set-uid root execution only allowed if the limit
1026134a1f4eSCasper H.S. Dik * set holds all unsafe privileges.
10277c478bd9Sstevel@tonic-gate */
1028134a1f4eSCasper H.S. Dik if (vattrp->va_mode & VSUID) {
1029134a1f4eSCasper H.S. Dik if (vattrp->va_uid == 0) {
1030134a1f4eSCasper H.S. Dik int res = get_forced_privs(cr, pathname, fset);
1031134a1f4eSCasper H.S. Dik
1032134a1f4eSCasper H.S. Dik switch (res) {
1033134a1f4eSCasper H.S. Dik case -1:
1034134a1f4eSCasper H.S. Dik if (priv_issubset(&priv_unsafe,
1035134a1f4eSCasper H.S. Dik &CR_LPRIV(cr))) {
10367c478bd9Sstevel@tonic-gate uid = vattrp->va_uid;
10377c478bd9Sstevel@tonic-gate privflags |= PRIV_SETUGID;
10387c478bd9Sstevel@tonic-gate }
1039134a1f4eSCasper H.S. Dik break;
1040134a1f4eSCasper H.S. Dik case 0:
1041134a1f4eSCasper H.S. Dik privflags |= PRIV_FORCED|PRIV_INCREASE;
1042134a1f4eSCasper H.S. Dik break;
1043134a1f4eSCasper H.S. Dik default:
1044134a1f4eSCasper H.S. Dik break;
1045134a1f4eSCasper H.S. Dik }
1046134a1f4eSCasper H.S. Dik } else {
1047134a1f4eSCasper H.S. Dik uid = vattrp->va_uid;
1048134a1f4eSCasper H.S. Dik privflags |= PRIV_SETUGID;
1049134a1f4eSCasper H.S. Dik }
1050134a1f4eSCasper H.S. Dik }
10517c478bd9Sstevel@tonic-gate if (vattrp->va_mode & VSGID) {
10527c478bd9Sstevel@tonic-gate gid = vattrp->va_gid;
10537c478bd9Sstevel@tonic-gate privflags |= PRIV_SETUGID;
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate /*
10587c478bd9Sstevel@tonic-gate * Do we need to change our credential anyway?
10597c478bd9Sstevel@tonic-gate * This is the case when E != I or P != I, as
10607c478bd9Sstevel@tonic-gate * we need to do the assignments (with F empty and A full)
10617c478bd9Sstevel@tonic-gate * Or when I is not a subset of L; in that case we need to
10627c478bd9Sstevel@tonic-gate * enforce L.
10637c478bd9Sstevel@tonic-gate *
10647c478bd9Sstevel@tonic-gate * I' = L & I
10657c478bd9Sstevel@tonic-gate *
10667c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A
10677c478bd9Sstevel@tonic-gate * or
10687c478bd9Sstevel@tonic-gate * E' = P' = I'
10697c478bd9Sstevel@tonic-gate */
10707c478bd9Sstevel@tonic-gate if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
10717c478bd9Sstevel@tonic-gate !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
10727c478bd9Sstevel@tonic-gate !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
10737c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET;
10747c478bd9Sstevel@tonic-gate
1075134a1f4eSCasper H.S. Dik /* Child has more privileges than parent */
1076134a1f4eSCasper H.S. Dik if (!priv_issubset(&CR_IPRIV(cr), &CR_PPRIV(cr)))
1077134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE;
1078134a1f4eSCasper H.S. Dik
107945916cd2Sjpk /* If MAC-aware flag(s) are on, need to update cred to remove. */
108045916cd2Sjpk if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
108145916cd2Sjpk (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
108245916cd2Sjpk privflags |= MAC_FLAGS;
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate * Set setuid/setgid protections if no ptrace() compatibility.
10857c478bd9Sstevel@tonic-gate * For privileged processes, honor setuid/setgid even in
10867c478bd9Sstevel@tonic-gate * the presence of ptrace() compatibility.
10877c478bd9Sstevel@tonic-gate */
10887c478bd9Sstevel@tonic-gate if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
10897c478bd9Sstevel@tonic-gate PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
10907c478bd9Sstevel@tonic-gate (cr->cr_uid != uid ||
10917c478bd9Sstevel@tonic-gate cr->cr_gid != gid ||
10927c478bd9Sstevel@tonic-gate cr->cr_suid != uid ||
10937c478bd9Sstevel@tonic-gate cr->cr_sgid != gid)) {
10947c478bd9Sstevel@tonic-gate *uidp = uid;
10957c478bd9Sstevel@tonic-gate *gidp = gid;
10967c478bd9Sstevel@tonic-gate privflags |= PRIV_SETID;
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate return (privflags);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate
11017c478bd9Sstevel@tonic-gate int
execpermissions(struct vnode * vp,struct vattr * vattrp,struct uarg * args)11027c478bd9Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate int error;
11057c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
1108da6c28aaSamw if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL))
11097c478bd9Sstevel@tonic-gate return (error);
11107c478bd9Sstevel@tonic-gate /*
11117c478bd9Sstevel@tonic-gate * Check the access mode.
11127c478bd9Sstevel@tonic-gate * If VPROC, ask /proc if the file is an object file.
11137c478bd9Sstevel@tonic-gate */
1114da6c28aaSamw if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 ||
11157c478bd9Sstevel@tonic-gate !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
11167c478bd9Sstevel@tonic-gate (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
11177c478bd9Sstevel@tonic-gate (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
11187c478bd9Sstevel@tonic-gate if (error == 0)
11197c478bd9Sstevel@tonic-gate error = EACCES;
11207c478bd9Sstevel@tonic-gate return (error);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
1124da6c28aaSamw (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) {
11257c478bd9Sstevel@tonic-gate /*
11267c478bd9Sstevel@tonic-gate * If process is under ptrace(2) compatibility,
11277c478bd9Sstevel@tonic-gate * fail the exec(2).
11287c478bd9Sstevel@tonic-gate */
11297c478bd9Sstevel@tonic-gate if (p->p_proc_flag & P_PR_PTRACE)
11307c478bd9Sstevel@tonic-gate goto bad;
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate * Process is traced via /proc.
11337c478bd9Sstevel@tonic-gate * Arrange to invalidate the /proc vnode.
11347c478bd9Sstevel@tonic-gate */
11357c478bd9Sstevel@tonic-gate args->traceinval = 1;
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate return (0);
11387c478bd9Sstevel@tonic-gate bad:
11397c478bd9Sstevel@tonic-gate if (error == 0)
11407c478bd9Sstevel@tonic-gate error = ENOEXEC;
11417c478bd9Sstevel@tonic-gate return (error);
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate
11447c478bd9Sstevel@tonic-gate /*
11457c478bd9Sstevel@tonic-gate * Map a section of an executable file into the user's
11467c478bd9Sstevel@tonic-gate * address space.
11477c478bd9Sstevel@tonic-gate */
11487c478bd9Sstevel@tonic-gate int
execmap(struct vnode * vp,caddr_t addr,size_t len,size_t zfodlen,off_t offset,int prot,int page,uint_t szc)11497c478bd9Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
11507c478bd9Sstevel@tonic-gate off_t offset, int prot, int page, uint_t szc)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate int error = 0;
11537c478bd9Sstevel@tonic-gate off_t oldoffset;
11547c478bd9Sstevel@tonic-gate caddr_t zfodbase, oldaddr;
11557c478bd9Sstevel@tonic-gate size_t end, oldlen;
11567c478bd9Sstevel@tonic-gate size_t zfoddiff;
11577c478bd9Sstevel@tonic-gate label_t ljb;
11587c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
11597c478bd9Sstevel@tonic-gate
11607c478bd9Sstevel@tonic-gate oldaddr = addr;
11617c478bd9Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
11627c478bd9Sstevel@tonic-gate if (len) {
11637c478bd9Sstevel@tonic-gate oldlen = len;
11647c478bd9Sstevel@tonic-gate len += ((size_t)oldaddr - (size_t)addr);
11657c478bd9Sstevel@tonic-gate oldoffset = offset;
11667c478bd9Sstevel@tonic-gate offset = (off_t)((uintptr_t)offset & PAGEMASK);
11677c478bd9Sstevel@tonic-gate if (page) {
11687c478bd9Sstevel@tonic-gate spgcnt_t prefltmem, availm, npages;
11697c478bd9Sstevel@tonic-gate int preread;
11707c478bd9Sstevel@tonic-gate uint_t mflag = MAP_PRIVATE | MAP_FIXED;
11717c478bd9Sstevel@tonic-gate
11727c478bd9Sstevel@tonic-gate if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
11737c478bd9Sstevel@tonic-gate mflag |= MAP_TEXT;
11747c478bd9Sstevel@tonic-gate } else {
11757c478bd9Sstevel@tonic-gate mflag |= MAP_INITDATA;
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate
11787c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
11797c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
11807c478bd9Sstevel@tonic-gate error = ENOMEM;
11817c478bd9Sstevel@tonic-gate goto bad;
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate if (error = VOP_MAP(vp, (offset_t)offset,
11847c478bd9Sstevel@tonic-gate p->p_as, &addr, len, prot, PROT_ALL,
1185da6c28aaSamw mflag, CRED(), NULL))
11867c478bd9Sstevel@tonic-gate goto bad;
11877c478bd9Sstevel@tonic-gate
11887c478bd9Sstevel@tonic-gate /*
11897c478bd9Sstevel@tonic-gate * If the segment can fit, then we prefault
11907c478bd9Sstevel@tonic-gate * the entire segment in. This is based on the
11917c478bd9Sstevel@tonic-gate * model that says the best working set of a
11927c478bd9Sstevel@tonic-gate * small program is all of its pages.
11937c478bd9Sstevel@tonic-gate */
11947c478bd9Sstevel@tonic-gate npages = (spgcnt_t)btopr(len);
11957c478bd9Sstevel@tonic-gate prefltmem = freemem - desfree;
11967c478bd9Sstevel@tonic-gate preread =
11977c478bd9Sstevel@tonic-gate (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
11987c478bd9Sstevel@tonic-gate
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate * If we aren't prefaulting the segment,
12017c478bd9Sstevel@tonic-gate * increment "deficit", if necessary to ensure
12027c478bd9Sstevel@tonic-gate * that pages will become available when this
12037c478bd9Sstevel@tonic-gate * process starts executing.
12047c478bd9Sstevel@tonic-gate */
12057c478bd9Sstevel@tonic-gate availm = freemem - lotsfree;
12067c478bd9Sstevel@tonic-gate if (preread == 0 && npages > availm &&
12077c478bd9Sstevel@tonic-gate deficit < lotsfree) {
12087c478bd9Sstevel@tonic-gate deficit += MIN((pgcnt_t)(npages - availm),
12097c478bd9Sstevel@tonic-gate lotsfree - deficit);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate
12127c478bd9Sstevel@tonic-gate if (preread) {
12137c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
12147c478bd9Sstevel@tonic-gate "execmap preread:freemem %d size %lu",
12157c478bd9Sstevel@tonic-gate freemem, len);
12167c478bd9Sstevel@tonic-gate (void) as_fault(p->p_as->a_hat, p->p_as,
12177c478bd9Sstevel@tonic-gate (caddr_t)addr, len, F_INVAL, S_READ);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate } else {
12207c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
12217c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
12227c478bd9Sstevel@tonic-gate error = ENOMEM;
12237c478bd9Sstevel@tonic-gate goto bad;
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate
12267c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, addr, len,
12277c478bd9Sstevel@tonic-gate segvn_create, zfod_argsp))
12287c478bd9Sstevel@tonic-gate goto bad;
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate * Read in the segment in one big chunk.
12317c478bd9Sstevel@tonic-gate */
12327c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
12337c478bd9Sstevel@tonic-gate oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
12347c478bd9Sstevel@tonic-gate (rlim64_t)0, CRED(), (ssize_t *)0))
12357c478bd9Sstevel@tonic-gate goto bad;
12367c478bd9Sstevel@tonic-gate /*
12377c478bd9Sstevel@tonic-gate * Now set protections.
12387c478bd9Sstevel@tonic-gate */
12397c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) {
12407c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)addr,
12417c478bd9Sstevel@tonic-gate len, prot);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate
12467c478bd9Sstevel@tonic-gate if (zfodlen) {
12479acbbeafSnn35248 struct as *as = curproc->p_as;
12489acbbeafSnn35248 struct seg *seg;
12499acbbeafSnn35248 uint_t zprot = 0;
12509acbbeafSnn35248
12517c478bd9Sstevel@tonic-gate end = (size_t)addr + len;
12527c478bd9Sstevel@tonic-gate zfodbase = (caddr_t)roundup(end, PAGESIZE);
12537c478bd9Sstevel@tonic-gate zfoddiff = (uintptr_t)zfodbase - end;
12547c478bd9Sstevel@tonic-gate if (zfoddiff) {
12559acbbeafSnn35248 /*
12569acbbeafSnn35248 * Before we go to zero the remaining space on the last
12579acbbeafSnn35248 * page, make sure we have write permission.
1258386e9c9eSJerry Jelinek *
1259386e9c9eSJerry Jelinek * Normal illumos binaries don't even hit the case
1260386e9c9eSJerry Jelinek * where we have to change permission on the last page
1261386e9c9eSJerry Jelinek * since their protection is typically either
1262386e9c9eSJerry Jelinek * PROT_USER | PROT_WRITE | PROT_READ
1263386e9c9eSJerry Jelinek * or
1264386e9c9eSJerry Jelinek * PROT_ZFOD (same as PROT_ALL).
1265386e9c9eSJerry Jelinek *
1266386e9c9eSJerry Jelinek * We need to be careful how we zero-fill the last page
1267386e9c9eSJerry Jelinek * if the segment protection does not include
1268386e9c9eSJerry Jelinek * PROT_WRITE. Using as_setprot() can cause the VM
1269386e9c9eSJerry Jelinek * segment code to call segvn_vpage(), which must
1270386e9c9eSJerry Jelinek * allocate a page struct for each page in the segment.
1271386e9c9eSJerry Jelinek * If we have a very large segment, this may fail, so
1272386e9c9eSJerry Jelinek * we have to check for that, even though we ignore
1273386e9c9eSJerry Jelinek * other return values from as_setprot.
12749acbbeafSnn35248 */
12759acbbeafSnn35248
1276*dc32d872SJosef 'Jeff' Sipek AS_LOCK_ENTER(as, RW_READER);
12779acbbeafSnn35248 seg = as_segat(curproc->p_as, (caddr_t)end);
12789acbbeafSnn35248 if (seg != NULL)
12799acbbeafSnn35248 SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1,
12809acbbeafSnn35248 &zprot);
1281*dc32d872SJosef 'Jeff' Sipek AS_LOCK_EXIT(as);
12829acbbeafSnn35248
12839acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0) {
1284386e9c9eSJerry Jelinek if (as_setprot(as, (caddr_t)end, zfoddiff - 1,
1285386e9c9eSJerry Jelinek zprot | PROT_WRITE) == ENOMEM) {
1286386e9c9eSJerry Jelinek error = ENOMEM;
1287386e9c9eSJerry Jelinek goto bad;
1288386e9c9eSJerry Jelinek }
12899acbbeafSnn35248 }
12909acbbeafSnn35248
12917c478bd9Sstevel@tonic-gate if (on_fault(&ljb)) {
12927c478bd9Sstevel@tonic-gate no_fault();
12939acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0)
12949acbbeafSnn35248 (void) as_setprot(as, (caddr_t)end,
12959acbbeafSnn35248 zfoddiff - 1, zprot);
12967c478bd9Sstevel@tonic-gate error = EFAULT;
12977c478bd9Sstevel@tonic-gate goto bad;
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate uzero((void *)end, zfoddiff);
13007c478bd9Sstevel@tonic-gate no_fault();
13019acbbeafSnn35248 if (seg != NULL && (zprot & PROT_WRITE) == 0)
13029acbbeafSnn35248 (void) as_setprot(as, (caddr_t)end,
13039acbbeafSnn35248 zfoddiff - 1, zprot);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate if (zfodlen > zfoddiff) {
13067c478bd9Sstevel@tonic-gate struct segvn_crargs crargs =
13077c478bd9Sstevel@tonic-gate SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
13087c478bd9Sstevel@tonic-gate
13097c478bd9Sstevel@tonic-gate zfodlen -= zfoddiff;
13107c478bd9Sstevel@tonic-gate if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
13117c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
13127c478bd9Sstevel@tonic-gate error = ENOMEM;
13137c478bd9Sstevel@tonic-gate goto bad;
13147c478bd9Sstevel@tonic-gate }
1315ec25b48fSsusans if (szc > 0) {
1316ec25b48fSsusans /*
1317ec25b48fSsusans * ASSERT alignment because the mapelfexec()
1318ec25b48fSsusans * caller for the szc > 0 case extended zfod
1319ec25b48fSsusans * so it's end is pgsz aligned.
1320ec25b48fSsusans */
1321ec25b48fSsusans size_t pgsz = page_get_pagesize(szc);
1322ec25b48fSsusans ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz));
1323ec25b48fSsusans
1324ec25b48fSsusans if (IS_P2ALIGNED(zfodbase, pgsz)) {
13257c478bd9Sstevel@tonic-gate crargs.szc = szc;
1326ec25b48fSsusans } else {
1327ec25b48fSsusans crargs.szc = AS_MAP_HEAP;
1328ec25b48fSsusans }
1329ec25b48fSsusans } else {
1330ec25b48fSsusans crargs.szc = AS_MAP_NO_LPOOB;
1331ec25b48fSsusans }
13327c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, (caddr_t)zfodbase,
13337c478bd9Sstevel@tonic-gate zfodlen, segvn_create, &crargs))
13347c478bd9Sstevel@tonic-gate goto bad;
13357c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) {
13367c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)zfodbase,
13377c478bd9Sstevel@tonic-gate zfodlen, prot);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate return (0);
13427c478bd9Sstevel@tonic-gate bad:
13437c478bd9Sstevel@tonic-gate return (error);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate
13467c478bd9Sstevel@tonic-gate void
setexecenv(struct execenv * ep)13477c478bd9Sstevel@tonic-gate setexecenv(struct execenv *ep)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
13507c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
13517c478bd9Sstevel@tonic-gate struct vnode *vp;
13527c478bd9Sstevel@tonic-gate
13537c478bd9Sstevel@tonic-gate p->p_bssbase = ep->ex_bssbase;
13547c478bd9Sstevel@tonic-gate p->p_brkbase = ep->ex_brkbase;
13557c478bd9Sstevel@tonic-gate p->p_brksize = ep->ex_brksize;
13567c478bd9Sstevel@tonic-gate if (p->p_exec)
13577c478bd9Sstevel@tonic-gate VN_RELE(p->p_exec); /* out with the old */
13587c478bd9Sstevel@tonic-gate vp = p->p_exec = ep->ex_vp;
13597c478bd9Sstevel@tonic-gate if (vp != NULL)
13607c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* in with the new */
13617c478bd9Sstevel@tonic-gate
13627c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
13637c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
13647c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate
13677c478bd9Sstevel@tonic-gate int
execopen(struct vnode ** vpp,int * fdp)13687c478bd9Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate struct vnode *vp = *vpp;
13717c478bd9Sstevel@tonic-gate file_t *fp;
13727c478bd9Sstevel@tonic-gate int error = 0;
13737c478bd9Sstevel@tonic-gate int filemode = FREAD;
13747c478bd9Sstevel@tonic-gate
13757c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* open reference */
13767c478bd9Sstevel@tonic-gate if (error = falloc(NULL, filemode, &fp, fdp)) {
13777c478bd9Sstevel@tonic-gate VN_RELE(vp);
13787c478bd9Sstevel@tonic-gate *fdp = -1; /* just in case falloc changed value */
13797c478bd9Sstevel@tonic-gate return (error);
13807c478bd9Sstevel@tonic-gate }
1381da6c28aaSamw if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) {
13827c478bd9Sstevel@tonic-gate VN_RELE(vp);
13837c478bd9Sstevel@tonic-gate setf(*fdp, NULL);
13847c478bd9Sstevel@tonic-gate unfalloc(fp);
13857c478bd9Sstevel@tonic-gate *fdp = -1;
13867c478bd9Sstevel@tonic-gate return (error);
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate *vpp = vp; /* vnode should not have changed */
13897c478bd9Sstevel@tonic-gate fp->f_vnode = vp;
13907c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
13917c478bd9Sstevel@tonic-gate setf(*fdp, fp);
13927c478bd9Sstevel@tonic-gate return (0);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate
13957c478bd9Sstevel@tonic-gate int
execclose(int fd)13967c478bd9Sstevel@tonic-gate execclose(int fd)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate return (closeandsetf(fd, NULL));
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate
14027c478bd9Sstevel@tonic-gate /*
14037c478bd9Sstevel@tonic-gate * noexec stub function.
14047c478bd9Sstevel@tonic-gate */
14057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14067c478bd9Sstevel@tonic-gate int
noexec(struct vnode * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,int setid,caddr_t exec_file,struct cred * cred)14077c478bd9Sstevel@tonic-gate noexec(
14087c478bd9Sstevel@tonic-gate struct vnode *vp,
14097c478bd9Sstevel@tonic-gate struct execa *uap,
14107c478bd9Sstevel@tonic-gate struct uarg *args,
14117c478bd9Sstevel@tonic-gate struct intpdata *idatap,
14127c478bd9Sstevel@tonic-gate int level,
14137c478bd9Sstevel@tonic-gate long *execsz,
14147c478bd9Sstevel@tonic-gate int setid,
14157c478bd9Sstevel@tonic-gate caddr_t exec_file,
14167c478bd9Sstevel@tonic-gate struct cred *cred)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
14197c478bd9Sstevel@tonic-gate return (ENOEXEC);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate * Support routines for building a user stack.
14247c478bd9Sstevel@tonic-gate *
14257c478bd9Sstevel@tonic-gate * execve(path, argv, envp) must construct a new stack with the specified
14267c478bd9Sstevel@tonic-gate * arguments and environment variables (see exec_args() for a description
14277c478bd9Sstevel@tonic-gate * of the user stack layout). To do this, we copy the arguments and
14287c478bd9Sstevel@tonic-gate * environment variables from the old user address space into the kernel,
14297c478bd9Sstevel@tonic-gate * free the old as, create the new as, and copy our buffered information
14307c478bd9Sstevel@tonic-gate * to the new stack. Our kernel buffer has the following structure:
14317c478bd9Sstevel@tonic-gate *
14327c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
14337c478bd9Sstevel@tonic-gate * | string offsets |
14347c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
14357c478bd9Sstevel@tonic-gate * | |
14367c478bd9Sstevel@tonic-gate * | STK_AVAIL() space |
14377c478bd9Sstevel@tonic-gate * | |
14387c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
14397c478bd9Sstevel@tonic-gate * | strings |
14407c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base
14417c478bd9Sstevel@tonic-gate *
14427c478bd9Sstevel@tonic-gate * When we add a string, we store the string's contents (including the null
14437c478bd9Sstevel@tonic-gate * terminator) at stk_strp, and we store the offset of the string relative to
14447c478bd9Sstevel@tonic-gate * stk_base at --stk_offp. At strings are added, stk_strp increases and
14457c478bd9Sstevel@tonic-gate * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just
14467c478bd9Sstevel@tonic-gate * the difference between these pointers. If we run out of space, we return
14477c478bd9Sstevel@tonic-gate * an error and exec_args() starts all over again with a buffer twice as large.
14487c478bd9Sstevel@tonic-gate * When we're all done, the kernel buffer looks like this:
14497c478bd9Sstevel@tonic-gate *
14507c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
14517c478bd9Sstevel@tonic-gate * | argv[0] offset |
14527c478bd9Sstevel@tonic-gate * +-----------------------+
14537c478bd9Sstevel@tonic-gate * | ... |
14547c478bd9Sstevel@tonic-gate * +-----------------------+
14557c478bd9Sstevel@tonic-gate * | argv[argc-1] offset |
14567c478bd9Sstevel@tonic-gate * +-----------------------+
14577c478bd9Sstevel@tonic-gate * | envp[0] offset |
14587c478bd9Sstevel@tonic-gate * +-----------------------+
14597c478bd9Sstevel@tonic-gate * | ... |
14607c478bd9Sstevel@tonic-gate * +-----------------------+
14617c478bd9Sstevel@tonic-gate * | envp[envc-1] offset |
14627c478bd9Sstevel@tonic-gate * +-----------------------+
14637c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
14647c478bd9Sstevel@tonic-gate * +-----------------------+
14657c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
14667c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
14677c478bd9Sstevel@tonic-gate * | |
14687c478bd9Sstevel@tonic-gate * | STK_AVAIL() space |
14697c478bd9Sstevel@tonic-gate * | |
14707c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
14717c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
14727c478bd9Sstevel@tonic-gate * +-----------------------+
14737c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
14747c478bd9Sstevel@tonic-gate * +-----------------------+
14757c478bd9Sstevel@tonic-gate * | envp[envc-1] string |
14767c478bd9Sstevel@tonic-gate * +-----------------------+
14777c478bd9Sstevel@tonic-gate * | ... |
14787c478bd9Sstevel@tonic-gate * +-----------------------+
14797c478bd9Sstevel@tonic-gate * | envp[0] string |
14807c478bd9Sstevel@tonic-gate * +-----------------------+
14817c478bd9Sstevel@tonic-gate * | argv[argc-1] string |
14827c478bd9Sstevel@tonic-gate * +-----------------------+
14837c478bd9Sstevel@tonic-gate * | ... |
14847c478bd9Sstevel@tonic-gate * +-----------------------+
14857c478bd9Sstevel@tonic-gate * | argv[0] string |
14867c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base
14877c478bd9Sstevel@tonic-gate */
14887c478bd9Sstevel@tonic-gate
14897c478bd9Sstevel@tonic-gate #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp)
14907c478bd9Sstevel@tonic-gate
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate * Add a string to the stack.
14937c478bd9Sstevel@tonic-gate */
14947c478bd9Sstevel@tonic-gate static int
stk_add(uarg_t * args,const char * sp,enum uio_seg segflg)14957c478bd9Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate int error;
14987c478bd9Sstevel@tonic-gate size_t len;
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < sizeof (int))
15017c478bd9Sstevel@tonic-gate return (E2BIG);
15027c478bd9Sstevel@tonic-gate *--args->stk_offp = args->stk_strp - args->stk_base;
15037c478bd9Sstevel@tonic-gate
15047c478bd9Sstevel@tonic-gate if (segflg == UIO_USERSPACE) {
15057c478bd9Sstevel@tonic-gate error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
15067c478bd9Sstevel@tonic-gate if (error != 0)
15077c478bd9Sstevel@tonic-gate return (error);
15087c478bd9Sstevel@tonic-gate } else {
15097c478bd9Sstevel@tonic-gate len = strlen(sp) + 1;
15107c478bd9Sstevel@tonic-gate if (len > STK_AVAIL(args))
15117c478bd9Sstevel@tonic-gate return (E2BIG);
15127c478bd9Sstevel@tonic-gate bcopy(sp, args->stk_strp, len);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate
15157c478bd9Sstevel@tonic-gate args->stk_strp += len;
15167c478bd9Sstevel@tonic-gate
15177c478bd9Sstevel@tonic-gate return (0);
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate static int
stk_getptr(uarg_t * args,char * src,char ** dst)15217c478bd9Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate int error;
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate if (args->from_model == DATAMODEL_NATIVE) {
15267c478bd9Sstevel@tonic-gate ulong_t ptr;
15277c478bd9Sstevel@tonic-gate error = fulword(src, &ptr);
15287c478bd9Sstevel@tonic-gate *dst = (caddr_t)ptr;
15297c478bd9Sstevel@tonic-gate } else {
15307c478bd9Sstevel@tonic-gate uint32_t ptr;
15317c478bd9Sstevel@tonic-gate error = fuword32(src, &ptr);
15327c478bd9Sstevel@tonic-gate *dst = (caddr_t)(uintptr_t)ptr;
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate return (error);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate
15377c478bd9Sstevel@tonic-gate static int
stk_putptr(uarg_t * args,char * addr,char * value)15387c478bd9Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE)
15417c478bd9Sstevel@tonic-gate return (sulword(addr, (ulong_t)value));
15427c478bd9Sstevel@tonic-gate else
15437c478bd9Sstevel@tonic-gate return (suword32(addr, (uint32_t)(uintptr_t)value));
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate
15467c478bd9Sstevel@tonic-gate static int
stk_copyin(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)15477c478bd9Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate char *sp;
15507c478bd9Sstevel@tonic-gate int argc, error;
15517c478bd9Sstevel@tonic-gate int argv_empty = 0;
15527c478bd9Sstevel@tonic-gate size_t ptrsize = args->from_ptrsize;
15537c478bd9Sstevel@tonic-gate size_t size, pad;
15547c478bd9Sstevel@tonic-gate char *argv = (char *)uap->argp;
15557c478bd9Sstevel@tonic-gate char *envp = (char *)uap->envp;
15567c478bd9Sstevel@tonic-gate
15577c478bd9Sstevel@tonic-gate /*
15587c478bd9Sstevel@tonic-gate * Copy interpreter's name and argument to argv[0] and argv[1].
15597c478bd9Sstevel@tonic-gate */
15607c478bd9Sstevel@tonic-gate if (intp != NULL && intp->intp_name != NULL) {
15617c478bd9Sstevel@tonic-gate if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
15627c478bd9Sstevel@tonic-gate return (error);
15637c478bd9Sstevel@tonic-gate if (intp->intp_arg != NULL &&
15647c478bd9Sstevel@tonic-gate (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
15657c478bd9Sstevel@tonic-gate return (error);
15667c478bd9Sstevel@tonic-gate if (args->fname != NULL)
15677c478bd9Sstevel@tonic-gate error = stk_add(args, args->fname, UIO_SYSSPACE);
15687c478bd9Sstevel@tonic-gate else
15697c478bd9Sstevel@tonic-gate error = stk_add(args, uap->fname, UIO_USERSPACE);
15707c478bd9Sstevel@tonic-gate if (error)
15717c478bd9Sstevel@tonic-gate return (error);
15727c478bd9Sstevel@tonic-gate
15737c478bd9Sstevel@tonic-gate /*
15747c478bd9Sstevel@tonic-gate * Check for an empty argv[].
15757c478bd9Sstevel@tonic-gate */
15767c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
15777c478bd9Sstevel@tonic-gate return (EFAULT);
15787c478bd9Sstevel@tonic-gate if (sp == NULL)
15797c478bd9Sstevel@tonic-gate argv_empty = 1;
15807c478bd9Sstevel@tonic-gate
15817c478bd9Sstevel@tonic-gate argv += ptrsize; /* ignore original argv[0] */
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate
15847c478bd9Sstevel@tonic-gate if (argv_empty == 0) {
15857c478bd9Sstevel@tonic-gate /*
15867c478bd9Sstevel@tonic-gate * Add argv[] strings to the stack.
15877c478bd9Sstevel@tonic-gate */
15887c478bd9Sstevel@tonic-gate for (;;) {
15897c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
15907c478bd9Sstevel@tonic-gate return (EFAULT);
15917c478bd9Sstevel@tonic-gate if (sp == NULL)
15927c478bd9Sstevel@tonic-gate break;
15937c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
15947c478bd9Sstevel@tonic-gate return (error);
15957c478bd9Sstevel@tonic-gate argv += ptrsize;
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
15997c478bd9Sstevel@tonic-gate args->arglen = args->stk_strp - args->stk_base;
16007c478bd9Sstevel@tonic-gate
16017c478bd9Sstevel@tonic-gate /*
16027c478bd9Sstevel@tonic-gate * Add environ[] strings to the stack.
16037c478bd9Sstevel@tonic-gate */
16047c478bd9Sstevel@tonic-gate if (envp != NULL) {
16057c478bd9Sstevel@tonic-gate for (;;) {
1606134a1f4eSCasper H.S. Dik char *tmp = args->stk_strp;
16077c478bd9Sstevel@tonic-gate if (stk_getptr(args, envp, &sp))
16087c478bd9Sstevel@tonic-gate return (EFAULT);
16097c478bd9Sstevel@tonic-gate if (sp == NULL)
16107c478bd9Sstevel@tonic-gate break;
16117c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
16127c478bd9Sstevel@tonic-gate return (error);
1613134a1f4eSCasper H.S. Dik if (args->scrubenv && strncmp(tmp, "LD_", 3) == 0) {
1614134a1f4eSCasper H.S. Dik /* Undo the copied string */
1615134a1f4eSCasper H.S. Dik args->stk_strp = tmp;
1616134a1f4eSCasper H.S. Dik *(args->stk_offp++) = NULL;
1617134a1f4eSCasper H.S. Dik }
16187c478bd9Sstevel@tonic-gate envp += ptrsize;
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate }
16217c478bd9Sstevel@tonic-gate args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
16227c478bd9Sstevel@tonic-gate args->ne = args->na - argc;
16237c478bd9Sstevel@tonic-gate
16247c478bd9Sstevel@tonic-gate /*
16259acbbeafSnn35248 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and
16269acbbeafSnn35248 * AT_SUN_EMULATOR strings to the stack.
16277c478bd9Sstevel@tonic-gate */
16287c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
16297c478bd9Sstevel@tonic-gate if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
16307c478bd9Sstevel@tonic-gate return (error);
16317c478bd9Sstevel@tonic-gate if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
16327c478bd9Sstevel@tonic-gate return (error);
16339acbbeafSnn35248 if (args->brandname != NULL &&
1634d4204c85Sraf (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0)
16359acbbeafSnn35248 return (error);
16369acbbeafSnn35248 if (args->emulator != NULL &&
1637d4204c85Sraf (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0)
16389acbbeafSnn35248 return (error);
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate
16417c478bd9Sstevel@tonic-gate /*
16427c478bd9Sstevel@tonic-gate * Compute the size of the stack. This includes all the pointers,
16437c478bd9Sstevel@tonic-gate * the space reserved for the aux vector, and all the strings.
16447c478bd9Sstevel@tonic-gate * The total number of pointers is args->na (which is argc + envc)
16457c478bd9Sstevel@tonic-gate * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
16467c478bd9Sstevel@tonic-gate * after the last argument (i.e. argv[argc]); (3) the NULL after the
16477c478bd9Sstevel@tonic-gate * last environment variable (i.e. envp[envc]); and (4) the NULL after
16487c478bd9Sstevel@tonic-gate * all the strings, at the very top of the stack.
16497c478bd9Sstevel@tonic-gate */
16507c478bd9Sstevel@tonic-gate size = (args->na + 4) * args->to_ptrsize + args->auxsize +
16517c478bd9Sstevel@tonic-gate (args->stk_strp - args->stk_base);
16527c478bd9Sstevel@tonic-gate
16537c478bd9Sstevel@tonic-gate /*
16547c478bd9Sstevel@tonic-gate * Pad the string section with zeroes to align the stack size.
16557c478bd9Sstevel@tonic-gate */
16567c478bd9Sstevel@tonic-gate pad = P2NPHASE(size, args->stk_align);
16577c478bd9Sstevel@tonic-gate
16587c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < pad)
16597c478bd9Sstevel@tonic-gate return (E2BIG);
16607c478bd9Sstevel@tonic-gate
16617c478bd9Sstevel@tonic-gate args->usrstack_size = size + pad;
16627c478bd9Sstevel@tonic-gate
16637c478bd9Sstevel@tonic-gate while (pad-- != 0)
16647c478bd9Sstevel@tonic-gate *args->stk_strp++ = 0;
16657c478bd9Sstevel@tonic-gate
16667c478bd9Sstevel@tonic-gate args->nc = args->stk_strp - args->stk_base;
16677c478bd9Sstevel@tonic-gate
16687c478bd9Sstevel@tonic-gate return (0);
16697c478bd9Sstevel@tonic-gate }
16707c478bd9Sstevel@tonic-gate
16717c478bd9Sstevel@tonic-gate static int
stk_copyout(uarg_t * args,char * usrstack,void ** auxvpp,user_t * up)16727c478bd9Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate size_t ptrsize = args->to_ptrsize;
16757c478bd9Sstevel@tonic-gate ssize_t pslen;
16767c478bd9Sstevel@tonic-gate char *kstrp = args->stk_base;
16777c478bd9Sstevel@tonic-gate char *ustrp = usrstack - args->nc - ptrsize;
16787c478bd9Sstevel@tonic-gate char *usp = usrstack - args->usrstack_size;
16797c478bd9Sstevel@tonic-gate int *offp = (int *)(args->stk_base + args->stk_size);
16807c478bd9Sstevel@tonic-gate int envc = args->ne;
16817c478bd9Sstevel@tonic-gate int argc = args->na - envc;
16827c478bd9Sstevel@tonic-gate int i;
16837c478bd9Sstevel@tonic-gate
16847c478bd9Sstevel@tonic-gate /*
16857c478bd9Sstevel@tonic-gate * Record argc for /proc.
16867c478bd9Sstevel@tonic-gate */
16877c478bd9Sstevel@tonic-gate up->u_argc = argc;
16887c478bd9Sstevel@tonic-gate
16897c478bd9Sstevel@tonic-gate /*
16907c478bd9Sstevel@tonic-gate * Put argc on the stack. Note that even though it's an int,
16917c478bd9Sstevel@tonic-gate * it always consumes ptrsize bytes (for alignment).
16927c478bd9Sstevel@tonic-gate */
16937c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
16947c478bd9Sstevel@tonic-gate return (-1);
16957c478bd9Sstevel@tonic-gate
16967c478bd9Sstevel@tonic-gate /*
16977c478bd9Sstevel@tonic-gate * Add argc space (ptrsize) to usp and record argv for /proc.
16987c478bd9Sstevel@tonic-gate */
16997c478bd9Sstevel@tonic-gate up->u_argv = (uintptr_t)(usp += ptrsize);
17007c478bd9Sstevel@tonic-gate
17017c478bd9Sstevel@tonic-gate /*
17027c478bd9Sstevel@tonic-gate * Put the argv[] pointers on the stack.
17037c478bd9Sstevel@tonic-gate */
17047c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++, usp += ptrsize)
17057c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
17067c478bd9Sstevel@tonic-gate return (-1);
17077c478bd9Sstevel@tonic-gate
17087c478bd9Sstevel@tonic-gate /*
17097c478bd9Sstevel@tonic-gate * Copy arguments to u_psargs.
17107c478bd9Sstevel@tonic-gate */
17117c478bd9Sstevel@tonic-gate pslen = MIN(args->arglen, PSARGSZ) - 1;
17127c478bd9Sstevel@tonic-gate for (i = 0; i < pslen; i++)
17137c478bd9Sstevel@tonic-gate up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
17147c478bd9Sstevel@tonic-gate while (i < PSARGSZ)
17157c478bd9Sstevel@tonic-gate up->u_psargs[i++] = '\0';
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate /*
17187c478bd9Sstevel@tonic-gate * Add space for argv[]'s NULL terminator (ptrsize) to usp and
17197c478bd9Sstevel@tonic-gate * record envp for /proc.
17207c478bd9Sstevel@tonic-gate */
17217c478bd9Sstevel@tonic-gate up->u_envp = (uintptr_t)(usp += ptrsize);
17227c478bd9Sstevel@tonic-gate
17237c478bd9Sstevel@tonic-gate /*
17247c478bd9Sstevel@tonic-gate * Put the envp[] pointers on the stack.
17257c478bd9Sstevel@tonic-gate */
17267c478bd9Sstevel@tonic-gate for (i = 0; i < envc; i++, usp += ptrsize)
17277c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
17287c478bd9Sstevel@tonic-gate return (-1);
17297c478bd9Sstevel@tonic-gate
17307c478bd9Sstevel@tonic-gate /*
17317c478bd9Sstevel@tonic-gate * Add space for envp[]'s NULL terminator (ptrsize) to usp and
17327c478bd9Sstevel@tonic-gate * remember where the stack ends, which is also where auxv begins.
17337c478bd9Sstevel@tonic-gate */
17347c478bd9Sstevel@tonic-gate args->stackend = usp += ptrsize;
17357c478bd9Sstevel@tonic-gate
17367c478bd9Sstevel@tonic-gate /*
17377c478bd9Sstevel@tonic-gate * Put all the argv[], envp[], and auxv strings on the stack.
17387c478bd9Sstevel@tonic-gate */
17397c478bd9Sstevel@tonic-gate if (copyout(args->stk_base, ustrp, args->nc))
17407c478bd9Sstevel@tonic-gate return (-1);
17417c478bd9Sstevel@tonic-gate
17427c478bd9Sstevel@tonic-gate /*
17437c478bd9Sstevel@tonic-gate * Fill in the aux vector now that we know the user stack addresses
17449acbbeafSnn35248 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and
17459acbbeafSnn35248 * AT_SUN_EMULATOR strings.
17467c478bd9Sstevel@tonic-gate */
17477c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
17487c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
17497c478bd9Sstevel@tonic-gate auxv_t **a = (auxv_t **)auxvpp;
17507c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
17517c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
17529acbbeafSnn35248 if (args->brandname != NULL)
17539acbbeafSnn35248 ADDAUX(*a,
17549acbbeafSnn35248 AT_SUN_BRANDNAME, (long)&ustrp[*--offp])
17559acbbeafSnn35248 if (args->emulator != NULL)
17569acbbeafSnn35248 ADDAUX(*a,
17579acbbeafSnn35248 AT_SUN_EMULATOR, (long)&ustrp[*--offp])
17587c478bd9Sstevel@tonic-gate } else {
17597c478bd9Sstevel@tonic-gate auxv32_t **a = (auxv32_t **)auxvpp;
17607c478bd9Sstevel@tonic-gate ADDAUX(*a,
17617c478bd9Sstevel@tonic-gate AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
17627c478bd9Sstevel@tonic-gate ADDAUX(*a,
17639acbbeafSnn35248 AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp])
17649acbbeafSnn35248 if (args->brandname != NULL)
17659acbbeafSnn35248 ADDAUX(*a, AT_SUN_BRANDNAME,
17669acbbeafSnn35248 (int)(uintptr_t)&ustrp[*--offp])
17679acbbeafSnn35248 if (args->emulator != NULL)
17689acbbeafSnn35248 ADDAUX(*a, AT_SUN_EMULATOR,
17699acbbeafSnn35248 (int)(uintptr_t)&ustrp[*--offp])
17707c478bd9Sstevel@tonic-gate }
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate return (0);
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate
17767c478bd9Sstevel@tonic-gate /*
17777c478bd9Sstevel@tonic-gate * Initialize a new user stack with the specified arguments and environment.
17787c478bd9Sstevel@tonic-gate * The initial user stack layout is as follows:
17797c478bd9Sstevel@tonic-gate *
17807c478bd9Sstevel@tonic-gate * User Stack
17817c478bd9Sstevel@tonic-gate * +---------------+ <--- curproc->p_usrstack
1782102033aaSdp78419 * | |
1783102033aaSdp78419 * | slew |
1784102033aaSdp78419 * | |
1785102033aaSdp78419 * +---------------+
17867c478bd9Sstevel@tonic-gate * | NULL |
17877c478bd9Sstevel@tonic-gate * +---------------+
17887c478bd9Sstevel@tonic-gate * | |
17897c478bd9Sstevel@tonic-gate * | auxv strings |
17907c478bd9Sstevel@tonic-gate * | |
17917c478bd9Sstevel@tonic-gate * +---------------+
17927c478bd9Sstevel@tonic-gate * | |
17937c478bd9Sstevel@tonic-gate * | envp strings |
17947c478bd9Sstevel@tonic-gate * | |
17957c478bd9Sstevel@tonic-gate * +---------------+
17967c478bd9Sstevel@tonic-gate * | |
17977c478bd9Sstevel@tonic-gate * | argv strings |
17987c478bd9Sstevel@tonic-gate * | |
17997c478bd9Sstevel@tonic-gate * +---------------+ <--- ustrp
18007c478bd9Sstevel@tonic-gate * | |
18017c478bd9Sstevel@tonic-gate * | aux vector |
18027c478bd9Sstevel@tonic-gate * | |
18037c478bd9Sstevel@tonic-gate * +---------------+ <--- auxv
18047c478bd9Sstevel@tonic-gate * | NULL |
18057c478bd9Sstevel@tonic-gate * +---------------+
18067c478bd9Sstevel@tonic-gate * | envp[envc-1] |
18077c478bd9Sstevel@tonic-gate * +---------------+
18087c478bd9Sstevel@tonic-gate * | ... |
18097c478bd9Sstevel@tonic-gate * +---------------+
18107c478bd9Sstevel@tonic-gate * | envp[0] |
18117c478bd9Sstevel@tonic-gate * +---------------+ <--- envp[]
18127c478bd9Sstevel@tonic-gate * | NULL |
18137c478bd9Sstevel@tonic-gate * +---------------+
18147c478bd9Sstevel@tonic-gate * | argv[argc-1] |
18157c478bd9Sstevel@tonic-gate * +---------------+
18167c478bd9Sstevel@tonic-gate * | ... |
18177c478bd9Sstevel@tonic-gate * +---------------+
18187c478bd9Sstevel@tonic-gate * | argv[0] |
18197c478bd9Sstevel@tonic-gate * +---------------+ <--- argv[]
18207c478bd9Sstevel@tonic-gate * | argc |
18217c478bd9Sstevel@tonic-gate * +---------------+ <--- stack base
18227c478bd9Sstevel@tonic-gate */
18237c478bd9Sstevel@tonic-gate int
exec_args(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)18247c478bd9Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate size_t size;
18277c478bd9Sstevel@tonic-gate int error;
18287c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
18297c478bd9Sstevel@tonic-gate user_t *up = PTOU(p);
18307c478bd9Sstevel@tonic-gate char *usrstack;
18317c478bd9Sstevel@tonic-gate rctl_entity_p_t e;
18327c478bd9Sstevel@tonic-gate struct as *as;
1833ec25b48fSsusans extern int use_stk_lpg;
1834102033aaSdp78419 size_t sp_slew;
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate args->from_model = p->p_model;
18377c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
18387c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (long);
18397c478bd9Sstevel@tonic-gate } else {
18407c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (int32_t);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
18447c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (long);
18457c478bd9Sstevel@tonic-gate args->ncargs = NCARGS;
18467c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN;
1847a0de58d6SRoger A. Faulkner if (args->addr32)
1848a0de58d6SRoger A. Faulkner usrstack = (char *)USRSTACK64_32;
1849a0de58d6SRoger A. Faulkner else
18507c478bd9Sstevel@tonic-gate usrstack = (char *)USRSTACK;
18517c478bd9Sstevel@tonic-gate } else {
18527c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (int32_t);
18537c478bd9Sstevel@tonic-gate args->ncargs = NCARGS32;
18547c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN32;
18557c478bd9Sstevel@tonic-gate usrstack = (char *)USRSTACK32;
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate
18587c478bd9Sstevel@tonic-gate ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
18597c478bd9Sstevel@tonic-gate
18607c478bd9Sstevel@tonic-gate #if defined(__sparc)
18617c478bd9Sstevel@tonic-gate /*
18627c478bd9Sstevel@tonic-gate * Make sure user register windows are empty before
18637c478bd9Sstevel@tonic-gate * attempting to make a new stack.
18647c478bd9Sstevel@tonic-gate */
18657c478bd9Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL);
18667c478bd9Sstevel@tonic-gate #endif
18677c478bd9Sstevel@tonic-gate
18687c478bd9Sstevel@tonic-gate for (size = PAGESIZE; ; size *= 2) {
18697c478bd9Sstevel@tonic-gate args->stk_size = size;
18707c478bd9Sstevel@tonic-gate args->stk_base = kmem_alloc(size, KM_SLEEP);
18717c478bd9Sstevel@tonic-gate args->stk_strp = args->stk_base;
18727c478bd9Sstevel@tonic-gate args->stk_offp = (int *)(args->stk_base + size);
18737c478bd9Sstevel@tonic-gate error = stk_copyin(uap, args, intp, auxvpp);
18747c478bd9Sstevel@tonic-gate if (error == 0)
18757c478bd9Sstevel@tonic-gate break;
18767c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, size);
18777c478bd9Sstevel@tonic-gate if (error != E2BIG && error != ENAMETOOLONG)
18787c478bd9Sstevel@tonic-gate return (error);
18797c478bd9Sstevel@tonic-gate if (size >= args->ncargs)
18807c478bd9Sstevel@tonic-gate return (E2BIG);
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate
18837c478bd9Sstevel@tonic-gate size = args->usrstack_size;
18847c478bd9Sstevel@tonic-gate
18857c478bd9Sstevel@tonic-gate ASSERT(error == 0);
18867c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(size, args->stk_align) == 0);
18877c478bd9Sstevel@tonic-gate ASSERT((ssize_t)STK_AVAIL(args) >= 0);
18887c478bd9Sstevel@tonic-gate
18897c478bd9Sstevel@tonic-gate if (size > args->ncargs) {
18907c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
18917c478bd9Sstevel@tonic-gate return (E2BIG);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate * Leave only the current lwp and force the other lwps to exit.
18967c478bd9Sstevel@tonic-gate * If another lwp beat us to the punch by calling exit(), bail out.
18977c478bd9Sstevel@tonic-gate */
18987c478bd9Sstevel@tonic-gate if ((error = exitlwps(0)) != 0) {
18997c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
19007c478bd9Sstevel@tonic-gate return (error);
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate
19037c478bd9Sstevel@tonic-gate /*
19047c478bd9Sstevel@tonic-gate * Revoke any doors created by the process.
19057c478bd9Sstevel@tonic-gate */
19067c478bd9Sstevel@tonic-gate if (p->p_door_list)
19077c478bd9Sstevel@tonic-gate door_exit();
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate /*
19107c478bd9Sstevel@tonic-gate * Release schedctl data structures.
19117c478bd9Sstevel@tonic-gate */
19127c478bd9Sstevel@tonic-gate if (p->p_pagep)
19137c478bd9Sstevel@tonic-gate schedctl_proc_cleanup();
19147c478bd9Sstevel@tonic-gate
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate * Clean up any DTrace helpers for the process.
19177c478bd9Sstevel@tonic-gate */
19187c478bd9Sstevel@tonic-gate if (p->p_dtrace_helpers != NULL) {
19197c478bd9Sstevel@tonic-gate ASSERT(dtrace_helpers_cleanup != NULL);
19207c478bd9Sstevel@tonic-gate (*dtrace_helpers_cleanup)();
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate
19237c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
19247c478bd9Sstevel@tonic-gate /*
19257c478bd9Sstevel@tonic-gate * Cleanup the DTrace provider associated with this process.
19267c478bd9Sstevel@tonic-gate */
19277c478bd9Sstevel@tonic-gate if (p->p_dtrace_probes) {
19287c478bd9Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr != NULL);
19297c478bd9Sstevel@tonic-gate dtrace_fasttrap_exec_ptr(p);
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
19327c478bd9Sstevel@tonic-gate
19337c478bd9Sstevel@tonic-gate /*
19347c478bd9Sstevel@tonic-gate * discard the lwpchan cache.
19357c478bd9Sstevel@tonic-gate */
19367c478bd9Sstevel@tonic-gate if (p->p_lcp != NULL)
19377c478bd9Sstevel@tonic-gate lwpchan_destroy_cache(1);
19387c478bd9Sstevel@tonic-gate
19397c478bd9Sstevel@tonic-gate /*
19407c478bd9Sstevel@tonic-gate * Delete the POSIX timers.
19417c478bd9Sstevel@tonic-gate */
19427c478bd9Sstevel@tonic-gate if (p->p_itimer != NULL)
19437c478bd9Sstevel@tonic-gate timer_exit();
19447c478bd9Sstevel@tonic-gate
1945e0cf54a5SRoger A. Faulkner /*
1946e0cf54a5SRoger A. Faulkner * Delete the ITIMER_REALPROF interval timer.
1947e0cf54a5SRoger A. Faulkner * The other ITIMER_* interval timers are specified
1948e0cf54a5SRoger A. Faulkner * to be inherited across exec().
1949e0cf54a5SRoger A. Faulkner */
1950e0cf54a5SRoger A. Faulkner delete_itimer_realprof();
1951e0cf54a5SRoger A. Faulkner
1952005d3febSMarek Pospisil if (AU_AUDITING())
19537c478bd9Sstevel@tonic-gate audit_exec(args->stk_base, args->stk_base + args->arglen,
1954134a1f4eSCasper H.S. Dik args->na - args->ne, args->ne, args->pfcred);
19557c478bd9Sstevel@tonic-gate
19567c478bd9Sstevel@tonic-gate /*
19577c478bd9Sstevel@tonic-gate * Ensure that we don't change resource associations while we
19587c478bd9Sstevel@tonic-gate * change address spaces.
19597c478bd9Sstevel@tonic-gate */
19607c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
19617c478bd9Sstevel@tonic-gate pool_barrier_enter();
19627c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
19637c478bd9Sstevel@tonic-gate
19647c478bd9Sstevel@tonic-gate /*
19657c478bd9Sstevel@tonic-gate * Destroy the old address space and create a new one.
19667c478bd9Sstevel@tonic-gate * From here on, any errors are fatal to the exec()ing process.
19677c478bd9Sstevel@tonic-gate * On error we return -1, which means the caller must SIGKILL
19687c478bd9Sstevel@tonic-gate * the process.
19697c478bd9Sstevel@tonic-gate */
19707c478bd9Sstevel@tonic-gate relvm();
19717c478bd9Sstevel@tonic-gate
19727c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
19737c478bd9Sstevel@tonic-gate pool_barrier_exit();
19747c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
19757c478bd9Sstevel@tonic-gate
19767c478bd9Sstevel@tonic-gate up->u_execsw = args->execswp;
19777c478bd9Sstevel@tonic-gate
19787c478bd9Sstevel@tonic-gate p->p_brkbase = NULL;
19797c478bd9Sstevel@tonic-gate p->p_brksize = 0;
1980ec25b48fSsusans p->p_brkpageszc = 0;
19817c478bd9Sstevel@tonic-gate p->p_stksize = 0;
1982ec25b48fSsusans p->p_stkpageszc = 0;
19837c478bd9Sstevel@tonic-gate p->p_model = args->to_model;
19847c478bd9Sstevel@tonic-gate p->p_usrstack = usrstack;
19857c478bd9Sstevel@tonic-gate p->p_stkprot = args->stk_prot;
19867c478bd9Sstevel@tonic-gate p->p_datprot = args->dat_prot;
19877c478bd9Sstevel@tonic-gate
19887c478bd9Sstevel@tonic-gate /*
19897c478bd9Sstevel@tonic-gate * Reset resource controls such that all controls are again active as
19907c478bd9Sstevel@tonic-gate * well as appropriate to the potentially new address model for the
19917c478bd9Sstevel@tonic-gate * process.
19927c478bd9Sstevel@tonic-gate */
19937c478bd9Sstevel@tonic-gate e.rcep_p.proc = p;
19947c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_PROCESS;
19957c478bd9Sstevel@tonic-gate rctl_set_reset(p->p_rctls, p, &e);
19967c478bd9Sstevel@tonic-gate
1997ec25b48fSsusans /* Too early to call map_pgsz for the heap */
1998ec25b48fSsusans if (use_stk_lpg) {
1999ec25b48fSsusans p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0));
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate
20027c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
20037c478bd9Sstevel@tonic-gate p->p_flag |= SAUTOLPG; /* kernel controls page sizes */
20047c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
20057c478bd9Sstevel@tonic-gate
2006102033aaSdp78419 /*
2007102033aaSdp78419 * Some platforms may choose to randomize real stack start by adding a
2008102033aaSdp78419 * small slew (not more than a few hundred bytes) to the top of the
2009102033aaSdp78419 * stack. This helps avoid cache thrashing when identical processes
2010102033aaSdp78419 * simultaneously share caches that don't provide enough associativity
2011102033aaSdp78419 * (e.g. sun4v systems). In this case stack slewing makes the same hot
2012102033aaSdp78419 * stack variables in different processes to live in different cache
2013102033aaSdp78419 * sets increasing effective associativity.
2014102033aaSdp78419 */
2015102033aaSdp78419 sp_slew = exec_get_spslew();
2016102033aaSdp78419 ASSERT(P2PHASE(sp_slew, args->stk_align) == 0);
2017102033aaSdp78419 exec_set_sp(size + sp_slew);
20187c478bd9Sstevel@tonic-gate
20197c478bd9Sstevel@tonic-gate as = as_alloc();
20207c478bd9Sstevel@tonic-gate p->p_as = as;
2021c6939658Ssl108498 as->a_proc = p;
2022a0de58d6SRoger A. Faulkner if (p->p_model == DATAMODEL_ILP32 || args->addr32)
20237c478bd9Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT32;
20247c478bd9Sstevel@tonic-gate (void) hat_setup(as->a_hat, HAT_ALLOC);
202505d3dc4bSpaulsan hat_join_srd(as->a_hat, args->ex_vp);
20267c478bd9Sstevel@tonic-gate
20277c478bd9Sstevel@tonic-gate /*
20287c478bd9Sstevel@tonic-gate * Finally, write out the contents of the new stack.
20297c478bd9Sstevel@tonic-gate */
2030102033aaSdp78419 error = stk_copyout(args, usrstack - sp_slew, auxvpp, up);
20317c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
20327c478bd9Sstevel@tonic-gate return (error);
20337c478bd9Sstevel@tonic-gate }
2034