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 * 312a531c80SDavid Greenman * $Id: kern_exec.c,v 1.7 1994/09/14 05:52:13 davidg Exp $ 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34df8bae1dSRodney W. Grimes #include <sys/param.h> 3526f9a767SRodney W. Grimes #include <sys/systm.h> 3626f9a767SRodney W. Grimes #include <sys/signalvar.h> 3726f9a767SRodney W. Grimes #include <sys/resourcevar.h> 3826f9a767SRodney W. Grimes #include <sys/kernel.h> 3926f9a767SRodney W. Grimes #include <sys/mount.h> 4026f9a767SRodney W. Grimes #include <sys/file.h> 4126f9a767SRodney W. Grimes #include <sys/acct.h> 4226f9a767SRodney W. Grimes #include <sys/exec.h> 4326f9a767SRodney W. Grimes #include <sys/imgact.h> 4426f9a767SRodney W. Grimes #include <sys/stat.h> 4526f9a767SRodney W. Grimes #include <sys/wait.h> 4626f9a767SRodney W. Grimes #include <sys/mman.h> 4726f9a767SRodney W. Grimes #include <sys/malloc.h> 4826f9a767SRodney W. Grimes #include <sys/syslog.h> 4926f9a767SRodney W. Grimes 5026f9a767SRodney W. Grimes #include <vm/vm.h> 5126f9a767SRodney W. Grimes #include <vm/vm_kern.h> 5226f9a767SRodney W. Grimes 5326f9a767SRodney W. Grimes #include <machine/reg.h> 5426f9a767SRodney W. Grimes 5526f9a767SRodney W. Grimes int *exec_copyout_strings __P((struct image_params *)); 56df8bae1dSRodney W. Grimes 57f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *); 58f23b4c91SGarrett Wollman 59df8bae1dSRodney W. Grimes /* 6026f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 6126f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 62df8bae1dSRodney W. Grimes */ 6326f9a767SRodney W. Grimes extern const struct linker_set execsw_set; 6426f9a767SRodney W. Grimes const struct execsw **execsw = (const struct execsw **)&execsw_set.ls_items[0]; 6526f9a767SRodney W. Grimes 6626f9a767SRodney W. Grimes /* 6726f9a767SRodney W. Grimes * execve() system call. 6826f9a767SRodney W. Grimes */ 6926f9a767SRodney W. Grimes int 7026f9a767SRodney W. Grimes execve(p, uap, retval) 7126f9a767SRodney W. Grimes struct proc *p; 7226f9a767SRodney W. Grimes register struct execve_args *uap; 7326f9a767SRodney W. Grimes int *retval; 74df8bae1dSRodney W. Grimes { 7526f9a767SRodney W. Grimes struct nameidata nd, *ndp; 7626f9a767SRodney W. Grimes char *stringbase, *stringp; 7726f9a767SRodney W. Grimes int *stack_base; 7826f9a767SRodney W. Grimes int error, resid, len, i; 7926f9a767SRodney W. Grimes struct image_params image_params, *iparams; 8026f9a767SRodney W. Grimes struct vnode *vnodep; 8126f9a767SRodney W. Grimes struct vattr attr; 8226f9a767SRodney W. Grimes char *image_header; 8326f9a767SRodney W. Grimes 8426f9a767SRodney W. Grimes iparams = &image_params; 8526f9a767SRodney W. Grimes bzero((caddr_t)iparams, sizeof(struct image_params)); 8626f9a767SRodney W. Grimes image_header = (char *)0; 87df8bae1dSRodney W. Grimes 88df8bae1dSRodney W. Grimes /* 8926f9a767SRodney W. Grimes * Initialize a few constants in the common area 90df8bae1dSRodney W. Grimes */ 9126f9a767SRodney W. Grimes iparams->proc = p; 9226f9a767SRodney W. Grimes iparams->uap = uap; 9326f9a767SRodney W. Grimes iparams->attr = &attr; 9426f9a767SRodney W. Grimes 9526f9a767SRodney W. Grimes /* 9626f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 9726f9a767SRodney W. Grimes * environment strings 9826f9a767SRodney W. Grimes */ 9926f9a767SRodney W. Grimes error = vm_allocate(kernel_map, (vm_offset_t *)&iparams->stringbase, 10026f9a767SRodney W. Grimes ARG_MAX, TRUE); 10126f9a767SRodney W. Grimes if (error) { 10226f9a767SRodney W. Grimes log(LOG_WARNING, "execve: failed to allocate string space\n"); 10326f9a767SRodney W. Grimes return (error); 10426f9a767SRodney W. Grimes } 10526f9a767SRodney W. Grimes 10626f9a767SRodney W. Grimes if (!iparams->stringbase) { 10726f9a767SRodney W. Grimes error = ENOMEM; 10826f9a767SRodney W. Grimes goto exec_fail; 10926f9a767SRodney W. Grimes } 11026f9a767SRodney W. Grimes iparams->stringp = iparams->stringbase; 11126f9a767SRodney W. Grimes iparams->stringspace = ARG_MAX; 11226f9a767SRodney W. Grimes 11326f9a767SRodney W. Grimes /* 11426f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 11526f9a767SRodney W. Grimes * in ni_vp amoung other things. 11626f9a767SRodney W. Grimes */ 11726f9a767SRodney W. Grimes ndp = &nd; 11826f9a767SRodney W. Grimes ndp->ni_cnd.cn_nameiop = LOOKUP; 11926f9a767SRodney W. Grimes ndp->ni_cnd.cn_flags = LOCKLEAF | FOLLOW | SAVENAME; 12026f9a767SRodney W. Grimes ndp->ni_cnd.cn_proc = curproc; 12126f9a767SRodney W. Grimes ndp->ni_cnd.cn_cred = curproc->p_cred->pc_ucred; 12226f9a767SRodney W. Grimes ndp->ni_segflg = UIO_USERSPACE; 12326f9a767SRodney W. Grimes ndp->ni_dirp = uap->fname; 12426f9a767SRodney W. Grimes 12526f9a767SRodney W. Grimes interpret: 12626f9a767SRodney W. Grimes 12726f9a767SRodney W. Grimes error = namei(ndp); 12826f9a767SRodney W. Grimes if (error) { 12926f9a767SRodney W. Grimes vm_deallocate(kernel_map, (vm_offset_t)iparams->stringbase, 13026f9a767SRodney W. Grimes ARG_MAX); 13126f9a767SRodney W. Grimes goto exec_fail; 13226f9a767SRodney W. Grimes } 13326f9a767SRodney W. Grimes 13426f9a767SRodney W. Grimes iparams->vnodep = vnodep = ndp->ni_vp; 13526f9a767SRodney W. Grimes 13626f9a767SRodney W. Grimes if (vnodep == NULL) { 13726f9a767SRodney W. Grimes error = ENOEXEC; 13826f9a767SRodney W. Grimes goto exec_fail_dealloc; 13926f9a767SRodney W. Grimes } 14026f9a767SRodney W. Grimes 14126f9a767SRodney W. Grimes /* 14226f9a767SRodney W. Grimes * Check file permissions (also 'opens' file) 14326f9a767SRodney W. Grimes */ 14426f9a767SRodney W. Grimes error = exec_check_permissions(iparams); 14526f9a767SRodney W. Grimes if (error) 14626f9a767SRodney W. Grimes goto exec_fail_dealloc; 14726f9a767SRodney W. Grimes 14826f9a767SRodney W. Grimes /* 14926f9a767SRodney W. Grimes * Map the image header (first page) of the file into 15026f9a767SRodney W. Grimes * kernel address space 15126f9a767SRodney W. Grimes */ 15226f9a767SRodney W. Grimes error = vm_mmap(kernel_map, /* map */ 15326f9a767SRodney W. Grimes (vm_offset_t *)&image_header, /* address */ 15426f9a767SRodney W. Grimes PAGE_SIZE, /* size */ 15526f9a767SRodney W. Grimes VM_PROT_READ, /* protection */ 15626f9a767SRodney W. Grimes VM_PROT_READ, /* max protection */ 15726f9a767SRodney W. Grimes 0, /* flags */ 15826f9a767SRodney W. Grimes (caddr_t)vnodep, /* vnode */ 15926f9a767SRodney W. Grimes 0); /* offset */ 16026f9a767SRodney W. Grimes if (error) { 16126f9a767SRodney W. Grimes uprintf("mmap failed: %d\n",error); 16226f9a767SRodney W. Grimes goto exec_fail_dealloc; 16326f9a767SRodney W. Grimes } 16426f9a767SRodney W. Grimes iparams->image_header = image_header; 16526f9a767SRodney W. Grimes 16626f9a767SRodney W. Grimes /* 16726f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 16826f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 16926f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 17026f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 17126f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 17226f9a767SRodney W. Grimes * the interpreter. 17326f9a767SRodney W. Grimes */ 17426f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 17526f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 17626f9a767SRodney W. Grimes error = (*execsw[i]->ex_imgact)(iparams); 17726f9a767SRodney W. Grimes else 17826f9a767SRodney W. Grimes continue; 17926f9a767SRodney W. Grimes 18026f9a767SRodney W. Grimes if (error == -1) 18126f9a767SRodney W. Grimes continue; 18226f9a767SRodney W. Grimes if (error) 18326f9a767SRodney W. Grimes goto exec_fail_dealloc; 18426f9a767SRodney W. Grimes if (iparams->interpreted) { 18526f9a767SRodney W. Grimes /* free old vnode and name buffer */ 18626f9a767SRodney W. Grimes vput(ndp->ni_vp); 18726f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 18826f9a767SRodney W. Grimes if (vm_deallocate(kernel_map, 18926f9a767SRodney W. Grimes (vm_offset_t)image_header, PAGE_SIZE)) 19026f9a767SRodney W. Grimes panic("execve: header dealloc failed (1)"); 19126f9a767SRodney W. Grimes 19226f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 19326f9a767SRodney W. Grimes ndp->ni_segflg = UIO_SYSSPACE; 19426f9a767SRodney W. Grimes ndp->ni_dirp = iparams->interpreter_name; 19526f9a767SRodney W. Grimes ndp->ni_cnd.cn_nameiop = LOOKUP; 19626f9a767SRodney W. Grimes ndp->ni_cnd.cn_flags = LOCKLEAF | FOLLOW | SAVENAME; 19726f9a767SRodney W. Grimes ndp->ni_cnd.cn_proc = curproc; 19826f9a767SRodney W. Grimes ndp->ni_cnd.cn_cred = curproc->p_cred->pc_ucred; 19926f9a767SRodney W. Grimes goto interpret; 20026f9a767SRodney W. Grimes } 20126f9a767SRodney W. Grimes break; 20226f9a767SRodney W. Grimes } 20326f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 20426f9a767SRodney W. Grimes if (error == -1) { 20526f9a767SRodney W. Grimes error = ENOEXEC; 20626f9a767SRodney W. Grimes goto exec_fail_dealloc; 20726f9a767SRodney W. Grimes } 20826f9a767SRodney W. Grimes 20926f9a767SRodney W. Grimes /* 21026f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 21126f9a767SRodney W. Grimes */ 21226f9a767SRodney W. Grimes stack_base = exec_copyout_strings(iparams); 21326f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 21426f9a767SRodney W. Grimes 21526f9a767SRodney W. Grimes /* 21626f9a767SRodney W. Grimes * Stuff argument count as first item on stack 21726f9a767SRodney W. Grimes */ 21826f9a767SRodney W. Grimes *(--stack_base) = iparams->argc; 21926f9a767SRodney W. Grimes 22026f9a767SRodney W. Grimes /* close files on exec */ 22126f9a767SRodney W. Grimes fdcloseexec(p); 22226f9a767SRodney W. Grimes 22326f9a767SRodney W. Grimes /* reset caught signals */ 22426f9a767SRodney W. Grimes execsigs(p); 22526f9a767SRodney W. Grimes 22626f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 22726f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 22826f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 22926f9a767SRodney W. Grimes p->p_comm[len] = 0; 23026f9a767SRodney W. Grimes 23126f9a767SRodney W. Grimes /* 23226f9a767SRodney W. Grimes * mark as executable, wakeup any process that was vforked and tell 23326f9a767SRodney W. Grimes * it that it now has it's own resources back 23426f9a767SRodney W. Grimes */ 23526f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 23626f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 23726f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 23826f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 23926f9a767SRodney W. Grimes } 24026f9a767SRodney W. Grimes 24126f9a767SRodney W. Grimes /* implement set userid/groupid */ 24226f9a767SRodney W. Grimes p->p_flag &= ~P_SUGID; 24326f9a767SRodney W. Grimes 24426f9a767SRodney W. Grimes /* 24526f9a767SRodney W. Grimes * Turn off kernel tracing for set-id programs, except for 24626f9a767SRodney W. Grimes * root. 24726f9a767SRodney W. Grimes */ 24826f9a767SRodney W. Grimes if (p->p_tracep && (attr.va_mode & (VSUID | VSGID)) && 24926f9a767SRodney W. Grimes suser(p->p_ucred, &p->p_acflag)) { 25026f9a767SRodney W. Grimes p->p_traceflag = 0; 25126f9a767SRodney W. Grimes vrele(p->p_tracep); 25226f9a767SRodney W. Grimes p->p_tracep = 0; 25326f9a767SRodney W. Grimes } 25426f9a767SRodney W. Grimes if ((attr.va_mode & VSUID) && (p->p_flag & P_TRACED) == 0) { 25526f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 25626f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 25726f9a767SRodney W. Grimes p->p_flag |= P_SUGID; 25826f9a767SRodney W. Grimes } 25926f9a767SRodney W. Grimes if ((attr.va_mode & VSGID) && (p->p_flag & P_TRACED) == 0) { 26026f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 26126f9a767SRodney W. Grimes p->p_ucred->cr_groups[0] = attr.va_gid; 26226f9a767SRodney W. Grimes p->p_flag |= P_SUGID; 26326f9a767SRodney W. Grimes } 26426f9a767SRodney W. Grimes 26526f9a767SRodney W. Grimes /* 26626f9a767SRodney W. Grimes * Implement correct POSIX saved uid behavior. 26726f9a767SRodney W. Grimes */ 26826f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 26926f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 27026f9a767SRodney W. Grimes 27126f9a767SRodney W. Grimes /* mark vnode pure text */ 27226f9a767SRodney W. Grimes ndp->ni_vp->v_flag |= VTEXT; 27326f9a767SRodney W. Grimes 27426f9a767SRodney W. Grimes /* 2752a531c80SDavid Greenman * Store the vp for use in procfs 2762a531c80SDavid Greenman */ 2772a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 2782a531c80SDavid Greenman vrele(p->p_textvp); 2792a531c80SDavid Greenman VREF(ndp->ni_vp); 2802a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 2812a531c80SDavid Greenman 2822a531c80SDavid Greenman /* 28326f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 28426f9a767SRodney W. Grimes * can be set before the program executes. 28526f9a767SRodney W. Grimes */ 28626f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 28726f9a767SRodney W. Grimes psignal(p, SIGTRAP); 28826f9a767SRodney W. Grimes 28926f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 29026f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 29126f9a767SRodney W. Grimes 29226f9a767SRodney W. Grimes /* Set entry address */ 29326f9a767SRodney W. Grimes setregs(p, iparams->entry_addr, stack_base); 29426f9a767SRodney W. Grimes 29526f9a767SRodney W. Grimes /* 29626f9a767SRodney W. Grimes * free various allocated resources 29726f9a767SRodney W. Grimes */ 29826f9a767SRodney W. Grimes if (vm_deallocate(kernel_map, (vm_offset_t)iparams->stringbase, ARG_MAX)) 29926f9a767SRodney W. Grimes panic("execve: string buffer dealloc failed (1)"); 30026f9a767SRodney W. Grimes if (vm_deallocate(kernel_map, (vm_offset_t)image_header, PAGE_SIZE)) 30126f9a767SRodney W. Grimes panic("execve: header dealloc failed (2)"); 30226f9a767SRodney W. Grimes vput(ndp->ni_vp); 30326f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 30426f9a767SRodney W. Grimes 30526f9a767SRodney W. Grimes return (0); 30626f9a767SRodney W. Grimes 30726f9a767SRodney W. Grimes exec_fail_dealloc: 30826f9a767SRodney W. Grimes if (iparams->stringbase && iparams->stringbase != (char *)-1) 30926f9a767SRodney W. Grimes if (vm_deallocate(kernel_map, (vm_offset_t)iparams->stringbase, 31026f9a767SRodney W. Grimes ARG_MAX)) 31126f9a767SRodney W. Grimes panic("execve: string buffer dealloc failed (2)"); 31226f9a767SRodney W. Grimes if (iparams->image_header && iparams->image_header != (char *)-1) 31326f9a767SRodney W. Grimes if (vm_deallocate(kernel_map, 31426f9a767SRodney W. Grimes (vm_offset_t)iparams->image_header, PAGE_SIZE)) 31526f9a767SRodney W. Grimes panic("execve: header dealloc failed (3)"); 31626f9a767SRodney W. Grimes vput(ndp->ni_vp); 31726f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 31826f9a767SRodney W. Grimes 31926f9a767SRodney W. Grimes exec_fail: 32026f9a767SRodney W. Grimes if (iparams->vmspace_destroyed) { 32126f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 32226f9a767SRodney W. Grimes #if 0 /* XXX */ 32326f9a767SRodney W. Grimes vm_deallocate(&vs->vm_map, USRSTACK - MAXSSIZ, MAXSSIZ); 32426f9a767SRodney W. Grimes #endif 32526f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 32626f9a767SRodney W. Grimes /* NOT REACHED */ 32726f9a767SRodney W. Grimes return(0); 32826f9a767SRodney W. Grimes } else { 32926f9a767SRodney W. Grimes return(error); 33026f9a767SRodney W. Grimes } 33126f9a767SRodney W. Grimes } 33226f9a767SRodney W. Grimes 33326f9a767SRodney W. Grimes /* 33426f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 33526f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 33626f9a767SRodney W. Grimes * automatically in trap.c. 33726f9a767SRodney W. Grimes */ 33826f9a767SRodney W. Grimes int 33926f9a767SRodney W. Grimes exec_new_vmspace(iparams) 34026f9a767SRodney W. Grimes struct image_params *iparams; 34126f9a767SRodney W. Grimes { 34226f9a767SRodney W. Grimes int error; 34326f9a767SRodney W. Grimes struct vmspace *vmspace = iparams->proc->p_vmspace; 34426f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 34526f9a767SRodney W. Grimes 34626f9a767SRodney W. Grimes iparams->vmspace_destroyed = 1; 34726f9a767SRodney W. Grimes 34826f9a767SRodney W. Grimes /* Blow away entire process VM */ 349d74643dfSDavid Greenman #ifdef SYSVSHM 3503d903220SDoug Rabson if (vmspace->vm_shm) 3513d903220SDoug Rabson shmexit(iparams->proc); 352d74643dfSDavid Greenman #endif 35326f9a767SRodney W. Grimes vm_deallocate(&vmspace->vm_map, 0, USRSTACK); 35426f9a767SRodney W. Grimes 35526f9a767SRodney W. Grimes /* Allocate a new stack */ 35626f9a767SRodney W. Grimes error = vm_allocate(&vmspace->vm_map, (vm_offset_t *)&stack_addr, 35726f9a767SRodney W. Grimes SGROWSIZ, FALSE); 35826f9a767SRodney W. Grimes if (error) 35926f9a767SRodney W. Grimes return(error); 36026f9a767SRodney W. Grimes 36126f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 36226f9a767SRodney W. Grimes 36326f9a767SRodney W. Grimes /* Initialize maximum stack address */ 36426f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 36526f9a767SRodney W. Grimes 36626f9a767SRodney W. Grimes return(0); 36726f9a767SRodney W. Grimes } 36826f9a767SRodney W. Grimes 36926f9a767SRodney W. Grimes /* 37026f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 37126f9a767SRodney W. Grimes * address space into the temporary string buffer. 37226f9a767SRodney W. Grimes */ 37326f9a767SRodney W. Grimes int 37426f9a767SRodney W. Grimes exec_extract_strings(iparams) 37526f9a767SRodney W. Grimes struct image_params *iparams; 37626f9a767SRodney W. Grimes { 37726f9a767SRodney W. Grimes char **argv, **envv; 37826f9a767SRodney W. Grimes char *argp, *envp; 3790fd1a014SDavid Greenman int error, length; 38026f9a767SRodney W. Grimes 38126f9a767SRodney W. Grimes /* 38226f9a767SRodney W. Grimes * extract arguments first 38326f9a767SRodney W. Grimes */ 38426f9a767SRodney W. Grimes 38526f9a767SRodney W. Grimes argv = iparams->uap->argv; 38626f9a767SRodney W. Grimes 3870fd1a014SDavid Greenman if (argv) { 38826f9a767SRodney W. Grimes while (argp = (caddr_t) fuword(argv++)) { 38926f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 39026f9a767SRodney W. Grimes return (EFAULT); 3910fd1a014SDavid Greenman if (error = copyinstr(argp, iparams->stringp, 3920fd1a014SDavid Greenman iparams->stringspace, &length)) { 3930fd1a014SDavid Greenman if (error == ENAMETOOLONG) 39426f9a767SRodney W. Grimes return(E2BIG); 3950fd1a014SDavid Greenman return (error); 3960fd1a014SDavid Greenman } 39726f9a767SRodney W. Grimes iparams->stringspace -= length; 39826f9a767SRodney W. Grimes iparams->stringp += length; 39926f9a767SRodney W. Grimes iparams->argc++; 40026f9a767SRodney W. Grimes } 4010fd1a014SDavid Greenman } 40226f9a767SRodney W. Grimes 40326f9a767SRodney W. Grimes /* 40426f9a767SRodney W. Grimes * extract environment strings 40526f9a767SRodney W. Grimes */ 40626f9a767SRodney W. Grimes 40726f9a767SRodney W. Grimes envv = iparams->uap->envv; 40826f9a767SRodney W. Grimes 4090fd1a014SDavid Greenman if (envv) { 41026f9a767SRodney W. Grimes while (envp = (caddr_t) fuword(envv++)) { 41126f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 41226f9a767SRodney W. Grimes return (EFAULT); 4130fd1a014SDavid Greenman if (error = copyinstr(envp, iparams->stringp, 4140fd1a014SDavid Greenman iparams->stringspace, &length)) { 4150fd1a014SDavid Greenman if (error == ENAMETOOLONG) 41626f9a767SRodney W. Grimes return(E2BIG); 4170fd1a014SDavid Greenman return (error); 4180fd1a014SDavid Greenman } 41926f9a767SRodney W. Grimes iparams->stringspace -= length; 42026f9a767SRodney W. Grimes iparams->stringp += length; 42126f9a767SRodney W. Grimes iparams->envc++; 42226f9a767SRodney W. Grimes } 4230fd1a014SDavid Greenman } 42426f9a767SRodney W. Grimes 42526f9a767SRodney W. Grimes return (0); 42626f9a767SRodney W. Grimes } 42726f9a767SRodney W. Grimes 42826f9a767SRodney W. Grimes /* 42926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 43026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 43126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 43226f9a767SRodney W. Grimes */ 43326f9a767SRodney W. Grimes int * 43426f9a767SRodney W. Grimes exec_copyout_strings(iparams) 43526f9a767SRodney W. Grimes struct image_params *iparams; 43626f9a767SRodney W. Grimes { 43726f9a767SRodney W. Grimes int argc, envc; 43826f9a767SRodney W. Grimes char **vectp; 43926f9a767SRodney W. Grimes char *stringp, *destp; 44026f9a767SRodney W. Grimes int *stack_base; 44126f9a767SRodney W. Grimes int vect_table_size, string_table_size; 44293f6448cSDavid Greenman struct ps_strings *arginfo; 44326f9a767SRodney W. Grimes 44426f9a767SRodney W. Grimes /* 44526f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 44626f9a767SRodney W. Grimes */ 44793f6448cSDavid Greenman arginfo = PS_STRINGS; 44893f6448cSDavid Greenman destp = (caddr_t)arginfo - roundup((ARG_MAX - iparams->stringspace), sizeof(char *)); 44926f9a767SRodney W. Grimes /* 45026f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 45126f9a767SRodney W. Grimes * arg and env vector sets 45226f9a767SRodney W. Grimes */ 45326f9a767SRodney W. Grimes vectp = (char **) (destp - 45426f9a767SRodney W. Grimes (iparams->argc + iparams->envc + 2) * sizeof(char *)); 45526f9a767SRodney W. Grimes 45626f9a767SRodney W. Grimes /* 45726f9a767SRodney W. Grimes * vectp also becomes our initial stack base 45826f9a767SRodney W. Grimes */ 45926f9a767SRodney W. Grimes stack_base = (int *)vectp; 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes stringp = iparams->stringbase; 46226f9a767SRodney W. Grimes argc = iparams->argc; 46326f9a767SRodney W. Grimes envc = iparams->envc; 46426f9a767SRodney W. Grimes 46593f6448cSDavid Greenman /* 46693f6448cSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 46793f6448cSDavid Greenman */ 46893f6448cSDavid Greenman arginfo->ps_argvstr = destp; 46993f6448cSDavid Greenman arginfo->ps_nargvstr = argc; 47093f6448cSDavid Greenman 47193f6448cSDavid Greenman /* 47293f6448cSDavid Greenman * Copy the arg strings and fill in vector table as we go. 47393f6448cSDavid Greenman */ 47426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 47526f9a767SRodney W. Grimes *(vectp++) = destp; 47626f9a767SRodney W. Grimes while (*destp++ = *stringp++); 47726f9a767SRodney W. Grimes } 47826f9a767SRodney W. Grimes 47926f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 48026f9a767SRodney W. Grimes *(vectp++) = NULL; 48126f9a767SRodney W. Grimes 48293f6448cSDavid Greenman arginfo->ps_envstr = destp; 48393f6448cSDavid Greenman arginfo->ps_nenvstr = envc; 48493f6448cSDavid Greenman 48593f6448cSDavid Greenman /* 48693f6448cSDavid Greenman * Copy the env strings and fill in vector table as we go. 48793f6448cSDavid Greenman */ 48826f9a767SRodney W. Grimes for (; envc > 0; --envc) { 48926f9a767SRodney W. Grimes *(vectp++) = destp; 49026f9a767SRodney W. Grimes while (*destp++ = *stringp++); 49126f9a767SRodney W. Grimes } 49226f9a767SRodney W. Grimes 49326f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 49426f9a767SRodney W. Grimes *vectp = NULL; 49526f9a767SRodney W. Grimes 49626f9a767SRodney W. Grimes return (stack_base); 49726f9a767SRodney W. Grimes } 49826f9a767SRodney W. Grimes 49926f9a767SRodney W. Grimes /* 50026f9a767SRodney W. Grimes * Check permissions of file to execute. 50126f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 50226f9a767SRodney W. Grimes */ 503f23b4c91SGarrett Wollman static int 50426f9a767SRodney W. Grimes exec_check_permissions(iparams) 50526f9a767SRodney W. Grimes struct image_params *iparams; 50626f9a767SRodney W. Grimes { 50726f9a767SRodney W. Grimes struct proc *p = iparams->proc; 50826f9a767SRodney W. Grimes struct vnode *vnodep = iparams->vnodep; 50926f9a767SRodney W. Grimes struct vattr *attr = iparams->attr; 51026f9a767SRodney W. Grimes int error; 51126f9a767SRodney W. Grimes 51226f9a767SRodney W. Grimes /* 51326f9a767SRodney W. Grimes * Check number of open-for-writes on the file and deny execution 51426f9a767SRodney W. Grimes * if there are any. 51526f9a767SRodney W. Grimes */ 51626f9a767SRodney W. Grimes if (vnodep->v_writecount) { 51726f9a767SRodney W. Grimes return (ETXTBSY); 51826f9a767SRodney W. Grimes } 51926f9a767SRodney W. Grimes 52026f9a767SRodney W. Grimes /* Get file attributes */ 52126f9a767SRodney W. Grimes error = VOP_GETATTR(vnodep, attr, p->p_ucred, p); 52226f9a767SRodney W. Grimes if (error) 52326f9a767SRodney W. Grimes return (error); 52426f9a767SRodney W. Grimes 52526f9a767SRodney W. Grimes /* 52626f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 52726f9a767SRodney W. Grimes * file resides on. 52826f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 52926f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 53026f9a767SRodney W. Grimes * file really is executable. 53126f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 53226f9a767SRodney W. Grimes */ 53326f9a767SRodney W. Grimes if ((vnodep->v_mount->mnt_flag & MNT_NOEXEC) || 53426f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 53526f9a767SRodney W. Grimes (attr->va_type != VREG)) { 53626f9a767SRodney W. Grimes return (EACCES); 53726f9a767SRodney W. Grimes } 53826f9a767SRodney W. Grimes 53926f9a767SRodney W. Grimes /* 54026f9a767SRodney W. Grimes * Zero length files can't be exec'd 54126f9a767SRodney W. Grimes */ 54226f9a767SRodney W. Grimes if (attr->va_size == 0) 54326f9a767SRodney W. Grimes return (ENOEXEC); 54426f9a767SRodney W. Grimes 54526f9a767SRodney W. Grimes /* 54626f9a767SRodney W. Grimes * Disable setuid/setgid if the filesystem prohibits it or if 54726f9a767SRodney W. Grimes * the process is being traced. 54826f9a767SRodney W. Grimes */ 54926f9a767SRodney W. Grimes if ((vnodep->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 55026f9a767SRodney W. Grimes attr->va_mode &= ~(VSUID | VSGID); 55126f9a767SRodney W. Grimes 55226f9a767SRodney W. Grimes /* 55326f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 55426f9a767SRodney W. Grimes * Then call filesystem specific open routine (which does nothing 55526f9a767SRodney W. Grimes * in the general case). 55626f9a767SRodney W. Grimes */ 55726f9a767SRodney W. Grimes error = VOP_ACCESS(vnodep, VEXEC, p->p_ucred, p); 55826f9a767SRodney W. Grimes if (error) 55926f9a767SRodney W. Grimes return (error); 56026f9a767SRodney W. Grimes 56126f9a767SRodney W. Grimes error = VOP_OPEN(vnodep, FREAD, p->p_ucred, p); 56226f9a767SRodney W. Grimes if (error) 56326f9a767SRodney W. Grimes return (error); 56426f9a767SRodney W. Grimes 56526f9a767SRodney W. Grimes return (0); 566df8bae1dSRodney W. Grimes } 567