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