1b5975d6bSopenharmony_ciFrom 1f86923766a3d1d319fe54ad24fcf6e2d75aca0d Mon Sep 17 00:00:00 2001
2b5975d6bSopenharmony_ciFrom: Philip Withnall <pwithnall@endlessos.org>
3b5975d6bSopenharmony_ciDate: Wed, 22 Feb 2023 12:40:49 +0000
4b5975d6bSopenharmony_ciSubject: [PATCH 1/3] gdbusinterfaceskeleton: Remove an unnecessary helper
5b5975d6bSopenharmony_ci struct member
6b5975d6bSopenharmony_ciMIME-Version: 1.0
7b5975d6bSopenharmony_ciContent-Type: text/plain; charset=UTF-8
8b5975d6bSopenharmony_ciContent-Transfer-Encoding: 8bit
9b5975d6bSopenharmony_ci
10b5975d6bSopenharmony_ciThe `GDBusInterfaceSkeleton` is already stored as the source object of
11b5975d6bSopenharmony_cithe `GTask` here, with a strong reference.
12b5975d6bSopenharmony_ci
13b5975d6bSopenharmony_ciStoring it again in the task’s data struct is redundant, and makes it
14b5975d6bSopenharmony_cilook like the `GDBusInterfaceSkeleton` is being used without holding a
15b5975d6bSopenharmony_cistrong reference. (There’s not actually a bug there though: the strong
16b5975d6bSopenharmony_cireference from the `GTask` outlives the data struct, so is sufficient.)
17b5975d6bSopenharmony_ci
18b5975d6bSopenharmony_ciRemove the unnecessary helper struct member to clarify the code a bit.
19b5975d6bSopenharmony_ci
20b5975d6bSopenharmony_ciSigned-off-by: Philip Withnall <pwithnall@endlessos.org>
21b5975d6bSopenharmony_ci
22b5975d6bSopenharmony_ciHelps: #2924
23b5975d6bSopenharmony_ci---
24b5975d6bSopenharmony_ci gio/gdbusinterfaceskeleton.c | 15 +++++++--------
25b5975d6bSopenharmony_ci 1 file changed, 7 insertions(+), 8 deletions(-)
26b5975d6bSopenharmony_ci
27b5975d6bSopenharmony_cidiff --git a/gio/gdbusinterfaceskeleton.c b/gio/gdbusinterfaceskeleton.c
28b5975d6bSopenharmony_ciindex 3f07d4d0b2..d28282fea3 100644
29b5975d6bSopenharmony_ci--- a/gio/gdbusinterfaceskeleton.c
30b5975d6bSopenharmony_ci+++ b/gio/gdbusinterfaceskeleton.c
31b5975d6bSopenharmony_ci@@ -461,7 +461,6 @@ dbus_interface_interface_init (GDBusInterfaceIface *iface)
32b5975d6bSopenharmony_ci typedef struct
33b5975d6bSopenharmony_ci {
34b5975d6bSopenharmony_ci   gint ref_count;  /* (atomic) */
35b5975d6bSopenharmony_ci-  GDBusInterfaceSkeleton       *interface;
36b5975d6bSopenharmony_ci   GDBusInterfaceMethodCallFunc  method_call_func;
37b5975d6bSopenharmony_ci   GDBusMethodInvocation        *invocation;
38b5975d6bSopenharmony_ci } DispatchData;
39b5975d6bSopenharmony_ci@@ -502,16 +501,17 @@ dispatch_in_thread_func (GTask        *task,
40b5975d6bSopenharmony_ci                          GCancellable *cancellable)
41b5975d6bSopenharmony_ci {
42b5975d6bSopenharmony_ci   DispatchData *data = task_data;
43b5975d6bSopenharmony_ci+  GDBusInterfaceSkeleton *interface = g_task_get_source_object (task);
44b5975d6bSopenharmony_ci   GDBusInterfaceSkeletonFlags flags;
45b5975d6bSopenharmony_ci   GDBusObject *object;
46b5975d6bSopenharmony_ci   gboolean authorized;
47b5975d6bSopenharmony_ci 
48b5975d6bSopenharmony_ci-  g_mutex_lock (&data->interface->priv->lock);
49b5975d6bSopenharmony_ci-  flags = data->interface->priv->flags;
50b5975d6bSopenharmony_ci-  object = data->interface->priv->object;
51b5975d6bSopenharmony_ci+  g_mutex_lock (&interface->priv->lock);
52b5975d6bSopenharmony_ci+  flags = interface->priv->flags;
53b5975d6bSopenharmony_ci+  object = interface->priv->object;
54b5975d6bSopenharmony_ci   if (object != NULL)
55b5975d6bSopenharmony_ci     g_object_ref (object);
56b5975d6bSopenharmony_ci-  g_mutex_unlock (&data->interface->priv->lock);
57b5975d6bSopenharmony_ci+  g_mutex_unlock (&interface->priv->lock);
58b5975d6bSopenharmony_ci 
59b5975d6bSopenharmony_ci   /* first check on the enclosing object (if any), then the interface */
60b5975d6bSopenharmony_ci   authorized = TRUE;
61b5975d6bSopenharmony_ci@@ -519,13 +519,13 @@ dispatch_in_thread_func (GTask        *task,
62b5975d6bSopenharmony_ci     {
63b5975d6bSopenharmony_ci       g_signal_emit_by_name (object,
64b5975d6bSopenharmony_ci                              "authorize-method",
65b5975d6bSopenharmony_ci-                             data->interface,
66b5975d6bSopenharmony_ci+                             interface,
67b5975d6bSopenharmony_ci                              data->invocation,
68b5975d6bSopenharmony_ci                              &authorized);
69b5975d6bSopenharmony_ci     }
70b5975d6bSopenharmony_ci   if (authorized)
71b5975d6bSopenharmony_ci     {
72b5975d6bSopenharmony_ci-      g_signal_emit (data->interface,
73b5975d6bSopenharmony_ci+      g_signal_emit (interface,
74b5975d6bSopenharmony_ci                      signals[G_AUTHORIZE_METHOD_SIGNAL],
75b5975d6bSopenharmony_ci                      0,
76b5975d6bSopenharmony_ci                      data->invocation,
77b5975d6bSopenharmony_ci@@ -627,7 +627,6 @@ g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface
78b5975d6bSopenharmony_ci       DispatchData *data;
79b5975d6bSopenharmony_ci 
80b5975d6bSopenharmony_ci       data = g_slice_new0 (DispatchData);
81b5975d6bSopenharmony_ci-      data->interface = interface;
82b5975d6bSopenharmony_ci       data->method_call_func = method_call_func;
83b5975d6bSopenharmony_ci       data->invocation = invocation;
84b5975d6bSopenharmony_ci       data->ref_count = 1;
85b5975d6bSopenharmony_ci-- 
86b5975d6bSopenharmony_ciGitLab
87b5975d6bSopenharmony_ci
88b5975d6bSopenharmony_ci
89b5975d6bSopenharmony_ciFrom d5710deb9d621bcf0cec0ff2db0708f361490752 Mon Sep 17 00:00:00 2001
90b5975d6bSopenharmony_ciFrom: Philip Withnall <pwithnall@endlessos.org>
91b5975d6bSopenharmony_ciDate: Wed, 22 Feb 2023 12:47:36 +0000
92b5975d6bSopenharmony_ciSubject: [PATCH 2/3] gdbusinterfaceskeleton: Fix a use-after-free of a
93b5975d6bSopenharmony_ci GDBusMethodInvocation
94b5975d6bSopenharmony_ciMIME-Version: 1.0
95b5975d6bSopenharmony_ciContent-Type: text/plain; charset=UTF-8
96b5975d6bSopenharmony_ciContent-Transfer-Encoding: 8bit
97b5975d6bSopenharmony_ci
98b5975d6bSopenharmony_ciThis `GDBusMethodInvocation` may be shared across threads, with no
99b5975d6bSopenharmony_ciguarantee on the strong ref in one thread outlasting any refs in other
100b5975d6bSopenharmony_cithreads — so it needs a ref in this helper struct.
101b5975d6bSopenharmony_ci
102b5975d6bSopenharmony_ciThis should fix a use-after-free where the `GDBusMethodInvocation` is
103b5975d6bSopenharmony_cifreed from `g_value_unset()` after `g_signal_emit()` returns in
104b5975d6bSopenharmony_ci`dispatch_in_thread_func()` in one thread; but then dereferenced again
105b5975d6bSopenharmony_ciin `g_source_destroy_internal()` from another thread.
106b5975d6bSopenharmony_ci
107b5975d6bSopenharmony_ciSigned-off-by: Philip Withnall <pwithnall@endlessos.org>
108b5975d6bSopenharmony_ci
109b5975d6bSopenharmony_ciFixes: #2924
110b5975d6bSopenharmony_ci---
111b5975d6bSopenharmony_ci gio/gdbusinterfaceskeleton.c | 9 ++++++---
112b5975d6bSopenharmony_ci 1 file changed, 6 insertions(+), 3 deletions(-)
113b5975d6bSopenharmony_ci
114b5975d6bSopenharmony_cidiff --git a/gio/gdbusinterfaceskeleton.c b/gio/gdbusinterfaceskeleton.c
115b5975d6bSopenharmony_ciindex d28282fea3..a2a79fe3d8 100644
116b5975d6bSopenharmony_ci--- a/gio/gdbusinterfaceskeleton.c
117b5975d6bSopenharmony_ci+++ b/gio/gdbusinterfaceskeleton.c
118b5975d6bSopenharmony_ci@@ -462,14 +462,17 @@ typedef struct
119b5975d6bSopenharmony_ci {
120b5975d6bSopenharmony_ci   gint ref_count;  /* (atomic) */
121b5975d6bSopenharmony_ci   GDBusInterfaceMethodCallFunc  method_call_func;
122b5975d6bSopenharmony_ci-  GDBusMethodInvocation        *invocation;
123b5975d6bSopenharmony_ci+  GDBusMethodInvocation        *invocation;  /* (owned) */
124b5975d6bSopenharmony_ci } DispatchData;
125b5975d6bSopenharmony_ci 
126b5975d6bSopenharmony_ci static void
127b5975d6bSopenharmony_ci dispatch_data_unref (DispatchData *data)
128b5975d6bSopenharmony_ci {
129b5975d6bSopenharmony_ci   if (g_atomic_int_dec_and_test (&data->ref_count))
130b5975d6bSopenharmony_ci-    g_slice_free (DispatchData, data);
131b5975d6bSopenharmony_ci+    {
132b5975d6bSopenharmony_ci+      g_clear_object (&data->invocation);
133b5975d6bSopenharmony_ci+      g_slice_free (DispatchData, data);
134b5975d6bSopenharmony_ci+    }
135b5975d6bSopenharmony_ci }
136b5975d6bSopenharmony_ci 
137b5975d6bSopenharmony_ci static DispatchData *
138b5975d6bSopenharmony_ci@@ -628,7 +631,7 @@ g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface
139b5975d6bSopenharmony_ci 
140b5975d6bSopenharmony_ci       data = g_slice_new0 (DispatchData);
141b5975d6bSopenharmony_ci       data->method_call_func = method_call_func;
142b5975d6bSopenharmony_ci-      data->invocation = invocation;
143b5975d6bSopenharmony_ci+      data->invocation = g_object_ref (invocation);
144b5975d6bSopenharmony_ci       data->ref_count = 1;
145b5975d6bSopenharmony_ci 
146b5975d6bSopenharmony_ci       task = g_task_new (interface, NULL, NULL, NULL);
147b5975d6bSopenharmony_ci-- 
148b5975d6bSopenharmony_ciGitLab
149b5975d6bSopenharmony_ci
150b5975d6bSopenharmony_ci
151b5975d6bSopenharmony_ciFrom 7b101588e924f3783a0f5075f06b3e1d698be936 Mon Sep 17 00:00:00 2001
152b5975d6bSopenharmony_ciFrom: Philip Withnall <pwithnall@endlessos.org>
153b5975d6bSopenharmony_ciDate: Wed, 22 Feb 2023 12:50:10 +0000
154b5975d6bSopenharmony_ciSubject: [PATCH 3/3] gdbusconnection: Make GDBusMethodInvocation transfer a
155b5975d6bSopenharmony_ci bit clearer
156b5975d6bSopenharmony_ci
157b5975d6bSopenharmony_ciAdd a missing steal call in `schedule_method_call()`. This introduces no
158b5975d6bSopenharmony_cifunctional changes, but documents the ownership transfer more clearly.
159b5975d6bSopenharmony_ci
160b5975d6bSopenharmony_ciSigned-off-by: Philip Withnall <pwithnall@endlessos.org>
161b5975d6bSopenharmony_ci
162b5975d6bSopenharmony_ciHelps: #2924
163b5975d6bSopenharmony_ci---
164b5975d6bSopenharmony_ci gio/gdbusconnection.c | 2 +-
165b5975d6bSopenharmony_ci 1 file changed, 1 insertion(+), 1 deletion(-)
166b5975d6bSopenharmony_ci
167b5975d6bSopenharmony_cidiff --git a/gio/gdbusconnection.c b/gio/gdbusconnection.c
168b5975d6bSopenharmony_ciindex d938f71b99..da6b66f2ec 100644
169b5975d6bSopenharmony_ci--- a/gio/gdbusconnection.c
170b5975d6bSopenharmony_ci+++ b/gio/gdbusconnection.c
171b5975d6bSopenharmony_ci@@ -5043,7 +5043,7 @@ schedule_method_call (GDBusConnection            *connection,
172b5975d6bSopenharmony_ci   g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
173b5975d6bSopenharmony_ci   g_source_set_callback (idle_source,
174b5975d6bSopenharmony_ci                          call_in_idle_cb,
175b5975d6bSopenharmony_ci-                         invocation,
176b5975d6bSopenharmony_ci+                         g_steal_pointer (&invocation),
177b5975d6bSopenharmony_ci                          g_object_unref);
178b5975d6bSopenharmony_ci   g_source_set_static_name (idle_source, "[gio, " __FILE__ "] call_in_idle_cb");
179b5975d6bSopenharmony_ci   g_source_attach (idle_source, main_context);
180b5975d6bSopenharmony_ci-- 
181b5975d6bSopenharmony_ciGitLab
182b5975d6bSopenharmony_ci
183