fs: record sequence number of origin mount namespace
authorChristian Brauner <brauner@kernel.org>
Fri, 21 Feb 2025 13:13:00 +0000 (14:13 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 4 Mar 2025 08:29:19 +0000 (09:29 +0100)
Store the sequence number of the mount namespace the anonymous mount
namespace has been created from. This information will be used in
follow-up patches.

Link: https://lore.kernel.org/r/20250221-brauner-open_tree-v1-1-dbcfcb98c676@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/mount.h
fs/namespace.c

index ffb613cdfeee97b99fe9419e8152c166e0a9ac83..820a79f1f7355d6d74c9157b3749f7d5aa1d29a5 100644 (file)
@@ -20,6 +20,7 @@ struct mnt_namespace {
                wait_queue_head_t       poll;
                struct rcu_head         mnt_ns_rcu;
        };
+       u64                     seq_origin; /* Sequence number of origin mount namespace */
        u64 event;
        unsigned int            nr_mounts; /* # of mounts in the namespace */
        unsigned int            pending_mounts;
index a3ed3f2980cbae6238cda09874e2dac146080eb6..d5c85b6ecb9878c271c319ef572ac51c33d7f296 100644 (file)
@@ -2853,15 +2853,30 @@ out:
 
 static struct file *open_detached_copy(struct path *path, bool recursive)
 {
-       struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
-       struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
+       struct mnt_namespace *ns, *mnt_ns = current->nsproxy->mnt_ns, *src_mnt_ns;
+       struct user_namespace *user_ns = mnt_ns->user_ns;
        struct mount *mnt, *p;
        struct file *file;
 
+       ns = alloc_mnt_ns(user_ns, true);
        if (IS_ERR(ns))
                return ERR_CAST(ns);
 
        namespace_lock();
+
+       /*
+        * Record the sequence number of the source mount namespace.
+        * This needs to hold namespace_sem to ensure that the mount
+        * doesn't get attached.
+        */
+       if (is_mounted(path->mnt)) {
+               src_mnt_ns = real_mount(path->mnt)->mnt_ns;
+               if (is_anon_ns(src_mnt_ns))
+                       ns->seq_origin = src_mnt_ns->seq_origin;
+               else
+                       ns->seq_origin = src_mnt_ns->seq;
+       }
+
        mnt = __do_loopback(path, recursive);
        if (IS_ERR(mnt)) {
                namespace_unlock();