Linux filesystem development
 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
  2026-11-26 11:40 ` [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted Christian Brauner
  0 siblings, 2 replies; 7+ 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] 7+ 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
  2026-11-27  8:58   ` Alexander Mikhalitsyn
  2026-11-26 11:40 ` [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted Christian Brauner
  1 sibling, 2 replies; 7+ 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] 7+ messages in thread

* [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted
  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-26 11:40 ` Christian Brauner
  2026-11-27  8:24   ` Alexander Mikhalitsyn
  1 sibling, 1 reply; 7+ 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)

Check that the ephemeral procfs files can be the source of a mount but
can't be mounted on:

- bind mounts of fd, fdinfo and net files, including over /proc/self/exe
  as libpathrs does
- open_tree() and move_mount() of an fdinfo file
- moving a mount of an fdinfo file from a slave to its master
- mounting on top of an fdinfo file
- moving an fdinfo file beneath a mount
- a propagated copy that would end up beneath a mount in a slave
- the same after a peer got its copy

The last four have to keep failing with ENOENT.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 .../selftests/filesystems/mount_cycle/.gitignore   |   1 +
 .../selftests/filesystems/mount_cycle/Makefile     |   2 +-
 .../filesystems/mount_cycle/procfs_source_test.c   | 283 +++++++++++++++++++++
 3 files changed, 285 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/filesystems/mount_cycle/.gitignore b/tools/testing/selftests/filesystems/mount_cycle/.gitignore
index 30f7dd071bcbb..514e7295abb4b 100644
--- a/tools/testing/selftests/filesystems/mount_cycle/.gitignore
+++ b/tools/testing/selftests/filesystems/mount_cycle/.gitignore
@@ -6,3 +6,4 @@ mount_cover_test
 locked_handle_test
 nsfs_rbind_loop_test
 overmount_ns_file_test
+procfs_source_test
diff --git a/tools/testing/selftests/filesystems/mount_cycle/Makefile b/tools/testing/selftests/filesystems/mount_cycle/Makefile
index fbe8c5e19d424..a2afe7090556a 100644
--- a/tools/testing/selftests/filesystems/mount_cycle/Makefile
+++ b/tools/testing/selftests/filesystems/mount_cycle/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 TEST_GEN_PROGS := loop_cycle_test unmounted_tree_test overmount_reparent_test mount_cover_test locked_handle_test
-TEST_GEN_PROGS += nsfs_rbind_loop_test overmount_ns_file_test
+TEST_GEN_PROGS += nsfs_rbind_loop_test overmount_ns_file_test procfs_source_test
 
 CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
 
diff --git a/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.c b/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.c
new file mode 100644
index 0000000000000..ece21f22e589e
--- /dev/null
+++ b/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.c
@@ -0,0 +1,283 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * The files in /proc/<pid>/fd, /proc/<pid>/fdinfo and below /proc/<pid>/net
+ * can't be mounted on but they can be bind-mounted elsewhere.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+#include "../wrappers.h"
+#include "../../kselftest_harness.h"
+
+#ifndef MOVE_MOUNT_BENEATH
+#define MOVE_MOUNT_BENEATH 0x00000200
+#endif
+
+#define DIR_LEN 64
+#define PATH_LEN 128
+
+/* Open the procfs file itself, not the file it refers to. */
+static int open_entry(const char *dir, int fd)
+{
+	char path[PATH_LEN];
+
+	snprintf(path, sizeof(path), "/proc/self/%s/%d", dir, fd);
+	return open(path, O_PATH | O_NOFOLLOW | O_CLOEXEC);
+}
+
+static int bind_entry(int pfd, const char *target)
+{
+	char src[PATH_LEN];
+
+	snprintf(src, sizeof(src), "/proc/self/fd/%d", pfd);
+	return mount(src, target, NULL, MS_BIND, NULL);
+}
+
+/* An fdinfo file starts with the position of the file. */
+static bool is_fdinfo(const char *path)
+{
+	char buf[5] = {};
+	int fd;
+	bool ret;
+
+	fd = open(path, O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return false;
+	ret = read(fd, buf, 4) == 4 && !strcmp(buf, "pos:");
+	close(fd);
+	return ret;
+}
+
+/* A shared tmpfs on @dir/a with a slave on @dir/b, both showing a file f. */
+static int setup_slave(const char *dir, char *af, char *bf)
+{
+	char a[PATH_LEN], b[PATH_LEN];
+
+	snprintf(a, sizeof(a), "%s/a", dir);
+	snprintf(b, sizeof(b), "%s/b", dir);
+	snprintf(af, PATH_LEN, "%s/a/f", dir);
+	snprintf(bf, PATH_LEN, "%s/b/f", dir);
+	if (mkdir(a, 0755) || mkdir(b, 0755) ||
+	    mount("tmpfs", a, "tmpfs", 0, NULL) ||
+	    mount(NULL, a, NULL, MS_SHARED, NULL) ||
+	    mount(a, b, NULL, MS_BIND, NULL) ||
+	    mount(NULL, b, NULL, MS_SLAVE, NULL))
+		return -1;
+	return mknod(af, S_IFREG | 0644, 0);
+}
+
+FIXTURE(procfs_source) {
+	char dir[DIR_LEN];
+	char file[PATH_LEN];
+	int fd;
+};
+
+FIXTURE_SETUP(procfs_source)
+{
+	if (geteuid())
+		SKIP(return, "test requires root");
+
+	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
+	ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
+	snprintf(self->dir, sizeof(self->dir), "/tmp/procfs_source.XXXXXX");
+	ASSERT_NE(mkdtemp(self->dir), NULL);
+	ASSERT_EQ(mount("tmpfs", self->dir, "tmpfs", 0, NULL), 0);
+	snprintf(self->file, sizeof(self->file), "%s/file", self->dir);
+	self->fd = open(self->file, O_RDWR | O_CREAT | O_CLOEXEC, 0644);
+	ASSERT_GE(self->fd, 0);
+}
+
+FIXTURE_TEARDOWN(procfs_source)
+{
+	umount2(self->dir, MNT_DETACH);
+	rmdir(self->dir);
+}
+
+TEST_F(procfs_source, bind_fd)
+{
+	struct stat st;
+	int pfd;
+
+	pfd = open_entry("fd", self->fd);
+	ASSERT_GE(pfd, 0);
+	ASSERT_EQ(bind_entry(pfd, self->file), 0);
+	ASSERT_EQ(lstat(self->file, &st), 0);
+	ASSERT_TRUE(S_ISLNK(st.st_mode));
+	close(pfd);
+}
+
+TEST_F(procfs_source, bind_fdinfo)
+{
+	int pfd;
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	ASSERT_EQ(bind_entry(pfd, self->file), 0);
+	ASSERT_TRUE(is_fdinfo(self->file));
+	close(pfd);
+}
+
+/* An entry below /proc/<pid>/net can't be mounted on either. */
+TEST_F(procfs_source, bind_net)
+{
+	char buf[7] = {};
+	int fd;
+
+	if (access("/proc/self/net/dev", F_OK))
+		SKIP(return, "no /proc/self/net/dev");
+	ASSERT_EQ(mount("/proc/self/net/dev", self->file, NULL, MS_BIND, NULL), 0);
+	fd = open(self->file, O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(fd, 0);
+	ASSERT_EQ(read(fd, buf, 6), 6);
+	ASSERT_STREQ(buf, "Inter-");
+	close(fd);
+}
+
+/* What libpathrs does to test that it notices overmounts in procfs. */
+TEST_F(procfs_source, bind_fd_on_exe)
+{
+	char target[PATH_LEN];
+	int pfd, exe;
+
+	exe = open("/proc/self/exe", O_PATH | O_NOFOLLOW | O_CLOEXEC);
+	ASSERT_GE(exe, 0);
+	pfd = open_entry("fd", self->fd);
+	ASSERT_GE(pfd, 0);
+	snprintf(target, sizeof(target), "/proc/self/fd/%d", exe);
+	ASSERT_EQ(bind_entry(pfd, target), 0);
+	close(pfd);
+	close(exe);
+}
+
+TEST_F(procfs_source, open_tree_fdinfo)
+{
+	int pfd, tfd;
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	tfd = sys_open_tree(pfd, "", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
+	ASSERT_GE(tfd, 0);
+	ASSERT_EQ(sys_move_mount(tfd, "", AT_FDCWD, self->file, MOVE_MOUNT_F_EMPTY_PATH), 0);
+	ASSERT_TRUE(is_fdinfo(self->file));
+	close(tfd);
+	close(pfd);
+}
+
+TEST_F(procfs_source, no_mount_on_fdinfo)
+{
+	char target[PATH_LEN];
+	int pfd;
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	snprintf(target, sizeof(target), "/proc/self/fd/%d", pfd);
+	ASSERT_NE(mount(self->file, target, NULL, MS_BIND, NULL), 0);
+	ASSERT_EQ(errno, ENOENT);
+	close(pfd);
+}
+
+/* Moving beneath would put the existing mount on top of the fdinfo file. */
+TEST_F(procfs_source, no_tuck_beneath_fdinfo)
+{
+	char other[PATH_LEN];
+	int pfd, tfd;
+
+	snprintf(other, sizeof(other), "%s/other", self->dir);
+	ASSERT_EQ(mknod(other, S_IFREG | 0644, 0), 0);
+	ASSERT_EQ(mount(other, self->file, NULL, MS_BIND, NULL), 0);
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	tfd = sys_open_tree(pfd, "", OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
+	ASSERT_GE(tfd, 0);
+	ASSERT_NE(sys_move_mount(tfd, "", AT_FDCWD, self->file,
+				 MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH), 0);
+	ASSERT_EQ(errno, ENOENT);
+	ASSERT_FALSE(is_fdinfo(self->file));
+	ASSERT_EQ(umount2(self->file, 0), 0);
+	ASSERT_NE(umount2(self->file, 0), 0);
+	close(tfd);
+	close(pfd);
+}
+
+/* A copy in a slave would be tucked beneath the slave's own mount. */
+TEST_F(procfs_source, no_tuck_beneath_fdinfo_copy)
+{
+	char af[PATH_LEN], bf[PATH_LEN];
+	int pfd;
+
+	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
+	ASSERT_EQ(mount(self->file, bf, NULL, MS_BIND, NULL), 0);
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	ASSERT_NE(bind_entry(pfd, af), 0);
+	ASSERT_EQ(errno, ENOENT);
+	ASSERT_FALSE(is_fdinfo(af));
+
+	/* Without the mount in the slave the copy goes there. */
+	ASSERT_EQ(umount2(bf, 0), 0);
+	ASSERT_EQ(bind_entry(pfd, af), 0);
+	ASSERT_TRUE(is_fdinfo(af));
+	ASSERT_TRUE(is_fdinfo(bf));
+	close(pfd);
+}
+
+/* A peer gets its copy before the copy for the slave fails. */
+TEST_F(procfs_source, no_tuck_beneath_fdinfo_after_peer)
+{
+	char af[PATH_LEN], bf[PATH_LEN], a[PATH_LEN], c[PATH_LEN], cf[PATH_LEN];
+	int pfd;
+
+	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
+	snprintf(a, sizeof(a), "%s/a", self->dir);
+	snprintf(c, sizeof(c), "%s/c", self->dir);
+	snprintf(cf, sizeof(cf), "%s/c/f", self->dir);
+	ASSERT_EQ(mkdir(c, 0755), 0);
+	ASSERT_EQ(mount(a, c, NULL, MS_BIND, NULL), 0);
+	ASSERT_EQ(mount(self->file, bf, NULL, MS_BIND, NULL), 0);
+
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	ASSERT_NE(bind_entry(pfd, af), 0);
+	ASSERT_EQ(errno, ENOENT);
+	ASSERT_FALSE(is_fdinfo(af));
+	ASSERT_FALSE(is_fdinfo(cf));
+
+	ASSERT_EQ(umount2(bf, 0), 0);
+	ASSERT_EQ(bind_entry(pfd, af), 0);
+	ASSERT_TRUE(is_fdinfo(af));
+	ASSERT_TRUE(is_fdinfo(bf));
+	ASSERT_TRUE(is_fdinfo(cf));
+	close(pfd);
+}
+
+/* Moved off a slave, its copy goes where the mount came from. */
+TEST_F(procfs_source, move_fdinfo_off_slave)
+{
+	char af[PATH_LEN], bf[PATH_LEN];
+	int pfd;
+
+	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
+	pfd = open_entry("fdinfo", self->fd);
+	ASSERT_GE(pfd, 0);
+	ASSERT_EQ(bind_entry(pfd, bf), 0);
+	ASSERT_EQ(mount(bf, af, NULL, MS_MOVE, NULL), 0);
+	ASSERT_TRUE(is_fdinfo(af));
+	ASSERT_TRUE(is_fdinfo(bf));
+	/* only the copy is left in the slave */
+	ASSERT_EQ(umount2(bf, 0), 0);
+	ASSERT_FALSE(is_fdinfo(bf));
+	close(pfd);
+}
+
+TEST_HARNESS_MAIN

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ 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
  2026-11-27  8:58   ` Alexander Mikhalitsyn
  1 sibling, 1 reply; 7+ 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] 7+ messages in thread

