Skip to content

Commit

Permalink
Use label for DM device name when unlocking encrypted devices
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechtrefny committed Oct 23, 2024
1 parent 261c0de commit ca58adc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
27 changes: 26 additions & 1 deletion src/tests/dbus-tests/test_70_encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,16 @@ def setUpClass(cls):

super(UdisksEncryptedTestLUKS2, cls).setUpClass()

def _create_luks(self, device, passphrase, binary=False):
def _create_luks(self, device, passphrase, binary=False, label=None):
options = dbus.Dictionary(signature='sv')
if binary:
options['encrypt.passphrase'] = self.bytes_to_ay(passphrase)
options['encrypt.type'] = 'luks2'
else:
options['encrypt.passphrase'] = passphrase
options['encrypt.type'] = 'luks2'
if label:
options['encrypt.label'] = label
device.Format('ext4', options,
dbus_interface=self.iface_prefix + '.Block')

Expand Down Expand Up @@ -824,6 +826,29 @@ def test_create_pbkdf_extra(self):
self.fail("Failed to get pbkdf information from:\n%s" % out)
self.assertEqual(m.group(1), "10000")

def test_create_open_label(self):
disk = self.vdevs[0]
device = self.get_device(disk)
self._create_luks(device, self.PASSPHRASE, label="TESTLUKS")

self.addCleanup(self._remove_luks, device)
self.udev_settle()

dbus_label = self.get_property(device, '.Block', 'IdLabel')
dbus_label.assertEqual("TESTLUKS")
self.assertTrue(os.path.exists('/dev/mapper/TESTLUKS'))

device.Lock(self.no_options, dbus_interface=self.iface_prefix + '.Encrypted')

crypt_path = device.Unlock(self.PASSPHRASE, self.no_options,
dbus_interface=self.iface_prefix + '.Encrypted')
self.assertIsNotNone(crypt_path)
crypt_dev = self.bus.get_object(self.iface_prefix, crypt_path)
self.assertIsNotNone(crypt_dev)
pref_device = self.get_property(crypt_dev, ".Block", "PreferredDevice")
pref_device.assertEqual(self.str_to_ay('/dev/mapper/TESTLUKS'))
self.assertTrue(os.path.exists('/dev/mapper/TESTLUKS'))


class UdisksEncryptedTestBITLK(udiskstestcase.UdisksTestCase):

Expand Down
28 changes: 18 additions & 10 deletions src/udiskslinuxencrypted.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ handle_unlock (UDisksEncrypted *encrypted,
gboolean handle_as_tcrypt;
void *open_func;
const gchar *uuid = NULL;
const gchar *label = NULL;

object = udisks_daemon_util_dup_object (encrypted, &error);
if (object == NULL)
Expand Down Expand Up @@ -524,19 +525,26 @@ handle_unlock (UDisksEncrypted *encrypted,
if (is_in_crypttab && crypttab_name != NULL)
name = g_strdup (crypttab_name);
else {
if (is_luks)
name = g_strdup_printf ("luks-%s", udisks_block_get_id_uuid (block));
else if (is_bitlk)
label = udisks_block_get_id_label (block);
if (label)
name = g_strdup (label);
else
{
uuid = udisks_block_get_id_uuid (block);
if (uuid && g_strcmp0 (uuid, "") != 0)
name = g_strdup_printf ("bitlk-%s", uuid);
if (is_luks)
name = g_strdup_printf ("luks-%s", udisks_block_get_id_uuid (block));
else if (is_bitlk)
{
uuid = udisks_block_get_id_uuid (block);
if (uuid && g_strcmp0 (uuid, "") != 0)
name = g_strdup_printf ("bitlk-%s", uuid);
else
name = g_strdup_printf ("bitlk-%" G_GUINT64_FORMAT, udisks_block_get_device_number (block));
}
else
name = g_strdup_printf ("bitlk-%" G_GUINT64_FORMAT, udisks_block_get_device_number (block));
/* TCRYPT devices don't have a UUID, so we use the device number instead */
name = g_strdup_printf ("tcrypt-%" G_GUINT64_FORMAT, udisks_block_get_device_number (block));
}
else
/* TCRYPT devices don't have a UUID, so we use the device number instead */
name = g_strdup_printf ("tcrypt-%" G_GUINT64_FORMAT, udisks_block_get_device_number (block));

}

/* save old encryption type to be able to restore it */
Expand Down

0 comments on commit ca58adc

Please sign in to comment.