Standalone namespace operations such as userns_set_idmap() and move_mount(..., MOVE_MOUNT_T_MNTNS) operate on nsfs file descriptors, requiring a way to obtain member namespace fds from an nsexec file descriptor before any process has entered it.
Add the NSEXEC_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 nsexec'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/nsexec.h | 19 ++++++++++ kernel/nsexec.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/nsexec.h b/include/uapi/linux/nsexec.h index 704eb206de7e..af8e287c229d 100644 --- a/include/uapi/linux/nsexec.h +++ b/include/uapi/linux/nsexec.h @@ -7,6 +7,8 @@ #ifndef _UAPI_LINUX_NSEXEC_H #define _UAPI_LINUX_NSEXEC_H +#include <linux/ioctl.h> + /** * enum nsexec_flags - Flags for nsexec_open() * @NSEXEC_AUTOKILL: Send %SIGKILL to nsexec init on final fd release. @@ -19,4 +21,21 @@ enum nsexec_flags { NSEXEC_AUTOKILL = 0x00000001, }; +#define NSEXEC_IOCTL_MAGIC 0xB9 + +/** + * DOC: NSEXEC_GET_NSFD + * + * ``ioctl(nsexec_fd, NSEXEC_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 nsexec's user namespace. + * Opening ``CLONE_NEWPID`` additionally requires running in an ancestor + * active PID namespace and that nsexec init is running. + */ +#define NSEXEC_GET_NSFD _IO(NSEXEC_IOCTL_MAGIC, 0x01) + #endif /* _UAPI_LINUX_NSEXEC_H */ diff --git a/kernel/nsexec.c b/kernel/nsexec.c index 3277822386f6..383fcc7112d4 100644 --- a/kernel/nsexec.c +++ b/kernel/nsexec.c @@ -24,6 +24,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> @@ -32,6 +34,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/nsexec.h> @@ -64,6 +68,76 @@ static __poll_t nsexec_poll(struct file *file, poll_table *wait) return mask; } +static long nsexec_get_nsfd(struct nsexec *nsexec, unsigned long nstype) +{ + struct ns_common *ns = NULL; + + if (nstype > UINT_MAX) + return -EINVAL; + if (!ns_capable(nsexec->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + + if (nstype == CLONE_NEWPID) { + if (!pidns_is_ancestor(nsexec->ns->pid_ns_for_children, + task_active_pid_ns(current))) + return -EXDEV; + if (READ_ONCE(nsexec->state) != NSEXEC_RUNNING) + return -ESRCH; + } + + scoped_guard(spinlock, &nsexec->lock) { + switch (nstype) { + case CLONE_NEWNS: + ns = from_mnt_ns(nsexec->ns->mnt_ns); + break; + case CLONE_NEWCGROUP: + ns = to_ns_common(nsexec->ns->cgroup_ns); + break; + case CLONE_NEWUTS: + ns = to_ns_common(nsexec->ns->uts_ns); + break; + case CLONE_NEWIPC: + ns = to_ns_common(nsexec->ns->ipc_ns); + break; + case CLONE_NEWUSER: + ns = to_ns_common(nsexec->user_ns); + break; + case CLONE_NEWPID: + ns = to_ns_common(nsexec->ns->pid_ns_for_children); + break; + case CLONE_NEWNET: + ns = to_ns_common(nsexec->ns->net_ns); + break; + case CLONE_NEWTIME: +#ifdef CONFIG_TIME_NS + ns = to_ns_common(nsexec->ns->time_ns); + break; +#else + return -EOPNOTSUPP; +#endif + default: + return -EINVAL; + } + + if (!__ns_ref_get(ns)) + return -ESRCH; + } + + return open_namespace(ns); +} + +static long nsexec_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct nsexec *nsexec = file->private_data; + + switch (cmd) { + case NSEXEC_GET_NSFD: + return nsexec_get_nsfd(nsexec, arg); + default: + return -ENOIOCTLCMD; + } +} + static int nsexec_release(struct inode *inode, struct file *file) { struct nsexec *nsexec = file->private_data; @@ -86,6 +160,8 @@ static int nsexec_release(struct inode *inode, struct file *file) static const struct file_operations nsexec_fops = { .poll = nsexec_poll, + .unlocked_ioctl = nsexec_ioctl, + .compat_ioctl = nsexec_ioctl, .release = nsexec_release, }; -- 2.47.3
