[RFC PATCH v9 22/29] ipc/mqueue: Implement ->set_nsexec() fs_context operation

Jarkko Sakkinen jarkko at kernel.org
Tue Dec 1 03:09:54 UTC 2026


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_NSEXEC and leaving the context bound to the
caller's IPC namespace.

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

Co-developed-by: David Howells <dhowells at redhat.com>
Signed-off-by: David Howells <dhowells at redhat.com>
Signed-off-by: Jarkko Sakkinen <jarkko at kernel.org>
---
 ipc/mqueue.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/ipc/mqueue.c b/ipc/mqueue.c
index 322c3dd98c44..026462d9b40f 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/nsexec.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_nsexec(struct fs_context *fc, struct nsexec *nsexec)
+{
+	struct mqueue_fs_context *ctx = fc->fs_private;
+	struct ipc_namespace *ipc_ns;
+
+	scoped_guard(spinlock, &nsexec->lock)
+		ipc_ns = get_ipc_ns(nsexec->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,13 @@ 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);
+	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
+		ctx->ipc_ns = get_ipc_ns(fc->root->d_sb->s_fs_info);
+	} else {
+		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 +1632,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_nsexec	= mqueue_set_nsexec,
 	.get_tree	= mqueue_get_tree,
 };
 
@@ -1620,7 +1640,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_NSEXEC_MOUNT,
 };
 
 int mq_init_ns(struct ipc_namespace *ns)
-- 
2.47.3




More information about the Linux-security-module-archive mailing list