]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.c
Merge pull request #7218 from matijaskala/patch-4
[thirdparty/systemd.git] / src / shared / dissect-image.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #if HAVE_LIBCRYPTSETUP
21 #include <libcryptsetup.h>
22 #endif
23 #include <sys/mount.h>
24
25 #include "architecture.h"
26 #include "ask-password-api.h"
27 #include "blkid-util.h"
28 #include "dissect-image.h"
29 #include "fd-util.h"
30 #include "fileio.h"
31 #include "fs-util.h"
32 #include "gpt.h"
33 #include "hexdecoct.h"
34 #include "linux-3.13/dm-ioctl.h"
35 #include "mount-util.h"
36 #include "path-util.h"
37 #include "stat-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "udev-util.h"
43 #include "xattr-util.h"
44
45 _unused_ static int probe_filesystem(const char *node, char **ret_fstype) {
46 #if HAVE_BLKID
47 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
48 const char *fstype;
49 int r;
50
51 b = blkid_new_probe_from_filename(node);
52 if (!b)
53 return -ENOMEM;
54
55 blkid_probe_enable_superblocks(b, 1);
56 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
57
58 errno = 0;
59 r = blkid_do_safeprobe(b);
60 if (IN_SET(r, -2, 1)) {
61 log_debug("Failed to identify any partition type on partition %s", node);
62 goto not_found;
63 }
64 if (r != 0)
65 return -errno ?: -EIO;
66
67 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
68
69 if (fstype) {
70 char *t;
71
72 t = strdup(fstype);
73 if (!t)
74 return -ENOMEM;
75
76 *ret_fstype = t;
77 return 1;
78 }
79
80 not_found:
81 *ret_fstype = NULL;
82 return 0;
83 #else
84 return -EOPNOTSUPP;
85 #endif
86 }
87
88 int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DissectedImage **ret) {
89
90 #if HAVE_BLKID
91 sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
92 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
93 bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
94 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
95 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
96 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
97 _cleanup_udev_unref_ struct udev *udev = NULL;
98 _cleanup_free_ char *generic_node = NULL;
99 sd_id128_t generic_uuid = SD_ID128_NULL;
100 const char *pttype = NULL;
101 struct udev_list_entry *first, *item;
102 blkid_partlist pl;
103 int r, generic_nr;
104 struct stat st;
105 unsigned i;
106
107 assert(fd >= 0);
108 assert(ret);
109 assert(root_hash || root_hash_size == 0);
110
111 /* Probes a disk image, and returns information about what it found in *ret.
112 *
113 * Returns -ENOPKG if no suitable partition table or file system could be found.
114 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
115
116 if (root_hash) {
117 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
118 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
119 * 128bit. */
120
121 if (root_hash_size < sizeof(sd_id128_t))
122 return -EINVAL;
123
124 memcpy(&root_uuid, root_hash, sizeof(sd_id128_t));
125 memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t));
126
127 if (sd_id128_is_null(root_uuid))
128 return -EINVAL;
129 if (sd_id128_is_null(verity_uuid))
130 return -EINVAL;
131 }
132
133 if (fstat(fd, &st) < 0)
134 return -errno;
135
136 if (!S_ISBLK(st.st_mode))
137 return -ENOTBLK;
138
139 b = blkid_new_probe();
140 if (!b)
141 return -ENOMEM;
142
143 errno = 0;
144 r = blkid_probe_set_device(b, fd, 0, 0);
145 if (r != 0)
146 return -errno ?: -ENOMEM;
147
148 if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
149 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
150 blkid_probe_enable_superblocks(b, 1);
151 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE);
152 }
153
154 blkid_probe_enable_partitions(b, 1);
155 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
156
157 errno = 0;
158 r = blkid_do_safeprobe(b);
159 if (IN_SET(r, -2, 1)) {
160 log_debug("Failed to identify any partition table.");
161 return -ENOPKG;
162 }
163 if (r != 0)
164 return -errno ?: -EIO;
165
166 m = new0(DissectedImage, 1);
167 if (!m)
168 return -ENOMEM;
169
170 if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
171 (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
172 const char *usage = NULL;
173
174 (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
175 if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
176 _cleanup_free_ char *t = NULL, *n = NULL;
177 const char *fstype = NULL;
178
179 /* OK, we have found a file system, that's our root partition then. */
180 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
181
182 if (fstype) {
183 t = strdup(fstype);
184 if (!t)
185 return -ENOMEM;
186 }
187
188 if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
189 return -ENOMEM;
190
191 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
192 .found = true,
193 .rw = true,
194 .partno = -1,
195 .architecture = _ARCHITECTURE_INVALID,
196 .fstype = t,
197 .node = n,
198 };
199
200 t = n = NULL;
201
202 m->encrypted = streq(fstype, "crypto_LUKS");
203
204 *ret = m;
205 m = NULL;
206
207 return 0;
208 }
209 }
210
211 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
212 if (!pttype)
213 return -ENOPKG;
214
215 is_gpt = streq_ptr(pttype, "gpt");
216 is_mbr = streq_ptr(pttype, "dos");
217
218 if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr))
219 return -ENOPKG;
220
221 errno = 0;
222 pl = blkid_probe_get_partitions(b);
223 if (!pl)
224 return -errno ?: -ENOMEM;
225
226 udev = udev_new();
227 if (!udev)
228 return -errno;
229
230 d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
231 if (!d)
232 return -ENOMEM;
233
234 for (i = 0;; i++) {
235 int n, z;
236
237 if (i >= 10) {
238 log_debug("Kernel partitions never appeared.");
239 return -ENXIO;
240 }
241
242 e = udev_enumerate_new(udev);
243 if (!e)
244 return -errno;
245
246 r = udev_enumerate_add_match_parent(e, d);
247 if (r < 0)
248 return r;
249
250 r = udev_enumerate_scan_devices(e);
251 if (r < 0)
252 return r;
253
254 /* Count the partitions enumerated by the kernel */
255 n = 0;
256 first = udev_enumerate_get_list_entry(e);
257 udev_list_entry_foreach(item, first)
258 n++;
259
260 /* Count the partitions enumerated by blkid */
261 z = blkid_partlist_numof_partitions(pl);
262 if (n == z + 1)
263 break;
264 if (n > z + 1) {
265 log_debug("blkid and kernel partition list do not match.");
266 return -EIO;
267 }
268 if (n < z + 1) {
269 unsigned j = 0;
270
271 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
272 * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
273 * synchronous call that waits until probing is complete. */
274
275 for (;;) {
276 if (j++ > 20)
277 return -EBUSY;
278
279 if (ioctl(fd, BLKRRPART, 0) < 0) {
280 r = -errno;
281
282 if (r == -EINVAL) {
283 struct loop_info64 info;
284
285 /* If we are running on a loop device that has partition scanning off,
286 * return an explicit recognizable error about this, so that callers
287 * can generate a proper message explaining the situation. */
288
289 if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0 && (info.lo_flags & LO_FLAGS_PARTSCAN) == 0) {
290 log_debug("Device is loop device and partition scanning is off!");
291 return -EPROTONOSUPPORT;
292 }
293 }
294 if (r != -EBUSY)
295 return r;
296 } else
297 break;
298
299 /* If something else has the device open, such as an udev rule, the ioctl will return
300 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
301 * bit, and try again.
302 *
303 * This is really something they should fix in the kernel! */
304
305 (void) usleep(50 * USEC_PER_MSEC);
306 }
307 }
308
309 e = udev_enumerate_unref(e);
310 }
311
312 first = udev_enumerate_get_list_entry(e);
313 udev_list_entry_foreach(item, first) {
314 _cleanup_udev_device_unref_ struct udev_device *q;
315 unsigned long long pflags;
316 blkid_partition pp;
317 const char *node, *sysname;
318 dev_t qn;
319 int nr;
320
321 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
322 if (!q)
323 return -errno;
324
325 qn = udev_device_get_devnum(q);
326 if (major(qn) == 0)
327 continue;
328
329 if (st.st_rdev == qn)
330 continue;
331
332 /* Filter out weird MMC RPMB partitions, which cannot reasonably be read, see
333 * https://github.com/systemd/systemd/issues/5806 */
334 sysname = udev_device_get_sysname(q);
335 if (sysname && startswith(sysname, "mmcblk") && endswith(sysname, "rpmb"))
336 continue;
337
338 node = udev_device_get_devnode(q);
339 if (!node)
340 continue;
341
342 pp = blkid_partlist_devno_to_partition(pl, qn);
343 if (!pp)
344 continue;
345
346 pflags = blkid_partition_get_flags(pp);
347
348 nr = blkid_partition_get_partno(pp);
349 if (nr < 0)
350 continue;
351
352 if (is_gpt) {
353 int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID;
354 const char *stype, *sid, *fstype = NULL;
355 sd_id128_t type_id, id;
356 bool rw = true;
357
358 sid = blkid_partition_get_uuid(pp);
359 if (!sid)
360 continue;
361 if (sd_id128_from_string(sid, &id) < 0)
362 continue;
363
364 stype = blkid_partition_get_type_string(pp);
365 if (!stype)
366 continue;
367 if (sd_id128_from_string(stype, &type_id) < 0)
368 continue;
369
370 if (sd_id128_equal(type_id, GPT_HOME)) {
371
372 if (pflags & GPT_FLAG_NO_AUTO)
373 continue;
374
375 designator = PARTITION_HOME;
376 rw = !(pflags & GPT_FLAG_READ_ONLY);
377 } else if (sd_id128_equal(type_id, GPT_SRV)) {
378
379 if (pflags & GPT_FLAG_NO_AUTO)
380 continue;
381
382 designator = PARTITION_SRV;
383 rw = !(pflags & GPT_FLAG_READ_ONLY);
384 } else if (sd_id128_equal(type_id, GPT_ESP)) {
385
386 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
387 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
388 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
389
390 if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL)
391 continue;
392
393 designator = PARTITION_ESP;
394 fstype = "vfat";
395 }
396 #ifdef GPT_ROOT_NATIVE
397 else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) {
398
399 if (pflags & GPT_FLAG_NO_AUTO)
400 continue;
401
402 /* If a root ID is specified, ignore everything but the root id */
403 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
404 continue;
405
406 designator = PARTITION_ROOT;
407 architecture = native_architecture();
408 rw = !(pflags & GPT_FLAG_READ_ONLY);
409 } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) {
410
411 if (pflags & GPT_FLAG_NO_AUTO)
412 continue;
413
414 m->can_verity = true;
415
416 /* Ignore verity unless a root hash is specified */
417 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
418 continue;
419
420 designator = PARTITION_ROOT_VERITY;
421 fstype = "DM_verity_hash";
422 architecture = native_architecture();
423 rw = false;
424 }
425 #endif
426 #ifdef GPT_ROOT_SECONDARY
427 else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) {
428
429 if (pflags & GPT_FLAG_NO_AUTO)
430 continue;
431
432 /* If a root ID is specified, ignore everything but the root id */
433 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
434 continue;
435
436 designator = PARTITION_ROOT_SECONDARY;
437 architecture = SECONDARY_ARCHITECTURE;
438 rw = !(pflags & GPT_FLAG_READ_ONLY);
439 } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) {
440
441 if (pflags & GPT_FLAG_NO_AUTO)
442 continue;
443
444 m->can_verity = true;
445
446 /* Ignore verity unless root has is specified */
447 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
448 continue;
449
450 designator = PARTITION_ROOT_SECONDARY_VERITY;
451 fstype = "DM_verity_hash";
452 architecture = SECONDARY_ARCHITECTURE;
453 rw = false;
454 }
455 #endif
456 else if (sd_id128_equal(type_id, GPT_SWAP)) {
457
458 if (pflags & GPT_FLAG_NO_AUTO)
459 continue;
460
461 designator = PARTITION_SWAP;
462 fstype = "swap";
463 } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) {
464
465 if (pflags & GPT_FLAG_NO_AUTO)
466 continue;
467
468 if (generic_node)
469 multiple_generic = true;
470 else {
471 generic_nr = nr;
472 generic_rw = !(pflags & GPT_FLAG_READ_ONLY);
473 generic_uuid = id;
474 generic_node = strdup(node);
475 if (!generic_node)
476 return -ENOMEM;
477 }
478 }
479
480 if (designator != _PARTITION_DESIGNATOR_INVALID) {
481 _cleanup_free_ char *t = NULL, *n = NULL;
482
483 /* First one wins */
484 if (m->partitions[designator].found)
485 continue;
486
487 if (fstype) {
488 t = strdup(fstype);
489 if (!t)
490 return -ENOMEM;
491 }
492
493 n = strdup(node);
494 if (!n)
495 return -ENOMEM;
496
497 m->partitions[designator] = (DissectedPartition) {
498 .found = true,
499 .partno = nr,
500 .rw = rw,
501 .architecture = architecture,
502 .node = n,
503 .fstype = t,
504 .uuid = id,
505 };
506
507 n = t = NULL;
508 }
509
510 } else if (is_mbr) {
511
512 if (pflags != 0x80) /* Bootable flag */
513 continue;
514
515 if (blkid_partition_get_type(pp) != 0x83) /* Linux partition */
516 continue;
517
518 if (generic_node)
519 multiple_generic = true;
520 else {
521 generic_nr = nr;
522 generic_rw = true;
523 generic_node = strdup(node);
524 if (!generic_node)
525 return -ENOMEM;
526 }
527 }
528 }
529
530 if (!m->partitions[PARTITION_ROOT].found) {
531 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
532 * either, then check if there's a single generic one, and use that. */
533
534 if (m->partitions[PARTITION_ROOT_VERITY].found)
535 return -EADDRNOTAVAIL;
536
537 if (m->partitions[PARTITION_ROOT_SECONDARY].found) {
538 m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
539 zero(m->partitions[PARTITION_ROOT_SECONDARY]);
540
541 m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
542 zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
543
544 } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) {
545
546 /* If the root has was set, then we won't fallback to a generic node, because the root hash
547 * decides */
548 if (root_hash)
549 return -EADDRNOTAVAIL;
550
551 /* If we didn't find a generic node, then we can't fix this up either */
552 if (!generic_node)
553 return -ENXIO;
554
555 /* If we didn't find a properly marked root partition, but we did find a single suitable
556 * generic Linux partition, then use this as root partition, if the caller asked for it. */
557 if (multiple_generic)
558 return -ENOTUNIQ;
559
560 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
561 .found = true,
562 .rw = generic_rw,
563 .partno = generic_nr,
564 .architecture = _ARCHITECTURE_INVALID,
565 .node = generic_node,
566 .uuid = generic_uuid,
567 };
568
569 generic_node = NULL;
570 }
571 }
572
573 if (root_hash) {
574 if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found)
575 return -EADDRNOTAVAIL;
576
577 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
578 * (which would be weird, after all the root hash should only be assigned to one pair of
579 * partitions... */
580 m->partitions[PARTITION_ROOT_SECONDARY].found = false;
581 m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false;
582
583 /* If we found a verity setup, then the root partition is necessarily read-only. */
584 m->partitions[PARTITION_ROOT].rw = false;
585
586 m->verity = true;
587 }
588
589 blkid_free_probe(b);
590 b = NULL;
591
592 /* Fill in file system types if we don't know them yet. */
593 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
594 DissectedPartition *p = m->partitions + i;
595
596 if (!p->found)
597 continue;
598
599 if (!p->fstype && p->node) {
600 r = probe_filesystem(p->node, &p->fstype);
601 if (r < 0)
602 return r;
603 }
604
605 if (streq_ptr(p->fstype, "crypto_LUKS"))
606 m->encrypted = true;
607
608 if (p->fstype && fstype_is_ro(p->fstype))
609 p->rw = false;
610 }
611
612 *ret = m;
613 m = NULL;
614
615 return 0;
616 #else
617 return -EOPNOTSUPP;
618 #endif
619 }
620
621 DissectedImage* dissected_image_unref(DissectedImage *m) {
622 unsigned i;
623
624 if (!m)
625 return NULL;
626
627 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
628 free(m->partitions[i].fstype);
629 free(m->partitions[i].node);
630 free(m->partitions[i].decrypted_fstype);
631 free(m->partitions[i].decrypted_node);
632 }
633
634 free(m);
635 return NULL;
636 }
637
638 static int is_loop_device(const char *path) {
639 char s[strlen("/sys/dev/block/") + DECIMAL_STR_MAX(dev_t) + 1 + DECIMAL_STR_MAX(dev_t) + strlen("/../loop/")];
640 struct stat st;
641
642 assert(path);
643
644 if (stat(path, &st) < 0)
645 return -errno;
646
647 if (!S_ISBLK(st.st_mode))
648 return -ENOTBLK;
649
650 xsprintf(s, "/sys/dev/block/%u:%u/loop/", major(st.st_rdev), minor(st.st_rdev));
651 if (access(s, F_OK) < 0) {
652 if (errno != ENOENT)
653 return -errno;
654
655 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
656 xsprintf(s, "/sys/dev/block/%u:%u/../loop/", major(st.st_rdev), minor(st.st_rdev));
657 if (access(s, F_OK) < 0)
658 return errno == ENOENT ? false : -errno;
659 }
660
661 return true;
662 }
663
664 static int mount_partition(
665 DissectedPartition *m,
666 const char *where,
667 const char *directory,
668 DissectImageFlags flags) {
669
670 const char *p, *options = NULL, *node, *fstype;
671 _cleanup_free_ char *chased = NULL;
672 bool rw;
673 int r;
674
675 assert(m);
676 assert(where);
677
678 node = m->decrypted_node ?: m->node;
679 fstype = m->decrypted_fstype ?: m->fstype;
680
681 if (!m->found || !node || !fstype)
682 return 0;
683
684 /* Stacked encryption? Yuck */
685 if (streq_ptr(fstype, "crypto_LUKS"))
686 return -ELOOP;
687
688 rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY);
689
690 if (directory) {
691 r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased);
692 if (r < 0)
693 return r;
694
695 p = chased;
696 } else
697 p = where;
698
699 /* If requested, turn on discard support. */
700 if (fstype_can_discard(fstype) &&
701 ((flags & DISSECT_IMAGE_DISCARD) ||
702 ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node))))
703 options = "discard";
704
705 return mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options);
706 }
707
708 int dissected_image_mount(DissectedImage *m, const char *where, DissectImageFlags flags) {
709 int r;
710
711 assert(m);
712 assert(where);
713
714 if (!m->partitions[PARTITION_ROOT].found)
715 return -ENXIO;
716
717 r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, flags);
718 if (r < 0)
719 return r;
720
721 r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", flags);
722 if (r < 0)
723 return r;
724
725 r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", flags);
726 if (r < 0)
727 return r;
728
729 if (m->partitions[PARTITION_ESP].found) {
730 const char *mp;
731
732 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
733
734 FOREACH_STRING(mp, "/efi", "/boot") {
735 _cleanup_free_ char *p = NULL;
736
737 r = chase_symlinks(mp, where, CHASE_PREFIX_ROOT, &p);
738 if (r < 0)
739 continue;
740
741 r = dir_is_empty(p);
742 if (r > 0) {
743 r = mount_partition(m->partitions + PARTITION_ESP, where, mp, flags);
744 if (r < 0)
745 return r;
746 }
747 }
748 }
749
750 return 0;
751 }
752
753 #if HAVE_LIBCRYPTSETUP
754 typedef struct DecryptedPartition {
755 struct crypt_device *device;
756 char *name;
757 bool relinquished;
758 } DecryptedPartition;
759
760 struct DecryptedImage {
761 DecryptedPartition *decrypted;
762 size_t n_decrypted;
763 size_t n_allocated;
764 };
765 #endif
766
767 DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
768 #if HAVE_LIBCRYPTSETUP
769 size_t i;
770 int r;
771
772 if (!d)
773 return NULL;
774
775 for (i = 0; i < d->n_decrypted; i++) {
776 DecryptedPartition *p = d->decrypted + i;
777
778 if (p->device && p->name && !p->relinquished) {
779 r = crypt_deactivate(p->device, p->name);
780 if (r < 0)
781 log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name);
782 }
783
784 if (p->device)
785 crypt_free(p->device);
786 free(p->name);
787 }
788
789 free(d);
790 #endif
791 return NULL;
792 }
793
794 #if HAVE_LIBCRYPTSETUP
795
796 static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) {
797 _cleanup_free_ char *name = NULL, *node = NULL;
798 const char *base;
799
800 assert(original_node);
801 assert(suffix);
802 assert(ret_name);
803 assert(ret_node);
804
805 base = strrchr(original_node, '/');
806 if (!base)
807 return -EINVAL;
808 base++;
809 if (isempty(base))
810 return -EINVAL;
811
812 name = strjoin(base, suffix);
813 if (!name)
814 return -ENOMEM;
815 if (!filename_is_valid(name))
816 return -EINVAL;
817
818 node = strjoin(crypt_get_dir(), "/", name);
819 if (!node)
820 return -ENOMEM;
821
822 *ret_name = name;
823 *ret_node = node;
824
825 name = node = NULL;
826 return 0;
827 }
828
829 static int decrypt_partition(
830 DissectedPartition *m,
831 const char *passphrase,
832 DissectImageFlags flags,
833 DecryptedImage *d) {
834
835 _cleanup_free_ char *node = NULL, *name = NULL;
836 struct crypt_device *cd;
837 int r;
838
839 assert(m);
840 assert(d);
841
842 if (!m->found || !m->node || !m->fstype)
843 return 0;
844
845 if (!streq(m->fstype, "crypto_LUKS"))
846 return 0;
847
848 r = make_dm_name_and_node(m->node, "-decrypted", &name, &node);
849 if (r < 0)
850 return r;
851
852 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
853 return -ENOMEM;
854
855 r = crypt_init(&cd, m->node);
856 if (r < 0)
857 return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
858
859 r = crypt_load(cd, CRYPT_LUKS1, NULL);
860 if (r < 0) {
861 log_debug_errno(r, "Failed to load LUKS metadata: %m");
862 goto fail;
863 }
864
865 r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
866 ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
867 ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
868 if (r < 0)
869 log_debug_errno(r, "Failed to activate LUKS device: %m");
870 if (r == -EPERM) {
871 r = -EKEYREJECTED;
872 goto fail;
873 }
874 if (r < 0)
875 goto fail;
876
877 d->decrypted[d->n_decrypted].name = name;
878 name = NULL;
879
880 d->decrypted[d->n_decrypted].device = cd;
881 d->n_decrypted++;
882
883 m->decrypted_node = node;
884 node = NULL;
885
886 return 0;
887
888 fail:
889 crypt_free(cd);
890 return r;
891 }
892
893 static int verity_partition(
894 DissectedPartition *m,
895 DissectedPartition *v,
896 const void *root_hash,
897 size_t root_hash_size,
898 DissectImageFlags flags,
899 DecryptedImage *d) {
900
901 _cleanup_free_ char *node = NULL, *name = NULL;
902 struct crypt_device *cd;
903 int r;
904
905 assert(m);
906 assert(v);
907
908 if (!root_hash)
909 return 0;
910
911 if (!m->found || !m->node || !m->fstype)
912 return 0;
913 if (!v->found || !v->node || !v->fstype)
914 return 0;
915
916 if (!streq(v->fstype, "DM_verity_hash"))
917 return 0;
918
919 r = make_dm_name_and_node(m->node, "-verity", &name, &node);
920 if (r < 0)
921 return r;
922
923 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
924 return -ENOMEM;
925
926 r = crypt_init(&cd, v->node);
927 if (r < 0)
928 return r;
929
930 r = crypt_load(cd, CRYPT_VERITY, NULL);
931 if (r < 0)
932 goto fail;
933
934 r = crypt_set_data_device(cd, m->node);
935 if (r < 0)
936 goto fail;
937
938 r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
939 if (r < 0)
940 goto fail;
941
942 d->decrypted[d->n_decrypted].name = name;
943 name = NULL;
944
945 d->decrypted[d->n_decrypted].device = cd;
946 d->n_decrypted++;
947
948 m->decrypted_node = node;
949 node = NULL;
950
951 return 0;
952
953 fail:
954 crypt_free(cd);
955 return r;
956 }
957 #endif
958
959 int dissected_image_decrypt(
960 DissectedImage *m,
961 const char *passphrase,
962 const void *root_hash,
963 size_t root_hash_size,
964 DissectImageFlags flags,
965 DecryptedImage **ret) {
966
967 _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
968 #if HAVE_LIBCRYPTSETUP
969 unsigned i;
970 int r;
971 #endif
972
973 assert(m);
974 assert(root_hash || root_hash_size == 0);
975
976 /* Returns:
977 *
978 * = 0 → There was nothing to decrypt
979 * > 0 → Decrypted successfully
980 * -ENOKEY → There's something to decrypt but no key was supplied
981 * -EKEYREJECTED → Passed key was not correct
982 */
983
984 if (root_hash && root_hash_size < sizeof(sd_id128_t))
985 return -EINVAL;
986
987 if (!m->encrypted && !m->verity) {
988 *ret = NULL;
989 return 0;
990 }
991
992 #if HAVE_LIBCRYPTSETUP
993 if (m->encrypted && !passphrase)
994 return -ENOKEY;
995
996 d = new0(DecryptedImage, 1);
997 if (!d)
998 return -ENOMEM;
999
1000 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
1001 DissectedPartition *p = m->partitions + i;
1002 int k;
1003
1004 if (!p->found)
1005 continue;
1006
1007 r = decrypt_partition(p, passphrase, flags, d);
1008 if (r < 0)
1009 return r;
1010
1011 k = PARTITION_VERITY_OF(i);
1012 if (k >= 0) {
1013 r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, flags, d);
1014 if (r < 0)
1015 return r;
1016 }
1017
1018 if (!p->decrypted_fstype && p->decrypted_node) {
1019 r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype);
1020 if (r < 0)
1021 return r;
1022 }
1023 }
1024
1025 *ret = d;
1026 d = NULL;
1027
1028 return 1;
1029 #else
1030 return -EOPNOTSUPP;
1031 #endif
1032 }
1033
1034 int dissected_image_decrypt_interactively(
1035 DissectedImage *m,
1036 const char *passphrase,
1037 const void *root_hash,
1038 size_t root_hash_size,
1039 DissectImageFlags flags,
1040 DecryptedImage **ret) {
1041
1042 _cleanup_strv_free_erase_ char **z = NULL;
1043 int n = 3, r;
1044
1045 if (passphrase)
1046 n--;
1047
1048 for (;;) {
1049 r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret);
1050 if (r >= 0)
1051 return r;
1052 if (r == -EKEYREJECTED)
1053 log_error_errno(r, "Incorrect passphrase, try again!");
1054 else if (r != -ENOKEY) {
1055 log_error_errno(r, "Failed to decrypt image: %m");
1056 return r;
1057 }
1058
1059 if (--n < 0) {
1060 log_error("Too many retries.");
1061 return -EKEYREJECTED;
1062 }
1063
1064 z = strv_free(z);
1065
1066 r = ask_password_auto("Please enter image passphrase!", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
1067 if (r < 0)
1068 return log_error_errno(r, "Failed to query for passphrase: %m");
1069
1070 passphrase = z[0];
1071 }
1072 }
1073
1074 #if HAVE_LIBCRYPTSETUP
1075 static int deferred_remove(DecryptedPartition *p) {
1076
1077 struct dm_ioctl dm = {
1078 .version = {
1079 DM_VERSION_MAJOR,
1080 DM_VERSION_MINOR,
1081 DM_VERSION_PATCHLEVEL
1082 },
1083 .data_size = sizeof(dm),
1084 .flags = DM_DEFERRED_REMOVE,
1085 };
1086
1087 _cleanup_close_ int fd = -1;
1088
1089 assert(p);
1090
1091 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1092
1093 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
1094 if (fd < 0)
1095 return -errno;
1096
1097 strncpy(dm.name, p->name, sizeof(dm.name));
1098
1099 if (ioctl(fd, DM_DEV_REMOVE, &dm))
1100 return -errno;
1101
1102 return 0;
1103 }
1104 #endif
1105
1106 int decrypted_image_relinquish(DecryptedImage *d) {
1107
1108 #if HAVE_LIBCRYPTSETUP
1109 size_t i;
1110 int r;
1111 #endif
1112
1113 assert(d);
1114
1115 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1116 * that we don't clean it up ourselves either anymore */
1117
1118 #if HAVE_LIBCRYPTSETUP
1119 for (i = 0; i < d->n_decrypted; i++) {
1120 DecryptedPartition *p = d->decrypted + i;
1121
1122 if (p->relinquished)
1123 continue;
1124
1125 r = deferred_remove(p);
1126 if (r < 0)
1127 return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name);
1128
1129 p->relinquished = true;
1130 }
1131 #endif
1132
1133 return 0;
1134 }
1135
1136 int root_hash_load(const char *image, void **ret, size_t *ret_size) {
1137 _cleanup_free_ char *text = NULL;
1138 _cleanup_free_ void *k = NULL;
1139 size_t l;
1140 int r;
1141
1142 assert(image);
1143 assert(ret);
1144 assert(ret_size);
1145
1146 if (is_device_path(image)) {
1147 /* If we are asked to load the root hash for a device node, exit early */
1148 *ret = NULL;
1149 *ret_size = 0;
1150 return 0;
1151 }
1152
1153 r = getxattr_malloc(image, "user.verity.roothash", &text, true);
1154 if (r < 0) {
1155 char *fn, *e, *n;
1156
1157 if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT))
1158 return r;
1159
1160 fn = newa(char, strlen(image) + strlen(".roothash") + 1);
1161 n = stpcpy(fn, image);
1162 e = endswith(fn, ".raw");
1163 if (e)
1164 n = e;
1165
1166 strcpy(n, ".roothash");
1167
1168 r = read_one_line_file(fn, &text);
1169 if (r == -ENOENT) {
1170 *ret = NULL;
1171 *ret_size = 0;
1172 return 0;
1173 }
1174 if (r < 0)
1175 return r;
1176 }
1177
1178 r = unhexmem(text, strlen(text), &k, &l);
1179 if (r < 0)
1180 return r;
1181 if (l < sizeof(sd_id128_t))
1182 return -EINVAL;
1183
1184 *ret = k;
1185 *ret_size = l;
1186
1187 k = NULL;
1188
1189 return 1;
1190 }
1191
1192 static const char *const partition_designator_table[] = {
1193 [PARTITION_ROOT] = "root",
1194 [PARTITION_ROOT_SECONDARY] = "root-secondary",
1195 [PARTITION_HOME] = "home",
1196 [PARTITION_SRV] = "srv",
1197 [PARTITION_ESP] = "esp",
1198 [PARTITION_SWAP] = "swap",
1199 [PARTITION_ROOT_VERITY] = "root-verity",
1200 [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity",
1201 };
1202
1203 DEFINE_STRING_TABLE_LOOKUP(partition_designator, int);