HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/mount-util.c
Git blob: c8f0fd53d4dbb8a72edef75f564df7d9d77bbaae
Lines 2001–2291 of 2,291
return MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_NOSYMFOLLOW|(ro ? MS_RDONLY : 0);
}
int fsmount_credentials_fs(int *ret_fsfd) {
_cleanup_close_ int fs_fd = -EBADF;
char size_str[DECIMAL_STR_MAX(uint64_t)];
/* Mounts a file system we can place credentials in, i.e. with tight access modes right from the
* beginning, and ideally swapping turned off. In order of preference:
*
* 1. tmpfs if it supports "noswap" (needs kernel >= 6.3)
* 2. ramfs
* 3. tmpfs without "noswap"
*/
fs_fd = fsopen("tmpfs", FSOPEN_CLOEXEC);
if (fs_fd < 0)
return -errno;
if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "nr_inodes", "1024", 0) < 0)
return -errno;
xsprintf(size_str, "%" PRIu64, CREDENTIALS_TOTAL_SIZE_MAX);
if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "size", size_str, 0) < 0)
return -errno;
if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "noswap", NULL, 0) < 0) {
if (errno != EINVAL)
return -errno;
int ramfs_fd = fsopen("ramfs", FSOPEN_CLOEXEC);
if (ramfs_fd >= 0)
close_and_replace(fs_fd, ramfs_fd);
}
if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "mode", "0700", 0) < 0)
return -errno;
if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
return -errno;
unsigned mount_attrs = ms_flags_to_mount_attr(credentials_fs_mount_flags(/* ro = */ false));
int mfd = RET_NERRNO(fsmount(fs_fd, FSMOUNT_CLOEXEC, mount_attrs));
if (mfd == -EINVAL) {
/* MS_NOSYMFOLLOW was added in kernel 5.10, but the new mount API counterpart was missing
* until 5.14 (c.f. https://github.com/torvalds/linux/commit/dd8b477f9a3d8edb136207acb3652e1a34a661b7).
*
* TODO: drop this once our baseline is raised to 5.14 */
assert(FLAGS_SET(mount_attrs, MOUNT_ATTR_NOSYMFOLLOW));
mfd = RET_NERRNO(fsmount(fs_fd, FSMOUNT_CLOEXEC, mount_attrs & ~MOUNT_ATTR_NOSYMFOLLOW));
}
if (mfd < 0)
return mfd;
if (ret_fsfd)
*ret_fsfd = TAKE_FD(fs_fd);
return mfd;
}
int mount_credentials_fs(const char *path) {
_cleanup_close_ int mfd = -EBADF;
assert(path);
mfd = fsmount_credentials_fs(/* ret_fsfd= */ NULL);
if (mfd < 0)
return mfd;
return RET_NERRNO(move_mount(mfd, "", AT_FDCWD, path, MOVE_MOUNT_F_EMPTY_PATH));
}
int make_fsmount(
int error_log_level,
const char *what,
const char *type,
unsigned long flags,
const char *options,
int userns_fd) {
_cleanup_close_ int fs_fd = -EBADF, mnt_fd = -EBADF;
_cleanup_free_ char *o = NULL;
unsigned long f;
int r;
assert(type);
assert(what);
r = mount_option_mangle(options, flags, &f, &o);
if (r < 0)
return log_full_errno(
error_log_level, r, "Failed to mangle mount options %s: %m",
strempty(options));
if (DEBUG_LOGGING) {
_cleanup_free_ char *fl = NULL;
(void) mount_flags_to_string(f, &fl);
log_debug("Creating mount fd for %s (%s) (%s \"%s\")...",
strna(what), strna(type), strnull(fl), strempty(o));
}
fs_fd = fsopen(type, FSOPEN_CLOEXEC);
if (fs_fd < 0)
return log_full_errno(error_log_level, errno, "Failed to open superblock for \"%s\": %m", type);
if (fsconfig(fs_fd, FSCONFIG_SET_STRING, "source", what, 0) < 0)
return log_full_errno(error_log_level, errno, "Failed to set mount source for \"%s\" to \"%s\": %m", type, what);
if (FLAGS_SET(f, MS_RDONLY))
if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, "ro", NULL, 0) < 0)
return log_full_errno(error_log_level, errno, "Failed to set read only mount flag for \"%s\": %m", type);
for (const char *p = o;;) {
_cleanup_free_ char *word = NULL;
char *eq;
r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
if (r < 0)
return log_full_errno(error_log_level, r, "Failed to parse mount option string \"%s\": %m", strempty(o));
if (r == 0)
break;
eq = strchr(word, '=');
if (eq) {
*eq = 0;
eq++;
if (fsconfig(fs_fd, FSCONFIG_SET_STRING, word, eq, 0) < 0)
return log_full_errno(error_log_level, errno, "Failed to set mount option \"%s=%s\" for \"%s\": %m", word, eq, type);
} else {
if (fsconfig(fs_fd, FSCONFIG_SET_FLAG, word, NULL, 0) < 0)
return log_full_errno(error_log_level, errno, "Failed to set mount flag \"%s\" for \"%s\": %m", word, type);
}
}
if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0)
return log_full_errno(error_log_level, errno, "Failed to realize fs fd for \"%s\" (\"%s\"): %m", what, type);
mnt_fd = fsmount(fs_fd, FSMOUNT_CLOEXEC, 0);
if (mnt_fd < 0)
return log_full_errno(error_log_level, errno, "Failed to create mount fd for \"%s\" (\"%s\"): %m", what, type);
struct mount_attr ma = {
.attr_clr = ms_flags_to_mount_attr_clr(f),
.attr_set = ms_flags_to_mount_attr(f) | (userns_fd >= 0 ? MOUNT_ATTR_IDMAP : 0),
.userns_fd = userns_fd,
};
if (ma.attr_set != 0 && mount_setattr(mnt_fd, "", AT_EMPTY_PATH|AT_RECURSIVE, &ma, MOUNT_ATTR_SIZE_VER0) < 0)
return log_full_errno(error_log_level,
errno,
"Failed to set mount flags for \"%s\" (\"%s\"): %m",
what,
type);
return TAKE_FD(mnt_fd);
}
char* umount_and_rmdir_and_free(char *p) {
if (!p)
return NULL;
PROTECT_ERRNO;
(void) umount_recursive(p, 0);
(void) rmdir(p);
return mfree(p);
}
char* umount_and_free(char *p) {
if (!p)
return NULL;
PROTECT_ERRNO;
(void) umount_recursive(p, 0);
return mfree(p);
}
char* umount_and_unlink_and_free(char *p) {
if (!p)
return NULL;
PROTECT_ERRNO;
(void) umount2(p, 0);
(void) unlink(p);
return mfree(p);
}
int path_get_mount_info_at(
int dir_fd,
const char *path,
char **ret_fstype,
char **ret_options,
char **ret_source) {
#if HAVE_LIBMOUNT
_cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
int r, mnt_id;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
r = path_get_mnt_id_at(dir_fd, path, &mnt_id);
if (r < 0)
return log_debug_errno(r, "Failed to get mount ID: %m");
/* When getting options is requested, we also need to parse utab, otherwise userspace options like
* "_netdev" will be lost. */
if (ret_options)
r = libmount_parse_with_utab(&table, &iter);
else
r = libmount_parse_mountinfo(/* source= */ NULL, &table, &iter);
if (r < 0)
return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
for (;;) {
struct libmnt_fs *fs;
r = sym_mnt_table_next_fs(table, iter, &fs);
if (r == 1)
break; /* EOF */
if (r < 0)
return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
if (sym_mnt_fs_get_id(fs) != mnt_id)
continue;
_cleanup_free_ char *fstype = NULL, *options = NULL, *source = NULL;
if (ret_fstype) {
fstype = strdup(strempty(sym_mnt_fs_get_fstype(fs)));
if (!fstype)
return log_oom_debug();
}
if (ret_options) {
options = strdup(strempty(sym_mnt_fs_get_options(fs)));
if (!options)
return log_oom_debug();
}
if (ret_source) {
source = strdup(strempty(sym_mnt_fs_get_source(fs)));
if (!source)
return log_oom_debug();
}
if (ret_fstype)
*ret_fstype = TAKE_PTR(fstype);
if (ret_options)
*ret_options = TAKE_PTR(options);
if (ret_source)
*ret_source = TAKE_PTR(source);
return 0;
}
return log_debug_errno(SYNTHETIC_ERRNO(ESTALE), "Cannot find mount ID %i from /proc/self/mountinfo.", mnt_id);
#else
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
#endif
}
int path_is_network_fs_harder_at(int dir_fd, const char *path) {
_cleanup_close_ int fd = -EBADF;
int r;
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
fd = xopenat(dir_fd, path, O_PATH | O_CLOEXEC | O_NOFOLLOW);
if (fd < 0)
return fd;
r = fd_is_network_fs(fd);
if (r != 0)
return r;
_cleanup_free_ char *fstype = NULL, *options = NULL;
r = path_get_mount_info_at(fd, /* path= */ NULL, &fstype, &options, /* ret_source= */ NULL);
if (r < 0)
return r;
if (fstype_is_network(fstype))
return true;
if (fstab_test_option(options, "_netdev\0"))
return true;
return false;
}