neil / rpms / udisks2

Forked from rpms/udisks2 a year ago
Clone
DistroBaker 2fccc8
commit e062c17e3829f3c04c25b5f6fc17ccc4491befa8
DistroBaker 2fccc8
Author: Tomas Bzatek <tbzatek@redhat.com>
DistroBaker 2fccc8
Date:   Tue Mar 23 16:48:08 2021 +0100
DistroBaker 2fccc8
DistroBaker 2fccc8
    modules: Limit module name to alphanumeric characters and -_ separators
DistroBaker 2fccc8
    
DistroBaker 2fccc8
    A hardening feature as long as the module name is directly involved
DistroBaker 2fccc8
    in filename creation.
DistroBaker 2fccc8
DistroBaker 2fccc8
diff --git a/doc/udisks2-sections.txt.daemon.sections.in b/doc/udisks2-sections.txt.daemon.sections.in
DistroBaker 2fccc8
index 16eaf74e..204ca897 100644
DistroBaker 2fccc8
--- a/doc/udisks2-sections.txt.daemon.sections.in
DistroBaker 2fccc8
+++ b/doc/udisks2-sections.txt.daemon.sections.in
DistroBaker 2fccc8
@@ -312,6 +312,7 @@ udisks_daemon_util_get_free_mdraid_device
DistroBaker 2fccc8
 udisks_ata_identify_get_word
DistroBaker 2fccc8
 udisks_daemon_util_trigger_uevent
DistroBaker 2fccc8
 udisks_daemon_util_trigger_uevent_sync
DistroBaker 2fccc8
+udisks_module_validate_name
DistroBaker 2fccc8
 </SECTION>
DistroBaker 2fccc8
 
DistroBaker 2fccc8
 <SECTION>
DistroBaker 2fccc8
diff --git a/src/udisksconfigmanager.c b/src/udisksconfigmanager.c
DistroBaker 2fccc8
index 9558e276..5868e864 100644
DistroBaker 2fccc8
--- a/src/udisksconfigmanager.c
DistroBaker 2fccc8
+++ b/src/udisksconfigmanager.c
DistroBaker 2fccc8
@@ -26,6 +26,7 @@
DistroBaker 2fccc8
 #include "udiskslogging.h"
DistroBaker 2fccc8
 #include "udisksdaemontypes.h"
DistroBaker 2fccc8
 #include "udisksconfigmanager.h"
DistroBaker 2fccc8
+#include "udisksdaemonutil.h"
DistroBaker 2fccc8
 
