getroot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <config.h>
  20. #include <grub/emu/getroot.h>
  21. #include <grub/mm.h>
  22. #ifdef HAVE_DEVICE_MAPPER
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <assert.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <dirent.h>
  30. #include <errno.h>
  31. #include <error.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdint.h>
  35. #ifdef HAVE_LIMITS_H
  36. #include <limits.h>
  37. #endif
  38. #include <grub/osdep/major.h>
  39. #include <libdevmapper.h>
  40. #include <grub/types.h>
  41. #include <grub/util/misc.h>
  42. #include <grub/mm.h>
  43. #include <grub/misc.h>
  44. #include <grub/emu/misc.h>
  45. #include <grub/emu/hostdisk.h>
  46. #include <grub/cryptodisk.h>
  47. static int
  48. grub_util_open_dm (const char *os_dev, struct dm_tree **tree,
  49. struct dm_tree_node **node)
  50. {
  51. uint32_t maj, min;
  52. struct stat st;
  53. *node = NULL;
  54. *tree = NULL;
  55. if (stat (os_dev, &st) < 0)
  56. return 0;
  57. maj = major (st.st_rdev);
  58. min = minor (st.st_rdev);
  59. if (!dm_is_dm_major (maj))
  60. return 0;
  61. *tree = dm_tree_create ();
  62. if (! *tree)
  63. {
  64. grub_puts_ (N_("Failed to create `device-mapper' tree"));
  65. grub_dprintf ("hostdisk", "dm_tree_create failed\n");
  66. return 0;
  67. }
  68. if (! dm_tree_add_dev (*tree, maj, min))
  69. {
  70. grub_dprintf ("hostdisk", "dm_tree_add_dev failed\n");
  71. dm_tree_free (*tree);
  72. *tree = NULL;
  73. return 0;
  74. }
  75. *node = dm_tree_find_node (*tree, maj, min);
  76. if (! *node)
  77. {
  78. grub_dprintf ("hostdisk", "dm_tree_find_node failed\n");
  79. dm_tree_free (*tree);
  80. *tree = NULL;
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. static char *
  86. get_dm_uuid (const char *os_dev)
  87. {
  88. struct dm_tree *tree;
  89. struct dm_tree_node *node;
  90. const char *node_uuid;
  91. char *ret;
  92. if (!grub_util_open_dm (os_dev, &tree, &node))
  93. return NULL;
  94. node_uuid = dm_tree_node_get_uuid (node);
  95. if (! node_uuid)
  96. {
  97. grub_dprintf ("hostdisk", "%s has no DM uuid\n", os_dev);
  98. dm_tree_free (tree);
  99. return NULL;
  100. }
  101. ret = grub_strdup (node_uuid);
  102. dm_tree_free (tree);
  103. return ret;
  104. }
  105. enum grub_dev_abstraction_types
  106. grub_util_get_dm_abstraction (const char *os_dev)
  107. {
  108. char *uuid;
  109. uuid = get_dm_uuid (os_dev);
  110. if (uuid == NULL)
  111. return GRUB_DEV_ABSTRACTION_NONE;
  112. if (strncmp (uuid, "LVM-", 4) == 0)
  113. {
  114. grub_free (uuid);
  115. return GRUB_DEV_ABSTRACTION_LVM;
  116. }
  117. if (strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0
  118. || strncmp (uuid, "CRYPT-LUKS2-", sizeof ("CRYPT-LUKS2-") - 1) == 0)
  119. {
  120. grub_free (uuid);
  121. return GRUB_DEV_ABSTRACTION_LUKS;
  122. }
  123. grub_free (uuid);
  124. return GRUB_DEV_ABSTRACTION_NONE;
  125. }
  126. void
  127. grub_util_pull_devmapper (const char *os_dev)
  128. {
  129. struct dm_tree *tree;
  130. struct dm_tree_node *node;
  131. struct dm_tree_node *child;
  132. void *handle = NULL;
  133. char *lastsubdev = NULL;
  134. char *uuid;
  135. uuid = get_dm_uuid (os_dev);
  136. if (!grub_util_open_dm (os_dev, &tree, &node))
  137. {
  138. grub_free (uuid);
  139. return;
  140. }
  141. while ((child = dm_tree_next_child (&handle, node, 0)))
  142. {
  143. const struct dm_info *dm = dm_tree_node_get_info (child);
  144. char *subdev;
  145. if (!dm)
  146. continue;
  147. subdev = grub_find_device ("/dev", makedev (dm->major, dm->minor));
  148. if (subdev)
  149. {
  150. lastsubdev = subdev;
  151. grub_util_pull_device (subdev);
  152. }
  153. }
  154. if (uuid
  155. && (strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0
  156. || strncmp (uuid, "CRYPT-LUKS2-", sizeof ("CRYPT-LUKS2-") - 1) == 0)
  157. && lastsubdev)
  158. {
  159. char *grdev = grub_util_get_grub_dev (lastsubdev);
  160. if (grdev)
  161. {
  162. grub_err_t err;
  163. err = grub_cryptodisk_cheat_mount (grdev, os_dev);
  164. if (err)
  165. grub_util_error (_("can't mount encrypted volume `%s': %s"),
  166. lastsubdev, grub_errmsg);
  167. if (strncmp (uuid, "CRYPT-LUKS2-", sizeof ("CRYPT-LUKS2-") - 1) == 0)
  168. {
  169. /*
  170. * Set LUKS2 cipher from dm parameters, since it is not
  171. * possible to determine the correct one without
  172. * unlocking, as there might be multiple segments.
  173. */
  174. grub_disk_t source;
  175. grub_cryptodisk_t cryptodisk;
  176. grub_uint64_t start, length;
  177. char *target_type;
  178. char *params;
  179. const char *name;
  180. char *cipher, *cipher_mode;
  181. struct dm_task *dmt;
  182. char *seek_head, *c;
  183. unsigned int remaining;
  184. source = grub_disk_open (grdev);
  185. if (! source)
  186. grub_util_error (_("cannot open grub disk `%s'"), grdev);
  187. cryptodisk = grub_cryptodisk_get_by_source_disk (source);
  188. if (! cryptodisk)
  189. grub_util_error (_("cannot get cryptodisk from source disk `%s'"), grdev);
  190. grub_disk_close (source);
  191. /*
  192. * The following function always returns a non-NULL pointer,
  193. * but the string may be empty if the relevant info is not present.
  194. */
  195. name = dm_tree_node_get_name (node);
  196. if (*name == '\0')
  197. grub_util_error (_("cannot get dm node name for grub dev `%s'"), grdev);
  198. grub_util_info ("populating parameters of cryptomount `%s' from DM device `%s'",
  199. uuid, name);
  200. dmt = dm_task_create (DM_DEVICE_TABLE);
  201. if (dmt == NULL)
  202. grub_util_error (_("can't create dm task DM_DEVICE_TABLE"));
  203. if (dm_task_set_name (dmt, name) == 0)
  204. grub_util_error (_("can't set dm task name to `%s'"), name);
  205. if (dm_task_run (dmt) == 0)
  206. grub_util_error (_("can't run dm task for `%s'"), name);
  207. /*
  208. * dm_get_next_target() doesn't have any error modes, everything has
  209. * been handled by dm_task_run().
  210. */
  211. dm_get_next_target (dmt, NULL, &start, &length,
  212. &target_type, &params);
  213. if (strncmp (target_type, "crypt", sizeof ("crypt")) != 0)
  214. grub_util_error (_("dm target of type `%s' is not `crypt'"), target_type);
  215. /*
  216. * The dm target parameters for dm-crypt are
  217. * <cipher> <key> <iv_offset> <device path> <offset> [<#opt_params> <opt_param1> ...]
  218. */
  219. c = params;
  220. remaining = grub_strlen (c);
  221. /* First, get the cipher name from the cipher. */
  222. seek_head = grub_memchr (c, '-', remaining);
  223. if (seek_head == NULL)
  224. grub_util_error (_("can't get cipher from dm-crypt parameters `%s'"),
  225. params);
  226. cipher = grub_strndup (c, seek_head - c);
  227. if (cipher == NULL)
  228. grub_util_error ("could not strndup cipher of length `%" PRIuGRUB_SIZE "'", (grub_size_t) (seek_head - c));
  229. remaining -= seek_head - c + 1;
  230. c = seek_head + 1;
  231. /* Now, the cipher mode. */
  232. seek_head = grub_memchr (c, ' ', remaining);
  233. if (seek_head == NULL)
  234. grub_util_error (_("can't get cipher mode from dm-crypt parameters `%s'"),
  235. params);
  236. cipher_mode = grub_strndup (c, seek_head - c);
  237. if (cipher_mode == NULL)
  238. grub_util_error ("could not strndup cipher_mode of length `%" PRIuGRUB_SIZE "'", (grub_size_t) (seek_head - c));
  239. remaining -= seek_head - c + 1;
  240. c = seek_head + 1;
  241. err = grub_cryptodisk_setcipher (cryptodisk, cipher, cipher_mode);
  242. if (err)
  243. grub_util_error (_("can't set cipher of cryptodisk `%s' to `%s' with mode `%s'"),
  244. uuid, cipher, cipher_mode);
  245. grub_free (cipher);
  246. grub_free (cipher_mode);
  247. /*
  248. * This is the only hash usable by PBKDF2, and we don't
  249. * have Argon2 support yet, so set it by default,
  250. * otherwise grub-probe would miss the required
  251. * abstraction.
  252. */
  253. cryptodisk->hash = grub_crypto_lookup_md_by_name ("sha256");
  254. if (cryptodisk->hash == NULL)
  255. grub_util_error (_("can't lookup hash sha256 by name"));
  256. dm_task_destroy (dmt);
  257. }
  258. }
  259. dm_tree_free (tree);
  260. grub_free (grdev);
  261. }
  262. else
  263. dm_tree_free (tree);
  264. grub_free (uuid);
  265. }
  266. char *
  267. grub_util_devmapper_part_to_disk (struct stat *st,
  268. int *is_part, const char *path)
  269. {
  270. int major, minor;
  271. if (grub_util_get_dm_node_linear_info (st->st_rdev,
  272. &major, &minor, 0))
  273. {
  274. *is_part = 1;
  275. return grub_find_device ("/dev", makedev (major, minor));
  276. }
  277. *is_part = 0;
  278. return xstrdup (path);
  279. }
  280. char *
  281. grub_util_get_devmapper_grub_dev (const char *os_dev)
  282. {
  283. char *uuid, *optr;
  284. char *grub_dev;
  285. uuid = get_dm_uuid (os_dev);
  286. if (!uuid)
  287. return NULL;
  288. switch (grub_util_get_dev_abstraction (os_dev))
  289. {
  290. case GRUB_DEV_ABSTRACTION_LVM:
  291. {
  292. unsigned i;
  293. int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32, 38, 42, 46, 50, 54, 58};
  294. grub_dev = xmalloc (grub_strlen (uuid) + 40);
  295. optr = grub_stpcpy (grub_dev, "lvmid/");
  296. for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
  297. {
  298. memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
  299. dashes[i+1] - dashes[i]);
  300. optr += dashes[i+1] - dashes[i];
  301. *optr++ = '-';
  302. }
  303. optr = stpcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i]);
  304. *optr = '\0';
  305. grub_dev[sizeof("lvmid/xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx") - 1]
  306. = '/';
  307. free (uuid);
  308. return grub_dev;
  309. }
  310. case GRUB_DEV_ABSTRACTION_LUKS:
  311. {
  312. char *dash;
  313. dash = grub_strchr (uuid + sizeof ("CRYPT-LUKS*-") - 1, '-');
  314. if (dash)
  315. *dash = 0;
  316. grub_dev = grub_xasprintf ("cryptouuid/%s",
  317. uuid + sizeof ("CRYPT-LUKS*-") - 1);
  318. grub_free (uuid);
  319. return grub_dev;
  320. }
  321. default:
  322. grub_free (uuid);
  323. return NULL;
  324. }
  325. }
  326. char *
  327. grub_util_get_vg_uuid (const char *os_dev)
  328. {
  329. char *uuid, *vgid;
  330. int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32};
  331. unsigned i;
  332. char *optr;
  333. uuid = get_dm_uuid (os_dev);
  334. if (!uuid)
  335. return NULL;
  336. vgid = xmalloc (grub_strlen (uuid));
  337. optr = vgid;
  338. for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
  339. {
  340. memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
  341. dashes[i+1] - dashes[i]);
  342. optr += dashes[i+1] - dashes[i];
  343. *optr++ = '-';
  344. }
  345. optr--;
  346. *optr = '\0';
  347. grub_free (uuid);
  348. return vgid;
  349. }
  350. void
  351. grub_util_devmapper_cleanup (void)
  352. {
  353. dm_lib_release ();
  354. }
  355. #else
  356. void
  357. grub_util_pull_devmapper (const char *os_dev __attribute__ ((unused)))
  358. {
  359. return;
  360. }
  361. void
  362. grub_util_devmapper_cleanup (void)
  363. {
  364. }
  365. enum grub_dev_abstraction_types
  366. grub_util_get_dm_abstraction (const char *os_dev __attribute__ ((unused)))
  367. {
  368. return GRUB_DEV_ABSTRACTION_NONE;
  369. }
  370. char *
  371. grub_util_get_vg_uuid (const char *os_dev __attribute__ ((unused)))
  372. {
  373. return NULL;
  374. }
  375. char *
  376. grub_util_devmapper_part_to_disk (struct stat *st __attribute__ ((unused)),
  377. int *is_part __attribute__ ((unused)),
  378. const char *os_dev __attribute__ ((unused)))
  379. {
  380. return NULL;
  381. }
  382. char *
  383. grub_util_get_devmapper_grub_dev (const char *os_dev __attribute__ ((unused)))
  384. {
  385. return NULL;
  386. }
  387. #endif