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
  2026-12-04  8:57 ` Christoph Hellwig
  0 siblings, 1 reply; 5+ messages 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] 5+ messages in thread

* Re: [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free()
  2026-12-03 13:22 [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free() Zheng Shuhao
@ 2026-12-04  8:57 ` Christoph Hellwig
  2026-12-05  5:31   ` Zheng Shuhao
  0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2026-12-04  8:57 UTC (permalink / raw)
  To: Zheng Shuhao
  Cc: linux-xfs, linux-kernel, hch, cem, djwong, hemanth.selam,
	shinsj4653, syzbot+e7f24354da9f7915bc54

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.

On Thu, Dec 03, 2026 at 09:22:03PM +0800, Zheng Shuhao wrote:
> 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.

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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free()
  2026-12-04  8:57 ` Christoph Hellwig
@ 2026-12-05  5:31   ` Zheng Shuhao
  2026-12-05  7:03     ` Carlos Maiolino
  0 siblings, 1 reply; 5+ messages in thread
From: Zheng Shuhao @ 2026-12-05  5:31 UTC (permalink / raw)
  To: hch
  Cc: linux-xfs, linux-kernel, cem, djwong, hemanth.selam, shinsj4653,
	syzbot+e7f24354da9f7915bc54

> 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.

I was told that new versions of a patch should be posted in a new thread to avoid mess. Sorry :)

> So xfs_alloc_commit_autoreap won't have any effect and we can just
> remove it entirely.

I've gone through the code carefully.It seems that removing this call in
junkit really doesn't seem to have any noticeable side effects overall...

If the filesystem is already shut down, the remaining deferred items stay
attached to the transaction until the repair unwinds and the transaction is
canceled. If an EFI was already committed without an EFD, log recovery will
handle it.

If xfs_defer_finish_noroll() fails, it cancels the paused items before
returning, so the autoreap pointer can already be stale and must not be
unpaused. If the final transaction roll fails after xfs_defer_finish_noroll()
succeeds, the paused items remain attached to the new transaction; teardown
cancels them regardless of whether they are paused. In the successful case,
the reservations have already been removed from the list before execution
falls through to junkit, so its cleanup loop has no autoreap items to unpause.

I also found another route to junkit: xrep_newbt_free_extent() can return an
error. The reservation that returned the error has already been removed from
the list; any remaining deferred items are canceled when the transaction is
torn down.

I also ran some tests after removing this call. It looks like it hasn't
introduced any obvious problems, and the results are consistent with the
baseline. I reran the ten tests that had failed in the previous run using
fresh disk overlays and a fresh log device. xfs/310 passed after enabling
CONFIG_DM_SNAPSHOT and CONFIG_DM_DELAY. The other nine failures were also
present in the baseline and list-empty results: four network-dependent generic
tests, generic/794's EOF-gap mismatch, three xfsprogs manpage-output
mismatches, and the same reproducible-image mismatch in xfs/841. There were
no unexpected writeback or no-space errors.

The focused reproducer also completed without a KASAN/use-after-free report,
and xfs_repair -n exited successfully. The trace recorded one defer-finish
error and one transaction-roll error. This was a rerun of the ten failed
tests, not a new full -g auto run.

But is it appropriate to do so? It does change the original semantics to some degree — is this the kind of change I shouldn't be the one submitting?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free()
  2026-12-05  5:31   ` Zheng Shuhao
@ 2026-12-05  7:03     ` Carlos Maiolino
  2026-12-05 12:58       ` Zheng Shuhao
  0 siblings, 1 reply; 5+ messages in thread
From: Carlos Maiolino @ 2026-12-05  7:03 UTC (permalink / raw)
  To: Zheng Shuhao
  Cc: hch, linux-xfs, linux-kernel, djwong, hemanth.selam, shinsj4653,
	syzbot+e7f24354da9f7915bc54

On Sat, Dec 05, 2026 at 01:31:00PM +0800, Zheng Shuhao wrote:
> > 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.
> 
> I was told that new versions of a patch should be posted in a new thread to avoid mess. Sorry :)

This is not related to what Christoph is saying. You've got things upside
down.

Your "patch" obviously contains a reply to another thread, it is not an
expected format.

Whatever MUA you use, look at how it ends up being viewed:

https://lore.kernel.org/linux-xfs/179644866020.268.4885883217145755379@gmail.com/T/#t

To quote a small piece of your patch:

"
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
"

^ This is obvious a reply which very likely should not be in the commit
message. This has nothing to do with sending new patch versions as
individual threads. Whatever you did, you got the tooling wrong.

Not sending patches in the same thread basically means using
git-send-email without filling the In-Reply-To fields.

If you're having trouble with the tooling I'm happy to help, but it has
nothing to do what I told you, i.e. to not send patch versions in reply
to old versions.


> 
> > So xfs_alloc_commit_autoreap won't have any effect and we can just
> > remove it entirely.
> 
> I've gone through the code carefully.It seems that removing this call in
> junkit really doesn't seem to have any noticeable side effects overall...
> 
> If the filesystem is already shut down, the remaining deferred items stay
> attached to the transaction until the repair unwinds and the transaction is
> canceled. If an EFI was already committed without an EFD, log recovery will
> handle it.
> 
> If xfs_defer_finish_noroll() fails, it cancels the paused items before
> returning, so the autoreap pointer can already be stale and must not be
> unpaused. If the final transaction roll fails after xfs_defer_finish_noroll()
> succeeds, the paused items remain attached to the new transaction; teardown
> cancels them regardless of whether they are paused. In the successful case,
> the reservations have already been removed from the list before execution
> falls through to junkit, so its cleanup loop has no autoreap items to unpause.
> 
> I also found another route to junkit: xrep_newbt_free_extent() can return an
> error. The reservation that returned the error has already been removed from
> the list; any remaining deferred items are canceled when the transaction is
> torn down.
> 
> I also ran some tests after removing this call. It looks like it hasn't
> introduced any obvious problems, and the results are consistent with the
> baseline. I reran the ten tests that had failed in the previous run using
> fresh disk overlays and a fresh log device. xfs/310 passed after enabling
> CONFIG_DM_SNAPSHOT and CONFIG_DM_DELAY. The other nine failures were also
> present in the baseline and list-empty results: four network-dependent generic
> tests, generic/794's EOF-gap mismatch, three xfsprogs manpage-output
> mismatches, and the same reproducible-image mismatch in xfs/841. There were
> no unexpected writeback or no-space errors.
> 
> The focused reproducer also completed without a KASAN/use-after-free report,
> and xfs_repair -n exited successfully. The trace recorded one defer-finish
> error and one transaction-roll error. This was a rerun of the ten failed
> tests, not a new full -g auto run.
> 
> But is it appropriate to do so? It does change the original semantics to some degree — is this the kind of change I shouldn't be the one submitting?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] xfs: fix slab-use-after-free in xrep_newbt_free()
  2026-12-05  7:03     ` Carlos Maiolino
