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