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 */ 57026f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 57126f9a767SRodney W. Grimes psignal(p, SIGTRAP); 57226f9a767SRodney W. Grimes 57326f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 57426f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 57526f9a767SRodney W. Grimes 576b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5779b3b1c5fSJohn Baldwin oldargs = p->p_args; 578b9df5231SPoul-Henning Kamp p->p_args = NULL; 579b9df5231SPoul-Henning Kamp 580e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 581e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 582e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 583e1b1aa3bSJohn Baldwin p->p_args = newargs; 584e1b1aa3bSJohn Baldwin newargs = NULL; 585e1b1aa3bSJohn Baldwin } 586e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 587e1b1aa3bSJohn Baldwin 588e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5893ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5903ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5913ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5923ebc1248SPeter Wemm else 593bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 594bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 595e913ca22SDoug Rabson 59669be5db9SAlfred Perlstein done1: 5979b3b1c5fSJohn Baldwin /* 5989b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5999b3b1c5fSJohn Baldwin */ 6001419eacbSAlfred Perlstein uifree(euip); 6019b3b1c5fSJohn Baldwin if (newcred == NULL) 6029b3b1c5fSJohn Baldwin crfree(oldcred); 6039b3b1c5fSJohn Baldwin else 6049b3b1c5fSJohn Baldwin crfree(newcred); 6059b3b1c5fSJohn Baldwin /* 6069b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 6079b3b1c5fSJohn Baldwin */ 6089b3b1c5fSJohn Baldwin if (textvp != NULL) 6099b3b1c5fSJohn Baldwin vrele(textvp); 610619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 611619eb6e5SJeff Roberson vrele(ndp->ni_vp); 6126c84de02SJohn Baldwin #ifdef KTRACE 6139b3b1c5fSJohn Baldwin if (tracevp != NULL) 6149b3b1c5fSJohn Baldwin vrele(tracevp); 6156c84de02SJohn Baldwin #endif 61669be5db9SAlfred Perlstein if (oldargs != NULL) 6179b3b1c5fSJohn Baldwin pargs_drop(oldargs); 61869be5db9SAlfred Perlstein if (newargs != NULL) 61969be5db9SAlfred Perlstein pargs_drop(newargs); 620b9df5231SPoul-Henning Kamp 6211616db3cSJohn Dyson exec_fail_dealloc: 6221616db3cSJohn Dyson 62326f9a767SRodney W. Grimes /* 62426f9a767SRodney W. Grimes * free various allocated resources 62526f9a767SRodney W. Grimes */ 6261616db3cSJohn Dyson if (imgp->firstpage) 6271616db3cSJohn Dyson exec_unmap_first_page(imgp); 62826f9a767SRodney W. Grimes 6299c0fed3dSDoug Rabson if (imgp->vp) { 630762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 631619eb6e5SJeff Roberson vput(imgp->vp); 632a3cf6ebaSDavid Greenman } 63326f9a767SRodney W. Grimes 634a9a08882SJeff Roberson if (imgp->stringbase != NULL) 635a9a08882SJeff Roberson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 636a9a08882SJeff Roberson ARG_MAX + PAGE_SIZE); 637a9a08882SJeff Roberson 6380b2ed1aeSJeff Roberson if (imgp->object) 6390b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6400b2ed1aeSJeff Roberson 641d1989db5SRobert Drehmel if (error == 0) { 642d1989db5SRobert Drehmel /* 643d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 644d1989db5SRobert Drehmel * the S_EXEC bit set. 645d1989db5SRobert Drehmel */ 646d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 647116734c4SMatthew Dillon goto done2; 648d1989db5SRobert Drehmel } 6491616db3cSJohn Dyson 65026f9a767SRodney W. Grimes exec_fail: 6519ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6529ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6539ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6549ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6559ca45e81SDag-Erling Smørgrav 656c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 65726f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 658670cb89bSRobert Watson #ifdef MAC 659670cb89bSRobert Watson mac_execve_exit(imgp); 660670cb89bSRobert Watson if (interplabelvalid) 661670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 662670cb89bSRobert Watson #endif 663b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 66426f9a767SRodney W. Grimes /* NOT REACHED */ 665116734c4SMatthew Dillon error = 0; 66626f9a767SRodney W. Grimes } 667116734c4SMatthew Dillon done2: 668670cb89bSRobert Watson #ifdef MAC 669670cb89bSRobert Watson mac_execve_exit(imgp); 670670cb89bSRobert Watson if (interplabelvalid) 671670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 672670cb89bSRobert Watson #endif 673116734c4SMatthew Dillon mtx_unlock(&Giant); 674116734c4SMatthew Dillon return (error); 67526f9a767SRodney W. Grimes } 67626f9a767SRodney W. Grimes 677450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 678450ffb44SRobert Watson struct execve_args { 679450ffb44SRobert Watson char *fname; 680450ffb44SRobert Watson char **argv; 681450ffb44SRobert Watson char **envv; 682450ffb44SRobert Watson }; 683450ffb44SRobert Watson #endif 684450ffb44SRobert Watson 685450ffb44SRobert Watson /* 686450ffb44SRobert Watson * MPSAFE 687450ffb44SRobert Watson */ 688450ffb44SRobert Watson int 689450ffb44SRobert Watson execve(td, uap) 690450ffb44SRobert Watson struct thread *td; 691450ffb44SRobert Watson struct execve_args /* { 692450ffb44SRobert Watson syscallarg(char *) fname; 693450ffb44SRobert Watson syscallarg(char **) argv; 694450ffb44SRobert Watson syscallarg(char **) envv; 695450ffb44SRobert Watson } */ *uap; 696450ffb44SRobert Watson { 697450ffb44SRobert Watson 698670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 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 7240c93266bSRobert Watson #ifdef MAC 725670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, 726670cb89bSRobert Watson uap->mac_p)); 7270c93266bSRobert Watson #else 7280c93266bSRobert Watson return (ENOSYS); 7290c93266bSRobert Watson #endif 730450ffb44SRobert Watson } 731450ffb44SRobert Watson 7321616db3cSJohn Dyson int 7331616db3cSJohn Dyson exec_map_first_page(imgp) 7341616db3cSJohn Dyson struct image_params *imgp; 7351616db3cSJohn Dyson { 736d8aad40cSJohn Baldwin int rv, i; 73795461b45SJohn Dyson int initial_pagein; 73895461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7391616db3cSJohn Dyson vm_object_t object; 7401616db3cSJohn Dyson 7410cddd8f0SMatthew Dillon GIANT_REQUIRED; 7421616db3cSJohn Dyson 7431616db3cSJohn Dyson if (imgp->firstpage) { 7441616db3cSJohn Dyson exec_unmap_first_page(imgp); 7451616db3cSJohn Dyson } 7461616db3cSJohn Dyson 7479ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 7481616db3cSJohn Dyson 74995461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 7501616db3cSJohn Dyson 75195461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 75295461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 75395461b45SJohn Dyson if (initial_pagein > object->size) 75495461b45SJohn Dyson initial_pagein = object->size; 75595461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 756d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 75795461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 75895461b45SJohn Dyson break; 75995461b45SJohn Dyson if (ma[i]->valid) 76095461b45SJohn Dyson break; 761e69763a3SDoug Rabson vm_page_busy(ma[i]); 76295461b45SJohn Dyson } else { 763ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 764ca0387efSJake Burkholder VM_ALLOC_NORMAL); 76595461b45SJohn Dyson if (ma[i] == NULL) 76695461b45SJohn Dyson break; 76795461b45SJohn Dyson } 76895461b45SJohn Dyson } 76995461b45SJohn Dyson initial_pagein = i; 7701616db3cSJohn Dyson 77195461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 77295461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 77395461b45SJohn Dyson 774ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 775ca0387efSJake Burkholder (ma[0]->valid == 0)) { 776ecbb00a2SDoug Rabson if (ma[0]) { 77797646f56SAlan Cox vm_page_lock_queues(); 7784fec79beSAlan Cox pmap_remove_all(ma[0]); 7798f9110f6SJohn Dyson vm_page_free(ma[0]); 78097646f56SAlan Cox vm_page_unlock_queues(); 781ecbb00a2SDoug Rabson } 782a7cddfedSJake Burkholder return (EIO); 7831616db3cSJohn Dyson } 7841616db3cSJohn Dyson } 78597646f56SAlan Cox vm_page_lock_queues(); 78695461b45SJohn Dyson vm_page_wire(ma[0]); 787e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 78897646f56SAlan Cox vm_page_unlock_queues(); 7891616db3cSJohn Dyson 790ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 79195461b45SJohn Dyson imgp->firstpage = ma[0]; 7921616db3cSJohn Dyson 793a7cddfedSJake Burkholder return (0); 7941616db3cSJohn Dyson } 7951616db3cSJohn Dyson 7961616db3cSJohn Dyson void 7971616db3cSJohn Dyson exec_unmap_first_page(imgp) 7981616db3cSJohn Dyson struct image_params *imgp; 7991616db3cSJohn Dyson { 8000cddd8f0SMatthew Dillon GIANT_REQUIRED; 80123955314SAlfred Perlstein 8021616db3cSJohn Dyson if (imgp->firstpage) { 803ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 80497646f56SAlan Cox vm_page_lock_queues(); 80573007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 80697646f56SAlan Cox vm_page_unlock_queues(); 8071616db3cSJohn Dyson imgp->firstpage = NULL; 8081616db3cSJohn Dyson } 8091616db3cSJohn Dyson } 8101616db3cSJohn Dyson 81126f9a767SRodney W. Grimes /* 81226f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 81326f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 81426f9a767SRodney W. Grimes * automatically in trap.c. 81526f9a767SRodney W. Grimes */ 81626f9a767SRodney W. Grimes int 81705ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 818c52007c2SDavid Greenman struct image_params *imgp; 81905ba50f5SJake Burkholder struct sysentvec *sv; 82026f9a767SRodney W. Grimes { 82126f9a767SRodney W. Grimes int error; 8226f5dafeaSAlan Cox struct execlist *ep; 8235e20c11fSAlan Cox struct proc *p = imgp->proc; 8245e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 82505ba50f5SJake Burkholder vm_offset_t stack_addr; 82605ba50f5SJake Burkholder vm_map_t map; 82726f9a767SRodney W. Grimes 8280cddd8f0SMatthew Dillon GIANT_REQUIRED; 8290cddd8f0SMatthew Dillon 83005ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 8313ebc1248SPeter Wemm 832c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 83326f9a767SRodney W. Grimes 834af9ec885SJohn Dyson /* 8356f5dafeaSAlan Cox * Perform functions registered with at_exec(). 8366f5dafeaSAlan Cox */ 8376f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 8385e20c11fSAlan Cox (*ep->function)(p); 8396f5dafeaSAlan Cox 8406f5dafeaSAlan Cox /* 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) { 8483d903220SDoug Rabson if (vmspace->vm_shm) 8495e20c11fSAlan Cox shmexit(p); 8502d21129dSAlan Cox vm_page_lock_queues(); 85105ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 85205ba50f5SJake Burkholder vm_map_max(map)); 8532d21129dSAlan Cox vm_page_unlock_queues(); 85405ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 855af9ec885SJohn Dyson } else { 85605ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 8575e20c11fSAlan Cox vmspace = p->p_vmspace; 85805ba50f5SJake Burkholder map = &vmspace->vm_map; 859af9ec885SJohn Dyson } 86026f9a767SRodney W. Grimes 86126f9a767SRodney W. Grimes /* Allocate a new stack */ 86205ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 86305ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 8642267af78SJulian Elischer if (error) 8652267af78SJulian Elischer return (error); 8662267af78SJulian Elischer 86763c47a5cSDoug Rabson #ifdef __ia64__ 86863c47a5cSDoug Rabson { 86963c47a5cSDoug Rabson /* 87063c47a5cSDoug Rabson * Allocate backing store. We really need something 87163c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 87263c47a5cSDoug Rabson * store to grow upwards. This will do for now. 87363c47a5cSDoug Rabson */ 87463c47a5cSDoug Rabson vm_offset_t bsaddr; 87505ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 87605ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 87781f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 8785e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 87963c47a5cSDoug Rabson } 88063c47a5cSDoug Rabson #endif 88163c47a5cSDoug Rabson 8822267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 8832267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 8842267af78SJulian Elischer * process stack so we can check the stack rlimit. 8852267af78SJulian Elischer */ 886cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 88705ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 88826f9a767SRodney W. Grimes 88926f9a767SRodney W. Grimes return (0); 89026f9a767SRodney W. Grimes } 89126f9a767SRodney W. Grimes 89226f9a767SRodney W. Grimes /* 89326f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 89426f9a767SRodney W. Grimes * address space into the temporary string buffer. 89526f9a767SRodney W. Grimes */ 89626f9a767SRodney W. Grimes int 897c52007c2SDavid Greenman exec_extract_strings(imgp) 898c52007c2SDavid Greenman struct image_params *imgp; 89926f9a767SRodney W. Grimes { 90026f9a767SRodney W. Grimes char **argv, **envv; 90126f9a767SRodney W. Grimes char *argp, *envp; 902ecbb00a2SDoug Rabson int error; 903ecbb00a2SDoug Rabson size_t length; 90426f9a767SRodney W. Grimes 90526f9a767SRodney W. Grimes /* 90626f9a767SRodney W. Grimes * extract arguments first 90726f9a767SRodney W. Grimes */ 90826f9a767SRodney W. Grimes 909450ffb44SRobert Watson argv = imgp->userspace_argv; 91026f9a767SRodney W. Grimes 9110fd1a014SDavid Greenman if (argv) { 912aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 9135cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 9145cf3d12cSAndrey A. Chernov return (EFAULT); 9155cf3d12cSAndrey A. Chernov if (argp) 9165cf3d12cSAndrey A. Chernov argv++; 9175cf3d12cSAndrey A. Chernov if (imgp->argv0) 9185cf3d12cSAndrey A. Chernov argp = imgp->argv0; 9195cf3d12cSAndrey A. Chernov if (argp) { 9205cf3d12cSAndrey A. Chernov do { 92126f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 92226f9a767SRodney W. Grimes return (EFAULT); 923c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 924c52007c2SDavid Greenman imgp->stringspace, &length))) { 9250fd1a014SDavid Greenman if (error == ENAMETOOLONG) 92626f9a767SRodney W. Grimes return (E2BIG); 9270fd1a014SDavid Greenman return (error); 9280fd1a014SDavid Greenman } 929c52007c2SDavid Greenman imgp->stringspace -= length; 930c52007c2SDavid Greenman imgp->stringp += length; 931c52007c2SDavid Greenman imgp->argc++; 932aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 93326f9a767SRodney W. Grimes } 9340fd1a014SDavid Greenman } 93526f9a767SRodney W. Grimes 936b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 937b9df5231SPoul-Henning Kamp 93826f9a767SRodney W. Grimes /* 93926f9a767SRodney W. Grimes * extract environment strings 94026f9a767SRodney W. Grimes */ 94126f9a767SRodney W. Grimes 942450ffb44SRobert Watson envv = imgp->userspace_envv; 94326f9a767SRodney W. Grimes 9440fd1a014SDavid Greenman if (envv) { 945aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 94626f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 94726f9a767SRodney W. Grimes return (EFAULT); 948c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 949c52007c2SDavid Greenman imgp->stringspace, &length))) { 9500fd1a014SDavid Greenman if (error == ENAMETOOLONG) 95126f9a767SRodney W. Grimes return (E2BIG); 9520fd1a014SDavid Greenman return (error); 9530fd1a014SDavid Greenman } 954c52007c2SDavid Greenman imgp->stringspace -= length; 955c52007c2SDavid Greenman imgp->stringp += length; 956c52007c2SDavid Greenman imgp->envc++; 95726f9a767SRodney W. Grimes } 9580fd1a014SDavid Greenman } 95926f9a767SRodney W. Grimes 96026f9a767SRodney W. Grimes return (0); 96126f9a767SRodney W. Grimes } 96226f9a767SRodney W. Grimes 96326f9a767SRodney W. Grimes /* 96426f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 96526f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 96626f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 96726f9a767SRodney W. Grimes */ 968654f6be1SBruce Evans register_t * 969c52007c2SDavid Greenman exec_copyout_strings(imgp) 970c52007c2SDavid Greenman struct image_params *imgp; 97126f9a767SRodney W. Grimes { 97226f9a767SRodney W. Grimes int argc, envc; 97326f9a767SRodney W. Grimes char **vectp; 97426f9a767SRodney W. Grimes char *stringp, *destp; 975654f6be1SBruce Evans register_t *stack_base; 97693f6448cSDavid Greenman struct ps_strings *arginfo; 977f3bec5d7SJake Burkholder struct proc *p; 978d66a5066SPeter Wemm int szsigcode; 97926f9a767SRodney W. Grimes 98026f9a767SRodney W. Grimes /* 98126f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 982d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 98326f9a767SRodney W. Grimes */ 984f3bec5d7SJake Burkholder p = imgp->proc; 985f3bec5d7SJake Burkholder szsigcode = 0; 98605ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 987f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 988f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 989d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 9901ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 9911ed012f9SPeter Wemm 99226f9a767SRodney W. Grimes /* 993d66a5066SPeter Wemm * install sigcode 994d66a5066SPeter Wemm */ 995d66a5066SPeter Wemm if (szsigcode) 996f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 997f3bec5d7SJake Burkholder szsigcode), szsigcode); 998d66a5066SPeter Wemm 999d66a5066SPeter Wemm /* 1000e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1001e1743d02SSøren Schmidt * on the stack. 1002e1743d02SSøren Schmidt */ 1003b9a22da4STakanori Watanabe if (imgp->auxargs) { 1004e1743d02SSøren Schmidt /* 1005b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1006b9a22da4STakanori Watanabe * lower compatibility. 1007b9a22da4STakanori Watanabe */ 1008ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1009ca0387efSJake Burkholder (AT_COUNT * 2); 1010b9a22da4STakanori Watanabe /* 1011b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1012b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1013b9a22da4STakanori Watanabe * for argument of Runtime loader. 1014e1743d02SSøren Schmidt */ 1015e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1016b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 1017b9a22da4STakanori Watanabe 1018b9a22da4STakanori Watanabe } else 1019e1743d02SSøren Schmidt /* 1020b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1021b9a22da4STakanori Watanabe * the arg and env vector sets 102226f9a767SRodney W. Grimes */ 1023ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1024ca0387efSJake Burkholder sizeof(char *)); 102526f9a767SRodney W. Grimes 102626f9a767SRodney W. Grimes /* 102726f9a767SRodney W. Grimes * vectp also becomes our initial stack base 102826f9a767SRodney W. Grimes */ 1029654f6be1SBruce Evans stack_base = (register_t *)vectp; 103026f9a767SRodney W. Grimes 1031c52007c2SDavid Greenman stringp = imgp->stringbase; 1032c52007c2SDavid Greenman argc = imgp->argc; 1033c52007c2SDavid Greenman envc = imgp->envc; 103426f9a767SRodney W. Grimes 103593f6448cSDavid Greenman /* 10363aed948bSDavid Greenman * Copy out strings - arguments and environment. 103793f6448cSDavid Greenman */ 1038c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 103993f6448cSDavid Greenman 104093f6448cSDavid Greenman /* 10413aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 10423aed948bSDavid Greenman */ 1043aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 10443aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 10453aed948bSDavid Greenman 10463aed948bSDavid Greenman /* 10473aed948bSDavid Greenman * Fill in argument portion of vector table. 104893f6448cSDavid Greenman */ 104926f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1050aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10513aed948bSDavid Greenman while (*stringp++ != 0) 10523aed948bSDavid Greenman destp++; 10533aed948bSDavid Greenman destp++; 105426f9a767SRodney W. Grimes } 105526f9a767SRodney W. Grimes 10561a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 10576ab46d52SBruce Evans suword(vectp++, 0); 105826f9a767SRodney W. Grimes 1059aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 10603aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 106193f6448cSDavid Greenman 106293f6448cSDavid Greenman /* 10633aed948bSDavid Greenman * Fill in environment portion of vector table. 106493f6448cSDavid Greenman */ 106526f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1066aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10673aed948bSDavid Greenman while (*stringp++ != 0) 10683aed948bSDavid Greenman destp++; 10693aed948bSDavid Greenman destp++; 107026f9a767SRodney W. Grimes } 107126f9a767SRodney W. Grimes 107226f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 10736ab46d52SBruce Evans suword(vectp, 0); 107426f9a767SRodney W. Grimes 107526f9a767SRodney W. Grimes return (stack_base); 107626f9a767SRodney W. Grimes } 107726f9a767SRodney W. Grimes 107826f9a767SRodney W. Grimes /* 107926f9a767SRodney W. Grimes * Check permissions of file to execute. 1080cf64863aSRobert Watson * Called with imgp->vp locked. 108126f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 108226f9a767SRodney W. Grimes */ 1083c8a79999SPeter Wemm int 1084c52007c2SDavid Greenman exec_check_permissions(imgp) 1085c52007c2SDavid Greenman struct image_params *imgp; 108626f9a767SRodney W. Grimes { 1087c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1088c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1089a854ed98SJohn Baldwin struct thread *td; 109026f9a767SRodney W. Grimes int error; 109126f9a767SRodney W. Grimes 1092a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1093339b79b9SRobert Watson 1094339b79b9SRobert Watson #ifdef MAC 1095670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1096339b79b9SRobert Watson if (error) 1097339b79b9SRobert Watson return (error); 1098339b79b9SRobert Watson #endif 1099339b79b9SRobert Watson 110026f9a767SRodney W. Grimes /* Get file attributes */ 1101a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 110226f9a767SRodney W. Grimes if (error) 110326f9a767SRodney W. Grimes return (error); 110426f9a767SRodney W. Grimes 110526f9a767SRodney W. Grimes /* 110626f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 110726f9a767SRodney W. Grimes * file resides on. 110826f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 110926f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 111026f9a767SRodney W. Grimes * file really is executable. 111126f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 111226f9a767SRodney W. Grimes */ 1113c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 111426f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1115a854ed98SJohn Baldwin (attr->va_type != VREG)) 111626f9a767SRodney W. Grimes return (EACCES); 111726f9a767SRodney W. Grimes 111826f9a767SRodney W. Grimes /* 111926f9a767SRodney W. Grimes * Zero length files can't be exec'd 112026f9a767SRodney W. Grimes */ 112126f9a767SRodney W. Grimes if (attr->va_size == 0) 112226f9a767SRodney W. Grimes return (ENOEXEC); 112326f9a767SRodney W. Grimes 112426f9a767SRodney W. Grimes /* 112526f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 112626f9a767SRodney W. Grimes */ 1127a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 112826f9a767SRodney W. Grimes if (error) 112926f9a767SRodney W. Grimes return (error); 113026f9a767SRodney W. Grimes 11316d5a0a8cSDavid Greenman /* 11326d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 11336d5a0a8cSDavid Greenman * if there are any. 11346d5a0a8cSDavid Greenman */ 11356d5a0a8cSDavid Greenman if (vp->v_writecount) 11366d5a0a8cSDavid Greenman return (ETXTBSY); 11376d5a0a8cSDavid Greenman 11386d5a0a8cSDavid Greenman /* 11396d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 11406d5a0a8cSDavid Greenman * general case). 11416d5a0a8cSDavid Greenman */ 1142a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 114326f9a767SRodney W. Grimes return (error); 1144df8bae1dSRodney W. Grimes } 1145aa855a59SPeter Wemm 1146aa855a59SPeter Wemm /* 1147aa855a59SPeter Wemm * Exec handler registration 1148aa855a59SPeter Wemm */ 1149aa855a59SPeter Wemm int 1150aa855a59SPeter Wemm exec_register(execsw_arg) 1151aa855a59SPeter Wemm const struct execsw *execsw_arg; 1152aa855a59SPeter Wemm { 1153aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1154aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1155aa855a59SPeter Wemm 1156aa855a59SPeter Wemm if (execsw) 1157aa855a59SPeter Wemm for (es = execsw; *es; es++) 1158aa855a59SPeter Wemm count++; 1159aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1160aa855a59SPeter Wemm if (newexecsw == NULL) 1161a7cddfedSJake Burkholder return (ENOMEM); 1162aa855a59SPeter Wemm xs = newexecsw; 1163aa855a59SPeter Wemm if (execsw) 1164aa855a59SPeter Wemm for (es = execsw; *es; es++) 1165aa855a59SPeter Wemm *xs++ = *es; 1166aa855a59SPeter Wemm *xs++ = execsw_arg; 1167aa855a59SPeter Wemm *xs = NULL; 1168aa855a59SPeter Wemm if (execsw) 1169aa855a59SPeter Wemm free(execsw, M_TEMP); 1170aa855a59SPeter Wemm execsw = newexecsw; 1171a7cddfedSJake Burkholder return (0); 1172aa855a59SPeter Wemm } 1173aa855a59SPeter Wemm 1174aa855a59SPeter Wemm int 1175aa855a59SPeter Wemm exec_unregister(execsw_arg) 1176aa855a59SPeter Wemm const struct execsw *execsw_arg; 1177aa855a59SPeter Wemm { 1178aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1179aa855a59SPeter Wemm int count = 1; 1180aa855a59SPeter Wemm 1181aa855a59SPeter Wemm if (execsw == NULL) 1182aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1183aa855a59SPeter Wemm 1184aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1185aa855a59SPeter Wemm if (*es == execsw_arg) 1186aa855a59SPeter Wemm break; 1187aa855a59SPeter Wemm } 1188aa855a59SPeter Wemm if (*es == NULL) 1189a7cddfedSJake Burkholder return (ENOENT); 1190aa855a59SPeter Wemm for (es = execsw; *es; es++) 1191aa855a59SPeter Wemm if (*es != execsw_arg) 1192aa855a59SPeter Wemm count++; 1193aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1194aa855a59SPeter Wemm if (newexecsw == NULL) 1195a7cddfedSJake Burkholder return (ENOMEM); 1196aa855a59SPeter Wemm xs = newexecsw; 1197aa855a59SPeter Wemm for (es = execsw; *es; es++) 1198aa855a59SPeter Wemm if (*es != execsw_arg) 1199aa855a59SPeter Wemm *xs++ = *es; 1200aa855a59SPeter Wemm *xs = NULL; 1201aa855a59SPeter Wemm if (execsw) 1202aa855a59SPeter Wemm free(execsw, M_TEMP); 1203aa855a59SPeter Wemm execsw = newexecsw; 1204a7cddfedSJake Burkholder return (0); 1205aa855a59SPeter Wemm } 120621d56e9cSAlfred Perlstein 120721d56e9cSAlfred Perlstein int 120821d56e9cSAlfred Perlstein at_exec(function) 120921d56e9cSAlfred Perlstein execlist_fn function; 121021d56e9cSAlfred Perlstein { 121121d56e9cSAlfred Perlstein struct execlist *ep; 121221d56e9cSAlfred Perlstein 121321d56e9cSAlfred Perlstein #ifdef INVARIANTS 121421d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 121521d56e9cSAlfred Perlstein if (rm_at_exec(function)) 121621d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 121721d56e9cSAlfred Perlstein function); 121821d56e9cSAlfred Perlstein #endif 121921d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 122021d56e9cSAlfred Perlstein if (ep == NULL) 122121d56e9cSAlfred Perlstein return (ENOMEM); 122221d56e9cSAlfred Perlstein ep->function = function; 122321d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 122421d56e9cSAlfred Perlstein return (0); 122521d56e9cSAlfred Perlstein } 122621d56e9cSAlfred Perlstein 122721d56e9cSAlfred Perlstein /* 122821d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 122921d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 123021d56e9cSAlfred Perlstein */ 123121d56e9cSAlfred Perlstein int 123221d56e9cSAlfred Perlstein rm_at_exec(function) 123321d56e9cSAlfred Perlstein execlist_fn function; 123421d56e9cSAlfred Perlstein { 123521d56e9cSAlfred Perlstein struct execlist *ep; 123621d56e9cSAlfred Perlstein 123721d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 123821d56e9cSAlfred Perlstein if (ep->function == function) { 123921d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 124021d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 124121d56e9cSAlfred Perlstein return (1); 124221d56e9cSAlfred Perlstein } 124321d56e9cSAlfred Perlstein } 124421d56e9cSAlfred Perlstein return (0); 124521d56e9cSAlfred Perlstein } 1246