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 * 261616db3cSJohn Dyson * $Id: kern_exec.c,v 1.73 1998/01/06 05:15:34 dyson 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> 42a794e791SBruce Evans #include <sys/proc.h> 432a024a2bSSean Eric Fagan #include <sys/pioctl.h> 4426f9a767SRodney W. Grimes #include <sys/malloc.h> 45a794e791SBruce Evans #include <sys/namei.h> 461e1e0b44SSøren Schmidt #include <sys/sysent.h> 47797f2d22SPoul-Henning Kamp #include <sys/shm.h> 4899ac3bc8SPeter Wemm #include <sys/sysctl.h> 49a794e791SBruce Evans #include <sys/vnode.h> 509caaadb6SDavid Greenman #include <sys/buf.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> 55996c772fSJohn Dyson #include <sys/lock.h> 56efeaf95aSDavid Greenman #include <vm/pmap.h> 571616db3cSJohn Dyson #include <vm/vm_page.h> 58efeaf95aSDavid Greenman #include <vm/vm_map.h> 5926f9a767SRodney W. Grimes #include <vm/vm_kern.h> 60efeaf95aSDavid Greenman #include <vm/vm_extern.h> 611ebd0c59SDavid Greenman #include <vm/vm_object.h> 62675ea6f0SBruce Evans #include <vm/vm_zone.h> 631616db3cSJohn Dyson #include <vm/vm_pager.h> 641616db3cSJohn Dyson #include <vm/vm_pageout.h> 6526f9a767SRodney W. Grimes 6626f9a767SRodney W. Grimes #include <machine/reg.h> 6726f9a767SRodney W. Grimes 6887b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *)); 69df8bae1dSRodney W. Grimes 701616db3cSJohn Dyson static int exec_check_permissions __P((struct image_params *)); 711616db3cSJohn Dyson static int exec_map_first_page __P((struct image_params *)); 721616db3cSJohn Dyson static void exec_unmap_first_page __P((struct image_params *)); 73f23b4c91SGarrett Wollman 74df8bae1dSRodney W. Grimes /* 7599ac3bc8SPeter Wemm * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts 7699ac3bc8SPeter Wemm * of the sysctl code also assumes this, and sizeof(int) == sizeof(long). 7799ac3bc8SPeter Wemm */ 786120fef1SDavid Greenman static struct ps_strings *ps_strings = PS_STRINGS; 7999ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, ""); 8099ac3bc8SPeter Wemm 8199ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK; 8299ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, ""); 8399ac3bc8SPeter Wemm 8499ac3bc8SPeter Wemm /* 8526f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 8626f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 87df8bae1dSRodney W. Grimes */ 8887b6de2bSPoul-Henning Kamp static const struct execsw **execsw = 8987b6de2bSPoul-Henning Kamp (const struct execsw **)&execsw_set.ls_items[0]; 9026f9a767SRodney W. Grimes 91d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 92ad7507e2SSteven Wallace struct execve_args { 93ad7507e2SSteven Wallace char *fname; 94ad7507e2SSteven Wallace char **argv; 95ad7507e2SSteven Wallace char **envv; 96ad7507e2SSteven Wallace }; 97d2d3e875SBruce Evans #endif 98ad7507e2SSteven Wallace 9926f9a767SRodney W. Grimes /* 10026f9a767SRodney W. Grimes * execve() system call. 10126f9a767SRodney W. Grimes */ 10226f9a767SRodney W. Grimes int 103cb226aaaSPoul-Henning Kamp execve(p, uap) 10426f9a767SRodney W. Grimes struct proc *p; 10526f9a767SRodney W. Grimes register struct execve_args *uap; 106df8bae1dSRodney W. Grimes { 10726f9a767SRodney W. Grimes struct nameidata nd, *ndp; 10826f9a767SRodney W. Grimes int *stack_base; 109bb56ec4aSPoul-Henning Kamp int error, len, i; 110c52007c2SDavid Greenman struct image_params image_params, *imgp; 11126f9a767SRodney W. Grimes struct vattr attr; 1129caaadb6SDavid Greenman struct buf *bp = NULL; 11326f9a767SRodney W. Grimes 114c52007c2SDavid Greenman imgp = &image_params; 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes /* 117c52007c2SDavid Greenman * Initialize part of the common data 118df8bae1dSRodney W. Grimes */ 119c52007c2SDavid Greenman imgp->proc = p; 120c52007c2SDavid Greenman imgp->uap = uap; 121c52007c2SDavid Greenman imgp->attr = &attr; 122c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1235cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 124c52007c2SDavid Greenman imgp->entry_addr = 0; 125c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 126c52007c2SDavid Greenman imgp->interpreted = 0; 127c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 128e1743d02SSøren Schmidt imgp->auxargs = NULL; 1291616db3cSJohn Dyson imgp->vp = NULL; 1301616db3cSJohn Dyson imgp->firstpage = NULL; 13126f9a767SRodney W. Grimes 13226f9a767SRodney W. Grimes /* 13326f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 13426f9a767SRodney W. Grimes * environment strings 13526f9a767SRodney W. Grimes */ 1361616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 137c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 13826f9a767SRodney W. Grimes error = ENOMEM; 13926f9a767SRodney W. Grimes goto exec_fail; 14026f9a767SRodney W. Grimes } 141c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 142c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1431616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 14426f9a767SRodney W. Grimes 14526f9a767SRodney W. Grimes /* 14626f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 14726f9a767SRodney W. Grimes * in ni_vp amoung other things. 14826f9a767SRodney W. Grimes */ 14926f9a767SRodney W. Grimes ndp = &nd; 150b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1513ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 15226f9a767SRodney W. Grimes 15326f9a767SRodney W. Grimes interpret: 15426f9a767SRodney W. Grimes 15526f9a767SRodney W. Grimes error = namei(ndp); 15626f9a767SRodney W. Grimes if (error) { 1571616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1581616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 15926f9a767SRodney W. Grimes goto exec_fail; 16026f9a767SRodney W. Grimes } 16126f9a767SRodney W. Grimes 162c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 16326f9a767SRodney W. Grimes 16426f9a767SRodney W. Grimes /* 1659022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1669022e4adSDavid Greenman */ 167c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1688677f509SDavid Greenman if (error) { 1698677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 17026f9a767SRodney W. Grimes goto exec_fail_dealloc; 1718677f509SDavid Greenman } 17226f9a767SRodney W. Grimes 1731616db3cSJohn Dyson error = exec_map_first_page(imgp); 1749caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1756d5a0a8cSDavid Greenman if (error) 17626f9a767SRodney W. Grimes goto exec_fail_dealloc; 17726f9a767SRodney W. Grimes 17826f9a767SRodney W. Grimes /* 17926f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 18026f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 18126f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 18226f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 18326f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 18426f9a767SRodney W. Grimes * the interpreter. 18526f9a767SRodney W. Grimes */ 18626f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 18726f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 188c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 18926f9a767SRodney W. Grimes else 19026f9a767SRodney W. Grimes continue; 19126f9a767SRodney W. Grimes if (error == -1) 19226f9a767SRodney W. Grimes continue; 19326f9a767SRodney W. Grimes if (error) 19426f9a767SRodney W. Grimes goto exec_fail_dealloc; 195c52007c2SDavid Greenman if (imgp->interpreted) { 1961616db3cSJohn Dyson exec_unmap_first_page(imgp); 19726f9a767SRodney W. Grimes /* free old vnode and name buffer */ 198f5277ae7SDavid Greenman vrele(ndp->ni_vp); 19999448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 20026f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 201b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 202c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 20326f9a767SRodney W. Grimes goto interpret; 20426f9a767SRodney W. Grimes } 20526f9a767SRodney W. Grimes break; 20626f9a767SRodney W. Grimes } 20726f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 20826f9a767SRodney W. Grimes if (error == -1) { 20926f9a767SRodney W. Grimes error = ENOEXEC; 21026f9a767SRodney W. Grimes goto exec_fail_dealloc; 21126f9a767SRodney W. Grimes } 21226f9a767SRodney W. Grimes 21326f9a767SRodney W. Grimes /* 21426f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 21526f9a767SRodney W. Grimes */ 216c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 21726f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 21826f9a767SRodney W. Grimes 21926f9a767SRodney W. Grimes /* 2201e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2211e1e0b44SSøren Schmidt * let it do the stack setup. 2221e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 22326f9a767SRodney W. Grimes */ 2241e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 225c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2261e1e0b44SSøren Schmidt else 227c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 22826f9a767SRodney W. Grimes 229a78e8d2aSDavid Greenman /* 230a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 231a78e8d2aSDavid Greenman * be shared after an exec. 232a78e8d2aSDavid Greenman */ 233a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 234a78e8d2aSDavid Greenman struct filedesc *tmp; 235a78e8d2aSDavid Greenman 236a78e8d2aSDavid Greenman tmp = fdcopy(p); 237a78e8d2aSDavid Greenman fdfree(p); 238a78e8d2aSDavid Greenman p->p_fd = tmp; 239a78e8d2aSDavid Greenman } 240a78e8d2aSDavid Greenman 24126f9a767SRodney W. Grimes /* close files on exec */ 24226f9a767SRodney W. Grimes fdcloseexec(p); 24326f9a767SRodney W. Grimes 24426f9a767SRodney W. Grimes /* reset caught signals */ 24526f9a767SRodney W. Grimes execsigs(p); 24626f9a767SRodney W. Grimes 24726f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 24826f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 24926f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 25026f9a767SRodney W. Grimes p->p_comm[len] = 0; 25126f9a767SRodney W. Grimes 25226f9a767SRodney W. Grimes /* 25324b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 25426f9a767SRodney W. Grimes * it that it now has it's own resources back 25526f9a767SRodney W. Grimes */ 25626f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 25726f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 25826f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 25926f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 26026f9a767SRodney W. Grimes } 26126f9a767SRodney W. Grimes 26226f9a767SRodney W. Grimes /* 263a78e8d2aSDavid Greenman * Implement image setuid/setgid. 264a78e8d2aSDavid Greenman * 265a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 266a78e8d2aSDavid Greenman * the process is being traced. 267c52007c2SDavid Greenman */ 268d021ae3dSGuido van Rooij if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid || 269d021ae3dSGuido van Rooij attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) && 270a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 271c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 272c52007c2SDavid Greenman /* 273c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 27426f9a767SRodney W. Grimes * root. 27526f9a767SRodney W. Grimes */ 276c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 27726f9a767SRodney W. Grimes p->p_traceflag = 0; 27826f9a767SRodney W. Grimes vrele(p->p_tracep); 279c52007c2SDavid Greenman p->p_tracep = NULL; 28026f9a767SRodney W. Grimes } 281c52007c2SDavid Greenman /* 282c52007c2SDavid Greenman * Set the new credentials. 283c52007c2SDavid Greenman */ 28426f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 285c52007c2SDavid Greenman if (attr.va_mode & VSUID) 28626f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 287c52007c2SDavid Greenman if (attr.va_mode & VSGID) 288d021ae3dSGuido van Rooij p->p_ucred->cr_gid = attr.va_gid; 289d5f81602SSean Eric Fagan setsugid(p); 290c52007c2SDavid Greenman } else { 291e47bda07SDavid Greenman if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 292e47bda07SDavid Greenman p->p_ucred->cr_gid == p->p_cred->p_rgid) 293c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 29426f9a767SRodney W. Grimes } 29526f9a767SRodney W. Grimes 29626f9a767SRodney W. Grimes /* 297c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 29826f9a767SRodney W. Grimes */ 29926f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 30026f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 30126f9a767SRodney W. Grimes 30226f9a767SRodney W. Grimes /* 3032a531c80SDavid Greenman * Store the vp for use in procfs 3042a531c80SDavid Greenman */ 3052a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3062a531c80SDavid Greenman vrele(p->p_textvp); 3072a531c80SDavid Greenman VREF(ndp->ni_vp); 3082a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3092a531c80SDavid Greenman 3102a531c80SDavid Greenman /* 31126f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 31226f9a767SRodney W. Grimes * can be set before the program executes. 31326f9a767SRodney W. Grimes */ 3142a024a2bSSean Eric Fagan STOPEVENT(p, S_EXEC, 0); 3152a024a2bSSean Eric Fagan 31626f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 31726f9a767SRodney W. Grimes psignal(p, SIGTRAP); 31826f9a767SRodney W. Grimes 31926f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 32026f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 32126f9a767SRodney W. Grimes 32226f9a767SRodney W. Grimes /* Set entry address */ 323c52007c2SDavid Greenman setregs(p, imgp->entry_addr, (u_long)stack_base); 32426f9a767SRodney W. Grimes 3251616db3cSJohn Dyson exec_fail_dealloc: 3261616db3cSJohn Dyson 32726f9a767SRodney W. Grimes /* 32826f9a767SRodney W. Grimes * free various allocated resources 32926f9a767SRodney W. Grimes */ 3301616db3cSJohn Dyson if (imgp->firstpage) 3311616db3cSJohn Dyson exec_unmap_first_page(imgp); 33226f9a767SRodney W. Grimes 333c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 3341616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 3351616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 3361616db3cSJohn Dyson 337a3cf6ebaSDavid Greenman if (ndp->ni_vp) { 338f5277ae7SDavid Greenman vrele(ndp->ni_vp); 33999448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 340a3cf6ebaSDavid Greenman } 34126f9a767SRodney W. Grimes 3421616db3cSJohn Dyson if (error == 0) 3431616db3cSJohn Dyson return (0); 3441616db3cSJohn Dyson 34526f9a767SRodney W. Grimes exec_fail: 346c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 34726f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 34826f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 34926f9a767SRodney W. Grimes /* NOT REACHED */ 35026f9a767SRodney W. Grimes return(0); 35126f9a767SRodney W. Grimes } else { 35226f9a767SRodney W. Grimes return(error); 35326f9a767SRodney W. Grimes } 35426f9a767SRodney W. Grimes } 35526f9a767SRodney W. Grimes 3561616db3cSJohn Dyson int 3571616db3cSJohn Dyson exec_map_first_page(imgp) 3581616db3cSJohn Dyson struct image_params *imgp; 3591616db3cSJohn Dyson { 3601616db3cSJohn Dyson int s; 3611616db3cSJohn Dyson vm_page_t m; 3621616db3cSJohn Dyson vm_object_t object; 3631616db3cSJohn Dyson 3641616db3cSJohn Dyson 3651616db3cSJohn Dyson if (imgp->firstpage) { 3661616db3cSJohn Dyson exec_unmap_first_page(imgp); 3671616db3cSJohn Dyson } 3681616db3cSJohn Dyson 3691616db3cSJohn Dyson object = imgp->vp->v_object; 3701616db3cSJohn Dyson s = splvm(); 3711616db3cSJohn Dyson 3721616db3cSJohn Dyson retry: 3731616db3cSJohn Dyson m = vm_page_lookup(object, 0); 3741616db3cSJohn Dyson if (m == NULL) { 3751616db3cSJohn Dyson m = vm_page_alloc(object, 0, VM_ALLOC_NORMAL); 3761616db3cSJohn Dyson if (m == NULL) { 3771616db3cSJohn Dyson VM_WAIT; 3781616db3cSJohn Dyson goto retry; 3791616db3cSJohn Dyson } 3801616db3cSJohn Dyson } else if ((m->flags & PG_BUSY) || m->busy) { 3811616db3cSJohn Dyson m->flags |= PG_WANTED; 3821616db3cSJohn Dyson tsleep(m, PVM, "execpw", 0); 3831616db3cSJohn Dyson goto retry; 3841616db3cSJohn Dyson } 3851616db3cSJohn Dyson 3861616db3cSJohn Dyson m->flags |= PG_BUSY; 3871616db3cSJohn Dyson 3881616db3cSJohn Dyson if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 3891616db3cSJohn Dyson int rv; 3901616db3cSJohn Dyson rv = vm_pager_get_pages(object, &m, 1, 0); 3911616db3cSJohn Dyson if (rv != VM_PAGER_OK) { 3921616db3cSJohn Dyson vm_page_protect(m, VM_PROT_NONE); 3931616db3cSJohn Dyson vm_page_deactivate(m); 3941616db3cSJohn Dyson PAGE_WAKEUP(m); 3951616db3cSJohn Dyson splx(s); 3961616db3cSJohn Dyson return EIO; 3971616db3cSJohn Dyson } 3981616db3cSJohn Dyson } 3991616db3cSJohn Dyson 4001616db3cSJohn Dyson vm_page_wire(m); 4011616db3cSJohn Dyson PAGE_WAKEUP(m); 4021616db3cSJohn Dyson splx(s); 4031616db3cSJohn Dyson 4041616db3cSJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(m)); 4051616db3cSJohn Dyson imgp->firstpage = m; 4061616db3cSJohn Dyson 4071616db3cSJohn Dyson return 0; 4081616db3cSJohn Dyson } 4091616db3cSJohn Dyson 4101616db3cSJohn Dyson void 4111616db3cSJohn Dyson exec_unmap_first_page(imgp) 4121616db3cSJohn Dyson struct image_params *imgp; 4131616db3cSJohn Dyson { 4141616db3cSJohn Dyson if (imgp->firstpage) { 4151616db3cSJohn Dyson pmap_kremove((vm_offset_t) imgp->image_header); 4161616db3cSJohn Dyson vm_page_unwire(imgp->firstpage); 4171616db3cSJohn Dyson imgp->firstpage = NULL; 4181616db3cSJohn Dyson } 4191616db3cSJohn Dyson } 4201616db3cSJohn Dyson 42126f9a767SRodney W. Grimes /* 42226f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 42326f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 42426f9a767SRodney W. Grimes * automatically in trap.c. 42526f9a767SRodney W. Grimes */ 42626f9a767SRodney W. Grimes int 427c52007c2SDavid Greenman exec_new_vmspace(imgp) 428c52007c2SDavid Greenman struct image_params *imgp; 42926f9a767SRodney W. Grimes { 43026f9a767SRodney W. Grimes int error; 431c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 43226f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 433af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 43426f9a767SRodney W. Grimes 435c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 43626f9a767SRodney W. Grimes 437af9ec885SJohn Dyson /* 438af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 439af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 440af9ec885SJohn Dyson * not disrupted 441af9ec885SJohn Dyson */ 442af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 4433d903220SDoug Rabson if (vmspace->vm_shm) 444c52007c2SDavid Greenman shmexit(imgp->proc); 4459d3fbbb5SJohn Dyson pmap_remove_pages(&vmspace->vm_pmap, 0, USRSTACK); 446af9ec885SJohn Dyson vm_map_remove(map, 0, USRSTACK); 447af9ec885SJohn Dyson } else { 448492da96cSJohn Dyson vmspace_exec(imgp->proc); 449492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 450af9ec885SJohn Dyson map = &vmspace->vm_map; 451af9ec885SJohn Dyson } 45226f9a767SRodney W. Grimes 45326f9a767SRodney W. Grimes /* Allocate a new stack */ 4541616db3cSJohn Dyson error = vm_map_insert(&vmspace->vm_map, NULL, 0, 4551616db3cSJohn Dyson (vm_offset_t) stack_addr, (vm_offset_t) USRSTACK, 4561616db3cSJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 0); 45726f9a767SRodney W. Grimes if (error) 45826f9a767SRodney W. Grimes return (error); 45926f9a767SRodney W. Grimes 46026f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 46126f9a767SRodney W. Grimes 46226f9a767SRodney W. Grimes /* Initialize maximum stack address */ 46326f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 46426f9a767SRodney W. Grimes 46526f9a767SRodney W. Grimes return(0); 46626f9a767SRodney W. Grimes } 46726f9a767SRodney W. Grimes 46826f9a767SRodney W. Grimes /* 46926f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 47026f9a767SRodney W. Grimes * address space into the temporary string buffer. 47126f9a767SRodney W. Grimes */ 47226f9a767SRodney W. Grimes int 473c52007c2SDavid Greenman exec_extract_strings(imgp) 474c52007c2SDavid Greenman struct image_params *imgp; 47526f9a767SRodney W. Grimes { 47626f9a767SRodney W. Grimes char **argv, **envv; 47726f9a767SRodney W. Grimes char *argp, *envp; 4780fd1a014SDavid Greenman int error, length; 47926f9a767SRodney W. Grimes 48026f9a767SRodney W. Grimes /* 48126f9a767SRodney W. Grimes * extract arguments first 48226f9a767SRodney W. Grimes */ 48326f9a767SRodney W. Grimes 484c52007c2SDavid Greenman argv = imgp->uap->argv; 48526f9a767SRodney W. Grimes 4860fd1a014SDavid Greenman if (argv) { 4875cf3d12cSAndrey A. Chernov argp = (caddr_t) fuword(argv); 4885cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 4895cf3d12cSAndrey A. Chernov return (EFAULT); 4905cf3d12cSAndrey A. Chernov if (argp) 4915cf3d12cSAndrey A. Chernov argv++; 4925cf3d12cSAndrey A. Chernov if (imgp->argv0) 4935cf3d12cSAndrey A. Chernov argp = imgp->argv0; 4945cf3d12cSAndrey A. Chernov if (argp) { 4955cf3d12cSAndrey A. Chernov do { 49626f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 49726f9a767SRodney W. Grimes return (EFAULT); 498c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 499c52007c2SDavid Greenman imgp->stringspace, &length))) { 5000fd1a014SDavid Greenman if (error == ENAMETOOLONG) 50126f9a767SRodney W. Grimes return(E2BIG); 5020fd1a014SDavid Greenman return (error); 5030fd1a014SDavid Greenman } 504c52007c2SDavid Greenman imgp->stringspace -= length; 505c52007c2SDavid Greenman imgp->stringp += length; 506c52007c2SDavid Greenman imgp->argc++; 5075cf3d12cSAndrey A. Chernov } while ((argp = (caddr_t) fuword(argv++))); 50826f9a767SRodney W. Grimes } 5090fd1a014SDavid Greenman } 51026f9a767SRodney W. Grimes 51126f9a767SRodney W. Grimes /* 51226f9a767SRodney W. Grimes * extract environment strings 51326f9a767SRodney W. Grimes */ 51426f9a767SRodney W. Grimes 515c52007c2SDavid Greenman envv = imgp->uap->envv; 51626f9a767SRodney W. Grimes 5170fd1a014SDavid Greenman if (envv) { 518bb56ec4aSPoul-Henning Kamp while ((envp = (caddr_t) fuword(envv++))) { 51926f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 52026f9a767SRodney W. Grimes return (EFAULT); 521c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 522c52007c2SDavid Greenman imgp->stringspace, &length))) { 5230fd1a014SDavid Greenman if (error == ENAMETOOLONG) 52426f9a767SRodney W. Grimes return(E2BIG); 5250fd1a014SDavid Greenman return (error); 5260fd1a014SDavid Greenman } 527c52007c2SDavid Greenman imgp->stringspace -= length; 528c52007c2SDavid Greenman imgp->stringp += length; 529c52007c2SDavid Greenman imgp->envc++; 53026f9a767SRodney W. Grimes } 5310fd1a014SDavid Greenman } 53226f9a767SRodney W. Grimes 53326f9a767SRodney W. Grimes return (0); 53426f9a767SRodney W. Grimes } 53526f9a767SRodney W. Grimes 53626f9a767SRodney W. Grimes /* 53726f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 53826f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 53926f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 54026f9a767SRodney W. Grimes */ 54126f9a767SRodney W. Grimes int * 542c52007c2SDavid Greenman exec_copyout_strings(imgp) 543c52007c2SDavid Greenman struct image_params *imgp; 54426f9a767SRodney W. Grimes { 54526f9a767SRodney W. Grimes int argc, envc; 54626f9a767SRodney W. Grimes char **vectp; 54726f9a767SRodney W. Grimes char *stringp, *destp; 54826f9a767SRodney W. Grimes int *stack_base; 54993f6448cSDavid Greenman struct ps_strings *arginfo; 550d66a5066SPeter Wemm int szsigcode; 55126f9a767SRodney W. Grimes 55226f9a767SRodney W. Grimes /* 55326f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 554d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 55526f9a767SRodney W. Grimes */ 55693f6448cSDavid Greenman arginfo = PS_STRINGS; 557d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 558d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 5591ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 5601ed012f9SPeter Wemm 56126f9a767SRodney W. Grimes /* 562d66a5066SPeter Wemm * install sigcode 563d66a5066SPeter Wemm */ 564d66a5066SPeter Wemm if (szsigcode) 565d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 566d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 567d66a5066SPeter Wemm 568d66a5066SPeter Wemm /* 569e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 570e1743d02SSøren Schmidt * on the stack. 571e1743d02SSøren Schmidt */ 572e1743d02SSøren Schmidt if (imgp->auxargs) 573e1743d02SSøren Schmidt /* 574e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 575e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 576e1743d02SSøren Schmidt * ELF Auxargs data. 577e1743d02SSøren Schmidt */ 578e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 579e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 580e1743d02SSøren Schmidt else 581e1743d02SSøren Schmidt /* 58226f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 58326f9a767SRodney W. Grimes * arg and env vector sets 58426f9a767SRodney W. Grimes */ 585e1743d02SSøren Schmidt vectp = (char **) 586e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 58726f9a767SRodney W. Grimes 58826f9a767SRodney W. Grimes /* 58926f9a767SRodney W. Grimes * vectp also becomes our initial stack base 59026f9a767SRodney W. Grimes */ 59126f9a767SRodney W. Grimes stack_base = (int *)vectp; 59226f9a767SRodney W. Grimes 593c52007c2SDavid Greenman stringp = imgp->stringbase; 594c52007c2SDavid Greenman argc = imgp->argc; 595c52007c2SDavid Greenman envc = imgp->envc; 59626f9a767SRodney W. Grimes 59793f6448cSDavid Greenman /* 5983aed948bSDavid Greenman * Copy out strings - arguments and environment. 59993f6448cSDavid Greenman */ 600c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 60193f6448cSDavid Greenman 60293f6448cSDavid Greenman /* 6033aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 6043aed948bSDavid Greenman */ 6051ed012f9SPeter Wemm suword(&arginfo->ps_argvstr, (int)vectp); 6063aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 6073aed948bSDavid Greenman 6083aed948bSDavid Greenman /* 6093aed948bSDavid Greenman * Fill in argument portion of vector table. 61093f6448cSDavid Greenman */ 61126f9a767SRodney W. Grimes for (; argc > 0; --argc) { 6123aed948bSDavid Greenman suword(vectp++, (int)destp); 6133aed948bSDavid Greenman while (*stringp++ != 0) 6143aed948bSDavid Greenman destp++; 6153aed948bSDavid Greenman destp++; 61626f9a767SRodney W. Grimes } 61726f9a767SRodney W. Grimes 61826f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 6196ab46d52SBruce Evans suword(vectp++, 0); 62026f9a767SRodney W. Grimes 6211ed012f9SPeter Wemm suword(&arginfo->ps_envstr, (int)vectp); 6223aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 62393f6448cSDavid Greenman 62493f6448cSDavid Greenman /* 6253aed948bSDavid Greenman * Fill in environment portion of vector table. 62693f6448cSDavid Greenman */ 62726f9a767SRodney W. Grimes for (; envc > 0; --envc) { 6283aed948bSDavid Greenman suword(vectp++, (int)destp); 6293aed948bSDavid Greenman while (*stringp++ != 0) 6303aed948bSDavid Greenman destp++; 6313aed948bSDavid Greenman destp++; 63226f9a767SRodney W. Grimes } 63326f9a767SRodney W. Grimes 63426f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 6356ab46d52SBruce Evans suword(vectp, 0); 63626f9a767SRodney W. Grimes 63726f9a767SRodney W. Grimes return (stack_base); 63826f9a767SRodney W. Grimes } 63926f9a767SRodney W. Grimes 64026f9a767SRodney W. Grimes /* 64126f9a767SRodney W. Grimes * Check permissions of file to execute. 64226f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 64326f9a767SRodney W. Grimes */ 644f23b4c91SGarrett Wollman static int 645c52007c2SDavid Greenman exec_check_permissions(imgp) 646c52007c2SDavid Greenman struct image_params *imgp; 64726f9a767SRodney W. Grimes { 648c52007c2SDavid Greenman struct proc *p = imgp->proc; 649c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 650c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 65126f9a767SRodney W. Grimes int error; 65226f9a767SRodney W. Grimes 65326f9a767SRodney W. Grimes /* Get file attributes */ 654c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 65526f9a767SRodney W. Grimes if (error) 65626f9a767SRodney W. Grimes return (error); 65726f9a767SRodney W. Grimes 65826f9a767SRodney W. Grimes /* 65926f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 66026f9a767SRodney W. Grimes * file resides on. 66126f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 66226f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 66326f9a767SRodney W. Grimes * file really is executable. 66426f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 66526f9a767SRodney W. Grimes */ 666c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 66726f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 66826f9a767SRodney W. Grimes (attr->va_type != VREG)) { 66926f9a767SRodney W. Grimes return (EACCES); 67026f9a767SRodney W. Grimes } 67126f9a767SRodney W. Grimes 67226f9a767SRodney W. Grimes /* 67326f9a767SRodney W. Grimes * Zero length files can't be exec'd 67426f9a767SRodney W. Grimes */ 67526f9a767SRodney W. Grimes if (attr->va_size == 0) 67626f9a767SRodney W. Grimes return (ENOEXEC); 67726f9a767SRodney W. Grimes 67826f9a767SRodney W. Grimes /* 67926f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 68026f9a767SRodney W. Grimes */ 681c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 68226f9a767SRodney W. Grimes if (error) 68326f9a767SRodney W. Grimes return (error); 68426f9a767SRodney W. Grimes 6856d5a0a8cSDavid Greenman /* 6866d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 6876d5a0a8cSDavid Greenman * if there are any. 6886d5a0a8cSDavid Greenman */ 6896d5a0a8cSDavid Greenman if (vp->v_writecount) 6906d5a0a8cSDavid Greenman return (ETXTBSY); 6916d5a0a8cSDavid Greenman 6926d5a0a8cSDavid Greenman /* 6936d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 6946d5a0a8cSDavid Greenman * general case). 6956d5a0a8cSDavid Greenman */ 696c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 69726f9a767SRodney W. Grimes if (error) 69826f9a767SRodney W. Grimes return (error); 69926f9a767SRodney W. Grimes 70026f9a767SRodney W. Grimes return (0); 701df8bae1dSRodney W. Grimes } 702