The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [BUG] xfs: slab-out-of-bounds past an attr node whose entry count exceeds its block
@ 2026-12-06  7:47 Piyus Kedia
  2026-12-06  7:47 ` [PATCH] xfs: bound the attr node entry count in the other dabtree walkers Piyus Kedia
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-12-06  7:47 UTC (permalink / raw)
  To: Carlos Maiolino, linux-xfs; +Cc: Darrick J . Wong, linux-kernel, Kapil Vaswani

Hi,

xfs_da3_node_verify() bounds a dabtree node's entry count only by the
larger of the directory and attr geometries:

  fs/xfs/libxfs/xfs_da_btree.c:246 (xfs_da3_node_verify)
	if (ichdr.count > mp->m_dir_geo->node_ents &&
	    ichdr.count > mp->m_attr_geo->node_ents)
		return __this_address;

When directory blocks are larger than fs blocks, an attr node, one fs
block, can claim more entries than it holds: with "mkfs.xfs -b
size=1024 -n size=65536" an attr node holds (1024 - 64) / 8 = 120
entries and the verifier accepts up to (65536 - 64) / 8 = 8184.
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() (xfs_da_btree.c:1798), which covers attr
lookups (getxattr, and the lookup before a set or remove).
Three other walkers of an attr node's btree[] do not go through
xfs_da3_node_lookup_int() and do not bound the count.  With the count
of an attr node raised to 121, its CRC recomputed and nothing else
changed, each of them reads past the node's 1024-byte buffer, and two
of them write past it.  Our fuzzer found the first two; we found the
third while checking the other walkers.

(3df5e5aaa3bc gives "getxattr/listxattr" as the way in, but listing does
not use xfs_da3_node_lookup_int(); see 1.)

1. Listing from a cursor: xfs_attr_node_list_lookup()

  fs/xfs/xfs_attr_list.c:270
		for (i = 0; i < nodehdr.count; btree++, i++) {
			if (cursor->hashval <= be32_to_cpu(btree->hashval)) {

A cursor hash above every hash in the node keeps the loop going to the
count.  XFS_IOC_ATTRLIST_BY_HANDLE takes the cursor from the caller (so
do the GETPARENTS ioctls, on a filesystem with parent pointers; we did
not run those).  listxattr(2) starts from a zero cursor and stops at
btree[0] on every level: on the crafted node it listed all 60 names,
getxattr failed with EUCLEAN (3df5e5aaa3bc), and ATTRLIST_BY_HANDLE
with cursor hash 0xffffffff gave

  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_file_ioctl (fs/xfs/xfs_ioctl.c:1548)
  The buggy address is located 0 bytes to the right of
   allocated 1024-byte region [ffff888000a63800, ffff888000a63c00)
  (kmalloc-rcl-1k, allocated by xfs_buf_alloc() for xfs_da3_node_read()
   from xfs_attr_node_list_lookup())

2. Inactivation: xfs_attr3_node_inactive()

It takes each child from btree[0] and then drops that entry with
xfs_attr3_node_entry_remove(*trans, dp, bp, 0) (xfs_attr_inactive.c:229),
that is __xfs_da3_node_remove():

  fs/xfs/libxfs/xfs_da_btree.c:1534
	if (index < nodehdr.count - 1) {
		tmp  = nodehdr.count - index - 1;
		tmp *= (uint)sizeof(xfs_da_node_entry_t);
		memmove(&btree[index], &btree[index + 1], tmp);
		...
	}
	memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));

For a count N the memmove() reads 8 * (N - 1) bytes from byte 72 of the
node and writes them at byte 64, and the memset() clears the 8 bytes at
64 + 8 * (N - 1).  With N = 121 that is a read ending 8 bytes past the
buffer and an 8-byte write just past it.  Unlinking the file:

  BUG: KASAN: slab-out-of-bounds in __xfs_da3_node_remove.isra.0+0x12d/0x270
  Read of size 960 at addr ffff8880284d3048 by task kworker/0:2/812
  Workqueue: xfs-inodegc/loop0 xfs_inodegc_worker
   __asan_memmove (mm/kasan/shadow.c:94)
   __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)
   xfs_inodegc_worker (fs/xfs/xfs_icache.c:1945 fs/xfs/xfs_icache.c:1991)
  The buggy address is located 72 bytes inside of
   allocated 1024-byte region [ffff8880284d3000, ffff8880284d3400)

and, with kasan_multi_shot, the memset() right after it:

  BUG: KASAN: slab-out-of-bounds in __xfs_da3_node_remove.isra.0+0x175/0x270
  Write of size 8 at addr ffff8880284d3400 by task kworker/0:2/812
   __asan_memset (mm/kasan/shadow.c:84)
   __xfs_da3_node_remove (fs/xfs/libxfs/xfs_da_btree.c:1542)
  The buggy address is located 0 bytes to the right of
   allocated 1024-byte region [ffff8880284d3000, ffff8880284d3400)

Under KASAN neither of the two is carried out (__asan_memmove() and
__asan_memset() return once their check fails), so our runs do not show
what a kernel without KASAN does next.  The loop has had this shape
since b854e1c4eff3 ("xfs: close crash window in attr dabtree
inactivation"), v7.0-rc6; before it, it re-read the parent for each
child and took btree[i + 1].before, which passes the end of the node
only after 120 children have been walked.

3. Shifting a path into the node: xfs_da3_path_shift()

On its way down into a node it has not walked before:

  fs/xfs/libxfs/xfs_da_btree.c:2245
			blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
			if (forward)
				blk->index = 0;
			else
				blk->index = nodehdr.count - 1;
			blkno = be32_to_cpu(btree[blk->index].before);

It is called by the attr leaf and node joins (xfs_attr3_leaf_toosmall(),
xfs_da3_node_toosmall()), by xfs_da3_node_lookup_int() when a leaf ends
in the hash it looks for, and by scrub; the node it comes down into is
one xfs_da3_node_lookup_int() did not read.  We made a three-level attr
tree with xfs_db on a 1 KiB-block filesystem (260 attrs with 300-byte
values: a root over two level-1 nodes, A with 89 entries and B with 99),
raised B's count to 121, and removed, as uid 65534, one of the two attrs
in the last leaf under A.  Joining that leaf with the next one shifts
the path forward into B:

  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)
   xfs_attr_set_iter (fs/xfs/libxfs/xfs_attr.c:934)
   xfs_attr_finish_item (fs/xfs/xfs_attr_item.c:503)
   xfs_defer_finish_noroll (fs/xfs/libxfs/xfs_defer.c:709)
   xfs_trans_commit (fs/xfs/xfs_trans.c:923)
   xfs_attr_set (fs/xfs/libxfs/xfs_attr.c:1243)
   xfs_xattr_set (fs/xfs/xfs_xattr.c:186)
   vfs_removexattr (fs/xattr.c:601)
   __x64_sys_removexattr (fs/xattr.c:1109 fs/xattr.c:1106 fs/xattr.c:1106)
  The buggy address is located 0 bytes to the right of
   allocated 1024-byte region [ffff888052db0000, ffff888052db0400)
  (kmalloc-rcl-1k, allocated by xfs_buf_alloc() for xfs_da3_node_read()
   from xfs_da3_path_shift(), xfs_da_btree.c:2203)

The join then drops B's btree[0] with xfs_da3_node_remove(), the same
memmove() and memset() as in 2, and kasan_multi_shot reports both (a
read of size 960 and a write of size 8, as above).  removexattr(2)
returned 0.

Impact - who can trigger it:

  - A crafted XFS image has to be mounted.  XFS is not
    FS_USERNS_MOUNT, so mount(2) needs CAP_SYS_ADMIN in the initial
    user namespace: root, or a service that mounts on a user's behalf.
    We have not tested whether desktop auto-mounting of removable media
    mounts such an image.  The image picks its own geometry, so no one
    has to have made a filesystem with -n size larger than -b size.

  - 1 also needs CAP_SYS_ADMIN: xfs_attrlist_by_handle() and the
    GETPARENTS ioctls check capable(CAP_SYS_ADMIN), and listxattr(2)
    does not get past btree[0].

  - 2 needs only the unlink of the file, i.e. write permission on its
    directory, which the image chooses.  The accesses then happen in the
    xfs-inodegc worker, after unlink(2) has returned.  We ran it as
    root.

  - 3 needs only the removal of a user xattr, i.e. write permission on
    the file, which the image chooses.  We ran it as uid 65534 with no
    capabilities, after root had mounted the image.

  - What is accessed, for a count N (up to 8184 with 64 KiB directory
    blocks).  1 reads the 4-byte hash of each entry from btree[120] to
    btree[N - 1], up to 8 * (N - 120) - 4 bytes past the buffer; by the
    source the values only steer the walk: a hash at or above the
    cursor's sends it to that entry's child block, which then has to
    read as a block of this attr fork.  2 and 3: the memmove() reads
    8 * (N - 120) bytes past the buffer and, for N > 121, writes
    8 * (N - 121) bytes past it, the bytes that lay 8 further on; the
    memset() then zeroes 8 bytes at 8 * (N - 121) past the end.  By the
    source, 2 repeats this for each child while the count is above 120.
    The buffer is a 1024-byte kmalloc-rcl-1k object (xfs_buf_alloc()),
    so what lies past it is the next object in that slab.  We have not
    looked at what those writes could be made to do.

  - After the access, in our runs: 1 returned 0 with no entries; in 2
    the walk went on to an entry past the real ones, XFS reported the
    corruption and shut the filesystem down; in 3 removexattr(2)
    returned 0 and the filesystem unmounted cleanly.

Seen on 7.4.0-rc5 (1 and 2, two reports each) and REPRODUCED ON
UPSTREAM: on ksmbd-for-next (d70570cf8003, 2026-12-04), our upstream
base, whose fs/xfs is identical to mainline master (9437ee8b6cf6), with
KASAN.  The three files are also unchanged in mainline c3500c5ef648
(2026-12-04) and in xfs-linux for-next (ab1a77ab136c).

Same config and image before and after, the patch the only change:

  without the patch
    1  a KASAN read in xfs_attr_node_list_lookup() in 4 of 4 runs (the
       hunt's own program, count 200, and ours, count 121)
    2  a KASAN read in __xfs_da3_node_remove() in 5 of 5 (the hunt's
       program and ours), and the write too in the 2 runs with
       kasan_multi_shot
    3  a KASAN read in xfs_da3_path_shift() in 3 of 3, and the read and
       write in __xfs_da3_node_remove() in the 2 runs with
       kasan_multi_shot
  with the patch (kasan_multi_shot on): no report in 5, 5 and 4 runs
    1  listxattr(2), getxattr(2) and both ATTRLIST_BY_HANDLE calls fail
       with EUCLEAN, and the kernel logs "Metadata corruption detected
       at xfs_attr_node_list_lookup"
    2  unlink(2) returns 0; inactivation logs the corruption in
       xfs_attr3_node_inactive() and shuts the filesystem down
    3  removexattr(2) fails with EUCLEAN; the kernel logs the
       corruption in xfs_da3_path_shift(), "deferred attr work failed,
       error -117", and shuts the filesystem down
  controls, the counts the images were built with (6, and 99 for B):
       both kernels list the 60 names, read them, unlink the file and
       remove the attr, with no KASAN report and no XFS error.  At 120,
       the most such a node holds, the unpatched kernel gives no KASAN
       report in any of the three.

A patch follows in reply to this mail.  It bounds 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 3df5e5aaa3bc does in
xfs_da3_node_lookup_int().  We looked at the other readers of node
entries: scrub's xchk_da_btree_block() bounds the count already,
xchk_xattr_find_leftmost_leaf() and xfs_da3_root_join() read only
btree[0] of a node whose count is not 0, xfs_da3_node_toosmall() uses a
sibling's count only to see whether it fits, and the remaining btree
operations work on nodes that xfs_da3_node_lookup_int() or
xfs_da3_path_shift() put in the path.  A directory node is one
directory block, and the directory node_ents is the larger of the two,
so the verifier's bound already fits directory nodes.

Bounding the count in xfs_da3_node_verify() by the buffer's own size,
(BBTOB(bp->b_length) - header size) / 8 entries, would cover every
walker at once, since an attr node buffer is one fs block and a
directory node buffer one directory block.  3df5e5aaa3bc chose to leave
the verifier alone, so the patch follows it; we can send it the other
way instead.

This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code, and every access
above was reproduced; what a kernel without KASAN does with the writes
we have from the source only, as said.  The reproducers are one program
each, carry their filesystem images, and we are happy to send them on
request.

Kapil Vaswani
Piyus Kedia

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

end of thread, other threads:[~2026-12-06  7:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  7:47 [BUG] xfs: slab-out-of-bounds past an attr node whose entry count exceeds its block Piyus Kedia
2026-12-06  7:47 ` [PATCH] xfs: bound the attr node entry count in the other dabtree walkers Piyus Kedia
2026-12-06  7:58   ` Piyus Kedia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox