2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
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.
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.
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/>.
20 #if HAVE_LIBCRYPTSETUP
21 #include <libcryptsetup.h>
23 #define CRYPT_LUKS NULL
26 #include <sys/mount.h>
28 #include "architecture.h"
29 #include "ask-password-api.h"
30 #include "blkid-util.h"
31 #include "dissect-image.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"
45 #include "udev-util.h"
46 #include "xattr-util.h"
48 _unused_
static int probe_filesystem(const char *node
, char **ret_fstype
) {
50 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
54 b
= blkid_new_probe_from_filename(node
);
58 blkid_probe_enable_superblocks(b
, 1);
59 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
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
);
68 return -errno
?: -EIO
;
70 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
91 int dissect_image(int fd
, const void *root_hash
, size_t root_hash_size
, DissectImageFlags flags
, DissectedImage
**ret
) {
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
;
112 assert(root_hash
|| root_hash_size
== 0);
114 /* Probes a disk image, and returns information about what it found in *ret.
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. */
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
124 if (root_hash_size
< sizeof(sd_id128_t
))
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
));
130 if (sd_id128_is_null(root_uuid
))
132 if (sd_id128_is_null(verity_uuid
))
136 if (fstat(fd
, &st
) < 0)
139 if (!S_ISBLK(st
.st_mode
))
142 b
= blkid_new_probe();
147 r
= blkid_probe_set_device(b
, fd
, 0, 0);
149 return -errno
?: -ENOMEM
;
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
);
157 blkid_probe_enable_partitions(b
, 1);
158 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
161 r
= blkid_do_safeprobe(b
);
162 if (IN_SET(r
, -2, 1)) {
163 log_debug("Failed to identify any partition table.");
167 return -errno
?: -EIO
;
169 m
= new0(DissectedImage
, 1);
173 if (!(flags
& DISSECT_IMAGE_GPT_ONLY
) &&
174 (flags
& DISSECT_IMAGE_REQUIRE_ROOT
)) {
175 const char *usage
= NULL
;
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
;
182 /* OK, we have found a file system, that's our root partition then. */
183 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
191 if (asprintf(&n
, "/dev/block/%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
194 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
198 .architecture
= _ARCHITECTURE_INVALID
,
205 m
->encrypted
= streq(fstype
, "crypto_LUKS");
214 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
218 is_gpt
= streq_ptr(pttype
, "gpt");
219 is_mbr
= streq_ptr(pttype
, "dos");
221 if (!is_gpt
&& ((flags
& DISSECT_IMAGE_GPT_ONLY
) || !is_mbr
))
225 pl
= blkid_probe_get_partitions(b
);
227 return -errno
?: -ENOMEM
;
233 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
241 log_debug("Kernel partitions never appeared.");
245 e
= udev_enumerate_new(udev
);
249 r
= udev_enumerate_add_match_parent(e
, d
);
253 r
= udev_enumerate_scan_devices(e
);
257 /* Count the partitions enumerated by the kernel */
259 first
= udev_enumerate_get_list_entry(e
);
260 udev_list_entry_foreach(item
, first
)
263 /* Count the partitions enumerated by blkid */
264 z
= blkid_partlist_numof_partitions(pl
);
268 log_debug("blkid and kernel partition list do not match.");
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. */
282 if (ioctl(fd
, BLKRRPART
, 0) < 0) {
286 struct loop_info64 info
;
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. */
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
;
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.
306 * This is really something they should fix in the kernel! */
308 (void) usleep(50 * USEC_PER_MSEC
);
312 e
= udev_enumerate_unref(e
);
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
;
320 const char *node
, *sysname
;
324 q
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
328 qn
= udev_device_get_devnum(q
);
332 if (st
.st_rdev
== qn
)
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"))
341 node
= udev_device_get_devnode(q
);
345 pp
= blkid_partlist_devno_to_partition(pl
, qn
);
349 pflags
= blkid_partition_get_flags(pp
);
351 nr
= blkid_partition_get_partno(pp
);
356 int designator
= _PARTITION_DESIGNATOR_INVALID
, architecture
= _ARCHITECTURE_INVALID
;
357 const char *stype
, *sid
, *fstype
= NULL
;
358 sd_id128_t type_id
, id
;
361 sid
= blkid_partition_get_uuid(pp
);
364 if (sd_id128_from_string(sid
, &id
) < 0)
367 stype
= blkid_partition_get_type_string(pp
);
370 if (sd_id128_from_string(stype
, &type_id
) < 0)
373 if (sd_id128_equal(type_id
, GPT_HOME
)) {
375 if (pflags
& GPT_FLAG_NO_AUTO
)
378 designator
= PARTITION_HOME
;
379 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
380 } else if (sd_id128_equal(type_id
, GPT_SRV
)) {
382 if (pflags
& GPT_FLAG_NO_AUTO
)
385 designator
= PARTITION_SRV
;
386 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
387 } else if (sd_id128_equal(type_id
, GPT_ESP
)) {
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"). */
393 if (pflags
& GPT_FLAG_NO_BLOCK_IO_PROTOCOL
)
396 designator
= PARTITION_ESP
;
399 #ifdef GPT_ROOT_NATIVE
400 else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE
)) {
402 if (pflags
& GPT_FLAG_NO_AUTO
)
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
))
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
)) {
414 if (pflags
& GPT_FLAG_NO_AUTO
)
417 m
->can_verity
= true;
419 /* Ignore verity unless a root hash is specified */
420 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
423 designator
= PARTITION_ROOT_VERITY
;
424 fstype
= "DM_verity_hash";
425 architecture
= native_architecture();
429 #ifdef GPT_ROOT_SECONDARY
430 else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY
)) {
432 if (pflags
& GPT_FLAG_NO_AUTO
)
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
))
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
)) {
444 if (pflags
& GPT_FLAG_NO_AUTO
)
447 m
->can_verity
= true;
449 /* Ignore verity unless root has is specified */
450 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
453 designator
= PARTITION_ROOT_SECONDARY_VERITY
;
454 fstype
= "DM_verity_hash";
455 architecture
= SECONDARY_ARCHITECTURE
;
459 else if (sd_id128_equal(type_id
, GPT_SWAP
)) {
461 if (pflags
& GPT_FLAG_NO_AUTO
)
464 designator
= PARTITION_SWAP
;
466 } else if (sd_id128_equal(type_id
, GPT_LINUX_GENERIC
)) {
468 if (pflags
& GPT_FLAG_NO_AUTO
)
472 multiple_generic
= true;
475 generic_rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
477 generic_node
= strdup(node
);
483 if (designator
!= _PARTITION_DESIGNATOR_INVALID
) {
484 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
487 if (m
->partitions
[designator
].found
)
500 m
->partitions
[designator
] = (DissectedPartition
) {
504 .architecture
= architecture
,
515 if (pflags
!= 0x80) /* Bootable flag */
518 if (blkid_partition_get_type(pp
) != 0x83) /* Linux partition */
522 multiple_generic
= true;
526 generic_node
= strdup(node
);
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. */
537 if (m
->partitions
[PARTITION_ROOT_VERITY
].found
)
538 return -EADDRNOTAVAIL
;
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
]);
544 m
->partitions
[PARTITION_ROOT_VERITY
] = m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
];
545 zero(m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
]);
547 } else if (flags
& DISSECT_IMAGE_REQUIRE_ROOT
) {
549 /* If the root has was set, then we won't fallback to a generic node, because the root hash
552 return -EADDRNOTAVAIL
;
554 /* If we didn't find a generic node, then we can't fix this up either */
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
)
563 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
566 .partno
= generic_nr
,
567 .architecture
= _ARCHITECTURE_INVALID
,
568 .node
= generic_node
,
569 .uuid
= generic_uuid
,
577 if (!m
->partitions
[PARTITION_ROOT_VERITY
].found
|| !m
->partitions
[PARTITION_ROOT
].found
)
578 return -EADDRNOTAVAIL
;
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
583 m
->partitions
[PARTITION_ROOT_SECONDARY
].found
= false;
584 m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
].found
= false;
586 /* If we found a verity setup, then the root partition is necessarily read-only. */
587 m
->partitions
[PARTITION_ROOT
].rw
= false;
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
;
602 if (!p
->fstype
&& p
->node
) {
603 r
= probe_filesystem(p
->node
, &p
->fstype
);
608 if (streq_ptr(p
->fstype
, "crypto_LUKS"))
611 if (p
->fstype
&& fstype_is_ro(p
->fstype
))
624 DissectedImage
* dissected_image_unref(DissectedImage
*m
) {
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
);
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/")];
647 if (stat(path
, &st
) < 0)
650 if (!S_ISBLK(st
.st_mode
))
653 xsprintf(s
, "/sys/dev/block/%u:%u/loop/", major(st
.st_rdev
), minor(st
.st_rdev
));
654 if (access(s
, F_OK
) < 0) {
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
;
667 static int mount_partition(
668 DissectedPartition
*m
,
670 const char *directory
,
671 DissectImageFlags flags
) {
673 const char *p
, *options
= NULL
, *node
, *fstype
;
674 _cleanup_free_
char *chased
= NULL
;
681 node
= m
->decrypted_node
?: m
->node
;
682 fstype
= m
->decrypted_fstype
?: m
->fstype
;
684 if (!m
->found
|| !node
|| !fstype
)
687 /* Stacked encryption? Yuck */
688 if (streq_ptr(fstype
, "crypto_LUKS"))
691 rw
= m
->rw
&& !(flags
& DISSECT_IMAGE_READ_ONLY
);
694 r
= chase_symlinks(directory
, where
, CHASE_PREFIX_ROOT
, &chased
);
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
))))
708 return mount_verbose(LOG_DEBUG
, node
, p
, fstype
, MS_NODEV
|(rw
? 0 : MS_RDONLY
), options
);
711 int dissected_image_mount(DissectedImage
*m
, const char *where
, DissectImageFlags flags
) {
717 if (!m
->partitions
[PARTITION_ROOT
].found
)
720 r
= mount_partition(m
->partitions
+ PARTITION_ROOT
, where
, NULL
, flags
);
724 r
= mount_partition(m
->partitions
+ PARTITION_HOME
, where
, "/home", flags
);
728 r
= mount_partition(m
->partitions
+ PARTITION_SRV
, where
, "/srv", flags
);
732 if (m
->partitions
[PARTITION_ESP
].found
) {
735 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
737 FOREACH_STRING(mp
, "/efi", "/boot") {
738 _cleanup_free_
char *p
= NULL
;
740 r
= chase_symlinks(mp
, where
, CHASE_PREFIX_ROOT
, &p
);
746 r
= mount_partition(m
->partitions
+ PARTITION_ESP
, where
, mp
, flags
);
756 #if HAVE_LIBCRYPTSETUP
757 typedef struct DecryptedPartition
{
758 struct crypt_device
*device
;
761 } DecryptedPartition
;
763 struct DecryptedImage
{
764 DecryptedPartition
*decrypted
;
770 DecryptedImage
* decrypted_image_unref(DecryptedImage
* d
) {
771 #if HAVE_LIBCRYPTSETUP
778 for (i
= 0; i
< d
->n_decrypted
; i
++) {
779 DecryptedPartition
*p
= d
->decrypted
+ i
;
781 if (p
->device
&& p
->name
&& !p
->relinquished
) {
782 r
= crypt_deactivate(p
->device
, p
->name
);
784 log_debug_errno(r
, "Failed to deactivate encrypted partition %s", p
->name
);
788 crypt_free(p
->device
);
797 #if HAVE_LIBCRYPTSETUP
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
;
803 assert(original_node
);
808 base
= strrchr(original_node
, '/');
815 name
= strjoin(base
, suffix
);
818 if (!filename_is_valid(name
))
821 node
= strjoin(crypt_get_dir(), "/", name
);
832 static int decrypt_partition(
833 DissectedPartition
*m
,
834 const char *passphrase
,
835 DissectImageFlags flags
,
838 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
839 struct crypt_device
*cd
;
845 if (!m
->found
|| !m
->node
|| !m
->fstype
)
848 if (!streq(m
->fstype
, "crypto_LUKS"))
851 r
= make_dm_name_and_node(m
->node
, "-decrypted", &name
, &node
);
855 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
858 r
= crypt_init(&cd
, m
->node
);
860 return log_debug_errno(r
, "Failed to initialize dm-crypt: %m");
862 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
864 log_debug_errno(r
, "Failed to load LUKS metadata: %m");
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));
872 log_debug_errno(r
, "Failed to activate LUKS device: %m");
880 d
->decrypted
[d
->n_decrypted
].name
= name
;
883 d
->decrypted
[d
->n_decrypted
].device
= cd
;
886 m
->decrypted_node
= node
;
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
,
904 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
905 struct crypt_device
*cd
;
914 if (!m
->found
|| !m
->node
|| !m
->fstype
)
916 if (!v
->found
|| !v
->node
|| !v
->fstype
)
919 if (!streq(v
->fstype
, "DM_verity_hash"))
922 r
= make_dm_name_and_node(m
->node
, "-verity", &name
, &node
);
926 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
929 r
= crypt_init(&cd
, v
->node
);
933 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
937 r
= crypt_set_data_device(cd
, m
->node
);
941 r
= crypt_activate_by_volume_key(cd
, name
, root_hash
, root_hash_size
, CRYPT_ACTIVATE_READONLY
);
945 d
->decrypted
[d
->n_decrypted
].name
= name
;
948 d
->decrypted
[d
->n_decrypted
].device
= cd
;
951 m
->decrypted_node
= node
;
962 int dissected_image_decrypt(
964 const char *passphrase
,
965 const void *root_hash
,
966 size_t root_hash_size
,
967 DissectImageFlags flags
,
968 DecryptedImage
**ret
) {
970 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*d
= NULL
;
971 #if HAVE_LIBCRYPTSETUP
977 assert(root_hash
|| root_hash_size
== 0);
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
987 if (root_hash
&& root_hash_size
< sizeof(sd_id128_t
))
990 if (!m
->encrypted
&& !m
->verity
) {
995 #if HAVE_LIBCRYPTSETUP
996 if (m
->encrypted
&& !passphrase
)
999 d
= new0(DecryptedImage
, 1);
1003 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
1004 DissectedPartition
*p
= m
->partitions
+ i
;
1010 r
= decrypt_partition(p
, passphrase
, flags
, d
);
1014 k
= PARTITION_VERITY_OF(i
);
1016 r
= verity_partition(p
, m
->partitions
+ k
, root_hash
, root_hash_size
, flags
, d
);
1021 if (!p
->decrypted_fstype
&& p
->decrypted_node
) {
1022 r
= probe_filesystem(p
->decrypted_node
, &p
->decrypted_fstype
);
1037 int dissected_image_decrypt_interactively(
1039 const char *passphrase
,
1040 const void *root_hash
,
1041 size_t root_hash_size
,
1042 DissectImageFlags flags
,
1043 DecryptedImage
**ret
) {
1045 _cleanup_strv_free_erase_
char **z
= NULL
;
1052 r
= dissected_image_decrypt(m
, passphrase
, root_hash
, root_hash_size
, flags
, ret
);
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");
1063 log_error("Too many retries.");
1064 return -EKEYREJECTED
;
1069 r
= ask_password_auto("Please enter image passphrase!", NULL
, "dissect", "dissect", USEC_INFINITY
, 0, &z
);
1071 return log_error_errno(r
, "Failed to query for passphrase: %m");
1077 #if HAVE_LIBCRYPTSETUP
1078 static int deferred_remove(DecryptedPartition
*p
) {
1080 struct dm_ioctl dm
= {
1084 DM_VERSION_PATCHLEVEL
1086 .data_size
= sizeof(dm
),
1087 .flags
= DM_DEFERRED_REMOVE
,
1090 _cleanup_close_
int fd
= -1;
1094 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1096 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
1100 strncpy(dm
.name
, p
->name
, sizeof(dm
.name
));
1102 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
))
1109 int decrypted_image_relinquish(DecryptedImage
*d
) {
1111 #if HAVE_LIBCRYPTSETUP
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 */
1121 #if HAVE_LIBCRYPTSETUP
1122 for (i
= 0; i
< d
->n_decrypted
; i
++) {
1123 DecryptedPartition
*p
= d
->decrypted
+ i
;
1125 if (p
->relinquished
)
1128 r
= deferred_remove(p
);
1130 return log_debug_errno(r
, "Failed to mark %s for auto-removal: %m", p
->name
);
1132 p
->relinquished
= true;
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
;
1149 if (is_device_path(image
)) {
1150 /* If we are asked to load the root hash for a device node, exit early */
1156 r
= getxattr_malloc(image
, "user.verity.roothash", &text
, true);
1160 if (!IN_SET(r
, -ENODATA
, -EOPNOTSUPP
, -ENOENT
))
1163 fn
= newa(char, strlen(image
) + strlen(".roothash") + 1);
1164 n
= stpcpy(fn
, image
);
1165 e
= endswith(fn
, ".raw");
1169 strcpy(n
, ".roothash");
1171 r
= read_one_line_file(fn
, &text
);
1181 r
= unhexmem(text
, strlen(text
), &k
, &l
);
1184 if (l
< sizeof(sd_id128_t
))
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",
1206 DEFINE_STRING_TABLE_LOOKUP(partition_designator
, int);