Skip to contentSkip to conversation

Cryptodisk fixes for v2.06 redux

PATCH Superseded Glenn Washburn 3 participants 289 messages Oct 3, 2020 – Dec 15, 2020 Page 3 of 3

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
Reviewed-by on v8 9/18
#257 lore raw

Re: [PATCH v8 09/18] luks2: Add string "index" to user strings using a json index.

In reply to Glenn Washburn
Quoted 5 lines · Glenn Washburn

This allows error messages to be more easily distinguishable between indexes and slot keys. The former include the string "index" in the error/debug string, and the later are surrounded in quotes.

Daniel

Reviewed-by on v8 9/18
#258 lore raw

Re: [PATCH v8 09/18] luks2: Add string "index" to user strings using a json index.

In reply to Glenn Washburn
Quoted 5 lines · Glenn Washburn

This allows error messages to be more easily distinguishable between indexes and slot keys. The former include the string "index" in the error/debug string, and the later are surrounded in quotes.

Nit: the commit subject still has a trailing dot. I guess Daniel can fix it when applying, though. So:

Quoted 38 lines
---
 grub-core/disk/luks2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
index 7e0419a82..ebf03e77b 100644
@@ -272,7 +272,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&k->idx, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_json_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_json_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -284,7 +284,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->idx, &digest, NULL) ||
 	  grub_json_getchild (&digest, &digest, 0) ||
 	  luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, json_idx);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, json_idx);
 
       if ((d->keyslots & (1 << k->idx)))
 	break;
@@ -302,7 +302,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
 	  luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, json_idx);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, json_idx);
 
       if ((d->segments & (1 << s->idx)))
 	break;
Signature · 1 line
2.27.0
#259 lore raw

PATCH v8 10/18 cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals

In reply to Glenn Washburn

The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type.

---
 grub-core/disk/cryptodisk.c | 7 ++++---
 include/grub/types.h        | 2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)
grub-core/disk/cryptodisk.c +4 -3
index 473c93976..0e955a020 100644
@@ -281,20 +281,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
+	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
 	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
 	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
+	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
+					       - dev->log_sector_size));
 	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
 	  {
 	    grub_uint64_t num = (sector << dev->benbi_log) + 1;
-	    iv[sz - 2] = grub_cpu_to_be32 (num >> 32);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
 	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
 	  }
 	  break;
include/grub/types.h +2 -0
index f22055f98..9989e3a16 100644
@@ -80,6 +80,8 @@
 # define GRUB_CHAR_BIT	__CHAR_BIT__
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
 /* Define various wide integers.  */
 typedef signed char		grub_int8_t;
 typedef short			grub_int16_t;
Signature · 1 line
2.27.0
Reviewed-by on v8 10/18
#261 lore raw

Re: [PATCH v8 10/18] cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type.

Quoted 50 lines
---
 grub-core/disk/cryptodisk.c | 7 ++++---
 include/grub/types.h        | 2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)
grub-core/disk/cryptodisk.c +4 -3
index 473c93976..0e955a020 100644
@@ -281,20 +281,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
+	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
 	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
 	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
+	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
+					       - dev->log_sector_size));
 	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
 	  {
 	    grub_uint64_t num = (sector << dev->benbi_log) + 1;
-	    iv[sz - 2] = grub_cpu_to_be32 (num >> 32);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
 	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
 	  }
 	  break;
include/grub/types.h +2 -0
index f22055f98..9989e3a16 100644
@@ -80,6 +80,8 @@
 # define GRUB_CHAR_BIT	__CHAR_BIT__
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
 /* Define various wide integers.  */
 typedef signed char		grub_int8_t;
 typedef short			grub_int16_t;
Signature · 1 line
2.27.0
#262 lore raw

PATCH v8 11/18 cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

In reply to Glenn Washburn

Add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.

---
 grub-core/disk/cryptodisk.c | 8 ++++----
 include/grub/types.h        | 7 +++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
grub-core/disk/cryptodisk.c +4 -4
index 0e955a020..5aa0c4720 100644
@@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
 	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
 	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
 					       - dev->log_sector_size));
 	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
-				    & 0xFFFFFFFF);
+				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
 	  {
 	    grub_uint64_t num = (sector << dev->benbi_log) + 1;
 	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX (iv[0]));
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_ESSIV:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
 	  err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +7 -0
index 9989e3a16..b36b26a79 100644
@@ -161,6 +161,13 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+/*
+ * Cast to unsigned long long so that the "return value" is always a consistent
+ * type and to ensure an unsigned value regardless of type parameter.
+ */
+#define GRUB_TYPE_U_MAX(type) ((unsigned long long)((typeof (type))(~0)))
+#define GRUB_TYPE_U_MIN(type) 0ULL
+
 typedef grub_uint64_t grub_properly_aligned_t;
 
 #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]
Signature · 1 line
2.27.0
Reviewed-by on v8 11/18
#264 lore raw

Re: [PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

Add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.

Quoted 58 lines
---
 grub-core/disk/cryptodisk.c | 8 ++++----
 include/grub/types.h        | 7 +++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
grub-core/disk/cryptodisk.c +4 -4
index 0e955a020..5aa0c4720 100644
@@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
 	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
 	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
 					       - dev->log_sector_size));
 	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
-				    & 0xFFFFFFFF);
+				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
 	  {
 	    grub_uint64_t num = (sector << dev->benbi_log) + 1;
 	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX (iv[0]));
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_ESSIV:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
 	  err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +7 -0
