Linux XFS filesystem development
 help / color / mirror / Atom feed
* [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free()
@ 2026-12-03 13:22 Zheng Shuhao
  0 siblings, 0 replies; only message in thread
From: Zheng Shuhao @ 2026-12-03 13:22 UTC (permalink / raw)
  To: linux-xfs
  Cc: linux-kernel, hch, cem, djwong, hemanth.selam, shinsj4653,
	syzbot+e7f24354da9f7915bc54

On Wed, Dec 2, 2026 at 07:28:34AM +0000, Christoph Hellwig wrote:
> 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_free in the xrep_newbt_free loop, 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.

Fixes: 6bb9ea8ecd2c ("xfs: log EFIs for all btree blocks being used to stage a btree")
Reported-by: syzbot+e7f24354da9f7915bc54@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e7f24354da9f7915bc54
Cc: stable@vger.kernel.org
Signed-off-by: Zheng Shuhao <viennaoffh@gmail.com>
---
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.

diff --git a/fs/xfs/libxfs/xfs_alloc.c b/fs/xfs/libxfs/xfs_alloc.c
index f762dcce8d133..74c86c48d7fbf 100644
--- a/fs/xfs/libxfs/xfs_alloc.c
+++ b/fs/xfs/libxfs/xfs_alloc.c
@@ -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;
+		}
+	}
 }
 
 /*

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 13:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:22 [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free() Zheng Shuhao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox