blob: 307749d6773c1471e186ce024555cc18391552ee [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
238static void untag_chunk(struct node *p)
239{
240 struct audit_chunk *chunk = find_chunk(p);
Eric Parise61ce862009-12-17 21:24:24 -0500241 struct fsnotify_mark *entry = &chunk->mark;
Al Virof7a998a2010-10-30 02:18:32 -0400242 struct audit_chunk *new = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400243 struct audit_tree *owner;
244 int size = chunk->count - 1;
245 int i, j;
246
Eric Paris28a3a7e2009-12-17 20:12:05 -0500247 fsnotify_get_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000248
249 spin_unlock(&hash_lock);
250
Al Virof7a998a2010-10-30 02:18:32 -0400251 if (size)
252 new = alloc_chunk(size);
253
Jan Karabe29d202016-12-14 14:40:05 +0100254 mutex_lock(&entry->group->mark_mutex);
Jan Kara6b3f05d2016-12-21 12:15:30 +0100255 /*
256 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300257 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100258 */
Jan Kara43471d12017-04-03 16:47:58 +0200259 if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Jan Karabe29d202016-12-14 14:40:05 +0100260 mutex_unlock(&entry->group->mark_mutex);
Al Virof7a998a2010-10-30 02:18:32 -0400261 if (new)
Jan Kara7b1293232016-12-21 18:32:48 +0100262 fsnotify_put_mark(&new->mark);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000263 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400264 }
265
266 owner = p->owner;
267
268 if (!size) {
269 chunk->dead = 1;
270 spin_lock(&hash_lock);
271 list_del_init(&chunk->trees);
272 if (owner->root == chunk)
273 owner->root = NULL;
274 list_del_init(&p->list);
275 list_del_rcu(&chunk->hash);
276 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500277 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100278 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500279 fsnotify_free_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000280 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400281 }
282
Al Viro74c3cbe2007-07-22 08:04:18 -0400283 if (!new)
284 goto Fallback;
Al Virof7a998a2010-10-30 02:18:32 -0400285
Amir Goldstein36f10f52018-06-23 17:54:49 +0300286 if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
287 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200288 fsnotify_put_mark(&new->mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400289 goto Fallback;
290 }
291
292 chunk->dead = 1;
293 spin_lock(&hash_lock);
Jan Kara8d20d6e2018-11-12 09:54:48 -0500294 new->key = chunk->key;
Al Viro74c3cbe2007-07-22 08:04:18 -0400295 list_replace_init(&chunk->trees, &new->trees);
296 if (owner->root == chunk) {
297 list_del_init(&owner->same_root);
298 owner->root = NULL;
299 }
300
Al Viro6f5d5112009-12-19 15:59:45 +0000301 for (i = j = 0; j <= size; i++, j++) {
Al Viro74c3cbe2007-07-22 08:04:18 -0400302 struct audit_tree *s;
303 if (&chunk->owners[j] == p) {
304 list_del_init(&p->list);
305 i--;
306 continue;
307 }
308 s = chunk->owners[j].owner;
309 new->owners[i].owner = s;
310 new->owners[i].index = chunk->owners[j].index - j + i;
311 if (!s) /* result of earlier fallback */
312 continue;
313 get_tree(s);
Al Viro6f5d5112009-12-19 15:59:45 +0000314 list_replace_init(&chunk->owners[j].list, &new->owners[i].list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400315 }
316
Al Viro74c3cbe2007-07-22 08:04:18 -0400317 list_for_each_entry(owner, &new->trees, same_root)
318 owner->root = new;
Jan Kara1635e572018-11-12 09:54:48 -0500319 /*
320 * Make sure chunk is fully initialized before making it visible in the
321 * hash. Pairs with a data dependency barrier in READ_ONCE() in
322 * audit_tree_lookup().
323 */
324 smp_wmb();
325 list_replace_rcu(&chunk->hash, &new->hash);
Al Viro74c3cbe2007-07-22 08:04:18 -0400326 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500327 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100328 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500329 fsnotify_free_mark(entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200330 fsnotify_put_mark(&new->mark); /* drop initial reference */
Al Viro8f7b0ba2008-11-15 01:15:43 +0000331 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400332
333Fallback:
334 // do the best we can
335 spin_lock(&hash_lock);
336 if (owner->root == chunk) {
337 list_del_init(&owner->same_root);
338 owner->root = NULL;
339 }
340 list_del_init(&p->list);
341 p->owner = NULL;
342 put_tree(owner);
343 spin_unlock(&hash_lock);
Jan Karabe29d202016-12-14 14:40:05 +0100344 mutex_unlock(&entry->group->mark_mutex);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000345out:
Eric Paris28a3a7e2009-12-17 20:12:05 -0500346 fsnotify_put_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000347 spin_lock(&hash_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -0400348}
349
Jan Karaa5789b02018-11-12 09:54:48 -0500350/* Call with group->mark_mutex held, releases it */
Al Viro74c3cbe2007-07-22 08:04:18 -0400351static int create_chunk(struct inode *inode, struct audit_tree *tree)
352{
Eric Parise61ce862009-12-17 21:24:24 -0500353 struct fsnotify_mark *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400354 struct audit_chunk *chunk = alloc_chunk(1);
Jan Karaa5789b02018-11-12 09:54:48 -0500355
356 if (!chunk) {
357 mutex_unlock(&audit_tree_group->mark_mutex);
Al Viro74c3cbe2007-07-22 08:04:18 -0400358 return -ENOMEM;
Jan Karaa5789b02018-11-12 09:54:48 -0500359 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400360
Eric Paris28a3a7e2009-12-17 20:12:05 -0500361 entry = &chunk->mark;
Jan Karaa5789b02018-11-12 09:54:48 -0500362 if (fsnotify_add_inode_mark_locked(entry, inode, 0)) {
363 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200364 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400365 return -ENOSPC;
366 }
367
Al Viro74c3cbe2007-07-22 08:04:18 -0400368 spin_lock(&hash_lock);
369 if (tree->goner) {
370 spin_unlock(&hash_lock);
371 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500372 fsnotify_detach_mark(entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500373 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500374 fsnotify_free_mark(entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500375 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400376 return 0;
377 }
378 chunk->owners[0].index = (1U << 31);
379 chunk->owners[0].owner = tree;
380 get_tree(tree);
381 list_add(&chunk->owners[0].list, &tree->chunks);
382 if (!tree->root) {
383 tree->root = chunk;
384 list_add(&tree->same_root, &chunk->trees);
385 }
Jan Kara8d20d6e2018-11-12 09:54:48 -0500386 chunk->key = inode_to_key(inode);
Jan Kara1635e572018-11-12 09:54:48 -0500387 /*
388 * Inserting into the hash table has to go last as once we do that RCU
389 * readers can see the chunk.
390 */
Al Viro74c3cbe2007-07-22 08:04:18 -0400391 insert_hash(chunk);
392 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500393 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200394 fsnotify_put_mark(entry); /* drop initial reference */
Al Viro74c3cbe2007-07-22 08:04:18 -0400395 return 0;
396}
397
398/* the first tagged inode becomes root of tree */
399static int tag_chunk(struct inode *inode, struct audit_tree *tree)
400{
Eric Parise61ce862009-12-17 21:24:24 -0500401 struct fsnotify_mark *old_entry, *chunk_entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400402 struct audit_tree *owner;
403 struct audit_chunk *chunk, *old;
404 struct node *p;
405 int n;
406
Jan Karaa5789b02018-11-12 09:54:48 -0500407 mutex_lock(&audit_tree_group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100408 old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks,
409 audit_tree_group);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500410 if (!old_entry)
Al Viro74c3cbe2007-07-22 08:04:18 -0400411 return create_chunk(inode, tree);
412
Eric Paris28a3a7e2009-12-17 20:12:05 -0500413 old = container_of(old_entry, struct audit_chunk, mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400414
415 /* are we already there? */
416 spin_lock(&hash_lock);
417 for (n = 0; n < old->count; n++) {
418 if (old->owners[n].owner == tree) {
419 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500420 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500421 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400422 return 0;
423 }
424 }
425 spin_unlock(&hash_lock);
426
427 chunk = alloc_chunk(old->count + 1);
Al Virob4c30aa2009-12-19 16:03:30 +0000428 if (!chunk) {
Jan Karaa5789b02018-11-12 09:54:48 -0500429 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500430 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400431 return -ENOMEM;
Al Virob4c30aa2009-12-19 16:03:30 +0000432 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400433
Eric Paris28a3a7e2009-12-17 20:12:05 -0500434 chunk_entry = &chunk->mark;
435
Jan Kara6b3f05d2016-12-21 12:15:30 +0100436 /*
437 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300438 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100439 */
Jan Kara43471d12017-04-03 16:47:58 +0200440 if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500441 /* old_entry is being shot, lets just lie */
Jan Karaa5789b02018-11-12 09:54:48 -0500442 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500443 fsnotify_put_mark(old_entry);
Jan Kara7b1293232016-12-21 18:32:48 +0100444 fsnotify_put_mark(&chunk->mark);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500445 return -ENOENT;
446 }
447
Amir Goldstein36f10f52018-06-23 17:54:49 +0300448 if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
449 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Jan Karaa5789b02018-11-12 09:54:48 -0500450 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200451 fsnotify_put_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500452 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400453 return -ENOSPC;
454 }
Eric Paris28a3a7e2009-12-17 20:12:05 -0500455
Al Viro74c3cbe2007-07-22 08:04:18 -0400456 spin_lock(&hash_lock);
457 if (tree->goner) {
458 spin_unlock(&hash_lock);
459 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500460 fsnotify_detach_mark(chunk_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500461 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500462 fsnotify_free_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500463 fsnotify_put_mark(chunk_entry);
464 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400465 return 0;
466 }
Jan Kara8d20d6e2018-11-12 09:54:48 -0500467 chunk->key = old->key;
Al Viro74c3cbe2007-07-22 08:04:18 -0400468 list_replace_init(&old->trees, &chunk->trees);
469 for (n = 0, p = chunk->owners; n < old->count; n++, p++) {
470 struct audit_tree *s = old->owners[n].owner;
471 p->owner = s;
472 p->index = old->owners[n].index;
473 if (!s) /* result of fallback in untag */
474 continue;
475 get_tree(s);
476 list_replace_init(&old->owners[n].list, &p->list);
477 }
478 p->index = (chunk->count - 1) | (1U<<31);
479 p->owner = tree;
480 get_tree(tree);
481 list_add(&p->list, &tree->chunks);
Al Viro74c3cbe2007-07-22 08:04:18 -0400482 list_for_each_entry(owner, &chunk->trees, same_root)
483 owner->root = chunk;
484 old->dead = 1;
485 if (!tree->root) {
486 tree->root = chunk;
487 list_add(&tree->same_root, &chunk->trees);
488 }
Jan Kara1635e572018-11-12 09:54:48 -0500489 /*
490 * Make sure chunk is fully initialized before making it visible in the
491 * hash. Pairs with a data dependency barrier in READ_ONCE() in
492 * audit_tree_lookup().
493 */
494 smp_wmb();
495 list_replace_rcu(&old->hash, &chunk->hash);
Al Viro74c3cbe2007-07-22 08:04:18 -0400496 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500497 fsnotify_detach_mark(old_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500498 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500499 fsnotify_free_mark(old_entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200500 fsnotify_put_mark(chunk_entry); /* drop initial reference */
Eric Paris28a3a7e2009-12-17 20:12:05 -0500501 fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
Al Viro74c3cbe2007-07-22 08:04:18 -0400502 return 0;
503}
504
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400505static void audit_tree_log_remove_rule(struct audit_krule *rule)
Kees Cook0644ec02013-01-11 14:32:07 -0800506{
507 struct audit_buffer *ab;
508
Richard Guy Briggs65a87662018-06-14 16:20:05 -0400509 if (!audit_enabled)
510 return;
Kees Cook0644ec02013-01-11 14:32:07 -0800511 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
512 if (unlikely(!ab))
513 return;
Steve Grubbc1e8f062016-11-16 16:14:33 -0500514 audit_log_format(ab, "op=remove_rule");
Kees Cook0644ec02013-01-11 14:32:07 -0800515 audit_log_format(ab, " dir=");
516 audit_log_untrustedstring(ab, rule->tree->pathname);
517 audit_log_key(ab, rule->filterkey);
518 audit_log_format(ab, " list=%d res=1", rule->listnr);
519 audit_log_end(ab);
520}
521
Al Viro74c3cbe2007-07-22 08:04:18 -0400522static void kill_rules(struct audit_tree *tree)
523{
524 struct audit_krule *rule, *next;
525 struct audit_entry *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400526
527 list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
528 entry = container_of(rule, struct audit_entry, rule);
529
530 list_del_init(&rule->rlist);
531 if (rule->tree) {
532 /* not a half-baked one */
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400533 audit_tree_log_remove_rule(rule);
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400534 if (entry->rule.exe)
535 audit_remove_mark(entry->rule.exe);
Al Viro74c3cbe2007-07-22 08:04:18 -0400536 rule->tree = NULL;
537 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -0500538 list_del(&entry->rule.list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400539 call_rcu(&entry->rcu, audit_free_rule_rcu);
540 }
541 }
542}
543
544/*
545 * finish killing struct audit_tree
546 */
547static void prune_one(struct audit_tree *victim)
548{
549 spin_lock(&hash_lock);
550 while (!list_empty(&victim->chunks)) {
551 struct node *p;
Al Viro74c3cbe2007-07-22 08:04:18 -0400552
553 p = list_entry(victim->chunks.next, struct node, list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400554
Al Viro8f7b0ba2008-11-15 01:15:43 +0000555 untag_chunk(p);
Al Viro74c3cbe2007-07-22 08:04:18 -0400556 }
557 spin_unlock(&hash_lock);
558 put_tree(victim);
559}
560
561/* trim the uncommitted chunks from tree */
562
563static void trim_marked(struct audit_tree *tree)
564{
565 struct list_head *p, *q;
566 spin_lock(&hash_lock);
567 if (tree->goner) {
568 spin_unlock(&hash_lock);
569 return;
570 }
571 /* reorder */
572 for (p = tree->chunks.next; p != &tree->chunks; p = q) {
573 struct node *node = list_entry(p, struct node, list);
574 q = p->next;
575 if (node->index & (1U<<31)) {
576 list_del_init(p);
577 list_add(p, &tree->chunks);
578 }
579 }
580
581 while (!list_empty(&tree->chunks)) {
582 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400583
584 node = list_entry(tree->chunks.next, struct node, list);
585
586 /* have we run out of marked? */
587 if (!(node->index & (1U<<31)))
588 break;
589
Al Viro8f7b0ba2008-11-15 01:15:43 +0000590 untag_chunk(node);
Al Viro74c3cbe2007-07-22 08:04:18 -0400591 }
592 if (!tree->root && !tree->goner) {
593 tree->goner = 1;
594 spin_unlock(&hash_lock);
595 mutex_lock(&audit_filter_mutex);
596 kill_rules(tree);
597 list_del_init(&tree->list);
598 mutex_unlock(&audit_filter_mutex);
599 prune_one(tree);
600 } else {
601 spin_unlock(&hash_lock);
602 }
603}
604
Al Viro916d7572009-06-24 00:02:38 -0400605static void audit_schedule_prune(void);
606
Al Viro74c3cbe2007-07-22 08:04:18 -0400607/* called with audit_filter_mutex */
608int audit_remove_tree_rule(struct audit_krule *rule)
609{
610 struct audit_tree *tree;
611 tree = rule->tree;
612 if (tree) {
613 spin_lock(&hash_lock);
614 list_del_init(&rule->rlist);
615 if (list_empty(&tree->rules) && !tree->goner) {
616 tree->root = NULL;
617 list_del_init(&tree->same_root);
618 tree->goner = 1;
619 list_move(&tree->list, &prune_list);
620 rule->tree = NULL;
621 spin_unlock(&hash_lock);
622 audit_schedule_prune();
623 return 1;
624 }
625 rule->tree = NULL;
626 spin_unlock(&hash_lock);
627 return 1;
628 }
629 return 0;
630}
631
Al Viro1f707132010-01-30 22:51:25 -0500632static int compare_root(struct vfsmount *mnt, void *arg)
633{
Jan Karaf410ff62016-12-16 10:13:37 +0100634 return inode_to_key(d_backing_inode(mnt->mnt_root)) ==
635 (unsigned long)arg;
Al Viro1f707132010-01-30 22:51:25 -0500636}
637
Al Viro74c3cbe2007-07-22 08:04:18 -0400638void audit_trim_trees(void)
639{
640 struct list_head cursor;
641
642 mutex_lock(&audit_filter_mutex);
643 list_add(&cursor, &tree_list);
644 while (cursor.next != &tree_list) {
645 struct audit_tree *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400646 struct path path;
Al Viro74c3cbe2007-07-22 08:04:18 -0400647 struct vfsmount *root_mnt;
648 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400649 int err;
650
651 tree = container_of(cursor.next, struct audit_tree, list);
652 get_tree(tree);
653 list_del(&cursor);
654 list_add(&cursor, &tree->list);
655 mutex_unlock(&audit_filter_mutex);
656
Al Viro98bc9932008-08-02 01:06:21 -0400657 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400658 if (err)
659 goto skip_it;
660
Al Viro589ff872009-04-18 03:28:19 -0400661 root_mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400662 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100663 if (IS_ERR(root_mnt))
Al Viro74c3cbe2007-07-22 08:04:18 -0400664 goto skip_it;
665
Al Viro74c3cbe2007-07-22 08:04:18 -0400666 spin_lock(&hash_lock);
667 list_for_each_entry(node, &tree->chunks, list) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500668 struct audit_chunk *chunk = find_chunk(node);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300669 /* this could be NULL if the watch is dying else where... */
Al Viro74c3cbe2007-07-22 08:04:18 -0400670 node->index |= 1U<<31;
Jan Karaf410ff62016-12-16 10:13:37 +0100671 if (iterate_mounts(compare_root,
Jan Kara8d20d6e2018-11-12 09:54:48 -0500672 (void *)(chunk->key),
Jan Karaf410ff62016-12-16 10:13:37 +0100673 root_mnt))
Al Viro1f707132010-01-30 22:51:25 -0500674 node->index &= ~(1U<<31);
Al Viro74c3cbe2007-07-22 08:04:18 -0400675 }
676 spin_unlock(&hash_lock);
677 trim_marked(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400678 drop_collected_mounts(root_mnt);
679skip_it:
Chen Gang12b2f112013-04-29 15:05:19 -0700680 put_tree(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400681 mutex_lock(&audit_filter_mutex);
682 }
683 list_del(&cursor);
684 mutex_unlock(&audit_filter_mutex);
685}
686
Al Viro74c3cbe2007-07-22 08:04:18 -0400687int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
688{
689
690 if (pathname[0] != '/' ||
691 rule->listnr != AUDIT_FILTER_EXIT ||
Al Viro5af75d82008-12-16 05:59:26 -0500692 op != Audit_equal ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400693 rule->inode_f || rule->watch || rule->tree)
694 return -EINVAL;
695 rule->tree = alloc_tree(pathname);
696 if (!rule->tree)
697 return -ENOMEM;
698 return 0;
699}
700
701void audit_put_tree(struct audit_tree *tree)
702{
703 put_tree(tree);
704}
705
Al Viro1f707132010-01-30 22:51:25 -0500706static int tag_mount(struct vfsmount *mnt, void *arg)
707{
David Howells3b362152015-03-17 22:26:21 +0000708 return tag_chunk(d_backing_inode(mnt->mnt_root), arg);
Al Viro1f707132010-01-30 22:51:25 -0500709}
710
Imre Palikf1aaf262015-02-23 15:37:59 -0500711/*
712 * That gets run when evict_chunk() ends up needing to kill audit_tree.
713 * Runs from a separate thread.
714 */
715static int prune_tree_thread(void *unused)
716{
717 for (;;) {
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200718 if (list_empty(&prune_list)) {
719 set_current_state(TASK_INTERRUPTIBLE);
Imre Palikf1aaf262015-02-23 15:37:59 -0500720 schedule();
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200721 }
Imre Palikf1aaf262015-02-23 15:37:59 -0500722
Paul Moorece423632018-02-20 09:52:38 -0500723 audit_ctl_lock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500724 mutex_lock(&audit_filter_mutex);
725
726 while (!list_empty(&prune_list)) {
727 struct audit_tree *victim;
728
729 victim = list_entry(prune_list.next,
730 struct audit_tree, list);
731 list_del_init(&victim->list);
732
733 mutex_unlock(&audit_filter_mutex);
734
735 prune_one(victim);
736
737 mutex_lock(&audit_filter_mutex);
738 }
739
740 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500741 audit_ctl_unlock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500742 }
743 return 0;
744}
745
746static int audit_launch_prune(void)
747{
748 if (prune_thread)
749 return 0;
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200750 prune_thread = kthread_run(prune_tree_thread, NULL,
Imre Palikf1aaf262015-02-23 15:37:59 -0500751 "audit_prune_tree");
752 if (IS_ERR(prune_thread)) {
753 pr_err("cannot start thread audit_prune_tree");
754 prune_thread = NULL;
755 return -ENOMEM;
Imre Palikf1aaf262015-02-23 15:37:59 -0500756 }
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200757 return 0;
Imre Palikf1aaf262015-02-23 15:37:59 -0500758}
759
Al Viro74c3cbe2007-07-22 08:04:18 -0400760/* called with audit_filter_mutex */
761int audit_add_tree_rule(struct audit_krule *rule)
762{
763 struct audit_tree *seed = rule->tree, *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400764 struct path path;
Al Viro1f707132010-01-30 22:51:25 -0500765 struct vfsmount *mnt;
Al Viro74c3cbe2007-07-22 08:04:18 -0400766 int err;
767
Chen Gang736f3202013-06-12 14:05:07 -0700768 rule->tree = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400769 list_for_each_entry(tree, &tree_list, list) {
770 if (!strcmp(seed->pathname, tree->pathname)) {
771 put_tree(seed);
772 rule->tree = tree;
773 list_add(&rule->rlist, &tree->rules);
774 return 0;
775 }
776 }
777 tree = seed;
778 list_add(&tree->list, &tree_list);
779 list_add(&rule->rlist, &tree->rules);
780 /* do not set rule->tree yet */
781 mutex_unlock(&audit_filter_mutex);
782
Imre Palikf1aaf262015-02-23 15:37:59 -0500783 if (unlikely(!prune_thread)) {
784 err = audit_launch_prune();
785 if (err)
786 goto Err;
787 }
788
Al Viro98bc9932008-08-02 01:06:21 -0400789 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400790 if (err)
791 goto Err;
Al Viro589ff872009-04-18 03:28:19 -0400792 mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400793 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100794 if (IS_ERR(mnt)) {
795 err = PTR_ERR(mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400796 goto Err;
797 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400798
799 get_tree(tree);
Al Viro1f707132010-01-30 22:51:25 -0500800 err = iterate_mounts(tag_mount, tree, mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400801 drop_collected_mounts(mnt);
802
803 if (!err) {
804 struct node *node;
805 spin_lock(&hash_lock);
806 list_for_each_entry(node, &tree->chunks, list)
807 node->index &= ~(1U<<31);
808 spin_unlock(&hash_lock);
809 } else {
810 trim_marked(tree);
811 goto Err;
812 }
813
814 mutex_lock(&audit_filter_mutex);
815 if (list_empty(&rule->rlist)) {
816 put_tree(tree);
817 return -ENOENT;
818 }
819 rule->tree = tree;
820 put_tree(tree);
821
822 return 0;
823Err:
824 mutex_lock(&audit_filter_mutex);
825 list_del_init(&tree->list);
826 list_del_init(&tree->rules);
827 put_tree(tree);
828 return err;
829}
830
831int audit_tag_tree(char *old, char *new)
832{
833 struct list_head cursor, barrier;
834 int failed = 0;
Al Viro2096f752010-01-30 13:16:21 -0500835 struct path path1, path2;
Al Viro74c3cbe2007-07-22 08:04:18 -0400836 struct vfsmount *tagged;
Al Viro74c3cbe2007-07-22 08:04:18 -0400837 int err;
838
Al Viro2096f752010-01-30 13:16:21 -0500839 err = kern_path(new, 0, &path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400840 if (err)
841 return err;
Al Viro2096f752010-01-30 13:16:21 -0500842 tagged = collect_mounts(&path2);
843 path_put(&path2);
David Howellsbe34d1a2012-06-25 12:55:18 +0100844 if (IS_ERR(tagged))
845 return PTR_ERR(tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400846
Al Viro2096f752010-01-30 13:16:21 -0500847 err = kern_path(old, 0, &path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400848 if (err) {
849 drop_collected_mounts(tagged);
850 return err;
851 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400852
Al Viro74c3cbe2007-07-22 08:04:18 -0400853 mutex_lock(&audit_filter_mutex);
854 list_add(&barrier, &tree_list);
855 list_add(&cursor, &barrier);
856
857 while (cursor.next != &tree_list) {
858 struct audit_tree *tree;
Al Viro2096f752010-01-30 13:16:21 -0500859 int good_one = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400860
861 tree = container_of(cursor.next, struct audit_tree, list);
862 get_tree(tree);
863 list_del(&cursor);
864 list_add(&cursor, &tree->list);
865 mutex_unlock(&audit_filter_mutex);
866
Al Viro2096f752010-01-30 13:16:21 -0500867 err = kern_path(tree->pathname, 0, &path2);
868 if (!err) {
869 good_one = path_is_under(&path1, &path2);
870 path_put(&path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400871 }
872
Al Viro2096f752010-01-30 13:16:21 -0500873 if (!good_one) {
Al Viro74c3cbe2007-07-22 08:04:18 -0400874 put_tree(tree);
875 mutex_lock(&audit_filter_mutex);
876 continue;
877 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400878
Al Viro1f707132010-01-30 22:51:25 -0500879 failed = iterate_mounts(tag_mount, tree, tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400880 if (failed) {
881 put_tree(tree);
882 mutex_lock(&audit_filter_mutex);
883 break;
884 }
885
886 mutex_lock(&audit_filter_mutex);
887 spin_lock(&hash_lock);
888 if (!tree->goner) {
889 list_del(&tree->list);
890 list_add(&tree->list, &tree_list);
891 }
892 spin_unlock(&hash_lock);
893 put_tree(tree);
894 }
895
896 while (barrier.prev != &tree_list) {
897 struct audit_tree *tree;
898
899 tree = container_of(barrier.prev, struct audit_tree, list);
900 get_tree(tree);
901 list_del(&tree->list);
902 list_add(&tree->list, &barrier);
903 mutex_unlock(&audit_filter_mutex);
904
905 if (!failed) {
906 struct node *node;
907 spin_lock(&hash_lock);
908 list_for_each_entry(node, &tree->chunks, list)
909 node->index &= ~(1U<<31);
910 spin_unlock(&hash_lock);
911 } else {
912 trim_marked(tree);
913 }
914
915 put_tree(tree);
916 mutex_lock(&audit_filter_mutex);
917 }
918 list_del(&barrier);
919 list_del(&cursor);
Al Viro74c3cbe2007-07-22 08:04:18 -0400920 mutex_unlock(&audit_filter_mutex);
Al Viro2096f752010-01-30 13:16:21 -0500921 path_put(&path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400922 drop_collected_mounts(tagged);
923 return failed;
924}
925
Al Viro916d7572009-06-24 00:02:38 -0400926
927static void audit_schedule_prune(void)
928{
Imre Palikf1aaf262015-02-23 15:37:59 -0500929 wake_up_process(prune_thread);
Al Viro916d7572009-06-24 00:02:38 -0400930}
931
932/*
933 * ... and that one is done if evict_chunk() decides to delay until the end
934 * of syscall. Runs synchronously.
935 */
936void audit_kill_trees(struct list_head *list)
937{
Paul Moorece423632018-02-20 09:52:38 -0500938 audit_ctl_lock();
Al Viro916d7572009-06-24 00:02:38 -0400939 mutex_lock(&audit_filter_mutex);
940
941 while (!list_empty(list)) {
942 struct audit_tree *victim;
943
944 victim = list_entry(list->next, struct audit_tree, list);
945 kill_rules(victim);
946 list_del_init(&victim->list);
947
948 mutex_unlock(&audit_filter_mutex);
949
950 prune_one(victim);
951
952 mutex_lock(&audit_filter_mutex);
953 }
954
955 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500956 audit_ctl_unlock();
Al Viro74c3cbe2007-07-22 08:04:18 -0400957}
958
959/*
960 * Here comes the stuff asynchronous to auditctl operations
961 */
962
Al Viro74c3cbe2007-07-22 08:04:18 -0400963static void evict_chunk(struct audit_chunk *chunk)
964{
965 struct audit_tree *owner;
Al Viro916d7572009-06-24 00:02:38 -0400966 struct list_head *postponed = audit_killed_trees();
967 int need_prune = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400968 int n;
969
970 if (chunk->dead)
971 return;
972
973 chunk->dead = 1;
974 mutex_lock(&audit_filter_mutex);
975 spin_lock(&hash_lock);
976 while (!list_empty(&chunk->trees)) {
977 owner = list_entry(chunk->trees.next,
978 struct audit_tree, same_root);
979 owner->goner = 1;
980 owner->root = NULL;
981 list_del_init(&owner->same_root);
982 spin_unlock(&hash_lock);
Al Viro916d7572009-06-24 00:02:38 -0400983 if (!postponed) {
984 kill_rules(owner);
985 list_move(&owner->list, &prune_list);
986 need_prune = 1;
987 } else {
988 list_move(&owner->list, postponed);
989 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400990 spin_lock(&hash_lock);
991 }
992 list_del_rcu(&chunk->hash);
993 for (n = 0; n < chunk->count; n++)
994 list_del_init(&chunk->owners[n].list);
995 spin_unlock(&hash_lock);
Imre Palikf1aaf262015-02-23 15:37:59 -0500996 mutex_unlock(&audit_filter_mutex);
Al Viro916d7572009-06-24 00:02:38 -0400997 if (need_prune)
998 audit_schedule_prune();
Al Viro74c3cbe2007-07-22 08:04:18 -0400999}
1000
Eric Paris3a9b16b2010-07-28 10:18:38 -04001001static int audit_tree_handle_event(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -08001002 struct inode *to_tell,
Al Viro3cd5eca2016-11-20 20:19:09 -05001003 u32 mask, const void *data, int data_type,
Jan Kara9385a842016-11-10 17:51:50 +01001004 const unsigned char *file_name, u32 cookie,
1005 struct fsnotify_iter_info *iter_info)
Al Viro74c3cbe2007-07-22 08:04:18 -04001006{
Jan Kara83c4c4b2014-01-21 15:48:15 -08001007 return 0;
Al Viro74c3cbe2007-07-22 08:04:18 -04001008}
1009
Eric Parise61ce862009-12-17 21:24:24 -05001010static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
Al Viro74c3cbe2007-07-22 08:04:18 -04001011{
Eric Paris28a3a7e2009-12-17 20:12:05 -05001012 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
1013
1014 evict_chunk(chunk);
Miklos Szeredib3e86922012-08-15 12:55:22 +02001015
1016 /*
1017 * We are guaranteed to have at least one reference to the mark from
1018 * either the inode or the caller of fsnotify_destroy_mark().
1019 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +03001020 BUG_ON(refcount_read(&entry->refcnt) < 1);
Al Viro74c3cbe2007-07-22 08:04:18 -04001021}
1022
Eric Paris28a3a7e2009-12-17 20:12:05 -05001023static const struct fsnotify_ops audit_tree_ops = {
1024 .handle_event = audit_tree_handle_event,
Eric Paris28a3a7e2009-12-17 20:12:05 -05001025 .freeing_mark = audit_tree_freeing_mark,
Jan Kara054c6362016-12-21 18:06:12 +01001026 .free_mark = audit_tree_destroy_watch,
Al Viro74c3cbe2007-07-22 08:04:18 -04001027};
1028
1029static int __init audit_tree_init(void)
1030{
1031 int i;
1032
Eric Paris0d2e2a12009-12-17 21:24:22 -05001033 audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
Eric Paris28a3a7e2009-12-17 20:12:05 -05001034 if (IS_ERR(audit_tree_group))
1035 audit_panic("cannot initialize fsnotify group for rectree watches");
Al Viro74c3cbe2007-07-22 08:04:18 -04001036
1037 for (i = 0; i < HASH_SIZE; i++)
1038 INIT_LIST_HEAD(&chunk_hash_heads[i]);
1039
1040 return 0;
1041}
1042__initcall(audit_tree_init);