Skip to content

Commit f7b5fa8

Browse files
Ibrahim Hashimovgregkh
authored andcommitted
xfs: bounds-check buffer log item's dirty bitmap
[ Upstream commit 813f813 ] xlog_recover_do_reg_buffer() replays each dirty region described by a buffer log item's bitmap into the buffer read for that item: memcpy(xfs_buf_offset(bp, (uint)bit << XFS_BLF_SHIFT), item->ri_buf[i].iov_base, nbits << XFS_BLF_SHIFT); The destination offset (bit/nbits, from the logged dirty bitmap) and the buffer size (from the logged blf_len) are both attacker-controlled and otherwise unrelated, yet the only thing bounding the copy is an ASSERT(), which compiles away on production kernels. A crafted image logging a small blf_len together with a bitmap bit past the end of that buffer drives the memcpy() past the buffer's allocation, corrupting adjacent kernel heap during mount-time log recovery. This is reachable by anyone who can get a crafted image mounted -- the malicious-filesystem threat model XFS already guards against elsewhere. Turn the ASSERT() into a real XFS_IS_CORRUPT() check that aborts recovery of the buffer with -EFSCORRUPTED, consistent with the validate-and-fail idiom already used in xlog_recover_do_inode_buffer() and xfs_dquot_item_recover.c. xlog_recover_do_reg_buffer() therefore becomes STATIC int and its three callers propagate the error. Found and confirmed with KASAN on a CONFIG_XFS_DEBUG=n build: the crafted image trips a slab-out-of-bounds write before this change and fails recovery cleanly with -EFSCORRUPTED after it. Fixes: 1da177e ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Ibrahim Hashimov <security@auditcode.ai> Reviewed-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Brian Foster <bfoster@redhat.com> Signed-off-by: Carlos Maiolino <cem@kernel.org> [ dropped the `xlog_recover_do_primary_sb_buffer()` error-propagation hunk and its primary-SB/rtsb arm since that helper doesn't exist, keeping only the `error = 0;` reset ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cacb124 commit f7b5fa8

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

fs/xfs/xfs_buf_item_recover.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ xlog_recover_validate_buf_type(
441441
* given buffer. The bitmap in the buf log format structure indicates
442442
* where to place the logged data.
443443
*/
444-
STATIC void
444+
STATIC int
445445
xlog_recover_do_reg_buffer(
446446
struct xfs_mount *mp,
447447
struct xlog_recover_item *item,
@@ -469,8 +469,24 @@ xlog_recover_do_reg_buffer(
469469
ASSERT(nbits > 0);
470470
ASSERT(item->ri_buf[i].iov_base != NULL);
471471
ASSERT(item->ri_buf[i].iov_len % XFS_BLF_CHUNK == 0);
472-
ASSERT(BBTOB(bp->b_length) >=
473-
((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
472+
/*
473+
* The bitmap is only trustworthy to the extent that it
474+
* describes a region that actually fits inside the buffer we
475+
* read in based on the (attacker-controlled) blf_len. Do not
476+
* rely on an ASSERT() for this -- it compiles away entirely on
477+
* non-DEBUG kernels, which is exactly where this matters, so
478+
* validate it for real and abort recovery of this buffer rather
479+
* than copying past the end of it.
480+
*/
481+
if (XFS_IS_CORRUPT(mp, BBTOB(bp->b_length) <
482+
((uint)bit << XFS_BLF_SHIFT) +
483+
(nbits << XFS_BLF_SHIFT))) {
484+
xfs_alert(mp,
485+
"Bad buffer log item dirty bitmap (bit %d, nbits %d) for %d-byte buffer at daddr 0x%llx.",
486+
bit, nbits, BBTOB(bp->b_length),
487+
xfs_buf_daddr(bp));
488+
return -EFSCORRUPTED;
489+
}
474490

475491
/*
476492
* The dirty regions logged in the buffer, even though
@@ -524,6 +540,7 @@ xlog_recover_do_reg_buffer(
524540
ASSERT(i == item->ri_total);
525541

526542
xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
543+
return 0;
527544
}
528545

529546
/*
@@ -532,10 +549,10 @@ xlog_recover_do_reg_buffer(
532549
* (ie. USR or GRP), then just toss this buffer away; don't recover it.
533550
* Else, treat it as a regular buffer and do recovery.
534551
*
535-
* Return false if the buffer was tossed and true if we recovered the buffer to
536-
* indicate to the caller if the buffer needs writing.
552+
* Return 0 if the buffer was not recovered (tossed), 1 if it was recovered and
553+
* needs writing, or a negative errno if recovery of the buffer failed.
537554
*/
538-
STATIC bool
555+
STATIC int
539556
xlog_recover_do_dquot_buffer(
540557
struct xfs_mount *mp,
541558
struct xlog *log,
@@ -544,14 +561,15 @@ xlog_recover_do_dquot_buffer(
544561
struct xfs_buf_log_format *buf_f)
545562
{
546563
uint type;
564+
int error;
547565

548566
trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
549567

550568
/*
551569
* Filesystems are required to send in quota flags at mount time.
552570
*/
553571
if (!mp->m_qflags)
554-
return false;
572+
return 0;
555573

556574
type = 0;
557575
if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
@@ -564,10 +582,12 @@ xlog_recover_do_dquot_buffer(
564582
* This type of quotas was turned off, so ignore this buffer
565583
*/
566584
if (log->l_quotaoffs_flag & type)
567-
return false;
585+
return 0;
568586

569-
xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
570-
return true;
587+
error = xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
588+
if (error)
589+
return error;
590+
return 1;
571591
}
572592

573593
/*
@@ -962,13 +982,16 @@ xlog_recover_buf_commit_pass2(
962982
goto out_release;
963983
} else if (buf_f->blf_flags &
964984
(XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
965-
bool dirty;
966-
967-
dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
968-
if (!dirty)
985+
error = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
986+
if (error <= 0)
969987
goto out_release;
988+
/* write dirty buffer */
989+
error = 0;
970990
} else {
971-
xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
991+
error = xlog_recover_do_reg_buffer(mp, item, bp, buf_f,
992+
current_lsn);
993+
if (error)
994+
goto out_release;
972995
}
973996

974997
/*

0 commit comments

Comments
 (0)