Skip to contentSkip to conversation

selinux: add a new warn_on_audited debug flag to selinuxfs

PATCH In review Ondrej Mosnacek 2 participants 3 messages Aug 8, 2022 – Aug 23, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: add a new warn_on_audited debug flag to selinuxfs

When debugging SELinux denials, it is often helpful to know which part of kernel code triggered the denial. Thus, this patch adds a new /sys/fs/selinux/warn_on_audited flag that, when set to 1, will cause any audited AVC event to log a WARNING to the kernel console, which naturally comes with a kernel stack trace.

While the same can be achieved via the "avc:selinux_audited" kernel tracepoint and the perf tool, that approach has several practical disadvantages:
1. It requires perf to be installed on the machine.
2. It requires kernel debug symbols to be available when decoding the stack trace.
3. It requires a perf process to be running in the background.
4. The stack traces can only be obtained at the end, after the perf process is terminated, not live during the capture. (Though this may be solved by writing a custom tool on top of libtraceevent.)

Thus, providing a simple native knob for this in selinuxfs is still valuable.

The warn_on_audited flag is always set to 0 on boot and is expected to be set to 1 only temporarily by system administrator in order to debug SELinux denials. It is not intended to be used on production systems.

---
 security/selinux/avc.c              |  6 +++
 security/selinux/ima.c              | 11 +++++-
 security/selinux/include/security.h | 11 ++++++
 security/selinux/selinuxfs.c        | 61 +++++++++++++++++++++++++++++
 4 files changed, 88 insertions(+), 1 deletion(-)
security/selinux/avc.c +6 -0
index 9a43af0ebd7d..0e615c9e8e79 100644
@@ -736,6 +736,12 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
 		audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
 
 	trace_selinux_audited(sad, scontext, tcontext, tclass);
+
+	WARN(warn_on_audited_get(sad->state),
+	     "SELinux:  AV: requested=0x%x denied=0x%x audited=0x%x result=%d scontext=%s tcontext=%s tclass=%s",
+	     sad->requested, sad->denied, sad->audited, sad->result,
+	     scontext, tcontext, tclass);
+
 	kfree(tcontext);
 	kfree(scontext);
 
security/selinux/ima.c +10 -1
index a915b89d55b0..506e880040f5 100644
@@ -26,7 +26,10 @@ static char *selinux_ima_collect_state(struct selinux_state *state)
 	char *buf;
 	int buf_len, len, i, rc;
 
-	buf_len = strlen("initialized=0;enforcing=0;checkreqprot=0;") + 1;
+	buf_len = strlen("initialized=0;"
+			 "enforcing=0;"
+			 "checkreqprot=0;"
+			 "warn_on_audited=0;") + 1;
 
 	len = strlen(on);
 	for (i = 0; i < __POLICYDB_CAP_MAX; i++)
@@ -54,6 +57,12 @@ static char *selinux_ima_collect_state(struct selinux_state *state)
 	rc = strlcat(buf, checkreqprot_get(state) ? on : off, buf_len);
 	WARN_ON(rc >= buf_len);
 
