HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/mstack.c
Git blob: c8efecca236cb3a76981ffa6792d2503b0afb3fe
Lines 1001–1200 of 1,200
int r;
assert(mstack);
_cleanup_close_ int _where_fd = -EBADF;
if (where_fd == AT_FDCWD) {
_where_fd = open(".", O_CLOEXEC|O_PATH|O_DIRECTORY);
if (_where_fd < 0)
return log_debug_errno(errno, "Failed to open current working directory: %m");
where_fd = _where_fd;
} else if (where_fd < 0) {
r = chase(where,
/* root= */ NULL,
(FLAGS_SET(flags, MSTACK_MKDIR) ? CHASE_MKDIR_0755 : 0)|CHASE_MUST_BE_DIRECTORY,
/* ret_path= */ NULL,
&_where_fd);
if (r < 0)
return log_debug_errno(r, "Failed to open '%s': %m", where);
where_fd = _where_fd;
}
assert(mstack->root_mount_fd >= 0);
if (move_mount(mstack->root_mount_fd, "", where_fd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH) < 0)
return log_debug_errno(errno, "Failed to attach mstack root mount to '%s': %m", where);
log_debug("Attached mstack root mount to '%s'.", where);
_cleanup_close_ int root_fd = open(where, O_CLOEXEC|O_PATH|O_DIRECTORY|O_NOFOLLOW);
if (root_fd < 0)
return log_debug_errno(errno, "Failed to mount root mount '%s': %m", where);
if (mstack->usr_mount_fd >= 0) {
_cleanup_close_ int subdir_fd = -EBADF;
r = chaseat(root_fd, root_fd, "usr", CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, &subdir_fd);
if (r < 0)
return log_debug_errno(r, "Failed to open mount point inode '%s': %m", where);
if (move_mount(mstack->usr_mount_fd, "", subdir_fd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH) < 0)
return log_debug_errno(errno, "Failed to attach bind mount to '/usr/' subdir: %m");
log_debug("Attached mstack '/usr/' mount to '%s/usr/'.", where);
}
FOREACH_ARRAY(m, mstack->mounts, mstack->n_mounts) {
if (!IN_SET(m->mount_type, MSTACK_BIND, MSTACK_ROBIND) ||
m == mstack->root_mount)
continue;
assert(m->mount_fd >= 0);
_cleanup_close_ int subdir_fd = -EBADF;
r = chaseat(root_fd, root_fd, m->where, CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, &subdir_fd);
if (r < 0)
return log_debug_errno(r, "Failed to open mount point inode '%s': %m", m->where);
if (move_mount(m->mount_fd, "", subdir_fd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH) < 0)
return log_debug_errno(errno, "Failed to attach bind mount to '%s' subdir: %m", m->where);
log_debug("Attached mstack '%s/' mount to '%s/%s/'.", m->where, where, m->where);
}
/* If we have a tmpfs root, the above might have created mount point inodes. Hence we left the tmpfs
* writable for that. Let's fix that now. Also, let's enable propagation for the future. (Reminder:
* we disconnect propagation from the host, but we *want* propagation by default for everything
* created further down the tree. Hence we'll set MS_SHARED here right-away.) */
if (mount_setattr(root_fd, "", AT_EMPTY_PATH|AT_RECURSIVE,
&(struct mount_attr) {
.attr_set = FLAGS_SET(flags, MSTACK_RDONLY) ? MOUNT_ATTR_RDONLY : 0,
.attr_clr = FLAGS_SET(flags, MSTACK_RDONLY) ? 0 : MOUNT_ATTR_RDONLY,
.propagation = MS_SHARED,
}, sizeof(struct mount_attr)) < 0)
return log_debug_errno(errno, "Failed to mark root bind mount read-only: %m");
if (ret_root_fd)
*ret_root_fd = TAKE_FD(root_fd);
return 0;
}
int mstack_apply(
const char *dir,
int dir_fd,
const char *where,
const char *temp_mount_dir,
sd_varlink *link,
int userns_fd,
const ImagePolicy *image_policy,
const ImageFilter *image_filter,
MStackFlags flags,
int *ret_root_fd) {
int r;
assert(where);
_cleanup_(mstack_done) MStack mstack = MSTACK_INIT;
r = mstack_load_now(&mstack, dir, dir_fd, flags);
if (r < 0)
return r;
r = mstack_open_images(&mstack, link, userns_fd, image_policy, image_filter, flags);
if (r < 0)
return r;
_cleanup_(rmdir_and_freep) char *t = NULL;
if (!temp_mount_dir) {
r = mkdtemp_malloc("/tmp/mstack-temporary-XXXXXX", &t);
if (r < 0)
return r;
temp_mount_dir = t;
}
r = mstack_make_mounts(&mstack, temp_mount_dir, flags);
if (r < 0)
return r;
return mstack_bind_mounts(&mstack, where, /* where_fd= */ -EBADF, flags, ret_root_fd);
}
int mstack_load(const char *dir, int dir_fd, MStack **ret) {
int r;
assert(ret);
/* Well-known errors:
*
* -ENOTUNIQ → Multiple conflicting layers for the same path defined
* -EBADMSG → Bad file suffix, inode type for layer, or unrecognized entry
*/
_cleanup_(mstack_freep) MStack *mstack = new(MStack, 1);
if (!mstack)
return -ENOMEM;
*mstack = MSTACK_INIT;
r = mstack_load_now(mstack, dir, dir_fd, /* flags= */ 0);
if (r < 0)
return r;
*ret = TAKE_PTR(mstack);
return 0;
}
int mstack_is_read_only(MStack *mstack) {
assert(mstack);
/* Checks if the mstack consists of only read-only layers and bind mounts */
if (mstack->has_tmpfs_root)
return false;
FOREACH_ARRAY(m, mstack->mounts, mstack->n_mounts)
if (IN_SET(m->mount_type, MSTACK_ROOT, MSTACK_RW, MSTACK_BIND))
return false;
return true;
}
int mstack_is_foreign_uid_owned(MStack *mstack) {
int r;
assert(mstack);
/* Checks if any of the layers are owned by the host's foreign UID range */
FOREACH_ARRAY(m, mstack->mounts, mstack->n_mounts) {
if (!IN_SET(m->image_type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME))
continue;
assert(m->what_fd >= 0);
struct stat st;
if (fstat(m->what_fd, &st) < 0)
return -errno;
r = stat_verify_directory(&st);
if (r < 0)
return r;
if (uid_is_foreign(st.st_uid))
return true;
}
return false;
}
static const char *const mstack_mount_type_table[] = {
[MSTACK_ROOT] = "root",
[MSTACK_LAYER] = "layer",
[MSTACK_RW] = "rw",
[MSTACK_BIND] = "bind",
[MSTACK_ROBIND] = "robind",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(mstack_mount_type, MStackMountType);