index 9989e3a16..b36b26a79 100644
@@ -161,6 +161,13 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+/*
+ * Cast to unsigned long long so that the "return value" is always a consistent
+ * type and to ensure an unsigned value regardless of type parameter.
+ */
+#define GRUB_TYPE_U_MAX(type) ((unsigned long long)((typeof (type))(~0)))
+#define GRUB_TYPE_U_MIN(type) 0ULL
+
 typedef grub_uint64_t grub_properly_aligned_t;
 
 #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]
Signature · 1 line
2.27.0
#265 lore raw

PATCH v8 12/18 luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

In reply to Glenn Washburn

We need to convert the sectors from the size of the underlying device to the cryptodisk sector size; segment.size is in bytes which need to be converted to cryptodisk sectors as well.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index ebf03e77b..429a4cfa1 100644
@@ -429,7 +429,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -615,9 +615,10 @@ luks2_recover_key (grub_disk_t source,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
+			       - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
Reviewed-by on v8 12/18
#266 lore raw

Re: [PATCH v8 12/18] luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

Quoted 8 lines · Glenn Washburn

We need to convert the sectors from the size of the underlying device to the cryptodisk sector size; segment.size is in bytes which need to be converted to cryptodisk sectors as well.

Also, removed an empty statement.

Quoted 33 lines
---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index ebf03e77b..429a4cfa1 100644
@@ -429,7 +429,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -615,9 +615,10 @@ luks2_recover_key (grub_disk_t source,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
+			       - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#267 lore raw

PATCH v8 13/18 cryptodisk: Properly handle non-512 byte sized sectors

In reply to Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 55 +++++++++++++++++++++++--------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 50 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +34 -21
index 5aa0c4720..b62835acc 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,15 +282,27 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t iv64;
+
+	    iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    grub_set_unaligned64 (iv, iv64);
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
+	  /* The IV is the 64 bit byte offset of the sector. */
 	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
-					       - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+					       - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -312,10 +325,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -323,10 +336,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -338,7 +351,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 	    
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -369,11 +382,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +395,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -400,9 +413,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -767,7 +780,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -808,7 +821,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 5fc79d880..13103ea6a 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  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));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  GRUB_LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 429a4cfa1..9abcb1c94 100644
@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      GRUB_LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index c9bf7597a..938912cf3 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define GRUB_LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -144,7 +150,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
Reviewed-by on v8 13/18
#268 lore raw

Re: [PATCH v8 13/18] cryptodisk: Properly handle non-512 byte sized sectors

Quoted 17 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

Quoted 8 lines

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

Quoted 234 lines
---
 grub-core/disk/cryptodisk.c | 55 +++++++++++++++++++++++--------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 50 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +34 -21
index 5aa0c4720..b62835acc 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,15 +282,27 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t iv64;
+
+	    iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    grub_set_unaligned64 (iv, iv64);
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
+	  /* The IV is the 64 bit byte offset of the sector. */
 	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
-					       - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+					       - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -312,10 +325,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -323,10 +336,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -338,7 +351,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -369,11 +382,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +395,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -400,9 +413,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -767,7 +780,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -808,7 +821,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 5fc79d880..13103ea6a 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  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));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  GRUB_LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 429a4cfa1..9abcb1c94 100644
@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      GRUB_LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index c9bf7597a..938912cf3 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define GRUB_LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -144,7 +150,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#269 lore raw

PATCH v8 14/18 luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn

First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.

---
 grub-core/disk/luks2.c | 84 ++++++++++++++++++++++++++++++++++++++++--
 include/grub/disk.h    | 17 +++++++++
 2 files changed, 98 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +81 -3
index 9abcb1c94..8cb11e899 100644
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      /* FIXME: Allow use of source disk, and maybe cause errors in read. */
+      grub_dprintf ("luks2", "Source disk %s has an unknown size, "
+			     "conservatively returning error\n", source->name);
+      ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2 source device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (json_idx = 0; json_idx < size; json_idx++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);
       if (ret)
 	goto err;
+      if (grub_errno != GRUB_ERR_NONE)
+	  grub_dprintf ("luks2", "Ignoring unhandled error %d from luks2_get_keyslot\n", grub_errno);
 
       if (keyslot.priority == 0)
 	{
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source,
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      /* Set to the source disk size, which is the maximum we allow. */
+      max_crypt_sectors = grub_disk_convert_sector(source,
+						   source->total_sectors,
+						   crypt->log_sector_size);
+
+      if (max_crypt_sectors < crypt->offset_sectors)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has offset"
+				 " %"PRIuGRUB_UINT64_T" which is greater than"
+				 " source disk size %"PRIuGRUB_UINT64_T","
+				 " skipping\n",
+				 segment.idx, crypt->offset_sectors,
+				 max_crypt_sectors);
+	  continue;
+	}
+
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	{
+	  grub_errno = GRUB_ERR_NONE;
+	  /* Convert segment.size to sectors, rounding up to nearest sector */
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	  crypt->total_sectors = ALIGN_UP (crypt->total_sectors,
+					   1 << crypt->log_sector_size);
+	  crypt->total_sectors >>= crypt->log_sector_size;
+
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " \"%s\" is not a parsable number\n",
+				     segment.idx, segment.size);
+	      continue;
+	    }
+	  else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /*
+	       * There was an overflow in parsing segment.size, so disk must
+	       * be very large or the string is incorrect.
+	       */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " %s overflowed 64-bit unsigned integer,"
+				     " the end of the crypto device will be"
+				     " inaccessible\n",
+				     segment.idx, segment.size);
+	      if (crypt->total_sectors > max_crypt_sectors)
+		crypt->total_sectors = max_crypt_sectors;
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has zero"
+				 " sectors, skipping\n",
+				 segment.idx);
+	  continue;
+	}
+      else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has last"
+				 " data position greater than source disk size,"
+				 " the end of the crypto device will be"
+				 " inaccessible\n",
+				 segment.idx);
+	  /* Allow decryption up to the end of the source disk. */
+	  crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/disk.h +17 -0
index 0fb727d3d..f9227f285 100644
@@ -27,6 +27,8 @@
 #include <grub/device.h>
 /* For NULL.  */
 #include <grub/mm.h>
