Generic Linux architectural discussions
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: fsdevel@vger.kernel.org
Cc: "Jarkko Sakkinen" <jarkko@kernel.org>,
	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" <aleksa@amutable.com>,
	"Alexander Mikhalitsyn" <alex@amutable.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Ben Segall" <bsegall@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"David Hildenbrand" <david@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Georgia Garcia" <georgia.garcia@canonical.com>,
	"Giuseppe Scrivano" <giuseppe@scrivano.org>,
	"Günther Noack" <gnoack@google.com>,
	"Hugh Dickins" <hughd@google.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Ingo Molnar" <mingo@redhat.com>,
	"James Morris" <jmorris@namei.org>, "Jan Kara" <jack@suse.cz>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"John Johansen" <john.johansen@canonical.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Kees Cook" <kees@kernel.org>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Lennart Poettering" <lennart@poettering.net>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Manfred Spraul" <manfred@colorfullife.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Matt Bobrowski" <matt@bobrowski.net>,
	"Mel Gorman" <mgorman@suse.de>, "Michal Hocko" <mhocko@suse.com>,
	"Mickaël Salaün" <mic@digikod.net>,
	"Mike Rapoport" <rppt@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Oleg Nesterov" <oleg@redhat.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Song Liu" <song@kernel.org>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: [RFC PATCH v9 19/29] fork: Support CLONE_INTO_NSEXEC in clone3()
Date: Tue,  1 Dec 2026 05:09:51 +0200	[thread overview]
Message-ID: <20261201031002.593830-20-jarkko@kernel.org> (raw)
In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org>

Multithreaded userspace daemons cannot call setns(CLONE_NEWUSER) or
safely mutate process-wide filesystem state in place before spawning a
child into a target namespace set.

Add CLONE_INTO_NSEXEC and struct clone_args.nsexec
(CLONE_ARGS_SIZE_VER3) to spawn a child process directly into an nsexec
whose root mount has been installed. Transition the nsexec from
NSEXEC_INIT through NSEXEC_FORKING to NSEXEC_RUNNING for the first
process so it becomes PID 1, serialize concurrent entrants on waitq
until bootstrap resolves, and admit subsequent callers into the running
PID namespace until teardown begins.

Derive child credentials from current, require CAP_SYS_ADMIN and
CAP_SYS_CHROOT in the target user namespace and an ancestor active PID
namespace, preserve caller umask, reset dumpability when exec_state
originates outside the target user namespace, commit time namespace vvar
mappings after copy_process() succeeds, and look up the live mount
namespace root. Restrict companion flags to CLONE_VFORK, CLONE_PIDFD,
CLONE_PIDFD_AUTOKILL, CLONE_AUTOREAP, CLONE_CLEAR_SIGHAND,
CLONE_INTO_CGROUP, and CLONE_NNP.

Co-developed-by: David Howells <dhowells@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 include/linux/cred.h       |   4 +-
 include/linux/nsexec.h     |  15 +++
 include/linux/nsproxy.h    |   4 +-
 include/linux/sched/task.h |   6 ++
 include/uapi/linux/sched.h |   5 +
 kernel/cred.c              |  20 +++-
 kernel/fork.c              |  83 ++++++++++++++--
 kernel/nsexec.c            | 188 ++++++++++++++++++++++++++++++++++++-
 kernel/nsproxy.c           |   9 +-
 9 files changed, 318 insertions(+), 16 deletions(-)

diff --git a/include/linux/cred.h b/include/linux/cred.h
index 49c26af37349..f7e2f6f25a94 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -19,6 +19,7 @@
 
 struct cred;
 struct inode;
+struct nsexec;
 
 extern struct task_struct init_task;
 
