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-nodes.h"
18 #include "device-util.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_ptr(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 /* Count the partitions enumerated by the kernel */
283 FOREACH_DEVICE(e
, q
) {
286 if (sd_device_get_devnum(q
, &qn
) < 0)
289 if (!device_is_block(q
))
292 if (device_is_mmc_special_partition(q
))
297 /* Count the partitions enumerated by blkid */
298 z
= blkid_partlist_numof_partitions(pl
);
302 log_debug("blkid and kernel partition list do not match.");
308 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
309 * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
310 * synchronous call that waits until probing is complete. */
316 if (ioctl(fd
, BLKRRPART
, 0) < 0) {
320 struct loop_info64 info
;
322 /* If we are running on a loop device that has partition scanning off,
323 * return an explicit recognizable error about this, so that callers
324 * can generate a proper message explaining the situation. */
326 if (ioctl(fd
, LOOP_GET_STATUS64
, &info
) >= 0 && (info
.lo_flags
& LO_FLAGS_PARTSCAN
) == 0) {
327 log_debug("Device is loop device and partition scanning is off!");
328 return -EPROTONOSUPPORT
;
336 /* If something else has the device open, such as an udev rule, the ioctl will return
337 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
338 * bit, and try again.
340 * This is really something they should fix in the kernel! */
342 (void) usleep(50 * USEC_PER_MSEC
);
346 e
= sd_device_enumerator_unref(e
);
349 FOREACH_DEVICE(e
, q
) {
350 unsigned long long pflags
;
356 r
= sd_device_get_devnum(q
, &qn
);
360 if (st
.st_rdev
== qn
)
363 if (!device_is_block(q
))
366 if (device_is_mmc_special_partition(q
))
369 r
= sd_device_get_devname(q
, &node
);
373 pp
= blkid_partlist_devno_to_partition(pl
, qn
);
377 pflags
= blkid_partition_get_flags(pp
);
379 nr
= blkid_partition_get_partno(pp
);
384 int designator
= _PARTITION_DESIGNATOR_INVALID
, architecture
= _ARCHITECTURE_INVALID
;
385 const char *stype
, *sid
, *fstype
= NULL
;
386 sd_id128_t type_id
, id
;
389 sid
= blkid_partition_get_uuid(pp
);
392 if (sd_id128_from_string(sid
, &id
) < 0)
395 stype
= blkid_partition_get_type_string(pp
);
398 if (sd_id128_from_string(stype
, &type_id
) < 0)
401 if (sd_id128_equal(type_id
, GPT_HOME
)) {
403 if (pflags
& GPT_FLAG_NO_AUTO
)
406 designator
= PARTITION_HOME
;
407 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
408 } else if (sd_id128_equal(type_id
, GPT_SRV
)) {
410 if (pflags
& GPT_FLAG_NO_AUTO
)
413 designator
= PARTITION_SRV
;
414 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
415 } else if (sd_id128_equal(type_id
, GPT_ESP
)) {
417 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
418 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
419 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
421 if (pflags
& GPT_FLAG_NO_BLOCK_IO_PROTOCOL
)
424 designator
= PARTITION_ESP
;
427 #ifdef GPT_ROOT_NATIVE
428 else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE
)) {
430 if (pflags
& GPT_FLAG_NO_AUTO
)
433 /* If a root ID is specified, ignore everything but the root id */
434 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
437 designator
= PARTITION_ROOT
;
438 architecture
= native_architecture();
439 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
440 } else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE_VERITY
)) {
442 if (pflags
& GPT_FLAG_NO_AUTO
)
445 m
->can_verity
= true;
447 /* Ignore verity unless a root hash is specified */
448 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
451 designator
= PARTITION_ROOT_VERITY
;
452 fstype
= "DM_verity_hash";
453 architecture
= native_architecture();
457 #ifdef GPT_ROOT_SECONDARY
458 else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY
)) {
460 if (pflags
& GPT_FLAG_NO_AUTO
)
463 /* If a root ID is specified, ignore everything but the root id */
464 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
467 designator
= PARTITION_ROOT_SECONDARY
;
468 architecture
= SECONDARY_ARCHITECTURE
;
469 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
470 } else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY_VERITY
)) {
472 if (pflags
& GPT_FLAG_NO_AUTO
)
475 m
->can_verity
= true;
477 /* Ignore verity unless root has is specified */
478 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
481 designator
= PARTITION_ROOT_SECONDARY_VERITY
;
482 fstype
= "DM_verity_hash";
483 architecture
= SECONDARY_ARCHITECTURE
;
487 else if (sd_id128_equal(type_id
, GPT_SWAP
)) {
489 if (pflags
& GPT_FLAG_NO_AUTO
)
492 designator
= PARTITION_SWAP
;
494 } else if (sd_id128_equal(type_id
, GPT_LINUX_GENERIC
)) {
496 if (pflags
& GPT_FLAG_NO_AUTO
)
500 multiple_generic
= true;
503 generic_rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
505 generic_node
= strdup(node
);
511 if (designator
!= _PARTITION_DESIGNATOR_INVALID
) {
512 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
515 if (m
->partitions
[designator
].found
)
528 m
->partitions
[designator
] = (DissectedPartition
) {
532 .architecture
= architecture
,
534 .fstype
= TAKE_PTR(t
),
541 if (pflags
!= 0x80) /* Bootable flag */
544 if (blkid_partition_get_type(pp
) != 0x83) /* Linux partition */
548 multiple_generic
= true;
552 generic_node
= strdup(node
);
559 if (!m
->partitions
[PARTITION_ROOT
].found
) {
560 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
561 * either, then check if there's a single generic one, and use that. */
563 if (m
->partitions
[PARTITION_ROOT_VERITY
].found
)
564 return -EADDRNOTAVAIL
;
566 if (m
->partitions
[PARTITION_ROOT_SECONDARY
].found
) {
567 m
->partitions
[PARTITION_ROOT
] = m
->partitions
[PARTITION_ROOT_SECONDARY
];
568 zero(m
->partitions
[PARTITION_ROOT_SECONDARY
]);
570 m
->partitions
[PARTITION_ROOT_VERITY
] = m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
];
571 zero(m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
]);
573 } else if (flags
& DISSECT_IMAGE_REQUIRE_ROOT
) {
575 /* If the root has was set, then we won't fallback to a generic node, because the root hash
578 return -EADDRNOTAVAIL
;
580 /* If we didn't find a generic node, then we can't fix this up either */
584 /* If we didn't find a properly marked root partition, but we did find a single suitable
585 * generic Linux partition, then use this as root partition, if the caller asked for it. */
586 if (multiple_generic
)
589 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
592 .partno
= generic_nr
,
593 .architecture
= _ARCHITECTURE_INVALID
,
594 .node
= TAKE_PTR(generic_node
),
595 .uuid
= generic_uuid
,
601 if (!m
->partitions
[PARTITION_ROOT_VERITY
].found
|| !m
->partitions
[PARTITION_ROOT
].found
)
602 return -EADDRNOTAVAIL
;
604 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
605 * (which would be weird, after all the root hash should only be assigned to one pair of
607 m
->partitions
[PARTITION_ROOT_SECONDARY
].found
= false;
608 m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
].found
= false;
610 /* If we found a verity setup, then the root partition is necessarily read-only. */
611 m
->partitions
[PARTITION_ROOT
].rw
= false;
619 /* Fill in file system types if we don't know them yet. */
620 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
621 DissectedPartition
*p
= m
->partitions
+ i
;
626 if (!p
->fstype
&& p
->node
) {
627 r
= probe_filesystem(p
->node
, &p
->fstype
);
628 if (r
< 0 && r
!= -EUCLEAN
)
632 if (streq_ptr(p
->fstype
, "crypto_LUKS"))
635 if (p
->fstype
&& fstype_is_ro(p
->fstype
))
647 DissectedImage
* dissected_image_unref(DissectedImage
*m
) {
653 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
654 free(m
->partitions
[i
].fstype
);
655 free(m
->partitions
[i
].node
);
656 free(m
->partitions
[i
].decrypted_fstype
);
657 free(m
->partitions
[i
].decrypted_node
);
661 strv_free(m
->machine_info
);
662 strv_free(m
->os_release
);
667 static int is_loop_device(const char *path
) {
668 char s
[SYS_BLOCK_PATH_MAX("/../loop/")];
673 if (stat(path
, &st
) < 0)
676 if (!S_ISBLK(st
.st_mode
))
679 xsprintf_sys_block_path(s
, "/loop/", st
.st_dev
);
680 if (access(s
, F_OK
) < 0) {
684 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
685 xsprintf_sys_block_path(s
, "/../loop/", st
.st_dev
);
686 if (access(s
, F_OK
) < 0)
687 return errno
== ENOENT
? false : -errno
;
693 static int mount_partition(
694 DissectedPartition
*m
,
696 const char *directory
,
698 DissectImageFlags flags
) {
700 _cleanup_free_
char *chased
= NULL
, *options
= NULL
;
701 const char *p
, *node
, *fstype
;
708 node
= m
->decrypted_node
?: m
->node
;
709 fstype
= m
->decrypted_fstype
?: m
->fstype
;
711 if (!m
->found
|| !node
|| !fstype
)
714 /* Stacked encryption? Yuck */
715 if (streq_ptr(fstype
, "crypto_LUKS"))
718 rw
= m
->rw
&& !(flags
& DISSECT_IMAGE_READ_ONLY
);
721 r
= chase_symlinks(directory
, where
, CHASE_PREFIX_ROOT
, &chased
);
729 /* If requested, turn on discard support. */
730 if (fstype_can_discard(fstype
) &&
731 ((flags
& DISSECT_IMAGE_DISCARD
) ||
732 ((flags
& DISSECT_IMAGE_DISCARD_ON_LOOP
) && is_loop_device(m
->node
)))) {
733 options
= strdup("discard");
738 if (uid_is_valid(uid_shift
) && uid_shift
!= 0 && fstype_can_uid_gid(fstype
)) {
739 _cleanup_free_
char *uid_option
= NULL
;
741 if (asprintf(&uid_option
, "uid=" UID_FMT
",gid=" GID_FMT
, uid_shift
, (gid_t
) uid_shift
) < 0)
744 if (!strextend_with_separator(&options
, ",", uid_option
, NULL
))
748 return mount_verbose(LOG_DEBUG
, node
, p
, fstype
, MS_NODEV
|(rw
? 0 : MS_RDONLY
), options
);
751 int dissected_image_mount(DissectedImage
*m
, const char *where
, uid_t uid_shift
, DissectImageFlags flags
) {
757 if (!m
->partitions
[PARTITION_ROOT
].found
)
760 if ((flags
& DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY
) == 0) {
761 r
= mount_partition(m
->partitions
+ PARTITION_ROOT
, where
, NULL
, uid_shift
, flags
);
765 if (flags
& DISSECT_IMAGE_VALIDATE_OS
) {
766 r
= path_is_os_tree(where
);
774 if ((flags
& DISSECT_IMAGE_MOUNT_ROOT_ONLY
))
777 r
= mount_partition(m
->partitions
+ PARTITION_HOME
, where
, "/home", uid_shift
, flags
);
781 r
= mount_partition(m
->partitions
+ PARTITION_SRV
, where
, "/srv", uid_shift
, flags
);
785 if (m
->partitions
[PARTITION_ESP
].found
) {
788 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
790 FOREACH_STRING(mp
, "/efi", "/boot") {
791 _cleanup_free_
char *p
= NULL
;
793 r
= chase_symlinks(mp
, where
, CHASE_PREFIX_ROOT
, &p
);
799 r
= mount_partition(m
->partitions
+ PARTITION_ESP
, where
, mp
, uid_shift
, flags
);
809 #if HAVE_LIBCRYPTSETUP
810 typedef struct DecryptedPartition
{
811 struct crypt_device
*device
;
814 } DecryptedPartition
;
816 struct DecryptedImage
{
817 DecryptedPartition
*decrypted
;
823 DecryptedImage
* decrypted_image_unref(DecryptedImage
* d
) {
824 #if HAVE_LIBCRYPTSETUP
831 for (i
= 0; i
< d
->n_decrypted
; i
++) {
832 DecryptedPartition
*p
= d
->decrypted
+ i
;
834 if (p
->device
&& p
->name
&& !p
->relinquished
) {
835 r
= crypt_deactivate(p
->device
, p
->name
);
837 log_debug_errno(r
, "Failed to deactivate encrypted partition %s", p
->name
);
841 crypt_free(p
->device
);
850 #if HAVE_LIBCRYPTSETUP
852 static int make_dm_name_and_node(const void *original_node
, const char *suffix
, char **ret_name
, char **ret_node
) {
853 _cleanup_free_
char *name
= NULL
, *node
= NULL
;
856 assert(original_node
);
861 base
= strrchr(original_node
, '/');
868 name
= strjoin(base
, suffix
);
871 if (!filename_is_valid(name
))
874 node
= strjoin(crypt_get_dir(), "/", name
);
878 *ret_name
= TAKE_PTR(name
);
879 *ret_node
= TAKE_PTR(node
);
884 static int decrypt_partition(
885 DissectedPartition
*m
,
886 const char *passphrase
,
887 DissectImageFlags flags
,
890 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
891 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
897 if (!m
->found
|| !m
->node
|| !m
->fstype
)
900 if (!streq(m
->fstype
, "crypto_LUKS"))
906 r
= make_dm_name_and_node(m
->node
, "-decrypted", &name
, &node
);
910 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
913 r
= crypt_init(&cd
, m
->node
);
915 return log_debug_errno(r
, "Failed to initialize dm-crypt: %m");
917 r
= crypt_load(cd
, CRYPT_LUKS
, NULL
);
919 return log_debug_errno(r
, "Failed to load LUKS metadata: %m");
921 r
= crypt_activate_by_passphrase(cd
, name
, CRYPT_ANY_SLOT
, passphrase
, strlen(passphrase
),
922 ((flags
& DISSECT_IMAGE_READ_ONLY
) ? CRYPT_ACTIVATE_READONLY
: 0) |
923 ((flags
& DISSECT_IMAGE_DISCARD_ON_CRYPTO
) ? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0));
925 log_debug_errno(r
, "Failed to activate LUKS device: %m");
926 return r
== -EPERM
? -EKEYREJECTED
: r
;
929 d
->decrypted
[d
->n_decrypted
].name
= TAKE_PTR(name
);
930 d
->decrypted
[d
->n_decrypted
].device
= TAKE_PTR(cd
);
933 m
->decrypted_node
= TAKE_PTR(node
);
938 static int verity_partition(
939 DissectedPartition
*m
,
940 DissectedPartition
*v
,
941 const void *root_hash
,
942 size_t root_hash_size
,
943 DissectImageFlags flags
,
946 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
947 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
956 if (!m
->found
|| !m
->node
|| !m
->fstype
)
958 if (!v
->found
|| !v
->node
|| !v
->fstype
)
961 if (!streq(v
->fstype
, "DM_verity_hash"))
964 r
= make_dm_name_and_node(m
->node
, "-verity", &name
, &node
);
968 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
971 r
= crypt_init(&cd
, v
->node
);
975 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
979 r
= crypt_set_data_device(cd
, m
->node
);
983 r
= crypt_activate_by_volume_key(cd
, name
, root_hash
, root_hash_size
, CRYPT_ACTIVATE_READONLY
);
987 d
->decrypted
[d
->n_decrypted
].name
= TAKE_PTR(name
);
988 d
->decrypted
[d
->n_decrypted
].device
= TAKE_PTR(cd
);
991 m
->decrypted_node
= TAKE_PTR(node
);
997 int dissected_image_decrypt(
999 const char *passphrase
,
1000 const void *root_hash
,
1001 size_t root_hash_size
,
1002 DissectImageFlags flags
,
1003 DecryptedImage
**ret
) {
1005 #if HAVE_LIBCRYPTSETUP
1006 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*d
= NULL
;
1012 assert(root_hash
|| root_hash_size
== 0);
1016 * = 0 → There was nothing to decrypt
1017 * > 0 → Decrypted successfully
1018 * -ENOKEY → There's something to decrypt but no key was supplied
1019 * -EKEYREJECTED → Passed key was not correct
1022 if (root_hash
&& root_hash_size
< sizeof(sd_id128_t
))
1025 if (!m
->encrypted
&& !m
->verity
) {
1030 #if HAVE_LIBCRYPTSETUP
1031 d
= new0(DecryptedImage
, 1);
1035 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
1036 DissectedPartition
*p
= m
->partitions
+ i
;
1042 r
= decrypt_partition(p
, passphrase
, flags
, d
);
1046 k
= PARTITION_VERITY_OF(i
);
1048 r
= verity_partition(p
, m
->partitions
+ k
, root_hash
, root_hash_size
, flags
, d
);
1053 if (!p
->decrypted_fstype
&& p
->decrypted_node
) {
1054 r
= probe_filesystem(p
->decrypted_node
, &p
->decrypted_fstype
);
1055 if (r
< 0 && r
!= -EUCLEAN
)
1068 int dissected_image_decrypt_interactively(
1070 const char *passphrase
,
1071 const void *root_hash
,
1072 size_t root_hash_size
,
1073 DissectImageFlags flags
,
1074 DecryptedImage
**ret
) {
1076 _cleanup_strv_free_erase_
char **z
= NULL
;
1083 r
= dissected_image_decrypt(m
, passphrase
, root_hash
, root_hash_size
, flags
, ret
);
1086 if (r
== -EKEYREJECTED
)
1087 log_error_errno(r
, "Incorrect passphrase, try again!");
1088 else if (r
!= -ENOKEY
)
1089 return log_error_errno(r
, "Failed to decrypt image: %m");
1092 log_error("Too many retries.");
1093 return -EKEYREJECTED
;
1098 r
= ask_password_auto("Please enter image passphrase:", NULL
, "dissect", "dissect", USEC_INFINITY
, 0, &z
);
1100 return log_error_errno(r
, "Failed to query for passphrase: %m");
1106 #if HAVE_LIBCRYPTSETUP
1107 static int deferred_remove(DecryptedPartition
*p
) {
1109 struct dm_ioctl dm
= {
1113 DM_VERSION_PATCHLEVEL
1115 .data_size
= sizeof(dm
),
1116 .flags
= DM_DEFERRED_REMOVE
,
1119 _cleanup_close_
int fd
= -1;
1123 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1125 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
1129 strncpy(dm
.name
, p
->name
, sizeof(dm
.name
));
1131 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
))
1138 int decrypted_image_relinquish(DecryptedImage
*d
) {
1140 #if HAVE_LIBCRYPTSETUP
1147 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1148 * that we don't clean it up ourselves either anymore */
1150 #if HAVE_LIBCRYPTSETUP
1151 for (i
= 0; i
< d
->n_decrypted
; i
++) {
1152 DecryptedPartition
*p
= d
->decrypted
+ i
;
1154 if (p
->relinquished
)
1157 r
= deferred_remove(p
);
1159 return log_debug_errno(r
, "Failed to mark %s for auto-removal: %m", p
->name
);
1161 p
->relinquished
= true;
1168 int root_hash_load(const char *image
, void **ret
, size_t *ret_size
) {
1169 _cleanup_free_
char *text
= NULL
;
1170 _cleanup_free_
void *k
= NULL
;
1178 if (is_device_path(image
)) {
1179 /* If we are asked to load the root hash for a device node, exit early */
1185 r
= getxattr_malloc(image
, "user.verity.roothash", &text
, true);
1189 if (!IN_SET(r
, -ENODATA
, -EOPNOTSUPP
, -ENOENT
))
1192 fn
= newa(char, strlen(image
) + STRLEN(".roothash") + 1);
1193 n
= stpcpy(fn
, image
);
1194 e
= endswith(fn
, ".raw");
1198 strcpy(n
, ".roothash");
1200 r
= read_one_line_file(fn
, &text
);
1210 r
= unhexmem(text
, strlen(text
), &k
, &l
);
1213 if (l
< sizeof(sd_id128_t
))
1222 int dissected_image_acquire_metadata(DissectedImage
*m
) {
1232 static const char *const paths
[_META_MAX
] = {
1233 [META_HOSTNAME
] = "/etc/hostname\0",
1234 [META_MACHINE_ID
] = "/etc/machine-id\0",
1235 [META_MACHINE_INFO
] = "/etc/machine-info\0",
1236 [META_OS_RELEASE
] = "/etc/os-release\0/usr/lib/os-release\0",
1239 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1240 _cleanup_(rmdir_and_freep
) char *t
= NULL
;
1241 _cleanup_(sigkill_waitp
) pid_t child
= 0;
1242 sd_id128_t machine_id
= SD_ID128_NULL
;
1243 _cleanup_free_
char *hostname
= NULL
;
1244 unsigned n_meta_initialized
= 0, k
;
1245 int fds
[2 * _META_MAX
], r
;
1247 BLOCK_SIGNALS(SIGCHLD
);
1251 for (; n_meta_initialized
< _META_MAX
; n_meta_initialized
++)
1252 if (pipe2(fds
+ 2*n_meta_initialized
, O_CLOEXEC
) < 0) {
1257 r
= mkdtemp_malloc("/tmp/dissect-XXXXXX", &t
);
1261 r
= safe_fork("(sd-dissect)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
, &child
);
1265 r
= dissected_image_mount(m
, t
, UID_INVALID
, DISSECT_IMAGE_READ_ONLY
|DISSECT_IMAGE_MOUNT_ROOT_ONLY
|DISSECT_IMAGE_VALIDATE_OS
);
1267 log_debug_errno(r
, "Failed to mount dissected image: %m");
1268 _exit(EXIT_FAILURE
);
1271 for (k
= 0; k
< _META_MAX
; k
++) {
1272 _cleanup_close_
int fd
= -1;
1275 fds
[2*k
] = safe_close(fds
[2*k
]);
1277 NULSTR_FOREACH(p
, paths
[k
]) {
1278 fd
= chase_symlinks_and_open(p
, t
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
, NULL
);
1283 log_debug_errno(fd
, "Failed to read %s file of image, ignoring: %m", paths
[k
]);
1287 r
= copy_bytes(fd
, fds
[2*k
+1], (uint64_t) -1, 0);
1289 _exit(EXIT_FAILURE
);
1291 fds
[2*k
+1] = safe_close(fds
[2*k
+1]);
1294 _exit(EXIT_SUCCESS
);
1297 for (k
= 0; k
< _META_MAX
; k
++) {
1298 _cleanup_fclose_
FILE *f
= NULL
;
1300 fds
[2*k
+1] = safe_close(fds
[2*k
+1]);
1302 f
= fdopen(fds
[2*k
], "re");
1313 r
= read_etc_hostname_stream(f
, &hostname
);
1315 log_debug_errno(r
, "Failed to read /etc/hostname: %m");
1319 case META_MACHINE_ID
: {
1320 _cleanup_free_
char *line
= NULL
;
1322 r
= read_line(f
, LONG_LINE_MAX
, &line
);
1324 log_debug_errno(r
, "Failed to read /etc/machine-id: %m");
1326 r
= sd_id128_from_string(line
, &machine_id
);
1328 log_debug_errno(r
, "Image contains invalid /etc/machine-id: %s", line
);
1330 log_debug("/etc/machine-id file is empty.");
1332 log_debug("/etc/machine-id has unexpected length %i.", r
);
1337 case META_MACHINE_INFO
:
1338 r
= load_env_file_pairs(f
, "machine-info", NULL
, &machine_info
);
1340 log_debug_errno(r
, "Failed to read /etc/machine-info: %m");
1344 case META_OS_RELEASE
:
1345 r
= load_env_file_pairs(f
, "os-release", NULL
, &os_release
);
1347 log_debug_errno(r
, "Failed to read OS release file: %m");
1353 r
= wait_for_terminate_and_check("(sd-dissect)", child
, 0);
1357 if (r
!= EXIT_SUCCESS
)
1360 free_and_replace(m
->hostname
, hostname
);
1361 m
->machine_id
= machine_id
;
1362 strv_free_and_replace(m
->machine_info
, machine_info
);
1363 strv_free_and_replace(m
->os_release
, os_release
);
1366 for (k
= 0; k
< n_meta_initialized
; k
++)
1367 safe_close_pair(fds
+ 2*k
);
1372 int dissect_image_and_warn(
1375 const void *root_hash
,
1376 size_t root_hash_size
,
1377 DissectImageFlags flags
,
1378 DissectedImage
**ret
) {
1380 _cleanup_free_
char *buffer
= NULL
;
1384 r
= fd_get_path(fd
, &buffer
);
1391 r
= dissect_image(fd
, root_hash
, root_hash_size
, flags
, ret
);
1396 return log_error_errno(r
, "Dissecting images is not supported, compiled without blkid support.");
1399 return log_error_errno(r
, "Couldn't identify a suitable partition table or file system in '%s'.", name
);
1401 case -EADDRNOTAVAIL
:
1402 return log_error_errno(r
, "No root partition for specified root hash found in '%s'.", name
);
1405 return log_error_errno(r
, "Multiple suitable root partitions found in image '%s'.", name
);
1408 return log_error_errno(r
, "No suitable root partition found in image '%s'.", name
);
1410 case -EPROTONOSUPPORT
:
1411 return log_error_errno(r
, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name
);
1415 return log_error_errno(r
, "Failed to dissect image '%s': %m", name
);
1421 static const char *const partition_designator_table
[] = {
1422 [PARTITION_ROOT
] = "root",
1423 [PARTITION_ROOT_SECONDARY
] = "root-secondary",
1424 [PARTITION_HOME
] = "home",
1425 [PARTITION_SRV
] = "srv",
1426 [PARTITION_ESP
] = "esp",
1427 [PARTITION_SWAP
] = "swap",
1428 [PARTITION_ROOT_VERITY
] = "root-verity",
1429 [PARTITION_ROOT_SECONDARY_VERITY
] = "root-secondary-verity",
1432 DEFINE_STRING_TABLE_LOOKUP(partition_designator
, int);