cef7c3
From 89b522ed31837cb2ac107a8961fbb0f2c7fc7ccb Mon Sep 17 00:00:00 2001
cef7c3
From: Krzesimir Nowak <qdlacz@gmail.com>
cef7c3
Date: Wed, 10 Feb 2021 23:51:07 +0100
cef7c3
Subject: [PATCH] gbytearray: Do not accept too large byte arrays
cef7c3
cef7c3
GByteArray uses guint for storing the length of the byte array, but it
cef7c3
also has a constructor (g_byte_array_new_take) that takes length as a
cef7c3
gsize. gsize may be larger than guint (64 bits for gsize vs 32 bits
cef7c3
for guint). It is possible to call the function with a value greater
cef7c3
than G_MAXUINT, which will result in silent length truncation. This
cef7c3
may happen as a result of unreffing GBytes into GByteArray, so rather
cef7c3
be loud about it.
cef7c3
cef7c3
(Test case tweaked by Philip Withnall.)
cef7c3
---
cef7c3
 glib/garray.c      |  6 ++++++
cef7c3
 glib/gbytes.c      |  4 ++++
cef7c3
 glib/tests/bytes.c | 37 +++++++++++++++++++++++++++++++++++--
cef7c3
 3 files changed, 45 insertions(+), 2 deletions(-)
cef7c3
cef7c3
diff --git a/glib/garray.c b/glib/garray.c
cef7c3
index aa3c04707..271d85ad8 100644
cef7c3
--- a/glib/garray.c
cef7c3
+++ b/glib/garray.c
cef7c3
@@ -1666,6 +1666,10 @@ g_byte_array_new (void)
cef7c3
  * Create byte array containing the data. The data will be owned by the array
cef7c3
  * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
cef7c3
  *
cef7c3
+ * Do not use it if @len is greater than %G_MAXUINT. #GByteArray
cef7c3
+ * stores the length of its data in #guint, which may be shorter than
cef7c3
+ * #gsize.
cef7c3
+ *
cef7c3
  * Since: 2.32
cef7c3
  *
cef7c3
  * Returns: (transfer full): a new #GByteArray
cef7c3
@@ -1677,6 +1681,8 @@ g_byte_array_new_take (guint8 *data,
cef7c3
   GByteArray *array;
cef7c3
   GRealArray *real;
cef7c3
 
cef7c3
+  g_return_val_if_fail (len <= G_MAXUINT, NULL);
cef7c3
+
cef7c3
   array = g_byte_array_new ();
cef7c3
   real = (GRealArray *)array;
cef7c3
   g_assert (real->data == NULL);
cef7c3
diff --git a/glib/gbytes.c b/glib/gbytes.c
cef7c3
index 5141170d7..635b79535 100644
cef7c3
--- a/glib/gbytes.c
cef7c3
+++ b/glib/gbytes.c
cef7c3
@@ -512,6 +512,10 @@ g_bytes_unref_to_data (GBytes *bytes,
cef7c3
  * g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
cef7c3
  * other cases the data is copied.
cef7c3
  *
cef7c3
+ * Do not use it if @bytes contains more than %G_MAXUINT
cef7c3
+ * bytes. #GByteArray stores the length of its data in #guint, which
cef7c3
+ * may be shorter than #gsize, that @bytes is using.
cef7c3
+ *
cef7c3
  * Returns: (transfer full): a new mutable #GByteArray containing the same byte data
cef7c3
  *
cef7c3
  * Since: 2.32
cef7c3
diff --git a/glib/tests/bytes.c b/glib/tests/bytes.c
cef7c3
index 5ea5c2b35..42281307b 100644
cef7c3
--- a/glib/tests/bytes.c
cef7c3
+++ b/glib/tests/bytes.c
cef7c3
@@ -10,12 +10,12 @@
cef7c3
  */
cef7c3
 
cef7c3
 #undef G_DISABLE_ASSERT
cef7c3
-#undef G_LOG_DOMAIN
cef7c3
 
cef7c3
 #include <stdio.h>
cef7c3
 #include <stdlib.h>
cef7c3
 #include <string.h>
cef7c3
 #include "glib.h"
cef7c3
+#include "glib/gstrfuncsprivate.h"
cef7c3
 
cef7c3
 /* Keep in sync with glib/gbytes.c */
cef7c3
 struct _GBytes
cef7c3
@@ -333,6 +333,38 @@ test_to_array_transferred (void)
cef7c3
   g_byte_array_unref (array);
cef7c3
 }
cef7c3
 
cef7c3
+static void
cef7c3
+test_to_array_transferred_oversize (void)
cef7c3
+{
cef7c3
+  g_test_message ("g_bytes_unref_to_array() can only take GBytes up to "
cef7c3
+                  "G_MAXUINT in length; test that longer ones are rejected");
cef7c3
+
cef7c3
+  if (sizeof (guint) >= sizeof (gsize))
cef7c3
+    {
cef7c3
+      g_test_skip ("Skipping test as guint is not smaller than gsize");
cef7c3
+    }
cef7c3
+  else if (g_test_undefined ())
cef7c3
+    {
cef7c3
+      GByteArray *array = NULL;
cef7c3
+      GBytes *bytes = NULL;
cef7c3
+      gpointer data = g_memdup2 (NYAN, N_NYAN);
cef7c3
+      gsize len = ((gsize) G_MAXUINT) + 1;
cef7c3
+
cef7c3
+      bytes = g_bytes_new_take (data, len);
cef7c3
+      g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
cef7c3
+                             "g_byte_array_new_take: assertion 'len <= G_MAXUINT' failed");
cef7c3
+      array = g_bytes_unref_to_array (g_steal_pointer (&bytes));
cef7c3
+      g_test_assert_expected_messages ();
cef7c3
+      g_assert_null (array);
cef7c3
+
cef7c3
+      g_free (data);
cef7c3
+    }
cef7c3
+  else
cef7c3
+    {
cef7c3
+      g_test_skip ("Skipping test as testing undefined behaviour is disabled");
cef7c3
+    }
cef7c3
+}
cef7c3
+
cef7c3
 static void
cef7c3
 test_to_array_two_refs (void)
cef7c3
 {
cef7c3
@@ -407,7 +439,8 @@ main (int argc, char *argv[])
cef7c3
   g_test_add_func ("/bytes/to-data/transfered", test_to_data_transferred);
cef7c3
   g_test_add_func ("/bytes/to-data/two-refs", test_to_data_two_refs);
cef7c3
   g_test_add_func ("/bytes/to-data/non-malloc", test_to_data_non_malloc);
cef7c3
-  g_test_add_func ("/bytes/to-array/transfered", test_to_array_transferred);
cef7c3
+  g_test_add_func ("/bytes/to-array/transferred", test_to_array_transferred);
cef7c3
+  g_test_add_func ("/bytes/to-array/transferred-oversize", test_to_array_transferred_oversize);
cef7c3
   g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs);
cef7c3
   g_test_add_func ("/bytes/to-array/non-malloc", test_to_array_non_malloc);
cef7c3
   g_test_add_func ("/bytes/null", test_null);
cef7c3
-- 
cef7c3
2.31.1
6de84d