+	rc = strlcat(buf, "warn_on_audited", buf_len);
+	WARN_ON(rc >= buf_len);
+
+	rc = strlcat(buf, warn_on_audited_get(state) ? on : off, buf_len);
+	WARN_ON(rc >= buf_len);
+
 	for (i = 0; i < __POLICYDB_CAP_MAX; i++) {
 		rc = strlcat(buf, selinux_policycap_names[i], buf_len);
 		WARN_ON(rc >= buf_len);
security/selinux/include/security.h +11 -0
index 393aff41d3ef..bb1f0507edb6 100644
@@ -97,6 +97,7 @@ struct selinux_state {
 	bool enforcing;
 #endif
 	bool checkreqprot;
+	bool warn_on_audited;
 	bool initialized;
 	bool policycap[__POLICYDB_CAP_MAX];
 
@@ -157,6 +158,16 @@ static inline void checkreqprot_set(struct selinux_state *state, bool value)
 	WRITE_ONCE(state->checkreqprot, value);
 }
 
+static inline bool warn_on_audited_get(const struct selinux_state *state)
+{
+	return READ_ONCE(state->warn_on_audited);
+}
+
+static inline void warn_on_audited_set(struct selinux_state *state, bool value)
+{
+	WRITE_ONCE(state->warn_on_audited, value);
+}
+
 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
 static inline bool selinux_disabled(struct selinux_state *state)
 {
security/selinux/selinuxfs.c +61 -0

79 lines not on this page · raw message

Signature · 1 line
2.37.1
maintainer
#1 lore raw
Quoted 25 lines · Ondrej Mosnacek

When debugging SELinux denials, it is often helpful to know which part of kernel code triggered the denial. Thus, this patch adds a new /sys/fs/selinux/warn_on_audited flag that, when set to 1, will cause any audited AVC event to log a WARNING to the kernel console, which naturally comes with a kernel stack trace.

While the same can be achieved via the "avc:selinux_audited" kernel tracepoint and the perf tool, that approach has several practical disadvantages:
1. It requires perf to be installed on the machine.
2. It requires kernel debug symbols to be available when decoding the stack trace.
3. It requires a perf process to be running in the background.
4. The stack traces can only be obtained at the end, after the perf process is terminated, not live during the capture. (Though this may be solved by writing a custom tool on top of libtraceevent.)

Thus, providing a simple native knob for this in selinuxfs is still valuable.

The warn_on_audited flag is always set to 0 on boot and is expected to be set to 1 only temporarily by system administrator in order to debug SELinux denials. It is not intended to be used on production systems.

Quoted 7 lines
---
 security/selinux/avc.c              |  6 +++
 security/selinux/ima.c              | 11 +++++-
 security/selinux/include/security.h | 11 ++++++
 security/selinux/selinuxfs.c        | 61 +++++++++++++++++++++++++++++
 4 files changed, 88 insertions(+), 1 deletion(-)

I'm sorry, but I'm not going to merge this. At least not now.

In general I don't like using WARN/WARN_ON/etc. for this; I believe their use should be limited for rather serious kernel issues and not as a developer's debugging tool. I also don't like duplicating the tracepoint functionality. I understand there are hurdles to using perf on a system, but I would much rather see work go into fixing that than duplicating its functionality

Signature · 1 line
paul-moore.com
#2 lore raw
Quoted 34 lines · Paul Moore
Quoted 25 lines · Ondrej Mosnacek

When debugging SELinux denials, it is often helpful to know which part of kernel code triggered the denial. Thus, this patch adds a new /sys/fs/selinux/warn_on_audited flag that, when set to 1, will cause any audited AVC event to log a WARNING to the kernel console, which naturally comes with a kernel stack trace.

While the same can be achieved via the "avc:selinux_audited" kernel tracepoint and the perf tool, that approach has several practical disadvantages:
1. It requires perf to be installed on the machine.
2. It requires kernel debug symbols to be available when decoding the stack trace.
3. It requires a perf process to be running in the background.
4. The stack traces can only be obtained at the end, after the perf process is terminated, not live during the capture. (Though this may be solved by writing a custom tool on top of libtraceevent.)

Thus, providing a simple native knob for this in selinuxfs is still valuable.

The warn_on_audited flag is always set to 0 on boot and is expected to be set to 1 only temporarily by system administrator in order to debug SELinux denials. It is not intended to be used on production systems.

Quoted 7 lines
---
 security/selinux/avc.c              |  6 +++
 security/selinux/ima.c              | 11 +++++-
 security/selinux/include/security.h | 11 ++++++
 security/selinux/selinuxfs.c        | 61 +++++++++++++++++++++++++++++
 4 files changed, 88 insertions(+), 1 deletion(-)
Quoted 8 lines

I'm sorry, but I'm not going to merge this. At least not now.

In general I don't like using WARN/WARN_ON/etc. for this; I believe their use should be limited for rather serious kernel issues and not as a developer's debugging tool. I also don't like duplicating the tracepoint functionality. I understand there are hurdles to using perf on a system, but I would much rather see work go into fixing that than duplicating its functionality

OK, I'm not going to argue against that, but I had to at least try :)

--
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.