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