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> 4226f9a767SRodney W. Grimes #include <sys/mount.h> 43797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 44079cc25bSDavid Greenman #include <sys/fcntl.h> 4526f9a767SRodney W. Grimes #include <sys/acct.h> 4626f9a767SRodney W. Grimes #include <sys/exec.h> 4726f9a767SRodney W. Grimes #include <sys/imgact.h> 48e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4926f9a767SRodney W. Grimes #include <sys/wait.h> 50333ea485SGuido van Rooij #include <sys/malloc.h> 51acd3428bSRobert Watson #include <sys/priv.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 82ae1078d6SWayne Salamon #include <security/audit/audit.h> 83aed55708SRobert Watson #include <security/mac/mac_framework.h> 84ae1078d6SWayne Salamon 85b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 86b9df5231SPoul-Henning Kamp 8705ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 8805ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 895dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 90610ecfe0SMaxim Sobolev static int do_execve(struct thread *td, struct image_args *args, 91610ecfe0SMaxim Sobolev struct mac *mac_p); 928917b8d2SJohn Baldwin static void exec_free_args(struct image_args *); 9305ba50f5SJake Burkholder 949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9505ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9605ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 97486bddb0SDoug Rabson 989701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9905ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 10005ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 10199ac3bc8SPeter Wemm 1025dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 1035dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 1045dadd17bSJake Burkholder 105b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 106c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1079701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 108b9df5231SPoul-Henning Kamp 10905ba50f5SJake Burkholder static int 11005ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 11105ba50f5SJake Burkholder { 11205ba50f5SJake Burkholder struct proc *p; 113df7c361eSPeter Wemm int error; 11405ba50f5SJake Burkholder 11505ba50f5SJake Burkholder p = curproc; 116a7bc3102SPeter Wemm #ifdef SCTL_MASK32 117a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 118df7c361eSPeter Wemm unsigned int val; 119df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_psstrings; 120df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 121df7c361eSPeter Wemm } else 122df7c361eSPeter Wemm #endif 123df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 124df7c361eSPeter Wemm sizeof(p->p_sysent->sv_psstrings)); 125df7c361eSPeter Wemm return error; 12605ba50f5SJake Burkholder } 12705ba50f5SJake Burkholder 12805ba50f5SJake Burkholder static int 12905ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 13005ba50f5SJake Burkholder { 13105ba50f5SJake Burkholder struct proc *p; 132df7c361eSPeter Wemm int error; 13305ba50f5SJake Burkholder 13405ba50f5SJake Burkholder p = curproc; 135a7bc3102SPeter Wemm #ifdef SCTL_MASK32 136a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 137df7c361eSPeter Wemm unsigned int val; 138df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_usrstack; 139df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 140df7c361eSPeter Wemm } else 141df7c361eSPeter Wemm #endif 142df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 143df7c361eSPeter Wemm sizeof(p->p_sysent->sv_usrstack)); 144df7c361eSPeter Wemm return error; 14505ba50f5SJake Burkholder } 14605ba50f5SJake Burkholder 1475dadd17bSJake Burkholder static int 1485dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1495dadd17bSJake Burkholder { 1505dadd17bSJake Burkholder struct proc *p; 1515dadd17bSJake Burkholder 1525dadd17bSJake Burkholder p = curproc; 1535dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1545dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1555dadd17bSJake Burkholder } 1565dadd17bSJake Burkholder 15799ac3bc8SPeter Wemm /* 158aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 159aa855a59SPeter Wemm * double pointer here. 160df8bae1dSRodney W. Grimes */ 161aa855a59SPeter Wemm static const struct execsw **execsw; 16226f9a767SRodney W. Grimes 163ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 164ca46e90eSBruce Evans struct execve_args { 165ca46e90eSBruce Evans char *fname; 166ca46e90eSBruce Evans char **argv; 167ca46e90eSBruce Evans char **envv; 168ca46e90eSBruce Evans }; 169ca46e90eSBruce Evans #endif 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 int 200ca46e90eSBruce Evans __mac_execve(td, uap) 201ca46e90eSBruce Evans struct thread *td; 202ca46e90eSBruce Evans struct __mac_execve_args /* { 203ca46e90eSBruce Evans char *fname; 204ca46e90eSBruce Evans char **argv; 205ca46e90eSBruce Evans char **envv; 206ca46e90eSBruce Evans struct mac *mac_p; 207ca46e90eSBruce Evans } */ *uap; 208ca46e90eSBruce Evans { 209ca46e90eSBruce Evans #ifdef MAC 210610ecfe0SMaxim Sobolev int error; 211610ecfe0SMaxim Sobolev struct image_args args; 212610ecfe0SMaxim Sobolev 213610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 214610ecfe0SMaxim Sobolev uap->argv, uap->envv); 215610ecfe0SMaxim Sobolev if (error == 0) 216610ecfe0SMaxim Sobolev error = kern_execve(td, &args, uap->mac_p); 217610ecfe0SMaxim Sobolev return (error); 218ca46e90eSBruce Evans #else 219ca46e90eSBruce Evans return (ENOSYS); 220ca46e90eSBruce Evans #endif 221ca46e90eSBruce Evans } 222ca46e90eSBruce Evans 22333812c06SColin Percival /* 224873fbcd7SRobert Watson * XXX: kern_execve has the astonishing property of not always returning to 225873fbcd7SRobert Watson * the caller. If sufficiently bad things happen during the call to 226873fbcd7SRobert Watson * do_execve(), it can end up calling exit1(); as a result, callers must 227873fbcd7SRobert Watson * avoid doing anything which they might need to undo (e.g., allocating 228873fbcd7SRobert Watson * memory). 22933812c06SColin Percival */ 23084e0b075SDavid Xu int 231610ecfe0SMaxim Sobolev kern_execve(td, args, mac_p) 232906ac69dSDavid Xu struct thread *td; 233610ecfe0SMaxim Sobolev struct image_args *args; 234906ac69dSDavid Xu struct mac *mac_p; 235906ac69dSDavid Xu { 236906ac69dSDavid Xu struct proc *p = td->td_proc; 237906ac69dSDavid Xu int error; 238906ac69dSDavid Xu 239ae1078d6SWayne Salamon AUDIT_ARG(argv, args->begin_argv, args->argc, 240ae1078d6SWayne Salamon args->begin_envv - args->begin_argv); 241ae1078d6SWayne Salamon AUDIT_ARG(envv, args->begin_envv, args->envc, 242ae1078d6SWayne Salamon args->endp - args->begin_envv); 243906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 244906ac69dSDavid Xu PROC_LOCK(p); 245906ac69dSDavid Xu if (thread_single(SINGLE_BOUNDARY)) { 246906ac69dSDavid Xu PROC_UNLOCK(p); 24768ff3c24SStephan Uphoff exec_free_args(args); 248906ac69dSDavid Xu return (ERESTART); /* Try again later. */ 249906ac69dSDavid Xu } 250906ac69dSDavid Xu PROC_UNLOCK(p); 251906ac69dSDavid Xu } 252906ac69dSDavid Xu 253610ecfe0SMaxim Sobolev error = do_execve(td, args, mac_p); 254906ac69dSDavid Xu 255906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 256906ac69dSDavid Xu PROC_LOCK(p); 257906ac69dSDavid Xu /* 258906ac69dSDavid Xu * If success, we upgrade to SINGLE_EXIT state to 259906ac69dSDavid Xu * force other threads to suicide. 260906ac69dSDavid Xu */ 261906ac69dSDavid Xu if (error == 0) 262906ac69dSDavid Xu thread_single(SINGLE_EXIT); 263906ac69dSDavid Xu else 264906ac69dSDavid Xu thread_single_end(); 265906ac69dSDavid Xu PROC_UNLOCK(p); 266906ac69dSDavid Xu } 267906ac69dSDavid Xu 268906ac69dSDavid Xu return (error); 269906ac69dSDavid Xu } 270906ac69dSDavid Xu 27126f9a767SRodney W. Grimes /* 272450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 273450ffb44SRobert Watson * userspace pointers from the passed thread. 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; 2889f5c1d19SDiomidis Spinellis struct vattr 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 30415139246SJoseph Koshy #ifdef HWPMC_HOOKS 30515139246SJoseph Koshy struct pmckern_procexec pe; 30615139246SJoseph Koshy #endif 30726f9a767SRodney W. Grimes 308532c491bSJeff Roberson vfslocked = 0; 309c52007c2SDavid Greenman imgp = &image_params; 310df8bae1dSRodney W. Grimes 3119ca45e81SDag-Erling Smørgrav /* 3129ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 3139ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 3149ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 3159ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 3169ca45e81SDag-Erling Smørgrav * privileges. 3179ca45e81SDag-Erling Smørgrav */ 3189ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 3199ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 32091f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 3219ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 3229ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 3239ca45e81SDag-Erling Smørgrav 324df8bae1dSRodney W. Grimes /* 325c52007c2SDavid Greenman * Initialize part of the common data 326df8bae1dSRodney W. Grimes */ 327c52007c2SDavid Greenman imgp->proc = p; 328670cb89bSRobert Watson imgp->execlabel = NULL; 329c52007c2SDavid Greenman imgp->attr = &attr; 330c52007c2SDavid Greenman imgp->entry_addr = 0; 331c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 332c52007c2SDavid Greenman imgp->interpreted = 0; 33390dc539bSMaxim Sobolev imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX; 334e1743d02SSøren Schmidt imgp->auxargs = NULL; 3351616db3cSJohn Dyson imgp->vp = NULL; 3360b2ed1aeSJeff Roberson imgp->object = NULL; 3371616db3cSJohn Dyson imgp->firstpage = NULL; 3384fe88fe6SJohn Polstra imgp->ps_strings = 0; 339b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 340610ecfe0SMaxim Sobolev imgp->args = args; 34126f9a767SRodney W. Grimes 342670cb89bSRobert Watson #ifdef MAC 343eca8a663SRobert Watson error = mac_execve_enter(imgp, mac_p); 344532c491bSJeff Roberson if (error) 345670cb89bSRobert Watson goto exec_fail; 346670cb89bSRobert Watson #endif 347670cb89bSRobert Watson 34859c8bc40SAlan Cox imgp->image_header = NULL; 34926f9a767SRodney W. Grimes 35026f9a767SRodney W. Grimes /* 35126f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 35226f9a767SRodney W. Grimes * in ni_vp amoung other things. 353ae1078d6SWayne Salamon * 354ae1078d6SWayne Salamon * XXXAUDIT: It would be desirable to also audit the name of the 355ae1078d6SWayne Salamon * interpreter if this is an interpreted binary. 35626f9a767SRodney W. Grimes */ 35726f9a767SRodney W. Grimes ndp = &nd; 3584bb260adSRobert Watson NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE | 3594bb260adSRobert Watson AUDITVNODE1, UIO_SYSSPACE, args->fname, td); 36026f9a767SRodney W. Grimes 36126f9a767SRodney W. Grimes interpret: 36226f9a767SRodney W. Grimes error = namei(ndp); 363610ecfe0SMaxim Sobolev if (error) 36426f9a767SRodney W. Grimes goto exec_fail; 36526f9a767SRodney W. Grimes 366269576c8SJeff Roberson vfslocked = NDHASGIANT(ndp); 367c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 36826f9a767SRodney W. Grimes 36926f9a767SRodney W. Grimes /* 3709022e4adSDavid Greenman * Check file permissions (also 'opens' file) 3719022e4adSDavid Greenman */ 372c52007c2SDavid Greenman error = exec_check_permissions(imgp); 373619eb6e5SJeff Roberson if (error) 37426f9a767SRodney W. Grimes goto exec_fail_dealloc; 375619eb6e5SJeff Roberson 3768516dd18SPoul-Henning Kamp imgp->object = imgp->vp->v_object; 3778516dd18SPoul-Henning Kamp if (imgp->object != NULL) 3780b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 37926f9a767SRodney W. Grimes 380619eb6e5SJeff Roberson /* 381619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 382619eb6e5SJeff Roberson * activating it. 383619eb6e5SJeff Roberson * 384619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 385619eb6e5SJeff Roberson * actually an executable image. 386619eb6e5SJeff Roberson */ 387619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 388619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 389619eb6e5SJeff Roberson 3901616db3cSJohn Dyson error = exec_map_first_page(imgp); 3916d5a0a8cSDavid Greenman if (error) 39226f9a767SRodney W. Grimes goto exec_fail_dealloc; 39326f9a767SRodney W. Grimes 39426f9a767SRodney W. Grimes /* 395d323ddf3SMatthew Dillon * If the current process has a special image activator it 396d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 397d323ddf3SMatthew Dillon * scripts differently. 39826f9a767SRodney W. Grimes */ 399d323ddf3SMatthew Dillon error = -1; 400d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 401d323ddf3SMatthew Dillon error = img_first(imgp); 402d323ddf3SMatthew Dillon 403d323ddf3SMatthew Dillon /* 404d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 405d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 406d323ddf3SMatthew Dillon * and an error otherwise. 407d323ddf3SMatthew Dillon */ 408d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 409d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 410d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 411d323ddf3SMatthew Dillon continue; 412d323ddf3SMatthew Dillon } 413c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 414d323ddf3SMatthew Dillon } 415d323ddf3SMatthew Dillon 416d323ddf3SMatthew Dillon if (error) { 417619eb6e5SJeff Roberson if (error == -1) { 418619eb6e5SJeff Roberson if (textset == 0) 419619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 420d323ddf3SMatthew Dillon error = ENOEXEC; 421619eb6e5SJeff Roberson } 42226f9a767SRodney W. Grimes goto exec_fail_dealloc; 423d323ddf3SMatthew Dillon } 424d323ddf3SMatthew Dillon 425d323ddf3SMatthew Dillon /* 426d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 427d323ddf3SMatthew Dillon * activate the interpreter. 428d323ddf3SMatthew Dillon */ 429c52007c2SDavid Greenman if (imgp->interpreted) { 4301616db3cSJohn Dyson exec_unmap_first_page(imgp); 431619eb6e5SJeff Roberson /* 432619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 433619eb6e5SJeff Roberson * period before we determine that something is a script where 434619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 435619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 436619eb6e5SJeff Roberson */ 437619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 438762e6b85SEivind Eklund /* free name buffer and old vnode */ 439762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 440670cb89bSRobert Watson #ifdef MAC 441eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 44230d239bcSRobert Watson mac_vnode_copy_label(ndp->ni_vp->v_label, interplabel); 443670cb89bSRobert Watson #endif 444619eb6e5SJeff Roberson vput(ndp->ni_vp); 4450b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 4460b2ed1aeSJeff Roberson imgp->object = NULL; 447269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 448532c491bSJeff Roberson vfslocked = 0; 44926f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 450269576c8SJeff Roberson NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 451b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 45226f9a767SRodney W. Grimes goto interpret; 45326f9a767SRodney W. Grimes } 45426f9a767SRodney W. Grimes 45526f9a767SRodney W. Grimes /* 45626f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 45726f9a767SRodney W. Grimes */ 4583ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 4593ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 4603ebc1248SPeter Wemm else 461c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 46226f9a767SRodney W. Grimes 46326f9a767SRodney W. Grimes /* 4641e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 4651e1e0b44SSøren Schmidt * let it do the stack setup. 4661e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 46726f9a767SRodney W. Grimes */ 468d6c847f3SBruce Evans if (p->p_sysent->sv_fixup != NULL) 469c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 4701e1e0b44SSøren Schmidt else 471610ecfe0SMaxim Sobolev suword(--stack_base, imgp->args->argc); 47226f9a767SRodney W. Grimes 473a78e8d2aSDavid Greenman /* 474a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 475a78e8d2aSDavid Greenman * be shared after an exec. 476a78e8d2aSDavid Greenman */ 477c113083cSPoul-Henning Kamp fdunshare(p, td); 478a78e8d2aSDavid Greenman 479333ea485SGuido van Rooij /* 4809b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4819b3b1c5fSJohn Baldwin */ 4829b3b1c5fSJohn Baldwin newcred = crget(); 4831419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 484610ecfe0SMaxim Sobolev i = imgp->args->begin_envv - imgp->args->begin_argv; 4855997cae9SDon Lewis /* Cache arguments if they fit inside our allowance */ 4865997cae9SDon Lewis if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 4879b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4885997cae9SDon Lewis bcopy(imgp->args->begin_argv, newargs->ar_args, i); 4895997cae9SDon Lewis } 4909b3b1c5fSJohn Baldwin 4919b3b1c5fSJohn Baldwin /* close files on exec */ 492d302786cSTor Egge VOP_UNLOCK(imgp->vp, 0, td); 4939b3b1c5fSJohn Baldwin fdcloseexec(td); 494d302786cSTor Egge vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td); 4959b3b1c5fSJohn Baldwin 496619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 497619eb6e5SJeff Roberson VREF(ndp->ni_vp); 498619eb6e5SJeff Roberson 4999b3b1c5fSJohn Baldwin /* 500333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 5018688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 502b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 503333ea485SGuido van Rooij * reset. 504333ea485SGuido van Rooij */ 5059b3b1c5fSJohn Baldwin PROC_LOCK(p); 50690af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 50790af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 5089b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 50990af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 51090af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 5119b3b1c5fSJohn Baldwin PROC_LOCK(p); 51290af4afaSJohn Baldwin p->p_sigacts = newsigacts; 51390af4afaSJohn Baldwin } else 51490af4afaSJohn Baldwin oldsigacts = NULL; 515333ea485SGuido van Rooij 516fdf4e8b3SWarner Losh /* Stop profiling */ 517fdf4e8b3SWarner Losh stopprofclock(p); 518fdf4e8b3SWarner Losh 51926f9a767SRodney W. Grimes /* reset caught signals */ 52026f9a767SRodney W. Grimes execsigs(p); 52126f9a767SRodney W. Grimes 52226f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 52326f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 52426f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 52526f9a767SRodney W. Grimes p->p_comm[len] = 0; 526ca081fdbSJulian Elischer bcopy(p->p_comm, td->td_name, sizeof(td->td_name)); 52726f9a767SRodney W. Grimes 52826f9a767SRodney W. Grimes /* 52924b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 530dc733423SDag-Erling Smørgrav * it that it now has its own resources back 53126f9a767SRodney W. Grimes */ 53226f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 53326f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 53426f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 5357f05b035SAlfred Perlstein wakeup(p->p_pptr); 53626f9a767SRodney W. Grimes } 53726f9a767SRodney W. Grimes 53826f9a767SRodney W. Grimes /* 539a78e8d2aSDavid Greenman * Implement image setuid/setgid. 540a78e8d2aSDavid Greenman * 541a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 542a78e8d2aSDavid Greenman * the process is being traced. 543670cb89bSRobert Watson * 544670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 545670cb89bSRobert Watson * transitions on the file system. 546c52007c2SDavid Greenman */ 547b1fc0ec1SRobert Watson oldcred = p->p_ucred; 548d06c0d4dSRobert Watson credential_changing = 0; 549d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 550d06c0d4dSRobert Watson attr.va_uid; 551d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 552d06c0d4dSRobert Watson attr.va_gid; 553ccafe7ebSRobert Watson #ifdef MAC 55430d239bcSRobert Watson will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp, 555eca8a663SRobert Watson interplabel, imgp); 556ccafe7ebSRobert Watson credential_changing |= will_transition; 557ccafe7ebSRobert Watson #endif 558d06c0d4dSRobert Watson 559d06c0d4dSRobert Watson if (credential_changing && 560a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 561c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 562c52007c2SDavid Greenman /* 563c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 564b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 565b85db196SPeter Wemm * we do not regain any tracing during a possible block. 56626f9a767SRodney W. Grimes */ 567b85db196SPeter Wemm setsugid(p); 568acd3428bSRobert Watson 5696c84de02SJohn Baldwin #ifdef KTRACE 570acd3428bSRobert Watson if (p->p_tracevp != NULL && 57132f9753cSRobert Watson priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) { 5726c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 57379deba82SMatthew Dillon p->p_traceflag = 0; 574a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 575a5881ea5SJohn Baldwin p->p_tracevp = NULL; 576a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 577a5881ea5SJohn Baldwin p->p_tracecred = NULL; 5786c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 57926f9a767SRodney W. Grimes } 5806c84de02SJohn Baldwin #endif 58128b325aaSDon Lewis /* 582c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 583c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 58428b325aaSDon Lewis * 585c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 586c1e2d386SNate Lawson * which may grab the process lock. 58728b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 58828b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 58928b325aaSDon Lewis */ 59028b325aaSDon Lewis PROC_UNLOCK(p); 591c1e2d386SNate Lawson setugidsafety(td); 592d302786cSTor Egge VOP_UNLOCK(imgp->vp, 0, td); 593e983a376SJacques Vidrine error = fdcheckstd(td); 594d302786cSTor Egge vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td); 59563c9e754SJohn Baldwin if (error != 0) 59669be5db9SAlfred Perlstein goto done1; 597e1b1aa3bSJohn Baldwin PROC_LOCK(p); 598c52007c2SDavid Greenman /* 599c52007c2SDavid Greenman * Set the new credentials. 600c52007c2SDavid Greenman */ 6019b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 602c52007c2SDavid Greenman if (attr.va_mode & VSUID) 6031419eacbSAlfred Perlstein change_euid(newcred, euip); 604c52007c2SDavid Greenman if (attr.va_mode & VSGID) 605b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 606ccafe7ebSRobert Watson #ifdef MAC 607670cb89bSRobert Watson if (will_transition) { 60830d239bcSRobert Watson mac_vnode_execve_transition(oldcred, newcred, imgp->vp, 609eca8a663SRobert Watson interplabel, imgp); 610670cb89bSRobert Watson } 611ccafe7ebSRobert Watson #endif 6129b3b1c5fSJohn Baldwin /* 6139b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 614ccafe7ebSRobert Watson * 615ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 616ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 617ccafe7ebSRobert Watson * though maybe it shouldn't. 6189b3b1c5fSJohn Baldwin */ 6199b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 6209b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 6219b3b1c5fSJohn Baldwin p->p_ucred = newcred; 6229b3b1c5fSJohn Baldwin newcred = NULL; 623c52007c2SDavid Greenman } else { 624b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 625b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 626c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 62726f9a767SRodney W. Grimes /* 628c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 629b1fc0ec1SRobert Watson * 630b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 6319b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 6329b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 6339b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 6349b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 6359b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 6369b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 6379b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 63826f9a767SRodney W. Grimes */ 639b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 640b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 6419b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 642b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 643b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 644b1fc0ec1SRobert Watson p->p_ucred = newcred; 6459b3b1c5fSJohn Baldwin newcred = NULL; 6469b3b1c5fSJohn Baldwin } 647b1fc0ec1SRobert Watson } 64826f9a767SRodney W. Grimes 64926f9a767SRodney W. Grimes /* 650619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 651619eb6e5SJeff Roberson * to locking the proc lock. 6522a531c80SDavid Greenman */ 6539b3b1c5fSJohn Baldwin textvp = p->p_textvp; 6542a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 6552a531c80SDavid Greenman 6562a531c80SDavid Greenman /* 6579ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 6589ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 659cb679c38SJonathan Lemon */ 660ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 6619ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 662cb679c38SJonathan Lemon 663cb679c38SJonathan Lemon /* 66426f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 66526f9a767SRodney W. Grimes * can be set before the program executes. 666906ac69dSDavid Xu * Use tdsignal to deliver signal to current thread, use 667906ac69dSDavid Xu * psignal may cause the signal to be delivered to wrong thread 668906ac69dSDavid Xu * because that thread will exit, remember we are going to enter 669906ac69dSDavid Xu * single thread mode. 67026f9a767SRodney W. Grimes */ 67126f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 6726d7b314bSDavid Xu tdsignal(p, td, SIGTRAP, NULL); 67326f9a767SRodney W. Grimes 67426f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 67526f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 67626f9a767SRodney W. Grimes 6775997cae9SDon Lewis /* 67834ea500bSDon Lewis * Free any previous argument cache and replace it with 6795997cae9SDon Lewis * the new argument cache, if any. 6805997cae9SDon Lewis */ 6819b3b1c5fSJohn Baldwin oldargs = p->p_args; 682e1b1aa3bSJohn Baldwin p->p_args = newargs; 683e1b1aa3bSJohn Baldwin newargs = NULL; 684ebccf1e3SJoseph Koshy 685ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 686ebccf1e3SJoseph Koshy /* 687f263522aSJoseph Koshy * Check if system-wide sampling is in effect or if the 688f263522aSJoseph Koshy * current process is using PMCs. If so, do exec() time 689ebccf1e3SJoseph Koshy * processing. This processing needs to happen AFTER the 690ebccf1e3SJoseph Koshy * P_INEXEC flag is cleared. 691ebccf1e3SJoseph Koshy * 692ebccf1e3SJoseph Koshy * The proc lock needs to be released before taking the PMC 693ebccf1e3SJoseph Koshy * SX. 694ebccf1e3SJoseph Koshy */ 695f263522aSJoseph Koshy if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) { 696e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 69715139246SJoseph Koshy pe.pm_credentialschanged = credential_changing; 69815139246SJoseph Koshy pe.pm_entryaddr = imgp->entry_addr; 69915139246SJoseph Koshy 70015139246SJoseph Koshy PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe); 701ebccf1e3SJoseph Koshy } else 702ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 703ebccf1e3SJoseph Koshy #else /* !HWPMC_HOOKS */ 704ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 705ebccf1e3SJoseph Koshy #endif 706e1b1aa3bSJohn Baldwin 707e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 7083ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 7093ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 7103ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 7113ebc1248SPeter Wemm else 712bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 713bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 714e913ca22SDoug Rabson 7159f5c1d19SDiomidis Spinellis vfs_mark_atime(imgp->vp, td); 7166341095eSKen Smith 71769be5db9SAlfred Perlstein done1: 7189b3b1c5fSJohn Baldwin /* 7199b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 7209b3b1c5fSJohn Baldwin */ 7211419eacbSAlfred Perlstein uifree(euip); 7229b3b1c5fSJohn Baldwin if (newcred == NULL) 7239b3b1c5fSJohn Baldwin crfree(oldcred); 7249b3b1c5fSJohn Baldwin else 7259b3b1c5fSJohn Baldwin crfree(newcred); 726d302786cSTor Egge VOP_UNLOCK(imgp->vp, 0, td); 7279b3b1c5fSJohn Baldwin /* 7289b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 7299b3b1c5fSJohn Baldwin */ 73068ce4375SJeff Roberson if (textvp != NULL) { 73168ce4375SJeff Roberson int tvfslocked; 73268ce4375SJeff Roberson 73368ce4375SJeff Roberson tvfslocked = VFS_LOCK_GIANT(textvp->v_mount); 7349b3b1c5fSJohn Baldwin vrele(textvp); 73568ce4375SJeff Roberson VFS_UNLOCK_GIANT(tvfslocked); 73668ce4375SJeff Roberson } 737619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 738619eb6e5SJeff Roberson vrele(ndp->ni_vp); 7396c84de02SJohn Baldwin #ifdef KTRACE 740ce0be646SJohn Baldwin if (tracevp != NULL) { 741ce0be646SJohn Baldwin int tvfslocked; 742ce0be646SJohn Baldwin 743ce0be646SJohn Baldwin tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount); 7449b3b1c5fSJohn Baldwin vrele(tracevp); 745ce0be646SJohn Baldwin VFS_UNLOCK_GIANT(tvfslocked); 746ce0be646SJohn Baldwin } 747a5881ea5SJohn Baldwin if (tracecred != NULL) 748a5881ea5SJohn Baldwin crfree(tracecred); 7496c84de02SJohn Baldwin #endif 750d302786cSTor Egge vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td); 75169be5db9SAlfred Perlstein if (oldargs != NULL) 7529b3b1c5fSJohn Baldwin pargs_drop(oldargs); 75369be5db9SAlfred Perlstein if (newargs != NULL) 75469be5db9SAlfred Perlstein pargs_drop(newargs); 75590af4afaSJohn Baldwin if (oldsigacts != NULL) 75690af4afaSJohn Baldwin sigacts_free(oldsigacts); 757b9df5231SPoul-Henning Kamp 7581616db3cSJohn Dyson exec_fail_dealloc: 7591616db3cSJohn Dyson 76026f9a767SRodney W. Grimes /* 76126f9a767SRodney W. Grimes * free various allocated resources 76226f9a767SRodney W. Grimes */ 763d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7641616db3cSJohn Dyson exec_unmap_first_page(imgp); 76526f9a767SRodney W. Grimes 766d6c847f3SBruce Evans if (imgp->vp != NULL) { 767762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 768619eb6e5SJeff Roberson vput(imgp->vp); 769a3cf6ebaSDavid Greenman } 77026f9a767SRodney W. Grimes 771d6c847f3SBruce Evans if (imgp->object != NULL) 7720b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 7730b2ed1aeSJeff Roberson 774d1989db5SRobert Drehmel if (error == 0) { 775d1989db5SRobert Drehmel /* 776d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 777d1989db5SRobert Drehmel * the S_EXEC bit set. 778d1989db5SRobert Drehmel */ 779d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 780116734c4SMatthew Dillon goto done2; 781d1989db5SRobert Drehmel } 7821616db3cSJohn Dyson 78326f9a767SRodney W. Grimes exec_fail: 7849ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 7859ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 7869ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 7879ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 7889ca45e81SDag-Erling Smørgrav 789116734c4SMatthew Dillon done2: 790670cb89bSRobert Watson #ifdef MAC 791670cb89bSRobert Watson mac_execve_exit(imgp); 792eca8a663SRobert Watson if (interplabel != NULL) 793eca8a663SRobert Watson mac_vnode_label_free(interplabel); 794670cb89bSRobert Watson #endif 795269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 7968917b8d2SJohn Baldwin exec_free_args(args); 7978917b8d2SJohn Baldwin 7988917b8d2SJohn Baldwin if (error && imgp->vmspace_destroyed) { 7998917b8d2SJohn Baldwin /* sorry, no more process anymore. exit gracefully */ 8008917b8d2SJohn Baldwin exit1(td, W_EXITCODE(0, SIGABRT)); 8018917b8d2SJohn Baldwin /* NOT REACHED */ 8028917b8d2SJohn Baldwin } 803116734c4SMatthew Dillon return (error); 80426f9a767SRodney W. Grimes } 80526f9a767SRodney W. Grimes 8061616db3cSJohn Dyson int 8071616db3cSJohn Dyson exec_map_first_page(imgp) 8081616db3cSJohn Dyson struct image_params *imgp; 8091616db3cSJohn Dyson { 810d8aad40cSJohn Baldwin int rv, i; 81195461b45SJohn Dyson int initial_pagein; 81295461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 8131616db3cSJohn Dyson vm_object_t object; 8141616db3cSJohn Dyson 815d6c847f3SBruce Evans if (imgp->firstpage != NULL) 8161616db3cSJohn Dyson exec_unmap_first_page(imgp); 8171616db3cSJohn Dyson 8188516dd18SPoul-Henning Kamp object = imgp->vp->v_object; 8194d7d2993SJeff Roberson if (object == NULL) 8204d7d2993SJeff Roberson return (EACCES); 821fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 82295461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 82395461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 82495461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 82595461b45SJohn Dyson if (initial_pagein > object->size) 82695461b45SJohn Dyson initial_pagein = object->size; 82795461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 828d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 8296ec2fca5SAlan Cox if (ma[i]->valid) 8306ec2fca5SAlan Cox break; 8319af80719SAlan Cox if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy) 83206fa71cdSAlan Cox break; 833e69763a3SDoug Rabson vm_page_busy(ma[i]); 83495461b45SJohn Dyson } else { 835ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 8367bfda801SAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED); 83795461b45SJohn Dyson if (ma[i] == NULL) 83895461b45SJohn Dyson break; 83995461b45SJohn Dyson } 84095461b45SJohn Dyson } 84195461b45SJohn Dyson initial_pagein = i; 84295461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 84395461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 844ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 845ca0387efSJake Burkholder (ma[0]->valid == 0)) { 846ecbb00a2SDoug Rabson if (ma[0]) { 8476ec2fca5SAlan Cox vm_page_lock_queues(); 8488f9110f6SJohn Dyson vm_page_free(ma[0]); 84906fa71cdSAlan Cox vm_page_unlock_queues(); 8506ec2fca5SAlan Cox } 85106fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 852a7cddfedSJake Burkholder return (EIO); 8531616db3cSJohn Dyson } 8541616db3cSJohn Dyson } 8556ec2fca5SAlan Cox vm_page_lock_queues(); 856148b3f62SAlan Cox vm_page_hold(ma[0]); 85797646f56SAlan Cox vm_page_unlock_queues(); 8582a53696fSAlan Cox vm_page_wakeup(ma[0]); 8596ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 8601616db3cSJohn Dyson 86159c8bc40SAlan Cox imgp->firstpage = sf_buf_alloc(ma[0], 0); 86259c8bc40SAlan Cox imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 8631616db3cSJohn Dyson 864a7cddfedSJake Burkholder return (0); 8651616db3cSJohn Dyson } 8661616db3cSJohn Dyson 8671616db3cSJohn Dyson void 8681616db3cSJohn Dyson exec_unmap_first_page(imgp) 8691616db3cSJohn Dyson struct image_params *imgp; 8701616db3cSJohn Dyson { 87159c8bc40SAlan Cox vm_page_t m; 87223955314SAlfred Perlstein 873d6c847f3SBruce Evans if (imgp->firstpage != NULL) { 87459c8bc40SAlan Cox m = sf_buf_page(imgp->firstpage); 87559c8bc40SAlan Cox sf_buf_free(imgp->firstpage); 8761616db3cSJohn Dyson imgp->firstpage = NULL; 87759c8bc40SAlan Cox vm_page_lock_queues(); 87859c8bc40SAlan Cox vm_page_unhold(m); 87959c8bc40SAlan Cox vm_page_unlock_queues(); 8801616db3cSJohn Dyson } 8811616db3cSJohn Dyson } 8821616db3cSJohn Dyson 88326f9a767SRodney W. Grimes /* 88426f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 88526f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 88626f9a767SRodney W. Grimes * automatically in trap.c. 88726f9a767SRodney W. Grimes */ 88826f9a767SRodney W. Grimes int 88905ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 890c52007c2SDavid Greenman struct image_params *imgp; 89105ba50f5SJake Burkholder struct sysentvec *sv; 89226f9a767SRodney W. Grimes { 89326f9a767SRodney W. Grimes int error; 8945e20c11fSAlan Cox struct proc *p = imgp->proc; 8955e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 89605ba50f5SJake Burkholder vm_offset_t stack_addr; 89705ba50f5SJake Burkholder vm_map_t map; 89859d8f3ffSJohn Baldwin u_long ssiz; 89926f9a767SRodney W. Grimes 900c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 901993182e5SAlexander Leidinger imgp->sysent = sv; 90226f9a767SRodney W. Grimes 903bd37fd72SKris Kennaway /* May be called with Giant held */ 904993182e5SAlexander Leidinger EVENTHANDLER_INVOKE(process_exec, p, imgp); 9056f5dafeaSAlan Cox 9066f5dafeaSAlan Cox /* 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); 915b9eee07eSPeter Wemm pmap_remove_pages(vmspace_pmap(vmspace)); 91605ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 917af9ec885SJohn Dyson } else { 91889b57fcfSKonstantin Belousov error = vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 91989b57fcfSKonstantin Belousov if (error) 92089b57fcfSKonstantin Belousov return (error); 9215e20c11fSAlan Cox vmspace = p->p_vmspace; 92205ba50f5SJake Burkholder map = &vmspace->vm_map; 923af9ec885SJohn Dyson } 92426f9a767SRodney W. Grimes 92526f9a767SRodney W. Grimes /* Allocate a new stack */ 92659d8f3ffSJohn Baldwin if (sv->sv_maxssiz != NULL) 92759d8f3ffSJohn Baldwin ssiz = *sv->sv_maxssiz; 92859d8f3ffSJohn Baldwin else 92959d8f3ffSJohn Baldwin ssiz = maxssiz; 93059d8f3ffSJohn Baldwin stack_addr = sv->sv_usrstack - ssiz; 93159d8f3ffSJohn Baldwin error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz, 932fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 9332267af78SJulian Elischer if (error) 9342267af78SJulian Elischer return (error); 9352267af78SJulian Elischer 93663c47a5cSDoug Rabson #ifdef __ia64__ 937fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 938bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 93959d8f3ffSJohn Baldwin error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz, 940fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 941fd75d710SMarcel Moolenaar if (error) 942fd75d710SMarcel Moolenaar return (error); 94363c47a5cSDoug Rabson #endif 94463c47a5cSDoug Rabson 9452267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 9462267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 9472267af78SJulian Elischer * process stack so we can check the stack rlimit. 9482267af78SJulian Elischer */ 949cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 95059d8f3ffSJohn Baldwin vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz; 95126f9a767SRodney W. Grimes 95226f9a767SRodney W. Grimes return (0); 95326f9a767SRodney W. Grimes } 95426f9a767SRodney W. Grimes 95526f9a767SRodney W. Grimes /* 956873fbcd7SRobert Watson * Copy out argument and environment strings from the old process address 957873fbcd7SRobert Watson * space into the temporary string buffer. 95826f9a767SRodney W. Grimes */ 95926f9a767SRodney W. Grimes int 960610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname, 961610ecfe0SMaxim Sobolev enum uio_seg segflg, char **argv, char **envv) 96226f9a767SRodney W. Grimes { 96326f9a767SRodney W. Grimes char *argp, *envp; 964ecbb00a2SDoug Rabson int error; 965ecbb00a2SDoug Rabson size_t length; 96626f9a767SRodney W. Grimes 967610ecfe0SMaxim Sobolev error = 0; 968610ecfe0SMaxim Sobolev 969610ecfe0SMaxim Sobolev bzero(args, sizeof(*args)); 970610ecfe0SMaxim Sobolev if (argv == NULL) 971610ecfe0SMaxim Sobolev return (EFAULT); 97226f9a767SRodney W. Grimes /* 973610ecfe0SMaxim Sobolev * Allocate temporary demand zeroed space for argument and 97490dc539bSMaxim Sobolev * environment strings: 97590dc539bSMaxim Sobolev * 97690dc539bSMaxim Sobolev * o ARG_MAX for argument and environment; 97790dc539bSMaxim Sobolev * o MAXSHELLCMDLEN for the name of interpreters. 97826f9a767SRodney W. Grimes */ 97990dc539bSMaxim Sobolev args->buf = (char *) kmem_alloc_wait(exec_map, 98090dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 981610ecfe0SMaxim Sobolev if (args->buf == NULL) 982610ecfe0SMaxim Sobolev return (ENOMEM); 983610ecfe0SMaxim Sobolev args->begin_argv = args->buf; 984610ecfe0SMaxim Sobolev args->endp = args->begin_argv; 985610ecfe0SMaxim Sobolev args->stringspace = ARG_MAX; 98626f9a767SRodney W. Grimes 987610ecfe0SMaxim Sobolev args->fname = args->buf + ARG_MAX; 98826f9a767SRodney W. Grimes 989610ecfe0SMaxim Sobolev /* 990610ecfe0SMaxim Sobolev * Copy the file name. 991610ecfe0SMaxim Sobolev */ 992610ecfe0SMaxim Sobolev error = (segflg == UIO_SYSSPACE) ? 993610ecfe0SMaxim Sobolev copystr(fname, args->fname, PATH_MAX, &length) : 994610ecfe0SMaxim Sobolev copyinstr(fname, args->fname, PATH_MAX, &length); 995c30af532SMaxim Sobolev if (error != 0) 99668ff3c24SStephan Uphoff goto err_exit; 99726f9a767SRodney W. Grimes 998610ecfe0SMaxim Sobolev /* 999610ecfe0SMaxim Sobolev * extract arguments first 1000610ecfe0SMaxim Sobolev */ 1001610ecfe0SMaxim Sobolev while ((argp = (caddr_t) (intptr_t) fuword(argv++))) { 100268ff3c24SStephan Uphoff if (argp == (caddr_t) -1) { 100368ff3c24SStephan Uphoff error = EFAULT; 100468ff3c24SStephan Uphoff goto err_exit; 100568ff3c24SStephan Uphoff } 1006610ecfe0SMaxim Sobolev if ((error = copyinstr(argp, args->endp, 1007610ecfe0SMaxim Sobolev args->stringspace, &length))) { 1008610ecfe0SMaxim Sobolev if (error == ENAMETOOLONG) 100968ff3c24SStephan Uphoff error = E2BIG; 101068ff3c24SStephan Uphoff goto err_exit; 1011610ecfe0SMaxim Sobolev } 1012610ecfe0SMaxim Sobolev args->stringspace -= length; 1013610ecfe0SMaxim Sobolev args->endp += length; 1014610ecfe0SMaxim Sobolev args->argc++; 1015610ecfe0SMaxim Sobolev } 1016610ecfe0SMaxim Sobolev 1017610ecfe0SMaxim Sobolev args->begin_envv = args->endp; 1018b9df5231SPoul-Henning Kamp 101926f9a767SRodney W. Grimes /* 102026f9a767SRodney W. Grimes * extract environment strings 102126f9a767SRodney W. Grimes */ 10220fd1a014SDavid Greenman if (envv) { 1023aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 102468ff3c24SStephan Uphoff if (envp == (caddr_t)-1) { 102568ff3c24SStephan Uphoff error = EFAULT; 102668ff3c24SStephan Uphoff goto err_exit; 102768ff3c24SStephan Uphoff } 1028610ecfe0SMaxim Sobolev if ((error = copyinstr(envp, args->endp, 1029610ecfe0SMaxim Sobolev args->stringspace, &length))) { 10300fd1a014SDavid Greenman if (error == ENAMETOOLONG) 103168ff3c24SStephan Uphoff error = E2BIG; 103268ff3c24SStephan Uphoff goto err_exit; 10330fd1a014SDavid Greenman } 1034610ecfe0SMaxim Sobolev args->stringspace -= length; 1035610ecfe0SMaxim Sobolev args->endp += length; 1036610ecfe0SMaxim Sobolev args->envc++; 103726f9a767SRodney W. Grimes } 10380fd1a014SDavid Greenman } 103926f9a767SRodney W. Grimes 104026f9a767SRodney W. Grimes return (0); 104168ff3c24SStephan Uphoff 104268ff3c24SStephan Uphoff err_exit: 104368ff3c24SStephan Uphoff exec_free_args(args); 104468ff3c24SStephan Uphoff return (error); 104526f9a767SRodney W. Grimes } 104626f9a767SRodney W. Grimes 10478917b8d2SJohn Baldwin static void 1048610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args) 1049610ecfe0SMaxim Sobolev { 1050610ecfe0SMaxim Sobolev 1051610ecfe0SMaxim Sobolev if (args->buf) { 105290dc539bSMaxim Sobolev kmem_free_wakeup(exec_map, (vm_offset_t)args->buf, 105390dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 1054610ecfe0SMaxim Sobolev args->buf = NULL; 1055610ecfe0SMaxim Sobolev } 1056610ecfe0SMaxim Sobolev } 1057610ecfe0SMaxim Sobolev 105826f9a767SRodney W. Grimes /* 1059873fbcd7SRobert Watson * Copy strings out to the new process address space, constructing new arg 1060873fbcd7SRobert Watson * and env vector tables. Return a pointer to the base so that it can be used 1061873fbcd7SRobert Watson * as the initial stack pointer. 106226f9a767SRodney W. Grimes */ 1063654f6be1SBruce Evans register_t * 1064c52007c2SDavid Greenman exec_copyout_strings(imgp) 1065c52007c2SDavid Greenman struct image_params *imgp; 106626f9a767SRodney W. Grimes { 106726f9a767SRodney W. Grimes int argc, envc; 106826f9a767SRodney W. Grimes char **vectp; 106926f9a767SRodney W. Grimes char *stringp, *destp; 1070654f6be1SBruce Evans register_t *stack_base; 107193f6448cSDavid Greenman struct ps_strings *arginfo; 1072f3bec5d7SJake Burkholder struct proc *p; 1073d66a5066SPeter Wemm int szsigcode; 107426f9a767SRodney W. Grimes 107526f9a767SRodney W. Grimes /* 107626f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 1077d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 107826f9a767SRodney W. Grimes */ 1079f3bec5d7SJake Burkholder p = imgp->proc; 1080f3bec5d7SJake Burkholder szsigcode = 0; 108105ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1082f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1083f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1084d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 1085610ecfe0SMaxim Sobolev roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 10861ed012f9SPeter Wemm 108726f9a767SRodney W. Grimes /* 1088d66a5066SPeter Wemm * install sigcode 1089d66a5066SPeter Wemm */ 1090d66a5066SPeter Wemm if (szsigcode) 1091f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1092f3bec5d7SJake Burkholder szsigcode), szsigcode); 1093d66a5066SPeter Wemm 1094d66a5066SPeter Wemm /* 1095e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1096e1743d02SSøren Schmidt * on the stack. 1097e1743d02SSøren Schmidt */ 1098b9a22da4STakanori Watanabe if (imgp->auxargs) { 1099e1743d02SSøren Schmidt /* 1100b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1101b9a22da4STakanori Watanabe * lower compatibility. 1102b9a22da4STakanori Watanabe */ 1103ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1104ca0387efSJake Burkholder (AT_COUNT * 2); 1105b9a22da4STakanori Watanabe /* 1106b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1107b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1108b9a22da4STakanori Watanabe * for argument of Runtime loader. 1109e1743d02SSøren Schmidt */ 1110610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + 1111610ecfe0SMaxim Sobolev imgp->args->envc + 2 + imgp->auxarg_size) * 1112610ecfe0SMaxim Sobolev sizeof(char *)); 1113b9a22da4STakanori Watanabe 1114610ecfe0SMaxim Sobolev } else { 1115e1743d02SSøren Schmidt /* 1116b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1117b9a22da4STakanori Watanabe * the arg and env vector sets 111826f9a767SRodney W. Grimes */ 1119610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * 1120ca0387efSJake Burkholder sizeof(char *)); 1121610ecfe0SMaxim Sobolev } 112226f9a767SRodney W. Grimes 112326f9a767SRodney W. Grimes /* 112426f9a767SRodney W. Grimes * vectp also becomes our initial stack base 112526f9a767SRodney W. Grimes */ 1126654f6be1SBruce Evans stack_base = (register_t *)vectp; 112726f9a767SRodney W. Grimes 1128610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 1129610ecfe0SMaxim Sobolev argc = imgp->args->argc; 1130610ecfe0SMaxim Sobolev envc = imgp->args->envc; 113126f9a767SRodney W. Grimes 113293f6448cSDavid Greenman /* 11333aed948bSDavid Greenman * Copy out strings - arguments and environment. 113493f6448cSDavid Greenman */ 1135610ecfe0SMaxim Sobolev copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 113693f6448cSDavid Greenman 113793f6448cSDavid Greenman /* 11383aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 11393aed948bSDavid Greenman */ 1140aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 11413aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 11423aed948bSDavid Greenman 11433aed948bSDavid Greenman /* 11443aed948bSDavid Greenman * Fill in argument portion of vector table. 114593f6448cSDavid Greenman */ 114626f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1147aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11483aed948bSDavid Greenman while (*stringp++ != 0) 11493aed948bSDavid Greenman destp++; 11503aed948bSDavid Greenman destp++; 115126f9a767SRodney W. Grimes } 115226f9a767SRodney W. Grimes 11531a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 11546ab46d52SBruce Evans suword(vectp++, 0); 115526f9a767SRodney W. Grimes 1156aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 11573aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 115893f6448cSDavid Greenman 115993f6448cSDavid Greenman /* 11603aed948bSDavid Greenman * Fill in environment portion of vector table. 116193f6448cSDavid Greenman */ 116226f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1163aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11643aed948bSDavid Greenman while (*stringp++ != 0) 11653aed948bSDavid Greenman destp++; 11663aed948bSDavid Greenman destp++; 116726f9a767SRodney W. Grimes } 116826f9a767SRodney W. Grimes 116926f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 11706ab46d52SBruce Evans suword(vectp, 0); 117126f9a767SRodney W. Grimes 117226f9a767SRodney W. Grimes return (stack_base); 117326f9a767SRodney W. Grimes } 117426f9a767SRodney W. Grimes 117526f9a767SRodney W. Grimes /* 117626f9a767SRodney W. Grimes * Check permissions of file to execute. 1177cf64863aSRobert Watson * Called with imgp->vp locked. 117826f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 117926f9a767SRodney W. Grimes */ 1180c8a79999SPeter Wemm int 1181c52007c2SDavid Greenman exec_check_permissions(imgp) 1182c52007c2SDavid Greenman struct image_params *imgp; 118326f9a767SRodney W. Grimes { 1184c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1185c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1186a854ed98SJohn Baldwin struct thread *td; 118726f9a767SRodney W. Grimes int error; 118826f9a767SRodney W. Grimes 1189a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1190339b79b9SRobert Watson 1191ec35c2afSRobert Watson /* Get file attributes */ 1192ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1193ec35c2afSRobert Watson if (error) 1194ec35c2afSRobert Watson return (error); 1195ec35c2afSRobert Watson 1196339b79b9SRobert Watson #ifdef MAC 119730d239bcSRobert Watson error = mac_vnode_check_exec(td->td_ucred, imgp->vp, imgp); 1198339b79b9SRobert Watson if (error) 1199339b79b9SRobert Watson return (error); 1200339b79b9SRobert Watson #endif 1201339b79b9SRobert Watson 120226f9a767SRodney W. Grimes /* 120326f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 120426f9a767SRodney W. Grimes * file resides on. 120526f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 120626f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 120726f9a767SRodney W. Grimes * file really is executable. 120826f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 120926f9a767SRodney W. Grimes */ 1210c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 121126f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1212a854ed98SJohn Baldwin (attr->va_type != VREG)) 121326f9a767SRodney W. Grimes return (EACCES); 121426f9a767SRodney W. Grimes 121526f9a767SRodney W. Grimes /* 121626f9a767SRodney W. Grimes * Zero length files can't be exec'd 121726f9a767SRodney W. Grimes */ 121826f9a767SRodney W. Grimes if (attr->va_size == 0) 121926f9a767SRodney W. Grimes return (ENOEXEC); 122026f9a767SRodney W. Grimes 122126f9a767SRodney W. Grimes /* 122226f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 122326f9a767SRodney W. Grimes */ 1224a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 122526f9a767SRodney W. Grimes if (error) 122626f9a767SRodney W. Grimes return (error); 122726f9a767SRodney W. Grimes 12286d5a0a8cSDavid Greenman /* 12296d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 12306d5a0a8cSDavid Greenman * if there are any. 12316d5a0a8cSDavid Greenman */ 12326d5a0a8cSDavid Greenman if (vp->v_writecount) 12336d5a0a8cSDavid Greenman return (ETXTBSY); 12346d5a0a8cSDavid Greenman 12356d5a0a8cSDavid Greenman /* 12366d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 12376d5a0a8cSDavid Greenman * general case). 12386d5a0a8cSDavid Greenman */ 12399e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 124026f9a767SRodney W. Grimes return (error); 1241df8bae1dSRodney W. Grimes } 1242aa855a59SPeter Wemm 1243aa855a59SPeter Wemm /* 1244aa855a59SPeter Wemm * Exec handler registration 1245aa855a59SPeter Wemm */ 1246aa855a59SPeter Wemm int 1247aa855a59SPeter Wemm exec_register(execsw_arg) 1248aa855a59SPeter Wemm const struct execsw *execsw_arg; 1249aa855a59SPeter Wemm { 1250aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1251aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1252aa855a59SPeter Wemm 1253aa855a59SPeter Wemm if (execsw) 1254aa855a59SPeter Wemm for (es = execsw; *es; es++) 1255aa855a59SPeter Wemm count++; 1256a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1257aa855a59SPeter Wemm if (newexecsw == NULL) 1258a7cddfedSJake Burkholder return (ENOMEM); 1259aa855a59SPeter Wemm xs = newexecsw; 1260aa855a59SPeter Wemm if (execsw) 1261aa855a59SPeter Wemm for (es = execsw; *es; es++) 1262aa855a59SPeter Wemm *xs++ = *es; 1263aa855a59SPeter Wemm *xs++ = execsw_arg; 1264aa855a59SPeter Wemm *xs = NULL; 1265aa855a59SPeter Wemm if (execsw) 1266aa855a59SPeter Wemm free(execsw, M_TEMP); 1267aa855a59SPeter Wemm execsw = newexecsw; 1268a7cddfedSJake Burkholder return (0); 1269aa855a59SPeter Wemm } 1270aa855a59SPeter Wemm 1271aa855a59SPeter Wemm int 1272aa855a59SPeter Wemm exec_unregister(execsw_arg) 1273aa855a59SPeter Wemm const struct execsw *execsw_arg; 1274aa855a59SPeter Wemm { 1275aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1276aa855a59SPeter Wemm int count = 1; 1277aa855a59SPeter Wemm 1278aa855a59SPeter Wemm if (execsw == NULL) 1279aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1280aa855a59SPeter Wemm 1281aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1282aa855a59SPeter Wemm if (*es == execsw_arg) 1283aa855a59SPeter Wemm break; 1284aa855a59SPeter Wemm } 1285aa855a59SPeter Wemm if (*es == NULL) 1286a7cddfedSJake Burkholder return (ENOENT); 1287aa855a59SPeter Wemm for (es = execsw; *es; es++) 1288aa855a59SPeter Wemm if (*es != execsw_arg) 1289aa855a59SPeter Wemm count++; 1290a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1291aa855a59SPeter Wemm if (newexecsw == NULL) 1292a7cddfedSJake Burkholder return (ENOMEM); 1293aa855a59SPeter Wemm xs = newexecsw; 1294aa855a59SPeter Wemm for (es = execsw; *es; es++) 1295aa855a59SPeter Wemm if (*es != execsw_arg) 1296aa855a59SPeter Wemm *xs++ = *es; 1297aa855a59SPeter Wemm *xs = NULL; 1298aa855a59SPeter Wemm if (execsw) 1299aa855a59SPeter Wemm free(execsw, M_TEMP); 1300aa855a59SPeter Wemm execsw = newexecsw; 1301a7cddfedSJake Burkholder return (0); 1302aa855a59SPeter Wemm } 1303