+/* For ALIGN_UP.  */
+#include <grub/misc.h>
 
 /* These are used to set a device id. When you add a new disk device,
    you must define a new id for it here.  */
@@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 /* Return value of grub_disk_native_sectors() in case disk size is unknown. */
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL
 
+/* Convert sector number from disk sized sectors to a log-size sized sector. */
+static inline grub_disk_addr_t
+grub_disk_convert_sector (grub_disk_t disk,
+			  grub_disk_addr_t sector,
+			  grub_size_t log_sector_size)
+{
+  if (disk->log_sector_size < log_sector_size)
+    {
+      sector = ALIGN_UP (sector, 1 << (log_sector_size / disk->log_sector_size));
+      return sector >> (log_sector_size - disk->log_sector_size);
+    }
+  else
+    return sector << (disk->log_sector_size - log_sector_size);
+}
+
 /* Convert to GRUB native disk sized sector from disk sized sector. */
 static inline grub_disk_addr_t
 grub_disk_from_native_sector (grub_disk_t disk, grub_disk_addr_t sector)
Signature · 1 line
2.27.0
#270 lore raw

Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk

Quoted 100 lines · Glenn Washburn

First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.

---
 grub-core/disk/luks2.c | 84 ++++++++++++++++++++++++++++++++++++++++--
 include/grub/disk.h    | 17 +++++++++
 2 files changed, 98 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +50 -3
index 9abcb1c94..8cb11e899 100644
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      /* FIXME: Allow use of source disk, and maybe cause errors in read. */
+      grub_dprintf ("luks2", "Source disk %s has an unknown size, "
+			     "conservatively returning error\n", source->name);
+      ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2 source device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (json_idx = 0; json_idx < size; json_idx++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);
       if (ret)
 	goto err;
+      if (grub_errno != GRUB_ERR_NONE)
+	  grub_dprintf ("luks2", "Ignoring unhandled error %d from luks2_get_keyslot\n", grub_errno);
 
       if (keyslot.priority == 0)
 	{
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source,
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      /* Set to the source disk size, which is the maximum we allow. */
+      max_crypt_sectors = grub_disk_convert_sector(source,
+						   source->total_sectors,
+						   crypt->log_sector_size);
+
+      if (max_crypt_sectors < crypt->offset_sectors)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has offset"
+				 " %"PRIuGRUB_UINT64_T" which is greater than"
+				 " source disk size %"PRIuGRUB_UINT64_T","
+				 " skipping\n",
+				 segment.idx, crypt->offset_sectors,
+				 max_crypt_sectors);
+	  continue;
+	}
+
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	{
+	  grub_errno = GRUB_ERR_NONE;
+	  /* Convert segment.size to sectors, rounding up to nearest sector */
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	  crypt->total_sectors = ALIGN_UP (crypt->total_sectors,
+					   1 << crypt->log_sector_size);
+	  crypt->total_sectors >>= crypt->log_sector_size;
+
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " \"%s\" is not a parsable number\n",
+				     segment.idx, segment.size);
+	      continue;
+	    }
+	  else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683

Quoted 10 lines
grub-core/disk/luks2.c +10 -0
+	      /*
+	       * There was an overflow in parsing segment.size, so disk must
+	       * be very large or the string is incorrect.
+	       */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " %s overflowed 64-bit unsigned integer,"
+				     " the end of the crypto device will be"
+				     " inaccessible\n",
+				     segment.idx, segment.size);
+	      if (crypt->total_sectors > max_crypt_sectors)

I think this if is bogus. You should clamp crypt->total_sectors without any checks here.

Quoted 40 lines
grub-core/disk/luks2.c +21 -0
+		crypt->total_sectors = max_crypt_sectors;
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has zero"
+				 " sectors, skipping\n",
+				 segment.idx);
+	  continue;
+	}
+      else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has last"
+				 " data position greater than source disk size,"
+				 " the end of the crypto device will be"
+				 " inaccessible\n",
+				 segment.idx);
+	  /* Allow decryption up to the end of the source disk. */
+	  crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/disk.h +2 -0
index 0fb727d3d..f9227f285 100644
@@ -27,6 +27,8 @@
 #include <grub/device.h>
 /* For NULL.  */
 #include <grub/mm.h>
+/* For ALIGN_UP.  */
+#include <grub/misc.h>
 
 /* These are used to set a device id. When you add a new disk device,
    you must define a new id for it here.  */
@@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 /* Return value of grub_disk_native_sectors() in case disk size is unknown. */
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL

include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 187

Quoted 10 lines
include/grub/disk.h +9 -0
 
