[PATCH] xfs: bound logged inode fork roots and the fork offset in log recovery

From: Piyus Kedia

Date: Sun Dec 06 2026 - 04:23:11 EST


xlog_recover_inode_commit_pass2() replays a logged fork root
(XFS_ILOG_DBROOT, XFS_ILOG_ABROOT) with xfs_bmbt_to_bmdr(), or for a
metadata btree file with xfs_rtrmapbt_to_disk() or
xfs_rtrefcountbt_to_disk(). These copy as many keys and pointers, or
records, as the logged root's bb_numrecs says, from the log region into
the inode fork:

dmxr = be16_to_cpu(dblock->bb_numrecs);
memcpy(tkp, fkp, sizeof(*fkp) * dmxr);
memcpy(tpp, fpp, sizeof(*fpp) * dmxr);

Nothing compares that count with the length of the region or with the
size of the fork. A crafted image whose dirty log holds a data fork
root of 88 bytes (one record) that claims 200 records reads past the
region at mount time:

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
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)
...
xfs_log_mount (fs/xfs/xfs_log.c:621)
xfs_mountfs (fs/xfs/xfs_mount.c:1051)

and the copies write as many bytes past the fork, over the inodes that
follow it in the inode buffer and, for the last inode of a buffer, past
the buffer.

