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 */ 26df8bae1dSRodney W. Grimes 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29677b542eSDavid E. O'Brien 301e9b3d91SPeter Wemm #include "opt_ktrace.h" 31339b79b9SRobert Watson #include "opt_mac.h" 321e9b3d91SPeter Wemm 33df8bae1dSRodney W. Grimes #include <sys/param.h> 3426f9a767SRodney W. Grimes #include <sys/systm.h> 3575b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 36fb919e4dSMark Murray #include <sys/lock.h> 37fb919e4dSMark Murray #include <sys/mutex.h> 38d2d3e875SBruce Evans #include <sys/sysproto.h> 3926f9a767SRodney W. Grimes #include <sys/signalvar.h> 4026f9a767SRodney W. Grimes #include <sys/kernel.h> 41339b79b9SRobert Watson #include <sys/mac.h> 4226f9a767SRodney W. Grimes #include <sys/mount.h> 43797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 44079cc25bSDavid Greenman #include <sys/fcntl.h> 4526f9a767SRodney W. Grimes #include <sys/acct.h> 4626f9a767SRodney W. Grimes #include <sys/exec.h> 4726f9a767SRodney W. Grimes #include <sys/imgact.h> 48e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4926f9a767SRodney W. Grimes #include <sys/wait.h> 50333ea485SGuido van Rooij #include <sys/malloc.h> 51a794e791SBruce Evans #include <sys/proc.h> 522a024a2bSSean Eric Fagan #include <sys/pioctl.h> 53a794e791SBruce Evans #include <sys/namei.h> 5459c8bc40SAlan Cox #include <sys/sf_buf.h> 5584e0b075SDavid Xu #include <sys/syscallsubr.h> 561e1e0b44SSøren Schmidt #include <sys/sysent.h> 57797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5899ac3bc8SPeter Wemm #include <sys/sysctl.h> 59333ea485SGuido van Rooij #include <sys/user.h> 60a794e791SBruce Evans #include <sys/vnode.h> 61c781aea8SPeter Wemm #ifdef KTRACE 62c781aea8SPeter Wemm #include <sys/ktrace.h> 63c781aea8SPeter Wemm #endif 6426f9a767SRodney W. Grimes 6526f9a767SRodney W. Grimes #include <vm/vm.h> 66efeaf95aSDavid Greenman #include <vm/vm_param.h> 67efeaf95aSDavid Greenman #include <vm/pmap.h> 681616db3cSJohn Dyson #include <vm/vm_page.h> 69efeaf95aSDavid Greenman #include <vm/vm_map.h> 7026f9a767SRodney W. Grimes #include <vm/vm_kern.h> 71efeaf95aSDavid Greenman #include <vm/vm_extern.h> 721ebd0c59SDavid Greenman #include <vm/vm_object.h> 731616db3cSJohn Dyson #include <vm/vm_pager.h> 7426f9a767SRodney W. Grimes 7526f9a767SRodney W. Grimes #include <machine/reg.h> 7626f9a767SRodney W. Grimes 77b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 78b9df5231SPoul-Henning Kamp 7905ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 8005ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 815dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 82906ac69dSDavid Xu static int do_execve(struct thread *td, char *fname, char **argv, 83906ac69dSDavid Xu char **envv, struct mac *mac_p); 8405ba50f5SJake Burkholder 859701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 8605ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 8705ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 88486bddb0SDoug Rabson 899701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9005ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9105ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 9299ac3bc8SPeter Wemm 935dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 945dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 955dadd17bSJake Burkholder 96b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 97c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 989701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 99b9df5231SPoul-Henning Kamp 10005ba50f5SJake Burkholder static int 10105ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 10205ba50f5SJake Burkholder { 10305ba50f5SJake Burkholder struct proc *p; 104df7c361eSPeter Wemm int error; 10505ba50f5SJake Burkholder 10605ba50f5SJake Burkholder p = curproc; 107df7c361eSPeter Wemm #if defined(__amd64__) || defined(__ia64__) 108df7c361eSPeter Wemm if (req->oldlen == sizeof(unsigned int)) { 109df7c361eSPeter Wemm unsigned int val; 110df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_psstrings; 111df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 112df7c361eSPeter Wemm } else 113df7c361eSPeter Wemm #endif 114df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 115df7c361eSPeter Wemm sizeof(p->p_sysent->sv_psstrings)); 116df7c361eSPeter Wemm return error; 11705ba50f5SJake Burkholder } 11805ba50f5SJake Burkholder 11905ba50f5SJake Burkholder static int 12005ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12105ba50f5SJake Burkholder { 12205ba50f5SJake Burkholder struct proc *p; 123df7c361eSPeter Wemm int error; 12405ba50f5SJake Burkholder 12505ba50f5SJake Burkholder p = curproc; 126df7c361eSPeter Wemm #if defined(__amd64__) || defined(__ia64__) 127df7c361eSPeter Wemm if (req->oldlen == sizeof(unsigned int)) { 128df7c361eSPeter Wemm unsigned int val; 129df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_usrstack; 130df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 131df7c361eSPeter Wemm } else 132df7c361eSPeter Wemm #endif 133df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 134df7c361eSPeter Wemm sizeof(p->p_sysent->sv_usrstack)); 135df7c361eSPeter Wemm return error; 13605ba50f5SJake Burkholder } 13705ba50f5SJake Burkholder 1385dadd17bSJake Burkholder static int 1395dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1405dadd17bSJake Burkholder { 1415dadd17bSJake Burkholder struct proc *p; 1425dadd17bSJake Burkholder 1435dadd17bSJake Burkholder p = curproc; 1445dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1455dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1465dadd17bSJake Burkholder } 1475dadd17bSJake Burkholder 14899ac3bc8SPeter Wemm /* 149aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 150aa855a59SPeter Wemm * double pointer here. 151df8bae1dSRodney W. Grimes */ 152aa855a59SPeter Wemm static const struct execsw **execsw; 15326f9a767SRodney W. Grimes 154ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 155ca46e90eSBruce Evans struct execve_args { 156ca46e90eSBruce Evans char *fname; 157ca46e90eSBruce Evans char **argv; 158ca46e90eSBruce Evans char **envv; 159ca46e90eSBruce Evans }; 160ca46e90eSBruce Evans #endif 161ca46e90eSBruce Evans 162ca46e90eSBruce Evans /* 163ca46e90eSBruce Evans * MPSAFE 164ca46e90eSBruce Evans */ 165ca46e90eSBruce Evans int 166ca46e90eSBruce Evans execve(td, uap) 167ca46e90eSBruce Evans struct thread *td; 168ca46e90eSBruce Evans struct execve_args /* { 169ca46e90eSBruce Evans char *fname; 170ca46e90eSBruce Evans char **argv; 171ca46e90eSBruce Evans char **envv; 172ca46e90eSBruce Evans } */ *uap; 173ca46e90eSBruce Evans { 174ca46e90eSBruce Evans 175ca46e90eSBruce Evans return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 176ca46e90eSBruce Evans } 177ca46e90eSBruce Evans 178ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 179ca46e90eSBruce Evans struct __mac_execve_args { 180ca46e90eSBruce Evans char *fname; 181ca46e90eSBruce Evans char **argv; 182ca46e90eSBruce Evans char **envv; 183ca46e90eSBruce Evans struct mac *mac_p; 184ca46e90eSBruce Evans }; 185ca46e90eSBruce Evans #endif 186ca46e90eSBruce Evans 187ca46e90eSBruce Evans /* 188ca46e90eSBruce Evans * MPSAFE 189ca46e90eSBruce Evans */ 190ca46e90eSBruce Evans int 191ca46e90eSBruce Evans __mac_execve(td, uap) 192ca46e90eSBruce Evans struct thread *td; 193ca46e90eSBruce Evans struct __mac_execve_args /* { 194ca46e90eSBruce Evans char *fname; 195ca46e90eSBruce Evans char **argv; 196ca46e90eSBruce Evans char **envv; 197ca46e90eSBruce Evans struct mac *mac_p; 198ca46e90eSBruce Evans } */ *uap; 199ca46e90eSBruce Evans { 200ca46e90eSBruce Evans 201ca46e90eSBruce Evans #ifdef MAC 202ca46e90eSBruce Evans return (kern_execve(td, uap->fname, uap->argv, uap->envv, 203ca46e90eSBruce Evans uap->mac_p)); 204ca46e90eSBruce Evans #else 205ca46e90eSBruce Evans return (ENOSYS); 206ca46e90eSBruce Evans #endif 207ca46e90eSBruce Evans } 208ca46e90eSBruce Evans 20984e0b075SDavid Xu int 210906ac69dSDavid Xu kern_execve(td, fname, argv, envv, mac_p) 211906ac69dSDavid Xu struct thread *td; 212906ac69dSDavid Xu char *fname; 213906ac69dSDavid Xu char **argv; 214906ac69dSDavid Xu char **envv; 215906ac69dSDavid Xu struct mac *mac_p; 216906ac69dSDavid Xu { 217906ac69dSDavid Xu struct proc *p = td->td_proc; 218906ac69dSDavid Xu int error; 219906ac69dSDavid Xu 220906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 221906ac69dSDavid Xu PROC_LOCK(p); 222906ac69dSDavid Xu if (thread_single(SINGLE_BOUNDARY)) { 223906ac69dSDavid Xu PROC_UNLOCK(p); 224906ac69dSDavid Xu return (ERESTART); /* Try again later. */ 225906ac69dSDavid Xu } 226906ac69dSDavid Xu PROC_UNLOCK(p); 227906ac69dSDavid Xu } 228906ac69dSDavid Xu 229906ac69dSDavid Xu error = do_execve(td, fname, argv, envv, mac_p); 230906ac69dSDavid Xu 231906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 232906ac69dSDavid Xu PROC_LOCK(p); 233906ac69dSDavid Xu /* 234906ac69dSDavid Xu * If success, we upgrade to SINGLE_EXIT state to 235906ac69dSDavid Xu * force other threads to suicide. 236906ac69dSDavid Xu */ 237906ac69dSDavid Xu if (error == 0) 238906ac69dSDavid Xu thread_single(SINGLE_EXIT); 239906ac69dSDavid Xu else 240906ac69dSDavid Xu thread_single_end(); 241906ac69dSDavid Xu PROC_UNLOCK(p); 242906ac69dSDavid Xu } 243906ac69dSDavid Xu 244906ac69dSDavid Xu return (error); 245906ac69dSDavid Xu } 246906ac69dSDavid Xu 24726f9a767SRodney W. Grimes /* 248450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 249450ffb44SRobert Watson * userspace pointers from the passed thread. 250116734c4SMatthew Dillon * 251116734c4SMatthew Dillon * MPSAFE 25226f9a767SRodney W. Grimes */ 253450ffb44SRobert Watson static int 254906ac69dSDavid Xu do_execve(td, fname, argv, envv, mac_p) 255b40ce416SJulian Elischer struct thread *td; 256450ffb44SRobert Watson char *fname; 257450ffb44SRobert Watson char **argv; 258450ffb44SRobert Watson char **envv; 259670cb89bSRobert Watson struct mac *mac_p; 260df8bae1dSRodney W. Grimes { 261b40ce416SJulian Elischer struct proc *p = td->td_proc; 26226f9a767SRodney W. Grimes struct nameidata nd, *ndp; 2639b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 2641419eacbSAlfred Perlstein struct uidinfo *euip; 265654f6be1SBruce Evans register_t *stack_base; 266bb56ec4aSPoul-Henning Kamp int error, len, i; 267c52007c2SDavid Greenman struct image_params image_params, *imgp; 26826f9a767SRodney W. Grimes struct vattr attr; 2694d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 27069be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 27190af4afaSJohn Baldwin struct sigacts *oldsigacts, *newsigacts; 2726c84de02SJohn Baldwin #ifdef KTRACE 2736c84de02SJohn Baldwin struct vnode *tracevp = NULL; 274a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 2756c84de02SJohn Baldwin #endif 2766c84de02SJohn Baldwin struct vnode *textvp = NULL; 277d06c0d4dSRobert Watson int credential_changing; 278619eb6e5SJeff Roberson int textset; 279ccafe7ebSRobert Watson #ifdef MAC 280eca8a663SRobert Watson struct label *interplabel = NULL; 281eca8a663SRobert Watson int will_transition; 282ccafe7ebSRobert Watson #endif 28326f9a767SRodney W. Grimes 284c52007c2SDavid Greenman imgp = &image_params; 285df8bae1dSRodney W. Grimes 2869ca45e81SDag-Erling Smørgrav /* 2879ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 2889ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 2899ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 2909ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 2919ca45e81SDag-Erling Smørgrav * privileges. 2929ca45e81SDag-Erling Smørgrav */ 2939ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 2949ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 29591f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 2969ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2979ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2989ca45e81SDag-Erling Smørgrav 299df8bae1dSRodney W. Grimes /* 300c52007c2SDavid Greenman * Initialize part of the common data 301df8bae1dSRodney W. Grimes */ 302c52007c2SDavid Greenman imgp->proc = p; 303450ffb44SRobert Watson imgp->userspace_argv = argv; 304450ffb44SRobert Watson imgp->userspace_envv = envv; 305670cb89bSRobert Watson imgp->execlabel = NULL; 306c52007c2SDavid Greenman imgp->attr = &attr; 307c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 3085cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 309c52007c2SDavid Greenman imgp->entry_addr = 0; 310c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 311c52007c2SDavid Greenman imgp->interpreted = 0; 312c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 313e1743d02SSøren Schmidt imgp->auxargs = NULL; 3141616db3cSJohn Dyson imgp->vp = NULL; 3150b2ed1aeSJeff Roberson imgp->object = NULL; 3161616db3cSJohn Dyson imgp->firstpage = NULL; 3174fe88fe6SJohn Polstra imgp->ps_strings = 0; 318b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 31926f9a767SRodney W. Grimes 320670cb89bSRobert Watson #ifdef MAC 321eca8a663SRobert Watson error = mac_execve_enter(imgp, mac_p); 322670cb89bSRobert Watson if (error) { 323670cb89bSRobert Watson mtx_lock(&Giant); 324670cb89bSRobert Watson goto exec_fail; 325670cb89bSRobert Watson } 326670cb89bSRobert Watson #endif 327670cb89bSRobert Watson 32826f9a767SRodney W. Grimes /* 32926f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 33026f9a767SRodney W. Grimes * environment strings 33126f9a767SRodney W. Grimes */ 33259c8bc40SAlan Cox imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX); 333c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 33426f9a767SRodney W. Grimes error = ENOMEM; 335b3afd20dSAlan Cox mtx_lock(&Giant); 33626f9a767SRodney W. Grimes goto exec_fail; 33726f9a767SRodney W. Grimes } 338c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 339c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 34059c8bc40SAlan Cox imgp->image_header = NULL; 34126f9a767SRodney W. Grimes 34226f9a767SRodney W. Grimes /* 34326f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 34426f9a767SRodney W. Grimes * in ni_vp amoung other things. 34526f9a767SRodney W. Grimes */ 34626f9a767SRodney W. Grimes ndp = &nd; 347b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 348450ffb44SRobert Watson UIO_USERSPACE, fname, td); 34926f9a767SRodney W. Grimes 350b3afd20dSAlan Cox mtx_lock(&Giant); 35126f9a767SRodney W. Grimes interpret: 35226f9a767SRodney W. Grimes 35326f9a767SRodney W. Grimes error = namei(ndp); 35426f9a767SRodney W. Grimes if (error) { 3551616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 35659c8bc40SAlan Cox ARG_MAX); 35726f9a767SRodney W. Grimes goto exec_fail; 35826f9a767SRodney W. Grimes } 35926f9a767SRodney W. Grimes 360c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 361450ffb44SRobert Watson imgp->fname = fname; 36226f9a767SRodney W. Grimes 36326f9a767SRodney W. Grimes /* 3649022e4adSDavid Greenman * Check file permissions (also 'opens' file) 3659022e4adSDavid Greenman */ 366c52007c2SDavid Greenman error = exec_check_permissions(imgp); 367619eb6e5SJeff Roberson if (error) 36826f9a767SRodney W. Grimes goto exec_fail_dealloc; 369619eb6e5SJeff Roberson 370619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 3710b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 37226f9a767SRodney W. Grimes 373619eb6e5SJeff Roberson /* 374619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 375619eb6e5SJeff Roberson * activating it. 376619eb6e5SJeff Roberson * 377619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 378619eb6e5SJeff Roberson * actually an executable image. 379619eb6e5SJeff Roberson */ 380619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 381619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 382619eb6e5SJeff Roberson 3831616db3cSJohn Dyson error = exec_map_first_page(imgp); 3846d5a0a8cSDavid Greenman if (error) 38526f9a767SRodney W. Grimes goto exec_fail_dealloc; 38626f9a767SRodney W. Grimes 38726f9a767SRodney W. Grimes /* 388d323ddf3SMatthew Dillon * If the current process has a special image activator it 389d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 390d323ddf3SMatthew Dillon * scripts differently. 39126f9a767SRodney W. Grimes */ 392d323ddf3SMatthew Dillon error = -1; 393d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 394d323ddf3SMatthew Dillon error = img_first(imgp); 395d323ddf3SMatthew Dillon 396d323ddf3SMatthew Dillon /* 397d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 398d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 399d323ddf3SMatthew Dillon * and an error otherwise. 400d323ddf3SMatthew Dillon */ 401d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 402d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 403d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 404d323ddf3SMatthew Dillon continue; 405d323ddf3SMatthew Dillon } 406c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 407d323ddf3SMatthew Dillon } 408d323ddf3SMatthew Dillon 409d323ddf3SMatthew Dillon if (error) { 410619eb6e5SJeff Roberson if (error == -1) { 411619eb6e5SJeff Roberson if (textset == 0) 412619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 413d323ddf3SMatthew Dillon error = ENOEXEC; 414619eb6e5SJeff Roberson } 41526f9a767SRodney W. Grimes goto exec_fail_dealloc; 416d323ddf3SMatthew Dillon } 417d323ddf3SMatthew Dillon 418d323ddf3SMatthew Dillon /* 419d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 420d323ddf3SMatthew Dillon * activate the interpreter. 421d323ddf3SMatthew Dillon */ 422c52007c2SDavid Greenman if (imgp->interpreted) { 4231616db3cSJohn Dyson exec_unmap_first_page(imgp); 424619eb6e5SJeff Roberson /* 425619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 426619eb6e5SJeff Roberson * period before we determine that something is a script where 427619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 428619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 429619eb6e5SJeff Roberson */ 430619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 431762e6b85SEivind Eklund /* free name buffer and old vnode */ 432762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 433670cb89bSRobert Watson #ifdef MAC 434eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 435eca8a663SRobert Watson mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel); 436670cb89bSRobert Watson #endif 437619eb6e5SJeff Roberson vput(ndp->ni_vp); 4380b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 4390b2ed1aeSJeff Roberson imgp->object = NULL; 44026f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 441b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 442b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 44326f9a767SRodney W. Grimes goto interpret; 44426f9a767SRodney W. Grimes } 44526f9a767SRodney W. Grimes 44626f9a767SRodney W. Grimes /* 44726f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 44826f9a767SRodney W. Grimes */ 4493ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 4503ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 4513ebc1248SPeter Wemm else 452c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 45326f9a767SRodney W. Grimes 45426f9a767SRodney W. Grimes /* 4551e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 4561e1e0b44SSøren Schmidt * let it do the stack setup. 4571e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 45826f9a767SRodney W. Grimes */ 459d6c847f3SBruce Evans if (p->p_sysent->sv_fixup != NULL) 460c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 4611e1e0b44SSøren Schmidt else 462c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 46326f9a767SRodney W. Grimes 464a78e8d2aSDavid Greenman /* 465a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 466a78e8d2aSDavid Greenman * be shared after an exec. 467a78e8d2aSDavid Greenman */ 468426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 469a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 470a78e8d2aSDavid Greenman struct filedesc *tmp; 471a78e8d2aSDavid Greenman 472c522c1bfSAlfred Perlstein tmp = fdcopy(td->td_proc->p_fd); 473426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 474b40ce416SJulian Elischer fdfree(td); 475a78e8d2aSDavid Greenman p->p_fd = tmp; 476426da3bcSAlfred Perlstein } else 477426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 478a78e8d2aSDavid Greenman 479333ea485SGuido van Rooij /* 4809b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4819b3b1c5fSJohn Baldwin */ 4829b3b1c5fSJohn Baldwin newcred = crget(); 4831419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 4849b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 4859b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 4869b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4879b3b1c5fSJohn Baldwin 4889b3b1c5fSJohn Baldwin /* close files on exec */ 4899b3b1c5fSJohn Baldwin fdcloseexec(td); 4909b3b1c5fSJohn Baldwin 491619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 492619eb6e5SJeff Roberson VREF(ndp->ni_vp); 493619eb6e5SJeff Roberson 4949b3b1c5fSJohn Baldwin /* 495333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4968688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 497b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 498333ea485SGuido van Rooij * reset. 499333ea485SGuido van Rooij */ 5009b3b1c5fSJohn Baldwin PROC_LOCK(p); 50190af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 50290af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 5039b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 50490af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 50590af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 5069b3b1c5fSJohn Baldwin PROC_LOCK(p); 50790af4afaSJohn Baldwin p->p_sigacts = newsigacts; 50890af4afaSJohn Baldwin } else 50990af4afaSJohn Baldwin oldsigacts = NULL; 510333ea485SGuido van Rooij 511fdf4e8b3SWarner Losh /* Stop profiling */ 512fdf4e8b3SWarner Losh stopprofclock(p); 513fdf4e8b3SWarner Losh 51426f9a767SRodney W. Grimes /* reset caught signals */ 51526f9a767SRodney W. Grimes execsigs(p); 51626f9a767SRodney W. Grimes 51726f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 51826f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 51926f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 52026f9a767SRodney W. Grimes p->p_comm[len] = 0; 52126f9a767SRodney W. Grimes 52226f9a767SRodney W. Grimes /* 52324b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 524dc733423SDag-Erling Smørgrav * it that it now has its own resources back 52526f9a767SRodney W. Grimes */ 52626f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 52726f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 52826f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 5297f05b035SAlfred Perlstein wakeup(p->p_pptr); 53026f9a767SRodney W. Grimes } 53126f9a767SRodney W. Grimes 53226f9a767SRodney W. Grimes /* 533a78e8d2aSDavid Greenman * Implement image setuid/setgid. 534a78e8d2aSDavid Greenman * 535a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 536a78e8d2aSDavid Greenman * the process is being traced. 537670cb89bSRobert Watson * 538670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 539670cb89bSRobert Watson * transitions on the file system. 540c52007c2SDavid Greenman */ 541b1fc0ec1SRobert Watson oldcred = p->p_ucred; 542d06c0d4dSRobert Watson credential_changing = 0; 543d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 544d06c0d4dSRobert Watson attr.va_uid; 545d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 546d06c0d4dSRobert Watson attr.va_gid; 547ccafe7ebSRobert Watson #ifdef MAC 548670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 549eca8a663SRobert Watson interplabel, imgp); 550ccafe7ebSRobert Watson credential_changing |= will_transition; 551ccafe7ebSRobert Watson #endif 552d06c0d4dSRobert Watson 553d06c0d4dSRobert Watson if (credential_changing && 554a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 555c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 556c52007c2SDavid Greenman /* 557c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 558b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 559b85db196SPeter Wemm * we do not regain any tracing during a possible block. 56026f9a767SRodney W. Grimes */ 561b85db196SPeter Wemm setsugid(p); 5626c84de02SJohn Baldwin #ifdef KTRACE 56356f21b9dSColin Percival if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) { 5646c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 56579deba82SMatthew Dillon p->p_traceflag = 0; 566a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 567a5881ea5SJohn Baldwin p->p_tracevp = NULL; 568a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 569a5881ea5SJohn Baldwin p->p_tracecred = NULL; 5706c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 57126f9a767SRodney W. Grimes } 5726c84de02SJohn Baldwin #endif 57328b325aaSDon Lewis /* 574c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 575c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 57628b325aaSDon Lewis * 577c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 578c1e2d386SNate Lawson * which may grab the process lock. 57928b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 58028b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 58128b325aaSDon Lewis */ 58228b325aaSDon Lewis PROC_UNLOCK(p); 583c1e2d386SNate Lawson setugidsafety(td); 584e983a376SJacques Vidrine error = fdcheckstd(td); 58563c9e754SJohn Baldwin if (error != 0) 58669be5db9SAlfred Perlstein goto done1; 587e1b1aa3bSJohn Baldwin PROC_LOCK(p); 588c52007c2SDavid Greenman /* 589c52007c2SDavid Greenman * Set the new credentials. 590c52007c2SDavid Greenman */ 5919b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 592c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5931419eacbSAlfred Perlstein change_euid(newcred, euip); 594c52007c2SDavid Greenman if (attr.va_mode & VSGID) 595b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 596ccafe7ebSRobert Watson #ifdef MAC 597670cb89bSRobert Watson if (will_transition) { 598670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 599eca8a663SRobert Watson interplabel, imgp); 600670cb89bSRobert Watson } 601ccafe7ebSRobert Watson #endif 6029b3b1c5fSJohn Baldwin /* 6039b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 604ccafe7ebSRobert Watson * 605ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 606ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 607ccafe7ebSRobert Watson * though maybe it shouldn't. 6089b3b1c5fSJohn Baldwin */ 6099b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 6109b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 6119b3b1c5fSJohn Baldwin p->p_ucred = newcred; 6129b3b1c5fSJohn Baldwin newcred = NULL; 613c52007c2SDavid Greenman } else { 614b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 615b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 616c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 61726f9a767SRodney W. Grimes /* 618c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 619b1fc0ec1SRobert Watson * 620b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 6219b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 6229b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 6239b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 6249b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 6259b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 6269b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 6279b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 62826f9a767SRodney W. Grimes */ 629b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 630b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 6319b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 632b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 633b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 634b1fc0ec1SRobert Watson p->p_ucred = newcred; 6359b3b1c5fSJohn Baldwin newcred = NULL; 6369b3b1c5fSJohn Baldwin } 637b1fc0ec1SRobert Watson } 63826f9a767SRodney W. Grimes 63926f9a767SRodney W. Grimes /* 640619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 641619eb6e5SJeff Roberson * to locking the proc lock. 6422a531c80SDavid Greenman */ 6439b3b1c5fSJohn Baldwin textvp = p->p_textvp; 6442a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 6452a531c80SDavid Greenman 6462a531c80SDavid Greenman /* 6479ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 6489ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 649cb679c38SJonathan Lemon */ 650ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 6519ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 652cb679c38SJonathan Lemon 653cb679c38SJonathan Lemon /* 65426f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 65526f9a767SRodney W. Grimes * can be set before the program executes. 656906ac69dSDavid Xu * Use tdsignal to deliver signal to current thread, use 657906ac69dSDavid Xu * psignal may cause the signal to be delivered to wrong thread 658906ac69dSDavid Xu * because that thread will exit, remember we are going to enter 659906ac69dSDavid Xu * single thread mode. 66026f9a767SRodney W. Grimes */ 66126f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 662906ac69dSDavid Xu tdsignal(td, SIGTRAP, SIGTARGET_TD); 66326f9a767SRodney W. Grimes 66426f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 66526f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 66626f9a767SRodney W. Grimes 667b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 6689b3b1c5fSJohn Baldwin oldargs = p->p_args; 669b9df5231SPoul-Henning Kamp p->p_args = NULL; 670b9df5231SPoul-Henning Kamp 671e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 672e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 673e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 674e1b1aa3bSJohn Baldwin p->p_args = newargs; 675e1b1aa3bSJohn Baldwin newargs = NULL; 676e1b1aa3bSJohn Baldwin } 677e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 678e1b1aa3bSJohn Baldwin 679e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 6803ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 6813ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 6823ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 6833ebc1248SPeter Wemm else 684bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 685bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 686e913ca22SDoug Rabson 68769be5db9SAlfred Perlstein done1: 6889b3b1c5fSJohn Baldwin /* 6899b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 6909b3b1c5fSJohn Baldwin */ 6911419eacbSAlfred Perlstein uifree(euip); 6929b3b1c5fSJohn Baldwin if (newcred == NULL) 6939b3b1c5fSJohn Baldwin crfree(oldcred); 6949b3b1c5fSJohn Baldwin else 6959b3b1c5fSJohn Baldwin crfree(newcred); 6969b3b1c5fSJohn Baldwin /* 6979b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 6989b3b1c5fSJohn Baldwin */ 6999b3b1c5fSJohn Baldwin if (textvp != NULL) 7009b3b1c5fSJohn Baldwin vrele(textvp); 701619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 702619eb6e5SJeff Roberson vrele(ndp->ni_vp); 7036c84de02SJohn Baldwin #ifdef KTRACE 7049b3b1c5fSJohn Baldwin if (tracevp != NULL) 7059b3b1c5fSJohn Baldwin vrele(tracevp); 706a5881ea5SJohn Baldwin if (tracecred != NULL) 707a5881ea5SJohn Baldwin crfree(tracecred); 7086c84de02SJohn Baldwin #endif 70969be5db9SAlfred Perlstein if (oldargs != NULL) 7109b3b1c5fSJohn Baldwin pargs_drop(oldargs); 71169be5db9SAlfred Perlstein if (newargs != NULL) 71269be5db9SAlfred Perlstein pargs_drop(newargs); 71390af4afaSJohn Baldwin if (oldsigacts != NULL) 71490af4afaSJohn Baldwin sigacts_free(oldsigacts); 715b9df5231SPoul-Henning Kamp 7161616db3cSJohn Dyson exec_fail_dealloc: 7171616db3cSJohn Dyson 71826f9a767SRodney W. Grimes /* 71926f9a767SRodney W. Grimes * free various allocated resources 72026f9a767SRodney W. Grimes */ 721d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7221616db3cSJohn Dyson exec_unmap_first_page(imgp); 72326f9a767SRodney W. Grimes 724d6c847f3SBruce Evans if (imgp->vp != NULL) { 725762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 726619eb6e5SJeff Roberson vput(imgp->vp); 727a3cf6ebaSDavid Greenman } 72826f9a767SRodney W. Grimes 729a9a08882SJeff Roberson if (imgp->stringbase != NULL) 730a9a08882SJeff Roberson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 73159c8bc40SAlan Cox ARG_MAX); 732a9a08882SJeff Roberson 733d6c847f3SBruce Evans if (imgp->object != NULL) 7340b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 7350b2ed1aeSJeff Roberson 736d1989db5SRobert Drehmel if (error == 0) { 737d1989db5SRobert Drehmel /* 738d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 739d1989db5SRobert Drehmel * the S_EXEC bit set. 740d1989db5SRobert Drehmel */ 741d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 742116734c4SMatthew Dillon goto done2; 743d1989db5SRobert Drehmel } 7441616db3cSJohn Dyson 74526f9a767SRodney W. Grimes exec_fail: 7469ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 7479ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 7489ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 7499ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 7509ca45e81SDag-Erling Smørgrav 751c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 75226f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 753670cb89bSRobert Watson #ifdef MAC 754670cb89bSRobert Watson mac_execve_exit(imgp); 755eca8a663SRobert Watson if (interplabel != NULL) 756eca8a663SRobert Watson mac_vnode_label_free(interplabel); 757670cb89bSRobert Watson #endif 75863993cf0SJohn Baldwin mtx_unlock(&Giant); 759b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 76026f9a767SRodney W. Grimes /* NOT REACHED */ 761116734c4SMatthew Dillon error = 0; 76226f9a767SRodney W. Grimes } 763116734c4SMatthew Dillon done2: 764670cb89bSRobert Watson #ifdef MAC 765670cb89bSRobert Watson mac_execve_exit(imgp); 766eca8a663SRobert Watson if (interplabel != NULL) 767eca8a663SRobert Watson mac_vnode_label_free(interplabel); 768670cb89bSRobert Watson #endif 769116734c4SMatthew Dillon mtx_unlock(&Giant); 770116734c4SMatthew Dillon return (error); 77126f9a767SRodney W. Grimes } 77226f9a767SRodney W. Grimes 7731616db3cSJohn Dyson int 7741616db3cSJohn Dyson exec_map_first_page(imgp) 7751616db3cSJohn Dyson struct image_params *imgp; 7761616db3cSJohn Dyson { 777d8aad40cSJohn Baldwin int rv, i; 77895461b45SJohn Dyson int initial_pagein; 77995461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7801616db3cSJohn Dyson vm_object_t object; 7811616db3cSJohn Dyson 7820cddd8f0SMatthew Dillon GIANT_REQUIRED; 7831616db3cSJohn Dyson 784d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7851616db3cSJohn Dyson exec_unmap_first_page(imgp); 7861616db3cSJohn Dyson 7879ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 788fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 78995461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 79095461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 79195461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 79295461b45SJohn Dyson if (initial_pagein > object->size) 79395461b45SJohn Dyson initial_pagein = object->size; 79495461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 795d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 7966ec2fca5SAlan Cox if (ma[i]->valid) 7976ec2fca5SAlan Cox break; 798ee113343SAlan Cox vm_page_lock_queues(); 79906fa71cdSAlan Cox if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) { 80006fa71cdSAlan Cox vm_page_unlock_queues(); 80106fa71cdSAlan Cox break; 80206fa71cdSAlan Cox } 803e69763a3SDoug Rabson vm_page_busy(ma[i]); 804ee113343SAlan Cox vm_page_unlock_queues(); 80595461b45SJohn Dyson } else { 806ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 807ca0387efSJake Burkholder VM_ALLOC_NORMAL); 80895461b45SJohn Dyson if (ma[i] == NULL) 80995461b45SJohn Dyson break; 81095461b45SJohn Dyson } 81195461b45SJohn Dyson } 81295461b45SJohn Dyson initial_pagein = i; 81395461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 81495461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 815ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 816ca0387efSJake Burkholder (ma[0]->valid == 0)) { 817ecbb00a2SDoug Rabson if (ma[0]) { 8186ec2fca5SAlan Cox vm_page_lock_queues(); 8194fec79beSAlan Cox pmap_remove_all(ma[0]); 8208f9110f6SJohn Dyson vm_page_free(ma[0]); 82106fa71cdSAlan Cox vm_page_unlock_queues(); 8226ec2fca5SAlan Cox } 82306fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 824a7cddfedSJake Burkholder return (EIO); 8251616db3cSJohn Dyson } 8261616db3cSJohn Dyson } 8276ec2fca5SAlan Cox vm_page_lock_queues(); 828148b3f62SAlan Cox vm_page_hold(ma[0]); 829e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 83097646f56SAlan Cox vm_page_unlock_queues(); 8316ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 8321616db3cSJohn Dyson 83359c8bc40SAlan Cox imgp->firstpage = sf_buf_alloc(ma[0], 0); 83459c8bc40SAlan Cox imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 8351616db3cSJohn Dyson 836a7cddfedSJake Burkholder return (0); 8371616db3cSJohn Dyson } 8381616db3cSJohn Dyson 8391616db3cSJohn Dyson void 8401616db3cSJohn Dyson exec_unmap_first_page(imgp) 8411616db3cSJohn Dyson struct image_params *imgp; 8421616db3cSJohn Dyson { 84359c8bc40SAlan Cox vm_page_t m; 84423955314SAlfred Perlstein 845d6c847f3SBruce Evans if (imgp->firstpage != NULL) { 84659c8bc40SAlan Cox m = sf_buf_page(imgp->firstpage); 84759c8bc40SAlan Cox sf_buf_free(imgp->firstpage); 8481616db3cSJohn Dyson imgp->firstpage = NULL; 84959c8bc40SAlan Cox vm_page_lock_queues(); 85059c8bc40SAlan Cox vm_page_unhold(m); 85159c8bc40SAlan Cox vm_page_unlock_queues(); 8521616db3cSJohn Dyson } 8531616db3cSJohn Dyson } 8541616db3cSJohn Dyson 85526f9a767SRodney W. Grimes /* 85626f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 85726f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 85826f9a767SRodney W. Grimes * automatically in trap.c. 85926f9a767SRodney W. Grimes */ 86026f9a767SRodney W. Grimes int 86105ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 862c52007c2SDavid Greenman struct image_params *imgp; 86305ba50f5SJake Burkholder struct sysentvec *sv; 86426f9a767SRodney W. Grimes { 86526f9a767SRodney W. Grimes int error; 8665e20c11fSAlan Cox struct proc *p = imgp->proc; 8675e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 86805ba50f5SJake Burkholder vm_offset_t stack_addr; 86905ba50f5SJake Burkholder vm_map_t map; 87026f9a767SRodney W. Grimes 8710cddd8f0SMatthew Dillon GIANT_REQUIRED; 8720cddd8f0SMatthew Dillon 873c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 87426f9a767SRodney W. Grimes 875a5bdcb2aSPeter Wemm /* Called with Giant held, do not depend on it! */ 87675b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exec, p); 8776f5dafeaSAlan Cox 8786f5dafeaSAlan Cox /* 879c460ac3aSPeter Wemm * Here is as good a place as any to do any resource limit cleanups. 880c460ac3aSPeter Wemm * This is needed if a 64 bit binary exec's a 32 bit binary - the 881c460ac3aSPeter Wemm * data size limit may need to be changed to a value that makes 882c460ac3aSPeter Wemm * sense for the 32 bit binary. 883c460ac3aSPeter Wemm */ 884d6c847f3SBruce Evans if (sv->sv_fixlimits != NULL) 885c460ac3aSPeter Wemm sv->sv_fixlimits(imgp); 886c460ac3aSPeter Wemm 887c460ac3aSPeter Wemm /* 888af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 889af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 890af9ec885SJohn Dyson * not disrupted 891af9ec885SJohn Dyson */ 89205ba50f5SJake Burkholder map = &vmspace->vm_map; 89305ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 89405ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 8953db161e0SMatthew Dillon shmexit(vmspace); 89605ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 89705ba50f5SJake Burkholder vm_map_max(map)); 89805ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 899af9ec885SJohn Dyson } else { 90005ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 9015e20c11fSAlan Cox vmspace = p->p_vmspace; 90205ba50f5SJake Burkholder map = &vmspace->vm_map; 903af9ec885SJohn Dyson } 90426f9a767SRodney W. Grimes 90526f9a767SRodney W. Grimes /* Allocate a new stack */ 906fd75d710SMarcel Moolenaar stack_addr = sv->sv_usrstack - maxssiz; 90705ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 908fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 9092267af78SJulian Elischer if (error) 9102267af78SJulian Elischer return (error); 9112267af78SJulian Elischer 91263c47a5cSDoug Rabson #ifdef __ia64__ 913fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 914bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 915fd75d710SMarcel Moolenaar error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 916fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 917fd75d710SMarcel Moolenaar if (error) 918fd75d710SMarcel Moolenaar return (error); 91963c47a5cSDoug Rabson #endif 92063c47a5cSDoug Rabson 9212267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 9222267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 9232267af78SJulian Elischer * process stack so we can check the stack rlimit. 9242267af78SJulian Elischer */ 925cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 92605ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 92726f9a767SRodney W. Grimes 92826f9a767SRodney W. Grimes return (0); 92926f9a767SRodney W. Grimes } 93026f9a767SRodney W. Grimes 93126f9a767SRodney W. Grimes /* 93226f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 93326f9a767SRodney W. Grimes * address space into the temporary string buffer. 93426f9a767SRodney W. Grimes */ 93526f9a767SRodney W. Grimes int 936c52007c2SDavid Greenman exec_extract_strings(imgp) 937c52007c2SDavid Greenman struct image_params *imgp; 93826f9a767SRodney W. Grimes { 93926f9a767SRodney W. Grimes char **argv, **envv; 94026f9a767SRodney W. Grimes char *argp, *envp; 941ecbb00a2SDoug Rabson int error; 942ecbb00a2SDoug Rabson size_t length; 94326f9a767SRodney W. Grimes 94426f9a767SRodney W. Grimes /* 94526f9a767SRodney W. Grimes * extract arguments first 94626f9a767SRodney W. Grimes */ 94726f9a767SRodney W. Grimes 948450ffb44SRobert Watson argv = imgp->userspace_argv; 94926f9a767SRodney W. Grimes 9500fd1a014SDavid Greenman if (argv) { 951aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 9525cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 9535cf3d12cSAndrey A. Chernov return (EFAULT); 9545cf3d12cSAndrey A. Chernov if (argp) 9555cf3d12cSAndrey A. Chernov argv++; 9565cf3d12cSAndrey A. Chernov if (imgp->argv0) 9575cf3d12cSAndrey A. Chernov argp = imgp->argv0; 9585cf3d12cSAndrey A. Chernov if (argp) { 9595cf3d12cSAndrey A. Chernov do { 96026f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 96126f9a767SRodney W. Grimes return (EFAULT); 962c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 963c52007c2SDavid Greenman imgp->stringspace, &length))) { 9640fd1a014SDavid Greenman if (error == ENAMETOOLONG) 96526f9a767SRodney W. Grimes return (E2BIG); 9660fd1a014SDavid Greenman return (error); 9670fd1a014SDavid Greenman } 968c52007c2SDavid Greenman imgp->stringspace -= length; 969c52007c2SDavid Greenman imgp->stringp += length; 970c52007c2SDavid Greenman imgp->argc++; 971aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 97226f9a767SRodney W. Grimes } 9737700eb86SRuslan Ermilov } else 9747700eb86SRuslan Ermilov return (EFAULT); 97526f9a767SRodney W. Grimes 976b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 977b9df5231SPoul-Henning Kamp 97826f9a767SRodney W. Grimes /* 97926f9a767SRodney W. Grimes * extract environment strings 98026f9a767SRodney W. Grimes */ 98126f9a767SRodney W. Grimes 982450ffb44SRobert Watson envv = imgp->userspace_envv; 98326f9a767SRodney W. Grimes 9840fd1a014SDavid Greenman if (envv) { 985aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 98626f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 98726f9a767SRodney W. Grimes return (EFAULT); 988c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 989c52007c2SDavid Greenman imgp->stringspace, &length))) { 9900fd1a014SDavid Greenman if (error == ENAMETOOLONG) 99126f9a767SRodney W. Grimes return (E2BIG); 9920fd1a014SDavid Greenman return (error); 9930fd1a014SDavid Greenman } 994c52007c2SDavid Greenman imgp->stringspace -= length; 995c52007c2SDavid Greenman imgp->stringp += length; 996c52007c2SDavid Greenman imgp->envc++; 99726f9a767SRodney W. Grimes } 9980fd1a014SDavid Greenman } 99926f9a767SRodney W. Grimes 100026f9a767SRodney W. Grimes return (0); 100126f9a767SRodney W. Grimes } 100226f9a767SRodney W. Grimes 100326f9a767SRodney W. Grimes /* 100426f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 100526f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 100626f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 100726f9a767SRodney W. Grimes */ 1008654f6be1SBruce Evans register_t * 1009c52007c2SDavid Greenman exec_copyout_strings(imgp) 1010c52007c2SDavid Greenman struct image_params *imgp; 101126f9a767SRodney W. Grimes { 101226f9a767SRodney W. Grimes int argc, envc; 101326f9a767SRodney W. Grimes char **vectp; 101426f9a767SRodney W. Grimes char *stringp, *destp; 1015654f6be1SBruce Evans register_t *stack_base; 101693f6448cSDavid Greenman struct ps_strings *arginfo; 1017f3bec5d7SJake Burkholder struct proc *p; 1018d66a5066SPeter Wemm int szsigcode; 101926f9a767SRodney W. Grimes 102026f9a767SRodney W. Grimes /* 102126f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 1022d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 102326f9a767SRodney W. Grimes */ 1024f3bec5d7SJake Burkholder p = imgp->proc; 1025f3bec5d7SJake Burkholder szsigcode = 0; 102605ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1027f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1028f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1029d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 10301ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 10311ed012f9SPeter Wemm 103226f9a767SRodney W. Grimes /* 1033d66a5066SPeter Wemm * install sigcode 1034d66a5066SPeter Wemm */ 1035d66a5066SPeter Wemm if (szsigcode) 1036f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1037f3bec5d7SJake Burkholder szsigcode), szsigcode); 1038d66a5066SPeter Wemm 1039d66a5066SPeter Wemm /* 1040e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1041e1743d02SSøren Schmidt * on the stack. 1042e1743d02SSøren Schmidt */ 1043b9a22da4STakanori Watanabe if (imgp->auxargs) { 1044e1743d02SSøren Schmidt /* 1045b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1046b9a22da4STakanori Watanabe * lower compatibility. 1047b9a22da4STakanori Watanabe */ 1048ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1049ca0387efSJake Burkholder (AT_COUNT * 2); 1050b9a22da4STakanori Watanabe /* 1051b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1052b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1053b9a22da4STakanori Watanabe * for argument of Runtime loader. 1054e1743d02SSøren Schmidt */ 1055e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1056b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 1057b9a22da4STakanori Watanabe 1058b9a22da4STakanori Watanabe } else 1059e1743d02SSøren Schmidt /* 1060b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1061b9a22da4STakanori Watanabe * the arg and env vector sets 106226f9a767SRodney W. Grimes */ 1063ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1064ca0387efSJake Burkholder sizeof(char *)); 106526f9a767SRodney W. Grimes 106626f9a767SRodney W. Grimes /* 106726f9a767SRodney W. Grimes * vectp also becomes our initial stack base 106826f9a767SRodney W. Grimes */ 1069654f6be1SBruce Evans stack_base = (register_t *)vectp; 107026f9a767SRodney W. Grimes 1071c52007c2SDavid Greenman stringp = imgp->stringbase; 1072c52007c2SDavid Greenman argc = imgp->argc; 1073c52007c2SDavid Greenman envc = imgp->envc; 107426f9a767SRodney W. Grimes 107593f6448cSDavid Greenman /* 10763aed948bSDavid Greenman * Copy out strings - arguments and environment. 107793f6448cSDavid Greenman */ 1078c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 107993f6448cSDavid Greenman 108093f6448cSDavid Greenman /* 10813aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 10823aed948bSDavid Greenman */ 1083aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 10843aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 10853aed948bSDavid Greenman 10863aed948bSDavid Greenman /* 10873aed948bSDavid Greenman * Fill in argument portion of vector table. 108893f6448cSDavid Greenman */ 108926f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1090aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10913aed948bSDavid Greenman while (*stringp++ != 0) 10923aed948bSDavid Greenman destp++; 10933aed948bSDavid Greenman destp++; 109426f9a767SRodney W. Grimes } 109526f9a767SRodney W. Grimes 10961a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 10976ab46d52SBruce Evans suword(vectp++, 0); 109826f9a767SRodney W. Grimes 1099aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 11003aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 110193f6448cSDavid Greenman 110293f6448cSDavid Greenman /* 11033aed948bSDavid Greenman * Fill in environment portion of vector table. 110493f6448cSDavid Greenman */ 110526f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1106aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11073aed948bSDavid Greenman while (*stringp++ != 0) 11083aed948bSDavid Greenman destp++; 11093aed948bSDavid Greenman destp++; 111026f9a767SRodney W. Grimes } 111126f9a767SRodney W. Grimes 111226f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 11136ab46d52SBruce Evans suword(vectp, 0); 111426f9a767SRodney W. Grimes 111526f9a767SRodney W. Grimes return (stack_base); 111626f9a767SRodney W. Grimes } 111726f9a767SRodney W. Grimes 111826f9a767SRodney W. Grimes /* 111926f9a767SRodney W. Grimes * Check permissions of file to execute. 1120cf64863aSRobert Watson * Called with imgp->vp locked. 112126f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 112226f9a767SRodney W. Grimes */ 1123c8a79999SPeter Wemm int 1124c52007c2SDavid Greenman exec_check_permissions(imgp) 1125c52007c2SDavid Greenman struct image_params *imgp; 112626f9a767SRodney W. Grimes { 1127c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1128c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1129a854ed98SJohn Baldwin struct thread *td; 113026f9a767SRodney W. Grimes int error; 113126f9a767SRodney W. Grimes 1132a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1133339b79b9SRobert Watson 1134ec35c2afSRobert Watson /* Get file attributes */ 1135ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1136ec35c2afSRobert Watson if (error) 1137ec35c2afSRobert Watson return (error); 1138ec35c2afSRobert Watson 1139339b79b9SRobert Watson #ifdef MAC 1140670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1141339b79b9SRobert Watson if (error) 1142339b79b9SRobert Watson return (error); 1143339b79b9SRobert Watson #endif 1144339b79b9SRobert Watson 114526f9a767SRodney W. Grimes /* 114626f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 114726f9a767SRodney W. Grimes * file resides on. 114826f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 114926f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 115026f9a767SRodney W. Grimes * file really is executable. 115126f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 115226f9a767SRodney W. Grimes */ 1153c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 115426f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1155a854ed98SJohn Baldwin (attr->va_type != VREG)) 115626f9a767SRodney W. Grimes return (EACCES); 115726f9a767SRodney W. Grimes 115826f9a767SRodney W. Grimes /* 115926f9a767SRodney W. Grimes * Zero length files can't be exec'd 116026f9a767SRodney W. Grimes */ 116126f9a767SRodney W. Grimes if (attr->va_size == 0) 116226f9a767SRodney W. Grimes return (ENOEXEC); 116326f9a767SRodney W. Grimes 116426f9a767SRodney W. Grimes /* 116526f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 116626f9a767SRodney W. Grimes */ 1167a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 116826f9a767SRodney W. Grimes if (error) 116926f9a767SRodney W. Grimes return (error); 117026f9a767SRodney W. Grimes 11716d5a0a8cSDavid Greenman /* 11726d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 11736d5a0a8cSDavid Greenman * if there are any. 11746d5a0a8cSDavid Greenman */ 11756d5a0a8cSDavid Greenman if (vp->v_writecount) 11766d5a0a8cSDavid Greenman return (ETXTBSY); 11776d5a0a8cSDavid Greenman 11786d5a0a8cSDavid Greenman /* 11796d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 11806d5a0a8cSDavid Greenman * general case). 11816d5a0a8cSDavid Greenman */ 1182a8d43c90SPoul-Henning Kamp error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 118326f9a767SRodney W. Grimes return (error); 1184df8bae1dSRodney W. Grimes } 1185aa855a59SPeter Wemm 1186aa855a59SPeter Wemm /* 1187aa855a59SPeter Wemm * Exec handler registration 1188aa855a59SPeter Wemm */ 1189aa855a59SPeter Wemm int 1190aa855a59SPeter Wemm exec_register(execsw_arg) 1191aa855a59SPeter Wemm const struct execsw *execsw_arg; 1192aa855a59SPeter Wemm { 1193aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1194aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1195aa855a59SPeter Wemm 1196aa855a59SPeter Wemm if (execsw) 1197aa855a59SPeter Wemm for (es = execsw; *es; es++) 1198aa855a59SPeter Wemm count++; 1199a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1200aa855a59SPeter Wemm if (newexecsw == NULL) 1201a7cddfedSJake Burkholder return (ENOMEM); 1202aa855a59SPeter Wemm xs = newexecsw; 1203aa855a59SPeter Wemm if (execsw) 1204aa855a59SPeter Wemm for (es = execsw; *es; es++) 1205aa855a59SPeter Wemm *xs++ = *es; 1206aa855a59SPeter Wemm *xs++ = execsw_arg; 1207aa855a59SPeter Wemm *xs = NULL; 1208aa855a59SPeter Wemm if (execsw) 1209aa855a59SPeter Wemm free(execsw, M_TEMP); 1210aa855a59SPeter Wemm execsw = newexecsw; 1211a7cddfedSJake Burkholder return (0); 1212aa855a59SPeter Wemm } 1213aa855a59SPeter Wemm 1214aa855a59SPeter Wemm int 1215aa855a59SPeter Wemm exec_unregister(execsw_arg) 1216aa855a59SPeter Wemm const struct execsw *execsw_arg; 1217aa855a59SPeter Wemm { 1218aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1219aa855a59SPeter Wemm int count = 1; 1220aa855a59SPeter Wemm 1221aa855a59SPeter Wemm if (execsw == NULL) 1222aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1223aa855a59SPeter Wemm 1224aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1225aa855a59SPeter Wemm if (*es == execsw_arg) 1226aa855a59SPeter Wemm break; 1227aa855a59SPeter Wemm } 1228aa855a59SPeter Wemm if (*es == NULL) 1229a7cddfedSJake Burkholder return (ENOENT); 1230aa855a59SPeter Wemm for (es = execsw; *es; es++) 1231aa855a59SPeter Wemm if (*es != execsw_arg) 1232aa855a59SPeter Wemm count++; 1233a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1234aa855a59SPeter Wemm if (newexecsw == NULL) 1235a7cddfedSJake Burkholder return (ENOMEM); 1236aa855a59SPeter Wemm xs = newexecsw; 1237aa855a59SPeter Wemm for (es = execsw; *es; es++) 1238aa855a59SPeter Wemm if (*es != execsw_arg) 1239aa855a59SPeter Wemm *xs++ = *es; 1240aa855a59SPeter Wemm *xs = NULL; 1241aa855a59SPeter Wemm if (execsw) 1242aa855a59SPeter Wemm free(execsw, M_TEMP); 1243aa855a59SPeter Wemm execsw = newexecsw; 1244a7cddfedSJake Burkholder return (0); 1245aa855a59SPeter Wemm } 1246