Skip to contentSkip to conversation

cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner

PATCH Superseded Glenn Washburn 2 participants 7 messages Mar 21, 2021 – Mar 24, 2021

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner

Since this has been on the shelf a while, I forgot that a null termination of the UUID field in LUKS 1/2 headers can be enforced as it is required by the spec[1][2]. So there's no need to preserve the last byte of the UUID field, as was done in the first version of the patch. I'll repeat here my comments taking into account this change.

There was also an issue in v1 that prevented it from building, which has been fixed.

--
I have another version of grub_uuidcasecmp which is more efficient by not copying the UUID bytes to another buffer to strip out the dashes. This also implies that it does not require null-terminated strings as inputs because one can use the n parameter to prevent access beyond length of array.

However, I'm not sure we're so memory constrained that the added complexity (its not trivial to verify correctness) is worth it here, so I've chosen to submit this simpler version. If that assumption isn't valid, I can submit the more complex version. This current version uses an extra buffer copy in grub_uuidcasecmp to strip out dashes so that grub_strncasecmp can be called.

Another benefit of this is that the complexity of UUID processing is moved into one place as opposed to each cryptodisk module potentially having that complexity (eg. current code duplication in LUKS1 and LUKS2 modules). --

Glenn

[1] https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/LUKS_docs/on-disk-format.pdf#7
[2] https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf

Glenn Washburn (2):
luks, luks2: Force header.uuid to be NULL terminated
cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner

 grub-core/disk/cryptodisk.c |  4 ++--
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       | 24 +++++++-----------------
 grub-core/disk/luks2.c      | 16 ++++++----------
 include/grub/misc.h         | 27 +++++++++++++++++++++++++++
 5 files changed, 43 insertions(+), 30 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/2 luks, luks2: Force header.uuid to be NULL terminated

According to the LUKS specification the uuid header field is of data type "char[]", which is defined as "a string stored as null terminated sequence of 8-bit characters". So enforce this by adding a null byte as the last byte of the uuid. The LUKS2 specification defers to the LUKS1 specification in this regard.

---
 grub-core/disk/luks.c  | 3 +++
 grub-core/disk/luks2.c | 3 +++
 2 files changed, 6 insertions(+)
grub-core/disk/luks.c +3 -0
index 13103ea6a..9a7323920 100644
@@ -90,6 +90,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
     }
 
+  /* According to the spec the uuid field is NULL terminated, so enforce it. */
+  header.uuid[sizeof(header.uuid)-1] = '\0';
+
   /* Look for LUKS magic sequence.  */
   if (grub_memcmp (header.magic, LUKS_MAGIC, sizeof (header.magic))
       || grub_be_to_cpu16 (header.version) != 1)
grub-core/disk/luks2.c +3 -0
index 125e8609a..a7e614470 100644
@@ -344,6 +344,9 @@ luks2_read_header (grub_disk_t disk, grub_luks2_header_t *outhdr)
       header = &secondary;
   grub_memcpy (outhdr, header, sizeof (*header));
 
+  /* According to the spec the uuid field is NULL terminated, so enforce it. */
+  header->uuid[sizeof(header->uuid)-1] = '\0';
+
   return GRUB_ERR_NONE;
 }
 
Signature · 1 line
2.27.0
#2 lore raw

PATCH v2 2/2 cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner

In reply to Glenn Washburn

A user can now specify UUID strings with dashes, instead of having to remove dashes. This is backwards-compatability preserving and also fixes a source of user confusion over the inconsistency with how UUIDs are specified between file system UUIDs and cryptomount UUIDs. Since cryptsetup, the reference implementation for LUKS, displays and generates UUIDs with dashes there has been additional confusion when using the UUID strings from cryptsetup as exact input into GRUB does not find the expected cryptodisk.

A new function grub_uuidcasecmp is added that is general enough to be used other places where UUIDs are being compared.

---
 grub-core/disk/cryptodisk.c |  4 ++--
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       | 21 ++++-----------------
 grub-core/disk/luks2.c      | 13 +++----------
 include/grub/misc.h         | 27 +++++++++++++++++++++++++++
 5 files changed, 37 insertions(+), 30 deletions(-)
grub-core/disk/cryptodisk.c +2 -2
index 90f82b2d3..f1a30d80c 100644
@@ -678,7 +678,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
   if (grub_memcmp (name, "cryptouuid/", sizeof ("cryptouuid/") - 1) == 0)
     {
       for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
-	if (grub_strcasecmp (name + sizeof ("cryptouuid/") - 1, dev->uuid) == 0)
+	if (grub_uuidcasecmp (name + sizeof ("cryptouuid/") - 1, dev->uuid, sizeof (dev->uuid)) == 0)
 	  break;
     }
   else
