126f9a767SRodney W. Grimes /* 226f9a767SRodney W. Grimes * Copyright (c) 1993, David Greenman 326f9a767SRodney W. Grimes * All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 1426f9a767SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1726f9a767SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24df8bae1dSRodney W. Grimes * SUCH DAMAGE. 25df8bae1dSRodney W. Grimes * 26c3aac50fSPeter Wemm * $FreeBSD$ 27df8bae1dSRodney W. Grimes */ 28df8bae1dSRodney W. Grimes 291e9b3d91SPeter Wemm #include "opt_ktrace.h" 30339b79b9SRobert Watson #include "opt_mac.h" 311e9b3d91SPeter Wemm 32df8bae1dSRodney W. Grimes #include <sys/param.h> 3326f9a767SRodney W. Grimes #include <sys/systm.h> 34fb919e4dSMark Murray #include <sys/lock.h> 35fb919e4dSMark Murray #include <sys/mutex.h> 36d2d3e875SBruce Evans #include <sys/sysproto.h> 3726f9a767SRodney W. Grimes #include <sys/signalvar.h> 3826f9a767SRodney W. Grimes #include <sys/kernel.h> 39339b79b9SRobert Watson #include <sys/mac.h> 4026f9a767SRodney W. Grimes #include <sys/mount.h> 41797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 42079cc25bSDavid Greenman #include <sys/fcntl.h> 4326f9a767SRodney W. Grimes #include <sys/acct.h> 4426f9a767SRodney W. Grimes #include <sys/exec.h> 4526f9a767SRodney W. Grimes #include <sys/imgact.h> 46e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4726f9a767SRodney W. Grimes #include <sys/wait.h> 48333ea485SGuido van Rooij #include <sys/malloc.h> 49a794e791SBruce Evans #include <sys/proc.h> 502a024a2bSSean Eric Fagan #include <sys/pioctl.h> 51a794e791SBruce Evans #include <sys/namei.h> 521e1e0b44SSøren Schmidt #include <sys/sysent.h> 53797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5499ac3bc8SPeter Wemm #include <sys/sysctl.h> 55333ea485SGuido van Rooij #include <sys/user.h> 56a794e791SBruce Evans #include <sys/vnode.h> 57c781aea8SPeter Wemm #ifdef KTRACE 58c781aea8SPeter Wemm #include <sys/ktrace.h> 59c781aea8SPeter Wemm #endif 6026f9a767SRodney W. Grimes 6126f9a767SRodney W. Grimes #include <vm/vm.h> 62efeaf95aSDavid Greenman #include <vm/vm_param.h> 63efeaf95aSDavid Greenman #include <vm/pmap.h> 641616db3cSJohn Dyson #include <vm/vm_page.h> 65efeaf95aSDavid Greenman #include <vm/vm_map.h> 6626f9a767SRodney W. Grimes #include <vm/vm_kern.h> 67efeaf95aSDavid Greenman #include <vm/vm_extern.h> 681ebd0c59SDavid Greenman #include <vm/vm_object.h> 691616db3cSJohn Dyson #include <vm/vm_pager.h> 7026f9a767SRodney W. Grimes 7126f9a767SRodney W. Grimes #include <machine/reg.h> 7226f9a767SRodney W. Grimes 73b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 74b9df5231SPoul-Henning Kamp 7521d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback"); 7621d56e9cSAlfred Perlstein 7721d56e9cSAlfred Perlstein /* 7821d56e9cSAlfred Perlstein * callout list for things to do at exec time 7921d56e9cSAlfred Perlstein */ 8021d56e9cSAlfred Perlstein struct execlist { 8121d56e9cSAlfred Perlstein execlist_fn function; 8221d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 8321d56e9cSAlfred Perlstein }; 8421d56e9cSAlfred Perlstein 8521d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 8621d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 8721d56e9cSAlfred Perlstein 884d77a549SAlfred Perlstein static register_t *exec_copyout_strings(struct image_params *); 89df8bae1dSRodney W. Grimes 909701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 919701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS; 929701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 93486bddb0SDoug Rabson 949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 959701cd40SJohn Baldwin static u_long usrstack = USRSTACK; 969701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 9799ac3bc8SPeter Wemm 98b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 99c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1009701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 101b9df5231SPoul-Henning Kamp 102a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 103a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 104a8704f89SPoul-Henning Kamp 105911fc923SPeter Wemm #ifdef __ia64__ 106911fc923SPeter Wemm /* XXX HACK */ 107911fc923SPeter Wemm static int regstkpages = 256; 108911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 109911fc923SPeter Wemm #endif 110911fc923SPeter Wemm 11199ac3bc8SPeter Wemm /* 112aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 113aa855a59SPeter Wemm * double pointer here. 114df8bae1dSRodney W. Grimes */ 115aa855a59SPeter Wemm static const struct execsw **execsw; 11626f9a767SRodney W. Grimes 117d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 118ad7507e2SSteven Wallace struct execve_args { 119ad7507e2SSteven Wallace char *fname; 120ad7507e2SSteven Wallace char **argv; 121ad7507e2SSteven Wallace char **envv; 122ad7507e2SSteven Wallace }; 123d2d3e875SBruce Evans #endif 124ad7507e2SSteven Wallace 12526f9a767SRodney W. Grimes /* 12626f9a767SRodney W. Grimes * execve() system call. 127116734c4SMatthew Dillon * 128116734c4SMatthew Dillon * MPSAFE 12926f9a767SRodney W. Grimes */ 13026f9a767SRodney W. Grimes int 131b40ce416SJulian Elischer execve(td, uap) 132b40ce416SJulian Elischer struct thread *td; 13326f9a767SRodney W. Grimes register struct execve_args *uap; 134df8bae1dSRodney W. Grimes { 135b40ce416SJulian Elischer struct proc *p = td->td_proc; 13626f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1379b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1381419eacbSAlfred Perlstein struct uidinfo *euip; 139654f6be1SBruce Evans register_t *stack_base; 140bb56ec4aSPoul-Henning Kamp int error, len, i; 141c52007c2SDavid Greenman struct image_params image_params, *imgp; 14226f9a767SRodney W. Grimes struct vattr attr; 1434d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 14469be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1459b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1466c84de02SJohn Baldwin #ifdef KTRACE 1476c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1486c84de02SJohn Baldwin #endif 1496c84de02SJohn Baldwin struct vnode *textvp = NULL; 150d06c0d4dSRobert Watson int credential_changing; 15126f9a767SRodney W. Grimes 152c52007c2SDavid Greenman imgp = &image_params; 153df8bae1dSRodney W. Grimes 1549ca45e81SDag-Erling Smørgrav /* 1559ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1569ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1579ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1589ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1599ca45e81SDag-Erling Smørgrav * privileges. 1609ca45e81SDag-Erling Smørgrav */ 1619ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1629ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 16391f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 164e602ba25SJulian Elischer if ((p->p_flag & P_KSES) && thread_single(SNGLE_EXIT)) { 165e602ba25SJulian Elischer PROC_UNLOCK(p); 166e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 167e602ba25SJulian Elischer } 168e602ba25SJulian Elischer /* If we get here all other threads are dead. */ 1699ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 1709ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 1719ca45e81SDag-Erling Smørgrav 172df8bae1dSRodney W. Grimes /* 173c52007c2SDavid Greenman * Initialize part of the common data 174df8bae1dSRodney W. Grimes */ 175c52007c2SDavid Greenman imgp->proc = p; 176c52007c2SDavid Greenman imgp->uap = uap; 177c52007c2SDavid Greenman imgp->attr = &attr; 178c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1795cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 180c52007c2SDavid Greenman imgp->entry_addr = 0; 181c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 182c52007c2SDavid Greenman imgp->interpreted = 0; 183c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 184e1743d02SSøren Schmidt imgp->auxargs = NULL; 1851616db3cSJohn Dyson imgp->vp = NULL; 1860b2ed1aeSJeff Roberson imgp->object = NULL; 1871616db3cSJohn Dyson imgp->firstpage = NULL; 1884fe88fe6SJohn Polstra imgp->ps_strings = 0; 189b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 19026f9a767SRodney W. Grimes 19126f9a767SRodney W. Grimes /* 19226f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 19326f9a767SRodney W. Grimes * environment strings 19426f9a767SRodney W. Grimes */ 1951616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 196c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 19726f9a767SRodney W. Grimes error = ENOMEM; 198b3afd20dSAlan Cox mtx_lock(&Giant); 19926f9a767SRodney W. Grimes goto exec_fail; 20026f9a767SRodney W. Grimes } 201c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 202c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2031616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 20426f9a767SRodney W. Grimes 20526f9a767SRodney W. Grimes /* 20626f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 20726f9a767SRodney W. Grimes * in ni_vp amoung other things. 20826f9a767SRodney W. Grimes */ 20926f9a767SRodney W. Grimes ndp = &nd; 210b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 211b40ce416SJulian Elischer UIO_USERSPACE, uap->fname, td); 21226f9a767SRodney W. Grimes 213b3afd20dSAlan Cox mtx_lock(&Giant); 21426f9a767SRodney W. Grimes interpret: 21526f9a767SRodney W. Grimes 21626f9a767SRodney W. Grimes error = namei(ndp); 21726f9a767SRodney W. Grimes if (error) { 2181616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2191616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 22026f9a767SRodney W. Grimes goto exec_fail; 22126f9a767SRodney W. Grimes } 22226f9a767SRodney W. Grimes 223c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 2249c0fed3dSDoug Rabson imgp->fname = uap->fname; 22526f9a767SRodney W. Grimes 22626f9a767SRodney W. Grimes /* 2279022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2289022e4adSDavid Greenman */ 229c52007c2SDavid Greenman error = exec_check_permissions(imgp); 2308677f509SDavid Greenman if (error) { 231b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 23226f9a767SRodney W. Grimes goto exec_fail_dealloc; 2338677f509SDavid Greenman } 2340b2ed1aeSJeff Roberson VOP_GETVOBJECT(imgp->vp, &imgp->object); 2350b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 23626f9a767SRodney W. Grimes 2371616db3cSJohn Dyson error = exec_map_first_page(imgp); 238b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 2396d5a0a8cSDavid Greenman if (error) 24026f9a767SRodney W. Grimes goto exec_fail_dealloc; 24126f9a767SRodney W. Grimes 24226f9a767SRodney W. Grimes /* 243d323ddf3SMatthew Dillon * If the current process has a special image activator it 244d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 245d323ddf3SMatthew Dillon * scripts differently. 24626f9a767SRodney W. Grimes */ 247d323ddf3SMatthew Dillon error = -1; 248d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 249d323ddf3SMatthew Dillon error = img_first(imgp); 250d323ddf3SMatthew Dillon 251d323ddf3SMatthew Dillon /* 252d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 253d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 254d323ddf3SMatthew Dillon * and an error otherwise. 255d323ddf3SMatthew Dillon */ 256d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 257d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 258d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 259d323ddf3SMatthew Dillon continue; 260d323ddf3SMatthew Dillon } 261c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 262d323ddf3SMatthew Dillon } 263d323ddf3SMatthew Dillon 264d323ddf3SMatthew Dillon if (error) { 26526f9a767SRodney W. Grimes if (error == -1) 266d323ddf3SMatthew Dillon error = ENOEXEC; 26726f9a767SRodney W. Grimes goto exec_fail_dealloc; 268d323ddf3SMatthew Dillon } 269d323ddf3SMatthew Dillon 270d323ddf3SMatthew Dillon /* 271d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 272d323ddf3SMatthew Dillon * activate the interpreter. 273d323ddf3SMatthew Dillon */ 274c52007c2SDavid Greenman if (imgp->interpreted) { 2751616db3cSJohn Dyson exec_unmap_first_page(imgp); 276762e6b85SEivind Eklund /* free name buffer and old vnode */ 277762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 278f5277ae7SDavid Greenman vrele(ndp->ni_vp); 2790b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 2800b2ed1aeSJeff Roberson imgp->object = NULL; 28126f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 282b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 283b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 28426f9a767SRodney W. Grimes goto interpret; 28526f9a767SRodney W. Grimes } 28626f9a767SRodney W. Grimes 28726f9a767SRodney W. Grimes /* 28826f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 28926f9a767SRodney W. Grimes */ 2903ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 2913ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 2923ebc1248SPeter Wemm else 293c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 29426f9a767SRodney W. Grimes 29526f9a767SRodney W. Grimes /* 2961e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2971e1e0b44SSøren Schmidt * let it do the stack setup. 2981e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 29926f9a767SRodney W. Grimes */ 3001e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 301c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3021e1e0b44SSøren Schmidt else 303c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 30426f9a767SRodney W. Grimes 305a78e8d2aSDavid Greenman /* 306a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 307a78e8d2aSDavid Greenman * be shared after an exec. 308a78e8d2aSDavid Greenman */ 309426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 310a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 311a78e8d2aSDavid Greenman struct filedesc *tmp; 312a78e8d2aSDavid Greenman 313b40ce416SJulian Elischer tmp = fdcopy(td); 314426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 315b40ce416SJulian Elischer fdfree(td); 316a78e8d2aSDavid Greenman p->p_fd = tmp; 317426da3bcSAlfred Perlstein } else 318426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 319a78e8d2aSDavid Greenman 320333ea485SGuido van Rooij /* 3219b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3229b3b1c5fSJohn Baldwin */ 3239b3b1c5fSJohn Baldwin newcred = crget(); 3241419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3259b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3269b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3279b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3289b3b1c5fSJohn Baldwin 3299b3b1c5fSJohn Baldwin /* close files on exec */ 3309b3b1c5fSJohn Baldwin fdcloseexec(td); 3319b3b1c5fSJohn Baldwin 3329b3b1c5fSJohn Baldwin /* 333333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 3348688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 335b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 336333ea485SGuido van Rooij * reset. 337333ea485SGuido van Rooij */ 3389b3b1c5fSJohn Baldwin PROC_LOCK(p); 3399b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 340333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 3419b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 3429b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 343333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 344333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 3459b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 3469b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 3479b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 3489b3b1c5fSJohn Baldwin PROC_LOCK(p); 349333ea485SGuido van Rooij p->p_procsig = newprocsig; 350b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 351b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 352333ea485SGuido van Rooij 353b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 354b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 355333ea485SGuido van Rooij } 356fdf4e8b3SWarner Losh /* Stop profiling */ 357fdf4e8b3SWarner Losh stopprofclock(p); 358fdf4e8b3SWarner Losh 35926f9a767SRodney W. Grimes /* reset caught signals */ 36026f9a767SRodney W. Grimes execsigs(p); 36126f9a767SRodney W. Grimes 36226f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 36326f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 36426f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 36526f9a767SRodney W. Grimes p->p_comm[len] = 0; 36626f9a767SRodney W. Grimes 36726f9a767SRodney W. Grimes /* 36824b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 369dc733423SDag-Erling Smørgrav * it that it now has its own resources back 37026f9a767SRodney W. Grimes */ 37126f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 37226f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 37326f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 3747f05b035SAlfred Perlstein wakeup(p->p_pptr); 37526f9a767SRodney W. Grimes } 37626f9a767SRodney W. Grimes 37726f9a767SRodney W. Grimes /* 378a78e8d2aSDavid Greenman * Implement image setuid/setgid. 379a78e8d2aSDavid Greenman * 380a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 381a78e8d2aSDavid Greenman * the process is being traced. 382c52007c2SDavid Greenman */ 383b1fc0ec1SRobert Watson oldcred = p->p_ucred; 384d06c0d4dSRobert Watson credential_changing = 0; 385d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 386d06c0d4dSRobert Watson attr.va_uid; 387d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 388d06c0d4dSRobert Watson attr.va_gid; 389d06c0d4dSRobert Watson 390d06c0d4dSRobert Watson if (credential_changing && 391a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 392c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 393c52007c2SDavid Greenman /* 394c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 395b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 396b85db196SPeter Wemm * we do not regain any tracing during a possible block. 39726f9a767SRodney W. Grimes */ 398b85db196SPeter Wemm setsugid(p); 3996c84de02SJohn Baldwin #ifdef KTRACE 40044731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 4016c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 40279deba82SMatthew Dillon p->p_traceflag = 0; 4039b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 4049b3b1c5fSJohn Baldwin p->p_tracep = NULL; 4056c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 40626f9a767SRodney W. Grimes } 4076c84de02SJohn Baldwin #endif 40889ab9307SJacques Vidrine /* Close any file descriptors 0..2 that reference procfs */ 40989ab9307SJacques Vidrine setugidsafety(td); 410e983a376SJacques Vidrine /* Make sure file descriptors 0..2 are in use. */ 411e983a376SJacques Vidrine error = fdcheckstd(td); 41263c9e754SJohn Baldwin if (error != 0) 41369be5db9SAlfred Perlstein goto done1; 414c52007c2SDavid Greenman /* 415c52007c2SDavid Greenman * Set the new credentials. 416c52007c2SDavid Greenman */ 4179b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 418c52007c2SDavid Greenman if (attr.va_mode & VSUID) 4191419eacbSAlfred Perlstein change_euid(newcred, euip); 420c52007c2SDavid Greenman if (attr.va_mode & VSGID) 421b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 4229b3b1c5fSJohn Baldwin /* 4239b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 4249b3b1c5fSJohn Baldwin */ 4259b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 4269b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 4279b3b1c5fSJohn Baldwin p->p_ucred = newcred; 4289b3b1c5fSJohn Baldwin newcred = NULL; 429c52007c2SDavid Greenman } else { 430b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 431b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 432c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 43326f9a767SRodney W. Grimes /* 434c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 435b1fc0ec1SRobert Watson * 436b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 4379b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 4389b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 4399b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 4409b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 4419b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 4429b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 4439b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 44426f9a767SRodney W. Grimes */ 445b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 446b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 4479b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 448b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 449b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 450b1fc0ec1SRobert Watson p->p_ucred = newcred; 4519b3b1c5fSJohn Baldwin newcred = NULL; 4529b3b1c5fSJohn Baldwin } 453b1fc0ec1SRobert Watson } 45426f9a767SRodney W. Grimes 45526f9a767SRodney W. Grimes /* 4562a531c80SDavid Greenman * Store the vp for use in procfs 4572a531c80SDavid Greenman */ 4589b3b1c5fSJohn Baldwin textvp = p->p_textvp; 4592a531c80SDavid Greenman VREF(ndp->ni_vp); 4602a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 4612a531c80SDavid Greenman 4622a531c80SDavid Greenman /* 4639ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 4649ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 465cb679c38SJonathan Lemon */ 466cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 4679ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 468cb679c38SJonathan Lemon 469cb679c38SJonathan Lemon /* 47026f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 47126f9a767SRodney W. Grimes * can be set before the program executes. 47226f9a767SRodney W. Grimes */ 473e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 4742a024a2bSSean Eric Fagan 47526f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 47626f9a767SRodney W. Grimes psignal(p, SIGTRAP); 47726f9a767SRodney W. Grimes 47826f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 47926f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 48026f9a767SRodney W. Grimes 481b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 4829b3b1c5fSJohn Baldwin oldargs = p->p_args; 483b9df5231SPoul-Henning Kamp p->p_args = NULL; 484b9df5231SPoul-Henning Kamp 485e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 4863ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 4873ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 4883ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 4893ebc1248SPeter Wemm else 490e913ca22SDoug Rabson setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 491e913ca22SDoug Rabson imgp->ps_strings); 492e913ca22SDoug Rabson 493b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 494b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 4959b3b1c5fSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 4969b3b1c5fSJohn Baldwin p->p_args = newargs; 4979b3b1c5fSJohn Baldwin newargs = NULL; 498fbd26f75SJohn Baldwin } 49969be5db9SAlfred Perlstein done1: 5009b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 5019b3b1c5fSJohn Baldwin 5029b3b1c5fSJohn Baldwin /* 5039b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5049b3b1c5fSJohn Baldwin */ 5051419eacbSAlfred Perlstein uifree(euip); 5069b3b1c5fSJohn Baldwin if (newcred == NULL) 5079b3b1c5fSJohn Baldwin crfree(oldcred); 5089b3b1c5fSJohn Baldwin else 5099b3b1c5fSJohn Baldwin crfree(newcred); 5109b3b1c5fSJohn Baldwin /* 5119b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 5129b3b1c5fSJohn Baldwin */ 5139b3b1c5fSJohn Baldwin if (textvp != NULL) 5149b3b1c5fSJohn Baldwin vrele(textvp); 5156c84de02SJohn Baldwin #ifdef KTRACE 5169b3b1c5fSJohn Baldwin if (tracevp != NULL) 5179b3b1c5fSJohn Baldwin vrele(tracevp); 5186c84de02SJohn Baldwin #endif 51969be5db9SAlfred Perlstein if (oldargs != NULL) 5209b3b1c5fSJohn Baldwin pargs_drop(oldargs); 52169be5db9SAlfred Perlstein if (newargs != NULL) 52269be5db9SAlfred Perlstein pargs_drop(newargs); 523b9df5231SPoul-Henning Kamp 5241616db3cSJohn Dyson exec_fail_dealloc: 5251616db3cSJohn Dyson 52626f9a767SRodney W. Grimes /* 52726f9a767SRodney W. Grimes * free various allocated resources 52826f9a767SRodney W. Grimes */ 5291616db3cSJohn Dyson if (imgp->firstpage) 5301616db3cSJohn Dyson exec_unmap_first_page(imgp); 53126f9a767SRodney W. Grimes 532c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 5331616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 5341616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 5351616db3cSJohn Dyson 5369c0fed3dSDoug Rabson if (imgp->vp) { 537762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 5389c0fed3dSDoug Rabson vrele(imgp->vp); 539a3cf6ebaSDavid Greenman } 54026f9a767SRodney W. Grimes 5410b2ed1aeSJeff Roberson if (imgp->object) 5420b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 5430b2ed1aeSJeff Roberson 5441616db3cSJohn Dyson if (error == 0) 545116734c4SMatthew Dillon goto done2; 5461616db3cSJohn Dyson 54726f9a767SRodney W. Grimes exec_fail: 5489ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 5499ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 5509ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 5519ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 5529ca45e81SDag-Erling Smørgrav 553c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 55426f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 555b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 55626f9a767SRodney W. Grimes /* NOT REACHED */ 557116734c4SMatthew Dillon error = 0; 55826f9a767SRodney W. Grimes } 559116734c4SMatthew Dillon done2: 560116734c4SMatthew Dillon mtx_unlock(&Giant); 561116734c4SMatthew Dillon return (error); 56226f9a767SRodney W. Grimes } 56326f9a767SRodney W. Grimes 5641616db3cSJohn Dyson int 5651616db3cSJohn Dyson exec_map_first_page(imgp) 5661616db3cSJohn Dyson struct image_params *imgp; 5671616db3cSJohn Dyson { 568d8aad40cSJohn Baldwin int rv, i; 56995461b45SJohn Dyson int initial_pagein; 57095461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 5711616db3cSJohn Dyson vm_object_t object; 5721616db3cSJohn Dyson 5730cddd8f0SMatthew Dillon GIANT_REQUIRED; 5741616db3cSJohn Dyson 5751616db3cSJohn Dyson if (imgp->firstpage) { 5761616db3cSJohn Dyson exec_unmap_first_page(imgp); 5771616db3cSJohn Dyson } 5781616db3cSJohn Dyson 5799ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 5801616db3cSJohn Dyson 58195461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 5821616db3cSJohn Dyson 58395461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 58495461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 58595461b45SJohn Dyson if (initial_pagein > object->size) 58695461b45SJohn Dyson initial_pagein = object->size; 58795461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 588d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 58995461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 59095461b45SJohn Dyson break; 59195461b45SJohn Dyson if (ma[i]->valid) 59295461b45SJohn Dyson break; 593e69763a3SDoug Rabson vm_page_busy(ma[i]); 59495461b45SJohn Dyson } else { 59595461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 59695461b45SJohn Dyson if (ma[i] == NULL) 59795461b45SJohn Dyson break; 59895461b45SJohn Dyson } 59995461b45SJohn Dyson } 60095461b45SJohn Dyson initial_pagein = i; 6011616db3cSJohn Dyson 60295461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 60395461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 60495461b45SJohn Dyson 605eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 606ecbb00a2SDoug Rabson if (ma[0]) { 60797646f56SAlan Cox vm_page_lock_queues(); 60895461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 6098f9110f6SJohn Dyson vm_page_free(ma[0]); 61097646f56SAlan Cox vm_page_unlock_queues(); 611ecbb00a2SDoug Rabson } 6121616db3cSJohn Dyson return EIO; 6131616db3cSJohn Dyson } 6141616db3cSJohn Dyson } 61597646f56SAlan Cox vm_page_lock_queues(); 61695461b45SJohn Dyson vm_page_wire(ma[0]); 617e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 61897646f56SAlan Cox vm_page_unlock_queues(); 6191616db3cSJohn Dyson 620ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 62195461b45SJohn Dyson imgp->firstpage = ma[0]; 6221616db3cSJohn Dyson 6231616db3cSJohn Dyson return 0; 6241616db3cSJohn Dyson } 6251616db3cSJohn Dyson 6261616db3cSJohn Dyson void 6271616db3cSJohn Dyson exec_unmap_first_page(imgp) 6281616db3cSJohn Dyson struct image_params *imgp; 6291616db3cSJohn Dyson { 6300cddd8f0SMatthew Dillon GIANT_REQUIRED; 63123955314SAlfred Perlstein 6321616db3cSJohn Dyson if (imgp->firstpage) { 633ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 63497646f56SAlan Cox vm_page_lock_queues(); 63573007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 63697646f56SAlan Cox vm_page_unlock_queues(); 6371616db3cSJohn Dyson imgp->firstpage = NULL; 6381616db3cSJohn Dyson } 6391616db3cSJohn Dyson } 6401616db3cSJohn Dyson 64126f9a767SRodney W. Grimes /* 64226f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 64326f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 64426f9a767SRodney W. Grimes * automatically in trap.c. 64526f9a767SRodney W. Grimes */ 64626f9a767SRodney W. Grimes int 6473ebc1248SPeter Wemm exec_new_vmspace(imgp, minuser, maxuser, stack_addr) 648c52007c2SDavid Greenman struct image_params *imgp; 6493ebc1248SPeter Wemm vm_offset_t minuser, maxuser, stack_addr; 65026f9a767SRodney W. Grimes { 65126f9a767SRodney W. Grimes int error; 6526f5dafeaSAlan Cox struct execlist *ep; 6535e20c11fSAlan Cox struct proc *p = imgp->proc; 6545e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 65526f9a767SRodney W. Grimes 6560cddd8f0SMatthew Dillon GIANT_REQUIRED; 6570cddd8f0SMatthew Dillon 6583ebc1248SPeter Wemm stack_addr = stack_addr - maxssiz; 6593ebc1248SPeter Wemm 660c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 66126f9a767SRodney W. Grimes 662af9ec885SJohn Dyson /* 6636f5dafeaSAlan Cox * Perform functions registered with at_exec(). 6646f5dafeaSAlan Cox */ 6656f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 6665e20c11fSAlan Cox (*ep->function)(p); 6676f5dafeaSAlan Cox 6686f5dafeaSAlan Cox /* 669af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 670af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 671af9ec885SJohn Dyson * not disrupted 672af9ec885SJohn Dyson */ 6733ebc1248SPeter Wemm if (vmspace->vm_refcnt == 1 6743ebc1248SPeter Wemm && vm_map_min(&vmspace->vm_map) == minuser 6753ebc1248SPeter Wemm && vm_map_max(&vmspace->vm_map) == maxuser) { 6763d903220SDoug Rabson if (vmspace->vm_shm) 6775e20c11fSAlan Cox shmexit(p); 6783ebc1248SPeter Wemm pmap_remove_pages(vmspace_pmap(vmspace), minuser, maxuser); 6793ebc1248SPeter Wemm vm_map_remove(&vmspace->vm_map, minuser, maxuser); 680af9ec885SJohn Dyson } else { 6813ebc1248SPeter Wemm vmspace_exec(p, minuser, maxuser); 6825e20c11fSAlan Cox vmspace = p->p_vmspace; 683af9ec885SJohn Dyson } 68426f9a767SRodney W. Grimes 68526f9a767SRodney W. Grimes /* Allocate a new stack */ 6869a024fc5SRobert Drehmel error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz, 6879a024fc5SRobert Drehmel VM_PROT_ALL, VM_PROT_ALL, 0); 6882267af78SJulian Elischer if (error) 6892267af78SJulian Elischer return (error); 6902267af78SJulian Elischer 69163c47a5cSDoug Rabson #ifdef __ia64__ 69263c47a5cSDoug Rabson { 69363c47a5cSDoug Rabson /* 69463c47a5cSDoug Rabson * Allocate backing store. We really need something 69563c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 69663c47a5cSDoug Rabson * store to grow upwards. This will do for now. 69763c47a5cSDoug Rabson */ 69863c47a5cSDoug Rabson vm_offset_t bsaddr; 699cbc89bfbSPaul Saab bsaddr = USRSTACK - 2*maxssiz; 70063c47a5cSDoug Rabson error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 701911fc923SPeter Wemm regstkpages * PAGE_SIZE, 0, 70263c47a5cSDoug Rabson VM_PROT_ALL, VM_PROT_ALL, 0); 7035e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 70463c47a5cSDoug Rabson } 70563c47a5cSDoug Rabson #endif 70663c47a5cSDoug Rabson 7072267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 7082267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 7092267af78SJulian Elischer * process stack so we can check the stack rlimit. 7102267af78SJulian Elischer */ 711cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 712cbc89bfbSPaul Saab vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz; 71326f9a767SRodney W. Grimes 71426f9a767SRodney W. Grimes return(0); 71526f9a767SRodney W. Grimes } 71626f9a767SRodney W. Grimes 71726f9a767SRodney W. Grimes /* 71826f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 71926f9a767SRodney W. Grimes * address space into the temporary string buffer. 72026f9a767SRodney W. Grimes */ 72126f9a767SRodney W. Grimes int 722c52007c2SDavid Greenman exec_extract_strings(imgp) 723c52007c2SDavid Greenman struct image_params *imgp; 72426f9a767SRodney W. Grimes { 72526f9a767SRodney W. Grimes char **argv, **envv; 72626f9a767SRodney W. Grimes char *argp, *envp; 727ecbb00a2SDoug Rabson int error; 728ecbb00a2SDoug Rabson size_t length; 72926f9a767SRodney W. Grimes 73026f9a767SRodney W. Grimes /* 73126f9a767SRodney W. Grimes * extract arguments first 73226f9a767SRodney W. Grimes */ 73326f9a767SRodney W. Grimes 734c52007c2SDavid Greenman argv = imgp->uap->argv; 73526f9a767SRodney W. Grimes 7360fd1a014SDavid Greenman if (argv) { 737aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 7385cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 7395cf3d12cSAndrey A. Chernov return (EFAULT); 7405cf3d12cSAndrey A. Chernov if (argp) 7415cf3d12cSAndrey A. Chernov argv++; 7425cf3d12cSAndrey A. Chernov if (imgp->argv0) 7435cf3d12cSAndrey A. Chernov argp = imgp->argv0; 7445cf3d12cSAndrey A. Chernov if (argp) { 7455cf3d12cSAndrey A. Chernov do { 74626f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 74726f9a767SRodney W. Grimes return (EFAULT); 748c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 749c52007c2SDavid Greenman imgp->stringspace, &length))) { 7500fd1a014SDavid Greenman if (error == ENAMETOOLONG) 75126f9a767SRodney W. Grimes return(E2BIG); 7520fd1a014SDavid Greenman return (error); 7530fd1a014SDavid Greenman } 754c52007c2SDavid Greenman imgp->stringspace -= length; 755c52007c2SDavid Greenman imgp->stringp += length; 756c52007c2SDavid Greenman imgp->argc++; 757aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 75826f9a767SRodney W. Grimes } 7590fd1a014SDavid Greenman } 76026f9a767SRodney W. Grimes 761b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 762b9df5231SPoul-Henning Kamp 76326f9a767SRodney W. Grimes /* 76426f9a767SRodney W. Grimes * extract environment strings 76526f9a767SRodney W. Grimes */ 76626f9a767SRodney W. Grimes 767c52007c2SDavid Greenman envv = imgp->uap->envv; 76826f9a767SRodney W. Grimes 7690fd1a014SDavid Greenman if (envv) { 770aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 77126f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 77226f9a767SRodney W. Grimes return (EFAULT); 773c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 774c52007c2SDavid Greenman imgp->stringspace, &length))) { 7750fd1a014SDavid Greenman if (error == ENAMETOOLONG) 77626f9a767SRodney W. Grimes return(E2BIG); 7770fd1a014SDavid Greenman return (error); 7780fd1a014SDavid Greenman } 779c52007c2SDavid Greenman imgp->stringspace -= length; 780c52007c2SDavid Greenman imgp->stringp += length; 781c52007c2SDavid Greenman imgp->envc++; 78226f9a767SRodney W. Grimes } 7830fd1a014SDavid Greenman } 78426f9a767SRodney W. Grimes 78526f9a767SRodney W. Grimes return (0); 78626f9a767SRodney W. Grimes } 78726f9a767SRodney W. Grimes 78826f9a767SRodney W. Grimes /* 78926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 79026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 79126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 79226f9a767SRodney W. Grimes */ 793654f6be1SBruce Evans register_t * 794c52007c2SDavid Greenman exec_copyout_strings(imgp) 795c52007c2SDavid Greenman struct image_params *imgp; 79626f9a767SRodney W. Grimes { 79726f9a767SRodney W. Grimes int argc, envc; 79826f9a767SRodney W. Grimes char **vectp; 79926f9a767SRodney W. Grimes char *stringp, *destp; 800654f6be1SBruce Evans register_t *stack_base; 80193f6448cSDavid Greenman struct ps_strings *arginfo; 802d66a5066SPeter Wemm int szsigcode; 80326f9a767SRodney W. Grimes 80426f9a767SRodney W. Grimes /* 80526f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 806d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 80726f9a767SRodney W. Grimes */ 8084c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 809d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 810d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 8111ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 8121ed012f9SPeter Wemm 81326f9a767SRodney W. Grimes /* 814d66a5066SPeter Wemm * install sigcode 815d66a5066SPeter Wemm */ 816d66a5066SPeter Wemm if (szsigcode) 817d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 818d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 819d66a5066SPeter Wemm 820d66a5066SPeter Wemm /* 821e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 822e1743d02SSøren Schmidt * on the stack. 823e1743d02SSøren Schmidt */ 824b9a22da4STakanori Watanabe if (imgp->auxargs) { 825e1743d02SSøren Schmidt /* 826b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 827b9a22da4STakanori Watanabe * lower compatibility. 828b9a22da4STakanori Watanabe */ 829b9a22da4STakanori Watanabe imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 830b9a22da4STakanori Watanabe : (AT_COUNT * 2); 831b9a22da4STakanori Watanabe /* 832b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 833b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 834b9a22da4STakanori Watanabe * for argument of Runtime loader. 835e1743d02SSøren Schmidt */ 836e1743d02SSøren Schmidt vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 837b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 838b9a22da4STakanori Watanabe 839b9a22da4STakanori Watanabe } else 840e1743d02SSøren Schmidt /* 841b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 842b9a22da4STakanori Watanabe * the arg and env vector sets 84326f9a767SRodney W. Grimes */ 844e1743d02SSøren Schmidt vectp = (char **) 845e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 84626f9a767SRodney W. Grimes 84726f9a767SRodney W. Grimes /* 84826f9a767SRodney W. Grimes * vectp also becomes our initial stack base 84926f9a767SRodney W. Grimes */ 850654f6be1SBruce Evans stack_base = (register_t *)vectp; 85126f9a767SRodney W. Grimes 852c52007c2SDavid Greenman stringp = imgp->stringbase; 853c52007c2SDavid Greenman argc = imgp->argc; 854c52007c2SDavid Greenman envc = imgp->envc; 85526f9a767SRodney W. Grimes 85693f6448cSDavid Greenman /* 8573aed948bSDavid Greenman * Copy out strings - arguments and environment. 85893f6448cSDavid Greenman */ 859c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 86093f6448cSDavid Greenman 86193f6448cSDavid Greenman /* 8623aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 8633aed948bSDavid Greenman */ 864aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 8653aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 8663aed948bSDavid Greenman 8673aed948bSDavid Greenman /* 8683aed948bSDavid Greenman * Fill in argument portion of vector table. 86993f6448cSDavid Greenman */ 87026f9a767SRodney W. Grimes for (; argc > 0; --argc) { 871aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 8723aed948bSDavid Greenman while (*stringp++ != 0) 8733aed948bSDavid Greenman destp++; 8743aed948bSDavid Greenman destp++; 87526f9a767SRodney W. Grimes } 87626f9a767SRodney W. Grimes 8771a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 8786ab46d52SBruce Evans suword(vectp++, 0); 87926f9a767SRodney W. Grimes 880aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 8813aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 88293f6448cSDavid Greenman 88393f6448cSDavid Greenman /* 8843aed948bSDavid Greenman * Fill in environment portion of vector table. 88593f6448cSDavid Greenman */ 88626f9a767SRodney W. Grimes for (; envc > 0; --envc) { 887aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 8883aed948bSDavid Greenman while (*stringp++ != 0) 8893aed948bSDavid Greenman destp++; 8903aed948bSDavid Greenman destp++; 89126f9a767SRodney W. Grimes } 89226f9a767SRodney W. Grimes 89326f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 8946ab46d52SBruce Evans suword(vectp, 0); 89526f9a767SRodney W. Grimes 89626f9a767SRodney W. Grimes return (stack_base); 89726f9a767SRodney W. Grimes } 89826f9a767SRodney W. Grimes 89926f9a767SRodney W. Grimes /* 90026f9a767SRodney W. Grimes * Check permissions of file to execute. 901cf64863aSRobert Watson * Called with imgp->vp locked. 90226f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 90326f9a767SRodney W. Grimes */ 904c8a79999SPeter Wemm int 905c52007c2SDavid Greenman exec_check_permissions(imgp) 906c52007c2SDavid Greenman struct image_params *imgp; 90726f9a767SRodney W. Grimes { 908c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 909c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 910a854ed98SJohn Baldwin struct thread *td; 91126f9a767SRodney W. Grimes int error; 91226f9a767SRodney W. Grimes 913a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 914339b79b9SRobert Watson 915339b79b9SRobert Watson #ifdef MAC 916339b79b9SRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp); 917339b79b9SRobert Watson if (error) 918339b79b9SRobert Watson return (error); 919339b79b9SRobert Watson #endif 920339b79b9SRobert Watson 92126f9a767SRodney W. Grimes /* Get file attributes */ 922a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 92326f9a767SRodney W. Grimes if (error) 92426f9a767SRodney W. Grimes return (error); 92526f9a767SRodney W. Grimes 92626f9a767SRodney W. Grimes /* 92726f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 92826f9a767SRodney W. Grimes * file resides on. 92926f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 93026f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 93126f9a767SRodney W. Grimes * file really is executable. 93226f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 93326f9a767SRodney W. Grimes */ 934c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 93526f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 936a854ed98SJohn Baldwin (attr->va_type != VREG)) 93726f9a767SRodney W. Grimes return (EACCES); 93826f9a767SRodney W. Grimes 93926f9a767SRodney W. Grimes /* 94026f9a767SRodney W. Grimes * Zero length files can't be exec'd 94126f9a767SRodney W. Grimes */ 94226f9a767SRodney W. Grimes if (attr->va_size == 0) 94326f9a767SRodney W. Grimes return (ENOEXEC); 94426f9a767SRodney W. Grimes 94526f9a767SRodney W. Grimes /* 94626f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 94726f9a767SRodney W. Grimes */ 948a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 94926f9a767SRodney W. Grimes if (error) 95026f9a767SRodney W. Grimes return (error); 95126f9a767SRodney W. Grimes 9526d5a0a8cSDavid Greenman /* 9536d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 9546d5a0a8cSDavid Greenman * if there are any. 9556d5a0a8cSDavid Greenman */ 9566d5a0a8cSDavid Greenman if (vp->v_writecount) 9576d5a0a8cSDavid Greenman return (ETXTBSY); 9586d5a0a8cSDavid Greenman 9596d5a0a8cSDavid Greenman /* 9606d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 9616d5a0a8cSDavid Greenman * general case). 9626d5a0a8cSDavid Greenman */ 963a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 96426f9a767SRodney W. Grimes return (error); 965df8bae1dSRodney W. Grimes } 966aa855a59SPeter Wemm 967aa855a59SPeter Wemm /* 968aa855a59SPeter Wemm * Exec handler registration 969aa855a59SPeter Wemm */ 970aa855a59SPeter Wemm int 971aa855a59SPeter Wemm exec_register(execsw_arg) 972aa855a59SPeter Wemm const struct execsw *execsw_arg; 973aa855a59SPeter Wemm { 974aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 975aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 976aa855a59SPeter Wemm 977aa855a59SPeter Wemm if (execsw) 978aa855a59SPeter Wemm for (es = execsw; *es; es++) 979aa855a59SPeter Wemm count++; 980aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 981aa855a59SPeter Wemm if (newexecsw == NULL) 982aa855a59SPeter Wemm return ENOMEM; 983aa855a59SPeter Wemm xs = newexecsw; 984aa855a59SPeter Wemm if (execsw) 985aa855a59SPeter Wemm for (es = execsw; *es; es++) 986aa855a59SPeter Wemm *xs++ = *es; 987aa855a59SPeter Wemm *xs++ = execsw_arg; 988aa855a59SPeter Wemm *xs = NULL; 989aa855a59SPeter Wemm if (execsw) 990aa855a59SPeter Wemm free(execsw, M_TEMP); 991aa855a59SPeter Wemm execsw = newexecsw; 992aa855a59SPeter Wemm return 0; 993aa855a59SPeter Wemm } 994aa855a59SPeter Wemm 995aa855a59SPeter Wemm int 996aa855a59SPeter Wemm exec_unregister(execsw_arg) 997aa855a59SPeter Wemm const struct execsw *execsw_arg; 998aa855a59SPeter Wemm { 999aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1000aa855a59SPeter Wemm int count = 1; 1001aa855a59SPeter Wemm 1002aa855a59SPeter Wemm if (execsw == NULL) 1003aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1004aa855a59SPeter Wemm 1005aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1006aa855a59SPeter Wemm if (*es == execsw_arg) 1007aa855a59SPeter Wemm break; 1008aa855a59SPeter Wemm } 1009aa855a59SPeter Wemm if (*es == NULL) 1010aa855a59SPeter Wemm return ENOENT; 1011aa855a59SPeter Wemm for (es = execsw; *es; es++) 1012aa855a59SPeter Wemm if (*es != execsw_arg) 1013aa855a59SPeter Wemm count++; 1014aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1015aa855a59SPeter Wemm if (newexecsw == NULL) 1016aa855a59SPeter Wemm return ENOMEM; 1017aa855a59SPeter Wemm xs = newexecsw; 1018aa855a59SPeter Wemm for (es = execsw; *es; es++) 1019aa855a59SPeter Wemm if (*es != execsw_arg) 1020aa855a59SPeter Wemm *xs++ = *es; 1021aa855a59SPeter Wemm *xs = NULL; 1022aa855a59SPeter Wemm if (execsw) 1023aa855a59SPeter Wemm free(execsw, M_TEMP); 1024aa855a59SPeter Wemm execsw = newexecsw; 1025aa855a59SPeter Wemm return 0; 1026aa855a59SPeter Wemm } 102721d56e9cSAlfred Perlstein 102821d56e9cSAlfred Perlstein int 102921d56e9cSAlfred Perlstein at_exec(function) 103021d56e9cSAlfred Perlstein execlist_fn function; 103121d56e9cSAlfred Perlstein { 103221d56e9cSAlfred Perlstein struct execlist *ep; 103321d56e9cSAlfred Perlstein 103421d56e9cSAlfred Perlstein #ifdef INVARIANTS 103521d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 103621d56e9cSAlfred Perlstein if (rm_at_exec(function)) 103721d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 103821d56e9cSAlfred Perlstein function); 103921d56e9cSAlfred Perlstein #endif 104021d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 104121d56e9cSAlfred Perlstein if (ep == NULL) 104221d56e9cSAlfred Perlstein return (ENOMEM); 104321d56e9cSAlfred Perlstein ep->function = function; 104421d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 104521d56e9cSAlfred Perlstein return (0); 104621d56e9cSAlfred Perlstein } 104721d56e9cSAlfred Perlstein 104821d56e9cSAlfred Perlstein /* 104921d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 105021d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 105121d56e9cSAlfred Perlstein */ 105221d56e9cSAlfred Perlstein int 105321d56e9cSAlfred Perlstein rm_at_exec(function) 105421d56e9cSAlfred Perlstein execlist_fn function; 105521d56e9cSAlfred Perlstein { 105621d56e9cSAlfred Perlstein struct execlist *ep; 105721d56e9cSAlfred Perlstein 105821d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 105921d56e9cSAlfred Perlstein if (ep->function == function) { 106021d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 106121d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 106221d56e9cSAlfred Perlstein return(1); 106321d56e9cSAlfred Perlstein } 106421d56e9cSAlfred Perlstein } 106521d56e9cSAlfred Perlstein return (0); 106621d56e9cSAlfred Perlstein } 106721d56e9cSAlfred Perlstein 1068