Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 0/2] namespace: allow bind mounts of ephemeral procfs files again
@ 2026-11-26 11:40 Christian Brauner
  2026-11-26 11:40 ` [PATCH 1/2] " Christian Brauner
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Brauner @ 2026-11-26 11:40 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Aleksa Sarai,
	Christian Brauner (Amutable), stable

Bind-mounting of files in /proc/<pid>/{fd,fdinfo,map_files} and below
/proc/<pid>/net got broken with commit 75f4197f1e11 ("dcache: don't put
a mountpoint on a dentry that's being removed").

procfs calls dont_mount() on these files so nothing can be mounted on
top of them. But 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 them fails with ENOENT now. libpathrs does exactly that in its
tests and it's broken in stable too.

Only refuse such a root when the source or one of its copies would go
beneath a mount. The race the original commit closed stays closed. The
selftest checks that these files can be bind-mounted but still can't be
mounted on.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (2):
      namespace: allow bind mounts of ephemeral procfs files again
      selftests/filesystems: check that ephemeral procfs files can be bind-mounted

 fs/dcache.c                                        |   5 +-
 fs/namespace.c                                     |  25 ++
 fs/pnode.c                                         |   6 +
 fs/pnode.h                                         |   2 +
 .../selftests/filesystems/mount_cycle/.gitignore   |   1 +
 .../selftests/filesystems/mount_cycle/Makefile     |   2 +-
 .../filesystems/mount_cycle/procfs_source_test.c   | 283 +++++++++++++++++++++
 7 files changed, 320 insertions(+), 4 deletions(-)
---
base-commit: 2dc60bd9975fc8666a13d58410499e06e6799ba9
change-id: 20261125-work-mount-cant_mount-6d744b18a9d0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] namespace: allow bind mounts of ephemeral procfs files again
  2026-11-26 11:40 [PATCH 0/2] namespace: allow bind mounts of ephemeral procfs files again Christian Brauner
@ 2026-11-26 11:40 ` Christian Brauner
  2026-11-27  8:22   ` Alexander Mikhalitsyn
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Brauner @ 2026-11-26 11:40 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Aleksa Sarai,
	Christian Brauner (Amutable), stable

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/<pid>/{fd,fdinfo,map_files} and below /proc/<pid>/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 <cyphar@cyphar.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] namespace: allow bind mounts of ephemeral procfs files again
  2026-11-26 11:40 ` [PATCH 1/2] " Christian Brauner
@ 2026-11-27  8:22   ` Alexander Mikhalitsyn
  2026-11-27  8:53     ` Christian Brauner
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-27  8:22 UTC (permalink / raw)
  To: Christian Brauner, linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Aleksa Sarai, stable

On Thu, 2026-11-26 at 12:40 +0100, Christian Brauner wrote:
> 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/<pid>/{fd,fdinfo,map_files} and below /proc/<pid>/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 <cyphar@cyphar.com>
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> ---
>  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

Hi Christian,

> @@ -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;
> +			}

Please correct me if I'm wrong, but shouldn't we do this check before
count_mounts()? AFAIU, it changes ns->pending_mounts and then I can't
see where we clean it up on the error path.

Kind regards,
Alex

>  			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);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] namespace: allow bind mounts of ephemeral procfs files again
  2026-11-27  8:22   ` Alexander Mikhalitsyn
@ 2026-11-27  8:53     ` Christian Brauner
  0 siblings, 0 replies; 4+ messages in thread
From: Christian Brauner @ 2026-11-27  8:53 UTC (permalink / raw)
  To: Alexander Mikhalitsyn
  Cc: linux-fsdevel, Alexander Viro, Jan Kara, Aleksa Sarai, stable

On Fri, Nov 27, 2026 at 09:22:03AM +0100, Alexander Mikhalitsyn wrote:
> On Thu, 2026-11-26 at 12:40 +0100, Christian Brauner wrote:
> > 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/<pid>/{fd,fdinfo,map_files} and below /proc/<pid>/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 <cyphar@cyphar.com>
> > Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> > ---
> >  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
> 
> Hi Christian,
> 
> > @@ -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;
> > +			}
> 
> Please correct me if I'm wrong, but shouldn't we do this check before
> count_mounts()? AFAIU, it changes ns->pending_mounts and then I can't
> see where we clean it up on the error path.

ns->pending_mounts is reset in the error path of
attach_recursive_mounts().

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-27  8:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 11:40 [PATCH 0/2] namespace: allow bind mounts of ephemeral procfs files again Christian Brauner
2026-11-26 11:40 ` [PATCH 1/2] " Christian Brauner
2026-11-27  8:22   ` Alexander Mikhalitsyn
2026-11-27  8:53     ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox