Skip to content
/ linux Public

Commit d69de52

Browse files
Darrick J. WongSasha Levin
authored andcommitted
xfs: check for deleted cursors when revalidating two btrees
[ Upstream commit 55e03b8 ] The free space and inode btree repair functions will rebuild both btrees at the same time, after which it needs to evaluate both btrees to confirm that the corruptions are gone. However, Jiaming Zhang ran syzbot and produced a crash in the second xchk_allocbt call. His root-cause analysis is as follows (with minor corrections): In xrep_revalidate_allocbt(), xchk_allocbt() is called twice (first for BNOBT, second for CNTBT). The cause of this issue is that the first call nullified the cursor required by the second call. Let's first enter xrep_revalidate_allocbt() via following call chain: xfs_file_ioctl() -> xfs_ioc_scrubv_metadata() -> xfs_scrub_metadata() -> `sc->ops->repair_eval(sc)` -> xrep_revalidate_allocbt() xchk_allocbt() is called twice in this function. In the first call: /* Note that sc->sm->sm_type is XFS_SCRUB_TYPE_BNOPT now */ xchk_allocbt() -> xchk_btree() -> `bs->scrub_rec(bs, recp)` -> xchk_allocbt_rec() -> xchk_allocbt_xref() -> xchk_allocbt_xref_other() since sm_type is XFS_SCRUB_TYPE_BNOBT, pur is set to &sc->sa.cnt_cur. Kernel called xfs_alloc_get_rec() and returned -EFSCORRUPTED. Call chain: xfs_alloc_get_rec() -> xfs_btree_get_rec() -> xfs_btree_check_block() -> (XFS_IS_CORRUPT || XFS_TEST_ERROR), the former is false and the latter is true, return -EFSCORRUPTED. This should be caused by ioctl$XFS_IOC_ERROR_INJECTION I guess. Back to xchk_allocbt_xref_other(), after receiving -EFSCORRUPTED from xfs_alloc_get_rec(), kernel called xchk_should_check_xref(). In this function, *curpp (points to sc->sa.cnt_cur) is nullified. Back to xrep_revalidate_allocbt(), since sc->sa.cnt_cur has been nullified, it then triggered null-ptr-deref via xchk_allocbt() (second call) -> xchk_btree(). So. The bnobt revalidation failed on a cross-reference attempt, so we deleted the cntbt cursor, and then crashed when we tried to revalidate the cntbt. Therefore, check for a null cntbt cursor before that revalidation, and mark the repair incomplete. Also we can ignore the second tree entirely if the first tree was rebuilt but is already corrupt. Apply the same fix to xrep_revalidate_iallocbt because it has the same problem. Cc: r772577952@gmail.com Link: https://lore.kernel.org/linux-xfs/CANypQFYU5rRPkTy=iG5m1Lp4RWasSgrHXAh3p8YJojxV0X15dQ@mail.gmail.com/T/#m520c7835fad637eccf843c7936c200589427cc7e Cc: <stable@vger.kernel.org> # v6.8 Fixes: dbfbf3b ("xfs: repair inode btrees") Signed-off-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Tested-by: Jiaming Zhang <r772577952@gmail.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d6f3f7d commit d69de52

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

fs/xfs/scrub/alloc_repair.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,22 @@ xrep_revalidate_allocbt(
925925
if (error)
926926
goto out;
927927

928+
/*
929+
* If the bnobt is still corrupt, we've failed to repair the filesystem
930+
* and should just bail out.
931+
*
932+
* If the bnobt fails cross-examination with the cntbt, the scan will
933+
* free the cntbt cursor, so we need to mark the repair incomplete
934+
* and avoid walking off the end of the NULL cntbt cursor.
935+
*/
936+
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
937+
goto out;
938+
928939
sc->sm->sm_type = XFS_SCRUB_TYPE_CNTBT;
940+
if (!sc->sa.cnt_cur) {
941+
xchk_set_incomplete(sc);
942+
goto out;
943+
}
929944
error = xchk_allocbt(sc);
930945
out:
931946
sc->sm->sm_type = old_type;

fs/xfs/scrub/ialloc_repair.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,24 @@ xrep_revalidate_iallocbt(
873873
if (error)
874874
goto out;
875875

876-
if (xfs_has_finobt(sc->mp)) {
877-
sc->sm->sm_type = XFS_SCRUB_TYPE_FINOBT;
878-
error = xchk_iallocbt(sc);
876+
/*
877+
* If the inobt is still corrupt, we've failed to repair the filesystem
878+
* and should just bail out.
879+
*
880+
* If the inobt fails cross-examination with the finobt, the scan will
881+
* free the finobt cursor, so we need to mark the repair incomplete
882+
* and avoid walking off the end of the NULL finobt cursor.
883+
*/
884+
if (!xfs_has_finobt(sc->mp) ||
885+
(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
886+
goto out;
887+
888+
sc->sm->sm_type = XFS_SCRUB_TYPE_FINOBT;
889+
if (!sc->sa.fino_cur) {
890+
xchk_set_incomplete(sc);
891+
goto out;
879892
}
893+
error = xchk_iallocbt(sc);
880894

881895
out:
882896
sc->sm->sm_type = old_type;

0 commit comments

Comments
 (0)