Files
stambha/third_party/patches/linux/0134-link-original-namespace.patch
T

2365 lines
69 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
Date: Wed, 4 Nov 2026 05:10:50 +0000
Subject: [PATCH] refactor(vfs): link original namespace lookup and mount trees
---
fs/Makefile | 2 +-
fs/namespace.c | 835 +-----------------------------------------
fs/namespace_tree.c | 871 ++++++++++++++++++++++++++++++++++++++++++++
fs/namespace_tree.h | 49 +++
fs/nsfs.c | 6 +
fs/open.c | 187 ----------
fs/path_open.c | 189 ++++++++++
7 files changed, 1117 insertions(+), 1022 deletions(-)
create mode 100644 fs/namespace_tree.c
create mode 100644 fs/namespace_tree.h
diff --git a/fs/Makefile b/fs/Makefile
index eaeb9380e986327476ebc776434d04c6fbb741f8..272f25f9ccb667529b28a438ea9ea52c23439e08 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -10,7 +10,7 @@
obj-y := open.o path_open.o filp_close.o read_write.o file_table.o file_table_init.o super.o super_anon_dev.o super_lifetime.o \
char_dev.o stat.o exec.o task_comm.o pipe.o namei.o path_get.o name_hash.o filename.o fcntl.o \
ioctl.o readdir.o select.o dcache.o dcache_init.o dentry_ancestry.o inode.o inode_cache_init.o inode_timestamps.o inode_evict.o \
- attr.o bad_inode.o bad_inode_ops.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o mount_cache.o \
+ attr.o bad_inode.o bad_inode_ops.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o namespace_tree.o mount_cache.o \
seq_file.o seq_lifetime.o seq_output.o seq_path.o xattr.o xattr_access.o simple_xattr.o libfs.o pseudo_metadata.o stashed_path.o fs-writeback.o \
pnode.o splice.o sync.o utimes.o d_path.o \
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
diff --git a/fs/namespace.c b/fs/namespace.c
index bd20d4884efca8099ff060e6181049b803d40fe0..c8562d31197f90b1cffc2becdbe5243881a67fa4 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -37,6 +37,7 @@
#include "pnode.h"
#include "mount_cache.h"
+#include "namespace_tree.h"
#include "internal.h"
static char * __initdata initramfs_options;
@@ -48,8 +49,6 @@ static int __init initramfs_options_setup(char *str)
__setup("initramfs_options=", initramfs_options_setup);
-DEFINE_FREE(mntput, struct vfsmount *, if (!IS_ERR(_T)) mntput(_T))
-
enum mount_kattr_flags_t {
MOUNT_KATTR_RECURSE = (1 << 0),
MOUNT_KATTR_IDMAP_REPLACE = (1 << 1),
@@ -228,39 +227,7 @@ int sb_prepare_remount_readonly(struct super_block *sb)
return err;
}
-/*
- * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
- * current mount namespace.
- *
- * The common case is dentries are not mountpoints at all and that
- * test is handled inline. For the slow case when we are actually
- * dealing with a mountpoint of some kind, walk through all of the
- * mounts in the current mount namespace and test to see if the dentry
- * is a mountpoint.
- *
- * The mount_hashtable is not usable in the context because we
- * need to identify all mounts that may be in the current mount
- * namespace not just a mount that happens to have some specified
- * parent mount.
- */
-bool __is_local_mountpoint(const struct dentry *dentry)
-{
- struct mnt_namespace *ns = current->nsproxy->mnt_ns;
- struct mount *mnt, *n;
-
- guard(namespace_shared)();
-
- rbtree_postorder_for_each_entry_safe(mnt, n, &ns->mounts, mnt_node)
- if (mnt->mnt_mountpoint == dentry)
- return true;
- return false;
-}
-
-static inline int check_mnt(const struct mount *mnt)
-{
- return mnt->mnt_ns == current->nsproxy->mnt_ns;
-}
static inline bool check_anonymous_mnt(struct mount *mnt)
{
@@ -273,82 +240,10 @@ static inline bool check_anonymous_mnt(struct mount *mnt)
return !seq || (seq == current->nsproxy->mnt_ns->ns.ns_id);
}
-/*
- * vfsmount lock must be held for write
- */
-static void touch_mnt_namespace(struct mnt_namespace *ns)
-{
- if (ns) {
- ns->event = ++mnt_namespace_event;
- wake_up_interruptible(&ns->poll);
- }
-}
-
-static inline struct mount *node_to_mount(struct rb_node *node)
-{
- return node ? rb_entry(node, struct mount, mnt_node) : NULL;
-}
-static void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt)
-{
- struct rb_node **link = &ns->mounts.rb_node;
- struct rb_node *parent = NULL;
- bool mnt_first_node = true, mnt_last_node = true;
- WARN_ON(mnt_ns_attached(mnt));
- WRITE_ONCE(mnt->mnt_ns, ns);
- while (*link) {
- parent = *link;
- if (mnt->mnt_id_unique < node_to_mount(parent)->mnt_id_unique) {
- link = &parent->rb_left;
- mnt_last_node = false;
- } else {
- link = &parent->rb_right;
- mnt_first_node = false;
- }
- }
- if (mnt_last_node)
- ns->mnt_last_node = &mnt->mnt_node;
- if (mnt_first_node)
- ns->mnt_first_node = &mnt->mnt_node;
- rb_link_node(&mnt->mnt_node, parent, link);
- rb_insert_color(&mnt->mnt_node, &ns->mounts);
- if ((mnt->mnt.mnt_sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED) &&
- mnt->mnt.mnt_root == mnt->mnt.mnt_sb->s_root)
- hlist_add_head(&mnt->mnt_ns_visible, &ns->mnt_visible_mounts);
-
- mnt_notify_add(mnt);
-}
-
-static struct mount *skip_mnt_tree(struct mount *p)
-{
- struct list_head *prev = p->mnt_mounts.prev;
- while (prev != &p->mnt_mounts) {
- p = list_entry(prev, struct mount, mnt_child);
- prev = p->mnt_mounts.prev;
- }
- return p;
-}
-
-/*
- * vfsmount lock must be held for write
- */
-static void commit_tree(struct mount *mnt)
-{
- struct mnt_namespace *n = mnt->mnt_parent->mnt_ns;
-
- if (!mnt_ns_attached(mnt)) {
- for (struct mount *m = mnt; m; m = next_mnt(m, mnt))
- mnt_add_to_ns(n, m);
- n->nr_mounts += n->pending_mounts;
- n->pending_mounts = 0;
- }
-
- make_visible(mnt);
- touch_mnt_namespace(n);
-}
/**
* path_is_mountpoint() - Check if path is a mount in the current namespace.
@@ -751,18 +646,6 @@ SYSCALL_DEFINE1(oldumount, char __user *, name)
#endif
-static bool is_mnt_ns_file(struct dentry *dentry)
-{
- struct ns_common *ns;
-
- /* Is this a proxy for a mount namespace? */
- if (dentry->d_op != &ns_dentry_operations)
- return false;
-
- ns = d_inode(dentry)->i_private;
-
- return ns->ops == &mntns_operations;
-}
struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry)
{
@@ -785,80 +668,6 @@ static bool mnt_ns_loop(struct dentry *dentry)
return current->nsproxy->mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
}
-struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,
- int flag)
-{
- struct mount *res, *src_parent, *src_root_child, *src_mnt,
- *dst_parent, *dst_mnt;
-
- if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(src_root))
- return ERR_PTR(-EINVAL);
-
- if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
- return ERR_PTR(-EINVAL);
-
- res = dst_mnt = clone_mnt(src_root, dentry, flag);
- if (IS_ERR(dst_mnt))
- return dst_mnt;
-
- src_parent = src_root;
-
- list_for_each_entry(src_root_child, &src_root->mnt_mounts, mnt_child) {
- if (!is_subdir(src_root_child->mnt_mountpoint, dentry))
- continue;
-
- for (src_mnt = src_root_child; src_mnt;
- src_mnt = next_mnt(src_mnt, src_root_child)) {
- if (!(flag & CL_COPY_UNBINDABLE) &&
- IS_MNT_UNBINDABLE(src_mnt)) {
- if (src_mnt->mnt.mnt_flags & MNT_LOCKED) {
- /* Both unbindable and locked. */
- dst_mnt = ERR_PTR(-EPERM);
- goto out;
- } else {
- src_mnt = skip_mnt_tree(src_mnt);
- continue;
- }
- }
- if (!(flag & CL_COPY_MNT_NS_FILE) &&
- is_mnt_ns_file(src_mnt->mnt.mnt_root)) {
- src_mnt = skip_mnt_tree(src_mnt);
- continue;
- }
- while (src_parent != src_mnt->mnt_parent) {
- src_parent = src_parent->mnt_parent;
- dst_mnt = dst_mnt->mnt_parent;
- }
-
- src_parent = src_mnt;
- dst_parent = dst_mnt;
- dst_mnt = clone_mnt(src_mnt, src_mnt->mnt.mnt_root, flag);
- if (IS_ERR(dst_mnt))
- goto out;
- lock_mount_hash();
- if (src_mnt->mnt.mnt_flags & MNT_LOCKED)
- dst_mnt->mnt.mnt_flags |= MNT_LOCKED;
- if (unlikely(flag & CL_EXPIRE)) {
- /* stick the duplicate mount on the same expiry
- * list as the original if that was on one */
- if (!list_empty(&src_mnt->mnt_expire))
- list_add(&dst_mnt->mnt_expire,
- &src_mnt->mnt_expire);
- }
- attach_mnt(dst_mnt, dst_parent, src_parent->mnt_mp);
- unlock_mount_hash();
- }
- }
- return res;
-
-out:
- if (res) {
- lock_mount_hash();
- umount_tree(res, UMOUNT_SYNC);
- unlock_mount_hash();
- }
- return dst_mnt;
-}
static inline bool extend_array(struct path **res, struct path **to_free,
unsigned n, unsigned *count, unsigned new_count)
@@ -918,8 +727,6 @@ void drop_collected_paths(const struct path *paths, const struct path *prealloc)
kfree(paths);
}
-static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
-
/* locks: namespace_shared && pinned(mnt) || mount_locked_reader */
static bool __has_locked_children(struct mount *mnt, struct dentry *dentry)
{
@@ -1022,278 +829,14 @@ struct vfsmount *clone_private_mount(const struct path *path)
}
EXPORT_SYMBOL_GPL(clone_private_mount);
-static void lock_mnt_tree(struct mount *mnt)
-{
- struct mount *p;
-
- for (p = mnt; p; p = next_mnt(p, mnt)) {
- int flags = p->mnt.mnt_flags;
- /* Don't allow unprivileged users to change mount flags */
- flags |= MNT_LOCK_ATIME;
-
- if (flags & MNT_READONLY)
- flags |= MNT_LOCK_READONLY;
-
- if (flags & MNT_NODEV)
- flags |= MNT_LOCK_NODEV;
-
- if (flags & MNT_NOSUID)
- flags |= MNT_LOCK_NOSUID;
-
- if (flags & MNT_NOEXEC)
- flags |= MNT_LOCK_NOEXEC;
- /* Don't allow unprivileged users to reveal what is under a mount */
- if (list_empty(&p->mnt_expire) && p != mnt)
- flags |= MNT_LOCKED;
- p->mnt.mnt_flags = flags;
- }
-}
-
-static void cleanup_group_ids(struct mount *mnt, struct mount *end)
-{
- struct mount *p;
- for (p = mnt; p != end; p = next_mnt(p, mnt)) {
- if (p->mnt_group_id && !IS_MNT_SHARED(p))
- mnt_release_group_id(p);
- }
-}
-static int invent_group_ids(struct mount *mnt, bool recurse)
-{
- struct mount *p;
-
- for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
- if (!p->mnt_group_id) {
- int err = mnt_alloc_group_id(p);
- if (err) {
- cleanup_group_ids(mnt, p);
- return err;
- }
- }
- }
-
- return 0;
-}
enum mnt_tree_flags_t {
MNT_TREE_BENEATH = BIT(0),
MNT_TREE_PROPAGATION = BIT(1),
};
-/**
- * attach_recursive_mnt - attach a source mount tree
- * @source_mnt: mount tree to be attached
- * @dest: the context for mounting at the place where the tree should go
- *
- * NOTE: in the table below explains the semantics when a source mount
- * of a given type is attached to a destination mount of a given type.
- * ---------------------------------------------------------------------------
- * | BIND MOUNT OPERATION |
- * |**************************************************************************
- * | source-->| shared | private | slave | unbindable |
- * | dest | | | | |
- * | | | | | | |
- * | v | | | | |
- * |**************************************************************************
- * | shared | shared (++) | shared (+) | shared(+++)| invalid |
- * | | | | | |
- * |non-shared| shared (+) | private | slave (*) | invalid |
- * ***************************************************************************
- * A bind operation clones the source mount and mounts the clone on the
- * destination mount.
- *
- * (++) the cloned mount is propagated to all the mounts in the propagation
- * tree of the destination mount and the cloned mount is added to
- * the peer group of the source mount.
- * (+) the cloned mount is created under the destination mount and is marked
- * as shared. The cloned mount is added to the peer group of the source
- * mount.
- * (+++) the mount is propagated to all the mounts in the propagation tree
- * of the destination mount and the cloned mount is made slave
- * of the same master as that of the source mount. The cloned mount
- * is marked as 'shared and slave'.
- * (*) the cloned mount is made a slave of the same master as that of the
- * source mount.
- *
- * ---------------------------------------------------------------------------
- * | MOVE MOUNT OPERATION |
- * |**************************************************************************
- * | source-->| shared | private | slave | unbindable |
- * | dest | | | | |
- * | | | | | | |
- * | v | | | | |
- * |**************************************************************************
- * | shared | shared (+) | shared (+) | shared(+++) | invalid |
- * | | | | | |
- * |non-shared| shared (+*) | private | slave (*) | unbindable |
- * ***************************************************************************
- *
- * (+) the mount is moved to the destination. And is then propagated to
- * all the mounts in the propagation tree of the destination mount.
- * (+*) the mount is moved to the destination.
- * (+++) the mount is moved to the destination and is then propagated to
- * all the mounts belonging to the destination mount's propagation tree.
- * the mount is marked as 'shared and slave'.
- * (*) the mount continues to be a slave at the new location.
- *
- * if the source mount is a tree, the operations explained above is
- * applied to each mount in the tree.
- * Must be called without spinlocks held, since this function can sleep
- * in allocations.
- *
- * Context: The function expects namespace_lock() to be held.
- * Return: If @source_mnt was successfully attached 0 is returned.
- * Otherwise a negative error code is returned.
- */
-static int attach_recursive_mnt(struct mount *source_mnt,
- const struct pinned_mountpoint *dest)
-{
- struct mount *dest_mnt = dest->parent;
- struct mountpoint *dest_mp = dest->mp;
- HLIST_HEAD(tree_list);
- struct mnt_namespace *ns = dest_mnt->mnt_ns;
- struct user_namespace *user_ns = ns->user_ns;
- struct pinned_mountpoint root = {};
- struct mountpoint *shorter = NULL;
- struct mount *child, *p;
- struct mount *top;
- struct hlist_node *n;
- int err = 0;
- bool moving = mnt_has_parent(source_mnt);
-
- /*
- * A caller in an unprivileged mount namespaces may trigger an
- * automount and propagate locked mounts into privileged mount
- * namespaces. Take ownership from the target mount namespace.
- * It's equivalent for everything but the automount case.
- *
- * Detached trees in anonymous mount namespaces by be handed
- * over via SCM_RIGHTS or inherited in other ways on purpose
- * the attaching task's mount namespace is authoritative, not
- * the creator of the detached tree.
- */
- if (is_anon_ns(ns))
- user_ns = current->nsproxy->mnt_ns->user_ns;
- else
- user_ns = ns->user_ns;
-
- /*
- * Preallocate a mountpoint in case the new mounts need to be
- * mounted beneath mounts on the same mountpoint.
- */
- for (top = source_mnt; ; top = top->overmount) {
- if (!shorter && is_mnt_ns_file(top->mnt.mnt_root))
- shorter = top->mnt_mp;
- if (likely(!top->overmount))
- break;
- }
- err = get_mountpoint(top->mnt.mnt_root, &root);
- if (err)
- return err;
-
- /* Is there space to add these mounts to the mount namespace? */
- if (!moving) {
- err = count_mounts(ns, source_mnt);
- if (err)
- goto out;
- }
-
- if (IS_MNT_SHARED(dest_mnt)) {
- err = invent_group_ids(source_mnt, true);
- if (err)
- goto out;
- err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
- }
- lock_mount_hash();
- if (err)
- goto out_cleanup_ids;
-
- if (IS_MNT_SHARED(dest_mnt)) {
- for (p = source_mnt; p; p = next_mnt(p, source_mnt))
- set_mnt_shared(p);
- }
-
- if (moving) {
- umount_mnt(source_mnt);
- mnt_notify_add(source_mnt);
- /* if the mount is moved, it should no longer be expired
- * automatically */
- list_del_init(&source_mnt->mnt_expire);
- } else {
- if (source_mnt->mnt_ns) {
- /* move from anon - the caller will destroy */
- emptied_ns = source_mnt->mnt_ns;
- for (p = source_mnt; p; p = next_mnt(p, source_mnt))
- move_from_ns(p);
- }
- }
-
- mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
- /*
- * Now the original copy is in the same state as the secondaries -
- * its root attached to mountpoint, but not hashed and all mounts
- * in it are either in our namespace or in no namespace at all.
- * Add the original to the list of copies and deal with the
- * rest of work for all of them uniformly.
- */
- hlist_add_head(&source_mnt->mnt_hash, &tree_list);
-
- hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
- struct mount *q;
- hlist_del_init(&child->mnt_hash);
- /* Notice when we are propagating across user namespaces */
- if (child->mnt_parent->mnt_ns->user_ns != user_ns)
- lock_mnt_tree(child);
- q = __lookup_mnt(&child->mnt_parent->mnt,
- child->mnt_mountpoint);
- commit_tree(child);
- if (q) {
- struct mount *r = topmost_overmount(child);
- struct mountpoint *mp = root.mp;
-
- if (unlikely(shorter) && child != source_mnt)
- mp = shorter;
- /*
- * If @q was locked it was meant to hide
- * whatever was under it. Let @child take over
- * that job and lock it. If @child is the mount
- * the caller placed we can then unlock @q:
- * nothing another namespace does removes it
- * again. A propagated copy goes away when the
- * mounter of the original unmounts it, so @q
- * keeps its lock.
- */
- if (IS_MNT_LOCKED(q)) {
- child->mnt.mnt_flags |= MNT_LOCKED;
- if (child == source_mnt)
- q->mnt.mnt_flags &= ~MNT_LOCKED;
- }
- mnt_change_mountpoint(r, mp, q);
- }
- }
- unpin_mountpoint(&root);
- unlock_mount_hash();
-
- return 0;
-
- out_cleanup_ids:
- while (!hlist_empty(&tree_list)) {
- child = hlist_entry(tree_list.first, struct mount, mnt_hash);
- child->mnt_parent->mnt_ns->pending_mounts = 0;
- umount_tree(child, UMOUNT_SYNC);
- }
- unlock_mount_hash();
- cleanup_group_ids(source_mnt, NULL);
- out:
- ns->pending_mounts = 0;
-
- read_seqlock_excl(&mount_lock);
- unpin_mountpoint(&root);
- read_sequnlock_excl(&mount_lock);
-
- return err;
-}
static inline struct mount *where_to_mount(const struct path *path,
struct dentry **dentry,
@@ -1408,48 +951,11 @@ static void do_lock_mount(const struct path *path,
} while (err == -EAGAIN);
}
-static void __unlock_mount(struct pinned_mountpoint *m)
-{
- inode_unlock(m->mp->m_dentry->d_inode);
- read_seqlock_excl(&mount_lock);
- unpin_mountpoint(m);
- read_sequnlock_excl(&mount_lock);
- namespace_unlock();
-}
-
-static inline void unlock_mount(struct pinned_mountpoint *m)
-{
- if (!IS_ERR(m->parent))
- __unlock_mount(m);
-}
-
-static void lock_mount_exact(const struct path *path,
- struct pinned_mountpoint *mp, bool copy_mount,
- unsigned int copy_flags);
#define LOCK_MOUNT_MAYBE_BENEATH(mp, path, beneath) \
struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
do_lock_mount((path), &mp, (beneath))
#define LOCK_MOUNT(mp, path) LOCK_MOUNT_MAYBE_BENEATH(mp, (path), false)
-#define LOCK_MOUNT_EXACT(mp, path) \
- struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
- lock_mount_exact((path), &mp, false, 0)
-#define LOCK_MOUNT_EXACT_COPY(mp, path, copy_flags) \
- struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
- lock_mount_exact((path), &mp, true, (copy_flags))
-
-static int graft_tree(struct mount *mnt, const struct pinned_mountpoint *mp)
-{
- if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
- return -EINVAL;
-
- if (d_is_dir(mp->mp->m_dentry) !=
- d_is_dir(mnt->mnt.mnt_root))
- return -ENOTDIR;
-
- return attach_recursive_mnt(mnt, mp);
-}
-
static int may_change_propagation(const struct mount *m)
{
struct mnt_namespace *ns = m->mnt_ns;
@@ -2110,33 +1616,6 @@ static int do_set_group(const struct path *from_path, const struct path *to_path
return 0;
}
-/**
- * path_overmounted - check if path is overmounted
- * @path: path to check
- *
- * Check if path is overmounted, i.e., if there's a mount on top of
- * @path->mnt with @path->dentry as mountpoint.
- *
- * Context: namespace_sem must be held at least shared.
- * MUST NOT be called under lock_mount_hash() (there one should just
- * call __lookup_mnt() and check if it returns NULL).
- * Return: If path is overmounted true is returned, false if not.
- */
-static inline bool path_overmounted(const struct path *path)
-{
- unsigned seq = read_seqbegin(&mount_lock);
- bool no_child;
-
- rcu_read_lock();
- no_child = !__lookup_mnt(path->mnt, path->dentry);
- rcu_read_unlock();
- if (need_seqretry(&mount_lock, seq)) {
- read_seqlock_excl(&mount_lock);
- no_child = !__lookup_mnt(path->mnt, path->dentry);
- read_sequnlock_excl(&mount_lock);
- }
- return unlikely(!no_child);
-}
/*
* Check if there is a possibly empty chain of descent from p1 to p2.
@@ -2344,39 +1823,6 @@ static int do_move_mount_old(const struct path *path, const char *old_name)
return do_move_mount(&old_path, path, 0);
}
-/*
- * add a mount into a namespace's mount tree
- */
-static int do_add_mount(struct mount *newmnt, const struct pinned_mountpoint *mp,
- int mnt_flags)
-{
- struct mount *parent = mp->parent;
-
- if (IS_ERR(parent))
- return PTR_ERR(parent);
-
- mnt_flags &= ~MNT_INTERNAL_FLAGS;
-
- if (unlikely(!check_mnt(parent))) {
- /* that's acceptable only for automounts done in private ns */
- if (!(mnt_flags & MNT_SHRINKABLE))
- return -EINVAL;
- /* ... and for those we'd better have mountpoint still alive */
- if (!is_mounted(&parent->mnt))
- return -EINVAL;
- }
-
- /* Refuse the same filesystem on the same mount point */
- if (parent->mnt.mnt_sb == newmnt->mnt.mnt_sb &&
- parent->mnt.mnt_root == mp->mp->m_dentry)
- return -EBUSY;
-
- if (d_is_symlink(newmnt->mnt.mnt_root))
- return -EINVAL;
-
- newmnt->mnt.mnt_flags = mnt_flags;
- return graft_tree(newmnt, mp);
-}
static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
@@ -2470,69 +1916,7 @@ static int do_new_mount(const struct path *path, const char *fstype,
return err;
}
-static void lock_mount_exact(const struct path *path,
- struct pinned_mountpoint *mp, bool copy_mount,
- unsigned int copy_flags)
-{
- struct dentry *dentry = path->dentry;
- int err;
-
- /* Assert that inode_lock() locked the correct inode. */
- VFS_WARN_ON_ONCE(copy_mount && !path_mounted(path));
-
- inode_lock(dentry->d_inode);
- namespace_lock();
- if (unlikely(cant_mount(dentry)))
- err = -ENOENT;
- else if (!copy_mount && path_overmounted(path))
- err = -EBUSY;
- else
- err = get_mountpoint(dentry, mp);
- if (unlikely(err)) {
- namespace_unlock();
- inode_unlock(dentry->d_inode);
- mp->parent = ERR_PTR(err);
- return;
- }
-
- if (copy_mount)
- mp->parent = clone_mnt(real_mount(path->mnt), dentry, copy_flags);
- else
- mp->parent = real_mount(path->mnt);
- if (unlikely(IS_ERR(mp->parent)))
- __unlock_mount(mp);
-}
-
-int finish_automount(struct vfsmount *__m, const struct path *path)
-{
- struct vfsmount *m __free(mntput) = __m;
- struct mount *mnt;
- int err;
-
- if (!m)
- return 0;
- if (IS_ERR(m))
- return PTR_ERR(m);
-
- mnt = real_mount(m);
- if (m->mnt_root == path->dentry)
- return -ELOOP;
-
- /*
- * we don't want to use LOCK_MOUNT() - in this case finding something
- * that overmounts our mountpoint to be means "quitely drop what we've
- * got", not "try to mount it on top".
- */
- LOCK_MOUNT_EXACT(mp, path);
- if (mp.parent == ERR_PTR(-EBUSY))
- return 0;
-
- err = do_add_mount(mnt, &mp, path->mnt->mnt_flags | MNT_SHRINKABLE);
- if (likely(!err))
- retain_and_null_ptr(m);
- return err;
-}
/**
* mnt_set_expiry - Put a mount on an expiration list
@@ -2831,146 +2215,9 @@ int do_mount(const char *dev_name, const char __user *dir_name,
return path_mount(dev_name, &path, type_page, flags, data_page);
}
-static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
-{
- return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
-}
-
-static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
-{
- struct mnt_namespace *new_ns;
- struct ucounts *ucounts;
- int ret;
-
- ucounts = inc_mnt_namespaces(user_ns);
- if (!ucounts)
- return ERR_PTR(-ENOSPC);
-
- new_ns = kzalloc_obj(struct mnt_namespace, GFP_KERNEL_ACCOUNT);
- if (!new_ns) {
- dec_mnt_namespaces(ucounts);
- return ERR_PTR(-ENOMEM);
- }
- if (anon)
- ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
- else
- ret = ns_common_init(new_ns);
- if (ret) {
- kfree(new_ns);
- dec_mnt_namespaces(ucounts);
- return ERR_PTR(ret);
- }
- ns_tree_gen_id(new_ns);
-
- new_ns->is_anon = anon;
- refcount_set(&new_ns->passive, 1);
- new_ns->mounts = RB_ROOT;
- init_waitqueue_head(&new_ns->poll);
- new_ns->user_ns = get_user_ns(user_ns);
- new_ns->ucounts = ucounts;
- return new_ns;
-}
__latent_entropy
-struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
- struct user_namespace *user_ns, struct fs_struct *new_fs)
-{
- struct mnt_namespace *new_ns;
- struct path old_root __free(path_put) = {};
- struct path old_pwd __free(path_put) = {};
- struct mount *p, *q;
- struct mount *old;
- struct mount *new;
- int copy_flags;
-
- BUG_ON(!ns);
-
- if (likely(!(flags & CLONE_NEWNS))) {
- get_mnt_ns(ns);
- return ns;
- }
-
- old = ns->root;
-
- new_ns = alloc_mnt_ns(user_ns, false);
- if (IS_ERR(new_ns))
- return new_ns;
-
- guard(namespace_excl)();
-
- if (flags & CLONE_EMPTY_MNTNS)
- copy_flags = 0;
- else
- copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
- if (user_ns != ns->user_ns)
- copy_flags |= CL_SLAVE;
-
- if (flags & CLONE_EMPTY_MNTNS)
- new = clone_mnt(old, old->mnt.mnt_root, copy_flags);
- else
- new = copy_tree(old, old->mnt.mnt_root, copy_flags);
- if (IS_ERR(new)) {
- emptied_ns = new_ns;
- return ERR_CAST(new);
- }
- if (user_ns != ns->user_ns) {
- guard(mount_writer)();
- lock_mnt_tree(new);
- }
- new_ns->root = new;
-
- if (flags & CLONE_EMPTY_MNTNS) {
- /*
- * Empty mount namespace: only the root mount exists.
- * Reset root and pwd to the cloned mount's root dentry.
- */
- if (new_fs) {
- old_root = new_fs->root;
- old_pwd = new_fs->pwd;
-
- new_fs->root.mnt = mntget(&new->mnt);
- new_fs->root.dentry = dget(new->mnt.mnt_root);
-
- new_fs->pwd.mnt = mntget(&new->mnt);
- new_fs->pwd.dentry = dget(new->mnt.mnt_root);
- }
- mnt_add_to_ns(new_ns, new);
- new_ns->nr_mounts++;
- } else {
- /*
- * Full copy: walk old and new trees in parallel, switching
- * the tsk->fs->* elements and marking new vfsmounts as
- * belonging to new namespace. We have already acquired a
- * private fs_struct, so tsk->fs->lock is not needed.
- */
- p = old;
- q = new;
- while (p) {
- mnt_add_to_ns(new_ns, q);
- new_ns->nr_mounts++;
- if (new_fs) {
- if (&p->mnt == new_fs->root.mnt) {
- old_root.mnt = new_fs->root.mnt;
- new_fs->root.mnt = mntget(&q->mnt);
- }
- if (&p->mnt == new_fs->pwd.mnt) {
- old_pwd.mnt = new_fs->pwd.mnt;
- new_fs->pwd.mnt = mntget(&q->mnt);
- }
- }
- p = next_mnt(p, old);
- q = next_mnt(q, new);
- if (!q)
- break;
- // an mntns binding we'd skipped?
- while (p->mnt.mnt_root != q->mnt.mnt_root)
- p = next_mnt(skip_mnt_tree(p), old);
- }
- }
- ns_tree_add_raw(new_ns);
- return new_ns;
-}
struct dentry *mount_subtree(struct vfsmount *m, const char *name)
{
@@ -4808,13 +4055,6 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req,
return ret;
}
-struct mnt_namespace init_mnt_ns = {
- .ns = NS_COMMON_INIT(init_mnt_ns),
- .user_ns = &init_user_ns,
- .passive = REFCOUNT_INIT(1),
- .mounts = RB_ROOT,
- .poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll),
-};
static void __init init_mount_tree(void)
{
@@ -5043,83 +4283,10 @@ bool mnt_may_suid(struct vfsmount *mnt)
current_in_userns(mnt->mnt_sb->s_user_ns);
}
-static struct ns_common *mntns_get(struct task_struct *task)
-{
- struct ns_common *ns = NULL;
- struct nsproxy *nsproxy;
-
- task_lock(task);
- nsproxy = task->nsproxy;
- if (nsproxy) {
- ns = &nsproxy->mnt_ns->ns;
- get_mnt_ns(to_mnt_ns(ns));
- }
- task_unlock(task);
-
- return ns;
-}
-
-static void mntns_put(struct ns_common *ns)
-{
- put_mnt_ns(to_mnt_ns(ns));
-}
-
-static int mntns_install(struct nsset *nsset, struct ns_common *ns)
-{
- struct nsproxy *nsproxy = nsset->nsproxy;
- struct fs_struct *fs = nsset->fs;
- struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
- struct user_namespace *user_ns = nsset->cred->user_ns;
- struct path root;
- int err;
-
- if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
- !ns_capable(user_ns, CAP_SYS_CHROOT) ||
- !ns_capable(user_ns, CAP_SYS_ADMIN))
- return -EPERM;
-
- if (is_anon_ns(mnt_ns))
- return -EINVAL;
- if (fs->users != 1)
- return -EINVAL;
- get_mnt_ns(mnt_ns);
- old_mnt_ns = nsproxy->mnt_ns;
- nsproxy->mnt_ns = mnt_ns;
- /* Find the root */
- err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
- "/", LOOKUP_DOWN, &root);
- if (err) {
- /* revert to old namespace */
- nsproxy->mnt_ns = old_mnt_ns;
- put_mnt_ns(mnt_ns);
- return err;
- }
- put_mnt_ns(old_mnt_ns);
-
- /* Update the pwd and root */
- set_fs_pwd(fs, &root);
- set_fs_root(fs, &root);
-
- path_put(&root);
- return 0;
-}
-
-static struct user_namespace *mntns_owner(struct ns_common *ns)
-{
- return to_mnt_ns(ns)->user_ns;
-}
-
-const struct proc_ns_operations mntns_operations = {
- .name = "mnt",
- .get = mntns_get,
- .put = mntns_put,
- .install = mntns_install,
- .owner = mntns_owner,
-};
#ifdef CONFIG_SYSCTL
static const struct ctl_table fs_namespace_sysctls[] = {
diff --git a/fs/namespace_tree.c b/fs/namespace_tree.c
new file mode 100644
index 0000000000000000000000000000000000000000..0bea52769aec35200156d3acf6f8470b7edcfcff
--- /dev/null
+++ b/fs/namespace_tree.c
@@ -0,0 +1,871 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Mount tree operations from linux/fs/namespace.c
+ *
+ * (C) Copyright Al Viro 2000, 2001
+ *
+ * Based on code from fs/super.c, copyright Linus Torvalds and others.
+ * Heavily rewritten.
+ */
+
+#include <linux/syscalls.h>
+#include <linux/export.h>
+#include <linux/capability.h>
+#include <linux/mnt_namespace.h>
+#include <linux/user_namespace.h>
+#include <linux/namei.h>
+#include <linux/security.h>
+#include <linux/cred.h>
+#include <linux/idr.h>
+#include <linux/init.h> /* init_rootfs */
+#include <linux/fs_struct.h> /* get_fs_root et.al. */
+#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
+#include <linux/file.h>
+#include <linux/uaccess.h>
+#include <linux/proc_ns.h>
+#include <linux/magic.h>
+#include <linux/memblock.h>
+#include <linux/proc_fs.h>
+#include <linux/task_work.h>
+#include <linux/sched/task.h>
+#include <uapi/linux/mount.h>
+#include <linux/fs_context.h>
+#include <linux/shmem_fs.h>
+#include <linux/mnt_idmapping.h>
+#include <linux/pidfs.h>
+#include <linux/nstree.h>
+
+#include "pnode.h"
+#include "mount_cache.h"
+#include "namespace_tree.h"
+#include "internal.h"
+
+/*
+ * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
+ * current mount namespace.
+ *
+ * The common case is dentries are not mountpoints at all and that
+ * test is handled inline. For the slow case when we are actually
+ * dealing with a mountpoint of some kind, walk through all of the
+ * mounts in the current mount namespace and test to see if the dentry
+ * is a mountpoint.
+ *
+ * The mount_hashtable is not usable in the context because we
+ * need to identify all mounts that may be in the current mount
+ * namespace not just a mount that happens to have some specified
+ * parent mount.
+ */
+bool __is_local_mountpoint(const struct dentry *dentry)
+{
+ struct mnt_namespace *ns = current->nsproxy->mnt_ns;
+ struct mount *mnt, *n;
+
+ guard(namespace_shared)();
+
+ rbtree_postorder_for_each_entry_safe(mnt, n, &ns->mounts, mnt_node)
+ if (mnt->mnt_mountpoint == dentry)
+ return true;
+
+ return false;
+}
+
+/*
+ * vfsmount lock must be held for write
+ */
+void touch_mnt_namespace(struct mnt_namespace *ns)
+{
+ if (ns) {
+ ns->event = ++mnt_namespace_event;
+ wake_up_interruptible(&ns->poll);
+ }
+}
+
+void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt)
+{
+ struct rb_node **link = &ns->mounts.rb_node;
+ struct rb_node *parent = NULL;
+ bool mnt_first_node = true, mnt_last_node = true;
+
+ WARN_ON(mnt_ns_attached(mnt));
+ WRITE_ONCE(mnt->mnt_ns, ns);
+ while (*link) {
+ parent = *link;
+ if (mnt->mnt_id_unique < node_to_mount(parent)->mnt_id_unique) {
+ link = &parent->rb_left;
+ mnt_last_node = false;
+ } else {
+ link = &parent->rb_right;
+ mnt_first_node = false;
+ }
+ }
+
+ if (mnt_last_node)
+ ns->mnt_last_node = &mnt->mnt_node;
+ if (mnt_first_node)
+ ns->mnt_first_node = &mnt->mnt_node;
+ rb_link_node(&mnt->mnt_node, parent, link);
+ rb_insert_color(&mnt->mnt_node, &ns->mounts);
+
+ if ((mnt->mnt.mnt_sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED) &&
+ mnt->mnt.mnt_root == mnt->mnt.mnt_sb->s_root)
+ hlist_add_head(&mnt->mnt_ns_visible, &ns->mnt_visible_mounts);
+
+ mnt_notify_add(mnt);
+}
+
+static struct mount *skip_mnt_tree(struct mount *p)
+{
+ struct list_head *prev = p->mnt_mounts.prev;
+ while (prev != &p->mnt_mounts) {
+ p = list_entry(prev, struct mount, mnt_child);
+ prev = p->mnt_mounts.prev;
+ }
+ return p;
+}
+
+/*
+ * vfsmount lock must be held for write
+ */
+static void commit_tree(struct mount *mnt)
+{
+ struct mnt_namespace *n = mnt->mnt_parent->mnt_ns;
+
+ if (!mnt_ns_attached(mnt)) {
+ for (struct mount *m = mnt; m; m = next_mnt(m, mnt))
+ mnt_add_to_ns(n, m);
+ n->nr_mounts += n->pending_mounts;
+ n->pending_mounts = 0;
+ }
+
+ make_visible(mnt);
+ touch_mnt_namespace(n);
+}
+
+bool is_mnt_ns_file(struct dentry *dentry)
+{
+ struct ns_common *ns;
+
+ /* Is this a proxy for a mount namespace? */
+ if (dentry->d_op != &ns_dentry_operations)
+ return false;
+
+ ns = d_inode(dentry)->i_private;
+
+ return ns->ops == &mntns_operations;
+}
+
+struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,
+ int flag)
+{
+ struct mount *res, *src_parent, *src_root_child, *src_mnt,
+ *dst_parent, *dst_mnt;
+
+ if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(src_root))
+ return ERR_PTR(-EINVAL);
+
+ if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
+ return ERR_PTR(-EINVAL);
+
+ res = dst_mnt = clone_mnt(src_root, dentry, flag);
+ if (IS_ERR(dst_mnt))
+ return dst_mnt;
+
+ src_parent = src_root;
+
+ list_for_each_entry(src_root_child, &src_root->mnt_mounts, mnt_child) {
+ if (!is_subdir(src_root_child->mnt_mountpoint, dentry))
+ continue;
+
+ for (src_mnt = src_root_child; src_mnt;
+ src_mnt = next_mnt(src_mnt, src_root_child)) {
+ if (!(flag & CL_COPY_UNBINDABLE) &&
+ IS_MNT_UNBINDABLE(src_mnt)) {
+ if (src_mnt->mnt.mnt_flags & MNT_LOCKED) {
+ /* Both unbindable and locked. */
+ dst_mnt = ERR_PTR(-EPERM);
+ goto out;
+ } else {
+ src_mnt = skip_mnt_tree(src_mnt);
+ continue;
+ }
+ }
+ if (!(flag & CL_COPY_MNT_NS_FILE) &&
+ is_mnt_ns_file(src_mnt->mnt.mnt_root)) {
+ src_mnt = skip_mnt_tree(src_mnt);
+ continue;
+ }
+ while (src_parent != src_mnt->mnt_parent) {
+ src_parent = src_parent->mnt_parent;
+ dst_mnt = dst_mnt->mnt_parent;
+ }
+
+ src_parent = src_mnt;
+ dst_parent = dst_mnt;
+ dst_mnt = clone_mnt(src_mnt, src_mnt->mnt.mnt_root, flag);
+ if (IS_ERR(dst_mnt))
+ goto out;
+ lock_mount_hash();
+ if (src_mnt->mnt.mnt_flags & MNT_LOCKED)
+ dst_mnt->mnt.mnt_flags |= MNT_LOCKED;
+ if (unlikely(flag & CL_EXPIRE)) {
+ /* stick the duplicate mount on the same expiry
+ * list as the original if that was on one */
+ if (!list_empty(&src_mnt->mnt_expire))
+ list_add(&dst_mnt->mnt_expire,
+ &src_mnt->mnt_expire);
+ }
+ attach_mnt(dst_mnt, dst_parent, src_parent->mnt_mp);
+ unlock_mount_hash();
+ }
+ }
+ return res;
+
+out:
+ if (res) {
+ lock_mount_hash();
+ umount_tree(res, UMOUNT_SYNC);
+ unlock_mount_hash();
+ }
+ return dst_mnt;
+}
+
+void lock_mnt_tree(struct mount *mnt)
+{
+ struct mount *p;
+
+ for (p = mnt; p; p = next_mnt(p, mnt)) {
+ int flags = p->mnt.mnt_flags;
+ /* Don't allow unprivileged users to change mount flags */
+ flags |= MNT_LOCK_ATIME;
+
+ if (flags & MNT_READONLY)
+ flags |= MNT_LOCK_READONLY;
+
+ if (flags & MNT_NODEV)
+ flags |= MNT_LOCK_NODEV;
+
+ if (flags & MNT_NOSUID)
+ flags |= MNT_LOCK_NOSUID;
+
+ if (flags & MNT_NOEXEC)
+ flags |= MNT_LOCK_NOEXEC;
+ /* Don't allow unprivileged users to reveal what is under a mount */
+ if (list_empty(&p->mnt_expire) && p != mnt)
+ flags |= MNT_LOCKED;
+ p->mnt.mnt_flags = flags;
+ }
+}
+
+void cleanup_group_ids(struct mount *mnt, struct mount *end)
+{
+ struct mount *p;
+
+ for (p = mnt; p != end; p = next_mnt(p, mnt)) {
+ if (p->mnt_group_id && !IS_MNT_SHARED(p))
+ mnt_release_group_id(p);
+ }
+}
+
+int invent_group_ids(struct mount *mnt, bool recurse)
+{
+ struct mount *p;
+
+ for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
+ if (!p->mnt_group_id) {
+ int err = mnt_alloc_group_id(p);
+ if (err) {
+ cleanup_group_ids(mnt, p);
+ return err;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * attach_recursive_mnt - attach a source mount tree
+ * @source_mnt: mount tree to be attached
+ * @dest: the context for mounting at the place where the tree should go
+ *
+ * NOTE: in the table below explains the semantics when a source mount
+ * of a given type is attached to a destination mount of a given type.
+ * ---------------------------------------------------------------------------
+ * | BIND MOUNT OPERATION |
+ * |**************************************************************************
+ * | source-->| shared | private | slave | unbindable |
+ * | dest | | | | |
+ * | | | | | | |
+ * | v | | | | |
+ * |**************************************************************************
+ * | shared | shared (++) | shared (+) | shared(+++)| invalid |
+ * | | | | | |
+ * |non-shared| shared (+) | private | slave (*) | invalid |
+ * ***************************************************************************
+ * A bind operation clones the source mount and mounts the clone on the
+ * destination mount.
+ *
+ * (++) the cloned mount is propagated to all the mounts in the propagation
+ * tree of the destination mount and the cloned mount is added to
+ * the peer group of the source mount.
+ * (+) the cloned mount is created under the destination mount and is marked
+ * as shared. The cloned mount is added to the peer group of the source
+ * mount.
+ * (+++) the mount is propagated to all the mounts in the propagation tree
+ * of the destination mount and the cloned mount is made slave
+ * of the same master as that of the source mount. The cloned mount
+ * is marked as 'shared and slave'.
+ * (*) the cloned mount is made a slave of the same master as that of the
+ * source mount.
+ *
+ * ---------------------------------------------------------------------------
+ * | MOVE MOUNT OPERATION |
+ * |**************************************************************************
+ * | source-->| shared | private | slave | unbindable |
+ * | dest | | | | |
+ * | | | | | | |
+ * | v | | | | |
+ * |**************************************************************************
+ * | shared | shared (+) | shared (+) | shared(+++) | invalid |
+ * | | | | | |
+ * |non-shared| shared (+*) | private | slave (*) | unbindable |
+ * ***************************************************************************
+ *
+ * (+) the mount is moved to the destination. And is then propagated to
+ * all the mounts in the propagation tree of the destination mount.
+ * (+*) the mount is moved to the destination.
+ * (+++) the mount is moved to the destination and is then propagated to
+ * all the mounts belonging to the destination mount's propagation tree.
+ * the mount is marked as 'shared and slave'.
+ * (*) the mount continues to be a slave at the new location.
+ *
+ * if the source mount is a tree, the operations explained above is
+ * applied to each mount in the tree.
+ * Must be called without spinlocks held, since this function can sleep
+ * in allocations.
+ *
+ * Context: The function expects namespace_lock() to be held.
+ * Return: If @source_mnt was successfully attached 0 is returned.
+ * Otherwise a negative error code is returned.
+ */
+int attach_recursive_mnt(struct mount *source_mnt,
+ const struct pinned_mountpoint *dest)
+{
+ struct mount *dest_mnt = dest->parent;
+ struct mountpoint *dest_mp = dest->mp;
+ HLIST_HEAD(tree_list);
+ struct mnt_namespace *ns = dest_mnt->mnt_ns;
+ struct user_namespace *user_ns = ns->user_ns;
+ struct pinned_mountpoint root = {};
+ struct mountpoint *shorter = NULL;
+ struct mount *child, *p;
+ struct mount *top;
+ struct hlist_node *n;
+ int err = 0;
+ bool moving = mnt_has_parent(source_mnt);
+
+ /*
+ * A caller in an unprivileged mount namespaces may trigger an
+ * automount and propagate locked mounts into privileged mount
+ * namespaces. Take ownership from the target mount namespace.
+ * It's equivalent for everything but the automount case.
+ *
+ * Detached trees in anonymous mount namespaces by be handed
+ * over via SCM_RIGHTS or inherited in other ways on purpose
+ * the attaching task's mount namespace is authoritative, not
+ * the creator of the detached tree.
+ */
+ if (is_anon_ns(ns))
+ user_ns = current->nsproxy->mnt_ns->user_ns;
+ else
+ user_ns = ns->user_ns;
+
+ /*
+ * Preallocate a mountpoint in case the new mounts need to be
+ * mounted beneath mounts on the same mountpoint.
+ */
+ for (top = source_mnt; ; top = top->overmount) {
+ if (!shorter && is_mnt_ns_file(top->mnt.mnt_root))
+ shorter = top->mnt_mp;
+ if (likely(!top->overmount))
+ break;
+ }
+ err = get_mountpoint(top->mnt.mnt_root, &root);
+ if (err)
+ return err;
+
+ /* Is there space to add these mounts to the mount namespace? */
+ if (!moving) {
+ err = count_mounts(ns, source_mnt);
+ if (err)
+ goto out;
+ }
+
+ if (IS_MNT_SHARED(dest_mnt)) {
+ err = invent_group_ids(source_mnt, true);
+ if (err)
+ goto out;
+ err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
+ }
+ lock_mount_hash();
+ if (err)
+ goto out_cleanup_ids;
+
+ if (IS_MNT_SHARED(dest_mnt)) {
+ for (p = source_mnt; p; p = next_mnt(p, source_mnt))
+ set_mnt_shared(p);
+ }
+
+ if (moving) {
+ umount_mnt(source_mnt);
+ mnt_notify_add(source_mnt);
+ /* if the mount is moved, it should no longer be expired
+ * automatically */
+ list_del_init(&source_mnt->mnt_expire);
+ } else {
+ if (source_mnt->mnt_ns) {
+ /* move from anon - the caller will destroy */
+ emptied_ns = source_mnt->mnt_ns;
+ for (p = source_mnt; p; p = next_mnt(p, source_mnt))
+ move_from_ns(p);
+ }
+ }
+
+ mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
+ /*
+ * Now the original copy is in the same state as the secondaries -
+ * its root attached to mountpoint, but not hashed and all mounts
+ * in it are either in our namespace or in no namespace at all.
+ * Add the original to the list of copies and deal with the
+ * rest of work for all of them uniformly.
+ */
+ hlist_add_head(&source_mnt->mnt_hash, &tree_list);
+
+ hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
+ struct mount *q;
+ hlist_del_init(&child->mnt_hash);
+ /* Notice when we are propagating across user namespaces */
+ if (child->mnt_parent->mnt_ns->user_ns != user_ns)
+ lock_mnt_tree(child);
+ q = __lookup_mnt(&child->mnt_parent->mnt,
+ child->mnt_mountpoint);
+ commit_tree(child);
+ if (q) {
+ struct mount *r = topmost_overmount(child);
+ struct mountpoint *mp = root.mp;
+
+ if (unlikely(shorter) && child != source_mnt)
+ mp = shorter;
+ /*
+ * If @q was locked it was meant to hide
+ * whatever was under it. Let @child take over
+ * that job and lock it. If @child is the mount
+ * the caller placed we can then unlock @q:
+ * nothing another namespace does removes it
+ * again. A propagated copy goes away when the
+ * mounter of the original unmounts it, so @q
+ * keeps its lock.
+ */
+ if (IS_MNT_LOCKED(q)) {
+ child->mnt.mnt_flags |= MNT_LOCKED;
+ if (child == source_mnt)
+ q->mnt.mnt_flags &= ~MNT_LOCKED;
+ }
+ mnt_change_mountpoint(r, mp, q);
+ }
+ }
+ unpin_mountpoint(&root);
+ unlock_mount_hash();
+
+ return 0;
+
+ out_cleanup_ids:
+ while (!hlist_empty(&tree_list)) {
+ child = hlist_entry(tree_list.first, struct mount, mnt_hash);
+ child->mnt_parent->mnt_ns->pending_mounts = 0;
+ umount_tree(child, UMOUNT_SYNC);
+ }
+ unlock_mount_hash();
+ cleanup_group_ids(source_mnt, NULL);
+ out:
+ ns->pending_mounts = 0;
+
+ read_seqlock_excl(&mount_lock);
+ unpin_mountpoint(&root);
+ read_sequnlock_excl(&mount_lock);
+
+ return err;
+}
+
+void __unlock_mount(struct pinned_mountpoint *m)
+{
+ inode_unlock(m->mp->m_dentry->d_inode);
+ read_seqlock_excl(&mount_lock);
+ unpin_mountpoint(m);
+ read_sequnlock_excl(&mount_lock);
+ namespace_unlock();
+}
+
+int graft_tree(struct mount *mnt, const struct pinned_mountpoint *mp)
+{
+ if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
+ return -EINVAL;
+
+ if (d_is_dir(mp->mp->m_dentry) !=
+ d_is_dir(mnt->mnt.mnt_root))
+ return -ENOTDIR;
+
+ return attach_recursive_mnt(mnt, mp);
+}
+
+/**
+ * path_overmounted - check if path is overmounted
+ * @path: path to check
+ *
+ * Check if path is overmounted, i.e., if there's a mount on top of
+ * @path->mnt with @path->dentry as mountpoint.
+ *
+ * Context: namespace_sem must be held at least shared.
+ * MUST NOT be called under lock_mount_hash() (there one should just
+ * call __lookup_mnt() and check if it returns NULL).
+ * Return: If path is overmounted true is returned, false if not.
+ */
+static inline bool path_overmounted(const struct path *path)
+{
+ unsigned seq = read_seqbegin(&mount_lock);
+ bool no_child;
+
+ rcu_read_lock();
+ no_child = !__lookup_mnt(path->mnt, path->dentry);
+ rcu_read_unlock();
+ if (need_seqretry(&mount_lock, seq)) {
+ read_seqlock_excl(&mount_lock);
+ no_child = !__lookup_mnt(path->mnt, path->dentry);
+ read_sequnlock_excl(&mount_lock);
+ }
+ return unlikely(!no_child);
+}
+
+/*
+ * add a mount into a namespace's mount tree
+ */
+int do_add_mount(struct mount *newmnt, const struct pinned_mountpoint *mp,
+ int mnt_flags)
+{
+ struct mount *parent = mp->parent;
+
+ if (IS_ERR(parent))
+ return PTR_ERR(parent);
+
+ mnt_flags &= ~MNT_INTERNAL_FLAGS;
+
+ if (unlikely(!check_mnt(parent))) {
+ /* that's acceptable only for automounts done in private ns */
+ if (!(mnt_flags & MNT_SHRINKABLE))
+ return -EINVAL;
+ /* ... and for those we'd better have mountpoint still alive */
+ if (!is_mounted(&parent->mnt))
+ return -EINVAL;
+ }
+
+ /* Refuse the same filesystem on the same mount point */
+ if (parent->mnt.mnt_sb == newmnt->mnt.mnt_sb &&
+ parent->mnt.mnt_root == mp->mp->m_dentry)
+ return -EBUSY;
+
+ if (d_is_symlink(newmnt->mnt.mnt_root))
+ return -EINVAL;
+
+ newmnt->mnt.mnt_flags = mnt_flags;
+ return graft_tree(newmnt, mp);
+}
+
+void lock_mount_exact(const struct path *path,
+ struct pinned_mountpoint *mp, bool copy_mount,
+ unsigned int copy_flags)
+{
+ struct dentry *dentry = path->dentry;
+ int err;
+
+ /* Assert that inode_lock() locked the correct inode. */
+ VFS_WARN_ON_ONCE(copy_mount && !path_mounted(path));
+
+ inode_lock(dentry->d_inode);
+ namespace_lock();
+ if (unlikely(cant_mount(dentry)))
+ err = -ENOENT;
+ else if (!copy_mount && path_overmounted(path))
+ err = -EBUSY;
+ else
+ err = get_mountpoint(dentry, mp);
+ if (unlikely(err)) {
+ namespace_unlock();
+ inode_unlock(dentry->d_inode);
+ mp->parent = ERR_PTR(err);
+ return;
+ }
+
+ if (copy_mount)
+ mp->parent = clone_mnt(real_mount(path->mnt), dentry, copy_flags);
+ else
+ mp->parent = real_mount(path->mnt);
+ if (unlikely(IS_ERR(mp->parent)))
+ __unlock_mount(mp);
+}
+
+int finish_automount(struct vfsmount *__m, const struct path *path)
+{
+ struct vfsmount *m __free(mntput) = __m;
+ struct mount *mnt;
+ int err;
+
+ if (!m)
+ return 0;
+ if (IS_ERR(m))
+ return PTR_ERR(m);
+
+ mnt = real_mount(m);
+
+ if (m->mnt_root == path->dentry)
+ return -ELOOP;
+
+ /*
+ * we don't want to use LOCK_MOUNT() - in this case finding something
+ * that overmounts our mountpoint to be means "quitely drop what we've
+ * got", not "try to mount it on top".
+ */
+ LOCK_MOUNT_EXACT(mp, path);
+ if (mp.parent == ERR_PTR(-EBUSY))
+ return 0;
+
+ err = do_add_mount(mnt, &mp, path->mnt->mnt_flags | MNT_SHRINKABLE);
+ if (likely(!err))
+ retain_and_null_ptr(m);
+ return err;
+}
+
+static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
+{
+ return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
+}
+
+struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
+{
+ struct mnt_namespace *new_ns;
+ struct ucounts *ucounts;
+ int ret;
+
+ ucounts = inc_mnt_namespaces(user_ns);
+ if (!ucounts)
+ return ERR_PTR(-ENOSPC);
+
+ new_ns = kzalloc_obj(struct mnt_namespace, GFP_KERNEL_ACCOUNT);
+ if (!new_ns) {
+ dec_mnt_namespaces(ucounts);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ if (anon)
+ ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
+ else
+ ret = ns_common_init(new_ns);
+ if (ret) {
+ kfree(new_ns);
+ dec_mnt_namespaces(ucounts);
+ return ERR_PTR(ret);
+ }
+ ns_tree_gen_id(new_ns);
+
+ new_ns->is_anon = anon;
+ refcount_set(&new_ns->passive, 1);
+ new_ns->mounts = RB_ROOT;
+ init_waitqueue_head(&new_ns->poll);
+ new_ns->user_ns = get_user_ns(user_ns);
+ new_ns->ucounts = ucounts;
+ return new_ns;
+}
+
+struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
+ struct user_namespace *user_ns, struct fs_struct *new_fs)
+{
+ struct mnt_namespace *new_ns;
+ struct path old_root __free(path_put) = {};
+ struct path old_pwd __free(path_put) = {};
+ struct mount *p, *q;
+ struct mount *old;
+ struct mount *new;
+ int copy_flags;
+
+ BUG_ON(!ns);
+
+ if (likely(!(flags & CLONE_NEWNS))) {
+ get_mnt_ns(ns);
+ return ns;
+ }
+
+ old = ns->root;
+
+ new_ns = alloc_mnt_ns(user_ns, false);
+ if (IS_ERR(new_ns))
+ return new_ns;
+
+ guard(namespace_excl)();
+
+ if (flags & CLONE_EMPTY_MNTNS)
+ copy_flags = 0;
+ else
+ copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
+ if (user_ns != ns->user_ns)
+ copy_flags |= CL_SLAVE;
+
+ if (flags & CLONE_EMPTY_MNTNS)
+ new = clone_mnt(old, old->mnt.mnt_root, copy_flags);
+ else
+ new = copy_tree(old, old->mnt.mnt_root, copy_flags);
+ if (IS_ERR(new)) {
+ emptied_ns = new_ns;
+ return ERR_CAST(new);
+ }
+ if (user_ns != ns->user_ns) {
+ guard(mount_writer)();
+ lock_mnt_tree(new);
+ }
+ new_ns->root = new;
+
+ if (flags & CLONE_EMPTY_MNTNS) {
+ /*
+ * Empty mount namespace: only the root mount exists.
+ * Reset root and pwd to the cloned mount's root dentry.
+ */
+ if (new_fs) {
+ old_root = new_fs->root;
+ old_pwd = new_fs->pwd;
+
+ new_fs->root.mnt = mntget(&new->mnt);
+ new_fs->root.dentry = dget(new->mnt.mnt_root);
+
+ new_fs->pwd.mnt = mntget(&new->mnt);
+ new_fs->pwd.dentry = dget(new->mnt.mnt_root);
+ }
+ mnt_add_to_ns(new_ns, new);
+ new_ns->nr_mounts++;
+ } else {
+ /*
+ * Full copy: walk old and new trees in parallel, switching
+ * the tsk->fs->* elements and marking new vfsmounts as
+ * belonging to new namespace. We have already acquired a
+ * private fs_struct, so tsk->fs->lock is not needed.
+ */
+ p = old;
+ q = new;
+ while (p) {
+ mnt_add_to_ns(new_ns, q);
+ new_ns->nr_mounts++;
+ if (new_fs) {
+ if (&p->mnt == new_fs->root.mnt) {
+ old_root.mnt = new_fs->root.mnt;
+ new_fs->root.mnt = mntget(&q->mnt);
+ }
+ if (&p->mnt == new_fs->pwd.mnt) {
+ old_pwd.mnt = new_fs->pwd.mnt;
+ new_fs->pwd.mnt = mntget(&q->mnt);
+ }
+ }
+ p = next_mnt(p, old);
+ q = next_mnt(q, new);
+ if (!q)
+ break;
+ // an mntns binding we'd skipped?
+ while (p->mnt.mnt_root != q->mnt.mnt_root)
+ p = next_mnt(skip_mnt_tree(p), old);
+ }
+ }
+ ns_tree_add_raw(new_ns);
+ return new_ns;
+}
+
+struct mnt_namespace init_mnt_ns = {
+ .ns = NS_COMMON_INIT(init_mnt_ns),
+ .user_ns = &init_user_ns,
+ .passive = REFCOUNT_INIT(1),
+ .mounts = RB_ROOT,
+ .poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll),
+};
+
+static struct ns_common *mntns_get(struct task_struct *task)
+{
+ struct ns_common *ns = NULL;
+ struct nsproxy *nsproxy;
+
+ task_lock(task);
+ nsproxy = task->nsproxy;
+ if (nsproxy) {
+ ns = &nsproxy->mnt_ns->ns;
+ get_mnt_ns(to_mnt_ns(ns));
+ }
+ task_unlock(task);
+
+ return ns;
+}
+
+static void mntns_put(struct ns_common *ns)
+{
+ put_mnt_ns(to_mnt_ns(ns));
+}
+
+static int mntns_install(struct nsset *nsset, struct ns_common *ns)
+{
+ struct nsproxy *nsproxy = nsset->nsproxy;
+ struct fs_struct *fs = nsset->fs;
+ struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
+ struct user_namespace *user_ns = nsset->cred->user_ns;
+ struct path root;
+ int err;
+
+ if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
+ !ns_capable(user_ns, CAP_SYS_CHROOT) ||
+ !ns_capable(user_ns, CAP_SYS_ADMIN))
+ return -EPERM;
+
+ if (is_anon_ns(mnt_ns))
+ return -EINVAL;
+
+ if (fs->users != 1)
+ return -EINVAL;
+
+ get_mnt_ns(mnt_ns);
+ old_mnt_ns = nsproxy->mnt_ns;
+ nsproxy->mnt_ns = mnt_ns;
+
+ /* Find the root */
+ err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
+ "/", LOOKUP_DOWN, &root);
+ if (err) {
+ /* revert to old namespace */
+ nsproxy->mnt_ns = old_mnt_ns;
+ put_mnt_ns(mnt_ns);
+ return err;
+ }
+
+ put_mnt_ns(old_mnt_ns);
+
+ /* Update the pwd and root */
+ set_fs_pwd(fs, &root);
+ set_fs_root(fs, &root);
+
+ path_put(&root);
+ return 0;
+}
+
+static struct user_namespace *mntns_owner(struct ns_common *ns)
+{
+ return to_mnt_ns(ns)->user_ns;
+}
+
+const struct proc_ns_operations mntns_operations = {
+ .name = "mnt",
+ .get = mntns_get,
+ .put = mntns_put,
+ .install = mntns_install,
+ .owner = mntns_owner,
+};
diff --git a/fs/namespace_tree.h b/fs/namespace_tree.h
new file mode 100644
index 0000000000000000000000000000000000000000..c9efd950522d86c2802d19920b0122fa181f30a3
--- /dev/null
+++ b/fs/namespace_tree.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _FS_NAMESPACE_TREE_H
+#define _FS_NAMESPACE_TREE_H
+
+
+static inline int check_mnt(const struct mount *mnt)
+{
+ return mnt->mnt_ns == current->nsproxy->mnt_ns;
+}
+
+void touch_mnt_namespace(struct mnt_namespace *ns);
+static inline struct mount *node_to_mount(struct rb_node *node)
+{
+ return node ? rb_entry(node, struct mount, mnt_node) : NULL;
+}
+
+void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt);
+bool is_mnt_ns_file(struct dentry *dentry);
+void lock_mnt_tree(struct mount *mnt);
+void cleanup_group_ids(struct mount *mnt, struct mount *end);
+int invent_group_ids(struct mount *mnt, bool recurse);
+int attach_recursive_mnt(struct mount *source_mnt,
+ const struct pinned_mountpoint *dest);
+void __unlock_mount(struct pinned_mountpoint *m);
+int graft_tree(struct mount *mnt, const struct pinned_mountpoint *mp);
+int do_add_mount(struct mount *newmnt, const struct pinned_mountpoint *mp,
+ int mnt_flags);
+void lock_mount_exact(const struct path *path,
+ struct pinned_mountpoint *mp, bool copy_mount,
+ unsigned int copy_flags);
+struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon);
+
+DEFINE_FREE(mntput, struct vfsmount *, if (!IS_ERR(_T)) mntput(_T))
+
+static inline void unlock_mount(struct pinned_mountpoint *m)
+{
+ if (!IS_ERR(m->parent))
+ __unlock_mount(m);
+}
+
+#define LOCK_MOUNT_EXACT(mp, path) \
+ struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
+ lock_mount_exact((path), &mp, false, 0)
+#define LOCK_MOUNT_EXACT_COPY(mp, path, copy_flags) \
+ struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
+ lock_mount_exact((path), &mp, true, (copy_flags))
+
+
+#endif
diff --git a/fs/nsfs.c b/fs/nsfs.c
index 3843d3e4072c05a3c56b9e66e03373060ed0d25e..d345666659dc0d0ef5a22ff32039497efbd5cd7d 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -44,6 +44,12 @@ static void nsfs_evict(struct inode *inode)
{
struct ns_common *ns = inode->i_private;
+ /* A failed root-dentry allocation can evict the namespace-free root. */
+ if (!ns) {
+ clear_inode(inode);
+ return;
+ }
+
__ns_ref_active_put(ns);
clear_inode(inode);
ns->ops->put(ns);
diff --git a/fs/open.c b/fs/open.c
index c717582cb6133c7ea8d0ea4eb6dd3487bc4d2667..a7c5c30df11c3a1992295a100ea6061a5d9e1e20 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -902,181 +902,6 @@ struct file *kernel_file_open(const struct path *path, int flags,
}
EXPORT_SYMBOL_GPL(kernel_file_open);
-#define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
-#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_EMPTYPATH)
-
-inline struct open_how build_open_how(int flags, umode_t mode)
-{
- struct open_how how = {
- .flags = flags & VALID_OPEN_FLAGS,
- .mode = mode & S_IALLUGO,
- };
-
- /* O_PATH beats everything else. */
- if (how.flags & O_PATH)
- how.flags &= O_PATH_FLAGS;
- /* Modes should only be set for create-like flags. */
- if (!WILL_CREATE(how.flags))
- how.mode = 0;
- return how;
-}
-
-inline int build_open_flags(const struct open_how *how, struct open_flags *op)
-{
- u64 flags = how->flags;
- u64 strip = O_CLOEXEC;
- int lookup_flags = 0;
- int acc_mode = ACC_MODE(flags);
-
- BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
- "VALID_OPEN_FLAGS must fit in 32 bits");
- /* The whole point: OPENAT2_REGULAR must be unrepresentable in int. */
- BUILD_BUG_ON_MSG(!upper_32_bits(OPENAT2_REGULAR),
- "OPENAT2_REGULAR must live in the upper 32 bits of open_how::flags");
- /* Prevent a future bit collision between UAPI and internal carrier. */
- BUILD_BUG_ON_MSG(OPENAT2_REGULAR & VALID_OPEN_FLAGS,
- "OPENAT2_REGULAR must not alias any open()/openat() flag");
- BUILD_BUG_ON_MSG(__O_REGULAR & VALID_OPENAT2_FLAGS,
- "__O_REGULAR must not alias any user-visible flag");
-
- /*
- * Strip flags that aren't relevant in determining struct open_flags.
- */
- flags &= ~strip;
-
- /*
- * Older syscalls implicitly clear all of the invalid flags or argument
- * values before calling build_open_flags(), but openat2(2) checks all
- * of its arguments.
- */
- if (flags & ~VALID_OPENAT2_FLAGS)
- return -EINVAL;
- if (how->resolve & ~VALID_RESOLVE_FLAGS)
- return -EINVAL;
-
- /* Scoping flags are mutually exclusive. */
- if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
- return -EINVAL;
-
- /* Deal with the mode. */
- if (WILL_CREATE(flags)) {
- if (how->mode & ~S_IALLUGO)
- return -EINVAL;
- op->mode = how->mode | S_IFREG;
- } else {
- if (how->mode != 0)
- return -EINVAL;
- op->mode = 0;
- }
-
- /*
- * Block bugs where O_DIRECTORY | O_CREAT created regular files.
- * Note, that blocking O_DIRECTORY | O_CREAT here also protects
- * O_TMPFILE below which requires O_DIRECTORY being raised.
- */
- if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
- return -EINVAL;
-
- /* Now handle the creative implementation of O_TMPFILE. */
- if (flags & __O_TMPFILE) {
- /*
- * In order to ensure programs get explicit errors when trying
- * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
- * is raised alongside __O_TMPFILE.
- */
- if (!(flags & O_DIRECTORY))
- return -EINVAL;
- if (!(acc_mode & MAY_WRITE))
- return -EINVAL;
- }
- /*
- * Asking to open a directory and a regular file at the same time is
- * contradictory.
- */
- if ((flags & (O_DIRECTORY | OPENAT2_REGULAR)) ==
- (O_DIRECTORY | OPENAT2_REGULAR))
- return -EINVAL;
-
- if (flags & O_PATH) {
- /* O_PATH only permits certain other flags to be set. */
- if (flags & ~O_PATH_FLAGS)
- return -EINVAL;
- acc_mode = 0;
- }
-
- /*
- * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
- * check for O_DSYNC if the need any syncing at all we enforce it's
- * always set instead of having to deal with possibly weird behaviour
- * for malicious applications setting only __O_SYNC.
- */
- if (flags & __O_SYNC)
- flags |= O_DSYNC;
-
- /*
- * Translate the upper-32-bit UAPI bit OPENAT2_REGULAR into the
- * kernel-internal lower-32-bit __O_REGULAR carrier so the bit
- * survives the assignment to op->open_flag (an int) below and the
- * subsequent flow through f->f_flags (unsigned int) and the
- * i_op->atomic_open() callback (unsigned). do_dentry_open() strips
- * __O_REGULAR before the file becomes visible to userspace.
- */
- if (flags & OPENAT2_REGULAR) {
- flags &= ~OPENAT2_REGULAR;
- flags |= __O_REGULAR;
- }
-
- op->open_flag = flags;
-
- /* O_TRUNC implies we need access checks for write permissions */
- if (flags & O_TRUNC)
- acc_mode |= MAY_WRITE;
-
- /* Allow the LSM permission hook to distinguish append
- access from general write access. */
- if (flags & O_APPEND)
- acc_mode |= MAY_APPEND;
-
- op->acc_mode = acc_mode;
-
- op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
-
- if (flags & O_CREAT) {
- op->intent |= LOOKUP_CREATE;
- if (flags & O_EXCL) {
- op->intent |= LOOKUP_EXCL;
- flags |= O_NOFOLLOW;
- }
- }
-
- if (flags & O_DIRECTORY)
- lookup_flags |= LOOKUP_DIRECTORY;
- if (!(flags & O_NOFOLLOW))
- lookup_flags |= LOOKUP_FOLLOW;
- if (flags & O_EMPTYPATH)
- lookup_flags |= LOOKUP_EMPTY;
-
- if (how->resolve & RESOLVE_NO_XDEV)
- lookup_flags |= LOOKUP_NO_XDEV;
- if (how->resolve & RESOLVE_NO_MAGICLINKS)
- lookup_flags |= LOOKUP_NO_MAGICLINKS;
- if (how->resolve & RESOLVE_NO_SYMLINKS)
- lookup_flags |= LOOKUP_NO_SYMLINKS;
- if (how->resolve & RESOLVE_BENEATH)
- lookup_flags |= LOOKUP_BENEATH;
- if (how->resolve & RESOLVE_IN_ROOT)
- lookup_flags |= LOOKUP_IN_ROOT;
- if (how->resolve & RESOLVE_CACHED) {
- /* Don't bother even trying for create/truncate/tmpfile open */
- if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE))
- return -EAGAIN;
- lookup_flags |= LOOKUP_CACHED;
- }
-
- op->lookup_flags = lookup_flags;
- return 0;
-}
-
/**
* file_open_name - open file and return file pointer
*
@@ -1116,18 +941,6 @@ struct file *filp_open(const char *filename, int flags, umode_t mode)
}
EXPORT_SYMBOL(filp_open);
-struct file *file_open_root(const struct path *root,
- const char *filename, int flags, umode_t mode)
-{
- struct open_flags op;
- struct open_how how = build_open_how(flags, mode);
- int err = build_open_flags(&how, &op);
- if (err)
- return ERR_PTR(err);
- return do_file_open_root(root, filename, &op);
-}
-EXPORT_SYMBOL(file_open_root);
-
static int do_sys_openat2(int dfd, const char __user *filename,
struct open_how *how)
{
diff --git a/fs/path_open.c b/fs/path_open.c
index bd2491fd657f40a45c2ae4d630aaed12cdd48e66..5a0d7663ceb2a8b1d4685d7ee1716036acc0c15e 100644
--- a/fs/path_open.c
+++ b/fs/path_open.c
@@ -4,12 +4,14 @@
* Copyright (C) 1991, 1992 Linus Torvalds
*/
#include <linux/export.h>
+#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/filelock.h>
#include <linux/fs.h>
#include <linux/fsnotify.h>
#include <linux/module.h>
#include <linux/mount.h>
+#include <linux/namei.h>
#include <linux/pagemap.h>
#include <linux/security.h>
@@ -253,3 +255,190 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
inode_unlock(dentry->d_inode);
return ret;
}
+
+#define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
+#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_EMPTYPATH)
+
+inline struct open_how build_open_how(int flags, umode_t mode)
+{
+ struct open_how how = {
+ .flags = flags & VALID_OPEN_FLAGS,
+ .mode = mode & S_IALLUGO,
+ };
+
+ /* O_PATH beats everything else. */
+ if (how.flags & O_PATH)
+ how.flags &= O_PATH_FLAGS;
+ /* Modes should only be set for create-like flags. */
+ if (!WILL_CREATE(how.flags))
+ how.mode = 0;
+ return how;
+}
+
+inline int build_open_flags(const struct open_how *how, struct open_flags *op)
+{
+ u64 flags = how->flags;
+ u64 strip = O_CLOEXEC;
+ int lookup_flags = 0;
+ int acc_mode = ACC_MODE(flags);
+
+ BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
+ "VALID_OPEN_FLAGS must fit in 32 bits");
+ /* The whole point: OPENAT2_REGULAR must be unrepresentable in int. */
+ BUILD_BUG_ON_MSG(!upper_32_bits(OPENAT2_REGULAR),
+ "OPENAT2_REGULAR must live in the upper 32 bits of open_how::flags");
+ /* Prevent a future bit collision between UAPI and internal carrier. */
+ BUILD_BUG_ON_MSG(OPENAT2_REGULAR & VALID_OPEN_FLAGS,
+ "OPENAT2_REGULAR must not alias any open()/openat() flag");
+ BUILD_BUG_ON_MSG(__O_REGULAR & VALID_OPENAT2_FLAGS,
+ "__O_REGULAR must not alias any user-visible flag");
+
+ /*
+ * Strip flags that aren't relevant in determining struct open_flags.
+ */
+ flags &= ~strip;
+
+ /*
+ * Older syscalls implicitly clear all of the invalid flags or argument
+ * values before calling build_open_flags(), but openat2(2) checks all
+ * of its arguments.
+ */
+ if (flags & ~VALID_OPENAT2_FLAGS)
+ return -EINVAL;
+ if (how->resolve & ~VALID_RESOLVE_FLAGS)
+ return -EINVAL;
+
+ /* Scoping flags are mutually exclusive. */
+ if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
+ return -EINVAL;
+
+ /* Deal with the mode. */
+ if (WILL_CREATE(flags)) {
+ if (how->mode & ~S_IALLUGO)
+ return -EINVAL;
+ op->mode = how->mode | S_IFREG;
+ } else {
+ if (how->mode != 0)
+ return -EINVAL;
+ op->mode = 0;
+ }
+
+ /*
+ * Block bugs where O_DIRECTORY | O_CREAT created regular files.
+ * Note, that blocking O_DIRECTORY | O_CREAT here also protects
+ * O_TMPFILE below which requires O_DIRECTORY being raised.
+ */
+ if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
+ return -EINVAL;
+
+ /* Now handle the creative implementation of O_TMPFILE. */
+ if (flags & __O_TMPFILE) {
+ /*
+ * In order to ensure programs get explicit errors when trying
+ * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
+ * is raised alongside __O_TMPFILE.
+ */
+ if (!(flags & O_DIRECTORY))
+ return -EINVAL;
+ if (!(acc_mode & MAY_WRITE))
+ return -EINVAL;
+ }
+ /*
+ * Asking to open a directory and a regular file at the same time is
+ * contradictory.
+ */
+ if ((flags & (O_DIRECTORY | OPENAT2_REGULAR)) ==
+ (O_DIRECTORY | OPENAT2_REGULAR))
+ return -EINVAL;
+
+ if (flags & O_PATH) {
+ /* O_PATH only permits certain other flags to be set. */
+ if (flags & ~O_PATH_FLAGS)
+ return -EINVAL;
+ acc_mode = 0;
+ }
+
+ /*
+ * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
+ * check for O_DSYNC if the need any syncing at all we enforce it's
+ * always set instead of having to deal with possibly weird behaviour
+ * for malicious applications setting only __O_SYNC.
+ */
+ if (flags & __O_SYNC)
+ flags |= O_DSYNC;
+
+ /*
+ * Translate the upper-32-bit UAPI bit OPENAT2_REGULAR into the
+ * kernel-internal lower-32-bit __O_REGULAR carrier so the bit
+ * survives the assignment to op->open_flag (an int) below and the
+ * subsequent flow through f->f_flags (unsigned int) and the
+ * i_op->atomic_open() callback (unsigned). do_dentry_open() strips
+ * __O_REGULAR before the file becomes visible to userspace.
+ */
+ if (flags & OPENAT2_REGULAR) {
+ flags &= ~OPENAT2_REGULAR;
+ flags |= __O_REGULAR;
+ }
+
+ op->open_flag = flags;
+
+ /* O_TRUNC implies we need access checks for write permissions */
+ if (flags & O_TRUNC)
+ acc_mode |= MAY_WRITE;
+
+ /* Allow the LSM permission hook to distinguish append
+ access from general write access. */
+ if (flags & O_APPEND)
+ acc_mode |= MAY_APPEND;
+
+ op->acc_mode = acc_mode;
+
+ op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
+
+ if (flags & O_CREAT) {
+ op->intent |= LOOKUP_CREATE;
+ if (flags & O_EXCL) {
+ op->intent |= LOOKUP_EXCL;
+ flags |= O_NOFOLLOW;
+ }
+ }
+
+ if (flags & O_DIRECTORY)
+ lookup_flags |= LOOKUP_DIRECTORY;
+ if (!(flags & O_NOFOLLOW))
+ lookup_flags |= LOOKUP_FOLLOW;
+ if (flags & O_EMPTYPATH)
+ lookup_flags |= LOOKUP_EMPTY;
+
+ if (how->resolve & RESOLVE_NO_XDEV)
+ lookup_flags |= LOOKUP_NO_XDEV;
+ if (how->resolve & RESOLVE_NO_MAGICLINKS)
+ lookup_flags |= LOOKUP_NO_MAGICLINKS;
+ if (how->resolve & RESOLVE_NO_SYMLINKS)
+ lookup_flags |= LOOKUP_NO_SYMLINKS;
+ if (how->resolve & RESOLVE_BENEATH)
+ lookup_flags |= LOOKUP_BENEATH;
+ if (how->resolve & RESOLVE_IN_ROOT)
+ lookup_flags |= LOOKUP_IN_ROOT;
+ if (how->resolve & RESOLVE_CACHED) {
+ /* Don't bother even trying for create/truncate/tmpfile open */
+ if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE))
+ return -EAGAIN;
+ lookup_flags |= LOOKUP_CACHED;
+ }
+
+ op->lookup_flags = lookup_flags;
+ return 0;
+}
+
+struct file *file_open_root(const struct path *root,
+ const char *filename, int flags, umode_t mode)
+{
+ struct open_flags op;
+ struct open_how how = build_open_how(flags, mode);
+ int err = build_open_flags(&how, &op);
+ if (err)
+ return ERR_PTR(err);
+ return do_file_open_root(root, filename, &op);
+}
+EXPORT_SYMBOL(file_open_root);