xfs: fix slab-use-after-free in xrep_newbt_free()
PATCH v3 1/1 xfs: fix slab-use-after-free in xrep_newbt_free()
Quoted 5 lines · Christoph Hellwig
Using the control flow instead of global state is usually preferable. I.e. check which path can free the autoreap information and treat them differently using a goto or similar. I think the only relevant call is
xrep_newbt_freein thexrep_newbt_freeloop, but I'm not an expert on this code, so please double check.
I considered changing the control flow, but found that this would make error-path handling more complex because the defer-finish failure paths would need to be handled separately.
I therefore kept the existing control flow and used a standard list traversal to check whether the exact deferred item is still pending before unpausing it.
I compared this with checking:
if (!list_empty(&sc->tp->t_dfops))
before calling xfs_alloc_commit_autoreap() in xrep_newbt_free(). Both approaches produced the same results with the reproducer and relevant xfstests. The current patch is more robust because it checks the exact item referenced by the autoreap reservation, rather than only checking whether the transaction has any pending deferred items.
An autoreap reservation can retain a pointer to a deferred item after a failed defer finish cancels that item. This patch checks that the exact item is still on the transaction's pending list before unpausing it.
Testing:
- The focused reproducer triggers the reported KASAN use-after-free on the baseline kernel. No KASAN use-after-free was observed with this patch in the recorded runs.
- I reran all 150 test IDs extracted from the earlier exact-dfp xfstests run using a corrected 4 GiB guest root filesystem and corrected device-mapper setup. The results were 144 passed, 5 failures, and 1 skipped test. All XFS cases passed, and no patch-specific failure was observed.
- The five failures were also present with the baseline and list_empty()
versions: generic/131, generic/571, generic/786, and generic/787 failed because guest networking was unavailable; generic/794 found the same EOF-gap issue on the baseline. generic/470 was skipped because the QEMU test device does not support DAX.
- A five-run 512 MiB benchmark averaged 45.214 seconds on the baseline, 44.849 seconds with the list_empty() variant, and 45.439 seconds with this lookup. The measurements showed no meaningful performance difference.
---
v3:
- Keep the existing control flow and check whether the exact autoreap item is still on the transaction's pending deferred list.
- Compare the exact-item lookup with the list_empty() approach.
fs/xfs/libxfs/xfs_alloc.c +12 -2
@@ -2774,8 +2774,18 @@ xfs_alloc_commit_autoreap(struct xfs_trans *tp, struct xfs_alloc_autoreap *aarp) { -if (aarp->dfp) -xfs_defer_item_unpause (tp, aarp->dfp); + struct xfs_defer_pending *dfp; + +if (!aarp->dfp) +return ; + +/* The deferred item might have been canceled by a failed defer finish. */ +list_for_each_entry (dfp, &tp->t_dfops, dfp_list) { +if (dfp == aarp->dfp) { +xfs_defer_item_unpause (tp, dfp); +return ; + } + } }/*
The email format is a bit odd. It obviously is a reply continuing the previous discussion, but it breaks threading and adds a new subject.
Quoted 7 lines · Zheng Shuhao
Quoted 5 lines · Christoph Hellwig
Using the control flow instead of global state is usually preferable. I.e. check which path can free the autoreap information and treat them differently using a goto or similar. I think the only relevant call is
xrep_newbt_freein thexrep_newbt_freeloop, but I'm not an expert on this code, so please double check.
Quoted 7 lines
I considered changing the control flow, but found that this would make error-path handling more complex because the defer-finish failure paths would need to be handled separately.
I therefore kept the existing control flow and used a standard list traversal to check whether the exact deferred item is still pending before unpausing it.
This now pokes into defer_ops implementation details. I spent some time looking at this, but AFAICS the call to xfs_alloc_commit_autoreap in xrep_newbt_free is always superfluous. The only way we can end up in that error path is with a file system either already shut down, or about to shut down due to corruption.
So xfs_alloc_commit_autoreap won't have any effect and we can just remove it entirely.