gentleknife / rpms / dbus

Forked from rpms/dbus 4 years ago
Clone
f8b01f
diff -urN dbus-1.10.24.old/bus/driver.c dbus-1.10.24/bus/driver.c
f8b01f
--- dbus-1.10.24.old/bus/driver.c	2017-09-25 16:20:08.000000000 +0100
f8b01f
+++ dbus-1.10.24/bus/driver.c	2018-02-13 10:15:09.570439595 +0000
f8b01f
@@ -555,6 +555,9 @@
f8b01f
   char **services;
f8b01f
   BusRegistry *registry;
f8b01f
   int i;
f8b01f
+#ifdef HAVE_SELINUX
f8b01f
+  dbus_bool_t mls_enabled;
f8b01f
+#endif
f8b01f
   DBusMessageIter iter;
f8b01f
   DBusMessageIter sub;
f8b01f
 
f8b01f
@@ -601,9 +604,58 @@
f8b01f
       }
f8b01f
   }
f8b01f
 
f8b01f
+#ifdef HAVE_SELINUX
f8b01f
+  mls_enabled = bus_selinux_mls_enabled ();
f8b01f
+#endif
f8b01f
   i = 0;
f8b01f
   while (i < len)
f8b01f
     {
f8b01f
+#ifdef HAVE_SELINUX
f8b01f
+      if (mls_enabled)
f8b01f
+        {
f8b01f
+          const char *requester;
f8b01f
+          BusService *service;
f8b01f
+          DBusString str;
f8b01f
+          DBusConnection *service_conn;
f8b01f
+          DBusConnection *requester_conn;
f8b01f
+
f8b01f
+          requester = dbus_message_get_destination (reply);
f8b01f
+          _dbus_string_init_const (&str, requester);
f8b01f
+          service = bus_registry_lookup (registry, &str);
f8b01f
+
f8b01f
+          if (service == NULL)
f8b01f
+            {
f8b01f
+              _dbus_warn_check_failed ("service lookup failed: %s", requester);
f8b01f
+              ++i;
f8b01f
+              continue;
f8b01f
+            }
f8b01f
+          requester_conn = bus_service_get_primary_owners_connection (service);
f8b01f
+          _dbus_string_init_const (&str, services[i]);
f8b01f
+          service = bus_registry_lookup (registry, &str);
f8b01f
+          if (service == NULL)
f8b01f
+            {
f8b01f
+              _dbus_warn_check_failed ("service lookup failed: %s", services[i]);
f8b01f
+              ++i;
f8b01f
+              continue;
f8b01f
+            }
f8b01f
+          service_conn = bus_service_get_primary_owners_connection (service);
f8b01f
+
f8b01f
+          if (!bus_selinux_allows_name (requester_conn, service_conn, error))
f8b01f
+            {
f8b01f
+              if (dbus_error_is_set (error) &&
f8b01f
+                  dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
f8b01f
+                {
f8b01f
+                  dbus_free_string_array (services);
f8b01f
+                  dbus_message_unref (reply);
f8b01f
+                  return FALSE;
f8b01f
+                }
f8b01f
+
f8b01f
+              /* Skip any services which are disallowed by SELinux policy. */
f8b01f
+              ++i;
f8b01f
+              continue;
f8b01f
+            }
f8b01f
+        }
f8b01f
+#endif
f8b01f
       if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
f8b01f
                                            &services[i]))
f8b01f
         {
f8b01f
diff -urN dbus-1.10.24.old/bus/selinux.c dbus-1.10.24/bus/selinux.c
f8b01f
--- dbus-1.10.24.old/bus/selinux.c	2017-07-28 07:24:16.000000000 +0100
f8b01f
+++ dbus-1.10.24/bus/selinux.c	2018-02-13 10:35:14.311477447 +0000
f8b01f
@@ -61,6 +61,9 @@
f8b01f
 /* Store the value telling us if SELinux is enabled in the kernel. */
f8b01f
 static dbus_bool_t selinux_enabled = FALSE;
f8b01f
 
f8b01f
+/* Store the value telling us if SELinux with MLS is enabled in the kernel. */
f8b01f
+static dbus_bool_t selinux_mls_enabled = FALSE;
f8b01f
+
f8b01f
 /* Store an avc_entry_ref to speed AVC decisions. */
f8b01f
 static struct avc_entry_ref aeref;
f8b01f
 
f8b01f
@@ -273,6 +276,20 @@
f8b01f
 }
