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 * 3199ac3bc8SPeter Wemm * $Id: kern_exec.c,v 1.34 1996/01/20 21:36:30 bde 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> 5099ac3bc8SPeter Wemm #include <sys/sysctl.h> 5126f9a767SRodney W. Grimes 5226f9a767SRodney W. Grimes #include <vm/vm.h> 53efeaf95aSDavid Greenman #include <vm/vm_param.h> 54efeaf95aSDavid Greenman #include <vm/vm_prot.h> 55efeaf95aSDavid Greenman #include <vm/lock.h> 56efeaf95aSDavid Greenman #include <vm/pmap.h> 57efeaf95aSDavid Greenman #include <vm/vm_map.h> 5826f9a767SRodney W. Grimes #include <vm/vm_kern.h> 59efeaf95aSDavid Greenman #include <vm/vm_extern.h> 6026f9a767SRodney W. Grimes 6126f9a767SRodney W. Grimes #include <machine/reg.h> 6226f9a767SRodney W. Grimes 6387b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *)); 64df8bae1dSRodney W. Grimes 65f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *); 66f23b4c91SGarrett Wollman 67df8bae1dSRodney W. Grimes /* 6899ac3bc8SPeter Wemm * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts 6999ac3bc8SPeter Wemm * of the sysctl code also assumes this, and sizeof(int) == sizeof(long). 7099ac3bc8SPeter Wemm */ 7199ac3bc8SPeter Wemm static caddr_t ps_strings = (caddr_t)PS_STRINGS; 7299ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, ""); 7399ac3bc8SPeter Wemm 7499ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK; 7599ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, ""); 7699ac3bc8SPeter Wemm 7799ac3bc8SPeter Wemm /* 7826f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 7926f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 80df8bae1dSRodney W. Grimes */ 8187b6de2bSPoul-Henning Kamp static const struct execsw **execsw = 8287b6de2bSPoul-Henning Kamp (const struct execsw **)&execsw_set.ls_items[0]; 8326f9a767SRodney W. Grimes 84d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 85ad7507e2SSteven Wallace struct execve_args { 86ad7507e2SSteven Wallace char *fname; 87ad7507e2SSteven Wallace char **argv; 88ad7507e2SSteven Wallace char **envv; 89ad7507e2SSteven Wallace }; 90d2d3e875SBruce Evans #endif 91ad7507e2SSteven Wallace 9226f9a767SRodney W. Grimes /* 9326f9a767SRodney W. Grimes * execve() system call. 9426f9a767SRodney W. Grimes */ 9526f9a767SRodney W. Grimes int 9626f9a767SRodney W. Grimes execve(p, uap, retval) 9726f9a767SRodney W. Grimes struct proc *p; 9826f9a767SRodney W. Grimes register struct execve_args *uap; 9926f9a767SRodney W. Grimes int *retval; 100df8bae1dSRodney W. Grimes { 10126f9a767SRodney W. Grimes struct nameidata nd, *ndp; 10226f9a767SRodney W. Grimes int *stack_base; 103bb56ec4aSPoul-Henning Kamp int error, len, i; 104c52007c2SDavid Greenman struct image_params image_params, *imgp; 10526f9a767SRodney W. Grimes struct vattr attr; 10626f9a767SRodney W. Grimes 107c52007c2SDavid Greenman imgp = &image_params; 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes /* 110c52007c2SDavid Greenman * Initialize part of the common data 111df8bae1dSRodney W. Grimes */ 112c52007c2SDavid Greenman imgp->proc = p; 113c52007c2SDavid Greenman imgp->uap = uap; 114c52007c2SDavid Greenman imgp->attr = &attr; 115c52007c2SDavid Greenman imgp->image_header = NULL; 116c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 117c52007c2SDavid Greenman imgp->entry_addr = 0; 118c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 119c52007c2SDavid Greenman imgp->interpreted = 0; 120c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 12126f9a767SRodney W. Grimes 12226f9a767SRodney W. Grimes /* 12326f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12426f9a767SRodney W. Grimes * environment strings 12526f9a767SRodney W. Grimes */ 126c2f9f36bSDavid Greenman imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX); 127c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 12826f9a767SRodney W. Grimes error = ENOMEM; 12926f9a767SRodney W. Grimes goto exec_fail; 13026f9a767SRodney W. Grimes } 131c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 132c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 13326f9a767SRodney W. Grimes 13426f9a767SRodney W. Grimes /* 13526f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 13626f9a767SRodney W. Grimes * in ni_vp amoung other things. 13726f9a767SRodney W. Grimes */ 13826f9a767SRodney W. Grimes ndp = &nd; 139b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1403ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 14126f9a767SRodney W. Grimes 14226f9a767SRodney W. Grimes interpret: 14326f9a767SRodney W. Grimes 14426f9a767SRodney W. Grimes error = namei(ndp); 14526f9a767SRodney W. Grimes if (error) { 146c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 14726f9a767SRodney W. Grimes goto exec_fail; 14826f9a767SRodney W. Grimes } 14926f9a767SRodney W. Grimes 150c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 151c52007c2SDavid Greenman if (imgp->vp == NULL) { 15226f9a767SRodney W. Grimes error = ENOEXEC; 15326f9a767SRodney W. Grimes goto exec_fail_dealloc; 15426f9a767SRodney W. Grimes } 15526f9a767SRodney W. Grimes 15626f9a767SRodney W. Grimes /* 1579022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1589022e4adSDavid Greenman */ 159c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1609022e4adSDavid Greenman 1619022e4adSDavid Greenman /* 1629022e4adSDavid Greenman * Lose the lock on the vnode. It's no longer needed, and must not 163f5277ae7SDavid Greenman * exist for the pagefault paging to work below. 164f5277ae7SDavid Greenman */ 165c52007c2SDavid Greenman VOP_UNLOCK(imgp->vp); 166f5277ae7SDavid Greenman 16726f9a767SRodney W. Grimes if (error) 16826f9a767SRodney W. Grimes goto exec_fail_dealloc; 16926f9a767SRodney W. Grimes 17026f9a767SRodney W. Grimes /* 17126f9a767SRodney W. Grimes * Map the image header (first page) of the file into 17226f9a767SRodney W. Grimes * kernel address space 17326f9a767SRodney W. Grimes */ 17426f9a767SRodney W. Grimes error = vm_mmap(kernel_map, /* map */ 175c52007c2SDavid Greenman (vm_offset_t *)&imgp->image_header, /* address */ 17626f9a767SRodney W. Grimes PAGE_SIZE, /* size */ 17726f9a767SRodney W. Grimes VM_PROT_READ, /* protection */ 17826f9a767SRodney W. Grimes VM_PROT_READ, /* max protection */ 17926f9a767SRodney W. Grimes 0, /* flags */ 180c52007c2SDavid Greenman (caddr_t)imgp->vp, /* vnode */ 18126f9a767SRodney W. Grimes 0); /* offset */ 18226f9a767SRodney W. Grimes if (error) { 18326f9a767SRodney W. Grimes uprintf("mmap failed: %d\n",error); 18426f9a767SRodney W. Grimes goto exec_fail_dealloc; 18526f9a767SRodney W. Grimes } 18626f9a767SRodney W. Grimes 18726f9a767SRodney W. Grimes /* 18826f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 18926f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 19026f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 19126f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 19226f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 19326f9a767SRodney W. Grimes * the interpreter. 19426f9a767SRodney W. Grimes */ 19526f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 19626f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 197c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 19826f9a767SRodney W. Grimes else 19926f9a767SRodney W. Grimes continue; 20026f9a767SRodney W. Grimes 20126f9a767SRodney W. Grimes if (error == -1) 20226f9a767SRodney W. Grimes continue; 20326f9a767SRodney W. Grimes if (error) 20426f9a767SRodney W. Grimes goto exec_fail_dealloc; 205c52007c2SDavid Greenman if (imgp->interpreted) { 20626f9a767SRodney W. Grimes /* free old vnode and name buffer */ 207f5277ae7SDavid Greenman vrele(ndp->ni_vp); 20826f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 209c52007c2SDavid Greenman if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header, 210c52007c2SDavid Greenman (vm_offset_t)imgp->image_header + PAGE_SIZE)) 21126f9a767SRodney W. Grimes panic("execve: header dealloc failed (1)"); 21226f9a767SRodney W. Grimes 21326f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 214b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 215c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 21626f9a767SRodney W. Grimes goto interpret; 21726f9a767SRodney W. Grimes } 21826f9a767SRodney W. Grimes break; 21926f9a767SRodney W. Grimes } 22026f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 22126f9a767SRodney W. Grimes if (error == -1) { 22226f9a767SRodney W. Grimes error = ENOEXEC; 22326f9a767SRodney W. Grimes goto exec_fail_dealloc; 22426f9a767SRodney W. Grimes } 22526f9a767SRodney W. Grimes 22626f9a767SRodney W. Grimes /* 22726f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 22826f9a767SRodney W. Grimes */ 229c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 23026f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 23126f9a767SRodney W. Grimes 23226f9a767SRodney W. Grimes /* 2331e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2341e1e0b44SSøren Schmidt * let it do the stack setup. 2351e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 23626f9a767SRodney W. Grimes */ 2371e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 238c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2391e1e0b44SSøren Schmidt else 240c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 24126f9a767SRodney W. Grimes 24226f9a767SRodney W. Grimes /* close files on exec */ 24326f9a767SRodney W. Grimes fdcloseexec(p); 24426f9a767SRodney W. Grimes 24526f9a767SRodney W. Grimes /* reset caught signals */ 24626f9a767SRodney W. Grimes execsigs(p); 24726f9a767SRodney W. Grimes 24826f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 24926f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 25026f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 25126f9a767SRodney W. Grimes p->p_comm[len] = 0; 25226f9a767SRodney W. Grimes 25326f9a767SRodney W. Grimes /* 25426f9a767SRodney W. Grimes * mark as executable, wakeup any process that was vforked and tell 25526f9a767SRodney W. Grimes * it that it now has it's own resources back 25626f9a767SRodney W. Grimes */ 25726f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 25826f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 25926f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 26026f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 26126f9a767SRodney W. Grimes } 26226f9a767SRodney W. Grimes 26326f9a767SRodney W. Grimes /* 264c52007c2SDavid Greenman * Implement image setuid/setgid. Disallow if the process is 265c52007c2SDavid Greenman * being traced. 266c52007c2SDavid Greenman */ 267c52007c2SDavid Greenman if ((attr.va_mode & (VSUID | VSGID)) && 268c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 269c52007c2SDavid Greenman /* 270c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 27126f9a767SRodney W. Grimes * root. 27226f9a767SRodney W. Grimes */ 273c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 27426f9a767SRodney W. Grimes p->p_traceflag = 0; 27526f9a767SRodney W. Grimes vrele(p->p_tracep); 276c52007c2SDavid Greenman p->p_tracep = NULL; 27726f9a767SRodney W. Grimes } 278c52007c2SDavid Greenman /* 279c52007c2SDavid Greenman * Set the new credentials. 280c52007c2SDavid Greenman */ 28126f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 282c52007c2SDavid Greenman if (attr.va_mode & VSUID) 28326f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 284c52007c2SDavid Greenman if (attr.va_mode & VSGID) 28526f9a767SRodney W. Grimes p->p_ucred->cr_groups[0] = attr.va_gid; 28626f9a767SRodney W. Grimes p->p_flag |= P_SUGID; 287c52007c2SDavid Greenman } else { 288c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 28926f9a767SRodney W. Grimes } 29026f9a767SRodney W. Grimes 29126f9a767SRodney W. Grimes /* 292c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 29326f9a767SRodney W. Grimes */ 29426f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 29526f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 29626f9a767SRodney W. Grimes 29726f9a767SRodney W. Grimes /* 2982a531c80SDavid Greenman * Store the vp for use in procfs 2992a531c80SDavid Greenman */ 3002a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3012a531c80SDavid Greenman vrele(p->p_textvp); 3022a531c80SDavid Greenman VREF(ndp->ni_vp); 3032a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3042a531c80SDavid Greenman 3052a531c80SDavid Greenman /* 30626f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 30726f9a767SRodney W. Grimes * can be set before the program executes. 30826f9a767SRodney W. Grimes */ 30926f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 31026f9a767SRodney W. Grimes psignal(p, SIGTRAP); 31126f9a767SRodney W. Grimes 31226f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 31326f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 31426f9a767SRodney W. Grimes 31526f9a767SRodney W. Grimes /* Set entry address */ 316c52007c2SDavid Greenman setregs(p, imgp->entry_addr, (u_long)stack_base); 31726f9a767SRodney W. Grimes 31826f9a767SRodney W. Grimes /* 31926f9a767SRodney W. Grimes * free various allocated resources 32026f9a767SRodney W. Grimes */ 321c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 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 (2)"); 325f5277ae7SDavid Greenman vrele(ndp->ni_vp); 32626f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 32726f9a767SRodney W. Grimes 32826f9a767SRodney W. Grimes return (0); 32926f9a767SRodney W. Grimes 33026f9a767SRodney W. Grimes exec_fail_dealloc: 331c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 332c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 333c52007c2SDavid Greenman if (imgp->image_header && imgp->image_header != (char *)-1) 334c52007c2SDavid Greenman if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header, 335c52007c2SDavid Greenman (vm_offset_t)imgp->image_header + PAGE_SIZE)) 33626f9a767SRodney W. Grimes panic("execve: header dealloc failed (3)"); 337f5277ae7SDavid Greenman if (ndp->ni_vp) 338f5277ae7SDavid Greenman vrele(ndp->ni_vp); 33926f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 34026f9a767SRodney W. Grimes 34126f9a767SRodney W. Grimes exec_fail: 342c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 34326f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 34426f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 34526f9a767SRodney W. Grimes /* NOT REACHED */ 34626f9a767SRodney W. Grimes return(0); 34726f9a767SRodney W. Grimes } else { 34826f9a767SRodney W. Grimes return(error); 34926f9a767SRodney W. Grimes } 35026f9a767SRodney W. Grimes } 35126f9a767SRodney W. Grimes 35226f9a767SRodney W. Grimes /* 35326f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 35426f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 35526f9a767SRodney W. Grimes * automatically in trap.c. 35626f9a767SRodney W. Grimes */ 35726f9a767SRodney W. Grimes int 358c52007c2SDavid Greenman exec_new_vmspace(imgp) 359c52007c2SDavid Greenman struct image_params *imgp; 36026f9a767SRodney W. Grimes { 36126f9a767SRodney W. Grimes int error; 362c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 36326f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 36426f9a767SRodney W. Grimes 365c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 36626f9a767SRodney W. Grimes 36726f9a767SRodney W. Grimes /* Blow away entire process VM */ 3683d903220SDoug Rabson if (vmspace->vm_shm) 369c52007c2SDavid Greenman shmexit(imgp->proc); 37068940ac1SDavid Greenman vm_map_remove(&vmspace->vm_map, 0, USRSTACK); 37126f9a767SRodney W. Grimes 37226f9a767SRodney W. Grimes /* Allocate a new stack */ 37368940ac1SDavid Greenman error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr, 374bd7e5f99SJohn Dyson SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 37526f9a767SRodney W. Grimes if (error) 37626f9a767SRodney W. Grimes return(error); 37726f9a767SRodney W. Grimes 37826f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 37926f9a767SRodney W. Grimes 38026f9a767SRodney W. Grimes /* Initialize maximum stack address */ 38126f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 38226f9a767SRodney W. Grimes 38326f9a767SRodney W. Grimes return(0); 38426f9a767SRodney W. Grimes } 38526f9a767SRodney W. Grimes 38626f9a767SRodney W. Grimes /* 38726f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 38826f9a767SRodney W. Grimes * address space into the temporary string buffer. 38926f9a767SRodney W. Grimes */ 39026f9a767SRodney W. Grimes int 391c52007c2SDavid Greenman exec_extract_strings(imgp) 392c52007c2SDavid Greenman struct image_params *imgp; 39326f9a767SRodney W. Grimes { 39426f9a767SRodney W. Grimes char **argv, **envv; 39526f9a767SRodney W. Grimes char *argp, *envp; 3960fd1a014SDavid Greenman int error, length; 39726f9a767SRodney W. Grimes 39826f9a767SRodney W. Grimes /* 39926f9a767SRodney W. Grimes * extract arguments first 40026f9a767SRodney W. Grimes */ 40126f9a767SRodney W. Grimes 402c52007c2SDavid Greenman argv = imgp->uap->argv; 40326f9a767SRodney W. Grimes 4040fd1a014SDavid Greenman if (argv) { 405bb56ec4aSPoul-Henning Kamp while ((argp = (caddr_t) fuword(argv++))) { 40626f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 40726f9a767SRodney W. Grimes return (EFAULT); 408c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 409c52007c2SDavid Greenman imgp->stringspace, &length))) { 4100fd1a014SDavid Greenman if (error == ENAMETOOLONG) 41126f9a767SRodney W. Grimes return(E2BIG); 4120fd1a014SDavid Greenman return (error); 4130fd1a014SDavid Greenman } 414c52007c2SDavid Greenman imgp->stringspace -= length; 415c52007c2SDavid Greenman imgp->stringp += length; 416c52007c2SDavid Greenman imgp->argc++; 41726f9a767SRodney W. Grimes } 4180fd1a014SDavid Greenman } 41926f9a767SRodney W. Grimes 42026f9a767SRodney W. Grimes /* 42126f9a767SRodney W. Grimes * extract environment strings 42226f9a767SRodney W. Grimes */ 42326f9a767SRodney W. Grimes 424c52007c2SDavid Greenman envv = imgp->uap->envv; 42526f9a767SRodney W. Grimes 4260fd1a014SDavid Greenman if (envv) { 427bb56ec4aSPoul-Henning Kamp while ((envp = (caddr_t) fuword(envv++))) { 42826f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 42926f9a767SRodney W. Grimes return (EFAULT); 430c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 431c52007c2SDavid Greenman imgp->stringspace, &length))) { 4320fd1a014SDavid Greenman if (error == ENAMETOOLONG) 43326f9a767SRodney W. Grimes return(E2BIG); 4340fd1a014SDavid Greenman return (error); 4350fd1a014SDavid Greenman } 436c52007c2SDavid Greenman imgp->stringspace -= length; 437c52007c2SDavid Greenman imgp->stringp += length; 438c52007c2SDavid Greenman imgp->envc++; 43926f9a767SRodney W. Grimes } 4400fd1a014SDavid Greenman } 44126f9a767SRodney W. Grimes 44226f9a767SRodney W. Grimes return (0); 44326f9a767SRodney W. Grimes } 44426f9a767SRodney W. Grimes 44526f9a767SRodney W. Grimes /* 44626f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 44726f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 44826f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 44926f9a767SRodney W. Grimes */ 45026f9a767SRodney W. Grimes int * 451c52007c2SDavid Greenman exec_copyout_strings(imgp) 452c52007c2SDavid Greenman struct image_params *imgp; 45326f9a767SRodney W. Grimes { 45426f9a767SRodney W. Grimes int argc, envc; 45526f9a767SRodney W. Grimes char **vectp; 45626f9a767SRodney W. Grimes char *stringp, *destp; 45726f9a767SRodney W. Grimes int *stack_base; 45893f6448cSDavid Greenman struct ps_strings *arginfo; 45926f9a767SRodney W. Grimes 46026f9a767SRodney W. Grimes /* 46126f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 46226f9a767SRodney W. Grimes */ 46393f6448cSDavid Greenman arginfo = PS_STRINGS; 4641ed012f9SPeter Wemm destp = (caddr_t)arginfo - SPARE_USRSPACE - 4651ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 4661ed012f9SPeter Wemm 46726f9a767SRodney W. Grimes /* 46826f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 46926f9a767SRodney W. Grimes * arg and env vector sets 47026f9a767SRodney W. Grimes */ 47126f9a767SRodney W. Grimes vectp = (char **) (destp - 472c52007c2SDavid Greenman (imgp->argc + imgp->envc + 2) * sizeof(char *)); 47326f9a767SRodney W. Grimes 47426f9a767SRodney W. Grimes /* 47526f9a767SRodney W. Grimes * vectp also becomes our initial stack base 47626f9a767SRodney W. Grimes */ 47726f9a767SRodney W. Grimes stack_base = (int *)vectp; 47826f9a767SRodney W. Grimes 479c52007c2SDavid Greenman stringp = imgp->stringbase; 480c52007c2SDavid Greenman argc = imgp->argc; 481c52007c2SDavid Greenman envc = imgp->envc; 48226f9a767SRodney W. Grimes 48393f6448cSDavid Greenman /* 4843aed948bSDavid Greenman * Copy out strings - arguments and environment. 48593f6448cSDavid Greenman */ 486c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 48793f6448cSDavid Greenman 48893f6448cSDavid Greenman /* 4893aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 4903aed948bSDavid Greenman */ 4911ed012f9SPeter Wemm suword(&arginfo->ps_argvstr, (int)vectp); 4923aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 4933aed948bSDavid Greenman 4943aed948bSDavid Greenman /* 4953aed948bSDavid Greenman * Fill in argument portion of vector table. 49693f6448cSDavid Greenman */ 49726f9a767SRodney W. Grimes for (; argc > 0; --argc) { 4983aed948bSDavid Greenman suword(vectp++, (int)destp); 4993aed948bSDavid Greenman while (*stringp++ != 0) 5003aed948bSDavid Greenman destp++; 5013aed948bSDavid Greenman destp++; 50226f9a767SRodney W. Grimes } 50326f9a767SRodney W. Grimes 50426f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 5053aed948bSDavid Greenman suword(vectp++, NULL); 50626f9a767SRodney W. Grimes 5071ed012f9SPeter Wemm suword(&arginfo->ps_envstr, (int)vectp); 5083aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 50993f6448cSDavid Greenman 51093f6448cSDavid Greenman /* 5113aed948bSDavid Greenman * Fill in environment portion of vector table. 51293f6448cSDavid Greenman */ 51326f9a767SRodney W. Grimes for (; envc > 0; --envc) { 5143aed948bSDavid Greenman suword(vectp++, (int)destp); 5153aed948bSDavid Greenman while (*stringp++ != 0) 5163aed948bSDavid Greenman destp++; 5173aed948bSDavid Greenman destp++; 51826f9a767SRodney W. Grimes } 51926f9a767SRodney W. Grimes 52026f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 5213aed948bSDavid Greenman suword(vectp, NULL); 52226f9a767SRodney W. Grimes 52326f9a767SRodney W. Grimes return (stack_base); 52426f9a767SRodney W. Grimes } 52526f9a767SRodney W. Grimes 52626f9a767SRodney W. Grimes /* 52726f9a767SRodney W. Grimes * Check permissions of file to execute. 52826f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 52926f9a767SRodney W. Grimes */ 530f23b4c91SGarrett Wollman static int 531c52007c2SDavid Greenman exec_check_permissions(imgp) 532c52007c2SDavid Greenman struct image_params *imgp; 53326f9a767SRodney W. Grimes { 534c52007c2SDavid Greenman struct proc *p = imgp->proc; 535c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 536c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 53726f9a767SRodney W. Grimes int error; 53826f9a767SRodney W. Grimes 53926f9a767SRodney W. Grimes /* 54026f9a767SRodney W. Grimes * Check number of open-for-writes on the file and deny execution 54126f9a767SRodney W. Grimes * if there are any. 54226f9a767SRodney W. Grimes */ 543c52007c2SDavid Greenman if (vp->v_writecount) { 54426f9a767SRodney W. Grimes return (ETXTBSY); 54526f9a767SRodney W. Grimes } 54626f9a767SRodney W. Grimes 54726f9a767SRodney W. Grimes /* Get file attributes */ 548c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 54926f9a767SRodney W. Grimes if (error) 55026f9a767SRodney W. Grimes return (error); 55126f9a767SRodney W. Grimes 55226f9a767SRodney W. Grimes /* 55326f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 55426f9a767SRodney W. Grimes * file resides on. 55526f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 55626f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 55726f9a767SRodney W. Grimes * file really is executable. 55826f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 55926f9a767SRodney W. Grimes */ 560c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 56126f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 56226f9a767SRodney W. Grimes (attr->va_type != VREG)) { 56326f9a767SRodney W. Grimes return (EACCES); 56426f9a767SRodney W. Grimes } 56526f9a767SRodney W. Grimes 56626f9a767SRodney W. Grimes /* 56726f9a767SRodney W. Grimes * Zero length files can't be exec'd 56826f9a767SRodney W. Grimes */ 56926f9a767SRodney W. Grimes if (attr->va_size == 0) 57026f9a767SRodney W. Grimes return (ENOEXEC); 57126f9a767SRodney W. Grimes 57226f9a767SRodney W. Grimes /* 57326f9a767SRodney W. Grimes * Disable setuid/setgid if the filesystem prohibits it or if 57426f9a767SRodney W. Grimes * the process is being traced. 57526f9a767SRodney W. Grimes */ 576c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 57726f9a767SRodney W. Grimes attr->va_mode &= ~(VSUID | VSGID); 57826f9a767SRodney W. Grimes 57926f9a767SRodney W. Grimes /* 58026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 58126f9a767SRodney W. Grimes * Then call filesystem specific open routine (which does nothing 58226f9a767SRodney W. Grimes * in the general case). 58326f9a767SRodney W. Grimes */ 584c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 58526f9a767SRodney W. Grimes if (error) 58626f9a767SRodney W. Grimes return (error); 58726f9a767SRodney W. Grimes 588c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 58926f9a767SRodney W. Grimes if (error) 59026f9a767SRodney W. Grimes return (error); 59126f9a767SRodney W. Grimes 59226f9a767SRodney W. Grimes return (0); 593df8bae1dSRodney W. Grimes } 594