+/* Convert sector number from disk sized sectors to a log-size sized sector. */
+static inline grub_disk_addr_t
+grub_disk_convert_sector (grub_disk_t disk,
+			  grub_disk_addr_t sector,
+			  grub_size_t log_sector_size)
+{
+  if (disk->log_sector_size < log_sector_size)
+    {
+      sector = ALIGN_UP (sector, 1 << (log_sector_size / disk->log_sector_size));

s#/#-#?

include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 192

Quoted 5 lines
include/grub/disk.h +5 -0
+      return sector >> (log_sector_size - disk->log_sector_size);
+    }
+  else
+    return sector << (disk->log_sector_size - log_sector_size);
+}

Daniel

#271 lore raw

Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk

Quoted 131 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 130 lines · Glenn Washburn

First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.

---
 grub-core/disk/luks2.c | 84

++++++++++++++++++++++++++++++++++++++++-- include/grub/disk.h |
17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)

grub-core/disk/luks2.c +3 -0
index 9abcb1c94..8cb11e899 100644
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      /* FIXME: Allow use of source disk, and maybe cause errors

in read. */

grub-core/disk/luks2.c +2 -0
+      grub_dprintf ("luks2", "Source disk %s has an unknown size, "
+			     "conservatively returning error\n",

source->name);
+ ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2
source device");

grub-core/disk/luks2.c +6 -0
+      goto err;
+    }
+
   /* Try all keyslot */
   for (json_idx = 0; json_idx < size; json_idx++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,

json_idx); if (ret)

grub-core/disk/luks2.c +2 -0
 	goto err;
+      if (grub_errno != GRUB_ERR_NONE)
+	  grub_dprintf ("luks2", "Ignoring unhandled error %d from

luks2_get_keyslot\n", grub_errno);

       if (keyslot.priority == 0)
grub-core/disk/luks2.c +0 -0
 	{
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source,
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); crypt->log_sector_size = sizeof (unsigned int) * 8
- __builtin_clz ((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */

grub-core/disk/luks2.c +2 -0
+      max_crypt_sectors = grub_disk_convert_sector(source,
+

source->total_sectors,
+
crypt->log_sector_size); +

grub-core/disk/luks2.c +3 -0
+      if (max_crypt_sectors < crypt->offset_sectors)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has offset"

grub-core/disk/luks2.c +1 -0
+				 " %"PRIuGRUB_UINT64_T" which is

greater than"

grub-core/disk/luks2.c +1 -0
+				 " source disk size

%"PRIuGRUB_UINT64_T","

grub-core/disk/luks2.c +2 -0
+				 " skipping\n",
+				 segment.idx,

crypt->offset_sectors,

grub-core/disk/luks2.c +4 -1
+				 max_crypt_sectors);
+	  continue;
+	}
+
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >>

(crypt->log_sector_size - source->log_sector_size))

grub-core/disk/luks2.c +1 -1
-			       - crypt->offset_sectors;
+	crypt->total_sectors = max_crypt_sectors -

crypt->offset_sectors; else

grub-core/disk/luks2.c +0 -1
-	crypt->total_sectors = grub_strtoull (segment.size, NULL,

10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +3 -0
+	{
+	  grub_errno = GRUB_ERR_NONE;
+	  /* Convert segment.size to sectors, rounding up to

nearest sector */

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10);

grub-core/disk/luks2.c +2 -0
+	  crypt->total_sectors = ALIGN_UP (crypt->total_sectors,
+					   1 <<

crypt->log_sector_size);

grub-core/disk/luks2.c +7 -0
+	  crypt->total_sectors >>= crypt->log_sector_size;
+
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " \"%s\" is not a parsable

number\n",

grub-core/disk/luks2.c +7 -0
+				     segment.idx, segment.size);
+	      continue;
+	    }
+	  else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /*
+	       * There was an overflow in parsing segment.size, so

disk must

grub-core/disk/luks2.c +3 -0
+	       * be very large or the string is incorrect.
+	       */
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " %s overflowed 64-bit

unsigned integer,"

Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683

Quoted 5 lines
grub-core/disk/luks2.c +1 -0
+				     " the end of the crypto

device will be"

grub-core/disk/luks2.c +3 -0
+				     " inaccessible\n",
+				     segment.idx, segment.size);
+	      if (crypt->total_sectors > max_crypt_sectors)

I think this if is bogus. You should clamp crypt->total_sectors without any checks here.

Actually, I wouldn't call this a clamp because in the overflow case crypt->total_sectors always equals 0. I just realized this, and its because grub_strtoull will return 2^64-1 thus causing the following ALIGN_UP to overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before the ALIGN_UP code was introduced, which would ALIGN_DOWN.

Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so grub_strtoull will return 2^64-1, this should have us set crypt->total_sectors to 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this case max_crypt_sectors will be 2^(58+9-12) => 2^57 sectors. So here we see that crypt->total_sectors < max_crypt_sectors in the overflow case. If we do as you suggest crypt->total_sectors will be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).

On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.

Quoted 48 lines
Quoted 48 lines
grub-core/disk/luks2.c +7 -0
+		crypt->total_sectors = max_crypt_sectors;
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has zero"

grub-core/disk/luks2.c +5 -0
+				 " sectors, skipping\n",
+				 segment.idx);
+	  continue;
+	}
+      else if (max_crypt_sectors < (crypt->offset_sectors +

crypt->total_sectors))

grub-core/disk/luks2.c +2 -0
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has last"

grub-core/disk/luks2.c +1 -0
+				 " data position greater than

source disk size,"

grub-core/disk/luks2.c +1 -0
+				 " the end of the crypto device

will be"

