2012-06-04 22:54:19 +00:00
|
|
|
/*-
|
2017-11-27 14:52:40 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2012-06-04 22:54:19 +00:00
|
|
|
* Copyright (c) 2011, David E. O'Brien.
|
|
|
|
* Copyright (c) 2009-2011, Juniper Networks, Inc.
|
2016-03-21 20:29:27 +00:00
|
|
|
* Copyright (c) 2015-2016, EMC Corp.
|
2012-06-04 22:54:19 +00: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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JUNIPER NETWORKS AND CONTRIBUTORS ``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 JUNIPER NETWORKS OR CONTRIBUTORS 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2016-01-28 00:51:17 +00:00
|
|
|
#include <sys/eventhandler.h>
|
2016-03-21 20:29:53 +00:00
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/imgact.h>
|
2016-05-27 23:57:43 +00:00
|
|
|
#include <sys/priv.h>
|
2015-06-19 17:19:20 +00:00
|
|
|
#include <sys/sx.h>
|
2016-06-04 17:39:42 +00:00
|
|
|
#include <sys/sysent.h>
|
2016-01-28 21:45:25 +00:00
|
|
|
#include <sys/vnode.h>
|
2015-06-19 17:19:20 +00:00
|
|
|
|
2013-06-04 06:38:01 +00:00
|
|
|
#include "opt_compat.h"
|
|
|
|
|
2016-01-28 21:45:25 +00:00
|
|
|
static eventhandler_tag filemon_exec_tag;
|
2016-01-28 00:51:17 +00:00
|
|
|
static eventhandler_tag filemon_exit_tag;
|
2016-01-28 01:17:55 +00:00
|
|
|
static eventhandler_tag filemon_fork_tag;
|
2016-01-28 00:51:17 +00:00
|
|
|
|
2012-06-04 22:54:19 +00:00
|
|
|
static void
|
|
|
|
filemon_output(struct filemon *filemon, char *msg, size_t len)
|
|
|
|
{
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
2016-03-22 22:41:07 +00:00
|
|
|
int error;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
if (filemon->fp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
aiov.iov_base = msg;
|
|
|
|
aiov.iov_len = len;
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_resid = len;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_td = curthread;
|
|
|
|
auio.uio_offset = (off_t) -1;
|
|
|
|
|
2016-03-07 21:10:19 +00:00
|
|
|
if (filemon->fp->f_type == DTYPE_VNODE)
|
|
|
|
bwillwrite();
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-05-27 23:57:53 +00:00
|
|
|
error = fo_write(filemon->fp, &auio, filemon->cred, 0, curthread);
|
2016-05-27 23:58:00 +00:00
|
|
|
if (error != 0 && filemon->error == 0)
|
2016-03-22 22:41:07 +00:00
|
|
|
filemon->error = error;
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_wrapper_chdir(struct thread *td, struct chdir_args *uap)
|
|
|
|
{
|
2016-03-22 22:41:14 +00:00
|
|
|
int error, ret;
|
2012-06-04 22:54:19 +00:00
|
|
|
size_t len;
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
|
|
|
if ((ret = sys_chdir(td, uap)) == 0) {
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(curproc)) != NULL) {
|
2016-03-22 22:41:14 +00:00
|
|
|
if ((error = copyinstr(uap->path, filemon->fname1,
|
|
|
|
sizeof(filemon->fname1), NULL)) != 0) {
|
|
|
|
filemon->error = error;
|
|
|
|
goto copyfail;
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "C %d %s\n",
|
|
|
|
curproc->p_pid, filemon->fname1);
|
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-22 22:41:14 +00:00
|
|
|
copyfail:
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-01-28 21:45:25 +00:00
|
|
|
static void
|
|
|
|
filemon_event_process_exec(void *arg __unused, struct proc *p,
|
|
|
|
struct image_params *imgp)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
2016-01-28 21:45:25 +00:00
|
|
|
size_t len;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(p)) != NULL) {
|
2016-01-28 21:45:25 +00:00
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "E %d %s\n",
|
2016-05-26 23:27:08 +00:00
|
|
|
p->p_pid,
|
|
|
|
imgp->execpath != NULL ? imgp->execpath : "<unknown>");
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-01-28 21:45:25 +00:00
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-05-27 23:57:43 +00:00
|
|
|
/* If the credentials changed then cease tracing. */
|
|
|
|
if (imgp->newcred != NULL &&
|
|
|
|
imgp->credential_setid &&
|
|
|
|
priv_check_cred(filemon->cred,
|
|
|
|
PRIV_DEBUG_DIFFCRED, 0) != 0) {
|
|
|
|
/*
|
|
|
|
* It may have changed to NULL already, but
|
|
|
|
* will not be re-attached by anything else.
|
|
|
|
*/
|
|
|
|
if (p->p_filemon != NULL) {
|
|
|
|
KASSERT(p->p_filemon == filemon,
|
|
|
|
("%s: proc %p didn't have expected"
|
|
|
|
" filemon %p", __func__, p, filemon));
|
|
|
|
filemon_proc_drop(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
static void
|
|
|
|
_filemon_wrapper_openat(struct thread *td, char *upath, int flags, int fd)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
2016-03-22 22:41:14 +00:00
|
|
|
int error;
|
2012-06-04 22:54:19 +00:00
|
|
|
size_t len;
|
2016-03-21 20:29:53 +00:00
|
|
|
struct file *fp;
|
2012-06-04 22:54:19 +00:00
|
|
|
struct filemon *filemon;
|
2016-03-21 20:29:53 +00:00
|
|
|
char *atpath, *freepath;
|
|
|
|
cap_rights_t rights;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
if ((filemon = filemon_proc_get(curproc)) != NULL) {
|
2016-03-21 20:29:53 +00:00
|
|
|
atpath = "";
|
|
|
|
freepath = NULL;
|
|
|
|
fp = NULL;
|
|
|
|
|
2016-03-22 22:41:14 +00:00
|
|
|
if ((error = copyinstr(upath, filemon->fname1,
|
|
|
|
sizeof(filemon->fname1), NULL)) != 0) {
|
|
|
|
filemon->error = error;
|
|
|
|
goto copyfail;
|
|
|
|
}
|
2016-03-21 20:29:43 +00:00
|
|
|
|
|
|
|
if (filemon->fname1[0] != '/' && fd != AT_FDCWD) {
|
|
|
|
/*
|
|
|
|
* rats - we cannot do too much about this.
|
|
|
|
* the trace should show a dir we read
|
|
|
|
* recently.. output an A record as a clue
|
|
|
|
* until we can do better.
|
2016-03-21 20:29:53 +00:00
|
|
|
* XXX: This may be able to come out with
|
|
|
|
* the namecache lookup now.
|
2016-03-21 20:29:43 +00:00
|
|
|
*/
|
2012-06-04 22:54:19 +00:00
|
|
|
len = snprintf(filemon->msgbufr,
|
2016-03-21 20:29:43 +00:00
|
|
|
sizeof(filemon->msgbufr), "A %d %s\n",
|
2012-06-04 22:54:19 +00:00
|
|
|
curproc->p_pid, filemon->fname1);
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-21 20:29:53 +00:00
|
|
|
/*
|
|
|
|
* Try to resolve the path from the vnode using the
|
|
|
|
* namecache. It may be inaccurate, but better
|
|
|
|
* than nothing.
|
|
|
|
*/
|
|
|
|
if (getvnode(td, fd,
|
|
|
|
cap_rights_init(&rights, CAP_LOOKUP), &fp) == 0) {
|
|
|
|
vn_fullpath(td, fp->f_vnode, &atpath,
|
|
|
|
&freepath);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
2016-03-21 20:29:43 +00:00
|
|
|
if (flags & O_RDWR) {
|
|
|
|
/*
|
|
|
|
* We'll get the W record below, but need
|
|
|
|
* to also output an R to distinguish from
|
|
|
|
* O_WRONLY.
|
|
|
|
*/
|
|
|
|
len = snprintf(filemon->msgbufr,
|
2016-03-21 20:29:53 +00:00
|
|
|
sizeof(filemon->msgbufr), "R %d %s%s%s\n",
|
|
|
|
curproc->p_pid, atpath,
|
|
|
|
atpath[0] != '\0' ? "/" : "", filemon->fname1);
|
2016-03-21 20:29:43 +00:00
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
len = snprintf(filemon->msgbufr,
|
2016-03-21 20:29:53 +00:00
|
|
|
sizeof(filemon->msgbufr), "%c %d %s%s%s\n",
|
2016-03-21 20:29:43 +00:00
|
|
|
(flags & O_ACCMODE) ? 'W':'R',
|
2016-03-21 20:29:53 +00:00
|
|
|
curproc->p_pid, atpath,
|
|
|
|
atpath[0] != '\0' ? "/" : "", filemon->fname1);
|
2016-03-21 20:29:43 +00:00
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-22 22:41:14 +00:00
|
|
|
copyfail:
|
2016-03-21 20:29:43 +00:00
|
|
|
filemon_drop(filemon);
|
2016-03-21 20:29:53 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
fdrop(fp, td);
|
|
|
|
free(freepath, M_TEMP);
|
2016-03-21 20:29:43 +00:00
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 16:31:06 +00:00
|
|
|
static int
|
2016-03-21 20:29:43 +00:00
|
|
|
filemon_wrapper_open(struct thread *td, struct open_args *uap)
|
2015-06-14 16:31:06 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
if ((ret = sys_open(td, uap)) == 0)
|
|
|
|
_filemon_wrapper_openat(td, uap->path, uap->flags, AT_FDCWD);
|
2015-06-14 16:31:06 +00:00
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2015-06-14 16:31:06 +00:00
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
static int
|
|
|
|
filemon_wrapper_openat(struct thread *td, struct openat_args *uap)
|
|
|
|
{
|
|
|
|
int ret;
|
2015-06-14 16:31:06 +00:00
|
|
|
|
2016-03-21 20:29:43 +00:00
|
|
|
if ((ret = sys_openat(td, uap)) == 0)
|
|
|
|
_filemon_wrapper_openat(td, uap->path, uap->flag, uap->fd);
|
2015-06-14 16:31:06 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:54:19 +00:00
|
|
|
static int
|
|
|
|
filemon_wrapper_rename(struct thread *td, struct rename_args *uap)
|
|
|
|
{
|
2016-03-22 22:41:14 +00:00
|
|
|
int error, ret;
|
2012-06-04 22:54:19 +00:00
|
|
|
size_t len;
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
|
|
|
if ((ret = sys_rename(td, uap)) == 0) {
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(curproc)) != NULL) {
|
2016-03-22 22:41:14 +00:00
|
|
|
if (((error = copyinstr(uap->from, filemon->fname1,
|
|
|
|
sizeof(filemon->fname1), NULL)) != 0) ||
|
|
|
|
((error = copyinstr(uap->to, filemon->fname2,
|
|
|
|
sizeof(filemon->fname2), NULL)) != 0)) {
|
|
|
|
filemon->error = error;
|
|
|
|
goto copyfail;
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "M %d '%s' '%s'\n",
|
|
|
|
curproc->p_pid, filemon->fname1, filemon->fname2);
|
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-22 22:41:14 +00:00
|
|
|
copyfail:
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
static void
|
|
|
|
_filemon_wrapper_link(struct thread *td, char *upath1, char *upath2)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
2016-03-21 23:22:19 +00:00
|
|
|
size_t len;
|
2016-03-22 22:41:14 +00:00
|
|
|
int error;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
if ((filemon = filemon_proc_get(curproc)) != NULL) {
|
2016-03-22 22:41:14 +00:00
|
|
|
if (((error = copyinstr(upath1, filemon->fname1,
|
|
|
|
sizeof(filemon->fname1), NULL)) != 0) ||
|
|
|
|
((error = copyinstr(upath2, filemon->fname2,
|
|
|
|
sizeof(filemon->fname2), NULL)) != 0)) {
|
|
|
|
filemon->error = error;
|
|
|
|
goto copyfail;
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "L %d '%s' '%s'\n",
|
|
|
|
curproc->p_pid, filemon->fname1, filemon->fname2);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-22 22:41:14 +00:00
|
|
|
copyfail:
|
2016-03-21 23:22:19 +00:00
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-21 23:22:19 +00:00
|
|
|
filemon_wrapper_link(struct thread *td, struct link_args *uap)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
if ((ret = sys_link(td, uap)) == 0)
|
|
|
|
_filemon_wrapper_link(td, uap->path, uap->link);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
static int
|
|
|
|
filemon_wrapper_symlink(struct thread *td, struct symlink_args *uap)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
if ((ret = sys_symlink(td, uap)) == 0)
|
|
|
|
_filemon_wrapper_link(td, uap->path, uap->link);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_wrapper_linkat(struct thread *td, struct linkat_args *uap)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-03-21 23:22:19 +00:00
|
|
|
if ((ret = sys_linkat(td, uap)) == 0)
|
|
|
|
_filemon_wrapper_link(td, uap->path1, uap->path2);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-28 00:51:17 +00:00
|
|
|
filemon_event_process_exit(void *arg __unused, struct proc *p)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(p)) != NULL) {
|
2012-06-04 22:54:19 +00:00
|
|
|
len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr),
|
2016-01-28 00:51:17 +00:00
|
|
|
"X %d %d %d\n", p->p_pid, p->p_xexit, p->p_xsig);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/*
|
|
|
|
* filemon_untrack_processes() may have dropped this p_filemon
|
|
|
|
* already while in filemon_proc_get() before acquiring the
|
|
|
|
* filemon lock.
|
|
|
|
*/
|
|
|
|
KASSERT(p->p_filemon == NULL || p->p_filemon == filemon,
|
|
|
|
("%s: p %p was attached while exiting, expected "
|
|
|
|
"filemon %p or NULL", __func__, p, filemon));
|
|
|
|
if (p->p_filemon == filemon)
|
|
|
|
filemon_proc_drop(p);
|
|
|
|
|
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_wrapper_unlink(struct thread *td, struct unlink_args *uap)
|
|
|
|
{
|
2016-03-22 22:41:14 +00:00
|
|
|
int error, ret;
|
2012-06-04 22:54:19 +00:00
|
|
|
size_t len;
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
|
|
|
if ((ret = sys_unlink(td, uap)) == 0) {
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(curproc)) != NULL) {
|
2016-03-22 22:41:14 +00:00
|
|
|
if ((error = copyinstr(uap->path, filemon->fname1,
|
|
|
|
sizeof(filemon->fname1), NULL)) != 0) {
|
|
|
|
filemon->error = error;
|
|
|
|
goto copyfail;
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "D %d %s\n",
|
|
|
|
curproc->p_pid, filemon->fname1);
|
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2016-03-22 22:41:14 +00:00
|
|
|
copyfail:
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2016-01-28 01:17:55 +00:00
|
|
|
static void
|
|
|
|
filemon_event_process_fork(void *arg __unused, struct proc *p1,
|
2016-01-28 01:19:19 +00:00
|
|
|
struct proc *p2, int flags __unused)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
if ((filemon = filemon_proc_get(p1)) != NULL) {
|
2016-01-28 01:17:55 +00:00
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr), "F %d %d\n",
|
|
|
|
p1->p_pid, p2->p_pid);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-01-28 01:17:55 +00:00
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/*
|
|
|
|
* filemon_untrack_processes() or
|
|
|
|
* filemon_ioctl(FILEMON_SET_PID) may have changed the parent's
|
|
|
|
* p_filemon while in filemon_proc_get() before acquiring the
|
|
|
|
* filemon lock. Only inherit if the parent is still traced by
|
|
|
|
* this filemon.
|
|
|
|
*/
|
|
|
|
if (p1->p_filemon == filemon) {
|
|
|
|
PROC_LOCK(p2);
|
|
|
|
/*
|
|
|
|
* It may have been attached to already by a new
|
|
|
|
* filemon.
|
|
|
|
*/
|
|
|
|
if (p2->p_filemon == NULL) {
|
|
|
|
p2->p_filemon = filemon_acquire(filemon);
|
|
|
|
++filemon->proccnt;
|
|
|
|
}
|
|
|
|
PROC_UNLOCK(p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
filemon_drop(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemon_wrapper_install(void)
|
|
|
|
{
|
2016-06-04 17:39:42 +00:00
|
|
|
|
|
|
|
sysent[SYS_chdir].sy_call = (sy_call_t *) filemon_wrapper_chdir;
|
|
|
|
sysent[SYS_open].sy_call = (sy_call_t *) filemon_wrapper_open;
|
|
|
|
sysent[SYS_openat].sy_call = (sy_call_t *) filemon_wrapper_openat;
|
|
|
|
sysent[SYS_rename].sy_call = (sy_call_t *) filemon_wrapper_rename;
|
|
|
|
sysent[SYS_unlink].sy_call = (sy_call_t *) filemon_wrapper_unlink;
|
|
|
|
sysent[SYS_link].sy_call = (sy_call_t *) filemon_wrapper_link;
|
|
|
|
sysent[SYS_symlink].sy_call = (sy_call_t *) filemon_wrapper_symlink;
|
|
|
|
sysent[SYS_linkat].sy_call = (sy_call_t *) filemon_wrapper_linkat;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-06-05 18:16:33 +00:00
|
|
|
#if defined(COMPAT_FREEBSD32)
|
2016-06-04 17:39:42 +00:00
|
|
|
freebsd32_sysent[FREEBSD32_SYS_chdir].sy_call = (sy_call_t *) filemon_wrapper_chdir;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_open].sy_call = (sy_call_t *) filemon_wrapper_open;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_openat].sy_call = (sy_call_t *) filemon_wrapper_openat;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_rename].sy_call = (sy_call_t *) filemon_wrapper_rename;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_unlink].sy_call = (sy_call_t *) filemon_wrapper_unlink;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_link].sy_call = (sy_call_t *) filemon_wrapper_link;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_symlink].sy_call = (sy_call_t *) filemon_wrapper_symlink;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_linkat].sy_call = (sy_call_t *) filemon_wrapper_linkat;
|
2016-06-05 18:16:33 +00:00
|
|
|
#endif /* COMPAT_FREEBSD32 */
|
2016-01-28 00:51:17 +00:00
|
|
|
|
2016-01-28 21:45:25 +00:00
|
|
|
filemon_exec_tag = EVENTHANDLER_REGISTER(process_exec,
|
|
|
|
filemon_event_process_exec, NULL, EVENTHANDLER_PRI_LAST);
|
2016-01-28 00:51:17 +00:00
|
|
|
filemon_exit_tag = EVENTHANDLER_REGISTER(process_exit,
|
|
|
|
filemon_event_process_exit, NULL, EVENTHANDLER_PRI_LAST);
|
2016-01-28 01:17:55 +00:00
|
|
|
filemon_fork_tag = EVENTHANDLER_REGISTER(process_fork,
|
|
|
|
filemon_event_process_fork, NULL, EVENTHANDLER_PRI_LAST);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemon_wrapper_deinstall(void)
|
|
|
|
{
|
2016-06-04 17:39:42 +00:00
|
|
|
|
|
|
|
sysent[SYS_chdir].sy_call = (sy_call_t *)sys_chdir;
|
|
|
|
sysent[SYS_open].sy_call = (sy_call_t *)sys_open;
|
|
|
|
sysent[SYS_openat].sy_call = (sy_call_t *)sys_openat;
|
|
|
|
sysent[SYS_rename].sy_call = (sy_call_t *)sys_rename;
|
|
|
|
sysent[SYS_unlink].sy_call = (sy_call_t *)sys_unlink;
|
|
|
|
sysent[SYS_link].sy_call = (sy_call_t *)sys_link;
|
|
|
|
sysent[SYS_symlink].sy_call = (sy_call_t *)sys_symlink;
|
|
|
|
sysent[SYS_linkat].sy_call = (sy_call_t *)sys_linkat;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-06-05 18:16:33 +00:00
|
|
|
#if defined(COMPAT_FREEBSD32)
|
2016-06-04 17:39:42 +00:00
|
|
|
freebsd32_sysent[FREEBSD32_SYS_chdir].sy_call = (sy_call_t *)sys_chdir;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_open].sy_call = (sy_call_t *)sys_open;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_openat].sy_call = (sy_call_t *)sys_openat;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_rename].sy_call = (sy_call_t *)sys_rename;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_unlink].sy_call = (sy_call_t *)sys_unlink;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_link].sy_call = (sy_call_t *)sys_link;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_symlink].sy_call = (sy_call_t *)sys_symlink;
|
|
|
|
freebsd32_sysent[FREEBSD32_SYS_linkat].sy_call = (sy_call_t *)sys_linkat;
|
2016-06-05 18:16:33 +00:00
|
|
|
#endif /* COMPAT_FREEBSD32 */
|
2016-01-28 00:51:17 +00:00
|
|
|
|
2016-01-28 21:45:25 +00:00
|
|
|
EVENTHANDLER_DEREGISTER(process_exec, filemon_exec_tag);
|
2016-01-28 00:51:17 +00:00
|
|
|
EVENTHANDLER_DEREGISTER(process_exit, filemon_exit_tag);
|
2016-01-28 01:17:55 +00:00
|
|
|
EVENTHANDLER_DEREGISTER(process_fork, filemon_fork_tag);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|