* [PATCH v2] btrfs: fix double free in create_space_info() error path
@ 2026-04-01 10:56 Guangshuo Li
2026-04-01 22:10 ` Qu Wenruo
0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-04-01 10:56 UTC (permalink / raw)
To: Chris Mason, David Sterba, Qu Wenruo, Jiasheng Jiang, linux-btrfs,
linux-kernel
Cc: Guangshuo Li, stable
When kobject_init_and_add() fails, the call chain is:
create_space_info()
-> btrfs_sysfs_add_space_info_type()
-> kobject_init_and_add()
-> failure
-> kobject_put(&space_info->kobj)
-> space_info_release()
-> kfree(space_info)
Then control returns to create_space_info():
btrfs_sysfs_add_space_info_type() returns error
-> goto out_free
-> kfree(space_info)
This causes a double free.
Keep the direct kfree(space_info) for the earlier failure path, but
after btrfs_sysfs_add_space_info_type() has called kobject_put(), let
the kobject release callback handle the cleanup.
Fixes: a11224a016d6d ("btrfs: fix memory leaks in create_space_info() error paths")
Cc: stable@vger.kernel.org
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
v2:
- add the full failure path to the changelog
fs/btrfs/space-info.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index 3f08e450f796..d7176eb2fcbf 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -311,7 +311,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)
ret = btrfs_sysfs_add_space_info_type(space_info);
if (ret)
- goto out_free;
+ return ret;
list_add(&space_info->list, &info->space_info);
if (flags & BTRFS_BLOCK_GROUP_DATA)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] btrfs: fix double free in create_space_info() error path
2026-04-01 10:56 [PATCH v2] btrfs: fix double free in create_space_info() error path Guangshuo Li
@ 2026-04-01 22:10 ` Qu Wenruo
0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2026-04-01 22:10 UTC (permalink / raw)
To: Guangshuo Li, Chris Mason, David Sterba, Jiasheng Jiang,
linux-btrfs, linux-kernel
Cc: stable
在 2026/4/1 21:26, Guangshuo Li 写道:
> When kobject_init_and_add() fails, the call chain is:
>
> create_space_info()
> -> btrfs_sysfs_add_space_info_type()
> -> kobject_init_and_add()
> -> failure
> -> kobject_put(&space_info->kobj)
> -> space_info_release()
> -> kfree(space_info)
>
> Then control returns to create_space_info():
>
> btrfs_sysfs_add_space_info_type() returns error
> -> goto out_free
> -> kfree(space_info)
>
> This causes a double free.
>
> Keep the direct kfree(space_info) for the earlier failure path, but
> after btrfs_sysfs_add_space_info_type() has called kobject_put(), let
> the kobject release callback handle the cleanup.
>
> Fixes: a11224a016d6d ("btrfs: fix memory leaks in create_space_info() error paths")
> Cc: stable@vger.kernel.org
> Reviewed-by: Qu Wenruo <wqu@suse.com>
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
> ---
> v2:
> - add the full failure path to the changelog
>
> fs/btrfs/space-info.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
> index 3f08e450f796..d7176eb2fcbf 100644
> --- a/fs/btrfs/space-info.c
> +++ b/fs/btrfs/space-info.c
> @@ -311,7 +311,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)
>
> ret = btrfs_sysfs_add_space_info_type(space_info);
> if (ret)
> - goto out_free;
> + return ret;
>
> list_add(&space_info->list, &info->space_info);
> if (flags & BTRFS_BLOCK_GROUP_DATA)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-04-01 22:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-01 10:56 [PATCH v2] btrfs: fix double free in create_space_info() error path Guangshuo Li
2026-04-01 22:10 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox