Linux XFS filesystem development
 help / color / mirror / Atom feed
From: Piyus Kedia <piyus@iiitd.ac.in>
To: Carlos Maiolino <cem@kernel.org>, linux-xfs@vger.kernel.org
Cc: "Darrick J . Wong" <djwong@kernel.org>,
	Aldo Ariel Panzardo <qwe.aldo@gmail.com>,
	Hongling Zeng <zenghongling@kylinos.cn>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] xfs: attr leaf join writes a name almost 64KiB past the leaf
Date: Sun,  6 Dec 2026 14:50:58 +0530	[thread overview]
Message-ID: <xfs-attr-leaf-join.20261206@iiitd.ac.in> (raw)

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

             reply	other threads:[~2026-12-06  9:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  9:20 Piyus Kedia [this message]
2026-12-06  9:21 ` [PATCH] xfs: check that the names fit before joining two attr leaves Piyus Kedia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xfs-attr-leaf-join.20261206@iiitd.ac.in \
    --to=piyus@iiitd.ac.in \
    --cc=cem@kernel.org \
    --cc=djwong@kernel.org \
    --cc=kapil@presceralabs.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=qwe.aldo@gmail.com \
    --cc=zenghongling@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox