HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/dissect-image.c

242,096 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 9280a90974918216809a09219e8a3162bddcf789

Lines 1001–2000 of 5,731

                const VeritySettings *verity,
                const MountOptions *mount_options,
                const ImagePolicy *policy,
                const ImageFilter *filter,
                int *mount_node_fd, /* taken over on success */
                char **fstype, /* taken over on success */
                DissectedImage *m,
                DissectImageFlags flags,
                PartitionPolicyFlags found_flags) {
 
        _cleanup_free_ char *n = NULL, *o = NULL;
        const char *options = NULL;
        int r;
 
        assert(devname);
        assert(m);
        assert(fstype);
        POINTER_MAY_BE_NULL(mount_node_fd);
 
        if (!image_filter_test(filter, PARTITION_ROOT, /* label= */ NULL)) /* do a filter check with an empty partition label */
                return -ECOMM;
 
        r = image_policy_may_use(policy, PARTITION_ROOT);
        if (r < 0)
                return r;
        if (r == 0) /* policy says ignore this, so we ignore it */
                return -ENOPKG;
 
        r = image_policy_check_protection(policy, PARTITION_ROOT, found_flags);
        if (r < 0)
                return r;
 
        r = image_policy_check_partition_flags(policy, PARTITION_ROOT, 0); /* we have no gpt partition flags, hence check against all bits off */
        if (r < 0)
                return r;
 
        r = make_partition_devname(devname, diskseq, /* nr= */ -1, flags, &n);
        if (r < 0)
                return r;
 
        m->single_file_system = true;
        m->encrypted = encrypted;
 
        m->has_verity = verity && verity->data_path;
        m->verity_ready = verity_settings_data_covers(verity, PARTITION_ROOT);
 
        m->has_verity_sig = false; /* signature not embedded, must be specified */
        m->verity_sig_ready = m->verity_ready && iovec_is_set(&verity->root_hash);
 
        if (uuid)
                m->image_uuid = *uuid;
 
        options = mount_options_from_designator(mount_options, PARTITION_ROOT);
        if (options) {
                o = strdup(options);
                if (!o)
                        return -ENOMEM;
        }
 
        m->partitions[PARTITION_ROOT] = (DissectedPartition) {
                .found = true,
                .rw = !m->verity_ready && !fstype_is_ro(*fstype),
                .partno = -1,
                .architecture = _ARCHITECTURE_INVALID,
                .fstype = TAKE_PTR(*fstype),
                .node = TAKE_PTR(n),
                .mount_options = TAKE_PTR(o),
                .mount_node_fd = mount_node_fd ? TAKE_FD(*mount_node_fd) : -EBADF,
                .size = UINT64_MAX,
                .fsmount_fd = -EBADF,
        };
 
        return 0;
}
 
