From nobody Mon Oct 6 10:14:36 2025 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07A3427EFF1; Tue, 22 Jul 2025 23:19:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226372; cv=none; b=XpicO0CXp4c9g2z84iBBWF75R4ESDzyl53NSeLU1G298xd1Flu6OtS7bkVt4J433aTMjmjByhLNO2RJMsYn/gSlQ+NPJ4y5FP99QDIhITVs/XhjmOIhNIpCnbwOsrBk/trf89mra867NNclhoi5rGLYpnK6bNBAVoTOd+pYAbL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226372; c=relaxed/simple; bh=KixoyUtbGrLnA3MTF47jcmrfse0K1oiiVQANOqAk1hI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=roQeDO1xoxhPsiseWZ02PZ6V3PCtJUTE1JtbwXWoEKR/Mnd4NvggY7DpdTN3IBUG9Wd5rWQToCzXq6/E9HZ53Q/wDrgoH5QvmOxlpd6/WM5JCDMPTcQObH+VkIoTRhrw/vdOYX/X9q8/sa6gqThou0yQ8r/xICIDC8DyydBw8M4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=VIlUuJOj; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="VIlUuJOj" Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4bmtWg2w3Tz9tN7; Wed, 23 Jul 2025 01:19:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1753226367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cz5+/xzkeEXoj428r2lantinAr2RMkc/0PWdNpXULag=; b=VIlUuJOjz6NdY9eJiosw8RkEmNlLuFDgzd3kGMxTqeiupMQ/qvEDNWd1SHdBG2IhbKelIS zPTL6exjG43c6Ec+7ucddZr56JC565X0Xu2RFOJp2kUlZk1maRh2P+QazJaAeonb5nPDv4 K5SdMvvlcJpyA3RfyY0HjvfSbKtEwbZynLZHVXTRrnFZoaJPsxv7f6+NpPO0G+yaU92EL8 it0/yBZIS0wPtpnPMuYgmpEhz4k89FYMeRCnuTDVU3yEJBIluYhUjwdw25+mbgRMd2SeRK bAMYahWbdWjqq6cIyGqbjKGnJpyJufHXHrA5EAsyGUUJdcw6j6RcSmzFnargMQ== From: Aleksa Sarai Date: Wed, 23 Jul 2025 09:18:51 +1000 Subject: [PATCH RFC v2 1/4] pidns: move is-ancestor logic to helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250723-procfs-pidns-api-v2-1-621e7edd8e40@cyphar.com> References: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> In-Reply-To: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=2749; i=cyphar@cyphar.com; h=from:subject:message-id; bh=KixoyUtbGrLnA3MTF47jcmrfse0K1oiiVQANOqAk1hI=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWQ0yOSv/6e3xqdpts1xBolul/kv5ps0i7+Rk96+SeTxX 2b+U5a3OkpZGMS4GGTFFFm2+XmGbpq/+Eryp5VsMHNYmUCGMHBxCsBENlcyMmw7KcJmUh+49qDN Nb7XE1ZskWP6GPnab/O/k7Ut74JSWTQYGY75e6cpT0uUXvPz3akzMikiutm7n3+YPGvfnN2Nsuy vUzkA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 This check will be needed in later patches, and there's no point open-coding it each time. Signed-off-by: Aleksa Sarai --- include/linux/pid_namespace.h | 9 +++++++++ kernel/pid_namespace.c | 23 +++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h index 7c67a5811199..17fdc059f8da 100644 --- a/include/linux/pid_namespace.h +++ b/include/linux/pid_namespace.h @@ -84,6 +84,9 @@ extern void zap_pid_ns_processes(struct pid_namespace *pi= d_ns); extern int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd); extern void put_pid_ns(struct pid_namespace *ns); =20 +extern bool pidns_is_ancestor(struct pid_namespace *child, + struct pid_namespace *ancestor); + #else /* !CONFIG_PID_NS */ #include =20 @@ -118,6 +121,12 @@ static inline int reboot_pid_ns(struct pid_namespace *= pid_ns, int cmd) { return 0; } + +static inline bool pidns_is_ancestor(struct pid_namespace *child, + struct pid_namespace *ancestor) +{ + return false; +} #endif /* CONFIG_PID_NS */ =20 extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk); diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 7098ed44e717..c2783c5fa90b 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -390,11 +390,24 @@ static void pidns_put(struct ns_common *ns) put_pid_ns(to_pid_ns(ns)); } =20 +bool pidns_is_ancestor(struct pid_namespace *child, + struct pid_namespace *ancestor) +{ + struct pid_namespace *ns; + + if (child->level < ancestor->level) + return false; + for (ns =3D child; ns->level > ancestor->level; ns =3D ns->parent) + ; + return ns =3D=3D ancestor; +} +EXPORT_SYMBOL_GPL(pidns_is_ancestor); + static int pidns_install(struct nsset *nsset, struct ns_common *ns) { struct nsproxy *nsproxy =3D nsset->nsproxy; struct pid_namespace *active =3D task_active_pid_ns(current); - struct pid_namespace *ancestor, *new =3D to_pid_ns(ns); + struct pid_namespace *new =3D to_pid_ns(ns); =20 if (!ns_capable(new->user_ns, CAP_SYS_ADMIN) || !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN)) @@ -408,13 +421,7 @@ static int pidns_install(struct nsset *nsset, struct n= s_common *ns) * this maintains the property that processes and their * children can not escape their current pid namespace. */ - if (new->level < active->level) - return -EINVAL; - - ancestor =3D new; - while (ancestor->level > active->level) - ancestor =3D ancestor->parent; - if (ancestor !=3D active) + if (!pidns_is_ancestor(new, active)) return -EINVAL; =20 put_pid_ns(nsproxy->pid_ns_for_children); --=20 2.50.0 From nobody Mon Oct 6 10:14:36 2025 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E10AE28C849; Tue, 22 Jul 2025 23:19:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226377; cv=none; b=X0up+SVmTHqp0zf+0ce809otmclQgL9BI4yvUYQnlVcNjOAsrAoSoXg1Exu9FEID2uipozzDD1iGAU+Lb6E3bddKUQ8rCdrGljhRzY2lHmHp91rGIlqJpV8jdYoF8K/jqIeZgtADcFf3l9jOqJlBC+5j0IPhMbyucsr9mABpwVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226377; c=relaxed/simple; bh=h32AkQqTQFMtDpPn/hB2iPuVrm4SLK7xkXiKfkcX7CE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RpWFwZOu/z6hxKPS+A0+dCZTNW38fvNUAiSu2+AwyKCZYgNWi1zMzSARwnltSY6CLOb+NwlvzKl1tCPau5CtXvjiRWxeWxeoJYaCVzIs5x4o9ggXE/rVjSIGrh1xc8CBCyR7TxplZNi//XXA3bL5k3Ofsk9edSTS2ts/cMtG2Zs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=krl84KTJ; arc=none smtp.client-ip=80.241.56.161 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="krl84KTJ" Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4bmtWm1zHFz9sbL; Wed, 23 Jul 2025 01:19:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1753226372; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8dffB/8g2jqxD1gkKKuu1Q7Sx0KA5pcUHUZQ8HGfuW8=; b=krl84KTJ0uBg/wZAWfhIJ1pCXOuDXcYGNcNCboWGRUvLDqmJuvIeOnOC1EugoIqV291VtH JuSh7IQoSEnG9Dp9jz6vciWPvCPgdG2J1jcyPx9K5AlqyPQ/sAyo2LM6/vZbfu6oXSpfyC 6Ftt7i2LX9jUmQatxUezLVP+JO/YLTb5YvKEN/6aeIdBT6uLxPoCQeT1nJYDs7tPKgQLgG WdHUAiQ1pHyfTdNs74dRHZAugVGzAVj18mFvqfaRtiPv7eUYJwNJksUYs+QsVvl3sXtWpu yQEvKwkj1h7cnWz7Lo21lBM8m2DS/NMW9UGAi9xJO7eZB9N0NQGSkX1g7BoeGA== From: Aleksa Sarai Date: Wed, 23 Jul 2025 09:18:52 +1000 Subject: [PATCH RFC v2 2/4] procfs: add "pidns" mount option Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250723-procfs-pidns-api-v2-2-621e7edd8e40@cyphar.com> References: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> In-Reply-To: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=6674; i=cyphar@cyphar.com; h=from:subject:message-id; bh=h32AkQqTQFMtDpPn/hB2iPuVrm4SLK7xkXiKfkcX7CE=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWQ0yOTnPH/7d6e13KXn0/+FTdX22uvVFi4m6dYluTeyQ /bM4i95HaUsDGJcDLJiiizb/DxDN81ffCX500o2mDmsTCBDGLg4BWAiC0MYGXa8stQ1PcW0ZIG9 z1Kb3ywKslXb8+IyzWc9frNrmrT9DmeG/+EP5neHJB3tX32p4pp2ke0mzzYNgUrvRi7lGtuGKfM nMgMA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 Since the introduction of pid namespaces, their interaction with procfs has been entirely implicit in ways that require a lot of dancing around by programs that need to construct sandboxes with different PID namespaces. Being able to explicitly specify the pid namespace to use when constructing a procfs super block will allow programs to no longer need to fork off a process which does then does unshare(2) / setns(2) and forks again in order to construct a procfs in a pidns. So, provide a "pidns" mount option which allows such users to just explicitly state which pid namespace they want that procfs instance to use. This interface can be used with fsconfig(2) either with a file descriptor or a path: fsconfig(procfd, FSCONFIG_SET_FD, "pidns", NULL, nsfd); fsconfig(procfd, FSCONFIG_SET_STRING, "pidns", "/proc/self/ns/pid", 0); or with classic mount(2) / mount(8): // mount -t proc -o pidns=3D/proc/self/ns/pid proc /tmp/proc mount("proc", "/tmp/proc", "proc", MS_..., "pidns=3D/proc/self/ns/pid"); As this new API is effectively shorthand for setns(2) followed by mount(2), the permission model for this mirrors pidns_install() to avoid opening up new attack surfaces by loosening the existing permission model. Note that the mount infrastructure also allows userspace to reconfigure the pidns of an existing procfs mount, which may or may not be useful to some users. Signed-off-by: Aleksa Sarai --- Documentation/filesystems/proc.rst | 6 +++ fs/proc/root.c | 90 ++++++++++++++++++++++++++++++++++= +--- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index 5236cb52e357..c520b9f8a3fd 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -2360,6 +2360,7 @@ The following mount options are supported: hidepid=3D Set /proc// access mode. gid=3D Set the group authorized to learn processes information. subset=3D Show only the specified subset of procfs. + pidns=3D Specify a the namespace used by this procfs. =3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 hidepid=3Doff or hidepid=3D0 means classic mode - everybody may access all @@ -2392,6 +2393,11 @@ information about processes information, just add id= entd to this group. subset=3Dpid hides all top level files and directories in the procfs that are not related to tasks. =20 +pidns=3D specifies a pid namespace (either as a string path to something l= ike +`/proc/$pid/ns/pid`, or a file descriptor when using `FSCONFIG_SET_FD`) th= at +will be used by the procfs instance when translating pids. By default, pro= cfs +will use the calling process's active pid namespace. + Chapter 5: Filesystem behavior =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D =20 diff --git a/fs/proc/root.c b/fs/proc/root.c index ed86ac710384..057c8a125c6e 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -38,12 +38,18 @@ enum proc_param { Opt_gid, Opt_hidepid, Opt_subset, +#ifdef CONFIG_PID_NS + Opt_pidns, +#endif }; =20 static const struct fs_parameter_spec proc_fs_parameters[] =3D { - fsparam_u32("gid", Opt_gid), + fsparam_u32("gid", Opt_gid), fsparam_string("hidepid", Opt_hidepid), fsparam_string("subset", Opt_subset), +#ifdef CONFIG_PID_NS + fsparam_file_or_string("pidns", Opt_pidns), +#endif {} }; =20 @@ -109,11 +115,67 @@ static int proc_parse_subset_param(struct fs_context = *fc, char *value) return 0; } =20 +#ifdef CONFIG_PID_NS +static int proc_parse_pidns_param(struct fs_context *fc, + struct fs_parameter *param, + struct fs_parse_result *result) +{ + struct proc_fs_context *ctx =3D fc->fs_private; + struct pid_namespace *target, *active =3D task_active_pid_ns(current); + struct ns_common *ns; + struct file *ns_filp __free(fput) =3D NULL; + + switch (param->type) { + case fs_value_is_file: + /* came throug fsconfig, steal the file reference */ + ns_filp =3D param->file; + param->file =3D NULL; + break; + case fs_value_is_string: + ns_filp =3D filp_open(param->string, O_RDONLY, 0); + break; + default: + WARN_ON_ONCE(true); + break; + } + if (!ns_filp) + ns_filp =3D ERR_PTR(-EBADF); + if (IS_ERR(ns_filp)) { + errorfc(fc, "could not get file from pidns argument"); + return PTR_ERR(ns_filp); + } + + if (!proc_ns_file(ns_filp)) + return invalfc(fc, "pidns argument is not an nsfs file"); + ns =3D get_proc_ns(file_inode(ns_filp)); + if (ns->ops->type !=3D CLONE_NEWPID) + return invalfc(fc, "pidns argument is not a pidns file"); + target =3D container_of(ns, struct pid_namespace, ns); + + /* + * pidns=3D is shorthand for joining the pidns to get a fsopen fd, so the + * permission model should be the same as pidns_install(). + */ + if (!ns_capable(target->user_ns, CAP_SYS_ADMIN)) { + errorfc(fc, "insufficient permissions to set pidns"); + return -EPERM; + } + if (!pidns_is_ancestor(target, active)) + return invalfc(fc, "cannot set pidns to non-descendant pidns"); + + put_pid_ns(ctx->pid_ns); + ctx->pid_ns =3D get_pid_ns(target); + put_user_ns(fc->user_ns); + fc->user_ns =3D get_user_ns(ctx->pid_ns->user_ns); + return 0; +} +#endif /* CONFIG_PID_NS */ + static int proc_parse_param(struct fs_context *fc, struct fs_parameter *pa= ram) { struct proc_fs_context *ctx =3D fc->fs_private; struct fs_parse_result result; - int opt; + int opt, err; =20 opt =3D fs_parse(fc, proc_fs_parameters, param, &result); if (opt < 0) @@ -125,14 +187,24 @@ static int proc_parse_param(struct fs_context *fc, st= ruct fs_parameter *param) break; =20 case Opt_hidepid: - if (proc_parse_hidepid_param(fc, param)) - return -EINVAL; + err =3D proc_parse_hidepid_param(fc, param); + if (err) + return err; break; =20 case Opt_subset: - if (proc_parse_subset_param(fc, param->string) < 0) - return -EINVAL; + err =3D proc_parse_subset_param(fc, param->string); + if (err) + return err; + break; + +#ifdef CONFIG_PID_NS + case Opt_pidns: + err =3D proc_parse_pidns_param(fc, param, &result); + if (err) + return err; break; +#endif =20 default: return -EINVAL; @@ -154,6 +226,12 @@ static void proc_apply_options(struct proc_fs_info *fs= _info, fs_info->hide_pid =3D ctx->hidepid; if (ctx->mask & (1 << Opt_subset)) fs_info->pidonly =3D ctx->pidonly; +#ifdef CONFIG_PID_NS + if (ctx->mask & (1 << Opt_pidns)) { + put_pid_ns(fs_info->pid_ns); + fs_info->pid_ns =3D get_pid_ns(ctx->pid_ns); + } +#endif } =20 static int proc_fill_super(struct super_block *s, struct fs_context *fc) --=20 2.50.0 From nobody Mon Oct 6 10:14:36 2025 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A08228A1C9; Tue, 22 Jul 2025 23:19:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226382; cv=none; b=Pfj2Ur1kPvyIVnqbA3oocFNIu0aCRGHP8IV7m9ZcuAVn15D5nDeKt1plQdDlLO4ML3xQl4kso4UAO6a4iCdhJK4PvhXkN78zIXm3wSpC8vFRtRpYr262M5e2TDaKjI1xNNQ+8e9dCJnZPKW43M4Rtce7/AX5amhUtZCit8VFg0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226382; c=relaxed/simple; bh=UmPMDauKqZHT2KGHGZlGLyTzSUO3Tof/oDKXZ1ByKLY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GqMJexLc4Feg9fHECdYihtogJclfIuWvnv625xoS5plDFBNf6safyKxPaDNz11JThDJeBddlRE5/MuzBNmbUXYolVoWEXrO3wYJUd4PUOvqa+lbmFmgZwyqbClEeVDuj0pN4ZQQN5AqJdkXJw+kkMlsQwMiY5/TooAW0vq3I/KY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=X1Gjg01a; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="X1Gjg01a" Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4bmtWr6CR2z9v23; Wed, 23 Jul 2025 01:19:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1753226376; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B/Z2GV+fudpYe8e8bFuab6/Kln8z/eUc8/jutrOYdpI=; b=X1Gjg01ad4Y91Kw6MHnHRO3k6m2v/9cn3sxuLFEfXDY/XmVab9n3TzkW3htPnPdXUpp8Vn 6Khx+rLiL6W0AxvKrFi+HUJHFxSj5vliglUnMOfJGHcDMMjjkWbp1rZhUGe4kaUvwPDrJF 1fg3F5fhyO12UFqqxeCfk27v13+Z/lkzY3V6PvvGAXh2A5l06qjUtIn0JBWpq6TyKLw8tn gG9Sxe48XW8Mza+ItuitHbdUHzcwSeBvZAIe9LvOtVbZmxGhqZlHgiYZK1DJrF5f0ELYqr DjFin5xi/8CDWxhLSxaH0IYU6cU3fLrrX4c4MybvFtbvbikztgT7iqyX3t2nBg== From: Aleksa Sarai Date: Wed, 23 Jul 2025 09:18:53 +1000 Subject: [PATCH RFC v2 3/4] procfs: add PROCFS_GET_PID_NAMESPACE ioctl Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250723-procfs-pidns-api-v2-3-621e7edd8e40@cyphar.com> References: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> In-Reply-To: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=5677; i=cyphar@cyphar.com; h=from:subject:message-id; bh=UmPMDauKqZHT2KGHGZlGLyTzSUO3Tof/oDKXZ1ByKLY=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWQ0yOTn7lj3qNve/vRDw39yYWmbjiXv4vDbzbTj9ErLg 3ePO3Yf6ShlYRDjYpAVU2TZ5ucZumn+4ivJn1aywcxhZQIZwsDFKQATmbmD4Q/v30zWTVm7mjY6 7tmUwzBpa1eXrdUDDnN9R0/t6q+Vj08x/FONDm+KUo5yLgio37L25a6s39umeN5vrGmsO9Sg8+l nKBMA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 /proc has historically had very opaque semantics about PID namespaces, which is a little unfortunate for container runtimes and other programs that deal with switching namespaces very often. One common issue is that of converting between PIDs in the process's namespace and PIDs in the namespace of /proc. In principle, it is possible to do this today by opening a pidfd with pidfd_open(2) and then looking at /proc/self/fdinfo/$n (which will contain a PID value translated to the pid namespace associated with that procfs superblock). However, allocating a new file for each PID to be converted is less than ideal for programs that may need to scan procfs, and it is generally useful for userspace to be able to finally get this information from procfs. So, add a new API for this in the form of an ioctl(2) you can call on the root directory of procfs. The returned file descriptor will have O_CLOEXEC set. This acts as a sister feature to the new "pidns" mount option, finally allowing userspace full control of the pid namespaces associated with procfs instances. The permission model for this is a bit looser than that of the "pidns" mount option, but this is mainly because /proc/1/ns/pid provides the same information, so as long as you have access to that magic-link (or something equivalently reasonable such as privileges with CAP_SYS_ADMIN or being in an ancestor pid namespace) it makes sense to allow userspace to grab a handle. setns(2) will still have their own permission checks, so being able to open a pidns handle doesn't really provide too many other capabilities. Signed-off-by: Aleksa Sarai --- Documentation/filesystems/proc.rst | 4 +++ fs/proc/root.c | 54 ++++++++++++++++++++++++++++++++++= ++-- include/uapi/linux/fs.h | 3 +++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index c520b9f8a3fd..506383273c9d 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -2398,6 +2398,10 @@ pidns=3D specifies a pid namespace (either as a stri= ng path to something like will be used by the procfs instance when translating pids. By default, pro= cfs will use the calling process's active pid namespace. =20 +Processes can check which pid namespace is used by a procfs instance by us= ing +the `PROCFS_GET_PID_NAMESPACE` ioctl() on the root directory of the procfs +instance. + Chapter 5: Filesystem behavior =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D =20 diff --git a/fs/proc/root.c b/fs/proc/root.c index 057c8a125c6e..548a57ec2152 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -23,8 +23,10 @@ #include #include #include +#include =20 #include "internal.h" +#include "../internal.h" =20 struct proc_fs_context { struct pid_namespace *pid_ns; @@ -418,15 +420,63 @@ static int proc_root_readdir(struct file *file, struc= t dir_context *ctx) return proc_pid_readdir(file, ctx); } =20 +static long int proc_root_ioctl(struct file *filp, unsigned int cmd, unsig= ned long arg) +{ + switch (cmd) { +#ifdef CONFIG_PID_NS + case PROCFS_GET_PID_NAMESPACE: { + struct pid_namespace *active =3D task_active_pid_ns(current); + struct pid_namespace *ns =3D proc_pid_ns(file_inode(filp)->i_sb); + bool can_access_pidns =3D false; + + /* + * If we are in an ancestors of the pidns, or have join + * privileges (CAP_SYS_ADMIN), then it makes sense that we + * would be able to grab a handle to the pidns. + * + * Otherwise, if there is a root process, then being able to + * access /proc/$pid/ns/pid is equivalent to this ioctl and so + * we should probably match the permission model. For empty + * namespaces it seems unlikely for there to be a downside to + * allowing unprivileged users to open a handle to it (setns + * will fail for unprivileged users anyway). + */ + can_access_pidns =3D pidns_is_ancestor(ns, active) || + ns_capable(ns->user_ns, CAP_SYS_ADMIN); + if (!can_access_pidns) { + bool cannot_ptrace_pid1 =3D false; + + read_lock(&tasklist_lock); + if (ns->child_reaper) + cannot_ptrace_pid1 =3D ptrace_may_access(ns->child_reaper, + PTRACE_MODE_READ_FSCREDS); + read_unlock(&tasklist_lock); + can_access_pidns =3D !cannot_ptrace_pid1; + } + if (!can_access_pidns) + return -EPERM; + + /* open_namespace() unconditionally consumes the reference. */ + get_pid_ns(ns); + return open_namespace(to_ns_common(ns)); + } +#endif /* CONFIG_PID_NS */ + default: + return -ENOIOCTLCMD; + } +} + /* * The root /proc directory is special, as it has the * directories. Thus we don't use the generic * directory handling functions for that.. */ static const struct file_operations proc_root_operations =3D { - .read =3D generic_read_dir, - .iterate_shared =3D proc_root_readdir, + .read =3D generic_read_dir, + .iterate_shared =3D proc_root_readdir, .llseek =3D generic_file_llseek, + .unlocked_ioctl =3D proc_root_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, }; =20 /* diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 0bd678a4a10e..aa642cb48feb 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -437,6 +437,9 @@ typedef int __bitwise __kernel_rwf_t; =20 #define PROCFS_IOCTL_MAGIC 'f' =20 +/* procfs root ioctls */ +#define PROCFS_GET_PID_NAMESPACE _IO(PROCFS_IOCTL_MAGIC, 1) + /* Pagemap ioctl */ #define PAGEMAP_SCAN _IOWR(PROCFS_IOCTL_MAGIC, 16, struct pm_scan_arg) =20 --=20 2.50.0 From nobody Mon Oct 6 10:14:36 2025 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3127C28B7D3; Tue, 22 Jul 2025 23:19:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226390; cv=none; b=ZkvCta/drOzxilm5KdsA5SCTl+yqC7WHPWnM6nGwiNwmtwyScu9sV+IKJLPisJftOqcmGX/Jm2cLOGFR3hBrVTb21VWOEacnrICGXxeIzi1s8yX0HJ8ClU30ZNuXHcQyUzw863FDYsR42iSMp50GYaiy0Coxne6t1Shqb0i45QQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753226390; c=relaxed/simple; bh=iERN6Xzk9sHzsB4pLCPzg+cd0rkOPiJ6NJagUizg4D0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HbY6IudQxf9WN+Upb8aPap46HWU7UgkOXFiPLLE6F/owTRbSH5BQzyotGJa2OEILyhrlAkbx04f/AUEvDvZuVhi5fdOu2z428ZIrI74X7PXKjMQxviPBxHiIoeUXrnmDA42s+bn7B7zRc7+2qyJbNw1iv6R/ZrWEH7u13RfXX6Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com; spf=pass smtp.mailfrom=cyphar.com; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b=pSsmAH0b; arc=none smtp.client-ip=80.241.56.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cyphar.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cyphar.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cyphar.com header.i=@cyphar.com header.b="pSsmAH0b" Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4bmtWx40bRz9tKY; Wed, 23 Jul 2025 01:19:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cyphar.com; s=MBO0001; t=1753226381; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ga146VfUnbYMyYkmA+aailWJBUlJlGizk/gmO5Hyyfg=; b=pSsmAH0b+jJYacyGxS8hatPE1X+DUMc0aN6RwPvJHabw0eaoUjNSBE0DgLeLaZLdlYhpZ9 rL+lBDcBr3JmrukxwtCLTaQSavAVzi76K6IgN9LC1o3XiWRP9QW6w4wR92jI3o1LGin1k2 V7RZz+VE7FZAJd+Rt0Zdc9L0mqVNE94Pg8tiZZyRm+wgG4fEfTP+AlXVq96BNIzkKDSZz7 Xj8k/d+8aWFejT+KBgXOsvjmcv5rS87UbIqaLu0z+iSFoIv0K7pqxo2AlzSrdnbJkRcF70 BIr1sSzA/8XSa6vyCO+W+Y6xynOifI4nIUZgF2iYJGeyAGZiF0waq+PNIhCP8A== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of cyphar@cyphar.com designates 2001:67c:2050:b231:465::202 as permitted sender) smtp.mailfrom=cyphar@cyphar.com From: Aleksa Sarai Date: Wed, 23 Jul 2025 09:18:54 +1000 Subject: [PATCH RFC v2 4/4] selftests/proc: add tests for new pidns APIs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250723-procfs-pidns-api-v2-4-621e7edd8e40@cyphar.com> References: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> In-Reply-To: <20250723-procfs-pidns-api-v2-0-621e7edd8e40@cyphar.com> To: Alexander Viro , Christian Brauner , Jan Kara , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai X-Developer-Signature: v=1; a=openpgp-sha256; l=9962; i=cyphar@cyphar.com; h=from:subject:message-id; bh=iERN6Xzk9sHzsB4pLCPzg+cd0rkOPiJ6NJagUizg4D0=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWQ0yBTsauX+3WS7hd/t/3ljl529dbIJNY7NRn85D0oxM GssDfndUcrCIMbFICumyLLNzzN00/zFV5I/rWSDmcPKBDKEgYtTACbys43hf+IP/Qqbb129cgJM Z47zhQec6EjY/kQxqCbkcOwpTkebLwz/o39nrnshbDFr4wH+o7sdlmuw/o7PfHR5ocFst6d5h0+ a8QAA X-Developer-Key: i=cyphar@cyphar.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 X-Rspamd-Queue-Id: 4bmtWx40bRz9tKY Signed-off-by: Aleksa Sarai --- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + tools/testing/selftests/proc/proc-pidns.c | 286 ++++++++++++++++++++++++++= ++++ 3 files changed, 288 insertions(+) diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selfte= sts/proc/.gitignore index 973968f45bba..2dced03e9e0e 100644 --- a/tools/testing/selftests/proc/.gitignore +++ b/tools/testing/selftests/proc/.gitignore @@ -17,6 +17,7 @@ /proc-tid0 /proc-uptime-001 /proc-uptime-002 +/proc-pidns /read /self /setns-dcache diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftest= s/proc/Makefile index b12921b9794b..c6f7046b9860 100644 --- a/tools/testing/selftests/proc/Makefile +++ b/tools/testing/selftests/proc/Makefile @@ -27,5 +27,6 @@ TEST_GEN_PROGS +=3D setns-sysvipc TEST_GEN_PROGS +=3D thread-self TEST_GEN_PROGS +=3D proc-multiple-procfs TEST_GEN_PROGS +=3D proc-fsconfig-hidepid +TEST_GEN_PROGS +=3D proc-pidns =20 include ../lib.mk diff --git a/tools/testing/selftests/proc/proc-pidns.c b/tools/testing/self= tests/proc/proc-pidns.c new file mode 100644 index 000000000000..e7e34c78d383 --- /dev/null +++ b/tools/testing/selftests/proc/proc-pidns.c @@ -0,0 +1,286 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Author: Aleksa Sarai + * Copyright (C) 2025 SUSE LLC. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#define bail(fmt, ...) \ + do { \ + fprintf(stderr, fmt ": %m", __VA_ARGS__); \ + exit(1); \ + } while (0) + +#define ASSERT_SUCCESS ASSERT_FALSE +#define ASSERT_FAIL ASSERT_TRUE + +int touch(char *path) +{ + int fd =3D open(path, O_WRONLY|O_CREAT|O_CLOEXEC, 0644); + if (fd < 0 || close(fd) < 0) + return -errno; + return 0; +} + +FIXTURE(ns) +{ + int host_mntns, host_pidns; + int dummy_pidns; +}; + +FIXTURE_SETUP(ns) +{ + /* Stash the old mntns. */ + self->host_mntns =3D open("/proc/self/ns/mnt", O_RDONLY|O_CLOEXEC); + ASSERT_GE(self->host_mntns, 0); + + /* Create a new mount namespace and make it private. */ + ASSERT_SUCCESS(unshare(CLONE_NEWNS)); + ASSERT_SUCCESS(mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL)); + + /* + * Create a proper tmpfs that we can use and will disappear once we + * leave this mntns. + */ + ASSERT_SUCCESS(mount("tmpfs", "/tmp", "tmpfs", 0, NULL)); + + /* + * Create a pidns we can use for later tests. We need to fork off a + * child so that we get a usable nsfd that we can bind-mount and open. + */ + ASSERT_SUCCESS(touch("/tmp/dummy-pidns")); + + self->host_pidns =3D open("/proc/self/ns/pid", O_RDONLY|O_CLOEXEC); + ASSERT_GE(self->host_pidns, 0); + ASSERT_SUCCESS(unshare(CLONE_NEWPID)); + + pid_t pid =3D fork(); + ASSERT_GE(pid, 0); + if (!pid) { + prctl(PR_SET_PDEATHSIG, SIGKILL); + ASSERT_SUCCESS(mount("/proc/self/ns/pid", "/tmp/dummy-pidns", NULL, MS_B= IND, 0)); + exit(0); + } + + int wstatus; + ASSERT_EQ(waitpid(pid, &wstatus, 0), pid); + ASSERT_TRUE(WIFEXITED(wstatus)); + ASSERT_EQ(WEXITSTATUS(wstatus), 0); + + ASSERT_SUCCESS(setns(self->host_pidns, CLONE_NEWPID)); + + self->dummy_pidns =3D open("/tmp/dummy-pidns", O_RDONLY|O_CLOEXEC); + ASSERT_GE(self->dummy_pidns, 0); +} + +FIXTURE_TEARDOWN(ns) +{ + ASSERT_SUCCESS(setns(self->host_mntns, CLONE_NEWNS)); + ASSERT_SUCCESS(close(self->host_mntns)); + + ASSERT_SUCCESS(close(self->host_pidns)); + ASSERT_SUCCESS(close(self->dummy_pidns)); +} + +TEST_F(ns, pidns_mount_string_path) +{ + ASSERT_SUCCESS(mkdir("/tmp/proc-host", 0755)); + ASSERT_SUCCESS(mount("proc", "/tmp/proc-host", "proc", 0, "pidns=3D/proc/= self/ns/pid")); + ASSERT_SUCCESS(access("/tmp/proc-host/self/", X_OK)); + + ASSERT_SUCCESS(mkdir("/tmp/proc-dummy", 0755)); + ASSERT_SUCCESS(mount("proc", "/tmp/proc-dummy", "proc", 0, "pidns=3D/tmp/= dummy-pidns")); + ASSERT_FAIL(access("/tmp/proc-dummy/1/", X_OK)); + ASSERT_FAIL(access("/tmp/proc-dummy/self/", X_OK)); +} + +TEST_F(ns, pidns_fsconfig_string_path) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_STRING, "pidns", "/tmp/dummy-p= idns", 0)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + ASSERT_FAIL(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_FAIL(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(mountfd)); +} + +TEST_F(ns, pidns_fsconfig_fd) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_FD, "pidns", NULL, self->dummy= _pidns)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + ASSERT_FAIL(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_FAIL(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(mountfd)); +} + +TEST_F(ns, pidns_reconfigure_remount) +{ + ASSERT_SUCCESS(mkdir("/tmp/proc", 0755)); + ASSERT_SUCCESS(mount("proc", "/tmp/proc", "proc", 0, "")); + ASSERT_SUCCESS(access("/tmp/proc/self/", X_OK)); + + ASSERT_SUCCESS(mount(NULL, "/tmp/proc", NULL, MS_REMOUNT, "pidns=3D/tmp/d= ummy-pidns")); + ASSERT_FAIL(access("/tmp/proc/self/", X_OK)); +} + +TEST_F(ns, pidns_reconfigure_fsconfig_string_path) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + ASSERT_SUCCESS(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_SUCCESS(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_STRING, "pidns", "/tmp/dummy-p= idns", 0)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0)); + + ASSERT_FAIL(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_FAIL(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(mountfd)); +} + +TEST_F(ns, pidns_reconfigure_fsconfig_fd) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + ASSERT_SUCCESS(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_SUCCESS(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_FD, "pidns", NULL, self->dummy= _pidns)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0)); + + ASSERT_FAIL(faccessat(mountfd, "1/", X_OK, 0)); + ASSERT_FAIL(faccessat(mountfd, "self/", X_OK, 0)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(mountfd)); +} + +int is_same_inode(int fd1, int fd2) +{ + struct stat stat1, stat2; + + assert(fstat(fd1, &stat1) =3D=3D 0); + assert(fstat(fd2, &stat2) =3D=3D 0); + + return stat1.st_ino =3D=3D stat2.st_ino && stat1.st_dev =3D=3D stat2.st_d= ev; +} + +#define PROCFS_IOCTL_MAGIC 'f' +#define PROCFS_GET_PID_NAMESPACE _IO(PROCFS_IOCTL_MAGIC, 1) + +TEST_F(ns, get_pidns_ioctl) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_FD, "pidns", NULL, self->dummy= _pidns)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + /* fsmount returns an O_PATH, which ioctl(2) doesn't accept. */ + int new_mountfd =3D openat(mountfd, ".", O_RDONLY|O_DIRECTORY|O_CLOEXEC); + ASSERT_GE(new_mountfd, 0); + + ASSERT_SUCCESS(close(mountfd)); + mountfd =3D -EBADF; + + int procfs_pidns =3D ioctl(new_mountfd, PROCFS_GET_PID_NAMESPACE); + ASSERT_GE(procfs_pidns, 0); + + ASSERT_NE(self->dummy_pidns, procfs_pidns); + ASSERT_FALSE(is_same_inode(self->host_pidns, procfs_pidns)); + ASSERT_TRUE(is_same_inode(self->dummy_pidns, procfs_pidns)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(new_mountfd)); + ASSERT_SUCCESS(close(procfs_pidns)); +} + +TEST_F(ns, reconfigure_get_pidns_ioctl) +{ + int fsfd =3D fsopen("proc", FSOPEN_CLOEXEC); + ASSERT_GE(fsfd, 0); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)); + + int mountfd =3D fsmount(fsfd, FSMOUNT_CLOEXEC, 0); + ASSERT_GE(mountfd, 0); + + /* fsmount returns an O_PATH, which ioctl(2) doesn't accept. */ + int new_mountfd =3D openat(mountfd, ".", O_RDONLY|O_DIRECTORY|O_CLOEXEC); + ASSERT_GE(new_mountfd, 0); + + ASSERT_SUCCESS(close(mountfd)); + mountfd =3D -EBADF; + + int procfs_pidns1 =3D ioctl(new_mountfd, PROCFS_GET_PID_NAMESPACE); + ASSERT_GE(procfs_pidns1, 0); + + ASSERT_NE(self->dummy_pidns, procfs_pidns1); + ASSERT_TRUE(is_same_inode(self->host_pidns, procfs_pidns1)); + ASSERT_FALSE(is_same_inode(self->dummy_pidns, procfs_pidns1)); + + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_SET_STRING, "pidns", "/tmp/dummy-p= idns", 0)); + ASSERT_SUCCESS(fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0)); + + int procfs_pidns2 =3D ioctl(new_mountfd, PROCFS_GET_PID_NAMESPACE); + ASSERT_GE(procfs_pidns2, 0); + + ASSERT_NE(self->dummy_pidns, procfs_pidns2); + ASSERT_FALSE(is_same_inode(self->host_pidns, procfs_pidns2)); + ASSERT_TRUE(is_same_inode(self->dummy_pidns, procfs_pidns2)); + + ASSERT_SUCCESS(close(fsfd)); + ASSERT_SUCCESS(close(new_mountfd)); + ASSERT_SUCCESS(close(procfs_pidns1)); + ASSERT_SUCCESS(close(procfs_pidns2)); +} + +TEST_HARNESS_MAIN --=20 2.50.0