xref: /openbsd-src/sys/kern/kern_exec.c (revision 33b792a3c1c87b47219fdf9a73548c4003214de3)
1 /*	$OpenBSD: kern_exec.c,v 1.65 2002/02/17 04:31:26 art 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/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/acct.h>
47 #include <sys/exec.h>
48 #include <sys/ktrace.h>
49 #include <sys/resourcevar.h>
50 #include <sys/wait.h>
51 #include <sys/mman.h>
52 #include <sys/signalvar.h>
53 #include <sys/stat.h>
54 #include <sys/conf.h>
55 #ifdef SYSVSHM
56 #include <sys/shm.h>
57 #endif
58 
59 #include <sys/syscallargs.h>
60 
61 #include <uvm/uvm_extern.h>
62 
63 #include <machine/cpu.h>
64 #include <machine/reg.h>
65 
66 #include <dev/rndvar.h>
67 
68 /*
69  * stackgap_random specifies if the stackgap should have a random size added
70  * to it. Must be a n^2. If non-zero, the stack gap will be calculated as:
71  * (arc4random() * ALIGNBYTES) & (stackgap_random - 1) + STACKGAPLEN.
72  */
73 int stackgap_random = 1024;
74 
75 /*
76  * check exec:
77  * given an "executable" described in the exec package's namei info,
78  * see what we can do with it.
79  *
80  * ON ENTRY:
81  *	exec package with appropriate namei info
82  *	proc pointer of exec'ing proc
83  *	NO SELF-LOCKED VNODES
84  *
85  * ON EXIT:
86  *	error:	nothing held, etc.  exec header still allocated.
87  *	ok:	filled exec package, one locked vnode.
88  *
89  * EXEC SWITCH ENTRY:
90  * 	Locked vnode to check, exec package, proc.
91  *
92  * EXEC SWITCH EXIT:
93  *	ok:	return 0, filled exec package, one locked vnode.
94  *	error:	destructive:
95  *			everything deallocated execept exec header.
96  *		non-descructive:
97  *			error code, locked vnode, exec header unmodified
98  */
99 int
100 check_exec(p, epp)
101 	struct proc *p;
102 	struct exec_package *epp;
103 {
104 	int error, i;
105 	struct vnode *vp;
106 	struct nameidata *ndp;
107 	size_t resid;
108 
109 	ndp = epp->ep_ndp;
110 	ndp->ni_cnd.cn_nameiop = LOOKUP;
111 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
112 	/* first get the vnode */
113 	if ((error = namei(ndp)) != 0)
114 		return (error);
115 	epp->ep_vp = vp = ndp->ni_vp;
116 
117 	/* check for regular file */
118 	if (vp->v_type == VDIR) {
119 		error = EISDIR;
120 		goto bad1;
121 	}
122 	if (vp->v_type != VREG) {
123 		error = EACCES;
124 		goto bad1;
125 	}
126 
127 	/* get attributes */
128 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
129 		goto bad1;
130 
131 	/* Check mount point */
132 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
133 		error = EACCES;
134 		goto bad1;
135 	}
136 
137 	if ((vp->v_mount->mnt_flag & MNT_NOSUID))
138 		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
139 
140 	/* check access.  for root we have to see if any exec bit on */
141 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
142 		goto bad1;
143 	if ((epp->ep_vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
144 		error = EACCES;
145 		goto bad1;
146 	}
147 
148 	/* try to open it */
149 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
150 		goto bad1;
151 
152 	/* now we have the file, get the exec header */
153 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
154 	    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
155 	if (error)
156 		goto bad2;
157 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
158 
159 	/*
160 	 * set up the vmcmds for creation of the process
161 	 * address space
162 	 */
163 	error = ENOEXEC;
164 	for (i = 0; i < nexecs && error != 0; i++) {
165 		int newerror;
166 
167 		if (execsw[i].es_check == NULL)
168 			continue;
169 
170 		newerror = (*execsw[i].es_check)(p, epp);
171 		/* make sure the first "interesting" error code is saved. */
172 		if (!newerror || error == ENOEXEC)
173 			error = newerror;
174 		if (epp->ep_flags & EXEC_DESTR && error != 0)
175 			return (error);
176 	}
177 	if (!error) {
178 		/* check that entry point is sane */
179 		if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
180 			error = ENOEXEC;
181 		}
182 
183 		/* check limits */
184 		if ((epp->ep_tsize > MAXTSIZ) ||
185 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
186 			error = ENOMEM;
187 
188 		if (!error)
189 			return (0);
190 	}
191 
192 	/*
193 	 * free any vmspace-creation commands,
194 	 * and release their references
195 	 */
196 	kill_vmcmds(&epp->ep_vmcmds);
197 
198 bad2:
199 	/*
200 	 * unlock and close the vnode, free the
201 	 * pathname buf, and punt.
202 	 */
203 	VOP_UNLOCK(vp, 0, p);
204 	vn_close(vp, FREAD, p->p_ucred, p);
205 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
206 	return (error);
207 
208 bad1:
209 	/*
210 	 * free the namei pathname buffer, and put the vnode
211 	 * (which we don't yet have open).
212 	 */
213 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
214 	vput(vp);
215 	return (error);
216 }
217 
218 /*
219  * exec system call
220  */
221 /* ARGSUSED */
222 int
223 sys_execve(p, v, retval)
224 	register struct proc *p;
225 	void *v;
226 	register_t *retval;
227 {
228 	struct sys_execve_args /* {
229 		syscallarg(char *) path;
230 		syscallarg(char * *) argp;
231 		syscallarg(char * *) envp;
232 	} */ *uap = v;
233 	int error, i;
234 	struct exec_package pack;
235 	struct nameidata nid;
236 	struct vattr attr;
237 	struct ucred *cred = p->p_ucred;
238 	char *argp;
239 	char * const *cpp, *dp, *sp;
240 	long argc, envc;
241 	size_t len, sgap;
242 #ifdef MACHINE_STACK_GROWS_UP
243 	size_t slen;
244 #endif
245 	char *stack;
246 	struct ps_strings arginfo;
247 	struct vmspace *vm = p->p_vmspace;
248 	char **tmpfap;
249 	int szsigcode;
250 	extern struct emul emul_native;
251 
252 	/*
253 	 * Cheap solution to complicated problems.
254 	 * Mark this process as "leave me alone, I'm execing".
255 	 */
256 	p->p_flag |= P_INEXEC;
257 
258 	/*
259 	 * figure out the maximum size of an exec header, if necessary.
260 	 * XXX should be able to keep LKM code from modifying exec switch
261 	 * when we're still using it, but...
262 	 */
263 	if (exec_maxhdrsz == 0) {
264 		for (i = 0; i < nexecs; i++)
265 			if (execsw[i].es_check != NULL
266 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
267 				exec_maxhdrsz = execsw[i].es_hdrsz;
268 	}
269 
270 	/* init the namei data to point the file user's program name */
271 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
272 
273 	/*
274 	 * initialize the fields of the exec package.
275 	 */
276 	pack.ep_name = (char *)SCARG(uap, path);
277 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
278 	pack.ep_hdrlen = exec_maxhdrsz;
279 	pack.ep_hdrvalid = 0;
280 	pack.ep_ndp = &nid;
281 	pack.ep_emul_arg = NULL;
282 	VMCMDSET_INIT(&pack.ep_vmcmds);
283 	pack.ep_vap = &attr;
284 	pack.ep_emul = &emul_native;
285 	pack.ep_flags = 0;
286 
287 	/* see if we can run it. */
288 	if ((error = check_exec(p, &pack)) != 0) {
289 		goto freehdr;
290 	}
291 
292 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
293 
294 	/* allocate an argument buffer */
295 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
296 #ifdef DIAGNOSTIC
297 	if (argp == (vaddr_t) 0)
298 		panic("execve: argp == NULL");
299 #endif
300 	dp = argp;
301 	argc = 0;
302 
303 	/* copy the fake args list, if there's one, freeing it as we go */
304 	if (pack.ep_flags & EXEC_HASARGL) {
305 		tmpfap = pack.ep_fa;
306 		while (*tmpfap != NULL) {
307 			char *cp;
308 
309 			cp = *tmpfap;
310 			while (*cp)
311 				*dp++ = *cp++;
312 			dp++;
313 
314 			free(*tmpfap, M_EXEC);
315 			tmpfap++; argc++;
316 		}
317 		FREE(pack.ep_fa, M_EXEC);
318 		pack.ep_flags &= ~EXEC_HASARGL;
319 	}
320 
321 	/* Now get argv & environment */
322 	if (!(cpp = SCARG(uap, argp))) {
323 		error = EINVAL;
324 		goto bad;
325 	}
326 
327 	if (pack.ep_flags & EXEC_SKIPARG)
328 		cpp++;
329 
330 	while (1) {
331 		len = argp + ARG_MAX - dp;
332 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
333 			goto bad;
334 		if (!sp)
335 			break;
336 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
337 			if (error == ENAMETOOLONG)
338 				error = E2BIG;
339 			goto bad;
340 		}
341 		dp += len;
342 		cpp++;
343 		argc++;
344 	}
345 
346 	envc = 0;
347 	/* environment need not be there */
348 	if ((cpp = SCARG(uap, envp)) != NULL ) {
349 		while (1) {
350 			len = argp + ARG_MAX - dp;
351 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
352 				goto bad;
353 			if (!sp)
354 				break;
355 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
356 				if (error == ENAMETOOLONG)
357 					error = E2BIG;
358 				goto bad;
359 			}
360 			dp += len;
361 			cpp++;
362 			envc++;
363 		}
364 	}
365 
366 	dp = (char *)ALIGN(dp);
367 
368 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
369 
370 	sgap = STACKGAPLEN;
371 	if (stackgap_random != 0)
372 		sgap += (arc4random() * ALIGNBYTES) & (stackgap_random - 1);
373 	/* Now check if args & environ fit into new stack */
374 	len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
375 	    sizeof(long) + dp + sgap + szsigcode +
376 	    sizeof(struct ps_strings)) - argp;
377 
378 	len = ALIGN(len);	/* make the stack "safely" aligned */
379 
380 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
381 		error = ENOMEM;
382 		goto bad;
383 	}
384 
385 	/* adjust "active stack depth" for process VSZ */
386 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
387 
388 	/*
389 	 * Prepare vmspace for remapping. Note that uvmspace_exec can replace
390 	 * p_vmspace!
391 	 */
392 	uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
393 
394 	vm = p->p_vmspace;
395 	/* Now map address space */
396 	vm->vm_taddr = (char *)pack.ep_taddr;
397 	vm->vm_tsize = btoc(pack.ep_tsize);
398 	vm->vm_daddr = (char *)pack.ep_daddr;
399 	vm->vm_dsize = btoc(pack.ep_dsize);
400 	vm->vm_ssize = btoc(pack.ep_ssize);
401 	vm->vm_maxsaddr = (char *)pack.ep_maxsaddr;
402 
403 	/* create the new process's VM space by running the vmcmds */
404 #ifdef DIAGNOSTIC
405 	if (pack.ep_vmcmds.evs_used == 0)
406 		panic("execve: no vmcmds");
407 #endif
408 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
409 		struct exec_vmcmd *vcp;
410 
411 		vcp = &pack.ep_vmcmds.evs_cmds[i];
412 		error = (*vcp->ev_proc)(p, vcp);
413 	}
414 
415 	/* free the vmspace-creation commands, and release their references */
416 	kill_vmcmds(&pack.ep_vmcmds);
417 
418 	/* if an error happened, deallocate and punt */
419 	if (error)
420 		goto exec_abort;
421 
422 	/* remember information about the process */
423 	arginfo.ps_nargvstr = argc;
424 	arginfo.ps_nenvstr = envc;
425 
426 #ifdef MACHINE_STACK_GROWS_UP
427 	stack = (char *)USRSTACK + sizeof(arginfo) + szsigcode;
428 	slen = len - sizeof(arginfo) - szsigcode;
429 #else
430 	stack = (char *)(USRSTACK - len);
431 #endif
432 	/* Now copy argc, args & environ to new stack */
433 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
434 		goto exec_abort;
435 
436 	/* copy out the process's ps_strings structure */
437 	if (copyout(&arginfo, (char *)PS_STRINGS, sizeof(arginfo)))
438 		goto exec_abort;
439 
440 	/* copy out the process's signal trampoline code */
441 #ifdef MACHINE_STACK_GROWS_UP
442 	if (szsigcode && copyout((char *)pack.ep_emul->e_sigcode,
443 	    ((char *)PS_STRINGS) + sizeof(arginfo), szsigcode))
444 		goto exec_abort;
445 #else
446 	if (szsigcode && copyout((char *)pack.ep_emul->e_sigcode,
447 	    ((char *)PS_STRINGS) - szsigcode, szsigcode))
448 		goto exec_abort;
449 #endif
450 
451 	stopprofclock(p);	/* stop profiling */
452 	fdcloseexec(p);		/* handle close on exec */
453 	execsigs(p);		/* reset catched signals */
454 
455 	/* set command name & other accounting info */
456 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
457 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
458 	p->p_comm[len] = 0;
459 	p->p_acflag &= ~AFORK;
460 
461 	/* record proc's vnode, for use by procfs and others */
462 	if (p->p_textvp)
463 		vrele(p->p_textvp);
464 	VREF(pack.ep_vp);
465 	p->p_textvp = pack.ep_vp;
466 
467 	p->p_flag |= P_EXEC;
468 	if (p->p_flag & P_PPWAIT) {
469 		p->p_flag &= ~P_PPWAIT;
470 		wakeup((caddr_t)p->p_pptr);
471 	}
472 
473 	/*
474 	 * If process does execve() while it has euid/uid or egid/gid
475 	 * which are mismatched, it remains P_SUGIDEXEC.
476 	 */
477 	if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
478 	    p->p_ucred->cr_gid == p->p_cred->p_rgid)
479 		p->p_flag &= ~P_SUGIDEXEC;
480 
481 	/*
482 	 * deal with set[ug]id.
483 	 * MNT_NOEXEC has already been used to disable s[ug]id.
484 	 */
485 	if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) {
486 		int i;
487 
488 		p->p_flag |= P_SUGID;
489 		p->p_flag |= P_SUGIDEXEC;
490 
491 #ifdef KTRACE
492 		/*
493 		 * If process is being ktraced, turn off - unless
494 		 * root set it.
495 		 */
496 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
497 			p->p_traceflag = 0;
498 			ktrsettracevnode(p, NULL);
499 		}
500 #endif
501 		p->p_ucred = crcopy(cred);
502 		if (attr.va_mode & VSUID)
503 			p->p_ucred->cr_uid = attr.va_uid;
504 		if (attr.va_mode & VSGID)
505 			p->p_ucred->cr_gid = attr.va_gid;
506 
507 		/*
508 		 * For set[ug]id processes, a few caveats apply to
509 		 * stdin, stdout, and stderr.
510 		 */
511 		for (i = 0; i < 3; i++) {
512 			struct file *fp = NULL;
513 
514 			/*
515 			 * NOTE - This will never return NULL because of
516 			 * unmature fds. The file descriptor table is not
517 			 * shared because we're suid.
518 			 */
519 			fp = fd_getfile(p->p_fd, i);
520 #ifdef PROCFS
521 			/*
522 			 * Close descriptors that are writing to procfs.
523 			 */
524 			if (fp && fp->f_type == DTYPE_VNODE &&
525 			    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS &&
526 			    (fp->f_flag & FWRITE)) {
527 				fdrelease(p, i);
528 				fp = NULL;
529 			}
530 #endif
531 
532 			/*
533 			 * Ensure that stdin, stdout, and stderr are already
534 			 * allocated.  We do not want userland to accidentally
535 			 * allocate descriptors in this range which has implied
536 			 * meaning to libc.
537 			 *
538 			 * XXX - Shouldn't the exec fail if we can't allocate
539 			 *       resources here?
540 			 */
541 			if (fp == NULL) {
542 				short flags = FREAD | (i == 0 ? 0 : FWRITE);
543 				struct vnode *vp;
544 				int indx;
545 
546 				if ((error = falloc(p, &fp, &indx)) != 0)
547 					break;
548 #ifdef DIAGNOSTIC
549 				if (indx != i)
550 					panic("sys_execve: falloc indx != i");
551 #endif
552 				if ((error = cdevvp(getnulldev(), &vp)) != 0) {
553 					fdremove(p->p_fd, indx);
554 					closef(fp, p);
555 					break;
556 				}
557 				if ((error = VOP_OPEN(vp, flags, p->p_ucred, p)) != 0) {
558 					fdremove(p->p_fd, indx);
559 					closef(fp, p);
560 					vrele(vp);
561 					break;
562 				}
563 				if (flags & FWRITE)
564 					vp->v_writecount++;
565 				fp->f_flag = flags;
566 				fp->f_type = DTYPE_VNODE;
567 				fp->f_ops = &vnops;
568 				fp->f_data = (caddr_t)vp;
569 				FILE_SET_MATURE(fp);
570 			}
571 		}
572 	} else
573 		p->p_flag &= ~P_SUGID;
574 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
575 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
576 
577 	if (p->p_flag & P_SUGIDEXEC) {
578 		int i, s = splclock();
579 
580 		timeout_del(&p->p_realit_to);
581 		timerclear(&p->p_realtimer.it_interval);
582 		timerclear(&p->p_realtimer.it_value);
583 		for (i = 0; i < sizeof(p->p_stats->p_timer) /
584 		    sizeof(p->p_stats->p_timer[0]); i++) {
585 			timerclear(&p->p_stats->p_timer[i].it_interval);
586 			timerclear(&p->p_stats->p_timer[i].it_value);
587 		}
588 		splx(s);
589 	}
590 
591 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
592 
593 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
594 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
595 	vput(pack.ep_vp);
596 
597 	/*
598 	 * notify others that we exec'd
599 	 */
600 	KNOTE(&p->p_klist, NOTE_EXEC);
601 
602 	/* setup new registers and do misc. setup. */
603 	if (pack.ep_emul->e_fixup != NULL) {
604 		if ((*pack.ep_emul->e_fixup)(p, &pack) != 0)
605 			goto free_pack_abort;
606 	}
607 #ifdef MACHINE_STACK_GROWS_UP
608 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval);
609 #else
610 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval);
611 #endif
612 
613 	if (p->p_flag & P_TRACED)
614 		psignal(p, SIGTRAP);
615 
616 	p->p_emul = pack.ep_emul;
617 	free(pack.ep_hdr, M_EXEC);
618 
619 #ifdef KTRACE
620 	if (KTRPOINT(p, KTR_EMUL))
621 		ktremul(p, p->p_emul->e_name);
622 #endif
623 	p->p_flag &= ~P_INEXEC;
624 	return (0);
625 
626 bad:
627 	/* free the vmspace-creation commands, and release their references */
628 	kill_vmcmds(&pack.ep_vmcmds);
629 	/* kill any opened file descriptor, if necessary */
630 	if (pack.ep_flags & EXEC_HASFD) {
631 		pack.ep_flags &= ~EXEC_HASFD;
632 		(void) fdrelease(p, pack.ep_fd);
633 	}
634 	/* close and put the exec'd file */
635 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
636 	vput(pack.ep_vp);
637 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
638 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
639 
640 freehdr:
641 	free(pack.ep_hdr, M_EXEC);
642 	p->p_flag &= ~P_INEXEC;
643 	return (error);
644 
645 exec_abort:
646 	/*
647 	 * the old process doesn't exist anymore.  exit gracefully.
648 	 * get rid of the (new) address space we have created, if any, get rid
649 	 * of our namei data and vnode, and exit noting failure
650 	 */
651 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
652 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
653 	if (pack.ep_emul_arg)
654 		FREE(pack.ep_emul_arg, M_TEMP);
655 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
656 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
657 	vput(pack.ep_vp);
658 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
659 
660 free_pack_abort:
661 	free(pack.ep_hdr, M_EXEC);
662 	exit1(p, W_EXITCODE(0, SIGABRT));
663 	exit1(p, -1);
664 
665 	/* NOTREACHED */
666 	p->p_flag &= ~P_INEXEC;
667 	return (0);
668 }
669 
670 
671 void *
672 copyargs(pack, arginfo, stack, argp)
673 	struct exec_package *pack;
674 	struct ps_strings *arginfo;
675 	void *stack;
676 	void *argp;
677 {
678 	char **cpp = stack;
679 	char *dp, *sp;
680 	size_t len;
681 	void *nullp = NULL;
682 	long argc = arginfo->ps_nargvstr;
683 	int envc = arginfo->ps_nenvstr;
684 
685 	if (copyout(&argc, cpp++, sizeof(argc)))
686 		return (NULL);
687 
688 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
689 	sp = argp;
690 
691 	/* XXX don't copy them out, remap them! */
692 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
693 
694 	for (; --argc >= 0; sp += len, dp += len)
695 		if (copyout(&dp, cpp++, sizeof(dp)) ||
696 		    copyoutstr(sp, dp, ARG_MAX, &len))
697 			return (NULL);
698 
699 	if (copyout(&nullp, cpp++, sizeof(nullp)))
700 		return (NULL);
701 
702 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
703 
704 	for (; --envc >= 0; sp += len, dp += len)
705 		if (copyout(&dp, cpp++, sizeof(dp)) ||
706 		    copyoutstr(sp, dp, ARG_MAX, &len))
707 			return (NULL);
708 
709 	if (copyout(&nullp, cpp++, sizeof(nullp)))
710 		return (NULL);
711 
712 	return (cpp);
713 }
714