xref: /freebsd/sys/kern/kern_exec.c (revision 24b34f097bfe2b57f331ede1fbcfa2aa4888681d)
126f9a767SRodney W. Grimes /*
226f9a767SRodney W. Grimes  * Copyright (c) 1993, David Greenman
326f9a767SRodney W. Grimes  * All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  *
1426f9a767SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1726f9a767SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
25df8bae1dSRodney W. Grimes  *
2624b34f09SSujal Patel  *	$Id: kern_exec.c,v 1.38 1996/04/08 01:21:59 davidg Exp $
27df8bae1dSRodney W. Grimes  */
28df8bae1dSRodney W. Grimes 
29df8bae1dSRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/systm.h>
31d2d3e875SBruce Evans #include <sys/sysproto.h>
3226f9a767SRodney W. Grimes #include <sys/signalvar.h>
3326f9a767SRodney W. Grimes #include <sys/kernel.h>
3426f9a767SRodney W. Grimes #include <sys/mount.h>
35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
36079cc25bSDavid Greenman #include <sys/fcntl.h>
3726f9a767SRodney W. Grimes #include <sys/acct.h>
3826f9a767SRodney W. Grimes #include <sys/exec.h>
3926f9a767SRodney W. Grimes #include <sys/imgact.h>
40e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4126f9a767SRodney W. Grimes #include <sys/wait.h>
4226f9a767SRodney W. Grimes #include <sys/malloc.h>
431e1e0b44SSøren Schmidt #include <sys/sysent.h>
4426f9a767SRodney W. Grimes #include <sys/syslog.h>
45797f2d22SPoul-Henning Kamp #include <sys/shm.h>
4699ac3bc8SPeter Wemm #include <sys/sysctl.h>
4726f9a767SRodney W. Grimes 
4826f9a767SRodney W. Grimes #include <vm/vm.h>
49efeaf95aSDavid Greenman #include <vm/vm_param.h>
50efeaf95aSDavid Greenman #include <vm/vm_prot.h>
51efeaf95aSDavid Greenman #include <vm/lock.h>
52efeaf95aSDavid Greenman #include <vm/pmap.h>
53efeaf95aSDavid Greenman #include <vm/vm_map.h>
5426f9a767SRodney W. Grimes #include <vm/vm_kern.h>
55efeaf95aSDavid Greenman #include <vm/vm_extern.h>
5626f9a767SRodney W. Grimes 
5726f9a767SRodney W. Grimes #include <machine/reg.h>
5826f9a767SRodney W. Grimes 
5987b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *));
60df8bae1dSRodney W. Grimes 
61f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *);
62f23b4c91SGarrett Wollman 
63df8bae1dSRodney W. Grimes /*
6499ac3bc8SPeter Wemm  * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts
6599ac3bc8SPeter Wemm  * of the sysctl code also assumes this, and sizeof(int) == sizeof(long).
6699ac3bc8SPeter Wemm  */
6799ac3bc8SPeter Wemm static caddr_t ps_strings = (caddr_t)PS_STRINGS;
6899ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, "");
6999ac3bc8SPeter Wemm 
7099ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK;
7199ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, "");
7299ac3bc8SPeter Wemm 
7399ac3bc8SPeter Wemm /*
7426f9a767SRodney W. Grimes  * execsw_set is constructed for us by the linker.  Each of the items
7526f9a767SRodney W. Grimes  * is a pointer to a `const struct execsw', hence the double pointer here.
76df8bae1dSRodney W. Grimes  */
7787b6de2bSPoul-Henning Kamp static const struct execsw **execsw =
7887b6de2bSPoul-Henning Kamp 	(const struct execsw **)&execsw_set.ls_items[0];
7926f9a767SRodney W. Grimes 
80d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
81ad7507e2SSteven Wallace struct execve_args {
82ad7507e2SSteven Wallace         char    *fname;
83ad7507e2SSteven Wallace         char    **argv;
84ad7507e2SSteven Wallace         char    **envv;
85ad7507e2SSteven Wallace };
86d2d3e875SBruce Evans #endif
87ad7507e2SSteven Wallace 
8826f9a767SRodney W. Grimes /*
8926f9a767SRodney W. Grimes  * execve() system call.
9026f9a767SRodney W. Grimes  */
9126f9a767SRodney W. Grimes int
9226f9a767SRodney W. Grimes execve(p, uap, retval)
9326f9a767SRodney W. Grimes 	struct proc *p;
9426f9a767SRodney W. Grimes 	register struct execve_args *uap;
9526f9a767SRodney W. Grimes 	int *retval;
96df8bae1dSRodney W. Grimes {
9726f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
9826f9a767SRodney W. Grimes 	int *stack_base;
99bb56ec4aSPoul-Henning Kamp 	int error, len, i;
100c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10126f9a767SRodney W. Grimes 	struct vattr attr;
10226f9a767SRodney W. Grimes 
103c52007c2SDavid Greenman 	imgp = &image_params;
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	/*
106c52007c2SDavid Greenman 	 * Initialize part of the common data
107df8bae1dSRodney W. Grimes 	 */
108c52007c2SDavid Greenman 	imgp->proc = p;
109c52007c2SDavid Greenman 	imgp->uap = uap;
110c52007c2SDavid Greenman 	imgp->attr = &attr;
111c52007c2SDavid Greenman 	imgp->image_header = NULL;
112c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
113c52007c2SDavid Greenman 	imgp->entry_addr = 0;
114c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
115c52007c2SDavid Greenman 	imgp->interpreted = 0;
116c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
117e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
11826f9a767SRodney W. Grimes 
11926f9a767SRodney W. Grimes 	/*
12026f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
12126f9a767SRodney W. Grimes 	 *	environment strings
12226f9a767SRodney W. Grimes 	 */
123c2f9f36bSDavid Greenman 	imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX);
124c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
12526f9a767SRodney W. Grimes 		error = ENOMEM;
12626f9a767SRodney W. Grimes 		goto exec_fail;
12726f9a767SRodney W. Grimes 	}
128c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
129c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
13026f9a767SRodney W. Grimes 
13126f9a767SRodney W. Grimes 	/*
13226f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
13326f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
13426f9a767SRodney W. Grimes 	 */
13526f9a767SRodney W. Grimes 	ndp = &nd;
136b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1373ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
13826f9a767SRodney W. Grimes 
13926f9a767SRodney W. Grimes interpret:
14026f9a767SRodney W. Grimes 
14126f9a767SRodney W. Grimes 	error = namei(ndp);
14226f9a767SRodney W. Grimes 	if (error) {
143c2f9f36bSDavid Greenman 		kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
14426f9a767SRodney W. Grimes 		goto exec_fail;
14526f9a767SRodney W. Grimes 	}
14626f9a767SRodney W. Grimes 
147c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
148c52007c2SDavid Greenman 	if (imgp->vp == NULL) {
14926f9a767SRodney W. Grimes 		error = ENOEXEC;
15026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
15126f9a767SRodney W. Grimes 	}
15226f9a767SRodney W. Grimes 
15326f9a767SRodney W. Grimes 	/*
1549022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1559022e4adSDavid Greenman 	 */
156c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1579022e4adSDavid Greenman 
1589022e4adSDavid Greenman 	/*
1599022e4adSDavid Greenman 	 * Lose the lock on the vnode. It's no longer needed, and must not
160f5277ae7SDavid Greenman 	 * exist for the pagefault paging to work below.
161f5277ae7SDavid Greenman 	 */
162c52007c2SDavid Greenman 	VOP_UNLOCK(imgp->vp);
163f5277ae7SDavid Greenman 
16426f9a767SRodney W. Grimes 	if (error)
16526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
16626f9a767SRodney W. Grimes 
16726f9a767SRodney W. Grimes 	/*
16826f9a767SRodney W. Grimes 	 * Map the image header (first page) of the file into
16926f9a767SRodney W. Grimes 	 *	kernel address space
17026f9a767SRodney W. Grimes 	 */
17126f9a767SRodney W. Grimes 	error = vm_mmap(kernel_map,			/* map */
172c52007c2SDavid Greenman 			(vm_offset_t *)&imgp->image_header, /* address */
17326f9a767SRodney W. Grimes 			PAGE_SIZE,			/* size */
17426f9a767SRodney W. Grimes 			VM_PROT_READ, 			/* protection */
17526f9a767SRodney W. Grimes 			VM_PROT_READ, 			/* max protection */
17626f9a767SRodney W. Grimes 			0,	 			/* flags */
177c52007c2SDavid Greenman 			(caddr_t)imgp->vp,		/* vnode */
17826f9a767SRodney W. Grimes 			0);				/* offset */
17926f9a767SRodney W. Grimes 	if (error) {
18026f9a767SRodney W. Grimes 		uprintf("mmap failed: %d\n",error);
18126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
18226f9a767SRodney W. Grimes 	}
18326f9a767SRodney W. Grimes 
18426f9a767SRodney W. Grimes 	/*
18526f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
18626f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
18726f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
18826f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
18926f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
19026f9a767SRodney W. Grimes 	 *	the interpreter.
19126f9a767SRodney W. Grimes 	 */
19226f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
19326f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
194c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
19526f9a767SRodney W. Grimes 		else
19626f9a767SRodney W. Grimes 			continue;
19726f9a767SRodney W. Grimes 
19826f9a767SRodney W. Grimes 		if (error == -1)
19926f9a767SRodney W. Grimes 			continue;
20026f9a767SRodney W. Grimes 		if (error)
20126f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
202c52007c2SDavid Greenman 		if (imgp->interpreted) {
20326f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
204f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
20526f9a767SRodney W. Grimes 			FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
206c52007c2SDavid Greenman 			if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
207c52007c2SDavid Greenman 			    (vm_offset_t)imgp->image_header + PAGE_SIZE))
20826f9a767SRodney W. Grimes 				panic("execve: header dealloc failed (1)");
20926f9a767SRodney W. Grimes 
21026f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
211b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
212c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
21326f9a767SRodney W. Grimes 			goto interpret;
21426f9a767SRodney W. Grimes 		}
21526f9a767SRodney W. Grimes 		break;
21626f9a767SRodney W. Grimes 	}
21726f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
21826f9a767SRodney W. Grimes 	if (error == -1) {
21926f9a767SRodney W. Grimes 		error = ENOEXEC;
22026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
22126f9a767SRodney W. Grimes 	}
22226f9a767SRodney W. Grimes 
22326f9a767SRodney W. Grimes 	/*
22426f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
22526f9a767SRodney W. Grimes 	 */
226c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
22726f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
22826f9a767SRodney W. Grimes 
22926f9a767SRodney W. Grimes 	/*
2301e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2311e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2321e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
23326f9a767SRodney W. Grimes 	 */
2341e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
235c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2361e1e0b44SSøren Schmidt 	else
237c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
23826f9a767SRodney W. Grimes 
23926f9a767SRodney W. Grimes 	/* close files on exec */
24026f9a767SRodney W. Grimes 	fdcloseexec(p);
24126f9a767SRodney W. Grimes 
24226f9a767SRodney W. Grimes 	/* reset caught signals */
24326f9a767SRodney W. Grimes 	execsigs(p);
24426f9a767SRodney W. Grimes 
24526f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
24626f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
24726f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
24826f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
24926f9a767SRodney W. Grimes 
25026f9a767SRodney W. Grimes 	/*
25124b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
25226f9a767SRodney W. Grimes 	 * it that it now has it's own resources back
25326f9a767SRodney W. Grimes 	 */
25426f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
25526f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
25626f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
25726f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
25826f9a767SRodney W. Grimes 	}
25926f9a767SRodney W. Grimes 
26026f9a767SRodney W. Grimes 	/*
261c52007c2SDavid Greenman 	 * Implement image setuid/setgid. Disallow if the process is
262c52007c2SDavid Greenman 	 * being traced.
263c52007c2SDavid Greenman 	 */
264c52007c2SDavid Greenman 	if ((attr.va_mode & (VSUID | VSGID)) &&
265c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
266c52007c2SDavid Greenman 		/*
267c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
26826f9a767SRodney W. Grimes 		 * root.
26926f9a767SRodney W. Grimes 		 */
270c52007c2SDavid Greenman 		if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
27126f9a767SRodney W. Grimes 			p->p_traceflag = 0;
27226f9a767SRodney W. Grimes 			vrele(p->p_tracep);
273c52007c2SDavid Greenman 			p->p_tracep = NULL;
27426f9a767SRodney W. Grimes 		}
275c52007c2SDavid Greenman 		/*
276c52007c2SDavid Greenman 		 * Set the new credentials.
277c52007c2SDavid Greenman 		 */
27826f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
279c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
28026f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
281c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
28226f9a767SRodney W. Grimes 			p->p_ucred->cr_groups[0] = attr.va_gid;
28326f9a767SRodney W. Grimes 		p->p_flag |= P_SUGID;
284c52007c2SDavid Greenman 	} else {
285c52007c2SDavid Greenman 		p->p_flag &= ~P_SUGID;
28626f9a767SRodney W. Grimes 	}
28726f9a767SRodney W. Grimes 
28826f9a767SRodney W. Grimes 	/*
289c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
29026f9a767SRodney W. Grimes 	 */
29126f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
29226f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
29326f9a767SRodney W. Grimes 
29426f9a767SRodney W. Grimes 	/*
2952a531c80SDavid Greenman 	 * Store the vp for use in procfs
2962a531c80SDavid Greenman 	 */
2972a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
2982a531c80SDavid Greenman 		vrele(p->p_textvp);
2992a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3002a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3012a531c80SDavid Greenman 
3022a531c80SDavid Greenman 	/*
30326f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
30426f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
30526f9a767SRodney W. Grimes 	 */
30626f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
30726f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
30826f9a767SRodney W. Grimes 
30926f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
31026f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
31126f9a767SRodney W. Grimes 
31226f9a767SRodney W. Grimes 	/* Set entry address */
313c52007c2SDavid Greenman 	setregs(p, imgp->entry_addr, (u_long)stack_base);
31426f9a767SRodney W. Grimes 
31526f9a767SRodney W. Grimes 	/*
31626f9a767SRodney W. Grimes 	 * free various allocated resources
31726f9a767SRodney W. Grimes 	 */
318c2f9f36bSDavid Greenman 	kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
319c52007c2SDavid Greenman 	if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
320c52007c2SDavid Greenman 	    (vm_offset_t)imgp->image_header + PAGE_SIZE))
32126f9a767SRodney W. Grimes 		panic("execve: header dealloc failed (2)");
322f5277ae7SDavid Greenman 	vrele(ndp->ni_vp);
32326f9a767SRodney W. Grimes 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
32426f9a767SRodney W. Grimes 
32526f9a767SRodney W. Grimes 	return (0);
32626f9a767SRodney W. Grimes 
32726f9a767SRodney W. Grimes exec_fail_dealloc:
328c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
329c2f9f36bSDavid Greenman 		kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
330c52007c2SDavid Greenman 	if (imgp->image_header && imgp->image_header != (char *)-1)
331c52007c2SDavid Greenman 		if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
332c52007c2SDavid Greenman 		    (vm_offset_t)imgp->image_header + PAGE_SIZE))
33326f9a767SRodney W. Grimes 			panic("execve: header dealloc failed (3)");
334f5277ae7SDavid Greenman 	if (ndp->ni_vp)
335f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
33626f9a767SRodney W. Grimes 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
33726f9a767SRodney W. Grimes 
33826f9a767SRodney W. Grimes exec_fail:
339c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
34026f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
34126f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
34226f9a767SRodney W. Grimes 		/* NOT REACHED */
34326f9a767SRodney W. Grimes 		return(0);
34426f9a767SRodney W. Grimes 	} else {
34526f9a767SRodney W. Grimes 		return(error);
34626f9a767SRodney W. Grimes 	}
34726f9a767SRodney W. Grimes }
34826f9a767SRodney W. Grimes 
34926f9a767SRodney W. Grimes /*
35026f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
35126f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
35226f9a767SRodney W. Grimes  *	automatically in trap.c.
35326f9a767SRodney W. Grimes  */
35426f9a767SRodney W. Grimes int
355c52007c2SDavid Greenman exec_new_vmspace(imgp)
356c52007c2SDavid Greenman 	struct image_params *imgp;
35726f9a767SRodney W. Grimes {
35826f9a767SRodney W. Grimes 	int error;
359c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
36026f9a767SRodney W. Grimes 	caddr_t	stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
36126f9a767SRodney W. Grimes 
362c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
36326f9a767SRodney W. Grimes 
36426f9a767SRodney W. Grimes 	/* Blow away entire process VM */
3653d903220SDoug Rabson 	if (vmspace->vm_shm)
366c52007c2SDavid Greenman 		shmexit(imgp->proc);
36768940ac1SDavid Greenman 	vm_map_remove(&vmspace->vm_map, 0, USRSTACK);
36826f9a767SRodney W. Grimes 
36926f9a767SRodney W. Grimes 	/* Allocate a new stack */
37068940ac1SDavid Greenman 	error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr,
371bd7e5f99SJohn Dyson 	    SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
37226f9a767SRodney W. Grimes 	if (error)
37326f9a767SRodney W. Grimes 		return(error);
37426f9a767SRodney W. Grimes 
37526f9a767SRodney W. Grimes 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
37626f9a767SRodney W. Grimes 
37726f9a767SRodney W. Grimes 	/* Initialize maximum stack address */
37826f9a767SRodney W. Grimes 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
37926f9a767SRodney W. Grimes 
38026f9a767SRodney W. Grimes 	return(0);
38126f9a767SRodney W. Grimes }
38226f9a767SRodney W. Grimes 
38326f9a767SRodney W. Grimes /*
38426f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
38526f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
38626f9a767SRodney W. Grimes  */
38726f9a767SRodney W. Grimes int
388c52007c2SDavid Greenman exec_extract_strings(imgp)
389c52007c2SDavid Greenman 	struct image_params *imgp;
39026f9a767SRodney W. Grimes {
39126f9a767SRodney W. Grimes 	char	**argv, **envv;
39226f9a767SRodney W. Grimes 	char	*argp, *envp;
3930fd1a014SDavid Greenman 	int	error, length;
39426f9a767SRodney W. Grimes 
39526f9a767SRodney W. Grimes 	/*
39626f9a767SRodney W. Grimes 	 * extract arguments first
39726f9a767SRodney W. Grimes 	 */
39826f9a767SRodney W. Grimes 
399c52007c2SDavid Greenman 	argv = imgp->uap->argv;
40026f9a767SRodney W. Grimes 
4010fd1a014SDavid Greenman 	if (argv) {
402bb56ec4aSPoul-Henning Kamp 		while ((argp = (caddr_t) fuword(argv++))) {
40326f9a767SRodney W. Grimes 			if (argp == (caddr_t) -1)
40426f9a767SRodney W. Grimes 				return (EFAULT);
405c52007c2SDavid Greenman 			if ((error = copyinstr(argp, imgp->stringp,
406c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
4070fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
40826f9a767SRodney W. Grimes 					return(E2BIG);
4090fd1a014SDavid Greenman 				return (error);
4100fd1a014SDavid Greenman 			}
411c52007c2SDavid Greenman 			imgp->stringspace -= length;
412c52007c2SDavid Greenman 			imgp->stringp += length;
413c52007c2SDavid Greenman 			imgp->argc++;
41426f9a767SRodney W. Grimes 		}
4150fd1a014SDavid Greenman 	}
41626f9a767SRodney W. Grimes 
41726f9a767SRodney W. Grimes 	/*
41826f9a767SRodney W. Grimes 	 * extract environment strings
41926f9a767SRodney W. Grimes 	 */
42026f9a767SRodney W. Grimes 
421c52007c2SDavid Greenman 	envv = imgp->uap->envv;
42226f9a767SRodney W. Grimes 
4230fd1a014SDavid Greenman 	if (envv) {
424bb56ec4aSPoul-Henning Kamp 		while ((envp = (caddr_t) fuword(envv++))) {
42526f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
42626f9a767SRodney W. Grimes 				return (EFAULT);
427c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
428c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
4290fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
43026f9a767SRodney W. Grimes 					return(E2BIG);
4310fd1a014SDavid Greenman 				return (error);
4320fd1a014SDavid Greenman 			}
433c52007c2SDavid Greenman 			imgp->stringspace -= length;
434c52007c2SDavid Greenman 			imgp->stringp += length;
435c52007c2SDavid Greenman 			imgp->envc++;
43626f9a767SRodney W. Grimes 		}
4370fd1a014SDavid Greenman 	}
43826f9a767SRodney W. Grimes 
43926f9a767SRodney W. Grimes 	return (0);
44026f9a767SRodney W. Grimes }
44126f9a767SRodney W. Grimes 
44226f9a767SRodney W. Grimes /*
44326f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
44426f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
44526f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
44626f9a767SRodney W. Grimes  */
44726f9a767SRodney W. Grimes int *
448c52007c2SDavid Greenman exec_copyout_strings(imgp)
449c52007c2SDavid Greenman 	struct image_params *imgp;
45026f9a767SRodney W. Grimes {
45126f9a767SRodney W. Grimes 	int argc, envc;
45226f9a767SRodney W. Grimes 	char **vectp;
45326f9a767SRodney W. Grimes 	char *stringp, *destp;
45426f9a767SRodney W. Grimes 	int *stack_base;
45593f6448cSDavid Greenman 	struct ps_strings *arginfo;
456d66a5066SPeter Wemm 	int szsigcode;
45726f9a767SRodney W. Grimes 
45826f9a767SRodney W. Grimes 	/*
45926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
460d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
46126f9a767SRodney W. Grimes 	 */
46293f6448cSDavid Greenman 	arginfo = PS_STRINGS;
463d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
464d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
4651ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
4661ed012f9SPeter Wemm 
46726f9a767SRodney W. Grimes 	/*
468d66a5066SPeter Wemm 	 * install sigcode
469d66a5066SPeter Wemm 	 */
470d66a5066SPeter Wemm 	if (szsigcode)
471d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
472d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
473d66a5066SPeter Wemm 
474d66a5066SPeter Wemm 	/*
475e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
476e1743d02SSøren Schmidt 	 * on the stack.
477e1743d02SSøren Schmidt 	 */
478e1743d02SSøren Schmidt 	if (imgp->auxargs)
479e1743d02SSøren Schmidt 	/*
480e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
481e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
482e1743d02SSøren Schmidt 	 * ELF Auxargs data.
483e1743d02SSøren Schmidt 	 */
484e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
485e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
486e1743d02SSøren Schmidt 	else
487e1743d02SSøren Schmidt 	/*
48826f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
48926f9a767SRodney W. Grimes 	 * arg and env vector sets
49026f9a767SRodney W. Grimes 	 */
491e1743d02SSøren Schmidt 		vectp = (char **)
492e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
49326f9a767SRodney W. Grimes 
49426f9a767SRodney W. Grimes 	/*
49526f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
49626f9a767SRodney W. Grimes 	 */
49726f9a767SRodney W. Grimes 	stack_base = (int *)vectp;
49826f9a767SRodney W. Grimes 
499c52007c2SDavid Greenman 	stringp = imgp->stringbase;
500c52007c2SDavid Greenman 	argc = imgp->argc;
501c52007c2SDavid Greenman 	envc = imgp->envc;
50226f9a767SRodney W. Grimes 
50393f6448cSDavid Greenman 	/*
5043aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
50593f6448cSDavid Greenman 	 */
506c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
50793f6448cSDavid Greenman 
50893f6448cSDavid Greenman 	/*
5093aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
5103aed948bSDavid Greenman 	 */
5111ed012f9SPeter Wemm 	suword(&arginfo->ps_argvstr, (int)vectp);
5123aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
5133aed948bSDavid Greenman 
5143aed948bSDavid Greenman 	/*
5153aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
51693f6448cSDavid Greenman 	 */
51726f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
5183aed948bSDavid Greenman 		suword(vectp++, (int)destp);
5193aed948bSDavid Greenman 		while (*stringp++ != 0)
5203aed948bSDavid Greenman 			destp++;
5213aed948bSDavid Greenman 		destp++;
52226f9a767SRodney W. Grimes 	}
52326f9a767SRodney W. Grimes 
52426f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
5253aed948bSDavid Greenman 	suword(vectp++, NULL);
52626f9a767SRodney W. Grimes 
5271ed012f9SPeter Wemm 	suword(&arginfo->ps_envstr, (int)vectp);
5283aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
52993f6448cSDavid Greenman 
53093f6448cSDavid Greenman 	/*
5313aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
53293f6448cSDavid Greenman 	 */
53326f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
5343aed948bSDavid Greenman 		suword(vectp++, (int)destp);
5353aed948bSDavid Greenman 		while (*stringp++ != 0)
5363aed948bSDavid Greenman 			destp++;
5373aed948bSDavid Greenman 		destp++;
53826f9a767SRodney W. Grimes 	}
53926f9a767SRodney W. Grimes 
54026f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
5413aed948bSDavid Greenman 	suword(vectp, NULL);
54226f9a767SRodney W. Grimes 
54326f9a767SRodney W. Grimes 	return (stack_base);
54426f9a767SRodney W. Grimes }
54526f9a767SRodney W. Grimes 
54626f9a767SRodney W. Grimes /*
54726f9a767SRodney W. Grimes  * Check permissions of file to execute.
54826f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
54926f9a767SRodney W. Grimes  */
550f23b4c91SGarrett Wollman static int
551c52007c2SDavid Greenman exec_check_permissions(imgp)
552c52007c2SDavid Greenman 	struct image_params *imgp;
55326f9a767SRodney W. Grimes {
554c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
555c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
556c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
55726f9a767SRodney W. Grimes 	int error;
55826f9a767SRodney W. Grimes 
55926f9a767SRodney W. Grimes 	/*
56026f9a767SRodney W. Grimes 	 * Check number of open-for-writes on the file and deny execution
56126f9a767SRodney W. Grimes 	 *	if there are any.
56226f9a767SRodney W. Grimes 	 */
563c52007c2SDavid Greenman 	if (vp->v_writecount) {
56426f9a767SRodney W. Grimes 		return (ETXTBSY);
56526f9a767SRodney W. Grimes 	}
56626f9a767SRodney W. Grimes 
56726f9a767SRodney W. Grimes 	/* Get file attributes */
568c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
56926f9a767SRodney W. Grimes 	if (error)
57026f9a767SRodney W. Grimes 		return (error);
57126f9a767SRodney W. Grimes 
57226f9a767SRodney W. Grimes 	/*
57326f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
57426f9a767SRodney W. Grimes 	 *	file resides on.
57526f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
57626f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
57726f9a767SRodney W. Grimes 	 *	file really is executable.
57826f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
57926f9a767SRodney W. Grimes 	 */
580c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
58126f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
58226f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
58326f9a767SRodney W. Grimes 		return (EACCES);
58426f9a767SRodney W. Grimes 	}
58526f9a767SRodney W. Grimes 
58626f9a767SRodney W. Grimes 	/*
58726f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
58826f9a767SRodney W. Grimes 	 */
58926f9a767SRodney W. Grimes 	if (attr->va_size == 0)
59026f9a767SRodney W. Grimes 		return (ENOEXEC);
59126f9a767SRodney W. Grimes 
59226f9a767SRodney W. Grimes 	/*
59326f9a767SRodney W. Grimes 	 * Disable setuid/setgid if the filesystem prohibits it or if
59426f9a767SRodney W. Grimes 	 *	the process is being traced.
59526f9a767SRodney W. Grimes 	 */
596c52007c2SDavid Greenman         if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
59726f9a767SRodney W. Grimes 		attr->va_mode &= ~(VSUID | VSGID);
59826f9a767SRodney W. Grimes 
59926f9a767SRodney W. Grimes 	/*
60026f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
60126f9a767SRodney W. Grimes 	 *	Then call filesystem specific open routine (which does nothing
60226f9a767SRodney W. Grimes 	 *	in the general case).
60326f9a767SRodney W. Grimes 	 */
604c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
60526f9a767SRodney W. Grimes 	if (error)
60626f9a767SRodney W. Grimes 		return (error);
60726f9a767SRodney W. Grimes 
608c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
60926f9a767SRodney W. Grimes 	if (error)
61026f9a767SRodney W. Grimes 		return (error);
61126f9a767SRodney W. Grimes 
61226f9a767SRodney W. Grimes 	return (0);
613df8bae1dSRodney W. Grimes }
614