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 #include <sys/mount.h>
25 #include "architecture.h"
26 #include "ask-password-api.h"
27 #include "blkid-util.h"
28 #include "dissect-image.h"
33 #include "hexdecoct.h"
34 #include "linux-3.13/dm-ioctl.h"
35 #include "mount-util.h"
36 #include "path-util.h"
37 #include "stat-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
42 #include "udev-util.h"
43 #include "xattr-util.h"
45 _unused_
static int probe_filesystem(const char *node
, char **ret_fstype
) {
47 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
51 b
= blkid_new_probe_from_filename(node
);
55 blkid_probe_enable_superblocks(b
, 1);
56 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
);
59 r
= blkid_do_safeprobe(b
);
60 if (IN_SET(r
, -2, 1)) {
61 log_debug("Failed to identify any partition type on partition %s", node
);
65 return -errno
?: -EIO
;
67 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
88 int dissect_image(int fd
, const void *root_hash
, size_t root_hash_size
, DissectImageFlags flags
, DissectedImage
**ret
) {
91 sd_id128_t root_uuid
= SD_ID128_NULL
, verity_uuid
= SD_ID128_NULL
;
92 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
93 bool is_gpt
, is_mbr
, generic_rw
, multiple_generic
= false;
94 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
95 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
96 _cleanup_blkid_free_probe_ blkid_probe b
= NULL
;
97 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
98 _cleanup_free_
char *generic_node
= NULL
;
99 sd_id128_t generic_uuid
= SD_ID128_NULL
;
100 const char *pttype
= NULL
;
101 struct udev_list_entry
*first
, *item
;
109 assert(root_hash
|| root_hash_size
== 0);
111 /* Probes a disk image, and returns information about what it found in *ret.
113 * Returns -ENOPKG if no suitable partition table or file system could be found.
114 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
117 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
118 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
121 if (root_hash_size
< sizeof(sd_id128_t
))
124 memcpy(&root_uuid
, root_hash
, sizeof(sd_id128_t
));
125 memcpy(&verity_uuid
, (const uint8_t*) root_hash
+ root_hash_size
- sizeof(sd_id128_t
), sizeof(sd_id128_t
));
127 if (sd_id128_is_null(root_uuid
))
129 if (sd_id128_is_null(verity_uuid
))
133 if (fstat(fd
, &st
) < 0)
136 if (!S_ISBLK(st
.st_mode
))
139 b
= blkid_new_probe();
144 r
= blkid_probe_set_device(b
, fd
, 0, 0);
146 return -errno
?: -ENOMEM
;
148 if ((flags
& DISSECT_IMAGE_GPT_ONLY
) == 0) {
149 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
150 blkid_probe_enable_superblocks(b
, 1);
151 blkid_probe_set_superblocks_flags(b
, BLKID_SUBLKS_TYPE
|BLKID_SUBLKS_USAGE
);
154 blkid_probe_enable_partitions(b
, 1);
155 blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
158 r
= blkid_do_safeprobe(b
);
159 if (IN_SET(r
, -2, 1)) {
160 log_debug("Failed to identify any partition table.");
164 return -errno
?: -EIO
;
166 m
= new0(DissectedImage
, 1);
170 if (!(flags
& DISSECT_IMAGE_GPT_ONLY
) &&
171 (flags
& DISSECT_IMAGE_REQUIRE_ROOT
)) {
172 const char *usage
= NULL
;
174 (void) blkid_probe_lookup_value(b
, "USAGE", &usage
, NULL
);
175 if (STRPTR_IN_SET(usage
, "filesystem", "crypto")) {
176 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
177 const char *fstype
= NULL
;
179 /* OK, we have found a file system, that's our root partition then. */
180 (void) blkid_probe_lookup_value(b
, "TYPE", &fstype
, NULL
);
188 if (asprintf(&n
, "/dev/block/%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
)) < 0)
191 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
195 .architecture
= _ARCHITECTURE_INVALID
,
202 m
->encrypted
= streq(fstype
, "crypto_LUKS");
211 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
215 is_gpt
= streq_ptr(pttype
, "gpt");
216 is_mbr
= streq_ptr(pttype
, "dos");
218 if (!is_gpt
&& ((flags
& DISSECT_IMAGE_GPT_ONLY
) || !is_mbr
))
222 pl
= blkid_probe_get_partitions(b
);
224 return -errno
?: -ENOMEM
;
230 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
238 log_debug("Kernel partitions never appeared.");
242 e
= udev_enumerate_new(udev
);
246 r
= udev_enumerate_add_match_parent(e
, d
);
250 r
= udev_enumerate_scan_devices(e
);
254 /* Count the partitions enumerated by the kernel */
256 first
= udev_enumerate_get_list_entry(e
);
257 udev_list_entry_foreach(item
, first
)
260 /* Count the partitions enumerated by blkid */
261 z
= blkid_partlist_numof_partitions(pl
);
265 log_debug("blkid and kernel partition list do not match.");
271 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
272 * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
273 * synchronous call that waits until probing is complete. */
275 for (j
= 0; j
< 20; j
++) {
277 r
= ioctl(fd
, BLKRRPART
, 0);
280 if (r
>= 0 || r
!= -EBUSY
)
283 /* If something else has the device open, such as an udev rule, the ioctl will return
284 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
285 * bit, and try again.
287 * This is really something they should fix in the kernel! */
289 usleep(50 * USEC_PER_MSEC
);
296 e
= udev_enumerate_unref(e
);
299 first
= udev_enumerate_get_list_entry(e
);
300 udev_list_entry_foreach(item
, first
) {
301 _cleanup_udev_device_unref_
struct udev_device
*q
;
302 unsigned long long pflags
;
304 const char *node
, *sysname
;
308 q
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
312 qn
= udev_device_get_devnum(q
);
316 if (st
.st_rdev
== qn
)
319 /* Filter out weird MMC RPMB partitions, which cannot reasonably be read, see
320 * https://github.com/systemd/systemd/issues/5806 */
321 sysname
= udev_device_get_sysname(q
);
322 if (sysname
&& startswith(sysname
, "mmcblk") && endswith(sysname
, "rpmb"))
325 node
= udev_device_get_devnode(q
);
329 pp
= blkid_partlist_devno_to_partition(pl
, qn
);
333 pflags
= blkid_partition_get_flags(pp
);
335 nr
= blkid_partition_get_partno(pp
);
340 int designator
= _PARTITION_DESIGNATOR_INVALID
, architecture
= _ARCHITECTURE_INVALID
;
341 const char *stype
, *sid
, *fstype
= NULL
;
342 sd_id128_t type_id
, id
;
345 sid
= blkid_partition_get_uuid(pp
);
348 if (sd_id128_from_string(sid
, &id
) < 0)
351 stype
= blkid_partition_get_type_string(pp
);
354 if (sd_id128_from_string(stype
, &type_id
) < 0)
357 if (sd_id128_equal(type_id
, GPT_HOME
)) {
359 if (pflags
& GPT_FLAG_NO_AUTO
)
362 designator
= PARTITION_HOME
;
363 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
364 } else if (sd_id128_equal(type_id
, GPT_SRV
)) {
366 if (pflags
& GPT_FLAG_NO_AUTO
)
369 designator
= PARTITION_SRV
;
370 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
371 } else if (sd_id128_equal(type_id
, GPT_ESP
)) {
373 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
374 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
375 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
377 if (pflags
& GPT_FLAG_NO_BLOCK_IO_PROTOCOL
)
380 designator
= PARTITION_ESP
;
383 #ifdef GPT_ROOT_NATIVE
384 else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE
)) {
386 if (pflags
& GPT_FLAG_NO_AUTO
)
389 /* If a root ID is specified, ignore everything but the root id */
390 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
393 designator
= PARTITION_ROOT
;
394 architecture
= native_architecture();
395 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
396 } else if (sd_id128_equal(type_id
, GPT_ROOT_NATIVE_VERITY
)) {
398 if (pflags
& GPT_FLAG_NO_AUTO
)
401 m
->can_verity
= true;
403 /* Ignore verity unless a root hash is specified */
404 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
407 designator
= PARTITION_ROOT_VERITY
;
408 fstype
= "DM_verity_hash";
409 architecture
= native_architecture();
413 #ifdef GPT_ROOT_SECONDARY
414 else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY
)) {
416 if (pflags
& GPT_FLAG_NO_AUTO
)
419 /* If a root ID is specified, ignore everything but the root id */
420 if (!sd_id128_is_null(root_uuid
) && !sd_id128_equal(root_uuid
, id
))
423 designator
= PARTITION_ROOT_SECONDARY
;
424 architecture
= SECONDARY_ARCHITECTURE
;
425 rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
426 } else if (sd_id128_equal(type_id
, GPT_ROOT_SECONDARY_VERITY
)) {
428 if (pflags
& GPT_FLAG_NO_AUTO
)
431 m
->can_verity
= true;
433 /* Ignore verity unless root has is specified */
434 if (sd_id128_is_null(verity_uuid
) || !sd_id128_equal(verity_uuid
, id
))
437 designator
= PARTITION_ROOT_SECONDARY_VERITY
;
438 fstype
= "DM_verity_hash";
439 architecture
= SECONDARY_ARCHITECTURE
;
443 else if (sd_id128_equal(type_id
, GPT_SWAP
)) {
445 if (pflags
& GPT_FLAG_NO_AUTO
)
448 designator
= PARTITION_SWAP
;
450 } else if (sd_id128_equal(type_id
, GPT_LINUX_GENERIC
)) {
452 if (pflags
& GPT_FLAG_NO_AUTO
)
456 multiple_generic
= true;
459 generic_rw
= !(pflags
& GPT_FLAG_READ_ONLY
);
461 generic_node
= strdup(node
);
467 if (designator
!= _PARTITION_DESIGNATOR_INVALID
) {
468 _cleanup_free_
char *t
= NULL
, *n
= NULL
;
471 if (m
->partitions
[designator
].found
)
484 m
->partitions
[designator
] = (DissectedPartition
) {
488 .architecture
= architecture
,
499 if (pflags
!= 0x80) /* Bootable flag */
502 if (blkid_partition_get_type(pp
) != 0x83) /* Linux partition */
506 multiple_generic
= true;
510 generic_node
= strdup(node
);
517 if (!m
->partitions
[PARTITION_ROOT
].found
) {
518 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
519 * either, then check if there's a single generic one, and use that. */
521 if (m
->partitions
[PARTITION_ROOT_VERITY
].found
)
522 return -EADDRNOTAVAIL
;
524 if (m
->partitions
[PARTITION_ROOT_SECONDARY
].found
) {
525 m
->partitions
[PARTITION_ROOT
] = m
->partitions
[PARTITION_ROOT_SECONDARY
];
526 zero(m
->partitions
[PARTITION_ROOT_SECONDARY
]);
528 m
->partitions
[PARTITION_ROOT_VERITY
] = m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
];
529 zero(m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
]);
531 } else if (flags
& DISSECT_IMAGE_REQUIRE_ROOT
) {
533 /* If the root has was set, then we won't fallback to a generic node, because the root hash
536 return -EADDRNOTAVAIL
;
538 /* If we didn't find a generic node, then we can't fix this up either */
542 /* If we didn't find a properly marked root partition, but we did find a single suitable
543 * generic Linux partition, then use this as root partition, if the caller asked for it. */
544 if (multiple_generic
)
547 m
->partitions
[PARTITION_ROOT
] = (DissectedPartition
) {
550 .partno
= generic_nr
,
551 .architecture
= _ARCHITECTURE_INVALID
,
552 .node
= generic_node
,
553 .uuid
= generic_uuid
,
561 if (!m
->partitions
[PARTITION_ROOT_VERITY
].found
|| !m
->partitions
[PARTITION_ROOT
].found
)
562 return -EADDRNOTAVAIL
;
564 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
565 * (which would be weird, after all the root hash should only be assigned to one pair of
567 m
->partitions
[PARTITION_ROOT_SECONDARY
].found
= false;
568 m
->partitions
[PARTITION_ROOT_SECONDARY_VERITY
].found
= false;
570 /* If we found a verity setup, then the root partition is necessarily read-only. */
571 m
->partitions
[PARTITION_ROOT
].rw
= false;
579 /* Fill in file system types if we don't know them yet. */
580 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
581 DissectedPartition
*p
= m
->partitions
+ i
;
586 if (!p
->fstype
&& p
->node
) {
587 r
= probe_filesystem(p
->node
, &p
->fstype
);
592 if (streq_ptr(p
->fstype
, "crypto_LUKS"))
595 if (p
->fstype
&& fstype_is_ro(p
->fstype
))
608 DissectedImage
* dissected_image_unref(DissectedImage
*m
) {
614 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
615 free(m
->partitions
[i
].fstype
);
616 free(m
->partitions
[i
].node
);
617 free(m
->partitions
[i
].decrypted_fstype
);
618 free(m
->partitions
[i
].decrypted_node
);
625 static int is_loop_device(const char *path
) {
626 char s
[strlen("/sys/dev/block/") + DECIMAL_STR_MAX(dev_t
) + 1 + DECIMAL_STR_MAX(dev_t
) + strlen("/../loop/")];
631 if (stat(path
, &st
) < 0)
634 if (!S_ISBLK(st
.st_mode
))
637 xsprintf(s
, "/sys/dev/block/%u:%u/loop/", major(st
.st_rdev
), minor(st
.st_rdev
));
638 if (access(s
, F_OK
) < 0) {
642 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
643 xsprintf(s
, "/sys/dev/block/%u:%u/../loop/", major(st
.st_rdev
), minor(st
.st_rdev
));
644 if (access(s
, F_OK
) < 0)
645 return errno
== ENOENT
? false : -errno
;
651 static int mount_partition(
652 DissectedPartition
*m
,
654 const char *directory
,
655 DissectImageFlags flags
) {
657 const char *p
, *options
= NULL
, *node
, *fstype
;
658 _cleanup_free_
char *chased
= NULL
;
665 node
= m
->decrypted_node
?: m
->node
;
666 fstype
= m
->decrypted_fstype
?: m
->fstype
;
668 if (!m
->found
|| !node
|| !fstype
)
671 /* Stacked encryption? Yuck */
672 if (streq_ptr(fstype
, "crypto_LUKS"))
675 rw
= m
->rw
&& !(flags
& DISSECT_IMAGE_READ_ONLY
);
678 r
= chase_symlinks(directory
, where
, CHASE_PREFIX_ROOT
, &chased
);
686 /* If requested, turn on discard support. */
687 if (fstype_can_discard(fstype
) &&
688 ((flags
& DISSECT_IMAGE_DISCARD
) ||
689 ((flags
& DISSECT_IMAGE_DISCARD_ON_LOOP
) && is_loop_device(m
->node
))))
692 return mount_verbose(LOG_DEBUG
, node
, p
, fstype
, MS_NODEV
|(rw
? 0 : MS_RDONLY
), options
);
695 int dissected_image_mount(DissectedImage
*m
, const char *where
, DissectImageFlags flags
) {
701 if (!m
->partitions
[PARTITION_ROOT
].found
)
704 r
= mount_partition(m
->partitions
+ PARTITION_ROOT
, where
, NULL
, flags
);
708 r
= mount_partition(m
->partitions
+ PARTITION_HOME
, where
, "/home", flags
);
712 r
= mount_partition(m
->partitions
+ PARTITION_SRV
, where
, "/srv", flags
);
716 if (m
->partitions
[PARTITION_ESP
].found
) {
719 /* Mount the ESP to /efi if it exists and is empty. If it doesn't exist, use /boot instead. */
721 FOREACH_STRING(mp
, "/efi", "/boot") {
722 _cleanup_free_
char *p
= NULL
;
724 r
= chase_symlinks(mp
, where
, CHASE_PREFIX_ROOT
, &p
);
730 r
= mount_partition(m
->partitions
+ PARTITION_ESP
, where
, mp
, flags
);
740 #if HAVE_LIBCRYPTSETUP
741 typedef struct DecryptedPartition
{
742 struct crypt_device
*device
;
745 } DecryptedPartition
;
747 struct DecryptedImage
{
748 DecryptedPartition
*decrypted
;
754 DecryptedImage
* decrypted_image_unref(DecryptedImage
* d
) {
755 #if HAVE_LIBCRYPTSETUP
762 for (i
= 0; i
< d
->n_decrypted
; i
++) {
763 DecryptedPartition
*p
= d
->decrypted
+ i
;
765 if (p
->device
&& p
->name
&& !p
->relinquished
) {
766 r
= crypt_deactivate(p
->device
, p
->name
);
768 log_debug_errno(r
, "Failed to deactivate encrypted partition %s", p
->name
);
772 crypt_free(p
->device
);
781 #if HAVE_LIBCRYPTSETUP
783 static int make_dm_name_and_node(const void *original_node
, const char *suffix
, char **ret_name
, char **ret_node
) {
784 _cleanup_free_
char *name
= NULL
, *node
= NULL
;
787 assert(original_node
);
792 base
= strrchr(original_node
, '/');
799 name
= strjoin(base
, suffix
);
802 if (!filename_is_valid(name
))
805 node
= strjoin(crypt_get_dir(), "/", name
);
816 static int decrypt_partition(
817 DissectedPartition
*m
,
818 const char *passphrase
,
819 DissectImageFlags flags
,
822 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
823 struct crypt_device
*cd
;
829 if (!m
->found
|| !m
->node
|| !m
->fstype
)
832 if (!streq(m
->fstype
, "crypto_LUKS"))
835 r
= make_dm_name_and_node(m
->node
, "-decrypted", &name
, &node
);
839 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
842 r
= crypt_init(&cd
, m
->node
);
844 return log_debug_errno(r
, "Failed to initialize dm-crypt: %m");
846 r
= crypt_load(cd
, CRYPT_LUKS1
, NULL
);
848 log_debug_errno(r
, "Failed to load LUKS metadata: %m");
852 r
= crypt_activate_by_passphrase(cd
, name
, CRYPT_ANY_SLOT
, passphrase
, strlen(passphrase
),
853 ((flags
& DISSECT_IMAGE_READ_ONLY
) ? CRYPT_ACTIVATE_READONLY
: 0) |
854 ((flags
& DISSECT_IMAGE_DISCARD_ON_CRYPTO
) ? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0));
856 log_debug_errno(r
, "Failed to activate LUKS device: %m");
864 d
->decrypted
[d
->n_decrypted
].name
= name
;
867 d
->decrypted
[d
->n_decrypted
].device
= cd
;
870 m
->decrypted_node
= node
;
880 static int verity_partition(
881 DissectedPartition
*m
,
882 DissectedPartition
*v
,
883 const void *root_hash
,
884 size_t root_hash_size
,
885 DissectImageFlags flags
,
888 _cleanup_free_
char *node
= NULL
, *name
= NULL
;
889 struct crypt_device
*cd
;
898 if (!m
->found
|| !m
->node
|| !m
->fstype
)
900 if (!v
->found
|| !v
->node
|| !v
->fstype
)
903 if (!streq(v
->fstype
, "DM_verity_hash"))
906 r
= make_dm_name_and_node(m
->node
, "-verity", &name
, &node
);
910 if (!GREEDY_REALLOC0(d
->decrypted
, d
->n_allocated
, d
->n_decrypted
+ 1))
913 r
= crypt_init(&cd
, v
->node
);
917 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
921 r
= crypt_set_data_device(cd
, m
->node
);
925 r
= crypt_activate_by_volume_key(cd
, name
, root_hash
, root_hash_size
, CRYPT_ACTIVATE_READONLY
);
929 d
->decrypted
[d
->n_decrypted
].name
= name
;
932 d
->decrypted
[d
->n_decrypted
].device
= cd
;
935 m
->decrypted_node
= node
;
946 int dissected_image_decrypt(
948 const char *passphrase
,
949 const void *root_hash
,
950 size_t root_hash_size
,
951 DissectImageFlags flags
,
952 DecryptedImage
**ret
) {
954 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*d
= NULL
;
955 #if HAVE_LIBCRYPTSETUP
961 assert(root_hash
|| root_hash_size
== 0);
965 * = 0 → There was nothing to decrypt
966 * > 0 → Decrypted successfully
967 * -ENOKEY → There's something to decrypt but no key was supplied
968 * -EKEYREJECTED → Passed key was not correct
971 if (root_hash
&& root_hash_size
< sizeof(sd_id128_t
))
974 if (!m
->encrypted
&& !m
->verity
) {
979 #if HAVE_LIBCRYPTSETUP
980 if (m
->encrypted
&& !passphrase
)
983 d
= new0(DecryptedImage
, 1);
987 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
988 DissectedPartition
*p
= m
->partitions
+ i
;
994 r
= decrypt_partition(p
, passphrase
, flags
, d
);
998 k
= PARTITION_VERITY_OF(i
);
1000 r
= verity_partition(p
, m
->partitions
+ k
, root_hash
, root_hash_size
, flags
, d
);
1005 if (!p
->decrypted_fstype
&& p
->decrypted_node
) {
1006 r
= probe_filesystem(p
->decrypted_node
, &p
->decrypted_fstype
);
1021 int dissected_image_decrypt_interactively(
1023 const char *passphrase
,
1024 const void *root_hash
,
1025 size_t root_hash_size
,
1026 DissectImageFlags flags
,
1027 DecryptedImage
**ret
) {
1029 _cleanup_strv_free_erase_
char **z
= NULL
;
1036 r
= dissected_image_decrypt(m
, passphrase
, root_hash
, root_hash_size
, flags
, ret
);
1039 if (r
== -EKEYREJECTED
)
1040 log_error_errno(r
, "Incorrect passphrase, try again!");
1041 else if (r
!= -ENOKEY
) {
1042 log_error_errno(r
, "Failed to decrypt image: %m");
1047 log_error("Too many retries.");
1048 return -EKEYREJECTED
;
1053 r
= ask_password_auto("Please enter image passphrase!", NULL
, "dissect", "dissect", USEC_INFINITY
, 0, &z
);
1055 return log_error_errno(r
, "Failed to query for passphrase: %m");
1061 #if HAVE_LIBCRYPTSETUP
1062 static int deferred_remove(DecryptedPartition
*p
) {
1064 struct dm_ioctl dm
= {
1068 DM_VERSION_PATCHLEVEL
1070 .data_size
= sizeof(dm
),
1071 .flags
= DM_DEFERRED_REMOVE
,
1074 _cleanup_close_
int fd
= -1;
1078 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1080 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
1084 strncpy(dm
.name
, p
->name
, sizeof(dm
.name
));
1086 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
))
1093 int decrypted_image_relinquish(DecryptedImage
*d
) {
1095 #if HAVE_LIBCRYPTSETUP
1102 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1103 * that we don't clean it up ourselves either anymore */
1105 #if HAVE_LIBCRYPTSETUP
1106 for (i
= 0; i
< d
->n_decrypted
; i
++) {
1107 DecryptedPartition
*p
= d
->decrypted
+ i
;
1109 if (p
->relinquished
)
1112 r
= deferred_remove(p
);
1114 return log_debug_errno(r
, "Failed to mark %s for auto-removal: %m", p
->name
);
1116 p
->relinquished
= true;
1123 int root_hash_load(const char *image
, void **ret
, size_t *ret_size
) {
1124 _cleanup_free_
char *text
= NULL
;
1125 _cleanup_free_
void *k
= NULL
;
1133 if (is_device_path(image
)) {
1134 /* If we are asked to load the root hash for a device node, exit early */
1140 r
= getxattr_malloc(image
, "user.verity.roothash", &text
, true);
1144 if (!IN_SET(r
, -ENODATA
, -EOPNOTSUPP
, -ENOENT
))
1147 fn
= newa(char, strlen(image
) + strlen(".roothash") + 1);
1148 n
= stpcpy(fn
, image
);
1149 e
= endswith(fn
, ".raw");
1153 strcpy(n
, ".roothash");
1155 r
= read_one_line_file(fn
, &text
);
1165 r
= unhexmem(text
, strlen(text
), &k
, &l
);
1168 if (l
< sizeof(sd_id128_t
))
1179 static const char *const partition_designator_table
[] = {
1180 [PARTITION_ROOT
] = "root",
1181 [PARTITION_ROOT_SECONDARY
] = "root-secondary",
1182 [PARTITION_HOME
] = "home",
1183 [PARTITION_SRV
] = "srv",
1184 [PARTITION_ESP
] = "esp",
1185 [PARTITION_SWAP
] = "swap",
1186 [PARTITION_ROOT_VERITY
] = "root-verity",
1187 [PARTITION_ROOT_SECONDARY_VERITY
] = "root-secondary-verity",
1190 DEFINE_STRING_TABLE_LOOKUP(partition_designator
, int);