094c4d
From 7b46597384de916b4027ebaff662d06ff3ea2bc8 Mon Sep 17 00:00:00 2001
094c4d
From: Philip Withnall <pwithnall@endlessos.org>
094c4d
Date: Thu, 4 Feb 2021 13:30:52 +0000
094c4d
Subject: [PATCH 1/6] gstrfuncs: Add internal g_memdup2() function
094c4d
MIME-Version: 1.0
094c4d
Content-Type: text/plain; charset=UTF-8
094c4d
Content-Transfer-Encoding: 8bit
094c4d
094c4d
This will replace the existing `g_memdup()` function for use within
094c4d
GLib. It has an unavoidable security flaw of taking its `byte_size`
094c4d
argument as a `guint` rather than as a `gsize`. Most callers will
094c4d
expect it to be a `gsize`, and may pass in large values which could
094c4d
silently be truncated, resulting in an undersize allocation compared
094c4d
to what the caller expects.
094c4d
094c4d
This could lead to a classic buffer overflow vulnerability for many
094c4d
callers of `g_memdup()`.
094c4d
094c4d
`g_memdup2()`, in comparison, takes its `byte_size` as a `gsize`.
094c4d
094c4d
Spotted by Kevin Backhouse of GHSL.
094c4d
094c4d
In GLib 2.68, `g_memdup2()` will be a new public API. In this version
094c4d
for backport to older stable releases, it’s a new `static inline` API
094c4d
in a private header, so that use of `g_memdup()` within GLib can be
094c4d
fixed without adding a new API in a stable release series.
094c4d
094c4d
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
094c4d
Helps: CVE-2021-27219
094c4d
Helps: GHSL-2021-045
094c4d
Helps: #2319
094c4d
(cherry picked from commit 5e5f75a77e399c638be66d74e5daa8caeb433e00)
094c4d
---
094c4d
 docs/reference/glib/meson.build |  1 +
094c4d
 glib/gstrfuncsprivate.h         | 55 +++++++++++++++++++++++++++++++++
094c4d
 glib/meson.build                |  1 +
094c4d
 glib/tests/strfuncs.c           | 23 ++++++++++++++
094c4d
 4 files changed, 80 insertions(+)
094c4d
 create mode 100644 glib/gstrfuncsprivate.h
094c4d
094c4d
diff --git a/docs/reference/glib/meson.build b/docs/reference/glib/meson.build
094c4d
index f0f915e96..1a3680941 100644
094c4d
--- a/docs/reference/glib/meson.build
094c4d
+++ b/docs/reference/glib/meson.build
094c4d
@@ -20,6 +20,7 @@ if get_option('gtk_doc')
094c4d
     'gprintfint.h',
094c4d
     'gmirroringtable.h',
094c4d
     'gscripttable.h',
094c4d
+    'gstrfuncsprivate.h',
094c4d
     'glib-mirroring-tab',
094c4d
     'gnulib',
094c4d
     'pcre',
094c4d
diff --git a/glib/gstrfuncsprivate.h b/glib/gstrfuncsprivate.h
094c4d
new file mode 100644
094c4d
index 000000000..85c88328a
094c4d
--- /dev/null
094c4d
+++ b/glib/gstrfuncsprivate.h
094c4d
@@ -0,0 +1,55 @@
094c4d
+/* GLIB - Library of useful routines for C programming
094c4d
+ * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
094c4d
+ *
094c4d
+ * This library is free software; you can redistribute it and/or
094c4d
+ * modify it under the terms of the GNU Lesser General Public
094c4d
+ * License as published by the Free Software Foundation; either
094c4d
+ * version 2.1 of the License, or (at your option) any later version.
094c4d
+ *
094c4d
+ * This library is distributed in the hope that it will be useful,
094c4d
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
094c4d
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
094c4d
+ * Lesser General Public License for more details.
094c4d
+ *
094c4d
+ * You should have received a copy of the GNU Lesser General Public
094c4d
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
094c4d
+ */
094c4d
+
094c4d
+#include <glib.h>
094c4d
+#include <string.h>
094c4d
+
094c4d
+/*
094c4d
+ * g_memdup2:
094c4d
+ * @mem: (nullable): the memory to copy.
094c4d
+ * @byte_size: the number of bytes to copy.
094c4d
+ *
094c4d
+ * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
094c4d
+ * from @mem. If @mem is %NULL it returns %NULL.
094c4d
+ *
094c4d
+ * This replaces g_memdup(), which was prone to integer overflows when
094c4d
+ * converting the argument from a #gsize to a #guint.
094c4d
+ *
094c4d
+ * This static inline version is a backport of the new public API from
094c4d
+ * GLib 2.68, kept internal to GLib for backport to older stable releases.
094c4d
+ * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319.
094c4d
+ *
094c4d
+ * Returns: (nullable): a pointer to the newly-allocated copy of the memory,
094c4d
+ *    or %NULL if @mem is %NULL.
094c4d
+ * Since: 2.68
094c4d
+ */
094c4d
+static inline gpointer
094c4d
+g_memdup2 (gconstpointer mem,
094c4d
+           gsize         byte_size)
094c4d
+{
094c4d
+  gpointer new_mem;
094c4d
+
094c4d
+  if (mem && byte_size != 0)
094c4d
+    {
094c4d
+      new_mem = g_malloc (byte_size);
094c4d
+      memcpy (new_mem, mem, byte_size);
094c4d
+    }
094c4d
+  else
094c4d
+    new_mem = NULL;
094c4d
+
094c4d
+  return new_mem;
094c4d
+}
094c4d
diff --git a/glib/meson.build b/glib/meson.build
094c4d
index a2f9da81c..481fd06ff 100644
094c4d
--- a/glib/meson.build
094c4d
+++ b/glib/meson.build
094c4d
@@ -167,6 +167,7 @@ glib_sources = files(
094c4d
   'gslist.c',
094c4d
   'gstdio.c',
094c4d
   'gstrfuncs.c',
094c4d
+  'gstrfuncsprivate.h',
094c4d
   'gstring.c',
094c4d
   'gstringchunk.c',
094c4d
   'gtestutils.c',
094c4d
diff --git a/glib/tests/strfuncs.c b/glib/tests/strfuncs.c
094c4d
index 7e031bdb1..2aa252946 100644
094c4d
--- a/glib/tests/strfuncs.c
094c4d
+++ b/glib/tests/strfuncs.c
094c4d
@@ -32,6 +32,8 @@
094c4d
 #include <string.h>
094c4d
 #include "glib.h"
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
+
094c4d
 #if defined (_MSC_VER) && (_MSC_VER <= 1800)
094c4d
 #define isnan(x) _isnan(x)
094c4d
 
094c4d
@@ -199,6 +201,26 @@ test_is_to_digit (void)
094c4d
   #undef TEST_DIGIT
094c4d
 }
