From ad83cfb26c2dd8d4532a634e105baaee76441c8f Mon Sep 17 00:00:00 2001
From: Tomas Bzatek <tbzatek@redhat.com>
Date: Mon, 3 Jun 2024 17:02:15 +0800
Subject: [PATCH] udisksctl: Guard object lookup

Added extra checks for object validity when looking up physical
device through a drive. Reproducible e.g. by calling 'power-off'
over a LUKS container.
---
 tools/udisksctl.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/tools/udisksctl.c b/tools/udisksctl.c
index 7a5de65..349dca3 100644
--- a/tools/udisksctl.c
+++ b/tools/udisksctl.c
@@ -2003,6 +2003,7 @@ handle_command_smart_simulate (gint        *argc,
     {
       UDisksObject *block_object;
       UDisksDrive *drive;
+
       block_object = lookup_object_by_device (opt_smart_simulate_device);
       if (block_object == NULL)
         {
@@ -2010,7 +2011,19 @@ handle_command_smart_simulate (gint        *argc,
           goto out;
         }
       drive = udisks_client_get_drive_for_block (client, udisks_object_peek_block (block_object));
+      if (drive == NULL)
+        {
+          g_printerr ("Error looking up drive for device %s\n", opt_smart_simulate_device);
+          g_object_unref (block_object);
+          goto out;
+        }
       object = (UDisksObject *) g_dbus_interface_dup_object (G_DBUS_INTERFACE (drive));
+      if (object == NULL)
+        {
+          g_printerr ("Error looking up object for device %s\n", opt_smart_simulate_device);
+          g_object_unref (block_object);
+          goto out;
+        }
       g_object_unref (block_object);
     }
   else
@@ -2244,6 +2257,7 @@ handle_command_power_off (gint        *argc,
     {
       UDisksObject *block_object;
       UDisksDrive *drive;
+
       block_object = lookup_object_by_device (opt_power_off_device);
       if (block_object == NULL)
         {
@@ -2251,7 +2265,19 @@ handle_command_power_off (gint        *argc,
           goto out;
         }
       drive = udisks_client_get_drive_for_block (client, udisks_object_peek_block (block_object));
+      if (drive == NULL)
+        {
+          g_printerr ("Error looking up drive for device %s\n", opt_power_off_device);
+          g_object_unref (block_object);
+          goto out;
+        }
       object = (UDisksObject *) g_dbus_interface_dup_object (G_DBUS_INTERFACE (drive));
+      if (object == NULL)
+        {
+          g_printerr ("Error looking up object for device %s\n", opt_power_off_device);
+          g_object_unref (block_object);
+          goto out;
+        }
       g_object_unref (block_object);
     }
   else
-- 
2.33.0

