From: Viacheslav Dubeyko <slava@dubeyko.com>
To: glaubitz@physik.fu-berlin.de, frank.li@vivo.com
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
vdubeyko@coreweave.com, Viacheslav Dubeyko <slava@dubeyko.com>,
Zheng Zheng <fengaojigui88@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount
Date: Thu, 19 Nov 2026 15:41:08 -0800 [thread overview]
Message-ID: <20261119234112.720963-2-slava@dubeyko.com> (raw)
In-Reply-To: <20261119234112.720963-1-slava@dubeyko.com>
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@gmail.com>
Closes: https://lore.kernel.org/linux-fsdevel/CAO-4RZ1a46EoJY7x6euiS+7aTOb-T9X62E9nGyToRcyvKU+eHg@mail.gmail.com/
Cc: stable@vger.kernel.org
cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
cc: Yangtao Li <frank.li@vivo.com>
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Viacheslav Dubeyko <slava@dubeyko.com>
---
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
next prev parent reply other threads:[~2026-11-19 23:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
2026-11-19 23:41 ` Viacheslav Dubeyko [this message]
2026-11-19 23:41 ` [PATCH 2/5] hfsplus: map the allocation file without the extents B-tree Viacheslav Dubeyko
2026-11-19 23:41 ` [PATCH 3/5] hfsplus: give the special files their own extents_lock lockdep class Viacheslav Dubeyko
2026-11-19 23:41 ` [PATCH 4/5] hfsplus: reject an allocation bitmap that does not cover the volume Viacheslav Dubeyko
2026-11-19 23:41 ` [PATCH 5/5] hfsplus: name the constants of the allocation bitmap check Viacheslav Dubeyko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119234112.720963-2-slava@dubeyko.com \
--to=slava@dubeyko.com \
--cc=fengaojigui88@gmail.com \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=vdubeyko@coreweave.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®