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 2 of 3

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#57 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 138 lines · Glenn Washburn
Quoted 126 lines · On Thu, 19 Nov 2020 15:25:33 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 125 lines · Glenn Washburn
Quoted 102 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 85 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.

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 | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c
  | 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -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;
grub-core/disk/cryptodisk.c +0 -0
@@ -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));

grub-core/disk/cryptodisk.c +2 -2
-  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,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }

grub-core/disk/cryptodisk.c +2 -3
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt

native sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c

Quoted 4 lines
grub-core/disk/cryptodisk.c +4 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf --with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required
alignment of target type [-Werror=cast-align] grub_uint64_t *iv64 = (grub_uint64_t *)iv; ^
cc1: all warnings being treated as errors

Quoted 5 lines

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

I'm having issues building for arm-linux-gnueabihf, so I can't effectively test this. This code does compile for --target=i386 --with-platform=pc, which I believe is 32-bit.

Since I can't test your suggestion, I can't verify that it works. However, I suspect it may get rid of the compiler problem, but not solve the underlying issue, which is that iv may not be 8-byte aligned. My guess is that if the compiler message goes away you could get a crash at the next line if iv is not 8-byte aligned. Can you see if the warning disappears if you define iv like this:

  grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
  __attribute__((aligned(sizeof(grub_uint64_t))));
Quoted 8 lines
Quoted 2 lines

I'm not sure the best way to handle this the "grub way". Do you see a better way to do this?

Below you can find all fixes which are needed to properly build the GRUB with your patches on all platforms. If you have any questions drop me a line.

Daniel

Quoted 9 lines

Great, thanks for the help. I'm confused as to why the changes to compiler-rt* are needed now. I assume that they are needed because of the use of __builtin_clzll. My v4 changes include a patch that moves the use of __builtin_clzll in luks2.c into a macro in misc.h, but I don't think that would have necessitated this change because the only code that uses the macro is in luks2.c. So the compiler should see no difference from current master. Could it be that mips targets are not properly building with current master due to the use of __builtin_clzll? Or am I totally off?

I am not sure why this pop up after your patch series. If I have some time I will dig into it.

Quoted 3 lines

I'll add these in the forth coming patch series. For now, I'll assume that I should put the compiler-rt* changes in a separate patch and merge in the cryptodisk.c changes.

Please provide compiler-rt* changes in a separate patch. Good example is in commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()). Please include this fix in new patch series.

Daniel

#127 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Glenn Washburn
Quoted 43 lines · Glenn Washburn
Quoted 30 lines · On Tue, 17 Nov 2020 15:26:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 29 lines · Glenn Washburn
---
 grub-core/disk/luks2.c | 70

+++++++++++++++++++++++++++++++++++++++--- include/grub/misc.h |
2 ++ 2 files changed, 67 insertions(+), 5 deletions(-)

grub-core/disk/luks2.c +7 -0
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
 i); if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
Quoted 9 lines

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

Quoted 7 lines
grub-core/disk/luks2.c +1 -2
       /* 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);

This change does not belong to this patch.

Quoted 1 line

I'll put it in a new patch.

Thanks!

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
grub-core/disk/luks2.c +0 -2
       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;
Quoted 8 lines
Quoted 5 lines
grub-core/disk/luks2.c +2 -0
+	{
+	  /* Convert source sized number of sectors to cryptodisk

sized sectors */

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

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

If you add the other checks could you also add the following check:
crypt->log_sector_size >= source->log_sector_size ?

Quoted 2 lines

Why should this be a constraint? I could add it, but I don't see the problem when crypt->log_sector_size < source->log_sector_size.

Is it possible and supported? If yes OK but then I think the result of (crypt->log_sector_size - source->log_sector_size) has limited set of values which do not overflow/underflow crypt->total_sectors.

[...]

Quoted 4 lines
Quoted 2 lines

By the way, may I ask you to stop adding full stop to the end of email subject?

If I understand correctly, you dislike periods ('.') at the end of the

Yep!

Quoted 2 lines

email subject line. I can not add them. What do you dislike about them there? (extra superfluous character?)

Yeah, extra superfluous character which should not be there.

Daniel

#58 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Daniel Kiper
Quoted 160 lines · On Fri, 20 Nov 2020 15:17:25 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 142 lines · Glenn Washburn
Quoted 130 lines · On Thu, 19 Nov 2020 15:25:33 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 129 lines · Glenn Washburn
Quoted 105 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 87 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.

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 | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c
  | 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644 --- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c

(quoted diff) +2 -1
@@ -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;
(quoted diff) +0 -0
@@ -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));

(quoted diff) +2 -2
-  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,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }

(quoted diff) +2 -3
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt

native sector

(quoted diff) +1 -0
+	   * number. If using 32 bit IV mode, zero out the

most significant

Review

Quoted 4 lines
(quoted diff) +4 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf
--with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required
alignment of target type [-Werror=cast-align] grub_uint64_t *iv64 = (grub_uint64_t *)iv; ^
cc1: all warnings being treated as errors

Quoted 5 lines

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

I'm having issues building for arm-linux-gnueabihf, so I can't effectively test this. This code does compile for --target=i386 --with-platform=pc, which I believe is 32-bit.

Since I can't test your suggestion, I can't verify that it works. However, I suspect it may get rid of the compiler problem, but not solve the underlying issue, which is that iv may not be 8-byte aligned. My guess is that if the compiler message goes away you could get a crash at the next line if iv is not 8-byte aligned. Can you see if the warning disappears if you define iv like this:

  grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
  __attribute__((aligned(sizeof(grub_uint64_t))));
Quoted 8 lines
Quoted 2 lines

I'm not sure the best way to handle this the "grub way". Do you see a better way to do this?

Below you can find all fixes which are needed to properly build the GRUB with your patches on all platforms. If you have any questions drop me a line.

Daniel

Quoted 9 lines

Great, thanks for the help. I'm confused as to why the changes to compiler-rt* are needed now. I assume that they are needed because of the use of __builtin_clzll. My v4 changes include a patch that moves the use of __builtin_clzll in luks2.c into a macro in misc.h, but I don't think that would have necessitated this change because the only code that uses the macro is in luks2.c. So the compiler should see no difference from current master. Could it be that mips targets are not properly building with current master due to the use of __builtin_clzll? Or am I totally off?

I am not sure why this pop up after your patch series. If I have some time I will dig into it.

Quoted 3 lines

I'll add these in the forth coming patch series. For now, I'll assume that I should put the compiler-rt* changes in a separate patch and merge in the cryptodisk.c changes.

Quoted 5 lines

Please provide compiler-rt* changes in a separate patch. Good example is in commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()). Please include this fix in new patch series.

Daniel

Ok, will do. I'm unsure how to commit the patch though. Since you are the author and I am the committer. I'm thinking I should have my name as the commit author, but yours should be in a Signed-off-by sig and I don't need to add a Signed-off-by sig. How should I proceed? I'll follow the commit message template from 9dab2f51e. Also, how should I attribute your contribution in cryptodisk.c?

Glenn

#128 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Daniel Kiper
Quoted 62 lines · On Fri, 20 Nov 2020 21:24:10 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 44 lines · Glenn Washburn
Quoted 30 lines · On Tue, 17 Nov 2020 15:26:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 29 lines · Glenn Washburn
---
 grub-core/disk/luks2.c | 70

+++++++++++++++++++++++++++++++++++++++--- include/grub/misc.h
| 2 ++ 2 files changed, 67 insertions(+), 5 deletions(-)

grub-core/disk/luks2.c +7 -0
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment,
 json, i); if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
Quoted 10 lines

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

Quoted 8 lines
grub-core/disk/luks2.c +1 -2
       /* 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);

This change does not belong to this patch.

Quoted 1 line

I'll put it in a new patch.

Thanks!

Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
grub-core/disk/luks2.c +2 -2
       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;
+	{
+	  /* Convert source sized number of sectors to

cryptodisk sized sectors */

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

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

If you add the other checks could you also add the following
check: crypt->log_sector_size >= source->log_sector_size ?

Quoted 4 lines
Quoted 2 lines

Why should this be a constraint? I could add it, but I don't see the problem when crypt->log_sector_size < source->log_sector_size.

Is it possible and supported?

Why shouldn't it be possible? LUKS doesn't care about the underlying sector size of the disk its on. What is the underlying sector size for a LUKS volume in a file on a file system with 2k block size on a disk with 4k sector sizes? Its all just a stream of bytes to LUKS. We use the cryptodisks sector size to determine how to decrypt it, which is why it matters for grub. But the source disk sector size adds no constraints on LUKS nor the cryptodisk. We just have to translate between the two occasionally. Its certainly supported by the linux implementation of LUKS. This is a good candidate to add to my tests.

Quoted 3 lines

If yes OK but then I think the result of (crypt->log_sector_size - source->log_sector_size) has limited set of values which do not overflow/underflow crypt->total_sectors.

Yes, that is problematic in the context of the bit shifts that are being done, which do not work as desired when the right operand is negative. I'm refactoring this patch to remove that constraint.

Quoted 8 lines

[...]

Quoted 5 lines
Quoted 2 lines

By the way, may I ask you to stop adding full stop to the end of email subject?

If I understand correctly, you dislike periods ('.') at the end of the

Quoted 8 lines

Yep!

Quoted 2 lines

email subject line. I can not add them. What do you dislike about them there? (extra superfluous character?)

Yeah, extra superfluous character which should not be there.

Daniel

I just got it, its really that there should be no periods at the end of the first line of the commit message. I'll take care of that.

Glenn

#121 lore raw

Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 285 lines · Glenn Washburn
Quoted 274 lines · On Sun, 15 Nov 2020 11:07:27 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 273 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.

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, 49 insertions(+), 26 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 31b73c535..61f8e57f4 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -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;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,12 +238,12 @@ 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));

grub-core/disk/cryptodisk.c +2 -2
-  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)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
__attribute__((aligned (sizeof (grub_uint64_t)))); 
       if (dev->rekey)
grub-core/disk/cryptodisk.c +1 -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));

grub-core/disk/cryptodisk.c +2 -3
@@ -281,15 +282,25 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* 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

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c +6 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 -

GRUB_CRYPTODISK_IV_LOG_SIZE));

grub-core/disk/cryptodisk.c +4 -0
+	    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])

grub-core/disk/cryptodisk.c +0 -1
-					       -

dev->log_sector_size));

grub-core/disk/cryptodisk.c +0 -1
-	  iv[0] = grub_cpu_to_le32 ((sector <<

dev->log_sector_size)

grub-core/disk/cryptodisk.c +2 -0
+					       - 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 +323,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_CBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); else
err = grub_crypto_cbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_PCBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); else
err = grub_crypto_pcbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +2 -2
 	    return err;
 	  break;
@@ -338,7 +349,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 +380,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,

grub-core/disk/cryptodisk.c +0 -1
-					     (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					     (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_ECB:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); else
err = grub_crypto_ecb_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

grub-core/disk/cryptodisk.c +1 -1
 			 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-core/disk/cryptodisk.c +1 -1
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector,

log_sector_size, 0); }

grub_err_t

grub-core/disk/cryptodisk.c +2 -2
@@ -767,7 +778,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 +819,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 +1 -1
index aa9877b68..84c3fa73a 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_get_size (disk) -

newdev->offset_sectors; grub_memcpy (newdev->uuid, uuid, sizeof (uuid)); newdev->modname = "luks";

grub-core/disk/luks.c +2 -1
@@ -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-core/disk/luks.c +0 -0
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 355bb4aec..4a4a0dec4 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 +0 -0
index 258b777bf..ee30e4537 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
Quoted 7 lines
Quoted 2 lines
include/grub/cryptodisk.h +1 -0
+/* LUKS1 specification defines the block size to always be 512

bytes. */ +#define GRUB_LUKS1_LOG_SECTOR_SIZE 9

Sorry to be nitpicky, but this constant is used for both LUKS1 and LUKS2. Shouldn't it just be called GRUB_LUKS_LOG_SECTOR_SIZE?

Patrick

Quoted 8 lines

Its named LUKS1 because that macro is meant to represent the log of the sector size for the encrypted data segment, which is fixed at 512-bytes (as you know LUKS2 is variable). I think a name suggesting its only for the encrypted key data would be even longer and more unwieldy. And I don't think just removing the '1' would be an accurate name. Do you have other suggestions?

Glenn

I mean we could split it up into two constants, GRUB_LUKS1_LOG_SECTOR_SIZE and GRUB_LUKS2_KEYSLOT_LOG_SECTOR_SIZE. Not sure if it's worth it, though.

Patrick

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
include/grub/cryptodisk.h +3 -1
+/* 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) @@ -145,7 +151,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t

dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

include/grub/cryptodisk.h +1 -1
 			 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
#133 lore raw

v5 cover Cryptodisk fixes for v2.06 redux

In reply to Glenn Washburn

This incorporates changes as noted in the v4 thread. This series has been rebased on the new master from accepted patches from v4. This means that patch 8 from v4 would be patch 1 in this series. However, I reworked 8-10 from v4 from 3 patches into 2, and I believe simplified the changeset. So v4 patches 8-10 are equivalent to patches 1-2 here.

Also, the v4 patch 14 has been nearly completely reworked to be easier to follow and more precisely reflect the error handling I wanted.

The changes that created and used grub_log2ull in v4 patch 14 has been pulled out into two separate patches. And there is a new patch converting spaces to tabs missed in the original commit of luks2.

Glenn

Glenn Washburn (11):
luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
luks: Use more intuitive slot key instead of index in user messages.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt
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
whitespace: convert 8 spaces to tabs.
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

 grub-core/disk/cryptodisk.c  |  64 ++++++++++------
 grub-core/disk/luks.c        |   5 +-
 grub-core/disk/luks2.c       | 144 ++++++++++++++++++++++++++++-------
 grub-core/kern/compiler-rt.c |   2 +-
 include/grub/compiler-rt.h   |   2 +-
 include/grub/cryptodisk.h    |   8 +-
 include/grub/disk.h          |  16 ++++
 include/grub/misc.h          |   3 +
 include/grub/types.h         |   5 ++
 9 files changed, 192 insertions(+), 57 deletions(-)

Range-diff against v4:

 1:  f1d530757 <  -:  --------- luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
 2:  b90ab8e75 <  -:  --------- luks2: Improve error messages in luks2_get_keyslot.
 -:  --------- >  1:  ec506b668 luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
 3:  51add6875 !  2:  3a9fca36f luks2: Use more intuitive keyslot key instead of index when naming keyslot.

@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- luks2: Use more intuitive keyslot key instead of index when naming keyslot.
+ luks: Use more intuitive slot key instead of index in user messages.

- Use the keyslot key value in the keyslot json array rather than the index of
- the keyslot in the json array. This is less confusing for the end user. For
- example, say you have a LUKS2 device with a key in slot 1 and slot 4. When
- using the password for slot 4 to unlock the device, the messages using the
- index of the keyslot will mention keyslot 1 (its a zero-based index).
- Furthermore, with this change the keyslot number will align with the number
- used to reference the keyslot when using the --key-slot argument to
- cryptsetup.
+ Use the slot key name in the json array rather than the 0 based index in the
+ json array for keyslots, segments, and digests. This is less confusing for
+ the end user. For example, say you have a LUKS2 device with a key in slot 1
+ and slot 4. When using the password for slot 4 to unlock the device, the
+ messages using the index of the keyslot will mention keyslot 1 (its a
+ zero-based index). Furthermore, with this change the keyslot number will
+ align with the number used to reference the keyslot when using the
+ --key-slot argument to cryptsetup. Error messages now distinguish between
+ indexes and slot keys. The former include the string "index" in the error
+ string, and the later are surrounded in quotes.

## grub-core/disk/luks2.c ##
-@@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
-
- struct grub_luks2_keyslot
- {
-+ grub_uint64_t slot_key;
- grub_int64_t key_size;
- grub_int64_t priority;
- struct
-@@ grub-core/disk/luks2.c: typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
-
- struct grub_luks2_segment
- {
-+ grub_uint64_t slot_key;
- grub_uint64_t offset;
- const char *size;
- const char *encryption;
-@@ grub-core/disk/luks2.c: typedef struct grub_luks2_segment grub_luks2_segment_t;
-
- struct grub_luks2_digest
- {
-+ grub_uint64_t slot_key;
- /* Both keyslots and segments are interpreted as bitfields here */
- grub_uint64_t keyslots;
- grub_uint64_t segments;
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
- {
- grub_json_t keyslots, keyslot, digests, digest, segments, segment;
- grub_size_t i, size;
-- grub_uint64_t keyslot_key, digest_key, segment_key;
-+ grub_uint64_t digest_key, segment_key;
-
- /* Get nth keyslot */
- if (grub_json_getvalue (&keyslots, root, "keyslots") ||
- grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-- grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
-+ grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
+ grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
grub_json_getchild (&keyslot, &keyslot, 0) ||
luks2_parse_keyslot (k, &keyslot))
- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
+- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
+
+ /* Get digest that matches the keyslot. */
+ if (grub_json_getvalue (&digests, root, "digests") ||
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
+ grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
+ grub_json_getchild (&digest, &digest, 0) ||
luks2_parse_digest (d, &digest))
- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
+- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

-- if ((d->keyslots & (1 << keyslot_key)))
-+ d->slot_key = digest_key;
-+ if ((d->keyslots & (1 << k->slot_key)))
+ if ((d->keyslots & (1 << k->slot_key)))
break;
}
if (i == size)
-- return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+- return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);

/* Get segment that matches the digest. */
if (grub_json_getvalue (&segments, root, "segments") ||
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
+ grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
+ grub_json_getchild (&segment, &segment, 0) ||
luks2_parse_segment (s, &segment))
- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
+- return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);

-+ s->slot_key = segment_key;
- if ((d->segments & (1 << segment_key)))
+ if ((d->segments & (1 << s->slot_key)))
break;
}
+ if (i == size)
+- return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
++ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+
+ return GRUB_ERR_NONE;
+ }
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,

if (keyslot.priority == 0)
4: 39e382053 ! 3: f45f5f3dd cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
+ cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of
@@ include/grub/types.h: typedef grub_int32_t grub_ssize_t;
#endif
# define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)

-+#define GRUB_TYPE_U_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BITS (type) - 1)) - 1)) + 1) ++#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof (type))(~0)))
+#define GRUB_TYPE_U_MIN(type) 0ULL
+
typedef grub_uint64_t grub_properly_aligned_t;
5: 7696a000f ! 4: da2b63607 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
+ luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

- The total_length field is named confusingly because length usually refers to
- bytes, whereas in this case its really the total number of sectors on the
- device. Also counter-intuitively, grub_disk_get_size returns the total
- number of device native sectors. We need to convert the sectors from the
- size of the underlying device to the cryptodisk sector size. And
- segment.size is in bytes which need to be converted to cryptodisk sectors.
+ 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. And counter-intuitively, grub_disk_get_size
+ returns the total number of device native sectors.

Also, removed an empty statement.

6: 43437eb13 ! 5: 036304262 cryptodisk: Properly handle non-512 byte sized sectors.

    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    cryptodisk: Properly handle non-512 byte sized sectors.
    +    cryptodisk: Properly handle non-512 byte sized sectors
     
         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
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
          {
            grub_size_t sz = ((dev->cipher->cipher->blocksize
      			 + sizeof (grub_uint32_t) - 1)
    - 			/ sizeof (grub_uint32_t));
    --      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
    -+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4] __attribute__((aligned (sizeof (grub_uint64_t))));
    - 
    -       if (dev->rekey)
    - 	{
     @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
      	    if (!ctx)
      	      return GPG_ERR_OUT_OF_MEMORY;
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
     +	   * 32 bits.
     +	   */
     +	  {
    -+	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
    -+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
    ++	    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;
     +	  }
 7:  71dadcd07 <  -:  --------- luks2: Better error handling when setting up the cryptodisk.
 -:  --------- >  6:  7bf56c4b3 luks2: Better error handling when setting up the cryptodisk
 8:  cc96baf5a !  7:  b167b014d luks2: Error check segment.sector_size.
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Error check segment.sector_size.
    +    luks2: Error check segment.sector_size
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
     +      /* 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))
    ++	    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.",
    ++				 " 512, 1024, 2048, or 4096\n",
     +				 segment.slot_key, 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 = grub_log2ull (segment.sector_size);
    +       crypt->log_sector_size = sizeof (unsigned int) * 8
 -:  --------- >  8:  c503ecbb8 whitespace: convert 8 spaces to tabs.
 -:  --------- >  9:  8316e94ce mips: Enable __clzdi2()
 -:  --------- > 10:  37257725c misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
 -:  --------- > 11:  8dd088cad luks2: Use grub_log2ull to calculate log_sector_size and improve readability
Signature · 1 line
2.27.0
#134 lore raw

PATCH v5 1/11 luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +8 -5
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &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_idx);
@@ -276,11 +278,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&d->slot_key, &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, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
@@ -293,12 +296,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&s->slot_key, &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, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << s->slot_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
#135 lore raw

PATCH v5 2/11 luks: Use more intuitive slot key instead of index in user messages.

In reply to Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup. Error messages now distinguish between indexes and slot keys. The former include the string "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
grub-core/disk/luks2.c +12 -12
index ab2c31dcd..c390ea3e6 100644
@@ -269,7 +269,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&k->slot_key, &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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -281,13 +281,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -299,13 +299,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->slot_key)))
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -601,11 +601,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -620,16 +620,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -637,7 +637,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#136 lore raw

PATCH v5 3/11 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

In reply to Glenn Washburn

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type. Also 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 | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +7 -6
index 473c93976..31b73c535 100644
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  iv[1] = grub_cpu_to_le32 (sector >> 32);
 	  /* 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 >> (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);
+				    & 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 >> 32);
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
+	    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 +5 -0
index 9cd7e2525..b9c7426fc 100644
@@ -72,6 +72,8 @@
 # endif
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
 #ifndef __CHAR_BIT__
 # error __CHAR_BIT__ is not defined
 #elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((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
#137 lore raw

PATCH v5 4/11 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. And counter-intuitively, grub_disk_get_size returns the total number of device native sectors.

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 c390ea3e6..e33df9803 100644
@@ -426,7 +426,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;
@@ -612,9 +612,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
#138 lore raw

PATCH v5 5/11 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 31b73c535..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 >> 32);
-	  /* 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 aa9877b68..84c3fa73a 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_get_size (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 e33df9803..5e3b255b9 100644
@@ -501,7 +501,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 258b777bf..ee30e4537 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)
@@ -145,7 +151,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
#139 lore raw

PATCH v5 6/11 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

---
 grub-core/disk/luks2.c | 77 ++++++++++++++++++++++++++++++++++++++++--
 include/grub/disk.h    | 16 +++++++++
 2 files changed, 90 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +74 -3
index 5e3b255b9..2f2515fb6 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
 	{
@@ -616,11 +630,68 @@ 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"
+				 " greater than source disk size, skipping\n",
+				 segment.slot_key);
+	  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;
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " is not a parsable number\n",
+				     segment.slot_key);
+	      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"
+				     " overflowed 64-bit unsigned integer,"
+				     " the end of the crypto device will be"
+				     " inaccessible\n",
+				     segment.slot_key);
+	      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.slot_key);
+	  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.slot_key);
+	  /* 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 +16 -0
index 132a1bb75..1a9e8fcf4 100644
@@ -174,6 +174,22 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 /* Return value of grub_disk_get_size() 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)
+    {
+      /* Round up to the nearest log_sector_size sized sector. */
+      sector += 1ULL << ((log_sector_size / disk->log_sector_size) - 1);
+      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
#140 lore raw

PATCH v5 7/11 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 2f2515fb6..0d838f2e0 100644
@@ -626,6 +626,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
+      /* 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.slot_key, 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
#141 lore raw

PATCH v5 8/11 whitespace: convert 8 spaces to tabs.

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index 0d838f2e0..7e7b79d17 100644
@@ -279,8 +279,8 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     {
       if (grub_json_getchild (&digest, &digests, i) ||
 	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
-          grub_json_getchild (&digest, &digest, 0) ||
-          luks2_parse_digest (d, &digest))
+	  grub_json_getchild (&digest, &digest, 0) ||
+	  luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->slot_key)))
@@ -298,7 +298,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
-          luks2_parse_segment (s, &segment))
+	  luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->slot_key)))
@@ -622,7 +622,7 @@ luks2_recover_key (grub_disk_t source,
 	{
 	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
-        }
+	}
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
Signature · 1 line
2.27.0
#142 lore raw

PATCH v5 9/11 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
#143 lore raw

PATCH v5 10/11 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 b7ca6dd58..85a481f0c 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
#144 lore raw

PATCH v5 11/11 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 7e7b79d17..de3933be8 100644
@@ -639,8 +639,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
#145 lore raw

v6 cover Cryptodisk fixes for v2.06 redux

In reply to Glenn Washburn

This is a minor update to v5. There's a commit message fix, changes to debug messages to add some more information on error, and an extra patch which removes unused arguments to grub_error in luks2_parse_segment and luks2_parse_digest.

Glenn

Glenn Washburn (12):
luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive slot key instead of index in user messages.
luks2: Remove unused argument in grub_error
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt
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
whitespace: convert 8 spaces to tabs.
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

 grub-core/disk/cryptodisk.c  |  64 +++++++++------
 grub-core/disk/luks.c        |   5 +-
 grub-core/disk/luks2.c       | 153 ++++++++++++++++++++++++++++-------
 grub-core/kern/compiler-rt.c |   2 +-
 include/grub/compiler-rt.h   |   2 +-
 include/grub/cryptodisk.h    |   8 +-
 include/grub/disk.h          |  16 ++++
 include/grub/misc.h          |   3 +
 include/grub/types.h         |   5 ++
 9 files changed, 198 insertions(+), 60 deletions(-)

Range-diff against v5:

 1:  e2fbfa3ad =  1:  6262aefe9 luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
 2:  a7be73eb2 !  2:  3cd52834b luks: Use more intuitive slot key instead of index in user messages.

@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- luks: Use more intuitive slot key instead of index in user messages.
+ luks2: Use more intuitive slot key instead of index in user messages.

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for

 -:  --------- >  3:  8527be145 luks2: Remove unused argument in grub_error
 3:  2598569ee =  4:  1a248b679 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt
 4:  6d67abeb4 =  5:  45f5d644f luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors
 5:  450a8b5a7 =  6:  6e01cafb1 cryptodisk: Properly handle non-512 byte sized sectors
 6:  268cceda5 !  7:  d8927c6bc luks2: Better error handling when setting up the cryptodisk
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +      if (max_crypt_sectors < crypt->offset_sectors)
     +	{
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has offset"
    -+				 " greater than source disk size, skipping\n",
    -+				 segment.slot_key);
    ++				 " %"PRIuGRUB_UINT64_T" which is greater than"
    ++				 " source disk size %"PRIuGRUB_UINT64_T","
    ++				 " skipping\n",
    ++				 segment.slot_key, crypt->offset_sectors,
    ++				 max_crypt_sectors);
     +	  continue;
     +	}
     +
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	    {
     +	      /* TODO: Unparsable number-string, try to use the whole disk */
     +	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
    -+				     " is not a parsable number\n",
    -+				     segment.slot_key);
    ++				     " \"%s\" is not a parsable number\n",
    ++				     segment.slot_key, segment.size);
     +	      continue;
     +	    }
     +	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	       * be very large or the string is incorrect.
     +	       */
     +	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
    -+				     " overflowed 64-bit unsigned integer,"
    ++				     " %s overflowed 64-bit unsigned integer,"
     +				     " the end of the crypto device will be"
     +				     " inaccessible\n",
    -+				     segment.slot_key);
    ++				     segment.slot_key, segment.size);
     +	      if (crypt->total_sectors > max_crypt_sectors)
     +		crypt->total_sectors = max_crypt_sectors;
     +	    }
 7:  cce3b042e =  8:  fcd7aadb7 luks2: Error check segment.sector_size
 8:  249967edc =  9:  61f77a1a8 whitespace: convert 8 spaces to tabs.
 9:  88490d61e = 10:  d71d26701 mips: Enable __clzdi2()
