HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/dissect-image.c
Git blob: 9280a90974918216809a09219e8a3162bddcf789
Lines 4001–5000 of 5,731
designator = PARTITION_ROOT;
} else {
if (designator < 0 || designator == PARTITION_ROOT) {
_cleanup_free_ char *p = NULL;
/* Follow naming convention recommended by the relevant RFC:
* https://tools.ietf.org/html/rfc5751#section-3.2.1 */
p = build_auxiliary_path(image, ".roothash.p7s");
if (!p)
return -ENOMEM;
r = read_full_file(p, (char**) &root_hash_sig.iov_base, &root_hash_sig.iov_len);
if (r < 0 && r != -ENOENT)
return r;
if (r >= 0) {
designator = PARTITION_ROOT;
if (root_hash_sig.iov_len == 0) /* refuse empty size signatures */
return -EINVAL;
}
}
if (!iovec_is_set(&root_hash_sig) && (designator < 0 || designator == PARTITION_USR)) {
_cleanup_free_ char *p = NULL;
p = build_auxiliary_path(image, ".usrhash.p7s");
if (!p)
return -ENOMEM;
r = read_full_file(p, (char**) &root_hash_sig.iov_base, &root_hash_sig.iov_len);
if (r < 0 && r != -ENOENT)
return r;
if (r >= 0) {
designator = PARTITION_USR;
if (root_hash_sig.iov_len == 0) /* refuse empty size signatures */
return -EINVAL;
}
}
}
}
_cleanup_free_ char *verity_data_path = NULL;
if (!verity->data_path) {
_cleanup_free_ char *p = NULL;
p = build_auxiliary_path(image, ".verity");
if (!p)
return -ENOMEM;
if (access(p, F_OK) < 0) {
if (errno != ENOENT)
return -errno;
} else
verity_data_path = TAKE_PTR(p);
}
if (iovec_is_set(&root_hash))
verity->root_hash = TAKE_STRUCT(root_hash);
if (iovec_is_set(&root_hash_sig))
verity->root_hash_sig = TAKE_STRUCT(root_hash_sig);
if (verity_data_path)
verity->data_path = TAKE_PTR(verity_data_path);
if (verity->designator < 0)
verity->designator = designator;
return 1;
}
int verity_settings_copy(VeritySettings *dest, const VeritySettings *source) {
assert(dest);
if (!source) {
*dest = VERITY_SETTINGS_DEFAULT;
return 0;
}
_cleanup_(iovec_done) struct iovec rh = {};
if (iovec_is_set(&source->root_hash)) {
if (!iovec_memdup(&source->root_hash, &rh))
return log_oom_debug();
}
_cleanup_(iovec_done) struct iovec sig = {};
if (iovec_is_set(&source->root_hash_sig)) {
if (!iovec_memdup(&source->root_hash_sig, &sig))
return log_oom_debug();
}
_cleanup_free_ char *p = NULL;
if (source->data_path) {
p = strdup(source->data_path);
if (!p)
return log_oom_debug();
}
*dest = (VeritySettings) {
.root_hash = TAKE_STRUCT(rh),
.root_hash_sig = TAKE_STRUCT(sig),
.data_path = TAKE_PTR(p),
.designator = source->designator,
};
return 1;
}
int dissected_image_load_verity_sig_partition(
DissectedImage *m,
int fd,
VeritySettings *verity) {
int r;
assert(m);
assert(fd >= 0);
assert(verity);
if (iovec_is_set(&verity->root_hash) && iovec_is_set(&verity->root_hash_sig)) /* Already loaded? */
return 0;
r = secure_getenv_bool("SYSTEMD_DISSECT_VERITY_EMBEDDED");
if (r < 0 && r != -ENXIO)
log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_EMBEDDED, ignoring: %m");
if (r == 0)
return 0;
PartitionDesignator dd = verity->designator;
if (dd < 0) {
if (m->partitions[PARTITION_ROOT_VERITY].found)
dd = PARTITION_ROOT;
else if (m->partitions[PARTITION_USR_VERITY].found)
dd = PARTITION_USR;
else
return 0;
}
if (!m->partitions[dd].found)
return 0;
PartitionDesignator dv = partition_verity_hash_of(dd);
assert(dv >= 0);
if (!m->partitions[dv].found)
return 0;
PartitionDesignator ds = partition_verity_sig_of(dd);
assert(ds >= 0);
DissectedPartition *p = m->partitions + ds;
if (!p->found)
return 0;
_cleanup_(iovec_done) struct iovec root_hash = {}, root_hash_sig = {};
r = acquire_sig_for_roothash(
fd,
p->offset,
p->size,
&root_hash,
&root_hash_sig);
if (r < 0)
return r;
/* Check if specified root hash matches if it is specified */
if (iovec_is_set(&verity->root_hash) &&
!iovec_equal(&verity->root_hash, &root_hash)) {
_cleanup_free_ char *a = NULL, *b = NULL;
a = hexmem(root_hash.iov_base, root_hash.iov_len);
b = hexmem(verity->root_hash.iov_base, verity->root_hash.iov_len);
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(a), strna(b));
}
iovec_done(&verity->root_hash);
verity->root_hash = TAKE_STRUCT(root_hash);
iovec_done(&verity->root_hash_sig);
verity->root_hash_sig = TAKE_STRUCT(root_hash_sig);
verity->designator = dd;
m->verity_ready = true;
m->verity_sig_ready = true;
m->partitions[dd].rw = false;
return 1;
}
int dissected_image_guess_verity_roothash(
DissectedImage *m,
VeritySettings *verity) {
int r;
assert(m);
assert(verity);
/* Guesses the Verity root hash from the partitions we found, taking into account that as per
* https://uapi-group.org/specifications/specs/discoverable_partitions_specification/ the UUIDS of
* the data and verity partitions are respectively the first and second halves of the dm-verity
* roothash.
*
* Note of course that relying on this guesswork is mostly useful for later attestation, not so much
* for a-priori security. */
if (iovec_is_set(&verity->root_hash)) /* Already loaded? */
return 0;
r = secure_getenv_bool("SYSTEMD_DISSECT_VERITY_GUESS");
if (r < 0 && r != -ENXIO)
log_debug_errno(r, "Failed to parse $SYSTEMD_DISSECT_VERITY_GUESS, ignoring: %m");
if (r == 0)
return 0;
PartitionDesignator dd = verity->designator;
if (dd < 0) {
if (m->partitions[PARTITION_ROOT_VERITY].found)
dd = PARTITION_ROOT;
else if (m->partitions[PARTITION_USR_VERITY].found)
dd = PARTITION_USR;
else
return 0;
}
DissectedPartition *d = m->partitions + dd;
if (!d->found)
return 0;
PartitionDesignator dv = partition_verity_hash_of(dd);
assert(dv >= 0);
DissectedPartition *p = m->partitions + dv;
if (!p->found)
return 0;
_cleanup_free_ void *rh = malloc(sizeof(sd_id128_t) * 2);
if (!rh)
return log_oom_debug();
memcpy(mempcpy(rh, &d->uuid, sizeof(sd_id128_t)), &p->uuid, sizeof(sd_id128_t));
verity->root_hash = IOVEC_MAKE(TAKE_PTR(rh), sizeof(sd_id128_t) * 2);
verity->designator = dd;
m->verity_ready = true;
m->partitions[dd].rw = false;
return 0;
}
int dissected_image_acquire_metadata(
DissectedImage *m,
int userns_fd,
DissectImageFlags extra_flags) {
enum {
META_HOSTNAME,
META_MACHINE_ID,
META_MACHINE_INFO,
META_OS_RELEASE,
META_INITRD_RELEASE,
META_SYSEXT_RELEASE,
META_CONFEXT_RELEASE,
META_HAS_INIT_SYSTEM,
_META_MAX,
};
static const char *const paths[_META_MAX] = {
[META_HOSTNAME] = "/etc/hostname\0",
[META_MACHINE_ID] = "/etc/machine-id\0",
[META_MACHINE_INFO] = "/etc/machine-info\0",
[META_OS_RELEASE] = "/etc/os-release\0"
"/usr/lib/os-release\0",
[META_INITRD_RELEASE] = "/etc/initrd-release\0"
"/usr/lib/initrd-release\0",
[META_SYSEXT_RELEASE] = "sysext-release\0", /* String used only for logging. */
[META_CONFEXT_RELEASE] = "confext-release\0", /* ditto */
[META_HAS_INIT_SYSTEM] = "has-init-system\0", /* ditto */
};
_cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **initrd_release = NULL, **sysext_release = NULL, **confext_release = NULL;
_cleanup_free_ char *hostname = NULL, *t = NULL;
_cleanup_close_pair_ int error_pipe[2] = EBADF_PAIR;
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
sd_id128_t machine_id = SD_ID128_NULL;
unsigned n_meta_initialized = 0;
int fds[2 * _META_MAX], r, v;
int has_init_system = -1;
ssize_t n;
assert(m);
r = dlopen_libmount(LOG_DEBUG);
if (r < 0)
return r;
for (; n_meta_initialized < _META_MAX; n_meta_initialized++) {
assert(paths[n_meta_initialized]);
if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) {
r = -errno;
goto finish;
}
}
r = get_common_dissect_directory(&t);
if (r < 0)
goto finish;
if (pipe2(error_pipe, O_CLOEXEC) < 0) {
r = -errno;
goto finish;
}
r = pidref_safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &child);
if (r < 0)
goto finish;
if (r == 0) {
/* Child */
error_pipe[0] = safe_close(error_pipe[0]);
if (userns_fd < 0)
r = detach_mount_namespace_harder(0, 0);
else
r = detach_mount_namespace_userns(userns_fd);
if (r < 0) {
log_debug_errno(r, "Failed to detach mount namespace: %m");
report_errno_and_exit(error_pipe[1], r);
}
r = dissected_image_mount(
m,
t,
/* uid_shift= */ UID_INVALID,
/* uid_range= */ UID_INVALID,
/* userns_fd= */ -EBADF,
extra_flags |
DISSECT_IMAGE_READ_ONLY |
DISSECT_IMAGE_MOUNT_ROOT_ONLY |
DISSECT_IMAGE_USR_NO_ROOT);
if (r < 0) {
log_debug_errno(r, "Failed to mount dissected image: %m");
report_errno_and_exit(error_pipe[1], r);
}
for (unsigned k = 0; k < _META_MAX; k++) {
_cleanup_close_ int fd = -ENOENT;
assert(paths[k]);
fds[2*k] = safe_close(fds[2*k]);
switch (k) {
case META_SYSEXT_RELEASE:
if (!m->image_name)
goto next;
/* As per the os-release spec, if the image is an extension it will have a
* file named after the image name in extension-release.d/ - we use the image
* name and try to resolve it with the extension-release helpers, as
* sometimes the image names are mangled on deployment and do not match
* anymore. Unlike other paths this is not fixed, and the image name can be
* mangled on deployment, so by calling into the helper we allow a fallback
* that matches on the first extension-release file found in the directory,
* if one named after the image cannot be found first. */
r = open_extension_release(
t,
IMAGE_SYSEXT,
m->image_name,
/* relax_extension_release_check= */ false,
/* ret_path= */ NULL,
&fd);
if (r < 0)
fd = r;
break;
case META_CONFEXT_RELEASE:
if (!m->image_name)
goto next;
/* As above */
r = open_extension_release(
t,
IMAGE_CONFEXT,
m->image_name,
/* relax_extension_release_check= */ false,
/* ret_path= */ NULL,
&fd);
if (r < 0)
fd = r;
break;
case META_HAS_INIT_SYSTEM: {
bool found = false;
FOREACH_STRING(init,
"/usr/lib/systemd/systemd", /* systemd on /usr/ merged system */
"/lib/systemd/systemd", /* systemd on /usr/ non-merged systems */
"/sbin/init") { /* traditional path the Linux kernel invokes */
r = chase(init, t, CHASE_PREFIX_ROOT, NULL, NULL);
if (r < 0) {
if (r != -ENOENT)
log_debug_errno(r, "Failed to resolve %s, ignoring: %m", init);
} else {
found = true;
break;
}
}
r = loop_write(fds[2*k+1], &found, sizeof(found));
if (r < 0)
report_errno_and_exit(error_pipe[1], r);
goto next;
}
default:
NULSTR_FOREACH(p, paths[k]) {
fd = chase_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
if (fd >= 0)
break;
}
}
if (fd < 0) {
log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]);
goto next;
}
r = copy_bytes(fd, fds[2*k+1], UINT64_MAX, 0);
if (r < 0)
report_errno_and_exit(error_pipe[1], r);
next:
fds[2*k+1] = safe_close(fds[2*k+1]);
}
_exit(EXIT_SUCCESS);
}
error_pipe[1] = safe_close(error_pipe[1]);
for (unsigned k = 0; k < _META_MAX; k++) {
_cleanup_fclose_ FILE *f = NULL;
assert(paths[k]);
fds[2*k+1] = safe_close(fds[2*k+1]);
f = take_fdopen(&fds[2*k], "r");
if (!f) {
r = -errno;
goto finish;
}
switch (k) {
case META_HOSTNAME:
r = read_etc_hostname_stream(f, /* substitute_wildcards= */ false, &hostname);
if (r < 0)
log_debug_errno(r, "Failed to read /etc/hostname of image: %m");
break;
case META_MACHINE_ID: {
_cleanup_free_ char *line = NULL;
r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0)
log_debug_errno(r, "Failed to read /etc/machine-id of image: %m");
else if (r == 33) {
r = sd_id128_from_string(line, &machine_id);
if (r < 0)
log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line);
} else if (r == 0)
log_debug("/etc/machine-id file of image is empty.");
else if (streq(line, "uninitialized"))
log_debug("/etc/machine-id file of image is uninitialized (likely aborted first boot).");
else
log_debug("/etc/machine-id file of image has unexpected length %i.", r);
break;
}
case META_MACHINE_INFO:
r = load_env_file_pairs(f, "machine-info", &machine_info);
if (r < 0)
log_debug_errno(r, "Failed to read /etc/machine-info of image: %m");
break;
case META_OS_RELEASE:
r = load_env_file_pairs(f, "os-release", &os_release);
if (r < 0)
log_debug_errno(r, "Failed to read OS release file of image: %m");
break;
case META_INITRD_RELEASE:
r = load_env_file_pairs(f, "initrd-release", &initrd_release);
if (r < 0)
log_debug_errno(r, "Failed to read initrd release file of image: %m");
break;
case META_SYSEXT_RELEASE:
r = load_env_file_pairs(f, "sysext-release", &sysext_release);
if (r < 0)
log_debug_errno(r, "Failed to read sysext release file of image: %m");
break;
case META_CONFEXT_RELEASE:
r = load_env_file_pairs(f, "confext-release", &confext_release);
if (r < 0)
log_debug_errno(r, "Failed to read confext release file of image: %m");
break;
case META_HAS_INIT_SYSTEM: {
bool b = false;
size_t nr;
errno = 0;
nr = fread(&b, 1, sizeof(b), f);
if (nr != sizeof(b))
log_debug_errno(errno_or_else(EIO), "Failed to read has-init-system boolean: %m");
else
has_init_system = b;
break;
}}
}
r = pidref_wait_for_terminate_and_check("(sd-dissect)", &child, 0);
if (r < 0)
goto finish;
pidref_done(&child);
n = read(error_pipe[0], &v, sizeof(v));
if (n < 0) {
r = -errno;
goto finish;
}
if (n == sizeof(v)) {
r = v; /* propagate error sent to us from child */
goto finish;
}
if (n != 0) {
r = -EIO;
goto finish;
}
if (r != EXIT_SUCCESS) {
r = -EPROTO;
goto finish;
}
free_and_replace(m->hostname, hostname);
m->machine_id = machine_id;
strv_free_and_replace(m->machine_info, machine_info);
strv_free_and_replace(m->os_release, os_release);
strv_free_and_replace(m->initrd_release, initrd_release);
strv_free_and_replace(m->sysext_release, sysext_release);
strv_free_and_replace(m->confext_release, confext_release);
m->has_init_system = has_init_system;
finish:
for (unsigned k = 0; k < n_meta_initialized; k++)
safe_close_pair(fds + 2*k);
return r;
}
Architecture dissected_image_architecture(DissectedImage *m) {
assert(m);
if (m->partitions[PARTITION_ROOT].found &&
m->partitions[PARTITION_ROOT].architecture >= 0)
return m->partitions[PARTITION_ROOT].architecture;
if (m->partitions[PARTITION_USR].found &&
m->partitions[PARTITION_USR].architecture >= 0)
return m->partitions[PARTITION_USR].architecture;
return _ARCHITECTURE_INVALID;
}
bool dissected_image_is_portable(DissectedImage *m) {
return m && (strv_env_pairs_get(m->os_release, "PORTABLE_PREFIXES") || strv_env_pairs_get(m->os_release, "PORTABLE_SCOPE"));
}
bool dissected_image_is_initrd(DissectedImage *m) {
return m && !strv_isempty(m->initrd_release);
}
int dissect_loop_device(
LoopDevice *loop,
const VeritySettings *verity,
const MountOptions *mount_options,
const ImagePolicy *image_policy,
const ImageFilter *image_filter,
DissectImageFlags flags,
DissectedImage **ret) {
#if HAVE_BLKID
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
int r;
assert(loop);
r = dissected_image_new(loop->backing_file ?: loop->node, &m);
if (r < 0)
return r;
m->loop = loop_device_ref(loop);
m->image_size = m->loop->device_size;
m->sector_size = m->loop->sector_size;
r = dissect_image(
m,
loop->fd,
loop->node,
verity,
mount_options,
image_policy,
image_filter,
flags);
if (r < 0)
return r;
if (ret)
*ret = TAKE_PTR(m);
return 0;
#else
return -EOPNOTSUPP;
#endif
}
int dissect_loop_device_and_warn(
LoopDevice *loop,
const VeritySettings *verity,
const MountOptions *mount_options,
const ImagePolicy *image_policy,
const ImageFilter *image_filter,
DissectImageFlags flags,
DissectedImage **ret) {
assert(loop);
return dissect_log_error(
LOG_ERR,
dissect_loop_device(loop, verity, mount_options, image_policy, image_filter, flags, ret),
loop->backing_file ?: loop->node,
verity);
}
bool dissected_image_verity_candidate(const DissectedImage *image, PartitionDesignator partition_designator) {
assert(image);
/* Checks if this partition could theoretically do Verity. For non-partitioned images this only works
* if there's an external verity file supplied, for which we can consult .has_verity. For partitioned
* images we only check the partition type.
*
* This call is used to decide whether to suppress or show a verity column in tabular output of the
* image. */
if (image->single_file_system)
return partition_designator == PARTITION_ROOT && image->has_verity;
return partition_verity_hash_of(partition_designator) >= 0;
}
bool dissected_image_verity_ready(const DissectedImage *image, PartitionDesignator partition_designator) {
PartitionDesignator k;
assert(image);
/* Checks if this partition has verity data available that we can activate. For non-partitioned this
* works for the root partition, for others only if the associated verity partition was found. */
if (!image->verity_ready)
return false;
if (image->single_file_system)
return partition_designator == PARTITION_ROOT;
k = partition_verity_hash_of(partition_designator);
return k >= 0 && image->partitions[k].found;
}
bool dissected_image_verity_sig_ready(const DissectedImage *image, PartitionDesignator partition_designator) {
PartitionDesignator k;
assert(image);
/* Checks if this partition has verity signature data available that we can use. */
if (!image->verity_sig_ready)
return false;
if (image->single_file_system)
return partition_designator == PARTITION_ROOT;
k = partition_verity_sig_of(partition_designator);
return k >= 0 && image->partitions[k].found;
}
int mount_options_set_and_consume(MountOptions **options, PartitionDesignator d, char *s) {
assert(options);
assert(d >= 0);
if (!*options) {
*options = new0(MountOptions, 1);
if (!*options) {
free(s);
return log_oom();
}
}
return free_and_replace((*options)->options[d], s);
}
int mount_options_dup(const MountOptions *source, MountOptions **ret) {
_cleanup_(mount_options_free_allp) MountOptions *options = NULL;
assert(source);
assert(ret);
options = new0(MountOptions, 1);
if (!options)
return log_oom_debug();
for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++)
if (source->options[d]) {
options->options[d] = strdup(source->options[d]);
if (!options->options[d])
return log_oom_debug();
}
*ret = TAKE_PTR(options);
return 0;
}
int mount_options_to_string(const MountOptions *mount_options, char **ret) {
_cleanup_free_ char *s = NULL;
assert(mount_options);
assert(ret);
for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++)
if (!isempty(mount_options->options[i]))
if (!strextend_with_separator(&s, ":", "%s:%s",
partition_designator_to_string(i),
mount_options->options[i]))
return log_oom_debug();
*ret = TAKE_PTR(s);
return 0;
}
MountOptions* mount_options_free_all(MountOptions *options) {
if (!options)
return NULL;
free_many_charp(options->options, _PARTITION_DESIGNATOR_MAX);
return mfree(options);
}
const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator) {
assert(designator >= 0 && designator < _PARTITION_DESIGNATOR_MAX);
if (!options)
return NULL;
return options->options[designator];
}
int mount_image_privately_interactively(
const char *image,
const ImagePolicy *image_policy,
DissectImageFlags flags,
char **ret_directory,
int *ret_dir_fd,
LoopDevice **ret_loop_device) {
_cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT;
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
_cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
_cleanup_free_ char *dir = NULL;
int r;
/* Mounts an OS image at a temporary place, inside a newly created mount namespace of our own. This
* is used by tools such as systemd-tmpfiles or systemd-firstboot to operate on some disk image
* easily. */
assert(image);
assert(ret_loop_device);
/* We intend to mount this right-away, hence add the partitions if needed and pin them. */
flags |= DISSECT_IMAGE_ADD_PARTITION_DEVICES |
DISSECT_IMAGE_PIN_PARTITION_DEVICES;
r = verity_settings_load(&verity, image, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to load root hash data: %m");
r = loop_device_make_by_path(
image,
FLAGS_SET(flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1,
/* sector_size= */ UINT32_MAX,
FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN,
LOCK_SH,
&d);
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device for %s: %m", image);
r = dissect_loop_device_and_warn(
d,
&verity,
/* mount_options= */ NULL,
image_policy,
/* image_filter= */ NULL,
flags,
&dissected_image);
if (r < 0)
return r;
r = dissected_image_load_verity_sig_partition(dissected_image, d->fd, &verity);
if (r < 0)
return r;
r = dissected_image_guess_verity_roothash(dissected_image, &verity);
if (r < 0)
return r;
r = dissected_image_decrypt_interactively(dissected_image, NULL, &verity, image_policy, flags);
if (r < 0)
return r;
r = detach_mount_namespace();
if (r < 0)
return log_error_errno(r, "Failed to detach mount namespace: %m");
r = mkdir_p("/run/systemd/mount-rootfs", 0555);
if (r < 0)
return log_error_errno(r, "Failed to create mount point: %m");
r = dissected_image_mount_and_warn(
dissected_image,
"/run/systemd/mount-rootfs",
/* uid_shift= */ UID_INVALID,
/* uid_range= */ UID_INVALID,
/* userns_fd= */ -EBADF,
flags);
if (r < 0)
return r;
r = loop_device_flock(d, LOCK_UN);
if (r < 0)
return r;
r = dissected_image_relinquish(dissected_image);
if (r < 0)
return log_error_errno(r, "Failed to relinquish DM and loopback block devices: %m");
if (ret_directory) {
dir = strdup("/run/systemd/mount-rootfs");
if (!dir)
return log_oom();
}
if (ret_dir_fd) {
_cleanup_close_ int dir_fd = -EBADF;
dir_fd = open("/run/systemd/mount-rootfs", O_CLOEXEC|O_DIRECTORY);
if (dir_fd < 0)
return log_error_errno(errno, "Failed to open mount point directory: %m");
*ret_dir_fd = TAKE_FD(dir_fd);
}
if (ret_directory)
*ret_directory = TAKE_PTR(dir);
*ret_loop_device = TAKE_PTR(d);
return 0;
}
static bool mount_options_relax_extension_release_checks(const MountOptions *options) {
if (!options)
return false;
return string_contains_word(mount_options_from_designator(options, PARTITION_ROOT), ",", "x-systemd.relax-extension-release-check") ||
string_contains_word(mount_options_from_designator(options, PARTITION_USR), ",", "x-systemd.relax-extension-release-check");
}
int verity_dissect_and_mount(
int src_fd,
const char *src,
const char *dest,
const MountOptions *options,
const ImagePolicy *image_policy,
const ImageFilter *image_filter,
const ExtensionReleaseData *extension_release_data,
ImageClass required_class,
VeritySettings *verity,
RuntimeScope runtime_scope,
DissectedImage **ret_image) {
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
_cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
_cleanup_(verity_settings_done) VeritySettings local_verity = VERITY_SETTINGS_DEFAULT;
_cleanup_close_ int userns_fd = -EBADF;
DissectImageFlags dissect_image_flags;
bool relax_extension_release_check;
int r;
assert(src);
/* Verifying release metadata requires mounted image for now, so ensure the check is skipped when
* opening an image without mounting it immediately (i.e.: 'dest' is NULL). */
assert(!extension_release_data || dest);
relax_extension_release_check = mount_options_relax_extension_release_checks(options);
/* We might get an FD for the image, but we use the original path to look for the dm-verity files.
* The caller might also give us a pre-loaded VeritySettings, in which case we just use it. It will
* also be extended, as dissected_image_load_verity_sig_partition() is invoked. */
if (!verity) {
r = verity_settings_load(&local_verity, src, NULL, NULL);
if (r < 0)
return log_debug_errno(r, "Failed to load root hash: %m");
verity = &local_verity;
}
dissect_image_flags =
(verity->data_path ? DISSECT_IMAGE_NO_PARTITION_TABLE : 0) |
(relax_extension_release_check ? DISSECT_IMAGE_RELAX_EXTENSION_CHECK : 0) |
DISSECT_IMAGE_ADD_PARTITION_DEVICES |
DISSECT_IMAGE_PIN_PARTITION_DEVICES |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY |
DISSECT_IMAGE_VERITY_SHARE;
/* First check if we have a verity device already open and with a fstype pinned by policy. If it
* cannot be found, then fallback to the slow path (full dissect). */
r = dissected_image_new_from_existing_verity(
src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src,
verity,
options,
image_policy,
image_filter,
runtime_scope,
dissect_image_flags,
&dissected_image);
if (r < 0 && !ERRNO_IS_NEG_DEVICE_ABSENT(r) && r != -ENOPKG)
return r;
if (r >= 0)
log_debug("Reusing pre-existing verity-protected image %s", src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src);
else {
if (runtime_scope == RUNTIME_SCOPE_SYSTEM) {
/* Note that we don't use loop_device_make here, as the FD is most likely O_PATH which would not be
* accepted by LOOP_CONFIGURE, so just let loop_device_make_by_path reopen it as a regular FD. */
r = loop_device_make_by_path(
src_fd >= 0 ? FORMAT_PROC_FD_PATH(src_fd) : src,
/* open_flags= */ -1,
/* sector_size= */ UINT32_MAX,
verity->data_path ? 0 : LO_FLAGS_PARTSCAN,
LOCK_SH,
&loop_device);
if (r < 0)
return log_debug_errno(r, "Failed to create loop device for image: %m");
r = dissect_loop_device(
loop_device,
verity,
options,
image_policy,
image_filter,
dissect_image_flags,
&dissected_image);
/* No partition table? Might be a single-filesystem image, try again */
if (!verity->data_path && r == -ENOPKG)
r = dissect_loop_device(
loop_device,
verity,
options,
image_policy,
image_filter,
dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE,
&dissected_image);
if (r < 0)
return log_debug_errno(r, "Failed to dissect image: %m");