From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Al Viro <viro@zeniv.linux.org.uk>,
Miklos Szeredi <miklos@szeredi.hu>,
Jeff Layton <jlayton@kernel.org>,
Josef Bacik <josef@toxicpanda.com>,
Seth Forshee <sforshee@kernel.org>,
Christian Brauner <brauner@kernel.org>
Subject: [PATCH RFC 05/16] fs: add may_copy_tree()
Date: Fri, 21 Feb 2025 14:13:04 +0100 [thread overview]
Message-ID: <20250221-brauner-open_tree-v1-5-dbcfcb98c676@kernel.org> (raw)
In-Reply-To: <20250221-brauner-open_tree-v1-0-dbcfcb98c676@kernel.org>
Add a helper that verifies whether a caller may copy a given mount tree.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/namespace.c | 44 +++++++++++++++++++++++++++++++++++++-------
1 file changed, 37 insertions(+), 7 deletions(-)
diff --git a/fs/namespace.c b/fs/namespace.c
index 2cffcda8a48e..c61b9704499a 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2808,6 +2808,41 @@ static int do_change_type(struct path *path, int ms_flags)
return err;
}
+/* may_copy_tree() - check if a mount tree can be copied
+ * @path: path to the mount tree to be copied
+ *
+ * This helper checks if the caller may copy the mount tree starting
+ * from @path->mnt. The caller may copy the mount tree under the
+ * following circumstances:
+ *
+ * (1) The caller is located in the mount namespace of the mount tree.
+ * This also implies that the mount does not belong to an anonymous
+ * mount namespace.
+ * (2) The caller tries to copy an nfs mount referring to a mount
+ * namespace, i.e., the caller is trying to copy a mount namespace
+ * entry from nsfs.
+ * (3) The caller tries to copy a pidfs mount referring to a pidfd.
+ *
+ * Returns true if the mount tree can be copied, false otherwise.
+ */
+static inline bool may_copy_tree(struct path *path)
+{
+ struct mount *mnt = real_mount(path->mnt);
+ const struct dentry_operations *d_op;
+
+ if (check_mnt(mnt))
+ return true;
+
+ d_op = path->dentry->d_op;
+ if (d_op == &ns_dentry_operations)
+ return true;
+
+ if (d_op == &pidfs_dentry_operations)
+ return true;
+
+ return false;
+}
+
static struct mount *__do_loopback(struct path *old_path, int recurse)
{
struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
@@ -2815,13 +2850,8 @@ static struct mount *__do_loopback(struct path *old_path, int recurse)
if (IS_MNT_UNBINDABLE(old))
return mnt;
- if (!check_mnt(old)) {
- const struct dentry_operations *d_op = old_path->dentry->d_op;
-
- if (d_op != &ns_dentry_operations &&
- d_op != &pidfs_dentry_operations)
- return mnt;
- }
+ if (!may_copy_tree(old_path))
+ return mnt;
if (!recurse && has_locked_children(old, old_path->dentry))
return mnt;
--
2.47.2
next prev parent reply other threads:[~2025-02-21 13:13 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-21 13:12 [PATCH RFC 00/16] fs: expand abilities of anonymous mount namespaces Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 01/16] fs: record sequence number of origin mount namespace Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 02/16] fs: add mnt_ns_empty() helper Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 03/16] fs: add assert for move_mount() Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 04/16] fs: add fastpath for dissolve_on_fput() Christian Brauner
2025-02-21 13:13 ` Christian Brauner [this message]
2025-02-21 13:13 ` [PATCH RFC 06/16] fs: create detached mounts from detached mounts Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 07/16] selftests: " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 08/16] fs: support getname_maybe_null() in move_mount() Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 09/16] fs: mount detached mounts onto detached mounts Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 10/16] selftests: first test for mounting " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 11/16] selftests: second " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 12/16] selftests: third " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 13/16] selftests: fourth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 14/16] selftests: fifth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 15/16] selftests: sixth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 16/16] selftests: seventh " 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=20250221-brauner-open_tree-v1-5-dbcfcb98c676@kernel.org \
--to=brauner@kernel.org \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=sforshee@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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