dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0101-selinux-Do-not-fail-if-SELinux-is-not-managed.patch

bb7cd1
From 9b7c29b67ec845b2004d6bcac2bcceabfd855f1e Mon Sep 17 00:00:00 2001
bb7cd1
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
bb7cd1
Date: Wed, 8 Feb 2017 12:01:37 +0100
bb7cd1
Subject: [PATCH 101/101] selinux: Do not fail if SELinux is not managed
bb7cd1
MIME-Version: 1.0
bb7cd1
Content-Type: text/plain; charset=UTF-8
bb7cd1
Content-Transfer-Encoding: 8bit
bb7cd1
bb7cd1
Previously we failed if semanage_is_managed returned 0 or -1 (not
bb7cd1
managed or error). With this patch we only fail in case of error and
bb7cd1
continue normally if selinux is not managed by libsemanage at all.
bb7cd1
bb7cd1
Resolves:
bb7cd1
https://fedorahosted.org/sssd/ticket/3297
bb7cd1
bb7cd1
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
bb7cd1
(cherry picked from commit 78a08d30b5fbf6e1e3b589e0cf67022e0c1faa33)
bb7cd1
---
bb7cd1
 Makefile.am                       |  1 +
bb7cd1
 src/providers/ipa/selinux_child.c |  9 ++++--
bb7cd1
 src/util/sss_semanage.c           | 61 +++++++++++++++++++++++++--------------
bb7cd1
 src/util/util_errors.c            |  1 +
bb7cd1
 src/util/util_errors.h            |  1 +
bb7cd1
 5 files changed, 49 insertions(+), 24 deletions(-)
bb7cd1
bb7cd1
diff --git a/Makefile.am b/Makefile.am
bb7cd1
index f5ac363a35e4aae51e8b70bad27c7fc824be10f2..370d6442ec58a14946ad288a23c696f25ca98f47 100644
bb7cd1
--- a/Makefile.am
bb7cd1
+++ b/Makefile.am
bb7cd1
@@ -4040,6 +4040,7 @@ selinux_child_SOURCES = \
bb7cd1
     src/util/atomic_io.c \
bb7cd1
     src/util/util.c \
bb7cd1
     src/util/util_ext.c \
bb7cd1
+    src/util/util_errors.c
bb7cd1
     $(NULL)
bb7cd1
 selinux_child_CFLAGS = \
bb7cd1
     $(AM_CFLAGS) \
bb7cd1
diff --git a/src/providers/ipa/selinux_child.c b/src/providers/ipa/selinux_child.c
bb7cd1
index 380005c7ad3269fc8113c62ceef30b076455b5dd..f8dd3954a7244df2dcbb910aabf8888f41306c09 100644
bb7cd1
--- a/src/providers/ipa/selinux_child.c
bb7cd1
+++ b/src/providers/ipa/selinux_child.c
bb7cd1
@@ -174,14 +174,19 @@ static bool seuser_needs_update(struct input_buffer *ibuf)
bb7cd1
 
bb7cd1
     ret = get_seuser(ibuf, ibuf->username, &db_seuser, &db_mls_range);
bb7cd1
     DEBUG(SSSDBG_TRACE_INTERNAL,
bb7cd1
-          "get_seuser: ret: %d seuser: %s mls: %s\n",
bb7cd1
-          ret, db_seuser ? db_seuser : "unknown",
bb7cd1
+          "get_seuser: ret: %d msg: [%s] seuser: %s mls: %s\n",
bb7cd1
+          ret, sss_strerror(ret),
bb7cd1
+          db_seuser ? db_seuser : "unknown",
bb7cd1
           db_mls_range ? db_mls_range : "unknown");
bb7cd1
     if (ret == EOK && db_seuser && db_mls_range &&
bb7cd1
             strcmp(db_seuser, ibuf->seuser) == 0 &&
bb7cd1
             strcmp(db_mls_range, ibuf->mls_range) == 0) {
bb7cd1
         needs_update = false;
bb7cd1
     }
