Debian Codegraph @

src/polkit/polkitsystembusname.c

raw
1 /*
2 * Copyright (C) 2008 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: David Zeuthen <[email protected]>
20 */
21
22 #include <string.h>
23 #include <gio/gunixfdlist.h>
24 #include "polkitsystembusname.h"
25 #include "polkitunixuser.h"
26 #include "polkitsubject.h"
27 #include "polkitprivate.h"
28
29 #include "polkitunixprocess.h"
30
31 /**
32 * SECTION:polkitsystembusname
33 * @title: PolkitSystemBusName
34 * @short_description: Unique system bus names
35 *
36 * An object that represents a process owning a unique name on the system bus.
37 */
38
39 /**
40 * PolkitUnixSystemBusName:
41 *
42 * The #PolkitSystemBusName struct should not be accessed directly.
43 */
44 struct _PolkitSystemBusName
45 {
46 GObject parent_instance;
47
48 gchar *name;
49 };
50
51 struct _PolkitSystemBusNameClass
52 {
53 GObjectClass parent_class;
54 };
55
56 enum
57 {
58 PROP_0,
59 PROP_NAME,
60 };
61
62
63 guint8 dbus_call_respond_fails; // has to be global because of callback
64
65
66 static void subject_iface_init (PolkitSubjectIface *subject_iface);
67
68 G_DEFINE_TYPE_WITH_CODE (PolkitSystemBusName, polkit_system_bus_name, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
70 );
71
72 static void
73 polkit_system_bus_name_init (PolkitSystemBusName *system_bus_name)
74 {
75 }
76
77 static void
78 polkit_system_bus_name_finalize (GObject *object)
79 {
80 PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object);
81
82 g_free (system_bus_name->name);
83
84 if (G_OBJECT_CLASS (polkit_system_bus_name_parent_class)->finalize != NULL)
85 G_OBJECT_CLASS (polkit_system_bus_name_parent_class)->finalize (object);
86 }
87
88 static void
89 polkit_system_bus_name_get_property (GObject *object,
90 guint prop_id,
91 GValue *value,
92 GParamSpec *pspec)
93 {
94 PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object);
95
96 switch (prop_id)
97 {
98 case PROP_NAME:
99 g_value_set_string (value, system_bus_name->name);
100 break;
101
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 break;
105 }
106 }
107
108 static void
109 polkit_system_bus_name_set_property (GObject *object,
110 guint prop_id,
111 const GValue *value,
112 GParamSpec *pspec)
113 {
114 PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object);
115
116 switch (prop_id)
117 {
118 case PROP_NAME:
119 polkit_system_bus_name_set_name (system_bus_name, g_value_get_string (value));
120 break;
121
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 break;
125 }
126 }
127
128 static void
129 polkit_system_bus_name_class_init (PolkitSystemBusNameClass *klass)
130 {
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
132
133 gobject_class->get_property = polkit_system_bus_name_get_property;
134 gobject_class->set_property = polkit_system_bus_name_set_property;
135 gobject_class->finalize = polkit_system_bus_name_finalize;
136
137 /**
138 * PolkitSystemBusName:name:
139 *
140 * The unique name on the system message bus.
141 */
142 g_object_class_install_property (gobject_class,
143 PROP_NAME,
144 g_param_spec_string ("name",
145 "Name",
146 "The unique name on the system message bus",
147 NULL,
148 G_PARAM_CONSTRUCT |
149 G_PARAM_READWRITE |
150 G_PARAM_STATIC_NAME |
151 G_PARAM_STATIC_BLURB |
152 G_PARAM_STATIC_NICK));
153
154 }
155
156 /**
157 * polkit_system_bus_name_get_name:
158 * @system_bus_name: A #PolkitSystemBusName.
159 *
160 * Gets the unique system bus name for @system_bus_name.
161 *
162 * Returns: The unique system bus name for @system_bus_name. Do not
163 * free, this string is owned by @system_bus_name.
164 */
165 const gchar *
166 polkit_system_bus_name_get_name (PolkitSystemBusName *system_bus_name)
167 {
168 g_return_val_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (system_bus_name), NULL);
169 return system_bus_name->name;
170 }
171
172 /**
173 * polkit_system_bus_name_set_name:
174 * @system_bus_name: A #PolkitSystemBusName.
175 * @name: A unique system bus name.
176 *
177 * Sets the unique system bus name for @system_bus_name.
178 */
179 void
180 polkit_system_bus_name_set_name (PolkitSystemBusName *system_bus_name,
181 const gchar *name)
182 {
183 g_return_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (system_bus_name));
184 g_return_if_fail (g_dbus_is_unique_name (name));
185 g_free (system_bus_name->name);
186 system_bus_name->name = g_strdup (name);
187 }
188
189 /**
190 * polkit_system_bus_name_new:
191 * @name: A unique system bus name.
192 *
193 * Creates a new #PolkitSystemBusName for @name.
194 *
195 * Returns: (transfer full): A #PolkitSystemBusName. Free with g_object_unref().
196 */
197 PolkitSubject *
198 polkit_system_bus_name_new (const gchar *name)
199 {
200 g_return_val_if_fail (g_dbus_is_unique_name (name), NULL);
201 return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_SYSTEM_BUS_NAME,
202 "name", name,
203 NULL));
204 }
205
206 static guint
207 polkit_system_bus_name_hash (PolkitSubject *subject)
208 {
209 PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (subject);
210
211 return g_str_hash (system_bus_name->name);
212 }
213
214 static gboolean
215 polkit_system_bus_name_equal (PolkitSubject *a,
216 PolkitSubject *b)
217 {
218 PolkitSystemBusName *name_a;
219 PolkitSystemBusName *name_b;
220
221 name_a = POLKIT_SYSTEM_BUS_NAME (a);
222 name_b = POLKIT_SYSTEM_BUS_NAME (b);
223
224 return strcmp (name_a->name, name_b->name) == 0;
225 }
226
227 static gchar *
228 polkit_system_bus_name_to_string (PolkitSubject *subject)
229 {
230 PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (subject);
231
232 return g_strdup_printf ("system-bus-name:%s", system_bus_name->name);
233 }
234
235 static gboolean
236 polkit_system_bus_name_exists_sync (PolkitSubject *subject,
237 GCancellable *cancellable,
238 GError **error)
239 {
240 PolkitSystemBusName *name = POLKIT_SYSTEM_BUS_NAME (subject);
241 GDBusConnection *connection;
242 GVariant *result;
243 gboolean ret;
244
245 ret = FALSE;
246
247 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
248 if (connection == NULL)
249 goto out;
250
251 result = g_dbus_connection_call_sync (connection,
252 "org.freedesktop.DBus", /* name */
253 "/org/freedesktop/DBus", /* object path */
254 "org.freedesktop.DBus", /* interface name */
255 "NameHasOwner", /* method */
256 g_variant_new ("(s)", name->name),
257 G_VARIANT_TYPE ("(b)"),
258 G_DBUS_CALL_FLAGS_NONE,
259 -1,
260 cancellable,
261 error);
262 if (result == NULL)
263 goto out;
264
265 g_variant_get (result, "(b)", &ret);
266 g_variant_unref (result);
267
268 out:
269 if (connection != NULL)
270 g_object_unref (connection);
271 return ret;
272 }
273
274 static void
275 exists_in_thread_func (GSimpleAsyncResult *res,
276 GObject *object,
277 GCancellable *cancellable)
278 {
279 GError *error;
280 error = NULL;
281 if (!polkit_system_bus_name_exists_sync (POLKIT_SUBJECT (object),
282 cancellable,
283 &error))
284 {
285 g_simple_async_result_set_from_error (res, error);
286 g_error_free (error);
287 }
288 }
289
290 static void
291 polkit_system_bus_name_exists (PolkitSubject *subject,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
294 gpointer user_data)
295 {
296 GSimpleAsyncResult *simple;
297
298 g_return_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (subject));
299
300 simple = g_simple_async_result_new (G_OBJECT (subject),
301 callback,
302 user_data,
303 polkit_system_bus_name_exists);
304 g_simple_async_result_run_in_thread (simple,
305 exists_in_thread_func,
306 G_PRIORITY_DEFAULT,
307 cancellable);
308 g_object_unref (simple);
309 }
310
311 static gboolean
312 polkit_system_bus_name_exists_finish (PolkitSubject *subject,
313 GAsyncResult *res,
314 GError **error)
315 {
316 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
317 gboolean ret;
318
319 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_system_bus_name_exists);
320
321 ret = FALSE;
322
323 if (g_simple_async_result_propagate_error (simple, error))
324 goto out;
325
326 ret = g_simple_async_result_get_op_res_gboolean (simple);
327
328 out:
329 return ret;
330 }
331
332 static void
333 subject_iface_init (PolkitSubjectIface *subject_iface)
334 {
335 subject_iface->hash = polkit_system_bus_name_hash;
336 subject_iface->equal = polkit_system_bus_name_equal;
337 subject_iface->to_string = polkit_system_bus_name_to_string;
338 subject_iface->exists = polkit_system_bus_name_exists;
339 subject_iface->exists_finish = polkit_system_bus_name_exists_finish;
340 subject_iface->exists_sync = polkit_system_bus_name_exists_sync;
341 }
342
343 /* ---------------------------------------------------------------------------------------------------- */
344
345 typedef struct {
346 GError **error;
347 guint retrieved_uid : 1;
348 guint retrieved_pid : 1;
349 guint caught_error : 1;
350
351 guint32 uid;
352 guint32 pid;
353 } AsyncGetBusNameCredsData;
354
355 static void
356 on_retrieved_unix_uid_pid (GObject *src,
357 GAsyncResult *res,
358 gpointer user_data)
359 {
360 AsyncGetBusNameCredsData *data = user_data;
361 GVariant *v;
362
363 v = g_dbus_connection_call_finish ((GDBusConnection*)src, res,
364 data->caught_error ? NULL : data->error);
365 if (!v)
366 {
367 data->caught_error = TRUE;
368 dbus_call_respond_fails += 1;
369 }
370 else
371 {
372 guint32 value;
373 g_variant_get (v, "(u)", &value);
374 g_variant_unref (v);
375 if (!data->retrieved_uid)
376 {
377 data->retrieved_uid = TRUE;
378 data->uid = value;
379 }
380 else
381 {
382 g_assert (!data->retrieved_pid);
383 data->retrieved_pid = TRUE;
384 data->pid = value;
385 }
386 }
387 }
388
389 static gboolean
390 polkit_system_bus_name_get_creds_fallback (PolkitSystemBusName *system_bus_name,
391 guint32 *out_uid,
392 guint32 *out_pid,
393 GCancellable *cancellable,
394 GDBusConnection *connection,
395 GMainContext *tmp_context,
396 GError **error)
397 {
398 gboolean ret = FALSE;
399 AsyncGetBusNameCredsData data = { };
400
401 data.error = error;
402
403 dbus_call_respond_fails = 0;
404
405 /* Do two async calls as it's basically as fast as one sync call.
406 */
407 g_dbus_connection_call (connection,
408 "org.freedesktop.DBus", /* name */
409 "/org/freedesktop/DBus", /* object path */
410 "org.freedesktop.DBus", /* interface name */
411 "GetConnectionUnixUser", /* method */
412 g_variant_new ("(s)", system_bus_name->name),
413 G_VARIANT_TYPE ("(u)"),
414 G_DBUS_CALL_FLAGS_NONE,
415 -1,
416 cancellable,
417 on_retrieved_unix_uid_pid,
418 &data);
419 g_dbus_connection_call (connection,
420 "org.freedesktop.DBus", /* name */
421 "/org/freedesktop/DBus", /* object path */
422 "org.freedesktop.DBus", /* interface name */
423 "GetConnectionUnixProcessID", /* method */
424 g_variant_new ("(s)", system_bus_name->name),
425 G_VARIANT_TYPE ("(u)"),
426 G_DBUS_CALL_FLAGS_NONE,
427 -1,
428 cancellable,
429 on_retrieved_unix_uid_pid,
430 &data);
431
432 while (TRUE)
433 {
434 /* If one dbus call returns error, we must wait until the other call
435 * calls _call_finish(), otherwise fd leak is possible.
436 * Resolves: GHSL-2021-077
437 */
438
439 if ( (dbus_call_respond_fails > 1) )
440 {
441 // we got two faults, we can leave
442 goto out;
443 }
444
445 if ((data.caught_error && (data.retrieved_pid || data.retrieved_uid)))
446 {
447 // we got one fault and the other call finally finished, we can leave
448 goto out;
449 }
450
451 if ( !(data.retrieved_uid && data.retrieved_pid) )
452 {
453 g_main_context_iteration (tmp_context, TRUE);
454 }
455 else
456 {
457 break;
458 }
459 }
460
461 if (out_uid)
462 *out_uid = data.uid;
463 if (out_pid)
464 *out_pid = data.pid;
465 ret = TRUE;
466 out:
467 if (tmp_context)
468 {
469 g_main_context_pop_thread_default (tmp_context);
470 g_main_context_unref (tmp_context);
471 }
472 if (connection != NULL)
473 g_object_unref (connection);
474
475 return ret;
476 }
477
478 static gboolean
479 polkit_system_bus_name_get_creds_sync (PolkitSystemBusName *system_bus_name,
480 guint32 *out_uid,
481 GArray **out_gids,
482 guint32 *out_pid,
483 gint *out_pidfd,
484 GCancellable *cancellable,
485 GError **error)
486 {
487 gboolean ret = FALSE;
488 GDBusConnection *connection = NULL;
489 GMainContext *tmp_context = NULL;
490 GVariantIter *iter;
491 GVariant *result, *value;
492 GUnixFDList *fd_list = NULL;
493 GError *dbus_error = NULL;
494 const gchar *key;
495 guint32 uid = G_MAXUINT32, pid = 0;
496 gint pidfd = -1;
497 GArray *gids = NULL;
498
499 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
500 if (connection == NULL)
501 goto out;
502
503 tmp_context = g_main_context_new ();
504 g_main_context_push_thread_default (tmp_context);
505
506 /* If the new unified API is available (since dbus-daemon 1.10.4) use it,
507 * or fallback to the old separate calls.
508 * Since dbus-daemon 1.15.7 and dbus-broker 34, the new API will return
509 * a ProcessFD that we can use to pin the caller against PID reuse.
510 */
511 result = g_dbus_connection_call_with_unix_fd_list_sync (connection,
512 "org.freedesktop.DBus", /* name */
513 "/org/freedesktop/DBus", /* object path */
514 "org.freedesktop.DBus", /* interface name */
515 "GetConnectionCredentials", /* method */
516 g_variant_new ("(s)", system_bus_name->name),
517 G_VARIANT_TYPE ("(a{sv})"),
518 G_DBUS_CALL_FLAGS_NONE,
519 -1,
520 NULL,
521 &fd_list,
522 cancellable,
523 &dbus_error);
524
525 if (result == NULL)
526 {
527 if (g_error_matches (dbus_error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
528 {
529 g_error_free (dbus_error);
530 return polkit_system_bus_name_get_creds_fallback(system_bus_name,
531 out_uid,
532 out_pid,
533 cancellable,
534 connection,
535 tmp_context,
536 error);
537 }
538 else
539 goto out;
540 }
541
542 g_variant_get (result, "(a{sv})", &iter);
543
544 while (g_variant_iter_loop (iter, "{&sv}", &key, &value))
545 {
546 if (g_strcmp0 (key, "ProcessID") == 0)
547 pid = g_variant_get_uint32 (value);
548 else if (g_strcmp0 (key, "UnixUserID") == 0)
549 uid = g_variant_get_uint32 (value);
550 else if (g_strcmp0 (key, "UnixGroupIDs") == 0)
551 {
552 GVariantIter *group_iter;
553 gid_t gid;
554
555 gids = g_array_new (FALSE, FALSE, sizeof (gid_t));
556 g_variant_get (value, "au", &group_iter);
557 while (g_variant_iter_loop (group_iter, "u", &gid))
558 g_array_append_val (gids, gid);
559 g_variant_iter_free (group_iter);
560 }
561 else if (g_strcmp0 (key, "ProcessFD") == 0)
562 {
563 gint32 index = g_variant_get_handle (value);
564 pidfd = g_unix_fd_list_get (fd_list, index, error);
565 }
566 }
567
568 g_variant_unref (result);
569 g_variant_iter_free (iter);
570
571 if (out_uid)
572 *out_uid = uid;
573 if (out_gids && gids)
574 *out_gids = g_array_ref(gids);
575 if (out_pid)
576 *out_pid = pid;
577 if (out_pidfd)
578 *out_pidfd = pidfd;
579 else if (pidfd >= 0)
580 close (pidfd);
581 ret = TRUE;
582 out:
583 if (tmp_context)
584 {
585 g_main_context_pop_thread_default (tmp_context);
586 g_main_context_unref (tmp_context);
587 }
588 if (connection != NULL)
589 g_object_unref (connection);
590 if (!ret && pidfd >= 0)
591 close (pidfd);
592 if (dbus_error && error)
593 g_propagate_error (error, dbus_error);
594 else if (dbus_error)
595 g_error_free (dbus_error);
596 if (gids)
597 g_array_unref (gids);
598 if (fd_list != NULL)
599 g_object_unref (fd_list);
600
601 return ret;
602 }
603
604 /**
605 * polkit_system_bus_name_get_process_sync:
606 * @system_bus_name: A #PolkitSystemBusName.
607 * @cancellable: (allow-none): A #GCancellable or %NULL.
608 * @error: (allow-none): Return location for error or %NULL.
609 *
610 * Synchronously gets a #PolkitUnixProcess object for @system_bus_name
611 * - the calling thread is blocked until a reply is received.
612 *
613 * Returns: (allow-none) (transfer full): A #PolkitUnixProcess object or %NULL if @error is set.
614 **/
615 PolkitSubject *
616 polkit_system_bus_name_get_process_sync (PolkitSystemBusName *system_bus_name,
617 GCancellable *cancellable,
618 GError **error)
619 {
620 PolkitSubject *ret = NULL;
621 gint pidfd = -1;
622 guint32 pid;
623 guint32 uid;
624 GArray *gids = NULL;
625
626 g_return_val_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (system_bus_name), NULL);
627 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
628 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
629
630 if (!polkit_system_bus_name_get_creds_sync (system_bus_name, &uid, &gids, &pid, &pidfd,
631 cancellable, error))
632 goto out;
633
634 if (pidfd >= 0)
635 ret = polkit_unix_process_new_pidfd (pidfd, uid, gids);
636 else
637 ret = polkit_unix_process_new_for_owner (pid, 0, uid);
638
639 polkit_unix_process_set_gids (POLKIT_UNIX_PROCESS (ret), gids);
640
641 out:
642 if (gids)
643 g_array_unref (gids);
644 return ret;
645 }
646
647 /**
648 * polkit_system_bus_name_get_user_sync:
649 * @system_bus_name: A #PolkitSystemBusName.
650 * @cancellable: (allow-none): A #GCancellable or %NULL.
651 * @error: (allow-none): Return location for error or %NULL.
652 *
653 * Synchronously gets a #PolkitUnixUser object for @system_bus_name;
654 * the calling thread is blocked until a reply is received.
655 *
656 * Returns: (allow-none) (transfer full): A #PolkitUnixUser object or %NULL if @error is set.
657 **/
658 PolkitUnixUser *
659 polkit_system_bus_name_get_user_sync (PolkitSystemBusName *system_bus_name,
660 GCancellable *cancellable,
661 GError **error)
662 {
663 PolkitUnixUser *ret = NULL;
664 guint32 uid;
665
666 g_return_val_if_fail (POLKIT_IS_SYSTEM_BUS_NAME (system_bus_name), NULL);
667 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
668 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
669
670 if (!polkit_system_bus_name_get_creds_sync (system_bus_name, &uid, NULL, NULL, NULL,
671 cancellable, error))
672 goto out;
673
674 ret = (PolkitUnixUser*)polkit_unix_user_new (uid);
675
676 out:
677 return ret;
678 }