filter: add filesystem filter with fstype
Subsystems (2)
PATCH v3 1/1 filter: add filesystem filter with fstype
Tracefs or debugfs were causing hundreds to thousands of PATH records to be associated with the init_module and finit_module SYSCALL records on a few modules when the following rule was in place for startup:
-a always,exit -F arch=x86_64 -S init_module -F key=mod-load
Add the new "filesystem" filter list anchored in __audit_inode_child() to filter out PATH records from uninteresting filesystem types, "fstype", keying on their kernel hexadecimal 4-octet magic identifier.
An example rule would look like:
-a never,filesystem -F fstype=0x74726163 -F key=ignore_tracefs -a never,filesystem -F fstype=0x64626720 -F key=ignore_debugfs
Note: "always,filesystem" will log the PATH record anyways and add latency.
See: https://github.com/linux-audit/audit-kernel/issues/8
See: https://github.com/linux-audit/audit-userspace/issues/15
Test case: https://github.com/linux-audit/audit-testsuite/issues/42
---
v3:
Update feature bitmap macros to reflect filter name change.
v2:
Change filter name from "path" to "filesystem". Rebase onto other patches accepted upstream.
docs/audit_add_rule_data.3 | 3 +++ lib/errormsg.h | 5 +++++ lib/fieldtab.h | 2 ++ lib/flagtab.h | 10 ++++++---- lib/libaudit.c | 26 ++++++++++++++++++++++++-- lib/libaudit.h | 10 ++++++++++ lib/private.h | 1 + src/auditctl-listing.c | 6 ++++-- src/auditctl.c | 16 ++++++++++++++-- 9 files changed, 69 insertions(+), 10 deletions(-)
docs/audit_add_rule_data.3 +3 -0
@@ -22,6 +22,9 @@ AUDIT_FILTER_EXIT - Apply rule at syscall exit. This is the main filter that is .TP \(bu AUDIT_FILTER_TYPE - Apply rule at audit_log_start. This is the exclude filter which discards any records that match. +.TP +\(bu +AUDIT_FILTER_FS - Apply rule when adding PATH auxiliary records to SYSCALL events. This is the filesystem filter. This is used to ignore PATH records that are not of interest. .LP .PP
lib/errormsg.h +5 -0
@@ -20,6 +20,7 @@* Authors: * Zhang Xiliang <[email protected]> * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ struct msg_tab {
@@ -66,6 +67,8 @@ struct msg_tab {#define EAU_FIELDNOFILTER 31#define EAU_FILTERMISSING 32#define EAU_COMPINCOMPAT 33 +#define EAU_FIELDUNAVAIL 34 +#define EAU_FILTERNOSUPPORT 35static const struct msg_tab err_msgtab[] = { { -EAU_OPMISSING ,2 ,"-F missing operation for" }, { -EAU_FIELDUNKNOWN ,2 ,"-F unknown field:" },
@@ -100,5 +103,7 @@ static const struct msg_tab err_msgtab[] = { { -EAU_FIELDNOFILTER, 1, "must be used with exclude, user, or exit filter" }, { -EAU_FILTERMISSING, 0, "filter is missing from rule" }, { -EAU_COMPINCOMPAT, 2, "-C incompatible comparison" }, + { -EAU_FIELDUNAVAIL, 1, "field is not valid for the filter" }, + { -EAU_FILTERNOSUPPORT, 1, "filter is not supported ty kernel" }, }; #endif
lib/fieldtab.h +2 -0
@@ -18,6 +18,7 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ _S (AUDIT_PID ,"pid" )
@@ -56,6 +57,7 @@ _S(AUDIT_WATCH, "path" ) _S(AUDIT_PERM, "perm" ) _S(AUDIT_DIR, "dir" ) _S(AUDIT_FILETYPE, "filetype" ) +_S(AUDIT_FSTYPE, "fstype" ) _S(AUDIT_OBJ_UID, "obj_uid" ) _S(AUDIT_OBJ_GID, "obj_gid" )_S (AUDIT_FIELD_COMPARE ,"field_compare" )
lib/flagtab.h +6 -4
@@ -18,8 +18,10 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ -_S(AUDIT_FILTER_TASK, "task" ) -_S(AUDIT_FILTER_EXIT, "exit" ) -_S(AUDIT_FILTER_USER, "user" ) -_S (AUDIT_FILTER_EXCLUDE ,"exclude" ) +_S(AUDIT_FILTER_TASK, "task" ) +_S(AUDIT_FILTER_EXIT, "exit" ) +_S(AUDIT_FILTER_USER, "user" ) +_S(AUDIT_FILTER_EXCLUDE, "exclude" ) +_S (AUDIT_FILTER_FS ,"filesystem" )
lib/libaudit.c +24 -2
@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h"
@@ -85,6 +86,7 @@ int _audit_permadded = 0;int _audit_archadded =0 ;int _audit_syscalladded =0 ;int _audit_exeadded =0 ; +int _audit_filterfsadded =0 ;unsigned int _audit_elf =0U ;static struct libaudit_conf config;
@@ -1466,6 +1468,23 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, } } +/* FS filter can be used only with FSTYPE field */ +if (flags ==AUDIT_FILTER_FS ) { +uint32_t features =audit_get_features (); +if ((features &AUDIT_FEATURE_BITMAP_FILTER_FS ) ==0 ) { +return -EAU_FILTERNOSUPPORT ; + }else { +switch (field) { +case AUDIT_FSTYPE : + _audit_filterfsadded =1 ; +case AUDIT_FILTERKEY : +break ; +default : +return -EAU_FIELDUNAVAIL ; + } + } + } + rule->fields[rule->field_count] = field; rule->fieldflags[rule->field_count] = op; switch (field)
@@ -1580,7 +1599,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, }if (field ==AUDIT_FILTERKEY && !(_audit_syscalladded || _audit_permadded || - _audit_exeadded)) + _audit_exeadded || + _audit_filterfsadded))return -EAU_KEYDEP ; vlen =strlen (v); if (field == AUDIT_FILTERKEY &&
@@ -1715,7 +1735,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,return -EAU_EXITONLY ;/* fallthrough */ default : -if (field ==AUDIT_INODE ) { +if (field ==AUDIT_INODE || field ==AUDIT_FSTYPE ) {if (!(op ==AUDIT_NOT_EQUAL || op ==AUDIT_EQUAL ))return -EAU_OPEQNOTEQ ;
@@ -1727,6 +1747,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair,if (!isdigit ((char )*(v)))return -EAU_FIELDVALNUM ; +if (field ==AUDIT_FSTYPE && flags !=AUDIT_FILTER_FS ) +return -EAU_FIELDUNAVAIL ;if (field ==AUDIT_INODE ) rule->values[rule->field_count] =strtoul (v,NULL ,0 );
lib/libaudit.h +10 -0
@@ -277,6 +277,9 @@ extern "C" {#define AUDIT_KEY_SEPARATOR 0x01/* These are used in filter control */ +#ifndef AUDIT_FILTER_FS +#define AUDIT_FILTER_FS 0x06/* FS record filter in __audit_inode_child */ +#endif #define AUDIT_FILTER_EXCLUDE AUDIT_FILTER_TYPE#define AUDIT_FILTER_MASK 0x07/* Mask to get actual filter */ #define AUDIT_FILTER_UNSET 0x80/* This value means filter is unset */
@@ -305,6 +308,9 @@ extern "C" {#ifndef AUDIT_FEATURE_BITMAP_LOST_RESET #define AUDIT_FEATURE_BITMAP_LOST_RESET 0x00000020#endif +#ifndef AUDIT_FEATURE_BITMAP_FILTER_FS +#define AUDIT_FEATURE_BITMAP_FILTER_FS 0x00000040 +#endif /* Defines for interfield comparison update */ #ifndef AUDIT_OBJ_UID
@@ -324,6 +330,10 @@ extern "C" {#define AUDIT_SESSIONID 25#endif +#ifndef AUDIT_FSTYPE +#define AUDIT_FSTYPE 26 +#endif +#ifndef AUDIT_COMPARE_UID_TO_OBJ_UID #define AUDIT_COMPARE_UID_TO_OBJ_UID 1#endif
lib/private.h +1 -0
@@ -139,6 +139,7 @@ extern int _audit_permadded;extern int _audit_archadded;extern int _audit_syscalladded;extern int _audit_exeadded; +extern int _audit_filterfsadded;extern unsigned int _audit_elf;#ifdef __cplusplus
src/auditctl-listing.c +4 -2
@@ -91,7 +91,8 @@ static int is_watch(const struct audit_rule_data *r)if (((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_USER ) && ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_TASK ) && - ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE )) { + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE ) && + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_FS )) {for (i =0 ; i < (AUDIT_BITMASK_SIZE -1 ); i++) {if (r->mask[i] != (uint32_t )~0 ) { all =0 ;
@@ -139,7 +140,8 @@ static int print_syscall(const struct audit_rule_data *r, unsigned int *sc)/* Rules on the following filters do not take a syscall */ if (((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_USER ) || ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_TASK ) || - ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE )) + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE ) || + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_FS ))return 0 ;/* See if its all or specific syscalls */
src/auditctl.c +14 -2
@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h"
@@ -74,6 +75,7 @@ static int reset_vars(void) _audit_permadded =0 ; _audit_archadded =0 ; _audit_exeadded =0 ; + _audit_filterfsadded =0 ; _audit_elf =0 ; add =AUDIT_FILTER_UNSET ; del =AUDIT_FILTER_UNSET ;
@@ -151,6 +153,8 @@ static int lookup_filter(const char *str, int *filter) *filter =AUDIT_FILTER_EXIT ; else if (strcmp(str, "user") == 0) *filter = AUDIT_FILTER_USER; + else if (strcmp(str, "filesystem") == 0) + *filter = AUDIT_FILTER_FS; else if (strcmp(str, "exclude") == 0) { *filter = AUDIT_FILTER_EXCLUDE; exclude =1 ;
@@ -760,6 +764,13 @@ static int setopt(int count, int lineno, char *vars[])audit_msg (LOG_ERR ,"Error: syscall auditing being added to user list" );return -1 ; + }else if (((add & (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS || (del & + (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS )) { +audit_msg (LOG_ERR , +"Error: syscall auditing being added to filesystem list" ); +return -1 ; }else if (exclude) {audit_msg (LOG_ERR ,"Error: syscall auditing cannot be put on exclude list" );
@@ -936,8 +947,9 @@ static int setopt(int count, int lineno, char *vars[])break ;case 'k' :if (!(_audit_syscalladded || _audit_permadded || - _audit_exeadded) || (add==AUDIT_FILTER_UNSET && - del==AUDIT_FILTER_UNSET )) { + _audit_exeadded || + _audit_filterfsadded) || + (add==AUDIT_FILTER_UNSET && del==AUDIT_FILTER_UNSET )) {audit_msg (LOG_ERR ,"key option needs a watch or syscall given prior to it" ); retval =-1 ;
Signature · 1 line
1.7.1
Steve Grubb
AppliedQuoted 13 lines · EDT Richard Guy Briggs
Tracefs or debugfs were causing hundreds to thousands of PATH records to be associated with the
init_moduleandfinit_moduleSYSCALL records on a few modules when the following rule was in place for startup:
-a always,exit -F arch=x86_64 -Sinit_module-F key=mod-loadAdd the new "filesystem" filter list anchored in
__audit_inode_child()to filter out PATH records from uninteresting filesystem types, "fstype", keying on their kernel hexadecimal 4-octet magic identifier.An example rule would look like:
-a never,filesystem -F fstype=0x74726163 -F key=ignore_tracefs -a never,filesystem -F fstype=0x64626720 -F key=ignore_debugfs
Quoted 7 lines
Note: "always,filesystem" will log the PATH record anyways and add latency.
See: https://github.com/linux-audit/audit-kernel/issues/8
See: https://github.com/linux-audit/audit-userspace/issues/15
Test case: https://github.com/linux-audit/audit-testsuite/issues/42Signed-off-by Richard Guy Briggs <[email protected]>
Applied. However, I don't like the idea of making people type in magic numbers. I'll start a fs name to magic translator and just add a few file systems.
-Steve
Quoted 178 lines
---
v3:
Update feature bitmap macros to reflect filter name change.
v2:
Change filter name from "path" to "filesystem". Rebase onto other patches accepted upstream.docs/audit_add_rule_data.3 | 3 +++ lib/errormsg.h | 5 +++++ lib/fieldtab.h | 2 ++ lib/flagtab.h | 10 ++++++---- lib/libaudit.c | 26 ++++++++++++++++++++++++-- lib/libaudit.h | 10 ++++++++++ lib/private.h | 1 + src/auditctl-listing.c | 6 ++++-- src/auditctl.c | 16 ++++++++++++++-- 9 files changed, 69 insertions(+), 10 deletions(-)docs/audit_add_rule_data.3 +0
-0@@ -22,6 +22,9 @@ AUDIT_FILTER_EXIT - Apply rule at syscall exit. This isthe main filter that is .TP
\(bu
AUDIT_FILTER_TYPE- Apply rule ataudit_log_start. This is the exclude
filter which discards any records that match. +.TP
+\(bu
+AUDIT_FILTER_FS - Apply rule when adding PATH auxiliary records to SYSCALL events. This is the filesystem filter. This is used to ignore PATH records that are not of interest. .LP.PP
lib/errormsg.h +5
-0@@ -20,6 +20,7 @@* Authors: * Zhang Xiliang <[email protected]> * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ struct msg_tab {@@ -66,6 +67,8 @@ struct msg_tab {#define EAU_FIELDNOFILTER 31#define EAU_FILTERMISSING 32#define EAU_COMPINCOMPAT 33 +#define EAU_FIELDUNAVAIL 34 +#define EAU_FILTERNOSUPPORT 35static const struct msg_tab err_msgtab[] = { { -EAU_OPMISSING ,2 ,"-F missing operation for" }, { -EAU_FIELDUNKNOWN ,2 ,"-F unknown field:" },@@ -100,5 +103,7 @@ static const struct msg_tab err_msgtab[] = { { -EAU_FIELDNOFILTER, 1, "must be used with exclude, user, or exit filter" }, { -EAU_FILTERMISSING, 0, "filter is missing from rule" }, { -EAU_COMPINCOMPAT, 2, "-C incompatible comparison" }, + { -EAU_FIELDUNAVAIL, 1, "field is not valid for the filter" }, + { -EAU_FILTERNOSUPPORT, 1, "filter is not supported ty kernel" }, }; #endiflib/fieldtab.h +2
-0@@ -18,6 +18,7 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ _S (AUDIT_PID ,"pid" )@@ -56,6 +57,7 @@ _S(AUDIT_WATCH, "path" ) _S(AUDIT_PERM, "perm" ) _S(AUDIT_DIR, "dir" ) _S(AUDIT_FILETYPE, "filetype" ) +_S(AUDIT_FSTYPE, "fstype" ) _S(AUDIT_OBJ_UID, "obj_uid" ) _S(AUDIT_OBJ_GID, "obj_gid" )_S (AUDIT_FIELD_COMPARE ,"field_compare" )lib/flagtab.h +6
-4@@ -18,8 +18,10 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ -_S(AUDIT_FILTER_TASK, "task" ) -_S(AUDIT_FILTER_EXIT, "exit" ) -_S(AUDIT_FILTER_USER, "user" ) -_S (AUDIT_FILTER_EXCLUDE ,"exclude" ) +_S(AUDIT_FILTER_TASK, "task" ) +_S(AUDIT_FILTER_EXIT, "exit" ) +_S(AUDIT_FILTER_USER, "user" ) +_S(AUDIT_FILTER_EXCLUDE, "exclude" ) +_S (AUDIT_FILTER_FS ,"filesystem" )lib/libaudit.c +24
-2@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h" @@ -85,6 +86,7 @@ int _audit_permadded = 0;int _audit_archadded =0 ;int _audit_syscalladded =0 ;int _audit_exeadded =0 ; +int _audit_filterfsadded =0 ;unsigned int _audit_elf =0U ;static struct libaudit_conf config;@@ -1466,6 +1468,23 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, } } +/* FS filter can be used only with FSTYPE field */ +if (flags ==AUDIT_FILTER_FS ) { +uint32_t features =audit_get_features (); +if ((features &AUDIT_FEATURE_BITMAP_FILTER_FS ) ==0 ) { +return -EAU_FILTERNOSUPPORT ; + }else { +switch (field) { +case AUDIT_FSTYPE : + _audit_filterfsadded =1 ; +case AUDIT_FILTERKEY : +break ; +default : +return -EAU_FIELDUNAVAIL ; + } + } + } + rule->fields[rule->field_count] = field; rule->fieldflags[rule->field_count] = op; switch (field)@@ -1580,7 +1599,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, }if (field ==AUDIT_FILTERKEY && !(_audit_syscalladded || _audit_permadded || - _audit_exeadded)) + _audit_exeadded || + _audit_filterfsadded))return -EAU_KEYDEP ; vlen =strlen (v); if (field == AUDIT_FILTERKEY &&@@ -1715,7 +1735,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, return -EAU_EXITONLY;/* fallthrough */ default : -if (field ==AUDIT_INODE ) { +if (field ==AUDIT_INODE || field ==AUDIT_FSTYPE ) {if (!(op ==AUDIT_NOT_EQUAL || op ==AUDIT_EQUAL ))return -EAU_OPEQNOTEQ ;@@ -1727,6 +1747,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, if (!isdigit((char)*(v)))return -EAU_FIELDVALNUM ; +if (field ==AUDIT_FSTYPE && flags !=AUDIT_FILTER_FS ) +return -EAU_FIELDUNAVAIL ;if (field ==AUDIT_INODE ) rule->values[rule->field_count] =strtoul (v,NULL ,0 );lib/libaudit.h +0
-0
lib/libaudit.h extern "C" { · line 281
Quoted 5 lines
lib/libaudit.h +2
-0@@ -277,6 +277,9 @@ extern "C" {#define AUDIT_KEY_SEPARATOR 0x01/* These are used in filter control */ +#ifndef AUDIT_FILTER_FS +#define AUDIT_FILTER_FS 0x06/* FS record filter in __audit_inode_child
*/
Quoted 117 lines
lib/libaudit.h +8
-0+#endif #define AUDIT_FILTER_EXCLUDE AUDIT_FILTER_TYPE#define AUDIT_FILTER_MASK 0x07/* Mask to get actual filter */ #define AUDIT_FILTER_UNSET 0x80/* This value means filter is unset */ @@ -305,6 +308,9 @@ extern "C" {#ifndef AUDIT_FEATURE_BITMAP_LOST_RESET #define AUDIT_FEATURE_BITMAP_LOST_RESET 0x00000020#endif +#ifndef AUDIT_FEATURE_BITMAP_FILTER_FS +#define AUDIT_FEATURE_BITMAP_FILTER_FS 0x00000040 +#endif /* Defines for interfield comparison update */ #ifndef AUDIT_OBJ_UID @@ -324,6 +330,10 @@ extern "C" {#define AUDIT_SESSIONID 25#endif +#ifndef AUDIT_FSTYPE +#define AUDIT_FSTYPE 26 +#endif +#ifndef AUDIT_COMPARE_UID_TO_OBJ_UID #define AUDIT_COMPARE_UID_TO_OBJ_UID 1#endif lib/private.h +1
-0@@ -139,6 +139,7 @@ extern int _audit_permadded;extern int _audit_archadded;extern int _audit_syscalladded;extern int _audit_exeadded; +extern int _audit_filterfsadded;extern unsigned int _audit_elf;#ifdef __cplusplussrc/auditctl-listing.c +2
-1@@ -91,7 +91,8 @@ static int is_watch(const struct audit_rule_data *r)if (((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_USER ) && ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_TASK ) && - ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE )) { + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE ) && + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_FS )) {for (i =0 ; i < (AUDIT_BITMASK_SIZE -1 ); i++) {if (r->mask[i] != (uint32_t )~0 ) { all =0 ;@@ -139,7 +140,8 @@ static int print_syscall(const struct audit_rule_data *r, unsigned int *sc) /* Rules on the following filters do not take asyscall */
src/auditctl-listing.c +2
-1if (((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_USER ) || ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_TASK ) || - ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE )) + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE ) || + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_FS ))return 0 ;/* See if its all or specific syscalls */ src/auditctl.c +14
-2@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h" @@ -74,6 +75,7 @@ static int reset_vars(void) _audit_permadded =0 ; _audit_archadded =0 ; _audit_exeadded =0 ; + _audit_filterfsadded =0 ; _audit_elf =0 ; add =AUDIT_FILTER_UNSET ; del =AUDIT_FILTER_UNSET ;@@ -151,6 +153,8 @@ static int lookup_filter(const char *str, int *filter) *filter =AUDIT_FILTER_EXIT ; else if (strcmp(str, "user") == 0) *filter = AUDIT_FILTER_USER; + else if (strcmp(str, "filesystem") == 0) + *filter = AUDIT_FILTER_FS; else if (strcmp(str, "exclude") == 0) { *filter = AUDIT_FILTER_EXCLUDE; exclude =1 ;@@ -760,6 +764,13 @@ static int setopt(int count, int lineno, char *vars[])audit_msg (LOG_ERR ,"Error: syscall auditing being added to user list" );return -1 ; + }else if (((add & (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS || (del & + (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS )) { +audit_msg (LOG_ERR , +"Error: syscall auditing being added to filesystem list" ); +return -1 ; }else if (exclude) {audit_msg (LOG_ERR ,"Error: syscall auditing cannot be put on exclude list" );@@ -936,8 +947,9 @@ static int setopt(int count, int lineno, char *vars[])break ;case 'k' :if (!(_audit_syscalladded || _audit_permadded || - _audit_exeadded) || (add==AUDIT_FILTER_UNSET && - del==AUDIT_FILTER_UNSET )) { + _audit_exeadded || + _audit_filterfsadded) || + (add==AUDIT_FILTER_UNSET && del==AUDIT_FILTER_UNSET )) {audit_msg (LOG_ERR ,"key option needs a watch or syscall given prior to it" ); retval =-1 ;
Quoted 20 lines · Steve Grubb
Quoted 19 lines · EDT Richard Guy Briggs
Tracefs or debugfs were causing hundreds to thousands of PATH records to be associated with the
init_moduleandfinit_moduleSYSCALL records on a few modules when the following rule was in place for startup:
-a always,exit -F arch=x86_64 -Sinit_module-F key=mod-loadAdd the new "filesystem" filter list anchored in
__audit_inode_child()to filter out PATH records from uninteresting filesystem types, "fstype", keying on their kernel hexadecimal 4-octet magic identifier.An example rule would look like:
-a never,filesystem -F fstype=0x74726163 -F key=ignore_tracefs -a never,filesystem -F fstype=0x64626720 -F key=ignore_debugfsNote: "always,filesystem" will log the PATH record anyways and add latency.
See: https://github.com/linux-audit/audit-kernel/issues/8
See: https://github.com/linux-audit/audit-userspace/issues/15
Test case: https://github.com/linux-audit/audit-testsuite/issues/42
Quoted 5 lines
Quoted 1 line
Signed-off-by Richard Guy Briggs <[email protected]>Applied. However, I don't like the idea of making people type in magic numbers. I'll start a fs name to magic translator and just add a few file systems.
I was originally hoping to accomodate iether magic or text name in the kernel api, but it seems there is no guarantee that a text name is unique. Thank you for adding this to auditctl. I assume on set, auditctl will accept either, and on list, it will leave it as raw, or will it translate?
Quoted 295 lines
-Steve
Quoted 178 lines
---
v3:
Update feature bitmap macros to reflect filter name change.
v2:
Change filter name from "path" to "filesystem". Rebase onto other patches accepted upstream.docs/audit_add_rule_data.3 | 3 +++ lib/errormsg.h | 5 +++++ lib/fieldtab.h | 2 ++ lib/flagtab.h | 10 ++++++---- lib/libaudit.c | 26 ++++++++++++++++++++++++-- lib/libaudit.h | 10 ++++++++++ lib/private.h | 1 + src/auditctl-listing.c | 6 ++++-- src/auditctl.c | 16 ++++++++++++++-- 9 files changed, 69 insertions(+), 10 deletions(-)docs/audit_add_rule_data.3 +0
-0@@ -22,6 +22,9 @@ AUDIT_FILTER_EXIT - Apply rule at syscall exit. This isthe main filter that is .TP
\(bu
AUDIT_FILTER_TYPE- Apply rule ataudit_log_start. This is the exclude
filter which discards any records that match. +.TP
+\(bu
+AUDIT_FILTER_FS - Apply rule when adding PATH auxiliary records to SYSCALL events. This is the filesystem filter. This is used to ignore PATH records that are not of interest. .LP.PP
lib/errormsg.h +5
-0@@ -20,6 +20,7 @@* Authors: * Zhang Xiliang <[email protected]> * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ struct msg_tab {@@ -66,6 +67,8 @@ struct msg_tab {#define EAU_FIELDNOFILTER 31#define EAU_FILTERMISSING 32#define EAU_COMPINCOMPAT 33 +#define EAU_FIELDUNAVAIL 34 +#define EAU_FILTERNOSUPPORT 35static const struct msg_tab err_msgtab[] = { { -EAU_OPMISSING ,2 ,"-F missing operation for" }, { -EAU_FIELDUNKNOWN ,2 ,"-F unknown field:" },@@ -100,5 +103,7 @@ static const struct msg_tab err_msgtab[] = { { -EAU_FIELDNOFILTER, 1, "must be used with exclude, user, or exit filter" }, { -EAU_FILTERMISSING, 0, "filter is missing from rule" }, { -EAU_COMPINCOMPAT, 2, "-C incompatible comparison" }, + { -EAU_FIELDUNAVAIL, 1, "field is not valid for the filter" }, + { -EAU_FILTERNOSUPPORT, 1, "filter is not supported ty kernel" }, }; #endiflib/fieldtab.h +2
-0@@ -18,6 +18,7 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ _S (AUDIT_PID ,"pid" )@@ -56,6 +57,7 @@ _S(AUDIT_WATCH, "path" ) _S(AUDIT_PERM, "perm" ) _S(AUDIT_DIR, "dir" ) _S(AUDIT_FILETYPE, "filetype" ) +_S(AUDIT_FSTYPE, "fstype" ) _S(AUDIT_OBJ_UID, "obj_uid" ) _S(AUDIT_OBJ_GID, "obj_gid" )_S (AUDIT_FIELD_COMPARE ,"field_compare" )lib/flagtab.h +6
-4@@ -18,8 +18,10 @@* * Authors: * Steve Grubb <[email protected]> +* Richard Guy Briggs <[email protected]> */ -_S(AUDIT_FILTER_TASK, "task" ) -_S(AUDIT_FILTER_EXIT, "exit" ) -_S(AUDIT_FILTER_USER, "user" ) -_S (AUDIT_FILTER_EXCLUDE ,"exclude" ) +_S(AUDIT_FILTER_TASK, "task" ) +_S(AUDIT_FILTER_EXIT, "exit" ) +_S(AUDIT_FILTER_USER, "user" ) +_S(AUDIT_FILTER_EXCLUDE, "exclude" ) +_S (AUDIT_FILTER_FS ,"filesystem" )lib/libaudit.c +24
-2@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h" @@ -85,6 +86,7 @@ int _audit_permadded = 0;int _audit_archadded =0 ;int _audit_syscalladded =0 ;int _audit_exeadded =0 ; +int _audit_filterfsadded =0 ;unsigned int _audit_elf =0U ;static struct libaudit_conf config;@@ -1466,6 +1468,23 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, } } +/* FS filter can be used only with FSTYPE field */ +if (flags ==AUDIT_FILTER_FS ) { +uint32_t features =audit_get_features (); +if ((features &AUDIT_FEATURE_BITMAP_FILTER_FS ) ==0 ) { +return -EAU_FILTERNOSUPPORT ; + }else { +switch (field) { +case AUDIT_FSTYPE : + _audit_filterfsadded =1 ; +case AUDIT_FILTERKEY : +break ; +default : +return -EAU_FIELDUNAVAIL ; + } + } + } + rule->fields[rule->field_count] = field; rule->fieldflags[rule->field_count] = op; switch (field)@@ -1580,7 +1599,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, }if (field ==AUDIT_FILTERKEY && !(_audit_syscalladded || _audit_permadded || - _audit_exeadded)) + _audit_exeadded || + _audit_filterfsadded))return -EAU_KEYDEP ; vlen =strlen (v); if (field == AUDIT_FILTERKEY &&@@ -1715,7 +1735,7 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, return -EAU_EXITONLY;/* fallthrough */ default : -if (field ==AUDIT_INODE ) { +if (field ==AUDIT_INODE || field ==AUDIT_FSTYPE ) {if (!(op ==AUDIT_NOT_EQUAL || op ==AUDIT_EQUAL ))return -EAU_OPEQNOTEQ ;@@ -1727,6 +1747,8 @@ int audit_rule_fieldpair_data(struct audit_rule_data **rulep, const char *pair, if (!isdigit((char)*(v)))return -EAU_FIELDVALNUM ; +if (field ==AUDIT_FSTYPE && flags !=AUDIT_FILTER_FS ) +return -EAU_FIELDUNAVAIL ;if (field ==AUDIT_INODE ) rule->values[rule->field_count] =strtoul (v,NULL ,0 );lib/libaudit.h +0
-0lib/libaudit.h extern "C" { · line 281
Quoted 5 lines
lib/libaudit.h +2
-0@@ -277,6 +277,9 @@ extern "C" {#define AUDIT_KEY_SEPARATOR 0x01/* These are used in filter control */ +#ifndef AUDIT_FILTER_FS +#define AUDIT_FILTER_FS 0x06/* FS record filter in __audit_inode_child */
Quoted 109 lines
lib/libaudit.h +8
-0+#endif #define AUDIT_FILTER_EXCLUDE AUDIT_FILTER_TYPE#define AUDIT_FILTER_MASK 0x07/* Mask to get actual filter */ #define AUDIT_FILTER_UNSET 0x80/* This value means filter is unset */ @@ -305,6 +308,9 @@ extern "C" {#ifndef AUDIT_FEATURE_BITMAP_LOST_RESET #define AUDIT_FEATURE_BITMAP_LOST_RESET 0x00000020#endif +#ifndef AUDIT_FEATURE_BITMAP_FILTER_FS +#define AUDIT_FEATURE_BITMAP_FILTER_FS 0x00000040 +#endif /* Defines for interfield comparison update */ #ifndef AUDIT_OBJ_UID @@ -324,6 +330,10 @@ extern "C" {#define AUDIT_SESSIONID 25#endif +#ifndef AUDIT_FSTYPE +#define AUDIT_FSTYPE 26 +#endif +#ifndef AUDIT_COMPARE_UID_TO_OBJ_UID #define AUDIT_COMPARE_UID_TO_OBJ_UID 1#endif lib/private.h +1
-0@@ -139,6 +139,7 @@ extern int _audit_permadded;extern int _audit_archadded;extern int _audit_syscalladded;extern int _audit_exeadded; +extern int _audit_filterfsadded;extern unsigned int _audit_elf;#ifdef __cplusplussrc/auditctl-listing.c +2
-1@@ -91,7 +91,8 @@ static int is_watch(const struct audit_rule_data *r)if (((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_USER ) && ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_TASK ) && - ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE )) { + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_EXCLUDE ) && + ((r->flags &AUDIT_FILTER_MASK ) !=AUDIT_FILTER_FS )) {for (i =0 ; i < (AUDIT_BITMASK_SIZE -1 ); i++) {if (r->mask[i] != (uint32_t )~0 ) { all =0 ;@@ -139,7 +140,8 @@ static int print_syscall(const struct audit_rule_data *r, unsigned int *sc) /* Rules on the following filters do not take asyscall */
src/auditctl-listing.c +2
-1if (((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_USER ) || ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_TASK ) || - ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE )) + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_EXCLUDE ) || + ((r->flags &AUDIT_FILTER_MASK ) ==AUDIT_FILTER_FS ))return 0 ;/* See if its all or specific syscalls */ src/auditctl.c +11
-0@@ -19,6 +19,7 @@* Authors: * Steve Grubb <[email protected]> * Rickard E. (Rik) Faith <[email protected]> +* Richard Guy Briggs <[email protected]> */ #include "config.h" @@ -74,6 +75,7 @@ static int reset_vars(void) _audit_permadded =0 ; _audit_archadded =0 ; _audit_exeadded =0 ; + _audit_filterfsadded =0 ; _audit_elf =0 ; add =AUDIT_FILTER_UNSET ; del =AUDIT_FILTER_UNSET ;@@ -151,6 +153,8 @@ static int lookup_filter(const char *str, int *filter) *filter =AUDIT_FILTER_EXIT ; else if (strcmp(str, "user") == 0) *filter = AUDIT_FILTER_USER; + else if (strcmp(str, "filesystem") == 0) + *filter = AUDIT_FILTER_FS; else if (strcmp(str, "exclude") == 0) { *filter = AUDIT_FILTER_EXCLUDE; exclude =1 ;@@ -760,6 +764,13 @@ static int setopt(int count, int lineno, char *vars[])audit_msg (LOG_ERR ,"Error: syscall auditing being added to user list" );return -1 ; + }else if (((add & (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS || (del & + (AUDIT_FILTER_MASK |AUDIT_FILTER_UNSET )) == +AUDIT_FILTER_FS )) { +audit_msg (LOG_ERR , +"Error: syscall auditing being added to filesystem list" ); +return -1 ; }else if (exclude) {audit_msg (LOG_ERR ,"Error: syscall auditing cannot be put on exclude list" );@@ -936,8 +947,9 @@ static int setopt(int count, int lineno, char *vars[])break ;case 'k' : if (!(_audit_syscalladded || _audit_permadded ||
Quoted 10 lines
src/auditctl.c static int setopt(int count, int lineno, char *vars[]) · line 955
Quoted 8 lines
src/auditctl.c +3
-2- _audit_exeadded) || (add==AUDIT_FILTER_UNSET && - del==AUDIT_FILTER_UNSET)) { + _audit_exeadded || + _audit_filterfsadded) || + (add==AUDIT_FILTER_UNSET && del==AUDIT_FILTER_UNSET)) {audit_msg (LOG_ERR ,"key option needs a watch or syscall given prior to it" ); retval =-1 ;
- RGB
--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545