xref: /titanic_50/usr/src/uts/common/os/exec.c (revision a4aeef46cda1835da2b19f8f62b4526de6521e6c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/systm.h>
34 #include <sys/signal.h>
35 #include <sys/cred_impl.h>
36 #include <sys/policy.h>
37 #include <sys/user.h>
38 #include <sys/errno.h>
39 #include <sys/file.h>
40 #include <sys/vfs.h>
41 #include <sys/vnode.h>
42 #include <sys/mman.h>
43 #include <sys/acct.h>
44 #include <sys/cpuvar.h>
45 #include <sys/proc.h>
46 #include <sys/cmn_err.h>
47 #include <sys/debug.h>
48 #include <sys/pathname.h>
49 #include <sys/vm.h>
50 #include <sys/lgrp.h>
51 #include <sys/vtrace.h>
52 #include <sys/exec.h>
53 #include <sys/exechdr.h>
54 #include <sys/kmem.h>
55 #include <sys/prsystm.h>
56 #include <sys/modctl.h>
57 #include <sys/vmparam.h>
58 #include <sys/door.h>
59 #include <sys/schedctl.h>
60 #include <sys/utrap.h>
61 #include <sys/systeminfo.h>
62 #include <sys/stack.h>
63 #include <sys/rctl.h>
64 #include <sys/dtrace.h>
65 #include <sys/lwpchan_impl.h>
66 #include <sys/pool.h>
67 #include <sys/sdt.h>
68 #include <sys/brand.h>
69 
70 #include <c2/audit.h>
71 
72 #include <vm/hat.h>
73 #include <vm/anon.h>
74 #include <vm/as.h>
75 #include <vm/seg.h>
76 #include <vm/seg_vn.h>
77 
78 #define	PRIV_RESET		0x01	/* needs to reset privs */
79 #define	PRIV_SETID		0x02	/* needs to change uids */
80 #define	PRIV_SETUGID		0x04	/* is setuid/setgid/forced privs */
81 #define	PRIV_INCREASE		0x08	/* child runs with more privs */
82 #define	MAC_FLAGS		0x10	/* need to adjust MAC flags */
83 
84 static int execsetid(struct vnode *, struct vattr *, uid_t *, uid_t *);
85 static int hold_execsw(struct execsw *);
86 
87 uint_t auxv_hwcap = 0;	/* auxv AT_SUN_HWCAP value; determined on the fly */
88 #if defined(_SYSCALL32_IMPL)
89 uint_t auxv_hwcap32 = 0;	/* 32-bit version of auxv_hwcap */
90 #endif
91 
92 #define	PSUIDFLAGS		(SNOCD|SUGID)
93 
94 /*
95  * exec() - wrapper around exece providing NULL environment pointer
96  */
97 int
98 exec(const char *fname, const char **argp)
99 {
100 	return (exece(fname, argp, NULL));
101 }
102 
103 /*
104  * exece() - system call wrapper around exec_common()
105  */
106 int
107 exece(const char *fname, const char **argp, const char **envp)
108 {
109 	int error;
110 
111 	error = exec_common(fname, argp, envp, EBA_NONE);
112 	return (error ? (set_errno(error)) : 0);
113 }
114 
115 int
116 exec_common(const char *fname, const char **argp, const char **envp,
117     int brand_action)
118 {
119 	vnode_t *vp = NULL, *dir = NULL, *tmpvp = NULL;
120 	proc_t *p = ttoproc(curthread);
121 	klwp_t *lwp = ttolwp(curthread);
122 	struct user *up = PTOU(p);
123 	long execsz;		/* temporary count of exec size */
124 	int i;
125 	int error;
126 	char exec_file[MAXCOMLEN+1];
127 	struct pathname pn;
128 	struct pathname resolvepn;
129 	struct uarg args;
130 	struct execa ua;
131 	k_sigset_t savedmask;
132 	lwpdir_t *lwpdir = NULL;
133 	tidhash_t *tidhash;
134 	lwpdir_t *old_lwpdir = NULL;
135 	uint_t old_lwpdir_sz;
136 	tidhash_t *old_tidhash;
137 	uint_t old_tidhash_sz;
138 	ret_tidhash_t *ret_tidhash;
139 	lwpent_t *lep;
140 	boolean_t brandme = B_FALSE;
141 
142 	/*
143 	 * exec() is not supported for the /proc agent lwp.
144 	 */
145 	if (curthread == p->p_agenttp)
146 		return (ENOTSUP);
147 
148 	if (brand_action != EBA_NONE) {
149 		/*
150 		 * Brand actions are not supported for processes that are not
151 		 * running in a branded zone.
152 		 */
153 		if (!ZONE_IS_BRANDED(p->p_zone))
154 			return (ENOTSUP);
155 
156 		if (brand_action == EBA_NATIVE) {
157 			/* Only branded processes can be unbranded */
158 			if (!PROC_IS_BRANDED(p))
159 				return (ENOTSUP);
160 		} else {
161 			/* Only unbranded processes can be branded */
162 			if (PROC_IS_BRANDED(p))
163 				return (ENOTSUP);
164 			brandme = B_TRUE;
165 		}
166 	} else {
167 		/*
168 		 * If this is a native zone, or if the process is already
169 		 * branded, then we don't need to do anything.  If this is
170 		 * a native process in a branded zone, we need to brand the
171 		 * process as it exec()s the new binary.
172 		 */
173 		if (ZONE_IS_BRANDED(p->p_zone) && !PROC_IS_BRANDED(p))
174 			brandme = B_TRUE;
175 	}
176 
177 	/*
178 	 * Inform /proc that an exec() has started.
179 	 * Hold signals that are ignored by default so that we will
180 	 * not be interrupted by a signal that will be ignored after
181 	 * successful completion of gexec().
182 	 */
183 	mutex_enter(&p->p_lock);
184 	prexecstart();
185 	schedctl_finish_sigblock(curthread);
186 	savedmask = curthread->t_hold;
187 	sigorset(&curthread->t_hold, &ignoredefault);
188 	mutex_exit(&p->p_lock);
189 
190 	/*
191 	 * Look up path name and remember last component for later.
192 	 * To help coreadm expand its %d token, we attempt to save
193 	 * the directory containing the executable in p_execdir. The
194 	 * first call to lookuppn() may fail and return EINVAL because
195 	 * dirvpp is non-NULL. In that case, we make a second call to
196 	 * lookuppn() with dirvpp set to NULL; p_execdir will be NULL,
197 	 * but coreadm is allowed to expand %d to the empty string and
198 	 * there are other cases in which that failure may occur.
199 	 */
200 	if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
201 		goto out;
202 	pn_alloc(&resolvepn);
203 	if ((error = lookuppn(&pn, &resolvepn, FOLLOW, &dir, &vp)) != 0) {
204 		pn_free(&resolvepn);
205 		pn_free(&pn);
206 		if (error != EINVAL)
207 			goto out;
208 
209 		dir = NULL;
210 		if ((error = pn_get((char *)fname, UIO_USERSPACE, &pn)) != 0)
211 			goto out;
212 		pn_alloc(&resolvepn);
213 		if ((error = lookuppn(&pn, &resolvepn, FOLLOW, NULLVPP,
214 		    &vp)) != 0) {
215 			pn_free(&resolvepn);
216 			pn_free(&pn);
217 			goto out;
218 		}
219 	}
220 	if (vp == NULL) {
221 		if (dir != NULL)
222 			VN_RELE(dir);
223 		error = ENOENT;
224 		pn_free(&resolvepn);
225 		pn_free(&pn);
226 		goto out;
227 	}
228 
229 	if ((error = secpolicy_basic_exec(CRED(), vp)) != 0) {
230 		if (dir != NULL)
231 			VN_RELE(dir);
232 		pn_free(&resolvepn);
233 		pn_free(&pn);
234 		VN_RELE(vp);
235 		goto out;
236 	}
237 
238 	/*
239 	 * We do not allow executing files in attribute directories.
240 	 * We test this by determining whether the resolved path
241 	 * contains a "/" when we're in an attribute directory;
242 	 * only if the pathname does not contain a "/" the resolved path
243 	 * points to a file in the current working (attribute) directory.
244 	 */
245 	if ((p->p_user.u_cdir->v_flag & V_XATTRDIR) != 0 &&
246 	    strchr(resolvepn.pn_path, '/') == NULL) {
247 		if (dir != NULL)
248 			VN_RELE(dir);
249 		error = EACCES;
250 		pn_free(&resolvepn);
251 		pn_free(&pn);
252 		VN_RELE(vp);
253 		goto out;
254 	}
255 
256 	bzero(exec_file, MAXCOMLEN+1);
257 	(void) strncpy(exec_file, pn.pn_path, MAXCOMLEN);
258 	bzero(&args, sizeof (args));
259 	args.pathname = resolvepn.pn_path;
260 	/* don't free resolvepn until we are done with args */
261 	pn_free(&pn);
262 
263 	/*
264 	 * Specific exec handlers, or policies determined via
265 	 * /etc/system may override the historical default.
266 	 */
267 	args.stk_prot = PROT_ZFOD;
268 	args.dat_prot = PROT_ZFOD;
269 
270 	CPU_STATS_ADD_K(sys, sysexec, 1);
271 	DTRACE_PROC1(exec, char *, args.pathname);
272 
273 	ua.fname = fname;
274 	ua.argp = argp;
275 	ua.envp = envp;
276 
277 	/* If necessary, brand this process before we start the exec. */
278 	if (brandme)
279 		brand_setbrand(p);
280 
281 	if ((error = gexec(&vp, &ua, &args, NULL, 0, &execsz,
282 	    exec_file, p->p_cred, brand_action)) != 0) {
283 		if (brandme)
284 			brand_clearbrand(p);
285 		VN_RELE(vp);
286 		if (dir != NULL)
287 			VN_RELE(dir);
288 		pn_free(&resolvepn);
289 		goto fail;
290 	}
291 
292 	/*
293 	 * Free floating point registers (sun4u only)
294 	 */
295 	ASSERT(lwp != NULL);
296 	lwp_freeregs(lwp, 1);
297 
298 	/*
299 	 * Free thread and process context ops.
300 	 */
301 	if (curthread->t_ctx)
302 		freectx(curthread, 1);
303 	if (p->p_pctx)
304 		freepctx(p, 1);
305 
306 	/*
307 	 * Remember file name for accounting; clear any cached DTrace predicate.
308 	 */
309 	up->u_acflag &= ~AFORK;
310 	bcopy(exec_file, up->u_comm, MAXCOMLEN+1);
311 	curthread->t_predcache = NULL;
312 
313 	/*
314 	 * Clear contract template state
315 	 */
316 	lwp_ctmpl_clear(lwp);
317 
318 	/*
319 	 * Save the directory in which we found the executable for expanding
320 	 * the %d token used in core file patterns.
321 	 */
322 	mutex_enter(&p->p_lock);
323 	tmpvp = p->p_execdir;
324 	p->p_execdir = dir;
325 	if (p->p_execdir != NULL)
326 		VN_HOLD(p->p_execdir);
327 	mutex_exit(&p->p_lock);
328 
329 	if (tmpvp != NULL)
330 		VN_RELE(tmpvp);
331 
332 	/*
333 	 * Reset stack state to the user stack, clear set of signals
334 	 * caught on the signal stack, and reset list of signals that
335 	 * restart system calls; the new program's environment should
336 	 * not be affected by detritus from the old program.  Any
337 	 * pending held signals remain held, so don't clear t_hold.
338 	 */
339 	mutex_enter(&p->p_lock);
340 	lwp->lwp_oldcontext = 0;
341 	lwp->lwp_ustack = 0;
342 	lwp->lwp_old_stk_ctl = 0;
343 	sigemptyset(&up->u_signodefer);
344 	sigemptyset(&up->u_sigonstack);
345 	sigemptyset(&up->u_sigresethand);
346 	lwp->lwp_sigaltstack.ss_sp = 0;
347 	lwp->lwp_sigaltstack.ss_size = 0;
348 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
349 
350 	/*
351 	 * Make saved resource limit == current resource limit.
352 	 */
353 	for (i = 0; i < RLIM_NLIMITS; i++) {
354 		/*CONSTCOND*/
355 		if (RLIM_SAVED(i)) {
356 			(void) rctl_rlimit_get(rctlproc_legacy[i], p,
357 			    &up->u_saved_rlimit[i]);
358 		}
359 	}
360 
361 	/*
362 	 * If the action was to catch the signal, then the action
363 	 * must be reset to SIG_DFL.
364 	 */
365 	sigdefault(p);
366 	p->p_flag &= ~(SNOWAIT|SJCTL);
367 	p->p_flag |= (SEXECED|SMSACCT|SMSFORK);
368 	up->u_signal[SIGCLD - 1] = SIG_DFL;
369 
370 	/*
371 	 * Delete the dot4 sigqueues/signotifies.
372 	 */
373 	sigqfree(p);
374 
375 	mutex_exit(&p->p_lock);
376 
377 	mutex_enter(&p->p_pflock);
378 	p->p_prof.pr_base = NULL;
379 	p->p_prof.pr_size = 0;
380 	p->p_prof.pr_off = 0;
381 	p->p_prof.pr_scale = 0;
382 	p->p_prof.pr_samples = 0;
383 	mutex_exit(&p->p_pflock);
384 
385 	ASSERT(curthread->t_schedctl == NULL);
386 
387 #if defined(__sparc)
388 	if (p->p_utraps != NULL)
389 		utrap_free(p);
390 #endif	/* __sparc */
391 
392 	/*
393 	 * Close all close-on-exec files.
394 	 */
395 	close_exec(P_FINFO(p));
396 	TRACE_2(TR_FAC_PROC, TR_PROC_EXEC, "proc_exec:p %p up %p", p, up);
397 
398 	/* Unbrand ourself if necessary. */
399 	if (PROC_IS_BRANDED(p) && (brand_action == EBA_NATIVE))
400 		brand_clearbrand(p);
401 
402 	setregs(&args);
403 
404 	/* Mark this as an executable vnode */
405 	mutex_enter(&vp->v_lock);
406 	vp->v_flag |= VVMEXEC;
407 	mutex_exit(&vp->v_lock);
408 
409 	VN_RELE(vp);
410 	if (dir != NULL)
411 		VN_RELE(dir);
412 	pn_free(&resolvepn);
413 
414 	/*
415 	 * Allocate a new lwp directory and lwpid hash table if necessary.
416 	 */
417 	if (curthread->t_tid != 1 || p->p_lwpdir_sz != 2) {
418 		lwpdir = kmem_zalloc(2 * sizeof (lwpdir_t), KM_SLEEP);
419 		lwpdir->ld_next = lwpdir + 1;
420 		tidhash = kmem_zalloc(2 * sizeof (tidhash_t), KM_SLEEP);
421 		if (p->p_lwpdir != NULL)
422 			lep = p->p_lwpdir[curthread->t_dslot].ld_entry;
423 		else
424 			lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
425 	}
426 
427 	if (PROC_IS_BRANDED(p))
428 		BROP(p)->b_exec();
429 
430 	mutex_enter(&p->p_lock);
431 	prbarrier(p);
432 
433 	/*
434 	 * Reset lwp id to the default value of 1.
435 	 * This is a single-threaded process now
436 	 * and lwp #1 is lwp_wait()able by default.
437 	 * The t_unpark flag should not be inherited.
438 	 */
439 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
440 	curthread->t_tid = 1;
441 	kpreempt_disable();
442 	ASSERT(curthread->t_lpl != NULL);
443 	p->p_t1_lgrpid = curthread->t_lpl->lpl_lgrpid;
444 	kpreempt_enable();
445 	if (p->p_tr_lgrpid != LGRP_NONE && p->p_tr_lgrpid != p->p_t1_lgrpid) {
446 		lgrp_update_trthr_migrations(1);
447 	}
448 	curthread->t_unpark = 0;
449 	curthread->t_proc_flag |= TP_TWAIT;
450 	curthread->t_proc_flag &= ~TP_DAEMON;	/* daemons shouldn't exec */
451 	p->p_lwpdaemon = 0;			/* but oh well ... */
452 	p->p_lwpid = 1;
453 
454 	/*
455 	 * Install the newly-allocated lwp directory and lwpid hash table
456 	 * and insert the current thread into the new hash table.
457 	 */
458 	if (lwpdir != NULL) {
459 		old_lwpdir = p->p_lwpdir;
460 		old_lwpdir_sz = p->p_lwpdir_sz;
461 		old_tidhash = p->p_tidhash;
462 		old_tidhash_sz = p->p_tidhash_sz;
463 		p->p_lwpdir = p->p_lwpfree = lwpdir;
464 		p->p_lwpdir_sz = 2;
465 		lep->le_thread = curthread;
466 		lep->le_lwpid = curthread->t_tid;
467 		lep->le_start = curthread->t_start;
468 		lwp_hash_in(p, lep, tidhash, 2, 0);
469 		p->p_tidhash = tidhash;
470 		p->p_tidhash_sz = 2;
471 	}
472 	ret_tidhash = p->p_ret_tidhash;
473 	p->p_ret_tidhash = NULL;
474 
475 	/*
476 	 * Restore the saved signal mask and
477 	 * inform /proc that the exec() has finished.
478 	 */
479 	curthread->t_hold = savedmask;
480 	prexecend();
481 	mutex_exit(&p->p_lock);
482 	if (old_lwpdir) {
483 		kmem_free(old_lwpdir, old_lwpdir_sz * sizeof (lwpdir_t));
484 		kmem_free(old_tidhash, old_tidhash_sz * sizeof (tidhash_t));
485 	}
486 	while (ret_tidhash != NULL) {
487 		ret_tidhash_t *next = ret_tidhash->rth_next;
488 		kmem_free(ret_tidhash->rth_tidhash,
489 		    ret_tidhash->rth_tidhash_sz * sizeof (tidhash_t));
490 		kmem_free(ret_tidhash, sizeof (*ret_tidhash));
491 		ret_tidhash = next;
492 	}
493 
494 	ASSERT(error == 0);
495 	DTRACE_PROC(exec__success);
496 	return (0);
497 
498 fail:
499 	DTRACE_PROC1(exec__failure, int, error);
500 out:		/* error return */
501 	mutex_enter(&p->p_lock);
502 	curthread->t_hold = savedmask;
503 	prexecend();
504 	mutex_exit(&p->p_lock);
505 	ASSERT(error != 0);
506 	return (error);
507 }
508 
509 
510 /*
511  * Perform generic exec duties and switchout to object-file specific
512  * handler.
513  */
514 int
515 gexec(
516 	struct vnode **vpp,
517 	struct execa *uap,
518 	struct uarg *args,
519 	struct intpdata *idatap,
520 	int level,
521 	long *execsz,
522 	caddr_t exec_file,
523 	struct cred *cred,
524 	int brand_action)
525 {
526 	struct vnode *vp, *execvp = NULL;
527 	proc_t *pp = ttoproc(curthread);
528 	struct execsw *eswp;
529 	int error = 0;
530 	int suidflags = 0;
531 	ssize_t resid;
532 	uid_t uid, gid;
533 	struct vattr vattr;
534 	char magbuf[MAGIC_BYTES];
535 	int setid;
536 	cred_t *oldcred, *newcred = NULL;
537 	int privflags = 0;
538 	int setidfl;
539 
540 	/*
541 	 * If the SNOCD or SUGID flag is set, turn it off and remember the
542 	 * previous setting so we can restore it if we encounter an error.
543 	 */
544 	if (level == 0 && (pp->p_flag & PSUIDFLAGS)) {
545 		mutex_enter(&pp->p_lock);
546 		suidflags = pp->p_flag & PSUIDFLAGS;
547 		pp->p_flag &= ~PSUIDFLAGS;
548 		mutex_exit(&pp->p_lock);
549 	}
550 
551 	if ((error = execpermissions(*vpp, &vattr, args)) != 0)
552 		goto bad_noclose;
553 
554 	/* need to open vnode for stateful file systems */
555 	if ((error = VOP_OPEN(vpp, FREAD, CRED(), NULL)) != 0)
556 		goto bad_noclose;
557 	vp = *vpp;
558 
559 	/*
560 	 * Note: to support binary compatibility with SunOS a.out
561 	 * executables, we read in the first four bytes, as the
562 	 * magic number is in bytes 2-3.
563 	 */
564 	if (error = vn_rdwr(UIO_READ, vp, magbuf, sizeof (magbuf),
565 	    (offset_t)0, UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
566 		goto bad;
567 	if (resid != 0)
568 		goto bad;
569 
570 	if ((eswp = findexec_by_hdr(magbuf)) == NULL)
571 		goto bad;
572 
573 	if (level == 0 &&
574 	    (privflags = execsetid(vp, &vattr, &uid, &gid)) != 0) {
575 
576 		newcred = cred = crdup(cred);
577 
578 		/* If we can, drop the PA bit */
579 		if ((privflags & PRIV_RESET) != 0)
580 			priv_adjust_PA(cred);
581 
582 		if (privflags & PRIV_SETID) {
583 			cred->cr_uid = uid;
584 			cred->cr_gid = gid;
585 			cred->cr_suid = uid;
586 			cred->cr_sgid = gid;
587 		}
588 
589 		if (privflags & MAC_FLAGS) {
590 			if (!(CR_FLAGS(cred) & NET_MAC_AWARE_INHERIT))
591 				CR_FLAGS(cred) &= ~NET_MAC_AWARE;
592 			CR_FLAGS(cred) &= ~NET_MAC_AWARE_INHERIT;
593 		}
594 
595 		/*
596 		 * Implement the privilege updates:
597 		 *
598 		 * Restrict with L:
599 		 *
600 		 *	I' = I & L
601 		 *
602 		 *	E' = P' = (I' + F) & A
603 		 *
604 		 * But if running under ptrace, we cap I with P.
605 		 */
606 		if ((privflags & PRIV_RESET) != 0) {
607 			if ((privflags & PRIV_INCREASE) != 0 &&
608 			    (pp->p_proc_flag & P_PR_PTRACE) != 0)
609 				priv_intersect(&CR_OPPRIV(cred),
610 				    &CR_IPRIV(cred));
611 			priv_intersect(&CR_LPRIV(cred), &CR_IPRIV(cred));
612 			CR_EPRIV(cred) = CR_PPRIV(cred) = CR_IPRIV(cred);
613 			priv_adjust_PA(cred);
614 		}
615 	}
616 
617 	/* SunOS 4.x buy-back */
618 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) &&
619 	    (vattr.va_mode & (VSUID|VSGID))) {
620 		char path[MAXNAMELEN];
621 		refstr_t *mntpt = NULL;
622 		int ret = -1;
623 
624 		bzero(path, sizeof (path));
625 		zone_hold(pp->p_zone);
626 
627 		ret = vnodetopath(pp->p_zone->zone_rootvp, vp, path,
628 		    sizeof (path), cred);
629 
630 		/* fallback to mountpoint if a path can't be found */
631 		if ((ret != 0) || (ret == 0 && path[0] == '\0'))
632 			mntpt = vfs_getmntpoint(vp->v_vfsp);
633 
634 		if (mntpt == NULL)
635 			zcmn_err(pp->p_zone->zone_id, CE_NOTE,
636 			    "!uid %d: setuid execution not allowed, "
637 			    "file=%s", cred->cr_uid, path);
638 		else
639 			zcmn_err(pp->p_zone->zone_id, CE_NOTE,
640 			    "!uid %d: setuid execution not allowed, "
641 			    "fs=%s, file=%s", cred->cr_uid,
642 			    ZONE_PATH_TRANSLATE(refstr_value(mntpt),
643 			    pp->p_zone), exec_file);
644 
645 		if (!INGLOBALZONE(pp)) {
646 			/* zone_rootpath always has trailing / */
647 			if (mntpt == NULL)
648 				cmn_err(CE_NOTE, "!zone: %s, uid: %d "
649 				    "setuid execution not allowed, file=%s%s",
650 				    pp->p_zone->zone_name, cred->cr_uid,
651 				    pp->p_zone->zone_rootpath, path + 1);
652 			else
653 				cmn_err(CE_NOTE, "!zone: %s, uid: %d "
654 				    "setuid execution not allowed, fs=%s, "
655 				    "file=%s", pp->p_zone->zone_name,
656 				    cred->cr_uid, refstr_value(mntpt),
657 				    exec_file);
658 		}
659 
660 		if (mntpt != NULL)
661 			refstr_rele(mntpt);
662 
663 		zone_rele(pp->p_zone);
664 	}
665 
666 	/*
667 	 * execsetid() told us whether or not we had to change the
668 	 * credentials of the process.  In privflags, it told us
669 	 * whether we gained any privileges or executed a set-uid executable.
670 	 */
671 	setid = (privflags & (PRIV_SETUGID|PRIV_INCREASE));
672 
673 	/*
674 	 * Use /etc/system variable to determine if the stack
675 	 * should be marked as executable by default.
676 	 */
677 	if (noexec_user_stack)
678 		args->stk_prot &= ~PROT_EXEC;
679 
680 	args->execswp = eswp; /* Save execsw pointer in uarg for exec_func */
681 	args->ex_vp = vp;
682 
683 	/*
684 	 * Traditionally, the setid flags told the sub processes whether
685 	 * the file just executed was set-uid or set-gid; this caused
686 	 * some confusion as the 'setid' flag did not match the SUGID
687 	 * process flag which is only set when the uids/gids do not match.
688 	 * A script set-gid/set-uid to the real uid/gid would start with
689 	 * /dev/fd/X but an executable would happily trust LD_LIBRARY_PATH.
690 	 * Now we flag those cases where the calling process cannot
691 	 * be trusted to influence the newly exec'ed process, either
692 	 * because it runs with more privileges or when the uids/gids
693 	 * do in fact not match.
694 	 * This also makes the runtime linker agree with the on exec
695 	 * values of SNOCD and SUGID.
696 	 */
697 	setidfl = 0;
698 	if (cred->cr_uid != cred->cr_ruid || (cred->cr_rgid != cred->cr_gid &&
699 	    !supgroupmember(cred->cr_gid, cred))) {
700 		setidfl |= EXECSETID_UGIDS;
701 	}
702 	if (setid & PRIV_SETUGID)
703 		setidfl |= EXECSETID_SETID;
704 	if (setid & PRIV_INCREASE)
705 		setidfl |= EXECSETID_PRIVS;
706 
707 	execvp = pp->p_exec;
708 	if (execvp)
709 		VN_HOLD(execvp);
710 
711 	error = (*eswp->exec_func)(vp, uap, args, idatap, level, execsz,
712 	    setidfl, exec_file, cred, brand_action);
713 	rw_exit(eswp->exec_lock);
714 	if (error != 0) {
715 		if (newcred != NULL)
716 			crfree(newcred);
717 		if (execvp)
718 			VN_RELE(execvp);
719 		goto bad;
720 	}
721 
722 	if (level == 0) {
723 		if (execvp != NULL) {
724 			/*
725 			 * Close the previous executable only if we are
726 			 * at level 0.
727 			 */
728 			(void) VOP_CLOSE(execvp, FREAD, 1, (offset_t)0,
729 			    cred, NULL);
730 		}
731 
732 		mutex_enter(&pp->p_crlock);
733 		if (newcred != NULL) {
734 			/*
735 			 * Free the old credentials, and set the new ones.
736 			 * Do this for both the process and the (single) thread.
737 			 */
738 			crfree(pp->p_cred);
739 			pp->p_cred = cred;	/* cred already held for proc */
740 			crhold(cred);		/* hold new cred for thread */
741 			/*
742 			 * DTrace accesses t_cred in probe context.  t_cred
743 			 * must always be either NULL, or point to a valid,
744 			 * allocated cred structure.
745 			 */
746 			oldcred = curthread->t_cred;
747 			curthread->t_cred = cred;
748 			crfree(oldcred);
749 
750 			if (priv_basic_test >= 0 &&
751 			    !PRIV_ISASSERT(&CR_IPRIV(newcred),
752 			    priv_basic_test)) {
753 				pid_t pid = pp->p_pid;
754 				char *fn = PTOU(pp)->u_comm;
755 
756 				cmn_err(CE_WARN, "%s[%d]: exec: basic_test "
757 				    "privilege removed from E/I", fn, pid);
758 			}
759 		}
760 		/*
761 		 * On emerging from a successful exec(), the saved
762 		 * uid and gid equal the effective uid and gid.
763 		 */
764 		cred->cr_suid = cred->cr_uid;
765 		cred->cr_sgid = cred->cr_gid;
766 
767 		/*
768 		 * If the real and effective ids do not match, this
769 		 * is a setuid process that should not dump core.
770 		 * The group comparison is tricky; we prevent the code
771 		 * from flagging SNOCD when executing with an effective gid
772 		 * which is a supplementary group.
773 		 */
774 		if (cred->cr_ruid != cred->cr_uid ||
775 		    (cred->cr_rgid != cred->cr_gid &&
776 		    !supgroupmember(cred->cr_gid, cred)) ||
777 		    (privflags & PRIV_INCREASE) != 0)
778 			suidflags = PSUIDFLAGS;
779 		else
780 			suidflags = 0;
781 
782 		mutex_exit(&pp->p_crlock);
783 		if (suidflags) {
784 			mutex_enter(&pp->p_lock);
785 			pp->p_flag |= suidflags;
786 			mutex_exit(&pp->p_lock);
787 		}
788 		if (setid && (pp->p_proc_flag & P_PR_PTRACE) == 0) {
789 			/*
790 			 * If process is traced via /proc, arrange to
791 			 * invalidate the associated /proc vnode.
792 			 */
793 			if (pp->p_plist || (pp->p_proc_flag & P_PR_TRACE))
794 				args->traceinval = 1;
795 		}
796 		if (pp->p_proc_flag & P_PR_PTRACE)
797 			psignal(pp, SIGTRAP);
798 		if (args->traceinval)
799 			prinvalidate(&pp->p_user);
800 	}
801 	if (execvp)
802 		VN_RELE(execvp);
803 	return (0);
804 
805 bad:
806 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
807 
808 bad_noclose:
809 	if (error == 0)
810 		error = ENOEXEC;
811 
812 	if (suidflags) {
813 		mutex_enter(&pp->p_lock);
814 		pp->p_flag |= suidflags;
815 		mutex_exit(&pp->p_lock);
816 	}
817 	return (error);
818 }
819 
820 extern char *execswnames[];
821 
822 struct execsw *
823 allocate_execsw(char *name, char *magic, size_t magic_size)
824 {
825 	int i, j;
826 	char *ename;
827 	char *magicp;
828 
829 	mutex_enter(&execsw_lock);
830 	for (i = 0; i < nexectype; i++) {
831 		if (execswnames[i] == NULL) {
832 			ename = kmem_alloc(strlen(name) + 1, KM_SLEEP);
833 			(void) strcpy(ename, name);
834 			execswnames[i] = ename;
835 			/*
836 			 * Set the magic number last so that we
837 			 * don't need to hold the execsw_lock in
838 			 * findexectype().
839 			 */
840 			magicp = kmem_alloc(magic_size, KM_SLEEP);
841 			for (j = 0; j < magic_size; j++)
842 				magicp[j] = magic[j];
843 			execsw[i].exec_magic = magicp;
844 			mutex_exit(&execsw_lock);
845 			return (&execsw[i]);
846 		}
847 	}
848 	mutex_exit(&execsw_lock);
849 	return (NULL);
850 }
851 
852 /*
853  * Find the exec switch table entry with the corresponding magic string.
854  */
855 struct execsw *
856 findexecsw(char *magic)
857 {
858 	struct execsw *eswp;
859 
860 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
861 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
862 		if (magic && eswp->exec_maglen != 0 &&
863 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0)
864 			return (eswp);
865 	}
866 	return (NULL);
867 }
868 
869 /*
870  * Find the execsw[] index for the given exec header string by looking for the
871  * magic string at a specified offset and length for each kind of executable
872  * file format until one matches.  If no execsw[] entry is found, try to
873  * autoload a module for this magic string.
874  */
875 struct execsw *
876 findexec_by_hdr(char *header)
877 {
878 	struct execsw *eswp;
879 
880 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
881 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
882 		if (header && eswp->exec_maglen != 0 &&
883 		    bcmp(&header[eswp->exec_magoff], eswp->exec_magic,
884 		    eswp->exec_maglen) == 0) {
885 			if (hold_execsw(eswp) != 0)
886 				return (NULL);
887 			return (eswp);
888 		}
889 	}
890 	return (NULL);	/* couldn't find the type */
891 }
892 
893 /*
894  * Find the execsw[] index for the given magic string.  If no execsw[] entry
895  * is found, try to autoload a module for this magic string.
896  */
897 struct execsw *
898 findexec_by_magic(char *magic)
899 {
900 	struct execsw *eswp;
901 
902 	for (eswp = execsw; eswp < &execsw[nexectype]; eswp++) {
903 		ASSERT(eswp->exec_maglen <= MAGIC_BYTES);
904 		if (magic && eswp->exec_maglen != 0 &&
905 		    bcmp(magic, eswp->exec_magic, eswp->exec_maglen) == 0) {
906 			if (hold_execsw(eswp) != 0)
907 				return (NULL);
908 			return (eswp);
909 		}
910 	}
911 	return (NULL);	/* couldn't find the type */
912 }
913 
914 static int
915 hold_execsw(struct execsw *eswp)
916 {
917 	char *name;
918 
919 	rw_enter(eswp->exec_lock, RW_READER);
920 	while (!LOADED_EXEC(eswp)) {
921 		rw_exit(eswp->exec_lock);
922 		name = execswnames[eswp-execsw];
923 		ASSERT(name);
924 		if (modload("exec", name) == -1)
925 			return (-1);
926 		rw_enter(eswp->exec_lock, RW_READER);
927 	}
928 	return (0);
929 }
930 
931 static int
932 execsetid(struct vnode *vp, struct vattr *vattrp, uid_t *uidp, uid_t *gidp)
933 {
934 	proc_t *pp = ttoproc(curthread);
935 	uid_t uid, gid;
936 	cred_t *cr = pp->p_cred;
937 	int privflags = 0;
938 
939 	/*
940 	 * Remember credentials.
941 	 */
942 	uid = cr->cr_uid;
943 	gid = cr->cr_gid;
944 
945 	/* Will try to reset the PRIV_AWARE bit later. */
946 	if ((CR_FLAGS(cr) & (PRIV_AWARE|PRIV_AWARE_INHERIT)) == PRIV_AWARE)
947 		privflags |= PRIV_RESET;
948 
949 	if ((vp->v_vfsp->vfs_flag & VFS_NOSETUID) == 0) {
950 		/*
951 		 * Set-uid root execution only allowed if the limit set
952 		 * holds all unsafe privileges.
953 		 */
954 		if ((vattrp->va_mode & VSUID) && (vattrp->va_uid != 0 ||
955 		    priv_issubset(&priv_unsafe, &CR_LPRIV(cr)))) {
956 			uid = vattrp->va_uid;
957 			privflags |= PRIV_SETUGID;
958 		}
959 		if (vattrp->va_mode & VSGID) {
960 			gid = vattrp->va_gid;
961 			privflags |= PRIV_SETUGID;
962 		}
963 	}
964 
965 	/*
966 	 * Do we need to change our credential anyway?
967 	 * This is the case when E != I or P != I, as
968 	 * we need to do the assignments (with F empty and A full)
969 	 * Or when I is not a subset of L; in that case we need to
970 	 * enforce L.
971 	 *
972 	 *		I' = L & I
973 	 *
974 	 *		E' = P' = (I' + F) & A
975 	 * or
976 	 *		E' = P' = I'
977 	 */
978 	if (!priv_isequalset(&CR_EPRIV(cr), &CR_IPRIV(cr)) ||
979 	    !priv_issubset(&CR_IPRIV(cr), &CR_LPRIV(cr)) ||
980 	    !priv_isequalset(&CR_PPRIV(cr), &CR_IPRIV(cr)))
981 		privflags |= PRIV_RESET;
982 
983 	/* If MAC-aware flag(s) are on, need to update cred to remove. */
984 	if ((CR_FLAGS(cr) & NET_MAC_AWARE) ||
985 	    (CR_FLAGS(cr) & NET_MAC_AWARE_INHERIT))
986 		privflags |= MAC_FLAGS;
987 
988 	/*
989 	 * When we introduce the "forced" set then we will need
990 	 * to set PRIV_INCREASE here if I not a subset of P.
991 	 * If the "allowed" set is introduced we will need to do
992 	 * a similar thing; however, it seems more reasonable to
993 	 * have the allowed set reduce "L": script language interpreters
994 	 * would typically have an allowed set of "all".
995 	 */
996 
997 	/*
998 	 * Set setuid/setgid protections if no ptrace() compatibility.
999 	 * For privileged processes, honor setuid/setgid even in
1000 	 * the presence of ptrace() compatibility.
1001 	 */
1002 	if (((pp->p_proc_flag & P_PR_PTRACE) == 0 ||
1003 	    PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, (uid == 0))) &&
1004 	    (cr->cr_uid != uid ||
1005 	    cr->cr_gid != gid ||
1006 	    cr->cr_suid != uid ||
1007 	    cr->cr_sgid != gid)) {
1008 		*uidp = uid;
1009 		*gidp = gid;
1010 		privflags |= PRIV_SETID;
1011 	}
1012 	return (privflags);
1013 }
1014 
1015 int
1016 execpermissions(struct vnode *vp, struct vattr *vattrp, struct uarg *args)
1017 {
1018 	int error;
1019 	proc_t *p = ttoproc(curthread);
1020 
1021 	vattrp->va_mask = AT_MODE | AT_UID | AT_GID | AT_SIZE;
1022 	if (error = VOP_GETATTR(vp, vattrp, ATTR_EXEC, p->p_cred, NULL))
1023 		return (error);
1024 	/*
1025 	 * Check the access mode.
1026 	 * If VPROC, ask /proc if the file is an object file.
1027 	 */
1028 	if ((error = VOP_ACCESS(vp, VEXEC, 0, p->p_cred, NULL)) != 0 ||
1029 	    !(vp->v_type == VREG || (vp->v_type == VPROC && pr_isobject(vp))) ||
1030 	    (vp->v_vfsp->vfs_flag & VFS_NOEXEC) != 0 ||
1031 	    (vattrp->va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
1032 		if (error == 0)
1033 			error = EACCES;
1034 		return (error);
1035 	}
1036 
1037 	if ((p->p_plist || (p->p_proc_flag & (P_PR_PTRACE|P_PR_TRACE))) &&
1038 	    (error = VOP_ACCESS(vp, VREAD, 0, p->p_cred, NULL))) {
1039 		/*
1040 		 * If process is under ptrace(2) compatibility,
1041 		 * fail the exec(2).
1042 		 */
1043 		if (p->p_proc_flag & P_PR_PTRACE)
1044 			goto bad;
1045 		/*
1046 		 * Process is traced via /proc.
1047 		 * Arrange to invalidate the /proc vnode.
1048 		 */
1049 		args->traceinval = 1;
1050 	}
1051 	return (0);
1052 bad:
1053 	if (error == 0)
1054 		error = ENOEXEC;
1055 	return (error);
1056 }
1057 
1058 /*
1059  * Map a section of an executable file into the user's
1060  * address space.
1061  */
1062 int
1063 execmap(struct vnode *vp, caddr_t addr, size_t len, size_t zfodlen,
1064     off_t offset, int prot, int page, uint_t szc)
1065 {
1066 	int error = 0;
1067 	off_t oldoffset;
1068 	caddr_t zfodbase, oldaddr;
1069 	size_t end, oldlen;
1070 	size_t zfoddiff;
1071 	label_t ljb;
1072 	proc_t *p = ttoproc(curthread);
1073 
1074 	oldaddr = addr;
1075 	addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
1076 	if (len) {
1077 		oldlen = len;
1078 		len += ((size_t)oldaddr - (size_t)addr);
1079 		oldoffset = offset;
1080 		offset = (off_t)((uintptr_t)offset & PAGEMASK);
1081 		if (page) {
1082 			spgcnt_t  prefltmem, availm, npages;
1083 			int preread;
1084 			uint_t mflag = MAP_PRIVATE | MAP_FIXED;
1085 
1086 			if ((prot & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC) {
1087 				mflag |= MAP_TEXT;
1088 			} else {
1089 				mflag |= MAP_INITDATA;
1090 			}
1091 
1092 			if (valid_usr_range(addr, len, prot, p->p_as,
1093 			    p->p_as->a_userlimit) != RANGE_OKAY) {
1094 				error = ENOMEM;
1095 				goto bad;
1096 			}
1097 			if (error = VOP_MAP(vp, (offset_t)offset,
1098 			    p->p_as, &addr, len, prot, PROT_ALL,
1099 			    mflag, CRED(), NULL))
1100 				goto bad;
1101 
1102 			/*
1103 			 * If the segment can fit, then we prefault
1104 			 * the entire segment in.  This is based on the
1105 			 * model that says the best working set of a
1106 			 * small program is all of its pages.
1107 			 */
1108 			npages = (spgcnt_t)btopr(len);
1109 			prefltmem = freemem - desfree;
1110 			preread =
1111 			    (npages < prefltmem && len < PGTHRESH) ? 1 : 0;
1112 
1113 			/*
1114 			 * If we aren't prefaulting the segment,
1115 			 * increment "deficit", if necessary to ensure
1116 			 * that pages will become available when this
1117 			 * process starts executing.
1118 			 */
1119 			availm = freemem - lotsfree;
1120 			if (preread == 0 && npages > availm &&
1121 			    deficit < lotsfree) {
1122 				deficit += MIN((pgcnt_t)(npages - availm),
1123 				    lotsfree - deficit);
1124 			}
1125 
1126 			if (preread) {
1127 				TRACE_2(TR_FAC_PROC, TR_EXECMAP_PREREAD,
1128 				    "execmap preread:freemem %d size %lu",
1129 				    freemem, len);
1130 				(void) as_fault(p->p_as->a_hat, p->p_as,
1131 				    (caddr_t)addr, len, F_INVAL, S_READ);
1132 			}
1133 		} else {
1134 			if (valid_usr_range(addr, len, prot, p->p_as,
1135 			    p->p_as->a_userlimit) != RANGE_OKAY) {
1136 				error = ENOMEM;
1137 				goto bad;
1138 			}
1139 
1140 			if (error = as_map(p->p_as, addr, len,
1141 			    segvn_create, zfod_argsp))
1142 				goto bad;
1143 			/*
1144 			 * Read in the segment in one big chunk.
1145 			 */
1146 			if (error = vn_rdwr(UIO_READ, vp, (caddr_t)oldaddr,
1147 			    oldlen, (offset_t)oldoffset, UIO_USERSPACE, 0,
1148 			    (rlim64_t)0, CRED(), (ssize_t *)0))
1149 				goto bad;
1150 			/*
1151 			 * Now set protections.
1152 			 */
1153 			if (prot != PROT_ZFOD) {
1154 				(void) as_setprot(p->p_as, (caddr_t)addr,
1155 				    len, prot);
1156 			}
1157 		}
1158 	}
1159 
1160 	if (zfodlen) {
1161 		struct as *as = curproc->p_as;
1162 		struct seg *seg;
1163 		uint_t zprot = 0;
1164 
1165 		end = (size_t)addr + len;
1166 		zfodbase = (caddr_t)roundup(end, PAGESIZE);
1167 		zfoddiff = (uintptr_t)zfodbase - end;
1168 		if (zfoddiff) {
1169 			/*
1170 			 * Before we go to zero the remaining space on the last
1171 			 * page, make sure we have write permission.
1172 			 */
1173 
1174 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
1175 			seg = as_segat(curproc->p_as, (caddr_t)end);
1176 			if (seg != NULL)
1177 				SEGOP_GETPROT(seg, (caddr_t)end, zfoddiff - 1,
1178 				    &zprot);
1179 			AS_LOCK_EXIT(as, &as->a_lock);
1180 
1181 			if (seg != NULL && (zprot & PROT_WRITE) == 0) {
1182 				(void) as_setprot(as, (caddr_t)end,
1183 				    zfoddiff - 1, zprot | PROT_WRITE);
1184 			}
1185 
1186 			if (on_fault(&ljb)) {
1187 				no_fault();
1188 				if (seg != NULL && (zprot & PROT_WRITE) == 0)
1189 					(void) as_setprot(as, (caddr_t)end,
1190 					    zfoddiff - 1, zprot);
1191 				error = EFAULT;
1192 				goto bad;
1193 			}
1194 			uzero((void *)end, zfoddiff);
1195 			no_fault();
1196 			if (seg != NULL && (zprot & PROT_WRITE) == 0)
1197 				(void) as_setprot(as, (caddr_t)end,
1198 				    zfoddiff - 1, zprot);
1199 		}
1200 		if (zfodlen > zfoddiff) {
1201 			struct segvn_crargs crargs =
1202 			    SEGVN_ZFOD_ARGS(PROT_ZFOD, PROT_ALL);
1203 
1204 			zfodlen -= zfoddiff;
1205 			if (valid_usr_range(zfodbase, zfodlen, prot, p->p_as,
1206 			    p->p_as->a_userlimit) != RANGE_OKAY) {
1207 				error = ENOMEM;
1208 				goto bad;
1209 			}
1210 			if (szc > 0) {
1211 				/*
1212 				 * ASSERT alignment because the mapelfexec()
1213 				 * caller for the szc > 0 case extended zfod
1214 				 * so it's end is pgsz aligned.
1215 				 */
1216 				size_t pgsz = page_get_pagesize(szc);
1217 				ASSERT(IS_P2ALIGNED(zfodbase + zfodlen, pgsz));
1218 
1219 				if (IS_P2ALIGNED(zfodbase, pgsz)) {
1220 					crargs.szc = szc;
1221 				} else {
1222 					crargs.szc = AS_MAP_HEAP;
1223 				}
1224 			} else {
1225 				crargs.szc = AS_MAP_NO_LPOOB;
1226 			}
1227 			if (error = as_map(p->p_as, (caddr_t)zfodbase,
1228 			    zfodlen, segvn_create, &crargs))
1229 				goto bad;
1230 			if (prot != PROT_ZFOD) {
1231 				(void) as_setprot(p->p_as, (caddr_t)zfodbase,
1232 				    zfodlen, prot);
1233 			}
1234 		}
1235 	}
1236 	return (0);
1237 bad:
1238 	return (error);
1239 }
1240 
1241 void
1242 setexecenv(struct execenv *ep)
1243 {
1244 	proc_t *p = ttoproc(curthread);
1245 	klwp_t *lwp = ttolwp(curthread);
1246 	struct vnode *vp;
1247 
1248 	p->p_bssbase = ep->ex_bssbase;
1249 	p->p_brkbase = ep->ex_brkbase;
1250 	p->p_brksize = ep->ex_brksize;
1251 	if (p->p_exec)
1252 		VN_RELE(p->p_exec);	/* out with the old */
1253 	vp = p->p_exec = ep->ex_vp;
1254 	if (vp != NULL)
1255 		VN_HOLD(vp);		/* in with the new */
1256 
1257 	lwp->lwp_sigaltstack.ss_sp = 0;
1258 	lwp->lwp_sigaltstack.ss_size = 0;
1259 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
1260 }
1261 
1262 int
1263 execopen(struct vnode **vpp, int *fdp)
1264 {
1265 	struct vnode *vp = *vpp;
1266 	file_t *fp;
1267 	int error = 0;
1268 	int filemode = FREAD;
1269 
1270 	VN_HOLD(vp);		/* open reference */
1271 	if (error = falloc(NULL, filemode, &fp, fdp)) {
1272 		VN_RELE(vp);
1273 		*fdp = -1;	/* just in case falloc changed value */
1274 		return (error);
1275 	}
1276 	if (error = VOP_OPEN(&vp, filemode, CRED(), NULL)) {
1277 		VN_RELE(vp);
1278 		setf(*fdp, NULL);
1279 		unfalloc(fp);
1280 		*fdp = -1;
1281 		return (error);
1282 	}
1283 	*vpp = vp;		/* vnode should not have changed */
1284 	fp->f_vnode = vp;
1285 	mutex_exit(&fp->f_tlock);
1286 	setf(*fdp, fp);
1287 	return (0);
1288 }
1289 
1290 int
1291 execclose(int fd)
1292 {
1293 	return (closeandsetf(fd, NULL));
1294 }
1295 
1296 
1297 /*
1298  * noexec stub function.
1299  */
1300 /*ARGSUSED*/
1301 int
1302 noexec(
1303     struct vnode *vp,
1304     struct execa *uap,
1305     struct uarg *args,
1306     struct intpdata *idatap,
1307     int level,
1308     long *execsz,
1309     int setid,
1310     caddr_t exec_file,
1311     struct cred *cred)
1312 {
1313 	cmn_err(CE_WARN, "missing exec capability for %s", uap->fname);
1314 	return (ENOEXEC);
1315 }
1316 
1317 /*
1318  * Support routines for building a user stack.
1319  *
1320  * execve(path, argv, envp) must construct a new stack with the specified
1321  * arguments and environment variables (see exec_args() for a description
1322  * of the user stack layout).  To do this, we copy the arguments and
1323  * environment variables from the old user address space into the kernel,
1324  * free the old as, create the new as, and copy our buffered information
1325  * to the new stack.  Our kernel buffer has the following structure:
1326  *
1327  *	+-----------------------+ <--- stk_base + stk_size
1328  *	| string offsets	|
1329  *	+-----------------------+ <--- stk_offp
1330  *	|			|
1331  *	| STK_AVAIL() space	|
1332  *	|			|
1333  *	+-----------------------+ <--- stk_strp
1334  *	| strings		|
1335  *	+-----------------------+ <--- stk_base
1336  *
1337  * When we add a string, we store the string's contents (including the null
1338  * terminator) at stk_strp, and we store the offset of the string relative to
1339  * stk_base at --stk_offp.  At strings are added, stk_strp increases and
1340  * stk_offp decreases.  The amount of space remaining, STK_AVAIL(), is just
1341  * the difference between these pointers.  If we run out of space, we return
1342  * an error and exec_args() starts all over again with a buffer twice as large.
1343  * When we're all done, the kernel buffer looks like this:
1344  *
1345  *	+-----------------------+ <--- stk_base + stk_size
1346  *	| argv[0] offset	|
1347  *	+-----------------------+
1348  *	| ...			|
1349  *	+-----------------------+
1350  *	| argv[argc-1] offset	|
1351  *	+-----------------------+
1352  *	| envp[0] offset	|
1353  *	+-----------------------+
1354  *	| ...			|
1355  *	+-----------------------+
1356  *	| envp[envc-1] offset	|
1357  *	+-----------------------+
1358  *	| AT_SUN_PLATFORM offset|
1359  *	+-----------------------+
1360  *	| AT_SUN_EXECNAME offset|
1361  *	+-----------------------+ <--- stk_offp
1362  *	|			|
1363  *	| STK_AVAIL() space	|
1364  *	|			|
1365  *	+-----------------------+ <--- stk_strp
1366  *	| AT_SUN_EXECNAME offset|
1367  *	+-----------------------+
1368  *	| AT_SUN_PLATFORM offset|
1369  *	+-----------------------+
1370  *	| envp[envc-1] string	|
1371  *	+-----------------------+
1372  *	| ...			|
1373  *	+-----------------------+
1374  *	| envp[0] string	|
1375  *	+-----------------------+
1376  *	| argv[argc-1] string	|
1377  *	+-----------------------+
1378  *	| ...			|
1379  *	+-----------------------+
1380  *	| argv[0] string	|
1381  *	+-----------------------+ <--- stk_base
1382  */
1383 
1384 #define	STK_AVAIL(args)		((char *)(args)->stk_offp - (args)->stk_strp)
1385 
1386 /*
1387  * Add a string to the stack.
1388  */
1389 static int
1390 stk_add(uarg_t *args, const char *sp, enum uio_seg segflg)
1391 {
1392 	int error;
1393 	size_t len;
1394 
1395 	if (STK_AVAIL(args) < sizeof (int))
1396 		return (E2BIG);
1397 	*--args->stk_offp = args->stk_strp - args->stk_base;
1398 
1399 	if (segflg == UIO_USERSPACE) {
1400 		error = copyinstr(sp, args->stk_strp, STK_AVAIL(args), &len);
1401 		if (error != 0)
1402 			return (error);
1403 	} else {
1404 		len = strlen(sp) + 1;
1405 		if (len > STK_AVAIL(args))
1406 			return (E2BIG);
1407 		bcopy(sp, args->stk_strp, len);
1408 	}
1409 
1410 	args->stk_strp += len;
1411 
1412 	return (0);
1413 }
1414 
1415 static int
1416 stk_getptr(uarg_t *args, char *src, char **dst)
1417 {
1418 	int error;
1419 
1420 	if (args->from_model == DATAMODEL_NATIVE) {
1421 		ulong_t ptr;
1422 		error = fulword(src, &ptr);
1423 		*dst = (caddr_t)ptr;
1424 	} else {
1425 		uint32_t ptr;
1426 		error = fuword32(src, &ptr);
1427 		*dst = (caddr_t)(uintptr_t)ptr;
1428 	}
1429 	return (error);
1430 }
1431 
1432 static int
1433 stk_putptr(uarg_t *args, char *addr, char *value)
1434 {
1435 	if (args->to_model == DATAMODEL_NATIVE)
1436 		return (sulword(addr, (ulong_t)value));
1437 	else
1438 		return (suword32(addr, (uint32_t)(uintptr_t)value));
1439 }
1440 
1441 static int
1442 stk_copyin(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
1443 {
1444 	char *sp;
1445 	int argc, error;
1446 	int argv_empty = 0;
1447 	size_t ptrsize = args->from_ptrsize;
1448 	size_t size, pad;
1449 	char *argv = (char *)uap->argp;
1450 	char *envp = (char *)uap->envp;
1451 
1452 	/*
1453 	 * Copy interpreter's name and argument to argv[0] and argv[1].
1454 	 */
1455 	if (intp != NULL && intp->intp_name != NULL) {
1456 		if ((error = stk_add(args, intp->intp_name, UIO_SYSSPACE)) != 0)
1457 			return (error);
1458 		if (intp->intp_arg != NULL &&
1459 		    (error = stk_add(args, intp->intp_arg, UIO_SYSSPACE)) != 0)
1460 			return (error);
1461 		if (args->fname != NULL)
1462 			error = stk_add(args, args->fname, UIO_SYSSPACE);
1463 		else
1464 			error = stk_add(args, uap->fname, UIO_USERSPACE);
1465 		if (error)
1466 			return (error);
1467 
1468 		/*
1469 		 * Check for an empty argv[].
1470 		 */
1471 		if (stk_getptr(args, argv, &sp))
1472 			return (EFAULT);
1473 		if (sp == NULL)
1474 			argv_empty = 1;
1475 
1476 		argv += ptrsize;		/* ignore original argv[0] */
1477 	}
1478 
1479 	if (argv_empty == 0) {
1480 		/*
1481 		 * Add argv[] strings to the stack.
1482 		 */
1483 		for (;;) {
1484 			if (stk_getptr(args, argv, &sp))
1485 				return (EFAULT);
1486 			if (sp == NULL)
1487 				break;
1488 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
1489 				return (error);
1490 			argv += ptrsize;
1491 		}
1492 	}
1493 	argc = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
1494 	args->arglen = args->stk_strp - args->stk_base;
1495 
1496 	/*
1497 	 * Add environ[] strings to the stack.
1498 	 */
1499 	if (envp != NULL) {
1500 		for (;;) {
1501 			if (stk_getptr(args, envp, &sp))
1502 				return (EFAULT);
1503 			if (sp == NULL)
1504 				break;
1505 			if ((error = stk_add(args, sp, UIO_USERSPACE)) != 0)
1506 				return (error);
1507 			envp += ptrsize;
1508 		}
1509 	}
1510 	args->na = (int *)(args->stk_base + args->stk_size) - args->stk_offp;
1511 	args->ne = args->na - argc;
1512 
1513 	/*
1514 	 * Add AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME, and
1515 	 * AT_SUN_EMULATOR strings to the stack.
1516 	 */
1517 	if (auxvpp != NULL && *auxvpp != NULL) {
1518 		if ((error = stk_add(args, platform, UIO_SYSSPACE)) != 0)
1519 			return (error);
1520 		if ((error = stk_add(args, args->pathname, UIO_SYSSPACE)) != 0)
1521 			return (error);
1522 		if (args->brandname != NULL &&
1523 		    (error = stk_add(args, args->brandname, UIO_SYSSPACE)) != 0)
1524 			return (error);
1525 		if (args->emulator != NULL &&
1526 		    (error = stk_add(args, args->emulator, UIO_SYSSPACE)) != 0)
1527 			return (error);
1528 	}
1529 
1530 	/*
1531 	 * Compute the size of the stack.  This includes all the pointers,
1532 	 * the space reserved for the aux vector, and all the strings.
1533 	 * The total number of pointers is args->na (which is argc + envc)
1534 	 * plus 4 more: (1) a pointer's worth of space for argc; (2) the NULL
1535 	 * after the last argument (i.e. argv[argc]); (3) the NULL after the
1536 	 * last environment variable (i.e. envp[envc]); and (4) the NULL after
1537 	 * all the strings, at the very top of the stack.
1538 	 */
1539 	size = (args->na + 4) * args->to_ptrsize + args->auxsize +
1540 	    (args->stk_strp - args->stk_base);
1541 
1542 	/*
1543 	 * Pad the string section with zeroes to align the stack size.
1544 	 */
1545 	pad = P2NPHASE(size, args->stk_align);
1546 
1547 	if (STK_AVAIL(args) < pad)
1548 		return (E2BIG);
1549 
1550 	args->usrstack_size = size + pad;
1551 
1552 	while (pad-- != 0)
1553 		*args->stk_strp++ = 0;
1554 
1555 	args->nc = args->stk_strp - args->stk_base;
1556 
1557 	return (0);
1558 }
1559 
1560 static int
1561 stk_copyout(uarg_t *args, char *usrstack, void **auxvpp, user_t *up)
1562 {
1563 	size_t ptrsize = args->to_ptrsize;
1564 	ssize_t pslen;
1565 	char *kstrp = args->stk_base;
1566 	char *ustrp = usrstack - args->nc - ptrsize;
1567 	char *usp = usrstack - args->usrstack_size;
1568 	int *offp = (int *)(args->stk_base + args->stk_size);
1569 	int envc = args->ne;
1570 	int argc = args->na - envc;
1571 	int i;
1572 
1573 	/*
1574 	 * Record argc for /proc.
1575 	 */
1576 	up->u_argc = argc;
1577 
1578 	/*
1579 	 * Put argc on the stack.  Note that even though it's an int,
1580 	 * it always consumes ptrsize bytes (for alignment).
1581 	 */
1582 	if (stk_putptr(args, usp, (char *)(uintptr_t)argc))
1583 		return (-1);
1584 
1585 	/*
1586 	 * Add argc space (ptrsize) to usp and record argv for /proc.
1587 	 */
1588 	up->u_argv = (uintptr_t)(usp += ptrsize);
1589 
1590 	/*
1591 	 * Put the argv[] pointers on the stack.
1592 	 */
1593 	for (i = 0; i < argc; i++, usp += ptrsize)
1594 		if (stk_putptr(args, usp, &ustrp[*--offp]))
1595 			return (-1);
1596 
1597 	/*
1598 	 * Copy arguments to u_psargs.
1599 	 */
1600 	pslen = MIN(args->arglen, PSARGSZ) - 1;
1601 	for (i = 0; i < pslen; i++)
1602 		up->u_psargs[i] = (kstrp[i] == '\0' ? ' ' : kstrp[i]);
1603 	while (i < PSARGSZ)
1604 		up->u_psargs[i++] = '\0';
1605 
1606 	/*
1607 	 * Add space for argv[]'s NULL terminator (ptrsize) to usp and
1608 	 * record envp for /proc.
1609 	 */
1610 	up->u_envp = (uintptr_t)(usp += ptrsize);
1611 
1612 	/*
1613 	 * Put the envp[] pointers on the stack.
1614 	 */
1615 	for (i = 0; i < envc; i++, usp += ptrsize)
1616 		if (stk_putptr(args, usp, &ustrp[*--offp]))
1617 			return (-1);
1618 
1619 	/*
1620 	 * Add space for envp[]'s NULL terminator (ptrsize) to usp and
1621 	 * remember where the stack ends, which is also where auxv begins.
1622 	 */
1623 	args->stackend = usp += ptrsize;
1624 
1625 	/*
1626 	 * Put all the argv[], envp[], and auxv strings on the stack.
1627 	 */
1628 	if (copyout(args->stk_base, ustrp, args->nc))
1629 		return (-1);
1630 
1631 	/*
1632 	 * Fill in the aux vector now that we know the user stack addresses
1633 	 * for the AT_SUN_PLATFORM, AT_SUN_EXECNAME, AT_SUN_BRANDNAME and
1634 	 * AT_SUN_EMULATOR strings.
1635 	 */
1636 	if (auxvpp != NULL && *auxvpp != NULL) {
1637 		if (args->to_model == DATAMODEL_NATIVE) {
1638 			auxv_t **a = (auxv_t **)auxvpp;
1639 			ADDAUX(*a, AT_SUN_PLATFORM, (long)&ustrp[*--offp])
1640 			ADDAUX(*a, AT_SUN_EXECNAME, (long)&ustrp[*--offp])
1641 			if (args->brandname != NULL)
1642 				ADDAUX(*a,
1643 				    AT_SUN_BRANDNAME, (long)&ustrp[*--offp])
1644 			if (args->emulator != NULL)
1645 				ADDAUX(*a,
1646 				    AT_SUN_EMULATOR, (long)&ustrp[*--offp])
1647 		} else {
1648 			auxv32_t **a = (auxv32_t **)auxvpp;
1649 			ADDAUX(*a,
1650 			    AT_SUN_PLATFORM, (int)(uintptr_t)&ustrp[*--offp])
1651 			ADDAUX(*a,
1652 			    AT_SUN_EXECNAME, (int)(uintptr_t)&ustrp[*--offp])
1653 			if (args->brandname != NULL)
1654 				ADDAUX(*a, AT_SUN_BRANDNAME,
1655 				    (int)(uintptr_t)&ustrp[*--offp])
1656 			if (args->emulator != NULL)
1657 				ADDAUX(*a, AT_SUN_EMULATOR,
1658 				    (int)(uintptr_t)&ustrp[*--offp])
1659 		}
1660 	}
1661 
1662 	return (0);
1663 }
1664 
1665 /*
1666  * Initialize a new user stack with the specified arguments and environment.
1667  * The initial user stack layout is as follows:
1668  *
1669  *	User Stack
1670  *	+---------------+ <--- curproc->p_usrstack
1671  *	|		|
1672  *	| slew		|
1673  *	|		|
1674  *	+---------------+
1675  *	| NULL		|
1676  *	+---------------+
1677  *	|		|
1678  *	| auxv strings	|
1679  *	|		|
1680  *	+---------------+
1681  *	|		|
1682  *	| envp strings	|
1683  *	|		|
1684  *	+---------------+
1685  *	|		|
1686  *	| argv strings	|
1687  *	|		|
1688  *	+---------------+ <--- ustrp
1689  *	|		|
1690  *	| aux vector	|
1691  *	|		|
1692  *	+---------------+ <--- auxv
1693  *	| NULL		|
1694  *	+---------------+
1695  *	| envp[envc-1]	|
1696  *	+---------------+
1697  *	| ...		|
1698  *	+---------------+
1699  *	| envp[0]	|
1700  *	+---------------+ <--- envp[]
1701  *	| NULL		|
1702  *	+---------------+
1703  *	| argv[argc-1]	|
1704  *	+---------------+
1705  *	| ...		|
1706  *	+---------------+
1707  *	| argv[0]	|
1708  *	+---------------+ <--- argv[]
1709  *	| argc		|
1710  *	+---------------+ <--- stack base
1711  */
1712 int
1713 exec_args(execa_t *uap, uarg_t *args, intpdata_t *intp, void **auxvpp)
1714 {
1715 	size_t size;
1716 	int error;
1717 	proc_t *p = ttoproc(curthread);
1718 	user_t *up = PTOU(p);
1719 	char *usrstack;
1720 	rctl_entity_p_t e;
1721 	struct as *as;
1722 	extern int use_stk_lpg;
1723 	size_t sp_slew;
1724 
1725 	args->from_model = p->p_model;
1726 	if (p->p_model == DATAMODEL_NATIVE) {
1727 		args->from_ptrsize = sizeof (long);
1728 	} else {
1729 		args->from_ptrsize = sizeof (int32_t);
1730 	}
1731 
1732 	if (args->to_model == DATAMODEL_NATIVE) {
1733 		args->to_ptrsize = sizeof (long);
1734 		args->ncargs = NCARGS;
1735 		args->stk_align = STACK_ALIGN;
1736 		if (args->addr32)
1737 			usrstack = (char *)USRSTACK64_32;
1738 		else
1739 			usrstack = (char *)USRSTACK;
1740 	} else {
1741 		args->to_ptrsize = sizeof (int32_t);
1742 		args->ncargs = NCARGS32;
1743 		args->stk_align = STACK_ALIGN32;
1744 		usrstack = (char *)USRSTACK32;
1745 	}
1746 
1747 	ASSERT(P2PHASE((uintptr_t)usrstack, args->stk_align) == 0);
1748 
1749 #if defined(__sparc)
1750 	/*
1751 	 * Make sure user register windows are empty before
1752 	 * attempting to make a new stack.
1753 	 */
1754 	(void) flush_user_windows_to_stack(NULL);
1755 #endif
1756 
1757 	for (size = PAGESIZE; ; size *= 2) {
1758 		args->stk_size = size;
1759 		args->stk_base = kmem_alloc(size, KM_SLEEP);
1760 		args->stk_strp = args->stk_base;
1761 		args->stk_offp = (int *)(args->stk_base + size);
1762 		error = stk_copyin(uap, args, intp, auxvpp);
1763 		if (error == 0)
1764 			break;
1765 		kmem_free(args->stk_base, size);
1766 		if (error != E2BIG && error != ENAMETOOLONG)
1767 			return (error);
1768 		if (size >= args->ncargs)
1769 			return (E2BIG);
1770 	}
1771 
1772 	size = args->usrstack_size;
1773 
1774 	ASSERT(error == 0);
1775 	ASSERT(P2PHASE(size, args->stk_align) == 0);
1776 	ASSERT((ssize_t)STK_AVAIL(args) >= 0);
1777 
1778 	if (size > args->ncargs) {
1779 		kmem_free(args->stk_base, args->stk_size);
1780 		return (E2BIG);
1781 	}
1782 
1783 	/*
1784 	 * Leave only the current lwp and force the other lwps to exit.
1785 	 * If another lwp beat us to the punch by calling exit(), bail out.
1786 	 */
1787 	if ((error = exitlwps(0)) != 0) {
1788 		kmem_free(args->stk_base, args->stk_size);
1789 		return (error);
1790 	}
1791 
1792 	/*
1793 	 * Revoke any doors created by the process.
1794 	 */
1795 	if (p->p_door_list)
1796 		door_exit();
1797 
1798 	/*
1799 	 * Release schedctl data structures.
1800 	 */
1801 	if (p->p_pagep)
1802 		schedctl_proc_cleanup();
1803 
1804 	/*
1805 	 * Clean up any DTrace helpers for the process.
1806 	 */
1807 	if (p->p_dtrace_helpers != NULL) {
1808 		ASSERT(dtrace_helpers_cleanup != NULL);
1809 		(*dtrace_helpers_cleanup)();
1810 	}
1811 
1812 	mutex_enter(&p->p_lock);
1813 	/*
1814 	 * Cleanup the DTrace provider associated with this process.
1815 	 */
1816 	if (p->p_dtrace_probes) {
1817 		ASSERT(dtrace_fasttrap_exec_ptr != NULL);
1818 		dtrace_fasttrap_exec_ptr(p);
1819 	}
1820 	mutex_exit(&p->p_lock);
1821 
1822 	/*
1823 	 * discard the lwpchan cache.
1824 	 */
1825 	if (p->p_lcp != NULL)
1826 		lwpchan_destroy_cache(1);
1827 
1828 	/*
1829 	 * Delete the POSIX timers.
1830 	 */
1831 	if (p->p_itimer != NULL)
1832 		timer_exit();
1833 
1834 	/*
1835 	 * Delete the ITIMER_REALPROF interval timer.
1836 	 * The other ITIMER_* interval timers are specified
1837 	 * to be inherited across exec().
1838 	 */
1839 	delete_itimer_realprof();
1840 
1841 	if (audit_active)
1842 		audit_exec(args->stk_base, args->stk_base + args->arglen,
1843 		    args->na - args->ne, args->ne);
1844 
1845 	/*
1846 	 * Ensure that we don't change resource associations while we
1847 	 * change address spaces.
1848 	 */
1849 	mutex_enter(&p->p_lock);
1850 	pool_barrier_enter();
1851 	mutex_exit(&p->p_lock);
1852 
1853 	/*
1854 	 * Destroy the old address space and create a new one.
1855 	 * From here on, any errors are fatal to the exec()ing process.
1856 	 * On error we return -1, which means the caller must SIGKILL
1857 	 * the process.
1858 	 */
1859 	relvm();
1860 
1861 	mutex_enter(&p->p_lock);
1862 	pool_barrier_exit();
1863 	mutex_exit(&p->p_lock);
1864 
1865 	up->u_execsw = args->execswp;
1866 
1867 	p->p_brkbase = NULL;
1868 	p->p_brksize = 0;
1869 	p->p_brkpageszc = 0;
1870 	p->p_stksize = 0;
1871 	p->p_stkpageszc = 0;
1872 	p->p_model = args->to_model;
1873 	p->p_usrstack = usrstack;
1874 	p->p_stkprot = args->stk_prot;
1875 	p->p_datprot = args->dat_prot;
1876 
1877 	/*
1878 	 * Reset resource controls such that all controls are again active as
1879 	 * well as appropriate to the potentially new address model for the
1880 	 * process.
1881 	 */
1882 	e.rcep_p.proc = p;
1883 	e.rcep_t = RCENTITY_PROCESS;
1884 	rctl_set_reset(p->p_rctls, p, &e);
1885 
1886 	/* Too early to call map_pgsz for the heap */
1887 	if (use_stk_lpg) {
1888 		p->p_stkpageszc = page_szc(map_pgsz(MAPPGSZ_STK, p, 0, 0, 0));
1889 	}
1890 
1891 	mutex_enter(&p->p_lock);
1892 	p->p_flag |= SAUTOLPG;	/* kernel controls page sizes */
1893 	mutex_exit(&p->p_lock);
1894 
1895 	/*
1896 	 * Some platforms may choose to randomize real stack start by adding a
1897 	 * small slew (not more than a few hundred bytes) to the top of the
1898 	 * stack. This helps avoid cache thrashing when identical processes
1899 	 * simultaneously share caches that don't provide enough associativity
1900 	 * (e.g. sun4v systems). In this case stack slewing makes the same hot
1901 	 * stack variables in different processes to live in different cache
1902 	 * sets increasing effective associativity.
1903 	 */
1904 	sp_slew = exec_get_spslew();
1905 	ASSERT(P2PHASE(sp_slew, args->stk_align) == 0);
1906 	exec_set_sp(size + sp_slew);
1907 
1908 	as = as_alloc();
1909 	p->p_as = as;
1910 	as->a_proc = p;
1911 	if (p->p_model == DATAMODEL_ILP32 || args->addr32)
1912 		as->a_userlimit = (caddr_t)USERLIMIT32;
1913 	(void) hat_setup(as->a_hat, HAT_ALLOC);
1914 	hat_join_srd(as->a_hat, args->ex_vp);
1915 
1916 	/*
1917 	 * Finally, write out the contents of the new stack.
1918 	 */
1919 	error = stk_copyout(args, usrstack - sp_slew, auxvpp, up);
1920 	kmem_free(args->stk_base, args->stk_size);
1921 	return (error);
1922 }
1923