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