Blame SOURCES/dbus-1.6.12-mls-listnames.patch

744420
From 8b74179ee31652bbaaf979777b9e829b426053ef Mon Sep 17 00:00:00 2001
744420
From: David King <dking@redhat.com>
744420
Date: Tue, 4 Nov 2014 10:10:36 +0000
744420
Subject: [PATCH] selinux: Check ListNames permissions with MLS
744420
744420
https://bugzilla.redhat.com/show_bug.cgi?id=1118399
744420
---
744420
 bus/driver.c  |  52 +++++++++++++++++++++++++
744420
 bus/selinux.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
744420
 bus/selinux.h |   5 +++
744420
 3 files changed, 180 insertions(+)
744420
744420
diff --git a/bus/driver.c b/bus/driver.c
744420
index 574e0f3..20cc940 100644
744420
--- a/bus/driver.c
744420
+++ b/bus/driver.c
744420
@@ -379,6 +379,9 @@ bus_driver_handle_list_services (DBusConnection *connection,
744420
   char **services;
744420
   BusRegistry *registry;
744420
   int i;
744420
+#ifdef HAVE_SELINUX
744420
+  dbus_bool_t mls_enabled;
744420
+#endif
744420
   DBusMessageIter iter;
744420
   DBusMessageIter sub;
744420
 
744420
@@ -425,9 +428,58 @@ bus_driver_handle_list_services (DBusConnection *connection,
744420
       }
744420
   }
744420
 
744420
+#ifdef HAVE_SELINUX
744420
+  mls_enabled = bus_selinux_mls_enabled ();
744420
+#endif
744420
   i = 0;
744420
   while (i < len)
744420
     {
744420
+#ifdef HAVE_SELINUX
744420
+      if (mls_enabled)
744420
+        {
744420
+          const char *requester;
744420
+          BusService *service;
744420
+          DBusString str;
744420
+          DBusConnection *service_conn;
744420
+          DBusConnection *requester_conn;
744420
+
744420
+          requester = dbus_message_get_destination (reply);
744420
+          _dbus_string_init_const (&str, requester);
744420
+          service = bus_registry_lookup (registry, &str);
744420
+
744420
+          if (service == NULL)
744420
+            {
744420
+              _dbus_warn_check_failed ("service lookup failed: %s", requester);
744420
+              ++i;
744420
+              continue;
744420
+            }
744420
+          requester_conn = bus_service_get_primary_owners_connection (service);
744420
+          _dbus_string_init_const (&str, services[i]);
744420
+          service = bus_registry_lookup (registry, &str);
744420
+          if (service == NULL)
744420
+            {
744420
+              _dbus_warn_check_failed ("service lookup failed: %s", services[i]);
744420
+              ++i;
744420
+              continue;
744420
+            }
744420
+          service_conn = bus_service_get_primary_owners_connection (service);
744420
+
744420
+          if (!bus_selinux_allows_name (requester_conn, service_conn, error))
744420
+            {
744420
+              if (dbus_error_is_set (error) &&
744420
+                  dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
744420
+                {
744420
+                  dbus_free_string_array (services);
744420
+                  dbus_message_unref (reply);
744420
+                  return FALSE;
744420
+                }
744420
+
744420
+              /* Skip any services which are disallowed by SELinux policy. */
744420
+              ++i;
744420
+              continue;
744420
+            }
744420
+        }
744420
+#endif
744420
       if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
744420
                                            &services[i]))
744420
         {
744420
diff --git a/bus/selinux.c b/bus/selinux.c
744420
index 36287e9..6442b79 100644
744420
--- a/bus/selinux.c
744420
+++ b/bus/selinux.c
744420
@@ -63,6 +63,9 @@
744420
 /* Store the value telling us if SELinux is enabled in the kernel. */
744420
 static dbus_bool_t selinux_enabled = FALSE;
744420
 
744420
+/* Store the value telling us if SELinux with MLS is enabled in the kernel. */
744420
+static dbus_bool_t selinux_mls_enabled = FALSE;
744420
+
744420
 /* Store an avc_entry_ref to speed AVC decisions. */
744420
 static struct avc_entry_ref aeref;
744420
 
744420
@@ -289,6 +292,20 @@ bus_selinux_enabled (void)
744420
 }
744420
 
744420
 /**
744420
+ * Return whether or not SELinux with MLS support is enabled; must be
744420
+ * called after bus_selinux_init.
744420
+ */
744420
+dbus_bool_t
744420
+bus_selinux_mls_enabled (void)
744420
+{
744420
+#ifdef HAVE_SELINUX
744420
+  return selinux_mls_enabled;
744420
+#else
744420
+  return FALSE;
744420
+#endif /* HAVE_SELINUX */
744420
+}
744420
+
744420
+/**
744420
  * Do early initialization; determine whether SELinux is enabled.
744420
  */
744420
 dbus_bool_t
744420
@@ -308,6 +325,16 @@ bus_selinux_pre_init (void)
744420
     }
744420
 
744420
   selinux_enabled = r != 0;
744420
+
744420
+  r = is_selinux_mls_enabled ();
744420
+  if (r < 0)
744420
+    {
744420
+      _dbus_warn ("Could not tell if SELinux MLS is enabled: %s\n",
744420
+                  _dbus_strerror (errno));
744420
+      return FALSE;
744420
+    }
744420
+
744420
+  selinux_mls_enabled = r != 0;
744420
   return TRUE;
