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 * 26a794e791SBruce Evans * $Id: kern_exec.c,v 1.39 1996/04/29 15:07:59 smpatel 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> 5026f9a767SRodney W. Grimes 5126f9a767SRodney W. Grimes #include <vm/vm.h> 52efeaf95aSDavid Greenman #include <vm/vm_param.h> 53efeaf95aSDavid Greenman #include <vm/vm_prot.h> 54efeaf95aSDavid Greenman #include <vm/lock.h> 55efeaf95aSDavid Greenman #include <vm/pmap.h> 56efeaf95aSDavid Greenman #include <vm/vm_map.h> 5726f9a767SRodney W. Grimes #include <vm/vm_kern.h> 58efeaf95aSDavid Greenman #include <vm/vm_extern.h> 5926f9a767SRodney W. Grimes 6026f9a767SRodney W. Grimes #include <machine/reg.h> 6126f9a767SRodney W. Grimes 6287b6de2bSPoul-Henning Kamp static int *exec_copyout_strings __P((struct image_params *)); 63df8bae1dSRodney W. Grimes 64f23b4c91SGarrett Wollman static int exec_check_permissions(struct image_params *); 65f23b4c91SGarrett Wollman 66df8bae1dSRodney W. Grimes /* 6799ac3bc8SPeter Wemm * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts 6899ac3bc8SPeter Wemm * of the sysctl code also assumes this, and sizeof(int) == sizeof(long). 6999ac3bc8SPeter Wemm */ 7099ac3bc8SPeter Wemm static caddr_t ps_strings = (caddr_t)PS_STRINGS; 7199ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, ""); 7299ac3bc8SPeter Wemm 7399ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK; 7499ac3bc8SPeter Wemm SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, ""); 7599ac3bc8SPeter Wemm 7699ac3bc8SPeter Wemm /* 7726f9a767SRodney W. Grimes * execsw_set is constructed for us by the linker. Each of the items 7826f9a767SRodney W. Grimes * is a pointer to a `const struct execsw', hence the double pointer here. 79df8bae1dSRodney W. Grimes */ 8087b6de2bSPoul-Henning Kamp static const struct execsw **execsw = 8187b6de2bSPoul-Henning Kamp (const struct execsw **)&execsw_set.ls_items[0]; 8226f9a767SRodney W. Grimes 83d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 84ad7507e2SSteven Wallace struct execve_args { 85ad7507e2SSteven Wallace char *fname; 86ad7507e2SSteven Wallace char **argv; 87ad7507e2SSteven Wallace char **envv; 88ad7507e2SSteven Wallace }; 89d2d3e875SBruce Evans #endif 90ad7507e2SSteven Wallace 9126f9a767SRodney W. Grimes /* 9226f9a767SRodney W. Grimes * execve() system call. 9326f9a767SRodney W. Grimes */ 9426f9a767SRodney W. Grimes int 9526f9a767SRodney W. Grimes execve(p, uap, retval) 9626f9a767SRodney W. Grimes struct proc *p; 9726f9a767SRodney W. Grimes register struct execve_args *uap; 9826f9a767SRodney W. Grimes int *retval; 99df8bae1dSRodney W. Grimes { 10026f9a767SRodney W. Grimes struct nameidata nd, *ndp; 10126f9a767SRodney W. Grimes int *stack_base; 102bb56ec4aSPoul-Henning Kamp int error, len, i; 103c52007c2SDavid Greenman struct image_params image_params, *imgp; 10426f9a767SRodney W. Grimes struct vattr attr; 10526f9a767SRodney W. Grimes 106c52007c2SDavid Greenman imgp = &image_params; 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes /* 109c52007c2SDavid Greenman * Initialize part of the common data 110df8bae1dSRodney W. Grimes */ 111c52007c2SDavid Greenman imgp->proc = p; 112c52007c2SDavid Greenman imgp->uap = uap; 113c52007c2SDavid Greenman imgp->attr = &attr; 114c52007c2SDavid Greenman imgp->image_header = NULL; 115c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 116c52007c2SDavid Greenman imgp->entry_addr = 0; 117c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 118c52007c2SDavid Greenman imgp->interpreted = 0; 119c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 120e1743d02SSøren Schmidt imgp->auxargs = NULL; 12126f9a767SRodney W. Grimes 12226f9a767SRodney W. Grimes /* 12326f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12426f9a767SRodney W. Grimes * environment strings 12526f9a767SRodney W. Grimes */ 126c2f9f36bSDavid Greenman imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX); 127c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 12826f9a767SRodney W. Grimes error = ENOMEM; 12926f9a767SRodney W. Grimes goto exec_fail; 13026f9a767SRodney W. Grimes } 131c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 132c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 13326f9a767SRodney W. Grimes 13426f9a767SRodney W. Grimes /* 13526f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 13626f9a767SRodney W. Grimes * in ni_vp amoung other things. 13726f9a767SRodney W. Grimes */ 13826f9a767SRodney W. Grimes ndp = &nd; 139b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1403ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 14126f9a767SRodney W. Grimes 14226f9a767SRodney W. Grimes interpret: 14326f9a767SRodney W. Grimes 14426f9a767SRodney W. Grimes error = namei(ndp); 14526f9a767SRodney W. Grimes if (error) { 146c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 14726f9a767SRodney W. Grimes goto exec_fail; 14826f9a767SRodney W. Grimes } 14926f9a767SRodney W. Grimes 150c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 151c52007c2SDavid Greenman if (imgp->vp == NULL) { 15226f9a767SRodney W. Grimes error = ENOEXEC; 15326f9a767SRodney W. Grimes goto exec_fail_dealloc; 15426f9a767SRodney W. Grimes } 15526f9a767SRodney W. Grimes 15626f9a767SRodney W. Grimes /* 1579022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1589022e4adSDavid Greenman */ 159c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1609022e4adSDavid Greenman 1619022e4adSDavid Greenman /* 1629022e4adSDavid Greenman * Lose the lock on the vnode. It's no longer needed, and must not 163f5277ae7SDavid Greenman * exist for the pagefault paging to work below. 164f5277ae7SDavid Greenman */ 165c52007c2SDavid Greenman VOP_UNLOCK(imgp->vp); 166f5277ae7SDavid Greenman 16726f9a767SRodney W. Grimes if (error) 16826f9a767SRodney W. Grimes goto exec_fail_dealloc; 16926f9a767SRodney W. Grimes 17026f9a767SRodney W. Grimes /* 17126f9a767SRodney W. Grimes * Map the image header (first page) of the file into 17226f9a767SRodney W. Grimes * kernel address space 17326f9a767SRodney W. Grimes */ 17426f9a767SRodney W. Grimes error = vm_mmap(kernel_map, /* map */ 175c52007c2SDavid Greenman (vm_offset_t *)&imgp->image_header, /* address */ 17626f9a767SRodney W. Grimes PAGE_SIZE, /* size */ 17726f9a767SRodney W. Grimes VM_PROT_READ, /* protection */ 17826f9a767SRodney W. Grimes VM_PROT_READ, /* max protection */ 17926f9a767SRodney W. Grimes 0, /* flags */ 180c52007c2SDavid Greenman (caddr_t)imgp->vp, /* vnode */ 18126f9a767SRodney W. Grimes 0); /* offset */ 18226f9a767SRodney W. Grimes if (error) { 18326f9a767SRodney W. Grimes uprintf("mmap failed: %d\n",error); 18426f9a767SRodney W. Grimes goto exec_fail_dealloc; 18526f9a767SRodney W. Grimes } 18626f9a767SRodney W. Grimes 18726f9a767SRodney W. Grimes /* 18826f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 18926f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 19026f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 19126f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 19226f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 19326f9a767SRodney W. Grimes * the interpreter. 19426f9a767SRodney W. Grimes */ 19526f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 19626f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 197c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 19826f9a767SRodney W. Grimes else 19926f9a767SRodney W. Grimes continue; 20026f9a767SRodney W. Grimes 20126f9a767SRodney W. Grimes if (error == -1) 20226f9a767SRodney W. Grimes continue; 20326f9a767SRodney W. Grimes if (error) 20426f9a767SRodney W. Grimes goto exec_fail_dealloc; 205c52007c2SDavid Greenman if (imgp->interpreted) { 20626f9a767SRodney W. Grimes /* free old vnode and name buffer */ 207f5277ae7SDavid Greenman vrele(ndp->ni_vp); 20826f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 209c52007c2SDavid Greenman if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header, 210c52007c2SDavid Greenman (vm_offset_t)imgp->image_header + PAGE_SIZE)) 21126f9a767SRodney W. Grimes panic("execve: header dealloc failed (1)"); 21226f9a767SRodney W. Grimes 21326f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 214b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 215c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 21626f9a767SRodney W. Grimes goto interpret; 21726f9a767SRodney W. Grimes } 21826f9a767SRodney W. Grimes break; 21926f9a767SRodney W. Grimes } 22026f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 22126f9a767SRodney W. Grimes if (error == -1) { 22226f9a767SRodney W. Grimes error = ENOEXEC; 22326f9a767SRodney W. Grimes goto exec_fail_dealloc; 22426f9a767SRodney W. Grimes } 22526f9a767SRodney W. Grimes 22626f9a767SRodney W. Grimes /* 22726f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 22826f9a767SRodney W. Grimes */ 229c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 23026f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 23126f9a767SRodney W. Grimes 23226f9a767SRodney W. Grimes /* 2331e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2341e1e0b44SSøren Schmidt * let it do the stack setup. 2351e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 23626f9a767SRodney W. Grimes */ 2371e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 238c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2391e1e0b44SSøren Schmidt else 240c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 24126f9a767SRodney W. Grimes 24226f9a767SRodney W. Grimes /* close files on exec */ 24326f9a767SRodney W. Grimes fdcloseexec(p); 24426f9a767SRodney W. Grimes 24526f9a767SRodney W. Grimes /* reset caught signals */ 24626f9a767SRodney W. Grimes execsigs(p); 24726f9a767SRodney W. Grimes 24826f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 24926f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 25026f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 25126f9a767SRodney W. Grimes p->p_comm[len] = 0; 25226f9a767SRodney W. Grimes 25326f9a767SRodney W. Grimes /* 25424b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 25526f9a767SRodney W. Grimes * it that it now has it's own resources back 25626f9a767SRodney W. Grimes */ 25726f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 25826f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 25926f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 26026f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 26126f9a767SRodney W. Grimes } 26226f9a767SRodney W. Grimes 26326f9a767SRodney W. Grimes /* 264c52007c2SDavid Greenman * Implement image setuid/setgid. Disallow if the process is 265c52007c2SDavid Greenman * being traced. 266c52007c2SDavid Greenman */ 267c52007c2SDavid Greenman if ((attr.va_mode & (VSUID | VSGID)) && 268c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 269c52007c2SDavid Greenman /* 270c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 27126f9a767SRodney W. Grimes * root. 27226f9a767SRodney W. Grimes */ 273c52007c2SDavid Greenman if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) { 27426f9a767SRodney W. Grimes p->p_traceflag = 0; 27526f9a767SRodney W. Grimes vrele(p->p_tracep); 276c52007c2SDavid Greenman p->p_tracep = NULL; 27726f9a767SRodney W. Grimes } 278c52007c2SDavid Greenman /* 279c52007c2SDavid Greenman * Set the new credentials. 280c52007c2SDavid Greenman */ 28126f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 282c52007c2SDavid Greenman if (attr.va_mode & VSUID) 28326f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 284c52007c2SDavid Greenman if (attr.va_mode & VSGID) 28526f9a767SRodney W. Grimes p->p_ucred->cr_groups[0] = attr.va_gid; 28626f9a767SRodney W. Grimes p->p_flag |= P_SUGID; 287c52007c2SDavid Greenman } else { 288c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 28926f9a767SRodney W. Grimes } 29026f9a767SRodney W. Grimes 29126f9a767SRodney W. Grimes /* 292c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 29326f9a767SRodney W. Grimes */ 29426f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 29526f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 29626f9a767SRodney W. Grimes 29726f9a767SRodney W. Grimes /* 2982a531c80SDavid Greenman * Store the vp for use in procfs 2992a531c80SDavid Greenman */ 3002a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3012a531c80SDavid Greenman vrele(p->p_textvp); 3022a531c80SDavid Greenman VREF(ndp->ni_vp); 3032a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3042a531c80SDavid Greenman 3052a531c80SDavid Greenman /* 30626f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 30726f9a767SRodney W. Grimes * can be set before the program executes. 30826f9a767SRodney W. Grimes */ 30926f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 31026f9a767SRodney W. Grimes psignal(p, SIGTRAP); 31126f9a767SRodney W. Grimes 31226f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 31326f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 31426f9a767SRodney W. Grimes 31526f9a767SRodney W. Grimes /* Set entry address */ 316c52007c2SDavid Greenman setregs(p, imgp->entry_addr, (u_long)stack_base); 31726f9a767SRodney W. Grimes 31826f9a767SRodney W. Grimes /* 31926f9a767SRodney W. Grimes * free various allocated resources 32026f9a767SRodney W. Grimes */ 321c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 322c52007c2SDavid Greenman if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header, 323c52007c2SDavid Greenman (vm_offset_t)imgp->image_header + PAGE_SIZE)) 32426f9a767SRodney W. Grimes panic("execve: header dealloc failed (2)"); 325f5277ae7SDavid Greenman vrele(ndp->ni_vp); 32626f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 32726f9a767SRodney W. Grimes 32826f9a767SRodney W. Grimes return (0); 32926f9a767SRodney W. Grimes 33026f9a767SRodney W. Grimes exec_fail_dealloc: 331c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 332c2f9f36bSDavid Greenman kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX); 333c52007c2SDavid Greenman if (imgp->image_header && imgp->image_header != (char *)-1) 334c52007c2SDavid Greenman if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header, 335c52007c2SDavid Greenman (vm_offset_t)imgp->image_header + PAGE_SIZE)) 33626f9a767SRodney W. Grimes panic("execve: header dealloc failed (3)"); 337f5277ae7SDavid Greenman if (ndp->ni_vp) 338f5277ae7SDavid Greenman vrele(ndp->ni_vp); 33926f9a767SRodney W. Grimes FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI); 34026f9a767SRodney W. Grimes 34126f9a767SRodney W. Grimes exec_fail: 342c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 34326f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 34426f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 34526f9a767SRodney W. Grimes /* NOT REACHED */ 34626f9a767SRodney W. Grimes return(0); 34726f9a767SRodney W. Grimes } else { 34826f9a767SRodney W. Grimes return(error); 34926f9a767SRodney W. Grimes } 35026f9a767SRodney W. Grimes } 35126f9a767SRodney W. Grimes 35226f9a767SRodney W. Grimes /* 35326f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 35426f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 35526f9a767SRodney W. Grimes * automatically in trap.c. 35626f9a767SRodney W. Grimes */ 35726f9a767SRodney W. Grimes int 358c52007c2SDavid Greenman exec_new_vmspace(imgp) 359c52007c2SDavid Greenman struct image_params *imgp; 36026f9a767SRodney W. Grimes { 36126f9a767SRodney W. Grimes int error; 362c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 36326f9a767SRodney W. Grimes caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ); 36426f9a767SRodney W. Grimes 365c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 36626f9a767SRodney W. Grimes 36726f9a767SRodney W. Grimes /* Blow away entire process VM */ 3683d903220SDoug Rabson if (vmspace->vm_shm) 369c52007c2SDavid Greenman shmexit(imgp->proc); 37068940ac1SDavid Greenman vm_map_remove(&vmspace->vm_map, 0, USRSTACK); 37126f9a767SRodney W. Grimes 37226f9a767SRodney W. Grimes /* Allocate a new stack */ 37368940ac1SDavid Greenman error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr, 374bd7e5f99SJohn Dyson SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 37526f9a767SRodney W. Grimes if (error) 37626f9a767SRodney W. Grimes return(error); 37726f9a767SRodney W. Grimes 37826f9a767SRodney W. Grimes vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 37926f9a767SRodney W. Grimes 38026f9a767SRodney W. Grimes /* Initialize maximum stack address */ 38126f9a767SRodney W. Grimes vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 38226f9a767SRodney W. Grimes 38326f9a767SRodney W. Grimes return(0); 38426f9a767SRodney W. Grimes } 38526f9a767SRodney W. Grimes 38626f9a767SRodney W. Grimes /* 38726f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 38826f9a767SRodney W. Grimes * address space into the temporary string buffer. 38926f9a767SRodney W. Grimes */ 39026f9a767SRodney W. Grimes int 391c52007c2SDavid Greenman exec_extract_strings(imgp) 392c52007c2SDavid Greenman struct image_params *imgp; 39326f9a767SRodney W. Grimes { 39426f9a767SRodney W. Grimes char **argv, **envv; 39526f9a767SRodney W. Grimes char *argp, *envp; 3960fd1a014SDavid Greenman int error, length; 39726f9a767SRodney W. Grimes 39826f9a767SRodney W. Grimes /* 39926f9a767SRodney W. Grimes * extract arguments first 40026f9a767SRodney W. Grimes */ 40126f9a767SRodney W. Grimes 402c52007c2SDavid Greenman argv = imgp->uap->argv; 40326f9a767SRodney W. Grimes 4040fd1a014SDavid Greenman if (argv) { 405bb56ec4aSPoul-Henning Kamp while ((argp = (caddr_t) fuword(argv++))) { 40626f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 40726f9a767SRodney W. Grimes return (EFAULT); 408c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 409c52007c2SDavid Greenman imgp->stringspace, &length))) { 4100fd1a014SDavid Greenman if (error == ENAMETOOLONG) 41126f9a767SRodney W. Grimes return(E2BIG); 4120fd1a014SDavid Greenman return (error); 4130fd1a014SDavid Greenman } 414c52007c2SDavid Greenman imgp->stringspace -= length; 415c52007c2SDavid Greenman imgp->stringp += length; 416c52007c2SDavid Greenman imgp->argc++; 41726f9a767SRodney W. Grimes } 4180fd1a014SDavid Greenman } 41926f9a767SRodney W. Grimes 42026f9a767SRodney W. Grimes /* 42126f9a767SRodney W. Grimes * extract environment strings 42226f9a767SRodney W. Grimes */ 42326f9a767SRodney W. Grimes 424c52007c2SDavid Greenman envv = imgp->uap->envv; 42526f9a767SRodney W. Grimes 4260fd1a014SDavid Greenman if (envv) { 427bb56ec4aSPoul-Henning Kamp while ((envp = (caddr_t) fuword(envv++))) { 42826f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 42926f9a767SRodney W. Grimes return (EFAULT); 430c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 431c52007c2SDavid Greenman imgp->stringspace, &length))) { 4320fd1a014SDavid Greenman if (error == ENAMETOOLONG) 43326f9a767SRodney W. Grimes return(E2BIG); 4340fd1a014SDavid Greenman return (error); 4350fd1a014SDavid Greenman } 436c52007c2SDavid Greenman imgp->stringspace -= length; 437c52007c2SDavid Greenman imgp->stringp += length; 438c52007c2SDavid Greenman imgp->envc++; 43926f9a767SRodney W. Grimes } 4400fd1a014SDavid Greenman } 44126f9a767SRodney W. Grimes 44226f9a767SRodney W. Grimes return (0); 44326f9a767SRodney W. Grimes } 44426f9a767SRodney W. Grimes 44526f9a767SRodney W. Grimes /* 44626f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 44726f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 44826f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 44926f9a767SRodney W. Grimes */ 45026f9a767SRodney W. Grimes int * 451c52007c2SDavid Greenman exec_copyout_strings(imgp) 452c52007c2SDavid Greenman struct image_params *imgp; 45326f9a767SRodney W. Grimes { 45426f9a767SRodney W. Grimes int argc, envc; 45526f9a767SRodney W. Grimes char **vectp; 45626f9a767SRodney W. Grimes char *stringp, *destp; 45726f9a767SRodney W. Grimes int *stack_base; 45893f6448cSDavid Greenman struct ps_strings *arginfo; 459d66a5066SPeter Wemm int szsigcode; 46026f9a767SRodney W. Grimes 46126f9a767SRodney W. Grimes /* 46226f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 463d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 46426f9a767SRodney W. Grimes */ 46593f6448cSDavid Greenman arginfo = PS_STRINGS; 466d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 467d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 4681ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 4691ed012f9SPeter Wemm 47026f9a767SRodney W. Grimes /* 471d66a5066SPeter Wemm * install sigcode 472d66a5066SPeter Wemm */ 473d66a5066SPeter Wemm if (szsigcode) 474d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 475d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 476d66a5066SPeter Wemm 477d66a5066SPeter Wemm /* 478e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 479e1743d02SSøren Schmidt * on the stack. 480e1743d02SSøren Schmidt */ 481e1743d02SSøren Schmidt if (imgp->auxargs) 482e1743d02SSøren Schmidt /* 483e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 484e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 485e1743d02SSøren Schmidt * ELF Auxargs data. 486e1743d02SSøren Schmidt */ 487e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 488e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 489e1743d02SSøren Schmidt else 490e1743d02SSøren Schmidt /* 49126f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 49226f9a767SRodney W. Grimes * arg and env vector sets 49326f9a767SRodney W. Grimes */ 494e1743d02SSøren Schmidt vectp = (char **) 495e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 49626f9a767SRodney W. Grimes 49726f9a767SRodney W. Grimes /* 49826f9a767SRodney W. Grimes * vectp also becomes our initial stack base 49926f9a767SRodney W. Grimes */ 50026f9a767SRodney W. Grimes stack_base = (int *)vectp; 50126f9a767SRodney W. Grimes 502c52007c2SDavid Greenman stringp = imgp->stringbase; 503c52007c2SDavid Greenman argc = imgp->argc; 504c52007c2SDavid Greenman envc = imgp->envc; 50526f9a767SRodney W. Grimes 50693f6448cSDavid Greenman /* 5073aed948bSDavid Greenman * Copy out strings - arguments and environment. 50893f6448cSDavid Greenman */ 509c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 51093f6448cSDavid Greenman 51193f6448cSDavid Greenman /* 5123aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 5133aed948bSDavid Greenman */ 5141ed012f9SPeter Wemm suword(&arginfo->ps_argvstr, (int)vectp); 5153aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 5163aed948bSDavid Greenman 5173aed948bSDavid Greenman /* 5183aed948bSDavid Greenman * Fill in argument portion of vector table. 51993f6448cSDavid Greenman */ 52026f9a767SRodney W. Grimes for (; argc > 0; --argc) { 5213aed948bSDavid Greenman suword(vectp++, (int)destp); 5223aed948bSDavid Greenman while (*stringp++ != 0) 5233aed948bSDavid Greenman destp++; 5243aed948bSDavid Greenman destp++; 52526f9a767SRodney W. Grimes } 52626f9a767SRodney W. Grimes 52726f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 5283aed948bSDavid Greenman suword(vectp++, NULL); 52926f9a767SRodney W. Grimes 5301ed012f9SPeter Wemm suword(&arginfo->ps_envstr, (int)vectp); 5313aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 53293f6448cSDavid Greenman 53393f6448cSDavid Greenman /* 5343aed948bSDavid Greenman * Fill in environment portion of vector table. 53593f6448cSDavid Greenman */ 53626f9a767SRodney W. Grimes for (; envc > 0; --envc) { 5373aed948bSDavid Greenman suword(vectp++, (int)destp); 5383aed948bSDavid Greenman while (*stringp++ != 0) 5393aed948bSDavid Greenman destp++; 5403aed948bSDavid Greenman destp++; 54126f9a767SRodney W. Grimes } 54226f9a767SRodney W. Grimes 54326f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 5443aed948bSDavid Greenman suword(vectp, NULL); 54526f9a767SRodney W. Grimes 54626f9a767SRodney W. Grimes return (stack_base); 54726f9a767SRodney W. Grimes } 54826f9a767SRodney W. Grimes 54926f9a767SRodney W. Grimes /* 55026f9a767SRodney W. Grimes * Check permissions of file to execute. 55126f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 55226f9a767SRodney W. Grimes */ 553f23b4c91SGarrett Wollman static int 554c52007c2SDavid Greenman exec_check_permissions(imgp) 555c52007c2SDavid Greenman struct image_params *imgp; 55626f9a767SRodney W. Grimes { 557c52007c2SDavid Greenman struct proc *p = imgp->proc; 558c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 559c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 56026f9a767SRodney W. Grimes int error; 56126f9a767SRodney W. Grimes 56226f9a767SRodney W. Grimes /* 56326f9a767SRodney W. Grimes * Check number of open-for-writes on the file and deny execution 56426f9a767SRodney W. Grimes * if there are any. 56526f9a767SRodney W. Grimes */ 566c52007c2SDavid Greenman if (vp->v_writecount) { 56726f9a767SRodney W. Grimes return (ETXTBSY); 56826f9a767SRodney W. Grimes } 56926f9a767SRodney W. Grimes 57026f9a767SRodney W. Grimes /* Get file attributes */ 571c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 57226f9a767SRodney W. Grimes if (error) 57326f9a767SRodney W. Grimes return (error); 57426f9a767SRodney W. Grimes 57526f9a767SRodney W. Grimes /* 57626f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 57726f9a767SRodney W. Grimes * file resides on. 57826f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 57926f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 58026f9a767SRodney W. Grimes * file really is executable. 58126f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 58226f9a767SRodney W. Grimes */ 583c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 58426f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 58526f9a767SRodney W. Grimes (attr->va_type != VREG)) { 58626f9a767SRodney W. Grimes return (EACCES); 58726f9a767SRodney W. Grimes } 58826f9a767SRodney W. Grimes 58926f9a767SRodney W. Grimes /* 59026f9a767SRodney W. Grimes * Zero length files can't be exec'd 59126f9a767SRodney W. Grimes */ 59226f9a767SRodney W. Grimes if (attr->va_size == 0) 59326f9a767SRodney W. Grimes return (ENOEXEC); 59426f9a767SRodney W. Grimes 59526f9a767SRodney W. Grimes /* 59626f9a767SRodney W. Grimes * Disable setuid/setgid if the filesystem prohibits it or if 59726f9a767SRodney W. Grimes * the process is being traced. 59826f9a767SRodney W. Grimes */ 599c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) 60026f9a767SRodney W. Grimes attr->va_mode &= ~(VSUID | VSGID); 60126f9a767SRodney W. Grimes 60226f9a767SRodney W. Grimes /* 60326f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 60426f9a767SRodney W. Grimes * Then call filesystem specific open routine (which does nothing 60526f9a767SRodney W. Grimes * in the general case). 60626f9a767SRodney W. Grimes */ 607c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 60826f9a767SRodney W. Grimes if (error) 60926f9a767SRodney W. Grimes return (error); 61026f9a767SRodney W. Grimes 611c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 61226f9a767SRodney W. Grimes if (error) 61326f9a767SRodney W. Grimes return (error); 61426f9a767SRodney W. Grimes 61526f9a767SRodney W. Grimes return (0); 616df8bae1dSRodney W. Grimes } 617