All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josselin Poiret <dev@jpoiret.xyz>
To: Glenn Washburn <development@efficientek.com>
Cc: grub-devel@gnu.org, Josselin Poiret <dev@jpoiret.xyz>
Subject: [PATCH v2 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters
Date: Sat, 11 Dec 2021 13:29:45 +0100	[thread overview]
Message-ID: <20211211122945.6326-3-dev@jpoiret.xyz> (raw)
In-Reply-To: <20211211122945.6326-1-dev@jpoiret.xyz>

This lets a LUKS2 cryptodisk have all the cipher, hash, and sizes
filled out, otherwise they wouldn't be initialized if cheat mounted.
---
 grub-core/osdep/devmapper/getroot.c | 99 ++++++++++++++++++++++++++++-
 1 file changed, 98 insertions(+), 1 deletion(-)

diff --git a/grub-core/osdep/devmapper/getroot.c b/grub-core/osdep/devmapper/getroot.c
index ad1daf9c8..4b3458639 100644
--- a/grub-core/osdep/devmapper/getroot.c
+++ b/grub-core/osdep/devmapper/getroot.c
@@ -51,6 +51,8 @@
 #include <grub/emu/misc.h>
 #include <grub/emu/hostdisk.h>
 
+#include <grub/cryptodisk.h>
+
 static int
 grub_util_open_dm (const char *os_dev, struct dm_tree **tree,
 		   struct dm_tree_node **node)
@@ -183,7 +185,6 @@ grub_util_pull_devmapper (const char *os_dev)
       && lastsubdev)
     {
       char *grdev = grub_util_get_grub_dev (lastsubdev);
-      dm_tree_free (tree);
       if (grdev)
 	{
 	  grub_err_t err;
@@ -191,7 +192,103 @@ grub_util_pull_devmapper (const char *os_dev)
 	  if (err)
 	    grub_util_error (_("can't mount encrypted volume `%s': %s"),
 			     lastsubdev, grub_errmsg);
+          if (strncmp (uuid, "CRYPT-LUKS2-", sizeof ("CRYPT-LUKS2-") - 1) == 0)
+            {
+              /* set LUKS2 cipher and size from dm parameter, since it is not
+               * possible to determine the correct ones without unlocking, as
+               * there might be multiple segments.
+               */
+              grub_disk_t source = grub_disk_open (grdev);
+              grub_cryptodisk_t cryptodisk = grub_cryptodisk_get_by_source_disk (source);
+              grub_disk_close (source);
+              grub_addr_t start, length;
+              char *target_type = NULL;
+              char *params;
+              const char *name = dm_tree_node_get_name (node);
+              char *cipher, *cipher_mode;
+              struct dm_task *dmt;
+              grub_util_info ("populating parameters of cryptomount `%s' from DM device `%s'",
+                              uuid, name);
+              if (!(dmt = dm_task_create (DM_DEVICE_TABLE)))
+                grub_util_error (_("can't create dm task DM_DEVICE_TABLE"));
+              if (!dm_task_set_name (dmt, name))
+                grub_util_error (_("can't set dm task name to `%s'"), name);
+              if (!dm_task_run (dmt))
+                grub_util_error (_("can't run dm task for `%s'"), name);
+              dm_get_next_target (dmt, NULL, &start, &length,
+                                  &target_type, &params);
+              if (strncmp (target_type, "crypt", sizeof ("crypt")) != 0)
+                grub_util_error (_("dm target is not `crypt'"));
+
+              cryptodisk->total_sectors = length;
+              cryptodisk->log_sector_size = 9; /* default dm sector size */
+
+              /* dm target parameters for dm-crypt is
+               * <cipher> <key> <iv_offset> <device path> <offset> [<#opt_params> <opt_params>]
+               */
+              char *seek_head, *c;
+              c = params;
+
+              /* first, get the cipher name from the cipher */
+              if (!(seek_head = grub_memchr (c, '-', grub_strlen (c))))
+                grub_util_error (_("can't get cipher from dm-crypt parameters `%s'"),
+                                 params);
+              cipher = grub_strndup (c, seek_head - c);
+              c = seek_head + 1;
+
+              /* now, the cipher mode */
+              if (!(seek_head = grub_memchr (c, ' ', grub_strlen (c))))
+                grub_util_error (_("can't get cipher mode from dm-crypt parameters `%s'"),
+                                 params);
+              cipher_mode = grub_strndup (c, seek_head - c);
+
+              grub_cryptodisk_setcipher (cryptodisk, cipher, cipher_mode);
+
+              /* This is the only hash usable by PBKDF2, so set it as such */
+              cryptodisk->hash = grub_crypto_lookup_md_by_name ("sha256");
+
+              /* drop key, iv_offset, device path and offset */
+              for (int dropped_tokens = 0; dropped_tokens < 4; dropped_tokens++)
+                {
+                  seek_head++;
+                  seek_head = grub_memchr (seek_head, ' ', grub_strlen (seek_head));
+                }
+
+              /* if we have optional parameters, skip #opt_params */
+              if (seek_head && (seek_head = grub_memchr (seek_head, ' ', grub_strlen (seek_head))))
+                {
+                  seek_head++;
+                  for (;seek_head;seek_head = grub_memchr (seek_head, ' ', grub_strlen (seek_head)))
+                    {
+                      seek_head++;
+                      if (strncmp (seek_head, "sector_size:", sizeof ("sector_size:") - 1) == 0)
+                        {
+                          char *sector_size_str;
+                          unsigned long long sector_size;
+                          c = seek_head + sizeof ("sector_size:") - 1;
+                          seek_head = grub_memchr (c, ' ', grub_strlen (c));
+                          if (seek_head)
+                            sector_size_str = grub_strndup (c, seek_head - c);
+                          else
+                            sector_size_str = c;
+                          sector_size = grub_strtoull (sector_size_str, NULL, 10);
+
+                          if (sector_size != 512 && sector_size != 1024 &&
+                              sector_size != 2048 && sector_size != 4096)
+                            grub_util_error (_("dm-crypt parameter sector_size `%s' is not a valid LUKS2 sector size"),
+                                             sector_size_str);
+                          cryptodisk->log_sector_size = grub_log2ull (sector_size);
+                          grub_util_info ("set sector_size for dm `%s' to `%d'",
+                                          name, 1 << cryptodisk->log_sector_size);
+                          break;
+                        }
+                    }
+                }
+
+              dm_task_destroy (dmt);
+            }
 	}
+      dm_tree_free (tree);
       grub_free (grdev);
     }
   else