094c4d
 
094c4d
+/* Testing g_memdup2() function with various positive and negative cases */
094c4d
+static void
094c4d
+test_memdup2 (void)
094c4d
+{
094c4d
+  gchar *str_dup = NULL;
094c4d
+  const gchar *str = "The quick brown fox jumps over the lazy dog";
094c4d
+
094c4d
+  /* Testing negative cases */
094c4d
+  g_assert_null (g_memdup2 (NULL, 1024));
094c4d
+  g_assert_null (g_memdup2 (str, 0));
094c4d
+  g_assert_null (g_memdup2 (NULL, 0));
094c4d
+
094c4d
+  /* Testing normal usage cases */
094c4d
+  str_dup = g_memdup2 (str, strlen (str) + 1);
094c4d
+  g_assert_nonnull (str_dup);
094c4d
+  g_assert_cmpstr (str, ==, str_dup);
094c4d
+
094c4d
+  g_free (str_dup);
094c4d
+}
094c4d
+
094c4d
 static void
094c4d
 test_strdup (void)
094c4d
 {
094c4d
@@ -1726,6 +1748,7 @@ main (int   argc,
094c4d
   g_test_init (&argc, &argv, NULL);
094c4d
 
094c4d
   g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
094c4d
+  g_test_add_func ("/strfuncs/memdup2", test_memdup2);
094c4d
   g_test_add_func ("/strfuncs/strdup", test_strdup);
094c4d
   g_test_add_func ("/strfuncs/strndup", test_strndup);
094c4d
   g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
094c4d
-- 
094c4d
2.31.1
094c4d
094c4d
From d6aab169954d9e6e77753dee68e1b3f5932f6dee Mon Sep 17 00:00:00 2001
094c4d
From: Philip Withnall <pwithnall@endlessos.org>
094c4d
Date: Thu, 4 Feb 2021 13:41:21 +0000
094c4d
Subject: [PATCH 2/6] glib: Use g_memdup2() instead of g_memdup() in obvious
094c4d
 places
094c4d
MIME-Version: 1.0
094c4d
Content-Type: text/plain; charset=UTF-8
094c4d
Content-Transfer-Encoding: 8bit
094c4d
094c4d
Convert all the call sites which use `g_memdup()`’s length argument
094c4d
trivially (for example, by passing a `sizeof()` or an existing `gsize`
094c4d
variable), so that they use `g_memdup2()` instead.
094c4d
094c4d
In almost all of these cases the use of `g_memdup()` would not have
094c4d
caused problems, but it will soon be deprecated, so best port away from
094c4d
it
094c4d
094c4d
In particular, this fixes an overflow within `g_bytes_new()`, identified
094c4d
as GHSL-2021-045 (aka CVE-2021-27219) by GHSL team member Kevin Backhouse.
094c4d
094c4d
Adapted for GLib 2.58 by Simon McVittie.
094c4d
094c4d
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
094c4d
Fixes: CVE-2021-27219
094c4d
Fixes: GHSL-2021-045
094c4d
Helps: #2319
094c4d
(cherry picked from commit 0736b7c1e7cf4232c5d7eb2b0fbfe9be81bd3baa)
094c4d
[Backport to 2.58: Omit changes to ghash.c, will be a separate commit]
094c4d
[Backport to 2.58: Omit changes to giochannel.c, not needed in this branch]
094c4d
[Backport to 2.58: Omit changes to uri test, not needed in this branch]
094c4d
Signed-off-by: Simon McVittie <smcv@collabora.com>
094c4d
---
094c4d
 glib/gbytes.c               | 6 ++++--
094c4d
 glib/gdir.c                 | 3 ++-
094c4d
 glib/gslice.c               | 3 ++-
094c4d
 glib/gtestutils.c           | 3 ++-
094c4d
 glib/gvariant.c             | 7 ++++---
094c4d
 glib/gvarianttype.c         | 3 ++-
094c4d
 glib/tests/array-test.c     | 2 +-
094c4d
 glib/tests/option-context.c | 6 ++++--
094c4d
 8 files changed, 21 insertions(+), 12 deletions(-)
094c4d
094c4d
diff --git a/glib/gbytes.c b/glib/gbytes.c
094c4d
index 3b14a51cd..5141170d7 100644
094c4d
--- a/glib/gbytes.c
094c4d
+++ b/glib/gbytes.c
094c4d
@@ -33,6 +33,8 @@
094c4d
 
094c4d
 #include <string.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
+
094c4d
 /**
094c4d
  * GBytes:
094c4d
  *
094c4d
@@ -94,7 +96,7 @@ g_bytes_new (gconstpointer data,
094c4d
 {
094c4d
   g_return_val_if_fail (data != NULL || size == 0, NULL);
094c4d
 
094c4d
-  return g_bytes_new_take (g_memdup (data, size), size);
094c4d
+  return g_bytes_new_take (g_memdup2 (data, size), size);
094c4d
 }
094c4d
 
094c4d
 /**
094c4d
@@ -490,7 +492,7 @@ g_bytes_unref_to_data (GBytes *bytes,
094c4d
        * Copy: Non g_malloc (or compatible) allocator, or static memory,
094c4d
        * so we have to copy, and then unref.
094c4d
        */
094c4d
-      result = g_memdup (bytes->data, bytes->size);
094c4d
+      result = g_memdup2 (bytes->data, bytes->size);
094c4d
       *size = bytes->size;
094c4d
       g_bytes_unref (bytes);
094c4d
     }
094c4d
diff --git a/glib/gdir.c b/glib/gdir.c
094c4d
index cb4ad0b2f..9d955d57f 100644
094c4d
--- a/glib/gdir.c
094c4d
+++ b/glib/gdir.c
094c4d
@@ -37,6 +37,7 @@
094c4d
 #include "gconvert.h"
094c4d
 #include "gfileutils.h"
094c4d
 #include "gstrfuncs.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtestutils.h"
094c4d
 #include "glibintl.h"
094c4d
 
094c4d
@@ -113,7 +114,7 @@ g_dir_open_with_errno (const gchar *path,
094c4d
     return NULL;
094c4d
 #endif
094c4d
 
094c4d
-  return g_memdup (&dir, sizeof dir);
094c4d
+  return g_memdup2 (&dir, sizeof dir);
094c4d
 }
094c4d
 
094c4d
 /**
094c4d
diff --git a/glib/gslice.c b/glib/gslice.c
094c4d
index 454c8a602..8e2359515 100644
094c4d
--- a/glib/gslice.c
094c4d
+++ b/glib/gslice.c
094c4d
@@ -45,6 +45,7 @@
094c4d
 #include "gmain.h"
094c4d
 #include "gmem.h"               /* gslice.h */
094c4d
 #include "gstrfuncs.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gutils.h"
094c4d
 #include "gtrashstack.h"
094c4d
 #include "gtestutils.h"
094c4d
@@ -352,7 +353,7 @@ g_slice_get_config_state (GSliceConfig ckey,
094c4d
       array[i++] = allocator->contention_counters[address];
094c4d
       array[i++] = allocator_get_magazine_threshold (allocator, address);
094c4d
       *n_values = i;
094c4d
-      return g_memdup (array, sizeof (array[0]) * *n_values);
094c4d
+      return g_memdup2 (array, sizeof (array[0]) * *n_values);
094c4d
     default:
094c4d
       return NULL;
094c4d
     }
094c4d
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
094c4d
index 0447dcda5..14e071fce 100644
094c4d
--- a/glib/gtestutils.c
094c4d
+++ b/glib/gtestutils.c
094c4d
@@ -49,6 +49,7 @@
094c4d
 #include "gpattern.h"
094c4d
 #include "grand.h"
094c4d
 #include "gstrfuncs.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtimer.h"
094c4d
 #include "gslice.h"
094c4d
 #include "gspawn.h"
094c4d
@@ -3397,7 +3398,7 @@ g_test_log_extract (GTestLogBuffer *tbuffer)
094c4d
       if (p <= tbuffer->data->str + mlength)
094c4d
         {
094c4d
           g_string_erase (tbuffer->data, 0, mlength);
094c4d
-          tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup (&msg, sizeof (msg)));
094c4d
+          tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup2 (&msg, sizeof (msg)));
094c4d
           return TRUE;
