Skip to contentSkip to conversation

Improved D-BUS SELinux mediation

PATCH New Colin Walters 1 participant 1 message Nov 11, 2004 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improved D-BUS SELinux mediation

Hi,

The attached patch improves on the previous patch sent to the D-BUS list for also having D-BUS check service labels for the send_msg permission. One problem I ran into is that send_msg also mediates reply messages; so in order to have a domain just reply to a sent message, it needs the general send_msg back to the sender domain, as well as the service
domain. The solution is to add a separate "reply" access vector.
D-BUS will check that permission if a message is in reply to a known sent message, otherwise it will use "send_msg".

While I was changing the access vectors, I thought I would go ahead and add an "activate_svc" vector too (and add the requisite check in the D- BUS code). The long term plan I think is to use D-BUS for system service activation too, and it seems undesirable for e.g. CUPS to have the permission to activate your mail server (which could have other side effects like binding to ports, etc).

The patch to D-BUS is first, and the patch to the Flask access_vectors follows.

I'll be working on a patch to update the policy itself to handle these new changes soon, along with labeling all the services we currently use.

? dbus-0.22.tar.gz
cvs diff: Diffing .
Index: configure.in
===================================================================
RCS file: /cvs/dbus/dbus/configure.in,v
retrieving revision 1.100

configure.in +3 -3
diff -u -d -r1.100 configure.in
@@ -698,13 +698,13 @@
     AC_CHECK_LIB(selinux, is_selinux_enabled, 
                  have_selinux=yes, have_selinux=no)
 
