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