19454b2d8SWarner Losh /*- 226f9a767SRodney W. Grimes * Copyright (c) 1993, David Greenman 326f9a767SRodney W. Grimes * All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 1426f9a767SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1726f9a767SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24df8bae1dSRodney W. Grimes * SUCH DAMAGE. 25df8bae1dSRodney W. Grimes */ 26df8bae1dSRodney W. Grimes 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29677b542eSDavid E. O'Brien 304da0d332SPeter Wemm #include "opt_hwpmc_hooks.h" 311e9b3d91SPeter Wemm #include "opt_ktrace.h" 32339b79b9SRobert Watson #include "opt_mac.h" 331e9b3d91SPeter Wemm 34df8bae1dSRodney W. Grimes #include <sys/param.h> 3526f9a767SRodney W. Grimes #include <sys/systm.h> 3675b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 37fb919e4dSMark Murray #include <sys/lock.h> 38fb919e4dSMark Murray #include <sys/mutex.h> 39d2d3e875SBruce Evans #include <sys/sysproto.h> 4026f9a767SRodney W. Grimes #include <sys/signalvar.h> 4126f9a767SRodney W. Grimes #include <sys/kernel.h> 42339b79b9SRobert Watson #include <sys/mac.h> 4326f9a767SRodney W. Grimes #include <sys/mount.h> 44797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 45079cc25bSDavid Greenman #include <sys/fcntl.h> 4626f9a767SRodney W. Grimes #include <sys/acct.h> 4726f9a767SRodney W. Grimes #include <sys/exec.h> 4826f9a767SRodney W. Grimes #include <sys/imgact.h> 49e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 5026f9a767SRodney W. Grimes #include <sys/wait.h> 51333ea485SGuido van Rooij #include <sys/malloc.h> 52a794e791SBruce Evans #include <sys/proc.h> 532a024a2bSSean Eric Fagan #include <sys/pioctl.h> 54a794e791SBruce Evans #include <sys/namei.h> 556004362eSDavid Schultz #include <sys/resourcevar.h> 5659c8bc40SAlan Cox #include <sys/sf_buf.h> 5784e0b075SDavid Xu #include <sys/syscallsubr.h> 581e1e0b44SSøren Schmidt #include <sys/sysent.h> 59797f2d22SPoul-Henning Kamp #include <sys/shm.h> 6099ac3bc8SPeter Wemm #include <sys/sysctl.h> 61a794e791SBruce Evans #include <sys/vnode.h> 62c781aea8SPeter Wemm #ifdef KTRACE 63c781aea8SPeter Wemm #include <sys/ktrace.h> 64c781aea8SPeter Wemm #endif 6526f9a767SRodney W. Grimes 6626f9a767SRodney W. Grimes #include <vm/vm.h> 67efeaf95aSDavid Greenman #include <vm/vm_param.h> 68efeaf95aSDavid Greenman #include <vm/pmap.h> 691616db3cSJohn Dyson #include <vm/vm_page.h> 70efeaf95aSDavid Greenman #include <vm/vm_map.h> 7126f9a767SRodney W. Grimes #include <vm/vm_kern.h> 72efeaf95aSDavid Greenman #include <vm/vm_extern.h> 731ebd0c59SDavid Greenman #include <vm/vm_object.h> 741616db3cSJohn Dyson #include <vm/vm_pager.h> 7526f9a767SRodney W. Grimes 76ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 77ebccf1e3SJoseph Koshy #include <sys/pmckern.h> 78ebccf1e3SJoseph Koshy #endif 79ebccf1e3SJoseph Koshy 8026f9a767SRodney W. Grimes #include <machine/reg.h> 8126f9a767SRodney W. Grimes 82b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 83b9df5231SPoul-Henning Kamp 8405ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 8505ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 865dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 87610ecfe0SMaxim Sobolev static int do_execve(struct thread *td, struct image_args *args, 88610ecfe0SMaxim Sobolev struct mac *mac_p); 8905ba50f5SJake Burkholder 909701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9105ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9205ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 93486bddb0SDoug Rabson 949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9505ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9605ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 9799ac3bc8SPeter Wemm 985dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 995dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 1005dadd17bSJake Burkholder 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 10505ba50f5SJake Burkholder static int 10605ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 10705ba50f5SJake Burkholder { 10805ba50f5SJake Burkholder struct proc *p; 109df7c361eSPeter Wemm int error; 11005ba50f5SJake Burkholder 11105ba50f5SJake Burkholder p = curproc; 112a7bc3102SPeter Wemm #ifdef SCTL_MASK32 113a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 114df7c361eSPeter Wemm unsigned int val; 115df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_psstrings; 116df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 117df7c361eSPeter Wemm } else 118df7c361eSPeter Wemm #endif 119df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 120df7c361eSPeter Wemm sizeof(p->p_sysent->sv_psstrings)); 121df7c361eSPeter Wemm return error; 12205ba50f5SJake Burkholder } 12305ba50f5SJake Burkholder 12405ba50f5SJake Burkholder static int 12505ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12605ba50f5SJake Burkholder { 12705ba50f5SJake Burkholder struct proc *p; 128df7c361eSPeter Wemm int error; 12905ba50f5SJake Burkholder 13005ba50f5SJake Burkholder p = curproc; 131a7bc3102SPeter Wemm #ifdef SCTL_MASK32 132a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 133df7c361eSPeter Wemm unsigned int val; 134df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_usrstack; 135df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 136df7c361eSPeter Wemm } else 137df7c361eSPeter Wemm #endif 138df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 139df7c361eSPeter Wemm sizeof(p->p_sysent->sv_usrstack)); 140df7c361eSPeter Wemm return error; 14105ba50f5SJake Burkholder } 14205ba50f5SJake Burkholder 1435dadd17bSJake Burkholder static int 1445dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1455dadd17bSJake Burkholder { 1465dadd17bSJake Burkholder struct proc *p; 1475dadd17bSJake Burkholder 1485dadd17bSJake Burkholder p = curproc; 1495dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1505dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1515dadd17bSJake Burkholder } 1525dadd17bSJake Burkholder 15399ac3bc8SPeter Wemm /* 154aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 155aa855a59SPeter Wemm * double pointer here. 156df8bae1dSRodney W. Grimes */ 157aa855a59SPeter Wemm static const struct execsw **execsw; 15826f9a767SRodney W. Grimes 159ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 160ca46e90eSBruce Evans struct execve_args { 161ca46e90eSBruce Evans char *fname; 162ca46e90eSBruce Evans char **argv; 163ca46e90eSBruce Evans char **envv; 164ca46e90eSBruce Evans }; 165ca46e90eSBruce Evans #endif 166ca46e90eSBruce Evans 167ca46e90eSBruce Evans /* 168ca46e90eSBruce Evans * MPSAFE 169ca46e90eSBruce Evans */ 170ca46e90eSBruce Evans int 171ca46e90eSBruce Evans execve(td, uap) 172ca46e90eSBruce Evans struct thread *td; 173ca46e90eSBruce Evans struct execve_args /* { 174ca46e90eSBruce Evans char *fname; 175ca46e90eSBruce Evans char **argv; 176ca46e90eSBruce Evans char **envv; 177ca46e90eSBruce Evans } */ *uap; 178ca46e90eSBruce Evans { 179610ecfe0SMaxim Sobolev int error; 180610ecfe0SMaxim Sobolev struct image_args args; 181ca46e90eSBruce Evans 182610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 183610ecfe0SMaxim Sobolev uap->argv, uap->envv); 184610ecfe0SMaxim Sobolev 185610ecfe0SMaxim Sobolev if (error == 0) 186610ecfe0SMaxim Sobolev error = kern_execve(td, &args, NULL); 187610ecfe0SMaxim Sobolev 188610ecfe0SMaxim Sobolev exec_free_args(&args); 189610ecfe0SMaxim Sobolev 190610ecfe0SMaxim Sobolev return (error); 191ca46e90eSBruce Evans } 192ca46e90eSBruce Evans 193ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 194ca46e90eSBruce Evans struct __mac_execve_args { 195ca46e90eSBruce Evans char *fname; 196ca46e90eSBruce Evans char **argv; 197ca46e90eSBruce Evans char **envv; 198ca46e90eSBruce Evans struct mac *mac_p; 199ca46e90eSBruce Evans }; 200ca46e90eSBruce Evans #endif 201ca46e90eSBruce Evans 202ca46e90eSBruce Evans /* 203ca46e90eSBruce Evans * MPSAFE 204ca46e90eSBruce Evans */ 205ca46e90eSBruce Evans int 206ca46e90eSBruce Evans __mac_execve(td, uap) 207ca46e90eSBruce Evans struct thread *td; 208ca46e90eSBruce Evans struct __mac_execve_args /* { 209ca46e90eSBruce Evans char *fname; 210ca46e90eSBruce Evans char **argv; 211ca46e90eSBruce Evans char **envv; 212ca46e90eSBruce Evans struct mac *mac_p; 213ca46e90eSBruce Evans } */ *uap; 214ca46e90eSBruce Evans { 215ca46e90eSBruce Evans #ifdef MAC 216610ecfe0SMaxim Sobolev int error; 217610ecfe0SMaxim Sobolev struct image_args args; 218610ecfe0SMaxim Sobolev 219610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 220610ecfe0SMaxim Sobolev uap->argv, uap->envv); 221610ecfe0SMaxim Sobolev 222610ecfe0SMaxim Sobolev if (error == 0) 223610ecfe0SMaxim Sobolev error = kern_execve(td, &args, uap->mac_p); 224610ecfe0SMaxim Sobolev 225610ecfe0SMaxim Sobolev exec_free_args(&args); 226610ecfe0SMaxim Sobolev 227610ecfe0SMaxim Sobolev return (error); 228ca46e90eSBruce Evans #else 229ca46e90eSBruce Evans return (ENOSYS); 230ca46e90eSBruce Evans #endif 231ca46e90eSBruce Evans } 232ca46e90eSBruce Evans 23384e0b075SDavid Xu int 234610ecfe0SMaxim Sobolev kern_execve(td, args, mac_p) 235906ac69dSDavid Xu struct thread *td; 236610ecfe0SMaxim Sobolev struct image_args *args; 237906ac69dSDavid Xu struct mac *mac_p; 238906ac69dSDavid Xu { 239906ac69dSDavid Xu struct proc *p = td->td_proc; 240906ac69dSDavid Xu int error; 241906ac69dSDavid Xu 242906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 243906ac69dSDavid Xu PROC_LOCK(p); 244906ac69dSDavid Xu if (thread_single(SINGLE_BOUNDARY)) { 245906ac69dSDavid Xu PROC_UNLOCK(p); 246906ac69dSDavid Xu return (ERESTART); /* Try again later. */ 247906ac69dSDavid Xu } 248906ac69dSDavid Xu PROC_UNLOCK(p); 249906ac69dSDavid Xu } 250906ac69dSDavid Xu 251610ecfe0SMaxim Sobolev error = do_execve(td, args, mac_p); 252906ac69dSDavid Xu 253906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 254906ac69dSDavid Xu PROC_LOCK(p); 255906ac69dSDavid Xu /* 256906ac69dSDavid Xu * If success, we upgrade to SINGLE_EXIT state to 257906ac69dSDavid Xu * force other threads to suicide. 258906ac69dSDavid Xu */ 259906ac69dSDavid Xu if (error == 0) 260906ac69dSDavid Xu thread_single(SINGLE_EXIT); 261906ac69dSDavid Xu else 262906ac69dSDavid Xu thread_single_end(); 263906ac69dSDavid Xu PROC_UNLOCK(p); 264906ac69dSDavid Xu } 265906ac69dSDavid Xu 266906ac69dSDavid Xu return (error); 267906ac69dSDavid Xu } 268906ac69dSDavid Xu 26926f9a767SRodney W. Grimes /* 270450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 271450ffb44SRobert Watson * userspace pointers from the passed thread. 272116734c4SMatthew Dillon * 273116734c4SMatthew Dillon * MPSAFE 27426f9a767SRodney W. Grimes */ 275450ffb44SRobert Watson static int 276610ecfe0SMaxim Sobolev do_execve(td, args, mac_p) 277b40ce416SJulian Elischer struct thread *td; 278610ecfe0SMaxim Sobolev struct image_args *args; 279670cb89bSRobert Watson struct mac *mac_p; 280df8bae1dSRodney W. Grimes { 281b40ce416SJulian Elischer struct proc *p = td->td_proc; 28226f9a767SRodney W. Grimes struct nameidata nd, *ndp; 2839b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 2841419eacbSAlfred Perlstein struct uidinfo *euip; 285654f6be1SBruce Evans register_t *stack_base; 286bb56ec4aSPoul-Henning Kamp int error, len, i; 287c52007c2SDavid Greenman struct image_params image_params, *imgp; 2886341095eSKen Smith struct vattr atimeattr, attr; 2894d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 29069be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 29190af4afaSJohn Baldwin struct sigacts *oldsigacts, *newsigacts; 2926c84de02SJohn Baldwin #ifdef KTRACE 2936c84de02SJohn Baldwin struct vnode *tracevp = NULL; 294a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 2956c84de02SJohn Baldwin #endif 2966c84de02SJohn Baldwin struct vnode *textvp = NULL; 297d06c0d4dSRobert Watson int credential_changing; 298269576c8SJeff Roberson int vfslocked; 299619eb6e5SJeff Roberson int textset; 300ccafe7ebSRobert Watson #ifdef MAC 301eca8a663SRobert Watson struct label *interplabel = NULL; 302eca8a663SRobert Watson int will_transition; 303ccafe7ebSRobert Watson #endif 30426f9a767SRodney W. Grimes 305532c491bSJeff Roberson vfslocked = 0; 306c52007c2SDavid Greenman imgp = &image_params; 307df8bae1dSRodney W. Grimes 3089ca45e81SDag-Erling Smørgrav /* 3099ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 3109ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 3119ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 3129ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 3139ca45e81SDag-Erling Smørgrav * privileges. 3149ca45e81SDag-Erling Smørgrav */ 3159ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 3169ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 31791f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 3189ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 3199ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 3209ca45e81SDag-Erling Smørgrav 321df8bae1dSRodney W. Grimes /* 322c52007c2SDavid Greenman * Initialize part of the common data 323df8bae1dSRodney W. Grimes */ 324c52007c2SDavid Greenman imgp->proc = p; 325670cb89bSRobert Watson imgp->execlabel = NULL; 326c52007c2SDavid Greenman imgp->attr = &attr; 327c52007c2SDavid Greenman imgp->entry_addr = 0; 328c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 329c52007c2SDavid Greenman imgp->interpreted = 0; 33090dc539bSMaxim Sobolev imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX; 331e1743d02SSøren Schmidt imgp->auxargs = NULL; 3321616db3cSJohn Dyson imgp->vp = NULL; 3330b2ed1aeSJeff Roberson imgp->object = NULL; 3341616db3cSJohn Dyson imgp->firstpage = NULL; 3354fe88fe6SJohn Polstra imgp->ps_strings = 0; 336b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 337610ecfe0SMaxim Sobolev imgp->args = args; 33826f9a767SRodney W. Grimes 339670cb89bSRobert Watson #ifdef MAC 340eca8a663SRobert Watson error = mac_execve_enter(imgp, mac_p); 341532c491bSJeff Roberson if (error) 342670cb89bSRobert Watson goto exec_fail; 343670cb89bSRobert Watson #endif 344670cb89bSRobert Watson 34559c8bc40SAlan Cox imgp->image_header = NULL; 34626f9a767SRodney W. Grimes 34726f9a767SRodney W. Grimes /* 34826f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 34926f9a767SRodney W. Grimes * in ni_vp amoung other things. 35026f9a767SRodney W. Grimes */ 35126f9a767SRodney W. Grimes ndp = &nd; 352269576c8SJeff Roberson NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 353610ecfe0SMaxim Sobolev UIO_SYSSPACE, args->fname, td); 35426f9a767SRodney W. Grimes 35526f9a767SRodney W. Grimes interpret: 35626f9a767SRodney W. Grimes error = namei(ndp); 357610ecfe0SMaxim Sobolev if (error) 35826f9a767SRodney W. Grimes goto exec_fail; 35926f9a767SRodney W. Grimes 360269576c8SJeff Roberson vfslocked = NDHASGIANT(ndp); 361c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 36226f9a767SRodney W. Grimes 36326f9a767SRodney W. Grimes /* 3649022e4adSDavid Greenman * Check file permissions (also 'opens' file) 3659022e4adSDavid Greenman */ 366c52007c2SDavid Greenman error = exec_check_permissions(imgp); 367619eb6e5SJeff Roberson if (error) 36826f9a767SRodney W. Grimes goto exec_fail_dealloc; 369619eb6e5SJeff Roberson 3708516dd18SPoul-Henning Kamp imgp->object = imgp->vp->v_object; 3718516dd18SPoul-Henning Kamp if (imgp->object != NULL) 3720b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 37326f9a767SRodney W. Grimes 374619eb6e5SJeff Roberson /* 375619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 376619eb6e5SJeff Roberson * activating it. 377619eb6e5SJeff Roberson * 378619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 379619eb6e5SJeff Roberson * actually an executable image. 380619eb6e5SJeff Roberson */ 381619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 382619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 383619eb6e5SJeff Roberson 3841616db3cSJohn Dyson error = exec_map_first_page(imgp); 3856d5a0a8cSDavid Greenman if (error) 38626f9a767SRodney W. Grimes goto exec_fail_dealloc; 38726f9a767SRodney W. Grimes 38826f9a767SRodney W. Grimes /* 389d323ddf3SMatthew Dillon * If the current process has a special image activator it 390d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 391d323ddf3SMatthew Dillon * scripts differently. 39226f9a767SRodney W. Grimes */ 393d323ddf3SMatthew Dillon error = -1; 394d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 395d323ddf3SMatthew Dillon error = img_first(imgp); 396d323ddf3SMatthew Dillon 397d323ddf3SMatthew Dillon /* 398d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 399d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 400d323ddf3SMatthew Dillon * and an error otherwise. 401d323ddf3SMatthew Dillon */ 402d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 403d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 404d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 405d323ddf3SMatthew Dillon continue; 406d323ddf3SMatthew Dillon } 407c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 408d323ddf3SMatthew Dillon } 409d323ddf3SMatthew Dillon 410d323ddf3SMatthew Dillon if (error) { 411619eb6e5SJeff Roberson if (error == -1) { 412619eb6e5SJeff Roberson if (textset == 0) 413619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 414d323ddf3SMatthew Dillon error = ENOEXEC; 415619eb6e5SJeff Roberson } 41626f9a767SRodney W. Grimes goto exec_fail_dealloc; 417d323ddf3SMatthew Dillon } 418d323ddf3SMatthew Dillon 419d323ddf3SMatthew Dillon /* 420d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 421d323ddf3SMatthew Dillon * activate the interpreter. 422d323ddf3SMatthew Dillon */ 423c52007c2SDavid Greenman if (imgp->interpreted) { 4241616db3cSJohn Dyson exec_unmap_first_page(imgp); 425619eb6e5SJeff Roberson /* 426619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 427619eb6e5SJeff Roberson * period before we determine that something is a script where 428619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 429619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 430619eb6e5SJeff Roberson */ 431619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 432762e6b85SEivind Eklund /* free name buffer and old vnode */ 433762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 434670cb89bSRobert Watson #ifdef MAC 435eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 436eca8a663SRobert Watson mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel); 437670cb89bSRobert Watson #endif 438619eb6e5SJeff Roberson vput(ndp->ni_vp); 4390b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 4400b2ed1aeSJeff Roberson imgp->object = NULL; 441269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 442532c491bSJeff Roberson vfslocked = 0; 44326f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 444269576c8SJeff Roberson NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 445b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 44626f9a767SRodney W. Grimes goto interpret; 44726f9a767SRodney W. Grimes } 44826f9a767SRodney W. Grimes 44926f9a767SRodney W. Grimes /* 45026f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 45126f9a767SRodney W. Grimes */ 4523ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 4533ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 4543ebc1248SPeter Wemm else 455c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 45626f9a767SRodney W. Grimes 45726f9a767SRodney W. Grimes /* 4581e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 4591e1e0b44SSøren Schmidt * let it do the stack setup. 4601e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 46126f9a767SRodney W. Grimes */ 462d6c847f3SBruce Evans if (p->p_sysent->sv_fixup != NULL) 463c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 4641e1e0b44SSøren Schmidt else 465610ecfe0SMaxim Sobolev suword(--stack_base, imgp->args->argc); 46626f9a767SRodney W. Grimes 467a78e8d2aSDavid Greenman /* 468a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 469a78e8d2aSDavid Greenman * be shared after an exec. 470a78e8d2aSDavid Greenman */ 471c113083cSPoul-Henning Kamp fdunshare(p, td); 472a78e8d2aSDavid Greenman 473333ea485SGuido van Rooij /* 4749b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4759b3b1c5fSJohn Baldwin */ 4769b3b1c5fSJohn Baldwin newcred = crget(); 4771419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 478610ecfe0SMaxim Sobolev i = imgp->args->begin_envv - imgp->args->begin_argv; 4799b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 4809b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4819b3b1c5fSJohn Baldwin 4829b3b1c5fSJohn Baldwin /* close files on exec */ 4839b3b1c5fSJohn Baldwin fdcloseexec(td); 4849b3b1c5fSJohn Baldwin 485619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 486619eb6e5SJeff Roberson VREF(ndp->ni_vp); 487619eb6e5SJeff Roberson 4889b3b1c5fSJohn Baldwin /* 489333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4908688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 491b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 492333ea485SGuido van Rooij * reset. 493333ea485SGuido van Rooij */ 4949b3b1c5fSJohn Baldwin PROC_LOCK(p); 49590af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 49690af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 4979b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 49890af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 49990af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 5009b3b1c5fSJohn Baldwin PROC_LOCK(p); 50190af4afaSJohn Baldwin p->p_sigacts = newsigacts; 50290af4afaSJohn Baldwin } else 50390af4afaSJohn Baldwin oldsigacts = NULL; 504333ea485SGuido van Rooij 505fdf4e8b3SWarner Losh /* Stop profiling */ 506fdf4e8b3SWarner Losh stopprofclock(p); 507fdf4e8b3SWarner Losh 50826f9a767SRodney W. Grimes /* reset caught signals */ 50926f9a767SRodney W. Grimes execsigs(p); 51026f9a767SRodney W. Grimes 51126f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 51226f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 51326f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 51426f9a767SRodney W. Grimes p->p_comm[len] = 0; 51526f9a767SRodney W. Grimes 51626f9a767SRodney W. Grimes /* 51724b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 518dc733423SDag-Erling Smørgrav * it that it now has its own resources back 51926f9a767SRodney W. Grimes */ 52026f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 52126f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 52226f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 5237f05b035SAlfred Perlstein wakeup(p->p_pptr); 52426f9a767SRodney W. Grimes } 52526f9a767SRodney W. Grimes 52626f9a767SRodney W. Grimes /* 527a78e8d2aSDavid Greenman * Implement image setuid/setgid. 528a78e8d2aSDavid Greenman * 529a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 530a78e8d2aSDavid Greenman * the process is being traced. 531670cb89bSRobert Watson * 532670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 533670cb89bSRobert Watson * transitions on the file system. 534c52007c2SDavid Greenman */ 535b1fc0ec1SRobert Watson oldcred = p->p_ucred; 536d06c0d4dSRobert Watson credential_changing = 0; 537d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 538d06c0d4dSRobert Watson attr.va_uid; 539d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 540d06c0d4dSRobert Watson attr.va_gid; 541ccafe7ebSRobert Watson #ifdef MAC 542670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 543eca8a663SRobert Watson interplabel, imgp); 544ccafe7ebSRobert Watson credential_changing |= will_transition; 545ccafe7ebSRobert Watson #endif 546d06c0d4dSRobert Watson 547d06c0d4dSRobert Watson if (credential_changing && 548a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 549c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 550c52007c2SDavid Greenman /* 551c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 552b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 553b85db196SPeter Wemm * we do not regain any tracing during a possible block. 55426f9a767SRodney W. Grimes */ 555b85db196SPeter Wemm setsugid(p); 5566c84de02SJohn Baldwin #ifdef KTRACE 55756f21b9dSColin Percival if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) { 5586c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 55979deba82SMatthew Dillon p->p_traceflag = 0; 560a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 561a5881ea5SJohn Baldwin p->p_tracevp = NULL; 562a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 563a5881ea5SJohn Baldwin p->p_tracecred = NULL; 5646c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 56526f9a767SRodney W. Grimes } 5666c84de02SJohn Baldwin #endif 56728b325aaSDon Lewis /* 568c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 569c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 57028b325aaSDon Lewis * 571c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 572c1e2d386SNate Lawson * which may grab the process lock. 57328b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 57428b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 57528b325aaSDon Lewis */ 57628b325aaSDon Lewis PROC_UNLOCK(p); 577c1e2d386SNate Lawson setugidsafety(td); 578e983a376SJacques Vidrine error = fdcheckstd(td); 57963c9e754SJohn Baldwin if (error != 0) 58069be5db9SAlfred Perlstein goto done1; 581e1b1aa3bSJohn Baldwin PROC_LOCK(p); 582c52007c2SDavid Greenman /* 583c52007c2SDavid Greenman * Set the new credentials. 584c52007c2SDavid Greenman */ 5859b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 586c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5871419eacbSAlfred Perlstein change_euid(newcred, euip); 588c52007c2SDavid Greenman if (attr.va_mode & VSGID) 589b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 590ccafe7ebSRobert Watson #ifdef MAC 591670cb89bSRobert Watson if (will_transition) { 592670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 593eca8a663SRobert Watson interplabel, imgp); 594670cb89bSRobert Watson } 595ccafe7ebSRobert Watson #endif 5969b3b1c5fSJohn Baldwin /* 5979b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 598ccafe7ebSRobert Watson * 599ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 600ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 601ccafe7ebSRobert Watson * though maybe it shouldn't. 6029b3b1c5fSJohn Baldwin */ 6039b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 6049b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 6059b3b1c5fSJohn Baldwin p->p_ucred = newcred; 6069b3b1c5fSJohn Baldwin newcred = NULL; 607c52007c2SDavid Greenman } else { 608b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 609b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 610c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 61126f9a767SRodney W. Grimes /* 612c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 613b1fc0ec1SRobert Watson * 614b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 6159b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 6169b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 6179b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 6189b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 6199b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 6209b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 6219b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 62226f9a767SRodney W. Grimes */ 623b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 624b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 6259b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 626b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 627b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 628b1fc0ec1SRobert Watson p->p_ucred = newcred; 6299b3b1c5fSJohn Baldwin newcred = NULL; 6309b3b1c5fSJohn Baldwin } 631b1fc0ec1SRobert Watson } 63226f9a767SRodney W. Grimes 63326f9a767SRodney W. Grimes /* 634619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 635619eb6e5SJeff Roberson * to locking the proc lock. 6362a531c80SDavid Greenman */ 6379b3b1c5fSJohn Baldwin textvp = p->p_textvp; 6382a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 6392a531c80SDavid Greenman 6402a531c80SDavid Greenman /* 6419ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 6429ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 643cb679c38SJonathan Lemon */ 644ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 6459ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 646cb679c38SJonathan Lemon 647cb679c38SJonathan Lemon /* 64826f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 64926f9a767SRodney W. Grimes * can be set before the program executes. 650906ac69dSDavid Xu * Use tdsignal to deliver signal to current thread, use 651906ac69dSDavid Xu * psignal may cause the signal to be delivered to wrong thread 652906ac69dSDavid Xu * because that thread will exit, remember we are going to enter 653906ac69dSDavid Xu * single thread mode. 65426f9a767SRodney W. Grimes */ 65526f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 656906ac69dSDavid Xu tdsignal(td, SIGTRAP, SIGTARGET_TD); 65726f9a767SRodney W. Grimes 65826f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 65926f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 66026f9a767SRodney W. Grimes 661b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 6629b3b1c5fSJohn Baldwin oldargs = p->p_args; 663b9df5231SPoul-Henning Kamp p->p_args = NULL; 664b9df5231SPoul-Henning Kamp 665e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 666e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 667610ecfe0SMaxim Sobolev bcopy(imgp->args->begin_argv, newargs->ar_args, i); 668e1b1aa3bSJohn Baldwin p->p_args = newargs; 669e1b1aa3bSJohn Baldwin newargs = NULL; 670e1b1aa3bSJohn Baldwin } 671ebccf1e3SJoseph Koshy 672ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 673ebccf1e3SJoseph Koshy /* 674f263522aSJoseph Koshy * Check if system-wide sampling is in effect or if the 675f263522aSJoseph Koshy * current process is using PMCs. If so, do exec() time 676ebccf1e3SJoseph Koshy * processing. This processing needs to happen AFTER the 677ebccf1e3SJoseph Koshy * P_INEXEC flag is cleared. 678ebccf1e3SJoseph Koshy * 679ebccf1e3SJoseph Koshy * The proc lock needs to be released before taking the PMC 680ebccf1e3SJoseph Koshy * SX. 681ebccf1e3SJoseph Koshy */ 682f263522aSJoseph Koshy if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) { 683e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 684ebccf1e3SJoseph Koshy PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, 685ebccf1e3SJoseph Koshy (void *) &credential_changing); 686ebccf1e3SJoseph Koshy } else 687ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 688ebccf1e3SJoseph Koshy #else /* !HWPMC_HOOKS */ 689ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 690ebccf1e3SJoseph Koshy #endif 691e1b1aa3bSJohn Baldwin 692e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 6933ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 6943ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 6953ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 6963ebc1248SPeter Wemm else 697bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 698bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 699e913ca22SDoug Rabson 7006341095eSKen Smith /* 7016341095eSKen Smith * Here we should update the access time of the file. This must 7026341095eSKen Smith * be implemented by the underlying filesystem in the same way as 7036341095eSKen Smith * access timestamps for a VOP_READ() because we want to avoid 7046341095eSKen Smith * blocking and/or I/O, and have not called vn_start_write(). 7056341095eSKen Smith */ 7066341095eSKen Smith if ((imgp->vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { 7076341095eSKen Smith VATTR_NULL(&atimeattr); 7086341095eSKen Smith atimeattr.va_vaflags |= VA_EXECVE_ATIME; 7096341095eSKen Smith (void)VOP_SETATTR(imgp->vp, &atimeattr, td->td_ucred, td); 7106341095eSKen Smith } 7116341095eSKen Smith 71269be5db9SAlfred Perlstein done1: 7139b3b1c5fSJohn Baldwin /* 7149b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 7159b3b1c5fSJohn Baldwin */ 7161419eacbSAlfred Perlstein uifree(euip); 7179b3b1c5fSJohn Baldwin if (newcred == NULL) 7189b3b1c5fSJohn Baldwin crfree(oldcred); 7199b3b1c5fSJohn Baldwin else 7209b3b1c5fSJohn Baldwin crfree(newcred); 7219b3b1c5fSJohn Baldwin /* 7229b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 7239b3b1c5fSJohn Baldwin */ 7249b3b1c5fSJohn Baldwin if (textvp != NULL) 7259b3b1c5fSJohn Baldwin vrele(textvp); 726619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 727619eb6e5SJeff Roberson vrele(ndp->ni_vp); 7286c84de02SJohn Baldwin #ifdef KTRACE 7299b3b1c5fSJohn Baldwin if (tracevp != NULL) 7309b3b1c5fSJohn Baldwin vrele(tracevp); 731a5881ea5SJohn Baldwin if (tracecred != NULL) 732a5881ea5SJohn Baldwin crfree(tracecred); 7336c84de02SJohn Baldwin #endif 73469be5db9SAlfred Perlstein if (oldargs != NULL) 7359b3b1c5fSJohn Baldwin pargs_drop(oldargs); 73669be5db9SAlfred Perlstein if (newargs != NULL) 73769be5db9SAlfred Perlstein pargs_drop(newargs); 73890af4afaSJohn Baldwin if (oldsigacts != NULL) 73990af4afaSJohn Baldwin sigacts_free(oldsigacts); 740b9df5231SPoul-Henning Kamp 7411616db3cSJohn Dyson exec_fail_dealloc: 7421616db3cSJohn Dyson 74326f9a767SRodney W. Grimes /* 74426f9a767SRodney W. Grimes * free various allocated resources 74526f9a767SRodney W. Grimes */ 746d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7471616db3cSJohn Dyson exec_unmap_first_page(imgp); 74826f9a767SRodney W. Grimes 749d6c847f3SBruce Evans if (imgp->vp != NULL) { 750762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 751619eb6e5SJeff Roberson vput(imgp->vp); 752a3cf6ebaSDavid Greenman } 75326f9a767SRodney W. Grimes 754d6c847f3SBruce Evans if (imgp->object != NULL) 7550b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 7560b2ed1aeSJeff Roberson 757d1989db5SRobert Drehmel if (error == 0) { 758d1989db5SRobert Drehmel /* 759d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 760d1989db5SRobert Drehmel * the S_EXEC bit set. 761d1989db5SRobert Drehmel */ 762d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 763116734c4SMatthew Dillon goto done2; 764d1989db5SRobert Drehmel } 7651616db3cSJohn Dyson 76626f9a767SRodney W. Grimes exec_fail: 7679ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 7689ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 7699ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 7709ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 7719ca45e81SDag-Erling Smørgrav 772c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 77326f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 774670cb89bSRobert Watson #ifdef MAC 775670cb89bSRobert Watson mac_execve_exit(imgp); 776eca8a663SRobert Watson if (interplabel != NULL) 777eca8a663SRobert Watson mac_vnode_label_free(interplabel); 778670cb89bSRobert Watson #endif 779532c491bSJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 780b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 78126f9a767SRodney W. Grimes /* NOT REACHED */ 782116734c4SMatthew Dillon error = 0; 78326f9a767SRodney W. Grimes } 784116734c4SMatthew Dillon done2: 785670cb89bSRobert Watson #ifdef MAC 786670cb89bSRobert Watson mac_execve_exit(imgp); 787eca8a663SRobert Watson if (interplabel != NULL) 788eca8a663SRobert Watson mac_vnode_label_free(interplabel); 789670cb89bSRobert Watson #endif 790269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 791116734c4SMatthew Dillon return (error); 79226f9a767SRodney W. Grimes } 79326f9a767SRodney W. Grimes 7941616db3cSJohn Dyson int 7951616db3cSJohn Dyson exec_map_first_page(imgp) 7961616db3cSJohn Dyson struct image_params *imgp; 7971616db3cSJohn Dyson { 798d8aad40cSJohn Baldwin int rv, i; 79995461b45SJohn Dyson int initial_pagein; 80095461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 8011616db3cSJohn Dyson vm_object_t object; 8021616db3cSJohn Dyson 803d6c847f3SBruce Evans if (imgp->firstpage != NULL) 8041616db3cSJohn Dyson exec_unmap_first_page(imgp); 8051616db3cSJohn Dyson 8068516dd18SPoul-Henning Kamp object = imgp->vp->v_object; 8074d7d2993SJeff Roberson if (object == NULL) 8084d7d2993SJeff Roberson return (EACCES); 809fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 81095461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 81195461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 81295461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 81395461b45SJohn Dyson if (initial_pagein > object->size) 81495461b45SJohn Dyson initial_pagein = object->size; 81595461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 816d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 8176ec2fca5SAlan Cox if (ma[i]->valid) 8186ec2fca5SAlan Cox break; 819ee113343SAlan Cox vm_page_lock_queues(); 82006fa71cdSAlan Cox if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) { 82106fa71cdSAlan Cox vm_page_unlock_queues(); 82206fa71cdSAlan Cox break; 82306fa71cdSAlan Cox } 824e69763a3SDoug Rabson vm_page_busy(ma[i]); 825ee113343SAlan Cox vm_page_unlock_queues(); 82695461b45SJohn Dyson } else { 827ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 828ca0387efSJake Burkholder VM_ALLOC_NORMAL); 82995461b45SJohn Dyson if (ma[i] == NULL) 83095461b45SJohn Dyson break; 83195461b45SJohn Dyson } 83295461b45SJohn Dyson } 83395461b45SJohn Dyson initial_pagein = i; 83495461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 83595461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 836ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 837ca0387efSJake Burkholder (ma[0]->valid == 0)) { 838ecbb00a2SDoug Rabson if (ma[0]) { 8396ec2fca5SAlan Cox vm_page_lock_queues(); 8404fec79beSAlan Cox pmap_remove_all(ma[0]); 8418f9110f6SJohn Dyson vm_page_free(ma[0]); 84206fa71cdSAlan Cox vm_page_unlock_queues(); 8436ec2fca5SAlan Cox } 84406fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 845a7cddfedSJake Burkholder return (EIO); 8461616db3cSJohn Dyson } 8471616db3cSJohn Dyson } 8486ec2fca5SAlan Cox vm_page_lock_queues(); 849148b3f62SAlan Cox vm_page_hold(ma[0]); 850e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 85197646f56SAlan Cox vm_page_unlock_queues(); 8526ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 8531616db3cSJohn Dyson 85459c8bc40SAlan Cox imgp->firstpage = sf_buf_alloc(ma[0], 0); 85559c8bc40SAlan Cox imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 8561616db3cSJohn Dyson 857a7cddfedSJake Burkholder return (0); 8581616db3cSJohn Dyson } 8591616db3cSJohn Dyson 8601616db3cSJohn Dyson void 8611616db3cSJohn Dyson exec_unmap_first_page(imgp) 8621616db3cSJohn Dyson struct image_params *imgp; 8631616db3cSJohn Dyson { 86459c8bc40SAlan Cox vm_page_t m; 86523955314SAlfred Perlstein 866d6c847f3SBruce Evans if (imgp->firstpage != NULL) { 86759c8bc40SAlan Cox m = sf_buf_page(imgp->firstpage); 86859c8bc40SAlan Cox sf_buf_free(imgp->firstpage); 8691616db3cSJohn Dyson imgp->firstpage = NULL; 87059c8bc40SAlan Cox vm_page_lock_queues(); 87159c8bc40SAlan Cox vm_page_unhold(m); 87259c8bc40SAlan Cox vm_page_unlock_queues(); 8731616db3cSJohn Dyson } 8741616db3cSJohn Dyson } 8751616db3cSJohn Dyson 87626f9a767SRodney W. Grimes /* 87726f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 87826f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 87926f9a767SRodney W. Grimes * automatically in trap.c. 88026f9a767SRodney W. Grimes */ 88126f9a767SRodney W. Grimes int 88205ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 883c52007c2SDavid Greenman struct image_params *imgp; 88405ba50f5SJake Burkholder struct sysentvec *sv; 88526f9a767SRodney W. Grimes { 88626f9a767SRodney W. Grimes int error; 8875e20c11fSAlan Cox struct proc *p = imgp->proc; 8885e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 88905ba50f5SJake Burkholder vm_offset_t stack_addr; 89005ba50f5SJake Burkholder vm_map_t map; 89126f9a767SRodney W. Grimes 892c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 89326f9a767SRodney W. Grimes 894a5bdcb2aSPeter Wemm /* Called with Giant held, do not depend on it! */ 89575b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exec, p); 8966f5dafeaSAlan Cox 8976f5dafeaSAlan Cox /* 898c460ac3aSPeter Wemm * Here is as good a place as any to do any resource limit cleanups. 899c460ac3aSPeter Wemm * This is needed if a 64 bit binary exec's a 32 bit binary - the 900c460ac3aSPeter Wemm * data size limit may need to be changed to a value that makes 901c460ac3aSPeter Wemm * sense for the 32 bit binary. 902c460ac3aSPeter Wemm */ 903d6c847f3SBruce Evans if (sv->sv_fixlimits != NULL) 904c460ac3aSPeter Wemm sv->sv_fixlimits(imgp); 905c460ac3aSPeter Wemm 906c460ac3aSPeter Wemm /* 907af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 908af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 909af9ec885SJohn Dyson * not disrupted 910af9ec885SJohn Dyson */ 91105ba50f5SJake Burkholder map = &vmspace->vm_map; 91205ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 91305ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 9143db161e0SMatthew Dillon shmexit(vmspace); 91505ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 91605ba50f5SJake Burkholder vm_map_max(map)); 91705ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 918af9ec885SJohn Dyson } else { 91905ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 9205e20c11fSAlan Cox vmspace = p->p_vmspace; 92105ba50f5SJake Burkholder map = &vmspace->vm_map; 922af9ec885SJohn Dyson } 92326f9a767SRodney W. Grimes 92426f9a767SRodney W. Grimes /* Allocate a new stack */ 925fd75d710SMarcel Moolenaar stack_addr = sv->sv_usrstack - maxssiz; 92605ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 927fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 9282267af78SJulian Elischer if (error) 9292267af78SJulian Elischer return (error); 9302267af78SJulian Elischer 93163c47a5cSDoug Rabson #ifdef __ia64__ 932fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 933bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 934fd75d710SMarcel Moolenaar error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 935fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 936fd75d710SMarcel Moolenaar if (error) 937fd75d710SMarcel Moolenaar return (error); 93863c47a5cSDoug Rabson #endif 93963c47a5cSDoug Rabson 9402267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 9412267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 9422267af78SJulian Elischer * process stack so we can check the stack rlimit. 9432267af78SJulian Elischer */ 944cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 94505ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 94626f9a767SRodney W. Grimes 94726f9a767SRodney W. Grimes return (0); 94826f9a767SRodney W. Grimes } 94926f9a767SRodney W. Grimes 95026f9a767SRodney W. Grimes /* 95126f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 95226f9a767SRodney W. Grimes * address space into the temporary string buffer. 95326f9a767SRodney W. Grimes */ 95426f9a767SRodney W. Grimes int 955610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname, 956610ecfe0SMaxim Sobolev enum uio_seg segflg, char **argv, char **envv) 95726f9a767SRodney W. Grimes { 95826f9a767SRodney W. Grimes char *argp, *envp; 959ecbb00a2SDoug Rabson int error; 960ecbb00a2SDoug Rabson size_t length; 96126f9a767SRodney W. Grimes 962610ecfe0SMaxim Sobolev error = 0; 963610ecfe0SMaxim Sobolev 964610ecfe0SMaxim Sobolev bzero(args, sizeof(*args)); 965610ecfe0SMaxim Sobolev if (argv == NULL) 966610ecfe0SMaxim Sobolev return (EFAULT); 96726f9a767SRodney W. Grimes /* 968610ecfe0SMaxim Sobolev * Allocate temporary demand zeroed space for argument and 96990dc539bSMaxim Sobolev * environment strings: 97090dc539bSMaxim Sobolev * 97190dc539bSMaxim Sobolev * o ARG_MAX for argument and environment; 97290dc539bSMaxim Sobolev * o MAXSHELLCMDLEN for the name of interpreters. 97326f9a767SRodney W. Grimes */ 97490dc539bSMaxim Sobolev args->buf = (char *) kmem_alloc_wait(exec_map, 97590dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 976610ecfe0SMaxim Sobolev if (args->buf == NULL) 977610ecfe0SMaxim Sobolev return (ENOMEM); 978610ecfe0SMaxim Sobolev args->begin_argv = args->buf; 979610ecfe0SMaxim Sobolev args->endp = args->begin_argv; 980610ecfe0SMaxim Sobolev args->stringspace = ARG_MAX; 98126f9a767SRodney W. Grimes 982610ecfe0SMaxim Sobolev args->fname = args->buf + ARG_MAX; 98326f9a767SRodney W. Grimes 984610ecfe0SMaxim Sobolev /* 985610ecfe0SMaxim Sobolev * Copy the file name. 986610ecfe0SMaxim Sobolev */ 987610ecfe0SMaxim Sobolev error = (segflg == UIO_SYSSPACE) ? 988610ecfe0SMaxim Sobolev copystr(fname, args->fname, PATH_MAX, &length) : 989610ecfe0SMaxim Sobolev copyinstr(fname, args->fname, PATH_MAX, &length); 990c30af532SMaxim Sobolev if (error != 0) 9910fd1a014SDavid Greenman return (error); 99226f9a767SRodney W. Grimes 993610ecfe0SMaxim Sobolev /* 994610ecfe0SMaxim Sobolev * extract arguments first 995610ecfe0SMaxim Sobolev */ 996610ecfe0SMaxim Sobolev while ((argp = (caddr_t) (intptr_t) fuword(argv++))) { 997610ecfe0SMaxim Sobolev if (argp == (caddr_t) -1) 998610ecfe0SMaxim Sobolev return (EFAULT); 999610ecfe0SMaxim Sobolev if ((error = copyinstr(argp, args->endp, 1000610ecfe0SMaxim Sobolev args->stringspace, &length))) { 1001610ecfe0SMaxim Sobolev if (error == ENAMETOOLONG) 1002610ecfe0SMaxim Sobolev return (E2BIG); 1003610ecfe0SMaxim Sobolev return (error); 1004610ecfe0SMaxim Sobolev } 1005610ecfe0SMaxim Sobolev args->stringspace -= length; 1006610ecfe0SMaxim Sobolev args->endp += length; 1007610ecfe0SMaxim Sobolev args->argc++; 1008610ecfe0SMaxim Sobolev } 1009610ecfe0SMaxim Sobolev 1010610ecfe0SMaxim Sobolev args->begin_envv = args->endp; 1011b9df5231SPoul-Henning Kamp 101226f9a767SRodney W. Grimes /* 101326f9a767SRodney W. Grimes * extract environment strings 101426f9a767SRodney W. Grimes */ 10150fd1a014SDavid Greenman if (envv) { 1016aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 101726f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 101826f9a767SRodney W. Grimes return (EFAULT); 1019610ecfe0SMaxim Sobolev if ((error = copyinstr(envp, args->endp, 1020610ecfe0SMaxim Sobolev args->stringspace, &length))) { 10210fd1a014SDavid Greenman if (error == ENAMETOOLONG) 102226f9a767SRodney W. Grimes return (E2BIG); 10230fd1a014SDavid Greenman return (error); 10240fd1a014SDavid Greenman } 1025610ecfe0SMaxim Sobolev args->stringspace -= length; 1026610ecfe0SMaxim Sobolev args->endp += length; 1027610ecfe0SMaxim Sobolev args->envc++; 102826f9a767SRodney W. Grimes } 10290fd1a014SDavid Greenman } 103026f9a767SRodney W. Grimes 103126f9a767SRodney W. Grimes return (0); 103226f9a767SRodney W. Grimes } 103326f9a767SRodney W. Grimes 1034610ecfe0SMaxim Sobolev void 1035610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args) 1036610ecfe0SMaxim Sobolev { 1037610ecfe0SMaxim Sobolev 1038610ecfe0SMaxim Sobolev if (args->buf) { 103990dc539bSMaxim Sobolev kmem_free_wakeup(exec_map, (vm_offset_t)args->buf, 104090dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 1041610ecfe0SMaxim Sobolev args->buf = NULL; 1042610ecfe0SMaxim Sobolev } 1043610ecfe0SMaxim Sobolev } 1044610ecfe0SMaxim Sobolev 104526f9a767SRodney W. Grimes /* 104626f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 104726f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 104826f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 104926f9a767SRodney W. Grimes */ 1050654f6be1SBruce Evans register_t * 1051c52007c2SDavid Greenman exec_copyout_strings(imgp) 1052c52007c2SDavid Greenman struct image_params *imgp; 105326f9a767SRodney W. Grimes { 105426f9a767SRodney W. Grimes int argc, envc; 105526f9a767SRodney W. Grimes char **vectp; 105626f9a767SRodney W. Grimes char *stringp, *destp; 1057654f6be1SBruce Evans register_t *stack_base; 105893f6448cSDavid Greenman struct ps_strings *arginfo; 1059f3bec5d7SJake Burkholder struct proc *p; 1060d66a5066SPeter Wemm int szsigcode; 106126f9a767SRodney W. Grimes 106226f9a767SRodney W. Grimes /* 106326f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 1064d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 106526f9a767SRodney W. Grimes */ 1066f3bec5d7SJake Burkholder p = imgp->proc; 1067f3bec5d7SJake Burkholder szsigcode = 0; 106805ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1069f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1070f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1071d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 1072610ecfe0SMaxim Sobolev roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 10731ed012f9SPeter Wemm 107426f9a767SRodney W. Grimes /* 1075d66a5066SPeter Wemm * install sigcode 1076d66a5066SPeter Wemm */ 1077d66a5066SPeter Wemm if (szsigcode) 1078f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1079f3bec5d7SJake Burkholder szsigcode), szsigcode); 1080d66a5066SPeter Wemm 1081d66a5066SPeter Wemm /* 1082e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1083e1743d02SSøren Schmidt * on the stack. 1084e1743d02SSøren Schmidt */ 1085b9a22da4STakanori Watanabe if (imgp->auxargs) { 1086e1743d02SSøren Schmidt /* 1087b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1088b9a22da4STakanori Watanabe * lower compatibility. 1089b9a22da4STakanori Watanabe */ 1090ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1091ca0387efSJake Burkholder (AT_COUNT * 2); 1092b9a22da4STakanori Watanabe /* 1093b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1094b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1095b9a22da4STakanori Watanabe * for argument of Runtime loader. 1096e1743d02SSøren Schmidt */ 1097610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + 1098610ecfe0SMaxim Sobolev imgp->args->envc + 2 + imgp->auxarg_size) * 1099610ecfe0SMaxim Sobolev sizeof(char *)); 1100b9a22da4STakanori Watanabe 1101610ecfe0SMaxim Sobolev } else { 1102e1743d02SSøren Schmidt /* 1103b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1104b9a22da4STakanori Watanabe * the arg and env vector sets 110526f9a767SRodney W. Grimes */ 1106610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * 1107ca0387efSJake Burkholder sizeof(char *)); 1108610ecfe0SMaxim Sobolev } 110926f9a767SRodney W. Grimes 111026f9a767SRodney W. Grimes /* 111126f9a767SRodney W. Grimes * vectp also becomes our initial stack base 111226f9a767SRodney W. Grimes */ 1113654f6be1SBruce Evans stack_base = (register_t *)vectp; 111426f9a767SRodney W. Grimes 1115610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 1116610ecfe0SMaxim Sobolev argc = imgp->args->argc; 1117610ecfe0SMaxim Sobolev envc = imgp->args->envc; 111826f9a767SRodney W. Grimes 111993f6448cSDavid Greenman /* 11203aed948bSDavid Greenman * Copy out strings - arguments and environment. 112193f6448cSDavid Greenman */ 1122610ecfe0SMaxim Sobolev copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 112393f6448cSDavid Greenman 112493f6448cSDavid Greenman /* 11253aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 11263aed948bSDavid Greenman */ 1127aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 11283aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 11293aed948bSDavid Greenman 11303aed948bSDavid Greenman /* 11313aed948bSDavid Greenman * Fill in argument portion of vector table. 113293f6448cSDavid Greenman */ 113326f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1134aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11353aed948bSDavid Greenman while (*stringp++ != 0) 11363aed948bSDavid Greenman destp++; 11373aed948bSDavid Greenman destp++; 113826f9a767SRodney W. Grimes } 113926f9a767SRodney W. Grimes 11401a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 11416ab46d52SBruce Evans suword(vectp++, 0); 114226f9a767SRodney W. Grimes 1143aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 11443aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 114593f6448cSDavid Greenman 114693f6448cSDavid Greenman /* 11473aed948bSDavid Greenman * Fill in environment portion of vector table. 114893f6448cSDavid Greenman */ 114926f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1150aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11513aed948bSDavid Greenman while (*stringp++ != 0) 11523aed948bSDavid Greenman destp++; 11533aed948bSDavid Greenman destp++; 115426f9a767SRodney W. Grimes } 115526f9a767SRodney W. Grimes 115626f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 11576ab46d52SBruce Evans suword(vectp, 0); 115826f9a767SRodney W. Grimes 115926f9a767SRodney W. Grimes return (stack_base); 116026f9a767SRodney W. Grimes } 116126f9a767SRodney W. Grimes 116226f9a767SRodney W. Grimes /* 116326f9a767SRodney W. Grimes * Check permissions of file to execute. 1164cf64863aSRobert Watson * Called with imgp->vp locked. 116526f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 116626f9a767SRodney W. Grimes */ 1167c8a79999SPeter Wemm int 1168c52007c2SDavid Greenman exec_check_permissions(imgp) 1169c52007c2SDavid Greenman struct image_params *imgp; 117026f9a767SRodney W. Grimes { 1171c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1172c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1173a854ed98SJohn Baldwin struct thread *td; 117426f9a767SRodney W. Grimes int error; 117526f9a767SRodney W. Grimes 1176a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1177339b79b9SRobert Watson 1178ec35c2afSRobert Watson /* Get file attributes */ 1179ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1180ec35c2afSRobert Watson if (error) 1181ec35c2afSRobert Watson return (error); 1182ec35c2afSRobert Watson 1183339b79b9SRobert Watson #ifdef MAC 1184670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1185339b79b9SRobert Watson if (error) 1186339b79b9SRobert Watson return (error); 1187339b79b9SRobert Watson #endif 1188339b79b9SRobert Watson 118926f9a767SRodney W. Grimes /* 119026f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 119126f9a767SRodney W. Grimes * file resides on. 119226f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 119326f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 119426f9a767SRodney W. Grimes * file really is executable. 119526f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 119626f9a767SRodney W. Grimes */ 1197c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 119826f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1199a854ed98SJohn Baldwin (attr->va_type != VREG)) 120026f9a767SRodney W. Grimes return (EACCES); 120126f9a767SRodney W. Grimes 120226f9a767SRodney W. Grimes /* 120326f9a767SRodney W. Grimes * Zero length files can't be exec'd 120426f9a767SRodney W. Grimes */ 120526f9a767SRodney W. Grimes if (attr->va_size == 0) 120626f9a767SRodney W. Grimes return (ENOEXEC); 120726f9a767SRodney W. Grimes 120826f9a767SRodney W. Grimes /* 120926f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 121026f9a767SRodney W. Grimes */ 1211a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 121226f9a767SRodney W. Grimes if (error) 121326f9a767SRodney W. Grimes return (error); 121426f9a767SRodney W. Grimes 12156d5a0a8cSDavid Greenman /* 12166d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 12176d5a0a8cSDavid Greenman * if there are any. 12186d5a0a8cSDavid Greenman */ 12196d5a0a8cSDavid Greenman if (vp->v_writecount) 12206d5a0a8cSDavid Greenman return (ETXTBSY); 12216d5a0a8cSDavid Greenman 12226d5a0a8cSDavid Greenman /* 12236d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 12246d5a0a8cSDavid Greenman * general case). 12256d5a0a8cSDavid Greenman */ 1226a8d43c90SPoul-Henning Kamp error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 122726f9a767SRodney W. Grimes return (error); 1228df8bae1dSRodney W. Grimes } 1229aa855a59SPeter Wemm 1230aa855a59SPeter Wemm /* 1231aa855a59SPeter Wemm * Exec handler registration 1232aa855a59SPeter Wemm */ 1233aa855a59SPeter Wemm int 1234aa855a59SPeter Wemm exec_register(execsw_arg) 1235aa855a59SPeter Wemm const struct execsw *execsw_arg; 1236aa855a59SPeter Wemm { 1237aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1238aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1239aa855a59SPeter Wemm 1240aa855a59SPeter Wemm if (execsw) 1241aa855a59SPeter Wemm for (es = execsw; *es; es++) 1242aa855a59SPeter Wemm count++; 1243a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1244aa855a59SPeter Wemm if (newexecsw == NULL) 1245a7cddfedSJake Burkholder return (ENOMEM); 1246aa855a59SPeter Wemm xs = newexecsw; 1247aa855a59SPeter Wemm if (execsw) 1248aa855a59SPeter Wemm for (es = execsw; *es; es++) 1249aa855a59SPeter Wemm *xs++ = *es; 1250aa855a59SPeter Wemm *xs++ = execsw_arg; 1251aa855a59SPeter Wemm *xs = NULL; 1252aa855a59SPeter Wemm if (execsw) 1253aa855a59SPeter Wemm free(execsw, M_TEMP); 1254aa855a59SPeter Wemm execsw = newexecsw; 1255a7cddfedSJake Burkholder return (0); 1256aa855a59SPeter Wemm } 1257aa855a59SPeter Wemm 1258aa855a59SPeter Wemm int 1259aa855a59SPeter Wemm exec_unregister(execsw_arg) 1260aa855a59SPeter Wemm const struct execsw *execsw_arg; 1261aa855a59SPeter Wemm { 1262aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1263aa855a59SPeter Wemm int count = 1; 1264aa855a59SPeter Wemm 1265aa855a59SPeter Wemm if (execsw == NULL) 1266aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1267aa855a59SPeter Wemm 1268aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1269aa855a59SPeter Wemm if (*es == execsw_arg) 1270aa855a59SPeter Wemm break; 1271aa855a59SPeter Wemm } 1272aa855a59SPeter Wemm if (*es == NULL) 1273a7cddfedSJake Burkholder return (ENOENT); 1274aa855a59SPeter Wemm for (es = execsw; *es; es++) 1275aa855a59SPeter Wemm if (*es != execsw_arg) 1276aa855a59SPeter Wemm count++; 1277a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1278aa855a59SPeter Wemm if (newexecsw == NULL) 1279a7cddfedSJake Burkholder return (ENOMEM); 1280aa855a59SPeter Wemm xs = newexecsw; 1281aa855a59SPeter Wemm for (es = execsw; *es; es++) 1282aa855a59SPeter Wemm if (*es != execsw_arg) 1283aa855a59SPeter Wemm *xs++ = *es; 1284aa855a59SPeter Wemm *xs = NULL; 1285aa855a59SPeter Wemm if (execsw) 1286aa855a59SPeter Wemm free(execsw, M_TEMP); 1287aa855a59SPeter Wemm execsw = newexecsw; 1288a7cddfedSJake Burkholder return (0); 1289aa855a59SPeter Wemm } 1290