diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-12-04 18:15:41 +0100 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2024-01-18 02:30:10 +0100 |
commit | 2d1e7d19377b5addbbf59c30e357f72bc447ec65 (patch) | |
tree | 181b57248524d438ebb95e261a16b65a09d42f3f /src | |
parent | Merge pull request #30990 from poettering/more-mime (diff) | |
download | systemd-2d1e7d19377b5addbbf59c30e357f72bc447ec65.tar.xz systemd-2d1e7d19377b5addbbf59c30e357f72bc447ec65.zip |
dissect-image: introduce new get_common_dissect_directory() helper
So far, if some component mounts a DDI in some local mount namespace we
created a temporary mountpoint in /tmp/ for that. Let's instead use the
same directory inode in /run/ instead. This is safe, since if everything
runs in a local mount namespace (with propagation on /run/ off) then
they shouldn't fight for the inode. And it relieves us from having to
clean up the directory after use. Morever, it allows us to run without
/tmp/ mounted.
This only moves dissect-image.c and the dissec tool over. More stuff is
moved over later.
Diffstat (limited to 'src')
-rw-r--r-- | src/dissect/dissect.c | 17 | ||||
-rw-r--r-- | src/shared/dissect-image.c | 31 | ||||
-rw-r--r-- | src/shared/dissect-image.h | 2 |
3 files changed, 35 insertions, 15 deletions
diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 2b080257fe..dce0209140 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -1273,8 +1273,7 @@ static int mtree_print_item( static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) { _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL; - _cleanup_(rmdir_and_freep) char *created_dir = NULL; - _cleanup_free_ char *temp = NULL; + _cleanup_free_ char *t = NULL; const char *root; int r; @@ -1288,19 +1287,13 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) { if (r < 0) return log_error_errno(r, "Failed to detach mount namespace: %m"); - r = tempfn_random_child(NULL, program_invocation_short_name, &temp); - if (r < 0) - return log_error_errno(r, "Failed to generate temporary mount directory: %m"); - - r = mkdir_p(temp, 0700); + r = get_common_dissect_directory(&t); if (r < 0) - return log_error_errno(r, "Failed to create mount point: %m"); - - created_dir = TAKE_PTR(temp); + return log_error_errno(r, "Failed generate private mount directory: %m"); r = dissected_image_mount_and_warn( m, - created_dir, + t, /* uid_shift= */ UID_INVALID, /* uid_range= */ UID_INVALID, /* userns_fd= */ -EBADF, @@ -1308,7 +1301,7 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) { if (r < 0) return r; - mounted_dir = TAKE_PTR(created_dir); + mounted_dir = TAKE_PTR(t); r = loop_device_flock(d, LOCK_UN); if (r < 0) diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index e5e47e4ac6..5b664d2333 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -3382,11 +3382,10 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_ }; _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_(rmdir_and_freep) char *t = NULL; _cleanup_(sigkill_waitp) pid_t child = 0; sd_id128_t machine_id = SD_ID128_NULL; - _cleanup_free_ char *hostname = NULL; unsigned n_meta_initialized = 0; int fds[2 * _META_MAX], r, v; int has_init_system = -1; @@ -3405,7 +3404,7 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_ } } - r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t); + r = get_common_dissect_directory(&t); if (r < 0) goto finish; @@ -4070,3 +4069,29 @@ int verity_dissect_and_mount( return 0; } + +int get_common_dissect_directory(char **ret) { + _cleanup_free_ char *t = NULL; + int r; + + /* A common location we mount dissected images to. The assumption is that everyone who uses this + * function runs in their own private mount namespace (with mount propagation off on /run/systemd/, + * and thus can mount something here without affecting anyone else). */ + + t = strdup("/run/systemd/dissect-root"); + if (!t) + return log_oom_debug(); + + r = mkdir_parents(t, 0755); + if (r < 0) + return log_debug_errno(r, "Failed to create parent dirs of mount point '%s': %m", t); + + r = RET_NERRNO(mkdir(t, 0000)); /* It's supposed to be overmounted, hence let's make this inaccessible */ + if (r < 0 && r != -EEXIST) + return log_debug_errno(r, "Failed to create mount point '%s': %m", t); + + if (ret) + *ret = TAKE_PTR(t); + + return 0; +} diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h index ed02049ed0..2366a38397 100644 --- a/src/shared/dissect-image.h +++ b/src/shared/dissect-image.h @@ -229,3 +229,5 @@ static inline const char *dissected_partition_fstype(const DissectedPartition *m return m->decrypted_node ? m->decrypted_fstype : m->fstype; } + +int get_common_dissect_directory(char **ret); |