static int dissect_image(
                DissectedImage *m,
                int fd,
                const char *devname,
                const VeritySettings *verity,
                const MountOptions *mount_options,
                const ImagePolicy *policy,
                const ImageFilter *filter,
                DissectImageFlags flags) {
 
        sd_id128_t root_uuid = SD_ID128_NULL, root_verity_uuid = SD_ID128_NULL;
        sd_id128_t usr_uuid = SD_ID128_NULL, usr_verity_uuid = SD_ID128_NULL;
        bool is_gpt, is_mbr, multiple_generic = false,
                generic_rw = false,  /* initialize to appease gcc */
                generic_growfs = false;
        _cleanup_(blkid_free_probep) blkid_probe b = NULL;
        _cleanup_free_ char *generic_node = NULL;
        sd_id128_t generic_uuid = SD_ID128_NULL;
        const char *pttype = NULL;
        blkid_partlist pl;
        int r, generic_nr = -1, n_partitions;
 
        assert(m);
        assert(fd >= 0);
        assert(devname);
        assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR));
        assert(!verity || iovec_is_valid(&verity->root_hash));
        assert(!verity || iovec_is_valid(&verity->root_hash_sig));
        assert(!verity || iovec_is_set(&verity->root_hash) || !iovec_is_set(&verity->root_hash_sig));
        assert(!((flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)));
        assert(m->sector_size > 0);
 
        /* Probes a disk image, and returns information about what it found in *ret.
         *
         * Returns -ENOPKG if no suitable partition table or file system could be found.
         * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found.
         * Returns -ENXIO if we couldn't find any partition suitable as root or /usr partition
         * Returns -ENOTUNIQ if we only found multiple generic partitions and thus don't know what to do with that
         * Returns -ERFKILL if image doesn't match image policy
         * Returns -EBADR if verity data was provided externally for an image that has a GPT partition table (i.e. is not just a naked fs)
         * Returns -EPROTONOSUPPORT if DISSECT_IMAGE_ADD_PARTITION_DEVICES is set but the block device does not have partition logic enabled
         * Returns -ENOMSG if we didn't find a single usable partition (and DISSECT_IMAGE_REFUSE_EMPTY is set)
         * Returns -EUCLEAN if some file system had an ambiguous file system superblock signature
         */
 
        uint64_t diskseq = m->loop ? m->loop->diskseq : 0;
 
        if (verity && iovec_is_set(&verity->root_hash)) {
                sd_id128_t fsuuid, vuuid;
 
                /* If a root hash is supplied, then we use the root partition that has a UUID that match the
                 * first 128-bit of the root hash. And we use the verity partition that has a UUID that match
                 * the final 128-bit. */
 
                if (verity->root_hash.iov_len < sizeof(sd_id128_t))
                        return -EINVAL;
 
                memcpy(&fsuuid, verity->root_hash.iov_base, sizeof(sd_id128_t));
                memcpy(&vuuid, (const uint8_t*) verity->root_hash.iov_base + verity->root_hash.iov_len - sizeof(sd_id128_t), sizeof(sd_id128_t));
 
                if (sd_id128_is_null(fsuuid))
                        return -EINVAL;
                if (sd_id128_is_null(vuuid))
                        return -EINVAL;
 
                /* If the verity data declares it's for the /usr partition, then search for that, in all
                 * other cases assume it's for the root partition. */
                if (verity->designator == PARTITION_USR) {
                        usr_uuid = fsuuid;
                        usr_verity_uuid = vuuid;
                } else {
                        root_uuid = fsuuid;
                        root_verity_uuid = vuuid;
                }
        }
 
        r = dlopen_libblkid(LOG_DEBUG);
        if (r < 0)
                return r;
 
        b = sym_blkid_new_probe();
        if (!b)
                return -ENOMEM;
 
        r = probe_blkid_filter(b);
        if (r < 0)
                return r;
 
        errno = 0;
        r = sym_blkid_probe_set_device(b, fd, 0, 0);
        if (r != 0)
                return errno_or_else(ENOMEM);
 
        errno = 0;
        r = sym_blkid_probe_set_sectorsize(b, m->sector_size);
        if (r != 0)
                return errno_or_else(EIO);
 
        if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
                /* Look for file system superblocks, unless we only shall look for GPT partition tables */
                sym_blkid_probe_enable_superblocks(b, 1);
                sym_blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE|BLKID_SUBLKS_UUID);
        }
 
        sym_blkid_probe_enable_partitions(b, 1);
        sym_blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
 
        errno = 0;
        r = sym_blkid_do_safeprobe(b);
        if (r == _BLKID_SAFEPROBE_ERROR)
                return errno_or_else(EIO);
        if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND))
                return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table.");
 
        assert(r == _BLKID_SAFEPROBE_FOUND);
 
        if ((!(flags & DISSECT_IMAGE_GPT_ONLY) &&
            (flags & DISSECT_IMAGE_GENERIC_ROOT)) ||
            (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) {
                _cleanup_free_ char *root_fstype_string = NULL;
                const char *usage = NULL;
                bool encrypted;
 
                r = partition_policy_determine_fstype(policy, PARTITION_ROOT, &encrypted, &root_fstype_string);
                if (r < 0)
                        return r;
 
                /* If flags permit this, also allow using non-partitioned single-filesystem images */
 
                (void) sym_blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
                if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
                        _cleanup_free_ char *t = NULL;
                        const char *fstype = NULL;
                        _cleanup_close_ int mount_node_fd = -EBADF;
                        sd_id128_t uuid = SD_ID128_NULL;
                        PartitionPolicyFlags found_flags;
 
                        /* OK, we have found a file system, that's our root partition then. */
 
                        if (!root_fstype_string) {
                                (void) sym_blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
 
                                /* blkid will return FAT's serial number as UUID, hence it is quite possible that
                                * parsing this will fail. We'll ignore the ID, since it's just too short to be
                                * useful as true identifier. */
                                (void) blkid_probe_lookup_value_id128(b, "UUID", &uuid);
                        } else
                                /* The policy fstype flags translate to the literal fstype name of each filesystem. */
                                fstype = root_fstype_string;
 
                        encrypted = encrypted || streq_ptr(fstype, "crypto_LUKS");
 
                        if (verity_settings_data_covers(verity, PARTITION_ROOT))
                                found_flags = iovec_is_set(&verity->root_hash_sig) ? PARTITION_POLICY_SIGNED : PARTITION_POLICY_VERITY;
                        else if (encrypted) {
                                r = partition_is_luks2_integrity(fd, /* offset= */ 0, /* size= */ UINT64_MAX);
                                if (r < 0)
                                        return r;
 
                                found_flags = r > 0 ? PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY : PARTITION_POLICY_ENCRYPTED;
                        } else
                                found_flags = PARTITION_POLICY_UNPROTECTED;
 
                        if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) {
                                mount_node_fd = open_partition(devname, /* is_partition= */ false, m->loop);
                                if (mount_node_fd < 0)
                                        return mount_node_fd;
                        }
 
                        if (fstype) {
                                t = strdup(fstype);
                                if (!t)
                                        return -ENOMEM;
                        }
 
                        return dissect_image_from_unpartitioned(
                                        devname,
                                        diskseq,
                                        &uuid,
                                        encrypted,
                                        verity,
                                        mount_options,
                                        policy,
                                        filter,
                                        &mount_node_fd,
                                        &t,
                                        m,
                                        flags,
                                        found_flags);
                }
        }
 
        (void) sym_blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
        if (!pttype)
                return -ENOPKG;
 
        is_gpt = streq_ptr(pttype, "gpt");
        is_mbr = streq_ptr(pttype, "dos");
 
        if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr))
                return -ENOPKG;
 
        /* We support external verity data partitions only if the image has no partition table */
        if (verity && verity->data_path)
                return -EBADR;
 
        if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) {
                /* Safety check: refuse block devices that carry a partition table but for which the kernel doesn't
                 * do partition scanning. */
                r = blockdev_partscan_enabled_fd(fd);
                if (r < 0)
                        return r;
                if (r == 0)
                        return -EPROTONOSUPPORT;
        }
 
        (void) blkid_probe_lookup_value_id128(b, "PTUUID", &m->image_uuid);
 
        errno = 0;
        pl = sym_blkid_probe_get_partitions(b);
        if (!pl)
                return errno_or_else(ENOMEM);
 
        errno = 0;
        n_partitions = sym_blkid_partlist_numof_partitions(pl);
        if (n_partitions < 0)
                return errno_or_else(EIO);
 
        for (int i = 0; i < n_partitions; i++) {
                _cleanup_free_ char *node = NULL;
                unsigned long long pflags;
                blkid_loff_t start, size;
                blkid_partition pp;
                int nr;
 
                errno = 0;
                pp = sym_blkid_partlist_get_partition(pl, i);
                if (!pp)
                        return errno_or_else(EIO);
 
                pflags = sym_blkid_partition_get_flags(pp);
 
                errno = 0;
                nr = sym_blkid_partition_get_partno(pp);
                if (nr < 0)
                        return errno_or_else(EIO);
 
                errno = 0;
                start = sym_blkid_partition_get_start(pp);
                if (start < 0)
                        return errno_or_else(EIO);
                if ((uint64_t) start > INT64_MAX / 512)
                        return log_debug_errno(SYNTHETIC_ERRNO(EOVERFLOW),
                                               "Partition start LBA too large to convert to a byte offset, refusing.");
 
                errno = 0;
                size = sym_blkid_partition_get_size(pp);
                if (size < 0)
                        return errno_or_else(EIO);
                if ((uint64_t) size > INT64_MAX / 512)
                        return log_debug_errno(SYNTHETIC_ERRNO(EOVERFLOW),
                                               "Partition size in LBA too large to convert to a byte offset, refusing.");
 
                /* Ensure the partition's exclusive end LBA can be converted to a byte offset. */
                if ((uint64_t) start + (uint64_t) size > INT64_MAX / 512)
                        return log_debug_errno(SYNTHETIC_ERRNO(EOVERFLOW),
                                               "Partition end LBA too large to convert to a byte offset, refusing.");
 
                /* While probing we need the non-diskseq device node name to access the thing, hence mask off
                 * DISSECT_IMAGE_DISKSEQ_DEVNODE. */
                r = make_partition_devname(devname, diskseq, nr, flags & ~DISSECT_IMAGE_DISKSEQ_DEVNODE, &node);
                if (r < 0)
                        return r;
 
                /* So here's the thing: after the main ("whole") block device popped up it might take a while
                 * before the kernel fully probed the partition table. Waiting for that to finish is icky in
                 * userspace. So here's what we do instead. We issue the BLKPG_ADD_PARTITION ioctl to add the
                 * partition ourselves, racing against the kernel. Good thing is: if this call fails with
                 * EBUSY then the kernel was quicker than us, and that's totally OK, the outcome is good for
                 * us: the device node will exist. If OTOH our call was successful we won the race. Which is
                 * also good as the outcome is the same: the partition block device exists, and we can use
                 * it.
                 *
                 * Kernel returns EBUSY if there's already a partition by that number or an overlapping
                 * partition already existent. */
 
                if (FLAGS_SET(flags, DISSECT_IMAGE_ADD_PARTITION_DEVICES)) {
                        r = block_device_add_partition(fd, nr, (uint64_t) start * 512, (uint64_t) size * 512);
                        if (r < 0) {
                                if (r != -EBUSY)
                                        return log_debug_errno(r, "BLKPG_ADD_PARTITION failed: %m");
 
                                log_debug_errno(r, "Kernel was quicker than us in adding partition %i.", nr);
                        } else
                                log_debug("We were quicker than kernel in adding partition %i.", nr);
                }
 
                if (is_gpt) {
                        const char *label;
                        sd_id128_t type_id, id;
                        GptPartitionType type;
                        bool rw = true, growfs = false;
 
                        r = blkid_partition_get_uuid_id128(pp, &id);
                        if (r < 0) {
                                log_debug_errno(r, "Failed to read partition UUID, ignoring: %m");
                                continue;
                        }
 
                        r = blkid_partition_get_type_id128(pp, &type_id);
                        if (r < 0) {
                                log_debug_errno(r, "Failed to read partition type UUID, ignoring: %m");
                                continue;
                        }
 
                        type = gpt_partition_type_from_uuid(type_id);
 
                        label = sym_blkid_partition_get_name(pp); /* libblkid returns NULL here if empty */
 
                        /* systemd-sysupdate expects empty partitions to be marked with an "_empty" label, hence ignore them here. */
                        if (streq_ptr(label, "_empty"))
                                continue;
 
                        if (!image_filter_test(filter, type.designator, label))
                                continue;
 
                        log_debug("Dissecting %s partition with label %s and UUID %s.",
                                  strna(partition_designator_to_string(type.designator)), strna(label), SD_ID128_TO_UUID_STRING(id));
 
                        /* Note that we don't check the SD_GPT_FLAG_NO_AUTO flag for the ESP, as it is
                         * not defined there. We instead check the SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as
                         * recommended by the UEFI spec (See "12.3.3 Number and Location of System
                         * Partitions"). */
                        if (FLAGS_SET(pflags, SD_GPT_FLAG_NO_AUTO) && type.designator != PARTITION_ESP) {
                                log_debug("Partition has 'no auto' flag set, ignoring.");
                                continue;
                        }
 
                        if (!verity && partition_designator_is_verity(type.designator)) {
                                log_debug("Partition is a verity hash or verity signature partition but no verity was requested, ignoring.");
                                continue;
                        }
 
                        PartitionDesignator vd = partition_verity_to_data(type.designator);
                        if (verity && verity->designator >= 0 && vd >= 0 && vd != verity->designator) {
                                log_debug("Partition is a %s partition but verity was only requested for the %s partition, ignoring.",
                                          partition_designator_to_string(type.designator),
                                          partition_designator_to_string(verity->designator));
                                continue;
                        }
 
                        const char *fstype = getenv_fstype(type.designator);
 
                        if (IN_SET(type.designator,
                                   PARTITION_HOME,
                                   PARTITION_SRV,
                                   PARTITION_XBOOTLDR,
                                   PARTITION_TMP)) {
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS);
 
                                rw = !(pflags & SD_GPT_FLAG_READ_ONLY);
                                growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS);
 
                                /* XBOOTLDR cannot be integrity protected (since firmware needs to access
                                 * it), hence be restrictive with the fs choice when dissecting. */
                                if (type.designator == PARTITION_XBOOTLDR && !fstype)
                                        fstype = "vfat";
 
                        } else if (type.designator == PARTITION_ESP) {
 
                                if (FLAGS_SET(pflags, SD_GPT_FLAG_NO_BLOCK_IO_PROTOCOL)) {
                                        log_debug("ESP Partition has 'no block io' flag set, ignoring.");
                                        continue;
                                }
 
                                /* Effectively the ESP has to be VFAT, let's enforce this */
                                if (!fstype)
                                        fstype = "vfat";
 
                        } else if (IN_SET(type.designator, PARTITION_ROOT, PARTITION_USR)) {
                                sd_id128_t expected_uuid = type.designator == PARTITION_ROOT ? root_uuid : usr_uuid;
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS);
 
                                if (!sd_id128_is_null(expected_uuid) && !sd_id128_equal(expected_uuid, id)) {
                                        log_debug("Partition UUID '%s' does not match expected UUID '%s' derived from %s verity hash, ignoring.",
                                                  SD_ID128_TO_UUID_STRING(id),
                                                  SD_ID128_TO_UUID_STRING(expected_uuid),
                                                  partition_designator_to_string(type.designator));
                                        continue;
                                }
 
                                rw = !(pflags & SD_GPT_FLAG_READ_ONLY);
                                growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS);
 
                        } else if (IN_SET(type.designator, PARTITION_ROOT_VERITY, PARTITION_USR_VERITY)) {
                                sd_id128_t expected_uuid = type.designator == PARTITION_ROOT_VERITY ? root_verity_uuid : usr_verity_uuid;
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY);
 
                                m->has_verity = true;
 
                                if (!sd_id128_is_null(expected_uuid) && !sd_id128_equal(expected_uuid, id)) {
                                        log_debug("Partition UUID '%s' does not match expected UUID '%s' derived from %s verity hash, ignoring.",
                                                  SD_ID128_TO_UUID_STRING(id),
                                                  SD_ID128_TO_UUID_STRING(expected_uuid),
                                                  partition_designator_to_string(partition_verity_to_data(type.designator)));
                                        continue;
                                }
 
                                fstype = "DM_verity_hash";
                                rw = false;
 
                        } else if (IN_SET(type.designator, PARTITION_ROOT_VERITY_SIG, PARTITION_USR_VERITY_SIG)) {
                                if (verity && iovec_is_set(&verity->root_hash)) {
                                        _cleanup_(iovec_done) struct iovec root_hash = {};
 
                                        r = acquire_sig_for_roothash(
                                                        fd,
                                                        (uint64_t) start * 512,
                                                        (uint64_t) size * 512,
                                                        &root_hash,
                                                        /* ret_root_hash_sig= */ NULL);
                                        if (r < 0)
                                                return r;
                                        if (!iovec_equal(&verity->root_hash, &root_hash)) {
                                                if (DEBUG_LOGGING) {
                                                        _cleanup_free_ char *found = NULL, *expected = NULL;
 
                                                        found = hexmem(root_hash.iov_base, root_hash.iov_len);
                                                        expected = hexmem(verity->root_hash.iov_base, verity->root_hash.iov_len);
 
                                                        log_debug("Verity root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(found), strna(expected));
                                                }
                                                continue;
                                        }
                                }
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY);
 
                                m->has_verity_sig = true;
                                fstype = "verity_hash_signature";
                                rw = false;
 
                        } else if (type.designator == PARTITION_SWAP) {
 
                                check_partition_flags(node, pflags, SD_GPT_FLAG_NO_AUTO);
 
                                /* Note: we don't set fstype = "swap" here, because we still need to probe if
                                 * it might be encrypted (i.e. fstype "crypt_LUKS") or unencrypted
                                 * (i.e. fstype "swap"), and the only way to figure that out is via fstype
                                 * probing. */
 
                        /* We don't have a designator for SD_GPT_LINUX_GENERIC so check the UUID instead. */
                        } else if (sd_id128_equal(type.uuid, SD_GPT_LINUX_GENERIC)) {
 
                                if (!image_filter_test(filter, PARTITION_ROOT, label))
                                        continue;
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS);
 
                                if (generic_node)
                                        multiple_generic = true;
                                else {
                                        generic_nr = nr;
                                        generic_rw = !(pflags & SD_GPT_FLAG_READ_ONLY);
                                        generic_growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS);
                                        generic_uuid = id;
                                        generic_node = TAKE_PTR(node);
                                }
 
                        } else if (type.designator == PARTITION_VAR) {
 
                                check_partition_flags(node, pflags,
                                                      SD_GPT_FLAG_NO_AUTO | SD_GPT_FLAG_READ_ONLY | SD_GPT_FLAG_GROWFS);
 
                                if (!FLAGS_SET(flags, DISSECT_IMAGE_RELAX_VAR_CHECK)) {
                                        sd_id128_t var_uuid;
 
                                        /* For /var we insist that the uuid of the partition matches the
                                         * HMAC-SHA256 of the /var GPT partition type uuid, keyed by machine
                                         * ID. Why? Unlike the other partitions /var is inherently
                                         * installation specific, hence we need to be careful not to mount it
                                         * in the wrong installation. By hashing the partition UUID from
                                         * /etc/machine-id we can securely bind the partition to the
                                         * installation. */
 
                                        r = sd_id128_get_machine_app_specific(SD_GPT_VAR, &var_uuid);
                                        if (r < 0)
                                                return r;
 
                                        if (!sd_id128_equal(var_uuid, id)) {
                                                log_debug("Found a /var/ partition, but its UUID didn't match our expectations "
                                                          "(found: " SD_ID128_UUID_FORMAT_STR ", expected: " SD_ID128_UUID_FORMAT_STR "), ignoring.",
                                                          SD_ID128_FORMAT_VAL(id), SD_ID128_FORMAT_VAL(var_uuid));
                                                continue;
                                        }
                                }
 
                                rw = !(pflags & SD_GPT_FLAG_READ_ONLY);
                                growfs = FLAGS_SET(pflags, SD_GPT_FLAG_GROWFS);
                        }
 
                        if (type.designator != _PARTITION_DESIGNATOR_INVALID) {
                                _cleanup_free_ char *t = NULL, *o = NULL, *l = NULL, *n = NULL;
                                _cleanup_close_ int mount_node_fd = -EBADF;
                                const char *options = NULL;
 
                                r = image_policy_may_use(policy, type.designator);
                                if (r < 0)
                                        return r;
                                if (r == 0) {
                                        /* Policy says: ignore; Remember this fact, so that we later can distinguish between "found but ignored" and "not found at all" */
 
                                        if (!m->partitions[type.designator].found)
                                                m->partitions[type.designator].ignored = true;
 
                                        continue;
                                }
 
                                if (m->partitions[type.designator].found) {
                                        int c;
 
                                        /* For most partition types the first one we see wins. Except for the
                                         * rootfs and /usr, where we do a version compare of the label, and
                                         * let the newest version win. This permits a simple A/B versioning
                                         * scheme in OS images. */
 
                                        c = compare_arch(type.arch, m->partitions[type.designator].architecture);
                                        if (c < 0) /* the arch we already found is better than the one we found now */
                                                continue;
                                        if (c == 0 && /* same arch? then go by version in label */
                                            (!partition_designator_is_versioned(type.designator) ||
                                             strverscmp_improved(label, m->partitions[type.designator].label) <= 0))
                                                continue;
 
                                        dissected_partition_done(m->partitions + type.designator);
                                }
 
                                if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES) &&
                                    type.designator != PARTITION_SWAP) {
                                        mount_node_fd = open_partition(node, /* is_partition= */ true, m->loop);
                                        if (mount_node_fd < 0)
                                                return mount_node_fd;
                                }
 
                                r = make_partition_devname(devname, diskseq, nr, flags, &n);
                                if (r < 0)
                                        return r;
 
                                /* Local override via env var or designator type wins */
                                if (fstype) {
                                        t = strdup(fstype);
                                        if (!t)
                                                return -ENOMEM;
                                } else {
                                        r = partition_policy_determine_fstype(policy, type.designator, /* ret_encrypted= */ NULL, &t);
                                        if (r < 0)
                                                return r;
                                }
 
                                if (label) {
                                        l = strdup(label);
                                        if (!l)
                                                return -ENOMEM;
                                }
 
                                options = mount_options_from_designator(mount_options, type.designator);
                                if (options) {
                                        o = strdup(options);
                                        if (!o)
                                                return -ENOMEM;
                                }
 
                                m->partitions[type.designator] = (DissectedPartition) {
                                        .found = true,
                                        .partno = nr,
                                        .rw = rw,
                                        .growfs = growfs,
                                        .architecture = type.arch,
                                        .node = TAKE_PTR(n),
                                        .fstype = TAKE_PTR(t),
                                        .label = TAKE_PTR(l),
                                        .uuid = id,
                                        .mount_options = TAKE_PTR(o),
                                        .mount_node_fd = TAKE_FD(mount_node_fd),
                                        .offset = (uint64_t) start * 512,
                                        .size = (uint64_t) size * 512,
                                        .gpt_flags = pflags,
                                        .fsmount_fd = -EBADF,
                                };
                        }
 
                } else if (is_mbr) {
 
                        switch (sym_blkid_partition_get_type(pp)) {
 
                        case 0x83: /* Linux partition */
 
                                if (pflags != 0x80) /* Bootable flag */
                                        continue;
 
                                if (!image_filter_test(filter, PARTITION_ROOT, /* label= */ NULL))
                                        continue;
 
                                if (generic_node)
                                        multiple_generic = true;
                                else {
                                        generic_nr = nr;
                                        generic_rw = true;
                                        generic_growfs = false;
                                        generic_node = TAKE_PTR(node);
                                }
 
                                break;
 
                        case 0xEA: { /* Boot Loader Spec extended $BOOT partition */
                                _cleanup_close_ int mount_node_fd = -EBADF;
                                _cleanup_free_ char *o = NULL, *n = NULL;
                                sd_id128_t id = SD_ID128_NULL;
                                const char *options = NULL;
 
                                if (!image_filter_test(filter, PARTITION_XBOOTLDR, /* label= */ NULL))
                                        continue;
 
                                r = image_policy_may_use(policy, PARTITION_XBOOTLDR);
                                if (r < 0)
                                        return r;
                                if (r == 0) { /* policy says: ignore */
                                        if (!m->partitions[PARTITION_XBOOTLDR].found)
                                                m->partitions[PARTITION_XBOOTLDR].ignored = true;
 
                                        continue;
                                }
 
                                /* First one wins */
                                if (m->partitions[PARTITION_XBOOTLDR].found)
                                        continue;
 
                                if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) {
                                        mount_node_fd = open_partition(node, /* is_partition= */ true, m->loop);
                                        if (mount_node_fd < 0)
                                                return mount_node_fd;
                                }
 
                                (void) blkid_partition_get_uuid_id128(pp, &id);
 
                                r = make_partition_devname(devname, diskseq, nr, flags, &n);
                                if (r < 0)
                                        return r;
 
                                options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR);
                                if (options) {
                                        o = strdup(options);
                                        if (!o)
                                                return -ENOMEM;
                                }
 
                                m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) {
                                        .found = true,
                                        .partno = nr,
                                        .rw = true,
                                        .growfs = false,
                                        .architecture = _ARCHITECTURE_INVALID,
                                        .node = TAKE_PTR(n),
                                        .uuid = id,
                                        .mount_options = TAKE_PTR(o),
                                        .mount_node_fd = TAKE_FD(mount_node_fd),
                                        .offset = (uint64_t) start * 512,
                                        .size = (uint64_t) size * 512,
                                        .fsmount_fd = -EBADF,
                                };
 
                                break;
                        }}
                }
        }
 
        /* Verity found but no matching data partition? Something is off, refuse. */
        FOREACH_ELEMENT(dd, ((const PartitionDesignator[]) { PARTITION_ROOT, PARTITION_USR })) {
                PartitionDesignator dv = partition_verity_hash_of(*dd);
                PartitionDesignator ds = partition_verity_sig_of(*dd);
 
                /* Hint to help static analyzers */
                assert(dv >= 0);
                assert(ds >= 0);
 
                if (!m->partitions[*dd].found && (m->partitions[dv].found || m->partitions[ds].found))
                        return log_debug_errno(
                                        SYNTHETIC_ERRNO(EADDRNOTAVAIL),
                                        "Found %s verity hash partition without matching %s data partition.",
                                        partition_designator_to_string(*dd),
                                        partition_designator_to_string(*dd));
 
                if (m->partitions[ds].found && !m->partitions[dv].found)
                        return log_debug_errno(
                                        SYNTHETIC_ERRNO(EADDRNOTAVAIL),
                                        "Found %s verity signature partition without matching %s verity hash partition.",
                                        partition_designator_to_string(*dd),
                                        partition_designator_to_string(*dd));
        }
 
        /* If root and /usr are combined then insist that the architecture matches */
        if (m->partitions[PARTITION_ROOT].found &&
            m->partitions[PARTITION_USR].found &&
            (m->partitions[PARTITION_ROOT].architecture >= 0 &&
             m->partitions[PARTITION_USR].architecture >= 0 &&
             m->partitions[PARTITION_ROOT].architecture != m->partitions[PARTITION_USR].architecture))
                return log_debug_errno(SYNTHETIC_ERRNO(EREMOTE),
                                       "Found root and usr partitions with different architectures (%s vs %s).",
                                       architecture_to_string(m->partitions[PARTITION_ROOT].architecture),
                                       architecture_to_string(m->partitions[PARTITION_USR].architecture));
 
        if (!m->partitions[PARTITION_ROOT].found &&
            !m->partitions[PARTITION_USR].found &&
            (flags & DISSECT_IMAGE_GENERIC_ROOT) &&
            (!verity || !iovec_is_set(&verity->root_hash) || verity->designator != PARTITION_USR)) {
 
                /* OK, we found nothing usable, then check if there's a single generic partition, and use
                 * that. If the root hash was set however, then we won't fall back to a generic node, because
                 * the root hash decides. */
 
                /* If we didn't find a properly marked root partition, but we did find a single suitable
                 * generic Linux partition, then use this as root partition, if the caller asked for it. */
                if (multiple_generic)
                        return -ENOTUNIQ;
 
                /* If we didn't find a generic node, then we can't fix this up either */
                if (generic_node) {
                        r = image_policy_may_use(policy, PARTITION_ROOT);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                /* Policy says: ignore; remember that we did */
                                m->partitions[PARTITION_ROOT].ignored = true;
                        else {
                                _cleanup_close_ int mount_node_fd = -EBADF;
                                _cleanup_free_ char *o = NULL, *n = NULL;
                                const char *options;
 
                                if (FLAGS_SET(flags, DISSECT_IMAGE_PIN_PARTITION_DEVICES)) {
                                        mount_node_fd = open_partition(generic_node, /* is_partition= */ true, m->loop);
                                        if (mount_node_fd < 0)
                                                return mount_node_fd;
                                }
 
                                r = make_partition_devname(devname, diskseq, generic_nr, flags, &n);
                                if (r < 0)
                                        return r;
 
                                options = mount_options_from_designator(mount_options, PARTITION_ROOT);
                                if (options) {
                                        o = strdup(options);
                                        if (!o)
                                                return -ENOMEM;
                                }
 
                                assert(generic_nr >= 0);
                                m->partitions[PARTITION_ROOT] = (DissectedPartition) {
                                        .found = true,
                                        .rw = generic_rw,
                                        .growfs = generic_growfs,
                                        .partno = generic_nr,
                                        .architecture = _ARCHITECTURE_INVALID,
                                        .node = TAKE_PTR(n),
                                        .uuid = generic_uuid,
                                        .mount_options = TAKE_PTR(o),
                                        .mount_node_fd = TAKE_FD(mount_node_fd),
                                        .offset = UINT64_MAX,
                                        .size = UINT64_MAX,
                                        .fsmount_fd = -EBADF,
                                };
                        }
                }
        }
 
        /* Check if we have a root fs if we are told to do check. /usr alone is fine too, but only if appropriate flag for that is set too */
        if (FLAGS_SET(flags, DISSECT_IMAGE_REQUIRE_ROOT) &&
            !(m->partitions[PARTITION_ROOT].found || (m->partitions[PARTITION_USR].found && FLAGS_SET(flags, DISSECT_IMAGE_USR_NO_ROOT))))
                return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "Root or usr partition requested but found neither.");
 
        if (m->partitions[PARTITION_ROOT_VERITY].found) {
                /* We only support one verity partition per image, i.e. can't do for both /usr and root fs */
                if (m->partitions[PARTITION_USR_VERITY].found)
                        return log_debug_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "Found both root and usr verity enabled partitions which is not supported.");
 
                /* We don't support verity enabled root with a split out /usr. Neither with nor without
                 * verity there. (Note that we do support verity-less root with verity-full /usr, though.) */
                if (m->partitions[PARTITION_USR].found)
                        return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), "Found verity enabled root partition with split usr partition which is not supported.");
        }
 
        if (verity) {
                /* If a verity designator is specified, then insist that the matching partition exists */
                if (verity->designator >= 0 && !m->partitions[verity->designator].found)
                        return log_debug_errno(
                                SYNTHETIC_ERRNO(EADDRNOTAVAIL),
                                "Explicit %s verity designator was specified but did not find %s partition.",
                                partition_designator_to_string(verity->designator),
                                partition_designator_to_string(verity->designator));
 
                if (iovec_is_set(&verity->root_hash)) {
                        /* If we have an explicit root hash and found the partitions for it, then we are ready to use
                         * Verity, set things up for it */
 
                        PartitionDesignator d = verity->designator < 0 || verity->designator == PARTITION_ROOT
                                ? PARTITION_ROOT : PARTITION_USR;
                        PartitionDesignator dv = partition_verity_hash_of(d);
                        assert(dv >= 0);
 
                        if (!m->partitions[d].found)
                                return log_debug_errno(
                                                SYNTHETIC_ERRNO(EADDRNOTAVAIL),
                                                "Verity enabled %s partition was requested but did not find a %s data partition.",
                                                partition_designator_to_string(d),
                                                partition_designator_to_string(d));
 
                        if (!m->partitions[dv].found)
                                return log_debug_errno(
                                                SYNTHETIC_ERRNO(EADDRNOTAVAIL),
                                                "Verity enabled %s partition was requested but did not find a %s verity hash partition.",
                                                partition_designator_to_string(d),
                                                partition_designator_to_string(d));
 
                        /* If we found a verity setup, then the data partition is necessarily read-only. */
                        m->partitions[d].rw = false;
 
                        m->verity_ready = true;
 
                        if (iovec_is_set(&verity->root_hash_sig))
                                m->verity_sig_ready = true;
                }
        }
 
        bool any = false;
 
        /* After we discovered all partitions let's see if the verity requirements match the policy. (Note:
         * we don't check encryption requirements here, because we haven't probed the file system yet, hence
         * don't know if this is encrypted or not) */
        for (PartitionDesignator di = 0; di < _PARTITION_DESIGNATOR_MAX; di++) {
                any = any || m->partitions[di].found;
 
                /* Determine the verity protection level for this partition. */
                PartitionPolicyFlags found_flags;
                if (m->partitions[di].found) {
                        found_flags = PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_UNUSED;
 
                        PartitionDesignator vi = partition_verity_hash_of(di);
                        if (vi >= 0 && m->partitions[vi].found) {
                                found_flags |= PARTITION_POLICY_VERITY;
 
                                PartitionDesignator si = partition_verity_sig_of(di);
                                if (si >= 0 && m->partitions[si].found)
                                        found_flags |= PARTITION_POLICY_SIGNED;
                        }
                } else
                        found_flags = m->partitions[di].ignored ? PARTITION_POLICY_UNUSED : PARTITION_POLICY_ABSENT;
 
                if (DEBUG_LOGGING) {
                        _cleanup_free_ char *s = NULL;
                        (void) partition_policy_flags_to_string(found_flags, /* simplify= */ false, &s);
                        log_debug("Found for designator %s: %s.", partition_designator_to_string(di), strna(s));
                }
 
                r = image_policy_check_protection(policy, di, found_flags);
                if (r < 0)
                        return r;
 
                if (m->partitions[di].found) {
                        r = image_policy_check_partition_flags(policy, di, m->partitions[di].gpt_flags);
                        if (r < 0)
                                return r;
                }
        }
 
        if (!any && !FLAGS_SET(flags, DISSECT_IMAGE_ALLOW_EMPTY))
                return -ENOMSG;
 
        r = dissected_image_probe_filesystems(m, fd, policy);
        if (r < 0)
                return r;
 
        return 0;
}
#endif
 
