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> 541e1e0b44SSøren Schmidt #include <sys/sysent.h> 55797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5699ac3bc8SPeter Wemm #include <sys/sysctl.h> 57333ea485SGuido van Rooij #include <sys/user.h> 58a794e791SBruce Evans #include <sys/vnode.h> 59c781aea8SPeter Wemm #ifdef KTRACE 60c781aea8SPeter Wemm #include <sys/ktrace.h> 61c781aea8SPeter Wemm #endif 6226f9a767SRodney W. Grimes 6326f9a767SRodney W. Grimes #include <vm/vm.h> 64efeaf95aSDavid Greenman #include <vm/vm_param.h> 65efeaf95aSDavid Greenman #include <vm/pmap.h> 661616db3cSJohn Dyson #include <vm/vm_page.h> 67efeaf95aSDavid Greenman #include <vm/vm_map.h> 6826f9a767SRodney W. Grimes #include <vm/vm_kern.h> 69efeaf95aSDavid Greenman #include <vm/vm_extern.h> 701ebd0c59SDavid Greenman #include <vm/vm_object.h> 711616db3cSJohn Dyson #include <vm/vm_pager.h> 7226f9a767SRodney W. Grimes 7326f9a767SRodney W. Grimes #include <machine/reg.h> 7426f9a767SRodney W. Grimes 75b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 76b9df5231SPoul-Henning Kamp 7705ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 7805ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 795dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 80450ffb44SRobert Watson static int kern_execve(struct thread *td, char *fname, char **argv, 81670cb89bSRobert Watson char **envv, struct mac *mac_p); 8205ba50f5SJake Burkholder 839701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 8405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 8505ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 86486bddb0SDoug Rabson 879701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 8805ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 8905ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 9099ac3bc8SPeter Wemm 915dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 925dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 935dadd17bSJake Burkholder 94b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 95c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 969701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 97b9df5231SPoul-Henning Kamp 98a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 99a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 100a8704f89SPoul-Henning Kamp 101911fc923SPeter Wemm #ifdef __ia64__ 102911fc923SPeter Wemm /* XXX HACK */ 103911fc923SPeter Wemm static int regstkpages = 256; 104911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 105911fc923SPeter Wemm #endif 106911fc923SPeter Wemm 10705ba50f5SJake Burkholder static int 10805ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 10905ba50f5SJake Burkholder { 11005ba50f5SJake Burkholder struct proc *p; 11105ba50f5SJake Burkholder 11205ba50f5SJake Burkholder p = curproc; 11305ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 11405ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 11505ba50f5SJake Burkholder } 11605ba50f5SJake Burkholder 11705ba50f5SJake Burkholder static int 11805ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 11905ba50f5SJake Burkholder { 12005ba50f5SJake Burkholder struct proc *p; 12105ba50f5SJake Burkholder 12205ba50f5SJake Burkholder p = curproc; 12305ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 12405ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 12505ba50f5SJake Burkholder } 12605ba50f5SJake Burkholder 1275dadd17bSJake Burkholder static int 1285dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1295dadd17bSJake Burkholder { 1305dadd17bSJake Burkholder struct proc *p; 1315dadd17bSJake Burkholder 1325dadd17bSJake Burkholder p = curproc; 1335dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1345dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1355dadd17bSJake Burkholder } 1365dadd17bSJake Burkholder 13799ac3bc8SPeter Wemm /* 138aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 139aa855a59SPeter Wemm * double pointer here. 140df8bae1dSRodney W. Grimes */ 141aa855a59SPeter Wemm static const struct execsw **execsw; 14226f9a767SRodney W. Grimes 14326f9a767SRodney W. Grimes /* 144450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 145450ffb44SRobert Watson * userspace pointers from the passed thread. 146116734c4SMatthew Dillon * 147116734c4SMatthew Dillon * MPSAFE 14826f9a767SRodney W. Grimes */ 149450ffb44SRobert Watson static int 150670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p) 151b40ce416SJulian Elischer struct thread *td; 152450ffb44SRobert Watson char *fname; 153450ffb44SRobert Watson char **argv; 154450ffb44SRobert Watson char **envv; 155670cb89bSRobert Watson struct mac *mac_p; 156df8bae1dSRodney W. Grimes { 157b40ce416SJulian Elischer struct proc *p = td->td_proc; 15826f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1599b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1601419eacbSAlfred Perlstein struct uidinfo *euip; 161654f6be1SBruce Evans register_t *stack_base; 162bb56ec4aSPoul-Henning Kamp int error, len, i; 163c52007c2SDavid Greenman struct image_params image_params, *imgp; 16426f9a767SRodney W. Grimes struct vattr attr; 1654d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 16669be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 16790af4afaSJohn Baldwin struct sigacts *oldsigacts, *newsigacts; 1686c84de02SJohn Baldwin #ifdef KTRACE 1696c84de02SJohn Baldwin struct vnode *tracevp = NULL; 170a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 1716c84de02SJohn Baldwin #endif 1726c84de02SJohn Baldwin struct vnode *textvp = NULL; 173d06c0d4dSRobert Watson int credential_changing; 174619eb6e5SJeff Roberson int textset; 175ccafe7ebSRobert Watson #ifdef MAC 176670cb89bSRobert Watson struct label interplabel; /* label of the interpreted vnode */ 177670cb89bSRobert Watson struct label execlabel; /* optional label argument */ 178670cb89bSRobert Watson int will_transition, interplabelvalid = 0; 179ccafe7ebSRobert Watson #endif 18026f9a767SRodney W. Grimes 181c52007c2SDavid Greenman imgp = &image_params; 182df8bae1dSRodney W. Grimes 1839ca45e81SDag-Erling Smørgrav /* 1849ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1859ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1869ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1879ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1889ca45e81SDag-Erling Smørgrav * privileges. 1899ca45e81SDag-Erling Smørgrav */ 1909ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1919ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 19291f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 1932c10d16aSJeff Roberson if (p->p_flag & P_THREADED || p->p_numthreads > 1) { 1941279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 195e602ba25SJulian Elischer PROC_UNLOCK(p); 196e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 197e602ba25SJulian Elischer } 19849539972SJulian Elischer /* 19949539972SJulian Elischer * If we get here all other threads are dead, 20049539972SJulian Elischer * so unset the associated flags and lose KSE mode. 20149539972SJulian Elischer */ 202ac2e4153SJulian Elischer p->p_flag &= ~P_THREADED; 20345f603e2SDavid Xu td->td_mailbox = NULL; 20449539972SJulian Elischer thread_single_end(); 20549539972SJulian Elischer } 2069ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2079ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2089ca45e81SDag-Erling Smørgrav 209df8bae1dSRodney W. Grimes /* 210c52007c2SDavid Greenman * Initialize part of the common data 211df8bae1dSRodney W. Grimes */ 212c52007c2SDavid Greenman imgp->proc = p; 213450ffb44SRobert Watson imgp->userspace_argv = argv; 214450ffb44SRobert Watson imgp->userspace_envv = envv; 215670cb89bSRobert Watson imgp->execlabel = NULL; 216c52007c2SDavid Greenman imgp->attr = &attr; 217c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2185cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 219c52007c2SDavid Greenman imgp->entry_addr = 0; 220c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 221c52007c2SDavid Greenman imgp->interpreted = 0; 222c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 223e1743d02SSøren Schmidt imgp->auxargs = NULL; 2241616db3cSJohn Dyson imgp->vp = NULL; 2250b2ed1aeSJeff Roberson imgp->object = NULL; 2261616db3cSJohn Dyson imgp->firstpage = NULL; 2274fe88fe6SJohn Polstra imgp->ps_strings = 0; 228b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 22926f9a767SRodney W. Grimes 230670cb89bSRobert Watson #ifdef MAC 231670cb89bSRobert Watson error = mac_execve_enter(imgp, mac_p, &execlabel); 232670cb89bSRobert Watson if (error) { 233670cb89bSRobert Watson mtx_lock(&Giant); 234670cb89bSRobert Watson goto exec_fail; 235670cb89bSRobert Watson } 236670cb89bSRobert Watson #endif 237670cb89bSRobert Watson 23826f9a767SRodney W. Grimes /* 23926f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 24026f9a767SRodney W. Grimes * environment strings 24126f9a767SRodney W. Grimes */ 242ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 243ca0387efSJake Burkholder PAGE_SIZE); 244c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 24526f9a767SRodney W. Grimes error = ENOMEM; 246b3afd20dSAlan Cox mtx_lock(&Giant); 24726f9a767SRodney W. Grimes goto exec_fail; 24826f9a767SRodney W. Grimes } 249c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 250c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2511616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 25226f9a767SRodney W. Grimes 25326f9a767SRodney W. Grimes /* 25426f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 25526f9a767SRodney W. Grimes * in ni_vp amoung other things. 25626f9a767SRodney W. Grimes */ 25726f9a767SRodney W. Grimes ndp = &nd; 258b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 259450ffb44SRobert Watson UIO_USERSPACE, fname, td); 26026f9a767SRodney W. Grimes 261b3afd20dSAlan Cox mtx_lock(&Giant); 26226f9a767SRodney W. Grimes interpret: 26326f9a767SRodney W. Grimes 26426f9a767SRodney W. Grimes error = namei(ndp); 26526f9a767SRodney W. Grimes if (error) { 2661616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2671616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 26826f9a767SRodney W. Grimes goto exec_fail; 26926f9a767SRodney W. Grimes } 27026f9a767SRodney W. Grimes 271c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 272450ffb44SRobert Watson imgp->fname = fname; 27326f9a767SRodney W. Grimes 27426f9a767SRodney W. Grimes /* 2759022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2769022e4adSDavid Greenman */ 277c52007c2SDavid Greenman error = exec_check_permissions(imgp); 278619eb6e5SJeff Roberson if (error) 27926f9a767SRodney W. Grimes goto exec_fail_dealloc; 280619eb6e5SJeff Roberson 281619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2820b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 28326f9a767SRodney W. Grimes 284619eb6e5SJeff Roberson /* 285619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 286619eb6e5SJeff Roberson * activating it. 287619eb6e5SJeff Roberson * 288619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 289619eb6e5SJeff Roberson * actually an executable image. 290619eb6e5SJeff Roberson */ 291619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 292619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 293619eb6e5SJeff Roberson 2941616db3cSJohn Dyson error = exec_map_first_page(imgp); 2956d5a0a8cSDavid Greenman if (error) 29626f9a767SRodney W. Grimes goto exec_fail_dealloc; 29726f9a767SRodney W. Grimes 29826f9a767SRodney W. Grimes /* 299d323ddf3SMatthew Dillon * If the current process has a special image activator it 300d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 301d323ddf3SMatthew Dillon * scripts differently. 30226f9a767SRodney W. Grimes */ 303d323ddf3SMatthew Dillon error = -1; 304d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 305d323ddf3SMatthew Dillon error = img_first(imgp); 306d323ddf3SMatthew Dillon 307d323ddf3SMatthew Dillon /* 308d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 309d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 310d323ddf3SMatthew Dillon * and an error otherwise. 311d323ddf3SMatthew Dillon */ 312d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 313d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 314d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 315d323ddf3SMatthew Dillon continue; 316d323ddf3SMatthew Dillon } 317c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 318d323ddf3SMatthew Dillon } 319d323ddf3SMatthew Dillon 320d323ddf3SMatthew Dillon if (error) { 321619eb6e5SJeff Roberson if (error == -1) { 322619eb6e5SJeff Roberson if (textset == 0) 323619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 324d323ddf3SMatthew Dillon error = ENOEXEC; 325619eb6e5SJeff Roberson } 32626f9a767SRodney W. Grimes goto exec_fail_dealloc; 327d323ddf3SMatthew Dillon } 328d323ddf3SMatthew Dillon 329d323ddf3SMatthew Dillon /* 330d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 331d323ddf3SMatthew Dillon * activate the interpreter. 332d323ddf3SMatthew Dillon */ 333c52007c2SDavid Greenman if (imgp->interpreted) { 3341616db3cSJohn Dyson exec_unmap_first_page(imgp); 335619eb6e5SJeff Roberson /* 336619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 337619eb6e5SJeff Roberson * period before we determine that something is a script where 338619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 339619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 340619eb6e5SJeff Roberson */ 341619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 342762e6b85SEivind Eklund /* free name buffer and old vnode */ 343762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 344670cb89bSRobert Watson #ifdef MAC 345670cb89bSRobert Watson mac_init_vnode_label(&interplabel); 346670cb89bSRobert Watson mac_copy_vnode_label(&ndp->ni_vp->v_label, &interplabel); 347670cb89bSRobert Watson interplabelvalid = 1; 348670cb89bSRobert Watson #endif 349619eb6e5SJeff Roberson vput(ndp->ni_vp); 3500b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3510b2ed1aeSJeff Roberson imgp->object = NULL; 35226f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 353b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 354b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 35526f9a767SRodney W. Grimes goto interpret; 35626f9a767SRodney W. Grimes } 35726f9a767SRodney W. Grimes 35826f9a767SRodney W. Grimes /* 35926f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 36026f9a767SRodney W. Grimes */ 3613ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3623ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3633ebc1248SPeter Wemm else 364c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 36526f9a767SRodney W. Grimes 36626f9a767SRodney W. Grimes /* 3671e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3681e1e0b44SSøren Schmidt * let it do the stack setup. 3691e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 37026f9a767SRodney W. Grimes */ 3711e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 372c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3731e1e0b44SSøren Schmidt else 374c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 37526f9a767SRodney W. Grimes 376a78e8d2aSDavid Greenman /* 377a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 378a78e8d2aSDavid Greenman * be shared after an exec. 379a78e8d2aSDavid Greenman */ 380426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 381a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 382a78e8d2aSDavid Greenman struct filedesc *tmp; 383a78e8d2aSDavid Greenman 384c522c1bfSAlfred Perlstein tmp = fdcopy(td->td_proc->p_fd); 385426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 386b40ce416SJulian Elischer fdfree(td); 387a78e8d2aSDavid Greenman p->p_fd = tmp; 388426da3bcSAlfred Perlstein } else 389426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 390a78e8d2aSDavid Greenman 391333ea485SGuido van Rooij /* 3929b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3939b3b1c5fSJohn Baldwin */ 3949b3b1c5fSJohn Baldwin newcred = crget(); 3951419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3969b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3979b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3989b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3999b3b1c5fSJohn Baldwin 4009b3b1c5fSJohn Baldwin /* close files on exec */ 4019b3b1c5fSJohn Baldwin fdcloseexec(td); 4029b3b1c5fSJohn Baldwin 403619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 404619eb6e5SJeff Roberson VREF(ndp->ni_vp); 405619eb6e5SJeff Roberson 4069b3b1c5fSJohn Baldwin /* 407333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4088688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 409b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 410333ea485SGuido van Rooij * reset. 411333ea485SGuido van Rooij */ 4129b3b1c5fSJohn Baldwin PROC_LOCK(p); 41390af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 41490af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 4159b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 41690af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 41790af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 4189b3b1c5fSJohn Baldwin PROC_LOCK(p); 41990af4afaSJohn Baldwin p->p_sigacts = newsigacts; 42090af4afaSJohn Baldwin } else 42190af4afaSJohn Baldwin oldsigacts = NULL; 422333ea485SGuido van Rooij 423fdf4e8b3SWarner Losh /* Stop profiling */ 424fdf4e8b3SWarner Losh stopprofclock(p); 425fdf4e8b3SWarner Losh 42626f9a767SRodney W. Grimes /* reset caught signals */ 42726f9a767SRodney W. Grimes execsigs(p); 42826f9a767SRodney W. Grimes 42926f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 43026f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 43126f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 43226f9a767SRodney W. Grimes p->p_comm[len] = 0; 43326f9a767SRodney W. Grimes 43426f9a767SRodney W. Grimes /* 43524b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 436dc733423SDag-Erling Smørgrav * it that it now has its own resources back 43726f9a767SRodney W. Grimes */ 43826f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 43926f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 44026f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4417f05b035SAlfred Perlstein wakeup(p->p_pptr); 44226f9a767SRodney W. Grimes } 44326f9a767SRodney W. Grimes 44426f9a767SRodney W. Grimes /* 445a78e8d2aSDavid Greenman * Implement image setuid/setgid. 446a78e8d2aSDavid Greenman * 447a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 448a78e8d2aSDavid Greenman * the process is being traced. 449670cb89bSRobert Watson * 450670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 451670cb89bSRobert Watson * transitions on the file system. 452c52007c2SDavid Greenman */ 453b1fc0ec1SRobert Watson oldcred = p->p_ucred; 454d06c0d4dSRobert Watson credential_changing = 0; 455d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 456d06c0d4dSRobert Watson attr.va_uid; 457d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 458d06c0d4dSRobert Watson attr.va_gid; 459ccafe7ebSRobert Watson #ifdef MAC 460670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 461670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 462ccafe7ebSRobert Watson credential_changing |= will_transition; 463ccafe7ebSRobert Watson #endif 464d06c0d4dSRobert Watson 465d06c0d4dSRobert Watson if (credential_changing && 466a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 467c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 468c52007c2SDavid Greenman /* 469c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 470b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 471b85db196SPeter Wemm * we do not regain any tracing during a possible block. 47226f9a767SRodney W. Grimes */ 473b85db196SPeter Wemm setsugid(p); 4746c84de02SJohn Baldwin #ifdef KTRACE 475a5881ea5SJohn Baldwin if (p->p_tracevp != NULL && suser_cred(oldcred, PRISON_ROOT)) { 4766c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 47779deba82SMatthew Dillon p->p_traceflag = 0; 478a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 479a5881ea5SJohn Baldwin p->p_tracevp = NULL; 480a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 481a5881ea5SJohn Baldwin p->p_tracecred = NULL; 4826c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 48326f9a767SRodney W. Grimes } 4846c84de02SJohn Baldwin #endif 48528b325aaSDon Lewis /* 486c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 487c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 48828b325aaSDon Lewis * 489c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 490c1e2d386SNate Lawson * which may grab the process lock. 49128b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 49228b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 49328b325aaSDon Lewis */ 49428b325aaSDon Lewis PROC_UNLOCK(p); 495c1e2d386SNate Lawson setugidsafety(td); 496e983a376SJacques Vidrine error = fdcheckstd(td); 49763c9e754SJohn Baldwin if (error != 0) 49869be5db9SAlfred Perlstein goto done1; 499e1b1aa3bSJohn Baldwin PROC_LOCK(p); 500c52007c2SDavid Greenman /* 501c52007c2SDavid Greenman * Set the new credentials. 502c52007c2SDavid Greenman */ 5039b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 504c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5051419eacbSAlfred Perlstein change_euid(newcred, euip); 506c52007c2SDavid Greenman if (attr.va_mode & VSGID) 507b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 508ccafe7ebSRobert Watson #ifdef MAC 509670cb89bSRobert Watson if (will_transition) { 510670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 511670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 512670cb89bSRobert Watson } 513ccafe7ebSRobert Watson #endif 5149b3b1c5fSJohn Baldwin /* 5159b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 516ccafe7ebSRobert Watson * 517ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 518ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 519ccafe7ebSRobert Watson * though maybe it shouldn't. 5209b3b1c5fSJohn Baldwin */ 5219b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 5229b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 5239b3b1c5fSJohn Baldwin p->p_ucred = newcred; 5249b3b1c5fSJohn Baldwin newcred = NULL; 525c52007c2SDavid Greenman } else { 526b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 527b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 528c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 52926f9a767SRodney W. Grimes /* 530c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 531b1fc0ec1SRobert Watson * 532b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 5339b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 5349b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 5359b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 5369b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5379b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5389b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5399b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 54026f9a767SRodney W. Grimes */ 541b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 542b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5439b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 544b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 545b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 546b1fc0ec1SRobert Watson p->p_ucred = newcred; 5479b3b1c5fSJohn Baldwin newcred = NULL; 5489b3b1c5fSJohn Baldwin } 549b1fc0ec1SRobert Watson } 55026f9a767SRodney W. Grimes 55126f9a767SRodney W. Grimes /* 552619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 553619eb6e5SJeff Roberson * to locking the proc lock. 5542a531c80SDavid Greenman */ 5559b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5562a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5572a531c80SDavid Greenman 5582a531c80SDavid Greenman /* 5599ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5609ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 561cb679c38SJonathan Lemon */ 562cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5639ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 564cb679c38SJonathan Lemon 565cb679c38SJonathan Lemon /* 56626f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 56726f9a767SRodney W. Grimes * can be set before the program executes. 56826f9a767SRodney W. Grimes */ 56926f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 57026f9a767SRodney W. Grimes psignal(p, SIGTRAP); 57126f9a767SRodney W. Grimes 57226f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 57326f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 57426f9a767SRodney W. Grimes 575b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5769b3b1c5fSJohn Baldwin oldargs = p->p_args; 577b9df5231SPoul-Henning Kamp p->p_args = NULL; 578b9df5231SPoul-Henning Kamp 579e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 580e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 581e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 582e1b1aa3bSJohn Baldwin p->p_args = newargs; 583e1b1aa3bSJohn Baldwin newargs = NULL; 584e1b1aa3bSJohn Baldwin } 585e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 586e1b1aa3bSJohn Baldwin 587e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5883ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5893ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5903ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5913ebc1248SPeter Wemm else 592bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 593bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 594e913ca22SDoug Rabson 59569be5db9SAlfred Perlstein done1: 5969b3b1c5fSJohn Baldwin /* 5979b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5989b3b1c5fSJohn Baldwin */ 5991419eacbSAlfred Perlstein uifree(euip); 6009b3b1c5fSJohn Baldwin if (newcred == NULL) 6019b3b1c5fSJohn Baldwin crfree(oldcred); 6029b3b1c5fSJohn Baldwin else 6039b3b1c5fSJohn Baldwin crfree(newcred); 6049b3b1c5fSJohn Baldwin /* 6059b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 6069b3b1c5fSJohn Baldwin */ 6079b3b1c5fSJohn Baldwin if (textvp != NULL) 6089b3b1c5fSJohn Baldwin vrele(textvp); 609619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 610619eb6e5SJeff Roberson vrele(ndp->ni_vp); 6116c84de02SJohn Baldwin #ifdef KTRACE 6129b3b1c5fSJohn Baldwin if (tracevp != NULL) 6139b3b1c5fSJohn Baldwin vrele(tracevp); 614a5881ea5SJohn Baldwin if (tracecred != NULL) 615a5881ea5SJohn Baldwin crfree(tracecred); 6166c84de02SJohn Baldwin #endif 61769be5db9SAlfred Perlstein if (oldargs != NULL) 6189b3b1c5fSJohn Baldwin pargs_drop(oldargs); 61969be5db9SAlfred Perlstein if (newargs != NULL) 62069be5db9SAlfred Perlstein pargs_drop(newargs); 62190af4afaSJohn Baldwin if (oldsigacts != NULL) 62290af4afaSJohn Baldwin sigacts_free(oldsigacts); 623b9df5231SPoul-Henning Kamp 6241616db3cSJohn Dyson exec_fail_dealloc: 6251616db3cSJohn Dyson 62626f9a767SRodney W. Grimes /* 62726f9a767SRodney W. Grimes * free various allocated resources 62826f9a767SRodney W. Grimes */ 6291616db3cSJohn Dyson if (imgp->firstpage) 6301616db3cSJohn Dyson exec_unmap_first_page(imgp); 63126f9a767SRodney W. Grimes 6329c0fed3dSDoug Rabson if (imgp->vp) { 633762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 634619eb6e5SJeff Roberson vput(imgp->vp); 635a3cf6ebaSDavid Greenman } 63626f9a767SRodney W. Grimes 637a9a08882SJeff Roberson if (imgp->stringbase != NULL) 638a9a08882SJeff Roberson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 639a9a08882SJeff Roberson ARG_MAX + PAGE_SIZE); 640a9a08882SJeff Roberson 6410b2ed1aeSJeff Roberson if (imgp->object) 6420b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6430b2ed1aeSJeff Roberson 644d1989db5SRobert Drehmel if (error == 0) { 645d1989db5SRobert Drehmel /* 646d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 647d1989db5SRobert Drehmel * the S_EXEC bit set. 648d1989db5SRobert Drehmel */ 649d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 650116734c4SMatthew Dillon goto done2; 651d1989db5SRobert Drehmel } 6521616db3cSJohn Dyson 65326f9a767SRodney W. Grimes exec_fail: 6549ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6559ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6569ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6579ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6589ca45e81SDag-Erling Smørgrav 659c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 66026f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 661670cb89bSRobert Watson #ifdef MAC 662670cb89bSRobert Watson mac_execve_exit(imgp); 663670cb89bSRobert Watson if (interplabelvalid) 664670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 665670cb89bSRobert Watson #endif 666b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 66726f9a767SRodney W. Grimes /* NOT REACHED */ 668116734c4SMatthew Dillon error = 0; 66926f9a767SRodney W. Grimes } 670116734c4SMatthew Dillon done2: 671670cb89bSRobert Watson #ifdef MAC 672670cb89bSRobert Watson mac_execve_exit(imgp); 673670cb89bSRobert Watson if (interplabelvalid) 674670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 675670cb89bSRobert Watson #endif 676116734c4SMatthew Dillon mtx_unlock(&Giant); 677116734c4SMatthew Dillon return (error); 67826f9a767SRodney W. Grimes } 67926f9a767SRodney W. Grimes 680450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 681450ffb44SRobert Watson struct execve_args { 682450ffb44SRobert Watson char *fname; 683450ffb44SRobert Watson char **argv; 684450ffb44SRobert Watson char **envv; 685450ffb44SRobert Watson }; 686450ffb44SRobert Watson #endif 687450ffb44SRobert Watson 688450ffb44SRobert Watson /* 689450ffb44SRobert Watson * MPSAFE 690450ffb44SRobert Watson */ 691450ffb44SRobert Watson int 692450ffb44SRobert Watson execve(td, uap) 693450ffb44SRobert Watson struct thread *td; 694450ffb44SRobert Watson struct execve_args /* { 695b80521feSAlfred Perlstein char *fname; 696b80521feSAlfred Perlstein char **argv; 697b80521feSAlfred Perlstein char **envv; 698450ffb44SRobert Watson } */ *uap; 699450ffb44SRobert Watson { 700450ffb44SRobert Watson 701670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 702670cb89bSRobert Watson } 703670cb89bSRobert Watson 704670cb89bSRobert Watson #ifndef _SYS_SYSPROTO_H_ 705670cb89bSRobert Watson struct __mac_execve_args { 706670cb89bSRobert Watson char *fname; 707670cb89bSRobert Watson char **argv; 708670cb89bSRobert Watson char **envv; 709670cb89bSRobert Watson struct mac *mac_p; 710670cb89bSRobert Watson }; 711670cb89bSRobert Watson #endif 712670cb89bSRobert Watson 713670cb89bSRobert Watson /* 714670cb89bSRobert Watson * MPSAFE 715670cb89bSRobert Watson */ 716670cb89bSRobert Watson int 717670cb89bSRobert Watson __mac_execve(td, uap) 718670cb89bSRobert Watson struct thread *td; 719670cb89bSRobert Watson struct __mac_execve_args /* { 720b80521feSAlfred Perlstein char *fname; 721b80521feSAlfred Perlstein char **argv; 722b80521feSAlfred Perlstein char **envv; 723b80521feSAlfred Perlstein struct mac *mac_p; 724670cb89bSRobert Watson } */ *uap; 725670cb89bSRobert Watson { 726670cb89bSRobert Watson 7270c93266bSRobert Watson #ifdef MAC 728670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, 729670cb89bSRobert Watson uap->mac_p)); 7300c93266bSRobert Watson #else 7310c93266bSRobert Watson return (ENOSYS); 7320c93266bSRobert Watson #endif 733450ffb44SRobert Watson } 734450ffb44SRobert Watson 7351616db3cSJohn Dyson int 7361616db3cSJohn Dyson exec_map_first_page(imgp) 7371616db3cSJohn Dyson struct image_params *imgp; 7381616db3cSJohn Dyson { 739d8aad40cSJohn Baldwin int rv, i; 74095461b45SJohn Dyson int initial_pagein; 74195461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7421616db3cSJohn Dyson vm_object_t object; 7431616db3cSJohn Dyson 7440cddd8f0SMatthew Dillon GIANT_REQUIRED; 7451616db3cSJohn Dyson 7461616db3cSJohn Dyson if (imgp->firstpage) { 7471616db3cSJohn Dyson exec_unmap_first_page(imgp); 7481616db3cSJohn Dyson } 7491616db3cSJohn Dyson 7509ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 751fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 75295461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 75306fa71cdSAlan Cox vm_page_lock_queues(); 75495461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 75506fa71cdSAlan Cox vm_page_unlock_queues(); 75695461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 75795461b45SJohn Dyson if (initial_pagein > object->size) 75895461b45SJohn Dyson initial_pagein = object->size; 75995461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 760d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 761ee113343SAlan Cox vm_page_lock_queues(); 76206fa71cdSAlan Cox if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) { 76306fa71cdSAlan Cox vm_page_unlock_queues(); 76406fa71cdSAlan Cox break; 76506fa71cdSAlan Cox } 76606fa71cdSAlan Cox if (ma[i]->valid) { 76706fa71cdSAlan Cox vm_page_unlock_queues(); 76806fa71cdSAlan Cox break; 76906fa71cdSAlan Cox } 770e69763a3SDoug Rabson vm_page_busy(ma[i]); 771ee113343SAlan Cox vm_page_unlock_queues(); 77295461b45SJohn Dyson } else { 773ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 774ca0387efSJake Burkholder VM_ALLOC_NORMAL); 77595461b45SJohn Dyson if (ma[i] == NULL) 77695461b45SJohn Dyson break; 77795461b45SJohn Dyson } 77895461b45SJohn Dyson } 77995461b45SJohn Dyson initial_pagein = i; 78006fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); /* XXX */ 78195461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 78206fa71cdSAlan Cox VM_OBJECT_LOCK(object); /* XXX */ 78395461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 78406fa71cdSAlan Cox vm_page_lock_queues(); 785ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 786ca0387efSJake Burkholder (ma[0]->valid == 0)) { 787ecbb00a2SDoug Rabson if (ma[0]) { 7884fec79beSAlan Cox pmap_remove_all(ma[0]); 7898f9110f6SJohn Dyson vm_page_free(ma[0]); 790ecbb00a2SDoug Rabson } 79106fa71cdSAlan Cox vm_page_unlock_queues(); 79206fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 793a7cddfedSJake Burkholder return (EIO); 7941616db3cSJohn Dyson } 7951616db3cSJohn Dyson } 79606fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 79795461b45SJohn Dyson vm_page_wire(ma[0]); 798e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 79997646f56SAlan Cox vm_page_unlock_queues(); 8001616db3cSJohn Dyson 801ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 80295461b45SJohn Dyson imgp->firstpage = ma[0]; 8031616db3cSJohn Dyson 804a7cddfedSJake Burkholder return (0); 8051616db3cSJohn Dyson } 8061616db3cSJohn Dyson 8071616db3cSJohn Dyson void 8081616db3cSJohn Dyson exec_unmap_first_page(imgp) 8091616db3cSJohn Dyson struct image_params *imgp; 8101616db3cSJohn Dyson { 8110cddd8f0SMatthew Dillon GIANT_REQUIRED; 81223955314SAlfred Perlstein 8131616db3cSJohn Dyson if (imgp->firstpage) { 814ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 81597646f56SAlan Cox vm_page_lock_queues(); 81673007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 81797646f56SAlan Cox vm_page_unlock_queues(); 8181616db3cSJohn Dyson imgp->firstpage = NULL; 8191616db3cSJohn Dyson } 8201616db3cSJohn Dyson } 8211616db3cSJohn Dyson 82226f9a767SRodney W. Grimes /* 82326f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 82426f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 82526f9a767SRodney W. Grimes * automatically in trap.c. 82626f9a767SRodney W. Grimes */ 82726f9a767SRodney W. Grimes int 82805ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 829c52007c2SDavid Greenman struct image_params *imgp; 83005ba50f5SJake Burkholder struct sysentvec *sv; 83126f9a767SRodney W. Grimes { 83226f9a767SRodney W. Grimes int error; 8335e20c11fSAlan Cox struct proc *p = imgp->proc; 8345e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 83505ba50f5SJake Burkholder vm_offset_t stack_addr; 83605ba50f5SJake Burkholder vm_map_t map; 83726f9a767SRodney W. Grimes 8380cddd8f0SMatthew Dillon GIANT_REQUIRED; 8390cddd8f0SMatthew Dillon 84005ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 8413ebc1248SPeter Wemm 842c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 84326f9a767SRodney W. Grimes 84475b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exec, p); 8456f5dafeaSAlan Cox 8466f5dafeaSAlan Cox /* 847af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 848af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 849af9ec885SJohn Dyson * not disrupted 850af9ec885SJohn Dyson */ 85105ba50f5SJake Burkholder map = &vmspace->vm_map; 85205ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 85305ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 8543db161e0SMatthew Dillon shmexit(vmspace); 8552d21129dSAlan Cox vm_page_lock_queues(); 85605ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 85705ba50f5SJake Burkholder vm_map_max(map)); 8582d21129dSAlan Cox vm_page_unlock_queues(); 85905ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 860af9ec885SJohn Dyson } else { 86105ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 8625e20c11fSAlan Cox vmspace = p->p_vmspace; 86305ba50f5SJake Burkholder map = &vmspace->vm_map; 864af9ec885SJohn Dyson } 86526f9a767SRodney W. Grimes 86626f9a767SRodney W. Grimes /* Allocate a new stack */ 86705ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 86805ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 8692267af78SJulian Elischer if (error) 8702267af78SJulian Elischer return (error); 8712267af78SJulian Elischer 87263c47a5cSDoug Rabson #ifdef __ia64__ 87363c47a5cSDoug Rabson { 87463c47a5cSDoug Rabson /* 87563c47a5cSDoug Rabson * Allocate backing store. We really need something 87663c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 87763c47a5cSDoug Rabson * store to grow upwards. This will do for now. 87863c47a5cSDoug Rabson */ 87963c47a5cSDoug Rabson vm_offset_t bsaddr; 88005ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 88105ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 88281f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 8835e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 88463c47a5cSDoug Rabson } 88563c47a5cSDoug Rabson #endif 88663c47a5cSDoug Rabson 8872267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 8882267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 8892267af78SJulian Elischer * process stack so we can check the stack rlimit. 8902267af78SJulian Elischer */ 891cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 89205ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 89326f9a767SRodney W. Grimes 89426f9a767SRodney W. Grimes return (0); 89526f9a767SRodney W. Grimes } 89626f9a767SRodney W. Grimes 89726f9a767SRodney W. Grimes /* 89826f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 89926f9a767SRodney W. Grimes * address space into the temporary string buffer. 90026f9a767SRodney W. Grimes */ 90126f9a767SRodney W. Grimes int 902c52007c2SDavid Greenman exec_extract_strings(imgp) 903c52007c2SDavid Greenman struct image_params *imgp; 90426f9a767SRodney W. Grimes { 90526f9a767SRodney W. Grimes char **argv, **envv; 90626f9a767SRodney W. Grimes char *argp, *envp; 907ecbb00a2SDoug Rabson int error; 908ecbb00a2SDoug Rabson size_t length; 90926f9a767SRodney W. Grimes 91026f9a767SRodney W. Grimes /* 91126f9a767SRodney W. Grimes * extract arguments first 91226f9a767SRodney W. Grimes */ 91326f9a767SRodney W. Grimes 914450ffb44SRobert Watson argv = imgp->userspace_argv; 91526f9a767SRodney W. Grimes 9160fd1a014SDavid Greenman if (argv) { 917aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 9185cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 9195cf3d12cSAndrey A. Chernov return (EFAULT); 9205cf3d12cSAndrey A. Chernov if (argp) 9215cf3d12cSAndrey A. Chernov argv++; 9225cf3d12cSAndrey A. Chernov if (imgp->argv0) 9235cf3d12cSAndrey A. Chernov argp = imgp->argv0; 9245cf3d12cSAndrey A. Chernov if (argp) { 9255cf3d12cSAndrey A. Chernov do { 92626f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 92726f9a767SRodney W. Grimes return (EFAULT); 928c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 929c52007c2SDavid Greenman imgp->stringspace, &length))) { 9300fd1a014SDavid Greenman if (error == ENAMETOOLONG) 93126f9a767SRodney W. Grimes return (E2BIG); 9320fd1a014SDavid Greenman return (error); 9330fd1a014SDavid Greenman } 934c52007c2SDavid Greenman imgp->stringspace -= length; 935c52007c2SDavid Greenman imgp->stringp += length; 936c52007c2SDavid Greenman imgp->argc++; 937aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 93826f9a767SRodney W. Grimes } 9390fd1a014SDavid Greenman } 94026f9a767SRodney W. Grimes 941b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 942b9df5231SPoul-Henning Kamp 94326f9a767SRodney W. Grimes /* 94426f9a767SRodney W. Grimes * extract environment strings 94526f9a767SRodney W. Grimes */ 94626f9a767SRodney W. Grimes 947450ffb44SRobert Watson envv = imgp->userspace_envv; 94826f9a767SRodney W. Grimes 9490fd1a014SDavid Greenman if (envv) { 950aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 95126f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 95226f9a767SRodney W. Grimes return (EFAULT); 953c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 954c52007c2SDavid Greenman imgp->stringspace, &length))) { 9550fd1a014SDavid Greenman if (error == ENAMETOOLONG) 95626f9a767SRodney W. Grimes return (E2BIG); 9570fd1a014SDavid Greenman return (error); 9580fd1a014SDavid Greenman } 959c52007c2SDavid Greenman imgp->stringspace -= length; 960c52007c2SDavid Greenman imgp->stringp += length; 961c52007c2SDavid Greenman imgp->envc++; 96226f9a767SRodney W. Grimes } 9630fd1a014SDavid Greenman } 96426f9a767SRodney W. Grimes 96526f9a767SRodney W. Grimes return (0); 96626f9a767SRodney W. Grimes } 96726f9a767SRodney W. Grimes 96826f9a767SRodney W. Grimes /* 96926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 97026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 97126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 97226f9a767SRodney W. Grimes */ 973654f6be1SBruce Evans register_t * 974c52007c2SDavid Greenman exec_copyout_strings(imgp) 975c52007c2SDavid Greenman struct image_params *imgp; 97626f9a767SRodney W. Grimes { 97726f9a767SRodney W. Grimes int argc, envc; 97826f9a767SRodney W. Grimes char **vectp; 97926f9a767SRodney W. Grimes char *stringp, *destp; 980654f6be1SBruce Evans register_t *stack_base; 98193f6448cSDavid Greenman struct ps_strings *arginfo; 982f3bec5d7SJake Burkholder struct proc *p; 983d66a5066SPeter Wemm int szsigcode; 98426f9a767SRodney W. Grimes 98526f9a767SRodney W. Grimes /* 98626f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 987d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 98826f9a767SRodney W. Grimes */ 989f3bec5d7SJake Burkholder p = imgp->proc; 990f3bec5d7SJake Burkholder szsigcode = 0; 99105ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 992f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 993f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 994d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 9951ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 9961ed012f9SPeter Wemm 99726f9a767SRodney W. Grimes /* 998d66a5066SPeter Wemm * install sigcode 999d66a5066SPeter Wemm */ 1000d66a5066SPeter Wemm if (szsigcode) 1001f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1002f3bec5d7SJake Burkholder szsigcode), szsigcode); 1003d66a5066SPeter Wemm 1004d66a5066SPeter Wemm /* 1005e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1006e1743d02SSøren Schmidt * on the stack. 1007e1743d02SSøren Schmidt */ 1008b9a22da4STakanori Watanabe if (imgp->auxargs) { 1009e1743d02SSøren Schmidt /* 1010b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1011b9a22da4STakanori Watanabe * lower compatibility. 1012b9a22da4STakanori Watanabe */ 1013ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1014ca0387efSJake Burkholder (AT_COUNT * 2); 1015b9a22da4STakanori Watanabe /* 1016b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1017b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1018b9a22da4STakanori Watanabe * for argument of Runtime loader. 1019e1743d02SSøren Schmidt */ 1020e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1021b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 1022b9a22da4STakanori Watanabe 1023b9a22da4STakanori Watanabe } else 1024e1743d02SSøren Schmidt /* 1025b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1026b9a22da4STakanori Watanabe * the arg and env vector sets 102726f9a767SRodney W. Grimes */ 1028ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1029ca0387efSJake Burkholder sizeof(char *)); 103026f9a767SRodney W. Grimes 103126f9a767SRodney W. Grimes /* 103226f9a767SRodney W. Grimes * vectp also becomes our initial stack base 103326f9a767SRodney W. Grimes */ 1034654f6be1SBruce Evans stack_base = (register_t *)vectp; 103526f9a767SRodney W. Grimes 1036c52007c2SDavid Greenman stringp = imgp->stringbase; 1037c52007c2SDavid Greenman argc = imgp->argc; 1038c52007c2SDavid Greenman envc = imgp->envc; 103926f9a767SRodney W. Grimes 104093f6448cSDavid Greenman /* 10413aed948bSDavid Greenman * Copy out strings - arguments and environment. 104293f6448cSDavid Greenman */ 1043c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 104493f6448cSDavid Greenman 104593f6448cSDavid Greenman /* 10463aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 10473aed948bSDavid Greenman */ 1048aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 10493aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 10503aed948bSDavid Greenman 10513aed948bSDavid Greenman /* 10523aed948bSDavid Greenman * Fill in argument portion of vector table. 105393f6448cSDavid Greenman */ 105426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1055aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10563aed948bSDavid Greenman while (*stringp++ != 0) 10573aed948bSDavid Greenman destp++; 10583aed948bSDavid Greenman destp++; 105926f9a767SRodney W. Grimes } 106026f9a767SRodney W. Grimes 10611a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 10626ab46d52SBruce Evans suword(vectp++, 0); 106326f9a767SRodney W. Grimes 1064aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 10653aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 106693f6448cSDavid Greenman 106793f6448cSDavid Greenman /* 10683aed948bSDavid Greenman * Fill in environment portion of vector table. 106993f6448cSDavid Greenman */ 107026f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1071aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10723aed948bSDavid Greenman while (*stringp++ != 0) 10733aed948bSDavid Greenman destp++; 10743aed948bSDavid Greenman destp++; 107526f9a767SRodney W. Grimes } 107626f9a767SRodney W. Grimes 107726f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 10786ab46d52SBruce Evans suword(vectp, 0); 107926f9a767SRodney W. Grimes 108026f9a767SRodney W. Grimes return (stack_base); 108126f9a767SRodney W. Grimes } 108226f9a767SRodney W. Grimes 108326f9a767SRodney W. Grimes /* 108426f9a767SRodney W. Grimes * Check permissions of file to execute. 1085cf64863aSRobert Watson * Called with imgp->vp locked. 108626f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 108726f9a767SRodney W. Grimes */ 1088c8a79999SPeter Wemm int 1089c52007c2SDavid Greenman exec_check_permissions(imgp) 1090c52007c2SDavid Greenman struct image_params *imgp; 109126f9a767SRodney W. Grimes { 1092c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1093c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1094a854ed98SJohn Baldwin struct thread *td; 109526f9a767SRodney W. Grimes int error; 109626f9a767SRodney W. Grimes 1097a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1098339b79b9SRobert Watson 1099ec35c2afSRobert Watson /* Get file attributes */ 1100ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1101ec35c2afSRobert Watson if (error) 1102ec35c2afSRobert Watson return (error); 1103ec35c2afSRobert Watson 1104339b79b9SRobert Watson #ifdef MAC 1105670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1106339b79b9SRobert Watson if (error) 1107339b79b9SRobert Watson return (error); 1108339b79b9SRobert Watson #endif 1109339b79b9SRobert Watson 111026f9a767SRodney W. Grimes /* 111126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 111226f9a767SRodney W. Grimes * file resides on. 111326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 111426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 111526f9a767SRodney W. Grimes * file really is executable. 111626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 111726f9a767SRodney W. Grimes */ 1118c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 111926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1120a854ed98SJohn Baldwin (attr->va_type != VREG)) 112126f9a767SRodney W. Grimes return (EACCES); 112226f9a767SRodney W. Grimes 112326f9a767SRodney W. Grimes /* 112426f9a767SRodney W. Grimes * Zero length files can't be exec'd 112526f9a767SRodney W. Grimes */ 112626f9a767SRodney W. Grimes if (attr->va_size == 0) 112726f9a767SRodney W. Grimes return (ENOEXEC); 112826f9a767SRodney W. Grimes 112926f9a767SRodney W. Grimes /* 113026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 113126f9a767SRodney W. Grimes */ 1132a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 113326f9a767SRodney W. Grimes if (error) 113426f9a767SRodney W. Grimes return (error); 113526f9a767SRodney W. Grimes 11366d5a0a8cSDavid Greenman /* 11376d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 11386d5a0a8cSDavid Greenman * if there are any. 11396d5a0a8cSDavid Greenman */ 11406d5a0a8cSDavid Greenman if (vp->v_writecount) 11416d5a0a8cSDavid Greenman return (ETXTBSY); 11426d5a0a8cSDavid Greenman 11436d5a0a8cSDavid Greenman /* 11446d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 11456d5a0a8cSDavid Greenman * general case). 11466d5a0a8cSDavid Greenman */ 1147a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 114826f9a767SRodney W. Grimes return (error); 1149df8bae1dSRodney W. Grimes } 1150aa855a59SPeter Wemm 1151aa855a59SPeter Wemm /* 1152aa855a59SPeter Wemm * Exec handler registration 1153aa855a59SPeter Wemm */ 1154aa855a59SPeter Wemm int 1155aa855a59SPeter Wemm exec_register(execsw_arg) 1156aa855a59SPeter Wemm const struct execsw *execsw_arg; 1157aa855a59SPeter Wemm { 1158aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1159aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1160aa855a59SPeter Wemm 1161aa855a59SPeter Wemm if (execsw) 1162aa855a59SPeter Wemm for (es = execsw; *es; es++) 1163aa855a59SPeter Wemm count++; 1164a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1165aa855a59SPeter Wemm if (newexecsw == NULL) 1166a7cddfedSJake Burkholder return (ENOMEM); 1167aa855a59SPeter Wemm xs = newexecsw; 1168aa855a59SPeter Wemm if (execsw) 1169aa855a59SPeter Wemm for (es = execsw; *es; es++) 1170aa855a59SPeter Wemm *xs++ = *es; 1171aa855a59SPeter Wemm *xs++ = execsw_arg; 1172aa855a59SPeter Wemm *xs = NULL; 1173aa855a59SPeter Wemm if (execsw) 1174aa855a59SPeter Wemm free(execsw, M_TEMP); 1175aa855a59SPeter Wemm execsw = newexecsw; 1176a7cddfedSJake Burkholder return (0); 1177aa855a59SPeter Wemm } 1178aa855a59SPeter Wemm 1179aa855a59SPeter Wemm int 1180aa855a59SPeter Wemm exec_unregister(execsw_arg) 1181aa855a59SPeter Wemm const struct execsw *execsw_arg; 1182aa855a59SPeter Wemm { 1183aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1184aa855a59SPeter Wemm int count = 1; 1185aa855a59SPeter Wemm 1186aa855a59SPeter Wemm if (execsw == NULL) 1187aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1188aa855a59SPeter Wemm 1189aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1190aa855a59SPeter Wemm if (*es == execsw_arg) 1191aa855a59SPeter Wemm break; 1192aa855a59SPeter Wemm } 1193aa855a59SPeter Wemm if (*es == NULL) 1194a7cddfedSJake Burkholder return (ENOENT); 1195aa855a59SPeter Wemm for (es = execsw; *es; es++) 1196aa855a59SPeter Wemm if (*es != execsw_arg) 1197aa855a59SPeter Wemm count++; 1198a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1199aa855a59SPeter Wemm if (newexecsw == NULL) 1200a7cddfedSJake Burkholder return (ENOMEM); 1201aa855a59SPeter Wemm xs = newexecsw; 1202aa855a59SPeter Wemm for (es = execsw; *es; es++) 1203aa855a59SPeter Wemm if (*es != execsw_arg) 1204aa855a59SPeter Wemm *xs++ = *es; 1205aa855a59SPeter Wemm *xs = NULL; 1206aa855a59SPeter Wemm if (execsw) 1207aa855a59SPeter Wemm free(execsw, M_TEMP); 1208aa855a59SPeter Wemm execsw = newexecsw; 1209a7cddfedSJake Burkholder return (0); 1210aa855a59SPeter Wemm } 1211