1From c86aea01a06ad4d6c428137e9cfe2f74b1ae7f01 Mon Sep 17 00:00:00 2001
2From: Jan Rybar <jrybar@redhat.com>
3Date: Mon, 21 Feb 2022 08:29:05 +0000
4Subject: [PATCH 2/3] CVE-2021-4115 (GHSL-2021-077) fix
5
6Signed-off-by: Mikko Rapeli <mikko.rapeli@bmw.de>
7
8---
9 src/polkit/polkitsystembusname.c | 38 ++++++++++++++++++++++++++++----
10 1 file changed, 34 insertions(+), 4 deletions(-)
11
12CVE: CVE-2021-4115
13Upstream-Status: Backport [41cb093f554da8772362654a128a84dd8a5542a7]
14
15diff --git a/src/polkit/polkitsystembusname.c b/src/polkit/polkitsystembusname.c
16index 8ed1363..2fbf5f1 100644
17--- a/src/polkit/polkitsystembusname.c
18+++ b/src/polkit/polkitsystembusname.c
19@@ -62,6 +62,10 @@ enum
20   PROP_NAME,
21 };
22
23+
24+guint8 dbus_call_respond_fails;      // has to be global because of callback
25+
26+
27 static void subject_iface_init (PolkitSubjectIface *subject_iface);
28
29 G_DEFINE_TYPE_WITH_CODE (PolkitSystemBusName, polkit_system_bus_name, G_TYPE_OBJECT,
30@@ -364,6 +368,7 @@ on_retrieved_unix_uid_pid (GObject              *src,
31   if (!v)
32     {
33       data->caught_error = TRUE;
34+      dbus_call_respond_fails += 1;
35     }
36   else
37     {
38@@ -405,6 +410,8 @@ polkit_system_bus_name_get_creds_sync (PolkitSystemBusName           *system_bus
39   tmp_context = g_main_context_new ();
40   g_main_context_push_thread_default (tmp_context);
41
42+  dbus_call_respond_fails = 0;
43+
44   /* Do two async calls as it's basically as fast as one sync call.
45    */
46   g_dbus_connection_call (connection,
47@@ -432,11 +439,34 @@ polkit_system_bus_name_get_creds_sync (PolkitSystemBusName           *system_bus
48 			  on_retrieved_unix_uid_pid,
49 			  &data);
50
51-  while (!((data.retrieved_uid && data.retrieved_pid) || data.caught_error))
52-    g_main_context_iteration (tmp_context, TRUE);
53+  while (TRUE)
54+  {
55+    /* If one dbus call returns error, we must wait until the other call
56+     * calls _call_finish(), otherwise fd leak is possible.
57+     * Resolves: GHSL-2021-077
58+    */
59
60-  if (data.caught_error)
61-    goto out;
62+    if ( (dbus_call_respond_fails > 1) )
63+    {
64+      // we got two faults, we can leave
65+      goto out;
66+    }
67+
68+    if ((data.caught_error && (data.retrieved_pid || data.retrieved_uid)))
69+    {
70+      // we got one fault and the other call finally finished, we can leave
71+      goto out;
72+    }
73+
74+    if ( !(data.retrieved_uid && data.retrieved_pid) )
75+    {
76+      g_main_context_iteration (tmp_context, TRUE);
77+    }
78+    else
79+    {
80+      break;
81+    }
82+  }
83
84   if (out_uid)
85     *out_uid = data.uid;
86--
872.20.1
88
89