10:  5f3ae1e2d = 11:  8aa295f77 misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
11:  cc56ab2a0 = 12:  7050a4ace luks2: Use grub_log2ull to calculate log_sector_size and improve readability
Signature · 1 line
2.27.0
#146 lore raw

PATCH v6 1/12 luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +8 -5
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &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_idx);
@@ -276,11 +278,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&d->slot_key, &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, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
@@ -293,12 +296,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&s->slot_key, &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, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << s->slot_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
#152 lore raw

PATCH v6 2/12 luks2: Use more intuitive slot key instead of index in user messages.

In reply to Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup. Error messages now distinguish between indexes and slot keys. The former include the string "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
grub-core/disk/luks2.c +12 -12
index ab2c31dcd..c390ea3e6 100644
@@ -269,7 +269,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&k->slot_key, &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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -281,13 +281,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -299,13 +299,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->slot_key)))
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -601,11 +601,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -620,16 +620,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -637,7 +637,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#156 lore raw

PATCH v6 3/12 luks2: Remove unused argument in grub_error

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
index c390ea3e6..619e57aad 100644
@@ -203,7 +203,7 @@ luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
       grub_json_getstring (&out->size, segment, "size") ||
       grub_json_getstring (&out->encryption, segment, "encryption") ||
       grub_json_getint64 (&out->sector_size, segment, "sector_size"))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters", type);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters");
 
   return GRUB_ERR_NONE;
 }
@@ -231,7 +231,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no segments", type);
+		       "Digest references no segments");
 
   for (i = 0; i < size; i++)
     {
@@ -243,7 +243,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &keyslots))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no keyslots", type);
+		       "Digest references no keyslots");
 
   for (i = 0; i < size; i++)
     {
Signature · 1 line
2.27.0
#158 lore raw

PATCH v6 4/12 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

In reply to Glenn Washburn

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type. Also 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 | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +7 -6
index 473c93976..31b73c535 100644
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  iv[1] = grub_cpu_to_le32 (sector >> 32);
 	  /* 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 >> (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);
+				    & 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 >> 32);
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
+	    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 +5 -0
index f22055f98..29d807f71 100644
@@ -72,6 +72,8 @@
 # endif
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
 #ifndef __CHAR_BIT__
 # error __CHAR_BIT__ is not defined
 #elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((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
#162 lore raw

PATCH v6 5/12 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. And counter-intuitively, grub_disk_get_size returns the total number of device native sectors.

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 619e57aad..0c814bd7c 100644
@@ -426,7 +426,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;
@@ -612,9 +612,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
#166 lore raw

PATCH v6 6/12 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 31b73c535..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 >> 32);
-	  /* 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 aa9877b68..84c3fa73a 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_get_size (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 0c814bd7c..b7ed0642e 100644
@@ -501,7 +501,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
#168 lore raw

PATCH v6 7/12 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

---
 grub-core/disk/luks2.c | 80 ++++++++++++++++++++++++++++++++++++++++--
 include/grub/disk.h    | 16 +++++++++
 2 files changed, 93 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +77 -3
index b7ed0642e..01f9608e5 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
 	{
@@ -616,11 +630,71 @@ 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.slot_key, 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;
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " \"%s\" is not a parsable number\n",
+				     segment.slot_key, 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.slot_key, 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.slot_key);
+	  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.slot_key);
+	  /* 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 +16 -0
index 132a1bb75..1a9e8fcf4 100644
@@ -174,6 +174,22 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 /* Return value of grub_disk_get_size() 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)
+    {
+      /* Round up to the nearest log_sector_size sized sector. */
+      sector += 1ULL << ((log_sector_size / disk->log_sector_size) - 1);
+      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
#173 lore raw

PATCH v6 8/12 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 01f9608e5..f2313cb0f 100644
@@ -626,6 +626,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
+      /* 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.slot_key, 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
#175 lore raw

PATCH v6 9/12 whitespace: convert 8 spaces to tabs.

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index f2313cb0f..c2129f929 100644
@@ -279,8 +279,8 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     {
       if (grub_json_getchild (&digest, &digests, i) ||
 	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
-          grub_json_getchild (&digest, &digest, 0) ||
-          luks2_parse_digest (d, &digest))
+	  grub_json_getchild (&digest, &digest, 0) ||
+	  luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->slot_key)))
@@ -298,7 +298,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
-          luks2_parse_segment (s, &segment))
+	  luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->slot_key)))
@@ -622,7 +622,7 @@ luks2_recover_key (grub_disk_t source,
 	{
 	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
-        }
+	}
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
Signature · 1 line
2.27.0
#177 lore raw

PATCH v6 10/12 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
#181 lore raw

PATCH v6 11/12 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 b7ca6dd58..85a481f0c 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
#183 lore raw

PATCH v6 12/12 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 c2129f929..173a14197 100644
@@ -639,8 +639,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
#147 lore raw

Re: [PATCH v6 01/12] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn
Quoted 5 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used

I prefer if you add "()" to the function names, i.e. luks2_get_keyslot(), in the comments and commit messages. This way it is easier to understand what you mean.

Quoted 12 lines

for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +0 -0
index d96764a02..ab2c31dcd 100644

grub-core/disk/luks2.c typedef struct grub_luks2_header grub_luks2_header_t; · line 68

Quoted 4 lines
grub-core/disk/luks2.c +1 -0
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;

Could you be more consistent and use keyslot instead of slot_key here?

grub-core/disk/luks2.c typedef struct grub_luks2_keyslot grub_luks2_keyslot_t; · line 107

Quoted 8 lines
grub-core/disk/luks2.c +1 -0
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;

Ditto. The code below uses keyslot instead...

Quoted 12 lines
grub-core/disk/luks2.c +1 -0
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 269

Quoted 10 lines
grub-core/disk/luks2.c +1 -2
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||

Daniel

#153 lore raw

Re: [PATCH v6 02/12] luks2: Use more intuitive slot key instead of index in user messages.

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup. Error messages now distinguish between indexes and slot keys. The former include the string "index" in the error string, and the later are surrounded in quotes.

This should be split into two separate patches. One should add "index" to the messages another should add quotes. Well, I am not convinced that quotes are good differentiators... I would drop them or rephrase messages to really differentiate indexes and slot keys.

Quoted 51 lines
---
 grub-core/disk/luks2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
grub-core/disk/luks2.c +5 -5
index ab2c31dcd..c390ea3e6 100644
@@ -269,7 +269,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&k->slot_key, &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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -281,13 +281,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -299,13 +299,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->slot_key)))
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
 
   return GRUB_ERR_NONE;
 }

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

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
@@ -601,11 +601,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);

Quotes are missing?

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

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);

Ditto...

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

Quoted 9 lines
grub-core/disk/luks2.c +1 -2
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -620,16 +620,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",

Ditto and below...

Quoted 15 lines
grub-core/disk/luks2.c +3 -2
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 

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

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
@@ -637,7 +637,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);

Daniel

#159 lore raw

Re: [PATCH v6 04/12] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type. Also add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.

Two separate patches please...

Quoted 45 lines
---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +7 -6
index 473c93976..31b73c535 100644
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  iv[1] = grub_cpu_to_le32 (sector >> 32);
 	  /* 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 >> (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);
+				    & 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 >> 32);
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
+	    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 +0 -0
index f22055f98..29d807f71 100644

include/grub/types.h line 75

Quoted 4 lines
include/grub/types.h +1 -0
@@ -72,6 +72,8 @@
 # endif
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)

This macro should go below "#ifndef __CHAR_BIT__ ...".

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 164

Quoted 8 lines
include/grub/types.h +1 -0
 #ifndef __CHAR_BIT__
 # error __CHAR_BIT__ is not defined
 #elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof (type))(~0)))

(typeof (1ULL)) == (unsigned long long)?

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_U_MIN(type) 0ULL

I am not sure why you cast everything to ULL/unsigned long long. Should not the final cast be to (typeof (type))?

Daniel

#163 lore raw

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

In reply to Glenn Washburn
Quoted 4 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. And counter-intuitively, grub_disk_get_size returns the total number of device native sectors.

Should not we change the name to, e.g., grub_disk_native_sectors() then? Could you do that?

Quoted 3 lines

Also, removed an empty statement.

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#148 lore raw

Re: [PATCH v6 01/12] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Daniel Kiper
Quoted 11 lines · On Wed, 2 Dec 2020 18:01:47 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 6 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used

I prefer if you add "()" to the function names, i.e. luks2_get_keyslot(), in the comments and commit messages. This way it is easier to understand what you mean.

Ok.

Quoted 14 lines
Quoted 14 lines

for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +0 -0
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header
 grub_luks2_header_t;
 
Quoted 5 lines

grub-core/disk/luks2.c typedef struct grub_luks2_header grub_luks2_header_t; · line 68

Quoted 3 lines
grub-core/disk/luks2.c +1 -0
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;

Could you be more consistent and use keyslot instead of slot_key here?

I intentionally chose not to use keyslot because I thought it was confusing. slot_key is not a keyslot in the sense of a slot for a cryptographic key or key material as in the usage keyslot in the struct named "grub_luks2_keyslot". Its the key value of a "slot" in a json associative array which is modeling a sparse array. Perhaps just "key" might be more to your liking?

Quoted 37 lines

grub-core/disk/luks2.c typedef struct grub_luks2_keyslot · line 108

Quoted 9 lines
grub-core/disk/luks2.c +0 -0
   grub_int64_t key_size;
   grub_int64_t priority;

struct

grub-core/disk/luks2.c +1 -0
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot
 grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;

Ditto. The code below uses keyslot instead...

Quoted 13 lines
grub-core/disk/luks2.c +0 -0
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
grub-core/disk/luks2.c +1 -0
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment
 grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here
 */ grub_uint64_t	keyslots;
   grub_uint64_t	segments;

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, · line 269

Quoted 10 lines
grub-core/disk/luks2.c +1 -2
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||

Daniel

#154 lore raw

Re: [PATCH v6 02/12] luks2: Use more intuitive slot key instead of index in user messages.

In reply to Daniel Kiper
Quoted 13 lines · On Wed, 2 Dec 2020 18:23:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 11 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup. Error messages now distinguish between indexes and slot keys. The former include the string "index" in the error string, and the later are surrounded in quotes.

Quoted 4 lines

This should be split into two separate patches. One should add "index" to the messages another should add quotes. Well, I am not convinced that quotes are good differentiators... I would drop them or rephrase messages to really differentiate indexes and slot keys.

I thought the quotes would make more intuitive sense to a user because the value would also be quoted in the json which would be output when using cryptosetup with --debug-json. I can split it in to two patches, but just tell me how you want me to rephrase and I will. No need for another round of reviews.

Quoted 62 lines
Quoted 62 lines
---
 grub-core/disk/luks2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
grub-core/disk/luks2.c +0 -0
index ab2c31dcd..c390ea3e6 100644
@@ -269,7 +269,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,

grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&k->slot_key, &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_idx);
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse
keyslot index %"PRIuGRUB_SIZE, keyslot_idx);

   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
grub-core/disk/luks2.c +0 -0
@@ -281,13 +281,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,

grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&d->slot_key, &digest, NULL) || grub_json_getchild (&digest, &digest, 0) || luks2_parse_digest (d, &digest))

grub-core/disk/luks2.c +0 -1
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

digest %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c +1 -0
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

digest index %"PRIuGRUB_SIZE, i);

       if ((d->keyslots & (1 << k->slot_key)))
grub-core/disk/luks2.c +0 -1
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);

   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +0 -0
@@ -299,13 +299,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,

grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&s->slot_key, &segment, NULL) || grub_json_getchild (&segment, &segment, 0) || luks2_parse_segment (s, &segment))

grub-core/disk/luks2.c +0 -1
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

segment %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c +1 -0
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

segment index %"PRIuGRUB_SIZE, i);

       if ((d->segments & (1 << s->slot_key)))
grub-core/disk/luks2.c +0 -1
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for

digest %"PRIuGRUB_SIZE);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);

   return GRUB_ERR_NONE;

}

grub-core/disk/luks2.c +0 -0
@@ -601,11 +601,11 @@ luks2_recover_key (grub_disk_t source,
 
Quoted 8 lines

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

Quoted 6 lines
grub-core/disk/luks2.c +1 -1
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);

Quotes are missing?

Yep. good catch.

Quoted 40 lines
Quoted 7 lines
grub-core/disk/luks2.c +1 -1
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n",
 i);
+      grub_dprintf ("luks2", "Trying keyslot

%"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);

Ditto...

Quoted 11 lines
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); @@ -620,16 +620,16 @@ luks2_recover_key (grub_disk_t source, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_SIZE" failed: %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_UINT64_T" failed: %s\n",

Ditto and below...

Quoted 16 lines
grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key,

keyslot.key_size); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_SIZE": %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_UINT64_T": %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
Quoted 10 lines
Quoted 8 lines
grub-core/disk/luks2.c +0 -0
@@ -637,7 +637,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element

of an array
* where each element is either empty or holds a key. */

grub-core/disk/luks2.c +1 -1
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"),

keyslot.slot_key);

Daniel

Glenn

#160 lore raw

Re: [PATCH v6 04/12] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

In reply to Daniel Kiper
Quoted 8 lines · On Wed, 2 Dec 2020 18:37:42 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type. Also add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.

Two separate patches please...

Ok.

Quoted 52 lines
Quoted 52 lines
---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 473c93976..31b73c535 100644

grub-core/disk/cryptodisk.c +3 -2
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, iv[1] = grub_cpu_to_le32 (sector >> 32);
 	  /* 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 >> (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)

grub-core/disk/cryptodisk.c +2 -3
-				    & 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 >> 32);
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS

(iv[0]));

grub-core/disk/cryptodisk.c +1 -0
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX

(iv[0])); }

grub-core/disk/cryptodisk.c +1 -1
 	  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,

grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +0 -0
index f22055f98..29d807f71 100644
@@ -72,6 +72,8 @@
 # endif
 #endif
 
Quoted 3 lines

include/grub/types.h line 75

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)

This macro should go below "#ifndef __CHAR_BIT__ ...".

Ack.

Quoted 11 lines

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 9 lines
include/grub/types.h +1 -0
 #ifndef __CHAR_BIT__
 # error __CHAR_BIT__ is not defined
 #elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof
 (type))(~0)))

(typeof (1ULL)) == (unsigned long long)?

I'll change it.

Quoted 4 lines

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_U_MIN(type) 0ULL

I am not sure why you cast everything to ULL/unsigned long long. Should not the final cast be to (typeof (type))?

I think that to as great as an extent as possible macros should behave like functions. So in this case, its "return type" should not change depending on the input arguments. This can be confusing and the source of errors. Sure, you can say that the user of the macro can just do the cast herself, but why add potentially unnecessary steps.

Also, I wanted to unsure that the result was an unsigned type, regardless of signedness of input type. And using the largest unsigned type seemed the best because I don't know how to take an arbitrary integer type can only convert its signedness.

But at this point, I really don't care. Tell me what you want it to be and I'll make it so.

Glenn

#164 lore raw

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

In reply to Daniel Kiper
Quoted 9 lines · On Wed, 2 Dec 2020 18:56:46 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 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. And counter-intuitively, grub_disk_get_size returns the total number of device native sectors.

Should not we change the name to, e.g., grub_disk_native_sectors() then? Could you do that?

Sure, I can do that.

Quoted 7 lines
Quoted 3 lines

Also, removed an empty statement.

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#149 lore raw

Re: [PATCH v6 01/12] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn
Quoted 37 lines · Glenn Washburn
Quoted 11 lines · On Wed, 2 Dec 2020 18:01:47 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 6 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used

I prefer if you add "()" to the function names, i.e. luks2_get_keyslot(), in the comments and commit messages. This way it is easier to understand what you mean.

Ok.

Quoted 14 lines
Quoted 14 lines

for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +0 -0
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header
 grub_luks2_header_t;
 
Quoted 5 lines

grub-core/disk/luks2.c typedef struct grub_luks2_header grub_luks2_header_t; · line 68

Quoted 3 lines
grub-core/disk/luks2.c +1 -0
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;

Could you be more consistent and use keyslot instead of slot_key here?

Quoted 6 lines

I intentionally chose not to use keyslot because I thought it was confusing. slot_key is not a keyslot in the sense of a slot for a cryptographic key or key material as in the usage keyslot in the struct named "grub_luks2_keyslot". Its the key value of a "slot" in a json associative array which is modeling a sparse array. Perhaps just "key" might be more to your liking?

IMO "key" is more confusing in this context and it would require at least a comment to clarify what it means. Is not json_slot_key better then? Probably with some comment what is it too...

Quoted 30 lines
Quoted 30 lines

grub-core/disk/luks2.c typedef struct grub_luks2_keyslot · line 108

Quoted 9 lines
grub-core/disk/luks2.c +0 -0
   grub_int64_t key_size;
   grub_int64_t priority;

struct

grub-core/disk/luks2.c +1 -0
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot
 grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;

Ditto. The code below uses keyslot instead...

Quoted 18 lines
grub-core/disk/luks2.c +0 -0
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
grub-core/disk/luks2.c +1 -0
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment
 grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here
 */ grub_uint64_t	keyslots;
   grub_uint64_t	segments;
grub-core/disk/luks2.c +0 -1
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t idx;
 
Quoted 5 lines
Quoted 5 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 269

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||

Should not keyslot be renamed to json_slot_key too?

Daniel

#155 lore raw

Re: [PATCH v6 02/12] luks2: Use more intuitive slot key instead of index in user messages.

In reply to Glenn Washburn
Quoted 21 lines · Glenn Washburn
Quoted 13 lines · On Wed, 2 Dec 2020 18:23:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 11 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup. Error messages now distinguish between indexes and slot keys. The former include the string "index" in the error string, and the later are surrounded in quotes.

Quoted 4 lines

This should be split into two separate patches. One should add "index" to the messages another should add quotes. Well, I am not convinced that quotes are good differentiators... I would drop them or rephrase messages to really differentiate indexes and slot keys.

Quoted 5 lines

I thought the quotes would make more intuitive sense to a user because the value would also be quoted in the json which would be output when using cryptosetup with --debug-json. I can split it in to two patches, but just tell me how you want me to rephrase and I will. No need for another round of reviews.

OK, let's stick with the quotes then.

Daniel

#161 lore raw

Re: [PATCH v6 04/12] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt

In reply to Glenn Washburn
Quoted 97 lines · Glenn Washburn
Quoted 8 lines · On Wed, 2 Dec 2020 18:37:42 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents. The new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type. Also add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.

Two separate patches please...

Ok.

Quoted 52 lines
Quoted 52 lines
---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 473c93976..31b73c535 100644

