mount: add OPEN_TREE_SKIP_MNTNS
RFC PATCH 1/1 mount: add OPEN_TREE_SKIP_MNTNS
crun, a container runtime, recently started using OPEN_TREE_NAMESPACE to set up the container's mount namespace, and open_tree(OPEN_TREE_CLONE) for the sources of its bind mounts. This turns out not to work on hosts that have a mount namespace pinned anywhere below such a source: the recursive clone picks the pinned namespace up, and move_mount(2) then refuses to attach the tree with ELOOP from check_for_nsfs_mounts(). This was reported to crun in [1].
The rule behind that goes back to commit 8823c079ba71 ("vfs: Add setns support for the mount namespace"), which requires "all bind mounts be of a younger mount namespace into an older mount namespace" so that mount namespace reference loops cannot form. OPEN_TREE_NAMESPACE ends up on the wrong side of it by construction: the namespace it creates is younger than everything else on the system, so from inside it every pinned namespace is older.
By the time this fails there is nothing userspace can do. setns() has already run, so the host tree is unreachable by path; it is unreachable through a fd saved beforehand as well, since mount(2) requires the source to live in the current mount namespace; and the offending mounts cannot be dropped from the detached copy first, because umount(2) requires check_mnt(). So a runtime cannot fall back to the pivot_root() path at that point -- it has to predict the situation before setns(), which is what crun now does, at the cost of the optimization on every such host.
Every other path that crosses a mount namespace boundary already leaves these mounts behind: copy_mnt_ns() passes CL_COPY_UNBINDABLE | CL_EXPIRE, and create_new_namespace() passes no CL_COPY_MNT_NS_FILE either -- the latter deliberately, per the comment added in commit 9b8a0ba68246 ("mount: add OPEN_TREE_NAMESPACE"): "When creating a new mount namespace we don't want to copy over mounts of mount namespaces to avoid the risk of cycles". Only get_detached_copy() asks for them, inherited from the unconditional CL_COPY_MNT_NS_FILE that predates that exception.
Add a flag so a caller can ask for a clone without them, as suggested by Aleksa when the exception was introduced [2]:
I kind of think this is a somewhat theoretical issue but I don't think we'll be bitten by it. My gut feeling is that I'd prefer this to be an OPEN_TREE_* flag that you have to set (so we can support this in the future) but that's kinda ugly too...
Keep it opt-in rather than changing the default. A clone attached in the caller's own namespace, or in an older one, keeps such mounts usable, and open_tree(OPEN_TREE_CLONE) plus move_mount(2) is what mount --rbind is through a file descriptor, so dropping them by default would make mount --rbind / /mnt silently lose /run/snapd/ns/*.mnt on a live system. Only a caller heading into a younger namespace, where the tree is refused anyway, has reason to ask for this.
With OPEN_TREE_NAMESPACE, which never copies these mounts, the flag is accepted and has no effect, so a runtime can pass it unconditionally. Locked nsfs mounts are skipped the same way copy_mnt_ns() already skips them, so this exposes nothing new.
--- fs/namespace.c | 14 +- include/uapi/linux/mount.h | 1 + .../open_tree_ns/open_tree_ns_test.c | 183 ++++++++++++++++++ 3 files changed, 196 insertions(+), 2 deletions(-)
fs/namespace.c +12 -2
@@ -3062,7 +3062,8 @@ static struct mnt_namespace *get_detached_copy(const struct path *path, unsigned ns->seq_origin = src_mnt_ns->ns.ns_id; } - mnt =__do_loopback (path, (flags &AT_RECURSIVE ),CL_COPY_MNT_NS_FILE ); + mnt =__do_loopback (path, (flags &AT_RECURSIVE ), + (flags &OPEN_TREE_SKIP_MNTNS ) ?0 :CL_COPY_MNT_NS_FILE );if (IS_ERR (mnt)) { emptied_ns = ns;return ERR_CAST (mnt);
@@ -3204,7 +3205,16 @@ static struct file *vfs_open_tree(int dfd, const char __user *filename, unsignedif (flags & ~(AT_EMPTY_PATH |AT_NO_AUTOMOUNT |AT_RECURSIVE |AT_SYMLINK_NOFOLLOW |OPEN_TREE_CLONE | -OPEN_TREE_CLOEXEC |OPEN_TREE_NAMESPACE )) +OPEN_TREE_CLOEXEC |OPEN_TREE_NAMESPACE | +OPEN_TREE_SKIP_MNTNS )) +return ERR_PTR (-EINVAL ); + +/* +* Only meaningful when a tree is copied. OPEN_TREE_NAMESPACE never +* copies pinned mount namespaces, so there the flag is a no-op. +*/ +if ((flags &OPEN_TREE_SKIP_MNTNS ) && + !(flags & (OPEN_TREE_CLONE |OPEN_TREE_NAMESPACE )))return ERR_PTR (-EINVAL ); if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)) ==
include/uapi/linux/mount.h +1 -0
@@ -63,6 +63,7 @@*/ #define OPEN_TREE_CLONE (1 << 0)/* Clone the target tree and attach the clone */ #define OPEN_TREE_NAMESPACE (1 << 1)/* Clone the target tree into a new mount namespace */ +#define OPEN_TREE_SKIP_MNTNS (1 << 2)/* Omit pinned mount namespaces from the clone */ #define OPEN_TREE_CLOEXEC O_CLOEXEC/* Close the file on execve() */ /*
tools/testing/selftests/filesystems/open_tree_ns/open_tree_ns_test.c +183 -0
199 lines not on this page · raw message
Signature · 1 line
2.55.0
Christian Brauner
maintainerQuoted 7 lines · Kir Kolyshkin
crun, a container runtime, recently started using
OPEN_TREE_NAMESPACEto set up the container's mount namespace, andopen_tree(OPEN_TREE_CLONE)for the sources of its bind mounts. This turns out not to work on hosts that have a mount namespace pinned anywhere below such a source: the recursive clone picks the pinned namespace up, andmove_mount(2)then refuses to attach the tree with ELOOP fromcheck_for_nsfs_mounts(). This was reported to crun in [1].
Yeah, I think that makes sense. Please split this into two patches
though: semantic changes separate from test changes. Maybe call it OPEN_TREE_DROP_MNTNS_MOUNTS instead?
Quoted 10 lines · Christian Brauner
Quoted 7 lines · Kir Kolyshkin
crun, a container runtime, recently started using
OPEN_TREE_NAMESPACEto set up the container's mount namespace, andopen_tree(OPEN_TREE_CLONE)for the sources of its bind mounts. This turns out not to work on hosts that have a mount namespace pinned anywhere below such a source: the recursive clone picks the pinned namespace up, andmove_mount(2)then refuses to attach the tree with ELOOP fromcheck_for_nsfs_mounts(). This was reported to crun in [1].
Quoted 3 lines
Yeah, I think that makes sense. Please split this into two patches
though: semantic changes separate from test changes. Maybe call itOPEN_TREE_DROP_MNTNS_MOUNTSinstead?
Thanks! Done in v2:
https://lore.kernel.org/r/[email protected]