f8b01f
 
f8b01f
 /**
f8b01f
+ * Return whether or not SELinux with MLS support is enabled; must be
f8b01f
+ * called after bus_selinux_init.
f8b01f
+ */
f8b01f
+dbus_bool_t
f8b01f
+bus_selinux_mls_enabled (void)
f8b01f
+{
f8b01f
+#ifdef HAVE_SELINUX
f8b01f
+  return selinux_mls_enabled;
f8b01f
+#else
f8b01f
+  return FALSE;
f8b01f
+#endif /* HAVE_SELINUX */
f8b01f
+}
f8b01f
+
f8b01f
+/**
f8b01f
  * Do early initialization; determine whether SELinux is enabled.
f8b01f
  */
f8b01f
 dbus_bool_t
f8b01f
@@ -292,6 +309,16 @@
f8b01f
     }
f8b01f
 
f8b01f
   selinux_enabled = r != 0;
f8b01f
+
f8b01f
+  r = is_selinux_mls_enabled ();
f8b01f
+  if (r < 0)
f8b01f
+    {
f8b01f
+      _dbus_warn ("Could not tell if SELinux MLS is enabled: %s\n",
f8b01f
+                  _dbus_strerror (errno));
f8b01f
+      return FALSE;
f8b01f
+    }
f8b01f
+
f8b01f
+  selinux_mls_enabled = r != 0;
f8b01f
   return TRUE;
f8b01f
 #else
f8b01f
   return TRUE;
f8b01f
@@ -304,14 +331,18 @@
f8b01f
  */
f8b01f
 /* security dbus class constants */
f8b01f
 #define SECCLASS_DBUS       1
f8b01f
+#define SECCLASS_CONTEXT    2
f8b01f
 
f8b01f
 /* dbus's per access vector constants */
f8b01f
 #define DBUS__ACQUIRE_SVC   1
f8b01f
 #define DBUS__SEND_MSG      2
f8b01f
 
f8b01f
+#define CONTEXT__CONTAINS   1
f8b01f
+
f8b01f
 #ifdef HAVE_SELINUX
f8b01f
 static struct security_class_mapping dbus_map[] = {
f8b01f
   { "dbus", { "acquire_svc", "send_msg", NULL } },
f8b01f
+  { "context", { "contains", NULL } },
f8b01f
   { NULL }
f8b01f
 };
f8b01f
 #endif /* HAVE_SELINUX */
f8b01f
@@ -734,6 +765,102 @@
f8b01f
 #endif /* HAVE_SELINUX */
f8b01f
 