@@ -908,7 +908,7 @@ grub_cryptodisk_get_by_uuid (const char *uuid)
 {
   grub_cryptodisk_t dev;
   for (dev = cryptodisk_list; dev != NULL; dev = dev->next)
-    if (grub_strcasecmp (dev->uuid, uuid) == 0)
+    if (grub_uuidcasecmp (dev->uuid, uuid, sizeof (dev->uuid)) == 0)
       return dev;
   return NULL;
 }
grub-core/disk/geli.c +1 -1
index 2f34a35e6..f7fac73df 100644
@@ -304,7 +304,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
     }
 
-  if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
+  if (check_uuid && grub_uuidcasecmp (check_uuid, uuid, sizeof (uuid)) != 0)
     {
       grub_dprintf ("geli", "%s != %s\n", uuid, check_uuid);
       return NULL;
grub-core/disk/luks.c +4 -17
index 9a7323920..eed0345b4 100644
@@ -69,10 +69,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 		   int check_boot)
 {
   grub_cryptodisk_t newdev;
-  const char *iptr;
   struct grub_luks_phdr header;
-  char *optr;
-  char uuid[sizeof (header.uuid) + 1];
   char ciphername[sizeof (header.cipherName) + 1];
   char ciphermode[sizeof (header.cipherMode) + 1];
   char hashspec[sizeof (header.hashSpec) + 1];
@@ -98,19 +95,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       || grub_be_to_cpu16 (header.version) != 1)
     return NULL;
 
-  grub_memset (uuid, 0, sizeof (uuid));
-  optr = uuid;
-  for (iptr = header.uuid; iptr < &header.uuid[ARRAY_SIZE (header.uuid)];
-       iptr++)
+  if (check_uuid && grub_uuidcasecmp (check_uuid, header.uuid, sizeof (header.uuid)) != 0)
     {
-      if (*iptr != '-')
-	*optr++ = *iptr;
-    }
-  *optr = 0;
-
-  if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
-    {
-      grub_dprintf ("luks", "%s != %s\n", uuid, check_uuid);
+      grub_dprintf ("luks", "%s != %s\n", header.uuid, check_uuid);
       return NULL;
     }
 
@@ -129,7 +116,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->source_disk = NULL;
   newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
   newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
-  grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
+  grub_memcpy (newdev->uuid, header.uuid, sizeof (header.uuid));
   newdev->modname = "luks";
 
   /* Configure the hash used for the AF splitter and HMAC.  */
@@ -149,7 +136,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
     }
 
-  COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (uuid));
+  COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (header.uuid));
   return newdev;
 }
 
grub-core/disk/luks2.c +3 -10
index a7e614470..e723977ee 100644
@@ -355,8 +355,6 @@ luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
 {
   grub_cryptodisk_t cryptodisk;
   grub_luks2_header_t header;
-  char uuid[sizeof (header.uuid) + 1];
-  grub_size_t i, j;
 
   if (check_boot)
     return NULL;
@@ -367,20 +365,15 @@ luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
       return NULL;
     }
 
-  for (i = 0, j = 0; i < sizeof (header.uuid); i++)
-    if (header.uuid[i] != '-')
-      uuid[j++] = header.uuid[i];
-  uuid[j] = '\0';
-
-  if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
+  if (check_uuid && grub_uuidcasecmp (check_uuid, header.uuid, sizeof (header.uuid)) != 0)
     return NULL;
 
   cryptodisk = grub_zalloc (sizeof (*cryptodisk));
   if (!cryptodisk)
     return NULL;
 
-  COMPILE_TIME_ASSERT (sizeof (cryptodisk->uuid) >= sizeof (uuid));
-  grub_memcpy (cryptodisk->uuid, uuid, sizeof (uuid));
+  COMPILE_TIME_ASSERT (sizeof (cryptodisk->uuid) >= sizeof (header.uuid));
+  grub_memcpy (cryptodisk->uuid, header.uuid, sizeof (header.uuid));
 
   cryptodisk->modname = "luks2";
   return cryptodisk;
include/grub/misc.h +27 -0
index 7d2b55196..f0141a6cf 100644
@@ -27,6 +27,8 @@
 #include <grub/i18n.h>
 #include <grub/compiler.h>
 
