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