grub-core/disk/luks2.c +4 -0
+				 " inaccessible\n",
+				 segment.idx);
+	  /* Allow decryption up to the end of the source disk. */
+	  crypt->total_sectors = max_crypt_sectors -

crypt->offset_sectors;

grub-core/disk/luks2.c +1 -0
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt,

&keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); diff --git a/include/grub/disk.h b/include/grub/disk.h index 0fb727d3d..f9227f285 100644

include/grub/disk.h +2 -0
@@ -27,6 +27,8 @@
 #include <grub/device.h>
 /* For NULL.  */
 #include <grub/mm.h>
+/* For ALIGN_UP.  */
+#include <grub/misc.h>
 
 /* These are used to set a device id. When you add a new disk

device, you must define a new id for it here. */

include/grub/disk.h +0 -0
@@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist
 *grub_disk_memberlist_t; /* Return value of

grub_disk_native_sectors() in case disk size is unknown. */ #define
GRUB_DISK_SIZE_UNKNOWN 0xffffffffffffffffULL

Quoted 12 lines
Quoted 10 lines

+/* Convert sector number from disk sized sectors to a log-size sized sector. */ +static inline grub_disk_addr_t

include/grub/disk.h +7 -0
+grub_disk_convert_sector (grub_disk_t disk,
+			  grub_disk_addr_t sector,
+			  grub_size_t log_sector_size)
+{
+  if (disk->log_sector_size < log_sector_size)
+    {
+      sector = ALIGN_UP (sector, 1 << (log_sector_size /

disk->log_sector_size));

s#/#-#?

Yep, looks like this passed my tests because an overflow is caused and crypt->total_sectors is set to a very large value, which doesn't affect the tests. I'll fix it.

Glenn

Quoted 7 lines

include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 192

Quoted 5 lines
include/grub/disk.h +5 -0
+      return sector >> (log_sector_size - disk->log_sector_size);
+    }
+  else
+    return sector << (disk->log_sector_size - log_sector_size);
+}

Daniel

#272 lore raw

Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk

Quoted 143 lines · Glenn Washburn
Quoted 131 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 130 lines · Glenn Washburn

First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.

---
 grub-core/disk/luks2.c | 84

++++++++++++++++++++++++++++++++++++++++-- include/grub/disk.h |
17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)

grub-core/disk/luks2.c +3 -0
index 9abcb1c94..8cb11e899 100644
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      /* FIXME: Allow use of source disk, and maybe cause errors

in read. */

grub-core/disk/luks2.c +2 -0
+      grub_dprintf ("luks2", "Source disk %s has an unknown size, "
+			     "conservatively returning error\n",

source->name);
+ ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2
source device");

grub-core/disk/luks2.c +6 -0
+      goto err;
+    }
+
   /* Try all keyslot */
   for (json_idx = 0; json_idx < size; json_idx++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,

json_idx); if (ret)

grub-core/disk/luks2.c +2 -0
 	goto err;
+      if (grub_errno != GRUB_ERR_NONE)
+	  grub_dprintf ("luks2", "Ignoring unhandled error %d from

luks2_get_keyslot\n", grub_errno);

       if (keyslot.priority == 0)
grub-core/disk/luks2.c +0 -0
 	{
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source,
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); crypt->log_sector_size = sizeof (unsigned int) * 8
- __builtin_clz ((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */

grub-core/disk/luks2.c +2 -0
+      max_crypt_sectors = grub_disk_convert_sector(source,
+

source->total_sectors,
+
crypt->log_sector_size); +

grub-core/disk/luks2.c +3 -0
+      if (max_crypt_sectors < crypt->offset_sectors)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has offset"

grub-core/disk/luks2.c +1 -0
+				 " %"PRIuGRUB_UINT64_T" which is

greater than"

grub-core/disk/luks2.c +1 -0
+				 " source disk size

%"PRIuGRUB_UINT64_T","

grub-core/disk/luks2.c +2 -0
+				 " skipping\n",
+				 segment.idx,

crypt->offset_sectors,

grub-core/disk/luks2.c +4 -1
+				 max_crypt_sectors);
+	  continue;
+	}
+
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >>

(crypt->log_sector_size - source->log_sector_size))

grub-core/disk/luks2.c +1 -1
-			       - crypt->offset_sectors;
+	crypt->total_sectors = max_crypt_sectors -

crypt->offset_sectors; else

grub-core/disk/luks2.c +0 -1
-	crypt->total_sectors = grub_strtoull (segment.size, NULL,

10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +3 -0
+	{
+	  grub_errno = GRUB_ERR_NONE;
+	  /* Convert segment.size to sectors, rounding up to

nearest sector */

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10);

grub-core/disk/luks2.c +2 -0
+	  crypt->total_sectors = ALIGN_UP (crypt->total_sectors,
+					   1 <<

crypt->log_sector_size);

grub-core/disk/luks2.c +7 -0
+	  crypt->total_sectors >>= crypt->log_sector_size;
+
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " \"%s\" is not a parsable

number\n",

grub-core/disk/luks2.c +7 -0
+				     segment.idx, segment.size);
+	      continue;
+	    }
+	  else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /*
+	       * There was an overflow in parsing segment.size, so

disk must

grub-core/disk/luks2.c +3 -0
+	       * be very large or the string is incorrect.
+	       */
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " %s overflowed 64-bit

unsigned integer,"

Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683

Quoted 5 lines
grub-core/disk/luks2.c +1 -0
+				     " the end of the crypto

device will be"

