* [BUG] xfs: attr leaf join writes a name almost 64KiB past the leaf
@ 2026-12-06 9:20 Piyus Kedia
2026-12-06 9:21 ` [PATCH] xfs: check that the names fit before joining two attr leaves Piyus Kedia
0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 9:20 UTC (permalink / raw)
To: Carlos Maiolino, linux-xfs
Cc: Darrick J . Wong, Aldo Ariel Panzardo, Hongling Zeng,
linux-kernel, Kapil Vaswani
Hi,
When removexattr(2) leaves an attr leaf small, xfs_da3_join() asks
xfs_attr3_leaf_toosmall() whether it can be joined with a sibling and
then has xfs_attr3_leaf_unbalance() move one leaf's entries into the
other. Both trust the leaf headers, and xfs_attr3_leaf_moveents() does
not check where the names end up. On a crafted image the last name is
copied 65500 bytes past the start of a 1024-byte buffer:
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)
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_fremovexattr (fs/xattr.c:1120)
The buggy address belongs to the object at ffff888022476000
which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 2012 bytes to the right of
allocated 4096-byte region [ffff888022476000, ffff888022477000)
(ksmbd-for-next d70570cf8003 with KASAN; the "object" is a neighbour,
not the leaf: the temporary leaf in this run is the kmalloc-1k object
at ffff888022467800, and ffff8880224777dc is that + 65500.)
What happens. xfs_attr3_leaf_moveents() places each name it moves
below the destination's firstused, by the name's real size:
fs/xfs/libxfs/xfs_attr_leaf.c:2808
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);
The in-core firstused is 32 bits, nameidx is 16. If the names do not
fit, firstused wraps below zero, nameidx keeps its low 16 bits and
xfs_attr3_leaf_name() puts the copy at the leaf + (firstused & 0xffff).
The only checks are ASSERTs (:2813, :2825), compiled out without
CONFIG_XFS_DEBUG or CONFIG_XFS_WARN.
Whether the names fit is decided from the headers.
xfs_attr3_leaf_toosmall() joins two leaves if their usedbytes and entry
tables leave a quarter of a block spare:
fs/xfs/libxfs/xfs_attr_leaf.c:2251
bytes = state->args->geo->blksize -
(state->args->geo->blksize >> 2) -
ichdr.usedbytes - ichdr2.usedbytes -
xfs_attr_leaf_entries_end(ichdr.count + ichdr2.count,
leaf);
and xfs_attr3_leaf_unbalance() then moves both leaves' entries into a
temporary leaf starting from firstused = blksize (:2498, :2512) if the
kept leaf has holes, or else the dropped entries straight into the kept
leaf, below its firstused (:2473). xfs_attr3_leaf_verify() checks that
firstused lies between the header and the end of the block, and that
each entry's name lies at or above firstused and inside the block
(c84760659dcf, tightened this year by b7eea80be25f and 15ccd92a782a).
Nothing ties usedbytes to the names, and nothing makes a leaf without
holes have its names packed down from the end of the block to
firstused, which is what the in-place join assumes.
How it is reached. The fuzzer's image has 1 KiB blocks and a file
with 40 user xattrs of 200 bytes, so its attr fork is a node over 16
leaves of two to four 212-byte local entries. In the first leaf (four
entries, usedbytes 848, firstused 176) usedbytes is set to 212 and the
block CRC recomputed; xfs_repair -n reports "would reset usedbytes cnt
from 212 to 848" and nothing else. Removing one of its attrs leaves
usedbytes 0 for three real entries, xfs_attr3_leaf_remove() asks for a
join, and toosmall() joins it with its sibling, two entries with
usedbytes 424: 768 - 0 - 424 - 120 >= 0. The kept leaf has a hole
where the removed name was, so both go into a temporary leaf: four
names take it from 1024 down to 176, and the fifth goes to 176 - 212 =
-36, nameidx 0xffdc. The trace above is that copy, 212 bytes (the
local name/value structure of the sibling's last attr, user.big_43: its
lengths, the name and the 200-byte value from the image) to the
temporary leaf + 65500, 64476 bytes past its end. removexattr(2)
returns 0. The merged leaf, holding an entry with nameidx 65500, is
logged, and at unmount the write verifier rejects it and shuts the
filesystem down.
The fuzzer saw this twice on 7.4-rc5, at xfs_attr_leaf.c:2815 both
times, as "KASAN: slab-out-of-bounds Write in xfs_attr3_leaf_moveents"
and "KASAN: slab-use-after-free Write in xfs_attr3_leaf_moveents": the
title names whatever lies 64KiB past the temporary leaf, a redzone in
one run and a freed kmalloc-4k buffer in the other. When that memory is
not poisoned KASAN says nothing and the copy is done: in the fuzzer's
console log two runs of the same image ended with the corrupt leaf at
unmount and no KASAN report, and so did the first of our upstream runs
(below).
usedbytes is not the only way there. With every usedbytes right, take
a leaf of two entries, move one of its names down to just above its
entry table, lower firstused to that name and leave holes at 0;
xfs_repair -n (xfsprogs 5.13) reports nothing on that image. When its
higher-numbered neighbour is joined into it, the join is done in place,
below that firstused, and the dropped name goes past the buffer in the
same way:
BUG: KASAN: use-after-free in xfs_attr3_leaf_moveents.constprop.0
Write of size 212 at addr ffff888028880f90 by task variant/9455
(the same stack; the destination this time is the kept leaf's own
1024-byte buffer at ffff888028871000, + 65424 = 100 - 212 as a u16.
removexattr(2) returned 0, and the unmount shut the filesystem down
as above.)
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.
- After that it needs only the removal of a user xattr, i.e. write
permission on the file, which the image chooses. Our reproducer
makes the file mode 0666 in the image and removes the xattr as uid
65534 with no capabilities, after root has mounted it.
- What is written: a whole name/value structure from the image, here
212 bytes, to the temporary leaf (kvzalloc(blksize), kmalloc-1k for
1 KiB blocks) + 65500, or, in the in-place case, to the kept leaf's
buffer (an xfs_buf, kmalloc-rcl-1k) + 65424. Its contents, the
name and value bytes, are the image's. The offset is (u16)(the
in-core firstused after the wrap): almost 64KiB past the start of
the leaf for 1 KiB to 32 KiB blocks, and further names of a larger
excess go below it, 212 bytes at a time here. What lies there is
whatever the allocator put there: in our upstream runs KASAN called
it the redzone after a kmalloc-512 or a kmalloc-4k object, a freed
kmalloc-8k object, or a freed page. We have not tried to aim it.
- What follows: the merged leaf keeps an entry whose nameidx is past
the block. Until it is written back, readers of the cached buffer
follow it: in our first run, where KASAN had not flagged the write,
a listxattr(2) after the removal got "KASAN: use-after-free Read of
size 1 in xfs_attr3_leaf_list_int" 65502 bytes past the kept leaf.
At writeback the verifier stops it and the filesystem shuts down,
so the corrupt leaf does not reach the disk.
Seen on 7.4.0-rc5 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.
xfs_attr_leaf.c, xfs_attr_leaf.h and xfs_da_btree.c are also unchanged
in mainline c3500c5ef648 (2026-12-04) and in xfs-linux for-next
(ab1a77ab136c), and nothing in either touches this.
On ksmbd-for-next, with KASAN, the same config and image before and
after the patch below, the removal done as uid 65534:
before the fuzzer's image (usedbytes 212 for 848): KASAN write of
212 bytes at the temporary leaf + 65500 in 5 of 5 runs,
titled slab-out-of-bounds (2), slab-use-after-free (1) and
use-after-free (2) after whatever lay there; before those,
one run in which KASAN did not flag it and the copy was done;
the fuzzer's own program, unchanged: 1 of 1. The second
image (firstused lowered): KASAN write at the kept leaf +
65424, 1 of 1. removexattr(2) returned 0 each time.
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, nothing is moved, and the
filesystem shuts down, since the removal had already changed
the leaf in that transaction. The second image: the same,
1 of 1.
control the fuzzer's image with usedbytes as built, removing three of
the leaf's four attrs: the third removal joins it with its
sibling through the temporary leaf, as above. No report and
a clean unmount on either kernel, 2 runs each, and the merged
leaf on disk holds 3 entries, usedbytes 636.
A patch follows in reply to this mail. Before moving anything,
xfs_attr3_leaf_unbalance() adds up the sizes of the names it is about
to move, with xfs_attr_leaf_entsize() as xfs_attr3_leaf_moveents() does,
and checks that they and the merged entry table fit below the kept
leaf's firstused (in place) or in the block (temporary leaf); if not it
reports corruption and returns -EFSCORRUPTED, which xfs_da3_join()
passes up. A pair of consistent leaves always passes, since toosmall()
keeps a quarter of the block spare.
We put the check in the join rather than in the verifier. A verifier
check that usedbytes is the sum of the entry sizes would reject the
fuzzer's image, but not the second one, whose usedbytes is right; what
is wrong there is the claim that a leaf without holes is packed, which
neither the verifier nor xfs_repair -n checks, and only the join knows
how much has to fit where. Unlike 3df5e5aaa3bc ("xfs: bound da-node
entry count against the correct geometry") this is not about the
verifier lacking fork context: an attr leaf is always an attr leaf, but
whether two of them fit in one is a question about the pair. We can
add the usedbytes check to the verifier as well if you prefer.
Not covered by the patch: the same arithmetic in
xfs_attr3_leaf_compact(), reached from setxattr(2), when names overlap.
With a leaf whose largest local value was stretched over the next name
and usedbytes set to the sum of the three entry sizes (1060, so a
usedbytes check would pass it; xfs_repair -n reports the overlap),
replacing an attr in that leaf compacts it and the stretched name goes
to the leaf's own buffer + 65500 (KASAN: Write of size 636 in
xfs_attr3_leaf_moveents, then eleven more reports at the same place
from xfs_attr3_leaf_add_work(), which put the new entry there, among
them a write of its 200-byte value, and from the lookups after it;
setxattr(2) returned 0). The compaction path cannot return an error
today; a verifier check that the names fit between firstused and the
end of the block (which overlapping names fail) would cover it. We
would send that separately.
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 other block sizes do we have from the
source only, as said. The reproducer builds the fuzzer's image in
place, runs as one program, and we are happy to send it on request.
Kapil Vaswani
Piyus Kedia
^ permalink raw reply [flat|nested] 2+ messages in thread* [PATCH] xfs: check that the names fit before joining two attr leaves
2026-12-06 9:20 [BUG] xfs: attr leaf join writes a name almost 64KiB past the leaf Piyus Kedia
@ 2026-12-06 9:21 ` Piyus Kedia
0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 9:21 UTC (permalink / raw)
To: Carlos Maiolino, linux-xfs
Cc: Darrick J . Wong, Aldo Ariel Panzardo, Hongling Zeng,
linux-kernel, Kapil Vaswani
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@iiitd.ac.in/
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 9:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 9:20 [BUG] xfs: attr leaf join writes a name almost 64KiB past the leaf Piyus Kedia
2026-12-06 9:21 ` [PATCH] xfs: check that the names fit before joining two attr leaves Piyus Kedia
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox