Skip to contentSkip to conversation

libselinux: selinux_check_access: fail cleanly if avc_open() failed

PATCH In review Stephen Smalley 2 participants 3 messages Aug 13 – Aug 17 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: selinux_check_access: fail cleanly if avc_open() failed

avc_init_once() calls avc_open() but discards a failure result, and a subsequent selinux_check_access() proceeds to avc_context_to_sid() which now hits assert(avc_running) and aborts the caller.

Latch the initializer outcome and have selinux_check_access() return -1/EINVAL when the AVC never came up, so callers see an error rather than a SIGABRT.

---
 libselinux/src/checkAccess.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
libselinux/src/checkAccess.c +8 -4
index d8f485e9..3cdd2413 100644
@@ -9,14 +9,13 @@
 
 static pthread_once_t once = PTHREAD_ONCE_INIT;
 static int selinux_enabled;
+static int avc_ok;
 
 static void avc_init_once(void)
 {
 	selinux_enabled = is_selinux_enabled();
-	if (selinux_enabled == 1) {
-		if (avc_open(NULL, 0))
-			return;
-	}
+	if (selinux_enabled == 1 && avc_open(NULL, 0) == 0)
+		avc_ok = 1;
 }
 
 int selinux_check_access(const char *scon, const char *tcon, const char *class,
@@ -33,6 +32,11 @@ int selinux_check_access(const char *scon, const char *tcon, const char *class,
 	if (selinux_enabled != 1)
 		return 0;
 
+	if (!avc_ok) {
+		errno = EINVAL;
+		return -1;
+	}
+
 	rc = avc_context_to_sid(scon, &scon_id);
 	if (rc < 0)
 		return rc;
Signature · 1 line
2.55.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Stephen Smalley

avc_init_once() calls avc_open() but discards a failure result, and a subsequent selinux_check_access() proceeds to avc_context_to_sid() which now hits assert(avc_running) and aborts the caller.

Latch the initializer outcome and have selinux_check_access() return -1/EINVAL when the AVC never came up, so callers see an error rather than a SIGABRT.

Quoted 41 lines
---
 libselinux/src/checkAccess.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
libselinux/src/checkAccess.c +8 -5
index d8f485e9..3cdd2413 100644
@@ -9,14 +9,13 @@
 
 static pthread_once_t once = PTHREAD_ONCE_INIT;
 static int selinux_enabled;
+static int avc_ok;
 
 static void avc_init_once(void)
 {
        selinux_enabled = is_selinux_enabled();
-       if (selinux_enabled == 1) {
-               if (avc_open(NULL, 0))
-                       return;
-       }
+       if (selinux_enabled == 1 && avc_open(NULL, 0) == 0)
+               avc_ok = 1;
 }
 
 int selinux_check_access(const char *scon, const char *tcon, const char *class,
@@ -33,6 +32,11 @@ int selinux_check_access(const char *scon, const char *tcon, const char *class,
        if (selinux_enabled != 1)
                return 0;
 
+       if (!avc_ok) {
+               errno = EINVAL;
+               return -1;
+       }
+
        rc = avc_context_to_sid(scon, &scon_id);
        if (rc < 0)
                return rc;
--

2.55.0

author maintainer
#2 lore raw
Quoted 8 lines · James Carter
Quoted 5 lines · Stephen Smalley

avc_init_once() calls avc_open() but discards a failure result, and a subsequent selinux_check_access() proceeds to avc_context_to_sid() which now hits assert(avc_running) and aborts the caller.

Quoted 7 lines
Quoted 5 lines

Latch the initializer outcome and have selinux_check_access() return -1/EINVAL when the AVC never came up, so callers see an error rather than a SIGABRT.

Thanks, merged.