From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796105934; b=gpdjqkJmMWiaEEEBePOSe/wxFlMw5ySv2HZmnpNN7J+JBKan022PXKF3SOFLzsq28qVO qJmbM4T628DZPf+53Hsf6h0Vo8jtn7bOF7fpoyBagklIXvHOUJ3dZH1nDQMLzPQCsp3Sk RrYW7Xh08lG4eEZPHsLaDTBBiosfEXjBAdQ/EKo+TGDj/c1KbvMj0dTlrmW50nvuoUNvD PRmqi213NrkdyXwPAaJjgw4k3erfvsHNkpfo/4c2UfLYhZTZd/XlarWpJs3pir1QHoSmm i7l1+2RVzjAbr/5FmxvP58RqxJW9pyPiIbKWSfwbOg1iwWh+rkBmkTaiStaaRx/WfHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796105934; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=WG0c0hmC14DpDv2O0G3jOp1jOF7ZDb3gw6uEtu/y2+M=; b=rZ33smNHqX2vqF96suBRl95w3U0drLax4QG3uTNwhwgZzZT06zYdEBNRFPw7LITc+bWy yZNirPvlPaIBx6LyTNcinVCS3TTyJStmmAJL37JTNbyuAMfW9LfTLOU6QPqmZbqZu1NaH RHXOtm5mlWl1XR/iu76qdX7A4qj7GLAyTwQuxNeTQaOoK+4YGVF0gBN3nUlQoK2xNOWwI 8c7V/xc1bKwWWJeX80NybQRFFxWC4RbRtJxO/U6iiMDWJITcV2d0MhWtWuzkutT5Q7DmN 6FI/kM5xRr9H1fyuwgIxyMkyzF1fXxqAXG8Z2opW6Nkr5bLsN6J2KBSrSopNmDR3NTg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=E3g8Na6A; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 5ECD7606A31A; Tue, 1 Dec 2026 06:18:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=E3g8Na6A Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 2765A1F000C1; Tue, 1 Dec 2026 06:18:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796105933; bh=WG0c0hmC14DpDv2O0G3jOp1jOF7ZDb3gw6uEtu/y2+M=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=E3g8Na6AXLRzuVmRkEr4Fr1hQYbezaJcQ2J1AumHcQDeEqSlja2+M75R/PA5nEpUc 1AjEIdKP8NW1FXShW1ZefnmsAOX1UHkiWXhFgerMgBYJ/slmAsPMbCaWr3eAF6H/+G ASr+1Iw4mCP93JxAxkGXmnsLajD5mPQhoSLBpdwl4L4QrYQJjpRujskOZRyogJ5xKB aBEf0+JU23kOZMcIxpuDIwPl+nPgG/whUF7YOGfiRa/5uC+kbY7KtJ4sie0ROb/Mbe Z1U3jAcJpsL8U6WVimFn0WDsECa6UxBSQLfX5nvt9ccvyOGQLEFjXt+0ki6R+LqMX9 JXgVvxwxSUahw== Date: Tue, 1 Dec 2026 08:18:49 +0200 From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?iso-8859-1?Q?G=FCnther?= Noack , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: Re: [RFC PATCH v9 14/29] nsexec: Add nsexec_open() system call Message-ID: References: <20261201031002.593830-1-jarkko@kernel.org> <20261201031002.593830-15-jarkko@kernel.org> X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261201031002.593830-15-jarkko@kernel.org> 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 > Signed-off-by: David Howells > Signed-off-by: Jarkko Sakkinen > --- > 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