[PATCH 1/5] hfsplus: reject special files larger than their allocation at mount
From: Viacheslav Dubeyko
Date: Thu Nov 19 2026 - 18:41:29 EST
The volume header describes each special file (extents, catalog,
allocation, startup and attributes file) with a fork that holds its
logical size, its number of allocated blocks and its first eight
extents. hfsplus_inode_read_fork() takes the logical size as is, and
fs_blocks is derived from it, so a corrupted header can make a special
file look larger than the blocks allocated to it.
Syzkaller found an image whose allocation file has a logical size of
0x800000000800 bytes but only one allocated block. Deleting a file on
that volume frees its blocks, and hfsplus_block_free() reads the
bitmap page through the allocation file while it holds the deleted
file's extents_lock and sbi->alloc_mutex. Page 0 of the bitmap spans
fs blocks beyond the first allocation block. As they are below the
inflated fs_blocks, hfsplus_get_block() does not treat them as beyond
EOF: it takes the allocation file's extents_lock and searches the
extents B-tree, which has no record for them. Lockdep reports
recursive locking on extents_lock and the block free fails with -EIO:
WARNING: possible recursive locking detected
rm/922 is trying to acquire lock:
(&HFSPLUS_I(inode)->extents_lock){+.+.}-{4:4}, at: hfsplus_get_block
but task is already holding lock:
(&HFSPLUS_I(inode)->extents_lock){+.+.}-{4:4}, at: hfsplus_file_truncate
...
hfsplus_get_block
block_read_full_folio
...
read_cache_page
hfsplus_block_free
hfsplus_free_extents
hfsplus_file_truncate
hfsplus_delete_inode
hfsplus_unlink
hfsplus: unable to mark blocks free: error -5
hfsplus: can't free extent: start 74, count 1
A logical size beyond the allocated blocks is invalid on HFS+, so
check it when the special file inodes are read from the volume header
and fail the mount if it is exceeded. The error is reported as:
hfsplus: special file 6: size 140737488357376 exceeds allocated size 2048
hfsplus: failed to load allocation file
Tested on a KASAN and lockdep kernel: the syzkaller image and images
with the logical size of the allocation, extents, catalog or
attributes file set one byte beyond its allocation are rejected, the
image with a consistent size mounts and its files can be deleted, and
generic/001, 002 and 013 pass on freshly created volumes.
The analysis, the fix and this changelog were drafted with the help of
an AI coding assistant (Claude Opus 5.5) and reviewed by the author.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Zheng Zheng <fengaojigui88@xxxxxxxxx>
Closes: https://lore.kernel.org/linux-fsdevel/CAO-4RZ1a46EoJY7x6euiS+7aTOb-T9X62E9nGyToRcyvKU+eHg@xxxxxxxxxxxxxx/
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 | 38 +++++++++++++++++++++++++++++++++-----
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c
index f3283e9b42c8..33594e1f8367 100644
--- a/fs/hfsplus/super.c
+++ b/fs/hfsplus/super.c
@@ -26,34 +26,62 @@ static void hfsplus_free_inode(struct inode *inode);
#include "hfsplus_fs.h"
#include "xattr.h"
+/*
+ * The logical size of a special file must fit within its allocated
+ * blocks. Otherwise its page cache would map blocks past the end of the
+ * extent records, which the code accessing these files (e.g. the bitmap
+ * code holding alloc_mutex) does not expect.
+ */
+static int hfsplus_system_read_fork(struct inode *inode,
+ struct hfsplus_fork_raw *fork)
+{
+ struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
+ u64 size = be64_to_cpu(fork->total_size);
+ u32 total_blocks = be32_to_cpu(fork->total_blocks);
+ u64 alloc_size = (u64)total_blocks << sbi->alloc_blksz_shift;
+
+ if (size > alloc_size) {
+ pr_err("special file %llu: size %llu exceeds allocated size %llu\n",
+ inode->i_ino, size, alloc_size);
+ return -EIO;
+ }
+
+ hfsplus_inode_read_fork(inode, fork);
+ return 0;
+}
+
static int hfsplus_system_read_inode(struct inode *inode)
{
struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
+ int err;
switch (inode->i_ino) {
case HFSPLUS_EXT_CNID:
- hfsplus_inode_read_fork(inode, &vhdr->ext_file);
+ err = hfsplus_system_read_fork(inode, &vhdr->ext_file);
inode->i_mapping->a_ops = &hfsplus_btree_aops;
break;
case HFSPLUS_CAT_CNID:
- hfsplus_inode_read_fork(inode, &vhdr->cat_file);
+ err = hfsplus_system_read_fork(inode, &vhdr->cat_file);
inode->i_mapping->a_ops = &hfsplus_btree_aops;
break;
case HFSPLUS_ALLOC_CNID:
- hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
+ err = hfsplus_system_read_fork(inode, &vhdr->alloc_file);
inode->i_mapping->a_ops = &hfsplus_aops;
break;
case HFSPLUS_START_CNID:
- hfsplus_inode_read_fork(inode, &vhdr->start_file);
+ err = hfsplus_system_read_fork(inode, &vhdr->start_file);
break;
case HFSPLUS_ATTR_CNID:
- hfsplus_inode_read_fork(inode, &vhdr->attr_file);
+ err = hfsplus_system_read_fork(inode, &vhdr->attr_file);
inode->i_mapping->a_ops = &hfsplus_btree_aops;
break;
default:
return -EIO;
}
+ if (err)
+ return err;
+
/*
* Assign a dummy file type, for may_open() requires that
* an inode has a valid file type.
--
2.53.0