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