xref: /openbsd-src/sys/kern/kern_exec.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: kern_exec.c,v 1.62 2001/12/19 08:58:06 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;
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 	 * figure out the maximum size of an exec header, if necessary.
254 	 * XXX should be able to keep LKM code from modifying exec switch
255 	 * when we're still using it, but...
256 	 */
257 	if (exec_maxhdrsz == 0) {
258 		for (i = 0; i < nexecs; i++)
259 			if (execsw[i].es_check != NULL
260 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
261 				exec_maxhdrsz = execsw[i].es_hdrsz;
262 	}
263 
264 	/* init the namei data to point the file user's program name */
265 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
266 
267 	/*
268 	 * initialize the fields of the exec package.
269 	 */
270 	pack.ep_name = (char *)SCARG(uap, path);
271 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
272 	pack.ep_hdrlen = exec_maxhdrsz;
273 	pack.ep_hdrvalid = 0;
274 	pack.ep_ndp = &nid;
275 	pack.ep_emul_arg = NULL;
276 	VMCMDSET_INIT(&pack.ep_vmcmds);
277 	pack.ep_vap = &attr;
278 	pack.ep_emul = &emul_native;
279 	pack.ep_flags = 0;
280 
281 	/* see if we can run it. */
282 	if ((error = check_exec(p, &pack)) != 0) {
283 		goto freehdr;
284 	}
285 
286 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
287 
288 	/* allocate an argument buffer */
289 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
290 #ifdef DIAGNOSTIC
291 	if (argp == (vaddr_t) 0)
292 		panic("execve: argp == NULL");
293 #endif
294 	dp = argp;
295 	argc = 0;
296 
297 	/* copy the fake args list, if there's one, freeing it as we go */
298 	if (pack.ep_flags & EXEC_HASARGL) {
299 		tmpfap = pack.ep_fa;
300 		while (*tmpfap != NULL) {
301 			char *cp;
302 
303 			cp = *tmpfap;
304 			while (*cp)
305 				*dp++ = *cp++;
306 			dp++;
307 
308 			free(*tmpfap, M_EXEC);
309 			tmpfap++; argc++;
310 		}
311 		FREE(pack.ep_fa, M_EXEC);
312 		pack.ep_flags &= ~EXEC_HASARGL;
313 	}
314 
315 	/* Now get argv & environment */
316 	if (!(cpp = SCARG(uap, argp))) {
317 		error = EINVAL;
318 		goto bad;
319 	}
320 
321 	if (pack.ep_flags & EXEC_SKIPARG)
322 		cpp++;
323 
324 	while (1) {
325 		len = argp + ARG_MAX - dp;
326 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
327 			goto bad;
328 		if (!sp)
329 			break;
330 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
331 			if (error == ENAMETOOLONG)
332 				error = E2BIG;
333 			goto bad;
334 		}
335 		dp += len;
336 		cpp++;
337 		argc++;
338 	}
339 
340 	envc = 0;
341 	/* environment need not be there */
342 	if ((cpp = SCARG(uap, envp)) != NULL ) {
343 		while (1) {
344 			len = argp + ARG_MAX - dp;
345 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
346 				goto bad;
347 			if (!sp)
348 				break;
349 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
350 				if (error == ENAMETOOLONG)
351 					error = E2BIG;
352 				goto bad;
353 			}
354 			dp += len;
355 			cpp++;
356 			envc++;
357 		}
358 	}
359 
360 	dp = (char *)ALIGN(dp);
361 
362 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
363 
364 	sgap = STACKGAPLEN;
365 	if (stackgap_random != 0)
366 		sgap += (arc4random() * ALIGNBYTES) & (stackgap_random - 1);
367 	/* Now check if args & environ fit into new stack */
368 	len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
369 	    sizeof(long) + dp + sgap + szsigcode +
370 	    sizeof(struct ps_strings)) - argp;
371 
372 	len = ALIGN(len);	/* make the stack "safely" aligned */
373 
374 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
375 		error = ENOMEM;
376 		goto bad;
377 	}
378 
379 	/* adjust "active stack depth" for process VSZ */
380 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
381 
382 	/*
383 	 * Prepare vmspace for remapping. Note that uvmspace_exec can replace
384 	 * p_vmspace!
385 	 */
386 	uvmspace_exec(p, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
387 
388 	vm = p->p_vmspace;
389 	/* Now map address space */
390 	vm->vm_taddr = (char *)pack.ep_taddr;
391 	vm->vm_tsize = btoc(pack.ep_tsize);
392 	vm->vm_daddr = (char *)pack.ep_daddr;
393 	vm->vm_dsize = btoc(pack.ep_dsize);
394 	vm->vm_ssize = btoc(pack.ep_ssize);
395 	vm->vm_maxsaddr = (char *)pack.ep_maxsaddr;
396 
397 	/* create the new process's VM space by running the vmcmds */
398 #ifdef DIAGNOSTIC
399 	if (pack.ep_vmcmds.evs_used == 0)
400 		panic("execve: no vmcmds");
401 #endif
402 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
403 		struct exec_vmcmd *vcp;
404 
405 		vcp = &pack.ep_vmcmds.evs_cmds[i];
406 		error = (*vcp->ev_proc)(p, vcp);
407 	}
408 
409 	/* free the vmspace-creation commands, and release their references */
410 	kill_vmcmds(&pack.ep_vmcmds);
411 
412 	/* if an error happened, deallocate and punt */
413 	if (error)
414 		goto exec_abort;
415 
416 	/* remember information about the process */
417 	arginfo.ps_nargvstr = argc;
418 	arginfo.ps_nenvstr = envc;
419 
420 #ifdef MACHINE_STACK_GROWS_UP
421 	stack = (char *)USRSTACK + sizeof(arginfo) + szsigcode;
422 	slen = len - sizeof(arginfo) - szsigcode;
423 #else
424 	stack = (char *)(USRSTACK - len);
425 #endif
426 	/* Now copy argc, args & environ to new stack */
427 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
428 		goto exec_abort;
429 
430 	/* copy out the process's ps_strings structure */
431 	if (copyout(&arginfo, (char *)PS_STRINGS, sizeof(arginfo)))
432 		goto exec_abort;
433 
434 	/* copy out the process's signal trampoline code */
435 #ifdef MACHINE_STACK_GROWS_UP
436 	if (szsigcode && copyout((char *)pack.ep_emul->e_sigcode,
437 	    ((char *)PS_STRINGS) + sizeof(arginfo), szsigcode))
438 		goto exec_abort;
439 #else
440 	if (szsigcode && copyout((char *)pack.ep_emul->e_sigcode,
441 	    ((char *)PS_STRINGS) - szsigcode, szsigcode))
442 		goto exec_abort;
443 #endif
444 
445 	stopprofclock(p);	/* stop profiling */
446 	fdcloseexec(p);		/* handle close on exec */
447 	execsigs(p);		/* reset catched signals */
448 
449 	/* set command name & other accounting info */
450 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
451 	bcopy(nid.ni_cnd.cn_nameptr, p->p_comm, len);
452 	p->p_comm[len] = 0;
453 	p->p_acflag &= ~AFORK;
454 
455 	/* record proc's vnode, for use by procfs and others */
456 	if (p->p_textvp)
457 		vrele(p->p_textvp);
458 	VREF(pack.ep_vp);
459 	p->p_textvp = pack.ep_vp;
460 
461 	p->p_flag |= P_EXEC;
462 	if (p->p_flag & P_PPWAIT) {
463 		p->p_flag &= ~P_PPWAIT;
464 		wakeup((caddr_t)p->p_pptr);
465 	}
466 
467 	/*
468 	 * If process does execve() while it has euid/uid or egid/gid
469 	 * which are mismatched, it remains P_SUGIDEXEC.
470 	 */
471 	if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
472 	    p->p_ucred->cr_gid == p->p_cred->p_rgid)
473 		p->p_flag &= ~P_SUGIDEXEC;
474 
475 	/*
476 	 * deal with set[ug]id.
477 	 * MNT_NOEXEC has already been used to disable s[ug]id.
478 	 */
479 	if ((attr.va_mode & (VSUID | VSGID)) && proc_cansugid(p)) {
480 		int i;
481 
482 		p->p_flag |= P_SUGID;
483 		p->p_flag |= P_SUGIDEXEC;
484 
485 #ifdef KTRACE
486 		/*
487 		 * If process is being ktraced, turn off - unless
488 		 * root set it.
489 		 */
490 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
491 			p->p_traceflag = 0;
492 			ktrsettracevnode(p, NULL);
493 		}
494 #endif
495 		p->p_ucred = crcopy(cred);
496 		if (attr.va_mode & VSUID)
497 			p->p_ucred->cr_uid = attr.va_uid;
498 		if (attr.va_mode & VSGID)
499 			p->p_ucred->cr_gid = attr.va_gid;
500 
501 		/*
502 		 * For set[ug]id processes, a few caveats apply to
503 		 * stdin, stdout, and stderr.
504 		 */
505 		for (i = 0; i < 3; i++) {
506 			struct file *fp = NULL;
507 
508 			/*
509 			 * NOTE - This will never return NULL because of
510 			 * unmature fds. The file descriptor table is not
511 			 * shared because we're suid.
512 			 */
513 			fp = fd_getfile(p->p_fd, i);
514 #ifdef PROCFS
515 			/*
516 			 * Close descriptors that are writing to procfs.
517 			 */
518 			if (fp && fp->f_type == DTYPE_VNODE &&
519 			    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS &&
520 			    (fp->f_flag & FWRITE)) {
521 				fdrelease(p, i);
522 				fp = NULL;
523 			}
524 #endif
525 
526 			/*
527 			 * Ensure that stdin, stdout, and stderr are already
528 			 * allocated.  We do not want userland to accidentally
529 			 * allocate descriptors in this range which has implied
530 			 * meaning to libc.
531 			 *
532 			 * XXX - Shouldn't the exec fail if we can't allocate
533 			 *       resources here?
534 			 */
535 			if (fp == NULL) {
536 				short flags = FREAD | (i == 0 ? 0 : FWRITE);
537 				struct vnode *vp;
538 				int indx;
539 
540 				if ((error = falloc(p, &fp, &indx)) != 0)
541 					break;
542 #ifdef DIAGNOSTIC
543 				if (indx != i)
544 					panic("sys_execve: falloc indx != i");
545 #endif
546 				if ((error = cdevvp(getnulldev(), &vp)) != 0) {
547 					ffree(fp);
548 					fdremove(p->p_fd, indx);
549 					break;
550 				}
551 				if ((error = VOP_OPEN(vp, flags, p->p_ucred, p)) != 0) {
552 					ffree(fp);
553 					fdremove(p->p_fd, indx);
554 					vrele(vp);
555 					break;
556 				}
557 				if (flags & FWRITE)
558 					vp->v_writecount++;
559 				fp->f_flag = flags;
560 				fp->f_type = DTYPE_VNODE;
561 				fp->f_ops = &vnops;
562 				fp->f_data = (caddr_t)vp;
563 				FILE_SET_MATURE(fp);
564 			}
565 		}
566 	} else
567 		p->p_flag &= ~P_SUGID;
568 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
569 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
570 
571 	if (p->p_flag & P_SUGIDEXEC) {
572 		int i, s = splclock();
573 
574 		timeout_del(&p->p_realit_to);
575 		timerclear(&p->p_realtimer.it_interval);
576 		timerclear(&p->p_realtimer.it_value);
577 		for (i = 0; i < sizeof(p->p_stats->p_timer) /
578 		    sizeof(p->p_stats->p_timer[0]); i++) {
579 			timerclear(&p->p_stats->p_timer[i].it_interval);
580 			timerclear(&p->p_stats->p_timer[i].it_value);
581 		}
582 		splx(s);
583 	}
584 
585 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
586 
587 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
588 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
589 	vput(pack.ep_vp);
590 
591 	/*
592 	 * notify others that we exec'd
593 	 */
594 	KNOTE(&p->p_klist, NOTE_EXEC);
595 
596 	/* setup new registers and do misc. setup. */
597 	if (pack.ep_emul->e_fixup != NULL) {
598 		if ((*pack.ep_emul->e_fixup)(p, &pack) != 0)
599 			goto free_pack_abort;
600 	}
601 #ifdef MACHINE_STACK_GROWS_UP
602 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack + slen, retval);
603 #else
604 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long)stack, retval);
605 #endif
606 
607 	if (p->p_flag & P_TRACED)
608 		psignal(p, SIGTRAP);
609 
610 	p->p_emul = pack.ep_emul;
611 	free(pack.ep_hdr, M_EXEC);
612 
613 #ifdef KTRACE
614 	if (KTRPOINT(p, KTR_EMUL))
615 		ktremul(p, p->p_emul->e_name);
616 #endif
617 	return (0);
618 
619 bad:
620 	/* free the vmspace-creation commands, and release their references */
621 	kill_vmcmds(&pack.ep_vmcmds);
622 	/* kill any opened file descriptor, if necessary */
623 	if (pack.ep_flags & EXEC_HASFD) {
624 		pack.ep_flags &= ~EXEC_HASFD;
625 		(void) fdrelease(p, pack.ep_fd);
626 	}
627 	/* close and put the exec'd file */
628 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
629 	vput(pack.ep_vp);
630 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
631 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
632 
633 freehdr:
634 	free(pack.ep_hdr, M_EXEC);
635 	return (error);
636 
637 exec_abort:
638 	/*
639 	 * the old process doesn't exist anymore.  exit gracefully.
640 	 * get rid of the (new) address space we have created, if any, get rid
641 	 * of our namei data and vnode, and exit noting failure
642 	 */
643 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
644 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
645 	if (pack.ep_emul_arg)
646 		FREE(pack.ep_emul_arg, M_TEMP);
647 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
648 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
649 	vput(pack.ep_vp);
650 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
651 
652 free_pack_abort:
653 	free(pack.ep_hdr, M_EXEC);
654 	exit1(p, W_EXITCODE(0, SIGABRT));
655 	exit1(p, -1);
656 
657 	/* NOTREACHED */
658 	return (0);
659 }
660 
661 
662 void *
663 copyargs(pack, arginfo, stack, argp)
664 	struct exec_package *pack;
665 	struct ps_strings *arginfo;
666 	void *stack;
667 	void *argp;
668 {
669 	char **cpp = stack;
670 	char *dp, *sp;
671 	size_t len;
672 	void *nullp = NULL;
673 	long argc = arginfo->ps_nargvstr;
674 	int envc = arginfo->ps_nenvstr;
675 
676 	if (copyout(&argc, cpp++, sizeof(argc)))
677 		return (NULL);
678 
679 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
680 	sp = argp;
681 
682 	/* XXX don't copy them out, remap them! */
683 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
684 
685 	for (; --argc >= 0; sp += len, dp += len)
686 		if (copyout(&dp, cpp++, sizeof(dp)) ||
687 		    copyoutstr(sp, dp, ARG_MAX, &len))
688 			return (NULL);
689 
690 	if (copyout(&nullp, cpp++, sizeof(nullp)))
691 		return (NULL);
692 
693 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
694 
695 	for (; --envc >= 0; sp += len, dp += len)
696 		if (copyout(&dp, cpp++, sizeof(dp)) ||
697 		    copyoutstr(sp, dp, ARG_MAX, &len))
698 			return (NULL);
699 
700 	if (copyout(&nullp, cpp++, sizeof(nullp)))
701 		return (NULL);
702 
703 	return (cpp);
704 }
705