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=1796094718; b=O7GzZHuglCMkqbsbhoXE9U+KVSl+VQsKB0zynEo7P5PwF3q3SUpGoqd/vAxd1Mu5dCY4 6JUW8iOi2WVed9TEStM6/ioUCANEpdUIIRYMVBVs/VE4DvNvJP4jw4xVK/zzndgKVG7yl ScCJGCpq+ePrVc9p548txHs/xZZfyDXiQh1QLYJfiKh5ww86ee++Oj6n1shCdqL5DRKxH I7XsOWPb/qKDsdkAg4aCS63kcp0hQsP4lXyZvY1douWqNB1s5dNQFmCFu6EY5vRUuja3L qiH7vnX+mYKFg0oxK/NJPBAnpqp7JsiK/fPxYavD95QOstZM2uFUNCVG7Z15MlO+kww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796094718; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Bqq4fHOs2KGOpiV3gjJCgIRg+cl6xiiMS2tALTUHT18=; b=AatRBx2KN/v+lNrpwbUENHv6G7l6DVprtE61QTGqjco7WveRJzChbAi1KKIUGzPuZsCc wNBGAqdbhOgMLvzQEpqS9aaOalR0okG0NlMjmgkiByAX1ACxRXqHCRR95XLn3EZVyuWBO ofw8I2kFZQqhNhClsvJOZ4bKvUOmpirDK5XfNnZSwrbrWxY5NGK7KPTtp6jkfKyujUcZU HMrVfycDcsbA+aBEOlqSgql69zN2eCFpqZaUhMU7zok7kcCyQZAllIe8yx7bv+SKy/ray knWYBRG54TKTsGvWvtrB20QNxJUZ/rDP8IIXd26NGCCeQheN8tMCvFoEEMPTlnK5/FQ== 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=YBEnJiBk; 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 392A8606A688; Tue, 1 Dec 2026 03:11:57 +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=YBEnJiBk Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 055611F00927; Tue, 1 Dec 2026 03:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796094717; bh=Bqq4fHOs2KGOpiV3gjJCgIRg+cl6xiiMS2tALTUHT18=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YBEnJiBkIbo1Xcj55T7WGdSw6dR8Wr11vb4Dnq9xJehlVcZndiuJ5pPU6+AvSRdlk 1vf459neqyzHhFUg2n5HBUGYKoswdEZJvChUkIKOdhZFPPALb8K5GclWpka7ZJRdbc VMuiHb9bAvRkGZupUaihcP/MzHxIyLJs/EDVf7rouCbIrjToIpMgiwOiQeILJ7vjAr PLj5bt/adgTHgPVqLWxKZBb6ug67qiZBarfnJ0c4I9cTG58c1rWEJgWAwKktAU9Wof 6V8s8Eq07NSrQa2UTBYAwExHZ78ZVByEdaCMk96IDvWr7zQ+/J33ea48s9oUzbPgSw 97fSC+rDYTQqw== 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 v9 21/29] proc: Implement ->set_nsexec() fs_context operation Date: Tue, 1 Dec 2026 05:09:53 +0200 Message-ID: <20261201031002.593830-22-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org> References: <20261201031002.593830-1-jarkko@kernel.org> X-Mailing-List: containers@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit proc_init_fs_context() binds ctx->pid_ns to task_active_pid_ns(current) and resets fc->user_ns to ctx->pid_ns->user_ns, overriding the user namespace delegated by FSCONFIG_SET_NSEXEC and leaving the context attached to the caller's PID namespace. Wire up proc_set_nsexec() to verify that the target nsexec's PID namespace is a descendant of the caller's active PID namespace and rebind ctx->pid_ns and fc->user_ns accordingly, and mark proc with FS_NSEXEC_MOUNT. Co-developed-by: David Howells Signed-off-by: David Howells Signed-off-by: Jarkko Sakkinen --- fs/proc/root.c | 61 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/fs/proc/root.c b/fs/proc/root.c index 7fbbe92bf73a..8ec022fbede4 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include #include @@ -30,7 +32,7 @@ struct proc_fs_context { struct pid_namespace *pid_ns; unsigned int mask; enum proc_hidepid hidepid; - int gid; + kgid_t gid; enum proc_pidonly pidonly; }; @@ -42,7 +44,7 @@ enum proc_param { }; static const struct fs_parameter_spec proc_fs_parameters[] = { - fsparam_u32("gid", Opt_gid), + fsparam_gid("gid", Opt_gid), fsparam_string("hidepid", Opt_hidepid), fsparam_string("subset", Opt_subset), fsparam_file_or_string("pidns", Opt_pidns), @@ -178,7 +180,9 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) switch (opt) { case Opt_gid: - ctx->gid = result.uint_32; + if (!kgid_has_mapping(fc->user_ns, result.gid)) + return invalfc(fc, "Invalid gid"); + ctx->gid = result.gid; break; case Opt_hidepid: @@ -206,6 +210,10 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) errorfc(fc, "cannot reconfigure pidns for existing procfs"); return -EBUSY; } + if (fc->nsexec_bound) { + errorfc(fc, "cannot override pidns on nsexec-bound procfs"); + return -EBUSY; + } err = proc_parse_pidns_param(fc, param, &result); if (err) return err; @@ -224,8 +232,7 @@ static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) } static int proc_apply_options(struct proc_fs_info *fs_info, - struct fs_context *fc, - struct user_namespace *user_ns) + struct fs_context *fc) { struct proc_fs_context *ctx = fc->fs_private; @@ -234,8 +241,11 @@ static int proc_apply_options(struct proc_fs_info *fs_info, ctx->pidonly != fs_info->pidonly) return invalf(fc, "proc: subset=pid cannot be changed\n"); - if (ctx->mask & (1 << Opt_gid)) - fs_info->pid_gid = make_kgid(user_ns, ctx->gid); + if (ctx->mask & (1 << Opt_gid)) { + if (!kgid_has_mapping(fc->user_ns, ctx->gid)) + return invalfc(fc, "Invalid gid"); + fs_info->pid_gid = ctx->gid; + } if (ctx->mask & (1 << Opt_hidepid)) fs_info->hide_pid = ctx->hidepid; if (ctx->mask & (1 << Opt_subset)) @@ -261,7 +271,8 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc) fs_info->pid_ns = get_pid_ns(ctx->pid_ns); fs_info->mounter_cred = get_cred(fc->cred); - ret = proc_apply_options(fs_info, fc, current_user_ns()); + s->s_fs_info = fs_info; + ret = proc_apply_options(fs_info, fc); if (ret) return ret; @@ -273,8 +284,6 @@ static int proc_fill_super(struct super_block *s, struct fs_context *fc) s->s_magic = PROC_SUPER_MAGIC; s->s_op = &proc_sops; s->s_time_gran = 1; - s->s_fs_info = fs_info; - if (fs_info->pidonly == PROC_PIDONLY_ON) s->s_iflags |= SB_I_RESTRICTED_VARIANT; @@ -315,7 +324,7 @@ static int proc_reconfigure(struct fs_context *fc) sync_filesystem(sb); - return proc_apply_options(fs_info, fc, current_user_ns()); + return proc_apply_options(fs_info, fc); } static int proc_get_tree(struct fs_context *fc) @@ -323,6 +332,21 @@ static int proc_get_tree(struct fs_context *fc) return get_tree_nodev(fc, proc_fill_super); } +static int proc_set_nsexec(struct fs_context *fc, struct nsexec *nsexec) +{ + struct proc_fs_context *ctx = fc->fs_private; + struct pid_namespace *target = nsexec->ns->pid_ns_for_children; + + if (!pidns_is_ancestor(target, task_active_pid_ns(current))) + return invalfc(fc, "cannot set pidns to non-descendant pidns"); + + put_pid_ns(ctx->pid_ns); + ctx->pid_ns = get_pid_ns(target); + put_user_ns(fc->user_ns); + fc->user_ns = get_user_ns(ctx->pid_ns->user_ns); + return 0; +} + static void proc_fs_context_free(struct fs_context *fc) { struct proc_fs_context *ctx = fc->fs_private; @@ -334,6 +358,7 @@ static void proc_fs_context_free(struct fs_context *fc) static const struct fs_context_operations proc_fs_context_ops = { .free = proc_fs_context_free, .parse_param = proc_parse_param, + .set_nsexec = proc_set_nsexec, .get_tree = proc_get_tree, .reconfigure = proc_reconfigure, }; @@ -346,9 +371,13 @@ static int proc_init_fs_context(struct fs_context *fc) if (!ctx) return -ENOMEM; - ctx->pid_ns = get_pid_ns(task_active_pid_ns(current)); - put_user_ns(fc->user_ns); - fc->user_ns = get_user_ns(ctx->pid_ns->user_ns); + if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { + ctx->pid_ns = get_pid_ns(proc_sb_info(fc->root->d_sb)->pid_ns); + } else { + ctx->pid_ns = get_pid_ns(task_active_pid_ns(current)); + put_user_ns(fc->user_ns); + fc->user_ns = get_user_ns(ctx->pid_ns->user_ns); + } fc->fs_private = ctx; fc->ops = &proc_fs_context_ops; return 0; @@ -371,7 +400,9 @@ static struct file_system_type proc_fs_type = { .init_fs_context = proc_init_fs_context, .parameters = proc_fs_parameters, .kill_sb = proc_kill_sb, - .fs_flags = FS_USERNS_MOUNT | FS_USERNS_MOUNT_RESTRICTED | FS_DISALLOW_NOTIFY_PERM, + .fs_flags = FS_USERNS_MOUNT | FS_NSEXEC_MOUNT | + FS_USERNS_MOUNT_RESTRICTED | + FS_DISALLOW_NOTIFY_PERM, }; void __init proc_root_init(void) -- 2.47.3