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 * 261ebd0c59SDavid Greenman * $Id: kern_exec.c,v 1.61 1997/04/13 03:05:31 dyson Exp $ 27df8bae1dSRodney W. Grimes */ 28df8bae1dSRodney W. Grimes 29df8bae1dSRodney W. Grimes #include <sys/param.h> 3026f9a767SRodney W. Grimes #include <sys/systm.h> 31d2d3e875SBruce Evans #include <sys/sysproto.h> 3226f9a767SRodney W. Grimes #include <sys/signalvar.h> 3326f9a767SRodney W. Grimes #include <sys/kernel.h> 3426f9a767SRodney W. Grimes #include <sys/mount.h> 35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 36079cc25bSDavid Greenman #include <sys/fcntl.h> 3726f9a767SRodney W. Grimes #include <sys/acct.h> 3826f9a767SRodney W. Grimes #include <sys/exec.h> 3926f9a767SRodney W. Grimes #include <sys/imgact.h> 40e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4126f9a767SRodney W. Grimes #include <sys/wait.h> 42a794e791SBruce Evans #include <sys/proc.h> 4326f9a767SRodney W. Grimes #include <sys/malloc.h> 44a794e791SBruce Evans #include <sys/namei.h> 451e1e0b44SSøren Schmidt #include <sys/sysent.h> 4626f9a767SRodney W. Grimes #include <sys/syslog.h> 47797f2d22SPoul-Henning Kamp #include <sys/shm.h> 4899ac3bc8SPeter Wemm #include <sys/sysctl.h> 49a794e791SBruce Evans #include <sys/vnode.h> 509caaadb6SDavid Greenman #include <sys/buf.h> 5126f9a767SRodney W. Grimes 5226f9a767SRodney W. Grimes #include <vm/vm.h> 53efeaf95aSDavid Greenman #include <vm/vm_param.h> 54efeaf95aSDavid Greenman #include <vm/vm_prot.h> 55996c772fSJohn Dyson #include <sys/lock.h> 56efeaf95aSDavid Greenman #include <vm/pmap.h> 57efeaf95aSDavid Greenman #include <vm/vm_map.h> 5826f9a767SRodney W. Grimes #include <vm/vm_kern.h> 59efeaf95aSDavid Greenman #include <vm/vm_extern.h> 601ebd0c59SDavid Greenman #include <vm/vm_object.h> 6126f9a767SRodney W. Grimes 625856e12eSJohn Dyson #include <sys/user.h> 635856e12eSJohn Dyson 6426f9a767SRodney W. Grimes #include <machine/reg.h> 6526f9a767SRodney W. Grimes 6687b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *)); 67df8bae1dSRodney W. Grimes 68f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *); 69f23b4c91SGarrett Wollman 70df8bae1dSRodney W. Grimes /* 7199ac3bc8SPeter Wemm * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts 7299ac3bc8SPeter Wemm * of the sysctl code also assumes this, and sizeof(int) == sizeof(long). 7399ac3bc8SPeter Wemm */ 746120fef1SDavid Greenman static struct ps_strings *ps_strings = PS_STRINGS; 7599ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, ""); 7699ac3bc8SPeter Wemm 7799ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK; 7899ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, ""); 7999ac3bc8SPeter Wemm 8099ac3bc8SPeter Wemm /* 8126f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 8226f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 83df8bae1dSRodney W. Grimes */ 8487b6de2bSPoul-Henning Kamp static const struct execsw **execsw = 8587b6de2bSPoul-Henning Kamp (const struct execsw **)&execsw_set.ls_items[0]; 8626f9a767SRodney W. Grimes 87d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 88ad7507e2SSteven Wallace struct execve_args { 89ad7507e2SSteven Wallace char *fname; 90ad7507e2SSteven Wallace char **argv; 91ad7507e2SSteven Wallace char **envv; 92ad7507e2SSteven Wallace }; 93d2d3e875SBruce Evans #endif 94ad7507e2SSteven Wallace 9526f9a767SRodney W. Grimes /* 9626f9a767SRodney W. Grimes * execve() system call. 9726f9a767SRodney W. Grimes */ 9826f9a767SRodney W. Grimes int 9926f9a767SRodney W. Grimes execve(p, uap, retval) 10026f9a767SRodney W. Grimes struct proc *p; 10126f9a767SRodney W. Grimes register struct execve_args *uap; 10226f9a767SRodney W. Grimes int *retval; 103df8bae1dSRodney W. Grimes { 10426f9a767SRodney W. Grimes struct nameidata nd, *ndp; 10526f9a767SRodney W. Grimes int *stack_base; 106bb56ec4aSPoul-Henning Kamp int error, len, i; 107c52007c2SDavid Greenman struct image_params image_params, *imgp; 10826f9a767SRodney W. Grimes struct vattr attr; 1099caaadb6SDavid Greenman struct buf *bp = NULL; 11026f9a767SRodney W. Grimes 111c52007c2SDavid Greenman imgp = &image_params; 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes /* 114c52007c2SDavid Greenman * Initialize part of the common data 115df8bae1dSRodney W. Grimes */ 116c52007c2SDavid Greenman imgp->proc = p; 117c52007c2SDavid Greenman imgp->uap = uap; 118c52007c2SDavid Greenman imgp->attr = &attr; 119c52007c2SDavid Greenman imgp->image_header = NULL; 120c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 121c52007c2SDavid Greenman imgp->entry_addr = 0; 122c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 123c52007c2SDavid Greenman imgp->interpreted = 0; 124c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 125e1743d02SSøren Schmidt imgp->auxargs = NULL; 12626f9a767SRodney W. Grimes 12726f9a767SRodney W. Grimes /* 12826f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12926f9a767SRodney W. Grimes * environment strings 13026f9a767SRodney W. Grimes */ 13186064318SDavid Greenman imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX); 132c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 13326f9a767SRodney W. Grimes error = ENOMEM; 13426f9a767SRodney W. Grimes goto exec_fail; 13526f9a767SRodney W. Grimes } 136c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 137c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 13826f9a767SRodney W. Grimes 13926f9a767SRodney W. Grimes /* 14026f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 14126f9a767SRodney W. Grimes * in ni_vp amoung other things. 14226f9a767SRodney W. Grimes */ 14326f9a767SRodney W. Grimes ndp = &nd; 144b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1453ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 14626f9a767SRodney W. Grimes 14726f9a767SRodney W. Grimes interpret: 14826f9a767SRodney W. Grimes 14926f9a767SRodney W. Grimes error = namei(ndp); 15026f9a767SRodney W. Grimes if (error) { 15186064318SDavid Greenman kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 15226f9a767SRodney W. Grimes goto exec_fail; 15326f9a767SRodney W. Grimes } 15426f9a767SRodney W. Grimes 155c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 15626f9a767SRodney W. Grimes 15726f9a767SRodney W. Grimes /* 1589022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1599022e4adSDavid Greenman */ 160c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1618677f509SDavid Greenman if (error) { 1628677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 16326f9a767SRodney W. Grimes goto exec_fail_dealloc; 1648677f509SDavid Greenman } 16526f9a767SRodney W. Grimes 16626f9a767SRodney W. Grimes /* 1679caaadb6SDavid Greenman * Get the image header, which we define here as meaning the first 1689caaadb6SDavid Greenman * page of the executable. 16926f9a767SRodney W. Grimes */ 1701ebd0c59SDavid Greenman if (imgp->vp->v_object && imgp->vp->v_mount && 1711ebd0c59SDavid Greenman imgp->vp->v_mount->mnt_stat.f_iosize >= PAGE_SIZE && 1721ebd0c59SDavid Greenman imgp->vp->v_object->un_pager.vnp.vnp_size >= 1731ebd0c59SDavid Greenman imgp->vp->v_mount->mnt_stat.f_iosize) { 1749caaadb6SDavid Greenman /* 1759caaadb6SDavid Greenman * Get a buffer with (at least) the first page. 1769caaadb6SDavid Greenman */ 1779caaadb6SDavid Greenman error = bread(imgp->vp, 0, imgp->vp->v_mount->mnt_stat.f_iosize, 1789caaadb6SDavid Greenman p->p_ucred, &bp); 1799caaadb6SDavid Greenman imgp->image_header = bp->b_data; 1809caaadb6SDavid Greenman } else { 1811ebd0c59SDavid Greenman int resid; 1821ebd0c59SDavid Greenman 1839caaadb6SDavid Greenman /* 1849caaadb6SDavid Greenman * The filesystem block size is too small, so do this the hard 1859caaadb6SDavid Greenman * way. Malloc some space and read PAGE_SIZE worth of the image 1869caaadb6SDavid Greenman * header into it. 1879caaadb6SDavid Greenman */ 1889caaadb6SDavid Greenman imgp->image_header = malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 1899caaadb6SDavid Greenman error = vn_rdwr(UIO_READ, imgp->vp, (void *)imgp->image_header, PAGE_SIZE, 0, 1901ebd0c59SDavid Greenman UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p); 1911ebd0c59SDavid Greenman /* 1921ebd0c59SDavid Greenman * Clear out any remaining junk. 1931ebd0c59SDavid Greenman */ 1941ebd0c59SDavid Greenman if (!error && resid) 1951ebd0c59SDavid Greenman bzero((char *)imgp->image_header + PAGE_SIZE - resid, resid); 1969caaadb6SDavid Greenman } 1979caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1986d5a0a8cSDavid Greenman if (error) 19926f9a767SRodney W. Grimes goto exec_fail_dealloc; 20026f9a767SRodney W. Grimes 20126f9a767SRodney W. Grimes /* 20226f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 20326f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 20426f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 20526f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 20626f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 20726f9a767SRodney W. Grimes * the interpreter. 20826f9a767SRodney W. Grimes */ 20926f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 21026f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 211c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 21226f9a767SRodney W. Grimes else 21326f9a767SRodney W. Grimes continue; 21426f9a767SRodney W. Grimes if (error == -1) 21526f9a767SRodney W. Grimes continue; 21626f9a767SRodney W. Grimes if (error) 21726f9a767SRodney W. Grimes goto exec_fail_dealloc; 218c52007c2SDavid Greenman if (imgp->interpreted) { 2199caaadb6SDavid Greenman /* free old bp/image_header */ 2209caaadb6SDavid Greenman if (bp != NULL) { 2219caaadb6SDavid Greenman brelse(bp); 2229caaadb6SDavid Greenman bp = NULL; 2239caaadb6SDavid Greenman } else { 2249caaadb6SDavid Greenman free((void *)imgp->image_header, M_TEMP); 2256d5a0a8cSDavid Greenman imgp->image_header = NULL; 2269caaadb6SDavid Greenman } 22726f9a767SRodney W. Grimes /* free old vnode and name buffer */ 228f5277ae7SDavid Greenman vrele(ndp->ni_vp); 22926f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 23026f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 231b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 232c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 23326f9a767SRodney W. Grimes goto interpret; 23426f9a767SRodney W. Grimes } 23526f9a767SRodney W. Grimes break; 23626f9a767SRodney W. Grimes } 23726f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 23826f9a767SRodney W. Grimes if (error == -1) { 23926f9a767SRodney W. Grimes error = ENOEXEC; 24026f9a767SRodney W. Grimes goto exec_fail_dealloc; 24126f9a767SRodney W. Grimes } 24226f9a767SRodney W. Grimes 24326f9a767SRodney W. Grimes /* 24426f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 24526f9a767SRodney W. Grimes */ 246c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 24726f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 24826f9a767SRodney W. Grimes 24926f9a767SRodney W. Grimes /* 2501e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2511e1e0b44SSøren Schmidt * let it do the stack setup. 2521e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 25326f9a767SRodney W. Grimes */ 2541e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 255c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2561e1e0b44SSøren Schmidt else 257c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 25826f9a767SRodney W. Grimes 25926f9a767SRodney W. Grimes /* close files on exec */ 26026f9a767SRodney W. Grimes fdcloseexec(p); 26126f9a767SRodney W. Grimes 26226f9a767SRodney W. Grimes /* reset caught signals */ 26326f9a767SRodney W. Grimes execsigs(p); 26426f9a767SRodney W. Grimes 26526f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 26626f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 26726f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 26826f9a767SRodney W. Grimes p->p_comm[len] = 0; 26926f9a767SRodney W. Grimes 27026f9a767SRodney W. Grimes /* 27124b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 27226f9a767SRodney W. Grimes * it that it now has it's own resources back 27326f9a767SRodney W. Grimes */ 27426f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 27526f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 27626f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 27726f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 27826f9a767SRodney W. Grimes } 27926f9a767SRodney W. Grimes 28026f9a767SRodney W. Grimes /* 281c52007c2SDavid Greenman * Implement image setuid/setgid. Disallow if the process is 282c52007c2SDavid Greenman * being traced. 283c52007c2SDavid Greenman */ 284c52007c2SDavid Greenman if ((attr.va_mode & (VSUID | VSGID)) && 285c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 286c52007c2SDavid Greenman /* 287c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 28826f9a767SRodney W. Grimes * root. 28926f9a767SRodney W. Grimes */ 290c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 29126f9a767SRodney W. Grimes p->p_traceflag = 0; 29226f9a767SRodney W. Grimes vrele(p->p_tracep); 293c52007c2SDavid Greenman p->p_tracep = NULL; 29426f9a767SRodney W. Grimes } 295c52007c2SDavid Greenman /* 296c52007c2SDavid Greenman * Set the new credentials. 297c52007c2SDavid Greenman */ 29826f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 299c52007c2SDavid Greenman if (attr.va_mode & VSUID) 30026f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 301c52007c2SDavid Greenman if (attr.va_mode & VSGID) 30226f9a767SRodney W. Grimes p->p_ucred->cr_groups[0] = attr.va_gid; 30326f9a767SRodney W. Grimes p->p_flag |= P_SUGID; 304c52007c2SDavid Greenman } else { 305e47bda07SDavid Greenman if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 306e47bda07SDavid Greenman p->p_ucred->cr_gid == p->p_cred->p_rgid) 307c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 30826f9a767SRodney W. Grimes } 30926f9a767SRodney W. Grimes 31026f9a767SRodney W. Grimes /* 311c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 31226f9a767SRodney W. Grimes */ 31326f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 31426f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 31526f9a767SRodney W. Grimes 31626f9a767SRodney W. Grimes /* 3172a531c80SDavid Greenman * Store the vp for use in procfs 3182a531c80SDavid Greenman */ 3192a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3202a531c80SDavid Greenman vrele(p->p_textvp); 3212a531c80SDavid Greenman VREF(ndp->ni_vp); 3222a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3232a531c80SDavid Greenman 3242a531c80SDavid Greenman /* 32526f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 32626f9a767SRodney W. Grimes * can be set before the program executes. 32726f9a767SRodney W. Grimes */ 32826f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 32926f9a767SRodney W. Grimes psignal(p, SIGTRAP); 33026f9a767SRodney W. Grimes 33126f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 33226f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 33326f9a767SRodney W. Grimes 33426f9a767SRodney W. Grimes /* Set entry address */ 335c52007c2SDavid Greenman setregs(p, imgp->entry_addr, (u_long)stack_base); 33626f9a767SRodney W. Grimes 33726f9a767SRodney W. Grimes /* 33826f9a767SRodney W. Grimes * free various allocated resources 33926f9a767SRodney W. Grimes */ 34086064318SDavid Greenman kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 3419caaadb6SDavid Greenman if (bp != NULL) 3429caaadb6SDavid Greenman brelse(bp); 3439caaadb6SDavid Greenman else if (imgp->image_header != NULL) 3449caaadb6SDavid Greenman free((void *)imgp->image_header, M_TEMP); 345f5277ae7SDavid Greenman vrele(ndp->ni_vp); 34626f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 34726f9a767SRodney W. Grimes 34826f9a767SRodney W. Grimes return (0); 34926f9a767SRodney W. Grimes 35026f9a767SRodney W. Grimes exec_fail_dealloc: 351c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 35286064318SDavid Greenman kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 3539caaadb6SDavid Greenman if (bp != NULL) 3549caaadb6SDavid Greenman brelse(bp); 3559caaadb6SDavid Greenman else if (imgp->image_header != NULL) 3569caaadb6SDavid Greenman free((void *)imgp->image_header, M_TEMP); 357a3cf6ebaSDavid Greenman if (ndp->ni_vp) { 358f5277ae7SDavid Greenman vrele(ndp->ni_vp); 35926f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 360a3cf6ebaSDavid Greenman } 36126f9a767SRodney W. Grimes 36226f9a767SRodney W. Grimes exec_fail: 363c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 36426f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 36526f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 36626f9a767SRodney W. Grimes /* NOT REACHED */ 36726f9a767SRodney W. Grimes return(0); 36826f9a767SRodney W. Grimes } else { 36926f9a767SRodney W. Grimes return(error); 37026f9a767SRodney W. Grimes } 37126f9a767SRodney W. Grimes } 37226f9a767SRodney W. Grimes 37326f9a767SRodney W. Grimes /* 37426f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 37526f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 37626f9a767SRodney W. Grimes * automatically in trap.c. 37726f9a767SRodney W. Grimes */ 37826f9a767SRodney W. Grimes int 379c52007c2SDavid Greenman exec_new_vmspace(imgp) 380c52007c2SDavid Greenman struct image_params *imgp; 38126f9a767SRodney W. Grimes { 38226f9a767SRodney W. Grimes int error; 383c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 38426f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 385af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 38626f9a767SRodney W. Grimes 387c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 38826f9a767SRodney W. Grimes 389af9ec885SJohn Dyson /* 390af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 391af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 392af9ec885SJohn Dyson * not disrupted 393af9ec885SJohn Dyson */ 394af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 3953d903220SDoug Rabson if (vmspace->vm_shm) 396c52007c2SDavid Greenman shmexit(imgp->proc); 3979d3fbbb5SJohn Dyson pmap_remove_pages(&vmspace->vm_pmap, 0, USRSTACK); 398af9ec885SJohn Dyson vm_map_remove(map, 0, USRSTACK); 399af9ec885SJohn Dyson } else { 400492da96cSJohn Dyson vmspace_exec(imgp->proc); 401492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 402af9ec885SJohn Dyson map = &vmspace->vm_map; 403af9ec885SJohn Dyson } 40426f9a767SRodney W. Grimes 40526f9a767SRodney W. Grimes /* Allocate a new stack */ 406af9ec885SJohn Dyson error = vm_map_find(map, NULL, 0, (vm_offset_t *)&stack_addr, 407bd7e5f99SJohn Dyson SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 40826f9a767SRodney W. Grimes if (error) 40926f9a767SRodney W. Grimes return(error); 41026f9a767SRodney W. Grimes 41126f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 41226f9a767SRodney W. Grimes 41326f9a767SRodney W. Grimes /* Initialize maximum stack address */ 41426f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 41526f9a767SRodney W. Grimes 41626f9a767SRodney W. Grimes return(0); 41726f9a767SRodney W. Grimes } 41826f9a767SRodney W. Grimes 41926f9a767SRodney W. Grimes /* 42026f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 42126f9a767SRodney W. Grimes * address space into the temporary string buffer. 42226f9a767SRodney W. Grimes */ 42326f9a767SRodney W. Grimes int 424c52007c2SDavid Greenman exec_extract_strings(imgp) 425c52007c2SDavid Greenman struct image_params *imgp; 42626f9a767SRodney W. Grimes { 42726f9a767SRodney W. Grimes char **argv, **envv; 42826f9a767SRodney W. Grimes char *argp, *envp; 4290fd1a014SDavid Greenman int error, length; 43026f9a767SRodney W. Grimes 43126f9a767SRodney W. Grimes /* 43226f9a767SRodney W. Grimes * extract arguments first 43326f9a767SRodney W. Grimes */ 43426f9a767SRodney W. Grimes 435c52007c2SDavid Greenman argv = imgp->uap->argv; 43626f9a767SRodney W. Grimes 4370fd1a014SDavid Greenman if (argv) { 438bb56ec4aSPoul-Henning Kamp while ((argp = (caddr_t) fuword(argv++))) { 43926f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 44026f9a767SRodney W. Grimes return (EFAULT); 441c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 442c52007c2SDavid Greenman imgp->stringspace, &length))) { 4430fd1a014SDavid Greenman if (error == ENAMETOOLONG) 44426f9a767SRodney W. Grimes return(E2BIG); 4450fd1a014SDavid Greenman return (error); 4460fd1a014SDavid Greenman } 447c52007c2SDavid Greenman imgp->stringspace -= length; 448c52007c2SDavid Greenman imgp->stringp += length; 449c52007c2SDavid Greenman imgp->argc++; 45026f9a767SRodney W. Grimes } 4510fd1a014SDavid Greenman } 45226f9a767SRodney W. Grimes 45326f9a767SRodney W. Grimes /* 45426f9a767SRodney W. Grimes * extract environment strings 45526f9a767SRodney W. Grimes */ 45626f9a767SRodney W. Grimes 457c52007c2SDavid Greenman envv = imgp->uap->envv; 45826f9a767SRodney W. Grimes 4590fd1a014SDavid Greenman if (envv) { 460bb56ec4aSPoul-Henning Kamp while ((envp = (caddr_t) fuword(envv++))) { 46126f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 46226f9a767SRodney W. Grimes return (EFAULT); 463c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 464c52007c2SDavid Greenman imgp->stringspace, &length))) { 4650fd1a014SDavid Greenman if (error == ENAMETOOLONG) 46626f9a767SRodney W. Grimes return(E2BIG); 4670fd1a014SDavid Greenman return (error); 4680fd1a014SDavid Greenman } 469c52007c2SDavid Greenman imgp->stringspace -= length; 470c52007c2SDavid Greenman imgp->stringp += length; 471c52007c2SDavid Greenman imgp->envc++; 47226f9a767SRodney W. Grimes } 4730fd1a014SDavid Greenman } 47426f9a767SRodney W. Grimes 47526f9a767SRodney W. Grimes return (0); 47626f9a767SRodney W. Grimes } 47726f9a767SRodney W. Grimes 47826f9a767SRodney W. Grimes /* 47926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 48026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 48126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 48226f9a767SRodney W. Grimes */ 48326f9a767SRodney W. Grimes int * 484c52007c2SDavid Greenman exec_copyout_strings(imgp) 485c52007c2SDavid Greenman struct image_params *imgp; 48626f9a767SRodney W. Grimes { 48726f9a767SRodney W. Grimes int argc, envc; 48826f9a767SRodney W. Grimes char **vectp; 48926f9a767SRodney W. Grimes char *stringp, *destp; 49026f9a767SRodney W. Grimes int *stack_base; 49193f6448cSDavid Greenman struct ps_strings *arginfo; 492d66a5066SPeter Wemm int szsigcode; 49326f9a767SRodney W. Grimes 49426f9a767SRodney W. Grimes /* 49526f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 496d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 49726f9a767SRodney W. Grimes */ 49893f6448cSDavid Greenman arginfo = PS_STRINGS; 499d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 500d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 5011ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 5021ed012f9SPeter Wemm 50326f9a767SRodney W. Grimes /* 504d66a5066SPeter Wemm * install sigcode 505d66a5066SPeter Wemm */ 506d66a5066SPeter Wemm if (szsigcode) 507d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 508d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 509d66a5066SPeter Wemm 510d66a5066SPeter Wemm /* 511e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 512e1743d02SSøren Schmidt * on the stack. 513e1743d02SSøren Schmidt */ 514e1743d02SSøren Schmidt if (imgp->auxargs) 515e1743d02SSøren Schmidt /* 516e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 517e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 518e1743d02SSøren Schmidt * ELF Auxargs data. 519e1743d02SSøren Schmidt */ 520e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 521e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 522e1743d02SSøren Schmidt else 523e1743d02SSøren Schmidt /* 52426f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 52526f9a767SRodney W. Grimes * arg and env vector sets 52626f9a767SRodney W. Grimes */ 527e1743d02SSøren Schmidt vectp = (char **) 528e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 52926f9a767SRodney W. Grimes 53026f9a767SRodney W. Grimes /* 53126f9a767SRodney W. Grimes * vectp also becomes our initial stack base 53226f9a767SRodney W. Grimes */ 53326f9a767SRodney W. Grimes stack_base = (int *)vectp; 53426f9a767SRodney W. Grimes 535c52007c2SDavid Greenman stringp = imgp->stringbase; 536c52007c2SDavid Greenman argc = imgp->argc; 537c52007c2SDavid Greenman envc = imgp->envc; 53826f9a767SRodney W. Grimes 53993f6448cSDavid Greenman /* 5403aed948bSDavid Greenman * Copy out strings - arguments and environment. 54193f6448cSDavid Greenman */ 542c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 54393f6448cSDavid Greenman 54493f6448cSDavid Greenman /* 5453aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 5463aed948bSDavid Greenman */ 5471ed012f9SPeter Wemm suword(&arginfo->ps_argvstr, (int)vectp); 5483aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 5493aed948bSDavid Greenman 5503aed948bSDavid Greenman /* 5513aed948bSDavid Greenman * Fill in argument portion of vector table. 55293f6448cSDavid Greenman */ 55326f9a767SRodney W. Grimes for (; argc > 0; --argc) { 5543aed948bSDavid Greenman suword(vectp++, (int)destp); 5553aed948bSDavid Greenman while (*stringp++ != 0) 5563aed948bSDavid Greenman destp++; 5573aed948bSDavid Greenman destp++; 55826f9a767SRodney W. Grimes } 55926f9a767SRodney W. Grimes 56026f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 5616ab46d52SBruce Evans suword(vectp++, 0); 56226f9a767SRodney W. Grimes 5631ed012f9SPeter Wemm suword(&arginfo->ps_envstr, (int)vectp); 5643aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 56593f6448cSDavid Greenman 56693f6448cSDavid Greenman /* 5673aed948bSDavid Greenman * Fill in environment portion of vector table. 56893f6448cSDavid Greenman */ 56926f9a767SRodney W. Grimes for (; envc > 0; --envc) { 5703aed948bSDavid Greenman suword(vectp++, (int)destp); 5713aed948bSDavid Greenman while (*stringp++ != 0) 5723aed948bSDavid Greenman destp++; 5733aed948bSDavid Greenman destp++; 57426f9a767SRodney W. Grimes } 57526f9a767SRodney W. Grimes 57626f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 5776ab46d52SBruce Evans suword(vectp, 0); 57826f9a767SRodney W. Grimes 57926f9a767SRodney W. Grimes return (stack_base); 58026f9a767SRodney W. Grimes } 58126f9a767SRodney W. Grimes 58226f9a767SRodney W. Grimes /* 58326f9a767SRodney W. Grimes * Check permissions of file to execute. 58426f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 58526f9a767SRodney W. Grimes */ 586f23b4c91SGarrett Wollman static int 587c52007c2SDavid Greenman exec_check_permissions(imgp) 588c52007c2SDavid Greenman struct image_params *imgp; 58926f9a767SRodney W. Grimes { 590c52007c2SDavid Greenman struct proc *p = imgp->proc; 591c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 592c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 59326f9a767SRodney W. Grimes int error; 59426f9a767SRodney W. Grimes 59526f9a767SRodney W. Grimes /* Get file attributes */ 596c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 59726f9a767SRodney W. Grimes if (error) 59826f9a767SRodney W. Grimes return (error); 59926f9a767SRodney W. Grimes 60026f9a767SRodney W. Grimes /* 60126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 60226f9a767SRodney W. Grimes * file resides on. 60326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 60426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 60526f9a767SRodney W. Grimes * file really is executable. 60626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 60726f9a767SRodney W. Grimes */ 608c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 60926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 61026f9a767SRodney W. Grimes (attr->va_type != VREG)) { 61126f9a767SRodney W. Grimes return (EACCES); 61226f9a767SRodney W. Grimes } 61326f9a767SRodney W. Grimes 61426f9a767SRodney W. Grimes /* 61526f9a767SRodney W. Grimes * Zero length files can't be exec'd 61626f9a767SRodney W. Grimes */ 61726f9a767SRodney W. Grimes if (attr->va_size == 0) 61826f9a767SRodney W. Grimes return (ENOEXEC); 61926f9a767SRodney W. Grimes 62026f9a767SRodney W. Grimes /* 62126f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 62226f9a767SRodney W. Grimes */ 623c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 62426f9a767SRodney W. Grimes if (error) 62526f9a767SRodney W. Grimes return (error); 62626f9a767SRodney W. Grimes 6276d5a0a8cSDavid Greenman /* 6286d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 6296d5a0a8cSDavid Greenman * if there are any. 6306d5a0a8cSDavid Greenman */ 6316d5a0a8cSDavid Greenman if (vp->v_writecount) 6326d5a0a8cSDavid Greenman return (ETXTBSY); 6336d5a0a8cSDavid Greenman 6346d5a0a8cSDavid Greenman /* 6356d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 6366d5a0a8cSDavid Greenman * general case). 6376d5a0a8cSDavid Greenman */ 638c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 63926f9a767SRodney W. Grimes if (error) 64026f9a767SRodney W. Grimes return (error); 64126f9a767SRodney W. Grimes 6426d5a0a8cSDavid Greenman /* 6436d5a0a8cSDavid Greenman * Disable setuid/setgid if the filesystem prohibits it or if 6446d5a0a8cSDavid Greenman * the process is being traced. 6456d5a0a8cSDavid Greenman */ 6466d5a0a8cSDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 6476d5a0a8cSDavid Greenman attr->va_mode &= ~(VSUID | VSGID); 6486d5a0a8cSDavid Greenman 64926f9a767SRodney W. Grimes return (0); 650df8bae1dSRodney W. Grimes } 651