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 */
2756726c7eSRobert Mustacchi /* All Rights Reserved */
28ebb8ac07SRobert Mustacchi /*
29*2b395c3cSAndy Fiddaman * Copyright 2015 Garrett D'Amore <garrett@damore.org>
304e18e297SPatrick Mooney * Copyright 2019 Joyent, Inc.
31*2b395c3cSAndy Fiddaman * Copyright 2024 Oxide Computer Company
32ebb8ac07SRobert Mustacchi */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/policy.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/mman.h>
477c478bd9Sstevel@tonic-gate #include <sys/acct.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
537c478bd9Sstevel@tonic-gate #include <sys/vm.h>
542cb27123Saguzovsk #include <sys/lgrp.h>
557c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
567c478bd9Sstevel@tonic-gate #include <sys/exec.h>
57*2b395c3cSAndy Fiddaman #include <sys/execx.h>
587c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
597c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
607c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
617c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
63d4204c85Sraf #include <sys/door.h>
647c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
657c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
667c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
677c478bd9Sstevel@tonic-gate #include <sys/stack.h>
687c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
697c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
707c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
717c478bd9Sstevel@tonic-gate #include <sys/pool.h>
727c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
739acbbeafSnn #include <sys/brand.h>
74134a1f4eSCasper H.S. Dik #include <sys/klpd.h>
75d2a70789SRichard Lowe #include <sys/random.h>
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #include <c2/audit.h>
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #include <vm/hat.h>
807c478bd9Sstevel@tonic-gate #include <vm/anon.h>
817c478bd9Sstevel@tonic-gate #include <vm/as.h>
827c478bd9Sstevel@tonic-gate #include <vm/seg.h>
837c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
84284ce987SPatrick Mooney #include <vm/seg_hole.h>
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #define PRIV_RESET 0x01 /* needs to reset privs */
877c478bd9Sstevel@tonic-gate #define PRIV_SETID 0x02 /* needs to change uids */
887c478bd9Sstevel@tonic-gate #define PRIV_SETUGID 0x04 /* is setuid/setgid/forced privs */
897c478bd9Sstevel@tonic-gate #define PRIV_INCREASE 0x08 /* child runs with more privs */
9045916cd2Sjpk #define MAC_FLAGS 0x10 /* need to adjust MAC flags */
91134a1f4eSCasper H.S. Dik #define PRIV_FORCED 0x20 /* has forced privileges */
927c478bd9Sstevel@tonic-gate
93134a1f4eSCasper H.S. Dik static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *,
94134a1f4eSCasper H.S. Dik priv_set_t *, cred_t *, const char *);
957c478bd9Sstevel@tonic-gate static int hold_execsw(struct execsw *);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate uint_t auxv_hwcap = 0; /* auxv AT_SUN_HWCAP value; determined on the fly */
98ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_2 = 0; /* AT_SUN_HWCAP2 */
9956726c7eSRobert Mustacchi uint_t auxv_hwcap_3 = 0; /* AT_SUN_HWCAP3 */
1007c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1017c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32 = 0; /* 32-bit version of auxv_hwcap */
102ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_2 = 0; /* 32-bit version of auxv_hwcap2 */
10356726c7eSRobert Mustacchi uint_t auxv_hwcap32_3 = 0; /* 32-bit version of auxv_hwcap3 */
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate #define PSUIDFLAGS (SNOCD|SUGID)
1077c478bd9Sstevel@tonic-gate
108d2a70789SRichard Lowe /*
109d2a70789SRichard Lowe * These are consumed within the specific exec modules, but are defined here
110d2a70789SRichard Lowe * because
111d2a70789SRichard Lowe *
112d2a70789SRichard Lowe * 1) The exec modules are unloadable, which would make this near useless.
113d2a70789SRichard Lowe *
114d2a70789SRichard Lowe * 2) We want them to be common across all of them, should more than ELF come
115d2a70789SRichard Lowe * to support them.
116d2a70789SRichard Lowe *
117d2a70789SRichard Lowe * All must be powers of 2.
118d2a70789SRichard Lowe */
119d2a70789SRichard Lowe size_t aslr_max_brk_skew = 16 * 1024 * 1024; /* 16MB */
120d2a70789SRichard Lowe #pragma weak exec_stackgap = aslr_max_stack_skew /* Old, compatible name */
121d2a70789SRichard Lowe size_t aslr_max_stack_skew = 64 * 1024; /* 64KB */
122d2a70789SRichard Lowe
123284ce987SPatrick Mooney /*
124284ce987SPatrick Mooney * Size of guard segment for 64-bit processes and minimum size it can be shrunk
125284ce987SPatrick Mooney * to in the case of grow() operations. These are kept as variables in case
126284ce987SPatrick Mooney * they need to be tuned in an emergency.
127284ce987SPatrick Mooney */
128284ce987SPatrick Mooney size_t stack_guard_seg_sz = 256 * 1024 * 1024;
129284ce987SPatrick Mooney size_t stack_guard_min_sz = 64 * 1024 * 1024;
130284ce987SPatrick Mooney
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * exece() - system call wrapper around exec_common()
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate int
exece(uintptr_t file,const char ** argp,const char ** envp,int flags)135*2b395c3cSAndy Fiddaman exece(uintptr_t file, const char **argp, const char **envp, int flags)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate int error;
1387c478bd9Sstevel@tonic-gate
139*2b395c3cSAndy Fiddaman if ((flags & ~EXEC_DESCRIPTOR) != 0)
140*2b395c3cSAndy Fiddaman return (set_errno(EINVAL));
141*2b395c3cSAndy Fiddaman
142*2b395c3cSAndy Fiddaman if ((flags & EXEC_DESCRIPTOR) != 0) {
143*2b395c3cSAndy Fiddaman /*
144*2b395c3cSAndy Fiddaman * If EXEC_DESCRIPTOR is specified, then the `file`
145*2b395c3cSAndy Fiddaman * parameter is the number of a file descriptor in the current
146*2b395c3cSAndy Fiddaman * process.
147*2b395c3cSAndy Fiddaman */
148*2b395c3cSAndy Fiddaman char *path = NULL;
149*2b395c3cSAndy Fiddaman size_t allocsize;
150*2b395c3cSAndy Fiddaman int fd = (int)file;
151*2b395c3cSAndy Fiddaman vnode_t *vp = NULL;
152*2b395c3cSAndy Fiddaman
153*2b395c3cSAndy Fiddaman if ((error = fgetstartvp(fd, NULL, &vp)) != 0)
154*2b395c3cSAndy Fiddaman return (set_errno(error));
155*2b395c3cSAndy Fiddaman
156*2b395c3cSAndy Fiddaman mutex_enter(&vp->v_lock);
157*2b395c3cSAndy Fiddaman if (vp->v_path != NULL && vp->v_path != vn_vpath_empty) {
158*2b395c3cSAndy Fiddaman allocsize = strlen(vp->v_path) + 1;
159*2b395c3cSAndy Fiddaman path = kmem_alloc(allocsize, KM_NOSLEEP);
160*2b395c3cSAndy Fiddaman if (path == NULL) {
161*2b395c3cSAndy Fiddaman mutex_exit(&vp->v_lock);
162*2b395c3cSAndy Fiddaman VN_RELE(vp);
163*2b395c3cSAndy Fiddaman return (set_errno(ENOMEM));
164*2b395c3cSAndy Fiddaman }
165*2b395c3cSAndy Fiddaman bcopy(vp->v_path, path, allocsize);
166*2b395c3cSAndy Fiddaman }
167*2b395c3cSAndy Fiddaman mutex_exit(&vp->v_lock);
168*2b395c3cSAndy Fiddaman
169*2b395c3cSAndy Fiddaman /*
170*2b395c3cSAndy Fiddaman * In the unlikely event that the descriptor's path is not
171*2b395c3cSAndy Fiddaman * cached, we fall back to using a constructed one.
172*2b395c3cSAndy Fiddaman */
173*2b395c3cSAndy Fiddaman if (path == NULL) {
174*2b395c3cSAndy Fiddaman /* 8 for "/dev/fd/", 10 for %d, + \0 == 19 */
175*2b395c3cSAndy Fiddaman allocsize = 20;
176*2b395c3cSAndy Fiddaman path = kmem_alloc(allocsize, KM_NOSLEEP);
177*2b395c3cSAndy Fiddaman if (path == NULL) {
178*2b395c3cSAndy Fiddaman VN_RELE(vp);
179*2b395c3cSAndy Fiddaman return (set_errno(ENOMEM));
180*2b395c3cSAndy Fiddaman }
181*2b395c3cSAndy Fiddaman (void) snprintf(path, allocsize, "/dev/fd/%d", fd);
182*2b395c3cSAndy Fiddaman }
183*2b395c3cSAndy Fiddaman
184*2b395c3cSAndy Fiddaman error = exec_common(path, argp, envp, vp, EBA_NONE);
185*2b395c3cSAndy Fiddaman VN_RELE(vp);
186*2b395c3cSAndy Fiddaman kmem_free(path, allocsize);
187*2b395c3cSAndy Fiddaman } else {
188*2b395c3cSAndy Fiddaman const char *fname = (const char *)file;
189*2b395c3cSAndy Fiddaman
190*2b395c3cSAndy Fiddaman error = exec_common(fname, argp, envp, NULL, EBA_NONE);
191*2b395c3cSAndy Fiddaman }
192*2b395c3cSAndy Fiddaman
1937c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate int
exec_common(const char * fname,const char ** argp,const char ** envp,vnode_t * vp,int brand_action)1979acbbeafSnn exec_common(const char *fname, const char **argp, const char **envp,
198*2b395c3cSAndy Fiddaman vnode_t *vp, int brand_action)
1997c478bd9Sstevel@tonic-gate {
200*2b395c3cSAndy Fiddaman vnode_t *dir = NULL, *tmpvp = NULL;
2017c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
2027c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
2037c478bd9Sstevel@tonic-gate struct user *up = PTOU(p);
2044e18e297SPatrick Mooney size_t execsz; /* temporary count of exec size */
2057c478bd9Sstevel@tonic-gate int i;
2067c478bd9Sstevel@tonic-gate int error;
2077c478bd9Sstevel@tonic-gate char exec_file[MAXCOMLEN+1];
2087c478bd9Sstevel@tonic-gate struct pathname pn;
2097c478bd9Sstevel@tonic-gate struct pathname resolvepn;
2107c478bd9Sstevel@tonic-gate struct uarg args;
2117c478bd9Sstevel@tonic-gate struct execa ua;
2127c478bd9Sstevel@tonic-gate k_sigset_t savedmask;
2137c478bd9Sstevel@tonic-gate lwpdir_t *lwpdir = NULL;
2146eb30ec3SRoger A. Faulkner tidhash_t *tidhash;
2157c478bd9Sstevel@tonic-gate lwpdir_t *old_lwpdir = NULL;
2167c478bd9Sstevel@tonic-gate uint_t old_lwpdir_sz;
2176eb30ec3SRoger A. Faulkner tidhash_t *old_tidhash;
2187c478bd9Sstevel@tonic-gate uint_t old_tidhash_sz;
2196eb30ec3SRoger A. Faulkner ret_tidhash_t *ret_tidhash;
2207c478bd9Sstevel@tonic-gate lwpent_t *lep;
221fd9e7635Sedp boolean_t brandme = B_FALSE;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * exec() is not supported for the /proc agent lwp.
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate if (curthread == p->p_agenttp)
2277c478bd9Sstevel@tonic-gate return (ENOTSUP);
2287c478bd9Sstevel@tonic-gate
2299acbbeafSnn if (brand_action != EBA_NONE) {
2309acbbeafSnn /*
2319acbbeafSnn * Brand actions are not supported for processes that are not
2329acbbeafSnn * running in a branded zone.
2339acbbeafSnn */
2349acbbeafSnn if (!ZONE_IS_BRANDED(p->p_zone))
2359acbbeafSnn return (ENOTSUP);
2369acbbeafSnn
2379acbbeafSnn if (brand_action == EBA_NATIVE) {
2389acbbeafSnn /* Only branded processes can be unbranded */
2399acbbeafSnn if (!PROC_IS_BRANDED(p))
2409acbbeafSnn return (ENOTSUP);
2419acbbeafSnn } else {
2429acbbeafSnn /* Only unbranded processes can be branded */
2439acbbeafSnn if (PROC_IS_BRANDED(p))
2449acbbeafSnn return (ENOTSUP);
245fd9e7635Sedp brandme = B_TRUE;
2469acbbeafSnn }
2479acbbeafSnn } else {
2489acbbeafSnn /*
2499acbbeafSnn * If this is a native zone, or if the process is already
2509acbbeafSnn * branded, then we don't need to do anything. If this is
2519acbbeafSnn * a native process in a branded zone, we need to brand the
2529acbbeafSnn * process as it exec()s the new binary.
2539acbbeafSnn */
2549acbbeafSnn if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p))
255fd9e7635Sedp brandme = B_TRUE;
2569acbbeafSnn }
2579acbbeafSnn
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * Inform /proc that an exec() has started.
2607c478bd9Sstevel@tonic-gate * Hold signals that are ignored by default so that we will
2617c478bd9Sstevel@tonic-gate * not be interrupted by a signal that will be ignored after
2627c478bd9Sstevel@tonic-gate * successful completion of gexec().
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2657c478bd9Sstevel@tonic-gate prexecstart();
2667c478bd9Sstevel@tonic-gate schedctl_finish_sigblock(curthread);
2677c478bd9Sstevel@tonic-gate savedmask = curthread->t_hold;
2687c478bd9Sstevel@tonic-gate sigorset(&curthread->t_hold, &ignoredefault);
2697c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2707c478bd9Sstevel@tonic-gate
271*2b395c3cSAndy Fiddaman if (vp != NULL) {
272*2b395c3cSAndy Fiddaman /*
273*2b395c3cSAndy Fiddaman * When a vnode is passed in we take an extra hold here and
274*2b395c3cSAndy Fiddaman * release it before returning. This means that callers don't
275*2b395c3cSAndy Fiddaman * need to account for the reference changing over the call.
276*2b395c3cSAndy Fiddaman */
277*2b395c3cSAndy Fiddaman VN_HOLD(vp);
278*2b395c3cSAndy Fiddaman pn_alloc(&pn);
279*2b395c3cSAndy Fiddaman pn_alloc(&resolvepn);
280*2b395c3cSAndy Fiddaman VERIFY0(pn_set(&pn, fname));
281*2b395c3cSAndy Fiddaman VERIFY0(pn_set(&resolvepn, fname));
282*2b395c3cSAndy Fiddaman } else {
283*2b395c3cSAndy Fiddaman /*
284*2b395c3cSAndy Fiddaman * Look up path name and remember last component for later.
285*2b395c3cSAndy Fiddaman * To help coreadm expand its %d token, we attempt to save
286*2b395c3cSAndy Fiddaman * the directory containing the executable in p_execdir. The
287*2b395c3cSAndy Fiddaman * first call to lookuppn() may fail and return EINVAL because
288*2b395c3cSAndy Fiddaman * dirvpp is non-NULL. In that case, we make a second call to
289*2b395c3cSAndy Fiddaman * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
290*2b395c3cSAndy Fiddaman * but coreadm is allowed to expand %d to the empty string and
291*2b395c3cSAndy Fiddaman * there are other cases in which that failure may occur.
292*2b395c3cSAndy Fiddaman */
2937c478bd9Sstevel@tonic-gate if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
2947c478bd9Sstevel@tonic-gate goto out;
2957c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn);
296*2b395c3cSAndy Fiddaman error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp);
297*2b395c3cSAndy Fiddaman if (error != 0) {
2987c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
2997c478bd9Sstevel@tonic-gate pn_free(&pn);
300*2b395c3cSAndy Fiddaman if (error != EINVAL)
301*2b395c3cSAndy Fiddaman goto out;
302*2b395c3cSAndy Fiddaman
303*2b395c3cSAndy Fiddaman dir = NULL;
304*2b395c3cSAndy Fiddaman if ((error = pn_get((char *)fname, UIO_USERSPACE,
305*2b395c3cSAndy Fiddaman &pn)) != 0) {
306*2b395c3cSAndy Fiddaman goto out;
307*2b395c3cSAndy Fiddaman }
308*2b395c3cSAndy Fiddaman pn_alloc(&resolvepn);
309*2b395c3cSAndy Fiddaman if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
310*2b395c3cSAndy Fiddaman &vp)) != 0) {
311*2b395c3cSAndy Fiddaman pn_free(&resolvepn);
312*2b395c3cSAndy Fiddaman pn_free(&pn);
313*2b395c3cSAndy Fiddaman goto out;
314*2b395c3cSAndy Fiddaman }
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate }
317*2b395c3cSAndy Fiddaman
3187c478bd9Sstevel@tonic-gate if (vp == NULL) {
3197c478bd9Sstevel@tonic-gate if (dir != NULL)
3207c478bd9Sstevel@tonic-gate VN_RELE(dir);
3217c478bd9Sstevel@tonic-gate error = ENOENT;
3227c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
3237c478bd9Sstevel@tonic-gate pn_free(&pn);
3247c478bd9Sstevel@tonic-gate goto out;
3257c478bd9Sstevel@tonic-gate }
326a3d40fb8Scasper
327ddf7fe95Scasper if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) {
328ddf7fe95Scasper if (dir != NULL)
329ddf7fe95Scasper VN_RELE(dir);
330ddf7fe95Scasper pn_free(&resolvepn);
331ddf7fe95Scasper pn_free(&pn);
332ddf7fe95Scasper VN_RELE(vp);
333ddf7fe95Scasper goto out;
334ddf7fe95Scasper }
335ddf7fe95Scasper
336a3d40fb8Scasper /*
337a3d40fb8Scasper * We do not allow executing files in attribute directories.
338a3d40fb8Scasper * We test this by determining whether the resolved path
339a3d40fb8Scasper * contains a "/" when we're in an attribute directory;
340a3d40fb8Scasper * only if the pathname does not contain a "/" the resolved path
341a3d40fb8Scasper * points to a file in the current working (attribute) directory.
342a3d40fb8Scasper */
343a3d40fb8Scasper if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
344a3d40fb8Scasper strchr(resolvepn.pn_path, '/') == NULL) {
345a3d40fb8Scasper if (dir != NULL)
346a3d40fb8Scasper VN_RELE(dir);
347a3d40fb8Scasper error = EACCES;
348a3d40fb8Scasper pn_free(&resolvepn);
349a3d40fb8Scasper pn_free(&pn);
350a3d40fb8Scasper VN_RELE(vp);
351a3d40fb8Scasper goto out;
352a3d40fb8Scasper }
353a3d40fb8Scasper
3547c478bd9Sstevel@tonic-gate bzero(exec_file, MAXCOMLEN+1);
3557c478bd9Sstevel@tonic-gate (void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
3567c478bd9Sstevel@tonic-gate bzero(&args, sizeof (args));
3577c478bd9Sstevel@tonic-gate args.pathname = resolvepn.pn_path;
3587c478bd9Sstevel@tonic-gate /* don't free resolvepn until we are done with args */
3597c478bd9Sstevel@tonic-gate pn_free(&pn);
3607c478bd9Sstevel@tonic-gate
361134a1f4eSCasper H.S. Dik /*
362134a1f4eSCasper H.S. Dik * If we're running in a profile shell, then call pfexecd.
363134a1f4eSCasper H.S. Dik */
364134a1f4eSCasper H.S. Dik if ((CR_FLAGS(p->p_cred) & PRIV_PFEXEC) != 0) {
365134a1f4eSCasper H.S. Dik error = pfexec_call(p->p_cred, &resolvepn, &args.pfcred,
366134a1f4eSCasper H.S. Dik &args.scrubenv);
367134a1f4eSCasper H.S. Dik
368134a1f4eSCasper H.S. Dik /* Returning errno in case we're not allowed to execute. */
369134a1f4eSCasper H.S. Dik if (error > 0) {
370134a1f4eSCasper H.S. Dik if (dir != NULL)
371134a1f4eSCasper H.S. Dik VN_RELE(dir);
372134a1f4eSCasper H.S. Dik pn_free(&resolvepn);
373134a1f4eSCasper H.S. Dik VN_RELE(vp);
374134a1f4eSCasper H.S. Dik goto out;
375134a1f4eSCasper H.S. Dik }
376134a1f4eSCasper H.S. Dik
377134a1f4eSCasper H.S. Dik /* Don't change the credentials when using old ptrace. */
378134a1f4eSCasper H.S. Dik if (args.pfcred != NULL &&
379134a1f4eSCasper H.S. Dik (p->p_proc_flag & P_PR_PTRACE) != 0) {
380134a1f4eSCasper H.S. Dik crfree(args.pfcred);
381134a1f4eSCasper H.S. Dik args.pfcred = NULL;
382134a1f4eSCasper H.S. Dik args.scrubenv = B_FALSE;
383134a1f4eSCasper H.S. Dik }
384134a1f4eSCasper H.S. Dik }
385134a1f4eSCasper H.S. Dik
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate * Specific exec handlers, or policies determined via
3887c478bd9Sstevel@tonic-gate * /etc/system may override the historical default.
3897c478bd9Sstevel@tonic-gate */
3907c478bd9Sstevel@tonic-gate args.stk_prot = PROT_ZFOD;
3917c478bd9Sstevel@tonic-gate args.dat_prot = PROT_ZFOD;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate CPU_STATS_ADD_K(sys, sysexec, 1);
3947c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec, char *, args.pathname);
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate ua.fname = fname;
3977c478bd9Sstevel@tonic-gate ua.argp = argp;
3987c478bd9Sstevel@tonic-gate ua.envp = envp;
3997c478bd9Sstevel@tonic-gate
4009acbbeafSnn /* If necessary, brand this process before we start the exec. */
401fd9e7635Sedp if (brandme)
4029acbbeafSnn brand_setbrand(p);
4039acbbeafSnn
4047c478bd9Sstevel@tonic-gate if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
4059acbbeafSnn exec_file, p->p_cred, brand_action)) != 0) {
406fd9e7635Sedp if (brandme)
407e9f7cbf0SVamsi Nagineni brand_clearbrand(p, B_FALSE);
4087c478bd9Sstevel@tonic-gate VN_RELE(vp);
4097c478bd9Sstevel@tonic-gate if (dir != NULL)
4107c478bd9Sstevel@tonic-gate VN_RELE(dir);
4117c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
4127c478bd9Sstevel@tonic-gate goto fail;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * Free floating point registers (sun4u only)
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate ASSERT(lwp != NULL);
4197c478bd9Sstevel@tonic-gate lwp_freeregs(lwp, 1);
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate /*
4220baeff3dSrab * Free thread and process context ops.
4237c478bd9Sstevel@tonic-gate */
4247c478bd9Sstevel@tonic-gate if (curthread->t_ctx)
4257c478bd9Sstevel@tonic-gate freectx(curthread, 1);
4260baeff3dSrab if (p->p_pctx)
4270baeff3dSrab freepctx(p, 1);
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate * Remember file name for accounting; clear any cached DTrace predicate.
4317c478bd9Sstevel@tonic-gate */
4327c478bd9Sstevel@tonic-gate up->u_acflag &= ~AFORK;
4337c478bd9Sstevel@tonic-gate bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
4347e12ceb3SToomas Soome curthread->t_predcache = 0;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * Clear contract template state
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(lwp);
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate * Save the directory in which we found the executable for expanding
4437c478bd9Sstevel@tonic-gate * the %d token used in core file patterns.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
4467c478bd9Sstevel@tonic-gate tmpvp = p->p_execdir;
4477c478bd9Sstevel@tonic-gate p->p_execdir = dir;
4487c478bd9Sstevel@tonic-gate if (p->p_execdir != NULL)
4497c478bd9Sstevel@tonic-gate VN_HOLD(p->p_execdir);
4507c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate if (tmpvp != NULL)
4537c478bd9Sstevel@tonic-gate VN_RELE(tmpvp);
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate * Reset stack state to the user stack, clear set of signals
4577c478bd9Sstevel@tonic-gate * caught on the signal stack, and reset list of signals that
4587c478bd9Sstevel@tonic-gate * restart system calls; the new program's environment should
4597c478bd9Sstevel@tonic-gate * not be affected by detritus from the old program. Any
4607c478bd9Sstevel@tonic-gate * pending held signals remain held, so don't clear t_hold.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
4637c478bd9Sstevel@tonic-gate lwp->lwp_oldcontext = 0;
4647c478bd9Sstevel@tonic-gate lwp->lwp_ustack = 0;
4657c478bd9Sstevel@tonic-gate lwp->lwp_old_stk_ctl = 0;
4667c478bd9Sstevel@tonic-gate sigemptyset(&up->u_signodefer);
4677c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigonstack);
4687c478bd9Sstevel@tonic-gate sigemptyset(&up->u_sigresethand);
4697c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
4707c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
4717c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * Make saved resource limit == current resource limit.
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate for (i = 0; i < RLIM_NLIMITS; i++) {
4777c478bd9Sstevel@tonic-gate /*CONSTCOND*/
4787c478bd9Sstevel@tonic-gate if (RLIM_SAVED(i)) {
4797c478bd9Sstevel@tonic-gate (void) rctl_rlimit_get(rctlproc_legacy[i], p,
4807c478bd9Sstevel@tonic-gate &up->u_saved_rlimit[i]);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate * If the action was to catch the signal, then the action
4867c478bd9Sstevel@tonic-gate * must be reset to SIG_DFL.
4877c478bd9Sstevel@tonic-gate */
4887c478bd9Sstevel@tonic-gate sigdefault(p);
4897c478bd9Sstevel@tonic-gate p->p_flag &= ~(SNOWAIT|SJCTL);
4907c478bd9Sstevel@tonic-gate p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
4917c478bd9Sstevel@tonic-gate up->u_signal[SIGCLD - 1] = SIG_DFL;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * Delete the dot4 sigqueues/signotifies.
4957c478bd9Sstevel@tonic-gate */
4967c478bd9Sstevel@tonic-gate sigqfree(p);
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate mutex_enter(&p->p_pflock);
5017c478bd9Sstevel@tonic-gate p->p_prof.pr_base = NULL;
5027c478bd9Sstevel@tonic-gate p->p_prof.pr_size = 0;
5037c478bd9Sstevel@tonic-gate p->p_prof.pr_off = 0;
5047c478bd9Sstevel@tonic-gate p->p_prof.pr_scale = 0;
5057c478bd9Sstevel@tonic-gate p->p_prof.pr_samples = 0;
5067c478bd9Sstevel@tonic-gate mutex_exit(&p->p_pflock);
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate ASSERT(curthread->t_schedctl == NULL);
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate #if defined(__sparc)
5117c478bd9Sstevel@tonic-gate if (p->p_utraps != NULL)
5127c478bd9Sstevel@tonic-gate utrap_free(p);
5137c478bd9Sstevel@tonic-gate #endif /* __sparc */
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate * Close all close-on-exec files.
5177c478bd9Sstevel@tonic-gate */
5187c478bd9Sstevel@tonic-gate close_exec(P_FINFO(p));
5197c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
5209acbbeafSnn
521fd9e7635Sedp /* Unbrand ourself if necessary. */
522fd9e7635Sedp if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE))
523e9f7cbf0SVamsi Nagineni brand_clearbrand(p, B_FALSE);
5249acbbeafSnn
5257c478bd9Sstevel@tonic-gate setregs(&args);
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate /* Mark this as an executable vnode */
5287c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock);
5297c478bd9Sstevel@tonic-gate vp->v_flag |= VVMEXEC;
5307c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock);
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate VN_RELE(vp);
5337c478bd9Sstevel@tonic-gate if (dir != NULL)
5347c478bd9Sstevel@tonic-gate VN_RELE(dir);
5357c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate * Allocate a new lwp directory and lwpid hash table if necessary.
5397c478bd9Sstevel@tonic-gate */
5407c478bd9Sstevel@tonic-gate if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
5417c478bd9Sstevel@tonic-gate lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
5427c478bd9Sstevel@tonic-gate lwpdir->ld_next = lwpdir + 1;
5436eb30ec3SRoger A. Faulkner tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP);
5447c478bd9Sstevel@tonic-gate if (p->p_lwpdir != NULL)
5457c478bd9Sstevel@tonic-gate lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
5467c478bd9Sstevel@tonic-gate else
5477c478bd9Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5509acbbeafSnn if (PROC_IS_BRANDED(p))
5519acbbeafSnn BROP(p)->b_exec();
5529acbbeafSnn
5537c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
5547c478bd9Sstevel@tonic-gate prbarrier(p);
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate * Reset lwp id to the default value of 1.
5587c478bd9Sstevel@tonic-gate * This is a single-threaded process now
5597c478bd9Sstevel@tonic-gate * and lwp #1 is lwp_wait()able by default.
5607c478bd9Sstevel@tonic-gate * The t_unpark flag should not be inherited.
5617c478bd9Sstevel@tonic-gate */
5627c478bd9Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
5637c478bd9Sstevel@tonic-gate curthread->t_tid = 1;
5642cb27123Saguzovsk kpreempt_disable();
5652cb27123Saguzovsk ASSERT(curthread->t_lpl != NULL);
5662cb27123Saguzovsk p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid;
5672cb27123Saguzovsk kpreempt_enable();
5682cb27123Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) {
5692cb27123Saguzovsk lgrp_update_trthr_migrations(1);
5702cb27123Saguzovsk }
5717c478bd9Sstevel@tonic-gate curthread->t_unpark = 0;
5727c478bd9Sstevel@tonic-gate curthread->t_proc_flag |= TP_TWAIT;
5737c478bd9Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_DAEMON; /* daemons shouldn't exec */
5747c478bd9Sstevel@tonic-gate p->p_lwpdaemon = 0; /* but oh well ... */
5757c478bd9Sstevel@tonic-gate p->p_lwpid = 1;
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate * Install the newly-allocated lwp directory and lwpid hash table
5797c478bd9Sstevel@tonic-gate * and insert the current thread into the new hash table.
5807c478bd9Sstevel@tonic-gate */
5817c478bd9Sstevel@tonic-gate if (lwpdir != NULL) {
5827c478bd9Sstevel@tonic-gate old_lwpdir = p->p_lwpdir;
5837c478bd9Sstevel@tonic-gate old_lwpdir_sz = p->p_lwpdir_sz;
5847c478bd9Sstevel@tonic-gate old_tidhash = p->p_tidhash;
5857c478bd9Sstevel@tonic-gate old_tidhash_sz = p->p_tidhash_sz;
5867c478bd9Sstevel@tonic-gate p->p_lwpdir = p->p_lwpfree = lwpdir;
5877c478bd9Sstevel@tonic-gate p->p_lwpdir_sz = 2;
5887c478bd9Sstevel@tonic-gate lep->le_thread = curthread;
5897c478bd9Sstevel@tonic-gate lep->le_lwpid = curthread->t_tid;
5907c478bd9Sstevel@tonic-gate lep->le_start = curthread->t_start;
5916eb30ec3SRoger A. Faulkner lwp_hash_in(p, lep, tidhash, 2, 0);
5926eb30ec3SRoger A. Faulkner p->p_tidhash = tidhash;
5936eb30ec3SRoger A. Faulkner p->p_tidhash_sz = 2;
5947c478bd9Sstevel@tonic-gate }
5956eb30ec3SRoger A. Faulkner ret_tidhash = p->p_ret_tidhash;
5966eb30ec3SRoger A. Faulkner p->p_ret_tidhash = NULL;
5979acbbeafSnn
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate * Restore the saved signal mask and
6007c478bd9Sstevel@tonic-gate * inform /proc that the exec() has finished.
6017c478bd9Sstevel@tonic-gate */
6027c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask;
6037c478bd9Sstevel@tonic-gate prexecend();
6047c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6057c478bd9Sstevel@tonic-gate if (old_lwpdir) {
6067c478bd9Sstevel@tonic-gate kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
6076eb30ec3SRoger A. Faulkner kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t));
6086eb30ec3SRoger A. Faulkner }
6096eb30ec3SRoger A. Faulkner while (ret_tidhash != NULL) {
6106eb30ec3SRoger A. Faulkner ret_tidhash_t *next = ret_tidhash->rth_next;
6116eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash->rth_tidhash,
6126eb30ec3SRoger A. Faulkner ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
6136eb30ec3SRoger A. Faulkner kmem_free(ret_tidhash, sizeof (*ret_tidhash));
6146eb30ec3SRoger A. Faulkner ret_tidhash = next;
6157c478bd9Sstevel@tonic-gate }
6169acbbeafSnn
6177c478bd9Sstevel@tonic-gate ASSERT(error == 0);
6187c478bd9Sstevel@tonic-gate DTRACE_PROC(exec__success);
6197c478bd9Sstevel@tonic-gate return (0);
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate fail:
6227c478bd9Sstevel@tonic-gate DTRACE_PROC1(exec__failure, int, error);
6237c478bd9Sstevel@tonic-gate out: /* error return */
6247c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
6257c478bd9Sstevel@tonic-gate curthread->t_hold = savedmask;
6267c478bd9Sstevel@tonic-gate prexecend();
6277c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6287c478bd9Sstevel@tonic-gate ASSERT(error != 0);
6297c478bd9Sstevel@tonic-gate return (error);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate * Perform generic exec duties and switchout to object-file specific
6357c478bd9Sstevel@tonic-gate * handler.
6367c478bd9Sstevel@tonic-gate */
6377c478bd9Sstevel@tonic-gate int
gexec(struct vnode ** vpp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,size_t * execsz,caddr_t exec_file,struct cred * cred,int brand_action)6387c478bd9Sstevel@tonic-gate gexec(
6397c478bd9Sstevel@tonic-gate struct vnode **vpp,
6407c478bd9Sstevel@tonic-gate struct execa *uap,
6417c478bd9Sstevel@tonic-gate struct uarg *args,
6427c478bd9Sstevel@tonic-gate struct intpdata *idatap,
6437c478bd9Sstevel@tonic-gate int level,
6444e18e297SPatrick Mooney size_t *execsz,
6457c478bd9Sstevel@tonic-gate caddr_t exec_file,
6469acbbeafSnn struct cred *cred,
6479acbbeafSnn int brand_action)
6487c478bd9Sstevel@tonic-gate {
649a4aeef46SDonghai Qiao struct vnode *vp, *execvp = NULL;
6507c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
6517c478bd9Sstevel@tonic-gate struct execsw *eswp;
6527c478bd9Sstevel@tonic-gate int error = 0;
6537c478bd9Sstevel@tonic-gate int suidflags = 0;
6547c478bd9Sstevel@tonic-gate ssize_t resid;
6557c478bd9Sstevel@tonic-gate uid_t uid, gid;
6567c478bd9Sstevel@tonic-gate struct vattr vattr;
6577c478bd9Sstevel@tonic-gate char magbuf[MAGIC_BYTES];
6587c478bd9Sstevel@tonic-gate int setid;
6597c478bd9Sstevel@tonic-gate cred_t *oldcred, *newcred = NULL;
6607c478bd9Sstevel@tonic-gate int privflags = 0;
661cc4b03b5Scasper int setidfl;
662134a1f4eSCasper H.S. Dik priv_set_t fset;
663d2a70789SRichard Lowe secflagset_t old_secflags;
664d2a70789SRichard Lowe
665d2a70789SRichard Lowe secflags_copy(&old_secflags, &pp->p_secflags.psf_effective);
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate * If the SNOCD or SUGID flag is set, turn it off and remember the
6697c478bd9Sstevel@tonic-gate * previous setting so we can restore it if we encounter an error.
6707c478bd9Sstevel@tonic-gate */
6717c478bd9Sstevel@tonic-gate if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
6727c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
6737c478bd9Sstevel@tonic-gate suidflags = pp->p_flag & PSUIDFLAGS;
6747c478bd9Sstevel@tonic-gate pp->p_flag &= ~PSUIDFLAGS;
6757c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate if ((error = execpermissions(*vpp, &vattr, args)) != 0)
679a4aeef46SDonghai Qiao goto bad_noclose;
6807c478bd9Sstevel@tonic-gate
681a4aeef46SDonghai Qiao /* need to open vnode for stateful file systems */
682da6c28aaSamw if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0)
683a4aeef46SDonghai Qiao goto bad_noclose;
6847c478bd9Sstevel@tonic-gate vp = *vpp;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate * Note: to support binary compatibility with SunOS a.out
6887c478bd9Sstevel@tonic-gate * executables, we read in the first four bytes, as the
6897c478bd9Sstevel@tonic-gate * magic number is in bytes 2-3.
6907c478bd9Sstevel@tonic-gate */
6917c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
6927c478bd9Sstevel@tonic-gate (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
6937c478bd9Sstevel@tonic-gate goto bad;
6947c478bd9Sstevel@tonic-gate if (resid != 0)
6957c478bd9Sstevel@tonic-gate goto bad;
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate if ((eswp = findexec_by_hdr(magbuf)) == NULL)
6987c478bd9Sstevel@tonic-gate goto bad;
6997c478bd9Sstevel@tonic-gate
7007c478bd9Sstevel@tonic-gate if (level == 0 &&
701134a1f4eSCasper H.S. Dik (privflags = execsetid(vp, &vattr, &uid, &gid, &fset,
702134a1f4eSCasper H.S. Dik args->pfcred == NULL ? cred : args->pfcred, args->pathname)) != 0) {
703134a1f4eSCasper H.S. Dik
704134a1f4eSCasper H.S. Dik /* Pfcred is a credential with a ref count of 1 */
7057c478bd9Sstevel@tonic-gate
706134a1f4eSCasper H.S. Dik if (args->pfcred != NULL) {
707134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE|PRIV_RESET;
708134a1f4eSCasper H.S. Dik newcred = cred = args->pfcred;
709134a1f4eSCasper H.S. Dik } else {
710134a1f4eSCasper H.S. Dik newcred = cred = crdup(cred);
711134a1f4eSCasper H.S. Dik }
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate /* If we can, drop the PA bit */
7147c478bd9Sstevel@tonic-gate if ((privflags & PRIV_RESET) != 0)
7157c478bd9Sstevel@tonic-gate priv_adjust_PA(cred);
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate if (privflags & PRIV_SETID) {
7187c478bd9Sstevel@tonic-gate cred->cr_uid = uid;
7197c478bd9Sstevel@tonic-gate cred->cr_gid = gid;
7207c478bd9Sstevel@tonic-gate cred->cr_suid = uid;
7217c478bd9Sstevel@tonic-gate cred->cr_sgid = gid;
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate
72445916cd2Sjpk if (privflags & MAC_FLAGS) {
72545916cd2Sjpk if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
72645916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE;
72745916cd2Sjpk CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
72845916cd2Sjpk }
72945916cd2Sjpk
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate * Implement the privilege updates:
7327c478bd9Sstevel@tonic-gate *
7337c478bd9Sstevel@tonic-gate * Restrict with L:
7347c478bd9Sstevel@tonic-gate *
7357c478bd9Sstevel@tonic-gate * I' = I & L
7367c478bd9Sstevel@tonic-gate *
7377c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A
7387c478bd9Sstevel@tonic-gate *
739134a1f4eSCasper H.S. Dik * But if running under ptrace, we cap I and F with P.
7407c478bd9Sstevel@tonic-gate */
741134a1f4eSCasper H.S. Dik if ((privflags & (PRIV_RESET|PRIV_FORCED)) != 0) {
7427c478bd9Sstevel@tonic-gate if ((privflags & PRIV_INCREASE) != 0 &&
743134a1f4eSCasper H.S. Dik (pp->p_proc_flag & P_PR_PTRACE) != 0) {
7447c478bd9Sstevel@tonic-gate priv_intersect(&CR_OPPRIV(cred),
745d3e55dcdSgww &CR_IPRIV(cred));
746134a1f4eSCasper H.S. Dik priv_intersect(&CR_OPPRIV(cred), &fset);
747134a1f4eSCasper H.S. Dik }
7487c478bd9Sstevel@tonic-gate priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
7497c478bd9Sstevel@tonic-gate CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
750134a1f4eSCasper H.S. Dik if (privflags & PRIV_FORCED) {
751134a1f4eSCasper H.S. Dik priv_set_PA(cred);
752134a1f4eSCasper H.S. Dik priv_union(&fset, &CR_EPRIV(cred));
753134a1f4eSCasper H.S. Dik priv_union(&fset, &CR_PPRIV(cred));
754134a1f4eSCasper H.S. Dik }
7557c478bd9Sstevel@tonic-gate priv_adjust_PA(cred);
7567c478bd9Sstevel@tonic-gate }
757134a1f4eSCasper H.S. Dik } else if (level == 0 && args->pfcred != NULL) {
758134a1f4eSCasper H.S. Dik newcred = cred = args->pfcred;
759134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE;
760134a1f4eSCasper H.S. Dik /* pfcred is not forced to adhere to these settings */
761134a1f4eSCasper H.S. Dik priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
762134a1f4eSCasper H.S. Dik CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
763134a1f4eSCasper H.S. Dik priv_adjust_PA(cred);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate
766d2a70789SRichard Lowe /* The new image gets the inheritable secflags as its secflags */
767d2a70789SRichard Lowe secflags_promote(pp);
768d2a70789SRichard Lowe
7697c478bd9Sstevel@tonic-gate /* SunOS 4.x buy-back */
7707c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
7717c478bd9Sstevel@tonic-gate (vattr.va_mode & (VSUID|VSGID))) {
77218c39a06SJason King char path[MAXNAMELEN];
77318c39a06SJason King refstr_t *mntpt = NULL;
77418c39a06SJason King int ret = -1;
77518c39a06SJason King
77618c39a06SJason King bzero(path, sizeof (path));
77718c39a06SJason King zone_hold(pp->p_zone);
77818c39a06SJason King
77918c39a06SJason King ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path,
78018c39a06SJason King sizeof (path), cred);
78118c39a06SJason King
78218c39a06SJason King /* fallback to mountpoint if a path can't be found */
78318c39a06SJason King if ((ret != 0) || (ret == 0 && path[0] == '\0'))
78418c39a06SJason King mntpt = vfs_getmntpoint(vp->v_vfsp);
78518c39a06SJason King
78618c39a06SJason King if (mntpt == NULL)
78718c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE,
78818c39a06SJason King "!uid %d: setuid execution not allowed, "
78918c39a06SJason King "file=%s", cred->cr_uid, path);
79018c39a06SJason King else
79118c39a06SJason King zcmn_err(pp->p_zone->zone_id, CE_NOTE,
79218c39a06SJason King "!uid %d: setuid execution not allowed, "
79318c39a06SJason King "fs=%s, file=%s", cred->cr_uid,
79418c39a06SJason King ZONE_PATH_TRANSLATE(refstr_value(mntpt),
79518c39a06SJason King pp->p_zone), exec_file);
79618c39a06SJason King
79718c39a06SJason King if (!INGLOBALZONE(pp)) {
79818c39a06SJason King /* zone_rootpath always has trailing / */
79918c39a06SJason King if (mntpt == NULL)
80018c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d "
80118c39a06SJason King "setuid execution not allowed, file=%s%s",
80218c39a06SJason King pp->p_zone->zone_name, cred->cr_uid,
80318c39a06SJason King pp->p_zone->zone_rootpath, path + 1);
80418c39a06SJason King else
80518c39a06SJason King cmn_err(CE_NOTE, "!zone: %s, uid: %d "
80618c39a06SJason King "setuid execution not allowed, fs=%s, "
80718c39a06SJason King "file=%s", pp->p_zone->zone_name,
80818c39a06SJason King cred->cr_uid, refstr_value(mntpt),
80918c39a06SJason King exec_file);
81018c39a06SJason King }
81118c39a06SJason King
81218c39a06SJason King if (mntpt != NULL)
81318c39a06SJason King refstr_rele(mntpt);
81418c39a06SJason King
81518c39a06SJason King zone_rele(pp->p_zone);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * execsetid() told us whether or not we had to change the
8207c478bd9Sstevel@tonic-gate * credentials of the process. In privflags, it told us
8217c478bd9Sstevel@tonic-gate * whether we gained any privileges or executed a set-uid executable.
8227c478bd9Sstevel@tonic-gate */
823134a1f4eSCasper H.S. Dik setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE|PRIV_FORCED));
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate * Use /etc/system variable to determine if the stack
8277c478bd9Sstevel@tonic-gate * should be marked as executable by default.
8287c478bd9Sstevel@tonic-gate */
829d2a70789SRichard Lowe if ((noexec_user_stack != 0) ||
830d2a70789SRichard Lowe secflag_enabled(pp, PROC_SEC_NOEXECSTACK))
8317c478bd9Sstevel@tonic-gate args->stk_prot &= ~PROT_EXEC;
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
83405d3dc4bSpaulsan args->ex_vp = vp;
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate * Traditionally, the setid flags told the sub processes whether
8387c478bd9Sstevel@tonic-gate * the file just executed was set-uid or set-gid; this caused
8397c478bd9Sstevel@tonic-gate * some confusion as the 'setid' flag did not match the SUGID
8407c478bd9Sstevel@tonic-gate * process flag which is only set when the uids/gids do not match.
8417c478bd9Sstevel@tonic-gate * A script set-gid/set-uid to the real uid/gid would start with
8427c478bd9Sstevel@tonic-gate * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
8437c478bd9Sstevel@tonic-gate * Now we flag those cases where the calling process cannot
8447c478bd9Sstevel@tonic-gate * be trusted to influence the newly exec'ed process, either
8457c478bd9Sstevel@tonic-gate * because it runs with more privileges or when the uids/gids
8467c478bd9Sstevel@tonic-gate * do in fact not match.
8477c478bd9Sstevel@tonic-gate * This also makes the runtime linker agree with the on exec
8487c478bd9Sstevel@tonic-gate * values of SNOCD and SUGID.
8497c478bd9Sstevel@tonic-gate */
850cc4b03b5Scasper setidfl = 0;
851cc4b03b5Scasper if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
852cc4b03b5Scasper !supgroupmember(cred->cr_gid, cred))) {
853cc4b03b5Scasper setidfl |= EXECSETID_UGIDS;
854cc4b03b5Scasper }
855cc4b03b5Scasper if (setid & PRIV_SETUGID)
856cc4b03b5Scasper setidfl |= EXECSETID_SETID;
857134a1f4eSCasper H.S. Dik if (setid & PRIV_FORCED)
858cc4b03b5Scasper setidfl |= EXECSETID_PRIVS;
859cc4b03b5Scasper
860a4aeef46SDonghai Qiao execvp = pp->p_exec;
861a4aeef46SDonghai Qiao if (execvp)
862a4aeef46SDonghai Qiao VN_HOLD(execvp);
863a4aeef46SDonghai Qiao
8647c478bd9Sstevel@tonic-gate error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
865d3e55dcdSgww setidfl, exec_file, cred, brand_action);
8667c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock);
8677c478bd9Sstevel@tonic-gate if (error != 0) {
868a4aeef46SDonghai Qiao if (execvp)
869a4aeef46SDonghai Qiao VN_RELE(execvp);
87084ca7842SDonghai Qiao /*
87184ca7842SDonghai Qiao * If this process's p_exec has been set to the vp of
87284ca7842SDonghai Qiao * the executable by exec_func, we will return without
87384ca7842SDonghai Qiao * calling VOP_CLOSE because proc_exit will close it
87484ca7842SDonghai Qiao * on exit.
87584ca7842SDonghai Qiao */
87684ca7842SDonghai Qiao if (pp->p_exec == vp)
87784ca7842SDonghai Qiao goto bad_noclose;
87884ca7842SDonghai Qiao else
87984ca7842SDonghai Qiao goto bad;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate if (level == 0) {
883134a1f4eSCasper H.S. Dik uid_t oruid;
884134a1f4eSCasper H.S. Dik
885a4aeef46SDonghai Qiao if (execvp != NULL) {
886a4aeef46SDonghai Qiao /*
887a4aeef46SDonghai Qiao * Close the previous executable only if we are
888a4aeef46SDonghai Qiao * at level 0.
889a4aeef46SDonghai Qiao */
890a4aeef46SDonghai Qiao (void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0,
891a4aeef46SDonghai Qiao cred, NULL);
892a4aeef46SDonghai Qiao }
893a4aeef46SDonghai Qiao
8947c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
895134a1f4eSCasper H.S. Dik
896134a1f4eSCasper H.S. Dik oruid = pp->p_cred->cr_ruid;
897134a1f4eSCasper H.S. Dik
8987c478bd9Sstevel@tonic-gate if (newcred != NULL) {
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate * Free the old credentials, and set the new ones.
9017c478bd9Sstevel@tonic-gate * Do this for both the process and the (single) thread.
9027c478bd9Sstevel@tonic-gate */
9037c478bd9Sstevel@tonic-gate crfree(pp->p_cred);
9047c478bd9Sstevel@tonic-gate pp->p_cred = cred; /* cred already held for proc */
9057c478bd9Sstevel@tonic-gate crhold(cred); /* hold new cred for thread */
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate * DTrace accesses t_cred in probe context. t_cred
9087c478bd9Sstevel@tonic-gate * must always be either NULL, or point to a valid,
9097c478bd9Sstevel@tonic-gate * allocated cred structure.
9107c478bd9Sstevel@tonic-gate */
9117c478bd9Sstevel@tonic-gate oldcred = curthread->t_cred;
9127c478bd9Sstevel@tonic-gate curthread->t_cred = cred;
9137c478bd9Sstevel@tonic-gate crfree(oldcred);
914634e26ecSCasper H.S. Dik
915d93c0b4cSCasper H.S. Dik if (priv_basic_test >= 0 &&
916634e26ecSCasper H.S. Dik !PRIV_ISASSERT(&CR_IPRIV(newcred),
917634e26ecSCasper H.S. Dik priv_basic_test)) {
918634e26ecSCasper H.S. Dik pid_t pid = pp->p_pid;
919634e26ecSCasper H.S. Dik char *fn = PTOU(pp)->u_comm;
920634e26ecSCasper H.S. Dik
921634e26ecSCasper H.S. Dik cmn_err(CE_WARN, "%s[%d]: exec: basic_test "
922634e26ecSCasper H.S. Dik "privilege removed from E/I", fn, pid);
923634e26ecSCasper H.S. Dik }
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate * On emerging from a successful exec(), the saved
9277c478bd9Sstevel@tonic-gate * uid and gid equal the effective uid and gid.
9287c478bd9Sstevel@tonic-gate */
9297c478bd9Sstevel@tonic-gate cred->cr_suid = cred->cr_uid;
9307c478bd9Sstevel@tonic-gate cred->cr_sgid = cred->cr_gid;
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate * If the real and effective ids do not match, this
9347c478bd9Sstevel@tonic-gate * is a setuid process that should not dump core.
9357c478bd9Sstevel@tonic-gate * The group comparison is tricky; we prevent the code
9367c478bd9Sstevel@tonic-gate * from flagging SNOCD when executing with an effective gid
9377c478bd9Sstevel@tonic-gate * which is a supplementary group.
9387c478bd9Sstevel@tonic-gate */
9397c478bd9Sstevel@tonic-gate if (cred->cr_ruid != cred->cr_uid ||
9407c478bd9Sstevel@tonic-gate (cred->cr_rgid != cred->cr_gid &&
9417c478bd9Sstevel@tonic-gate !supgroupmember(cred->cr_gid, cred)) ||
9427c478bd9Sstevel@tonic-gate (privflags & PRIV_INCREASE) != 0)
9437c478bd9Sstevel@tonic-gate suidflags = PSUIDFLAGS;
9447c478bd9Sstevel@tonic-gate else
9457c478bd9Sstevel@tonic-gate suidflags = 0;
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
948134a1f4eSCasper H.S. Dik if (newcred != NULL && oruid != newcred->cr_ruid) {
949134a1f4eSCasper H.S. Dik /* Note that the process remains in the same zone. */
950134a1f4eSCasper H.S. Dik mutex_enter(&pidlock);
951134a1f4eSCasper H.S. Dik upcount_dec(oruid, crgetzoneid(newcred));
952134a1f4eSCasper H.S. Dik upcount_inc(newcred->cr_ruid, crgetzoneid(newcred));
953134a1f4eSCasper H.S. Dik mutex_exit(&pidlock);
954134a1f4eSCasper H.S. Dik }
9557c478bd9Sstevel@tonic-gate if (suidflags) {
9567c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
9577c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags;
9587c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
9617c478bd9Sstevel@tonic-gate /*
9627c478bd9Sstevel@tonic-gate * If process is traced via /proc, arrange to
9637c478bd9Sstevel@tonic-gate * invalidate the associated /proc vnode.
9647c478bd9Sstevel@tonic-gate */
9657c478bd9Sstevel@tonic-gate if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
9667c478bd9Sstevel@tonic-gate args->traceinval = 1;
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate if (pp->p_proc_flag & P_PR_PTRACE)
9697c478bd9Sstevel@tonic-gate psignal(pp, SIGTRAP);
9707c478bd9Sstevel@tonic-gate if (args->traceinval)
9717c478bd9Sstevel@tonic-gate prinvalidate(&pp->p_user);
9727c478bd9Sstevel@tonic-gate }
973a4aeef46SDonghai Qiao if (execvp)
974a4aeef46SDonghai Qiao VN_RELE(execvp);
9757c478bd9Sstevel@tonic-gate return (0);
976a4aeef46SDonghai Qiao
9777c478bd9Sstevel@tonic-gate bad:
978a4aeef46SDonghai Qiao (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
979a4aeef46SDonghai Qiao
980a4aeef46SDonghai Qiao bad_noclose:
98184ca7842SDonghai Qiao if (newcred != NULL)
98284ca7842SDonghai Qiao crfree(newcred);
9837c478bd9Sstevel@tonic-gate if (error == 0)
9847c478bd9Sstevel@tonic-gate error = ENOEXEC;
9857c478bd9Sstevel@tonic-gate
986d2a70789SRichard Lowe mutex_enter(&pp->p_lock);
9877c478bd9Sstevel@tonic-gate if (suidflags) {
9887c478bd9Sstevel@tonic-gate pp->p_flag |= suidflags;
9897c478bd9Sstevel@tonic-gate }
990d2a70789SRichard Lowe /*
991d2a70789SRichard Lowe * Restore the effective secflags, to maintain the invariant they
992d2a70789SRichard Lowe * never change for a given process
993d2a70789SRichard Lowe */
994d2a70789SRichard Lowe secflags_copy(&pp->p_secflags.psf_effective, &old_secflags);
995d2a70789SRichard Lowe mutex_exit(&pp->p_lock);
996d2a70789SRichard Lowe
9977c478bd9Sstevel@tonic-gate return (error);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate extern char *execswnames[];
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate struct execsw *
allocate_execsw(char * name,char * magic,size_t magic_size)10037c478bd9Sstevel@tonic-gate allocate_execsw(char *name, char *magic, size_t magic_size)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate int i, j;
10067c478bd9Sstevel@tonic-gate char *ename;
10077c478bd9Sstevel@tonic-gate char *magicp;
10087c478bd9Sstevel@tonic-gate
10097c478bd9Sstevel@tonic-gate mutex_enter(&execsw_lock);
10107c478bd9Sstevel@tonic-gate for (i = 0; i < nexectype; i++) {
10117c478bd9Sstevel@tonic-gate if (execswnames[i] == NULL) {
10127c478bd9Sstevel@tonic-gate ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
10137c478bd9Sstevel@tonic-gate (void) strcpy(ename, name);
10147c478bd9Sstevel@tonic-gate execswnames[i] = ename;
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate * Set the magic number last so that we
10177c478bd9Sstevel@tonic-gate * don't need to hold the execsw_lock in
10187c478bd9Sstevel@tonic-gate * findexectype().
10197c478bd9Sstevel@tonic-gate */
10207c478bd9Sstevel@tonic-gate magicp = kmem_alloc(magic_size, KM_SLEEP);
10217c478bd9Sstevel@tonic-gate for (j = 0; j < magic_size; j++)
10227c478bd9Sstevel@tonic-gate magicp[j] = magic[j];
10237c478bd9Sstevel@tonic-gate execsw[i].exec_magic = magicp;
10247c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock);
10257c478bd9Sstevel@tonic-gate return (&execsw[i]);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate mutex_exit(&execsw_lock);
10297c478bd9Sstevel@tonic-gate return (NULL);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate * Find the exec switch table entry with the corresponding magic string.
10347c478bd9Sstevel@tonic-gate */
10357c478bd9Sstevel@tonic-gate struct execsw *
findexecsw(char * magic)10367c478bd9Sstevel@tonic-gate findexecsw(char *magic)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate struct execsw *eswp;
10397c478bd9Sstevel@tonic-gate
10407c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
10417c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
10427c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
10437c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
10447c478bd9Sstevel@tonic-gate return (eswp);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate return (NULL);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given exec header string by looking for the
10517c478bd9Sstevel@tonic-gate * magic string at a specified offset and length for each kind of executable
10527c478bd9Sstevel@tonic-gate * file format until one matches. If no execsw[] entry is found, try to
10537c478bd9Sstevel@tonic-gate * autoload a module for this magic string.
10547c478bd9Sstevel@tonic-gate */
10557c478bd9Sstevel@tonic-gate struct execsw *
findexec_by_hdr(char * header)10567c478bd9Sstevel@tonic-gate findexec_by_hdr(char *header)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate struct execsw *eswp;
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
10617c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
10627c478bd9Sstevel@tonic-gate if (header && eswp->exec_maglen != 0 &&
10637c478bd9Sstevel@tonic-gate bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
1064d3e55dcdSgww eswp->exec_maglen) == 0) {
10657c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
10667c478bd9Sstevel@tonic-gate return (NULL);
10677c478bd9Sstevel@tonic-gate return (eswp);
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate * Find the execsw[] index for the given magic string. If no execsw[] entry
10757c478bd9Sstevel@tonic-gate * is found, try to autoload a module for this magic string.
10767c478bd9Sstevel@tonic-gate */
10777c478bd9Sstevel@tonic-gate struct execsw *
findexec_by_magic(char * magic)10787c478bd9Sstevel@tonic-gate findexec_by_magic(char *magic)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate struct execsw *eswp;
10817c478bd9Sstevel@tonic-gate
10827c478bd9Sstevel@tonic-gate for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
10837c478bd9Sstevel@tonic-gate ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
10847c478bd9Sstevel@tonic-gate if (magic && eswp->exec_maglen != 0 &&
10857c478bd9Sstevel@tonic-gate bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
10867c478bd9Sstevel@tonic-gate if (hold_execsw(eswp) != 0)
10877c478bd9Sstevel@tonic-gate return (NULL);
10887c478bd9Sstevel@tonic-gate return (eswp);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate return (NULL); /* couldn't find the type */
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate static int
hold_execsw(struct execsw * eswp)10957c478bd9Sstevel@tonic-gate hold_execsw(struct execsw *eswp)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate char *name;
10987c478bd9Sstevel@tonic-gate
10997c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
11007c478bd9Sstevel@tonic-gate while (!LOADED_EXEC(eswp)) {
11017c478bd9Sstevel@tonic-gate rw_exit(eswp->exec_lock);
11027c478bd9Sstevel@tonic-gate name = execswnames[eswp-execsw];
11037c478bd9Sstevel@tonic-gate ASSERT(name);
11047c478bd9Sstevel@tonic-gate if (modload("exec", name) == -1)
11057c478bd9Sstevel@tonic-gate return (-1);
11067c478bd9Sstevel@tonic-gate rw_enter(eswp->exec_lock, RW_READER);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate return (0);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate
11117c478bd9Sstevel@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)1112134a1f4eSCasper H.S. Dik execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp,
1113134a1f4eSCasper H.S. Dik priv_set_t *fset, cred_t *cr, const char *pathname)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(curthread);
11167c478bd9Sstevel@tonic-gate uid_t uid, gid;
11177c478bd9Sstevel@tonic-gate int privflags = 0;
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate /*
11207c478bd9Sstevel@tonic-gate * Remember credentials.
11217c478bd9Sstevel@tonic-gate */
11227c478bd9Sstevel@tonic-gate uid = cr->cr_uid;
11237c478bd9Sstevel@tonic-gate gid = cr->cr_gid;
11247c478bd9Sstevel@tonic-gate
11257c478bd9Sstevel@tonic-gate /* Will try to reset the PRIV_AWARE bit later. */
11267c478bd9Sstevel@tonic-gate if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
11277c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
11307c478bd9Sstevel@tonic-gate /*
1131134a1f4eSCasper H.S. Dik * If it's a set-uid root program we perform the
1132134a1f4eSCasper H.S. Dik * forced privilege look-aside. This has three possible
1133134a1f4eSCasper H.S. Dik * outcomes:
1134134a1f4eSCasper H.S. Dik * no look aside information -> treat as before
1135134a1f4eSCasper H.S. Dik * look aside in Limit set -> apply forced privs
1136134a1f4eSCasper H.S. Dik * look aside not in Limit set -> ignore set-uid root
1137134a1f4eSCasper H.S. Dik *
1138134a1f4eSCasper H.S. Dik * Ordinary set-uid root execution only allowed if the limit
1139134a1f4eSCasper H.S. Dik * set holds all unsafe privileges.
11407c478bd9Sstevel@tonic-gate */
1141134a1f4eSCasper H.S. Dik if (vattrp->va_mode & VSUID) {
1142134a1f4eSCasper H.S. Dik if (vattrp->va_uid == 0) {
1143134a1f4eSCasper H.S. Dik int res = get_forced_privs(cr, pathname, fset);
1144134a1f4eSCasper H.S. Dik
1145134a1f4eSCasper H.S. Dik switch (res) {
1146134a1f4eSCasper H.S. Dik case -1:
1147134a1f4eSCasper H.S. Dik if (priv_issubset(&priv_unsafe,
1148134a1f4eSCasper H.S. Dik &CR_LPRIV(cr))) {
1149134a1f4eSCasper H.S. Dik uid = vattrp->va_uid;
1150134a1f4eSCasper H.S. Dik privflags |= PRIV_SETUGID;
1151134a1f4eSCasper H.S. Dik }
1152134a1f4eSCasper H.S. Dik break;
1153134a1f4eSCasper H.S. Dik case 0:
1154134a1f4eSCasper H.S. Dik privflags |= PRIV_FORCED|PRIV_INCREASE;
1155134a1f4eSCasper H.S. Dik break;
1156134a1f4eSCasper H.S. Dik default:
1157134a1f4eSCasper H.S. Dik break;
1158134a1f4eSCasper H.S. Dik }
1159134a1f4eSCasper H.S. Dik } else {
1160134a1f4eSCasper H.S. Dik uid = vattrp->va_uid;
1161134a1f4eSCasper H.S. Dik privflags |= PRIV_SETUGID;
1162134a1f4eSCasper H.S. Dik }
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate if (vattrp->va_mode & VSGID) {
11657c478bd9Sstevel@tonic-gate gid = vattrp->va_gid;
11667c478bd9Sstevel@tonic-gate privflags |= PRIV_SETUGID;
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate /*
11717c478bd9Sstevel@tonic-gate * Do we need to change our credential anyway?
11727c478bd9Sstevel@tonic-gate * This is the case when E != I or P != I, as
11737c478bd9Sstevel@tonic-gate * we need to do the assignments (with F empty and A full)
11747c478bd9Sstevel@tonic-gate * Or when I is not a subset of L; in that case we need to
11757c478bd9Sstevel@tonic-gate * enforce L.
11767c478bd9Sstevel@tonic-gate *
11777c478bd9Sstevel@tonic-gate * I' = L & I
11787c478bd9Sstevel@tonic-gate *
11797c478bd9Sstevel@tonic-gate * E' = P' = (I' + F) & A
11807c478bd9Sstevel@tonic-gate * or
11817c478bd9Sstevel@tonic-gate * E' = P' = I'
11827c478bd9Sstevel@tonic-gate */
11837c478bd9Sstevel@tonic-gate if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
11847c478bd9Sstevel@tonic-gate !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
11857c478bd9Sstevel@tonic-gate !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
11867c478bd9Sstevel@tonic-gate privflags |= PRIV_RESET;
11877c478bd9Sstevel@tonic-gate
1188134a1f4eSCasper H.S. Dik /* Child has more privileges than parent */
1189134a1f4eSCasper H.S. Dik if (!priv_issubset(&CR_IPRIV(cr), &CR_PPRIV(cr)))
1190134a1f4eSCasper H.S. Dik privflags |= PRIV_INCREASE;
1191134a1f4eSCasper H.S. Dik
119245916cd2Sjpk /* If MAC-aware flag(s) are on, need to update cred to remove. */
119345916cd2Sjpk if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
119445916cd2Sjpk (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
119545916cd2Sjpk privflags |= MAC_FLAGS;
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate * Set setuid/setgid protections if no ptrace() compatibility.
11987c478bd9Sstevel@tonic-gate * For privileged processes, honor setuid/setgid even in
11997c478bd9Sstevel@tonic-gate * the presence of ptrace() compatibility.
12007c478bd9Sstevel@tonic-gate */
12017c478bd9Sstevel@tonic-gate if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
12027c478bd9Sstevel@tonic-gate PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
12037c478bd9Sstevel@tonic-gate (cr->cr_uid != uid ||
12047c478bd9Sstevel@tonic-gate cr->cr_gid != gid ||
12057c478bd9Sstevel@tonic-gate cr->cr_suid != uid ||
12067c478bd9Sstevel@tonic-gate cr->cr_sgid != gid)) {
12077c478bd9Sstevel@tonic-gate *uidp = uid;
12087c478bd9Sstevel@tonic-gate *gidp = gid;
12097c478bd9Sstevel@tonic-gate privflags |= PRIV_SETID;
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate return (privflags);
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate int
execpermissions(struct vnode * vp,struct vattr * vattrp,struct uarg * args)12157c478bd9Sstevel@tonic-gate execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate int error;
12187c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
1221da6c28aaSamw if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL))
12227c478bd9Sstevel@tonic-gate return (error);
12237c478bd9Sstevel@tonic-gate /*
12247c478bd9Sstevel@tonic-gate * Check the access mode.
12257c478bd9Sstevel@tonic-gate * If VPROC, ask /proc if the file is an object file.
12267c478bd9Sstevel@tonic-gate */
1227da6c28aaSamw if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 ||
12287c478bd9Sstevel@tonic-gate !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
12297c478bd9Sstevel@tonic-gate (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
12307c478bd9Sstevel@tonic-gate (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
12317c478bd9Sstevel@tonic-gate if (error == 0)
12327c478bd9Sstevel@tonic-gate error = EACCES;
12337c478bd9Sstevel@tonic-gate return (error);
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
1237da6c28aaSamw (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) {
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate * If process is under ptrace(2) compatibility,
12407c478bd9Sstevel@tonic-gate * fail the exec(2).
12417c478bd9Sstevel@tonic-gate */
12427c478bd9Sstevel@tonic-gate if (p->p_proc_flag & P_PR_PTRACE)
12437c478bd9Sstevel@tonic-gate goto bad;
12447c478bd9Sstevel@tonic-gate /*
12457c478bd9Sstevel@tonic-gate * Process is traced via /proc.
12467c478bd9Sstevel@tonic-gate * Arrange to invalidate the /proc vnode.
12477c478bd9Sstevel@tonic-gate */
12487c478bd9Sstevel@tonic-gate args->traceinval = 1;
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate return (0);
12517c478bd9Sstevel@tonic-gate bad:
12527c478bd9Sstevel@tonic-gate if (error == 0)
12537c478bd9Sstevel@tonic-gate error = ENOEXEC;
12547c478bd9Sstevel@tonic-gate return (error);
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate
12577c478bd9Sstevel@tonic-gate /*
12587c478bd9Sstevel@tonic-gate * Map a section of an executable file into the user's
12597c478bd9Sstevel@tonic-gate * address space.
12607c478bd9Sstevel@tonic-gate */
12617c478bd9Sstevel@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)12627c478bd9Sstevel@tonic-gate execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
12637c478bd9Sstevel@tonic-gate off_t offset, int prot, int page, uint_t szc)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate int error = 0;
12667c478bd9Sstevel@tonic-gate off_t oldoffset;
12677c478bd9Sstevel@tonic-gate caddr_t zfodbase, oldaddr;
12687c478bd9Sstevel@tonic-gate size_t end, oldlen;
12697c478bd9Sstevel@tonic-gate size_t zfoddiff;
12707c478bd9Sstevel@tonic-gate label_t ljb;
12717c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
12727c478bd9Sstevel@tonic-gate
12737c478bd9Sstevel@tonic-gate oldaddr = addr;
12747c478bd9Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
12757c478bd9Sstevel@tonic-gate if (len) {
12767c478bd9Sstevel@tonic-gate oldlen = len;
12777c478bd9Sstevel@tonic-gate len += ((size_t)oldaddr - (size_t)addr);
12787c478bd9Sstevel@tonic-gate oldoffset = offset;
12797c478bd9Sstevel@tonic-gate offset = (off_t)((uintptr_t)offset & PAGEMASK);
12807c478bd9Sstevel@tonic-gate if (page) {
12817c478bd9Sstevel@tonic-gate spgcnt_t prefltmem, availm, npages;
12827c478bd9Sstevel@tonic-gate int preread;
12837c478bd9Sstevel@tonic-gate uint_t mflag = MAP_PRIVATE | MAP_FIXED;
12847c478bd9Sstevel@tonic-gate
12857c478bd9Sstevel@tonic-gate if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
12867c478bd9Sstevel@tonic-gate mflag |= MAP_TEXT;
12877c478bd9Sstevel@tonic-gate } else {
12887c478bd9Sstevel@tonic-gate mflag |= MAP_INITDATA;
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate
12917c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
12927c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
12937c478bd9Sstevel@tonic-gate error = ENOMEM;
12947c478bd9Sstevel@tonic-gate goto bad;
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate if (error = VOP_MAP(vp, (offset_t)offset,
12977c478bd9Sstevel@tonic-gate p->p_as, &addr, len, prot, PROT_ALL,
1298da6c28aaSamw mflag, CRED(), NULL))
12997c478bd9Sstevel@tonic-gate goto bad;
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate * If the segment can fit, then we prefault
13037c478bd9Sstevel@tonic-gate * the entire segment in. This is based on the
13047c478bd9Sstevel@tonic-gate * model that says the best working set of a
13057c478bd9Sstevel@tonic-gate * small program is all of its pages.
13067c478bd9Sstevel@tonic-gate */
13077c478bd9Sstevel@tonic-gate npages = (spgcnt_t)btopr(len);
13087c478bd9Sstevel@tonic-gate prefltmem = freemem - desfree;
13097c478bd9Sstevel@tonic-gate preread =
13107c478bd9Sstevel@tonic-gate (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
13117c478bd9Sstevel@tonic-gate
13127c478bd9Sstevel@tonic-gate /*
13137c478bd9Sstevel@tonic-gate * If we aren't prefaulting the segment,
13147c478bd9Sstevel@tonic-gate * increment "deficit", if necessary to ensure
13157c478bd9Sstevel@tonic-gate * that pages will become available when this
13167c478bd9Sstevel@tonic-gate * process starts executing.
13177c478bd9Sstevel@tonic-gate */
13187c478bd9Sstevel@tonic-gate availm = freemem - lotsfree;
13197c478bd9Sstevel@tonic-gate if (preread == 0 && npages > availm &&
13207c478bd9Sstevel@tonic-gate deficit < lotsfree) {
13217c478bd9Sstevel@tonic-gate deficit += MIN((pgcnt_t)(npages - availm),
13227c478bd9Sstevel@tonic-gate lotsfree - deficit);
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate
13257c478bd9Sstevel@tonic-gate if (preread) {
13267c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
13277c478bd9Sstevel@tonic-gate "execmap preread:freemem %d size %lu",
13287c478bd9Sstevel@tonic-gate freemem, len);
13297c478bd9Sstevel@tonic-gate (void) as_fault(p->p_as->a_hat, p->p_as,
13307c478bd9Sstevel@tonic-gate (caddr_t)addr, len, F_INVAL, S_READ);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate } else {
13337c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, len, prot, p->p_as,
13347c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
13357c478bd9Sstevel@tonic-gate error = ENOMEM;
13367c478bd9Sstevel@tonic-gate goto bad;
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate
13397c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, addr, len,
13407c478bd9Sstevel@tonic-gate segvn_create, zfod_argsp))
13417c478bd9Sstevel@tonic-gate goto bad;
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate * Read in the segment in one big chunk.
13447c478bd9Sstevel@tonic-gate */
13457c478bd9Sstevel@tonic-gate if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
13467c478bd9Sstevel@tonic-gate oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
13477c478bd9Sstevel@tonic-gate (rlim64_t)0, CRED(), (ssize_t *)0))
13487c478bd9Sstevel@tonic-gate goto bad;
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate * Now set protections.
13517c478bd9Sstevel@tonic-gate */
13527c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) {
13537c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)addr,
13547c478bd9Sstevel@tonic-gate len, prot);
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate
13597c478bd9Sstevel@tonic-gate if (zfodlen) {
13609acbbeafSnn struct as *as = curproc->p_as;
13619acbbeafSnn struct seg *seg;
13629acbbeafSnn uint_t zprot = 0;
13639acbbeafSnn
13647c478bd9Sstevel@tonic-gate end = (size_t)addr + len;
13657c478bd9Sstevel@tonic-gate zfodbase = (caddr_t)roundup(end, PAGESIZE);
13667c478bd9Sstevel@tonic-gate zfoddiff = (uintptr_t)zfodbase - end;
13677c478bd9Sstevel@tonic-gate if (zfoddiff) {
13689acbbeafSnn /*
13699acbbeafSnn * Before we go to zero the remaining space on the last
13709acbbeafSnn * page, make sure we have write permission.
1371386e9c9eSJerry Jelinek *
1372386e9c9eSJerry Jelinek * Normal illumos binaries don't even hit the case
1373386e9c9eSJerry Jelinek * where we have to change permission on the last page
1374386e9c9eSJerry Jelinek * since their protection is typically either
1375386e9c9eSJerry Jelinek * PROT_USER | PROT_WRITE | PROT_READ
1376386e9c9eSJerry Jelinek * or
1377386e9c9eSJerry Jelinek * PROT_ZFOD (same as PROT_ALL).
1378386e9c9eSJerry Jelinek *
1379386e9c9eSJerry Jelinek * We need to be careful how we zero-fill the last page
1380386e9c9eSJerry Jelinek * if the segment protection does not include
1381386e9c9eSJerry Jelinek * PROT_WRITE. Using as_setprot() can cause the VM
1382386e9c9eSJerry Jelinek * segment code to call segvn_vpage(), which must
1383386e9c9eSJerry Jelinek * allocate a page struct for each page in the segment.
1384386e9c9eSJerry Jelinek * If we have a very large segment, this may fail, so
1385386e9c9eSJerry Jelinek * we have to check for that, even though we ignore
1386386e9c9eSJerry Jelinek * other return values from as_setprot.
13879acbbeafSnn */
13889acbbeafSnn
1389dc32d872SJosef 'Jeff' Sipek AS_LOCK_ENTER(as, RW_READER);
13909acbbeafSnn seg = as_segat(curproc->p_as, (caddr_t)end);
13919acbbeafSnn if (seg != NULL)
13929acbbeafSnn SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1,
13939acbbeafSnn &zprot);
1394dc32d872SJosef 'Jeff' Sipek AS_LOCK_EXIT(as);
13959acbbeafSnn
13969acbbeafSnn if (seg != NULL && (zprot & PROT_WRITE) == 0) {
1397386e9c9eSJerry Jelinek if (as_setprot(as, (caddr_t)end, zfoddiff - 1,
1398386e9c9eSJerry Jelinek zprot | PROT_WRITE) == ENOMEM) {
1399386e9c9eSJerry Jelinek error = ENOMEM;
1400386e9c9eSJerry Jelinek goto bad;
1401386e9c9eSJerry Jelinek }
14029acbbeafSnn }
14039acbbeafSnn
14047c478bd9Sstevel@tonic-gate if (on_fault(&ljb)) {
14057c478bd9Sstevel@tonic-gate no_fault();
14069acbbeafSnn if (seg != NULL && (zprot & PROT_WRITE) == 0)
14079acbbeafSnn (void) as_setprot(as, (caddr_t)end,
1408d3e55dcdSgww zfoddiff - 1, zprot);
14097c478bd9Sstevel@tonic-gate error = EFAULT;
14107c478bd9Sstevel@tonic-gate goto bad;
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate uzero((void *)end, zfoddiff);
14137c478bd9Sstevel@tonic-gate no_fault();
14149acbbeafSnn if (seg != NULL && (zprot & PROT_WRITE) == 0)
14159acbbeafSnn (void) as_setprot(as, (caddr_t)end,
14169acbbeafSnn zfoddiff - 1, zprot);
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate if (zfodlen > zfoddiff) {
14197c478bd9Sstevel@tonic-gate struct segvn_crargs crargs =
14207c478bd9Sstevel@tonic-gate SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate zfodlen -= zfoddiff;
14237c478bd9Sstevel@tonic-gate if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
14247c478bd9Sstevel@tonic-gate p->p_as->a_userlimit) != RANGE_OKAY) {
14257c478bd9Sstevel@tonic-gate error = ENOMEM;
14267c478bd9Sstevel@tonic-gate goto bad;
14277c478bd9Sstevel@tonic-gate }
1428ec25b48fSsusans if (szc > 0) {
1429ec25b48fSsusans /*
1430ec25b48fSsusans * ASSERT alignment because the mapelfexec()
1431ec25b48fSsusans * caller for the szc > 0 case extended zfod
1432ec25b48fSsusans * so it's end is pgsz aligned.
1433ec25b48fSsusans */
1434ec25b48fSsusans size_t pgsz = page_get_pagesize(szc);
1435ec25b48fSsusans ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz));
1436ec25b48fSsusans
1437ec25b48fSsusans if (IS_P2ALIGNED(zfodbase, pgsz)) {
1438ec25b48fSsusans crargs.szc = szc;
1439ec25b48fSsusans } else {
1440ec25b48fSsusans crargs.szc = AS_MAP_HEAP;
1441ec25b48fSsusans }
1442ec25b48fSsusans } else {
1443ec25b48fSsusans crargs.szc = AS_MAP_NO_LPOOB;
1444ec25b48fSsusans }
14457c478bd9Sstevel@tonic-gate if (error = as_map(p->p_as, (caddr_t)zfodbase,
14467c478bd9Sstevel@tonic-gate zfodlen, segvn_create, &crargs))
14477c478bd9Sstevel@tonic-gate goto bad;
14487c478bd9Sstevel@tonic-gate if (prot != PROT_ZFOD) {
14497c478bd9Sstevel@tonic-gate (void) as_setprot(p->p_as, (caddr_t)zfodbase,
14507c478bd9Sstevel@tonic-gate zfodlen, prot);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate return (0);
14557c478bd9Sstevel@tonic-gate bad:
14567c478bd9Sstevel@tonic-gate return (error);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate void
setexecenv(struct execenv * ep)14607c478bd9Sstevel@tonic-gate setexecenv(struct execenv *ep)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
14637c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
14647c478bd9Sstevel@tonic-gate struct vnode *vp;
14657c478bd9Sstevel@tonic-gate
14667c478bd9Sstevel@tonic-gate p->p_bssbase = ep->ex_bssbase;
14677c478bd9Sstevel@tonic-gate p->p_brkbase = ep->ex_brkbase;
14687c478bd9Sstevel@tonic-gate p->p_brksize = ep->ex_brksize;
14697c478bd9Sstevel@tonic-gate if (p->p_exec)
14707c478bd9Sstevel@tonic-gate VN_RELE(p->p_exec); /* out with the old */
14717c478bd9Sstevel@tonic-gate vp = p->p_exec = ep->ex_vp;
14727c478bd9Sstevel@tonic-gate if (vp != NULL)
14737c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* in with the new */
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_sp = 0;
14767c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_size = 0;
14777c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate
14807c478bd9Sstevel@tonic-gate int
execopen(struct vnode ** vpp,int * fdp)14817c478bd9Sstevel@tonic-gate execopen(struct vnode **vpp, int *fdp)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate struct vnode *vp = *vpp;
14847c478bd9Sstevel@tonic-gate file_t *fp;
14857c478bd9Sstevel@tonic-gate int error = 0;
14867c478bd9Sstevel@tonic-gate int filemode = FREAD;
14877c478bd9Sstevel@tonic-gate
14887c478bd9Sstevel@tonic-gate VN_HOLD(vp); /* open reference */
14897c478bd9Sstevel@tonic-gate if (error = falloc(NULL, filemode, &fp, fdp)) {
14907c478bd9Sstevel@tonic-gate VN_RELE(vp);
14917c478bd9Sstevel@tonic-gate *fdp = -1; /* just in case falloc changed value */
14927c478bd9Sstevel@tonic-gate return (error);
14937c478bd9Sstevel@tonic-gate }
1494da6c28aaSamw if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) {
14957c478bd9Sstevel@tonic-gate VN_RELE(vp);
14967c478bd9Sstevel@tonic-gate setf(*fdp, NULL);
14977c478bd9Sstevel@tonic-gate unfalloc(fp);
14987c478bd9Sstevel@tonic-gate *fdp = -1;
14997c478bd9Sstevel@tonic-gate return (error);
15007c478bd9Sstevel@tonic-gate }
15017c478bd9Sstevel@tonic-gate *vpp = vp; /* vnode should not have changed */
15027c478bd9Sstevel@tonic-gate fp->f_vnode = vp;
15037c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
15047c478bd9Sstevel@tonic-gate setf(*fdp, fp);
15057c478bd9Sstevel@tonic-gate return (0);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate
15087c478bd9Sstevel@tonic-gate int
execclose(int fd)15097c478bd9Sstevel@tonic-gate execclose(int fd)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate return (closeandsetf(fd, NULL));
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate
15147c478bd9Sstevel@tonic-gate
15157c478bd9Sstevel@tonic-gate /*
15167c478bd9Sstevel@tonic-gate * noexec stub function.
15177c478bd9Sstevel@tonic-gate */
15187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15197c478bd9Sstevel@tonic-gate int
noexec(struct vnode * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,size_t * execsz,int setid,caddr_t exec_file,struct cred * cred)15207c478bd9Sstevel@tonic-gate noexec(
15217c478bd9Sstevel@tonic-gate struct vnode *vp,
15227c478bd9Sstevel@tonic-gate struct execa *uap,
15237c478bd9Sstevel@tonic-gate struct uarg *args,
15247c478bd9Sstevel@tonic-gate struct intpdata *idatap,
15257c478bd9Sstevel@tonic-gate int level,
15264e18e297SPatrick Mooney size_t *execsz,
15277c478bd9Sstevel@tonic-gate int setid,
15287c478bd9Sstevel@tonic-gate caddr_t exec_file,
15297c478bd9Sstevel@tonic-gate struct cred *cred)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
15327c478bd9Sstevel@tonic-gate return (ENOEXEC);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate
15357c478bd9Sstevel@tonic-gate /*
15367c478bd9Sstevel@tonic-gate * Support routines for building a user stack.
15377c478bd9Sstevel@tonic-gate *
15387c478bd9Sstevel@tonic-gate * execve(path, argv, envp) must construct a new stack with the specified
15397c478bd9Sstevel@tonic-gate * arguments and environment variables (see exec_args() for a description
15407c478bd9Sstevel@tonic-gate * of the user stack layout). To do this, we copy the arguments and
15417c478bd9Sstevel@tonic-gate * environment variables from the old user address space into the kernel,
15427c478bd9Sstevel@tonic-gate * free the old as, create the new as, and copy our buffered information
15437c478bd9Sstevel@tonic-gate * to the new stack. Our kernel buffer has the following structure:
15447c478bd9Sstevel@tonic-gate *
15457c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
15467c478bd9Sstevel@tonic-gate * | string offsets |
15477c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
15487c478bd9Sstevel@tonic-gate * | |
15497c478bd9Sstevel@tonic-gate * | STK_AVAIL() space |
15507c478bd9Sstevel@tonic-gate * | |
15517c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
15527c478bd9Sstevel@tonic-gate * | strings |
15537c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base
15547c478bd9Sstevel@tonic-gate *
15557c478bd9Sstevel@tonic-gate * When we add a string, we store the string's contents (including the null
15567c478bd9Sstevel@tonic-gate * terminator) at stk_strp, and we store the offset of the string relative to
15577c478bd9Sstevel@tonic-gate * stk_base at --stk_offp. At strings are added, stk_strp increases and
15587c478bd9Sstevel@tonic-gate * stk_offp decreases. The amount of space remaining, STK_AVAIL(), is just
15597c478bd9Sstevel@tonic-gate * the difference between these pointers. If we run out of space, we return
15607c478bd9Sstevel@tonic-gate * an error and exec_args() starts all over again with a buffer twice as large.
15617c478bd9Sstevel@tonic-gate * When we're all done, the kernel buffer looks like this:
15627c478bd9Sstevel@tonic-gate *
15637c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base + stk_size
15647c478bd9Sstevel@tonic-gate * | argv[0] offset |
15657c478bd9Sstevel@tonic-gate * +-----------------------+
15667c478bd9Sstevel@tonic-gate * | ... |
15677c478bd9Sstevel@tonic-gate * +-----------------------+
15687c478bd9Sstevel@tonic-gate * | argv[argc-1] offset |
15697c478bd9Sstevel@tonic-gate * +-----------------------+
15707c478bd9Sstevel@tonic-gate * | envp[0] offset |
15717c478bd9Sstevel@tonic-gate * +-----------------------+
15727c478bd9Sstevel@tonic-gate * | ... |
15737c478bd9Sstevel@tonic-gate * +-----------------------+
15747c478bd9Sstevel@tonic-gate * | envp[envc-1] offset |
15757c478bd9Sstevel@tonic-gate * +-----------------------+
15767c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
15777c478bd9Sstevel@tonic-gate * +-----------------------+
15787c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
15797c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_offp
15807c478bd9Sstevel@tonic-gate * | |
15817c478bd9Sstevel@tonic-gate * | STK_AVAIL() space |
15827c478bd9Sstevel@tonic-gate * | |
15837c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_strp
15847c478bd9Sstevel@tonic-gate * | AT_SUN_EXECNAME offset|
15857c478bd9Sstevel@tonic-gate * +-----------------------+
15867c478bd9Sstevel@tonic-gate * | AT_SUN_PLATFORM offset|
15877c478bd9Sstevel@tonic-gate * +-----------------------+
15887c478bd9Sstevel@tonic-gate * | envp[envc-1] string |
15897c478bd9Sstevel@tonic-gate * +-----------------------+
15907c478bd9Sstevel@tonic-gate * | ... |
15917c478bd9Sstevel@tonic-gate * +-----------------------+
15927c478bd9Sstevel@tonic-gate * | envp[0] string |
15937c478bd9Sstevel@tonic-gate * +-----------------------+
15947c478bd9Sstevel@tonic-gate * | argv[argc-1] string |
15957c478bd9Sstevel@tonic-gate * +-----------------------+
15967c478bd9Sstevel@tonic-gate * | ... |
15977c478bd9Sstevel@tonic-gate * +-----------------------+
15987c478bd9Sstevel@tonic-gate * | argv[0] string |
15997c478bd9Sstevel@tonic-gate * +-----------------------+ <--- stk_base
16007c478bd9Sstevel@tonic-gate */
16017c478bd9Sstevel@tonic-gate
16027c478bd9Sstevel@tonic-gate #define STK_AVAIL(args) ((char *)(args)->stk_offp - (args)->stk_strp)
16037c478bd9Sstevel@tonic-gate
16047c478bd9Sstevel@tonic-gate /*
16057c478bd9Sstevel@tonic-gate * Add a string to the stack.
16067c478bd9Sstevel@tonic-gate */
16077c478bd9Sstevel@tonic-gate static int
stk_add(uarg_t * args,const char * sp,enum uio_seg segflg)16087c478bd9Sstevel@tonic-gate stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
16097c478bd9Sstevel@tonic-gate {
16107c478bd9Sstevel@tonic-gate int error;
16117c478bd9Sstevel@tonic-gate size_t len;
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < sizeof (int))
16147c478bd9Sstevel@tonic-gate return (E2BIG);
16157c478bd9Sstevel@tonic-gate *--args->stk_offp = args->stk_strp - args->stk_base;
16167c478bd9Sstevel@tonic-gate
16177c478bd9Sstevel@tonic-gate if (segflg == UIO_USERSPACE) {
16187c478bd9Sstevel@tonic-gate error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
16197c478bd9Sstevel@tonic-gate if (error != 0)
16207c478bd9Sstevel@tonic-gate return (error);
16217c478bd9Sstevel@tonic-gate } else {
16227c478bd9Sstevel@tonic-gate len = strlen(sp) + 1;
16237c478bd9Sstevel@tonic-gate if (len > STK_AVAIL(args))
16247c478bd9Sstevel@tonic-gate return (E2BIG);
16257c478bd9Sstevel@tonic-gate bcopy(sp, args->stk_strp, len);
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate
16287c478bd9Sstevel@tonic-gate args->stk_strp += len;
16297c478bd9Sstevel@tonic-gate
16307c478bd9Sstevel@tonic-gate return (0);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate
16337c478bd9Sstevel@tonic-gate static int
stk_getptr(uarg_t * args,char * src,char ** dst)16347c478bd9Sstevel@tonic-gate stk_getptr(uarg_t *args, char *src, char **dst)
16357c478bd9Sstevel@tonic-gate {
16367c478bd9Sstevel@tonic-gate int error;
16377c478bd9Sstevel@tonic-gate
16387c478bd9Sstevel@tonic-gate if (args->from_model == DATAMODEL_NATIVE) {
16397c478bd9Sstevel@tonic-gate ulong_t ptr;
16407c478bd9Sstevel@tonic-gate error = fulword(src, &ptr);
16417c478bd9Sstevel@tonic-gate *dst = (caddr_t)ptr;
16427c478bd9Sstevel@tonic-gate } else {
16437c478bd9Sstevel@tonic-gate uint32_t ptr;
16447c478bd9Sstevel@tonic-gate error = fuword32(src, &ptr);
16457c478bd9Sstevel@tonic-gate *dst = (caddr_t)(uintptr_t)ptr;
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate return (error);
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate
16507c478bd9Sstevel@tonic-gate static int
stk_putptr(uarg_t * args,char * addr,char * value)16517c478bd9Sstevel@tonic-gate stk_putptr(uarg_t *args, char *addr, char *value)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE)
16547c478bd9Sstevel@tonic-gate return (sulword(addr, (ulong_t)value));
16557c478bd9Sstevel@tonic-gate else
16567c478bd9Sstevel@tonic-gate return (suword32(addr, (uint32_t)(uintptr_t)value));
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate
16597c478bd9Sstevel@tonic-gate static int
stk_copyin(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)16607c478bd9Sstevel@tonic-gate stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate char *sp;
16637c478bd9Sstevel@tonic-gate int argc, error;
16647c478bd9Sstevel@tonic-gate int argv_empty = 0;
16657c478bd9Sstevel@tonic-gate size_t ptrsize = args->from_ptrsize;
16667c478bd9Sstevel@tonic-gate size_t size, pad;
16677c478bd9Sstevel@tonic-gate char *argv = (char *)uap->argp;
16687c478bd9Sstevel@tonic-gate char *envp = (char *)uap->envp;
16697c478bd9Sstevel@tonic-gate
16707c478bd9Sstevel@tonic-gate /*
16717c478bd9Sstevel@tonic-gate * Copy interpreter's name and argument to argv[0] and argv[1].
167293cf283aSJerry Jelinek * In the rare case that we have nested interpreters then those names
167393cf283aSJerry Jelinek * and arguments are also copied to the subsequent slots in argv.
16747c478bd9Sstevel@tonic-gate */
167593cf283aSJerry Jelinek if (intp != NULL && intp->intp_name[0] != NULL) {
167693cf283aSJerry Jelinek int i;
167793cf283aSJerry Jelinek
167893cf283aSJerry Jelinek for (i = 0; i < INTP_MAXDEPTH; i++) {
167993cf283aSJerry Jelinek if (intp->intp_name[i] == NULL)
168093cf283aSJerry Jelinek break;
168193cf283aSJerry Jelinek error = stk_add(args, intp->intp_name[i], UIO_SYSSPACE);
168293cf283aSJerry Jelinek if (error != 0)
168393cf283aSJerry Jelinek return (error);
168493cf283aSJerry Jelinek if (intp->intp_arg[i] != NULL) {
168593cf283aSJerry Jelinek error = stk_add(args, intp->intp_arg[i],
168693cf283aSJerry Jelinek UIO_SYSSPACE);
168793cf283aSJerry Jelinek if (error != 0)
168893cf283aSJerry Jelinek return (error);
168993cf283aSJerry Jelinek }
169093cf283aSJerry Jelinek }
169193cf283aSJerry Jelinek
16927c478bd9Sstevel@tonic-gate if (args->fname != NULL)
16937c478bd9Sstevel@tonic-gate error = stk_add(args, args->fname, UIO_SYSSPACE);
16947c478bd9Sstevel@tonic-gate else
16957c478bd9Sstevel@tonic-gate error = stk_add(args, uap->fname, UIO_USERSPACE);
16967c478bd9Sstevel@tonic-gate if (error)
16977c478bd9Sstevel@tonic-gate return (error);
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate /*
17007c478bd9Sstevel@tonic-gate * Check for an empty argv[].
17017c478bd9Sstevel@tonic-gate */
17027c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
17037c478bd9Sstevel@tonic-gate return (EFAULT);
17047c478bd9Sstevel@tonic-gate if (sp == NULL)
17057c478bd9Sstevel@tonic-gate argv_empty = 1;
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate argv += ptrsize; /* ignore original argv[0] */
17087c478bd9Sstevel@tonic-gate }
17097c478bd9Sstevel@tonic-gate
17107c478bd9Sstevel@tonic-gate if (argv_empty == 0) {
17117c478bd9Sstevel@tonic-gate /*
17127c478bd9Sstevel@tonic-gate * Add argv[] strings to the stack.
17137c478bd9Sstevel@tonic-gate */
17147c478bd9Sstevel@tonic-gate for (;;) {
17157c478bd9Sstevel@tonic-gate if (stk_getptr(args, argv, &sp))
17167c478bd9Sstevel@tonic-gate return (EFAULT);
17177c478bd9Sstevel@tonic-gate if (sp == NULL)
17187c478bd9Sstevel@tonic-gate break;
17197c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
17207c478bd9Sstevel@tonic-gate return (error);
17217c478bd9Sstevel@tonic-gate argv += ptrsize;
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
17257c478bd9Sstevel@tonic-gate args->arglen = args->stk_strp - args->stk_base;
17267c478bd9Sstevel@tonic-gate
17277c478bd9Sstevel@tonic-gate /*
17287c478bd9Sstevel@tonic-gate * Add environ[] strings to the stack.
17297c478bd9Sstevel@tonic-gate */
17307c478bd9Sstevel@tonic-gate if (envp != NULL) {
17317c478bd9Sstevel@tonic-gate for (;;) {
1732134a1f4eSCasper H.S. Dik char *tmp = args->stk_strp;
17337c478bd9Sstevel@tonic-gate if (stk_getptr(args, envp, &sp))
17347c478bd9Sstevel@tonic-gate return (EFAULT);
17357c478bd9Sstevel@tonic-gate if (sp == NULL)
17367c478bd9Sstevel@tonic-gate break;
17377c478bd9Sstevel@tonic-gate if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
17387c478bd9Sstevel@tonic-gate return (error);
1739134a1f4eSCasper H.S. Dik if (args->scrubenv && strncmp(tmp, "LD_", 3) == 0) {
1740134a1f4eSCasper H.S. Dik /* Undo the copied string */
1741134a1f4eSCasper H.S. Dik args->stk_strp = tmp;
17427e12ceb3SToomas Soome *(args->stk_offp++) = 0;
1743134a1f4eSCasper H.S. Dik }
17447c478bd9Sstevel@tonic-gate envp += ptrsize;
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
17487c478bd9Sstevel@tonic-gate args->ne = args->na - argc;
17497c478bd9Sstevel@tonic-gate
17507c478bd9Sstevel@tonic-gate /*
17519acbbeafSnn * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and
17529acbbeafSnn * AT_SUN_EMULATOR strings to the stack.
17537c478bd9Sstevel@tonic-gate */
17547c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
17557c478bd9Sstevel@tonic-gate if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
17567c478bd9Sstevel@tonic-gate return (error);
17577c478bd9Sstevel@tonic-gate if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
17587c478bd9Sstevel@tonic-gate return (error);
17599acbbeafSnn if (args->brandname != NULL &&
1760d4204c85Sraf (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0)
17619acbbeafSnn return (error);
17629acbbeafSnn if (args->emulator != NULL &&
1763d4204c85Sraf (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0)
17649acbbeafSnn return (error);
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate
17677c478bd9Sstevel@tonic-gate /*
17687c478bd9Sstevel@tonic-gate * Compute the size of the stack. This includes all the pointers,
17697c478bd9Sstevel@tonic-gate * the space reserved for the aux vector, and all the strings.
17707c478bd9Sstevel@tonic-gate * The total number of pointers is args->na (which is argc + envc)
17717c478bd9Sstevel@tonic-gate * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
17727c478bd9Sstevel@tonic-gate * after the last argument (i.e. argv[argc]); (3) the NULL after the
17737c478bd9Sstevel@tonic-gate * last environment variable (i.e. envp[envc]); and (4) the NULL after
17747c478bd9Sstevel@tonic-gate * all the strings, at the very top of the stack.
17757c478bd9Sstevel@tonic-gate */
17767c478bd9Sstevel@tonic-gate size = (args->na + 4) * args->to_ptrsize + args->auxsize +
17777c478bd9Sstevel@tonic-gate (args->stk_strp - args->stk_base);
17787c478bd9Sstevel@tonic-gate
17797c478bd9Sstevel@tonic-gate /*
17807c478bd9Sstevel@tonic-gate * Pad the string section with zeroes to align the stack size.
17817c478bd9Sstevel@tonic-gate */
17827c478bd9Sstevel@tonic-gate pad = P2NPHASE(size, args->stk_align);
17837c478bd9Sstevel@tonic-gate
17847c478bd9Sstevel@tonic-gate if (STK_AVAIL(args) < pad)
17857c478bd9Sstevel@tonic-gate return (E2BIG);
17867c478bd9Sstevel@tonic-gate
17877c478bd9Sstevel@tonic-gate args->usrstack_size = size + pad;
17887c478bd9Sstevel@tonic-gate
17897c478bd9Sstevel@tonic-gate while (pad-- != 0)
17907c478bd9Sstevel@tonic-gate *args->stk_strp++ = 0;
17917c478bd9Sstevel@tonic-gate
17927c478bd9Sstevel@tonic-gate args->nc = args->stk_strp - args->stk_base;
17937c478bd9Sstevel@tonic-gate
17947c478bd9Sstevel@tonic-gate return (0);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate
17977c478bd9Sstevel@tonic-gate static int
stk_copyout(uarg_t * args,char * usrstack,void ** auxvpp,user_t * up)17987c478bd9Sstevel@tonic-gate stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate size_t ptrsize = args->to_ptrsize;
18017c478bd9Sstevel@tonic-gate ssize_t pslen;
18027c478bd9Sstevel@tonic-gate char *kstrp = args->stk_base;
18037c478bd9Sstevel@tonic-gate char *ustrp = usrstack - args->nc - ptrsize;
18047c478bd9Sstevel@tonic-gate char *usp = usrstack - args->usrstack_size;
18057c478bd9Sstevel@tonic-gate int *offp = (int *)(args->stk_base + args->stk_size);
18067c478bd9Sstevel@tonic-gate int envc = args->ne;
18077c478bd9Sstevel@tonic-gate int argc = args->na - envc;
18087c478bd9Sstevel@tonic-gate int i;
18097c478bd9Sstevel@tonic-gate
18107c478bd9Sstevel@tonic-gate /*
18117c478bd9Sstevel@tonic-gate * Record argc for /proc.
18127c478bd9Sstevel@tonic-gate */
18137c478bd9Sstevel@tonic-gate up->u_argc = argc;
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate /*
18167c478bd9Sstevel@tonic-gate * Put argc on the stack. Note that even though it's an int,
18177c478bd9Sstevel@tonic-gate * it always consumes ptrsize bytes (for alignment).
18187c478bd9Sstevel@tonic-gate */
18197c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
18207c478bd9Sstevel@tonic-gate return (-1);
18217c478bd9Sstevel@tonic-gate
18227c478bd9Sstevel@tonic-gate /*
18237c478bd9Sstevel@tonic-gate * Add argc space (ptrsize) to usp and record argv for /proc.
18247c478bd9Sstevel@tonic-gate */
18257c478bd9Sstevel@tonic-gate up->u_argv = (uintptr_t)(usp += ptrsize);
18267c478bd9Sstevel@tonic-gate
18277c478bd9Sstevel@tonic-gate /*
18287c478bd9Sstevel@tonic-gate * Put the argv[] pointers on the stack.
18297c478bd9Sstevel@tonic-gate */
18307c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++, usp += ptrsize)
18317c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
18327c478bd9Sstevel@tonic-gate return (-1);
18337c478bd9Sstevel@tonic-gate
18347c478bd9Sstevel@tonic-gate /*
18357c478bd9Sstevel@tonic-gate * Copy arguments to u_psargs.
18367c478bd9Sstevel@tonic-gate */
18377c478bd9Sstevel@tonic-gate pslen = MIN(args->arglen, PSARGSZ) - 1;
18387c478bd9Sstevel@tonic-gate for (i = 0; i < pslen; i++)
18397c478bd9Sstevel@tonic-gate up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
18407c478bd9Sstevel@tonic-gate while (i < PSARGSZ)
18417c478bd9Sstevel@tonic-gate up->u_psargs[i++] = '\0';
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate /*
18447c478bd9Sstevel@tonic-gate * Add space for argv[]'s NULL terminator (ptrsize) to usp and
18457c478bd9Sstevel@tonic-gate * record envp for /proc.
18467c478bd9Sstevel@tonic-gate */
18477c478bd9Sstevel@tonic-gate up->u_envp = (uintptr_t)(usp += ptrsize);
18487c478bd9Sstevel@tonic-gate
18497c478bd9Sstevel@tonic-gate /*
18507c478bd9Sstevel@tonic-gate * Put the envp[] pointers on the stack.
18517c478bd9Sstevel@tonic-gate */
18527c478bd9Sstevel@tonic-gate for (i = 0; i < envc; i++, usp += ptrsize)
18537c478bd9Sstevel@tonic-gate if (stk_putptr(args, usp, &ustrp[*--offp]))
18547c478bd9Sstevel@tonic-gate return (-1);
18557c478bd9Sstevel@tonic-gate
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate * Add space for envp[]'s NULL terminator (ptrsize) to usp and
18587c478bd9Sstevel@tonic-gate * remember where the stack ends, which is also where auxv begins.
18597c478bd9Sstevel@tonic-gate */
18607c478bd9Sstevel@tonic-gate args->stackend = usp += ptrsize;
18617c478bd9Sstevel@tonic-gate
18627c478bd9Sstevel@tonic-gate /*
18637c478bd9Sstevel@tonic-gate * Put all the argv[], envp[], and auxv strings on the stack.
18647c478bd9Sstevel@tonic-gate */
18657c478bd9Sstevel@tonic-gate if (copyout(args->stk_base, ustrp, args->nc))
18667c478bd9Sstevel@tonic-gate return (-1);
18677c478bd9Sstevel@tonic-gate
18687c478bd9Sstevel@tonic-gate /*
18697c478bd9Sstevel@tonic-gate * Fill in the aux vector now that we know the user stack addresses
18709acbbeafSnn * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and
18719acbbeafSnn * AT_SUN_EMULATOR strings.
18727c478bd9Sstevel@tonic-gate */
18737c478bd9Sstevel@tonic-gate if (auxvpp != NULL && *auxvpp != NULL) {
18747c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
18757c478bd9Sstevel@tonic-gate auxv_t **a = (auxv_t **)auxvpp;
18767c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
18777c478bd9Sstevel@tonic-gate ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
18789acbbeafSnn if (args->brandname != NULL)
18799acbbeafSnn ADDAUX(*a,
18809acbbeafSnn AT_SUN_BRANDNAME, (long)&ustrp[*--offp])
18819acbbeafSnn if (args->emulator != NULL)
18829acbbeafSnn ADDAUX(*a,
18839acbbeafSnn AT_SUN_EMULATOR, (long)&ustrp[*--offp])
18847c478bd9Sstevel@tonic-gate } else {
18857c478bd9Sstevel@tonic-gate auxv32_t **a = (auxv32_t **)auxvpp;
18867c478bd9Sstevel@tonic-gate ADDAUX(*a,
18877c478bd9Sstevel@tonic-gate AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
18887c478bd9Sstevel@tonic-gate ADDAUX(*a,
18899acbbeafSnn AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp])
18909acbbeafSnn if (args->brandname != NULL)
18919acbbeafSnn ADDAUX(*a, AT_SUN_BRANDNAME,
18929acbbeafSnn (int)(uintptr_t)&ustrp[*--offp])
18939acbbeafSnn if (args->emulator != NULL)
18949acbbeafSnn ADDAUX(*a, AT_SUN_EMULATOR,
18959acbbeafSnn (int)(uintptr_t)&ustrp[*--offp])
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate }
18987c478bd9Sstevel@tonic-gate
18997c478bd9Sstevel@tonic-gate return (0);
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate
1902d2a70789SRichard Lowe /*
1903d2a70789SRichard Lowe * Though the actual stack base is constant, slew the %sp by a random aligned
1904d2a70789SRichard Lowe * amount in [0,aslr_max_stack_skew). Mostly, this makes life slightly more
1905d2a70789SRichard Lowe * complicated for buffer overflows hoping to overwrite the return address.
1906d2a70789SRichard Lowe *
1907d2a70789SRichard Lowe * On some platforms this helps avoid cache thrashing when identical processes
1908d2a70789SRichard Lowe * simultaneously share caches that don't provide enough associativity
1909d2a70789SRichard Lowe * (e.g. sun4v systems). In this case stack slewing makes the same hot stack
1910d2a70789SRichard Lowe * variables in different processes live in different cache sets increasing
1911d2a70789SRichard Lowe * effective associativity.
1912d2a70789SRichard Lowe */
1913d2a70789SRichard Lowe size_t
exec_get_spslew(void)1914d2a70789SRichard Lowe exec_get_spslew(void)
1915d2a70789SRichard Lowe {
1916d2a70789SRichard Lowe #ifdef sun4v
1917d2a70789SRichard Lowe static uint_t sp_color_stride = 16;
1918d2a70789SRichard Lowe static uint_t sp_color_mask = 0x1f;
1919d2a70789SRichard Lowe static uint_t sp_current_color = (uint_t)-1;
1920d2a70789SRichard Lowe #endif
1921d2a70789SRichard Lowe size_t off;
1922d2a70789SRichard Lowe
1923d2a70789SRichard Lowe ASSERT(ISP2(aslr_max_stack_skew));
1924d2a70789SRichard Lowe
1925d2a70789SRichard Lowe if ((aslr_max_stack_skew == 0) ||
1926d2a70789SRichard Lowe !secflag_enabled(curproc, PROC_SEC_ASLR)) {
1927d2a70789SRichard Lowe #ifdef sun4v
1928d2a70789SRichard Lowe uint_t spcolor = atomic_inc_32_nv(&sp_current_color);
1929d2a70789SRichard Lowe return ((size_t)((spcolor & sp_color_mask) *
1930d2a70789SRichard Lowe SA(sp_color_stride)));
1931d2a70789SRichard Lowe #else
1932d2a70789SRichard Lowe return (0);
1933d2a70789SRichard Lowe #endif
1934d2a70789SRichard Lowe }
1935d2a70789SRichard Lowe
1936d2a70789SRichard Lowe (void) random_get_pseudo_bytes((uint8_t *)&off, sizeof (off));
1937d2a70789SRichard Lowe return (SA(P2PHASE(off, aslr_max_stack_skew)));
1938d2a70789SRichard Lowe }
1939d2a70789SRichard Lowe
19407c478bd9Sstevel@tonic-gate /*
19417c478bd9Sstevel@tonic-gate * Initialize a new user stack with the specified arguments and environment.
19427c478bd9Sstevel@tonic-gate * The initial user stack layout is as follows:
19437c478bd9Sstevel@tonic-gate *
19447c478bd9Sstevel@tonic-gate * User Stack
1945284ce987SPatrick Mooney * +---------------+
1946284ce987SPatrick Mooney * | |
1947284ce987SPatrick Mooney * | stack guard |
1948284ce987SPatrick Mooney * | (64-bit only) |
1949284ce987SPatrick Mooney * | |
1950284ce987SPatrick Mooney * +...............+ <--- stack limit (base - curproc->p_stk_ctl)
1951284ce987SPatrick Mooney * . .
1952284ce987SPatrick Mooney * . .
1953284ce987SPatrick Mooney * . .
19547c478bd9Sstevel@tonic-gate * +---------------+ <--- curproc->p_usrstack
1955102033aaSdp * | |
1956102033aaSdp * | slew |
1957102033aaSdp * | |
1958102033aaSdp * +---------------+
19597c478bd9Sstevel@tonic-gate * | NULL |
19607c478bd9Sstevel@tonic-gate * +---------------+
19617c478bd9Sstevel@tonic-gate * | |
19627c478bd9Sstevel@tonic-gate * | auxv strings |
19637c478bd9Sstevel@tonic-gate * | |
19647c478bd9Sstevel@tonic-gate * +---------------+
19657c478bd9Sstevel@tonic-gate * | |
19667c478bd9Sstevel@tonic-gate * | envp strings |
19677c478bd9Sstevel@tonic-gate * | |
19687c478bd9Sstevel@tonic-gate * +---------------+
19697c478bd9Sstevel@tonic-gate * | |
19707c478bd9Sstevel@tonic-gate * | argv strings |
19717c478bd9Sstevel@tonic-gate * | |
19727c478bd9Sstevel@tonic-gate * +---------------+ <--- ustrp
19737c478bd9Sstevel@tonic-gate * | |
19747c478bd9Sstevel@tonic-gate * | aux vector |
19757c478bd9Sstevel@tonic-gate * | |
19767c478bd9Sstevel@tonic-gate * +---------------+ <--- auxv
19777c478bd9Sstevel@tonic-gate * | NULL |
19787c478bd9Sstevel@tonic-gate * +---------------+
19797c478bd9Sstevel@tonic-gate * | envp[envc-1] |
19807c478bd9Sstevel@tonic-gate * +---------------+
19817c478bd9Sstevel@tonic-gate * | ... |
19827c478bd9Sstevel@tonic-gate * +---------------+
19837c478bd9Sstevel@tonic-gate * | envp[0] |
19847c478bd9Sstevel@tonic-gate * +---------------+ <--- envp[]
19857c478bd9Sstevel@tonic-gate * | NULL |
19867c478bd9Sstevel@tonic-gate * +---------------+
19877c478bd9Sstevel@tonic-gate * | argv[argc-1] |
19887c478bd9Sstevel@tonic-gate * +---------------+
19897c478bd9Sstevel@tonic-gate * | ... |
19907c478bd9Sstevel@tonic-gate * +---------------+
19917c478bd9Sstevel@tonic-gate * | argv[0] |
19927c478bd9Sstevel@tonic-gate * +---------------+ <--- argv[]
19937c478bd9Sstevel@tonic-gate * | argc |
19947c478bd9Sstevel@tonic-gate * +---------------+ <--- stack base
1995284ce987SPatrick Mooney *
1996284ce987SPatrick Mooney * In 64-bit processes, a stack guard segment is allocated at the address
1997284ce987SPatrick Mooney * immediately below where the stack limit ends. This protects new library
1998284ce987SPatrick Mooney * mappings (such as the linker) from being placed in relatively dangerous
1999284ce987SPatrick Mooney * proximity to the stack.
20007c478bd9Sstevel@tonic-gate */
20017c478bd9Sstevel@tonic-gate int
exec_args(execa_t * uap,uarg_t * args,intpdata_t * intp,void ** auxvpp)20027c478bd9Sstevel@tonic-gate exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate size_t size;
20057c478bd9Sstevel@tonic-gate int error;
20067c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
20077c478bd9Sstevel@tonic-gate user_t *up = PTOU(p);
20087c478bd9Sstevel@tonic-gate char *usrstack;
20097c478bd9Sstevel@tonic-gate rctl_entity_p_t e;
20107c478bd9Sstevel@tonic-gate struct as *as;
2011ec25b48fSsusans extern int use_stk_lpg;
2012102033aaSdp size_t sp_slew;
2013284ce987SPatrick Mooney #if defined(_LP64)
2014284ce987SPatrick Mooney const size_t sg_sz = (stack_guard_seg_sz & PAGEMASK);
2015284ce987SPatrick Mooney #endif /* defined(_LP64) */
20167c478bd9Sstevel@tonic-gate
20177c478bd9Sstevel@tonic-gate args->from_model = p->p_model;
20187c478bd9Sstevel@tonic-gate if (p->p_model == DATAMODEL_NATIVE) {
20197c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (long);
20207c478bd9Sstevel@tonic-gate } else {
20217c478bd9Sstevel@tonic-gate args->from_ptrsize = sizeof (int32_t);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate
20247c478bd9Sstevel@tonic-gate if (args->to_model == DATAMODEL_NATIVE) {
20257c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (long);
20267c478bd9Sstevel@tonic-gate args->ncargs = NCARGS;
20277c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN;
2028a0de58d6SRoger A. Faulkner if (args->addr32)
2029a0de58d6SRoger A. Faulkner usrstack = (char *)USRSTACK64_32;
2030a0de58d6SRoger A. Faulkner else
2031a0de58d6SRoger A. Faulkner usrstack = (char *)USRSTACK;
20327c478bd9Sstevel@tonic-gate } else {
20337c478bd9Sstevel@tonic-gate args->to_ptrsize = sizeof (int32_t);
20347c478bd9Sstevel@tonic-gate args->ncargs = NCARGS32;
20357c478bd9Sstevel@tonic-gate args->stk_align = STACK_ALIGN32;
20367c478bd9Sstevel@tonic-gate usrstack = (char *)USRSTACK32;
20377c478bd9Sstevel@tonic-gate }
20387c478bd9Sstevel@tonic-gate
20397c478bd9Sstevel@tonic-gate ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
20407c478bd9Sstevel@tonic-gate
20417c478bd9Sstevel@tonic-gate #if defined(__sparc)
20427c478bd9Sstevel@tonic-gate /*
20437c478bd9Sstevel@tonic-gate * Make sure user register windows are empty before
20447c478bd9Sstevel@tonic-gate * attempting to make a new stack.
20457c478bd9Sstevel@tonic-gate */
20467c478bd9Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL);
20477c478bd9Sstevel@tonic-gate #endif
20487c478bd9Sstevel@tonic-gate
20497c478bd9Sstevel@tonic-gate for (size = PAGESIZE; ; size *= 2) {
20507c478bd9Sstevel@tonic-gate args->stk_size = size;
20517c478bd9Sstevel@tonic-gate args->stk_base = kmem_alloc(size, KM_SLEEP);
20527c478bd9Sstevel@tonic-gate args->stk_strp = args->stk_base;
20537c478bd9Sstevel@tonic-gate args->stk_offp = (int *)(args->stk_base + size);
20547c478bd9Sstevel@tonic-gate error = stk_copyin(uap, args, intp, auxvpp);
20557c478bd9Sstevel@tonic-gate if (error == 0)
20567c478bd9Sstevel@tonic-gate break;
20577c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, size);
20587c478bd9Sstevel@tonic-gate if (error != E2BIG && error != ENAMETOOLONG)
20597c478bd9Sstevel@tonic-gate return (error);
20607c478bd9Sstevel@tonic-gate if (size >= args->ncargs)
20617c478bd9Sstevel@tonic-gate return (E2BIG);
20627c478bd9Sstevel@tonic-gate }
20637c478bd9Sstevel@tonic-gate
20647c478bd9Sstevel@tonic-gate size = args->usrstack_size;
20657c478bd9Sstevel@tonic-gate
20667c478bd9Sstevel@tonic-gate ASSERT(error == 0);
20677c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(size, args->stk_align) == 0);
20687c478bd9Sstevel@tonic-gate ASSERT((ssize_t)STK_AVAIL(args) >= 0);
20697c478bd9Sstevel@tonic-gate
20707c478bd9Sstevel@tonic-gate if (size > args->ncargs) {
20717c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
20727c478bd9Sstevel@tonic-gate return (E2BIG);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate
20757c478bd9Sstevel@tonic-gate /*
20767c478bd9Sstevel@tonic-gate * Leave only the current lwp and force the other lwps to exit.
20777c478bd9Sstevel@tonic-gate * If another lwp beat us to the punch by calling exit(), bail out.
20787c478bd9Sstevel@tonic-gate */
20797c478bd9Sstevel@tonic-gate if ((error = exitlwps(0)) != 0) {
20807c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
20817c478bd9Sstevel@tonic-gate return (error);
20827c478bd9Sstevel@tonic-gate }
20837c478bd9Sstevel@tonic-gate
20847c478bd9Sstevel@tonic-gate /*
20857c478bd9Sstevel@tonic-gate * Revoke any doors created by the process.
20867c478bd9Sstevel@tonic-gate */
20877c478bd9Sstevel@tonic-gate if (p->p_door_list)
20887c478bd9Sstevel@tonic-gate door_exit();
20897c478bd9Sstevel@tonic-gate
20907c478bd9Sstevel@tonic-gate /*
20917c478bd9Sstevel@tonic-gate * Release schedctl data structures.
20927c478bd9Sstevel@tonic-gate */
20937c478bd9Sstevel@tonic-gate if (p->p_pagep)
20947c478bd9Sstevel@tonic-gate schedctl_proc_cleanup();
20957c478bd9Sstevel@tonic-gate
20967c478bd9Sstevel@tonic-gate /*
20977c478bd9Sstevel@tonic-gate * Clean up any DTrace helpers for the process.
20987c478bd9Sstevel@tonic-gate */
20997c478bd9Sstevel@tonic-gate if (p->p_dtrace_helpers != NULL) {
21007c478bd9Sstevel@tonic-gate ASSERT(dtrace_helpers_cleanup != NULL);
21019d7cab14SPatrick Mooney (*dtrace_helpers_cleanup)(p);
21027c478bd9Sstevel@tonic-gate }
21037c478bd9Sstevel@tonic-gate
21047c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
21057c478bd9Sstevel@tonic-gate /*
21067c478bd9Sstevel@tonic-gate * Cleanup the DTrace provider associated with this process.
21077c478bd9Sstevel@tonic-gate */
21087c478bd9Sstevel@tonic-gate if (p->p_dtrace_probes) {
21097c478bd9Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr != NULL);
21107c478bd9Sstevel@tonic-gate dtrace_fasttrap_exec_ptr(p);
21117c478bd9Sstevel@tonic-gate }
21127c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
21137c478bd9Sstevel@tonic-gate
21147c478bd9Sstevel@tonic-gate /*
21157c478bd9Sstevel@tonic-gate * discard the lwpchan cache.
21167c478bd9Sstevel@tonic-gate */
21177c478bd9Sstevel@tonic-gate if (p->p_lcp != NULL)
21187c478bd9Sstevel@tonic-gate lwpchan_destroy_cache(1);
21197c478bd9Sstevel@tonic-gate
21207c478bd9Sstevel@tonic-gate /*
21217c478bd9Sstevel@tonic-gate * Delete the POSIX timers.
21227c478bd9Sstevel@tonic-gate */
21237c478bd9Sstevel@tonic-gate if (p->p_itimer != NULL)
21247c478bd9Sstevel@tonic-gate timer_exit();
21257c478bd9Sstevel@tonic-gate
2126e0cf54a5SRoger A. Faulkner /*
2127e0cf54a5SRoger A. Faulkner * Delete the ITIMER_REALPROF interval timer.
2128e0cf54a5SRoger A. Faulkner * The other ITIMER_* interval timers are specified
2129e0cf54a5SRoger A. Faulkner * to be inherited across exec().
2130e0cf54a5SRoger A. Faulkner */
2131e0cf54a5SRoger A. Faulkner delete_itimer_realprof();
2132e0cf54a5SRoger A. Faulkner
2133005d3febSMarek Pospisil if (AU_AUDITING())
21347c478bd9Sstevel@tonic-gate audit_exec(args->stk_base, args->stk_base + args->arglen,
2135134a1f4eSCasper H.S. Dik args->na - args->ne, args->ne, args->pfcred);
21367c478bd9Sstevel@tonic-gate
21377c478bd9Sstevel@tonic-gate /*
21387c478bd9Sstevel@tonic-gate * Ensure that we don't change resource associations while we
21397c478bd9Sstevel@tonic-gate * change address spaces.
21407c478bd9Sstevel@tonic-gate */
21417c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
21427c478bd9Sstevel@tonic-gate pool_barrier_enter();
21437c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
21447c478bd9Sstevel@tonic-gate
21457c478bd9Sstevel@tonic-gate /*
21467c478bd9Sstevel@tonic-gate * Destroy the old address space and create a new one.
21477c478bd9Sstevel@tonic-gate * From here on, any errors are fatal to the exec()ing process.
21487c478bd9Sstevel@tonic-gate * On error we return -1, which means the caller must SIGKILL
21497c478bd9Sstevel@tonic-gate * the process.
21507c478bd9Sstevel@tonic-gate */
21517c478bd9Sstevel@tonic-gate relvm();
21527c478bd9Sstevel@tonic-gate
21537c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
21547c478bd9Sstevel@tonic-gate pool_barrier_exit();
21557c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
21567c478bd9Sstevel@tonic-gate
21577c478bd9Sstevel@tonic-gate up->u_execsw = args->execswp;
21587c478bd9Sstevel@tonic-gate
21597c478bd9Sstevel@tonic-gate p->p_brkbase = NULL;
21607c478bd9Sstevel@tonic-gate p->p_brksize = 0;
2161ec25b48fSsusans p->p_brkpageszc = 0;
21627c478bd9Sstevel@tonic-gate p->p_stksize = 0;
2163ec25b48fSsusans p->p_stkpageszc = 0;
2164284ce987SPatrick Mooney p->p_stkg_start = 0;
2165284ce987SPatrick Mooney p->p_stkg_end = 0;
21667c478bd9Sstevel@tonic-gate p->p_model = args->to_model;
21677c478bd9Sstevel@tonic-gate p->p_usrstack = usrstack;
21687c478bd9Sstevel@tonic-gate p->p_stkprot = args->stk_prot;
21697c478bd9Sstevel@tonic-gate p->p_datprot = args->dat_prot;
21707c478bd9Sstevel@tonic-gate
21717c478bd9Sstevel@tonic-gate /*
21727c478bd9Sstevel@tonic-gate * Reset resource controls such that all controls are again active as
21737c478bd9Sstevel@tonic-gate * well as appropriate to the potentially new address model for the
21747c478bd9Sstevel@tonic-gate * process.
21757c478bd9Sstevel@tonic-gate */
21767c478bd9Sstevel@tonic-gate e.rcep_p.proc = p;
21777c478bd9Sstevel@tonic-gate e.rcep_t = RCENTITY_PROCESS;
21787c478bd9Sstevel@tonic-gate rctl_set_reset(p->p_rctls, p, &e);
21797c478bd9Sstevel@tonic-gate
2180ec25b48fSsusans /* Too early to call map_pgsz for the heap */
2181ec25b48fSsusans if (use_stk_lpg) {
2182ec25b48fSsusans p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0));
21837c478bd9Sstevel@tonic-gate }
21847c478bd9Sstevel@tonic-gate
2185ec25b48fSsusans mutex_enter(&p->p_lock);
2186ec25b48fSsusans p->p_flag |= SAUTOLPG; /* kernel controls page sizes */
2187ec25b48fSsusans mutex_exit(&p->p_lock);
2188ec25b48fSsusans
2189102033aaSdp sp_slew = exec_get_spslew();
2190102033aaSdp ASSERT(P2PHASE(sp_slew, args->stk_align) == 0);
2191d2a70789SRichard Lowe /* Be certain we don't underflow */
2192d2a70789SRichard Lowe VERIFY((curproc->p_usrstack - (size + sp_slew)) < curproc->p_usrstack);
2193102033aaSdp exec_set_sp(size + sp_slew);
21947c478bd9Sstevel@tonic-gate
21957c478bd9Sstevel@tonic-gate as = as_alloc();
21967c478bd9Sstevel@tonic-gate p->p_as = as;
2197c6939658Ssl as->a_proc = p;
2198a0de58d6SRoger A. Faulkner if (p->p_model == DATAMODEL_ILP32 || args->addr32)
21997c478bd9Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT32;
22007c478bd9Sstevel@tonic-gate (void) hat_setup(as->a_hat, HAT_ALLOC);
220105d3dc4bSpaulsan hat_join_srd(as->a_hat, args->ex_vp);
22027c478bd9Sstevel@tonic-gate
2203284ce987SPatrick Mooney /* Write out the contents of the new stack. */
2204102033aaSdp error = stk_copyout(args, usrstack - sp_slew, auxvpp, up);
22057c478bd9Sstevel@tonic-gate kmem_free(args->stk_base, args->stk_size);
2206284ce987SPatrick Mooney
2207284ce987SPatrick Mooney #if defined(_LP64)
2208284ce987SPatrick Mooney /* Add stack guard segment (if needed) after successful copyout */
2209284ce987SPatrick Mooney if (error == 0 && p->p_model == DATAMODEL_LP64 && sg_sz != 0) {
2210284ce987SPatrick Mooney seghole_crargs_t sca;
2211284ce987SPatrick Mooney caddr_t addr_end = (caddr_t)(((uintptr_t)usrstack -
2212284ce987SPatrick Mooney p->p_stk_ctl) & PAGEMASK);
2213284ce987SPatrick Mooney caddr_t addr_start = addr_end - sg_sz;
2214284ce987SPatrick Mooney
2215284ce987SPatrick Mooney DTRACE_PROBE4(stack__guard__chk, proc_t *, p,
2216284ce987SPatrick Mooney caddr_t, addr_start, caddr_t, addr_end, size_t, sg_sz);
2217284ce987SPatrick Mooney
2218284ce987SPatrick Mooney if (addr_end >= usrstack || addr_start >= addr_end ||
2219284ce987SPatrick Mooney valid_usr_range(addr_start, sg_sz, PROT_NONE, as,
2220284ce987SPatrick Mooney as->a_userlimit) != RANGE_OKAY) {
2221284ce987SPatrick Mooney return (E2BIG);
2222284ce987SPatrick Mooney }
2223284ce987SPatrick Mooney
2224284ce987SPatrick Mooney /* Create un-mappable area in AS with seg_hole */
2225284ce987SPatrick Mooney sca.name = "stack_guard";
2226284ce987SPatrick Mooney error = as_map(as, addr_start, sg_sz, seghole_create, &sca);
2227284ce987SPatrick Mooney if (error == 0) {
2228284ce987SPatrick Mooney p->p_stkg_start = (uintptr_t)addr_start;
2229284ce987SPatrick Mooney p->p_stkg_end = (uintptr_t)addr_start + sg_sz;
2230284ce987SPatrick Mooney }
2231284ce987SPatrick Mooney }
2232284ce987SPatrick Mooney #endif /* defined(_LP64) */
2233284ce987SPatrick Mooney
22347c478bd9Sstevel@tonic-gate return (error);
22357c478bd9Sstevel@tonic-gate }
2236