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