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>,
Ibrahim Hashimov <security@auditcode.ai>,
linux-kernel@vger.kernel.org,
Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] xfs: buffer log item recovery indexes regions by the dirty bitmap without a bound
Date: Sun, 6 Dec 2026 18:11:31 +0530 [thread overview]
Message-ID: <xfs-log-buf-regions.20261206@iiitd.ac.in> (raw)
Hi,
Mounting an XFS image whose log is dirty replays the buffer items in
the log, and both ways a buffer item is replayed find the logged
region for each run of set bits in the item's dirty bitmap by counting
runs into item->ri_buf[], without checking that the region exists or
that it is as long as its run. Our fuzzer found two out-of-bounds
reads this way, both inside mount(2). The code is identical in
7.4-rc5, mainline and xfs/for-next, and the line numbers below hold
for all three.
xlog_recover_add_to_trans() sizes item->ri_buf[] by the item's
blf_size (xfs_log_recover.c:2235), and the item is committed with
however many regions arrived, ri_cnt <= ri_total; 2094dab19d45 ("xfs:
fail recovery on a committed log item with no regions") rejects only
ri_cnt == 0. Each image below is one filesystem whose dirty log has a
field of its items changed and the record's CRC recomputed; the same
log written back unchanged recovers and mounts on every kernel we ran.
xlog_recover_do_reg_buffer() copies one region per run of set bits in
blf_data_map, and nothing bounds i:
fs/xfs/xfs_buf_item_recover.c:480
i = 1; /* 0 is the buf format structure */
while (1) {
bit = xfs_next_bit(buf_f->blf_data_map, ...);
...
if (item->ri_buf[i].iov_len < (nbits << XFS_BLF_SHIFT))
nbits = item->ri_buf[i].iov_len >> XFS_BLF_SHIFT;
...
memcpy(xfs_buf_offset(bp, (uint)bit << XFS_BLF_SHIFT),
item->ri_buf[i].iov_base, nbits << XFS_BLF_SHIFT);
i++;
bit += nbits;
}
/* Shouldn't be any more regions */
ASSERT(i == item->ri_total);
813f8136a2ce ("xfs: bounds-check buffer log item's dirty bitmap")
bounds each run by the buffer, not by the regions. A buffer item with
one data region (blf_size 2) and a bitmap of every other 128-byte
chunk:
BUG: KASAN: slab-out-of-bounds in xlog_recover_do_reg_buffer+0x235/0x580
Read of size 8 at addr ffff8880216d70e8 by task x/9437
xlog_recover_do_reg_buffer (fs/xfs/xfs_buf_item_recover.c:518)
xlog_recover_buf_commit_pass2 (fs/xfs/xfs_buf_item_recover.c:1128)
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)
The buggy address is located 8 bytes to the right of
allocated 32-byte region (kmalloc-32: ri_buf[] of two entries,
xlog_recover_add_to_trans(), xfs_log_recover.c:2236)
That is ri_buf[2].iov_len; ri_buf[2].iov_base, the 8 bytes after the
array (without KASAN, the start of the next kmalloc-32 object), is
then the memcpy() source at :549, and in all three of our runs and
both of the fuzzer's it was not a valid pointer:
Oops: general protection fault, ... non-canonical address 0x9a025400000010
RIP: 0010:memcpy_orig+0x16/0x140
xlog_recover_do_reg_buffer (fs/xfs/xfs_buf_item_recover.c:549)
xlog_recover_do_inode_buffer(), for XFS_BLF_INODE_BUF items, counts
runs the same way into item_index and reads each inode's
di_next_unlinked from its run's region, at its offset in the run:
fs/xfs/xfs_buf_item_recover.c:700
logged_nextp = item->ri_buf[item_index].iov_base +
next_unlinked_offset - reg_buf_offset;
if (XFS_IS_CORRUPT(mp, *logged_nextp == 0)) {
Neither item_index nor the offset is checked against what was logged.
The unlink of a file logs one 128-byte region of its 16 KiB inode
cluster buffer; with that item's bitmap moved to chunks 3 and 4, inode
1's di_next_unlinked (buffer offset 608) is read 224 bytes into the
region:
BUG: KASAN: slab-out-of-bounds in xlog_recover_do_inode_buffer+0x29a/0x4c0
Read of size 4 at addr ffff88801cd77fe0 by task poc/9456
xlog_recover_do_inode_buffer (fs/xfs/xfs_buf_item_recover.c:702)
xlog_recover_buf_commit_pass2 (fs/xfs/xfs_buf_item_recover.c:1100)
xlog_recover_items_pass2 (fs/xfs/xfs_log_recover.c:2020)
The buggy address is located 96 bytes to the right of
allocated 128-byte region (kmalloc-128: the logged region,
xlog_recover_add_to_trans(), xfs_log_recover.c:2209)
With chunks 1 and 4 instead (two runs, one region) ri_buf[2] is read
past its end at :700 and its iov_base dereferenced, a general
protection fault as above. The fuzzer set XFS_BLF_INODE_BUF on 4 KiB
buffer items of one 128-byte region with a run over chunks 1 to 31:
the read was 480 bytes into the region, and KASAN called it
use-after-free once and out-of-bounds once, after what lay there.
Impact - who can trigger it
- A crafted XFS image with a dirty log has to be mounted; 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 (we measured that
with a dquot item). The image chooses its own geometry and
contents.
- What is read, and what it feeds:
1. xlog_recover_do_reg_buffer(): one 16-byte struct kvec per run
beyond the item's regions, past ri_buf[] (16 * blf_size bytes,
kmalloc). Its length and pointer become the length (at most
the run, which 813f8136a2ce keeps in the buffer) and the source
of a memcpy() into the metadata buffer being recovered. Every
run of ours and the fuzzer's ended in a general protection fault
there. By the source, if the bytes past the array hold a kernel
pointer, the memcpy() copies that kernel memory into the buffer,
which recovery then writes to the image if the buffer's verifier
passes; we have not tested that.
2. xlog_recover_do_inode_buffer(): 4 bytes at the region plus the
field's offset in its run, up to the buffer's length (16 KiB
here) past a region of 128 bytes, or a struct kvec past ri_buf[]
as in 1. A non-zero value is stored as that inode's
di_next_unlinked, the inode's CRC recomputed and the buffer
written back (by the source); in our runs it read 0, which XFS
rejects, and the mount failed.
Seen on 7.4.0-rc5 (each read 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_buf_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
reg_buffer: bitmap of every other chunk 3 of 3, + fault
inode buffer: run 96 bytes past the region 3 of 3
inode buffer: two runs, one region 1 of 1, + fault
with the patch, kasan_multi_shot on: no report, alone in two
boots that each ran every trigger above (and the fuzzer's bitmap),
and together with our other log recovery fixes in three more (one
of them the fuzzer's own programs). Each mount fails with EUCLEAN
after an XFS_IS_CORRUPT() report and one of
Too few regions (1) for buffer log item bitmap at daddr 0x0.
No logged region for inode buffer offset 608 at daddr 0x24c0.
controls, the same logs written back unchanged: both kernels recover
and mount.
Not covered by the patch, by the source and not run: the buffer
format's blf_flags is read by xlog_recover_buf_reorder() before
xfs_buf_log_check_iovec() checks the format region's length in pass 1,
and xlog_recover_add_to_trans() reads the size field of a first region
that may be 1 to 3 bytes long. We can send a patch for those.
A patch follows in reply to this mail. In both functions it stops at
a bitmap run that has no region left, and in
xlog_recover_do_inode_buffer() it checks that di_next_unlinked lies in
the region it is read from; the mount then fails with EUCLEAN and the
corruption report.
We are reporting four related log recovery bugs separately, each with
its own patch: inode items whose fork regions are missing, an
unchecked inode ilf_boffset, an unchecked dquot qlf_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
next reply other threads:[~2026-12-06 12:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 12:41 Piyus Kedia [this message]
2026-12-06 12:41 ` [PATCH] xfs: check buffer log item regions against the dirty bitmap in 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-buf-regions.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=security@auditcode.ai \
/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®