1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "architecture.h"
11 #include "ask-password-api.h"
12 #include "blkid-util.h"
13 #include "blockdev-util.h"
15 #include "crypt-util.h"
17 #include "device-enumerator-private.h"
18 #include "device-nodes.h"
19 #include "dissect-image.h"
24 #include "hexdecoct.h"
25 #include "hostname-util.h"
26 #include "id128-util.h"
27 #include "linux-3.13/dm-ioctl.h"
29 #include "mount-util.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "raw-clone.h"
34 #include "signal-util.h"
35 #include "stat-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
40 #include "user-util.h"
41 #include "xattr-util.h"
43 int probe_filesystem(const char *node
, char **ret_fstype
) {
44 /* Try to find device content type and return it in *ret_fstype. If nothing is found,
45 * 0/NULL will be returned. -EUCLEAN will be returned for ambigous results, and an
46 * different error otherwise. */
49 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
54 b
= blkid_new_probe_from_filename(node
);
56 return -errno
?: -ENOMEM
;
58 blkid_probe_enable_superblocks(b
, 1);
59 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
62 r
= blkid_do_safeprobe(b
);
64 log_debug("No type detected on partition %s", node
);
68 log_debug("Results ambiguous for partition %s", node
);
72 return -errno
?: -EIO
;
74 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
96 /* Detect RPMB and Boot partitions, which are not listed by blkid.
97 * See https://github.com/systemd/systemd/issues/5806. */
98 static bool device_is_mmc_special_partition(sd_device
*d
) {
101 if (sd_device_get_sysname(d
, &sysname
) < 0)
104 return startswith(sysname
, "mmcblk") &&
105 (endswith(sysname
, "rpmb") || endswith(sysname
, "boot0") || endswith(sysname
, "boot1"));
108 static bool device_is_block(sd_device
*d
) {
111 if (sd_device_get_subsystem(d
, &ss
) < 0)
114 return streq(ss
, "block");
120 const void *root_hash
,
121 size_t root_hash_size
,
122 DissectImageFlags flags
,
123 DissectedImage
**ret
) {
126 sd_id128_t root_uuid
= SD_ID128_NULL
, verity_uuid
= SD_ID128_NULL
;
127 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
128 bool is_gpt
, is_mbr
, generic_rw
, multiple_generic
= false;
129 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
130 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
131 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
132 _cleanup_free_
char *generic_node
= NULL
;
133 sd_id128_t generic_uuid
= SD_ID128_NULL
;
134 const char *pttype
= NULL
;
143 assert(root_hash
|| root_hash_size
== 0);
145 /* Probes a disk image, and returns information about what it found in *ret.
147 * Returns -ENOPKG if no suitable partition table or file system could be found.
148 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
151 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
152 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
155 if (root_hash_size
< sizeof(sd_id128_t
))
158 memcpy(&root_uuid
, root_hash
, sizeof(sd_id128_t
));
159 memcpy(&verity_uuid
, (const uint8_t*) root_hash
+ root_hash_size
- sizeof(sd_id128_t
), sizeof(sd_id128_t
));
161 if (sd_id128_is_null(root_uuid
))
163 if (sd_id128_is_null(verity_uuid
))
167 if (fstat(fd
, &st
) < 0)
170 if (!S_ISBLK(st
.st_mode
))
173 b
= blkid_new_probe();
178 r
= blkid_probe_set_device(b
, fd
, 0, 0);
180 return -errno
?: -ENOMEM
;
182 if ((flags
& DISSECT_IMAGE_GPT_ONLY
) == 0) {
183 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
184 blkid_probe_enable_superblocks(b
, 1);
185 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
|BLKID_SUBLKS_USAGE
);
188 blkid_probe_enable_partitions(b
, 1);
189 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
192 r
= blkid_do_safeprobe(b
);
193 if (IN_SET(r
, -2, 1)) {
194 log_debug("Failed to identify any partition table.");
198 return -errno
?: -EIO
;
200 m
= new0(DissectedImage
, 1);
204 if (!(flags
& DISSECT_IMAGE_GPT_ONLY
) &&
205 (flags
& DISSECT_IMAGE_REQUIRE_ROOT
)) {
206 const char *usage
= NULL
;
208 (void) blkid_probe_lookup_value(b
, "USAGE", &usage
, NULL
);
209 if (STRPTR_IN_SET(usage
, "filesystem", "crypto")) {
210 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
211 const char *fstype
= NULL
;
213 /* OK, we have found a file system, that's our root partition then. */
214 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
222 if (asprintf(&n
, "/dev/block/%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
225 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
229 .architecture
= _ARCHITECTURE_INVALID
,
230 .fstype
= TAKE_PTR(t
),
234 m
->encrypted
= streq(fstype
, "crypto_LUKS");
242 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
246 is_gpt
= streq_ptr(pttype
, "gpt");
247 is_mbr
= streq_ptr(pttype
, "dos");
249 if (!is_gpt
&& ((flags
& DISSECT_IMAGE_GPT_ONLY
) || !is_mbr
))
253 pl
= blkid_probe_get_partitions(b
);
255 return -errno
?: -ENOMEM
;
257 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
265 log_debug("Kernel partitions never appeared.");
269 r
= sd_device_enumerator_new(&e
);
273 r
= sd_device_enumerator_allow_uninitialized(e
);
277 r
= sd_device_enumerator_add_match_parent(e
, d
);
281 r
= device_enumerator_scan_devices(e
);
285 /* Count the partitions enumerated by the kernel */
287 FOREACH_DEVICE_AND_SUBSYSTEM(e
, q
) {
290 if (sd_device_get_devnum(q
, &qn
) < 0)
293 if (!device_is_block(q
))
296 if (device_is_mmc_special_partition(q
))
301 /* Count the partitions enumerated by blkid */
302 z
= blkid_partlist_numof_partitions(pl
);
306 log_debug("blkid and kernel partition list do not match.");
312 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
313 * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
314 * synchronous call that waits until probing is complete. */
320 if (ioctl(fd
, BLKRRPART
, 0) < 0) {
324 struct loop_info64 info
;
326 /* If we are running on a loop device that has partition scanning off,
327 * return an explicit recognizable error about this, so that callers
328 * can generate a proper message explaining the situation. */
330 if (ioctl(fd
, LOOP_GET_STATUS64
, &info
) >= 0 && (info
.lo_flags
& LO_FLAGS_PARTSCAN
) == 0) {
331 log_debug("Device is loop device and partition scanning is off!");
332 return -EPROTONOSUPPORT
;
340 /* If something else has the device open, such as an udev rule, the ioctl will return
341 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
342 * bit, and try again.
344 * This is really something they should fix in the kernel! */
346 (void) usleep(50 * USEC_PER_MSEC
);
350 e
= sd_device_enumerator_unref(e
);
353 FOREACH_DEVICE_AND_SUBSYSTEM(e
, q
) {
354 unsigned long long pflags
;
360 r
= sd_device_get_devnum(q
, &qn
);
364 if (st
.st_rdev
== qn
)
367 if (!device_is_block(q
))
370 if (device_is_mmc_special_partition(q
))
373 r
= sd_device_get_devname(q
, &node
);
377 pp
= blkid_partlist_devno_to_partition(pl
, qn
);
381 pflags
= blkid_partition_get_flags(pp
);
383 nr
= blkid_partition_get_partno(pp
);
388 int designator
= _PARTITION_DESIGNATOR_INVALID
, architecture
= _ARCHITECTURE_INVALID
;
389 const char *stype
, *sid
, *fstype
= NULL
;
390 sd_id128_t type_id
, id
;
393 sid
= blkid_partition_get_uuid(pp
);
396 if (sd_id128_from_string(sid
, &id
) < 0)
399 stype
= blkid_partition_get_type_string(pp
);
402 if (sd_id128_from_string(stype
, &type_id
) < 0)
405 if (sd_id128_equal(type_id
, GPT_HOME
)) {
407 if (pflags
& GPT_FLAG_NO_AUTO
)
410 designator
= PARTITION_HOME
;
411 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
412 } else if (sd_id128_equal(type_id
, GPT_SRV
)) {
414 if (pflags
& GPT_FLAG_NO_AUTO
)
417 designator
= PARTITION_SRV
;
418 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
419 } else if (sd_id128_equal(type_id
, GPT_ESP
)) {
421 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
422 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
423 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
425 if (pflags
& GPT_FLAG_NO_BLOCK_IO_PROTOCOL
)
428 designator
= PARTITION_ESP
;
431 #ifdef GPT_ROOT_NATIVE
432 else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE
)) {
434 if (pflags
& GPT_FLAG_NO_AUTO
)
437 /* If a root ID is specified, ignore everything but the root id */
438 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
441 designator
= PARTITION_ROOT
;
442 architecture
= native_architecture();
443 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
444 } else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE_VERITY
)) {
446 if (pflags
& GPT_FLAG_NO_AUTO
)
449 m
->can_verity
= true;
451 /* Ignore verity unless a root hash is specified */
452 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
455 designator
= PARTITION_ROOT_VERITY
;
456 fstype
= "DM_verity_hash";
457 architecture
= native_architecture();
461 #ifdef GPT_ROOT_SECONDARY
462 else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY
)) {
464 if (pflags
& GPT_FLAG_NO_AUTO
)
467 /* If a root ID is specified, ignore everything but the root id */
468 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
471 designator
= PARTITION_ROOT_SECONDARY
;
472 architecture
= SECONDARY_ARCHITECTURE
;
473 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
474 } else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY_VERITY
)) {
476 if (pflags
& GPT_FLAG_NO_AUTO
)
479 m
->can_verity
= true;
481 /* Ignore verity unless root has is specified */
482 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
485 designator
= PARTITION_ROOT_SECONDARY_VERITY
;
486 fstype
= "DM_verity_hash";
487 architecture
= SECONDARY_ARCHITECTURE
;
491 else if (sd_id128_equal(type_id
, GPT_SWAP
)) {
493 if (pflags
& GPT_FLAG_NO_AUTO
)
496 designator
= PARTITION_SWAP
;
498 } else if (sd_id128_equal(type_id
, GPT_LINUX_GENERIC
)) {
500 if (pflags
& GPT_FLAG_NO_AUTO
)
504 multiple_generic
= true;
507 generic_rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
509 generic_node
= strdup(node
);
515 if (designator
!= _PARTITION_DESIGNATOR_INVALID
) {
516 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
519 if (m
->partitions
[designator
].found
)
532 m
->partitions
[designator
] = (DissectedPartition
) {
536 .architecture
= architecture
,
538 .fstype
= TAKE_PTR(t
),
545 if (pflags
!= 0x80) /* Bootable flag */
548 if (blkid_partition_get_type(pp
) != 0x83) /* Linux partition */
552 multiple_generic
= true;
556 generic_node
= strdup(node
);
563 if (!m
->partitions
[PARTITION_ROOT
].found
) {
564 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
565 * either, then check if there's a single generic one, and use that. */
567 if (m
->partitions
[PARTITION_ROOT_VERITY
].found
)
568 return -EADDRNOTAVAIL
;
570 if (m
->partitions
[PARTITION_ROOT_SECONDARY
].found
) {
571 m
->partitions
[PARTITION_ROOT
] = m
->partitions
[PARTITION_ROOT_SECONDARY
];
572 zero(m
->partitions
[PARTITION_ROOT_SECONDARY
]);
574 m
->partitions
[PARTITION_ROOT_VERITY
] = m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
];
575 zero(m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
]);
577 } else if (flags
& DISSECT_IMAGE_REQUIRE_ROOT
) {
579 /* If the root has was set, then we won't fallback to a generic node, because the root hash
582 return -EADDRNOTAVAIL
;
584 /* If we didn't find a generic node, then we can't fix this up either */
588 /* If we didn't find a properly marked root partition, but we did find a single suitable
589 * generic Linux partition, then use this as root partition, if the caller asked for it. */
590 if (multiple_generic
)
593 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
596 .partno
= generic_nr
,
597 .architecture
= _ARCHITECTURE_INVALID
,
598 .node
= TAKE_PTR(generic_node
),
599 .uuid
= generic_uuid
,
605 if (!m
->partitions
[PARTITION_ROOT_VERITY
].found
|| !m
->partitions
[PARTITION_ROOT
].found
)
606 return -EADDRNOTAVAIL
;
608 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
609 * (which would be weird, after all the root hash should only be assigned to one pair of
611 m
->partitions
[PARTITION_ROOT_SECONDARY
].found
= false;
612 m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
].found
= false;
614 /* If we found a verity setup, then the root partition is necessarily read-only. */
615 m
->partitions
[PARTITION_ROOT
].rw
= false;
623 /* Fill in file system types if we don't know them yet. */
624 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
625 DissectedPartition
*p
= m
->partitions
+ i
;
630 if (!p
->fstype
&& p
->node
) {
631 r
= probe_filesystem(p
->node
, &p
->fstype
);
632 if (r
< 0 && r
!= -EUCLEAN
)
636 if (streq_ptr(p
->fstype
, "crypto_LUKS"))
639 if (p
->fstype
&& fstype_is_ro(p
->fstype
))
651 DissectedImage
* dissected_image_unref(DissectedImage
*m
) {
657 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
658 free(m
->partitions
[i
].fstype
);
659 free(m
->partitions
[i
].node
);
660 free(m
->partitions
[i
].decrypted_fstype
);
661 free(m
->partitions
[i
].decrypted_node
);
665 strv_free(m
->machine_info
);
666 strv_free(m
->os_release
);
671 static int is_loop_device(const char *path
) {
672 char s
[SYS_BLOCK_PATH_MAX("/../loop/")];
677 if (stat(path
, &st
) < 0)
680 if (!S_ISBLK(st
.st_mode
))
683 xsprintf_sys_block_path(s
, "/loop/", st
.st_dev
);
684 if (access(s
, F_OK
) < 0) {
688 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
689 xsprintf_sys_block_path(s
, "/../loop/", st
.st_dev
);
690 if (access(s
, F_OK
) < 0)
691 return errno
== ENOENT
? false : -errno
;
697 static int mount_partition(
698 DissectedPartition
*m
,
700 const char *directory
,
702 DissectImageFlags flags
) {
704 _cleanup_free_
char *chased
= NULL
, *options
= NULL
;
705 const char *p
, *node
, *fstype
;
712 node
= m
->decrypted_node
?: m
->node
;
713 fstype
= m
->decrypted_fstype
?: m
->fstype
;
715 if (!m
->found
|| !node
|| !fstype
)
718 /* Stacked encryption? Yuck */
719 if (streq_ptr(fstype
, "crypto_LUKS"))
722 rw
= m
->rw
&& !(flags
& DISSECT_IMAGE_READ_ONLY
);
725 r
= chase_symlinks(directory
, where
, CHASE_PREFIX_ROOT
, &chased
);
733 /* If requested, turn on discard support. */
734 if (fstype_can_discard(fstype
) &&
735 ((flags
& DISSECT_IMAGE_DISCARD
) ||
736 ((flags
& DISSECT_IMAGE_DISCARD_ON_LOOP
) && is_loop_device(m
->node
)))) {
737 options
= strdup("discard");
742 if (uid_is_valid(uid_shift
) && uid_shift
!= 0 && fstype_can_uid_gid(fstype
)) {
743 _cleanup_free_
char *uid_option
= NULL
;
745 if (asprintf(&uid_option
, "uid=" UID_FMT
",gid=" GID_FMT
, uid_shift
, (gid_t
) uid_shift
) < 0)
748 if (!strextend_with_separator(&options
, ",", uid_option
, NULL
))
752 return mount_verbose(LOG_DEBUG
, node
, p
, fstype
, MS_NODEV
|(rw
? 0 : MS_RDONLY
), options
);
755 int dissected_image_mount(DissectedImage
*m
, const char *where
, uid_t uid_shift
, DissectImageFlags flags
) {
761 if (!m
->partitions
[PARTITION_ROOT
].found
)
764 if ((flags
& DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY
) == 0) {
765 r
= mount_partition(m
->partitions
+ PARTITION_ROOT
, where
, NULL
, uid_shift
, flags
);
769 if (flags
& DISSECT_IMAGE_VALIDATE_OS
) {
770 r
= path_is_os_tree(where
);
778 if ((flags
& DISSECT_IMAGE_MOUNT_ROOT_ONLY
))
781 r
= mount_partition(m
->partitions
+ PARTITION_HOME
, where
, "/home", uid_shift
, flags
);
785 r
= mount_partition(m
->partitions
+ PARTITION_SRV
, where
, "/srv", uid_shift
, flags
);
789 if (m
->partitions
[PARTITION_ESP
].found
) {
792 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
794 FOREACH_STRING(mp
, "/efi", "/boot") {
795 _cleanup_free_
char *p
= NULL
;
797 r
= chase_symlinks(mp
, where
, CHASE_PREFIX_ROOT
, &p
);
803 r
= mount_partition(m
->partitions
+ PARTITION_ESP
, where
, mp
, uid_shift
, flags
);
813 #if HAVE_LIBCRYPTSETUP
814 typedef struct DecryptedPartition
{
815 struct crypt_device
*device
;
818 } DecryptedPartition
;
820 struct DecryptedImage
{
821 DecryptedPartition
*decrypted
;
827 DecryptedImage
* decrypted_image_unref(DecryptedImage
* d
) {
828 #if HAVE_LIBCRYPTSETUP
835 for (i
= 0; i
< d
->n_decrypted
; i
++) {
836 DecryptedPartition
*p
= d
->decrypted
+ i
;
838 if (p
->device
&& p
->name
&& !p
->relinquished
) {
839 r
= crypt_deactivate(p
->device
, p
->name
);
841 log_debug_errno(r
, "Failed to deactivate encrypted partition %s", p
->name
);
845 crypt_free(p
->device
);
854 #if HAVE_LIBCRYPTSETUP
856 static int make_dm_name_and_node(const void *original_node
, const char *suffix
, char **ret_name
, char **ret_node
) {
857 _cleanup_free_
char *name
= NULL
, *node
= NULL
;
860 assert(original_node
);
865 base
= strrchr(original_node
, '/');
872 name
= strjoin(base
, suffix
);
875 if (!filename_is_valid(name
))
878 node
= strjoin(crypt_get_dir(), "/", name
);
882 *ret_name
= TAKE_PTR(name
);
883 *ret_node
= TAKE_PTR(node
);
888 static int decrypt_partition(
889 DissectedPartition
*m
,
890 const char *passphrase
,
891 DissectImageFlags flags
,
894 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
895 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
901 if (!m
->found
|| !m
->node
|| !m
->fstype
)
904 if (!streq(m
->fstype
, "crypto_LUKS"))
910 r
= make_dm_name_and_node(m
->node
, "-decrypted", &name
, &node
);
914 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
917 r
= crypt_init(&cd
, m
->node
);
919 return log_debug_errno(r
, "Failed to initialize dm-crypt: %m");
921 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
923 return log_debug_errno(r
, "Failed to load LUKS metadata: %m");
925 r
= crypt_activate_by_passphrase(cd
, name
, CRYPT_ANY_SLOT
, passphrase
, strlen(passphrase
),
926 ((flags
& DISSECT_IMAGE_READ_ONLY
) ? CRYPT_ACTIVATE_READONLY
: 0) |
927 ((flags
& DISSECT_IMAGE_DISCARD_ON_CRYPTO
) ? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0));
929 log_debug_errno(r
, "Failed to activate LUKS device: %m");
930 return r
== -EPERM
? -EKEYREJECTED
: r
;
933 d
->decrypted
[d
->n_decrypted
].name
= TAKE_PTR(name
);
934 d
->decrypted
[d
->n_decrypted
].device
= TAKE_PTR(cd
);
937 m
->decrypted_node
= TAKE_PTR(node
);
942 static int verity_partition(
943 DissectedPartition
*m
,
944 DissectedPartition
*v
,
945 const void *root_hash
,
946 size_t root_hash_size
,
947 DissectImageFlags flags
,
950 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
951 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
960 if (!m
->found
|| !m
->node
|| !m
->fstype
)
962 if (!v
->found
|| !v
->node
|| !v
->fstype
)
965 if (!streq(v
->fstype
, "DM_verity_hash"))
968 r
= make_dm_name_and_node(m
->node
, "-verity", &name
, &node
);
972 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
975 r
= crypt_init(&cd
, v
->node
);
979 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
983 r
= crypt_set_data_device(cd
, m
->node
);
987 r
= crypt_activate_by_volume_key(cd
, name
, root_hash
, root_hash_size
, CRYPT_ACTIVATE_READONLY
);
991 d
->decrypted
[d
->n_decrypted
].name
= TAKE_PTR(name
);
992 d
->decrypted
[d
->n_decrypted
].device
= TAKE_PTR(cd
);
995 m
->decrypted_node
= TAKE_PTR(node
);
1001 int dissected_image_decrypt(
1003 const char *passphrase
,
1004 const void *root_hash
,
1005 size_t root_hash_size
,
1006 DissectImageFlags flags
,
1007 DecryptedImage
**ret
) {
1009 #if HAVE_LIBCRYPTSETUP
1010 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*d
= NULL
;
1016 assert(root_hash
|| root_hash_size
== 0);
1020 * = 0 → There was nothing to decrypt
1021 * > 0 → Decrypted successfully
1022 * -ENOKEY → There's something to decrypt but no key was supplied
1023 * -EKEYREJECTED → Passed key was not correct
1026 if (root_hash
&& root_hash_size
< sizeof(sd_id128_t
))
1029 if (!m
->encrypted
&& !m
->verity
) {
1034 #if HAVE_LIBCRYPTSETUP
1035 d
= new0(DecryptedImage
, 1);
1039 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
1040 DissectedPartition
*p
= m
->partitions
+ i
;
1046 r
= decrypt_partition(p
, passphrase
, flags
, d
);
1050 k
= PARTITION_VERITY_OF(i
);
1052 r
= verity_partition(p
, m
->partitions
+ k
, root_hash
, root_hash_size
, flags
, d
);
1057 if (!p
->decrypted_fstype
&& p
->decrypted_node
) {
1058 r
= probe_filesystem(p
->decrypted_node
, &p
->decrypted_fstype
);
1059 if (r
< 0 && r
!= -EUCLEAN
)
1072 int dissected_image_decrypt_interactively(
1074 const char *passphrase
,
1075 const void *root_hash
,
1076 size_t root_hash_size
,
1077 DissectImageFlags flags
,
1078 DecryptedImage
**ret
) {
1080 _cleanup_strv_free_erase_
char **z
= NULL
;
1087 r
= dissected_image_decrypt(m
, passphrase
, root_hash
, root_hash_size
, flags
, ret
);
1090 if (r
== -EKEYREJECTED
)
1091 log_error_errno(r
, "Incorrect passphrase, try again!");
1092 else if (r
!= -ENOKEY
)
1093 return log_error_errno(r
, "Failed to decrypt image: %m");
1096 log_error("Too many retries.");
1097 return -EKEYREJECTED
;
1102 r
= ask_password_auto("Please enter image passphrase!", NULL
, "dissect", "dissect", USEC_INFINITY
, 0, &z
);
1104 return log_error_errno(r
, "Failed to query for passphrase: %m");
1110 #if HAVE_LIBCRYPTSETUP
1111 static int deferred_remove(DecryptedPartition
*p
) {
1113 struct dm_ioctl dm
= {
1117 DM_VERSION_PATCHLEVEL
1119 .data_size
= sizeof(dm
),
1120 .flags
= DM_DEFERRED_REMOVE
,
1123 _cleanup_close_
int fd
= -1;
1127 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1129 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
1133 strncpy(dm
.name
, p
->name
, sizeof(dm
.name
));
1135 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
))
1142 int decrypted_image_relinquish(DecryptedImage
*d
) {
1144 #if HAVE_LIBCRYPTSETUP
1151 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1152 * that we don't clean it up ourselves either anymore */
1154 #if HAVE_LIBCRYPTSETUP
1155 for (i
= 0; i
< d
->n_decrypted
; i
++) {
1156 DecryptedPartition
*p
= d
->decrypted
+ i
;
1158 if (p
->relinquished
)
1161 r
= deferred_remove(p
);
1163 return log_debug_errno(r
, "Failed to mark %s for auto-removal: %m", p
->name
);
1165 p
->relinquished
= true;
1172 int root_hash_load(const char *image
, void **ret
, size_t *ret_size
) {
1173 _cleanup_free_
char *text
= NULL
;
1174 _cleanup_free_
void *k
= NULL
;
1182 if (is_device_path(image
)) {
1183 /* If we are asked to load the root hash for a device node, exit early */
1189 r
= getxattr_malloc(image
, "user.verity.roothash", &text
, true);
1193 if (!IN_SET(r
, -ENODATA
, -EOPNOTSUPP
, -ENOENT
))
1196 fn
= newa(char, strlen(image
) + STRLEN(".roothash") + 1);
1197 n
= stpcpy(fn
, image
);
1198 e
= endswith(fn
, ".raw");
1202 strcpy(n
, ".roothash");
1204 r
= read_one_line_file(fn
, &text
);
1214 r
= unhexmem(text
, strlen(text
), &k
, &l
);
1217 if (l
< sizeof(sd_id128_t
))
1226 int dissected_image_acquire_metadata(DissectedImage
*m
) {
1236 static const char *const paths
[_META_MAX
] = {
1237 [META_HOSTNAME
] = "/etc/hostname\0",
1238 [META_MACHINE_ID
] = "/etc/machine-id\0",
1239 [META_MACHINE_INFO
] = "/etc/machine-info\0",
1240 [META_OS_RELEASE
] = "/etc/os-release\0/usr/lib/os-release\0",
1243 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1244 _cleanup_(rmdir_and_freep
) char *t
= NULL
;
1245 _cleanup_(sigkill_waitp
) pid_t child
= 0;
1246 sd_id128_t machine_id
= SD_ID128_NULL
;
1247 _cleanup_free_
char *hostname
= NULL
;
1248 unsigned n_meta_initialized
= 0, k
;
1249 int fds
[2 * _META_MAX
], r
;
1251 BLOCK_SIGNALS(SIGCHLD
);
1255 for (; n_meta_initialized
< _META_MAX
; n_meta_initialized
++)
1256 if (pipe2(fds
+ 2*n_meta_initialized
, O_CLOEXEC
) < 0) {
1261 r
= mkdtemp_malloc("/tmp/dissect-XXXXXX", &t
);
1265 r
= safe_fork("(sd-dissect)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
, &child
);
1269 r
= dissected_image_mount(m
, t
, UID_INVALID
, DISSECT_IMAGE_READ_ONLY
|DISSECT_IMAGE_MOUNT_ROOT_ONLY
|DISSECT_IMAGE_VALIDATE_OS
);
1271 log_debug_errno(r
, "Failed to mount dissected image: %m");
1272 _exit(EXIT_FAILURE
);
1275 for (k
= 0; k
< _META_MAX
; k
++) {
1276 _cleanup_close_
int fd
= -1;
1279 fds
[2*k
] = safe_close(fds
[2*k
]);
1281 NULSTR_FOREACH(p
, paths
[k
]) {
1282 fd
= chase_symlinks_and_open(p
, t
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
, NULL
);
1287 log_debug_errno(fd
, "Failed to read %s file of image, ignoring: %m", paths
[k
]);
1291 r
= copy_bytes(fd
, fds
[2*k
+1], (uint64_t) -1, 0);
1293 _exit(EXIT_FAILURE
);
1295 fds
[2*k
+1] = safe_close(fds
[2*k
+1]);
1298 _exit(EXIT_SUCCESS
);
1301 for (k
= 0; k
< _META_MAX
; k
++) {
1302 _cleanup_fclose_
FILE *f
= NULL
;
1304 fds
[2*k
+1] = safe_close(fds
[2*k
+1]);
1306 f
= fdopen(fds
[2*k
], "re");
1317 r
= read_etc_hostname_stream(f
, &hostname
);
1319 log_debug_errno(r
, "Failed to read /etc/hostname: %m");
1323 case META_MACHINE_ID
: {
1324 _cleanup_free_
char *line
= NULL
;
1326 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1328 log_debug_errno(r
, "Failed to read /etc/machine-id: %m");
1330 r
= sd_id128_from_string(line
, &machine_id
);
1332 log_debug_errno(r
, "Image contains invalid /etc/machine-id: %s", line
);
1334 log_debug("/etc/machine-id file is empty.");
1336 log_debug("/etc/machine-id has unexpected length %i.", r
);
1341 case META_MACHINE_INFO
:
1342 r
= load_env_file_pairs(f
, "machine-info", NULL
, &machine_info
);
1344 log_debug_errno(r
, "Failed to read /etc/machine-info: %m");
1348 case META_OS_RELEASE
:
1349 r
= load_env_file_pairs(f
, "os-release", NULL
, &os_release
);
1351 log_debug_errno(r
, "Failed to read OS release file: %m");
1357 r
= wait_for_terminate_and_check("(sd-dissect)", child
, 0);
1361 if (r
!= EXIT_SUCCESS
)
1364 free_and_replace(m
->hostname
, hostname
);
1365 m
->machine_id
= machine_id
;
1366 strv_free_and_replace(m
->machine_info
, machine_info
);
1367 strv_free_and_replace(m
->os_release
, os_release
);
1370 for (k
= 0; k
< n_meta_initialized
; k
++)
1371 safe_close_pair(fds
+ 2*k
);
1376 int dissect_image_and_warn(
1379 const void *root_hash
,
1380 size_t root_hash_size
,
1381 DissectImageFlags flags
,
1382 DissectedImage
**ret
) {
1384 _cleanup_free_
char *buffer
= NULL
;
1388 r
= fd_get_path(fd
, &buffer
);
1395 r
= dissect_image(fd
, root_hash
, root_hash_size
, flags
, ret
);
1400 return log_error_errno(r
, "Dissecting images is not supported, compiled without blkid support.");
1403 return log_error_errno(r
, "Couldn't identify a suitable partition table or file system in '%s'.", name
);
1405 case -EADDRNOTAVAIL
:
1406 return log_error_errno(r
, "No root partition for specified root hash found in '%s'.", name
);
1409 return log_error_errno(r
, "Multiple suitable root partitions found in image '%s'.", name
);
1412 return log_error_errno(r
, "No suitable root partition found in image '%s'.", name
);
1414 case -EPROTONOSUPPORT
:
1415 return log_error_errno(r
, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name
);
1419 return log_error_errno(r
, "Failed to dissect image '%s': %m", name
);
1425 static const char *const partition_designator_table
[] = {
1426 [PARTITION_ROOT
] = "root",
1427 [PARTITION_ROOT_SECONDARY
] = "root-secondary",
1428 [PARTITION_HOME
] = "home",
1429 [PARTITION_SRV
] = "srv",
1430 [PARTITION_ESP
] = "esp",
1431 [PARTITION_SWAP
] = "swap",
1432 [PARTITION_ROOT_VERITY
] = "root-verity",
1433 [PARTITION_ROOT_SECONDARY_VERITY
] = "root-secondary-verity",
1436 DEFINE_STRING_TABLE_LOOKUP(partition_designator
, int);