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