Commit 75f4197f1e11 ("dcache: don't put a mountpoint on a dentry that's being removed") made d_set_mounted() refuse dentries that dont_mount() was called on. I overlooked that procfs calls dont_mount() on the files in /proc//{fd,fdinfo,map_files} and below /proc//net when it creates them and that attach_recursive_mnt() makes a mountpoint out of the root of every source in case it goes beneath a mount. So bind-mounting one of these files fails with ENOENT now. libpathrs does that in its tests, e.g., by bind-mounting /proc/1/fd/0 over /proc/self/exe. Nothing may be mounted on top of these files but they can be the source of a mount. So drop the check from d_set_mounted() again and refuse such a root only if the source or one of its copies would go beneath a mount that is already there. attach_recursive_mnt() checks the source and propagate_mnt() checks each copy right after making it. The root is a mountpoint by then so either we see the flag or detach_mounts() finds the mount, same as before. Fixes: 75f4197f1e11 ("dcache: don't put a mountpoint on a dentry that's being removed") Cc: stable@vger.kernel.org Reported-by: Aleksa Sarai Signed-off-by: Christian Brauner (Amutable) --- fs/dcache.c | 5 ++--- fs/namespace.c | 25 +++++++++++++++++++++++++ fs/pnode.c | 6 ++++++ fs/pnode.h | 2 ++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index ba17f49f8a636..b734ce65e6377 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -1628,8 +1628,7 @@ static __cacheline_aligned_in_smp DEFINE_SEQLOCK(d_set_mounted_lock); * * Only one of d_invalidate() and d_set_mounted() must succeed. For * this reason take rename_lock, d_set_mounted_lock and d_lock on dentry - * and ancestors. Likewise for dont_mount() which marks a dentry that - * is being removed under d_lock. + * and ancestors. */ int d_set_mounted(struct dentry *dentry) { @@ -1647,7 +1646,7 @@ int d_set_mounted(struct dentry *dentry) spin_unlock(&p->d_lock); } spin_lock(&dentry->d_lock); - if (!d_unlinked(dentry) && !cant_mount(dentry)) { + if (!d_unlinked(dentry)) { ret = -EBUSY; if (!d_mountpoint(dentry)) { dentry->d_flags |= DCACHE_MOUNTED; diff --git a/fs/namespace.c b/fs/namespace.c index aecd05ac01705..443a8e3b74f9b 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2561,6 +2561,21 @@ int count_mounts(struct mnt_namespace *ns, struct mount *mnt) return 0; } +/* + * Would @source_mnt or a copy of it go beneath a mount that is already at + * @mp on @parent? Call under namespace_sem, without mount_lock. + */ +bool mount_goes_beneath(struct mount *parent, struct mountpoint *mp, + struct mount *source_mnt) +{ + struct mount *q; + + guard(mount_locked_reader)(); + q = __lookup_mnt(&parent->mnt, mp->m_dentry); + /* a moved @source_mnt is gone from there by then */ + return q && q != source_mnt; +} + enum mnt_tree_flags_t { MNT_TREE_BENEATH = BIT(0), MNT_TREE_PROPAGATION = BIT(1), @@ -2677,6 +2692,16 @@ static int attach_recursive_mnt(struct mount *source_mnt, if (err) return err; + /* + * Don't mount on top of a root that dont_mount() was called on. It's + * a mountpoint now so a later detach_mounts() will find it. + */ + if (cant_mount(root.mp->m_dentry) && + mount_goes_beneath(dest_mnt, dest_mp, source_mnt)) { + err = -ENOENT; + goto out; + } + /* Is there space to add these mounts to the mount namespace? */ if (!moving) { err = count_mounts(ns, source_mnt); diff --git a/fs/pnode.c b/fs/pnode.c index 5f07fa14099bf..4514e75311972 100644 --- a/fs/pnode.c +++ b/fs/pnode.c @@ -351,6 +351,12 @@ int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp, err = count_mounts(n->mnt_ns, this); if (err) break; + /* what is already there would end up on this root */ + if (cant_mount(topmost_overmount(this)->mnt.mnt_root) && + mount_goes_beneath(n, dest_mp, source_mnt)) { + err = -ENOENT; + break; + } type = CL_MAKE_SHARED; } while ((n = next_peer(n)) != m); } diff --git a/fs/pnode.h b/fs/pnode.h index b029db225f33c..0fb217c48f17a 100644 --- a/fs/pnode.h +++ b/fs/pnode.h @@ -59,6 +59,8 @@ struct mount *copy_tree(struct mount *, struct dentry *, int); bool is_path_reachable(struct mount *, struct dentry *, const struct path *root); int count_mounts(struct mnt_namespace *ns, struct mount *mnt); +bool mount_goes_beneath(struct mount *parent, struct mountpoint *mp, + struct mount *source_mnt); bool propagation_would_overmount(const struct mount *from, const struct mount *to, const struct mountpoint *mp); -- 2.53.0