grub-core/disk/cryptodisk.c +3 -2
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, iv[1] = grub_cpu_to_le32 (sector >> 32);
 	  /* 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 >> (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)

grub-core/disk/cryptodisk.c +2 -3
-				    & 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 >> 32);
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS

(iv[0]));

grub-core/disk/cryptodisk.c +1 -0
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX

(iv[0])); }

grub-core/disk/cryptodisk.c +1 -1
 	  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,

grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +0 -0
index f22055f98..29d807f71 100644
@@ -72,6 +72,8 @@
 # endif
 #endif
 
Quoted 3 lines

include/grub/types.h line 75

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)

This macro should go below "#ifndef __CHAR_BIT__ ...".

Ack.

Quoted 11 lines

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 9 lines
include/grub/types.h +1 -0
 #ifndef __CHAR_BIT__
 # error __CHAR_BIT__ is not defined
 #elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof
 (type))(~0)))

(typeof (1ULL)) == (unsigned long long)?

I'll change it.

Quoted 4 lines

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_U_MIN(type) 0ULL

I am not sure why you cast everything to ULL/unsigned long long. Should not the final cast be to (typeof (type))?

I think that to as great as an extent as possible macros should behave like functions. So in this case, its "return type" should not change depending on the input arguments. This can be confusing and the source of errors. Sure, you can say that the user of the macro can just do the cast herself, but why add potentially unnecessary steps.

Quoted 7 lines

Also, I wanted to unsure that the result was an unsigned type, regardless of signedness of input type. And using the largest unsigned type seemed the best because I don't know how to take an arbitrary integer type can only convert its signedness.

But at this point, I really don't care. Tell me what you want it to be and I'll make it so.

OK, make sense for me. However, please put short comment before macros why you are casting final result to unsigned long long.

Daniel

#165 lore raw

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

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn
Quoted 7 lines · On Wed, 2 Dec 2020 18:56:46 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 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. And counter-intuitively, grub_disk_get_size returns the total number of device native sectors.

Quoted 4 lines
Quoted 2 lines

Should not we change the name to, e.g., grub_disk_native_sectors() then? Could you do that?

Sure, I can do that.

Thanks!

Daniel

Reviewed-by on v6 6/12
#167 lore raw

Re: [PATCH v6 06/12] cryptodisk: Properly handle non-512 byte sized sectors

In reply to Glenn Washburn
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 7 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.

Daniel

#169 lore raw

Re: [PATCH v6 07/12] luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn
Quoted 76 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

---
 grub-core/disk/luks2.c | 80 ++++++++++++++++++++++++++++++++++++++++--
 include/grub/disk.h    | 16 +++++++++
 2 files changed, 93 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +29 -0
index b7ed0642e..01f9608e5 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
 	{
@@ -616,11 +630,71 @@ 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.slot_key, crypt->offset_sectors,
+				 max_crypt_sectors);
+	  continue;
+	}

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

Quoted 10 lines
grub-core/disk/luks2.c +5 -3
+
       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;
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;

I think ">>" should not happen here...

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

Quoted 2 lines
grub-core/disk/luks2.c +2 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

It should happen here... Or to be exact...
crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 << crypt->log_sector_size); // Am I right? crypt->total_sectors >>= crypt->log_sector_size;

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

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)

Missing space before "("...

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

Quoted 5 lines
grub-core/disk/luks2.c +5 -0
+	    {
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " \"%s\" is not a parsable number\n",
+				     segment.slot_key, segment.size);

crypt->total_sectors = max_crypt_sectors; ?

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

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	      continue;

...and then drop continue?

Quoted 45 lines
grub-core/disk/luks2.c +34 -0
+	    }
+	  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.slot_key, 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.slot_key);
+	  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.slot_key);
+	  /* 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 +0 -0
index 132a1bb75..1a9e8fcf4 100644
@@ -174,6 +174,22 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 /* Return value of grub_disk_get_size() 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 186

Quoted 10 lines
include/grub/disk.h +10 -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)
+    {
+      /* Round up to the nearest log_sector_size sized sector. */
+      sector += 1ULL << ((log_sector_size / disk->log_sector_size) - 1);

ALIGN_UP()?

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

Quoted 9 lines
include/grub/disk.h +6 -0
+      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)

Daniel

#178 lore raw

Re: [PATCH v6 10/12] mips: Enable __clzdi2()

In reply to Glenn Washburn
Quoted 5 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.

This should be replaced with:

In general if somebody suggests you something and you use they idea then you should use Suggested-by instead of SOB.

Anyway, I can fix it before committing the patch if you are OK with me adding your SOB.

Daniel

#179 lore raw

Re: [PATCH v6 10/12] mips: Enable __clzdi2()

In reply to Daniel Kiper
Quoted 9 lines · On Thu, 3 Dec 2020 15:00:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 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.

This should be replaced with:

Quoted 8 lines

In general if somebody suggests you something and you use they idea then you should use Suggested-by instead of SOB.

Anyway, I can fix it before committing the patch if you are OK with me adding your SOB.

Sure, I'm fine with you adding my SOB. I don't know if you're not taking finished patches that come after unfinished patches, so I was just going to fix and include in my next iteration. If you're ready to take these patches, then go ahead and I'll have less patches in the v7 series.

Glenn

#170 lore raw

Re: [PATCH v6 07/12] luks2: Better error handling when setting up the cryptodisk

In reply to Daniel Kiper
Quoted 98 lines · On Thu, 3 Dec 2020 14:31:49 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 97 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +3 -0
index b7ed0642e..01f9608e5 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,

i); 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
 	{
@@ -616,11 +630,71 @@ 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.slot_key,

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

Quoted 8 lines
Quoted 6 lines
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;
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

I think ">>" should not happen here...

Do you think this because ">>" should not operate on a value returned by a call to grub_strtoull that has errored? I don't think there's any problem because the return value is a number, so there's no harm in using ">>" on a garbage number to get another garbage number, as long as we don't use the value. (Its also not technically a garbage number, just one of two values in the error case)

Also I didn't want to set total_sectors to grub_strtoull because it could be a little confusing since the return value of grub_strtoull is bytes not sectors. And yet storing in total_sectors. I could use a local variable, but was wanting to avoid that. But I'm fine with this suggestion.

Quoted 7 lines

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

Quoted 2 lines
grub-core/disk/luks2.c +2 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

It should happen here... Or to be exact...
crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 <<
crypt->log_sector_size); // Am I right? crypt->total_sectors >>= crypt->log_sector_size;

I don't think we need to decrypt partial blocks at the end. Cryptsetup/dm-crypt enforces disk sizes that are multiples of the sector size. So if the size is not a multiple of blocksize, there's a bug somewhere in cryptsetup/dm-crypt, and we don't need to deal with the partial block. At worse the use loses access to a partial block at the end that they should never have had access to in the first place.

Quoted 3 lines

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

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)

Missing space before "("...

Ack.

Quoted 11 lines
Quoted 9 lines
grub-core/disk/luks2.c +2 -0
+	    {
+	      /* TODO: Unparsable number-string, try to use the

whole disk */

grub-core/disk/luks2.c +1 -0
+	      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.slot_key,

segment.size);

crypt->total_sectors = max_crypt_sectors; ?

Sure, I didn't think you'd want this since it potentially entails trying to read past the end of the luks data, and thus getting random data, which might crash (buggy) filesystem read code.

Quoted 61 lines

grub-core/disk/luks2.c

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	      continue;

...and then drop continue?

Quoted 57 lines
grub-core/disk/luks2.c +5 -0
+	    }
+	  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

device will be"

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

segment.size);

grub-core/disk/luks2.c +8 -0
+	      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"

grub-core/disk/luks2.c +5 -0
+				 " sectors, skipping\n",
+				 segment.slot_key);
+	  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.slot_key);
+	  /* 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 132a1bb75..1a9e8fcf4 100644

include/grub/disk.h +0 -0
@@ -174,6 +174,22 @@ typedef struct grub_disk_memberlist
 *grub_disk_memberlist_t; /* Return value of grub_disk_get_size() in

case disk size is unknown. */ #define GRUB_DISK_SIZE_UNKNOWN
0xffffffffffffffffULL

Quoted 13 lines

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

Quoted 11 lines

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

include/grub/disk.h +8 -1
+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)
+    {
+      /* Round up to the nearest log_sector_size sized sector. */
+      sector += 1ULL << ((log_sector_size / disk->log_sector_size)
- 1);

ALIGN_UP()?

Sure, I can do that and then remove the comment.

Quoted 12 lines
Quoted 10 lines
include/grub/disk.h +6 -0
+      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)

Daniel

#150 lore raw

Re: [PATCH v6 01/12] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Daniel Kiper
Quoted 46 lines · On Thu, 3 Dec 2020 13:35:28 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 38 lines · Glenn Washburn
Quoted 11 lines · On Wed, 2 Dec 2020 18:01:47 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 6 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used

I prefer if you add "()" to the function names, i.e. luks2_get_keyslot(), in the comments and commit messages. This way it is easier to understand what you mean.

Ok.

Quoted 14 lines
Quoted 14 lines

for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +0 -0
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header
 grub_luks2_header_t;
 
Quoted 6 lines

grub-core/disk/luks2.c typedef struct grub_luks2_header grub_luks2_header_t; · line 68

Quoted 3 lines
grub-core/disk/luks2.c +1 -0
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;

Could you be more consistent and use keyslot instead of slot_key here?

Quoted 6 lines

I intentionally chose not to use keyslot because I thought it was confusing. slot_key is not a keyslot in the sense of a slot for a cryptographic key or key material as in the usage keyslot in the struct named "grub_luks2_keyslot". Its the key value of a "slot" in a json associative array which is modeling a sparse array. Perhaps just "key" might be more to your liking?

Quoted 3 lines

IMO "key" is more confusing in this context and it would require at least a comment to clarify what it means. Is not json_slot_key better then? Probably with some comment what is it too...

Yeah, that's a better name in terms of its descriptive power, but feels kinda long. I'm fine with using that though, and will use that for the next patch series, unless I hear otherwise.

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

grub-core/disk/luks2.c typedef struct grub_luks2_keyslot · line 108

Quoted 9 lines
grub-core/disk/luks2.c +0 -0
   grub_int64_t key_size;
   grub_int64_t priority;

struct

grub-core/disk/luks2.c +1 -0
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot
 grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;

Ditto. The code below uses keyslot instead...

Quoted 18 lines
grub-core/disk/luks2.c +0 -0
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
grub-core/disk/luks2.c +1 -0
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment
 grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields
 here */ grub_uint64_t	keyslots;
   grub_uint64_t	segments;
grub-core/disk/luks2.c +0 -1
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t
 *k, grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t idx;
 
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 269

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||

Should not keyslot be renamed to json_slot_key too?

If I understand you correctly, you're asking about the variable keyslot of type grub_json_t. Here keyslot is a json object representing a keyslot (ie collection of parameters used to decrypt an encrypted key). It is not what I call a "slot key", which is the value of a key in a json associative array. So keyslot is a json object, where as a slot key is always a json string (afaik). I believe that it is currently appropriately named.

Glenn

#180 lore raw

Re: [PATCH v6 10/12] mips: Enable __clzdi2()

In reply to Glenn Washburn
Quoted 18 lines · Glenn Washburn
Quoted 15 lines · On Thu, 3 Dec 2020 15:00:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 5 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.

This should be replaced with:

In general if somebody suggests you something and you use they idea then you should use Suggested-by instead of SOB.

Quoted 8 lines
Quoted 2 lines

Anyway, I can fix it before committing the patch if you are OK with me adding your SOB.

Sure, I'm fine with you adding my SOB. I don't know if you're not taking finished patches that come after unfinished patches, so I was just going to fix and include in my next iteration. If you're ready to take these patches, then go ahead and I'll have less patches in the v7 series.

Usually I try to take the patches which got my RB. I am going to take your patches together with many others next week. If you want to repost v7 before that go ahead. Just add my RB to the patches which got one.

Daniel

#171 lore raw

Re: [PATCH v6 07/12] luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn
Quoted 117 lines · Glenn Washburn
Quoted 98 lines · On Thu, 3 Dec 2020 14:31:49 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 97 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +3 -0
index b7ed0642e..01f9608e5 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,

i); 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
 	{
@@ -616,11 +630,71 @@ 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.slot_key,

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

Quoted 8 lines
Quoted 6 lines
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;
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

I think ">>" should not happen here...

Do you think this because ">>" should not operate on a value returned by a call to grub_strtoull that has errored? I don't think there's any problem because the return value is a number, so there's no harm in using ">>" on a garbage number to get another garbage number, as long as we don't use the value. (Its also not technically a garbage number, just one of two values in the error case)

Quoted 5 lines

Also I didn't want to set total_sectors to grub_strtoull because it could be a little confusing since the return value of grub_strtoull is bytes not sectors. And yet storing in total_sectors. I could use a local variable, but was wanting to avoid that. But I'm fine with this suggestion.

OK, let's leave it as it is...

Quoted 8 lines
Quoted 7 lines

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

Quoted 2 lines
grub-core/disk/luks2.c +2 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

It should happen here... Or to be exact...
crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 <<
crypt->log_sector_size); // Am I right? crypt->total_sectors >>= crypt->log_sector_size;

Quoted 6 lines

I don't think we need to decrypt partial blocks at the end. Cryptsetup/dm-crypt enforces disk sizes that are multiples of the sector size. So if the size is not a multiple of blocksize, there's a bug somewhere in cryptsetup/dm-crypt, and we don't need to deal with the partial block. At worse the use loses access to a partial block at the end that they should never have had access to in the first place.

Does GRUB fail safely in such cases?

Quoted 18 lines
Quoted 3 lines

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

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)

Missing space before "("...

Ack.

Quoted 11 lines
Quoted 9 lines
grub-core/disk/luks2.c +2 -0
+	    {
+	      /* TODO: Unparsable number-string, try to use the

whole disk */

grub-core/disk/luks2.c +1 -0
+	      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.slot_key,

segment.size);

crypt->total_sectors = max_crypt_sectors; ?

Quoted 3 lines

Sure, I didn't think you'd want this since it potentially entails trying to read past the end of the luks data, and thus getting random data, which might crash (buggy) filesystem read code.

OK but then I would drop "TODO" comment because we do not want to make this case work...

Daniel

#151 lore raw

Re: [PATCH v6 01/12] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn
Quoted 49 lines · Glenn Washburn
Quoted 46 lines · On Thu, 3 Dec 2020 13:35:28 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 44 lines · Glenn Washburn
Quoted 11 lines · On Wed, 2 Dec 2020 18:01:47 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 6 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot which was overloaded to be used

I prefer if you add "()" to the function names, i.e. luks2_get_keyslot(), in the comments and commit messages. This way it is easier to understand what you mean.

Ok.

Quoted 14 lines
Quoted 14 lines

for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +0 -0
index d96764a02..ab2c31dcd 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header
 grub_luks2_header_t;
 
Quoted 6 lines

grub-core/disk/luks2.c typedef struct grub_luks2_header grub_luks2_header_t; · line 68

Quoted 3 lines
grub-core/disk/luks2.c +1 -0
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;

Could you be more consistent and use keyslot instead of slot_key here?

I intentionally chose not to use keyslot because I thought it was confusing. slot_key is not a keyslot in the sense of a slot for a cryptographic key or key material as in the usage keyslot in the struct named "grub_luks2_keyslot". Its the key value of a "slot" in a json associative array which is modeling a sparse array. Perhaps just "key" might be more to your liking?

Quoted 7 lines
Quoted 3 lines

IMO "key" is more confusing in this context and it would require at least a comment to clarify what it means. Is not json_slot_key better then? Probably with some comment what is it too...

Yeah, that's a better name in terms of its descriptive power, but feels kinda long. I'm fine with using that though, and will use that for the next patch series, unless I hear otherwise.

Yeah, unfortunately world is not perfect... :-(

Quoted 38 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

grub-core/disk/luks2.c typedef struct grub_luks2_keyslot · line 108

Quoted 9 lines
grub-core/disk/luks2.c +0 -0
   grub_int64_t key_size;
   grub_int64_t priority;

struct

grub-core/disk/luks2.c +1 -0
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot
 grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;

Ditto. The code below uses keyslot instead...

Quoted 18 lines
grub-core/disk/luks2.c +0 -0
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
grub-core/disk/luks2.c +1 -0
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment
 grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields
 here */ grub_uint64_t	keyslots;
   grub_uint64_t	segments;
grub-core/disk/luks2.c +0 -1
@@ -259,12 +262,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t
 *k, grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t idx;
 
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 269

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||

Should not keyslot be renamed to json_slot_key too?

Quoted 7 lines

If I understand you correctly, you're asking about the variable keyslot of type grub_json_t. Here keyslot is a json object representing a keyslot (ie collection of parameters used to decrypt an encrypted key). It is not what I call a "slot key", which is the value of a key in a json associative array. So keyslot is a json object, where as a slot key is always a json string (afaik). I believe that it is currently appropriately named.

OK, so, leave it as it is right now.

Daniel

#172 lore raw

Re: [PATCH v6 07/12] luks2: Better error handling when setting up the cryptodisk

In reply to Daniel Kiper
Quoted 145 lines · On Fri, 4 Dec 2020 14:19:29 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 120 lines · Glenn Washburn
Quoted 100 lines · On Thu, 3 Dec 2020 14:31:49 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 99 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +3 -0
index b7ed0642e..01f9608e5 100644
@@ -597,12 +597,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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment,

json, i); 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
 	{
@@ -616,11 +630,71 @@ 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.slot_key,

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

Quoted 8 lines
Quoted 6 lines
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;
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

I think ">>" should not happen here...

Do you think this because ">>" should not operate on a value returned by a call to grub_strtoull that has errored? I don't think there's any problem because the return value is a number, so there's no harm in using ">>" on a garbage number to get another garbage number, as long as we don't use the value. (Its also not technically a garbage number, just one of two values in the error case)

Quoted 5 lines

Also I didn't want to set total_sectors to grub_strtoull because it could be a little confusing since the return value of grub_strtoull is bytes not sectors. And yet storing in total_sectors. I could use a local variable, but was wanting to avoid that. But I'm fine with this suggestion.

OK, let's leave it as it is...

Quoted 8 lines
Quoted 7 lines

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

Quoted 2 lines
grub-core/disk/luks2.c +2 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

It should happen here... Or to be exact...
crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 <<
crypt->log_sector_size); // Am I right? crypt->total_sectors >>= crypt->log_sector_size;

Quoted 7 lines

I don't think we need to decrypt partial blocks at the end. Cryptsetup/dm-crypt enforces disk sizes that are multiples of the sector size. So if the size is not a multiple of blocksize, there's a bug somewhere in cryptsetup/dm-crypt, and we don't need to deal with the partial block. At worse the use loses access to a partial block at the end that they should never have had access to in the first place.

Quoted 1 line

Does GRUB fail safely in such cases?

I think the question boils down to the filesystem driver code. On second thought, I'll take the suggestion to round up to nearest sector.

Quoted 18 lines
Quoted 18 lines
Quoted 3 lines

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

Quoted 1 line
grub-core/disk/luks2.c +1 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)

Missing space before "("...

Ack.

Quoted 11 lines
Quoted 9 lines
grub-core/disk/luks2.c +2 -0
+	    {
+	      /* TODO: Unparsable number-string, try to use the

whole disk */

grub-core/disk/luks2.c +1 -0
+	      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.slot_key,

segment.size);

crypt->total_sectors = max_crypt_sectors; ?

Quoted 6 lines
Quoted 3 lines

Sure, I didn't think you'd want this since it potentially entails trying to read past the end of the luks data, and thus getting random data, which might crash (buggy) filesystem read code.

OK but then I would drop "TODO" comment because we do not want to make this case work...

Ideally I think we do want to make this case work, which would entail being confident enough that the file system code won't horribly break. We may actually be at that point, I just don't know the various filesystem drivers well enough to know either way. If we had a setup to run a filesystem fuzzer over it that might help confidence levels.

Since I think its pretty improbable that there's an unparsable segment.size but we can get all other needed params to unlock the drive, I'll drop the todo.

Glenn

#224 lore raw

PATCH v7 12/17 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

---
 grub-core/disk/luks2.c | 98 +++++++++++++++++++++++++++++++++++++-----
 include/grub/disk.h    | 17 ++++++++
 2 files changed, 105 insertions(+), 10 deletions(-)
