1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* auditfilter.c -- filtering of audit events
4 * Copyright 2003-2004 Red Hat, Inc.
5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright 2005 IBM Corporation
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/audit.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
16 #include <linux/namei.h>
17 #include <linux/netlink.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/security.h>
21 #include <net/net_namespace.h>
29 * Synchronizes writes and blocking reads of audit's filterlist
30 * data. Rcu is used to traverse the filterlist and access
31 * contents of structs audit_entry, audit_watch and opaque
32 * LSM rules during filtering. If modified, these structures
33 * must be copied and replace their counterparts in the filterlist.
34 * An audit_parent struct is not accessed during filtering, so may
35 * be written directly provided audit_filter_mutex is held.
38 /* Audit filter lists, defined in <linux/audit.h> */
39 struct list_head audit_filter_list
[AUDIT_NR_FILTERS
] = {
40 LIST_HEAD_INIT(audit_filter_list
[0]),
41 LIST_HEAD_INIT(audit_filter_list
[1]),
42 LIST_HEAD_INIT(audit_filter_list
[2]),
43 LIST_HEAD_INIT(audit_filter_list
[3]),
44 LIST_HEAD_INIT(audit_filter_list
[4]),
45 LIST_HEAD_INIT(audit_filter_list
[5]),
46 LIST_HEAD_INIT(audit_filter_list
[6]),
47 #if AUDIT_NR_FILTERS != 7
48 #error Fix audit_filter_list initialiser
51 static struct list_head audit_rules_list
[AUDIT_NR_FILTERS
] = {
52 LIST_HEAD_INIT(audit_rules_list
[0]),
53 LIST_HEAD_INIT(audit_rules_list
[1]),
54 LIST_HEAD_INIT(audit_rules_list
[2]),
55 LIST_HEAD_INIT(audit_rules_list
[3]),
56 LIST_HEAD_INIT(audit_rules_list
[4]),
57 LIST_HEAD_INIT(audit_rules_list
[5]),
58 LIST_HEAD_INIT(audit_rules_list
[6]),
61 DEFINE_MUTEX(audit_filter_mutex
);
63 static void audit_free_lsm_field(struct audit_field
*f
)
74 case AUDIT_OBJ_LEV_LOW
:
75 case AUDIT_OBJ_LEV_HIGH
:
77 security_audit_rule_free(f
->lsm_rules
);
81 static inline void audit_free_rule(struct audit_entry
*e
)
84 struct audit_krule
*erule
= &e
->rule
;
86 /* some rules don't have associated watches */
88 audit_put_watch(erule
->watch
);
90 for (i
= 0; i
< erule
->field_count
; i
++)
91 audit_free_lsm_field(&erule
->fields
[i
]);
93 kfree(erule
->filterkey
);
97 void audit_free_rule_rcu(struct rcu_head
*head
)
99 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
103 /* Initialize an audit filterlist entry. */
104 static inline struct audit_entry
*audit_init_entry(u32 field_count
)
106 struct audit_entry
*entry
;
107 struct audit_field
*fields
;
109 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
110 if (unlikely(!entry
))
113 fields
= kcalloc(field_count
, sizeof(*fields
), GFP_KERNEL
);
114 if (unlikely(!fields
)) {
118 entry
->rule
.fields
= fields
;
123 /* Unpack a filter field's string representation from user-space
125 char *audit_unpack_string(void **bufp
, size_t *remain
, size_t len
)
129 if (!*bufp
|| (len
== 0) || (len
> *remain
))
130 return ERR_PTR(-EINVAL
);
132 /* Of the currently implemented string fields, PATH_MAX
133 * defines the longest valid length.
136 return ERR_PTR(-ENAMETOOLONG
);
138 str
= kmalloc(len
+ 1, GFP_KERNEL
);
140 return ERR_PTR(-ENOMEM
);
142 memcpy(str
, *bufp
, len
);
150 /* Translate an inode field to kernel representation. */
151 static inline int audit_to_inode(struct audit_krule
*krule
,
152 struct audit_field
*f
)
154 if (krule
->listnr
!= AUDIT_FILTER_EXIT
||
155 krule
->inode_f
|| krule
->watch
|| krule
->tree
||
156 (f
->op
!= Audit_equal
&& f
->op
!= Audit_not_equal
))
163 static __u32
*classes
[AUDIT_SYSCALL_CLASSES
];
165 int __init
audit_register_class(int class, unsigned *list
)
167 __u32
*p
= kcalloc(AUDIT_BITMASK_SIZE
, sizeof(__u32
), GFP_KERNEL
);
170 while (*list
!= ~0U) {
171 unsigned n
= *list
++;
172 if (n
>= AUDIT_BITMASK_SIZE
* 32 - AUDIT_SYSCALL_CLASSES
) {
176 p
[AUDIT_WORD(n
)] |= AUDIT_BIT(n
);
178 if (class >= AUDIT_SYSCALL_CLASSES
|| classes
[class]) {
186 int audit_match_class(int class, unsigned syscall
)
188 if (unlikely(syscall
>= AUDIT_BITMASK_SIZE
* 32))
190 if (unlikely(class >= AUDIT_SYSCALL_CLASSES
|| !classes
[class]))
192 return classes
[class][AUDIT_WORD(syscall
)] & AUDIT_BIT(syscall
);
195 #ifdef CONFIG_AUDITSYSCALL
196 static inline int audit_match_class_bits(int class, u32
*mask
)
200 if (classes
[class]) {
201 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
202 if (mask
[i
] & classes
[class][i
])
208 static int audit_match_signal(struct audit_entry
*entry
)
210 struct audit_field
*arch
= entry
->rule
.arch_f
;
213 /* When arch is unspecified, we must check both masks on biarch
214 * as syscall number alone is ambiguous. */
215 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL
,
217 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32
,
221 switch(audit_classify_arch(arch
->val
)) {
223 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL
,
225 case 1: /* 32bit on biarch */
226 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32
,
234 /* Common user-space to kernel rule translation. */
235 static inline struct audit_entry
*audit_to_entry_common(struct audit_rule_data
*rule
)
238 struct audit_entry
*entry
;
242 listnr
= rule
->flags
& ~AUDIT_FILTER_PREPEND
;
246 #ifdef CONFIG_AUDITSYSCALL
247 case AUDIT_FILTER_ENTRY
:
248 pr_err("AUDIT_FILTER_ENTRY is deprecated\n");
250 case AUDIT_FILTER_EXIT
:
251 case AUDIT_FILTER_TASK
:
253 case AUDIT_FILTER_USER
:
254 case AUDIT_FILTER_EXCLUDE
:
255 case AUDIT_FILTER_FS
:
258 if (unlikely(rule
->action
== AUDIT_POSSIBLE
)) {
259 pr_err("AUDIT_POSSIBLE is deprecated\n");
262 if (rule
->action
!= AUDIT_NEVER
&& rule
->action
!= AUDIT_ALWAYS
)
264 if (rule
->field_count
> AUDIT_MAX_FIELDS
)
268 entry
= audit_init_entry(rule
->field_count
);
272 entry
->rule
.flags
= rule
->flags
& AUDIT_FILTER_PREPEND
;
273 entry
->rule
.listnr
= listnr
;
274 entry
->rule
.action
= rule
->action
;
275 entry
->rule
.field_count
= rule
->field_count
;
277 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
278 entry
->rule
.mask
[i
] = rule
->mask
[i
];
280 for (i
= 0; i
< AUDIT_SYSCALL_CLASSES
; i
++) {
281 int bit
= AUDIT_BITMASK_SIZE
* 32 - i
- 1;
282 __u32
*p
= &entry
->rule
.mask
[AUDIT_WORD(bit
)];
285 if (!(*p
& AUDIT_BIT(bit
)))
287 *p
&= ~AUDIT_BIT(bit
);
291 for (j
= 0; j
< AUDIT_BITMASK_SIZE
; j
++)
292 entry
->rule
.mask
[j
] |= class[j
];
302 static u32 audit_ops
[] =
304 [Audit_equal
] = AUDIT_EQUAL
,
305 [Audit_not_equal
] = AUDIT_NOT_EQUAL
,
306 [Audit_bitmask
] = AUDIT_BIT_MASK
,
307 [Audit_bittest
] = AUDIT_BIT_TEST
,
308 [Audit_lt
] = AUDIT_LESS_THAN
,
309 [Audit_gt
] = AUDIT_GREATER_THAN
,
310 [Audit_le
] = AUDIT_LESS_THAN_OR_EQUAL
,
311 [Audit_ge
] = AUDIT_GREATER_THAN_OR_EQUAL
,
314 static u32
audit_to_op(u32 op
)
317 for (n
= Audit_equal
; n
< Audit_bad
&& audit_ops
[n
] != op
; n
++)
322 /* check if an audit field is valid */
323 static int audit_field_valid(struct audit_entry
*entry
, struct audit_field
*f
)
327 if (entry
->rule
.listnr
!= AUDIT_FILTER_EXCLUDE
&&
328 entry
->rule
.listnr
!= AUDIT_FILTER_USER
)
332 if (entry
->rule
.listnr
!= AUDIT_FILTER_FS
)
337 switch (entry
->rule
.listnr
) {
338 case AUDIT_FILTER_FS
:
341 case AUDIT_FILTERKEY
:
348 /* Check for valid field type and op */
354 case AUDIT_PERS
: /* <uapi/linux/personality.h> */
356 /* all ops are valid */
376 case AUDIT_SESSIONID
:
379 case AUDIT_OBJ_LEV_LOW
:
380 case AUDIT_OBJ_LEV_HIGH
:
381 case AUDIT_SADDR_FAM
:
382 /* bit ops are only useful on syscall args */
383 if (f
->op
== Audit_bitmask
|| f
->op
== Audit_bittest
)
386 case AUDIT_SUBJ_USER
:
387 case AUDIT_SUBJ_ROLE
:
388 case AUDIT_SUBJ_TYPE
:
394 case AUDIT_FILTERKEY
:
395 case AUDIT_LOGINUID_SET
:
400 case AUDIT_FIELD_COMPARE
:
402 /* only equal and not equal valid ops */
403 if (f
->op
!= Audit_not_equal
&& f
->op
!= Audit_equal
)
407 /* field not recognized */
411 /* Check for select valid field values */
413 case AUDIT_LOGINUID_SET
:
414 if ((f
->val
!= 0) && (f
->val
!= 1))
422 if (f
->val
& ~S_IFMT
)
425 case AUDIT_FIELD_COMPARE
:
426 if (f
->val
> AUDIT_MAX_FIELD_COMPARE
)
429 case AUDIT_SADDR_FAM
:
430 if (f
->val
>= AF_MAX
)
440 /* Translate struct audit_rule_data to kernel's rule representation. */
441 static struct audit_entry
*audit_data_to_entry(struct audit_rule_data
*data
,
445 struct audit_entry
*entry
;
447 size_t remain
= datasz
- sizeof(struct audit_rule_data
);
450 struct audit_fsnotify_mark
*audit_mark
;
452 entry
= audit_to_entry_common(data
);
457 for (i
= 0; i
< data
->field_count
; i
++) {
458 struct audit_field
*f
= &entry
->rule
.fields
[i
];
463 f
->op
= audit_to_op(data
->fieldflags
[i
]);
464 if (f
->op
== Audit_bad
)
467 f
->type
= data
->fields
[i
];
468 f_val
= data
->values
[i
];
470 /* Support legacy tests for a valid loginuid */
471 if ((f
->type
== AUDIT_LOGINUID
) && (f_val
== AUDIT_UID_UNSET
)) {
472 f
->type
= AUDIT_LOGINUID_SET
;
474 entry
->rule
.pflags
|= AUDIT_LOGINUID_LEGACY
;
477 err
= audit_field_valid(entry
, f
);
489 f
->uid
= make_kuid(current_user_ns(), f_val
);
490 if (!uid_valid(f
->uid
))
498 f
->gid
= make_kgid(current_user_ns(), f_val
);
499 if (!gid_valid(f
->gid
))
504 entry
->rule
.arch_f
= f
;
506 case AUDIT_SUBJ_USER
:
507 case AUDIT_SUBJ_ROLE
:
508 case AUDIT_SUBJ_TYPE
:
514 case AUDIT_OBJ_LEV_LOW
:
515 case AUDIT_OBJ_LEV_HIGH
:
516 str
= audit_unpack_string(&bufp
, &remain
, f_val
);
521 entry
->rule
.buflen
+= f_val
;
524 err
= security_audit_rule_init(f
->type
, f
->op
, str
,
526 /* Keep currently invalid fields around in case they
527 * become valid after a policy reload. */
528 if (err
== -EINVAL
) {
529 pr_warn("audit rule for LSM \'%s\' is invalid\n",
536 str
= audit_unpack_string(&bufp
, &remain
, f_val
);
541 err
= audit_to_watch(&entry
->rule
, str
, f_val
, f
->op
);
546 entry
->rule
.buflen
+= f_val
;
549 str
= audit_unpack_string(&bufp
, &remain
, f_val
);
554 err
= audit_make_tree(&entry
->rule
, str
, f
->op
);
558 entry
->rule
.buflen
+= f_val
;
562 err
= audit_to_inode(&entry
->rule
, f
);
566 case AUDIT_FILTERKEY
:
567 if (entry
->rule
.filterkey
|| f_val
> AUDIT_MAX_KEY_LEN
)
569 str
= audit_unpack_string(&bufp
, &remain
, f_val
);
574 entry
->rule
.buflen
+= f_val
;
575 entry
->rule
.filterkey
= str
;
578 if (entry
->rule
.exe
|| f_val
> PATH_MAX
)
580 str
= audit_unpack_string(&bufp
, &remain
, f_val
);
585 audit_mark
= audit_alloc_mark(&entry
->rule
, str
, f_val
);
586 if (IS_ERR(audit_mark
)) {
588 err
= PTR_ERR(audit_mark
);
591 entry
->rule
.buflen
+= f_val
;
592 entry
->rule
.exe
= audit_mark
;
600 if (entry
->rule
.inode_f
&& entry
->rule
.inode_f
->op
== Audit_not_equal
)
601 entry
->rule
.inode_f
= NULL
;
607 if (entry
->rule
.tree
)
608 audit_put_tree(entry
->rule
.tree
); /* that's the temporary one */
610 audit_remove_mark(entry
->rule
.exe
); /* that's the template one */
611 audit_free_rule(entry
);
615 /* Pack a filter field's string representation into data block. */
616 static inline size_t audit_pack_string(void **bufp
, const char *str
)
618 size_t len
= strlen(str
);
620 memcpy(*bufp
, str
, len
);
626 /* Translate kernel rule representation to struct audit_rule_data. */
627 static struct audit_rule_data
*audit_krule_to_data(struct audit_krule
*krule
)
629 struct audit_rule_data
*data
;
633 data
= kmalloc(sizeof(*data
) + krule
->buflen
, GFP_KERNEL
);
636 memset(data
, 0, sizeof(*data
));
638 data
->flags
= krule
->flags
| krule
->listnr
;
639 data
->action
= krule
->action
;
640 data
->field_count
= krule
->field_count
;
642 for (i
= 0; i
< data
->field_count
; i
++) {
643 struct audit_field
*f
= &krule
->fields
[i
];
645 data
->fields
[i
] = f
->type
;
646 data
->fieldflags
[i
] = audit_ops
[f
->op
];
648 case AUDIT_SUBJ_USER
:
649 case AUDIT_SUBJ_ROLE
:
650 case AUDIT_SUBJ_TYPE
:
656 case AUDIT_OBJ_LEV_LOW
:
657 case AUDIT_OBJ_LEV_HIGH
:
658 data
->buflen
+= data
->values
[i
] =
659 audit_pack_string(&bufp
, f
->lsm_str
);
662 data
->buflen
+= data
->values
[i
] =
663 audit_pack_string(&bufp
,
664 audit_watch_path(krule
->watch
));
667 data
->buflen
+= data
->values
[i
] =
668 audit_pack_string(&bufp
,
669 audit_tree_path(krule
->tree
));
671 case AUDIT_FILTERKEY
:
672 data
->buflen
+= data
->values
[i
] =
673 audit_pack_string(&bufp
, krule
->filterkey
);
676 data
->buflen
+= data
->values
[i
] =
677 audit_pack_string(&bufp
, audit_mark_path(krule
->exe
));
679 case AUDIT_LOGINUID_SET
:
680 if (krule
->pflags
& AUDIT_LOGINUID_LEGACY
&& !f
->val
) {
681 data
->fields
[i
] = AUDIT_LOGINUID
;
682 data
->values
[i
] = AUDIT_UID_UNSET
;
685 fallthrough
; /* if set */
687 data
->values
[i
] = f
->val
;
690 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) data
->mask
[i
] = krule
->mask
[i
];
695 /* Compare two rules in kernel format. Considered success if rules
697 static int audit_compare_rule(struct audit_krule
*a
, struct audit_krule
*b
)
701 if (a
->flags
!= b
->flags
||
702 a
->pflags
!= b
->pflags
||
703 a
->listnr
!= b
->listnr
||
704 a
->action
!= b
->action
||
705 a
->field_count
!= b
->field_count
)
708 for (i
= 0; i
< a
->field_count
; i
++) {
709 if (a
->fields
[i
].type
!= b
->fields
[i
].type
||
710 a
->fields
[i
].op
!= b
->fields
[i
].op
)
713 switch(a
->fields
[i
].type
) {
714 case AUDIT_SUBJ_USER
:
715 case AUDIT_SUBJ_ROLE
:
716 case AUDIT_SUBJ_TYPE
:
722 case AUDIT_OBJ_LEV_LOW
:
723 case AUDIT_OBJ_LEV_HIGH
:
724 if (strcmp(a
->fields
[i
].lsm_str
, b
->fields
[i
].lsm_str
))
728 if (strcmp(audit_watch_path(a
->watch
),
729 audit_watch_path(b
->watch
)))
733 if (strcmp(audit_tree_path(a
->tree
),
734 audit_tree_path(b
->tree
)))
737 case AUDIT_FILTERKEY
:
738 /* both filterkeys exist based on above type compare */
739 if (strcmp(a
->filterkey
, b
->filterkey
))
743 /* both paths exist based on above type compare */
744 if (strcmp(audit_mark_path(a
->exe
),
745 audit_mark_path(b
->exe
)))
754 if (!uid_eq(a
->fields
[i
].uid
, b
->fields
[i
].uid
))
762 if (!gid_eq(a
->fields
[i
].gid
, b
->fields
[i
].gid
))
766 if (a
->fields
[i
].val
!= b
->fields
[i
].val
)
771 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
772 if (a
->mask
[i
] != b
->mask
[i
])
778 /* Duplicate LSM field information. The lsm_rules is opaque, so must be
780 static inline int audit_dupe_lsm_field(struct audit_field
*df
,
781 struct audit_field
*sf
)
786 /* our own copy of lsm_str */
787 lsm_str
= kstrdup(sf
->lsm_str
, GFP_KERNEL
);
788 if (unlikely(!lsm_str
))
790 df
->lsm_str
= lsm_str
;
792 /* our own (refreshed) copy of lsm_rules */
793 ret
= security_audit_rule_init(df
->type
, df
->op
, df
->lsm_str
,
795 /* Keep currently invalid fields around in case they
796 * become valid after a policy reload. */
797 if (ret
== -EINVAL
) {
798 pr_warn("audit rule for LSM \'%s\' is invalid\n",
806 /* Duplicate an audit rule. This will be a deep copy with the exception
807 * of the watch - that pointer is carried over. The LSM specific fields
808 * will be updated in the copy. The point is to be able to replace the old
809 * rule with the new rule in the filterlist, then free the old rule.
810 * The rlist element is undefined; list manipulations are handled apart from
811 * the initial copy. */
812 struct audit_entry
*audit_dupe_rule(struct audit_krule
*old
)
814 u32 fcount
= old
->field_count
;
815 struct audit_entry
*entry
;
816 struct audit_krule
*new;
820 entry
= audit_init_entry(fcount
);
821 if (unlikely(!entry
))
822 return ERR_PTR(-ENOMEM
);
825 new->flags
= old
->flags
;
826 new->pflags
= old
->pflags
;
827 new->listnr
= old
->listnr
;
828 new->action
= old
->action
;
829 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
830 new->mask
[i
] = old
->mask
[i
];
831 new->prio
= old
->prio
;
832 new->buflen
= old
->buflen
;
833 new->inode_f
= old
->inode_f
;
834 new->field_count
= old
->field_count
;
837 * note that we are OK with not refcounting here; audit_match_tree()
838 * never dereferences tree and we can't get false positives there
839 * since we'd have to have rule gone from the list *and* removed
840 * before the chunks found by lookup had been allocated, i.e. before
841 * the beginning of list scan.
843 new->tree
= old
->tree
;
844 memcpy(new->fields
, old
->fields
, sizeof(struct audit_field
) * fcount
);
846 /* deep copy this information, updating the lsm_rules fields, because
847 * the originals will all be freed when the old rule is freed. */
848 for (i
= 0; i
< fcount
; i
++) {
849 switch (new->fields
[i
].type
) {
850 case AUDIT_SUBJ_USER
:
851 case AUDIT_SUBJ_ROLE
:
852 case AUDIT_SUBJ_TYPE
:
858 case AUDIT_OBJ_LEV_LOW
:
859 case AUDIT_OBJ_LEV_HIGH
:
860 err
= audit_dupe_lsm_field(&new->fields
[i
],
863 case AUDIT_FILTERKEY
:
864 fk
= kstrdup(old
->filterkey
, GFP_KERNEL
);
871 err
= audit_dupe_exe(new, old
);
876 audit_remove_mark(new->exe
);
877 audit_free_rule(entry
);
883 audit_get_watch(old
->watch
);
884 new->watch
= old
->watch
;
890 /* Find an existing audit rule.
891 * Caller must hold audit_filter_mutex to prevent stale rule data. */
892 static struct audit_entry
*audit_find_rule(struct audit_entry
*entry
,
893 struct list_head
**p
)
895 struct audit_entry
*e
, *found
= NULL
;
896 struct list_head
*list
;
899 if (entry
->rule
.inode_f
) {
900 h
= audit_hash_ino(entry
->rule
.inode_f
->val
);
901 *p
= list
= &audit_inode_hash
[h
];
902 } else if (entry
->rule
.watch
) {
903 /* we don't know the inode number, so must walk entire hash */
904 for (h
= 0; h
< AUDIT_INODE_BUCKETS
; h
++) {
905 list
= &audit_inode_hash
[h
];
906 list_for_each_entry(e
, list
, list
)
907 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
914 *p
= list
= &audit_filter_list
[entry
->rule
.listnr
];
917 list_for_each_entry(e
, list
, list
)
918 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
927 static u64 prio_low
= ~0ULL/2;
928 static u64 prio_high
= ~0ULL/2 - 1;
930 /* Add rule to given filterlist if not a duplicate. */
931 static inline int audit_add_rule(struct audit_entry
*entry
)
933 struct audit_entry
*e
;
934 struct audit_watch
*watch
= entry
->rule
.watch
;
935 struct audit_tree
*tree
= entry
->rule
.tree
;
936 struct list_head
*list
;
938 #ifdef CONFIG_AUDITSYSCALL
941 /* If any of these, don't count towards total */
942 switch(entry
->rule
.listnr
) {
943 case AUDIT_FILTER_USER
:
944 case AUDIT_FILTER_EXCLUDE
:
945 case AUDIT_FILTER_FS
:
950 mutex_lock(&audit_filter_mutex
);
951 e
= audit_find_rule(entry
, &list
);
953 mutex_unlock(&audit_filter_mutex
);
955 /* normally audit_add_tree_rule() will free it on failure */
957 audit_put_tree(tree
);
962 /* audit_filter_mutex is dropped and re-taken during this call */
963 err
= audit_add_watch(&entry
->rule
, &list
);
965 mutex_unlock(&audit_filter_mutex
);
967 * normally audit_add_tree_rule() will free it
971 audit_put_tree(tree
);
976 err
= audit_add_tree_rule(&entry
->rule
);
978 mutex_unlock(&audit_filter_mutex
);
983 entry
->rule
.prio
= ~0ULL;
984 if (entry
->rule
.listnr
== AUDIT_FILTER_EXIT
) {
985 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
)
986 entry
->rule
.prio
= ++prio_high
;
988 entry
->rule
.prio
= --prio_low
;
991 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
) {
992 list_add(&entry
->rule
.list
,
993 &audit_rules_list
[entry
->rule
.listnr
]);
994 list_add_rcu(&entry
->list
, list
);
995 entry
->rule
.flags
&= ~AUDIT_FILTER_PREPEND
;
997 list_add_tail(&entry
->rule
.list
,
998 &audit_rules_list
[entry
->rule
.listnr
]);
999 list_add_tail_rcu(&entry
->list
, list
);
1001 #ifdef CONFIG_AUDITSYSCALL
1005 if (!audit_match_signal(entry
))
1008 mutex_unlock(&audit_filter_mutex
);
1013 /* Remove an existing rule from filterlist. */
1014 int audit_del_rule(struct audit_entry
*entry
)
1016 struct audit_entry
*e
;
1017 struct audit_tree
*tree
= entry
->rule
.tree
;
1018 struct list_head
*list
;
1020 #ifdef CONFIG_AUDITSYSCALL
1023 /* If any of these, don't count towards total */
1024 switch(entry
->rule
.listnr
) {
1025 case AUDIT_FILTER_USER
:
1026 case AUDIT_FILTER_EXCLUDE
:
1027 case AUDIT_FILTER_FS
:
1032 mutex_lock(&audit_filter_mutex
);
1033 e
= audit_find_rule(entry
, &list
);
1040 audit_remove_watch_rule(&e
->rule
);
1043 audit_remove_tree_rule(&e
->rule
);
1046 audit_remove_mark_rule(&e
->rule
);
1048 #ifdef CONFIG_AUDITSYSCALL
1052 if (!audit_match_signal(entry
))
1056 list_del_rcu(&e
->list
);
1057 list_del(&e
->rule
.list
);
1058 call_rcu(&e
->rcu
, audit_free_rule_rcu
);
1061 mutex_unlock(&audit_filter_mutex
);
1064 audit_put_tree(tree
); /* that's the temporary one */
1069 /* List rules using struct audit_rule_data. */
1070 static void audit_list_rules(int seq
, struct sk_buff_head
*q
)
1072 struct sk_buff
*skb
;
1073 struct audit_krule
*r
;
1076 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1077 * iterator to sync with list writers. */
1078 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
1079 list_for_each_entry(r
, &audit_rules_list
[i
], list
) {
1080 struct audit_rule_data
*data
;
1082 data
= audit_krule_to_data(r
);
1083 if (unlikely(!data
))
1085 skb
= audit_make_reply(seq
, AUDIT_LIST_RULES
, 0, 1,
1087 sizeof(*data
) + data
->buflen
);
1089 skb_queue_tail(q
, skb
);
1093 skb
= audit_make_reply(seq
, AUDIT_LIST_RULES
, 1, 1, NULL
, 0);
1095 skb_queue_tail(q
, skb
);
1098 /* Log rule additions and removals */
1099 static void audit_log_rule_change(char *action
, struct audit_krule
*rule
, int res
)
1101 struct audit_buffer
*ab
;
1106 ab
= audit_log_start(audit_context(), GFP_KERNEL
, AUDIT_CONFIG_CHANGE
);
1109 audit_log_session_info(ab
);
1110 audit_log_task_context(ab
, NULL
);
1111 audit_log_format(ab
, " op=%s", action
);
1112 audit_log_key(ab
, rule
->filterkey
);
1113 audit_log_format(ab
, " list=%d res=%d", rule
->listnr
, res
);
1118 * audit_rule_change - apply all rules to the specified message type
1119 * @type: audit message type
1120 * @seq: netlink audit message sequence (serial) number
1121 * @data: payload data
1122 * @datasz: size of payload data
1124 int audit_rule_change(int type
, int seq
, void *data
, size_t datasz
)
1127 struct audit_entry
*entry
;
1130 case AUDIT_ADD_RULE
:
1131 entry
= audit_data_to_entry(data
, datasz
);
1133 return PTR_ERR(entry
);
1134 err
= audit_add_rule(entry
);
1135 audit_log_rule_change("add_rule", &entry
->rule
, !err
);
1137 case AUDIT_DEL_RULE
:
1138 entry
= audit_data_to_entry(data
, datasz
);
1140 return PTR_ERR(entry
);
1141 err
= audit_del_rule(entry
);
1142 audit_log_rule_change("remove_rule", &entry
->rule
, !err
);
1149 if (err
|| type
== AUDIT_DEL_RULE
) {
1150 if (entry
->rule
.exe
)
1151 audit_remove_mark(entry
->rule
.exe
);
1152 audit_free_rule(entry
);
1159 * audit_list_rules_send - list the audit rules
1160 * @request_skb: skb of request we are replying to (used to target the reply)
1161 * @seq: netlink audit message sequence (serial) number
1163 int audit_list_rules_send(struct sk_buff
*request_skb
, int seq
)
1165 struct task_struct
*tsk
;
1166 struct audit_netlink_list
*dest
;
1168 /* We can't just spew out the rules here because we might fill
1169 * the available socket buffer space and deadlock waiting for
1170 * auditctl to read from it... which isn't ever going to
1171 * happen if we're actually running in the context of auditctl
1172 * trying to _send_ the stuff */
1174 dest
= kmalloc(sizeof(*dest
), GFP_KERNEL
);
1177 dest
->net
= get_net(sock_net(NETLINK_CB(request_skb
).sk
));
1178 dest
->portid
= NETLINK_CB(request_skb
).portid
;
1179 skb_queue_head_init(&dest
->q
);
1181 mutex_lock(&audit_filter_mutex
);
1182 audit_list_rules(seq
, &dest
->q
);
1183 mutex_unlock(&audit_filter_mutex
);
1185 tsk
= kthread_run(audit_send_list_thread
, dest
, "audit_send_list");
1187 skb_queue_purge(&dest
->q
);
1190 return PTR_ERR(tsk
);
1196 int audit_comparator(u32 left
, u32 op
, u32 right
)
1200 return (left
== right
);
1201 case Audit_not_equal
:
1202 return (left
!= right
);
1204 return (left
< right
);
1206 return (left
<= right
);
1208 return (left
> right
);
1210 return (left
>= right
);
1212 return (left
& right
);
1214 return ((left
& right
) == right
);
1220 int audit_uid_comparator(kuid_t left
, u32 op
, kuid_t right
)
1224 return uid_eq(left
, right
);
1225 case Audit_not_equal
:
1226 return !uid_eq(left
, right
);
1228 return uid_lt(left
, right
);
1230 return uid_lte(left
, right
);
1232 return uid_gt(left
, right
);
1234 return uid_gte(left
, right
);
1242 int audit_gid_comparator(kgid_t left
, u32 op
, kgid_t right
)
1246 return gid_eq(left
, right
);
1247 case Audit_not_equal
:
1248 return !gid_eq(left
, right
);
1250 return gid_lt(left
, right
);
1252 return gid_lte(left
, right
);
1254 return gid_gt(left
, right
);
1256 return gid_gte(left
, right
);
1265 * parent_len - find the length of the parent portion of a pathname
1266 * @path: pathname of which to determine length
1268 int parent_len(const char *path
)
1273 plen
= strlen(path
);
1278 /* disregard trailing slashes */
1279 p
= path
+ plen
- 1;
1280 while ((*p
== '/') && (p
> path
))
1283 /* walk backward until we find the next slash or hit beginning */
1284 while ((*p
!= '/') && (p
> path
))
1287 /* did we find a slash? Then increment to include it in path */
1295 * audit_compare_dname_path - compare given dentry name with last component in
1296 * given path. Return of 0 indicates a match.
1297 * @dname: dentry name that we're comparing
1298 * @path: full pathname that we're comparing
1299 * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
1300 * here indicates that we must compute this value.
1302 int audit_compare_dname_path(const struct qstr
*dname
, const char *path
, int parentlen
)
1308 pathlen
= strlen(path
);
1312 parentlen
= parentlen
== AUDIT_NAME_FULL
? parent_len(path
) : parentlen
;
1313 if (pathlen
- parentlen
!= dlen
)
1316 p
= path
+ parentlen
;
1318 return strncmp(p
, dname
->name
, dlen
);
1321 int audit_filter(int msgtype
, unsigned int listtype
)
1323 struct audit_entry
*e
;
1324 int ret
= 1; /* Audit by default */
1327 list_for_each_entry_rcu(e
, &audit_filter_list
[listtype
], list
) {
1330 for (i
= 0; i
< e
->rule
.field_count
; i
++) {
1331 struct audit_field
*f
= &e
->rule
.fields
[i
];
1333 struct lsmblob blob
;
1337 pid
= task_pid_nr(current
);
1338 result
= audit_comparator(pid
, f
->op
, f
->val
);
1341 result
= audit_uid_comparator(current_uid(), f
->op
, f
->uid
);
1344 result
= audit_gid_comparator(current_gid(), f
->op
, f
->gid
);
1346 case AUDIT_LOGINUID
:
1347 result
= audit_uid_comparator(audit_get_loginuid(current
),
1350 case AUDIT_LOGINUID_SET
:
1351 result
= audit_comparator(audit_loginuid_set(current
),
1355 result
= audit_comparator(msgtype
, f
->op
, f
->val
);
1357 case AUDIT_SUBJ_USER
:
1358 case AUDIT_SUBJ_ROLE
:
1359 case AUDIT_SUBJ_TYPE
:
1360 case AUDIT_SUBJ_SEN
:
1361 case AUDIT_SUBJ_CLR
:
1363 security_task_getsecid_subj(current
, &blob
);
1364 result
= security_audit_rule_match(
1365 &blob
, f
->type
, f
->op
,
1370 result
= audit_exe_compare(current
, e
->rule
.exe
);
1371 if (f
->op
== Audit_not_equal
)
1375 goto unlock_and_return
;
1377 if (result
< 0) /* error */
1378 goto unlock_and_return
;
1383 if (e
->rule
.action
== AUDIT_NEVER
|| listtype
== AUDIT_FILTER_EXCLUDE
)
1393 static int update_lsm_rules(struct audit_krule
*r
)
1395 struct audit_entry
*entry
= container_of(r
, struct audit_entry
, rule
);
1396 struct audit_entry
*nentry
;
1399 if (!security_audit_rule_known(r
))
1402 nentry
= audit_dupe_rule(r
);
1403 if (entry
->rule
.exe
)
1404 audit_remove_mark(entry
->rule
.exe
);
1405 if (IS_ERR(nentry
)) {
1406 /* save the first error encountered for the
1408 err
= PTR_ERR(nentry
);
1409 audit_panic("error updating LSM filters");
1411 list_del(&r
->rlist
);
1412 list_del_rcu(&entry
->list
);
1415 if (r
->watch
|| r
->tree
)
1416 list_replace_init(&r
->rlist
, &nentry
->rule
.rlist
);
1417 list_replace_rcu(&entry
->list
, &nentry
->list
);
1418 list_replace(&r
->list
, &nentry
->rule
.list
);
1420 call_rcu(&entry
->rcu
, audit_free_rule_rcu
);
1425 /* This function will re-initialize the lsm_rules field of all applicable rules.
1426 * It will traverse the filter lists serarching for rules that contain LSM
1427 * specific filter fields. When such a rule is found, it is copied, the
1428 * LSM field is re-initialized, and the old rule is replaced with the
1430 int audit_update_lsm_rules(void)
1432 struct audit_krule
*r
, *n
;
1435 /* audit_filter_mutex synchronizes the writers */
1436 mutex_lock(&audit_filter_mutex
);
1438 for (i
= 0; i
< AUDIT_NR_FILTERS
; i
++) {
1439 list_for_each_entry_safe(r
, n
, &audit_rules_list
[i
], list
) {
1440 int res
= update_lsm_rules(r
);
1445 mutex_unlock(&audit_filter_mutex
);