* [BUG] xfs: dquot log item recovery reads and writes at an unchecked qlf_boffset
@ 2026-12-06 12:43 Piyus Kedia
2026-12-06 12:43 ` [PATCH] xfs: check the dquot log item regions and offset in recovery Piyus Kedia
0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 12:43 UTC (permalink / raw)
To: Carlos Maiolino, linux-xfs
Cc: Darrick J . Wong, Long Li, linux-kernel, Kapil Vaswani
Hi,
Mounting an XFS image whose log is dirty, with quota accounting on,
replays the dquot items in the log, and
xlog_recover_dquot_commit_pass2() finds the dquot at the item's
qlf_boffset in the buffer it reads for it, without checking the
offset against that buffer. Our fuzzer found an out-of-bounds read
this way, inside mount(2), and the logged dquot is then copied past
the buffer, which we measured. The code is identical in 7.4-rc5,
mainline and xfs/for-next, and the line numbers below hold for all
three.
fs/xfs/xfs_dquot_item_recover.c:135
dqb = xfs_buf_offset(bp, dq_f->qlf_boffset);
ddq = &dqb->dd_diskdq;
if (xfs_has_crc(mp)) {
xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0)
goto out_release;
}
memcpy(ddq, recddq, item->ri_buf[1].iov_len);
qlf_boffset is not checked against the buffer, and the length of the
copy, ri_buf[1].iov_len, only from below. The image below is one
filesystem whose dirty log has a field of its dquot items changed and
the record's CRC recomputed; the same log written back unchanged
recovers and mounts on every kernel we ran. qlf_boffset 1024 on a
1 KiB dquot buffer, mounted -o uquota:
BUG: KASAN: slab-out-of-bounds in xlog_recover_dquot_commit_pass2+0x2f8/0x5e0
Read of size 8 at addr ffff88802770b470 by task poc/9461
xlog_recover_dquot_commit_pass2 (fs/xfs/xfs_dquot_item_recover.c:143)
xlog_recover_items_pass2 (fs/xfs/xfs_log_recover.c:2020)
xlog_recover_commit_trans (fs/xfs/xfs_log_recover.c:2089)
...
xfs_log_mount (fs/xfs/xfs_log.c:621)
The buggy address is located 112 bytes to the right of
allocated 1024-byte region (kmalloc-rcl-1k, the dquot buffer)
The dd_lsn found there did not stop the replay: with kasan_multi_shot
the next reports are the copy and the checksum,
BUG: KASAN: slab-out-of-bounds in xlog_recover_dquot_commit_pass2+0x3e2/0x5e0
Write of size 104 at addr ffff8880285c1c00 by task poc/9455
xlog_recover_dquot_commit_pass2 (fs/xfs/xfs_dquot_item_recover.c:150)
at the first byte past the buffer, then xfs_update_cksum() (:152).
KASAN's memcpy() skips a copy it reports; in our runs without
kasan_multi_shot only the dd_lsn read is reported, the copy goes
ahead, and recovery stops later at "Bad dquot after recovery" (:159).
Both this function and xlog_recover_dquot_ra_pass2() also take
ri_buf[1] of an item sized for one region (qlf_size 1): the NULL check
there catches a region that did not arrive, not an array entry that
does not exist (KASAN reports the readahead's read, at :37).
Impact - who can trigger it
- A crafted XFS image with a dirty log has to be mounted with quota
accounting (m_qflags, from the mount options; -o uquota here); all
of the above happens inside mount(2), before the filesystem can be
used. 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. Recovery runs at every mount
of a dirty log but -o norecovery (which has to be read-only); a
read-only mount (MS_RDONLY) replays the log too, measured on both
kernels. The image chooses its own geometry and contents.
- What is read, and what is written: the dquot block anywhere up to
4 GiB past the buffer. dd_lsn is read there, and unless it looks
newer than the transaction, the logged dquot (104 bytes as the
kernel logs it, unbounded above) is copied there and a CRC stored
after it. Measured: a 104-byte copy to the first byte past a
1 KiB buffer (KASAN skips the copies it reports, and in our runs
without kasan_multi_shot did not report it, so it ran).
- The write lands in whatever follows the buffer, here a 1 KiB
kmalloc object. We have not looked at what such writes could be
made to do on a kernel without KASAN.
Seen on 7.4.0-rc5 (twice) 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.
fs/xfs/xfs_dquot_item_recover.c is also unchanged in mainline
c3500c5ef648 (2026-12-04) and in xfs-linux for-next (ab1a77ab136c),
where the patch applies as is.
Same config and image before and after, the patch the only change;
one boot per run, the fuzzer's own program included where it had one:
without the patch KASAN report
qlf_boffset 1024 in a 1 KiB buffer 4 of 4
qlf_size 1 1 of 1
with the patch, kasan_multi_shot on: no report, alone in one boot
and together with our other log recovery fixes in three more (one
of them the fuzzer's own program). The mount fails with EUCLEAN
after an XFS_IS_CORRUPT() report and one of
dquot ID 0x0 log item offset 1024 in 1024-byte buffer.
NULL dquot in xlog_recover_dquot_commit_pass2.
and a read-only mount fails the same way.
controls, the same logs written back unchanged: both kernels recover
and mount.
Not covered by the patch, by the source and not run: the dquot format
region (24 bytes) is read in readahead and pass 2 without a length
check, and so is a QUOTAOFF item's qf_flags. We can send a patch for
those.
A patch follows in reply to this mail. It checks that the item has
its dquot region, that the region is sizeof(struct xfs_disk_dquot) as
the kernel logs it, and that the dquot block at qlf_boffset lies in
the buffer. The error goes out through out_release, which
e2b4a856085e ("xfs: don't swallow dquot recovery verification errors")
made return it.
We are reporting four related log recovery bugs separately, each with
its own patch: buffer items whose dirty bitmap outruns their regions,
inode items whose fork regions are missing, an unchecked inode
ilf_boffset, and the inode buffer verifier's inode count.
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. The reproducer is one program that carries its
filesystem image, 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 the dquot log item regions and offset in recovery
2026-12-06 12:43 [BUG] xfs: dquot log item recovery reads and writes at an unchecked qlf_boffset Piyus Kedia
@ 2026-12-06 12:43 ` Piyus Kedia
0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 12:43 UTC (permalink / raw)
To: Carlos Maiolino, linux-xfs
Cc: Darrick J . Wong, Long Li, linux-kernel, Kapil Vaswani
xlog_recover_dquot_commit_pass2() finds the dquot at qlf_boffset in
the buffer read for it, and the offset is not checked against the
buffer. dd_lsn is read there, and unless it is newer than the
transaction, the logged dquot is memcpy()ed over it, as many bytes as
the logged region has: the region's length is bounded only from below.
qlf_boffset 1024 on a 1 KiB dquot buffer:
BUG: KASAN: slab-out-of-bounds in xlog_recover_dquot_commit_pass2+0x2f8/0x5e0
Read of size 8 at addr ffff88802770b470 by task poc/9461
xlog_recover_dquot_commit_pass2 (fs/xfs/xfs_dquot_item_recover.c:143)
xlog_recover_items_pass2 (fs/xfs/xfs_log_recover.c:2020)
...
The buggy address is located 112 bytes to the right of
allocated 1024-byte region (kmalloc-rcl-1k, the dquot buffer)
It and xlog_recover_dquot_ra_pass2() also take the dquot from ri_buf[1]
of an item sized for one region (qlf_size 1), one entry past the array;
the NULL check there catches a region that is missing, not an array
entry that is.
Check that the item has the dquot region and that the region is the
size of the struct xfs_disk_dquot it is logged as, and that the dquot
block at qlf_boffset lies inside the buffer. The error returns through
out_release, which commit e2b4a856085e ("xfs: don't swallow dquot
recovery verification errors") made return it.
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-dquot-boffset.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) and to xfs/for-next (ab1a77ab136c),
where this applies as is; KASAN, the same config and image
before and after. A dirty log with one field changed and the record
CRC recomputed:
before qlf_boffset 1024 on a 1 KiB dquot buffer, -o uquota: KASAN
at :143 in 4 of 4 runs, and with kasan_multi_shot the
104-byte copy at :150; qlf_size 1: at :37 in 1 of 1
after no report, alone and with our other log recovery fixes; the
mount fails with EUCLEAN and the messages added
Stable trees without e2b4a856085e ("xfs: don't swallow dquot recovery
verification errors") return 0 from out_release: the access is still
prevented there, but the corrupt item is skipped instead of failing
recovery.
fs/xfs/xfs_dquot_item_recover.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/fs/xfs/xfs_dquot_item_recover.c b/fs/xfs/xfs_dquot_item_recover.c
index b8854316ac75..bfdfdd9b2b5b 100644
--- a/fs/xfs/xfs_dquot_item_recover.c
+++ b/fs/xfs/xfs_dquot_item_recover.c
@@ -34,9 +34,9 @@ xlog_recover_dquot_ra_pass2(
if (mp->m_qflags == 0)
return;
- recddq = item->ri_buf[1].iov_base;
- if (recddq == NULL)
+ if (item->ri_cnt < 2)
return;
+ recddq = item->ri_buf[1].iov_base;
if (item->ri_buf[1].iov_len < sizeof(struct xfs_disk_dquot))
return;
@@ -79,14 +79,19 @@ xlog_recover_dquot_commit_pass2(
if (mp->m_qflags == 0)
return 0;
- recddq = item->ri_buf[1].iov_base;
- if (recddq == NULL) {
+ /*
+ * qlf_size sized the item's regions and the dquot is the second one;
+ * the item may have fewer. The whole region is copied over the dquot
+ * in the buffer below, so it has to be the size it is logged with.
+ */
+ if (item->ri_cnt < 2) {
xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
return -EFSCORRUPTED;
}
- if (item->ri_buf[1].iov_len < sizeof(struct xfs_disk_dquot)) {
- xfs_alert(log->l_mp, "dquot too small (%zd) in %s.",
- item->ri_buf[1].iov_len, __func__);
+ recddq = item->ri_buf[1].iov_base;
+ if (item->ri_buf[1].iov_len != sizeof(struct xfs_disk_dquot)) {
+ xfs_alert(log->l_mp, "bad dquot size (%zd) in %s.",
+ item->ri_buf[1].iov_len, __func__);
return -EFSCORRUPTED;
}
@@ -132,6 +137,16 @@ xlog_recover_dquot_commit_pass2(
return error;
ASSERT(bp);
+
+ /* qlf_boffset comes from the log: the dquot has to be in the buffer. */
+ if (XFS_IS_CORRUPT(mp, (u64)dq_f->qlf_boffset +
+ sizeof(struct xfs_dqblk) > BBTOB(bp->b_length))) {
+ xfs_alert(mp,
+ "dquot ID 0x%x log item offset %u in %u-byte buffer.",
+ dq_f->qlf_id, dq_f->qlf_boffset, BBTOB(bp->b_length));
+ error = -EFSCORRUPTED;
+ goto out_release;
+ }
dqb = xfs_buf_offset(bp, dq_f->qlf_boffset);
ddq = &dqb->dd_diskdq;
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 12:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06 12:43 [BUG] xfs: dquot log item recovery reads and writes at an unchecked qlf_boffset Piyus Kedia
2026-12-06 12:43 ` [PATCH] xfs: check the dquot log item regions and offset in recovery Piyus Kedia
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®