[RFC PATCH v10 13/28] runfd: Add runfd() system call

From: Jarkko Sakkinen

Date: Fri Dec 04 2026 - 22:29:16 EST


Setting up an isolated namespace environment from a multithreaded
manager process currently requires helper processes to call unshare()
and pass file descriptors back over sockets.

Add the runfd() system call to allocate a run owning a
fresh user namespace alongside dedicated empty mount
(CLONE_EMPTY_MNTNS), PID, UTS, IPC, network, cgroup, and time
namespaces, and return its anonymous-inode file descriptor. Because a
run encapsulates process lifecycle and mount delegation rather than
an nsfs namespace entity, keep it separate from namespace creation APIs.
Finalize the nsproxy time namespace and pin active references for the
lifetime of the run.

Co-developed-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
include/linux/syscalls.h | 1 +
kernel/runfd.c | 107 +++++++++++++++++++++++++++++++++++++++
kernel/sys_ni.c | 3 ++
3 files changed, 111 insertions(+)

diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 552d9710a0d5..2c2db703a005 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -1006,6 +1006,7 @@ asmlinkage long sys_lsm_get_self_attr(unsigned int attr, struct lsm_ctx __user *
asmlinkage long sys_lsm_set_self_attr(unsigned int attr, struct lsm_ctx __user *ctx,
u32 size, u32 flags);
asmlinkage long sys_lsm_list_modules(u64 __user *ids, u32 __user *size, u32 flags);
+asmlinkage long sys_runfd(unsigned int flags);

/*
* Architecture-specific system calls
diff --git a/kernel/runfd.c b/kernel/runfd.c
index 2dfd5c55ca41..6b250796c24e 100644
--- a/kernel/runfd.c
+++ b/kernel/runfd.c
@@ -135,3 +135,110 @@ void runfd_exit(struct pid_namespace *pid_ns, struct task_struct *tsk)
__wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP));
runfd_put(c);
}
+
+static struct user_namespace *runfd_create_user_ns(void)
+{
+ struct user_namespace *user_ns;
+ struct cred *new;
+ int ret;
+
+ new = prepare_creds();
+ if (!new)
+ return ERR_PTR(-ENOMEM);
+
+ ret = create_user_ns(new);
+ if (ret < 0) {
+ abort_creds(new);
+ return ERR_PTR(ret);
+ }
+
+ user_ns = get_user_ns(new->user_ns);
+ abort_creds(new);
+ return user_ns;
+}
+
+static struct run *runfd_create(unsigned int flags)
+{
+ u64 clone_flags = CLONE_NEWNS | CLONE_EMPTY_MNTNS | CLONE_NEWPID |
+ CLONE_NEWCGROUP | CLONE_NEWUTS | CLONE_NEWIPC |
+ CLONE_NEWNET |
+ (IS_ENABLED(CONFIG_TIME_NS) ? CLONE_NEWTIME : 0);
+ struct user_namespace *user_ns;
+ struct nsproxy *ns;
+ struct run *c;
+ int ret;
+
+ c = kzalloc_obj(*c, GFP_KERNEL);
+ if (!c)
+ return ERR_PTR(-ENOMEM);
+
+ init_waitqueue_head(&c->waitq);
+ spin_lock_init(&c->lock);
+ refcount_set(&c->usage, 1);
+ c->flags = flags;
+
+ user_ns = runfd_create_user_ns();
+ if (IS_ERR(user_ns)) {
+ ret = PTR_ERR(user_ns);
+ goto err_put;
+ }
+ c->user_ns = user_ns;
+ ns_ref_active_get(user_ns);
+
+ if (!ns_capable(user_ns, CAP_SYS_ADMIN)) {
+ ret = -EPERM;
+ goto err_put;
+ }
+
+ ns = create_new_namespaces(clone_flags, current, user_ns, NULL);
+ if (IS_ERR(ns)) {
+ ret = PTR_ERR(ns);
+ goto err_put;
+ }
+
+ if (ns->time_ns != ns->time_ns_for_children) {
+ get_time_ns(ns->time_ns_for_children);
+ put_time_ns(ns->time_ns);
+ ns->time_ns = ns->time_ns_for_children;
+ }
+ c->ns = ns;
+ nsproxy_ns_active_get(ns);
+
+ return c;
+
+err_put:
+ runfd_put(c);
+ return ERR_PTR(ret);
+}
+
+/**
+ * sys_runfd - Create a run and return its file descriptor
+ * @flags: Combination of &enum runfd_flags values
+ *
+ * Allocate a run owning a fresh child user namespace (with the caller's
+ * effective UID and GID as owner and initially empty ID mappings) and dedicated
+ * empty mount, PID, UTS, IPC, network, cgroup, and time namespaces. Control
+ * operations authorize against capabilities in the run's owning user
+ * namespace.
+ *
+ * Return: Close-on-exec run file descriptor on success, negative error code
+ * on failure.
+ */
+SYSCALL_DEFINE1(runfd, unsigned int, flags)
+{
+ struct run *c;
+ int fd;
+
+ if (flags & ~RUNFD_ALL_FLAGS)
+ return -EINVAL;
+
+ c = runfd_create(flags);
+ if (IS_ERR(c))
+ return PTR_ERR(c);
+
+ fd = anon_inode_getfd("runfd", &runfd_fops, c, O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ runfd_put(c);
+
+ return fd;
+}
diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
index c8be0abaa407..9c9ee50443fb 100644
--- a/kernel/sys_ni.c
+++ b/kernel/sys_ni.c
@@ -398,3 +398,6 @@ COND_SYSCALL(rseq_slice_yield);

COND_SYSCALL(uretprobe);
COND_SYSCALL(uprobe);
+
+/* kernel/runfd.c */
+COND_SYSCALL(runfd);
--
2.47.3