123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- #include <linux/file.h>
- #include <linux/kernel.h>
- #include <linux/audit.h>
- #include <linux/kthread.h>
- #include <linux/mutex.h>
- #include <linux/fs.h>
- #include <linux/fsnotify_backend.h>
- #include <linux/namei.h>
- #include <linux/netlink.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/security.h>
- #include "audit.h"
- struct audit_watch {
- atomic_t count;
- dev_t dev;
- char *path;
- unsigned long ino;
- struct audit_parent *parent;
- struct list_head wlist;
- struct list_head rules;
- };
- struct audit_parent {
- struct list_head watches;
- struct fsnotify_mark mark;
- };
- static struct fsnotify_group *audit_watch_group;
- #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)
- {
- WARN_ON(!list_empty(&parent->watches));
- kfree(parent);
- }
- static void audit_watch_free_mark(struct fsnotify_mark *entry)
- {
- struct audit_parent *parent;
- parent = container_of(entry, struct audit_parent, mark);
- 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);
- }
- static inline struct audit_parent *audit_find_parent(struct inode *inode)
- {
- struct audit_parent *parent = NULL;
- struct fsnotify_mark *entry;
- entry = fsnotify_find_inode_mark(audit_watch_group, inode);
- if (entry)
- parent = container_of(entry, struct audit_parent, mark);
- return parent;
- }
- void audit_get_watch(struct audit_watch *watch)
- {
- atomic_inc(&watch->count);
- }
- void audit_put_watch(struct audit_watch *watch)
- {
- if (atomic_dec_and_test(&watch->count)) {
- WARN_ON(watch->parent);
- WARN_ON(!list_empty(&watch->rules));
- kfree(watch->path);
- kfree(watch);
- }
- }
- static void audit_remove_watch(struct audit_watch *watch)
- {
- list_del(&watch->wlist);
- audit_put_parent(watch->parent);
- watch->parent = NULL;
- audit_put_watch(watch);
- }
- char *audit_watch_path(struct audit_watch *watch)
- {
- return watch->path;
- }
- int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
- {
- return (watch->ino != AUDIT_INO_UNSET) &&
- (watch->ino == ino) &&
- (watch->dev == dev);
- }
- static struct audit_parent *audit_init_parent(struct path *path)
- {
- struct inode *inode = d_backing_inode(path->dentry);
- struct audit_parent *parent;
- int ret;
- parent = kzalloc(sizeof(*parent), GFP_KERNEL);
- if (unlikely(!parent))
- return ERR_PTR(-ENOMEM);
- INIT_LIST_HEAD(&parent->watches);
- fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
- parent->mark.mask = AUDIT_FS_WATCH;
- ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
- if (ret < 0) {
- audit_free_parent(parent);
- return ERR_PTR(ret);
- }
- return parent;
- }
- static struct audit_watch *audit_init_watch(char *path)
- {
- struct audit_watch *watch;
- watch = kzalloc(sizeof(*watch), GFP_KERNEL);
- if (unlikely(!watch))
- return ERR_PTR(-ENOMEM);
- INIT_LIST_HEAD(&watch->rules);
- atomic_set(&watch->count, 1);
- watch->path = path;
- watch->dev = AUDIT_DEV_UNSET;
- watch->ino = AUDIT_INO_UNSET;
- return watch;
- }
- int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
- {
- struct audit_watch *watch;
- if (!audit_watch_group)
- return -EOPNOTSUPP;
- if (path[0] != '/' || path[len-1] == '/' ||
- krule->listnr != AUDIT_FILTER_EXIT ||
- op != Audit_equal ||
- krule->inode_f || krule->watch || krule->tree)
- return -EINVAL;
- watch = audit_init_watch(path);
- if (IS_ERR(watch))
- return PTR_ERR(watch);
- krule->watch = watch;
- return 0;
- }
- static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
- {
- char *path;
- struct audit_watch *new;
- path = kstrdup(old->path, GFP_KERNEL);
- if (unlikely(!path))
- return ERR_PTR(-ENOMEM);
- new = audit_init_watch(path);
- if (IS_ERR(new)) {
- kfree(path);
- goto out;
- }
- new->dev = old->dev;
- new->ino = old->ino;
- audit_get_parent(old->parent);
- new->parent = old->parent;
- out:
- return new;
- }
- static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
- {
- if (audit_enabled) {
- struct audit_buffer *ab;
- ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
- if (unlikely(!ab))
- return;
- audit_log_format(ab, "auid=%u ses=%u op=",
- from_kuid(&init_user_ns, audit_get_loginuid(current)),
- audit_get_sessionid(current));
- audit_log_string(ab, op);
- audit_log_format(ab, " path=");
- audit_log_untrustedstring(ab, w->path);
- audit_log_key(ab, r->filterkey);
- audit_log_format(ab, " list=%d res=1", r->listnr);
- audit_log_end(ab);
- }
- }
- static void audit_update_watch(struct audit_parent *parent,
- const char *dname, dev_t dev,
- unsigned long ino, unsigned invalidating)
- {
- struct audit_watch *owatch, *nwatch, *nextw;
- struct audit_krule *r, *nextr;
- struct audit_entry *oentry, *nentry;
- mutex_lock(&audit_filter_mutex);
-
- list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
- if (audit_compare_dname_path(dname, owatch->path,
- AUDIT_NAME_FULL))
- continue;
-
- if (invalidating && !audit_dummy_context())
- audit_filter_inodes(current, current->audit_context);
-
- nwatch = audit_dupe_watch(owatch);
- if (IS_ERR(nwatch)) {
- mutex_unlock(&audit_filter_mutex);
- audit_panic("error updating watch, skipping");
- return;
- }
- nwatch->dev = dev;
- nwatch->ino = ino;
- list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
- oentry = container_of(r, struct audit_entry, rule);
- list_del(&oentry->rule.rlist);
- list_del_rcu(&oentry->list);
- nentry = audit_dupe_rule(&oentry->rule);
- if (IS_ERR(nentry)) {
- list_del(&oentry->rule.list);
- audit_panic("error updating watch, removing");
- } else {
- int h = audit_hash_ino((u32)ino);
-
- audit_put_watch(nentry->rule.watch);
- audit_get_watch(nwatch);
- nentry->rule.watch = nwatch;
- list_add(&nentry->rule.rlist, &nwatch->rules);
- list_add_rcu(&nentry->list, &audit_inode_hash[h]);
- list_replace(&oentry->rule.list,
- &nentry->rule.list);
- }
- if (oentry->rule.exe)
- audit_remove_mark(oentry->rule.exe);
- audit_watch_log_rule_change(r, owatch, "updated_rules");
- call_rcu(&oentry->rcu, audit_free_rule_rcu);
- }
- audit_remove_watch(owatch);
- goto add_watch_to_parent;
- }
- mutex_unlock(&audit_filter_mutex);
- return;
- add_watch_to_parent:
- list_add(&nwatch->wlist, &parent->watches);
- mutex_unlock(&audit_filter_mutex);
- return;
- }
- static void audit_remove_parent_watches(struct audit_parent *parent)
- {
- struct audit_watch *w, *nextw;
- struct audit_krule *r, *nextr;
- struct audit_entry *e;
- mutex_lock(&audit_filter_mutex);
- list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
- list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
- e = container_of(r, struct audit_entry, rule);
- audit_watch_log_rule_change(r, w, "remove_rule");
- if (e->rule.exe)
- audit_remove_mark(e->rule.exe);
- list_del(&r->rlist);
- list_del(&r->list);
- list_del_rcu(&e->list);
- call_rcu(&e->rcu, audit_free_rule_rcu);
- }
- audit_remove_watch(w);
- }
- mutex_unlock(&audit_filter_mutex);
- fsnotify_destroy_mark(&parent->mark, audit_watch_group);
- }
- static int audit_get_nd(struct audit_watch *watch, struct path *parent)
- {
- struct dentry *d = kern_path_locked(watch->path, parent);
- if (IS_ERR(d))
- return PTR_ERR(d);
- inode_unlock(d_backing_inode(parent->dentry));
- if (d_is_positive(d)) {
-
- watch->dev = d->d_sb->s_dev;
- watch->ino = d_backing_inode(d)->i_ino;
- }
- dput(d);
- return 0;
- }
- static void audit_add_to_parent(struct audit_krule *krule,
- struct audit_parent *parent)
- {
- struct audit_watch *w, *watch = krule->watch;
- int watch_found = 0;
- BUG_ON(!mutex_is_locked(&audit_filter_mutex));
- list_for_each_entry(w, &parent->watches, wlist) {
- if (strcmp(watch->path, w->path))
- continue;
- watch_found = 1;
-
- audit_put_watch(watch);
- audit_get_watch(w);
- krule->watch = watch = w;
- audit_put_parent(parent);
- break;
- }
- if (!watch_found) {
- watch->parent = parent;
- audit_get_watch(watch);
- list_add(&watch->wlist, &parent->watches);
- }
- list_add(&krule->rlist, &watch->rules);
- }
- int audit_add_watch(struct audit_krule *krule, struct list_head **list)
- {
- struct audit_watch *watch = krule->watch;
- struct audit_parent *parent;
- struct path parent_path;
- int h, ret = 0;
-
- audit_get_watch(watch);
- mutex_unlock(&audit_filter_mutex);
-
- ret = audit_get_nd(watch, &parent_path);
-
- mutex_lock(&audit_filter_mutex);
- if (ret) {
- audit_put_watch(watch);
- return ret;
- }
-
- parent = audit_find_parent(d_backing_inode(parent_path.dentry));
- if (!parent) {
- parent = audit_init_parent(&parent_path);
- if (IS_ERR(parent)) {
- ret = PTR_ERR(parent);
- goto error;
- }
- }
- audit_add_to_parent(krule, parent);
- h = audit_hash_ino((u32)watch->ino);
- *list = &audit_inode_hash[h];
- error:
- path_put(&parent_path);
- audit_put_watch(watch);
- return ret;
- }
- void audit_remove_watch_rule(struct audit_krule *krule)
- {
- struct audit_watch *watch = krule->watch;
- struct audit_parent *parent = watch->parent;
- list_del(&krule->rlist);
- if (list_empty(&watch->rules)) {
-
- audit_get_parent(parent);
- audit_remove_watch(watch);
- if (list_empty(&parent->watches))
- fsnotify_destroy_mark(&parent->mark, audit_watch_group);
- audit_put_parent(parent);
- }
- }
- static int audit_watch_handle_event(struct fsnotify_group *group,
- struct inode *to_tell,
- struct fsnotify_mark *inode_mark,
- struct fsnotify_mark *vfsmount_mark,
- u32 mask, void *data, int data_type,
- const unsigned char *dname, u32 cookie)
- {
- struct inode *inode;
- struct audit_parent *parent;
- parent = container_of(inode_mark, struct audit_parent, mark);
- BUG_ON(group != audit_watch_group);
- switch (data_type) {
- case (FSNOTIFY_EVENT_PATH):
- inode = d_backing_inode(((struct path *)data)->dentry);
- break;
- case (FSNOTIFY_EVENT_INODE):
- inode = (struct inode *)data;
- break;
- default:
- BUG();
- inode = NULL;
- break;
- };
- if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
- audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
- else if (mask & (FS_DELETE|FS_MOVED_FROM))
- audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
- else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
- audit_remove_parent_watches(parent);
- return 0;
- }
- static const struct fsnotify_ops audit_watch_fsnotify_ops = {
- .handle_event = audit_watch_handle_event,
- };
- static int __init audit_watch_init(void)
- {
- audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
- if (IS_ERR(audit_watch_group)) {
- audit_watch_group = NULL;
- audit_panic("cannot create audit fsnotify group");
- }
- return 0;
- }
- device_initcall(audit_watch_init);
- int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
- {
- struct audit_fsnotify_mark *audit_mark;
- char *pathname;
- pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
- if (!pathname)
- return -ENOMEM;
- audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
- if (IS_ERR(audit_mark)) {
- kfree(pathname);
- return PTR_ERR(audit_mark);
- }
- new->exe = audit_mark;
- return 0;
- }
- int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
- {
- struct file *exe_file;
- unsigned long ino;
- dev_t dev;
- exe_file = get_task_exe_file(tsk);
- if (!exe_file)
- return 0;
- ino = exe_file->f_inode->i_ino;
- dev = exe_file->f_inode->i_sb->s_dev;
- fput(exe_file);
- return audit_mark_compare(mark, ino, dev);
- }
|