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