2007-11-07 03:23:13 +03:00
|
|
|
/* $NetBSD: kern_systrace.c,v 1.76 2007/11/07 00:23:22 ad Exp $ */
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/*
|
2003-03-30 04:40:05 +04:00
|
|
|
* Copyright 2002, 2003 Niels Provos <provos@citi.umich.edu>
|
2002-06-17 20:22:50 +04:00
|
|
|
* 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 Niels Provos.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
|
|
|
|
*/
|
2002-06-18 05:24:15 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
2007-11-07 03:23:13 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_systrace.c,v 1.76 2007/11/07 00:23:22 ad Exp $");
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
#include "opt_systrace.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/tree.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#include <sys/signalvar.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/mutex.h>
|
2002-06-17 20:22:50 +04:00
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/poll.h>
|
2002-06-27 17:22:54 +04:00
|
|
|
#include <sys/ptrace.h>
|
2003-08-25 13:12:42 +04:00
|
|
|
#include <sys/namei.h>
|
2002-06-17 20:22:50 +04:00
|
|
|
#include <sys/systrace.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
#include <compat/common/compat_util.h>
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
#define SYSTRACE_LOCK(fst, p) mutex_enter(&fst->mutex)
|
|
|
|
#define SYSTRACE_UNLOCK(fst, p) mutex_exit(&fst->mutex)
|
2007-02-15 18:08:59 +03:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
#ifndef M_XDATA
|
2003-08-25 13:12:42 +04:00
|
|
|
MALLOC_DEFINE(M_SYSTR, "systrace", "systrace");
|
|
|
|
#define M_XDATA M_SYSTR
|
2002-06-17 20:22:50 +04:00
|
|
|
#endif
|
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
dev_type_open(systraceopen);
|
|
|
|
#else
|
2002-06-18 05:24:15 +04:00
|
|
|
cdev_decl(systrace);
|
2002-09-06 17:18:43 +04:00
|
|
|
#endif
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
2006-05-15 01:15:11 +04:00
|
|
|
int systracef_read(struct file *, off_t *, struct uio *, kauth_cred_t,
|
2002-06-17 20:22:50 +04:00
|
|
|
int);
|
2006-05-15 01:15:11 +04:00
|
|
|
int systracef_write(struct file *, off_t *, struct uio *, kauth_cred_t,
|
2002-06-17 20:22:50 +04:00
|
|
|
int);
|
2005-12-11 15:16:03 +03:00
|
|
|
int systracef_poll(struct file *, int, struct lwp *);
|
|
|
|
int systracef_ioctl(struct file *, u_long, void *, struct lwp *);
|
|
|
|
int systracef_close(struct file *, struct lwp *);
|
2002-06-17 20:22:50 +04:00
|
|
|
#else
|
2006-05-15 01:15:11 +04:00
|
|
|
int systracef_read(struct file *, off_t *, struct uio *, kauth_cred_t);
|
|
|
|
int systracef_write(struct file *, off_t *, struct uio *, kauth_cred_t);
|
2002-07-02 20:16:33 +04:00
|
|
|
int systracef_select(struct file *, int, struct proc *);
|
2007-03-04 08:59:00 +03:00
|
|
|
int systracef_ioctl(struct file *, u_long, void *, struct proc *);
|
2005-12-11 15:16:03 +03:00
|
|
|
int systracef_stat(struct file *, struct stat *, struct proc *);
|
2003-06-30 02:28:00 +04:00
|
|
|
int systracef_close(struct file *, struct proc *);
|
2005-12-11 15:16:03 +03:00
|
|
|
#endif
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
struct str_policy {
|
|
|
|
TAILQ_ENTRY(str_policy) next;
|
|
|
|
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
const struct emul *emul; /* Is only valid for this emulation */
|
|
|
|
|
|
|
|
int refcount;
|
|
|
|
|
|
|
|
int nsysent;
|
|
|
|
u_char *sysent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define STR_PROC_ONQUEUE 0x01
|
|
|
|
#define STR_PROC_WAITANSWER 0x02
|
|
|
|
#define STR_PROC_SYSCALLRES 0x04
|
|
|
|
#define STR_PROC_REPORT 0x08 /* Report emulation */
|
2002-07-30 20:29:28 +04:00
|
|
|
#define STR_PROC_NEEDSEQNR 0x10 /* Answer must quote seqnr */
|
2002-10-12 01:54:55 +04:00
|
|
|
#define STR_PROC_SETEUID 0x20 /* Elevate privileges */
|
|
|
|
#define STR_PROC_SETEGID 0x40
|
2004-04-09 20:49:33 +04:00
|
|
|
#define STR_PROC_DIDSETUGID 0x80
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
struct str_process {
|
|
|
|
TAILQ_ENTRY(str_process) next;
|
|
|
|
|
|
|
|
struct proc *proc;
|
|
|
|
const struct emul *oldemul;
|
2002-10-08 18:46:24 +04:00
|
|
|
uid_t olduid;
|
|
|
|
gid_t oldgid;
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
struct fsystrace *parent;
|
|
|
|
struct str_policy *policy;
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
struct systrace_replace *replace;
|
2003-08-25 13:12:42 +04:00
|
|
|
char *fname[SYSTR_MAXFNAME];
|
|
|
|
size_t nfname;
|
2002-07-30 20:29:28 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
int flags;
|
|
|
|
short answer;
|
|
|
|
short error;
|
2005-12-26 21:41:36 +03:00
|
|
|
uint16_t seqnr; /* expected reply sequence number */
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
uid_t seteuid;
|
|
|
|
uid_t saveuid;
|
|
|
|
gid_t setegid;
|
|
|
|
gid_t savegid;
|
2005-06-27 21:11:20 +04:00
|
|
|
|
|
|
|
int isscript;
|
|
|
|
char scriptname[MAXPATHLEN];
|
2002-06-17 20:22:50 +04:00
|
|
|
};
|
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
uid_t systrace_seteuid(struct lwp *, uid_t);
|
|
|
|
gid_t systrace_setegid(struct lwp *, gid_t);
|
2002-06-17 20:22:50 +04:00
|
|
|
void systrace_lock(void);
|
|
|
|
void systrace_unlock(void);
|
|
|
|
|
|
|
|
/* Needs to be called with fst locked */
|
|
|
|
|
|
|
|
int systrace_attach(struct fsystrace *, pid_t);
|
|
|
|
int systrace_detach(struct str_process *);
|
|
|
|
int systrace_answer(struct str_process *, struct systrace_answer *);
|
2005-06-27 21:11:20 +04:00
|
|
|
int systrace_setscriptname(struct str_process *, struct
|
|
|
|
systrace_scriptname *);
|
2002-06-17 20:22:50 +04:00
|
|
|
int systrace_io(struct str_process *, struct systrace_io *);
|
|
|
|
int systrace_policy(struct fsystrace *, struct systrace_policy *);
|
2002-07-30 20:29:28 +04:00
|
|
|
int systrace_preprepl(struct str_process *, struct systrace_replace *);
|
|
|
|
int systrace_replace(struct str_process *, size_t, register_t []);
|
2002-06-17 20:22:50 +04:00
|
|
|
int systrace_getcwd(struct fsystrace *, struct str_process *);
|
2007-03-04 08:59:00 +03:00
|
|
|
int systrace_fname(struct str_process *, void *, size_t);
|
2003-08-25 13:12:42 +04:00
|
|
|
void systrace_replacefree(struct str_process *);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
int systrace_processready(struct str_process *);
|
|
|
|
struct proc *systrace_find(struct str_process *);
|
|
|
|
struct str_process *systrace_findpid(struct fsystrace *fst, pid_t pid);
|
|
|
|
void systrace_wakeup(struct fsystrace *);
|
|
|
|
void systrace_closepolicy(struct fsystrace *, struct str_policy *);
|
2003-03-30 04:40:05 +04:00
|
|
|
int systrace_insert_process(struct fsystrace *, struct proc *,
|
|
|
|
struct str_process **);
|
2002-06-17 20:22:50 +04:00
|
|
|
struct str_policy *systrace_newpolicy(struct fsystrace *, int);
|
|
|
|
int systrace_msg_child(struct fsystrace *, struct str_process *, pid_t);
|
2003-06-03 09:24:00 +04:00
|
|
|
int systrace_msg_policyfree(struct fsystrace *, struct str_policy *);
|
2002-06-17 20:22:50 +04:00
|
|
|
int systrace_msg_ask(struct fsystrace *, struct str_process *,
|
|
|
|
int, size_t, register_t []);
|
|
|
|
int systrace_msg_result(struct fsystrace *, struct str_process *,
|
|
|
|
int, int, size_t, register_t [], register_t []);
|
|
|
|
int systrace_msg_emul(struct fsystrace *, struct str_process *);
|
2002-10-08 18:46:24 +04:00
|
|
|
int systrace_msg_ugid(struct fsystrace *, struct str_process *);
|
2003-03-30 04:40:05 +04:00
|
|
|
int systrace_make_msg(struct str_process *, int, struct str_message *);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2004-11-30 07:25:43 +03:00
|
|
|
static const struct fileops systracefops = {
|
2002-06-17 20:22:50 +04:00
|
|
|
systracef_read,
|
|
|
|
systracef_write,
|
|
|
|
systracef_ioctl,
|
2004-11-30 07:25:43 +03:00
|
|
|
fnullop_fcntl,
|
2002-07-02 20:16:33 +04:00
|
|
|
systracef_poll,
|
2004-11-30 07:25:43 +03:00
|
|
|
fbadop_stat,
|
|
|
|
systracef_close,
|
|
|
|
fnullop_kqfilter
|
2002-06-17 20:22:50 +04:00
|
|
|
};
|
|
|
|
|
2004-04-25 20:42:40 +04:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
POOL_INIT(systr_proc_pl, sizeof(struct str_process), 0, 0, 0, "strprocpl",
|
2007-03-12 21:18:22 +03:00
|
|
|
NULL, IPL_NONE);
|
2004-04-25 20:42:40 +04:00
|
|
|
POOL_INIT(systr_policy_pl, sizeof(struct str_policy), 0, 0, 0, "strpolpl",
|
2007-03-12 21:18:22 +03:00
|
|
|
NULL, IPL_NONE);
|
2004-04-25 20:42:40 +04:00
|
|
|
POOL_INIT(systr_msgcontainer_pl, sizeof(struct str_msgcontainer), 0, 0, 0,
|
2007-03-12 21:18:22 +03:00
|
|
|
"strmsgpl", NULL, IPL_NONE);
|
2005-11-25 23:32:32 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
kmutex_t systrace_mutex;
|
2005-11-25 23:32:32 +03:00
|
|
|
#else /* ! __NetBSD__ */
|
2002-06-17 20:22:50 +04:00
|
|
|
struct pool systr_proc_pl;
|
|
|
|
struct pool systr_policy_pl;
|
2003-03-30 04:40:05 +04:00
|
|
|
struct pool systr_msgcontainer_pl;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
struct lock systrace_lck;
|
2005-11-25 23:32:32 +03:00
|
|
|
#endif /* __NetBSD__ */
|
|
|
|
|
|
|
|
int systrace_debug = 0;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-09-06 17:18:43 +04:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
const struct cdevsw systrace_cdevsw = {
|
|
|
|
systraceopen, noclose, noread, nowrite, noioctl,
|
2006-09-02 10:35:49 +04:00
|
|
|
nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
|
2002-09-06 17:18:43 +04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
#define DPRINTF(y) if (systrace_debug) printf y;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systracef_read(struct file *fp, off_t *poff, struct uio *uio, kauth_cred_t cred,
|
|
|
|
int flags)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
|
|
|
struct fsystrace *fst = (struct fsystrace *)fp->f_data;
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_msgcontainer *cont;
|
2002-06-17 20:22:50 +04:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (uio->uio_resid != sizeof(struct str_message))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
again:
|
|
|
|
systrace_lock();
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
systrace_unlock();
|
2003-03-30 04:40:05 +04:00
|
|
|
if ((cont = TAILQ_FIRST(&fst->messages)) != NULL) {
|
2007-03-04 08:59:00 +03:00
|
|
|
error = uiomove((void *)&cont->msg,
|
2002-06-17 20:22:50 +04:00
|
|
|
sizeof(struct str_message), uio);
|
|
|
|
if (!error) {
|
2003-03-30 04:40:05 +04:00
|
|
|
TAILQ_REMOVE(&fst->messages, cont, next);
|
|
|
|
if (!SYSTR_MSG_NOPROCESS(cont))
|
|
|
|
CLR(cont->strp->flags, STR_PROC_ONQUEUE);
|
|
|
|
pool_put(&systr_msgcontainer_pl, cont);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
} else if (TAILQ_FIRST(&fst->processes) == NULL) {
|
|
|
|
/* EOF situation */
|
|
|
|
;
|
|
|
|
} else {
|
|
|
|
if (fp->f_flag & FNONBLOCK)
|
|
|
|
error = EAGAIN;
|
|
|
|
else {
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
error = tsleep(fst, PWAIT|PCATCH, "systrrd", 0);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
out:
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systracef_write(struct file *fp, off_t *poff, struct uio *uio,
|
|
|
|
kauth_cred_t cred, int flags)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2006-11-01 13:17:58 +03:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define POLICY_VALID(x) ((x) == SYSTR_POLICY_PERMIT || \
|
|
|
|
(x) == SYSTR_POLICY_ASK || \
|
|
|
|
(x) == SYSTR_POLICY_NEVER)
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
systracef_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2007-06-08 21:51:41 +04:00
|
|
|
struct fsystrace *fst = fp->f_data;
|
2002-06-17 20:22:50 +04:00
|
|
|
#ifdef __NetBSD__
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct cwdinfo *cwdp;
|
|
|
|
#else
|
|
|
|
struct filedesc *fdp;
|
|
|
|
#endif
|
2002-11-24 14:37:54 +03:00
|
|
|
struct str_process *strp = NULL;
|
2002-06-17 20:22:50 +04:00
|
|
|
pid_t pid = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FIONBIO:
|
2002-06-18 05:24:15 +04:00
|
|
|
case FIOASYNC:
|
|
|
|
return (0);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
case STRIOCDETACH:
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCREPORT:
|
2007-06-08 21:51:41 +04:00
|
|
|
case STRIOCGETCWD:
|
2002-06-17 20:22:50 +04:00
|
|
|
pid = *(pid_t *)data;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCANSWER:
|
2002-06-17 20:22:50 +04:00
|
|
|
pid = ((struct systrace_answer *)data)->stra_pid;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCIO:
|
2002-06-17 20:22:50 +04:00
|
|
|
pid = ((struct systrace_io *)data)->strio_pid;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
2005-06-27 21:11:20 +04:00
|
|
|
case STRIOCSCRIPTNAME:
|
|
|
|
pid = ((struct systrace_scriptname *)data)->sn_pid;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCATTACH:
|
|
|
|
case STRIOCRESCWD:
|
2002-06-17 20:22:50 +04:00
|
|
|
case STRIOCPOLICY:
|
|
|
|
break;
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCREPLACE:
|
|
|
|
pid = ((struct systrace_replace *)data)->strr_pid;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
2002-06-17 20:22:50 +04:00
|
|
|
default:
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return (ret);
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_lock();
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_unlock();
|
2002-06-17 20:22:50 +04:00
|
|
|
if (pid) {
|
|
|
|
strp = systrace_findpid(fst, pid);
|
|
|
|
if (strp == NULL) {
|
|
|
|
ret = ESRCH;
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCATTACH:
|
|
|
|
pid = *(pid_t *)data;
|
|
|
|
if (!pid)
|
|
|
|
ret = EINVAL;
|
|
|
|
else
|
|
|
|
ret = systrace_attach(fst, pid);
|
|
|
|
DPRINTF(("%s: attach to %u: %d\n", __func__, pid, ret));
|
|
|
|
break;
|
2002-06-17 20:22:50 +04:00
|
|
|
case STRIOCDETACH:
|
|
|
|
ret = systrace_detach(strp);
|
|
|
|
break;
|
|
|
|
case STRIOCREPORT:
|
|
|
|
SET(strp->flags, STR_PROC_REPORT);
|
|
|
|
break;
|
|
|
|
case STRIOCANSWER:
|
|
|
|
ret = systrace_answer(strp, (struct systrace_answer *)data);
|
|
|
|
break;
|
|
|
|
case STRIOCIO:
|
|
|
|
ret = systrace_io(strp, (struct systrace_io *)data);
|
|
|
|
break;
|
2005-06-27 21:11:20 +04:00
|
|
|
case STRIOCSCRIPTNAME:
|
|
|
|
ret = systrace_setscriptname(strp,
|
|
|
|
(struct systrace_scriptname *)data);
|
|
|
|
break;
|
2002-06-17 20:22:50 +04:00
|
|
|
case STRIOCPOLICY:
|
|
|
|
ret = systrace_policy(fst, (struct systrace_policy *)data);
|
|
|
|
break;
|
2002-07-30 20:29:28 +04:00
|
|
|
case STRIOCREPLACE:
|
|
|
|
ret = systrace_preprepl(strp, (struct systrace_replace *)data);
|
|
|
|
break;
|
|
|
|
case STRIOCRESCWD:
|
|
|
|
if (!fst->fd_pid) {
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
cwdp = p->p_cwdi;
|
|
|
|
|
|
|
|
/* Release cwd from other process */
|
|
|
|
if (cwdp->cwdi_cdir)
|
|
|
|
vrele(cwdp->cwdi_cdir);
|
|
|
|
if (cwdp->cwdi_rdir)
|
|
|
|
vrele(cwdp->cwdi_rdir);
|
|
|
|
cwdp->cwdi_cdir = fst->fd_cdir;
|
|
|
|
cwdp->cwdi_rdir = fst->fd_rdir;
|
|
|
|
#else
|
|
|
|
fdp = p->p_fd;
|
|
|
|
|
|
|
|
/* Release cwd from other process */
|
|
|
|
if (fdp->fd_cdir)
|
|
|
|
vrele(fdp->fd_cdir);
|
|
|
|
if (fdp->fd_rdir)
|
|
|
|
vrele(fdp->fd_rdir);
|
|
|
|
/* This restores the cwd we had before */
|
|
|
|
fdp->fd_cdir = fst->fd_cdir;
|
|
|
|
fdp->fd_rdir = fst->fd_rdir;
|
|
|
|
#endif
|
|
|
|
/* Note that we are normal again */
|
|
|
|
fst->fd_pid = 0;
|
|
|
|
fst->fd_cdir = fst->fd_rdir = NULL;
|
|
|
|
break;
|
2002-06-17 20:22:50 +04:00
|
|
|
case STRIOCGETCWD:
|
|
|
|
ret = systrace_getcwd(fst, strp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-07 03:23:13 +03:00
|
|
|
if (strp)
|
|
|
|
rw_exit(&strp->proc->p_reflock);
|
2002-06-17 20:22:50 +04:00
|
|
|
unlock:
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2002-07-02 20:16:33 +04:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
systracef_poll(struct file *fp, int events, struct lwp *l)
|
2002-07-02 20:16:33 +04:00
|
|
|
{
|
|
|
|
struct fsystrace *fst = (struct fsystrace *)fp->f_data;
|
|
|
|
int revents = 0;
|
|
|
|
|
|
|
|
if ((events & (POLLIN | POLLRDNORM)) == 0)
|
|
|
|
return (revents);
|
|
|
|
|
2002-07-20 03:55:01 +04:00
|
|
|
systrace_lock();
|
2005-12-11 15:16:03 +03:00
|
|
|
SYSTRACE_LOCK(fst, l->l_proc);
|
2002-07-20 03:55:01 +04:00
|
|
|
systrace_unlock();
|
2002-07-30 20:29:28 +04:00
|
|
|
if (!TAILQ_EMPTY(&fst->messages))
|
2002-07-02 20:16:33 +04:00
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
2002-07-30 20:29:28 +04:00
|
|
|
if (revents == 0)
|
2005-12-11 15:16:03 +03:00
|
|
|
selrecord(l, &fst->si);
|
|
|
|
SYSTRACE_UNLOCK(fst, l->l_proc);
|
2002-07-02 20:16:33 +04:00
|
|
|
|
|
|
|
return (revents);
|
|
|
|
}
|
|
|
|
#else
|
2002-06-17 20:22:50 +04:00
|
|
|
int
|
|
|
|
systracef_select(struct file *fp, int which, struct proc *p)
|
|
|
|
{
|
|
|
|
struct fsystrace *fst = (struct fsystrace *)fp->f_data;
|
|
|
|
int ready = 0;
|
|
|
|
|
|
|
|
if (which != FREAD)
|
|
|
|
return (0);
|
|
|
|
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_lock();
|
2002-06-17 20:22:50 +04:00
|
|
|
SYSTRACE_LOCK(fst, p);
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_unlock();
|
2002-06-17 20:22:50 +04:00
|
|
|
ready = TAILQ_FIRST(&fst->messages) != NULL;
|
|
|
|
if (!ready)
|
|
|
|
selrecord(p, &fst->si);
|
|
|
|
SYSTRACE_UNLOCK(fst, p);
|
|
|
|
|
|
|
|
return (ready);
|
|
|
|
}
|
2002-07-02 20:16:33 +04:00
|
|
|
#endif /* __NetBSD__ */
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systracef_close(struct file *fp, struct lwp *l)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
|
|
|
struct fsystrace *fst = (struct fsystrace *)fp->f_data;
|
|
|
|
struct str_process *strp;
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_msgcontainer *cont;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct str_policy *strpol;
|
|
|
|
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_lock();
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
2002-07-21 04:25:01 +04:00
|
|
|
systrace_unlock();
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/* Untrace all processes */
|
|
|
|
for (strp = TAILQ_FIRST(&fst->processes); strp;
|
|
|
|
strp = TAILQ_FIRST(&fst->processes)) {
|
|
|
|
struct proc *q = strp->proc;
|
|
|
|
|
|
|
|
systrace_detach(strp);
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_enter(&proclist_lock); /* XXXSMP */
|
2002-06-17 20:22:50 +04:00
|
|
|
psignal(q, SIGKILL);
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock); /* XXXSMP */
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up fork and exit messages */
|
2003-03-30 04:40:05 +04:00
|
|
|
for (cont = TAILQ_FIRST(&fst->messages); cont;
|
|
|
|
cont = TAILQ_FIRST(&fst->messages)) {
|
|
|
|
TAILQ_REMOVE(&fst->messages, cont, next);
|
|
|
|
pool_put(&systr_msgcontainer_pl, cont);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up all policies */
|
|
|
|
for (strpol = TAILQ_FIRST(&fst->policies); strpol;
|
|
|
|
strpol = TAILQ_FIRST(&fst->policies))
|
|
|
|
systrace_closepolicy(fst, strpol);
|
|
|
|
|
|
|
|
/* Release vnodes */
|
|
|
|
if (fst->fd_cdir)
|
|
|
|
vrele(fst->fd_cdir);
|
|
|
|
if (fst->fd_rdir)
|
|
|
|
vrele(fst->fd_rdir);
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-09-25 18:04:07 +04:00
|
|
|
seldestroy(&fst->si);
|
2002-06-17 20:22:50 +04:00
|
|
|
FREE(fp->f_data, M_XDATA);
|
|
|
|
fp->f_data = NULL;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_lock(void)
|
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_enter(&systrace_mutex);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_unlock(void)
|
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&systrace_mutex);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-06-18 05:24:15 +04:00
|
|
|
systrace_init(void)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_init(&systrace_mutex, MUTEX_DEFAULT, IPL_NONE);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systraceopen(dev_t dev, int flag, int mode, struct lwp *l)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2002-06-18 05:24:15 +04:00
|
|
|
struct fsystrace *fst;
|
|
|
|
struct file *fp;
|
|
|
|
int error, fd;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-06-18 05:24:15 +04:00
|
|
|
/* falloc() will use the descriptor for us. */
|
2006-07-24 02:06:03 +04:00
|
|
|
if ((error = falloc(l, &fp, &fd)) != 0)
|
2002-06-18 05:24:15 +04:00
|
|
|
return (error);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-06-18 05:24:15 +04:00
|
|
|
MALLOC(fst, struct fsystrace *, sizeof(*fst), M_XDATA, M_WAITOK);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-06-18 05:24:15 +04:00
|
|
|
memset(fst, 0, sizeof(struct fsystrace));
|
2007-02-10 00:55:00 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
mutex_init(&fst->mutex, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
#else
|
2002-06-18 05:24:15 +04:00
|
|
|
lockinit(&fst->lock, PLOCK, "systrace", 0, 0);
|
2007-02-10 00:55:00 +03:00
|
|
|
#endif
|
2007-09-25 18:04:07 +04:00
|
|
|
selinit(&fst->si);
|
2002-06-18 05:24:15 +04:00
|
|
|
TAILQ_INIT(&fst->processes);
|
|
|
|
TAILQ_INIT(&fst->messages);
|
|
|
|
TAILQ_INIT(&fst->policies);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2006-07-24 02:06:03 +04:00
|
|
|
if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
|
2007-01-04 19:55:29 +03:00
|
|
|
NULL) == 0)
|
2002-06-18 05:24:15 +04:00
|
|
|
fst->issuser = 1;
|
2006-07-24 02:06:03 +04:00
|
|
|
fst->p_ruid = kauth_cred_getuid(l->l_cred);
|
|
|
|
fst->p_rgid = kauth_cred_getgid(l->l_cred);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
return fdclone(l, fp, fd, flag, &systracefops, fst);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_wakeup(struct fsystrace *fst)
|
|
|
|
{
|
2007-03-04 08:59:00 +03:00
|
|
|
wakeup((void *)fst);
|
2002-06-17 20:22:50 +04:00
|
|
|
selwakeup(&fst->si);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct proc *
|
|
|
|
systrace_find(struct str_process *strp)
|
|
|
|
{
|
|
|
|
struct proc *proc;
|
2007-02-10 00:55:00 +03:00
|
|
|
int error;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_enter(&proclist_lock);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((proc = p_find(strp->pid, PFIND_LOCKED)) == NULL) {
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock);
|
2002-06-17 20:22:50 +04:00
|
|
|
return (NULL);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_enter(&proc->p_mutex);
|
2007-02-18 01:31:36 +03:00
|
|
|
if (proc != strp->proc || !ISSET(proc->p_flag, PK_SYSTRACE)) {
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proc->p_mutex);
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock);
|
2002-06-17 20:22:50 +04:00
|
|
|
return (NULL);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2007-11-07 03:23:13 +03:00
|
|
|
if (!rw_tryenter(&proc->p_reflock, RW_READER))
|
|
|
|
error = EBUSY;
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proc->p_mutex);
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
return (error ? NULL : proc);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_sys_exit(struct proc *proc)
|
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
struct fsystrace *fst;
|
|
|
|
|
|
|
|
systrace_lock();
|
|
|
|
strp = proc->p_systrace;
|
|
|
|
if (strp != NULL) {
|
|
|
|
fst = strp->parent;
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
systrace_unlock();
|
|
|
|
|
|
|
|
/* Insert Exit message */
|
|
|
|
systrace_msg_child(fst, strp, -1);
|
|
|
|
|
|
|
|
systrace_detach(strp);
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
} else
|
|
|
|
systrace_unlock();
|
2007-02-18 01:31:36 +03:00
|
|
|
CLR(proc->p_flag, PK_SYSTRACE);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_sys_fork(struct proc *oldproc, struct proc *p)
|
|
|
|
{
|
|
|
|
struct str_process *oldstrp, *strp;
|
|
|
|
struct fsystrace *fst;
|
|
|
|
|
|
|
|
systrace_lock();
|
|
|
|
oldstrp = oldproc->p_systrace;
|
|
|
|
if (oldstrp == NULL) {
|
|
|
|
systrace_unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fst = oldstrp->parent;
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
systrace_unlock();
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
if (systrace_insert_process(fst, p, &strp)) {
|
|
|
|
/* We need to kill the child */
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_enter(&proclist_lock); /* XXXSMP */
|
2003-03-30 04:40:05 +04:00
|
|
|
psignal(p, SIGKILL);
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock); /* XXXSMP */
|
2002-06-17 20:22:50 +04:00
|
|
|
goto out;
|
2003-03-30 04:40:05 +04:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/* Reference policy */
|
|
|
|
if ((strp->policy = oldstrp->policy) != NULL)
|
|
|
|
strp->policy->refcount++;
|
|
|
|
|
|
|
|
/* Insert fork message */
|
|
|
|
systrace_msg_child(fst, oldstrp, p->p_pid);
|
|
|
|
out:
|
2003-01-18 13:06:22 +03:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_enter(struct lwp *l, register_t code, void *v)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
|
|
|
const struct sysent *callp;
|
|
|
|
struct str_process *strp;
|
|
|
|
struct str_policy *strpolicy;
|
|
|
|
struct fsystrace *fst;
|
2006-07-20 01:11:37 +04:00
|
|
|
struct proc *p = l->l_proc;
|
2006-05-15 01:15:11 +04:00
|
|
|
kauth_cred_t pc;
|
2003-10-30 04:58:17 +03:00
|
|
|
int policy, error = 0, maycontrol = 0, issuser = 0;
|
2004-01-16 08:03:02 +03:00
|
|
|
size_t argsize;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
|
|
|
if (strp == NULL) {
|
|
|
|
systrace_unlock();
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT(strp->proc == p);
|
|
|
|
|
|
|
|
fst = strp->parent;
|
|
|
|
|
|
|
|
SYSTRACE_LOCK(fst, p);
|
|
|
|
systrace_unlock();
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
/*
|
|
|
|
* We can not monitor a SUID process unless we are root,
|
|
|
|
* but we wait until it executes something unprivileged.
|
|
|
|
* A non-root user may only monitor if the real uid and
|
|
|
|
* real gid match the monitored process. Changing the
|
|
|
|
* uid or gid causes P_SUGID to be set.
|
|
|
|
*/
|
2002-10-12 01:54:55 +04:00
|
|
|
if (fst->issuser) {
|
2002-07-30 20:29:28 +04:00
|
|
|
maycontrol = 1;
|
2002-10-12 01:54:55 +04:00
|
|
|
issuser = 1;
|
2007-02-18 01:31:36 +03:00
|
|
|
} else if (!(p->p_flag & PK_SUGID)) {
|
2006-05-15 01:15:11 +04:00
|
|
|
maycontrol = fst->p_ruid == kauth_cred_getuid(p->p_cred) &&
|
|
|
|
fst->p_rgid == kauth_cred_getgid(p->p_cred);
|
2002-07-30 20:29:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!maycontrol) {
|
2002-06-17 20:22:50 +04:00
|
|
|
policy = SYSTR_POLICY_PERMIT;
|
|
|
|
} else {
|
|
|
|
/* Find out current policy */
|
|
|
|
if ((strpolicy = strp->policy) == NULL)
|
|
|
|
policy = SYSTR_POLICY_ASK;
|
|
|
|
else {
|
|
|
|
if (code >= strpolicy->nsysent)
|
|
|
|
policy = SYSTR_POLICY_NEVER;
|
|
|
|
else
|
|
|
|
policy = strpolicy->sysent[code];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callp = p->p_emul->e_sysent + code;
|
2003-04-21 00:30:34 +04:00
|
|
|
|
|
|
|
/* Fast-path */
|
|
|
|
if (policy != SYSTR_POLICY_ASK) {
|
|
|
|
if (policy != SYSTR_POLICY_PERMIT) {
|
|
|
|
if (policy > 0)
|
|
|
|
error = policy;
|
|
|
|
else
|
|
|
|
error = EPERM;
|
|
|
|
}
|
|
|
|
strp->oldemul = NULL;
|
2003-08-25 13:12:42 +04:00
|
|
|
systrace_replacefree(strp);
|
2003-04-21 00:30:34 +04:00
|
|
|
SYSTRACE_UNLOCK(fst, p);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-01-16 08:03:02 +03:00
|
|
|
/* Get the (adjusted) argsize */
|
|
|
|
argsize = callp->sy_argsize;
|
|
|
|
#ifdef _LP64
|
2007-02-18 01:31:36 +03:00
|
|
|
if (p->p_flag & PK_32)
|
2004-01-16 08:03:02 +03:00
|
|
|
argsize = argsize << 1;
|
|
|
|
#endif
|
|
|
|
|
2003-04-21 00:30:34 +04:00
|
|
|
/* Puts the current process to sleep, return unlocked */
|
2004-01-16 08:03:02 +03:00
|
|
|
error = systrace_msg_ask(fst, strp, code, argsize, v);
|
2003-04-21 00:30:34 +04:00
|
|
|
|
|
|
|
/* lock has been released in systrace_msg_ask() */
|
|
|
|
fst = NULL;
|
|
|
|
/* We might have detached by now for some reason */
|
|
|
|
if (!error && (strp = p->p_systrace) != NULL) {
|
|
|
|
/* XXX - do I need to lock here? */
|
|
|
|
if (strp->answer == SYSTR_POLICY_NEVER) {
|
|
|
|
error = strp->error;
|
2003-08-25 13:12:42 +04:00
|
|
|
systrace_replacefree(strp);
|
2003-04-21 00:30:34 +04:00
|
|
|
} else {
|
|
|
|
if (ISSET(strp->flags, STR_PROC_SYSCALLRES)) {
|
2002-07-30 20:29:28 +04:00
|
|
|
#ifndef __NetBSD__
|
2003-04-21 00:30:34 +04:00
|
|
|
CLR(strp->flags, STR_PROC_SYSCALLRES);
|
2002-07-30 20:29:28 +04:00
|
|
|
#endif
|
2003-04-21 00:30:34 +04:00
|
|
|
}
|
|
|
|
/* Replace the arguments if necessary */
|
|
|
|
if (strp->replace != NULL) {
|
2004-01-16 08:03:02 +03:00
|
|
|
error = systrace_replace(strp, argsize, v);
|
2002-07-30 20:29:28 +04:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
}
|
2002-10-08 18:46:24 +04:00
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
systrace_lock();
|
|
|
|
if ((strp = p->p_systrace) == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
strp->oldemul = NULL;
|
|
|
|
goto out;
|
2002-07-02 20:22:28 +04:00
|
|
|
}
|
2002-10-12 01:54:55 +04:00
|
|
|
|
|
|
|
pc = p->p_cred;
|
|
|
|
strp->oldemul = p->p_emul;
|
2006-05-15 01:15:11 +04:00
|
|
|
strp->olduid = kauth_cred_getuid(pc);
|
|
|
|
strp->oldgid = kauth_cred_getgid(pc);
|
2002-10-12 01:54:55 +04:00
|
|
|
|
|
|
|
/* Elevate privileges as desired */
|
|
|
|
if (issuser) {
|
2004-04-09 20:49:33 +04:00
|
|
|
if (ISSET(strp->flags, STR_PROC_SETEUID)) {
|
2006-07-20 01:11:37 +04:00
|
|
|
strp->saveuid = systrace_seteuid(l, strp->seteuid);
|
2004-04-09 20:49:33 +04:00
|
|
|
SET(strp->flags, STR_PROC_DIDSETUGID);
|
|
|
|
}
|
|
|
|
if (ISSET(strp->flags, STR_PROC_SETEGID)) {
|
2006-07-20 01:11:37 +04:00
|
|
|
strp->savegid = systrace_setegid(l, strp->setegid);
|
2004-04-09 20:49:33 +04:00
|
|
|
SET(strp->flags, STR_PROC_DIDSETUGID);
|
|
|
|
}
|
2002-10-12 01:54:55 +04:00
|
|
|
} else
|
2004-04-09 20:49:33 +04:00
|
|
|
CLR(strp->flags,
|
|
|
|
STR_PROC_SETEUID|STR_PROC_SETEGID|STR_PROC_DIDSETUGID);
|
2002-10-12 01:54:55 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
systrace_unlock();
|
2002-10-08 18:46:24 +04:00
|
|
|
return (error);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_exit(struct lwp *l, register_t code, void *v, register_t retval[],
|
2002-06-17 20:22:50 +04:00
|
|
|
int error)
|
|
|
|
{
|
|
|
|
const struct sysent *callp;
|
|
|
|
struct str_process *strp;
|
|
|
|
struct fsystrace *fst;
|
2006-07-20 01:11:37 +04:00
|
|
|
struct proc *p = l->l_proc;
|
2006-05-15 01:15:11 +04:00
|
|
|
kauth_cred_t pc;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
/* Report change in emulation */
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
|
|
|
if (strp == NULL || strp->oldemul == NULL) {
|
|
|
|
systrace_unlock();
|
|
|
|
return;
|
|
|
|
}
|
2002-08-08 18:43:40 +04:00
|
|
|
DPRINTF(("exit syscall %lu, oldemul %p\n", (u_long)code, strp->oldemul));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
/* Return to old privileges */
|
|
|
|
pc = p->p_cred;
|
2004-04-09 20:49:33 +04:00
|
|
|
if (ISSET(strp->flags, STR_PROC_DIDSETUGID)) {
|
|
|
|
if (ISSET(strp->flags, STR_PROC_SETEUID)) {
|
2006-05-15 01:15:11 +04:00
|
|
|
if (kauth_cred_geteuid(pc) == strp->seteuid)
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_seteuid(l, strp->saveuid);
|
2004-04-09 20:49:33 +04:00
|
|
|
}
|
|
|
|
if (ISSET(strp->flags, STR_PROC_SETEGID)) {
|
2006-05-15 01:15:11 +04:00
|
|
|
if (kauth_cred_getegid(pc) == strp->setegid)
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_setegid(l, strp->savegid);
|
2004-04-09 20:49:33 +04:00
|
|
|
}
|
2002-10-12 01:54:55 +04:00
|
|
|
}
|
2004-04-09 20:49:33 +04:00
|
|
|
CLR(strp->flags,
|
|
|
|
STR_PROC_SETEUID|STR_PROC_SETEGID|STR_PROC_DIDSETUGID);
|
2003-08-25 13:12:42 +04:00
|
|
|
|
|
|
|
systrace_replacefree(strp);
|
|
|
|
|
2007-02-18 01:31:36 +03:00
|
|
|
if (p->p_flag & PK_SUGID) {
|
2002-06-17 20:22:50 +04:00
|
|
|
if ((fst = strp->parent) == NULL || !fst->issuser) {
|
|
|
|
systrace_unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we should force a report */
|
|
|
|
if (ISSET(strp->flags, STR_PROC_REPORT)) {
|
|
|
|
CLR(strp->flags, STR_PROC_REPORT);
|
|
|
|
strp->oldemul = NULL;
|
|
|
|
}
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
if (p->p_emul != strp->oldemul && strp != NULL) {
|
2002-06-17 20:22:50 +04:00
|
|
|
fst = strp->parent;
|
|
|
|
SYSTRACE_LOCK(fst, p);
|
|
|
|
systrace_unlock();
|
|
|
|
|
|
|
|
/* Old policy is without meaning now */
|
|
|
|
if (strp->policy) {
|
|
|
|
systrace_closepolicy(fst, strp->policy);
|
|
|
|
strp->policy = NULL;
|
|
|
|
}
|
|
|
|
systrace_msg_emul(fst, strp);
|
|
|
|
} else
|
|
|
|
systrace_unlock();
|
|
|
|
|
2002-10-08 18:46:24 +04:00
|
|
|
/* Report if effective uid or gid changed */
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
2006-05-15 01:15:11 +04:00
|
|
|
if (strp != NULL && (strp->olduid != kauth_cred_getuid(p->p_cred) ||
|
|
|
|
strp->oldgid != kauth_cred_getgid(p->p_cred))) {
|
2002-10-08 18:46:24 +04:00
|
|
|
|
|
|
|
fst = strp->parent;
|
|
|
|
SYSTRACE_LOCK(fst, p);
|
|
|
|
systrace_unlock();
|
|
|
|
|
|
|
|
systrace_msg_ugid(fst, strp);
|
|
|
|
} else
|
|
|
|
systrace_unlock();
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
/* Report result from system call */
|
|
|
|
systrace_lock();
|
2002-07-30 20:29:28 +04:00
|
|
|
strp = p->p_systrace;
|
|
|
|
if (strp != NULL && ISSET(strp->flags, STR_PROC_SYSCALLRES)) {
|
2004-01-16 08:03:02 +03:00
|
|
|
size_t argsize;
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
CLR(strp->flags, STR_PROC_SYSCALLRES);
|
2002-06-17 20:22:50 +04:00
|
|
|
fst = strp->parent;
|
|
|
|
SYSTRACE_LOCK(fst, p);
|
|
|
|
systrace_unlock();
|
2002-08-08 18:43:40 +04:00
|
|
|
DPRINTF(("will ask syscall %lu, strp %p\n", (u_long)code, strp));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2004-01-16 08:03:02 +03:00
|
|
|
/* Get the (adjusted) argsize */
|
2002-06-17 20:22:50 +04:00
|
|
|
callp = p->p_emul->e_sysent + code;
|
2004-01-16 08:03:02 +03:00
|
|
|
argsize = callp->sy_argsize;
|
|
|
|
#ifdef _LP64
|
2007-02-18 01:31:36 +03:00
|
|
|
if (p->p_flag & PK_32)
|
2004-01-16 08:03:02 +03:00
|
|
|
argsize = argsize << 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
systrace_msg_result(fst, strp, error, code, argsize, v, retval);
|
2002-06-17 20:22:50 +04:00
|
|
|
} else {
|
2002-08-08 18:43:40 +04:00
|
|
|
DPRINTF(("will not ask syscall %lu, strp %p\n", (u_long)code, strp));
|
2002-06-17 20:22:50 +04:00
|
|
|
systrace_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
uid_t
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_seteuid(struct lwp *l, uid_t euid)
|
2002-10-12 01:54:55 +04:00
|
|
|
{
|
2006-07-31 01:58:11 +04:00
|
|
|
struct proc *p = l->l_proc;
|
2007-02-10 00:55:00 +03:00
|
|
|
kauth_cred_t cred, ncred;
|
2006-05-15 01:15:11 +04:00
|
|
|
uid_t oeuid;
|
2002-10-12 01:54:55 +04:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
ncred = kauth_cred_alloc();
|
|
|
|
proc_crmod_enter();
|
2006-07-31 01:58:11 +04:00
|
|
|
cred = p->p_cred;
|
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
oeuid = kauth_cred_geteuid(cred);
|
2006-07-31 01:58:11 +04:00
|
|
|
if (oeuid == euid) {
|
2007-02-22 09:34:42 +03:00
|
|
|
proc_crmod_leave(cred, ncred, false);
|
2002-10-12 01:54:55 +04:00
|
|
|
return (oeuid);
|
2006-07-31 01:58:11 +04:00
|
|
|
}
|
2002-10-12 01:54:55 +04:00
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
/* Copy credentials so other references do not see our changes. */
|
2007-02-10 00:55:00 +03:00
|
|
|
kauth_cred_clone(cred, ncred);
|
|
|
|
kauth_cred_seteuid(ncred, euid);
|
2002-10-12 01:54:55 +04:00
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
/* Broadcast our credentials to the process and other LWPs. */
|
2007-02-22 09:34:42 +03:00
|
|
|
proc_crmod_leave(ncred, cred, true);
|
2006-07-20 01:11:37 +04:00
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
return (oeuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t
|
2006-07-20 01:11:37 +04:00
|
|
|
systrace_setegid(struct lwp *l, gid_t egid)
|
2002-10-12 01:54:55 +04:00
|
|
|
{
|
2006-07-31 01:58:11 +04:00
|
|
|
struct proc *p = l->l_proc;
|
2007-02-10 00:55:00 +03:00
|
|
|
kauth_cred_t cred, ncred;
|
2006-05-15 01:15:11 +04:00
|
|
|
gid_t oegid;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
ncred = kauth_cred_alloc();
|
|
|
|
proc_crmod_enter();
|
2006-07-31 01:58:11 +04:00
|
|
|
cred = p->p_cred;
|
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
oegid = kauth_cred_getegid(cred);
|
2006-07-31 01:58:11 +04:00
|
|
|
if (oegid == egid) {
|
2007-02-22 09:34:42 +03:00
|
|
|
proc_crmod_leave(cred, ncred, false);
|
2002-10-12 01:54:55 +04:00
|
|
|
return (oegid);
|
2006-07-31 01:58:11 +04:00
|
|
|
}
|
2002-10-12 01:54:55 +04:00
|
|
|
|
2006-07-20 01:11:37 +04:00
|
|
|
/* Copy credentials so other references do not see our changes. */
|
2007-02-10 00:55:00 +03:00
|
|
|
kauth_cred_clone(cred, ncred);
|
|
|
|
kauth_cred_setegid(ncred, egid);
|
2006-07-20 01:11:37 +04:00
|
|
|
|
|
|
|
/* Broadcast our credentials to the process and other LWPs. */
|
2007-02-22 09:34:42 +03:00
|
|
|
proc_crmod_leave(cred, ncred, true);
|
2002-10-12 01:54:55 +04:00
|
|
|
|
|
|
|
return (oegid);
|
|
|
|
}
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
/* Called with fst locked */
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_answer(struct str_process *strp, struct systrace_answer *ans)
|
|
|
|
{
|
|
|
|
int error = 0;
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
DPRINTF(("%s: %u: policy %d\n", __func__,
|
|
|
|
ans->stra_pid, ans->stra_policy));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
if (!POLICY_VALID(ans->stra_policy)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
/* Check if answer is in sync with us */
|
|
|
|
if (ans->stra_seqnr != strp->seqnr) {
|
|
|
|
error = ESRCH;
|
2002-06-17 20:22:50 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2002-07-30 20:29:28 +04:00
|
|
|
|
|
|
|
if ((error = systrace_processready(strp)) != 0)
|
2002-06-17 20:22:50 +04:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
strp->answer = ans->stra_policy;
|
|
|
|
strp->error = ans->stra_error;
|
|
|
|
if (!strp->error)
|
|
|
|
strp->error = EPERM;
|
|
|
|
if (ISSET(ans->stra_flags, SYSTR_FLAGS_RESULT))
|
|
|
|
SET(strp->flags, STR_PROC_SYSCALLRES);
|
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
/* See if we should elevate privileges for this system call */
|
|
|
|
if (ISSET(ans->stra_flags, SYSTR_FLAGS_SETEUID)) {
|
|
|
|
SET(strp->flags, STR_PROC_SETEUID);
|
|
|
|
strp->seteuid = ans->stra_seteuid;
|
|
|
|
}
|
|
|
|
if (ISSET(ans->stra_flags, SYSTR_FLAGS_SETEGID)) {
|
|
|
|
SET(strp->flags, STR_PROC_SETEGID);
|
|
|
|
strp->setegid = ans->stra_setegid;
|
|
|
|
}
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2002-10-12 01:54:55 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
/* Clearing the flag indicates to the process that it woke up */
|
|
|
|
CLR(strp->flags, STR_PROC_WAITANSWER);
|
|
|
|
wakeup(strp);
|
|
|
|
out:
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2005-06-27 21:11:20 +04:00
|
|
|
int
|
|
|
|
systrace_setscriptname(struct str_process *strp,
|
2006-10-12 05:30:41 +04:00
|
|
|
struct systrace_scriptname *ans)
|
2005-06-27 21:11:20 +04:00
|
|
|
{
|
|
|
|
strlcpy(strp->scriptname, ans->sn_scriptname,
|
|
|
|
sizeof(strp->scriptname));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
int
|
|
|
|
systrace_policy(struct fsystrace *fst, struct systrace_policy *pol)
|
|
|
|
{
|
|
|
|
struct str_policy *strpol;
|
|
|
|
struct str_process *strp;
|
|
|
|
|
|
|
|
switch(pol->strp_op) {
|
|
|
|
case SYSTR_POLICY_NEW:
|
|
|
|
DPRINTF(("%s: new, ents %d\n", __func__,
|
|
|
|
pol->strp_maxents));
|
|
|
|
if (pol->strp_maxents <= 0 || pol->strp_maxents > 1024)
|
|
|
|
return (EINVAL);
|
|
|
|
strpol = systrace_newpolicy(fst, pol->strp_maxents);
|
|
|
|
if (strpol == NULL)
|
|
|
|
return (ENOBUFS);
|
|
|
|
pol->strp_num = strpol->nr;
|
|
|
|
break;
|
|
|
|
case SYSTR_POLICY_ASSIGN:
|
|
|
|
DPRINTF(("%s: %d -> pid %d\n", __func__,
|
|
|
|
pol->strp_num, pol->strp_pid));
|
|
|
|
|
|
|
|
/* Find right policy by number */
|
|
|
|
TAILQ_FOREACH(strpol, &fst->policies, next)
|
|
|
|
if (strpol->nr == pol->strp_num)
|
|
|
|
break;
|
|
|
|
if (strpol == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
strp = systrace_findpid(fst, pol->strp_pid);
|
|
|
|
if (strp == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Check that emulation matches */
|
2007-02-10 00:55:00 +03:00
|
|
|
if (strpol->emul && strpol->emul != strp->proc->p_emul) {
|
|
|
|
struct proc *p = strp->proc;
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_exit(&p->p_reflock);
|
2002-06-17 20:22:50 +04:00
|
|
|
return (EINVAL);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
if (strp->policy)
|
|
|
|
systrace_closepolicy(fst, strp->policy);
|
|
|
|
strp->policy = strpol;
|
|
|
|
strpol->refcount++;
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
/* Record emulation for this policy */
|
|
|
|
if (strpol->emul == NULL)
|
|
|
|
strpol->emul = strp->proc->p_emul;
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_exit(&strp->proc->p_reflock);
|
2002-06-17 20:22:50 +04:00
|
|
|
break;
|
|
|
|
case SYSTR_POLICY_MODIFY:
|
|
|
|
DPRINTF(("%s: %d: code %d -> policy %d\n", __func__,
|
2002-07-30 20:29:28 +04:00
|
|
|
pol->strp_num, pol->strp_code, pol->strp_policy));
|
2002-06-17 20:22:50 +04:00
|
|
|
if (!POLICY_VALID(pol->strp_policy))
|
|
|
|
return (EINVAL);
|
|
|
|
TAILQ_FOREACH(strpol, &fst->policies, next)
|
|
|
|
if (strpol->nr == pol->strp_num)
|
|
|
|
break;
|
|
|
|
if (strpol == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
if (pol->strp_code < 0 || pol->strp_code >= strpol->nsysent)
|
|
|
|
return (EINVAL);
|
|
|
|
strpol->sysent[pol->strp_code] = pol->strp_policy;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_processready(struct str_process *strp)
|
|
|
|
{
|
|
|
|
if (ISSET(strp->flags, STR_PROC_ONQUEUE))
|
2002-08-28 07:40:54 +04:00
|
|
|
return (EBUSY);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
if (!ISSET(strp->flags, STR_PROC_WAITANSWER))
|
2002-08-28 07:40:54 +04:00
|
|
|
return (EBUSY);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2003-02-16 23:24:47 +03:00
|
|
|
/* XXX - ignore until systrace knows about lwps. :-(
|
2003-01-18 13:06:22 +03:00
|
|
|
if (strp->proc->p_stat != LSSLEEP)
|
2002-06-17 20:22:50 +04:00
|
|
|
return (EBUSY);
|
2003-02-16 23:24:47 +03:00
|
|
|
*/
|
2002-06-17 20:22:50 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_getcwd(struct fsystrace *fst, struct str_process *strp)
|
|
|
|
{
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
struct cwdinfo *mycwdp, *cwdp;
|
|
|
|
#else
|
|
|
|
struct filedesc *myfdp, *fdp;
|
|
|
|
#endif
|
|
|
|
int error;
|
|
|
|
|
|
|
|
DPRINTF(("%s: %d\n", __func__, strp->pid));
|
|
|
|
|
|
|
|
error = systrace_processready(strp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
mycwdp = curproc->p_cwdi;
|
|
|
|
cwdp = strp->proc->p_cwdi;
|
|
|
|
if (mycwdp == NULL || cwdp == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Store our current values */
|
|
|
|
fst->fd_pid = strp->pid;
|
2007-07-10 00:51:58 +04:00
|
|
|
rw_enter(&mycwdp->cwdi_lock, RW_READER);
|
2002-06-17 20:22:50 +04:00
|
|
|
fst->fd_cdir = mycwdp->cwdi_cdir;
|
|
|
|
fst->fd_rdir = mycwdp->cwdi_rdir;
|
|
|
|
if ((mycwdp->cwdi_cdir = cwdp->cwdi_cdir) != NULL)
|
|
|
|
VREF(mycwdp->cwdi_cdir);
|
|
|
|
if ((mycwdp->cwdi_rdir = cwdp->cwdi_rdir) != NULL)
|
|
|
|
VREF(mycwdp->cwdi_rdir);
|
2007-07-10 00:51:58 +04:00
|
|
|
rw_exit(&mycwdp->cwdi_lock);
|
2002-06-17 20:22:50 +04:00
|
|
|
#else
|
2003-01-18 13:06:22 +03:00
|
|
|
myfdp = curlwp->p_fd;
|
2002-06-17 20:22:50 +04:00
|
|
|
fdp = strp->proc->p_fd;
|
|
|
|
if (myfdp == NULL || fdp == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Store our current values */
|
|
|
|
fst->fd_pid = strp->pid;
|
|
|
|
fst->fd_cdir = myfdp->fd_cdir;
|
|
|
|
fst->fd_rdir = myfdp->fd_rdir;
|
|
|
|
|
|
|
|
if ((myfdp->fd_cdir = fdp->fd_cdir) != NULL)
|
|
|
|
VREF(myfdp->fd_cdir);
|
|
|
|
if ((myfdp->fd_rdir = fdp->fd_rdir) != NULL)
|
|
|
|
VREF(myfdp->fd_rdir);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_io(struct str_process *strp, struct systrace_io *io)
|
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct proc *t = strp->proc;
|
|
|
|
struct lwp *l = curlwp;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct uio uio;
|
|
|
|
struct iovec iov;
|
|
|
|
int error = 0;
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-08-08 18:43:40 +04:00
|
|
|
DPRINTF(("%s: %u: %p(%lu)\n", __func__,
|
|
|
|
io->strio_pid, io->strio_offs, (u_long)io->strio_len));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
switch (io->strio_op) {
|
|
|
|
case SYSTR_READ:
|
|
|
|
uio.uio_rw = UIO_READ;
|
|
|
|
break;
|
|
|
|
case SYSTR_WRITE:
|
|
|
|
uio.uio_rw = UIO_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = systrace_processready(strp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
iov.iov_base = io->strio_addr;
|
|
|
|
iov.iov_len = io->strio_len;
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
2004-05-26 20:28:05 +04:00
|
|
|
uio.uio_offset = (off_t)(unsigned long)io->strio_offs;
|
2002-06-17 20:22:50 +04:00
|
|
|
uio.uio_resid = io->strio_len;
|
2006-03-01 15:38:10 +03:00
|
|
|
uio.uio_vmspace = l->l_proc->p_vmspace;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2006-11-28 20:27:09 +03:00
|
|
|
error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSYSTRACE,
|
|
|
|
t, NULL, NULL, NULL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
#ifdef __NetBSD__
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct lwp *tl;
|
|
|
|
mutex_enter(&t->p_smutex);
|
|
|
|
tl = proc_representative_lwp(t, NULL, 1);
|
|
|
|
lwp_addref(tl);
|
|
|
|
mutex_exit(&t->p_smutex);
|
|
|
|
error = process_domem(l, tl, &uio);
|
|
|
|
lwp_delref(tl);
|
|
|
|
}
|
2002-07-30 20:29:28 +04:00
|
|
|
#else
|
|
|
|
error = procfs_domem(p, t, NULL, &uio);
|
|
|
|
#endif
|
2002-06-17 20:22:50 +04:00
|
|
|
io->strio_len -= uio.uio_resid;
|
|
|
|
out:
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_attach(struct fsystrace *fst, pid_t pid)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
struct proc *proc, *p = curproc;
|
|
|
|
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_enter(&proclist_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if ((proc = p_find(pid, PFIND_LOCKED)) == NULL) {
|
2007-03-09 17:11:22 +03:00
|
|
|
mutex_exit(&proclist_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
return (ESRCH);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
2007-11-07 03:23:13 +03:00
|
|
|
if (!rw_tryenter(&proc->p_reflock, RW_READER)) {
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proc->p_mutex);
|
2007-11-07 03:23:13 +03:00
|
|
|
return (EBUSY);
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You can't attach to a process if:
|
|
|
|
* (1) it's the process that's doing the attaching,
|
|
|
|
*/
|
|
|
|
if (proc->p_pid == p->p_pid) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (2) it's a system process
|
|
|
|
*/
|
2007-02-18 01:31:36 +03:00
|
|
|
if (ISSET(proc->p_flag, PK_SYSTEM)) {
|
2002-06-17 20:22:50 +04:00
|
|
|
error = EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (3) it's being traced already
|
|
|
|
*/
|
2007-02-18 01:31:36 +03:00
|
|
|
if (ISSET(proc->p_flag, PK_SYSTRACE)) {
|
2002-06-17 20:22:50 +04:00
|
|
|
error = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-11-28 20:58:10 +03:00
|
|
|
* (4) the security model prevents it it.
|
2002-06-17 20:22:50 +04:00
|
|
|
*/
|
2006-11-28 20:27:09 +03:00
|
|
|
error = kauth_authorize_process(kauth_cred_get(),
|
|
|
|
KAUTH_PROCESS_CANSYSTRACE, proc, NULL, NULL, NULL);
|
|
|
|
if (error)
|
2002-06-17 20:22:50 +04:00
|
|
|
goto out;
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
error = systrace_insert_process(fst, proc, NULL);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
#if defined(__NetBSD__) && defined(__HAVE_SYSCALL_INTERN)
|
|
|
|
/*
|
|
|
|
* Make sure we're using the version of the syscall handler that
|
|
|
|
* has systrace hooks.
|
|
|
|
*/
|
|
|
|
if (!error)
|
|
|
|
(*proc->p_emul->e_syscall_intern)(proc);
|
|
|
|
#endif
|
|
|
|
out:
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_exit(&proc->p_reflock);
|
2007-02-10 00:55:00 +03:00
|
|
|
mutex_exit(&proc->p_mutex);
|
2002-06-17 20:22:50 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2005-06-26 23:58:29 +04:00
|
|
|
void
|
2005-06-27 21:11:20 +04:00
|
|
|
systrace_execve0(struct proc *p)
|
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
|
|
|
strp->isscript = 0;
|
|
|
|
systrace_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_execve1(char *path, struct proc *p)
|
2005-06-26 23:58:29 +04:00
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
struct fsystrace *fst;
|
|
|
|
struct str_message msg;
|
|
|
|
struct str_msg_execve *msg_execve;
|
|
|
|
|
|
|
|
do {
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
|
|
|
if (strp == NULL) {
|
|
|
|
systrace_unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_execve = &msg.msg_data.msg_execve;
|
|
|
|
fst = strp->parent;
|
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
|
|
|
systrace_unlock();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* susers will get the execve call anyway. Also, if
|
|
|
|
* we're not allowed to control the process, escape.
|
|
|
|
*/
|
|
|
|
if (fst->issuser ||
|
2006-05-15 01:15:11 +04:00
|
|
|
fst->p_ruid != kauth_cred_getuid(p->p_cred) ||
|
|
|
|
fst->p_rgid != kauth_cred_getgid(p->p_cred)) {
|
2005-06-26 23:58:29 +04:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(msg_execve->path, path, sizeof(msg_execve->path));
|
|
|
|
} while (systrace_make_msg(strp, SYSTR_MSG_EXECVE, &msg) != 0);
|
|
|
|
}
|
|
|
|
|
2002-07-30 20:29:28 +04:00
|
|
|
/* Prepare to replace arguments */
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_preprepl(struct str_process *strp, struct systrace_replace *repl)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
ret = systrace_processready(strp);
|
|
|
|
if (ret)
|
|
|
|
return (ret);
|
|
|
|
|
2003-08-25 13:12:42 +04:00
|
|
|
systrace_replacefree(strp);
|
2002-07-30 20:29:28 +04:00
|
|
|
|
|
|
|
if (repl->strr_nrepl < 0 || repl->strr_nrepl > SYSTR_MAXARGS)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
for (i = 0, len = 0; i < repl->strr_nrepl; i++) {
|
2006-10-06 20:17:11 +04:00
|
|
|
if (repl->strr_argind[i] < 0 ||
|
|
|
|
repl->strr_argind[i] >= SYSTR_MAXARGS)
|
|
|
|
return (EINVAL);
|
2002-07-30 20:29:28 +04:00
|
|
|
if (repl->strr_offlen[i] == 0)
|
|
|
|
continue;
|
2006-10-06 20:17:11 +04:00
|
|
|
len += repl->strr_offlen[i];
|
|
|
|
if (repl->strr_offlen[i] > SYSTR_MAXREPLEN ||
|
|
|
|
repl->strr_off[i] > SYSTR_MAXREPLEN ||
|
|
|
|
len > SYSTR_MAXREPLEN)
|
|
|
|
return (EINVAL);
|
2002-07-30 20:29:28 +04:00
|
|
|
if (repl->strr_offlen[i] + repl->strr_off[i] > len)
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the length adds up */
|
|
|
|
if (repl->strr_len != len)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Check against a maximum length */
|
2006-10-06 20:17:11 +04:00
|
|
|
if (repl->strr_len > SYSTR_MAXREPLEN)
|
2002-07-30 20:29:28 +04:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
strp->replace = (struct systrace_replace *)
|
|
|
|
malloc(sizeof(struct systrace_replace) + len, M_XDATA, M_WAITOK);
|
|
|
|
|
|
|
|
memcpy(strp->replace, repl, sizeof(struct systrace_replace));
|
|
|
|
ret = copyin(repl->strr_base, strp->replace + 1, len);
|
|
|
|
if (ret) {
|
|
|
|
free(strp->replace, M_XDATA);
|
|
|
|
strp->replace = NULL;
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the offset */
|
|
|
|
repl = strp->replace;
|
2007-03-04 08:59:00 +03:00
|
|
|
repl->strr_base = (void *)(repl + 1);
|
2002-07-30 20:29:28 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace the arguments with arguments from the monitoring process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_replace(struct str_process *strp, size_t argsize, register_t args[])
|
|
|
|
{
|
|
|
|
struct proc *p = strp->proc;
|
|
|
|
struct systrace_replace *repl = strp->replace;
|
2007-03-04 08:59:00 +03:00
|
|
|
void *sg, *kdata, *udata, *kbase, *ubase;
|
2002-07-30 20:29:28 +04:00
|
|
|
int i, maxarg, ind, ret = 0;
|
|
|
|
|
|
|
|
maxarg = argsize/sizeof(register_t);
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
sg = stackgap_init(p, 0);
|
|
|
|
ubase = stackgap_alloc(p, &sg, repl->strr_len);
|
|
|
|
#else
|
|
|
|
sg = stackgap_init(p->p_emul);
|
|
|
|
ubase = stackgap_alloc(&sg, repl->strr_len);
|
|
|
|
#endif
|
2006-10-06 20:17:11 +04:00
|
|
|
if (ubase == NULL) {
|
|
|
|
ret = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-07-30 20:29:28 +04:00
|
|
|
|
|
|
|
kbase = repl->strr_base;
|
|
|
|
for (i = 0; i < maxarg && i < repl->strr_nrepl; i++) {
|
|
|
|
ind = repl->strr_argind[i];
|
|
|
|
if (ind < 0 || ind >= maxarg) {
|
|
|
|
ret = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (repl->strr_offlen[i] == 0) {
|
|
|
|
args[ind] = repl->strr_off[i];
|
|
|
|
continue;
|
|
|
|
}
|
2007-03-04 08:59:00 +03:00
|
|
|
kdata = (char *)kbase + repl->strr_off[i];
|
|
|
|
udata = (char *)ubase + repl->strr_off[i];
|
2003-08-25 13:12:42 +04:00
|
|
|
if (repl->strr_flags[i] & SYSTR_NOLINKS) {
|
|
|
|
ret = systrace_fname(strp, kdata, repl->strr_offlen[i]);
|
|
|
|
if (ret != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2002-07-30 20:29:28 +04:00
|
|
|
if (copyout(kdata, udata, repl->strr_offlen[i])) {
|
|
|
|
ret = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the argument with the new address */
|
2002-08-26 15:26:09 +04:00
|
|
|
args[ind] = (register_t)(intptr_t)udata;
|
2002-07-30 20:29:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2003-08-25 13:12:42 +04:00
|
|
|
int
|
2007-03-04 08:59:00 +03:00
|
|
|
systrace_fname(struct str_process *strp, void *kdata, size_t len)
|
2003-08-25 13:12:42 +04:00
|
|
|
{
|
|
|
|
if (strp->nfname >= SYSTR_MAXFNAME || len < 2)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
strp->fname[strp->nfname] = kdata;
|
|
|
|
strp->fname[strp->nfname][len - 1] = '\0';
|
|
|
|
strp->nfname++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_replacefree(struct str_process *strp)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (strp->replace != NULL) {
|
|
|
|
free(strp->replace, M_XDATA);
|
|
|
|
strp->replace = NULL;
|
|
|
|
}
|
|
|
|
while (strp->nfname > 0) {
|
|
|
|
strp->nfname--;
|
|
|
|
strp->fname[strp->nfname] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-27 21:11:20 +04:00
|
|
|
int
|
|
|
|
systrace_scriptname(struct proc *p, char *dst)
|
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
struct fsystrace *fst;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
systrace_lock();
|
|
|
|
strp = p->p_systrace;
|
2006-03-18 20:44:13 +03:00
|
|
|
if (strp == NULL) {
|
|
|
|
systrace_unlock();
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2005-06-27 21:11:20 +04:00
|
|
|
fst = strp->parent;
|
|
|
|
|
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
|
|
|
systrace_unlock();
|
|
|
|
|
2007-02-18 01:31:36 +03:00
|
|
|
if (!fst->issuser && (ISSET(p->p_flag, PK_SUGID) ||
|
2006-05-15 01:15:11 +04:00
|
|
|
fst->p_ruid != kauth_cred_getuid(p->p_cred) ||
|
|
|
|
fst->p_rgid != kauth_cred_getgid(p->p_cred))) {
|
2005-06-27 21:11:20 +04:00
|
|
|
error = EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2006-03-18 20:44:13 +03:00
|
|
|
if (strp->scriptname[0] == '\0') {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
2005-06-27 21:11:20 +04:00
|
|
|
}
|
|
|
|
|
2006-03-18 20:44:13 +03:00
|
|
|
strlcpy(dst, strp->scriptname, MAXPATHLEN);
|
|
|
|
strp->isscript = 1;
|
|
|
|
|
2005-06-27 21:11:20 +04:00
|
|
|
out:
|
|
|
|
strp->scriptname[0] = '\0';
|
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2003-08-25 13:12:42 +04:00
|
|
|
void
|
|
|
|
systrace_namei(struct nameidata *ndp)
|
|
|
|
{
|
|
|
|
struct str_process *strp;
|
2005-06-26 23:58:29 +04:00
|
|
|
struct fsystrace *fst = NULL; /* XXXGCC */
|
2003-08-25 13:12:42 +04:00
|
|
|
struct componentname *cnp = &ndp->ni_cnd;
|
|
|
|
size_t i;
|
2005-06-26 23:58:29 +04:00
|
|
|
int hamper = 0;
|
2003-08-25 13:12:42 +04:00
|
|
|
|
|
|
|
systrace_lock();
|
2005-12-11 15:16:03 +03:00
|
|
|
strp = cnp->cn_lwp->l_proc->p_systrace;
|
2003-08-25 13:12:42 +04:00
|
|
|
if (strp != NULL) {
|
|
|
|
fst = strp->parent;
|
|
|
|
SYSTRACE_LOCK(fst, curlwp);
|
|
|
|
systrace_unlock();
|
|
|
|
|
2005-06-27 21:11:20 +04:00
|
|
|
for (i = 0; i < strp->nfname; i++) {
|
2003-08-25 13:12:42 +04:00
|
|
|
if (strcmp(cnp->cn_pnbuf, strp->fname[i]) == 0) {
|
2005-06-26 23:58:29 +04:00
|
|
|
hamper = 1;
|
2003-08-25 13:12:42 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-06-27 21:11:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hamper && strp->isscript &&
|
|
|
|
strcmp(cnp->cn_pnbuf, strp->scriptname) == 0)
|
|
|
|
hamper = 1;
|
|
|
|
|
2003-08-25 13:12:42 +04:00
|
|
|
SYSTRACE_UNLOCK(fst, curlwp);
|
|
|
|
} else
|
|
|
|
systrace_unlock();
|
2005-06-26 23:58:29 +04:00
|
|
|
|
|
|
|
if (hamper) {
|
|
|
|
/* ELOOP if namei() tries to readlink */
|
|
|
|
ndp->ni_loopcnt = MAXSYMLINKS;
|
|
|
|
cnp->cn_flags &= ~FOLLOW;
|
|
|
|
cnp->cn_flags |= NOFOLLOW;
|
|
|
|
}
|
2003-08-25 13:12:42 +04:00
|
|
|
}
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
struct str_process *
|
|
|
|
systrace_findpid(struct fsystrace *fst, pid_t pid)
|
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
struct proc *proc = NULL;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(strp, &fst->processes, next)
|
|
|
|
if (strp->pid == pid)
|
|
|
|
break;
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
if (strp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
proc = systrace_find(strp);
|
|
|
|
|
|
|
|
return (proc ? strp : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_detach(struct str_process *strp)
|
|
|
|
{
|
|
|
|
struct proc *proc;
|
|
|
|
struct fsystrace *fst = NULL;
|
|
|
|
int error = 0;
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
DPRINTF(("%s: Trying to detach from %d\n", __func__, strp->pid));
|
|
|
|
|
|
|
|
if ((proc = systrace_find(strp)) != NULL) {
|
2007-02-18 01:31:36 +03:00
|
|
|
CLR(proc->p_flag, PK_SYSTRACE);
|
2002-06-17 20:22:50 +04:00
|
|
|
proc->p_systrace = NULL;
|
|
|
|
} else
|
|
|
|
error = ESRCH;
|
|
|
|
|
|
|
|
if (ISSET(strp->flags, STR_PROC_WAITANSWER)) {
|
|
|
|
CLR(strp->flags, STR_PROC_WAITANSWER);
|
|
|
|
wakeup(strp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fst = strp->parent;
|
|
|
|
systrace_wakeup(fst);
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&fst->processes, strp, next);
|
|
|
|
fst->nprocesses--;
|
|
|
|
|
|
|
|
if (strp->policy)
|
|
|
|
systrace_closepolicy(fst, strp->policy);
|
2003-08-25 13:12:42 +04:00
|
|
|
systrace_replacefree(strp);
|
2002-06-17 20:22:50 +04:00
|
|
|
pool_put(&systr_proc_pl, strp);
|
|
|
|
|
2007-11-07 03:23:13 +03:00
|
|
|
rw_exit(&strp->proc->p_reflock);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
systrace_closepolicy(struct fsystrace *fst, struct str_policy *policy)
|
|
|
|
{
|
|
|
|
if (--policy->refcount)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fst->npolicies--;
|
|
|
|
|
|
|
|
if (policy->nsysent)
|
2002-07-20 03:55:01 +04:00
|
|
|
free(policy->sysent, M_XDATA);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&fst->policies, policy, next);
|
|
|
|
|
|
|
|
pool_put(&systr_policy_pl, policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2003-03-30 04:40:05 +04:00
|
|
|
systrace_insert_process(struct fsystrace *fst, struct proc *proc,
|
|
|
|
struct str_process **pstrp)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
|
|
|
struct str_process *strp;
|
|
|
|
|
|
|
|
strp = pool_get(&systr_proc_pl, PR_NOWAIT);
|
|
|
|
if (strp == NULL)
|
|
|
|
return (ENOBUFS);
|
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
memset((void *)strp, 0, sizeof(struct str_process));
|
2002-06-17 20:22:50 +04:00
|
|
|
strp->pid = proc->p_pid;
|
|
|
|
strp->proc = proc;
|
|
|
|
strp->parent = fst;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&fst->processes, strp, next);
|
|
|
|
fst->nprocesses++;
|
|
|
|
|
|
|
|
proc->p_systrace = strp;
|
2007-02-18 01:31:36 +03:00
|
|
|
SET(proc->p_flag, PK_SYSTRACE);
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
/* Pass the new pointer back to the caller */
|
|
|
|
if (pstrp != NULL)
|
|
|
|
*pstrp = strp;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct str_policy *
|
|
|
|
systrace_newpolicy(struct fsystrace *fst, int maxents)
|
|
|
|
{
|
|
|
|
struct str_policy *pol;
|
|
|
|
int i;
|
|
|
|
|
2003-06-03 09:24:00 +04:00
|
|
|
if (fst->npolicies > SYSTR_MAX_POLICIES && !fst->issuser) {
|
|
|
|
struct str_policy *tmp;
|
|
|
|
|
|
|
|
/* Try to find a policy for freeing */
|
|
|
|
TAILQ_FOREACH(tmp, &fst->policies, next) {
|
|
|
|
if (tmp->refcount == 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* Notify userland about freed policy */
|
|
|
|
systrace_msg_policyfree(fst, tmp);
|
|
|
|
/* Free this policy */
|
|
|
|
systrace_closepolicy(fst, tmp);
|
|
|
|
}
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
pol = pool_get(&systr_policy_pl, PR_NOWAIT);
|
|
|
|
if (pol == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2002-08-08 18:43:40 +04:00
|
|
|
DPRINTF(("%s: allocating %d -> %lu\n", __func__,
|
|
|
|
maxents, (u_long)maxents * sizeof(int)));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
memset((void *)pol, 0, sizeof(struct str_policy));
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2002-07-20 03:55:01 +04:00
|
|
|
pol->sysent = (u_char *)malloc(maxents * sizeof(u_char),
|
2002-06-17 20:22:50 +04:00
|
|
|
M_XDATA, M_WAITOK);
|
|
|
|
pol->nsysent = maxents;
|
|
|
|
for (i = 0; i < maxents; i++)
|
|
|
|
pol->sysent[i] = SYSTR_POLICY_ASK;
|
|
|
|
|
|
|
|
fst->npolicies++;
|
|
|
|
pol->nr = fst->npolicynr++;
|
|
|
|
pol->refcount = 1;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&fst->policies, pol, next);
|
|
|
|
|
|
|
|
return (pol);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systrace_msg_ask(struct fsystrace *fst, struct str_process *strp, int code,
|
|
|
|
size_t argsize, register_t args[])
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_message msg;
|
|
|
|
struct str_msg_ask *msg_ask = &msg.msg_data.msg_ask;
|
2002-06-17 20:22:50 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
msg_ask->code = code;
|
|
|
|
msg_ask->argsize = argsize;
|
|
|
|
for (i = 0; i < (argsize/sizeof(register_t)) && i < SYSTR_MAXARGS; i++)
|
|
|
|
msg_ask->args[i] = args[i];
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
return (systrace_make_msg(strp, SYSTR_MSG_ASK, &msg));
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systrace_msg_result(struct fsystrace *fst, struct str_process *strp, int error,
|
|
|
|
int code, size_t argsize, register_t args[], register_t rval[])
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_message msg;
|
|
|
|
struct str_msg_ask *msg_ask = &msg.msg_data.msg_ask;
|
2002-06-17 20:22:50 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
msg_ask->code = code;
|
|
|
|
msg_ask->argsize = argsize;
|
|
|
|
msg_ask->result = error;
|
|
|
|
for (i = 0; i < (argsize/sizeof(register_t)) && i < SYSTR_MAXARGS; i++)
|
|
|
|
msg_ask->args[i] = args[i];
|
|
|
|
|
|
|
|
msg_ask->rval[0] = rval[0];
|
|
|
|
msg_ask->rval[1] = rval[1];
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
return (systrace_make_msg(strp, SYSTR_MSG_RES, &msg));
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systrace_msg_emul(struct fsystrace *fst, struct str_process *strp)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_message msg;
|
|
|
|
struct str_msg_emul *msg_emul = &msg.msg_data.msg_emul;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct proc *p = strp->proc;
|
|
|
|
|
|
|
|
memcpy(msg_emul->emul, p->p_emul->e_name, SYSTR_EMULEN);
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
return (systrace_make_msg(strp, SYSTR_MSG_EMUL, &msg));
|
2002-06-17 20:22:50 +04:00
|
|
|
}
|
|
|
|
|
2002-10-08 18:46:24 +04:00
|
|
|
int
|
2006-11-01 13:17:58 +03:00
|
|
|
systrace_msg_ugid(struct fsystrace *fst, struct str_process *strp)
|
2002-10-08 18:46:24 +04:00
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_message msg;
|
|
|
|
struct str_msg_ugid *msg_ugid = &msg.msg_data.msg_ugid;
|
2002-10-08 18:46:24 +04:00
|
|
|
struct proc *p = strp->proc;
|
|
|
|
|
2006-05-15 01:15:11 +04:00
|
|
|
msg_ugid->uid = kauth_cred_getuid(p->p_cred);
|
|
|
|
msg_ugid->gid = kauth_cred_getgid(p->p_cred);
|
2002-10-08 18:46:24 +04:00
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
return (systrace_make_msg(strp, SYSTR_MSG_UGID, &msg));
|
2002-10-08 18:46:24 +04:00
|
|
|
}
|
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
int
|
2003-03-30 04:40:05 +04:00
|
|
|
systrace_make_msg(struct str_process *strp, int type, struct str_message *tmsg)
|
2002-06-17 20:22:50 +04:00
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_msgcontainer *cont;
|
|
|
|
struct str_message *msg;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct fsystrace *fst = strp->parent;
|
2005-12-27 03:26:58 +03:00
|
|
|
int st;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
cont = pool_get(&systr_msgcontainer_pl, PR_WAITOK);
|
|
|
|
memset(cont, 0, sizeof(struct str_msgcontainer));
|
|
|
|
cont->strp = strp;
|
|
|
|
|
|
|
|
msg = &cont->msg;
|
|
|
|
|
|
|
|
/* Copy the already filled in fields */
|
|
|
|
memcpy(&msg->msg_data, &tmsg->msg_data, sizeof(msg->msg_data));
|
|
|
|
|
|
|
|
/* Add the extra fields to the message */
|
2002-07-30 20:29:28 +04:00
|
|
|
msg->msg_seqnr = ++strp->seqnr;
|
2002-06-17 20:22:50 +04:00
|
|
|
msg->msg_type = type;
|
|
|
|
msg->msg_pid = strp->pid;
|
|
|
|
if (strp->policy)
|
|
|
|
msg->msg_policy = strp->policy->nr;
|
|
|
|
else
|
|
|
|
msg->msg_policy = -1;
|
|
|
|
|
|
|
|
SET(strp->flags, STR_PROC_WAITANSWER);
|
|
|
|
if (ISSET(strp->flags, STR_PROC_ONQUEUE))
|
|
|
|
goto out;
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
TAILQ_INSERT_TAIL(&fst->messages, cont, next);
|
2002-06-17 20:22:50 +04:00
|
|
|
SET(strp->flags, STR_PROC_ONQUEUE);
|
|
|
|
|
|
|
|
out:
|
|
|
|
systrace_wakeup(fst);
|
|
|
|
|
|
|
|
/* Release the lock - XXX */
|
2002-07-16 23:53:37 +04:00
|
|
|
SYSTRACE_UNLOCK(fst, strp->proc);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
while (1) {
|
2005-12-27 03:26:58 +03:00
|
|
|
st = tsleep(strp, PWAIT, "systrmsg", 0);
|
2002-06-17 20:22:50 +04:00
|
|
|
if (st != 0)
|
2003-06-03 09:24:00 +04:00
|
|
|
return (ERESTART);
|
2002-06-17 20:22:50 +04:00
|
|
|
/* If we detach, then everything is permitted */
|
|
|
|
if ((strp = curproc->p_systrace) == NULL)
|
|
|
|
return (0);
|
|
|
|
if (!ISSET(strp->flags, STR_PROC_WAITANSWER))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
systrace_msg_child(struct fsystrace *fst, struct str_process *strp, pid_t npid)
|
|
|
|
{
|
2003-03-30 04:40:05 +04:00
|
|
|
struct str_msgcontainer *cont;
|
2002-06-17 20:22:50 +04:00
|
|
|
struct str_message *msg;
|
|
|
|
struct str_msg_child *msg_child;
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
cont = pool_get(&systr_msgcontainer_pl, PR_WAITOK);
|
|
|
|
memset(cont, 0, sizeof(struct str_msgcontainer));
|
|
|
|
cont->strp = strp;
|
2002-06-17 20:22:50 +04:00
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
msg = &cont->msg;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
DPRINTF(("%s: %p: pid %d -> pid %d\n", __func__,
|
2003-03-30 04:40:05 +04:00
|
|
|
msg, strp->pid, npid));
|
2002-08-28 07:40:54 +04:00
|
|
|
|
2002-06-17 20:22:50 +04:00
|
|
|
msg_child = &msg->msg_data.msg_child;
|
|
|
|
|
|
|
|
msg->msg_type = SYSTR_MSG_CHILD;
|
|
|
|
msg->msg_pid = strp->pid;
|
|
|
|
if (strp->policy)
|
|
|
|
msg->msg_policy = strp->policy->nr;
|
|
|
|
else
|
|
|
|
msg->msg_policy = -1;
|
|
|
|
msg_child->new_pid = npid;
|
|
|
|
|
2003-03-30 04:40:05 +04:00
|
|
|
TAILQ_INSERT_TAIL(&fst->messages, cont, next);
|
2002-06-17 20:22:50 +04:00
|
|
|
|
|
|
|
systrace_wakeup(fst);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2003-06-03 09:24:00 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
systrace_msg_policyfree(struct fsystrace *fst, struct str_policy *strpol)
|
|
|
|
{
|
|
|
|
struct str_msgcontainer *cont;
|
|
|
|
struct str_message *msg;
|
|
|
|
|
|
|
|
cont = pool_get(&systr_msgcontainer_pl, PR_WAITOK);
|
|
|
|
memset(cont, 0, sizeof(struct str_msgcontainer));
|
|
|
|
|
|
|
|
msg = &cont->msg;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2003-06-03 09:24:00 +04:00
|
|
|
DPRINTF(("%s: free %d\n", __func__, strpol->nr));
|
|
|
|
|
|
|
|
msg->msg_type = SYSTR_MSG_POLICYFREE;
|
|
|
|
msg->msg_policy = strpol->nr;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&fst->messages, cont, next);
|
|
|
|
|
|
|
|
systrace_wakeup(fst);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|