[RFC PATCH v5 09/25] nsfs: Add nsproxy_open_namespace()

From: Jarkko Sakkinen

Date: Fri Nov 20 2026 - 19:09:59 EST


Opening nsfs file descriptors currently operates on a live task via
procfs or pidfs, preventing callers that own a detached struct nsproxy
and struct user_namespace from opening member namespace file descriptors
directly.

Add nsproxy_open_namespace() to take a passive reference on the
requested namespace type (CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS,
CLONE_NEWIPC, CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, or
CLONE_NEWTIME) from a supplied nsproxy and user_ns pair and return an
nsfs file descriptor via open_namespace().

Co-developed-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
fs/nsfs.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
include/linux/nsfs.h | 2 ++
2 files changed, 66 insertions(+)

diff --git a/fs/nsfs.c b/fs/nsfs.c
index 8179d69da038..1de51cd47508 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -711,3 +711,67 @@ void nsproxy_ns_active_put(struct nsproxy *ns)
ns_ref_active_put(ns->time_ns);
ns_ref_active_put(ns->time_ns_for_children);
}
+
+int nsproxy_open_namespace(struct nsproxy *nsp, struct user_namespace *user_ns,
+ unsigned long nstype)
+{
+ struct ns_common *ns = NULL;
+
+ switch (nstype) {
+ case CLONE_NEWNS:
+ if (ns_ref_get(nsp->mnt_ns))
+ ns = to_ns_common(nsp->mnt_ns);
+ break;
+#ifdef CONFIG_CGROUPS
+ case CLONE_NEWCGROUP:
+ if (ns_ref_get(nsp->cgroup_ns))
+ ns = to_ns_common(nsp->cgroup_ns);
+ break;
+#endif
+#ifdef CONFIG_UTS_NS
+ case CLONE_NEWUTS:
+ if (ns_ref_get(nsp->uts_ns))
+ ns = to_ns_common(nsp->uts_ns);
+ break;
+#endif
+#ifdef CONFIG_IPC_NS
+ case CLONE_NEWIPC:
+ if (ns_ref_get(nsp->ipc_ns))
+ ns = to_ns_common(nsp->ipc_ns);
+ break;
+#endif
+#ifdef CONFIG_USER_NS
+ case CLONE_NEWUSER:
+ if (ns_ref_get(user_ns))
+ ns = to_ns_common(user_ns);
+ break;
+#endif
+#ifdef CONFIG_PID_NS
+ case CLONE_NEWPID:
+ if (ns_ref_get(nsp->pid_ns_for_children))
+ ns = to_ns_common(nsp->pid_ns_for_children);
+ break;
+#endif
+#ifdef CONFIG_NET_NS
+ case CLONE_NEWNET:
+ if (ns_ref_get(nsp->net_ns))
+ ns = to_ns_common(nsp->net_ns);
+ break;
+#endif
+ case CLONE_NEWTIME:
+#ifdef CONFIG_TIME_NS
+ if (ns_ref_get(nsp->time_ns))
+ ns = to_ns_common(nsp->time_ns);
+ break;
+#else
+ return -EOPNOTSUPP;
+#endif
+ default:
+ return -EINVAL;
+ }
+
+ if (!ns)
+ return -ESRCH;
+
+ return open_namespace(ns);
+}
diff --git a/include/linux/nsfs.h b/include/linux/nsfs.h
index 731b67fc2fec..bfba790ee1a7 100644
--- a/include/linux/nsfs.h
+++ b/include/linux/nsfs.h
@@ -39,5 +39,7 @@ void nsfs_init(void);

void nsproxy_ns_active_get(struct nsproxy *ns);
void nsproxy_ns_active_put(struct nsproxy *ns);
+int nsproxy_open_namespace(struct nsproxy *nsp, struct user_namespace *user_ns,
+ unsigned long nstype);

#endif /* _LINUX_NSFS_H */
--
2.47.3