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=1796441253; b=GvBytU3e+r4qpDyNuZIDOaUST/r5FOEjtl+cFtETg52+/gmWps878oGbG1hsgrJ5qxaj mX4ChHpEOEGXeJQ6VIkpKKReu0jTIz2MT6GMdoshzmHybmEfPWCDUGUubddd5Ai22LFJm LpbdVGaAbNqcBkPysLf0R3u0gtKM0yFtVOfwNhX7y7AU3ZM63kmSBqhuz51IJbPOMoJ3U eF4ct9UUubGZwAiquGtCWPa+S5BVjnCRmduo+UGo/UHU363IHv88HtmL3S1TATHmsLX8q UQc94XkYgBcLLU9Zff3T0nNlZx/rl81hFOvWJ4C8uPoGsx0ndmpjhHte/wC+NHTyapA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441253; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=E1QENmcA9GnJGnsxOUI6hkzBKO4OwRHA0vfyKmuBtnQ=; b=haoMvzJJvrOZLts2gUMJhK3h1Rjz3fld3GGESTdVwQ48A9D6bmahGZHyE5MVOUnXxlMW C3PEtIBfyISfyBaZz/hd6JewT/v4ozDdn5DrhlC3MmVd+pedyT+F+GR5JNqMu7xE6mgKQ YT26CGnTQp4L55HjOdsxtviLk1cDxVLz8lLdzrKjXrXrW2secAIFF0GmURH7WgbITVyRh EUXKV37Oonm5SpF9ndnNMzawhHFUmDTwWIOYjrYRyRP0tXi7WMAQvm4jWYcdM6r/YtEIb oNCwMpwi4M4dueJ1JJ4AIeMqKd/I9gGPl7c30cB90fl7RneRpd6bijCeLBuXm1iAK2A== 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="F2hNhS/R"; 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 2E2BC606A6A3; Sat, 5 Dec 2026 03:27:32 +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=F2hNhS/R Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 04CE01F00A1C; Sat, 5 Dec 2026 03:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441252; bh=E1QENmcA9GnJGnsxOUI6hkzBKO4OwRHA0vfyKmuBtnQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=F2hNhS/RjElem+l/wZSgkaPopOsjXrpE9xroDROjaEeMA2SxZosIf6Xdye3k7sXvz XNQ2deiOJiKM0texLEC0v0tRZM5naVpI0YBWL+mdMrgAynTZMTAA45dMDGyBO8GGk7 FKwoyFXCeamsiki8463Q55C8fryyNSlwfCyXB7id62kTPqszF8W3nbdQkIJERoy1NT piacZJnL82CFhAceTahCZBggJQ5/kcrXga5DUymoNo0SWusCvjA0YM/vWzCsDeu6lX rh7aMDKvvCpuoavVtSZMycx+PaNSoZOpuhuVYQ8uWMH23ScKr0xhf5766qPoJt51fE hndohJxTTEwZg== 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 v10 11/28] runfd: Add core infrastructure Date: Sat, 5 Dec 2026 05:26:16 +0200 Message-ID: <20261205032633.1702772-12-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-1-jarkko@kernel.org> References: <20261205032633.1702772-1-jarkko@kernel.org> X-Mailing-List: linux-security-module@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 Linux namespaces can be created and entered individually, but userspace orchestrators lack a single control object that binds a full detached namespace set and an init process lifecycle behind a pollable file descriptor. Because this object manages process lifecycle, wait and signal delivery, VFS mount delegation, and LSM state rather than representing an nsfs namespace entity, model it as a distinct run abstraction. Add struct run, its lifecycle states, reference counting, uAPI flags, and anonymous inode file operations under CONFIG_RUNFD. Drop active and passive namespace references on final release, send SIGKILL to the run init thread group on close when RUNFD_AUTOKILL is set, and report EPOLLHUP from poll() once the run reaches RUNFD_DEAD. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/runfd.h | 83 ++++++++++++++++++++++++++++++++ include/uapi/linux/runfd.h | 22 +++++++++ init/Kconfig | 10 ++++ kernel/Makefile | 2 + kernel/runfd.c | 96 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+) create mode 100644 include/linux/runfd.h create mode 100644 include/uapi/linux/runfd.h create mode 100644 kernel/runfd.c diff --git a/include/linux/runfd.h b/include/linux/runfd.h new file mode 100644 index 000000000000..d7faba4da949 --- /dev/null +++ b/include/linux/runfd.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +#ifndef _LINUX_RUNFD_H +#define _LINUX_RUNFD_H + +#include +#include +#include +#include + +#define RUNFD_ALL_FLAGS RUNFD_AUTOKILL + +struct file; +struct nsproxy; +struct pid; +struct user_namespace; + +/** + * enum runfd_state - run lifecycle states + * @RUNFD_INIT: Build phase after ``runfd()`` before any + * process has entered. ``RUNFD_SHARE_NSFD`` is + * permitted, ID mappings may be configured on the owning + * user namespace, and ``"/"`` must be mounted in the + * mount namespace before entry. + * @RUNFD_FORKING: Bootstrap phase while the first + * ``clone3(CLONE_INTO_RUN)`` call creates PID 1. + * Concurrent callers wait killably; reverts to + * %RUNFD_INIT if ``copy_process()`` fails, or advances + * to %RUNFD_RUNNING once PID 1 is attached. + * @RUNFD_RUNNING: Live phase while PID 1 is attached. Subsequent + * ``clone3(CLONE_INTO_RUN)`` calls join the PID + * namespace until teardown begins, + * ``RUNFD_GET_NSFD(CLONE_NEWPID)`` and + * ``pidfd_send_signal()`` are enabled, and + * ``RUNFD_SHARE_NSFD`` returns ``-EBUSY``. + * @RUNFD_DEAD: Terminal phase entered after PID 1 exits and the PID + * namespace has killed and reaped all member processes. + * ``poll()`` reports ``EPOLLHUP`` and + * ``waitid(P_RUNFD, ...)`` reports PID 1's exit status + * without consuming it. + * + * State transitions advance from %RUNFD_INIT through %RUNFD_FORKING and + * %RUNFD_RUNNING to %RUNFD_DEAD, reverting from %RUNFD_FORKING to + * %RUNFD_INIT only if the initial bootstrap fork fails. + */ +enum runfd_state { + RUNFD_INIT = 0, + RUNFD_FORKING = 1, + RUNFD_RUNNING = 2, + RUNFD_DEAD = 3, +}; + +struct run { + refcount_t usage; + int exit_code; + struct user_namespace *user_ns; + struct nsproxy *ns; + struct pid *init_pid; + spinlock_t lock; /* Guards run lifecycle state */ + wait_queue_head_t waitq; + unsigned int flags; + enum runfd_state state; +}; + +#ifdef CONFIG_RUNFD +void runfd_put(struct run *c); +bool runfd_is_file(struct file *file); +#else +static inline void runfd_put(struct run *c) +{ +} + +static inline bool runfd_is_file(struct file *file) +{ + return false; +} +#endif /* CONFIG_RUNFD */ + +#endif /* _LINUX_RUNFD_H */ diff --git a/include/uapi/linux/runfd.h b/include/uapi/linux/runfd.h new file mode 100644 index 000000000000..f46a757b1159 --- /dev/null +++ b/include/uapi/linux/runfd.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +#ifndef _UAPI_LINUX_RUNFD_H +#define _UAPI_LINUX_RUNFD_H + +/** + * enum runfd_flags - Flags for runfd() + * @RUNFD_AUTOKILL: Send %SIGKILL to run init on final fd release. + * + * Every run created by runfd() allocates a full set of + * detached namespaces owned by a fresh user namespace and sets %O_CLOEXEC + * on the returned control file descriptor. + */ +enum runfd_flags { + RUNFD_AUTOKILL = 0x00000001, +}; + +#endif /* _UAPI_LINUX_RUNFD_H */ diff --git a/init/Kconfig b/init/Kconfig index c8a7cfa60a6e..ffd0f1c19c4c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1281,6 +1281,16 @@ config NET_NS Allow user space to create what appear to be multiple instances of the network stack. +config RUNFD + bool "Container run descriptors" + depends on USER_NS && PID_NS && UTS_NS && IPC_NS && NET_NS && CGROUPS + help + Allow user space to open and manage isolated runs and their init + process lifecycle through an anonymous-inode control file descriptor + via runfd(), waitid(P_RUNFD), pidfd_send_signal(), + RUNFD_GET_NSFD, RUNFD_SHARE_NSFD, FSCONFIG_SET_RUN, and + clone3() with CLONE_INTO_RUN. + endif # NAMESPACES config CHECKPOINT_RESTORE diff --git a/kernel/Makefile b/kernel/Makefile index 4ff92e24680c..eb39bd150903 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -152,6 +152,8 @@ KCOV_INSTRUMENT_kstack_erase.o := n obj-$(CONFIG_SCF_TORTURE_TEST) += scftorture.o +obj-$(CONFIG_RUNFD) += runfd.o + $(obj)/configs.o: $(obj)/config_data.gz # .config is small, so it is compressed with gzip rather than the image compressor. diff --git a/kernel/runfd.c b/kernel/runfd.c new file mode 100644 index 000000000000..019e90e8bf2f --- /dev/null +++ b/kernel/runfd.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2017-2019 Red Hat, Inc. All Rights Reserved. + * Copyright (C) 2026 Opinsys Oy + */ + +/** + * DOC: Overview + * + * A run is one instance of a container, from setup through init exit. + * ``runfd()`` creates a run and returns its control file descriptor before + * any process starts. Namespaces can be configured through file descriptors + * before ``clone3()`` starts init. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void runfd_put(struct run *c) +{ + if (!c || !refcount_dec_and_test(&c->usage)) + return; + + if (c->ns) + put_nsproxy(c->ns); + if (c->init_pid) + put_pid(c->init_pid); + if (c->user_ns) { + ns_ref_active_put(c->user_ns); + put_user_ns(c->user_ns); + } + kfree(c); +} + +static __poll_t runfd_poll(struct file *file, poll_table *wait) +{ + struct run *run = file->private_data; + __poll_t mask = 0; + + poll_wait(file, &run->waitq, wait); + + if (READ_ONCE(run->state) == RUNFD_DEAD) + mask |= EPOLLHUP; + + return mask; +} + +static int runfd_release(struct inode *inode, struct file *file) +{ + struct run *run = file->private_data; + + if (run->flags & RUNFD_AUTOKILL) { + struct pid *pid = NULL; + + scoped_guard(spinlock, &run->lock) { + if (run->state == RUNFD_RUNNING) + pid = get_pid(run->init_pid); + } + if (pid) { + kill_pid(pid, SIGKILL, true); + put_pid(pid); + } + } + runfd_put(run); + return 0; +} + +static const struct file_operations runfd_fops = { + .poll = runfd_poll, + .release = runfd_release, +}; + +bool runfd_is_file(struct file *file) +{ + return file->f_op == &runfd_fops; +} -- 2.47.3