1 /* auditfilter.c -- filtering of audit events
3 * Copyright 2003-2004 Red Hat, Inc.
4 * Copyright 2005 Hewlett-Packard Development Company, L.P.
5 * Copyright 2005 IBM Corporation
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.
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.
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
22 #include <linux/kernel.h>
23 #include <linux/audit.h>
24 #include <linux/kthread.h>
25 #include <linux/mutex.h>
27 #include <linux/namei.h>
28 #include <linux/netlink.h>
29 #include <linux/sched.h>
30 #include <linux/inotify.h>
31 #include <linux/security.h>
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
41 * LSM rules during filtering. If modified, these structures
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.
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.
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.
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 */
67 * audit_parent status flags:
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.
75 #define AUDIT_PARENT_INVALID 0x001
77 /* Audit filter lists, defined in <linux/audit.h> */
78 struct 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
90 DEFINE_MUTEX(audit_filter_mutex);
92 /* Inotify events we care about. */
93 #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
95 void audit_free_parent(struct inotify_watch *i_watch)
97 struct audit_parent *parent;
99 parent = container_of(i_watch, struct audit_parent, wdata);
100 WARN_ON(!list_empty(&parent->watches));
104 static inline void audit_get_watch(struct audit_watch *watch)
106 atomic_inc(&watch->count);
109 static void audit_put_watch(struct audit_watch *watch)
111 if (atomic_dec_and_test(&watch->count)) {
112 WARN_ON(watch->parent);
113 WARN_ON(!list_empty(&watch->rules));
119 static void audit_remove_watch(struct audit_watch *watch)
121 list_del(&watch->wlist);
122 put_inotify_watch(&watch->parent->wdata);
123 watch->parent = NULL;
124 audit_put_watch(watch); /* match initial get */
127 static inline void audit_free_rule(struct audit_entry *e)
131 /* some rules don't have associated watches */
133 audit_put_watch(e->rule.watch);
135 for (i = 0; i < e->rule.field_count; i++) {
136 struct audit_field *f = &e->rule.fields[i];
138 security_audit_rule_free(f->lsm_rule);
140 kfree(e->rule.fields);
141 kfree(e->rule.filterkey);
145 void audit_free_rule_rcu(struct rcu_head *head)
147 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
151 /* Initialize a parent watch entry. */
152 static struct audit_parent *audit_init_parent(struct nameidata *ndp)
154 struct audit_parent *parent;
157 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
158 if (unlikely(!parent))
159 return ERR_PTR(-ENOMEM);
161 INIT_LIST_HEAD(&parent->watches);
164 inotify_init_watch(&parent->wdata);
165 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
166 get_inotify_watch(&parent->wdata);
167 wd = inotify_add_watch(audit_ih, &parent->wdata,
168 ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
170 audit_free_parent(&parent->wdata);
177 /* Initialize a watch entry. */
178 static struct audit_watch *audit_init_watch(char *path)
180 struct audit_watch *watch;
182 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
183 if (unlikely(!watch))
184 return ERR_PTR(-ENOMEM);
186 INIT_LIST_HEAD(&watch->rules);
187 atomic_set(&watch->count, 1);
189 watch->dev = (dev_t)-1;
190 watch->ino = (unsigned long)-1;
195 /* Initialize an audit filterlist entry. */
196 static inline struct audit_entry *audit_init_entry(u32 field_count)
198 struct audit_entry *entry;
199 struct audit_field *fields;
201 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
202 if (unlikely(!entry))
205 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
206 if (unlikely(!fields)) {
210 entry->rule.fields = fields;
215 /* Unpack a filter field's string representation from user-space
217 char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
221 if (!*bufp || (len == 0) || (len > *remain))
222 return ERR_PTR(-EINVAL);
224 /* Of the currently implemented string fields, PATH_MAX
225 * defines the longest valid length.
228 return ERR_PTR(-ENAMETOOLONG);
230 str = kmalloc(len + 1, GFP_KERNEL);
232 return ERR_PTR(-ENOMEM);
234 memcpy(str, *bufp, len);
242 /* Translate an inode field to kernel respresentation. */
243 static inline int audit_to_inode(struct audit_krule *krule,
244 struct audit_field *f)
246 if (krule->listnr != AUDIT_FILTER_EXIT ||
247 krule->watch || krule->inode_f || krule->tree)
254 /* Translate a watch string to kernel respresentation. */
255 static int audit_to_watch(struct audit_krule *krule, char *path, int len,
258 struct audit_watch *watch;
263 if (path[0] != '/' || path[len-1] == '/' ||
264 krule->listnr != AUDIT_FILTER_EXIT ||
266 krule->inode_f || krule->watch || krule->tree)
269 watch = audit_init_watch(path);
271 return PTR_ERR(watch);
273 audit_get_watch(watch);
274 krule->watch = watch;
279 static __u32 *classes[AUDIT_SYSCALL_CLASSES];
281 int __init audit_register_class(int class, unsigned *list)
283 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
286 while (*list != ~0U) {
287 unsigned n = *list++;
288 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
292 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
294 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
302 int audit_match_class(int class, unsigned syscall)
304 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
306 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
308 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
311 #ifdef CONFIG_AUDITSYSCALL
312 static inline int audit_match_class_bits(int class, u32 *mask)
316 if (classes[class]) {
317 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
318 if (mask[i] & classes[class][i])
324 static int audit_match_signal(struct audit_entry *entry)
326 struct audit_field *arch = entry->rule.arch_f;
329 /* When arch is unspecified, we must check both masks on biarch
330 * as syscall number alone is ambiguous. */
331 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
333 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
337 switch(audit_classify_arch(arch->val)) {
339 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
341 case 1: /* 32bit on biarch */
342 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
350 /* Common user-space to kernel rule translation. */
351 static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
354 struct audit_entry *entry;
358 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
362 case AUDIT_FILTER_USER:
363 case AUDIT_FILTER_TYPE:
364 #ifdef CONFIG_AUDITSYSCALL
365 case AUDIT_FILTER_ENTRY:
366 case AUDIT_FILTER_EXIT:
367 case AUDIT_FILTER_TASK:
371 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
372 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
375 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
377 if (rule->field_count > AUDIT_MAX_FIELDS)
381 entry = audit_init_entry(rule->field_count);
385 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
386 entry->rule.listnr = listnr;
387 entry->rule.action = rule->action;
388 entry->rule.field_count = rule->field_count;
390 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
391 entry->rule.mask[i] = rule->mask[i];
393 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
394 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
395 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
398 if (!(*p & AUDIT_BIT(bit)))
400 *p &= ~AUDIT_BIT(bit);
404 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
405 entry->rule.mask[j] |= class[j];
415 /* Translate struct audit_rule to kernel's rule respresentation.
416 * Exists for backward compatibility with userspace. */
417 static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
419 struct audit_entry *entry;
420 struct audit_field *ino_f;
424 entry = audit_to_entry_common(rule);
428 for (i = 0; i < rule->field_count; i++) {
429 struct audit_field *f = &entry->rule.fields[i];
431 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
432 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
433 f->val = rule->values[i];
456 /* bit ops are only useful on syscall args */
457 if (f->op == AUDIT_BIT_MASK ||
458 f->op == AUDIT_BIT_TEST) {
468 /* arch is only allowed to be = or != */
470 if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
471 && (f->op != AUDIT_NEGATE) && (f->op)) {
475 entry->rule.arch_f = f;
482 if ((f->val & ~S_IFMT) > S_IFMT)
486 err = audit_to_inode(&entry->rule, f);
492 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
494 /* Support for legacy operators where
495 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
496 if (f->op & AUDIT_NEGATE)
497 f->op = AUDIT_NOT_EQUAL;
500 else if (f->op == AUDIT_OPERATORS) {
506 ino_f = entry->rule.inode_f;
509 case AUDIT_NOT_EQUAL:
510 entry->rule.inode_f = NULL;
523 audit_free_rule(entry);
527 /* Translate struct audit_rule_data to kernel's rule respresentation. */
528 static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
532 struct audit_entry *entry;
533 struct audit_field *ino_f;
535 size_t remain = datasz - sizeof(struct audit_rule_data);
539 entry = audit_to_entry_common((struct audit_rule *)data);
544 entry->rule.vers_ops = 2;
545 for (i = 0; i < data->field_count; i++) {
546 struct audit_field *f = &entry->rule.fields[i];
549 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
550 data->fieldflags[i] & ~AUDIT_OPERATORS)
553 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
554 f->type = data->fields[i];
555 f->val = data->values[i];
582 entry->rule.arch_f = f;
584 case AUDIT_SUBJ_USER:
585 case AUDIT_SUBJ_ROLE:
586 case AUDIT_SUBJ_TYPE:
592 case AUDIT_OBJ_LEV_LOW:
593 case AUDIT_OBJ_LEV_HIGH:
594 str = audit_unpack_string(&bufp, &remain, f->val);
597 entry->rule.buflen += f->val;
599 err = security_audit_rule_init(f->type, f->op, str,
600 (void **)&f->lsm_rule);
601 /* Keep currently invalid fields around in case they
602 * become valid after a policy reload. */
603 if (err == -EINVAL) {
604 printk(KERN_WARNING "audit rule for LSM "
605 "\'%s\' is invalid\n", str);
615 str = audit_unpack_string(&bufp, &remain, f->val);
618 entry->rule.buflen += f->val;
620 err = audit_to_watch(&entry->rule, str, f->val, f->op);
627 str = audit_unpack_string(&bufp, &remain, f->val);
630 entry->rule.buflen += f->val;
632 err = audit_make_tree(&entry->rule, str, f->op);
638 err = audit_to_inode(&entry->rule, f);
642 case AUDIT_FILTERKEY:
644 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
646 str = audit_unpack_string(&bufp, &remain, f->val);
649 entry->rule.buflen += f->val;
650 entry->rule.filterkey = str;
657 if ((f->val & ~S_IFMT) > S_IFMT)
665 ino_f = entry->rule.inode_f;
668 case AUDIT_NOT_EQUAL:
669 entry->rule.inode_f = NULL;
682 audit_free_rule(entry);
686 /* Pack a filter field's string representation into data block. */
687 static inline size_t audit_pack_string(void **bufp, const char *str)
689 size_t len = strlen(str);
691 memcpy(*bufp, str, len);
697 /* Translate kernel rule respresentation to struct audit_rule.
698 * Exists for backward compatibility with userspace. */
699 static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
701 struct audit_rule *rule;
704 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
708 rule->flags = krule->flags | krule->listnr;
709 rule->action = krule->action;
710 rule->field_count = krule->field_count;
711 for (i = 0; i < rule->field_count; i++) {
712 rule->values[i] = krule->fields[i].val;
713 rule->fields[i] = krule->fields[i].type;
715 if (krule->vers_ops == 1) {
716 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
717 rule->fields[i] |= AUDIT_NEGATE;
719 rule->fields[i] |= krule->fields[i].op;
722 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
727 /* Translate kernel rule respresentation to struct audit_rule_data. */
728 static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
730 struct audit_rule_data *data;
734 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
737 memset(data, 0, sizeof(*data));
739 data->flags = krule->flags | krule->listnr;
740 data->action = krule->action;
741 data->field_count = krule->field_count;
743 for (i = 0; i < data->field_count; i++) {
744 struct audit_field *f = &krule->fields[i];
746 data->fields[i] = f->type;
747 data->fieldflags[i] = f->op;
749 case AUDIT_SUBJ_USER:
750 case AUDIT_SUBJ_ROLE:
751 case AUDIT_SUBJ_TYPE:
757 case AUDIT_OBJ_LEV_LOW:
758 case AUDIT_OBJ_LEV_HIGH:
759 data->buflen += data->values[i] =
760 audit_pack_string(&bufp, f->lsm_str);
763 data->buflen += data->values[i] =
764 audit_pack_string(&bufp, krule->watch->path);
767 data->buflen += data->values[i] =
768 audit_pack_string(&bufp,
769 audit_tree_path(krule->tree));
771 case AUDIT_FILTERKEY:
772 data->buflen += data->values[i] =
773 audit_pack_string(&bufp, krule->filterkey);
776 data->values[i] = f->val;
779 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
784 /* Compare two rules in kernel format. Considered success if rules
786 static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
790 if (a->flags != b->flags ||
791 a->listnr != b->listnr ||
792 a->action != b->action ||
793 a->field_count != b->field_count)
796 for (i = 0; i < a->field_count; i++) {
797 if (a->fields[i].type != b->fields[i].type ||
798 a->fields[i].op != b->fields[i].op)
801 switch(a->fields[i].type) {
802 case AUDIT_SUBJ_USER:
803 case AUDIT_SUBJ_ROLE:
804 case AUDIT_SUBJ_TYPE:
810 case AUDIT_OBJ_LEV_LOW:
811 case AUDIT_OBJ_LEV_HIGH:
812 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
816 if (strcmp(a->watch->path, b->watch->path))
820 if (strcmp(audit_tree_path(a->tree),
821 audit_tree_path(b->tree)))
824 case AUDIT_FILTERKEY:
825 /* both filterkeys exist based on above type compare */
826 if (strcmp(a->filterkey, b->filterkey))
830 if (a->fields[i].val != b->fields[i].val)
835 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
836 if (a->mask[i] != b->mask[i])
842 /* Duplicate the given audit watch. The new watch's rules list is initialized
843 * to an empty list and wlist is undefined. */
844 static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
847 struct audit_watch *new;
849 path = kstrdup(old->path, GFP_KERNEL);
851 return ERR_PTR(-ENOMEM);
853 new = audit_init_watch(path);
861 get_inotify_watch(&old->parent->wdata);
862 new->parent = old->parent;
868 /* Duplicate LSM field information. The lsm_rule is opaque, so must be
870 static inline int audit_dupe_lsm_field(struct audit_field *df,
871 struct audit_field *sf)
876 /* our own copy of lsm_str */
877 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
878 if (unlikely(!lsm_str))
880 df->lsm_str = lsm_str;
882 /* our own (refreshed) copy of lsm_rule */
883 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
884 (void **)&df->lsm_rule);
885 /* Keep currently invalid fields around in case they
886 * become valid after a policy reload. */
887 if (ret == -EINVAL) {
888 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
889 "invalid\n", df->lsm_str);
896 /* Duplicate an audit rule. This will be a deep copy with the exception
897 * of the watch - that pointer is carried over. The LSM specific fields
898 * will be updated in the copy. The point is to be able to replace the old
899 * rule with the new rule in the filterlist, then free the old rule.
900 * The rlist element is undefined; list manipulations are handled apart from
901 * the initial copy. */
902 static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
903 struct audit_watch *watch)
905 u32 fcount = old->field_count;
906 struct audit_entry *entry;
907 struct audit_krule *new;
911 entry = audit_init_entry(fcount);
912 if (unlikely(!entry))
913 return ERR_PTR(-ENOMEM);
916 new->vers_ops = old->vers_ops;
917 new->flags = old->flags;
918 new->listnr = old->listnr;
919 new->action = old->action;
920 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
921 new->mask[i] = old->mask[i];
922 new->buflen = old->buflen;
923 new->inode_f = old->inode_f;
925 new->field_count = old->field_count;
927 * note that we are OK with not refcounting here; audit_match_tree()
928 * never dereferences tree and we can't get false positives there
929 * since we'd have to have rule gone from the list *and* removed
930 * before the chunks found by lookup had been allocated, i.e. before
931 * the beginning of list scan.
933 new->tree = old->tree;
934 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
936 /* deep copy this information, updating the lsm_rule fields, because
937 * the originals will all be freed when the old rule is freed. */
938 for (i = 0; i < fcount; i++) {
939 switch (new->fields[i].type) {
940 case AUDIT_SUBJ_USER:
941 case AUDIT_SUBJ_ROLE:
942 case AUDIT_SUBJ_TYPE:
948 case AUDIT_OBJ_LEV_LOW:
949 case AUDIT_OBJ_LEV_HIGH:
950 err = audit_dupe_lsm_field(&new->fields[i],
953 case AUDIT_FILTERKEY:
954 fk = kstrdup(old->filterkey, GFP_KERNEL);
961 audit_free_rule(entry);
967 audit_get_watch(watch);
974 /* Update inode info in audit rules based on filesystem event. */
975 static void audit_update_watch(struct audit_parent *parent,
976 const char *dname, dev_t dev,
977 unsigned long ino, unsigned invalidating)
979 struct audit_watch *owatch, *nwatch, *nextw;
980 struct audit_krule *r, *nextr;
981 struct audit_entry *oentry, *nentry;
983 mutex_lock(&audit_filter_mutex);
984 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
985 if (audit_compare_dname_path(dname, owatch->path, NULL))
988 /* If the update involves invalidating rules, do the inode-based
989 * filtering now, so we don't omit records. */
990 if (invalidating && current->audit_context &&
991 audit_filter_inodes(current, current->audit_context) == AUDIT_RECORD_CONTEXT)
992 audit_set_auditable(current->audit_context);
994 nwatch = audit_dupe_watch(owatch);
995 if (IS_ERR(nwatch)) {
996 mutex_unlock(&audit_filter_mutex);
997 audit_panic("error updating watch, skipping");
1003 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
1005 oentry = container_of(r, struct audit_entry, rule);
1006 list_del(&oentry->rule.rlist);
1007 list_del_rcu(&oentry->list);
1009 nentry = audit_dupe_rule(&oentry->rule, nwatch);
1011 audit_panic("error updating watch, removing");
1013 int h = audit_hash_ino((u32)ino);
1014 list_add(&nentry->rule.rlist, &nwatch->rules);
1015 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
1018 call_rcu(&oentry->rcu, audit_free_rule_rcu);
1021 if (audit_enabled) {
1022 struct audit_buffer *ab;
1023 ab = audit_log_start(NULL, GFP_KERNEL,
1024 AUDIT_CONFIG_CHANGE);
1025 audit_log_format(ab, "auid=%u ses=%u",
1026 audit_get_loginuid(current),
1027 audit_get_sessionid(current));
1028 audit_log_format(ab,
1029 " op=updated rules specifying path=");
1030 audit_log_untrustedstring(ab, owatch->path);
1031 audit_log_format(ab, " with dev=%u ino=%lu\n",
1033 audit_log_format(ab, " list=%d res=1", r->listnr);
1036 audit_remove_watch(owatch);
1037 goto add_watch_to_parent; /* event applies to a single watch */
1039 mutex_unlock(&audit_filter_mutex);
1042 add_watch_to_parent:
1043 list_add(&nwatch->wlist, &parent->watches);
1044 mutex_unlock(&audit_filter_mutex);
1048 /* Remove all watches & rules associated with a parent that is going away. */
1049 static void audit_remove_parent_watches(struct audit_parent *parent)
1051 struct audit_watch *w, *nextw;
1052 struct audit_krule *r, *nextr;
1053 struct audit_entry *e;
1055 mutex_lock(&audit_filter_mutex);
1056 parent->flags |= AUDIT_PARENT_INVALID;
1057 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
1058 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
1059 e = container_of(r, struct audit_entry, rule);
1060 if (audit_enabled) {
1061 struct audit_buffer *ab;
1062 ab = audit_log_start(NULL, GFP_KERNEL,
1063 AUDIT_CONFIG_CHANGE);
1064 audit_log_format(ab, "auid=%u ses=%u",
1065 audit_get_loginuid(current),
1066 audit_get_sessionid(current));
1067 audit_log_format(ab, " op=remove rule path=");
1068 audit_log_untrustedstring(ab, w->path);
1070 audit_log_format(ab, " key=");
1071 audit_log_untrustedstring(ab,
1074 audit_log_format(ab, " key=(null)");
1075 audit_log_format(ab, " list=%d res=1",
1079 list_del(&r->rlist);
1080 list_del_rcu(&e->list);
1081 call_rcu(&e->rcu, audit_free_rule_rcu);
1083 audit_remove_watch(w);
1085 mutex_unlock(&audit_filter_mutex);
1088 /* Unregister inotify watches for parents on in_list.
1089 * Generates an IN_IGNORED event. */
1090 static void audit_inotify_unregister(struct list_head *in_list)
1092 struct audit_parent *p, *n;
1094 list_for_each_entry_safe(p, n, in_list, ilist) {
1095 list_del(&p->ilist);
1096 inotify_rm_watch(audit_ih, &p->wdata);
1097 /* the put matching the get in audit_do_del_rule() */
1098 put_inotify_watch(&p->wdata);
1102 /* Find an existing audit rule.
1103 * Caller must hold audit_filter_mutex to prevent stale rule data. */
1104 static struct audit_entry *audit_find_rule(struct audit_entry *entry,
1105 struct list_head *list)
1107 struct audit_entry *e, *found = NULL;
1110 if (entry->rule.watch) {
1111 /* we don't know the inode number, so must walk entire hash */
1112 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
1113 list = &audit_inode_hash[h];
1114 list_for_each_entry(e, list, list)
1115 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1123 list_for_each_entry(e, list, list)
1124 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1133 /* Get path information necessary for adding watches. */
1134 static int audit_get_nd(char *path, struct nameidata **ndp,
1135 struct nameidata **ndw)
1137 struct nameidata *ndparent, *ndwatch;
1140 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
1141 if (unlikely(!ndparent))
1144 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
1145 if (unlikely(!ndwatch)) {
1150 err = path_lookup(path, LOOKUP_PARENT, ndparent);
1157 err = path_lookup(path, 0, ndwatch);
1169 /* Release resources used for watch path information. */
1170 static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
1173 path_put(&ndp->path);
1177 path_put(&ndw->path);
1182 /* Associate the given rule with an existing parent inotify_watch.
1183 * Caller must hold audit_filter_mutex. */
1184 static void audit_add_to_parent(struct audit_krule *krule,
1185 struct audit_parent *parent)
1187 struct audit_watch *w, *watch = krule->watch;
1188 int watch_found = 0;
1190 list_for_each_entry(w, &parent->watches, wlist) {
1191 if (strcmp(watch->path, w->path))
1196 /* put krule's and initial refs to temporary watch */
1197 audit_put_watch(watch);
1198 audit_put_watch(watch);
1201 krule->watch = watch = w;
1206 get_inotify_watch(&parent->wdata);
1207 watch->parent = parent;
1209 list_add(&watch->wlist, &parent->watches);
1211 list_add(&krule->rlist, &watch->rules);
1214 /* Find a matching watch entry, or add this one.
1215 * Caller must hold audit_filter_mutex. */
1216 static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
1217 struct nameidata *ndw)
1219 struct audit_watch *watch = krule->watch;
1220 struct inotify_watch *i_watch;
1221 struct audit_parent *parent;
1224 /* update watch filter fields */
1226 watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
1227 watch->ino = ndw->path.dentry->d_inode->i_ino;
1230 /* The audit_filter_mutex must not be held during inotify calls because
1231 * we hold it during inotify event callback processing. If an existing
1232 * inotify watch is found, inotify_find_watch() grabs a reference before
1235 mutex_unlock(&audit_filter_mutex);
1237 if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
1239 parent = audit_init_parent(ndp);
1240 if (IS_ERR(parent)) {
1241 /* caller expects mutex locked */
1242 mutex_lock(&audit_filter_mutex);
1243 return PTR_ERR(parent);
1246 parent = container_of(i_watch, struct audit_parent, wdata);
1248 mutex_lock(&audit_filter_mutex);
1250 /* parent was moved before we took audit_filter_mutex */
1251 if (parent->flags & AUDIT_PARENT_INVALID)
1254 audit_add_to_parent(krule, parent);
1256 /* match get in audit_init_parent or inotify_find_watch */
1257 put_inotify_watch(&parent->wdata);
1261 /* Add rule to given filterlist if not a duplicate. */
1262 static inline int audit_add_rule(struct audit_entry *entry,
1263 struct list_head *list)
1265 struct audit_entry *e;
1266 struct audit_field *inode_f = entry->rule.inode_f;
1267 struct audit_watch *watch = entry->rule.watch;
1268 struct audit_tree *tree = entry->rule.tree;
1269 struct nameidata *ndp = NULL, *ndw = NULL;
1271 #ifdef CONFIG_AUDITSYSCALL
1274 /* If either of these, don't count towards total */
1275 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1276 entry->rule.listnr == AUDIT_FILTER_TYPE)
1281 h = audit_hash_ino(inode_f->val);
1282 list = &audit_inode_hash[h];
1285 mutex_lock(&audit_filter_mutex);
1286 e = audit_find_rule(entry, list);
1287 mutex_unlock(&audit_filter_mutex);
1290 /* normally audit_add_tree_rule() will free it on failure */
1292 audit_put_tree(tree);
1296 /* Avoid calling path_lookup under audit_filter_mutex. */
1298 err = audit_get_nd(watch->path, &ndp, &ndw);
1303 mutex_lock(&audit_filter_mutex);
1305 /* audit_filter_mutex is dropped and re-taken during this call */
1306 err = audit_add_watch(&entry->rule, ndp, ndw);
1308 mutex_unlock(&audit_filter_mutex);
1311 h = audit_hash_ino((u32)watch->ino);
1312 list = &audit_inode_hash[h];
1315 err = audit_add_tree_rule(&entry->rule);
1317 mutex_unlock(&audit_filter_mutex);
1322 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
1323 list_add_rcu(&entry->list, list);
1324 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
1326 list_add_tail_rcu(&entry->list, list);
1328 #ifdef CONFIG_AUDITSYSCALL
1332 if (!audit_match_signal(entry))
1335 mutex_unlock(&audit_filter_mutex);
1337 audit_put_nd(ndp, ndw); /* NULL args OK */
1341 audit_put_nd(ndp, ndw); /* NULL args OK */
1343 audit_put_watch(watch); /* tmp watch, matches initial get */
1347 /* Remove an existing rule from filterlist. */
1348 static inline int audit_del_rule(struct audit_entry *entry,
1349 struct list_head *list)
1351 struct audit_entry *e;
1352 struct audit_field *inode_f = entry->rule.inode_f;
1353 struct audit_watch *watch, *tmp_watch = entry->rule.watch;
1354 struct audit_tree *tree = entry->rule.tree;
1355 LIST_HEAD(inotify_list);
1357 #ifdef CONFIG_AUDITSYSCALL
1360 /* If either of these, don't count towards total */
1361 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1362 entry->rule.listnr == AUDIT_FILTER_TYPE)
1367 h = audit_hash_ino(inode_f->val);
1368 list = &audit_inode_hash[h];
1371 mutex_lock(&audit_filter_mutex);
1372 e = audit_find_rule(entry, list);
1374 mutex_unlock(&audit_filter_mutex);
1379 watch = e->rule.watch;
1381 struct audit_parent *parent = watch->parent;
1383 list_del(&e->rule.rlist);
1385 if (list_empty(&watch->rules)) {
1386 audit_remove_watch(watch);
1388 if (list_empty(&parent->watches)) {
1389 /* Put parent on the inotify un-registration
1390 * list. Grab a reference before releasing
1391 * audit_filter_mutex, to be released in
1392 * audit_inotify_unregister(). */
1393 list_add(&parent->ilist, &inotify_list);
1394 get_inotify_watch(&parent->wdata);
1400 audit_remove_tree_rule(&e->rule);
1402 list_del_rcu(&e->list);
1403 call_rcu(&e->rcu, audit_free_rule_rcu);
1405 #ifdef CONFIG_AUDITSYSCALL
1409 if (!audit_match_signal(entry))
1412 mutex_unlock(&audit_filter_mutex);
1414 if (!list_empty(&inotify_list))
1415 audit_inotify_unregister(&inotify_list);
1419 audit_put_watch(tmp_watch); /* match initial get */
1421 audit_put_tree(tree); /* that's the temporary one */
1426 /* List rules using struct audit_rule. Exists for backward
1427 * compatibility with userspace. */
1428 static void audit_list(int pid, int seq, struct sk_buff_head *q)
1430 struct sk_buff *skb;
1431 struct audit_entry *entry;
1434 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1435 * iterator to sync with list writers. */
1436 for (i=0; i<AUDIT_NR_FILTERS; i++) {
1437 list_for_each_entry(entry, &audit_filter_list[i], list) {
1438 struct audit_rule *rule;
1440 rule = audit_krule_to_rule(&entry->rule);
1441 if (unlikely(!rule))
1443 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1444 rule, sizeof(*rule));
1446 skb_queue_tail(q, skb);
1450 for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
1451 list_for_each_entry(entry, &audit_inode_hash[i], list) {
1452 struct audit_rule *rule;
1454 rule = audit_krule_to_rule(&entry->rule);
1455 if (unlikely(!rule))
1457 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1458 rule, sizeof(*rule));
1460 skb_queue_tail(q, skb);
1464 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
1466 skb_queue_tail(q, skb);
1469 /* List rules using struct audit_rule_data. */
1470 static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
1472 struct sk_buff *skb;
1473 struct audit_entry *e;
1476 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1477 * iterator to sync with list writers. */
1478 for (i=0; i<AUDIT_NR_FILTERS; i++) {
1479 list_for_each_entry(e, &audit_filter_list[i], list) {
1480 struct audit_rule_data *data;
1482 data = audit_krule_to_data(&e->rule);
1483 if (unlikely(!data))
1485 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1486 data, sizeof(*data) + data->buflen);
1488 skb_queue_tail(q, skb);
1492 for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
1493 list_for_each_entry(e, &audit_inode_hash[i], list) {
1494 struct audit_rule_data *data;
1496 data = audit_krule_to_data(&e->rule);
1497 if (unlikely(!data))
1499 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1500 data, sizeof(*data) + data->buflen);
1502 skb_queue_tail(q, skb);
1506 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1508 skb_queue_tail(q, skb);
1511 /* Log rule additions and removals */
1512 static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
1513 char *action, struct audit_krule *rule,
1516 struct audit_buffer *ab;
1521 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1524 audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
1528 if (security_secid_to_secctx(sid, &ctx, &len))
1529 audit_log_format(ab, " ssid=%u", sid);
1531 audit_log_format(ab, " subj=%s", ctx);
1532 security_release_secctx(ctx, len);
1535 audit_log_format(ab, " op=%s rule key=", action);
1536 if (rule->filterkey)
1537 audit_log_untrustedstring(ab, rule->filterkey);
1539 audit_log_format(ab, "(null)");
1540 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1545 * audit_receive_filter - apply all rules to the specified message type
1546 * @type: audit message type
1547 * @pid: target pid for netlink audit messages
1548 * @uid: target uid for netlink audit messages
1549 * @seq: netlink audit message sequence (serial) number
1550 * @data: payload data
1551 * @datasz: size of payload data
1552 * @loginuid: loginuid of sender
1553 * @sessionid: sessionid for netlink audit message
1554 * @sid: SE Linux Security ID of sender
1556 int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
1557 size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
1559 struct task_struct *tsk;
1560 struct audit_netlink_list *dest;
1562 struct audit_entry *entry;
1566 case AUDIT_LIST_RULES:
1567 /* We can't just spew out the rules here because we might fill
1568 * the available socket buffer space and deadlock waiting for
1569 * auditctl to read from it... which isn't ever going to
1570 * happen if we're actually running in the context of auditctl
1571 * trying to _send_ the stuff */
1573 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
1577 skb_queue_head_init(&dest->q);
1579 mutex_lock(&audit_filter_mutex);
1580 if (type == AUDIT_LIST)
1581 audit_list(pid, seq, &dest->q);
1583 audit_list_rules(pid, seq, &dest->q);
1584 mutex_unlock(&audit_filter_mutex);
1586 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
1588 skb_queue_purge(&dest->q);
1594 case AUDIT_ADD_RULE:
1595 if (type == AUDIT_ADD)
1596 entry = audit_rule_to_entry(data);
1598 entry = audit_data_to_entry(data, datasz);
1600 return PTR_ERR(entry);
1602 err = audit_add_rule(entry,
1603 &audit_filter_list[entry->rule.listnr]);
1604 audit_log_rule_change(loginuid, sessionid, sid, "add",
1605 &entry->rule, !err);
1608 audit_free_rule(entry);
1611 case AUDIT_DEL_RULE:
1612 if (type == AUDIT_DEL)
1613 entry = audit_rule_to_entry(data);
1615 entry = audit_data_to_entry(data, datasz);
1617 return PTR_ERR(entry);
1619 err = audit_del_rule(entry,
1620 &audit_filter_list[entry->rule.listnr]);
1621 audit_log_rule_change(loginuid, sessionid, sid, "remove",
1622 &entry->rule, !err);
1624 audit_free_rule(entry);
1633 int audit_comparator(const u32 left, const u32 op, const u32 right)
1637 return (left == right);
1638 case AUDIT_NOT_EQUAL:
1639 return (left != right);
1640 case AUDIT_LESS_THAN:
1641 return (left < right);
1642 case AUDIT_LESS_THAN_OR_EQUAL:
1643 return (left <= right);
1644 case AUDIT_GREATER_THAN:
1645 return (left > right);
1646 case AUDIT_GREATER_THAN_OR_EQUAL:
1647 return (left >= right);
1648 case AUDIT_BIT_MASK:
1649 return (left & right);
1650 case AUDIT_BIT_TEST:
1651 return ((left & right) == right);
1657 /* Compare given dentry name with last component in given path,
1658 * return of 0 indicates a match. */
1659 int audit_compare_dname_path(const char *dname, const char *path,
1665 if (!dname || !path)
1668 dlen = strlen(dname);
1669 plen = strlen(path);
1673 /* disregard trailing slashes */
1674 p = path + plen - 1;
1675 while ((*p == '/') && (p > path))
1678 /* find last path component */
1682 else if (p > path) {
1689 /* return length of path's directory component */
1692 return strncmp(p, dname, dlen);
1695 static int audit_filter_user_rules(struct netlink_skb_parms *cb,
1696 struct audit_krule *rule,
1697 enum audit_state *state)
1701 for (i = 0; i < rule->field_count; i++) {
1702 struct audit_field *f = &rule->fields[i];
1707 result = audit_comparator(cb->creds.pid, f->op, f->val);
1710 result = audit_comparator(cb->creds.uid, f->op, f->val);
1713 result = audit_comparator(cb->creds.gid, f->op, f->val);
1715 case AUDIT_LOGINUID:
1716 result = audit_comparator(cb->loginuid, f->op, f->val);
1723 switch (rule->action) {
1724 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
1725 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1730 int audit_filter_user(struct netlink_skb_parms *cb)
1732 enum audit_state state = AUDIT_DISABLED;
1733 struct audit_entry *e;
1737 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1738 if (audit_filter_user_rules(cb, &e->rule, &state)) {
1739 if (state == AUDIT_DISABLED)
1746 return ret; /* Audit by default */
1749 int audit_filter_type(int type)
1751 struct audit_entry *e;
1755 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1756 goto unlock_and_return;
1758 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1761 for (i = 0; i < e->rule.field_count; i++) {
1762 struct audit_field *f = &e->rule.fields[i];
1763 if (f->type == AUDIT_MSGTYPE) {
1764 result = audit_comparator(type, f->op, f->val);
1770 goto unlock_and_return;
1777 /* This function will re-initialize the lsm_rule field of all applicable rules.
1778 * It will traverse the filter lists serarching for rules that contain LSM
1779 * specific filter fields. When such a rule is found, it is copied, the
1780 * LSM field is re-initialized, and the old rule is replaced with the
1782 int audit_update_lsm_rules(void)
1784 struct audit_entry *entry, *n, *nentry;
1785 struct audit_watch *watch;
1786 struct audit_tree *tree;
1789 /* audit_filter_mutex synchronizes the writers */
1790 mutex_lock(&audit_filter_mutex);
1792 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
1793 list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
1794 if (!security_audit_rule_known(&entry->rule))
1797 watch = entry->rule.watch;
1798 tree = entry->rule.tree;
1799 nentry = audit_dupe_rule(&entry->rule, watch);
1800 if (IS_ERR(nentry)) {
1801 /* save the first error encountered for the
1804 err = PTR_ERR(nentry);
1805 audit_panic("error updating LSM filters");
1807 list_del(&entry->rule.rlist);
1808 list_del_rcu(&entry->list);
1811 list_add(&nentry->rule.rlist,
1813 list_del(&entry->rule.rlist);
1815 list_replace_init(&entry->rule.rlist,
1816 &nentry->rule.rlist);
1817 list_replace_rcu(&entry->list, &nentry->list);
1819 call_rcu(&entry->rcu, audit_free_rule_rcu);
1823 mutex_unlock(&audit_filter_mutex);
1828 /* Update watch data in audit rules based on inotify events. */
1829 void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
1830 u32 cookie, const char *dname, struct inode *inode)
1832 struct audit_parent *parent;
1834 parent = container_of(i_watch, struct audit_parent, wdata);
1836 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
1837 audit_update_watch(parent, dname, inode->i_sb->s_dev,
1839 else if (mask & (IN_DELETE|IN_MOVED_FROM))
1840 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
1841 /* inotify automatically removes the watch and sends IN_IGNORED */
1842 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
1843 audit_remove_parent_watches(parent);
1844 /* inotify does not remove the watch, so remove it manually */
1845 else if(mask & IN_MOVE_SELF) {
1846 audit_remove_parent_watches(parent);
1847 inotify_remove_watch_locked(audit_ih, i_watch);
1848 } else if (mask & IN_IGNORED)
1849 put_inotify_watch(i_watch);