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 10105ba50f5SJake Burkholder static int 10205ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 10305ba50f5SJake Burkholder { 10405ba50f5SJake Burkholder struct proc *p; 10505ba50f5SJake Burkholder 10605ba50f5SJake Burkholder p = curproc; 10705ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 10805ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 10905ba50f5SJake Burkholder } 11005ba50f5SJake Burkholder 11105ba50f5SJake Burkholder static int 11205ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 11305ba50f5SJake Burkholder { 11405ba50f5SJake Burkholder struct proc *p; 11505ba50f5SJake Burkholder 11605ba50f5SJake Burkholder p = curproc; 11705ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 11805ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 11905ba50f5SJake Burkholder } 12005ba50f5SJake Burkholder 1215dadd17bSJake Burkholder static int 1225dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1235dadd17bSJake Burkholder { 1245dadd17bSJake Burkholder struct proc *p; 1255dadd17bSJake Burkholder 1265dadd17bSJake Burkholder p = curproc; 1275dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1285dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1295dadd17bSJake Burkholder } 1305dadd17bSJake Burkholder 13199ac3bc8SPeter Wemm /* 132aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 133aa855a59SPeter Wemm * double pointer here. 134df8bae1dSRodney W. Grimes */ 135aa855a59SPeter Wemm static const struct execsw **execsw; 13626f9a767SRodney W. Grimes 13726f9a767SRodney W. Grimes /* 138450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 139450ffb44SRobert Watson * userspace pointers from the passed thread. 140116734c4SMatthew Dillon * 141116734c4SMatthew Dillon * MPSAFE 14226f9a767SRodney W. Grimes */ 143450ffb44SRobert Watson static int 144670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p) 145b40ce416SJulian Elischer struct thread *td; 146450ffb44SRobert Watson char *fname; 147450ffb44SRobert Watson char **argv; 148450ffb44SRobert Watson char **envv; 149670cb89bSRobert Watson struct mac *mac_p; 150df8bae1dSRodney W. Grimes { 151b40ce416SJulian Elischer struct proc *p = td->td_proc; 15226f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1539b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1541419eacbSAlfred Perlstein struct uidinfo *euip; 155654f6be1SBruce Evans register_t *stack_base; 156bb56ec4aSPoul-Henning Kamp int error, len, i; 157c52007c2SDavid Greenman struct image_params image_params, *imgp; 15826f9a767SRodney W. Grimes struct vattr attr; 1594d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 16069be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 16190af4afaSJohn Baldwin struct sigacts *oldsigacts, *newsigacts; 1626c84de02SJohn Baldwin #ifdef KTRACE 1636c84de02SJohn Baldwin struct vnode *tracevp = NULL; 164a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 1656c84de02SJohn Baldwin #endif 1666c84de02SJohn Baldwin struct vnode *textvp = NULL; 167d06c0d4dSRobert Watson int credential_changing; 168619eb6e5SJeff Roberson int textset; 169ccafe7ebSRobert Watson #ifdef MAC 170eca8a663SRobert Watson struct label *interplabel = NULL; 171eca8a663SRobert Watson int will_transition; 172ccafe7ebSRobert Watson #endif 17326f9a767SRodney W. Grimes 174c52007c2SDavid Greenman imgp = &image_params; 175df8bae1dSRodney W. Grimes 1769ca45e81SDag-Erling Smørgrav /* 1779ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1789ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1799ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1809ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1819ca45e81SDag-Erling Smørgrav * privileges. 1829ca45e81SDag-Erling Smørgrav */ 1839ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1849ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 18591f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 1860e2a4d3aSDavid Xu if (p->p_flag & P_SA || p->p_numthreads > 1) { 1871279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 188e602ba25SJulian Elischer PROC_UNLOCK(p); 189e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 190e602ba25SJulian Elischer } 19149539972SJulian Elischer /* 19249539972SJulian Elischer * If we get here all other threads are dead, 19349539972SJulian Elischer * so unset the associated flags and lose KSE mode. 19449539972SJulian Elischer */ 1950e2a4d3aSDavid Xu p->p_flag &= ~P_SA; 19645f603e2SDavid Xu td->td_mailbox = NULL; 19749539972SJulian Elischer thread_single_end(); 19849539972SJulian Elischer } 1999ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2009ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2019ca45e81SDag-Erling Smørgrav 202df8bae1dSRodney W. Grimes /* 203c52007c2SDavid Greenman * Initialize part of the common data 204df8bae1dSRodney W. Grimes */ 205c52007c2SDavid Greenman imgp->proc = p; 206450ffb44SRobert Watson imgp->userspace_argv = argv; 207450ffb44SRobert Watson imgp->userspace_envv = envv; 208670cb89bSRobert Watson imgp->execlabel = NULL; 209c52007c2SDavid Greenman imgp->attr = &attr; 210c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2115cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 212c52007c2SDavid Greenman imgp->entry_addr = 0; 213c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 214c52007c2SDavid Greenman imgp->interpreted = 0; 215c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 216e1743d02SSøren Schmidt imgp->auxargs = NULL; 2171616db3cSJohn Dyson imgp->vp = NULL; 2180b2ed1aeSJeff Roberson imgp->object = NULL; 2191616db3cSJohn Dyson imgp->firstpage = NULL; 2204fe88fe6SJohn Polstra imgp->ps_strings = 0; 221b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 22226f9a767SRodney W. Grimes 223670cb89bSRobert Watson #ifdef MAC 224eca8a663SRobert Watson error = mac_execve_enter(imgp, mac_p); 225670cb89bSRobert Watson if (error) { 226670cb89bSRobert Watson mtx_lock(&Giant); 227670cb89bSRobert Watson goto exec_fail; 228670cb89bSRobert Watson } 229670cb89bSRobert Watson #endif 230670cb89bSRobert Watson 23126f9a767SRodney W. Grimes /* 23226f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 23326f9a767SRodney W. Grimes * environment strings 23426f9a767SRodney W. Grimes */ 235ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 236ca0387efSJake Burkholder PAGE_SIZE); 237c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 23826f9a767SRodney W. Grimes error = ENOMEM; 239b3afd20dSAlan Cox mtx_lock(&Giant); 24026f9a767SRodney W. Grimes goto exec_fail; 24126f9a767SRodney W. Grimes } 242c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 243c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2441616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 24526f9a767SRodney W. Grimes 24626f9a767SRodney W. Grimes /* 24726f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 24826f9a767SRodney W. Grimes * in ni_vp amoung other things. 24926f9a767SRodney W. Grimes */ 25026f9a767SRodney W. Grimes ndp = &nd; 251b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 252450ffb44SRobert Watson UIO_USERSPACE, fname, td); 25326f9a767SRodney W. Grimes 254b3afd20dSAlan Cox mtx_lock(&Giant); 25526f9a767SRodney W. Grimes interpret: 25626f9a767SRodney W. Grimes 25726f9a767SRodney W. Grimes error = namei(ndp); 25826f9a767SRodney W. Grimes if (error) { 2591616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2601616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 26126f9a767SRodney W. Grimes goto exec_fail; 26226f9a767SRodney W. Grimes } 26326f9a767SRodney W. Grimes 264c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 265450ffb44SRobert Watson imgp->fname = fname; 26626f9a767SRodney W. Grimes 26726f9a767SRodney W. Grimes /* 2689022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2699022e4adSDavid Greenman */ 270c52007c2SDavid Greenman error = exec_check_permissions(imgp); 271619eb6e5SJeff Roberson if (error) 27226f9a767SRodney W. Grimes goto exec_fail_dealloc; 273619eb6e5SJeff Roberson 274619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2750b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 27626f9a767SRodney W. Grimes 277619eb6e5SJeff Roberson /* 278619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 279619eb6e5SJeff Roberson * activating it. 280619eb6e5SJeff Roberson * 281619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 282619eb6e5SJeff Roberson * actually an executable image. 283619eb6e5SJeff Roberson */ 284619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 285619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 286619eb6e5SJeff Roberson 2871616db3cSJohn Dyson error = exec_map_first_page(imgp); 2886d5a0a8cSDavid Greenman if (error) 28926f9a767SRodney W. Grimes goto exec_fail_dealloc; 29026f9a767SRodney W. Grimes 29126f9a767SRodney W. Grimes /* 292d323ddf3SMatthew Dillon * If the current process has a special image activator it 293d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 294d323ddf3SMatthew Dillon * scripts differently. 29526f9a767SRodney W. Grimes */ 296d323ddf3SMatthew Dillon error = -1; 297d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 298d323ddf3SMatthew Dillon error = img_first(imgp); 299d323ddf3SMatthew Dillon 300d323ddf3SMatthew Dillon /* 301d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 302d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 303d323ddf3SMatthew Dillon * and an error otherwise. 304d323ddf3SMatthew Dillon */ 305d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 306d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 307d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 308d323ddf3SMatthew Dillon continue; 309d323ddf3SMatthew Dillon } 310c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 311d323ddf3SMatthew Dillon } 312d323ddf3SMatthew Dillon 313d323ddf3SMatthew Dillon if (error) { 314619eb6e5SJeff Roberson if (error == -1) { 315619eb6e5SJeff Roberson if (textset == 0) 316619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 317d323ddf3SMatthew Dillon error = ENOEXEC; 318619eb6e5SJeff Roberson } 31926f9a767SRodney W. Grimes goto exec_fail_dealloc; 320d323ddf3SMatthew Dillon } 321d323ddf3SMatthew Dillon 322d323ddf3SMatthew Dillon /* 323d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 324d323ddf3SMatthew Dillon * activate the interpreter. 325d323ddf3SMatthew Dillon */ 326c52007c2SDavid Greenman if (imgp->interpreted) { 3271616db3cSJohn Dyson exec_unmap_first_page(imgp); 328619eb6e5SJeff Roberson /* 329619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 330619eb6e5SJeff Roberson * period before we determine that something is a script where 331619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 332619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 333619eb6e5SJeff Roberson */ 334619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 335762e6b85SEivind Eklund /* free name buffer and old vnode */ 336762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 337670cb89bSRobert Watson #ifdef MAC 338eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 339eca8a663SRobert Watson mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel); 340670cb89bSRobert Watson #endif 341619eb6e5SJeff Roberson vput(ndp->ni_vp); 3420b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3430b2ed1aeSJeff Roberson imgp->object = NULL; 34426f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 345b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 346b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 34726f9a767SRodney W. Grimes goto interpret; 34826f9a767SRodney W. Grimes } 34926f9a767SRodney W. Grimes 35026f9a767SRodney W. Grimes /* 35126f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 35226f9a767SRodney W. Grimes */ 3533ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3543ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3553ebc1248SPeter Wemm else 356c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 35726f9a767SRodney W. Grimes 35826f9a767SRodney W. Grimes /* 3591e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3601e1e0b44SSøren Schmidt * let it do the stack setup. 3611e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 36226f9a767SRodney W. Grimes */ 3631e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 364c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3651e1e0b44SSøren Schmidt else 366c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 36726f9a767SRodney W. Grimes 368a78e8d2aSDavid Greenman /* 369a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 370a78e8d2aSDavid Greenman * be shared after an exec. 371a78e8d2aSDavid Greenman */ 372426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 373a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 374a78e8d2aSDavid Greenman struct filedesc *tmp; 375a78e8d2aSDavid Greenman 376c522c1bfSAlfred Perlstein tmp = fdcopy(td->td_proc->p_fd); 377426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 378b40ce416SJulian Elischer fdfree(td); 379a78e8d2aSDavid Greenman p->p_fd = tmp; 380426da3bcSAlfred Perlstein } else 381426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 382a78e8d2aSDavid Greenman 383333ea485SGuido van Rooij /* 3849b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3859b3b1c5fSJohn Baldwin */ 3869b3b1c5fSJohn Baldwin newcred = crget(); 3871419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3889b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3899b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3909b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3919b3b1c5fSJohn Baldwin 3929b3b1c5fSJohn Baldwin /* close files on exec */ 3939b3b1c5fSJohn Baldwin fdcloseexec(td); 3949b3b1c5fSJohn Baldwin 395619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 396619eb6e5SJeff Roberson VREF(ndp->ni_vp); 397619eb6e5SJeff Roberson 3989b3b1c5fSJohn Baldwin /* 399333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4008688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 401b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 402333ea485SGuido van Rooij * reset. 403333ea485SGuido van Rooij */ 4049b3b1c5fSJohn Baldwin PROC_LOCK(p); 40590af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 40690af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 4079b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 40890af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 40990af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 4109b3b1c5fSJohn Baldwin PROC_LOCK(p); 41190af4afaSJohn Baldwin p->p_sigacts = newsigacts; 41290af4afaSJohn Baldwin } else 41390af4afaSJohn Baldwin oldsigacts = NULL; 414333ea485SGuido van Rooij 415fdf4e8b3SWarner Losh /* Stop profiling */ 416fdf4e8b3SWarner Losh stopprofclock(p); 417fdf4e8b3SWarner Losh 41826f9a767SRodney W. Grimes /* reset caught signals */ 41926f9a767SRodney W. Grimes execsigs(p); 42026f9a767SRodney W. Grimes 42126f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 42226f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 42326f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 42426f9a767SRodney W. Grimes p->p_comm[len] = 0; 42526f9a767SRodney W. Grimes 42626f9a767SRodney W. Grimes /* 42724b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 428dc733423SDag-Erling Smørgrav * it that it now has its own resources back 42926f9a767SRodney W. Grimes */ 43026f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 43126f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 43226f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4337f05b035SAlfred Perlstein wakeup(p->p_pptr); 43426f9a767SRodney W. Grimes } 43526f9a767SRodney W. Grimes 43626f9a767SRodney W. Grimes /* 437a78e8d2aSDavid Greenman * Implement image setuid/setgid. 438a78e8d2aSDavid Greenman * 439a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 440a78e8d2aSDavid Greenman * the process is being traced. 441670cb89bSRobert Watson * 442670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 443670cb89bSRobert Watson * transitions on the file system. 444c52007c2SDavid Greenman */ 445b1fc0ec1SRobert Watson oldcred = p->p_ucred; 446d06c0d4dSRobert Watson credential_changing = 0; 447d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 448d06c0d4dSRobert Watson attr.va_uid; 449d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 450d06c0d4dSRobert Watson attr.va_gid; 451ccafe7ebSRobert Watson #ifdef MAC 452670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 453eca8a663SRobert Watson interplabel, imgp); 454ccafe7ebSRobert Watson credential_changing |= will_transition; 455ccafe7ebSRobert Watson #endif 456d06c0d4dSRobert Watson 457d06c0d4dSRobert Watson if (credential_changing && 458a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 459c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 460c52007c2SDavid Greenman /* 461c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 462b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 463b85db196SPeter Wemm * we do not regain any tracing during a possible block. 46426f9a767SRodney W. Grimes */ 465b85db196SPeter Wemm setsugid(p); 4666c84de02SJohn Baldwin #ifdef KTRACE 467a5881ea5SJohn Baldwin if (p->p_tracevp != NULL && suser_cred(oldcred, PRISON_ROOT)) { 4686c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 46979deba82SMatthew Dillon p->p_traceflag = 0; 470a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 471a5881ea5SJohn Baldwin p->p_tracevp = NULL; 472a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 473a5881ea5SJohn Baldwin p->p_tracecred = NULL; 4746c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 47526f9a767SRodney W. Grimes } 4766c84de02SJohn Baldwin #endif 47728b325aaSDon Lewis /* 478c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 479c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 48028b325aaSDon Lewis * 481c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 482c1e2d386SNate Lawson * which may grab the process lock. 48328b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 48428b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 48528b325aaSDon Lewis */ 48628b325aaSDon Lewis PROC_UNLOCK(p); 487c1e2d386SNate Lawson setugidsafety(td); 488e983a376SJacques Vidrine error = fdcheckstd(td); 48963c9e754SJohn Baldwin if (error != 0) 49069be5db9SAlfred Perlstein goto done1; 491e1b1aa3bSJohn Baldwin PROC_LOCK(p); 492c52007c2SDavid Greenman /* 493c52007c2SDavid Greenman * Set the new credentials. 494c52007c2SDavid Greenman */ 4959b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 496c52007c2SDavid Greenman if (attr.va_mode & VSUID) 4971419eacbSAlfred Perlstein change_euid(newcred, euip); 498c52007c2SDavid Greenman if (attr.va_mode & VSGID) 499b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 500ccafe7ebSRobert Watson #ifdef MAC 501670cb89bSRobert Watson if (will_transition) { 502670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 503eca8a663SRobert Watson interplabel, imgp); 504670cb89bSRobert Watson } 505ccafe7ebSRobert Watson #endif 5069b3b1c5fSJohn Baldwin /* 5079b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 508ccafe7ebSRobert Watson * 509ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 510ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 511ccafe7ebSRobert Watson * though maybe it shouldn't. 5129b3b1c5fSJohn Baldwin */ 5139b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 5149b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 5159b3b1c5fSJohn Baldwin p->p_ucred = newcred; 5169b3b1c5fSJohn Baldwin newcred = NULL; 517c52007c2SDavid Greenman } else { 518b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 519b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 520c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 52126f9a767SRodney W. Grimes /* 522c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 523b1fc0ec1SRobert Watson * 524b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 5259b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 5269b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 5279b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 5289b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5299b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5309b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5319b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 53226f9a767SRodney W. Grimes */ 533b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 534b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5359b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 536b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 537b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 538b1fc0ec1SRobert Watson p->p_ucred = newcred; 5399b3b1c5fSJohn Baldwin newcred = NULL; 5409b3b1c5fSJohn Baldwin } 541b1fc0ec1SRobert Watson } 54226f9a767SRodney W. Grimes 54326f9a767SRodney W. Grimes /* 544619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 545619eb6e5SJeff Roberson * to locking the proc lock. 5462a531c80SDavid Greenman */ 5479b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5482a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5492a531c80SDavid Greenman 5502a531c80SDavid Greenman /* 5519ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5529ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 553cb679c38SJonathan Lemon */ 554cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5559ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 556cb679c38SJonathan Lemon 557cb679c38SJonathan Lemon /* 55826f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 55926f9a767SRodney W. Grimes * can be set before the program executes. 56026f9a767SRodney W. Grimes */ 56126f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 56226f9a767SRodney W. Grimes psignal(p, SIGTRAP); 56326f9a767SRodney W. Grimes 56426f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 56526f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 56626f9a767SRodney W. Grimes 567b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5689b3b1c5fSJohn Baldwin oldargs = p->p_args; 569b9df5231SPoul-Henning Kamp p->p_args = NULL; 570b9df5231SPoul-Henning Kamp 571e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 572e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 573e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 574e1b1aa3bSJohn Baldwin p->p_args = newargs; 575e1b1aa3bSJohn Baldwin newargs = NULL; 576e1b1aa3bSJohn Baldwin } 577e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 578e1b1aa3bSJohn Baldwin 579e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5803ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5813ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5823ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5833ebc1248SPeter Wemm else 584bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 585bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 586e913ca22SDoug Rabson 58769be5db9SAlfred Perlstein done1: 5889b3b1c5fSJohn Baldwin /* 5899b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5909b3b1c5fSJohn Baldwin */ 5911419eacbSAlfred Perlstein uifree(euip); 5929b3b1c5fSJohn Baldwin if (newcred == NULL) 5939b3b1c5fSJohn Baldwin crfree(oldcred); 5949b3b1c5fSJohn Baldwin else 5959b3b1c5fSJohn Baldwin crfree(newcred); 5969b3b1c5fSJohn Baldwin /* 5979b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 5989b3b1c5fSJohn Baldwin */ 5999b3b1c5fSJohn Baldwin if (textvp != NULL) 6009b3b1c5fSJohn Baldwin vrele(textvp); 601619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 602619eb6e5SJeff Roberson vrele(ndp->ni_vp); 6036c84de02SJohn Baldwin #ifdef KTRACE 6049b3b1c5fSJohn Baldwin if (tracevp != NULL) 6059b3b1c5fSJohn Baldwin vrele(tracevp); 606a5881ea5SJohn Baldwin if (tracecred != NULL) 607a5881ea5SJohn Baldwin crfree(tracecred); 6086c84de02SJohn Baldwin #endif 60969be5db9SAlfred Perlstein if (oldargs != NULL) 6109b3b1c5fSJohn Baldwin pargs_drop(oldargs); 61169be5db9SAlfred Perlstein if (newargs != NULL) 61269be5db9SAlfred Perlstein pargs_drop(newargs); 61390af4afaSJohn Baldwin if (oldsigacts != NULL) 61490af4afaSJohn Baldwin sigacts_free(oldsigacts); 615b9df5231SPoul-Henning Kamp 6161616db3cSJohn Dyson exec_fail_dealloc: 6171616db3cSJohn Dyson 61826f9a767SRodney W. Grimes /* 61926f9a767SRodney W. Grimes * free various allocated resources 62026f9a767SRodney W. Grimes */ 6211616db3cSJohn Dyson if (imgp->firstpage) 6221616db3cSJohn Dyson exec_unmap_first_page(imgp); 62326f9a767SRodney W. Grimes 6249c0fed3dSDoug Rabson if (imgp->vp) { 625762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 626619eb6e5SJeff Roberson vput(imgp->vp); 627a3cf6ebaSDavid Greenman } 62826f9a767SRodney W. Grimes 629a9a08882SJeff Roberson if (imgp->stringbase != NULL) 630a9a08882SJeff Roberson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 631a9a08882SJeff Roberson ARG_MAX + PAGE_SIZE); 632a9a08882SJeff Roberson 6330b2ed1aeSJeff Roberson if (imgp->object) 6340b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6350b2ed1aeSJeff Roberson 636d1989db5SRobert Drehmel if (error == 0) { 637d1989db5SRobert Drehmel /* 638d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 639d1989db5SRobert Drehmel * the S_EXEC bit set. 640d1989db5SRobert Drehmel */ 641d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 642116734c4SMatthew Dillon goto done2; 643d1989db5SRobert Drehmel } 6441616db3cSJohn Dyson 64526f9a767SRodney W. Grimes exec_fail: 6469ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6479ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6489ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6499ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6509ca45e81SDag-Erling Smørgrav 651c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 65226f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 653670cb89bSRobert Watson #ifdef MAC 654670cb89bSRobert Watson mac_execve_exit(imgp); 655eca8a663SRobert Watson if (interplabel != NULL) 656eca8a663SRobert Watson mac_vnode_label_free(interplabel); 657670cb89bSRobert Watson #endif 658b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 65926f9a767SRodney W. Grimes /* NOT REACHED */ 660116734c4SMatthew Dillon error = 0; 66126f9a767SRodney W. Grimes } 662116734c4SMatthew Dillon done2: 663670cb89bSRobert Watson #ifdef MAC 664670cb89bSRobert Watson mac_execve_exit(imgp); 665eca8a663SRobert Watson if (interplabel != NULL) 666eca8a663SRobert Watson mac_vnode_label_free(interplabel); 667670cb89bSRobert Watson #endif 668116734c4SMatthew Dillon mtx_unlock(&Giant); 669116734c4SMatthew Dillon return (error); 67026f9a767SRodney W. Grimes } 67126f9a767SRodney W. Grimes 672450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 673450ffb44SRobert Watson struct execve_args { 674450ffb44SRobert Watson char *fname; 675450ffb44SRobert Watson char **argv; 676450ffb44SRobert Watson char **envv; 677450ffb44SRobert Watson }; 678450ffb44SRobert Watson #endif 679450ffb44SRobert Watson 680450ffb44SRobert Watson /* 681450ffb44SRobert Watson * MPSAFE 682450ffb44SRobert Watson */ 683450ffb44SRobert Watson int 684450ffb44SRobert Watson execve(td, uap) 685450ffb44SRobert Watson struct thread *td; 686450ffb44SRobert Watson struct execve_args /* { 687b80521feSAlfred Perlstein char *fname; 688b80521feSAlfred Perlstein char **argv; 689b80521feSAlfred Perlstein char **envv; 690450ffb44SRobert Watson } */ *uap; 691450ffb44SRobert Watson { 692450ffb44SRobert Watson 693670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 694670cb89bSRobert Watson } 695670cb89bSRobert Watson 696670cb89bSRobert Watson #ifndef _SYS_SYSPROTO_H_ 697670cb89bSRobert Watson struct __mac_execve_args { 698670cb89bSRobert Watson char *fname; 699670cb89bSRobert Watson char **argv; 700670cb89bSRobert Watson char **envv; 701670cb89bSRobert Watson struct mac *mac_p; 702670cb89bSRobert Watson }; 703670cb89bSRobert Watson #endif 704670cb89bSRobert Watson 705670cb89bSRobert Watson /* 706670cb89bSRobert Watson * MPSAFE 707670cb89bSRobert Watson */ 708670cb89bSRobert Watson int 709670cb89bSRobert Watson __mac_execve(td, uap) 710670cb89bSRobert Watson struct thread *td; 711670cb89bSRobert Watson struct __mac_execve_args /* { 712b80521feSAlfred Perlstein char *fname; 713b80521feSAlfred Perlstein char **argv; 714b80521feSAlfred Perlstein char **envv; 715b80521feSAlfred Perlstein struct mac *mac_p; 716670cb89bSRobert Watson } */ *uap; 717670cb89bSRobert Watson { 718670cb89bSRobert Watson 7190c93266bSRobert Watson #ifdef MAC 720670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, 721670cb89bSRobert Watson uap->mac_p)); 7220c93266bSRobert Watson #else 7230c93266bSRobert Watson return (ENOSYS); 7240c93266bSRobert Watson #endif 725450ffb44SRobert Watson } 726450ffb44SRobert Watson 7271616db3cSJohn Dyson int 7281616db3cSJohn Dyson exec_map_first_page(imgp) 7291616db3cSJohn Dyson struct image_params *imgp; 7301616db3cSJohn Dyson { 731d8aad40cSJohn Baldwin int rv, i; 73295461b45SJohn Dyson int initial_pagein; 73395461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7341616db3cSJohn Dyson vm_object_t object; 7351616db3cSJohn Dyson 7360cddd8f0SMatthew Dillon GIANT_REQUIRED; 7371616db3cSJohn Dyson 7381616db3cSJohn Dyson if (imgp->firstpage) { 7391616db3cSJohn Dyson exec_unmap_first_page(imgp); 7401616db3cSJohn Dyson } 7411616db3cSJohn Dyson 7429ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 743fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 74495461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 74595461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 74695461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 74795461b45SJohn Dyson if (initial_pagein > object->size) 74895461b45SJohn Dyson initial_pagein = object->size; 74995461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 750d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 7516ec2fca5SAlan Cox if (ma[i]->valid) 7526ec2fca5SAlan Cox break; 753ee113343SAlan Cox vm_page_lock_queues(); 75406fa71cdSAlan Cox if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) { 75506fa71cdSAlan Cox vm_page_unlock_queues(); 75606fa71cdSAlan Cox break; 75706fa71cdSAlan Cox } 758e69763a3SDoug Rabson vm_page_busy(ma[i]); 759ee113343SAlan Cox vm_page_unlock_queues(); 76095461b45SJohn Dyson } else { 761ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 762ca0387efSJake Burkholder VM_ALLOC_NORMAL); 76395461b45SJohn Dyson if (ma[i] == NULL) 76495461b45SJohn Dyson break; 76595461b45SJohn Dyson } 76695461b45SJohn Dyson } 76795461b45SJohn Dyson initial_pagein = i; 76895461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 76995461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 770ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 771ca0387efSJake Burkholder (ma[0]->valid == 0)) { 772ecbb00a2SDoug Rabson if (ma[0]) { 7736ec2fca5SAlan Cox vm_page_lock_queues(); 7744fec79beSAlan Cox pmap_remove_all(ma[0]); 7758f9110f6SJohn Dyson vm_page_free(ma[0]); 77606fa71cdSAlan Cox vm_page_unlock_queues(); 7776ec2fca5SAlan Cox } 77806fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 779a7cddfedSJake Burkholder return (EIO); 7801616db3cSJohn Dyson } 7811616db3cSJohn Dyson } 7826ec2fca5SAlan Cox vm_page_lock_queues(); 78395461b45SJohn Dyson vm_page_wire(ma[0]); 784e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 78597646f56SAlan Cox vm_page_unlock_queues(); 7866ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 7871616db3cSJohn Dyson 788ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 78995461b45SJohn Dyson imgp->firstpage = ma[0]; 7901616db3cSJohn Dyson 791a7cddfedSJake Burkholder return (0); 7921616db3cSJohn Dyson } 7931616db3cSJohn Dyson 7941616db3cSJohn Dyson void 7951616db3cSJohn Dyson exec_unmap_first_page(imgp) 7961616db3cSJohn Dyson struct image_params *imgp; 7971616db3cSJohn Dyson { 7980cddd8f0SMatthew Dillon GIANT_REQUIRED; 79923955314SAlfred Perlstein 8001616db3cSJohn Dyson if (imgp->firstpage) { 801ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 80297646f56SAlan Cox vm_page_lock_queues(); 80373007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 80497646f56SAlan Cox vm_page_unlock_queues(); 8051616db3cSJohn Dyson imgp->firstpage = NULL; 8061616db3cSJohn Dyson } 8071616db3cSJohn Dyson } 8081616db3cSJohn Dyson 80926f9a767SRodney W. Grimes /* 81026f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 81126f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 81226f9a767SRodney W. Grimes * automatically in trap.c. 81326f9a767SRodney W. Grimes */ 81426f9a767SRodney W. Grimes int 81505ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 816c52007c2SDavid Greenman struct image_params *imgp; 81705ba50f5SJake Burkholder struct sysentvec *sv; 81826f9a767SRodney W. Grimes { 81926f9a767SRodney W. Grimes int error; 8205e20c11fSAlan Cox struct proc *p = imgp->proc; 8215e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 82205ba50f5SJake Burkholder vm_offset_t stack_addr; 82305ba50f5SJake Burkholder vm_map_t map; 82426f9a767SRodney W. Grimes 8250cddd8f0SMatthew Dillon GIANT_REQUIRED; 8260cddd8f0SMatthew Dillon 827c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 82826f9a767SRodney W. Grimes 82975b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exec, p); 8306f5dafeaSAlan Cox 8316f5dafeaSAlan Cox /* 832c460ac3aSPeter Wemm * Here is as good a place as any to do any resource limit cleanups. 833c460ac3aSPeter Wemm * This is needed if a 64 bit binary exec's a 32 bit binary - the 834c460ac3aSPeter Wemm * data size limit may need to be changed to a value that makes 835c460ac3aSPeter Wemm * sense for the 32 bit binary. 836c460ac3aSPeter Wemm */ 837c460ac3aSPeter Wemm if (sv->sv_fixlimits) 838c460ac3aSPeter Wemm sv->sv_fixlimits(imgp); 839c460ac3aSPeter Wemm 840c460ac3aSPeter Wemm /* 841af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 842af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 843af9ec885SJohn Dyson * not disrupted 844af9ec885SJohn Dyson */ 84505ba50f5SJake Burkholder map = &vmspace->vm_map; 84605ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 84705ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 8483db161e0SMatthew Dillon shmexit(vmspace); 8492d21129dSAlan Cox vm_page_lock_queues(); 85005ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 85105ba50f5SJake Burkholder vm_map_max(map)); 8522d21129dSAlan Cox vm_page_unlock_queues(); 85305ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 854af9ec885SJohn Dyson } else { 85505ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 8565e20c11fSAlan Cox vmspace = p->p_vmspace; 85705ba50f5SJake Burkholder map = &vmspace->vm_map; 858af9ec885SJohn Dyson } 85926f9a767SRodney W. Grimes 86026f9a767SRodney W. Grimes /* Allocate a new stack */ 861fd75d710SMarcel Moolenaar stack_addr = sv->sv_usrstack - maxssiz; 86205ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 863fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 8642267af78SJulian Elischer if (error) 8652267af78SJulian Elischer return (error); 8662267af78SJulian Elischer 86763c47a5cSDoug Rabson #ifdef __ia64__ 868fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 869bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 870fd75d710SMarcel Moolenaar error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 871fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 872fd75d710SMarcel Moolenaar if (error) 873fd75d710SMarcel Moolenaar return (error); 87463c47a5cSDoug Rabson #endif 87563c47a5cSDoug Rabson 8762267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 8772267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 8782267af78SJulian Elischer * process stack so we can check the stack rlimit. 8792267af78SJulian Elischer */ 880cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 88105ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 88226f9a767SRodney W. Grimes 88326f9a767SRodney W. Grimes return (0); 88426f9a767SRodney W. Grimes } 88526f9a767SRodney W. Grimes 88626f9a767SRodney W. Grimes /* 88726f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 88826f9a767SRodney W. Grimes * address space into the temporary string buffer. 88926f9a767SRodney W. Grimes */ 89026f9a767SRodney W. Grimes int 891c52007c2SDavid Greenman exec_extract_strings(imgp) 892c52007c2SDavid Greenman struct image_params *imgp; 89326f9a767SRodney W. Grimes { 89426f9a767SRodney W. Grimes char **argv, **envv; 89526f9a767SRodney W. Grimes char *argp, *envp; 896ecbb00a2SDoug Rabson int error; 897ecbb00a2SDoug Rabson size_t length; 89826f9a767SRodney W. Grimes 89926f9a767SRodney W. Grimes /* 90026f9a767SRodney W. Grimes * extract arguments first 90126f9a767SRodney W. Grimes */ 90226f9a767SRodney W. Grimes 903450ffb44SRobert Watson argv = imgp->userspace_argv; 90426f9a767SRodney W. Grimes 9050fd1a014SDavid Greenman if (argv) { 906aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 9075cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 9085cf3d12cSAndrey A. Chernov return (EFAULT); 9095cf3d12cSAndrey A. Chernov if (argp) 9105cf3d12cSAndrey A. Chernov argv++; 9115cf3d12cSAndrey A. Chernov if (imgp->argv0) 9125cf3d12cSAndrey A. Chernov argp = imgp->argv0; 9135cf3d12cSAndrey A. Chernov if (argp) { 9145cf3d12cSAndrey A. Chernov do { 91526f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 91626f9a767SRodney W. Grimes return (EFAULT); 917c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 918c52007c2SDavid Greenman imgp->stringspace, &length))) { 9190fd1a014SDavid Greenman if (error == ENAMETOOLONG) 92026f9a767SRodney W. Grimes return (E2BIG); 9210fd1a014SDavid Greenman return (error); 9220fd1a014SDavid Greenman } 923c52007c2SDavid Greenman imgp->stringspace -= length; 924c52007c2SDavid Greenman imgp->stringp += length; 925c52007c2SDavid Greenman imgp->argc++; 926aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 92726f9a767SRodney W. Grimes } 9280fd1a014SDavid Greenman } 92926f9a767SRodney W. Grimes 930b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 931b9df5231SPoul-Henning Kamp 93226f9a767SRodney W. Grimes /* 93326f9a767SRodney W. Grimes * extract environment strings 93426f9a767SRodney W. Grimes */ 93526f9a767SRodney W. Grimes 936450ffb44SRobert Watson envv = imgp->userspace_envv; 93726f9a767SRodney W. Grimes 9380fd1a014SDavid Greenman if (envv) { 939aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 94026f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 94126f9a767SRodney W. Grimes return (EFAULT); 942c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 943c52007c2SDavid Greenman imgp->stringspace, &length))) { 9440fd1a014SDavid Greenman if (error == ENAMETOOLONG) 94526f9a767SRodney W. Grimes return (E2BIG); 9460fd1a014SDavid Greenman return (error); 9470fd1a014SDavid Greenman } 948c52007c2SDavid Greenman imgp->stringspace -= length; 949c52007c2SDavid Greenman imgp->stringp += length; 950c52007c2SDavid Greenman imgp->envc++; 95126f9a767SRodney W. Grimes } 9520fd1a014SDavid Greenman } 95326f9a767SRodney W. Grimes 95426f9a767SRodney W. Grimes return (0); 95526f9a767SRodney W. Grimes } 95626f9a767SRodney W. Grimes 95726f9a767SRodney W. Grimes /* 95826f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 95926f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 96026f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 96126f9a767SRodney W. Grimes */ 962654f6be1SBruce Evans register_t * 963c52007c2SDavid Greenman exec_copyout_strings(imgp) 964c52007c2SDavid Greenman struct image_params *imgp; 96526f9a767SRodney W. Grimes { 96626f9a767SRodney W. Grimes int argc, envc; 96726f9a767SRodney W. Grimes char **vectp; 96826f9a767SRodney W. Grimes char *stringp, *destp; 969654f6be1SBruce Evans register_t *stack_base; 97093f6448cSDavid Greenman struct ps_strings *arginfo; 971f3bec5d7SJake Burkholder struct proc *p; 972d66a5066SPeter Wemm int szsigcode; 97326f9a767SRodney W. Grimes 97426f9a767SRodney W. Grimes /* 97526f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 976d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 97726f9a767SRodney W. Grimes */ 978f3bec5d7SJake Burkholder p = imgp->proc; 979f3bec5d7SJake Burkholder szsigcode = 0; 98005ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 981f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 982f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 983d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 9841ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 9851ed012f9SPeter Wemm 98626f9a767SRodney W. Grimes /* 987d66a5066SPeter Wemm * install sigcode 988d66a5066SPeter Wemm */ 989d66a5066SPeter Wemm if (szsigcode) 990f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 991f3bec5d7SJake Burkholder szsigcode), szsigcode); 992d66a5066SPeter Wemm 993d66a5066SPeter Wemm /* 994e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 995e1743d02SSøren Schmidt * on the stack. 996e1743d02SSøren Schmidt */ 997b9a22da4STakanori Watanabe if (imgp->auxargs) { 998e1743d02SSøren Schmidt /* 999b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1000b9a22da4STakanori Watanabe * lower compatibility. 1001b9a22da4STakanori Watanabe */ 1002ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1003ca0387efSJake Burkholder (AT_COUNT * 2); 1004b9a22da4STakanori Watanabe /* 1005b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1006b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1007b9a22da4STakanori Watanabe * for argument of Runtime loader. 1008e1743d02SSøren Schmidt */ 1009e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1010b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 1011b9a22da4STakanori Watanabe 1012b9a22da4STakanori Watanabe } else 1013e1743d02SSøren Schmidt /* 1014b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1015b9a22da4STakanori Watanabe * the arg and env vector sets 101626f9a767SRodney W. Grimes */ 1017ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1018ca0387efSJake Burkholder sizeof(char *)); 101926f9a767SRodney W. Grimes 102026f9a767SRodney W. Grimes /* 102126f9a767SRodney W. Grimes * vectp also becomes our initial stack base 102226f9a767SRodney W. Grimes */ 1023654f6be1SBruce Evans stack_base = (register_t *)vectp; 102426f9a767SRodney W. Grimes 1025c52007c2SDavid Greenman stringp = imgp->stringbase; 1026c52007c2SDavid Greenman argc = imgp->argc; 1027c52007c2SDavid Greenman envc = imgp->envc; 102826f9a767SRodney W. Grimes 102993f6448cSDavid Greenman /* 10303aed948bSDavid Greenman * Copy out strings - arguments and environment. 103193f6448cSDavid Greenman */ 1032c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 103393f6448cSDavid Greenman 103493f6448cSDavid Greenman /* 10353aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 10363aed948bSDavid Greenman */ 1037aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 10383aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 10393aed948bSDavid Greenman 10403aed948bSDavid Greenman /* 10413aed948bSDavid Greenman * Fill in argument portion of vector table. 104293f6448cSDavid Greenman */ 104326f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1044aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10453aed948bSDavid Greenman while (*stringp++ != 0) 10463aed948bSDavid Greenman destp++; 10473aed948bSDavid Greenman destp++; 104826f9a767SRodney W. Grimes } 104926f9a767SRodney W. Grimes 10501a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 10516ab46d52SBruce Evans suword(vectp++, 0); 105226f9a767SRodney W. Grimes 1053aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 10543aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 105593f6448cSDavid Greenman 105693f6448cSDavid Greenman /* 10573aed948bSDavid Greenman * Fill in environment portion of vector table. 105893f6448cSDavid Greenman */ 105926f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1060aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10613aed948bSDavid Greenman while (*stringp++ != 0) 10623aed948bSDavid Greenman destp++; 10633aed948bSDavid Greenman destp++; 106426f9a767SRodney W. Grimes } 106526f9a767SRodney W. Grimes 106626f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 10676ab46d52SBruce Evans suword(vectp, 0); 106826f9a767SRodney W. Grimes 106926f9a767SRodney W. Grimes return (stack_base); 107026f9a767SRodney W. Grimes } 107126f9a767SRodney W. Grimes 107226f9a767SRodney W. Grimes /* 107326f9a767SRodney W. Grimes * Check permissions of file to execute. 1074cf64863aSRobert Watson * Called with imgp->vp locked. 107526f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 107626f9a767SRodney W. Grimes */ 1077c8a79999SPeter Wemm int 1078c52007c2SDavid Greenman exec_check_permissions(imgp) 1079c52007c2SDavid Greenman struct image_params *imgp; 108026f9a767SRodney W. Grimes { 1081c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1082c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1083a854ed98SJohn Baldwin struct thread *td; 108426f9a767SRodney W. Grimes int error; 108526f9a767SRodney W. Grimes 1086a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1087339b79b9SRobert Watson 1088ec35c2afSRobert Watson /* Get file attributes */ 1089ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1090ec35c2afSRobert Watson if (error) 1091ec35c2afSRobert Watson return (error); 1092ec35c2afSRobert Watson 1093339b79b9SRobert Watson #ifdef MAC 1094670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1095339b79b9SRobert Watson if (error) 1096339b79b9SRobert Watson return (error); 1097339b79b9SRobert Watson #endif 1098339b79b9SRobert Watson 109926f9a767SRodney W. Grimes /* 110026f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 110126f9a767SRodney W. Grimes * file resides on. 110226f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 110326f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 110426f9a767SRodney W. Grimes * file really is executable. 110526f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 110626f9a767SRodney W. Grimes */ 1107c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 110826f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1109a854ed98SJohn Baldwin (attr->va_type != VREG)) 111026f9a767SRodney W. Grimes return (EACCES); 111126f9a767SRodney W. Grimes 111226f9a767SRodney W. Grimes /* 111326f9a767SRodney W. Grimes * Zero length files can't be exec'd 111426f9a767SRodney W. Grimes */ 111526f9a767SRodney W. Grimes if (attr->va_size == 0) 111626f9a767SRodney W. Grimes return (ENOEXEC); 111726f9a767SRodney W. Grimes 111826f9a767SRodney W. Grimes /* 111926f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 112026f9a767SRodney W. Grimes */ 1121a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 112226f9a767SRodney W. Grimes if (error) 112326f9a767SRodney W. Grimes return (error); 112426f9a767SRodney W. Grimes 11256d5a0a8cSDavid Greenman /* 11266d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 11276d5a0a8cSDavid Greenman * if there are any. 11286d5a0a8cSDavid Greenman */ 11296d5a0a8cSDavid Greenman if (vp->v_writecount) 11306d5a0a8cSDavid Greenman return (ETXTBSY); 11316d5a0a8cSDavid Greenman 11326d5a0a8cSDavid Greenman /* 11336d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 11346d5a0a8cSDavid Greenman * general case). 11356d5a0a8cSDavid Greenman */ 1136a8d43c90SPoul-Henning Kamp error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 113726f9a767SRodney W. Grimes return (error); 1138df8bae1dSRodney W. Grimes } 1139aa855a59SPeter Wemm 1140aa855a59SPeter Wemm /* 1141aa855a59SPeter Wemm * Exec handler registration 1142aa855a59SPeter Wemm */ 1143aa855a59SPeter Wemm int 1144aa855a59SPeter Wemm exec_register(execsw_arg) 1145aa855a59SPeter Wemm const struct execsw *execsw_arg; 1146aa855a59SPeter Wemm { 1147aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1148aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1149aa855a59SPeter Wemm 1150aa855a59SPeter Wemm if (execsw) 1151aa855a59SPeter Wemm for (es = execsw; *es; es++) 1152aa855a59SPeter Wemm count++; 1153a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1154aa855a59SPeter Wemm if (newexecsw == NULL) 1155a7cddfedSJake Burkholder return (ENOMEM); 1156aa855a59SPeter Wemm xs = newexecsw; 1157aa855a59SPeter Wemm if (execsw) 1158aa855a59SPeter Wemm for (es = execsw; *es; es++) 1159aa855a59SPeter Wemm *xs++ = *es; 1160aa855a59SPeter Wemm *xs++ = execsw_arg; 1161aa855a59SPeter Wemm *xs = NULL; 1162aa855a59SPeter Wemm if (execsw) 1163aa855a59SPeter Wemm free(execsw, M_TEMP); 1164aa855a59SPeter Wemm execsw = newexecsw; 1165a7cddfedSJake Burkholder return (0); 1166aa855a59SPeter Wemm } 1167aa855a59SPeter Wemm 1168aa855a59SPeter Wemm int 1169aa855a59SPeter Wemm exec_unregister(execsw_arg) 1170aa855a59SPeter Wemm const struct execsw *execsw_arg; 1171aa855a59SPeter Wemm { 1172aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1173aa855a59SPeter Wemm int count = 1; 1174aa855a59SPeter Wemm 1175aa855a59SPeter Wemm if (execsw == NULL) 1176aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1177aa855a59SPeter Wemm 1178aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1179aa855a59SPeter Wemm if (*es == execsw_arg) 1180aa855a59SPeter Wemm break; 1181aa855a59SPeter Wemm } 1182aa855a59SPeter Wemm if (*es == NULL) 1183a7cddfedSJake Burkholder return (ENOENT); 1184aa855a59SPeter Wemm for (es = execsw; *es; es++) 1185aa855a59SPeter Wemm if (*es != execsw_arg) 1186aa855a59SPeter Wemm count++; 1187a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1188aa855a59SPeter Wemm if (newexecsw == NULL) 1189a7cddfedSJake Burkholder return (ENOMEM); 1190aa855a59SPeter Wemm xs = newexecsw; 1191aa855a59SPeter Wemm for (es = execsw; *es; es++) 1192aa855a59SPeter Wemm if (*es != execsw_arg) 1193aa855a59SPeter Wemm *xs++ = *es; 1194aa855a59SPeter Wemm *xs = NULL; 1195aa855a59SPeter Wemm if (execsw) 1196aa855a59SPeter Wemm free(execsw, M_TEMP); 1197aa855a59SPeter Wemm execsw = newexecsw; 1198a7cddfedSJake Burkholder return (0); 1199aa855a59SPeter Wemm } 1200