[RFC PATCH v6 12/26] userland: Add userland_open() system call
From: Jarkko Sakkinen
Date: Sat Nov 21 2026 - 20:19:28 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 userland_open() system call to allocate a userland 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
userland 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 userland.
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/sys_ni.c | 3 ++
kernel/userland.c | 107 +++++++++++++++++++++++++++++++++++++++
3 files changed, 111 insertions(+)
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 72308cc11908..2ea8ebd71743 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -1010,6 +1010,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_userland_open(unsigned int flags);
/*
* Architecture-specific system calls
diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
index e3202e7cc2a1..fca9663d09b7 100644
--- a/kernel/sys_ni.c
+++ b/kernel/sys_ni.c
@@ -399,3 +399,6 @@ COND_SYSCALL(rseq_slice_yield);
COND_SYSCALL(uretprobe);
COND_SYSCALL(uprobe);
+
+/* kernel/userland.c */
+COND_SYSCALL(userland_open);
diff --git a/kernel/userland.c b/kernel/userland.c
index e117b00cb8a2..d99b5d99fcc4 100644
--- a/kernel/userland.c
+++ b/kernel/userland.c
@@ -128,3 +128,110 @@ void userland_exit(struct pid_namespace *pid_ns, struct task_struct *tsk)
__wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP));
userland_put(c);
}
+
+static struct user_namespace *userland_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 userland *userland_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 userland *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 = userland_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:
+ userland_put(c);
+ return ERR_PTR(ret);
+}
+
+/**
+ * sys_userland_open - Open a new userland
+ * @flags: Combination of &enum userland_flags values
+ *
+ * Allocate a userland owning a fresh user namespace and dedicated
+ * empty mount, PID, UTS, IPC, network, cgroup, and time namespaces.
+ *
+ * Return: Userland file descriptor on success, negative error code on failure.
+ */
+SYSCALL_DEFINE1(userland_open, unsigned int, flags)
+{
+ struct userland *c;
+ int fd;
+
+ if (flags & ~USERLAND_ALL_FLAGS)
+ return -EINVAL;
+ if (unlikely(!current->nsproxy))
+ return -EOPNOTSUPP;
+
+ c = userland_create(flags);
+ if (IS_ERR(c))
+ return PTR_ERR(c);
+
+ fd = anon_inode_getfd("userland", &userland_fops, c,
+ O_RDWR |
+ (flags & USERLAND_CLOSE_ON_EXEC ? O_CLOEXEC : 0));
+ if (fd < 0)
+ userland_put(c);
+
+ return fd;
+}
--
2.47.3