From: Snaipe <me@snai.pe>
To: linux-fsdevel@vger.kernel.org
Cc: linux-mm@kvack.org
Subject: [RFC PATCH 1/1] fs,ns: allow copying of shm_mnt mount trees
Date: Thu, 29 Jan 2026 18:35:15 +0100 [thread overview]
Message-ID: <20260129173515.1649305-2-me@snai.pe> (raw)
In-Reply-To: <20260129173515.1649305-1-me@snai.pe>
From: "Franklin \"Snaipe\" Mathieu" <me@snai.pe>
The main motivation for this change is to be able to bind-mount memfd file
descriptors. Prior to this change, it was not easy for a process to
create a private in-memory handle that could then be bind-mounted.
A process had to have access to a tmpfs, create a file in it, call
open_tree on the resulting file descriptor, close the original file
descriptor, unlink the file, and then check that no other process raced
the process to open the new file. Doable, but not great for mounting
sensitive content like secrets.
With this change, it is now possible for a process to prepare a memfd,
and call open_tree on it:
int tmpfd = memfd_create("secret", 0);
fchmod(tmpfd, 0600);
write(tmpfd, "SecretKey", 9);
int treefd = open_tree(tmpfd, "", OPEN_TREE_CLONE|AT_EMPTY_PATH|AT_RECURSIVE);
move_mount(treefd, "", -1, "/secret.txt", MOVE_MOUNT_F_EMPTY_PATH);
Signed-off-by: Franklin "Snaipe" Mathieu <me@snai.pe>
---
fs/namespace.c | 8 ++++++++
mm/internal.h | 2 ++
mm/shmem.c | 2 +-
3 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/fs/namespace.c b/fs/namespace.c
index d82910f33dc4..f51ad2013662 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -38,6 +38,9 @@
#include "pnode.h"
#include "internal.h"
+/* For checking memfd bind-mounts via shm_mnt */
+#include "../mm/internal.h"
+
/* Maximum number of mounts in a mount namespace */
static unsigned int sysctl_mount_max __read_mostly = 100000;
@@ -2901,6 +2904,8 @@ static int do_change_type(const struct path *path, int ms_flags)
* (3) The caller tries to copy a pidfs mount referring to a pidfd.
* (4) The caller is trying to copy a mount tree that belongs to an
* anonymous mount namespace.
+ * (5) The caller is trying to copy a mount tree belonging to shm_mnt
+ * (e.g. bind-mounting a file descriptor obtained from memfd_create)
*
* For that to be safe, this helper enforces that the origin mount
* namespace the anonymous mount namespace was created from is the
@@ -2943,6 +2948,9 @@ static inline bool may_copy_tree(const struct path *path)
if (d_op == &pidfs_dentry_operations)
return true;
+ if (path->mnt == shm_mnt)
+ return true;
+
if (!is_mounted(path->mnt))
return false;
diff --git a/mm/internal.h b/mm/internal.h
index 1561fc2ff5b8..aa45c5576b16 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -24,6 +24,8 @@
struct folio_batch;
+extern struct vfsmount *shm_mnt __ro_after_init;
+
/*
* Maintains state across a page table move. The operation assumes both source
* and destination VMAs already exist and are specified by the user.
diff --git a/mm/shmem.c b/mm/shmem.c
index b9081b817d28..449d6bc813ae 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -43,7 +43,7 @@
#include <linux/unicode.h>
#include "swap.h"
-static struct vfsmount *shm_mnt __ro_after_init;
+struct vfsmount *shm_mnt __ro_after_init;
#ifdef CONFIG_SHMEM
/*
--
2.52.0
next prev parent reply other threads:[~2026-01-29 17:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-29 17:35 [RFC PATCH 0/1] Bind-mounting memfds Snaipe
2026-01-29 17:35 ` Snaipe [this message]
2026-01-30 8:38 ` [RFC PATCH 1/1] fs,ns: allow copying of shm_mnt mount trees Johannes Thumshirn
2026-01-30 14:17 ` Snaipe
2026-02-04 16:51 ` Christian Brauner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260129173515.1649305-2-me@snai.pe \
--to=me@snai.pe \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox