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.
|
|
|
|
*/
|
|
|
|
|
2013-06-04 15:35:37 +00:00
|
|
|
#include <sys/cdefs.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2013-06-04 06:38:01 +00:00
|
|
|
#include "opt_compat.h"
|
|
|
|
|
2013-06-04 15:35:37 +00:00
|
|
|
#include <sys/param.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
2016-02-27 21:08:27 +00:00
|
|
|
#include <sys/capsicum.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#include <sys/condvar.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/ioccom.h>
|
|
|
|
#include <sys/kernel.h>
|
2015-08-26 03:44:48 +00:00
|
|
|
#include <sys/lock.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/proc.h>
|
2015-08-26 03:44:48 +00:00
|
|
|
#include <sys/sx.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include "filemon.h"
|
|
|
|
|
2016-06-05 18:16:33 +00:00
|
|
|
#if defined(COMPAT_FREEBSD32)
|
2012-06-04 22:54:19 +00:00
|
|
|
#include <compat/freebsd32/freebsd32_syscall.h>
|
|
|
|
#include <compat/freebsd32/freebsd32_proto.h>
|
2016-06-05 18:16:33 +00:00
|
|
|
#include <compat/freebsd32/freebsd32_util.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static d_close_t filemon_close;
|
|
|
|
static d_ioctl_t filemon_ioctl;
|
|
|
|
static d_open_t filemon_open;
|
|
|
|
|
|
|
|
static struct cdevsw filemon_cdevsw = {
|
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_close = filemon_close,
|
|
|
|
.d_ioctl = filemon_ioctl,
|
|
|
|
.d_open = filemon_open,
|
|
|
|
.d_name = "filemon",
|
|
|
|
};
|
|
|
|
|
|
|
|
MALLOC_DECLARE(M_FILEMON);
|
|
|
|
MALLOC_DEFINE(M_FILEMON, "filemon", "File access monitor");
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/*
|
|
|
|
* The filemon->lock protects several things currently:
|
|
|
|
* - fname1/fname2/msgbufr are pre-allocated and used per syscall
|
|
|
|
* for logging and copyins rather than stack variables.
|
|
|
|
* - Serializing the filemon's log output.
|
|
|
|
* - Preventing inheritance or removal of the filemon into proc.p_filemon.
|
|
|
|
*/
|
2012-06-04 22:54:19 +00:00
|
|
|
struct filemon {
|
2016-03-21 20:29:27 +00:00
|
|
|
struct sx lock; /* Lock for this filemon. */
|
2012-06-04 22:54:19 +00:00
|
|
|
struct file *fp; /* Output file pointer. */
|
2016-05-27 23:57:43 +00:00
|
|
|
struct ucred *cred; /* Credential of tracer. */
|
2012-06-04 22:54:19 +00:00
|
|
|
char fname1[MAXPATHLEN]; /* Temporary filename buffer. */
|
|
|
|
char fname2[MAXPATHLEN]; /* Temporary filename buffer. */
|
|
|
|
char msgbufr[1024]; /* Output message buffer. */
|
2016-03-22 22:41:07 +00:00
|
|
|
int error; /* Log write error, returned on close(2). */
|
2016-03-21 20:29:27 +00:00
|
|
|
u_int refcnt; /* Pointer reference count. */
|
|
|
|
u_int proccnt; /* Process count. */
|
2012-06-04 22:54:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cdev *filemon_dev;
|
2016-03-21 20:29:27 +00:00
|
|
|
static void filemon_output(struct filemon *filemon, char *msg, size_t len);
|
|
|
|
|
|
|
|
static __inline struct filemon *
|
|
|
|
filemon_acquire(struct filemon *filemon)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (filemon != NULL)
|
|
|
|
refcount_acquire(&filemon->refcnt);
|
|
|
|
return (filemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-03-22 22:41:03 +00:00
|
|
|
* Release a reference and free on the last one.
|
2016-03-21 20:29:27 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
filemon_release(struct filemon *filemon)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (refcount_release(&filemon->refcnt) == 0)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* There are valid cases of releasing while locked, such as in
|
|
|
|
* filemon_untrack_processes, but none which are done where there
|
|
|
|
* is not at least 1 reference remaining.
|
|
|
|
*/
|
|
|
|
sx_assert(&filemon->lock, SA_UNLOCKED);
|
|
|
|
|
2016-05-27 23:57:43 +00:00
|
|
|
if (filemon->cred != NULL)
|
|
|
|
crfree(filemon->cred);
|
2016-03-21 20:29:27 +00:00
|
|
|
sx_destroy(&filemon->lock);
|
|
|
|
free(filemon, M_FILEMON);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire the proc's p_filemon reference and lock the filemon.
|
|
|
|
* The proc's p_filemon may not match this filemon on return.
|
|
|
|
*/
|
|
|
|
static struct filemon *
|
|
|
|
filemon_proc_get(struct proc *p)
|
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
2016-08-12 16:05:53 +00:00
|
|
|
if (p->p_filemon == NULL)
|
|
|
|
return (NULL);
|
2016-03-21 20:29:27 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
filemon = filemon_acquire(p->p_filemon);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
|
|
|
|
if (filemon == NULL)
|
|
|
|
return (NULL);
|
|
|
|
/*
|
|
|
|
* The p->p_filemon may have changed by now. That case is handled
|
|
|
|
* by the exit and fork hooks and filemon_attach_proc specially.
|
|
|
|
*/
|
|
|
|
sx_xlock(&filemon->lock);
|
|
|
|
return (filemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove and release the filemon on the given process. */
|
|
|
|
static void
|
|
|
|
filemon_proc_drop(struct proc *p)
|
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
|
|
|
KASSERT(p->p_filemon != NULL, ("%s: proc %p NULL p_filemon",
|
|
|
|
__func__, p));
|
|
|
|
sx_assert(&p->p_filemon->lock, SA_XLOCKED);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
filemon = p->p_filemon;
|
|
|
|
p->p_filemon = NULL;
|
|
|
|
--filemon->proccnt;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
/*
|
|
|
|
* This should not be the last reference yet. filemon_release()
|
|
|
|
* cannot be called with filemon locked, which the caller expects
|
|
|
|
* will stay locked.
|
|
|
|
*/
|
|
|
|
KASSERT(filemon->refcnt > 1, ("%s: proc %p dropping filemon %p "
|
|
|
|
"with last reference", __func__, p, filemon));
|
|
|
|
filemon_release(filemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock and release the filemon. */
|
|
|
|
static __inline void
|
|
|
|
filemon_drop(struct filemon *filemon)
|
|
|
|
{
|
|
|
|
|
|
|
|
sx_xunlock(&filemon->lock);
|
|
|
|
filemon_release(filemon);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
#include "filemon_wrapper.c"
|
|
|
|
|
2016-01-27 21:17:43 +00:00
|
|
|
static void
|
2016-05-27 23:57:57 +00:00
|
|
|
filemon_write_header(struct filemon *filemon)
|
2016-01-27 21:17:43 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
getmicrotime(&now);
|
|
|
|
|
|
|
|
len = snprintf(filemon->msgbufr, sizeof(filemon->msgbufr),
|
|
|
|
"# filemon version %d\n# Target pid %d\n# Start %ju.%06ju\nV %d\n",
|
|
|
|
FILEMON_VERSION, curproc->p_pid, (uintmax_t)now.tv_sec,
|
|
|
|
(uintmax_t)now.tv_usec, FILEMON_VERSION);
|
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/*
|
|
|
|
* Invalidate the passed filemon in all processes.
|
|
|
|
*/
|
2012-06-04 22:54:19 +00:00
|
|
|
static void
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_untrack_processes(struct filemon *filemon)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
2016-03-21 20:29:27 +00:00
|
|
|
struct proc *p;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
sx_assert(&filemon->lock, SA_XLOCKED);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/* Avoid allproc loop if there is no need. */
|
|
|
|
if (filemon->proccnt == 0)
|
|
|
|
return;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
/*
|
|
|
|
* Processes in this list won't go away while here since
|
|
|
|
* filemon_event_process_exit() will lock on filemon->lock
|
|
|
|
* which we hold.
|
|
|
|
*/
|
|
|
|
sx_slock(&allproc_lock);
|
|
|
|
FOREACH_PROC_IN_SYSTEM(p) {
|
|
|
|
/*
|
|
|
|
* No PROC_LOCK is needed to compare here since it is
|
|
|
|
* guaranteed to not change since we have its filemon
|
|
|
|
* locked. Everything that changes this p_filemon will
|
|
|
|
* be locked on it.
|
|
|
|
*/
|
|
|
|
if (p->p_filemon == filemon)
|
|
|
|
filemon_proc_drop(p);
|
|
|
|
}
|
|
|
|
sx_sunlock(&allproc_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's possible some references were acquired but will be
|
|
|
|
* dropped shortly as they are restricted from being
|
|
|
|
* inherited. There is at least the reference in cdevpriv remaining.
|
|
|
|
*/
|
|
|
|
KASSERT(filemon->refcnt > 0, ("%s: filemon %p should have "
|
|
|
|
"references still.", __func__, filemon));
|
|
|
|
KASSERT(filemon->proccnt == 0, ("%s: filemon %p should not have "
|
|
|
|
"attached procs still.", __func__, filemon));
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-22 22:41:03 +00:00
|
|
|
/*
|
|
|
|
* Close out the log.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
filemon_close_log(struct filemon *filemon)
|
|
|
|
{
|
|
|
|
struct file *fp;
|
|
|
|
struct timeval now;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
sx_assert(&filemon->lock, SA_XLOCKED);
|
|
|
|
if (filemon->fp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
getmicrotime(&now);
|
|
|
|
|
|
|
|
len = snprintf(filemon->msgbufr,
|
|
|
|
sizeof(filemon->msgbufr),
|
|
|
|
"# Stop %ju.%06ju\n# Bye bye\n",
|
|
|
|
(uintmax_t)now.tv_sec, (uintmax_t)now.tv_usec);
|
|
|
|
|
|
|
|
filemon_output(filemon, filemon->msgbufr, len);
|
|
|
|
fp = filemon->fp;
|
|
|
|
filemon->fp = NULL;
|
|
|
|
|
|
|
|
sx_xunlock(&filemon->lock);
|
|
|
|
fdrop(fp, curthread);
|
|
|
|
sx_xlock(&filemon->lock);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-22 22:41:07 +00:00
|
|
|
/*
|
|
|
|
* The devfs file is being closed. Untrace all processes. It is possible
|
|
|
|
* filemon_close/close(2) was not called.
|
|
|
|
*/
|
2016-03-21 20:29:27 +00:00
|
|
|
static void
|
|
|
|
filemon_dtr(void *data)
|
|
|
|
{
|
|
|
|
struct filemon *filemon = data;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
if (filemon == NULL)
|
|
|
|
return;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
sx_xlock(&filemon->lock);
|
|
|
|
/*
|
2016-03-22 22:41:03 +00:00
|
|
|
* Detach the filemon. It cannot be inherited after this.
|
2016-03-21 20:29:27 +00:00
|
|
|
*/
|
|
|
|
filemon_untrack_processes(filemon);
|
2016-03-22 22:41:03 +00:00
|
|
|
filemon_close_log(filemon);
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon_drop(filemon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach the filemon to the process. */
|
|
|
|
static int
|
|
|
|
filemon_attach_proc(struct filemon *filemon, struct proc *p)
|
|
|
|
{
|
|
|
|
struct filemon *filemon2;
|
|
|
|
|
|
|
|
sx_assert(&filemon->lock, SA_XLOCKED);
|
|
|
|
PROC_LOCK_ASSERT(p, MA_OWNED);
|
|
|
|
KASSERT((p->p_flag & P_WEXIT) == 0,
|
|
|
|
("%s: filemon %p attaching to exiting process %p",
|
|
|
|
__func__, filemon, p));
|
2016-05-27 23:57:43 +00:00
|
|
|
KASSERT((p->p_flag & P_INEXEC) == 0,
|
|
|
|
("%s: filemon %p attaching to execing process %p",
|
|
|
|
__func__, filemon, p));
|
2016-03-21 20:29:27 +00:00
|
|
|
|
|
|
|
if (p->p_filemon == filemon)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* Don't allow truncating other process traces. It is
|
|
|
|
* not really intended to trace procs other than curproc
|
|
|
|
* anyhow.
|
|
|
|
*/
|
|
|
|
if (p->p_filemon != NULL && p != curproc)
|
|
|
|
return (EBUSY);
|
|
|
|
/*
|
|
|
|
* Historic behavior of filemon has been to let a child initiate
|
|
|
|
* tracing on itself and cease existing tracing. Bmake
|
|
|
|
* .META + .MAKE relies on this. It is only relevant for attaching to
|
|
|
|
* curproc.
|
|
|
|
*/
|
|
|
|
while (p->p_filemon != NULL) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
sx_xunlock(&filemon->lock);
|
|
|
|
while ((filemon2 = filemon_proc_get(p)) != NULL) {
|
|
|
|
/* It may have changed. */
|
|
|
|
if (p->p_filemon == filemon2)
|
|
|
|
filemon_proc_drop(p);
|
|
|
|
filemon_drop(filemon2);
|
|
|
|
}
|
|
|
|
sx_xlock(&filemon->lock);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
/*
|
|
|
|
* It may have been attached to, though unlikely.
|
|
|
|
* Try again if needed.
|
|
|
|
*/
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
2016-03-21 20:29:27 +00:00
|
|
|
|
|
|
|
KASSERT(p->p_filemon == NULL,
|
|
|
|
("%s: proc %p didn't detach filemon %p", __func__, p,
|
|
|
|
p->p_filemon));
|
|
|
|
p->p_filemon = filemon_acquire(filemon);
|
|
|
|
++filemon->proccnt;
|
|
|
|
|
|
|
|
return (0);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag __unused,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
struct filemon *filemon;
|
2013-08-02 14:44:11 +00:00
|
|
|
struct proc *p;
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
cap_rights_t rights;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-01-27 21:12:18 +00:00
|
|
|
if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
|
|
|
|
return (error);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-02 00:13:13 +00:00
|
|
|
sx_xlock(&filemon->lock);
|
2016-01-27 21:14:09 +00:00
|
|
|
|
2012-06-04 22:54:19 +00:00
|
|
|
switch (cmd) {
|
|
|
|
/* Set the output file descriptor. */
|
|
|
|
case FILEMON_SET_FD:
|
2016-03-09 19:50:35 +00:00
|
|
|
if (filemon->fp != NULL) {
|
|
|
|
error = EEXIST;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-27 19:11:11 +00:00
|
|
|
|
Change the cap_rights_t type from uint64_t to a structure that we can extend
in the future in a backward compatible (API and ABI) way.
The cap_rights_t represents capability rights. We used to use one bit to
represent one right, but we are running out of spare bits. Currently the new
structure provides place for 114 rights (so 50 more than the previous
cap_rights_t), but it is possible to grow the structure to hold at least 285
rights, although we can make it even larger if 285 rights won't be enough.
The structure definition looks like this:
struct cap_rights {
uint64_t cr_rights[CAP_RIGHTS_VERSION + 2];
};
The initial CAP_RIGHTS_VERSION is 0.
The top two bits in the first element of the cr_rights[] array contain total
number of elements in the array - 2. This means if those two bits are equal to
0, we have 2 array elements.
The top two bits in all remaining array elements should be 0.
The next five bits in all array elements contain array index. Only one bit is
used and bit position in this five-bits range defines array index. This means
there can be at most five array elements in the future.
To define new right the CAPRIGHT() macro must be used. The macro takes two
arguments - an array index and a bit to set, eg.
#define CAP_PDKILL CAPRIGHT(1, 0x0000000000000800ULL)
We still support aliases that combine few rights, but the rights have to belong
to the same array element, eg:
#define CAP_LOOKUP CAPRIGHT(0, 0x0000000000000400ULL)
#define CAP_FCHMOD CAPRIGHT(0, 0x0000000000002000ULL)
#define CAP_FCHMODAT (CAP_FCHMOD | CAP_LOOKUP)
There is new API to manage the new cap_rights_t structure:
cap_rights_t *cap_rights_init(cap_rights_t *rights, ...);
void cap_rights_set(cap_rights_t *rights, ...);
void cap_rights_clear(cap_rights_t *rights, ...);
bool cap_rights_is_set(const cap_rights_t *rights, ...);
bool cap_rights_is_valid(const cap_rights_t *rights);
void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
Capability rights to the cap_rights_init(), cap_rights_set(),
cap_rights_clear() and cap_rights_is_set() functions are provided by
separating them with commas, eg:
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FSTAT);
There is no need to terminate the list of rights, as those functions are
actually macros that take care of the termination, eg:
#define cap_rights_set(rights, ...) \
__cap_rights_set((rights), __VA_ARGS__, 0ULL)
void __cap_rights_set(cap_rights_t *rights, ...);
Thanks to using one bit as an array index we can assert in those functions that
there are no two rights belonging to different array elements provided
together. For example this is illegal and will be detected, because CAP_LOOKUP
belongs to element 0 and CAP_PDKILL to element 1:
cap_rights_init(&rights, CAP_LOOKUP | CAP_PDKILL);
Providing several rights that belongs to the same array's element this way is
correct, but is not advised. It should only be used for aliases definition.
This commit also breaks compatibility with some existing Capsicum system calls,
but I see no other way to do that. This should be fine as Capsicum is still
experimental and this change is not going to 9.x.
Sponsored by: The FreeBSD Foundation
2013-09-05 00:09:56 +00:00
|
|
|
error = fget_write(td, *(int *)data,
|
|
|
|
cap_rights_init(&rights, CAP_PWRITE),
|
|
|
|
&filemon->fp);
|
|
|
|
if (error == 0)
|
2012-06-04 22:54:19 +00:00
|
|
|
/* Write the file header. */
|
2016-05-27 23:57:57 +00:00
|
|
|
filemon_write_header(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the monitored process ID. */
|
|
|
|
case FILEMON_SET_PID:
|
2016-03-21 20:29:27 +00:00
|
|
|
/* Invalidate any existing processes already set. */
|
|
|
|
filemon_untrack_processes(filemon);
|
|
|
|
|
2016-05-27 23:57:43 +00:00
|
|
|
error = pget(*((pid_t *)data),
|
|
|
|
PGET_CANDEBUG | PGET_NOTWEXIT | PGET_NOTINEXEC, &p);
|
2013-08-06 02:14:30 +00:00
|
|
|
if (error == 0) {
|
2016-03-21 20:29:27 +00:00
|
|
|
KASSERT(p->p_filemon != filemon,
|
|
|
|
("%s: proc %p didn't untrack filemon %p",
|
|
|
|
__func__, p, filemon));
|
|
|
|
error = filemon_attach_proc(filemon, p);
|
2013-08-06 02:14:30 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
2012-06-04 22:54:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:13:13 +00:00
|
|
|
sx_xunlock(&filemon->lock);
|
2012-06-04 22:54:19 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
|
2016-05-27 23:57:43 +00:00
|
|
|
struct thread *td)
|
2012-06-04 22:54:19 +00:00
|
|
|
{
|
2016-03-21 20:29:27 +00:00
|
|
|
int error;
|
2012-06-04 22:54:19 +00:00
|
|
|
struct filemon *filemon;
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
filemon = malloc(sizeof(*filemon), M_FILEMON,
|
|
|
|
M_WAITOK | M_ZERO);
|
|
|
|
sx_init(&filemon->lock, "filemon");
|
|
|
|
refcount_init(&filemon->refcnt, 1);
|
2016-05-27 23:57:43 +00:00
|
|
|
filemon->cred = crhold(td->td_ucred);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
error = devfs_set_cdevpriv(filemon, filemon_dtr);
|
|
|
|
if (error != 0)
|
|
|
|
filemon_release(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
return (error);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:41:07 +00:00
|
|
|
/* Called on close of last devfs file handle, before filemon_dtr(). */
|
2012-06-04 22:54:19 +00:00
|
|
|
static int
|
|
|
|
filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
|
|
|
|
struct thread *td __unused)
|
|
|
|
{
|
2016-03-22 22:41:07 +00:00
|
|
|
struct filemon *filemon;
|
|
|
|
int error;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-22 22:41:07 +00:00
|
|
|
if ((error = devfs_get_cdevpriv((void **) &filemon)) != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
sx_xlock(&filemon->lock);
|
|
|
|
filemon_close_log(filemon);
|
|
|
|
error = filemon->error;
|
|
|
|
sx_xunlock(&filemon->lock);
|
|
|
|
/*
|
|
|
|
* Processes are still being traced but won't log anything
|
|
|
|
* now. After this call returns filemon_dtr() is called which
|
|
|
|
* will detach processes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (error);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemon_load(void *dummy __unused)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Install the syscall wrappers. */
|
|
|
|
filemon_wrapper_install();
|
|
|
|
|
|
|
|
filemon_dev = make_dev(&filemon_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666,
|
|
|
|
"filemon");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_unload(void)
|
|
|
|
{
|
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
destroy_dev(filemon_dev);
|
|
|
|
filemon_wrapper_deinstall();
|
2012-06-04 22:54:19 +00:00
|
|
|
|
2016-03-21 20:29:27 +00:00
|
|
|
return (0);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_modevent(module_t mod __unused, int type, void *data)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
filemon_load(data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
error = filemon_unload();
|
|
|
|
break;
|
|
|
|
|
2016-03-07 21:39:29 +00:00
|
|
|
case MOD_QUIESCE:
|
|
|
|
/*
|
|
|
|
* The wrapper implementation is unsafe for reliable unload.
|
|
|
|
* Require forcing an unload.
|
|
|
|
*/
|
|
|
|
error = EBUSY;
|
2016-03-07 21:45:24 +00:00
|
|
|
break;
|
2016-03-07 21:39:29 +00:00
|
|
|
|
2012-06-04 22:54:19 +00:00
|
|
|
case MOD_SHUTDOWN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEV_MODULE(filemon, filemon_modevent, NULL);
|
|
|
|
MODULE_VERSION(filemon, 1);
|