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 * 26ecbb00a2SDoug Rabson * $Id: kern_exec.c,v 1.82 1998/04/17 22:36:50 des 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> 44a794e791SBruce Evans #include <sys/namei.h> 451e1e0b44SSøren Schmidt #include <sys/sysent.h> 46797f2d22SPoul-Henning Kamp #include <sys/shm.h> 4799ac3bc8SPeter Wemm #include <sys/sysctl.h> 48a794e791SBruce Evans #include <sys/vnode.h> 499caaadb6SDavid Greenman #include <sys/buf.h> 5026f9a767SRodney W. Grimes 5126f9a767SRodney W. Grimes #include <vm/vm.h> 52efeaf95aSDavid Greenman #include <vm/vm_param.h> 53efeaf95aSDavid Greenman #include <vm/vm_prot.h> 54996c772fSJohn Dyson #include <sys/lock.h> 55efeaf95aSDavid Greenman #include <vm/pmap.h> 561616db3cSJohn Dyson #include <vm/vm_page.h> 57efeaf95aSDavid Greenman #include <vm/vm_map.h> 5826f9a767SRodney W. Grimes #include <vm/vm_kern.h> 59efeaf95aSDavid Greenman #include <vm/vm_extern.h> 601ebd0c59SDavid Greenman #include <vm/vm_object.h> 61675ea6f0SBruce Evans #include <vm/vm_zone.h> 621616db3cSJohn Dyson #include <vm/vm_pager.h> 6326f9a767SRodney W. Grimes 6426f9a767SRodney W. Grimes #include <machine/reg.h> 6526f9a767SRodney W. Grimes 66ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *)); 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes /* 6999ac3bc8SPeter Wemm * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts 7099ac3bc8SPeter Wemm * of the sysctl code also assumes this, and sizeof(int) == sizeof(long). 7199ac3bc8SPeter Wemm */ 726120fef1SDavid Greenman static struct ps_strings *ps_strings = PS_STRINGS; 7399ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, ""); 7499ac3bc8SPeter Wemm 7599ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK; 7699ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, ""); 7799ac3bc8SPeter Wemm 7899ac3bc8SPeter Wemm /* 7926f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 8026f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 81df8bae1dSRodney W. Grimes */ 8287b6de2bSPoul-Henning Kamp static const struct execsw **execsw = 8387b6de2bSPoul-Henning Kamp (const struct execsw **)&execsw_set.ls_items[0]; 8426f9a767SRodney W. Grimes 85d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 86ad7507e2SSteven Wallace struct execve_args { 87ad7507e2SSteven Wallace char *fname; 88ad7507e2SSteven Wallace char **argv; 89ad7507e2SSteven Wallace char **envv; 90ad7507e2SSteven Wallace }; 91d2d3e875SBruce Evans #endif 92ad7507e2SSteven Wallace 9326f9a767SRodney W. Grimes /* 9426f9a767SRodney W. Grimes * execve() system call. 9526f9a767SRodney W. Grimes */ 9626f9a767SRodney W. Grimes int 97cb226aaaSPoul-Henning Kamp execve(p, uap) 9826f9a767SRodney W. Grimes struct proc *p; 9926f9a767SRodney W. Grimes register struct execve_args *uap; 100df8bae1dSRodney W. Grimes { 10126f9a767SRodney W. Grimes struct nameidata nd, *ndp; 102ecbb00a2SDoug Rabson long *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->argc = imgp->envc = 0; 1165cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 117c52007c2SDavid Greenman imgp->entry_addr = 0; 118c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 119c52007c2SDavid Greenman imgp->interpreted = 0; 120c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 121e1743d02SSøren Schmidt imgp->auxargs = NULL; 1221616db3cSJohn Dyson imgp->vp = NULL; 1231616db3cSJohn Dyson imgp->firstpage = NULL; 12426f9a767SRodney W. Grimes 12526f9a767SRodney W. Grimes /* 12626f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12726f9a767SRodney W. Grimes * environment strings 12826f9a767SRodney W. Grimes */ 1291616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 130c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 13126f9a767SRodney W. Grimes error = ENOMEM; 13226f9a767SRodney W. Grimes goto exec_fail; 13326f9a767SRodney W. Grimes } 134c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 135c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1361616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 13726f9a767SRodney W. Grimes 13826f9a767SRodney W. Grimes /* 13926f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 14026f9a767SRodney W. Grimes * in ni_vp amoung other things. 14126f9a767SRodney W. Grimes */ 14226f9a767SRodney W. Grimes ndp = &nd; 143b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1443ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 14526f9a767SRodney W. Grimes 14626f9a767SRodney W. Grimes interpret: 14726f9a767SRodney W. Grimes 14826f9a767SRodney W. Grimes error = namei(ndp); 14926f9a767SRodney W. Grimes if (error) { 1501616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1511616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 15226f9a767SRodney W. Grimes goto exec_fail; 15326f9a767SRodney W. Grimes } 15426f9a767SRodney W. Grimes 155c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 15626f9a767SRodney W. Grimes 15726f9a767SRodney W. Grimes /* 1589022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1599022e4adSDavid Greenman */ 160c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1618677f509SDavid Greenman if (error) { 1628677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 16326f9a767SRodney W. Grimes goto exec_fail_dealloc; 1648677f509SDavid Greenman } 16526f9a767SRodney W. Grimes 1661616db3cSJohn Dyson error = exec_map_first_page(imgp); 1679caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1686d5a0a8cSDavid Greenman if (error) 16926f9a767SRodney W. Grimes goto exec_fail_dealloc; 17026f9a767SRodney W. Grimes 17126f9a767SRodney W. Grimes /* 17226f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 17326f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 17426f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 17526f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 17626f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 17726f9a767SRodney W. Grimes * the interpreter. 17826f9a767SRodney W. Grimes */ 17926f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 18026f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 181c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 18226f9a767SRodney W. Grimes else 18326f9a767SRodney W. Grimes continue; 18426f9a767SRodney W. Grimes if (error == -1) 18526f9a767SRodney W. Grimes continue; 18626f9a767SRodney W. Grimes if (error) 18726f9a767SRodney W. Grimes goto exec_fail_dealloc; 188c52007c2SDavid Greenman if (imgp->interpreted) { 1891616db3cSJohn Dyson exec_unmap_first_page(imgp); 19026f9a767SRodney W. Grimes /* free old vnode and name buffer */ 191f5277ae7SDavid Greenman vrele(ndp->ni_vp); 19299448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 19326f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 194b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 195c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 19626f9a767SRodney W. Grimes goto interpret; 19726f9a767SRodney W. Grimes } 19826f9a767SRodney W. Grimes break; 19926f9a767SRodney W. Grimes } 20026f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 20126f9a767SRodney W. Grimes if (error == -1) { 20226f9a767SRodney W. Grimes error = ENOEXEC; 20326f9a767SRodney W. Grimes goto exec_fail_dealloc; 20426f9a767SRodney W. Grimes } 20526f9a767SRodney W. Grimes 20626f9a767SRodney W. Grimes /* 20726f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 20826f9a767SRodney W. Grimes */ 209c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 21026f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 21126f9a767SRodney W. Grimes 21226f9a767SRodney W. Grimes /* 2131e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2141e1e0b44SSøren Schmidt * let it do the stack setup. 2151e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 21626f9a767SRodney W. Grimes */ 2171e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 218c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2191e1e0b44SSøren Schmidt else 220c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 22126f9a767SRodney W. Grimes 222a78e8d2aSDavid Greenman /* 223a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 224a78e8d2aSDavid Greenman * be shared after an exec. 225a78e8d2aSDavid Greenman */ 226a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 227a78e8d2aSDavid Greenman struct filedesc *tmp; 228a78e8d2aSDavid Greenman 229a78e8d2aSDavid Greenman tmp = fdcopy(p); 230a78e8d2aSDavid Greenman fdfree(p); 231a78e8d2aSDavid Greenman p->p_fd = tmp; 232a78e8d2aSDavid Greenman } 233a78e8d2aSDavid Greenman 23426f9a767SRodney W. Grimes /* close files on exec */ 23526f9a767SRodney W. Grimes fdcloseexec(p); 23626f9a767SRodney W. Grimes 23726f9a767SRodney W. Grimes /* reset caught signals */ 23826f9a767SRodney W. Grimes execsigs(p); 23926f9a767SRodney W. Grimes 24026f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 24126f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 24226f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 24326f9a767SRodney W. Grimes p->p_comm[len] = 0; 24426f9a767SRodney W. Grimes 24526f9a767SRodney W. Grimes /* 24624b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 247dc733423SDag-Erling Smørgrav * it that it now has its own resources back 24826f9a767SRodney W. Grimes */ 24926f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 25026f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 25126f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 25226f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 25326f9a767SRodney W. Grimes } 25426f9a767SRodney W. Grimes 25526f9a767SRodney W. Grimes /* 256a78e8d2aSDavid Greenman * Implement image setuid/setgid. 257a78e8d2aSDavid Greenman * 258a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 259a78e8d2aSDavid Greenman * the process is being traced. 260c52007c2SDavid Greenman */ 261d021ae3dSGuido van Rooij if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid || 262d021ae3dSGuido van Rooij attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) && 263a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 264c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 265c52007c2SDavid Greenman /* 266c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 26726f9a767SRodney W. Grimes * root. 26826f9a767SRodney W. Grimes */ 269c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 27026f9a767SRodney W. Grimes p->p_traceflag = 0; 27126f9a767SRodney W. Grimes vrele(p->p_tracep); 272c52007c2SDavid Greenman p->p_tracep = NULL; 27326f9a767SRodney W. Grimes } 274c52007c2SDavid Greenman /* 275c52007c2SDavid Greenman * Set the new credentials. 276c52007c2SDavid Greenman */ 27726f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 278c52007c2SDavid Greenman if (attr.va_mode & VSUID) 27926f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 280c52007c2SDavid Greenman if (attr.va_mode & VSGID) 281d021ae3dSGuido van Rooij p->p_ucred->cr_gid = attr.va_gid; 282d5f81602SSean Eric Fagan setsugid(p); 283c52007c2SDavid Greenman } else { 284e47bda07SDavid Greenman if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 285e47bda07SDavid Greenman p->p_ucred->cr_gid == p->p_cred->p_rgid) 286c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 28726f9a767SRodney W. Grimes } 28826f9a767SRodney W. Grimes 28926f9a767SRodney W. Grimes /* 290c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 29126f9a767SRodney W. Grimes */ 29226f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 29326f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 29426f9a767SRodney W. Grimes 29526f9a767SRodney W. Grimes /* 2962a531c80SDavid Greenman * Store the vp for use in procfs 2972a531c80SDavid Greenman */ 2982a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 2992a531c80SDavid Greenman vrele(p->p_textvp); 3002a531c80SDavid Greenman VREF(ndp->ni_vp); 3012a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3022a531c80SDavid Greenman 3032a531c80SDavid Greenman /* 30426f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 30526f9a767SRodney W. Grimes * can be set before the program executes. 30626f9a767SRodney W. Grimes */ 3072a024a2bSSean Eric Fagan STOPEVENT(p, S_EXEC, 0); 3082a024a2bSSean Eric Fagan 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 3181616db3cSJohn Dyson exec_fail_dealloc: 3191616db3cSJohn Dyson 32026f9a767SRodney W. Grimes /* 32126f9a767SRodney W. Grimes * free various allocated resources 32226f9a767SRodney W. Grimes */ 3231616db3cSJohn Dyson if (imgp->firstpage) 3241616db3cSJohn Dyson exec_unmap_first_page(imgp); 32526f9a767SRodney W. Grimes 326c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 3271616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 3281616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 3291616db3cSJohn Dyson 330a3cf6ebaSDavid Greenman if (ndp->ni_vp) { 331f5277ae7SDavid Greenman vrele(ndp->ni_vp); 33299448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 333a3cf6ebaSDavid Greenman } 33426f9a767SRodney W. Grimes 3351616db3cSJohn Dyson if (error == 0) 3361616db3cSJohn Dyson return (0); 3371616db3cSJohn Dyson 33826f9a767SRodney W. Grimes exec_fail: 339c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 34026f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 34126f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 34226f9a767SRodney W. Grimes /* NOT REACHED */ 34326f9a767SRodney W. Grimes return(0); 34426f9a767SRodney W. Grimes } else { 34526f9a767SRodney W. Grimes return(error); 34626f9a767SRodney W. Grimes } 34726f9a767SRodney W. Grimes } 34826f9a767SRodney W. Grimes 3491616db3cSJohn Dyson int 3501616db3cSJohn Dyson exec_map_first_page(imgp) 3511616db3cSJohn Dyson struct image_params *imgp; 3521616db3cSJohn Dyson { 35395461b45SJohn Dyson int s, rv, i; 35495461b45SJohn Dyson int initial_pagein; 35595461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 3561616db3cSJohn Dyson vm_object_t object; 3571616db3cSJohn Dyson 3581616db3cSJohn Dyson 3591616db3cSJohn Dyson if (imgp->firstpage) { 3601616db3cSJohn Dyson exec_unmap_first_page(imgp); 3611616db3cSJohn Dyson } 3621616db3cSJohn Dyson 3631616db3cSJohn Dyson object = imgp->vp->v_object; 3641616db3cSJohn Dyson s = splvm(); 3651616db3cSJohn Dyson 36695461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3671616db3cSJohn Dyson 36895461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 36995461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 37095461b45SJohn Dyson if (initial_pagein > object->size) 37195461b45SJohn Dyson initial_pagein = object->size; 37295461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 37395461b45SJohn Dyson if (ma[i] = vm_page_lookup(object, i)) { 37495461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 37595461b45SJohn Dyson break; 37695461b45SJohn Dyson if (ma[i]->valid) 37795461b45SJohn Dyson break; 37895461b45SJohn Dyson ma[i]->flags |= PG_BUSY; 37995461b45SJohn Dyson } else { 38095461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 38195461b45SJohn Dyson if (ma[i] == NULL) 38295461b45SJohn Dyson break; 38395461b45SJohn Dyson } 38495461b45SJohn Dyson } 38595461b45SJohn Dyson initial_pagein = i; 3861616db3cSJohn Dyson 38795461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 38895461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 38995461b45SJohn Dyson 390eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 391ecbb00a2SDoug Rabson if (ma[0]) { 39295461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 3938f9110f6SJohn Dyson vm_page_free(ma[0]); 394ecbb00a2SDoug Rabson } 3951616db3cSJohn Dyson splx(s); 3961616db3cSJohn Dyson return EIO; 3971616db3cSJohn Dyson } 3981616db3cSJohn Dyson } 3991616db3cSJohn Dyson 40095461b45SJohn Dyson vm_page_wire(ma[0]); 40195461b45SJohn Dyson PAGE_WAKEUP(ma[0]); 4021616db3cSJohn Dyson splx(s); 4031616db3cSJohn Dyson 40495461b45SJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 40595461b45SJohn Dyson imgp->firstpage = ma[0]; 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; 478ecbb00a2SDoug Rabson int error; 479ecbb00a2SDoug Rabson size_t length; 48026f9a767SRodney W. Grimes 48126f9a767SRodney W. Grimes /* 48226f9a767SRodney W. Grimes * extract arguments first 48326f9a767SRodney W. Grimes */ 48426f9a767SRodney W. Grimes 485c52007c2SDavid Greenman argv = imgp->uap->argv; 48626f9a767SRodney W. Grimes 4870fd1a014SDavid Greenman if (argv) { 4885cf3d12cSAndrey A. Chernov argp = (caddr_t) fuword(argv); 4895cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 4905cf3d12cSAndrey A. Chernov return (EFAULT); 4915cf3d12cSAndrey A. Chernov if (argp) 4925cf3d12cSAndrey A. Chernov argv++; 4935cf3d12cSAndrey A. Chernov if (imgp->argv0) 4945cf3d12cSAndrey A. Chernov argp = imgp->argv0; 4955cf3d12cSAndrey A. Chernov if (argp) { 4965cf3d12cSAndrey A. Chernov do { 49726f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 49826f9a767SRodney W. Grimes return (EFAULT); 499c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 500c52007c2SDavid Greenman imgp->stringspace, &length))) { 5010fd1a014SDavid Greenman if (error == ENAMETOOLONG) 50226f9a767SRodney W. Grimes return(E2BIG); 5030fd1a014SDavid Greenman return (error); 5040fd1a014SDavid Greenman } 505c52007c2SDavid Greenman imgp->stringspace -= length; 506c52007c2SDavid Greenman imgp->stringp += length; 507c52007c2SDavid Greenman imgp->argc++; 5085cf3d12cSAndrey A. Chernov } while ((argp = (caddr_t) fuword(argv++))); 50926f9a767SRodney W. Grimes } 5100fd1a014SDavid Greenman } 51126f9a767SRodney W. Grimes 51226f9a767SRodney W. Grimes /* 51326f9a767SRodney W. Grimes * extract environment strings 51426f9a767SRodney W. Grimes */ 51526f9a767SRodney W. Grimes 516c52007c2SDavid Greenman envv = imgp->uap->envv; 51726f9a767SRodney W. Grimes 5180fd1a014SDavid Greenman if (envv) { 519bb56ec4aSPoul-Henning Kamp while ((envp = (caddr_t) fuword(envv++))) { 52026f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 52126f9a767SRodney W. Grimes return (EFAULT); 522c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 523c52007c2SDavid Greenman imgp->stringspace, &length))) { 5240fd1a014SDavid Greenman if (error == ENAMETOOLONG) 52526f9a767SRodney W. Grimes return(E2BIG); 5260fd1a014SDavid Greenman return (error); 5270fd1a014SDavid Greenman } 528c52007c2SDavid Greenman imgp->stringspace -= length; 529c52007c2SDavid Greenman imgp->stringp += length; 530c52007c2SDavid Greenman imgp->envc++; 53126f9a767SRodney W. Grimes } 5320fd1a014SDavid Greenman } 53326f9a767SRodney W. Grimes 53426f9a767SRodney W. Grimes return (0); 53526f9a767SRodney W. Grimes } 53626f9a767SRodney W. Grimes 53726f9a767SRodney W. Grimes /* 53826f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 53926f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 54026f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 54126f9a767SRodney W. Grimes */ 542ecbb00a2SDoug Rabson long * 543c52007c2SDavid Greenman exec_copyout_strings(imgp) 544c52007c2SDavid Greenman struct image_params *imgp; 54526f9a767SRodney W. Grimes { 54626f9a767SRodney W. Grimes int argc, envc; 54726f9a767SRodney W. Grimes char **vectp; 54826f9a767SRodney W. Grimes char *stringp, *destp; 549ecbb00a2SDoug Rabson long *stack_base; 55093f6448cSDavid Greenman struct ps_strings *arginfo; 551d66a5066SPeter Wemm int szsigcode; 55226f9a767SRodney W. Grimes 55326f9a767SRodney W. Grimes /* 55426f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 555d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 55626f9a767SRodney W. Grimes */ 55793f6448cSDavid Greenman arginfo = PS_STRINGS; 558d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 559d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 5601ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 5611ed012f9SPeter Wemm 56226f9a767SRodney W. Grimes /* 563d66a5066SPeter Wemm * install sigcode 564d66a5066SPeter Wemm */ 565d66a5066SPeter Wemm if (szsigcode) 566d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 567d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 568d66a5066SPeter Wemm 569d66a5066SPeter Wemm /* 570e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 571e1743d02SSøren Schmidt * on the stack. 572e1743d02SSøren Schmidt */ 573e1743d02SSøren Schmidt if (imgp->auxargs) 574e1743d02SSøren Schmidt /* 575e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 576e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 577e1743d02SSøren Schmidt * ELF Auxargs data. 578e1743d02SSøren Schmidt */ 579e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 580e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 581e1743d02SSøren Schmidt else 582e1743d02SSøren Schmidt /* 58326f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 58426f9a767SRodney W. Grimes * arg and env vector sets 58526f9a767SRodney W. Grimes */ 586e1743d02SSøren Schmidt vectp = (char **) 587e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 58826f9a767SRodney W. Grimes 58926f9a767SRodney W. Grimes /* 59026f9a767SRodney W. Grimes * vectp also becomes our initial stack base 59126f9a767SRodney W. Grimes */ 592ecbb00a2SDoug Rabson stack_base = (long *)vectp; 59326f9a767SRodney W. Grimes 594c52007c2SDavid Greenman stringp = imgp->stringbase; 595c52007c2SDavid Greenman argc = imgp->argc; 596c52007c2SDavid Greenman envc = imgp->envc; 59726f9a767SRodney W. Grimes 59893f6448cSDavid Greenman /* 5993aed948bSDavid Greenman * Copy out strings - arguments and environment. 60093f6448cSDavid Greenman */ 601c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 60293f6448cSDavid Greenman 60393f6448cSDavid Greenman /* 6043aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 6053aed948bSDavid Greenman */ 606ecbb00a2SDoug Rabson suword(&arginfo->ps_argvstr, (long)vectp); 6073aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 6083aed948bSDavid Greenman 6093aed948bSDavid Greenman /* 6103aed948bSDavid Greenman * Fill in argument portion of vector table. 61193f6448cSDavid Greenman */ 61226f9a767SRodney W. Grimes for (; argc > 0; --argc) { 613ecbb00a2SDoug Rabson suword(vectp++, (long)destp); 6143aed948bSDavid Greenman while (*stringp++ != 0) 6153aed948bSDavid Greenman destp++; 6163aed948bSDavid Greenman destp++; 61726f9a767SRodney W. Grimes } 61826f9a767SRodney W. Grimes 61926f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 6206ab46d52SBruce Evans suword(vectp++, 0); 62126f9a767SRodney W. Grimes 622ecbb00a2SDoug Rabson suword(&arginfo->ps_envstr, (long)vectp); 6233aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 62493f6448cSDavid Greenman 62593f6448cSDavid Greenman /* 6263aed948bSDavid Greenman * Fill in environment portion of vector table. 62793f6448cSDavid Greenman */ 62826f9a767SRodney W. Grimes for (; envc > 0; --envc) { 629ecbb00a2SDoug Rabson suword(vectp++, (long)destp); 6303aed948bSDavid Greenman while (*stringp++ != 0) 6313aed948bSDavid Greenman destp++; 6323aed948bSDavid Greenman destp++; 63326f9a767SRodney W. Grimes } 63426f9a767SRodney W. Grimes 63526f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 6366ab46d52SBruce Evans suword(vectp, 0); 63726f9a767SRodney W. Grimes 63826f9a767SRodney W. Grimes return (stack_base); 63926f9a767SRodney W. Grimes } 64026f9a767SRodney W. Grimes 64126f9a767SRodney W. Grimes /* 64226f9a767SRodney W. Grimes * Check permissions of file to execute. 64326f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 64426f9a767SRodney W. Grimes */ 645c8a79999SPeter Wemm int 646c52007c2SDavid Greenman exec_check_permissions(imgp) 647c52007c2SDavid Greenman struct image_params *imgp; 64826f9a767SRodney W. Grimes { 649c52007c2SDavid Greenman struct proc *p = imgp->proc; 650c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 651c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 65226f9a767SRodney W. Grimes int error; 65326f9a767SRodney W. Grimes 65426f9a767SRodney W. Grimes /* Get file attributes */ 655c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 65626f9a767SRodney W. Grimes if (error) 65726f9a767SRodney W. Grimes return (error); 65826f9a767SRodney W. Grimes 65926f9a767SRodney W. Grimes /* 66026f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 66126f9a767SRodney W. Grimes * file resides on. 66226f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 66326f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 66426f9a767SRodney W. Grimes * file really is executable. 66526f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 66626f9a767SRodney W. Grimes */ 667c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 66826f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 66926f9a767SRodney W. Grimes (attr->va_type != VREG)) { 67026f9a767SRodney W. Grimes return (EACCES); 67126f9a767SRodney W. Grimes } 67226f9a767SRodney W. Grimes 67326f9a767SRodney W. Grimes /* 67426f9a767SRodney W. Grimes * Zero length files can't be exec'd 67526f9a767SRodney W. Grimes */ 67626f9a767SRodney W. Grimes if (attr->va_size == 0) 67726f9a767SRodney W. Grimes return (ENOEXEC); 67826f9a767SRodney W. Grimes 67926f9a767SRodney W. Grimes /* 68026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 68126f9a767SRodney W. Grimes */ 682c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 68326f9a767SRodney W. Grimes if (error) 68426f9a767SRodney W. Grimes return (error); 68526f9a767SRodney W. Grimes 6866d5a0a8cSDavid Greenman /* 6876d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 6886d5a0a8cSDavid Greenman * if there are any. 6896d5a0a8cSDavid Greenman */ 6906d5a0a8cSDavid Greenman if (vp->v_writecount) 6916d5a0a8cSDavid Greenman return (ETXTBSY); 6926d5a0a8cSDavid Greenman 6936d5a0a8cSDavid Greenman /* 6946d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 6956d5a0a8cSDavid Greenman * general case). 6966d5a0a8cSDavid Greenman */ 697c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 69826f9a767SRodney W. Grimes if (error) 69926f9a767SRodney W. Grimes return (error); 70026f9a767SRodney W. Grimes 70126f9a767SRodney W. Grimes return (0); 702df8bae1dSRodney W. Grimes } 703