[PATCH] xfs: check that the names fit before joining two attr leaves

From: Piyus Kedia

Date: Sun Dec 06 2026 - 04:21:18 EST


When an attr leaf gets small, xfs_attr3_leaf_toosmall() decides whether
it can be joined with a sibling from the usedbytes in the two headers,
and xfs_attr3_leaf_unbalance() then moves the entries of one into the
other with xfs_attr3_leaf_moveents(): into a temporary leaf if the kept
leaf has holes, or else right below the kept leaf's firstused.
xfs_attr3_leaf_moveents() places each name by its real size:

ichdr_d->firstused -= tmp;
...
entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
...
memmove(xfs_attr3_leaf_name(leaf_d, desti),
xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);

and only ASSERTs, compiled out without CONFIG_XFS_DEBUG or
CONFIG_XFS_WARN, check that firstused stays above the entry table.

The leaf verifier does not relate usedbytes to the names, so a leaf
that understates it gets joined with a sibling it does not fit with.
firstused, 32 bits in core, then wraps below zero, nameidx keeps its
low 16 bits, and the name is copied to the leaf + (firstused & 0xffff),
almost 64KiB past its start. With 1KiB blocks and a leaf holding four
212-byte local attrs whose usedbytes says 212 instead of 848, removing
one of them joins the other three with a sibling that holds two more,
1060 bytes of names for a 1024-byte block, and the last name goes to
the temporary leaf + 65500:

BUG: KASAN: slab-out-of-bounds in xfs_attr3_leaf_moveents.constprop.0
Write of size 212 at addr ffff8880224777dc by task poc/9462
__asan_memmove (mm/kasan/shadow.c:95)
xfs_attr3_leaf_moveents (fs/xfs/libxfs/xfs_attr_leaf.c:2815)
xfs_attr3_leaf_unbalance (fs/xfs/libxfs/xfs_attr_leaf.c:2532)
xfs_da3_join (fs/xfs/libxfs/xfs_da_btree.c:1163)
xfs_attr_node_remove_attr (fs/xfs/libxfs/xfs_attr.c:1552)
xfs_attr_set_iter (fs/xfs/libxfs/xfs_attr.c:934)

KASAN sees the write only when that address happens to be poisoned;
otherwise the copy is done.

usedbytes is not the only way there. A kept leaf without holes takes
the dropped names right below its firstused, which assumes that its
names are packed from the end of the block down to firstused, and
nothing checks that. A sibling whose lowest name sits just above its
entry table, with firstused there too, holes 0 and usedbytes right, is
something neither the verifier nor xfs_repair -n objects to, and when
its neighbour is joined into it the dropped name goes past its buffer
in the same way. So a usedbytes check in the verifier would not be
enough; only the join knows how much has to fit where.

Check, before moving anything, that the dropped names, and for the
temporary leaf the kept names as well, fit together with the merged
entry table below the kept leaf's firstused or in the block, sizing
them with xfs_attr_leaf_entsize() as xfs_attr3_leaf_moveents() does,
and report corruption if they do not. A consistent pair always fits,
since xfs_attr3_leaf_toosmall() keeps a quarter of the block spare.

The join has trusted these header fields since before git.

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-leaf-join.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. 1KiB blocks, the removal
done as uid 65534:

before usedbytes 212 for 848: KASAN write of 212 bytes at the
temporary leaf + 65500 in 5 of 5 runs, titled
slab-out-of-bounds, slab-use-after-free or use-after-free
after whatever lay there; in one more run KASAN did not
flag it and the copy was done; the fuzzer's own program:
1 of 1. The kept leaf with firstused lowered: KASAN write
at that leaf + 65424, 1 of 1
after no report in 5 runs of our program and 1 of the fuzzer's:
"Internal error need > room" from xfs_attr3_leaf_unbalance(),
removexattr(2) fails with EUCLEAN and the filesystem shuts
down, nothing moved; the lowered firstused: the same, 1 of 1
control usedbytes as built, three removals, the third joining the
leaf with its sibling through the temporary leaf: no report
on either kernel, 2 runs each, the merged leaf holds 3
entries and 636 bytes

Overlapping names can still overflow the same way when a leaf is
compacted on setxattr; the report has the details and we will send that
separately.

