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=1796441260; b=tLEENauB/SnbDZIHfd9picrwvmLhxg7JTlVO/tcjAhcsDfrnyeYA1Nhz/n9cObHAoPYB a2BzsteB2AKL/lDmxMGdznd+uE32U3N530x503AO+P4kVauHbPtX3KwYgwt/amRQ0coW+ TF0lpncb2FHp98e7Pei6pvwkExXQDKqmsTBamCI0YuJRiv7yhmml/SHOkWlxx20+R4Eww rFnK2W5qV0J6GKrztaaEL3ISHvo67AUth2lRdEU4d7YQvuXXZpCHNWOcnCX+i545YG0Wf 9q+iIy8xPnL1b/2NoN54XlFEylhEEZScEG2aKpJSe48KJkN5nXZQc7FlM8z8RKVFqGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441260; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=FDd94ACrrzvwyahLuJZ7m4C4kkTM5G7Vn2nv4A0voJw=; b=l2fCyoQRsLvF7TjNcNxgDuZCyE2ptZ7AFnaXoyekSKNYdtuQBdPLGULY0AZ2xqr2onuf uTxRGhQAQ1QdjNzcxKpranw5XdPr/AfrFVPJWQNkiqbWTWAycRJOZes7K8Bi+1EL3G5VI yZG4JNvvWtiUEha7wc72Vhl0fTCMJZxAZup8K22rGWZVCQ8Gt1C1dCKVZSCrmgJxiRowo rPN6ZSzo3dHEmvJuJxC1VXIzFNvBXAjV5QYtGwUlOT/AY+QZlGz7osZyhBbIFwwU/GrhD 3CI6mEsfxNHaed8uqiGIhJBDBs205nQu+HHQu37mybwRjg+YLXmuJSy14RiuZhGSH/A== 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=mzzlaVwk; 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 D0EE3606A330; Sat, 5 Dec 2026 03:27:40 +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=mzzlaVwk Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id C29781F00A2D; Sat, 5 Dec 2026 03:27:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441260; bh=FDd94ACrrzvwyahLuJZ7m4C4kkTM5G7Vn2nv4A0voJw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mzzlaVwkRjnpRSem7pWCQo0U+LRPDvUuezDKhBM9deeThONNqT1dUOB9/O3hZGvi4 Y0xPAAsyJi1sUQyXz8aFeqRX6UMbgDeJOfzB6qSOQqG8DW07b4bY2zJpnZXKMsEHA2 /21EehLuSANj9vXpRk1ys2omm65LgBiipi1gvBWmPn7LeapU12uLEcRnHDHJ84t/Hr k1haJ9W7b2YytruYjyknun/TGxtuFFqrNZyWD3omp4cP0zkNQ92zYOJMdR0OnzrQDt xXpwz9Ay2gRtfAG6Ojc4fuOCIT2pdY5skinPk0yGqqb5uXSuPMVtIWHebvXUTfGXIl UCP3zcCD8x40g== 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 13/28] runfd: Add runfd() system call Date: Sat, 5 Dec 2026 05:26:18 +0200 Message-ID: <20261205032633.1702772-14-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-arch@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 runfd() system call to allocate a run 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 run 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 run. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- include/linux/syscalls.h | 1 + kernel/runfd.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 552d9710a0d5..2c2db703a005 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1006,6 +1006,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_runfd(unsigned int flags); /* * Architecture-specific system calls diff --git a/kernel/runfd.c b/kernel/runfd.c index 2dfd5c55ca41..6b250796c24e 100644 --- a/kernel/runfd.c +++ b/kernel/runfd.c @@ -135,3 +135,110 @@ void runfd_exit(struct pid_namespace *pid_ns, struct task_struct *tsk) __wake_up(&c->waitq, TASK_NORMAL, 0, poll_to_key(EPOLLHUP)); runfd_put(c); } + +static struct user_namespace *runfd_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 run *runfd_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 run *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 = runfd_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: + runfd_put(c); + return ERR_PTR(ret); +} + +/** + * sys_runfd - Create a run and return its file descriptor + * @flags: Combination of &enum runfd_flags values + * + * Allocate a run 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 run's owning user + * namespace. + * + * Return: Close-on-exec run file descriptor on success, negative error code + * on failure. + */ +SYSCALL_DEFINE1(runfd, unsigned int, flags) +{ + struct run *c; + int fd; + + if (flags & ~RUNFD_ALL_FLAGS) + return -EINVAL; + + c = runfd_create(flags); + if (IS_ERR(c)) + return PTR_ERR(c); + + fd = anon_inode_getfd("runfd", &runfd_fops, c, O_RDWR | O_CLOEXEC); + if (fd < 0) + runfd_put(c); + + return fd; +} diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index c8be0abaa407..9c9ee50443fb 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -398,3 +398,6 @@ COND_SYSCALL(rseq_slice_yield); COND_SYSCALL(uretprobe); COND_SYSCALL(uprobe); + +/* kernel/runfd.c */ +COND_SYSCALL(runfd); -- 2.47.3