094c4d
         }
094c4d
 
094c4d
diff --git a/glib/gvariant.c b/glib/gvariant.c
094c4d
index 8be9ce798..45a1a73dc 100644
094c4d
--- a/glib/gvariant.c
094c4d
+++ b/glib/gvariant.c
094c4d
@@ -33,6 +33,7 @@
094c4d
 
094c4d
 #include <string.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 /**
094c4d
  * SECTION:gvariant
094c4d
@@ -720,7 +721,7 @@ g_variant_new_variant (GVariant *value)
094c4d
   g_variant_ref_sink (value);
094c4d
 
094c4d
   return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
094c4d
-                                      g_memdup (&value, sizeof value),
094c4d
+                                      g_memdup2 (&value, sizeof value),
094c4d
                                       1, g_variant_is_trusted (value));
094c4d
 }
094c4d
 
094c4d
@@ -1224,7 +1225,7 @@ g_variant_new_fixed_array (const GVariantType  *element_type,
094c4d
       return NULL;
094c4d
     }
094c4d
 
094c4d
-  data = g_memdup (elements, n_elements * element_size);
094c4d
+  data = g_memdup2 (elements, n_elements * element_size);
094c4d
   value = g_variant_new_from_data (array_type, data,
094c4d
                                    n_elements * element_size,
094c4d
                                    FALSE, g_free, data);