+#define GRUB_MAX_UUID_LENGTH 71
+
 #define ALIGN_UP(addr, align) \
 	(((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1))
 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
@@ -243,6 +245,31 @@ grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
     - (int) grub_tolower ((grub_uint8_t) *s2);
 }
 
+/*
+ * Do a case insensitive compare of two UUID strings by first stripping out
+ * all dashes from both UUID arguments and then doing a regular
+ * grub_strncasecmp.
+ */
+static inline int
+grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n)
+{
+  char *ptr;
+  char ruuid1[GRUB_MAX_UUID_LENGTH + 1];
+  char ruuid2[GRUB_MAX_UUID_LENGTH + 1];
+
+  for (ptr = ruuid1; *uuid1; uuid1++)
+    if (*uuid1 != '-')
+      *(ptr++) = *uuid1;
+  *ptr = '\0';
+
+  for (ptr = ruuid2; *uuid2; uuid2++)
+    if (*uuid2 != '-')
+      *(ptr++) = *uuid2;
+  *ptr = '\0';
+
+  return grub_strncasecmp (ruuid1, ruuid2, n);
+}
+
 /*
  * Note that these differ from the C standard's definitions of strtol,
  * strtoul(), and strtoull() by the addition of two const qualifiers on the end
Signature · 1 line
2.27.0
#3 lore raw
In reply to Glenn Washburn
Quoted 2 lines · * On 3/21/21 9:06 PM, Glenn Washburn wrote:

I have another version of grub_uuidcasecmp which is more efficient by not copying the UUID bytes to another buffer to strip out the dashes. [...]

At first, I didn't understand why you would need to copy the original UUIDs in the first place, but then ...

Quoted 3 lines

However, I'm not sure we're so memory constrained that the added complexity (its not trivial to verify correctness) is worth it here, so I've chosen to submit this simpler version.

I don't think that memory is so scarce that allocating two tiny string buffers on the stack would be problematic, but ...

Quoted 3 lines

If that assumption isn't valid, I can submit
the more complex version. This current version uses an extra buffer copy in grub_uuidcasecmp to strip out dashes so that grub_strncasecmp can be called.

Do you do all of that just to be able to use grub_strncasecmp()?

Wouldn't it make more sense to just duplicate a bit of code in this case and do away with all additional buffers and complexities?

Untested, written from the top of my head without any syntax check or the like:

static inline int
grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n) {
const char *s1 = uuid1,

             *s2 = uuid2;

/*
* Assume that n is the number of valid non-dash characters in a UUID
* and that both UUIDs are sized the same.
*/
for (grub_size_t i = 0; i < n; ++i)

  {
    /* Skip forward to non-dash on both UUIDs. */
    while ('-' == *s1)
    {
      ++s1;
    }

    while ('-' == *s2)
    {
      ++s2;
    }

    if (grub_tolower (*s1) != grub_tolower (*s2))
    {
      break;
    }

    ++s1;
    ++s2;

}

return (int) grub_tolower ((grub_uint8_t) *s1)
- (int) grub_tolower ((grub_uint8_t) *s2);
}

That is, admittedly, copying the actual part of grub_strncasecmp(), but we're doing away with all copies and additional buffers and it's different enough to warrant being a different function, I figure.

Mihai

#4 lore raw
Quoted 64 lines · On Mon, 22 Mar 2021 23:53:16 +0100 Mihai Moldovan <[email protected]> wrote:
Quoted 3 lines · * On 3/21/21 9:06 PM, Glenn Washburn wrote:

I have another version of grub_uuidcasecmp which is more efficient by not copying the UUID bytes to another buffer to strip out the dashes. [...]

At first, I didn't understand why you would need to copy the original UUIDs in the first place, but then ...

Quoted 3 lines

However, I'm not sure we're so memory constrained that the added complexity (its not trivial to verify correctness) is worth it here, so I've chosen to submit this simpler version.

I don't think that memory is so scarce that allocating two tiny string buffers on the stack would be problematic, but ...

Quoted 4 lines

If that assumption isn't valid, I can submit
the more complex version. This current version uses an extra buffer copy in grub_uuidcasecmp to strip out dashes so that grub_strncasecmp can be called.

Do you do all of that just to be able to use grub_strncasecmp()?

Wouldn't it make more sense to just duplicate a bit of code in this case and do away with all additional buffers and complexities?

Untested, written from the top of my head without any syntax check or the like:

