5825b3
From eb917d346bc8592924c5f6566b01841176c53c8c Mon Sep 17 00:00:00 2001
5825b3
From: Tomas Bzatek <tbzatek@redhat.com>
5825b3
Date: Mon, 22 Aug 2022 16:27:11 +0200
5825b3
Subject: [PATCH] udiskslinuxblock: Only permit ATA Secure Erase during
5825b3
 Format() on a whole block device
5825b3
5825b3
ATA Secure Erase requested as an option to the Format() method call used
5825b3
to perform the actual erase on a whole drive object it looked up. When
5825b3
Format() was called on a partition, this led to data loss on a whole drive.
5825b3
This commit adds a safeguard to check that the Format() is requested
5825b3
on a whole block device.
5825b3
5825b3
Severity of this issue was slightly lowered by a failure to submit
5825b3
the ATA Secure erase command in case some filesystem was mounted
5825b3
at that point.
5825b3
---
5825b3
 src/udiskslinuxblock.c | 16 ++++++++++++++++
5825b3
 1 file changed, 16 insertions(+)
5825b3
5825b3
diff --git a/src/udiskslinuxblock.c b/src/udiskslinuxblock.c
5825b3
index d1da94edf..db0ed2bf6 100644
5825b3
--- a/src/udiskslinuxblock.c
5825b3
+++ b/src/udiskslinuxblock.c
5825b3
@@ -2354,6 +2354,7 @@ erase_ata_device (UDisksBlock   *block,
5825b3
 {
5825b3
   gboolean ret = FALSE;
5825b3
   UDisksObject *drive_object = NULL;
5825b3
+  UDisksLinuxBlockObject *block_object = NULL;
5825b3
   UDisksDriveAta *ata = NULL;
5825b3
 
5825b3
   drive_object = udisks_daemon_find_object (daemon, udisks_block_get_drive (block));
5825b3
@@ -2369,6 +2370,20 @@ erase_ata_device (UDisksBlock   *block,
5825b3
       goto out;
5825b3
     }
5825b3
 
5825b3
+  /* Reverse check to ensure we're erasing whole block device and not a partition */
5825b3
+  block_object = udisks_linux_drive_object_get_block (UDISKS_LINUX_DRIVE_OBJECT (drive_object), FALSE /* get_hw */);
5825b3
+  if (block_object == NULL)
5825b3
+    {
5825b3
+      g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED, "Couldn't find a block device for the drive to erase");
5825b3
+      goto out;
5825b3
+    }
5825b3
+  if (g_strcmp0 (g_dbus_object_get_object_path (G_DBUS_OBJECT (object)),
5825b3
+                 g_dbus_object_get_object_path (G_DBUS_OBJECT (block_object))) != 0)
5825b3
+    {
5825b3
+      g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_FAILED, "ATA secure erase needs to be performed on a whole block device");
5825b3
+      goto out;
5825b3
+    }
5825b3
+
5825b3
   /* sleep a tiny bit here to avoid the secure erase code racing with
5825b3
    * programs spawned by udev
5825b3
    */
5825b3
@@ -2382,6 +2397,7 @@ erase_ata_device (UDisksBlock   *block,
5825b3
  out:
5825b3
   g_clear_object (&ata;;
5825b3
   g_clear_object (&drive_object);
5825b3
+  g_clear_object (&block_object);
5825b3
   return ret;
5825b3
 }
5825b3