Skip to content

Commit

Permalink
btrfs: fix possible memory leak in btrfs_get_dev_args_from_path()
Browse files Browse the repository at this point in the history
In btrfs_get_dev_args_from_path(), btrfs_get_bdev_and_sb() can fail if
the path is invalid. In this case, btrfs_get_dev_args_from_path()
returns directly without freeing args->uuid and args->fsid allocated
before, which causes memory leak.

To fix these possible leaks, when btrfs_get_bdev_and_sb() fails,
btrfs_put_dev_args_from_path() is called to clean up the memory.

Reported-by: TOTE Robot <[email protected]>
Fixes: faa775c ("btrfs: add a btrfs_get_dev_args_from_path helper")
CC: [email protected] # 5.16
Reviewed-by: Boris Burkov <[email protected]>
Signed-off-by: Zixuan Fu <[email protected]>
Reviewed-by: David Sterba <[email protected]>
Signed-off-by: David Sterba <[email protected]>
  • Loading branch information
r33s3n6 authored and kdave committed Aug 22, 2022
1 parent b511112 commit 9ea0106
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion fs/btrfs/volumes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2345,8 +2345,11 @@ int btrfs_get_dev_args_from_path(struct btrfs_fs_info *fs_info,

ret = btrfs_get_bdev_and_sb(path, FMODE_READ, fs_info->bdev_holder, 0,
&bdev, &disk_super);
if (ret)
if (ret) {
btrfs_put_dev_args_from_path(args);
return ret;
}

args->devid = btrfs_stack_device_id(&disk_super->dev_item);
memcpy(args->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE);
if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Expand Down

0 comments on commit 9ea0106

Please sign in to comment.