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); 795dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 80450ffb44SRobert Watson static int kern_execve(struct thread *td, char *fname, char **argv, 81670cb89bSRobert Watson char **envv, struct mac *mac_p); 8205ba50f5SJake Burkholder 8321d56e9cSAlfred Perlstein /* 8421d56e9cSAlfred Perlstein * callout list for things to do at exec time 8521d56e9cSAlfred Perlstein */ 8621d56e9cSAlfred Perlstein struct execlist { 8721d56e9cSAlfred Perlstein execlist_fn function; 8821d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 8921d56e9cSAlfred Perlstein }; 9021d56e9cSAlfred Perlstein 9121d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 9221d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 9321d56e9cSAlfred Perlstein 949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9505ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9605ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 97486bddb0SDoug Rabson 989701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9905ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 10005ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 10199ac3bc8SPeter Wemm 1025dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 1035dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 1045dadd17bSJake Burkholder 105b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 106c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1079701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 108b9df5231SPoul-Henning Kamp 109a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 110a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 111a8704f89SPoul-Henning Kamp 112911fc923SPeter Wemm #ifdef __ia64__ 113911fc923SPeter Wemm /* XXX HACK */ 114911fc923SPeter Wemm static int regstkpages = 256; 115911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 116911fc923SPeter Wemm #endif 117911fc923SPeter Wemm 11805ba50f5SJake Burkholder static int 11905ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 12005ba50f5SJake Burkholder { 12105ba50f5SJake Burkholder struct proc *p; 12205ba50f5SJake Burkholder 12305ba50f5SJake Burkholder p = curproc; 12405ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 12505ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 12605ba50f5SJake Burkholder } 12705ba50f5SJake Burkholder 12805ba50f5SJake Burkholder static int 12905ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 13005ba50f5SJake Burkholder { 13105ba50f5SJake Burkholder struct proc *p; 13205ba50f5SJake Burkholder 13305ba50f5SJake Burkholder p = curproc; 13405ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 13505ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 13605ba50f5SJake Burkholder } 13705ba50f5SJake Burkholder 1385dadd17bSJake Burkholder static int 1395dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1405dadd17bSJake Burkholder { 1415dadd17bSJake Burkholder struct proc *p; 1425dadd17bSJake Burkholder 1435dadd17bSJake Burkholder p = curproc; 1445dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1455dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1465dadd17bSJake Burkholder } 1475dadd17bSJake Burkholder 14899ac3bc8SPeter Wemm /* 149aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 150aa855a59SPeter Wemm * double pointer here. 151df8bae1dSRodney W. Grimes */ 152aa855a59SPeter Wemm static const struct execsw **execsw; 15326f9a767SRodney W. Grimes 15426f9a767SRodney W. Grimes /* 155450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 156450ffb44SRobert Watson * userspace pointers from the passed thread. 157116734c4SMatthew Dillon * 158116734c4SMatthew Dillon * MPSAFE 15926f9a767SRodney W. Grimes */ 160450ffb44SRobert Watson static int 161670cb89bSRobert Watson kern_execve(td, fname, argv, envv, mac_p) 162b40ce416SJulian Elischer struct thread *td; 163450ffb44SRobert Watson char *fname; 164450ffb44SRobert Watson char **argv; 165450ffb44SRobert Watson char **envv; 166670cb89bSRobert Watson struct mac *mac_p; 167df8bae1dSRodney W. Grimes { 168b40ce416SJulian Elischer struct proc *p = td->td_proc; 16926f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1709b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1711419eacbSAlfred Perlstein struct uidinfo *euip; 172654f6be1SBruce Evans register_t *stack_base; 173bb56ec4aSPoul-Henning Kamp int error, len, i; 174c52007c2SDavid Greenman struct image_params image_params, *imgp; 17526f9a767SRodney W. Grimes struct vattr attr; 1764d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 17769be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1789b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1796c84de02SJohn Baldwin #ifdef KTRACE 1806c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1816c84de02SJohn Baldwin #endif 1826c84de02SJohn Baldwin struct vnode *textvp = NULL; 183d06c0d4dSRobert Watson int credential_changing; 184619eb6e5SJeff Roberson int textset; 185ccafe7ebSRobert Watson #ifdef MAC 186670cb89bSRobert Watson struct label interplabel; /* label of the interpreted vnode */ 187670cb89bSRobert Watson struct label execlabel; /* optional label argument */ 188670cb89bSRobert Watson int will_transition, interplabelvalid = 0; 189ccafe7ebSRobert Watson #endif 19026f9a767SRodney W. Grimes 191c52007c2SDavid Greenman imgp = &image_params; 192df8bae1dSRodney W. Grimes 1939ca45e81SDag-Erling Smørgrav /* 1949ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1959ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1969ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1979ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1989ca45e81SDag-Erling Smørgrav * privileges. 1999ca45e81SDag-Erling Smørgrav */ 2009ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 2019ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 20291f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 20349539972SJulian Elischer if (p->p_flag & P_KSES) { 2041279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 205e602ba25SJulian Elischer PROC_UNLOCK(p); 206e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 207e602ba25SJulian Elischer } 20849539972SJulian Elischer /* 20949539972SJulian Elischer * If we get here all other threads are dead, 21049539972SJulian Elischer * so unset the associated flags and lose KSE mode. 21149539972SJulian Elischer */ 21249539972SJulian Elischer p->p_flag &= ~P_KSES; 21345f603e2SDavid Xu td->td_mailbox = NULL; 21449539972SJulian Elischer thread_single_end(); 21549539972SJulian Elischer } 2169ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2179ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2189ca45e81SDag-Erling Smørgrav 219df8bae1dSRodney W. Grimes /* 220c52007c2SDavid Greenman * Initialize part of the common data 221df8bae1dSRodney W. Grimes */ 222c52007c2SDavid Greenman imgp->proc = p; 223450ffb44SRobert Watson imgp->userspace_argv = argv; 224450ffb44SRobert Watson imgp->userspace_envv = envv; 225670cb89bSRobert Watson imgp->execlabel = NULL; 226c52007c2SDavid Greenman imgp->attr = &attr; 227c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2285cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 229c52007c2SDavid Greenman imgp->entry_addr = 0; 230c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 231c52007c2SDavid Greenman imgp->interpreted = 0; 232c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 233e1743d02SSøren Schmidt imgp->auxargs = NULL; 2341616db3cSJohn Dyson imgp->vp = NULL; 2350b2ed1aeSJeff Roberson imgp->object = NULL; 2361616db3cSJohn Dyson imgp->firstpage = NULL; 2374fe88fe6SJohn Polstra imgp->ps_strings = 0; 238b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 23926f9a767SRodney W. Grimes 240670cb89bSRobert Watson #ifdef MAC 241670cb89bSRobert Watson error = mac_execve_enter(imgp, mac_p, &execlabel); 242670cb89bSRobert Watson if (error) { 243670cb89bSRobert Watson mtx_lock(&Giant); 244670cb89bSRobert Watson goto exec_fail; 245670cb89bSRobert Watson } 246670cb89bSRobert Watson #endif 247670cb89bSRobert Watson 24826f9a767SRodney W. Grimes /* 24926f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 25026f9a767SRodney W. Grimes * environment strings 25126f9a767SRodney W. Grimes */ 252ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 253ca0387efSJake Burkholder PAGE_SIZE); 254c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 25526f9a767SRodney W. Grimes error = ENOMEM; 256b3afd20dSAlan Cox mtx_lock(&Giant); 25726f9a767SRodney W. Grimes goto exec_fail; 25826f9a767SRodney W. Grimes } 259c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 260c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2611616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 26226f9a767SRodney W. Grimes 26326f9a767SRodney W. Grimes /* 26426f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 26526f9a767SRodney W. Grimes * in ni_vp amoung other things. 26626f9a767SRodney W. Grimes */ 26726f9a767SRodney W. Grimes ndp = &nd; 268b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 269450ffb44SRobert Watson UIO_USERSPACE, fname, td); 27026f9a767SRodney W. Grimes 271b3afd20dSAlan Cox mtx_lock(&Giant); 27226f9a767SRodney W. Grimes interpret: 27326f9a767SRodney W. Grimes 27426f9a767SRodney W. Grimes error = namei(ndp); 27526f9a767SRodney W. Grimes if (error) { 2761616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2771616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 27826f9a767SRodney W. Grimes goto exec_fail; 27926f9a767SRodney W. Grimes } 28026f9a767SRodney W. Grimes 281c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 282450ffb44SRobert Watson imgp->fname = fname; 28326f9a767SRodney W. Grimes 28426f9a767SRodney W. Grimes /* 2859022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2869022e4adSDavid Greenman */ 287c52007c2SDavid Greenman error = exec_check_permissions(imgp); 288619eb6e5SJeff Roberson if (error) 28926f9a767SRodney W. Grimes goto exec_fail_dealloc; 290619eb6e5SJeff Roberson 291619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2920b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 29326f9a767SRodney W. Grimes 294619eb6e5SJeff Roberson /* 295619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 296619eb6e5SJeff Roberson * activating it. 297619eb6e5SJeff Roberson * 298619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 299619eb6e5SJeff Roberson * actually an executable image. 300619eb6e5SJeff Roberson */ 301619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 302619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 303619eb6e5SJeff Roberson 3041616db3cSJohn Dyson error = exec_map_first_page(imgp); 3056d5a0a8cSDavid Greenman if (error) 30626f9a767SRodney W. Grimes goto exec_fail_dealloc; 30726f9a767SRodney W. Grimes 30826f9a767SRodney W. Grimes /* 309d323ddf3SMatthew Dillon * If the current process has a special image activator it 310d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 311d323ddf3SMatthew Dillon * scripts differently. 31226f9a767SRodney W. Grimes */ 313d323ddf3SMatthew Dillon error = -1; 314d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 315d323ddf3SMatthew Dillon error = img_first(imgp); 316d323ddf3SMatthew Dillon 317d323ddf3SMatthew Dillon /* 318d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 319d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 320d323ddf3SMatthew Dillon * and an error otherwise. 321d323ddf3SMatthew Dillon */ 322d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 323d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 324d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 325d323ddf3SMatthew Dillon continue; 326d323ddf3SMatthew Dillon } 327c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 328d323ddf3SMatthew Dillon } 329d323ddf3SMatthew Dillon 330d323ddf3SMatthew Dillon if (error) { 331619eb6e5SJeff Roberson if (error == -1) { 332619eb6e5SJeff Roberson if (textset == 0) 333619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 334d323ddf3SMatthew Dillon error = ENOEXEC; 335619eb6e5SJeff Roberson } 33626f9a767SRodney W. Grimes goto exec_fail_dealloc; 337d323ddf3SMatthew Dillon } 338d323ddf3SMatthew Dillon 339d323ddf3SMatthew Dillon /* 340d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 341d323ddf3SMatthew Dillon * activate the interpreter. 342d323ddf3SMatthew Dillon */ 343c52007c2SDavid Greenman if (imgp->interpreted) { 3441616db3cSJohn Dyson exec_unmap_first_page(imgp); 345619eb6e5SJeff Roberson /* 346619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 347619eb6e5SJeff Roberson * period before we determine that something is a script where 348619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 349619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 350619eb6e5SJeff Roberson */ 351619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 352762e6b85SEivind Eklund /* free name buffer and old vnode */ 353762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 354670cb89bSRobert Watson #ifdef MAC 355670cb89bSRobert Watson mac_init_vnode_label(&interplabel); 356670cb89bSRobert Watson mac_copy_vnode_label(&ndp->ni_vp->v_label, &interplabel); 357670cb89bSRobert Watson interplabelvalid = 1; 358670cb89bSRobert Watson #endif 359619eb6e5SJeff Roberson vput(ndp->ni_vp); 3600b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3610b2ed1aeSJeff Roberson imgp->object = NULL; 36226f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 363b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 364b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 36526f9a767SRodney W. Grimes goto interpret; 36626f9a767SRodney W. Grimes } 36726f9a767SRodney W. Grimes 36826f9a767SRodney W. Grimes /* 36926f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 37026f9a767SRodney W. Grimes */ 3713ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3723ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3733ebc1248SPeter Wemm else 374c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 37526f9a767SRodney W. Grimes 37626f9a767SRodney W. Grimes /* 3771e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3781e1e0b44SSøren Schmidt * let it do the stack setup. 3791e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 38026f9a767SRodney W. Grimes */ 3811e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 382c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3831e1e0b44SSøren Schmidt else 384c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 38526f9a767SRodney W. Grimes 386a78e8d2aSDavid Greenman /* 387a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 388a78e8d2aSDavid Greenman * be shared after an exec. 389a78e8d2aSDavid Greenman */ 390426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 391a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 392a78e8d2aSDavid Greenman struct filedesc *tmp; 393a78e8d2aSDavid Greenman 394c522c1bfSAlfred Perlstein tmp = fdcopy(td->td_proc->p_fd); 395426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 396b40ce416SJulian Elischer fdfree(td); 397a78e8d2aSDavid Greenman p->p_fd = tmp; 398426da3bcSAlfred Perlstein } else 399426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 400a78e8d2aSDavid Greenman 401333ea485SGuido van Rooij /* 4029b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4039b3b1c5fSJohn Baldwin */ 4049b3b1c5fSJohn Baldwin newcred = crget(); 4051419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 4069b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 4079b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 4089b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4099b3b1c5fSJohn Baldwin 4109b3b1c5fSJohn Baldwin /* close files on exec */ 4119b3b1c5fSJohn Baldwin fdcloseexec(td); 4129b3b1c5fSJohn Baldwin 413619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 414619eb6e5SJeff Roberson VREF(ndp->ni_vp); 415619eb6e5SJeff Roberson 4169b3b1c5fSJohn Baldwin /* 417333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4188688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 419b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 420333ea485SGuido van Rooij * reset. 421333ea485SGuido van Rooij */ 4229b3b1c5fSJohn Baldwin PROC_LOCK(p); 4239b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 424333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 4259b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 4269b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 427333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 428a163d034SWarner Losh M_SUBPROC, M_WAITOK); 4299b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 4309b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 4319b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 4329b3b1c5fSJohn Baldwin PROC_LOCK(p); 433333ea485SGuido van Rooij p->p_procsig = newprocsig; 434b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 435b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 436333ea485SGuido van Rooij 437b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 438b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 439333ea485SGuido van Rooij } 440fdf4e8b3SWarner Losh /* Stop profiling */ 441fdf4e8b3SWarner Losh stopprofclock(p); 442fdf4e8b3SWarner Losh 44326f9a767SRodney W. Grimes /* reset caught signals */ 44426f9a767SRodney W. Grimes execsigs(p); 44526f9a767SRodney W. Grimes 44626f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 44726f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 44826f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 44926f9a767SRodney W. Grimes p->p_comm[len] = 0; 45026f9a767SRodney W. Grimes 45126f9a767SRodney W. Grimes /* 45224b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 453dc733423SDag-Erling Smørgrav * it that it now has its own resources back 45426f9a767SRodney W. Grimes */ 45526f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 45626f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 45726f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4587f05b035SAlfred Perlstein wakeup(p->p_pptr); 45926f9a767SRodney W. Grimes } 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes /* 462a78e8d2aSDavid Greenman * Implement image setuid/setgid. 463a78e8d2aSDavid Greenman * 464a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 465a78e8d2aSDavid Greenman * the process is being traced. 466670cb89bSRobert Watson * 467670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 468670cb89bSRobert Watson * transitions on the file system. 469c52007c2SDavid Greenman */ 470b1fc0ec1SRobert Watson oldcred = p->p_ucred; 471d06c0d4dSRobert Watson credential_changing = 0; 472d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 473d06c0d4dSRobert Watson attr.va_uid; 474d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 475d06c0d4dSRobert Watson attr.va_gid; 476ccafe7ebSRobert Watson #ifdef MAC 477670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 478670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 479ccafe7ebSRobert Watson credential_changing |= will_transition; 480ccafe7ebSRobert Watson #endif 481d06c0d4dSRobert Watson 482d06c0d4dSRobert Watson if (credential_changing && 483a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 484c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 485c52007c2SDavid Greenman /* 486c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 487b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 488b85db196SPeter Wemm * we do not regain any tracing during a possible block. 48926f9a767SRodney W. Grimes */ 490b85db196SPeter Wemm setsugid(p); 4916c84de02SJohn Baldwin #ifdef KTRACE 49244731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 4936c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 49479deba82SMatthew Dillon p->p_traceflag = 0; 4959b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 4969b3b1c5fSJohn Baldwin p->p_tracep = NULL; 4976c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 49826f9a767SRodney W. Grimes } 4996c84de02SJohn Baldwin #endif 50028b325aaSDon Lewis /* 501c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 502c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 50328b325aaSDon Lewis * 504c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 505c1e2d386SNate Lawson * which may grab the process lock. 50628b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 50728b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 50828b325aaSDon Lewis */ 50928b325aaSDon Lewis PROC_UNLOCK(p); 510c1e2d386SNate Lawson setugidsafety(td); 511e983a376SJacques Vidrine error = fdcheckstd(td); 51263c9e754SJohn Baldwin if (error != 0) 51369be5db9SAlfred Perlstein goto done1; 514e1b1aa3bSJohn Baldwin PROC_LOCK(p); 515c52007c2SDavid Greenman /* 516c52007c2SDavid Greenman * Set the new credentials. 517c52007c2SDavid Greenman */ 5189b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 519c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5201419eacbSAlfred Perlstein change_euid(newcred, euip); 521c52007c2SDavid Greenman if (attr.va_mode & VSGID) 522b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 523ccafe7ebSRobert Watson #ifdef MAC 524670cb89bSRobert Watson if (will_transition) { 525670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 526670cb89bSRobert Watson interplabelvalid ? &interplabel : NULL, imgp); 527670cb89bSRobert Watson } 528ccafe7ebSRobert Watson #endif 5299b3b1c5fSJohn Baldwin /* 5309b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 531ccafe7ebSRobert Watson * 532ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 533ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 534ccafe7ebSRobert Watson * though maybe it shouldn't. 5359b3b1c5fSJohn Baldwin */ 5369b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 5379b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 5389b3b1c5fSJohn Baldwin p->p_ucred = newcred; 5399b3b1c5fSJohn Baldwin newcred = NULL; 540c52007c2SDavid Greenman } else { 541b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 542b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 543c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 54426f9a767SRodney W. Grimes /* 545c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 546b1fc0ec1SRobert Watson * 547b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 5489b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 5499b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 5509b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 5519b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5529b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5539b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5549b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 55526f9a767SRodney W. Grimes */ 556b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 557b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5589b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 559b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 560b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 561b1fc0ec1SRobert Watson p->p_ucred = newcred; 5629b3b1c5fSJohn Baldwin newcred = NULL; 5639b3b1c5fSJohn Baldwin } 564b1fc0ec1SRobert Watson } 56526f9a767SRodney W. Grimes 56626f9a767SRodney W. Grimes /* 567619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 568619eb6e5SJeff Roberson * to locking the proc lock. 5692a531c80SDavid Greenman */ 5709b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5712a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5722a531c80SDavid Greenman 5732a531c80SDavid Greenman /* 5749ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5759ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 576cb679c38SJonathan Lemon */ 577cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5789ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 579cb679c38SJonathan Lemon 580cb679c38SJonathan Lemon /* 58126f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 58226f9a767SRodney W. Grimes * can be set before the program executes. 58326f9a767SRodney W. Grimes */ 58426f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 58526f9a767SRodney W. Grimes psignal(p, SIGTRAP); 58626f9a767SRodney W. Grimes 58726f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 58826f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 58926f9a767SRodney W. Grimes 590b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5919b3b1c5fSJohn Baldwin oldargs = p->p_args; 592b9df5231SPoul-Henning Kamp p->p_args = NULL; 593b9df5231SPoul-Henning Kamp 594e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 595e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 596e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 597e1b1aa3bSJohn Baldwin p->p_args = newargs; 598e1b1aa3bSJohn Baldwin newargs = NULL; 599e1b1aa3bSJohn Baldwin } 600e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 601e1b1aa3bSJohn Baldwin 602e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 6033ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 6043ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 6053ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 6063ebc1248SPeter Wemm else 607bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 608bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 609e913ca22SDoug Rabson 61069be5db9SAlfred Perlstein done1: 6119b3b1c5fSJohn Baldwin /* 6129b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 6139b3b1c5fSJohn Baldwin */ 6141419eacbSAlfred Perlstein uifree(euip); 6159b3b1c5fSJohn Baldwin if (newcred == NULL) 6169b3b1c5fSJohn Baldwin crfree(oldcred); 6179b3b1c5fSJohn Baldwin else 6189b3b1c5fSJohn Baldwin crfree(newcred); 6199b3b1c5fSJohn Baldwin /* 6209b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 6219b3b1c5fSJohn Baldwin */ 6229b3b1c5fSJohn Baldwin if (textvp != NULL) 6239b3b1c5fSJohn Baldwin vrele(textvp); 624619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 625619eb6e5SJeff Roberson vrele(ndp->ni_vp); 6266c84de02SJohn Baldwin #ifdef KTRACE 6279b3b1c5fSJohn Baldwin if (tracevp != NULL) 6289b3b1c5fSJohn Baldwin vrele(tracevp); 6296c84de02SJohn Baldwin #endif 63069be5db9SAlfred Perlstein if (oldargs != NULL) 6319b3b1c5fSJohn Baldwin pargs_drop(oldargs); 63269be5db9SAlfred Perlstein if (newargs != NULL) 63369be5db9SAlfred Perlstein pargs_drop(newargs); 634b9df5231SPoul-Henning Kamp 6351616db3cSJohn Dyson exec_fail_dealloc: 6361616db3cSJohn Dyson 63726f9a767SRodney W. Grimes /* 63826f9a767SRodney W. Grimes * free various allocated resources 63926f9a767SRodney W. Grimes */ 6401616db3cSJohn Dyson if (imgp->firstpage) 6411616db3cSJohn Dyson exec_unmap_first_page(imgp); 64226f9a767SRodney W. Grimes 6439c0fed3dSDoug Rabson if (imgp->vp) { 644762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 645619eb6e5SJeff Roberson vput(imgp->vp); 646a3cf6ebaSDavid Greenman } 64726f9a767SRodney W. Grimes 648a9a08882SJeff Roberson if (imgp->stringbase != NULL) 649a9a08882SJeff Roberson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 650a9a08882SJeff Roberson ARG_MAX + PAGE_SIZE); 651a9a08882SJeff Roberson 6520b2ed1aeSJeff Roberson if (imgp->object) 6530b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6540b2ed1aeSJeff Roberson 655d1989db5SRobert Drehmel if (error == 0) { 656d1989db5SRobert Drehmel /* 657d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 658d1989db5SRobert Drehmel * the S_EXEC bit set. 659d1989db5SRobert Drehmel */ 660d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 661116734c4SMatthew Dillon goto done2; 662d1989db5SRobert Drehmel } 6631616db3cSJohn Dyson 66426f9a767SRodney W. Grimes exec_fail: 6659ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6669ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6679ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6689ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6699ca45e81SDag-Erling Smørgrav 670c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 67126f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 672670cb89bSRobert Watson #ifdef MAC 673670cb89bSRobert Watson mac_execve_exit(imgp); 674670cb89bSRobert Watson if (interplabelvalid) 675670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 676670cb89bSRobert Watson #endif 677b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 67826f9a767SRodney W. Grimes /* NOT REACHED */ 679116734c4SMatthew Dillon error = 0; 68026f9a767SRodney W. Grimes } 681116734c4SMatthew Dillon done2: 682670cb89bSRobert Watson #ifdef MAC 683670cb89bSRobert Watson mac_execve_exit(imgp); 684670cb89bSRobert Watson if (interplabelvalid) 685670cb89bSRobert Watson mac_destroy_vnode_label(&interplabel); 686670cb89bSRobert Watson #endif 687116734c4SMatthew Dillon mtx_unlock(&Giant); 688116734c4SMatthew Dillon return (error); 68926f9a767SRodney W. Grimes } 69026f9a767SRodney W. Grimes 691450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 692450ffb44SRobert Watson struct execve_args { 693450ffb44SRobert Watson char *fname; 694450ffb44SRobert Watson char **argv; 695450ffb44SRobert Watson char **envv; 696450ffb44SRobert Watson }; 697450ffb44SRobert Watson #endif 698450ffb44SRobert Watson 699450ffb44SRobert Watson /* 700450ffb44SRobert Watson * MPSAFE 701450ffb44SRobert Watson */ 702450ffb44SRobert Watson int 703450ffb44SRobert Watson execve(td, uap) 704450ffb44SRobert Watson struct thread *td; 705450ffb44SRobert Watson struct execve_args /* { 706b80521feSAlfred Perlstein char *fname; 707b80521feSAlfred Perlstein char **argv; 708b80521feSAlfred Perlstein char **envv; 709450ffb44SRobert Watson } */ *uap; 710450ffb44SRobert Watson { 711450ffb44SRobert Watson 712670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL)); 713670cb89bSRobert Watson } 714670cb89bSRobert Watson 715670cb89bSRobert Watson #ifndef _SYS_SYSPROTO_H_ 716670cb89bSRobert Watson struct __mac_execve_args { 717670cb89bSRobert Watson char *fname; 718670cb89bSRobert Watson char **argv; 719670cb89bSRobert Watson char **envv; 720670cb89bSRobert Watson struct mac *mac_p; 721670cb89bSRobert Watson }; 722670cb89bSRobert Watson #endif 723670cb89bSRobert Watson 724670cb89bSRobert Watson /* 725670cb89bSRobert Watson * MPSAFE 726670cb89bSRobert Watson */ 727670cb89bSRobert Watson int 728670cb89bSRobert Watson __mac_execve(td, uap) 729670cb89bSRobert Watson struct thread *td; 730670cb89bSRobert Watson struct __mac_execve_args /* { 731b80521feSAlfred Perlstein char *fname; 732b80521feSAlfred Perlstein char **argv; 733b80521feSAlfred Perlstein char **envv; 734b80521feSAlfred Perlstein struct mac *mac_p; 735670cb89bSRobert Watson } */ *uap; 736670cb89bSRobert Watson { 737670cb89bSRobert Watson 7380c93266bSRobert Watson #ifdef MAC 739670cb89bSRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv, 740670cb89bSRobert Watson uap->mac_p)); 7410c93266bSRobert Watson #else 7420c93266bSRobert Watson return (ENOSYS); 7430c93266bSRobert Watson #endif 744450ffb44SRobert Watson } 745450ffb44SRobert Watson 7461616db3cSJohn Dyson int 7471616db3cSJohn Dyson exec_map_first_page(imgp) 7481616db3cSJohn Dyson struct image_params *imgp; 7491616db3cSJohn Dyson { 750d8aad40cSJohn Baldwin int rv, i; 75195461b45SJohn Dyson int initial_pagein; 75295461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7531616db3cSJohn Dyson vm_object_t object; 7541616db3cSJohn Dyson 7550cddd8f0SMatthew Dillon GIANT_REQUIRED; 7561616db3cSJohn Dyson 7571616db3cSJohn Dyson if (imgp->firstpage) { 7581616db3cSJohn Dyson exec_unmap_first_page(imgp); 7591616db3cSJohn Dyson } 7601616db3cSJohn Dyson 7619ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 7621616db3cSJohn Dyson 76395461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 7641616db3cSJohn Dyson 76595461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 76695461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 76795461b45SJohn Dyson if (initial_pagein > object->size) 76895461b45SJohn Dyson initial_pagein = object->size; 76995461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 770d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 77195461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 77295461b45SJohn Dyson break; 77395461b45SJohn Dyson if (ma[i]->valid) 77495461b45SJohn Dyson break; 775ee113343SAlan Cox vm_page_lock_queues(); 776e69763a3SDoug Rabson vm_page_busy(ma[i]); 777ee113343SAlan Cox vm_page_unlock_queues(); 77895461b45SJohn Dyson } else { 779ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 780ca0387efSJake Burkholder VM_ALLOC_NORMAL); 78195461b45SJohn Dyson if (ma[i] == NULL) 78295461b45SJohn Dyson break; 78395461b45SJohn Dyson } 78495461b45SJohn Dyson } 78595461b45SJohn Dyson initial_pagein = i; 7861616db3cSJohn Dyson 78795461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 78895461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 78995461b45SJohn Dyson 790ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 791ca0387efSJake Burkholder (ma[0]->valid == 0)) { 792ecbb00a2SDoug Rabson if (ma[0]) { 79397646f56SAlan Cox vm_page_lock_queues(); 7944fec79beSAlan Cox pmap_remove_all(ma[0]); 7958f9110f6SJohn Dyson vm_page_free(ma[0]); 79697646f56SAlan Cox vm_page_unlock_queues(); 797ecbb00a2SDoug Rabson } 798a7cddfedSJake Burkholder return (EIO); 7991616db3cSJohn Dyson } 8001616db3cSJohn Dyson } 80197646f56SAlan Cox vm_page_lock_queues(); 80295461b45SJohn Dyson vm_page_wire(ma[0]); 803e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 80497646f56SAlan Cox vm_page_unlock_queues(); 8051616db3cSJohn Dyson 806ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 80795461b45SJohn Dyson imgp->firstpage = ma[0]; 8081616db3cSJohn Dyson 809a7cddfedSJake Burkholder return (0); 8101616db3cSJohn Dyson } 8111616db3cSJohn Dyson 8121616db3cSJohn Dyson void 8131616db3cSJohn Dyson exec_unmap_first_page(imgp) 8141616db3cSJohn Dyson struct image_params *imgp; 8151616db3cSJohn Dyson { 8160cddd8f0SMatthew Dillon GIANT_REQUIRED; 81723955314SAlfred Perlstein 8181616db3cSJohn Dyson if (imgp->firstpage) { 819ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 82097646f56SAlan Cox vm_page_lock_queues(); 82173007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 82297646f56SAlan Cox vm_page_unlock_queues(); 8231616db3cSJohn Dyson imgp->firstpage = NULL; 8241616db3cSJohn Dyson } 8251616db3cSJohn Dyson } 8261616db3cSJohn Dyson 82726f9a767SRodney W. Grimes /* 82826f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 82926f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 83026f9a767SRodney W. Grimes * automatically in trap.c. 83126f9a767SRodney W. Grimes */ 83226f9a767SRodney W. Grimes int 83305ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 834c52007c2SDavid Greenman struct image_params *imgp; 83505ba50f5SJake Burkholder struct sysentvec *sv; 83626f9a767SRodney W. Grimes { 83726f9a767SRodney W. Grimes int error; 8386f5dafeaSAlan Cox struct execlist *ep; 8395e20c11fSAlan Cox struct proc *p = imgp->proc; 8405e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 84105ba50f5SJake Burkholder vm_offset_t stack_addr; 84205ba50f5SJake Burkholder vm_map_t map; 84326f9a767SRodney W. Grimes 8440cddd8f0SMatthew Dillon GIANT_REQUIRED; 8450cddd8f0SMatthew Dillon 84605ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 8473ebc1248SPeter Wemm 848c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 84926f9a767SRodney W. Grimes 850af9ec885SJohn Dyson /* 8516f5dafeaSAlan Cox * Perform functions registered with at_exec(). 8526f5dafeaSAlan Cox */ 8536f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 8545e20c11fSAlan Cox (*ep->function)(p); 8556f5dafeaSAlan Cox 8566f5dafeaSAlan Cox /* 857af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 858af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 859af9ec885SJohn Dyson * not disrupted 860af9ec885SJohn Dyson */ 86105ba50f5SJake Burkholder map = &vmspace->vm_map; 86205ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 86305ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 8643db161e0SMatthew Dillon shmexit(vmspace); 8652d21129dSAlan Cox vm_page_lock_queues(); 86605ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 86705ba50f5SJake Burkholder vm_map_max(map)); 8682d21129dSAlan Cox vm_page_unlock_queues(); 86905ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 870af9ec885SJohn Dyson } else { 87105ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 8725e20c11fSAlan Cox vmspace = p->p_vmspace; 87305ba50f5SJake Burkholder map = &vmspace->vm_map; 874af9ec885SJohn Dyson } 87526f9a767SRodney W. Grimes 87626f9a767SRodney W. Grimes /* Allocate a new stack */ 87705ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 87805ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 8792267af78SJulian Elischer if (error) 8802267af78SJulian Elischer return (error); 8812267af78SJulian Elischer 88263c47a5cSDoug Rabson #ifdef __ia64__ 88363c47a5cSDoug Rabson { 88463c47a5cSDoug Rabson /* 88563c47a5cSDoug Rabson * Allocate backing store. We really need something 88663c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 88763c47a5cSDoug Rabson * store to grow upwards. This will do for now. 88863c47a5cSDoug Rabson */ 88963c47a5cSDoug Rabson vm_offset_t bsaddr; 89005ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 89105ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 89281f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 8935e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 89463c47a5cSDoug Rabson } 89563c47a5cSDoug Rabson #endif 89663c47a5cSDoug Rabson 8972267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 8982267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 8992267af78SJulian Elischer * process stack so we can check the stack rlimit. 9002267af78SJulian Elischer */ 901cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 90205ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 90326f9a767SRodney W. Grimes 90426f9a767SRodney W. Grimes return (0); 90526f9a767SRodney W. Grimes } 90626f9a767SRodney W. Grimes 90726f9a767SRodney W. Grimes /* 90826f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 90926f9a767SRodney W. Grimes * address space into the temporary string buffer. 91026f9a767SRodney W. Grimes */ 91126f9a767SRodney W. Grimes int 912c52007c2SDavid Greenman exec_extract_strings(imgp) 913c52007c2SDavid Greenman struct image_params *imgp; 91426f9a767SRodney W. Grimes { 91526f9a767SRodney W. Grimes char **argv, **envv; 91626f9a767SRodney W. Grimes char *argp, *envp; 917ecbb00a2SDoug Rabson int error; 918ecbb00a2SDoug Rabson size_t length; 91926f9a767SRodney W. Grimes 92026f9a767SRodney W. Grimes /* 92126f9a767SRodney W. Grimes * extract arguments first 92226f9a767SRodney W. Grimes */ 92326f9a767SRodney W. Grimes 924450ffb44SRobert Watson argv = imgp->userspace_argv; 92526f9a767SRodney W. Grimes 9260fd1a014SDavid Greenman if (argv) { 927aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 9285cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 9295cf3d12cSAndrey A. Chernov return (EFAULT); 9305cf3d12cSAndrey A. Chernov if (argp) 9315cf3d12cSAndrey A. Chernov argv++; 9325cf3d12cSAndrey A. Chernov if (imgp->argv0) 9335cf3d12cSAndrey A. Chernov argp = imgp->argv0; 9345cf3d12cSAndrey A. Chernov if (argp) { 9355cf3d12cSAndrey A. Chernov do { 93626f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 93726f9a767SRodney W. Grimes return (EFAULT); 938c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 939c52007c2SDavid Greenman imgp->stringspace, &length))) { 9400fd1a014SDavid Greenman if (error == ENAMETOOLONG) 94126f9a767SRodney W. Grimes return (E2BIG); 9420fd1a014SDavid Greenman return (error); 9430fd1a014SDavid Greenman } 944c52007c2SDavid Greenman imgp->stringspace -= length; 945c52007c2SDavid Greenman imgp->stringp += length; 946c52007c2SDavid Greenman imgp->argc++; 947aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 94826f9a767SRodney W. Grimes } 9490fd1a014SDavid Greenman } 95026f9a767SRodney W. Grimes 951b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 952b9df5231SPoul-Henning Kamp 95326f9a767SRodney W. Grimes /* 95426f9a767SRodney W. Grimes * extract environment strings 95526f9a767SRodney W. Grimes */ 95626f9a767SRodney W. Grimes 957450ffb44SRobert Watson envv = imgp->userspace_envv; 95826f9a767SRodney W. Grimes 9590fd1a014SDavid Greenman if (envv) { 960aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 96126f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 96226f9a767SRodney W. Grimes return (EFAULT); 963c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 964c52007c2SDavid Greenman imgp->stringspace, &length))) { 9650fd1a014SDavid Greenman if (error == ENAMETOOLONG) 96626f9a767SRodney W. Grimes return (E2BIG); 9670fd1a014SDavid Greenman return (error); 9680fd1a014SDavid Greenman } 969c52007c2SDavid Greenman imgp->stringspace -= length; 970c52007c2SDavid Greenman imgp->stringp += length; 971c52007c2SDavid Greenman imgp->envc++; 97226f9a767SRodney W. Grimes } 9730fd1a014SDavid Greenman } 97426f9a767SRodney W. Grimes 97526f9a767SRodney W. Grimes return (0); 97626f9a767SRodney W. Grimes } 97726f9a767SRodney W. Grimes 97826f9a767SRodney W. Grimes /* 97926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 98026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 98126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 98226f9a767SRodney W. Grimes */ 983654f6be1SBruce Evans register_t * 984c52007c2SDavid Greenman exec_copyout_strings(imgp) 985c52007c2SDavid Greenman struct image_params *imgp; 98626f9a767SRodney W. Grimes { 98726f9a767SRodney W. Grimes int argc, envc; 98826f9a767SRodney W. Grimes char **vectp; 98926f9a767SRodney W. Grimes char *stringp, *destp; 990654f6be1SBruce Evans register_t *stack_base; 99193f6448cSDavid Greenman struct ps_strings *arginfo; 992f3bec5d7SJake Burkholder struct proc *p; 993d66a5066SPeter Wemm int szsigcode; 99426f9a767SRodney W. Grimes 99526f9a767SRodney W. Grimes /* 99626f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 997d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 99826f9a767SRodney W. Grimes */ 999f3bec5d7SJake Burkholder p = imgp->proc; 1000f3bec5d7SJake Burkholder szsigcode = 0; 100105ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1002f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1003f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1004d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 10051ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 10061ed012f9SPeter Wemm 100726f9a767SRodney W. Grimes /* 1008d66a5066SPeter Wemm * install sigcode 1009d66a5066SPeter Wemm */ 1010d66a5066SPeter Wemm if (szsigcode) 1011f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1012f3bec5d7SJake Burkholder szsigcode), szsigcode); 1013d66a5066SPeter Wemm 1014d66a5066SPeter Wemm /* 1015e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1016e1743d02SSøren Schmidt * on the stack. 1017e1743d02SSøren Schmidt */ 1018b9a22da4STakanori Watanabe if (imgp->auxargs) { 1019e1743d02SSøren Schmidt /* 1020b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1021b9a22da4STakanori Watanabe * lower compatibility. 1022b9a22da4STakanori Watanabe */ 1023ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1024ca0387efSJake Burkholder (AT_COUNT * 2); 1025b9a22da4STakanori Watanabe /* 1026b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1027b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1028b9a22da4STakanori Watanabe * for argument of Runtime loader. 1029e1743d02SSøren Schmidt */ 1030e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1031b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 1032b9a22da4STakanori Watanabe 1033b9a22da4STakanori Watanabe } else 1034e1743d02SSøren Schmidt /* 1035b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1036b9a22da4STakanori Watanabe * the arg and env vector sets 103726f9a767SRodney W. Grimes */ 1038ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1039ca0387efSJake Burkholder sizeof(char *)); 104026f9a767SRodney W. Grimes 104126f9a767SRodney W. Grimes /* 104226f9a767SRodney W. Grimes * vectp also becomes our initial stack base 104326f9a767SRodney W. Grimes */ 1044654f6be1SBruce Evans stack_base = (register_t *)vectp; 104526f9a767SRodney W. Grimes 1046c52007c2SDavid Greenman stringp = imgp->stringbase; 1047c52007c2SDavid Greenman argc = imgp->argc; 1048c52007c2SDavid Greenman envc = imgp->envc; 104926f9a767SRodney W. Grimes 105093f6448cSDavid Greenman /* 10513aed948bSDavid Greenman * Copy out strings - arguments and environment. 105293f6448cSDavid Greenman */ 1053c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 105493f6448cSDavid Greenman 105593f6448cSDavid Greenman /* 10563aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 10573aed948bSDavid Greenman */ 1058aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 10593aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 10603aed948bSDavid Greenman 10613aed948bSDavid Greenman /* 10623aed948bSDavid Greenman * Fill in argument portion of vector table. 106393f6448cSDavid Greenman */ 106426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1065aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10663aed948bSDavid Greenman while (*stringp++ != 0) 10673aed948bSDavid Greenman destp++; 10683aed948bSDavid Greenman destp++; 106926f9a767SRodney W. Grimes } 107026f9a767SRodney W. Grimes 10711a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 10726ab46d52SBruce Evans suword(vectp++, 0); 107326f9a767SRodney W. Grimes 1074aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 10753aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 107693f6448cSDavid Greenman 107793f6448cSDavid Greenman /* 10783aed948bSDavid Greenman * Fill in environment portion of vector table. 107993f6448cSDavid Greenman */ 108026f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1081aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 10823aed948bSDavid Greenman while (*stringp++ != 0) 10833aed948bSDavid Greenman destp++; 10843aed948bSDavid Greenman destp++; 108526f9a767SRodney W. Grimes } 108626f9a767SRodney W. Grimes 108726f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 10886ab46d52SBruce Evans suword(vectp, 0); 108926f9a767SRodney W. Grimes 109026f9a767SRodney W. Grimes return (stack_base); 109126f9a767SRodney W. Grimes } 109226f9a767SRodney W. Grimes 109326f9a767SRodney W. Grimes /* 109426f9a767SRodney W. Grimes * Check permissions of file to execute. 1095cf64863aSRobert Watson * Called with imgp->vp locked. 109626f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 109726f9a767SRodney W. Grimes */ 1098c8a79999SPeter Wemm int 1099c52007c2SDavid Greenman exec_check_permissions(imgp) 1100c52007c2SDavid Greenman struct image_params *imgp; 110126f9a767SRodney W. Grimes { 1102c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1103c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1104a854ed98SJohn Baldwin struct thread *td; 110526f9a767SRodney W. Grimes int error; 110626f9a767SRodney W. Grimes 1107a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1108339b79b9SRobert Watson 1109ec35c2afSRobert Watson /* Get file attributes */ 1110ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1111ec35c2afSRobert Watson if (error) 1112ec35c2afSRobert Watson return (error); 1113ec35c2afSRobert Watson 1114339b79b9SRobert Watson #ifdef MAC 1115670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1116339b79b9SRobert Watson if (error) 1117339b79b9SRobert Watson return (error); 1118339b79b9SRobert Watson #endif 1119339b79b9SRobert Watson 112026f9a767SRodney W. Grimes /* 112126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 112226f9a767SRodney W. Grimes * file resides on. 112326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 112426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 112526f9a767SRodney W. Grimes * file really is executable. 112626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 112726f9a767SRodney W. Grimes */ 1128c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 112926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1130a854ed98SJohn Baldwin (attr->va_type != VREG)) 113126f9a767SRodney W. Grimes return (EACCES); 113226f9a767SRodney W. Grimes 113326f9a767SRodney W. Grimes /* 113426f9a767SRodney W. Grimes * Zero length files can't be exec'd 113526f9a767SRodney W. Grimes */ 113626f9a767SRodney W. Grimes if (attr->va_size == 0) 113726f9a767SRodney W. Grimes return (ENOEXEC); 113826f9a767SRodney W. Grimes 113926f9a767SRodney W. Grimes /* 114026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 114126f9a767SRodney W. Grimes */ 1142a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 114326f9a767SRodney W. Grimes if (error) 114426f9a767SRodney W. Grimes return (error); 114526f9a767SRodney W. Grimes 11466d5a0a8cSDavid Greenman /* 11476d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 11486d5a0a8cSDavid Greenman * if there are any. 11496d5a0a8cSDavid Greenman */ 11506d5a0a8cSDavid Greenman if (vp->v_writecount) 11516d5a0a8cSDavid Greenman return (ETXTBSY); 11526d5a0a8cSDavid Greenman 11536d5a0a8cSDavid Greenman /* 11546d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 11556d5a0a8cSDavid Greenman * general case). 11566d5a0a8cSDavid Greenman */ 1157a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 115826f9a767SRodney W. Grimes return (error); 1159df8bae1dSRodney W. Grimes } 1160aa855a59SPeter Wemm 1161aa855a59SPeter Wemm /* 1162aa855a59SPeter Wemm * Exec handler registration 1163aa855a59SPeter Wemm */ 1164aa855a59SPeter Wemm int 1165aa855a59SPeter Wemm exec_register(execsw_arg) 1166aa855a59SPeter Wemm const struct execsw *execsw_arg; 1167aa855a59SPeter Wemm { 1168aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1169aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1170aa855a59SPeter Wemm 1171aa855a59SPeter Wemm if (execsw) 1172aa855a59SPeter Wemm for (es = execsw; *es; es++) 1173aa855a59SPeter Wemm count++; 1174a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1175aa855a59SPeter Wemm if (newexecsw == NULL) 1176a7cddfedSJake Burkholder return (ENOMEM); 1177aa855a59SPeter Wemm xs = newexecsw; 1178aa855a59SPeter Wemm if (execsw) 1179aa855a59SPeter Wemm for (es = execsw; *es; es++) 1180aa855a59SPeter Wemm *xs++ = *es; 1181aa855a59SPeter Wemm *xs++ = execsw_arg; 1182aa855a59SPeter Wemm *xs = NULL; 1183aa855a59SPeter Wemm if (execsw) 1184aa855a59SPeter Wemm free(execsw, M_TEMP); 1185aa855a59SPeter Wemm execsw = newexecsw; 1186a7cddfedSJake Burkholder return (0); 1187aa855a59SPeter Wemm } 1188aa855a59SPeter Wemm 1189aa855a59SPeter Wemm int 1190aa855a59SPeter Wemm exec_unregister(execsw_arg) 1191aa855a59SPeter Wemm const struct execsw *execsw_arg; 1192aa855a59SPeter Wemm { 1193aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1194aa855a59SPeter Wemm int count = 1; 1195aa855a59SPeter Wemm 1196aa855a59SPeter Wemm if (execsw == NULL) 1197aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1198aa855a59SPeter Wemm 1199aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1200aa855a59SPeter Wemm if (*es == execsw_arg) 1201aa855a59SPeter Wemm break; 1202aa855a59SPeter Wemm } 1203aa855a59SPeter Wemm if (*es == NULL) 1204a7cddfedSJake Burkholder return (ENOENT); 1205aa855a59SPeter Wemm for (es = execsw; *es; es++) 1206aa855a59SPeter Wemm if (*es != execsw_arg) 1207aa855a59SPeter Wemm count++; 1208a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1209aa855a59SPeter Wemm if (newexecsw == NULL) 1210a7cddfedSJake Burkholder return (ENOMEM); 1211aa855a59SPeter Wemm xs = newexecsw; 1212aa855a59SPeter Wemm for (es = execsw; *es; es++) 1213aa855a59SPeter Wemm if (*es != execsw_arg) 1214aa855a59SPeter Wemm *xs++ = *es; 1215aa855a59SPeter Wemm *xs = NULL; 1216aa855a59SPeter Wemm if (execsw) 1217aa855a59SPeter Wemm free(execsw, M_TEMP); 1218aa855a59SPeter Wemm execsw = newexecsw; 1219a7cddfedSJake Burkholder return (0); 1220aa855a59SPeter Wemm } 122121d56e9cSAlfred Perlstein 122221d56e9cSAlfred Perlstein int 122321d56e9cSAlfred Perlstein at_exec(function) 122421d56e9cSAlfred Perlstein execlist_fn function; 122521d56e9cSAlfred Perlstein { 122621d56e9cSAlfred Perlstein struct execlist *ep; 122721d56e9cSAlfred Perlstein 122821d56e9cSAlfred Perlstein #ifdef INVARIANTS 122921d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 123021d56e9cSAlfred Perlstein if (rm_at_exec(function)) 123121d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 123221d56e9cSAlfred Perlstein function); 123321d56e9cSAlfred Perlstein #endif 123421d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 123521d56e9cSAlfred Perlstein if (ep == NULL) 123621d56e9cSAlfred Perlstein return (ENOMEM); 123721d56e9cSAlfred Perlstein ep->function = function; 123821d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 123921d56e9cSAlfred Perlstein return (0); 124021d56e9cSAlfred Perlstein } 124121d56e9cSAlfred Perlstein 124221d56e9cSAlfred Perlstein /* 124321d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 124421d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 124521d56e9cSAlfred Perlstein */ 124621d56e9cSAlfred Perlstein int 124721d56e9cSAlfred Perlstein rm_at_exec(function) 124821d56e9cSAlfred Perlstein execlist_fn function; 124921d56e9cSAlfred Perlstein { 125021d56e9cSAlfred Perlstein struct execlist *ep; 125121d56e9cSAlfred Perlstein 125221d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 125321d56e9cSAlfred Perlstein if (ep->function == function) { 125421d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 125521d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 125621d56e9cSAlfred Perlstein return (1); 125721d56e9cSAlfred Perlstein } 125821d56e9cSAlfred Perlstein } 125921d56e9cSAlfred Perlstein return (0); 126021d56e9cSAlfred Perlstein } 1261