5137f5
From 89229c2b0b139c527e98fc3bf1a829bc2b68394e Mon Sep 17 00:00:00 2001
5137f5
From: Tomas Bzatek <tbzatek@redhat.com>
5137f5
Date: Wed, 15 Jan 2014 16:16:34 +0000
5137f5
Subject: Use reentrant version of getpwuid() for thread safety
5137f5
5137f5
...and get rid of static variables.
5137f5
5137f5
https://bugs.freedesktop.org/show_bug.cgi?id=73669
5137f5
5137f5
Signed-off-by: David Zeuthen <zeuthen@gmail.com>
5137f5
---
5137f5
diff --git a/src/udisksdaemonutil.c b/src/udisksdaemonutil.c
5137f5
index 574bf2c..b3a3acb 100644
5137f5
--- a/src/udisksdaemonutil.c
5137f5
+++ b/src/udisksdaemonutil.c
5137f5
@@ -830,7 +830,7 @@ udisks_daemon_util_get_caller_uid_sync (UDisksDaemon            *daemon,
5137f5
     {
5137f5
       struct passwd pwstruct;
5137f5
       gchar pwbuf[8192];
5137f5
-      static struct passwd *pw;
5137f5
+      struct passwd *pw = NULL;
5137f5
       int rc;
5137f5
 
5137f5
       rc = getpwuid_r (uid, &pwstruct, pwbuf, sizeof pwbuf, &pw;;
5137f5
diff --git a/src/udiskslinuxfilesystem.c b/src/udiskslinuxfilesystem.c
5137f5
index 4c8d8aa..f243046 100644
5137f5
--- a/src/udiskslinuxfilesystem.c
5137f5
+++ b/src/udiskslinuxfilesystem.c
5137f5
@@ -348,13 +348,16 @@ find_mount_options_for_fs (const gchar *fstype)
5137f5
 static gid_t
5137f5
 find_primary_gid (uid_t uid)
5137f5
 {
5137f5
-  struct passwd *pw;
5137f5
+  struct passwd *pw = NULL;
5137f5
+  struct passwd pwstruct;
5137f5
+  gchar pwbuf[8192];
5137f5
+  int rc;
5137f5
   gid_t gid;
5137f5
 
5137f5
   gid = (gid_t) - 1;
5137f5
 
5137f5
-  pw = getpwuid (uid);
5137f5
-  if (pw == NULL)
5137f5
+  rc = getpwuid_r (uid, &pwstruct, pwbuf, sizeof pwbuf, &pw;;
5137f5
+  if (rc != 0 || pw == NULL)
5137f5
     {
5137f5
       udisks_warning ("Error looking up uid %d: %m", uid);
5137f5
       goto out;
5137f5
@@ -370,7 +373,10 @@ is_uid_in_gid (uid_t uid,
5137f5
                gid_t gid)
5137f5
 {
5137f5
   gboolean ret;
5137f5
-  struct passwd *pw;
5137f5
+  struct passwd *pw = NULL;
5137f5
+  struct passwd pwstruct;
5137f5
+  gchar pwbuf[8192];
5137f5
+  int rc;
5137f5
   static gid_t supplementary_groups[128];
5137f5
   int num_supplementary_groups = 128;
5137f5
   int n;
5137f5
@@ -379,8 +385,8 @@ is_uid_in_gid (uid_t uid,
5137f5
 
5137f5
   ret = FALSE;
5137f5
 
5137f5
-  pw = getpwuid (uid);
5137f5
-  if (pw == NULL)
5137f5
+  rc = getpwuid_r (uid, &pwstruct, pwbuf, sizeof pwbuf, &pw;;
5137f5
+  if (rc != 0 || pw == NULL)
5137f5
     {
5137f5
       udisks_warning ("Error looking up uid %d: %m", uid);
5137f5
       goto out;
5137f5
diff --git a/src/udisksspawnedjob.c b/src/udisksspawnedjob.c
5137f5
index 802551f..b181933 100644
5137f5
--- a/src/udisksspawnedjob.c
5137f5
+++ b/src/udisksspawnedjob.c
5137f5
@@ -371,22 +371,25 @@ static void
5137f5
 child_setup (gpointer user_data)
5137f5
 {
5137f5
   UDisksSpawnedJob *job = UDISKS_SPAWNED_JOB (user_data);
5137f5
-  struct passwd *pw;
5137f5
+  struct passwd pwstruct;
5137f5
+  gchar pwbuf[8192];
5137f5
+  struct passwd *pw = NULL;
5137f5
+  int rc;
5137f5
   gid_t egid;
5137f5
 
5137f5
   if (job->run_as_uid == getuid () && job->run_as_euid == geteuid ())
5137f5
     goto out;
5137f5
 
5137f5
-  pw = getpwuid (job->run_as_euid);
5137f5
-  if (pw == NULL)
5137f5
+  rc = getpwuid_r (job->run_as_euid, &pwstruct, pwbuf, sizeof pwbuf, &pw;;
5137f5
+  if (rc != 0 || pw == NULL)
5137f5
    {
5137f5
      g_printerr ("No password record for uid %d: %m\n", (gint) job->run_as_euid);
5137f5
      abort ();
5137f5
    }
5137f5
   egid = pw->pw_gid;
5137f5
 
5137f5
-  pw = getpwuid (job->run_as_uid);
5137f5
-  if (pw == NULL)
5137f5
+  rc = getpwuid_r (job->run_as_uid, &pwstruct, pwbuf, sizeof pwbuf, &pw;;
5137f5
+  if (rc != 0 || pw == NULL)
5137f5
    {
5137f5
      g_printerr ("No password record for uid %d: %m\n", (gint) job->run_as_uid);
5137f5
      abort ();
5137f5
--
5137f5
cgit v0.9.0.2-2-gbebe