Re: [RFC PATCH v9 14/29] nsexec: Add nsexec_open() system call
From: Jarkko Sakkinen
Date: Tue Dec 01 2026 - 01:19:00 EST
On Tue, Dec 01, 2026 at 05:09:46AM +0200, Jarkko Sakkinen wrote:
> 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 nsexec_open() system call to allocate an nsexec 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
> nsexec 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 nsexec.
>
> 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/nsexec.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 72308cc11908..7dc55fb0857e 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_nsexec_open(unsigned int flags);
>
> /*
> * Architecture-specific system calls
> diff --git a/kernel/nsexec.c b/kernel/nsexec.c
> index b9fc8fdf9b5c..3277822386f6 100644
> --- a/kernel/nsexec.c
> +++ b/kernel/nsexec.c
> @@ -134,3 +134,110 @@ void nsexec_exit(struct pid_namespace *pid_ns, struct task_struct *tsk)
> __wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP));
> nsexec_put(c);
> }
> +
> +static struct user_namespace *nsexec_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 nsexec *nsexec_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 nsexec *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 = nsexec_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:
> + nsexec_put(c);
> + return ERR_PTR(ret);
> +}
> +
> +/**
> + * sys_nsexec_open - Open a new nsexec
> + * @flags: Combination of &enum nsexec_flags values
> + *
> + * Allocate an nsexec 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 nsexec's owning user
> + * namespace.
> + *
> + * Return: Close-on-exec nsexec file descriptor on success, negative error code
> + * on failure.
> + */
> +SYSCALL_DEFINE1(nsexec_open, unsigned int, flags)
This does not really matter all that much but I was going to rename this
as nsexec() but forgot to do it.
> +{
> + struct nsexec *c;
> + int fd;
> +
> + if (flags & ~NSEXEC_ALL_FLAGS)
> + return -EINVAL;
> +
> + c = nsexec_create(flags);
> + if (IS_ERR(c))
> + return PTR_ERR(c);
This could be mapped to ioctls if wanted e.g.,
1. NSEXEC_CREATE with flags parameter, which would be available up
until clone3().
2. One-way parameterless NSEXEC_SET_AUTOKILL and NSEXEC_CREATE.
I.e., factor even more like SGX enclave initialization.
For the time being, I don't see anything useful except dropping a single
fallible case from nsexec_open(), which by itself is not gain but in
this sudoku its good to be aware of various sequencing twists (just like
it was in SGX).
> +
> + fd = anon_inode_getfd("nsexec", &nsexec_fops, c, O_RDWR | O_CLOEXEC);
> + if (fd < 0)
> + nsexec_put(c);
> +
> + return fd;
> +}
> diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c
> index e3202e7cc2a1..760724d57a0d 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/nsexec.c */
> +COND_SYSCALL(nsexec_open);
> --
> 2.47.3
>
>
Br, Jarkko