diff mbox series

[2025.02.x,v2] package/libglib2: fix CVE-2025-6052

Message ID 20250708073934.1926413-1-tim.soubry@mind.be
State Accepted
Headers show
Series [2025.02.x,v2] package/libglib2: fix CVE-2025-6052 | expand

Commit Message

Tim Soubry July 8, 2025, 7:39 a.m. UTC
Buildroot LTS 2025.02.x utilises libglib2 version 2.82.5 which is prone
to CVE-2025-6052 [1].
0001-gstring-Fix-overflow-check-when-expanding-the-string.patch is
included in the package/libglib2 directory. This patch mitigates
CVE-2025-6052 as seen in [1], by backporting to libglib2 2.82.5.
libglib2 shares part of its infrastructure with libglib2-bootstrap,
by adding the patch in package/libglib2 the patch isn't applied to
libglib2-bootstrap.
When building libglib2, the patch was correctly applied in
output/build/host-libglib2-2.82.5/glib/gstring.c .  When building
libglib2-bootstrap it was verified that the patch was not applied here.

Signed-off-by: Tim Soubry <tim.soubry@mind.be>
---
 ...flow-check-when-expanding-the-string.patch | 71 +++++++++++++++++++
 package/libglib2/libglib2.mk                  |  3 +
 2 files changed, 74 insertions(+)
 create mode 100644 package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch

Comments

Arnout Vandecappelle Aug. 14, 2025, 8:10 p.m. UTC | #1
On 08/07/2025 09:39, Tim Soubry via buildroot wrote:
> Buildroot LTS 2025.02.x utilises libglib2 version 2.82.5 which is prone
> to CVE-2025-6052 [1].
> 0001-gstring-Fix-overflow-check-when-expanding-the-string.patch is
> included in the package/libglib2 directory. This patch mitigates
> CVE-2025-6052 as seen in [1], by backporting to libglib2 2.82.5.
> libglib2 shares part of its infrastructure with libglib2-bootstrap,
> by adding the patch in package/libglib2 the patch isn't applied to
> libglib2-bootstrap.
> When building libglib2, the patch was correctly applied in
> output/build/host-libglib2-2.82.5/glib/gstring.c .  When building
> libglib2-bootstrap it was verified that the patch was not applied here.
> 
> Signed-off-by: Tim Soubry <tim.soubry@mind.be>
> ---
>   ...flow-check-when-expanding-the-string.patch | 71 +++++++++++++++++++
>   package/libglib2/libglib2.mk                  |  3 +
>   2 files changed, 74 insertions(+)
>   create mode 100644 package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
> 
> diff --git a/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch b/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
> new file mode 100644
> index 0000000000..447b654780
> --- /dev/null
> +++ b/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
> @@ -0,0 +1,71 @@
> +From 9c403f787eee69be850ba5f92348ecea65f9badc Mon Sep 17 00:00:00 2001
> +From: Philip Withnall <pwithnall@gnome.org>
> +Date: Tue, 3 Jun 2025 11:31:04 +0100
> +Subject: [PATCH 1/1] gstring: Fix overflow check when expanding the string

  check-package complains:

package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch:4: 
generate your patches with 'git format-patch -N'


  Applied to 2025.02.x with that fixed, thanks.

  Regards,
  Arnout


