diff mbox series

[next] btrfs: Accessing head_ref within delayed_refs lock

Message ID tencent_0EBF9E731B704B091B022578BA9EBB8E3308@qq.com (mailing list archive)
State New, archived
Headers show
Series [next] btrfs: Accessing head_ref within delayed_refs lock | expand

Commit Message

Edward Adam Davis Oct. 15, 2024, 12:26 p.m. UTC
This is because the thread routine btrfs_work_helper released head_def after
exiting delayed_refs->lock in add_delayed_ref.
Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
outside the delayed_refs->lock.

Move head_ref->bytenr into the protection range of delayed_refs->lock 
to avoid uaf in add_delayed_ref.

Fixes: a3aad8f4f5d9 ("btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record")
Reported-and-tested-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 fs/btrfs/delayed-ref.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Filipe Manana Oct. 15, 2024, 12:49 p.m. UTC | #1
On Tue, Oct 15, 2024 at 1:40 PM Edward Adam Davis <eadavis@qq.com> wrote:
>
> This is because the thread routine btrfs_work_helper released head_def after
> exiting delayed_refs->lock in add_delayed_ref.

This should be explained a lot better. Starting the changelog with
"This is because..." is odd. It should explain why the head reference
was freed (because delayed references were run).

> Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
> outside the delayed_refs->lock.
>
> Move head_ref->bytenr into the protection range of delayed_refs->lock
> to avoid uaf in add_delayed_ref.


This was already fixed yesterday, in a simpler way and with an
explanation of what's going on to trigger the use-after-free:

https://lore.kernel.org/linux-btrfs/02fc507b62b19be2348fc08de8b13bd7af1a440e.1728922973.git.fdmanana@suse.com/

Thanks.


>
> Fixes: a3aad8f4f5d9 ("btrfs: qgroups: remove bytenr field from struct btrfs_qgroup_extent_record")
> Reported-and-tested-by: syzbot+c3a3a153f0190dca5be9@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=c3a3a153f0190dca5be9
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
>  fs/btrfs/delayed-ref.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
> index 13c2e00d1270..f50fc05847a1 100644
> --- a/fs/btrfs/delayed-ref.c
> +++ b/fs/btrfs/delayed-ref.c
> @@ -1012,6 +1012,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>         int action = generic_ref->action;
>         bool merged;
>         int ret;
> +       u64 bytenr;
>
>         node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
>         if (!node)
> @@ -1056,6 +1057,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>                 goto free_record;
>         }
>         head_ref = new_head_ref;
> +       bytenr = head_ref->bytenr;
>
>         merged = insert_delayed_ref(trans, head_ref, node);
>         spin_unlock(&delayed_refs->lock);
> @@ -1074,7 +1076,7 @@ static int add_delayed_ref(struct btrfs_trans_handle *trans,
>                 kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
>
>         if (qrecord_inserted)
> -               return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
> +               return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
>         return 0;
>
>  free_record:
> --
> 2.43.0
>
>
David Sterba Oct. 15, 2024, 4:38 p.m. UTC | #2
On Tue, Oct 15, 2024 at 01:49:04PM +0100, Filipe Manana wrote:
> On Tue, Oct 15, 2024 at 1:40 PM Edward Adam Davis <eadavis@qq.com> wrote:
> >
> > This is because the thread routine btrfs_work_helper released head_def after
> > exiting delayed_refs->lock in add_delayed_ref.
> 
> This should be explained a lot better. Starting the changelog with
> "This is because..." is odd. It should explain why the head reference
> was freed (because delayed references were run).
> 
> > Causing add_delayed_ref to encounter uaf when accessing head_def->bytenr
> > outside the delayed_refs->lock.
> >
> > Move head_ref->bytenr into the protection range of delayed_refs->lock
> > to avoid uaf in add_delayed_ref.
> 
> 
> This was already fixed yesterday, in a simpler way and with an
> explanation of what's going on to trigger the use-after-free:
> 
> https://lore.kernel.org/linux-btrfs/02fc507b62b19be2348fc08de8b13bd7af1a440e.1728922973.git.fdmanana@suse.com/

This will be in the upcoming linux-next, so we'll not get the syzbot and
build reports anymore.
diff mbox series

Patch

diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 13c2e00d1270..f50fc05847a1 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -1012,6 +1012,7 @@  static int add_delayed_ref(struct btrfs_trans_handle *trans,
 	int action = generic_ref->action;
 	bool merged;
 	int ret;
+	u64 bytenr;
 
 	node = kmem_cache_alloc(btrfs_delayed_ref_node_cachep, GFP_NOFS);
 	if (!node)
@@ -1056,6 +1057,7 @@  static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		goto free_record;
 	}
 	head_ref = new_head_ref;
+	bytenr = head_ref->bytenr;
 
 	merged = insert_delayed_ref(trans, head_ref, node);
 	spin_unlock(&delayed_refs->lock);
@@ -1074,7 +1076,7 @@  static int add_delayed_ref(struct btrfs_trans_handle *trans,
 		kmem_cache_free(btrfs_delayed_ref_node_cachep, node);
 
 	if (qrecord_inserted)
-		return btrfs_qgroup_trace_extent_post(trans, record, head_ref->bytenr);
+		return btrfs_qgroup_trace_extent_post(trans, record, bytenr);
 	return 0;
 
 free_record: