Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kernfs: make directory seek namespace-aware

The rbtree backing kernfs directories is ordered by (hash, ns_id, name)
but kernfs_dir_pos() only searches by hash when seeking to a position
during readdir. When two nodes from different namespaces share the same
hash value, the binary search can land on a node in the wrong namespace.
The subsequent skip-forward loop walks rb_next() and may overshoot the
correct node, silently dropping an entry from the readdir results.

With the recent switch from raw namespace pointers to public namespace
ids as hash seeds, computing hash collisions became an offline operation.
An unprivileged user could unshare into a new network namespace, create
a single interface whose name-hash collides with a target entry in
init_net, and cause a victim's seekdir/readdir on /sys/class/net to miss
that entry.

Fix this by extending the rbtree search in kernfs_dir_pos() to also
compare namespace ids when hashes match. Since the rbtree is already
ordered by (hash, ns_id, name), this makes the seek land directly in the
correct namespace's range, eliminating the wrong-namespace overshoot.

Signed-off-by: Christian Brauner <brauner@kernel.org>

+5
+5
fs/kernfs/dir.c
··· 1866 1866 } 1867 1867 if (!pos && (hash > 1) && (hash < INT_MAX)) { 1868 1868 struct rb_node *node = parent->dir.children.rb_node; 1869 + u64 ns_id = kernfs_ns_id(ns); 1869 1870 while (node) { 1870 1871 pos = rb_to_kn(node); 1871 1872 1872 1873 if (hash < pos->hash) 1873 1874 node = node->rb_left; 1874 1875 else if (hash > pos->hash) 1876 + node = node->rb_right; 1877 + else if (ns_id < kernfs_ns_id(pos->ns)) 1878 + node = node->rb_left; 1879 + else if (ns_id > kernfs_ns_id(pos->ns)) 1875 1880 node = node->rb_right; 1876 1881 else 1877 1882 break;