* Re: [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted
  2026-11-26 11:40 ` [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted Christian Brauner
@ 2026-11-27  8:24   ` Alexander Mikhalitsyn
  0 siblings, 0 replies; 7+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-27  8:24 UTC (permalink / raw)
  To: Christian Brauner, linux-fsdevel; +Cc: Alexander Viro, Jan Kara, Aleksa Sarai

On Thu, 2026-11-26 at 12:40 +0100, Christian Brauner wrote:
> Check that the ephemeral procfs files can be the source of a mount
> but
> can't be mounted on:
> 
> - bind mounts of fd, fdinfo and net files, including over
> /proc/self/exe
>   as libpathrs does
> - open_tree() and move_mount() of an fdinfo file
> - moving a mount of an fdinfo file from a slave to its master
> - mounting on top of an fdinfo file
> - moving an fdinfo file beneath a mount
> - a propagated copy that would end up beneath a mount in a slave
> - the same after a peer got its copy
> 
> The last four have to keep failing with ENOENT.
> 
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

> ---
>  .../selftests/filesystems/mount_cycle/.gitignore   |   1 +
>  .../selftests/filesystems/mount_cycle/Makefile     |   2 +-
>  .../filesystems/mount_cycle/procfs_source_test.c   | 283
> +++++++++++++++++++++
>  3 files changed, 285 insertions(+), 1 deletion(-)
> 
> diff --git
> a/tools/testing/selftests/filesystems/mount_cycle/.gitignore
> b/tools/testing/selftests/filesystems/mount_cycle/.gitignore
> index 30f7dd071bcbb..514e7295abb4b 100644
> --- a/tools/testing/selftests/filesystems/mount_cycle/.gitignore
> +++ b/tools/testing/selftests/filesystems/mount_cycle/.gitignore
> @@ -6,3 +6,4 @@ mount_cover_test
>  locked_handle_test
>  nsfs_rbind_loop_test
>  overmount_ns_file_test
> +procfs_source_test
> diff --git a/tools/testing/selftests/filesystems/mount_cycle/Makefile
> b/tools/testing/selftests/filesystems/mount_cycle/Makefile
> index fbe8c5e19d424..a2afe7090556a 100644
> --- a/tools/testing/selftests/filesystems/mount_cycle/Makefile
> +++ b/tools/testing/selftests/filesystems/mount_cycle/Makefile
> @@ -1,6 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0
>  TEST_GEN_PROGS := loop_cycle_test unmounted_tree_test
> overmount_reparent_test mount_cover_test locked_handle_test
> -TEST_GEN_PROGS += nsfs_rbind_loop_test overmount_ns_file_test
> +TEST_GEN_PROGS += nsfs_rbind_loop_test overmount_ns_file_test
> procfs_source_test
>  
>  CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
>  
> diff --git
> a/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.
> c
> b/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.
> c
> new file mode 100644
> index 0000000000000..ece21f22e589e
> --- /dev/null
> +++
> b/tools/testing/selftests/filesystems/mount_cycle/procfs_source_test.
> c
> @@ -0,0 +1,283 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * The files in /proc/<pid>/fd, /proc/<pid>/fdinfo and below
> /proc/<pid>/net
> + * can't be mounted on but they can be bind-mounted elsewhere.
> + */
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <sched.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/mount.h>
> +#include <sys/stat.h>
> +
> +#include "../wrappers.h"
> +#include "../../kselftest_harness.h"
> +
> +#ifndef MOVE_MOUNT_BENEATH
> +#define MOVE_MOUNT_BENEATH 0x00000200
> +#endif
> +
> +#define DIR_LEN 64
> +#define PATH_LEN 128
> +
> +/* Open the procfs file itself, not the file it refers to. */
> +static int open_entry(const char *dir, int fd)
> +{
> +	char path[PATH_LEN];
> +
> +	snprintf(path, sizeof(path), "/proc/self/%s/%d", dir, fd);
> +	return open(path, O_PATH | O_NOFOLLOW | O_CLOEXEC);
> +}
> +
> +static int bind_entry(int pfd, const char *target)
> +{
> +	char src[PATH_LEN];
> +
> +	snprintf(src, sizeof(src), "/proc/self/fd/%d", pfd);
> +	return mount(src, target, NULL, MS_BIND, NULL);
> +}
> +
> +/* An fdinfo file starts with the position of the file. */
> +static bool is_fdinfo(const char *path)
> +{
> +	char buf[5] = {};
> +	int fd;
> +	bool ret;
> +
> +	fd = open(path, O_RDONLY | O_CLOEXEC);
> +	if (fd < 0)
> +		return false;
> +	ret = read(fd, buf, 4) == 4 && !strcmp(buf, "pos:");
> +	close(fd);
> +	return ret;
> +}
> +
> +/* A shared tmpfs on @dir/a with a slave on @dir/b, both showing a
> file f. */
> +static int setup_slave(const char *dir, char *af, char *bf)
> +{
> +	char a[PATH_LEN], b[PATH_LEN];
> +
> +	snprintf(a, sizeof(a), "%s/a", dir);
> +	snprintf(b, sizeof(b), "%s/b", dir);
> +	snprintf(af, PATH_LEN, "%s/a/f", dir);
> +	snprintf(bf, PATH_LEN, "%s/b/f", dir);
> +	if (mkdir(a, 0755) || mkdir(b, 0755) ||
> +	    mount("tmpfs", a, "tmpfs", 0, NULL) ||
> +	    mount(NULL, a, NULL, MS_SHARED, NULL) ||
> +	    mount(a, b, NULL, MS_BIND, NULL) ||
> +	    mount(NULL, b, NULL, MS_SLAVE, NULL))
> +		return -1;
> +	return mknod(af, S_IFREG | 0644, 0);
> +}
> +
> +FIXTURE(procfs_source) {
> +	char dir[DIR_LEN];
> +	char file[PATH_LEN];
> +	int fd;
> +};
> +
> +FIXTURE_SETUP(procfs_source)
> +{
> +	if (geteuid())
> +		SKIP(return, "test requires root");
> +
> +	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
> +	ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL),
> 0);
> +	snprintf(self->dir, sizeof(self->dir),
> "/tmp/procfs_source.XXXXXX");
> +	ASSERT_NE(mkdtemp(self->dir), NULL);
> +	ASSERT_EQ(mount("tmpfs", self->dir, "tmpfs", 0, NULL), 0);
> +	snprintf(self->file, sizeof(self->file), "%s/file", self-
> >dir);
> +	self->fd = open(self->file, O_RDWR | O_CREAT | O_CLOEXEC,
> 0644);
> +	ASSERT_GE(self->fd, 0);
> +}
> +
> +FIXTURE_TEARDOWN(procfs_source)
> +{
> +	umount2(self->dir, MNT_DETACH);
> +	rmdir(self->dir);
> +}
> +
> +TEST_F(procfs_source, bind_fd)
> +{
> +	struct stat st;
> +	int pfd;
> +
> +	pfd = open_entry("fd", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	ASSERT_EQ(bind_entry(pfd, self->file), 0);
> +	ASSERT_EQ(lstat(self->file, &st), 0);
> +	ASSERT_TRUE(S_ISLNK(st.st_mode));
> +	close(pfd);
> +}
> +
> +TEST_F(procfs_source, bind_fdinfo)
> +{
> +	int pfd;
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	ASSERT_EQ(bind_entry(pfd, self->file), 0);
> +	ASSERT_TRUE(is_fdinfo(self->file));
> +	close(pfd);
> +}
> +
> +/* An entry below /proc/<pid>/net can't be mounted on either. */
> +TEST_F(procfs_source, bind_net)
> +{
> +	char buf[7] = {};
> +	int fd;
> +
> +	if (access("/proc/self/net/dev", F_OK))
> +		SKIP(return, "no /proc/self/net/dev");
> +	ASSERT_EQ(mount("/proc/self/net/dev", self->file, NULL,
> MS_BIND, NULL), 0);
> +	fd = open(self->file, O_RDONLY | O_CLOEXEC);
> +	ASSERT_GE(fd, 0);
> +	ASSERT_EQ(read(fd, buf, 6), 6);
> +	ASSERT_STREQ(buf, "Inter-");
> +	close(fd);
> +}
> +
> +/* What libpathrs does to test that it notices overmounts in procfs.
> */
> +TEST_F(procfs_source, bind_fd_on_exe)
> +{
> +	char target[PATH_LEN];
> +	int pfd, exe;
> +
> +	exe = open("/proc/self/exe", O_PATH | O_NOFOLLOW |
> O_CLOEXEC);
> +	ASSERT_GE(exe, 0);
> +	pfd = open_entry("fd", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	snprintf(target, sizeof(target), "/proc/self/fd/%d", exe);
> +	ASSERT_EQ(bind_entry(pfd, target), 0);
> +	close(pfd);
> +	close(exe);
> +}
> +
> +TEST_F(procfs_source, open_tree_fdinfo)
> +{
> +	int pfd, tfd;
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	tfd = sys_open_tree(pfd, "", OPEN_TREE_CLONE |
> OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
> +	ASSERT_GE(tfd, 0);
> +	ASSERT_EQ(sys_move_mount(tfd, "", AT_FDCWD, self->file,
> MOVE_MOUNT_F_EMPTY_PATH), 0);
> +	ASSERT_TRUE(is_fdinfo(self->file));
> +	close(tfd);
> +	close(pfd);
> +}
> +
> +TEST_F(procfs_source, no_mount_on_fdinfo)
> +{
> +	char target[PATH_LEN];
> +	int pfd;
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	snprintf(target, sizeof(target), "/proc/self/fd/%d", pfd);
> +	ASSERT_NE(mount(self->file, target, NULL, MS_BIND, NULL),
> 0);
> +	ASSERT_EQ(errno, ENOENT);
> +	close(pfd);
> +}
> +
> +/* Moving beneath would put the existing mount on top of the fdinfo
> file. */
> +TEST_F(procfs_source, no_tuck_beneath_fdinfo)
> +{
> +	char other[PATH_LEN];
> +	int pfd, tfd;
> +
> +	snprintf(other, sizeof(other), "%s/other", self->dir);
> +	ASSERT_EQ(mknod(other, S_IFREG | 0644, 0), 0);
> +	ASSERT_EQ(mount(other, self->file, NULL, MS_BIND, NULL), 0);
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	tfd = sys_open_tree(pfd, "", OPEN_TREE_CLONE |
> OPEN_TREE_CLOEXEC | AT_EMPTY_PATH);
> +	ASSERT_GE(tfd, 0);
> +	ASSERT_NE(sys_move_mount(tfd, "", AT_FDCWD, self->file,
> +				 MOVE_MOUNT_F_EMPTY_PATH |
> MOVE_MOUNT_BENEATH), 0);
> +	ASSERT_EQ(errno, ENOENT);
> +	ASSERT_FALSE(is_fdinfo(self->file));
> +	ASSERT_EQ(umount2(self->file, 0), 0);
> +	ASSERT_NE(umount2(self->file, 0), 0);
> +	close(tfd);
> +	close(pfd);
> +}
> +
> +/* A copy in a slave would be tucked beneath the slave's own mount.
> */
> +TEST_F(procfs_source, no_tuck_beneath_fdinfo_copy)
> +{
> +	char af[PATH_LEN], bf[PATH_LEN];
> +	int pfd;
> +
> +	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
> +	ASSERT_EQ(mount(self->file, bf, NULL, MS_BIND, NULL), 0);
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	ASSERT_NE(bind_entry(pfd, af), 0);
> +	ASSERT_EQ(errno, ENOENT);
> +	ASSERT_FALSE(is_fdinfo(af));
> +
> +	/* Without the mount in the slave the copy goes there. */
> +	ASSERT_EQ(umount2(bf, 0), 0);
> +	ASSERT_EQ(bind_entry(pfd, af), 0);
> +	ASSERT_TRUE(is_fdinfo(af));
> +	ASSERT_TRUE(is_fdinfo(bf));
> +	close(pfd);
> +}
> +
> +/* A peer gets its copy before the copy for the slave fails. */
> +TEST_F(procfs_source, no_tuck_beneath_fdinfo_after_peer)
> +{
> +	char af[PATH_LEN], bf[PATH_LEN], a[PATH_LEN], c[PATH_LEN],
> cf[PATH_LEN];
> +	int pfd;
> +
> +	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
> +	snprintf(a, sizeof(a), "%s/a", self->dir);
> +	snprintf(c, sizeof(c), "%s/c", self->dir);
> +	snprintf(cf, sizeof(cf), "%s/c/f", self->dir);
> +	ASSERT_EQ(mkdir(c, 0755), 0);
> +	ASSERT_EQ(mount(a, c, NULL, MS_BIND, NULL), 0);
> +	ASSERT_EQ(mount(self->file, bf, NULL, MS_BIND, NULL), 0);
> +
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	ASSERT_NE(bind_entry(pfd, af), 0);
> +	ASSERT_EQ(errno, ENOENT);
> +	ASSERT_FALSE(is_fdinfo(af));
> +	ASSERT_FALSE(is_fdinfo(cf));
> +
> +	ASSERT_EQ(umount2(bf, 0), 0);
> +	ASSERT_EQ(bind_entry(pfd, af), 0);
> +	ASSERT_TRUE(is_fdinfo(af));
> +	ASSERT_TRUE(is_fdinfo(bf));
> +	ASSERT_TRUE(is_fdinfo(cf));
> +	close(pfd);
> +}
> +
> +/* Moved off a slave, its copy goes where the mount came from. */
> +TEST_F(procfs_source, move_fdinfo_off_slave)
> +{
> +	char af[PATH_LEN], bf[PATH_LEN];
> +	int pfd;
> +
> +	ASSERT_EQ(setup_slave(self->dir, af, bf), 0);
> +	pfd = open_entry("fdinfo", self->fd);
> +	ASSERT_GE(pfd, 0);
> +	ASSERT_EQ(bind_entry(pfd, bf), 0);
> +	ASSERT_EQ(mount(bf, af, NULL, MS_MOVE, NULL), 0);
> +	ASSERT_TRUE(is_fdinfo(af));
> +	ASSERT_TRUE(is_fdinfo(bf));
> +	/* only the copy is left in the slave */
> +	ASSERT_EQ(umount2(bf, 0), 0);
> +	ASSERT_FALSE(is_fdinfo(bf));
> +	close(pfd);
> +}
> +
> +TEST_HARNESS_MAIN

^ permalink raw reply	[flat|nested] 7+ 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; 7+ 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] 7+ 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:58   ` Alexander Mikhalitsyn
  1 sibling, 0 replies; 7+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-27  8:58 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>

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

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

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

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

Thread overview: 7+ 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
2026-11-27  8:58   ` Alexander Mikhalitsyn
2026-11-26 11:40 ` [PATCH 2/2] selftests/filesystems: check that ephemeral procfs files can be bind-mounted Christian Brauner
2026-11-27  8:24   ` Alexander Mikhalitsyn

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