Blame SOURCES/0098-SELINUX-Check-if-SELinux-is-managed-in-selinux_child.patch

ced1f5
From ae37ee533a791e038aab47683278fced2bc0b687 Mon Sep 17 00:00:00 2001
976a3f
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
976a3f
Date: Thu, 1 Feb 2018 11:34:21 +0100
ced1f5
Subject: [PATCH 98/99] SELINUX: Check if SELinux is managed in selinux_child
976a3f
MIME-Version: 1.0
976a3f
Content-Type: text/plain; charset=UTF-8
976a3f
Content-Transfer-Encoding: 8bit
976a3f
976a3f
If SELinux policy is not managed at all, don't call any SELinux user
976a3f
handling functions and instead return that no update is needed.
976a3f
976a3f
Pair-Programmed-With: Jakub Hrozek <jhrozek@redhat.com>
976a3f
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
976a3f
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
976a3f
976a3f
Resolves:
976a3f
https://pagure.io/SSSD/sssd/issue/3618
976a3f
(cherry picked from commit 450b472a68abf442479755c7916c757907b35ea5)
976a3f
---
976a3f
 src/providers/ipa/selinux_child.c |  3 +-
976a3f
 src/util/sss_semanage.c           | 82 +++++++++++++++++++++++++++++++--------
976a3f
 src/util/util.h                   |  3 ++
976a3f
 3 files changed, 70 insertions(+), 18 deletions(-)
976a3f
976a3f
diff --git a/src/providers/ipa/selinux_child.c b/src/providers/ipa/selinux_child.c
ced1f5
index a7e20f715626d0f3ecef7cc06f3de5d44b6a15c1..c659976e80cb7317671da52fe4777ee821589e36 100644
976a3f
--- a/src/providers/ipa/selinux_child.c
976a3f
+++ b/src/providers/ipa/selinux_child.c
976a3f
@@ -27,7 +27,6 @@
976a3f
 #include <unistd.h>
976a3f
 #include <sys/stat.h>
976a3f
 #include <popt.h>
976a3f
-#include <selinux/selinux.h>
976a3f
 
976a3f
 #include "util/util.h"
976a3f
 #include "util/child_common.h"
976a3f
@@ -173,7 +172,7 @@ static bool seuser_needs_update(struct input_buffer *ibuf)
976a3f
     char *db_mls_range = NULL;
976a3f
     errno_t ret;
976a3f
 