fs/xfs/libxfs/xfs_attr_leaf.c | 31 ++++++++++++++++++++++++++++++-
fs/xfs/libxfs/xfs_attr_leaf.h | 6 +++---
fs/xfs/libxfs/xfs_da_btree.c | 5 ++++-
3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_attr_leaf.c b/fs/xfs/libxfs/xfs_attr_leaf.c
index 82932b5c02fd..49a021d2ba65 100644
--- a/fs/xfs/libxfs/xfs_attr_leaf.c
+++ b/fs/xfs/libxfs/xfs_attr_leaf.c
@@ -2442,7 +2442,7 @@ xfs_attr3_leaf_remove(
/*
* Move all the attribute list entries from drop_leaf into save_leaf.
*/
-void
+int
xfs_attr3_leaf_unbalance(
struct xfs_da_state *state,
struct xfs_da_state_blk *drop_blk,
@@ -2453,6 +2453,9 @@ xfs_attr3_leaf_unbalance(
struct xfs_attr3_icleaf_hdr drophdr;
struct xfs_attr3_icleaf_hdr savehdr;
struct xfs_attr_leaf_entry *entry;
+ unsigned int need;
+ unsigned int room;
+ int i;

trace_xfs_attr_leaf_unbalance(state->args);

@@ -2460,6 +2463,31 @@ xfs_attr3_leaf_unbalance(
xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
entry = xfs_attr3_leaf_entryp(drop_leaf);

+ /*
+ * xfs_attr3_leaf_toosmall() decided that both leaves fit in one from
+ * the usedbytes in their headers, and a save leaf without holes takes
+ * the dropped names right below its firstused. Nothing checks either
+ * against the names, and xfs_attr3_leaf_moveents() lowers firstused
+ * by the real size of each name it moves without checking that it
+ * stays above the entry table. Check that the names fit before
+ * moving any of them.
+ */
+ need = xfs_attr_leaf_entries_end(savehdr.count + drophdr.count,
+ save_leaf);
+ for (i = 0; i < drophdr.count; i++)
+ need += xfs_attr_leaf_entsize(drop_leaf, i);
+ if (savehdr.holes == 0) {
+ room = savehdr.firstused;
+ } else {
+ room = state->args->geo->blksize;
+ for (i = 0; i < savehdr.count; i++)
+ need += xfs_attr_leaf_entsize(save_leaf, i);
+ }
+ if (XFS_IS_CORRUPT(state->mp, need > room)) {
+ xfs_da_mark_sick(state->args);
+ return -EFSCORRUPTED;
+ }
+
/*
* Save last hashval from dying block for later Btree fixup.
*/
@@ -2548,6 +2576,7 @@ xfs_attr3_leaf_unbalance(
*/
entry = xfs_attr3_leaf_entryp(save_leaf);
save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
+ return 0;
}

/*========================================================================
diff --git a/fs/xfs/libxfs/xfs_attr_leaf.h b/fs/xfs/libxfs/xfs_attr_leaf.h
index 72639efe6ac3..0b39cd62799b 100644
--- a/fs/xfs/libxfs/xfs_attr_leaf.h
+++ b/fs/xfs/libxfs/xfs_attr_leaf.h
@@ -91,9 +91,9 @@ int xfs_attr3_leaf_list_int(struct xfs_buf *bp,
int xfs_attr3_leaf_init(struct xfs_trans *tp, struct xfs_inode *dp,
xfs_dablk_t blkno);
int xfs_attr3_leaf_toosmall(struct xfs_da_state *state, int *retval);
-void xfs_attr3_leaf_unbalance(struct xfs_da_state *state,
- struct xfs_da_state_blk *drop_blk,
- struct xfs_da_state_blk *save_blk);
+int xfs_attr3_leaf_unbalance(struct xfs_da_state *state,
+ struct xfs_da_state_blk *drop_blk,
+ struct xfs_da_state_blk *save_blk);
/*
* Utility routines.
*/
diff --git a/fs/xfs/libxfs/xfs_da_btree.c b/fs/xfs/libxfs/xfs_da_btree.c
index faf88488265c..9009601cd087 100644
--- a/fs/xfs/libxfs/xfs_da_btree.c
+++ b/fs/xfs/libxfs/xfs_da_btree.c
@@ -1160,7 +1160,10 @@ xfs_da3_join(
return error;
if (action == 0)
return 0;
- xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
+ error = xfs_attr3_leaf_unbalance(state, drop_blk,
+ save_blk);
+ if (error)
+ return error;
break;
case XFS_DIR2_LEAFN_MAGIC:
error = xfs_dir2_leafn_toosmall(state, &action);
--
2.34.1