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

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