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=1796094686; b=xLHBydFmt88rLmeq7mABxKKR78w5feoZubH0sQW1iWzp6QXyriHZpwv4mac2ITkQGGvp 57xAXAAghYlBQGF3LH7bKFawoQkFLiNxO3GjpvwKVpfTnLqoYIVY+diP338yDUD4BPYaj YtaFNjERoo7LBgyqEY3Bz/Qu+MDLFzvtDjHh8DsczXlKikwguOfBIza8CGZL2JRYLTaOz ptVAQrONBEYU8jIiciFCOXycFuE99qXsUzj/TIpZbygdeGLxVpwRURzwrC0/SSERPD/82 jH5o74/XeDui6O//gHzdOaWeykgT5LnrNhIasZd+6jy2d5F4jbFLonsXPTSLUZLohHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796094686; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=GhgKwUbmV+bph0sStabPmcEf9Rd/3DhDvPH7vtbZ5g0=; b=DPzEa/8ENAHXwz0OW6h5gpiqeYfGzJvsEezSV8OPpqccEMAoJtq4BSdIyzOe8DyP2Oxr Q+0uds9PJSJfTk2vkXMrtdtkaAJD5kG2KXb5K0JRw//Vku/IrrQK0ETDdriE8AKoJmAqU ExU1N1bUN8VWXldrVs2QFvnxtM3QCZ3i4a1txMGSX1KEqs838Sa2/UgUfE9wEVFxJFs/w HNlQaM8jb6bgz15oEQVP3wk35wylUYax2NgKZ942dlvgnqrVC+w3CW9PDerEWwVgj5ekk tPJrAhMMebckTPXNvGPAsNP1hT1qckL/9GrMvhhRo3VWRqbfz6lZL0oVYaAex+rBOnA== 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=eWs9pmfl; 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 0BF1C606A33C; Tue, 1 Dec 2026 03:11:25 +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=eWs9pmfl Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id E603C1F00919; Tue, 1 Dec 2026 03:11:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796094685; bh=GhgKwUbmV+bph0sStabPmcEf9Rd/3DhDvPH7vtbZ5g0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eWs9pmflfPAdMBz4FtTKOd7Zj+UVXHRlcqdxtLHLzrrhH65/Uz4rh+7PpbrMbXDlU nBeIzdmlxdyIBNw3Wm/fHi7xja81AaOvCizCrHDAjXpkJ09qymZugeV8PFC0mdJgNU doGLEMjyP2NqHOFy6fdYrQ9yCjG23SKpggLd3kc0u1pdkxy5T+XFVSHc4B8gcXFYtq 8T6d/ZMeThHt76sWMVkce/yastfBkp6EVFRCcCKolIShQ9q5oBEE2eD2bAKKpcZMDD hz/YevuMwtOWtyBWhsXlve/ywaW9skJlpZCJH0OJ/CunhQliLrHF09cd+QRMI7Bqky kdEKto2ST9Lig== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , 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 , =?UTF-8?q?G=C3=BCnther=20Noack?= , 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 , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , 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: [RFC PATCH v9 14/29] nsexec: Add nsexec_open() system call Date: Tue, 1 Dec 2026 05:09:46 +0200 Message-ID: <20261201031002.593830-15-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org> References: <20261201031002.593830-1-jarkko@kernel.org> X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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) +{ + struct nsexec *c; + int fd; + + if (flags & ~NSEXEC_ALL_FLAGS) + return -EINVAL; + + c = nsexec_create(flags); + if (IS_ERR(c)) + return PTR_ERR(c); + + 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