[PATCH 1/3] btrfs-progs: fix the incorrect buffer size for super block

Qu Wenruo
2025-02-26T03:59:52Z

Inside the function btrfs_add_to_fsid(), we allocate a buffer to write
the superblock to the 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>
---
common/device-scan.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/device-scan.c b/common/device-scan.c
index a34d86652f06..7d7d67fb5b71 100644
--- a/common/device-scan.c
+++ b/common/device-scan.c
@@ -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;
--
2.48.1