From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8411E606A6A8 for ; Tue, 1 Dec 2026 03:12:34 +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 kanga.kvack.org (Postfix) id 4F4C06B05D7; Mon, 30 Nov 2026 22:12:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A5166B05D9; Mon, 30 Nov 2026 22:12:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 346726B05DA; Mon, 30 Nov 2026 22:12:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 078D06B05D7 for ; Mon, 30 Nov 2026 22:12:00 -0500 (EST) Received: from smtpin27.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 70B4C1C47BA for ; Tue, 1 Dec 2026 03:12:00 +0000 (UTC) X-FDA: 85493940000.27.BCC400E Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf22.hostedemail.com (Postfix) with ESMTP id C77F9C000C for ; Tue, 1 Dec 2026 03:11:58 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=YBEnJiBk; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796094719; b=MJrJjejoRIBVgR56Z8/Am0nJYTiGDm0rectNDXEtHRDEFhwmDB9LdKCL/Z9o6XKgb/ePhn eYVLQKZ6ivYln7PpiKLXJAA2rq48MhXB+TPlNHgTJAKfvsREQUcN14Md1F/GlvUjstVt5Y Ez+BtMnhFueD0g8UGT0rnzBbkiPSl6A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796094719; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Bqq4fHOs2KGOpiV3gjJCgIRg+cl6xiiMS2tALTUHT18=; b=Ln/xvHIAAqpoUt/AucwnyvCqf0DHuZ92WgUmE3RNDaq0WWApxetIac5gwHtyBs4nmQWEAZ plgWs/ikv57SfUkzaYS+0vWXarXg80o0livNM1tkZ7c9L43iGke0nqKKYy0n8llulvkQLK 22JPubqcc87OylUmJTpJf8OGhvEKtFQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=YBEnJiBk; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id E175F40FDF; Tue, 1 Dec 2026 03:11:57 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: C77F9C000C X-Stat-Signature: 7gkrqq1ek4wiwtiwzbs9nu9ro3oeem1z X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1796094718-758311 X-HE-Meta: U2FsdGVkX19DefAh8/F7ItE4MDeRtuAyDl1djHoZVrG7Moqz2qmFF5e2/YoWCo6FzkV/eLvg48BO73aLUV1gssrx9Cz1eWExz3t4iuPRkN5d6Qy03lCld+m/AYH9Y8177xEKr+kSP+YzIDDlL4nIYDqMAm2VnpciyT4EfBgxAEVYbdF1wuy7gJ6cY0nonnOsmls2OjuixEDBzDas+WfUeMiezcKjByFdkprSS89RigdzZs8RzNUjmlkc3tEVcKM72ELiKzDZ3KVoFQgQHclDOcMWyEHlbmTevpeYjQWoS5pwz/F6+EWkfMWjyWYnSetWRTn4QXjZxgEYprxT4minm8ZhcQ3Wsa5PexNfsCSTKbFEc7fhDkfY/dIXO6mHRzHrwoEi+G4lmrWLyuvfS2vFnP2JY2ucVMd0LLZ1/XbcoLvtteEKrUQ+gtqbuehONT8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jHD37n9xn4MlN1gpUQmRIarXLObjA7l37Dqo+v+qI7fohv1nFveg0HTfL6WM3NpXNDts42yj6A== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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