From 56c6d90f8cdb392b1f2a1f180249d7b8fcb9c449 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 9 Jul 2025 10:07:07 +0200 Subject: [PATCH] mount-util: teach open_tree_attr_fallback() our usual AT_EMPTY_PATH trick While at it, rename it to _with_fallback following the naming scheme we use elsewhere. --- src/nspawn/nspawn-mount.c | 2 +- src/shared/mount-util.c | 16 ++++++++++++---- src/shared/mount-util.h | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index be1c01caf9f..af794b00172 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -824,7 +824,7 @@ static int mount_bind(const char *dest, CustomMount *m, uid_t uid_shift, uid_t u * caller's userns *without* any mount idmapping in place. To get that uid, we clone the * mount source tree and clear any existing idmapping and temporarily mount that tree over * the mount source before we stat the mount source to figure out the source uid. */ - _cleanup_close_ int fd_clone = open_tree_attr_fallback( + _cleanup_close_ int fd_clone = open_tree_attr_with_fallback( AT_FDCWD, m->source, OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC, diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index a0db226c259..3d10a050919 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -1441,10 +1441,18 @@ int make_userns(uid_t uid_shift, return TAKE_FD(userns_fd); } -int open_tree_attr_fallback(int dir_fd, const char *path, unsigned int flags, struct mount_attr *attr) { +int open_tree_attr_with_fallback(int dir_fd, const char *path, unsigned int flags, struct mount_attr *attr) { + _cleanup_close_ int fd = -EBADF; + + assert(dir_fd >= 0 || dir_fd == AT_FDCWD); assert(attr); - _cleanup_close_ int fd = open_tree_attr(dir_fd, path, flags, attr, sizeof(struct mount_attr)); + if (isempty(path)) { + path = ""; + flags |= AT_EMPTY_PATH; + } + + fd = open_tree_attr(dir_fd, path, flags, attr, sizeof(struct mount_attr)); if (fd >= 0) return TAKE_FD(fd); if (!ERRNO_IS_NOT_SUPPORTED(errno)) @@ -1492,8 +1500,8 @@ int remount_idmap_fd( for (size_t i = 0; i < n; i++) { /* Clone the mount point and et the user namespace mapping attribute on the cloned mount point. */ - mount_fds[n_mounts_fds] = open_tree_attr_fallback( - /* dir_fd= */ -EBADF, + mount_fds[n_mounts_fds] = open_tree_attr_with_fallback( + AT_FDCWD, paths[i], OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC, &(struct mount_attr) { diff --git a/src/shared/mount-util.h b/src/shared/mount-util.h index 3c1fe9dc1ef..3140762498f 100644 --- a/src/shared/mount-util.h +++ b/src/shared/mount-util.h @@ -150,7 +150,7 @@ typedef enum RemountIdmapping { _REMOUNT_IDMAPPING_INVALID = -EINVAL, } RemountIdmapping; -int open_tree_attr_fallback(int dir_fd, const char *path, unsigned int flags, struct mount_attr *attr); +int open_tree_attr_with_fallback(int dir_fd, const char *path, unsigned int flags, struct mount_attr *attr); int make_userns(uid_t uid_shift, uid_t uid_range, uid_t host_owner, uid_t dest_owner, RemountIdmapping idmapping); int remount_idmap_fd(char **p, int userns_fd, uint64_t extra_mount_attr_set); -- 2.47.3