f8b01f
 /**
f8b01f
+ * Check if SELinux security controls allow one connection to determine the
f8b01f
+ * name of the other, taking into account MLS considerations.
f8b01f
+ *
f8b01f
+ * @param source the requester of the name.
f8b01f
+ * @param destination the name being requested.
f8b01f
+ * @returns whether the name should be visible by the source of the request
f8b01f
+ */
f8b01f
+dbus_bool_t
f8b01f
+bus_selinux_allows_name (DBusConnection     *source,
f8b01f
+                         DBusConnection     *destination,
f8b01f
+                         DBusError          *error)
f8b01f
+{
f8b01f
+#ifdef HAVE_SELINUX
f8b01f
+  int err;
f8b01f
+  char *policy_type;
f8b01f
+  unsigned long spid, tpid;
f8b01f
+  BusSELinuxID *source_sid;
f8b01f
+  BusSELinuxID *dest_sid;
f8b01f
+  dbus_bool_t ret;
f8b01f
+  dbus_bool_t string_alloced;
f8b01f
+  DBusString auxdata;
f8b01f
+
f8b01f
+  if (!selinux_mls_enabled)
f8b01f
+    return TRUE;
f8b01f
+
f8b01f
+  err = selinux_getpolicytype (&policy_type);
f8b01f
+  if (err < 0)
f8b01f
+    {
f8b01f
+      dbus_set_error_const (error, DBUS_ERROR_IO_ERROR,
f8b01f
+                            "Failed to get SELinux policy type");
f8b01f
+      return FALSE;
f8b01f
+    }
f8b01f
+
f8b01f
+  /* Only check against MLS policy if running under that policy. */
f8b01f
+  if (strcmp (policy_type, "mls") != 0)
f8b01f
+    {
f8b01f
+      free (policy_type);
f8b01f
+      return TRUE;
f8b01f
+    }
f8b01f
+
f8b01f
+  free (policy_type);
f8b01f
+
f8b01f
+  _dbus_assert (source != NULL);
f8b01f
+  _dbus_assert (destination != NULL);
f8b01f
+
f8b01f
+  if (!source || !dbus_connection_get_unix_process_id (source, &spid))
f8b01f
+    spid = 0;
f8b01f
+  if (!destination || !dbus_connection_get_unix_process_id (destination, &tpid))
f8b01f
+    tpid = 0;
f8b01f
+
f8b01f
+  string_alloced = FALSE;
f8b01f
+  if (!_dbus_string_init (&auxdata))
f8b01f
+    goto oom;
f8b01f
+  string_alloced = TRUE;
f8b01f
+
f8b01f
+  if (spid)
f8b01f
+    {
f8b01f
+      if (!_dbus_string_append (&auxdata, " spid="))
f8b01f
+	goto oom;
f8b01f
+
f8b01f
+      if (!_dbus_string_append_uint (&auxdata, spid))
f8b01f
+	goto oom;
f8b01f
+    }
f8b01f
+
f8b01f
+  if (tpid)
f8b01f
+    {
f8b01f
+      if (!_dbus_string_append (&auxdata, " tpid="))
f8b01f
+	goto oom;
f8b01f
+
f8b01f
+      if (!_dbus_string_append_uint (&auxdata, tpid))
f8b01f
+	goto oom;
f8b01f
+    }
f8b01f
+
f8b01f
+  source_sid = bus_connection_get_selinux_id (source);
f8b01f
+  dest_sid = bus_connection_get_selinux_id (destination);
f8b01f
+
f8b01f
+  ret = bus_selinux_check (source_sid,
f8b01f
+                           dest_sid,
f8b01f
+                           SECCLASS_CONTEXT,
f8b01f
+                           CONTEXT__CONTAINS,
f8b01f
+                           &auxdata);
f8b01f
+
f8b01f
+  _dbus_string_free (&auxdata);
f8b01f
+  return ret;
f8b01f
+
f8b01f
+ oom:
f8b01f
+  if (string_alloced)
f8b01f
+    _dbus_string_free (&auxdata);
f8b01f
+  BUS_SET_OOM (error);
f8b01f
+  return FALSE;
f8b01f
+#else
f8b01f
+  return TRUE;
f8b01f
+#endif /* HAVE_SELINUX */
f8b01f
+}
f8b01f
+
f8b01f
+/**
f8b01f
  * Read the SELinux ID from the connection.
f8b01f
  *
f8b01f
  * @param connection the connection to read from
f8b01f
Binary files dbus-1.10.24.old/bus/.selinux.c.swp and dbus-1.10.24/bus/.selinux.c.swp differ
f8b01f
diff -urN dbus-1.10.24.old/bus/selinux.h dbus-1.10.24/bus/selinux.h
f8b01f
--- dbus-1.10.24.old/bus/selinux.h	2017-07-28 07:24:16.000000000 +0100
f8b01f
+++ dbus-1.10.24/bus/selinux.h	2018-02-13 10:15:09.573439444 +0000
f8b01f
@@ -32,6 +32,7 @@
f8b01f
 void        bus_selinux_shutdown (void);
f8b01f
 
f8b01f
 dbus_bool_t bus_selinux_enabled  (void);
f8b01f
+dbus_bool_t bus_selinux_mls_enabled (void);
f8b01f
 
f8b01f
 void bus_selinux_id_ref    (BusSELinuxID *sid);
f8b01f
 void bus_selinux_id_unref  (BusSELinuxID *sid);
f8b01f
@@ -54,6 +55,10 @@
f8b01f
 						const char     *service_name,
f8b01f
 						DBusError      *error);
f8b01f
 
f8b01f
+dbus_bool_t bus_selinux_allows_name            (DBusConnection *source,
f8b01f
+                                                DBusConnection *destination,
f8b01f
+                                                DBusError      *error);
f8b01f
+
f8b01f
 dbus_bool_t bus_selinux_allows_send            (DBusConnection *sender,
f8b01f
                                                 DBusConnection *proposed_recipient,
f8b01f
 						const char     *msgtype, /* Supplementary audit data */