-- 
2.34.0



  parent reply	other threads:[~2021-12-11 12:30 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 17:38 [PATCH 0/2] Have LUKS2 cryptomounts be useable with grub-probe Josselin Poiret
2021-12-09 17:38 ` [PATCH 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2021-12-09 17:38 ` [PATCH 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Josselin Poiret
2021-12-09 20:15 ` [PATCH 0/2] Have LUKS2 cryptomounts be useable with grub-probe Glenn Washburn
2021-12-11 12:29   ` [PATCH v2 " Josselin Poiret
2021-12-11 12:29     ` [PATCH v2 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2022-05-12 22:25       ` Glenn Washburn
2021-12-11 12:29     ` Josselin Poiret [this message]
2022-05-12 22:38       ` [PATCH v2 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Glenn Washburn
2022-05-20 18:20         ` [PATCH v3 0/2] Have LUKS2 cryptomounts be useable with grub-probe Josselin Poiret
2022-05-20 18:20           ` [PATCH v3 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2022-05-20 18:20           ` [PATCH v3 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Josselin Poiret
2022-05-21  0:20             ` Glenn Washburn
2022-05-21  9:27               ` Josselin Poiret
2022-06-14 13:47               ` [PATCH v4 0/2] Have LUKS2 cryptomounts be useable with grub-probe Josselin Poiret
2022-06-14 13:47                 ` [PATCH v4 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2022-06-14 13:47                 ` [PATCH v4 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Josselin Poiret
2022-06-14 18:28                   ` Glenn Washburn
2022-06-15  3:52                   ` Michael Chang
2022-06-14 18:31                 ` [PATCH v4 0/2] Have LUKS2 cryptomounts be useable with grub-probe Glenn Washburn
2022-06-15 10:01                   ` [PATCH v5 " Josselin Poiret
2022-06-15 10:02                     ` [PATCH v5 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2022-06-15 10:02                     ` [PATCH v5 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Josselin Poiret
2022-07-05 11:09                       ` Daniel Kiper
2022-07-08 10:06                         ` [PATCH v6 0/2] Have LUKS2 cryptomounts be useable with grub-probe Josselin Poiret
2022-07-08 10:06                           ` [PATCH v6 1/2] devmapper/getroot: Have devmapper recognize LUKS2 Josselin Poiret
2022-07-08 10:06                           ` [PATCH v6 2/2] devmapper/getroot: Set up cheated LUKS2 cryptodisk mount from DM parameters Josselin Poiret
2022-11-08 15:07                             ` Fabian Vogt
2022-08-11 18:18                           ` [PATCH v6 0/2] Have LUKS2 cryptomounts be useable with grub-probe Glenn Washburn
2022-05-12 22:20     ` [PATCH v2 " Glenn Washburn
2022-05-13 11:58       ` Fabian Vogt

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=20211211122945.6326-3-dev@jpoiret.xyz \
    --to=dev@jpoiret.xyz \
    --cc=development@efficientek.com \
    --cc=grub-devel@gnu.org \
    /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.