blob: d8f6cfa0005b49d99b5346dedd7b1af257f27989 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Viro74c3cbe2007-07-22 08:04:18 -04002#include "audit.h"
Eric Paris28a3a7e2009-12-17 20:12:05 -05003#include <linux/fsnotify_backend.h>
Al Viro74c3cbe2007-07-22 08:04:18 -04004#include <linux/namei.h>
5#include <linux/mount.h>
Al Viro916d7572009-06-24 00:02:38 -04006#include <linux/kthread.h>
Elena Reshetova9d2378f2017-05-02 10:16:04 -04007#include <linux/refcount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Al Viro74c3cbe2007-07-22 08:04:18 -04009
10struct audit_tree;
11struct audit_chunk;
12
13struct audit_tree {
Elena Reshetova9d2378f2017-05-02 10:16:04 -040014 refcount_t count;
Al Viro74c3cbe2007-07-22 08:04:18 -040015 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;
Jan Kara8d20d6e2018-11-12 09:54:48 -050027 unsigned long key;
Eric Parise61ce862009-12-17 21:24:24 -050028 struct fsnotify_mark mark;
Al Viro74c3cbe2007-07-22 08:04:18 -040029 struct list_head trees; /* with root here */
30 int dead;
31 int count;
Al Viro8f7b0ba2008-11-15 01:15:43 +000032 atomic_long_t refs;
Al Viro74c3cbe2007-07-22 08:04:18 -040033 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);
Imre Palikf1aaf262015-02-23 15:37:59 -050043static struct task_struct *prune_thread;
Al Viro74c3cbe2007-07-22 08:04:18 -040044
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 *
Eric Paris28a3a7e2009-12-17 20:12:05 -050066 * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount
Al Viro8f7b0ba2008-11-15 01:15:43 +000067 * of watch contributes 1 to .refs).
Al Viro74c3cbe2007-07-22 08:04:18 -040068 *
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
Eric Paris28a3a7e2009-12-17 20:12:05 -050075static struct fsnotify_group *audit_tree_group;
Al Viro74c3cbe2007-07-22 08:04:18 -040076
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) {
Elena Reshetova9d2378f2017-05-02 10:16:04 -040083 refcount_set(&tree->count, 1);
Al Viro74c3cbe2007-07-22 08:04:18 -040084 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{
Elena Reshetova9d2378f2017-05-02 10:16:04 -040097 refcount_inc(&tree->count);
Al Viro74c3cbe2007-07-22 08:04:18 -040098}
99
Al Viro74c3cbe2007-07-22 08:04:18 -0400100static inline void put_tree(struct audit_tree *tree)
101{
Elena Reshetova9d2378f2017-05-02 10:16:04 -0400102 if (refcount_dec_and_test(&tree->count))
Lai Jiangshan3b097c42011-03-15 18:03:53 +0800103 kfree_rcu(tree, head);
Al Viro74c3cbe2007-07-22 08:04:18 -0400104}
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
Al Viro8f7b0ba2008-11-15 01:15:43 +0000112static void free_chunk(struct audit_chunk *chunk)
Al Viro74c3cbe2007-07-22 08:04:18 -0400113{
Al Viro74c3cbe2007-07-22 08:04:18 -0400114 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
Al Viro74c3cbe2007-07-22 08:04:18 -0400123void audit_put_chunk(struct audit_chunk *chunk)
124{
Al Viro8f7b0ba2008-11-15 01:15:43 +0000125 if (atomic_long_dec_and_test(&chunk->refs))
126 free_chunk(chunk);
127}
128
129static 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);
Al Viro74c3cbe2007-07-22 08:04:18 -0400133}
134
Eric Parise61ce862009-12-17 21:24:24 -0500135static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
Eric Paris28a3a7e2009-12-17 20:12:05 -0500136{
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 }
Jan Kara054c6362016-12-21 18:06:12 +0100160 fsnotify_init_mark(&chunk->mark, audit_tree_group);
Miklos Szeredi799b6012014-11-04 11:27:12 +0100161 chunk->mark.mask = FS_IN_IGNORED;
Eric Paris28a3a7e2009-12-17 20:12:05 -0500162 return chunk;
163}
164
Al Viro74c3cbe2007-07-22 08:04:18 -0400165enum {HASH_SIZE = 128};
166static struct list_head chunk_hash_heads[HASH_SIZE];
167static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
168
Jan Karaf410ff62016-12-16 10:13:37 +0100169/* Function to return search key in our hash from inode. */
170static unsigned long inode_to_key(const struct inode *inode)
Al Viro74c3cbe2007-07-22 08:04:18 -0400171{
Amir Goldstein36f10f52018-06-23 17:54:49 +0300172 /* Use address pointed to by connector->obj as the key */
173 return (unsigned long)&inode->i_fsnotify_marks;
Jan Karaf410ff62016-12-16 10:13:37 +0100174}
175
Jan Karaf410ff62016-12-16 10:13:37 +0100176static inline struct list_head *chunk_hash(unsigned long key)
177{
178 unsigned long n = key / L1_CACHE_BYTES;
Al Viro74c3cbe2007-07-22 08:04:18 -0400179 return chunk_hash_heads + n % HASH_SIZE;
180}
181
Jan Kara9f16d2e2018-10-17 12:14:52 +0200182/* hash_lock & entry->group->mark_mutex is held by caller */
Al Viro74c3cbe2007-07-22 08:04:18 -0400183static void insert_hash(struct audit_chunk *chunk)
184{
Eric Paris28a3a7e2009-12-17 20:12:05 -0500185 struct list_head *list;
186
Jan Kara43471d12017-04-03 16:47:58 +0200187 if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED))
Eric Paris28a3a7e2009-12-17 20:12:05 -0500188 return;
Jan Kara1635e572018-11-12 09:54:48 -0500189 /*
190 * Make sure chunk is fully initialized before making it visible in the
191 * hash. Pairs with a data dependency barrier in READ_ONCE() in
192 * audit_tree_lookup().
193 */
194 smp_wmb();
Jan Kara8d20d6e2018-11-12 09:54:48 -0500195 WARN_ON_ONCE(!chunk->key);
196 list = chunk_hash(chunk->key);
Al Viro74c3cbe2007-07-22 08:04:18 -0400197 list_add_rcu(&chunk->hash, list);
198}
199
200/* called under rcu_read_lock */
201struct audit_chunk *audit_tree_lookup(const struct inode *inode)
202{
Jan Karaf410ff62016-12-16 10:13:37 +0100203 unsigned long key = inode_to_key(inode);
204 struct list_head *list = chunk_hash(key);
Paul E. McKenney6793a052008-05-14 17:10:12 -0700205 struct audit_chunk *p;
Al Viro74c3cbe2007-07-22 08:04:18 -0400206
Paul E. McKenney6793a052008-05-14 17:10:12 -0700207 list_for_each_entry_rcu(p, list, hash) {
Jan Kara1635e572018-11-12 09:54:48 -0500208 /*
209 * We use a data dependency barrier in READ_ONCE() to make sure
210 * the chunk we see is fully initialized.
211 */
212 if (READ_ONCE(p->key) == key) {
Al Viro8f7b0ba2008-11-15 01:15:43 +0000213 atomic_long_inc(&p->refs);
Al Viro74c3cbe2007-07-22 08:04:18 -0400214 return p;
215 }
216 }
217 return NULL;
218}
219
Yaowei Bai6f1b5d72015-11-04 08:23:51 -0500220bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
Al Viro74c3cbe2007-07-22 08:04:18 -0400221{
222 int n;
223 for (n = 0; n < chunk->count; n++)
224 if (chunk->owners[n].owner == tree)
Yaowei Bai6f1b5d72015-11-04 08:23:51 -0500225 return true;
226 return false;
Al Viro74c3cbe2007-07-22 08:04:18 -0400227}
228
229/* tagging and untagging inodes with trees */
230
Al Viro8f7b0ba2008-11-15 01:15:43 +0000231static struct audit_chunk *find_chunk(struct node *p)
Al Viro74c3cbe2007-07-22 08:04:18 -0400232{
Al Viro8f7b0ba2008-11-15 01:15:43 +0000233 int index = p->index & ~(1U<<31);
234 p -= index;
235 return container_of(p, struct audit_chunk, owners[0]);
236}
237
Jan Karad31b3262018-11-12 09:54:48 -0500238static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
239 struct node *skip)
240{
241 struct audit_tree *owner;
242 int i, j;
243
244 new->key = old->key;
245 list_splice_init(&old->trees, &new->trees);
246 list_for_each_entry(owner, &new->trees, same_root)
247 owner->root = new;
248 for (i = j = 0; j < old->count; i++, j++) {
249 if (&old->owners[j] == skip) {
250 i--;
251 continue;
252 }
253 owner = old->owners[j].owner;
254 new->owners[i].owner = owner;
255 new->owners[i].index = old->owners[j].index - j + i;
256 if (!owner) /* result of earlier fallback */
257 continue;
258 get_tree(owner);
259 list_replace_init(&old->owners[j].list, &new->owners[i].list);
260 }
261 /*
262 * Make sure chunk is fully initialized before making it visible in the
263 * hash. Pairs with a data dependency barrier in READ_ONCE() in
264 * audit_tree_lookup().
265 */
266 smp_wmb();
267 list_replace_rcu(&old->hash, &new->hash);
268}
269
Al Viro8f7b0ba2008-11-15 01:15:43 +0000270static void untag_chunk(struct node *p)
271{
272 struct audit_chunk *chunk = find_chunk(p);
Eric Parise61ce862009-12-17 21:24:24 -0500273 struct fsnotify_mark *entry = &chunk->mark;
Al Virof7a998a2010-10-30 02:18:32 -0400274 struct audit_chunk *new = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400275 struct audit_tree *owner;
276 int size = chunk->count - 1;
Al Viro74c3cbe2007-07-22 08:04:18 -0400277
Eric Paris28a3a7e2009-12-17 20:12:05 -0500278 fsnotify_get_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000279
280 spin_unlock(&hash_lock);
281
Al Virof7a998a2010-10-30 02:18:32 -0400282 if (size)
283 new = alloc_chunk(size);
284
Jan Karabe29d202016-12-14 14:40:05 +0100285 mutex_lock(&entry->group->mark_mutex);
Jan Kara6b3f05d2016-12-21 12:15:30 +0100286 /*
287 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300288 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100289 */
Jan Kara43471d12017-04-03 16:47:58 +0200290 if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Jan Karabe29d202016-12-14 14:40:05 +0100291 mutex_unlock(&entry->group->mark_mutex);
Al Virof7a998a2010-10-30 02:18:32 -0400292 if (new)
Jan Kara7b1293232016-12-21 18:32:48 +0100293 fsnotify_put_mark(&new->mark);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000294 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400295 }
296
297 owner = p->owner;
298
299 if (!size) {
300 chunk->dead = 1;
301 spin_lock(&hash_lock);
302 list_del_init(&chunk->trees);
303 if (owner->root == chunk)
304 owner->root = NULL;
305 list_del_init(&p->list);
306 list_del_rcu(&chunk->hash);
307 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500308 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100309 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500310 fsnotify_free_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000311 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400312 }
313
Al Viro74c3cbe2007-07-22 08:04:18 -0400314 if (!new)
315 goto Fallback;
Al Virof7a998a2010-10-30 02:18:32 -0400316
Amir Goldstein36f10f52018-06-23 17:54:49 +0300317 if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
318 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200319 fsnotify_put_mark(&new->mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400320 goto Fallback;
321 }
322
323 chunk->dead = 1;
324 spin_lock(&hash_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -0400325 if (owner->root == chunk) {
326 list_del_init(&owner->same_root);
327 owner->root = NULL;
328 }
Jan Karad31b3262018-11-12 09:54:48 -0500329 list_del_init(&p->list);
Jan Kara1635e572018-11-12 09:54:48 -0500330 /*
Jan Karad31b3262018-11-12 09:54:48 -0500331 * This has to go last when updating chunk as once replace_chunk() is
332 * called, new RCU readers can see the new chunk.
Jan Kara1635e572018-11-12 09:54:48 -0500333 */
Jan Karad31b3262018-11-12 09:54:48 -0500334 replace_chunk(new, chunk, p);
Al Viro74c3cbe2007-07-22 08:04:18 -0400335 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500336 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100337 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500338 fsnotify_free_mark(entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200339 fsnotify_put_mark(&new->mark); /* drop initial reference */
Al Viro8f7b0ba2008-11-15 01:15:43 +0000340 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400341
342Fallback:
343 // do the best we can
344 spin_lock(&hash_lock);
345 if (owner->root == chunk) {
346 list_del_init(&owner->same_root);
347 owner->root = NULL;
348 }
349 list_del_init(&p->list);
350 p->owner = NULL;
351 put_tree(owner);
352 spin_unlock(&hash_lock);
Jan Karabe29d202016-12-14 14:40:05 +0100353 mutex_unlock(&entry->group->mark_mutex);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000354out:
Eric Paris28a3a7e2009-12-17 20:12:05 -0500355 fsnotify_put_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000356 spin_lock(&hash_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -0400357}
358
Jan Karaa5789b02018-11-12 09:54:48 -0500359/* Call with group->mark_mutex held, releases it */
Al Viro74c3cbe2007-07-22 08:04:18 -0400360static int create_chunk(struct inode *inode, struct audit_tree *tree)
361{
Eric Parise61ce862009-12-17 21:24:24 -0500362 struct fsnotify_mark *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400363 struct audit_chunk *chunk = alloc_chunk(1);
Jan Karaa5789b02018-11-12 09:54:48 -0500364
365 if (!chunk) {
366 mutex_unlock(&audit_tree_group->mark_mutex);
Al Viro74c3cbe2007-07-22 08:04:18 -0400367 return -ENOMEM;
Jan Karaa5789b02018-11-12 09:54:48 -0500368 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400369
Eric Paris28a3a7e2009-12-17 20:12:05 -0500370 entry = &chunk->mark;
Jan Karaa5789b02018-11-12 09:54:48 -0500371 if (fsnotify_add_inode_mark_locked(entry, inode, 0)) {
372 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200373 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400374 return -ENOSPC;
375 }
376
Al Viro74c3cbe2007-07-22 08:04:18 -0400377 spin_lock(&hash_lock);
378 if (tree->goner) {
379 spin_unlock(&hash_lock);
380 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500381 fsnotify_detach_mark(entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500382 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500383 fsnotify_free_mark(entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500384 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400385 return 0;
386 }
387 chunk->owners[0].index = (1U << 31);
388 chunk->owners[0].owner = tree;
389 get_tree(tree);
390 list_add(&chunk->owners[0].list, &tree->chunks);
391 if (!tree->root) {
392 tree->root = chunk;
393 list_add(&tree->same_root, &chunk->trees);
394 }
Jan Kara8d20d6e2018-11-12 09:54:48 -0500395 chunk->key = inode_to_key(inode);
Jan Kara1635e572018-11-12 09:54:48 -0500396 /*
397 * Inserting into the hash table has to go last as once we do that RCU
398 * readers can see the chunk.
399 */
Al Viro74c3cbe2007-07-22 08:04:18 -0400400 insert_hash(chunk);
401 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500402 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200403 fsnotify_put_mark(entry); /* drop initial reference */
Al Viro74c3cbe2007-07-22 08:04:18 -0400404 return 0;
405}
406
407/* the first tagged inode becomes root of tree */
408static int tag_chunk(struct inode *inode, struct audit_tree *tree)
409{
Eric Parise61ce862009-12-17 21:24:24 -0500410 struct fsnotify_mark *old_entry, *chunk_entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400411 struct audit_chunk *chunk, *old;
412 struct node *p;
413 int n;
414
Jan Karaa5789b02018-11-12 09:54:48 -0500415 mutex_lock(&audit_tree_group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100416 old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks,
417 audit_tree_group);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500418 if (!old_entry)
Al Viro74c3cbe2007-07-22 08:04:18 -0400419 return create_chunk(inode, tree);
420
Eric Paris28a3a7e2009-12-17 20:12:05 -0500421 old = container_of(old_entry, struct audit_chunk, mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400422
423 /* are we already there? */
424 spin_lock(&hash_lock);
425 for (n = 0; n < old->count; n++) {
426 if (old->owners[n].owner == tree) {
427 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500428 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500429 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400430 return 0;
431 }
432 }
433 spin_unlock(&hash_lock);
434
435 chunk = alloc_chunk(old->count + 1);
Al Virob4c30aa2009-12-19 16:03:30 +0000436 if (!chunk) {
Jan Karaa5789b02018-11-12 09:54:48 -0500437 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500438 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400439 return -ENOMEM;
Al Virob4c30aa2009-12-19 16:03:30 +0000440 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400441
Eric Paris28a3a7e2009-12-17 20:12:05 -0500442 chunk_entry = &chunk->mark;
443
Jan Kara6b3f05d2016-12-21 12:15:30 +0100444 /*
445 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300446 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100447 */
Jan Kara43471d12017-04-03 16:47:58 +0200448 if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500449 /* old_entry is being shot, lets just lie */
Jan Karaa5789b02018-11-12 09:54:48 -0500450 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500451 fsnotify_put_mark(old_entry);
Jan Kara7b1293232016-12-21 18:32:48 +0100452 fsnotify_put_mark(&chunk->mark);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500453 return -ENOENT;
454 }
455
Amir Goldstein36f10f52018-06-23 17:54:49 +0300456 if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
457 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Jan Karaa5789b02018-11-12 09:54:48 -0500458 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200459 fsnotify_put_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500460 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400461 return -ENOSPC;
462 }
Eric Paris28a3a7e2009-12-17 20:12:05 -0500463
Al Viro74c3cbe2007-07-22 08:04:18 -0400464 spin_lock(&hash_lock);
465 if (tree->goner) {
466 spin_unlock(&hash_lock);
467 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500468 fsnotify_detach_mark(chunk_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500469 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500470 fsnotify_free_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500471 fsnotify_put_mark(chunk_entry);
472 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400473 return 0;
474 }
Jan Karad31b3262018-11-12 09:54:48 -0500475 p = &chunk->owners[chunk->count - 1];
Al Viro74c3cbe2007-07-22 08:04:18 -0400476 p->index = (chunk->count - 1) | (1U<<31);
477 p->owner = tree;
478 get_tree(tree);
479 list_add(&p->list, &tree->chunks);
Al Viro74c3cbe2007-07-22 08:04:18 -0400480 old->dead = 1;
481 if (!tree->root) {
482 tree->root = chunk;
483 list_add(&tree->same_root, &chunk->trees);
484 }
Jan Kara1635e572018-11-12 09:54:48 -0500485 /*
Jan Karad31b3262018-11-12 09:54:48 -0500486 * This has to go last when updating chunk as once replace_chunk() is
487 * called, new RCU readers can see the new chunk.
Jan Kara1635e572018-11-12 09:54:48 -0500488 */
Jan Karad31b3262018-11-12 09:54:48 -0500489 replace_chunk(chunk, old, NULL);
Al Viro74c3cbe2007-07-22 08:04:18 -0400490 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500491 fsnotify_detach_mark(old_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500492 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500493 fsnotify_free_mark(old_entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200494 fsnotify_put_mark(chunk_entry); /* drop initial reference */
Eric Paris28a3a7e2009-12-17 20:12:05 -0500495 fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
Al Viro74c3cbe2007-07-22 08:04:18 -0400496 return 0;
497}
498
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400499static void audit_tree_log_remove_rule(struct audit_krule *rule)
Kees Cook0644ec02013-01-11 14:32:07 -0800500{
501 struct audit_buffer *ab;
502
Richard Guy Briggs65a87662018-06-14 16:20:05 -0400503 if (!audit_enabled)
504 return;
Kees Cook0644ec02013-01-11 14:32:07 -0800505 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
506 if (unlikely(!ab))
507 return;
Steve Grubbc1e8f062016-11-16 16:14:33 -0500508 audit_log_format(ab, "op=remove_rule");
Kees Cook0644ec02013-01-11 14:32:07 -0800509 audit_log_format(ab, " dir=");
510 audit_log_untrustedstring(ab, rule->tree->pathname);
511 audit_log_key(ab, rule->filterkey);
512 audit_log_format(ab, " list=%d res=1", rule->listnr);
513 audit_log_end(ab);
514}
515
Al Viro74c3cbe2007-07-22 08:04:18 -0400516static void kill_rules(struct audit_tree *tree)
517{
518 struct audit_krule *rule, *next;
519 struct audit_entry *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400520
521 list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
522 entry = container_of(rule, struct audit_entry, rule);
523
524 list_del_init(&rule->rlist);
525 if (rule->tree) {
526 /* not a half-baked one */
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400527 audit_tree_log_remove_rule(rule);
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400528 if (entry->rule.exe)
529 audit_remove_mark(entry->rule.exe);
Al Viro74c3cbe2007-07-22 08:04:18 -0400530 rule->tree = NULL;
531 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -0500532 list_del(&entry->rule.list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400533 call_rcu(&entry->rcu, audit_free_rule_rcu);
534 }
535 }
536}
537
538/*
539 * finish killing struct audit_tree
540 */
541static void prune_one(struct audit_tree *victim)
542{
543 spin_lock(&hash_lock);
544 while (!list_empty(&victim->chunks)) {
545 struct node *p;
Al Viro74c3cbe2007-07-22 08:04:18 -0400546
547 p = list_entry(victim->chunks.next, struct node, list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400548
Al Viro8f7b0ba2008-11-15 01:15:43 +0000549 untag_chunk(p);
Al Viro74c3cbe2007-07-22 08:04:18 -0400550 }
551 spin_unlock(&hash_lock);
552 put_tree(victim);
553}
554
555/* trim the uncommitted chunks from tree */
556
557static void trim_marked(struct audit_tree *tree)
558{
559 struct list_head *p, *q;
560 spin_lock(&hash_lock);
561 if (tree->goner) {
562 spin_unlock(&hash_lock);
563 return;
564 }
565 /* reorder */
566 for (p = tree->chunks.next; p != &tree->chunks; p = q) {
567 struct node *node = list_entry(p, struct node, list);
568 q = p->next;
569 if (node->index & (1U<<31)) {
570 list_del_init(p);
571 list_add(p, &tree->chunks);
572 }
573 }
574
575 while (!list_empty(&tree->chunks)) {
576 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400577
578 node = list_entry(tree->chunks.next, struct node, list);
579
580 /* have we run out of marked? */
581 if (!(node->index & (1U<<31)))
582 break;
583
Al Viro8f7b0ba2008-11-15 01:15:43 +0000584 untag_chunk(node);
Al Viro74c3cbe2007-07-22 08:04:18 -0400585 }
586 if (!tree->root && !tree->goner) {
587 tree->goner = 1;
588 spin_unlock(&hash_lock);
589 mutex_lock(&audit_filter_mutex);
590 kill_rules(tree);
591 list_del_init(&tree->list);
592 mutex_unlock(&audit_filter_mutex);
593 prune_one(tree);
594 } else {
595 spin_unlock(&hash_lock);
596 }
597}
598
Al Viro916d7572009-06-24 00:02:38 -0400599static void audit_schedule_prune(void);
600
Al Viro74c3cbe2007-07-22 08:04:18 -0400601/* called with audit_filter_mutex */
602int audit_remove_tree_rule(struct audit_krule *rule)
603{
604 struct audit_tree *tree;
605 tree = rule->tree;
606 if (tree) {
607 spin_lock(&hash_lock);
608 list_del_init(&rule->rlist);
609 if (list_empty(&tree->rules) && !tree->goner) {
610 tree->root = NULL;
611 list_del_init(&tree->same_root);
612 tree->goner = 1;
613 list_move(&tree->list, &prune_list);
614 rule->tree = NULL;
615 spin_unlock(&hash_lock);
616 audit_schedule_prune();
617 return 1;
618 }
619 rule->tree = NULL;
620 spin_unlock(&hash_lock);
621 return 1;
622 }
623 return 0;
624}
625
Al Viro1f707132010-01-30 22:51:25 -0500626static int compare_root(struct vfsmount *mnt, void *arg)
627{
Jan Karaf410ff62016-12-16 10:13:37 +0100628 return inode_to_key(d_backing_inode(mnt->mnt_root)) ==
629 (unsigned long)arg;
Al Viro1f707132010-01-30 22:51:25 -0500630}
631
Al Viro74c3cbe2007-07-22 08:04:18 -0400632void audit_trim_trees(void)
633{
634 struct list_head cursor;
635
636 mutex_lock(&audit_filter_mutex);
637 list_add(&cursor, &tree_list);
638 while (cursor.next != &tree_list) {
639 struct audit_tree *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400640 struct path path;
Al Viro74c3cbe2007-07-22 08:04:18 -0400641 struct vfsmount *root_mnt;
642 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400643 int err;
644
645 tree = container_of(cursor.next, struct audit_tree, list);
646 get_tree(tree);
647 list_del(&cursor);
648 list_add(&cursor, &tree->list);
649 mutex_unlock(&audit_filter_mutex);
650
Al Viro98bc9932008-08-02 01:06:21 -0400651 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400652 if (err)
653 goto skip_it;
654
Al Viro589ff872009-04-18 03:28:19 -0400655 root_mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400656 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100657 if (IS_ERR(root_mnt))
Al Viro74c3cbe2007-07-22 08:04:18 -0400658 goto skip_it;
659
Al Viro74c3cbe2007-07-22 08:04:18 -0400660 spin_lock(&hash_lock);
661 list_for_each_entry(node, &tree->chunks, list) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500662 struct audit_chunk *chunk = find_chunk(node);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300663 /* this could be NULL if the watch is dying else where... */
Al Viro74c3cbe2007-07-22 08:04:18 -0400664 node->index |= 1U<<31;
Jan Karaf410ff62016-12-16 10:13:37 +0100665 if (iterate_mounts(compare_root,
Jan Kara8d20d6e2018-11-12 09:54:48 -0500666 (void *)(chunk->key),
Jan Karaf410ff62016-12-16 10:13:37 +0100667 root_mnt))
Al Viro1f707132010-01-30 22:51:25 -0500668 node->index &= ~(1U<<31);
Al Viro74c3cbe2007-07-22 08:04:18 -0400669 }
670 spin_unlock(&hash_lock);
671 trim_marked(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400672 drop_collected_mounts(root_mnt);
673skip_it:
Chen Gang12b2f112013-04-29 15:05:19 -0700674 put_tree(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400675 mutex_lock(&audit_filter_mutex);
676 }
677 list_del(&cursor);
678 mutex_unlock(&audit_filter_mutex);
679}
680
Al Viro74c3cbe2007-07-22 08:04:18 -0400681int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
682{
683
684 if (pathname[0] != '/' ||
685 rule->listnr != AUDIT_FILTER_EXIT ||
Al Viro5af75d82008-12-16 05:59:26 -0500686 op != Audit_equal ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400687 rule->inode_f || rule->watch || rule->tree)
688 return -EINVAL;
689 rule->tree = alloc_tree(pathname);
690 if (!rule->tree)
691 return -ENOMEM;
692 return 0;
693}
694
695void audit_put_tree(struct audit_tree *tree)
696{
697 put_tree(tree);
698}
699
Al Viro1f707132010-01-30 22:51:25 -0500700static int tag_mount(struct vfsmount *mnt, void *arg)
701{
David Howells3b362152015-03-17 22:26:21 +0000702 return tag_chunk(d_backing_inode(mnt->mnt_root), arg);
Al Viro1f707132010-01-30 22:51:25 -0500703}
704
Imre Palikf1aaf262015-02-23 15:37:59 -0500705/*
706 * That gets run when evict_chunk() ends up needing to kill audit_tree.
707 * Runs from a separate thread.
708 */
709static int prune_tree_thread(void *unused)
710{
711 for (;;) {
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200712 if (list_empty(&prune_list)) {
713 set_current_state(TASK_INTERRUPTIBLE);
Imre Palikf1aaf262015-02-23 15:37:59 -0500714 schedule();
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200715 }
Imre Palikf1aaf262015-02-23 15:37:59 -0500716
Paul Moorece423632018-02-20 09:52:38 -0500717 audit_ctl_lock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500718 mutex_lock(&audit_filter_mutex);
719
720 while (!list_empty(&prune_list)) {
721 struct audit_tree *victim;
722
723 victim = list_entry(prune_list.next,
724 struct audit_tree, list);
725 list_del_init(&victim->list);
726
727 mutex_unlock(&audit_filter_mutex);
728
729 prune_one(victim);
730
731 mutex_lock(&audit_filter_mutex);
732 }
733
734 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500735 audit_ctl_unlock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500736 }
737 return 0;
738}
739
740static int audit_launch_prune(void)
741{
742 if (prune_thread)
743 return 0;
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200744 prune_thread = kthread_run(prune_tree_thread, NULL,
Imre Palikf1aaf262015-02-23 15:37:59 -0500745 "audit_prune_tree");
746 if (IS_ERR(prune_thread)) {
747 pr_err("cannot start thread audit_prune_tree");
748 prune_thread = NULL;
749 return -ENOMEM;
Imre Palikf1aaf262015-02-23 15:37:59 -0500750 }
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200751 return 0;
Imre Palikf1aaf262015-02-23 15:37:59 -0500752}
753
Al Viro74c3cbe2007-07-22 08:04:18 -0400754/* called with audit_filter_mutex */
755int audit_add_tree_rule(struct audit_krule *rule)
756{
757 struct audit_tree *seed = rule->tree, *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400758 struct path path;
Al Viro1f707132010-01-30 22:51:25 -0500759 struct vfsmount *mnt;
Al Viro74c3cbe2007-07-22 08:04:18 -0400760 int err;
761
Chen Gang736f3202013-06-12 14:05:07 -0700762 rule->tree = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400763 list_for_each_entry(tree, &tree_list, list) {
764 if (!strcmp(seed->pathname, tree->pathname)) {
765 put_tree(seed);
766 rule->tree = tree;
767 list_add(&rule->rlist, &tree->rules);
768 return 0;
769 }
770 }
771 tree = seed;
772 list_add(&tree->list, &tree_list);
773 list_add(&rule->rlist, &tree->rules);
774 /* do not set rule->tree yet */
775 mutex_unlock(&audit_filter_mutex);
776
Imre Palikf1aaf262015-02-23 15:37:59 -0500777 if (unlikely(!prune_thread)) {
778 err = audit_launch_prune();
779 if (err)
780 goto Err;
781 }
782
Al Viro98bc9932008-08-02 01:06:21 -0400783 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400784 if (err)
785 goto Err;
Al Viro589ff872009-04-18 03:28:19 -0400786 mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400787 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100788 if (IS_ERR(mnt)) {
789 err = PTR_ERR(mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400790 goto Err;
791 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400792
793 get_tree(tree);
Al Viro1f707132010-01-30 22:51:25 -0500794 err = iterate_mounts(tag_mount, tree, mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400795 drop_collected_mounts(mnt);
796
797 if (!err) {
798 struct node *node;
799 spin_lock(&hash_lock);
800 list_for_each_entry(node, &tree->chunks, list)
801 node->index &= ~(1U<<31);
802 spin_unlock(&hash_lock);
803 } else {
804 trim_marked(tree);
805 goto Err;
806 }
807
808 mutex_lock(&audit_filter_mutex);
809 if (list_empty(&rule->rlist)) {
810 put_tree(tree);
811 return -ENOENT;
812 }
813 rule->tree = tree;
814 put_tree(tree);
815
816 return 0;
817Err:
818 mutex_lock(&audit_filter_mutex);
819 list_del_init(&tree->list);
820 list_del_init(&tree->rules);
821 put_tree(tree);
822 return err;
823}
824
825int audit_tag_tree(char *old, char *new)
826{
827 struct list_head cursor, barrier;
828 int failed = 0;
Al Viro2096f752010-01-30 13:16:21 -0500829 struct path path1, path2;
Al Viro74c3cbe2007-07-22 08:04:18 -0400830 struct vfsmount *tagged;
Al Viro74c3cbe2007-07-22 08:04:18 -0400831 int err;
832
Al Viro2096f752010-01-30 13:16:21 -0500833 err = kern_path(new, 0, &path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400834 if (err)
835 return err;
Al Viro2096f752010-01-30 13:16:21 -0500836 tagged = collect_mounts(&path2);
837 path_put(&path2);
David Howellsbe34d1a2012-06-25 12:55:18 +0100838 if (IS_ERR(tagged))
839 return PTR_ERR(tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400840
Al Viro2096f752010-01-30 13:16:21 -0500841 err = kern_path(old, 0, &path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400842 if (err) {
843 drop_collected_mounts(tagged);
844 return err;
845 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400846
Al Viro74c3cbe2007-07-22 08:04:18 -0400847 mutex_lock(&audit_filter_mutex);
848 list_add(&barrier, &tree_list);
849 list_add(&cursor, &barrier);
850
851 while (cursor.next != &tree_list) {
852 struct audit_tree *tree;
Al Viro2096f752010-01-30 13:16:21 -0500853 int good_one = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400854
855 tree = container_of(cursor.next, struct audit_tree, list);
856 get_tree(tree);
857 list_del(&cursor);
858 list_add(&cursor, &tree->list);
859 mutex_unlock(&audit_filter_mutex);
860
Al Viro2096f752010-01-30 13:16:21 -0500861 err = kern_path(tree->pathname, 0, &path2);
862 if (!err) {
863 good_one = path_is_under(&path1, &path2);
864 path_put(&path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400865 }
866
Al Viro2096f752010-01-30 13:16:21 -0500867 if (!good_one) {
Al Viro74c3cbe2007-07-22 08:04:18 -0400868 put_tree(tree);
869 mutex_lock(&audit_filter_mutex);
870 continue;
871 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400872
Al Viro1f707132010-01-30 22:51:25 -0500873 failed = iterate_mounts(tag_mount, tree, tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400874 if (failed) {
875 put_tree(tree);
876 mutex_lock(&audit_filter_mutex);
877 break;
878 }
879
880 mutex_lock(&audit_filter_mutex);
881 spin_lock(&hash_lock);
882 if (!tree->goner) {
883 list_del(&tree->list);
884 list_add(&tree->list, &tree_list);
885 }
886 spin_unlock(&hash_lock);
887 put_tree(tree);
888 }
889
890 while (barrier.prev != &tree_list) {
891 struct audit_tree *tree;
892
893 tree = container_of(barrier.prev, struct audit_tree, list);
894 get_tree(tree);
895 list_del(&tree->list);
896 list_add(&tree->list, &barrier);
897 mutex_unlock(&audit_filter_mutex);
898
899 if (!failed) {
900 struct node *node;
901 spin_lock(&hash_lock);
902 list_for_each_entry(node, &tree->chunks, list)
903 node->index &= ~(1U<<31);
904 spin_unlock(&hash_lock);
905 } else {
906 trim_marked(tree);
907 }
908
909 put_tree(tree);
910 mutex_lock(&audit_filter_mutex);
911 }
912 list_del(&barrier);
913 list_del(&cursor);
Al Viro74c3cbe2007-07-22 08:04:18 -0400914 mutex_unlock(&audit_filter_mutex);
Al Viro2096f752010-01-30 13:16:21 -0500915 path_put(&path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400916 drop_collected_mounts(tagged);
917 return failed;
918}
919
Al Viro916d7572009-06-24 00:02:38 -0400920
921static void audit_schedule_prune(void)
922{
Imre Palikf1aaf262015-02-23 15:37:59 -0500923 wake_up_process(prune_thread);
Al Viro916d7572009-06-24 00:02:38 -0400924}
925
926/*
927 * ... and that one is done if evict_chunk() decides to delay until the end
928 * of syscall. Runs synchronously.
929 */
930void audit_kill_trees(struct list_head *list)
931{
Paul Moorece423632018-02-20 09:52:38 -0500932 audit_ctl_lock();
Al Viro916d7572009-06-24 00:02:38 -0400933 mutex_lock(&audit_filter_mutex);
934
935 while (!list_empty(list)) {
936 struct audit_tree *victim;
937
938 victim = list_entry(list->next, struct audit_tree, list);
939 kill_rules(victim);
940 list_del_init(&victim->list);
941
942 mutex_unlock(&audit_filter_mutex);
943
944 prune_one(victim);
945
946 mutex_lock(&audit_filter_mutex);
947 }
948
949 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500950 audit_ctl_unlock();
Al Viro74c3cbe2007-07-22 08:04:18 -0400951}
952
953/*
954 * Here comes the stuff asynchronous to auditctl operations
955 */
956
Al Viro74c3cbe2007-07-22 08:04:18 -0400957static void evict_chunk(struct audit_chunk *chunk)
958{
959 struct audit_tree *owner;
Al Viro916d7572009-06-24 00:02:38 -0400960 struct list_head *postponed = audit_killed_trees();
961 int need_prune = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400962 int n;
963
964 if (chunk->dead)
965 return;
966
967 chunk->dead = 1;
968 mutex_lock(&audit_filter_mutex);
969 spin_lock(&hash_lock);
970 while (!list_empty(&chunk->trees)) {
971 owner = list_entry(chunk->trees.next,
972 struct audit_tree, same_root);
973 owner->goner = 1;
974 owner->root = NULL;
975 list_del_init(&owner->same_root);
976 spin_unlock(&hash_lock);
Al Viro916d7572009-06-24 00:02:38 -0400977 if (!postponed) {
978 kill_rules(owner);
979 list_move(&owner->list, &prune_list);
980 need_prune = 1;
981 } else {
982 list_move(&owner->list, postponed);
983 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400984 spin_lock(&hash_lock);
985 }
986 list_del_rcu(&chunk->hash);
987 for (n = 0; n < chunk->count; n++)
988 list_del_init(&chunk->owners[n].list);
989 spin_unlock(&hash_lock);
Imre Palikf1aaf262015-02-23 15:37:59 -0500990 mutex_unlock(&audit_filter_mutex);
Al Viro916d7572009-06-24 00:02:38 -0400991 if (need_prune)
992 audit_schedule_prune();
Al Viro74c3cbe2007-07-22 08:04:18 -0400993}
994
Eric Paris3a9b16b2010-07-28 10:18:38 -0400995static int audit_tree_handle_event(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -0800996 struct inode *to_tell,
Al Viro3cd5eca2016-11-20 20:19:09 -0500997 u32 mask, const void *data, int data_type,
Jan Kara9385a842016-11-10 17:51:50 +0100998 const unsigned char *file_name, u32 cookie,
999 struct fsnotify_iter_info *iter_info)
Al Viro74c3cbe2007-07-22 08:04:18 -04001000{
Jan Kara83c4c4b2014-01-21 15:48:15 -08001001 return 0;
Al Viro74c3cbe2007-07-22 08:04:18 -04001002}
1003
Eric Parise61ce862009-12-17 21:24:24 -05001004static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
Al Viro74c3cbe2007-07-22 08:04:18 -04001005{
Eric Paris28a3a7e2009-12-17 20:12:05 -05001006 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
1007
1008 evict_chunk(chunk);
Miklos Szeredib3e86922012-08-15 12:55:22 +02001009
1010 /*
1011 * We are guaranteed to have at least one reference to the mark from
1012 * either the inode or the caller of fsnotify_destroy_mark().
1013 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +03001014 BUG_ON(refcount_read(&entry->refcnt) < 1);
Al Viro74c3cbe2007-07-22 08:04:18 -04001015}
1016
Eric Paris28a3a7e2009-12-17 20:12:05 -05001017static const struct fsnotify_ops audit_tree_ops = {
1018 .handle_event = audit_tree_handle_event,
Eric Paris28a3a7e2009-12-17 20:12:05 -05001019 .freeing_mark = audit_tree_freeing_mark,
Jan Kara054c6362016-12-21 18:06:12 +01001020 .free_mark = audit_tree_destroy_watch,
Al Viro74c3cbe2007-07-22 08:04:18 -04001021};
1022
1023static int __init audit_tree_init(void)
1024{
1025 int i;
1026
Eric Paris0d2e2a12009-12-17 21:24:22 -05001027 audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
Eric Paris28a3a7e2009-12-17 20:12:05 -05001028 if (IS_ERR(audit_tree_group))
1029 audit_panic("cannot initialize fsnotify group for rectree watches");
Al Viro74c3cbe2007-07-22 08:04:18 -04001030
1031 for (i = 0; i < HASH_SIZE; i++)
1032 INIT_LIST_HEAD(&chunk_hash_heads[i]);
1033
1034 return 0;
1035}
1036__initcall(audit_tree_init);