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 * 26c3aac50fSPeter Wemm * $FreeBSD$ 27df8bae1dSRodney W. Grimes */ 28df8bae1dSRodney W. Grimes 291e9b3d91SPeter Wemm #include "opt_ktrace.h" 30339b79b9SRobert Watson #include "opt_mac.h" 311e9b3d91SPeter Wemm 32df8bae1dSRodney W. Grimes #include <sys/param.h> 3326f9a767SRodney W. Grimes #include <sys/systm.h> 34fb919e4dSMark Murray #include <sys/lock.h> 35fb919e4dSMark Murray #include <sys/mutex.h> 36d2d3e875SBruce Evans #include <sys/sysproto.h> 3726f9a767SRodney W. Grimes #include <sys/signalvar.h> 3826f9a767SRodney W. Grimes #include <sys/kernel.h> 39339b79b9SRobert Watson #include <sys/mac.h> 4026f9a767SRodney W. Grimes #include <sys/mount.h> 41797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 42079cc25bSDavid Greenman #include <sys/fcntl.h> 4326f9a767SRodney W. Grimes #include <sys/acct.h> 4426f9a767SRodney W. Grimes #include <sys/exec.h> 4526f9a767SRodney W. Grimes #include <sys/imgact.h> 46e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4726f9a767SRodney W. Grimes #include <sys/wait.h> 48333ea485SGuido van Rooij #include <sys/malloc.h> 49a794e791SBruce Evans #include <sys/proc.h> 502a024a2bSSean Eric Fagan #include <sys/pioctl.h> 51a794e791SBruce Evans #include <sys/namei.h> 521e1e0b44SSøren Schmidt #include <sys/sysent.h> 53797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5499ac3bc8SPeter Wemm #include <sys/sysctl.h> 55333ea485SGuido van Rooij #include <sys/user.h> 56a794e791SBruce Evans #include <sys/vnode.h> 57c781aea8SPeter Wemm #ifdef KTRACE 58c781aea8SPeter Wemm #include <sys/ktrace.h> 59c781aea8SPeter Wemm #endif 6026f9a767SRodney W. Grimes 6126f9a767SRodney W. Grimes #include <vm/vm.h> 62efeaf95aSDavid Greenman #include <vm/vm_param.h> 63efeaf95aSDavid Greenman #include <vm/pmap.h> 641616db3cSJohn Dyson #include <vm/vm_page.h> 65efeaf95aSDavid Greenman #include <vm/vm_map.h> 6626f9a767SRodney W. Grimes #include <vm/vm_kern.h> 67efeaf95aSDavid Greenman #include <vm/vm_extern.h> 681ebd0c59SDavid Greenman #include <vm/vm_object.h> 691616db3cSJohn Dyson #include <vm/vm_pager.h> 7026f9a767SRodney W. Grimes 7126f9a767SRodney W. Grimes #include <machine/reg.h> 7226f9a767SRodney W. Grimes 73b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 74b9df5231SPoul-Henning Kamp 7521d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback"); 7621d56e9cSAlfred Perlstein 7705ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 7805ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 79450ffb44SRobert Watson static int kern_execve(struct thread *td, char *fname, char **argv, 80670cb89bSRobert Watson char **envv, struct mac *mac_p); 8105ba50f5SJake Burkholder 8221d56e9cSAlfred Perlstein /* 8321d56e9cSAlfred Perlstein * callout list for things to do at exec time 8421d56e9cSAlfred Perlstein */ 8521d56e9cSAlfred Perlstein struct execlist { 8621d56e9cSAlfred Perlstein execlist_fn function; 8721d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 8821d56e9cSAlfred Perlstein }; 8921d56e9cSAlfred Perlstein 9021d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 9121d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 9221d56e9cSAlfred Perlstein 939701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9505ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 96486bddb0SDoug Rabson 979701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9805ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9905ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 10099ac3bc8SPeter Wemm 101b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 102c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1039701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 104b9df5231SPoul-Henning Kamp 105a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 106a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 107a8704f89SPoul-Henning Kamp 108911fc923SPeter Wemm #ifdef __ia64__ 109911fc923SPeter Wemm /* XXX HACK */ 110911fc923SPeter Wemm static int regstkpages = 256; 111911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 112911fc923SPeter Wemm #endif 113911fc923SPeter Wemm 11405ba50f5SJake Burkholder static int 11505ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 11605ba50f5SJake Burkholder { 11705ba50f5SJake Burkholder struct proc *p; 11805ba50f5SJake Burkholder 11905ba50f5SJake Burkholder p = curproc; 12005ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 12105ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 12205ba50f5SJake Burkholder } 12305ba50f5SJake Burkholder 12405ba50f5SJake Burkholder static int 12505ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12605ba50f5SJake Burkholder { 12705ba50f5SJake Burkholder struct proc *p; 12805ba50f5SJake Burkholder 12905ba50f5SJake Burkholder p = curproc; 13005ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 13105ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 13205ba50f5SJake Burkholder } 13305ba50f5SJake Burkholder 13499ac3bc8SPeter Wemm /* 135aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 136aa855a59SPeter Wemm * double pointer here. 137df8bae1dSRodney W. Grimes */ 138aa855a59SPeter Wemm static const struct execsw **execsw; 13926f9a767SRodney W. Grimes 14026f9a767SRodney W. Grimes /* 141450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 142450ffb44SRobert Watson * userspace pointers from the passed thread. 143116734c4SMatthew Dillon * 144116734c4SMatthew Dillon * MPSAFE 14526f9a767SRodney W. Grimes */ 146450ffb44SRobert Watson static int 147670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p) 148b40ce416SJulian Elischer struct thread *td; 149450ffb44SRobert Watson char *fname; 150450ffb44SRobert Watson char **argv; 151450ffb44SRobert Watson char **envv; 152670cb89bSRobert Watson struct mac *mac_p; 153df8bae1dSRodney W. Grimes { 154b40ce416SJulian Elischer struct proc *p = td->td_proc; 15526f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1569b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1571419eacbSAlfred Perlstein struct uidinfo *euip; 158654f6be1SBruce Evans register_t *stack_base; 159bb56ec4aSPoul-Henning Kamp int error, len, i; 160c52007c2SDavid Greenman struct image_params image_params, *imgp; 16126f9a767SRodney W. Grimes struct vattr attr; 1624d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 16369be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1649b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1656c84de02SJohn Baldwin #ifdef KTRACE 1666c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1676c84de02SJohn Baldwin #endif 1686c84de02SJohn Baldwin struct vnode *textvp = NULL; 169d06c0d4dSRobert Watson int credential_changing; 170619eb6e5SJeff Roberson int textset; 171ccafe7ebSRobert Watson #ifdef MAC 172670cb89bSRobert Watson struct label interplabel; /* label of the interpreted vnode */ 173670cb89bSRobert Watson struct label execlabel; /* optional label argument */ 174670cb89bSRobert Watson int will_transition, interplabelvalid = 0; 175ccafe7ebSRobert Watson #endif 17626f9a767SRodney W. Grimes 177c52007c2SDavid Greenman imgp = &image_params; 178df8bae1dSRodney W. Grimes 1799ca45e81SDag-Erling Smørgrav /* 1809ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1819ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1829ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1839ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1849ca45e81SDag-Erling Smørgrav * privileges. 1859ca45e81SDag-Erling Smørgrav */ 1869ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1879ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 18891f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 18949539972SJulian Elischer if (p->p_flag & P_KSES) { 1901279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 191e602ba25SJulian Elischer PROC_UNLOCK(p); 192e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 193e602ba25SJulian Elischer } 19449539972SJulian Elischer /* 19549539972SJulian Elischer * If we get here all other threads are dead, 19649539972SJulian Elischer * so unset the associated flags and lose KSE mode. 19749539972SJulian Elischer */ 19849539972SJulian Elischer p->p_flag &= ~P_KSES; 19949539972SJulian Elischer td->td_flags &= ~TDF_UNBOUND; 20049539972SJulian Elischer thread_single_end(); 20149539972SJulian Elischer } 2029ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2039ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2049ca45e81SDag-Erling Smørgrav 205df8bae1dSRodney W. Grimes /* 206c52007c2SDavid Greenman * Initialize part of the common data 207df8bae1dSRodney W. Grimes */ 208c52007c2SDavid Greenman imgp->proc = p; 209450ffb44SRobert Watson imgp->userspace_argv = argv; 210450ffb44SRobert Watson imgp->userspace_envv = envv; 211670cb89bSRobert Watson imgp->execlabel = NULL; 212c52007c2SDavid Greenman imgp->attr = &attr; 213c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2145cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 215c52007c2SDavid Greenman imgp->entry_addr = 0; 216c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 217c52007c2SDavid Greenman imgp->interpreted = 0; 218c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 219e1743d02SSøren Schmidt imgp->auxargs = NULL; 2201616db3cSJohn Dyson imgp->vp = NULL; 2210b2ed1aeSJeff Roberson imgp->object = NULL; 2221616db3cSJohn Dyson imgp->firstpage = NULL; 2234fe88fe6SJohn Polstra imgp->ps_strings = 0; 224b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 22526f9a767SRodney W. Grimes 226670cb89bSRobert Watson #ifdef MAC 227670cb89bSRobert Watson error = mac_execve_enter(imgp, mac_p, &execlabel); 228670cb89bSRobert Watson if (error) { 229670cb89bSRobert Watson mtx_lock(&Giant); 230670cb89bSRobert Watson goto exec_fail; 231670cb89bSRobert Watson } 232670cb89bSRobert Watson #endif 233670cb89bSRobert Watson 23426f9a767SRodney W. Grimes /* 23526f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 23626f9a767SRodney W. Grimes * environment strings 23726f9a767SRodney W. Grimes */ 238ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 239ca0387efSJake Burkholder PAGE_SIZE); 240c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 24126f9a767SRodney W. Grimes error = ENOMEM; 242b3afd20dSAlan Cox mtx_lock(&Giant); 24326f9a767SRodney W. Grimes goto exec_fail; 24426f9a767SRodney W. Grimes } 245c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 246c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2471616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 24826f9a767SRodney W. Grimes 24926f9a767SRodney W. Grimes /* 25026f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 25126f9a767SRodney W. Grimes * in ni_vp amoung other things. 25226f9a767SRodney W. Grimes */ 25326f9a767SRodney W. Grimes ndp = &nd; 254b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 255450ffb44SRobert Watson UIO_USERSPACE, fname, td); 25626f9a767SRodney W. Grimes 257b3afd20dSAlan Cox mtx_lock(&Giant); 25826f9a767SRodney W. Grimes interpret: 25926f9a767SRodney W. Grimes 26026f9a767SRodney W. Grimes error = namei(ndp); 26126f9a767SRodney W. Grimes if (error) { 2621616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2631616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 26426f9a767SRodney W. Grimes goto exec_fail; 26526f9a767SRodney W. Grimes } 26626f9a767SRodney W. Grimes 267c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 268450ffb44SRobert Watson imgp->fname = fname; 26926f9a767SRodney W. Grimes 27026f9a767SRodney W. Grimes /* 2719022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2729022e4adSDavid Greenman */ 273c52007c2SDavid Greenman error = exec_check_permissions(imgp); 274619eb6e5SJeff Roberson if (error) 27526f9a767SRodney W. Grimes goto exec_fail_dealloc; 276619eb6e5SJeff Roberson 277619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2780b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 27926f9a767SRodney W. Grimes 280619eb6e5SJeff Roberson /* 281619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 282619eb6e5SJeff Roberson * activating it. 283619eb6e5SJeff Roberson * 284619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 285619eb6e5SJeff Roberson * actually an executable image. 286619eb6e5SJeff Roberson */ 287619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 288619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 289619eb6e5SJeff Roberson 2901616db3cSJohn Dyson error = exec_map_first_page(imgp); 2916d5a0a8cSDavid Greenman if (error) 29226f9a767SRodney W. Grimes goto exec_fail_dealloc; 29326f9a767SRodney W. Grimes 29426f9a767SRodney W. Grimes /* 295d323ddf3SMatthew Dillon * If the current process has a special image activator it 296d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 297d323ddf3SMatthew Dillon * scripts differently. 29826f9a767SRodney W. Grimes */ 299d323ddf3SMatthew Dillon error = -1; 300d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 301d323ddf3SMatthew Dillon error = img_first(imgp); 302d323ddf3SMatthew Dillon 303d323ddf3SMatthew Dillon /* 304d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 305d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 306d323ddf3SMatthew Dillon * and an error otherwise. 307d323ddf3SMatthew Dillon */ 308d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 309d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 310d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 311d323ddf3SMatthew Dillon continue; 312d323ddf3SMatthew Dillon } 313c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 314d323ddf3SMatthew Dillon } 315d323ddf3SMatthew Dillon 316d323ddf3SMatthew Dillon if (error) { 317619eb6e5SJeff Roberson if (error == -1) { 318619eb6e5SJeff Roberson if (textset == 0) 319619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 320d323ddf3SMatthew Dillon error = ENOEXEC; 321619eb6e5SJeff Roberson } 32226f9a767SRodney W. Grimes goto exec_fail_dealloc; 323d323ddf3SMatthew Dillon } 324d323ddf3SMatthew Dillon 325d323ddf3SMatthew Dillon /* 326d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 327d323ddf3SMatthew Dillon * activate the interpreter. 328d323ddf3SMatthew Dillon */ 329c52007c2SDavid Greenman if (imgp->interpreted) { 3301616db3cSJohn Dyson exec_unmap_first_page(imgp); 331619eb6e5SJeff Roberson /* 332619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 333619eb6e5SJeff Roberson * period before we determine that something is a script where 334619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 335619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 336619eb6e5SJeff Roberson */ 337619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 338762e6b85SEivind Eklund /* free name buffer and old vnode */ 339762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 340670cb89bSRobert Watson #ifdef MAC 341670cb89bSRobert Watson mac_init_vnode_label(&interplabel); 342670cb89bSRobert Watson mac_copy_vnode_label(&ndp->ni_vp->v_label, &interplabel); 343670cb89bSRobert Watson interplabelvalid = 1; 344670cb89bSRobert Watson #endif 345619eb6e5SJeff Roberson vput(ndp->ni_vp); 3460b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3470b2ed1aeSJeff Roberson imgp->object = NULL; 34826f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 349b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 350b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 35126f9a767SRodney W. Grimes goto interpret; 35226f9a767SRodney W. Grimes } 35326f9a767SRodney W. Grimes 35426f9a767SRodney W. Grimes /* 35526f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 35626f9a767SRodney W. Grimes */ 3573ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3583ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3593ebc1248SPeter Wemm else 360c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 36126f9a767SRodney W. Grimes 36226f9a767SRodney W. Grimes /* 3631e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3641e1e0b44SSøren Schmidt * let it do the stack setup. 3651e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 36626f9a767SRodney W. Grimes */ 3671e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 368c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3691e1e0b44SSøren Schmidt else 370c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 37126f9a767SRodney W. Grimes 372a78e8d2aSDavid Greenman /* 373a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 374a78e8d2aSDavid Greenman * be shared after an exec. 375a78e8d2aSDavid Greenman */ 376426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 377a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 378a78e8d2aSDavid Greenman struct filedesc *tmp; 379a78e8d2aSDavid Greenman 380b40ce416SJulian Elischer tmp = fdcopy(td); 381426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 382b40ce416SJulian Elischer fdfree(td); 383a78e8d2aSDavid Greenman p->p_fd = tmp; 384426da3bcSAlfred Perlstein } else 385426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 386a78e8d2aSDavid Greenman 387333ea485SGuido van Rooij /* 3889b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3899b3b1c5fSJohn Baldwin */ 3909b3b1c5fSJohn Baldwin newcred = crget(); 3911419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3929b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3939b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3949b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3959b3b1c5fSJohn Baldwin 3969b3b1c5fSJohn Baldwin /* close files on exec */ 3979b3b1c5fSJohn Baldwin fdcloseexec(td); 3989b3b1c5fSJohn Baldwin 399619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 400619eb6e5SJeff Roberson VREF(ndp->ni_vp); 401619eb6e5SJeff Roberson 4029b3b1c5fSJohn Baldwin /* 403333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4048688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 405b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 406333ea485SGuido van Rooij * reset. 407333ea485SGuido van Rooij */ 4089b3b1c5fSJohn Baldwin PROC_LOCK(p); 4099b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 410333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 4119b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 4129b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 413333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 414333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 4159b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 4169b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 4179b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 4189b3b1c5fSJohn Baldwin PROC_LOCK(p); 419333ea485SGuido van Rooij p->p_procsig = newprocsig; 420b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 421b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 422333ea485SGuido van Rooij 423b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 424b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 425333ea485SGuido van Rooij } 426fdf4e8b3SWarner Losh /* Stop profiling */ 427fdf4e8b3SWarner Losh stopprofclock(p); 428fdf4e8b3SWarner Losh 42926f9a767SRodney W. Grimes /* reset caught signals */ 43026f9a767SRodney W. Grimes execsigs(p); 43126f9a767SRodney W. Grimes 43226f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 43326f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 43426f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 43526f9a767SRodney W. Grimes p->p_comm[len] = 0; 43626f9a767SRodney W. Grimes 43726f9a767SRodney W. Grimes /* 43824b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 439dc733423SDag-Erling Smørgrav * it that it now has its own resources back 44026f9a767SRodney W. Grimes */ 44126f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 44226f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 44326f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4447f05b035SAlfred Perlstein wakeup(p->p_pptr); 44526f9a767SRodney W. Grimes } 44626f9a767SRodney W. Grimes 44726f9a767SRodney W. Grimes /* 448a78e8d2aSDavid Greenman * Implement image setuid/setgid. 449a78e8d2aSDavid Greenman * 450a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 451a78e8d2aSDavid Greenman * the process is being traced. 452670cb89bSRobert Watson * 453670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 454670cb89bSRobert Watson * transitions on the file system. 455c52007c2SDavid Greenman */ 456b1fc0ec1SRobert Watson oldcred = p->p_ucred; 457d06c0d4dSRobert Watson credential_changing = 0; 458d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 459d06c0d4dSRobert Watson attr.va_uid; 460d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 461d06c0d4dSRobert Watson attr.va_gid; 462ccafe7ebSRobert Watson #ifdef MAC 463670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 464670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 465ccafe7ebSRobert Watson credential_changing |= will_transition; 466ccafe7ebSRobert Watson #endif 467d06c0d4dSRobert Watson 468d06c0d4dSRobert Watson if (credential_changing && 469a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 470c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 471c52007c2SDavid Greenman /* 472c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 473b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 474b85db196SPeter Wemm * we do not regain any tracing during a possible block. 47526f9a767SRodney W. Grimes */ 476b85db196SPeter Wemm setsugid(p); 4776c84de02SJohn Baldwin #ifdef KTRACE 47844731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 4796c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 48079deba82SMatthew Dillon p->p_traceflag = 0; 4819b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 4829b3b1c5fSJohn Baldwin p->p_tracep = NULL; 4836c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 48426f9a767SRodney W. Grimes } 4856c84de02SJohn Baldwin #endif 48628b325aaSDon Lewis /* 487c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 488c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 48928b325aaSDon Lewis * 490c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 491c1e2d386SNate Lawson * which may grab the process lock. 49228b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 49328b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 49428b325aaSDon Lewis */ 49528b325aaSDon Lewis PROC_UNLOCK(p); 496c1e2d386SNate Lawson setugidsafety(td); 497e983a376SJacques Vidrine error = fdcheckstd(td); 49863c9e754SJohn Baldwin if (error != 0) 49969be5db9SAlfred Perlstein goto done1; 500e1b1aa3bSJohn Baldwin PROC_LOCK(p); 501c52007c2SDavid Greenman /* 502c52007c2SDavid Greenman * Set the new credentials. 503c52007c2SDavid Greenman */ 5049b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 505c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5061419eacbSAlfred Perlstein change_euid(newcred, euip); 507c52007c2SDavid Greenman if (attr.va_mode & VSGID) 508b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 509ccafe7ebSRobert Watson #ifdef MAC 510670cb89bSRobert Watson if (will_transition) { 511670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 512670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 513670cb89bSRobert Watson } 514ccafe7ebSRobert Watson #endif 5159b3b1c5fSJohn Baldwin /* 5169b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 517ccafe7ebSRobert Watson * 518ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 519ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 520ccafe7ebSRobert Watson * though maybe it shouldn't. 5219b3b1c5fSJohn Baldwin */ 5229b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 5239b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 5249b3b1c5fSJohn Baldwin p->p_ucred = newcred; 5259b3b1c5fSJohn Baldwin newcred = NULL; 526c52007c2SDavid Greenman } else { 527b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 528b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 529c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 53026f9a767SRodney W. Grimes /* 531c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 532b1fc0ec1SRobert Watson * 533b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 5349b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 5359b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 5369b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 5379b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5389b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5399b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5409b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 54126f9a767SRodney W. Grimes */ 542b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 543b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5449b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 545b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 546b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 547b1fc0ec1SRobert Watson p->p_ucred = newcred; 5489b3b1c5fSJohn Baldwin newcred = NULL; 5499b3b1c5fSJohn Baldwin } 550b1fc0ec1SRobert Watson } 55126f9a767SRodney W. Grimes 55226f9a767SRodney W. Grimes /* 553619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 554619eb6e5SJeff Roberson * to locking the proc lock. 5552a531c80SDavid Greenman */ 5569b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5572a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5582a531c80SDavid Greenman 5592a531c80SDavid Greenman /* 5609ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5619ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 562cb679c38SJonathan Lemon */ 563cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5649ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 565cb679c38SJonathan Lemon 566cb679c38SJonathan Lemon /* 56726f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 56826f9a767SRodney W. Grimes * can be set before the program executes. 56926f9a767SRodney W. Grimes */ 570e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 5712a024a2bSSean Eric Fagan 57226f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 57326f9a767SRodney W. Grimes psignal(p, SIGTRAP); 57426f9a767SRodney W. Grimes 57526f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 57626f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 57726f9a767SRodney W. Grimes 578b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5799b3b1c5fSJohn Baldwin oldargs = p->p_args; 580b9df5231SPoul-Henning Kamp p->p_args = NULL; 581b9df5231SPoul-Henning Kamp 582e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 583e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 584e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 585e1b1aa3bSJohn Baldwin p->p_args = newargs; 586e1b1aa3bSJohn Baldwin newargs = NULL; 587e1b1aa3bSJohn Baldwin } 588e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 589e1b1aa3bSJohn Baldwin 590e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5913ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5923ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5933ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5943ebc1248SPeter Wemm else 595bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 596bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 597e913ca22SDoug Rabson 59869be5db9SAlfred Perlstein done1: 5999b3b1c5fSJohn Baldwin /* 6009b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 6019b3b1c5fSJohn Baldwin */ 6021419eacbSAlfred Perlstein uifree(euip); 6039b3b1c5fSJohn Baldwin if (newcred == NULL) 6049b3b1c5fSJohn Baldwin crfree(oldcred); 6059b3b1c5fSJohn Baldwin else 6069b3b1c5fSJohn Baldwin crfree(newcred); 6079b3b1c5fSJohn Baldwin /* 6089b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 6099b3b1c5fSJohn Baldwin */ 6109b3b1c5fSJohn Baldwin if (textvp != NULL) 6119b3b1c5fSJohn Baldwin vrele(textvp); 612619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 613619eb6e5SJeff Roberson vrele(ndp->ni_vp); 6146c84de02SJohn Baldwin #ifdef KTRACE 6159b3b1c5fSJohn Baldwin if (tracevp != NULL) 6169b3b1c5fSJohn Baldwin vrele(tracevp); 6176c84de02SJohn Baldwin #endif 61869be5db9SAlfred Perlstein if (oldargs != NULL) 6199b3b1c5fSJohn Baldwin pargs_drop(oldargs); 62069be5db9SAlfred Perlstein if (newargs != NULL) 62169be5db9SAlfred Perlstein pargs_drop(newargs); 622b9df5231SPoul-Henning Kamp 6231616db3cSJohn Dyson exec_fail_dealloc: 6241616db3cSJohn Dyson 62526f9a767SRodney W. Grimes /* 62626f9a767SRodney W. Grimes * free various allocated resources 62726f9a767SRodney W. Grimes */ 6281616db3cSJohn Dyson if (imgp->firstpage) 6291616db3cSJohn Dyson exec_unmap_first_page(imgp); 63026f9a767SRodney W. Grimes 631c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 6321616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 6331616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 6341616db3cSJohn Dyson 6359c0fed3dSDoug Rabson if (imgp->vp) { 636762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 637619eb6e5SJeff Roberson vput(imgp->vp); 638a3cf6ebaSDavid Greenman } 63926f9a767SRodney W. Grimes 6400b2ed1aeSJeff Roberson if (imgp->object) 6410b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6420b2ed1aeSJeff Roberson 6431616db3cSJohn Dyson if (error == 0) 644116734c4SMatthew Dillon goto done2; 6451616db3cSJohn Dyson 64626f9a767SRodney W. Grimes exec_fail: 6479ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6489ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6499ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6509ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6519ca45e81SDag-Erling Smørgrav 652c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 65326f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 654670cb89bSRobert Watson #ifdef MAC 655670cb89bSRobert Watson mac_execve_exit(imgp); 656670cb89bSRobert Watson if (interplabelvalid) 657670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 658670cb89bSRobert Watson #endif 659b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 66026f9a767SRodney W. Grimes /* NOT REACHED */ 661116734c4SMatthew Dillon error = 0; 66226f9a767SRodney W. Grimes } 663116734c4SMatthew Dillon done2: 664670cb89bSRobert Watson #ifdef MAC 665670cb89bSRobert Watson mac_execve_exit(imgp); 666670cb89bSRobert Watson if (interplabelvalid) 667670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 668670cb89bSRobert Watson #endif 669116734c4SMatthew Dillon mtx_unlock(&Giant); 670116734c4SMatthew Dillon return (error); 67126f9a767SRodney W. Grimes } 67226f9a767SRodney W. Grimes 673450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 674450ffb44SRobert Watson struct execve_args { 675450ffb44SRobert Watson char *fname; 676450ffb44SRobert Watson char **argv; 677450ffb44SRobert Watson char **envv; 678450ffb44SRobert Watson }; 679450ffb44SRobert Watson #endif 680450ffb44SRobert Watson 681450ffb44SRobert Watson /* 682450ffb44SRobert Watson * MPSAFE 683450ffb44SRobert Watson */ 684450ffb44SRobert Watson int 685450ffb44SRobert Watson execve(td, uap) 686450ffb44SRobert Watson struct thread *td; 687450ffb44SRobert Watson struct execve_args /* { 688450ffb44SRobert Watson syscallarg(char *) fname; 689450ffb44SRobert Watson syscallarg(char **) argv; 690450ffb44SRobert Watson syscallarg(char **) envv; 691450ffb44SRobert Watson } */ *uap; 692450ffb44SRobert Watson { 693450ffb44SRobert Watson 694670cb89bSRobert Watson #ifdef MAC 695670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 696670cb89bSRobert Watson #else 697670cb89bSRobert Watson return (ENOSYS); 698670cb89bSRobert Watson #endif 699670cb89bSRobert Watson } 700670cb89bSRobert Watson 701670cb89bSRobert Watson #ifndef _SYS_SYSPROTO_H_ 702670cb89bSRobert Watson struct __mac_execve_args { 703670cb89bSRobert Watson char *fname; 704670cb89bSRobert Watson char **argv; 705670cb89bSRobert Watson char **envv; 706670cb89bSRobert Watson struct mac *mac_p; 707670cb89bSRobert Watson }; 708670cb89bSRobert Watson #endif 709670cb89bSRobert Watson 710670cb89bSRobert Watson /* 711670cb89bSRobert Watson * MPSAFE 712670cb89bSRobert Watson */ 713670cb89bSRobert Watson int 714670cb89bSRobert Watson __mac_execve(td, uap) 715670cb89bSRobert Watson struct thread *td; 716670cb89bSRobert Watson struct __mac_execve_args /* { 717670cb89bSRobert Watson syscallarg(char *) fname; 718670cb89bSRobert Watson syscallarg(char **) argv; 719670cb89bSRobert Watson syscallarg(char **) envv; 720670cb89bSRobert Watson syscallarg(struct mac *) mac_p; 721670cb89bSRobert Watson } */ *uap; 722670cb89bSRobert Watson { 723670cb89bSRobert Watson 724670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, 725670cb89bSRobert Watson uap->mac_p)); 726450ffb44SRobert Watson } 727450ffb44SRobert Watson 7281616db3cSJohn Dyson int 7291616db3cSJohn Dyson exec_map_first_page(imgp) 7301616db3cSJohn Dyson struct image_params *imgp; 7311616db3cSJohn Dyson { 732d8aad40cSJohn Baldwin int rv, i; 73395461b45SJohn Dyson int initial_pagein; 73495461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7351616db3cSJohn Dyson vm_object_t object; 7361616db3cSJohn Dyson 7370cddd8f0SMatthew Dillon GIANT_REQUIRED; 7381616db3cSJohn Dyson 7391616db3cSJohn Dyson if (imgp->firstpage) { 7401616db3cSJohn Dyson exec_unmap_first_page(imgp); 7411616db3cSJohn Dyson } 7421616db3cSJohn Dyson 7439ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 7441616db3cSJohn Dyson 74595461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 7461616db3cSJohn Dyson 74795461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 74895461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 74995461b45SJohn Dyson if (initial_pagein > object->size) 75095461b45SJohn Dyson initial_pagein = object->size; 75195461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 752d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 75395461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 75495461b45SJohn Dyson break; 75595461b45SJohn Dyson if (ma[i]->valid) 75695461b45SJohn Dyson break; 757e69763a3SDoug Rabson vm_page_busy(ma[i]); 75895461b45SJohn Dyson } else { 759ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 760ca0387efSJake Burkholder VM_ALLOC_NORMAL); 76195461b45SJohn Dyson if (ma[i] == NULL) 76295461b45SJohn Dyson break; 76395461b45SJohn Dyson } 76495461b45SJohn Dyson } 76595461b45SJohn Dyson initial_pagein = i; 7661616db3cSJohn Dyson 76795461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 76895461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 76995461b45SJohn Dyson 770ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 771ca0387efSJake Burkholder (ma[0]->valid == 0)) { 772ecbb00a2SDoug Rabson if (ma[0]) { 77397646f56SAlan Cox vm_page_lock_queues(); 77495461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 7758f9110f6SJohn Dyson vm_page_free(ma[0]); 77697646f56SAlan Cox vm_page_unlock_queues(); 777ecbb00a2SDoug Rabson } 778a7cddfedSJake Burkholder return (EIO); 7791616db3cSJohn Dyson } 7801616db3cSJohn Dyson } 78197646f56SAlan Cox vm_page_lock_queues(); 78295461b45SJohn Dyson vm_page_wire(ma[0]); 783e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 78497646f56SAlan Cox vm_page_unlock_queues(); 7851616db3cSJohn Dyson 786ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 78795461b45SJohn Dyson imgp->firstpage = ma[0]; 7881616db3cSJohn Dyson 789a7cddfedSJake Burkholder return (0); 7901616db3cSJohn Dyson } 7911616db3cSJohn Dyson 7921616db3cSJohn Dyson void 7931616db3cSJohn Dyson exec_unmap_first_page(imgp) 7941616db3cSJohn Dyson struct image_params *imgp; 7951616db3cSJohn Dyson { 7960cddd8f0SMatthew Dillon GIANT_REQUIRED; 79723955314SAlfred Perlstein 7981616db3cSJohn Dyson if (imgp->firstpage) { 799ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 80097646f56SAlan Cox vm_page_lock_queues(); 80173007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 80297646f56SAlan Cox vm_page_unlock_queues(); 8031616db3cSJohn Dyson imgp->firstpage = NULL; 8041616db3cSJohn Dyson } 8051616db3cSJohn Dyson } 8061616db3cSJohn Dyson 80726f9a767SRodney W. Grimes /* 80826f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 80926f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 81026f9a767SRodney W. Grimes * automatically in trap.c. 81126f9a767SRodney W. Grimes */ 81226f9a767SRodney W. Grimes int 81305ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 814c52007c2SDavid Greenman struct image_params *imgp; 81505ba50f5SJake Burkholder struct sysentvec *sv; 81626f9a767SRodney W. Grimes { 81726f9a767SRodney W. Grimes int error; 8186f5dafeaSAlan Cox struct execlist *ep; 8195e20c11fSAlan Cox struct proc *p = imgp->proc; 8205e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 82105ba50f5SJake Burkholder vm_offset_t stack_addr; 82205ba50f5SJake Burkholder vm_map_t map; 82326f9a767SRodney W. Grimes 8240cddd8f0SMatthew Dillon GIANT_REQUIRED; 8250cddd8f0SMatthew Dillon 82605ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 8273ebc1248SPeter Wemm 828c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 82926f9a767SRodney W. Grimes 830af9ec885SJohn Dyson /* 8316f5dafeaSAlan Cox * Perform functions registered with at_exec(). 8326f5dafeaSAlan Cox */ 8336f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 8345e20c11fSAlan Cox (*ep->function)(p); 8356f5dafeaSAlan Cox 8366f5dafeaSAlan Cox /* 837af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 838af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 839af9ec885SJohn Dyson * not disrupted 840af9ec885SJohn Dyson */ 84105ba50f5SJake Burkholder map = &vmspace->vm_map; 84205ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 84305ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 8443d903220SDoug Rabson if (vmspace->vm_shm) 8455e20c11fSAlan Cox shmexit(p); 84605ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 84705ba50f5SJake Burkholder vm_map_max(map)); 84805ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 849af9ec885SJohn Dyson } else { 85005ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 8515e20c11fSAlan Cox vmspace = p->p_vmspace; 85205ba50f5SJake Burkholder map = &vmspace->vm_map; 853af9ec885SJohn Dyson } 85426f9a767SRodney W. Grimes 85526f9a767SRodney W. Grimes /* Allocate a new stack */ 85605ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 85705ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 8582267af78SJulian Elischer if (error) 8592267af78SJulian Elischer return (error); 8602267af78SJulian Elischer 86163c47a5cSDoug Rabson #ifdef __ia64__ 86263c47a5cSDoug Rabson { 86363c47a5cSDoug Rabson /* 86463c47a5cSDoug Rabson * Allocate backing store. We really need something 86563c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 86663c47a5cSDoug Rabson * store to grow upwards. This will do for now. 86763c47a5cSDoug Rabson */ 86863c47a5cSDoug Rabson vm_offset_t bsaddr; 86905ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 87005ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 87181f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 8725e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 87363c47a5cSDoug Rabson } 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 1088339b79b9SRobert Watson #ifdef MAC 1089670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1090339b79b9SRobert Watson if (error) 1091339b79b9SRobert Watson return (error); 1092339b79b9SRobert Watson #endif 1093339b79b9SRobert Watson 109426f9a767SRodney W. Grimes /* Get file attributes */ 1095a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 109626f9a767SRodney W. Grimes if (error) 109726f9a767SRodney W. Grimes return (error); 109826f9a767SRodney W. Grimes 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 */ 1136a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 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++; 1153aa855a59SPeter Wemm 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++; 1187aa855a59SPeter Wemm 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 } 120021d56e9cSAlfred Perlstein 120121d56e9cSAlfred Perlstein int 120221d56e9cSAlfred Perlstein at_exec(function) 120321d56e9cSAlfred Perlstein execlist_fn function; 120421d56e9cSAlfred Perlstein { 120521d56e9cSAlfred Perlstein struct execlist *ep; 120621d56e9cSAlfred Perlstein 120721d56e9cSAlfred Perlstein #ifdef INVARIANTS 120821d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 120921d56e9cSAlfred Perlstein if (rm_at_exec(function)) 121021d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 121121d56e9cSAlfred Perlstein function); 121221d56e9cSAlfred Perlstein #endif 121321d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 121421d56e9cSAlfred Perlstein if (ep == NULL) 121521d56e9cSAlfred Perlstein return (ENOMEM); 121621d56e9cSAlfred Perlstein ep->function = function; 121721d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 121821d56e9cSAlfred Perlstein return (0); 121921d56e9cSAlfred Perlstein } 122021d56e9cSAlfred Perlstein 122121d56e9cSAlfred Perlstein /* 122221d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 122321d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 122421d56e9cSAlfred Perlstein */ 122521d56e9cSAlfred Perlstein int 122621d56e9cSAlfred Perlstein rm_at_exec(function) 122721d56e9cSAlfred Perlstein execlist_fn function; 122821d56e9cSAlfred Perlstein { 122921d56e9cSAlfred Perlstein struct execlist *ep; 123021d56e9cSAlfred Perlstein 123121d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 123221d56e9cSAlfred Perlstein if (ep->function == function) { 123321d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 123421d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 123521d56e9cSAlfred Perlstein return (1); 123621d56e9cSAlfred Perlstein } 123721d56e9cSAlfred Perlstein } 123821d56e9cSAlfred Perlstein return (0); 123921d56e9cSAlfred Perlstein } 1240