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