2 #include <linux/fsnotify_backend.h>
3 #include <linux/namei.h>
4 #include <linux/mount.h>
5 #include <linux/kthread.h>
6 #include <linux/slab.h>
14 struct audit_chunk *root;
15 struct list_head chunks;
16 struct list_head rules;
17 struct list_head list;
18 struct list_head same_root;
24 struct list_head hash;
25 struct fsnotify_mark mark;
26 struct list_head trees; /* with root here */
32 struct list_head list;
33 struct audit_tree *owner;
34 unsigned index; /* index; upper bit indicates 'will prune' */
38 static LIST_HEAD(tree_list);
39 static LIST_HEAD(prune_list);
42 * One struct chunk is attached to each inode of interest.
43 * We replace struct chunk on tagging/untagging.
44 * Rules have pointer to struct audit_tree.
45 * Rules have struct list_head rlist forming a list of rules over
47 * References to struct chunk are collected at audit_inode{,_child}()
48 * time and used in AUDIT_TREE rule matching.
49 * These references are dropped at the same time we are calling
50 * audit_free_names(), etc.
52 * Cyclic lists galore:
53 * tree.chunks anchors chunk.owners[].list hash_lock
54 * tree.rules anchors rule.rlist audit_filter_mutex
55 * chunk.trees anchors tree.same_root hash_lock
56 * chunk.hash is a hash with middle bits of watch.inode as
57 * a hash function. RCU, hash_lock
59 * tree is refcounted; one reference for "some rules on rules_list refer to
60 * it", one for each chunk with pointer to it.
62 * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
63 * of watch contributes 1 to .refs).
65 * node.index allows to get from node.list to containing chunk.
66 * MSB of that sucker is stolen to mark taggings that we might have to
67 * revert - several operations have very unpleasant cleanup logics and
68 * that makes a difference. Some.
71 static struct fsnotify_group *audit_tree_group;
73 static struct audit_tree *alloc_tree(const char *s)
75 struct audit_tree *tree;
77 tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
79 atomic_set(&tree->count, 1);
81 INIT_LIST_HEAD(&tree->chunks);
82 INIT_LIST_HEAD(&tree->rules);
83 INIT_LIST_HEAD(&tree->list);
84 INIT_LIST_HEAD(&tree->same_root);
86 strcpy(tree->pathname, s);
91 static inline void get_tree(struct audit_tree *tree)
93 atomic_inc(&tree->count);
96 static void __put_tree(struct rcu_head *rcu)
98 struct audit_tree *tree = container_of(rcu, struct audit_tree, head);
102 static inline void put_tree(struct audit_tree *tree)
104 if (atomic_dec_and_test(&tree->count))
105 call_rcu(&tree->head, __put_tree);
108 /* to avoid bringing the entire thing in audit.h */
109 const char *audit_tree_path(struct audit_tree *tree)
111 return tree->pathname;
114 static void free_chunk(struct audit_chunk *chunk)
118 for (i = 0; i < chunk->count; i++) {
119 if (chunk->owners[i].owner)
120 put_tree(chunk->owners[i].owner);
125 void audit_put_chunk(struct audit_chunk *chunk)
127 if (atomic_long_dec_and_test(&chunk->refs))
131 static void __put_chunk(struct rcu_head *rcu)
133 struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
134 audit_put_chunk(chunk);
137 static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
139 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
140 call_rcu(&chunk->head, __put_chunk);
143 static struct audit_chunk *alloc_chunk(int count)
145 struct audit_chunk *chunk;
149 size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
150 chunk = kzalloc(size, GFP_KERNEL);
154 INIT_LIST_HEAD(&chunk->hash);
155 INIT_LIST_HEAD(&chunk->trees);
156 chunk->count = count;
157 atomic_long_set(&chunk->refs, 1);
158 for (i = 0; i < count; i++) {
159 INIT_LIST_HEAD(&chunk->owners[i].list);
160 chunk->owners[i].index = i;
162 fsnotify_init_mark(&chunk->mark, audit_tree_destroy_watch);
166 enum {HASH_SIZE = 128};
167 static struct list_head chunk_hash_heads[HASH_SIZE];
168 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
170 static inline struct list_head *chunk_hash(const struct inode *inode)
172 unsigned long n = (unsigned long)inode / L1_CACHE_BYTES;
173 return chunk_hash_heads + n % HASH_SIZE;
176 /* hash_lock & entry->lock is held by caller */
177 static void insert_hash(struct audit_chunk *chunk)
179 struct fsnotify_mark *entry = &chunk->mark;
180 struct list_head *list;
184 list = chunk_hash(entry->i.inode);
185 list_add_rcu(&chunk->hash, list);
188 /* called under rcu_read_lock */
189 struct audit_chunk *audit_tree_lookup(const struct inode *inode)
191 struct list_head *list = chunk_hash(inode);
192 struct audit_chunk *p;
194 list_for_each_entry_rcu(p, list, hash) {
195 /* mark.inode may have gone NULL, but who cares? */
196 if (p->mark.i.inode == inode) {
197 atomic_long_inc(&p->refs);
204 int audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
207 for (n = 0; n < chunk->count; n++)
208 if (chunk->owners[n].owner == tree)
213 /* tagging and untagging inodes with trees */
215 static struct audit_chunk *find_chunk(struct node *p)
217 int index = p->index & ~(1U<<31);
219 return container_of(p, struct audit_chunk, owners[0]);
222 static void untag_chunk(struct node *p)
224 struct audit_chunk *chunk = find_chunk(p);
225 struct fsnotify_mark *entry = &chunk->mark;
226 struct audit_chunk *new = NULL;
227 struct audit_tree *owner;
228 int size = chunk->count - 1;
231 fsnotify_get_mark(entry);
233 spin_unlock(&hash_lock);
236 new = alloc_chunk(size);
238 spin_lock(&entry->lock);
239 if (chunk->dead || !entry->i.inode) {
240 spin_unlock(&entry->lock);
250 spin_lock(&hash_lock);
251 list_del_init(&chunk->trees);
252 if (owner->root == chunk)
254 list_del_init(&p->list);
255 list_del_rcu(&chunk->hash);
256 spin_unlock(&hash_lock);
257 spin_unlock(&entry->lock);
258 fsnotify_destroy_mark(entry);
259 fsnotify_put_mark(entry);
266 fsnotify_duplicate_mark(&new->mark, entry);
267 if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, NULL, 1)) {
273 spin_lock(&hash_lock);
274 list_replace_init(&chunk->trees, &new->trees);
275 if (owner->root == chunk) {
276 list_del_init(&owner->same_root);
280 for (i = j = 0; j <= size; i++, j++) {
281 struct audit_tree *s;
282 if (&chunk->owners[j] == p) {
283 list_del_init(&p->list);
287 s = chunk->owners[j].owner;
288 new->owners[i].owner = s;
289 new->owners[i].index = chunk->owners[j].index - j + i;
290 if (!s) /* result of earlier fallback */
293 list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
296 list_replace_rcu(&chunk->hash, &new->hash);
297 list_for_each_entry(owner, &new->trees, same_root)
299 spin_unlock(&hash_lock);
300 spin_unlock(&entry->lock);
301 fsnotify_destroy_mark(entry);
302 fsnotify_put_mark(entry);
306 // do the best we can
307 spin_lock(&hash_lock);
308 if (owner->root == chunk) {
309 list_del_init(&owner->same_root);
312 list_del_init(&p->list);
315 spin_unlock(&hash_lock);
316 spin_unlock(&entry->lock);
318 fsnotify_put_mark(entry);
319 spin_lock(&hash_lock);
322 static int create_chunk(struct inode *inode, struct audit_tree *tree)
324 struct fsnotify_mark *entry;
325 struct audit_chunk *chunk = alloc_chunk(1);
329 entry = &chunk->mark;
330 if (fsnotify_add_mark(entry, audit_tree_group, inode, NULL, 0)) {
335 spin_lock(&entry->lock);
336 spin_lock(&hash_lock);
338 spin_unlock(&hash_lock);
340 spin_unlock(&entry->lock);
341 fsnotify_destroy_mark(entry);
342 fsnotify_put_mark(entry);
345 chunk->owners[0].index = (1U << 31);
346 chunk->owners[0].owner = tree;
348 list_add(&chunk->owners[0].list, &tree->chunks);
351 list_add(&tree->same_root, &chunk->trees);
354 spin_unlock(&hash_lock);
355 spin_unlock(&entry->lock);
359 /* the first tagged inode becomes root of tree */
360 static int tag_chunk(struct inode *inode, struct audit_tree *tree)
362 struct fsnotify_mark *old_entry, *chunk_entry;
363 struct audit_tree *owner;
364 struct audit_chunk *chunk, *old;
368 old_entry = fsnotify_find_inode_mark(audit_tree_group, inode);
370 return create_chunk(inode, tree);
372 old = container_of(old_entry, struct audit_chunk, mark);
374 /* are we already there? */
375 spin_lock(&hash_lock);
376 for (n = 0; n < old->count; n++) {
377 if (old->owners[n].owner == tree) {
378 spin_unlock(&hash_lock);
379 fsnotify_put_mark(old_entry);
383 spin_unlock(&hash_lock);
385 chunk = alloc_chunk(old->count + 1);
387 fsnotify_put_mark(old_entry);
391 chunk_entry = &chunk->mark;
393 spin_lock(&old_entry->lock);
394 if (!old_entry->i.inode) {
395 /* old_entry is being shot, lets just lie */
396 spin_unlock(&old_entry->lock);
397 fsnotify_put_mark(old_entry);
402 fsnotify_duplicate_mark(chunk_entry, old_entry);
403 if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, NULL, 1)) {
404 spin_unlock(&old_entry->lock);
406 fsnotify_put_mark(old_entry);
410 /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
411 spin_lock(&chunk_entry->lock);
412 spin_lock(&hash_lock);
414 /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
416 spin_unlock(&hash_lock);
418 spin_unlock(&chunk_entry->lock);
419 spin_unlock(&old_entry->lock);
421 fsnotify_destroy_mark(chunk_entry);
423 fsnotify_put_mark(chunk_entry);
424 fsnotify_put_mark(old_entry);
427 list_replace_init(&old->trees, &chunk->trees);
428 for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
429 struct audit_tree *s = old->owners[n].owner;
431 p->index = old->owners[n].index;
432 if (!s) /* result of fallback in untag */
435 list_replace_init(&old->owners[n].list, &p->list);
437 p->index = (chunk->count - 1) | (1U<<31);
440 list_add(&p->list, &tree->chunks);
441 list_replace_rcu(&old->hash, &chunk->hash);
442 list_for_each_entry(owner, &chunk->trees, same_root)
447 list_add(&tree->same_root, &chunk->trees);
449 spin_unlock(&hash_lock);
450 spin_unlock(&chunk_entry->lock);
451 spin_unlock(&old_entry->lock);
452 fsnotify_destroy_mark(old_entry);
453 fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
454 fsnotify_put_mark(old_entry); /* and kill it */
458 static void kill_rules(struct audit_tree *tree)
460 struct audit_krule *rule, *next;
461 struct audit_entry *entry;
462 struct audit_buffer *ab;
464 list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
465 entry = container_of(rule, struct audit_entry, rule);
467 list_del_init(&rule->rlist);
469 /* not a half-baked one */
470 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
471 audit_log_format(ab, "op=");
472 audit_log_string(ab, "remove rule");
473 audit_log_format(ab, " dir=");
474 audit_log_untrustedstring(ab, rule->tree->pathname);
475 audit_log_key(ab, rule->filterkey);
476 audit_log_format(ab, " list=%d res=1", rule->listnr);
479 list_del_rcu(&entry->list);
480 list_del(&entry->rule.list);
481 call_rcu(&entry->rcu, audit_free_rule_rcu);
487 * finish killing struct audit_tree
489 static void prune_one(struct audit_tree *victim)
491 spin_lock(&hash_lock);
492 while (!list_empty(&victim->chunks)) {
495 p = list_entry(victim->chunks.next, struct node, list);
499 spin_unlock(&hash_lock);
503 /* trim the uncommitted chunks from tree */
505 static void trim_marked(struct audit_tree *tree)
507 struct list_head *p, *q;
508 spin_lock(&hash_lock);
510 spin_unlock(&hash_lock);
514 for (p = tree->chunks.next; p != &tree->chunks; p = q) {
515 struct node *node = list_entry(p, struct node, list);
517 if (node->index & (1U<<31)) {
519 list_add(p, &tree->chunks);
523 while (!list_empty(&tree->chunks)) {
526 node = list_entry(tree->chunks.next, struct node, list);
528 /* have we run out of marked? */
529 if (!(node->index & (1U<<31)))
534 if (!tree->root && !tree->goner) {
536 spin_unlock(&hash_lock);
537 mutex_lock(&audit_filter_mutex);
539 list_del_init(&tree->list);
540 mutex_unlock(&audit_filter_mutex);
543 spin_unlock(&hash_lock);
547 static void audit_schedule_prune(void);
549 /* called with audit_filter_mutex */
550 int audit_remove_tree_rule(struct audit_krule *rule)
552 struct audit_tree *tree;
555 spin_lock(&hash_lock);
556 list_del_init(&rule->rlist);
557 if (list_empty(&tree->rules) && !tree->goner) {
559 list_del_init(&tree->same_root);
561 list_move(&tree->list, &prune_list);
563 spin_unlock(&hash_lock);
564 audit_schedule_prune();
568 spin_unlock(&hash_lock);
574 static int compare_root(struct vfsmount *mnt, void *arg)
576 return mnt->mnt_root->d_inode == arg;
579 void audit_trim_trees(void)
581 struct list_head cursor;
583 mutex_lock(&audit_filter_mutex);
584 list_add(&cursor, &tree_list);
585 while (cursor.next != &tree_list) {
586 struct audit_tree *tree;
588 struct vfsmount *root_mnt;
592 tree = container_of(cursor.next, struct audit_tree, list);
595 list_add(&cursor, &tree->list);
596 mutex_unlock(&audit_filter_mutex);
598 err = kern_path(tree->pathname, 0, &path);
602 root_mnt = collect_mounts(&path);
607 spin_lock(&hash_lock);
608 list_for_each_entry(node, &tree->chunks, list) {
609 struct audit_chunk *chunk = find_chunk(node);
610 /* this could be NULL if the watch is dying else where... */
611 struct inode *inode = chunk->mark.i.inode;
612 node->index |= 1U<<31;
613 if (iterate_mounts(compare_root, inode, root_mnt))
614 node->index &= ~(1U<<31);
616 spin_unlock(&hash_lock);
619 drop_collected_mounts(root_mnt);
621 mutex_lock(&audit_filter_mutex);
624 mutex_unlock(&audit_filter_mutex);
627 int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
630 if (pathname[0] != '/' ||
631 rule->listnr != AUDIT_FILTER_EXIT ||
633 rule->inode_f || rule->watch || rule->tree)
635 rule->tree = alloc_tree(pathname);
641 void audit_put_tree(struct audit_tree *tree)
646 static int tag_mount(struct vfsmount *mnt, void *arg)
648 return tag_chunk(mnt->mnt_root->d_inode, arg);
651 /* called with audit_filter_mutex */
652 int audit_add_tree_rule(struct audit_krule *rule)
654 struct audit_tree *seed = rule->tree, *tree;
656 struct vfsmount *mnt;
659 list_for_each_entry(tree, &tree_list, list) {
660 if (!strcmp(seed->pathname, tree->pathname)) {
663 list_add(&rule->rlist, &tree->rules);
668 list_add(&tree->list, &tree_list);
669 list_add(&rule->rlist, &tree->rules);
670 /* do not set rule->tree yet */
671 mutex_unlock(&audit_filter_mutex);
673 err = kern_path(tree->pathname, 0, &path);
676 mnt = collect_mounts(&path);
684 err = iterate_mounts(tag_mount, tree, mnt);
685 drop_collected_mounts(mnt);
689 spin_lock(&hash_lock);
690 list_for_each_entry(node, &tree->chunks, list)
691 node->index &= ~(1U<<31);
692 spin_unlock(&hash_lock);
698 mutex_lock(&audit_filter_mutex);
699 if (list_empty(&rule->rlist)) {
708 mutex_lock(&audit_filter_mutex);
709 list_del_init(&tree->list);
710 list_del_init(&tree->rules);
715 int audit_tag_tree(char *old, char *new)
717 struct list_head cursor, barrier;
719 struct path path1, path2;
720 struct vfsmount *tagged;
723 err = kern_path(new, 0, &path2);
726 tagged = collect_mounts(&path2);
731 err = kern_path(old, 0, &path1);
733 drop_collected_mounts(tagged);
737 mutex_lock(&audit_filter_mutex);
738 list_add(&barrier, &tree_list);
739 list_add(&cursor, &barrier);
741 while (cursor.next != &tree_list) {
742 struct audit_tree *tree;
745 tree = container_of(cursor.next, struct audit_tree, list);
748 list_add(&cursor, &tree->list);
749 mutex_unlock(&audit_filter_mutex);
751 err = kern_path(tree->pathname, 0, &path2);
753 good_one = path_is_under(&path1, &path2);
759 mutex_lock(&audit_filter_mutex);
763 failed = iterate_mounts(tag_mount, tree, tagged);
766 mutex_lock(&audit_filter_mutex);
770 mutex_lock(&audit_filter_mutex);
771 spin_lock(&hash_lock);
773 list_del(&tree->list);
774 list_add(&tree->list, &tree_list);
776 spin_unlock(&hash_lock);
780 while (barrier.prev != &tree_list) {
781 struct audit_tree *tree;
783 tree = container_of(barrier.prev, struct audit_tree, list);
785 list_del(&tree->list);
786 list_add(&tree->list, &barrier);
787 mutex_unlock(&audit_filter_mutex);
791 spin_lock(&hash_lock);
792 list_for_each_entry(node, &tree->chunks, list)
793 node->index &= ~(1U<<31);
794 spin_unlock(&hash_lock);
800 mutex_lock(&audit_filter_mutex);
804 mutex_unlock(&audit_filter_mutex);
806 drop_collected_mounts(tagged);
811 * That gets run when evict_chunk() ends up needing to kill audit_tree.
812 * Runs from a separate thread.
814 static int prune_tree_thread(void *unused)
816 mutex_lock(&audit_cmd_mutex);
817 mutex_lock(&audit_filter_mutex);
819 while (!list_empty(&prune_list)) {
820 struct audit_tree *victim;
822 victim = list_entry(prune_list.next, struct audit_tree, list);
823 list_del_init(&victim->list);
825 mutex_unlock(&audit_filter_mutex);
829 mutex_lock(&audit_filter_mutex);
832 mutex_unlock(&audit_filter_mutex);
833 mutex_unlock(&audit_cmd_mutex);
837 static void audit_schedule_prune(void)
839 kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
843 * ... and that one is done if evict_chunk() decides to delay until the end
844 * of syscall. Runs synchronously.
846 void audit_kill_trees(struct list_head *list)
848 mutex_lock(&audit_cmd_mutex);
849 mutex_lock(&audit_filter_mutex);
851 while (!list_empty(list)) {
852 struct audit_tree *victim;
854 victim = list_entry(list->next, struct audit_tree, list);
856 list_del_init(&victim->list);
858 mutex_unlock(&audit_filter_mutex);
862 mutex_lock(&audit_filter_mutex);
865 mutex_unlock(&audit_filter_mutex);
866 mutex_unlock(&audit_cmd_mutex);
870 * Here comes the stuff asynchronous to auditctl operations
873 static void evict_chunk(struct audit_chunk *chunk)
875 struct audit_tree *owner;
876 struct list_head *postponed = audit_killed_trees();
884 mutex_lock(&audit_filter_mutex);
885 spin_lock(&hash_lock);
886 while (!list_empty(&chunk->trees)) {
887 owner = list_entry(chunk->trees.next,
888 struct audit_tree, same_root);
891 list_del_init(&owner->same_root);
892 spin_unlock(&hash_lock);
895 list_move(&owner->list, &prune_list);
898 list_move(&owner->list, postponed);
900 spin_lock(&hash_lock);
902 list_del_rcu(&chunk->hash);
903 for (n = 0; n < chunk->count; n++)
904 list_del_init(&chunk->owners[n].list);
905 spin_unlock(&hash_lock);
907 audit_schedule_prune();
908 mutex_unlock(&audit_filter_mutex);
911 static int audit_tree_handle_event(struct fsnotify_group *group,
912 struct fsnotify_mark *inode_mark,
913 struct fsnotify_mark *vfsmonut_mark,
914 struct fsnotify_event *event)
920 static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
922 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
925 fsnotify_put_mark(entry);
928 static bool audit_tree_send_event(struct fsnotify_group *group, struct inode *inode,
929 struct fsnotify_mark *inode_mark,
930 struct fsnotify_mark *vfsmount_mark,
931 __u32 mask, void *data, int data_type)
936 static const struct fsnotify_ops audit_tree_ops = {
937 .handle_event = audit_tree_handle_event,
938 .should_send_event = audit_tree_send_event,
939 .free_group_priv = NULL,
940 .free_event_priv = NULL,
941 .freeing_mark = audit_tree_freeing_mark,
944 static int __init audit_tree_init(void)
948 audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
949 if (IS_ERR(audit_tree_group))
950 audit_panic("cannot initialize fsnotify group for rectree watches");
952 for (i = 0; i < HASH_SIZE; i++)
953 INIT_LIST_HEAD(&chunk_hash_heads[i]);
957 __initcall(audit_tree_init);