From de61722efe8a43c282004d4dcb8293fed037368a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 22 Feb 2022 21:13:58 +0100 Subject: [PATCH] core: add nm_dbus_manager_lookup_object_with_type() helper This makes the non-obvious fact clearer, that when you look up an object by an untrusted, user-provided path, it might not be the object type you are looking for. In basically all cases, you need to check that the result is of the expected type. This helper makes that clearer. --- src/core/nm-dbus-manager.c | 15 +++++++++++++++ src/core/nm-dbus-manager.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/core/nm-dbus-manager.c b/src/core/nm-dbus-manager.c index 2c5f7acee8..7fcbf6cad8 100644 --- a/src/core/nm-dbus-manager.c +++ b/src/core/nm-dbus-manager.c @@ -1082,6 +1082,21 @@ nm_dbus_manager_lookup_object(NMDBusManager *self, const char *path) return obj; } +gpointer +nm_dbus_manager_lookup_object_with_type(NMDBusManager *self, GType gtype, const char *path) +{ + gpointer ptr; + + nm_assert(g_type_is_a(gtype, NM_TYPE_DBUS_OBJECT)); + nm_assert(gtype != NM_TYPE_DBUS_OBJECT); + + ptr = nm_dbus_manager_lookup_object(self, path); + if (!ptr || !G_TYPE_CHECK_INSTANCE_TYPE(ptr, gtype)) + return NULL; + + return ptr; +} + void _nm_dbus_manager_obj_export(NMDBusObject *obj) { diff --git a/src/core/nm-dbus-manager.h b/src/core/nm-dbus-manager.h index d7ca78d174..d977f99ffd 100644 --- a/src/core/nm-dbus-manager.h +++ b/src/core/nm-dbus-manager.h @@ -52,6 +52,9 @@ gboolean nm_dbus_manager_is_stopping(NMDBusManager *self); gpointer nm_dbus_manager_lookup_object(NMDBusManager *self, const char *path); +gpointer +nm_dbus_manager_lookup_object_with_type(NMDBusManager *self, GType gtype, const char *path); + void _nm_dbus_manager_obj_export(NMDBusObject *obj); void _nm_dbus_manager_obj_unexport(NMDBusObject *obj); void