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> 44a794e791SBruce Evans #include <sys/proc.h> 452a024a2bSSean Eric Fagan #include <sys/pioctl.h> 46aa855a59SPeter Wemm #include <sys/malloc.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> 51a794e791SBruce Evans #include <sys/vnode.h> 5226f9a767SRodney W. Grimes 5326f9a767SRodney W. Grimes #include <vm/vm.h> 54efeaf95aSDavid Greenman #include <vm/vm_param.h> 55efeaf95aSDavid Greenman #include <vm/pmap.h> 561616db3cSJohn Dyson #include <vm/vm_page.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> 611616db3cSJohn Dyson #include <vm/vm_pager.h> 6226f9a767SRodney W. Grimes 6326f9a767SRodney W. Grimes #include <machine/reg.h> 6426f9a767SRodney W. Grimes 65b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 66b9df5231SPoul-Henning Kamp 67654f6be1SBruce Evans static register_t *exec_copyout_strings __P((struct image_params *)); 68df8bae1dSRodney W. Grimes 699701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 709701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS; 719701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 72486bddb0SDoug Rabson 739701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 749701cd40SJohn Baldwin static u_long usrstack = USRSTACK; 759701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 7699ac3bc8SPeter Wemm 77b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 78b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 799701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 80b9df5231SPoul-Henning Kamp 81a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 82a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 83a8704f89SPoul-Henning Kamp 8499ac3bc8SPeter Wemm /* 85aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 86aa855a59SPeter Wemm * double pointer here. 87df8bae1dSRodney W. Grimes */ 88aa855a59SPeter Wemm static const struct execsw **execsw; 8926f9a767SRodney W. Grimes 90d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 91ad7507e2SSteven Wallace struct execve_args { 92ad7507e2SSteven Wallace char *fname; 93ad7507e2SSteven Wallace char **argv; 94ad7507e2SSteven Wallace char **envv; 95ad7507e2SSteven Wallace }; 96d2d3e875SBruce Evans #endif 97ad7507e2SSteven Wallace 9826f9a767SRodney W. Grimes /* 9926f9a767SRodney W. Grimes * execve() system call. 10026f9a767SRodney W. Grimes */ 10126f9a767SRodney W. Grimes int 102cb226aaaSPoul-Henning Kamp execve(p, uap) 10326f9a767SRodney W. Grimes struct proc *p; 10426f9a767SRodney W. Grimes register struct execve_args *uap; 105df8bae1dSRodney W. Grimes { 10626f9a767SRodney W. Grimes struct nameidata nd, *ndp; 107b1fc0ec1SRobert Watson struct ucred *newcred, *oldcred; 108654f6be1SBruce Evans register_t *stack_base; 109bb56ec4aSPoul-Henning Kamp int error, len, i; 110c52007c2SDavid Greenman struct image_params image_params, *imgp; 11126f9a767SRodney W. Grimes struct vattr attr; 112d323ddf3SMatthew Dillon int (*img_first) __P((struct image_params *)); 11326f9a767SRodney W. Grimes 114c52007c2SDavid Greenman imgp = &image_params; 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes /* 117c52007c2SDavid Greenman * Initialize part of the common data 118df8bae1dSRodney W. Grimes */ 119c52007c2SDavid Greenman imgp->proc = p; 120c52007c2SDavid Greenman imgp->uap = uap; 121c52007c2SDavid Greenman imgp->attr = &attr; 122c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1235cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 124c52007c2SDavid Greenman imgp->entry_addr = 0; 125c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 126c52007c2SDavid Greenman imgp->interpreted = 0; 127c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 128e1743d02SSøren Schmidt imgp->auxargs = NULL; 1291616db3cSJohn Dyson imgp->vp = NULL; 1301616db3cSJohn Dyson imgp->firstpage = NULL; 1314fe88fe6SJohn Polstra imgp->ps_strings = 0; 132b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 13326f9a767SRodney W. Grimes 13426f9a767SRodney W. Grimes /* 13526f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 13626f9a767SRodney W. Grimes * environment strings 13726f9a767SRodney W. Grimes */ 1381616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 139c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 14026f9a767SRodney W. Grimes error = ENOMEM; 14126f9a767SRodney W. Grimes goto exec_fail; 14226f9a767SRodney W. Grimes } 143c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 144c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1451616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 14626f9a767SRodney W. Grimes 14726f9a767SRodney W. Grimes /* 14826f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 14926f9a767SRodney W. Grimes * in ni_vp amoung other things. 15026f9a767SRodney W. Grimes */ 15126f9a767SRodney W. Grimes ndp = &nd; 152b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1533ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 15426f9a767SRodney W. Grimes 15526f9a767SRodney W. Grimes interpret: 15626f9a767SRodney W. Grimes 15726f9a767SRodney W. Grimes error = namei(ndp); 15826f9a767SRodney W. Grimes if (error) { 1591616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1601616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 16126f9a767SRodney W. Grimes goto exec_fail; 16226f9a767SRodney W. Grimes } 16326f9a767SRodney W. Grimes 164c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 1659c0fed3dSDoug Rabson imgp->fname = uap->fname; 16626f9a767SRodney W. Grimes 16726f9a767SRodney W. Grimes /* 1689022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1699022e4adSDavid Greenman */ 170c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1718677f509SDavid Greenman if (error) { 1728677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 17326f9a767SRodney W. Grimes goto exec_fail_dealloc; 1748677f509SDavid Greenman } 17526f9a767SRodney W. Grimes 1761616db3cSJohn Dyson error = exec_map_first_page(imgp); 1779caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1786d5a0a8cSDavid Greenman if (error) 17926f9a767SRodney W. Grimes goto exec_fail_dealloc; 18026f9a767SRodney W. Grimes 18126f9a767SRodney W. Grimes /* 182d323ddf3SMatthew Dillon * If the current process has a special image activator it 183d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 184d323ddf3SMatthew Dillon * scripts differently. 18526f9a767SRodney W. Grimes */ 186d323ddf3SMatthew Dillon error = -1; 187d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 188d323ddf3SMatthew Dillon error = img_first(imgp); 189d323ddf3SMatthew Dillon 190d323ddf3SMatthew Dillon /* 191d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 192d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 193d323ddf3SMatthew Dillon * and an error otherwise. 194d323ddf3SMatthew Dillon */ 195d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 196d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 197d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 198d323ddf3SMatthew Dillon continue; 199d323ddf3SMatthew Dillon } 200c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 201d323ddf3SMatthew Dillon } 202d323ddf3SMatthew Dillon 203d323ddf3SMatthew Dillon if (error) { 20426f9a767SRodney W. Grimes if (error == -1) 205d323ddf3SMatthew Dillon error = ENOEXEC; 20626f9a767SRodney W. Grimes goto exec_fail_dealloc; 207d323ddf3SMatthew Dillon } 208d323ddf3SMatthew Dillon 209d323ddf3SMatthew Dillon /* 210d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 211d323ddf3SMatthew Dillon * activate the interpreter. 212d323ddf3SMatthew Dillon */ 213c52007c2SDavid Greenman if (imgp->interpreted) { 2141616db3cSJohn Dyson exec_unmap_first_page(imgp); 215762e6b85SEivind Eklund /* free name buffer and old vnode */ 216762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 217f5277ae7SDavid Greenman vrele(ndp->ni_vp); 21826f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 219b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 220c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 22126f9a767SRodney W. Grimes goto interpret; 22226f9a767SRodney W. Grimes } 22326f9a767SRodney W. Grimes 22426f9a767SRodney W. Grimes /* 22526f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 22626f9a767SRodney W. Grimes */ 227c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 22826f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 22926f9a767SRodney W. Grimes 23026f9a767SRodney W. Grimes /* 2311e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2321e1e0b44SSøren Schmidt * let it do the stack setup. 2331e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 23426f9a767SRodney W. Grimes */ 2351e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 236c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2371e1e0b44SSøren Schmidt else 238c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 23926f9a767SRodney W. Grimes 240a78e8d2aSDavid Greenman /* 241a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 242a78e8d2aSDavid Greenman * be shared after an exec. 243a78e8d2aSDavid Greenman */ 244a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 245a78e8d2aSDavid Greenman struct filedesc *tmp; 246a78e8d2aSDavid Greenman 247a78e8d2aSDavid Greenman tmp = fdcopy(p); 248a78e8d2aSDavid Greenman fdfree(p); 249a78e8d2aSDavid Greenman p->p_fd = tmp; 250a78e8d2aSDavid Greenman } 251a78e8d2aSDavid Greenman 252fdf4e8b3SWarner Losh /* Stop profiling */ 253fdf4e8b3SWarner Losh stopprofclock(p); 254fdf4e8b3SWarner Losh 25526f9a767SRodney W. Grimes /* close files on exec */ 25626f9a767SRodney W. Grimes fdcloseexec(p); 25726f9a767SRodney W. Grimes 25826f9a767SRodney W. Grimes /* reset caught signals */ 25926f9a767SRodney W. Grimes execsigs(p); 26026f9a767SRodney W. Grimes 26126f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 26226f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 26326f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 26426f9a767SRodney W. Grimes p->p_comm[len] = 0; 26526f9a767SRodney W. Grimes 26626f9a767SRodney W. Grimes /* 26724b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 268dc733423SDag-Erling Smørgrav * it that it now has its own resources back 26926f9a767SRodney W. Grimes */ 270e65897c3SJohn Baldwin PROC_LOCK(p); 27126f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 27226f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 27326f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 27426f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 27526f9a767SRodney W. Grimes } 27626f9a767SRodney W. Grimes 27726f9a767SRodney W. Grimes /* 278b1fc0ec1SRobert Watson * XXX: Note, the whole execve() is incredibly racey right now 279b1fc0ec1SRobert Watson * with regards to debugging and privilege/credential management. 280b1fc0ec1SRobert Watson * In particular, it's possible to race during exec() to attach 281b1fc0ec1SRobert Watson * debugging to a process that will gain privilege. 282b1fc0ec1SRobert Watson * 283b1fc0ec1SRobert Watson * This MUST be fixed prior to any release. 284b1fc0ec1SRobert Watson */ 285b1fc0ec1SRobert Watson 286b1fc0ec1SRobert Watson /* 287a78e8d2aSDavid Greenman * Implement image setuid/setgid. 288a78e8d2aSDavid Greenman * 289a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 290a78e8d2aSDavid Greenman * the process is being traced. 291c52007c2SDavid Greenman */ 292b1fc0ec1SRobert Watson oldcred = p->p_ucred; 293b1fc0ec1SRobert Watson newcred = NULL; 294b1fc0ec1SRobert Watson if ((((attr.va_mode & VSUID) && oldcred->cr_uid != attr.va_uid) || 295b1fc0ec1SRobert Watson ((attr.va_mode & VSGID) && oldcred->cr_gid != attr.va_gid)) && 296a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 297c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 298e65897c3SJohn Baldwin PROC_UNLOCK(p); 299c52007c2SDavid Greenman /* 300c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 30126f9a767SRodney W. Grimes * root. 30226f9a767SRodney W. Grimes */ 303b1fc0ec1SRobert Watson if (p->p_tracep && suser_xxx(oldcred, NULL, PRISON_ROOT)) { 30426f9a767SRodney W. Grimes p->p_traceflag = 0; 30526f9a767SRodney W. Grimes vrele(p->p_tracep); 306c52007c2SDavid Greenman p->p_tracep = NULL; 30726f9a767SRodney W. Grimes } 308c52007c2SDavid Greenman /* 309c52007c2SDavid Greenman * Set the new credentials. 310c52007c2SDavid Greenman */ 311b1fc0ec1SRobert Watson newcred = crdup(oldcred); 312c52007c2SDavid Greenman if (attr.va_mode & VSUID) 313b1fc0ec1SRobert Watson change_euid(newcred, attr.va_uid); 314c52007c2SDavid Greenman if (attr.va_mode & VSGID) 315b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 316d5f81602SSean Eric Fagan setsugid(p); 3175e266442SWarner Losh setugidsafety(p); 318c52007c2SDavid Greenman } else { 319b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 320b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 321c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 322e65897c3SJohn Baldwin PROC_UNLOCK(p); 32326f9a767SRodney W. Grimes } 32426f9a767SRodney W. Grimes 32526f9a767SRodney W. Grimes /* 326c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 327b1fc0ec1SRobert Watson * 328b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 329b1fc0ec1SRobert Watson * POSIX-compliant. A number of sourses indicate that the saved 330b1fc0ec1SRobert Watson * uid/gid should only be updated if the new ruid is not equal to 331b1fc0ec1SRobert Watson * the old ruid, or the new euid is not equal to the old euid and 332b1fc0ec1SRobert Watson * the new euid is not equal to the old ruid. The FreeBSD code 333b1fc0ec1SRobert Watson * always updates the saved uid/gid. Also, this code uses the new 334b1fc0ec1SRobert Watson * (replaced) euid and egid as the source, which may or may not be 335b1fc0ec1SRobert Watson * the right ones to use. 33626f9a767SRodney W. Grimes */ 337b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 338b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 339b1fc0ec1SRobert Watson /* 340b1fc0ec1SRobert Watson * Avoid allocating a newcred if we don't have one yet and 341b1fc0ec1SRobert Watson * the saved uid/gid update would be a noop. 342b1fc0ec1SRobert Watson */ 343b1fc0ec1SRobert Watson if (newcred == NULL) 344b1fc0ec1SRobert Watson newcred = crdup(oldcred); 345b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 346b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 347b1fc0ec1SRobert Watson } 348b1fc0ec1SRobert Watson if (newcred != NULL) { 349b1fc0ec1SRobert Watson PROC_LOCK(p); 350b1fc0ec1SRobert Watson p->p_ucred = newcred; 351b1fc0ec1SRobert Watson PROC_UNLOCK(p); 352b1fc0ec1SRobert Watson crfree(oldcred); 353b1fc0ec1SRobert Watson } 35426f9a767SRodney W. Grimes 35526f9a767SRodney W. Grimes /* 3562a531c80SDavid Greenman * Store the vp for use in procfs 3572a531c80SDavid Greenman */ 3582a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3592a531c80SDavid Greenman vrele(p->p_textvp); 3602a531c80SDavid Greenman VREF(ndp->ni_vp); 3612a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3622a531c80SDavid Greenman 3632a531c80SDavid Greenman /* 364cb679c38SJonathan Lemon * notify others that we exec'd 365cb679c38SJonathan Lemon */ 366e65897c3SJohn Baldwin PROC_LOCK(p); 367cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 368cb679c38SJonathan Lemon 369cb679c38SJonathan Lemon /* 37026f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 37126f9a767SRodney W. Grimes * can be set before the program executes. 37226f9a767SRodney W. Grimes */ 373e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 3742a024a2bSSean Eric Fagan 37526f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 37626f9a767SRodney W. Grimes psignal(p, SIGTRAP); 37726f9a767SRodney W. Grimes 37826f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 37926f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 38026f9a767SRodney W. Grimes 3814fe88fe6SJohn Polstra /* Set values passed into the program in registers. */ 3824fe88fe6SJohn Polstra setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 3834fe88fe6SJohn Polstra imgp->ps_strings); 38426f9a767SRodney W. Grimes 385b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 386b9df5231SPoul-Henning Kamp if (p->p_args && --p->p_args->ar_ref == 0) 387b9df5231SPoul-Henning Kamp FREE(p->p_args, M_PARGS); 388b9df5231SPoul-Henning Kamp p->p_args = NULL; 389b9df5231SPoul-Henning Kamp 390b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 391b9df5231SPoul-Henning Kamp i = imgp->endargs - imgp->stringbase; 392b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 393e65897c3SJohn Baldwin PROC_UNLOCK(p); 394b9df5231SPoul-Henning Kamp MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 395b9df5231SPoul-Henning Kamp M_PARGS, M_WAITOK); 396e65897c3SJohn Baldwin KASSERT(p->p_args != NULL, ("malloc of p_args failed")); 397e65897c3SJohn Baldwin PROC_LOCK(p); 398b9df5231SPoul-Henning Kamp p->p_args->ar_ref = 1; 399b9df5231SPoul-Henning Kamp p->p_args->ar_length = i; 400b9df5231SPoul-Henning Kamp bcopy(imgp->stringbase, p->p_args->ar_args, i); 401b9df5231SPoul-Henning Kamp } 402e65897c3SJohn Baldwin PROC_UNLOCK(p); 403b9df5231SPoul-Henning Kamp 4041616db3cSJohn Dyson exec_fail_dealloc: 4051616db3cSJohn Dyson 40626f9a767SRodney W. Grimes /* 40726f9a767SRodney W. Grimes * free various allocated resources 40826f9a767SRodney W. Grimes */ 4091616db3cSJohn Dyson if (imgp->firstpage) 4101616db3cSJohn Dyson exec_unmap_first_page(imgp); 41126f9a767SRodney W. Grimes 412c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 4131616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 4141616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 4151616db3cSJohn Dyson 4169c0fed3dSDoug Rabson if (imgp->vp) { 417762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 4189c0fed3dSDoug Rabson vrele(imgp->vp); 419a3cf6ebaSDavid Greenman } 42026f9a767SRodney W. Grimes 4211616db3cSJohn Dyson if (error == 0) 4221616db3cSJohn Dyson return (0); 4231616db3cSJohn Dyson 42426f9a767SRodney W. Grimes exec_fail: 425c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 42626f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 42726f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 42826f9a767SRodney W. Grimes /* NOT REACHED */ 42926f9a767SRodney W. Grimes return(0); 43026f9a767SRodney W. Grimes } else { 43126f9a767SRodney W. Grimes return(error); 43226f9a767SRodney W. Grimes } 43326f9a767SRodney W. Grimes } 43426f9a767SRodney W. Grimes 4351616db3cSJohn Dyson int 4361616db3cSJohn Dyson exec_map_first_page(imgp) 4371616db3cSJohn Dyson struct image_params *imgp; 4381616db3cSJohn Dyson { 439d8aad40cSJohn Baldwin int rv, i; 44095461b45SJohn Dyson int initial_pagein; 44195461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 4421616db3cSJohn Dyson vm_object_t object; 4431616db3cSJohn Dyson 4441616db3cSJohn Dyson 4451616db3cSJohn Dyson if (imgp->firstpage) { 4461616db3cSJohn Dyson exec_unmap_first_page(imgp); 4471616db3cSJohn Dyson } 4481616db3cSJohn Dyson 4499ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 45023955314SAlfred Perlstein mtx_lock(&vm_mtx); 4511616db3cSJohn Dyson 45295461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 4531616db3cSJohn Dyson 45495461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 45595461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 45695461b45SJohn Dyson if (initial_pagein > object->size) 45795461b45SJohn Dyson initial_pagein = object->size; 45895461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 459d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 46095461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 46195461b45SJohn Dyson break; 46295461b45SJohn Dyson if (ma[i]->valid) 46395461b45SJohn Dyson break; 464e69763a3SDoug Rabson vm_page_busy(ma[i]); 46595461b45SJohn Dyson } else { 46695461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 46795461b45SJohn Dyson if (ma[i] == NULL) 46895461b45SJohn Dyson break; 46995461b45SJohn Dyson } 47095461b45SJohn Dyson } 47195461b45SJohn Dyson initial_pagein = i; 4721616db3cSJohn Dyson 47395461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 47495461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 47595461b45SJohn Dyson 476eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 477ecbb00a2SDoug Rabson if (ma[0]) { 47895461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 4798f9110f6SJohn Dyson vm_page_free(ma[0]); 480ecbb00a2SDoug Rabson } 48123955314SAlfred Perlstein mtx_unlock(&vm_mtx); 4821616db3cSJohn Dyson return EIO; 4831616db3cSJohn Dyson } 4841616db3cSJohn Dyson } 4851616db3cSJohn Dyson 48695461b45SJohn Dyson vm_page_wire(ma[0]); 487e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 4881616db3cSJohn Dyson 48995461b45SJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 49095461b45SJohn Dyson imgp->firstpage = ma[0]; 4911616db3cSJohn Dyson 49223955314SAlfred Perlstein mtx_unlock(&vm_mtx); 4931616db3cSJohn Dyson return 0; 4941616db3cSJohn Dyson } 4951616db3cSJohn Dyson 4961616db3cSJohn Dyson void 4971616db3cSJohn Dyson exec_unmap_first_page(imgp) 4981616db3cSJohn Dyson struct image_params *imgp; 4991616db3cSJohn Dyson { 50023955314SAlfred Perlstein 5011616db3cSJohn Dyson if (imgp->firstpage) { 50223955314SAlfred Perlstein mtx_lock(&vm_mtx); 5031616db3cSJohn Dyson pmap_kremove((vm_offset_t) imgp->image_header); 50473007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 50523955314SAlfred Perlstein mtx_unlock(&vm_mtx); 5061616db3cSJohn Dyson imgp->firstpage = NULL; 5071616db3cSJohn Dyson } 5081616db3cSJohn Dyson } 5091616db3cSJohn Dyson 51026f9a767SRodney W. Grimes /* 51126f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 51226f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 51326f9a767SRodney W. Grimes * automatically in trap.c. 51426f9a767SRodney W. Grimes */ 51526f9a767SRodney W. Grimes int 516c52007c2SDavid Greenman exec_new_vmspace(imgp) 517c52007c2SDavid Greenman struct image_params *imgp; 51826f9a767SRodney W. Grimes { 51926f9a767SRodney W. Grimes int error; 520c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 5212267af78SJulian Elischer caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 522af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 52326f9a767SRodney W. Grimes 52423955314SAlfred Perlstein mtx_assert(&vm_mtx, MA_OWNED); 525c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 52626f9a767SRodney W. Grimes 527af9ec885SJohn Dyson /* 528af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 529af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 530af9ec885SJohn Dyson * not disrupted 531af9ec885SJohn Dyson */ 532af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 5333d903220SDoug Rabson if (vmspace->vm_shm) 534c52007c2SDavid Greenman shmexit(imgp->proc); 535b1028ad1SLuoqi Chen pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 5369c0fed3dSDoug Rabson vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 537af9ec885SJohn Dyson } else { 538492da96cSJohn Dyson vmspace_exec(imgp->proc); 539492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 540af9ec885SJohn Dyson map = &vmspace->vm_map; 541af9ec885SJohn Dyson } 54226f9a767SRodney W. Grimes 54326f9a767SRodney W. Grimes /* Allocate a new stack */ 5442267af78SJulian Elischer error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 5452267af78SJulian Elischer (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 5462267af78SJulian Elischer if (error) 5472267af78SJulian Elischer return (error); 5482267af78SJulian Elischer 54963c47a5cSDoug Rabson #ifdef __ia64__ 55063c47a5cSDoug Rabson { 55163c47a5cSDoug Rabson /* 55263c47a5cSDoug Rabson * Allocate backing store. We really need something 55363c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 55463c47a5cSDoug Rabson * store to grow upwards. This will do for now. 55563c47a5cSDoug Rabson */ 55663c47a5cSDoug Rabson vm_offset_t bsaddr; 55763c47a5cSDoug Rabson bsaddr = USRSTACK - 2*MAXSSIZ; 55863c47a5cSDoug Rabson error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 55963c47a5cSDoug Rabson 4*PAGE_SIZE, 0, 56063c47a5cSDoug Rabson VM_PROT_ALL, VM_PROT_ALL, 0); 56163c47a5cSDoug Rabson imgp->proc->p_md.md_bspstore = bsaddr; 56263c47a5cSDoug Rabson } 56363c47a5cSDoug Rabson #endif 56463c47a5cSDoug Rabson 5652267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 5662267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 5672267af78SJulian Elischer * process stack so we can check the stack rlimit. 5682267af78SJulian Elischer */ 5692267af78SJulian Elischer vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 5702267af78SJulian Elischer vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 57126f9a767SRodney W. Grimes 57226f9a767SRodney W. Grimes return(0); 57326f9a767SRodney W. Grimes } 57426f9a767SRodney W. Grimes 57526f9a767SRodney W. Grimes /* 57626f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 57726f9a767SRodney W. Grimes * address space into the temporary string buffer. 57826f9a767SRodney W. Grimes */ 57926f9a767SRodney W. Grimes int 580c52007c2SDavid Greenman exec_extract_strings(imgp) 581c52007c2SDavid Greenman struct image_params *imgp; 58226f9a767SRodney W. Grimes { 58326f9a767SRodney W. Grimes char **argv, **envv; 58426f9a767SRodney W. Grimes char *argp, *envp; 585ecbb00a2SDoug Rabson int error; 586ecbb00a2SDoug Rabson size_t length; 58726f9a767SRodney W. Grimes 58826f9a767SRodney W. Grimes /* 58926f9a767SRodney W. Grimes * extract arguments first 59026f9a767SRodney W. Grimes */ 59126f9a767SRodney W. Grimes 592c52007c2SDavid Greenman argv = imgp->uap->argv; 59326f9a767SRodney W. Grimes 5940fd1a014SDavid Greenman if (argv) { 595aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 5965cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 5975cf3d12cSAndrey A. Chernov return (EFAULT); 5985cf3d12cSAndrey A. Chernov if (argp) 5995cf3d12cSAndrey A. Chernov argv++; 6005cf3d12cSAndrey A. Chernov if (imgp->argv0) 6015cf3d12cSAndrey A. Chernov argp = imgp->argv0; 6025cf3d12cSAndrey A. Chernov if (argp) { 6035cf3d12cSAndrey A. Chernov do { 60426f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 60526f9a767SRodney W. Grimes return (EFAULT); 606c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 607c52007c2SDavid Greenman imgp->stringspace, &length))) { 6080fd1a014SDavid Greenman if (error == ENAMETOOLONG) 60926f9a767SRodney W. Grimes return(E2BIG); 6100fd1a014SDavid Greenman return (error); 6110fd1a014SDavid Greenman } 612c52007c2SDavid Greenman imgp->stringspace -= length; 613c52007c2SDavid Greenman imgp->stringp += length; 614c52007c2SDavid Greenman imgp->argc++; 615aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 61626f9a767SRodney W. Grimes } 6170fd1a014SDavid Greenman } 61826f9a767SRodney W. Grimes 619b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 620b9df5231SPoul-Henning Kamp 62126f9a767SRodney W. Grimes /* 62226f9a767SRodney W. Grimes * extract environment strings 62326f9a767SRodney W. Grimes */ 62426f9a767SRodney W. Grimes 625c52007c2SDavid Greenman envv = imgp->uap->envv; 62626f9a767SRodney W. Grimes 6270fd1a014SDavid Greenman if (envv) { 628aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 62926f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 63026f9a767SRodney W. Grimes return (EFAULT); 631c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 632c52007c2SDavid Greenman imgp->stringspace, &length))) { 6330fd1a014SDavid Greenman if (error == ENAMETOOLONG) 63426f9a767SRodney W. Grimes return(E2BIG); 6350fd1a014SDavid Greenman return (error); 6360fd1a014SDavid Greenman } 637c52007c2SDavid Greenman imgp->stringspace -= length; 638c52007c2SDavid Greenman imgp->stringp += length; 639c52007c2SDavid Greenman imgp->envc++; 64026f9a767SRodney W. Grimes } 6410fd1a014SDavid Greenman } 64226f9a767SRodney W. Grimes 64326f9a767SRodney W. Grimes return (0); 64426f9a767SRodney W. Grimes } 64526f9a767SRodney W. Grimes 64626f9a767SRodney W. Grimes /* 64726f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 64826f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 64926f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 65026f9a767SRodney W. Grimes */ 651654f6be1SBruce Evans register_t * 652c52007c2SDavid Greenman exec_copyout_strings(imgp) 653c52007c2SDavid Greenman struct image_params *imgp; 65426f9a767SRodney W. Grimes { 65526f9a767SRodney W. Grimes int argc, envc; 65626f9a767SRodney W. Grimes char **vectp; 65726f9a767SRodney W. Grimes char *stringp, *destp; 658654f6be1SBruce Evans register_t *stack_base; 65993f6448cSDavid Greenman struct ps_strings *arginfo; 660d66a5066SPeter Wemm int szsigcode; 66126f9a767SRodney W. Grimes 66226f9a767SRodney W. Grimes /* 66326f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 664d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 66526f9a767SRodney W. Grimes */ 6664c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 667d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 668d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 6691ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 6701ed012f9SPeter Wemm 67126f9a767SRodney W. Grimes /* 672d66a5066SPeter Wemm * install sigcode 673d66a5066SPeter Wemm */ 674d66a5066SPeter Wemm if (szsigcode) 675d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 676d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 677d66a5066SPeter Wemm 678d66a5066SPeter Wemm /* 679e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 680e1743d02SSøren Schmidt * on the stack. 681e1743d02SSøren Schmidt */ 682b9a22da4STakanori Watanabe if (imgp->auxargs) { 683e1743d02SSøren Schmidt /* 684b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 685b9a22da4STakanori Watanabe * lower compatibility. 686b9a22da4STakanori Watanabe */ 687b9a22da4STakanori Watanabe imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 688b9a22da4STakanori Watanabe : (AT_COUNT * 2); 689b9a22da4STakanori Watanabe /* 690b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 691b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 692b9a22da4STakanori Watanabe * for argument of Runtime loader. 693e1743d02SSøren Schmidt */ 694e1743d02SSøren Schmidt vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 695b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 696b9a22da4STakanori Watanabe 697b9a22da4STakanori Watanabe } else 698e1743d02SSøren Schmidt /* 699b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 700b9a22da4STakanori Watanabe * the arg and env vector sets 70126f9a767SRodney W. Grimes */ 702e1743d02SSøren Schmidt vectp = (char **) 703e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 70426f9a767SRodney W. Grimes 70526f9a767SRodney W. Grimes /* 70626f9a767SRodney W. Grimes * vectp also becomes our initial stack base 70726f9a767SRodney W. Grimes */ 708654f6be1SBruce Evans stack_base = (register_t *)vectp; 70926f9a767SRodney W. Grimes 710c52007c2SDavid Greenman stringp = imgp->stringbase; 711c52007c2SDavid Greenman argc = imgp->argc; 712c52007c2SDavid Greenman envc = imgp->envc; 71326f9a767SRodney W. Grimes 71493f6448cSDavid Greenman /* 7153aed948bSDavid Greenman * Copy out strings - arguments and environment. 71693f6448cSDavid Greenman */ 717c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 71893f6448cSDavid Greenman 71993f6448cSDavid Greenman /* 7203aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 7213aed948bSDavid Greenman */ 722aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 7233aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 7243aed948bSDavid Greenman 7253aed948bSDavid Greenman /* 7263aed948bSDavid Greenman * Fill in argument portion of vector table. 72793f6448cSDavid Greenman */ 72826f9a767SRodney W. Grimes for (; argc > 0; --argc) { 729aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 7303aed948bSDavid Greenman while (*stringp++ != 0) 7313aed948bSDavid Greenman destp++; 7323aed948bSDavid Greenman destp++; 73326f9a767SRodney W. Grimes } 73426f9a767SRodney W. Grimes 7351a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 7366ab46d52SBruce Evans suword(vectp++, 0); 73726f9a767SRodney W. Grimes 738aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 7393aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 74093f6448cSDavid Greenman 74193f6448cSDavid Greenman /* 7423aed948bSDavid Greenman * Fill in environment portion of vector table. 74393f6448cSDavid Greenman */ 74426f9a767SRodney W. Grimes for (; envc > 0; --envc) { 745aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 7463aed948bSDavid Greenman while (*stringp++ != 0) 7473aed948bSDavid Greenman destp++; 7483aed948bSDavid Greenman destp++; 74926f9a767SRodney W. Grimes } 75026f9a767SRodney W. Grimes 75126f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 7526ab46d52SBruce Evans suword(vectp, 0); 75326f9a767SRodney W. Grimes 75426f9a767SRodney W. Grimes return (stack_base); 75526f9a767SRodney W. Grimes } 75626f9a767SRodney W. Grimes 75726f9a767SRodney W. Grimes /* 75826f9a767SRodney W. Grimes * Check permissions of file to execute. 759cf64863aSRobert Watson * Called with imgp->vp locked. 76026f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 76126f9a767SRodney W. Grimes */ 762c8a79999SPeter Wemm int 763c52007c2SDavid Greenman exec_check_permissions(imgp) 764c52007c2SDavid Greenman struct image_params *imgp; 76526f9a767SRodney W. Grimes { 766c52007c2SDavid Greenman struct proc *p = imgp->proc; 767c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 768c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 76926f9a767SRodney W. Grimes int error; 77026f9a767SRodney W. Grimes 77126f9a767SRodney W. Grimes /* Get file attributes */ 772c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 77326f9a767SRodney W. Grimes if (error) 77426f9a767SRodney W. Grimes return (error); 77526f9a767SRodney W. Grimes 77626f9a767SRodney W. Grimes /* 77726f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 77826f9a767SRodney W. Grimes * file resides on. 77926f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 78026f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 78126f9a767SRodney W. Grimes * file really is executable. 78226f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 78326f9a767SRodney W. Grimes */ 784c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 78526f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 78626f9a767SRodney W. Grimes (attr->va_type != VREG)) { 78726f9a767SRodney W. Grimes return (EACCES); 78826f9a767SRodney W. Grimes } 78926f9a767SRodney W. Grimes 79026f9a767SRodney W. Grimes /* 79126f9a767SRodney W. Grimes * Zero length files can't be exec'd 79226f9a767SRodney W. Grimes */ 79326f9a767SRodney W. Grimes if (attr->va_size == 0) 79426f9a767SRodney W. Grimes return (ENOEXEC); 79526f9a767SRodney W. Grimes 79626f9a767SRodney W. Grimes /* 79726f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 79826f9a767SRodney W. Grimes */ 799c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 80026f9a767SRodney W. Grimes if (error) 80126f9a767SRodney W. Grimes return (error); 80226f9a767SRodney W. Grimes 8036d5a0a8cSDavid Greenman /* 8046d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 8056d5a0a8cSDavid Greenman * if there are any. 8066d5a0a8cSDavid Greenman */ 8076d5a0a8cSDavid Greenman if (vp->v_writecount) 8086d5a0a8cSDavid Greenman return (ETXTBSY); 8096d5a0a8cSDavid Greenman 8106d5a0a8cSDavid Greenman /* 8116d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 8126d5a0a8cSDavid Greenman * general case). 8136d5a0a8cSDavid Greenman */ 814c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 81526f9a767SRodney W. Grimes if (error) 81626f9a767SRodney W. Grimes return (error); 81726f9a767SRodney W. Grimes 81826f9a767SRodney W. Grimes return (0); 819df8bae1dSRodney W. Grimes } 820aa855a59SPeter Wemm 821aa855a59SPeter Wemm /* 822aa855a59SPeter Wemm * Exec handler registration 823aa855a59SPeter Wemm */ 824aa855a59SPeter Wemm int 825aa855a59SPeter Wemm exec_register(execsw_arg) 826aa855a59SPeter Wemm const struct execsw *execsw_arg; 827aa855a59SPeter Wemm { 828aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 829aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 830aa855a59SPeter Wemm 831aa855a59SPeter Wemm if (execsw) 832aa855a59SPeter Wemm for (es = execsw; *es; es++) 833aa855a59SPeter Wemm count++; 834aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 835aa855a59SPeter Wemm if (newexecsw == NULL) 836aa855a59SPeter Wemm return ENOMEM; 837aa855a59SPeter Wemm xs = newexecsw; 838aa855a59SPeter Wemm if (execsw) 839aa855a59SPeter Wemm for (es = execsw; *es; es++) 840aa855a59SPeter Wemm *xs++ = *es; 841aa855a59SPeter Wemm *xs++ = execsw_arg; 842aa855a59SPeter Wemm *xs = NULL; 843aa855a59SPeter Wemm if (execsw) 844aa855a59SPeter Wemm free(execsw, M_TEMP); 845aa855a59SPeter Wemm execsw = newexecsw; 846aa855a59SPeter Wemm return 0; 847aa855a59SPeter Wemm } 848aa855a59SPeter Wemm 849aa855a59SPeter Wemm int 850aa855a59SPeter Wemm exec_unregister(execsw_arg) 851aa855a59SPeter Wemm const struct execsw *execsw_arg; 852aa855a59SPeter Wemm { 853aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 854aa855a59SPeter Wemm int count = 1; 855aa855a59SPeter Wemm 856aa855a59SPeter Wemm if (execsw == NULL) 857aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 858aa855a59SPeter Wemm 859aa855a59SPeter Wemm for (es = execsw; *es; es++) { 860aa855a59SPeter Wemm if (*es == execsw_arg) 861aa855a59SPeter Wemm break; 862aa855a59SPeter Wemm } 863aa855a59SPeter Wemm if (*es == NULL) 864aa855a59SPeter Wemm return ENOENT; 865aa855a59SPeter Wemm for (es = execsw; *es; es++) 866aa855a59SPeter Wemm if (*es != execsw_arg) 867aa855a59SPeter Wemm count++; 868aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 869aa855a59SPeter Wemm if (newexecsw == NULL) 870aa855a59SPeter Wemm return ENOMEM; 871aa855a59SPeter Wemm xs = newexecsw; 872aa855a59SPeter Wemm for (es = execsw; *es; es++) 873aa855a59SPeter Wemm if (*es != execsw_arg) 874aa855a59SPeter Wemm *xs++ = *es; 875aa855a59SPeter Wemm *xs = NULL; 876aa855a59SPeter Wemm if (execsw) 877aa855a59SPeter Wemm free(execsw, M_TEMP); 878aa855a59SPeter Wemm execsw = newexecsw; 879aa855a59SPeter Wemm return 0; 880aa855a59SPeter Wemm } 881