grub-core/disk/luks2.c +88 -10
index de2e70796..1bb3a333d 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -600,37 +600,115 @@ 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 (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
       /* 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;
+      /* 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.json_slot_key, 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.json_slot_key, 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.json_slot_key, 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.json_slot_key);
+	  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.json_slot_key);
+	  /* 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));
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -638,7 +716,7 @@ luks2_recover_key (grub_disk_t source,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -646,7 +724,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.json_slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
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
#222 lore raw

PATCH v7 11/17 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 bdf40768b..de2e70796 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
#233 lore raw

PATCH v7 15/17 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
#197 lore raw

PATCH v7 4/17 luks2: Make sure all fields of output argument in luks2_parse_digest() are written to

In reply to Glenn Washburn

We should assume that the output argument "out" is uninitialized and could have random data. So, make sure to initialize the segments and keyslots bit fields because potentially not all bits of those fields are written to. Otherwise, the digest could say it belongs to keyslots and segments that it does not.

---
 grub-core/disk/luks2.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/disk/luks2.c +2 -0
index bdb90e4b6..eadd529e9 100644
@@ -230,6 +230,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no segments");
 
+  out->segments = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &segments, i) ||
@@ -242,6 +243,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no keyslots");
 
+  out->keyslots = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &keyslots, i) ||
Signature · 1 line
2.27.0
#216 lore raw

PATCH v7 9/17 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..0542011cc 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
#210 lore raw

PATCH v7 7/17 luks2: Add string "index" to user strings using a json index.

In reply to 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.

---
 grub-core/disk/luks2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/luks2.c +2 -2
index ea1065bcf..4be40b22b 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->json_slot_key, &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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -302,7 +302,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->json_slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->json_slot_key)))
 	break;
Signature · 1 line
2.27.0
#185 lore raw

v7 cover Cryptodisk fixes for v2.06 redux

In reply to Glenn Washburn

This patch series is an update to reflect changes suggested in v6. Of note, there are a few new patches: 01, 02, and 04.

01: Daniel suggested to renae grub_disk_get_size to grub_disk_native_sectors 02: Make ALIGN_UP and ALIGN_DOWN safer
04: Make luks2_parse_digest() safer

Glenn

Glenn Washburn (17):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add json_slot_key member to struct
grub_luks2_keyslot/segment/digest
luks2: Use more intuitive slot key instead of index in user messages
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
whitespace: convert 8 spaces to tabs
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

 grub-core/disk/cryptodisk.c        |  64 +++++++-----
 grub-core/disk/diskfilter.c        |  12 +--
 grub-core/disk/dmraid_nvidia.c     |   2 +-
 grub-core/disk/efi/efidisk.c       |   2 +-
 grub-core/disk/geli.c              |   6 +-
 grub-core/disk/ldm.c               |   4 +-
 grub-core/disk/luks.c              |   7 +-
 grub-core/disk/luks2.c             | 160 +++++++++++++++++++++++------
 grub-core/disk/mdraid1x_linux.c    |   2 +-
 grub-core/disk/mdraid_linux.c      |   2 +-
 grub-core/fs/cbfs.c                |  16 +--
 grub-core/fs/nilfs2.c              |   2 +-
 grub-core/fs/zfs/zfs.c             |   4 +-
 grub-core/kern/compiler-rt.c       |   2 +-
 grub-core/kern/disk.c              |   2 +-
 grub-core/kern/mips/arc/init.c     |   2 +-
 grub-core/normal/misc.c            |   6 +-
 grub-core/osdep/windows/platform.c |   2 +-
 include/grub/compiler-rt.h         |   2 +-
 include/grub/cryptodisk.h          |   8 +-
 include/grub/disk.h                |  21 +++-
 include/grub/misc.h                |   7 +-
 include/grub/types.h               |   9 ++
 util/grub-install.c                |   2 +-
 util/grub-probe.c                  |   2 +-
 25 files changed, 249 insertions(+), 99 deletions(-)

Range-diff against v6:

 -:  --------- >  1:  7e79d6fb1 disk: Rename grub_disk_get_size to grub_disk_native_sectors
 -:  --------- >  2:  77f9671d5 misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
 3:  8527be145 !  3:  d1a36aa79 luks2: Remove unused argument in grub_error

@@ Metadata
## Commit message ##
luks2: Remove unused argument in grub_error

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
grub_json_getstring (&out->size, segment, "size") ||

 -:  --------- >  4:  dab43e033 luks2: Make sure all fields of output argument in luks2_parse_digest() are written to
 1:  6262aefe9 !  5:  35f47644c luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
    +    luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest
     
         This allows code using these structs to know the named key associated with
         these json data structures. In the future we can use these to provide better
         error messages to the user.
     
    -    Get rid of idx variable in luks2_get_keyslot which was overloaded to be used
    -    for both keyslot and segment slot keys.
    +    Get rid of idx variable in luks2_get_keyslot() which was overloaded to be
    +    used for both keyslot and segment slot keys.
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
      
      struct grub_luks2_keyslot
      {
    -+  grub_uint64_t slot_key;
    ++  /* The integer key to the associative array of keyslots */
    ++  grub_uint64_t json_slot_key;
        grub_int64_t key_size;
        grub_int64_t priority;
        struct
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
      
      struct grub_luks2_segment
      {
    -+  grub_uint64_t slot_key;
    ++  grub_uint64_t json_slot_key;
        grub_uint64_t offset;
        const char	*size;
        const char	*encryption;
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_segment grub_luks2_segment_t;
      
      struct grub_luks2_digest
      {
    -+  grub_uint64_t slot_key;
    ++  grub_uint64_t json_slot_key;
        /* Both keyslots and segments are interpreted as bitfields here */
        grub_uint64_t	keyslots;
        grub_uint64_t	segments;
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        if (grub_json_getvalue (&keyslots, root, "keyslots") ||
            grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
     -      grub_json_getuint64 (&idx, &keyslot, NULL) ||
    -+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
    ++      grub_json_getuint64 (&k->json_slot_key, &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_idx);
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        for (i = 0; i < size; i++)
          {
            if (grub_json_getchild (&digest, &digests, i) ||
    -+	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
    ++	  grub_json_getuint64 (&d->json_slot_key, &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, i);
      
     -      if ((d->keyslots & (1 << idx)))
    -+      if ((d->keyslots & (1 << k->slot_key)))
    ++      if ((d->keyslots & (1 << k->json_slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
          {
            if (grub_json_getchild (&segment, &segments, i) ||
     -	  grub_json_getuint64 (&idx, &segment, NULL) ||
    -+	  grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
    ++	  grub_json_getuint64 (&s->json_slot_key, &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, i);
      
     -      if ((d->segments & (1 << idx)))
    -+      if ((d->segments & (1 << s->slot_key)))
    ++      if ((d->segments & (1 << s->json_slot_key)))
      	break;
          }
        if (i == size)
 2:  3cd52834b !  6:  e52887944 luks2: Use more intuitive slot key instead of index in user messages.
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Use more intuitive slot key instead of index in user messages.
    +    luks2: Use more intuitive slot key instead of index in user messages
     
         Use the slot key name in the json array rather than the 0 based index in the
         json array for keyslots, segments, and digests. This is less confusing for
    @@ Commit message
         messages using the index of the keyslot will mention keyslot 1 (its a
         zero-based index). Furthermore, with this change the keyslot number will
         align with the number used to reference the keyslot when using the
    -    --key-slot argument to cryptsetup. Error messages now distinguish between
    -    indexes and slot keys. The former include the string "index" in the error
    -    string, and the later are surrounded in quotes.
    +    --key-slot argument to cryptsetup.
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    -       grub_json_getuint64 (&k->slot_key, &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_idx);
    -+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
    - 
    -   /* Get digest that matches the keyslot. */
    -   if (grub_json_getvalue (&digests, root, "digests") ||
    -@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
    + 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
     +	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
      
    -       if ((d->keyslots & (1 << k->slot_key)))
    +       if ((d->keyslots & (1 << k->json_slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        /* Get segment that matches the digest. */
        if (grub_json_getvalue (&segments, root, "segments") ||
     @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	  grub_json_getuint64 (&s->slot_key, &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, i);
    -+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
    - 
    -       if ((d->segments & (1 << s->slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
      	  continue;
              }
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      	{
     -	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
    -+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
    ++	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
     +			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      	{
     -	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
    -+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
    ++	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
     +			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
    ++      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
      
            candidate_key_len = keyslot.key_size;
            break;
 4:  1a248b679 <  -:  --------- cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt
 -:  --------- >  7:  43e3b6cce luks2: Add string "index" to user strings using a json index.
 -:  --------- >  8:  4aa9757e1 cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
 -:  --------- >  9:  d18993a63 cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
 5:  45f5d644f ! 10:  7e8f242d1 luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

@@ Commit message

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. And counter-intuitively, grub_disk_get_size
- returns the total number of device native sectors.
+ to cryptodisk sectors as well.

Also, removed an empty statement.

- Signed-off-by: Glenn Washburn <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
6: 6e01cafb1 ! 11: 1fe9c38ed cryptodisk: Properly handle non-512 byte sized sectors
@@ Commit message
512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/cryptodisk.c ##
@@ grub-core/disk/cryptodisk.c: lrw_xor (const struct lrw_sector *sec,
static gcry_err_code_t
@@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
}
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 & GRUB_TYPE_U_MAX (iv[0]));
@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu
newdev->source_disk = NULL;
- newdev->log_sector_size = 9;
+ newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
- newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
+ newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
newdev->modname = "luks";
@@ grub-core/disk/luks.c: luks_recover_key (grub_disk_t source,
7: d8927c6bc ! 12: c9db343e8 luks2: Better error handling when setting up the cryptodisk

    @@ Commit message
         an invalid segment, and continue on to the next key.
     
      ## grub-core/disk/luks2.c ##
    +@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    + 	break;
    +     }
    +   if (i == size)
    +-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
    ++      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key);
    + 
    +   /* Get segment that matches the digest. */
    +   if (grub_json_getvalue (&segments, root, "segments") ||
    +@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    + 	break;
    +     }
    +   if (i == size)
    +-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
    ++    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key);
    + 
    +   return GRUB_ERR_NONE;
    + }
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            goto err;
          }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
            if (keyslot.priority == 0)
      	{
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +-	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
    + 	  continue;
    +         }
    + 
    +-      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
    + 
    +       /* 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;
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " %"PRIuGRUB_UINT64_T" which is greater than"
     +				 " source disk size %"PRIuGRUB_UINT64_T","
     +				 " skipping\n",
    -+				 segment.slot_key, crypt->offset_sectors,
    ++				 segment.json_slot_key, crypt->offset_sectors,
     +				 max_crypt_sectors);
     +	  continue;
     +	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     -	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
     +	{
     +	  grub_errno = GRUB_ERR_NONE;
    -+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
    ++	  /* 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)
    ++	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
     +	    {
    -+	      /* TODO: Unparsable number-string, try to use the whole disk */
     +	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
     +				     " \"%s\" is not a parsable number\n",
    -+				     segment.slot_key, segment.size);
    ++				     segment.json_slot_key, segment.size);
     +	      continue;
     +	    }
    -+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
    ++	  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: luks2_recover_key (grub_disk_t source,
     +				     " %s overflowed 64-bit unsigned integer,"
     +				     " the end of the crypto device will be"
     +				     " inaccessible\n",
    -+				     segment.slot_key, segment.size);
    ++				     segment.json_slot_key, segment.size);
     +	      if (crypt->total_sectors > max_crypt_sectors)
     +		crypt->total_sectors = max_crypt_sectors;
     +	    }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	{
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has zero"
     +				 " sectors, skipping\n",
    -+				 segment.slot_key);
    ++				 segment.json_slot_key);
     +	  continue;
     +	}
     +      else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " data position greater than source disk size,"
     +				 " the end of the crypto device will be"
     +				 " inaccessible\n",
    -+				 segment.slot_key);
    ++				 segment.json_slot_key);
     +	  /* 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));
    +       if (ret)
    + 	{
    + 	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
    +-			keyslot.slot_key, grub_errmsg);
    ++			keyslot.json_slot_key, grub_errmsg);
    + 	  continue;
    + 	}
    + 
    +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +       if (ret)
    + 	{
    + 	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
    +-			keyslot.slot_key, grub_errmsg);
    ++			keyslot.json_slot_key, grub_errmsg);
    + 	  continue;
    + 	}
    + 
    +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +        * TRANSLATORS: It's a cryptographic key slot: one element of an array
    +        * where each element is either empty or holds a key.
    +        */
    +-      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
    ++      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.json_slot_key);
    + 
    +       candidate_key_len = keyslot.key_size;
    +       break;
     
      ## include/grub/disk.h ##
    +@@
    + #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: typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
    - /* Return value of grub_disk_get_size() in case disk size is unknown. */
    + /* 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. */
    @@ include/grub/disk.h: typedef struct grub_disk_memberlist *grub_disk_memberlist_t
     +{
     +  if (disk->log_sector_size < log_sector_size)
     +    {
    -+      /* Round up to the nearest log_sector_size sized sector. */
    -+      sector += 1ULL << ((log_sector_size / disk->log_sector_size) - 1);
    ++      sector = ALIGN_UP (sector, 1 << (log_sector_size / disk->log_sector_size));
     +      return sector >> (log_sector_size - disk->log_sector_size);
     +    }
     +  else
 8:  fcd7aadb7 ! 13:  2fcef44b6 luks2: Error check segment.sector_size
    @@ Metadata
      ## Commit message ##
         luks2: Error check segment.sector_size
     
    +    Reviewed-by: Daniel Kiper <[email protected]>
    +
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
    -       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
    +       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
      
     +      /* Sector size should be one of 512, 1024, 2048, or 4096. */
     +      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
     +				 " size %"PRIuGRUB_UINT64_T" is not one of"
     +				 " 512, 1024, 2048, or 4096\n",
    -+				 segment.slot_key, segment.sector_size);
    ++				 segment.json_slot_key, segment.sector_size);
     +	  continue;
     +	}
     +

9: 61f77a1a8 ! 14: 74eed4a62 whitespace: convert 8 spaces to tabs.
@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- whitespace: convert 8 spaces to tabs.
+ whitespace: convert 8 spaces to tabs
+
+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
{
if (grub_json_getchild (&digest, &digests, i) ||
- grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
+ grub_json_getuint64 (&d->json_slot_key, &digest, NULL) ||
- grub_json_getchild (&digest, &digest, 0) ||
- luks2_parse_digest (d, &digest))
+ grub_json_getchild (&digest, &digest, 0) ||
+ luks2_parse_digest (d, &digest))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

- if ((d->keyslots & (1 << k->slot_key)))
+ if ((d->keyslots & (1 << k->json_slot_key)))
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
if (grub_json_getchild (&segment, &segments, i) ||
- grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
+ grub_json_getuint64 (&s->json_slot_key, &segment, NULL) ||
grub_json_getchild (&segment, &segment, 0) ||
- luks2_parse_segment (s, &segment))
+ luks2_parse_segment (s, &segment))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);

- if ((d->segments & (1 << s->slot_key)))
+ if ((d->segments & (1 << s->json_slot_key)))
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
{
- grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
continue;
- }
+ }

- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);

10: d71d26701 ! 15: 51c2e9160 mips: Enable __clzdi2()
@@ Commit message
__clzdi2()) but for MIPS target and __clzdi2 only, __clzsi2 was already enabled.

- Signed-off-by: Daniel Kiper <[email protected]>
+ Suggested-by: Daniel Kiper <[email protected]>

## grub-core/kern/compiler-rt.c ##
@@ grub-core/kern/compiler-rt.c: __clzsi2 (grub_uint32_t val)
11: 8aa295f77 ! 16: 3b4ccda1f misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
@@ Metadata
## Commit message ##
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## include/grub/misc.h ##
@@ include/grub/misc.h: void EXPORT_FUNC(grub_real_boot_time) (const char *file,
#define grub_max(a, b) (((a) > (b)) ? (a) : (b))
12: 7050a4ace ! 17: 8b97e6887 luks2: Use grub_log2ull to calculate log_sector_size and improve readability
@@ Metadata
## Commit message ##
luks2: Use grub_log2ull to calculate log_sector_size and improve readability

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,

Signature · 1 line
2.27.0
#220 lore raw

PATCH v7 10/17 luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

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 4be40b22b..bdf40768b 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
#186 lore raw

PATCH v7 1/17 disk: Rename grub_disk_get_size to grub_disk_native_sectors

In reply to Glenn Washburn

The function grub_disk_get_size is confusingly named because it actually returns a sector count where the sectors are sized in the grub native sector size. Rename to something more appropriate.

---
 grub-core/disk/diskfilter.c        | 12 ++++++------
 grub-core/disk/dmraid_nvidia.c     |  2 +-
 grub-core/disk/efi/efidisk.c       |  2 +-
 grub-core/disk/geli.c              |  6 +++---
 grub-core/disk/ldm.c               |  4 ++--
 grub-core/disk/luks.c              |  2 +-
 grub-core/disk/mdraid1x_linux.c    |  2 +-
 grub-core/disk/mdraid_linux.c      |  2 +-
 grub-core/fs/cbfs.c                | 16 ++++++++--------
 grub-core/fs/nilfs2.c              |  2 +-
 grub-core/fs/zfs/zfs.c             |  4 ++--
 grub-core/kern/disk.c              |  2 +-
 grub-core/kern/mips/arc/init.c     |  2 +-
 grub-core/normal/misc.c            |  6 +++---
 grub-core/osdep/windows/platform.c |  2 +-
 include/grub/disk.h                |  4 ++--
 util/grub-install.c                |  2 +-
 util/grub-probe.c                  |  2 +-
 18 files changed, 37 insertions(+), 37 deletions(-)
grub-core/disk/diskfilter.c +6 -6
index 86557f923..032011566 100644
@@ -148,7 +148,7 @@ scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
 	if (m->disk && m->disk->id == disk->id
 	    && m->disk->dev->id == disk->dev->id
 	    && m->part_start == grub_partition_get_start (disk->partition)
-	    && m->part_size == grub_disk_get_size (disk))
+	    && m->part_size == grub_disk_native_sectors (disk))
 	  return 0;
     }
 
@@ -1190,13 +1190,13 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 
   grub_dprintf ("diskfilter", "Inserting %s (+%lld,%lld) into %s (%s)\n", disk->name,
 		(unsigned long long) grub_partition_get_start (disk->partition),
-		(unsigned long long) grub_disk_get_size (disk),
+		(unsigned long long) grub_disk_native_sectors (disk),
 		array->name, diskfilter->name);
 #ifdef GRUB_UTIL
   grub_util_info ("Inserting %s (+%" GRUB_HOST_PRIuLONG_LONG ",%"
 		  GRUB_HOST_PRIuLONG_LONG ") into %s (%s)\n", disk->name,
 		  (unsigned long long) grub_partition_get_start (disk->partition),
-		  (unsigned long long) grub_disk_get_size (disk),
+		  (unsigned long long) grub_disk_native_sectors (disk),
 		  array->name, diskfilter->name);
   array->driver = diskfilter;
 #endif
@@ -1210,7 +1210,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	struct grub_diskfilter_lv *lv;
 	/* FIXME: Check whether the update time of the superblocks are
 	   the same.  */
-	if (pv->disk && grub_disk_get_size (disk) >= pv->part_size)
+	if (pv->disk && grub_disk_native_sectors (disk) >= pv->part_size)
 	  return GRUB_ERR_NONE;
 	pv->disk = grub_disk_open (disk->name);
 	if (!pv->disk)
@@ -1219,7 +1219,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	   raid device, we shouldn't change it.  */
 	pv->start_sector -= pv->part_start;
 	pv->part_start = grub_partition_get_start (disk->partition);
-	pv->part_size = grub_disk_get_size (disk);
+	pv->part_size = grub_disk_native_sectors (disk);
 
 #ifdef GRUB_UTIL
 	{
@@ -1311,7 +1311,7 @@ grub_diskfilter_get_pv_from_disk (grub_disk_t disk,
 	if (pv->disk && pv->disk->id == disk->id
 	    && pv->disk->dev->id == disk->dev->id
 	    && pv->part_start == grub_partition_get_start (disk->partition)
-	    && pv->part_size == grub_disk_get_size (disk))
+	    && pv->part_size == grub_disk_native_sectors (disk))
 	  {
 	    if (vg_out)
 	      *vg_out = vg;
grub-core/disk/dmraid_nvidia.c +1 -1
index 060279124..4d2fb04d1 100644
@@ -107,7 +107,7 @@ grub_dmraid_nv_detect (grub_disk_t disk,
     /* Skip partition.  */
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN)
     /* Not raid.  */
     return NULL;
grub-core/disk/efi/efidisk.c +1 -1
index 9e20af70e..f077b5f55 100644
@@ -867,7 +867,7 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
       if (ctx.hd->partition_start == 0
 	  && (ctx.hd->partition_size << (parent->log_sector_size
 					 - GRUB_DISK_SECTOR_BITS))
-	  == grub_disk_get_size (parent))
+	  == grub_disk_native_sectors (parent))
 	{
 	  dev_name = grub_strdup (parent->name);
 	}
grub-core/disk/geli.c +3 -3
index 0175ce4c4..2f34a35e6 100644
@@ -258,7 +258,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return NULL;
 
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_sectors = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
@@ -420,7 +420,7 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev)
   if (dev->hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
     return grub_error (GRUB_ERR_BUG, "mdlen is too long");
 
-  sector = grub_disk_get_size (source);
+  sector = grub_disk_native_sectors (source);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return grub_error (GRUB_ERR_BUG, "not a geli");
 
grub-core/disk/ldm.c +2 -2
index 58f8a53e1..912e88255 100644
@@ -821,7 +821,7 @@ grub_ldm_detect (grub_disk_t disk,
 	    /* LDM is never inside a partition.  */
 	    if (!has_ldm || disk->partition)
 	      continue;
-	    sector = grub_disk_get_size (disk);
+	    sector = grub_disk_native_sectors (disk);
 	    if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	      continue;
 	    sector--;
@@ -938,7 +938,7 @@ grub_util_is_ldm (grub_disk_t disk)
 	  /* LDM is never inside a partition.  */
 	  if (!has_ldm || disk->partition)
 	    continue;
-	  sector = grub_disk_get_size (disk);
+	  sector = grub_disk_native_sectors (disk);
 	  if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	    continue;
 	  sector--;
grub-core/disk/luks.c +1 -1
index aa9877b68..5fc79d880 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/mdraid1x_linux.c +1 -1
index c980feba4..38444b02c 100644
@@ -111,7 +111,7 @@ grub_mdraid_detect (grub_disk_t disk,
   grub_uint64_t size;
   grub_uint8_t minor_version;
 
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
 
   /* Check for an 1.x superblock.
    * It's always aligned to a 4K boundary
grub-core/disk/mdraid_linux.c +1 -1
index 11024ae31..e40216f51 100644
@@ -190,7 +190,7 @@ grub_mdraid_detect (grub_disk_t disk,
   struct grub_diskfilter_vg *ret;
 
   /* The sector where the mdraid 0.90 superblock is stored, if available.  */
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
   if (size == GRUB_DISK_SIZE_UNKNOWN)
     /* not 0.9x raid.  */
     return NULL;
grub-core/fs/cbfs.c +8 -8
index 857bea991..581215ef1 100644
@@ -148,16 +148,16 @@ grub_cbfs_mount (grub_disk_t disk)
   grub_off_t header_off;
   struct cbfs_header head;
 
-  if (grub_disk_get_size (disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (disk) == GRUB_DISK_SIZE_UNKNOWN)
     goto fail;
 
-  if (grub_disk_read (disk, grub_disk_get_size (disk) - 1,
+  if (grub_disk_read (disk, grub_disk_native_sectors (disk) - 1,
 		      GRUB_DISK_SECTOR_SIZE - sizeof (ptr),
 		      sizeof (ptr), &ptr))
     goto fail;
 
   ptr = grub_cpu_to_le32 (ptr);
-  header_off = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  header_off = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     + (grub_int32_t) ptr;
 
   if (grub_disk_read (disk, 0, header_off,
@@ -171,16 +171,16 @@ grub_cbfs_mount (grub_disk_t disk)
   if (!data)
     goto fail;
 
-  data->cbfs_start = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_start = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - (grub_be_to_cpu32 (head.romsize) - grub_be_to_cpu32 (head.offset));
-  data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - grub_be_to_cpu32 (head.bootblocksize);
   data->cbfs_align = grub_be_to_cpu32 (head.align);
 
-  if (data->cbfs_start >= (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
+  if (data->cbfs_start >= (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
     goto fail;
-  if (data->cbfs_end > (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
-    data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS);
+  if (data->cbfs_end > (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
+    data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS);
 
   data->next_hofs = data->cbfs_start;
 
grub-core/fs/nilfs2.c +1 -1
index 7ed148d3b..082326f38 100644
@@ -753,7 +753,7 @@ grub_nilfs2_load_sb (struct grub_nilfs2_data *data)
     partition_size = (grub_le_to_cpu64 (data->sblock.s_dev_size)
 		      >> GRUB_DISK_SECTOR_BITS);
   else
-    partition_size = grub_disk_get_size (disk);
+    partition_size = grub_disk_native_sectors (disk);
   if (partition_size != GRUB_DISK_SIZE_UNKNOWN)
     {
       /* Read second super block. */
grub-core/fs/zfs/zfs.c +2 -2
index 41ef0ff57..b6e1e178d 100644
@@ -1175,7 +1175,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
   desc.original = original;
 
   /* Don't check back labels on CDROM.  */
-  if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
     vdevnum = VDEV_LABELS / 2;
 
   for (label = 0; ubbest == NULL && label < vdevnum; label++)
@@ -1184,7 +1184,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
 	= label * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT)
 	+ ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT)
 	+ (label < VDEV_LABELS / 2 ? 0 : 
-	   ALIGN_DOWN (grub_disk_get_size (dev->disk), sizeof (vdev_label_t))
+	   ALIGN_DOWN (grub_disk_native_sectors (dev->disk), sizeof (vdev_label_t))
 	   - VDEV_LABELS * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT));
 
       /* Read in the uberblock ring (128K). */
grub-core/kern/disk.c +1 -1
index ffb09c8ee..e1b0e073e 100644
@@ -533,7 +533,7 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
 }
 
 grub_uint64_t
-grub_disk_get_size (grub_disk_t disk)
+grub_disk_native_sectors (grub_disk_t disk)
 {
   if (disk->partition)
     return grub_partition_get_len (disk->partition);
grub-core/kern/mips/arc/init.c +1 -1
index 3834a1490..2ed3ff319 100644
@@ -430,7 +430,7 @@ grub_machine_get_bootlocation (char **device, char **path)
     }
 
   if (poff == 0
-      && pend == grub_disk_get_size (parent))
+      && pend == grub_disk_native_sectors (parent))
     {
       grub_disk_close (parent);
       *device = dname;
grub-core/normal/misc.c +3 -3
index 435cd9234..8bb6da31f 100644
@@ -176,14 +176,14 @@ grub_normal_print_device_info (const char *name)
 		     (grub_partition_get_start (dev->disk->partition) & 1) ? ".5" : "" );
       else
 	grub_printf_ (N_(" - Sector size %uB"), 1 << dev->disk->log_sector_size);
-      if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+      if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
 	grub_puts_ (N_(" - Total size unknown"));
       else
 	grub_printf (_(" - Total size %llu%sKiB"),
-		     (unsigned long long) (grub_disk_get_size (dev->disk) >> 1),
+		     (unsigned long long) (grub_disk_native_sectors (dev->disk) >> 1),
 		     /* TRANSLATORS: Replace dot with appropriate decimal separator for
 			your language.  */
-		     (grub_disk_get_size (dev->disk) & 1) ? _(".5") : "");
+		     (grub_disk_native_sectors (dev->disk) & 1) ? _(".5") : "");
     }
 
   if (dev)
grub-core/osdep/windows/platform.c +1 -1
index 1ef86bf58..253f8d101 100644
@@ -371,7 +371,7 @@ grub_install_register_efi (grub_device_t efidir_grub_dev,
 
   hddp->partition_start = grub_partition_get_start (efidir_grub_dev->disk->partition)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
-  hddp->partition_size = grub_disk_get_size (efidir_grub_dev->disk)
+  hddp->partition_size = grub_disk_native_sectors (efidir_grub_dev->disk)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
 
   pathptr = hddp + 1;
include/grub/disk.h +2 -2
index 132a1bb75..0fb727d3d 100644
@@ -171,7 +171,7 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 
 #define GRUB_DISK_MAX_MAX_AGGLOMERATE ((1 << (30 - GRUB_DISK_CACHE_BITS - GRUB_DISK_SECTOR_BITS)) - 1)
 
-/* Return value of grub_disk_get_size() in case disk size is unknown. */
+/* Return value of grub_disk_native_sectors() in case disk size is unknown. */
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL
 
 /* Convert to GRUB native disk sized sector from disk sized sector. */
@@ -219,7 +219,7 @@ extern grub_err_t (*EXPORT_VAR(grub_disk_write_weak)) (grub_disk_t disk,
 						       const void *buf);
 
 
-grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk);
+grub_uint64_t EXPORT_FUNC(grub_disk_native_sectors) (grub_disk_t disk);
 
 #if DISK_CACHE_STATS
 void
util/grub-install.c +1 -1
index a35a2e2e8..f9f8ee7b2 100644
@@ -748,7 +748,7 @@ is_prep_empty (grub_device_t dev)
   grub_disk_addr_t dsize, addr;
   grub_uint32_t buffer[32768];
 
-  dsize = grub_disk_get_size (dev->disk);
+  dsize = grub_disk_native_sectors (dev->disk);
   for (addr = 0; addr < dsize;
        addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
     {
util/grub-probe.c +1 -1
index cbe6ed94c..c08e46bbb 100644
@@ -398,7 +398,7 @@ probe (const char *path, char **device_names, char delim)
 	  if (! dev || !dev->disk)
 	    grub_util_error ("%s", grub_errmsg);
 
-	  dsize = grub_disk_get_size (dev->disk);
+	  dsize = grub_disk_native_sectors (dev->disk);
 	  for (addr = 0; addr < dsize;
 	       addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
 	    {
Signature · 1 line
2.27.0
#235 lore raw

PATCH v7 16/17 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
#229 lore raw

PATCH v7 13/17 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 1bb3a333d..499c9330b 100644
@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
+      /* 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.json_slot_key, 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
#213 lore raw

PATCH v7 8/17 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
#206 lore raw

PATCH v7 6/17 luks2: Use more intuitive slot key instead of index in user messages

In reply to Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
grub-core/disk/luks2.c +10 -10
index 437c1da07..ea1065bcf 100644
@@ -284,13 +284,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->json_slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#192 lore raw

PATCH v7 3/17 luks2: Remove unused argument in grub_error

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
index d96764a02..bdb90e4b6 100644
@@ -200,7 +200,7 @@ luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
       grub_json_getstring (&out->size, segment, "size") ||
       grub_json_getstring (&out->encryption, segment, "encryption") ||
       grub_json_getint64 (&out->sector_size, segment, "sector_size"))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters", type);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters");
 
   return GRUB_ERR_NONE;
 }
@@ -228,7 +228,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no segments", type);
+		       "Digest references no segments");
 
   for (i = 0; i < size; i++)
     {
@@ -240,7 +240,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &keyslots))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no keyslots", type);
+		       "Digest references no keyslots");
 
   for (i = 0; i < size; i++)
     {
Signature · 1 line
2.27.0
#237 lore raw

PATCH v7 17/17 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 2335ded77..4fa5a0dbc 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
#189 lore raw

PATCH v7 2/17 misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments

In reply to Glenn Washburn

This ensures that expected order of operations is preserved when arguments are expressions.

---
 include/grub/misc.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/grub/misc.h +2 -2
index b7ca6dd58..780a34e90 100644
@@ -28,10 +28,10 @@
 #include <grub/compiler.h>
 
 #define ALIGN_UP(addr, align) \
-	((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
+	(((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1))
 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
 #define ALIGN_DOWN(addr, align) \
-	((addr) & ~((typeof (addr)) align - 1))
+	((addr) & ~((typeof (addr)) (align) - 1))
 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
 
Signature · 1 line
2.27.0
#200 lore raw

PATCH v7 5/17 luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

---
 grub-core/disk/luks2.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +9 -5
index eadd529e9..437c1da07 100644
@@ -65,6 +65,8 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  /* The integer key to the associative array of keyslots */
+  grub_uint64_t json_slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +105,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t json_slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +115,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t json_slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -261,12 +265,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->json_slot_key, &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_idx);
@@ -278,11 +281,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&d->json_slot_key, &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, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << k->json_slot_key)))
 	break;
     }
   if (i == size)
@@ -295,12 +299,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&s->json_slot_key, &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, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << s->json_slot_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
#231 lore raw

PATCH v7 14/17 whitespace: convert 8 spaces to tabs

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index 499c9330b..2335ded77 100644
@@ -282,8 +282,8 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     {
       if (grub_json_getchild (&digest, &digests, i) ||
 	  grub_json_getuint64 (&d->json_slot_key, &digest, NULL) ||
-          grub_json_getchild (&digest, &digest, 0) ||
-          luks2_parse_digest (d, &digest))
+	  grub_json_getchild (&digest, &digest, 0) ||
+	  luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->json_slot_key)))
@@ -301,7 +301,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&s->json_slot_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
-          luks2_parse_segment (s, &segment))
+	  luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->json_slot_key)))
@@ -625,7 +625,7 @@ luks2_recover_key (grub_disk_t source,
 	{
 	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
 	  continue;
-        }
+	}
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
Signature · 1 line
2.27.0
Reviewed-by on v7 1/17
#187 lore raw

Re: [PATCH v7 01/17] disk: Rename grub_disk_get_size to grub_disk_native_sectors

In reply to Glenn Washburn
Quoted 7 lines · Glenn Washburn

The function grub_disk_get_size is confusingly named because it actually returns a sector count where the sectors are sized in the grub native sector size. Rename to something more appropriate.

Quoted 388 lines
---
 grub-core/disk/diskfilter.c        | 12 ++++++------
 grub-core/disk/dmraid_nvidia.c     |  2 +-
 grub-core/disk/efi/efidisk.c       |  2 +-
 grub-core/disk/geli.c              |  6 +++---
 grub-core/disk/ldm.c               |  4 ++--
 grub-core/disk/luks.c              |  2 +-
 grub-core/disk/mdraid1x_linux.c    |  2 +-
 grub-core/disk/mdraid_linux.c      |  2 +-
 grub-core/fs/cbfs.c                | 16 ++++++++--------
 grub-core/fs/nilfs2.c              |  2 +-
 grub-core/fs/zfs/zfs.c             |  4 ++--
 grub-core/kern/disk.c              |  2 +-
 grub-core/kern/mips/arc/init.c     |  2 +-
 grub-core/normal/misc.c            |  6 +++---
 grub-core/osdep/windows/platform.c |  2 +-
 include/grub/disk.h                |  4 ++--
 util/grub-install.c                |  2 +-
 util/grub-probe.c                  |  2 +-
 18 files changed, 37 insertions(+), 37 deletions(-)
grub-core/disk/diskfilter.c +6 -6
index 86557f923..032011566 100644
@@ -148,7 +148,7 @@ scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
 	if (m->disk && m->disk->id == disk->id
 	    && m->disk->dev->id == disk->dev->id
 	    && m->part_start == grub_partition_get_start (disk->partition)
-	    && m->part_size == grub_disk_get_size (disk))
+	    && m->part_size == grub_disk_native_sectors (disk))
 	  return 0;
     }
 
