0001-Cryptomount-support-LUKS-detached-header.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. From 04e079fed3b275b8ba2081c7fbf9acd853ce055b Mon Sep 17 00:00:00 2001
  2. From: John Lane <john@lane.uk.net>
  3. Date: Tue, 23 Jun 2015 11:16:30 +0100
  4. Subject: [PATCH 02/10] Cryptomount support LUKS detached header
  5. ---
  6. grub-core/disk/cryptodisk.c | 22 ++++++++++++++++++----
  7. grub-core/disk/geli.c | 7 +++++--
  8. grub-core/disk/luks.c | 45 +++++++++++++++++++++++++++++++++++++--------
  9. include/grub/cryptodisk.h | 5 +++--
  10. 4 files changed, 63 insertions(+), 16 deletions(-)
  11. diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
  12. index 1e03a09..dd8870d 100644
  13. --- a/grub-core/disk/cryptodisk.c
  14. +++ b/grub-core/disk/cryptodisk.c
  15. @@ -41,6 +41,7 @@ static const struct grub_arg_option options[] =
  16. /* TRANSLATORS: It's still restricted to cryptodisks only. */
  17. {"all", 'a', 0, N_("Mount all."), 0, 0},
  18. {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0},
  19. + {"header", 'H', 0, N_("Read LUKS header from file"), 0, ARG_TYPE_STRING},
  20. {0, 0, 0, 0, 0, 0}
  21. };
  22. @@ -808,6 +809,7 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
  23. static int check_boot, have_it;
  24. static char *search_uuid;
  25. +static grub_file_t hdr;
  26. static void
  27. cryptodisk_close (grub_cryptodisk_t dev)
  28. @@ -832,13 +834,13 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source)
  29. FOR_CRYPTODISK_DEVS (cr)
  30. {
  31. - dev = cr->scan (source, search_uuid, check_boot);
  32. + dev = cr->scan (source, search_uuid, check_boot, hdr);
  33. if (grub_errno)
  34. return grub_errno;
  35. if (!dev)
  36. continue;
  37. - err = cr->recover_key (source, dev);
  38. + err = cr->recover_key (source, dev, hdr);
  39. if (err)
  40. {
  41. cryptodisk_close (dev);
  42. @@ -879,7 +881,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, const char *cheat)
  43. FOR_CRYPTODISK_DEVS (cr)
  44. {
  45. - dev = cr->scan (source, search_uuid, check_boot);
  46. + dev = cr->scan (source, search_uuid, check_boot,0);
  47. if (grub_errno)
  48. return grub_errno;
  49. if (!dev)
  50. @@ -933,6 +935,18 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
  51. if (argc < 1 && !state[1].set && !state[2].set)
  52. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  53. + if (state[3].set) /* LUKS detached header */
  54. + {
  55. + if (state[0].set) /* Cannot use UUID lookup with detached header */
  56. + return GRUB_ERR_BAD_ARGUMENT;
  57. +
  58. + hdr = grub_file_open (state[3].arg);
  59. + if (!hdr)
  60. + return grub_errno;
  61. + }
  62. + else
  63. + hdr = NULL;
  64. +
  65. have_it = 0;
  66. if (state[0].set)
  67. {
  68. @@ -1140,7 +1154,7 @@ GRUB_MOD_INIT (cryptodisk)
  69. {
  70. grub_disk_dev_register (&grub_cryptodisk_dev);
  71. cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
  72. - N_("SOURCE|-u UUID|-a|-b"),
  73. + N_("SOURCE|-u UUID|-a|-b|-H file"),
  74. N_("Mount a crypto device."), options);
  75. grub_procfs_register ("luks_script", &luks_script);
  76. }
  77. diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
  78. index e9d2329..f4394eb 100644
  79. --- a/grub-core/disk/geli.c
  80. +++ b/grub-core/disk/geli.c
  81. @@ -52,6 +52,7 @@
  82. #include <grub/dl.h>
  83. #include <grub/err.h>
  84. #include <grub/disk.h>
  85. +#include <grub/file.h>
  86. #include <grub/crypto.h>
  87. #include <grub/partition.h>
  88. #include <grub/i18n.h>
  89. @@ -243,7 +244,8 @@ grub_util_get_geli_uuid (const char *dev)
  90. static grub_cryptodisk_t
  91. configure_ciphers (grub_disk_t disk, const char *check_uuid,
  92. - int boot_only)
  93. + int boot_only,
  94. + grub_file_t hdr __attribute__ ((unused)) )
  95. {
  96. grub_cryptodisk_t newdev;
  97. struct grub_geli_phdr header;
  98. @@ -398,7 +400,8 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
  99. }
  100. static grub_err_t
  101. -recover_key (grub_disk_t source, grub_cryptodisk_t dev)
  102. +recover_key (grub_disk_t source, grub_cryptodisk_t dev,
  103. + grub_file_t hdr __attribute__ ((unused)) )
  104. {
  105. grub_size_t keysize;
  106. grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN];
  107. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  108. index 86c50c6..66e64c0 100644
  109. --- a/grub-core/disk/luks.c
  110. +++ b/grub-core/disk/luks.c
  111. @@ -23,6 +23,7 @@
  112. #include <grub/dl.h>
  113. #include <grub/err.h>
  114. #include <grub/disk.h>
  115. +#include <grub/file.h>
  116. #include <grub/crypto.h>
  117. #include <grub/partition.h>
  118. #include <grub/i18n.h>
  119. @@ -66,7 +67,7 @@ gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  120. static grub_cryptodisk_t
  121. configure_ciphers (grub_disk_t disk, const char *check_uuid,
  122. - int check_boot)
  123. + int check_boot, grub_file_t hdr)
  124. {
  125. grub_cryptodisk_t newdev;
  126. const char *iptr;
  127. @@ -86,11 +87,21 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
  128. int benbi_log = 0;
  129. grub_err_t err;
  130. + err = GRUB_ERR_NONE;
  131. +
  132. if (check_boot)
  133. return NULL;
  134. /* Read the LUKS header. */
  135. - err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  136. + if (hdr)
  137. + {
  138. + grub_file_seek (hdr, 0);
  139. + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header))
  140. + err = GRUB_ERR_READ_ERROR;
  141. + }
  142. + else
  143. + err = grub_disk_read (disk, 0, 0, sizeof (header), &header);
  144. +
  145. if (err)
  146. {
  147. if (err == GRUB_ERR_OUT_OF_RANGE)
  148. @@ -304,12 +315,14 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
  149. grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
  150. newdev->modname = "luks";
  151. COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (uuid));
  152. +
  153. return newdev;
  154. }
  155. static grub_err_t
  156. luks_recover_key (grub_disk_t source,
  157. - grub_cryptodisk_t dev)
  158. + grub_cryptodisk_t dev,
  159. + grub_file_t hdr)
  160. {
  161. struct grub_luks_phdr header;
  162. grub_size_t keysize;
  163. @@ -321,8 +334,19 @@ luks_recover_key (grub_disk_t source,
  164. grub_err_t err;
  165. grub_size_t max_stripes = 1;
  166. char *tmp;
  167. + grub_uint32_t sector;
  168. +
  169. + err = GRUB_ERR_NONE;
  170. +
  171. + if (hdr)
  172. + {
  173. + grub_file_seek (hdr, 0);
  174. + if (grub_file_read (hdr, &header, sizeof (header)) != sizeof (header))
  175. + err = GRUB_ERR_READ_ERROR;
  176. + }
  177. + else
  178. + err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  179. - err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  180. if (err)
  181. return err;
  182. @@ -391,13 +415,18 @@ luks_recover_key (grub_disk_t source,
  183. return grub_crypto_gcry_error (gcry_err);
  184. }
  185. + sector = grub_be_to_cpu32 (header.keyblock[i].keyMaterialOffset);
  186. length = (keysize * grub_be_to_cpu32 (header.keyblock[i].stripes));
  187. /* Read and decrypt the key material from the disk. */
  188. - err = grub_disk_read (source,
  189. - grub_be_to_cpu32 (header.keyblock
  190. - [i].keyMaterialOffset), 0,
  191. - length, split_key);
  192. + if (hdr)
  193. + {
  194. + grub_file_seek (hdr, sector * 512);
  195. + if (grub_file_read (hdr, split_key, length) != (grub_ssize_t)length)
  196. + err = GRUB_ERR_READ_ERROR;
  197. + }
  198. + else
  199. + err = grub_disk_read (source, sector, 0, length, split_key);
  200. if (err)
  201. {
  202. grub_free (split_key);
  203. diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h
  204. index 32f564a..4e6e89a 100644
  205. --- a/include/grub/cryptodisk.h
  206. +++ b/include/grub/cryptodisk.h
  207. @@ -20,6 +20,7 @@
  208. #define GRUB_CRYPTODISK_HEADER 1
  209. #include <grub/disk.h>
  210. +#include <grub/file.h>
  211. #include <grub/crypto.h>
  212. #include <grub/list.h>
  213. #ifdef GRUB_UTIL
  214. @@ -107,8 +108,8 @@ struct grub_cryptodisk_dev
  215. struct grub_cryptodisk_dev **prev;
  216. grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid,
  217. - int boot_only);
  218. - grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev);
  219. + int boot_only, grub_file_t hdr);
  220. + grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev, grub_file_t hdr);
  221. };
  222. typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t;
  223. --
  224. 1.9.1