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