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" 33f8a47341SAlan Cox #include "opt_vm.h" 341e9b3d91SPeter Wemm 35df8bae1dSRodney W. Grimes #include <sys/param.h> 3626f9a767SRodney W. Grimes #include <sys/systm.h> 3775b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 38fb919e4dSMark Murray #include <sys/lock.h> 39fb919e4dSMark Murray #include <sys/mutex.h> 40d2d3e875SBruce Evans #include <sys/sysproto.h> 4126f9a767SRodney W. Grimes #include <sys/signalvar.h> 4226f9a767SRodney W. Grimes #include <sys/kernel.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> 52acd3428bSRobert Watson #include <sys/priv.h> 53a794e791SBruce Evans #include <sys/proc.h> 542a024a2bSSean Eric Fagan #include <sys/pioctl.h> 55a794e791SBruce Evans #include <sys/namei.h> 566004362eSDavid Schultz #include <sys/resourcevar.h> 5759c8bc40SAlan Cox #include <sys/sf_buf.h> 5884e0b075SDavid Xu #include <sys/syscallsubr.h> 591e1e0b44SSøren Schmidt #include <sys/sysent.h> 60797f2d22SPoul-Henning Kamp #include <sys/shm.h> 6199ac3bc8SPeter Wemm #include <sys/sysctl.h> 62a794e791SBruce Evans #include <sys/vnode.h> 63c781aea8SPeter Wemm #ifdef KTRACE 64c781aea8SPeter Wemm #include <sys/ktrace.h> 65c781aea8SPeter Wemm #endif 6626f9a767SRodney W. Grimes 6726f9a767SRodney W. Grimes #include <vm/vm.h> 68efeaf95aSDavid Greenman #include <vm/vm_param.h> 69efeaf95aSDavid Greenman #include <vm/pmap.h> 701616db3cSJohn Dyson #include <vm/vm_page.h> 71efeaf95aSDavid Greenman #include <vm/vm_map.h> 7226f9a767SRodney W. Grimes #include <vm/vm_kern.h> 73efeaf95aSDavid Greenman #include <vm/vm_extern.h> 741ebd0c59SDavid Greenman #include <vm/vm_object.h> 751616db3cSJohn Dyson #include <vm/vm_pager.h> 7626f9a767SRodney W. Grimes 77ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 78ebccf1e3SJoseph Koshy #include <sys/pmckern.h> 79ebccf1e3SJoseph Koshy #endif 80ebccf1e3SJoseph Koshy 8126f9a767SRodney W. Grimes #include <machine/reg.h> 8226f9a767SRodney W. Grimes 83ae1078d6SWayne Salamon #include <security/audit/audit.h> 84aed55708SRobert Watson #include <security/mac/mac_framework.h> 85ae1078d6SWayne Salamon 86b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 87b9df5231SPoul-Henning Kamp 8805ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 8905ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 905dadd17bSJake Burkholder static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); 91610ecfe0SMaxim Sobolev static int do_execve(struct thread *td, struct image_args *args, 92610ecfe0SMaxim Sobolev struct mac *mac_p); 938917b8d2SJohn Baldwin static void exec_free_args(struct image_args *); 9405ba50f5SJake Burkholder 959701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9605ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9705ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 98486bddb0SDoug Rabson 999701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 10005ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 10105ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 10299ac3bc8SPeter Wemm 1035dadd17bSJake Burkholder SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD, 1045dadd17bSJake Burkholder NULL, 0, sysctl_kern_stackprot, "I", ""); 1055dadd17bSJake Burkholder 106b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 107c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1089701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 109b9df5231SPoul-Henning Kamp 11005ba50f5SJake Burkholder static int 11105ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 11205ba50f5SJake Burkholder { 11305ba50f5SJake Burkholder struct proc *p; 114df7c361eSPeter Wemm int error; 11505ba50f5SJake Burkholder 11605ba50f5SJake Burkholder p = curproc; 117a7bc3102SPeter Wemm #ifdef SCTL_MASK32 118a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 119df7c361eSPeter Wemm unsigned int val; 120df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_psstrings; 121df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 122df7c361eSPeter Wemm } else 123df7c361eSPeter Wemm #endif 124df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 125df7c361eSPeter Wemm sizeof(p->p_sysent->sv_psstrings)); 126df7c361eSPeter Wemm return error; 12705ba50f5SJake Burkholder } 12805ba50f5SJake Burkholder 12905ba50f5SJake Burkholder static int 13005ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 13105ba50f5SJake Burkholder { 13205ba50f5SJake Burkholder struct proc *p; 133df7c361eSPeter Wemm int error; 13405ba50f5SJake Burkholder 13505ba50f5SJake Burkholder p = curproc; 136a7bc3102SPeter Wemm #ifdef SCTL_MASK32 137a7bc3102SPeter Wemm if (req->flags & SCTL_MASK32) { 138df7c361eSPeter Wemm unsigned int val; 139df7c361eSPeter Wemm val = (unsigned int)p->p_sysent->sv_usrstack; 140df7c361eSPeter Wemm error = SYSCTL_OUT(req, &val, sizeof(val)); 141df7c361eSPeter Wemm } else 142df7c361eSPeter Wemm #endif 143df7c361eSPeter Wemm error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 144df7c361eSPeter Wemm sizeof(p->p_sysent->sv_usrstack)); 145df7c361eSPeter Wemm return error; 14605ba50f5SJake Burkholder } 14705ba50f5SJake Burkholder 1485dadd17bSJake Burkholder static int 1495dadd17bSJake Burkholder sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS) 1505dadd17bSJake Burkholder { 1515dadd17bSJake Burkholder struct proc *p; 1525dadd17bSJake Burkholder 1535dadd17bSJake Burkholder p = curproc; 1545dadd17bSJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot, 1555dadd17bSJake Burkholder sizeof(p->p_sysent->sv_stackprot))); 1565dadd17bSJake Burkholder } 1575dadd17bSJake Burkholder 15899ac3bc8SPeter Wemm /* 159aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 160aa855a59SPeter Wemm * double pointer here. 161df8bae1dSRodney W. Grimes */ 162aa855a59SPeter Wemm static const struct execsw **execsw; 16326f9a767SRodney W. Grimes 164ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 165ca46e90eSBruce Evans struct execve_args { 166ca46e90eSBruce Evans char *fname; 167ca46e90eSBruce Evans char **argv; 168ca46e90eSBruce Evans char **envv; 169ca46e90eSBruce Evans }; 170ca46e90eSBruce Evans #endif 171ca46e90eSBruce Evans 172ca46e90eSBruce Evans int 173ca46e90eSBruce Evans execve(td, uap) 174ca46e90eSBruce Evans struct thread *td; 175ca46e90eSBruce Evans struct execve_args /* { 176ca46e90eSBruce Evans char *fname; 177ca46e90eSBruce Evans char **argv; 178ca46e90eSBruce Evans char **envv; 179ca46e90eSBruce Evans } */ *uap; 180ca46e90eSBruce Evans { 181610ecfe0SMaxim Sobolev int error; 182610ecfe0SMaxim Sobolev struct image_args args; 183ca46e90eSBruce Evans 184610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 185610ecfe0SMaxim Sobolev uap->argv, uap->envv); 186610ecfe0SMaxim Sobolev if (error == 0) 187610ecfe0SMaxim Sobolev error = kern_execve(td, &args, NULL); 188610ecfe0SMaxim Sobolev return (error); 189ca46e90eSBruce Evans } 190ca46e90eSBruce Evans 191ca46e90eSBruce Evans #ifndef _SYS_SYSPROTO_H_ 192ca46e90eSBruce Evans struct __mac_execve_args { 193ca46e90eSBruce Evans char *fname; 194ca46e90eSBruce Evans char **argv; 195ca46e90eSBruce Evans char **envv; 196ca46e90eSBruce Evans struct mac *mac_p; 197ca46e90eSBruce Evans }; 198ca46e90eSBruce Evans #endif 199ca46e90eSBruce Evans 200ca46e90eSBruce Evans int 201ca46e90eSBruce Evans __mac_execve(td, uap) 202ca46e90eSBruce Evans struct thread *td; 203ca46e90eSBruce Evans struct __mac_execve_args /* { 204ca46e90eSBruce Evans char *fname; 205ca46e90eSBruce Evans char **argv; 206ca46e90eSBruce Evans char **envv; 207ca46e90eSBruce Evans struct mac *mac_p; 208ca46e90eSBruce Evans } */ *uap; 209ca46e90eSBruce Evans { 210ca46e90eSBruce Evans #ifdef MAC 211610ecfe0SMaxim Sobolev int error; 212610ecfe0SMaxim Sobolev struct image_args args; 213610ecfe0SMaxim Sobolev 214610ecfe0SMaxim Sobolev error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE, 215610ecfe0SMaxim Sobolev uap->argv, uap->envv); 216610ecfe0SMaxim Sobolev if (error == 0) 217610ecfe0SMaxim Sobolev error = kern_execve(td, &args, uap->mac_p); 218610ecfe0SMaxim Sobolev return (error); 219ca46e90eSBruce Evans #else 220ca46e90eSBruce Evans return (ENOSYS); 221ca46e90eSBruce Evans #endif 222ca46e90eSBruce Evans } 223ca46e90eSBruce Evans 22433812c06SColin Percival /* 225873fbcd7SRobert Watson * XXX: kern_execve has the astonishing property of not always returning to 226873fbcd7SRobert Watson * the caller. If sufficiently bad things happen during the call to 227873fbcd7SRobert Watson * do_execve(), it can end up calling exit1(); as a result, callers must 228873fbcd7SRobert Watson * avoid doing anything which they might need to undo (e.g., allocating 229873fbcd7SRobert Watson * memory). 23033812c06SColin Percival */ 23184e0b075SDavid Xu int 232610ecfe0SMaxim Sobolev kern_execve(td, args, mac_p) 233906ac69dSDavid Xu struct thread *td; 234610ecfe0SMaxim Sobolev struct image_args *args; 235906ac69dSDavid Xu struct mac *mac_p; 236906ac69dSDavid Xu { 237906ac69dSDavid Xu struct proc *p = td->td_proc; 238906ac69dSDavid Xu int error; 239906ac69dSDavid Xu 240ae1078d6SWayne Salamon AUDIT_ARG(argv, args->begin_argv, args->argc, 241ae1078d6SWayne Salamon args->begin_envv - args->begin_argv); 242ae1078d6SWayne Salamon AUDIT_ARG(envv, args->begin_envv, args->envc, 243ae1078d6SWayne Salamon args->endp - args->begin_envv); 244906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 245906ac69dSDavid Xu PROC_LOCK(p); 246906ac69dSDavid Xu if (thread_single(SINGLE_BOUNDARY)) { 247906ac69dSDavid Xu PROC_UNLOCK(p); 24868ff3c24SStephan Uphoff exec_free_args(args); 249906ac69dSDavid Xu return (ERESTART); /* Try again later. */ 250906ac69dSDavid Xu } 251906ac69dSDavid Xu PROC_UNLOCK(p); 252906ac69dSDavid Xu } 253906ac69dSDavid Xu 254610ecfe0SMaxim Sobolev error = do_execve(td, args, mac_p); 255906ac69dSDavid Xu 256906ac69dSDavid Xu if (p->p_flag & P_HADTHREADS) { 257906ac69dSDavid Xu PROC_LOCK(p); 258906ac69dSDavid Xu /* 259906ac69dSDavid Xu * If success, we upgrade to SINGLE_EXIT state to 260906ac69dSDavid Xu * force other threads to suicide. 261906ac69dSDavid Xu */ 262906ac69dSDavid Xu if (error == 0) 263906ac69dSDavid Xu thread_single(SINGLE_EXIT); 264906ac69dSDavid Xu else 265906ac69dSDavid Xu thread_single_end(); 266906ac69dSDavid Xu PROC_UNLOCK(p); 267906ac69dSDavid Xu } 268906ac69dSDavid Xu 269906ac69dSDavid Xu return (error); 270906ac69dSDavid Xu } 271906ac69dSDavid Xu 27226f9a767SRodney W. Grimes /* 273450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 274450ffb44SRobert Watson * userspace pointers from the passed thread. 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. 354ae1078d6SWayne Salamon * 355ae1078d6SWayne Salamon * XXXAUDIT: It would be desirable to also audit the name of the 356ae1078d6SWayne Salamon * interpreter if this is an interpreted binary. 35726f9a767SRodney W. Grimes */ 35826f9a767SRodney W. Grimes ndp = &nd; 3594bb260adSRobert Watson NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE | 3604bb260adSRobert Watson AUDITVNODE1, UIO_SYSSPACE, args->fname, td); 36126f9a767SRodney W. Grimes 36226f9a767SRodney W. Grimes interpret: 36326f9a767SRodney W. Grimes error = namei(ndp); 364610ecfe0SMaxim Sobolev if (error) 36526f9a767SRodney W. Grimes goto exec_fail; 36626f9a767SRodney W. Grimes 367269576c8SJeff Roberson vfslocked = NDHASGIANT(ndp); 368c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 36926f9a767SRodney W. Grimes 37026f9a767SRodney W. Grimes /* 3719022e4adSDavid Greenman * Check file permissions (also 'opens' file) 3729022e4adSDavid Greenman */ 373c52007c2SDavid Greenman error = exec_check_permissions(imgp); 374619eb6e5SJeff Roberson if (error) 37526f9a767SRodney W. Grimes goto exec_fail_dealloc; 376619eb6e5SJeff Roberson 3778516dd18SPoul-Henning Kamp imgp->object = imgp->vp->v_object; 3788516dd18SPoul-Henning Kamp if (imgp->object != NULL) 3790b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 38026f9a767SRodney W. Grimes 381619eb6e5SJeff Roberson /* 382619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 383619eb6e5SJeff Roberson * activating it. 384619eb6e5SJeff Roberson * 385619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 386619eb6e5SJeff Roberson * actually an executable image. 387619eb6e5SJeff Roberson */ 388619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 389619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 390619eb6e5SJeff Roberson 3911616db3cSJohn Dyson error = exec_map_first_page(imgp); 3926d5a0a8cSDavid Greenman if (error) 39326f9a767SRodney W. Grimes goto exec_fail_dealloc; 39426f9a767SRodney W. Grimes 395f231de47SKonstantin Belousov imgp->proc->p_osrel = 0; 39626f9a767SRodney W. Grimes /* 397d323ddf3SMatthew Dillon * If the current process has a special image activator it 398d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 399d323ddf3SMatthew Dillon * scripts differently. 40026f9a767SRodney W. Grimes */ 401d323ddf3SMatthew Dillon error = -1; 402d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 403d323ddf3SMatthew Dillon error = img_first(imgp); 404d323ddf3SMatthew Dillon 405d323ddf3SMatthew Dillon /* 406d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 407d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 408d323ddf3SMatthew Dillon * and an error otherwise. 409d323ddf3SMatthew Dillon */ 410d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 411d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 412d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 413d323ddf3SMatthew Dillon continue; 414d323ddf3SMatthew Dillon } 415c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 416d323ddf3SMatthew Dillon } 417d323ddf3SMatthew Dillon 418d323ddf3SMatthew Dillon if (error) { 419619eb6e5SJeff Roberson if (error == -1) { 420619eb6e5SJeff Roberson if (textset == 0) 421619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 422d323ddf3SMatthew Dillon error = ENOEXEC; 423619eb6e5SJeff Roberson } 42426f9a767SRodney W. Grimes goto exec_fail_dealloc; 425d323ddf3SMatthew Dillon } 426d323ddf3SMatthew Dillon 427d323ddf3SMatthew Dillon /* 428d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 429d323ddf3SMatthew Dillon * activate the interpreter. 430d323ddf3SMatthew Dillon */ 431c52007c2SDavid Greenman if (imgp->interpreted) { 4321616db3cSJohn Dyson exec_unmap_first_page(imgp); 433619eb6e5SJeff Roberson /* 434619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 435619eb6e5SJeff Roberson * period before we determine that something is a script where 436619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 437619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 438619eb6e5SJeff Roberson */ 439619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 440762e6b85SEivind Eklund /* free name buffer and old vnode */ 441762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 442670cb89bSRobert Watson #ifdef MAC 443eca8a663SRobert Watson interplabel = mac_vnode_label_alloc(); 44430d239bcSRobert Watson mac_vnode_copy_label(ndp->ni_vp->v_label, interplabel); 445670cb89bSRobert Watson #endif 446619eb6e5SJeff Roberson vput(ndp->ni_vp); 4470b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 4480b2ed1aeSJeff Roberson imgp->object = NULL; 449269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 450532c491bSJeff Roberson vfslocked = 0; 45126f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 452269576c8SJeff Roberson NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE, 453b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 45426f9a767SRodney W. Grimes goto interpret; 45526f9a767SRodney W. Grimes } 45626f9a767SRodney W. Grimes 45726f9a767SRodney W. Grimes /* 45826f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 45926f9a767SRodney W. Grimes */ 4603ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 4613ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 4623ebc1248SPeter Wemm else 463c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 46426f9a767SRodney W. Grimes 46526f9a767SRodney W. Grimes /* 4661e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 4671e1e0b44SSøren Schmidt * let it do the stack setup. 4681e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 46926f9a767SRodney W. Grimes */ 470d6c847f3SBruce Evans if (p->p_sysent->sv_fixup != NULL) 471c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 4721e1e0b44SSøren Schmidt else 473610ecfe0SMaxim Sobolev suword(--stack_base, imgp->args->argc); 47426f9a767SRodney W. Grimes 475a78e8d2aSDavid Greenman /* 476a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 477a78e8d2aSDavid Greenman * be shared after an exec. 478a78e8d2aSDavid Greenman */ 479c113083cSPoul-Henning Kamp fdunshare(p, td); 480a78e8d2aSDavid Greenman 481333ea485SGuido van Rooij /* 4829b3b1c5fSJohn Baldwin * Malloc things before we need locks. 4839b3b1c5fSJohn Baldwin */ 4849b3b1c5fSJohn Baldwin newcred = crget(); 4851419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 486610ecfe0SMaxim Sobolev i = imgp->args->begin_envv - imgp->args->begin_argv; 4875997cae9SDon Lewis /* Cache arguments if they fit inside our allowance */ 4885997cae9SDon Lewis if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 4899b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 4905997cae9SDon Lewis bcopy(imgp->args->begin_argv, newargs->ar_args, i); 4915997cae9SDon Lewis } 4929b3b1c5fSJohn Baldwin 4939b3b1c5fSJohn Baldwin /* close files on exec */ 49422db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 4959b3b1c5fSJohn Baldwin fdcloseexec(td); 496cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 4979b3b1c5fSJohn Baldwin 498619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 499619eb6e5SJeff Roberson VREF(ndp->ni_vp); 500619eb6e5SJeff Roberson 5019b3b1c5fSJohn Baldwin /* 502333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 5038688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 504b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 505333ea485SGuido van Rooij * reset. 506333ea485SGuido van Rooij */ 5079b3b1c5fSJohn Baldwin PROC_LOCK(p); 50890af4afaSJohn Baldwin if (sigacts_shared(p->p_sigacts)) { 50990af4afaSJohn Baldwin oldsigacts = p->p_sigacts; 5109b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 51190af4afaSJohn Baldwin newsigacts = sigacts_alloc(); 51290af4afaSJohn Baldwin sigacts_copy(newsigacts, oldsigacts); 5139b3b1c5fSJohn Baldwin PROC_LOCK(p); 51490af4afaSJohn Baldwin p->p_sigacts = newsigacts; 51590af4afaSJohn Baldwin } else 51690af4afaSJohn Baldwin oldsigacts = NULL; 517333ea485SGuido van Rooij 518fdf4e8b3SWarner Losh /* Stop profiling */ 519fdf4e8b3SWarner Losh stopprofclock(p); 520fdf4e8b3SWarner Losh 52126f9a767SRodney W. Grimes /* reset caught signals */ 52226f9a767SRodney W. Grimes execsigs(p); 52326f9a767SRodney W. Grimes 52426f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 52526f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 52626f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 52726f9a767SRodney W. Grimes p->p_comm[len] = 0; 528ca081fdbSJulian Elischer bcopy(p->p_comm, td->td_name, sizeof(td->td_name)); 52926f9a767SRodney W. Grimes 53026f9a767SRodney W. Grimes /* 53124b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 532dc733423SDag-Erling Smørgrav * it that it now has its own resources back 53326f9a767SRodney W. Grimes */ 53426f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 53526f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 53626f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 5377f05b035SAlfred Perlstein wakeup(p->p_pptr); 53826f9a767SRodney W. Grimes } 53926f9a767SRodney W. Grimes 54026f9a767SRodney W. Grimes /* 541a78e8d2aSDavid Greenman * Implement image setuid/setgid. 542a78e8d2aSDavid Greenman * 543a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 544a78e8d2aSDavid Greenman * the process is being traced. 545670cb89bSRobert Watson * 546670cb89bSRobert Watson * XXXMAC: For the time being, use NOSUID to also prohibit 547670cb89bSRobert Watson * transitions on the file system. 548c52007c2SDavid Greenman */ 549b1fc0ec1SRobert Watson oldcred = p->p_ucred; 550d06c0d4dSRobert Watson credential_changing = 0; 551d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 552d06c0d4dSRobert Watson attr.va_uid; 553d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 554d06c0d4dSRobert Watson attr.va_gid; 555ccafe7ebSRobert Watson #ifdef MAC 55630d239bcSRobert Watson will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp, 557eca8a663SRobert Watson interplabel, imgp); 558ccafe7ebSRobert Watson credential_changing |= will_transition; 559ccafe7ebSRobert Watson #endif 560d06c0d4dSRobert Watson 561d06c0d4dSRobert Watson if (credential_changing && 562a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 563c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 564c52007c2SDavid Greenman /* 565c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 566b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 567b85db196SPeter Wemm * we do not regain any tracing during a possible block. 56826f9a767SRodney W. Grimes */ 569b85db196SPeter Wemm setsugid(p); 570acd3428bSRobert Watson 5716c84de02SJohn Baldwin #ifdef KTRACE 572acd3428bSRobert Watson if (p->p_tracevp != NULL && 57332f9753cSRobert Watson priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) { 5746c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 57579deba82SMatthew Dillon p->p_traceflag = 0; 576a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 577a5881ea5SJohn Baldwin p->p_tracevp = NULL; 578a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 579a5881ea5SJohn Baldwin p->p_tracecred = NULL; 5806c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 58126f9a767SRodney W. Grimes } 5826c84de02SJohn Baldwin #endif 58328b325aaSDon Lewis /* 584c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 585c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 58628b325aaSDon Lewis * 587c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 588c1e2d386SNate Lawson * which may grab the process lock. 58928b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 59028b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 59128b325aaSDon Lewis */ 59228b325aaSDon Lewis PROC_UNLOCK(p); 593c1e2d386SNate Lawson setugidsafety(td); 59422db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 595e983a376SJacques Vidrine error = fdcheckstd(td); 596cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 59763c9e754SJohn Baldwin if (error != 0) 59869be5db9SAlfred Perlstein goto done1; 599e1b1aa3bSJohn Baldwin PROC_LOCK(p); 600c52007c2SDavid Greenman /* 601c52007c2SDavid Greenman * Set the new credentials. 602c52007c2SDavid Greenman */ 6039b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 604c52007c2SDavid Greenman if (attr.va_mode & VSUID) 6051419eacbSAlfred Perlstein change_euid(newcred, euip); 606c52007c2SDavid Greenman if (attr.va_mode & VSGID) 607b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 608ccafe7ebSRobert Watson #ifdef MAC 609670cb89bSRobert Watson if (will_transition) { 61030d239bcSRobert Watson mac_vnode_execve_transition(oldcred, newcred, imgp->vp, 611eca8a663SRobert Watson interplabel, imgp); 612670cb89bSRobert Watson } 613ccafe7ebSRobert Watson #endif 6149b3b1c5fSJohn Baldwin /* 6159b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 616ccafe7ebSRobert Watson * 617ccafe7ebSRobert Watson * XXXMAC: Note that the current logic will save the 618ccafe7ebSRobert Watson * uid and gid if a MAC domain transition occurs, even 619ccafe7ebSRobert Watson * though maybe it shouldn't. 6209b3b1c5fSJohn Baldwin */ 6219b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 6229b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 6239b3b1c5fSJohn Baldwin p->p_ucred = newcred; 6249b3b1c5fSJohn Baldwin newcred = NULL; 625c52007c2SDavid Greenman } else { 626b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 627b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 628c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 62926f9a767SRodney W. Grimes /* 630c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 631b1fc0ec1SRobert Watson * 632b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 6339b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 6349b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 6359b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 6369b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 6379b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 6389b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 6399b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 64026f9a767SRodney W. Grimes */ 641b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 642b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 6439b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 644b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 645b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 646b1fc0ec1SRobert Watson p->p_ucred = newcred; 6479b3b1c5fSJohn Baldwin newcred = NULL; 6489b3b1c5fSJohn Baldwin } 649b1fc0ec1SRobert Watson } 65026f9a767SRodney W. Grimes 65126f9a767SRodney W. Grimes /* 652619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 653619eb6e5SJeff Roberson * to locking the proc lock. 6542a531c80SDavid Greenman */ 6559b3b1c5fSJohn Baldwin textvp = p->p_textvp; 6562a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 6572a531c80SDavid Greenman 6582a531c80SDavid Greenman /* 6599ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 6609ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 661cb679c38SJonathan Lemon */ 662ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXEC); 6639ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 664cb679c38SJonathan Lemon 665cb679c38SJonathan Lemon /* 66626f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 66726f9a767SRodney W. Grimes * can be set before the program executes. 668906ac69dSDavid Xu * Use tdsignal to deliver signal to current thread, use 669906ac69dSDavid Xu * psignal may cause the signal to be delivered to wrong thread 670906ac69dSDavid Xu * because that thread will exit, remember we are going to enter 671906ac69dSDavid Xu * single thread mode. 67226f9a767SRodney W. Grimes */ 67326f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 6746d7b314bSDavid Xu tdsignal(p, td, SIGTRAP, NULL); 67526f9a767SRodney W. Grimes 67626f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 67726f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 67826f9a767SRodney W. Grimes 6795997cae9SDon Lewis /* 68034ea500bSDon Lewis * Free any previous argument cache and replace it with 6815997cae9SDon Lewis * the new argument cache, if any. 6825997cae9SDon Lewis */ 6839b3b1c5fSJohn Baldwin oldargs = p->p_args; 684e1b1aa3bSJohn Baldwin p->p_args = newargs; 685e1b1aa3bSJohn Baldwin newargs = NULL; 686ebccf1e3SJoseph Koshy 687ebccf1e3SJoseph Koshy #ifdef HWPMC_HOOKS 688ebccf1e3SJoseph Koshy /* 689f263522aSJoseph Koshy * Check if system-wide sampling is in effect or if the 690f263522aSJoseph Koshy * current process is using PMCs. If so, do exec() time 691ebccf1e3SJoseph Koshy * processing. This processing needs to happen AFTER the 692ebccf1e3SJoseph Koshy * P_INEXEC flag is cleared. 693ebccf1e3SJoseph Koshy * 694ebccf1e3SJoseph Koshy * The proc lock needs to be released before taking the PMC 695ebccf1e3SJoseph Koshy * SX. 696ebccf1e3SJoseph Koshy */ 697f263522aSJoseph Koshy if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) { 698e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 69915139246SJoseph Koshy pe.pm_credentialschanged = credential_changing; 70015139246SJoseph Koshy pe.pm_entryaddr = imgp->entry_addr; 70115139246SJoseph Koshy 70215139246SJoseph Koshy PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe); 703ebccf1e3SJoseph Koshy } else 704ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 705ebccf1e3SJoseph Koshy #else /* !HWPMC_HOOKS */ 706ebccf1e3SJoseph Koshy PROC_UNLOCK(p); 707ebccf1e3SJoseph Koshy #endif 708e1b1aa3bSJohn Baldwin 709e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 7103ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 7113ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 7123ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 7133ebc1248SPeter Wemm else 714bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 715bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 716e913ca22SDoug Rabson 7179f5c1d19SDiomidis Spinellis vfs_mark_atime(imgp->vp, td); 7186341095eSKen Smith 71969be5db9SAlfred Perlstein done1: 7209b3b1c5fSJohn Baldwin /* 7219b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 7229b3b1c5fSJohn Baldwin */ 7231419eacbSAlfred Perlstein uifree(euip); 7249b3b1c5fSJohn Baldwin if (newcred == NULL) 7259b3b1c5fSJohn Baldwin crfree(oldcred); 7269b3b1c5fSJohn Baldwin else 7279b3b1c5fSJohn Baldwin crfree(newcred); 72822db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 7299b3b1c5fSJohn Baldwin /* 7309b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 7319b3b1c5fSJohn Baldwin */ 73268ce4375SJeff Roberson if (textvp != NULL) { 73368ce4375SJeff Roberson int tvfslocked; 73468ce4375SJeff Roberson 73568ce4375SJeff Roberson tvfslocked = VFS_LOCK_GIANT(textvp->v_mount); 7369b3b1c5fSJohn Baldwin vrele(textvp); 73768ce4375SJeff Roberson VFS_UNLOCK_GIANT(tvfslocked); 73868ce4375SJeff Roberson } 739619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 740619eb6e5SJeff Roberson vrele(ndp->ni_vp); 7416c84de02SJohn Baldwin #ifdef KTRACE 742ce0be646SJohn Baldwin if (tracevp != NULL) { 743ce0be646SJohn Baldwin int tvfslocked; 744ce0be646SJohn Baldwin 745ce0be646SJohn Baldwin tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount); 7469b3b1c5fSJohn Baldwin vrele(tracevp); 747ce0be646SJohn Baldwin VFS_UNLOCK_GIANT(tvfslocked); 748ce0be646SJohn Baldwin } 749a5881ea5SJohn Baldwin if (tracecred != NULL) 750a5881ea5SJohn Baldwin crfree(tracecred); 7516c84de02SJohn Baldwin #endif 752cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 75369be5db9SAlfred Perlstein if (oldargs != NULL) 7549b3b1c5fSJohn Baldwin pargs_drop(oldargs); 75569be5db9SAlfred Perlstein if (newargs != NULL) 75669be5db9SAlfred Perlstein pargs_drop(newargs); 75790af4afaSJohn Baldwin if (oldsigacts != NULL) 75890af4afaSJohn Baldwin sigacts_free(oldsigacts); 759b9df5231SPoul-Henning Kamp 7601616db3cSJohn Dyson exec_fail_dealloc: 7611616db3cSJohn Dyson 76226f9a767SRodney W. Grimes /* 76326f9a767SRodney W. Grimes * free various allocated resources 76426f9a767SRodney W. Grimes */ 765d6c847f3SBruce Evans if (imgp->firstpage != NULL) 7661616db3cSJohn Dyson exec_unmap_first_page(imgp); 76726f9a767SRodney W. Grimes 768d6c847f3SBruce Evans if (imgp->vp != NULL) { 769762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 770619eb6e5SJeff Roberson vput(imgp->vp); 771a3cf6ebaSDavid Greenman } 77226f9a767SRodney W. Grimes 773d6c847f3SBruce Evans if (imgp->object != NULL) 7740b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 7750b2ed1aeSJeff Roberson 776d1989db5SRobert Drehmel if (error == 0) { 777d1989db5SRobert Drehmel /* 778d1989db5SRobert Drehmel * Stop the process here if its stop event mask has 779d1989db5SRobert Drehmel * the S_EXEC bit set. 780d1989db5SRobert Drehmel */ 781d1989db5SRobert Drehmel STOPEVENT(p, S_EXEC, 0); 782116734c4SMatthew Dillon goto done2; 783d1989db5SRobert Drehmel } 7841616db3cSJohn Dyson 78526f9a767SRodney W. Grimes exec_fail: 7869ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 7879ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 7889ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 7899ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 7909ca45e81SDag-Erling Smørgrav 791116734c4SMatthew Dillon done2: 792670cb89bSRobert Watson #ifdef MAC 793670cb89bSRobert Watson mac_execve_exit(imgp); 794eca8a663SRobert Watson if (interplabel != NULL) 795eca8a663SRobert Watson mac_vnode_label_free(interplabel); 796670cb89bSRobert Watson #endif 797269576c8SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 7988917b8d2SJohn Baldwin exec_free_args(args); 7998917b8d2SJohn Baldwin 8008917b8d2SJohn Baldwin if (error && imgp->vmspace_destroyed) { 8018917b8d2SJohn Baldwin /* sorry, no more process anymore. exit gracefully */ 8028917b8d2SJohn Baldwin exit1(td, W_EXITCODE(0, SIGABRT)); 8038917b8d2SJohn Baldwin /* NOT REACHED */ 8048917b8d2SJohn Baldwin } 805116734c4SMatthew Dillon return (error); 80626f9a767SRodney W. Grimes } 80726f9a767SRodney W. Grimes 8081616db3cSJohn Dyson int 8091616db3cSJohn Dyson exec_map_first_page(imgp) 8101616db3cSJohn Dyson struct image_params *imgp; 8111616db3cSJohn Dyson { 812d8aad40cSJohn Baldwin int rv, i; 81395461b45SJohn Dyson int initial_pagein; 81495461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 8151616db3cSJohn Dyson vm_object_t object; 8161616db3cSJohn Dyson 817d6c847f3SBruce Evans if (imgp->firstpage != NULL) 8181616db3cSJohn Dyson exec_unmap_first_page(imgp); 8191616db3cSJohn Dyson 8208516dd18SPoul-Henning Kamp object = imgp->vp->v_object; 8214d7d2993SJeff Roberson if (object == NULL) 8224d7d2993SJeff Roberson return (EACCES); 823fd0cc9a8SAlan Cox VM_OBJECT_LOCK(object); 824f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 825f8a47341SAlan Cox if ((object->flags & OBJ_COLORED) == 0) { 826f8a47341SAlan Cox object->flags |= OBJ_COLORED; 827f8a47341SAlan Cox object->pg_color = 0; 828f8a47341SAlan Cox } 829f8a47341SAlan Cox #endif 83095461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 83195461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 83295461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 83395461b45SJohn Dyson if (initial_pagein > object->size) 83495461b45SJohn Dyson initial_pagein = object->size; 83595461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 836d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 8376ec2fca5SAlan Cox if (ma[i]->valid) 8386ec2fca5SAlan Cox break; 8399af80719SAlan Cox if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy) 84006fa71cdSAlan Cox break; 841e69763a3SDoug Rabson vm_page_busy(ma[i]); 84295461b45SJohn Dyson } else { 843ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 8447bfda801SAlan Cox VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED); 84595461b45SJohn Dyson if (ma[i] == NULL) 84695461b45SJohn Dyson break; 84795461b45SJohn Dyson } 84895461b45SJohn Dyson } 84995461b45SJohn Dyson initial_pagein = i; 85095461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 85195461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 852ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 853ca0387efSJake Burkholder (ma[0]->valid == 0)) { 854ecbb00a2SDoug Rabson if (ma[0]) { 8556ec2fca5SAlan Cox vm_page_lock_queues(); 8568f9110f6SJohn Dyson vm_page_free(ma[0]); 85706fa71cdSAlan Cox vm_page_unlock_queues(); 8586ec2fca5SAlan Cox } 85906fa71cdSAlan Cox VM_OBJECT_UNLOCK(object); 860a7cddfedSJake Burkholder return (EIO); 8611616db3cSJohn Dyson } 8621616db3cSJohn Dyson } 8636ec2fca5SAlan Cox vm_page_lock_queues(); 864148b3f62SAlan Cox vm_page_hold(ma[0]); 86597646f56SAlan Cox vm_page_unlock_queues(); 8662a53696fSAlan Cox vm_page_wakeup(ma[0]); 8676ec2fca5SAlan Cox VM_OBJECT_UNLOCK(object); 8681616db3cSJohn Dyson 86959c8bc40SAlan Cox imgp->firstpage = sf_buf_alloc(ma[0], 0); 87059c8bc40SAlan Cox imgp->image_header = (char *)sf_buf_kva(imgp->firstpage); 8711616db3cSJohn Dyson 872a7cddfedSJake Burkholder return (0); 8731616db3cSJohn Dyson } 8741616db3cSJohn Dyson 8751616db3cSJohn Dyson void 8761616db3cSJohn Dyson exec_unmap_first_page(imgp) 8771616db3cSJohn Dyson struct image_params *imgp; 8781616db3cSJohn Dyson { 87959c8bc40SAlan Cox vm_page_t m; 88023955314SAlfred Perlstein 881d6c847f3SBruce Evans if (imgp->firstpage != NULL) { 88259c8bc40SAlan Cox m = sf_buf_page(imgp->firstpage); 88359c8bc40SAlan Cox sf_buf_free(imgp->firstpage); 8841616db3cSJohn Dyson imgp->firstpage = NULL; 88559c8bc40SAlan Cox vm_page_lock_queues(); 88659c8bc40SAlan Cox vm_page_unhold(m); 88759c8bc40SAlan Cox vm_page_unlock_queues(); 8881616db3cSJohn Dyson } 8891616db3cSJohn Dyson } 8901616db3cSJohn Dyson 89126f9a767SRodney W. Grimes /* 89226f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 89326f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 89426f9a767SRodney W. Grimes * automatically in trap.c. 89526f9a767SRodney W. Grimes */ 89626f9a767SRodney W. Grimes int 89705ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 898c52007c2SDavid Greenman struct image_params *imgp; 89905ba50f5SJake Burkholder struct sysentvec *sv; 90026f9a767SRodney W. Grimes { 90126f9a767SRodney W. Grimes int error; 9025e20c11fSAlan Cox struct proc *p = imgp->proc; 9035e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 90405ba50f5SJake Burkholder vm_offset_t stack_addr; 90505ba50f5SJake Burkholder vm_map_t map; 90659d8f3ffSJohn Baldwin u_long ssiz; 90726f9a767SRodney W. Grimes 908c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 909993182e5SAlexander Leidinger imgp->sysent = sv; 91026f9a767SRodney W. Grimes 911bd37fd72SKris Kennaway /* May be called with Giant held */ 912993182e5SAlexander Leidinger EVENTHANDLER_INVOKE(process_exec, p, imgp); 9136f5dafeaSAlan Cox 9146f5dafeaSAlan Cox /* 915af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 916af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 917af9ec885SJohn Dyson * not disrupted 918af9ec885SJohn Dyson */ 91905ba50f5SJake Burkholder map = &vmspace->vm_map; 92005ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 92105ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 9223db161e0SMatthew Dillon shmexit(vmspace); 923b9eee07eSPeter Wemm pmap_remove_pages(vmspace_pmap(vmspace)); 92405ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 925af9ec885SJohn Dyson } else { 92689b57fcfSKonstantin Belousov error = vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 92789b57fcfSKonstantin Belousov if (error) 92889b57fcfSKonstantin Belousov return (error); 9295e20c11fSAlan Cox vmspace = p->p_vmspace; 93005ba50f5SJake Burkholder map = &vmspace->vm_map; 931af9ec885SJohn Dyson } 93226f9a767SRodney W. Grimes 93326f9a767SRodney W. Grimes /* Allocate a new stack */ 93459d8f3ffSJohn Baldwin if (sv->sv_maxssiz != NULL) 93559d8f3ffSJohn Baldwin ssiz = *sv->sv_maxssiz; 93659d8f3ffSJohn Baldwin else 93759d8f3ffSJohn Baldwin ssiz = maxssiz; 93859d8f3ffSJohn Baldwin stack_addr = sv->sv_usrstack - ssiz; 93959d8f3ffSJohn Baldwin error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz, 940fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN); 9412267af78SJulian Elischer if (error) 9422267af78SJulian Elischer return (error); 9432267af78SJulian Elischer 94463c47a5cSDoug Rabson #ifdef __ia64__ 945fd75d710SMarcel Moolenaar /* Allocate a new register stack */ 946bab1f052SMarcel Moolenaar stack_addr = IA64_BACKINGSTORE; 94759d8f3ffSJohn Baldwin error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz, 948fd75d710SMarcel Moolenaar sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP); 949fd75d710SMarcel Moolenaar if (error) 950fd75d710SMarcel Moolenaar return (error); 95163c47a5cSDoug Rabson #endif 95263c47a5cSDoug Rabson 9532267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 9542267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 9552267af78SJulian Elischer * process stack so we can check the stack rlimit. 9562267af78SJulian Elischer */ 957cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 95859d8f3ffSJohn Baldwin vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz; 95926f9a767SRodney W. Grimes 96026f9a767SRodney W. Grimes return (0); 96126f9a767SRodney W. Grimes } 96226f9a767SRodney W. Grimes 96326f9a767SRodney W. Grimes /* 964873fbcd7SRobert Watson * Copy out argument and environment strings from the old process address 965873fbcd7SRobert Watson * space into the temporary string buffer. 96626f9a767SRodney W. Grimes */ 96726f9a767SRodney W. Grimes int 968610ecfe0SMaxim Sobolev exec_copyin_args(struct image_args *args, char *fname, 969610ecfe0SMaxim Sobolev enum uio_seg segflg, char **argv, char **envv) 97026f9a767SRodney W. Grimes { 97126f9a767SRodney W. Grimes char *argp, *envp; 972ecbb00a2SDoug Rabson int error; 973ecbb00a2SDoug Rabson size_t length; 97426f9a767SRodney W. Grimes 975610ecfe0SMaxim Sobolev error = 0; 976610ecfe0SMaxim Sobolev 977610ecfe0SMaxim Sobolev bzero(args, sizeof(*args)); 978610ecfe0SMaxim Sobolev if (argv == NULL) 979610ecfe0SMaxim Sobolev return (EFAULT); 98026f9a767SRodney W. Grimes /* 981610ecfe0SMaxim Sobolev * Allocate temporary demand zeroed space for argument and 98290dc539bSMaxim Sobolev * environment strings: 98390dc539bSMaxim Sobolev * 98490dc539bSMaxim Sobolev * o ARG_MAX for argument and environment; 98590dc539bSMaxim Sobolev * o MAXSHELLCMDLEN for the name of interpreters. 98626f9a767SRodney W. Grimes */ 98790dc539bSMaxim Sobolev args->buf = (char *) kmem_alloc_wait(exec_map, 98890dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 989610ecfe0SMaxim Sobolev if (args->buf == NULL) 990610ecfe0SMaxim Sobolev return (ENOMEM); 991610ecfe0SMaxim Sobolev args->begin_argv = args->buf; 992610ecfe0SMaxim Sobolev args->endp = args->begin_argv; 993610ecfe0SMaxim Sobolev args->stringspace = ARG_MAX; 99426f9a767SRodney W. Grimes 995610ecfe0SMaxim Sobolev args->fname = args->buf + ARG_MAX; 99626f9a767SRodney W. Grimes 997610ecfe0SMaxim Sobolev /* 998610ecfe0SMaxim Sobolev * Copy the file name. 999610ecfe0SMaxim Sobolev */ 1000610ecfe0SMaxim Sobolev error = (segflg == UIO_SYSSPACE) ? 1001610ecfe0SMaxim Sobolev copystr(fname, args->fname, PATH_MAX, &length) : 1002610ecfe0SMaxim Sobolev copyinstr(fname, args->fname, PATH_MAX, &length); 1003c30af532SMaxim Sobolev if (error != 0) 100468ff3c24SStephan Uphoff goto err_exit; 100526f9a767SRodney W. Grimes 1006610ecfe0SMaxim Sobolev /* 1007610ecfe0SMaxim Sobolev * extract arguments first 1008610ecfe0SMaxim Sobolev */ 1009610ecfe0SMaxim Sobolev while ((argp = (caddr_t) (intptr_t) fuword(argv++))) { 101068ff3c24SStephan Uphoff if (argp == (caddr_t) -1) { 101168ff3c24SStephan Uphoff error = EFAULT; 101268ff3c24SStephan Uphoff goto err_exit; 101368ff3c24SStephan Uphoff } 1014610ecfe0SMaxim Sobolev if ((error = copyinstr(argp, args->endp, 1015610ecfe0SMaxim Sobolev args->stringspace, &length))) { 1016610ecfe0SMaxim Sobolev if (error == ENAMETOOLONG) 101768ff3c24SStephan Uphoff error = E2BIG; 101868ff3c24SStephan Uphoff goto err_exit; 1019610ecfe0SMaxim Sobolev } 1020610ecfe0SMaxim Sobolev args->stringspace -= length; 1021610ecfe0SMaxim Sobolev args->endp += length; 1022610ecfe0SMaxim Sobolev args->argc++; 1023610ecfe0SMaxim Sobolev } 1024610ecfe0SMaxim Sobolev 1025610ecfe0SMaxim Sobolev args->begin_envv = args->endp; 1026b9df5231SPoul-Henning Kamp 102726f9a767SRodney W. Grimes /* 102826f9a767SRodney W. Grimes * extract environment strings 102926f9a767SRodney W. Grimes */ 10300fd1a014SDavid Greenman if (envv) { 1031aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 103268ff3c24SStephan Uphoff if (envp == (caddr_t)-1) { 103368ff3c24SStephan Uphoff error = EFAULT; 103468ff3c24SStephan Uphoff goto err_exit; 103568ff3c24SStephan Uphoff } 1036610ecfe0SMaxim Sobolev if ((error = copyinstr(envp, args->endp, 1037610ecfe0SMaxim Sobolev args->stringspace, &length))) { 10380fd1a014SDavid Greenman if (error == ENAMETOOLONG) 103968ff3c24SStephan Uphoff error = E2BIG; 104068ff3c24SStephan Uphoff goto err_exit; 10410fd1a014SDavid Greenman } 1042610ecfe0SMaxim Sobolev args->stringspace -= length; 1043610ecfe0SMaxim Sobolev args->endp += length; 1044610ecfe0SMaxim Sobolev args->envc++; 104526f9a767SRodney W. Grimes } 10460fd1a014SDavid Greenman } 104726f9a767SRodney W. Grimes 104826f9a767SRodney W. Grimes return (0); 104968ff3c24SStephan Uphoff 105068ff3c24SStephan Uphoff err_exit: 105168ff3c24SStephan Uphoff exec_free_args(args); 105268ff3c24SStephan Uphoff return (error); 105326f9a767SRodney W. Grimes } 105426f9a767SRodney W. Grimes 10558917b8d2SJohn Baldwin static void 1056610ecfe0SMaxim Sobolev exec_free_args(struct image_args *args) 1057610ecfe0SMaxim Sobolev { 1058610ecfe0SMaxim Sobolev 1059610ecfe0SMaxim Sobolev if (args->buf) { 106090dc539bSMaxim Sobolev kmem_free_wakeup(exec_map, (vm_offset_t)args->buf, 106190dc539bSMaxim Sobolev PATH_MAX + ARG_MAX + MAXSHELLCMDLEN); 1062610ecfe0SMaxim Sobolev args->buf = NULL; 1063610ecfe0SMaxim Sobolev } 1064610ecfe0SMaxim Sobolev } 1065610ecfe0SMaxim Sobolev 106626f9a767SRodney W. Grimes /* 1067873fbcd7SRobert Watson * Copy strings out to the new process address space, constructing new arg 1068873fbcd7SRobert Watson * and env vector tables. Return a pointer to the base so that it can be used 1069873fbcd7SRobert Watson * as the initial stack pointer. 107026f9a767SRodney W. Grimes */ 1071654f6be1SBruce Evans register_t * 1072c52007c2SDavid Greenman exec_copyout_strings(imgp) 1073c52007c2SDavid Greenman struct image_params *imgp; 107426f9a767SRodney W. Grimes { 107526f9a767SRodney W. Grimes int argc, envc; 107626f9a767SRodney W. Grimes char **vectp; 107726f9a767SRodney W. Grimes char *stringp, *destp; 1078654f6be1SBruce Evans register_t *stack_base; 107993f6448cSDavid Greenman struct ps_strings *arginfo; 1080f3bec5d7SJake Burkholder struct proc *p; 1081d66a5066SPeter Wemm int szsigcode; 108226f9a767SRodney W. Grimes 108326f9a767SRodney W. Grimes /* 108426f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 1085d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 108626f9a767SRodney W. Grimes */ 1087f3bec5d7SJake Burkholder p = imgp->proc; 1088f3bec5d7SJake Burkholder szsigcode = 0; 108905ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1090f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 1091f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 1092d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 1093610ecfe0SMaxim Sobolev roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *)); 10941ed012f9SPeter Wemm 109526f9a767SRodney W. Grimes /* 1096d66a5066SPeter Wemm * install sigcode 1097d66a5066SPeter Wemm */ 1098d66a5066SPeter Wemm if (szsigcode) 1099f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1100f3bec5d7SJake Burkholder szsigcode), szsigcode); 1101d66a5066SPeter Wemm 1102d66a5066SPeter Wemm /* 1103e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 1104e1743d02SSøren Schmidt * on the stack. 1105e1743d02SSøren Schmidt */ 1106b9a22da4STakanori Watanabe if (imgp->auxargs) { 1107e1743d02SSøren Schmidt /* 1108b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1109b9a22da4STakanori Watanabe * lower compatibility. 1110b9a22da4STakanori Watanabe */ 1111ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1112ca0387efSJake Burkholder (AT_COUNT * 2); 1113b9a22da4STakanori Watanabe /* 1114b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1115b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 1116b9a22da4STakanori Watanabe * for argument of Runtime loader. 1117e1743d02SSøren Schmidt */ 1118610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + 1119610ecfe0SMaxim Sobolev imgp->args->envc + 2 + imgp->auxarg_size) * 1120610ecfe0SMaxim Sobolev sizeof(char *)); 1121b9a22da4STakanori Watanabe 1122610ecfe0SMaxim Sobolev } else { 1123e1743d02SSøren Schmidt /* 1124b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 1125b9a22da4STakanori Watanabe * the arg and env vector sets 112626f9a767SRodney W. Grimes */ 1127610ecfe0SMaxim Sobolev vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * 1128ca0387efSJake Burkholder sizeof(char *)); 1129610ecfe0SMaxim Sobolev } 113026f9a767SRodney W. Grimes 113126f9a767SRodney W. Grimes /* 113226f9a767SRodney W. Grimes * vectp also becomes our initial stack base 113326f9a767SRodney W. Grimes */ 1134654f6be1SBruce Evans stack_base = (register_t *)vectp; 113526f9a767SRodney W. Grimes 1136610ecfe0SMaxim Sobolev stringp = imgp->args->begin_argv; 1137610ecfe0SMaxim Sobolev argc = imgp->args->argc; 1138610ecfe0SMaxim Sobolev envc = imgp->args->envc; 113926f9a767SRodney W. Grimes 114093f6448cSDavid Greenman /* 11413aed948bSDavid Greenman * Copy out strings - arguments and environment. 114293f6448cSDavid Greenman */ 1143610ecfe0SMaxim Sobolev copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); 114493f6448cSDavid Greenman 114593f6448cSDavid Greenman /* 11463aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 11473aed948bSDavid Greenman */ 1148aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 11493aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 11503aed948bSDavid Greenman 11513aed948bSDavid Greenman /* 11523aed948bSDavid Greenman * Fill in argument portion of vector table. 115393f6448cSDavid Greenman */ 115426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 1155aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11563aed948bSDavid Greenman while (*stringp++ != 0) 11573aed948bSDavid Greenman destp++; 11583aed948bSDavid Greenman destp++; 115926f9a767SRodney W. Grimes } 116026f9a767SRodney W. Grimes 11611a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 11626ab46d52SBruce Evans suword(vectp++, 0); 116326f9a767SRodney W. Grimes 1164aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 11653aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 116693f6448cSDavid Greenman 116793f6448cSDavid Greenman /* 11683aed948bSDavid Greenman * Fill in environment portion of vector table. 116993f6448cSDavid Greenman */ 117026f9a767SRodney W. Grimes for (; envc > 0; --envc) { 1171aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 11723aed948bSDavid Greenman while (*stringp++ != 0) 11733aed948bSDavid Greenman destp++; 11743aed948bSDavid Greenman destp++; 117526f9a767SRodney W. Grimes } 117626f9a767SRodney W. Grimes 117726f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 11786ab46d52SBruce Evans suword(vectp, 0); 117926f9a767SRodney W. Grimes 118026f9a767SRodney W. Grimes return (stack_base); 118126f9a767SRodney W. Grimes } 118226f9a767SRodney W. Grimes 118326f9a767SRodney W. Grimes /* 118426f9a767SRodney W. Grimes * Check permissions of file to execute. 1185cf64863aSRobert Watson * Called with imgp->vp locked. 118626f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 118726f9a767SRodney W. Grimes */ 1188c8a79999SPeter Wemm int 1189c52007c2SDavid Greenman exec_check_permissions(imgp) 1190c52007c2SDavid Greenman struct image_params *imgp; 119126f9a767SRodney W. Grimes { 1192c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1193c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1194a854ed98SJohn Baldwin struct thread *td; 119526f9a767SRodney W. Grimes int error; 119626f9a767SRodney W. Grimes 11976617724cSJeff Roberson td = curthread; 1198339b79b9SRobert Watson 1199ec35c2afSRobert Watson /* Get file attributes */ 1200ec35c2afSRobert Watson error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1201ec35c2afSRobert Watson if (error) 1202ec35c2afSRobert Watson return (error); 1203ec35c2afSRobert Watson 1204339b79b9SRobert Watson #ifdef MAC 120530d239bcSRobert Watson error = mac_vnode_check_exec(td->td_ucred, imgp->vp, imgp); 1206339b79b9SRobert Watson if (error) 1207339b79b9SRobert Watson return (error); 1208339b79b9SRobert Watson #endif 1209339b79b9SRobert Watson 121026f9a767SRodney W. Grimes /* 121126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 121226f9a767SRodney W. Grimes * file resides on. 121326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 121426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 121526f9a767SRodney W. Grimes * file really is executable. 121626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 121726f9a767SRodney W. Grimes */ 1218c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 121926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1220a854ed98SJohn Baldwin (attr->va_type != VREG)) 122126f9a767SRodney W. Grimes return (EACCES); 122226f9a767SRodney W. Grimes 122326f9a767SRodney W. Grimes /* 122426f9a767SRodney W. Grimes * Zero length files can't be exec'd 122526f9a767SRodney W. Grimes */ 122626f9a767SRodney W. Grimes if (attr->va_size == 0) 122726f9a767SRodney W. Grimes return (ENOEXEC); 122826f9a767SRodney W. Grimes 122926f9a767SRodney W. Grimes /* 123026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 123126f9a767SRodney W. Grimes */ 1232a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 123326f9a767SRodney W. Grimes if (error) 123426f9a767SRodney W. Grimes return (error); 123526f9a767SRodney W. Grimes 12366d5a0a8cSDavid Greenman /* 12376d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 12386d5a0a8cSDavid Greenman * if there are any. 12396d5a0a8cSDavid Greenman */ 12406d5a0a8cSDavid Greenman if (vp->v_writecount) 12416d5a0a8cSDavid Greenman return (ETXTBSY); 12426d5a0a8cSDavid Greenman 12436d5a0a8cSDavid Greenman /* 12446d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 12456d5a0a8cSDavid Greenman * general case). 12466d5a0a8cSDavid Greenman */ 12479e223287SKonstantin Belousov error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 124826f9a767SRodney W. Grimes return (error); 1249df8bae1dSRodney W. Grimes } 1250aa855a59SPeter Wemm 1251aa855a59SPeter Wemm /* 1252aa855a59SPeter Wemm * Exec handler registration 1253aa855a59SPeter Wemm */ 1254aa855a59SPeter Wemm int 1255aa855a59SPeter Wemm exec_register(execsw_arg) 1256aa855a59SPeter Wemm const struct execsw *execsw_arg; 1257aa855a59SPeter Wemm { 1258aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1259aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1260aa855a59SPeter Wemm 1261aa855a59SPeter Wemm if (execsw) 1262aa855a59SPeter Wemm for (es = execsw; *es; es++) 1263aa855a59SPeter Wemm count++; 1264a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1265aa855a59SPeter Wemm if (newexecsw == NULL) 1266a7cddfedSJake Burkholder return (ENOMEM); 1267aa855a59SPeter Wemm xs = newexecsw; 1268aa855a59SPeter Wemm if (execsw) 1269aa855a59SPeter Wemm for (es = execsw; *es; es++) 1270aa855a59SPeter Wemm *xs++ = *es; 1271aa855a59SPeter Wemm *xs++ = execsw_arg; 1272aa855a59SPeter Wemm *xs = NULL; 1273aa855a59SPeter Wemm if (execsw) 1274aa855a59SPeter Wemm free(execsw, M_TEMP); 1275aa855a59SPeter Wemm execsw = newexecsw; 1276a7cddfedSJake Burkholder return (0); 1277aa855a59SPeter Wemm } 1278aa855a59SPeter Wemm 1279aa855a59SPeter Wemm int 1280aa855a59SPeter Wemm exec_unregister(execsw_arg) 1281aa855a59SPeter Wemm const struct execsw *execsw_arg; 1282aa855a59SPeter Wemm { 1283aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1284aa855a59SPeter Wemm int count = 1; 1285aa855a59SPeter Wemm 1286aa855a59SPeter Wemm if (execsw == NULL) 1287aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1288aa855a59SPeter Wemm 1289aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1290aa855a59SPeter Wemm if (*es == execsw_arg) 1291aa855a59SPeter Wemm break; 1292aa855a59SPeter Wemm } 1293aa855a59SPeter Wemm if (*es == NULL) 1294a7cddfedSJake Burkholder return (ENOENT); 1295aa855a59SPeter Wemm for (es = execsw; *es; es++) 1296aa855a59SPeter Wemm if (*es != execsw_arg) 1297aa855a59SPeter Wemm count++; 1298a163d034SWarner Losh newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1299aa855a59SPeter Wemm if (newexecsw == NULL) 1300a7cddfedSJake Burkholder return (ENOMEM); 1301aa855a59SPeter Wemm xs = newexecsw; 1302aa855a59SPeter Wemm for (es = execsw; *es; es++) 1303aa855a59SPeter Wemm if (*es != execsw_arg) 1304aa855a59SPeter Wemm *xs++ = *es; 1305aa855a59SPeter Wemm *xs = NULL; 1306aa855a59SPeter Wemm if (execsw) 1307aa855a59SPeter Wemm free(execsw, M_TEMP); 1308aa855a59SPeter Wemm execsw = newexecsw; 1309a7cddfedSJake Burkholder return (0); 1310aa855a59SPeter Wemm } 1311