grub-core/disk/luks2.c +3 -0
+				     " inaccessible\n",
+				     segment.idx, segment.size);
+	      if (crypt->total_sectors > max_crypt_sectors)

I think this if is bogus. You should clamp crypt->total_sectors without any checks here.

Quoted 6 lines

Actually, I wouldn't call this a clamp because in the overflow case crypt->total_sectors always equals 0. I just realized this, and its because grub_strtoull will return 2^64-1 thus causing the following ALIGN_UP to overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before the ALIGN_UP code was introduced, which would ALIGN_DOWN.

Ugh... Right. That is why I still think you should do further calculations on value returned from grub_strtoull() if grub_errno == GRUB_ERR_NONE only. I understand that then crypt->total_sectors does not contain total sectors for a while but you can rectify this a bit by putting short comment before grub_strtoull() call.

Quoted 16 lines

Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so grub_strtoull will return 2^64-1, this should have us set crypt->total_sectors to 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this case max_crypt_sectors will be 2^(58+9-12) => 2^57 sectors. So here we see that crypt->total_sectors < max_crypt_sectors in the overflow case. If we do as you suggest crypt->total_sectors will be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).

Quoted 7 lines

On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.

If think the code should look like this:

/*
* ...a comment saying what crypt->total_sectors contains
* and why LUKS2 volumes larger than 2^64 does not work... */
crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);

  if (grub_errno == GRUB_ERR_NONE)
    {
      crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 << crypt->log_sector_size);
      crypt->total_sectors >>= crypt->log_sector_size;
    }
  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
    {
      ...
      continue;
    }

if (grub_errno == GRUB_ERR_OUT_OF_RANGE ||

      !crypt->total_sectors ||
      crypt->total_sectors > max_crypt_sectors)
    {
      ...
      continue;
      /*
       * Yes, I think we should not guess crypt->total_sectors
       * value and always fail. It seems safer.
       */
    }

Daniel

#273 lore raw

Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk

Quoted 197 lines · On Sat, 12 Dec 2020 16:19:18 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 149 lines · Glenn Washburn
Quoted 137 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 136 lines · Glenn Washburn

First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.

---
 grub-core/disk/luks2.c | 84

++++++++++++++++++++++++++++++++++++++++-- include/grub/disk.h
| 17 +++++++++ 2 files changed, 98 insertions(+), 3
deletions(-)

grub-core/disk/luks2.c +3 -0
index 9abcb1c94..8cb11e899 100644
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      /* FIXME: Allow use of source disk, and maybe cause

errors in read. */
+ grub_dprintf ("luks2", "Source disk %s has an unknown
size, "

grub-core/disk/luks2.c +1 -0
+			     "conservatively returning

error\n", source->name);
+ ret = grub_error (GRUB_ERR_BUG, "Unknown size of luks2
source device");

grub-core/disk/luks2.c +6 -0
+      goto err;
+    }
+
   /* Try all keyslot */
   for (json_idx = 0; json_idx < size; json_idx++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment,

json, json_idx); if (ret)

grub-core/disk/luks2.c +2 -0
 	goto err;
+      if (grub_errno != GRUB_ERR_NONE)
+	  grub_dprintf ("luks2", "Ignoring unhandled error %d

from luks2_get_keyslot\n", grub_errno);

       if (keyslot.priority == 0)
grub-core/disk/luks2.c +0 -0
 	{
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source,
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); crypt->log_sector_size = sizeof (unsigned int) * 8
- __builtin_clz ((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */

grub-core/disk/luks2.c +2 -0
+      max_crypt_sectors = grub_disk_convert_sector(source,
+

source->total_sectors,
+
crypt->log_sector_size); +

grub-core/disk/luks2.c +3 -0
+      if (max_crypt_sectors < crypt->offset_sectors)
+	{
+	  grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" has offset"

grub-core/disk/luks2.c +1 -0
+				 " %"PRIuGRUB_UINT64_T" which

is greater than"

grub-core/disk/luks2.c +1 -0
+				 " source disk size

%"PRIuGRUB_UINT64_T","

grub-core/disk/luks2.c +2 -0
+				 " skipping\n",
+				 segment.idx,

crypt->offset_sectors,

grub-core/disk/luks2.c +4 -1
+				 max_crypt_sectors);
+	  continue;
+	}
+
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >>

(crypt->log_sector_size - source->log_sector_size))

grub-core/disk/luks2.c +1 -1
-			       - crypt->offset_sectors;
+	crypt->total_sectors = max_crypt_sectors -

crypt->offset_sectors; else

grub-core/disk/luks2.c +0 -1
-	crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +3 -0
+	{
+	  grub_errno = GRUB_ERR_NONE;
+	  /* Convert segment.size to sectors, rounding up to

nearest sector */

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10);

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = ALIGN_UP

(crypt->total_sectors,

grub-core/disk/luks2.c +1 -0
+					   1 <<

crypt->log_sector_size);

grub-core/disk/luks2.c +7 -0
+	  crypt->total_sectors >>= crypt->log_sector_size;
+
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " \"%s\" is not a parsable

number\n",

grub-core/disk/luks2.c +1 -0
+				     segment.idx,

segment.size);

grub-core/disk/luks2.c +6 -0
+	      continue;
+	    }
+	  else if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /*
+	       * There was an overflow in parsing

segment.size, so disk must

grub-core/disk/luks2.c +3 -0
+	       * be very large or the string is incorrect.
+	       */
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +1 -0
+				     " %s overflowed 64-bit

