Skip to content

Commit 30969bc

Browse files
Peter Enderborgpcmoore
authored andcommitted
selinux: add basic filtering for audit trace events
This patch adds further attributes to the event. These attributes are helpful to understand the context of the message and can be used to filter the events. There are three common items. Source context, target context and tclass. There are also items from the outcome of operation performed. An event is similar to: <...>-1309 [002] .... 6346.691689: selinux_audited: requested=0x4000000 denied=0x4000000 audited=0x4000000 result=-13 scontext=system_u:system_r:cupsd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:bin_t:s0 tclass=file With systems where many denials are occurring, it is useful to apply a filter. The filtering is a set of logic that is inserted with the filter file. Example: echo "tclass==\"file\" " > events/avc/selinux_audited/filter This adds that we only get tclass=file. The trace can also have extra properties. Adding the user stack can be done with echo 1 > options/userstacktrace Now the output will be runcon-1365 [003] .... 6960.955530: selinux_audited: requested=0x4000000 denied=0x4000000 audited=0x4000000 result=-13 scontext=system_u:system_r:cupsd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:bin_t:s0 tclass=file runcon-1365 [003] .... 6960.955560: <user stack trace> => <00007f325b4ce45b> => <00005607093efa57> Signed-off-by: Peter Enderborg <peter.enderborg@sony.com> Reviewed-by: Thiébaud Weksteen <tweek@google.com> Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent dd81662 commit 30969bc

2 files changed

Lines changed: 41 additions & 23 deletions

File tree

include/trace/events/avc.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
3-
* Author: Thiébaud Weksteen <tweek@google.com>
3+
* Authors: Thiébaud Weksteen <tweek@google.com>
4+
* Peter Enderborg <Peter.Enderborg@sony.com>
45
*/
56
#undef TRACE_SYSTEM
67
#define TRACE_SYSTEM avc
@@ -12,23 +13,38 @@
1213

1314
TRACE_EVENT(selinux_audited,
1415

15-
TP_PROTO(struct selinux_audit_data *sad),
16+
TP_PROTO(struct selinux_audit_data *sad,
17+
char *scontext,
18+
char *tcontext,
19+
const char *tclass
20+
),
1621

17-
TP_ARGS(sad),
22+
TP_ARGS(sad, scontext, tcontext, tclass),
1823

1924
TP_STRUCT__entry(
20-
__field(unsigned int, tclass)
21-
__field(unsigned int, audited)
25+
__field(u32, requested)
26+
__field(u32, denied)
27+
__field(u32, audited)
28+
__field(int, result)
29+
__string(scontext, scontext)
30+
__string(tcontext, tcontext)
31+
__string(tclass, tclass)
2232
),
2333

2434
TP_fast_assign(
25-
__entry->tclass = sad->tclass;
26-
__entry->audited = sad->audited;
35+
__entry->requested = sad->requested;
36+
__entry->denied = sad->denied;
37+
__entry->audited = sad->audited;
38+
__entry->result = sad->result;
39+
__assign_str(tcontext, tcontext);
40+
__assign_str(scontext, scontext);
41+
__assign_str(tclass, tclass);
2742
),
2843

29-
TP_printk("tclass=%u audited=%x",
30-
__entry->tclass,
31-
__entry->audited)
44+
TP_printk("requested=0x%x denied=0x%x audited=0x%x result=%d scontext=%s tcontext=%s tclass=%s",
45+
__entry->requested, __entry->denied, __entry->audited, __entry->result,
46+
__get_str(scontext), __get_str(tcontext), __get_str(tclass)
47+
)
3248
);
3349

3450
#endif

security/selinux/avc.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -705,35 +705,37 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
705705
{
706706
struct common_audit_data *ad = a;
707707
struct selinux_audit_data *sad = ad->selinux_audit_data;
708-
char *scontext;
708+
char *scontext = NULL;
709+
char *tcontext = NULL;
710+
const char *tclass = NULL;
709711
u32 scontext_len;
712+
u32 tcontext_len;
710713
int rc;
711714

712-
trace_selinux_audited(sad);
713-
714715
rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
715716
&scontext_len);
716717
if (rc)
717718
audit_log_format(ab, " ssid=%d", sad->ssid);
718-
else {
719+
else
719720
audit_log_format(ab, " scontext=%s", scontext);
720-
kfree(scontext);
721-
}
722721

723-
rc = security_sid_to_context(sad->state, sad->tsid, &scontext,
724-
&scontext_len);
722+
rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
723+
&tcontext_len);
725724
if (rc)
726725
audit_log_format(ab, " tsid=%d", sad->tsid);
727-
else {
728-
audit_log_format(ab, " tcontext=%s", scontext);
729-
kfree(scontext);
730-
}
726+
else
727+
audit_log_format(ab, " tcontext=%s", tcontext);
731728

732-
audit_log_format(ab, " tclass=%s", secclass_map[sad->tclass-1].name);
729+
tclass = secclass_map[sad->tclass-1].name;
730+
audit_log_format(ab, " tclass=%s", tclass);
733731

734732
if (sad->denied)
735733
audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
736734

735+
trace_selinux_audited(sad, scontext, tcontext, tclass);
736+
kfree(tcontext);
737+
kfree(scontext);
738+
737739
/* in case of invalid context report also the actual context string */
738740
rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
739741
&scontext_len);

0 commit comments

Comments
 (0)