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/slab.h>
31 #include <linux/security.h>
32 #include <net/net_namespace.h>
40 * Synchronizes writes and blocking reads of audit's filterlist
41 * data. Rcu is used to traverse the filterlist and access
42 * contents of structs audit_entry, audit_watch and opaque
43 * LSM rules during filtering. If modified, these structures
44 * must be copied and replace their counterparts in the filterlist.
45 * An audit_parent struct is not accessed during filtering, so may
46 * be written directly provided audit_filter_mutex is held.
49 /* Audit filter lists, defined in <linux/audit.h> */
50 struct list_head audit_filter_list
[AUDIT_NR_FILTERS
] = {
51 LIST_HEAD_INIT(audit_filter_list
[0]),
52 LIST_HEAD_INIT(audit_filter_list
[1]),
53 LIST_HEAD_INIT(audit_filter_list
[2]),
54 LIST_HEAD_INIT(audit_filter_list
[3]),
55 LIST_HEAD_INIT(audit_filter_list
[4]),
56 LIST_HEAD_INIT(audit_filter_list
[5]),
57 #if AUDIT_NR_FILTERS != 6
58 #error Fix audit_filter_list initialiser
61 static struct list_head audit_rules_list
[AUDIT_NR_FILTERS
] = {
62 LIST_HEAD_INIT(audit_rules_list
[0]),
63 LIST_HEAD_INIT(audit_rules_list
[1]),
64 LIST_HEAD_INIT(audit_rules_list
[2]),
65 LIST_HEAD_INIT(audit_rules_list
[3]),
66 LIST_HEAD_INIT(audit_rules_list
[4]),
67 LIST_HEAD_INIT(audit_rules_list
[5]),
70 DEFINE_MUTEX(audit_filter_mutex
);
72 static inline void audit_free_rule(struct audit_entry
*e
)
75 struct audit_krule
*erule
= &e
->rule
;
77 /* some rules don't have associated watches */
79 audit_put_watch(erule
->watch
);
81 for (i
= 0; i
< erule
->field_count
; i
++) {
82 struct audit_field
*f
= &erule
->fields
[i
];
84 security_audit_rule_free(f
->lsm_rule
);
87 kfree(erule
->filterkey
);
91 void audit_free_rule_rcu(struct rcu_head
*head
)
93 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
97 /* Initialize an audit filterlist entry. */
98 static inline struct audit_entry
*audit_init_entry(u32 field_count
)
100 struct audit_entry
*entry
;
101 struct audit_field
*fields
;
103 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
104 if (unlikely(!entry
))
107 fields
= kzalloc(sizeof(*fields
) * field_count
, GFP_KERNEL
);
108 if (unlikely(!fields
)) {
112 entry
->rule
.fields
= fields
;
117 /* Unpack a filter field's string representation from user-space
119 char *audit_unpack_string(void **bufp
, size_t *remain
, size_t len
)
123 if (!*bufp
|| (len
== 0) || (len
> *remain
))
124 return ERR_PTR(-EINVAL
);
126 /* Of the currently implemented string fields, PATH_MAX
127 * defines the longest valid length.
130 return ERR_PTR(-ENAMETOOLONG
);
132 str
= kmalloc(len
+ 1, GFP_KERNEL
);
134 return ERR_PTR(-ENOMEM
);
136 memcpy(str
, *bufp
, len
);
144 /* Translate an inode field to kernel respresentation. */
145 static inline int audit_to_inode(struct audit_krule
*krule
,
146 struct audit_field
*f
)
148 if (krule
->listnr
!= AUDIT_FILTER_EXIT
||
149 krule
->watch
|| krule
->inode_f
|| krule
->tree
||
150 (f
->op
!= Audit_equal
&& f
->op
!= Audit_not_equal
))
157 static __u32
*classes
[AUDIT_SYSCALL_CLASSES
];
159 int __init
audit_register_class(int class, unsigned *list
)
161 __u32
*p
= kzalloc(AUDIT_BITMASK_SIZE
* sizeof(__u32
), GFP_KERNEL
);
164 while (*list
!= ~0U) {
165 unsigned n
= *list
++;
166 if (n
>= AUDIT_BITMASK_SIZE
* 32 - AUDIT_SYSCALL_CLASSES
) {
170 p
[AUDIT_WORD(n
)] |= AUDIT_BIT(n
);
172 if (class >= AUDIT_SYSCALL_CLASSES
|| classes
[class]) {
180 int audit_match_class(int class, unsigned syscall
)
182 if (unlikely(syscall
>= AUDIT_BITMASK_SIZE
* 32))
184 if (unlikely(class >= AUDIT_SYSCALL_CLASSES
|| !classes
[class]))
186 return classes
[class][AUDIT_WORD(syscall
)] & AUDIT_BIT(syscall
);
189 #ifdef CONFIG_AUDITSYSCALL
190 static inline int audit_match_class_bits(int class, u32
*mask
)
194 if (classes
[class]) {
195 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
196 if (mask
[i
] & classes
[class][i
])
202 static int audit_match_signal(struct audit_entry
*entry
)
204 struct audit_field
*arch
= entry
->rule
.arch_f
;
207 /* When arch is unspecified, we must check both masks on biarch
208 * as syscall number alone is ambiguous. */
209 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL
,
211 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32
,
215 switch(audit_classify_arch(arch
->val
)) {
217 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL
,
219 case 1: /* 32bit on biarch */
220 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32
,
228 /* Common user-space to kernel rule translation. */
229 static inline struct audit_entry
*audit_to_entry_common(struct audit_rule
*rule
)
232 struct audit_entry
*entry
;
236 listnr
= rule
->flags
& ~AUDIT_FILTER_PREPEND
;
240 #ifdef CONFIG_AUDITSYSCALL
241 case AUDIT_FILTER_ENTRY
:
242 if (rule
->action
== AUDIT_ALWAYS
)
244 case AUDIT_FILTER_EXIT
:
245 case AUDIT_FILTER_TASK
:
247 case AUDIT_FILTER_USER
:
248 case AUDIT_FILTER_TYPE
:
251 if (unlikely(rule
->action
== AUDIT_POSSIBLE
)) {
252 printk(KERN_ERR
"AUDIT_POSSIBLE is deprecated\n");
255 if (rule
->action
!= AUDIT_NEVER
&& rule
->action
!= AUDIT_ALWAYS
)
257 if (rule
->field_count
> AUDIT_MAX_FIELDS
)
261 entry
= audit_init_entry(rule
->field_count
);
265 entry
->rule
.flags
= rule
->flags
& AUDIT_FILTER_PREPEND
;
266 entry
->rule
.listnr
= listnr
;
267 entry
->rule
.action
= rule
->action
;
268 entry
->rule
.field_count
= rule
->field_count
;
270 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
271 entry
->rule
.mask
[i
] = rule
->mask
[i
];
273 for (i
= 0; i
< AUDIT_SYSCALL_CLASSES
; i
++) {
274 int bit
= AUDIT_BITMASK_SIZE
* 32 - i
- 1;
275 __u32
*p
= &entry
->rule
.mask
[AUDIT_WORD(bit
)];
278 if (!(*p
& AUDIT_BIT(bit
)))
280 *p
&= ~AUDIT_BIT(bit
);
284 for (j
= 0; j
< AUDIT_BITMASK_SIZE
; j
++)
285 entry
->rule
.mask
[j
] |= class[j
];
295 static u32 audit_ops
[] =
297 [Audit_equal
] = AUDIT_EQUAL
,
298 [Audit_not_equal
] = AUDIT_NOT_EQUAL
,
299 [Audit_bitmask
] = AUDIT_BIT_MASK
,
300 [Audit_bittest
] = AUDIT_BIT_TEST
,
301 [Audit_lt
] = AUDIT_LESS_THAN
,
302 [Audit_gt
] = AUDIT_GREATER_THAN
,
303 [Audit_le
] = AUDIT_LESS_THAN_OR_EQUAL
,
304 [Audit_ge
] = AUDIT_GREATER_THAN_OR_EQUAL
,
307 static u32
audit_to_op(u32 op
)
310 for (n
= Audit_equal
; n
< Audit_bad
&& audit_ops
[n
] != op
; n
++)
315 /* check if an audit field is valid */
316 static int audit_field_valid(struct audit_entry
*entry
, struct audit_field
*f
)
320 if (entry
->rule
.listnr
!= AUDIT_FILTER_TYPE
&&
321 entry
->rule
.listnr
!= AUDIT_FILTER_USER
)
349 /* bit ops are only useful on syscall args */
350 if (f
->op
== Audit_bitmask
|| f
->op
== Audit_bittest
)
357 case AUDIT_SUBJ_USER
:
358 case AUDIT_SUBJ_ROLE
:
359 case AUDIT_SUBJ_TYPE
:
365 case AUDIT_OBJ_LEV_LOW
:
366 case AUDIT_OBJ_LEV_HIGH
:
369 case AUDIT_FILTERKEY
:
371 case AUDIT_LOGINUID_SET
:
372 if ((f
->val
!= 0) && (f
->val
!= 1))
376 if (f
->op
!= Audit_not_equal
&& f
->op
!= Audit_equal
)
384 if (f
->val
& ~S_IFMT
)
387 case AUDIT_FIELD_COMPARE
:
388 if (f
->val
> AUDIT_MAX_FIELD_COMPARE
)
395 /* Translate struct audit_rule_data to kernel's rule respresentation. */
396 static struct audit_entry
*audit_data_to_entry(struct audit_rule_data
*data
,
400 struct audit_entry
*entry
;
402 size_t remain
= datasz
- sizeof(struct audit_rule_data
);
406 entry
= audit_to_entry_common((struct audit_rule
*)data
);
411 entry
->rule
.vers_ops
= 2;
412 for (i
= 0; i
< data
->field_count
; i
++) {
413 struct audit_field
*f
= &entry
->rule
.fields
[i
];
417 f
->op
= audit_to_op(data
->fieldflags
[i
]);
418 if (f
->op
== Audit_bad
)
421 f
->type
= data
->fields
[i
];
422 f
->val
= data
->values
[i
];
423 f
->uid
= INVALID_UID
;
424 f
->gid
= INVALID_GID
;
428 /* Support legacy tests for a valid loginuid */
429 if ((f
->type
== AUDIT_LOGINUID
) && (f
->val
== AUDIT_UID_UNSET
)) {
430 f
->type
= AUDIT_LOGINUID_SET
;
434 err
= audit_field_valid(entry
, f
);
446 f
->uid
= make_kuid(current_user_ns(), f
->val
);
447 if (!uid_valid(f
->uid
))
455 f
->gid
= make_kgid(current_user_ns(), f
->val
);
456 if (!gid_valid(f
->gid
))
460 entry
->rule
.arch_f
= f
;
462 case AUDIT_SUBJ_USER
:
463 case AUDIT_SUBJ_ROLE
:
464 case AUDIT_SUBJ_TYPE
:
470 case AUDIT_OBJ_LEV_LOW
:
471 case AUDIT_OBJ_LEV_HIGH
:
472 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
475 entry
->rule
.buflen
+= f
->val
;
477 err
= security_audit_rule_init(f
->type
, f
->op
, str
,
478 (void **)&f
->lsm_rule
);
479 /* Keep currently invalid fields around in case they
480 * become valid after a policy reload. */
481 if (err
== -EINVAL
) {
482 printk(KERN_WARNING
"audit rule for LSM "
483 "\'%s\' is invalid\n", str
);
493 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
496 entry
->rule
.buflen
+= f
->val
;
498 err
= audit_to_watch(&entry
->rule
, str
, f
->val
, f
->op
);
505 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
508 entry
->rule
.buflen
+= f
->val
;
510 err
= audit_make_tree(&entry
->rule
, str
, f
->op
);
516 err
= audit_to_inode(&entry
->rule
, f
);
520 case AUDIT_FILTERKEY
:
521 if (entry
->rule
.filterkey
|| f
->val
> AUDIT_MAX_KEY_LEN
)
523 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
526 entry
->rule
.buflen
+= f
->val
;
527 entry
->rule
.filterkey
= str
;
532 if (entry
->rule
.inode_f
&& entry
->rule
.inode_f
->op
== Audit_not_equal
)
533 entry
->rule
.inode_f
= NULL
;
539 if (entry
->rule
.watch
)
540 audit_put_watch(entry
->rule
.watch
); /* matches initial get */
541 if (entry
->rule
.tree
)
542 audit_put_tree(entry
->rule
.tree
); /* that's the temporary one */
543 audit_free_rule(entry
);
547 /* Pack a filter field's string representation into data block. */
548 static inline size_t audit_pack_string(void **bufp
, const char *str
)
550 size_t len
= strlen(str
);
552 memcpy(*bufp
, str
, len
);
558 /* Translate kernel rule respresentation to struct audit_rule_data. */
559 static struct audit_rule_data
*audit_krule_to_data(struct audit_krule
*krule
)
561 struct audit_rule_data
*data
;
565 data
= kmalloc(sizeof(*data
) + krule
->buflen
, GFP_KERNEL
);
568 memset(data
, 0, sizeof(*data
));
570 data
->flags
= krule
->flags
| krule
->listnr
;
571 data
->action
= krule
->action
;
572 data
->field_count
= krule
->field_count
;
574 for (i
= 0; i
< data
->field_count
; i
++) {
575 struct audit_field
*f
= &krule
->fields
[i
];
577 data
->fields
[i
] = f
->type
;
578 data
->fieldflags
[i
] = audit_ops
[f
->op
];
580 case AUDIT_SUBJ_USER
:
581 case AUDIT_SUBJ_ROLE
:
582 case AUDIT_SUBJ_TYPE
:
588 case AUDIT_OBJ_LEV_LOW
:
589 case AUDIT_OBJ_LEV_HIGH
:
590 data
->buflen
+= data
->values
[i
] =
591 audit_pack_string(&bufp
, f
->lsm_str
);
594 data
->buflen
+= data
->values
[i
] =
595 audit_pack_string(&bufp
,
596 audit_watch_path(krule
->watch
));
599 data
->buflen
+= data
->values
[i
] =
600 audit_pack_string(&bufp
,
601 audit_tree_path(krule
->tree
));
603 case AUDIT_FILTERKEY
:
604 data
->buflen
+= data
->values
[i
] =
605 audit_pack_string(&bufp
, krule
->filterkey
);
608 data
->values
[i
] = f
->val
;
611 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) data
->mask
[i
] = krule
->mask
[i
];
616 /* Compare two rules in kernel format. Considered success if rules
618 static int audit_compare_rule(struct audit_krule
*a
, struct audit_krule
*b
)
622 if (a
->flags
!= b
->flags
||
623 a
->listnr
!= b
->listnr
||
624 a
->action
!= b
->action
||
625 a
->field_count
!= b
->field_count
)
628 for (i
= 0; i
< a
->field_count
; i
++) {
629 if (a
->fields
[i
].type
!= b
->fields
[i
].type
||
630 a
->fields
[i
].op
!= b
->fields
[i
].op
)
633 switch(a
->fields
[i
].type
) {
634 case AUDIT_SUBJ_USER
:
635 case AUDIT_SUBJ_ROLE
:
636 case AUDIT_SUBJ_TYPE
:
642 case AUDIT_OBJ_LEV_LOW
:
643 case AUDIT_OBJ_LEV_HIGH
:
644 if (strcmp(a
->fields
[i
].lsm_str
, b
->fields
[i
].lsm_str
))
648 if (strcmp(audit_watch_path(a
->watch
),
649 audit_watch_path(b
->watch
)))
653 if (strcmp(audit_tree_path(a
->tree
),
654 audit_tree_path(b
->tree
)))
657 case AUDIT_FILTERKEY
:
658 /* both filterkeys exist based on above type compare */
659 if (strcmp(a
->filterkey
, b
->filterkey
))
668 if (!uid_eq(a
->fields
[i
].uid
, b
->fields
[i
].uid
))
676 if (!gid_eq(a
->fields
[i
].gid
, b
->fields
[i
].gid
))
680 if (a
->fields
[i
].val
!= b
->fields
[i
].val
)
685 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
686 if (a
->mask
[i
] != b
->mask
[i
])
692 /* Duplicate LSM field information. The lsm_rule is opaque, so must be
694 static inline int audit_dupe_lsm_field(struct audit_field
*df
,
695 struct audit_field
*sf
)
700 /* our own copy of lsm_str */
701 lsm_str
= kstrdup(sf
->lsm_str
, GFP_KERNEL
);
702 if (unlikely(!lsm_str
))
704 df
->lsm_str
= lsm_str
;
706 /* our own (refreshed) copy of lsm_rule */
707 ret
= security_audit_rule_init(df
->type
, df
->op
, df
->lsm_str
,
708 (void **)&df
->lsm_rule
);
709 /* Keep currently invalid fields around in case they
710 * become valid after a policy reload. */
711 if (ret
== -EINVAL
) {
712 printk(KERN_WARNING
"audit rule for LSM \'%s\' is "
713 "invalid\n", df
->lsm_str
);
720 /* Duplicate an audit rule. This will be a deep copy with the exception
721 * of the watch - that pointer is carried over. The LSM specific fields
722 * will be updated in the copy. The point is to be able to replace the old
723 * rule with the new rule in the filterlist, then free the old rule.
724 * The rlist element is undefined; list manipulations are handled apart from
725 * the initial copy. */
726 struct audit_entry
*audit_dupe_rule(struct audit_krule
*old
)
728 u32 fcount
= old
->field_count
;
729 struct audit_entry
*entry
;
730 struct audit_krule
*new;
734 entry
= audit_init_entry(fcount
);
735 if (unlikely(!entry
))
736 return ERR_PTR(-ENOMEM
);
739 new->vers_ops
= old
->vers_ops
;
740 new->flags
= old
->flags
;
741 new->listnr
= old
->listnr
;
742 new->action
= old
->action
;
743 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
744 new->mask
[i
] = old
->mask
[i
];
745 new->prio
= old
->prio
;
746 new->buflen
= old
->buflen
;
747 new->inode_f
= old
->inode_f
;
748 new->field_count
= old
->field_count
;
751 * note that we are OK with not refcounting here; audit_match_tree()
752 * never dereferences tree and we can't get false positives there
753 * since we'd have to have rule gone from the list *and* removed
754 * before the chunks found by lookup had been allocated, i.e. before
755 * the beginning of list scan.
757 new->tree
= old
->tree
;
758 memcpy(new->fields
, old
->fields
, sizeof(struct audit_field
) * fcount
);
760 /* deep copy this information, updating the lsm_rule fields, because
761 * the originals will all be freed when the old rule is freed. */
762 for (i
= 0; i
< fcount
; i
++) {
763 switch (new->fields
[i
].type
) {
764 case AUDIT_SUBJ_USER
:
765 case AUDIT_SUBJ_ROLE
:
766 case AUDIT_SUBJ_TYPE
:
772 case AUDIT_OBJ_LEV_LOW
:
773 case AUDIT_OBJ_LEV_HIGH
:
774 err
= audit_dupe_lsm_field(&new->fields
[i
],
777 case AUDIT_FILTERKEY
:
778 fk
= kstrdup(old
->filterkey
, GFP_KERNEL
);
785 audit_free_rule(entry
);
791 audit_get_watch(old
->watch
);
792 new->watch
= old
->watch
;
798 /* Find an existing audit rule.
799 * Caller must hold audit_filter_mutex to prevent stale rule data. */
800 static struct audit_entry
*audit_find_rule(struct audit_entry
*entry
,
801 struct list_head
**p
)
803 struct audit_entry
*e
, *found
= NULL
;
804 struct list_head
*list
;
807 if (entry
->rule
.inode_f
) {
808 h
= audit_hash_ino(entry
->rule
.inode_f
->val
);
809 *p
= list
= &audit_inode_hash
[h
];
810 } else if (entry
->rule
.watch
) {
811 /* we don't know the inode number, so must walk entire hash */
812 for (h
= 0; h
< AUDIT_INODE_BUCKETS
; h
++) {
813 list
= &audit_inode_hash
[h
];
814 list_for_each_entry(e
, list
, list
)
815 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
822 *p
= list
= &audit_filter_list
[entry
->rule
.listnr
];
825 list_for_each_entry(e
, list
, list
)
826 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
835 static u64 prio_low
= ~0ULL/2;
836 static u64 prio_high
= ~0ULL/2 - 1;
838 /* Add rule to given filterlist if not a duplicate. */
839 static inline int audit_add_rule(struct audit_entry
*entry
)
841 struct audit_entry
*e
;
842 struct audit_watch
*watch
= entry
->rule
.watch
;
843 struct audit_tree
*tree
= entry
->rule
.tree
;
844 struct list_head
*list
;
846 #ifdef CONFIG_AUDITSYSCALL
849 /* If either of these, don't count towards total */
850 if (entry
->rule
.listnr
== AUDIT_FILTER_USER
||
851 entry
->rule
.listnr
== AUDIT_FILTER_TYPE
)
855 mutex_lock(&audit_filter_mutex
);
856 e
= audit_find_rule(entry
, &list
);
858 mutex_unlock(&audit_filter_mutex
);
860 /* normally audit_add_tree_rule() will free it on failure */
862 audit_put_tree(tree
);
867 /* audit_filter_mutex is dropped and re-taken during this call */
868 err
= audit_add_watch(&entry
->rule
, &list
);
870 mutex_unlock(&audit_filter_mutex
);
872 * normally audit_add_tree_rule() will free it
876 audit_put_tree(tree
);
881 err
= audit_add_tree_rule(&entry
->rule
);
883 mutex_unlock(&audit_filter_mutex
);
888 entry
->rule
.prio
= ~0ULL;
889 if (entry
->rule
.listnr
== AUDIT_FILTER_EXIT
) {
890 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
)
891 entry
->rule
.prio
= ++prio_high
;
893 entry
->rule
.prio
= --prio_low
;
896 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
) {
897 list_add(&entry
->rule
.list
,
898 &audit_rules_list
[entry
->rule
.listnr
]);
899 list_add_rcu(&entry
->list
, list
);
900 entry
->rule
.flags
&= ~AUDIT_FILTER_PREPEND
;
902 list_add_tail(&entry
->rule
.list
,
903 &audit_rules_list
[entry
->rule
.listnr
]);
904 list_add_tail_rcu(&entry
->list
, list
);
906 #ifdef CONFIG_AUDITSYSCALL
910 if (!audit_match_signal(entry
))
913 mutex_unlock(&audit_filter_mutex
);
919 audit_put_watch(watch
); /* tmp watch, matches initial get */
923 /* Remove an existing rule from filterlist. */
924 static inline int audit_del_rule(struct audit_entry
*entry
)
926 struct audit_entry
*e
;
927 struct audit_watch
*watch
= entry
->rule
.watch
;
928 struct audit_tree
*tree
= entry
->rule
.tree
;
929 struct list_head
*list
;
931 #ifdef CONFIG_AUDITSYSCALL
934 /* If either of these, don't count towards total */
935 if (entry
->rule
.listnr
== AUDIT_FILTER_USER
||
936 entry
->rule
.listnr
== AUDIT_FILTER_TYPE
)
940 mutex_lock(&audit_filter_mutex
);
941 e
= audit_find_rule(entry
, &list
);
943 mutex_unlock(&audit_filter_mutex
);
949 audit_remove_watch_rule(&e
->rule
);
952 audit_remove_tree_rule(&e
->rule
);
954 list_del_rcu(&e
->list
);
955 list_del(&e
->rule
.list
);
956 call_rcu(&e
->rcu
, audit_free_rule_rcu
);
958 #ifdef CONFIG_AUDITSYSCALL
962 if (!audit_match_signal(entry
))
965 mutex_unlock(&audit_filter_mutex
);
969 audit_put_watch(watch
); /* match initial get */
971 audit_put_tree(tree
); /* that's the temporary one */
976 /* List rules using struct audit_rule_data. */
977 static void audit_list_rules(__u32 portid
, int seq
, struct sk_buff_head
*q
)
980 struct audit_krule
*r
;
983 /* This is a blocking read, so use audit_filter_mutex instead of rcu
984 * iterator to sync with list writers. */
985 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
986 list_for_each_entry(r
, &audit_rules_list
[i
], list
) {
987 struct audit_rule_data
*data
;
989 data
= audit_krule_to_data(r
);
992 skb
= audit_make_reply(portid
, seq
, AUDIT_LIST_RULES
,
994 sizeof(*data
) + data
->buflen
);
996 skb_queue_tail(q
, skb
);
1000 skb
= audit_make_reply(portid
, seq
, AUDIT_LIST_RULES
, 1, 1, NULL
, 0);
1002 skb_queue_tail(q
, skb
);
1005 /* Log rule additions and removals */
1006 static void audit_log_rule_change(char *action
, struct audit_krule
*rule
, int res
)
1008 struct audit_buffer
*ab
;
1009 uid_t loginuid
= from_kuid(&init_user_ns
, audit_get_loginuid(current
));
1010 unsigned int sessionid
= audit_get_sessionid(current
);
1015 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
);
1018 audit_log_format(ab
, "auid=%u ses=%u" ,loginuid
, sessionid
);
1019 audit_log_task_context(ab
);
1020 audit_log_format(ab
, " op=");
1021 audit_log_string(ab
, action
);
1022 audit_log_key(ab
, rule
->filterkey
);
1023 audit_log_format(ab
, " list=%d res=%d", rule
->listnr
, res
);
1028 * audit_rule_change - apply all rules to the specified message type
1029 * @type: audit message type
1030 * @portid: target port id for netlink audit messages
1031 * @seq: netlink audit message sequence (serial) number
1032 * @data: payload data
1033 * @datasz: size of payload data
1035 int audit_rule_change(int type
, __u32 portid
, int seq
, void *data
,
1039 struct audit_entry
*entry
;
1042 case AUDIT_ADD_RULE
:
1043 entry
= audit_data_to_entry(data
, datasz
);
1045 return PTR_ERR(entry
);
1047 err
= audit_add_rule(entry
);
1048 audit_log_rule_change("add rule", &entry
->rule
, !err
);
1050 audit_free_rule(entry
);
1052 case AUDIT_DEL_RULE
:
1053 entry
= audit_data_to_entry(data
, datasz
);
1055 return PTR_ERR(entry
);
1057 err
= audit_del_rule(entry
);
1058 audit_log_rule_change("remove rule", &entry
->rule
, !err
);
1059 audit_free_rule(entry
);
1069 * audit_list_rules_send - list the audit rules
1070 * @request_skb: skb of request we are replying to (used to target the reply)
1071 * @seq: netlink audit message sequence (serial) number
1073 int audit_list_rules_send(struct sk_buff
*request_skb
, int seq
)
1075 u32 portid
= NETLINK_CB(request_skb
).portid
;
1076 struct net
*net
= sock_net(NETLINK_CB(request_skb
).sk
);
1077 struct task_struct
*tsk
;
1078 struct audit_netlink_list
*dest
;
1081 /* We can't just spew out the rules here because we might fill
1082 * the available socket buffer space and deadlock waiting for
1083 * auditctl to read from it... which isn't ever going to
1084 * happen if we're actually running in the context of auditctl
1085 * trying to _send_ the stuff */
1087 dest
= kmalloc(sizeof(struct audit_netlink_list
), GFP_KERNEL
);
1090 dest
->net
= get_net(net
);
1091 dest
->portid
= portid
;
1092 skb_queue_head_init(&dest
->q
);
1094 mutex_lock(&audit_filter_mutex
);
1095 audit_list_rules(portid
, seq
, &dest
->q
);
1096 mutex_unlock(&audit_filter_mutex
);
1098 tsk
= kthread_run(audit_send_list
, dest
, "audit_send_list");
1100 skb_queue_purge(&dest
->q
);
1108 int audit_comparator(u32 left
, u32 op
, u32 right
)
1112 return (left
== right
);
1113 case Audit_not_equal
:
1114 return (left
!= right
);
1116 return (left
< right
);
1118 return (left
<= right
);
1120 return (left
> right
);
1122 return (left
>= right
);
1124 return (left
& right
);
1126 return ((left
& right
) == right
);
1133 int audit_uid_comparator(kuid_t left
, u32 op
, kuid_t right
)
1137 return uid_eq(left
, right
);
1138 case Audit_not_equal
:
1139 return !uid_eq(left
, right
);
1141 return uid_lt(left
, right
);
1143 return uid_lte(left
, right
);
1145 return uid_gt(left
, right
);
1147 return uid_gte(left
, right
);
1156 int audit_gid_comparator(kgid_t left
, u32 op
, kgid_t right
)
1160 return gid_eq(left
, right
);
1161 case Audit_not_equal
:
1162 return !gid_eq(left
, right
);
1164 return gid_lt(left
, right
);
1166 return gid_lte(left
, right
);
1168 return gid_gt(left
, right
);
1170 return gid_gte(left
, right
);
1180 * parent_len - find the length of the parent portion of a pathname
1181 * @path: pathname of which to determine length
1183 int parent_len(const char *path
)
1188 plen
= strlen(path
);
1193 /* disregard trailing slashes */
1194 p
= path
+ plen
- 1;
1195 while ((*p
== '/') && (p
> path
))
1198 /* walk backward until we find the next slash or hit beginning */
1199 while ((*p
!= '/') && (p
> path
))
1202 /* did we find a slash? Then increment to include it in path */
1210 * audit_compare_dname_path - compare given dentry name with last component in
1211 * given path. Return of 0 indicates a match.
1212 * @dname: dentry name that we're comparing
1213 * @path: full pathname that we're comparing
1214 * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
1215 * here indicates that we must compute this value.
1217 int audit_compare_dname_path(const char *dname
, const char *path
, int parentlen
)
1222 dlen
= strlen(dname
);
1223 pathlen
= strlen(path
);
1227 parentlen
= parentlen
== AUDIT_NAME_FULL
? parent_len(path
) : parentlen
;
1228 if (pathlen
- parentlen
!= dlen
)
1231 p
= path
+ parentlen
;
1233 return strncmp(p
, dname
, dlen
);
1236 static int audit_filter_user_rules(struct audit_krule
*rule
, int type
,
1237 enum audit_state
*state
)
1241 for (i
= 0; i
< rule
->field_count
; i
++) {
1242 struct audit_field
*f
= &rule
->fields
[i
];
1248 result
= audit_comparator(task_pid_vnr(current
), f
->op
, f
->val
);
1251 result
= audit_uid_comparator(current_uid(), f
->op
, f
->uid
);
1254 result
= audit_gid_comparator(current_gid(), f
->op
, f
->gid
);
1256 case AUDIT_LOGINUID
:
1257 result
= audit_uid_comparator(audit_get_loginuid(current
),
1260 case AUDIT_LOGINUID_SET
:
1261 result
= audit_comparator(audit_loginuid_set(current
),
1265 result
= audit_comparator(type
, f
->op
, f
->val
);
1267 case AUDIT_SUBJ_USER
:
1268 case AUDIT_SUBJ_ROLE
:
1269 case AUDIT_SUBJ_TYPE
:
1270 case AUDIT_SUBJ_SEN
:
1271 case AUDIT_SUBJ_CLR
:
1273 security_task_getsecid(current
, &sid
);
1274 result
= security_audit_rule_match(sid
,
1286 switch (rule
->action
) {
1287 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
1288 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
1293 int audit_filter_user(int type
)
1295 enum audit_state state
= AUDIT_DISABLED
;
1296 struct audit_entry
*e
;
1299 ret
= 1; /* Audit by default */
1302 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_USER
], list
) {
1303 rc
= audit_filter_user_rules(&e
->rule
, type
, &state
);
1305 if (rc
> 0 && state
== AUDIT_DISABLED
)
1315 int audit_filter_type(int type
)
1317 struct audit_entry
*e
;
1321 if (list_empty(&audit_filter_list
[AUDIT_FILTER_TYPE
]))
1322 goto unlock_and_return
;
1324 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TYPE
],
1327 for (i
= 0; i
< e
->rule
.field_count
; i
++) {
1328 struct audit_field
*f
= &e
->rule
.fields
[i
];
1329 if (f
->type
== AUDIT_MSGTYPE
) {
1330 result
= audit_comparator(type
, f
->op
, f
->val
);
1336 goto unlock_and_return
;
1343 static int update_lsm_rule(struct audit_krule
*r
)
1345 struct audit_entry
*entry
= container_of(r
, struct audit_entry
, rule
);
1346 struct audit_entry
*nentry
;
1349 if (!security_audit_rule_known(r
))
1352 nentry
= audit_dupe_rule(r
);
1353 if (IS_ERR(nentry
)) {
1354 /* save the first error encountered for the
1356 err
= PTR_ERR(nentry
);
1357 audit_panic("error updating LSM filters");
1359 list_del(&r
->rlist
);
1360 list_del_rcu(&entry
->list
);
1363 if (r
->watch
|| r
->tree
)
1364 list_replace_init(&r
->rlist
, &nentry
->rule
.rlist
);
1365 list_replace_rcu(&entry
->list
, &nentry
->list
);
1366 list_replace(&r
->list
, &nentry
->rule
.list
);
1368 call_rcu(&entry
->rcu
, audit_free_rule_rcu
);
1373 /* This function will re-initialize the lsm_rule field of all applicable rules.
1374 * It will traverse the filter lists serarching for rules that contain LSM
1375 * specific filter fields. When such a rule is found, it is copied, the
1376 * LSM field is re-initialized, and the old rule is replaced with the
1378 int audit_update_lsm_rules(void)
1380 struct audit_krule
*r
, *n
;
1383 /* audit_filter_mutex synchronizes the writers */
1384 mutex_lock(&audit_filter_mutex
);
1386 for (i
= 0; i
< AUDIT_NR_FILTERS
; i
++) {
1387 list_for_each_entry_safe(r
, n
, &audit_rules_list
[i
], list
) {
1388 int res
= update_lsm_rule(r
);
1393 mutex_unlock(&audit_filter_mutex
);