Re: [btrfs?] KASAN: slab-use-after-free Read in add_delayed_ref

From: Edward Adam Davis
Date: Tue Oct 15 2024 - 06:01:18 EST


using delayed_refs->lock to sync btrfs_work_helper and add_delayed_ref

#syz test

diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 13c2e00d1270..cb57e65c9872 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -1058,7 +1058,6 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
head_ref = new_head_ref;

merged = insert_delayed_ref(trans, head_ref, node);
- spin_unlock(&delayed_refs->lock);

/*
* Need to update the delayed_refs_rsv with any changes we may have
@@ -1073,8 +1072,12 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
if (merged)
kmem_cache_free(btrfs_delayed_ref_node_cachep, node);

- if (qrecord_inserted)
- return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+ if (qrecord_inserted) {
+ ret = btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+ spin_unlock(&delayed_refs->lock);
+ return ret;
+ }
+ spin_unlock(&delayed_refs->lock);
return 0;

free_record: