mirror of https://lore.kernel.org/lkml/
 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>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] xfs: log recovery copies a logged fork root by its own record count, into a fork sized by an unchecked di_forkoff
Date: Sun,  6 Dec 2026 14:52:52 +0530	[thread overview]
Message-ID: <xfs-log-fork-root.20261206@iiitd.ac.in> (raw)

Hi,

When log recovery replays an inode whose data or attr fork root was
logged (XFS_ILOG_DBROOT, XFS_ILOG_ABROOT), it converts the logged incore
root into the inode with xfs_bmbt_to_bmdr(), which copies as many keys
and pointers as the logged root says it has:

  fs/xfs/libxfs/xfs_bmap_btree.c:170 (xfs_bmbt_to_bmdr)
	dmxr = xfs_bmdr_maxrecs(dblocklen, 0);
	fkp = xfs_bmbt_key_addr(mp, rblock, 1);
	tkp = xfs_bmdr_key_addr(dblock, 1);
	fpp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, rblocklen);
	tpp = xfs_bmdr_ptr_addr(dblock, 1, dmxr);
	dmxr = be16_to_cpu(dblock->bb_numrecs);
	memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
	memcpy(tpp, fpp, sizeof(*fpp) * dmxr);

rblock is the log region, which xlog_recover_add_to_trans() allocates
at the length the log gives it, and rblocklen that length; dblock is the
fork in the inode buffer and dblocklen its size.  The two lengths only
place the pointer arrays.  Nothing on the way compares bb_numrecs with
either: not xlog_recover_inode_commit_pass2(), and not
xfs_dinode_verify(), which runs on the inode after the copy.
xfs_rtrmapbt_to_disk() and xfs_rtrefcountbt_to_disk(), which replay
the root of a realtime rmap or refcount btree inode
(xlog_recover_inode_dbroot()), copy their count unchecked the same way.

