All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabian Vogt <fvogt@suse.de>
To: grub-devel@gnu.org, Josselin Poiret <dev@jpoiret.xyz>
Cc: Patrick Steinhardt <ps@pks.im>, Michael Chang <mchang@suse.com>,
	Pierre-Louis Bonicoli <pierre-louis.bonicoli@libregerbil.fr>,
	aplanas@suse.de, Glenn Washburn <development@efficientek.com>
Subject: [PATCH v3] disk/cryptodisk: When cheatmounting, use the sector info of the cheat device
Date: Tue, 14 Jun 2022 15:55:21 +0200	[thread overview]
Message-ID: <2848907.e9J7NaK4W3@linux-e202.suse.de> (raw)
In-Reply-To: <20220613211942.76d843f7@crass-HP-ZBook-15-G2>

When using grub-probe with cryptodisk, the mapped block device from the host
is used directly instead of decrypting the source device in GRUB code.
In that case, the sector size and count of the host device needs to be used.
This is especially important when using luks2, which does not assign
total_sectors and log_sector_size when scanning, but only later when the
segments in the JSON area are evaluated. With an unset log_sector_size,
grub_open_device complains.

This fixes grub-probe failing with
"error: sector sizes of 1 bytes aren't supported yet."

Signed-off-by: Fabian Vogt <fvogt@suse.de>
---
v2: Moved new code from grub_cryptodisk_cheat_mount to grub_cryptodisk_open,
    which allowed to simplify the code a bit. Also improved error handling.
v3: More liberal placement of variable declarations.

 grub-core/disk/cryptodisk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index c80cf9907..b3c76ef57 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -709,16 +709,31 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
   if (!dev)
     return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device");
 
-  disk->log_sector_size = dev->log_sector_size;
-
 #ifdef GRUB_UTIL
   if (dev->cheat)
     {
+      grub_uint64_t cheat_dev_size;
+      unsigned int cheat_log_sector_size;
+
       if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd))
 	dev->cheat_fd = grub_util_fd_open (dev->cheat, GRUB_UTIL_FD_O_RDONLY);
       if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd))
 	return grub_error (GRUB_ERR_IO, N_("cannot open `%s': %s"),
 			   dev->cheat, grub_util_fd_strerror ());
+
+      /* Use the sector size and count of the cheat device */
+      cheat_dev_size = grub_util_get_fd_size (dev->cheat_fd, dev->cheat, &cheat_log_sector_size);
+      if (cheat_dev_size == -1)
+        {
+          const char *errmsg = grub_util_fd_strerror ();
+          grub_util_fd_close (dev->cheat_fd);
+          dev->cheat_fd = GRUB_UTIL_FD_INVALID;
+          return grub_error (GRUB_ERR_IO, N_("failed to query size of device `%s': %s"),
+                             dev->cheat, errmsg);
+        }
+
+      dev->log_sector_size = cheat_log_sector_size;
+      dev->total_sectors = cheat_dev_size >> cheat_log_sector_size;
     }
 #endif
 
@@ -732,6 +747,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
+  disk->log_sector_size = dev->log_sector_size;
   disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
-- 
2.36.1






  reply	other threads:[~2022-06-14 13:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-30 12:25 [PATCH 0/4] Probing support for LUKS2 Patrick Steinhardt
2020-05-30 12:25 ` [PATCH 1/4] luks: fix out-of-bounds copy of UUID Patrick Steinhardt
2020-06-06 23:32   ` Petr Vorel
2020-05-30 12:25 ` [PATCH 2/4] luks2: strip dashes off of the UUID Patrick Steinhardt
2020-09-15 14:30   ` Daniel Kiper
2020-05-30 12:25 ` [PATCH 3/4] luks2: set up dummy sector size during scan Patrick Steinhardt
2021-08-06  4:51   ` Michael Chang
2021-08-08 14:20     ` Patrick Steinhardt
2021-12-16 15:52       ` Fabian Vogt
2021-12-22 18:17         ` Josselin Poiret
2022-02-04 15:46           ` Fabian Vogt
2022-02-07 13:15             ` Josselin Poiret
2022-05-21  0:13               ` Glenn Washburn
2022-05-21 10:53                 ` Fabian Vogt
2022-06-13 14:29                 ` [PATCH v2] disk/cryptodisk: When cheatmounting, use the sector info of the cheat device Fabian Vogt
2022-06-14  2:19                   ` Glenn Washburn
2022-06-14 13:55                     ` Fabian Vogt [this message]
2022-06-14 18:18                       ` [PATCH v3] " Glenn Washburn
2022-06-21 15:40                       ` Patrick Steinhardt
2022-08-11 18:22                       ` Glenn Washburn
2020-05-30 12:25 ` [PATCH 4/4] osdep: detect LUKS2-encrypted devices Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2848907.e9J7NaK4W3@linux-e202.suse.de \
    --to=fvogt@suse.de \
    --cc=aplanas@suse.de \
    --cc=dev@jpoiret.xyz \
    --cc=development@efficientek.com \
    --cc=grub-devel@gnu.org \
    --cc=mchang@suse.com \
    --cc=pierre-louis.bonicoli@libregerbil.fr \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.