orion / rpms / dbus

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