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