diff -rub orig/sys/conf/files sys/conf/files --- orig/sys/conf/files Sun May 11 01:37:52 2003 +++ sys/conf/files Tue Jul 15 16:58:09 2003 @@ -751,6 +751,7 @@ dev/syscons/rain/rain_saver.c optional rain_saver dev/syscons/star/star_saver.c optional star_saver dev/syscons/warp/warp_saver.c optional warp_saver +dev/systrace/systrace.c optional systrace dev/tdfx/tdfx_pci.c optional tdfx pci dev/trm/trm.c optional trm dev/twe/twe.c optional twe diff -rub orig/sys/conf/majors sys/conf/majors --- orig/sys/conf/majors Sun May 11 01:18:33 2003 +++ sys/conf/majors Tue Jul 15 18:06:32 2003 @@ -178,6 +178,7 @@ 183 *smapi SMAPI BIOS interface 200 ?? entries from 200-252 are reserved for local use 248 *isp dev/isp/isp_freebsd.c +251 systrace system call filtering 252 ?? entries from 200-252 are reserved for local use 254 internal Used internally by the kernel 255 bad_choice -1 is 255 which has magic meanings internally diff -rub orig/sys/conf/options sys/conf/options --- orig/sys/conf/options Sat May 17 22:46:30 2003 +++ sys/conf/options Tue Jul 15 17:26:45 2003 @@ -603,3 +603,5 @@ # options for hifn driver HIFN_DEBUG opt_hifn.h HIFN_RNDTEST opt_hifn.h + +SYSTRACE opt_systrace.h diff -rub orig/sys/dev/systrace/systrace.c sys/dev/systrace/systrace.c --- orig/sys/dev/systrace/systrace.c Tue Jul 15 19:05:06 2003 +++ sys/dev/systrace/systrace.c Tue Jul 15 20:53:34 2003 @@ -0,0 +1,1432 @@ +/* + * Copyright 2002 Carey Richard Murphey + * Copyright 2002 Niels Provos + * 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. + */ + +#include "opt_systrace.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* for DELAY */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +#include +#include +#include +#include +/* #include */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static MALLOC_DEFINE(M_SYSTRACE, "systrace", "Systrace processes and policies"); + +void systraceattach(dev_t dev); + +static int systrace_debug; +SYSCTL_INT(_debug, OID_AUTO, systracedebug, CTLFLAG_RW, &systrace_debug, 0, ""); +#define DPRINTF(y) if (systrace_debug) printf y; + +#define CDEV_MAJOR 151 + +static d_ioctl_t systraceioctl; +static d_open_t systraceopen; +static d_close_t systraceclose; +static d_read_t systraceread; +static d_write_t systracewrite; +static d_poll_t systraceselect; + +static struct cdevsw systrace_cdevsw = { + .d_open = systraceopen, + .d_close = systraceclose, + .d_ioctl = systraceioctl, + .d_name = "systrace", + .d_maj = CDEV_MAJOR, + .d_read = systraceread, + .d_write = systracewrite, + .d_poll = systraceselect +}; + +static dev_t sdev; + +/* + * This function acts is called by the kld[un]load(2) system calls to + * determine what actions to take when a module is loaded or unloaded. + */ + +static int +systrace_modevent(module_t mod, int type, void *data) +{ + int err = 0; + + switch (type) { + case MOD_LOAD: /* kldload */ + sdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "systrace"); + systraceattach(sdev); + break; + case MOD_UNLOAD: + destroy_dev(sdev); + break; + default: + err = EINVAL; + break; + } + return(err); +} + +DEV_MODULE(systrace,systrace_modevent,NULL); + +struct str_policy { + TAILQ_ENTRY(str_policy) next; + + int nr; + + struct sysentvec *sv; /* 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 */ + +#ifndef CLR +#define SET(t, f) ((t) |= (f)) +#define CLR(t, f) ((t) &= ~(f)) +#define ISSET(t, f) ((t) & (f)) +#endif + +struct str_process { + TAILQ_ENTRY(str_process) next; + TAILQ_ENTRY(str_process) msg_next; + + struct proc *proc; + pid_t pid; + + struct fsystrace *parent; + struct str_policy *policy; + + struct systrace_replace *replace; + + int flags; + short answer; + short error; + + struct str_message msg; +}; + +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 *); +int systrace_io(struct str_process *, struct systrace_io *); +int systrace_policy(struct fsystrace *, struct systrace_policy *); +int systrace_preprepl(struct str_process *, struct systrace_replace *); +int systrace_replace(struct str_process *, int, register_t []); +int systrace_getcwd(struct fsystrace *, struct str_process *); + +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 *); +int systrace_insert_process(struct fsystrace *, struct proc *); +struct str_policy *systrace_newpolicy(struct fsystrace *, int); +int systrace_msg_child(struct fsystrace *, struct str_process *, pid_t); +int systrace_msg_ask(struct fsystrace *, struct str_process *, + int, int, register_t []); +int systrace_msg_result(struct fsystrace *, struct str_process *, + int, int, int, register_t []); +int systrace_msg_emul(struct fsystrace *, struct str_process *); +int systrace_make_msg(struct str_process *, int); + +static fo_rdwr_t systracef_read; +static fo_rdwr_t systracef_write; +static fo_ioctl_t systracef_ioctl; +static fo_poll_t systracef_select; +static fo_kqfilter_t systracef_kqfilter; +static fo_stat_t systracef_stat; +static fo_close_t systracef_close; + +static struct fileops systracefops = { + systracef_read, + systracef_write, + systracef_ioctl, + systracef_select, + systracef_kqfilter, + systracef_stat, + systracef_close +}; + +struct lock systrace_lck; + +/* ARGSUSED */ +int +systracef_read(struct file *fp, struct uio *uio, + struct ucred *cred, int flags, struct thread *p) +{ + struct fsystrace *fst = (struct fsystrace *)fp->f_data; + struct str_process *process; + int error = 0; + + if (uio->uio_resid != sizeof(struct str_message)) + return (EINVAL); + + again: + systrace_lock(); + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, p); + systrace_unlock(); + if ((process = TAILQ_FIRST(&fst->messages)) != NULL) { + error = uiomove((caddr_t)&process->msg, + sizeof(struct str_message), uio); + if (!error) { + TAILQ_REMOVE(&fst->messages, process, msg_next); + CLR(process->flags, STR_PROC_ONQUEUE); + + if (SYSTR_MSG_NOPROCESS(process)) + free(process, M_SYSTRACE); + + } + } else if (TAILQ_FIRST(&fst->processes) == NULL) { + /* EOF situation */ + ; + } else { + if (fp->f_flag & O_NONBLOCK) + error = EAGAIN; + else { + lockmgr(&fst->lock, LK_RELEASE, NULL, p); + error = tsleep(fst, PWAIT|PCATCH, "systrrd", 0); + if (error) + goto out; + goto again; + } + + } + + lockmgr(&fst->lock, LK_RELEASE, NULL, p); + out: + return (error); +} + +/* ARGSUSED */ +int +systracef_write(struct file *fp, struct uio *uio, + struct ucred *cred, int flags, struct thread *p) +{ + return (EIO); +} + +#define POLICY_VALID(x) ((x) == SYSTR_POLICY_PERMIT || \ + (x) == SYSTR_POLICY_ASK || \ + (x) == SYSTR_POLICY_NEVER) + +/* ARGSUSED */ +int +systracef_ioctl(struct file *fp, u_long com, void *data, + struct ucred *active_cred, struct thread *p) +{ + int ret = 0; + struct fsystrace *fst = (struct fsystrace *)fp->f_data; + struct filedesc *fdp; + struct str_process *strp = NULL; + pid_t pid = 0; + + switch (com) { + case FIONBIO: + case FIOASYNC: + return (0); + + case STRIOCATTACH: + pid = *(pid_t *)data; + if (!pid) + ret = EINVAL; + else + ret = systrace_attach(fst, pid); + DPRINTF(("%s: attach to %d: %d\n", __func__, pid, ret)); + goto unlock; + case STRIOCDETACH: + case STRIOCREPORT: + pid = *(pid_t *)data; + if (!pid) + ret = EINVAL; + break; + case STRIOCRESCWD: + if (!fst->fd_pid) { + ret = EINVAL; + goto unlock; + } + fdp = p->td_proc->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; + /* Note that we are normal again */ + fst->fd_pid = 0; + fst->fd_cdir = fst->fd_rdir = NULL; + goto unlock; + case STRIOCANSWER: + pid = ((struct systrace_answer *)data)->stra_pid; + if (!pid) + ret = EINVAL; + break; + case STRIOCIO: + pid = ((struct systrace_io *)data)->strio_pid; + if (!pid) + ret = EINVAL; + break; + case STRIOCGETCWD: + pid = *(pid_t *)data; + if (!pid) + ret = EINVAL; + break; + case STRIOCPOLICY: + break; + case STRIOCREPLACE: + pid = ((struct systrace_replace *)data)->strr_pid; + if (!pid) + ret = EINVAL; + break; + default: + ret = EINVAL; + break; + } + + if (ret) + return (ret); + + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, p); + if (pid) { + strp = systrace_findpid(fst, pid); + if (strp == NULL) { + ret = ESRCH; + goto unlock; + } + } + + switch (com) { + 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; + case STRIOCPOLICY: + ret = systrace_policy(fst, (struct systrace_policy *)data); + break; + case STRIOCREPLACE: + ret = systrace_preprepl(strp, (struct systrace_replace *)data); + break; + case STRIOCGETCWD: + ret = systrace_getcwd(fst, strp); + break; + default: + ret = EINVAL; + break; + } + + unlock: + lockmgr(&fst->lock, LK_RELEASE, NULL, p); + return (ret); +} + +/* ARGSUSED */ +int +systracef_select(struct file *fp, int events, + struct ucred *cred, struct thread *p) +{ + struct fsystrace *fst = (struct fsystrace *)fp->f_data; + int ready = 0; + + if (events != FREAD) /* XXX which -> events? */ + return (0); + + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, p); + ready = TAILQ_FIRST(&fst->messages) != NULL; + if (!ready) + selrecord(p, &fst->si); + lockmgr(&fst->lock, LK_RELEASE, NULL, p); + + return (ready); +} + +/* ARGSUSED */ +int +systracef_kqfilter(struct file *fp, struct knote *kn) +{ + return (1); +} + +/* ARGSUSED */ +int +systracef_stat(struct file *fp, struct stat *sb, + struct ucred *active_cred, struct thread *p) +{ + return (EOPNOTSUPP); +} + +/* ARGSUSED */ +int +systracef_close(struct file *fp, struct thread *p) +{ + struct fsystrace *fst = (struct fsystrace *)fp->f_data; + struct str_process *strp; + struct str_policy *strpol; + + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, p); + + /* Untrace all processes */ + for (strp = TAILQ_FIRST(&fst->processes); strp; + strp = TAILQ_FIRST(&fst->processes)) { + struct proc *q = strp->proc; + + systrace_detach(strp); + psignal(q, SIGKILL); + } + + /* Clean up fork and exit messages */ + for (strp = TAILQ_FIRST(&fst->messages); strp; + strp = TAILQ_FIRST(&fst->messages)) { + TAILQ_REMOVE(&fst->messages, strp, msg_next); + FREE(strp, M_SYSTRACE); + } + + /* 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); + lockmgr(&fst->lock, LK_RELEASE, NULL, p); + + FREE(fp->f_data, M_SYSTRACE); + fp->f_data = NULL; + + return (0); +} + +void +systrace_lock(void) +{ + lockmgr(&systrace_lck, LK_EXCLUSIVE, NULL, curthread); +} + +void +systrace_unlock(void) +{ + lockmgr(&systrace_lck, LK_RELEASE, NULL, curthread); +} + +void +systraceattach(dev_t dev) +{ + static int init_done = 0; + if (!init_done) { + lockinit(&systrace_lck, PLOCK, "systrace", 0, 0); + init_done++; + } +} + +int +systraceopen(dev_t dev, int flags, int fmt, struct thread *td) +{ + return (0); +} + +int +systraceclose(dev_t dev, int flag, int mode, struct thread *p) +{ + return (0); +} + +int +systraceread(dev_t dev, struct uio * uio, int ioflag) +{ + return (EIO); +} + +int +systracewrite(dev_t dev, struct uio * uio, int ioflag) +{ + return (EIO); +} + +int +systraceioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *p) +{ + struct file *f; + struct fsystrace *fst = NULL; + int fd, error; + + switch (cmd) { + case SYSTR_CLONE: + MALLOC(fst, struct fsystrace *, sizeof(struct fsystrace), + M_SYSTRACE, M_WAITOK); + + memset(fst, 0, sizeof(struct fsystrace)); + lockinit(&fst->lock, PLOCK, "systrace", 0, 0); + TAILQ_INIT(&fst->processes); + TAILQ_INIT(&fst->messages); + TAILQ_INIT(&fst->policies); + + if (suser(p) == 0) + fst->issuser = 1; + + error = falloc(p, &f, &fd); /* XXX no corresponding ffree. */ + if (error) { + FREE(fst, M_SYSTRACE); + return (error); + } + f->f_flag = FREAD | FWRITE; + f->f_type = DTYPE_SYSTRACE; + f->f_ops = &systracefops; + f->f_data = (caddr_t) fst; + *(int *)data = fd; + break; + default: + error = EINVAL; + break; + } + return (error); +} + +int +systraceselect(dev_t dev, int events, struct thread *td) +{ + return (0); +} + +void +systrace_wakeup(struct fsystrace *fst) +{ + wakeup((caddr_t)fst); + selwakeup(&fst->si); +} + +struct proc * +systrace_find(struct str_process *strp) +{ + struct proc *proc; + + if ((proc = pfind(strp->pid)) == NULL) + return (NULL); + + if (proc != strp->proc) + return (NULL); + + if (!ISSET(proc->p_flag, P_SYSTRACE)) + return (NULL); + + return (proc); +} + +void +systrace_exit(struct proc *proc) +{ + struct str_process *strp; + struct fsystrace *fst; + + systrace_lock(); + strp = proc->p_systrace; + if (strp != NULL) { + fst = strp->parent; + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, curthread); + systrace_unlock(); + + /* Insert Exit message */ + systrace_msg_child(fst, strp, -1); + + systrace_detach(strp); + lockmgr(&fst->lock, LK_RELEASE, NULL, curthread); + } else + systrace_unlock(); + CLR(proc->p_flag, P_SYSTRACE); +} + +void +systrace_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; + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, curthread); + systrace_unlock(); + + if (systrace_insert_process(fst, p)) + goto out; + if ((strp = systrace_findpid(fst, p->p_pid)) == NULL) + panic("systrace_fork"); + + /* Reference policy */ + if ((strp->policy = oldstrp->policy) != NULL) + strp->policy->refcount++; + + /* Insert fork message */ + systrace_msg_child(fst, oldstrp, p->p_pid); + out: + lockmgr(&fst->lock, LK_RELEASE, NULL, curthread); +} + +int +systrace_redirect(int code, struct thread *td, void *v) +{ + struct sysent *callp; + struct str_process *strp; + struct str_policy *strpolicy; + struct fsystrace *fst = NULL; + int policy, error = 0, report = 0; + + systrace_lock(); + strp = td->td_proc->p_systrace; + if (strp == NULL) { + systrace_unlock(); + return (EINVAL); + } + + KASSERT(strp->proc == td->td_proc, ("systrace: wrong proc!")); + + fst = strp->parent; + + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, td); + systrace_unlock(); + + if ((td->td_proc->p_flag & P_SUGID) && !fst->issuser) { + /* We can not monitor a SUID process unless we are root, + * but we wait until it executes something unprivileged. + */ + 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 = (td->td_proc->p_sysent->sv_table) + code; + + switch (policy) { + case SYSTR_POLICY_PERMIT: + break; + case SYSTR_POLICY_ASK: + /* Puts the current process to sleep, return unlocked */ + error = systrace_msg_ask(fst,strp, code, callp->sy_narg, v); + + /* We might have detached by now for some reason */ + fst = NULL; + if (!error && (strp = td->td_proc->p_systrace) != NULL) { + /* XXX - do I need to lock here? */ + if (strp->answer == SYSTR_POLICY_NEVER) + error = strp->error; + else { + if (ISSET(strp->flags, STR_PROC_SYSCALLRES)) { + CLR(strp->flags, STR_PROC_SYSCALLRES); + report = 1; + } + /* Replace the arguments if necessary */ + if (strp->replace != NULL) { + error = systrace_replace(strp, callp->sy_narg, v); + } + } + } + break; + default: + if (policy > 0) + error = policy; + else + error = EPERM; + break; + } + + if (fst) { + lockmgr(&fst->lock, LK_RELEASE, NULL, td); + fst = NULL; + } + + if (!error) { + struct sysentvec *old_sysentvec = td->td_proc->p_sysent; + + error = (*callp->sy_call)(td, v); + + if (td->td_proc->p_flag & P_SUGID) { + /* Stupid Locking not necessary */ + if ((strp = td->td_proc->p_systrace) == NULL || + (fst = strp->parent) == NULL) + return (error); + if (!fst->issuser) + return (error); + } + + /* Report change in emulation */ + systrace_lock(); + strp = td->td_proc->p_systrace; + + /* See if we should force a report */ + if (strp != NULL && ISSET(strp->flags, STR_PROC_REPORT)) { + CLR(strp->flags, STR_PROC_REPORT); + old_sysentvec = NULL; + } + + if (td->td_proc->p_sysent != old_sysentvec && strp != NULL) { + fst = strp->parent; + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, td); + 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(); + + /* Report result from system call */ + systrace_lock(); + if (report && (strp = td->td_proc->p_systrace) != NULL) { + fst = strp->parent; + lockmgr(&fst->lock, LK_EXCLUSIVE, NULL, td); + systrace_unlock(); + + systrace_msg_result(fst, strp, error, code, + callp->sy_narg, v); + } else + systrace_unlock(); + } + + return (error); +} + +/* Called with fst locked */ + +int +systrace_answer(struct str_process *strp, struct systrace_answer *ans) +{ + int error = 0; + + DPRINTF(("%s: %d: policy %d\n", __func__, + ans->stra_pid, ans->stra_policy)); + + if (!POLICY_VALID(ans->stra_policy)) { + error = EINVAL; + goto out; + } + + if ((error = systrace_processready(strp)) != 0) + 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); + + /* Clearing the flag indicates to the process that it woke up */ + CLR(strp->flags, STR_PROC_WAITANSWER); + wakeup(strp); + out: + + return (error); +} + +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 */ + if (strpol->sv && strpol->sv != strp->proc->p_sysent) + return (EINVAL); + + if (strp->policy) + systrace_closepolicy(fst, strp->policy); + strp->policy = strpol; + strpol->refcount++; + + /* Record emulation for this policy */ + if (strpol->sv == NULL) + strpol->sv = strp->proc->p_sysent; + + break; + case SYSTR_POLICY_MODIFY: + DPRINTF(("%s: %d: code %d -> policy %d\n", __func__, + pol->strp_num, pol->strp_code, pol->strp_policy)); + 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)) + return (EINVAL); + + if (!ISSET(strp->flags, STR_PROC_WAITANSWER)) + return (EINVAL); + + if (strp->proc->p_sflag != SSLEEP) + return (EBUSY); + + return (0); +} + +int +systrace_getcwd(struct fsystrace *fst, struct str_process *strp) +{ + struct filedesc *myfdp, *fdp; + int error; + + DPRINTF(("%s: %d\n", __func__, strp->pid)); + + error = systrace_processready(strp); + if (error) + return (error); + + myfdp = curproc->p_fd; + 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); + + return (0); +} + +int +systrace_io(struct str_process *strp, struct systrace_io *io) +{ + struct uio uio; + struct iovec iov; + int error = 0; + + DPRINTF(("%s: %d: %p(%d)\n", __func__, + io->strio_pid, io->strio_offs, io->strio_len)); + + 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; + uio.uio_offset = (off_t)(long)io->strio_offs; + uio.uio_resid = io->strio_len; + uio.uio_segflg = UIO_USERSPACE; + uio.uio_td = curthread; /* xxx? */ + + /* error = procfs_doprocmem(p, t, NULL, &uio); xxx */ + 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; + struct thread *td = curthread; + + if ((proc = pfind(pid)) == NULL) { + error = ESRCH; + goto out; + } + + if (ISSET(proc->p_flag, P_INEXEC)) { + error = EAGAIN; + goto out; + } + + /* + * 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 + */ + if (ISSET(proc->p_flag, P_SYSTEM)) { + error = EPERM; + goto out; + } + + /* + * (3) it's being traced already + */ + if (ISSET(proc->p_flag, P_SYSTRACE)) { + error = EBUSY; + goto out; + } + + /* + * (4) it's not owned by you, or the last exec + * gave us setuid/setgid privs (unless + * you're root), or... + * + * [Note: once P_SUGID gets set in execve(), it stays + * set until the process does another execve(). Hence + * this prevents a setuid process which revokes it's + * special privilidges using setuid() from being + * traced. This is good security.] + */ + if ((proc->p_singlethread->td_ucred->cr_ruid != td->td_ucred->cr_ruid || + ISSET(proc->p_flag, P_SUGID)) && + (error = suser(td)) != 0) + goto out; + + /* + * (5) ...it's init, which controls the security level + * of the entire system, and the system was not + * compiled with permanently insecure mode turned + * on. + */ + if ((proc->p_pid == 1) && (securelevel > -1)) { + error = EPERM; + goto out; + } + + error = systrace_insert_process(fst, proc); + + out: + return (error); +} + +/* 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); + + if (strp->replace != NULL) + free(strp->replace, M_SYSTRACE); + + if (repl->strr_nrepl < 0 || repl->strr_nrepl > SYSTR_MAXARGS) + return (EINVAL); + + for (i = 0, len = 0; i < repl->strr_nrepl; i++) { + len += repl->strr_offlen[i]; + if (repl->strr_offlen[i] == 0) + continue; + 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 */ + if (repl->strr_len > 2048) + return (EINVAL); + + MALLOC(strp->replace,struct systrace_replace *, + sizeof(struct systrace_replace) + len, M_SYSTRACE, 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_SYSTRACE); + strp->replace = NULL; + return (ret); + } + + /* Adjust the offset */ + repl = strp->replace; + repl->strr_base = (caddr_t)(repl + 1); + + return (0); +} + +/* + * Memory allocation from the 'stack gap' region below the stack. + */ +static __inline caddr_t stackgap_init(void); +static __inline void *stackgap_alloc(caddr_t *, size_t); + +#define szsigcode (*(curproc->p_sysent->sv_szsigcode)) + +static __inline caddr_t +stackgap_init() +{ + return (caddr_t)(PS_STRINGS - szsigcode - SPARE_USRSPACE); +} + +static __inline void * +stackgap_alloc(sgp, sz) + caddr_t *sgp; + size_t sz; +{ + void *p = (void *) *sgp; + + sz = ALIGN(sz); + if (*sgp + sz > (caddr_t)(PS_STRINGS - szsigcode)) + return NULL; + *sgp += sz; + return p; +} + +/* + * Replace the arguments with arguments from the monitoring process. + */ + +int +systrace_replace(struct str_process *strp, int narg, register_t args[]) +{ + struct systrace_replace *repl = strp->replace; + caddr_t sg, kdata, udata, kbase, ubase; + int i, ind, ret = 0; + + sg = stackgap_init(); + ubase = stackgap_alloc(&sg, repl->strr_len); + + kbase = repl->strr_base; + for (i = 0; i < narg && i < repl->strr_nrepl; i++) { + ind = repl->strr_argind[i]; + if (ind < 0 || ind >= narg) { + ret = EINVAL; + goto out; + } + if (repl->strr_offlen[i] == 0) { + args[ind] = repl->strr_off[i]; + continue; + } + kdata = kbase + repl->strr_off[i]; + udata = ubase + repl->strr_off[i]; + if (copyout(kdata, udata, repl->strr_offlen[i])) { + ret = EINVAL; + goto out; + } + + /* Replace the argument with the new address */ + args[ind] = (register_t)udata; + } + + out: + free(repl, M_SYSTRACE); + strp->replace = NULL; + return (ret); +} + +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; + + 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; + + DPRINTF(("%s: Trying to detach from %d\n", __func__, strp->pid)); + + if ((proc = systrace_find(strp)) != NULL) { + CLR(proc->p_flag, P_SYSTRACE); + 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); + + if (ISSET(strp->flags, STR_PROC_ONQUEUE)) + TAILQ_REMOVE(&fst->messages, strp, msg_next); + + TAILQ_REMOVE(&fst->processes, strp, next); + fst->nprocesses--; + + if (strp->policy) + systrace_closepolicy(fst, strp->policy); + if (strp->replace) + free(strp->replace, M_SYSTRACE); + FREE(strp, M_SYSTRACE); + + return (error); +} + +void +systrace_closepolicy(struct fsystrace *fst, struct str_policy *policy) +{ + if (--policy->refcount) + return; + + fst->npolicies--; + + if (policy->nsysent) + free(policy->sysent, M_SYSTRACE); + + TAILQ_REMOVE(&fst->policies, policy, next); + + FREE(policy, M_SYSTRACE); +} + + +int +systrace_insert_process(struct fsystrace *fst, struct proc *proc) +{ + struct str_process *strp; + + MALLOC(strp, struct str_process *, + sizeof(struct str_process), M_SYSTRACE, M_WAITOK); + if (strp == NULL) + return (ENOBUFS); + + memset((caddr_t)strp, 0, sizeof(struct str_process)); + strp->pid = proc->p_pid; + strp->proc = proc; + strp->parent = fst; + + TAILQ_INSERT_TAIL(&fst->processes, strp, next); + fst->nprocesses++; + + proc->p_systrace = strp; + SET(proc->p_flag, P_SYSTRACE); + + return (0); +} + +struct str_policy * +systrace_newpolicy(struct fsystrace *fst, int maxents) +{ + struct str_policy *pol; + int i; + + if (fst->npolicies > SYSTR_MAX_POLICIES && !fst->issuser) + return (NULL); + + MALLOC(pol, struct str_policy *, + sizeof(struct str_policy), M_SYSTRACE, M_WAITOK); + if (pol == NULL) + return (NULL); + + DPRINTF(("%s: allocating %d -> %d\n", __func__, + maxents, maxents * sizeof(int))); + + memset((caddr_t)pol, 0, sizeof(struct str_policy)); + + pol->sysent = (u_char *)malloc(maxents * sizeof(u_char), + M_SYSTRACE, 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 +systrace_msg_ask(struct fsystrace *fst, struct str_process *strp, + int code, int narg, register_t args[]) +{ + struct str_msg_ask *msg_ask = &strp->msg.msg_data.msg_ask; + int i; + + msg_ask->code = code; + msg_ask->narg = narg; + for (i = 0; i < narg && i < SYSTR_MAXARGS; i++) + msg_ask->args[i] = args[i]; + + return (systrace_make_msg(strp, SYSTR_MSG_ASK)); +} + +int +systrace_msg_result(struct fsystrace *fst, struct str_process *strp, + int error, int code, int narg, register_t args[]) +{ + struct str_msg_ask *msg_ask = &strp->msg.msg_data.msg_ask; + int i; + + msg_ask->code = code; + msg_ask->narg = narg; + msg_ask->result = error; + for (i = 0; i < narg && i < SYSTR_MAXARGS; i++) + msg_ask->args[i] = args[i]; + + return (systrace_make_msg(strp, SYSTR_MSG_RES)); +} + +int +systrace_msg_emul(struct fsystrace *fst, struct str_process *strp) +{ + /* XXX p->p_emul doesn't exist for FreeBSD. + struct str_msg_emul *msg_emul = &strp->msg.msg_data.msg_emul; + struct proc *p = strp->proc; + memcpy(msg_emul->emul, p->p_emul->e_name, SYSTR_EMULEN); */ + + return (systrace_make_msg(strp, SYSTR_MSG_EMUL)); +} + +int +systrace_make_msg(struct str_process *strp, int type) +{ + struct str_message *msg = &strp->msg; + struct fsystrace *fst = strp->parent; + struct proc *p = strp->proc; + int st; + + 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; + + TAILQ_INSERT_TAIL(&fst->messages, strp, msg_next); + SET(strp->flags, STR_PROC_ONQUEUE); + + out: + systrace_wakeup(fst); + + /* Release the lock - XXX */ + lockmgr(&fst->lock, LK_RELEASE, NULL, p->p_singlethread); /* xxx */ + + while (1) { + st = tsleep(strp, PWAIT | PCATCH, "systrmsg", 0); + if (st != 0) + return (EINTR); + /* 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) +{ + struct str_process *nstrp; + struct str_message *msg; + struct str_msg_child *msg_child; + + nstrp = malloc(sizeof(struct str_process), M_SYSTRACE, M_WAITOK); + + memset(nstrp, 0, sizeof(struct str_process)); + + DPRINTF(("%s: %p: pid %d -> pid %d\n", __func__, + nstrp, strp->pid, npid)); + + msg = &nstrp->msg; + 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; + + TAILQ_INSERT_TAIL(&fst->messages, nstrp, msg_next); + + systrace_wakeup(fst); + + return (0); +} + +/* Local Variables: */ +/* c-file-style:"bsd" */ +/* End: */ diff -rub orig/sys/dev/systrace/systrace.h sys/dev/systrace/systrace.h --- orig/sys/dev/systrace/systrace.h Tue Jul 15 19:05:07 2003 +++ sys/dev/systrace/systrace.h Tue Jul 15 20:30:16 2003 @@ -0,0 +1,184 @@ +/* + * Copyright 2002 Niels Provos + * 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. + */ + +#ifndef _SYSTRACE_H_ +#define _SYSTRACE_H_ + +#include +#include +#include + +#ifdef _KERNEL +MALLOC_DECLARE(M_SYSTRACE); +#endif /* _KERNEL */ + +#define SYSTR_CLONE _IOR('s', 1, int) + +#define SYSTR_EMULEN 16 /* sync with sys proc */ + +struct str_msg_emul { + char emul[SYSTR_EMULEN]; +}; + +#define SYSTR_MAX_POLICIES 64 +#define SYSTR_MAXARGS 64 + +struct str_msg_ask { + int code; + int narg; + register_t args[SYSTR_MAXARGS]; + register_t rval[2]; + int result; +}; + +/* Queued on fork or exit of a process */ + +struct str_msg_child { + pid_t new_pid; +}; + +#define SYSTR_MSG_ASK 1 +#define SYSTR_MSG_RES 2 +#define SYSTR_MSG_EMUL 3 +#define SYSTR_MSG_CHILD 4 + +#define SYSTR_MSG_NOPROCESS(x) \ + ((x)->msg.msg_type == SYSTR_MSG_CHILD) + +struct str_message { + int msg_type; + pid_t msg_pid; + short msg_policy; + short reserved; + union { + struct str_msg_emul msg_emul; + struct str_msg_ask msg_ask; + struct str_msg_child msg_child; + } msg_data; +}; + +struct systrace_answer { + pid_t stra_pid; + int stra_policy; + int stra_error; + int stra_flags; +}; + +#define SYSTR_READ 1 +#define SYSTR_WRITE 2 + +struct systrace_io { + pid_t strio_pid; + int strio_op; + void *strio_offs; + void *strio_addr; + size_t strio_len; +}; + +#define SYSTR_POLICY_NEW 1 +#define SYSTR_POLICY_ASSIGN 2 +#define SYSTR_POLICY_MODIFY 3 + +struct systrace_policy { + int strp_op; + int strp_num; + union { + struct { + short code; + short policy; + } assign; + pid_t pid; + int maxents; + } strp_data; +}; + +#define strp_pid strp_data.pid +#define strp_maxents strp_data.maxents +#define strp_code strp_data.assign.code +#define strp_policy strp_data.assign.policy + +struct systrace_replace { + pid_t strr_pid; + int strr_nrepl; + caddr_t strr_base; /* Base memory */ + size_t strr_len; /* Length of memory */ + int strr_argind[SYSTR_MAXARGS]; + size_t strr_off[SYSTR_MAXARGS]; + size_t strr_offlen[SYSTR_MAXARGS]; +}; + +#define STRIOCATTACH _IOW('s', 101, pid_t) +#define STRIOCDETACH _IOW('s', 102, pid_t) +#define STRIOCANSWER _IOW('s', 103, struct systrace_answer) +#define STRIOCIO _IOWR('s', 104, struct systrace_io) +#define STRIOCPOLICY _IOWR('s', 105, struct systrace_policy) +#define STRIOCGETCWD _IOW('s', 106, pid_t) +#define STRIOCRESCWD _IO('s', 107) +#define STRIOCREPORT _IOW('s', 108, pid_t) +#define STRIOCREPLACE _IOW('s', 109, struct systrace_replace) + +#define SYSTR_POLICY_ASK 0 +#define SYSTR_POLICY_PERMIT 1 +#define SYSTR_POLICY_NEVER 2 + +#define SYSTR_FLAGS_RESULT 0x001 + +#ifdef _KERNEL +struct str_process; +struct fsystrace { + struct lock lock; + struct selinfo si; + + TAILQ_HEAD(strprocessq, str_process) processes; + int nprocesses; + + TAILQ_HEAD(strpolicyq, str_policy) policies; + + struct strprocessq messages; + + int npolicynr; + int npolicies; + + int issuser; + + /* cwd magic */ + pid_t fd_pid; + struct vnode *fd_cdir; + struct vnode *fd_rdir; +}; + +/* Internal prototypes */ + +int systrace_redirect(int, struct thread *, void * /*, register_t * */); +void systrace_exit(struct proc *); +void systrace_fork(struct proc *, struct proc *); + +#endif /* _KERNEL */ +#endif /* _SYSTRACE_H_ */ diff -rub orig/sys/i386/conf/GENERIC sys/i386/conf/GENERIC --- orig/sys/i386/conf/GENERIC Sat May 31 10:18:41 2003 +++ sys/i386/conf/GENERIC Tue Jul 15 16:58:52 2003 @@ -235,6 +235,7 @@ device md # Memory "disks" device gif # IPv6 and IPv4 tunneling device faith # IPv6-to-IPv4 relaying (translation) +device systrace # system call filtering device # The `bpf' device enables the Berkeley Packet Filter. # Be aware of the administrative consequences of enabling this! diff -rub orig/sys/i386/conf/VMWARE40 sys/i386/conf/VMWARE40 --- orig/sys/i386/conf/VMWARE40 Tue Jul 15 15:09:19 2003 +++ sys/i386/conf/VMWARE40 Tue Jul 15 16:59:58 2003 @@ -105,6 +105,7 @@ device pty # Pseudo-ttys (telnet etc) device gif # IPv6 and IPv4 tunneling device faith # IPv6-to-IPv4 relaying (translation) +device systrace # system call filtering device # The `bpf' device enables the Berkeley Packet Filter. # Be aware of the administrative consequences of enabling this! diff -rub orig/sys/i386/i386/trap.c sys/i386/i386/trap.c --- orig/sys/i386/i386/trap.c Tue May 20 15:50:33 2003 +++ sys/i386/i386/trap.c Tue Jul 15 17:25:40 2003 @@ -49,6 +49,7 @@ #include "opt_ktrace.h" #include "opt_npx.h" #include "opt_trap.h" +#include "opt_systrace.h" #include #include @@ -66,6 +67,7 @@ #include #include #include +#include #ifdef KTRACE #include #endif @@ -100,6 +102,8 @@ #include +#include + int (*pmath_emulate)(struct trapframe *); extern void trap(struct trapframe frame); @@ -1018,6 +1022,11 @@ STOPEVENT(p, S_SCE, narg); +#if defined(__i386__) && (NSYSTRACE > 0) + if (p->p_flag & P_SYSTRACE) + error = systrace_redirect(code, p, args); + else +#endif error = (*callp->sy_call)(td, args); } diff -rub orig/sys/i386/linux/Makefile sys/i386/linux/Makefile --- orig/sys/i386/linux/Makefile Wed Nov 1 02:47:33 2000 +++ sys/i386/linux/Makefile Tue Jul 15 16:09:22 2003 @@ -7,8 +7,8 @@ sysent: linux_sysent.c linux_syscall.h linux_proto.h -linux_sysent.c linux_syscall.h linux_proto.h: ../../kern/makesyscalls.sh \ - syscalls.master syscalls.conf +linux_syscalls.c linux_sysent.c linux_syscall.h linux_proto.h: \ + ../../kern/makesyscalls.sh syscalls.master syscalls.conf -mv -f linux_sysent.c linux_sysent.c.bak -mv -f linux_syscall.h linux_syscall.h.bak -mv -f linux_proto.h linux_proto.h.bak diff -rub orig/sys/i386/linux/syscalls.conf sys/i386/linux/syscalls.conf --- orig/sys/i386/linux/syscalls.conf Fri Aug 27 19:45:25 1999 +++ sys/i386/linux/syscalls.conf Tue Jul 15 16:09:22 2003 @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/i386/linux/syscalls.conf,v 1.5 1999/08/28 00:45:25 peter Exp $ -sysnames="/dev/null" +sysnames="linux_syscalls.c" sysproto="linux_proto.h" sysproto_h=_LINUX_SYSPROTO_H_ syshdr="linux_syscall.h" diff -rub orig/sys/kern/kern_exit.c sys/kern/kern_exit.c --- orig/sys/kern/kern_exit.c Tue May 13 15:35:59 2003 +++ sys/kern/kern_exit.c Tue Jul 15 17:26:00 2003 @@ -42,6 +42,7 @@ #include "opt_compat.h" #include "opt_ktrace.h" #include "opt_mac.h" +#include "opt_systrace.h" #include #include @@ -76,6 +77,9 @@ #include #include #include + +#include + #include #include #include @@ -350,6 +354,10 @@ vrele(tracevp); if (tracecred != NULL) crfree(tracecred); +#endif +#if defined(__i386__) && (NSYSTRACE > 0) + if (p->p_flag & P_SYSTRACE) + systrace_exit(p); #endif /* * Release reference to text vnode diff -rub orig/sys/kern/kern_fork.c sys/kern/kern_fork.c --- orig/sys/kern/kern_fork.c Tue May 13 15:35:59 2003 +++ sys/kern/kern_fork.c Tue Jul 15 17:25:09 2003 @@ -41,6 +41,7 @@ #include "opt_ktrace.h" #include "opt_mac.h" +#include "opt_systrace.h" #include #include @@ -69,6 +70,10 @@ #include #include + +/* #include "systrace.h" */ +#include + #include #include #include @@ -588,6 +593,10 @@ p2->p_tracecred = crhold(p1->p_tracecred); } } +#if defined(__i386__) && (NSYSTRACE > 0) + if (p1->p_flag & P_SYSTRACE) + systrace_fork(p1, p2); +#endif mtx_unlock(&ktrace_mtx); #endif diff -rub orig/sys/modules/Makefile sys/modules/Makefile --- orig/sys/modules/Makefile Tue May 13 06:26:08 2003 +++ sys/modules/Makefile Tue Jul 15 16:13:41 2003 @@ -200,6 +200,7 @@ sr \ stg \ streams \ + systrace \ vinum \ wi \ xe diff -rub orig/sys/modules/systrace/Makefile sys/modules/systrace/Makefile --- orig/sys/modules/systrace/Makefile Tue Jul 15 19:05:51 2003 +++ sys/modules/systrace/Makefile Tue Jul 15 17:17:55 2003 @@ -0,0 +1,7 @@ + +.PATH: ${.CURDIR}/../../dev/systrace + +KMOD= systrace +SRCS= systrace.c systrace.h opt_systrace.h + +.include diff -rub orig/sys/sys/file.h sys/sys/file.h --- orig/sys/sys/file.h Sat Feb 15 00:04:55 2003 +++ sys/sys/file.h Tue Jul 15 18:55:02 2003 @@ -62,6 +62,7 @@ #define DTYPE_FIFO 4 /* fifo (named pipe) */ #define DTYPE_KQUEUE 5 /* event queue */ #define DTYPE_CRYPTO 6 /* crypto */ +#define DTYPE_SYSTRACE 6 /* systrace */ #ifdef _KERNEL diff -rub orig/sys/sys/proc.h sys/sys/proc.h --- orig/sys/sys/proc.h Tue Jun 3 15:43:18 2003 +++ sys/sys/proc.h Tue Jul 15 20:06:55 2003 @@ -599,6 +599,7 @@ void *p_emuldata; /* (c) Emulator state data. */ struct label p_label; /* (*) Process (not subject) MAC label */ struct p_sched *p_sched; /* (*) Scheduler specific data */ + void *p_systrace; /* Back pointer to systrace */ }; #define p_rlimit p_limit->pl_rlimit @@ -643,6 +644,7 @@ #define P_JAILED 0x1000000 /* Process is in jail. */ #define P_ALTSTACK 0x2000000 /* Have alternate signal stack. */ #define P_INEXEC 0x4000000 /* Process is in execve(). */ +#define P_SYSTRACE 0x800000 /* Process system call tracing active*/ /* These flags are kept in p_sflag and are protected with sched_lock. */ #define PS_INMEM 0x00001 /* Loaded into memory. */