9232301894a0 ("xfs: bound inode fork length against the fork size
during log recovery") bounded the DDATA/DEXT and ADATA/AEXT copies of
the same function and left these two: its message says "The
XFS_ILOG_DBROOT sibling already passes XFS_DFORK_DSIZE as a bound", and
its comment (xfs_inode_item_recover.c:514) "The DBROOT/ABROOT cases
already bound their copies against the fork size".  They pass the fork
size, but the length they copy is bb_numrecs.

And the fork size itself comes from the logged inode, whose di_forkoff
xfs_log_dinode_to_disk() copies in before the forks are replayed.  The
one check on it

  fs/xfs/xfs_inode_item_recover.c:465 (xlog_recover_inode_commit_pass2)
	if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {

compares a count of 8-byte units with a size in bytes, so with 512-byte
inodes no value fails it.  xfs_dinode_verify_forkoff() would reject a
di_forkoff past the literal area, but only after the copy.

Our fuzzer found the first of the following; we found the others while
working out what the copy writes.  All were run on the upstream base
named below.

1. The count

The fuzzer made a file with a btree data fork on a 4 KiB-block V5
filesystem with 512-byte inodes, forced the log out with
XFS_IOC_GOINGDOWN(LOGFLUSH), set bb_numrecs of the logged root (an
88-byte region, one record) to 200, recomputed the record CRC and
mounted the image.  The inode was logged with di_forkoff 24: a 192-byte
data fork, room for a root of 11 records.

  BUG: KASAN: slab-out-of-bounds in xfs_bmbt_to_bmdr
                                    (fs/xfs/libxfs/xfs_bmap_btree.c:176)
  Read of size 1600 at addr ffff88804ad9a6c8 by task x/9442
   __asan_memcpy (mm/kasan/shadow.c:105)
   xfs_bmbt_to_bmdr (fs/xfs/libxfs/xfs_bmap_btree.c:176)
   xlog_recover_inode_commit_pass2 (fs/xfs/xfs_inode_item_recover.c:284
                                    fs/xfs/xfs_inode_item_recover.c:536)
   xlog_recover_items_pass2 (fs/xfs/xfs_log_recover.c:2020)
   xlog_recover_commit_trans (fs/xfs/xfs_log_recover.c:2089)
   ...
   xlog_recover (fs/xfs/xfs_log_recover.c:3506)
   xfs_log_mount (fs/xfs/xfs_log.c:621)
   xfs_mountfs (fs/xfs/xfs_mount.c:1051)
   xfs_fs_fill_super (fs/xfs/xfs_super.c:1987)
   ...
   __x64_sys_mount (fs/namespace.c:4350 ...)
  The buggy address is located 72 bytes inside of
   allocated 88-byte region [ffff88804ad9a680, ffff88804ad9a6d8)
  (kmalloc-96, allocated by xlog_recover_add_to_trans(),
   fs/xfs/xfs_log_recover.c:2209)

With kasan_multi_shot the pointers' memcpy() (:177, a tail call, so
the report names the caller) is reported too: a read of size 1600 from
80 bytes into the same region.  The same edit of a logged attr fork root
(XFS_ILOG_ABROOT, xfs_inode_item_recover.c:573) gives the same two
reads.

The two copies write as much as they read: 8 * N bytes from 4 bytes
into the fork and 8 * N from 4 + 8 * 11 = 92 bytes in, so past the
192-byte fork from N = 13 and past the inode's 336 bytes of forks from
N = 31.  KASAN does not report those writes, because __asan_memcpy()
gives up on the read before it checks the write; but it reports only
the first bad access of a boot, and lets the later ones through, which
shows what they do:

  - N = 200: after the one report, the other copies (the first root's
    pointers, and both copies of the two roots logged after it) each
    write 1600 bytes of kmalloc-96 memory over the rest of /f's inode
    and into the three inodes after it in the buffer.  At the end of
    recovery the buffer fails its write verifier, which dumps the next
    inode, kernel addresses (ffff8880..., ffffffff8...) and all; the
    filesystem shuts down and the mount fails with EUCLEAN.  Nothing
    reaches the disk.  The fuzzer's crash on 7.4-rc5 ended the same way.

  - N = 20, which stays inside the inode: the mount succeeds, and /f's
    inode goes to disk with 109 bytes changed, against 57 when the
    count is left at 1; the extra bytes are what the copies read past
    the region, from its neighbours in kmalloc-96.  We did not identify
    those objects.  The next iget of /f fails in xfs_iformat_btree()
    ("corrupt inode 8323 (btree)").

  - N = 200 with /f the last inode of its buffer, in a boot where KASAN
    had already reported once: the keys' copy runs off the end of the
    buffer.  To make that end visible we also set ilf_len of the logged
    inode items of that cluster to 24 basic blocks, so the 12 KiB
    buffer is vmalloc()ed (xfs_buf_alloc_backing_mem()) and followed by
    a guard page:

  BUG: unable to handle page fault for address: ffffc90002fcc000
  #PF: supervisor write access in kernel mode
  #PF: error_code(0x0002) - not-present page
  Oops: Oops: 0002 [#1] SMP KASAN PTI
  RIP: 0010:memcpy_orig+0x2c/0x140
   xfs_bmbt_to_bmdr (fs/xfs/libxfs/xfs_bmap_btree.c:176)
   xlog_recover_inode_commit_pass2 (fs/xfs/xfs_inode_item_recover.c:284
                                    fs/xfs/xfs_inode_item_recover.c:536)
   ...
  Kernel panic - not syncing: Fatal exception

2. The fork offset

With the root left as logged (one record, which fits both the region
and the real fork) and di_forkoff set to 255 in the logged inode, the
data fork is taken to be 2040 bytes, room for a root of 127 records, so
the pointer is written 4 + 8 * 127 = 1020 bytes into the fork, past the
inode.  With /f last in a 12 KiB buffer as above:

  BUG: KASAN: vmalloc-out-of-bounds in xlog_recover_inode_commit_pass2
                                    (fs/xfs/xfs_inode_item_recover.c:284
                                     fs/xfs/xfs_inode_item_recover.c:536)
  Write of size 8 at addr ffffc900031142ac by task poc/9483
   __asan_memcpy (mm/kasan/shadow.c:106)
   xlog_recover_inode_commit_pass2 (...)
  The buggy address belongs to a 3-page vmalloc region starting at
   0xffffc90003111000 allocated at xfs_buf_alloc+0x8ba/0xff0

684 bytes past the end of the buffer.  The same di_forkoff starts the
attr fork 2040 bytes into the inode with a size of XFS_LITINO(mp) - 2040
computed in size_t, so the ADATA/AEXT check that 9232301894a0 added
passes for any length.  /f with four extents and a 16-byte xattr
(logged with DEXT and ADATA), di_forkoff 255:

  BUG: KASAN: vmalloc-out-of-bounds in xlog_recover_inode_commit_pass2
                                    (fs/xfs/xfs_inode_item_recover.c:568)
  Write of size 24 at addr ffffc90002fc46a8 by task poc/9509

1704 bytes past the end of the buffer.  In both cases the recovered
inode then fails xfs_dinode_verify() and the mount fails with EUCLEAN;
the write has been done by then.

3. Not run: xfs_rtrmapbt_to_disk() and xfs_rtrefcountbt_to_disk()
copy the logged root of a metadir rtrmap or rtrefcount inode by its
bb_numrecs the same way, 48 or 12 bytes per record at level > 0 and 24
or 12 at level 0.  We could not make such a filesystem here (the VM
image has no mkfs.xfs; the host's xfsprogs, 5.13.0, predates metadir),
so this is from the source only.

Impact - who can trigger it:

  - A crafted XFS image with a dirty log has to be mounted, and nothing
    more: everything above happens inside mount(2), in log recovery,
    before the filesystem is usable.  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.  We mounted read-write and did not try a read-only mount.

  - The log needs no forgery beyond the edited fields: the record CRC,
    which the image's author recomputes, is all that guards them.

  - What is read: for a count N (at most 65535), 8 * N bytes from 72
    bytes into the logged root and 8 * N from where its pointers start
    (80 bytes in, for the 88-byte root), so up to about 512 KiB past a
    kmalloc() buffer of the root's size.  The neighbouring slab objects
    are what gets read.

  - What is written: the same bytes into the inode buffer, from 4 and
    from 4 + 8 * (the fork's root capacity) bytes into the fork.  We
    measured them landing over the next inodes of the buffer (and,
    with N = 20, in the inode written to disk), and running off the end
    of a vmalloc()ed buffer into its guard page.  With di_forkoff 255,
    a root that fits writes its pointers up to 1020 + 8 * (records)
    bytes into the data fork, and an ADATA or AEXT region, of bytes the
    image chooses, is written 2040 bytes into it; both are past a
    512-byte inode.  An inode buffer of the usual 16 KiB is a folio
    (xfs_buf_alloc_backing_mem()), so for the last inodes of a cluster
    these writes go into whatever page follows; we did not look at
    what that could be made to do.

  - What a kernel without KASAN does: we have no non-KASAN build in
    this rig.  In the runs above where KASAN had stopped reporting, the
    copies were carried out as such a kernel carries them out; that is
    as far as we measured.

Seen on 7.4.0-rc5 (two reports, the 88-byte root with N = 200) 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_inode_item_recover.c,
xfs_bmap_btree.c and xfs_rtrmap_btree.c are also unchanged in mainline
c3500c5ef648 (2026-12-04) and in xfs-linux for-next (ab1a77ab136c);
xfs_rtrefcount_btree.c differs in those two only in
xfs_rtrefcountbt_broot_realloc().

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

  without the patch
    count, data fork   a KASAN read in xfs_bmbt_to_bmdr() in 6 of 6
                       boots: the fuzzer's program unchanged, ours with
                       N = 200 four times and N = 20 once; with KASAN
                       no longer reporting and /f last in a vmalloc()ed
                       buffer, N = 200 ran off its end (1 boot, an oops)
    count, attr fork   the same reads in 2 of 2 boots
    di_forkoff 255     a KASAN write past the inode buffer, the root's
                       pointer in 2 of 2 boots and the ADATA copy in 2
                       of 2
  with the patch (kasan_multi_shot on): no KASAN report and no oops in
    5 boots, each running every trigger above (the first also the
    fuzzer's program).  The mount fails with EUCLEAN, and the kernel
    logs, for the count:
      XFS (loop0): Internal error Bad log dinode fork root (-117) at
        line 319 of file fs/xfs/xfs_inode_item_recover.c. ...
      XFS (loop0): Bad inode 0x2083, data fork root of 88 bytes with
        200 records, fork size 192
    ("attr fork root ... fork size 144" for the attr fork), and for
    di_forkoff 255:
      XFS (loop0): Internal error Bad log dinode fork offset (-117) at
        line 529 of file fs/xfs/xfs_inode_item_recover.c. ...
      XFS (loop0): Bad inode 0x2097, di_forkoff 0xff
    then "log mount/recovery failed: error -117".
  controls, the log as written (count 1, di_forkoff 24), CRCs still
    recomputed: both kernels recover the log and mount, /f stats, and
    no KASAN report or XFS error, in 4 boots on the base and 5 on
    the fixed kernel.

A patch follows in reply to this mail.  It checks the logged
di_forkoff against the literal area, as xfs_dinode_verify_forkoff()
does for the recovered inode, and checks a logged root's record count
against both its region and the fork it goes into (bmbt, rtrmap and
rtrefcount roots, data and attr fork) before anything is copied, failing
recovery with -EFSCORRUPTED and a corruption report.  The checks are in
log recovery, next to 9232301894a0's, rather than in xfs_bmbt_to_bmdr()
and the rt helpers, which the inode flush path also uses, with roots
the kernel built itself.

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, except the rtrmap and rtrefcount copies, which we
have from the source only.  The reproducer is one program that carries
its filesystem image, and we are happy to send it on request.

Kapil Vaswani
Piyus Kedia

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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  9:22 Piyus Kedia [this message]
2026-12-06  9:22 ` [PATCH] xfs: bound logged inode fork roots and the fork offset in log recovery 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-log-fork-root.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 \
    /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

all inboxes | Powered by JetHome®