btrfs-progs: fix the incorrect buffer size for super block structure

Inside the function btrfs_add_to_fsid(), we allocate a buffer to write
the superblock to disk.

However the buffer size is based on block size, which can cause two
problems:

- 2K block size
  The block size is too small for the super block, and we will write
  beyond the buffer and corrupt the memory.

- 16/64K block size
  The block size will be larger than super block size, this will not
  cause any problem but waste some memory.

Fix the bug by using BTRFS_SUPER_INFO_SIZE as the correct buffer size.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2025-02-26 14:29:15 +10:30 committed by David Sterba
parent 17b49b9dbd
commit 84aa7cc830

View File

@ -148,7 +148,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
if (!device)
return -ENOMEM;
buf = calloc(1, sectorsize);
buf = calloc(1, BTRFS_SUPER_INFO_SIZE);
if (!buf) {
ret = -ENOMEM;
goto out;