]>
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. | |
86ffe245 JK |
175 | * |
176 | * Must be called with chunk->mark.lock held to protect from connector | |
177 | * becoming NULL. | |
f410ff65 | 178 | */ |
86ffe245 JK |
179 | static unsigned long __chunk_to_key(struct audit_chunk *chunk) |
180 | { | |
181 | if (!chunk->mark.connector) | |
182 | return 0; | |
183 | return (unsigned long)chunk->mark.connector->inode; | |
184 | } | |
185 | ||
f410ff65 JK |
186 | static unsigned long chunk_to_key(struct audit_chunk *chunk) |
187 | { | |
86ffe245 JK |
188 | unsigned long key; |
189 | ||
190 | spin_lock(&chunk->mark.lock); | |
191 | key = __chunk_to_key(chunk); | |
192 | spin_unlock(&chunk->mark.lock); | |
193 | return key; | |
f410ff65 JK |
194 | } |
195 | ||
196 | static inline struct list_head *chunk_hash(unsigned long key) | |
197 | { | |
198 | unsigned long n = key / L1_CACHE_BYTES; | |
74c3cbe3 AV |
199 | return chunk_hash_heads + n % HASH_SIZE; |
200 | } | |
201 | ||
28a3a7eb | 202 | /* hash_lock & entry->lock is held by caller */ |
74c3cbe3 AV |
203 | static void insert_hash(struct audit_chunk *chunk) |
204 | { | |
86ffe245 | 205 | unsigned long key = __chunk_to_key(chunk); |
28a3a7eb EP |
206 | struct list_head *list; |
207 | ||
43471d15 | 208 | if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED)) |
28a3a7eb | 209 | return; |
f410ff65 | 210 | list = chunk_hash(key); |
74c3cbe3 AV |
211 | list_add_rcu(&chunk->hash, list); |
212 | } | |
213 | ||
214 | /* called under rcu_read_lock */ | |
215 | struct audit_chunk *audit_tree_lookup(const struct inode *inode) | |
216 | { | |
f410ff65 JK |
217 | unsigned long key = inode_to_key(inode); |
218 | struct list_head *list = chunk_hash(key); | |
6793a051 | 219 | struct audit_chunk *p; |
74c3cbe3 | 220 | |
6793a051 | 221 | list_for_each_entry_rcu(p, list, hash) { |
f410ff65 | 222 | if (chunk_to_key(p) == key) { |
8f7b0ba1 | 223 | atomic_long_inc(&p->refs); |
74c3cbe3 AV |
224 | return p; |
225 | } | |
226 | } | |
227 | return NULL; | |
228 | } | |
229 | ||
6f1b5d7a | 230 | bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree) |
74c3cbe3 AV |
231 | { |
232 | int n; | |
233 | for (n = 0; n < chunk->count; n++) | |
234 | if (chunk->owners[n].owner == tree) | |
6f1b5d7a YB |
235 | return true; |
236 | return false; | |
74c3cbe3 AV |
237 | } |
238 | ||
239 | /* tagging and untagging inodes with trees */ | |
240 | ||
8f7b0ba1 AV |
241 | static struct audit_chunk *find_chunk(struct node *p) |
242 | { | |
243 | int index = p->index & ~(1U<<31); | |
244 | p -= index; | |
245 | return container_of(p, struct audit_chunk, owners[0]); | |
246 | } | |
247 | ||
248 | static void untag_chunk(struct node *p) | |
74c3cbe3 | 249 | { |
8f7b0ba1 | 250 | struct audit_chunk *chunk = find_chunk(p); |
e61ce867 | 251 | struct fsnotify_mark *entry = &chunk->mark; |
f7a998a9 | 252 | struct audit_chunk *new = NULL; |
74c3cbe3 AV |
253 | struct audit_tree *owner; |
254 | int size = chunk->count - 1; | |
255 | int i, j; | |
256 | ||
28a3a7eb | 257 | fsnotify_get_mark(entry); |
8f7b0ba1 AV |
258 | |
259 | spin_unlock(&hash_lock); | |
260 | ||
f7a998a9 AV |
261 | if (size) |
262 | new = alloc_chunk(size); | |
263 | ||
be29d20f | 264 | mutex_lock(&entry->group->mark_mutex); |
28a3a7eb | 265 | spin_lock(&entry->lock); |
43471d15 | 266 | if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
28a3a7eb | 267 | spin_unlock(&entry->lock); |
be29d20f | 268 | mutex_unlock(&entry->group->mark_mutex); |
f7a998a9 AV |
269 | if (new) |
270 | free_chunk(new); | |
8f7b0ba1 | 271 | goto out; |
74c3cbe3 AV |
272 | } |
273 | ||
274 | owner = p->owner; | |
275 | ||
276 | if (!size) { | |
277 | chunk->dead = 1; | |
278 | spin_lock(&hash_lock); | |
279 | list_del_init(&chunk->trees); | |
280 | if (owner->root == chunk) | |
281 | owner->root = NULL; | |
282 | list_del_init(&p->list); | |
283 | list_del_rcu(&chunk->hash); | |
284 | spin_unlock(&hash_lock); | |
28a3a7eb | 285 | spin_unlock(&entry->lock); |
be29d20f | 286 | mutex_unlock(&entry->group->mark_mutex); |
e2a29943 | 287 | fsnotify_destroy_mark(entry, audit_tree_group); |
8f7b0ba1 | 288 | goto out; |
74c3cbe3 AV |
289 | } |
290 | ||
74c3cbe3 AV |
291 | if (!new) |
292 | goto Fallback; | |
f7a998a9 | 293 | |
86ffe245 JK |
294 | if (fsnotify_add_mark_locked(&new->mark, entry->group, |
295 | entry->connector->inode, NULL, 1)) { | |
0fe33aae | 296 | fsnotify_put_mark(&new->mark); |
74c3cbe3 AV |
297 | goto Fallback; |
298 | } | |
299 | ||
300 | chunk->dead = 1; | |
301 | spin_lock(&hash_lock); | |
302 | list_replace_init(&chunk->trees, &new->trees); | |
303 | if (owner->root == chunk) { | |
304 | list_del_init(&owner->same_root); | |
305 | owner->root = NULL; | |
306 | } | |
307 | ||
6f5d5114 | 308 | for (i = j = 0; j <= size; i++, j++) { |
74c3cbe3 AV |
309 | struct audit_tree *s; |
310 | if (&chunk->owners[j] == p) { | |
311 | list_del_init(&p->list); | |
312 | i--; | |
313 | continue; | |
314 | } | |
315 | s = chunk->owners[j].owner; | |
316 | new->owners[i].owner = s; | |
317 | new->owners[i].index = chunk->owners[j].index - j + i; | |
318 | if (!s) /* result of earlier fallback */ | |
319 | continue; | |
320 | get_tree(s); | |
6f5d5114 | 321 | list_replace_init(&chunk->owners[j].list, &new->owners[i].list); |
74c3cbe3 AV |
322 | } |
323 | ||
324 | list_replace_rcu(&chunk->hash, &new->hash); | |
325 | list_for_each_entry(owner, &new->trees, same_root) | |
326 | owner->root = new; | |
327 | spin_unlock(&hash_lock); | |
28a3a7eb | 328 | spin_unlock(&entry->lock); |
be29d20f | 329 | mutex_unlock(&entry->group->mark_mutex); |
e2a29943 | 330 | fsnotify_destroy_mark(entry, audit_tree_group); |
b3e8692b | 331 | fsnotify_put_mark(&new->mark); /* drop initial reference */ |
8f7b0ba1 | 332 | goto out; |
74c3cbe3 AV |
333 | |
334 | Fallback: | |
335 | // do the best we can | |
336 | spin_lock(&hash_lock); | |
337 | if (owner->root == chunk) { | |
338 | list_del_init(&owner->same_root); | |
339 | owner->root = NULL; | |
340 | } | |
341 | list_del_init(&p->list); | |
342 | p->owner = NULL; | |
343 | put_tree(owner); | |
344 | spin_unlock(&hash_lock); | |
28a3a7eb | 345 | spin_unlock(&entry->lock); |
be29d20f | 346 | mutex_unlock(&entry->group->mark_mutex); |
8f7b0ba1 | 347 | out: |
28a3a7eb | 348 | fsnotify_put_mark(entry); |
8f7b0ba1 | 349 | spin_lock(&hash_lock); |
74c3cbe3 AV |
350 | } |
351 | ||
352 | static int create_chunk(struct inode *inode, struct audit_tree *tree) | |
353 | { | |
e61ce867 | 354 | struct fsnotify_mark *entry; |
74c3cbe3 AV |
355 | struct audit_chunk *chunk = alloc_chunk(1); |
356 | if (!chunk) | |
357 | return -ENOMEM; | |
358 | ||
28a3a7eb | 359 | entry = &chunk->mark; |
5444e298 | 360 | if (fsnotify_add_mark(entry, audit_tree_group, inode, NULL, 0)) { |
0fe33aae | 361 | fsnotify_put_mark(entry); |
74c3cbe3 AV |
362 | return -ENOSPC; |
363 | } | |
364 | ||
28a3a7eb | 365 | spin_lock(&entry->lock); |
74c3cbe3 AV |
366 | spin_lock(&hash_lock); |
367 | if (tree->goner) { | |
368 | spin_unlock(&hash_lock); | |
369 | chunk->dead = 1; | |
28a3a7eb | 370 | spin_unlock(&entry->lock); |
e2a29943 | 371 | fsnotify_destroy_mark(entry, audit_tree_group); |
28a3a7eb | 372 | fsnotify_put_mark(entry); |
74c3cbe3 AV |
373 | return 0; |
374 | } | |
375 | chunk->owners[0].index = (1U << 31); | |
376 | chunk->owners[0].owner = tree; | |
377 | get_tree(tree); | |
378 | list_add(&chunk->owners[0].list, &tree->chunks); | |
379 | if (!tree->root) { | |
380 | tree->root = chunk; | |
381 | list_add(&tree->same_root, &chunk->trees); | |
382 | } | |
383 | insert_hash(chunk); | |
384 | spin_unlock(&hash_lock); | |
28a3a7eb | 385 | spin_unlock(&entry->lock); |
b3e8692b | 386 | fsnotify_put_mark(entry); /* drop initial reference */ |
74c3cbe3 AV |
387 | return 0; |
388 | } | |
389 | ||
390 | /* the first tagged inode becomes root of tree */ | |
391 | static int tag_chunk(struct inode *inode, struct audit_tree *tree) | |
392 | { | |
e61ce867 | 393 | struct fsnotify_mark *old_entry, *chunk_entry; |
74c3cbe3 AV |
394 | struct audit_tree *owner; |
395 | struct audit_chunk *chunk, *old; | |
396 | struct node *p; | |
397 | int n; | |
398 | ||
5444e298 | 399 | old_entry = fsnotify_find_inode_mark(audit_tree_group, inode); |
28a3a7eb | 400 | if (!old_entry) |
74c3cbe3 AV |
401 | return create_chunk(inode, tree); |
402 | ||
28a3a7eb | 403 | old = container_of(old_entry, struct audit_chunk, mark); |
74c3cbe3 AV |
404 | |
405 | /* are we already there? */ | |
406 | spin_lock(&hash_lock); | |
407 | for (n = 0; n < old->count; n++) { | |
408 | if (old->owners[n].owner == tree) { | |
409 | spin_unlock(&hash_lock); | |
28a3a7eb | 410 | fsnotify_put_mark(old_entry); |
74c3cbe3 AV |
411 | return 0; |
412 | } | |
413 | } | |
414 | spin_unlock(&hash_lock); | |
415 | ||
416 | chunk = alloc_chunk(old->count + 1); | |
b4c30aad | 417 | if (!chunk) { |
28a3a7eb | 418 | fsnotify_put_mark(old_entry); |
74c3cbe3 | 419 | return -ENOMEM; |
b4c30aad | 420 | } |
74c3cbe3 | 421 | |
28a3a7eb EP |
422 | chunk_entry = &chunk->mark; |
423 | ||
be29d20f | 424 | mutex_lock(&old_entry->group->mark_mutex); |
28a3a7eb | 425 | spin_lock(&old_entry->lock); |
43471d15 | 426 | if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
28a3a7eb EP |
427 | /* old_entry is being shot, lets just lie */ |
428 | spin_unlock(&old_entry->lock); | |
be29d20f | 429 | mutex_unlock(&old_entry->group->mark_mutex); |
28a3a7eb | 430 | fsnotify_put_mark(old_entry); |
74c3cbe3 | 431 | free_chunk(chunk); |
28a3a7eb EP |
432 | return -ENOENT; |
433 | } | |
434 | ||
be29d20f | 435 | if (fsnotify_add_mark_locked(chunk_entry, old_entry->group, |
86ffe245 | 436 | old_entry->connector->inode, NULL, 1)) { |
28a3a7eb | 437 | spin_unlock(&old_entry->lock); |
be29d20f | 438 | mutex_unlock(&old_entry->group->mark_mutex); |
0fe33aae | 439 | fsnotify_put_mark(chunk_entry); |
28a3a7eb | 440 | fsnotify_put_mark(old_entry); |
74c3cbe3 AV |
441 | return -ENOSPC; |
442 | } | |
28a3a7eb EP |
443 | |
444 | /* even though we hold old_entry->lock, this is safe since chunk_entry->lock could NEVER have been grabbed before */ | |
445 | spin_lock(&chunk_entry->lock); | |
74c3cbe3 | 446 | spin_lock(&hash_lock); |
28a3a7eb EP |
447 | |
448 | /* we now hold old_entry->lock, chunk_entry->lock, and hash_lock */ | |
74c3cbe3 AV |
449 | if (tree->goner) { |
450 | spin_unlock(&hash_lock); | |
451 | chunk->dead = 1; | |
28a3a7eb EP |
452 | spin_unlock(&chunk_entry->lock); |
453 | spin_unlock(&old_entry->lock); | |
be29d20f | 454 | mutex_unlock(&old_entry->group->mark_mutex); |
28a3a7eb | 455 | |
e2a29943 | 456 | fsnotify_destroy_mark(chunk_entry, audit_tree_group); |
28a3a7eb EP |
457 | |
458 | fsnotify_put_mark(chunk_entry); | |
459 | fsnotify_put_mark(old_entry); | |
74c3cbe3 AV |
460 | return 0; |
461 | } | |
462 | list_replace_init(&old->trees, &chunk->trees); | |
463 | for (n = 0, p = chunk->owners; n < old->count; n++, p++) { | |
464 | struct audit_tree *s = old->owners[n].owner; | |
465 | p->owner = s; | |
466 | p->index = old->owners[n].index; | |
467 | if (!s) /* result of fallback in untag */ | |
468 | continue; | |
469 | get_tree(s); | |
470 | list_replace_init(&old->owners[n].list, &p->list); | |
471 | } | |
472 | p->index = (chunk->count - 1) | (1U<<31); | |
473 | p->owner = tree; | |
474 | get_tree(tree); | |
475 | list_add(&p->list, &tree->chunks); | |
476 | list_replace_rcu(&old->hash, &chunk->hash); | |
477 | list_for_each_entry(owner, &chunk->trees, same_root) | |
478 | owner->root = chunk; | |
479 | old->dead = 1; | |
480 | if (!tree->root) { | |
481 | tree->root = chunk; | |
482 | list_add(&tree->same_root, &chunk->trees); | |
483 | } | |
484 | spin_unlock(&hash_lock); | |
28a3a7eb EP |
485 | spin_unlock(&chunk_entry->lock); |
486 | spin_unlock(&old_entry->lock); | |
be29d20f | 487 | mutex_unlock(&old_entry->group->mark_mutex); |
e2a29943 | 488 | fsnotify_destroy_mark(old_entry, audit_tree_group); |
b3e8692b | 489 | fsnotify_put_mark(chunk_entry); /* drop initial reference */ |
28a3a7eb | 490 | fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */ |
74c3cbe3 AV |
491 | return 0; |
492 | } | |
493 | ||
2991dd2b | 494 | static void audit_tree_log_remove_rule(struct audit_krule *rule) |
0644ec0c KC |
495 | { |
496 | struct audit_buffer *ab; | |
497 | ||
498 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); | |
499 | if (unlikely(!ab)) | |
500 | return; | |
c1e8f06d | 501 | audit_log_format(ab, "op=remove_rule"); |
0644ec0c KC |
502 | audit_log_format(ab, " dir="); |
503 | audit_log_untrustedstring(ab, rule->tree->pathname); | |
504 | audit_log_key(ab, rule->filterkey); | |
505 | audit_log_format(ab, " list=%d res=1", rule->listnr); | |
506 | audit_log_end(ab); | |
507 | } | |
508 | ||
74c3cbe3 AV |
509 | static void kill_rules(struct audit_tree *tree) |
510 | { | |
511 | struct audit_krule *rule, *next; | |
512 | struct audit_entry *entry; | |
74c3cbe3 AV |
513 | |
514 | list_for_each_entry_safe(rule, next, &tree->rules, rlist) { | |
515 | entry = container_of(rule, struct audit_entry, rule); | |
516 | ||
517 | list_del_init(&rule->rlist); | |
518 | if (rule->tree) { | |
519 | /* not a half-baked one */ | |
2991dd2b | 520 | audit_tree_log_remove_rule(rule); |
34d99af5 RGB |
521 | if (entry->rule.exe) |
522 | audit_remove_mark(entry->rule.exe); | |
74c3cbe3 AV |
523 | rule->tree = NULL; |
524 | list_del_rcu(&entry->list); | |
e45aa212 | 525 | list_del(&entry->rule.list); |
74c3cbe3 AV |
526 | call_rcu(&entry->rcu, audit_free_rule_rcu); |
527 | } | |
528 | } | |
529 | } | |
530 | ||
531 | /* | |
532 | * finish killing struct audit_tree | |
533 | */ | |
534 | static void prune_one(struct audit_tree *victim) | |
535 | { | |
536 | spin_lock(&hash_lock); | |
537 | while (!list_empty(&victim->chunks)) { | |
538 | struct node *p; | |
74c3cbe3 AV |
539 | |
540 | p = list_entry(victim->chunks.next, struct node, list); | |
74c3cbe3 | 541 | |
8f7b0ba1 | 542 | untag_chunk(p); |
74c3cbe3 AV |
543 | } |
544 | spin_unlock(&hash_lock); | |
545 | put_tree(victim); | |
546 | } | |
547 | ||
548 | /* trim the uncommitted chunks from tree */ | |
549 | ||
550 | static void trim_marked(struct audit_tree *tree) | |
551 | { | |
552 | struct list_head *p, *q; | |
553 | spin_lock(&hash_lock); | |
554 | if (tree->goner) { | |
555 | spin_unlock(&hash_lock); | |
556 | return; | |
557 | } | |
558 | /* reorder */ | |
559 | for (p = tree->chunks.next; p != &tree->chunks; p = q) { | |
560 | struct node *node = list_entry(p, struct node, list); | |
561 | q = p->next; | |
562 | if (node->index & (1U<<31)) { | |
563 | list_del_init(p); | |
564 | list_add(p, &tree->chunks); | |
565 | } | |
566 | } | |
567 | ||
568 | while (!list_empty(&tree->chunks)) { | |
569 | struct node *node; | |
74c3cbe3 AV |
570 | |
571 | node = list_entry(tree->chunks.next, struct node, list); | |
572 | ||
573 | /* have we run out of marked? */ | |
574 | if (!(node->index & (1U<<31))) | |
575 | break; | |
576 | ||
8f7b0ba1 | 577 | untag_chunk(node); |
74c3cbe3 AV |
578 | } |
579 | if (!tree->root && !tree->goner) { | |
580 | tree->goner = 1; | |
581 | spin_unlock(&hash_lock); | |
582 | mutex_lock(&audit_filter_mutex); | |
583 | kill_rules(tree); | |
584 | list_del_init(&tree->list); | |
585 | mutex_unlock(&audit_filter_mutex); | |
586 | prune_one(tree); | |
587 | } else { | |
588 | spin_unlock(&hash_lock); | |
589 | } | |
590 | } | |
591 | ||
916d7576 AV |
592 | static void audit_schedule_prune(void); |
593 | ||
74c3cbe3 AV |
594 | /* called with audit_filter_mutex */ |
595 | int audit_remove_tree_rule(struct audit_krule *rule) | |
596 | { | |
597 | struct audit_tree *tree; | |
598 | tree = rule->tree; | |
599 | if (tree) { | |
600 | spin_lock(&hash_lock); | |
601 | list_del_init(&rule->rlist); | |
602 | if (list_empty(&tree->rules) && !tree->goner) { | |
603 | tree->root = NULL; | |
604 | list_del_init(&tree->same_root); | |
605 | tree->goner = 1; | |
606 | list_move(&tree->list, &prune_list); | |
607 | rule->tree = NULL; | |
608 | spin_unlock(&hash_lock); | |
609 | audit_schedule_prune(); | |
610 | return 1; | |
611 | } | |
612 | rule->tree = NULL; | |
613 | spin_unlock(&hash_lock); | |
614 | return 1; | |
615 | } | |
616 | return 0; | |
617 | } | |
618 | ||
1f707137 AV |
619 | static int compare_root(struct vfsmount *mnt, void *arg) |
620 | { | |
f410ff65 JK |
621 | return inode_to_key(d_backing_inode(mnt->mnt_root)) == |
622 | (unsigned long)arg; | |
1f707137 AV |
623 | } |
624 | ||
74c3cbe3 AV |
625 | void audit_trim_trees(void) |
626 | { | |
627 | struct list_head cursor; | |
628 | ||
629 | mutex_lock(&audit_filter_mutex); | |
630 | list_add(&cursor, &tree_list); | |
631 | while (cursor.next != &tree_list) { | |
632 | struct audit_tree *tree; | |
98bc993f | 633 | struct path path; |
74c3cbe3 AV |
634 | struct vfsmount *root_mnt; |
635 | struct node *node; | |
74c3cbe3 AV |
636 | int err; |
637 | ||
638 | tree = container_of(cursor.next, struct audit_tree, list); | |
639 | get_tree(tree); | |
640 | list_del(&cursor); | |
641 | list_add(&cursor, &tree->list); | |
642 | mutex_unlock(&audit_filter_mutex); | |
643 | ||
98bc993f | 644 | err = kern_path(tree->pathname, 0, &path); |
74c3cbe3 AV |
645 | if (err) |
646 | goto skip_it; | |
647 | ||
589ff870 | 648 | root_mnt = collect_mounts(&path); |
98bc993f | 649 | path_put(&path); |
be34d1a3 | 650 | if (IS_ERR(root_mnt)) |
74c3cbe3 AV |
651 | goto skip_it; |
652 | ||
74c3cbe3 AV |
653 | spin_lock(&hash_lock); |
654 | list_for_each_entry(node, &tree->chunks, list) { | |
28a3a7eb | 655 | struct audit_chunk *chunk = find_chunk(node); |
25985edc | 656 | /* this could be NULL if the watch is dying else where... */ |
74c3cbe3 | 657 | node->index |= 1U<<31; |
f410ff65 JK |
658 | if (iterate_mounts(compare_root, |
659 | (void *)chunk_to_key(chunk), | |
660 | root_mnt)) | |
1f707137 | 661 | node->index &= ~(1U<<31); |
74c3cbe3 AV |
662 | } |
663 | spin_unlock(&hash_lock); | |
664 | trim_marked(tree); | |
74c3cbe3 AV |
665 | drop_collected_mounts(root_mnt); |
666 | skip_it: | |
12b2f117 | 667 | put_tree(tree); |
74c3cbe3 AV |
668 | mutex_lock(&audit_filter_mutex); |
669 | } | |
670 | list_del(&cursor); | |
671 | mutex_unlock(&audit_filter_mutex); | |
672 | } | |
673 | ||
74c3cbe3 AV |
674 | int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op) |
675 | { | |
676 | ||
677 | if (pathname[0] != '/' || | |
678 | rule->listnr != AUDIT_FILTER_EXIT || | |
5af75d8d | 679 | op != Audit_equal || |
74c3cbe3 AV |
680 | rule->inode_f || rule->watch || rule->tree) |
681 | return -EINVAL; | |
682 | rule->tree = alloc_tree(pathname); | |
683 | if (!rule->tree) | |
684 | return -ENOMEM; | |
685 | return 0; | |
686 | } | |
687 | ||
688 | void audit_put_tree(struct audit_tree *tree) | |
689 | { | |
690 | put_tree(tree); | |
691 | } | |
692 | ||
1f707137 AV |
693 | static int tag_mount(struct vfsmount *mnt, void *arg) |
694 | { | |
3b362157 | 695 | return tag_chunk(d_backing_inode(mnt->mnt_root), arg); |
1f707137 AV |
696 | } |
697 | ||
f1aaf262 IP |
698 | /* |
699 | * That gets run when evict_chunk() ends up needing to kill audit_tree. | |
700 | * Runs from a separate thread. | |
701 | */ | |
702 | static int prune_tree_thread(void *unused) | |
703 | { | |
704 | for (;;) { | |
0bf676d1 JS |
705 | if (list_empty(&prune_list)) { |
706 | set_current_state(TASK_INTERRUPTIBLE); | |
f1aaf262 | 707 | schedule(); |
0bf676d1 | 708 | } |
f1aaf262 IP |
709 | |
710 | mutex_lock(&audit_cmd_mutex); | |
711 | mutex_lock(&audit_filter_mutex); | |
712 | ||
713 | while (!list_empty(&prune_list)) { | |
714 | struct audit_tree *victim; | |
715 | ||
716 | victim = list_entry(prune_list.next, | |
717 | struct audit_tree, list); | |
718 | list_del_init(&victim->list); | |
719 | ||
720 | mutex_unlock(&audit_filter_mutex); | |
721 | ||
722 | prune_one(victim); | |
723 | ||
724 | mutex_lock(&audit_filter_mutex); | |
725 | } | |
726 | ||
727 | mutex_unlock(&audit_filter_mutex); | |
728 | mutex_unlock(&audit_cmd_mutex); | |
729 | } | |
730 | return 0; | |
731 | } | |
732 | ||
733 | static int audit_launch_prune(void) | |
734 | { | |
735 | if (prune_thread) | |
736 | return 0; | |
0bf676d1 | 737 | prune_thread = kthread_run(prune_tree_thread, NULL, |
f1aaf262 IP |
738 | "audit_prune_tree"); |
739 | if (IS_ERR(prune_thread)) { | |
740 | pr_err("cannot start thread audit_prune_tree"); | |
741 | prune_thread = NULL; | |
742 | return -ENOMEM; | |
f1aaf262 | 743 | } |
0bf676d1 | 744 | return 0; |
f1aaf262 IP |
745 | } |
746 | ||
74c3cbe3 AV |
747 | /* called with audit_filter_mutex */ |
748 | int audit_add_tree_rule(struct audit_krule *rule) | |
749 | { | |
750 | struct audit_tree *seed = rule->tree, *tree; | |
98bc993f | 751 | struct path path; |
1f707137 | 752 | struct vfsmount *mnt; |
74c3cbe3 AV |
753 | int err; |
754 | ||
736f3203 | 755 | rule->tree = NULL; |
74c3cbe3 AV |
756 | list_for_each_entry(tree, &tree_list, list) { |
757 | if (!strcmp(seed->pathname, tree->pathname)) { | |
758 | put_tree(seed); | |
759 | rule->tree = tree; | |
760 | list_add(&rule->rlist, &tree->rules); | |
761 | return 0; | |
762 | } | |
763 | } | |
764 | tree = seed; | |
765 | list_add(&tree->list, &tree_list); | |
766 | list_add(&rule->rlist, &tree->rules); | |
767 | /* do not set rule->tree yet */ | |
768 | mutex_unlock(&audit_filter_mutex); | |
769 | ||
f1aaf262 IP |
770 | if (unlikely(!prune_thread)) { |
771 | err = audit_launch_prune(); | |
772 | if (err) | |
773 | goto Err; | |
774 | } | |
775 | ||
98bc993f | 776 | err = kern_path(tree->pathname, 0, &path); |
74c3cbe3 AV |
777 | if (err) |
778 | goto Err; | |
589ff870 | 779 | mnt = collect_mounts(&path); |
98bc993f | 780 | path_put(&path); |
be34d1a3 DH |
781 | if (IS_ERR(mnt)) { |
782 | err = PTR_ERR(mnt); | |
74c3cbe3 AV |
783 | goto Err; |
784 | } | |
74c3cbe3 AV |
785 | |
786 | get_tree(tree); | |
1f707137 | 787 | err = iterate_mounts(tag_mount, tree, mnt); |
74c3cbe3 AV |
788 | drop_collected_mounts(mnt); |
789 | ||
790 | if (!err) { | |
791 | struct node *node; | |
792 | spin_lock(&hash_lock); | |
793 | list_for_each_entry(node, &tree->chunks, list) | |
794 | node->index &= ~(1U<<31); | |
795 | spin_unlock(&hash_lock); | |
796 | } else { | |
797 | trim_marked(tree); | |
798 | goto Err; | |
799 | } | |
800 | ||
801 | mutex_lock(&audit_filter_mutex); | |
802 | if (list_empty(&rule->rlist)) { | |
803 | put_tree(tree); | |
804 | return -ENOENT; | |
805 | } | |
806 | rule->tree = tree; | |
807 | put_tree(tree); | |
808 | ||
809 | return 0; | |
810 | Err: | |
811 | mutex_lock(&audit_filter_mutex); | |
812 | list_del_init(&tree->list); | |
813 | list_del_init(&tree->rules); | |
814 | put_tree(tree); | |
815 | return err; | |
816 | } | |
817 | ||
818 | int audit_tag_tree(char *old, char *new) | |
819 | { | |
820 | struct list_head cursor, barrier; | |
821 | int failed = 0; | |
2096f759 | 822 | struct path path1, path2; |
74c3cbe3 | 823 | struct vfsmount *tagged; |
74c3cbe3 AV |
824 | int err; |
825 | ||
2096f759 | 826 | err = kern_path(new, 0, &path2); |
74c3cbe3 AV |
827 | if (err) |
828 | return err; | |
2096f759 AV |
829 | tagged = collect_mounts(&path2); |
830 | path_put(&path2); | |
be34d1a3 DH |
831 | if (IS_ERR(tagged)) |
832 | return PTR_ERR(tagged); | |
74c3cbe3 | 833 | |
2096f759 | 834 | err = kern_path(old, 0, &path1); |
74c3cbe3 AV |
835 | if (err) { |
836 | drop_collected_mounts(tagged); | |
837 | return err; | |
838 | } | |
74c3cbe3 | 839 | |
74c3cbe3 AV |
840 | mutex_lock(&audit_filter_mutex); |
841 | list_add(&barrier, &tree_list); | |
842 | list_add(&cursor, &barrier); | |
843 | ||
844 | while (cursor.next != &tree_list) { | |
845 | struct audit_tree *tree; | |
2096f759 | 846 | int good_one = 0; |
74c3cbe3 AV |
847 | |
848 | tree = container_of(cursor.next, struct audit_tree, list); | |
849 | get_tree(tree); | |
850 | list_del(&cursor); | |
851 | list_add(&cursor, &tree->list); | |
852 | mutex_unlock(&audit_filter_mutex); | |
853 | ||
2096f759 AV |
854 | err = kern_path(tree->pathname, 0, &path2); |
855 | if (!err) { | |
856 | good_one = path_is_under(&path1, &path2); | |
857 | path_put(&path2); | |
74c3cbe3 AV |
858 | } |
859 | ||
2096f759 | 860 | if (!good_one) { |
74c3cbe3 AV |
861 | put_tree(tree); |
862 | mutex_lock(&audit_filter_mutex); | |
863 | continue; | |
864 | } | |
74c3cbe3 | 865 | |
1f707137 | 866 | failed = iterate_mounts(tag_mount, tree, tagged); |
74c3cbe3 AV |
867 | if (failed) { |
868 | put_tree(tree); | |
869 | mutex_lock(&audit_filter_mutex); | |
870 | break; | |
871 | } | |
872 | ||
873 | mutex_lock(&audit_filter_mutex); | |
874 | spin_lock(&hash_lock); | |
875 | if (!tree->goner) { | |
876 | list_del(&tree->list); | |
877 | list_add(&tree->list, &tree_list); | |
878 | } | |
879 | spin_unlock(&hash_lock); | |
880 | put_tree(tree); | |
881 | } | |
882 | ||
883 | while (barrier.prev != &tree_list) { | |
884 | struct audit_tree *tree; | |
885 | ||
886 | tree = container_of(barrier.prev, struct audit_tree, list); | |
887 | get_tree(tree); | |
888 | list_del(&tree->list); | |
889 | list_add(&tree->list, &barrier); | |
890 | mutex_unlock(&audit_filter_mutex); | |
891 | ||
892 | if (!failed) { | |
893 | struct node *node; | |
894 | spin_lock(&hash_lock); | |
895 | list_for_each_entry(node, &tree->chunks, list) | |
896 | node->index &= ~(1U<<31); | |
897 | spin_unlock(&hash_lock); | |
898 | } else { | |
899 | trim_marked(tree); | |
900 | } | |
901 | ||
902 | put_tree(tree); | |
903 | mutex_lock(&audit_filter_mutex); | |
904 | } | |
905 | list_del(&barrier); | |
906 | list_del(&cursor); | |
74c3cbe3 | 907 | mutex_unlock(&audit_filter_mutex); |
2096f759 | 908 | path_put(&path1); |
74c3cbe3 AV |
909 | drop_collected_mounts(tagged); |
910 | return failed; | |
911 | } | |
912 | ||
916d7576 AV |
913 | |
914 | static void audit_schedule_prune(void) | |
915 | { | |
f1aaf262 | 916 | wake_up_process(prune_thread); |
916d7576 AV |
917 | } |
918 | ||
919 | /* | |
920 | * ... and that one is done if evict_chunk() decides to delay until the end | |
921 | * of syscall. Runs synchronously. | |
922 | */ | |
923 | void audit_kill_trees(struct list_head *list) | |
924 | { | |
925 | mutex_lock(&audit_cmd_mutex); | |
926 | mutex_lock(&audit_filter_mutex); | |
927 | ||
928 | while (!list_empty(list)) { | |
929 | struct audit_tree *victim; | |
930 | ||
931 | victim = list_entry(list->next, struct audit_tree, list); | |
932 | kill_rules(victim); | |
933 | list_del_init(&victim->list); | |
934 | ||
935 | mutex_unlock(&audit_filter_mutex); | |
936 | ||
937 | prune_one(victim); | |
938 | ||
939 | mutex_lock(&audit_filter_mutex); | |
940 | } | |
941 | ||
942 | mutex_unlock(&audit_filter_mutex); | |
943 | mutex_unlock(&audit_cmd_mutex); | |
74c3cbe3 AV |
944 | } |
945 | ||
946 | /* | |
947 | * Here comes the stuff asynchronous to auditctl operations | |
948 | */ | |
949 | ||
74c3cbe3 AV |
950 | static void evict_chunk(struct audit_chunk *chunk) |
951 | { | |
952 | struct audit_tree *owner; | |
916d7576 AV |
953 | struct list_head *postponed = audit_killed_trees(); |
954 | int need_prune = 0; | |
74c3cbe3 AV |
955 | int n; |
956 | ||
957 | if (chunk->dead) | |
958 | return; | |
959 | ||
960 | chunk->dead = 1; | |
961 | mutex_lock(&audit_filter_mutex); | |
962 | spin_lock(&hash_lock); | |
963 | while (!list_empty(&chunk->trees)) { | |
964 | owner = list_entry(chunk->trees.next, | |
965 | struct audit_tree, same_root); | |
966 | owner->goner = 1; | |
967 | owner->root = NULL; | |
968 | list_del_init(&owner->same_root); | |
969 | spin_unlock(&hash_lock); | |
916d7576 AV |
970 | if (!postponed) { |
971 | kill_rules(owner); | |
972 | list_move(&owner->list, &prune_list); | |
973 | need_prune = 1; | |
974 | } else { | |
975 | list_move(&owner->list, postponed); | |
976 | } | |
74c3cbe3 AV |
977 | spin_lock(&hash_lock); |
978 | } | |
979 | list_del_rcu(&chunk->hash); | |
980 | for (n = 0; n < chunk->count; n++) | |
981 | list_del_init(&chunk->owners[n].list); | |
982 | spin_unlock(&hash_lock); | |
f1aaf262 | 983 | mutex_unlock(&audit_filter_mutex); |
916d7576 AV |
984 | if (need_prune) |
985 | audit_schedule_prune(); | |
74c3cbe3 AV |
986 | } |
987 | ||
3a9b16b4 | 988 | static int audit_tree_handle_event(struct fsnotify_group *group, |
7053aee2 | 989 | struct inode *to_tell, |
ce8f76fb | 990 | struct fsnotify_mark *inode_mark, |
7053aee2 | 991 | struct fsnotify_mark *vfsmount_mark, |
3cd5eca8 | 992 | u32 mask, const void *data, int data_type, |
45a22f4c | 993 | const unsigned char *file_name, u32 cookie) |
74c3cbe3 | 994 | { |
83c4c4b0 | 995 | return 0; |
28a3a7eb | 996 | } |
74c3cbe3 | 997 | |
e61ce867 | 998 | static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group) |
28a3a7eb EP |
999 | { |
1000 | struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark); | |
1001 | ||
1002 | evict_chunk(chunk); | |
b3e8692b MS |
1003 | |
1004 | /* | |
1005 | * We are guaranteed to have at least one reference to the mark from | |
1006 | * either the inode or the caller of fsnotify_destroy_mark(). | |
1007 | */ | |
1008 | BUG_ON(atomic_read(&entry->refcnt) < 1); | |
74c3cbe3 AV |
1009 | } |
1010 | ||
28a3a7eb EP |
1011 | static const struct fsnotify_ops audit_tree_ops = { |
1012 | .handle_event = audit_tree_handle_event, | |
28a3a7eb | 1013 | .freeing_mark = audit_tree_freeing_mark, |
74c3cbe3 AV |
1014 | }; |
1015 | ||
1016 | static int __init audit_tree_init(void) | |
1017 | { | |
1018 | int i; | |
1019 | ||
0d2e2a1d | 1020 | audit_tree_group = fsnotify_alloc_group(&audit_tree_ops); |
28a3a7eb EP |
1021 | if (IS_ERR(audit_tree_group)) |
1022 | audit_panic("cannot initialize fsnotify group for rectree watches"); | |
74c3cbe3 AV |
1023 | |
1024 | for (i = 0; i < HASH_SIZE; i++) | |
1025 | INIT_LIST_HEAD(&chunk_hash_heads[i]); | |
1026 | ||
1027 | return 0; | |
1028 | } | |
1029 | __initcall(audit_tree_init); |