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