unsigned integer,"

grub-core/disk/luks2.c +1 -0
+				     " the end of the crypto
Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683

Quoted 5 lines

device will be"

grub-core/disk/luks2.c +2 -0
+				     " inaccessible\n",
+				     segment.idx,

segment.size);

grub-core/disk/luks2.c +1 -0
+	      if (crypt->total_sectors > max_crypt_sectors)

I think this if is bogus. You should clamp crypt->total_sectors without any checks here.

Quoted 6 lines

Actually, I wouldn't call this a clamp because in the overflow case crypt->total_sectors always equals 0. I just realized this, and its because grub_strtoull will return 2^64-1 thus causing the following ALIGN_UP to overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before the ALIGN_UP code was introduced, which would ALIGN_DOWN.

Ugh... Right. That is why I still think you should do further calculations on value returned from grub_strtoull() if grub_errno == GRUB_ERR_NONE only. I understand that then crypt->total_sectors does not contain total sectors for a while but you can rectify this a bit by putting short comment before grub_strtoull() call.

Quoted 17 lines

Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so grub_strtoull will return 2^64-1, this should have us set crypt->total_sectors to 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this case max_crypt_sectors will be 2^(58+9-12) => 2^57 sectors. So here we see that crypt->total_sectors < max_crypt_sectors in the overflow case. If we do as you suggest crypt->total_sectors will be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).

Quoted 8 lines

On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.

If think the code should look like this:

/*
* ...a comment saying what crypt->total_sectors contains
* and why LUKS2 volumes larger than 2^64 does not work... */
crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);

Quoted 5 lines

if (grub_errno == GRUB_ERR_NONE)

    {
      crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 <<

crypt->log_sector_size); crypt->total_sectors >>=
crypt->log_sector_size; }

Now that ALIGN_UP is being used which we don't want in the GRUB_ERR_OUT_OF_RANGE case, I'm ok with this. Although, if we just continue in grub_strtoull error case as you suggest below, it doesn't matter. And this actually doesn't prevent ALIGN_UP from potentially overflowing crypt->total_sectors either, when segment.size is very large. Perhaps we should check for that.

Quoted 7 lines
  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
    {
      ...
      continue;
    }

if (grub_errno == GRUB_ERR_OUT_OF_RANGE ||

Quoted 9 lines
      !crypt->total_sectors ||
      crypt->total_sectors > max_crypt_sectors)
    {
      ...
      continue;
      /*
       * Yes, I think we should not guess crypt->total_sectors
       * value and always fail. It seems safer.
       */

I agree this is safer, but its at the cost of using volumes over 2^64 bytes working at all. I think the question is how safe is it to allow arbitrary disk read failures in grub. Ultimately I don't really like continuing here, which is why I originally had the TODO, because it prevents otherwise accessible drives from being accessible (admittedly in an extremely rare case). Think about a 2^65 sized raid with LUKS on top and LVM on top of that. But really you should probably not be booting from that anyway. I think I'll add a TODO here, with a work around suggesting not to use such large devices to boot from.

Quoted 1 line
    }

Glenn

#274 lore raw

PATCH v8 15/18 luks2: Error check segment.sector_size

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
index 8cb11e899..c737797b9 100644
@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.idx);
 
+      /* Sector size should be one of 512, 1024, 2048, or 4096. */
+      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
+	    segment.sector_size == 2048 || segment.sector_size == 4096))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
+				 " size %"PRIuGRUB_UINT64_T" is not one of"
+				 " 512, 1024, 2048, or 4096\n",
+				 segment.idx, segment.sector_size);
+	  continue;
+	}
+
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
Signature · 1 line
2.27.0
Reviewed-by on v8 15/18
#276 lore raw

Re: [PATCH v8 15/18] luks2: Error check segment.sector_size

In reply to Glenn Washburn
Quoted 1 line · Glenn Washburn
Quoted 29 lines
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
index 8cb11e899..c737797b9 100644
@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.idx);
 
+      /* Sector size should be one of 512, 1024, 2048, or 4096. */
+      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
+	    segment.sector_size == 2048 || segment.sector_size == 4096))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
+				 " size %"PRIuGRUB_UINT64_T" is not one of"
+				 " 512, 1024, 2048, or 4096\n",
+				 segment.idx, segment.sector_size);
+	  continue;
+	}
+
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
Signature · 1 line
2.27.0
#277 lore raw

PATCH v8 16/18 mips: Enable __clzdi2()

In reply to Glenn Washburn

This patch is similiar to commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()) but for MIPS target and __clzdi2 only, __clzsi2 was already enabled.

---
 grub-core/kern/compiler-rt.c | 2 +-
 include/grub/compiler-rt.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/compiler-rt.c +1 -1
index a464200c6..2057c2e0c 100644
@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val)
 }
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 __clzdi2 (grub_uint64_t val)
 {
include/grub/compiler-rt.h +1 -1
index 7591980b4..17828b322 100644
@@ -115,7 +115,7 @@ int
 EXPORT_FUNC (__clzsi2) (grub_uint32_t val);
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 EXPORT_FUNC (__clzdi2) (grub_uint64_t val);
 #endif
Signature · 1 line
2.27.0
Reviewed-by on v8 16/18
#279 lore raw

Re: [PATCH v8 16/18] mips: Enable __clzdi2()

In reply to Glenn Washburn
Quoted 6 lines · Glenn Washburn

This patch is similiar to commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()) but for MIPS target and __clzdi2 only, __clzsi2 was already enabled.

