2000-07-13 06:33:36 +04:00
|
|
|
/* $NetBSD: kern_exec.c,v 1.115 2000/07/13 02:33:36 matt Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*-
|
1996-10-01 03:18:43 +04:00
|
|
|
* Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
|
1994-06-29 10:29:24 +04:00
|
|
|
* Copyright (C) 1992 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1992 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1998-06-26 01:17:15 +04:00
|
|
|
#include "opt_ktrace.h"
|
1998-02-10 17:08:44 +03:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/acct.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check exec:
|
|
|
|
* given an "executable" described in the exec package's namei info,
|
|
|
|
* see what we can do with it.
|
|
|
|
*
|
|
|
|
* ON ENTRY:
|
|
|
|
* exec package with appropriate namei info
|
|
|
|
* proc pointer of exec'ing proc
|
|
|
|
* NO SELF-LOCKED VNODES
|
|
|
|
*
|
|
|
|
* ON EXIT:
|
|
|
|
* error: nothing held, etc. exec header still allocated.
|
1996-10-01 03:18:43 +04:00
|
|
|
* ok: filled exec package, executable's vnode (unlocked).
|
1994-06-29 10:29:24 +04:00
|
|
|
*
|
|
|
|
* EXEC SWITCH ENTRY:
|
|
|
|
* Locked vnode to check, exec package, proc.
|
|
|
|
*
|
|
|
|
* EXEC SWITCH EXIT:
|
1996-10-01 03:18:43 +04:00
|
|
|
* ok: return 0, filled exec package, executable's vnode (unlocked).
|
1994-06-29 10:29:24 +04:00
|
|
|
* error: destructive:
|
|
|
|
* everything deallocated execept exec header.
|
1996-09-27 03:34:46 +04:00
|
|
|
* non-destructive:
|
1996-10-01 03:18:43 +04:00
|
|
|
* error code, executable's vnode (unlocked),
|
1996-09-27 03:34:46 +04:00
|
|
|
* exec header unmodified.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
check_exec(p, epp)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct nameidata *ndp;
|
1998-07-28 22:37:47 +04:00
|
|
|
size_t resid;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
ndp = epp->ep_ndp;
|
|
|
|
ndp->ni_cnd.cn_nameiop = LOOKUP;
|
|
|
|
ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
|
|
|
|
/* first get the vnode */
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = namei(ndp)) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
return error;
|
|
|
|
epp->ep_vp = vp = ndp->ni_vp;
|
|
|
|
|
1997-05-08 20:19:43 +04:00
|
|
|
/* check access and type */
|
1994-06-29 10:29:24 +04:00
|
|
|
if (vp->v_type != VREG) {
|
1997-04-10 23:45:40 +04:00
|
|
|
error = EACCES;
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad1;
|
|
|
|
}
|
1997-05-08 20:19:43 +04:00
|
|
|
if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
|
|
|
|
goto bad1;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* get attributes */
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad1;
|
|
|
|
|
|
|
|
/* Check mount point */
|
|
|
|
if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
|
|
|
|
error = EACCES;
|
|
|
|
goto bad1;
|
|
|
|
}
|
|
|
|
if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
|
1997-05-08 14:19:10 +04:00
|
|
|
epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* try to open it */
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad1;
|
|
|
|
|
1999-02-27 02:38:55 +03:00
|
|
|
/* unlock vp, since we need it unlocked from here on out. */
|
1998-03-01 05:20:01 +03:00
|
|
|
VOP_UNLOCK(vp, 0);
|
1996-10-01 03:18:43 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* now we have the file, get the exec header */
|
1996-02-04 05:15:01 +03:00
|
|
|
error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
|
1996-10-01 03:18:43 +04:00
|
|
|
UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
|
1996-02-04 05:15:01 +03:00
|
|
|
if (error)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad2;
|
|
|
|
epp->ep_hdrvalid = epp->ep_hdrlen - resid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up the vmcmds for creation of the process
|
|
|
|
* address space
|
|
|
|
*/
|
|
|
|
error = ENOEXEC;
|
|
|
|
for (i = 0; i < nexecs && error != 0; i++) {
|
1995-05-02 02:36:45 +04:00
|
|
|
int newerror;
|
|
|
|
|
|
|
|
if (execsw[i].es_check == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
newerror = (*execsw[i].es_check)(p, epp);
|
|
|
|
/* make sure the first "interesting" error code is saved. */
|
|
|
|
if (!newerror || error == ENOEXEC)
|
|
|
|
error = newerror;
|
1994-06-29 10:29:24 +04:00
|
|
|
if (epp->ep_flags & EXEC_DESTR && error != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
/* check that entry point is sane */
|
|
|
|
if (epp->ep_entry > VM_MAXUSER_ADDRESS)
|
|
|
|
error = ENOEXEC;
|
|
|
|
|
|
|
|
/* check limits */
|
|
|
|
if ((epp->ep_tsize > MAXTSIZ) ||
|
|
|
|
(epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
|
|
|
|
error = ENOMEM;
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free any vmspace-creation commands,
|
|
|
|
* and release their references
|
|
|
|
*/
|
|
|
|
kill_vmcmds(&epp->ep_vmcmds);
|
|
|
|
|
|
|
|
bad2:
|
|
|
|
/*
|
1999-02-27 02:38:55 +03:00
|
|
|
* close and release the vnode, restore the old one, free the
|
1994-06-29 10:29:24 +04:00
|
|
|
* pathname buf, and punt.
|
|
|
|
*/
|
1999-02-27 02:38:55 +03:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
|
1996-10-01 03:18:43 +04:00
|
|
|
VOP_CLOSE(vp, FREAD, p->p_ucred, p);
|
1999-02-27 02:38:55 +03:00
|
|
|
vput(vp);
|
1994-06-29 10:29:24 +04:00
|
|
|
FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
|
|
|
|
return error;
|
|
|
|
|
|
|
|
bad1:
|
|
|
|
/*
|
|
|
|
* free the namei pathname buffer, and put the vnode
|
|
|
|
* (which we don't yet have open).
|
|
|
|
*/
|
1996-10-01 03:18:43 +04:00
|
|
|
vput(vp); /* was still locked */
|
1994-06-29 10:29:24 +04:00
|
|
|
FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec system call
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1996-02-09 21:59:18 +03:00
|
|
|
int
|
1995-10-07 09:25:19 +03:00
|
|
|
sys_execve(p, v, retval)
|
2000-03-30 13:27:11 +04:00
|
|
|
struct proc *p;
|
1995-09-20 01:40:36 +04:00
|
|
|
void *v;
|
|
|
|
register_t *retval;
|
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys_execve_args /* {
|
1996-12-22 13:21:06 +03:00
|
|
|
syscallarg(const char *) path;
|
|
|
|
syscallarg(char * const *) argp;
|
|
|
|
syscallarg(char * const *) envp;
|
1995-09-20 01:40:36 +04:00
|
|
|
} */ *uap = v;
|
1994-06-29 10:29:24 +04:00
|
|
|
int error, i;
|
|
|
|
struct exec_package pack;
|
|
|
|
struct nameidata nid;
|
|
|
|
struct vattr attr;
|
|
|
|
struct ucred *cred = p->p_ucred;
|
|
|
|
char *argp;
|
1996-12-22 13:21:06 +03:00
|
|
|
char * const *cpp;
|
|
|
|
char *dp, *sp;
|
1995-03-08 04:23:00 +03:00
|
|
|
long argc, envc;
|
1995-03-09 15:05:21 +03:00
|
|
|
size_t len;
|
1994-06-29 10:29:24 +04:00
|
|
|
char *stack;
|
|
|
|
struct ps_strings arginfo;
|
1997-12-31 10:47:41 +03:00
|
|
|
struct vmspace *vm;
|
1994-06-29 10:29:24 +04:00
|
|
|
char **tmpfap;
|
1995-04-08 02:33:23 +04:00
|
|
|
int szsigcode;
|
2000-07-13 05:24:04 +04:00
|
|
|
struct exec_vmcmd *base_vcp = NULL;
|
1995-04-22 23:42:47 +04:00
|
|
|
extern struct emul emul_netbsd;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* figure out the maximum size of an exec header, if necessary.
|
|
|
|
* XXX should be able to keep LKM code from modifying exec switch
|
|
|
|
* when we're still using it, but...
|
|
|
|
*/
|
|
|
|
if (exec_maxhdrsz == 0) {
|
|
|
|
for (i = 0; i < nexecs; i++)
|
|
|
|
if (execsw[i].es_check != NULL
|
|
|
|
&& execsw[i].es_hdrsz > exec_maxhdrsz)
|
|
|
|
exec_maxhdrsz = execsw[i].es_hdrsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init the namei data to point the file user's program name */
|
1996-10-01 03:18:43 +04:00
|
|
|
/* XXX cgd 960926: why do this here? most will be clobbered. */
|
1994-10-20 07:22:35 +03:00
|
|
|
NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the fields of the exec package.
|
|
|
|
*/
|
1994-10-20 07:22:35 +03:00
|
|
|
pack.ep_name = SCARG(uap, path);
|
1994-06-29 10:29:24 +04:00
|
|
|
MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
|
|
|
|
pack.ep_hdrlen = exec_maxhdrsz;
|
|
|
|
pack.ep_hdrvalid = 0;
|
|
|
|
pack.ep_ndp = &nid;
|
1995-04-22 23:42:47 +04:00
|
|
|
pack.ep_emul_arg = NULL;
|
1994-06-29 10:29:24 +04:00
|
|
|
pack.ep_vmcmds.evs_cnt = 0;
|
|
|
|
pack.ep_vmcmds.evs_used = 0;
|
|
|
|
pack.ep_vap = &attr;
|
1995-04-22 23:42:47 +04:00
|
|
|
pack.ep_emul = &emul_netbsd;
|
1994-06-29 10:29:24 +04:00
|
|
|
pack.ep_flags = 0;
|
|
|
|
|
|
|
|
/* see if we can run it. */
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = check_exec(p, &pack)) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto freehdr;
|
|
|
|
|
|
|
|
/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
|
|
|
|
|
|
|
|
/* allocate an argument buffer */
|
1998-02-05 10:59:28 +03:00
|
|
|
argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
|
1994-06-29 10:29:24 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1998-08-13 06:10:37 +04:00
|
|
|
if (argp == (vaddr_t) 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
panic("execve: argp == NULL");
|
|
|
|
#endif
|
|
|
|
dp = argp;
|
|
|
|
argc = 0;
|
|
|
|
|
|
|
|
/* copy the fake args list, if there's one, freeing it as we go */
|
|
|
|
if (pack.ep_flags & EXEC_HASARGL) {
|
|
|
|
tmpfap = pack.ep_fa;
|
|
|
|
while (*tmpfap != NULL) {
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = *tmpfap;
|
|
|
|
while (*cp)
|
|
|
|
*dp++ = *cp++;
|
1996-02-04 05:15:01 +03:00
|
|
|
dp++;
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
FREE(*tmpfap, M_EXEC);
|
|
|
|
tmpfap++; argc++;
|
|
|
|
}
|
|
|
|
FREE(pack.ep_fa, M_EXEC);
|
|
|
|
pack.ep_flags &= ~EXEC_HASARGL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get argv & environment */
|
1994-10-20 07:22:35 +03:00
|
|
|
if (!(cpp = SCARG(uap, argp))) {
|
1994-06-29 10:29:24 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pack.ep_flags & EXEC_SKIPARG)
|
|
|
|
cpp++;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
len = argp + ARG_MAX - dp;
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad;
|
|
|
|
if (!sp)
|
|
|
|
break;
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = copyinstr(sp, dp, len, &len)) != 0) {
|
1994-06-29 10:29:24 +04:00
|
|
|
if (error == ENAMETOOLONG)
|
|
|
|
error = E2BIG;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
dp += len;
|
|
|
|
cpp++;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
envc = 0;
|
1996-02-04 05:15:01 +03:00
|
|
|
/* environment need not be there */
|
|
|
|
if ((cpp = SCARG(uap, envp)) != NULL ) {
|
1994-06-29 10:29:24 +04:00
|
|
|
while (1) {
|
|
|
|
len = argp + ARG_MAX - dp;
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
|
1994-06-29 10:29:24 +04:00
|
|
|
goto bad;
|
|
|
|
if (!sp)
|
|
|
|
break;
|
1996-02-04 05:15:01 +03:00
|
|
|
if ((error = copyinstr(sp, dp, len, &len)) != 0) {
|
1994-06-29 10:29:24 +04:00
|
|
|
if (error == ENAMETOOLONG)
|
|
|
|
error = E2BIG;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
dp += len;
|
|
|
|
cpp++;
|
|
|
|
envc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-02-22 04:39:56 +03:00
|
|
|
dp = (char *) ALIGN(dp);
|
|
|
|
|
1995-04-22 23:42:47 +04:00
|
|
|
szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
|
1995-04-08 02:33:23 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* Now check if args & environ fit into new stack */
|
1999-12-30 18:59:26 +03:00
|
|
|
if (pack.ep_flags & EXEC_32)
|
2000-01-05 11:11:31 +03:00
|
|
|
len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(int) +
|
1999-12-30 18:59:26 +03:00
|
|
|
sizeof(int) + dp + STACKGAPLEN + szsigcode +
|
|
|
|
sizeof(struct ps_strings)) - argp;
|
|
|
|
else
|
2000-01-05 11:11:31 +03:00
|
|
|
len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
|
1999-12-30 18:59:26 +03:00
|
|
|
sizeof(int) + dp + STACKGAPLEN + szsigcode +
|
|
|
|
sizeof(struct ps_strings)) - argp;
|
1995-04-22 23:42:47 +04:00
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
len = ALIGN(len); /* make the stack "safely" aligned */
|
|
|
|
|
|
|
|
if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
|
|
|
|
error = ENOMEM;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust "active stack depth" for process VSZ */
|
|
|
|
pack.ep_ssize = len; /* maybe should go elsewhere, but... */
|
|
|
|
|
1997-12-31 10:47:41 +03:00
|
|
|
/*
|
|
|
|
* Do whatever is necessary to prepare the address space
|
|
|
|
* for remapping. Note that this might replace the current
|
|
|
|
* vmspace with another!
|
|
|
|
*/
|
1998-02-05 10:59:28 +03:00
|
|
|
uvmspace_exec(p);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* Now map address space */
|
1997-12-31 10:47:41 +03:00
|
|
|
vm = p->p_vmspace;
|
1994-06-29 10:29:24 +04:00
|
|
|
vm->vm_taddr = (char *) pack.ep_taddr;
|
|
|
|
vm->vm_tsize = btoc(pack.ep_tsize);
|
|
|
|
vm->vm_daddr = (char *) pack.ep_daddr;
|
|
|
|
vm->vm_dsize = btoc(pack.ep_dsize);
|
|
|
|
vm->vm_ssize = btoc(pack.ep_ssize);
|
|
|
|
vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
|
|
|
|
|
|
|
|
/* create the new process's VM space by running the vmcmds */
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (pack.ep_vmcmds.evs_used == 0)
|
|
|
|
panic("execve: no vmcmds");
|
|
|
|
#endif
|
|
|
|
for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
|
|
|
|
struct exec_vmcmd *vcp;
|
|
|
|
|
|
|
|
vcp = &pack.ep_vmcmds.evs_cmds[i];
|
2000-07-13 05:24:04 +04:00
|
|
|
if (vcp->ev_flags & VMCMD_RELATIVE) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (base_vcp == NULL)
|
|
|
|
panic("execve: relative vmcmd with no base");
|
|
|
|
if (vcp->ev_flags & VMCMD_BASE)
|
|
|
|
panic("execve: illegal base & relative vmcmd");
|
|
|
|
#endif
|
|
|
|
vcp->ev_addr += base_vcp->ev_addr;
|
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
error = (*vcp->ev_proc)(p, vcp);
|
2000-06-21 09:43:33 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (error) {
|
|
|
|
if (i > 0)
|
|
|
|
printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i-1,
|
|
|
|
vcp[-1].ev_addr, vcp[-1].ev_len,
|
|
|
|
vcp[-1].ev_offset);
|
|
|
|
printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i,
|
|
|
|
vcp->ev_addr, vcp->ev_len, vcp->ev_offset);
|
|
|
|
}
|
|
|
|
#endif
|
2000-07-13 05:24:04 +04:00
|
|
|
if (vcp->ev_flags & VMCMD_BASE)
|
|
|
|
base_vcp = vcp;
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free the vmspace-creation commands, and release their references */
|
|
|
|
kill_vmcmds(&pack.ep_vmcmds);
|
|
|
|
|
|
|
|
/* if an error happened, deallocate and punt */
|
2000-06-21 09:43:33 +04:00
|
|
|
if (error) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("execve: vmcmd %i failed: %d\n", i-1, error);
|
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
goto exec_abort;
|
2000-06-21 09:43:33 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* remember information about the process */
|
|
|
|
arginfo.ps_nargvstr = argc;
|
|
|
|
arginfo.ps_nenvstr = envc;
|
|
|
|
|
|
|
|
stack = (char *) (USRSTACK - len);
|
1995-04-22 23:42:47 +04:00
|
|
|
/* Now copy argc, args & environ to new stack */
|
2000-06-21 09:43:33 +04:00
|
|
|
if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("execve: copyargs failed\n");
|
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
goto exec_abort;
|
2000-06-21 09:43:33 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* copy out the process's ps_strings structure */
|
2000-06-21 09:43:33 +04:00
|
|
|
if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo))) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("execve: ps_strings copyout failed\n");
|
|
|
|
#endif
|
1994-06-29 10:29:24 +04:00
|
|
|
goto exec_abort;
|
2000-06-21 09:43:33 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
2000-05-26 06:24:37 +04:00
|
|
|
/* fill process ps_strings info */
|
|
|
|
p->p_psstr = PS_STRINGS;
|
|
|
|
p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
|
|
|
|
p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
|
|
|
|
p->p_psenv = offsetof(struct ps_strings, ps_envstr);
|
|
|
|
p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
|
|
|
|
|
1994-06-29 10:29:24 +04:00
|
|
|
/* copy out the process's signal trapoline code */
|
1998-09-11 16:50:05 +04:00
|
|
|
if (szsigcode) {
|
|
|
|
if (copyout((char *)pack.ep_emul->e_sigcode,
|
|
|
|
p->p_sigacts->ps_sigcode = (char *)PS_STRINGS - szsigcode,
|
2000-06-21 09:43:33 +04:00
|
|
|
szsigcode)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("execve: sig trampoline copyout failed\n");
|
|
|
|
#endif
|
1999-01-22 23:51:04 +03:00
|
|
|
goto exec_abort;
|
2000-06-21 09:43:33 +04:00
|
|
|
}
|
1999-11-21 20:04:05 +03:00
|
|
|
#ifdef PMAP_NEED_PROCWR
|
|
|
|
/* This is code. Let the pmap do what is needed. */
|
|
|
|
pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
|
|
|
|
#endif
|
1998-09-11 16:50:05 +04:00
|
|
|
}
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1999-08-09 06:42:20 +04:00
|
|
|
stopprofclock(p); /* stop profiling */
|
1994-06-29 10:29:24 +04:00
|
|
|
fdcloseexec(p); /* handle close on exec */
|
|
|
|
execsigs(p); /* reset catched signals */
|
1999-01-25 19:00:06 +03:00
|
|
|
p->p_ctxlink = NULL; /* reset ucontext link */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* set command name & other accounting info */
|
|
|
|
len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_comm[len] = 0;
|
|
|
|
p->p_acflag &= ~AFORK;
|
|
|
|
|
|
|
|
/* record proc's vnode, for use by procfs and others */
|
|
|
|
if (p->p_textvp)
|
|
|
|
vrele(p->p_textvp);
|
|
|
|
VREF(pack.ep_vp);
|
|
|
|
p->p_textvp = pack.ep_vp;
|
|
|
|
|
|
|
|
p->p_flag |= P_EXEC;
|
|
|
|
if (p->p_flag & P_PPWAIT) {
|
|
|
|
p->p_flag &= ~P_PPWAIT;
|
|
|
|
wakeup((caddr_t) p->p_pptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deal with set[ug]id.
|
1999-04-27 09:28:44 +04:00
|
|
|
* MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
|
1994-06-29 10:29:24 +04:00
|
|
|
*/
|
1997-05-08 14:19:10 +04:00
|
|
|
if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
|
|
|
|
|| ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_ucred = crcopy(cred);
|
|
|
|
#ifdef KTRACE
|
|
|
|
/*
|
|
|
|
* If process is being ktraced, turn off - unless
|
|
|
|
* root set it.
|
|
|
|
*/
|
1998-05-02 22:33:19 +04:00
|
|
|
if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
|
|
|
|
ktrderef(p);
|
1994-06-29 10:29:24 +04:00
|
|
|
#endif
|
1997-05-08 14:19:10 +04:00
|
|
|
if (attr.va_mode & S_ISUID)
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_ucred->cr_uid = attr.va_uid;
|
1997-05-08 14:19:10 +04:00
|
|
|
if (attr.va_mode & S_ISGID)
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_ucred->cr_gid = attr.va_gid;
|
1999-09-28 18:47:00 +04:00
|
|
|
p_sugid(p);
|
1997-01-07 13:41:02 +03:00
|
|
|
} else
|
|
|
|
p->p_flag &= ~P_SUGID;
|
1994-06-29 10:29:24 +04:00
|
|
|
p->p_cred->p_svuid = p->p_ucred->cr_uid;
|
|
|
|
p->p_cred->p_svgid = p->p_ucred->cr_gid;
|
|
|
|
|
2000-01-25 04:15:14 +03:00
|
|
|
doexechooks(p);
|
|
|
|
|
1998-08-13 06:10:37 +04:00
|
|
|
uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
|
1999-02-27 02:38:55 +03:00
|
|
|
vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
|
1994-06-29 10:29:24 +04:00
|
|
|
VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
|
1999-02-27 02:38:55 +03:00
|
|
|
vput(pack.ep_vp);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
/* setup new registers and do misc. setup. */
|
1997-09-12 03:01:44 +04:00
|
|
|
(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
if (p->p_flag & P_TRACED)
|
|
|
|
psignal(p, SIGTRAP);
|
|
|
|
|
|
|
|
p->p_emul = pack.ep_emul;
|
|
|
|
FREE(pack.ep_hdr, M_EXEC);
|
1995-07-19 19:19:08 +04:00
|
|
|
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(p, KTR_EMUL))
|
2000-05-27 04:40:29 +04:00
|
|
|
ktremul(p);
|
1995-07-19 19:19:08 +04:00
|
|
|
#endif
|
1997-09-12 03:01:44 +04:00
|
|
|
|
|
|
|
return (EJUSTRETURN);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
bad:
|
|
|
|
/* free the vmspace-creation commands, and release their references */
|
|
|
|
kill_vmcmds(&pack.ep_vmcmds);
|
|
|
|
/* kill any opened file descriptor, if necessary */
|
|
|
|
if (pack.ep_flags & EXEC_HASFD) {
|
|
|
|
pack.ep_flags &= ~EXEC_HASFD;
|
1995-04-10 22:27:59 +04:00
|
|
|
(void) fdrelease(p, pack.ep_fd);
|
1994-06-29 10:29:24 +04:00
|
|
|
}
|
|
|
|
/* close and put the exec'd file */
|
1999-02-27 02:38:55 +03:00
|
|
|
vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
|
1994-06-29 10:29:24 +04:00
|
|
|
VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
|
1999-02-27 02:38:55 +03:00
|
|
|
vput(pack.ep_vp);
|
1994-06-29 10:29:24 +04:00
|
|
|
FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
|
1998-08-13 06:10:37 +04:00
|
|
|
uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
|
1994-06-29 10:29:24 +04:00
|
|
|
|
|
|
|
freehdr:
|
|
|
|
FREE(pack.ep_hdr, M_EXEC);
|
|
|
|
return error;
|
|
|
|
|
|
|
|
exec_abort:
|
|
|
|
/*
|
|
|
|
* the old process doesn't exist anymore. exit gracefully.
|
|
|
|
* get rid of the (new) address space we have created, if any, get rid
|
|
|
|
* of our namei data and vnode, and exit noting failure
|
|
|
|
*/
|
1998-02-05 10:59:28 +03:00
|
|
|
uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
|
|
|
|
VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
|
1995-12-09 07:11:00 +03:00
|
|
|
if (pack.ep_emul_arg)
|
|
|
|
FREE(pack.ep_emul_arg, M_TEMP);
|
1994-06-29 10:29:24 +04:00
|
|
|
FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
|
1999-02-27 02:38:55 +03:00
|
|
|
vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
|
1994-06-29 10:29:24 +04:00
|
|
|
VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
|
1999-02-27 02:38:55 +03:00
|
|
|
vput(pack.ep_vp);
|
1998-08-13 06:10:37 +04:00
|
|
|
uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
|
1994-06-29 10:29:24 +04:00
|
|
|
FREE(pack.ep_hdr, M_EXEC);
|
|
|
|
exit1(p, W_EXITCODE(0, SIGABRT));
|
|
|
|
exit1(p, -1);
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
return 0;
|
|
|
|
}
|
1995-04-22 23:42:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
copyargs(pack, arginfo, stack, argp)
|
|
|
|
struct exec_package *pack;
|
|
|
|
struct ps_strings *arginfo;
|
|
|
|
void *stack;
|
|
|
|
void *argp;
|
|
|
|
{
|
|
|
|
char **cpp = stack;
|
|
|
|
char *dp, *sp;
|
|
|
|
size_t len;
|
|
|
|
void *nullp = NULL;
|
|
|
|
int argc = arginfo->ps_nargvstr;
|
|
|
|
int envc = arginfo->ps_nenvstr;
|
|
|
|
|
|
|
|
if (copyout(&argc, cpp++, sizeof(argc)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
|
|
|
|
sp = argp;
|
|
|
|
|
|
|
|
/* XXX don't copy them out, remap them! */
|
1995-05-16 18:19:03 +04:00
|
|
|
arginfo->ps_argvstr = cpp; /* remember location of argv for later */
|
1995-04-22 23:42:47 +04:00
|
|
|
|
|
|
|
for (; --argc >= 0; sp += len, dp += len)
|
|
|
|
if (copyout(&dp, cpp++, sizeof(dp)) ||
|
|
|
|
copyoutstr(sp, dp, ARG_MAX, &len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (copyout(&nullp, cpp++, sizeof(nullp)))
|
|
|
|
return NULL;
|
|
|
|
|
1995-05-16 18:19:03 +04:00
|
|
|
arginfo->ps_envstr = cpp; /* remember location of envp for later */
|
1995-04-22 23:42:47 +04:00
|
|
|
|
|
|
|
for (; --envc >= 0; sp += len, dp += len)
|
|
|
|
if (copyout(&dp, cpp++, sizeof(dp)) ||
|
|
|
|
copyoutstr(sp, dp, ARG_MAX, &len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (copyout(&nullp, cpp++, sizeof(nullp)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return cpp;
|
|
|
|
}
|