linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] uapi: export PROCFS_ROOT_INO
@ 2025-07-08 13:21 Aleksa Sarai
  2025-07-10  7:39 ` Christian Brauner
  0 siblings, 1 reply; 2+ messages in thread
From: Aleksa Sarai @ 2025-07-08 13:21 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara
  Cc: linux-kernel, linux-fsdevel, Aleksa Sarai

The root inode of /proc having a fixed inode number has been part of the
core kernel ABI since its inception, and recently some userspace
programs (mainly container runtimes) have started to explicitly depend
on this behaviour.

The main reason this is useful to userspace is that by checking that a
suspect /proc handle has fstype PROC_SUPER_MAGIC and is PROCFS_ROOT_INO,
they can then use openat2(RESOLVE_{NO_{XDEV,MAGICLINK},BENEATH}) to
ensure that there isn't a bind-mount that replaces some procfs file with
a different one. This kind of attack has lead to security issues in
container runtimes in the past (such as CVE-2019-19921) and libraries
like libpathrs[1] use this feature of procfs to provide safe procfs
handling functions.

There was also some trailing whitespace in the "struct proc_dir_entry"
initialiser, so fix that up as well.

[1]: https://github.com/openSUSE/libpathrs

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
---
 fs/proc/root.c          | 10 +++++-----
 include/linux/proc_ns.h |  1 -
 include/uapi/linux/fs.h | 11 +++++++++++
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/fs/proc/root.c b/fs/proc/root.c
index 06a297a27ba3..ed86ac710384 100644
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -363,12 +363,12 @@ static const struct inode_operations proc_root_inode_operations = {
  * This is the root "inode" in the /proc tree..
  */
 struct proc_dir_entry proc_root = {
-	.low_ino	= PROC_ROOT_INO, 
-	.namelen	= 5, 
-	.mode		= S_IFDIR | S_IRUGO | S_IXUGO, 
-	.nlink		= 2, 
+	.low_ino	= PROCFS_ROOT_INO,
+	.namelen	= 5,
+	.mode		= S_IFDIR | S_IRUGO | S_IXUGO,
+	.nlink		= 2,
 	.refcnt		= REFCOUNT_INIT(1),
-	.proc_iops	= &proc_root_inode_operations, 
+	.proc_iops	= &proc_root_inode_operations,
 	.proc_dir_ops	= &proc_root_operations,
 	.parent		= &proc_root,
 	.subdir		= RB_ROOT,
diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h
index 6258455e49a4..4b20375f3783 100644
--- a/include/linux/proc_ns.h
+++ b/include/linux/proc_ns.h
@@ -40,7 +40,6 @@ extern const struct proc_ns_operations timens_for_children_operations;
  * We always define these enumerators
  */
 enum {
-	PROC_ROOT_INO		= 1,
 	PROC_IPC_INIT_INO	= IPC_NS_INIT_INO,
 	PROC_UTS_INIT_INO	= UTS_NS_INIT_INO,
 	PROC_USER_INIT_INO	= USER_NS_INIT_INO,
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index 3d7bb6580cfb..0bd678a4a10e 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -60,6 +60,17 @@
 #define RENAME_EXCHANGE		(1 << 1)	/* Exchange source and dest */
 #define RENAME_WHITEOUT		(1 << 2)	/* Whiteout source */
 
+/*
+ * The root inode of procfs is guaranteed to always have the same inode number.
+ * For programs that make heavy use of procfs, verifying that the root is a
+ * real procfs root and using openat2(RESOLVE_{NO_{XDEV,MAGICLINKS},BENEATH})
+ * will allow you to make sure you are never tricked into operating on the
+ * wrong procfs file.
+ */
+enum procfs_ino {
+	PROCFS_ROOT_INO = 1,
+};
+
 struct file_clone_range {
 	__s64 src_fd;
 	__u64 src_offset;

---
base-commit: 40e87bc3b0e06018c908c338b73268ca12e28d89
change-id: 20250708-uapi-procfs-root-ino-a180d59a9a6d

Best regards,
-- 
Aleksa Sarai <cyphar@cyphar.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] uapi: export PROCFS_ROOT_INO
  2025-07-08 13:21 [PATCH] uapi: export PROCFS_ROOT_INO Aleksa Sarai
@ 2025-07-10  7:39 ` Christian Brauner
  0 siblings, 0 replies; 2+ messages in thread
From: Christian Brauner @ 2025-07-10  7:39 UTC (permalink / raw)
  To: Aleksa Sarai
  Cc: Christian Brauner, linux-kernel, linux-fsdevel, Alexander Viro,
	Jan Kara

On Tue, 08 Jul 2025 23:21:51 +1000, Aleksa Sarai wrote:
> The root inode of /proc having a fixed inode number has been part of the
> core kernel ABI since its inception, and recently some userspace
> programs (mainly container runtimes) have started to explicitly depend
> on this behaviour.
> 
> The main reason this is useful to userspace is that by checking that a
> suspect /proc handle has fstype PROC_SUPER_MAGIC and is PROCFS_ROOT_INO,
> they can then use openat2(RESOLVE_{NO_{XDEV,MAGICLINK},BENEATH}) to
> ensure that there isn't a bind-mount that replaces some procfs file with
> a different one. This kind of attack has lead to security issues in
> container runtimes in the past (such as CVE-2019-19921) and libraries
> like libpathrs[1] use this feature of procfs to provide safe procfs
> handling functions.
> 
> [...]

Applied to the vfs-6.17.nsfs branch of the vfs/vfs.git tree.
Patches in the vfs-6.17.nsfs branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.17.nsfs

[1/1] uapi: export PROCFS_ROOT_INO
      https://git.kernel.org/vfs/vfs/c/76fdb7eb4e1c

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-07-10  7:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08 13:21 [PATCH] uapi: export PROCFS_ROOT_INO Aleksa Sarai
2025-07-10  7:39 ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).