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