Blame SOURCES/0047-SELINUX-Always-add-SELinux-user-to-the-semanage-data.patch

0048be
From e7e942ceb1f8402d00f5f14a9e065d3fc434b711 Mon Sep 17 00:00:00 2001
0048be
From: Jakub Hrozek <jhrozek@redhat.com>
0048be
Date: Thu, 23 Aug 2018 13:55:51 +0200
0048be
Subject: [PATCH] SELINUX: Always add SELinux user to the semanage database if
0048be
 it doesn't exist
0048be
MIME-Version: 1.0
0048be
Content-Type: text/plain; charset=UTF-8
0048be
Content-Transfer-Encoding: 8bit
0048be
0048be
Previously, we tried to optimize too much and only set the SELinux user
0048be
to Linux user mapping in case the SELinux user was different from the
0048be
system default. But this doesn't work for the case where the Linux user
0048be
has a non-standard home directory, because then SELinux would not have
0048be
any idea that this user's home directory should be labeled as a home
0048be
directory.
0048be
0048be
This patch relaxes the optimization in the sense that on the first
0048be
login, the SELinux context is saved regardless of whether it is the same
0048be
as the default or different.
0048be
0048be
Resolves:
0048be
https://pagure.io/SSSD/sssd/issue/3819
0048be
0048be
Reviewed-by: Michal Židek <mzidek@redhat.com>
0048be
(cherry picked from commit 945865ae16120ffade267227ca48cefd58822fd2)
0048be
---
0048be
 src/providers/ipa/selinux_child.c | 10 ++++++++--
0048be
 src/util/sss_semanage.c           | 30 ++++++++++++++++++++++++++++++
0048be
 src/util/util.h                   |  1 +
0048be
 src/util/util_errors.c            |  1 +
0048be
 src/util/util_errors.h            |  1 +
0048be
 5 files changed, 41 insertions(+), 2 deletions(-)
