[PATCH] xfs: bound the attr node entry count in the other dabtree walkers
From: Piyus Kedia
Date: Sun Dec 06 2026 - 02:47:49 EST
xfs_da3_node_verify() bounds a da node's entry count only by the larger
of the directory and attr node_ents, since a buffer verifier cannot tell
which tree a node belongs to. When directory blocks are larger than fs
blocks, an attr node, one fs block, can claim more entries than it holds
and still pass: with "mkfs.xfs -b size=1024 -n size=65536" an attr node
holds 120 entries and the verifier accepts up to 8184.
Commit 3df5e5aaa3bc ("xfs: bound da-node entry count against the correct
geometry") bounds the count by args->geo->node_ents in
xfs_da3_node_lookup_int(), but three other walkers index an attr node's
btree[] by that count without going through it:
- xfs_attr_node_list_lookup() compares the list cursor's hash with
btree[i].hashval for every i below the count, so a cursor hash above
the node's hashes (XFS_IOC_ATTRLIST_BY_HANDLE takes the cursor from
the caller) reads past the node:
BUG: KASAN: slab-out-of-bounds in xfs_attr_node_list_lookup+0x2ef/0x760
Read of size 4 at addr ffff888000a63c00 by task poc/9453
xfs_attr_node_list_lookup (fs/xfs/xfs_attr_list.c:271)
xfs_attr_node_list (fs/xfs/xfs_attr_list.c:399)
xfs_attr_list_ilocked (fs/xfs/xfs_attr_list.c:579)
xfs_attr_list (fs/xfs/xfs_attr_list.c:596)
xfs_ioc_attr_list (fs/xfs/xfs_handle.c:456)
xfs_attrlist_by_handle (fs/xfs/xfs_handle.c:486)
- xfs_attr3_node_inactive() drops btree[0] with
xfs_attr3_node_entry_remove(), whose memmove() moves count - 1
entries down and whose memset() then clears btree[count - 1]:
BUG: KASAN: slab-out-of-bounds in __xfs_da3_node_remove.isra.0+0x12d/0x270
Read of size 960 at addr ffff88805432b048 by task kworker/0:1/11
Workqueue: xfs-inodegc/loop0 xfs_inodegc_worker
__xfs_da3_node_remove (fs/xfs/libxfs/xfs_da_btree.c:1537)
xfs_attr3_node_entry_remove (fs/xfs/libxfs/xfs_da_btree.c:1585)
xfs_attr3_node_inactive (fs/xfs/xfs_attr_inactive.c:229)
xfs_attr3_root_inactive (fs/xfs/xfs_attr_inactive.c:285) (inlined)
xfs_attr_inactive (fs/xfs/xfs_attr_inactive.c:371)
xfs_inactive (fs/xfs/xfs_inode.c:1484)
and, with kasan_multi_shot, a write of size 8 just past the buffer
from the memset() (xfs_da_btree.c:1542).
- xfs_da3_path_shift(), coming down into a node it has not walked
before, reads btree[count - 1] and may leave blk->index there. A
removexattr(2) whose leaf join shifts the path into such a node reads
past it, and the join's xfs_da3_node_remove() then does the same
memmove() and memset() on it:
BUG: KASAN: slab-out-of-bounds in xfs_da3_path_shift+0x801/0x8a0
Read of size 4 at addr ffff888052db0400 by task poc/9461
xfs_da3_path_shift (fs/xfs/libxfs/xfs_da_btree.c:2245)
xfs_attr3_leaf_toosmall (fs/xfs/libxfs/xfs_attr_leaf.c:2275)
xfs_da3_join (fs/xfs/libxfs/xfs_da_btree.c:1158)
xfs_attr_node_remove_attr (fs/xfs/libxfs/xfs_attr.c:1552)
Bound the count by the attr geometry in xfs_attr_node_list_lookup() and
xfs_attr3_node_inactive(), and by args->geo in xfs_da3_path_shift(), as
xfs_da3_node_lookup_int() does. The other readers of node entries
bound the count already (xchk_da_btree_block()), read only btree[0] of
a node whose count is not 0 (xchk_xattr_find_leftmost_leaf(),
xfs_da3_root_join()), use a sibling's count only to see whether it fits
(xfs_da3_node_toosmall()), or work on nodes that
xfs_da3_node_lookup_int() or xfs_da3_path_shift() put in the path. A
directory node fits the verifier's bound already, since the directory
node_ents is the larger one.
The walkers have trusted the on-disk count since before git. The
verifier had no bound on it at all until
commit f5ea110044fa ("xfs: add CRCs to dir2/da node blocks"),
which added the one above.
We found this with a syzkaller-based fuzzing harness driven by an AI tool.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Closes: https://lore.kernel.org/r/xfs-attr-node-count.20261206@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Tested on ksmbd-for-next (d70570cf8003), whose fs/xfs is identical to
mainline master (9437ee8b6cf6), with KASAN, the same config and image
before and after. The three files are the same in xfs/for-next
(ab1a77ab136c), where this applies as is. Filesystems made with
"mkfs.xfs -b size=1024 -n size=65536", the count of one attr node
raised to 121:
before listing with cursor hash 0xffffffff: KASAN in 4 of 4 runs;
unlink: KASAN in 5 of 5; removexattr(2) as uid 65534 on a
three-level attr tree: KASAN in 3 of 3
after no report in 5, 5 and 4 runs with kasan_multi_shot: listing
fails with EUCLEAN, and the inactivation and the attr removal
shut the filesystem down, as other attr fork corruption does
control the counts as built (6 and 99): both kernels list, unlink and
remove as usual, no report
fs/xfs/libxfs/xfs_da_btree.c | 9 +++++++++
fs/xfs/xfs_attr_inactive.c | 13 +++++++++++++
fs/xfs/xfs_attr_list.c | 7 +++++++
3 files changed, 29 insertions(+)
diff --git a/fs/xfs/libxfs/xfs_da_btree.c b/fs/xfs/libxfs/xfs_da_btree.c
index faf88488265c..b7da8130fafb 100644
--- a/fs/xfs/libxfs/xfs_da_btree.c
+++ b/fs/xfs/libxfs/xfs_da_btree.c
@@ -2241,6 +2241,15 @@ xfs_da3_path_shift(
blk->magic = XFS_DA_NODE_MAGIC;
xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
bp->b_addr);
+ /*
+ * As in xfs_da3_node_lookup_int(), bound the count by
+ * the geometry of the fork we are walking.
+ */
+ if (nodehdr.count > args->geo->node_ents) {
+ xfs_buf_mark_corrupt(blk->bp);
+ xfs_da_mark_sick(args);
+ return -EFSCORRUPTED;
+ }
btree = nodehdr.btree;
blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
if (forward)
diff --git a/fs/xfs/xfs_attr_inactive.c b/fs/xfs/xfs_attr_inactive.c
index a5b69c0fbfd0..c060c4163f05 100644
--- a/fs/xfs/xfs_attr_inactive.c
+++ b/fs/xfs/xfs_attr_inactive.c
@@ -153,6 +153,19 @@ xfs_attr3_node_inactive(
}
xfs_da3_node_hdr_from_disk(mp, &ichdr, bp->b_addr);
+
+ /*
+ * The node verifier only bounds the count by the larger of the
+ * directory and attr geometries, and each entry removal below moves
+ * count - 1 entries; make sure they all fit in this node.
+ */
+ if (ichdr.count > mp->m_attr_geo->node_ents) {
+ xfs_buf_mark_corrupt(bp);
+ xfs_trans_brelse(*trans, bp);
+ xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
+ return -EFSCORRUPTED;
+ }
+
parent_blkno = xfs_buf_daddr(bp);
if (!ichdr.count) {
xfs_trans_brelse(*trans, bp);
diff --git a/fs/xfs/xfs_attr_list.c b/fs/xfs/xfs_attr_list.c
index f751edb930b6..348a0c587ea1 100644
--- a/fs/xfs/xfs_attr_list.c
+++ b/fs/xfs/xfs_attr_list.c
@@ -258,6 +258,13 @@ xfs_attr_node_list_lookup(
if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
goto out_corruptbuf;
+ /*
+ * The node verifier only bounds the count by the larger of the
+ * directory and attr geometries; don't walk off this node.
+ */
+ if (nodehdr.count > mp->m_attr_geo->node_ents)
+ goto out_corruptbuf;
+
/* Check the level from the root node. */
if (cursor->blkno == 0)
expected_level = nodehdr.level - 1;
--
2.34.1