2 #include <linux/inotify.h>
3 #include <linux/namei.h>
4 #include <linux/mount.h>
12 struct audit_chunk *root;
13 struct list_head chunks;
14 struct list_head rules;
15 struct list_head list;
16 struct list_head same_root;
22 struct list_head hash;
23 struct inotify_watch watch;
24 struct list_head trees; /* with root here */
29 struct list_head list;
30 struct audit_tree *owner;
31 unsigned index; /* index; upper bit indicates 'will prune' */
35 static LIST_HEAD(tree_list);
36 static LIST_HEAD(prune_list);
39 * One struct chunk is attached to each inode of interest.
40 * We replace struct chunk on tagging/untagging.
41 * Rules have pointer to struct audit_tree.
42 * Rules have struct list_head rlist forming a list of rules over
44 * References to struct chunk are collected at audit_inode{,_child}()
45 * time and used in AUDIT_TREE rule matching.
46 * These references are dropped at the same time we are calling
47 * audit_free_names(), etc.
49 * Cyclic lists galore:
50 * tree.chunks anchors chunk.owners[].list hash_lock
51 * tree.rules anchors rule.rlist audit_filter_mutex
52 * chunk.trees anchors tree.same_root hash_lock
53 * chunk.hash is a hash with middle bits of watch.inode as
54 * a hash function. RCU, hash_lock
56 * tree is refcounted; one reference for "some rules on rules_list refer to
57 * it", one for each chunk with pointer to it.
59 * chunk is refcounted by embedded inotify_watch.
61 * node.index allows to get from node.list to containing chunk.
62 * MSB of that sucker is stolen to mark taggings that we might have to
63 * revert - several operations have very unpleasant cleanup logics and
64 * that makes a difference. Some.
67 static struct inotify_handle *rtree_ih;
69 static struct audit_tree *alloc_tree(const char *s)
71 struct audit_tree *tree;
73 tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
75 atomic_set(&tree->count, 1);
77 INIT_LIST_HEAD(&tree->chunks);
78 INIT_LIST_HEAD(&tree->rules);
79 INIT_LIST_HEAD(&tree->list);
80 INIT_LIST_HEAD(&tree->same_root);
82 strcpy(tree->pathname, s);
87 static inline void get_tree(struct audit_tree *tree)
89 atomic_inc(&tree->count);
92 static void __put_tree(struct rcu_head *rcu)
94 struct audit_tree *tree = container_of(rcu, struct audit_tree, head);
98 static inline void put_tree(struct audit_tree *tree)
100 if (atomic_dec_and_test(&tree->count))
101 call_rcu(&tree->head, __put_tree);
104 /* to avoid bringing the entire thing in audit.h */
105 const char *audit_tree_path(struct audit_tree *tree)
107 return tree->pathname;
110 static struct audit_chunk *alloc_chunk(int count)
112 struct audit_chunk *chunk;
116 size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
117 chunk = kzalloc(size, GFP_KERNEL);
121 INIT_LIST_HEAD(&chunk->hash);
122 INIT_LIST_HEAD(&chunk->trees);
123 chunk->count = count;
124 for (i = 0; i < count; i++) {
125 INIT_LIST_HEAD(&chunk->owners[i].list);
126 chunk->owners[i].index = i;
128 inotify_init_watch(&chunk->watch);
132 static void __free_chunk(struct rcu_head *rcu)
134 struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
137 for (i = 0; i < chunk->count; i++) {
138 if (chunk->owners[i].owner)
139 put_tree(chunk->owners[i].owner);
144 static inline void free_chunk(struct audit_chunk *chunk)
146 call_rcu(&chunk->head, __free_chunk);
149 void audit_put_chunk(struct audit_chunk *chunk)
151 put_inotify_watch(&chunk->watch);
154 enum {HASH_SIZE = 128};
155 static struct list_head chunk_hash_heads[HASH_SIZE];
156 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
158 static inline struct list_head *chunk_hash(const struct inode *inode)
160 unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
161 return chunk_hash_heads + n % HASH_SIZE;
164 /* hash_lock is held by caller */
165 static void insert_hash(struct audit_chunk *chunk)
167 struct list_head *list = chunk_hash(chunk->watch.inode);
168 list_add_rcu(&chunk->hash, list);
171 /* called under rcu_read_lock */
172 struct audit_chunk *audit_tree_lookup(const struct inode *inode)
174 struct list_head *list = chunk_hash(inode);
175 struct audit_chunk *p;
177 list_for_each_entry_rcu(p, list, hash) {
178 if (p->watch.inode == inode) {
179 get_inotify_watch(&p->watch);
186 int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
189 for (n = 0; n < chunk->count; n++)
190 if (chunk->owners[n].owner == tree)
195 /* tagging and untagging inodes with trees */
197 static void untag_chunk(struct audit_chunk *chunk, struct node *p)
199 struct audit_chunk *new;
200 struct audit_tree *owner;
201 int size = chunk->count - 1;
204 mutex_lock(&chunk->watch.inode->inotify_mutex);
206 mutex_unlock(&chunk->watch.inode->inotify_mutex);
214 spin_lock(&hash_lock);
215 list_del_init(&chunk->trees);
216 if (owner->root == chunk)
218 list_del_init(&p->list);
219 list_del_rcu(&chunk->hash);
220 spin_unlock(&hash_lock);
221 inotify_evict_watch(&chunk->watch);
222 mutex_unlock(&chunk->watch.inode->inotify_mutex);
223 put_inotify_watch(&chunk->watch);
227 new = alloc_chunk(size);
230 if (inotify_clone_watch(&chunk->watch, &new->watch) < 0) {
236 spin_lock(&hash_lock);
237 list_replace_init(&chunk->trees, &new->trees);
238 if (owner->root == chunk) {
239 list_del_init(&owner->same_root);
243 for (i = j = 0; i < size; i++, j++) {
244 struct audit_tree *s;
245 if (&chunk->owners[j] == p) {
246 list_del_init(&p->list);
250 s = chunk->owners[j].owner;
251 new->owners[i].owner = s;
252 new->owners[i].index = chunk->owners[j].index - j + i;
253 if (!s) /* result of earlier fallback */
256 list_replace_init(&chunk->owners[i].list, &new->owners[j].list);
259 list_replace_rcu(&chunk->hash, &new->hash);
260 list_for_each_entry(owner, &new->trees, same_root)
262 spin_unlock(&hash_lock);
263 inotify_evict_watch(&chunk->watch);
264 mutex_unlock(&chunk->watch.inode->inotify_mutex);
265 put_inotify_watch(&chunk->watch);
269 // do the best we can
270 spin_lock(&hash_lock);
271 if (owner->root == chunk) {
272 list_del_init(&owner->same_root);
275 list_del_init(&p->list);
278 spin_unlock(&hash_lock);
279 mutex_unlock(&chunk->watch.inode->inotify_mutex);
282 static int create_chunk(struct inode *inode, struct audit_tree *tree)
284 struct audit_chunk *chunk = alloc_chunk(1);
288 if (inotify_add_watch(rtree_ih, &chunk->watch, inode, IN_IGNORED | IN_DELETE_SELF) < 0) {
293 mutex_lock(&inode->inotify_mutex);
294 spin_lock(&hash_lock);
296 spin_unlock(&hash_lock);
298 inotify_evict_watch(&chunk->watch);
299 mutex_unlock(&inode->inotify_mutex);
300 put_inotify_watch(&chunk->watch);
303 chunk->owners[0].index = (1U << 31);
304 chunk->owners[0].owner = tree;
306 list_add(&chunk->owners[0].list, &tree->chunks);
309 list_add(&tree->same_root, &chunk->trees);
312 spin_unlock(&hash_lock);
313 mutex_unlock(&inode->inotify_mutex);
317 /* the first tagged inode becomes root of tree */
318 static int tag_chunk(struct inode *inode, struct audit_tree *tree)
320 struct inotify_watch *watch;
321 struct audit_tree *owner;
322 struct audit_chunk *chunk, *old;
326 if (inotify_find_watch(rtree_ih, inode, &watch) < 0)
327 return create_chunk(inode, tree);
329 old = container_of(watch, struct audit_chunk, watch);
331 /* are we already there? */
332 spin_lock(&hash_lock);
333 for (n = 0; n < old->count; n++) {
334 if (old->owners[n].owner == tree) {
335 spin_unlock(&hash_lock);
336 put_inotify_watch(watch);
340 spin_unlock(&hash_lock);
342 chunk = alloc_chunk(old->count + 1);
346 mutex_lock(&inode->inotify_mutex);
347 if (inotify_clone_watch(&old->watch, &chunk->watch) < 0) {
348 mutex_unlock(&inode->inotify_mutex);
352 spin_lock(&hash_lock);
354 spin_unlock(&hash_lock);
356 inotify_evict_watch(&chunk->watch);
357 mutex_unlock(&inode->inotify_mutex);
358 put_inotify_watch(&chunk->watch);
361 list_replace_init(&old->trees, &chunk->trees);
362 for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
363 struct audit_tree *s = old->owners[n].owner;
365 p->index = old->owners[n].index;
366 if (!s) /* result of fallback in untag */
369 list_replace_init(&old->owners[n].list, &p->list);
371 p->index = (chunk->count - 1) | (1U<<31);
374 list_add(&p->list, &tree->chunks);
375 list_replace_rcu(&old->hash, &chunk->hash);
376 list_for_each_entry(owner, &chunk->trees, same_root)
381 list_add(&tree->same_root, &chunk->trees);
383 spin_unlock(&hash_lock);
384 inotify_evict_watch(&old->watch);
385 mutex_unlock(&inode->inotify_mutex);
386 put_inotify_watch(&old->watch);
390 static struct audit_chunk *find_chunk(struct node *p)
392 int index = p->index & ~(1U<<31);
394 return container_of(p, struct audit_chunk, owners[0]);
397 static void kill_rules(struct audit_tree *tree)
399 struct audit_krule *rule, *next;
400 struct audit_entry *entry;
401 struct audit_buffer *ab;
403 list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
404 entry = container_of(rule, struct audit_entry, rule);
406 list_del_init(&rule->rlist);
408 /* not a half-baked one */
409 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
410 audit_log_format(ab, "op=remove rule dir=");
411 audit_log_untrustedstring(ab, rule->tree->pathname);
412 if (rule->filterkey) {
413 audit_log_format(ab, " key=");
414 audit_log_untrustedstring(ab, rule->filterkey);
416 audit_log_format(ab, " key=(null)");
417 audit_log_format(ab, " list=%d res=1", rule->listnr);
420 list_del_rcu(&entry->list);
421 call_rcu(&entry->rcu, audit_free_rule_rcu);
427 * finish killing struct audit_tree
429 static void prune_one(struct audit_tree *victim)
431 spin_lock(&hash_lock);
432 while (!list_empty(&victim->chunks)) {
434 struct audit_chunk *chunk;
436 p = list_entry(victim->chunks.next, struct node, list);
437 chunk = find_chunk(p);
438 get_inotify_watch(&chunk->watch);
439 spin_unlock(&hash_lock);
441 untag_chunk(chunk, p);
443 put_inotify_watch(&chunk->watch);
444 spin_lock(&hash_lock);
446 spin_unlock(&hash_lock);
450 /* trim the uncommitted chunks from tree */
452 static void trim_marked(struct audit_tree *tree)
454 struct list_head *p, *q;
455 spin_lock(&hash_lock);
457 spin_unlock(&hash_lock);
461 for (p = tree->chunks.next; p != &tree->chunks; p = q) {
462 struct node *node = list_entry(p, struct node, list);
464 if (node->index & (1U<<31)) {
466 list_add(p, &tree->chunks);
470 while (!list_empty(&tree->chunks)) {
472 struct audit_chunk *chunk;
474 node = list_entry(tree->chunks.next, struct node, list);
476 /* have we run out of marked? */
477 if (!(node->index & (1U<<31)))
480 chunk = find_chunk(node);
481 get_inotify_watch(&chunk->watch);
482 spin_unlock(&hash_lock);
484 untag_chunk(chunk, node);
486 put_inotify_watch(&chunk->watch);
487 spin_lock(&hash_lock);
489 if (!tree->root && !tree->goner) {
491 spin_unlock(&hash_lock);
492 mutex_lock(&audit_filter_mutex);
494 list_del_init(&tree->list);
495 mutex_unlock(&audit_filter_mutex);
498 spin_unlock(&hash_lock);
502 /* called with audit_filter_mutex */
503 int audit_remove_tree_rule(struct audit_krule *rule)
505 struct audit_tree *tree;
508 spin_lock(&hash_lock);
509 list_del_init(&rule->rlist);
510 if (list_empty(&tree->rules) && !tree->goner) {
512 list_del_init(&tree->same_root);
514 list_move(&tree->list, &prune_list);
516 spin_unlock(&hash_lock);
517 audit_schedule_prune();
521 spin_unlock(&hash_lock);
527 void audit_trim_trees(void)
529 struct list_head cursor;
531 mutex_lock(&audit_filter_mutex);
532 list_add(&cursor, &tree_list);
533 while (cursor.next != &tree_list) {
534 struct audit_tree *tree;
536 struct vfsmount *root_mnt;
538 struct list_head list;
541 tree = container_of(cursor.next, struct audit_tree, list);
544 list_add(&cursor, &tree->list);
545 mutex_unlock(&audit_filter_mutex);
547 err = path_lookup(tree->pathname, 0, &nd);
551 root_mnt = collect_mounts(nd.path.mnt, nd.path.dentry);
556 list_add_tail(&list, &root_mnt->mnt_list);
557 spin_lock(&hash_lock);
558 list_for_each_entry(node, &tree->chunks, list) {
559 struct audit_chunk *chunk = find_chunk(node);
560 struct inode *inode = chunk->watch.inode;
561 struct vfsmount *mnt;
562 node->index |= 1U<<31;
563 list_for_each_entry(mnt, &list, mnt_list) {
564 if (mnt->mnt_root->d_inode == inode) {
565 node->index &= ~(1U<<31);
570 spin_unlock(&hash_lock);
573 list_del_init(&list);
574 drop_collected_mounts(root_mnt);
576 mutex_lock(&audit_filter_mutex);
579 mutex_unlock(&audit_filter_mutex);
582 static int is_under(struct vfsmount *mnt, struct dentry *dentry,
583 struct nameidata *nd)
585 if (mnt != nd->path.mnt) {
587 if (mnt->mnt_parent == mnt)
589 if (mnt->mnt_parent == nd->path.mnt)
591 mnt = mnt->mnt_parent;
593 dentry = mnt->mnt_mountpoint;
595 return is_subdir(dentry, nd->path.dentry);
598 int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
601 if (pathname[0] != '/' ||
602 rule->listnr != AUDIT_FILTER_EXIT ||
604 rule->inode_f || rule->watch || rule->tree)
606 rule->tree = alloc_tree(pathname);
612 void audit_put_tree(struct audit_tree *tree)
617 /* called with audit_filter_mutex */
618 int audit_add_tree_rule(struct audit_krule *rule)
620 struct audit_tree *seed = rule->tree, *tree;
622 struct vfsmount *mnt, *p;
623 struct list_head list;
626 list_for_each_entry(tree, &tree_list, list) {
627 if (!strcmp(seed->pathname, tree->pathname)) {
630 list_add(&rule->rlist, &tree->rules);
635 list_add(&tree->list, &tree_list);
636 list_add(&rule->rlist, &tree->rules);
637 /* do not set rule->tree yet */
638 mutex_unlock(&audit_filter_mutex);
640 err = path_lookup(tree->pathname, 0, &nd);
643 mnt = collect_mounts(nd.path.mnt, nd.path.dentry);
649 list_add_tail(&list, &mnt->mnt_list);
652 list_for_each_entry(p, &list, mnt_list) {
653 err = tag_chunk(p->mnt_root->d_inode, tree);
659 drop_collected_mounts(mnt);
663 spin_lock(&hash_lock);
664 list_for_each_entry(node, &tree->chunks, list)
665 node->index &= ~(1U<<31);
666 spin_unlock(&hash_lock);
672 mutex_lock(&audit_filter_mutex);
673 if (list_empty(&rule->rlist)) {
682 mutex_lock(&audit_filter_mutex);
683 list_del_init(&tree->list);
684 list_del_init(&tree->rules);
689 int audit_tag_tree(char *old, char *new)
691 struct list_head cursor, barrier;
694 struct vfsmount *tagged;
695 struct list_head list;
696 struct vfsmount *mnt;
697 struct dentry *dentry;
700 err = path_lookup(new, 0, &nd);
703 tagged = collect_mounts(nd.path.mnt, nd.path.dentry);
708 err = path_lookup(old, 0, &nd);
710 drop_collected_mounts(tagged);
713 mnt = mntget(nd.path.mnt);
714 dentry = dget(nd.path.dentry);
717 if (dentry == tagged->mnt_root && dentry == mnt->mnt_root)
718 follow_up(&mnt, &dentry);
720 list_add_tail(&list, &tagged->mnt_list);
722 mutex_lock(&audit_filter_mutex);
723 list_add(&barrier, &tree_list);
724 list_add(&cursor, &barrier);
726 while (cursor.next != &tree_list) {
727 struct audit_tree *tree;
730 tree = container_of(cursor.next, struct audit_tree, list);
733 list_add(&cursor, &tree->list);
734 mutex_unlock(&audit_filter_mutex);
736 err = path_lookup(tree->pathname, 0, &nd);
739 mutex_lock(&audit_filter_mutex);
743 spin_lock(&vfsmount_lock);
744 if (!is_under(mnt, dentry, &nd)) {
745 spin_unlock(&vfsmount_lock);
748 mutex_lock(&audit_filter_mutex);
751 spin_unlock(&vfsmount_lock);
754 list_for_each_entry(p, &list, mnt_list) {
755 failed = tag_chunk(p->mnt_root->d_inode, tree);
762 mutex_lock(&audit_filter_mutex);
766 mutex_lock(&audit_filter_mutex);
767 spin_lock(&hash_lock);
769 list_del(&tree->list);
770 list_add(&tree->list, &tree_list);
772 spin_unlock(&hash_lock);
776 while (barrier.prev != &tree_list) {
777 struct audit_tree *tree;
779 tree = container_of(barrier.prev, struct audit_tree, list);
781 list_del(&tree->list);
782 list_add(&tree->list, &barrier);
783 mutex_unlock(&audit_filter_mutex);
787 spin_lock(&hash_lock);
788 list_for_each_entry(node, &tree->chunks, list)
789 node->index &= ~(1U<<31);
790 spin_unlock(&hash_lock);
796 mutex_lock(&audit_filter_mutex);
801 mutex_unlock(&audit_filter_mutex);
804 drop_collected_mounts(tagged);
809 * That gets run when evict_chunk() ends up needing to kill audit_tree.
810 * Runs from a separate thread, with audit_cmd_mutex held.
812 void audit_prune_trees(void)
814 mutex_lock(&audit_filter_mutex);
816 while (!list_empty(&prune_list)) {
817 struct audit_tree *victim;
819 victim = list_entry(prune_list.next, struct audit_tree, list);
820 list_del_init(&victim->list);
822 mutex_unlock(&audit_filter_mutex);
826 mutex_lock(&audit_filter_mutex);
829 mutex_unlock(&audit_filter_mutex);
833 * Here comes the stuff asynchronous to auditctl operations
836 /* inode->inotify_mutex is locked */
837 static void evict_chunk(struct audit_chunk *chunk)
839 struct audit_tree *owner;
846 mutex_lock(&audit_filter_mutex);
847 spin_lock(&hash_lock);
848 while (!list_empty(&chunk->trees)) {
849 owner = list_entry(chunk->trees.next,
850 struct audit_tree, same_root);
853 list_del_init(&owner->same_root);
854 spin_unlock(&hash_lock);
856 list_move(&owner->list, &prune_list);
857 audit_schedule_prune();
858 spin_lock(&hash_lock);
860 list_del_rcu(&chunk->hash);
861 for (n = 0; n < chunk->count; n++)
862 list_del_init(&chunk->owners[n].list);
863 spin_unlock(&hash_lock);
864 mutex_unlock(&audit_filter_mutex);
867 static void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
868 u32 cookie, const char *dname, struct inode *inode)
870 struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
872 if (mask & IN_IGNORED) {
874 put_inotify_watch(watch);
878 static void destroy_watch(struct inotify_watch *watch)
880 struct audit_chunk *chunk = container_of(watch, struct audit_chunk, watch);
884 static const struct inotify_operations rtree_inotify_ops = {
885 .handle_event = handle_event,
886 .destroy_watch = destroy_watch,
889 static int __init audit_tree_init(void)
893 rtree_ih = inotify_init(&rtree_inotify_ops);
894 if (IS_ERR(rtree_ih))
895 audit_panic("cannot initialize inotify handle for rectree watches");
897 for (i = 0; i < HASH_SIZE; i++)
898 INIT_LIST_HEAD(&chunk_hash_heads[i]);
902 __initcall(audit_tree_init);