mqueue_init_fs_context() captures current->nsproxy->ipc_ns and resets
fc->user_ns to ctx->ipc_ns->user_ns, overriding the user namespace
delegated by FSCONFIG_SET_RUN and leaving the context bound to the
caller's IPC namespace.

Wire up mqueue_set_run() to switch ctx->ipc_ns to the target
run's IPC namespace, update fc->user_ns to match, and mark mqueue
with FS_RUN_MOUNT.

Co-developed-by: David Howells <[email protected]>
Signed-off-by: David Howells <[email protected]>
Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 ipc/mqueue.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/ipc/mqueue.c b/ipc/mqueue.c
index 322c3dd98c44..f31df31c5053 100644
--- a/ipc/mqueue.c
+++ b/ipc/mqueue.c
@@ -30,6 +30,7 @@
 #include <linux/audit.h>
 #include <linux/signal.h>
 #include <linux/mutex.h>
+#include <linux/runfd.h>
 #include <linux/nsproxy.h>
 #include <linux/pid.h>
 #include <linux/ipc_namespace.h>
@@ -400,6 +401,20 @@ static struct inode *mqueue_get_inode(struct super_block 
*sb,
        return ERR_PTR(ret);
 }
 
+static int mqueue_set_run(struct fs_context *fc, struct run *run)
+{
+       struct mqueue_fs_context *ctx = fc->fs_private;
+       struct ipc_namespace *ipc_ns;
+
+       scoped_guard(spinlock, &run->lock)
+               ipc_ns = get_ipc_ns(run->ns->ipc_ns);
+       put_ipc_ns(ctx->ipc_ns);
+       ctx->ipc_ns = ipc_ns;
+       put_user_ns(fc->user_ns);
+       fc->user_ns = get_user_ns(ipc_ns->user_ns);
+       return 0;
+}
+
 static int mqueue_fill_super(struct super_block *sb, struct fs_context *fc)
 {
        struct inode *inode;
@@ -453,9 +468,16 @@ static int mqueue_init_fs_context(struct fs_context *fc)
        if (!ctx)
                return -ENOMEM;
 
-       ctx->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
-       put_user_ns(fc->user_ns);
-       fc->user_ns = get_user_ns(ctx->ipc_ns->user_ns);
+       /*
+        * Reconfiguration keeps the superblock's user namespace and does not
+        * use ctx->ipc_ns, which put_ipc_ns() could be tearing down via
+        * sb->s_fs_info.
+        */
+       if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE) {
+               ctx->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
+               put_user_ns(fc->user_ns);
+               fc->user_ns = get_user_ns(ctx->ipc_ns->user_ns);
+       }
        fc->fs_private = ctx;
        fc->ops = &mqueue_fs_context_ops;
        return 0;
@@ -1613,6 +1635,7 @@ static const struct super_operations mqueue_super_ops = {
 
 static const struct fs_context_operations mqueue_fs_context_ops = {
        .free           = mqueue_fs_context_free,
+       .set_run        = mqueue_set_run,
        .get_tree       = mqueue_get_tree,
 };
 
@@ -1620,7 +1643,7 @@ static struct file_system_type mqueue_fs_type = {
        .name                   = "mqueue",
        .init_fs_context        = mqueue_init_fs_context,
        .kill_sb                = kill_anon_super,
-       .fs_flags               = FS_USERNS_MOUNT,
+       .fs_flags               = FS_USERNS_MOUNT | FS_RUN_MOUNT,
 };
 
 int mq_init_ns(struct ipc_namespace *ns)
-- 
2.47.3


Reply via email to