Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 2 | #include "audit.h" |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 3 | #include <linux/fsnotify_backend.h> |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 4 | #include <linux/namei.h> |
| 5 | #include <linux/mount.h> |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 6 | #include <linux/kthread.h> |
Elena Reshetova | 9d2378f | 2017-05-02 10:16:04 -0400 | [diff] [blame] | 7 | #include <linux/refcount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 9 | |
| 10 | struct audit_tree; |
| 11 | struct audit_chunk; |
| 12 | |
| 13 | struct audit_tree { |
Elena Reshetova | 9d2378f | 2017-05-02 10:16:04 -0400 | [diff] [blame] | 14 | refcount_t count; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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; |
Jan Kara | 8d20d6e | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 27 | unsigned long key; |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 28 | struct fsnotify_mark mark; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 29 | struct list_head trees; /* with root here */ |
| 30 | int dead; |
| 31 | int count; |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 32 | atomic_long_t refs; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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); |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 43 | static struct task_struct *prune_thread; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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 | * |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 66 | * chunk is refcounted by embedded fsnotify_mark + .refs (non-zero refcount |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 67 | * of watch contributes 1 to .refs). |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 75 | static struct fsnotify_group *audit_tree_group; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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) { |
Elena Reshetova | 9d2378f | 2017-05-02 10:16:04 -0400 | [diff] [blame] | 83 | refcount_set(&tree->count, 1); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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 | { |
Elena Reshetova | 9d2378f | 2017-05-02 10:16:04 -0400 | [diff] [blame] | 97 | refcount_inc(&tree->count); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 100 | static inline void put_tree(struct audit_tree *tree) |
| 101 | { |
Elena Reshetova | 9d2378f | 2017-05-02 10:16:04 -0400 | [diff] [blame] | 102 | if (refcount_dec_and_test(&tree->count)) |
Lai Jiangshan | 3b097c4 | 2011-03-15 18:03:53 +0800 | [diff] [blame] | 103 | kfree_rcu(tree, head); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 112 | static void free_chunk(struct audit_chunk *chunk) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 113 | { |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 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 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 123 | void audit_put_chunk(struct audit_chunk *chunk) |
| 124 | { |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 125 | if (atomic_long_dec_and_test(&chunk->refs)) |
| 126 | free_chunk(chunk); |
| 127 | } |
| 128 | |
| 129 | static void __put_chunk(struct rcu_head *rcu) |
| 130 | { |
| 131 | struct audit_chunk *chunk = container_of(rcu, struct audit_chunk, head); |
| 132 | audit_put_chunk(chunk); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Jan Kara | a837571 | 2018-11-12 09:54:49 -0500 | [diff] [blame^] | 135 | /* |
| 136 | * Drop reference to the chunk that was held by the mark. This is the reference |
| 137 | * that gets dropped after we've removed the chunk from the hash table and we |
| 138 | * use it to make sure chunk cannot be freed before RCU grace period expires. |
| 139 | */ |
| 140 | static void audit_mark_put_chunk(struct audit_chunk *chunk) |
| 141 | { |
| 142 | call_rcu(&chunk->head, __put_chunk); |
| 143 | } |
| 144 | |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 145 | static void audit_tree_destroy_watch(struct fsnotify_mark *entry) |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 146 | { |
| 147 | struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark); |
Jan Kara | a837571 | 2018-11-12 09:54:49 -0500 | [diff] [blame^] | 148 | audit_mark_put_chunk(chunk); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static struct audit_chunk *alloc_chunk(int count) |
| 152 | { |
| 153 | struct audit_chunk *chunk; |
| 154 | size_t size; |
| 155 | int i; |
| 156 | |
| 157 | size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node); |
| 158 | chunk = kzalloc(size, GFP_KERNEL); |
| 159 | if (!chunk) |
| 160 | return NULL; |
| 161 | |
| 162 | INIT_LIST_HEAD(&chunk->hash); |
| 163 | INIT_LIST_HEAD(&chunk->trees); |
| 164 | chunk->count = count; |
| 165 | atomic_long_set(&chunk->refs, 1); |
| 166 | for (i = 0; i < count; i++) { |
| 167 | INIT_LIST_HEAD(&chunk->owners[i].list); |
| 168 | chunk->owners[i].index = i; |
| 169 | } |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 170 | fsnotify_init_mark(&chunk->mark, audit_tree_group); |
Miklos Szeredi | 799b601 | 2014-11-04 11:27:12 +0100 | [diff] [blame] | 171 | chunk->mark.mask = FS_IN_IGNORED; |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 172 | return chunk; |
| 173 | } |
| 174 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 175 | enum {HASH_SIZE = 128}; |
| 176 | static struct list_head chunk_hash_heads[HASH_SIZE]; |
| 177 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock); |
| 178 | |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 179 | /* Function to return search key in our hash from inode. */ |
| 180 | static unsigned long inode_to_key(const struct inode *inode) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 181 | { |
Amir Goldstein | 36f10f5 | 2018-06-23 17:54:49 +0300 | [diff] [blame] | 182 | /* Use address pointed to by connector->obj as the key */ |
| 183 | return (unsigned long)&inode->i_fsnotify_marks; |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 186 | static inline struct list_head *chunk_hash(unsigned long key) |
| 187 | { |
| 188 | unsigned long n = key / L1_CACHE_BYTES; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 189 | return chunk_hash_heads + n % HASH_SIZE; |
| 190 | } |
| 191 | |
Jan Kara | 9f16d2e | 2018-10-17 12:14:52 +0200 | [diff] [blame] | 192 | /* hash_lock & entry->group->mark_mutex is held by caller */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 193 | static void insert_hash(struct audit_chunk *chunk) |
| 194 | { |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 195 | struct list_head *list; |
| 196 | |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 197 | /* |
| 198 | * Make sure chunk is fully initialized before making it visible in the |
| 199 | * hash. Pairs with a data dependency barrier in READ_ONCE() in |
| 200 | * audit_tree_lookup(). |
| 201 | */ |
| 202 | smp_wmb(); |
Jan Kara | 8d20d6e | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 203 | WARN_ON_ONCE(!chunk->key); |
| 204 | list = chunk_hash(chunk->key); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 205 | list_add_rcu(&chunk->hash, list); |
| 206 | } |
| 207 | |
| 208 | /* called under rcu_read_lock */ |
| 209 | struct audit_chunk *audit_tree_lookup(const struct inode *inode) |
| 210 | { |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 211 | unsigned long key = inode_to_key(inode); |
| 212 | struct list_head *list = chunk_hash(key); |
Paul E. McKenney | 6793a05 | 2008-05-14 17:10:12 -0700 | [diff] [blame] | 213 | struct audit_chunk *p; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 214 | |
Paul E. McKenney | 6793a05 | 2008-05-14 17:10:12 -0700 | [diff] [blame] | 215 | list_for_each_entry_rcu(p, list, hash) { |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 216 | /* |
| 217 | * We use a data dependency barrier in READ_ONCE() to make sure |
| 218 | * the chunk we see is fully initialized. |
| 219 | */ |
| 220 | if (READ_ONCE(p->key) == key) { |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 221 | atomic_long_inc(&p->refs); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 222 | return p; |
| 223 | } |
| 224 | } |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Yaowei Bai | 6f1b5d7 | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 228 | bool audit_tree_match(struct audit_chunk *chunk, struct audit_tree *tree) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 229 | { |
| 230 | int n; |
| 231 | for (n = 0; n < chunk->count; n++) |
| 232 | if (chunk->owners[n].owner == tree) |
Yaowei Bai | 6f1b5d7 | 2015-11-04 08:23:51 -0500 | [diff] [blame] | 233 | return true; |
| 234 | return false; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* tagging and untagging inodes with trees */ |
| 238 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 239 | static struct audit_chunk *find_chunk(struct node *p) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 240 | { |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 241 | int index = p->index & ~(1U<<31); |
| 242 | p -= index; |
| 243 | return container_of(p, struct audit_chunk, owners[0]); |
| 244 | } |
| 245 | |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 246 | static void replace_chunk(struct audit_chunk *new, struct audit_chunk *old, |
| 247 | struct node *skip) |
| 248 | { |
| 249 | struct audit_tree *owner; |
| 250 | int i, j; |
| 251 | |
| 252 | new->key = old->key; |
| 253 | list_splice_init(&old->trees, &new->trees); |
| 254 | list_for_each_entry(owner, &new->trees, same_root) |
| 255 | owner->root = new; |
| 256 | for (i = j = 0; j < old->count; i++, j++) { |
| 257 | if (&old->owners[j] == skip) { |
| 258 | i--; |
| 259 | continue; |
| 260 | } |
| 261 | owner = old->owners[j].owner; |
| 262 | new->owners[i].owner = owner; |
| 263 | new->owners[i].index = old->owners[j].index - j + i; |
| 264 | if (!owner) /* result of earlier fallback */ |
| 265 | continue; |
| 266 | get_tree(owner); |
| 267 | list_replace_init(&old->owners[j].list, &new->owners[i].list); |
| 268 | } |
| 269 | /* |
| 270 | * Make sure chunk is fully initialized before making it visible in the |
| 271 | * hash. Pairs with a data dependency barrier in READ_ONCE() in |
| 272 | * audit_tree_lookup(). |
| 273 | */ |
| 274 | smp_wmb(); |
| 275 | list_replace_rcu(&old->hash, &new->hash); |
| 276 | } |
| 277 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 278 | static void untag_chunk(struct node *p) |
| 279 | { |
| 280 | struct audit_chunk *chunk = find_chunk(p); |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 281 | struct fsnotify_mark *entry = &chunk->mark; |
Al Viro | f7a998a | 2010-10-30 02:18:32 -0400 | [diff] [blame] | 282 | struct audit_chunk *new = NULL; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 283 | struct audit_tree *owner; |
| 284 | int size = chunk->count - 1; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 285 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 286 | fsnotify_get_mark(entry); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 287 | |
| 288 | spin_unlock(&hash_lock); |
| 289 | |
Al Viro | f7a998a | 2010-10-30 02:18:32 -0400 | [diff] [blame] | 290 | if (size) |
| 291 | new = alloc_chunk(size); |
| 292 | |
Jan Kara | be29d20 | 2016-12-14 14:40:05 +0100 | [diff] [blame] | 293 | mutex_lock(&entry->group->mark_mutex); |
Jan Kara | 6b3f05d | 2016-12-21 12:15:30 +0100 | [diff] [blame] | 294 | /* |
| 295 | * mark_mutex protects mark from getting detached and thus also from |
Amir Goldstein | 36f10f5 | 2018-06-23 17:54:49 +0300 | [diff] [blame] | 296 | * mark->connector->obj getting NULL. |
Jan Kara | 6b3f05d | 2016-12-21 12:15:30 +0100 | [diff] [blame] | 297 | */ |
Jan Kara | 43471d1 | 2017-04-03 16:47:58 +0200 | [diff] [blame] | 298 | if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
Jan Kara | be29d20 | 2016-12-14 14:40:05 +0100 | [diff] [blame] | 299 | mutex_unlock(&entry->group->mark_mutex); |
Al Viro | f7a998a | 2010-10-30 02:18:32 -0400 | [diff] [blame] | 300 | if (new) |
Jan Kara | 7b129323 | 2016-12-21 18:32:48 +0100 | [diff] [blame] | 301 | fsnotify_put_mark(&new->mark); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 302 | goto out; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | owner = p->owner; |
| 306 | |
| 307 | if (!size) { |
| 308 | chunk->dead = 1; |
| 309 | spin_lock(&hash_lock); |
| 310 | list_del_init(&chunk->trees); |
| 311 | if (owner->root == chunk) |
| 312 | owner->root = NULL; |
| 313 | list_del_init(&p->list); |
| 314 | list_del_rcu(&chunk->hash); |
| 315 | spin_unlock(&hash_lock); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 316 | fsnotify_detach_mark(entry); |
Jan Kara | be29d20 | 2016-12-14 14:40:05 +0100 | [diff] [blame] | 317 | mutex_unlock(&entry->group->mark_mutex); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 318 | fsnotify_free_mark(entry); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 319 | goto out; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 322 | if (!new) |
| 323 | goto Fallback; |
Al Viro | f7a998a | 2010-10-30 02:18:32 -0400 | [diff] [blame] | 324 | |
Amir Goldstein | 36f10f5 | 2018-06-23 17:54:49 +0300 | [diff] [blame] | 325 | if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj, |
| 326 | FSNOTIFY_OBJ_TYPE_INODE, 1)) { |
Miklos Szeredi | 0fe33aa | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 327 | fsnotify_put_mark(&new->mark); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 328 | goto Fallback; |
| 329 | } |
| 330 | |
| 331 | chunk->dead = 1; |
| 332 | spin_lock(&hash_lock); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 333 | if (owner->root == chunk) { |
| 334 | list_del_init(&owner->same_root); |
| 335 | owner->root = NULL; |
| 336 | } |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 337 | list_del_init(&p->list); |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 338 | /* |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 339 | * This has to go last when updating chunk as once replace_chunk() is |
| 340 | * called, new RCU readers can see the new chunk. |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 341 | */ |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 342 | replace_chunk(new, chunk, p); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 343 | spin_unlock(&hash_lock); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 344 | fsnotify_detach_mark(entry); |
Jan Kara | be29d20 | 2016-12-14 14:40:05 +0100 | [diff] [blame] | 345 | mutex_unlock(&entry->group->mark_mutex); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 346 | fsnotify_free_mark(entry); |
Miklos Szeredi | b3e8692 | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 347 | fsnotify_put_mark(&new->mark); /* drop initial reference */ |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 348 | goto out; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 349 | |
| 350 | Fallback: |
| 351 | // do the best we can |
| 352 | spin_lock(&hash_lock); |
| 353 | if (owner->root == chunk) { |
| 354 | list_del_init(&owner->same_root); |
| 355 | owner->root = NULL; |
| 356 | } |
| 357 | list_del_init(&p->list); |
| 358 | p->owner = NULL; |
| 359 | put_tree(owner); |
| 360 | spin_unlock(&hash_lock); |
Jan Kara | be29d20 | 2016-12-14 14:40:05 +0100 | [diff] [blame] | 361 | mutex_unlock(&entry->group->mark_mutex); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 362 | out: |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 363 | fsnotify_put_mark(entry); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 364 | spin_lock(&hash_lock); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 367 | /* Call with group->mark_mutex held, releases it */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 368 | static int create_chunk(struct inode *inode, struct audit_tree *tree) |
| 369 | { |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 370 | struct fsnotify_mark *entry; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 371 | struct audit_chunk *chunk = alloc_chunk(1); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 372 | |
| 373 | if (!chunk) { |
| 374 | mutex_unlock(&audit_tree_group->mark_mutex); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 375 | return -ENOMEM; |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 376 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 377 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 378 | entry = &chunk->mark; |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 379 | if (fsnotify_add_inode_mark_locked(entry, inode, 0)) { |
| 380 | mutex_unlock(&audit_tree_group->mark_mutex); |
Miklos Szeredi | 0fe33aa | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 381 | fsnotify_put_mark(entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 382 | return -ENOSPC; |
| 383 | } |
| 384 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 385 | spin_lock(&hash_lock); |
| 386 | if (tree->goner) { |
| 387 | spin_unlock(&hash_lock); |
| 388 | chunk->dead = 1; |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 389 | fsnotify_detach_mark(entry); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 390 | mutex_unlock(&audit_tree_group->mark_mutex); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 391 | fsnotify_free_mark(entry); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 392 | fsnotify_put_mark(entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | chunk->owners[0].index = (1U << 31); |
| 396 | chunk->owners[0].owner = tree; |
| 397 | get_tree(tree); |
| 398 | list_add(&chunk->owners[0].list, &tree->chunks); |
| 399 | if (!tree->root) { |
| 400 | tree->root = chunk; |
| 401 | list_add(&tree->same_root, &chunk->trees); |
| 402 | } |
Jan Kara | 8d20d6e | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 403 | chunk->key = inode_to_key(inode); |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 404 | /* |
| 405 | * Inserting into the hash table has to go last as once we do that RCU |
| 406 | * readers can see the chunk. |
| 407 | */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 408 | insert_hash(chunk); |
| 409 | spin_unlock(&hash_lock); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 410 | mutex_unlock(&audit_tree_group->mark_mutex); |
Miklos Szeredi | b3e8692 | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 411 | fsnotify_put_mark(entry); /* drop initial reference */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* the first tagged inode becomes root of tree */ |
| 416 | static int tag_chunk(struct inode *inode, struct audit_tree *tree) |
| 417 | { |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 418 | struct fsnotify_mark *old_entry, *chunk_entry; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 419 | struct audit_chunk *chunk, *old; |
| 420 | struct node *p; |
| 421 | int n; |
| 422 | |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 423 | mutex_lock(&audit_tree_group->mark_mutex); |
Jan Kara | b1362ed | 2016-12-21 16:28:45 +0100 | [diff] [blame] | 424 | old_entry = fsnotify_find_mark(&inode->i_fsnotify_marks, |
| 425 | audit_tree_group); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 426 | if (!old_entry) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 427 | return create_chunk(inode, tree); |
| 428 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 429 | old = container_of(old_entry, struct audit_chunk, mark); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 430 | |
| 431 | /* are we already there? */ |
| 432 | spin_lock(&hash_lock); |
| 433 | for (n = 0; n < old->count; n++) { |
| 434 | if (old->owners[n].owner == tree) { |
| 435 | spin_unlock(&hash_lock); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 436 | mutex_unlock(&audit_tree_group->mark_mutex); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 437 | fsnotify_put_mark(old_entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | } |
| 441 | spin_unlock(&hash_lock); |
| 442 | |
| 443 | chunk = alloc_chunk(old->count + 1); |
Al Viro | b4c30aa | 2009-12-19 16:03:30 +0000 | [diff] [blame] | 444 | if (!chunk) { |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 445 | mutex_unlock(&audit_tree_group->mark_mutex); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 446 | fsnotify_put_mark(old_entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 447 | return -ENOMEM; |
Al Viro | b4c30aa | 2009-12-19 16:03:30 +0000 | [diff] [blame] | 448 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 449 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 450 | chunk_entry = &chunk->mark; |
| 451 | |
Jan Kara | 6b3f05d | 2016-12-21 12:15:30 +0100 | [diff] [blame] | 452 | /* |
| 453 | * mark_mutex protects mark from getting detached and thus also from |
Amir Goldstein | 36f10f5 | 2018-06-23 17:54:49 +0300 | [diff] [blame] | 454 | * mark->connector->obj getting NULL. |
Jan Kara | 6b3f05d | 2016-12-21 12:15:30 +0100 | [diff] [blame] | 455 | */ |
Jan Kara | 43471d1 | 2017-04-03 16:47:58 +0200 | [diff] [blame] | 456 | if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 457 | /* old_entry is being shot, lets just lie */ |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 458 | mutex_unlock(&audit_tree_group->mark_mutex); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 459 | fsnotify_put_mark(old_entry); |
Jan Kara | 7b129323 | 2016-12-21 18:32:48 +0100 | [diff] [blame] | 460 | fsnotify_put_mark(&chunk->mark); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 461 | return -ENOENT; |
| 462 | } |
| 463 | |
Amir Goldstein | 36f10f5 | 2018-06-23 17:54:49 +0300 | [diff] [blame] | 464 | if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj, |
| 465 | FSNOTIFY_OBJ_TYPE_INODE, 1)) { |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 466 | mutex_unlock(&audit_tree_group->mark_mutex); |
Miklos Szeredi | 0fe33aa | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 467 | fsnotify_put_mark(chunk_entry); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 468 | fsnotify_put_mark(old_entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 469 | return -ENOSPC; |
| 470 | } |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 471 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 472 | spin_lock(&hash_lock); |
| 473 | if (tree->goner) { |
| 474 | spin_unlock(&hash_lock); |
| 475 | chunk->dead = 1; |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 476 | fsnotify_detach_mark(chunk_entry); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 477 | mutex_unlock(&audit_tree_group->mark_mutex); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 478 | fsnotify_free_mark(chunk_entry); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 479 | fsnotify_put_mark(chunk_entry); |
| 480 | fsnotify_put_mark(old_entry); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 481 | return 0; |
| 482 | } |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 483 | p = &chunk->owners[chunk->count - 1]; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 484 | p->index = (chunk->count - 1) | (1U<<31); |
| 485 | p->owner = tree; |
| 486 | get_tree(tree); |
| 487 | list_add(&p->list, &tree->chunks); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 488 | old->dead = 1; |
| 489 | if (!tree->root) { |
| 490 | tree->root = chunk; |
| 491 | list_add(&tree->same_root, &chunk->trees); |
| 492 | } |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 493 | /* |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 494 | * This has to go last when updating chunk as once replace_chunk() is |
| 495 | * called, new RCU readers can see the new chunk. |
Jan Kara | 1635e57 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 496 | */ |
Jan Kara | d31b326 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 497 | replace_chunk(chunk, old, NULL); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 498 | spin_unlock(&hash_lock); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 499 | fsnotify_detach_mark(old_entry); |
Jan Kara | a5789b0 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 500 | mutex_unlock(&audit_tree_group->mark_mutex); |
Jan Kara | b1e4603 | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 501 | fsnotify_free_mark(old_entry); |
Miklos Szeredi | b3e8692 | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 502 | fsnotify_put_mark(chunk_entry); /* drop initial reference */ |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 503 | fsnotify_put_mark(old_entry); /* pair to fsnotify_find mark_entry */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
Richard Guy Briggs | 2991dd2 | 2014-10-02 22:05:24 -0400 | [diff] [blame] | 507 | static void audit_tree_log_remove_rule(struct audit_krule *rule) |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 508 | { |
| 509 | struct audit_buffer *ab; |
| 510 | |
Richard Guy Briggs | 65a8766 | 2018-06-14 16:20:05 -0400 | [diff] [blame] | 511 | if (!audit_enabled) |
| 512 | return; |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 513 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
| 514 | if (unlikely(!ab)) |
| 515 | return; |
Steve Grubb | c1e8f06 | 2016-11-16 16:14:33 -0500 | [diff] [blame] | 516 | audit_log_format(ab, "op=remove_rule"); |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 517 | audit_log_format(ab, " dir="); |
| 518 | audit_log_untrustedstring(ab, rule->tree->pathname); |
| 519 | audit_log_key(ab, rule->filterkey); |
| 520 | audit_log_format(ab, " list=%d res=1", rule->listnr); |
| 521 | audit_log_end(ab); |
| 522 | } |
| 523 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 524 | static void kill_rules(struct audit_tree *tree) |
| 525 | { |
| 526 | struct audit_krule *rule, *next; |
| 527 | struct audit_entry *entry; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 528 | |
| 529 | list_for_each_entry_safe(rule, next, &tree->rules, rlist) { |
| 530 | entry = container_of(rule, struct audit_entry, rule); |
| 531 | |
| 532 | list_del_init(&rule->rlist); |
| 533 | if (rule->tree) { |
| 534 | /* not a half-baked one */ |
Richard Guy Briggs | 2991dd2 | 2014-10-02 22:05:24 -0400 | [diff] [blame] | 535 | audit_tree_log_remove_rule(rule); |
Richard Guy Briggs | 34d99af5 | 2015-08-05 16:29:37 -0400 | [diff] [blame] | 536 | if (entry->rule.exe) |
| 537 | audit_remove_mark(entry->rule.exe); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 538 | rule->tree = NULL; |
| 539 | list_del_rcu(&entry->list); |
Al Viro | e45aa21 | 2008-12-15 01:17:50 -0500 | [diff] [blame] | 540 | list_del(&entry->rule.list); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 541 | call_rcu(&entry->rcu, audit_free_rule_rcu); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * finish killing struct audit_tree |
| 548 | */ |
| 549 | static void prune_one(struct audit_tree *victim) |
| 550 | { |
| 551 | spin_lock(&hash_lock); |
| 552 | while (!list_empty(&victim->chunks)) { |
| 553 | struct node *p; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 554 | |
| 555 | p = list_entry(victim->chunks.next, struct node, list); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 556 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 557 | untag_chunk(p); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 558 | } |
| 559 | spin_unlock(&hash_lock); |
| 560 | put_tree(victim); |
| 561 | } |
| 562 | |
| 563 | /* trim the uncommitted chunks from tree */ |
| 564 | |
| 565 | static void trim_marked(struct audit_tree *tree) |
| 566 | { |
| 567 | struct list_head *p, *q; |
| 568 | spin_lock(&hash_lock); |
| 569 | if (tree->goner) { |
| 570 | spin_unlock(&hash_lock); |
| 571 | return; |
| 572 | } |
| 573 | /* reorder */ |
| 574 | for (p = tree->chunks.next; p != &tree->chunks; p = q) { |
| 575 | struct node *node = list_entry(p, struct node, list); |
| 576 | q = p->next; |
| 577 | if (node->index & (1U<<31)) { |
| 578 | list_del_init(p); |
| 579 | list_add(p, &tree->chunks); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | while (!list_empty(&tree->chunks)) { |
| 584 | struct node *node; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 585 | |
| 586 | node = list_entry(tree->chunks.next, struct node, list); |
| 587 | |
| 588 | /* have we run out of marked? */ |
| 589 | if (!(node->index & (1U<<31))) |
| 590 | break; |
| 591 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 592 | untag_chunk(node); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 593 | } |
| 594 | if (!tree->root && !tree->goner) { |
| 595 | tree->goner = 1; |
| 596 | spin_unlock(&hash_lock); |
| 597 | mutex_lock(&audit_filter_mutex); |
| 598 | kill_rules(tree); |
| 599 | list_del_init(&tree->list); |
| 600 | mutex_unlock(&audit_filter_mutex); |
| 601 | prune_one(tree); |
| 602 | } else { |
| 603 | spin_unlock(&hash_lock); |
| 604 | } |
| 605 | } |
| 606 | |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 607 | static void audit_schedule_prune(void); |
| 608 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 609 | /* called with audit_filter_mutex */ |
| 610 | int audit_remove_tree_rule(struct audit_krule *rule) |
| 611 | { |
| 612 | struct audit_tree *tree; |
| 613 | tree = rule->tree; |
| 614 | if (tree) { |
| 615 | spin_lock(&hash_lock); |
| 616 | list_del_init(&rule->rlist); |
| 617 | if (list_empty(&tree->rules) && !tree->goner) { |
| 618 | tree->root = NULL; |
| 619 | list_del_init(&tree->same_root); |
| 620 | tree->goner = 1; |
| 621 | list_move(&tree->list, &prune_list); |
| 622 | rule->tree = NULL; |
| 623 | spin_unlock(&hash_lock); |
| 624 | audit_schedule_prune(); |
| 625 | return 1; |
| 626 | } |
| 627 | rule->tree = NULL; |
| 628 | spin_unlock(&hash_lock); |
| 629 | return 1; |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 634 | static int compare_root(struct vfsmount *mnt, void *arg) |
| 635 | { |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 636 | return inode_to_key(d_backing_inode(mnt->mnt_root)) == |
| 637 | (unsigned long)arg; |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 638 | } |
| 639 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 640 | void audit_trim_trees(void) |
| 641 | { |
| 642 | struct list_head cursor; |
| 643 | |
| 644 | mutex_lock(&audit_filter_mutex); |
| 645 | list_add(&cursor, &tree_list); |
| 646 | while (cursor.next != &tree_list) { |
| 647 | struct audit_tree *tree; |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 648 | struct path path; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 649 | struct vfsmount *root_mnt; |
| 650 | struct node *node; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 651 | int err; |
| 652 | |
| 653 | tree = container_of(cursor.next, struct audit_tree, list); |
| 654 | get_tree(tree); |
| 655 | list_del(&cursor); |
| 656 | list_add(&cursor, &tree->list); |
| 657 | mutex_unlock(&audit_filter_mutex); |
| 658 | |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 659 | err = kern_path(tree->pathname, 0, &path); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 660 | if (err) |
| 661 | goto skip_it; |
| 662 | |
Al Viro | 589ff87 | 2009-04-18 03:28:19 -0400 | [diff] [blame] | 663 | root_mnt = collect_mounts(&path); |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 664 | path_put(&path); |
David Howells | be34d1a | 2012-06-25 12:55:18 +0100 | [diff] [blame] | 665 | if (IS_ERR(root_mnt)) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 666 | goto skip_it; |
| 667 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 668 | spin_lock(&hash_lock); |
| 669 | list_for_each_entry(node, &tree->chunks, list) { |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 670 | struct audit_chunk *chunk = find_chunk(node); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 671 | /* this could be NULL if the watch is dying else where... */ |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 672 | node->index |= 1U<<31; |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 673 | if (iterate_mounts(compare_root, |
Jan Kara | 8d20d6e | 2018-11-12 09:54:48 -0500 | [diff] [blame] | 674 | (void *)(chunk->key), |
Jan Kara | f410ff6 | 2016-12-16 10:13:37 +0100 | [diff] [blame] | 675 | root_mnt)) |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 676 | node->index &= ~(1U<<31); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 677 | } |
| 678 | spin_unlock(&hash_lock); |
| 679 | trim_marked(tree); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 680 | drop_collected_mounts(root_mnt); |
| 681 | skip_it: |
Chen Gang | 12b2f11 | 2013-04-29 15:05:19 -0700 | [diff] [blame] | 682 | put_tree(tree); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 683 | mutex_lock(&audit_filter_mutex); |
| 684 | } |
| 685 | list_del(&cursor); |
| 686 | mutex_unlock(&audit_filter_mutex); |
| 687 | } |
| 688 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 689 | int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op) |
| 690 | { |
| 691 | |
| 692 | if (pathname[0] != '/' || |
| 693 | rule->listnr != AUDIT_FILTER_EXIT || |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 694 | op != Audit_equal || |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 695 | rule->inode_f || rule->watch || rule->tree) |
| 696 | return -EINVAL; |
| 697 | rule->tree = alloc_tree(pathname); |
| 698 | if (!rule->tree) |
| 699 | return -ENOMEM; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | void audit_put_tree(struct audit_tree *tree) |
| 704 | { |
| 705 | put_tree(tree); |
| 706 | } |
| 707 | |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 708 | static int tag_mount(struct vfsmount *mnt, void *arg) |
| 709 | { |
David Howells | 3b36215 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 710 | return tag_chunk(d_backing_inode(mnt->mnt_root), arg); |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 711 | } |
| 712 | |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 713 | /* |
| 714 | * That gets run when evict_chunk() ends up needing to kill audit_tree. |
| 715 | * Runs from a separate thread. |
| 716 | */ |
| 717 | static int prune_tree_thread(void *unused) |
| 718 | { |
| 719 | for (;;) { |
Jiri Slaby | 0bf676d | 2016-03-31 10:49:28 +0200 | [diff] [blame] | 720 | if (list_empty(&prune_list)) { |
| 721 | set_current_state(TASK_INTERRUPTIBLE); |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 722 | schedule(); |
Jiri Slaby | 0bf676d | 2016-03-31 10:49:28 +0200 | [diff] [blame] | 723 | } |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 724 | |
Paul Moore | ce42363 | 2018-02-20 09:52:38 -0500 | [diff] [blame] | 725 | audit_ctl_lock(); |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 726 | mutex_lock(&audit_filter_mutex); |
| 727 | |
| 728 | while (!list_empty(&prune_list)) { |
| 729 | struct audit_tree *victim; |
| 730 | |
| 731 | victim = list_entry(prune_list.next, |
| 732 | struct audit_tree, list); |
| 733 | list_del_init(&victim->list); |
| 734 | |
| 735 | mutex_unlock(&audit_filter_mutex); |
| 736 | |
| 737 | prune_one(victim); |
| 738 | |
| 739 | mutex_lock(&audit_filter_mutex); |
| 740 | } |
| 741 | |
| 742 | mutex_unlock(&audit_filter_mutex); |
Paul Moore | ce42363 | 2018-02-20 09:52:38 -0500 | [diff] [blame] | 743 | audit_ctl_unlock(); |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 744 | } |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static int audit_launch_prune(void) |
| 749 | { |
| 750 | if (prune_thread) |
| 751 | return 0; |
Jiri Slaby | 0bf676d | 2016-03-31 10:49:28 +0200 | [diff] [blame] | 752 | prune_thread = kthread_run(prune_tree_thread, NULL, |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 753 | "audit_prune_tree"); |
| 754 | if (IS_ERR(prune_thread)) { |
| 755 | pr_err("cannot start thread audit_prune_tree"); |
| 756 | prune_thread = NULL; |
| 757 | return -ENOMEM; |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 758 | } |
Jiri Slaby | 0bf676d | 2016-03-31 10:49:28 +0200 | [diff] [blame] | 759 | return 0; |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 760 | } |
| 761 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 762 | /* called with audit_filter_mutex */ |
| 763 | int audit_add_tree_rule(struct audit_krule *rule) |
| 764 | { |
| 765 | struct audit_tree *seed = rule->tree, *tree; |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 766 | struct path path; |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 767 | struct vfsmount *mnt; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 768 | int err; |
| 769 | |
Chen Gang | 736f320 | 2013-06-12 14:05:07 -0700 | [diff] [blame] | 770 | rule->tree = NULL; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 771 | list_for_each_entry(tree, &tree_list, list) { |
| 772 | if (!strcmp(seed->pathname, tree->pathname)) { |
| 773 | put_tree(seed); |
| 774 | rule->tree = tree; |
| 775 | list_add(&rule->rlist, &tree->rules); |
| 776 | return 0; |
| 777 | } |
| 778 | } |
| 779 | tree = seed; |
| 780 | list_add(&tree->list, &tree_list); |
| 781 | list_add(&rule->rlist, &tree->rules); |
| 782 | /* do not set rule->tree yet */ |
| 783 | mutex_unlock(&audit_filter_mutex); |
| 784 | |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 785 | if (unlikely(!prune_thread)) { |
| 786 | err = audit_launch_prune(); |
| 787 | if (err) |
| 788 | goto Err; |
| 789 | } |
| 790 | |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 791 | err = kern_path(tree->pathname, 0, &path); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 792 | if (err) |
| 793 | goto Err; |
Al Viro | 589ff87 | 2009-04-18 03:28:19 -0400 | [diff] [blame] | 794 | mnt = collect_mounts(&path); |
Al Viro | 98bc993 | 2008-08-02 01:06:21 -0400 | [diff] [blame] | 795 | path_put(&path); |
David Howells | be34d1a | 2012-06-25 12:55:18 +0100 | [diff] [blame] | 796 | if (IS_ERR(mnt)) { |
| 797 | err = PTR_ERR(mnt); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 798 | goto Err; |
| 799 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 800 | |
| 801 | get_tree(tree); |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 802 | err = iterate_mounts(tag_mount, tree, mnt); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 803 | drop_collected_mounts(mnt); |
| 804 | |
| 805 | if (!err) { |
| 806 | struct node *node; |
| 807 | spin_lock(&hash_lock); |
| 808 | list_for_each_entry(node, &tree->chunks, list) |
| 809 | node->index &= ~(1U<<31); |
| 810 | spin_unlock(&hash_lock); |
| 811 | } else { |
| 812 | trim_marked(tree); |
| 813 | goto Err; |
| 814 | } |
| 815 | |
| 816 | mutex_lock(&audit_filter_mutex); |
| 817 | if (list_empty(&rule->rlist)) { |
| 818 | put_tree(tree); |
| 819 | return -ENOENT; |
| 820 | } |
| 821 | rule->tree = tree; |
| 822 | put_tree(tree); |
| 823 | |
| 824 | return 0; |
| 825 | Err: |
| 826 | mutex_lock(&audit_filter_mutex); |
| 827 | list_del_init(&tree->list); |
| 828 | list_del_init(&tree->rules); |
| 829 | put_tree(tree); |
| 830 | return err; |
| 831 | } |
| 832 | |
| 833 | int audit_tag_tree(char *old, char *new) |
| 834 | { |
| 835 | struct list_head cursor, barrier; |
| 836 | int failed = 0; |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 837 | struct path path1, path2; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 838 | struct vfsmount *tagged; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 839 | int err; |
| 840 | |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 841 | err = kern_path(new, 0, &path2); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 842 | if (err) |
| 843 | return err; |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 844 | tagged = collect_mounts(&path2); |
| 845 | path_put(&path2); |
David Howells | be34d1a | 2012-06-25 12:55:18 +0100 | [diff] [blame] | 846 | if (IS_ERR(tagged)) |
| 847 | return PTR_ERR(tagged); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 848 | |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 849 | err = kern_path(old, 0, &path1); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 850 | if (err) { |
| 851 | drop_collected_mounts(tagged); |
| 852 | return err; |
| 853 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 854 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 855 | mutex_lock(&audit_filter_mutex); |
| 856 | list_add(&barrier, &tree_list); |
| 857 | list_add(&cursor, &barrier); |
| 858 | |
| 859 | while (cursor.next != &tree_list) { |
| 860 | struct audit_tree *tree; |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 861 | int good_one = 0; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 862 | |
| 863 | tree = container_of(cursor.next, struct audit_tree, list); |
| 864 | get_tree(tree); |
| 865 | list_del(&cursor); |
| 866 | list_add(&cursor, &tree->list); |
| 867 | mutex_unlock(&audit_filter_mutex); |
| 868 | |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 869 | err = kern_path(tree->pathname, 0, &path2); |
| 870 | if (!err) { |
| 871 | good_one = path_is_under(&path1, &path2); |
| 872 | path_put(&path2); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 875 | if (!good_one) { |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 876 | put_tree(tree); |
| 877 | mutex_lock(&audit_filter_mutex); |
| 878 | continue; |
| 879 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 880 | |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 881 | failed = iterate_mounts(tag_mount, tree, tagged); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 882 | if (failed) { |
| 883 | put_tree(tree); |
| 884 | mutex_lock(&audit_filter_mutex); |
| 885 | break; |
| 886 | } |
| 887 | |
| 888 | mutex_lock(&audit_filter_mutex); |
| 889 | spin_lock(&hash_lock); |
| 890 | if (!tree->goner) { |
| 891 | list_del(&tree->list); |
| 892 | list_add(&tree->list, &tree_list); |
| 893 | } |
| 894 | spin_unlock(&hash_lock); |
| 895 | put_tree(tree); |
| 896 | } |
| 897 | |
| 898 | while (barrier.prev != &tree_list) { |
| 899 | struct audit_tree *tree; |
| 900 | |
| 901 | tree = container_of(barrier.prev, struct audit_tree, list); |
| 902 | get_tree(tree); |
| 903 | list_del(&tree->list); |
| 904 | list_add(&tree->list, &barrier); |
| 905 | mutex_unlock(&audit_filter_mutex); |
| 906 | |
| 907 | if (!failed) { |
| 908 | struct node *node; |
| 909 | spin_lock(&hash_lock); |
| 910 | list_for_each_entry(node, &tree->chunks, list) |
| 911 | node->index &= ~(1U<<31); |
| 912 | spin_unlock(&hash_lock); |
| 913 | } else { |
| 914 | trim_marked(tree); |
| 915 | } |
| 916 | |
| 917 | put_tree(tree); |
| 918 | mutex_lock(&audit_filter_mutex); |
| 919 | } |
| 920 | list_del(&barrier); |
| 921 | list_del(&cursor); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 922 | mutex_unlock(&audit_filter_mutex); |
Al Viro | 2096f75 | 2010-01-30 13:16:21 -0500 | [diff] [blame] | 923 | path_put(&path1); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 924 | drop_collected_mounts(tagged); |
| 925 | return failed; |
| 926 | } |
| 927 | |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 928 | |
| 929 | static void audit_schedule_prune(void) |
| 930 | { |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 931 | wake_up_process(prune_thread); |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* |
| 935 | * ... and that one is done if evict_chunk() decides to delay until the end |
| 936 | * of syscall. Runs synchronously. |
| 937 | */ |
| 938 | void audit_kill_trees(struct list_head *list) |
| 939 | { |
Paul Moore | ce42363 | 2018-02-20 09:52:38 -0500 | [diff] [blame] | 940 | audit_ctl_lock(); |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 941 | mutex_lock(&audit_filter_mutex); |
| 942 | |
| 943 | while (!list_empty(list)) { |
| 944 | struct audit_tree *victim; |
| 945 | |
| 946 | victim = list_entry(list->next, struct audit_tree, list); |
| 947 | kill_rules(victim); |
| 948 | list_del_init(&victim->list); |
| 949 | |
| 950 | mutex_unlock(&audit_filter_mutex); |
| 951 | |
| 952 | prune_one(victim); |
| 953 | |
| 954 | mutex_lock(&audit_filter_mutex); |
| 955 | } |
| 956 | |
| 957 | mutex_unlock(&audit_filter_mutex); |
Paul Moore | ce42363 | 2018-02-20 09:52:38 -0500 | [diff] [blame] | 958 | audit_ctl_unlock(); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Here comes the stuff asynchronous to auditctl operations |
| 963 | */ |
| 964 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 965 | static void evict_chunk(struct audit_chunk *chunk) |
| 966 | { |
| 967 | struct audit_tree *owner; |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 968 | struct list_head *postponed = audit_killed_trees(); |
| 969 | int need_prune = 0; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 970 | int n; |
| 971 | |
| 972 | if (chunk->dead) |
| 973 | return; |
| 974 | |
| 975 | chunk->dead = 1; |
| 976 | mutex_lock(&audit_filter_mutex); |
| 977 | spin_lock(&hash_lock); |
| 978 | while (!list_empty(&chunk->trees)) { |
| 979 | owner = list_entry(chunk->trees.next, |
| 980 | struct audit_tree, same_root); |
| 981 | owner->goner = 1; |
| 982 | owner->root = NULL; |
| 983 | list_del_init(&owner->same_root); |
| 984 | spin_unlock(&hash_lock); |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 985 | if (!postponed) { |
| 986 | kill_rules(owner); |
| 987 | list_move(&owner->list, &prune_list); |
| 988 | need_prune = 1; |
| 989 | } else { |
| 990 | list_move(&owner->list, postponed); |
| 991 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 992 | spin_lock(&hash_lock); |
| 993 | } |
| 994 | list_del_rcu(&chunk->hash); |
| 995 | for (n = 0; n < chunk->count; n++) |
| 996 | list_del_init(&chunk->owners[n].list); |
| 997 | spin_unlock(&hash_lock); |
Imre Palik | f1aaf26 | 2015-02-23 15:37:59 -0500 | [diff] [blame] | 998 | mutex_unlock(&audit_filter_mutex); |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 999 | if (need_prune) |
| 1000 | audit_schedule_prune(); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Eric Paris | 3a9b16b | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 1003 | static int audit_tree_handle_event(struct fsnotify_group *group, |
Jan Kara | 7053aee | 2014-01-21 15:48:14 -0800 | [diff] [blame] | 1004 | struct inode *to_tell, |
Al Viro | 3cd5eca | 2016-11-20 20:19:09 -0500 | [diff] [blame] | 1005 | u32 mask, const void *data, int data_type, |
Jan Kara | 9385a84 | 2016-11-10 17:51:50 +0100 | [diff] [blame] | 1006 | const unsigned char *file_name, u32 cookie, |
| 1007 | struct fsnotify_iter_info *iter_info) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1008 | { |
Jan Kara | 83c4c4b | 2014-01-21 15:48:15 -0800 | [diff] [blame] | 1009 | return 0; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 1012 | static void audit_tree_freeing_mark(struct fsnotify_mark *entry, struct fsnotify_group *group) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1013 | { |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 1014 | struct audit_chunk *chunk = container_of(entry, struct audit_chunk, mark); |
| 1015 | |
| 1016 | evict_chunk(chunk); |
Miklos Szeredi | b3e8692 | 2012-08-15 12:55:22 +0200 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * We are guaranteed to have at least one reference to the mark from |
| 1020 | * either the inode or the caller of fsnotify_destroy_mark(). |
| 1021 | */ |
Elena Reshetova | ab97f873 | 2017-10-20 13:26:02 +0300 | [diff] [blame] | 1022 | BUG_ON(refcount_read(&entry->refcnt) < 1); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 1025 | static const struct fsnotify_ops audit_tree_ops = { |
| 1026 | .handle_event = audit_tree_handle_event, |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 1027 | .freeing_mark = audit_tree_freeing_mark, |
Jan Kara | 054c636 | 2016-12-21 18:06:12 +0100 | [diff] [blame] | 1028 | .free_mark = audit_tree_destroy_watch, |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1029 | }; |
| 1030 | |
| 1031 | static int __init audit_tree_init(void) |
| 1032 | { |
| 1033 | int i; |
| 1034 | |
Eric Paris | 0d2e2a1 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 1035 | audit_tree_group = fsnotify_alloc_group(&audit_tree_ops); |
Eric Paris | 28a3a7e | 2009-12-17 20:12:05 -0500 | [diff] [blame] | 1036 | if (IS_ERR(audit_tree_group)) |
| 1037 | audit_panic("cannot initialize fsnotify group for rectree watches"); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1038 | |
| 1039 | for (i = 0; i < HASH_SIZE; i++) |
| 1040 | INIT_LIST_HEAD(&chunk_hash_heads[i]); |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | __initcall(audit_tree_init); |