@@ -1190,13 +1190,13 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 
   grub_dprintf ("diskfilter", "Inserting %s (+%lld,%lld) into %s (%s)\n", disk->name,
 		(unsigned long long) grub_partition_get_start (disk->partition),
-		(unsigned long long) grub_disk_get_size (disk),
+		(unsigned long long) grub_disk_native_sectors (disk),
 		array->name, diskfilter->name);
 #ifdef GRUB_UTIL
   grub_util_info ("Inserting %s (+%" GRUB_HOST_PRIuLONG_LONG ",%"
 		  GRUB_HOST_PRIuLONG_LONG ") into %s (%s)\n", disk->name,
 		  (unsigned long long) grub_partition_get_start (disk->partition),
-		  (unsigned long long) grub_disk_get_size (disk),
+		  (unsigned long long) grub_disk_native_sectors (disk),
 		  array->name, diskfilter->name);
   array->driver = diskfilter;
 #endif
@@ -1210,7 +1210,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	struct grub_diskfilter_lv *lv;
 	/* FIXME: Check whether the update time of the superblocks are
 	   the same.  */
-	if (pv->disk && grub_disk_get_size (disk) >= pv->part_size)
+	if (pv->disk && grub_disk_native_sectors (disk) >= pv->part_size)
 	  return GRUB_ERR_NONE;
 	pv->disk = grub_disk_open (disk->name);
 	if (!pv->disk)
@@ -1219,7 +1219,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	   raid device, we shouldn't change it.  */
 	pv->start_sector -= pv->part_start;
 	pv->part_start = grub_partition_get_start (disk->partition);
-	pv->part_size = grub_disk_get_size (disk);
+	pv->part_size = grub_disk_native_sectors (disk);
 
 #ifdef GRUB_UTIL
 	{
@@ -1311,7 +1311,7 @@ grub_diskfilter_get_pv_from_disk (grub_disk_t disk,
 	if (pv->disk && pv->disk->id == disk->id
 	    && pv->disk->dev->id == disk->dev->id
 	    && pv->part_start == grub_partition_get_start (disk->partition)
-	    && pv->part_size == grub_disk_get_size (disk))
+	    && pv->part_size == grub_disk_native_sectors (disk))
 	  {
 	    if (vg_out)
 	      *vg_out = vg;
grub-core/disk/dmraid_nvidia.c +1 -1
index 060279124..4d2fb04d1 100644
@@ -107,7 +107,7 @@ grub_dmraid_nv_detect (grub_disk_t disk,
     /* Skip partition.  */
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN)
     /* Not raid.  */
     return NULL;
grub-core/disk/efi/efidisk.c +1 -1
index 9e20af70e..f077b5f55 100644
@@ -867,7 +867,7 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
       if (ctx.hd->partition_start == 0
 	  && (ctx.hd->partition_size << (parent->log_sector_size
 					 - GRUB_DISK_SECTOR_BITS))
-	  == grub_disk_get_size (parent))
+	  == grub_disk_native_sectors (parent))
 	{
 	  dev_name = grub_strdup (parent->name);
 	}
grub-core/disk/geli.c +3 -3
index 0175ce4c4..2f34a35e6 100644
@@ -258,7 +258,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return NULL;
 
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_sectors = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
@@ -420,7 +420,7 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev)
   if (dev->hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
     return grub_error (GRUB_ERR_BUG, "mdlen is too long");
 
-  sector = grub_disk_get_size (source);
+  sector = grub_disk_native_sectors (source);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return grub_error (GRUB_ERR_BUG, "not a geli");
 
grub-core/disk/ldm.c +2 -2
index 58f8a53e1..912e88255 100644
@@ -821,7 +821,7 @@ grub_ldm_detect (grub_disk_t disk,
 	    /* LDM is never inside a partition.  */
 	    if (!has_ldm || disk->partition)
 	      continue;
-	    sector = grub_disk_get_size (disk);
+	    sector = grub_disk_native_sectors (disk);
 	    if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	      continue;
 	    sector--;
@@ -938,7 +938,7 @@ grub_util_is_ldm (grub_disk_t disk)
 	  /* LDM is never inside a partition.  */
 	  if (!has_ldm || disk->partition)
 	    continue;
-	  sector = grub_disk_get_size (disk);
+	  sector = grub_disk_native_sectors (disk);
 	  if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	    continue;
 	  sector--;
grub-core/disk/luks.c +1 -1
index aa9877b68..5fc79d880 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/mdraid1x_linux.c +1 -1
index c980feba4..38444b02c 100644
@@ -111,7 +111,7 @@ grub_mdraid_detect (grub_disk_t disk,
   grub_uint64_t size;
   grub_uint8_t minor_version;
 
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
 
   /* Check for an 1.x superblock.
    * It's always aligned to a 4K boundary
grub-core/disk/mdraid_linux.c +1 -1
index 11024ae31..e40216f51 100644
@@ -190,7 +190,7 @@ grub_mdraid_detect (grub_disk_t disk,
   struct grub_diskfilter_vg *ret;
 
   /* The sector where the mdraid 0.90 superblock is stored, if available.  */
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
   if (size == GRUB_DISK_SIZE_UNKNOWN)
     /* not 0.9x raid.  */
     return NULL;
grub-core/fs/cbfs.c +8 -8
index 857bea991..581215ef1 100644
@@ -148,16 +148,16 @@ grub_cbfs_mount (grub_disk_t disk)
   grub_off_t header_off;
   struct cbfs_header head;
 
-  if (grub_disk_get_size (disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (disk) == GRUB_DISK_SIZE_UNKNOWN)
     goto fail;
 
-  if (grub_disk_read (disk, grub_disk_get_size (disk) - 1,
+  if (grub_disk_read (disk, grub_disk_native_sectors (disk) - 1,
 		      GRUB_DISK_SECTOR_SIZE - sizeof (ptr),
 		      sizeof (ptr), &ptr))
     goto fail;
 
   ptr = grub_cpu_to_le32 (ptr);
-  header_off = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  header_off = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     + (grub_int32_t) ptr;
 
   if (grub_disk_read (disk, 0, header_off,
@@ -171,16 +171,16 @@ grub_cbfs_mount (grub_disk_t disk)
   if (!data)
     goto fail;
 
-  data->cbfs_start = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_start = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - (grub_be_to_cpu32 (head.romsize) - grub_be_to_cpu32 (head.offset));
-  data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - grub_be_to_cpu32 (head.bootblocksize);
   data->cbfs_align = grub_be_to_cpu32 (head.align);
 
-  if (data->cbfs_start >= (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
+  if (data->cbfs_start >= (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
     goto fail;
-  if (data->cbfs_end > (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
-    data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS);
+  if (data->cbfs_end > (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
+    data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS);
 
   data->next_hofs = data->cbfs_start;
 
grub-core/fs/nilfs2.c +1 -1
index 7ed148d3b..082326f38 100644
@@ -753,7 +753,7 @@ grub_nilfs2_load_sb (struct grub_nilfs2_data *data)
     partition_size = (grub_le_to_cpu64 (data->sblock.s_dev_size)
 		      >> GRUB_DISK_SECTOR_BITS);
   else
-    partition_size = grub_disk_get_size (disk);
+    partition_size = grub_disk_native_sectors (disk);
   if (partition_size != GRUB_DISK_SIZE_UNKNOWN)
     {
       /* Read second super block. */
grub-core/fs/zfs/zfs.c +2 -2
index 41ef0ff57..b6e1e178d 100644
@@ -1175,7 +1175,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
   desc.original = original;
 
   /* Don't check back labels on CDROM.  */
-  if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
     vdevnum = VDEV_LABELS / 2;
 
   for (label = 0; ubbest == NULL && label < vdevnum; label++)
@@ -1184,7 +1184,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
 	= label * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT)
 	+ ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT)
 	+ (label < VDEV_LABELS / 2 ? 0 : 
-	   ALIGN_DOWN (grub_disk_get_size (dev->disk), sizeof (vdev_label_t))
+	   ALIGN_DOWN (grub_disk_native_sectors (dev->disk), sizeof (vdev_label_t))
 	   - VDEV_LABELS * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT));
 
       /* Read in the uberblock ring (128K). */
grub-core/kern/disk.c +1 -1
index ffb09c8ee..e1b0e073e 100644
@@ -533,7 +533,7 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
 }
 
 grub_uint64_t
-grub_disk_get_size (grub_disk_t disk)
+grub_disk_native_sectors (grub_disk_t disk)
 {
   if (disk->partition)
     return grub_partition_get_len (disk->partition);
grub-core/kern/mips/arc/init.c +1 -1
index 3834a1490..2ed3ff319 100644
@@ -430,7 +430,7 @@ grub_machine_get_bootlocation (char **device, char **path)
     }
 
   if (poff == 0
-      && pend == grub_disk_get_size (parent))
+      && pend == grub_disk_native_sectors (parent))
     {
       grub_disk_close (parent);
       *device = dname;
grub-core/normal/misc.c +3 -3
index 435cd9234..8bb6da31f 100644
@@ -176,14 +176,14 @@ grub_normal_print_device_info (const char *name)
 		     (grub_partition_get_start (dev->disk->partition) & 1) ? ".5" : "" );
       else
 	grub_printf_ (N_(" - Sector size %uB"), 1 << dev->disk->log_sector_size);
-      if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+      if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
 	grub_puts_ (N_(" - Total size unknown"));
       else
 	grub_printf (_(" - Total size %llu%sKiB"),
-		     (unsigned long long) (grub_disk_get_size (dev->disk) >> 1),
+		     (unsigned long long) (grub_disk_native_sectors (dev->disk) >> 1),
 		     /* TRANSLATORS: Replace dot with appropriate decimal separator for
 			your language.  */
-		     (grub_disk_get_size (dev->disk) & 1) ? _(".5") : "");
+		     (grub_disk_native_sectors (dev->disk) & 1) ? _(".5") : "");
     }
 
   if (dev)
grub-core/osdep/windows/platform.c +1 -1
index 1ef86bf58..253f8d101 100644
@@ -371,7 +371,7 @@ grub_install_register_efi (grub_device_t efidir_grub_dev,
 
   hddp->partition_start = grub_partition_get_start (efidir_grub_dev->disk->partition)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
-  hddp->partition_size = grub_disk_get_size (efidir_grub_dev->disk)
+  hddp->partition_size = grub_disk_native_sectors (efidir_grub_dev->disk)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
 
   pathptr = hddp + 1;
include/grub/disk.h +2 -2
index 132a1bb75..0fb727d3d 100644
@@ -171,7 +171,7 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 
 #define GRUB_DISK_MAX_MAX_AGGLOMERATE ((1 << (30 - GRUB_DISK_CACHE_BITS - GRUB_DISK_SECTOR_BITS)) - 1)
 
-/* Return value of grub_disk_get_size() in case disk size is unknown. */
+/* Return value of grub_disk_native_sectors() in case disk size is unknown. */
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL
 
 /* Convert to GRUB native disk sized sector from disk sized sector. */
@@ -219,7 +219,7 @@ extern grub_err_t (*EXPORT_VAR(grub_disk_write_weak)) (grub_disk_t disk,
 						       const void *buf);
 
 
-grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk);
+grub_uint64_t EXPORT_FUNC(grub_disk_native_sectors) (grub_disk_t disk);
 
 #if DISK_CACHE_STATS
 void
util/grub-install.c +1 -1
index a35a2e2e8..f9f8ee7b2 100644
@@ -748,7 +748,7 @@ is_prep_empty (grub_device_t dev)
   grub_disk_addr_t dsize, addr;
   grub_uint32_t buffer[32768];
 
-  dsize = grub_disk_get_size (dev->disk);
+  dsize = grub_disk_native_sectors (dev->disk);
   for (addr = 0; addr < dsize;
        addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
     {
util/grub-probe.c +1 -1
index cbe6ed94c..c08e46bbb 100644
@@ -398,7 +398,7 @@ probe (const char *path, char **device_names, char delim)
 	  if (! dev || !dev->disk)
 	    grub_util_error ("%s", grub_errmsg);
 
-	  dsize = grub_disk_get_size (dev->disk);
+	  dsize = grub_disk_native_sectors (dev->disk);
 	  for (addr = 0; addr < dsize;
 	       addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
 	    {
Signature · 1 line
2.27.0
Reviewed-by on v7 2/17
#190 lore raw

Re: [PATCH v7 02/17] misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This ensures that expected order of operations is preserved when arguments are expressions.

Quoted 24 lines
---
 include/grub/misc.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/grub/misc.h +2 -2
index b7ca6dd58..780a34e90 100644
@@ -28,10 +28,10 @@
 #include <grub/compiler.h>
 
 #define ALIGN_UP(addr, align) \
-	((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
+	(((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1))
 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
 #define ALIGN_DOWN(addr, align) \
-	((addr) & ~((typeof (addr)) align - 1))
+	((addr) & ~((typeof (addr)) (align) - 1))
 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
 
Signature · 1 line
2.27.0
Reviewed-by on v7 3/17
#193 lore raw

Re: [PATCH v7 03/17] luks2: Remove unused argument in grub_error

In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn
Quoted 38 lines
---
 grub-core/disk/luks2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
index d96764a02..bdb90e4b6 100644
@@ -200,7 +200,7 @@ luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
       grub_json_getstring (&out->size, segment, "size") ||
       grub_json_getstring (&out->encryption, segment, "encryption") ||
       grub_json_getint64 (&out->sector_size, segment, "sector_size"))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters", type);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters");
 
   return GRUB_ERR_NONE;
 }
@@ -228,7 +228,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no segments", type);
+		       "Digest references no segments");
 
   for (i = 0; i < size; i++)
     {
@@ -240,7 +240,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &keyslots))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no keyslots", type);
+		       "Digest references no keyslots");
 
   for (i = 0; i < size; i++)
     {
Signature · 1 line
2.27.0
#198 lore raw

Re: [PATCH v7 04/17] luks2: Make sure all fields of output argument in luks2_parse_digest() are written to

In reply to Glenn Washburn
Quoted 7 lines · Glenn Washburn

We should assume that the output argument "out" is uninitialized and could have random data. So, make sure to initialize the segments and keyslots bit fields because potentially not all bits of those fields are written to. Otherwise, the digest could say it belongs to keyslots and segments that it does not.

Makes sense.

Quoted 27 lines
---
 grub-core/disk/luks2.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/disk/luks2.c +2 -0
index bdb90e4b6..eadd529e9 100644
@@ -230,6 +230,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no segments");
 
+  out->segments = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &segments, i) ||
@@ -242,6 +243,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no keyslots");
 
+  out->keyslots = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &keyslots, i) ||
Signature · 1 line
2.27.0
#201 lore raw

Re: [PATCH v7 05/17] luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn
Quoted 8 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

Personally, I'd have named them json_slot_idx. But you've already done so much work on improving the code that I don't want this to be the reason to not give an SOB, especially considering that it's a strict improvement anyway. So:

Quoted 79 lines
---
 grub-core/disk/luks2.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +9 -5
index eadd529e9..437c1da07 100644
@@ -65,6 +65,8 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  /* The integer key to the associative array of keyslots */
+  grub_uint64_t json_slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +105,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t json_slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +115,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t json_slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -261,12 +265,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->json_slot_key, &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_idx);
@@ -278,11 +281,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&d->json_slot_key, &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, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << k->json_slot_key)))
 	break;
     }
   if (i == size)
@@ -295,12 +299,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&s->json_slot_key, &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, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << s->json_slot_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
Reviewed-by on v7 6/17
#207 lore raw

Re: [PATCH v7 06/17] luks2: Use more intuitive slot key instead of index in user messages

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

Oops. I just realized I gave SOBs on the previous patches. Naturally, these should've been Reviewed-by's :(

Quoted 80 lines
---
 grub-core/disk/luks2.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
grub-core/disk/luks2.c +10 -10
index 437c1da07..ea1065bcf 100644
@@ -284,13 +284,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->json_slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
Reviewed-by on v7 7/17
#211 lore raw

Re: [PATCH v7 07/17] 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.

Quoted 29 lines
---
 grub-core/disk/luks2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/luks2.c +2 -2
index ea1065bcf..4be40b22b 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->json_slot_key, &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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -302,7 +302,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&s->json_slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->json_slot_key)))
 	break;
Signature · 1 line
2.27.0
Reviewed-by on v7 8/17
#214 lore raw

Re: [PATCH v7 08/17] 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
Reviewed-by on v7 9/17
#217 lore raw

Re: [PATCH v7 09/17] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

In reply to Glenn Washburn
Quoted 46 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.

---
 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 +0 -0
index 9989e3a16..0542011cc 100644

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 170

Quoted 10 lines
include/grub/types.h +7 -0
@@ -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
+

Nit: multi-line comments do use the following format:

/*
* Foo bar
* bar foo
/

Other than that:

Quoted 6 lines
include/grub/types.h +0 -0
 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 v7 10/17
#221 lore raw

Re: [PATCH v7 10/17] luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

In reply to Glenn Washburn
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 4be40b22b..bdf40768b 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 v7 14/17
#232 lore raw

Re: [PATCH v7 14/17] whitespace: convert 8 spaces to tabs

In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn
Quoted 40 lines
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index 499c9330b..2335ded77 100644
@@ -282,8 +282,8 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     {
       if (grub_json_getchild (&digest, &digests, i) ||
 	  grub_json_getuint64 (&d->json_slot_key, &digest, NULL) ||
-          grub_json_getchild (&digest, &digest, 0) ||
-          luks2_parse_digest (d, &digest))
+	  grub_json_getchild (&digest, &digest, 0) ||
+	  luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << k->json_slot_key)))
@@ -301,7 +301,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&s->json_slot_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
-          luks2_parse_segment (s, &segment))
+	  luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << s->json_slot_key)))
@@ -625,7 +625,7 @@ luks2_recover_key (grub_disk_t source,
 	{
 	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
 	  continue;
-        }
+	}
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
Signature · 1 line
2.27.0
Reviewed-by on v7 16/17
#236 lore raw

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

In reply to Glenn Washburn
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
#239 lore raw
In reply to Glenn Washburn
Quoted 6 lines · Glenn Washburn

This patch series is an update to reflect changes suggested in v6. Of note, there are a few new patches: 01, 02, and 04.

01: Daniel suggested to renae grub_disk_get_size to grub_disk_native_sectors 02: Make ALIGN_UP and ALIGN_DOWN safer
04: Make luks2_parse_digest() safer

The patches I reviewed look good to me. I gotta jump, so I'll hopefully get to the remaining patches this evening.

Also note that I accidentally posted SOBs for the first few ones. These should've been Reviewed-by's.

Thanks for all your work!

Patrick

Quoted 566 lines

Glenn

Glenn Washburn (17):
disk: Rename grub_disk_get_size to grub_disk_native_sectors
misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
luks2: Remove unused argument in grub_error
luks2: Make sure all fields of output argument in luks2_parse_digest()
are written to
luks2: Add json_slot_key member to struct
grub_luks2_keyslot/segment/digest
luks2: Use more intuitive slot key instead of index in user messages
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
whitespace: convert 8 spaces to tabs
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

 grub-core/disk/cryptodisk.c        |  64 +++++++-----
 grub-core/disk/diskfilter.c        |  12 +--
 grub-core/disk/dmraid_nvidia.c     |   2 +-
 grub-core/disk/efi/efidisk.c       |   2 +-
 grub-core/disk/geli.c              |   6 +-
 grub-core/disk/ldm.c               |   4 +-
 grub-core/disk/luks.c              |   7 +-
 grub-core/disk/luks2.c             | 160 +++++++++++++++++++++++------
 grub-core/disk/mdraid1x_linux.c    |   2 +-
 grub-core/disk/mdraid_linux.c      |   2 +-
 grub-core/fs/cbfs.c                |  16 +--
 grub-core/fs/nilfs2.c              |   2 +-
 grub-core/fs/zfs/zfs.c             |   4 +-
 grub-core/kern/compiler-rt.c       |   2 +-
 grub-core/kern/disk.c              |   2 +-
 grub-core/kern/mips/arc/init.c     |   2 +-
 grub-core/normal/misc.c            |   6 +-
 grub-core/osdep/windows/platform.c |   2 +-
 include/grub/compiler-rt.h         |   2 +-
 include/grub/cryptodisk.h          |   8 +-
 include/grub/disk.h                |  21 +++-
 include/grub/misc.h                |   7 +-
 include/grub/types.h               |   9 ++
 util/grub-install.c                |   2 +-
 util/grub-probe.c                  |   2 +-
 25 files changed, 249 insertions(+), 99 deletions(-)

Range-diff against v6:

 -:  --------- >  1:  7e79d6fb1 disk: Rename grub_disk_get_size to grub_disk_native_sectors
 -:  --------- >  2:  77f9671d5 misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
 3:  8527be145 !  3:  d1a36aa79 luks2: Remove unused argument in grub_error

@@ Metadata
## Commit message ##
luks2: Remove unused argument in grub_error

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
grub_json_getstring (&out->size, segment, "size") ||

 -:  --------- >  4:  dab43e033 luks2: Make sure all fields of output argument in luks2_parse_digest() are written to
 1:  6262aefe9 !  5:  35f47644c luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest
    +    luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest
     
         This allows code using these structs to know the named key associated with
         these json data structures. In the future we can use these to provide better
         error messages to the user.
     
    -    Get rid of idx variable in luks2_get_keyslot which was overloaded to be used
    -    for both keyslot and segment slot keys.
    +    Get rid of idx variable in luks2_get_keyslot() which was overloaded to be
    +    used for both keyslot and segment slot keys.
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
      
      struct grub_luks2_keyslot
      {
    -+  grub_uint64_t slot_key;
    ++  /* The integer key to the associative array of keyslots */
    ++  grub_uint64_t json_slot_key;
        grub_int64_t key_size;
        grub_int64_t priority;
        struct
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
      
      struct grub_luks2_segment
      {
    -+  grub_uint64_t slot_key;
    ++  grub_uint64_t json_slot_key;
        grub_uint64_t offset;
        const char	*size;
        const char	*encryption;
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_segment grub_luks2_segment_t;
      
      struct grub_luks2_digest
      {
    -+  grub_uint64_t slot_key;
    ++  grub_uint64_t json_slot_key;
        /* Both keyslots and segments are interpreted as bitfields here */
        grub_uint64_t	keyslots;
        grub_uint64_t	segments;
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        if (grub_json_getvalue (&keyslots, root, "keyslots") ||
            grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
     -      grub_json_getuint64 (&idx, &keyslot, NULL) ||
    -+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
    ++      grub_json_getuint64 (&k->json_slot_key, &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_idx);
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        for (i = 0; i < size; i++)
          {
            if (grub_json_getchild (&digest, &digests, i) ||
    -+	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
    ++	  grub_json_getuint64 (&d->json_slot_key, &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, i);
      
     -      if ((d->keyslots & (1 << idx)))
    -+      if ((d->keyslots & (1 << k->slot_key)))
    ++      if ((d->keyslots & (1 << k->json_slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
          {
            if (grub_json_getchild (&segment, &segments, i) ||
     -	  grub_json_getuint64 (&idx, &segment, NULL) ||
    -+	  grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
    ++	  grub_json_getuint64 (&s->json_slot_key, &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, i);
      
     -      if ((d->segments & (1 << idx)))
    -+      if ((d->segments & (1 << s->slot_key)))
    ++      if ((d->segments & (1 << s->json_slot_key)))
      	break;
          }
        if (i == size)
 2:  3cd52834b !  6:  e52887944 luks2: Use more intuitive slot key instead of index in user messages.
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Use more intuitive slot key instead of index in user messages.
    +    luks2: Use more intuitive slot key instead of index in user messages
     
         Use the slot key name in the json array rather than the 0 based index in the
         json array for keyslots, segments, and digests. This is less confusing for
    @@ Commit message
         messages using the index of the keyslot will mention keyslot 1 (its a
         zero-based index). Furthermore, with this change the keyslot number will
         align with the number used to reference the keyslot when using the
    -    --key-slot argument to cryptsetup. Error messages now distinguish between
    -    indexes and slot keys. The former include the string "index" in the error
    -    string, and the later are surrounded in quotes.
    +    --key-slot argument to cryptsetup.
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    -       grub_json_getuint64 (&k->slot_key, &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_idx);
    -+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
    - 
    -   /* Get digest that matches the keyslot. */
    -   if (grub_json_getvalue (&digests, root, "digests") ||
    -@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	  grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
    + 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
     +	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
      
    -       if ((d->keyslots & (1 << k->slot_key)))
    +       if ((d->keyslots & (1 << k->json_slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        /* Get segment that matches the digest. */
        if (grub_json_getvalue (&segments, root, "segments") ||
     @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	  grub_json_getuint64 (&s->slot_key, &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, i);
    -+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
    - 
    -       if ((d->segments & (1 << s->slot_key)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
      	  continue;
              }
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      	{
     -	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
    -+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
    ++	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
     +			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      	{
     -	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
    -+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
    ++	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
     +			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
    ++      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
      
            candidate_key_len = keyslot.key_size;
            break;
 4:  1a248b679 <  -:  --------- cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt
 -:  --------- >  7:  43e3b6cce luks2: Add string "index" to user strings using a json index.
 -:  --------- >  8:  4aa9757e1 cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
 -:  --------- >  9:  d18993a63 cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
 5:  45f5d644f ! 10:  7e8f242d1 luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors

@@ Commit message

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. And counter-intuitively, grub_disk_get_size
- returns the total number of device native sectors.
+ to cryptodisk sectors as well.

Also, removed an empty statement.

- Signed-off-by: Glenn Washburn <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
6: 6e01cafb1 ! 11: 1fe9c38ed cryptodisk: Properly handle non-512 byte sized sectors
@@ Commit message
512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/cryptodisk.c ##
@@ grub-core/disk/cryptodisk.c: lrw_xor (const struct lrw_sector *sec,
static gcry_err_code_t
@@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
}
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 & GRUB_TYPE_U_MAX (iv[0]));
@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu
newdev->source_disk = NULL;
- newdev->log_sector_size = 9;
+ newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
- newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
+ newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
newdev->modname = "luks";
@@ grub-core/disk/luks.c: luks_recover_key (grub_disk_t source,
7: d8927c6bc ! 12: c9db343e8 luks2: Better error handling when setting up the cryptodisk

    @@ Commit message
         an invalid segment, and continue on to the next key.
     
      ## grub-core/disk/luks2.c ##
    +@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    + 	break;
    +     }
    +   if (i == size)
    +-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
    ++      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key);
    + 
    +   /* Get segment that matches the digest. */
    +   if (grub_json_getvalue (&segments, root, "segments") ||
    +@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    + 	break;
    +     }
    +   if (i == size)
    +-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
    ++    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key);
    + 
    +   return GRUB_ERR_NONE;
    + }
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            goto err;
          }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
            if (keyslot.priority == 0)
      	{
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +-	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
    + 	  continue;
    +         }
    + 
    +-      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
    + 
    +       /* 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;
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " %"PRIuGRUB_UINT64_T" which is greater than"
     +				 " source disk size %"PRIuGRUB_UINT64_T","
     +				 " skipping\n",
    -+				 segment.slot_key, crypt->offset_sectors,
    ++				 segment.json_slot_key, crypt->offset_sectors,
     +				 max_crypt_sectors);
     +	  continue;
     +	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     -	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
     +	{
     +	  grub_errno = GRUB_ERR_NONE;
    -+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
    ++	  /* 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)
    ++	  else if (grub_errno == GRUB_ERR_BAD_NUMBER)
     +	    {
    -+	      /* TODO: Unparsable number-string, try to use the whole disk */
     +	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
     +				     " \"%s\" is not a parsable number\n",
    -+				     segment.slot_key, segment.size);
    ++				     segment.json_slot_key, segment.size);
     +	      continue;
     +	    }
    -+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
    ++	  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: luks2_recover_key (grub_disk_t source,
     +				     " %s overflowed 64-bit unsigned integer,"
     +				     " the end of the crypto device will be"
     +				     " inaccessible\n",
    -+				     segment.slot_key, segment.size);
    ++				     segment.json_slot_key, segment.size);
     +	      if (crypt->total_sectors > max_crypt_sectors)
     +		crypt->total_sectors = max_crypt_sectors;
     +	    }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	{
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has zero"
     +				 " sectors, skipping\n",
    -+				 segment.slot_key);
    ++				 segment.json_slot_key);
     +	  continue;
     +	}
     +      else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " data position greater than source disk size,"
     +				 " the end of the crypto device will be"
     +				 " inaccessible\n",
    -+				 segment.slot_key);
    ++				 segment.json_slot_key);
     +	  /* 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));
    +       if (ret)
    + 	{
    + 	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
    +-			keyslot.slot_key, grub_errmsg);
    ++			keyslot.json_slot_key, grub_errmsg);
    + 	  continue;
    + 	}
    + 
    +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +       if (ret)
    + 	{
    + 	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
    +-			keyslot.slot_key, grub_errmsg);
    ++			keyslot.json_slot_key, grub_errmsg);
    + 	  continue;
    + 	}
    + 
    +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    +        * TRANSLATORS: It's a cryptographic key slot: one element of an array
    +        * where each element is either empty or holds a key.
    +        */
    +-      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
    ++      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.json_slot_key);
    + 
    +       candidate_key_len = keyslot.key_size;
    +       break;
     
      ## include/grub/disk.h ##
    +@@
    + #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: typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
    - /* Return value of grub_disk_get_size() in case disk size is unknown. */
    + /* 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. */
    @@ include/grub/disk.h: typedef struct grub_disk_memberlist *grub_disk_memberlist_t
     +{
     +  if (disk->log_sector_size < log_sector_size)
     +    {
    -+      /* Round up to the nearest log_sector_size sized sector. */
    -+      sector += 1ULL << ((log_sector_size / disk->log_sector_size) - 1);
    ++      sector = ALIGN_UP (sector, 1 << (log_sector_size / disk->log_sector_size));
     +      return sector >> (log_sector_size - disk->log_sector_size);
     +    }
     +  else
 8:  fcd7aadb7 ! 13:  2fcef44b6 luks2: Error check segment.sector_size
    @@ Metadata
      ## Commit message ##
         luks2: Error check segment.sector_size
     
    +    Reviewed-by: Daniel Kiper <[email protected]>
    +
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
    -       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
    +       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
      
     +      /* Sector size should be one of 512, 1024, 2048, or 4096. */
     +      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
     +				 " size %"PRIuGRUB_UINT64_T" is not one of"
     +				 " 512, 1024, 2048, or 4096\n",
    -+				 segment.slot_key, segment.sector_size);
    ++				 segment.json_slot_key, segment.sector_size);
     +	  continue;
     +	}
     +

9: 61f77a1a8 ! 14: 74eed4a62 whitespace: convert 8 spaces to tabs.
@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- whitespace: convert 8 spaces to tabs.
+ whitespace: convert 8 spaces to tabs
+
+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
{
if (grub_json_getchild (&digest, &digests, i) ||
- grub_json_getuint64 (&d->slot_key, &digest, NULL) ||
+ grub_json_getuint64 (&d->json_slot_key, &digest, NULL) ||
- grub_json_getchild (&digest, &digest, 0) ||
- luks2_parse_digest (d, &digest))
+ grub_json_getchild (&digest, &digest, 0) ||
+ luks2_parse_digest (d, &digest))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

- if ((d->keyslots & (1 << k->slot_key)))
+ if ((d->keyslots & (1 << k->json_slot_key)))
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
if (grub_json_getchild (&segment, &segments, i) ||
- grub_json_getuint64 (&s->slot_key, &segment, NULL) ||
+ grub_json_getuint64 (&s->json_slot_key, &segment, NULL) ||
grub_json_getchild (&segment, &segment, 0) ||
- luks2_parse_segment (s, &segment))
+ luks2_parse_segment (s, &segment))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);

