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