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=1795910355; b=c0JHUWxHItlnRZp1tv5ZGcKijUzcvbPvkAT48l+z6UEOGS17MNqKWxM9GcrZgfr635nf TUvczPKPDi+fQWl7QQoCvlFMoYWt7mnefj4LepYwOXRFfCOc0WBeoajIVYm2dMss8NZEl Ka5n8BDQhkxRiaMj7p2QXcuCfNnvKUfOjZzlOfg7iwHUSg6xRzQXWFBgGyuEBnGaUfS4F ROlKyMW8VaQYNLybUufXn45nV4RP5ZVRxd8PGyZec4ys4hervE7BF3CncyybjZQXYcEp0 BnRey+TLMrV8BKrBDQmSg7U0B0U2bfDmNIvIEmyaCw4+6bRlzfSLR/OSOPaVjojRABA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795910355; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=/ipDVjPVI+9hYYecYKxafjd74wGNDtiCWUhVhpb40TA=; b=QPeMD96U/hbvS8idqIwq1NzAaYsKfx1dJgLXVdZTxm7g79+t0yPLCCx+BV1NHZ/1oJGd y6TomW8EHyOVwpQfaWGQHJ1Jfjn7SaU2mdACk4TwqBNKy0i94S3PJy8BC1M5EIAyAsyk9 E1hSrFxlC/bz636vrQfXPOwDgSH1eGvj0K0a6q/r/+qltiF5Apo7zd6l8YNxQVXrXcJTQ AE/EcW9Bla81KEiexX4X7zyeddScXcTb0vWM1Ib8OCCeY+TqwRZct4KYmj4PXv+QDQ5iS 5+da5h5JlWEpjriF36S20eNFZ1odaeCwYIT3qVctD1r2smmoT/kOHyz9x5+bDaSaetA== 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=G3a6aFKX; 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 32C53606A323; Sat, 28 Nov 2026 23:59:15 +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=G3a6aFKX Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 10FDB1F0089B; Sat, 28 Nov 2026 23:59:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795910354; bh=/ipDVjPVI+9hYYecYKxafjd74wGNDtiCWUhVhpb40TA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=G3a6aFKXC/URMfVD7aAa0YNNHWhvrHzQLfLSnyKAGHKFUjb4NktqzTfujccxY+vFL tX9250ZkfDqm+4XXUeSJQa2pVlZHhPHvFuVLNgxNtwXjATowrmr2lNIRSyHeNUytnu CxNpGyY6/VafzjDdYO6160pCoFLJ5I/bpR/Sy/InjwKmwOxIU97oc/tL2RjGjiBp2a KRHZFKT2Rq9cGk7fh+edVSKTAjK/GIgQFa787RBujEDPnpdtVerb0v82+S92Qb5fa9 kJ6gThkpXDFFMQQWS5+OIRB1t9g4LW+VSDCLtXZEDisc8CyQThPy90ph6LZjkQ9S5r EItbJ6jEHx9Yg== 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 14/29] userland: Add userland_open() system call Date: Sun, 29 Nov 2026 01:57:53 +0200 Message-ID: <20261128235808.2369970-15-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-doc@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 userland_open() system call to allocate a userland 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 userland 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 userland. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/syscalls.h | 1 + kernel/sys_ni.c | 3 ++ kernel/userland.c | 108 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 72308cc11908..2ea8ebd71743 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_userland_open(unsigned int flags); /* * Architecture-specific system calls diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index e3202e7cc2a1..fca9663d09b7 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/userland.c */ +COND_SYSCALL(userland_open); diff --git a/kernel/userland.c b/kernel/userland.c index b7aa6caecafd..22575f60e682 100644 --- a/kernel/userland.c +++ b/kernel/userland.c @@ -133,3 +133,111 @@ void userland_exit(struct pid_namespace *pid_ns, struct task_struct *tsk) __wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP)); userland_put(c); } + +static struct user_namespace *userland_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 userland *userland_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 userland *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 = userland_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: + userland_put(c); + return ERR_PTR(ret); +} + +/** + * sys_userland_open - Open a new userland + * @flags: Combination of &enum userland_flags values + * + * Allocate a userland 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 userland's owning user + * namespace. + * + * Return: Userland file descriptor on success, negative error code on failure. + */ +SYSCALL_DEFINE1(userland_open, unsigned int, flags) +{ + struct userland *c; + int fd; + + if (flags & ~USERLAND_ALL_FLAGS) + return -EINVAL; + + c = userland_create(flags); + if (IS_ERR(c)) + return PTR_ERR(c); + + fd = anon_inode_getfd("userland", &userland_fops, c, + O_RDWR | + (flags & USERLAND_CLOEXEC ? O_CLOEXEC : 0)); + if (fd < 0) + userland_put(c); + + return fd; +} -- 2.47.3