Skip to contentSkip to conversation

btrfs: fix transaction use-after-free in raid stripe insertion

PATCH Superseded Shuangpeng Bai 2 participants 6 messages Aug 17 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix transaction use-after-free in raid stripe insertion

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.

btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.

The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.

Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")
---
 fs/btrfs/raid-stripe-tree.c | 1 -
 1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
 	stripe_extent = kzalloc(item_size, GFP_NOFS);
 	if (unlikely(!stripe_extent)) {
 		btrfs_abort_transaction(trans, -ENOMEM);
-		btrfs_end_transaction(trans);
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 14 lines · Shuangpeng Bai

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.

btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.

The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.

Quoted 5 lines

Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")

Please disclose LLM usage.

Quoted 17 lines
---
  fs/btrfs/raid-stripe-tree.c | 1 -
  1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
  	stripe_extent = kzalloc(item_size, GFP_NOFS);
  	if (unlikely(!stripe_extent)) {
  		btrfs_abort_transaction(trans, -ENOMEM);
-		btrfs_end_transaction(trans);
  		return -ENOMEM;
  	}
 
author
#2 lore raw
Quoted 23 lines · Qu Wenruo
Quoted 13 lines · Shuangpeng Bai

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.
btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.
The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.
Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Quoted 2 lines
Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")
Quoted 2 lines

Please disclose LLM usage.

Hi Qu,

Thanks for pointing this out.

I used Codex to help generate the patch. I had confirmed the bug with KASAN, and I also verified the proposed fix with my reproducer. With the patch applied, the reproducer no longer triggers the KASAN report.

I will send a v2 with the appropriate LLM disclosure tag.

Thanks,
Shuangpeng

Quoted 17 lines
Quoted 16 lines
---
 fs/btrfs/raid-stripe-tree.c | 1 -
 1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
  stripe_extent = kzalloc(item_size, GFP_NOFS);
  if (unlikely(!stripe_extent)) {
  btrfs_abort_transaction(trans, -ENOMEM);
- btrfs_end_transaction(trans);
  return -ENOMEM;
  }
 
#3 lore raw
Quoted 26 lines · Shuangpeng
Quoted 23 lines · Qu Wenruo
Quoted 13 lines · Shuangpeng Bai

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.
btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.
The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.
Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Quoted 2 lines
Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")
Quoted 2 lines

Please disclose LLM usage.

Quoted 7 lines

Hi Qu,

Thanks for pointing this out.

I used Codex to help generate the patch. I had confirmed the bug with KASAN, and I also verified the proposed fix with my reproducer. With the patch applied, the reproducer no longer triggers the KASAN report.

And a full fstests run to prevent regression.

Quoted 24 lines

I will send a v2 with the appropriate LLM disclosure tag.

Thanks,
Shuangpeng

Quoted 17 lines
Quoted 16 lines
---
  fs/btrfs/raid-stripe-tree.c | 1 -
  1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
   stripe_extent = kzalloc(item_size, GFP_NOFS);
   if (unlikely(!stripe_extent)) {
   btrfs_abort_transaction(trans, -ENOMEM);
- btrfs_end_transaction(trans);
   return -ENOMEM;
   }
 
#4 lore raw

PATCH v2 1/1 btrfs: fix transaction use-after-free in raid stripe insertion

In reply to Shuangpeng Bai

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.

btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.

The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.

Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")
Assisted-by Codex:GPT-5

---
Changes in v2:
- Add the Assisted-by tag to disclose LLM usage.

v1: https://lore.kernel.org/r/[email protected]

 fs/btrfs/raid-stripe-tree.c | 1 -
 1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
 	stripe_extent = kzalloc(item_size, GFP_NOFS);
 	if (unlikely(!stripe_extent)) {
 		btrfs_abort_transaction(trans, -ENOMEM);
-		btrfs_end_transaction(trans);
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#5 lore raw
Quoted 14 lines · Shuangpeng Bai

If allocation of a RAID stripe extent fails, btrfs_insert_one_raid_extent() aborts and ends the transaction before returning -ENOMEM.

btrfs_finish_one_ordered(), the production caller through btrfs_insert_raid_extent(), still owns the transaction handle. It handles the error by aborting the transaction and then reaches the common exit path, which ends the transaction again.

The premature end can free the handle and drop its transaction reference. Transaction cleanup can then free the transaction before the caller's second abort accesses the handle and transaction, resulting in use-after-free.

Quoted 7 lines

Keep the abort at the failure site, but let the caller's common exit path end the transaction once, after it has finished using both objects.

Fixes 02c372e1f016 ("btrfs: add support for inserting raid stripe extents")
Assisted-by Codex:GPT-5
Quoted 21 lines

---
Changes in v2:
- Add the Assisted-by tag to disclose LLM usage.

v1: https://lore.kernel.org/r/[email protected]

  fs/btrfs/raid-stripe-tree.c | 1 -
  1 file changed, 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -1
index b210371ce91e..89e259a47d8d 100644
@@ -337,7 +337,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
  	stripe_extent = kzalloc(item_size, GFP_NOFS);
  	if (unlikely(!stripe_extent)) {
  		btrfs_abort_transaction(trans, -ENOMEM);
-		btrfs_end_transaction(trans);
  		return -ENOMEM;
  	}