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=1795910347; b=nVyQ9qO+17vZLoRDRv1suvqIIIJJIAYHffvTXM019/9jBFHI5gXYHSjLXhipqd9agBHR /aVc7japd4P1Nt4IR1krk9VsxZ2z5u8MYsgfzpWjpZnxTf3zxmbtJ+S1rjYDrzLk6peBK zg3g5kxitc3yAR7GNXpH2FKs3KiplRFIhqpV4+TYXyHMDRF36QVpIU4SqzeKLiVjU24HH 94HZUhPeA4SPTlmzwMdjTEXiFg5fUG0RVOd0lxoCtZjbMR45c7x0lHNk74+FloinexyLs vZys/afrFmrt99vO0K+jWpu5BtNd9M+ARnmyO9VapHTn9jizCCuFp4tha0RheR6KZAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910347; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=QwWBedYXvg2Pdj9tBVIVBLdQYIJwWxF1Qc9DLbcZvqY=; b=KhG7HxYvbFBdqd6w8KQnPjmRYd4I7nl85DAcu12KMm2PuRLceXoar3ZH3mWXVjvdoYyK +SU5qbiPN1z5kcPya/uGAjz8lD4FtXREjMGHmmZLJpxMTrp9vr9p1e0L51sqznyoxhxMX 2KO8q2yWUMq5XTGil8sMZt99jKVyp1kDykv1RaWKJSpeCx+IEGxx2ddwyHL6Bc/HZdw3v bImQHwzNFN+qj9sOo35RKnKK7gzluwqyZRVsMvHTvZJIyjBHvNwrd/H+Pvr3ZobqqoZm5 ZJqDliWO8GpmD13cwq7pP/49JO8myutORRchYmbp69nYGTcBjJw4rxpQ9b/fzTx7R/g== 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=QXfsoAei; 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 4E051606A6B2; Sat, 28 Nov 2026 23:59:06 +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=QXfsoAei Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 321071F00A3F; Sat, 28 Nov 2026 23:59:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910346; bh=QwWBedYXvg2Pdj9tBVIVBLdQYIJwWxF1Qc9DLbcZvqY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QXfsoAeiNDJHdcmom2e5SQ1/l75/uqfr0ibnIrj87592gU5mR9bOlhHn2sQYVztOx xKVTOGvg85o5Du2R3Gr0SDafPMEHo6n2Hmw+gF6rybvrLRumB0HSsGp1NYkqs5+WDP pWjz+thvEYDkuWzRjUeQQZxMWQOm4JE2Tuqof81TN+PxWx0WkCJWsQks3lSXwUsPue iGlc349T2qjSOOJcKYtJjUdBKvtcjlSxF649HrN+p3luiVnBoti8p/J9PtE0po7z61 iZwFSJFv3uy+4UyFezxptof24VPZSV/VbBMwGyY5EU2qP8JpCmAZy8jSZMJjqGWf7C +MA40L43nwPew== 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-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 , Andy Lutomirski , Arnd Bergmann , Christian Brauner , David Howells , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , Hugh Dickins , James Morris , Jan Kara , Jeff Layton , John Johansen , Jonathan Corbet , Josef Bacik , Kees Cook , KP Singh , Lennart Poettering , Matt Bobrowski , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Serge Hallyn , Shuah Khan , Stephen Smalley , Zygmunt Krynicki Subject: [RFC PATCH v8 12/29] userland: Add core infrastructure Date: Sun, 29 Nov 2026 01:57:51 +0200 Message-ID: <20261128235808.2369970-13-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261128235808.2369970-1-jarkko@kernel.org> References: <20261128235808.2369970-1-jarkko@kernel.org> X-Mailing-List: linux-kselftest@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 userland abstraction. Add struct userland, its lifecycle states, reference counting, uAPI flags, and anonymous inode file operations under CONFIG_USERLAND. Drop active and passive namespace references on final release, send SIGKILL to the userland init thread group on close when USERLAND_KILL_ON_CLOSE is set, and report EPOLLHUP from poll() once the userland reaches USERLAND_DEAD. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/userland.h | 83 +++++++++++++++++++++++++++++++ include/uapi/linux/userland.h | 24 +++++++++ init/Kconfig | 10 ++++ kernel/Makefile | 2 + kernel/userland.c | 94 +++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+) create mode 100644 include/linux/userland.h create mode 100644 include/uapi/linux/userland.h create mode 100644 kernel/userland.c diff --git a/include/linux/userland.h b/include/linux/userland.h new file mode 100644 index 000000000000..364dd27f0eb0 --- /dev/null +++ b/include/linux/userland.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_USERLAND_H +#define _LINUX_USERLAND_H + +#include +#include +#include +#include + +#define USERLAND_ALL_FLAGS (USERLAND_KILL_ON_CLOSE | USERLAND_CLOEXEC) + +struct file; +struct nsproxy; +struct pid; +struct user_namespace; + +/** + * enum userland_state - Userland lifecycle states + * @USERLAND_INIT: Build phase after ``userland_open()`` before any + * process has entered. ``USERLAND_SHARE_NSFD`` is + * permitted, ID mappings may be configured on the owning + * user namespace, and ``"/"`` must be mounted in the + * mount namespace before entry. + * @USERLAND_FORKING: Bootstrap phase while the first + * ``clone3(CLONE_INTO_USERLAND)`` call creates PID 1. + * Concurrent callers wait killably; reverts to + * %USERLAND_INIT if ``copy_process()`` fails, or advances + * to %USERLAND_RUNNING once PID 1 is attached. + * @USERLAND_RUNNING: Live phase while PID 1 is attached. Subsequent + * ``clone3(CLONE_INTO_USERLAND)`` calls join the PID + * namespace until teardown begins, + * ``USERLAND_GET_NSFD(CLONE_NEWPID)`` and + * ``pidfd_send_signal()`` are enabled, and + * ``USERLAND_SHARE_NSFD`` returns ``-EBUSY``. + * @USERLAND_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_USERLAND, ...)`` reports PID 1's exit status + * without consuming it. + * + * State transitions advance from %USERLAND_INIT through %USERLAND_FORKING and + * %USERLAND_RUNNING to %USERLAND_DEAD, reverting from %USERLAND_FORKING to + * %USERLAND_INIT only if the initial bootstrap fork fails. + */ +enum userland_state { + USERLAND_INIT = 0, + USERLAND_FORKING = 1, + USERLAND_RUNNING = 2, + USERLAND_DEAD = 3, +}; + +struct userland { + refcount_t usage; + int exit_code; + struct user_namespace *user_ns; + struct nsproxy *ns; + struct pid *init_pid; + spinlock_t lock; /* Guards userland lifecycle state */ + wait_queue_head_t waitq; + unsigned int flags; + enum userland_state state; +}; + +#ifdef CONFIG_USERLAND +void userland_put(struct userland *c); +bool userland_is_file(struct file *file); +#else +static inline void userland_put(struct userland *c) +{ +} + +static inline bool userland_is_file(struct file *file) +{ + return false; +} +#endif /* CONFIG_USERLAND */ + +#endif /* _LINUX_USERLAND_H */ diff --git a/include/uapi/linux/userland.h b/include/uapi/linux/userland.h new file mode 100644 index 000000000000..ecc9cc3fb539 --- /dev/null +++ b/include/uapi/linux/userland.h @@ -0,0 +1,24 @@ +/* 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_USERLAND_H +#define _UAPI_LINUX_USERLAND_H + +/** + * enum userland_flags - Flags for userland_open() + * @USERLAND_KILL_ON_CLOSE: Send %SIGKILL to userland init on final fd + * release. + * @USERLAND_CLOEXEC: Set %O_CLOEXEC on the returned control fd. + * + * Every userland created by userland_open() allocates a full set of + * detached namespaces owned by a fresh user namespace. + */ +enum userland_flags { + USERLAND_KILL_ON_CLOSE = 0x00000001, + USERLAND_CLOEXEC = 0x00000002, +}; + +#endif /* _UAPI_LINUX_USERLAND_H */ diff --git a/init/Kconfig b/init/Kconfig index c8a7cfa60a6e..0c5fada1ba88 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1283,6 +1283,16 @@ config NET_NS endif # NAMESPACES +config USERLAND + bool "Userland support" + depends on NAMESPACES && USER_NS && PID_NS && UTS_NS && IPC_NS && NET_NS && CGROUPS + help + Allow user space to open and manage isolated userlands and their init + process lifecycle through an anonymous-inode control file descriptor + via userland_open(), waitid(P_USERLAND), pidfd_send_signal(), + USERLAND_GET_NSFD, USERLAND_SHARE_NSFD, FSCONFIG_SET_USERLAND, and + clone3() with CLONE_INTO_USERLAND. + config CHECKPOINT_RESTORE bool "Checkpoint/restore support" depends on PROC_FS diff --git a/kernel/Makefile b/kernel/Makefile index 4ff92e24680c..80eb26639776 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_USERLAND) += userland.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/userland.c b/kernel/userland.c new file mode 100644 index 000000000000..7be0c4b5385b --- /dev/null +++ b/kernel/userland.c @@ -0,0 +1,94 @@ +// 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 userland is an anonymous-inode control object opened by ``userland_open()`` + * that binds a detached namespace set, an initially empty mount namespace, an + * owning user namespace, and an init process lifecycle. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void userland_put(struct userland *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 userland_poll(struct file *file, poll_table *wait) +{ + struct userland *userland = file->private_data; + __poll_t mask = 0; + + poll_wait(file, &userland->waitq, wait); + + if (READ_ONCE(userland->state) == USERLAND_DEAD) + mask |= EPOLLHUP; + + return mask; +} + +static int userland_release(struct inode *inode, struct file *file) +{ + struct userland *userland = file->private_data; + + if (userland->flags & USERLAND_KILL_ON_CLOSE) { + struct pid *pid = NULL; + + scoped_guard(spinlock, &userland->lock) { + if (userland->state == USERLAND_RUNNING) + pid = get_pid(userland->init_pid); + } + if (pid) { + kill_pid(pid, SIGKILL, true); + put_pid(pid); + } + } + userland_put(userland); + return 0; +} + +static const struct file_operations userland_fops = { + .poll = userland_poll, + .release = userland_release, +}; + +bool userland_is_file(struct file *file) +{ + return file->f_op == &userland_fops; +} -- 2.47.3