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 68654f6be1SBruce Evans static register_t *exec_copyout_strings __P((struct image_params *)); 69df8bae1dSRodney W. Grimes 709701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 719701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS; 729701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 73486bddb0SDoug Rabson 749701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 759701cd40SJohn Baldwin static u_long usrstack = USRSTACK; 769701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 7799ac3bc8SPeter Wemm 78b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 79c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 809701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 81b9df5231SPoul-Henning Kamp 82a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 83a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 84a8704f89SPoul-Henning Kamp 8599ac3bc8SPeter Wemm /* 86aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 87aa855a59SPeter Wemm * double pointer here. 88df8bae1dSRodney W. Grimes */ 89aa855a59SPeter Wemm static const struct execsw **execsw; 9026f9a767SRodney W. Grimes 91d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 92ad7507e2SSteven Wallace struct execve_args { 93ad7507e2SSteven Wallace char *fname; 94ad7507e2SSteven Wallace char **argv; 95ad7507e2SSteven Wallace char **envv; 96ad7507e2SSteven Wallace }; 97d2d3e875SBruce Evans #endif 98ad7507e2SSteven Wallace 9926f9a767SRodney W. Grimes /* 10026f9a767SRodney W. Grimes * execve() system call. 101116734c4SMatthew Dillon * 102116734c4SMatthew Dillon * MPSAFE 10326f9a767SRodney W. Grimes */ 10426f9a767SRodney W. Grimes int 105b40ce416SJulian Elischer execve(td, uap) 106b40ce416SJulian Elischer struct thread *td; 10726f9a767SRodney W. Grimes register struct execve_args *uap; 108df8bae1dSRodney W. Grimes { 109b40ce416SJulian Elischer struct proc *p = td->td_proc; 11026f9a767SRodney W. Grimes struct nameidata nd, *ndp; 111b1fc0ec1SRobert Watson struct ucred *newcred, *oldcred; 112654f6be1SBruce Evans register_t *stack_base; 113bb56ec4aSPoul-Henning Kamp int error, len, i; 114c52007c2SDavid Greenman struct image_params image_params, *imgp; 11526f9a767SRodney W. Grimes struct vattr attr; 116d323ddf3SMatthew Dillon int (*img_first) __P((struct image_params *)); 117fbd26f75SJohn Baldwin struct pargs *pa; 11826f9a767SRodney W. Grimes 119c52007c2SDavid Greenman imgp = &image_params; 120df8bae1dSRodney W. Grimes 1219ca45e81SDag-Erling Smørgrav /* 1229ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1239ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1249ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1259ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1269ca45e81SDag-Erling Smørgrav * privileges. 1279ca45e81SDag-Erling Smørgrav */ 1289ca45e81SDag-Erling Smørgrav mtx_lock(&Giant); 1299ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1309ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 1319ca45e81SDag-Erling Smørgrav (__FUNCTION__ "(): process already has P_INEXEC flag")); 1329ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 1339ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 1349ca45e81SDag-Erling Smørgrav 135b40ce416SJulian Elischer /* XXXKSE */ 136b40ce416SJulian Elischer /* !!!!!!!! we need abort all the other threads of this process before we */ 137b40ce416SJulian Elischer /* proceed beyond his point! */ 138b40ce416SJulian Elischer 139df8bae1dSRodney W. Grimes /* 140c52007c2SDavid Greenman * Initialize part of the common data 141df8bae1dSRodney W. Grimes */ 142c52007c2SDavid Greenman imgp->proc = p; 143c52007c2SDavid Greenman imgp->uap = uap; 144c52007c2SDavid Greenman imgp->attr = &attr; 145c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1465cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 147c52007c2SDavid Greenman imgp->entry_addr = 0; 148c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 149c52007c2SDavid Greenman imgp->interpreted = 0; 150c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 151e1743d02SSøren Schmidt imgp->auxargs = NULL; 1521616db3cSJohn Dyson imgp->vp = NULL; 1531616db3cSJohn Dyson imgp->firstpage = NULL; 1544fe88fe6SJohn Polstra imgp->ps_strings = 0; 155b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 15626f9a767SRodney W. Grimes 15726f9a767SRodney W. Grimes /* 15826f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 15926f9a767SRodney W. Grimes * environment strings 16026f9a767SRodney W. Grimes */ 1611616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 162c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 16326f9a767SRodney W. Grimes error = ENOMEM; 16426f9a767SRodney W. Grimes goto exec_fail; 16526f9a767SRodney W. Grimes } 166c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 167c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1681616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 16926f9a767SRodney W. Grimes 17026f9a767SRodney W. Grimes /* 17126f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 17226f9a767SRodney W. Grimes * in ni_vp amoung other things. 17326f9a767SRodney W. Grimes */ 17426f9a767SRodney W. Grimes ndp = &nd; 175b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 176b40ce416SJulian Elischer UIO_USERSPACE, uap->fname, td); 17726f9a767SRodney W. Grimes 17826f9a767SRodney W. Grimes interpret: 17926f9a767SRodney W. Grimes 18026f9a767SRodney W. Grimes error = namei(ndp); 18126f9a767SRodney W. Grimes if (error) { 1821616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1831616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 18426f9a767SRodney W. Grimes goto exec_fail; 18526f9a767SRodney W. Grimes } 18626f9a767SRodney W. Grimes 187c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 1889c0fed3dSDoug Rabson imgp->fname = uap->fname; 18926f9a767SRodney W. Grimes 19026f9a767SRodney W. Grimes /* 1919022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1929022e4adSDavid Greenman */ 193c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1948677f509SDavid Greenman if (error) { 195b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 19626f9a767SRodney W. Grimes goto exec_fail_dealloc; 1978677f509SDavid Greenman } 19826f9a767SRodney W. Grimes 1991616db3cSJohn Dyson error = exec_map_first_page(imgp); 200b40ce416SJulian Elischer VOP_UNLOCK(imgp->vp, 0, td); 2016d5a0a8cSDavid Greenman if (error) 20226f9a767SRodney W. Grimes goto exec_fail_dealloc; 20326f9a767SRodney W. Grimes 20426f9a767SRodney W. Grimes /* 205d323ddf3SMatthew Dillon * If the current process has a special image activator it 206d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 207d323ddf3SMatthew Dillon * scripts differently. 20826f9a767SRodney W. Grimes */ 209d323ddf3SMatthew Dillon error = -1; 210d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 211d323ddf3SMatthew Dillon error = img_first(imgp); 212d323ddf3SMatthew Dillon 213d323ddf3SMatthew Dillon /* 214d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 215d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 216d323ddf3SMatthew Dillon * and an error otherwise. 217d323ddf3SMatthew Dillon */ 218d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 219d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 220d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 221d323ddf3SMatthew Dillon continue; 222d323ddf3SMatthew Dillon } 223c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 224d323ddf3SMatthew Dillon } 225d323ddf3SMatthew Dillon 226d323ddf3SMatthew Dillon if (error) { 22726f9a767SRodney W. Grimes if (error == -1) 228d323ddf3SMatthew Dillon error = ENOEXEC; 22926f9a767SRodney W. Grimes goto exec_fail_dealloc; 230d323ddf3SMatthew Dillon } 231d323ddf3SMatthew Dillon 232d323ddf3SMatthew Dillon /* 233d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 234d323ddf3SMatthew Dillon * activate the interpreter. 235d323ddf3SMatthew Dillon */ 236c52007c2SDavid Greenman if (imgp->interpreted) { 2371616db3cSJohn Dyson exec_unmap_first_page(imgp); 238762e6b85SEivind Eklund /* free name buffer and old vnode */ 239762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 240f5277ae7SDavid Greenman vrele(ndp->ni_vp); 24126f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 242b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 243b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 24426f9a767SRodney W. Grimes goto interpret; 24526f9a767SRodney W. Grimes } 24626f9a767SRodney W. Grimes 24726f9a767SRodney W. Grimes /* 24826f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 24926f9a767SRodney W. Grimes */ 250c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 25126f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 25226f9a767SRodney W. Grimes 25326f9a767SRodney W. Grimes /* 2541e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2551e1e0b44SSøren Schmidt * let it do the stack setup. 2561e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 25726f9a767SRodney W. Grimes */ 2581e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 259c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2601e1e0b44SSøren Schmidt else 261c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 26226f9a767SRodney W. Grimes 263a78e8d2aSDavid Greenman /* 264a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 265a78e8d2aSDavid Greenman * be shared after an exec. 266a78e8d2aSDavid Greenman */ 267a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 268a78e8d2aSDavid Greenman struct filedesc *tmp; 269a78e8d2aSDavid Greenman 270b40ce416SJulian Elischer tmp = fdcopy(td); 271b40ce416SJulian Elischer fdfree(td); 272a78e8d2aSDavid Greenman p->p_fd = tmp; 273a78e8d2aSDavid Greenman } 274a78e8d2aSDavid Greenman 275333ea485SGuido van Rooij /* 276333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 2778688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 278b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 279333ea485SGuido van Rooij * reset. 280333ea485SGuido van Rooij */ 281333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 282333ea485SGuido van Rooij struct procsig *newprocsig; 283333ea485SGuido van Rooij 284333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 285333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 286333ea485SGuido van Rooij bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig)); 287333ea485SGuido van Rooij p->p_procsig->ps_refcnt--; 288333ea485SGuido van Rooij p->p_procsig = newprocsig; 289333ea485SGuido van Rooij p->p_procsig->ps_refcnt = 1; 290b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 291b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 292333ea485SGuido van Rooij 293b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 294b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 295333ea485SGuido van Rooij } 296fdf4e8b3SWarner Losh /* Stop profiling */ 297fdf4e8b3SWarner Losh stopprofclock(p); 298fdf4e8b3SWarner Losh 29926f9a767SRodney W. Grimes /* close files on exec */ 300b40ce416SJulian Elischer fdcloseexec(td); 30126f9a767SRodney W. Grimes 30226f9a767SRodney W. Grimes /* reset caught signals */ 30326f9a767SRodney W. Grimes execsigs(p); 30426f9a767SRodney W. Grimes 30526f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 30626f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 30726f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 30826f9a767SRodney W. Grimes p->p_comm[len] = 0; 30926f9a767SRodney W. Grimes 31026f9a767SRodney W. Grimes /* 31124b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 312dc733423SDag-Erling Smørgrav * it that it now has its own resources back 31326f9a767SRodney W. Grimes */ 314e65897c3SJohn Baldwin PROC_LOCK(p); 31526f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 31626f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 31726f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 31826f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 31926f9a767SRodney W. Grimes } 32026f9a767SRodney W. Grimes 32126f9a767SRodney W. Grimes /* 322a78e8d2aSDavid Greenman * Implement image setuid/setgid. 323a78e8d2aSDavid Greenman * 324a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 325a78e8d2aSDavid Greenman * the process is being traced. 326c52007c2SDavid Greenman */ 327b1fc0ec1SRobert Watson oldcred = p->p_ucred; 328b1fc0ec1SRobert Watson newcred = NULL; 329b1fc0ec1SRobert Watson if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) || 330b1fc0ec1SRobert Watson ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) && 331a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 332c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 333e65897c3SJohn Baldwin PROC_UNLOCK(p); 334c52007c2SDavid Greenman /* 335c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 336b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 337b85db196SPeter Wemm * we do not regain any tracing during a possible block. 33826f9a767SRodney W. Grimes */ 339b85db196SPeter Wemm setsugid(p); 340b1fc0ec1SRobert Watson if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) { 34179deba82SMatthew Dillon struct vnode *vtmp; 34279deba82SMatthew Dillon 34379deba82SMatthew Dillon if ((vtmp = p->p_tracep) != NULL) { 344c52007c2SDavid Greenman p->p_tracep = NULL; 34579deba82SMatthew Dillon p->p_traceflag = 0; 34679deba82SMatthew Dillon vrele(vtmp); 34779deba82SMatthew Dillon } 34826f9a767SRodney W. Grimes } 349c52007c2SDavid Greenman /* 350c52007c2SDavid Greenman * Set the new credentials. 351c52007c2SDavid Greenman */ 352b1fc0ec1SRobert Watson newcred = crdup(oldcred); 353c52007c2SDavid Greenman if (attr.va_mode & VSUID) 354b1fc0ec1SRobert Watson change_euid(newcred, attr.va_uid); 355c52007c2SDavid Greenman if (attr.va_mode & VSGID) 356b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 357b40ce416SJulian Elischer setugidsafety(td); 358c52007c2SDavid Greenman } else { 359b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 360b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 361c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 362e65897c3SJohn Baldwin PROC_UNLOCK(p); 36326f9a767SRodney W. Grimes } 36426f9a767SRodney W. Grimes 36526f9a767SRodney W. Grimes /* 366c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 367b1fc0ec1SRobert Watson * 368b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 369b8c526dfSAlexander Langer * POSIX-compliant. A number of sources indicate that the saved 370b1fc0ec1SRobert Watson * uid/gid should only be updated if the new ruid is not equal to 371b1fc0ec1SRobert Watson * the old ruid, or the new euid is not equal to the old euid and 372b1fc0ec1SRobert Watson * the new euid is not equal to the old ruid. The FreeBSD code 373b1fc0ec1SRobert Watson * always updates the saved uid/gid. Also, this code uses the new 374b1fc0ec1SRobert Watson * (replaced) euid and egid as the source, which may or may not be 375b1fc0ec1SRobert Watson * the right ones to use. 37626f9a767SRodney W. Grimes */ 3777cb8e4d2SRobert Watson if (newcred == NULL) { 378b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 379b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 380b1fc0ec1SRobert Watson newcred = crdup(oldcred); 381b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 382b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 383b1fc0ec1SRobert Watson } 3847cb8e4d2SRobert Watson } else { 3857cb8e4d2SRobert Watson change_svuid(newcred, newcred->cr_uid); 3867cb8e4d2SRobert Watson change_svgid(newcred, newcred->cr_gid); 3877cb8e4d2SRobert Watson } 3887cb8e4d2SRobert Watson 389b1fc0ec1SRobert Watson if (newcred != NULL) { 390b1fc0ec1SRobert Watson PROC_LOCK(p); 391b1fc0ec1SRobert Watson p->p_ucred = newcred; 392b1fc0ec1SRobert Watson PROC_UNLOCK(p); 393b1fc0ec1SRobert Watson crfree(oldcred); 394b1fc0ec1SRobert Watson } 39526f9a767SRodney W. Grimes 39626f9a767SRodney W. Grimes /* 3972a531c80SDavid Greenman * Store the vp for use in procfs 3982a531c80SDavid Greenman */ 3992a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 4002a531c80SDavid Greenman vrele(p->p_textvp); 4012a531c80SDavid Greenman VREF(ndp->ni_vp); 4022a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 4032a531c80SDavid Greenman 4042a531c80SDavid Greenman /* 4059ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 4069ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 407cb679c38SJonathan Lemon */ 408e65897c3SJohn Baldwin PROC_LOCK(p); 409cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 4109ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 411cb679c38SJonathan Lemon 412cb679c38SJonathan Lemon /* 41326f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 41426f9a767SRodney W. Grimes * can be set before the program executes. 41526f9a767SRodney W. Grimes */ 416e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 4172a024a2bSSean Eric Fagan 41826f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 41926f9a767SRodney W. Grimes psignal(p, SIGTRAP); 42026f9a767SRodney W. Grimes 42126f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 42226f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 42326f9a767SRodney W. Grimes 424b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 425fbd26f75SJohn Baldwin pa = p->p_args; 426b9df5231SPoul-Henning Kamp p->p_args = NULL; 427fbd26f75SJohn Baldwin PROC_UNLOCK(p); 428fbd26f75SJohn Baldwin if (pa != NULL && --pa->ar_ref == 0) 429fbd26f75SJohn Baldwin FREE(pa, M_PARGS); 430b9df5231SPoul-Henning Kamp 431e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 432e913ca22SDoug Rabson setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 433e913ca22SDoug Rabson imgp->ps_strings); 434e913ca22SDoug Rabson 435b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 436b9df5231SPoul-Henning Kamp i = imgp->endargs - imgp->stringbase; 437b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 438fbd26f75SJohn Baldwin MALLOC(pa, struct pargs *, sizeof(struct pargs) + i, 439b9df5231SPoul-Henning Kamp M_PARGS, M_WAITOK); 440fbd26f75SJohn Baldwin pa->ar_ref = 1; 441fbd26f75SJohn Baldwin pa->ar_length = i; 442fbd26f75SJohn Baldwin bcopy(imgp->stringbase, pa->ar_args, i); 443e65897c3SJohn Baldwin PROC_LOCK(p); 444fbd26f75SJohn Baldwin p->p_args = pa; 445e65897c3SJohn Baldwin PROC_UNLOCK(p); 446fbd26f75SJohn Baldwin } 447b9df5231SPoul-Henning Kamp 4481616db3cSJohn Dyson exec_fail_dealloc: 4491616db3cSJohn Dyson 45026f9a767SRodney W. Grimes /* 45126f9a767SRodney W. Grimes * free various allocated resources 45226f9a767SRodney W. Grimes */ 4531616db3cSJohn Dyson if (imgp->firstpage) 4541616db3cSJohn Dyson exec_unmap_first_page(imgp); 45526f9a767SRodney W. Grimes 456c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 4571616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 4581616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 4591616db3cSJohn Dyson 4609c0fed3dSDoug Rabson if (imgp->vp) { 461762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 4629c0fed3dSDoug Rabson vrele(imgp->vp); 463a3cf6ebaSDavid Greenman } 46426f9a767SRodney W. Grimes 4651616db3cSJohn Dyson if (error == 0) 466116734c4SMatthew Dillon goto done2; 4671616db3cSJohn Dyson 46826f9a767SRodney W. Grimes exec_fail: 4699ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 4709ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 4719ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 4729ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 4739ca45e81SDag-Erling Smørgrav 474c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 47526f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 476b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 47726f9a767SRodney W. Grimes /* NOT REACHED */ 478116734c4SMatthew Dillon error = 0; 47926f9a767SRodney W. Grimes } 480116734c4SMatthew Dillon done2: 481116734c4SMatthew Dillon mtx_unlock(&Giant); 482116734c4SMatthew Dillon return (error); 48326f9a767SRodney W. Grimes } 48426f9a767SRodney W. Grimes 4851616db3cSJohn Dyson int 4861616db3cSJohn Dyson exec_map_first_page(imgp) 4871616db3cSJohn Dyson struct image_params *imgp; 4881616db3cSJohn Dyson { 489d8aad40cSJohn Baldwin int rv, i; 49095461b45SJohn Dyson int initial_pagein; 49195461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 4921616db3cSJohn Dyson vm_object_t object; 4931616db3cSJohn Dyson 4940cddd8f0SMatthew Dillon GIANT_REQUIRED; 4951616db3cSJohn Dyson 4961616db3cSJohn Dyson if (imgp->firstpage) { 4971616db3cSJohn Dyson exec_unmap_first_page(imgp); 4981616db3cSJohn Dyson } 4991616db3cSJohn Dyson 5009ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 5011616db3cSJohn Dyson 50295461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 5031616db3cSJohn Dyson 50495461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 50595461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 50695461b45SJohn Dyson if (initial_pagein > object->size) 50795461b45SJohn Dyson initial_pagein = object->size; 50895461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 509d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 51095461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 51195461b45SJohn Dyson break; 51295461b45SJohn Dyson if (ma[i]->valid) 51395461b45SJohn Dyson break; 514e69763a3SDoug Rabson vm_page_busy(ma[i]); 51595461b45SJohn Dyson } else { 51695461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 51795461b45SJohn Dyson if (ma[i] == NULL) 51895461b45SJohn Dyson break; 51995461b45SJohn Dyson } 52095461b45SJohn Dyson } 52195461b45SJohn Dyson initial_pagein = i; 5221616db3cSJohn Dyson 52395461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 52495461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 52595461b45SJohn Dyson 526eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 527ecbb00a2SDoug Rabson if (ma[0]) { 52895461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 5298f9110f6SJohn Dyson vm_page_free(ma[0]); 530ecbb00a2SDoug Rabson } 5311616db3cSJohn Dyson return EIO; 5321616db3cSJohn Dyson } 5331616db3cSJohn Dyson } 5341616db3cSJohn Dyson 53595461b45SJohn Dyson vm_page_wire(ma[0]); 536e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 5371616db3cSJohn Dyson 53895461b45SJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 53995461b45SJohn Dyson imgp->firstpage = ma[0]; 5401616db3cSJohn Dyson 5411616db3cSJohn Dyson return 0; 5421616db3cSJohn Dyson } 5431616db3cSJohn Dyson 5441616db3cSJohn Dyson void 5451616db3cSJohn Dyson exec_unmap_first_page(imgp) 5461616db3cSJohn Dyson struct image_params *imgp; 5471616db3cSJohn Dyson { 5480cddd8f0SMatthew Dillon GIANT_REQUIRED; 54923955314SAlfred Perlstein 5501616db3cSJohn Dyson if (imgp->firstpage) { 5511616db3cSJohn Dyson pmap_kremove((vm_offset_t) imgp->image_header); 55273007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 5531616db3cSJohn Dyson imgp->firstpage = NULL; 5541616db3cSJohn Dyson } 5551616db3cSJohn Dyson } 5561616db3cSJohn Dyson 55726f9a767SRodney W. Grimes /* 55826f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 55926f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 56026f9a767SRodney W. Grimes * automatically in trap.c. 56126f9a767SRodney W. Grimes */ 56226f9a767SRodney W. Grimes int 563c52007c2SDavid Greenman exec_new_vmspace(imgp) 564c52007c2SDavid Greenman struct image_params *imgp; 56526f9a767SRodney W. Grimes { 56626f9a767SRodney W. Grimes int error; 567c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 5689a024fc5SRobert Drehmel vm_offset_t stack_addr = USRSTACK - maxssiz; 569af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 57026f9a767SRodney W. Grimes 5710cddd8f0SMatthew Dillon GIANT_REQUIRED; 5720cddd8f0SMatthew Dillon 573c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 57426f9a767SRodney W. Grimes 575af9ec885SJohn Dyson /* 576af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 577af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 578af9ec885SJohn Dyson * not disrupted 579af9ec885SJohn Dyson */ 580af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 5813d903220SDoug Rabson if (vmspace->vm_shm) 582c52007c2SDavid Greenman shmexit(imgp->proc); 583b1028ad1SLuoqi Chen pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 5849c0fed3dSDoug Rabson vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 585af9ec885SJohn Dyson } else { 586492da96cSJohn Dyson vmspace_exec(imgp->proc); 587492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 588af9ec885SJohn Dyson map = &vmspace->vm_map; 589af9ec885SJohn Dyson } 59026f9a767SRodney W. Grimes 59126f9a767SRodney W. Grimes /* Allocate a new stack */ 5929a024fc5SRobert Drehmel error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz, 5939a024fc5SRobert Drehmel VM_PROT_ALL, VM_PROT_ALL, 0); 5942267af78SJulian Elischer if (error) 5952267af78SJulian Elischer return (error); 5962267af78SJulian Elischer 59763c47a5cSDoug Rabson #ifdef __ia64__ 59863c47a5cSDoug Rabson { 59963c47a5cSDoug Rabson /* 60063c47a5cSDoug Rabson * Allocate backing store. We really need something 60163c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 60263c47a5cSDoug Rabson * store to grow upwards. This will do for now. 60363c47a5cSDoug Rabson */ 60463c47a5cSDoug Rabson vm_offset_t bsaddr; 605cbc89bfbSPaul Saab bsaddr = USRSTACK - 2*maxssiz; 60663c47a5cSDoug Rabson error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 60763c47a5cSDoug Rabson 4*PAGE_SIZE, 0, 60863c47a5cSDoug Rabson VM_PROT_ALL, VM_PROT_ALL, 0); 609b40ce416SJulian Elischer imgp->proc->p_thread.td_md.md_bspstore = bsaddr; 61063c47a5cSDoug Rabson } 61163c47a5cSDoug Rabson #endif 61263c47a5cSDoug Rabson 6132267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 6142267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 6152267af78SJulian Elischer * process stack so we can check the stack rlimit. 6162267af78SJulian Elischer */ 617cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 618cbc89bfbSPaul Saab vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz; 61926f9a767SRodney W. Grimes 62026f9a767SRodney W. Grimes return(0); 62126f9a767SRodney W. Grimes } 62226f9a767SRodney W. Grimes 62326f9a767SRodney W. Grimes /* 62426f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 62526f9a767SRodney W. Grimes * address space into the temporary string buffer. 62626f9a767SRodney W. Grimes */ 62726f9a767SRodney W. Grimes int 628c52007c2SDavid Greenman exec_extract_strings(imgp) 629c52007c2SDavid Greenman struct image_params *imgp; 63026f9a767SRodney W. Grimes { 63126f9a767SRodney W. Grimes char **argv, **envv; 63226f9a767SRodney W. Grimes char *argp, *envp; 633ecbb00a2SDoug Rabson int error; 634ecbb00a2SDoug Rabson size_t length; 63526f9a767SRodney W. Grimes 63626f9a767SRodney W. Grimes /* 63726f9a767SRodney W. Grimes * extract arguments first 63826f9a767SRodney W. Grimes */ 63926f9a767SRodney W. Grimes 640c52007c2SDavid Greenman argv = imgp->uap->argv; 64126f9a767SRodney W. Grimes 6420fd1a014SDavid Greenman if (argv) { 643aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 6445cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 6455cf3d12cSAndrey A. Chernov return (EFAULT); 6465cf3d12cSAndrey A. Chernov if (argp) 6475cf3d12cSAndrey A. Chernov argv++; 6485cf3d12cSAndrey A. Chernov if (imgp->argv0) 6495cf3d12cSAndrey A. Chernov argp = imgp->argv0; 6505cf3d12cSAndrey A. Chernov if (argp) { 6515cf3d12cSAndrey A. Chernov do { 65226f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 65326f9a767SRodney W. Grimes return (EFAULT); 654c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 655c52007c2SDavid Greenman imgp->stringspace, &length))) { 6560fd1a014SDavid Greenman if (error == ENAMETOOLONG) 65726f9a767SRodney W. Grimes return(E2BIG); 6580fd1a014SDavid Greenman return (error); 6590fd1a014SDavid Greenman } 660c52007c2SDavid Greenman imgp->stringspace -= length; 661c52007c2SDavid Greenman imgp->stringp += length; 662c52007c2SDavid Greenman imgp->argc++; 663aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 66426f9a767SRodney W. Grimes } 6650fd1a014SDavid Greenman } 66626f9a767SRodney W. Grimes 667b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 668b9df5231SPoul-Henning Kamp 66926f9a767SRodney W. Grimes /* 67026f9a767SRodney W. Grimes * extract environment strings 67126f9a767SRodney W. Grimes */ 67226f9a767SRodney W. Grimes 673c52007c2SDavid Greenman envv = imgp->uap->envv; 67426f9a767SRodney W. Grimes 6750fd1a014SDavid Greenman if (envv) { 676aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 67726f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 67826f9a767SRodney W. Grimes return (EFAULT); 679c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 680c52007c2SDavid Greenman imgp->stringspace, &length))) { 6810fd1a014SDavid Greenman if (error == ENAMETOOLONG) 68226f9a767SRodney W. Grimes return(E2BIG); 6830fd1a014SDavid Greenman return (error); 6840fd1a014SDavid Greenman } 685c52007c2SDavid Greenman imgp->stringspace -= length; 686c52007c2SDavid Greenman imgp->stringp += length; 687c52007c2SDavid Greenman imgp->envc++; 68826f9a767SRodney W. Grimes } 6890fd1a014SDavid Greenman } 69026f9a767SRodney W. Grimes 69126f9a767SRodney W. Grimes return (0); 69226f9a767SRodney W. Grimes } 69326f9a767SRodney W. Grimes 69426f9a767SRodney W. Grimes /* 69526f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 69626f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 69726f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 69826f9a767SRodney W. Grimes */ 699654f6be1SBruce Evans register_t * 700c52007c2SDavid Greenman exec_copyout_strings(imgp) 701c52007c2SDavid Greenman struct image_params *imgp; 70226f9a767SRodney W. Grimes { 70326f9a767SRodney W. Grimes int argc, envc; 70426f9a767SRodney W. Grimes char **vectp; 70526f9a767SRodney W. Grimes char *stringp, *destp; 706654f6be1SBruce Evans register_t *stack_base; 70793f6448cSDavid Greenman struct ps_strings *arginfo; 708d66a5066SPeter Wemm int szsigcode; 70926f9a767SRodney W. Grimes 71026f9a767SRodney W. Grimes /* 71126f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 712d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 71326f9a767SRodney W. Grimes */ 7144c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 715d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 716d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 7171ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 7181ed012f9SPeter Wemm 71926f9a767SRodney W. Grimes /* 720d66a5066SPeter Wemm * install sigcode 721d66a5066SPeter Wemm */ 722d66a5066SPeter Wemm if (szsigcode) 723d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 724d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 725d66a5066SPeter Wemm 726d66a5066SPeter Wemm /* 727e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 728e1743d02SSøren Schmidt * on the stack. 729e1743d02SSøren Schmidt */ 730b9a22da4STakanori Watanabe if (imgp->auxargs) { 731e1743d02SSøren Schmidt /* 732b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 733b9a22da4STakanori Watanabe * lower compatibility. 734b9a22da4STakanori Watanabe */ 735b9a22da4STakanori Watanabe imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 736b9a22da4STakanori Watanabe : (AT_COUNT * 2); 737b9a22da4STakanori Watanabe /* 738b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 739b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 740b9a22da4STakanori Watanabe * for argument of Runtime loader. 741e1743d02SSøren Schmidt */ 742e1743d02SSøren Schmidt vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 743b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 744b9a22da4STakanori Watanabe 745b9a22da4STakanori Watanabe } else 746e1743d02SSøren Schmidt /* 747b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 748b9a22da4STakanori Watanabe * the arg and env vector sets 74926f9a767SRodney W. Grimes */ 750e1743d02SSøren Schmidt vectp = (char **) 751e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 75226f9a767SRodney W. Grimes 75326f9a767SRodney W. Grimes /* 75426f9a767SRodney W. Grimes * vectp also becomes our initial stack base 75526f9a767SRodney W. Grimes */ 756654f6be1SBruce Evans stack_base = (register_t *)vectp; 75726f9a767SRodney W. Grimes 758c52007c2SDavid Greenman stringp = imgp->stringbase; 759c52007c2SDavid Greenman argc = imgp->argc; 760c52007c2SDavid Greenman envc = imgp->envc; 76126f9a767SRodney W. Grimes 76293f6448cSDavid Greenman /* 7633aed948bSDavid Greenman * Copy out strings - arguments and environment. 76493f6448cSDavid Greenman */ 765c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 76693f6448cSDavid Greenman 76793f6448cSDavid Greenman /* 7683aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 7693aed948bSDavid Greenman */ 770aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 7713aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 7723aed948bSDavid Greenman 7733aed948bSDavid Greenman /* 7743aed948bSDavid Greenman * Fill in argument portion of vector table. 77593f6448cSDavid Greenman */ 77626f9a767SRodney W. Grimes for (; argc > 0; --argc) { 777aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 7783aed948bSDavid Greenman while (*stringp++ != 0) 7793aed948bSDavid Greenman destp++; 7803aed948bSDavid Greenman destp++; 78126f9a767SRodney W. Grimes } 78226f9a767SRodney W. Grimes 7831a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 7846ab46d52SBruce Evans suword(vectp++, 0); 78526f9a767SRodney W. Grimes 786aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 7873aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 78893f6448cSDavid Greenman 78993f6448cSDavid Greenman /* 7903aed948bSDavid Greenman * Fill in environment portion of vector table. 79193f6448cSDavid Greenman */ 79226f9a767SRodney W. Grimes for (; envc > 0; --envc) { 793aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 7943aed948bSDavid Greenman while (*stringp++ != 0) 7953aed948bSDavid Greenman destp++; 7963aed948bSDavid Greenman destp++; 79726f9a767SRodney W. Grimes } 79826f9a767SRodney W. Grimes 79926f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 8006ab46d52SBruce Evans suword(vectp, 0); 80126f9a767SRodney W. Grimes 80226f9a767SRodney W. Grimes return (stack_base); 80326f9a767SRodney W. Grimes } 80426f9a767SRodney W. Grimes 80526f9a767SRodney W. Grimes /* 80626f9a767SRodney W. Grimes * Check permissions of file to execute. 807cf64863aSRobert Watson * Called with imgp->vp locked. 80826f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 80926f9a767SRodney W. Grimes */ 810c8a79999SPeter Wemm int 811c52007c2SDavid Greenman exec_check_permissions(imgp) 812c52007c2SDavid Greenman struct image_params *imgp; 81326f9a767SRodney W. Grimes { 814c52007c2SDavid Greenman struct proc *p = imgp->proc; 815c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 816c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 81726f9a767SRodney W. Grimes int error; 81826f9a767SRodney W. Grimes 81926f9a767SRodney W. Grimes /* Get file attributes */ 820b40ce416SJulian Elischer error = VOP_GETATTR(vp, attr, p->p_ucred, curthread); /* XXXKSE */ 82126f9a767SRodney W. Grimes if (error) 82226f9a767SRodney W. Grimes return (error); 82326f9a767SRodney W. Grimes 82426f9a767SRodney W. Grimes /* 82526f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 82626f9a767SRodney W. Grimes * file resides on. 82726f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 82826f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 82926f9a767SRodney W. Grimes * file really is executable. 83026f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 83126f9a767SRodney W. Grimes */ 832c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 83326f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 83426f9a767SRodney W. Grimes (attr->va_type != VREG)) { 83526f9a767SRodney W. Grimes return (EACCES); 83626f9a767SRodney W. Grimes } 83726f9a767SRodney W. Grimes 83826f9a767SRodney W. Grimes /* 83926f9a767SRodney W. Grimes * Zero length files can't be exec'd 84026f9a767SRodney W. Grimes */ 84126f9a767SRodney W. Grimes if (attr->va_size == 0) 84226f9a767SRodney W. Grimes return (ENOEXEC); 84326f9a767SRodney W. Grimes 84426f9a767SRodney W. Grimes /* 84526f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 84626f9a767SRodney W. Grimes */ 847b40ce416SJulian Elischer error = VOP_ACCESS(vp, VEXEC, p->p_ucred, curthread); /* XXXKSE */ 84826f9a767SRodney W. Grimes if (error) 84926f9a767SRodney W. Grimes return (error); 85026f9a767SRodney W. Grimes 8516d5a0a8cSDavid Greenman /* 8526d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 8536d5a0a8cSDavid Greenman * if there are any. 8546d5a0a8cSDavid Greenman */ 8556d5a0a8cSDavid Greenman if (vp->v_writecount) 8566d5a0a8cSDavid Greenman return (ETXTBSY); 8576d5a0a8cSDavid Greenman 8586d5a0a8cSDavid Greenman /* 8596d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 8606d5a0a8cSDavid Greenman * general case). 8616d5a0a8cSDavid Greenman */ 862b40ce416SJulian Elischer error = VOP_OPEN(vp, FREAD, p->p_ucred, curthread); /* XXXKSE */ 86326f9a767SRodney W. Grimes if (error) 86426f9a767SRodney W. Grimes return (error); 86526f9a767SRodney W. Grimes 86626f9a767SRodney W. Grimes return (0); 867df8bae1dSRodney W. Grimes } 868aa855a59SPeter Wemm 869aa855a59SPeter Wemm /* 870aa855a59SPeter Wemm * Exec handler registration 871aa855a59SPeter Wemm */ 872aa855a59SPeter Wemm int 873aa855a59SPeter Wemm exec_register(execsw_arg) 874aa855a59SPeter Wemm const struct execsw *execsw_arg; 875aa855a59SPeter Wemm { 876aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 877aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 878aa855a59SPeter Wemm 879aa855a59SPeter Wemm if (execsw) 880aa855a59SPeter Wemm for (es = execsw; *es; es++) 881aa855a59SPeter Wemm count++; 882aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 883aa855a59SPeter Wemm if (newexecsw == NULL) 884aa855a59SPeter Wemm return ENOMEM; 885aa855a59SPeter Wemm xs = newexecsw; 886aa855a59SPeter Wemm if (execsw) 887aa855a59SPeter Wemm for (es = execsw; *es; es++) 888aa855a59SPeter Wemm *xs++ = *es; 889aa855a59SPeter Wemm *xs++ = execsw_arg; 890aa855a59SPeter Wemm *xs = NULL; 891aa855a59SPeter Wemm if (execsw) 892aa855a59SPeter Wemm free(execsw, M_TEMP); 893aa855a59SPeter Wemm execsw = newexecsw; 894aa855a59SPeter Wemm return 0; 895aa855a59SPeter Wemm } 896aa855a59SPeter Wemm 897aa855a59SPeter Wemm int 898aa855a59SPeter Wemm exec_unregister(execsw_arg) 899aa855a59SPeter Wemm const struct execsw *execsw_arg; 900aa855a59SPeter Wemm { 901aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 902aa855a59SPeter Wemm int count = 1; 903aa855a59SPeter Wemm 904aa855a59SPeter Wemm if (execsw == NULL) 905aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 906aa855a59SPeter Wemm 907aa855a59SPeter Wemm for (es = execsw; *es; es++) { 908aa855a59SPeter Wemm if (*es == execsw_arg) 909aa855a59SPeter Wemm break; 910aa855a59SPeter Wemm } 911aa855a59SPeter Wemm if (*es == NULL) 912aa855a59SPeter Wemm return ENOENT; 913aa855a59SPeter Wemm for (es = execsw; *es; es++) 914aa855a59SPeter Wemm if (*es != execsw_arg) 915aa855a59SPeter Wemm count++; 916aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 917aa855a59SPeter Wemm if (newexecsw == NULL) 918aa855a59SPeter Wemm return ENOMEM; 919aa855a59SPeter Wemm xs = newexecsw; 920aa855a59SPeter Wemm for (es = execsw; *es; es++) 921aa855a59SPeter Wemm if (*es != execsw_arg) 922aa855a59SPeter Wemm *xs++ = *es; 923aa855a59SPeter Wemm *xs = NULL; 924aa855a59SPeter Wemm if (execsw) 925aa855a59SPeter Wemm free(execsw, M_TEMP); 926aa855a59SPeter Wemm execsw = newexecsw; 927aa855a59SPeter Wemm return 0; 928aa855a59SPeter Wemm } 929