Quoted 34 lines
---
 grub-core/kern/compiler-rt.c | 2 +-
 include/grub/compiler-rt.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/compiler-rt.c +1 -1
index a464200c6..2057c2e0c 100644
@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val)
 }
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 __clzdi2 (grub_uint64_t val)
 {
include/grub/compiler-rt.h +1 -1
index 7591980b4..17828b322 100644
@@ -115,7 +115,7 @@ int
 EXPORT_FUNC (__clzsi2) (grub_uint32_t val);
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 EXPORT_FUNC (__clzdi2) (grub_uint64_t val);
 #endif
Signature · 1 line
2.27.0
#280 lore raw

PATCH v8 17/18 misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers

In reply to Glenn Washburn
---
 include/grub/misc.h | 3 +++
 1 file changed, 3 insertions(+)
include/grub/misc.h +3 -0
index 780a34e90..73a514eb1 100644
@@ -482,4 +482,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
 
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)
+
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
Reviewed-by on v8 17/18
#281 lore raw

Re: [PATCH v8 17/18] misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers

Quoted 2 lines · Glenn Washburn
Quoted 19 lines
---
 include/grub/misc.h | 3 +++
 1 file changed, 3 insertions(+)
include/grub/misc.h +3 -0
index 780a34e90..73a514eb1 100644
@@ -482,4 +482,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
 
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)
+
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
#282 lore raw

PATCH v8 18/18 luks2: Use grub_log2ull to calculate log_sector_size and improve readability

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
grub-core/disk/luks2.c +1 -2
index c737797b9..31b51f1d4 100644
@@ -642,8 +642,7 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);
       /* Set to the source disk size, which is the maximum we allow. */
       max_crypt_sectors = grub_disk_convert_sector(source,
 						   source->total_sectors,
Signature · 1 line
2.27.0
Reviewed-by on v8 18/18
#283 lore raw

Re: [PATCH v8 18/18] luks2: Use grub_log2ull to calculate log_sector_size and improve readability

Quoted 2 lines · Glenn Washburn
Quoted 21 lines
---
 grub-core/disk/luks2.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
grub-core/disk/luks2.c +1 -2
index c737797b9..31b51f1d4 100644
@@ -642,8 +642,7 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);
       /* Set to the source disk size, which is the maximum we allow. */
       max_crypt_sectors = grub_disk_convert_sector(source,
 						   source->total_sectors,
Signature · 1 line
2.27.0
#284 lore raw
In reply to Glenn Washburn
Quoted 24 lines · Glenn Washburn

This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.

Glenn

Glenn Washburn (18):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2: grub_cryptodisk_t->total_sectors is the max number of device
native sectors

Quoted 8 lines

cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable __clzdi2()
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit
integers
luks2: Use grub_log2ull to calculate log_sector_size and improve
readability

I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.

Thank you for doing the work!

Daniel

#285 lore raw
Quoted 34 lines · Daniel Kiper
Quoted 24 lines · Glenn Washburn

This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.

Glenn

Glenn Washburn (18):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2: grub_cryptodisk_t->total_sectors is the max number of device
native sectors

Quoted 8 lines

cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable __clzdi2()
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit
integers
luks2: Use grub_log2ull to calculate log_sector_size and improve
readability

Quoted 6 lines

I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.

Thank you for doing the work!

Daniel

For what it's worth, I've also gone through all patches except 14 and added by RBs. Thanks!

Patrick

#286 lore raw
Quoted 35 lines · Patrick Steinhardt
Quoted 34 lines · Daniel Kiper
Quoted 32 lines · Glenn Washburn

This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.

Glenn

Glenn Washburn (18):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2: grub_cryptodisk_t->total_sectors is the max number of device
native sectors
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable __clzdi2()
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit
integers
luks2: Use grub_log2ull to calculate log_sector_size and improve
readability

Quoted 2 lines
Quoted 2 lines

I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.

Thanks! However, I have pushed some of these patches before you did review.

Daniel

#287 lore raw
Quoted 36 lines · Daniel Kiper
Quoted 35 lines · Patrick Steinhardt
Quoted 34 lines · Daniel Kiper
Quoted 32 lines · Glenn Washburn

This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.

Glenn

Glenn Washburn (18):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2: grub_cryptodisk_t->total_sectors is the max number of device
native sectors
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable __clzdi2()
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit
integers
luks2: Use grub_log2ull to calculate log_sector_size and improve
readability

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.

Thanks! However, I have pushed some of these patches before you did review.

Daniel

I thought as much, though I didn't check. I don't really mind, thought it might make sense to send them out in any case.

Patrick

#288 lore raw
Quoted 40 lines · Patrick Steinhardt
Quoted 39 lines · Daniel Kiper
Quoted 37 lines · Patrick Steinhardt
Quoted 36 lines · Daniel Kiper
Quoted 24 lines · Glenn Washburn

This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.

Glenn

Glenn Washburn (18):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2: grub_cryptodisk_t->total_sectors is the max number of device
native sectors

Quoted 8 lines

cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable __clzdi2()
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit
integers
luks2: Use grub_log2ull to calculate log_sector_size and improve
readability

I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.

Quoted 6 lines
Quoted 3 lines

Thanks! However, I have pushed some of these patches before you did review.

Daniel

I thought as much, though I didn't check. I don't really mind, thought it might make sense to send them out in any case.

Yeah, at least I am more sure I did not miss any mistakes... :-)

Daniel