blob: 35c031ebcc12e492b882b145d09dfa6c7de97c45 [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
Jan Karaa8375712018-11-12 09:54:49 -0500135/*
136 * Drop reference to the chunk that was held by the mark. This is the reference
137 * that gets dropped after we've removed the chunk from the hash table and we
138 * use it to make sure chunk cannot be freed before RCU grace period expires.
139 */
140static void audit_mark_put_chunk(struct audit_chunk *chunk)
141{
142 call_rcu(&chunk->head, __put_chunk);
143}
144
Eric Parise61ce862009-12-17 21:24:24 -0500145static void audit_tree_destroy_watch(struct fsnotify_mark *entry)
Eric Paris28a3a7e2009-12-17 20:12:05 -0500146{
147 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
Jan Karaa8375712018-11-12 09:54:49 -0500148 audit_mark_put_chunk(chunk);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500149}
150
151static struct audit_chunk *alloc_chunk(int count)
152{
153 struct audit_chunk *chunk;
154 size_t size;
155 int i;
156
157 size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
158 chunk = kzalloc(size, GFP_KERNEL);
159 if (!chunk)
160 return NULL;
161
162 INIT_LIST_HEAD(&chunk->hash);
163 INIT_LIST_HEAD(&chunk->trees);
164 chunk->count = count;
165 atomic_long_set(&chunk->refs, 1);
166 for (i = 0; i < count; i++) {
167 INIT_LIST_HEAD(&chunk->owners[i].list);
168 chunk->owners[i].index = i;
169 }
Jan Kara054c6362016-12-21 18:06:12 +0100170 fsnotify_init_mark(&chunk->mark, audit_tree_group);
Miklos Szeredi799b6012014-11-04 11:27:12 +0100171 chunk->mark.mask = FS_IN_IGNORED;
Eric Paris28a3a7e2009-12-17 20:12:05 -0500172 return chunk;
173}
174
Al Viro74c3cbe2007-07-22 08:04:18 -0400175enum {HASH_SIZE = 128};
176static struct list_head chunk_hash_heads[HASH_SIZE];
177static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
178
Jan Karaf410ff62016-12-16 10:13:37 +0100179/* Function to return search key in our hash from inode. */
180static unsigned long inode_to_key(const struct inode *inode)
Al Viro74c3cbe2007-07-22 08:04:18 -0400181{
Amir Goldstein36f10f52018-06-23 17:54:49 +0300182 /* Use address pointed to by connector->obj as the key */
183 return (unsigned long)&inode->i_fsnotify_marks;
Jan Karaf410ff62016-12-16 10:13:37 +0100184}
185
Jan Karaf410ff62016-12-16 10:13:37 +0100186static inline struct list_head *chunk_hash(unsigned long key)
187{
188 unsigned long n = key / L1_CACHE_BYTES;
Al Viro74c3cbe2007-07-22 08:04:18 -0400189 return chunk_hash_heads + n % HASH_SIZE;
190}
191
Jan Kara9f16d2e2018-10-17 12:14:52 +0200192/* hash_lock & entry->group->mark_mutex is held by caller */
Al Viro74c3cbe2007-07-22 08:04:18 -0400193static void insert_hash(struct audit_chunk *chunk)
194{
Eric Paris28a3a7e2009-12-17 20:12:05 -0500195 struct list_head *list;
196
Jan Kara1635e572018-11-12 09:54:48 -0500197 /*
198 * Make sure chunk is fully initialized before making it visible in the
199 * hash. Pairs with a data dependency barrier in READ_ONCE() in
200 * audit_tree_lookup().
201 */
202 smp_wmb();
Jan Kara8d20d6e2018-11-12 09:54:48 -0500203 WARN_ON_ONCE(!chunk->key);
204 list = chunk_hash(chunk->key);
Al Viro74c3cbe2007-07-22 08:04:18 -0400205 list_add_rcu(&chunk->hash, list);
206}
207
208/* called under rcu_read_lock */
209struct audit_chunk *audit_tree_lookup(const struct inode *inode)
210{
Jan Karaf410ff62016-12-16 10:13:37 +0100211 unsigned long key = inode_to_key(inode);
212 struct list_head *list = chunk_hash(key);
Paul E. McKenney6793a052008-05-14 17:10:12 -0700213 struct audit_chunk *p;
Al Viro74c3cbe2007-07-22 08:04:18 -0400214
Paul E. McKenney6793a052008-05-14 17:10:12 -0700215 list_for_each_entry_rcu(p, list, hash) {
Jan Kara1635e572018-11-12 09:54:48 -0500216 /*
217 * We use a data dependency barrier in READ_ONCE() to make sure
218 * the chunk we see is fully initialized.
219 */
220 if (READ_ONCE(p->key) == key) {
Al Viro8f7b0ba2008-11-15 01:15:43 +0000221 atomic_long_inc(&p->refs);
Al Viro74c3cbe2007-07-22 08:04:18 -0400222 return p;
223 }
224 }
225 return NULL;
226}
227
Yaowei Bai6f1b5d72015-11-04 08:23:51 -0500228bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree)
Al Viro74c3cbe2007-07-22 08:04:18 -0400229{
230 int n;
231 for (n = 0; n < chunk->count; n++)
232 if (chunk->owners[n].owner == tree)
Yaowei Bai6f1b5d72015-11-04 08:23:51 -0500233 return true;
234 return false;
Al Viro74c3cbe2007-07-22 08:04:18 -0400235}
236
237/* tagging and untagging inodes with trees */
238
Al Viro8f7b0ba2008-11-15 01:15:43 +0000239static struct audit_chunk *find_chunk(struct node *p)
Al Viro74c3cbe2007-07-22 08:04:18 -0400240{
Al Viro8f7b0ba2008-11-15 01:15:43 +0000241 int index = p->index & ~(1U<<31);
242 p -= index;
243 return container_of(p, struct audit_chunk, owners[0]);
244}
245
Jan Karad31b3262018-11-12 09:54:48 -0500246static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old,
247 struct node *skip)
248{
249 struct audit_tree *owner;
250 int i, j;
251
252 new->key = old->key;
253 list_splice_init(&old->trees, &new->trees);
254 list_for_each_entry(owner, &new->trees, same_root)
255 owner->root = new;
256 for (i = j = 0; j < old->count; i++, j++) {
257 if (&old->owners[j] == skip) {
258 i--;
259 continue;
260 }
261 owner = old->owners[j].owner;
262 new->owners[i].owner = owner;
263 new->owners[i].index = old->owners[j].index - j + i;
264 if (!owner) /* result of earlier fallback */
265 continue;
266 get_tree(owner);
267 list_replace_init(&old->owners[j].list, &new->owners[i].list);
268 }
269 /*
270 * Make sure chunk is fully initialized before making it visible in the
271 * hash. Pairs with a data dependency barrier in READ_ONCE() in
272 * audit_tree_lookup().
273 */
274 smp_wmb();
275 list_replace_rcu(&old->hash, &new->hash);
276}
277
Al Viro8f7b0ba2008-11-15 01:15:43 +0000278static void untag_chunk(struct node *p)
279{
280 struct audit_chunk *chunk = find_chunk(p);
Eric Parise61ce862009-12-17 21:24:24 -0500281 struct fsnotify_mark *entry = &chunk->mark;
Al Virof7a998a2010-10-30 02:18:32 -0400282 struct audit_chunk *new = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400283 struct audit_tree *owner;
284 int size = chunk->count - 1;
Al Viro74c3cbe2007-07-22 08:04:18 -0400285
Eric Paris28a3a7e2009-12-17 20:12:05 -0500286 fsnotify_get_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000287
288 spin_unlock(&hash_lock);
289
Al Virof7a998a2010-10-30 02:18:32 -0400290 if (size)
291 new = alloc_chunk(size);
292
Jan Karabe29d202016-12-14 14:40:05 +0100293 mutex_lock(&entry->group->mark_mutex);
Jan Kara6b3f05d2016-12-21 12:15:30 +0100294 /*
295 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300296 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100297 */
Jan Kara43471d12017-04-03 16:47:58 +0200298 if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Jan Karabe29d202016-12-14 14:40:05 +0100299 mutex_unlock(&entry->group->mark_mutex);
Al Virof7a998a2010-10-30 02:18:32 -0400300 if (new)
Jan Kara7b1293232016-12-21 18:32:48 +0100301 fsnotify_put_mark(&new->mark);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000302 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400303 }
304
305 owner = p->owner;
306
307 if (!size) {
308 chunk->dead = 1;
309 spin_lock(&hash_lock);
310 list_del_init(&chunk->trees);
311 if (owner->root == chunk)
312 owner->root = NULL;
313 list_del_init(&p->list);
314 list_del_rcu(&chunk->hash);
315 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500316 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100317 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500318 fsnotify_free_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000319 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400320 }
321
Al Viro74c3cbe2007-07-22 08:04:18 -0400322 if (!new)
323 goto Fallback;
Al Virof7a998a2010-10-30 02:18:32 -0400324
Amir Goldstein36f10f52018-06-23 17:54:49 +0300325 if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
326 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200327 fsnotify_put_mark(&new->mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400328 goto Fallback;
329 }
330
331 chunk->dead = 1;
332 spin_lock(&hash_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -0400333 if (owner->root == chunk) {
334 list_del_init(&owner->same_root);
335 owner->root = NULL;
336 }
Jan Karad31b3262018-11-12 09:54:48 -0500337 list_del_init(&p->list);
Jan Kara1635e572018-11-12 09:54:48 -0500338 /*
Jan Karad31b3262018-11-12 09:54:48 -0500339 * This has to go last when updating chunk as once replace_chunk() is
340 * called, new RCU readers can see the new chunk.
Jan Kara1635e572018-11-12 09:54:48 -0500341 */
Jan Karad31b3262018-11-12 09:54:48 -0500342 replace_chunk(new, chunk, p);
Al Viro74c3cbe2007-07-22 08:04:18 -0400343 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500344 fsnotify_detach_mark(entry);
Jan Karabe29d202016-12-14 14:40:05 +0100345 mutex_unlock(&entry->group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500346 fsnotify_free_mark(entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200347 fsnotify_put_mark(&new->mark); /* drop initial reference */
Al Viro8f7b0ba2008-11-15 01:15:43 +0000348 goto out;
Al Viro74c3cbe2007-07-22 08:04:18 -0400349
350Fallback:
351 // do the best we can
352 spin_lock(&hash_lock);
353 if (owner->root == chunk) {
354 list_del_init(&owner->same_root);
355 owner->root = NULL;
356 }
357 list_del_init(&p->list);
358 p->owner = NULL;
359 put_tree(owner);
360 spin_unlock(&hash_lock);
Jan Karabe29d202016-12-14 14:40:05 +0100361 mutex_unlock(&entry->group->mark_mutex);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000362out:
Eric Paris28a3a7e2009-12-17 20:12:05 -0500363 fsnotify_put_mark(entry);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000364 spin_lock(&hash_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -0400365}
366
Jan Karaa5789b02018-11-12 09:54:48 -0500367/* Call with group->mark_mutex held, releases it */
Al Viro74c3cbe2007-07-22 08:04:18 -0400368static int create_chunk(struct inode *inode, struct audit_tree *tree)
369{
Eric Parise61ce862009-12-17 21:24:24 -0500370 struct fsnotify_mark *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400371 struct audit_chunk *chunk = alloc_chunk(1);
Jan Karaa5789b02018-11-12 09:54:48 -0500372
373 if (!chunk) {
374 mutex_unlock(&audit_tree_group->mark_mutex);
Al Viro74c3cbe2007-07-22 08:04:18 -0400375 return -ENOMEM;
Jan Karaa5789b02018-11-12 09:54:48 -0500376 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400377
Eric Paris28a3a7e2009-12-17 20:12:05 -0500378 entry = &chunk->mark;
Jan Karaa5789b02018-11-12 09:54:48 -0500379 if (fsnotify_add_inode_mark_locked(entry, inode, 0)) {
380 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200381 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400382 return -ENOSPC;
383 }
384
Al Viro74c3cbe2007-07-22 08:04:18 -0400385 spin_lock(&hash_lock);
386 if (tree->goner) {
387 spin_unlock(&hash_lock);
388 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500389 fsnotify_detach_mark(entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500390 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500391 fsnotify_free_mark(entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500392 fsnotify_put_mark(entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400393 return 0;
394 }
395 chunk->owners[0].index = (1U << 31);
396 chunk->owners[0].owner = tree;
397 get_tree(tree);
398 list_add(&chunk->owners[0].list, &tree->chunks);
399 if (!tree->root) {
400 tree->root = chunk;
401 list_add(&tree->same_root, &chunk->trees);
402 }
Jan Kara8d20d6e2018-11-12 09:54:48 -0500403 chunk->key = inode_to_key(inode);
Jan Kara1635e572018-11-12 09:54:48 -0500404 /*
405 * Inserting into the hash table has to go last as once we do that RCU
406 * readers can see the chunk.
407 */
Al Viro74c3cbe2007-07-22 08:04:18 -0400408 insert_hash(chunk);
409 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500410 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200411 fsnotify_put_mark(entry); /* drop initial reference */
Al Viro74c3cbe2007-07-22 08:04:18 -0400412 return 0;
413}
414
415/* the first tagged inode becomes root of tree */
416static int tag_chunk(struct inode *inode, struct audit_tree *tree)
417{
Eric Parise61ce862009-12-17 21:24:24 -0500418 struct fsnotify_mark *old_entry, *chunk_entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400419 struct audit_chunk *chunk, *old;
420 struct node *p;
421 int n;
422
Jan Karaa5789b02018-11-12 09:54:48 -0500423 mutex_lock(&audit_tree_group->mark_mutex);
Jan Karab1362ed2016-12-21 16:28:45 +0100424 old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks,
425 audit_tree_group);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500426 if (!old_entry)
Al Viro74c3cbe2007-07-22 08:04:18 -0400427 return create_chunk(inode, tree);
428
Eric Paris28a3a7e2009-12-17 20:12:05 -0500429 old = container_of(old_entry, struct audit_chunk, mark);
Al Viro74c3cbe2007-07-22 08:04:18 -0400430
431 /* are we already there? */
432 spin_lock(&hash_lock);
433 for (n = 0; n < old->count; n++) {
434 if (old->owners[n].owner == tree) {
435 spin_unlock(&hash_lock);
Jan Karaa5789b02018-11-12 09:54:48 -0500436 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500437 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400438 return 0;
439 }
440 }
441 spin_unlock(&hash_lock);
442
443 chunk = alloc_chunk(old->count + 1);
Al Virob4c30aa2009-12-19 16:03:30 +0000444 if (!chunk) {
Jan Karaa5789b02018-11-12 09:54:48 -0500445 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500446 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400447 return -ENOMEM;
Al Virob4c30aa2009-12-19 16:03:30 +0000448 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400449
Eric Paris28a3a7e2009-12-17 20:12:05 -0500450 chunk_entry = &chunk->mark;
451
Jan Kara6b3f05d2016-12-21 12:15:30 +0100452 /*
453 * mark_mutex protects mark from getting detached and thus also from
Amir Goldstein36f10f52018-06-23 17:54:49 +0300454 * mark->connector->obj getting NULL.
Jan Kara6b3f05d2016-12-21 12:15:30 +0100455 */
Jan Kara43471d12017-04-03 16:47:58 +0200456 if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500457 /* old_entry is being shot, lets just lie */
Jan Karaa5789b02018-11-12 09:54:48 -0500458 mutex_unlock(&audit_tree_group->mark_mutex);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500459 fsnotify_put_mark(old_entry);
Jan Kara7b1293232016-12-21 18:32:48 +0100460 fsnotify_put_mark(&chunk->mark);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500461 return -ENOENT;
462 }
463
Amir Goldstein36f10f52018-06-23 17:54:49 +0300464 if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
465 FSNOTIFY_OBJ_TYPE_INODE, 1)) {
Jan Karaa5789b02018-11-12 09:54:48 -0500466 mutex_unlock(&audit_tree_group->mark_mutex);
Miklos Szeredi0fe33aa2012-08-15 12:55:22 +0200467 fsnotify_put_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500468 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400469 return -ENOSPC;
470 }
Eric Paris28a3a7e2009-12-17 20:12:05 -0500471
Al Viro74c3cbe2007-07-22 08:04:18 -0400472 spin_lock(&hash_lock);
473 if (tree->goner) {
474 spin_unlock(&hash_lock);
475 chunk->dead = 1;
Jan Karab1e46032018-11-12 09:54:48 -0500476 fsnotify_detach_mark(chunk_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500477 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500478 fsnotify_free_mark(chunk_entry);
Eric Paris28a3a7e2009-12-17 20:12:05 -0500479 fsnotify_put_mark(chunk_entry);
480 fsnotify_put_mark(old_entry);
Al Viro74c3cbe2007-07-22 08:04:18 -0400481 return 0;
482 }
Jan Karad31b3262018-11-12 09:54:48 -0500483 p = &chunk->owners[chunk->count - 1];
Al Viro74c3cbe2007-07-22 08:04:18 -0400484 p->index = (chunk->count - 1) | (1U<<31);
485 p->owner = tree;
486 get_tree(tree);
487 list_add(&p->list, &tree->chunks);
Al Viro74c3cbe2007-07-22 08:04:18 -0400488 old->dead = 1;
489 if (!tree->root) {
490 tree->root = chunk;
491 list_add(&tree->same_root, &chunk->trees);
492 }
Jan Kara1635e572018-11-12 09:54:48 -0500493 /*
Jan Karad31b3262018-11-12 09:54:48 -0500494 * This has to go last when updating chunk as once replace_chunk() is
495 * called, new RCU readers can see the new chunk.
Jan Kara1635e572018-11-12 09:54:48 -0500496 */
Jan Karad31b3262018-11-12 09:54:48 -0500497 replace_chunk(chunk, old, NULL);
Al Viro74c3cbe2007-07-22 08:04:18 -0400498 spin_unlock(&hash_lock);
Jan Karab1e46032018-11-12 09:54:48 -0500499 fsnotify_detach_mark(old_entry);
Jan Karaa5789b02018-11-12 09:54:48 -0500500 mutex_unlock(&audit_tree_group->mark_mutex);
Jan Karab1e46032018-11-12 09:54:48 -0500501 fsnotify_free_mark(old_entry);
Miklos Szeredib3e86922012-08-15 12:55:22 +0200502 fsnotify_put_mark(chunk_entry); /* drop initial reference */
Eric Paris28a3a7e2009-12-17 20:12:05 -0500503 fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */
Al Viro74c3cbe2007-07-22 08:04:18 -0400504 return 0;
505}
506
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400507static void audit_tree_log_remove_rule(struct audit_krule *rule)
Kees Cook0644ec02013-01-11 14:32:07 -0800508{
509 struct audit_buffer *ab;
510
Richard Guy Briggs65a87662018-06-14 16:20:05 -0400511 if (!audit_enabled)
512 return;
Kees Cook0644ec02013-01-11 14:32:07 -0800513 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
514 if (unlikely(!ab))
515 return;
Steve Grubbc1e8f062016-11-16 16:14:33 -0500516 audit_log_format(ab, "op=remove_rule");
Kees Cook0644ec02013-01-11 14:32:07 -0800517 audit_log_format(ab, " dir=");
518 audit_log_untrustedstring(ab, rule->tree->pathname);
519 audit_log_key(ab, rule->filterkey);
520 audit_log_format(ab, " list=%d res=1", rule->listnr);
521 audit_log_end(ab);
522}
523
Al Viro74c3cbe2007-07-22 08:04:18 -0400524static void kill_rules(struct audit_tree *tree)
525{
526 struct audit_krule *rule, *next;
527 struct audit_entry *entry;
Al Viro74c3cbe2007-07-22 08:04:18 -0400528
529 list_for_each_entry_safe(rule, next, &tree->rules, rlist) {
530 entry = container_of(rule, struct audit_entry, rule);
531
532 list_del_init(&rule->rlist);
533 if (rule->tree) {
534 /* not a half-baked one */
Richard Guy Briggs2991dd22014-10-02 22:05:24 -0400535 audit_tree_log_remove_rule(rule);
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400536 if (entry->rule.exe)
537 audit_remove_mark(entry->rule.exe);
Al Viro74c3cbe2007-07-22 08:04:18 -0400538 rule->tree = NULL;
539 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -0500540 list_del(&entry->rule.list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400541 call_rcu(&entry->rcu, audit_free_rule_rcu);
542 }
543 }
544}
545
546/*
547 * finish killing struct audit_tree
548 */
549static void prune_one(struct audit_tree *victim)
550{
551 spin_lock(&hash_lock);
552 while (!list_empty(&victim->chunks)) {
553 struct node *p;
Al Viro74c3cbe2007-07-22 08:04:18 -0400554
555 p = list_entry(victim->chunks.next, struct node, list);
Al Viro74c3cbe2007-07-22 08:04:18 -0400556
Al Viro8f7b0ba2008-11-15 01:15:43 +0000557 untag_chunk(p);
Al Viro74c3cbe2007-07-22 08:04:18 -0400558 }
559 spin_unlock(&hash_lock);
560 put_tree(victim);
561}
562
563/* trim the uncommitted chunks from tree */
564
565static void trim_marked(struct audit_tree *tree)
566{
567 struct list_head *p, *q;
568 spin_lock(&hash_lock);
569 if (tree->goner) {
570 spin_unlock(&hash_lock);
571 return;
572 }
573 /* reorder */
574 for (p = tree->chunks.next; p != &tree->chunks; p = q) {
575 struct node *node = list_entry(p, struct node, list);
576 q = p->next;
577 if (node->index & (1U<<31)) {
578 list_del_init(p);
579 list_add(p, &tree->chunks);
580 }
581 }
582
583 while (!list_empty(&tree->chunks)) {
584 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400585
586 node = list_entry(tree->chunks.next, struct node, list);
587
588 /* have we run out of marked? */
589 if (!(node->index & (1U<<31)))
590 break;
591
Al Viro8f7b0ba2008-11-15 01:15:43 +0000592 untag_chunk(node);
Al Viro74c3cbe2007-07-22 08:04:18 -0400593 }
594 if (!tree->root && !tree->goner) {
595 tree->goner = 1;
596 spin_unlock(&hash_lock);
597 mutex_lock(&audit_filter_mutex);
598 kill_rules(tree);
599 list_del_init(&tree->list);
600 mutex_unlock(&audit_filter_mutex);
601 prune_one(tree);
602 } else {
603 spin_unlock(&hash_lock);
604 }
605}
606
Al Viro916d7572009-06-24 00:02:38 -0400607static void audit_schedule_prune(void);
608
Al Viro74c3cbe2007-07-22 08:04:18 -0400609/* called with audit_filter_mutex */
610int audit_remove_tree_rule(struct audit_krule *rule)
611{
612 struct audit_tree *tree;
613 tree = rule->tree;
614 if (tree) {
615 spin_lock(&hash_lock);
616 list_del_init(&rule->rlist);
617 if (list_empty(&tree->rules) && !tree->goner) {
618 tree->root = NULL;
619 list_del_init(&tree->same_root);
620 tree->goner = 1;
621 list_move(&tree->list, &prune_list);
622 rule->tree = NULL;
623 spin_unlock(&hash_lock);
624 audit_schedule_prune();
625 return 1;
626 }
627 rule->tree = NULL;
628 spin_unlock(&hash_lock);
629 return 1;
630 }
631 return 0;
632}
633
Al Viro1f707132010-01-30 22:51:25 -0500634static int compare_root(struct vfsmount *mnt, void *arg)
635{
Jan Karaf410ff62016-12-16 10:13:37 +0100636 return inode_to_key(d_backing_inode(mnt->mnt_root)) ==
637 (unsigned long)arg;
Al Viro1f707132010-01-30 22:51:25 -0500638}
639
Al Viro74c3cbe2007-07-22 08:04:18 -0400640void audit_trim_trees(void)
641{
642 struct list_head cursor;
643
644 mutex_lock(&audit_filter_mutex);
645 list_add(&cursor, &tree_list);
646 while (cursor.next != &tree_list) {
647 struct audit_tree *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400648 struct path path;
Al Viro74c3cbe2007-07-22 08:04:18 -0400649 struct vfsmount *root_mnt;
650 struct node *node;
Al Viro74c3cbe2007-07-22 08:04:18 -0400651 int err;
652
653 tree = container_of(cursor.next, struct audit_tree, list);
654 get_tree(tree);
655 list_del(&cursor);
656 list_add(&cursor, &tree->list);
657 mutex_unlock(&audit_filter_mutex);
658
Al Viro98bc9932008-08-02 01:06:21 -0400659 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400660 if (err)
661 goto skip_it;
662
Al Viro589ff872009-04-18 03:28:19 -0400663 root_mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400664 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100665 if (IS_ERR(root_mnt))
Al Viro74c3cbe2007-07-22 08:04:18 -0400666 goto skip_it;
667
Al Viro74c3cbe2007-07-22 08:04:18 -0400668 spin_lock(&hash_lock);
669 list_for_each_entry(node, &tree->chunks, list) {
Eric Paris28a3a7e2009-12-17 20:12:05 -0500670 struct audit_chunk *chunk = find_chunk(node);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300671 /* this could be NULL if the watch is dying else where... */
Al Viro74c3cbe2007-07-22 08:04:18 -0400672 node->index |= 1U<<31;
Jan Karaf410ff62016-12-16 10:13:37 +0100673 if (iterate_mounts(compare_root,
Jan Kara8d20d6e2018-11-12 09:54:48 -0500674 (void *)(chunk->key),
Jan Karaf410ff62016-12-16 10:13:37 +0100675 root_mnt))
Al Viro1f707132010-01-30 22:51:25 -0500676 node->index &= ~(1U<<31);
Al Viro74c3cbe2007-07-22 08:04:18 -0400677 }
678 spin_unlock(&hash_lock);
679 trim_marked(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400680 drop_collected_mounts(root_mnt);
681skip_it:
Chen Gang12b2f112013-04-29 15:05:19 -0700682 put_tree(tree);
Al Viro74c3cbe2007-07-22 08:04:18 -0400683 mutex_lock(&audit_filter_mutex);
684 }
685 list_del(&cursor);
686 mutex_unlock(&audit_filter_mutex);
687}
688
Al Viro74c3cbe2007-07-22 08:04:18 -0400689int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op)
690{
691
692 if (pathname[0] != '/' ||
693 rule->listnr != AUDIT_FILTER_EXIT ||
Al Viro5af75d82008-12-16 05:59:26 -0500694 op != Audit_equal ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400695 rule->inode_f || rule->watch || rule->tree)
696 return -EINVAL;
697 rule->tree = alloc_tree(pathname);
698 if (!rule->tree)
699 return -ENOMEM;
700 return 0;
701}
702
703void audit_put_tree(struct audit_tree *tree)
704{
705 put_tree(tree);
706}
707
Al Viro1f707132010-01-30 22:51:25 -0500708static int tag_mount(struct vfsmount *mnt, void *arg)
709{
David Howells3b362152015-03-17 22:26:21 +0000710 return tag_chunk(d_backing_inode(mnt->mnt_root), arg);
Al Viro1f707132010-01-30 22:51:25 -0500711}
712
Imre Palikf1aaf262015-02-23 15:37:59 -0500713/*
714 * That gets run when evict_chunk() ends up needing to kill audit_tree.
715 * Runs from a separate thread.
716 */
717static int prune_tree_thread(void *unused)
718{
719 for (;;) {
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200720 if (list_empty(&prune_list)) {
721 set_current_state(TASK_INTERRUPTIBLE);
Imre Palikf1aaf262015-02-23 15:37:59 -0500722 schedule();
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200723 }
Imre Palikf1aaf262015-02-23 15:37:59 -0500724
Paul Moorece423632018-02-20 09:52:38 -0500725 audit_ctl_lock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500726 mutex_lock(&audit_filter_mutex);
727
728 while (!list_empty(&prune_list)) {
729 struct audit_tree *victim;
730
731 victim = list_entry(prune_list.next,
732 struct audit_tree, list);
733 list_del_init(&victim->list);
734
735 mutex_unlock(&audit_filter_mutex);
736
737 prune_one(victim);
738
739 mutex_lock(&audit_filter_mutex);
740 }
741
742 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500743 audit_ctl_unlock();
Imre Palikf1aaf262015-02-23 15:37:59 -0500744 }
745 return 0;
746}
747
748static int audit_launch_prune(void)
749{
750 if (prune_thread)
751 return 0;
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200752 prune_thread = kthread_run(prune_tree_thread, NULL,
Imre Palikf1aaf262015-02-23 15:37:59 -0500753 "audit_prune_tree");
754 if (IS_ERR(prune_thread)) {
755 pr_err("cannot start thread audit_prune_tree");
756 prune_thread = NULL;
757 return -ENOMEM;
Imre Palikf1aaf262015-02-23 15:37:59 -0500758 }
Jiri Slaby0bf676d2016-03-31 10:49:28 +0200759 return 0;
Imre Palikf1aaf262015-02-23 15:37:59 -0500760}
761
Al Viro74c3cbe2007-07-22 08:04:18 -0400762/* called with audit_filter_mutex */
763int audit_add_tree_rule(struct audit_krule *rule)
764{
765 struct audit_tree *seed = rule->tree, *tree;
Al Viro98bc9932008-08-02 01:06:21 -0400766 struct path path;
Al Viro1f707132010-01-30 22:51:25 -0500767 struct vfsmount *mnt;
Al Viro74c3cbe2007-07-22 08:04:18 -0400768 int err;
769
Chen Gang736f3202013-06-12 14:05:07 -0700770 rule->tree = NULL;
Al Viro74c3cbe2007-07-22 08:04:18 -0400771 list_for_each_entry(tree, &tree_list, list) {
772 if (!strcmp(seed->pathname, tree->pathname)) {
773 put_tree(seed);
774 rule->tree = tree;
775 list_add(&rule->rlist, &tree->rules);
776 return 0;
777 }
778 }
779 tree = seed;
780 list_add(&tree->list, &tree_list);
781 list_add(&rule->rlist, &tree->rules);
782 /* do not set rule->tree yet */
783 mutex_unlock(&audit_filter_mutex);
784
Imre Palikf1aaf262015-02-23 15:37:59 -0500785 if (unlikely(!prune_thread)) {
786 err = audit_launch_prune();
787 if (err)
788 goto Err;
789 }
790
Al Viro98bc9932008-08-02 01:06:21 -0400791 err = kern_path(tree->pathname, 0, &path);
Al Viro74c3cbe2007-07-22 08:04:18 -0400792 if (err)
793 goto Err;
Al Viro589ff872009-04-18 03:28:19 -0400794 mnt = collect_mounts(&path);
Al Viro98bc9932008-08-02 01:06:21 -0400795 path_put(&path);
David Howellsbe34d1a2012-06-25 12:55:18 +0100796 if (IS_ERR(mnt)) {
797 err = PTR_ERR(mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400798 goto Err;
799 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400800
801 get_tree(tree);
Al Viro1f707132010-01-30 22:51:25 -0500802 err = iterate_mounts(tag_mount, tree, mnt);
Al Viro74c3cbe2007-07-22 08:04:18 -0400803 drop_collected_mounts(mnt);
804
805 if (!err) {
806 struct node *node;
807 spin_lock(&hash_lock);
808 list_for_each_entry(node, &tree->chunks, list)
809 node->index &= ~(1U<<31);
810 spin_unlock(&hash_lock);
811 } else {
812 trim_marked(tree);
813 goto Err;
814 }
815
816 mutex_lock(&audit_filter_mutex);
817 if (list_empty(&rule->rlist)) {
818 put_tree(tree);
819 return -ENOENT;
820 }
821 rule->tree = tree;
822 put_tree(tree);
823
824 return 0;
825Err:
826 mutex_lock(&audit_filter_mutex);
827 list_del_init(&tree->list);
828 list_del_init(&tree->rules);
829 put_tree(tree);
830 return err;
831}
832
833int audit_tag_tree(char *old, char *new)
834{
835 struct list_head cursor, barrier;
836 int failed = 0;
Al Viro2096f752010-01-30 13:16:21 -0500837 struct path path1, path2;
Al Viro74c3cbe2007-07-22 08:04:18 -0400838 struct vfsmount *tagged;
Al Viro74c3cbe2007-07-22 08:04:18 -0400839 int err;
840
Al Viro2096f752010-01-30 13:16:21 -0500841 err = kern_path(new, 0, &path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400842 if (err)
843 return err;
Al Viro2096f752010-01-30 13:16:21 -0500844 tagged = collect_mounts(&path2);
845 path_put(&path2);
David Howellsbe34d1a2012-06-25 12:55:18 +0100846 if (IS_ERR(tagged))
847 return PTR_ERR(tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400848
Al Viro2096f752010-01-30 13:16:21 -0500849 err = kern_path(old, 0, &path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400850 if (err) {
851 drop_collected_mounts(tagged);
852 return err;
853 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400854
Al Viro74c3cbe2007-07-22 08:04:18 -0400855 mutex_lock(&audit_filter_mutex);
856 list_add(&barrier, &tree_list);
857 list_add(&cursor, &barrier);
858
859 while (cursor.next != &tree_list) {
860 struct audit_tree *tree;
Al Viro2096f752010-01-30 13:16:21 -0500861 int good_one = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400862
863 tree = container_of(cursor.next, struct audit_tree, list);
864 get_tree(tree);
865 list_del(&cursor);
866 list_add(&cursor, &tree->list);
867 mutex_unlock(&audit_filter_mutex);
868
Al Viro2096f752010-01-30 13:16:21 -0500869 err = kern_path(tree->pathname, 0, &path2);
870 if (!err) {
871 good_one = path_is_under(&path1, &path2);
872 path_put(&path2);
Al Viro74c3cbe2007-07-22 08:04:18 -0400873 }
874
Al Viro2096f752010-01-30 13:16:21 -0500875 if (!good_one) {
Al Viro74c3cbe2007-07-22 08:04:18 -0400876 put_tree(tree);
877 mutex_lock(&audit_filter_mutex);
878 continue;
879 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400880
Al Viro1f707132010-01-30 22:51:25 -0500881 failed = iterate_mounts(tag_mount, tree, tagged);
Al Viro74c3cbe2007-07-22 08:04:18 -0400882 if (failed) {
883 put_tree(tree);
884 mutex_lock(&audit_filter_mutex);
885 break;
886 }
887
888 mutex_lock(&audit_filter_mutex);
889 spin_lock(&hash_lock);
890 if (!tree->goner) {
891 list_del(&tree->list);
892 list_add(&tree->list, &tree_list);
893 }
894 spin_unlock(&hash_lock);
895 put_tree(tree);
896 }
897
898 while (barrier.prev != &tree_list) {
899 struct audit_tree *tree;
900
901 tree = container_of(barrier.prev, struct audit_tree, list);
902 get_tree(tree);
903 list_del(&tree->list);
904 list_add(&tree->list, &barrier);
905 mutex_unlock(&audit_filter_mutex);
906
907 if (!failed) {
908 struct node *node;
909 spin_lock(&hash_lock);
910 list_for_each_entry(node, &tree->chunks, list)
911 node->index &= ~(1U<<31);
912 spin_unlock(&hash_lock);
913 } else {
914 trim_marked(tree);
915 }
916
917 put_tree(tree);
918 mutex_lock(&audit_filter_mutex);
919 }
920 list_del(&barrier);
921 list_del(&cursor);
Al Viro74c3cbe2007-07-22 08:04:18 -0400922 mutex_unlock(&audit_filter_mutex);
Al Viro2096f752010-01-30 13:16:21 -0500923 path_put(&path1);
Al Viro74c3cbe2007-07-22 08:04:18 -0400924 drop_collected_mounts(tagged);
925 return failed;
926}
927
Al Viro916d7572009-06-24 00:02:38 -0400928
929static void audit_schedule_prune(void)
930{
Imre Palikf1aaf262015-02-23 15:37:59 -0500931 wake_up_process(prune_thread);
Al Viro916d7572009-06-24 00:02:38 -0400932}
933
934/*
935 * ... and that one is done if evict_chunk() decides to delay until the end
936 * of syscall. Runs synchronously.
937 */
938void audit_kill_trees(struct list_head *list)
939{
Paul Moorece423632018-02-20 09:52:38 -0500940 audit_ctl_lock();
Al Viro916d7572009-06-24 00:02:38 -0400941 mutex_lock(&audit_filter_mutex);
942
943 while (!list_empty(list)) {
944 struct audit_tree *victim;
945
946 victim = list_entry(list->next, struct audit_tree, list);
947 kill_rules(victim);
948 list_del_init(&victim->list);
949
950 mutex_unlock(&audit_filter_mutex);
951
952 prune_one(victim);
953
954 mutex_lock(&audit_filter_mutex);
955 }
956
957 mutex_unlock(&audit_filter_mutex);
Paul Moorece423632018-02-20 09:52:38 -0500958 audit_ctl_unlock();
Al Viro74c3cbe2007-07-22 08:04:18 -0400959}
960
961/*
962 * Here comes the stuff asynchronous to auditctl operations
963 */
964
Al Viro74c3cbe2007-07-22 08:04:18 -0400965static void evict_chunk(struct audit_chunk *chunk)
966{
967 struct audit_tree *owner;
Al Viro916d7572009-06-24 00:02:38 -0400968 struct list_head *postponed = audit_killed_trees();
969 int need_prune = 0;
Al Viro74c3cbe2007-07-22 08:04:18 -0400970 int n;
971
972 if (chunk->dead)
973 return;
974
975 chunk->dead = 1;
976 mutex_lock(&audit_filter_mutex);
977 spin_lock(&hash_lock);
978 while (!list_empty(&chunk->trees)) {
979 owner = list_entry(chunk->trees.next,
980 struct audit_tree, same_root);
981 owner->goner = 1;
982 owner->root = NULL;
983 list_del_init(&owner->same_root);
984 spin_unlock(&hash_lock);
Al Viro916d7572009-06-24 00:02:38 -0400985 if (!postponed) {
986 kill_rules(owner);
987 list_move(&owner->list, &prune_list);
988 need_prune = 1;
989 } else {
990 list_move(&owner->list, postponed);
991 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400992 spin_lock(&hash_lock);
993 }
994 list_del_rcu(&chunk->hash);
995 for (n = 0; n < chunk->count; n++)
996 list_del_init(&chunk->owners[n].list);
997 spin_unlock(&hash_lock);
Imre Palikf1aaf262015-02-23 15:37:59 -0500998 mutex_unlock(&audit_filter_mutex);
Al Viro916d7572009-06-24 00:02:38 -0400999 if (need_prune)
1000 audit_schedule_prune();
Al Viro74c3cbe2007-07-22 08:04:18 -04001001}
1002
Eric Paris3a9b16b2010-07-28 10:18:38 -04001003static int audit_tree_handle_event(struct fsnotify_group *group,
Jan Kara7053aee2014-01-21 15:48:14 -08001004 struct inode *to_tell,
Al Viro3cd5eca2016-11-20 20:19:09 -05001005 u32 mask, const void *data, int data_type,
Jan Kara9385a842016-11-10 17:51:50 +01001006 const unsigned char *file_name, u32 cookie,
1007 struct fsnotify_iter_info *iter_info)
Al Viro74c3cbe2007-07-22 08:04:18 -04001008{
Jan Kara83c4c4b2014-01-21 15:48:15 -08001009 return 0;
Al Viro74c3cbe2007-07-22 08:04:18 -04001010}
1011
Eric Parise61ce862009-12-17 21:24:24 -05001012static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group)
Al Viro74c3cbe2007-07-22 08:04:18 -04001013{
Eric Paris28a3a7e2009-12-17 20:12:05 -05001014 struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark);
1015
1016 evict_chunk(chunk);
Miklos Szeredib3e86922012-08-15 12:55:22 +02001017
1018 /*
1019 * We are guaranteed to have at least one reference to the mark from
1020 * either the inode or the caller of fsnotify_destroy_mark().
1021 */
Elena Reshetovaab97f8732017-10-20 13:26:02 +03001022 BUG_ON(refcount_read(&entry->refcnt) < 1);
Al Viro74c3cbe2007-07-22 08:04:18 -04001023}
1024
Eric Paris28a3a7e2009-12-17 20:12:05 -05001025static const struct fsnotify_ops audit_tree_ops = {
1026 .handle_event = audit_tree_handle_event,
Eric Paris28a3a7e2009-12-17 20:12:05 -05001027 .freeing_mark = audit_tree_freeing_mark,
Jan Kara054c6362016-12-21 18:06:12 +01001028 .free_mark = audit_tree_destroy_watch,
Al Viro74c3cbe2007-07-22 08:04:18 -04001029};
1030
1031static int __init audit_tree_init(void)
1032{
1033 int i;
1034
Eric Paris0d2e2a12009-12-17 21:24:22 -05001035 audit_tree_group = fsnotify_alloc_group(&audit_tree_ops);
Eric Paris28a3a7e2009-12-17 20:12:05 -05001036 if (IS_ERR(audit_tree_group))
1037 audit_panic("cannot initialize fsnotify group for rectree watches");
Al Viro74c3cbe2007-07-22 08:04:18 -04001038
1039 for (i = 0; i < HASH_SIZE; i++)
1040 INIT_LIST_HEAD(&chunk_hash_heads[i]);
1041
1042 return 0;
1043}
1044__initcall(audit_tree_init);