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