@ 2026-12-05 12:58       ` Zheng Shuhao
  0 siblings, 0 replies; 5+ messages in thread
From: Zheng Shuhao @ 2026-12-05 12:58 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: hch, linux-xfs, linux-kernel, djwong, hemanth.selam, shinsj4653,
	syzbot+e7f24354da9f7915bc54

Sorry...It was my fault.
I will be careful.

Carlos Maiolino <cem@kernel.org> 于2026年12月5日周六 15:03写道:
>
> On Sat, Dec 05, 2026 at 01:31:00PM +0800, Zheng Shuhao wrote:
> > > 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.
> >
> > I was told that new versions of a patch should be posted in a new thread to avoid mess. Sorry :)
>
> This is not related to what Christoph is saying. You've got things upside
> down.
>
> Your "patch" obviously contains a reply to another thread, it is not an
> expected format.
>
> Whatever MUA you use, look at how it ends up being viewed:
>
> https://lore.kernel.org/linux-xfs/179644866020.268.4885883217145755379@gmail.com/T/#t
>
> To quote a small piece of your patch:
>
> "
> 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
> "
>
> ^ This is obvious a reply which very likely should not be in the commit
> message. This has nothing to do with sending new patch versions as
> individual threads. Whatever you did, you got the tooling wrong.
>
> Not sending patches in the same thread basically means using
> git-send-email without filling the In-Reply-To fields.
>
> If you're having trouble with the tooling I'm happy to help, but it has
> nothing to do what I told you, i.e. to not send patch versions in reply
> to old versions.
>
>
> >
> > > So xfs_alloc_commit_autoreap won't have any effect and we can just
> > > remove it entirely.
> >
> > I've gone through the code carefully.It seems that removing this call in
> > junkit really doesn't seem to have any noticeable side effects overall...
> >
> > If the filesystem is already shut down, the remaining deferred items stay
> > attached to the transaction until the repair unwinds and the transaction is
> > canceled. If an EFI was already committed without an EFD, log recovery will
> > handle it.
> >
> > If xfs_defer_finish_noroll() fails, it cancels the paused items before
> > returning, so the autoreap pointer can already be stale and must not be
> > unpaused. If the final transaction roll fails after xfs_defer_finish_noroll()
> > succeeds, the paused items remain attached to the new transaction; teardown
> > cancels them regardless of whether they are paused. In the successful case,
> > the reservations have already been removed from the list before execution
> > falls through to junkit, so its cleanup loop has no autoreap items to unpause.
> >
> > I also found another route to junkit: xrep_newbt_free_extent() can return an
> > error. The reservation that returned the error has already been removed from
> > the list; any remaining deferred items are canceled when the transaction is
> > torn down.
> >
> > I also ran some tests after removing this call. It looks like it hasn't
> > introduced any obvious problems, and the results are consistent with the
> > baseline. I reran the ten tests that had failed in the previous run using
> > fresh disk overlays and a fresh log device. xfs/310 passed after enabling
> > CONFIG_DM_SNAPSHOT and CONFIG_DM_DELAY. The other nine failures were also
> > present in the baseline and list-empty results: four network-dependent generic
> > tests, generic/794's EOF-gap mismatch, three xfsprogs manpage-output
> > mismatches, and the same reproducible-image mismatch in xfs/841. There were
> > no unexpected writeback or no-space errors.
> >
> > The focused reproducer also completed without a KASAN/use-after-free report,
> > and xfs_repair -n exited successfully. The trace recorded one defer-finish
> > error and one transaction-roll error. This was a rerun of the ten failed
> > tests, not a new full -g auto run.
> >
> > But is it appropriate to do so? It does change the original semantics to some degree — is this the kind of change I shouldn't be the one submitting?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-05 12:58 UTC | newest]

Thread overview: 5+ messages (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
2026-12-04  8:57 ` Christoph Hellwig
2026-12-05  5:31   ` Zheng Shuhao
2026-12-05  7:03     ` Carlos Maiolino
2026-12-05 12:58       ` Zheng Shuhao

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