HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/dissect/dissect.c

93,809 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 29bf4218191eb6f1dd3491b091dfaed8940e5beb

Lines 2001–2160 of 2,160

                r = verity_settings_load(
                                &arg_verity_settings,
                                arg_image,
                                /* root_hash_path= */ NULL,
                                /* root_hash_sig_path= */ NULL);
                if (r < 0)
                        return log_error_errno(r, "Failed to read verity artifacts for %s: %m", arg_image);
 
                if (arg_verity_settings.data_path)
                        arg_flags |= DISSECT_IMAGE_NO_PARTITION_TABLE; /* We only support Verity per file system,
                                                                        * hence if there's external Verity data
                                                                        * available we turn off partition table
                                                                        * support */
        }
 
        if (!arg_loop_ref && arg_loop_ref_auto) {
                r = path_extract_filename(arg_image, &arg_loop_ref);
                if (r < 0)
                        return log_error_errno(r, "Failed to extract file name from image path '%s': %m", arg_image);
        }
 
        if (arg_action == ACTION_VALIDATE)
                return action_validate();
 
        if (arg_image) {
                /* First try locally, if we are allowed to */
                if (!arg_via_service) {
                        uint32_t loop_flags;
                        int open_flags;
 
                        open_flags = FLAGS_SET(arg_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1;
                        loop_flags = FLAGS_SET(arg_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN;
                        /* --attach hands a loop device to the user, who may populate it with a (nested)
                         * partition table afterwards, so force a real loopback device with partition
                         * scanning even if the image is currently unpartitioned. */
                        if (arg_action == ACTION_ATTACH)
                                loop_flags |= LOOP_DEVICE_MAY_POPULATE_PARTITION_TABLE;
 
                        if (arg_in_memory)
                                r = loop_device_make_by_path_memory(arg_image, open_flags, /* sector_size= */ UINT32_MAX, loop_flags, LOCK_SH, &d);
                        else
                                r = loop_device_make_by_path(arg_image, open_flags, /* sector_size= */ UINT32_MAX, loop_flags, LOCK_SH, &d);
                        if (r < 0) {
                                if ((r != -ENOENT && !ERRNO_IS_PRIVILEGE(r)) || arg_action == ACTION_ATTACH)
                                        return log_error_errno(r, "Failed to set up loopback device for %s: %m", arg_image);
 
                                log_debug_errno(r, "Lacking permissions or missing /dev/loop-control to set up loopback block device for %s, using service: %m", arg_image);
                                arg_via_service = true;
                        } else {
                                if (arg_loop_ref && !LOOP_DEVICE_IS_FOREIGN(d)) {
                                        r = loop_device_set_filename(d, arg_loop_ref);
                                        if (r < 0)
                                                log_warning_errno(r, "Failed to set loop reference string to '%s', ignoring: %m", arg_loop_ref);
                                }
 
                                r = dissect_loop_device_and_warn(
                                                d,
                                                &arg_verity_settings,
                                                /* mount_options= */ NULL,
                                                arg_image_policy,
                                                arg_image_filter,
                                                arg_flags,
                                                &m);
                                if (r < 0)
                                        return r;
 
                                if (arg_action == ACTION_ATTACH)
                                        return action_attach(m, d);
 
                                r = dissected_image_load_verity_sig_partition(
                                                m,
                                                d->fd,
                                                &arg_verity_settings);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to load verity signature partition: %m");
 
                                r = dissected_image_guess_verity_roothash(
                                                m,
                                                &arg_verity_settings);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to guess verity root hash: %m");
 
                                if (arg_action != ACTION_DISSECT) {
                                        _cleanup_(erase_and_freep) char *envpw = NULL;
 
                                        r = getenv_steal_erase("PASSWORD", &envpw);
                                        if (r < 0)
                                                return log_error_errno(r, "Failed to acquire password from environment: %m");
 
                                        r = dissected_image_decrypt_interactively(
                                                        m,
                                                        envpw,
                                                        &arg_verity_settings,
                                                        arg_image_policy,
                                                        arg_flags);
                                        if (r < 0)
                                                return r;
                                }
                        }
                }
 
                /* Try via service */
                if (arg_via_service) {
                        if (arg_in_memory)
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--in-memory= not supported when operating via systemd-mountfsd.");
 
                        if (arg_loop_ref || arg_loop_ref_auto) /* yes, the 2nd check is strictly speaking redundant, given the normalization we did above, but let's be explicit here */
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--loop-ref=/--loop-ref-auto not supported when operating via systemd-mountfsd.");
 
                        /* Don't run things in private userns, if the mount shall be attached to the host
                         * or if we're copying from/to the host. */
                        if (!IN_SET(arg_action, ACTION_MOUNT, ACTION_WITH, ACTION_COPY_FROM, ACTION_COPY_TO)) {
                                userns_fd = nsresource_allocate_userns(
                                                /* vl= */ NULL,
                                                /* name= */ NULL,
                                                NSRESOURCE_UIDS_64K); /* allocate 64K users by default */
                                if (userns_fd < 0)
                                        return log_error_errno(userns_fd, "Failed to allocate user namespace with 64K users: %m");
                        }
 
                        r = mountfsd_mount_image(
                                        /* vl= */ NULL,
                                        arg_image,
                                        userns_fd,
                                        /* options= */ NULL,
                                        arg_image_policy,
                                        &arg_verity_settings,
                                        arg_flags,
                                        &m);
                        if (r < 0)
                                return log_error_errno(r, "Failed to mount image via mountfsd: %m");
                }
        }
 
        switch (arg_action) {
 
        case ACTION_DISSECT:
                return action_dissect(m, d, userns_fd);
 
        case ACTION_MOUNT:
                return action_mount(m, d);
 
        case ACTION_LIST:
        case ACTION_MTREE:
        case ACTION_COPY_FROM:
        case ACTION_COPY_TO:
        case ACTION_MAKE_ARCHIVE:
        case ACTION_SHIFT:
                return action_list_or_mtree_or_copy_or_make_archive(m, d, userns_fd);
 
        case ACTION_WITH:
                return action_with(m, d);
 
        default:
                assert_not_reached();
        }
}
 
DEFINE_MAIN_FUNCTION(run);