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); 898917b8d2SJohn Baldwin static void exec_free_args(struct image_args *); 9005ba50f5SJake Burkholder 919701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9205ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9305ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 94486bddb0SDoug Rabson 959701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9605ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9705ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 9899ac3bc8SPeter Wemm 995dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 1005dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 1015dadd17bSJake Burkholder 102b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 103c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1049701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 105b9df5231SPoul-Henning Kamp 10605ba50f5SJake Burkholder static int 10705ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 10805ba50f5SJake Burkholder { 10905ba50f5SJake Burkholder struct proc *p; 110df7c361eSPeter Wemm int error; 11105ba50f5SJake Burkholder 11205ba50f5SJake Burkholder p = curproc; 113a7bc3102SPeter Wemm #ifdef SCTL_MASK32 114a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 115df7c361eSPeter Wemm unsigned int val; 116df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_psstrings; 117df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 118df7c361eSPeter Wemm } else 119df7c361eSPeter Wemm #endif 120df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 121df7c361eSPeter Wemm sizeof(p->p_sysent->sv_psstrings)); 122df7c361eSPeter Wemm return error; 12305ba50f5SJake Burkholder } 12405ba50f5SJake Burkholder 12505ba50f5SJake Burkholder static int 12605ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12705ba50f5SJake Burkholder { 12805ba50f5SJake Burkholder struct proc *p; 129df7c361eSPeter Wemm int error; 13005ba50f5SJake Burkholder 13105ba50f5SJake Burkholder p = curproc; 132a7bc3102SPeter Wemm #ifdef SCTL_MASK32 133a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 134df7c361eSPeter Wemm unsigned int val; 135df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_usrstack; 136df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 137df7c361eSPeter Wemm } else 138df7c361eSPeter Wemm #endif 139df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 140df7c361eSPeter Wemm sizeof(p->p_sysent->sv_usrstack)); 141df7c361eSPeter Wemm return error; 14205ba50f5SJake Burkholder } 14305ba50f5SJake Burkholder 1445dadd17bSJake Burkholder static int 1455dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1465dadd17bSJake Burkholder { 1475dadd17bSJake Burkholder struct proc *p; 1485dadd17bSJake Burkholder 1495dadd17bSJake Burkholder p = curproc; 1505dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1515dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1525dadd17bSJake Burkholder } 1535dadd17bSJake Burkholder 15499ac3bc8SPeter Wemm /* 155aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 156aa855a59SPeter Wemm * double pointer here. 157df8bae1dSRodney W. Grimes */ 158aa855a59SPeter Wemm static const struct execsw **execsw; 15926f9a767SRodney W. Grimes 160ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 161ca46e90eSBruce Evans struct execve_args { 162ca46e90eSBruce Evans char *fname; 163ca46e90eSBruce Evans char **argv; 164ca46e90eSBruce Evans char **envv; 165ca46e90eSBruce Evans }; 166ca46e90eSBruce Evans #endif 167ca46e90eSBruce Evans 168ca46e90eSBruce Evans /* 169ca46e90eSBruce Evans * MPSAFE 170ca46e90eSBruce Evans */ 171ca46e90eSBruce Evans int 172ca46e90eSBruce Evans execve(td, uap) 173ca46e90eSBruce Evans struct thread *td; 174ca46e90eSBruce Evans struct execve_args /* { 175ca46e90eSBruce Evans char *fname; 176ca46e90eSBruce Evans char **argv; 177ca46e90eSBruce Evans char **envv; 178ca46e90eSBruce Evans } */ *uap; 179ca46e90eSBruce Evans { 180610ecfe0SMaxim Sobolev int error; 181610ecfe0SMaxim Sobolev struct image_args args; 182ca46e90eSBruce Evans 183610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 184610ecfe0SMaxim Sobolev uap->argv, uap->envv); 185610ecfe0SMaxim Sobolev if (error == 0) 186610ecfe0SMaxim Sobolev error = kern_execve(td, &args, NULL); 187610ecfe0SMaxim Sobolev return (error); 188ca46e90eSBruce Evans } 189ca46e90eSBruce Evans 190ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 191ca46e90eSBruce Evans struct __mac_execve_args { 192ca46e90eSBruce Evans char *fname; 193ca46e90eSBruce Evans char **argv; 194ca46e90eSBruce Evans char **envv; 195ca46e90eSBruce Evans struct mac *mac_p; 196ca46e90eSBruce Evans }; 197ca46e90eSBruce Evans #endif 198ca46e90eSBruce Evans 199ca46e90eSBruce Evans /* 200ca46e90eSBruce Evans * MPSAFE 201ca46e90eSBruce Evans */ 202ca46e90eSBruce Evans int 203ca46e90eSBruce Evans __mac_execve(td, uap) 204ca46e90eSBruce Evans struct thread *td; 205ca46e90eSBruce Evans struct __mac_execve_args /* { 206ca46e90eSBruce Evans char *fname; 207ca46e90eSBruce Evans char **argv; 208ca46e90eSBruce Evans char **envv; 209ca46e90eSBruce Evans struct mac *mac_p; 210ca46e90eSBruce Evans } */ *uap; 211ca46e90eSBruce Evans { 212ca46e90eSBruce Evans #ifdef MAC 213610ecfe0SMaxim Sobolev int error; 214610ecfe0SMaxim Sobolev struct image_args args; 215610ecfe0SMaxim Sobolev 216610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 217610ecfe0SMaxim Sobolev uap->argv, uap->envv); 218610ecfe0SMaxim Sobolev if (error == 0) 219610ecfe0SMaxim Sobolev error = kern_execve(td, &args, uap->mac_p); 220610ecfe0SMaxim Sobolev return (error); 221ca46e90eSBruce Evans #else 222ca46e90eSBruce Evans return (ENOSYS); 223ca46e90eSBruce Evans #endif 224ca46e90eSBruce Evans } 225ca46e90eSBruce Evans 22633812c06SColin Percival /* 22733812c06SColin Percival * XXX: kern_execve has the astonishing property of not always 22833812c06SColin Percival * returning to the caller. If sufficiently bad things happen during 22933812c06SColin Percival * the call to do_execve(), it can end up calling exit1(); as a result, 23033812c06SColin Percival * callers must avoid doing anything which they might need to undo 23133812c06SColin Percival * (e.g., allocating memory). 23233812c06SColin Percival */ 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); 24668ff3c24SStephan Uphoff exec_free_args(args); 247906ac69dSDavid Xu return (ERESTART); /* Try again later. */ 248906ac69dSDavid Xu } 249906ac69dSDavid Xu PROC_UNLOCK(p); 250906ac69dSDavid Xu } 251906ac69dSDavid Xu 252610ecfe0SMaxim Sobolev error = do_execve(td, args, mac_p); 253906ac69dSDavid Xu 254906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 255906ac69dSDavid Xu PROC_LOCK(p); 256906ac69dSDavid Xu /* 257906ac69dSDavid Xu * If success, we upgrade to SINGLE_EXIT state to 258906ac69dSDavid Xu * force other threads to suicide. 259906ac69dSDavid Xu */ 260906ac69dSDavid Xu if (error == 0) 261906ac69dSDavid Xu thread_single(SINGLE_EXIT); 262906ac69dSDavid Xu else 263906ac69dSDavid Xu thread_single_end(); 264906ac69dSDavid Xu PROC_UNLOCK(p); 265906ac69dSDavid Xu } 266906ac69dSDavid Xu 267906ac69dSDavid Xu return (error); 268906ac69dSDavid Xu } 269906ac69dSDavid Xu 27026f9a767SRodney W. Grimes /* 271450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 272450ffb44SRobert Watson * userspace pointers from the passed thread. 273116734c4SMatthew Dillon * 274116734c4SMatthew Dillon * MPSAFE 27526f9a767SRodney W. Grimes */ 276450ffb44SRobert Watson static int 277610ecfe0SMaxim Sobolev do_execve(td, args, mac_p) 278b40ce416SJulian Elischer struct thread *td; 279610ecfe0SMaxim Sobolev struct image_args *args; 280670cb89bSRobert Watson struct mac *mac_p; 281df8bae1dSRodney W. Grimes { 282b40ce416SJulian Elischer struct proc *p = td->td_proc; 28326f9a767SRodney W. Grimes struct nameidata nd, *ndp; 2849b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 2851419eacbSAlfred Perlstein struct uidinfo *euip; 286654f6be1SBruce Evans register_t *stack_base; 287bb56ec4aSPoul-Henning Kamp int error, len, i; 288c52007c2SDavid Greenman struct image_params image_params, *imgp; 2899f5c1d19SDiomidis Spinellis struct vattr attr; 2904d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 29169be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 29290af4afaSJohn Baldwin struct sigacts *oldsigacts, *newsigacts; 2936c84de02SJohn Baldwin #ifdef KTRACE 2946c84de02SJohn Baldwin struct vnode *tracevp = NULL; 295a5881ea5SJohn Baldwin struct ucred *tracecred = NULL; 2966c84de02SJohn Baldwin #endif 2976c84de02SJohn Baldwin struct vnode *textvp = NULL; 298d06c0d4dSRobert Watson int credential_changing; 299269576c8SJeff Roberson int vfslocked; 300619eb6e5SJeff Roberson int textset; 301ccafe7ebSRobert Watson #ifdef MAC 302eca8a663SRobert Watson struct label *interplabel = NULL; 303eca8a663SRobert Watson int will_transition; 304ccafe7ebSRobert Watson #endif 30515139246SJoseph Koshy #ifdef HWPMC_HOOKS 30615139246SJoseph Koshy struct pmckern_procexec pe; 30715139246SJoseph Koshy #endif 30826f9a767SRodney W. Grimes 309532c491bSJeff Roberson vfslocked = 0; 310c52007c2SDavid Greenman imgp = &image_params; 311df8bae1dSRodney W. Grimes 3129ca45e81SDag-Erling Smørgrav /* 3139ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 3149ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 3159ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 3169ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 3179ca45e81SDag-Erling Smørgrav * privileges. 3189ca45e81SDag-Erling Smørgrav */ 3199ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 3209ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 32191f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 3229ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 3239ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 3249ca45e81SDag-Erling Smørgrav 325df8bae1dSRodney W. Grimes /* 326c52007c2SDavid Greenman * Initialize part of the common data 327df8bae1dSRodney W. Grimes */ 328c52007c2SDavid Greenman imgp->proc = p; 329670cb89bSRobert Watson imgp->execlabel = NULL; 330c52007c2SDavid Greenman imgp->attr = &attr; 331c52007c2SDavid Greenman imgp->entry_addr = 0; 332c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 333c52007c2SDavid Greenman imgp->interpreted = 0; 33490dc539bSMaxim Sobolev imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX; 335e1743d02SSøren Schmidt imgp->auxargs = NULL; 3361616db3cSJohn Dyson imgp->vp = NULL; 3370b2ed1aeSJeff Roberson imgp->object = NULL; 3381616db3cSJohn Dyson imgp->firstpage = NULL; 3394fe88fe6SJohn Polstra imgp->ps_strings = 0; 340b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 341610ecfe0SMaxim Sobolev imgp->args = args; 34226f9a767SRodney W. Grimes 343670cb89bSRobert Watson #ifdef MAC 344eca8a663SRobert Watson error = mac_execve_enter(imgp, mac_p); 345532c491bSJeff Roberson if (error) 346670cb89bSRobert Watson goto exec_fail; 347670cb89bSRobert Watson #endif 348670cb89bSRobert Watson 34959c8bc40SAlan Cox imgp->image_header = NULL; 35026f9a767SRodney W. Grimes 35126f9a767SRodney W. Grimes /* 35226f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 35326f9a767SRodney W. Grimes * in ni_vp amoung other things. 35426f9a767SRodney W. Grimes */ 35526f9a767SRodney W. Grimes ndp = &nd; 356269576c8SJeff Roberson NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 357610ecfe0SMaxim Sobolev UIO_SYSSPACE, args->fname, td); 35826f9a767SRodney W. Grimes 35926f9a767SRodney W. Grimes interpret: 36026f9a767SRodney W. Grimes error = namei(ndp); 361610ecfe0SMaxim Sobolev if (error) 36226f9a767SRodney W. Grimes goto exec_fail; 36326f9a767SRodney W. Grimes 364269576c8SJeff Roberson vfslocked = NDHASGIANT(ndp); 365c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 36626f9a767SRodney W. Grimes 36726f9a767SRodney W. Grimes /* 3689022e4adSDavid Greenman * Check file permissions (also 'opens' file) 3699022e4adSDavid Greenman */ 370c52007c2SDavid Greenman error = exec_check_permissions(imgp); 371619eb6e5SJeff Roberson if (error) 37226f9a767SRodney W. Grimes goto exec_fail_dealloc; 373619eb6e5SJeff Roberson 3748516dd18SPoul-Henning Kamp imgp->object = imgp->vp->v_object; 3758516dd18SPoul-Henning Kamp if (imgp->object != NULL) 3760b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 37726f9a767SRodney W. Grimes 378619eb6e5SJeff Roberson /* 379619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 380619eb6e5SJeff Roberson * activating it. 381619eb6e5SJeff Roberson * 382619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 383619eb6e5SJeff Roberson * actually an executable image. 384619eb6e5SJeff Roberson */ 385619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 386619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 387619eb6e5SJeff Roberson 3881616db3cSJohn Dyson error = exec_map_first_page(imgp); 3896d5a0a8cSDavid Greenman if (error) 39026f9a767SRodney W. Grimes goto exec_fail_dealloc; 39126f9a767SRodney W. Grimes 39226f9a767SRodney W. Grimes /* 393d323ddf3SMatthew Dillon * If the current process has a special image activator it 394d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 395d323ddf3SMatthew Dillon * scripts differently. 39626f9a767SRodney W. Grimes */ 397d323ddf3SMatthew Dillon error = -1; 398d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 399d323ddf3SMatthew Dillon error = img_first(imgp); 400d323ddf3SMatthew Dillon 401d323ddf3SMatthew Dillon /* 402d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 403d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 404d323ddf3SMatthew Dillon * and an error otherwise. 405d323ddf3SMatthew Dillon */ 406d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 407d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 408d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 409d323ddf3SMatthew Dillon continue; 410d323ddf3SMatthew Dillon } 411c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 412d323ddf3SMatthew Dillon } 413d323ddf3SMatthew Dillon 414d323ddf3SMatthew Dillon if (error) { 415619eb6e5SJeff Roberson if (error == -1) { 416619eb6e5SJeff Roberson if (textset == 0) 417619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 418d323ddf3SMatthew Dillon error = ENOEXEC; 419619eb6e5SJeff Roberson } 42026f9a767SRodney W. Grimes goto exec_fail_dealloc; 421d323ddf3SMatthew Dillon } 422d323ddf3SMatthew Dillon 423d323ddf3SMatthew Dillon /* 424d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 425d323ddf3SMatthew Dillon * activate the interpreter. 426d323ddf3SMatthew Dillon */ 427c52007c2SDavid Greenman if (imgp->interpreted) { 4281616db3cSJohn Dyson exec_unmap_first_page(imgp); 429619eb6e5SJeff Roberson /* 430619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 431619eb6e5SJeff Roberson * period before we determine that something is a script where 432619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 433619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 434619eb6e5SJeff Roberson */ 435619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 436762e6b85SEivind Eklund /* free name buffer and old vnode */ 437762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 438670cb89bSRobert Watson #ifdef MAC 439eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 440eca8a663SRobert Watson mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel); 441670cb89bSRobert Watson #endif 442619eb6e5SJeff Roberson vput(ndp->ni_vp); 4430b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 4440b2ed1aeSJeff Roberson imgp->object = NULL; 445269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 446532c491bSJeff Roberson vfslocked = 0; 44726f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 448269576c8SJeff Roberson NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 449b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 45026f9a767SRodney W. Grimes goto interpret; 45126f9a767SRodney W. Grimes } 45226f9a767SRodney W. Grimes 45326f9a767SRodney W. Grimes /* 45426f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 45526f9a767SRodney W. Grimes */ 4563ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 4573ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 4583ebc1248SPeter Wemm else 459c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes /* 4621e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 4631e1e0b44SSøren Schmidt * let it do the stack setup. 4641e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 46526f9a767SRodney W. Grimes */ 466d6c847f3SBruce Evans if (p->p_sysent->sv_fixup != NULL) 467c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 4681e1e0b44SSøren Schmidt else 469610ecfe0SMaxim Sobolev suword(--stack_base, imgp->args->argc); 47026f9a767SRodney W. Grimes 471a78e8d2aSDavid Greenman /* 472a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 473a78e8d2aSDavid Greenman * be shared after an exec. 474a78e8d2aSDavid Greenman */ 475c113083cSPoul-Henning Kamp fdunshare(p, td); 476a78e8d2aSDavid Greenman 477333ea485SGuido van Rooij /* 4789b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4799b3b1c5fSJohn Baldwin */ 4809b3b1c5fSJohn Baldwin newcred = crget(); 4811419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 482610ecfe0SMaxim Sobolev i = imgp->args->begin_envv - imgp->args->begin_argv; 4835997cae9SDon Lewis /* Cache arguments if they fit inside our allowance */ 4845997cae9SDon Lewis if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 4859b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4865997cae9SDon Lewis bcopy(imgp->args->begin_argv, newargs->ar_args, i); 4875997cae9SDon Lewis } 4889b3b1c5fSJohn Baldwin 4899b3b1c5fSJohn Baldwin /* close files on exec */ 4909b3b1c5fSJohn Baldwin fdcloseexec(td); 4919b3b1c5fSJohn Baldwin 492619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 493619eb6e5SJeff Roberson VREF(ndp->ni_vp); 494619eb6e5SJeff Roberson 4959b3b1c5fSJohn Baldwin /* 496333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 4978688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 498b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 499333ea485SGuido van Rooij * reset. 500333ea485SGuido van Rooij */ 5019b3b1c5fSJohn Baldwin PROC_LOCK(p); 50290af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 50390af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 5049b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 50590af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 50690af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 5079b3b1c5fSJohn Baldwin PROC_LOCK(p); 50890af4afaSJohn Baldwin p->p_sigacts = newsigacts; 50990af4afaSJohn Baldwin } else 51090af4afaSJohn Baldwin oldsigacts = NULL; 511333ea485SGuido van Rooij 512fdf4e8b3SWarner Losh /* Stop profiling */ 513fdf4e8b3SWarner Losh stopprofclock(p); 514fdf4e8b3SWarner Losh 51526f9a767SRodney W. Grimes /* reset caught signals */ 51626f9a767SRodney W. Grimes execsigs(p); 51726f9a767SRodney W. Grimes 51826f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 51926f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 52026f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 52126f9a767SRodney W. Grimes p->p_comm[len] = 0; 52226f9a767SRodney W. Grimes 52326f9a767SRodney W. Grimes /* 52424b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 525dc733423SDag-Erling Smørgrav * it that it now has its own resources back 52626f9a767SRodney W. Grimes */ 52726f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 52826f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 52926f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 5307f05b035SAlfred Perlstein wakeup(p->p_pptr); 53126f9a767SRodney W. Grimes } 53226f9a767SRodney W. Grimes 53326f9a767SRodney W. Grimes /* 534a78e8d2aSDavid Greenman * Implement image setuid/setgid. 535a78e8d2aSDavid Greenman * 536a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 537a78e8d2aSDavid Greenman * the process is being traced. 538670cb89bSRobert Watson * 539670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 540670cb89bSRobert Watson * transitions on the file system. 541c52007c2SDavid Greenman */ 542b1fc0ec1SRobert Watson oldcred = p->p_ucred; 543d06c0d4dSRobert Watson credential_changing = 0; 544d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 545d06c0d4dSRobert Watson attr.va_uid; 546d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 547d06c0d4dSRobert Watson attr.va_gid; 548ccafe7ebSRobert Watson #ifdef MAC 549670cb89bSRobert Watson will_transition = mac_execve_will_transition(oldcred, imgp->vp, 550eca8a663SRobert Watson interplabel, imgp); 551ccafe7ebSRobert Watson credential_changing |= will_transition; 552ccafe7ebSRobert Watson #endif 553d06c0d4dSRobert Watson 554d06c0d4dSRobert Watson if (credential_changing && 555a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 556c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 557c52007c2SDavid Greenman /* 558c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 559b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 560b85db196SPeter Wemm * we do not regain any tracing during a possible block. 56126f9a767SRodney W. Grimes */ 562b85db196SPeter Wemm setsugid(p); 5636c84de02SJohn Baldwin #ifdef KTRACE 56456f21b9dSColin Percival if (p->p_tracevp != NULL && suser_cred(oldcred, SUSER_ALLOWJAIL)) { 5656c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 56679deba82SMatthew Dillon p->p_traceflag = 0; 567a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 568a5881ea5SJohn Baldwin p->p_tracevp = NULL; 569a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 570a5881ea5SJohn Baldwin p->p_tracecred = NULL; 5716c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 57226f9a767SRodney W. Grimes } 5736c84de02SJohn Baldwin #endif 57428b325aaSDon Lewis /* 575c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 576c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 57728b325aaSDon Lewis * 578c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 579c1e2d386SNate Lawson * which may grab the process lock. 58028b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 58128b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 58228b325aaSDon Lewis */ 58328b325aaSDon Lewis PROC_UNLOCK(p); 584c1e2d386SNate Lawson setugidsafety(td); 585e983a376SJacques Vidrine error = fdcheckstd(td); 58663c9e754SJohn Baldwin if (error != 0) 58769be5db9SAlfred Perlstein goto done1; 588e1b1aa3bSJohn Baldwin PROC_LOCK(p); 589c52007c2SDavid Greenman /* 590c52007c2SDavid Greenman * Set the new credentials. 591c52007c2SDavid Greenman */ 5929b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 593c52007c2SDavid Greenman if (attr.va_mode & VSUID) 5941419eacbSAlfred Perlstein change_euid(newcred, euip); 595c52007c2SDavid Greenman if (attr.va_mode & VSGID) 596b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 597ccafe7ebSRobert Watson #ifdef MAC 598670cb89bSRobert Watson if (will_transition) { 599670cb89bSRobert Watson mac_execve_transition(oldcred, newcred, imgp->vp, 600eca8a663SRobert Watson interplabel, imgp); 601670cb89bSRobert Watson } 602ccafe7ebSRobert Watson #endif 6039b3b1c5fSJohn Baldwin /* 6049b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 605ccafe7ebSRobert Watson * 606ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 607ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 608ccafe7ebSRobert Watson * though maybe it shouldn't. 6099b3b1c5fSJohn Baldwin */ 6109b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 6119b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 6129b3b1c5fSJohn Baldwin p->p_ucred = newcred; 6139b3b1c5fSJohn Baldwin newcred = NULL; 614c52007c2SDavid Greenman } else { 615b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 616b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 617c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 61826f9a767SRodney W. Grimes /* 619c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 620b1fc0ec1SRobert Watson * 621b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 6229b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 6239b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 6249b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 6259b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 6269b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 6279b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 6289b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 62926f9a767SRodney W. Grimes */ 630b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 631b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 6329b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 633b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 634b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 635b1fc0ec1SRobert Watson p->p_ucred = newcred; 6369b3b1c5fSJohn Baldwin newcred = NULL; 6379b3b1c5fSJohn Baldwin } 638b1fc0ec1SRobert Watson } 63926f9a767SRodney W. Grimes 64026f9a767SRodney W. Grimes /* 641619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 642619eb6e5SJeff Roberson * to locking the proc lock. 6432a531c80SDavid Greenman */ 6449b3b1c5fSJohn Baldwin textvp = p->p_textvp; 6452a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 6462a531c80SDavid Greenman 6472a531c80SDavid Greenman /* 6489ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 6499ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 650cb679c38SJonathan Lemon */ 651ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 6529ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 653cb679c38SJonathan Lemon 654cb679c38SJonathan Lemon /* 65526f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 65626f9a767SRodney W. Grimes * can be set before the program executes. 657906ac69dSDavid Xu * Use tdsignal to deliver signal to current thread, use 658906ac69dSDavid Xu * psignal may cause the signal to be delivered to wrong thread 659906ac69dSDavid Xu * because that thread will exit, remember we are going to enter 660906ac69dSDavid Xu * single thread mode. 66126f9a767SRodney W. Grimes */ 66226f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 6636d7b314bSDavid Xu tdsignal(p, td, SIGTRAP, NULL); 66426f9a767SRodney W. Grimes 66526f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 66626f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 66726f9a767SRodney W. Grimes 6685997cae9SDon Lewis /* 66934ea500bSDon Lewis * Free any previous argument cache and replace it with 6705997cae9SDon Lewis * the new argument cache, if any. 6715997cae9SDon Lewis */ 6729b3b1c5fSJohn Baldwin oldargs = p->p_args; 673e1b1aa3bSJohn Baldwin p->p_args = newargs; 674e1b1aa3bSJohn Baldwin newargs = NULL; 675ebccf1e3SJoseph Koshy 676ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 677ebccf1e3SJoseph Koshy /* 678f263522aSJoseph Koshy * Check if system-wide sampling is in effect or if the 679f263522aSJoseph Koshy * current process is using PMCs. If so, do exec() time 680ebccf1e3SJoseph Koshy * processing. This processing needs to happen AFTER the 681ebccf1e3SJoseph Koshy * P_INEXEC flag is cleared. 682ebccf1e3SJoseph Koshy * 683ebccf1e3SJoseph Koshy * The proc lock needs to be released before taking the PMC 684ebccf1e3SJoseph Koshy * SX. 685ebccf1e3SJoseph Koshy */ 686f263522aSJoseph Koshy if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) { 687e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 68815139246SJoseph Koshy pe.pm_credentialschanged = credential_changing; 68915139246SJoseph Koshy pe.pm_entryaddr = imgp->entry_addr; 69015139246SJoseph Koshy 69115139246SJoseph Koshy PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe); 692ebccf1e3SJoseph Koshy } else 693ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 694ebccf1e3SJoseph Koshy #else /* !HWPMC_HOOKS */ 695ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 696ebccf1e3SJoseph Koshy #endif 697e1b1aa3bSJohn Baldwin 698e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 6993ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 7003ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 7013ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 7023ebc1248SPeter Wemm else 703bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 704bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 705e913ca22SDoug Rabson 7069f5c1d19SDiomidis Spinellis vfs_mark_atime(imgp->vp, td); 7076341095eSKen Smith 70869be5db9SAlfred Perlstein done1: 7099b3b1c5fSJohn Baldwin /* 7109b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 7119b3b1c5fSJohn Baldwin */ 7121419eacbSAlfred Perlstein uifree(euip); 7139b3b1c5fSJohn Baldwin if (newcred == NULL) 7149b3b1c5fSJohn Baldwin crfree(oldcred); 7159b3b1c5fSJohn Baldwin else 7169b3b1c5fSJohn Baldwin crfree(newcred); 7179b3b1c5fSJohn Baldwin /* 7189b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 7199b3b1c5fSJohn Baldwin */ 72068ce4375SJeff Roberson if (textvp != NULL) { 72168ce4375SJeff Roberson int tvfslocked; 72268ce4375SJeff Roberson 72368ce4375SJeff Roberson tvfslocked = VFS_LOCK_GIANT(textvp->v_mount); 7249b3b1c5fSJohn Baldwin vrele(textvp); 72568ce4375SJeff Roberson VFS_UNLOCK_GIANT(tvfslocked); 72668ce4375SJeff Roberson } 727619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 728619eb6e5SJeff Roberson vrele(ndp->ni_vp); 7296c84de02SJohn Baldwin #ifdef KTRACE 7309b3b1c5fSJohn Baldwin if (tracevp != NULL) 7319b3b1c5fSJohn Baldwin vrele(tracevp); 732a5881ea5SJohn Baldwin if (tracecred != NULL) 733a5881ea5SJohn Baldwin crfree(tracecred); 7346c84de02SJohn Baldwin #endif 73569be5db9SAlfred Perlstein if (oldargs != NULL) 7369b3b1c5fSJohn Baldwin pargs_drop(oldargs); 73769be5db9SAlfred Perlstein if (newargs != NULL) 73869be5db9SAlfred Perlstein pargs_drop(newargs); 73990af4afaSJohn Baldwin if (oldsigacts != NULL) 74090af4afaSJohn Baldwin sigacts_free(oldsigacts); 741b9df5231SPoul-Henning Kamp 7421616db3cSJohn Dyson exec_fail_dealloc: 7431616db3cSJohn Dyson 74426f9a767SRodney W. Grimes /* 74526f9a767SRodney W. Grimes * free various allocated resources 74626f9a767SRodney W. Grimes */ 747d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7481616db3cSJohn Dyson exec_unmap_first_page(imgp); 74926f9a767SRodney W. Grimes 750d6c847f3SBruce Evans if (imgp->vp != NULL) { 751762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 752619eb6e5SJeff Roberson vput(imgp->vp); 753a3cf6ebaSDavid Greenman } 75426f9a767SRodney W. Grimes 755d6c847f3SBruce Evans if (imgp->object != NULL) 7560b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 7570b2ed1aeSJeff Roberson 758d1989db5SRobert Drehmel if (error == 0) { 759d1989db5SRobert Drehmel /* 760d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 761d1989db5SRobert Drehmel * the S_EXEC bit set. 762d1989db5SRobert Drehmel */ 763d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 764116734c4SMatthew Dillon goto done2; 765d1989db5SRobert Drehmel } 7661616db3cSJohn Dyson 76726f9a767SRodney W. Grimes exec_fail: 7689ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 7699ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 7709ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 7719ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 7729ca45e81SDag-Erling Smørgrav 773116734c4SMatthew Dillon done2: 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 779269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 7808917b8d2SJohn Baldwin exec_free_args(args); 7818917b8d2SJohn Baldwin 7828917b8d2SJohn Baldwin if (error && imgp->vmspace_destroyed) { 7838917b8d2SJohn Baldwin /* sorry, no more process anymore. exit gracefully */ 7848917b8d2SJohn Baldwin exit1(td, W_EXITCODE(0, SIGABRT)); 7858917b8d2SJohn Baldwin /* NOT REACHED */ 7868917b8d2SJohn Baldwin } 787116734c4SMatthew Dillon return (error); 78826f9a767SRodney W. Grimes } 78926f9a767SRodney W. Grimes 7901616db3cSJohn Dyson int 7911616db3cSJohn Dyson exec_map_first_page(imgp) 7921616db3cSJohn Dyson struct image_params *imgp; 7931616db3cSJohn Dyson { 794d8aad40cSJohn Baldwin int rv, i; 79595461b45SJohn Dyson int initial_pagein; 79695461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 7971616db3cSJohn Dyson vm_object_t object; 7981616db3cSJohn Dyson 799d6c847f3SBruce Evans if (imgp->firstpage != NULL) 8001616db3cSJohn Dyson exec_unmap_first_page(imgp); 8011616db3cSJohn Dyson 8028516dd18SPoul-Henning Kamp object = imgp->vp->v_object; 8034d7d2993SJeff Roberson if (object == NULL) 8044d7d2993SJeff Roberson return (EACCES); 805fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 80695461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 80795461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 80895461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 80995461b45SJohn Dyson if (initial_pagein > object->size) 81095461b45SJohn Dyson initial_pagein = object->size; 81195461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 812d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 8136ec2fca5SAlan Cox if (ma[i]->valid) 8146ec2fca5SAlan Cox break; 8158ad398d0SAlan Cox if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 81606fa71cdSAlan Cox break; 8178ad398d0SAlan Cox vm_page_lock_queues(); 818e69763a3SDoug Rabson vm_page_busy(ma[i]); 819ee113343SAlan Cox vm_page_unlock_queues(); 82095461b45SJohn Dyson } else { 821ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 822ca0387efSJake Burkholder VM_ALLOC_NORMAL); 82395461b45SJohn Dyson if (ma[i] == NULL) 82495461b45SJohn Dyson break; 82595461b45SJohn Dyson } 82695461b45SJohn Dyson } 82795461b45SJohn Dyson initial_pagein = i; 82895461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 82995461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 830ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 831ca0387efSJake Burkholder (ma[0]->valid == 0)) { 832ecbb00a2SDoug Rabson if (ma[0]) { 8336ec2fca5SAlan Cox vm_page_lock_queues(); 8348f9110f6SJohn Dyson vm_page_free(ma[0]); 83506fa71cdSAlan Cox vm_page_unlock_queues(); 8366ec2fca5SAlan Cox } 83706fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 838a7cddfedSJake Burkholder return (EIO); 8391616db3cSJohn Dyson } 8401616db3cSJohn Dyson } 8416ec2fca5SAlan Cox vm_page_lock_queues(); 842148b3f62SAlan Cox vm_page_hold(ma[0]); 843e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 84497646f56SAlan Cox vm_page_unlock_queues(); 8456ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 8461616db3cSJohn Dyson 84759c8bc40SAlan Cox imgp->firstpage = sf_buf_alloc(ma[0], 0); 84859c8bc40SAlan Cox imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 8491616db3cSJohn Dyson 850a7cddfedSJake Burkholder return (0); 8511616db3cSJohn Dyson } 8521616db3cSJohn Dyson 8531616db3cSJohn Dyson void 8541616db3cSJohn Dyson exec_unmap_first_page(imgp) 8551616db3cSJohn Dyson struct image_params *imgp; 8561616db3cSJohn Dyson { 85759c8bc40SAlan Cox vm_page_t m; 85823955314SAlfred Perlstein 859d6c847f3SBruce Evans if (imgp->firstpage != NULL) { 86059c8bc40SAlan Cox m = sf_buf_page(imgp->firstpage); 86159c8bc40SAlan Cox sf_buf_free(imgp->firstpage); 8621616db3cSJohn Dyson imgp->firstpage = NULL; 86359c8bc40SAlan Cox vm_page_lock_queues(); 86459c8bc40SAlan Cox vm_page_unhold(m); 86559c8bc40SAlan Cox vm_page_unlock_queues(); 8661616db3cSJohn Dyson } 8671616db3cSJohn Dyson } 8681616db3cSJohn Dyson 86926f9a767SRodney W. Grimes /* 87026f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 87126f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 87226f9a767SRodney W. Grimes * automatically in trap.c. 87326f9a767SRodney W. Grimes */ 87426f9a767SRodney W. Grimes int 87505ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 876c52007c2SDavid Greenman struct image_params *imgp; 87705ba50f5SJake Burkholder struct sysentvec *sv; 87826f9a767SRodney W. Grimes { 87926f9a767SRodney W. Grimes int error; 8805e20c11fSAlan Cox struct proc *p = imgp->proc; 8815e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 88205ba50f5SJake Burkholder vm_offset_t stack_addr; 88305ba50f5SJake Burkholder vm_map_t map; 88426f9a767SRodney W. Grimes 885c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 88626f9a767SRodney W. Grimes 887a5bdcb2aSPeter Wemm /* Called with Giant held, do not depend on it! */ 88875b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exec, p); 8896f5dafeaSAlan Cox 8906f5dafeaSAlan Cox /* 891c460ac3aSPeter Wemm * Here is as good a place as any to do any resource limit cleanups. 892c460ac3aSPeter Wemm * This is needed if a 64 bit binary exec's a 32 bit binary - the 893c460ac3aSPeter Wemm * data size limit may need to be changed to a value that makes 894c460ac3aSPeter Wemm * sense for the 32 bit binary. 895c460ac3aSPeter Wemm */ 896d6c847f3SBruce Evans if (sv->sv_fixlimits != NULL) 8971471f287SPaul Saab sv->sv_fixlimits(p); 898c460ac3aSPeter Wemm 899c460ac3aSPeter Wemm /* 900af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 901af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 902af9ec885SJohn Dyson * not disrupted 903af9ec885SJohn Dyson */ 90405ba50f5SJake Burkholder map = &vmspace->vm_map; 90505ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 90605ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 9073db161e0SMatthew Dillon shmexit(vmspace); 90805ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 90905ba50f5SJake Burkholder vm_map_max(map)); 91005ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 911af9ec885SJohn Dyson } else { 91205ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 9135e20c11fSAlan Cox vmspace = p->p_vmspace; 91405ba50f5SJake Burkholder map = &vmspace->vm_map; 915af9ec885SJohn Dyson } 91626f9a767SRodney W. Grimes 91726f9a767SRodney W. Grimes /* Allocate a new stack */ 918fd75d710SMarcel Moolenaar stack_addr = sv->sv_usrstack - maxssiz; 91905ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 920fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 9212267af78SJulian Elischer if (error) 9222267af78SJulian Elischer return (error); 9232267af78SJulian Elischer 92463c47a5cSDoug Rabson #ifdef __ia64__ 925fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 926bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 927fd75d710SMarcel Moolenaar error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 928fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 929fd75d710SMarcel Moolenaar if (error) 930fd75d710SMarcel Moolenaar return (error); 93163c47a5cSDoug Rabson #endif 93263c47a5cSDoug Rabson 9332267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 9342267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 9352267af78SJulian Elischer * process stack so we can check the stack rlimit. 9362267af78SJulian Elischer */ 937cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 93805ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 93926f9a767SRodney W. Grimes 94026f9a767SRodney W. Grimes return (0); 94126f9a767SRodney W. Grimes } 94226f9a767SRodney W. Grimes 94326f9a767SRodney W. Grimes /* 94426f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 94526f9a767SRodney W. Grimes * address space into the temporary string buffer. 94626f9a767SRodney W. Grimes */ 94726f9a767SRodney W. Grimes int 948610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname, 949610ecfe0SMaxim Sobolev enum uio_seg segflg, char **argv, char **envv) 95026f9a767SRodney W. Grimes { 95126f9a767SRodney W. Grimes char *argp, *envp; 952ecbb00a2SDoug Rabson int error; 953ecbb00a2SDoug Rabson size_t length; 95426f9a767SRodney W. Grimes 955610ecfe0SMaxim Sobolev error = 0; 956610ecfe0SMaxim Sobolev 957610ecfe0SMaxim Sobolev bzero(args, sizeof(*args)); 958610ecfe0SMaxim Sobolev if (argv == NULL) 959610ecfe0SMaxim Sobolev return (EFAULT); 96026f9a767SRodney W. Grimes /* 961610ecfe0SMaxim Sobolev * Allocate temporary demand zeroed space for argument and 96290dc539bSMaxim Sobolev * environment strings: 96390dc539bSMaxim Sobolev * 96490dc539bSMaxim Sobolev * o ARG_MAX for argument and environment; 96590dc539bSMaxim Sobolev * o MAXSHELLCMDLEN for the name of interpreters. 96626f9a767SRodney W. Grimes */ 96790dc539bSMaxim Sobolev args->buf = (char *) kmem_alloc_wait(exec_map, 96890dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 969610ecfe0SMaxim Sobolev if (args->buf == NULL) 970610ecfe0SMaxim Sobolev return (ENOMEM); 971610ecfe0SMaxim Sobolev args->begin_argv = args->buf; 972610ecfe0SMaxim Sobolev args->endp = args->begin_argv; 973610ecfe0SMaxim Sobolev args->stringspace = ARG_MAX; 97426f9a767SRodney W. Grimes 975610ecfe0SMaxim Sobolev args->fname = args->buf + ARG_MAX; 97626f9a767SRodney W. Grimes 977610ecfe0SMaxim Sobolev /* 978610ecfe0SMaxim Sobolev * Copy the file name. 979610ecfe0SMaxim Sobolev */ 980610ecfe0SMaxim Sobolev error = (segflg == UIO_SYSSPACE) ? 981610ecfe0SMaxim Sobolev copystr(fname, args->fname, PATH_MAX, &length) : 982610ecfe0SMaxim Sobolev copyinstr(fname, args->fname, PATH_MAX, &length); 983c30af532SMaxim Sobolev if (error != 0) 98468ff3c24SStephan Uphoff goto err_exit; 98526f9a767SRodney W. Grimes 986610ecfe0SMaxim Sobolev /* 987610ecfe0SMaxim Sobolev * extract arguments first 988610ecfe0SMaxim Sobolev */ 989610ecfe0SMaxim Sobolev while ((argp = (caddr_t) (intptr_t) fuword(argv++))) { 99068ff3c24SStephan Uphoff if (argp == (caddr_t) -1) { 99168ff3c24SStephan Uphoff error = EFAULT; 99268ff3c24SStephan Uphoff goto err_exit; 99368ff3c24SStephan Uphoff } 994610ecfe0SMaxim Sobolev if ((error = copyinstr(argp, args->endp, 995610ecfe0SMaxim Sobolev args->stringspace, &length))) { 996610ecfe0SMaxim Sobolev if (error == ENAMETOOLONG) 99768ff3c24SStephan Uphoff error = E2BIG; 99868ff3c24SStephan Uphoff goto err_exit; 999610ecfe0SMaxim Sobolev } 1000610ecfe0SMaxim Sobolev args->stringspace -= length; 1001610ecfe0SMaxim Sobolev args->endp += length; 1002610ecfe0SMaxim Sobolev args->argc++; 1003610ecfe0SMaxim Sobolev } 1004610ecfe0SMaxim Sobolev 1005610ecfe0SMaxim Sobolev args->begin_envv = args->endp; 1006b9df5231SPoul-Henning Kamp 100726f9a767SRodney W. Grimes /* 100826f9a767SRodney W. Grimes * extract environment strings 100926f9a767SRodney W. Grimes */ 10100fd1a014SDavid Greenman if (envv) { 1011aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 101268ff3c24SStephan Uphoff if (envp == (caddr_t)-1) { 101368ff3c24SStephan Uphoff error = EFAULT; 101468ff3c24SStephan Uphoff goto err_exit; 101568ff3c24SStephan Uphoff } 1016610ecfe0SMaxim Sobolev if ((error = copyinstr(envp, args->endp, 1017610ecfe0SMaxim Sobolev args->stringspace, &length))) { 10180fd1a014SDavid Greenman if (error == ENAMETOOLONG) 101968ff3c24SStephan Uphoff error = E2BIG; 102068ff3c24SStephan Uphoff goto err_exit; 10210fd1a014SDavid Greenman } 1022610ecfe0SMaxim Sobolev args->stringspace -= length; 1023610ecfe0SMaxim Sobolev args->endp += length; 1024610ecfe0SMaxim Sobolev args->envc++; 102526f9a767SRodney W. Grimes } 10260fd1a014SDavid Greenman } 102726f9a767SRodney W. Grimes 102826f9a767SRodney W. Grimes return (0); 102968ff3c24SStephan Uphoff 103068ff3c24SStephan Uphoff err_exit: 103168ff3c24SStephan Uphoff exec_free_args(args); 103268ff3c24SStephan Uphoff return (error); 103326f9a767SRodney W. Grimes } 103426f9a767SRodney W. Grimes 10358917b8d2SJohn Baldwin static void 1036610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args) 1037610ecfe0SMaxim Sobolev { 1038610ecfe0SMaxim Sobolev 1039610ecfe0SMaxim Sobolev if (args->buf) { 104090dc539bSMaxim Sobolev kmem_free_wakeup(exec_map, (vm_offset_t)args->buf, 104190dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 1042610ecfe0SMaxim Sobolev args->buf = NULL; 1043610ecfe0SMaxim Sobolev } 1044610ecfe0SMaxim Sobolev } 1045610ecfe0SMaxim Sobolev 104626f9a767SRodney W. Grimes /* 104726f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 104826f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 104926f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 105026f9a767SRodney W. Grimes */ 1051654f6be1SBruce Evans register_t * 1052c52007c2SDavid Greenman exec_copyout_strings(imgp) 1053c52007c2SDavid Greenman struct image_params *imgp; 105426f9a767SRodney W. Grimes { 105526f9a767SRodney W. Grimes int argc, envc; 105626f9a767SRodney W. Grimes char **vectp; 105726f9a767SRodney W. Grimes char *stringp, *destp; 1058654f6be1SBruce Evans register_t *stack_base; 105993f6448cSDavid Greenman struct ps_strings *arginfo; 1060f3bec5d7SJake Burkholder struct proc *p; 1061d66a5066SPeter Wemm int szsigcode; 106226f9a767SRodney W. Grimes 106326f9a767SRodney W. Grimes /* 106426f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 1065d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 106626f9a767SRodney W. Grimes */ 1067f3bec5d7SJake Burkholder p = imgp->proc; 1068f3bec5d7SJake Burkholder szsigcode = 0; 106905ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1070f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1071f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1072d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 1073610ecfe0SMaxim Sobolev roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 10741ed012f9SPeter Wemm 107526f9a767SRodney W. Grimes /* 1076d66a5066SPeter Wemm * install sigcode 1077d66a5066SPeter Wemm */ 1078d66a5066SPeter Wemm if (szsigcode) 1079f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1080f3bec5d7SJake Burkholder szsigcode), szsigcode); 1081d66a5066SPeter Wemm 1082d66a5066SPeter Wemm /* 1083e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1084e1743d02SSøren Schmidt * on the stack. 1085e1743d02SSøren Schmidt */ 1086b9a22da4STakanori Watanabe if (imgp->auxargs) { 1087e1743d02SSøren Schmidt /* 1088b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1089b9a22da4STakanori Watanabe * lower compatibility. 1090b9a22da4STakanori Watanabe */ 1091ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1092ca0387efSJake Burkholder (AT_COUNT * 2); 1093b9a22da4STakanori Watanabe /* 1094b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1095b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1096b9a22da4STakanori Watanabe * for argument of Runtime loader. 1097e1743d02SSøren Schmidt */ 1098610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + 1099610ecfe0SMaxim Sobolev imgp->args->envc + 2 + imgp->auxarg_size) * 1100610ecfe0SMaxim Sobolev sizeof(char *)); 1101b9a22da4STakanori Watanabe 1102610ecfe0SMaxim Sobolev } else { 1103e1743d02SSøren Schmidt /* 1104b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1105b9a22da4STakanori Watanabe * the arg and env vector sets 110626f9a767SRodney W. Grimes */ 1107610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * 1108ca0387efSJake Burkholder sizeof(char *)); 1109610ecfe0SMaxim Sobolev } 111026f9a767SRodney W. Grimes 111126f9a767SRodney W. Grimes /* 111226f9a767SRodney W. Grimes * vectp also becomes our initial stack base 111326f9a767SRodney W. Grimes */ 1114654f6be1SBruce Evans stack_base = (register_t *)vectp; 111526f9a767SRodney W. Grimes 1116610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 1117610ecfe0SMaxim Sobolev argc = imgp->args->argc; 1118610ecfe0SMaxim Sobolev envc = imgp->args->envc; 111926f9a767SRodney W. Grimes 112093f6448cSDavid Greenman /* 11213aed948bSDavid Greenman * Copy out strings - arguments and environment. 112293f6448cSDavid Greenman */ 1123610ecfe0SMaxim Sobolev copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 112493f6448cSDavid Greenman 112593f6448cSDavid Greenman /* 11263aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 11273aed948bSDavid Greenman */ 1128aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 11293aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 11303aed948bSDavid Greenman 11313aed948bSDavid Greenman /* 11323aed948bSDavid Greenman * Fill in argument portion of vector table. 113393f6448cSDavid Greenman */ 113426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1135aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11363aed948bSDavid Greenman while (*stringp++ != 0) 11373aed948bSDavid Greenman destp++; 11383aed948bSDavid Greenman destp++; 113926f9a767SRodney W. Grimes } 114026f9a767SRodney W. Grimes 11411a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 11426ab46d52SBruce Evans suword(vectp++, 0); 114326f9a767SRodney W. Grimes 1144aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 11453aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 114693f6448cSDavid Greenman 114793f6448cSDavid Greenman /* 11483aed948bSDavid Greenman * Fill in environment portion of vector table. 114993f6448cSDavid Greenman */ 115026f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1151aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11523aed948bSDavid Greenman while (*stringp++ != 0) 11533aed948bSDavid Greenman destp++; 11543aed948bSDavid Greenman destp++; 115526f9a767SRodney W. Grimes } 115626f9a767SRodney W. Grimes 115726f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 11586ab46d52SBruce Evans suword(vectp, 0); 115926f9a767SRodney W. Grimes 116026f9a767SRodney W. Grimes return (stack_base); 116126f9a767SRodney W. Grimes } 116226f9a767SRodney W. Grimes 116326f9a767SRodney W. Grimes /* 116426f9a767SRodney W. Grimes * Check permissions of file to execute. 1165cf64863aSRobert Watson * Called with imgp->vp locked. 116626f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 116726f9a767SRodney W. Grimes */ 1168c8a79999SPeter Wemm int 1169c52007c2SDavid Greenman exec_check_permissions(imgp) 1170c52007c2SDavid Greenman struct image_params *imgp; 117126f9a767SRodney W. Grimes { 1172c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1173c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1174a854ed98SJohn Baldwin struct thread *td; 117526f9a767SRodney W. Grimes int error; 117626f9a767SRodney W. Grimes 1177a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1178339b79b9SRobert Watson 1179ec35c2afSRobert Watson /* Get file attributes */ 1180ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1181ec35c2afSRobert Watson if (error) 1182ec35c2afSRobert Watson return (error); 1183ec35c2afSRobert Watson 1184339b79b9SRobert Watson #ifdef MAC 1185670cb89bSRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1186339b79b9SRobert Watson if (error) 1187339b79b9SRobert Watson return (error); 1188339b79b9SRobert Watson #endif 1189339b79b9SRobert Watson 119026f9a767SRodney W. Grimes /* 119126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 119226f9a767SRodney W. Grimes * file resides on. 119326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 119426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 119526f9a767SRodney W. Grimes * file really is executable. 119626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 119726f9a767SRodney W. Grimes */ 1198c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 119926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1200a854ed98SJohn Baldwin (attr->va_type != VREG)) 120126f9a767SRodney W. Grimes return (EACCES); 120226f9a767SRodney W. Grimes 120326f9a767SRodney W. Grimes /* 120426f9a767SRodney W. Grimes * Zero length files can't be exec'd 120526f9a767SRodney W. Grimes */ 120626f9a767SRodney W. Grimes if (attr->va_size == 0) 120726f9a767SRodney W. Grimes return (ENOEXEC); 120826f9a767SRodney W. Grimes 120926f9a767SRodney W. Grimes /* 121026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 121126f9a767SRodney W. Grimes */ 1212a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 121326f9a767SRodney W. Grimes if (error) 121426f9a767SRodney W. Grimes return (error); 121526f9a767SRodney W. Grimes 12166d5a0a8cSDavid Greenman /* 12176d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 12186d5a0a8cSDavid Greenman * if there are any. 12196d5a0a8cSDavid Greenman */ 12206d5a0a8cSDavid Greenman if (vp->v_writecount) 12216d5a0a8cSDavid Greenman return (ETXTBSY); 12226d5a0a8cSDavid Greenman 12236d5a0a8cSDavid Greenman /* 12246d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 12256d5a0a8cSDavid Greenman * general case). 12266d5a0a8cSDavid Greenman */ 1227a8d43c90SPoul-Henning Kamp error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1); 122826f9a767SRodney W. Grimes return (error); 1229df8bae1dSRodney W. Grimes } 1230aa855a59SPeter Wemm 1231aa855a59SPeter Wemm /* 1232aa855a59SPeter Wemm * Exec handler registration 1233aa855a59SPeter Wemm */ 1234aa855a59SPeter Wemm int 1235aa855a59SPeter Wemm exec_register(execsw_arg) 1236aa855a59SPeter Wemm const struct execsw *execsw_arg; 1237aa855a59SPeter Wemm { 1238aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1239aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1240aa855a59SPeter Wemm 1241aa855a59SPeter Wemm if (execsw) 1242aa855a59SPeter Wemm for (es = execsw; *es; es++) 1243aa855a59SPeter Wemm count++; 1244a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1245aa855a59SPeter Wemm if (newexecsw == NULL) 1246a7cddfedSJake Burkholder return (ENOMEM); 1247aa855a59SPeter Wemm xs = newexecsw; 1248aa855a59SPeter Wemm if (execsw) 1249aa855a59SPeter Wemm for (es = execsw; *es; es++) 1250aa855a59SPeter Wemm *xs++ = *es; 1251aa855a59SPeter Wemm *xs++ = execsw_arg; 1252aa855a59SPeter Wemm *xs = NULL; 1253aa855a59SPeter Wemm if (execsw) 1254aa855a59SPeter Wemm free(execsw, M_TEMP); 1255aa855a59SPeter Wemm execsw = newexecsw; 1256a7cddfedSJake Burkholder return (0); 1257aa855a59SPeter Wemm } 1258aa855a59SPeter Wemm 1259aa855a59SPeter Wemm int 1260aa855a59SPeter Wemm exec_unregister(execsw_arg) 1261aa855a59SPeter Wemm const struct execsw *execsw_arg; 1262aa855a59SPeter Wemm { 1263aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1264aa855a59SPeter Wemm int count = 1; 1265aa855a59SPeter Wemm 1266aa855a59SPeter Wemm if (execsw == NULL) 1267aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1268aa855a59SPeter Wemm 1269aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1270aa855a59SPeter Wemm if (*es == execsw_arg) 1271aa855a59SPeter Wemm break; 1272aa855a59SPeter Wemm } 1273aa855a59SPeter Wemm if (*es == NULL) 1274a7cddfedSJake Burkholder return (ENOENT); 1275aa855a59SPeter Wemm for (es = execsw; *es; es++) 1276aa855a59SPeter Wemm if (*es != execsw_arg) 1277aa855a59SPeter Wemm count++; 1278a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1279aa855a59SPeter Wemm if (newexecsw == NULL) 1280a7cddfedSJake Burkholder return (ENOMEM); 1281aa855a59SPeter Wemm xs = newexecsw; 1282aa855a59SPeter Wemm for (es = execsw; *es; es++) 1283aa855a59SPeter Wemm if (*es != execsw_arg) 1284aa855a59SPeter Wemm *xs++ = *es; 1285aa855a59SPeter Wemm *xs = NULL; 1286aa855a59SPeter Wemm if (execsw) 1287aa855a59SPeter Wemm free(execsw, M_TEMP); 1288aa855a59SPeter Wemm execsw = newexecsw; 1289a7cddfedSJake Burkholder return (0); 1290aa855a59SPeter Wemm } 1291