提交 e9fd702a 编写于 作者: E Eric Paris

audit: convert audit watches to use fsnotify instead of inotify

Audit currently uses inotify to pin inodes in core and to detect when
watched inodes are deleted or unmounted.  This patch uses fsnotify instead
of inotify.
Signed-off-by: NEric Paris <eparis@redhat.com>
上级 ae7b8f41
...@@ -58,9 +58,12 @@ ...@@ -58,9 +58,12 @@
FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\ FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\
FS_DELETE) FS_DELETE)
#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
/* listeners that hard code group numbers near the top */ /* listeners that hard code group numbers near the top */
#define DNOTIFY_GROUP_NUM UINT_MAX #define DNOTIFY_GROUP_NUM UINT_MAX
#define INOTIFY_GROUP_NUM (DNOTIFY_GROUP_NUM-1) #define AUDIT_WATCH_GROUP_NUM (DNOTIFY_GROUP_NUM-1)
#define INOTIFY_GROUP_NUM (AUDIT_WATCH_GROUP_NUM-1)
struct fsnotify_group; struct fsnotify_group;
struct fsnotify_event; struct fsnotify_event;
......
...@@ -24,18 +24,18 @@ ...@@ -24,18 +24,18 @@
#include <linux/kthread.h> #include <linux/kthread.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/fsnotify_backend.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/inotify.h>
#include <linux/security.h> #include <linux/security.h>
#include "audit.h" #include "audit.h"
/* /*
* Reference counting: * Reference counting:
* *
* audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
* event. Each audit_watch holds a reference to its associated parent. * event. Each audit_watch holds a reference to its associated parent.
* *
* audit_watch: if added to lists, lifetime is from audit_init_watch() to * audit_watch: if added to lists, lifetime is from audit_init_watch() to
...@@ -57,26 +57,27 @@ struct audit_watch { ...@@ -57,26 +57,27 @@ struct audit_watch {
struct audit_parent { struct audit_parent {
struct list_head ilist; /* tmp list used to free parents */ struct list_head ilist; /* tmp list used to free parents */
struct list_head watches; /* anchor for audit_watch->wlist */ struct list_head watches; /* anchor for audit_watch->wlist */
struct inotify_watch wdata; /* inotify watch data */ struct fsnotify_mark_entry mark; /* fsnotify mark on the inode */
unsigned flags; /* status flags */ unsigned flags; /* status flags */
}; };
/* Inotify handle. */ /* fsnotify handle. */
struct inotify_handle *audit_ih; struct fsnotify_group *audit_watch_group;
/* /*
* audit_parent status flags: * audit_parent status flags:
* *
* AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
* a filesystem event to ensure we're adding audit watches to a valid parent. * a filesystem event to ensure we're adding audit watches to a valid parent.
* Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot * Technically not needed for FS_DELETE_SELF or FS_UNMOUNT events, as we cannot
* receive them while we have nameidata, but must be used for IN_MOVE_SELF which * receive them while we have nameidata, but must be used for FS_MOVE_SELF which
* we can receive while holding nameidata. * we can receive while holding nameidata.
*/ */
#define AUDIT_PARENT_INVALID 0x001 #define AUDIT_PARENT_INVALID 0x001
/* Inotify events we care about. */ /* fsnotify events we care about. */
#define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
FS_MOVE_SELF | FS_EVENT_ON_CHILD)
static void audit_free_parent(struct audit_parent *parent) static void audit_free_parent(struct audit_parent *parent)
{ {
...@@ -84,14 +85,45 @@ static void audit_free_parent(struct audit_parent *parent) ...@@ -84,14 +85,45 @@ static void audit_free_parent(struct audit_parent *parent)
kfree(parent); kfree(parent);
} }
static void audit_destroy_watch(struct inotify_watch *i_watch) static void audit_watch_free_mark(struct fsnotify_mark_entry *entry)
{ {
struct audit_parent *parent; struct audit_parent *parent;
parent = container_of(i_watch, struct audit_parent, wdata); parent = container_of(entry, struct audit_parent, mark);
audit_free_parent(parent); audit_free_parent(parent);
} }
static void audit_get_parent(struct audit_parent *parent)
{
if (likely(parent))
fsnotify_get_mark(&parent->mark);
}
static void audit_put_parent(struct audit_parent *parent)
{
if (likely(parent))
fsnotify_put_mark(&parent->mark);
}
/*
* Find and return the audit_parent on the given inode. If found a reference
* is taken on this parent.
*/
static inline struct audit_parent *audit_find_parent(struct inode *inode)
{
struct audit_parent *parent = NULL;
struct fsnotify_mark_entry *entry;
spin_lock(&inode->i_lock);
entry = fsnotify_find_mark_entry(audit_watch_group, inode);
spin_unlock(&inode->i_lock);
if (entry)
parent = container_of(entry, struct audit_parent, mark);
return parent;
}
void audit_get_watch(struct audit_watch *watch) void audit_get_watch(struct audit_watch *watch)
{ {
atomic_inc(&watch->count); atomic_inc(&watch->count);
...@@ -110,7 +142,7 @@ void audit_put_watch(struct audit_watch *watch) ...@@ -110,7 +142,7 @@ void audit_put_watch(struct audit_watch *watch)
void audit_remove_watch(struct audit_watch *watch) void audit_remove_watch(struct audit_watch *watch)
{ {
list_del(&watch->wlist); list_del(&watch->wlist);
put_inotify_watch(&watch->parent->wdata); audit_put_parent(watch->parent);
watch->parent = NULL; watch->parent = NULL;
audit_put_watch(watch); /* match initial get */ audit_put_watch(watch); /* match initial get */
} }
...@@ -130,8 +162,9 @@ int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev) ...@@ -130,8 +162,9 @@ int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
/* Initialize a parent watch entry. */ /* Initialize a parent watch entry. */
static struct audit_parent *audit_init_parent(struct nameidata *ndp) static struct audit_parent *audit_init_parent(struct nameidata *ndp)
{ {
struct inode *inode = ndp->path.dentry->d_inode;
struct audit_parent *parent; struct audit_parent *parent;
s32 wd; int ret;
parent = kzalloc(sizeof(*parent), GFP_KERNEL); parent = kzalloc(sizeof(*parent), GFP_KERNEL);
if (unlikely(!parent)) if (unlikely(!parent))
...@@ -140,14 +173,14 @@ static struct audit_parent *audit_init_parent(struct nameidata *ndp) ...@@ -140,14 +173,14 @@ static struct audit_parent *audit_init_parent(struct nameidata *ndp)
INIT_LIST_HEAD(&parent->watches); INIT_LIST_HEAD(&parent->watches);
parent->flags = 0; parent->flags = 0;
inotify_init_watch(&parent->wdata); fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
/* grab a ref so inotify watch hangs around until we take audit_filter_mutex */ parent->mark.mask = AUDIT_FS_WATCH;
get_inotify_watch(&parent->wdata); /* grab a ref so fsnotify mark hangs around until we take audit_filter_mutex */
wd = inotify_add_watch(audit_ih, &parent->wdata, audit_get_parent(parent);
ndp->path.dentry->d_inode, AUDIT_IN_WATCH); ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode);
if (wd < 0) { if (ret < 0) {
audit_free_parent(parent); audit_free_parent(parent);
return ERR_PTR(wd); return ERR_PTR(ret);
} }
return parent; return parent;
...@@ -176,7 +209,7 @@ int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op) ...@@ -176,7 +209,7 @@ int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
{ {
struct audit_watch *watch; struct audit_watch *watch;
if (!audit_ih) if (!audit_watch_group)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (path[0] != '/' || path[len-1] == '/' || if (path[0] != '/' || path[len-1] == '/' ||
...@@ -214,7 +247,7 @@ static struct audit_watch *audit_dupe_watch(struct audit_watch *old) ...@@ -214,7 +247,7 @@ static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
new->dev = old->dev; new->dev = old->dev;
new->ino = old->ino; new->ino = old->ino;
get_inotify_watch(&old->parent->wdata); audit_get_parent(old->parent);
new->parent = old->parent; new->parent = old->parent;
out: out:
...@@ -335,19 +368,21 @@ static void audit_remove_parent_watches(struct audit_parent *parent) ...@@ -335,19 +368,21 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
audit_remove_watch(w); audit_remove_watch(w);
} }
mutex_unlock(&audit_filter_mutex); mutex_unlock(&audit_filter_mutex);
fsnotify_destroy_mark_by_entry(&parent->mark);
} }
/* Unregister inotify watches for parents on in_list. /* Unregister inotify watches for parents on in_list.
* Generates an IN_IGNORED event. */ * Generates an FS_IGNORED event. */
void audit_watch_inotify_unregister(struct list_head *in_list) void audit_watch_inotify_unregister(struct list_head *in_list)
{ {
struct audit_parent *p, *n; struct audit_parent *p, *n;
list_for_each_entry_safe(p, n, in_list, ilist) { list_for_each_entry_safe(p, n, in_list, ilist) {
list_del(&p->ilist); list_del(&p->ilist);
inotify_rm_watch(audit_ih, &p->wdata); fsnotify_destroy_mark_by_entry(&p->mark);
/* the unpin matching the pin in audit_remove_watch_rule() */ /* matches the get in audit_remove_watch_rule() */
unpin_inotify_watch(&p->wdata); audit_put_parent(p);
} }
} }
...@@ -399,7 +434,7 @@ static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw) ...@@ -399,7 +434,7 @@ static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
} }
} }
/* Associate the given rule with an existing parent inotify_watch. /* Associate the given rule with an existing parent.
* Caller must hold audit_filter_mutex. */ * Caller must hold audit_filter_mutex. */
static void audit_add_to_parent(struct audit_krule *krule, static void audit_add_to_parent(struct audit_krule *krule,
struct audit_parent *parent) struct audit_parent *parent)
...@@ -407,6 +442,8 @@ static void audit_add_to_parent(struct audit_krule *krule, ...@@ -407,6 +442,8 @@ static void audit_add_to_parent(struct audit_krule *krule,
struct audit_watch *w, *watch = krule->watch; struct audit_watch *w, *watch = krule->watch;
int watch_found = 0; int watch_found = 0;
BUG_ON(!mutex_is_locked(&audit_filter_mutex));
list_for_each_entry(w, &parent->watches, wlist) { list_for_each_entry(w, &parent->watches, wlist) {
if (strcmp(watch->path, w->path)) if (strcmp(watch->path, w->path))
continue; continue;
...@@ -423,7 +460,7 @@ static void audit_add_to_parent(struct audit_krule *krule, ...@@ -423,7 +460,7 @@ static void audit_add_to_parent(struct audit_krule *krule,
} }
if (!watch_found) { if (!watch_found) {
get_inotify_watch(&parent->wdata); audit_get_parent(parent);
watch->parent = parent; watch->parent = parent;
list_add(&watch->wlist, &parent->watches); list_add(&watch->wlist, &parent->watches);
...@@ -436,7 +473,6 @@ static void audit_add_to_parent(struct audit_krule *krule, ...@@ -436,7 +473,6 @@ static void audit_add_to_parent(struct audit_krule *krule,
int audit_add_watch(struct audit_krule *krule, struct list_head **list) int audit_add_watch(struct audit_krule *krule, struct list_head **list)
{ {
struct audit_watch *watch = krule->watch; struct audit_watch *watch = krule->watch;
struct inotify_watch *i_watch;
struct audit_parent *parent; struct audit_parent *parent;
struct nameidata *ndp = NULL, *ndw = NULL; struct nameidata *ndp = NULL, *ndw = NULL;
int h, ret = 0; int h, ret = 0;
...@@ -462,8 +498,8 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list) ...@@ -462,8 +498,8 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
* inotify watch is found, inotify_find_watch() grabs a reference before * inotify watch is found, inotify_find_watch() grabs a reference before
* returning. * returning.
*/ */
if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode, parent = audit_find_parent(ndp->path.dentry->d_inode);
&i_watch) < 0) { if (!parent) {
parent = audit_init_parent(ndp); parent = audit_init_parent(ndp);
if (IS_ERR(parent)) { if (IS_ERR(parent)) {
/* caller expects mutex locked */ /* caller expects mutex locked */
...@@ -471,8 +507,7 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list) ...@@ -471,8 +507,7 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
ret = PTR_ERR(parent); ret = PTR_ERR(parent);
goto error; goto error;
} }
} else }
parent = container_of(i_watch, struct audit_parent, wdata);
mutex_lock(&audit_filter_mutex); mutex_lock(&audit_filter_mutex);
...@@ -482,8 +517,8 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list) ...@@ -482,8 +517,8 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
else else
audit_add_to_parent(krule, parent); audit_add_to_parent(krule, parent);
/* match get in audit_init_parent or inotify_find_watch */ /* match get in audit_find_parent or audit_init_parent */
put_inotify_watch(&parent->wdata); audit_put_parent(parent);
h = audit_hash_ino((u32)watch->ino); h = audit_hash_ino((u32)watch->ino);
*list = &audit_inode_hash[h]; *list = &audit_inode_hash[h];
...@@ -504,52 +539,105 @@ void audit_remove_watch_rule(struct audit_krule *krule, struct list_head *list) ...@@ -504,52 +539,105 @@ void audit_remove_watch_rule(struct audit_krule *krule, struct list_head *list)
audit_remove_watch(watch); audit_remove_watch(watch);
if (list_empty(&parent->watches)) { if (list_empty(&parent->watches)) {
/* Put parent on the inotify un-registration /* Put parent on the un-registration list.
* list. Grab a reference before releasing * Grab a reference before releasing
* audit_filter_mutex, to be released in * audit_filter_mutex, to be released in
* audit_inotify_unregister(). * audit_watch_inotify_unregister().
* If filesystem is going away, just leave * If filesystem is going away, just leave
* the sucker alone, eviction will take * the sucker alone, eviction will take
* care of it. */ * care of it. */
if (pin_inotify_watch(&parent->wdata)) audit_get_parent(parent);
list_add(&parent->ilist, list); list_add(&parent->ilist, list);
} }
} }
} }
/* Update watch data in audit rules based on inotify events. */ static bool audit_watch_should_send_event(struct fsnotify_group *group, struct inode *inode, __u32 mask)
static void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
u32 cookie, const char *dname, struct inode *inode)
{ {
struct fsnotify_mark_entry *entry;
bool send;
spin_lock(&inode->i_lock);
entry = fsnotify_find_mark_entry(group, inode);
spin_unlock(&inode->i_lock);
if (!entry)
return false;
mask = (mask & ~FS_EVENT_ON_CHILD);
send = (entry->mask & mask);
/* find took a reference */
fsnotify_put_mark(entry);
return send;
}
/* Update watch data in audit rules based on fsnotify events. */
static int audit_watch_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
{
struct inode *inode;
__u32 mask = event->mask;
const char *dname = event->file_name;
struct audit_parent *parent; struct audit_parent *parent;
parent = container_of(i_watch, struct audit_parent, wdata); BUG_ON(group != audit_watch_group);
parent = audit_find_parent(event->to_tell);
if (unlikely(!parent))
return 0;
switch (event->data_type) {
case (FSNOTIFY_EVENT_PATH):
inode = event->path.dentry->d_inode;
break;
case (FSNOTIFY_EVENT_INODE):
inode = event->inode;
break;
default:
BUG();
inode = NULL;
break;
};
if (mask & (IN_CREATE|IN_MOVED_TO) && inode) if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0); audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
else if (mask & (IN_DELETE|IN_MOVED_FROM)) else if (mask & (FS_DELETE|FS_MOVED_FROM))
audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1); audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
/* inotify automatically removes the watch and sends IN_IGNORED */ else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
audit_remove_parent_watches(parent); audit_remove_parent_watches(parent);
/* inotify does not remove the watch, so remove it manually */ /* moved put_inotify_watch to freeing mark */
else if(mask & IN_MOVE_SELF) {
audit_remove_parent_watches(parent); /* matched the ref taken by audit_find_parent */
inotify_remove_watch_locked(audit_ih, i_watch); audit_put_parent(parent);
} else if (mask & IN_IGNORED)
put_inotify_watch(i_watch); return 0;
}
static void audit_watch_freeing_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *group)
{
struct audit_parent *parent;
parent = container_of(entry, struct audit_parent, mark);
/* taken from audit_handle_ievent & FS_IGNORED please figure out what I match... */
audit_put_parent(parent);
} }
static const struct inotify_operations audit_inotify_ops = { static const struct fsnotify_ops audit_watch_fsnotify_ops = {
.handle_event = audit_handle_ievent, .should_send_event = audit_watch_should_send_event,
.destroy_watch = audit_destroy_watch, .handle_event = audit_watch_handle_event,
.free_group_priv = NULL,
.freeing_mark = audit_watch_freeing_mark,
.free_event_priv = NULL,
}; };
static int __init audit_watch_init(void) static int __init audit_watch_init(void)
{ {
audit_ih = inotify_init(&audit_inotify_ops); audit_watch_group = fsnotify_obtain_group(AUDIT_WATCH_GROUP_NUM, AUDIT_FS_WATCH,
if (IS_ERR(audit_ih)) &audit_watch_fsnotify_ops);
audit_panic("cannot initialize inotify handle"); if (IS_ERR(audit_watch_group)) {
audit_watch_group = NULL;
audit_panic("cannot create audit fsnotify group");
}
return 0; return 0;
} }
subsys_initcall(audit_watch_init); subsys_initcall(audit_watch_init);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册