976a3f
-    ret = getseuserbyname(ibuf->username, &db_seuser, &db_mls_range);
976a3f
+    ret = sss_get_seuser(ibuf->username, &db_seuser, &db_mls_range);
976a3f
     DEBUG(SSSDBG_TRACE_INTERNAL,
976a3f
           "getseuserbyname: ret: %d seuser: %s mls: %s\n",
976a3f
           ret, db_seuser ? db_seuser : "unknown",
976a3f
diff --git a/src/util/sss_semanage.c b/src/util/sss_semanage.c
976a3f
index 37278cc986a1ea49dc2218a635d52b9d54ca089d..25b6bcdad2ad7e7ac710497f13d6a6e22360b0dd 100644
976a3f
--- a/src/util/sss_semanage.c
976a3f
+++ b/src/util/sss_semanage.c
976a3f
@@ -22,8 +22,9 @@
976a3f
 #include "config.h"
976a3f
 
976a3f
 #include <stdio.h>
976a3f
-#ifdef HAVE_SEMANAGE
976a3f
+#if defined(HAVE_SEMANAGE) && defined(HAVE_SELINUX)
976a3f
 #include <semanage/semanage.h>
976a3f
+#include <selinux/selinux.h>
976a3f
 #endif
976a3f
 
976a3f
 #include "util/util.h"
976a3f
@@ -32,7 +33,7 @@
976a3f
 #define DEFAULT_SERANGE "s0"
976a3f
 #endif
976a3f
 
976a3f
-#ifdef HAVE_SEMANAGE
976a3f
+#if defined(HAVE_SEMANAGE) && defined(HAVE_SELINUX)
976a3f
 /* turn libselinux messages into SSSD DEBUG() calls */
976a3f
 static void sss_semanage_error_callback(void *varg,
976a3f
                                         semanage_handle_t *handle,
976a3f
@@ -73,33 +74,47 @@ static void sss_semanage_close(semanage_handle_t *handle)
976a3f
     semanage_handle_destroy(handle);
976a3f
 }
976a3f
 
976a3f
-static int sss_semanage_init(semanage_handle_t **_handle)
976a3f
+static int sss_is_selinux_managed(semanage_handle_t *handle)
976a3f
 {
976a3f
     int ret;
976a3f
-    semanage_handle_t *handle = NULL;
976a3f
 
976a3f
-    handle = semanage_handle_create();
976a3f
-    if (!handle) {
976a3f
-        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux management handle\n");
976a3f
-        ret = EIO;
976a3f
-        goto done;
976a3f
+    if (handle == NULL) {
976a3f
+        return EINVAL;
976a3f
     }
976a3f
 
976a3f
-    semanage_msg_set_callback(handle,
976a3f
-                              sss_semanage_error_callback,
976a3f
-                              NULL);
976a3f
-
976a3f
     ret = semanage_is_managed(handle);
976a3f
     if (ret == 0) {
976a3f
         DEBUG(SSSDBG_TRACE_FUNC, "SELinux policy not managed via libsemanage\n");
976a3f
-        ret = ERR_SELINUX_NOT_MANAGED;
976a3f
-        goto done;
976a3f
+        return ERR_SELINUX_NOT_MANAGED;
976a3f
     } else if (ret == -1) {
976a3f
         DEBUG(SSSDBG_CRIT_FAILURE, "Call to semanage_is_managed failed\n");
976a3f
+        return EIO;
976a3f
+    }
976a3f
+
976a3f
+    return EOK;
976a3f
+}
976a3f
+
976a3f
+static int sss_semanage_init(semanage_handle_t **_handle)
976a3f
+{
976a3f
+    int ret;
976a3f
+    semanage_handle_t *handle = NULL;
976a3f
+
976a3f
+    handle = semanage_handle_create();
976a3f
+    if (!handle) {
976a3f
+        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux management handle\n");
976a3f
         ret = EIO;
976a3f
         goto done;
976a3f
     }
976a3f
 
976a3f
+    semanage_msg_set_callback(handle,
976a3f
+                              sss_semanage_error_callback,
976a3f
+                              NULL);
976a3f
+
976a3f
+    ret = sss_is_selinux_managed(handle);
976a3f
+    if (ret != EOK) {
976a3f
+        goto done;
976a3f
+    }
976a3f
+
976a3f
     ret = semanage_access_check(handle);
976a3f
     if (ret < SEMANAGE_CAN_READ) {
976a3f
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot read SELinux policy store\n");
976a3f
@@ -229,6 +244,34 @@ done:
976a3f
     return ret;
976a3f
 }
976a3f
 
976a3f
+int sss_get_seuser(const char *linuxuser,
976a3f
+                   char **selinuxuser,
976a3f
+                   char **level)
976a3f
+{
976a3f
+    int ret;
976a3f
+    semanage_handle_t *handle;
976a3f
+
976a3f
+    handle = semanage_handle_create();
976a3f
+    if (handle == NULL) {
976a3f
+        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create SELinux management handle\n");
976a3f
+        return EIO;
976a3f
+    }
976a3f
+
976a3f
+    semanage_msg_set_callback(handle,
976a3f
+                              sss_semanage_error_callback,
976a3f
+                              NULL);
976a3f
+
976a3f
+    /* We only needed the handle for this call. Close the handle right
976a3f
+     * after it */
976a3f
+    ret = sss_is_selinux_managed(handle);
976a3f
+    sss_semanage_close(handle);
976a3f
+    if (ret != EOK) {
976a3f
+        return ret;
976a3f
+    }
976a3f
+
976a3f
+    return getseuserbyname(linuxuser, selinuxuser, level);
976a3f
+}
976a3f
+
976a3f
 int set_seuser(const char *login_name, const char *seuser_name,
976a3f
                const char *mls)
976a3f
 {
976a3f
@@ -382,7 +425,7 @@ done:
976a3f
     sss_semanage_close(handle);
976a3f
     return ret;
976a3f
 }
976a3f
-#else /* HAVE_SEMANAGE */
976a3f
+#else /* HAVE_SEMANAGE && HAVE_SELINUX */
976a3f
 int set_seuser(const char *login_name, const char *seuser_name,
976a3f
                const char *mls)
976a3f
 {
976a3f
@@ -393,4 +436,11 @@ int del_seuser(const char *login_name)
976a3f
 {
976a3f
     return EOK;
976a3f
 }
976a3f
+
976a3f
+int sss_get_seuser(const char *linuxuser,
976a3f
+                   char **selinuxuser,
976a3f
+                   char **level)
976a3f
+{
976a3f
+    return EOK;
976a3f
+}
976a3f
 #endif  /* HAVE_SEMANAGE */
976a3f
diff --git a/src/util/util.h b/src/util/util.h
ced1f5
index 2521b1789b0b8701b1fbcce33890eedb7fe18d5e..be818a9531897e4f988cae48bf6ba30aea0e6d56 100644
976a3f
--- a/src/util/util.h
976a3f
+++ b/src/util/util.h
ced1f5
@@ -654,6 +654,9 @@ errno_t restore_creds(struct sss_creds *saved_creds);
976a3f
 int set_seuser(const char *login_name, const char *seuser_name,
976a3f
                const char *mlsrange);
976a3f
 int del_seuser(const char *login_name);
976a3f
+int sss_get_seuser(const char *linuxuser,
976a3f
+                   char **selinuxuser,
976a3f
+                   char **level);
976a3f
 
976a3f
 /* convert time from generalized form to unix time */
976a3f
 errno_t sss_utc_to_time_t(const char *str, const char *format, time_t *unix_time);
976a3f
-- 
976a3f
2.14.3
976a3f