xref: /freebsd/sys/kern/kern_exec.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
1 /*
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/mutex.h>
33 #include <sys/sysproto.h>
34 #include <sys/signalvar.h>
35 #include <sys/kernel.h>
36 #include <sys/mount.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
39 #include <sys/acct.h>
40 #include <sys/exec.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/wait.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/pioctl.h>
47 #include <sys/namei.h>
48 #include <sys/sysent.h>
49 #include <sys/shm.h>
50 #include <sys/sysctl.h>
51 #include <sys/user.h>
52 #include <sys/vnode.h>
53 
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_pager.h>
63 
64 #include <machine/reg.h>
65 
66 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
67 
68 static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback");
69 
70 /*
71  * callout list for things to do at exec time
72  */
73 struct execlist {
74 	execlist_fn function;
75 	TAILQ_ENTRY(execlist) next;
76 };
77 
78 TAILQ_HEAD(exec_list_head, execlist);
79 static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list);
80 
81 static register_t *exec_copyout_strings __P((struct image_params *));
82 
83 /* XXX This should be vm_size_t. */
84 static u_long ps_strings = PS_STRINGS;
85 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
86 
87 /* XXX This should be vm_size_t. */
88 static u_long usrstack = USRSTACK;
89 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
90 
91 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
92 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
93     &ps_arg_cache_limit, 0, "");
94 
95 int ps_argsopen = 1;
96 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
97 
98 /*
99  * Each of the items is a pointer to a `const struct execsw', hence the
100  * double pointer here.
101  */
102 static const struct execsw **execsw;
103 
104 #ifndef _SYS_SYSPROTO_H_
105 struct execve_args {
106         char    *fname;
107         char    **argv;
108         char    **envv;
109 };
110 #endif
111 
112 /*
113  * execve() system call.
114  *
115  * MPSAFE
116  */
117 int
118 execve(td, uap)
119 	struct thread *td;
120 	register struct execve_args *uap;
121 {
122 	struct proc *p = td->td_proc;
123 	struct nameidata nd, *ndp;
124 	struct ucred *newcred, *oldcred;
125 	register_t *stack_base;
126 	int error, len, i;
127 	struct image_params image_params, *imgp;
128 	struct vattr attr;
129 	int (*img_first) __P((struct image_params *));
130 	struct pargs *pa;
131 	struct execlist *ep;
132 
133 	imgp = &image_params;
134 
135 	/*
136 	 * Lock the process and set the P_INEXEC flag to indicate that
137 	 * it should be left alone until we're done here.  This is
138 	 * necessary to avoid race conditions - e.g. in ptrace() -
139 	 * that might allow a local user to illicitly obtain elevated
140 	 * privileges.
141 	 */
142 	mtx_lock(&Giant);
143 	PROC_LOCK(p);
144 	KASSERT((p->p_flag & P_INEXEC) == 0,
145 	    ("%s(): process already has P_INEXEC flag", __func__));
146 	p->p_flag |= P_INEXEC;
147 	PROC_UNLOCK(p);
148 
149 /* XXXKSE */
150 /* !!!!!!!! we need abort all the other threads of this process before we */
151 /* proceed beyond his point! */
152 
153 	/*
154 	 * Initialize part of the common data
155 	 */
156 	imgp->proc = p;
157 	imgp->uap = uap;
158 	imgp->attr = &attr;
159 	imgp->argc = imgp->envc = 0;
160 	imgp->argv0 = NULL;
161 	imgp->entry_addr = 0;
162 	imgp->vmspace_destroyed = 0;
163 	imgp->interpreted = 0;
164 	imgp->interpreter_name[0] = '\0';
165 	imgp->auxargs = NULL;
166 	imgp->vp = NULL;
167 	imgp->firstpage = NULL;
168 	imgp->ps_strings = 0;
169 	imgp->auxarg_size = 0;
170 
171 	/*
172 	 * Allocate temporary demand zeroed space for argument and
173 	 *	environment strings
174 	 */
175 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
176 	if (imgp->stringbase == NULL) {
177 		error = ENOMEM;
178 		goto exec_fail;
179 	}
180 	imgp->stringp = imgp->stringbase;
181 	imgp->stringspace = ARG_MAX;
182 	imgp->image_header = imgp->stringbase + ARG_MAX;
183 
184 	/*
185 	 * Translate the file name. namei() returns a vnode pointer
186 	 *	in ni_vp amoung other things.
187 	 */
188 	ndp = &nd;
189 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
190 	    UIO_USERSPACE, uap->fname, td);
191 
192 interpret:
193 
194 	error = namei(ndp);
195 	if (error) {
196 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
197 			ARG_MAX + PAGE_SIZE);
198 		goto exec_fail;
199 	}
200 
201 	imgp->vp = ndp->ni_vp;
202 	imgp->fname = uap->fname;
203 
204 	/*
205 	 * Check file permissions (also 'opens' file)
206 	 */
207 	error = exec_check_permissions(imgp);
208 	if (error) {
209 		VOP_UNLOCK(imgp->vp, 0, td);
210 		goto exec_fail_dealloc;
211 	}
212 
213 	error = exec_map_first_page(imgp);
214 	VOP_UNLOCK(imgp->vp, 0, td);
215 	if (error)
216 		goto exec_fail_dealloc;
217 
218 	/*
219 	 *	If the current process has a special image activator it
220 	 *	wants to try first, call it.   For example, emulating shell
221 	 *	scripts differently.
222 	 */
223 	error = -1;
224 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
225 		error = img_first(imgp);
226 
227 	/*
228 	 *	Loop through the list of image activators, calling each one.
229 	 *	An activator returns -1 if there is no match, 0 on success,
230 	 *	and an error otherwise.
231 	 */
232 	for (i = 0; error == -1 && execsw[i]; ++i) {
233 		if (execsw[i]->ex_imgact == NULL ||
234 		    execsw[i]->ex_imgact == img_first) {
235 			continue;
236 		}
237 		error = (*execsw[i]->ex_imgact)(imgp);
238 	}
239 
240 	if (error) {
241 		if (error == -1)
242 			error = ENOEXEC;
243 		goto exec_fail_dealloc;
244 	}
245 
246 	/*
247 	 * Special interpreter operation, cleanup and loop up to try to
248 	 * activate the interpreter.
249 	 */
250 	if (imgp->interpreted) {
251 		exec_unmap_first_page(imgp);
252 		/* free name buffer and old vnode */
253 		NDFREE(ndp, NDF_ONLY_PNBUF);
254 		vrele(ndp->ni_vp);
255 		/* set new name to that of the interpreter */
256 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
257 		    UIO_SYSSPACE, imgp->interpreter_name, td);
258 		goto interpret;
259 	}
260 
261 	TAILQ_FOREACH(ep, &exec_list, next)
262 		(*ep->function)(p);
263 
264 	/*
265 	 * Copy out strings (args and env) and initialize stack base
266 	 */
267 	stack_base = exec_copyout_strings(imgp);
268 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
269 
270 	/*
271 	 * If custom stack fixup routine present for this process
272 	 * let it do the stack setup.
273 	 * Else stuff argument count as first item on stack
274 	 */
275 	if (p->p_sysent->sv_fixup)
276 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
277 	else
278 		suword(--stack_base, imgp->argc);
279 
280 	/*
281 	 * For security and other reasons, the file descriptor table cannot
282 	 * be shared after an exec.
283 	 */
284 	if (p->p_fd->fd_refcnt > 1) {
285 		struct filedesc *tmp;
286 
287 		tmp = fdcopy(td);
288 		fdfree(td);
289 		p->p_fd = tmp;
290 	}
291 
292 	/*
293 	 * For security and other reasons, signal handlers cannot
294 	 * be shared after an exec. The new process gets a copy of the old
295 	 * handlers. In execsigs(), the new process will have its signals
296 	 * reset.
297 	 */
298 	if (p->p_procsig->ps_refcnt > 1) {
299 		struct procsig *newprocsig;
300 
301 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
302 		       M_SUBPROC, M_WAITOK);
303 		bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
304 		p->p_procsig->ps_refcnt--;
305 		p->p_procsig = newprocsig;
306 		p->p_procsig->ps_refcnt = 1;
307 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
308 			panic("shared procsig but private sigacts?");
309 
310 		p->p_uarea->u_sigacts = *p->p_sigacts;
311 		p->p_sigacts = &p->p_uarea->u_sigacts;
312 	}
313 	/* Stop profiling */
314 	stopprofclock(p);
315 
316 	/* close files on exec */
317 	fdcloseexec(td);
318 
319 	/* reset caught signals */
320 	execsigs(p);
321 
322 	/* name this process - nameiexec(p, ndp) */
323 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
324 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
325 	p->p_comm[len] = 0;
326 
327 	/*
328 	 * mark as execed, wakeup the process that vforked (if any) and tell
329 	 * it that it now has its own resources back
330 	 */
331 	PROC_LOCK(p);
332 	p->p_flag |= P_EXEC;
333 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
334 		p->p_flag &= ~P_PPWAIT;
335 		wakeup((caddr_t)p->p_pptr);
336 	}
337 
338 	/*
339 	 * Implement image setuid/setgid.
340 	 *
341 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
342 	 * the process is being traced.
343 	 */
344 	oldcred = p->p_ucred;
345 	newcred = NULL;
346 	if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) ||
347 	     ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) &&
348 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
349 	    (p->p_flag & P_TRACED) == 0) {
350 		PROC_UNLOCK(p);
351 		/*
352 		 * Turn off syscall tracing for set-id programs, except for
353 		 * root.  Record any set-id flags first to make sure that
354 		 * we do not regain any tracing during a possible block.
355 		 */
356 		setsugid(p);
357 		if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) {
358 			struct vnode *vtmp;
359 
360 			if ((vtmp = p->p_tracep) != NULL) {
361 				p->p_tracep = NULL;
362 				p->p_traceflag = 0;
363 				vrele(vtmp);
364 			}
365 		}
366 		/*
367 		 * Set the new credentials.
368 		 */
369 		newcred = crdup(oldcred);
370 		if (attr.va_mode & VSUID)
371 			change_euid(newcred, attr.va_uid);
372 		if (attr.va_mode & VSGID)
373 			change_egid(newcred, attr.va_gid);
374 		setugidsafety(td);
375 	} else {
376 		if (oldcred->cr_uid == oldcred->cr_ruid &&
377 		    oldcred->cr_gid == oldcred->cr_rgid)
378 			p->p_flag &= ~P_SUGID;
379 		PROC_UNLOCK(p);
380 	}
381 
382 	/*
383 	 * Implement correct POSIX saved-id behavior.
384 	 *
385 	 * XXX: It's not clear that the existing behavior is
386 	 * POSIX-compliant.  A number of sources indicate that the saved
387 	 * uid/gid should only be updated if the new ruid is not equal to
388 	 * the old ruid, or the new euid is not equal to the old euid and
389 	 * the new euid is not equal to the old ruid.  The FreeBSD code
390 	 * always updates the saved uid/gid.  Also, this code uses the new
391 	 * (replaced) euid and egid as the source, which may or may not be
392 	 * the right ones to use.
393 	 */
394 	if (newcred == NULL) {
395 		if (oldcred->cr_svuid != oldcred->cr_uid ||
396 		    oldcred->cr_svgid != oldcred->cr_gid) {
397 			newcred = crdup(oldcred);
398 			change_svuid(newcred, newcred->cr_uid);
399 			change_svgid(newcred, newcred->cr_gid);
400 		}
401 	} else {
402 		change_svuid(newcred, newcred->cr_uid);
403 		change_svgid(newcred, newcred->cr_gid);
404 	}
405 
406 	if (newcred != NULL) {
407 		PROC_LOCK(p);
408 		p->p_ucred = newcred;
409 		PROC_UNLOCK(p);
410 		crfree(oldcred);
411 	}
412 
413 	/*
414 	 * Store the vp for use in procfs
415 	 */
416 	if (p->p_textvp)		/* release old reference */
417 		vrele(p->p_textvp);
418 	VREF(ndp->ni_vp);
419 	p->p_textvp = ndp->ni_vp;
420 
421 	/*
422 	 * Notify others that we exec'd, and clear the P_INEXEC flag
423 	 * as we're now a bona fide freshly-execed process.
424 	 */
425 	PROC_LOCK(p);
426 	KNOTE(&p->p_klist, NOTE_EXEC);
427 	p->p_flag &= ~P_INEXEC;
428 
429 	/*
430 	 * If tracing the process, trap to debugger so breakpoints
431 	 * can be set before the program executes.
432 	 */
433 	_STOPEVENT(p, S_EXEC, 0);
434 
435 	if (p->p_flag & P_TRACED)
436 		psignal(p, SIGTRAP);
437 
438 	/* clear "fork but no exec" flag, as we _are_ execing */
439 	p->p_acflag &= ~AFORK;
440 
441 	/* Free any previous argument cache */
442 	pa = p->p_args;
443 	p->p_args = NULL;
444 	PROC_UNLOCK(p);
445 	if (pa != NULL && --pa->ar_ref == 0)
446 		FREE(pa, M_PARGS);
447 
448 	/* Set values passed into the program in registers. */
449 	setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
450 	    imgp->ps_strings);
451 
452 	/* Cache arguments if they fit inside our allowance */
453 	i = imgp->endargs - imgp->stringbase;
454 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
455 		MALLOC(pa, struct pargs *, sizeof(struct pargs) + i,
456 		    M_PARGS, M_WAITOK);
457 		pa->ar_ref = 1;
458 		pa->ar_length = i;
459 		bcopy(imgp->stringbase, pa->ar_args, i);
460 		PROC_LOCK(p);
461 		p->p_args = pa;
462 		PROC_UNLOCK(p);
463 	}
464 
465 exec_fail_dealloc:
466 
467 	/*
468 	 * free various allocated resources
469 	 */
470 	if (imgp->firstpage)
471 		exec_unmap_first_page(imgp);
472 
473 	if (imgp->stringbase != NULL)
474 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
475 			ARG_MAX + PAGE_SIZE);
476 
477 	if (imgp->vp) {
478 		NDFREE(ndp, NDF_ONLY_PNBUF);
479 		vrele(imgp->vp);
480 	}
481 
482 	if (error == 0)
483 		goto done2;
484 
485 exec_fail:
486 	/* we're done here, clear P_INEXEC */
487 	PROC_LOCK(p);
488 	p->p_flag &= ~P_INEXEC;
489 	PROC_UNLOCK(p);
490 
491 	if (imgp->vmspace_destroyed) {
492 		/* sorry, no more process anymore. exit gracefully */
493 		exit1(td, W_EXITCODE(0, SIGABRT));
494 		/* NOT REACHED */
495 		error = 0;
496 	}
497 done2:
498 	mtx_unlock(&Giant);
499 	return (error);
500 }
501 
502 int
503 exec_map_first_page(imgp)
504 	struct image_params *imgp;
505 {
506 	int rv, i;
507 	int initial_pagein;
508 	vm_page_t ma[VM_INITIAL_PAGEIN];
509 	vm_object_t object;
510 
511 	GIANT_REQUIRED;
512 
513 	if (imgp->firstpage) {
514 		exec_unmap_first_page(imgp);
515 	}
516 
517 	VOP_GETVOBJECT(imgp->vp, &object);
518 
519 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
520 
521 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
522 		initial_pagein = VM_INITIAL_PAGEIN;
523 		if (initial_pagein > object->size)
524 			initial_pagein = object->size;
525 		for (i = 1; i < initial_pagein; i++) {
526 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
527 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
528 					break;
529 				if (ma[i]->valid)
530 					break;
531 				vm_page_busy(ma[i]);
532 			} else {
533 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
534 				if (ma[i] == NULL)
535 					break;
536 			}
537 		}
538 		initial_pagein = i;
539 
540 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
541 		ma[0] = vm_page_lookup(object, 0);
542 
543 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
544 			if (ma[0]) {
545 				vm_page_protect(ma[0], VM_PROT_NONE);
546 				vm_page_free(ma[0]);
547 			}
548 			return EIO;
549 		}
550 	}
551 
552 	vm_page_wire(ma[0]);
553 	vm_page_wakeup(ma[0]);
554 
555 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
556 	imgp->firstpage = ma[0];
557 
558 	return 0;
559 }
560 
561 void
562 exec_unmap_first_page(imgp)
563 	struct image_params *imgp;
564 {
565 	GIANT_REQUIRED;
566 
567 	if (imgp->firstpage) {
568 		pmap_kremove((vm_offset_t) imgp->image_header);
569 		vm_page_unwire(imgp->firstpage, 1);
570 		imgp->firstpage = NULL;
571 	}
572 }
573 
574 /*
575  * Destroy old address space, and allocate a new stack
576  *	The new stack is only SGROWSIZ large because it is grown
577  *	automatically in trap.c.
578  */
579 int
580 exec_new_vmspace(imgp)
581 	struct image_params *imgp;
582 {
583 	int error;
584 	struct vmspace *vmspace = imgp->proc->p_vmspace;
585 	vm_offset_t stack_addr = USRSTACK - maxssiz;
586 	vm_map_t map = &vmspace->vm_map;
587 
588 	GIANT_REQUIRED;
589 
590 	imgp->vmspace_destroyed = 1;
591 
592 	/*
593 	 * Blow away entire process VM, if address space not shared,
594 	 * otherwise, create a new VM space so that other threads are
595 	 * not disrupted
596 	 */
597 	if (vmspace->vm_refcnt == 1) {
598 		if (vmspace->vm_shm)
599 			shmexit(imgp->proc);
600 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
601 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
602 	} else {
603 		vmspace_exec(imgp->proc);
604 		vmspace = imgp->proc->p_vmspace;
605 		map = &vmspace->vm_map;
606 	}
607 
608 	/* Allocate a new stack */
609 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
610 	    VM_PROT_ALL, VM_PROT_ALL, 0);
611 	if (error)
612 		return (error);
613 
614 #ifdef __ia64__
615 	{
616 		/*
617 		 * Allocate backing store. We really need something
618 		 * similar to vm_map_stack which can allow the backing
619 		 * store to grow upwards. This will do for now.
620 		 */
621 		vm_offset_t bsaddr;
622 		bsaddr = USRSTACK - 2*maxssiz;
623 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
624 				    4*PAGE_SIZE, 0,
625 				    VM_PROT_ALL, VM_PROT_ALL, 0);
626 		imgp->proc->p_thread.td_md.md_bspstore = bsaddr;
627 	}
628 #endif
629 
630 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
631 	 * VM_STACK case, but they are still used to monitor the size of the
632 	 * process stack so we can check the stack rlimit.
633 	 */
634 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
635 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
636 
637 	return(0);
638 }
639 
640 /*
641  * Copy out argument and environment strings from the old process
642  *	address space into the temporary string buffer.
643  */
644 int
645 exec_extract_strings(imgp)
646 	struct image_params *imgp;
647 {
648 	char	**argv, **envv;
649 	char	*argp, *envp;
650 	int	error;
651 	size_t	length;
652 
653 	/*
654 	 * extract arguments first
655 	 */
656 
657 	argv = imgp->uap->argv;
658 
659 	if (argv) {
660 		argp = (caddr_t) (intptr_t) fuword(argv);
661 		if (argp == (caddr_t) -1)
662 			return (EFAULT);
663 		if (argp)
664 			argv++;
665 		if (imgp->argv0)
666 			argp = imgp->argv0;
667 		if (argp) {
668 			do {
669 				if (argp == (caddr_t) -1)
670 					return (EFAULT);
671 				if ((error = copyinstr(argp, imgp->stringp,
672 				    imgp->stringspace, &length))) {
673 					if (error == ENAMETOOLONG)
674 						return(E2BIG);
675 					return (error);
676 				}
677 				imgp->stringspace -= length;
678 				imgp->stringp += length;
679 				imgp->argc++;
680 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
681 		}
682 	}
683 
684 	imgp->endargs = imgp->stringp;
685 
686 	/*
687 	 * extract environment strings
688 	 */
689 
690 	envv = imgp->uap->envv;
691 
692 	if (envv) {
693 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
694 			if (envp == (caddr_t) -1)
695 				return (EFAULT);
696 			if ((error = copyinstr(envp, imgp->stringp,
697 			    imgp->stringspace, &length))) {
698 				if (error == ENAMETOOLONG)
699 					return(E2BIG);
700 				return (error);
701 			}
702 			imgp->stringspace -= length;
703 			imgp->stringp += length;
704 			imgp->envc++;
705 		}
706 	}
707 
708 	return (0);
709 }
710 
711 /*
712  * Copy strings out to the new process address space, constructing
713  *	new arg and env vector tables. Return a pointer to the base
714  *	so that it can be used as the initial stack pointer.
715  */
716 register_t *
717 exec_copyout_strings(imgp)
718 	struct image_params *imgp;
719 {
720 	int argc, envc;
721 	char **vectp;
722 	char *stringp, *destp;
723 	register_t *stack_base;
724 	struct ps_strings *arginfo;
725 	int szsigcode;
726 
727 	/*
728 	 * Calculate string base and vector table pointers.
729 	 * Also deal with signal trampoline code for this exec type.
730 	 */
731 	arginfo = (struct ps_strings *)PS_STRINGS;
732 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
733 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
734 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
735 
736 	/*
737 	 * install sigcode
738 	 */
739 	if (szsigcode)
740 		copyout(imgp->proc->p_sysent->sv_sigcode,
741 			((caddr_t)arginfo - szsigcode), szsigcode);
742 
743 	/*
744 	 * If we have a valid auxargs ptr, prepare some room
745 	 * on the stack.
746 	 */
747 	if (imgp->auxargs) {
748 		/*
749 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
750 		 * lower compatibility.
751 		 */
752 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
753 			: (AT_COUNT * 2);
754 		/*
755 		 * The '+ 2' is for the null pointers at the end of each of
756 		 * the arg and env vector sets,and imgp->auxarg_size is room
757 		 * for argument of Runtime loader.
758 		 */
759 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
760 				       imgp->auxarg_size) * sizeof(char *));
761 
762 	} else
763 		/*
764 		 * The '+ 2' is for the null pointers at the end of each of
765 		 * the arg and env vector sets
766 		 */
767 		vectp = (char **)
768 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
769 
770 	/*
771 	 * vectp also becomes our initial stack base
772 	 */
773 	stack_base = (register_t *)vectp;
774 
775 	stringp = imgp->stringbase;
776 	argc = imgp->argc;
777 	envc = imgp->envc;
778 
779 	/*
780 	 * Copy out strings - arguments and environment.
781 	 */
782 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
783 
784 	/*
785 	 * Fill in "ps_strings" struct for ps, w, etc.
786 	 */
787 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
788 	suword(&arginfo->ps_nargvstr, argc);
789 
790 	/*
791 	 * Fill in argument portion of vector table.
792 	 */
793 	for (; argc > 0; --argc) {
794 		suword(vectp++, (long)(intptr_t)destp);
795 		while (*stringp++ != 0)
796 			destp++;
797 		destp++;
798 	}
799 
800 	/* a null vector table pointer separates the argp's from the envp's */
801 	suword(vectp++, 0);
802 
803 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
804 	suword(&arginfo->ps_nenvstr, envc);
805 
806 	/*
807 	 * Fill in environment portion of vector table.
808 	 */
809 	for (; envc > 0; --envc) {
810 		suword(vectp++, (long)(intptr_t)destp);
811 		while (*stringp++ != 0)
812 			destp++;
813 		destp++;
814 	}
815 
816 	/* end of vector table is a null pointer */
817 	suword(vectp, 0);
818 
819 	return (stack_base);
820 }
821 
822 /*
823  * Check permissions of file to execute.
824  *	Called with imgp->vp locked.
825  *	Return 0 for success or error code on failure.
826  */
827 int
828 exec_check_permissions(imgp)
829 	struct image_params *imgp;
830 {
831 	struct proc *p = imgp->proc;
832 	struct vnode *vp = imgp->vp;
833 	struct vattr *attr = imgp->attr;
834 	int error;
835 
836 	/* Get file attributes */
837 	error = VOP_GETATTR(vp, attr, p->p_ucred, curthread); /* XXXKSE */
838 	if (error)
839 		return (error);
840 
841 	/*
842 	 * 1) Check if file execution is disabled for the filesystem that this
843 	 *	file resides on.
844 	 * 2) Insure that at least one execute bit is on - otherwise root
845 	 *	will always succeed, and we don't want to happen unless the
846 	 *	file really is executable.
847 	 * 3) Insure that the file is a regular file.
848 	 */
849 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
850 	    ((attr->va_mode & 0111) == 0) ||
851 	    (attr->va_type != VREG)) {
852 		return (EACCES);
853 	}
854 
855 	/*
856 	 * Zero length files can't be exec'd
857 	 */
858 	if (attr->va_size == 0)
859 		return (ENOEXEC);
860 
861 	/*
862 	 *  Check for execute permission to file based on current credentials.
863 	 */
864 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, curthread); /* XXXKSE */
865 	if (error)
866 		return (error);
867 
868 	/*
869 	 * Check number of open-for-writes on the file and deny execution
870 	 * if there are any.
871 	 */
872 	if (vp->v_writecount)
873 		return (ETXTBSY);
874 
875 	/*
876 	 * Call filesystem specific open routine (which does nothing in the
877 	 * general case).
878 	 */
879 	error = VOP_OPEN(vp, FREAD, p->p_ucred, curthread); /* XXXKSE */
880 	if (error)
881 		return (error);
882 
883 	return (0);
884 }
885 
886 /*
887  * Exec handler registration
888  */
889 int
890 exec_register(execsw_arg)
891 	const struct execsw *execsw_arg;
892 {
893 	const struct execsw **es, **xs, **newexecsw;
894 	int count = 2;	/* New slot and trailing NULL */
895 
896 	if (execsw)
897 		for (es = execsw; *es; es++)
898 			count++;
899 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
900 	if (newexecsw == NULL)
901 		return ENOMEM;
902 	xs = newexecsw;
903 	if (execsw)
904 		for (es = execsw; *es; es++)
905 			*xs++ = *es;
906 	*xs++ = execsw_arg;
907 	*xs = NULL;
908 	if (execsw)
909 		free(execsw, M_TEMP);
910 	execsw = newexecsw;
911 	return 0;
912 }
913 
914 int
915 exec_unregister(execsw_arg)
916 	const struct execsw *execsw_arg;
917 {
918 	const struct execsw **es, **xs, **newexecsw;
919 	int count = 1;
920 
921 	if (execsw == NULL)
922 		panic("unregister with no handlers left?\n");
923 
924 	for (es = execsw; *es; es++) {
925 		if (*es == execsw_arg)
926 			break;
927 	}
928 	if (*es == NULL)
929 		return ENOENT;
930 	for (es = execsw; *es; es++)
931 		if (*es != execsw_arg)
932 			count++;
933 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
934 	if (newexecsw == NULL)
935 		return ENOMEM;
936 	xs = newexecsw;
937 	for (es = execsw; *es; es++)
938 		if (*es != execsw_arg)
939 			*xs++ = *es;
940 	*xs = NULL;
941 	if (execsw)
942 		free(execsw, M_TEMP);
943 	execsw = newexecsw;
944 	return 0;
945 }
946 
947 int
948 at_exec(function)
949 	execlist_fn function;
950 {
951 	struct execlist *ep;
952 
953 #ifdef INVARIANTS
954 	/* Be noisy if the programmer has lost track of things */
955 	if (rm_at_exec(function))
956 		printf("WARNING: exec callout entry (%p) already present\n",
957 		    function);
958 #endif
959 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
960 	if (ep == NULL)
961 		return (ENOMEM);
962 	ep->function = function;
963 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
964 	return (0);
965 }
966 
967 /*
968  * Scan the exec callout list for the given item and remove it.
969  * Returns the number of items removed (0 or 1)
970  */
971 int
972 rm_at_exec(function)
973 	execlist_fn function;
974 {
975 	struct execlist *ep;
976 
977 	TAILQ_FOREACH(ep, &exec_list, next) {
978 		if (ep->function == function) {
979 			TAILQ_REMOVE(&exec_list, ep, next);
980 			free(ep, M_ATEXEC);
981 			return(1);
982 		}
983 	}
984 	return (0);
985 }
986 
987