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
;
432 entry
->rule
.pflags
|= AUDIT_LOGINUID_LEGACY
;
435 err
= audit_field_valid(entry
, f
);
447 f
->uid
= make_kuid(current_user_ns(), f
->val
);
448 if (!uid_valid(f
->uid
))
456 f
->gid
= make_kgid(current_user_ns(), f
->val
);
457 if (!gid_valid(f
->gid
))
461 entry
->rule
.arch_f
= f
;
463 case AUDIT_SUBJ_USER
:
464 case AUDIT_SUBJ_ROLE
:
465 case AUDIT_SUBJ_TYPE
:
471 case AUDIT_OBJ_LEV_LOW
:
472 case AUDIT_OBJ_LEV_HIGH
:
473 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
476 entry
->rule
.buflen
+= f
->val
;
478 err
= security_audit_rule_init(f
->type
, f
->op
, str
,
479 (void **)&f
->lsm_rule
);
480 /* Keep currently invalid fields around in case they
481 * become valid after a policy reload. */
482 if (err
== -EINVAL
) {
483 printk(KERN_WARNING
"audit rule for LSM "
484 "\'%s\' is invalid\n", str
);
494 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
497 entry
->rule
.buflen
+= f
->val
;
499 err
= audit_to_watch(&entry
->rule
, str
, f
->val
, f
->op
);
506 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
509 entry
->rule
.buflen
+= f
->val
;
511 err
= audit_make_tree(&entry
->rule
, str
, f
->op
);
517 err
= audit_to_inode(&entry
->rule
, f
);
521 case AUDIT_FILTERKEY
:
522 if (entry
->rule
.filterkey
|| f
->val
> AUDIT_MAX_KEY_LEN
)
524 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
527 entry
->rule
.buflen
+= f
->val
;
528 entry
->rule
.filterkey
= str
;
533 if (entry
->rule
.inode_f
&& entry
->rule
.inode_f
->op
== Audit_not_equal
)
534 entry
->rule
.inode_f
= NULL
;
540 if (entry
->rule
.watch
)
541 audit_put_watch(entry
->rule
.watch
); /* matches initial get */
542 if (entry
->rule
.tree
)
543 audit_put_tree(entry
->rule
.tree
); /* that's the temporary one */
544 audit_free_rule(entry
);
548 /* Pack a filter field's string representation into data block. */
549 static inline size_t audit_pack_string(void **bufp
, const char *str
)
551 size_t len
= strlen(str
);
553 memcpy(*bufp
, str
, len
);
559 /* Translate kernel rule respresentation to struct audit_rule_data. */
560 static struct audit_rule_data
*audit_krule_to_data(struct audit_krule
*krule
)
562 struct audit_rule_data
*data
;
566 data
= kmalloc(sizeof(*data
) + krule
->buflen
, GFP_KERNEL
);
569 memset(data
, 0, sizeof(*data
));
571 data
->flags
= krule
->flags
| krule
->listnr
;
572 data
->action
= krule
->action
;
573 data
->field_count
= krule
->field_count
;
575 for (i
= 0; i
< data
->field_count
; i
++) {
576 struct audit_field
*f
= &krule
->fields
[i
];
578 data
->fields
[i
] = f
->type
;
579 data
->fieldflags
[i
] = audit_ops
[f
->op
];
581 case AUDIT_SUBJ_USER
:
582 case AUDIT_SUBJ_ROLE
:
583 case AUDIT_SUBJ_TYPE
:
589 case AUDIT_OBJ_LEV_LOW
:
590 case AUDIT_OBJ_LEV_HIGH
:
591 data
->buflen
+= data
->values
[i
] =
592 audit_pack_string(&bufp
, f
->lsm_str
);
595 data
->buflen
+= data
->values
[i
] =
596 audit_pack_string(&bufp
,
597 audit_watch_path(krule
->watch
));
600 data
->buflen
+= data
->values
[i
] =
601 audit_pack_string(&bufp
,
602 audit_tree_path(krule
->tree
));
604 case AUDIT_FILTERKEY
:
605 data
->buflen
+= data
->values
[i
] =
606 audit_pack_string(&bufp
, krule
->filterkey
);
608 case AUDIT_LOGINUID_SET
:
609 if (krule
->pflags
& AUDIT_LOGINUID_LEGACY
&& !f
->val
) {
610 data
->fields
[i
] = AUDIT_LOGINUID
;
611 data
->values
[i
] = AUDIT_UID_UNSET
;
614 /* fallthrough if set */
616 data
->values
[i
] = f
->val
;
619 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) data
->mask
[i
] = krule
->mask
[i
];
624 /* Compare two rules in kernel format. Considered success if rules
626 static int audit_compare_rule(struct audit_krule
*a
, struct audit_krule
*b
)
630 if (a
->flags
!= b
->flags
||
631 a
->pflags
!= b
->pflags
||
632 a
->listnr
!= b
->listnr
||
633 a
->action
!= b
->action
||
634 a
->field_count
!= b
->field_count
)
637 for (i
= 0; i
< a
->field_count
; i
++) {
638 if (a
->fields
[i
].type
!= b
->fields
[i
].type
||
639 a
->fields
[i
].op
!= b
->fields
[i
].op
)
642 switch(a
->fields
[i
].type
) {
643 case AUDIT_SUBJ_USER
:
644 case AUDIT_SUBJ_ROLE
:
645 case AUDIT_SUBJ_TYPE
:
651 case AUDIT_OBJ_LEV_LOW
:
652 case AUDIT_OBJ_LEV_HIGH
:
653 if (strcmp(a
->fields
[i
].lsm_str
, b
->fields
[i
].lsm_str
))
657 if (strcmp(audit_watch_path(a
->watch
),
658 audit_watch_path(b
->watch
)))
662 if (strcmp(audit_tree_path(a
->tree
),
663 audit_tree_path(b
->tree
)))
666 case AUDIT_FILTERKEY
:
667 /* both filterkeys exist based on above type compare */
668 if (strcmp(a
->filterkey
, b
->filterkey
))
677 if (!uid_eq(a
->fields
[i
].uid
, b
->fields
[i
].uid
))
685 if (!gid_eq(a
->fields
[i
].gid
, b
->fields
[i
].gid
))
689 if (a
->fields
[i
].val
!= b
->fields
[i
].val
)
694 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
695 if (a
->mask
[i
] != b
->mask
[i
])
701 /* Duplicate LSM field information. The lsm_rule is opaque, so must be
703 static inline int audit_dupe_lsm_field(struct audit_field
*df
,
704 struct audit_field
*sf
)
709 /* our own copy of lsm_str */
710 lsm_str
= kstrdup(sf
->lsm_str
, GFP_KERNEL
);
711 if (unlikely(!lsm_str
))
713 df
->lsm_str
= lsm_str
;
715 /* our own (refreshed) copy of lsm_rule */
716 ret
= security_audit_rule_init(df
->type
, df
->op
, df
->lsm_str
,
717 (void **)&df
->lsm_rule
);
718 /* Keep currently invalid fields around in case they
719 * become valid after a policy reload. */
720 if (ret
== -EINVAL
) {
721 printk(KERN_WARNING
"audit rule for LSM \'%s\' is "
722 "invalid\n", df
->lsm_str
);
729 /* Duplicate an audit rule. This will be a deep copy with the exception
730 * of the watch - that pointer is carried over. The LSM specific fields
731 * will be updated in the copy. The point is to be able to replace the old
732 * rule with the new rule in the filterlist, then free the old rule.
733 * The rlist element is undefined; list manipulations are handled apart from
734 * the initial copy. */
735 struct audit_entry
*audit_dupe_rule(struct audit_krule
*old
)
737 u32 fcount
= old
->field_count
;
738 struct audit_entry
*entry
;
739 struct audit_krule
*new;
743 entry
= audit_init_entry(fcount
);
744 if (unlikely(!entry
))
745 return ERR_PTR(-ENOMEM
);
748 new->vers_ops
= old
->vers_ops
;
749 new->flags
= old
->flags
;
750 new->pflags
= old
->pflags
;
751 new->listnr
= old
->listnr
;
752 new->action
= old
->action
;
753 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
754 new->mask
[i
] = old
->mask
[i
];
755 new->prio
= old
->prio
;
756 new->buflen
= old
->buflen
;
757 new->inode_f
= old
->inode_f
;
758 new->field_count
= old
->field_count
;
761 * note that we are OK with not refcounting here; audit_match_tree()
762 * never dereferences tree and we can't get false positives there
763 * since we'd have to have rule gone from the list *and* removed
764 * before the chunks found by lookup had been allocated, i.e. before
765 * the beginning of list scan.
767 new->tree
= old
->tree
;
768 memcpy(new->fields
, old
->fields
, sizeof(struct audit_field
) * fcount
);
770 /* deep copy this information, updating the lsm_rule fields, because
771 * the originals will all be freed when the old rule is freed. */
772 for (i
= 0; i
< fcount
; i
++) {
773 switch (new->fields
[i
].type
) {
774 case AUDIT_SUBJ_USER
:
775 case AUDIT_SUBJ_ROLE
:
776 case AUDIT_SUBJ_TYPE
:
782 case AUDIT_OBJ_LEV_LOW
:
783 case AUDIT_OBJ_LEV_HIGH
:
784 err
= audit_dupe_lsm_field(&new->fields
[i
],
787 case AUDIT_FILTERKEY
:
788 fk
= kstrdup(old
->filterkey
, GFP_KERNEL
);
795 audit_free_rule(entry
);
801 audit_get_watch(old
->watch
);
802 new->watch
= old
->watch
;
808 /* Find an existing audit rule.
809 * Caller must hold audit_filter_mutex to prevent stale rule data. */
810 static struct audit_entry
*audit_find_rule(struct audit_entry
*entry
,
811 struct list_head
**p
)
813 struct audit_entry
*e
, *found
= NULL
;
814 struct list_head
*list
;
817 if (entry
->rule
.inode_f
) {
818 h
= audit_hash_ino(entry
->rule
.inode_f
->val
);
819 *p
= list
= &audit_inode_hash
[h
];
820 } else if (entry
->rule
.watch
) {
821 /* we don't know the inode number, so must walk entire hash */
822 for (h
= 0; h
< AUDIT_INODE_BUCKETS
; h
++) {
823 list
= &audit_inode_hash
[h
];
824 list_for_each_entry(e
, list
, list
)
825 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
832 *p
= list
= &audit_filter_list
[entry
->rule
.listnr
];
835 list_for_each_entry(e
, list
, list
)
836 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
845 static u64 prio_low
= ~0ULL/2;
846 static u64 prio_high
= ~0ULL/2 - 1;
848 /* Add rule to given filterlist if not a duplicate. */
849 static inline int audit_add_rule(struct audit_entry
*entry
)
851 struct audit_entry
*e
;
852 struct audit_watch
*watch
= entry
->rule
.watch
;
853 struct audit_tree
*tree
= entry
->rule
.tree
;
854 struct list_head
*list
;
856 #ifdef CONFIG_AUDITSYSCALL
859 /* If either of these, don't count towards total */
860 if (entry
->rule
.listnr
== AUDIT_FILTER_USER
||
861 entry
->rule
.listnr
== AUDIT_FILTER_TYPE
)
865 mutex_lock(&audit_filter_mutex
);
866 e
= audit_find_rule(entry
, &list
);
868 mutex_unlock(&audit_filter_mutex
);
870 /* normally audit_add_tree_rule() will free it on failure */
872 audit_put_tree(tree
);
877 /* audit_filter_mutex is dropped and re-taken during this call */
878 err
= audit_add_watch(&entry
->rule
, &list
);
880 mutex_unlock(&audit_filter_mutex
);
882 * normally audit_add_tree_rule() will free it
886 audit_put_tree(tree
);
891 err
= audit_add_tree_rule(&entry
->rule
);
893 mutex_unlock(&audit_filter_mutex
);
898 entry
->rule
.prio
= ~0ULL;
899 if (entry
->rule
.listnr
== AUDIT_FILTER_EXIT
) {
900 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
)
901 entry
->rule
.prio
= ++prio_high
;
903 entry
->rule
.prio
= --prio_low
;
906 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
) {
907 list_add(&entry
->rule
.list
,
908 &audit_rules_list
[entry
->rule
.listnr
]);
909 list_add_rcu(&entry
->list
, list
);
910 entry
->rule
.flags
&= ~AUDIT_FILTER_PREPEND
;
912 list_add_tail(&entry
->rule
.list
,
913 &audit_rules_list
[entry
->rule
.listnr
]);
914 list_add_tail_rcu(&entry
->list
, list
);
916 #ifdef CONFIG_AUDITSYSCALL
920 if (!audit_match_signal(entry
))
923 mutex_unlock(&audit_filter_mutex
);
929 audit_put_watch(watch
); /* tmp watch, matches initial get */
933 /* Remove an existing rule from filterlist. */
934 static inline int audit_del_rule(struct audit_entry
*entry
)
936 struct audit_entry
*e
;
937 struct audit_watch
*watch
= entry
->rule
.watch
;
938 struct audit_tree
*tree
= entry
->rule
.tree
;
939 struct list_head
*list
;
941 #ifdef CONFIG_AUDITSYSCALL
944 /* If either of these, don't count towards total */
945 if (entry
->rule
.listnr
== AUDIT_FILTER_USER
||
946 entry
->rule
.listnr
== AUDIT_FILTER_TYPE
)
950 mutex_lock(&audit_filter_mutex
);
951 e
= audit_find_rule(entry
, &list
);
953 mutex_unlock(&audit_filter_mutex
);
959 audit_remove_watch_rule(&e
->rule
);
962 audit_remove_tree_rule(&e
->rule
);
964 list_del_rcu(&e
->list
);
965 list_del(&e
->rule
.list
);
966 call_rcu(&e
->rcu
, audit_free_rule_rcu
);
968 #ifdef CONFIG_AUDITSYSCALL
972 if (!audit_match_signal(entry
))
975 mutex_unlock(&audit_filter_mutex
);
979 audit_put_watch(watch
); /* match initial get */
981 audit_put_tree(tree
); /* that's the temporary one */
986 /* List rules using struct audit_rule_data. */
987 static void audit_list_rules(__u32 portid
, int seq
, struct sk_buff_head
*q
)
990 struct audit_krule
*r
;
993 /* This is a blocking read, so use audit_filter_mutex instead of rcu
994 * iterator to sync with list writers. */
995 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
996 list_for_each_entry(r
, &audit_rules_list
[i
], list
) {
997 struct audit_rule_data
*data
;
999 data
= audit_krule_to_data(r
);
1000 if (unlikely(!data
))
1002 skb
= audit_make_reply(portid
, seq
, AUDIT_LIST_RULES
,
1004 sizeof(*data
) + data
->buflen
);
1006 skb_queue_tail(q
, skb
);
1010 skb
= audit_make_reply(portid
, seq
, AUDIT_LIST_RULES
, 1, 1, NULL
, 0);
1012 skb_queue_tail(q
, skb
);
1015 /* Log rule additions and removals */
1016 static void audit_log_rule_change(char *action
, struct audit_krule
*rule
, int res
)
1018 struct audit_buffer
*ab
;
1019 uid_t loginuid
= from_kuid(&init_user_ns
, audit_get_loginuid(current
));
1020 unsigned int sessionid
= audit_get_sessionid(current
);
1025 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
);
1028 audit_log_format(ab
, "auid=%u ses=%u" ,loginuid
, sessionid
);
1029 audit_log_task_context(ab
);
1030 audit_log_format(ab
, " op=");
1031 audit_log_string(ab
, action
);
1032 audit_log_key(ab
, rule
->filterkey
);
1033 audit_log_format(ab
, " list=%d res=%d", rule
->listnr
, res
);
1038 * audit_rule_change - apply all rules to the specified message type
1039 * @type: audit message type
1040 * @portid: target port id for netlink audit messages
1041 * @seq: netlink audit message sequence (serial) number
1042 * @data: payload data
1043 * @datasz: size of payload data
1045 int audit_rule_change(int type
, __u32 portid
, int seq
, void *data
,
1049 struct audit_entry
*entry
;
1052 case AUDIT_ADD_RULE
:
1053 entry
= audit_data_to_entry(data
, datasz
);
1055 return PTR_ERR(entry
);
1057 err
= audit_add_rule(entry
);
1058 audit_log_rule_change("add rule", &entry
->rule
, !err
);
1060 audit_free_rule(entry
);
1062 case AUDIT_DEL_RULE
:
1063 entry
= audit_data_to_entry(data
, datasz
);
1065 return PTR_ERR(entry
);
1067 err
= audit_del_rule(entry
);
1068 audit_log_rule_change("remove rule", &entry
->rule
, !err
);
1069 audit_free_rule(entry
);
1079 * audit_list_rules_send - list the audit rules
1080 * @request_skb: skb of request we are replying to (used to target the reply)
1081 * @seq: netlink audit message sequence (serial) number
1083 int audit_list_rules_send(struct sk_buff
*request_skb
, int seq
)
1085 u32 portid
= NETLINK_CB(request_skb
).portid
;
1086 struct net
*net
= sock_net(NETLINK_CB(request_skb
).sk
);
1087 struct task_struct
*tsk
;
1088 struct audit_netlink_list
*dest
;
1091 /* We can't just spew out the rules here because we might fill
1092 * the available socket buffer space and deadlock waiting for
1093 * auditctl to read from it... which isn't ever going to
1094 * happen if we're actually running in the context of auditctl
1095 * trying to _send_ the stuff */
1097 dest
= kmalloc(sizeof(struct audit_netlink_list
), GFP_KERNEL
);
1100 dest
->net
= get_net(net
);
1101 dest
->portid
= portid
;
1102 skb_queue_head_init(&dest
->q
);
1104 mutex_lock(&audit_filter_mutex
);
1105 audit_list_rules(portid
, seq
, &dest
->q
);
1106 mutex_unlock(&audit_filter_mutex
);
1108 tsk
= kthread_run(audit_send_list
, dest
, "audit_send_list");
1110 skb_queue_purge(&dest
->q
);
1118 int audit_comparator(u32 left
, u32 op
, u32 right
)
1122 return (left
== right
);
1123 case Audit_not_equal
:
1124 return (left
!= right
);
1126 return (left
< right
);
1128 return (left
<= right
);
1130 return (left
> right
);
1132 return (left
>= right
);
1134 return (left
& right
);
1136 return ((left
& right
) == right
);
1143 int audit_uid_comparator(kuid_t left
, u32 op
, kuid_t right
)
1147 return uid_eq(left
, right
);
1148 case Audit_not_equal
:
1149 return !uid_eq(left
, right
);
1151 return uid_lt(left
, right
);
1153 return uid_lte(left
, right
);
1155 return uid_gt(left
, right
);
1157 return uid_gte(left
, right
);
1166 int audit_gid_comparator(kgid_t left
, u32 op
, kgid_t right
)
1170 return gid_eq(left
, right
);
1171 case Audit_not_equal
:
1172 return !gid_eq(left
, right
);
1174 return gid_lt(left
, right
);
1176 return gid_lte(left
, right
);
1178 return gid_gt(left
, right
);
1180 return gid_gte(left
, right
);
1190 * parent_len - find the length of the parent portion of a pathname
1191 * @path: pathname of which to determine length
1193 int parent_len(const char *path
)
1198 plen
= strlen(path
);
1203 /* disregard trailing slashes */
1204 p
= path
+ plen
- 1;
1205 while ((*p
== '/') && (p
> path
))
1208 /* walk backward until we find the next slash or hit beginning */
1209 while ((*p
!= '/') && (p
> path
))
1212 /* did we find a slash? Then increment to include it in path */
1220 * audit_compare_dname_path - compare given dentry name with last component in
1221 * given path. Return of 0 indicates a match.
1222 * @dname: dentry name that we're comparing
1223 * @path: full pathname that we're comparing
1224 * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
1225 * here indicates that we must compute this value.
1227 int audit_compare_dname_path(const char *dname
, const char *path
, int parentlen
)
1232 dlen
= strlen(dname
);
1233 pathlen
= strlen(path
);
1237 parentlen
= parentlen
== AUDIT_NAME_FULL
? parent_len(path
) : parentlen
;
1238 if (pathlen
- parentlen
!= dlen
)
1241 p
= path
+ parentlen
;
1243 return strncmp(p
, dname
, dlen
);
1246 static int audit_filter_user_rules(struct audit_krule
*rule
, int type
,
1247 enum audit_state
*state
)
1251 for (i
= 0; i
< rule
->field_count
; i
++) {
1252 struct audit_field
*f
= &rule
->fields
[i
];
1258 result
= audit_comparator(task_pid_vnr(current
), f
->op
, f
->val
);
1261 result
= audit_uid_comparator(current_uid(), f
->op
, f
->uid
);
1264 result
= audit_gid_comparator(current_gid(), f
->op
, f
->gid
);
1266 case AUDIT_LOGINUID
:
1267 result
= audit_uid_comparator(audit_get_loginuid(current
),
1270 case AUDIT_LOGINUID_SET
:
1271 result
= audit_comparator(audit_loginuid_set(current
),
1275 result
= audit_comparator(type
, f
->op
, f
->val
);
1277 case AUDIT_SUBJ_USER
:
1278 case AUDIT_SUBJ_ROLE
:
1279 case AUDIT_SUBJ_TYPE
:
1280 case AUDIT_SUBJ_SEN
:
1281 case AUDIT_SUBJ_CLR
:
1283 security_task_getsecid(current
, &sid
);
1284 result
= security_audit_rule_match(sid
,
1296 switch (rule
->action
) {
1297 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
1298 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
1303 int audit_filter_user(int type
)
1305 enum audit_state state
= AUDIT_DISABLED
;
1306 struct audit_entry
*e
;
1309 ret
= 1; /* Audit by default */
1312 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_USER
], list
) {
1313 rc
= audit_filter_user_rules(&e
->rule
, type
, &state
);
1315 if (rc
> 0 && state
== AUDIT_DISABLED
)
1325 int audit_filter_type(int type
)
1327 struct audit_entry
*e
;
1331 if (list_empty(&audit_filter_list
[AUDIT_FILTER_TYPE
]))
1332 goto unlock_and_return
;
1334 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TYPE
],
1337 for (i
= 0; i
< e
->rule
.field_count
; i
++) {
1338 struct audit_field
*f
= &e
->rule
.fields
[i
];
1339 if (f
->type
== AUDIT_MSGTYPE
) {
1340 result
= audit_comparator(type
, f
->op
, f
->val
);
1346 goto unlock_and_return
;
1353 static int update_lsm_rule(struct audit_krule
*r
)
1355 struct audit_entry
*entry
= container_of(r
, struct audit_entry
, rule
);
1356 struct audit_entry
*nentry
;
1359 if (!security_audit_rule_known(r
))
1362 nentry
= audit_dupe_rule(r
);
1363 if (IS_ERR(nentry
)) {
1364 /* save the first error encountered for the
1366 err
= PTR_ERR(nentry
);
1367 audit_panic("error updating LSM filters");
1369 list_del(&r
->rlist
);
1370 list_del_rcu(&entry
->list
);
1373 if (r
->watch
|| r
->tree
)
1374 list_replace_init(&r
->rlist
, &nentry
->rule
.rlist
);
1375 list_replace_rcu(&entry
->list
, &nentry
->list
);
1376 list_replace(&r
->list
, &nentry
->rule
.list
);
1378 call_rcu(&entry
->rcu
, audit_free_rule_rcu
);
1383 /* This function will re-initialize the lsm_rule field of all applicable rules.
1384 * It will traverse the filter lists serarching for rules that contain LSM
1385 * specific filter fields. When such a rule is found, it is copied, the
1386 * LSM field is re-initialized, and the old rule is replaced with the
1388 int audit_update_lsm_rules(void)
1390 struct audit_krule
*r
, *n
;
1393 /* audit_filter_mutex synchronizes the writers */
1394 mutex_lock(&audit_filter_mutex
);
1396 for (i
= 0; i
< AUDIT_NR_FILTERS
; i
++) {
1397 list_for_each_entry_safe(r
, n
, &audit_rules_list
[i
], list
) {
1398 int res
= update_lsm_rule(r
);
1403 mutex_unlock(&audit_filter_mutex
);