094c4d
@@ -1901,7 +1902,7 @@ g_variant_dup_bytestring (GVariant *value,
094c4d
   if (length)
094c4d
     *length = size;
094c4d
 
094c4d
-  return g_memdup (original, size + 1);
094c4d
+  return g_memdup2 (original, size + 1);
094c4d
 }
094c4d
 
094c4d
 /**
094c4d
diff --git a/glib/gvarianttype.c b/glib/gvarianttype.c
094c4d
index c8433e65a..dbbf7d2d1 100644
094c4d
--- a/glib/gvarianttype.c
094c4d
+++ b/glib/gvarianttype.c
094c4d
@@ -28,6 +28,7 @@
094c4d
 
094c4d
 #include <string.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 /**
094c4d
  * SECTION:gvarianttype
094c4d
@@ -1174,7 +1175,7 @@ g_variant_type_new_tuple (const GVariantType * const *items,
094c4d
   g_assert (offset < sizeof buffer);
094c4d
   buffer[offset++] = ')';
094c4d
 
094c4d
-  return (GVariantType *) g_memdup (buffer, offset);
094c4d
+  return (GVariantType *) g_memdup2 (buffer, offset);
094c4d
 }
094c4d
 
094c4d
 /**
094c4d
-- 
094c4d
2.31.1
094c4d
094c4d
From 7e2c2a07508a97b9d75e402afe4749b02a34dd8b Mon Sep 17 00:00:00 2001
094c4d
From: Simon McVittie <smcv@collabora.com>
094c4d
Date: Thu, 18 Mar 2021 10:31:00 +0000
094c4d
Subject: [PATCH 3/6] ghash: Use g_memdup2() instead of g_memdup()
094c4d
094c4d
Backport of part of commit 0736b7c1e7cf4232c5d7eb2b0fbfe9be81bd3baa
094c4d
to the simpler structure of the GHashTable code in glib-2-58.
094c4d
094c4d
Helps: #2319
094c4d
Signed-off-by: Simon McVittie <smcv@collabora.com>
094c4d
---
094c4d
 glib/ghash.c | 3 ++-
094c4d
 1 file changed, 2 insertions(+), 1 deletion(-)
094c4d
094c4d
diff --git a/glib/ghash.c b/glib/ghash.c
094c4d
index 6bb04a50d..608d136f4 100644
094c4d
--- a/glib/ghash.c
094c4d
+++ b/glib/ghash.c
094c4d
@@ -34,6 +34,7 @@
094c4d
 
094c4d
 #include "glib-private.h"
094c4d
 #include "gstrfuncs.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gatomic.h"
094c4d
 #include "gtestutils.h"
094c4d
 #include "gslice.h"
094c4d
@@ -967,7 +968,7 @@ g_hash_table_insert_node (GHashTable *hash_table,
094c4d
    * split the table.
094c4d
    */
094c4d
   if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
094c4d
-    hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
094c4d
+    hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
094c4d
 
094c4d
   /* Step 3: Actually do the write */
094c4d
   hash_table->values[node_index] = new_value;
094c4d
-- 
094c4d
2.31.1
094c4d
094c4d
From 9e0c87610dccd1b0eaca28a3baa521ea6a24f46b Mon Sep 17 00:00:00 2001
094c4d
From: Philip Withnall <pwithnall@endlessos.org>
094c4d
Date: Thu, 4 Feb 2021 13:39:25 +0000
094c4d
Subject: [PATCH 4/6] gobject: Use g_memdup2() instead of g_memdup() in obvious
094c4d
 places
094c4d
MIME-Version: 1.0
094c4d
Content-Type: text/plain; charset=UTF-8
094c4d
Content-Transfer-Encoding: 8bit
094c4d
094c4d
Convert all the call sites which use `g_memdup()`’s length argument
094c4d
trivially (for example, by passing a `sizeof()`), so that they use
094c4d
`g_memdup2()` instead.
094c4d
094c4d
In almost all of these cases the use of `g_memdup()` would not have
094c4d
caused problems, but it will soon be deprecated, so best port away from
094c4d
it.
094c4d
094c4d
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
094c4d
Helps: #2319
094c4d
(cherry picked from commit 6110caea45b235420b98cd41d845cc92238f6781)
094c4d
---
094c4d
 gobject/gsignal.c     | 3 ++-
094c4d
 gobject/gtype.c       | 9 +++++----
094c4d
 gobject/gtypemodule.c | 3 ++-
094c4d
 gobject/tests/param.c | 4 +++-
094c4d
 4 files changed, 12 insertions(+), 7 deletions(-)
094c4d
094c4d
diff --git a/gobject/gsignal.c b/gobject/gsignal.c
094c4d
index b22dfcca8..92555eb60 100644
094c4d
--- a/gobject/gsignal.c
094c4d
+++ b/gobject/gsignal.c
094c4d
@@ -28,6 +28,7 @@
094c4d
 #include <signal.h>
094c4d
 
094c4d
 #include "gsignal.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtype-private.h"
