[PATCH 4/5] hfsplus: reject an allocation bitmap that does not cover the volume
From: Viacheslav Dubeyko
Date: Thu Nov 19 2026 - 18:41:51 EST
The allocation file is a bitmap with one bit per allocation block, so
it has to be at least DIV_ROUND_UP(total_blocks, 8) bytes long. This is
not checked at mount. hfsplus_block_allocate() and hfsplus_block_free()
look up any block below total_blocks in the bitmap, so with a shorter
allocation file they read and modify bitmap bits beyond its end, which
are never written back.
The only guard is in hfsplus_file_extend(), which fails with -ENOSPC
("extend alloc file!") once the blocks in use approach the bitmap size,
and hfsplus_block_free() has none. On a 64M volume created with
"mkfs.hfsplus -b 4096" whose allocation file size in the volume header
was changed from 4096 to 1024 bytes (fsck.hfsplus reports the volume as
OK), writing two 24M files fails with -ENOSPC at half of the volume,
and after unmount fsck.hfsplus reports:
Volume bitmap needs minor repair for under-allocation
Fail the mount if the allocation file is too small for the volume.
Tested on a KASAN and lockdep kernel: the volume above is rejected at
mount with "allocation file size 1024 is too small for 16384 blocks",
the same volume with an allocation file of 2048 bytes (the minimum) or
4096 bytes mounts.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
cc: John Paul Adrian Glaubitz <glaubitz@xxxxxxxxxxxxxxxxxxx>
cc: Yangtao Li <frank.li@xxxxxxxx>
cc: linux-fsdevel@xxxxxxxxxxxxxxx
Signed-off-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
---
fs/hfsplus/super.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c
index d23594d12064..6e4068e53b4f 100644
--- a/fs/hfsplus/super.c
+++ b/fs/hfsplus/super.c
@@ -777,6 +777,19 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc)
}
sbi->alloc_file.inode = inode;
+ /*
+ * The allocation bitmap has one bit per allocation block. The
+ * bitmap code looks up any block below total_blocks in it, so it
+ * must cover all of them.
+ */
+ if (inode->i_size < DIV_ROUND_UP((u64)sbi->total_blocks,
+ BITS_PER_BYTE)) {
+ pr_err("allocation file size %lld is too small for %u blocks\n",
+ inode->i_size, sbi->total_blocks);
+ err = -EIO;
+ goto out_put_alloc_file;
+ }
+
err = hfsplus_ext_load_alloc_file(sb);
if (err)
goto out_put_alloc_file;
--
2.53.0