xref: /openbsd-src/sys/kern/kern_exec.c (revision 5ad04d351680822078003e2b066cfc9680d6157d)
1 /*	$OpenBSD: kern_exec.c,v 1.142 2014/05/15 03:52:25 guenther Exp $	*/
2 /*	$NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $	*/
3 
4 /*-
5  * Copyright (C) 1993, 1994 Christopher G. Demetriou
6  * Copyright (C) 1992 Wolfgang Solfrank.
7  * Copyright (C) 1992 TooLs GmbH.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/pool.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/acct.h>
48 #include <sys/exec.h>
49 #include <sys/ktrace.h>
50 #include <sys/resourcevar.h>
51 #include <sys/wait.h>
52 #include <sys/mman.h>
53 #include <sys/signalvar.h>
54 #include <sys/stat.h>
55 #include <sys/conf.h>
56 #ifdef SYSVSHM
57 #include <sys/shm.h>
58 #endif
59 
60 #include <sys/syscallargs.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <machine/reg.h>
65 
66 #ifdef __HAVE_MD_TCB
67 # include <machine/tcb.h>
68 #endif
69 
70 #include <dev/rndvar.h>
71 
72 #include "systrace.h"
73 
74 #if NSYSTRACE > 0
75 #include <dev/systrace.h>
76 #endif
77 
78 /*
79  * Map the shared signal code.
80  */
81 int exec_sigcode_map(struct process *, struct emul *);
82 
83 /*
84  * If non-zero, stackgap_random specifies the upper limit of the random gap size
85  * added to the fixed stack gap. Must be n^2.
86  */
87 int stackgap_random = STACKGAP_RANDOM;
88 
89 /*
90  * check exec:
91  * given an "executable" described in the exec package's namei info,
92  * see what we can do with it.
93  *
94  * ON ENTRY:
95  *	exec package with appropriate namei info
96  *	proc pointer of exec'ing proc
97  *	NO SELF-LOCKED VNODES
98  *
99  * ON EXIT:
100  *	error:	nothing held, etc.  exec header still allocated.
101  *	ok:	filled exec package, one locked vnode.
102  *
103  * EXEC SWITCH ENTRY:
104  * 	Locked vnode to check, exec package, proc.
105  *
106  * EXEC SWITCH EXIT:
107  *	ok:	return 0, filled exec package, one locked vnode.
108  *	error:	destructive:
109  *			everything deallocated except exec header.
110  *		non-destructive:
111  *			error code, locked vnode, exec header unmodified
112  */
113 int
114 check_exec(struct proc *p, struct exec_package *epp)
115 {
116 	int error, i;
117 	struct vnode *vp;
118 	struct nameidata *ndp;
119 	size_t resid;
120 
121 	ndp = epp->ep_ndp;
122 	ndp->ni_cnd.cn_nameiop = LOOKUP;
123 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
124 	/* first get the vnode */
125 	if ((error = namei(ndp)) != 0)
126 		return (error);
127 	epp->ep_vp = vp = ndp->ni_vp;
128 
129 	/* check for regular file */
130 	if (vp->v_type == VDIR) {
131 		error = EISDIR;
132 		goto bad1;
133 	}
134 	if (vp->v_type != VREG) {
135 		error = EACCES;
136 		goto bad1;
137 	}
138 
139 	/* get attributes */
140 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
141 		goto bad1;
142 
143 	/* Check mount point */
144 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
145 		error = EACCES;
146 		goto bad1;
147 	}
148 
149 	if ((vp->v_mount->mnt_flag & MNT_NOSUID))
150 		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
151 
152 	/* check access.  for root we have to see if any exec bit on */
153 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
154 		goto bad1;
155 	if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
156 		error = EACCES;
157 		goto bad1;
158 	}
159 
160 	/* try to open it */
161 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
162 		goto bad1;
163 
164 	/* unlock vp, we need it unlocked from here */
165 	VOP_UNLOCK(vp, 0, p);
166 
167 	/* now we have the file, get the exec header */
168 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
169 	    UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
170 	if (error)
171 		goto bad2;
172 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
173 
174 	/*
175 	 * set up the vmcmds for creation of the process
176 	 * address space
177 	 */
178 	error = ENOEXEC;
179 	for (i = 0; i < nexecs && error != 0; i++) {
180 		int newerror;
181 
182 		if (execsw[i].es_check == NULL)
183 			continue;
184 		newerror = (*execsw[i].es_check)(p, epp);
185 		if (!newerror && !(epp->ep_emul->e_flags & EMUL_ENABLED))
186 			newerror = EPERM;
187 		/* make sure the first "interesting" error code is saved. */
188 		if (!newerror || error == ENOEXEC)
189 			error = newerror;
190 		if (epp->ep_flags & EXEC_DESTR && error != 0)
191 			return (error);
192 	}
193 	if (!error) {
194 		/* check that entry point is sane */
195 		if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
196 			error = ENOEXEC;
197 		}
198 
199 		/* check limits */
200 		if ((epp->ep_tsize > MAXTSIZ) ||
201 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
202 			error = ENOMEM;
203 
204 		if (!error)
205 			return (0);
206 	}
207 
208 	/*
209 	 * free any vmspace-creation commands,
210 	 * and release their references
211 	 */
212 	kill_vmcmds(&epp->ep_vmcmds);
213 
214 bad2:
215 	/*
216 	 * close the vnode, free the pathname buf, and punt.
217 	 */
218 	vn_close(vp, FREAD, p->p_ucred, p);
219 	pool_put(&namei_pool, ndp->ni_cnd.cn_pnbuf);
220 	return (error);
221 
222 bad1:
223 	/*
224 	 * free the namei pathname buffer, and put the vnode
225 	 * (which we don't yet have open).
226 	 */
227 	pool_put(&namei_pool, ndp->ni_cnd.cn_pnbuf);
228 	vput(vp);
229 	return (error);
230 }
231 
232 /*
233  * exec system call
234  */
235 /* ARGSUSED */
236 int
237 sys_execve(struct proc *p, void *v, register_t *retval)
238 {
239 	struct sys_execve_args /* {
240 		syscallarg(const char *) path;
241 		syscallarg(char *const *) argp;
242 		syscallarg(char *const *) envp;
243 	} */ *uap = v;
244 	int error;
245 	struct exec_package pack;
246 	struct nameidata nid;
247 	struct vattr attr;
248 	struct ucred *cred = p->p_ucred;
249 	char *argp;
250 	char * const *cpp, *dp, *sp;
251 	struct process *pr = p->p_p;
252 	long argc, envc;
253 	size_t len, sgap;
254 #ifdef MACHINE_STACK_GROWS_UP
255 	size_t slen;
256 #endif
257 	char *stack;
258 	struct ps_strings arginfo;
259 	struct vmspace *vm = pr->ps_vmspace;
260 	char **tmpfap;
261 	extern struct emul emul_native;
262 #if NSYSTRACE > 0
263 	int wassugid = ISSET(pr->ps_flags, PS_SUGID | PS_SUGIDEXEC);
264 	size_t pathbuflen;
265 #endif
266 	char *pathbuf = NULL;
267 	struct vnode *otvp;
268 
269 	/* get other threads to stop */
270 	if ((error = single_thread_set(p, SINGLE_UNWIND, 1)))
271 		return (error);
272 
273 	/*
274 	 * Cheap solution to complicated problems.
275 	 * Mark this process as "leave me alone, I'm execing".
276 	 */
277 	atomic_setbits_int(&pr->ps_flags, PS_INEXEC);
278 
279 #if NSYSTRACE > 0
280 	if (ISSET(p->p_flag, P_SYSTRACE)) {
281 		systrace_execve0(p);
282 		pathbuf = pool_get(&namei_pool, PR_WAITOK);
283 		error = copyinstr(SCARG(uap, path), pathbuf, MAXPATHLEN,
284 		    &pathbuflen);
285 		if (error != 0)
286 			goto clrflag;
287 	}
288 #endif
289 	if (pathbuf != NULL) {
290 		NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, p);
291 	} else {
292 		NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE,
293 		    SCARG(uap, path), p);
294 	}
295 
296 	/*
297 	 * initialize the fields of the exec package.
298 	 */
299 	if (pathbuf != NULL)
300 		pack.ep_name = pathbuf;
301 	else
302 		pack.ep_name = (char *)SCARG(uap, path);
303 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
304 	pack.ep_hdrlen = exec_maxhdrsz;
305 	pack.ep_hdrvalid = 0;
306 	pack.ep_ndp = &nid;
307 	pack.ep_interp = NULL;
308 	pack.ep_emul_arg = NULL;
309 	VMCMDSET_INIT(&pack.ep_vmcmds);
310 	pack.ep_vap = &attr;
311 	pack.ep_emul = &emul_native;
312 	pack.ep_flags = 0;
313 
314 	/* see if we can run it. */
315 	if ((error = check_exec(p, &pack)) != 0) {
316 		goto freehdr;
317 	}
318 
319 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
320 
321 	/* allocate an argument buffer */
322 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
323 #ifdef DIAGNOSTIC
324 	if (argp == NULL)
325 		panic("execve: argp == NULL");
326 #endif
327 	dp = argp;
328 	argc = 0;
329 
330 	/* copy the fake args list, if there's one, freeing it as we go */
331 	if (pack.ep_flags & EXEC_HASARGL) {
332 		tmpfap = pack.ep_fa;
333 		while (*tmpfap != NULL) {
334 			char *cp;
335 
336 			cp = *tmpfap;
337 			while (*cp)
338 				*dp++ = *cp++;
339 			*dp++ = '\0';
340 
341 			free(*tmpfap, M_EXEC);
342 			tmpfap++; argc++;
343 		}
344 		free(pack.ep_fa, M_EXEC);
345 		pack.ep_flags &= ~EXEC_HASARGL;
346 	}
347 
348 	/* Now get argv & environment */
349 	if (!(cpp = SCARG(uap, argp))) {
350 		error = EFAULT;
351 		goto bad;
352 	}
353 
354 	if (pack.ep_flags & EXEC_SKIPARG)
355 		cpp++;
356 
357 	while (1) {
358 		len = argp + ARG_MAX - dp;
359 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
360 			goto bad;
361 		if (!sp)
362 			break;
363 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
364 			if (error == ENAMETOOLONG)
365 				error = E2BIG;
366 			goto bad;
367 		}
368 		dp += len;
369 		cpp++;
370 		argc++;
371 	}
372 
373 	envc = 0;
374 	/* environment does not need to be there */
375 	if ((cpp = SCARG(uap, envp)) != NULL ) {
376 		while (1) {
377 			len = argp + ARG_MAX - dp;
378 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
379 				goto bad;
380 			if (!sp)
381 				break;
382 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
383 				if (error == ENAMETOOLONG)
384 					error = E2BIG;
385 				goto bad;
386 			}
387 			dp += len;
388 			cpp++;
389 			envc++;
390 		}
391 	}
392 
393 	dp = (char *)(((long)dp + _STACKALIGNBYTES) & ~_STACKALIGNBYTES);
394 
395 	sgap = STACKGAPLEN;
396 	if (stackgap_random != 0) {
397 		sgap += arc4random() & (stackgap_random - 1);
398 		sgap = (sgap + _STACKALIGNBYTES) & ~_STACKALIGNBYTES;
399 	}
400 
401 	/* Now check if args & environ fit into new stack */
402 	len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
403 	    sizeof(long) + dp + sgap + sizeof(struct ps_strings)) - argp;
404 
405 	len = (len + _STACKALIGNBYTES) &~ _STACKALIGNBYTES;
406 
407 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
408 		error = ENOMEM;
409 		goto bad;
410 	}
411 
412 	/* adjust "active stack depth" for process VSZ */
413 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
414 
415 	/*
416 	 * we're committed: any further errors will kill the process, so
417 	 * kill the other threads now.
418 	 */
419 	single_thread_set(p, SINGLE_EXIT, 0);
420 
421 	/*
422 	 * Prepare vmspace for remapping. Note that uvmspace_exec can replace
423 	 * pr_vmspace!
424 	 */
425 	uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
426 
427 	vm = pr->ps_vmspace;
428 	/* Now map address space */
429 	vm->vm_taddr = (char *)pack.ep_taddr;
430 	vm->vm_tsize = atop(round_page(pack.ep_tsize));
431 	vm->vm_daddr = (char *)pack.ep_daddr;
432 	vm->vm_dsize = atop(round_page(pack.ep_dsize));
433 	vm->vm_dused = 0;
434 	vm->vm_ssize = atop(round_page(pack.ep_ssize));
435 	vm->vm_maxsaddr = (char *)pack.ep_maxsaddr;
436 	vm->vm_minsaddr = (char *)pack.ep_minsaddr;
437 
438 	/* create the new process's VM space by running the vmcmds */
439 #ifdef DIAGNOSTIC
440 	if (pack.ep_vmcmds.evs_used == 0)
441 		panic("execve: no vmcmds");
442 #endif
443 	error = exec_process_vmcmds(p, &pack);
444 
445 	/* if an error happened, deallocate and punt */
446 	if (error)
447 		goto exec_abort;
448 
449 	/* remember information about the process */
450 	arginfo.ps_nargvstr = argc;
451 	arginfo.ps_nenvstr = envc;
452 
453 #ifdef MACHINE_STACK_GROWS_UP
454 	stack = (char *)USRSTACK + sizeof(arginfo) + sgap;
455 	slen = len - sizeof(arginfo) - sgap;
456 #else
457 	stack = (char *)(USRSTACK - len);
458 #endif
459 	/* Now copy argc, args & environ to new stack */
460 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
461 		goto exec_abort;
462 
463 	/* copy out the process's ps_strings structure */
464 	if (copyout(&arginfo, (char *)PS_STRINGS, sizeof(arginfo)))
465 		goto exec_abort;
466 
467 	stopprofclock(pr);	/* stop profiling */
468 	fdcloseexec(p);		/* handle close on exec */
469 	execsigs(p);		/* reset caught signals */
470 	TCB_SET(p, NULL);	/* reset the TCB address */
471 
472 	/* set command name & other accounting info */
473 	memset(p->p_comm, 0, sizeof(p->p_comm));
474 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
475 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
476 	pr->ps_acflag &= ~AFORK;
477 
478 	/* record proc's vnode, for use by procfs and others */
479 	otvp = pr->ps_textvp;
480 	vref(pack.ep_vp);
481 	pr->ps_textvp = pack.ep_vp;
482 	if (otvp)
483 		vrele(otvp);
484 
485 	atomic_setbits_int(&pr->ps_flags, PS_EXEC);
486 	if (pr->ps_flags & PS_PPWAIT) {
487 		atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
488 		atomic_clearbits_int(&pr->ps_pptr->ps_flags, PS_ISPWAIT);
489 		wakeup(pr->ps_pptr);
490 	}
491 
492 	/*
493 	 * If process does execve() while it has a mismatched real,
494 	 * effective, or saved uid/gid, we set PS_SUGIDEXEC.
495 	 */
496 	if (cred->cr_uid != cred->cr_ruid ||
497 	    cred->cr_uid != cred->cr_svuid ||
498 	    cred->cr_gid != cred->cr_rgid ||
499 	    cred->cr_gid != cred->cr_svgid)
500 		atomic_setbits_int(&pr->ps_flags, PS_SUGIDEXEC);
501 	else
502 		atomic_clearbits_int(&pr->ps_flags, PS_SUGIDEXEC);
503 
504 	/*
505 	 * deal with set[ug]id.
506 	 * MNT_NOEXEC has already been used to disable s[ug]id.
507 	 */
508 	if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) {
509 		int i;
510 
511 		atomic_setbits_int(&pr->ps_flags, PS_SUGID|PS_SUGIDEXEC);
512 
513 #ifdef KTRACE
514 		/*
515 		 * If process is being ktraced, turn off - unless
516 		 * root set it.
517 		 */
518 		if (pr->ps_tracevp && !(pr->ps_traceflag & KTRFAC_ROOT))
519 			ktrcleartrace(pr);
520 #endif
521 		p->p_ucred = cred = crcopy(cred);
522 		if (attr.va_mode & VSUID)
523 			cred->cr_uid = attr.va_uid;
524 		if (attr.va_mode & VSGID)
525 			cred->cr_gid = attr.va_gid;
526 
527 		/*
528 		 * For set[ug]id processes, a few caveats apply to
529 		 * stdin, stdout, and stderr.
530 		 */
531 		error = 0;
532 		fdplock(p->p_fd);
533 		for (i = 0; i < 3; i++) {
534 			struct file *fp = NULL;
535 
536 			/*
537 			 * NOTE - This will never return NULL because of
538 			 * immature fds. The file descriptor table is not
539 			 * shared because we're suid.
540 			 */
541 			fp = fd_getfile(p->p_fd, i);
542 #ifdef PROCFS
543 			/*
544 			 * Close descriptors that are writing to procfs.
545 			 */
546 			if (fp && fp->f_type == DTYPE_VNODE &&
547 			    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS &&
548 			    (fp->f_flag & FWRITE)) {
549 				fdrelease(p, i);
550 				fp = NULL;
551 			}
552 #endif
553 
554 			/*
555 			 * Ensure that stdin, stdout, and stderr are already
556 			 * allocated.  We do not want userland to accidentally
557 			 * allocate descriptors in this range which has implied
558 			 * meaning to libc.
559 			 */
560 			if (fp == NULL) {
561 				short flags = FREAD | (i == 0 ? 0 : FWRITE);
562 				struct vnode *vp;
563 				int indx;
564 
565 				if ((error = falloc(p, &fp, &indx)) != 0)
566 					break;
567 #ifdef DIAGNOSTIC
568 				if (indx != i)
569 					panic("sys_execve: falloc indx != i");
570 #endif
571 				if ((error = cdevvp(getnulldev(), &vp)) != 0) {
572 					fdremove(p->p_fd, indx);
573 					closef(fp, p);
574 					break;
575 				}
576 				if ((error = VOP_OPEN(vp, flags, cred, p)) != 0) {
577 					fdremove(p->p_fd, indx);
578 					closef(fp, p);
579 					vrele(vp);
580 					break;
581 				}
582 				if (flags & FWRITE)
583 					vp->v_writecount++;
584 				fp->f_flag = flags;
585 				fp->f_type = DTYPE_VNODE;
586 				fp->f_ops = &vnops;
587 				fp->f_data = (caddr_t)vp;
588 				FILE_SET_MATURE(fp, p);
589 			}
590 		}
591 		fdpunlock(p->p_fd);
592 		if (error)
593 			goto exec_abort;
594 	} else
595 		atomic_clearbits_int(&pr->ps_flags, PS_SUGID);
596 
597 	/*
598 	 * Reset the saved ugids and update the process's copy of the
599 	 * creds if the creds have been changed
600 	 */
601 	if (cred->cr_uid != cred->cr_svuid ||
602 	    cred->cr_gid != cred->cr_svgid) {
603 		/* make sure we have unshared ucreds */
604 		p->p_ucred = cred = crcopy(cred);
605 		cred->cr_svuid = cred->cr_uid;
606 		cred->cr_svgid = cred->cr_gid;
607 	}
608 
609 	if (pr->ps_ucred != cred) {
610 		struct ucred *ocred;
611 
612 		ocred = pr->ps_ucred;
613 		crhold(cred);
614 		pr->ps_ucred = cred;
615 		crfree(ocred);
616 	}
617 
618 	if (pr->ps_flags & PS_SUGIDEXEC) {
619 		int i, s = splclock();
620 
621 		timeout_del(&pr->ps_realit_to);
622 		for (i = 0; i < nitems(pr->ps_timer); i++) {
623 			timerclear(&pr->ps_timer[i].it_interval);
624 			timerclear(&pr->ps_timer[i].it_value);
625 		}
626 		splx(s);
627 	}
628 
629 	/* reset CPU time usage for the thread, but not the process */
630 	timespecclear(&p->p_tu.tu_runtime);
631 	p->p_tu.tu_uticks = p->p_tu.tu_sticks = p->p_tu.tu_iticks = 0;
632 
633 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
634 
635 	pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
636 	vn_close(pack.ep_vp, FREAD, cred, p);
637 
638 	/*
639 	 * notify others that we exec'd
640 	 */
641 	KNOTE(&pr->ps_klist, NOTE_EXEC);
642 
643 	/* setup new registers and do misc. setup. */
644 	if (pack.ep_emul->e_fixup != NULL) {
645 		if ((*pack.ep_emul->e_fixup)(p, &pack) != 0)
646 			goto free_pack_abort;
647 	}
648 #ifdef MACHINE_STACK_GROWS_UP
649 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval);
650 #else
651 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval);
652 #endif
653 
654 	/* map the process's signal trampoline code */
655 	if (exec_sigcode_map(pr, pack.ep_emul))
656 		goto free_pack_abort;
657 
658 #ifdef __HAVE_EXEC_MD_MAP
659 	/* perform md specific mappings that process might need */
660 	if (exec_md_map(p, &pack))
661 		goto free_pack_abort;
662 #endif
663 
664 	if (pr->ps_flags & PS_TRACED)
665 		psignal(p, SIGTRAP);
666 
667 	free(pack.ep_hdr, M_EXEC);
668 
669 	/*
670 	 * Call emulation specific exec hook. This can setup per-process
671 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
672 	 *
673 	 * If we are executing process of different emulation than the
674 	 * original forked process, call e_proc_exit() of the old emulation
675 	 * first, then e_proc_exec() of new emulation. If the emulation is
676 	 * same, the exec hook code should deallocate any old emulation
677 	 * resources held previously by this process.
678 	 */
679 	if (pr->ps_emul && pr->ps_emul->e_proc_exit &&
680 	    pr->ps_emul != pack.ep_emul)
681 		(*pr->ps_emul->e_proc_exit)(p);
682 
683 	p->p_descfd = 255;
684 	if ((pack.ep_flags & EXEC_HASFD) && pack.ep_fd < 255)
685 		p->p_descfd = pack.ep_fd;
686 
687 	/*
688 	 * Call exec hook. Emulation code may NOT store reference to anything
689 	 * from &pack.
690 	 */
691 	if (pack.ep_emul->e_proc_exec)
692 		(*pack.ep_emul->e_proc_exec)(p, &pack);
693 
694 	/* update ps_emul, the old value is no longer needed */
695 	pr->ps_emul = pack.ep_emul;
696 
697 #ifdef KTRACE
698 	if (KTRPOINT(p, KTR_EMUL))
699 		ktremul(p);
700 #endif
701 
702 	atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
703 	single_thread_clear(p, P_SUSPSIG);
704 
705 #if NSYSTRACE > 0
706 	if (ISSET(p->p_flag, P_SYSTRACE) &&
707 	    wassugid && !ISSET(pr->ps_flags, PS_SUGID | PS_SUGIDEXEC))
708 		systrace_execve1(pathbuf, p);
709 #endif
710 
711 	if (pathbuf != NULL)
712 		pool_put(&namei_pool, pathbuf);
713 
714 	return (0);
715 
716 bad:
717 	/* free the vmspace-creation commands, and release their references */
718 	kill_vmcmds(&pack.ep_vmcmds);
719 	/* kill any opened file descriptor, if necessary */
720 	if (pack.ep_flags & EXEC_HASFD) {
721 		pack.ep_flags &= ~EXEC_HASFD;
722 		fdplock(p->p_fd);
723 		(void) fdrelease(p, pack.ep_fd);
724 		fdpunlock(p->p_fd);
725 	}
726 	if (pack.ep_interp != NULL)
727 		pool_put(&namei_pool, pack.ep_interp);
728 	if (pack.ep_emul_arg != NULL)
729 		free(pack.ep_emul_arg, M_TEMP);
730 	/* close and put the exec'd file */
731 	vn_close(pack.ep_vp, FREAD, cred, p);
732 	pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
733 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
734 
735  freehdr:
736 	free(pack.ep_hdr, M_EXEC);
737 #if NSYSTRACE > 0
738  clrflag:
739 #endif
740 	atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
741 	single_thread_clear(p, P_SUSPSIG);
742 
743 	if (pathbuf != NULL)
744 		pool_put(&namei_pool, pathbuf);
745 
746 	return (error);
747 
748 exec_abort:
749 	/*
750 	 * the old process doesn't exist anymore.  exit gracefully.
751 	 * get rid of the (new) address space we have created, if any, get rid
752 	 * of our namei data and vnode, and exit noting failure
753 	 */
754 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
755 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
756 	if (pack.ep_interp != NULL)
757 		pool_put(&namei_pool, pack.ep_interp);
758 	if (pack.ep_emul_arg != NULL)
759 		free(pack.ep_emul_arg, M_TEMP);
760 	pool_put(&namei_pool, nid.ni_cnd.cn_pnbuf);
761 	vn_close(pack.ep_vp, FREAD, cred, p);
762 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
763 
764 free_pack_abort:
765 	free(pack.ep_hdr, M_EXEC);
766 	exit1(p, W_EXITCODE(0, SIGABRT), EXIT_NORMAL);
767 
768 	/* NOTREACHED */
769 	atomic_clearbits_int(&pr->ps_flags, PS_INEXEC);
770 	if (pathbuf != NULL)
771 		pool_put(&namei_pool, pathbuf);
772 
773 	return (0);
774 }
775 
776 
777 void *
778 copyargs(struct exec_package *pack, struct ps_strings *arginfo, void *stack,
779     void *argp)
780 {
781 	char **cpp = stack;
782 	char *dp, *sp;
783 	size_t len;
784 	void *nullp = NULL;
785 	long argc = arginfo->ps_nargvstr;
786 	int envc = arginfo->ps_nenvstr;
787 
788 	if (copyout(&argc, cpp++, sizeof(argc)))
789 		return (NULL);
790 
791 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
792 	sp = argp;
793 
794 	/* XXX don't copy them out, remap them! */
795 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
796 
797 	for (; --argc >= 0; sp += len, dp += len)
798 		if (copyout(&dp, cpp++, sizeof(dp)) ||
799 		    copyoutstr(sp, dp, ARG_MAX, &len))
800 			return (NULL);
801 
802 	if (copyout(&nullp, cpp++, sizeof(nullp)))
803 		return (NULL);
804 
805 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
806 
807 	for (; --envc >= 0; sp += len, dp += len)
808 		if (copyout(&dp, cpp++, sizeof(dp)) ||
809 		    copyoutstr(sp, dp, ARG_MAX, &len))
810 			return (NULL);
811 
812 	if (copyout(&nullp, cpp++, sizeof(nullp)))
813 		return (NULL);
814 
815 	return (cpp);
816 }
817 
818 int
819 exec_sigcode_map(struct process *pr, struct emul *e)
820 {
821 	vsize_t sz;
822 
823 	sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
824 
825 	/*
826 	 * If we don't have a sigobject for this emulation, create one.
827 	 *
828 	 * sigobject is an anonymous memory object (just like SYSV shared
829 	 * memory) that we keep a permanent reference to and that we map
830 	 * in all processes that need this sigcode. The creation is simple,
831 	 * we create an object, add a permanent reference to it, map it in
832 	 * kernel space, copy out the sigcode to it and unmap it.
833 	 * Then we map it with PROT_READ|PROT_EXEC into the process just
834 	 * the way sys_mmap would map it.
835 	 */
836 	if (e->e_sigobject == NULL) {
837 		vaddr_t va;
838 		int r;
839 
840 		e->e_sigobject = uao_create(sz, 0);
841 		uao_reference(e->e_sigobject);	/* permanent reference */
842 
843 		if ((r = uvm_map(kernel_map, &va, round_page(sz), e->e_sigobject,
844 		    0, 0, UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
845 		    UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
846 			uao_detach(e->e_sigobject);
847 			return (ENOMEM);
848 		}
849 		memcpy((void *)va, e->e_sigcode, sz);
850 		uvm_unmap(kernel_map, va, va + round_page(sz));
851 	}
852 
853 	pr->ps_sigcode = 0; /* no hint */
854 	uao_reference(e->e_sigobject);
855 	if (uvm_map(&pr->ps_vmspace->vm_map, &pr->ps_sigcode, round_page(sz),
856 	    e->e_sigobject, 0, 0, UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX,
857 	    UVM_INH_SHARE, UVM_ADV_RANDOM, 0))) {
858 		uao_detach(e->e_sigobject);
859 		return (ENOMEM);
860 	}
861 
862 	return (0);
863 }
864