clean up audit_rule_{add,del} a bit
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / kernel / auditfilter.c
CommitLineData
fe7752ba
DW
1/* auditfilter.c -- filtering of audit events
2 *
3 * Copyright 2003-2004 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>
f368c07d
AG
25#include <linux/mutex.h>
26#include <linux/fs.h>
27#include <linux/namei.h>
fe7752ba 28#include <linux/netlink.h>
f368c07d
AG
29#include <linux/sched.h>
30#include <linux/inotify.h>
2a862b32 31#include <linux/security.h>
fe7752ba
DW
32#include "audit.h"
33
f368c07d
AG
34/*
35 * Locking model:
36 *
37 * audit_filter_mutex:
38 * Synchronizes writes and blocking reads of audit's filterlist
39 * data. Rcu is used to traverse the filterlist and access
40 * contents of structs audit_entry, audit_watch and opaque
d7a96f3a 41 * LSM rules during filtering. If modified, these structures
f368c07d
AG
42 * must be copied and replace their counterparts in the filterlist.
43 * An audit_parent struct is not accessed during filtering, so may
44 * be written directly provided audit_filter_mutex is held.
45 */
46
47/*
48 * Reference counting:
49 *
50 * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
51 * event. Each audit_watch holds a reference to its associated parent.
52 *
53 * audit_watch: if added to lists, lifetime is from audit_init_watch() to
54 * audit_remove_watch(). Additionally, an audit_watch may exist
55 * temporarily to assist in searching existing filter data. Each
56 * audit_krule holds a reference to its associated watch.
57 */
58
59struct audit_parent {
60 struct list_head ilist; /* entry in inotify registration list */
61 struct list_head watches; /* associated watches */
62 struct inotify_watch wdata; /* inotify watch data */
63 unsigned flags; /* status flags */
64};
65
66/*
67 * audit_parent status flags:
68 *
69 * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
70 * a filesystem event to ensure we're adding audit watches to a valid parent.
71 * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
72 * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
73 * we can receive while holding nameidata.
74 */
75#define AUDIT_PARENT_INVALID 0x001
76
77/* Audit filter lists, defined in <linux/audit.h> */
fe7752ba
DW
78struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
79 LIST_HEAD_INIT(audit_filter_list[0]),
80 LIST_HEAD_INIT(audit_filter_list[1]),
81 LIST_HEAD_INIT(audit_filter_list[2]),
82 LIST_HEAD_INIT(audit_filter_list[3]),
83 LIST_HEAD_INIT(audit_filter_list[4]),
84 LIST_HEAD_INIT(audit_filter_list[5]),
85#if AUDIT_NR_FILTERS != 6
86#error Fix audit_filter_list initialiser
87#endif
88};
e45aa212
AV
89static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
90 LIST_HEAD_INIT(audit_rules_list[0]),
91 LIST_HEAD_INIT(audit_rules_list[1]),
92 LIST_HEAD_INIT(audit_rules_list[2]),
93 LIST_HEAD_INIT(audit_rules_list[3]),
94 LIST_HEAD_INIT(audit_rules_list[4]),
95 LIST_HEAD_INIT(audit_rules_list[5]),
96};
fe7752ba 97
74c3cbe3 98DEFINE_MUTEX(audit_filter_mutex);
f368c07d 99
f368c07d
AG
100/* Inotify events we care about. */
101#define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
102
103void audit_free_parent(struct inotify_watch *i_watch)
104{
105 struct audit_parent *parent;
106
107 parent = container_of(i_watch, struct audit_parent, wdata);
108 WARN_ON(!list_empty(&parent->watches));
109 kfree(parent);
110}
111
112static inline void audit_get_watch(struct audit_watch *watch)
113{
114 atomic_inc(&watch->count);
115}
116
117static void audit_put_watch(struct audit_watch *watch)
118{
119 if (atomic_dec_and_test(&watch->count)) {
120 WARN_ON(watch->parent);
121 WARN_ON(!list_empty(&watch->rules));
122 kfree(watch->path);
123 kfree(watch);
124 }
125}
126
127static void audit_remove_watch(struct audit_watch *watch)
128{
129 list_del(&watch->wlist);
130 put_inotify_watch(&watch->parent->wdata);
131 watch->parent = NULL;
132 audit_put_watch(watch); /* match initial get */
133}
134
93315ed6 135static inline void audit_free_rule(struct audit_entry *e)
fe7752ba 136{
3dc7e315 137 int i;
f368c07d
AG
138
139 /* some rules don't have associated watches */
140 if (e->rule.watch)
141 audit_put_watch(e->rule.watch);
3dc7e315
DG
142 if (e->rule.fields)
143 for (i = 0; i < e->rule.field_count; i++) {
144 struct audit_field *f = &e->rule.fields[i];
04305e4a
AD
145 kfree(f->lsm_str);
146 security_audit_rule_free(f->lsm_rule);
3dc7e315 147 }
93315ed6 148 kfree(e->rule.fields);
5adc8a6a 149 kfree(e->rule.filterkey);
93315ed6
AG
150 kfree(e);
151}
152
74c3cbe3 153void audit_free_rule_rcu(struct rcu_head *head)
93315ed6
AG
154{
155 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
156 audit_free_rule(e);
157}
158
f368c07d
AG
159/* Initialize a parent watch entry. */
160static struct audit_parent *audit_init_parent(struct nameidata *ndp)
161{
162 struct audit_parent *parent;
163 s32 wd;
164
165 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
166 if (unlikely(!parent))
167 return ERR_PTR(-ENOMEM);
168
169 INIT_LIST_HEAD(&parent->watches);
170 parent->flags = 0;
171
172 inotify_init_watch(&parent->wdata);
173 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
174 get_inotify_watch(&parent->wdata);
4ac91378
JB
175 wd = inotify_add_watch(audit_ih, &parent->wdata,
176 ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
f368c07d
AG
177 if (wd < 0) {
178 audit_free_parent(&parent->wdata);
179 return ERR_PTR(wd);
180 }
181
182 return parent;
183}
184
185/* Initialize a watch entry. */
186static struct audit_watch *audit_init_watch(char *path)
187{
188 struct audit_watch *watch;
189
190 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
191 if (unlikely(!watch))
192 return ERR_PTR(-ENOMEM);
193
194 INIT_LIST_HEAD(&watch->rules);
195 atomic_set(&watch->count, 1);
196 watch->path = path;
197 watch->dev = (dev_t)-1;
198 watch->ino = (unsigned long)-1;
199
200 return watch;
201}
202
3dc7e315
DG
203/* Initialize an audit filterlist entry. */
204static inline struct audit_entry *audit_init_entry(u32 field_count)
205{
206 struct audit_entry *entry;
207 struct audit_field *fields;
208
209 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
210 if (unlikely(!entry))
211 return NULL;
212
213 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
214 if (unlikely(!fields)) {
215 kfree(entry);
216 return NULL;
217 }
218 entry->rule.fields = fields;
219
220 return entry;
221}
222
93315ed6
AG
223/* Unpack a filter field's string representation from user-space
224 * buffer. */
74c3cbe3 225char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
93315ed6
AG
226{
227 char *str;
228
229 if (!*bufp || (len == 0) || (len > *remain))
230 return ERR_PTR(-EINVAL);
231
232 /* Of the currently implemented string fields, PATH_MAX
233 * defines the longest valid length.
234 */
235 if (len > PATH_MAX)
236 return ERR_PTR(-ENAMETOOLONG);
237
238 str = kmalloc(len + 1, GFP_KERNEL);
239 if (unlikely(!str))
240 return ERR_PTR(-ENOMEM);
241
242 memcpy(str, *bufp, len);
243 str[len] = 0;
244 *bufp += len;
245 *remain -= len;
246
247 return str;
248}
249
f368c07d
AG
250/* Translate an inode field to kernel respresentation. */
251static inline int audit_to_inode(struct audit_krule *krule,
252 struct audit_field *f)
253{
254 if (krule->listnr != AUDIT_FILTER_EXIT ||
74c3cbe3 255 krule->watch || krule->inode_f || krule->tree)
f368c07d
AG
256 return -EINVAL;
257
258 krule->inode_f = f;
259 return 0;
260}
261
262/* Translate a watch string to kernel respresentation. */
263static int audit_to_watch(struct audit_krule *krule, char *path, int len,
264 u32 op)
265{
266 struct audit_watch *watch;
267
268 if (!audit_ih)
269 return -EOPNOTSUPP;
270
271 if (path[0] != '/' || path[len-1] == '/' ||
272 krule->listnr != AUDIT_FILTER_EXIT ||
273 op & ~AUDIT_EQUAL ||
74c3cbe3 274 krule->inode_f || krule->watch || krule->tree)
f368c07d
AG
275 return -EINVAL;
276
277 watch = audit_init_watch(path);
801678c5 278 if (IS_ERR(watch))
f368c07d
AG
279 return PTR_ERR(watch);
280
281 audit_get_watch(watch);
282 krule->watch = watch;
283
284 return 0;
285}
286
b915543b
AV
287static __u32 *classes[AUDIT_SYSCALL_CLASSES];
288
289int __init audit_register_class(int class, unsigned *list)
290{
291 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
292 if (!p)
293 return -ENOMEM;
294 while (*list != ~0U) {
295 unsigned n = *list++;
296 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
297 kfree(p);
298 return -EINVAL;
299 }
300 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
301 }
302 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
303 kfree(p);
304 return -EINVAL;
305 }
306 classes[class] = p;
307 return 0;
308}
309
55669bfa
AV
310int audit_match_class(int class, unsigned syscall)
311{
c926e4f4 312 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
55669bfa
AV
313 return 0;
314 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
315 return 0;
316 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
317}
318
327b9eeb 319#ifdef CONFIG_AUDITSYSCALL
e54dc243
AG
320static inline int audit_match_class_bits(int class, u32 *mask)
321{
322 int i;
323
324 if (classes[class]) {
325 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
326 if (mask[i] & classes[class][i])
327 return 0;
328 }
329 return 1;
330}
331
332static int audit_match_signal(struct audit_entry *entry)
333{
334 struct audit_field *arch = entry->rule.arch_f;
335
336 if (!arch) {
337 /* When arch is unspecified, we must check both masks on biarch
338 * as syscall number alone is ambiguous. */
339 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
340 entry->rule.mask) &&
341 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
342 entry->rule.mask));
343 }
344
345 switch(audit_classify_arch(arch->val)) {
346 case 0: /* native */
347 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
348 entry->rule.mask));
349 case 1: /* 32bit on biarch */
350 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
351 entry->rule.mask));
352 default:
353 return 1;
354 }
355}
327b9eeb 356#endif
e54dc243 357
93315ed6
AG
358/* Common user-space to kernel rule translation. */
359static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
360{
361 unsigned listnr;
362 struct audit_entry *entry;
93315ed6
AG
363 int i, err;
364
365 err = -EINVAL;
366 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
367 switch(listnr) {
368 default:
369 goto exit_err;
370 case AUDIT_FILTER_USER:
371 case AUDIT_FILTER_TYPE:
372#ifdef CONFIG_AUDITSYSCALL
373 case AUDIT_FILTER_ENTRY:
374 case AUDIT_FILTER_EXIT:
375 case AUDIT_FILTER_TASK:
376#endif
377 ;
378 }
014149cc
AV
379 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
380 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
381 goto exit_err;
382 }
383 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
93315ed6
AG
384 goto exit_err;
385 if (rule->field_count > AUDIT_MAX_FIELDS)
386 goto exit_err;
387
388 err = -ENOMEM;
3dc7e315
DG
389 entry = audit_init_entry(rule->field_count);
390 if (!entry)
93315ed6 391 goto exit_err;
93315ed6
AG
392
393 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
394 entry->rule.listnr = listnr;
395 entry->rule.action = rule->action;
396 entry->rule.field_count = rule->field_count;
93315ed6
AG
397
398 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
399 entry->rule.mask[i] = rule->mask[i];
400
b915543b
AV
401 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
402 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
403 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
404 __u32 *class;
405
406 if (!(*p & AUDIT_BIT(bit)))
407 continue;
408 *p &= ~AUDIT_BIT(bit);
409 class = classes[i];
410 if (class) {
411 int j;
412 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
413 entry->rule.mask[j] |= class[j];
414 }
415 }
416
93315ed6
AG
417 return entry;
418
419exit_err:
420 return ERR_PTR(err);
421}
422
423/* Translate struct audit_rule to kernel's rule respresentation.
424 * Exists for backward compatibility with userspace. */
425static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
426{
427 struct audit_entry *entry;
7719e437 428 struct audit_field *ino_f;
93315ed6 429 int err = 0;
fe7752ba
DW
430 int i;
431
93315ed6
AG
432 entry = audit_to_entry_common(rule);
433 if (IS_ERR(entry))
434 goto exit_nofree;
435
436 for (i = 0; i < rule->field_count; i++) {
437 struct audit_field *f = &entry->rule.fields[i];
438
93315ed6
AG
439 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
440 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
441 f->val = rule->values[i];
442
f368c07d 443 err = -EINVAL;
f368c07d 444 switch(f->type) {
0a73dccc 445 default:
3dc7e315 446 goto exit_free;
0a73dccc
AV
447 case AUDIT_PID:
448 case AUDIT_UID:
449 case AUDIT_EUID:
450 case AUDIT_SUID:
451 case AUDIT_FSUID:
452 case AUDIT_GID:
453 case AUDIT_EGID:
454 case AUDIT_SGID:
455 case AUDIT_FSGID:
456 case AUDIT_LOGINUID:
457 case AUDIT_PERS:
0a73dccc 458 case AUDIT_MSGTYPE:
3b33ac31 459 case AUDIT_PPID:
0a73dccc
AV
460 case AUDIT_DEVMAJOR:
461 case AUDIT_DEVMINOR:
462 case AUDIT_EXIT:
463 case AUDIT_SUCCESS:
74f2345b
EP
464 /* bit ops are only useful on syscall args */
465 if (f->op == AUDIT_BIT_MASK ||
466 f->op == AUDIT_BIT_TEST) {
467 err = -EINVAL;
468 goto exit_free;
469 }
470 break;
0a73dccc
AV
471 case AUDIT_ARG0:
472 case AUDIT_ARG1:
473 case AUDIT_ARG2:
474 case AUDIT_ARG3:
475 break;
4b8a311b
EP
476 /* arch is only allowed to be = or != */
477 case AUDIT_ARCH:
478 if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
479 && (f->op != AUDIT_NEGATE) && (f->op)) {
480 err = -EINVAL;
481 goto exit_free;
482 }
e54dc243 483 entry->rule.arch_f = f;
4b8a311b 484 break;
55669bfa
AV
485 case AUDIT_PERM:
486 if (f->val & ~15)
487 goto exit_free;
488 break;
8b67dca9
AV
489 case AUDIT_FILETYPE:
490 if ((f->val & ~S_IFMT) > S_IFMT)
491 goto exit_free;
492 break;
f368c07d
AG
493 case AUDIT_INODE:
494 err = audit_to_inode(&entry->rule, f);
495 if (err)
496 goto exit_free;
497 break;
3dc7e315
DG
498 }
499
93315ed6 500 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
d9d9ec6e
DK
501
502 /* Support for legacy operators where
503 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
93315ed6 504 if (f->op & AUDIT_NEGATE)
d9d9ec6e
DK
505 f->op = AUDIT_NOT_EQUAL;
506 else if (!f->op)
507 f->op = AUDIT_EQUAL;
508 else if (f->op == AUDIT_OPERATORS) {
509 err = -EINVAL;
510 goto exit_free;
511 }
fe7752ba 512 }
93315ed6 513
7719e437
HH
514 ino_f = entry->rule.inode_f;
515 if (ino_f) {
516 switch(ino_f->op) {
f368c07d
AG
517 case AUDIT_NOT_EQUAL:
518 entry->rule.inode_f = NULL;
519 case AUDIT_EQUAL:
520 break;
521 default:
5422e01a 522 err = -EINVAL;
f368c07d
AG
523 goto exit_free;
524 }
525 }
526
93315ed6
AG
527exit_nofree:
528 return entry;
529
530exit_free:
531 audit_free_rule(entry);
532 return ERR_PTR(err);
fe7752ba
DW
533}
534
93315ed6
AG
535/* Translate struct audit_rule_data to kernel's rule respresentation. */
536static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
537 size_t datasz)
fe7752ba 538{
93315ed6
AG
539 int err = 0;
540 struct audit_entry *entry;
7719e437 541 struct audit_field *ino_f;
93315ed6 542 void *bufp;
3dc7e315 543 size_t remain = datasz - sizeof(struct audit_rule_data);
fe7752ba 544 int i;
3dc7e315 545 char *str;
fe7752ba 546
93315ed6
AG
547 entry = audit_to_entry_common((struct audit_rule *)data);
548 if (IS_ERR(entry))
549 goto exit_nofree;
fe7752ba 550
93315ed6
AG
551 bufp = data->buf;
552 entry->rule.vers_ops = 2;
553 for (i = 0; i < data->field_count; i++) {
554 struct audit_field *f = &entry->rule.fields[i];
555
556 err = -EINVAL;
557 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
558 data->fieldflags[i] & ~AUDIT_OPERATORS)
559 goto exit_free;
560
561 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
562 f->type = data->fields[i];
3dc7e315 563 f->val = data->values[i];
04305e4a
AD
564 f->lsm_str = NULL;
565 f->lsm_rule = NULL;
93315ed6 566 switch(f->type) {
0a73dccc
AV
567 case AUDIT_PID:
568 case AUDIT_UID:
569 case AUDIT_EUID:
570 case AUDIT_SUID:
571 case AUDIT_FSUID:
572 case AUDIT_GID:
573 case AUDIT_EGID:
574 case AUDIT_SGID:
575 case AUDIT_FSGID:
576 case AUDIT_LOGINUID:
577 case AUDIT_PERS:
0a73dccc
AV
578 case AUDIT_MSGTYPE:
579 case AUDIT_PPID:
580 case AUDIT_DEVMAJOR:
581 case AUDIT_DEVMINOR:
582 case AUDIT_EXIT:
583 case AUDIT_SUCCESS:
584 case AUDIT_ARG0:
585 case AUDIT_ARG1:
586 case AUDIT_ARG2:
587 case AUDIT_ARG3:
588 break;
e54dc243
AG
589 case AUDIT_ARCH:
590 entry->rule.arch_f = f;
591 break;
3a6b9f85
DG
592 case AUDIT_SUBJ_USER:
593 case AUDIT_SUBJ_ROLE:
594 case AUDIT_SUBJ_TYPE:
595 case AUDIT_SUBJ_SEN:
596 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
597 case AUDIT_OBJ_USER:
598 case AUDIT_OBJ_ROLE:
599 case AUDIT_OBJ_TYPE:
600 case AUDIT_OBJ_LEV_LOW:
601 case AUDIT_OBJ_LEV_HIGH:
3dc7e315
DG
602 str = audit_unpack_string(&bufp, &remain, f->val);
603 if (IS_ERR(str))
604 goto exit_free;
605 entry->rule.buflen += f->val;
606
d7a96f3a 607 err = security_audit_rule_init(f->type, f->op, str,
04305e4a 608 (void **)&f->lsm_rule);
3dc7e315
DG
609 /* Keep currently invalid fields around in case they
610 * become valid after a policy reload. */
611 if (err == -EINVAL) {
d7a96f3a 612 printk(KERN_WARNING "audit rule for LSM "
3dc7e315
DG
613 "\'%s\' is invalid\n", str);
614 err = 0;
615 }
616 if (err) {
617 kfree(str);
618 goto exit_free;
619 } else
04305e4a 620 f->lsm_str = str;
3dc7e315 621 break;
f368c07d
AG
622 case AUDIT_WATCH:
623 str = audit_unpack_string(&bufp, &remain, f->val);
624 if (IS_ERR(str))
625 goto exit_free;
626 entry->rule.buflen += f->val;
627
628 err = audit_to_watch(&entry->rule, str, f->val, f->op);
629 if (err) {
630 kfree(str);
631 goto exit_free;
632 }
633 break;
74c3cbe3
AV
634 case AUDIT_DIR:
635 str = audit_unpack_string(&bufp, &remain, f->val);
636 if (IS_ERR(str))
637 goto exit_free;
638 entry->rule.buflen += f->val;
639
640 err = audit_make_tree(&entry->rule, str, f->op);
641 kfree(str);
642 if (err)
643 goto exit_free;
644 break;
f368c07d
AG
645 case AUDIT_INODE:
646 err = audit_to_inode(&entry->rule, f);
647 if (err)
648 goto exit_free;
649 break;
5adc8a6a
AG
650 case AUDIT_FILTERKEY:
651 err = -EINVAL;
652 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
653 goto exit_free;
654 str = audit_unpack_string(&bufp, &remain, f->val);
655 if (IS_ERR(str))
656 goto exit_free;
657 entry->rule.buflen += f->val;
658 entry->rule.filterkey = str;
659 break;
55669bfa
AV
660 case AUDIT_PERM:
661 if (f->val & ~15)
662 goto exit_free;
663 break;
8b67dca9
AV
664 case AUDIT_FILETYPE:
665 if ((f->val & ~S_IFMT) > S_IFMT)
666 goto exit_free;
667 break;
0a73dccc
AV
668 default:
669 goto exit_free;
f368c07d
AG
670 }
671 }
672
7719e437
HH
673 ino_f = entry->rule.inode_f;
674 if (ino_f) {
675 switch(ino_f->op) {
f368c07d
AG
676 case AUDIT_NOT_EQUAL:
677 entry->rule.inode_f = NULL;
678 case AUDIT_EQUAL:
679 break;
680 default:
5422e01a 681 err = -EINVAL;
f368c07d 682 goto exit_free;
93315ed6
AG
683 }
684 }
685
686exit_nofree:
687 return entry;
688
689exit_free:
690 audit_free_rule(entry);
691 return ERR_PTR(err);
692}
693
694/* Pack a filter field's string representation into data block. */
74c3cbe3 695static inline size_t audit_pack_string(void **bufp, const char *str)
93315ed6
AG
696{
697 size_t len = strlen(str);
698
699 memcpy(*bufp, str, len);
700 *bufp += len;
701
702 return len;
703}
704
705/* Translate kernel rule respresentation to struct audit_rule.
706 * Exists for backward compatibility with userspace. */
707static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
708{
709 struct audit_rule *rule;
710 int i;
711
4668edc3 712 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
93315ed6 713 if (unlikely(!rule))
0a3b483e 714 return NULL;
93315ed6
AG
715
716 rule->flags = krule->flags | krule->listnr;
717 rule->action = krule->action;
718 rule->field_count = krule->field_count;
719 for (i = 0; i < rule->field_count; i++) {
720 rule->values[i] = krule->fields[i].val;
721 rule->fields[i] = krule->fields[i].type;
722
723 if (krule->vers_ops == 1) {
724 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
725 rule->fields[i] |= AUDIT_NEGATE;
726 } else {
727 rule->fields[i] |= krule->fields[i].op;
728 }
729 }
730 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
731
732 return rule;
733}
fe7752ba 734
93315ed6
AG
735/* Translate kernel rule respresentation to struct audit_rule_data. */
736static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
737{
738 struct audit_rule_data *data;
739 void *bufp;
740 int i;
741
742 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
743 if (unlikely(!data))
0a3b483e 744 return NULL;
93315ed6
AG
745 memset(data, 0, sizeof(*data));
746
747 data->flags = krule->flags | krule->listnr;
748 data->action = krule->action;
749 data->field_count = krule->field_count;
750 bufp = data->buf;
751 for (i = 0; i < data->field_count; i++) {
752 struct audit_field *f = &krule->fields[i];
753
754 data->fields[i] = f->type;
755 data->fieldflags[i] = f->op;
756 switch(f->type) {
3a6b9f85
DG
757 case AUDIT_SUBJ_USER:
758 case AUDIT_SUBJ_ROLE:
759 case AUDIT_SUBJ_TYPE:
760 case AUDIT_SUBJ_SEN:
761 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
762 case AUDIT_OBJ_USER:
763 case AUDIT_OBJ_ROLE:
764 case AUDIT_OBJ_TYPE:
765 case AUDIT_OBJ_LEV_LOW:
766 case AUDIT_OBJ_LEV_HIGH:
3dc7e315 767 data->buflen += data->values[i] =
04305e4a 768 audit_pack_string(&bufp, f->lsm_str);
3dc7e315 769 break;
f368c07d
AG
770 case AUDIT_WATCH:
771 data->buflen += data->values[i] =
772 audit_pack_string(&bufp, krule->watch->path);
773 break;
74c3cbe3
AV
774 case AUDIT_DIR:
775 data->buflen += data->values[i] =
776 audit_pack_string(&bufp,
777 audit_tree_path(krule->tree));
778 break;
5adc8a6a
AG
779 case AUDIT_FILTERKEY:
780 data->buflen += data->values[i] =
781 audit_pack_string(&bufp, krule->filterkey);
782 break;
93315ed6
AG
783 default:
784 data->values[i] = f->val;
785 }
786 }
787 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
788
789 return data;
790}
791
792/* Compare two rules in kernel format. Considered success if rules
793 * don't match. */
794static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
795{
796 int i;
797
798 if (a->flags != b->flags ||
799 a->listnr != b->listnr ||
800 a->action != b->action ||
801 a->field_count != b->field_count)
fe7752ba
DW
802 return 1;
803
804 for (i = 0; i < a->field_count; i++) {
93315ed6
AG
805 if (a->fields[i].type != b->fields[i].type ||
806 a->fields[i].op != b->fields[i].op)
fe7752ba 807 return 1;
93315ed6
AG
808
809 switch(a->fields[i].type) {
3a6b9f85
DG
810 case AUDIT_SUBJ_USER:
811 case AUDIT_SUBJ_ROLE:
812 case AUDIT_SUBJ_TYPE:
813 case AUDIT_SUBJ_SEN:
814 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
815 case AUDIT_OBJ_USER:
816 case AUDIT_OBJ_ROLE:
817 case AUDIT_OBJ_TYPE:
818 case AUDIT_OBJ_LEV_LOW:
819 case AUDIT_OBJ_LEV_HIGH:
04305e4a 820 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
3dc7e315
DG
821 return 1;
822 break;
f368c07d
AG
823 case AUDIT_WATCH:
824 if (strcmp(a->watch->path, b->watch->path))
825 return 1;
826 break;
74c3cbe3
AV
827 case AUDIT_DIR:
828 if (strcmp(audit_tree_path(a->tree),
829 audit_tree_path(b->tree)))
830 return 1;
831 break;
5adc8a6a
AG
832 case AUDIT_FILTERKEY:
833 /* both filterkeys exist based on above type compare */
834 if (strcmp(a->filterkey, b->filterkey))
835 return 1;
836 break;
93315ed6
AG
837 default:
838 if (a->fields[i].val != b->fields[i].val)
839 return 1;
840 }
fe7752ba
DW
841 }
842
843 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
844 if (a->mask[i] != b->mask[i])
845 return 1;
846
847 return 0;
848}
849
f368c07d
AG
850/* Duplicate the given audit watch. The new watch's rules list is initialized
851 * to an empty list and wlist is undefined. */
852static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
853{
854 char *path;
855 struct audit_watch *new;
856
857 path = kstrdup(old->path, GFP_KERNEL);
858 if (unlikely(!path))
859 return ERR_PTR(-ENOMEM);
860
861 new = audit_init_watch(path);
801678c5 862 if (IS_ERR(new)) {
f368c07d
AG
863 kfree(path);
864 goto out;
865 }
866
867 new->dev = old->dev;
868 new->ino = old->ino;
869 get_inotify_watch(&old->parent->wdata);
870 new->parent = old->parent;
871
872out:
873 return new;
874}
875
04305e4a 876/* Duplicate LSM field information. The lsm_rule is opaque, so must be
3dc7e315 877 * re-initialized. */
d7a96f3a 878static inline int audit_dupe_lsm_field(struct audit_field *df,
3dc7e315
DG
879 struct audit_field *sf)
880{
881 int ret = 0;
04305e4a 882 char *lsm_str;
3dc7e315 883
04305e4a
AD
884 /* our own copy of lsm_str */
885 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
886 if (unlikely(!lsm_str))
3e1fbd12 887 return -ENOMEM;
04305e4a 888 df->lsm_str = lsm_str;
3dc7e315 889
04305e4a
AD
890 /* our own (refreshed) copy of lsm_rule */
891 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
892 (void **)&df->lsm_rule);
3dc7e315
DG
893 /* Keep currently invalid fields around in case they
894 * become valid after a policy reload. */
895 if (ret == -EINVAL) {
d7a96f3a 896 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
04305e4a 897 "invalid\n", df->lsm_str);
3dc7e315
DG
898 ret = 0;
899 }
900
901 return ret;
902}
903
904/* Duplicate an audit rule. This will be a deep copy with the exception
d7a96f3a 905 * of the watch - that pointer is carried over. The LSM specific fields
3dc7e315 906 * will be updated in the copy. The point is to be able to replace the old
f368c07d
AG
907 * rule with the new rule in the filterlist, then free the old rule.
908 * The rlist element is undefined; list manipulations are handled apart from
909 * the initial copy. */
910static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
911 struct audit_watch *watch)
3dc7e315
DG
912{
913 u32 fcount = old->field_count;
914 struct audit_entry *entry;
915 struct audit_krule *new;
5adc8a6a 916 char *fk;
3dc7e315
DG
917 int i, err = 0;
918
919 entry = audit_init_entry(fcount);
920 if (unlikely(!entry))
921 return ERR_PTR(-ENOMEM);
922
923 new = &entry->rule;
924 new->vers_ops = old->vers_ops;
925 new->flags = old->flags;
926 new->listnr = old->listnr;
927 new->action = old->action;
928 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
929 new->mask[i] = old->mask[i];
0590b933 930 new->prio = old->prio;
3dc7e315 931 new->buflen = old->buflen;
f368c07d
AG
932 new->inode_f = old->inode_f;
933 new->watch = NULL;
3dc7e315 934 new->field_count = old->field_count;
74c3cbe3
AV
935 /*
936 * note that we are OK with not refcounting here; audit_match_tree()
937 * never dereferences tree and we can't get false positives there
938 * since we'd have to have rule gone from the list *and* removed
939 * before the chunks found by lookup had been allocated, i.e. before
940 * the beginning of list scan.
941 */
942 new->tree = old->tree;
3dc7e315
DG
943 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
944
04305e4a 945 /* deep copy this information, updating the lsm_rule fields, because
3dc7e315
DG
946 * the originals will all be freed when the old rule is freed. */
947 for (i = 0; i < fcount; i++) {
948 switch (new->fields[i].type) {
3a6b9f85
DG
949 case AUDIT_SUBJ_USER:
950 case AUDIT_SUBJ_ROLE:
951 case AUDIT_SUBJ_TYPE:
952 case AUDIT_SUBJ_SEN:
953 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
954 case AUDIT_OBJ_USER:
955 case AUDIT_OBJ_ROLE:
956 case AUDIT_OBJ_TYPE:
957 case AUDIT_OBJ_LEV_LOW:
958 case AUDIT_OBJ_LEV_HIGH:
d7a96f3a 959 err = audit_dupe_lsm_field(&new->fields[i],
3dc7e315 960 &old->fields[i]);
5adc8a6a
AG
961 break;
962 case AUDIT_FILTERKEY:
963 fk = kstrdup(old->filterkey, GFP_KERNEL);
964 if (unlikely(!fk))
965 err = -ENOMEM;
966 else
967 new->filterkey = fk;
3dc7e315
DG
968 }
969 if (err) {
970 audit_free_rule(entry);
971 return ERR_PTR(err);
972 }
973 }
974
f368c07d
AG
975 if (watch) {
976 audit_get_watch(watch);
977 new->watch = watch;
978 }
979
3dc7e315
DG
980 return entry;
981}
982
f368c07d
AG
983/* Update inode info in audit rules based on filesystem event. */
984static void audit_update_watch(struct audit_parent *parent,
985 const char *dname, dev_t dev,
986 unsigned long ino, unsigned invalidating)
987{
988 struct audit_watch *owatch, *nwatch, *nextw;
989 struct audit_krule *r, *nextr;
990 struct audit_entry *oentry, *nentry;
f368c07d
AG
991
992 mutex_lock(&audit_filter_mutex);
993 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
9c937dcc 994 if (audit_compare_dname_path(dname, owatch->path, NULL))
f368c07d
AG
995 continue;
996
997 /* If the update involves invalidating rules, do the inode-based
998 * filtering now, so we don't omit records. */
0590b933
AV
999 if (invalidating && current->audit_context)
1000 audit_filter_inodes(current, current->audit_context);
f368c07d
AG
1001
1002 nwatch = audit_dupe_watch(owatch);
801678c5 1003 if (IS_ERR(nwatch)) {
f368c07d
AG
1004 mutex_unlock(&audit_filter_mutex);
1005 audit_panic("error updating watch, skipping");
1006 return;
1007 }
1008 nwatch->dev = dev;
1009 nwatch->ino = ino;
1010
1011 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
1012
1013 oentry = container_of(r, struct audit_entry, rule);
1014 list_del(&oentry->rule.rlist);
1015 list_del_rcu(&oentry->list);
1016
1017 nentry = audit_dupe_rule(&oentry->rule, nwatch);
e45aa212
AV
1018 if (IS_ERR(nentry)) {
1019 list_del(&oentry->rule.list);
f368c07d 1020 audit_panic("error updating watch, removing");
e45aa212 1021 } else {
f368c07d
AG
1022 int h = audit_hash_ino((u32)ino);
1023 list_add(&nentry->rule.rlist, &nwatch->rules);
1024 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
e45aa212
AV
1025 list_replace(&oentry->rule.list,
1026 &nentry->rule.list);
f368c07d
AG
1027 }
1028
1029 call_rcu(&oentry->rcu, audit_free_rule_rcu);
1030 }
1031
1a6b9f23
EP
1032 if (audit_enabled) {
1033 struct audit_buffer *ab;
1034 ab = audit_log_start(NULL, GFP_KERNEL,
1035 AUDIT_CONFIG_CHANGE);
036bbf76 1036 audit_log_format(ab, "auid=%u ses=%u",
1037 audit_get_loginuid(current),
1038 audit_get_sessionid(current));
1a6b9f23 1039 audit_log_format(ab,
036bbf76 1040 " op=updated rules specifying path=");
1a6b9f23
EP
1041 audit_log_untrustedstring(ab, owatch->path);
1042 audit_log_format(ab, " with dev=%u ino=%lu\n",
1043 dev, ino);
1044 audit_log_format(ab, " list=%d res=1", r->listnr);
1045 audit_log_end(ab);
1046 }
f368c07d
AG
1047 audit_remove_watch(owatch);
1048 goto add_watch_to_parent; /* event applies to a single watch */
1049 }
1050 mutex_unlock(&audit_filter_mutex);
1051 return;
1052
1053add_watch_to_parent:
1054 list_add(&nwatch->wlist, &parent->watches);
1055 mutex_unlock(&audit_filter_mutex);
1056 return;
1057}
1058
1059/* Remove all watches & rules associated with a parent that is going away. */
1060static void audit_remove_parent_watches(struct audit_parent *parent)
1061{
1062 struct audit_watch *w, *nextw;
1063 struct audit_krule *r, *nextr;
1064 struct audit_entry *e;
1065
1066 mutex_lock(&audit_filter_mutex);
1067 parent->flags |= AUDIT_PARENT_INVALID;
1068 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
1069 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
1070 e = container_of(r, struct audit_entry, rule);
1a6b9f23
EP
1071 if (audit_enabled) {
1072 struct audit_buffer *ab;
1073 ab = audit_log_start(NULL, GFP_KERNEL,
1074 AUDIT_CONFIG_CHANGE);
036bbf76 1075 audit_log_format(ab, "auid=%u ses=%u",
1076 audit_get_loginuid(current),
1077 audit_get_sessionid(current));
1078 audit_log_format(ab, " op=remove rule path=");
1a6b9f23
EP
1079 audit_log_untrustedstring(ab, w->path);
1080 if (r->filterkey) {
1081 audit_log_format(ab, " key=");
1082 audit_log_untrustedstring(ab,
1083 r->filterkey);
1084 } else
1085 audit_log_format(ab, " key=(null)");
1086 audit_log_format(ab, " list=%d res=1",
1087 r->listnr);
1088 audit_log_end(ab);
1089 }
f368c07d 1090 list_del(&r->rlist);
e45aa212 1091 list_del(&r->list);
f368c07d
AG
1092 list_del_rcu(&e->list);
1093 call_rcu(&e->rcu, audit_free_rule_rcu);
f368c07d
AG
1094 }
1095 audit_remove_watch(w);
1096 }
1097 mutex_unlock(&audit_filter_mutex);
1098}
1099
1100/* Unregister inotify watches for parents on in_list.
1101 * Generates an IN_IGNORED event. */
1102static void audit_inotify_unregister(struct list_head *in_list)
1103{
1104 struct audit_parent *p, *n;
1105
1106 list_for_each_entry_safe(p, n, in_list, ilist) {
1107 list_del(&p->ilist);
1108 inotify_rm_watch(audit_ih, &p->wdata);
8f7b0ba1
AV
1109 /* the unpin matching the pin in audit_do_del_rule() */
1110 unpin_inotify_watch(&p->wdata);
f368c07d
AG
1111 }
1112}
1113
1114/* Find an existing audit rule.
1115 * Caller must hold audit_filter_mutex to prevent stale rule data. */
1116static struct audit_entry *audit_find_rule(struct audit_entry *entry,
36c4f1b1 1117 struct list_head **p)
f368c07d
AG
1118{
1119 struct audit_entry *e, *found = NULL;
36c4f1b1 1120 struct list_head *list;
f368c07d
AG
1121 int h;
1122
36c4f1b1
AV
1123 if (entry->rule.inode_f) {
1124 h = audit_hash_ino(entry->rule.inode_f->val);
1125 *p = list = &audit_inode_hash[h];
1126 } else if (entry->rule.watch) {
f368c07d
AG
1127 /* we don't know the inode number, so must walk entire hash */
1128 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
1129 list = &audit_inode_hash[h];
1130 list_for_each_entry(e, list, list)
1131 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1132 found = e;
1133 goto out;
1134 }
1135 }
1136 goto out;
36c4f1b1
AV
1137 } else {
1138 *p = list = &audit_filter_list[entry->rule.listnr];
f368c07d
AG
1139 }
1140
1141 list_for_each_entry(e, list, list)
1142 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1143 found = e;
1144 goto out;
1145 }
1146
1147out:
1148 return found;
1149}
1150
1151/* Get path information necessary for adding watches. */
1152static int audit_get_nd(char *path, struct nameidata **ndp,
1153 struct nameidata **ndw)
1154{
1155 struct nameidata *ndparent, *ndwatch;
1156 int err;
1157
1158 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
1159 if (unlikely(!ndparent))
1160 return -ENOMEM;
1161
1162 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
1163 if (unlikely(!ndwatch)) {
1164 kfree(ndparent);
1165 return -ENOMEM;
1166 }
1167
1168 err = path_lookup(path, LOOKUP_PARENT, ndparent);
1169 if (err) {
1170 kfree(ndparent);
1171 kfree(ndwatch);
1172 return err;
1173 }
1174
1175 err = path_lookup(path, 0, ndwatch);
1176 if (err) {
1177 kfree(ndwatch);
1178 ndwatch = NULL;
1179 }
1180
1181 *ndp = ndparent;
1182 *ndw = ndwatch;
1183
1184 return 0;
1185}
1186
1187/* Release resources used for watch path information. */
1188static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
1189{
1190 if (ndp) {
1d957f9b 1191 path_put(&ndp->path);
f368c07d
AG
1192 kfree(ndp);
1193 }
1194 if (ndw) {
1d957f9b 1195 path_put(&ndw->path);
f368c07d
AG
1196 kfree(ndw);
1197 }
1198}
1199
1200/* Associate the given rule with an existing parent inotify_watch.
1201 * Caller must hold audit_filter_mutex. */
1202static void audit_add_to_parent(struct audit_krule *krule,
1203 struct audit_parent *parent)
1204{
1205 struct audit_watch *w, *watch = krule->watch;
1206 int watch_found = 0;
1207
1208 list_for_each_entry(w, &parent->watches, wlist) {
1209 if (strcmp(watch->path, w->path))
1210 continue;
1211
1212 watch_found = 1;
1213
1214 /* put krule's and initial refs to temporary watch */
1215 audit_put_watch(watch);
1216 audit_put_watch(watch);
1217
1218 audit_get_watch(w);
1219 krule->watch = watch = w;
1220 break;
1221 }
1222
1223 if (!watch_found) {
1224 get_inotify_watch(&parent->wdata);
1225 watch->parent = parent;
1226
1227 list_add(&watch->wlist, &parent->watches);
1228 }
1229 list_add(&krule->rlist, &watch->rules);
1230}
1231
1232/* Find a matching watch entry, or add this one.
1233 * Caller must hold audit_filter_mutex. */
1234static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
1235 struct nameidata *ndw)
1236{
1237 struct audit_watch *watch = krule->watch;
1238 struct inotify_watch *i_watch;
1239 struct audit_parent *parent;
1240 int ret = 0;
1241
1242 /* update watch filter fields */
1243 if (ndw) {
4ac91378
JB
1244 watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
1245 watch->ino = ndw->path.dentry->d_inode->i_ino;
f368c07d
AG
1246 }
1247
1248 /* The audit_filter_mutex must not be held during inotify calls because
1249 * we hold it during inotify event callback processing. If an existing
1250 * inotify watch is found, inotify_find_watch() grabs a reference before
1251 * returning.
1252 */
1253 mutex_unlock(&audit_filter_mutex);
1254
4ac91378
JB
1255 if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
1256 &i_watch) < 0) {
f368c07d
AG
1257 parent = audit_init_parent(ndp);
1258 if (IS_ERR(parent)) {
1259 /* caller expects mutex locked */
1260 mutex_lock(&audit_filter_mutex);
1261 return PTR_ERR(parent);
1262 }
1263 } else
1264 parent = container_of(i_watch, struct audit_parent, wdata);
1265
1266 mutex_lock(&audit_filter_mutex);
1267
1268 /* parent was moved before we took audit_filter_mutex */
1269 if (parent->flags & AUDIT_PARENT_INVALID)
1270 ret = -ENOENT;
1271 else
1272 audit_add_to_parent(krule, parent);
1273
1274 /* match get in audit_init_parent or inotify_find_watch */
1275 put_inotify_watch(&parent->wdata);
1276 return ret;
1277}
1278
0590b933
AV
1279static u64 prio_low = ~0ULL/2;
1280static u64 prio_high = ~0ULL/2 - 1;
1281
f368c07d 1282/* Add rule to given filterlist if not a duplicate. */
36c4f1b1 1283static inline int audit_add_rule(struct audit_entry *entry)
fe7752ba 1284{
93315ed6 1285 struct audit_entry *e;
f368c07d 1286 struct audit_watch *watch = entry->rule.watch;
74c3cbe3 1287 struct audit_tree *tree = entry->rule.tree;
6f686d3d 1288 struct nameidata *ndp = NULL, *ndw = NULL;
36c4f1b1 1289 struct list_head *list;
6f686d3d 1290 int h, err;
471a5c7c
AV
1291#ifdef CONFIG_AUDITSYSCALL
1292 int dont_count = 0;
1293
1294 /* If either of these, don't count towards total */
1295 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1296 entry->rule.listnr == AUDIT_FILTER_TYPE)
1297 dont_count = 1;
1298#endif
f368c07d 1299
f368c07d 1300 mutex_lock(&audit_filter_mutex);
36c4f1b1 1301 e = audit_find_rule(entry, &list);
f368c07d
AG
1302 mutex_unlock(&audit_filter_mutex);
1303 if (e) {
1304 err = -EEXIST;
74c3cbe3
AV
1305 /* normally audit_add_tree_rule() will free it on failure */
1306 if (tree)
1307 audit_put_tree(tree);
f368c07d
AG
1308 goto error;
1309 }
fe7752ba 1310
f368c07d
AG
1311 /* Avoid calling path_lookup under audit_filter_mutex. */
1312 if (watch) {
1313 err = audit_get_nd(watch->path, &ndp, &ndw);
1314 if (err)
1315 goto error;
f368c07d
AG
1316 }
1317
1318 mutex_lock(&audit_filter_mutex);
1319 if (watch) {
1320 /* audit_filter_mutex is dropped and re-taken during this call */
1321 err = audit_add_watch(&entry->rule, ndp, ndw);
1322 if (err) {
1323 mutex_unlock(&audit_filter_mutex);
1324 goto error;
1325 }
1326 h = audit_hash_ino((u32)watch->ino);
1327 list = &audit_inode_hash[h];
fe7752ba 1328 }
74c3cbe3
AV
1329 if (tree) {
1330 err = audit_add_tree_rule(&entry->rule);
1331 if (err) {
1332 mutex_unlock(&audit_filter_mutex);
1333 goto error;
1334 }
1335 }
fe7752ba 1336
0590b933
AV
1337 entry->rule.prio = ~0ULL;
1338 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
1339 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
1340 entry->rule.prio = ++prio_high;
1341 else
1342 entry->rule.prio = --prio_low;
1343 }
1344
fe7752ba 1345 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
e45aa212
AV
1346 list_add(&entry->rule.list,
1347 &audit_rules_list[entry->rule.listnr]);
fe7752ba 1348 list_add_rcu(&entry->list, list);
6a2bceec 1349 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
fe7752ba 1350 } else {
e45aa212
AV
1351 list_add_tail(&entry->rule.list,
1352 &audit_rules_list[entry->rule.listnr]);
fe7752ba
DW
1353 list_add_tail_rcu(&entry->list, list);
1354 }
471a5c7c
AV
1355#ifdef CONFIG_AUDITSYSCALL
1356 if (!dont_count)
1357 audit_n_rules++;
e54dc243
AG
1358
1359 if (!audit_match_signal(entry))
1360 audit_signals++;
471a5c7c 1361#endif
f368c07d 1362 mutex_unlock(&audit_filter_mutex);
fe7752ba 1363
6f686d3d 1364 audit_put_nd(ndp, ndw); /* NULL args OK */
f368c07d
AG
1365 return 0;
1366
1367error:
6f686d3d 1368 audit_put_nd(ndp, ndw); /* NULL args OK */
f368c07d
AG
1369 if (watch)
1370 audit_put_watch(watch); /* tmp watch, matches initial get */
1371 return err;
fe7752ba
DW
1372}
1373
f368c07d 1374/* Remove an existing rule from filterlist. */
36c4f1b1 1375static inline int audit_del_rule(struct audit_entry *entry)
fe7752ba
DW
1376{
1377 struct audit_entry *e;
f368c07d 1378 struct audit_watch *watch, *tmp_watch = entry->rule.watch;
74c3cbe3 1379 struct audit_tree *tree = entry->rule.tree;
36c4f1b1 1380 struct list_head *list;
f368c07d 1381 LIST_HEAD(inotify_list);
36c4f1b1 1382 int ret = 0;
471a5c7c
AV
1383#ifdef CONFIG_AUDITSYSCALL
1384 int dont_count = 0;
1385
1386 /* If either of these, don't count towards total */
1387 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1388 entry->rule.listnr == AUDIT_FILTER_TYPE)
1389 dont_count = 1;
1390#endif
f368c07d 1391
f368c07d 1392 mutex_lock(&audit_filter_mutex);
36c4f1b1 1393 e = audit_find_rule(entry, &list);
f368c07d
AG
1394 if (!e) {
1395 mutex_unlock(&audit_filter_mutex);
1396 ret = -ENOENT;
1397 goto out;
1398 }
1399
1400 watch = e->rule.watch;
1401 if (watch) {
1402 struct audit_parent *parent = watch->parent;
1403
1404 list_del(&e->rule.rlist);
1405
1406 if (list_empty(&watch->rules)) {
1407 audit_remove_watch(watch);
1408
1409 if (list_empty(&parent->watches)) {
1410 /* Put parent on the inotify un-registration
1411 * list. Grab a reference before releasing
1412 * audit_filter_mutex, to be released in
8f7b0ba1
AV
1413 * audit_inotify_unregister().
1414 * If filesystem is going away, just leave
1415 * the sucker alone, eviction will take
1416 * care of it.
1417 */
1418 if (pin_inotify_watch(&parent->wdata))
1419 list_add(&parent->ilist, &inotify_list);
f368c07d 1420 }
fe7752ba
DW
1421 }
1422 }
f368c07d 1423
74c3cbe3
AV
1424 if (e->rule.tree)
1425 audit_remove_tree_rule(&e->rule);
1426
f368c07d 1427 list_del_rcu(&e->list);
e45aa212 1428 list_del(&e->rule.list);
f368c07d
AG
1429 call_rcu(&e->rcu, audit_free_rule_rcu);
1430
471a5c7c
AV
1431#ifdef CONFIG_AUDITSYSCALL
1432 if (!dont_count)
1433 audit_n_rules--;
e54dc243
AG
1434
1435 if (!audit_match_signal(entry))
1436 audit_signals--;
471a5c7c 1437#endif
f368c07d
AG
1438 mutex_unlock(&audit_filter_mutex);
1439
1440 if (!list_empty(&inotify_list))
1441 audit_inotify_unregister(&inotify_list);
1442
1443out:
1444 if (tmp_watch)
1445 audit_put_watch(tmp_watch); /* match initial get */
74c3cbe3
AV
1446 if (tree)
1447 audit_put_tree(tree); /* that's the temporary one */
f368c07d
AG
1448
1449 return ret;
fe7752ba
DW
1450}
1451
93315ed6
AG
1452/* List rules using struct audit_rule. Exists for backward
1453 * compatibility with userspace. */
9044e6bc 1454static void audit_list(int pid, int seq, struct sk_buff_head *q)
fe7752ba 1455{
9044e6bc 1456 struct sk_buff *skb;
e45aa212 1457 struct audit_krule *r;
fe7752ba
DW
1458 int i;
1459
f368c07d
AG
1460 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1461 * iterator to sync with list writers. */
fe7752ba 1462 for (i=0; i<AUDIT_NR_FILTERS; i++) {
e45aa212 1463 list_for_each_entry(r, &audit_rules_list[i], list) {
f368c07d
AG
1464 struct audit_rule *rule;
1465
e45aa212 1466 rule = audit_krule_to_rule(r);
f368c07d
AG
1467 if (unlikely(!rule))
1468 break;
1469 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1470 rule, sizeof(*rule));
1471 if (skb)
1472 skb_queue_tail(q, skb);
1473 kfree(rule);
1474 }
1475 }
9044e6bc
AV
1476 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
1477 if (skb)
1478 skb_queue_tail(q, skb);
fe7752ba
DW
1479}
1480
93315ed6 1481/* List rules using struct audit_rule_data. */
9044e6bc 1482static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
93315ed6 1483{
9044e6bc 1484 struct sk_buff *skb;
e45aa212 1485 struct audit_krule *r;
93315ed6
AG
1486 int i;
1487
f368c07d
AG
1488 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1489 * iterator to sync with list writers. */
93315ed6 1490 for (i=0; i<AUDIT_NR_FILTERS; i++) {
e45aa212 1491 list_for_each_entry(r, &audit_rules_list[i], list) {
f368c07d
AG
1492 struct audit_rule_data *data;
1493
e45aa212 1494 data = audit_krule_to_data(r);
f368c07d
AG
1495 if (unlikely(!data))
1496 break;
1497 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1498 data, sizeof(*data) + data->buflen);
9044e6bc
AV
1499 if (skb)
1500 skb_queue_tail(q, skb);
93315ed6
AG
1501 kfree(data);
1502 }
1503 }
9044e6bc
AV
1504 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1505 if (skb)
1506 skb_queue_tail(q, skb);
93315ed6
AG
1507}
1508
5adc8a6a 1509/* Log rule additions and removals */
2532386f
EP
1510static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
1511 char *action, struct audit_krule *rule,
1512 int res)
5adc8a6a
AG
1513{
1514 struct audit_buffer *ab;
1515
1a6b9f23
EP
1516 if (!audit_enabled)
1517 return;
1518
5adc8a6a
AG
1519 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1520 if (!ab)
1521 return;
2532386f 1522 audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
5adc8a6a
AG
1523 if (sid) {
1524 char *ctx = NULL;
1525 u32 len;
2a862b32 1526 if (security_secid_to_secctx(sid, &ctx, &len))
5adc8a6a 1527 audit_log_format(ab, " ssid=%u", sid);
2a862b32 1528 else {
5adc8a6a 1529 audit_log_format(ab, " subj=%s", ctx);
2a862b32
AD
1530 security_release_secctx(ctx, len);
1531 }
5adc8a6a 1532 }
a17b4ad7 1533 audit_log_format(ab, " op=%s rule key=", action);
5adc8a6a
AG
1534 if (rule->filterkey)
1535 audit_log_untrustedstring(ab, rule->filterkey);
1536 else
1537 audit_log_format(ab, "(null)");
1538 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1539 audit_log_end(ab);
1540}
1541
fe7752ba
DW
1542/**
1543 * audit_receive_filter - apply all rules to the specified message type
1544 * @type: audit message type
1545 * @pid: target pid for netlink audit messages
1546 * @uid: target uid for netlink audit messages
1547 * @seq: netlink audit message sequence (serial) number
1548 * @data: payload data
93315ed6 1549 * @datasz: size of payload data
fe7752ba 1550 * @loginuid: loginuid of sender
9f0aecdd 1551 * @sessionid: sessionid for netlink audit message
ce29b682 1552 * @sid: SE Linux Security ID of sender
fe7752ba
DW
1553 */
1554int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
2532386f 1555 size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
fe7752ba
DW
1556{
1557 struct task_struct *tsk;
9044e6bc 1558 struct audit_netlink_list *dest;
93315ed6
AG
1559 int err = 0;
1560 struct audit_entry *entry;
fe7752ba
DW
1561
1562 switch (type) {
1563 case AUDIT_LIST:
93315ed6 1564 case AUDIT_LIST_RULES:
fe7752ba
DW
1565 /* We can't just spew out the rules here because we might fill
1566 * the available socket buffer space and deadlock waiting for
1567 * auditctl to read from it... which isn't ever going to
1568 * happen if we're actually running in the context of auditctl
1569 * trying to _send_ the stuff */
9ce34218 1570
9044e6bc 1571 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
fe7752ba
DW
1572 if (!dest)
1573 return -ENOMEM;
9044e6bc
AV
1574 dest->pid = pid;
1575 skb_queue_head_init(&dest->q);
fe7752ba 1576
f368c07d 1577 mutex_lock(&audit_filter_mutex);
93315ed6 1578 if (type == AUDIT_LIST)
9044e6bc 1579 audit_list(pid, seq, &dest->q);
93315ed6 1580 else
9044e6bc 1581 audit_list_rules(pid, seq, &dest->q);
f368c07d 1582 mutex_unlock(&audit_filter_mutex);
9044e6bc
AV
1583
1584 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
fe7752ba 1585 if (IS_ERR(tsk)) {
9044e6bc 1586 skb_queue_purge(&dest->q);
fe7752ba
DW
1587 kfree(dest);
1588 err = PTR_ERR(tsk);
1589 }
1590 break;
1591 case AUDIT_ADD:
93315ed6
AG
1592 case AUDIT_ADD_RULE:
1593 if (type == AUDIT_ADD)
1594 entry = audit_rule_to_entry(data);
1595 else
1596 entry = audit_data_to_entry(data, datasz);
1597 if (IS_ERR(entry))
1598 return PTR_ERR(entry);
1599
36c4f1b1 1600 err = audit_add_rule(entry);
2532386f
EP
1601 audit_log_rule_change(loginuid, sessionid, sid, "add",
1602 &entry->rule, !err);
5d330108
SG
1603
1604 if (err)
93315ed6 1605 audit_free_rule(entry);
fe7752ba
DW
1606 break;
1607 case AUDIT_DEL:
93315ed6
AG
1608 case AUDIT_DEL_RULE:
1609 if (type == AUDIT_DEL)
1610 entry = audit_rule_to_entry(data);
1611 else
1612 entry = audit_data_to_entry(data, datasz);
1613 if (IS_ERR(entry))
1614 return PTR_ERR(entry);
1615
36c4f1b1 1616 err = audit_del_rule(entry);
2532386f
EP
1617 audit_log_rule_change(loginuid, sessionid, sid, "remove",
1618 &entry->rule, !err);
5d330108 1619
93315ed6 1620 audit_free_rule(entry);
fe7752ba
DW
1621 break;
1622 default:
1623 return -EINVAL;
1624 }
1625
1626 return err;
1627}
1628
1629int audit_comparator(const u32 left, const u32 op, const u32 right)
1630{
1631 switch (op) {
1632 case AUDIT_EQUAL:
1633 return (left == right);
1634 case AUDIT_NOT_EQUAL:
1635 return (left != right);
1636 case AUDIT_LESS_THAN:
1637 return (left < right);
1638 case AUDIT_LESS_THAN_OR_EQUAL:
1639 return (left <= right);
1640 case AUDIT_GREATER_THAN:
1641 return (left > right);
1642 case AUDIT_GREATER_THAN_OR_EQUAL:
1643 return (left >= right);
74f2345b
EP
1644 case AUDIT_BIT_MASK:
1645 return (left & right);
1646 case AUDIT_BIT_TEST:
1647 return ((left & right) == right);
fe7752ba 1648 }
d9d9ec6e
DK
1649 BUG();
1650 return 0;
fe7752ba
DW
1651}
1652
f368c07d
AG
1653/* Compare given dentry name with last component in given path,
1654 * return of 0 indicates a match. */
9c937dcc
AG
1655int audit_compare_dname_path(const char *dname, const char *path,
1656 int *dirlen)
f368c07d
AG
1657{
1658 int dlen, plen;
1659 const char *p;
1660
1661 if (!dname || !path)
1662 return 1;
1663
1664 dlen = strlen(dname);
1665 plen = strlen(path);
1666 if (plen < dlen)
1667 return 1;
1668
1669 /* disregard trailing slashes */
1670 p = path + plen - 1;
1671 while ((*p == '/') && (p > path))
1672 p--;
1673
1674 /* find last path component */
1675 p = p - dlen + 1;
1676 if (p < path)
1677 return 1;
1678 else if (p > path) {
1679 if (*--p != '/')
1680 return 1;
1681 else
1682 p++;
1683 }
fe7752ba 1684
9c937dcc
AG
1685 /* return length of path's directory component */
1686 if (dirlen)
1687 *dirlen = p - path;
f368c07d
AG
1688 return strncmp(p, dname, dlen);
1689}
fe7752ba
DW
1690
1691static int audit_filter_user_rules(struct netlink_skb_parms *cb,
93315ed6 1692 struct audit_krule *rule,
fe7752ba
DW
1693 enum audit_state *state)
1694{
1695 int i;
1696
1697 for (i = 0; i < rule->field_count; i++) {
93315ed6 1698 struct audit_field *f = &rule->fields[i];
fe7752ba
DW
1699 int result = 0;
1700
93315ed6 1701 switch (f->type) {
fe7752ba 1702 case AUDIT_PID:
93315ed6 1703 result = audit_comparator(cb->creds.pid, f->op, f->val);
fe7752ba
DW
1704 break;
1705 case AUDIT_UID:
93315ed6 1706 result = audit_comparator(cb->creds.uid, f->op, f->val);
fe7752ba
DW
1707 break;
1708 case AUDIT_GID:
93315ed6 1709 result = audit_comparator(cb->creds.gid, f->op, f->val);
fe7752ba
DW
1710 break;
1711 case AUDIT_LOGINUID:
93315ed6 1712 result = audit_comparator(cb->loginuid, f->op, f->val);
fe7752ba
DW
1713 break;
1714 }
1715
1716 if (!result)
1717 return 0;
1718 }
1719 switch (rule->action) {
1720 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
fe7752ba
DW
1721 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1722 }
1723 return 1;
1724}
1725
d8de7247 1726int audit_filter_user(struct netlink_skb_parms *cb)
fe7752ba 1727{
11f57ced 1728 enum audit_state state = AUDIT_DISABLED;
fe7752ba 1729 struct audit_entry *e;
fe7752ba
DW
1730 int ret = 1;
1731
1732 rcu_read_lock();
1733 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1734 if (audit_filter_user_rules(cb, &e->rule, &state)) {
1735 if (state == AUDIT_DISABLED)
1736 ret = 0;
1737 break;
1738 }
1739 }
1740 rcu_read_unlock();
1741
1742 return ret; /* Audit by default */
1743}
1744
1745int audit_filter_type(int type)
1746{
1747 struct audit_entry *e;
1748 int result = 0;
9ce34218 1749
fe7752ba
DW
1750 rcu_read_lock();
1751 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1752 goto unlock_and_return;
1753
1754 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1755 list) {
fe7752ba 1756 int i;
93315ed6
AG
1757 for (i = 0; i < e->rule.field_count; i++) {
1758 struct audit_field *f = &e->rule.fields[i];
1759 if (f->type == AUDIT_MSGTYPE) {
1760 result = audit_comparator(type, f->op, f->val);
fe7752ba
DW
1761 if (!result)
1762 break;
1763 }
1764 }
1765 if (result)
1766 goto unlock_and_return;
1767 }
1768unlock_and_return:
1769 rcu_read_unlock();
1770 return result;
1771}
3dc7e315 1772
e45aa212 1773static int update_lsm_rule(struct audit_krule *r)
1a9d0797 1774{
e45aa212 1775 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
1a9d0797
AV
1776 struct audit_entry *nentry;
1777 struct audit_watch *watch;
1778 struct audit_tree *tree;
1779 int err = 0;
1780
e45aa212 1781 if (!security_audit_rule_known(r))
1a9d0797
AV
1782 return 0;
1783
e45aa212
AV
1784 watch = r->watch;
1785 tree = r->tree;
1786 nentry = audit_dupe_rule(r, watch);
1a9d0797
AV
1787 if (IS_ERR(nentry)) {
1788 /* save the first error encountered for the
1789 * return value */
1790 err = PTR_ERR(nentry);
1791 audit_panic("error updating LSM filters");
1792 if (watch)
e45aa212 1793 list_del(&r->rlist);
1a9d0797 1794 list_del_rcu(&entry->list);
e45aa212 1795 list_del(&r->list);
1a9d0797
AV
1796 } else {
1797 if (watch) {
1798 list_add(&nentry->rule.rlist, &watch->rules);
e45aa212 1799 list_del(&r->rlist);
1a9d0797 1800 } else if (tree)
e45aa212 1801 list_replace_init(&r->rlist, &nentry->rule.rlist);
1a9d0797 1802 list_replace_rcu(&entry->list, &nentry->list);
e45aa212 1803 list_replace(&r->list, &nentry->rule.list);
1a9d0797
AV
1804 }
1805 call_rcu(&entry->rcu, audit_free_rule_rcu);
1806
1807 return err;
1808}
1809
04305e4a 1810/* This function will re-initialize the lsm_rule field of all applicable rules.
d7a96f3a 1811 * It will traverse the filter lists serarching for rules that contain LSM
3dc7e315 1812 * specific filter fields. When such a rule is found, it is copied, the
d7a96f3a 1813 * LSM field is re-initialized, and the old rule is replaced with the
3dc7e315 1814 * updated rule. */
d7a96f3a 1815int audit_update_lsm_rules(void)
3dc7e315 1816{
e45aa212 1817 struct audit_krule *r, *n;
3dc7e315
DG
1818 int i, err = 0;
1819
f368c07d
AG
1820 /* audit_filter_mutex synchronizes the writers */
1821 mutex_lock(&audit_filter_mutex);
3dc7e315
DG
1822
1823 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
e45aa212
AV
1824 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1825 int res = update_lsm_rule(r);
1a9d0797
AV
1826 if (!err)
1827 err = res;
3dc7e315
DG
1828 }
1829 }
f368c07d 1830 mutex_unlock(&audit_filter_mutex);
3dc7e315
DG
1831
1832 return err;
1833}
f368c07d
AG
1834
1835/* Update watch data in audit rules based on inotify events. */
1836void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
1837 u32 cookie, const char *dname, struct inode *inode)
1838{
1839 struct audit_parent *parent;
1840
1841 parent = container_of(i_watch, struct audit_parent, wdata);
1842
1843 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
1844 audit_update_watch(parent, dname, inode->i_sb->s_dev,
1845 inode->i_ino, 0);
1846 else if (mask & (IN_DELETE|IN_MOVED_FROM))
1847 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
1848 /* inotify automatically removes the watch and sends IN_IGNORED */
1849 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
1850 audit_remove_parent_watches(parent);
1851 /* inotify does not remove the watch, so remove it manually */
1852 else if(mask & IN_MOVE_SELF) {
1853 audit_remove_parent_watches(parent);
1854 inotify_remove_watch_locked(audit_ih, i_watch);
1855 } else if (mask & IN_IGNORED)
1856 put_inotify_watch(i_watch);
1857}