0048be
0048be
diff --git a/src/providers/ipa/selinux_child.c b/src/providers/ipa/selinux_child.c
0048be
index d061417..925591e 100644
0048be
--- a/src/providers/ipa/selinux_child.c
0048be
+++ b/src/providers/ipa/selinux_child.c
0048be
@@ -176,13 +176,16 @@ static bool seuser_needs_update(const char *username,
0048be
 
0048be
     ret = sss_get_seuser(username, &db_seuser, &db_mls_range);
0048be
     DEBUG(SSSDBG_TRACE_INTERNAL,
0048be
-          "getseuserbyname: ret: %d seuser: %s mls: %s\n",
0048be
+          "sss_get_seuser: ret: %d seuser: %s mls: %s\n",
0048be
           ret, db_seuser ? db_seuser : "unknown",
0048be
           db_mls_range ? db_mls_range : "unknown");
0048be
     if (ret == EOK && db_seuser && db_mls_range &&
0048be
             strcmp(db_seuser, seuser) == 0 &&
0048be
             strcmp(db_mls_range, mls_range) == 0) {
0048be
-        needs_update = false;
0048be
+        ret = sss_seuser_exists(username);
0048be
+        if (ret == EOK) {
0048be
+            needs_update = false;
0048be
+        }
0048be
     }
0048be
     /* OR */
0048be
     if (ret == ERR_SELINUX_NOT_MANAGED) {
0048be
@@ -191,6 +194,9 @@ static bool seuser_needs_update(const char *username,
0048be
 
0048be
     free(db_seuser);
0048be
     free(db_mls_range);
0048be
+    DEBUG(SSSDBG_TRACE_FUNC,
0048be
+          "The SELinux user does %sneed an update\n",
0048be
+          needs_update ? "" : "not ");
0048be
     return needs_update;
0048be
 }
0048be
 
0048be
diff --git a/src/util/sss_semanage.c b/src/util/sss_semanage.c
0048be
index bcce57b..aea0385 100644
0048be
--- a/src/util/sss_semanage.c
0048be
+++ b/src/util/sss_semanage.c
0048be
@@ -248,6 +248,36 @@ done:
0048be
     return ret;
0048be
 }
0048be
 
0048be
+int sss_seuser_exists(const char *linuxuser)
0048be
+{
0048be
+    int ret;
0048be
+    int exists;
0048be
+    semanage_seuser_key_t *sm_key = NULL;
0048be
+    semanage_handle_t *sm_handle = NULL;
0048be
+
0048be
+    ret = sss_semanage_init(&sm_handle);
0048be
+    if (ret != EOK) {
0048be
+        return ret;
0048be
+    }
0048be
+
0048be
+    ret = semanage_seuser_key_create(sm_handle, linuxuser, &sm_key);
0048be
+    if (ret < 0) {
0048be
+        sss_semanage_close(sm_handle);
0048be
+        return EIO;
0048be
+    }
0048be
+
0048be
+    ret = semanage_seuser_exists(sm_handle, sm_key, &exists;;
0048be
+    semanage_seuser_key_free(sm_key);
0048be
+    sss_semanage_close(sm_handle);
0048be
+    if (ret < 0) {
0048be
+        return EIO;
0048be
+    }
0048be
+
0048be
+    DEBUG(SSSDBG_TRACE_FUNC, "seuser exists: %s\n", exists ? "yes" : "no");
0048be
+
0048be
+    return exists ? EOK : ERR_SELINUX_USER_NOT_FOUND;
0048be
+}
0048be
+
0048be
 int sss_get_seuser(const char *linuxuser,
0048be
                    char **selinuxuser,
0048be
                    char **level)
0048be
diff --git a/src/util/util.h b/src/util/util.h
0048be
index bc89ecb..c78615b 100644
0048be
--- a/src/util/util.h
0048be
+++ b/src/util/util.h
0048be
@@ -660,6 +660,7 @@ int sss_del_seuser(const char *login_name);
0048be
 int sss_get_seuser(const char *linuxuser,
0048be
                    char **selinuxuser,
0048be
                    char **level);
0048be
+int sss_seuser_exists(const char *linuxuser);
0048be
 
0048be
 /* convert time from generalized form to unix time */
0048be
 errno_t sss_utc_to_time_t(const char *str, const char *format, time_t *unix_time);
0048be
diff --git a/src/util/util_errors.c b/src/util/util_errors.c
0048be
index e2bb2a0..e5c5bd1 100644
0048be
--- a/src/util/util_errors.c
0048be
+++ b/src/util/util_errors.c
0048be
@@ -75,6 +75,7 @@ struct err_string error_to_str[] = {
0048be
     { "LDAP search returned a referral" }, /* ERR_REFERRAL */
0048be
     { "Error setting SELinux user context" }, /* ERR_SELINUX_CONTEXT */
0048be
     { "SELinux is not managed by libsemanage" }, /* ERR_SELINUX_NOT_MANAGED */
0048be
+    { "SELinux user does not exist" }, /* ERR_SELINUX_USER_NOT_FOUND */
0048be
     { "Username format not allowed by re_expression" }, /* ERR_REGEX_NOMATCH */
0048be
     { "Time specification not supported" }, /* ERR_TIMESPEC_NOT_SUPPORTED */
0048be
     { "Invalid SSSD configuration detected" }, /* ERR_INVALID_CONFIG */
0048be
diff --git a/src/util/util_errors.h b/src/util/util_errors.h
0048be
index 4950172..a4760a1 100644
0048be
--- a/src/util/util_errors.h
0048be
+++ b/src/util/util_errors.h
0048be
@@ -97,6 +97,7 @@ enum sssd_errors {
0048be
     ERR_REFERRAL,
0048be
     ERR_SELINUX_CONTEXT,
0048be
     ERR_SELINUX_NOT_MANAGED,
0048be
+    ERR_SELINUX_USER_NOT_FOUND,
0048be
     ERR_REGEX_NOMATCH,
0048be
     ERR_TIMESPEC_NOT_SUPPORTED,
0048be
     ERR_INVALID_CONFIG,
0048be
-- 
0048be
2.9.5
0048be