int dissected_image_new_from_existing_verity(
                const char *src,
                const VeritySettings *verity,
                const MountOptions *options,
                const ImagePolicy *image_policy,
                const ImageFilter *image_filter,
                RuntimeScope runtime_scope,
                DissectImageFlags dissect_image_flags,
                DissectedImage **ret) {
 
        /* Look for an already set up dm-verity device with a single filesystem, according to our naming
         * scheme and image policy, and if it is pinned by filesystem type set up the image directly. */
 
#if HAVE_BLKID
        _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
        _cleanup_free_ char *node = NULL, *root_hash_encoded = NULL, *root_fstype_string = NULL;
        _cleanup_close_ int mount_node_fd = -EBADF;
        PartitionPolicyFlags found_flags;
        bool encrypted = false;
        int r;
 
        assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR));
        assert(!verity || iovec_is_valid(&verity->root_hash));
        assert(!verity || iovec_is_valid(&verity->root_hash_sig));
        assert(!verity || iovec_is_set(&verity->root_hash) || !iovec_is_set(&verity->root_hash_sig));
        assert(ret);
 
        /* Shortcut: this deals only with verity images and requires a policy, and only for system services */
        if (runtime_scope != RUNTIME_SCOPE_SYSTEM ||
            !FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_VERITY_SHARE) ||
            !image_policy ||
            !verity ||
            !verity->data_path ||