744420
 #else
744420
   return TRUE;
744420
@@ -724,6 +751,102 @@ bus_connection_read_selinux_context (DBusConnection     *connection,
744420
 #endif /* HAVE_SELINUX */
744420
 
744420
 /**
744420
+ * Check if SELinux security controls allow one connection to determine the
744420
+ * name of the other, taking into account MLS considerations.
744420
+ *
744420
+ * @param source the requester of the name.
744420
+ * @param destination the name being requested.
744420
+ * @returns whether the name should be visible by the source of the request
744420
+ */
744420
+dbus_bool_t
744420
+bus_selinux_allows_name (DBusConnection     *source,
744420
+                         DBusConnection     *destination,
744420
+                         DBusError          *error)
744420
+{
744420
+#ifdef HAVE_SELINUX
744420
+  int err;
744420
+  char *policy_type;
744420
+  unsigned long spid, tpid;
744420
+  BusSELinuxID *source_sid;
744420
+  BusSELinuxID *dest_sid;
744420
+  dbus_bool_t ret;
744420
+  dbus_bool_t string_alloced;
744420
+  DBusString auxdata;
744420
+
744420
+  if (!selinux_mls_enabled)
744420
+    return TRUE;
744420
+
744420
+  err = selinux_getpolicytype (&policy_type);
744420
+  if (err < 0)
744420
+    {
744420
+      dbus_set_error_const (error, DBUS_ERROR_IO_ERROR,
744420
+                            "Failed to get SELinux policy type");
744420
+      return FALSE;
744420
+    }
744420
+
744420
+  /* Only check against MLS policy if running under that policy. */
744420
+  if (strcmp (policy_type, "mls") != 0)
744420
+    {
744420
+      free (policy_type);
744420
+      return TRUE;
744420
+    }
744420
+
744420
+  free (policy_type);
744420
+
744420
+  _dbus_assert (source != NULL);
744420
+  _dbus_assert (destination != NULL);
744420
+
744420
+  if (!source || !dbus_connection_get_unix_process_id (source, &spid))
744420
+    spid = 0;
744420
+  if (!destination || !dbus_connection_get_unix_process_id (destination, &tpid))
744420
+    tpid = 0;
744420
+
744420
+  string_alloced = FALSE;
744420
+  if (!_dbus_string_init (&auxdata))
744420
+    goto oom;
744420
+  string_alloced = TRUE;
744420
+
744420
+  if (spid)
744420
+    {
744420
+      if (!_dbus_string_append (&auxdata, " spid="))
744420
+	goto oom;
744420
+
744420
+      if (!_dbus_string_append_uint (&auxdata, spid))
744420
+	goto oom;
744420
+    }
744420
+
744420
+  if (tpid)
744420
+    {
744420
+      if (!_dbus_string_append (&auxdata, " tpid="))
744420
+	goto oom;
744420
+
744420
+      if (!_dbus_string_append_uint (&auxdata, tpid))
744420
+	goto oom;
744420
+    }
744420
+
744420
+  source_sid = bus_connection_get_selinux_id (source);
744420
+  dest_sid = bus_connection_get_selinux_id (destination);
744420
+
744420
+  ret = bus_selinux_check (source_sid,
744420
+                           dest_sid,
744420
+                           SECCLASS_CONTEXT,
744420
+                           CONTEXT__CONTAINS,
744420
+                           &auxdata);
744420
+
744420
+  _dbus_string_free (&auxdata);
744420
+  return ret;
744420
+
744420
+ oom:
744420
+  if (string_alloced)
744420
+    _dbus_string_free (&auxdata);
744420
+  BUS_SET_OOM (error);
744420
+  return FALSE;
744420
+#else
744420
+  return TRUE;
744420
+#endif /* HAVE_SELINUX */
744420
+}
744420
+
744420
+/**
744420
  * Read the SELinux ID from the connection.
744420
  *
744420
  * @param connection the connection to read from
744420
diff --git a/bus/selinux.h b/bus/selinux.h
744420
index 3bab36d..fcaac5f 100644
744420
--- a/bus/selinux.h
744420
+++ b/bus/selinux.h
744420
@@ -32,6 +32,7 @@ dbus_bool_t bus_selinux_full_init(void);
744420
 void        bus_selinux_shutdown (void);
744420
 
744420
 dbus_bool_t bus_selinux_enabled  (void);
744420
+dbus_bool_t bus_selinux_mls_enabled (void);
744420
 
744420
 void bus_selinux_id_ref    (BusSELinuxID *sid);
744420
 void bus_selinux_id_unref  (BusSELinuxID *sid);
744420
@@ -54,6 +55,10 @@ dbus_bool_t bus_selinux_allows_acquire_service (DBusConnection *connection,
744420
 						const char     *service_name,
744420
 						DBusError      *error);
744420
 
744420
+dbus_bool_t bus_selinux_allows_name            (DBusConnection *source,
744420
+                                                DBusConnection *destination,
744420
+                                                DBusError      *error);
744420
+
744420
 dbus_bool_t bus_selinux_allows_send            (DBusConnection *sender,
744420
                                                 DBusConnection *proposed_recipient,
744420
 						const char     *msgtype, /* Supplementary audit data */
744420
-- 
744420
2.1.0
744420