Commit 9232301894a0 ("xfs: bound inode fork length against the fork size
during log recovery") bounded the DDATA/DEXT/ADATA/AEXT copies and left
these two, as "the XFS_ILOG_DBROOT sibling already passes
XFS_DFORK_DSIZE as a bound". The fork size only tells the helper where
the pointers go; the length copied is the root's count.

The fork sizes come from the logged di_forkoff, which is checked against
sb_inodesize; but di_forkoff counts 8-byte units, so with 512-byte
inodes every value passes. With di_forkoff 255 the data fork is taken
to be 2040 bytes, and the pointer of a one-record root that fits both
the region and the real fork is written 1020 bytes into it, past the
inode:

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

The attr fork then starts 2040 bytes in, with a negative size that the
ADATA/AEXT check added by that commit compares as unsigned, so an ADATA
region is copied there as well (xfs_inode_item_recover.c:568).

Check di_forkoff against the space in the inode, as
xfs_dinode_verify_forkoff() does for the recovered inode, and check a
logged root's record count against both the region and the fork before
anything is copied, failing recovery with -EFSCORRUPTED and a corruption
report.

The copy has trusted the logged count since before git, and the
di_forkoff check has compared 8-byte units with bytes as long.

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-log-fork-root.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 images
before and after. The file is the same in xfs/for-next (ab1a77ab136c),
where this applies as is. A 4 KiB-block V5 filesystem with 512-byte
inodes, a file whose fork root is logged, the log edited, then mount:

before data fork root claiming 200 (or 20) records: KASAN read in
xfs_bmbt_to_bmdr() in 6 of 6 boots; attr fork root: 2 of 2;
di_forkoff 255: KASAN write past the inode buffer from the
root's pointer (2 of 2) and from the ADATA copy (2 of 2)
after no report in 5 boots (kasan_multi_shot) running all of
these: recovery fails with EUCLEAN and "Bad log dinode fork
root" or "Bad log dinode fork offset"
control the log as written: both kernels recover and mount

Not tested: the rtrmap and rtrefcount roots (no metadir mkfs.xfs here).
Stable trees without xlog_recover_inode_dbroot() (before v6.14) need
the bmbt and di_forkoff parts adapted.

fs/xfs/xfs_inode_item_recover.c | 82 +++++++++++++++++++++++++++++++--
1 file changed, 79 insertions(+), 3 deletions(-)

diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 8da95131ab55..6d5df24592dc 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -269,6 +269,64 @@ xlog_dinode_verify_extent_counts(
return 0;
}

+/*
+ * A fork root logged with XFS_ILOG_[DA]BROOT is an incore btree root. The
+ * helpers that convert it to the ondisk format copy as many records, or keys
+ * and pointers, as its bb_numrecs says from the log region into the inode
+ * fork, so make sure that the region holds that many and that the fork has
+ * room for them.
+ */
+static int
+xlog_recover_verify_broot(
+ struct xfs_mount *mp,
+ struct xfs_inode_log_format *in_f,
+ struct xfs_dinode *dip,
+ int whichfork,
+ struct kvec *region)
+{
+ struct xfs_btree_block *rblock = region->iov_base;
+ size_t fsize = XFS_DFORK_SIZE(dip, mp, whichfork);
+ unsigned int level, numrecs = 0;
+ size_t incore, ondisk;
+
+ if (region->iov_len < xfs_bmbt_block_len(mp))
+ goto bad;
+ level = be16_to_cpu(rblock->bb_level);
+ numrecs = be16_to_cpu(rblock->bb_numrecs);
+
+ if (whichfork == XFS_DATA_FORK &&
+ dip->di_format == XFS_DINODE_FMT_META_BTREE) {
+ switch (be16_to_cpu(dip->di_metatype)) {
+ case XFS_METAFILE_RTRMAP:
+ incore = xfs_rtrmap_broot_space_calc(level, numrecs);
+ ondisk = xfs_rtrmap_droot_space_calc(level, numrecs);
+ break;
+ case XFS_METAFILE_RTREFCOUNT:
+ incore = xfs_rtrefcount_broot_space_calc(level, numrecs);
+ ondisk = xfs_rtrefcount_droot_space_calc(level, numrecs);
+ break;
+ default:
+ /* xlog_recover_inode_dbroot() rejects it */
+ return 0;
+ }
+ } else {
+ incore = xfs_bmap_broot_space_calc(mp, numrecs);
+ ondisk = xfs_bmdr_space_calc(numrecs);
+ }
+ if (incore <= region->iov_len && ondisk <= fsize)
+ return 0;
+bad:
+ XFS_CORRUPTION_ERROR("Bad log dinode fork root", XFS_ERRLEVEL_LOW, mp,
+ region->iov_base,
+ min_t(size_t, region->iov_len,
+ xfs_bmbt_block_len(mp)));
+ xfs_alert(mp,
+ "Bad inode 0x%llx, %s fork root of %zu bytes with %u records, fork size %zu",
+ in_f->ilf_ino, whichfork == XFS_DATA_FORK ? "data" : "attr",
+ region->iov_len, numrecs, fsize);
+ return -EFSCORRUPTED;
+}
+
static inline int
xlog_recover_inode_dbroot(
struct xfs_mount *mp,
@@ -462,7 +520,12 @@ xlog_recover_inode_commit_pass2(
if (error)
goto out_release;

- if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
+ /*
+ * di_forkoff is in units of 8 bytes and sizes and places both forks
+ * below, so it has to leave room for both inside the inode, as
+ * xfs_dinode_verify_forkoff() requires of the recovered inode.
+ */
+ if (unlikely(ldip->di_forkoff >= XFS_B_TO_FORKOFFT(XFS_LITINO(mp)))) {
XFS_CORRUPTION_ERROR("Bad log dinode fork offset",
XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
xfs_alert(mp,
@@ -511,8 +574,8 @@ xlog_recover_inode_commit_pass2(
* The recovered inode is verified only after the fork data has been
* copied into it, so bound each logged fork region against the size of
* its fork now, before the memcpy below can overrun the on-disk inode.
- * The DBROOT/ABROOT cases already bound their copies against the fork
- * size.
+ * A logged btree root is copied as many records as it says it has, so
+ * that count is checked against both the region and the fork.
*/
if ((fields & (XFS_ILOG_DDATA | XFS_ILOG_DEXT)) &&
item->ri_buf[2].iov_len > XFS_DFORK_DSIZE(dip, mp)) {
@@ -525,6 +588,19 @@ xlog_recover_inode_commit_pass2(
error = -EFSCORRUPTED;
goto out_release;
}
+ if ((fields & XFS_ILOG_DFORK) == XFS_ILOG_DBROOT) {
+ error = xlog_recover_verify_broot(mp, in_f, dip, XFS_DATA_FORK,
+ &item->ri_buf[2]);
+ if (error)
+ goto out_release;
+ }
+ if ((fields & XFS_ILOG_AFORK) == XFS_ILOG_ABROOT) {
+ attr_index = (fields & XFS_ILOG_DFORK) ? 3 : 2;
+ error = xlog_recover_verify_broot(mp, in_f, dip, XFS_ATTR_FORK,
+ &item->ri_buf[attr_index]);
+ if (error)
+ goto out_release;
+ }

switch (fields & XFS_ILOG_DFORK) {
case XFS_ILOG_DDATA:
--
2.34.1