audit: show user land backtrace as part of audit context messages
PATCH 1/2 audit: show user land backtrace as part of audit context messages
From: Victor Kamensky <[email protected]>
To efficiently find out where SELinux AVC denial is comming from take backtrace of user land process and display it as type=UBACKTRACE message that comes as audit context for SELinux AVC and other audit messages.
By default UBACKTRACE context messages are off. Needs to be enabled through audit AUDIT_FEATURE_UBACKTRACE_CONTEXT feature.
Context UBACKTRACE message example:
type=UBACKTRACE msg=audit(1574205625.557:30): backtrace=libc-2.30.so+0xc99ab:dmesg.util-linux+0x1483:libc-2.30.so+0x1e35:dmesg.util-linux+0x1e5a
I.e because of ASLR instead of absolute user land addresses, name of executable or library captured and followed by offset from text segment vma. To decode backtrace entry: find executable or library symbol file, find its text segment vma and add offset to it; run 'addr2line -f -e symbol_file resulting_address'.
Note feature depends on PERF_EVENTS, from perf subsystem it uses perf_callchain_user function on architectures where it is implemented. And it has the same capturing restriction as 'perf -g': user land code must be compiled with -fno-omit-frame-pointer option, otherwise kernel is not capable walk user land stack frames.
--- include/uapi/linux/audit.h | 6 ++- init/Kconfig | 13 ++++++ kernel/audit.c | 3 +- kernel/auditsc.c | 93 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-)
include/uapi/linux/audit.h +4 -2
@@ -118,6 +118,7 @@#define AUDIT_TIME_ADJNTPVAL 1333/* NTP value adjustment */ #define AUDIT_BPF 1334/* BPF subsystem */ #define AUDIT_EVENT_LISTENER 1335/* Task joined multicast read socket */ +#define AUDIT_UBACKTRACE 1336/* User land backtrace */ #define AUDIT_AVC 1400/* SE Linux avc denial or grant */ #define AUDIT_SELINUX_ERR 1401/* Internal SE Linux Errors */
@@ -474,7 +475,7 @@ struct audit_status { };struct audit_features { -#define AUDIT_FEATURE_VERSION 1 +#define AUDIT_FEATURE_VERSION 2__u32 vers;__u32 mask;/* which bits we are dealing with */ __u32 features;/* which feature to enable/disable */
@@ -483,7 +484,8 @@ struct audit_features {#define AUDIT_FEATURE_ONLY_UNSET_LOGINUID 0#define AUDIT_FEATURE_LOGINUID_IMMUTABLE 1 -#define AUDIT_LAST_FEATURE AUDIT_FEATURE_LOGINUID_IMMUTABLE +#define AUDIT_FEATURE_UBACKTRACE_CONTEXT 2 +#define AUDIT_LAST_FEATURE AUDIT_FEATURE_UBACKTRACE_CONTEXT#define audit_feature_valid (x) ((x) >= 0 && (x) <= AUDIT_LAST_FEATURE)#define AUDIT_FEATURE_TO_MASK (x) (1 << ((x) & 31))/* mask for __u32 */
init/Kconfig +13 -0
@@ -433,6 +433,19 @@ config AUDITSYSCALL depends on AUDIT && HAVE_ARCH_AUDITSYSCALL select FSNOTIFY +config AUDIT_USER_BACKTRACE + bool "Enable user land backtrace in audit context messages" + def_bool n + depends on AUDITSYSCALL && PERF_EVENTS + help + Enable UBACKTRACE audit context messages, capturing backtrace of + user land process causing the message. + +config AUDIT_USER_BACKTRACE_SIZE + int "Maximum size of user land backtrace entries captured in UBACKTACE" + depends on AUDIT_USER_BACKTRACE + default 40 + source "kernel/irq/Kconfig" source "kernel/time/Kconfig" source "kernel/Kconfig.preempt"
kernel/audit.c +2 -1
@@ -165,9 +165,10 @@ static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION, .features = 0, .lock = 0,}; -static char *audit_feature_names[2 ] = { +static char *audit_feature_names[3 ] = {"only_unset_loginuid" ,"loginuid_immutable" , +"ubacktrace_context" , };/**
kernel/auditsc.c +93 -0
117 lines not on this page · raw message
Signature · 1 line
2.17.1
Paul Moore
maintainerQuoted 6 lines · Daniel Walker
From: Victor Kamensky <[email protected]>
To efficiently find out where SELinux AVC denial is comming from take backtrace of user land process and display it as type=UBACKTRACE message that comes as audit context for SELinux AVC and other audit messages ...
Have you tried the new perf tracepoint for SELinux AVC decisions that trigger an audit event? It's a new feature for v5.10 and looks to accomplish most of what you are looking for with this patch.
* https://www.paul-moore.com/blog/d/2020/12/linux_v510.html
Signature · 7 lines
paul moore www.paul-moore.com -- Linux-audit mailing list [email protected] https://www.redhat.com/mailman/listinfo/linux-audit
Quoted 8 lines · Paul Moore
Quoted 6 lines · Daniel Walker
From: Victor Kamensky <[email protected]>
To efficiently find out where SELinux AVC denial is comming from take backtrace of user land process and display it as type=UBACKTRACE message that comes as audit context for SELinux AVC and other audit messages ...
Quoted 5 lines
Have you tried the new perf tracepoint for SELinux AVC decisions that trigger an audit event? It's a new feature for v5.10 and looks to accomplish most of what you are looking for with this patch.
We haven't tried it, but I can look into it. We're not using v5.10 extensively yet.
Daniel
Paul Moore
maintainerQuoted 9 lines · Daniel Walker (danielwa)
Quoted 8 lines · Paul Moore
Quoted 6 lines · Daniel Walker
From: Victor Kamensky <[email protected]>
To efficiently find out where SELinux AVC denial is comming from take backtrace of user land process and display it as type=UBACKTRACE message that comes as audit context for SELinux AVC and other audit messages ...
Quoted 8 lines
Quoted 5 lines
Have you tried the new perf tracepoint for SELinux AVC decisions that trigger an audit event? It's a new feature for v5.10 and looks to accomplish most of what you are looking for with this patch.
We haven't tried it, but I can look into it. We're not using v5.10 extensively yet.
Let us know if that works for you, and if it doesn't, let us know what might be missing. I hate seeing the kernel grow multiple features which do the same thing.
Signature · 7 lines
paul moore www.paul-moore.com -- Linux-audit mailing list [email protected] https://www.redhat.com/mailman/listinfo/linux-audit
Quoted 15 lines · Paul Moore
Quoted 13 lines · Daniel Walker (danielwa)
Quoted 12 lines · Paul Moore
Quoted 6 lines · Daniel Walker
From: Victor Kamensky <[email protected]>
To efficiently find out where SELinux AVC denial is comming from take backtrace of user land process and display it as type=UBACKTRACE message that comes as audit context for SELinux AVC and other audit messages ...
Have you tried the new perf tracepoint for SELinux AVC decisions that trigger an audit event? It's a new feature for v5.10 and looks to accomplish most of what you are looking for with this patch.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
We haven't tried it, but I can look into it. We're not using v5.10 extensively yet.
Let us know if that works for you, and if it doesn't, let us know what might be missing. I hate seeing the kernel grow multiple features which do the same thing.
I agree - I played around with this new tracepoint recently and you can use it to achieve what you want quite easily:
# collect traces for denials (just interrupt/kill the sleep process when done) - will create a perf.data file you can analyze later perf record -a -e avc:selinux_audited -g --call-graph=dwarf sleep infinity # dump all collected backtraces from the perf.data file perf script
It's a bit complicated if you want to have it running in the background permanently as a service (you need to tell perf to dump the recorded data before you can read it), but perf already has some command-line options to help with that use case.
--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.
--
Linux-audit mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/linux-audit