5c320d
From b6036e23b0477be147211b4e21a6b49cd4d6c9a0 Mon Sep 17 00:00:00 2001
5c320d
From: Jamie Bainbridge <jamie.bainbridge@gmail.com>
5c320d
Date: Wed, 8 Sep 2021 12:08:17 +1000
5c320d
Subject: [PATCH] gutils: Avoid segfault in g_get_user_database_entry
5c320d
5c320d
g_get_user_database_entry() uses variable pwd to store the contents of
5c320d
the call to getpwnam_r(), then capitalises the first letter of pw_name
5c320d
with g_ascii_toupper (pw->pw_name[0]).
5c320d
5c320d
However, as per the getpwnam manpage, the result of that call "may point
5c320d
to a static area". When this happens, GLib is trying to edit static
5c320d
memory which belongs to a shared library, so segfaults.
5c320d
5c320d
Instead, copy pw_name off to a temporary variable, set uppercase on
5c320d
that variable, and use the variable to join into the desired string.
5c320d
Free the new variable after it is no longer needed.
5c320d
5c320d
Signed-off-by: Jamie Bainbridge <jamie.bainbridge@gmail.com>
5c320d
---
5c320d
 glib/gutils.c | 7 +++++--
5c320d
 1 file changed, 5 insertions(+), 2 deletions(-)
5c320d
5c320d
diff --git a/glib/gutils.c b/glib/gutils.c
5c320d
index b7a2113d4..4bccd7229 100644
5c320d
--- a/glib/gutils.c
5c320d
+++ b/glib/gutils.c
5c320d
@@ -692,14 +692,17 @@ g_get_user_database_entry (void)
5c320d
               {
5c320d
                 gchar **gecos_fields;
5c320d
                 gchar **name_parts;
5c320d
+                gchar *uppercase_pw_name;
5c320d
 
5c320d
                 /* split the gecos field and substitute '&' */
5c320d
                 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
5c320d
                 name_parts = g_strsplit (gecos_fields[0], "&", 0);
5c320d
-                pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
5c320d
-                e.real_name = g_strjoinv (pw->pw_name, name_parts);
5c320d
+                uppercase_pw_name = g_strdup (pw->pw_name);
5c320d
+                uppercase_pw_name[0] = g_ascii_toupper (uppercase_pw_name[0]);
5c320d
+                e.real_name = g_strjoinv (uppercase_pw_name, name_parts);
5c320d
                 g_strfreev (gecos_fields);
5c320d
                 g_strfreev (name_parts);
5c320d
+                g_free (uppercase_pw_name);
5c320d
               }
5c320d
 #endif
5c320d
 
5c320d
-- 
5c320d
GitLab
5c320d