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/netlink.h>
26 #include <linux/selinux.h>
29 /* There are three lists of rules -- one to search at task creation
30 * time, one to search at syscall entry time, and another to search at
31 * syscall exit time. */
32 struct list_head audit_filter_list
[AUDIT_NR_FILTERS
] = {
33 LIST_HEAD_INIT(audit_filter_list
[0]),
34 LIST_HEAD_INIT(audit_filter_list
[1]),
35 LIST_HEAD_INIT(audit_filter_list
[2]),
36 LIST_HEAD_INIT(audit_filter_list
[3]),
37 LIST_HEAD_INIT(audit_filter_list
[4]),
38 LIST_HEAD_INIT(audit_filter_list
[5]),
39 #if AUDIT_NR_FILTERS != 6
40 #error Fix audit_filter_list initialiser
44 static inline void audit_free_rule(struct audit_entry
*e
)
48 for (i
= 0; i
< e
->rule
.field_count
; i
++) {
49 struct audit_field
*f
= &e
->rule
.fields
[i
];
51 selinux_audit_rule_free(f
->se_rule
);
53 kfree(e
->rule
.fields
);
57 static inline void audit_free_rule_rcu(struct rcu_head
*head
)
59 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
63 /* Initialize an audit filterlist entry. */
64 static inline struct audit_entry
*audit_init_entry(u32 field_count
)
66 struct audit_entry
*entry
;
67 struct audit_field
*fields
;
69 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
73 fields
= kzalloc(sizeof(*fields
) * field_count
, GFP_KERNEL
);
74 if (unlikely(!fields
)) {
78 entry
->rule
.fields
= fields
;
83 /* Unpack a filter field's string representation from user-space
85 static char *audit_unpack_string(void **bufp
, size_t *remain
, size_t len
)
89 if (!*bufp
|| (len
== 0) || (len
> *remain
))
90 return ERR_PTR(-EINVAL
);
92 /* Of the currently implemented string fields, PATH_MAX
93 * defines the longest valid length.
96 return ERR_PTR(-ENAMETOOLONG
);
98 str
= kmalloc(len
+ 1, GFP_KERNEL
);
100 return ERR_PTR(-ENOMEM
);
102 memcpy(str
, *bufp
, len
);
110 /* Common user-space to kernel rule translation. */
111 static inline struct audit_entry
*audit_to_entry_common(struct audit_rule
*rule
)
114 struct audit_entry
*entry
;
118 listnr
= rule
->flags
& ~AUDIT_FILTER_PREPEND
;
122 case AUDIT_FILTER_USER
:
123 case AUDIT_FILTER_TYPE
:
124 #ifdef CONFIG_AUDITSYSCALL
125 case AUDIT_FILTER_ENTRY
:
126 case AUDIT_FILTER_EXIT
:
127 case AUDIT_FILTER_TASK
:
131 if (rule
->action
!= AUDIT_NEVER
&& rule
->action
!= AUDIT_POSSIBLE
&&
132 rule
->action
!= AUDIT_ALWAYS
)
134 if (rule
->field_count
> AUDIT_MAX_FIELDS
)
138 entry
= audit_init_entry(rule
->field_count
);
142 entry
->rule
.flags
= rule
->flags
& AUDIT_FILTER_PREPEND
;
143 entry
->rule
.listnr
= listnr
;
144 entry
->rule
.action
= rule
->action
;
145 entry
->rule
.field_count
= rule
->field_count
;
147 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
148 entry
->rule
.mask
[i
] = rule
->mask
[i
];
156 /* Translate struct audit_rule to kernel's rule respresentation.
157 * Exists for backward compatibility with userspace. */
158 static struct audit_entry
*audit_rule_to_entry(struct audit_rule
*rule
)
160 struct audit_entry
*entry
;
164 entry
= audit_to_entry_common(rule
);
168 for (i
= 0; i
< rule
->field_count
; i
++) {
169 struct audit_field
*f
= &entry
->rule
.fields
[i
];
171 f
->op
= rule
->fields
[i
] & (AUDIT_NEGATE
|AUDIT_OPERATORS
);
172 f
->type
= rule
->fields
[i
] & ~(AUDIT_NEGATE
|AUDIT_OPERATORS
);
173 f
->val
= rule
->values
[i
];
175 if (f
->type
& AUDIT_UNUSED_BITS
||
176 f
->type
== AUDIT_SE_USER
||
177 f
->type
== AUDIT_SE_ROLE
||
178 f
->type
== AUDIT_SE_TYPE
||
179 f
->type
== AUDIT_SE_SEN
||
180 f
->type
== AUDIT_SE_CLR
) {
185 entry
->rule
.vers_ops
= (f
->op
& AUDIT_OPERATORS
) ? 2 : 1;
187 /* Support for legacy operators where
188 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
189 if (f
->op
& AUDIT_NEGATE
)
190 f
->op
= AUDIT_NOT_EQUAL
;
193 else if (f
->op
== AUDIT_OPERATORS
) {
203 audit_free_rule(entry
);
207 /* Translate struct audit_rule_data to kernel's rule respresentation. */
208 static struct audit_entry
*audit_data_to_entry(struct audit_rule_data
*data
,
212 struct audit_entry
*entry
;
214 size_t remain
= datasz
- sizeof(struct audit_rule_data
);
218 entry
= audit_to_entry_common((struct audit_rule
*)data
);
223 entry
->rule
.vers_ops
= 2;
224 for (i
= 0; i
< data
->field_count
; i
++) {
225 struct audit_field
*f
= &entry
->rule
.fields
[i
];
228 if (!(data
->fieldflags
[i
] & AUDIT_OPERATORS
) ||
229 data
->fieldflags
[i
] & ~AUDIT_OPERATORS
)
232 f
->op
= data
->fieldflags
[i
] & AUDIT_OPERATORS
;
233 f
->type
= data
->fields
[i
];
234 f
->val
= data
->values
[i
];
243 str
= audit_unpack_string(&bufp
, &remain
, f
->val
);
246 entry
->rule
.buflen
+= f
->val
;
248 err
= selinux_audit_rule_init(f
->type
, f
->op
, str
,
250 /* Keep currently invalid fields around in case they
251 * become valid after a policy reload. */
252 if (err
== -EINVAL
) {
253 printk(KERN_WARNING
"audit rule for selinux "
254 "\'%s\' is invalid\n", str
);
270 audit_free_rule(entry
);
274 /* Pack a filter field's string representation into data block. */
275 static inline size_t audit_pack_string(void **bufp
, char *str
)
277 size_t len
= strlen(str
);
279 memcpy(*bufp
, str
, len
);
285 /* Translate kernel rule respresentation to struct audit_rule.
286 * Exists for backward compatibility with userspace. */
287 static struct audit_rule
*audit_krule_to_rule(struct audit_krule
*krule
)
289 struct audit_rule
*rule
;
292 rule
= kmalloc(sizeof(*rule
), GFP_KERNEL
);
294 return ERR_PTR(-ENOMEM
);
295 memset(rule
, 0, sizeof(*rule
));
297 rule
->flags
= krule
->flags
| krule
->listnr
;
298 rule
->action
= krule
->action
;
299 rule
->field_count
= krule
->field_count
;
300 for (i
= 0; i
< rule
->field_count
; i
++) {
301 rule
->values
[i
] = krule
->fields
[i
].val
;
302 rule
->fields
[i
] = krule
->fields
[i
].type
;
304 if (krule
->vers_ops
== 1) {
305 if (krule
->fields
[i
].op
& AUDIT_NOT_EQUAL
)
306 rule
->fields
[i
] |= AUDIT_NEGATE
;
308 rule
->fields
[i
] |= krule
->fields
[i
].op
;
311 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) rule
->mask
[i
] = krule
->mask
[i
];
316 /* Translate kernel rule respresentation to struct audit_rule_data. */
317 static struct audit_rule_data
*audit_krule_to_data(struct audit_krule
*krule
)
319 struct audit_rule_data
*data
;
323 data
= kmalloc(sizeof(*data
) + krule
->buflen
, GFP_KERNEL
);
325 return ERR_PTR(-ENOMEM
);
326 memset(data
, 0, sizeof(*data
));
328 data
->flags
= krule
->flags
| krule
->listnr
;
329 data
->action
= krule
->action
;
330 data
->field_count
= krule
->field_count
;
332 for (i
= 0; i
< data
->field_count
; i
++) {
333 struct audit_field
*f
= &krule
->fields
[i
];
335 data
->fields
[i
] = f
->type
;
336 data
->fieldflags
[i
] = f
->op
;
343 data
->buflen
+= data
->values
[i
] =
344 audit_pack_string(&bufp
, f
->se_str
);
347 data
->values
[i
] = f
->val
;
350 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) data
->mask
[i
] = krule
->mask
[i
];
355 /* Compare two rules in kernel format. Considered success if rules
357 static int audit_compare_rule(struct audit_krule
*a
, struct audit_krule
*b
)
361 if (a
->flags
!= b
->flags
||
362 a
->listnr
!= b
->listnr
||
363 a
->action
!= b
->action
||
364 a
->field_count
!= b
->field_count
)
367 for (i
= 0; i
< a
->field_count
; i
++) {
368 if (a
->fields
[i
].type
!= b
->fields
[i
].type
||
369 a
->fields
[i
].op
!= b
->fields
[i
].op
)
372 switch(a
->fields
[i
].type
) {
378 if (strcmp(a
->fields
[i
].se_str
, b
->fields
[i
].se_str
))
382 if (a
->fields
[i
].val
!= b
->fields
[i
].val
)
387 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
388 if (a
->mask
[i
] != b
->mask
[i
])
394 /* Duplicate selinux field information. The se_rule is opaque, so must be
396 static inline int audit_dupe_selinux_field(struct audit_field
*df
,
397 struct audit_field
*sf
)
402 /* our own copy of se_str */
403 se_str
= kstrdup(sf
->se_str
, GFP_KERNEL
);
404 if (unlikely(IS_ERR(se_str
)))
408 /* our own (refreshed) copy of se_rule */
409 ret
= selinux_audit_rule_init(df
->type
, df
->op
, df
->se_str
,
411 /* Keep currently invalid fields around in case they
412 * become valid after a policy reload. */
413 if (ret
== -EINVAL
) {
414 printk(KERN_WARNING
"audit rule for selinux \'%s\' is "
415 "invalid\n", df
->se_str
);
422 /* Duplicate an audit rule. This will be a deep copy with the exception
423 * of the watch - that pointer is carried over. The selinux specific fields
424 * will be updated in the copy. The point is to be able to replace the old
425 * rule with the new rule in the filterlist, then free the old rule. */
426 static struct audit_entry
*audit_dupe_rule(struct audit_krule
*old
)
428 u32 fcount
= old
->field_count
;
429 struct audit_entry
*entry
;
430 struct audit_krule
*new;
433 entry
= audit_init_entry(fcount
);
434 if (unlikely(!entry
))
435 return ERR_PTR(-ENOMEM
);
438 new->vers_ops
= old
->vers_ops
;
439 new->flags
= old
->flags
;
440 new->listnr
= old
->listnr
;
441 new->action
= old
->action
;
442 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
443 new->mask
[i
] = old
->mask
[i
];
444 new->buflen
= old
->buflen
;
445 new->field_count
= old
->field_count
;
446 memcpy(new->fields
, old
->fields
, sizeof(struct audit_field
) * fcount
);
448 /* deep copy this information, updating the se_rule fields, because
449 * the originals will all be freed when the old rule is freed. */
450 for (i
= 0; i
< fcount
; i
++) {
451 switch (new->fields
[i
].type
) {
457 err
= audit_dupe_selinux_field(&new->fields
[i
],
461 audit_free_rule(entry
);
469 /* Add rule to given filterlist if not a duplicate. Protected by
470 * audit_netlink_mutex. */
471 static inline int audit_add_rule(struct audit_entry
*entry
,
472 struct list_head
*list
)
474 struct audit_entry
*e
;
476 /* Do not use the _rcu iterator here, since this is the only
477 * addition routine. */
478 list_for_each_entry(e
, list
, list
) {
479 if (!audit_compare_rule(&entry
->rule
, &e
->rule
))
483 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
) {
484 list_add_rcu(&entry
->list
, list
);
486 list_add_tail_rcu(&entry
->list
, list
);
492 /* Remove an existing rule from filterlist. Protected by
493 * audit_netlink_mutex. */
494 static inline int audit_del_rule(struct audit_entry
*entry
,
495 struct list_head
*list
)
497 struct audit_entry
*e
;
499 /* Do not use the _rcu iterator here, since this is the only
500 * deletion routine. */
501 list_for_each_entry(e
, list
, list
) {
502 if (!audit_compare_rule(&entry
->rule
, &e
->rule
)) {
503 list_del_rcu(&e
->list
);
504 call_rcu(&e
->rcu
, audit_free_rule_rcu
);
508 return -ENOENT
; /* No matching rule */
511 /* List rules using struct audit_rule. Exists for backward
512 * compatibility with userspace. */
513 static int audit_list(void *_dest
)
517 struct audit_entry
*entry
;
524 mutex_lock(&audit_netlink_mutex
);
526 /* The *_rcu iterators not needed here because we are
527 always called with audit_netlink_mutex held. */
528 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
529 list_for_each_entry(entry
, &audit_filter_list
[i
], list
) {
530 struct audit_rule
*rule
;
532 rule
= audit_krule_to_rule(&entry
->rule
);
535 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
536 rule
, sizeof(*rule
));
540 audit_send_reply(pid
, seq
, AUDIT_LIST
, 1, 1, NULL
, 0);
542 mutex_unlock(&audit_netlink_mutex
);
546 /* List rules using struct audit_rule_data. */
547 static int audit_list_rules(void *_dest
)
551 struct audit_entry
*e
;
558 mutex_lock(&audit_netlink_mutex
);
560 /* The *_rcu iterators not needed here because we are
561 always called with audit_netlink_mutex held. */
562 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
563 list_for_each_entry(e
, &audit_filter_list
[i
], list
) {
564 struct audit_rule_data
*data
;
566 data
= audit_krule_to_data(&e
->rule
);
569 audit_send_reply(pid
, seq
, AUDIT_LIST_RULES
, 0, 1,
570 data
, sizeof(*data
));
574 audit_send_reply(pid
, seq
, AUDIT_LIST_RULES
, 1, 1, NULL
, 0);
576 mutex_unlock(&audit_netlink_mutex
);
581 * audit_receive_filter - apply all rules to the specified message type
582 * @type: audit message type
583 * @pid: target pid for netlink audit messages
584 * @uid: target uid for netlink audit messages
585 * @seq: netlink audit message sequence (serial) number
586 * @data: payload data
587 * @datasz: size of payload data
588 * @loginuid: loginuid of sender
589 * @sid: SE Linux Security ID of sender
591 int audit_receive_filter(int type
, int pid
, int uid
, int seq
, void *data
,
592 size_t datasz
, uid_t loginuid
, u32 sid
)
594 struct task_struct
*tsk
;
597 struct audit_entry
*entry
;
601 case AUDIT_LIST_RULES
:
602 /* We can't just spew out the rules here because we might fill
603 * the available socket buffer space and deadlock waiting for
604 * auditctl to read from it... which isn't ever going to
605 * happen if we're actually running in the context of auditctl
606 * trying to _send_ the stuff */
608 dest
= kmalloc(2 * sizeof(int), GFP_KERNEL
);
614 if (type
== AUDIT_LIST
)
615 tsk
= kthread_run(audit_list
, dest
, "audit_list");
617 tsk
= kthread_run(audit_list_rules
, dest
,
626 if (type
== AUDIT_ADD
)
627 entry
= audit_rule_to_entry(data
);
629 entry
= audit_data_to_entry(data
, datasz
);
631 return PTR_ERR(entry
);
633 err
= audit_add_rule(entry
,
634 &audit_filter_list
[entry
->rule
.listnr
]);
638 if (selinux_ctxid_to_string(sid
, &ctx
, &len
)) {
639 /* Maybe call audit_panic? */
640 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
641 "auid=%u ssid=%u add rule to list=%d res=%d",
642 loginuid
, sid
, entry
->rule
.listnr
, !err
);
644 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
645 "auid=%u subj=%s add rule to list=%d res=%d",
646 loginuid
, ctx
, entry
->rule
.listnr
, !err
);
649 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
650 "auid=%u add rule to list=%d res=%d",
651 loginuid
, entry
->rule
.listnr
, !err
);
654 audit_free_rule(entry
);
658 if (type
== AUDIT_DEL
)
659 entry
= audit_rule_to_entry(data
);
661 entry
= audit_data_to_entry(data
, datasz
);
663 return PTR_ERR(entry
);
665 err
= audit_del_rule(entry
,
666 &audit_filter_list
[entry
->rule
.listnr
]);
671 if (selinux_ctxid_to_string(sid
, &ctx
, &len
)) {
672 /* Maybe call audit_panic? */
673 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
674 "auid=%u ssid=%u remove rule from list=%d res=%d",
675 loginuid
, sid
, entry
->rule
.listnr
, !err
);
677 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
678 "auid=%u subj=%s remove rule from list=%d res=%d",
679 loginuid
, ctx
, entry
->rule
.listnr
, !err
);
682 audit_log(NULL
, GFP_KERNEL
, AUDIT_CONFIG_CHANGE
,
683 "auid=%u remove rule from list=%d res=%d",
684 loginuid
, entry
->rule
.listnr
, !err
);
686 audit_free_rule(entry
);
695 int audit_comparator(const u32 left
, const u32 op
, const u32 right
)
699 return (left
== right
);
700 case AUDIT_NOT_EQUAL
:
701 return (left
!= right
);
702 case AUDIT_LESS_THAN
:
703 return (left
< right
);
704 case AUDIT_LESS_THAN_OR_EQUAL
:
705 return (left
<= right
);
706 case AUDIT_GREATER_THAN
:
707 return (left
> right
);
708 case AUDIT_GREATER_THAN_OR_EQUAL
:
709 return (left
>= right
);
717 static int audit_filter_user_rules(struct netlink_skb_parms
*cb
,
718 struct audit_krule
*rule
,
719 enum audit_state
*state
)
723 for (i
= 0; i
< rule
->field_count
; i
++) {
724 struct audit_field
*f
= &rule
->fields
[i
];
729 result
= audit_comparator(cb
->creds
.pid
, f
->op
, f
->val
);
732 result
= audit_comparator(cb
->creds
.uid
, f
->op
, f
->val
);
735 result
= audit_comparator(cb
->creds
.gid
, f
->op
, f
->val
);
738 result
= audit_comparator(cb
->loginuid
, f
->op
, f
->val
);
745 switch (rule
->action
) {
746 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
747 case AUDIT_POSSIBLE
: *state
= AUDIT_BUILD_CONTEXT
; break;
748 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
753 int audit_filter_user(struct netlink_skb_parms
*cb
, int type
)
755 struct audit_entry
*e
;
756 enum audit_state state
;
760 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_USER
], list
) {
761 if (audit_filter_user_rules(cb
, &e
->rule
, &state
)) {
762 if (state
== AUDIT_DISABLED
)
769 return ret
; /* Audit by default */
772 int audit_filter_type(int type
)
774 struct audit_entry
*e
;
778 if (list_empty(&audit_filter_list
[AUDIT_FILTER_TYPE
]))
779 goto unlock_and_return
;
781 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TYPE
],
784 for (i
= 0; i
< e
->rule
.field_count
; i
++) {
785 struct audit_field
*f
= &e
->rule
.fields
[i
];
786 if (f
->type
== AUDIT_MSGTYPE
) {
787 result
= audit_comparator(type
, f
->op
, f
->val
);
793 goto unlock_and_return
;
800 /* Check to see if the rule contains any selinux fields. Returns 1 if there
801 are selinux fields specified in the rule, 0 otherwise. */
802 static inline int audit_rule_has_selinux(struct audit_krule
*rule
)
806 for (i
= 0; i
< rule
->field_count
; i
++) {
807 struct audit_field
*f
= &rule
->fields
[i
];
821 /* This function will re-initialize the se_rule field of all applicable rules.
822 * It will traverse the filter lists serarching for rules that contain selinux
823 * specific filter fields. When such a rule is found, it is copied, the
824 * selinux field is re-initialized, and the old rule is replaced with the
826 int selinux_audit_rule_update(void)
828 struct audit_entry
*entry
, *n
, *nentry
;
831 /* audit_netlink_mutex synchronizes the writers */
832 mutex_lock(&audit_netlink_mutex
);
834 for (i
= 0; i
< AUDIT_NR_FILTERS
; i
++) {
835 list_for_each_entry_safe(entry
, n
, &audit_filter_list
[i
], list
) {
836 if (!audit_rule_has_selinux(&entry
->rule
))
839 nentry
= audit_dupe_rule(&entry
->rule
);
840 if (unlikely(IS_ERR(nentry
))) {
841 /* save the first error encountered for the
844 err
= PTR_ERR(nentry
);
845 audit_panic("error updating selinux filters");
846 list_del_rcu(&entry
->list
);
848 list_replace_rcu(&entry
->list
, &nentry
->list
);
850 call_rcu(&entry
->rcu
, audit_free_rule_rcu
);
854 mutex_unlock(&audit_netlink_mutex
);