DistroBaker 2fccc8
 struct _UDisksConfigManager {
DistroBaker 2fccc8
   GObject parent_instance;
DistroBaker 2fccc8
@@ -60,6 +61,8 @@ enum
DistroBaker 2fccc8
 #define DEFAULTS_GROUP_NAME "defaults"
DistroBaker 2fccc8
 #define DEFAULTS_ENCRYPTION_KEY "encryption"
DistroBaker 2fccc8
 
DistroBaker 2fccc8
+#define MODULES_ALL_ARG "*"
DistroBaker 2fccc8
+
DistroBaker 2fccc8
 static void
DistroBaker 2fccc8
 udisks_config_manager_get_property (GObject    *object,
DistroBaker 2fccc8
                                     guint       property_id,
DistroBaker 2fccc8
@@ -170,7 +173,16 @@ parse_config_file (UDisksConfigManager         *manager,
DistroBaker 2fccc8
             {
DistroBaker 2fccc8
               modules_tmp = modules;
DistroBaker 2fccc8
               for (module_i = *modules_tmp; module_i; module_i = *++modules_tmp)
DistroBaker 2fccc8
-                *out_modules = g_list_append (*out_modules, g_strdup (g_strstrip (module_i)));
DistroBaker 2fccc8
+                {
DistroBaker 2fccc8
+                  g_strstrip (module_i);
DistroBaker 2fccc8
+                  if (! udisks_module_validate_name (module_i) && !g_str_equal (module_i, MODULES_ALL_ARG))
DistroBaker 2fccc8
+                    {
DistroBaker 2fccc8
+                      g_warning ("Invalid module name '%s' specified in the %s config file.",
DistroBaker 2fccc8
+                                 module_i, conf_filename);
DistroBaker 2fccc8
+                      continue;
DistroBaker 2fccc8
+                    }
DistroBaker 2fccc8
+                  *out_modules = g_list_append (*out_modules, g_strdup (module_i));
DistroBaker 2fccc8
+                }
DistroBaker 2fccc8
               g_strfreev (modules);
DistroBaker 2fccc8
             }
DistroBaker 2fccc8
         }
DistroBaker 2fccc8
@@ -397,7 +409,7 @@ udisks_config_manager_get_modules_all (UDisksConfigManager *manager)
DistroBaker 2fccc8
 
DistroBaker 2fccc8
   parse_config_file (manager, NULL, NULL, &modules);
DistroBaker 2fccc8
 
DistroBaker 2fccc8
-  ret = !modules || (g_strcmp0 (modules->data, "*") == 0 && g_list_length (modules) == 1);
DistroBaker 2fccc8
+  ret = !modules || (g_strcmp0 (modules->data, MODULES_ALL_ARG) == 0 && g_list_length (modules) == 1);
DistroBaker 2fccc8
 
DistroBaker 2fccc8
   g_list_free_full (modules, (GDestroyNotify) g_free);
DistroBaker 2fccc8
 
DistroBaker 2fccc8
diff --git a/src/udisksdaemonutil.c b/src/udisksdaemonutil.c
DistroBaker 2fccc8
index 60134765..1695b524 100644
DistroBaker 2fccc8
--- a/src/udisksdaemonutil.c
DistroBaker 2fccc8
+++ b/src/udisksdaemonutil.c
DistroBaker 2fccc8
@@ -1880,3 +1880,29 @@ udisks_daemon_util_trigger_uevent_sync (UDisksDaemon *daemon,
DistroBaker 2fccc8
 }
DistroBaker 2fccc8
 
DistroBaker 2fccc8
 /* ---------------------------------------------------------------------------------------------------- */
DistroBaker 2fccc8
+
DistroBaker 2fccc8
+/**
DistroBaker 2fccc8
+ * udisks_module_validate_name:
DistroBaker 2fccc8
+ * @module_name: A udisks2 module name.
DistroBaker 2fccc8
+ *
DistroBaker 2fccc8
+ * Checks the string for a valid udisks2 module name. Only alphanumeric characters
DistroBaker 2fccc8
+ * along with the '-' and '_' separators are permitted.
DistroBaker 2fccc8
+ *
DistroBaker 2fccc8
+ * Returns: %TRUE if the string is a valid udisks2 module name, %FALSE otherwise.
DistroBaker 2fccc8
+ */
DistroBaker 2fccc8
+gboolean
DistroBaker 2fccc8
+udisks_module_validate_name (const gchar *module_name)
DistroBaker 2fccc8
+{
DistroBaker 2fccc8
+  int i;
DistroBaker 2fccc8
+
DistroBaker 2fccc8
+  for (i = 0; module_name[i] != '\0'; i++)
DistroBaker 2fccc8
+    /* going ASCII, will disqualify any UTF-* string */
DistroBaker 2fccc8
+    if (! g_ascii_isalnum (module_name[i]) &&
DistroBaker 2fccc8
+        module_name[i] != '-' &&
DistroBaker 2fccc8
+        module_name[i] != '_')
DistroBaker 2fccc8
+      return FALSE;
DistroBaker 2fccc8
+
DistroBaker 2fccc8
+  return TRUE;
DistroBaker 2fccc8
+}
DistroBaker 2fccc8
+
DistroBaker 2fccc8
+/* ---------------------------------------------------------------------------------------------------- */
DistroBaker 2fccc8
diff --git a/src/udisksdaemonutil.h b/src/udisksdaemonutil.h
DistroBaker 2fccc8
index 2d7ac981..df584de4 100644
DistroBaker 2fccc8
--- a/src/udisksdaemonutil.h
DistroBaker 2fccc8
+++ b/src/udisksdaemonutil.h
DistroBaker 2fccc8
@@ -129,6 +129,8 @@ gchar *udisks_daemon_util_get_free_mdraid_device (void);
DistroBaker 2fccc8
 
DistroBaker 2fccc8
 guint16 udisks_ata_identify_get_word (const guchar *identify_data, guint word_number);
DistroBaker 2fccc8
 
DistroBaker 2fccc8
+gboolean udisks_module_validate_name (const gchar *module_name);
DistroBaker 2fccc8
+
DistroBaker 2fccc8
 /* Utility macro for policy verification. */
DistroBaker 2fccc8
 #define UDISKS_DAEMON_CHECK_AUTHORIZATION(daemon,                   \
DistroBaker 2fccc8
                                           object,                   \
DistroBaker 2fccc8
diff --git a/src/udiskslinuxmanager.c b/src/udiskslinuxmanager.c
DistroBaker 2fccc8
index 8af65d97..26d8a5d7 100644
DistroBaker 2fccc8
--- a/src/udiskslinuxmanager.c
DistroBaker 2fccc8
+++ b/src/udiskslinuxmanager.c
DistroBaker 2fccc8
@@ -956,6 +956,15 @@ handle_enable_module (UDisksManager         *object,
DistroBaker 2fccc8
   UDisksLinuxManager *manager = UDISKS_LINUX_MANAGER (object);
DistroBaker 2fccc8
   EnableModulesData *data;
DistroBaker 2fccc8
 
DistroBaker 2fccc8
+  if (! udisks_module_validate_name (arg_name))
DistroBaker 2fccc8
+    {
DistroBaker 2fccc8
+      g_dbus_method_invocation_return_error (invocation,
DistroBaker 2fccc8
+                                             G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
DistroBaker 2fccc8
+                                             "Requested module name '%s' is not a valid udisks2 module name.",
DistroBaker 2fccc8
+                                             arg_name);
DistroBaker 2fccc8
+      return TRUE;
DistroBaker 2fccc8
+    }
DistroBaker 2fccc8
+
DistroBaker 2fccc8
   if (! arg_enable)
DistroBaker 2fccc8
     {
DistroBaker 2fccc8
       /* TODO: implement proper module unloading */