2012-06-04 22:54:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011, David E. O'Brien.
|
|
|
|
* Copyright (c) 2009-2011, Juniper Networks, Inc.
|
2015-08-26 03:44:48 +00:00
|
|
|
* Copyright (c) 2015, 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>
|
|
|
|
#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>
|
|
|
|
#include <sys/queue.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>
|
|
|
|
|
|
|
|
#if __FreeBSD_version >= 900041
|
2014-03-16 10:55:57 +00:00
|
|
|
#include <sys/capsicum.h>
|
2012-06-04 22:54:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "filemon.h"
|
|
|
|
|
|
|
|
#if defined(COMPAT_IA32) || defined(COMPAT_FREEBSD32) || defined(COMPAT_ARCH32)
|
|
|
|
#include <compat/freebsd32/freebsd32_syscall.h>
|
|
|
|
#include <compat/freebsd32/freebsd32_proto.h>
|
|
|
|
|
|
|
|
extern struct sysentvec ia32_freebsd_sysvec;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern struct sysentvec elf32_freebsd_sysvec;
|
|
|
|
extern struct sysentvec elf64_freebsd_sysvec;
|
|
|
|
|
|
|
|
static d_close_t filemon_close;
|
|
|
|
static d_ioctl_t filemon_ioctl;
|
|
|
|
static d_open_t filemon_open;
|
|
|
|
static int filemon_unload(void);
|
|
|
|
static void filemon_load(void *);
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
struct filemon {
|
|
|
|
TAILQ_ENTRY(filemon) link; /* Link into the in-use list. */
|
2015-08-26 03:44:48 +00:00
|
|
|
struct sx lock; /* Lock mutex for this filemon. */
|
2012-06-04 22:54:19 +00:00
|
|
|
struct file *fp; /* Output file pointer. */
|
|
|
|
pid_t pid; /* The process ID being monitored. */
|
|
|
|
char fname1[MAXPATHLEN]; /* Temporary filename buffer. */
|
|
|
|
char fname2[MAXPATHLEN]; /* Temporary filename buffer. */
|
|
|
|
char msgbufr[1024]; /* Output message buffer. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static TAILQ_HEAD(, filemon) filemons_inuse = TAILQ_HEAD_INITIALIZER(filemons_inuse);
|
|
|
|
static TAILQ_HEAD(, filemon) filemons_free = TAILQ_HEAD_INITIALIZER(filemons_free);
|
2015-08-26 03:44:48 +00:00
|
|
|
static struct sx access_lock;
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
static struct cdev *filemon_dev;
|
|
|
|
|
|
|
|
#include "filemon_lock.c"
|
|
|
|
#include "filemon_wrapper.c"
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemon_dtr(void *data)
|
|
|
|
{
|
|
|
|
struct filemon *filemon = data;
|
|
|
|
|
|
|
|
if (filemon != NULL) {
|
|
|
|
struct file *fp = filemon->fp;
|
|
|
|
|
|
|
|
/* Get exclusive write access. */
|
|
|
|
filemon_lock_write();
|
|
|
|
|
|
|
|
/* Remove from the in-use list. */
|
|
|
|
TAILQ_REMOVE(&filemons_inuse, filemon, link);
|
|
|
|
|
|
|
|
filemon->fp = NULL;
|
|
|
|
filemon->pid = -1;
|
|
|
|
|
|
|
|
/* Add to the free list. */
|
|
|
|
TAILQ_INSERT_TAIL(&filemons_free, filemon, link);
|
|
|
|
|
|
|
|
/* Give up write access. */
|
|
|
|
filemon_unlock_write();
|
|
|
|
|
|
|
|
if (fp != NULL)
|
|
|
|
fdrop(fp, curthread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
#if __FreeBSD_version >= 900041
|
|
|
|
cap_rights_t rights;
|
|
|
|
#endif
|
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-01-27 21:14:09 +00:00
|
|
|
filemon_filemon_lock(filemon);
|
|
|
|
|
2012-06-04 22:54:19 +00:00
|
|
|
switch (cmd) {
|
|
|
|
/* Set the output file descriptor. */
|
|
|
|
case FILEMON_SET_FD:
|
2016-01-27 19:11:11 +00:00
|
|
|
if (filemon->fp != NULL)
|
|
|
|
fdrop(filemon->fp, td);
|
|
|
|
|
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,
|
|
|
|
#if __FreeBSD_version >= 900041
|
|
|
|
cap_rights_init(&rights, CAP_PWRITE),
|
|
|
|
#endif
|
|
|
|
&filemon->fp);
|
|
|
|
if (error == 0)
|
2012-06-04 22:54:19 +00:00
|
|
|
/* Write the file header. */
|
|
|
|
filemon_comment(filemon);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the monitored process ID. */
|
|
|
|
case FILEMON_SET_PID:
|
2013-08-06 02:14:30 +00:00
|
|
|
error = pget(*((pid_t *)data), PGET_CANDEBUG | PGET_NOTWEXIT,
|
|
|
|
&p);
|
|
|
|
if (error == 0) {
|
2013-08-02 14:44:11 +00:00
|
|
|
filemon->pid = p->p_pid;
|
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-01-27 21:14:09 +00:00
|
|
|
filemon_filemon_unlock(filemon);
|
2012-06-04 22:54:19 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_open(struct cdev *dev, int oflags __unused, int devtype __unused,
|
|
|
|
struct thread *td __unused)
|
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
|
|
|
|
|
|
|
/* Get exclusive write access. */
|
|
|
|
filemon_lock_write();
|
|
|
|
|
|
|
|
if ((filemon = TAILQ_FIRST(&filemons_free)) != NULL)
|
|
|
|
TAILQ_REMOVE(&filemons_free, filemon, link);
|
|
|
|
|
|
|
|
/* Give up write access. */
|
|
|
|
filemon_unlock_write();
|
|
|
|
|
|
|
|
if (filemon == NULL) {
|
|
|
|
filemon = malloc(sizeof(struct filemon), M_FILEMON,
|
|
|
|
M_WAITOK | M_ZERO);
|
2015-08-26 03:44:48 +00:00
|
|
|
sx_init(&filemon->lock, "filemon");
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filemon->pid = curproc->p_pid;
|
|
|
|
|
|
|
|
devfs_set_cdevpriv(filemon, filemon_dtr);
|
|
|
|
|
|
|
|
/* Get exclusive write access. */
|
|
|
|
filemon_lock_write();
|
|
|
|
|
|
|
|
/* Add to the in-use list. */
|
|
|
|
TAILQ_INSERT_TAIL(&filemons_inuse, filemon, link);
|
|
|
|
|
|
|
|
/* Give up write access. */
|
|
|
|
filemon_unlock_write();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filemon_close(struct cdev *dev __unused, int flag __unused, int fmt __unused,
|
|
|
|
struct thread *td __unused)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemon_load(void *dummy __unused)
|
|
|
|
{
|
2015-08-26 03:44:48 +00:00
|
|
|
sx_init(&access_lock, "filemons_inuse");
|
2012-06-04 22:54:19 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
struct filemon *filemon;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* Get exclusive write access. */
|
|
|
|
filemon_lock_write();
|
|
|
|
|
|
|
|
if (TAILQ_FIRST(&filemons_inuse) != NULL)
|
|
|
|
error = EBUSY;
|
|
|
|
else {
|
|
|
|
destroy_dev(filemon_dev);
|
|
|
|
|
|
|
|
/* Deinstall the syscall wrappers. */
|
|
|
|
filemon_wrapper_deinstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Give up write access. */
|
|
|
|
filemon_unlock_write();
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
/* free() filemon structs free list. */
|
|
|
|
filemon_lock_write();
|
|
|
|
while ((filemon = TAILQ_FIRST(&filemons_free)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&filemons_free, filemon, link);
|
2015-08-26 03:44:48 +00:00
|
|
|
sx_destroy(&filemon->lock);
|
2012-06-04 22:54:19 +00:00
|
|
|
free(filemon, M_FILEMON);
|
|
|
|
}
|
|
|
|
filemon_unlock_write();
|
|
|
|
|
2015-08-26 03:44:48 +00:00
|
|
|
sx_destroy(&access_lock);
|
2012-06-04 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case MOD_SHUTDOWN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEV_MODULE(filemon, filemon_modevent, NULL);
|
|
|
|
MODULE_VERSION(filemon, 1);
|