orion / rpms / dbus

Forked from rpms/dbus 2 years ago
Clone
d790e3
From 8f382ee405ec68850866298ba0574f12e261a6fa Mon Sep 17 00:00:00 2001
d790e3
From: Simon McVittie <smcv@collabora.com>
d790e3
Date: Tue, 13 Sep 2022 15:10:22 +0100
d790e3
Subject: [PATCH] dbus-marshal-validate: Check brackets in signature nest
d790e3
 correctly
d790e3
d790e3
In debug builds with assertions enabled, a signature with incorrectly
d790e3
nested `()` and `{}`, for example `a{i(u}` or `(a{ii)}`, could result
d790e3
in an assertion failure.
d790e3
d790e3
In production builds without assertions enabled, a signature with
d790e3
incorrectly nested `()` and `{}` could potentially result in a crash
d790e3
or incorrect message parsing, although we do not have a concrete example
d790e3
of either of these failure modes.
d790e3
d790e3
Thanks: Evgeny Vereshchagin
d790e3
Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/418
d790e3
Resolves: CVE-2022-42010
d790e3
Signed-off-by: Simon McVittie <smcv@collabora.com>
d790e3
(cherry picked from commit 9d07424e9011e3bbe535e83043d335f3093d2916)
d790e3
(cherry picked from commit 3e53a785dee8d1432156188a2c4260e4cbc78c4d)
d790e3
---
d790e3
 dbus/dbus-marshal-validate.c | 38 +++++++++++++++++++++++++++++++++++-
d790e3
 1 file changed, 37 insertions(+), 1 deletion(-)
d790e3
d790e3
diff --git a/dbus/dbus-marshal-validate.c b/dbus/dbus-marshal-validate.c
d790e3
index 4d492f3f3..ae68414dd 100644
d790e3
--- a/dbus/dbus-marshal-validate.c
d790e3
+++ b/dbus/dbus-marshal-validate.c
d790e3
@@ -62,6 +62,8 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
 
d790e3
   int element_count;
d790e3
   DBusList *element_count_stack;
d790e3
+  char opened_brackets[DBUS_MAXIMUM_TYPE_RECURSION_DEPTH * 2 + 1] = { '\0' };
d790e3
+  char last_bracket;
d790e3
 
d790e3
   result = DBUS_VALID;
d790e3
   element_count_stack = NULL;
d790e3
@@ -93,6 +95,10 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
 
d790e3
   while (p != end)
d790e3
     {
d790e3
+      _dbus_assert (struct_depth + dict_entry_depth >= 0);
d790e3
+      _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
d790e3
+      _dbus_assert (opened_brackets[struct_depth + dict_entry_depth] == '\0');
d790e3
+
d790e3
       switch (*p)
d790e3
         {
d790e3
         case DBUS_TYPE_BYTE:
d790e3
@@ -136,6 +142,10 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
               goto out;
d790e3
             }
d790e3
 
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth >= 1);
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
d790e3
+          _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
d790e3
+          opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_STRUCT_BEGIN_CHAR;
d790e3
           break;
d790e3
 
d790e3
         case DBUS_STRUCT_END_CHAR:
d790e3
@@ -151,9 +161,20 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
               goto out;
d790e3
             }
d790e3
 
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth >= 1);
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
d790e3
+          last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
d790e3
+
d790e3
+          if (last_bracket != DBUS_STRUCT_BEGIN_CHAR)
d790e3
+            {
d790e3
+              result = DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED;
d790e3
+              goto out;
d790e3
+            }
d790e3
+
d790e3
           _dbus_list_pop_last (&element_count_stack);
d790e3
 
d790e3
           struct_depth -= 1;
d790e3
+          opened_brackets[struct_depth + dict_entry_depth] = '\0';
d790e3
           break;
d790e3
 
d790e3
         case DBUS_DICT_ENTRY_BEGIN_CHAR:
d790e3
@@ -178,6 +199,10 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
               goto out;
d790e3
             }
d790e3
 
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth >= 1);
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
d790e3
+          _dbus_assert (opened_brackets[struct_depth + dict_entry_depth - 1] == '\0');
d790e3
+          opened_brackets[struct_depth + dict_entry_depth - 1] = DBUS_DICT_ENTRY_BEGIN_CHAR;
d790e3
           break;
d790e3
 
d790e3
         case DBUS_DICT_ENTRY_END_CHAR:
d790e3
@@ -186,8 +211,19 @@ _dbus_validate_signature_with_reason (const DBusString *type_str,
d790e3
               result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
d790e3
               goto out;
d790e3
             }
d790e3
-            
d790e3
+
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth >= 1);
d790e3
+          _dbus_assert (struct_depth + dict_entry_depth < _DBUS_N_ELEMENTS (opened_brackets));
d790e3
+          last_bracket = opened_brackets[struct_depth + dict_entry_depth - 1];
d790e3
+
d790e3
+          if (last_bracket != DBUS_DICT_ENTRY_BEGIN_CHAR)
d790e3
+            {
d790e3
+              result = DBUS_INVALID_DICT_ENTRY_ENDED_BUT_NOT_STARTED;
d790e3
+              goto out;
d790e3
+            }
d790e3
+
d790e3
           dict_entry_depth -= 1;
d790e3
+          opened_brackets[struct_depth + dict_entry_depth] = '\0';
d790e3
 
d790e3
           element_count = 
d790e3
             _DBUS_POINTER_TO_INT (_dbus_list_pop_last (&element_count_stack));
d790e3
-- 
d790e3
GitLab
d790e3