GNU Linux-libre 4.19.207-gnu1
[releases.git] / kernel / audit_tree.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "audit.h"
3 #include <linux/fsnotify_backend.h>
4 #include <linux/namei.h>
5 #include <linux/mount.h>
6 #include <linux/kthread.h>
7 #include <linux/refcount.h>
8 #include <linux/slab.h>
9
10 struct audit_tree;
11 struct audit_chunk;
12
13 struct audit_tree {
14         refcount_t count;
15         int goner;
16         struct audit_chunk *root;
17         struct list_head chunks;
18         struct list_head rules;
19         struct list_head list;
20         struct list_head same_root;
21         struct rcu_head head;
22         char pathname[];
23 };
24
25 struct audit_chunk {
26         struct list_head hash;
27         unsigned long key;
28         struct fsnotify_mark mark;
29         struct list_head trees;         /* with root here */
30         int dead;
31         int count;
32         atomic_long_t refs;
33         struct rcu_head head;
34         struct node {
35                 struct list_head list;
36                 struct audit_tree *owner;
37                 unsigned index;         /* index; upper bit indicates 'will prune' */
38         } owners[];
39 };
40
41 static LIST_HEAD(tree_list);
42 static LIST_HEAD(prune_list);
43 static struct task_struct *prune_thread;
44
45 /*
46  * One struct chunk is attached to each inode of interest.
47  * We replace struct chunk on tagging/untagging.
48  * Rules have pointer to struct audit_tree.
49  * Rules have struct list_head rlist forming a list of rules over
50  * the same tree.
51  * References to struct chunk are collected at audit_inode{,_child}()
52  * time and used in AUDIT_TREE rule matching.
53  * These references are dropped at the same time we are calling
54  * audit_free_names(), etc.
55  *
56  * Cyclic lists galore:
57  * tree.chunks anchors chunk.owners[].list                      hash_lock
58  * tree.rules anchors rule.rlist                                audit_filter_mutex
59  * chunk.trees anchors tree.same_root                           hash_lock
60  * chunk.hash is a hash with middle bits of watch.inode as
61  * a hash function.                                             RCU, hash_lock
62  *
63  * tree is refcounted; one reference for "some rules on rules_list refer to
64  * it", one for each chunk with pointer to it.
65  *
66  * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
67  * of watch contributes 1 to .refs).
68  *
69  * node.index allows to get from node.list to containing chunk.
70  * MSB of that sucker is stolen to mark taggings that we might have to
71  * revert - several operations have very unpleasant cleanup logics and
72  * that makes a difference.  Some.
73  */
74
75 static struct fsnotify_group *audit_tree_group;
76
77 static struct audit_tree *alloc_tree(const char *s)
78 {
79         struct audit_tree *tree;
80
81         tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
82         if (tree) {
83                 refcount_set(&tree->count, 1);
84                 tree->goner = 0;
85                 INIT_LIST_HEAD(&tree->chunks);
86                 INIT_LIST_HEAD(&tree->rules);
87                 INIT_LIST_HEAD(&tree->list);
88                 INIT_LIST_HEAD(&tree->same_root);
89                 tree->root = NULL;
90                 strcpy(tree->pathname, s);
91         }
92         return tree;
93 }
94
95 static inline void get_tree(struct audit_tree *tree)
96 {
97         refcount_inc(&tree->count);
98 }
99
100 static inline void put_tree(struct audit_tree *tree)
101 {
102         if (refcount_dec_and_test(&tree->count))
103                 kfree_rcu(tree, head);
104 }
105
106 /* to avoid bringing the entire thing in audit.h */
107 const char *audit_tree_path(struct audit_tree *tree)
108 {
109         return tree->pathname;
110 }
111
112 static void free_chunk(struct audit_chunk *chunk)
113 {
114         int i;
115
116         for (i = 0; i < chunk->count; i++) {
117                 if (chunk->owners[i].owner)
118                         put_tree(chunk->owners[i].owner);
119         }
120         kfree(chunk);
121 }
122
123 void audit_put_chunk(struct audit_chunk *chunk)
124 {
125         if (atomic_long_dec_and_test(&chunk->refs))
126                 free_chunk(chunk);
127 }
128
129 static void __put_chunk(struct rcu_head *rcu)
130 {
131         struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head);
132         audit_put_chunk(chunk);
133 }
134
135 static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
136 {
137         struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
138         call_rcu(&chunk->head, __put_chunk);
139 }
140
141 static struct audit_chunk *alloc_chunk(int count)
142 {
143         struct audit_chunk *chunk;
144         size_t size;
145         int i;
146
147         size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
148         chunk = kzalloc(size, GFP_KERNEL);
149         if (!chunk)
150                 return NULL;
151
152         INIT_LIST_HEAD(&chunk->hash);
153         INIT_LIST_HEAD(&chunk->trees);
154         chunk->count = count;
155         atomic_long_set(&chunk->refs, 1);
156         for (i = 0; i < count; i++) {
157                 INIT_LIST_HEAD(&chunk->owners[i].list);
158                 chunk->owners[i].index = i;
159         }
160         fsnotify_init_mark(&chunk->mark, audit_tree_group);
161         chunk->mark.mask = FS_IN_IGNORED;
162         return chunk;
163 }
164
165 enum {HASH_SIZE = 128};
166 static struct list_head chunk_hash_heads[HASH_SIZE];
167 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
168
169 /* Function to return search key in our hash from inode. */
170 static unsigned long inode_to_key(const struct inode *inode)
171 {
172         /* Use address pointed to by connector->obj as the key */
173         return (unsigned long)&inode->i_fsnotify_marks;
174 }
175
176 static inline struct list_head *chunk_hash(unsigned long key)
177 {
178         unsigned long n = key / L1_CACHE_BYTES;
179         return chunk_hash_heads + n % HASH_SIZE;
180 }
181
182 /* hash_lock & entry->lock is held by caller */
183 static void insert_hash(struct audit_chunk *chunk)
184 {
185         struct list_head *list;
186
187         if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED))
188                 return;
189         WARN_ON_ONCE(!chunk->key);
190         list = chunk_hash(chunk->key);
191         list_add_rcu(&chunk->hash, list);
192 }
193
194 /* called under rcu_read_lock */
195 struct audit_chunk *audit_tree_lookup(const struct inode *inode)
196 {
197         unsigned long key = inode_to_key(inode);
198         struct list_head *list = chunk_hash(key);
199         struct audit_chunk *p;
200
201         list_for_each_entry_rcu(p, list, hash) {
202                 if (p->key == key) {
203                         atomic_long_inc(&p->refs);
204                         return p;
205                 }
206         }
207         return NULL;
208 }
209
210 bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
211 {
212         int n;
213         for (n = 0; n < chunk->count; n++)
214                 if (chunk->owners[n].owner == tree)
215                         return true;
216         return false;
217 }
218
219 /* tagging and untagging inodes with trees */
220
221 static struct audit_chunk *find_chunk(struct node *p)
222 {
223         int index = p->index & ~(1U<<31);
224         p -= index;
225         return container_of(p, struct audit_chunk, owners[0]);
226 }
227
228 static void untag_chunk(struct node *p)
229 {
230         struct audit_chunk *chunk = find_chunk(p);
231         struct fsnotify_mark *entry = &chunk->mark;
232         struct audit_chunk *new = NULL;
233         struct audit_tree *owner;
234         int size = chunk->count - 1;
235         int i, j;
236
237         fsnotify_get_mark(entry);
238
239         spin_unlock(&hash_lock);
240
241         if (size)
242                 new = alloc_chunk(size);
243
244         mutex_lock(&entry->group->mark_mutex);
245         spin_lock(&entry->lock);
246         /*
247          * mark_mutex protects mark from getting detached and thus also from
248          * mark->connector->obj getting NULL.
249          */
250         if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
251                 spin_unlock(&entry->lock);
252                 mutex_unlock(&entry->group->mark_mutex);
253                 if (new)
254                         fsnotify_put_mark(&new->mark);
255                 goto out;
256         }
257
258         owner = p->owner;
259
260         if (!size) {
261                 chunk->dead = 1;
262                 spin_lock(&hash_lock);
263                 list_del_init(&chunk->trees);
264                 if (owner->root == chunk)
265                         owner->root = NULL;
266                 list_del_init(&p->list);
267                 list_del_rcu(&chunk->hash);
268                 spin_unlock(&hash_lock);
269                 spin_unlock(&entry->lock);
270                 mutex_unlock(&entry->group->mark_mutex);
271                 fsnotify_destroy_mark(entry, audit_tree_group);
272                 goto out;
273         }
274
275         if (!new)
276                 goto Fallback;
277
278         if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
279                                      FSNOTIFY_OBJ_TYPE_INODE, 1)) {
280                 fsnotify_put_mark(&new->mark);
281                 goto Fallback;
282         }
283
284         chunk->dead = 1;
285         spin_lock(&hash_lock);
286         new->key = chunk->key;
287         list_replace_init(&chunk->trees, &new->trees);
288         if (owner->root == chunk) {
289                 list_del_init(&owner->same_root);
290                 owner->root = NULL;
291         }
292
293         for (i = j = 0; j <= size; i++, j++) {
294                 struct audit_tree *s;
295                 if (&chunk->owners[j] == p) {
296                         list_del_init(&p->list);
297                         i--;
298                         continue;
299                 }
300                 s = chunk->owners[j].owner;
301                 new->owners[i].owner = s;
302                 new->owners[i].index = chunk->owners[j].index - j + i;
303                 if (!s) /* result of earlier fallback */
304                         continue;
305                 get_tree(s);
306                 list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
307         }
308
309         list_replace_rcu(&chunk->hash, &new->hash);
310         list_for_each_entry(owner, &new->trees, same_root)
311                 owner->root = new;
312         spin_unlock(&hash_lock);
313         spin_unlock(&entry->lock);
314         mutex_unlock(&entry->group->mark_mutex);
315         fsnotify_destroy_mark(entry, audit_tree_group);
316         fsnotify_put_mark(&new->mark);  /* drop initial reference */
317         goto out;
318
319 Fallback:
320         // do the best we can
321         spin_lock(&hash_lock);
322         if (owner->root == chunk) {
323                 list_del_init(&owner->same_root);
324                 owner->root = NULL;
325         }
326         list_del_init(&p->list);
327         p->owner = NULL;
328         put_tree(owner);
329         spin_unlock(&hash_lock);
330         spin_unlock(&entry->lock);
331         mutex_unlock(&entry->group->mark_mutex);
332 out:
333         fsnotify_put_mark(entry);
334         spin_lock(&hash_lock);
335 }
336
337 static int create_chunk(struct inode *inode, struct audit_tree *tree)
338 {
339         struct fsnotify_mark *entry;
340         struct audit_chunk *chunk = alloc_chunk(1);
341         if (!chunk)
342                 return -ENOMEM;
343
344         entry = &chunk->mark;
345         if (fsnotify_add_inode_mark(entry, inode, 0)) {
346                 fsnotify_put_mark(entry);
347                 return -ENOSPC;
348         }
349
350         spin_lock(&entry->lock);
351         spin_lock(&hash_lock);
352         if (tree->goner) {
353                 spin_unlock(&hash_lock);
354                 chunk->dead = 1;
355                 spin_unlock(&entry->lock);
356                 fsnotify_destroy_mark(entry, audit_tree_group);
357                 fsnotify_put_mark(entry);
358                 return 0;
359         }
360         chunk->owners[0].index = (1U << 31);
361         chunk->owners[0].owner = tree;
362         get_tree(tree);
363         list_add(&chunk->owners[0].list, &tree->chunks);
364         if (!tree->root) {
365                 tree->root = chunk;
366                 list_add(&tree->same_root, &chunk->trees);
367         }
368         chunk->key = inode_to_key(inode);
369         insert_hash(chunk);
370         spin_unlock(&hash_lock);
371         spin_unlock(&entry->lock);
372         fsnotify_put_mark(entry);       /* drop initial reference */
373         return 0;
374 }
375
376 /* the first tagged inode becomes root of tree */
377 static int tag_chunk(struct inode *inode, struct audit_tree *tree)
378 {
379         struct fsnotify_mark *old_entry, *chunk_entry;
380         struct audit_tree *owner;
381         struct audit_chunk *chunk, *old;
382         struct node *p;
383         int n;
384
385         old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks,
386                                        audit_tree_group);
387         if (!old_entry)
388                 return create_chunk(inode, tree);
389
390         old = container_of(old_entry, struct audit_chunk, mark);
391
392         /* are we already there? */
393         spin_lock(&hash_lock);
394         for (n = 0; n < old->count; n++) {
395                 if (old->owners[n].owner == tree) {
396                         spin_unlock(&hash_lock);
397                         fsnotify_put_mark(old_entry);
398                         return 0;
399                 }
400         }
401         spin_unlock(&hash_lock);
402
403         chunk = alloc_chunk(old->count + 1);
404         if (!chunk) {
405                 fsnotify_put_mark(old_entry);
406                 return -ENOMEM;
407         }
408
409         chunk_entry = &chunk->mark;
410
411         mutex_lock(&old_entry->group->mark_mutex);
412         spin_lock(&old_entry->lock);
413         /*
414          * mark_mutex protects mark from getting detached and thus also from
415          * mark->connector->obj getting NULL.
416          */
417         if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
418                 /* old_entry is being shot, lets just lie */
419                 spin_unlock(&old_entry->lock);
420                 mutex_unlock(&old_entry->group->mark_mutex);
421                 fsnotify_put_mark(old_entry);
422                 fsnotify_put_mark(&chunk->mark);
423                 return -ENOENT;
424         }
425
426         if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
427                                      FSNOTIFY_OBJ_TYPE_INODE, 1)) {
428                 spin_unlock(&old_entry->lock);
429                 mutex_unlock(&old_entry->group->mark_mutex);
430                 fsnotify_put_mark(chunk_entry);
431                 fsnotify_put_mark(old_entry);
432                 return -ENOSPC;
433         }
434
435         /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */
436         spin_lock(&chunk_entry->lock);
437         spin_lock(&hash_lock);
438
439         /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */
440         if (tree->goner) {
441                 spin_unlock(&hash_lock);
442                 chunk->dead = 1;
443                 spin_unlock(&chunk_entry->lock);
444                 spin_unlock(&old_entry->lock);
445                 mutex_unlock(&old_entry->group->mark_mutex);
446
447                 fsnotify_destroy_mark(chunk_entry, audit_tree_group);
448
449                 fsnotify_put_mark(chunk_entry);
450                 fsnotify_put_mark(old_entry);
451                 return 0;
452         }
453         chunk->key = old->key;
454         list_replace_init(&old->trees, &chunk->trees);
455         for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
456                 struct audit_tree *s = old->owners[n].owner;
457                 p->owner = s;
458                 p->index = old->owners[n].index;
459                 if (!s) /* result of fallback in untag */
460                         continue;
461                 get_tree(s);
462                 list_replace_init(&old->owners[n].list, &p->list);
463         }
464         p->index = (chunk->count - 1) | (1U<<31);
465         p->owner = tree;
466         get_tree(tree);
467         list_add(&p->list, &tree->chunks);
468         list_replace_rcu(&old->hash, &chunk->hash);
469         list_for_each_entry(owner, &chunk->trees, same_root)
470                 owner->root = chunk;
471         old->dead = 1;
472         if (!tree->root) {
473                 tree->root = chunk;
474                 list_add(&tree->same_root, &chunk->trees);
475         }
476         spin_unlock(&hash_lock);
477         spin_unlock(&chunk_entry->lock);
478         spin_unlock(&old_entry->lock);
479         mutex_unlock(&old_entry->group->mark_mutex);
480         fsnotify_destroy_mark(old_entry, audit_tree_group);
481         fsnotify_put_mark(chunk_entry); /* drop initial reference */
482         fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
483         return 0;
484 }
485
486 static void audit_tree_log_remove_rule(struct audit_krule *rule)
487 {
488         struct audit_buffer *ab;
489
490         if (!audit_enabled)
491                 return;
492         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
493         if (unlikely(!ab))
494                 return;
495         audit_log_format(ab, "op=remove_rule");
496         audit_log_format(ab, " dir=");
497         audit_log_untrustedstring(ab, rule->tree->pathname);
498         audit_log_key(ab, rule->filterkey);
499         audit_log_format(ab, " list=%d res=1", rule->listnr);
500         audit_log_end(ab);
501 }
502
503 static void kill_rules(struct audit_tree *tree)
504 {
505         struct audit_krule *rule, *next;
506         struct audit_entry *entry;
507
508         list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
509                 entry = container_of(rule, struct audit_entry, rule);
510
511                 list_del_init(&rule->rlist);
512                 if (rule->tree) {
513                         /* not a half-baked one */
514                         audit_tree_log_remove_rule(rule);
515                         if (entry->rule.exe)
516                                 audit_remove_mark(entry->rule.exe);
517                         rule->tree = NULL;
518                         list_del_rcu(&entry->list);
519                         list_del(&entry->rule.list);
520                         call_rcu(&entry->rcu, audit_free_rule_rcu);
521                 }
522         }
523 }
524
525 /*
526  * finish killing struct audit_tree
527  */
528 static void prune_one(struct audit_tree *victim)
529 {
530         spin_lock(&hash_lock);
531         while (!list_empty(&victim->chunks)) {
532                 struct node *p;
533
534                 p = list_entry(victim->chunks.next, struct node, list);
535
536                 untag_chunk(p);
537         }
538         spin_unlock(&hash_lock);
539         put_tree(victim);
540 }
541
542 /* trim the uncommitted chunks from tree */
543
544 static void trim_marked(struct audit_tree *tree)
545 {
546         struct list_head *p, *q;
547         spin_lock(&hash_lock);
548         if (tree->goner) {
549                 spin_unlock(&hash_lock);
550                 return;
551         }
552         /* reorder */
553         for (p = tree->chunks.next; p != &tree->chunks; p = q) {
554                 struct node *node = list_entry(p, struct node, list);
555                 q = p->next;
556                 if (node->index & (1U<<31)) {
557                         list_del_init(p);
558                         list_add(p, &tree->chunks);
559                 }
560         }
561
562         while (!list_empty(&tree->chunks)) {
563                 struct node *node;
564
565                 node = list_entry(tree->chunks.next, struct node, list);
566
567                 /* have we run out of marked? */
568                 if (!(node->index & (1U<<31)))
569                         break;
570
571                 untag_chunk(node);
572         }
573         if (!tree->root && !tree->goner) {
574                 tree->goner = 1;
575                 spin_unlock(&hash_lock);
576                 mutex_lock(&audit_filter_mutex);
577                 kill_rules(tree);
578                 list_del_init(&tree->list);
579                 mutex_unlock(&audit_filter_mutex);
580                 prune_one(tree);
581         } else {
582                 spin_unlock(&hash_lock);
583         }
584 }
585
586 static void audit_schedule_prune(void);
587
588 /* called with audit_filter_mutex */
589 int audit_remove_tree_rule(struct audit_krule *rule)
590 {
591         struct audit_tree *tree;
592         tree = rule->tree;
593         if (tree) {
594                 spin_lock(&hash_lock);
595                 list_del_init(&rule->rlist);
596                 if (list_empty(&tree->rules) && !tree->goner) {
597                         tree->root = NULL;
598                         list_del_init(&tree->same_root);
599                         tree->goner = 1;
600                         list_move(&tree->list, &prune_list);
601                         rule->tree = NULL;
602                         spin_unlock(&hash_lock);
603                         audit_schedule_prune();
604                         return 1;
605                 }
606                 rule->tree = NULL;
607                 spin_unlock(&hash_lock);
608                 return 1;
609         }
610         return 0;
611 }
612
613 static int compare_root(struct vfsmount *mnt, void *arg)
614 {
615         return inode_to_key(d_backing_inode(mnt->mnt_root)) ==
616                (unsigned long)arg;
617 }
618
619 void audit_trim_trees(void)
620 {
621         struct list_head cursor;
622
623         mutex_lock(&audit_filter_mutex);
624         list_add(&cursor, &tree_list);
625         while (cursor.next != &tree_list) {
626                 struct audit_tree *tree;
627                 struct path path;
628                 struct vfsmount *root_mnt;
629                 struct node *node;
630                 int err;
631
632                 tree = container_of(cursor.next, struct audit_tree, list);
633                 get_tree(tree);
634                 list_del(&cursor);
635                 list_add(&cursor, &tree->list);
636                 mutex_unlock(&audit_filter_mutex);
637
638                 err = kern_path(tree->pathname, 0, &path);
639                 if (err)
640                         goto skip_it;
641
642                 root_mnt = collect_mounts(&path);
643                 path_put(&path);
644                 if (IS_ERR(root_mnt))
645                         goto skip_it;
646
647                 spin_lock(&hash_lock);
648                 list_for_each_entry(node, &tree->chunks, list) {
649                         struct audit_chunk *chunk = find_chunk(node);
650                         /* this could be NULL if the watch is dying else where... */
651                         node->index |= 1U<<31;
652                         if (iterate_mounts(compare_root,
653                                            (void *)(chunk->key),
654                                            root_mnt))
655                                 node->index &= ~(1U<<31);
656                 }
657                 spin_unlock(&hash_lock);
658                 trim_marked(tree);
659                 drop_collected_mounts(root_mnt);
660 skip_it:
661                 put_tree(tree);
662                 mutex_lock(&audit_filter_mutex);
663         }
664         list_del(&cursor);
665         mutex_unlock(&audit_filter_mutex);
666 }
667
668 int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
669 {
670
671         if (pathname[0] != '/' ||
672             rule->listnr != AUDIT_FILTER_EXIT ||
673             op != Audit_equal ||
674             rule->inode_f || rule->watch || rule->tree)
675                 return -EINVAL;
676         rule->tree = alloc_tree(pathname);
677         if (!rule->tree)
678                 return -ENOMEM;
679         return 0;
680 }
681
682 void audit_put_tree(struct audit_tree *tree)
683 {
684         put_tree(tree);
685 }
686
687 static int tag_mount(struct vfsmount *mnt, void *arg)
688 {
689         return tag_chunk(d_backing_inode(mnt->mnt_root), arg);
690 }
691
692 /*
693  * That gets run when evict_chunk() ends up needing to kill audit_tree.
694  * Runs from a separate thread.
695  */
696 static int prune_tree_thread(void *unused)
697 {
698         for (;;) {
699                 if (list_empty(&prune_list)) {
700                         set_current_state(TASK_INTERRUPTIBLE);
701                         schedule();
702                 }
703
704                 audit_ctl_lock();
705                 mutex_lock(&audit_filter_mutex);
706
707                 while (!list_empty(&prune_list)) {
708                         struct audit_tree *victim;
709
710                         victim = list_entry(prune_list.next,
711                                         struct audit_tree, list);
712                         list_del_init(&victim->list);
713
714                         mutex_unlock(&audit_filter_mutex);
715
716                         prune_one(victim);
717
718                         mutex_lock(&audit_filter_mutex);
719                 }
720
721                 mutex_unlock(&audit_filter_mutex);
722                 audit_ctl_unlock();
723         }
724         return 0;
725 }
726
727 static int audit_launch_prune(void)
728 {
729         if (prune_thread)
730                 return 0;
731         prune_thread = kthread_run(prune_tree_thread, NULL,
732                                 "audit_prune_tree");
733         if (IS_ERR(prune_thread)) {
734                 pr_err("cannot start thread audit_prune_tree");
735                 prune_thread = NULL;
736                 return -ENOMEM;
737         }
738         return 0;
739 }
740
741 /* called with audit_filter_mutex */
742 int audit_add_tree_rule(struct audit_krule *rule)
743 {
744         struct audit_tree *seed = rule->tree, *tree;
745         struct path path;
746         struct vfsmount *mnt;
747         int err;
748
749         rule->tree = NULL;
750         list_for_each_entry(tree, &tree_list, list) {
751                 if (!strcmp(seed->pathname, tree->pathname)) {
752                         put_tree(seed);
753                         rule->tree = tree;
754                         list_add(&rule->rlist, &tree->rules);
755                         return 0;
756                 }
757         }
758         tree = seed;
759         list_add(&tree->list, &tree_list);
760         list_add(&rule->rlist, &tree->rules);
761         /* do not set rule->tree yet */
762         mutex_unlock(&audit_filter_mutex);
763
764         if (unlikely(!prune_thread)) {
765                 err = audit_launch_prune();
766                 if (err)
767                         goto Err;
768         }
769
770         err = kern_path(tree->pathname, 0, &path);
771         if (err)
772                 goto Err;
773         mnt = collect_mounts(&path);
774         path_put(&path);
775         if (IS_ERR(mnt)) {
776                 err = PTR_ERR(mnt);
777                 goto Err;
778         }
779
780         get_tree(tree);
781         err = iterate_mounts(tag_mount, tree, mnt);
782         drop_collected_mounts(mnt);
783
784         if (!err) {
785                 struct node *node;
786                 spin_lock(&hash_lock);
787                 list_for_each_entry(node, &tree->chunks, list)
788                         node->index &= ~(1U<<31);
789                 spin_unlock(&hash_lock);
790         } else {
791                 trim_marked(tree);
792                 goto Err;
793         }
794
795         mutex_lock(&audit_filter_mutex);
796         if (list_empty(&rule->rlist)) {
797                 put_tree(tree);
798                 return -ENOENT;
799         }
800         rule->tree = tree;
801         put_tree(tree);
802
803         return 0;
804 Err:
805         mutex_lock(&audit_filter_mutex);
806         list_del_init(&tree->list);
807         list_del_init(&tree->rules);
808         put_tree(tree);
809         return err;
810 }
811
812 int audit_tag_tree(char *old, char *new)
813 {
814         struct list_head cursor, barrier;
815         int failed = 0;
816         struct path path1, path2;
817         struct vfsmount *tagged;
818         int err;
819
820         err = kern_path(new, 0, &path2);
821         if (err)
822                 return err;
823         tagged = collect_mounts(&path2);
824         path_put(&path2);
825         if (IS_ERR(tagged))
826                 return PTR_ERR(tagged);
827
828         err = kern_path(old, 0, &path1);
829         if (err) {
830                 drop_collected_mounts(tagged);
831                 return err;
832         }
833
834         mutex_lock(&audit_filter_mutex);
835         list_add(&barrier, &tree_list);
836         list_add(&cursor, &barrier);
837
838         while (cursor.next != &tree_list) {
839                 struct audit_tree *tree;
840                 int good_one = 0;
841
842                 tree = container_of(cursor.next, struct audit_tree, list);
843                 get_tree(tree);
844                 list_del(&cursor);
845                 list_add(&cursor, &tree->list);
846                 mutex_unlock(&audit_filter_mutex);
847
848                 err = kern_path(tree->pathname, 0, &path2);
849                 if (!err) {
850                         good_one = path_is_under(&path1, &path2);
851                         path_put(&path2);
852                 }
853
854                 if (!good_one) {
855                         put_tree(tree);
856                         mutex_lock(&audit_filter_mutex);
857                         continue;
858                 }
859
860                 failed = iterate_mounts(tag_mount, tree, tagged);
861                 if (failed) {
862                         put_tree(tree);
863                         mutex_lock(&audit_filter_mutex);
864                         break;
865                 }
866
867                 mutex_lock(&audit_filter_mutex);
868                 spin_lock(&hash_lock);
869                 if (!tree->goner) {
870                         list_del(&tree->list);
871                         list_add(&tree->list, &tree_list);
872                 }
873                 spin_unlock(&hash_lock);
874                 put_tree(tree);
875         }
876
877         while (barrier.prev != &tree_list) {
878                 struct audit_tree *tree;
879
880                 tree = container_of(barrier.prev, struct audit_tree, list);
881                 get_tree(tree);
882                 list_del(&tree->list);
883                 list_add(&tree->list, &barrier);
884                 mutex_unlock(&audit_filter_mutex);
885
886                 if (!failed) {
887                         struct node *node;
888                         spin_lock(&hash_lock);
889                         list_for_each_entry(node, &tree->chunks, list)
890                                 node->index &= ~(1U<<31);
891                         spin_unlock(&hash_lock);
892                 } else {
893                         trim_marked(tree);
894                 }
895
896                 put_tree(tree);
897                 mutex_lock(&audit_filter_mutex);
898         }
899         list_del(&barrier);
900         list_del(&cursor);
901         mutex_unlock(&audit_filter_mutex);
902         path_put(&path1);
903         drop_collected_mounts(tagged);
904         return failed;
905 }
906
907
908 static void audit_schedule_prune(void)
909 {
910         wake_up_process(prune_thread);
911 }
912
913 /*
914  * ... and that one is done if evict_chunk() decides to delay until the end
915  * of syscall.  Runs synchronously.
916  */
917 void audit_kill_trees(struct list_head *list)
918 {
919         audit_ctl_lock();
920         mutex_lock(&audit_filter_mutex);
921
922         while (!list_empty(list)) {
923                 struct audit_tree *victim;
924
925                 victim = list_entry(list->next, struct audit_tree, list);
926                 kill_rules(victim);
927                 list_del_init(&victim->list);
928
929                 mutex_unlock(&audit_filter_mutex);
930
931                 prune_one(victim);
932
933                 mutex_lock(&audit_filter_mutex);
934         }
935
936         mutex_unlock(&audit_filter_mutex);
937         audit_ctl_unlock();
938 }
939
940 /*
941  *  Here comes the stuff asynchronous to auditctl operations
942  */
943
944 static void evict_chunk(struct audit_chunk *chunk)
945 {
946         struct audit_tree *owner;
947         struct list_head *postponed = audit_killed_trees();
948         int need_prune = 0;
949         int n;
950
951         if (chunk->dead)
952                 return;
953
954         chunk->dead = 1;
955         mutex_lock(&audit_filter_mutex);
956         spin_lock(&hash_lock);
957         while (!list_empty(&chunk->trees)) {
958                 owner = list_entry(chunk->trees.next,
959                                    struct audit_tree, same_root);
960                 owner->goner = 1;
961                 owner->root = NULL;
962                 list_del_init(&owner->same_root);
963                 spin_unlock(&hash_lock);
964                 if (!postponed) {
965                         kill_rules(owner);
966                         list_move(&owner->list, &prune_list);
967                         need_prune = 1;
968                 } else {
969                         list_move(&owner->list, postponed);
970                 }
971                 spin_lock(&hash_lock);
972         }
973         list_del_rcu(&chunk->hash);
974         for (n = 0; n < chunk->count; n++)
975                 list_del_init(&chunk->owners[n].list);
976         spin_unlock(&hash_lock);
977         mutex_unlock(&audit_filter_mutex);
978         if (need_prune)
979                 audit_schedule_prune();
980 }
981
982 static int audit_tree_handle_event(struct fsnotify_group *group,
983                                    struct inode *to_tell,
984                                    u32 mask, const void *data, int data_type,
985                                    const unsigned char *file_name, u32 cookie,
986                                    struct fsnotify_iter_info *iter_info)
987 {
988         return 0;
989 }
990
991 static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
992 {
993         struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
994
995         evict_chunk(chunk);
996
997         /*
998          * We are guaranteed to have at least one reference to the mark from
999          * either the inode or the caller of fsnotify_destroy_mark().
1000          */
1001         BUG_ON(refcount_read(&entry->refcnt) < 1);
1002 }
1003
1004 static const struct fsnotify_ops audit_tree_ops = {
1005         .handle_event = audit_tree_handle_event,
1006         .freeing_mark = audit_tree_freeing_mark,
1007         .free_mark = audit_tree_destroy_watch,
1008 };
1009
1010 static int __init audit_tree_init(void)
1011 {
1012         int i;
1013
1014         audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
1015         if (IS_ERR(audit_tree_group))
1016                 audit_panic("cannot initialize fsnotify group for rectree watches");
1017
1018         for (i = 0; i < HASH_SIZE; i++)
1019                 INIT_LIST_HEAD(&chunk_hash_heads[i]);
1020
1021         return 0;
1022 }
1023 __initcall(audit_tree_init);