static inline int
grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n) {
const char *s1 = uuid1,

             *s2 = uuid2;

/*
* Assume that n is the number of valid non-dash characters in a
UUID
* and that both UUIDs are sized the same.
*/
for (grub_size_t i = 0; i < n; ++i)

  {
    /* Skip forward to non-dash on both UUIDs. */
    while ('-' == *s1)
    {
      ++s1;
    }

    while ('-' == *s2)
    {
      ++s2;
    }

    if (grub_tolower (*s1) != grub_tolower (*s2))
    {
      break;
    }

    ++s1;
    ++s2;
Quoted 5 lines

}

return (int) grub_tolower ((grub_uint8_t) *s1)
- (int) grub_tolower ((grub_uint8_t) *s2);
}

Aside from the fact that this doesn't handle negative values of n, this would be fine.

Quoted 3 lines

That is, admittedly, copying the actual part of grub_strncasecmp(), but we're doing away with all copies and additional buffers and it's different enough to warrant being a different function, I figure.

I don't think its as big a deal as it sounds. There are just two extra arrays on the stack, so not memory management issues. Partly I chose to implement it the way I did by using grub_strncasecmp because its pretty trivial to see just by looking at the patches that I'm not changing any core logic. But I'm not wedded to my implementation, I just want this functionality. If I get time, I'll make a new version taking this in to account.

Glenn

#5 lore raw
Quoted 2 lines · * On 3/24/21 5:24 AM, Glenn Washburn wrote:

Aside from the fact that this doesn't handle negative values of n, this would be fine.

Neither do the original version or grub_strncasecmp()... and if you *can* get n to be negative, I'd be highly impressed!

Jokes aside, it's unsigned, so there's no way for it to become negative. It should probably handle 0 == n in a different way like grub_strncasecmp() does, but that's easily added.

Quoted 2 lines

I don't think its as big a deal as it sounds. There are just two extra arrays on the stack, so not memory management issues.

Agree.

Quoted 3 lines

Partly I chose to implement it the way I did by using grub_strncasecmp because its pretty trivial to see just by looking at the patches that I'm not changing any core logic.

Yeah, that's pretty obvious by it using grub_strncasecmp() in the end, but my arguments would be that we don't need two passes over the input strings instead of just one and could avoid the copies entirely. (Yes, these arguments are clearly over-optimizations.)

Also, semantically, UUID parsing *is* different from simple string parsing, which is also better reflected there.

Quoted 3 lines

But I'm not wedded to my implementation, I just want this functionality. If I get time, I'll make a new version taking this in to account.

I'm not in any way authoritative, just adding my thoughts. :)

I don't really care which version gets in, but I figured that since it might be a while until this lands in grub proper, I could still offer some insights.

Mihai

#6 lore raw
Quoted 10 lines · On Wed, 24 Mar 2021 06:43:34 +0100 Mihai Moldovan <[email protected]> wrote:
Quoted 2 lines · * On 3/24/21 5:24 AM, Glenn Washburn wrote:

Aside from the fact that this doesn't handle negative values of n, this would be fine.

Neither do the original version or grub_strncasecmp()... and if you *can* get n to be negative, I'd be highly impressed!

Jokes aside, it's unsigned, so there's no way for it to become negative. It should probably handle 0 == n in a different way like grub_strncasecmp() does, but that's easily added.

Hah, calling with n < 0 has the same effect whether n is signed or not, so I hadn't noticed it.

Quoted 10 lines
Quoted 2 lines

I don't think its as big a deal as it sounds. There are just two extra arrays on the stack, so not memory management issues.

Agree.

Quoted 3 lines

Partly I chose to implement it the way I did by using grub_strncasecmp because its pretty trivial to see just by looking at the patches that I'm not changing any core logic.

Quoted 7 lines

Yeah, that's pretty obvious by it using grub_strncasecmp() in the end, but my arguments would be that we don't need two passes over the input strings instead of just one and could avoid the copies entirely. (Yes, these arguments are clearly over-optimizations.)

Also, semantically, UUID parsing *is* different from simple string parsing, which is also better reflected there.

I agree with this wrt to UUID parsing, but not with regard to UUID comparison (at least as we use UUID which has no semantic awareness). It really is just a case insensitive string compare with a limited character set.

Quoted 9 lines
Quoted 3 lines

But I'm not wedded to my implementation, I just want this functionality. If I get time, I'll make a new version taking this in to account.

I'm not in any way authoritative, just adding my thoughts. :)

I don't really care which version gets in, but I figured that since it might be a while until this lands in grub proper, I could still offer some insights.

Yep, appreciate the feedback. And I think your suggestion makes sense.

Glenn