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