> +MIME-Version: 1.0
> +Content-Type: text/plain; charset=UTF-8
> +Content-Transfer-Encoding: 8bit
> +
> +After commit 34b7992fd6e3894bf6d2229b8aa59cac34bcb1b5 the overflow check
> +was only done when expanding the string, but we need to do it before
> +checking whether to expand the string, otherwise that calculation could
> +overflow and falsely decide that the string is big enough already.
> +
> +As a concrete example, consider a `GString` which has:
> + * `.len = G_MAXSIZE / 2 + 1`
> + * `.allocated_len = G_MAXSIZE / 2 + 1`
> +and `g_string_append()` is called on it with an input string of length
> +`G_MAXSIZE / 2`.
> +
> +This results in a call `g_string_maybe_expand (string, G_MAXSIZE / 2)`,
> +which calculates `string->len + len` as `(G_MAXSIZE / 2 + 1) +
> +(G_MAXSIZE / 2)` which evaluates to `1` as it overflows. This is not
> +greater than `string->allocated_len` (which is `G_MAXSIZE / 2 + 1`), so
> +`g_string_expand()` is *not* called, and `g_string_maybe_expand()`
> +returns successfully. The caller then assumes that there’s enough space
> +in the buffer, and happily continues to cause a buffer overflow.
> +
> +It’s unlikely anyone could hit this in practice because it requires
> +ludicrously big strings and `GString` allocations, which likely would
> +have been blocked by other code, but if we’re going to have the overflow
> +checks in `GString` then they should be effective.
> +
> +Spotted by code inspection.
> +
> +Signed-off-by: Philip Withnall <pwithnall@gnome.org>
> +Upstream: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4655/diffs
> +CVE: CVE-2025-6052
> +Signed-off-by: Tim Soubry <tim.soubry@mind.be>
> +---
> + glib/gstring.c | 8 ++++----
> + 1 file changed, 4 insertions(+), 4 deletions(-)
> +
> +diff --git a/glib/gstring.c b/glib/gstring.c
> +index bdd068344..45a6f68b1 100644
> +--- a/glib/gstring.c
> ++++ b/glib/gstring.c
> +@@ -68,10 +68,6 @@ static void
> + g_string_expand (GString *string,
> +                  gsize    len)
> + {
> +-  /* Detect potential overflow */
> +-  if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
> +-    g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
> +-
> +   string->allocated_len = g_nearest_pow (string->len + len + 1);
> +   /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough
> +    * memory for this string and don't over-allocate.
> +@@ -86,6 +82,10 @@ static inline void
> + g_string_maybe_expand (GString *string,
> +                        gsize    len)
> + {
> ++  /* Detect potential overflow */
> ++  if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
> ++    g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
> ++
> +   if (G_UNLIKELY (string->len + len >= string->allocated_len))
> +     g_string_expand (string, len);
> + }
> +--
> +2.39.5
> +
> diff --git a/package/libglib2/libglib2.mk b/package/libglib2/libglib2.mk
> index 6c012c79c7..b8c853b752 100644
> --- a/package/libglib2/libglib2.mk
> +++ b/package/libglib2/libglib2.mk
> @@ -22,6 +22,9 @@ ifeq ($(BR2_ARM_INSTRUCTIONS_THUMB),y)
>   LIBGLIB2_CFLAGS += -marm
>   endif
>   
> +# 0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
> +LIBGLIB2_IGNORE_CVES += CVE-2025-6052
> +
>   HOST_LIBGLIB2_CONF_OPTS = \
>   	-Ddtrace=false \
>   	-Dglib_debug=disabled \
diff mbox series

Patch

diff --git a/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch b/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
new file mode 100644
index 0000000000..447b654780
--- /dev/null
+++ b/package/libglib2/0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
@@ -0,0 +1,71 @@ 
+From 9c403f787eee69be850ba5f92348ecea65f9badc Mon Sep 17 00:00:00 2001
+From: Philip Withnall <pwithnall@gnome.org>
+Date: Tue, 3 Jun 2025 11:31:04 +0100
+Subject: [PATCH 1/1] gstring: Fix overflow check when expanding the string
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+After commit 34b7992fd6e3894bf6d2229b8aa59cac34bcb1b5 the overflow check
+was only done when expanding the string, but we need to do it before
+checking whether to expand the string, otherwise that calculation could
+overflow and falsely decide that the string is big enough already.
+
+As a concrete example, consider a `GString` which has:
+ * `.len = G_MAXSIZE / 2 + 1`
+ * `.allocated_len = G_MAXSIZE / 2 + 1`
+and `g_string_append()` is called on it with an input string of length
+`G_MAXSIZE / 2`.
+
+This results in a call `g_string_maybe_expand (string, G_MAXSIZE / 2)`,
+which calculates `string->len + len` as `(G_MAXSIZE / 2 + 1) +
+(G_MAXSIZE / 2)` which evaluates to `1` as it overflows. This is not
+greater than `string->allocated_len` (which is `G_MAXSIZE / 2 + 1`), so
+`g_string_expand()` is *not* called, and `g_string_maybe_expand()`
+returns successfully. The caller then assumes that there’s enough space
+in the buffer, and happily continues to cause a buffer overflow.
+
+It’s unlikely anyone could hit this in practice because it requires
+ludicrously big strings and `GString` allocations, which likely would
+have been blocked by other code, but if we’re going to have the overflow
+checks in `GString` then they should be effective.
+
+Spotted by code inspection.
+
+Signed-off-by: Philip Withnall <pwithnall@gnome.org>
+Upstream: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4655/diffs
+CVE: CVE-2025-6052
+Signed-off-by: Tim Soubry <tim.soubry@mind.be>
+---
+ glib/gstring.c | 8 ++++----
+ 1 file changed, 4 insertions(+), 4 deletions(-)
+
+diff --git a/glib/gstring.c b/glib/gstring.c
+index bdd068344..45a6f68b1 100644
+--- a/glib/gstring.c
++++ b/glib/gstring.c
+@@ -68,10 +68,6 @@ static void
+ g_string_expand (GString *string,
+                  gsize    len)
+ {
+-  /* Detect potential overflow */
+-  if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
+-    g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
+-
+   string->allocated_len = g_nearest_pow (string->len + len + 1);
+   /* If the new size is bigger than G_MAXSIZE / 2, only allocate enough
+    * memory for this string and don't over-allocate.
+@@ -86,6 +82,10 @@ static inline void
+ g_string_maybe_expand (GString *string,
+                        gsize    len)
+ {
++  /* Detect potential overflow */
++  if G_UNLIKELY ((G_MAXSIZE - string->len - 1) < len)
++    g_error ("adding %" G_GSIZE_FORMAT " to string would overflow", len);
++
+   if (G_UNLIKELY (string->len + len >= string->allocated_len))
+     g_string_expand (string, len);
+ }
+-- 
+2.39.5
+
diff --git a/package/libglib2/libglib2.mk b/package/libglib2/libglib2.mk
index 6c012c79c7..b8c853b752 100644
--- a/package/libglib2/libglib2.mk
+++ b/package/libglib2/libglib2.mk
@@ -22,6 +22,9 @@  ifeq ($(BR2_ARM_INSTRUCTIONS_THUMB),y)
 LIBGLIB2_CFLAGS += -marm
 endif
 
+# 0001-gstring-Fix-overflow-check-when-expanding-the-string.patch
+LIBGLIB2_IGNORE_CVES += CVE-2025-6052
+
 HOST_LIBGLIB2_CONF_OPTS = \
 	-Ddtrace=false \
 	-Dglib_debug=disabled \