- if ((d->segments & (1 << s->slot_key)))
+ if ((d->segments & (1 << s->json_slot_key)))
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
{
- grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
continue;
- }
+ }

- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);

10: d71d26701 ! 15: 51c2e9160 mips: Enable __clzdi2()
@@ Commit message
__clzdi2()) but for MIPS target and __clzdi2 only, __clzsi2 was already enabled.

- Signed-off-by: Daniel Kiper <[email protected]>
+ Suggested-by: Daniel Kiper <[email protected]>

## grub-core/kern/compiler-rt.c ##
@@ grub-core/kern/compiler-rt.c: __clzsi2 (grub_uint32_t val)
11: 8aa295f77 ! 16: 3b4ccda1f misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
@@ Metadata
## Commit message ##
misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## include/grub/misc.h ##
@@ include/grub/misc.h: void EXPORT_FUNC(grub_real_boot_time) (const char *file,
#define grub_max(a, b) (((a) > (b)) ? (a) : (b))
12: 7050a4ace ! 17: 8b97e6887 luks2: Use grub_log2ull to calculate log_sector_size and improve readability
@@ Metadata
## Commit message ##
luks2: Use grub_log2ull to calculate log_sector_size and improve readability

+ Reviewed-by: Daniel Kiper <[email protected]>
+
## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,

Signature · 1 line
2.27.0
Reviewed-by on v7 11/17
#223 lore raw

Re: [PATCH v7 11/17] cryptodisk: Properly handle non-512 byte sized sectors

In reply to Glenn Washburn
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 bdf40768b..de2e70796 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
#225 lore raw

Re: [PATCH v7 12/17] luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn
Quoted 10 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",

Nit: there's something missing here.

Quoted 44 lines

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

---
 grub-core/disk/luks2.c | 98 +++++++++++++++++++++++++++++++++++++-----
 include/grub/disk.h    | 17 ++++++++
 2 files changed, 105 insertions(+), 10 deletions(-)
grub-core/disk/luks2.c +5 -2
index de2e70796..1bb3a333d 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key);
 
   return GRUB_ERR_NONE;
 }
@@ -600,37 +600,115 @@ 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 luks2_recover_key (grub_disk_t source, · line 615

Quoted 10 lines
grub-core/disk/luks2.c +7 -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");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;

Please bear with me if this has been discussed in a previous round, but why exactly do we cast max_crypt_sectors to the type of source->total_sectors? And isn't the variable always set anyway in case the keyslot has a non-zero priority?

Patrick

Quoted 161 lines
grub-core/disk/luks2.c +76 -8
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
       /* 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;
+      /* 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.json_slot_key, 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.json_slot_key, 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.json_slot_key, 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.json_slot_key);
+	  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.json_slot_key);
+	  /* 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));
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -638,7 +716,7 @@ luks2_recover_key (grub_disk_t source,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -646,7 +724,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.json_slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
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
Reviewed-by on v7 13/17
#230 lore raw

Re: [PATCH v7 13/17] luks2: Error check segment.sector_size

In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn
Quoted 29 lines
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
index 1bb3a333d..499c9330b 100644
@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
 
+      /* 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.json_slot_key, 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
#234 lore raw

Re: [PATCH v7 15/17] 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.

I'm not going to give my Reviewed-by on this patch as I honestly ain't got a clue if those are supported on MIPS. So I don't feel qualified to.

Patrick

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
Reviewed-by on v7 17/17
#238 lore raw

Re: [PATCH v7 17/17] luks2: Use grub_log2ull to calculate log_sector_size and improve readability

In reply to Glenn Washburn
Quoted 11 lines · Glenn Washburn
---
 grub-core/disk/luks2.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
grub-core/disk/luks2.c +0 -0
index 2335ded77..4fa5a0dbc 100644

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

Quoted 6 lines
grub-core/disk/luks2.c +1 -2
@@ -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);

I was quite confused by the fact that previously we were using an unsigned int (which typically is 4 bytes on 64 bit systems), but now we're using the 8 byte wide uint64. Until I realized that grub_log2ull(n) uses __builtin_clzll instead of __builtin_clz. A commit message would've helped to clear up this confusion.

Other than that:

Quoted 6 lines
grub-core/disk/luks2.c +0 -0
       /* 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
#188 lore raw

Re: [PATCH v7 01/17] disk: Rename grub_disk_get_size to grub_disk_native_sectors

In reply to Glenn Washburn
Quoted 6 lines · Glenn Washburn

The function grub_disk_get_size is confusingly named because it actually returns a sector count where the sectors are sized in the grub native sector size. Rename to something more appropriate.

Nit, please do not leave empty lines between the tags. I will fix it before committing.

Quoted 1 line

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

Reviewed-by on v7 4/17
#199 lore raw

Re: [PATCH v7 04/17] luks2: Make sure all fields of output argument in luks2_parse_digest() are written to

In reply to Glenn Washburn
Quoted 7 lines · Glenn Washburn

We should assume that the output argument "out" is uninitialized and could have random data. So, make sure to initialize the segments and keyslots bit fields because potentially not all bits of those fields are written to. Otherwise, the digest could say it belongs to keyslots and segments that it does not.

Daniel

#202 lore raw

Re: [PATCH v7 05/17] luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Patrick Steinhardt
Quoted 8 lines · Patrick Steinhardt
Quoted 7 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

Quoted 8 lines
Quoted 1 line

Personally, I'd have named them json_slot_idx. But you've already done so much work on improving the code that I don't want this to be the reason to not give an SOB, especially considering that it's a strict improvement anyway. So:

I can change this to json_slot_idx before committing if Glenn does not object. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#208 lore raw

Re: [PATCH v7 06/17] luks2: Use more intuitive slot key instead of index in user messages

In reply to Glenn Washburn
Quoted 19 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
grub-core/disk/luks2.c +0 -0
index 437c1da07..ea1065bcf 100644

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 287

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
@@ -284,13 +284,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

Does not this change belong to next patch?

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 293

Quoted 6 lines
grub-core/disk/luks2.c +1 -1
       if ((d->keyslots & (1 << k->json_slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);

I am afraid this does not build. The slot_key seems a remnant from v6.

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 311

Quoted 8 lines
grub-core/disk/luks2.c +1 -1
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);

Ditto and below...

Quoted 38 lines
grub-core/disk/luks2.c +6 -6
   return GRUB_ERR_NONE;
 }
@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 

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

Quoted 8 lines
grub-core/disk/luks2.c +1 -1
@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;

Daniel

Reviewed-by on v7 7/17
#212 lore raw

Re: [PATCH v7 07/17] 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 v7 9/17
#218 lore raw

Re: [PATCH v7 09/17] 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.

But one nit below...

Quoted 42 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 +0 -0
index 9989e3a16..0542011cc 100644

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 167

Quoted 7 lines
include/grub/types.h +4 -0
@@ -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.
+ */

Incorrect comment formatting...

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 173

Quoted 6 lines
include/grub/types.h +3 -0
+#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)]

Daniel

#226 lore raw

Re: [PATCH v7 12/17] luks2: Better error handling when setting up the cryptodisk

In reply to Patrick Steinhardt
Quoted 10 lines · On Sun, 6 Dec 2020 20:35:13 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 9 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.

Quoted 4 lines
Quoted 2 lines

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic",

Nit: there's something missing here.

Yep, thanks for catching that. I was going to complete this and forgot in my rush to get the series out before some travel. I'll rework that.

Quoted 56 lines
Quoted 56 lines

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +1 -1
index de2e70796..1bb3a333d 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key); 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +1 -1
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key); 
   return GRUB_ERR_NONE;

}

grub-core/disk/luks2.c +3 -0
@@ -600,37 +600,115 @@ 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 +3 -0
+      goto err;
+    }
+
Quoted 8 lines
Quoted 4 lines
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {

+ typeof(source->total_sectors) max_crypt_sectors = 0;

Please bear with me if this has been discussed in a previous round, but why exactly do we cast max_crypt_sectors to the type of source->total_sectors?

Technically, this isn't a cast. Its a variable declaration. But I'm using the typeof(source->total_sectors) because max_crypt_sectors can be no more or less than the total sectors, ie its of the same type.

Quoted 2 lines

And isn't the variable always set anyway in
case the keyslot has a non-zero priority?

Yep. Are you suggesting that it need not be initialized? This is true, but I don't think that's a problem. I think generally initializing things is a good practice. It might be problematic if this was in an oft used function (or it might not, would need to see if the compiler was smart enough to ignore the initialization). But that initialization is going to happen very rarely in the lifetime of a grub execution instance. I also don't really care either way.

Glenn

Quoted 197 lines

Patrick

Quoted 195 lines
grub-core/disk/luks2.c +2 -0
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,

i); 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 -1
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot

\"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);

grub-core/disk/luks2.c +1 -0
+	  grub_dprintf ("luks2", "Ignoring keyslot

\"%"PRIuGRUB_UINT64_T"\" due to priority\n",
keyslot.json_slot_key); continue; }

- grub_dprintf ("luks2", "Trying keyslot
\"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Trying keyslot

\"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key); 
       /* 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;
+ /* 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.json_slot_key,

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 +1 -0
+				     segment.json_slot_key,

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

device will be"

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

segment.size);

grub-core/disk/luks2.c +8 -0
+	      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"

grub-core/disk/luks2.c +5 -0
+				 " sectors, skipping\n",
+				 segment.json_slot_key);
+	  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.json_slot_key);
+	  /* 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)); if (ret)

grub-core/disk/luks2.c +0 -0
 	{
 	  grub_dprintf ("luks2", "Decryption with keyslot

\"%"PRIuGRUB_UINT64_T"\" failed: %s\n",

grub-core/disk/luks2.c +2 -2
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
@@ -638,7 +716,7 @@ luks2_recover_key (grub_disk_t source,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Could not open keyslot
 \"%"PRIuGRUB_UINT64_T"\": %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
@@ -646,7 +724,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element

of an array
* where each element is either empty or holds a key. */
- grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"),
keyslot.slot_key);
+ grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"),