@@ -152,7 +153,8 @@ struct cred {
 
 extern void __put_cred(struct cred *);
 extern void exit_creds(struct task_struct *);
-extern int copy_creds(struct task_struct *, u64);
+int copy_creds(struct task_struct *tsk, u64 clone_flags,
+	       struct nsexec *nsexec);
 extern const struct cred *get_task_cred(struct task_struct *);
 extern struct cred *cred_alloc_blank(void);
 extern struct cred *prepare_creds(void);
diff --git a/include/linux/nsexec.h b/include/linux/nsexec.h
index 6eee61c30f3c..5e1d40e51205 100644
--- a/include/linux/nsexec.h
+++ b/include/linux/nsexec.h
@@ -16,6 +16,7 @@
 #define NSEXEC_ALL_FLAGS	NSEXEC_AUTOKILL
 
 struct file;
+struct kernel_clone_args;
 struct nsproxy;
 struct pid;
 struct pid_namespace;
@@ -82,6 +83,9 @@ long nsexec_waitid(int upid, struct waitid_info *infop, int options,
 		    struct rusage *ru);
 int nsexec_send_signal(struct file *file, int sig,
 			struct siginfo __user *info, unsigned int flags);
+int nsexec_prepare_fork(struct kernel_clone_args *args);
+int nsexec_finish_fork(struct kernel_clone_args *args,
+			struct task_struct *p);
 #else
 static inline void nsexec_put(struct nsexec *c)
 {
@@ -113,6 +117,17 @@ static inline int nsexec_send_signal(struct file *file, int sig,
 {
 	return -EBADF;
 }
+
+static inline int nsexec_prepare_fork(struct kernel_clone_args *args)
+{
+	return -EINVAL;
+}
+
+static inline int nsexec_finish_fork(struct kernel_clone_args *args,
+				      struct task_struct *p)
+{
+	return 0;
+}
 #endif /* CONFIG_NSEXEC */
 
 #endif /* _LINUX_NSEXEC_H */
diff --git a/include/linux/nsproxy.h b/include/linux/nsproxy.h
index d611c40cb4ce..46c83d2d5deb 100644
--- a/include/linux/nsproxy.h
+++ b/include/linux/nsproxy.h
@@ -12,6 +12,7 @@ struct ipc_namespace;
 struct pid_namespace;
 struct cgroup_namespace;
 struct fs_struct;
+struct nsexec;
 struct user_namespace;
 
 /*
@@ -96,7 +97,8 @@ static inline struct cred *nsset_cred(struct nsset *set)
 struct nsproxy *create_new_namespaces(u64 flags, struct task_struct *tsk,
 				      struct user_namespace *user_ns,
 				      struct fs_struct *new_fs);
-int copy_namespaces(u64 flags, struct task_struct *tsk);
+int copy_namespaces(u64 flags, struct task_struct *tsk,
+		    struct nsexec *nsexec);
 void switch_cred_namespaces(const struct cred *old, const struct cred *new);
 void exit_nsproxy_namespaces(struct task_struct *tsk);
 void get_cred_namespaces(struct task_struct *tsk);
diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index 90ed5bc3c7af..2053ceb91fa6 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -16,6 +16,8 @@ struct task_struct;
 struct rusage;
 union thread_union;
 struct css_set;
+struct file;
+struct nsexec;
 
 /* All the bits taken by the old clone syscall. */
 #define CLONE_LEGACY_FLAGS 0xffffffffULL
@@ -39,11 +41,15 @@ struct kernel_clone_args {
 	/* Number of elements in *set_tid */
 	size_t set_tid_size;
 	int cgroup;
+	int nsexec_fd;
 	int idle;
 	int (*fn)(void *);
 	void *fn_arg;
 	struct cgroup *cgrp;
 	struct css_set *cset;
+	struct file *nsexec_file;
+	struct nsexec *nsexec;
+	bool nsexec_bootstrapping;
 	unsigned int kill_seq;
 };
 
diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h
index 19ffeba89428..2703c53fbdc0 100644
--- a/include/uapi/linux/sched.h
+++ b/include/uapi/linux/sched.h
@@ -40,6 +40,7 @@
 #define CLONE_NNP		(1ULL << 35) /* Set no_new_privs on child. */
 #define CLONE_PIDFD_AUTOKILL	(1ULL << 36) /* Kill child when clone pidfd closes. */
 #define CLONE_EMPTY_MNTNS	(1ULL << 37) /* Create an empty mount namespace. */
+#define CLONE_INTO_NSEXEC	(1ULL << 38) /* Clone into an nsexec. */
 
 /*
  * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
@@ -94,6 +95,8 @@
  *                kernel's limit of nested PID namespaces.
  * @cgroup:       If CLONE_INTO_CGROUP is specified set this to
  *                a file descriptor for the cgroup.
+ * @nsexec:       If CLONE_INTO_NSEXEC is specified set this to
+ *                a file descriptor for the nsexec.
  *
  * The structure is versioned by size and thus extensible.
  * New struct members must go at the end of the struct and
@@ -111,12 +114,14 @@ struct clone_args {
 	__aligned_u64 set_tid;
 	__aligned_u64 set_tid_size;
 	__aligned_u64 cgroup;
+	__aligned_u64 nsexec;
 };
 #endif
 
 #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
 #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
 #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
+#define CLONE_ARGS_SIZE_VER3 96 /* sizeof fourth published struct */
 
 /*
  * Scheduling policies
diff --git a/kernel/cred.c b/kernel/cred.c
index d01df40a9a38..09128a4846df 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -12,13 +12,16 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/sched/coredump.h>
+#include <linux/sched/exec_state.h>
 #include <linux/key.h>
 #include <linux/keyctl.h>
 #include <linux/init_task.h>
+#include <linux/nsexec.h>
 #include <linux/security.h>
 #include <linux/binfmts.h>
 #include <linux/cn_proc.h>
 #include <linux/uidgid.h>
+#include <linux/user_namespace.h>
 
 #if 0
 #define kdebug(FMT, ...)						\
@@ -260,7 +263,8 @@ struct cred *prepare_exec_creds(void)
  * The new process gets the current process's subjective credentials as its
  * objective and subjective credentials
  */
-int copy_creds(struct task_struct *p, u64 clone_flags)
+int copy_creds(struct task_struct *p, u64 clone_flags,
+	       struct nsexec *nsexec)
 {
 	struct cred *new;
 	int ret;
@@ -287,6 +291,20 @@ int copy_creds(struct task_struct *p, u64 clone_flags)
 	if (!new)
 		return -ENOMEM;
 
+	if (nsexec) {
+		struct task_exec_state *exec_state;
+
+		if (new->user_ns != nsexec->user_ns) {
+			ret = install_user_ns_cred(new, nsexec->user_ns);
+			if (ret < 0)
+				goto error_put;
+		}
+		exec_state = rcu_dereference_protected(p->exec_state, true);
+		if (exec_state->user_ns != nsexec->user_ns)
+			WRITE_ONCE(exec_state->dumpable,
+				   READ_ONCE(suid_dumpable));
+	}
+
 	if (clone_flags & CLONE_NEWUSER) {
 		ret = create_user_ns(new);
 		if (ret < 0)
diff --git a/kernel/fork.c b/kernel/fork.c
index 83c2f87aaf75..01e55d094ff6 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -65,6 +65,7 @@
 #include <linux/rcupdate.h>
 #include <linux/ptrace.h>
 #include <linux/mount.h>
+#include <linux/mnt_namespace.h>
 #include <linux/audit.h>
 #include <linux/memcontrol.h>
 #include <linux/ftrace.h>
@@ -1616,7 +1617,7 @@ static int copy_exec_state(u64 clone_flags, struct task_struct *tsk)
 	struct task_exec_state *exec_state;
 
 	/* CLONE_VM siblings refcount-share the parent's exec_state. */
-	if (clone_flags & CLONE_VM) {
+	if ((clone_flags & (CLONE_VM | CLONE_INTO_NSEXEC)) == CLONE_VM) {
 		exec_state = rcu_dereference_protected(current->exec_state, true);
 		refcount_inc(&exec_state->count);
 		rcu_assign_pointer(tsk->exec_state, exec_state);
@@ -1633,7 +1634,7 @@ enum copy_fs_flags_t {
 };
 
 static int copy_fs(u64 clone_flags, struct task_struct *tsk,
-		   enum copy_fs_flags_t flags)
+		   enum copy_fs_flags_t flags, struct nsexec *nsexec)
 {
 	struct fs_struct *fs;
 
@@ -1649,7 +1650,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk,
 		if (current->nsproxy->mnt_ns != &init_mnt_ns)
 			return -EINVAL;
 		fs = userspace_init_fs;
-	} else if (flags & COPY_FS_IDLE) {
+	} else if (flags & COPY_FS_IDLE || nsexec) {
 		fs = &init_fs;
 	} else {
 		fs = current->fs;
@@ -1671,6 +1672,21 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk,
 	tsk->real_fs = tsk->fs = copy_fs_struct(fs);
 	if (!tsk->fs)
 		return -ENOMEM;
+	if (nsexec) {
+		struct path root __free(path_put) = {};
+		int err;
+
+		err = mnt_ns_get_root(nsexec->ns->mnt_ns, &root);
+		if (err) {
+			free_fs_struct(tsk->fs);
+			tsk->real_fs = NULL;
+			tsk->fs = NULL;
+			return err;
+		}
+		tsk->fs->umask = READ_ONCE(current->fs->umask);
+		set_fs_root(tsk->fs, &root);
+		set_fs_pwd(tsk->fs, &root);
+	}
 	return 0;
 }
 
@@ -2019,6 +2035,17 @@ static bool need_futex_hash_allocate_default(u64 clone_flags)
 	return clone_flags & CLONE_VM;
 }
 
+static bool pid_ns_is_adding(const struct pid *pid)
+{
+	int i;
+
+	for (i = pid->level; i >= 0; i--) {
+		if (unlikely(!(pid->numbers[i].ns->pid_allocated & PIDNS_ADDING)))
+			return false;
+	}
+	return true;
+}
+
 /*
  * This creates a new process as a copy of the old one,
  * but does not actually start it yet.
@@ -2186,7 +2213,7 @@ __latent_entropy struct task_struct *copy_process(
 #ifdef CONFIG_PROVE_LOCKING
 	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
 #endif
-	retval = copy_creds(p, clone_flags);
+	retval = copy_creds(p, clone_flags, args->nsexec);
 	if (retval < 0)
 		goto bad_fork_free;
 
@@ -2317,7 +2344,7 @@ __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_semundo;
 	retval = copy_fs(clone_flags, p,
 			 (args->umh ? COPY_FS_UMH : 0) |
-			 (args->idle ? COPY_FS_IDLE : 0));
+			 (args->idle ? COPY_FS_IDLE : 0), args->nsexec);
 	if (retval)
 		goto bad_fork_cleanup_semundo;
 	retval = copy_sighand(clone_flags, p);
@@ -2329,7 +2356,7 @@ __latent_entropy struct task_struct *copy_process(
 	retval = copy_mm(clone_flags, p);
 	if (retval)
 		goto bad_fork_cleanup_signal;
-	retval = copy_namespaces(clone_flags, p);
+	retval = copy_namespaces(clone_flags, p, args->nsexec);
 	if (retval)
 		goto bad_fork_cleanup_mm;
 	retval = copy_io(clone_flags, p);
@@ -2505,11 +2532,16 @@ __latent_entropy struct task_struct *copy_process(
 	 * child missed SIGKILL.  If current is not in the same namespace,
 	 * we can't rely on fatal_signal_pending() below.
 	 */
-	if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
+	if (unlikely(!pid_ns_is_adding(pid))) {
 		retval = -ENOMEM;
 		goto bad_fork_core_free;
 	}
 
+	if (unlikely(args->nsexec_bootstrapping && !is_child_reaper(pid))) {
+		retval = -EBUSY;
+		goto bad_fork_core_free;
+	}
+
 	/* Let kill terminate clone/fork in the middle */
 	if (fatal_signal_pending(current)) {
 		retval = -EINTR;
@@ -2586,7 +2618,7 @@ __latent_entropy struct task_struct *copy_process(
 	write_unlock_irq(&tasklist_lock);
 
 	if (likely(p->pid) && thread_group_leader(p))
-		nsexec_copy(pid, NULL);
+		nsexec_copy(pid, args->nsexec);
 
 	if (pidfile)
 		fd_install(pidfd, pidfile);
@@ -2792,6 +2824,13 @@ pid_t kernel_clone(struct kernel_clone_args *args)
 	if (!valid_signal(args->exit_signal))
 		return -EINVAL;
 
+	if (unlikely(clone_flags & CLONE_INTO_NSEXEC)) {
+		int err = nsexec_prepare_fork(args);
+
+		if (err)
+			return err;
+	}
+
 	/*
 	 * Determine whether and which event to report to ptracer.  When
 	 * called from kernel_thread or CLONE_UNTRACED is explicitly
@@ -2813,6 +2852,13 @@ pid_t kernel_clone(struct kernel_clone_args *args)
 	p = copy_process(NULL, trace, NUMA_NO_NODE, args);
 	add_latent_entropy();
 
+	if (unlikely(clone_flags & CLONE_INTO_NSEXEC)) {
+		int err = nsexec_finish_fork(args, p);
+
+		if (err)
+			return err;
+	}
+
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
@@ -2970,7 +3016,9 @@ static noinline int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 		     CLONE_ARGS_SIZE_VER1);
 	BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) !=
 		     CLONE_ARGS_SIZE_VER2);
-	BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2);
+	BUILD_BUG_ON(offsetofend(struct clone_args, nsexec) !=
+		     CLONE_ARGS_SIZE_VER3);
+	BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER3);
 
 	if (unlikely(usize > PAGE_SIZE))
 		return -E2BIG;
@@ -3000,6 +3048,10 @@ static noinline int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 	    (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2))
 		return -EINVAL;
 
+	if ((args.flags & CLONE_INTO_NSEXEC) &&
+	    (args.nsexec > INT_MAX || usize < CLONE_ARGS_SIZE_VER3))
+		return -EINVAL;
+
 	*kargs = (struct kernel_clone_args){
 		.flags		= args.flags,
 		.pidfd		= u64_to_user_ptr(args.pidfd),
@@ -3011,6 +3063,7 @@ static noinline int copy_clone_args_from_user(struct kernel_clone_args *kargs,
 		.tls		= args.tls,
 		.set_tid_size	= args.set_tid_size,
 		.cgroup		= args.cgroup,
+		.nsexec_fd	= args.nsexec,
 	};
 
 	if (args.set_tid &&
@@ -3051,13 +3104,19 @@ static inline bool clone3_stack_valid(struct kernel_clone_args *kargs)
 	return true;
 }
 
+#define CLONE_INTO_NSEXEC_FLAGS \
+	(CLONE_INTO_NSEXEC | CLONE_VFORK | CLONE_PIDFD | \
+	 CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP | CLONE_AUTOREAP | \
+	 CLONE_NNP | CLONE_PIDFD_AUTOKILL)
+
 static bool clone3_args_valid(struct kernel_clone_args *kargs)
 {
 	/* Verify that no unknown flags are passed along. */
 	if (kargs->flags &
 	    ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND |
 	      CLONE_INTO_CGROUP | CLONE_AUTOREAP | CLONE_NNP |
-	      CLONE_PIDFD_AUTOKILL | CLONE_EMPTY_MNTNS))
+	      CLONE_PIDFD_AUTOKILL | CLONE_EMPTY_MNTNS |
+	      CLONE_INTO_NSEXEC))
 		return false;
 
 	/*
@@ -3075,6 +3134,10 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs)
 	    kargs->exit_signal)
 		return false;
 
+	if ((kargs->flags & CLONE_INTO_NSEXEC) &&
+	    (kargs->flags & ~CLONE_INTO_NSEXEC_FLAGS))
+		return false;
+
 	if (!clone3_stack_valid(kargs))
 		return false;
 
diff --git a/kernel/nsexec.c b/kernel/nsexec.c
index 895cc8c3429d..761f994977c1 100644
--- a/kernel/nsexec.c
+++ b/kernel/nsexec.c
@@ -18,6 +18,7 @@
 #include <linux/file.h>
 #include <linux/freezer.h>
 #include <linux/fs.h>
+#include <linux/fs_struct.h>
 #include <linux/anon_inodes.h>
 #include <linux/nsexec.h>
 #include <linux/sched/signal.h>
@@ -131,6 +132,12 @@ static long nsexec_get_nsfd(struct nsexec *nsexec, unsigned long nstype)
 	return open_namespace(ns);
 }
 
+static bool nsexec_may_use_ns(struct ns_common *ns)
+{
+	return is_current_namespace(ns) ||
+	       ns_capable(ns->ops->owner(ns), CAP_SYS_ADMIN);
+}
+
 static long nsexec_share_nsfd(struct nsexec *c, unsigned long arg)
 {
 	struct ns_common *old_time_children = NULL;
@@ -163,8 +170,7 @@ static long nsexec_share_nsfd(struct nsexec *c, unsigned long arg)
 
 	if (!ns_capable(c->user_ns, CAP_SYS_ADMIN))
 		return -EPERM;
-	if (!is_current_namespace(ns) &&
-	    !ns_capable(ns->ops->owner(ns), CAP_SYS_ADMIN))
+	if (!nsexec_may_use_ns(ns))
 		return -EPERM;
 
 	scoped_guard(spinlock, &c->lock) {
@@ -549,3 +555,181 @@ SYSCALL_DEFINE1(nsexec_open, unsigned int, flags)
 
 	return fd;
 }
+
+/**
+ * DOC: CLONE_INTO_NSEXEC
+ *
+ * Passing ``CLONE_INTO_NSEXEC`` in ``struct clone_args.flags`` (with
+ * ``size >= CLONE_ARGS_SIZE_VER3``) spawns the child into the nsexec identified
+ * by ``struct clone_args.nsexec`` once ``"/"`` is mounted in its mount
+ * namespace. The first caller bootstraps PID 1 while concurrent bootstrap
+ * callers wait killably; subsequent callers join the running PID namespace
+ * until teardown begins.
+ *
+ * Requires ``CAP_SYS_ADMIN`` and ``CAP_SYS_CHROOT`` in the target user
+ * namespace (plus ``CAP_SYS_CHROOT`` in the caller's user namespace when
+ * chrooted), membership or ``CAP_SYS_ADMIN`` in the owning user namespace of
+ * any shared namespaces, and an ancestor active PID namespace. May be combined
+ * with ``CLONE_VFORK``, ``CLONE_PIDFD``, ``CLONE_PIDFD_AUTOKILL``,
+ * ``CLONE_AUTOREAP``, ``CLONE_CLEAR_SIGHAND``, ``CLONE_INTO_CGROUP``, and
+ * ``CLONE_NNP``.
+ *
+ * When ``current`` is outside the nsexec's user namespace, the child's
+ * credentials are copied from ``current`` and installed into the nsexec's
+ * user namespace with capabilities recomputed as in ``setns(CLONE_NEWUSER)``,
+ * retaining ``current``'s kernel UID, GID, and supplementary groups as mapped
+ * there.
+ *
+ * ``CLONE_VM``, ``CLONE_FILES``, ``CLONE_FS``, and ``CLONE_SIGHAND`` are
+ * rejected so the child never shares mutable task state with the caller.
+ * Whenever the caller's ``exec_state`` originates outside the nsexec's user
+ * namespace, the child keeps ``exec_state->user_ns`` in that outer user
+ * namespace and resets dumpability to ``suid_dumpable``; until ``execve()``,
+ * ``ptrace_may_access()`` checks ``CAP_SYS_PTRACE`` against the outer user
+ * namespace, preventing processes already running inside the nsexec from
+ * tracing the child or accessing ``/proc/<pid>/{exe,mem,fd}``.
+ */
+static bool nsexec_pidns_alive(struct pid_namespace *pid_ns)
+{
+	for (; pid_ns != &init_pid_ns; pid_ns = pid_ns->parent) {
+		if (!(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING))
+			return false;
+	}
+	return true;
+}
+
+static int nsexec_check_namespaces(struct nsexec *nsexec)
+{
+	struct nsproxy *ns = nsexec->ns;
+
+	if (!nsexec_may_use_ns(to_ns_common(ns->cgroup_ns)) ||
+	    !nsexec_may_use_ns(to_ns_common(ns->ipc_ns)) ||
+	    !nsexec_may_use_ns(to_ns_common(ns->uts_ns)) ||
+	    !nsexec_may_use_ns(to_ns_common(ns->net_ns)))
+		return -EPERM;
+#ifdef CONFIG_TIME_NS
+	if (!nsexec_may_use_ns(to_ns_common(ns->time_ns)))
+		return -EPERM;
+#endif
+	return 0;
+}
+
+int nsexec_prepare_fork(struct kernel_clone_args *args)
+{
+	struct file *file __free(fput) = fget(args->nsexec_fd);
+	struct path root __free(path_put) = {};
+	struct pid_namespace *pid_ns;
+	bool bootstrapping = false;
+	struct nsexec *nsexec;
+	int ret;
+
+	if (!file)
+		return -EBADF;
+	if (!nsexec_is_file(file))
+		return -EINVAL;
+
+	nsexec = file->private_data;
+	if (!ns_capable(nsexec->user_ns, CAP_SYS_ADMIN) ||
+	    !ns_capable(nsexec->user_ns, CAP_SYS_CHROOT))
+		return -EPERM;
+	if (current_chrooted() && !ns_capable(current_user_ns(), CAP_SYS_CHROOT))
+		return -EPERM;
+
+	pid_ns = nsexec->ns->pid_ns_for_children;
+	if (!pidns_is_ancestor(pid_ns, task_active_pid_ns(current)))
+		return -EXDEV;
+
+	ret = mnt_ns_get_root(nsexec->ns->mnt_ns, &root);
+	if (ret)
+		return ret;
+
+	for (;;) {
+		bool wait = false;
+
+		scoped_guard(spinlock, &nsexec->lock) {
+			switch (nsexec->state) {
+			case NSEXEC_INIT:
+				if (!nsexec_pidns_alive(pid_ns))
+					return -ESRCH;
+				if (READ_ONCE(pid_ns->child_reaper))
+					return -EBUSY;
+				WRITE_ONCE(nsexec->state, NSEXEC_FORKING);
+				bootstrapping = true;
+				break;
+			case NSEXEC_FORKING:
+				wait = true;
+				break;
+			case NSEXEC_RUNNING:
+				if (!nsexec_pidns_alive(pid_ns))
+					return -ESRCH;
+				break;
+			case NSEXEC_DEAD:
+				return -ESRCH;
+			}
+		}
+		if (!wait)
+			break;
+
+		ret = wait_event_killable(nsexec->waitq,
+					  READ_ONCE(nsexec->state) !=
+					  NSEXEC_FORKING);
+		if (ret)
+			return ret;
+	}
+
+	ret = nsexec_check_namespaces(nsexec);
+	if (ret)
+		goto err_forking;
+
+	args->nsexec_file = no_free_ptr(file);
+	args->nsexec = nsexec;
+	args->nsexec_bootstrapping = bootstrapping;
+	return 0;
+
+err_forking:
+	if (bootstrapping) {
+		scoped_guard(spinlock, &nsexec->lock) {
+			if (nsexec->state == NSEXEC_FORKING)
+				WRITE_ONCE(nsexec->state, NSEXEC_INIT);
+		}
+		wake_up_all(&nsexec->waitq);
+	}
+	return ret;
+}
+
+int nsexec_finish_fork(struct kernel_clone_args *args,
+			struct task_struct *p)
+{
+	struct file *file __free(fput) = args->nsexec_file;
+	struct nsexec *nsexec = args->nsexec;
+	struct pid_namespace *pid_ns = nsexec->ns->pid_ns_for_children;
+	int ret;
+
+	args->nsexec_file = NULL;
+	args->nsexec = NULL;
+	if (!IS_ERR(p)) {
+		if (args->nsexec_bootstrapping)
+			wake_up_all(&nsexec->waitq);
+		if (current->nsproxy->time_ns != nsexec->ns->time_ns)
+			timens_commit(p, nsexec->ns->time_ns);
+		return 0;
+	}
+
+	ret = PTR_ERR(p);
+	if (args->nsexec_bootstrapping) {
+		scoped_guard(spinlock, &nsexec->lock) {
+			if (nsexec->state == NSEXEC_FORKING)
+				WRITE_ONCE(nsexec->state, NSEXEC_INIT);
+		}
+		wake_up_all(&nsexec->waitq);
+		if (ret == -EINVAL && !args->set_tid_size &&
+		    (READ_ONCE(pid_ns->child_reaper) ||
+		     (READ_ONCE(pid_ns->pid_allocated) & ~PIDNS_ADDING)))
+			ret = -EBUSY;
+	}
+	if ((ret == -ENOMEM || ret == -EBUSY) &&
+	    !nsexec_pidns_alive(pid_ns))
+		return -ESRCH;
+
+	return ret;
+}
diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
index 3c4161515c3c..83471e420b2a 100644
--- a/kernel/nsproxy.c
+++ b/kernel/nsproxy.c
@@ -11,6 +11,7 @@
 
 #include <linux/slab.h>
 #include <linux/export.h>
+#include <linux/nsexec.h>
 #include <linux/nsproxy.h>
 #include <linux/ns/ns_common_types.h>
 #include <linux/init_task.h>
@@ -166,12 +167,18 @@ struct nsproxy *create_new_namespaces(u64 flags, struct task_struct *tsk,
  * called from clone.  This now handles copy for nsproxy and all
  * namespaces therein.
  */
-int copy_namespaces(u64 flags, struct task_struct *tsk)
+int copy_namespaces(u64 flags, struct task_struct *tsk,
+		    struct nsexec *nsexec)
 {
 	struct nsproxy *old_ns = tsk->nsproxy;
 	struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
 	struct nsproxy *new_ns;
 
+	if (nsexec) {
+		get_nsproxy(nsexec->ns);
+		tsk->nsproxy = nsexec->ns;
+		return 0;
+	}
 	if (likely(!(flags & (CLONE_NS_ALL & ~CLONE_NEWUSER)))) {
 		if ((flags & CLONE_VM) ||
 		    likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
-- 
2.47.3


  parent reply	other threads:[~2026-12-01  3:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  3:09 [RFC PATCH v9 00/29] nsexec: Staged namespace execution Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 01/29] userns: Factor out idmap validation and commit helpers Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 02/29] userns: Add userns_set_idmap() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 03/29] arch: Wire up " Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 04/29] mount: Add mnt_ns_get_root() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 05/29] mount: Enforce mount_too_revealing() for foreign mntns Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 06/29] mount: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 07/29] security: Pass flags to security_move_mount() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 08/29] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 09/29] apparmor: Mediate MOVE_MOUNT_CLONE as recursive bind Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 10/29] userns: Factor out install_user_ns_cred() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 11/29] nsproxy: Expose create_new_namespaces() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 12/29] nsexec: Add core infrastructure Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 13/29] nsexec: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 14/29] nsexec: Add nsexec_open() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 15/29] nsexec: Add NSEXEC_GET_NSFD ioctl Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 16/29] nsexec: Add NSEXEC_SHARE_NSFD ioctl Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 17/29] exit: Support P_NSEXEC in waitid() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 18/29] signal: Support nsexec fds in pidfd_send_signal() Jarkko Sakkinen
2026-12-01  3:09 ` Jarkko Sakkinen [this message]
2026-12-01  3:09 ` [RFC PATCH v9 20/29] fs: Support FSCONFIG_SET_NSEXEC in fsconfig() Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 21/29] proc: Implement ->set_nsexec() fs_context operation Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 22/29] ipc/mqueue: " Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 23/29] security: Add nsexec LSM hooks Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 24/29] landlock: Enforce namespace use restrictions on nsexec Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 25/29] bpf: Register nsexec LSM hooks Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 26/29] arch: Wire up nsexec_open() system call Jarkko Sakkinen
2026-12-01  3:09 ` [RFC PATCH v9 27/29] docs: Document nsexec uAPI Jarkko Sakkinen
2026-12-01  3:10 ` [RFC PATCH v9 28/29] selftests/nsexec: Add nsexec test suite Jarkko Sakkinen
2026-12-01  3:10 ` [RFC PATCH v9 29/29] MAINTAINERS: Add entry for NSEXEC Jarkko Sakkinen
2026-12-01  4:02 ` [RFC PATCH v9 00/29] nsexec: Staged namespace execution Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201031002.593830-20-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aleksa@amutable.com \
    --cc=alex@amutable.com \
    --cc=andrii@kernel.org \
    --cc=arnd@arndb.de \
    --cc=ast@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=containers@lists.linux.dev \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=dwmw2@infradead.org \
    --cc=ebiggers@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=fsdevel@vger.kernel.org \
    --cc=georgia.garcia@canonical.com \
    --cc=giuseppe@scrivano.org \
    --cc=gnoack@google.com \
    --cc=hughd@google.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jgg@nvidia.com \
    --cc=jlayton@kernel.org \
    --cc=jmorris@namei.org \
    --cc=john.johansen@canonical.com \
    --cc=jolsa@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=kpsingh@kernel.org \
    --cc=lennart@poettering.net \
    --cc=liam@infradead.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=manfred@colorfullife.com \
    --cc=martin.lau@linux.dev \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=mic@digikod.net \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=oleg@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=peterz@infradead.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=serge@hallyn.com \
    --cc=sergeh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=song@kernel.org \
    --cc=stephen.smalley.work@gmail.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=yonghong.song@linux.dev \
    --cc=zygmunt.krynicki@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox