History

uapi: export PROCFS_ROOT_INO

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

Aleksa Sarai committed 1 year ago

Commit 76fdb7e1 parent cd267cd3 files · +16 −6Browse files

Unified · Split

3 files
  1. fs/proc/root.c +5 −5
  2. include/linux/proc_ns.h +0 −1
  3. include/uapi/linux/fs.h +11 −0
fs/proc/root.c+5−5
@@ -363,12 +363,12 @@ static const struct inode_operations proc_root_inode_operations = { · 362 unmodified lines
363 * This is the root "inode" in the /proc tree..363 * This is the root "inode" in the /proc tree..
364 */364 */
365struct proc_dir_entry proc_root = {365struct proc_dir_entry proc_root = {
366 .low_ino = PROC_ROOT_INO, 366 .low_ino = PROCFS_ROOT_INO,
367 .namelen = 5, 367 .namelen = 5,
368 .mode = S_IFDIR | S_IRUGO | S_IXUGO, 368 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
369 .nlink = 2, 369 .nlink = 2,
370 .refcnt = REFCOUNT_INIT(1),370 .refcnt = REFCOUNT_INIT(1),
371 .proc_iops = &proc_root_inode_operations, 371 .proc_iops = &proc_root_inode_operations,
372 .proc_dir_ops = &proc_root_operations,372 .proc_dir_ops = &proc_root_operations,
373 .parent = &proc_root,373 .parent = &proc_root,
374 .subdir = RB_ROOT,374 .subdir = RB_ROOT,
include/linux/proc_ns.h+0−1
@@ -40,7 +40,6 @@ extern const struct proc_ns_operations timens_for_children_operations; · 39 unmodified lines
40 * We always define these enumerators40 * We always define these enumerators
41 */41 */
42enum {42enum {
43 PROC_ROOT_INO = 1,
44 PROC_IPC_INIT_INO = IPC_NS_INIT_INO,43 PROC_IPC_INIT_INO = IPC_NS_INIT_INO,
45 PROC_UTS_INIT_INO = UTS_NS_INIT_INO,44 PROC_UTS_INIT_INO = UTS_NS_INIT_INO,
46 PROC_USER_INIT_INO = USER_NS_INIT_INO,45 PROC_USER_INIT_INO = USER_NS_INIT_INO,
include/uapi/linux/fs.h+11−0
@@ -60,6 +60,17 @@ · 59 unmodified lines
60#define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */60#define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
61#define RENAME_WHITEOUT (1 << 2) /* Whiteout source */61#define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
62 62
63/*
64 * The root inode of procfs is guaranteed to always have the same inode number.
65 * For programs that make heavy use of procfs, verifying that the root is a
66 * real procfs root and using openat2(RESOLVE_{NO_{XDEV,MAGICLINKS},BENEATH})
67 * will allow you to make sure you are never tricked into operating on the
68 * wrong procfs file.
69 */
70enum procfs_ino {
71 PROCFS_ROOT_INO = 1,
72};
73
63struct file_clone_range {74struct file_clone_range {
64 __s64 src_fd;75 __s64 src_fd;
65 __u64 src_offset;76 __u64 src_offset;