Standalone namespace operations such as NS_SET_IDMAP and
move_mount(..., MOVE_MOUNT_T_MNTNS) operate on nsfs file descriptors,
requiring a way to obtain member namespace fds from a run file
descriptor before any process has entered it.

Add the RUNFD_GET_NSFD ioctl to open an O_CLOEXEC nsfs file
descriptor for a requested member namespace when the caller holds
CAP_SYS_ADMIN in the run's user namespace, additionally requiring
an ancestor active PID namespace and a running init process for
CLONE_NEWPID.

Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 fs/internal.h              |  1 -
 include/linux/nsfs.h       |  1 +
 include/uapi/linux/runfd.h | 19 ++++++++++
 kernel/runfd.c             | 76 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/fs/internal.h b/fs/internal.h
index 3f2fbac40f8e..ee3f796241ac 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -250,7 +250,6 @@ extern void mnt_pin_kill(struct mount *m);
  * fs/nsfs.c
  */
 extern const struct dentry_operations ns_dentry_operations;
-int open_namespace(struct ns_common *ns);
 struct file *open_namespace_file(struct ns_common *ns);
 
 /*
diff --git a/include/linux/nsfs.h b/include/linux/nsfs.h
index 731b67fc2fec..40912c1eb5ff 100644
--- a/include/linux/nsfs.h
+++ b/include/linux/nsfs.h
@@ -23,6 +23,7 @@ bool ns_match(const struct ns_common *ns, dev_t dev, ino_t 
ino);
 int ns_get_name(char *buf, size_t size, struct task_struct *task,
                        const struct proc_ns_operations *ns_ops);
 void nsfs_init(void);
+int open_namespace(struct ns_common *ns);
 
 #define __current_namespace_from_type(__ns)                            \
        _Generic((__ns),                                                \
diff --git a/include/uapi/linux/runfd.h b/include/uapi/linux/runfd.h
index f46a757b1159..7c4ecc5e4f1c 100644
--- a/include/uapi/linux/runfd.h
+++ b/include/uapi/linux/runfd.h
@@ -7,6 +7,8 @@
 #ifndef _UAPI_LINUX_RUNFD_H
 #define _UAPI_LINUX_RUNFD_H
 
+#include <linux/ioctl.h>
+
 /**
  * enum runfd_flags - Flags for runfd()
  * @RUNFD_AUTOKILL:    Send %SIGKILL to run init on final fd release.
@@ -19,4 +21,21 @@ enum runfd_flags {
        RUNFD_AUTOKILL  = 0x00000001,
 };
 
+#define RUNFD_IOCTL_MAGIC      0xB9
+
+/**
+ * DOC: RUNFD_GET_NSFD
+ *
+ * ``ioctl(run_fd, RUNFD_GET_NSFD, nstype)`` opens an ``nsfs`` file
+ * descriptor (with ``O_CLOEXEC``) for the member namespace identified by
+ * ``nstype`` (one of ``CLONE_NEWNS``, ``CLONE_NEWCGROUP``,
+ * ``CLONE_NEWUTS``, ``CLONE_NEWIPC``, ``CLONE_NEWUSER``, ``CLONE_NEWPID``,
+ * ``CLONE_NEWNET``, or ``CLONE_NEWTIME``).
+ *
+ * Requires ``CAP_SYS_ADMIN`` in the run's user namespace.
+ * Opening ``CLONE_NEWPID`` additionally requires running in an ancestor
+ * active PID namespace and that run init is running.
+ */
+#define RUNFD_GET_NSFD _IO(RUNFD_IOCTL_MAGIC, 0x01)
+
 #endif /* _UAPI_LINUX_RUNFD_H */
diff --git a/kernel/runfd.c b/kernel/runfd.c
index 6b250796c24e..b0dd7666f5d7 100644
--- a/kernel/runfd.c
+++ b/kernel/runfd.c
@@ -25,6 +25,8 @@
 #include <linux/slab.h>
 #include <linux/syscalls.h>
 #include <linux/uaccess.h>
+#include <linux/cgroup_namespace.h>
+#include <linux/ipc_namespace.h>
 #include <linux/mnt_namespace.h>
 #include <linux/nsproxy.h>
 #include <linux/ns_common.h>
@@ -33,6 +35,8 @@
 #include <linux/pid_namespace.h>
 #include <linux/time_namespace.h>
 #include <linux/user_namespace.h>
+#include <linux/uts_namespace.h>
+#include <net/net_namespace.h>
 #include <linux/idr.h>
 #include <uapi/linux/runfd.h>
 
@@ -65,6 +69,76 @@ static __poll_t runfd_poll(struct file *file, poll_table 
*wait)
        return mask;
 }
 
+static long runfd_get_nsfd(struct run *run, unsigned long nstype)
+{
+       struct ns_common *ns = NULL;
+
+       if (nstype > UINT_MAX)
+               return -EINVAL;
+       if (!ns_capable(run->user_ns, CAP_SYS_ADMIN))
+               return -EPERM;
+
+       if (nstype == CLONE_NEWPID) {
+               if (!pidns_is_ancestor(run->ns->pid_ns_for_children,
+                                      task_active_pid_ns(current)))
+                       return -EXDEV;
+               if (READ_ONCE(run->state) != RUNFD_RUNNING)
+                       return -ESRCH;
+       }
+
+       scoped_guard(spinlock, &run->lock) {
+               switch (nstype) {
+               case CLONE_NEWNS:
+                       ns = from_mnt_ns(run->ns->mnt_ns);
+                       break;
+               case CLONE_NEWCGROUP:
+                       ns = to_ns_common(run->ns->cgroup_ns);
+                       break;
+               case CLONE_NEWUTS:
+                       ns = to_ns_common(run->ns->uts_ns);
+                       break;
+               case CLONE_NEWIPC:
+                       ns = to_ns_common(run->ns->ipc_ns);
+                       break;
+               case CLONE_NEWUSER:
+                       ns = to_ns_common(run->user_ns);
+                       break;
+               case CLONE_NEWPID:
+                       ns = to_ns_common(run->ns->pid_ns_for_children);
+                       break;
+               case CLONE_NEWNET:
+                       ns = to_ns_common(run->ns->net_ns);
+                       break;
+               case CLONE_NEWTIME:
+#ifdef CONFIG_TIME_NS
+                       ns = to_ns_common(run->ns->time_ns);
+                       break;
+#else
+                       return -EOPNOTSUPP;
+#endif
+               default:
+                       return -EINVAL;
+               }
+
+               if (!__ns_ref_get(ns))
+                       return -ESRCH;
+       }
+
+       return open_namespace(ns);
+}
+
+static long runfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       struct run *run = file->private_data;
+
+       switch (cmd) {
+       case RUNFD_GET_NSFD:
+               return runfd_get_nsfd(run, arg);
+       default:
+               return -ENOIOCTLCMD;
+       }
+}
+
 static int runfd_release(struct inode *inode, struct file *file)
 {
        struct run *run = file->private_data;
@@ -87,6 +161,8 @@ static int runfd_release(struct inode *inode, struct file 
*file)
 
 static const struct file_operations runfd_fops = {
        .poll           = runfd_poll,
+       .unlocked_ioctl = runfd_ioctl,
+       .compat_ioctl   = runfd_ioctl,
        .release        = runfd_release,
 };
 
-- 
2.47.3


Reply via email to