keyslot.json_slot_key); 
       candidate_key_len = keyslot.key_size;
       break;
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. */

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
+/* 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));

include/grub/disk.h +6 -0
+      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) --
2.27.0

#219 lore raw

Re: [PATCH v7 09/17] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

In reply to Daniel Kiper
Quoted 58 lines · On Mon, 7 Dec 2020 21:22:09 +0100 Daniel Kiper <[email protected]> wrote:
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.

But one nit below...

Quoted 48 lines
---
 grub-core/disk/cryptodisk.c | 8 ++++----
 include/grub/types.h        | 7 +++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 0e955a020..5aa0c4720 100644

grub-core/disk/cryptodisk.c +0 -0
@@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0])); /* FALLTHROUGH */

grub-core/disk/cryptodisk.c +1 -1
 	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;

grub-core/disk/cryptodisk.c +0 -0
 	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)

grub-core/disk/cryptodisk.c +1 -1
-				    & 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]));

grub-core/disk/cryptodisk.c +1 -1
-	    iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX

(iv[0])); }

grub-core/disk/cryptodisk.c +1 -1
 	  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,

grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +0 -0
index 9989e3a16..0542011cc 100644
@@ -161,6 +161,13 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
Quoted 8 lines
Quoted 6 lines
include/grub/types.h +2 -0
+/*
+  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.
+ */

Incorrect comment formatting...

I'll fix it, and get it right one of these days.

Quoted 10 lines
Quoted 8 lines

+#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)]

Daniel

#203 lore raw

Re: [PATCH v7 05/17] luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Daniel Kiper
Quoted 16 lines · On Mon, 7 Dec 2020 21:02:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 15 lines · Patrick Steinhardt
Quoted 8 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

Personally, I'd have named them json_slot_idx. But you've already done so much work on improving the code that I don't want this to be the reason to not give an SOB, especially considering that it's a strict improvement anyway. So:

Quoted 4 lines
Quoted 1 line

I can change this to json_slot_idx before committing if Glenn does not object. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Thanks Patrick for the sentiment. Looking at the luks2 spec, it says:

"JSON objects must have their names formatted as a string that represents a number in the decimal notation (unsigned integer) – for example ”0”, ”1” and must contain attribute _type_. According to the _type_, the implementation decides how to handle (or ignore) such an object. This notation allows mapping to LUKS1 API functions that use an integer as a reference to keyslots objects."

So here, the spec is calling that value a "name", and saying that it must be a string of decimal digits. Looking at the spec, the "name" of the keyslot object does not need to correspond to the index into the array of keyslot areas on disk. However it does in the canonical implementation for use with LUKS1 API functions which require and integer, as suggested in the quote above.

I'd say that these numbers are actually an id for the object of its respective class. In the cryptsetup implementation, the "id" happens to correspond to the index into the binary key area array, but that's needn't be the case. My preference would be to name it "id" and second choice would be just "idx" (since its usually an index into a physical array of key areas or virtual array of segments and digests).

I don't think the "json" part of the name is warranted, because it really has nothing to do with json. The "slot" part is really only applicable to keyslots because digests and segments don't have an equivalent slot aspect. So I suggest we name the struct member names to just "id" instead. And where we just the index of the name-value pair in the json associative array we use "json_idx" as a suffix. So this would mean changing the argument keyslot_idx in luks2_get_keyslot() to keyslot_json_idx. Optionally the local variable "i" in luks2_get_keyslot() and luks2_parse_segment() could be renamed to "json_idx" as well (I don't care either way about this though).

Glenn

#228 lore raw

Re: [PATCH v7 12/17] luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn

This patch got mangled accidentally with patch 5. I'll update them both.

Glenn

Quoted 265 lines · On Fri, 4 Dec 2020 10:43:41 -0600 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",

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +1 -1
index de2e70796..1bb3a333d 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key); 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +1 -1
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key); 
   return GRUB_ERR_NONE;

}

grub-core/disk/luks2.c +3 -0
@@ -600,37 +600,115 @@ 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 +8 -0
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      typeof(source->total_sectors) max_crypt_sectors = 0;
+
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       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)
grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot

\"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);

grub-core/disk/luks2.c +1 -0
+	  grub_dprintf ("luks2", "Ignoring keyslot

\"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key); continue; }

- grub_dprintf ("luks2", "Trying keyslot
\"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Trying keyslot

\"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key); 
       /* 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

grub-core/disk/luks2.c +1 -1
 		- __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.json_slot_key,

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 +1 -0
+				     segment.json_slot_key,

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 device

will be"

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

segment.size);

grub-core/disk/luks2.c +8 -0
+	      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"

grub-core/disk/luks2.c +5 -0
+				 " sectors, skipping\n",
+				 segment.json_slot_key);
+	  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.json_slot_key);
+	  /* 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)); if (ret)

grub-core/disk/luks2.c +0 -0
 	{
 	  grub_dprintf ("luks2", "Decryption with keyslot

\"%"PRIuGRUB_UINT64_T"\" failed: %s\n",

grub-core/disk/luks2.c +2 -2
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
@@ -638,7 +716,7 @@ luks2_recover_key (grub_disk_t source,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Could not open keyslot
 \"%"PRIuGRUB_UINT64_T"\": %s\n",
-			keyslot.slot_key, grub_errmsg);
+			keyslot.json_slot_key, grub_errmsg);
 	  continue;
 	}
@@ -646,7 +724,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of

an array
* where each element is either empty or holds a key. */
- grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"),
keyslot.slot_key);
+ grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"),

keyslot.json_slot_key); 
       candidate_key_len = keyslot.key_size;
       break;
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
+/* 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));

include/grub/disk.h +6 -0
+      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)

#204 lore raw

Re: [PATCH v7 05/17] luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Glenn Washburn
Quoted 58 lines · Glenn Washburn
Quoted 16 lines · On Mon, 7 Dec 2020 21:02:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 15 lines · Patrick Steinhardt
Quoted 8 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

Personally, I'd have named them json_slot_idx. But you've already done so much work on improving the code that I don't want this to be the reason to not give an SOB, especially considering that it's a strict improvement anyway. So:

Quoted 4 lines
Quoted 1 line

I can change this to json_slot_idx before committing if Glenn does not object. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Thanks Patrick for the sentiment. Looking at the luks2 spec, it says:

"JSON objects must have their names formatted as a string that represents a number in the decimal notation (unsigned integer) – for example ”0”, ”1” and must contain attribute _type_. According to the _type_, the implementation decides how to handle (or ignore) such an object. This notation allows mapping to LUKS1 API functions that use an integer as a reference to keyslots objects."

So here, the spec is calling that value a "name", and saying that it must be a string of decimal digits. Looking at the spec, the "name" of the keyslot object does not need to correspond to the index into the array of keyslot areas on disk. However it does in the canonical implementation for use with LUKS1 API functions which require and integer, as suggested in the quote above.

I'd say that these numbers are actually an id for the object of its respective class. In the cryptsetup implementation, the "id" happens to correspond to the index into the binary key area array, but that's needn't be the case. My preference would be to name it "id" and second choice would be just "idx" (since its usually an index into a physical array of key areas or virtual array of segments and digests).

I don't think the "json" part of the name is warranted, because it really has nothing to do with json. The "slot" part is really only applicable to keyslots because digests and segments don't have an equivalent slot aspect. So I suggest we name the struct member names to just "id" instead. And where we just the index of the name-value pair in the json associative array we use "json_idx" as a suffix. So this would mean changing the argument keyslot_idx in luks2_get_keyslot() to keyslot_json_idx. Optionally the local variable "i" in luks2_get_keyslot() and luks2_parse_segment() could be renamed to "json_idx" as well (I don't care either way about this though).

Quoted 1 line

Glenn

Sounds sensible to me. Based on your reasoning, I'm happy with either "id" or "key". So we may just as well just keep it as-is, as you prefer.

Patrick

#227 lore raw

Re: [PATCH v7 12/17] luks2: Better error handling when setting up the cryptodisk

In reply to Glenn Washburn
Quoted 86 lines · Glenn Washburn
Quoted 10 lines · On Sun, 6 Dec 2020 20:35:13 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 9 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.

Quoted 4 lines
Quoted 2 lines

Do some sanity checking on data coming from the luks header. If segment.size is "dynamic",

Nit: there's something missing here.

Yep, thanks for catching that. I was going to complete this and forgot in my rush to get the series out before some travel. I'll rework that.

Quoted 56 lines
Quoted 56 lines

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 so long as the source disk's size is greater than this segment size. Otherwise, this is an invalid segment, and continue on to the next key.

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

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

grub-core/disk/luks2.c +1 -1
index de2e70796..1bb3a333d 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
 keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key); 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +1 -1
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key); 
   return GRUB_ERR_NONE;

}

grub-core/disk/luks2.c +3 -0
@@ -600,37 +600,115 @@ 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 +3 -0
+      goto err;
+    }
+
Quoted 8 lines
Quoted 4 lines
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {

+ typeof(source->total_sectors) max_crypt_sectors = 0;

Please bear with me if this has been discussed in a previous round, but why exactly do we cast max_crypt_sectors to the type of source->total_sectors?

Quoted 3 lines

Technically, this isn't a cast. Its a variable declaration. But I'm using the typeof(source->total_sectors) because max_crypt_sectors can be no more or less than the total sectors, ie its of the same type.

Oh, of course. I somehow didn't realize this at all, probably because this is not something one sees that often.

Quoted 11 lines
Quoted 2 lines

And isn't the variable always set anyway in
case the keyslot has a non-zero priority?

Yep. Are you suggesting that it need not be initialized? This is true, but I don't think that's a problem. I think generally initializing things is a good practice. It might be problematic if this was in an oft used function (or it might not, would need to see if the compiler was smart enough to ignore the initialization). But that initialization is going to happen very rarely in the lifetime of a grub execution instance. I also don't really care either way.

Quoted 1 line

Glenn

I just didn't realize it's a declaration, so keeping the initialization is fine.

Patrick

#195 lore raw

Re: [PATCH v7 03/17] luks2: Remove unused argument in grub_error

In reply to Daniel Kiper
Quoted 7 lines · On Mon, 7 Dec 2020 20:52:44 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn

Nit, my RB should be after your SOB. I will fix it.

Daniel

This is annoying to do with how my git is setup, which is to automatically append my SOB when doing format-patch. Do you just do SOBs and RBs by hand, or have a way to do this more automatically?

Glenn

#205 lore raw

Re: [PATCH v7 05/17] luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

In reply to Patrick Steinhardt
Quoted 65 lines · On Tue, 8 Dec 2020 07:38:31 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 64 lines · Glenn Washburn
Quoted 17 lines · On Mon, 7 Dec 2020 21:02:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 15 lines · Patrick Steinhardt
Quoted 8 lines · Glenn Washburn

This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.

Get rid of idx variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.

Personally, I'd have named them json_slot_idx. But you've already done so much work on improving the code that I don't want this to be the reason to not give an SOB, especially considering that it's a strict improvement anyway. So:

Quoted 5 lines
Quoted 1 line

I can change this to json_slot_idx before committing if Glenn does not object. Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Thanks Patrick for the sentiment. Looking at the luks2 spec, it says:

"JSON objects must have their names formatted as a string that represents a number in the decimal notation (unsigned integer) – for example ”0”, ”1” and must contain attribute _type_. According to the _type_, the implementation decides how to handle (or ignore) such an object. This notation allows mapping to LUKS1 API functions that use an integer as a reference to keyslots objects."

So here, the spec is calling that value a "name", and saying that it must be a string of decimal digits. Looking at the spec, the "name" of the keyslot object does not need to correspond to the index into the array of keyslot areas on disk. However it does in the canonical implementation for use with LUKS1 API functions which require and integer, as suggested in the quote above.

I'd say that these numbers are actually an id for the object of its respective class. In the cryptsetup implementation, the "id" happens to correspond to the index into the binary key area array, but that's needn't be the case. My preference would be to name it "id" and second choice would be just "idx" (since its usually an index into a physical array of key areas or virtual array of segments and digests).

I don't think the "json" part of the name is warranted, because it really has nothing to do with json. The "slot" part is really only applicable to keyslots because digests and segments don't have an equivalent slot aspect. So I suggest we name the struct member names to just "id" instead. And where we just the index of the name-value pair in the json associative array we use "json_idx" as a suffix. So this would mean changing the argument keyslot_idx in luks2_get_keyslot() to keyslot_json_idx. Optionally the local variable "i" in luks2_get_keyslot() and luks2_parse_segment() could be renamed to "json_idx" as well (I don't care either way about this though).

Quoted 7 lines
Quoted 1 line

Glenn

Sounds sensible to me. Based on your reasoning, I'm happy with either "id" or "key". So we may just as well just keep it as-is, as you prefer.

Patrick

On third thought, while I like "id" better because its really a generic id, we use it as an index into the segments and keyslots bitfield of the grub_luks2_digest_t struct. So I'm going to I'm going to choose idx instead of id. If the implementation changes and we choose to support arbitrary object names we can change the member name to something more generic.

Glenn

#209 lore raw

Re: [PATCH v7 06/17] luks2: Use more intuitive slot key instead of index in user messages

In reply to Daniel Kiper
Quoted 16 lines · On Mon, 7 Dec 2020 21:15:33 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 15 lines · Glenn Washburn

Use the slot key name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
Quoted 14 lines
Quoted 11 lines
grub-core/disk/luks2.c +0 -0
index 437c1da07..ea1065bcf 100644
@@ -284,13 +284,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,

grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&d->json_slot_key, &digest, NULL) || grub_json_getchild (&digest, &digest, 0) || luks2_parse_digest (d, &digest))

grub-core/disk/luks2.c +0 -1
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

digest %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c +1 -0
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse
Quoted 1 line

digest index %"PRIuGRUB_SIZE, i);

Does not this change belong to next patch?

Ugh, yes.

Quoted 10 lines
Quoted 8 lines
       if ((d->keyslots & (1 << k->json_slot_key)))
grub-core/disk/luks2.c +0 -1
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for
keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);

I am afraid this does not build. The slot_key seems a remnant from v6.

Hmm, yes, I'm fairly certain the whole patch series builds, which is why I didn't catch this. Some changes got applied to the wrong patches, which is why this happened, I think.

Quoted 66 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, · line 313

Quoted 10 lines
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +1 -1
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for
 digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);

Ditto and below...

Quoted 53 lines
grub-core/disk/luks2.c +0 -0
   return GRUB_ERR_NONE;

}

grub-core/disk/luks2.c +2 -2
@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot
 \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
 continue; }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n",
 i);
+      grub_dprintf ("luks2", "Trying keyslot
 \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); @@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_SIZE" failed: %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot

\"%"PRIuGRUB_UINT64_T"\" failed: %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key,

keyslot.key_size); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_SIZE": %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot

\"%"PRIuGRUB_UINT64_T"\": %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element

of an array
* where each element is either empty or holds a key. */

grub-core/disk/luks2.c +1 -1
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"),

keyslot.slot_key);

Quoted 4 lines
Quoted 2 lines
       candidate_key_len = keyslot.key_size;
       break;

Daniel

Glenn

#196 lore raw

Re: [PATCH v7 03/17] luks2: Remove unused argument in grub_error

In reply to Glenn Washburn
Quoted 9 lines · Glenn Washburn
Quoted 6 lines · On Mon, 7 Dec 2020 20:52:44 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn

Nit, my RB should be after your SOB. I will fix it.

Quoted 7 lines
Quoted 1 line

Daniel

This is annoying to do with how my git is setup, which is to automatically append my SOB when doing format-patch. Do you just do SOBs and RBs by hand, or have a way to do this more automatically?

Glenn

You can for example just use git commit --signoff when creating the commit. There's also git rebase --signoff to add SOBs for rebased patches if they don't have one yet. If RBs come in during review, I typically just git rebase --interactive and r commits to add the trailing RBs.

Patrick

#241 lore raw

PATCH v8 1/18 disk: Rename grub_disk_get_size to grub_disk_native_sectors

In reply to Glenn Washburn

The function grub_disk_get_size is confusingly named because it actually returns a sector count where the sectors are sized in the grub native sector size. Rename to something more appropriate.

---
 grub-core/disk/diskfilter.c        | 12 ++++++------
 grub-core/disk/dmraid_nvidia.c     |  2 +-
 grub-core/disk/efi/efidisk.c       |  2 +-
 grub-core/disk/geli.c              |  6 +++---
 grub-core/disk/ldm.c               |  4 ++--
 grub-core/disk/luks.c              |  2 +-
 grub-core/disk/mdraid1x_linux.c    |  2 +-
 grub-core/disk/mdraid_linux.c      |  2 +-
 grub-core/fs/cbfs.c                | 16 ++++++++--------
 grub-core/fs/nilfs2.c              |  2 +-
 grub-core/fs/zfs/zfs.c             |  4 ++--
 grub-core/kern/disk.c              |  2 +-
 grub-core/kern/mips/arc/init.c     |  2 +-
 grub-core/normal/misc.c            |  6 +++---
 grub-core/osdep/windows/platform.c |  2 +-
 include/grub/disk.h                |  4 ++--
 util/grub-install.c                |  2 +-
 util/grub-probe.c                  |  2 +-
 18 files changed, 37 insertions(+), 37 deletions(-)
grub-core/disk/diskfilter.c +6 -6
index 86557f923..032011566 100644
@@ -148,7 +148,7 @@ scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
 	if (m->disk && m->disk->id == disk->id
 	    && m->disk->dev->id == disk->dev->id
 	    && m->part_start == grub_partition_get_start (disk->partition)
-	    && m->part_size == grub_disk_get_size (disk))
+	    && m->part_size == grub_disk_native_sectors (disk))
 	  return 0;
     }
 
@@ -1190,13 +1190,13 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 
   grub_dprintf ("diskfilter", "Inserting %s (+%lld,%lld) into %s (%s)\n", disk->name,
 		(unsigned long long) grub_partition_get_start (disk->partition),
-		(unsigned long long) grub_disk_get_size (disk),
+		(unsigned long long) grub_disk_native_sectors (disk),
 		array->name, diskfilter->name);
 #ifdef GRUB_UTIL
   grub_util_info ("Inserting %s (+%" GRUB_HOST_PRIuLONG_LONG ",%"
 		  GRUB_HOST_PRIuLONG_LONG ") into %s (%s)\n", disk->name,
 		  (unsigned long long) grub_partition_get_start (disk->partition),
-		  (unsigned long long) grub_disk_get_size (disk),
+		  (unsigned long long) grub_disk_native_sectors (disk),
 		  array->name, diskfilter->name);
   array->driver = diskfilter;
 #endif
@@ -1210,7 +1210,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	struct grub_diskfilter_lv *lv;
 	/* FIXME: Check whether the update time of the superblocks are
 	   the same.  */
-	if (pv->disk && grub_disk_get_size (disk) >= pv->part_size)
+	if (pv->disk && grub_disk_native_sectors (disk) >= pv->part_size)
 	  return GRUB_ERR_NONE;
 	pv->disk = grub_disk_open (disk->name);
 	if (!pv->disk)
