15fb41
diff --git a/src/polkit/polkitsystembusname.c b/src/polkit/polkitsystembusname.c
15fb41
index 8ed1363..2fbf5f1 100644
15fb41
--- a/src/polkit/polkitsystembusname.c
15fb41
+++ b/src/polkit/polkitsystembusname.c
15fb41
@@ -62,6 +62,10 @@ enum
15fb41
   PROP_NAME,
15fb41
 };
15fb41
 
15fb41
+
15fb41
+guint8 dbus_call_respond_fails;      // has to be global because of callback
15fb41
+
15fb41
+
15fb41
 static void subject_iface_init (PolkitSubjectIface *subject_iface);
15fb41
 
15fb41
 G_DEFINE_TYPE_WITH_CODE (PolkitSystemBusName, polkit_system_bus_name, G_TYPE_OBJECT,
15fb41
@@ -364,6 +368,7 @@ on_retrieved_unix_uid_pid (GObject              *src,
15fb41
   if (!v)
15fb41
     {
15fb41
       data->caught_error = TRUE;
15fb41
+      dbus_call_respond_fails += 1;
15fb41
     }
15fb41
   else
15fb41
     {
15fb41
@@ -405,6 +410,8 @@ polkit_system_bus_name_get_creds_sync (PolkitSystemBusName           *system_bus
15fb41
   tmp_context = g_main_context_new ();
15fb41
   g_main_context_push_thread_default (tmp_context);
15fb41
 
15fb41
+  dbus_call_respond_fails = 0;
15fb41
+
15fb41
   /* Do two async calls as it's basically as fast as one sync call.
15fb41
    */
15fb41
   g_dbus_connection_call (connection,
15fb41
@@ -432,11 +439,34 @@ polkit_system_bus_name_get_creds_sync (PolkitSystemBusName           *system_bus
15fb41
 			  on_retrieved_unix_uid_pid,
15fb41
 			  &data);
15fb41
 
15fb41
-  while (!((data.retrieved_uid && data.retrieved_pid) || data.caught_error))
15fb41
-    g_main_context_iteration (tmp_context, TRUE);
15fb41
+  while (TRUE)
15fb41
+  {
15fb41
+    /* If one dbus call returns error, we must wait until the other call
15fb41
+     * calls _call_finish(), otherwise fd leak is possible.
15fb41
+     * Resolves: GHSL-2021-077
15fb41
+    */
15fb41
 
15fb41
-  if (data.caught_error)
15fb41
-    goto out;
15fb41
+    if ( (dbus_call_respond_fails > 1) )
15fb41
+    {
15fb41
+      // we got two faults, we can leave
15fb41
+      goto out;
15fb41
+    }
15fb41
+
15fb41
+    if ((data.caught_error && (data.retrieved_pid || data.retrieved_uid)))
15fb41
+    {
15fb41
+      // we got one fault and the other call finally finished, we can leave
15fb41
+      goto out;
15fb41
+    }
15fb41
+
15fb41
+    if ( !(data.retrieved_uid && data.retrieved_pid) )
15fb41
+    {
15fb41
+      g_main_context_iteration (tmp_context, TRUE);
15fb41
+    }
15fb41
+    else
15fb41
+    {
15fb41
+      break;
15fb41
+    }
15fb41
+  }
15fb41
 
15fb41
   if (out_uid)
15fb41
     *out_uid = data.uid;