diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2022-10-10 23:34:04 +0200 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2022-11-15 20:23:51 +0100 |
commit | e59678b2cf42e4206ddabc959d3cf9a5a865ecdc (patch) | |
tree | 642586af7da65432b6d4398a8187e411e477e2b6 /src/shared/mkfs-util.c | |
parent | repart: Fix copy failure error message (diff) | |
download | systemd-e59678b2cf42e4206ddabc959d3cf9a5a865ecdc.tar.xz systemd-e59678b2cf42e4206ddabc959d3cf9a5a865ecdc.zip |
repart: Ensure files end up owned by root in generated filesystems
By forking off a user namespace before running mkfs and ID mapping
the user running repart to root in the user namespace, we can make
sure that files in the generated filesystems are all owned by root
instead of the user running repart.
To make this work we have to make sure that all the files in the
root directory that's passed to the mkfs binary are owned by the
user running repart, so we have to drop the shortcut for only a
single root directory in partition_populate_directory().
Diffstat (limited to 'src/shared/mkfs-util.c')
-rw-r--r-- | src/shared/mkfs-util.c | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index 2adb2a25c7..b1c8fe7bc4 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -4,6 +4,7 @@ #include "dirent-util.h" #include "fd-util.h" +#include "fileio.h" #include "id128-util.h" #include "mkfs-util.h" #include "mountpoint-util.h" @@ -94,9 +95,39 @@ static int mangle_fat_label(const char *s, char **ret) { return 0; } +static int setup_userns(uid_t uid, gid_t gid) { + int r; + + /* mkfs programs tend to keep ownership intact when bootstrapping themselves from a root directory. + * However, we'd like for the files to be owned by root instead, so we fork off a user namespace and + * inside of it, map the uid/gid of the root directory to root in the user namespace. mkfs programs + * will pick up on this and the files will be owned by root in the generated filesystem. */ + + r = write_string_filef("/proc/self/uid_map", WRITE_STRING_FILE_DISABLE_BUFFER, + UID_FMT " " UID_FMT " " UID_FMT, 0u, uid, 1u); + if (r < 0) + return log_error_errno(r, + "Failed to write mapping for "UID_FMT" to /proc/self/uid_map: %m", + uid); + + r = write_string_file("/proc/self/setgroups", "deny", WRITE_STRING_FILE_DISABLE_BUFFER); + if (r < 0) + return log_error_errno(r, "Failed to write 'deny' to /proc/self/setgroups: %m"); + + r = write_string_filef("/proc/self/gid_map", WRITE_STRING_FILE_DISABLE_BUFFER, + UID_FMT " " UID_FMT " " UID_FMT, 0u, gid, 1u); + if (r < 0) + return log_error_errno(r, + "Failed to write mapping for "UID_FMT" to /proc/self/gid_map: %m", + gid); + + return 0; +} + static int do_mcopy(const char *node, const char *root) { _cleanup_strv_free_ char **argv = NULL; _cleanup_closedir_ DIR *rootdir = NULL; + struct stat st; int r; assert(node); @@ -131,10 +162,17 @@ static int do_mcopy(const char *node, const char *root) { if (r < 0) return log_oom(); + if (stat(root, &st) < 0) + return log_error_errno(errno, "Failed to stat '%s': %m", root); + r = safe_fork("(mcopy)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_NEW_USERNS, NULL); if (r < 0) return r; if (r == 0) { + r = setup_userns(st.st_uid, st.st_gid); + if (r < 0) + _exit(EXIT_FAILURE); + /* Avoid failures caused by mismatch in expectations between mkfs.vfat and mcopy by disabling * the stricter mcopy checks using MTOOLS_SKIP_CHECK. */ execvpe("mcopy", argv, STRV_MAKE("MTOOLS_SKIP_CHECK=1")); @@ -158,6 +196,7 @@ int make_filesystem( _cleanup_free_ char *mkfs = NULL, *mangled_label = NULL; _cleanup_strv_free_ char **argv = NULL; char vol_id[CONST_MAX(SD_ID128_UUID_STRING_MAX, 8U + 1U)] = {}; + struct stat st; int r; assert(node); @@ -347,12 +386,21 @@ int make_filesystem( if (!argv) return log_oom(); - r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR, NULL); + if (root && stat(root, &st) < 0) + return log_error_errno(errno, "Failed to stat %s: %m", root); + + r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|(root ? FORK_NEW_USERNS : 0), NULL); if (r < 0) return r; if (r == 0) { /* Child */ + if (root) { + r = setup_userns(st.st_uid, st.st_gid); + if (r < 0) + _exit(EXIT_FAILURE); + } + execvp(mkfs, argv); log_error_errno(errno, "Failed to execute %s: %m", mkfs); |