@@ -1219,7 +1219,7 @@ insert_array (grub_disk_t disk, const struct grub_diskfilter_pv_id *id,
 	   raid device, we shouldn't change it.  */
 	pv->start_sector -= pv->part_start;
 	pv->part_start = grub_partition_get_start (disk->partition);
-	pv->part_size = grub_disk_get_size (disk);
+	pv->part_size = grub_disk_native_sectors (disk);
 
 #ifdef GRUB_UTIL
 	{
@@ -1311,7 +1311,7 @@ grub_diskfilter_get_pv_from_disk (grub_disk_t disk,
 	if (pv->disk && pv->disk->id == disk->id
 	    && pv->disk->dev->id == disk->dev->id
 	    && pv->part_start == grub_partition_get_start (disk->partition)
-	    && pv->part_size == grub_disk_get_size (disk))
+	    && pv->part_size == grub_disk_native_sectors (disk))
 	  {
 	    if (vg_out)
 	      *vg_out = vg;
grub-core/disk/dmraid_nvidia.c +1 -1
index 060279124..4d2fb04d1 100644
@@ -107,7 +107,7 @@ grub_dmraid_nv_detect (grub_disk_t disk,
     /* Skip partition.  */
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN)
     /* Not raid.  */
     return NULL;
grub-core/disk/efi/efidisk.c +1 -1
index 9e20af70e..f077b5f55 100644
@@ -867,7 +867,7 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
       if (ctx.hd->partition_start == 0
 	  && (ctx.hd->partition_size << (parent->log_sector_size
 					 - GRUB_DISK_SECTOR_BITS))
-	  == grub_disk_get_size (parent))
+	  == grub_disk_native_sectors (parent))
 	{
 	  dev_name = grub_strdup (parent->name);
 	}
grub-core/disk/geli.c +3 -3
index 0175ce4c4..2f34a35e6 100644
@@ -258,7 +258,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
     return NULL;
 
-  sector = grub_disk_get_size (disk);
+  sector = grub_disk_native_sectors (disk);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return NULL;
 
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_sectors = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
@@ -420,7 +420,7 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev)
   if (dev->hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
     return grub_error (GRUB_ERR_BUG, "mdlen is too long");
 
-  sector = grub_disk_get_size (source);
+  sector = grub_disk_native_sectors (source);
   if (sector == GRUB_DISK_SIZE_UNKNOWN || sector == 0)
     return grub_error (GRUB_ERR_BUG, "not a geli");
 
grub-core/disk/ldm.c +2 -2
index 58f8a53e1..912e88255 100644
@@ -821,7 +821,7 @@ grub_ldm_detect (grub_disk_t disk,
 	    /* LDM is never inside a partition.  */
 	    if (!has_ldm || disk->partition)
 	      continue;
-	    sector = grub_disk_get_size (disk);
+	    sector = grub_disk_native_sectors (disk);
 	    if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	      continue;
 	    sector--;
@@ -938,7 +938,7 @@ grub_util_is_ldm (grub_disk_t disk)
 	  /* LDM is never inside a partition.  */
 	  if (!has_ldm || disk->partition)
 	    continue;
-	  sector = grub_disk_get_size (disk);
+	  sector = grub_disk_native_sectors (disk);
 	  if (sector == GRUB_DISK_SIZE_UNKNOWN)
 	    continue;
 	  sector--;
grub-core/disk/luks.c +1 -1
index aa9877b68..5fc79d880 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
+  newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/mdraid1x_linux.c +1 -1
index c980feba4..38444b02c 100644
@@ -111,7 +111,7 @@ grub_mdraid_detect (grub_disk_t disk,
   grub_uint64_t size;
   grub_uint8_t minor_version;
 
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
 
   /* Check for an 1.x superblock.
    * It's always aligned to a 4K boundary
grub-core/disk/mdraid_linux.c +1 -1
index 11024ae31..e40216f51 100644
@@ -190,7 +190,7 @@ grub_mdraid_detect (grub_disk_t disk,
   struct grub_diskfilter_vg *ret;
 
   /* The sector where the mdraid 0.90 superblock is stored, if available.  */
-  size = grub_disk_get_size (disk);
+  size = grub_disk_native_sectors (disk);
   if (size == GRUB_DISK_SIZE_UNKNOWN)
     /* not 0.9x raid.  */
     return NULL;
grub-core/fs/cbfs.c +8 -8
index 857bea991..581215ef1 100644
@@ -148,16 +148,16 @@ grub_cbfs_mount (grub_disk_t disk)
   grub_off_t header_off;
   struct cbfs_header head;
 
-  if (grub_disk_get_size (disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (disk) == GRUB_DISK_SIZE_UNKNOWN)
     goto fail;
 
-  if (grub_disk_read (disk, grub_disk_get_size (disk) - 1,
+  if (grub_disk_read (disk, grub_disk_native_sectors (disk) - 1,
 		      GRUB_DISK_SECTOR_SIZE - sizeof (ptr),
 		      sizeof (ptr), &ptr))
     goto fail;
 
   ptr = grub_cpu_to_le32 (ptr);
-  header_off = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  header_off = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     + (grub_int32_t) ptr;
 
   if (grub_disk_read (disk, 0, header_off,
@@ -171,16 +171,16 @@ grub_cbfs_mount (grub_disk_t disk)
   if (!data)
     goto fail;
 
-  data->cbfs_start = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_start = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - (grub_be_to_cpu32 (head.romsize) - grub_be_to_cpu32 (head.offset));
-  data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS)
+  data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS)
     - grub_be_to_cpu32 (head.bootblocksize);
   data->cbfs_align = grub_be_to_cpu32 (head.align);
 
-  if (data->cbfs_start >= (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
+  if (data->cbfs_start >= (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
     goto fail;
-  if (data->cbfs_end > (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS))
-    data->cbfs_end = (grub_disk_get_size (disk) << GRUB_DISK_SECTOR_BITS);
+  if (data->cbfs_end > (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS))
+    data->cbfs_end = (grub_disk_native_sectors (disk) << GRUB_DISK_SECTOR_BITS);
 
   data->next_hofs = data->cbfs_start;
 
grub-core/fs/nilfs2.c +1 -1
index 7ed148d3b..082326f38 100644
@@ -753,7 +753,7 @@ grub_nilfs2_load_sb (struct grub_nilfs2_data *data)
     partition_size = (grub_le_to_cpu64 (data->sblock.s_dev_size)
 		      >> GRUB_DISK_SECTOR_BITS);
   else
-    partition_size = grub_disk_get_size (disk);
+    partition_size = grub_disk_native_sectors (disk);
   if (partition_size != GRUB_DISK_SIZE_UNKNOWN)
     {
       /* Read second super block. */
grub-core/fs/zfs/zfs.c +2 -2
index 41ef0ff57..b6e1e178d 100644
@@ -1175,7 +1175,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
   desc.original = original;
 
   /* Don't check back labels on CDROM.  */
-  if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+  if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
     vdevnum = VDEV_LABELS / 2;
 
   for (label = 0; ubbest == NULL && label < vdevnum; label++)
@@ -1184,7 +1184,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,
 	= label * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT)
 	+ ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT)
 	+ (label < VDEV_LABELS / 2 ? 0 : 
-	   ALIGN_DOWN (grub_disk_get_size (dev->disk), sizeof (vdev_label_t))
+	   ALIGN_DOWN (grub_disk_native_sectors (dev->disk), sizeof (vdev_label_t))
 	   - VDEV_LABELS * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT));
 
       /* Read in the uberblock ring (128K). */
grub-core/kern/disk.c +1 -1
index ffb09c8ee..e1b0e073e 100644
@@ -533,7 +533,7 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
 }
 
 grub_uint64_t
-grub_disk_get_size (grub_disk_t disk)
+grub_disk_native_sectors (grub_disk_t disk)
 {
   if (disk->partition)
     return grub_partition_get_len (disk->partition);
grub-core/kern/mips/arc/init.c +1 -1
index 3834a1490..2ed3ff319 100644
@@ -430,7 +430,7 @@ grub_machine_get_bootlocation (char **device, char **path)
     }
 
   if (poff == 0
-      && pend == grub_disk_get_size (parent))
+      && pend == grub_disk_native_sectors (parent))
     {
       grub_disk_close (parent);
       *device = dname;
grub-core/normal/misc.c +3 -3
index 435cd9234..8bb6da31f 100644
@@ -176,14 +176,14 @@ grub_normal_print_device_info (const char *name)
 		     (grub_partition_get_start (dev->disk->partition) & 1) ? ".5" : "" );
       else
 	grub_printf_ (N_(" - Sector size %uB"), 1 << dev->disk->log_sector_size);
-      if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
+      if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
 	grub_puts_ (N_(" - Total size unknown"));
       else
 	grub_printf (_(" - Total size %llu%sKiB"),
-		     (unsigned long long) (grub_disk_get_size (dev->disk) >> 1),
+		     (unsigned long long) (grub_disk_native_sectors (dev->disk) >> 1),
 		     /* TRANSLATORS: Replace dot with appropriate decimal separator for
 			your language.  */
-		     (grub_disk_get_size (dev->disk) & 1) ? _(".5") : "");
+		     (grub_disk_native_sectors (dev->disk) & 1) ? _(".5") : "");
     }
 
   if (dev)
grub-core/osdep/windows/platform.c +1 -1
index 1ef86bf58..253f8d101 100644
@@ -371,7 +371,7 @@ grub_install_register_efi (grub_device_t efidir_grub_dev,
 
   hddp->partition_start = grub_partition_get_start (efidir_grub_dev->disk->partition)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
-  hddp->partition_size = grub_disk_get_size (efidir_grub_dev->disk)
+  hddp->partition_size = grub_disk_native_sectors (efidir_grub_dev->disk)
     >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
 
   pathptr = hddp + 1;
include/grub/disk.h +2 -2
index 132a1bb75..0fb727d3d 100644
@@ -171,7 +171,7 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;
 
 #define GRUB_DISK_MAX_MAX_AGGLOMERATE ((1 << (30 - GRUB_DISK_CACHE_BITS - GRUB_DISK_SECTOR_BITS)) - 1)
 
-/* Return value of grub_disk_get_size() in case disk size is unknown. */
+/* Return value of grub_disk_native_sectors() in case disk size is unknown. */
 #define GRUB_DISK_SIZE_UNKNOWN	 0xffffffffffffffffULL
 
 /* Convert to GRUB native disk sized sector from disk sized sector. */
@@ -219,7 +219,7 @@ extern grub_err_t (*EXPORT_VAR(grub_disk_write_weak)) (grub_disk_t disk,
 						       const void *buf);
 
 
-grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk);
+grub_uint64_t EXPORT_FUNC(grub_disk_native_sectors) (grub_disk_t disk);
 
 #if DISK_CACHE_STATS
 void
util/grub-install.c +1 -1
index a35a2e2e8..f9f8ee7b2 100644
@@ -748,7 +748,7 @@ is_prep_empty (grub_device_t dev)
   grub_disk_addr_t dsize, addr;
   grub_uint32_t buffer[32768];
 
-  dsize = grub_disk_get_size (dev->disk);
+  dsize = grub_disk_native_sectors (dev->disk);
   for (addr = 0; addr < dsize;
        addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
     {
util/grub-probe.c +1 -1
index cbe6ed94c..c08e46bbb 100644
@@ -398,7 +398,7 @@ probe (const char *path, char **device_names, char delim)
 	  if (! dev || !dev->disk)
 	    grub_util_error ("%s", grub_errmsg);
 
-	  dsize = grub_disk_get_size (dev->disk);
+	  dsize = grub_disk_native_sectors (dev->disk);
 	  for (addr = 0; addr < dsize;
 	       addr += sizeof (buffer) / GRUB_DISK_SECTOR_SIZE)
 	    {
Signature · 1 line
2.27.0
#244 lore raw

PATCH v8 3/18 whitespace: convert 8 spaces to tabs

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index d96764a02..66e3b8798 100644
@@ -276,8 +276,8 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
-          grub_json_getchild (&digest, &digest, 0) ||
-          luks2_parse_digest (d, &digest))
+	  grub_json_getchild (&digest, &digest, 0) ||
+	  luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << idx)))
@@ -295,7 +295,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
-          luks2_parse_segment (s, &segment))
+	  luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << idx)))
@@ -600,7 +600,7 @@ luks2_recover_key (grub_disk_t source,
 	{
 	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
 	  continue;
-        }
+	}
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
 
Signature · 1 line
2.27.0
#250 lore raw

PATCH v8 7/18 luks2: Use more intuitive object name instead of json index in user messages

In reply to Glenn Washburn

Use the object name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
grub-core/disk/luks2.c +9 -9
index 67b1823d3..b6930b2f5 100644
@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->idx);
 
   return GRUB_ERR_NONE;
 }
@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.idx);
 	  continue;
 	}
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.idx);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
+			keyslot.idx, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
+			keyslot.idx, grub_errmsg);
 	  continue;
 	}
 
@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.idx);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#240 lore raw

v8 cover Cryptodisk fixes for v2.06 redux

In reply to 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

 grub-core/disk/cryptodisk.c        |  64 ++++++----
 grub-core/disk/diskfilter.c        |  12 +-
 grub-core/disk/dmraid_nvidia.c     |   2 +-
 grub-core/disk/efi/efidisk.c       |   2 +-
 grub-core/disk/geli.c              |   6 +-
 grub-core/disk/ldm.c               |   4 +-
 grub-core/disk/luks.c              |   7 +-
 grub-core/disk/luks2.c             | 184 ++++++++++++++++++++++-------
 grub-core/disk/mdraid1x_linux.c    |   2 +-
 grub-core/disk/mdraid_linux.c      |   2 +-
 grub-core/fs/cbfs.c                |  16 +--
 grub-core/fs/nilfs2.c              |   2 +-
 grub-core/fs/zfs/zfs.c             |   4 +-
 grub-core/kern/compiler-rt.c       |   2 +-
 grub-core/kern/disk.c              |   2 +-
 grub-core/kern/mips/arc/init.c     |   2 +-
 grub-core/normal/misc.c            |   6 +-
 grub-core/osdep/windows/platform.c |   2 +-
 include/grub/compiler-rt.h         |   2 +-
 include/grub/cryptodisk.h          |   8 +-
 include/grub/disk.h                |  21 +++-
 include/grub/misc.h                |   7 +-
 include/grub/types.h               |   9 ++
 util/grub-install.c                |   2 +-
 util/grub-probe.c                  |   2 +-
 25 files changed, 261 insertions(+), 111 deletions(-)

Range-diff against v7:
1: a80929b15 ! 1: f4062e4f6 disk: Rename grub_disk_get_size to grub_disk_native_sectors
@@ Commit message
size. Rename to something more appropriate.

+ Reviewed-by: Daniel Kiper <[email protected]>
+ Reviewed-by: Patrick Steinhardt <[email protected]>

## grub-core/disk/diskfilter.c ##
@@ grub-core/disk/diskfilter.c: scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
2: d4b462fd3 ! 2: 17b1ef195 misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
@@ Commit message
This ensures that expected order of operations is preserved when arguments
are expressions.

+ Reviewed-by: Daniel Kiper <[email protected]>
+ Reviewed-by: Patrick Steinhardt <[email protected]>
+
## include/grub/misc.h ##
@@
#include <grub/compiler.h>

 -:  --------- >  3:  eb8625d9f whitespace: convert 8 spaces to tabs
 3:  d1a23ff0a !  4:  0f28628e2 luks2: Remove unused argument in grub_error

@@ Commit message
luks2: Remove unused argument in grub_error

+ Reviewed-by: Patrick Steinhardt <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
4: 61fcebc1a ! 5: 705548bc2 luks2: Make sure all fields of output argument in luks2_parse_digest() are written to
@@ Commit message
Otherwise, the digest could say it belongs to keyslots and segments that it
does not.

+ Reviewed-by: Daniel Kiper <[email protected]>
+ Reviewed-by: Patrick Steinhardt <[email protected]>
+
## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
return grub_error (GRUB_ERR_BAD_ARGUMENT,
5: f6a6befdb ! 6: 56d4c397a luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest

    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Add json_slot_key member to struct grub_luks2_keyslot/segment/digest
    +    luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
     
         This allows code using these structs to know the named key associated with
         these json data structures. In the future we can use these to provide better
         error messages to the user.
     
    -    Get rid of idx variable in luks2_get_keyslot() which was overloaded to be
    -    used for both keyslot and segment slot keys.
    +    Get rid of idx local variable in luks2_get_keyslot() which was overloaded to
    +    be used for both keyslot and segment slot keys.
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
      struct grub_luks2_keyslot
      {
     +  /* The integer key to the associative array of keyslots */
    -+  grub_uint64_t json_slot_key;
    ++  grub_uint64_t idx;
        grub_int64_t key_size;
        grub_int64_t priority;
        struct
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
      
      struct grub_luks2_segment
      {
    -+  grub_uint64_t json_slot_key;
    ++  grub_uint64_t idx;
        grub_uint64_t offset;
        const char	*size;
        const char	*encryption;
    @@ grub-core/disk/luks2.c: typedef struct grub_luks2_segment grub_luks2_segment_t;
      
      struct grub_luks2_digest
      {
    -+  grub_uint64_t json_slot_key;
    ++  grub_uint64_t idx;
        /* Both keyslots and segments are interpreted as bitfields here */
        grub_uint64_t	keyslots;
        grub_uint64_t	segments;
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        if (grub_json_getvalue (&keyslots, root, "keyslots") ||
            grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
     -      grub_json_getuint64 (&idx, &keyslot, NULL) ||
    -+      grub_json_getuint64 (&k->json_slot_key, &keyslot, NULL) ||
    ++      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_idx);
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
        for (i = 0; i < size; i++)
          {
            if (grub_json_getchild (&digest, &digests, i) ||
    -+	  grub_json_getuint64 (&d->json_slot_key, &digest, NULL) ||
    -           grub_json_getchild (&digest, &digest, 0) ||
    -           luks2_parse_digest (d, &digest))
    ++	  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, i);
      
     -      if ((d->keyslots & (1 << idx)))
    -+      if ((d->keyslots & (1 << k->json_slot_key)))
    ++      if ((d->keyslots & (1 << k->idx)))
      	break;
          }
        if (i == size)
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
          {
            if (grub_json_getchild (&segment, &segments, i) ||
     -	  grub_json_getuint64 (&idx, &segment, NULL) ||
    -+	  grub_json_getuint64 (&s->json_slot_key, &segment, NULL) ||
    ++	  grub_json_getuint64 (&s->idx, &segment, NULL) ||
      	  grub_json_getchild (&segment, &segment, 0) ||
    -           luks2_parse_segment (s, &segment))
    + 	  luks2_parse_segment (s, &segment))
      	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
      
     -      if ((d->segments & (1 << idx)))
    -+      if ((d->segments & (1 << s->json_slot_key)))
    ++      if ((d->segments & (1 << s->idx)))
      	break;
          }
        if (i == size)
 6:  5c53328e4 !  7:  1991eca5a luks2: Use more intuitive slot key instead of index in user messages
    @@ Metadata
     Author: Glenn Washburn <[email protected]>
     
      ## Commit message ##
    -    luks2: Use more intuitive slot key instead of index in user messages
    +    luks2: Use more intuitive object name instead of json index in user messages
     
    -    Use the slot key name in the json array rather than the 0 based index in the
    +    Use the object name in the json array rather than the 0 based index in the
         json array for keyslots, segments, and digests. This is less confusing for
         the end user. For example, say you have a LUKS2 device with a key in slot 1
         and slot 4. When using the password for slot 4 to unlock the device, the
    @@ Commit message
     
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	  grub_json_getuint64 (&d->json_slot_key, &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, i);
    -+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
    - 
    -       if ((d->keyslots & (1 << k->json_slot_key)))
      	break;
          }
        if (i == size)
     -      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
    -+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
    ++      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->idx);
      
        /* Get segment that matches the digest. */
        if (grub_json_getvalue (&segments, root, "segments") ||
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
          }
        if (i == size)
     -    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
    -+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
    ++    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->idx);
      
        return GRUB_ERR_NONE;
      }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.idx);
      	  continue;
    -         }
    + 	}
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.idx);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     -	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
     +	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
    -+			keyslot.slot_key, grub_errmsg);
    ++			keyslot.idx, grub_errmsg);
      	  continue;
      	}
      
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     -	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
     +	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
    -+			keyslot.slot_key, grub_errmsg);
    ++			keyslot.idx, grub_errmsg);
      	  continue;
      	}
      
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
    ++      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.idx);
      
            candidate_key_len = keyslot.key_size;
            break;
 7:  a4e5e65de <  -:  --------- luks2: Add string "index" to user strings using a json index.
 -:  --------- >  8:  c1af2d286 luks2: Rename json index variables to names that they are obviously json indexes
 -:  --------- >  9:  984166770 luks2: Add string "index" to user strings using a json index.
 8:  d143d8eac = 10:  acc4a205c cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
 9:  905cb3912 ! 11:  c39083e56 cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

@@ include/grub/types.h: typedef grub_int32_t grub_ssize_t;
# 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. ++ * 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

10:  5dfb095fd = 12:  dee584996 luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors
11:  78949263b = 13:  06cd1951f cryptodisk: Properly handle non-512 byte sized sectors
12:  f4277024e ! 14:  9fbd815f9 luks2: Better error handling when setting up the cryptodisk
    @@ Commit message
         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",
    -
    -    Check for errors from grub_strtoull when converting segment size from
    +    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 so long as the
    -    source disk's size is greater than this segment size. Otherwise, this is
    -    an invalid segment, and continue on to the next key.
    +    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 ##
    -@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	break;
    -     }
    -   if (i == size)
    --      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
    -+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->json_slot_key);
    - 
    -   /* Get segment that matches the digest. */
    -   if (grub_json_getvalue (&segments, root, "segments") ||
    -@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
    - 	break;
    -     }
    -   if (i == size)
    --    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->slot_key);
    -+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->json_slot_key);
    - 
    -   return GRUB_ERR_NONE;
    - }
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            goto err;
          }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +    }
     +
        /* Try all keyslot */
    -   for (i = 0; i < size; i++)
    +   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, i);
    +       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);
            if (ret)
      	goto err;
     +      if (grub_errno != GRUB_ERR_NONE)
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
            if (keyslot.priority == 0)
      	{
    --	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.slot_key);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot \"%"PRIuGRUB_UINT64_T"\" due to priority\n", keyslot.json_slot_key);
    - 	  continue;
    -         }
    - 
    --      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.slot_key);
    -+      grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
    - 
    -       /* Set up disk according to keyslot's segment. */
    +@@ grub-core/disk/luks2.c: 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;
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " %"PRIuGRUB_UINT64_T" which is greater than"
     +				 " source disk size %"PRIuGRUB_UINT64_T","
     +				 " skipping\n",
    -+				 segment.json_slot_key, crypt->offset_sectors,
    ++				 segment.idx, crypt->offset_sectors,
     +				 max_crypt_sectors);
     +	  continue;
     +	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	    {
     +	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
     +				     " \"%s\" is not a parsable number\n",
    -+				     segment.json_slot_key, segment.size);
    ++				     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,
     +				     " %s overflowed 64-bit unsigned integer,"
     +				     " the end of the crypto device will be"
     +				     " inaccessible\n",
    -+				     segment.json_slot_key, segment.size);
    ++				     segment.idx, segment.size);
     +	      if (crypt->total_sectors > max_crypt_sectors)
     +		crypt->total_sectors = max_crypt_sectors;
     +	    }
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	{
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has zero"
     +				 " sectors, skipping\n",
    -+				 segment.json_slot_key);
    ++				 segment.idx);
     +	  continue;
     +	}
     +      else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors))
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +				 " data position greater than source disk size,"
     +				 " the end of the crypto device will be"
     +				 " inaccessible\n",
    -+				 segment.json_slot_key);
    ++				 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));
    -       if (ret)
    - 	{
    - 	  grub_dprintf ("luks2", "Decryption with keyslot \"%"PRIuGRUB_UINT64_T"\" failed: %s\n",
    --			keyslot.slot_key, grub_errmsg);
    -+			keyslot.json_slot_key, grub_errmsg);
    - 	  continue;
    - 	}
    - 
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    -       if (ret)
    - 	{
    - 	  grub_dprintf ("luks2", "Could not open keyslot \"%"PRIuGRUB_UINT64_T"\": %s\n",
    --			keyslot.slot_key, grub_errmsg);
    -+			keyslot.json_slot_key, grub_errmsg);
    - 	  continue;
    - 	}
    - 
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
    -        * TRANSLATORS: It's a cryptographic key slot: one element of an array
    -        * where each element is either empty or holds a key.
    -        */
    --      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.slot_key);
    -+      grub_printf_ (N_("Slot \"%"PRIuGRUB_UINT64_T"\" opened\n"), keyslot.json_slot_key);
    - 
    -       candidate_key_len = keyslot.key_size;
    -       break;
     
      ## include/grub/disk.h ##
     @@
13:  65255f2d4 ! 15:  09c5b15f3 luks2: Error check segment.sector_size
    @@ Metadata
      ## Commit message ##
         luks2: Error check segment.sector_size
     
    -    Reviewed-by: Daniel Kiper <[email protected]>
    -
      ## grub-core/disk/luks2.c ##
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
      
    -       grub_dprintf ("luks2", "Trying keyslot \"%"PRIuGRUB_UINT64_T"\"\n", keyslot.json_slot_key);
    +       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 ||
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
     +	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
     +				 " size %"PRIuGRUB_UINT64_T" is not one of"
     +				 " 512, 1024, 2048, or 4096\n",
    -+				 segment.json_slot_key, segment.sector_size);
    ++				 segment.idx, segment.sector_size);
     +	  continue;
     +	}
     +
14:  20357ebd6 <  -:  --------- whitespace: convert 8 spaces to tabs
15:  600b89d70 = 16:  60e37ca9f mips: Enable __clzdi2()
16:  a2d394c95 = 17:  302862ec1 misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
17:  68e0dac97 = 18:  baa1de127 luks2: Use grub_log2ull to calculate log_sector_size and improve readability
Signature · 1 line
2.27.0
#245 lore raw

PATCH v8 4/18 luks2: Remove unused argument in grub_error

---
 grub-core/disk/luks2.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
index 66e3b8798..402926680 100644
@@ -200,7 +200,7 @@ luks2_parse_segment (grub_luks2_segment_t *out, const grub_json_t *segment)
       grub_json_getstring (&out->size, segment, "size") ||
       grub_json_getstring (&out->encryption, segment, "encryption") ||
       grub_json_getint64 (&out->sector_size, segment, "sector_size"))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters", type);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing segment parameters");
 
   return GRUB_ERR_NONE;
 }
@@ -228,7 +228,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no segments", type);
+		       "Digest references no segments");
 
   for (i = 0; i < size; i++)
     {
@@ -240,7 +240,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
   if (grub_json_getsize (&size, &keyslots))
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
-		       "Digest references no keyslots", type);
+		       "Digest references no keyslots");
 
   for (i = 0; i < size; i++)
     {
Signature · 1 line
2.27.0
#253 lore raw

PATCH v8 8/18 luks2: Rename json index variables to names that they are obviously json indexes

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
grub-core/disk/luks2.c +15 -15
index b6930b2f5..7e0419a82 100644
@@ -261,53 +261,53 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t keyslot_idx)
+		   const grub_json_t *root, grub_size_t keyslot_json_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t i, size;
+  grub_size_t json_idx, size;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_json_idx) ||
       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_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_json_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (i = 0; i < size; i++)
+  for (json_idx = 0; json_idx < size; json_idx++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, json_idx) ||
 	  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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, json_idx);
 
       if ((d->keyslots & (1 << k->idx)))
 	break;
     }
-  if (i == size)
+  if (json_idx == size)
       return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (i = 0; i < size; i++)
+  for (json_idx = 0; json_idx < size; json_idx++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, json_idx) ||
 	  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, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, json_idx);
 
       if ((d->segments & (1 << s->idx)))
 	break;
     }
-  if (i == size)
+  if (json_idx == size)
     return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", d->idx);
 
   return GRUB_ERR_NONE;
@@ -542,7 +542,7 @@ luks2_recover_key (grub_disk_t source,
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
   char passphrase[MAX_PASSPHRASE], cipher[32];
   char *json_header = NULL, *part = NULL, *ptr;
-  grub_size_t candidate_key_len = 0, i, size;
+  grub_size_t candidate_key_len = 0, json_idx, size;
   grub_luks2_header_t header;
   grub_luks2_keyslot_t keyslot;
   grub_luks2_digest_t digest;
@@ -596,9 +596,9 @@ luks2_recover_key (grub_disk_t source,
     }
 
   /* Try all keyslot */
-  for (i = 0; i < size; i++)
+  for (json_idx = 0; json_idx < size; json_idx++)
     {
-      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
+      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);
       if (ret)
 	goto err;
 
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
#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
#246 lore raw

PATCH v8 5/18 luks2: Make sure all fields of output argument in luks2_parse_digest() are written to

In reply to Glenn Washburn

We should assume that the output argument "out" is uninitialized and could have random data. So, make sure to initialize the segments and keyslots bit fields because potentially not all bits of those fields are written to. Otherwise, the digest could say it belongs to keyslots and segments that it does not.

---
 grub-core/disk/luks2.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/disk/luks2.c +2 -0
index 402926680..9b19d35c1 100644
@@ -230,6 +230,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no segments");
 
+  out->segments = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &segments, i) ||
@@ -242,6 +243,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
 		       "Digest references no keyslots");
 
+  out->keyslots = 0;
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&o, &keyslots, i) ||
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