bd1529
From 4e48673172b012a06575e4f5b681d3554eded2e2 Mon Sep 17 00:00:00 2001
bd1529
From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= <cgzones@googlemail.com>
bd1529
Date: Mon, 6 Jan 2020 15:27:23 +0100
bd1529
Subject: [PATCH] selinux: add trigger for policy reload to refresh internal
bd1529
 selabel cache
bd1529
bd1529
Reload the internal selabel cache automatically on SELinux policy reloads so non pid-1 daemons are participating.
bd1529
bd1529
Run the reload function `mac_selinux_reload()` not manually on daemon-reload, but rather pass it as callback to libselinux.
bd1529
Trigger the callback prior usage of the systemd internal selabel cache by depleting the selinux netlink socket via `avc_netlink_check_nb()`.
bd1529
bd1529
Improves: a9dfac21ec85 ("core: reload SELinux label cache on daemon-reload")
bd1529
Improves: #13363
bd1529
(cherry picked from commit 61f3e897f13101f29fb8027e8839498a469ad58e)
bd1529
bd1529
Related: #1888912
bd1529
---
bd1529
 src/basic/selinux-util.c | 23 +++++++++++++++++++----
bd1529
 src/basic/selinux-util.h |  1 -
bd1529
 src/core/main.c          |  2 --
bd1529
 3 files changed, 19 insertions(+), 7 deletions(-)
bd1529
bd1529
diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c
bd1529
index a078ce23ef..bfe3d015aa 100644
bd1529
--- a/src/basic/selinux-util.c
bd1529
+++ b/src/basic/selinux-util.c
bd1529
@@ -10,6 +10,7 @@
bd1529
 #include <syslog.h>
bd1529
 
bd1529
 #if HAVE_SELINUX
bd1529
+#include <selinux/avc.h>
bd1529
 #include <selinux/context.h>
bd1529
 #include <selinux/label.h>
bd1529
 #include <selinux/selinux.h>
bd1529
@@ -32,6 +33,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
bd1529
 #define _cleanup_freecon_ _cleanup_(freeconp)
bd1529
 #define _cleanup_context_free_ _cleanup_(context_freep)
bd1529
 
bd1529
+static int mac_selinux_reload(int seqno);
bd1529
+
bd1529
 static int cached_use = -1;
bd1529
 static struct selabel_handle *label_hnd = NULL;
bd1529
 
bd1529
@@ -63,6 +66,8 @@ int mac_selinux_init(void) {
bd1529
         usec_t before_timestamp, after_timestamp;
bd1529
         struct mallinfo before_mallinfo, after_mallinfo;
bd1529
 
bd1529
+        selinux_set_callback(SELINUX_CB_POLICYLOAD, (union selinux_callback) mac_selinux_reload);
bd1529
+
bd1529
         if (label_hnd)
bd1529
                 return 0;
bd1529
 
bd1529
@@ -105,13 +110,12 @@ void mac_selinux_finish(void) {
bd1529
 #endif
bd1529
 }
bd1529
 
bd1529
-void mac_selinux_reload(void) {
bd1529
-
bd1529
 #if HAVE_SELINUX
bd1529
+static int mac_selinux_reload(int seqno) {
bd1529
         struct selabel_handle *backup_label_hnd;
bd1529
 
bd1529
         if (!label_hnd)
bd1529
-                return;
bd1529
+                return 0;
bd1529
 
bd1529
         backup_label_hnd = TAKE_PTR(label_hnd);
bd1529
 
bd1529
@@ -122,8 +126,10 @@ void mac_selinux_reload(void) {
bd1529
                 selabel_close(backup_label_hnd);
bd1529
         else
bd1529
                 label_hnd = backup_label_hnd;
bd1529
-#endif
bd1529
+
bd1529
+        return 0;
bd1529
 }
bd1529
+#endif
bd1529
 
bd1529
 int mac_selinux_fix(const char *path, LabelFixFlags flags) {
bd1529
 
bd1529
@@ -152,6 +158,9 @@ int mac_selinux_fix(const char *path, LabelFixFlags flags) {
bd1529
         if (fstat(fd, &st) < 0)
bd1529
                 return -errno;
bd1529
 
bd1529
+        /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
bd1529
+        (void) avc_netlink_check_nb();
bd1529
+
bd1529
         if (selabel_lookup_raw(label_hnd, &fcon, path, st.st_mode) < 0) {
bd1529
                 r = -errno;
bd1529
 
bd1529
@@ -345,6 +354,9 @@ static int selinux_create_file_prepare_abspath(const char *abspath, mode_t mode)
bd1529
         assert(abspath);
bd1529
         assert(path_is_absolute(abspath));
bd1529
 
bd1529
+        /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
bd1529
+        (void) avc_netlink_check_nb();
bd1529
+
bd1529
         r = selabel_lookup_raw(label_hnd, &filecon, abspath, mode);
bd1529
         if (r < 0) {
bd1529
                 /* No context specified by the policy? Proceed without setting it. */
bd1529
@@ -496,6 +508,9 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
bd1529
 
bd1529
         path = strndupa(un->sun_path, addrlen - offsetof(struct sockaddr_un, sun_path));
bd1529
 
bd1529
+        /* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */
bd1529
+        (void) avc_netlink_check_nb();
bd1529
+
bd1529
         if (path_is_absolute(path))
bd1529
                 r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
bd1529
         else {
bd1529
diff --git a/src/basic/selinux-util.h b/src/basic/selinux-util.h
bd1529
index 639c35b687..bd5207c318 100644
bd1529
--- a/src/basic/selinux-util.h
bd1529
+++ b/src/basic/selinux-util.h
bd1529
@@ -13,7 +13,6 @@ void mac_selinux_retest(void);
bd1529
 
bd1529
 int mac_selinux_init(void);
bd1529
 void mac_selinux_finish(void);
bd1529
-void mac_selinux_reload(void);
bd1529
 
bd1529
 int mac_selinux_fix(const char *path, LabelFixFlags flags);
bd1529
 int mac_selinux_apply(const char *path, const char *label);
bd1529
diff --git a/src/core/main.c b/src/core/main.c
bd1529
index d5c41da0c4..d897155644 100644
bd1529
--- a/src/core/main.c
bd1529
+++ b/src/core/main.c
bd1529
@@ -1682,8 +1682,6 @@ static int invoke_main_loop(
bd1529
                         saved_log_level = m->log_level_overridden ? log_get_max_level() : -1;
bd1529
                         saved_log_target = m->log_target_overridden ? log_get_target() : _LOG_TARGET_INVALID;
bd1529
 
bd1529
-                        mac_selinux_reload();
bd1529
-
bd1529
                         (void) parse_configuration(saved_rlimit_nofile, saved_rlimit_memlock);
bd1529
 
bd1529
                         set_manager_defaults(m);