bb7cd1
+    /* OR */
bb7cd1
+    if (ret == ERR_SELINUX_NOT_MANAGED) {
bb7cd1
+        needs_update = false;
bb7cd1
+    }
bb7cd1
 
bb7cd1
     talloc_free(db_seuser);
bb7cd1
     talloc_free(db_mls_range);
bb7cd1
diff --git a/src/util/sss_semanage.c b/src/util/sss_semanage.c
bb7cd1
index fe06bee1dfec3abca3aa3cd5e85e55386ac11343..0da97aad4d8eba733b131c2749932e03ca4242c4 100644
bb7cd1
--- a/src/util/sss_semanage.c
bb7cd1
+++ b/src/util/sss_semanage.c
bb7cd1
@@ -73,7 +73,7 @@ static void sss_semanage_close(semanage_handle_t *handle)
bb7cd1
     semanage_handle_destroy(handle);
bb7cd1
 }
bb7cd1
 
bb7cd1
-static semanage_handle_t *sss_semanage_init(void)
bb7cd1
+static int sss_semanage_init(semanage_handle_t **_handle)
bb7cd1
 {
bb7cd1
     int ret;
bb7cd1
     semanage_handle_t *handle = NULL;
bb7cd1
@@ -81,7 +81,8 @@ static semanage_handle_t *sss_semanage_init(void)
bb7cd1
     handle = semanage_handle_create();
bb7cd1
     if (!handle) {
bb7cd1
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux management handle\n");
bb7cd1
-        return NULL;
bb7cd1
+        ret = EIO;
bb7cd1
+        goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
     semanage_msg_set_callback(handle,
bb7cd1
@@ -89,28 +90,41 @@ static semanage_handle_t *sss_semanage_init(void)
bb7cd1
                               NULL);
bb7cd1
 
bb7cd1
     ret = semanage_is_managed(handle);
bb7cd1
-    if (ret != 1) {
bb7cd1
-        DEBUG(SSSDBG_CRIT_FAILURE, "SELinux policy not managed\n");
bb7cd1
-        goto fail;
bb7cd1
+    if (ret == 0) {
bb7cd1
+        DEBUG(SSSDBG_TRACE_FUNC, "SELinux policy not managed via libsemanage\n");
bb7cd1
+        ret = ERR_SELINUX_NOT_MANAGED;
bb7cd1
+        goto done;
bb7cd1
+    } else if (ret == -1) {
bb7cd1
+        DEBUG(SSSDBG_CRIT_FAILURE, "Call to semanage_is_managed failed\n");
bb7cd1
+        ret = EIO;
bb7cd1
+        goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
     ret = semanage_access_check(handle);
bb7cd1
     if (ret < SEMANAGE_CAN_READ) {
bb7cd1
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot read SELinux policy store\n");
bb7cd1
-        goto fail;
bb7cd1
+        ret = EACCES;
bb7cd1
+        goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
     ret = semanage_connect(handle);
bb7cd1
     if (ret != 0) {
bb7cd1
         DEBUG(SSSDBG_CRIT_FAILURE,
bb7cd1
               "Cannot estabilish SELinux management connection\n");
bb7cd1
-        goto fail;
bb7cd1
+        ret = EIO;
bb7cd1
+        goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    return handle;
bb7cd1
-fail:
bb7cd1
-    sss_semanage_close(handle);
bb7cd1
-    return NULL;
bb7cd1
+    ret = EOK;
bb7cd1
+
bb7cd1
+done:
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        sss_semanage_close(handle);
bb7cd1
+    } else {
bb7cd1
+        *_handle = handle;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return ret;
bb7cd1
 }
bb7cd1
 
bb7cd1
 static int sss_semanage_user_add(semanage_handle_t *handle,
bb7cd1
@@ -228,10 +242,11 @@ int set_seuser(const char *login_name, const char *seuser_name,
bb7cd1
         return EOK;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    handle = sss_semanage_init();
bb7cd1
-    if (!handle) {
bb7cd1
-        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot init SELinux management\n");
bb7cd1
-        ret = EIO;
bb7cd1
+    ret = sss_semanage_init(&handle);
bb7cd1
+    if (ret == ERR_SELINUX_NOT_MANAGED) {
bb7cd1
+        goto done;
bb7cd1
+    } else if (ret != EOK) {
bb7cd1
+        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux handle\n");
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
@@ -295,10 +310,11 @@ int del_seuser(const char *login_name)
bb7cd1
     int ret;
bb7cd1
     int exists = 0;
bb7cd1
 
bb7cd1
-    handle = sss_semanage_init();
bb7cd1
-    if (!handle) {
bb7cd1
-        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot init SELinux management\n");
bb7cd1
-        ret = EIO;
bb7cd1
+    ret = sss_semanage_init(&handle);
bb7cd1
+    if (ret == ERR_SELINUX_NOT_MANAGED) {
bb7cd1
+        goto done;
bb7cd1
+    } else if (ret != EOK) {
bb7cd1
+        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux handle\n");
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
@@ -377,10 +393,11 @@ int get_seuser(TALLOC_CTX *mem_ctx, const char *login_name,
bb7cd1
     semanage_seuser_t *sm_user = NULL;
bb7cd1
     semanage_seuser_key_t *sm_key = NULL;
bb7cd1
 
bb7cd1
-    sm_handle = sss_semanage_init();
bb7cd1
-    if (sm_handle == NULL) {
bb7cd1
+    ret = sss_semanage_init(&sm_handle);
bb7cd1
+    if (ret == ERR_SELINUX_NOT_MANAGED) {
bb7cd1
+        goto done;
bb7cd1
+    } else if (ret != EOK) {
bb7cd1
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux handle\n");
bb7cd1
-        ret = EIO;
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
diff --git a/src/util/util_errors.c b/src/util/util_errors.c
bb7cd1
index 466a3b4062f39b29d831a5d8a62dc8d576eb2e97..97eaf160f20bcc8cfe52254070a2d182e19addd4 100644
bb7cd1
--- a/src/util/util_errors.c
bb7cd1
+++ b/src/util/util_errors.c
bb7cd1
@@ -75,6 +75,7 @@ struct err_string error_to_str[] = {
bb7cd1
     { "Cannot connect to system bus" }, /* ERR_NO_SYSBUS */
bb7cd1
     { "LDAP search returned a referral" }, /* ERR_REFERRAL */
bb7cd1
     { "Error setting SELinux user context" }, /* ERR_SELINUX_CONTEXT */
bb7cd1
+    { "SELinux is not managed by libsemanage" }, /* ERR_SELINUX_NOT_MANAGED */
bb7cd1
     { "Username format not allowed by re_expression" }, /* ERR_REGEX_NOMATCH */
bb7cd1
     { "Time specification not supported" }, /* ERR_TIMESPEC_NOT_SUPPORTED */
bb7cd1
     { "Invalid SSSD configuration detected" }, /* ERR_INVALID_CONFIG */
bb7cd1
diff --git a/src/util/util_errors.h b/src/util/util_errors.h
bb7cd1
index 2f90c0a5d65325a431a8e4d9a480170808c9198e..4a250bf0339ba689680c155fa8e6d43f42c2467e 100644
bb7cd1
--- a/src/util/util_errors.h
bb7cd1
+++ b/src/util/util_errors.h
bb7cd1
@@ -97,6 +97,7 @@ enum sssd_errors {
bb7cd1
     ERR_NO_SYSBUS,
bb7cd1
     ERR_REFERRAL,
bb7cd1
     ERR_SELINUX_CONTEXT,
bb7cd1
+    ERR_SELINUX_NOT_MANAGED,
bb7cd1
     ERR_REGEX_NOMATCH,
bb7cd1
     ERR_TIMESPEC_NOT_SUPPORTED,
bb7cd1
     ERR_INVALID_CONFIG,
bb7cd1
-- 
bb7cd1
2.9.3
bb7cd1