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 29df8bae1dSRodney W. Grimes #include <sys/param.h> 3026f9a767SRodney W. Grimes #include <sys/systm.h> 31fb919e4dSMark Murray #include <sys/lock.h> 32fb919e4dSMark Murray #include <sys/mutex.h> 33d2d3e875SBruce Evans #include <sys/sysproto.h> 3426f9a767SRodney W. Grimes #include <sys/signalvar.h> 3526f9a767SRodney W. Grimes #include <sys/kernel.h> 3626f9a767SRodney W. Grimes #include <sys/mount.h> 37797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 38079cc25bSDavid Greenman #include <sys/fcntl.h> 3926f9a767SRodney W. Grimes #include <sys/acct.h> 4026f9a767SRodney W. Grimes #include <sys/exec.h> 4126f9a767SRodney W. Grimes #include <sys/imgact.h> 42e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4326f9a767SRodney W. Grimes #include <sys/wait.h> 44333ea485SGuido van Rooij #include <sys/malloc.h> 45a794e791SBruce Evans #include <sys/proc.h> 462a024a2bSSean Eric Fagan #include <sys/pioctl.h> 47a794e791SBruce Evans #include <sys/namei.h> 481e1e0b44SSøren Schmidt #include <sys/sysent.h> 49797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5099ac3bc8SPeter Wemm #include <sys/sysctl.h> 51333ea485SGuido van Rooij #include <sys/user.h> 52a794e791SBruce Evans #include <sys/vnode.h> 5326f9a767SRodney W. Grimes 5426f9a767SRodney W. Grimes #include <vm/vm.h> 55efeaf95aSDavid Greenman #include <vm/vm_param.h> 56efeaf95aSDavid Greenman #include <vm/pmap.h> 571616db3cSJohn Dyson #include <vm/vm_page.h> 58efeaf95aSDavid Greenman #include <vm/vm_map.h> 5926f9a767SRodney W. Grimes #include <vm/vm_kern.h> 60efeaf95aSDavid Greenman #include <vm/vm_extern.h> 611ebd0c59SDavid Greenman #include <vm/vm_object.h> 621616db3cSJohn Dyson #include <vm/vm_pager.h> 6326f9a767SRodney W. Grimes 6426f9a767SRodney W. Grimes #include <machine/reg.h> 6526f9a767SRodney W. Grimes 66b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 67b9df5231SPoul-Henning Kamp 6821d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback"); 6921d56e9cSAlfred Perlstein 7021d56e9cSAlfred Perlstein /* 7121d56e9cSAlfred Perlstein * callout list for things to do at exec time 7221d56e9cSAlfred Perlstein */ 7321d56e9cSAlfred Perlstein struct execlist { 7421d56e9cSAlfred Perlstein execlist_fn function; 7521d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 7621d56e9cSAlfred Perlstein }; 7721d56e9cSAlfred Perlstein 7821d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 7921d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 8021d56e9cSAlfred Perlstein 814d77a549SAlfred Perlstein static register_t *exec_copyout_strings(struct image_params *); 82df8bae1dSRodney W. Grimes 839701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 849701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS; 859701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 86486bddb0SDoug Rabson 879701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 889701cd40SJohn Baldwin static u_long usrstack = USRSTACK; 899701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 9099ac3bc8SPeter Wemm 91b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 92c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 939701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 94b9df5231SPoul-Henning Kamp 95a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 96a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 97a8704f89SPoul-Henning Kamp 98911fc923SPeter Wemm #ifdef __ia64__ 99911fc923SPeter Wemm /* XXX HACK */ 100911fc923SPeter Wemm static int regstkpages = 256; 101911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 102911fc923SPeter Wemm #endif 103911fc923SPeter Wemm 10499ac3bc8SPeter Wemm /* 105aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 106aa855a59SPeter Wemm * double pointer here. 107df8bae1dSRodney W. Grimes */ 108aa855a59SPeter Wemm static const struct execsw **execsw; 10926f9a767SRodney W. Grimes 110d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 111ad7507e2SSteven Wallace struct execve_args { 112ad7507e2SSteven Wallace char *fname; 113ad7507e2SSteven Wallace char **argv; 114ad7507e2SSteven Wallace char **envv; 115ad7507e2SSteven Wallace }; 116d2d3e875SBruce Evans #endif 117ad7507e2SSteven Wallace 11826f9a767SRodney W. Grimes /* 11926f9a767SRodney W. Grimes * execve() system call. 120116734c4SMatthew Dillon * 121116734c4SMatthew Dillon * MPSAFE 12226f9a767SRodney W. Grimes */ 12326f9a767SRodney W. Grimes int 124b40ce416SJulian Elischer execve(td, uap) 125b40ce416SJulian Elischer struct thread *td; 12626f9a767SRodney W. Grimes register struct execve_args *uap; 127df8bae1dSRodney W. Grimes { 128b40ce416SJulian Elischer struct proc *p = td->td_proc; 12926f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1309b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1311419eacbSAlfred Perlstein struct uidinfo *euip; 132654f6be1SBruce Evans register_t *stack_base; 133bb56ec4aSPoul-Henning Kamp int error, len, i; 134c52007c2SDavid Greenman struct image_params image_params, *imgp; 13526f9a767SRodney W. Grimes struct vattr attr; 1364d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 13769be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1389b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1396c84de02SJohn Baldwin #ifdef KTRACE 1406c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1416c84de02SJohn Baldwin #endif 1426c84de02SJohn Baldwin struct vnode *textvp = NULL; 14326f9a767SRodney W. Grimes 144c52007c2SDavid Greenman imgp = &image_params; 145df8bae1dSRodney W. Grimes 1469ca45e81SDag-Erling Smørgrav /* 1479ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1489ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1499ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1509ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1519ca45e81SDag-Erling Smørgrav * privileges. 1529ca45e81SDag-Erling Smørgrav */ 1539ca45e81SDag-Erling Smørgrav mtx_lock(&Giant); 1549ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1559ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 15691f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 1579ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 1589ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 1599ca45e81SDag-Erling Smørgrav 160b40ce416SJulian Elischer /* XXXKSE */ 161b40ce416SJulian Elischer /* !!!!!!!! we need abort all the other threads of this process before we */ 162b40ce416SJulian Elischer /* proceed beyond his point! */ 163b40ce416SJulian Elischer 164df8bae1dSRodney W. Grimes /* 165c52007c2SDavid Greenman * Initialize part of the common data 166df8bae1dSRodney W. Grimes */ 167c52007c2SDavid Greenman imgp->proc = p; 168c52007c2SDavid Greenman imgp->uap = uap; 169c52007c2SDavid Greenman imgp->attr = &attr; 170c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1715cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 172c52007c2SDavid Greenman imgp->entry_addr = 0; 173c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 174c52007c2SDavid Greenman imgp->interpreted = 0; 175c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 176e1743d02SSøren Schmidt imgp->auxargs = NULL; 1771616db3cSJohn Dyson imgp->vp = NULL; 1781616db3cSJohn Dyson imgp->firstpage = NULL; 1794fe88fe6SJohn Polstra imgp->ps_strings = 0; 180b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 18126f9a767SRodney W. Grimes 18226f9a767SRodney W. Grimes /* 18326f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 18426f9a767SRodney W. Grimes * environment strings 18526f9a767SRodney W. Grimes */ 1861616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 187c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 18826f9a767SRodney W. Grimes error = ENOMEM; 18926f9a767SRodney W. Grimes goto exec_fail; 19026f9a767SRodney W. Grimes } 191c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 192c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1931616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 19426f9a767SRodney W. Grimes 19526f9a767SRodney W. Grimes /* 19626f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 19726f9a767SRodney W. Grimes * in ni_vp amoung other things. 19826f9a767SRodney W. Grimes */ 19926f9a767SRodney W. Grimes ndp = &nd; 200b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 201b40ce416SJulian Elischer UIO_USERSPACE, uap->fname, td); 20226f9a767SRodney W. Grimes 20326f9a767SRodney W. Grimes interpret: 20426f9a767SRodney W. Grimes 20526f9a767SRodney W. Grimes error = namei(ndp); 20626f9a767SRodney W. Grimes if (error) { 2071616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2081616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 20926f9a767SRodney W. Grimes goto exec_fail; 21026f9a767SRodney W. Grimes } 21126f9a767SRodney W. Grimes 212c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 2139c0fed3dSDoug Rabson imgp->fname = uap->fname; 21426f9a767SRodney W. Grimes 21526f9a767SRodney W. Grimes /* 2169022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2179022e4adSDavid Greenman */ 218c52007c2SDavid Greenman error = exec_check_permissions(imgp); 2198677f509SDavid Greenman if (error) { 220b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 22126f9a767SRodney W. Grimes goto exec_fail_dealloc; 2228677f509SDavid Greenman } 22326f9a767SRodney W. Grimes 2241616db3cSJohn Dyson error = exec_map_first_page(imgp); 225b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 2266d5a0a8cSDavid Greenman if (error) 22726f9a767SRodney W. Grimes goto exec_fail_dealloc; 22826f9a767SRodney W. Grimes 22926f9a767SRodney W. Grimes /* 230d323ddf3SMatthew Dillon * If the current process has a special image activator it 231d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 232d323ddf3SMatthew Dillon * scripts differently. 23326f9a767SRodney W. Grimes */ 234d323ddf3SMatthew Dillon error = -1; 235d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 236d323ddf3SMatthew Dillon error = img_first(imgp); 237d323ddf3SMatthew Dillon 238d323ddf3SMatthew Dillon /* 239d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 240d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 241d323ddf3SMatthew Dillon * and an error otherwise. 242d323ddf3SMatthew Dillon */ 243d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 244d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 245d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 246d323ddf3SMatthew Dillon continue; 247d323ddf3SMatthew Dillon } 248c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 249d323ddf3SMatthew Dillon } 250d323ddf3SMatthew Dillon 251d323ddf3SMatthew Dillon if (error) { 25226f9a767SRodney W. Grimes if (error == -1) 253d323ddf3SMatthew Dillon error = ENOEXEC; 25426f9a767SRodney W. Grimes goto exec_fail_dealloc; 255d323ddf3SMatthew Dillon } 256d323ddf3SMatthew Dillon 257d323ddf3SMatthew Dillon /* 258d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 259d323ddf3SMatthew Dillon * activate the interpreter. 260d323ddf3SMatthew Dillon */ 261c52007c2SDavid Greenman if (imgp->interpreted) { 2621616db3cSJohn Dyson exec_unmap_first_page(imgp); 263762e6b85SEivind Eklund /* free name buffer and old vnode */ 264762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 265f5277ae7SDavid Greenman vrele(ndp->ni_vp); 26626f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 267b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 268b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 26926f9a767SRodney W. Grimes goto interpret; 27026f9a767SRodney W. Grimes } 27126f9a767SRodney W. Grimes 27226f9a767SRodney W. Grimes /* 27326f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 27426f9a767SRodney W. Grimes */ 275c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 27626f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 27726f9a767SRodney W. Grimes 27826f9a767SRodney W. Grimes /* 2791e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2801e1e0b44SSøren Schmidt * let it do the stack setup. 2811e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 28226f9a767SRodney W. Grimes */ 2831e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 284c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2851e1e0b44SSøren Schmidt else 286c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 28726f9a767SRodney W. Grimes 288a78e8d2aSDavid Greenman /* 289a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 290a78e8d2aSDavid Greenman * be shared after an exec. 291a78e8d2aSDavid Greenman */ 292426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 293a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 294a78e8d2aSDavid Greenman struct filedesc *tmp; 295a78e8d2aSDavid Greenman 296b40ce416SJulian Elischer tmp = fdcopy(td); 297426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 298b40ce416SJulian Elischer fdfree(td); 299a78e8d2aSDavid Greenman p->p_fd = tmp; 300426da3bcSAlfred Perlstein } else 301426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 302a78e8d2aSDavid Greenman 303333ea485SGuido van Rooij /* 3049b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3059b3b1c5fSJohn Baldwin */ 3069b3b1c5fSJohn Baldwin newcred = crget(); 3071419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3089b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3099b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3109b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3119b3b1c5fSJohn Baldwin 3129b3b1c5fSJohn Baldwin /* close files on exec */ 3139b3b1c5fSJohn Baldwin fdcloseexec(td); 3149b3b1c5fSJohn Baldwin 3159b3b1c5fSJohn Baldwin /* 316333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 3178688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 318b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 319333ea485SGuido van Rooij * reset. 320333ea485SGuido van Rooij */ 3219b3b1c5fSJohn Baldwin PROC_LOCK(p); 3229b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 323333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 3249b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 3259b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 326333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 327333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 3289b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 3299b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 3309b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 3319b3b1c5fSJohn Baldwin PROC_LOCK(p); 332333ea485SGuido van Rooij p->p_procsig = newprocsig; 333b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 334b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 335333ea485SGuido van Rooij 336b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 337b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 338333ea485SGuido van Rooij } 339fdf4e8b3SWarner Losh /* Stop profiling */ 340fdf4e8b3SWarner Losh stopprofclock(p); 341fdf4e8b3SWarner Losh 34226f9a767SRodney W. Grimes /* reset caught signals */ 34326f9a767SRodney W. Grimes execsigs(p); 34426f9a767SRodney W. Grimes 34526f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 34626f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 34726f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 34826f9a767SRodney W. Grimes p->p_comm[len] = 0; 34926f9a767SRodney W. Grimes 35026f9a767SRodney W. Grimes /* 35124b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 352dc733423SDag-Erling Smørgrav * it that it now has its own resources back 35326f9a767SRodney W. Grimes */ 35426f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 35526f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 35626f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 35726f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 35826f9a767SRodney W. Grimes } 35926f9a767SRodney W. Grimes 36026f9a767SRodney W. Grimes /* 361a78e8d2aSDavid Greenman * Implement image setuid/setgid. 362a78e8d2aSDavid Greenman * 363a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 364a78e8d2aSDavid Greenman * the process is being traced. 365c52007c2SDavid Greenman */ 366b1fc0ec1SRobert Watson oldcred = p->p_ucred; 367b1fc0ec1SRobert Watson if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) || 368b1fc0ec1SRobert Watson ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) && 369a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 370c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 371c52007c2SDavid Greenman /* 372c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 373b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 374b85db196SPeter Wemm * we do not regain any tracing during a possible block. 37526f9a767SRodney W. Grimes */ 376b85db196SPeter Wemm setsugid(p); 3776c84de02SJohn Baldwin #ifdef KTRACE 37844731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 3796c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 38079deba82SMatthew Dillon p->p_traceflag = 0; 3819b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 3829b3b1c5fSJohn Baldwin p->p_tracep = NULL; 3836c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 38426f9a767SRodney W. Grimes } 3856c84de02SJohn Baldwin #endif 386e983a376SJacques Vidrine /* Make sure file descriptors 0..2 are in use. */ 387e983a376SJacques Vidrine error = fdcheckstd(td); 38869be5db9SAlfred Perlstein if (error != 0) { 38969be5db9SAlfred Perlstein oldcred = NULL; 39069be5db9SAlfred Perlstein goto done1; 39169be5db9SAlfred Perlstein } 392c52007c2SDavid Greenman /* 393c52007c2SDavid Greenman * Set the new credentials. 394c52007c2SDavid Greenman */ 3959b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 396c52007c2SDavid Greenman if (attr.va_mode & VSUID) 3971419eacbSAlfred Perlstein change_euid(newcred, euip); 398c52007c2SDavid Greenman if (attr.va_mode & VSGID) 399b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 400b40ce416SJulian Elischer setugidsafety(td); 4019b3b1c5fSJohn Baldwin /* 4029b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 4039b3b1c5fSJohn Baldwin */ 4049b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 4059b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 4069b3b1c5fSJohn Baldwin p->p_ucred = newcred; 4079b3b1c5fSJohn Baldwin newcred = NULL; 408c52007c2SDavid Greenman } else { 409b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 410b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 411c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 41226f9a767SRodney W. Grimes /* 413c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 414b1fc0ec1SRobert Watson * 415b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 4169b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 4179b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 4189b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 4199b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 4209b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 4219b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 4229b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 42326f9a767SRodney W. Grimes */ 424b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 425b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 4269b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 427b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 428b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 429b1fc0ec1SRobert Watson p->p_ucred = newcred; 4309b3b1c5fSJohn Baldwin newcred = NULL; 4319b3b1c5fSJohn Baldwin } 432b1fc0ec1SRobert Watson } 43326f9a767SRodney W. Grimes 43426f9a767SRodney W. Grimes /* 4352a531c80SDavid Greenman * Store the vp for use in procfs 4362a531c80SDavid Greenman */ 4379b3b1c5fSJohn Baldwin textvp = p->p_textvp; 4382a531c80SDavid Greenman VREF(ndp->ni_vp); 4392a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 4402a531c80SDavid Greenman 4412a531c80SDavid Greenman /* 4429ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 4439ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 444cb679c38SJonathan Lemon */ 445cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 4469ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 447cb679c38SJonathan Lemon 448cb679c38SJonathan Lemon /* 44926f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 45026f9a767SRodney W. Grimes * can be set before the program executes. 45126f9a767SRodney W. Grimes */ 452e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 4532a024a2bSSean Eric Fagan 45426f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 45526f9a767SRodney W. Grimes psignal(p, SIGTRAP); 45626f9a767SRodney W. Grimes 45726f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 45826f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 45926f9a767SRodney W. Grimes 460b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 4619b3b1c5fSJohn Baldwin oldargs = p->p_args; 462b9df5231SPoul-Henning Kamp p->p_args = NULL; 463b9df5231SPoul-Henning Kamp 464e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 465e913ca22SDoug Rabson setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 466e913ca22SDoug Rabson imgp->ps_strings); 467e913ca22SDoug Rabson 468b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 469b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 4709b3b1c5fSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 4719b3b1c5fSJohn Baldwin p->p_args = newargs; 4729b3b1c5fSJohn Baldwin newargs = NULL; 473fbd26f75SJohn Baldwin } 47469be5db9SAlfred Perlstein done1: 4759b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 4769b3b1c5fSJohn Baldwin 4779b3b1c5fSJohn Baldwin /* 4789b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 4799b3b1c5fSJohn Baldwin */ 4801419eacbSAlfred Perlstein uifree(euip); 4819b3b1c5fSJohn Baldwin if (newcred == NULL) 4829b3b1c5fSJohn Baldwin crfree(oldcred); 4839b3b1c5fSJohn Baldwin else 4849b3b1c5fSJohn Baldwin crfree(newcred); 4859b3b1c5fSJohn Baldwin /* 4869b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 4879b3b1c5fSJohn Baldwin */ 4889b3b1c5fSJohn Baldwin if (textvp != NULL) 4899b3b1c5fSJohn Baldwin vrele(textvp); 4906c84de02SJohn Baldwin #ifdef KTRACE 4919b3b1c5fSJohn Baldwin if (tracevp != NULL) 4929b3b1c5fSJohn Baldwin vrele(tracevp); 4936c84de02SJohn Baldwin #endif 49469be5db9SAlfred Perlstein if (oldargs != NULL) 4959b3b1c5fSJohn Baldwin pargs_drop(oldargs); 49669be5db9SAlfred Perlstein if (newargs != NULL) 49769be5db9SAlfred Perlstein pargs_drop(newargs); 498b9df5231SPoul-Henning Kamp 4991616db3cSJohn Dyson exec_fail_dealloc: 5001616db3cSJohn Dyson 50126f9a767SRodney W. Grimes /* 50226f9a767SRodney W. Grimes * free various allocated resources 50326f9a767SRodney W. Grimes */ 5041616db3cSJohn Dyson if (imgp->firstpage) 5051616db3cSJohn Dyson exec_unmap_first_page(imgp); 50626f9a767SRodney W. Grimes 507c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 5081616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 5091616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 5101616db3cSJohn Dyson 5119c0fed3dSDoug Rabson if (imgp->vp) { 512762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 5139c0fed3dSDoug Rabson vrele(imgp->vp); 514a3cf6ebaSDavid Greenman } 51526f9a767SRodney W. Grimes 5161616db3cSJohn Dyson if (error == 0) 517116734c4SMatthew Dillon goto done2; 5181616db3cSJohn Dyson 51926f9a767SRodney W. Grimes exec_fail: 5209ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 5219ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 5229ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 5239ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 5249ca45e81SDag-Erling Smørgrav 525c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 52626f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 527b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 52826f9a767SRodney W. Grimes /* NOT REACHED */ 529116734c4SMatthew Dillon error = 0; 53026f9a767SRodney W. Grimes } 531116734c4SMatthew Dillon done2: 532116734c4SMatthew Dillon mtx_unlock(&Giant); 533116734c4SMatthew Dillon return (error); 53426f9a767SRodney W. Grimes } 53526f9a767SRodney W. Grimes 5361616db3cSJohn Dyson int 5371616db3cSJohn Dyson exec_map_first_page(imgp) 5381616db3cSJohn Dyson struct image_params *imgp; 5391616db3cSJohn Dyson { 540d8aad40cSJohn Baldwin int rv, i; 54195461b45SJohn Dyson int initial_pagein; 54295461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 5431616db3cSJohn Dyson vm_object_t object; 5441616db3cSJohn Dyson 5450cddd8f0SMatthew Dillon GIANT_REQUIRED; 5461616db3cSJohn Dyson 5471616db3cSJohn Dyson if (imgp->firstpage) { 5481616db3cSJohn Dyson exec_unmap_first_page(imgp); 5491616db3cSJohn Dyson } 5501616db3cSJohn Dyson 5519ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 5521616db3cSJohn Dyson 55395461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 5541616db3cSJohn Dyson 55595461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 55695461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 55795461b45SJohn Dyson if (initial_pagein > object->size) 55895461b45SJohn Dyson initial_pagein = object->size; 55995461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 560d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 56195461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 56295461b45SJohn Dyson break; 56395461b45SJohn Dyson if (ma[i]->valid) 56495461b45SJohn Dyson break; 565e69763a3SDoug Rabson vm_page_busy(ma[i]); 56695461b45SJohn Dyson } else { 56795461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 56895461b45SJohn Dyson if (ma[i] == NULL) 56995461b45SJohn Dyson break; 57095461b45SJohn Dyson } 57195461b45SJohn Dyson } 57295461b45SJohn Dyson initial_pagein = i; 5731616db3cSJohn Dyson 57495461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 57595461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 57695461b45SJohn Dyson 577eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 578ecbb00a2SDoug Rabson if (ma[0]) { 57995461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 5808f9110f6SJohn Dyson vm_page_free(ma[0]); 581ecbb00a2SDoug Rabson } 5821616db3cSJohn Dyson return EIO; 5831616db3cSJohn Dyson } 5841616db3cSJohn Dyson } 5851616db3cSJohn Dyson 58695461b45SJohn Dyson vm_page_wire(ma[0]); 587e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 5881616db3cSJohn Dyson 589ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 59095461b45SJohn Dyson imgp->firstpage = ma[0]; 5911616db3cSJohn Dyson 5921616db3cSJohn Dyson return 0; 5931616db3cSJohn Dyson } 5941616db3cSJohn Dyson 5951616db3cSJohn Dyson void 5961616db3cSJohn Dyson exec_unmap_first_page(imgp) 5971616db3cSJohn Dyson struct image_params *imgp; 5981616db3cSJohn Dyson { 5990cddd8f0SMatthew Dillon GIANT_REQUIRED; 60023955314SAlfred Perlstein 6011616db3cSJohn Dyson if (imgp->firstpage) { 602ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 60373007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 6041616db3cSJohn Dyson imgp->firstpage = NULL; 6051616db3cSJohn Dyson } 6061616db3cSJohn Dyson } 6071616db3cSJohn Dyson 60826f9a767SRodney W. Grimes /* 60926f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 61026f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 61126f9a767SRodney W. Grimes * automatically in trap.c. 61226f9a767SRodney W. Grimes */ 61326f9a767SRodney W. Grimes int 614c52007c2SDavid Greenman exec_new_vmspace(imgp) 615c52007c2SDavid Greenman struct image_params *imgp; 61626f9a767SRodney W. Grimes { 61726f9a767SRodney W. Grimes int error; 6186f5dafeaSAlan Cox struct execlist *ep; 6195e20c11fSAlan Cox struct proc *p = imgp->proc; 6205e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 6219a024fc5SRobert Drehmel vm_offset_t stack_addr = USRSTACK - maxssiz; 62226f9a767SRodney W. Grimes 6230cddd8f0SMatthew Dillon GIANT_REQUIRED; 6240cddd8f0SMatthew Dillon 625c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 62626f9a767SRodney W. Grimes 627af9ec885SJohn Dyson /* 6286f5dafeaSAlan Cox * Perform functions registered with at_exec(). 6296f5dafeaSAlan Cox */ 6306f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 6315e20c11fSAlan Cox (*ep->function)(p); 6326f5dafeaSAlan Cox 6336f5dafeaSAlan Cox /* 634af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 635af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 636af9ec885SJohn Dyson * not disrupted 637af9ec885SJohn Dyson */ 638af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 6393d903220SDoug Rabson if (vmspace->vm_shm) 6405e20c11fSAlan Cox shmexit(p); 641b1028ad1SLuoqi Chen pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 642cb100b25SAlan Cox vm_map_remove(&vmspace->vm_map, 0, VM_MAXUSER_ADDRESS); 643af9ec885SJohn Dyson } else { 6445e20c11fSAlan Cox vmspace_exec(p); 6455e20c11fSAlan Cox vmspace = p->p_vmspace; 646af9ec885SJohn Dyson } 64726f9a767SRodney W. Grimes 64826f9a767SRodney W. Grimes /* Allocate a new stack */ 6499a024fc5SRobert Drehmel error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz, 6509a024fc5SRobert Drehmel VM_PROT_ALL, VM_PROT_ALL, 0); 6512267af78SJulian Elischer if (error) 6522267af78SJulian Elischer return (error); 6532267af78SJulian Elischer 65463c47a5cSDoug Rabson #ifdef __ia64__ 65563c47a5cSDoug Rabson { 65663c47a5cSDoug Rabson /* 65763c47a5cSDoug Rabson * Allocate backing store. We really need something 65863c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 65963c47a5cSDoug Rabson * store to grow upwards. This will do for now. 66063c47a5cSDoug Rabson */ 66163c47a5cSDoug Rabson vm_offset_t bsaddr; 662cbc89bfbSPaul Saab bsaddr = USRSTACK - 2*maxssiz; 66363c47a5cSDoug Rabson error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 664911fc923SPeter Wemm regstkpages * PAGE_SIZE, 0, 66563c47a5cSDoug Rabson VM_PROT_ALL, VM_PROT_ALL, 0); 6665e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 66763c47a5cSDoug Rabson } 66863c47a5cSDoug Rabson #endif 66963c47a5cSDoug Rabson 6702267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 6712267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 6722267af78SJulian Elischer * process stack so we can check the stack rlimit. 6732267af78SJulian Elischer */ 674cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 675cbc89bfbSPaul Saab vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz; 67626f9a767SRodney W. Grimes 67726f9a767SRodney W. Grimes return(0); 67826f9a767SRodney W. Grimes } 67926f9a767SRodney W. Grimes 68026f9a767SRodney W. Grimes /* 68126f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 68226f9a767SRodney W. Grimes * address space into the temporary string buffer. 68326f9a767SRodney W. Grimes */ 68426f9a767SRodney W. Grimes int 685c52007c2SDavid Greenman exec_extract_strings(imgp) 686c52007c2SDavid Greenman struct image_params *imgp; 68726f9a767SRodney W. Grimes { 68826f9a767SRodney W. Grimes char **argv, **envv; 68926f9a767SRodney W. Grimes char *argp, *envp; 690ecbb00a2SDoug Rabson int error; 691ecbb00a2SDoug Rabson size_t length; 69226f9a767SRodney W. Grimes 69326f9a767SRodney W. Grimes /* 69426f9a767SRodney W. Grimes * extract arguments first 69526f9a767SRodney W. Grimes */ 69626f9a767SRodney W. Grimes 697c52007c2SDavid Greenman argv = imgp->uap->argv; 69826f9a767SRodney W. Grimes 6990fd1a014SDavid Greenman if (argv) { 700aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 7015cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 7025cf3d12cSAndrey A. Chernov return (EFAULT); 7035cf3d12cSAndrey A. Chernov if (argp) 7045cf3d12cSAndrey A. Chernov argv++; 7055cf3d12cSAndrey A. Chernov if (imgp->argv0) 7065cf3d12cSAndrey A. Chernov argp = imgp->argv0; 7075cf3d12cSAndrey A. Chernov if (argp) { 7085cf3d12cSAndrey A. Chernov do { 70926f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 71026f9a767SRodney W. Grimes return (EFAULT); 711c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 712c52007c2SDavid Greenman imgp->stringspace, &length))) { 7130fd1a014SDavid Greenman if (error == ENAMETOOLONG) 71426f9a767SRodney W. Grimes return(E2BIG); 7150fd1a014SDavid Greenman return (error); 7160fd1a014SDavid Greenman } 717c52007c2SDavid Greenman imgp->stringspace -= length; 718c52007c2SDavid Greenman imgp->stringp += length; 719c52007c2SDavid Greenman imgp->argc++; 720aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 72126f9a767SRodney W. Grimes } 7220fd1a014SDavid Greenman } 72326f9a767SRodney W. Grimes 724b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 725b9df5231SPoul-Henning Kamp 72626f9a767SRodney W. Grimes /* 72726f9a767SRodney W. Grimes * extract environment strings 72826f9a767SRodney W. Grimes */ 72926f9a767SRodney W. Grimes 730c52007c2SDavid Greenman envv = imgp->uap->envv; 73126f9a767SRodney W. Grimes 7320fd1a014SDavid Greenman if (envv) { 733aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 73426f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 73526f9a767SRodney W. Grimes return (EFAULT); 736c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 737c52007c2SDavid Greenman imgp->stringspace, &length))) { 7380fd1a014SDavid Greenman if (error == ENAMETOOLONG) 73926f9a767SRodney W. Grimes return(E2BIG); 7400fd1a014SDavid Greenman return (error); 7410fd1a014SDavid Greenman } 742c52007c2SDavid Greenman imgp->stringspace -= length; 743c52007c2SDavid Greenman imgp->stringp += length; 744c52007c2SDavid Greenman imgp->envc++; 74526f9a767SRodney W. Grimes } 7460fd1a014SDavid Greenman } 74726f9a767SRodney W. Grimes 74826f9a767SRodney W. Grimes return (0); 74926f9a767SRodney W. Grimes } 75026f9a767SRodney W. Grimes 75126f9a767SRodney W. Grimes /* 75226f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 75326f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 75426f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 75526f9a767SRodney W. Grimes */ 756654f6be1SBruce Evans register_t * 757c52007c2SDavid Greenman exec_copyout_strings(imgp) 758c52007c2SDavid Greenman struct image_params *imgp; 75926f9a767SRodney W. Grimes { 76026f9a767SRodney W. Grimes int argc, envc; 76126f9a767SRodney W. Grimes char **vectp; 76226f9a767SRodney W. Grimes char *stringp, *destp; 763654f6be1SBruce Evans register_t *stack_base; 76493f6448cSDavid Greenman struct ps_strings *arginfo; 765d66a5066SPeter Wemm int szsigcode; 76626f9a767SRodney W. Grimes 76726f9a767SRodney W. Grimes /* 76826f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 769d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 77026f9a767SRodney W. Grimes */ 7714c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 772d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 773d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 7741ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 7751ed012f9SPeter Wemm 77626f9a767SRodney W. Grimes /* 777d66a5066SPeter Wemm * install sigcode 778d66a5066SPeter Wemm */ 779d66a5066SPeter Wemm if (szsigcode) 780d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 781d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 782d66a5066SPeter Wemm 783d66a5066SPeter Wemm /* 784e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 785e1743d02SSøren Schmidt * on the stack. 786e1743d02SSøren Schmidt */ 787b9a22da4STakanori Watanabe if (imgp->auxargs) { 788e1743d02SSøren Schmidt /* 789b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 790b9a22da4STakanori Watanabe * lower compatibility. 791b9a22da4STakanori Watanabe */ 792b9a22da4STakanori Watanabe imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 793b9a22da4STakanori Watanabe : (AT_COUNT * 2); 794b9a22da4STakanori Watanabe /* 795b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 796b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 797b9a22da4STakanori Watanabe * for argument of Runtime loader. 798e1743d02SSøren Schmidt */ 799e1743d02SSøren Schmidt vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 800b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 801b9a22da4STakanori Watanabe 802b9a22da4STakanori Watanabe } else 803e1743d02SSøren Schmidt /* 804b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 805b9a22da4STakanori Watanabe * the arg and env vector sets 80626f9a767SRodney W. Grimes */ 807e1743d02SSøren Schmidt vectp = (char **) 808e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 80926f9a767SRodney W. Grimes 81026f9a767SRodney W. Grimes /* 81126f9a767SRodney W. Grimes * vectp also becomes our initial stack base 81226f9a767SRodney W. Grimes */ 813654f6be1SBruce Evans stack_base = (register_t *)vectp; 81426f9a767SRodney W. Grimes 815c52007c2SDavid Greenman stringp = imgp->stringbase; 816c52007c2SDavid Greenman argc = imgp->argc; 817c52007c2SDavid Greenman envc = imgp->envc; 81826f9a767SRodney W. Grimes 81993f6448cSDavid Greenman /* 8203aed948bSDavid Greenman * Copy out strings - arguments and environment. 82193f6448cSDavid Greenman */ 822c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 82393f6448cSDavid Greenman 82493f6448cSDavid Greenman /* 8253aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 8263aed948bSDavid Greenman */ 827aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 8283aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 8293aed948bSDavid Greenman 8303aed948bSDavid Greenman /* 8313aed948bSDavid Greenman * Fill in argument portion of vector table. 83293f6448cSDavid Greenman */ 83326f9a767SRodney W. Grimes for (; argc > 0; --argc) { 834aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 8353aed948bSDavid Greenman while (*stringp++ != 0) 8363aed948bSDavid Greenman destp++; 8373aed948bSDavid Greenman destp++; 83826f9a767SRodney W. Grimes } 83926f9a767SRodney W. Grimes 8401a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 8416ab46d52SBruce Evans suword(vectp++, 0); 84226f9a767SRodney W. Grimes 843aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 8443aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 84593f6448cSDavid Greenman 84693f6448cSDavid Greenman /* 8473aed948bSDavid Greenman * Fill in environment portion of vector table. 84893f6448cSDavid Greenman */ 84926f9a767SRodney W. Grimes for (; envc > 0; --envc) { 850aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 8513aed948bSDavid Greenman while (*stringp++ != 0) 8523aed948bSDavid Greenman destp++; 8533aed948bSDavid Greenman destp++; 85426f9a767SRodney W. Grimes } 85526f9a767SRodney W. Grimes 85626f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 8576ab46d52SBruce Evans suword(vectp, 0); 85826f9a767SRodney W. Grimes 85926f9a767SRodney W. Grimes return (stack_base); 86026f9a767SRodney W. Grimes } 86126f9a767SRodney W. Grimes 86226f9a767SRodney W. Grimes /* 86326f9a767SRodney W. Grimes * Check permissions of file to execute. 864cf64863aSRobert Watson * Called with imgp->vp locked. 86526f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 86626f9a767SRodney W. Grimes */ 867c8a79999SPeter Wemm int 868c52007c2SDavid Greenman exec_check_permissions(imgp) 869c52007c2SDavid Greenman struct image_params *imgp; 87026f9a767SRodney W. Grimes { 871c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 872c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 873a854ed98SJohn Baldwin struct thread *td; 87426f9a767SRodney W. Grimes int error; 87526f9a767SRodney W. Grimes 876a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 87726f9a767SRodney W. Grimes /* Get file attributes */ 878a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 87926f9a767SRodney W. Grimes if (error) 88026f9a767SRodney W. Grimes return (error); 88126f9a767SRodney W. Grimes 88226f9a767SRodney W. Grimes /* 88326f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 88426f9a767SRodney W. Grimes * file resides on. 88526f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 88626f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 88726f9a767SRodney W. Grimes * file really is executable. 88826f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 88926f9a767SRodney W. Grimes */ 890c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 89126f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 892a854ed98SJohn Baldwin (attr->va_type != VREG)) 89326f9a767SRodney W. Grimes return (EACCES); 89426f9a767SRodney W. Grimes 89526f9a767SRodney W. Grimes /* 89626f9a767SRodney W. Grimes * Zero length files can't be exec'd 89726f9a767SRodney W. Grimes */ 89826f9a767SRodney W. Grimes if (attr->va_size == 0) 89926f9a767SRodney W. Grimes return (ENOEXEC); 90026f9a767SRodney W. Grimes 90126f9a767SRodney W. Grimes /* 90226f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 90326f9a767SRodney W. Grimes */ 904a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 90526f9a767SRodney W. Grimes if (error) 90626f9a767SRodney W. Grimes return (error); 90726f9a767SRodney W. Grimes 9086d5a0a8cSDavid Greenman /* 9096d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 9106d5a0a8cSDavid Greenman * if there are any. 9116d5a0a8cSDavid Greenman */ 9126d5a0a8cSDavid Greenman if (vp->v_writecount) 9136d5a0a8cSDavid Greenman return (ETXTBSY); 9146d5a0a8cSDavid Greenman 9156d5a0a8cSDavid Greenman /* 9166d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 9176d5a0a8cSDavid Greenman * general case). 9186d5a0a8cSDavid Greenman */ 919a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 92026f9a767SRodney W. Grimes return (error); 921df8bae1dSRodney W. Grimes } 922aa855a59SPeter Wemm 923aa855a59SPeter Wemm /* 924aa855a59SPeter Wemm * Exec handler registration 925aa855a59SPeter Wemm */ 926aa855a59SPeter Wemm int 927aa855a59SPeter Wemm exec_register(execsw_arg) 928aa855a59SPeter Wemm const struct execsw *execsw_arg; 929aa855a59SPeter Wemm { 930aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 931aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 932aa855a59SPeter Wemm 933aa855a59SPeter Wemm if (execsw) 934aa855a59SPeter Wemm for (es = execsw; *es; es++) 935aa855a59SPeter Wemm count++; 936aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 937aa855a59SPeter Wemm if (newexecsw == NULL) 938aa855a59SPeter Wemm return ENOMEM; 939aa855a59SPeter Wemm xs = newexecsw; 940aa855a59SPeter Wemm if (execsw) 941aa855a59SPeter Wemm for (es = execsw; *es; es++) 942aa855a59SPeter Wemm *xs++ = *es; 943aa855a59SPeter Wemm *xs++ = execsw_arg; 944aa855a59SPeter Wemm *xs = NULL; 945aa855a59SPeter Wemm if (execsw) 946aa855a59SPeter Wemm free(execsw, M_TEMP); 947aa855a59SPeter Wemm execsw = newexecsw; 948aa855a59SPeter Wemm return 0; 949aa855a59SPeter Wemm } 950aa855a59SPeter Wemm 951aa855a59SPeter Wemm int 952aa855a59SPeter Wemm exec_unregister(execsw_arg) 953aa855a59SPeter Wemm const struct execsw *execsw_arg; 954aa855a59SPeter Wemm { 955aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 956aa855a59SPeter Wemm int count = 1; 957aa855a59SPeter Wemm 958aa855a59SPeter Wemm if (execsw == NULL) 959aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 960aa855a59SPeter Wemm 961aa855a59SPeter Wemm for (es = execsw; *es; es++) { 962aa855a59SPeter Wemm if (*es == execsw_arg) 963aa855a59SPeter Wemm break; 964aa855a59SPeter Wemm } 965aa855a59SPeter Wemm if (*es == NULL) 966aa855a59SPeter Wemm return ENOENT; 967aa855a59SPeter Wemm for (es = execsw; *es; es++) 968aa855a59SPeter Wemm if (*es != execsw_arg) 969aa855a59SPeter Wemm count++; 970aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 971aa855a59SPeter Wemm if (newexecsw == NULL) 972aa855a59SPeter Wemm return ENOMEM; 973aa855a59SPeter Wemm xs = newexecsw; 974aa855a59SPeter Wemm for (es = execsw; *es; es++) 975aa855a59SPeter Wemm if (*es != execsw_arg) 976aa855a59SPeter Wemm *xs++ = *es; 977aa855a59SPeter Wemm *xs = NULL; 978aa855a59SPeter Wemm if (execsw) 979aa855a59SPeter Wemm free(execsw, M_TEMP); 980aa855a59SPeter Wemm execsw = newexecsw; 981aa855a59SPeter Wemm return 0; 982aa855a59SPeter Wemm } 98321d56e9cSAlfred Perlstein 98421d56e9cSAlfred Perlstein int 98521d56e9cSAlfred Perlstein at_exec(function) 98621d56e9cSAlfred Perlstein execlist_fn function; 98721d56e9cSAlfred Perlstein { 98821d56e9cSAlfred Perlstein struct execlist *ep; 98921d56e9cSAlfred Perlstein 99021d56e9cSAlfred Perlstein #ifdef INVARIANTS 99121d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 99221d56e9cSAlfred Perlstein if (rm_at_exec(function)) 99321d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 99421d56e9cSAlfred Perlstein function); 99521d56e9cSAlfred Perlstein #endif 99621d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 99721d56e9cSAlfred Perlstein if (ep == NULL) 99821d56e9cSAlfred Perlstein return (ENOMEM); 99921d56e9cSAlfred Perlstein ep->function = function; 100021d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 100121d56e9cSAlfred Perlstein return (0); 100221d56e9cSAlfred Perlstein } 100321d56e9cSAlfred Perlstein 100421d56e9cSAlfred Perlstein /* 100521d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 100621d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 100721d56e9cSAlfred Perlstein */ 100821d56e9cSAlfred Perlstein int 100921d56e9cSAlfred Perlstein rm_at_exec(function) 101021d56e9cSAlfred Perlstein execlist_fn function; 101121d56e9cSAlfred Perlstein { 101221d56e9cSAlfred Perlstein struct execlist *ep; 101321d56e9cSAlfred Perlstein 101421d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 101521d56e9cSAlfred Perlstein if (ep->function == function) { 101621d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 101721d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 101821d56e9cSAlfred Perlstein return(1); 101921d56e9cSAlfred Perlstein } 102021d56e9cSAlfred Perlstein } 102121d56e9cSAlfred Perlstein return (0); 102221d56e9cSAlfred Perlstein } 102321d56e9cSAlfred Perlstein 1024