xref: /freebsd/sys/kern/kern_exec.c (revision efeaf95a41820e4eb661a90cfb59a26e36575784)
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
1526f9a767SRodney W. Grimes  *	This product includes software developed by David Greenman
1626f9a767SRodney W. Grimes  * 4. The name of the developer may not be used to endorse or promote products
1726f9a767SRodney W. Grimes  *    derived from this software without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
1926f9a767SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2226f9a767SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31efeaf95aSDavid Greenman  *	$Id: kern_exec.c,v 1.27 1995/11/13 10:45:22 davidg Exp $
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34df8bae1dSRodney W. Grimes #include <sys/param.h>
3526f9a767SRodney W. Grimes #include <sys/systm.h>
36d2d3e875SBruce Evans #include <sys/sysproto.h>
3726f9a767SRodney W. Grimes #include <sys/signalvar.h>
3826f9a767SRodney W. Grimes #include <sys/kernel.h>
3926f9a767SRodney W. Grimes #include <sys/mount.h>
40797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
41079cc25bSDavid Greenman #include <sys/fcntl.h>
4226f9a767SRodney W. Grimes #include <sys/acct.h>
4326f9a767SRodney W. Grimes #include <sys/exec.h>
4426f9a767SRodney W. Grimes #include <sys/imgact.h>
4526f9a767SRodney W. Grimes #include <sys/wait.h>
4626f9a767SRodney W. Grimes #include <sys/malloc.h>
471e1e0b44SSøren Schmidt #include <sys/sysent.h>
4826f9a767SRodney W. Grimes #include <sys/syslog.h>
49797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5026f9a767SRodney W. Grimes 
5126f9a767SRodney W. Grimes #include <vm/vm.h>
52efeaf95aSDavid Greenman #include <vm/vm_param.h>
53efeaf95aSDavid Greenman #include <vm/vm_prot.h>
54efeaf95aSDavid Greenman #include <vm/lock.h>
55efeaf95aSDavid Greenman #include <vm/pmap.h>
56efeaf95aSDavid Greenman #include <vm/vm_map.h>
5726f9a767SRodney W. Grimes #include <vm/vm_kern.h>
58efeaf95aSDavid Greenman #include <vm/vm_extern.h>
5926f9a767SRodney W. Grimes 
6026f9a767SRodney W. Grimes #include <machine/reg.h>
6126f9a767SRodney W. Grimes 
6226f9a767SRodney W. Grimes int *exec_copyout_strings __P((struct image_params *));
63df8bae1dSRodney W. Grimes 
64f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *);
65f23b4c91SGarrett Wollman 
66df8bae1dSRodney W. Grimes /*
6726f9a767SRodney W. Grimes  * execsw_set is constructed for us by the linker.  Each of the items
6826f9a767SRodney W. Grimes  * is a pointer to a `const struct execsw', hence the double pointer here.
69df8bae1dSRodney W. Grimes  */
7026f9a767SRodney W. Grimes const struct execsw **execsw = (const struct execsw **)&execsw_set.ls_items[0];
7126f9a767SRodney W. Grimes 
72d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
73ad7507e2SSteven Wallace struct execve_args {
74ad7507e2SSteven Wallace         char    *fname;
75ad7507e2SSteven Wallace         char    **argv;
76ad7507e2SSteven Wallace         char    **envv;
77ad7507e2SSteven Wallace };
78d2d3e875SBruce Evans #endif
79ad7507e2SSteven Wallace 
8026f9a767SRodney W. Grimes /*
8126f9a767SRodney W. Grimes  * execve() system call.
8226f9a767SRodney W. Grimes  */
8326f9a767SRodney W. Grimes int
8426f9a767SRodney W. Grimes execve(p, uap, retval)
8526f9a767SRodney W. Grimes 	struct proc *p;
8626f9a767SRodney W. Grimes 	register struct execve_args *uap;
8726f9a767SRodney W. Grimes 	int *retval;
88df8bae1dSRodney W. Grimes {
8926f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
9026f9a767SRodney W. Grimes 	int *stack_base;
91bb56ec4aSPoul-Henning Kamp 	int error, len, i;
92c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
9326f9a767SRodney W. Grimes 	struct vattr attr;
9426f9a767SRodney W. Grimes 
95c52007c2SDavid Greenman 	imgp = &image_params;
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes 	/*
98c52007c2SDavid Greenman 	 * Initialize part of the common data
99df8bae1dSRodney W. Grimes 	 */
100c52007c2SDavid Greenman 	imgp->proc = p;
101c52007c2SDavid Greenman 	imgp->uap = uap;
102c52007c2SDavid Greenman 	imgp->attr = &attr;
103c52007c2SDavid Greenman 	imgp->image_header = NULL;
104c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
105c52007c2SDavid Greenman 	imgp->entry_addr = 0;
106c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
107c52007c2SDavid Greenman 	imgp->interpreted = 0;
108c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
10926f9a767SRodney W. Grimes 
11026f9a767SRodney W. Grimes 	/*
11126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
11226f9a767SRodney W. Grimes 	 *	environment strings
11326f9a767SRodney W. Grimes 	 */
114c2f9f36bSDavid Greenman 	imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX);
115c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
11626f9a767SRodney W. Grimes 		error = ENOMEM;
11726f9a767SRodney W. Grimes 		goto exec_fail;
11826f9a767SRodney W. Grimes 	}
119c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
120c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
12126f9a767SRodney W. Grimes 
12226f9a767SRodney W. Grimes 	/*
12326f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
12426f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
12526f9a767SRodney W. Grimes 	 */
12626f9a767SRodney W. Grimes 	ndp = &nd;
127b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1283ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
12926f9a767SRodney W. Grimes 
13026f9a767SRodney W. Grimes interpret:
13126f9a767SRodney W. Grimes 
13226f9a767SRodney W. Grimes 	error = namei(ndp);
13326f9a767SRodney W. Grimes 	if (error) {
134c2f9f36bSDavid Greenman 		kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
13526f9a767SRodney W. Grimes 		goto exec_fail;
13626f9a767SRodney W. Grimes 	}
13726f9a767SRodney W. Grimes 
138c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
139c52007c2SDavid Greenman 	if (imgp->vp == NULL) {
14026f9a767SRodney W. Grimes 		error = ENOEXEC;
14126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
14226f9a767SRodney W. Grimes 	}
14326f9a767SRodney W. Grimes 
14426f9a767SRodney W. Grimes 	/*
1459022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1469022e4adSDavid Greenman 	 */
147c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1489022e4adSDavid Greenman 
1499022e4adSDavid Greenman 	/*
1509022e4adSDavid Greenman 	 * Lose the lock on the vnode. It's no longer needed, and must not
151f5277ae7SDavid Greenman 	 * exist for the pagefault paging to work below.
152f5277ae7SDavid Greenman 	 */
153c52007c2SDavid Greenman 	VOP_UNLOCK(imgp->vp);
154f5277ae7SDavid Greenman 
15526f9a767SRodney W. Grimes 	if (error)
15626f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
15726f9a767SRodney W. Grimes 
15826f9a767SRodney W. Grimes 	/*
15926f9a767SRodney W. Grimes 	 * Map the image header (first page) of the file into
16026f9a767SRodney W. Grimes 	 *	kernel address space
16126f9a767SRodney W. Grimes 	 */
16226f9a767SRodney W. Grimes 	error = vm_mmap(kernel_map,			/* map */
163c52007c2SDavid Greenman 			(vm_offset_t *)&imgp->image_header, /* address */
16426f9a767SRodney W. Grimes 			PAGE_SIZE,			/* size */
16526f9a767SRodney W. Grimes 			VM_PROT_READ, 			/* protection */
16626f9a767SRodney W. Grimes 			VM_PROT_READ, 			/* max protection */
16726f9a767SRodney W. Grimes 			0,	 			/* flags */
168c52007c2SDavid Greenman 			(caddr_t)imgp->vp,		/* vnode */
16926f9a767SRodney W. Grimes 			0);				/* offset */
17026f9a767SRodney W. Grimes 	if (error) {
17126f9a767SRodney W. Grimes 		uprintf("mmap failed: %d\n",error);
17226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
17326f9a767SRodney W. Grimes 	}
17426f9a767SRodney W. Grimes 
17526f9a767SRodney W. Grimes 	/*
17626f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
17726f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
17826f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
17926f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
18026f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
18126f9a767SRodney W. Grimes 	 *	the interpreter.
18226f9a767SRodney W. Grimes 	 */
18326f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
18426f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
185c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
18626f9a767SRodney W. Grimes 		else
18726f9a767SRodney W. Grimes 			continue;
18826f9a767SRodney W. Grimes 
18926f9a767SRodney W. Grimes 		if (error == -1)
19026f9a767SRodney W. Grimes 			continue;
19126f9a767SRodney W. Grimes 		if (error)
19226f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
193c52007c2SDavid Greenman 		if (imgp->interpreted) {
19426f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
195f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
19626f9a767SRodney W. Grimes 			FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
197c52007c2SDavid Greenman 			if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
198c52007c2SDavid Greenman 			    (vm_offset_t)imgp->image_header + PAGE_SIZE))
19926f9a767SRodney W. Grimes 				panic("execve: header dealloc failed (1)");
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
202b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
203c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
20426f9a767SRodney W. Grimes 			goto interpret;
20526f9a767SRodney W. Grimes 		}
20626f9a767SRodney W. Grimes 		break;
20726f9a767SRodney W. Grimes 	}
20826f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
20926f9a767SRodney W. Grimes 	if (error == -1) {
21026f9a767SRodney W. Grimes 		error = ENOEXEC;
21126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
21226f9a767SRodney W. Grimes 	}
21326f9a767SRodney W. Grimes 
21426f9a767SRodney W. Grimes 	/*
21526f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
21626f9a767SRodney W. Grimes 	 */
217c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
21826f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
21926f9a767SRodney W. Grimes 
22026f9a767SRodney W. Grimes 	/*
2211e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2221e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2231e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
22426f9a767SRodney W. Grimes 	 */
2251e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
226c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2271e1e0b44SSøren Schmidt 	else
228c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
22926f9a767SRodney W. Grimes 
23026f9a767SRodney W. Grimes 	/* close files on exec */
23126f9a767SRodney W. Grimes 	fdcloseexec(p);
23226f9a767SRodney W. Grimes 
23326f9a767SRodney W. Grimes 	/* reset caught signals */
23426f9a767SRodney W. Grimes 	execsigs(p);
23526f9a767SRodney W. Grimes 
23626f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
23726f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
23826f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
23926f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
24026f9a767SRodney W. Grimes 
24126f9a767SRodney W. Grimes 	/*
24226f9a767SRodney W. Grimes 	 * mark as executable, wakeup any process that was vforked and tell
24326f9a767SRodney W. Grimes 	 * it that it now has it's own resources back
24426f9a767SRodney W. Grimes 	 */
24526f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
24626f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
24726f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
24826f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
24926f9a767SRodney W. Grimes 	}
25026f9a767SRodney W. Grimes 
25126f9a767SRodney W. Grimes 	/*
252c52007c2SDavid Greenman 	 * Implement image setuid/setgid. Disallow if the process is
253c52007c2SDavid Greenman 	 * being traced.
254c52007c2SDavid Greenman 	 */
255c52007c2SDavid Greenman 	if ((attr.va_mode & (VSUID | VSGID)) &&
256c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
257c52007c2SDavid Greenman 		/*
258c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
25926f9a767SRodney W. Grimes 		 * root.
26026f9a767SRodney W. Grimes 		 */
261c52007c2SDavid Greenman 		if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
26226f9a767SRodney W. Grimes 			p->p_traceflag = 0;
26326f9a767SRodney W. Grimes 			vrele(p->p_tracep);
264c52007c2SDavid Greenman 			p->p_tracep = NULL;
26526f9a767SRodney W. Grimes 		}
266c52007c2SDavid Greenman 		/*
267c52007c2SDavid Greenman 		 * Set the new credentials.
268c52007c2SDavid Greenman 		 */
26926f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
270c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
27126f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
272c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
27326f9a767SRodney W. Grimes 			p->p_ucred->cr_groups[0] = attr.va_gid;
27426f9a767SRodney W. Grimes 		p->p_flag |= P_SUGID;
275c52007c2SDavid Greenman 	} else {
276c52007c2SDavid Greenman 		p->p_flag &= ~P_SUGID;
27726f9a767SRodney W. Grimes 	}
27826f9a767SRodney W. Grimes 
27926f9a767SRodney W. Grimes 	/*
280c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
28126f9a767SRodney W. Grimes 	 */
28226f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
28326f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
28426f9a767SRodney W. Grimes 
28526f9a767SRodney W. Grimes 	/*
2862a531c80SDavid Greenman 	 * Store the vp for use in procfs
2872a531c80SDavid Greenman 	 */
2882a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
2892a531c80SDavid Greenman 		vrele(p->p_textvp);
2902a531c80SDavid Greenman 	VREF(ndp->ni_vp);
2912a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
2922a531c80SDavid Greenman 
2932a531c80SDavid Greenman 	/*
29426f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
29526f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
29626f9a767SRodney W. Grimes 	 */
29726f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
29826f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
29926f9a767SRodney W. Grimes 
30026f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
30126f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
30226f9a767SRodney W. Grimes 
30326f9a767SRodney W. Grimes 	/* Set entry address */
304c52007c2SDavid Greenman 	setregs(p, imgp->entry_addr, (u_long)stack_base);
30526f9a767SRodney W. Grimes 
30626f9a767SRodney W. Grimes 	/*
30726f9a767SRodney W. Grimes 	 * free various allocated resources
30826f9a767SRodney W. Grimes 	 */
309c2f9f36bSDavid Greenman 	kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
310c52007c2SDavid Greenman 	if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
311c52007c2SDavid Greenman 	    (vm_offset_t)imgp->image_header + PAGE_SIZE))
31226f9a767SRodney W. Grimes 		panic("execve: header dealloc failed (2)");
313f5277ae7SDavid Greenman 	vrele(ndp->ni_vp);
31426f9a767SRodney W. Grimes 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
31526f9a767SRodney W. Grimes 
31626f9a767SRodney W. Grimes 	return (0);
31726f9a767SRodney W. Grimes 
31826f9a767SRodney W. Grimes exec_fail_dealloc:
319c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
320c2f9f36bSDavid Greenman 		kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
321c52007c2SDavid Greenman 	if (imgp->image_header && imgp->image_header != (char *)-1)
322c52007c2SDavid Greenman 		if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
323c52007c2SDavid Greenman 		    (vm_offset_t)imgp->image_header + PAGE_SIZE))
32426f9a767SRodney W. Grimes 			panic("execve: header dealloc failed (3)");
325f5277ae7SDavid Greenman 	if (ndp->ni_vp)
326f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
32726f9a767SRodney W. Grimes 	FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
32826f9a767SRodney W. Grimes 
32926f9a767SRodney W. Grimes exec_fail:
330c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
33126f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
33226f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
33326f9a767SRodney W. Grimes 		/* NOT REACHED */
33426f9a767SRodney W. Grimes 		return(0);
33526f9a767SRodney W. Grimes 	} else {
33626f9a767SRodney W. Grimes 		return(error);
33726f9a767SRodney W. Grimes 	}
33826f9a767SRodney W. Grimes }
33926f9a767SRodney W. Grimes 
34026f9a767SRodney W. Grimes /*
34126f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
34226f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
34326f9a767SRodney W. Grimes  *	automatically in trap.c.
34426f9a767SRodney W. Grimes  */
34526f9a767SRodney W. Grimes int
346c52007c2SDavid Greenman exec_new_vmspace(imgp)
347c52007c2SDavid Greenman 	struct image_params *imgp;
34826f9a767SRodney W. Grimes {
34926f9a767SRodney W. Grimes 	int error;
350c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
35126f9a767SRodney W. Grimes 	caddr_t	stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
35226f9a767SRodney W. Grimes 
353c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
35426f9a767SRodney W. Grimes 
35526f9a767SRodney W. Grimes 	/* Blow away entire process VM */
356d74643dfSDavid Greenman #ifdef SYSVSHM
3573d903220SDoug Rabson 	if (vmspace->vm_shm)
358c52007c2SDavid Greenman 		shmexit(imgp->proc);
359d74643dfSDavid Greenman #endif
36068940ac1SDavid Greenman 	vm_map_remove(&vmspace->vm_map, 0, USRSTACK);
36126f9a767SRodney W. Grimes 
36226f9a767SRodney W. Grimes 	/* Allocate a new stack */
36368940ac1SDavid Greenman 	error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr,
36426f9a767SRodney W. Grimes 	    SGROWSIZ, FALSE);
36526f9a767SRodney W. Grimes 	if (error)
36626f9a767SRodney W. Grimes 		return(error);
36726f9a767SRodney W. Grimes 
36826f9a767SRodney W. Grimes 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
36926f9a767SRodney W. Grimes 
37026f9a767SRodney W. Grimes 	/* Initialize maximum stack address */
37126f9a767SRodney W. Grimes 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
37226f9a767SRodney W. Grimes 
37326f9a767SRodney W. Grimes 	return(0);
37426f9a767SRodney W. Grimes }
37526f9a767SRodney W. Grimes 
37626f9a767SRodney W. Grimes /*
37726f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
37826f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
37926f9a767SRodney W. Grimes  */
38026f9a767SRodney W. Grimes int
381c52007c2SDavid Greenman exec_extract_strings(imgp)
382c52007c2SDavid Greenman 	struct image_params *imgp;
38326f9a767SRodney W. Grimes {
38426f9a767SRodney W. Grimes 	char	**argv, **envv;
38526f9a767SRodney W. Grimes 	char	*argp, *envp;
3860fd1a014SDavid Greenman 	int	error, length;
38726f9a767SRodney W. Grimes 
38826f9a767SRodney W. Grimes 	/*
38926f9a767SRodney W. Grimes 	 * extract arguments first
39026f9a767SRodney W. Grimes 	 */
39126f9a767SRodney W. Grimes 
392c52007c2SDavid Greenman 	argv = imgp->uap->argv;
39326f9a767SRodney W. Grimes 
3940fd1a014SDavid Greenman 	if (argv) {
395bb56ec4aSPoul-Henning Kamp 		while ((argp = (caddr_t) fuword(argv++))) {
39626f9a767SRodney W. Grimes 			if (argp == (caddr_t) -1)
39726f9a767SRodney W. Grimes 				return (EFAULT);
398c52007c2SDavid Greenman 			if ((error = copyinstr(argp, imgp->stringp,
399c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
4000fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
40126f9a767SRodney W. Grimes 					return(E2BIG);
4020fd1a014SDavid Greenman 				return (error);
4030fd1a014SDavid Greenman 			}
404c52007c2SDavid Greenman 			imgp->stringspace -= length;
405c52007c2SDavid Greenman 			imgp->stringp += length;
406c52007c2SDavid Greenman 			imgp->argc++;
40726f9a767SRodney W. Grimes 		}
4080fd1a014SDavid Greenman 	}
40926f9a767SRodney W. Grimes 
41026f9a767SRodney W. Grimes 	/*
41126f9a767SRodney W. Grimes 	 * extract environment strings
41226f9a767SRodney W. Grimes 	 */
41326f9a767SRodney W. Grimes 
414c52007c2SDavid Greenman 	envv = imgp->uap->envv;
41526f9a767SRodney W. Grimes 
4160fd1a014SDavid Greenman 	if (envv) {
417bb56ec4aSPoul-Henning Kamp 		while ((envp = (caddr_t) fuword(envv++))) {
41826f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
41926f9a767SRodney W. Grimes 				return (EFAULT);
420c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
421c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
4220fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
42326f9a767SRodney W. Grimes 					return(E2BIG);
4240fd1a014SDavid Greenman 				return (error);
4250fd1a014SDavid Greenman 			}
426c52007c2SDavid Greenman 			imgp->stringspace -= length;
427c52007c2SDavid Greenman 			imgp->stringp += length;
428c52007c2SDavid Greenman 			imgp->envc++;
42926f9a767SRodney W. Grimes 		}
4300fd1a014SDavid Greenman 	}
43126f9a767SRodney W. Grimes 
43226f9a767SRodney W. Grimes 	return (0);
43326f9a767SRodney W. Grimes }
43426f9a767SRodney W. Grimes 
43526f9a767SRodney W. Grimes /*
43626f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
43726f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
43826f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
43926f9a767SRodney W. Grimes  */
44026f9a767SRodney W. Grimes int *
441c52007c2SDavid Greenman exec_copyout_strings(imgp)
442c52007c2SDavid Greenman 	struct image_params *imgp;
44326f9a767SRodney W. Grimes {
44426f9a767SRodney W. Grimes 	int argc, envc;
44526f9a767SRodney W. Grimes 	char **vectp;
44626f9a767SRodney W. Grimes 	char *stringp, *destp;
44726f9a767SRodney W. Grimes 	int *stack_base;
44893f6448cSDavid Greenman 	struct ps_strings *arginfo;
44926f9a767SRodney W. Grimes 
45026f9a767SRodney W. Grimes 	/*
45126f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
45226f9a767SRodney W. Grimes 	 */
45393f6448cSDavid Greenman 	arginfo = PS_STRINGS;
454c52007c2SDavid Greenman 	destp =	(caddr_t)arginfo - roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
45526f9a767SRodney W. Grimes 	/*
45626f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
45726f9a767SRodney W. Grimes 	 *	arg and	env vector sets
45826f9a767SRodney W. Grimes 	 */
45926f9a767SRodney W. Grimes 	vectp = (char **) (destp -
460c52007c2SDavid Greenman 		(imgp->argc + imgp->envc + 2) * sizeof(char *));
46126f9a767SRodney W. Grimes 
46226f9a767SRodney W. Grimes 	/*
46326f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
46426f9a767SRodney W. Grimes 	 */
46526f9a767SRodney W. Grimes 	stack_base = (int *)vectp;
46626f9a767SRodney W. Grimes 
467c52007c2SDavid Greenman 	stringp = imgp->stringbase;
468c52007c2SDavid Greenman 	argc = imgp->argc;
469c52007c2SDavid Greenman 	envc = imgp->envc;
47026f9a767SRodney W. Grimes 
47193f6448cSDavid Greenman 	/*
4723aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
47393f6448cSDavid Greenman 	 */
474c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
47593f6448cSDavid Greenman 
47693f6448cSDavid Greenman 	/*
4773aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
4783aed948bSDavid Greenman 	 */
4793aed948bSDavid Greenman 	suword(&arginfo->ps_argvstr, (int)destp);
4803aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
4813aed948bSDavid Greenman 
4823aed948bSDavid Greenman 	/*
4833aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
48493f6448cSDavid Greenman 	 */
48526f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
4863aed948bSDavid Greenman 		suword(vectp++, (int)destp);
4873aed948bSDavid Greenman 		while (*stringp++ != 0)
4883aed948bSDavid Greenman 			destp++;
4893aed948bSDavid Greenman 		destp++;
49026f9a767SRodney W. Grimes 	}
49126f9a767SRodney W. Grimes 
49226f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
4933aed948bSDavid Greenman 	suword(vectp++, NULL);
49426f9a767SRodney W. Grimes 
4953aed948bSDavid Greenman 	suword(&arginfo->ps_envstr, (int)destp);
4963aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
49793f6448cSDavid Greenman 
49893f6448cSDavid Greenman 	/*
4993aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
50093f6448cSDavid Greenman 	 */
50126f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
5023aed948bSDavid Greenman 		suword(vectp++, (int)destp);
5033aed948bSDavid Greenman 		while (*stringp++ != 0)
5043aed948bSDavid Greenman 			destp++;
5053aed948bSDavid Greenman 		destp++;
50626f9a767SRodney W. Grimes 	}
50726f9a767SRodney W. Grimes 
50826f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
5093aed948bSDavid Greenman 	suword(vectp, NULL);
51026f9a767SRodney W. Grimes 
51126f9a767SRodney W. Grimes 	return (stack_base);
51226f9a767SRodney W. Grimes }
51326f9a767SRodney W. Grimes 
51426f9a767SRodney W. Grimes /*
51526f9a767SRodney W. Grimes  * Check permissions of file to execute.
51626f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
51726f9a767SRodney W. Grimes  */
518f23b4c91SGarrett Wollman static int
519c52007c2SDavid Greenman exec_check_permissions(imgp)
520c52007c2SDavid Greenman 	struct image_params *imgp;
52126f9a767SRodney W. Grimes {
522c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
523c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
524c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
52526f9a767SRodney W. Grimes 	int error;
52626f9a767SRodney W. Grimes 
52726f9a767SRodney W. Grimes 	/*
52826f9a767SRodney W. Grimes 	 * Check number of open-for-writes on the file and deny execution
52926f9a767SRodney W. Grimes 	 *	if there are any.
53026f9a767SRodney W. Grimes 	 */
531c52007c2SDavid Greenman 	if (vp->v_writecount) {
53226f9a767SRodney W. Grimes 		return (ETXTBSY);
53326f9a767SRodney W. Grimes 	}
53426f9a767SRodney W. Grimes 
53526f9a767SRodney W. Grimes 	/* Get file attributes */
536c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
53726f9a767SRodney W. Grimes 	if (error)
53826f9a767SRodney W. Grimes 		return (error);
53926f9a767SRodney W. Grimes 
54026f9a767SRodney W. Grimes 	/*
54126f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
54226f9a767SRodney W. Grimes 	 *	file resides on.
54326f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
54426f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
54526f9a767SRodney W. Grimes 	 *	file really is executable.
54626f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
54726f9a767SRodney W. Grimes 	 */
548c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
54926f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
55026f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
55126f9a767SRodney W. Grimes 		return (EACCES);
55226f9a767SRodney W. Grimes 	}
55326f9a767SRodney W. Grimes 
55426f9a767SRodney W. Grimes 	/*
55526f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
55626f9a767SRodney W. Grimes 	 */
55726f9a767SRodney W. Grimes 	if (attr->va_size == 0)
55826f9a767SRodney W. Grimes 		return (ENOEXEC);
55926f9a767SRodney W. Grimes 
56026f9a767SRodney W. Grimes 	/*
56126f9a767SRodney W. Grimes 	 * Disable setuid/setgid if the filesystem prohibits it or if
56226f9a767SRodney W. Grimes 	 *	the process is being traced.
56326f9a767SRodney W. Grimes 	 */
564c52007c2SDavid Greenman         if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
56526f9a767SRodney W. Grimes 		attr->va_mode &= ~(VSUID | VSGID);
56626f9a767SRodney W. Grimes 
56726f9a767SRodney W. Grimes 	/*
56826f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
56926f9a767SRodney W. Grimes 	 *	Then call filesystem specific open routine (which does nothing
57026f9a767SRodney W. Grimes 	 *	in the general case).
57126f9a767SRodney W. Grimes 	 */
572c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
57326f9a767SRodney W. Grimes 	if (error)
57426f9a767SRodney W. Grimes 		return (error);
57526f9a767SRodney W. Grimes 
576c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
57726f9a767SRodney W. Grimes 	if (error)
57826f9a767SRodney W. Grimes 		return (error);
57926f9a767SRodney W. Grimes 
58026f9a767SRodney W. Grimes 	return (0);
581df8bae1dSRodney W. Grimes }
582