094c4d
 #include "gbsearcharray.h"
094c4d
 #include "gvaluecollector.h"
094c4d
@@ -1724,7 +1725,7 @@ g_signal_newv (const gchar       *signal_name,
094c4d
   node->single_va_closure_is_valid = FALSE;
094c4d
   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
094c4d
   node->n_params = n_params;
094c4d
-  node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
094c4d
+  node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params);
094c4d
   node->return_type = return_type;
094c4d
   node->class_closure_bsa = NULL;
094c4d
   if (accumulator)
094c4d
diff --git a/gobject/gtype.c b/gobject/gtype.c
094c4d
index 275a8b60b..9e663ce52 100644
094c4d
--- a/gobject/gtype.c
094c4d
+++ b/gobject/gtype.c
094c4d
@@ -33,6 +33,7 @@
094c4d
 
094c4d
 #include "glib-private.h"
094c4d
 #include "gconstructor.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 #ifdef G_OS_WIN32
094c4d
 #include <windows.h>
094c4d
@@ -1471,7 +1472,7 @@ type_add_interface_Wm (TypeNode             *node,
094c4d
   iholder->next = iface_node_get_holders_L (iface);
094c4d
   iface_node_set_holders_W (iface, iholder);
094c4d
   iholder->instance_type = NODE_TYPE (node);
094c4d
-  iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
094c4d
+  iholder->info = info ? g_memdup2 (info, sizeof (*info)) : NULL;
094c4d
   iholder->plugin = plugin;
094c4d
 
094c4d
   /* create an iface entry for this type */
094c4d
@@ -1732,7 +1733,7 @@ type_iface_retrieve_holder_info_Wm (TypeNode *iface,
094c4d
         INVALID_RECURSION ("g_type_plugin_*", iholder->plugin, NODE_NAME (iface));
094c4d
       
094c4d
       check_interface_info_I (iface, instance_type, &tmp_info);
094c4d
-      iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
094c4d
+      iholder->info = g_memdup2 (&tmp_info, sizeof (tmp_info));
094c4d
     }
094c4d
   
094c4d
   return iholder;	/* we don't modify write lock upon returning NULL */
094c4d
@@ -2013,10 +2014,10 @@ type_iface_vtable_base_init_Wm (TypeNode *iface,
094c4d
       IFaceEntry *pentry = type_lookup_iface_entry_L (pnode, iface);
094c4d
       
094c4d
       if (pentry)
094c4d
-	vtable = g_memdup (pentry->vtable, iface->data->iface.vtable_size);
094c4d
+	vtable = g_memdup2 (pentry->vtable, iface->data->iface.vtable_size);
094c4d
     }
094c4d
   if (!vtable)
094c4d
-    vtable = g_memdup (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
094c4d
+    vtable = g_memdup2 (iface->data->iface.dflt_vtable, iface->data->iface.vtable_size);
094c4d
   entry->vtable = vtable;
094c4d
   vtable->g_type = NODE_TYPE (iface);
094c4d
   vtable->g_instance_type = NODE_TYPE (node);
094c4d
diff --git a/gobject/gtypemodule.c b/gobject/gtypemodule.c
094c4d
index c67f789b1..cf877bc0b 100644
094c4d
--- a/gobject/gtypemodule.c
094c4d
+++ b/gobject/gtypemodule.c
094c4d
@@ -19,6 +19,7 @@
094c4d
 
094c4d
 #include <stdlib.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtypeplugin.h"
094c4d
 #include "gtypemodule.h"
094c4d
 
094c4d
@@ -436,7 +437,7 @@ g_type_module_register_type (GTypeModule     *module,
094c4d
   module_type_info->loaded = TRUE;
094c4d
   module_type_info->info = *type_info;
094c4d
   if (type_info->value_table)
094c4d
-    module_type_info->info.value_table = g_memdup (type_info->value_table,
094c4d
+    module_type_info->info.value_table = g_memdup2 (type_info->value_table,
094c4d
 						   sizeof (GTypeValueTable));
094c4d
 
094c4d
   return module_type_info->type;
094c4d
diff --git a/gobject/tests/param.c b/gobject/tests/param.c
094c4d
index 758289bf8..971cff162 100644
094c4d
--- a/gobject/tests/param.c
094c4d
+++ b/gobject/tests/param.c
094c4d
@@ -2,6 +2,8 @@
094c4d
 #include <glib-object.h>
094c4d
 #include <stdlib.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
+
094c4d
 static void
094c4d
 test_param_value (void)
094c4d
 {
094c4d
@@ -851,7 +853,7 @@ main (int argc, char *argv[])
094c4d
             test_path = g_strdup_printf ("/param/implement/subprocess/%d-%d-%d-%d",
094c4d
                                          data.change_this_flag, data.change_this_type,
094c4d
                                          data.use_this_flag, data.use_this_type);
094c4d
-            test_data = g_memdup (&data, sizeof (TestParamImplementData));
094c4d
+            test_data = g_memdup2 (&data, sizeof (TestParamImplementData));
094c4d
             g_test_add_data_func_full (test_path, test_data, test_param_implement_child, g_free);
094c4d
             g_free (test_path);
094c4d
           }
094c4d
-- 
094c4d
2.31.1
094c4d
094c4d
From d3f7a79540fc1e85eb82c2987e9f7e2dbd93ff74 Mon Sep 17 00:00:00 2001
094c4d
From: Philip Withnall <pwithnall@endlessos.org>
094c4d
Date: Thu, 4 Feb 2021 13:37:56 +0000
094c4d
Subject: [PATCH 5/6] gio: Use g_memdup2() instead of g_memdup() in obvious
094c4d
 places
094c4d
MIME-Version: 1.0
094c4d
Content-Type: text/plain; charset=UTF-8
094c4d
Content-Transfer-Encoding: 8bit
094c4d
094c4d
Convert all the call sites which use `g_memdup()`’s length argument
094c4d
trivially (for example, by passing a `sizeof()`), so that they use
094c4d
`g_memdup2()` instead.
094c4d
094c4d
In almost all of these cases the use of `g_memdup()` would not have
094c4d
caused problems, but it will soon be deprecated, so best port away from
094c4d
it.
094c4d
094c4d
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
094c4d
Helps: #2319
094c4d
(cherry picked from commit be8834340a2d928ece82025463ae23dee2c333d0)
094c4d
---
094c4d
 gio/gdbusconnection.c                 | 5 +++--
094c4d
 gio/gdbusinterfaceskeleton.c          | 3 ++-
094c4d
 gio/gfile.c                           | 7 ++++---
094c4d
 gio/gsettingsschema.c                 | 5 +++--
094c4d
 gio/gwin32registrykey.c               | 8 +++++---
094c4d
 gio/tests/async-close-output-stream.c | 6 ++++--
094c4d
 gio/tests/gdbus-export.c              | 5 +++--
094c4d
 gio/win32/gwinhttpfile.c              | 9 +++++----
094c4d
 8 files changed, 29 insertions(+), 19 deletions(-)
094c4d
094c4d
diff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
094c4d
index 6f7e5fefc..117c8df35 100644
094c4d
--- a/gio/gdbusconnection.c
094c4d
+++ b/gio/gdbusconnection.c
094c4d
@@ -119,6 +119,7 @@
094c4d
 #include "gasyncinitable.h"
094c4d
 #include "giostream.h"
094c4d
 #include "gasyncresult.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtask.h"
094c4d
 
094c4d
 #ifdef G_OS_UNIX
094c4d
@@ -3970,7 +3971,7 @@ _g_dbus_interface_vtable_copy (const GDBusInterfaceVTable *vtable)
094c4d
   /* Don't waste memory by copying padding - remember to update this
094c4d
    * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
094c4d
    */
094c4d
-  return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
094c4d
+  return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
094c4d
 }
094c4d
 
094c4d
 static void
094c4d
@@ -3987,7 +3988,7 @@ _g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable *vtable)
094c4d
   /* Don't waste memory by copying padding - remember to update this
094c4d
    * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
094c4d
    */
094c4d
-  return g_memdup ((gconstpointer) vtable, 3 * sizeof (gpointer));
094c4d
+  return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
094c4d
 }
094c4d
 
094c4d
 static void
094c4d
diff --git a/gio/gdbusinterfaceskeleton.c b/gio/gdbusinterfaceskeleton.c
094c4d
index 96bd520aa..672604c49 100644
094c4d
--- a/gio/gdbusinterfaceskeleton.c
094c4d
+++ b/gio/gdbusinterfaceskeleton.c
094c4d
@@ -27,6 +27,7 @@
094c4d
 #include "gdbusprivate.h"
094c4d
 #include "gdbusmethodinvocation.h"
094c4d
 #include "gdbusconnection.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gtask.h"
094c4d
 #include "gioerror.h"
094c4d
 
094c4d
@@ -697,7 +698,7 @@ add_connection_locked (GDBusInterfaceSkeleton *interface_,
094c4d
        * properly before building the hooked_vtable, so we create it
094c4d
        * once at the last minute.
094c4d
        */
094c4d
-      interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
094c4d
+      interface_->priv->hooked_vtable = g_memdup2 (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
094c4d
       interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
094c4d
     }
094c4d
 
094c4d
diff --git a/gio/gfile.c b/gio/gfile.c
094c4d
index ff313ebf8..29ebaaa62 100644
094c4d
--- a/gio/gfile.c
094c4d
+++ b/gio/gfile.c
094c4d
@@ -60,6 +60,7 @@
094c4d
 #include "gasyncresult.h"
094c4d
 #include "gioerror.h"
094c4d
 #include "glibintl.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 
094c4d
 /**
094c4d
@@ -7734,7 +7735,7 @@ measure_disk_usage_progress (gboolean reporting,
094c4d
   g_main_context_invoke_full (g_task_get_context (task),
094c4d
                               g_task_get_priority (task),
094c4d
                               measure_disk_usage_invoke_progress,
094c4d
-                              g_memdup (&progress, sizeof progress),
094c4d
+                              g_memdup2 (&progress, sizeof progress),
094c4d
                               g_free);
094c4d
 }
094c4d
 
094c4d
@@ -7752,7 +7753,7 @@ measure_disk_usage_thread (GTask        *task,
094c4d
                                  data->progress_callback ? measure_disk_usage_progress : NULL, task,
094c4d
                                  &result.disk_usage, &result.num_dirs, &result.num_files,
094c4d
                                  &error))
094c4d
-    g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
094c4d
+    g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
094c4d
   else
094c4d
     g_task_return_error (task, error);
094c4d
 }
094c4d
@@ -7776,7 +7777,7 @@ g_file_real_measure_disk_usage_async (GFile                        *file,
094c4d
 
094c4d
   task = g_task_new (file, cancellable, callback, user_data);
094c4d
   g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
094c4d
-  g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
094c4d
+  g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
094c4d
   g_task_set_priority (task, io_priority);
094c4d
 
094c4d
   g_task_run_in_thread (task, measure_disk_usage_thread);
094c4d
diff --git a/gio/gsettingsschema.c b/gio/gsettingsschema.c
094c4d
index 17b7e3b01..499944395 100644
094c4d
--- a/gio/gsettingsschema.c
094c4d
+++ b/gio/gsettingsschema.c
094c4d
@@ -20,6 +20,7 @@
094c4d
 
094c4d
 #include "gsettingsschema-internal.h"
094c4d
 #include "gsettings.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 #include "gvdb/gvdb-reader.h"
094c4d
 #include "strinfo.c"
094c4d
@@ -1054,9 +1055,9 @@ g_settings_schema_list_children (GSettingsSchema *schema)
094c4d
 
094c4d
       if (g_str_has_suffix (key, "/"))
094c4d
         {
094c4d
-          gint length = strlen (key);
094c4d
+          gsize length = strlen (key);
094c4d
 
094c4d
-          strv[j] = g_memdup (key, length);
094c4d
+          strv[j] = g_memdup2 (key, length);
094c4d
           strv[j][length - 1] = '\0';
094c4d
           j++;
094c4d
         }
094c4d
diff --git a/gio/gwin32registrykey.c b/gio/gwin32registrykey.c
094c4d
index c19fede4e..619fd48af 100644
094c4d
--- a/gio/gwin32registrykey.c
094c4d
+++ b/gio/gwin32registrykey.c
094c4d
@@ -28,6 +28,8 @@
094c4d
 #include <ntstatus.h>
094c4d
 #include <winternl.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
+
094c4d
 #ifndef _WDMDDK_
094c4d
 typedef enum _KEY_INFORMATION_CLASS {
094c4d
   KeyBasicInformation,
094c4d
@@ -247,7 +249,7 @@ g_win32_registry_value_iter_copy (const GWin32RegistryValueIter *iter)
094c4d
   new_iter->value_name_size = iter->value_name_size;
094c4d
 
094c4d
   if (iter->value_data != NULL)
094c4d
-    new_iter->value_data = g_memdup (iter->value_data, iter->value_data_size);
094c4d
+    new_iter->value_data = g_memdup2 (iter->value_data, iter->value_data_size);
094c4d
 
094c4d
   new_iter->value_data_size = iter->value_data_size;
094c4d
 
094c4d
@@ -268,8 +270,8 @@ g_win32_registry_value_iter_copy (const GWin32RegistryValueIter *iter)
094c4d
   new_iter->value_data_expanded_charsize = iter->value_data_expanded_charsize;
094c4d
 
094c4d
   if (iter->value_data_expanded_u8 != NULL)
094c4d
-    new_iter->value_data_expanded_u8 = g_memdup (iter->value_data_expanded_u8,
094c4d
-                                                 iter->value_data_expanded_charsize);
094c4d
+    new_iter->value_data_expanded_u8 = g_memdup2 (iter->value_data_expanded_u8,
094c4d
+                                                  iter->value_data_expanded_charsize);
094c4d
 
094c4d
   new_iter->value_data_expanded_u8_size = iter->value_data_expanded_charsize;
094c4d
 
094c4d
diff --git a/gio/tests/async-close-output-stream.c b/gio/tests/async-close-output-stream.c
094c4d
index 5f6620275..d3f97a119 100644
094c4d
--- a/gio/tests/async-close-output-stream.c
094c4d
+++ b/gio/tests/async-close-output-stream.c
094c4d
@@ -24,6 +24,8 @@
094c4d
 #include <stdlib.h>
094c4d
 #include <string.h>
094c4d
 
094c4d
+#include "gstrfuncsprivate.h"
094c4d
+
094c4d
 #define DATA_TO_WRITE "Hello world\n"
094c4d
 
094c4d
 typedef struct
094c4d
@@ -147,9 +149,9 @@ prepare_data (SetupData *data,
094c4d
 
094c4d
   data->expected_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
094c4d
 
094c4d
-  g_assert_cmpint (data->expected_size, >, 0);
094c4d
+  g_assert_cmpuint (data->expected_size, >, 0);
094c4d
 
094c4d
-  data->expected_output = g_memdup (written, (guint)data->expected_size);
094c4d
+  data->expected_output = g_memdup2 (written, data->expected_size);
094c4d
 
094c4d
   /* then recreate the streams and prepare them for the asynchronous close */
094c4d
   destroy_streams (data);
094c4d
diff --git a/gio/tests/gdbus-export.c b/gio/tests/gdbus-export.c
094c4d
index ef0dddeee..a3c842360 100644
094c4d
--- a/gio/tests/gdbus-export.c
094c4d
+++ b/gio/tests/gdbus-export.c
094c4d
@@ -23,6 +23,7 @@
094c4d
 #include <string.h>
094c4d
 
094c4d
 #include "gdbus-tests.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 /* all tests rely on a shared mainloop */
094c4d
 static GMainLoop *loop = NULL;
094c4d
@@ -652,7 +653,7 @@ subtree_introspect (GDBusConnection       *connection,
094c4d
       g_assert_not_reached ();
094c4d
     }
094c4d
 
094c4d
-  return g_memdup (interfaces, 2 * sizeof (void *));
094c4d
+  return g_memdup2 (interfaces, 2 * sizeof (void *));
094c4d
 }
094c4d
 
094c4d
 static const GDBusInterfaceVTable *
094c4d
@@ -708,7 +709,7 @@ dynamic_subtree_introspect (GDBusConnection       *connection,
094c4d
 {
094c4d
   const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
094c4d
 
094c4d
-  return g_memdup (interfaces, 2 * sizeof (void *));
094c4d
+  return g_memdup2 (interfaces, 2 * sizeof (void *));
094c4d
 }
094c4d
 
094c4d
 static const GDBusInterfaceVTable *
094c4d
diff --git a/gio/win32/gwinhttpfile.c b/gio/win32/gwinhttpfile.c
094c4d
index d5df16d91..f424d21cc 100644
094c4d
--- a/gio/win32/gwinhttpfile.c
094c4d
+++ b/gio/win32/gwinhttpfile.c
094c4d
@@ -29,6 +29,7 @@
094c4d
 #include "gio/gfile.h"
094c4d
 #include "gio/gfileattribute.h"
094c4d
 #include "gio/gfileinfo.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 #include "gwinhttpfile.h"
094c4d
 #include "gwinhttpfileinputstream.h"
094c4d
 #include "gwinhttpfileoutputstream.h"
094c4d
@@ -393,10 +394,10 @@ g_winhttp_file_resolve_relative_path (GFile      *file,
094c4d
   child = g_object_new (G_TYPE_WINHTTP_FILE, NULL);
094c4d
   child->vfs = winhttp_file->vfs;
094c4d
   child->url = winhttp_file->url;
094c4d
-  child->url.lpszScheme = g_memdup (winhttp_file->url.lpszScheme, (winhttp_file->url.dwSchemeLength+1)*2);
094c4d
-  child->url.lpszHostName = g_memdup (winhttp_file->url.lpszHostName, (winhttp_file->url.dwHostNameLength+1)*2);
094c4d
-  child->url.lpszUserName = g_memdup (winhttp_file->url.lpszUserName, (winhttp_file->url.dwUserNameLength+1)*2);
094c4d
-  child->url.lpszPassword = g_memdup (winhttp_file->url.lpszPassword, (winhttp_file->url.dwPasswordLength+1)*2);
094c4d
+  child->url.lpszScheme = g_memdup2 (winhttp_file->url.lpszScheme, (winhttp_file->url.dwSchemeLength+1)*2);
094c4d
+  child->url.lpszHostName = g_memdup2 (winhttp_file->url.lpszHostName, (winhttp_file->url.dwHostNameLength+1)*2);
094c4d
+  child->url.lpszUserName = g_memdup2 (winhttp_file->url.lpszUserName, (winhttp_file->url.dwUserNameLength+1)*2);
094c4d
+  child->url.lpszPassword = g_memdup2 (winhttp_file->url.lpszPassword, (winhttp_file->url.dwPasswordLength+1)*2);
094c4d
   child->url.lpszUrlPath = wnew_path;
094c4d
   child->url.dwUrlPathLength = wcslen (wnew_path);
094c4d
   child->url.lpszExtraInfo = NULL;
094c4d
-- 
094c4d
2.31.1
094c4d
094c4d
From 661f5edc901219a1a99bb51f171be13063878bd6 Mon Sep 17 00:00:00 2001
094c4d
From: Michael Catanzaro <mcatanzaro@redhat.com>
094c4d
Date: Thu, 20 May 2021 15:58:53 -0500
094c4d
Subject: [PATCH 6/6] gdatainputstream: replace easy use of g_memdup()
094c4d
094c4d
This code is passing a gsize, so might as well switch this to g_memdup2().
094c4d
094c4d
This is the only use of g_memdup() in GLib 2.56 that is not part of GLib
094c4d
2.58. All other uses analyzed in glib!2000.
094c4d
---
094c4d
 gio/gdatainputstream.c | 3 ++-
094c4d
 1 file changed, 2 insertions(+), 1 deletion(-)
094c4d
094c4d
diff --git a/gio/gdatainputstream.c b/gio/gdatainputstream.c
094c4d
index 9f207b158..ebef7c797 100644
094c4d
--- a/gio/gdatainputstream.c
094c4d
+++ b/gio/gdatainputstream.c
094c4d
@@ -27,6 +27,7 @@
094c4d
 #include "gioenumtypes.h"
094c4d
 #include "gioerror.h"
094c4d
 #include "glibintl.h"
094c4d
+#include "gstrfuncsprivate.h"
094c4d
 
094c4d
 #include <string.h>
094c4d
 
094c4d
@@ -1082,7 +1083,7 @@ g_data_input_stream_read_async (GDataInputStream    *stream,
094c4d
   data = g_slice_new0 (GDataInputStreamReadData);
094c4d
   if (stop_chars_len == -1)
094c4d
     stop_chars_len = strlen (stop_chars);
094c4d
-  data->stop_chars = g_memdup (stop_chars, stop_chars_len);
094c4d
+  data->stop_chars = g_memdup2 (stop_chars, stop_chars_len);
094c4d
   data->stop_chars_len = stop_chars_len;
094c4d
   data->last_saw_cr = FALSE;
094c4d
 
094c4d
-- 
094c4d
2.31.1