|
| 1 | +From 5ba0ed9ab2c28294713bdc56a8744ff0a446b59c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Marco Trevisan <mail@3v1n0.net> |
| 3 | +Date: Fri, 23 Jan 2026 18:48:30 +0100 |
| 4 | +Subject: [PATCH 1/2] gbase64: Use gsize to prevent potential overflow |
| 5 | +[PATCH 2/2] gbase64: Ensure that the out value is within allocated |
| 6 | +MIME-Version: 1.0 |
| 7 | +Content-Type: text/plain; charset=UTF-8 |
| 8 | +Content-Transfer-Encoding: 8bit |
| 9 | + |
| 10 | +Both g_base64_encode_step() and g_base64_encode_close() return gsize |
| 11 | +values, but these are summed to an int value. |
| 12 | + |
| 13 | +If the sum of these returned values is bigger than MAXINT, we overflow |
| 14 | +while doing the null byte write. |
| 15 | + |
| 16 | +Spotted by treeplus. |
| 17 | +Thanks to the Sovereign Tech Resilience programme from the Sovereign |
| 18 | +Tech Agency. |
| 19 | + |
| 20 | +ID: #YWH-PGM9867-168 |
| 21 | +Closes: #3870 |
| 22 | + |
| 23 | + |
| 24 | +(cherry picked from commit 6845f7776982849a2be1d8c9b0495e389092bff2) |
| 25 | +Upstream Patch reference: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4979.patch |
| 26 | + |
| 27 | +Co-authored-by: Marco Trevisan (Treviño) <mail@3v1n0.net> |
| 28 | +--- |
| 29 | + glib/gbase64.c | 11 +++++++++-- |
| 30 | + 1 file changed, 9 insertions(+), 2 deletions(-) |
| 31 | + |
| 32 | +diff --git a/glib/gbase64.c b/glib/gbase64.c |
| 33 | +index f2d110e..19f8e5e 100644 |
| 34 | +--- a/glib/gbase64.c |
| 35 | ++++ b/glib/gbase64.c |
| 36 | +@@ -262,8 +262,10 @@ g_base64_encode (const guchar *data, |
| 37 | + gsize len) |
| 38 | + { |
| 39 | + gchar *out; |
| 40 | +- gint state = 0, outlen; |
| 41 | ++ gint state = 0; |
| 42 | + gint save = 0; |
| 43 | ++ gsize outlen; |
| 44 | ++ gsize allocsize; |
| 45 | + |
| 46 | + g_return_val_if_fail (data != NULL || len == 0, NULL); |
| 47 | + |
| 48 | +@@ -271,10 +273,15 @@ g_base64_encode (const guchar *data, |
| 49 | + +1 is needed for trailing \0, also check for unlikely integer overflow */ |
| 50 | + g_return_val_if_fail (len < ((G_MAXSIZE - 1) / 4 - 1) * 3, NULL); |
| 51 | + |
| 52 | +- out = g_malloc ((len / 3 + 1) * 4 + 1); |
| 53 | ++ allocsize = (len / 3 + 1) * 4 + 1; |
| 54 | ++ out = g_malloc (allocsize); |
| 55 | + |
| 56 | + outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save); |
| 57 | ++ g_assert (outlen < allocsize); |
| 58 | ++ |
| 59 | + outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save); |
| 60 | ++ g_assert (outlen < allocsize); |
| 61 | ++ |
| 62 | + out[outlen] = '\0'; |
| 63 | + |
| 64 | + return (gchar *) out; |
| 65 | +-- |
| 66 | +2.45.4 |
| 67 | + |
0 commit comments