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>,
	Ibrahim Hashimov <security@auditcode.ai>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [PATCH] xfs: check buffer log item regions against the dirty bitmap in recovery
Date: Sun,  6 Dec 2026 18:11:38 +0530	[thread overview]
Message-ID: <20261206124138.1307637-1-piyus@iiitd.ac.in> (raw)
In-Reply-To: <xfs-log-buf-regions.20261206@iiitd.ac.in>

xlog_recover_do_reg_buffer() copies one logged region per run of set
bits in a buffer log item's dirty bitmap, and
xlog_recover_do_inode_buffer() reads each di_next_unlinked from the
region of the run that covers it.  Both find the region by counting
runs into item->ri_buf[], but ri_buf[] has as many entries as the
item's blf_size asked for when xlog_recover_add_to_trans() sized it,
the item may have been committed with fewer, and nothing ties either
number, or a region's length, to the bitmap.  Only ASSERTs check them.

Commit 813f8136a2ce ("xfs: bounds-check buffer log item's dirty
bitmap") bounded the bitmap by the buffer.  A buffer item with one
region and a bitmap of several runs still reads ri_buf[] past its end
when a crafted image is mounted:

  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)
   ...
   xfs_log_mount (fs/xfs/xfs_log.c:621)
  The buggy address is located 8 bytes to the right of
   allocated 32-byte region (kmalloc-32, xlog_recover_add_to_trans())

and then memcpy()s from the iov_base it read there.  In
xlog_recover_do_inode_buffer() a run longer than its region has
di_next_unlinked read past 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)

and a second run with no region left reads ri_buf[2] past its end, as
above.

Stop at a run that has no region left in both, and check that
di_next_unlinked lies inside the region it is read from.

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-buf-regions.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   buffer items of one region given a bitmap of every other
           128-byte chunk: KASAN at :518 in 3 of 3 runs, then a general
           protection fault in the memcpy(); the unlinked-list inode
           buffer item's bitmap moved to chunks 3-4: KASAN at :702 in
           3 of 3; chunks 1 and 4: KASAN at :700 and a fault, 1 of 1
  after    no report; the mount fails with EUCLEAN and the messages
           added
  control  the same logs written back unchanged: both kernels recover
           and mount

 fs/xfs/xfs_buf_item_recover.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 57929f115055..5b973aebbe44 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -486,6 +486,19 @@ xlog_recover_do_reg_buffer(
 		nbits = xfs_contig_bits(buf_f->blf_data_map,
 					buf_f->blf_map_size, bit);
 		ASSERT(nbits > 0);
+
+		/*
+		 * Each run of the bitmap is copied from the next logged region,
+		 * but the number of regions is the item's own blf_size, and
+		 * nothing ties it to the bitmap.  Do not index past the regions
+		 * the item has.
+		 */
+		if (XFS_IS_CORRUPT(mp, i >= item->ri_cnt)) {
+			xfs_alert(mp,
+				  "Too few regions (%d) for buffer log item bitmap at daddr 0x%llx.",
+				  item->ri_cnt - 1, xfs_buf_daddr(bp));
+			return -EFSCORRUPTED;
+		}
 		ASSERT(item->ri_buf[i].iov_base != NULL);
 		ASSERT(item->ri_buf[i].iov_len % XFS_BLF_CHUNK == 0);
 		/*
@@ -688,6 +701,22 @@ xlog_recover_do_inode_buffer(
 		if (next_unlinked_offset < reg_buf_offset)
 			continue;
 
+		/*
+		 * di_next_unlinked is read from the logged region of this run,
+		 * at its offset in the run.  Neither the number of regions nor
+		 * their lengths are tied to the bitmap, so make sure the field
+		 * lies inside a region the item has.
+		 */
+		if (XFS_IS_CORRUPT(mp, item_index >= item->ri_cnt ||
+				   next_unlinked_offset - reg_buf_offset +
+				   sizeof(xfs_agino_t) >
+				   item->ri_buf[item_index].iov_len)) {
+			xfs_alert(mp,
+				  "No logged region for inode buffer offset %d at daddr 0x%llx.",
+				  next_unlinked_offset, xfs_buf_daddr(bp));
+			return -EFSCORRUPTED;
+		}
+
 		ASSERT(item->ri_buf[item_index].iov_base != NULL);
 		ASSERT((item->ri_buf[item_index].iov_len % XFS_BLF_CHUNK) == 0);
 		ASSERT((reg_buf_offset + reg_buf_bytes) <= BBTOB(bp->b_length));
-- 
2.34.1


      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 [BUG] xfs: buffer log item recovery indexes regions by the dirty bitmap without a bound Piyus Kedia
2026-12-06 12:41 ` Piyus Kedia [this message]

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=20261206124138.1307637-1-piyus@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®