Skip to contentSkip to conversation

cryptodisk: Support encrypted volumes using detached headers on a partition

PATCH In review Glenn Washburn 3 participants 4 messages Aug 7, 2022 – Aug 9, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cryptodisk: Support encrypted volumes using detached headers on a partition

Update the read hook to take into account encrypted volumes on a partition. Grub disk read hooks supply an absolute sector number at which the read is started from. If the encrypted volume is in a partition, the sector number given to the read hook will be offset by the number of the sector at the start of the partition. The read hook then needs to subtract the partition start from the supplied sector to get the correct start sector for the read into the detached header file.

---
 grub-core/disk/cryptodisk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +3 -1
index f1fe0d390..e89430812 100644
@@ -64,6 +64,7 @@ static const struct grub_arg_option options[] =
 struct cryptodisk_read_hook_ctx
 {
   grub_file_t hdr_file;
+  grub_disk_addr_t part_start;
 };
 typedef struct cryptodisk_read_hook_ctx *cryptodisk_read_hook_ctx_t;
 
@@ -1022,7 +1023,7 @@ cryptodisk_read_hook (grub_disk_addr_t sector, unsigned offset,
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("header file not found"));
 
   if (grub_file_seek (ctx->hdr_file,
-		      (sector * GRUB_DISK_SECTOR_SIZE) + offset)
+		      ((sector - ctx->part_start) * GRUB_DISK_SECTOR_SIZE) + offset)
       == (grub_off_t) -1)
     return grub_errno;
 
@@ -1078,6 +1079,7 @@ grub_cryptodisk_scan_device_real (const char *name,
        * times by a backend. This is fine because of the assumptions mentioned
        * and the read hook reads from absolute offsets and is stateless.
        */
+      read_hook_data.part_start = grub_partition_get_start (source->partition);
       read_hook_data.hdr_file = cargs->hdr_file;
       source->read_hook = cryptodisk_read_hook;
       source->read_hook_data = (void *) &read_hook_data;
Signature · 1 line
2.34.1
#1 lore raw

This patch fixed the issue I had and now makes it possible to decrypt a partition with cryptomount when using a detached header.

Glenn, thanks for the patience with my not always efficient approach, but at least I reported something that needed a fix ;-)

Van: Glenn Washburn <[email protected]>
Aan: [email protected];
   Daniel Kiper <[email protected]>
Onderwerp: [PATCH] cryptodisk: Support encrypted volumes using detached headers on a partition
Datum: 07/08/2022 07:18:52 Europe/Paris
Cc: [email protected];
   Glenn Washburn <[email protected]>

Update the read hook to take into account encrypted volumes on a partition. Grub disk read hooks supply an absolute sector number at which the read is started from. If the encrypted volume is in a partition, the sector number given to the read hook will be offset by the number of the sector at the start of the partition. The read hook then needs to subtract the partition start from the supplied sector to get the correct start sector for the read into the detached header file.

---
grub-core/disk/cryptodisk.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +0 -0
index f1fe0d390..e89430812 100644
@@ -64,6 +64,7 @@ static const struct grub_arg_option options[] =

struct cryptodisk_read_hook_ctx
{
grub_file_t hdr_file;
+ grub_disk_addr_t part_start;
};
typedef struct cryptodisk_read_hook_ctx *cryptodisk_read_hook_ctx_t;

@@ -1022,7 +1023,7 @@ cryptodisk_read_hook (grub_disk_addr_t sector, unsigned offset, return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("header file not found"));

if (grub_file_seek (ctx->hdr_file,
- (sector * GRUB_DISK_SECTOR_SIZE) + offset)
+ ((sector - ctx->part_start) * GRUB_DISK_SECTOR_SIZE) + offset) == (grub_off_t) -1)
return grub_errno;

@@ -1078,6 +1079,7 @@ grub_cryptodisk_scan_device_real (const char *name,
* times by a backend. This is fine because of the assumptions mentioned
* and the read hook reads from absolute offsets and is stateless. */
+ read_hook_data.part_start = grub_partition_get_start (source->partition); read_hook_data.hdr_file = cargs->hdr_file;
source->read_hook = cryptodisk_read_hook; source->read_hook_data = (void *) &read_hook_data;

Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by Tested-by brutser on v1 1/1
#2 lore raw
In reply to Glenn Washburn
Quoted 9 lines · Glenn Washburn

Update the read hook to take into account encrypted volumes on a partition. Grub disk read hooks supply an absolute sector number at which the read is started from. If the encrypted volume is in a partition, the sector number given to the read hook will be offset by the number of the sector at the start of the partition. The read hook then needs to subtract the partition start from the supplied sector to get the correct start sector for the read into the detached header file.

brutser, may I add on your behalf:

Glenn, thank you for fixing this issue!

Daniel

Reviewed-by Daniel Kiper Tested-by
#3 lore raw

Hi Daniel! No problem, I will do some more testing end of this week with different setups.

Van: Daniel Kiper <[email protected]>
Aan: Glenn Washburn <[email protected]>
Onderwerp: Re: [PATCH] cryptodisk: Support encrypted volumes using detached headers on a partition
Datum: 09/08/2022 15:17:59 Europe/Paris
Cc: [email protected];
   [email protected]

Quoted 9 lines · Glenn Washburn

Update the read hook to take into account encrypted volumes on a partition. Grub disk read hooks supply an absolute sector number at which the read is started from. If the encrypted volume is in a partition, the sector number given to the read hook will be offset by the number of the sector at the start of the partition. The read hook then needs to subtract the partition start from the supplied sector to get the correct start sector for the read into the detached header file.

brutser, may I add on your behalf:

Glenn, thank you for fixing this issue!

Daniel