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