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 * 26486bddb0SDoug Rabson * $Id: kern_exec.c,v 1.90 1998/12/16 16:28:57 bde 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> 44aa855a59SPeter Wemm #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> 6426f9a767SRodney W. Grimes 6526f9a767SRodney W. Grimes #include <machine/reg.h> 6626f9a767SRodney W. Grimes 67ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *)); 68df8bae1dSRodney W. Grimes 69486bddb0SDoug Rabson static long ps_strings = PS_STRINGS; 70486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, ""); 71486bddb0SDoug Rabson 72486bddb0SDoug Rabson static long usrstack = USRSTACK; 73486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, ""); 7499ac3bc8SPeter Wemm 7599ac3bc8SPeter Wemm /* 76aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 77aa855a59SPeter Wemm * double pointer here. 78df8bae1dSRodney W. Grimes */ 79aa855a59SPeter Wemm static const struct execsw **execsw; 8026f9a767SRodney W. Grimes 81d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 82ad7507e2SSteven Wallace struct execve_args { 83ad7507e2SSteven Wallace char *fname; 84ad7507e2SSteven Wallace char **argv; 85ad7507e2SSteven Wallace char **envv; 86ad7507e2SSteven Wallace }; 87d2d3e875SBruce Evans #endif 88ad7507e2SSteven Wallace 8926f9a767SRodney W. Grimes /* 9026f9a767SRodney W. Grimes * execve() system call. 9126f9a767SRodney W. Grimes */ 9226f9a767SRodney W. Grimes int 93cb226aaaSPoul-Henning Kamp execve(p, uap) 9426f9a767SRodney W. Grimes struct proc *p; 9526f9a767SRodney W. Grimes register struct execve_args *uap; 96df8bae1dSRodney W. Grimes { 9726f9a767SRodney W. Grimes struct nameidata nd, *ndp; 98ecbb00a2SDoug Rabson long *stack_base; 99bb56ec4aSPoul-Henning Kamp int error, len, i; 100c52007c2SDavid Greenman struct image_params image_params, *imgp; 10126f9a767SRodney W. Grimes struct vattr attr; 10226f9a767SRodney W. Grimes 103c52007c2SDavid Greenman imgp = &image_params; 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes /* 106c52007c2SDavid Greenman * Initialize part of the common data 107df8bae1dSRodney W. Grimes */ 108c52007c2SDavid Greenman imgp->proc = p; 109c52007c2SDavid Greenman imgp->uap = uap; 110c52007c2SDavid Greenman imgp->attr = &attr; 111c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1125cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 113c52007c2SDavid Greenman imgp->entry_addr = 0; 114c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 115c52007c2SDavid Greenman imgp->interpreted = 0; 116c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 117e1743d02SSøren Schmidt imgp->auxargs = NULL; 1181616db3cSJohn Dyson imgp->vp = NULL; 1191616db3cSJohn Dyson imgp->firstpage = NULL; 12026f9a767SRodney W. Grimes 12126f9a767SRodney W. Grimes /* 12226f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12326f9a767SRodney W. Grimes * environment strings 12426f9a767SRodney W. Grimes */ 1251616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 126c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 12726f9a767SRodney W. Grimes error = ENOMEM; 12826f9a767SRodney W. Grimes goto exec_fail; 12926f9a767SRodney W. Grimes } 130c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 131c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1321616db3cSJohn Dyson imgp->image_header = imgp->stringbase + 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) { 1461616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1471616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 14826f9a767SRodney W. Grimes goto exec_fail; 14926f9a767SRodney W. Grimes } 15026f9a767SRodney W. Grimes 151c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 15226f9a767SRodney W. Grimes 15326f9a767SRodney W. Grimes /* 1549022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1559022e4adSDavid Greenman */ 156c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1578677f509SDavid Greenman if (error) { 1588677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 15926f9a767SRodney W. Grimes goto exec_fail_dealloc; 1608677f509SDavid Greenman } 16126f9a767SRodney W. Grimes 1621616db3cSJohn Dyson error = exec_map_first_page(imgp); 1639caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1646d5a0a8cSDavid Greenman if (error) 16526f9a767SRodney W. Grimes goto exec_fail_dealloc; 16626f9a767SRodney W. Grimes 16726f9a767SRodney W. Grimes /* 16826f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 16926f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 17026f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 17126f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 17226f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 17326f9a767SRodney W. Grimes * the interpreter. 17426f9a767SRodney W. Grimes */ 17526f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 17626f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 177c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 17826f9a767SRodney W. Grimes else 17926f9a767SRodney W. Grimes continue; 18026f9a767SRodney W. Grimes if (error == -1) 18126f9a767SRodney W. Grimes continue; 18226f9a767SRodney W. Grimes if (error) 18326f9a767SRodney W. Grimes goto exec_fail_dealloc; 184c52007c2SDavid Greenman if (imgp->interpreted) { 1851616db3cSJohn Dyson exec_unmap_first_page(imgp); 18626f9a767SRodney W. Grimes /* free old vnode and name buffer */ 187f5277ae7SDavid Greenman vrele(ndp->ni_vp); 18899448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 18926f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 190b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 191c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 19226f9a767SRodney W. Grimes goto interpret; 19326f9a767SRodney W. Grimes } 19426f9a767SRodney W. Grimes break; 19526f9a767SRodney W. Grimes } 19626f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 19726f9a767SRodney W. Grimes if (error == -1) { 19826f9a767SRodney W. Grimes error = ENOEXEC; 19926f9a767SRodney W. Grimes goto exec_fail_dealloc; 20026f9a767SRodney W. Grimes } 20126f9a767SRodney W. Grimes 20226f9a767SRodney W. Grimes /* 20326f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 20426f9a767SRodney W. Grimes */ 205c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 20626f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 20726f9a767SRodney W. Grimes 20826f9a767SRodney W. Grimes /* 2091e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2101e1e0b44SSøren Schmidt * let it do the stack setup. 2111e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 21226f9a767SRodney W. Grimes */ 2131e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 214c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2151e1e0b44SSøren Schmidt else 216c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 21726f9a767SRodney W. Grimes 218a78e8d2aSDavid Greenman /* 219a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 220a78e8d2aSDavid Greenman * be shared after an exec. 221a78e8d2aSDavid Greenman */ 222a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 223a78e8d2aSDavid Greenman struct filedesc *tmp; 224a78e8d2aSDavid Greenman 225a78e8d2aSDavid Greenman tmp = fdcopy(p); 226a78e8d2aSDavid Greenman fdfree(p); 227a78e8d2aSDavid Greenman p->p_fd = tmp; 228a78e8d2aSDavid Greenman } 229a78e8d2aSDavid Greenman 23026f9a767SRodney W. Grimes /* close files on exec */ 23126f9a767SRodney W. Grimes fdcloseexec(p); 23226f9a767SRodney W. Grimes 23326f9a767SRodney W. Grimes /* reset caught signals */ 23426f9a767SRodney W. Grimes execsigs(p); 23526f9a767SRodney W. Grimes 23626f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 23726f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 23826f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 23926f9a767SRodney W. Grimes p->p_comm[len] = 0; 24026f9a767SRodney W. Grimes 24126f9a767SRodney W. Grimes /* 24224b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 243dc733423SDag-Erling Smørgrav * it that it now has its own resources back 24426f9a767SRodney W. Grimes */ 24526f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 24626f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 24726f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 24826f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 24926f9a767SRodney W. Grimes } 25026f9a767SRodney W. Grimes 25126f9a767SRodney W. Grimes /* 252a78e8d2aSDavid Greenman * Implement image setuid/setgid. 253a78e8d2aSDavid Greenman * 254a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 255a78e8d2aSDavid Greenman * the process is being traced. 256c52007c2SDavid Greenman */ 257d021ae3dSGuido van Rooij if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid || 258d021ae3dSGuido van Rooij attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) && 259a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 260c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 261c52007c2SDavid Greenman /* 262c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 26326f9a767SRodney W. Grimes * root. 26426f9a767SRodney W. Grimes */ 265c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 26626f9a767SRodney W. Grimes p->p_traceflag = 0; 26726f9a767SRodney W. Grimes vrele(p->p_tracep); 268c52007c2SDavid Greenman p->p_tracep = NULL; 26926f9a767SRodney W. Grimes } 270c52007c2SDavid Greenman /* 271c52007c2SDavid Greenman * Set the new credentials. 272c52007c2SDavid Greenman */ 27326f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 274c52007c2SDavid Greenman if (attr.va_mode & VSUID) 27526f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 276c52007c2SDavid Greenman if (attr.va_mode & VSGID) 277d021ae3dSGuido van Rooij p->p_ucred->cr_gid = attr.va_gid; 278d5f81602SSean Eric Fagan setsugid(p); 279c52007c2SDavid Greenman } else { 280e47bda07SDavid Greenman if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 281e47bda07SDavid Greenman p->p_ucred->cr_gid == p->p_cred->p_rgid) 282c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 28326f9a767SRodney W. Grimes } 28426f9a767SRodney W. Grimes 28526f9a767SRodney W. Grimes /* 286c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 28726f9a767SRodney W. Grimes */ 28826f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 28926f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 29026f9a767SRodney W. Grimes 29126f9a767SRodney W. Grimes /* 2922a531c80SDavid Greenman * Store the vp for use in procfs 2932a531c80SDavid Greenman */ 2942a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 2952a531c80SDavid Greenman vrele(p->p_textvp); 2962a531c80SDavid Greenman VREF(ndp->ni_vp); 2972a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 2982a531c80SDavid Greenman 2992a531c80SDavid Greenman /* 30026f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 30126f9a767SRodney W. Grimes * can be set before the program executes. 30226f9a767SRodney W. Grimes */ 3032a024a2bSSean Eric Fagan STOPEVENT(p, S_EXEC, 0); 3042a024a2bSSean Eric Fagan 30526f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 30626f9a767SRodney W. Grimes psignal(p, SIGTRAP); 30726f9a767SRodney W. Grimes 30826f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 30926f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 31026f9a767SRodney W. Grimes 31126f9a767SRodney W. Grimes /* Set entry address */ 312aae0aa45SBruce Evans setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base); 31326f9a767SRodney W. Grimes 3141616db3cSJohn Dyson exec_fail_dealloc: 3151616db3cSJohn Dyson 31626f9a767SRodney W. Grimes /* 31726f9a767SRodney W. Grimes * free various allocated resources 31826f9a767SRodney W. Grimes */ 3191616db3cSJohn Dyson if (imgp->firstpage) 3201616db3cSJohn Dyson exec_unmap_first_page(imgp); 32126f9a767SRodney W. Grimes 322c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 3231616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 3241616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 3251616db3cSJohn Dyson 326a3cf6ebaSDavid Greenman if (ndp->ni_vp) { 327f5277ae7SDavid Greenman vrele(ndp->ni_vp); 32899448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 329a3cf6ebaSDavid Greenman } 33026f9a767SRodney W. Grimes 3311616db3cSJohn Dyson if (error == 0) 3321616db3cSJohn Dyson return (0); 3331616db3cSJohn Dyson 33426f9a767SRodney W. Grimes exec_fail: 335c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 33626f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 33726f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 33826f9a767SRodney W. Grimes /* NOT REACHED */ 33926f9a767SRodney W. Grimes return(0); 34026f9a767SRodney W. Grimes } else { 34126f9a767SRodney W. Grimes return(error); 34226f9a767SRodney W. Grimes } 34326f9a767SRodney W. Grimes } 34426f9a767SRodney W. Grimes 3451616db3cSJohn Dyson int 3461616db3cSJohn Dyson exec_map_first_page(imgp) 3471616db3cSJohn Dyson struct image_params *imgp; 3481616db3cSJohn Dyson { 34995461b45SJohn Dyson int s, rv, i; 35095461b45SJohn Dyson int initial_pagein; 35195461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 3521616db3cSJohn Dyson vm_object_t object; 3531616db3cSJohn Dyson 3541616db3cSJohn Dyson 3551616db3cSJohn Dyson if (imgp->firstpage) { 3561616db3cSJohn Dyson exec_unmap_first_page(imgp); 3571616db3cSJohn Dyson } 3581616db3cSJohn Dyson 3591616db3cSJohn Dyson object = imgp->vp->v_object; 3601616db3cSJohn Dyson s = splvm(); 3611616db3cSJohn Dyson 36295461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3631616db3cSJohn Dyson 36495461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 36595461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 36695461b45SJohn Dyson if (initial_pagein > object->size) 36795461b45SJohn Dyson initial_pagein = object->size; 36895461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 36995461b45SJohn Dyson if (ma[i] = vm_page_lookup(object, i)) { 37095461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 37195461b45SJohn Dyson break; 37295461b45SJohn Dyson if (ma[i]->valid) 37395461b45SJohn Dyson break; 374e69763a3SDoug Rabson vm_page_busy(ma[i]); 37595461b45SJohn Dyson } else { 37695461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 37795461b45SJohn Dyson if (ma[i] == NULL) 37895461b45SJohn Dyson break; 37995461b45SJohn Dyson } 38095461b45SJohn Dyson } 38195461b45SJohn Dyson initial_pagein = i; 3821616db3cSJohn Dyson 38395461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 38495461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 38595461b45SJohn Dyson 386eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 387ecbb00a2SDoug Rabson if (ma[0]) { 38895461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 3898f9110f6SJohn Dyson vm_page_free(ma[0]); 390ecbb00a2SDoug Rabson } 3911616db3cSJohn Dyson splx(s); 3921616db3cSJohn Dyson return EIO; 3931616db3cSJohn Dyson } 3941616db3cSJohn Dyson } 3951616db3cSJohn Dyson 39695461b45SJohn Dyson vm_page_wire(ma[0]); 397e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 3981616db3cSJohn Dyson splx(s); 3991616db3cSJohn Dyson 40095461b45SJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 40195461b45SJohn Dyson imgp->firstpage = ma[0]; 4021616db3cSJohn Dyson 4031616db3cSJohn Dyson return 0; 4041616db3cSJohn Dyson } 4051616db3cSJohn Dyson 4061616db3cSJohn Dyson void 4071616db3cSJohn Dyson exec_unmap_first_page(imgp) 4081616db3cSJohn Dyson struct image_params *imgp; 4091616db3cSJohn Dyson { 4101616db3cSJohn Dyson if (imgp->firstpage) { 4111616db3cSJohn Dyson pmap_kremove((vm_offset_t) imgp->image_header); 41273007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 4131616db3cSJohn Dyson imgp->firstpage = NULL; 4141616db3cSJohn Dyson } 4151616db3cSJohn Dyson } 4161616db3cSJohn Dyson 41726f9a767SRodney W. Grimes /* 41826f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 41926f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 42026f9a767SRodney W. Grimes * automatically in trap.c. 42126f9a767SRodney W. Grimes */ 42226f9a767SRodney W. Grimes int 423c52007c2SDavid Greenman exec_new_vmspace(imgp) 424c52007c2SDavid Greenman struct image_params *imgp; 42526f9a767SRodney W. Grimes { 42626f9a767SRodney W. Grimes int error; 427c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 42826f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 429af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 43026f9a767SRodney W. Grimes 431c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 43226f9a767SRodney W. Grimes 433af9ec885SJohn Dyson /* 434af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 435af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 436af9ec885SJohn Dyson * not disrupted 437af9ec885SJohn Dyson */ 438af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 4393d903220SDoug Rabson if (vmspace->vm_shm) 440c52007c2SDavid Greenman shmexit(imgp->proc); 4419d3fbbb5SJohn Dyson pmap_remove_pages(&vmspace->vm_pmap, 0, USRSTACK); 442af9ec885SJohn Dyson vm_map_remove(map, 0, USRSTACK); 443af9ec885SJohn Dyson } else { 444492da96cSJohn Dyson vmspace_exec(imgp->proc); 445492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 446af9ec885SJohn Dyson map = &vmspace->vm_map; 447af9ec885SJohn Dyson } 44826f9a767SRodney W. Grimes 44926f9a767SRodney W. Grimes /* Allocate a new stack */ 4501616db3cSJohn Dyson error = vm_map_insert(&vmspace->vm_map, NULL, 0, 4511616db3cSJohn Dyson (vm_offset_t) stack_addr, (vm_offset_t) USRSTACK, 4521616db3cSJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 0); 45326f9a767SRodney W. Grimes if (error) 45426f9a767SRodney W. Grimes return (error); 45526f9a767SRodney W. Grimes 45626f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 45726f9a767SRodney W. Grimes 45826f9a767SRodney W. Grimes /* Initialize maximum stack address */ 45926f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes return(0); 46226f9a767SRodney W. Grimes } 46326f9a767SRodney W. Grimes 46426f9a767SRodney W. Grimes /* 46526f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 46626f9a767SRodney W. Grimes * address space into the temporary string buffer. 46726f9a767SRodney W. Grimes */ 46826f9a767SRodney W. Grimes int 469c52007c2SDavid Greenman exec_extract_strings(imgp) 470c52007c2SDavid Greenman struct image_params *imgp; 47126f9a767SRodney W. Grimes { 47226f9a767SRodney W. Grimes char **argv, **envv; 47326f9a767SRodney W. Grimes char *argp, *envp; 474ecbb00a2SDoug Rabson int error; 475ecbb00a2SDoug Rabson size_t length; 47626f9a767SRodney W. Grimes 47726f9a767SRodney W. Grimes /* 47826f9a767SRodney W. Grimes * extract arguments first 47926f9a767SRodney W. Grimes */ 48026f9a767SRodney W. Grimes 481c52007c2SDavid Greenman argv = imgp->uap->argv; 48226f9a767SRodney W. Grimes 4830fd1a014SDavid Greenman if (argv) { 484aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 4855cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 4865cf3d12cSAndrey A. Chernov return (EFAULT); 4875cf3d12cSAndrey A. Chernov if (argp) 4885cf3d12cSAndrey A. Chernov argv++; 4895cf3d12cSAndrey A. Chernov if (imgp->argv0) 4905cf3d12cSAndrey A. Chernov argp = imgp->argv0; 4915cf3d12cSAndrey A. Chernov if (argp) { 4925cf3d12cSAndrey A. Chernov do { 49326f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 49426f9a767SRodney W. Grimes return (EFAULT); 495c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 496c52007c2SDavid Greenman imgp->stringspace, &length))) { 4970fd1a014SDavid Greenman if (error == ENAMETOOLONG) 49826f9a767SRodney W. Grimes return(E2BIG); 4990fd1a014SDavid Greenman return (error); 5000fd1a014SDavid Greenman } 501c52007c2SDavid Greenman imgp->stringspace -= length; 502c52007c2SDavid Greenman imgp->stringp += length; 503c52007c2SDavid Greenman imgp->argc++; 504aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 50526f9a767SRodney W. Grimes } 5060fd1a014SDavid Greenman } 50726f9a767SRodney W. Grimes 50826f9a767SRodney W. Grimes /* 50926f9a767SRodney W. Grimes * extract environment strings 51026f9a767SRodney W. Grimes */ 51126f9a767SRodney W. Grimes 512c52007c2SDavid Greenman envv = imgp->uap->envv; 51326f9a767SRodney W. Grimes 5140fd1a014SDavid Greenman if (envv) { 515aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 51626f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 51726f9a767SRodney W. Grimes return (EFAULT); 518c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 519c52007c2SDavid Greenman imgp->stringspace, &length))) { 5200fd1a014SDavid Greenman if (error == ENAMETOOLONG) 52126f9a767SRodney W. Grimes return(E2BIG); 5220fd1a014SDavid Greenman return (error); 5230fd1a014SDavid Greenman } 524c52007c2SDavid Greenman imgp->stringspace -= length; 525c52007c2SDavid Greenman imgp->stringp += length; 526c52007c2SDavid Greenman imgp->envc++; 52726f9a767SRodney W. Grimes } 5280fd1a014SDavid Greenman } 52926f9a767SRodney W. Grimes 53026f9a767SRodney W. Grimes return (0); 53126f9a767SRodney W. Grimes } 53226f9a767SRodney W. Grimes 53326f9a767SRodney W. Grimes /* 53426f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 53526f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 53626f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 53726f9a767SRodney W. Grimes */ 538ecbb00a2SDoug Rabson long * 539c52007c2SDavid Greenman exec_copyout_strings(imgp) 540c52007c2SDavid Greenman struct image_params *imgp; 54126f9a767SRodney W. Grimes { 54226f9a767SRodney W. Grimes int argc, envc; 54326f9a767SRodney W. Grimes char **vectp; 54426f9a767SRodney W. Grimes char *stringp, *destp; 545ecbb00a2SDoug Rabson long *stack_base; 54693f6448cSDavid Greenman struct ps_strings *arginfo; 547d66a5066SPeter Wemm int szsigcode; 54826f9a767SRodney W. Grimes 54926f9a767SRodney W. Grimes /* 55026f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 551d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 55226f9a767SRodney W. Grimes */ 5534c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 554d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 555d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 5561ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 5571ed012f9SPeter Wemm 55826f9a767SRodney W. Grimes /* 559d66a5066SPeter Wemm * install sigcode 560d66a5066SPeter Wemm */ 561d66a5066SPeter Wemm if (szsigcode) 562d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 563d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 564d66a5066SPeter Wemm 565d66a5066SPeter Wemm /* 566e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 567e1743d02SSøren Schmidt * on the stack. 568e1743d02SSøren Schmidt */ 569e1743d02SSøren Schmidt if (imgp->auxargs) 570e1743d02SSøren Schmidt /* 571e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 572e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 573e1743d02SSøren Schmidt * ELF Auxargs data. 574e1743d02SSøren Schmidt */ 575e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 576e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 577e1743d02SSøren Schmidt else 578e1743d02SSøren Schmidt /* 57926f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 58026f9a767SRodney W. Grimes * arg and env vector sets 58126f9a767SRodney W. Grimes */ 582e1743d02SSøren Schmidt vectp = (char **) 583e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 58426f9a767SRodney W. Grimes 58526f9a767SRodney W. Grimes /* 58626f9a767SRodney W. Grimes * vectp also becomes our initial stack base 58726f9a767SRodney W. Grimes */ 588ecbb00a2SDoug Rabson stack_base = (long *)vectp; 58926f9a767SRodney W. Grimes 590c52007c2SDavid Greenman stringp = imgp->stringbase; 591c52007c2SDavid Greenman argc = imgp->argc; 592c52007c2SDavid Greenman envc = imgp->envc; 59326f9a767SRodney W. Grimes 59493f6448cSDavid Greenman /* 5953aed948bSDavid Greenman * Copy out strings - arguments and environment. 59693f6448cSDavid Greenman */ 597c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 59893f6448cSDavid Greenman 59993f6448cSDavid Greenman /* 6003aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 6013aed948bSDavid Greenman */ 602aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 6033aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 6043aed948bSDavid Greenman 6053aed948bSDavid Greenman /* 6063aed948bSDavid Greenman * Fill in argument portion of vector table. 60793f6448cSDavid Greenman */ 60826f9a767SRodney W. Grimes for (; argc > 0; --argc) { 609aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 6103aed948bSDavid Greenman while (*stringp++ != 0) 6113aed948bSDavid Greenman destp++; 6123aed948bSDavid Greenman destp++; 61326f9a767SRodney W. Grimes } 61426f9a767SRodney W. Grimes 61526f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 6166ab46d52SBruce Evans suword(vectp++, 0); 61726f9a767SRodney W. Grimes 618aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 6193aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 62093f6448cSDavid Greenman 62193f6448cSDavid Greenman /* 6223aed948bSDavid Greenman * Fill in environment portion of vector table. 62393f6448cSDavid Greenman */ 62426f9a767SRodney W. Grimes for (; envc > 0; --envc) { 625aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 6263aed948bSDavid Greenman while (*stringp++ != 0) 6273aed948bSDavid Greenman destp++; 6283aed948bSDavid Greenman destp++; 62926f9a767SRodney W. Grimes } 63026f9a767SRodney W. Grimes 63126f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 6326ab46d52SBruce Evans suword(vectp, 0); 63326f9a767SRodney W. Grimes 63426f9a767SRodney W. Grimes return (stack_base); 63526f9a767SRodney W. Grimes } 63626f9a767SRodney W. Grimes 63726f9a767SRodney W. Grimes /* 63826f9a767SRodney W. Grimes * Check permissions of file to execute. 63926f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 64026f9a767SRodney W. Grimes */ 641c8a79999SPeter Wemm int 642c52007c2SDavid Greenman exec_check_permissions(imgp) 643c52007c2SDavid Greenman struct image_params *imgp; 64426f9a767SRodney W. Grimes { 645c52007c2SDavid Greenman struct proc *p = imgp->proc; 646c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 647c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 64826f9a767SRodney W. Grimes int error; 64926f9a767SRodney W. Grimes 65026f9a767SRodney W. Grimes /* Get file attributes */ 651c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 65226f9a767SRodney W. Grimes if (error) 65326f9a767SRodney W. Grimes return (error); 65426f9a767SRodney W. Grimes 65526f9a767SRodney W. Grimes /* 65626f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 65726f9a767SRodney W. Grimes * file resides on. 65826f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 65926f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 66026f9a767SRodney W. Grimes * file really is executable. 66126f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 66226f9a767SRodney W. Grimes */ 663c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 66426f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 66526f9a767SRodney W. Grimes (attr->va_type != VREG)) { 66626f9a767SRodney W. Grimes return (EACCES); 66726f9a767SRodney W. Grimes } 66826f9a767SRodney W. Grimes 66926f9a767SRodney W. Grimes /* 67026f9a767SRodney W. Grimes * Zero length files can't be exec'd 67126f9a767SRodney W. Grimes */ 67226f9a767SRodney W. Grimes if (attr->va_size == 0) 67326f9a767SRodney W. Grimes return (ENOEXEC); 67426f9a767SRodney W. Grimes 67526f9a767SRodney W. Grimes /* 67626f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 67726f9a767SRodney W. Grimes */ 678c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 67926f9a767SRodney W. Grimes if (error) 68026f9a767SRodney W. Grimes return (error); 68126f9a767SRodney W. Grimes 6826d5a0a8cSDavid Greenman /* 6836d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 6846d5a0a8cSDavid Greenman * if there are any. 6856d5a0a8cSDavid Greenman */ 6866d5a0a8cSDavid Greenman if (vp->v_writecount) 6876d5a0a8cSDavid Greenman return (ETXTBSY); 6886d5a0a8cSDavid Greenman 6896d5a0a8cSDavid Greenman /* 6906d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 6916d5a0a8cSDavid Greenman * general case). 6926d5a0a8cSDavid Greenman */ 693c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 69426f9a767SRodney W. Grimes if (error) 69526f9a767SRodney W. Grimes return (error); 69626f9a767SRodney W. Grimes 69726f9a767SRodney W. Grimes return (0); 698df8bae1dSRodney W. Grimes } 699aa855a59SPeter Wemm 700aa855a59SPeter Wemm /* 701aa855a59SPeter Wemm * Exec handler registration 702aa855a59SPeter Wemm */ 703aa855a59SPeter Wemm int 704aa855a59SPeter Wemm exec_register(execsw_arg) 705aa855a59SPeter Wemm const struct execsw *execsw_arg; 706aa855a59SPeter Wemm { 707aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 708aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 709aa855a59SPeter Wemm 710aa855a59SPeter Wemm if (execsw) 711aa855a59SPeter Wemm for (es = execsw; *es; es++) 712aa855a59SPeter Wemm count++; 713aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 714aa855a59SPeter Wemm if (newexecsw == NULL) 715aa855a59SPeter Wemm return ENOMEM; 716aa855a59SPeter Wemm xs = newexecsw; 717aa855a59SPeter Wemm if (execsw) 718aa855a59SPeter Wemm for (es = execsw; *es; es++) 719aa855a59SPeter Wemm *xs++ = *es; 720aa855a59SPeter Wemm *xs++ = execsw_arg; 721aa855a59SPeter Wemm *xs = NULL; 722aa855a59SPeter Wemm if (execsw) 723aa855a59SPeter Wemm free(execsw, M_TEMP); 724aa855a59SPeter Wemm execsw = newexecsw; 725aa855a59SPeter Wemm return 0; 726aa855a59SPeter Wemm } 727aa855a59SPeter Wemm 728aa855a59SPeter Wemm int 729aa855a59SPeter Wemm exec_unregister(execsw_arg) 730aa855a59SPeter Wemm const struct execsw *execsw_arg; 731aa855a59SPeter Wemm { 732aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 733aa855a59SPeter Wemm int count = 1; 734aa855a59SPeter Wemm 735aa855a59SPeter Wemm if (execsw == NULL) 736aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 737aa855a59SPeter Wemm 738aa855a59SPeter Wemm for (es = execsw; *es; es++) { 739aa855a59SPeter Wemm if (*es == execsw_arg) 740aa855a59SPeter Wemm break; 741aa855a59SPeter Wemm } 742aa855a59SPeter Wemm if (*es == NULL) 743aa855a59SPeter Wemm return ENOENT; 744aa855a59SPeter Wemm for (es = execsw; *es; es++) 745aa855a59SPeter Wemm if (*es != execsw_arg) 746aa855a59SPeter Wemm count++; 747aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 748aa855a59SPeter Wemm if (newexecsw == NULL) 749aa855a59SPeter Wemm return ENOMEM; 750aa855a59SPeter Wemm xs = newexecsw; 751aa855a59SPeter Wemm for (es = execsw; *es; es++) 752aa855a59SPeter Wemm if (*es != execsw_arg) 753aa855a59SPeter Wemm *xs++ = *es; 754aa855a59SPeter Wemm *xs = NULL; 755aa855a59SPeter Wemm if (execsw) 756aa855a59SPeter Wemm free(execsw, M_TEMP); 757aa855a59SPeter Wemm execsw = newexecsw; 758aa855a59SPeter Wemm return 0; 759aa855a59SPeter Wemm } 760