]> git.ipfire.org Git - thirdparty/linux.git/blame - kernel/audit_watch.c
Audit: clean up the audit_watch split
[thirdparty/linux.git] / kernel / audit_watch.c
CommitLineData
cfcad62c
EP
1/* audit_watch.c -- watching inodes
2 *
3 * Copyright 2003-2009 Red Hat, Inc.
4 * Copyright 2005 Hewlett-Packard Development Company, L.P.
5 * Copyright 2005 IBM Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/audit.h>
24#include <linux/kthread.h>
25#include <linux/mutex.h>
26#include <linux/fs.h>
27#include <linux/namei.h>
28#include <linux/netlink.h>
29#include <linux/sched.h>
5a0e3ad6 30#include <linux/slab.h>
cfcad62c
EP
31#include <linux/inotify.h>
32#include <linux/security.h>
33#include "audit.h"
34
35/*
36 * Reference counting:
37 *
38 * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
39 * event. Each audit_watch holds a reference to its associated parent.
40 *
41 * audit_watch: if added to lists, lifetime is from audit_init_watch() to
42 * audit_remove_watch(). Additionally, an audit_watch may exist
43 * temporarily to assist in searching existing filter data. Each
44 * audit_krule holds a reference to its associated watch.
45 */
46
47struct audit_watch {
48 atomic_t count; /* reference count */
cfcad62c 49 dev_t dev; /* associated superblock device */
e08b061e 50 char *path; /* insertion path */
cfcad62c
EP
51 unsigned long ino; /* associated inode number */
52 struct audit_parent *parent; /* associated parent */
53 struct list_head wlist; /* entry in parent->watches list */
ae7b8f41 54 struct list_head rules; /* anchor for krule->rlist */
cfcad62c
EP
55};
56
57struct audit_parent {
ae7b8f41
EP
58 struct list_head ilist; /* tmp list used to free parents */
59 struct list_head watches; /* anchor for audit_watch->wlist */
cfcad62c
EP
60 struct inotify_watch wdata; /* inotify watch data */
61 unsigned flags; /* status flags */
62};
63
64/* Inotify handle. */
65struct inotify_handle *audit_ih;
66
67/*
68 * audit_parent status flags:
69 *
70 * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
71 * a filesystem event to ensure we're adding audit watches to a valid parent.
72 * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
73 * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
74 * we can receive while holding nameidata.
75 */
76#define AUDIT_PARENT_INVALID 0x001
77
78/* Inotify events we care about. */
79#define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
80
ae7b8f41
EP
81static void audit_free_parent(struct audit_parent *parent)
82{
83 WARN_ON(!list_empty(&parent->watches));
84 kfree(parent);
85}
86
87static void audit_destroy_watch(struct inotify_watch *i_watch)
cfcad62c
EP
88{
89 struct audit_parent *parent;
90
91 parent = container_of(i_watch, struct audit_parent, wdata);
ae7b8f41 92 audit_free_parent(parent);
cfcad62c
EP
93}
94
95void audit_get_watch(struct audit_watch *watch)
96{
97 atomic_inc(&watch->count);
98}
99
100void audit_put_watch(struct audit_watch *watch)
101{
102 if (atomic_dec_and_test(&watch->count)) {
103 WARN_ON(watch->parent);
104 WARN_ON(!list_empty(&watch->rules));
105 kfree(watch->path);
106 kfree(watch);
107 }
108}
109
110void audit_remove_watch(struct audit_watch *watch)
111{
112 list_del(&watch->wlist);
113 put_inotify_watch(&watch->parent->wdata);
114 watch->parent = NULL;
115 audit_put_watch(watch); /* match initial get */
116}
117
118char *audit_watch_path(struct audit_watch *watch)
119{
120 return watch->path;
121}
122
ae7b8f41 123int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
cfcad62c 124{
ae7b8f41
EP
125 return (watch->ino != (unsigned long)-1) &&
126 (watch->ino == ino) &&
127 (watch->dev == dev);
cfcad62c
EP
128}
129
130/* Initialize a parent watch entry. */
131static struct audit_parent *audit_init_parent(struct nameidata *ndp)
132{
133 struct audit_parent *parent;
134 s32 wd;
135
136 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
137 if (unlikely(!parent))
138 return ERR_PTR(-ENOMEM);
139
140 INIT_LIST_HEAD(&parent->watches);
141 parent->flags = 0;
142
143 inotify_init_watch(&parent->wdata);
144 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
145 get_inotify_watch(&parent->wdata);
146 wd = inotify_add_watch(audit_ih, &parent->wdata,
147 ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
148 if (wd < 0) {
ae7b8f41 149 audit_free_parent(parent);
cfcad62c
EP
150 return ERR_PTR(wd);
151 }
152
153 return parent;
154}
155
156/* Initialize a watch entry. */
157static struct audit_watch *audit_init_watch(char *path)
158{
159 struct audit_watch *watch;
160
161 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
162 if (unlikely(!watch))
163 return ERR_PTR(-ENOMEM);
164
165 INIT_LIST_HEAD(&watch->rules);
166 atomic_set(&watch->count, 1);
167 watch->path = path;
168 watch->dev = (dev_t)-1;
169 watch->ino = (unsigned long)-1;
170
171 return watch;
172}
173
174/* Translate a watch string to kernel respresentation. */
175int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
176{
177 struct audit_watch *watch;
178
179 if (!audit_ih)
180 return -EOPNOTSUPP;
181
182 if (path[0] != '/' || path[len-1] == '/' ||
183 krule->listnr != AUDIT_FILTER_EXIT ||
184 op != Audit_equal ||
185 krule->inode_f || krule->watch || krule->tree)
186 return -EINVAL;
187
188 watch = audit_init_watch(path);
189 if (IS_ERR(watch))
190 return PTR_ERR(watch);
191
192 audit_get_watch(watch);
193 krule->watch = watch;
194
195 return 0;
196}
197
198/* Duplicate the given audit watch. The new watch's rules list is initialized
199 * to an empty list and wlist is undefined. */
200static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
201{
202 char *path;
203 struct audit_watch *new;
204
205 path = kstrdup(old->path, GFP_KERNEL);
206 if (unlikely(!path))
207 return ERR_PTR(-ENOMEM);
208
209 new = audit_init_watch(path);
210 if (IS_ERR(new)) {
211 kfree(path);
212 goto out;
213 }
214
215 new->dev = old->dev;
216 new->ino = old->ino;
217 get_inotify_watch(&old->parent->wdata);
218 new->parent = old->parent;
219
220out:
221 return new;
222}
223
224static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
225{
226 if (audit_enabled) {
227 struct audit_buffer *ab;
228 ab = audit_log_start(NULL, GFP_NOFS, AUDIT_CONFIG_CHANGE);
229 audit_log_format(ab, "auid=%u ses=%u op=",
230 audit_get_loginuid(current),
231 audit_get_sessionid(current));
232 audit_log_string(ab, op);
233 audit_log_format(ab, " path=");
234 audit_log_untrustedstring(ab, w->path);
9d960985 235 audit_log_key(ab, r->filterkey);
cfcad62c
EP
236 audit_log_format(ab, " list=%d res=1", r->listnr);
237 audit_log_end(ab);
238 }
239}
240
241/* Update inode info in audit rules based on filesystem event. */
242static void audit_update_watch(struct audit_parent *parent,
243 const char *dname, dev_t dev,
244 unsigned long ino, unsigned invalidating)
245{
246 struct audit_watch *owatch, *nwatch, *nextw;
247 struct audit_krule *r, *nextr;
248 struct audit_entry *oentry, *nentry;
249
250 mutex_lock(&audit_filter_mutex);
ae7b8f41
EP
251 /* Run all of the watches on this parent looking for the one that
252 * matches the given dname */
cfcad62c
EP
253 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
254 if (audit_compare_dname_path(dname, owatch->path, NULL))
255 continue;
256
257 /* If the update involves invalidating rules, do the inode-based
258 * filtering now, so we don't omit records. */
ae7b8f41 259 if (invalidating && !audit_dummy_context())
cfcad62c
EP
260 audit_filter_inodes(current, current->audit_context);
261
ae7b8f41
EP
262 /* updating ino will likely change which audit_hash_list we
263 * are on so we need a new watch for the new list */
cfcad62c
EP
264 nwatch = audit_dupe_watch(owatch);
265 if (IS_ERR(nwatch)) {
266 mutex_unlock(&audit_filter_mutex);
267 audit_panic("error updating watch, skipping");
268 return;
269 }
270 nwatch->dev = dev;
271 nwatch->ino = ino;
272
273 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
274
275 oentry = container_of(r, struct audit_entry, rule);
276 list_del(&oentry->rule.rlist);
277 list_del_rcu(&oentry->list);
278
ae7b8f41 279 nentry = audit_dupe_rule(&oentry->rule);
cfcad62c
EP
280 if (IS_ERR(nentry)) {
281 list_del(&oentry->rule.list);
282 audit_panic("error updating watch, removing");
283 } else {
284 int h = audit_hash_ino((u32)ino);
ae7b8f41
EP
285
286 /*
287 * nentry->rule.watch == oentry->rule.watch so
288 * we must drop that reference and set it to our
289 * new watch.
290 */
291 audit_put_watch(nentry->rule.watch);
292 audit_get_watch(nwatch);
293 nentry->rule.watch = nwatch;
cfcad62c
EP
294 list_add(&nentry->rule.rlist, &nwatch->rules);
295 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
296 list_replace(&oentry->rule.list,
297 &nentry->rule.list);
298 }
299
300 audit_watch_log_rule_change(r, owatch, "updated rules");
301
302 call_rcu(&oentry->rcu, audit_free_rule_rcu);
303 }
304
305 audit_remove_watch(owatch);
306 goto add_watch_to_parent; /* event applies to a single watch */
307 }
308 mutex_unlock(&audit_filter_mutex);
309 return;
310
311add_watch_to_parent:
312 list_add(&nwatch->wlist, &parent->watches);
313 mutex_unlock(&audit_filter_mutex);
314 return;
315}
316
317/* Remove all watches & rules associated with a parent that is going away. */
318static void audit_remove_parent_watches(struct audit_parent *parent)
319{
320 struct audit_watch *w, *nextw;
321 struct audit_krule *r, *nextr;
322 struct audit_entry *e;
323
324 mutex_lock(&audit_filter_mutex);
325 parent->flags |= AUDIT_PARENT_INVALID;
326 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
327 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
328 e = container_of(r, struct audit_entry, rule);
329 audit_watch_log_rule_change(r, w, "remove rule");
330 list_del(&r->rlist);
331 list_del(&r->list);
332 list_del_rcu(&e->list);
333 call_rcu(&e->rcu, audit_free_rule_rcu);
334 }
335 audit_remove_watch(w);
336 }
337 mutex_unlock(&audit_filter_mutex);
338}
339
340/* Unregister inotify watches for parents on in_list.
341 * Generates an IN_IGNORED event. */
ae7b8f41 342void audit_watch_inotify_unregister(struct list_head *in_list)
cfcad62c
EP
343{
344 struct audit_parent *p, *n;
345
346 list_for_each_entry_safe(p, n, in_list, ilist) {
347 list_del(&p->ilist);
348 inotify_rm_watch(audit_ih, &p->wdata);
ae7b8f41 349 /* the unpin matching the pin in audit_remove_watch_rule() */
cfcad62c
EP
350 unpin_inotify_watch(&p->wdata);
351 }
352}
353
354/* Get path information necessary for adding watches. */
35fe4d0b 355static int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw)
cfcad62c
EP
356{
357 struct nameidata *ndparent, *ndwatch;
358 int err;
359
360 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
361 if (unlikely(!ndparent))
362 return -ENOMEM;
363
364 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
365 if (unlikely(!ndwatch)) {
366 kfree(ndparent);
367 return -ENOMEM;
368 }
369
370 err = path_lookup(path, LOOKUP_PARENT, ndparent);
371 if (err) {
372 kfree(ndparent);
373 kfree(ndwatch);
374 return err;
375 }
376
377 err = path_lookup(path, 0, ndwatch);
378 if (err) {
379 kfree(ndwatch);
380 ndwatch = NULL;
381 }
382
383 *ndp = ndparent;
384 *ndw = ndwatch;
385
386 return 0;
387}
388
389/* Release resources used for watch path information. */
35fe4d0b 390static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
cfcad62c
EP
391{
392 if (ndp) {
393 path_put(&ndp->path);
394 kfree(ndp);
395 }
396 if (ndw) {
397 path_put(&ndw->path);
398 kfree(ndw);
399 }
400}
401
402/* Associate the given rule with an existing parent inotify_watch.
403 * Caller must hold audit_filter_mutex. */
404static void audit_add_to_parent(struct audit_krule *krule,
405 struct audit_parent *parent)
406{
407 struct audit_watch *w, *watch = krule->watch;
408 int watch_found = 0;
409
410 list_for_each_entry(w, &parent->watches, wlist) {
411 if (strcmp(watch->path, w->path))
412 continue;
413
414 watch_found = 1;
415
416 /* put krule's and initial refs to temporary watch */
417 audit_put_watch(watch);
418 audit_put_watch(watch);
419
420 audit_get_watch(w);
421 krule->watch = watch = w;
422 break;
423 }
424
425 if (!watch_found) {
426 get_inotify_watch(&parent->wdata);
427 watch->parent = parent;
428
429 list_add(&watch->wlist, &parent->watches);
430 }
431 list_add(&krule->rlist, &watch->rules);
432}
433
434/* Find a matching watch entry, or add this one.
435 * Caller must hold audit_filter_mutex. */
ae7b8f41 436int audit_add_watch(struct audit_krule *krule, struct list_head **list)
cfcad62c
EP
437{
438 struct audit_watch *watch = krule->watch;
439 struct inotify_watch *i_watch;
440 struct audit_parent *parent;
35fe4d0b 441 struct nameidata *ndp = NULL, *ndw = NULL;
ae7b8f41 442 int h, ret = 0;
cfcad62c 443
35fe4d0b
EP
444 mutex_unlock(&audit_filter_mutex);
445
446 /* Avoid calling path_lookup under audit_filter_mutex. */
447 ret = audit_get_nd(watch->path, &ndp, &ndw);
448 if (ret) {
449 /* caller expects mutex locked */
450 mutex_lock(&audit_filter_mutex);
451 goto error;
452 }
453
cfcad62c
EP
454 /* update watch filter fields */
455 if (ndw) {
456 watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
457 watch->ino = ndw->path.dentry->d_inode->i_ino;
458 }
459
460 /* The audit_filter_mutex must not be held during inotify calls because
461 * we hold it during inotify event callback processing. If an existing
462 * inotify watch is found, inotify_find_watch() grabs a reference before
463 * returning.
464 */
cfcad62c
EP
465 if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
466 &i_watch) < 0) {
467 parent = audit_init_parent(ndp);
468 if (IS_ERR(parent)) {
469 /* caller expects mutex locked */
470 mutex_lock(&audit_filter_mutex);
35fe4d0b
EP
471 ret = PTR_ERR(parent);
472 goto error;
cfcad62c
EP
473 }
474 } else
475 parent = container_of(i_watch, struct audit_parent, wdata);
476
477 mutex_lock(&audit_filter_mutex);
478
479 /* parent was moved before we took audit_filter_mutex */
480 if (parent->flags & AUDIT_PARENT_INVALID)
481 ret = -ENOENT;
482 else
483 audit_add_to_parent(krule, parent);
484
485 /* match get in audit_init_parent or inotify_find_watch */
486 put_inotify_watch(&parent->wdata);
35fe4d0b 487
ae7b8f41
EP
488 h = audit_hash_ino((u32)watch->ino);
489 *list = &audit_inode_hash[h];
35fe4d0b
EP
490error:
491 audit_put_nd(ndp, ndw); /* NULL args OK */
cfcad62c 492 return ret;
35fe4d0b 493
cfcad62c
EP
494}
495
496void audit_remove_watch_rule(struct audit_krule *krule, struct list_head *list)
497{
498 struct audit_watch *watch = krule->watch;
499 struct audit_parent *parent = watch->parent;
500
501 list_del(&krule->rlist);
502
503 if (list_empty(&watch->rules)) {
504 audit_remove_watch(watch);
505
506 if (list_empty(&parent->watches)) {
507 /* Put parent on the inotify un-registration
508 * list. Grab a reference before releasing
509 * audit_filter_mutex, to be released in
510 * audit_inotify_unregister().
511 * If filesystem is going away, just leave
512 * the sucker alone, eviction will take
513 * care of it. */
514 if (pin_inotify_watch(&parent->wdata))
515 list_add(&parent->ilist, list);
516 }
517 }
518}
519
520/* Update watch data in audit rules based on inotify events. */
521static void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
522 u32 cookie, const char *dname, struct inode *inode)
523{
524 struct audit_parent *parent;
525
526 parent = container_of(i_watch, struct audit_parent, wdata);
527
528 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
ae7b8f41 529 audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
cfcad62c
EP
530 else if (mask & (IN_DELETE|IN_MOVED_FROM))
531 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
532 /* inotify automatically removes the watch and sends IN_IGNORED */
533 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
534 audit_remove_parent_watches(parent);
535 /* inotify does not remove the watch, so remove it manually */
536 else if(mask & IN_MOVE_SELF) {
537 audit_remove_parent_watches(parent);
538 inotify_remove_watch_locked(audit_ih, i_watch);
539 } else if (mask & IN_IGNORED)
540 put_inotify_watch(i_watch);
541}
542
543static const struct inotify_operations audit_inotify_ops = {
544 .handle_event = audit_handle_ievent,
ae7b8f41 545 .destroy_watch = audit_destroy_watch,
cfcad62c
EP
546};
547
548static int __init audit_watch_init(void)
549{
550 audit_ih = inotify_init(&audit_inotify_ops);
551 if (IS_ERR(audit_ih))
552 audit_panic("cannot initialize inotify handle");
553 return 0;
554}
555subsys_initcall(audit_watch_init);