-    # see if we have the SELinux header with the new D-BUS stuff in it
+    # see if we have the SELinux header with new enough D-BUS stuff in it
     if test x$have_selinux = xyes ; then
         AC_MSG_CHECKING([for DBUS Flask permissions in selinux/av_permissions.h])
 	AC_TRY_COMPILE([#include <selinux/av_permissions.h>],
-			[#ifdef DBUS__ACQUIRE_SVC return 0;
+			[#ifdef DBUS__ACTIVATE_SVC return 0;
 			 #else
-			 #error DBUS__ACQUIRE_SVC not defined
+			 #error DBUS__ACTIVATE_SVC not defined
 			 #endif],
                         have_selinux=yes, have_selinux=no)
 	AC_MSG_RESULT($have_selinux)

cvs diff: Diffing bus
Index: bus/activation.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/activation.c,v
retrieving revision 1.36

activation.c +19 -2
diff -u -d -r1.36 activation.c
@@ -25,6 +25,7 @@
 #include "activation.h"
 #include "desktop-file.h"
 #include "services.h"
+#include "selinux.h"
 #include "test.h"
 #include "utils.h"
 #include <dbus/dbus-internals.h>
@@ -1297,6 +1298,8 @@
   BusPendingActivationEntry *pending_activation_entry;
   DBusMessage *message;
   DBusString service_str;
+  BusSELinuxID *service_sid;
+  BusRegistry *bus_registry;
   char *argv[2];
   dbus_bool_t retval;
   DBusHashIter iter;
@@ -1319,14 +1322,16 @@
   if (!entry) 
     return FALSE;
 
+  bus_registry = bus_context_get_registry (activation->context);
+  _dbus_string_init_const (&service_str, service_name);
+
   /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
    * call us if the service is already active.
    */
   if (!auto_activation)
     {
       /* Check if the service is active */
-      _dbus_string_init_const (&service_str, service_name);
-      if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
+      if (bus_registry_lookup (bus_registry, &service_str) != NULL)
         {
           _dbus_verbose ("Service \"%s\" is already active\n", service_name);
       
@@ -1361,6 +1366,18 @@
         }
     }
 
+  service_sid = bus_registry_get_service_selinux_id (bus_registry, &service_str);
+  if (!bus_selinux_allows_activate_service (connection, service_sid,
+					    service_name))
+    {
+      dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+		      "SELinux policy prevents this sender "
+		      "from activating the service %s",
+		      service_name);
+      _dbus_verbose ("SELinux security check denying activation\n");
+      return FALSE;
+    }
+
   pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
   if (!pending_activation_entry)
     {

Index: bus/bus.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/bus.c,v
retrieving revision 1.59

bus.c +65 -32

124 lines not on this page · raw message

Index: bus/config-parser.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/config-parser.c,v
retrieving revision 1.39

config-parser.c +47 -3

86 lines not on this page · raw message

Index: bus/config-parser.h
===================================================================
RCS file: /cvs/dbus/dbus/bus/config-parser.h,v
retrieving revision 1.17

config-parser.h +2 -1
diff -u -d -r1.17 config-parser.h
@@ -71,7 +71,8 @@
 void        bus_config_parser_get_limits       (BusConfigParser *parser,
                                                 BusLimits       *limits);
 
-DBusHashTable* bus_config_parser_steal_service_context_table (BusConfigParser *parser);
+DBusHashTable*   bus_config_parser_steal_service_context_table      (BusConfigParser *parser);
+char         *   bus_config_parser_steal_unlabeled_service_context  (BusConfigParser *parser);
 
 /* Loader functions (backended off one of the XML parsers).  Returns a
  * finished ConfigParser.

Index: bus/selinux.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/selinux.c,v
retrieving revision 1.12

selinux.c +243 -92

485 lines not on this page · raw message

Index: bus/selinux.h
===================================================================
RCS file: /cvs/dbus/dbus/bus/selinux.h,v
retrieving revision 1.7

selinux.h +19 -10
diff -u -d -r1.7 selinux.h
@@ -27,6 +27,8 @@
 #include <dbus/dbus-connection.h>
 #include "services.h"
 
+typedef struct BusSELinuxData BusSELinuxData;
+
 dbus_bool_t bus_selinux_pre_init (void);
 dbus_bool_t bus_selinux_full_init(void);
 void        bus_selinux_shutdown (void);
@@ -36,26 +38,33 @@
 void bus_selinux_id_ref    (BusSELinuxID *sid);
 void bus_selinux_id_unref  (BusSELinuxID *sid);
 
-DBusHashTable* bus_selinux_id_table_new    (void);
-BusSELinuxID*  bus_selinux_id_table_lookup (DBusHashTable    *service_table,
-                                            const DBusString *service_name);
-dbus_bool_t    bus_selinux_id_table_insert (DBusHashTable    *service_table,
-                                            const char       *service_name,
-                                            const char       *service_context);
-void           bus_selinux_id_table_print  (DBusHashTable    *service_table);
-const char*    bus_selinux_get_policy_root (void);
-
+BusSELinuxData* bus_selinux_data_new            (const char             *default_context);
+BusSELinuxID*   bus_selinux_data_lookup_service (const BusSELinuxData   *data,
+						 const DBusString       *service_name);
+dbus_bool_t     bus_selinux_data_insert_service (BusSELinuxData         *selinux_data,
+						 const char             *service_name,
+						 const char             *service_context);
+void            bus_selinux_data_print_services (BusSELinuxData         *selinux_data);
+void            bus_selinux_data_free           (BusSELinuxData         *selinux_data);
+const char*     bus_selinux_get_policy_root     (void);
 
 dbus_bool_t bus_selinux_allows_acquire_service (DBusConnection *connection,
                                                 BusSELinuxID   *service_sid,
 						const char     *service_name);
+dbus_bool_t bus_selinux_allows_activate_service(DBusConnection *connection,
+                                                BusSELinuxID   *service_sid,
+						const char     *service_name);
 dbus_bool_t bus_selinux_allows_send            (DBusConnection *sender,
+						BusSELinuxID   *service_sid,
                                                 DBusConnection *proposed_recipient,
 						const char     *msgtype, /* Supplementary audit data */
 						const char     *interface,
 						const char     *member,
-						const char     *error_name,
 						const char     *destination);
+dbus_bool_t bus_selinux_allows_reply           (DBusConnection *sender,
+                                                DBusConnection *proposed_recipient,
+						const char     *msgtype, /* Supplementary audit data */
+						const char     *error_name);
 
 BusSELinuxID* bus_selinux_init_connection_id (DBusConnection *connection,
                                               DBusError      *error);

Index: bus/services.c
===================================================================
RCS file: /cvs/dbus/dbus/bus/services.c,v
retrieving revision 1.27

services.c +27 -18

89 lines not on this page · raw message

Index: bus/services.h
===================================================================
RCS file: /cvs/dbus/dbus/bus/services.h,v
retrieving revision 1.13

services.h +3 -0
diff -u -d -r1.13 services.h
@@ -57,7 +57,10 @@
                                            BusTransaction              *transaction,
                                            DBusError                   *error);
 dbus_bool_t  bus_registry_set_service_context_table (BusRegistry           *registry,
+						     const char            *unlabeled_service,
 						     DBusHashTable         *table);
+BusSELinuxID*bus_registry_get_service_selinux_id    (BusRegistry           *registry,
+						     const DBusString      *service_name);
 
 BusService*     bus_service_ref                      (BusService     *service);
 void            bus_service_unref                    (BusService     *service);

cvs diff: Diffing dbus
cvs diff: Diffing doc
Index: doc/busconfig.dtd
===================================================================
RCS file: /cvs/dbus/dbus/doc/busconfig.dtd,v
retrieving revision 1.5

busconfig.dtd +2 -0
diff -u -d -r1.5 busconfig.dtd
@@ -55,6 +55,8 @@
 <!ATTLIST limit name CDATA #REQUIRED>
 
 <!ELEMENT selinux (associate)*>
+<!ATTLIST selinux
+          default_context CDATA #IMPLIED>
 <!ELEMENT associate EMPTY>
 <!ATTLIST associate
           own CDATA #REQUIRED

cvs diff: Diffing gcj
cvs diff: Diffing gcj/org
cvs diff: Diffing gcj/org/freedesktop
cvs diff: Diffing gcj/org/freedesktop/dbus
cvs diff: Diffing glib
cvs diff: Diffing mono
cvs diff: Diffing mono/DBusType
cvs diff: Diffing mono/doc
cvs diff: Diffing mono/doc/en
cvs diff: Diffing mono/doc/en/DBus
cvs diff: Diffing mono/doc/en/DBus.DBusType
cvs diff: Diffing mono/example
cvs diff: Diffing python
cvs diff: Diffing python/examples
cvs diff: Diffing python/tests
cvs diff: Diffing qt
cvs diff: Diffing server
cvs diff: Diffing test
cvs diff: Diffing test/data
cvs diff: Diffing test/data/auth
cvs diff: Diffing test/data/equiv-config-files
cvs diff: Diffing test/data/equiv-config-files/basic cvs diff: Diffing test/data/equiv-config-files/basic/basic.d cvs diff: Diffing test/data/equiv-config-files/entities cvs diff: Diffing test/data/equiv-config-files/entities/basic.d cvs diff: Diffing test/data/incomplete-messages
cvs diff: Diffing test/data/invalid-config-files
cvs diff: Diffing test/data/invalid-messages
cvs diff: Diffing test/data/sha-1
cvs diff: Diffing test/data/valid-config-files
cvs diff: Diffing test/data/valid-config-files/basic.d cvs diff: Diffing test/data/valid-config-files/system.d cvs diff: Diffing test/data/valid-introspection-files cvs diff: Diffing test/data/valid-messages
cvs diff: Diffing test/data/valid-service-files
cvs diff: Diffing test/glib
cvs diff: Diffing tools

access_vectors renamed from access_vectors~ +2 -0
@@ -559,6 +559,8 @@
 {
 	acquire_svc
 	send_msg
+	activate_svc
+	reply
 }
 
 # Define the access vector interpretation for controlling