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