mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] hfsplus: fix locking and validation of the special files
@ 2026-11-19 23:41 Viacheslav Dubeyko
  2026-11-19 23:41 ` [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount Viacheslav Dubeyko
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko

Syzkaller reported a lockdep "possible recursive locking" warning on
the extents_lock in hfsplus_get_block(): deleting a file frees its
blocks, and reading the allocation bitmap took the allocation file's
extents_lock inside the deleted file's one. Looking into it showed
three problems around the special files (extents, catalog, allocation,
startup and attributes file), patches 1-3, and a missing check of the
allocation file's size, patches 4-5.

Patch 1 fixes how syzkaller reached the warning. Its image claims an
allocation file of 0x800000000800 bytes in one allocated block, so the
bitmap read mapped blocks that no extent covers through the extents
B-tree. The mount now fails when the logical size of a special file in
the volume header is larger than its allocation.

Patch 2 fixes a real deadlock on valid volumes. Reading the bitmap with
sbi->alloc_mutex held can search the extents B-tree for an allocation
file extent, while growing the extents B-tree allocates blocks with the
B-tree's tree_lock held. When the allocation file has an extent record
in the extents B-tree (which a fragmented allocation file has), a
single writer deadlocks on the tree_lock it holds. The allocation file
does not change while mounted, so its extent records are now read once
at mount time and its blocks are mapped without the extents B-tree.
This also removes the lock nesting from the syzkaller report.

Patch 3 silences a lockdep false positive on valid volumes: growing
the extents B-tree takes the B-tree file's extents_lock inside the
extents_lock of the file being extended, and with one lockdep class for
every inode read from disk this looks like a circular dependency. Each
special file now gets its own named lockdep class.

Patch 4 rejects at mount an allocation file that is too small to hold
one bit for each block of the volume. The bitmap code looks up any
block below total_blocks in the bitmap, so with a short allocation file
it read and modified bits beyond its end that were never written back;
fsck.hfsplus does not catch such a volume either. Only
hfsplus_file_extend() guarded against it, by failing with -ENOSPC once
the blocks in use approached the bitmap size.

Patch 5 names the two constants of that guard in hfsplus_file_extend()
(bits per byte and a margin of eight bits); no functional change.

Testing (KASAN, lockdep, x86_64 QEMU guest):
- The syzkaller image is rejected at mount, as are images with the size
  of the allocation, extents, catalog or attributes file set one byte
  beyond its allocation; consistent images still mount.
- The deadlock in patch 2 is reproduced, before the fix, on a volume
  created with "mkfs.hfsplus -b 512 -c e=8,d=8" and edited so that the
  allocation file has one extent record in the extents B-tree
  (fsck.hfsplus reports it as OK), by interleaving 4K appends to two
  files. With the fix the writes complete and fsck.hfsplus reports the
  volume as OK.
- The lockdep report in patch 3 is reproduced on a volume created with
  "mkfs.hfsplus -c e=1,d=1" by fragmenting files created before a
  remount, and is gone with the patch.
- A 64M volume whose allocation file size was cut from 4096 to 1024
  bytes mounts without patch 4, fails writes with -ENOSPC at half of
  the volume and is left with an inconsistent bitmap; with the patch it
  is rejected at mount, and it mounts with 2048 bytes (the minimum).
  Filling that volume fails with -ENOSPC at 0 free blocks and leaves it
  consistent.
- xfstests generic/001, 002, 013, 075 and 112 pass; generic/091 fails
  as it does without the series.

Viacheslav Dubeyko (5):
  hfsplus: reject special files larger than their allocation at mount
  hfsplus: map the allocation file without the extents B-tree
  hfsplus: give the special files their own extents_lock lockdep class
  hfsplus: reject an allocation bitmap that does not cover the volume
  hfsplus: name the constants of the allocation bitmap check

 fs/hfsplus/bitmap.c     |   4 +-
 fs/hfsplus/extents.c    | 141 +++++++++++++++++++++++++++++++++++-----
 fs/hfsplus/hfsplus_fs.h |  20 +++++-
 fs/hfsplus/inode.c      |   4 +-
 fs/hfsplus/super.c      |  89 ++++++++++++++++++++++---
 5 files changed, 228 insertions(+), 30 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount
  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
  2026-11-19 23:41 ` [PATCH 2/5] hfsplus: map the allocation file without the extents B-tree Viacheslav Dubeyko
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko,
	Zheng Zheng, stable

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] hfsplus: map the allocation file without the extents B-tree
  2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
  2026-11-19 23:41 ` [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount Viacheslav Dubeyko
@ 2026-11-19 23:41 ` Viacheslav Dubeyko
  2026-11-19 23:41 ` [PATCH 3/5] hfsplus: give the special files their own extents_lock lockdep class Viacheslav Dubeyko
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko,
	Zheng Zheng, stable

hfsplus_block_allocate() and hfsplus_block_free() read the allocation
bitmap through the allocation file with sbi->alloc_mutex held. When the
bitmap page is not cached and lies beyond the allocation file's extents
in the volume header, hfsplus_get_block() takes the allocation file's
extents_lock and looks the block up in the extents B-tree under its
tree_lock.

Growing the extents B-tree does the opposite: hfs_bmap_reserve() is
called with the B-tree's tree_lock held and extends the B-tree file,
which allocates blocks under alloc_mutex. If the bitmap page needed
there is not cached and is mapped by an extent record of the
allocation file, the task tries to take the tree_lock it already holds
and deadlocks:

  WARNING: possible recursive locking detected
  dd/1404 is trying to acquire lock:
  (&tree->tree_lock/1){+.+.}-{4:4}, at: hfsplus_find_init
  but task is already holding lock:
  (&tree->tree_lock/1){+.+.}-{4:4}, at: hfsplus_find_init
  ...
   hfsplus_find_init
   hfsplus_ext_read_extent
   hfsplus_get_block
   block_read_full_folio
   ...
   read_cache_page
   hfsplus_block_allocate
   hfsplus_file_extend
   hfsplus_bmap_reserve
   __hfsplus_ext_write_extent
   hfsplus_ext_read_extent
   hfsplus_file_extend
   hfsplus_get_block
   ...
   hfsplus_write_begin

The writer, the flusher and the superblock commit work then hang. Two
tasks can also deadlock on the same pair of locks taken in opposite
order.

The same lookup is behind the lockdep report from syzkaller in which
truncating an unlinked file takes the allocation file's extents_lock
inside its own (hfsplus_file_truncate() -> hfsplus_free_extents() ->
hfsplus_block_free() -> read_cache_page() -> hfsplus_get_block()).
Syzkaller reached it with a corrupted image, which the previous patch
rejects at mount.

The allocation file cannot grow or shrink while the volume is mounted.
Read its extent records from the extents B-tree once at mount time and
map its blocks beyond the volume header's extents from them, so that
reading the bitmap takes neither the allocation file's extents_lock nor
the extents B-tree's tree_lock. The extents read are kept in the new
struct hfsplus_overflow_extents, which sbi->alloc_file now holds
together with the allocation file's inode as struct hfsplus_alloc_file.
A missing or inconsistent extent record now fails the mount instead of
failing the bitmap reads later.

Tested on a KASAN and lockdep kernel: the reproducer completes without
a lockdep report and fsck.hfsplus reports the volume as OK, also after
allocating blocks whose bitmap page is mapped by the extent record; a
volume whose extent record of the allocation file starts at the wrong
block is rejected at mount; generic/001, 002, 013, 075 and 112 pass,
generic/091 fails as it does without this change.

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/bitmap.c     |   4 +-
 fs/hfsplus/extents.c    | 136 ++++++++++++++++++++++++++++++++++++----
 fs/hfsplus/hfsplus_fs.h |  14 ++++-
 fs/hfsplus/inode.c      |   4 +-
 fs/hfsplus/super.c      |  18 ++++--
 5 files changed, 153 insertions(+), 23 deletions(-)

diff --git a/fs/hfsplus/bitmap.c b/fs/hfsplus/bitmap.c
index 30178ea47362..62c39849e381 100644
--- a/fs/hfsplus/bitmap.c
+++ b/fs/hfsplus/bitmap.c
@@ -33,7 +33,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size,
 
 	hfs_dbg("size %u, offset %u, len %u\n", size, offset, len);
 	mutex_lock(&sbi->alloc_mutex);
-	mapping = sbi->alloc_file->i_mapping;
+	mapping = sbi->alloc_file.inode->i_mapping;
 	page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL);
 	if (IS_ERR(page)) {
 		start = size;
@@ -192,7 +192,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
 		return -ENOENT;
 
 	mutex_lock(&sbi->alloc_mutex);
-	mapping = sbi->alloc_file->i_mapping;
+	mapping = sbi->alloc_file.inode->i_mapping;
 	pnr = offset / PAGE_CACHE_BITS;
 	page = read_mapping_page(mapping, pnr, NULL);
 	if (IS_ERR(page))
diff --git a/fs/hfsplus/extents.c b/fs/hfsplus/extents.c
index 924941440066..da6ab85a64ae 100644
--- a/fs/hfsplus/extents.c
+++ b/fs/hfsplus/extents.c
@@ -53,7 +53,7 @@ static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
 	int i;
 	u32 count;
 
-	for (i = 0; i < 8; ext++, i++) {
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; ext++, i++) {
 		count = be32_to_cpu(ext->block_count);
 		if (off < count)
 			return be32_to_cpu(ext->start_block) + off;
@@ -68,17 +68,18 @@ static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
 	int i;
 	u32 count = 0;
 
-	for (i = 0; i < 8; ext++, i++)
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; ext++, i++)
 		count += be32_to_cpu(ext->block_count);
 	return count;
 }
 
 static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
 {
+	int last_index = HFSPLUS_FORK_EXTENT_COUNT - 1;
 	int i;
 
-	ext += 7;
-	for (i = 0; i < 7; ext--, i++)
+	ext += last_index;
+	for (i = 0; i < last_index; ext--, i++)
 		if (ext->block_count)
 			break;
 	return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
@@ -225,6 +226,102 @@ static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
 	return res;
 }
 
+/*
+ * The allocation bitmap is read through the allocation file with
+ * sbi->alloc_mutex held, sometimes by a task that holds the extents
+ * B-tree's tree_lock because it is growing the B-tree. So mapping a
+ * block of the allocation file must not search the extents B-tree.
+ *
+ * The allocation file is not resized while the volume is mounted, so
+ * read the extents of it that are not in the volume header once, at
+ * mount time, and map its blocks from them.
+ */
+int hfsplus_ext_load_alloc_file(struct super_block *sb)
+{
+	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
+	struct hfsplus_alloc_file *alloc_file = &sbi->alloc_file;
+	struct inode *inode = alloc_file->inode;
+	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
+	struct hfsplus_extent *ext = NULL, *new;
+	struct hfs_find_data fd;
+	u32 block = hip->first_blocks;
+	u32 nr = 0;
+	int res;
+
+	if (block >= hip->alloc_blocks)
+		return 0;
+
+	res = hfs_find_init(sbi->ext_tree, &fd);
+	if (res)
+		return res;
+
+	while (block < hip->alloc_blocks) {
+		hfsplus_extent_rec rec;
+		u32 start_block, count;
+		u32 rest_blocks;
+
+		res = __hfsplus_ext_read_extent(&fd, rec, inode->i_ino, block,
+						HFSPLUS_TYPE_DATA);
+		if (res)
+			goto fail;
+
+		start_block = be32_to_cpu(fd.key->ext.start_block);
+		count = hfsplus_ext_block_count(rec);
+		rest_blocks = hip->alloc_blocks - block;
+
+		if (start_block != block || count == 0 || count > rest_blocks) {
+			res = -EIO;
+			goto fail;
+		}
+
+		new = krealloc_array(ext, nr + ARRAY_SIZE(rec), sizeof(*ext),
+				     GFP_KERNEL);
+		if (!new) {
+			res = -ENOMEM;
+			goto fail;
+		}
+
+		ext = new;
+		memcpy(ext + nr, rec, sizeof(rec));
+		nr += ARRAY_SIZE(rec);
+		block += count;
+	}
+
+	hfs_find_exit(&fd);
+	alloc_file->overflow.extents = ext;
+	alloc_file->overflow.count = nr;
+	return 0;
+
+fail:
+	hfs_find_exit(&fd);
+	kfree(ext);
+	pr_err("can't read the allocation file extents at block %u: %d\n",
+	       block, res);
+	return res == -ENOENT ? -EIO : res;
+}
+
+/*
+ * Map @ablock of the allocation file, which lies beyond its extents in
+ * the volume header. Returns 0 if it is not allocated.
+ */
+static u32 hfsplus_ext_alloc_file_block(struct super_block *sb, u32 ablock)
+{
+	struct hfsplus_alloc_file *alloc_file = &HFSPLUS_SB(sb)->alloc_file;
+	struct hfsplus_overflow_extents *overflow = &alloc_file->overflow;
+	u32 off = ablock - HFSPLUS_I(alloc_file->inode)->first_blocks;
+	u32 start_block, count;
+	u32 i;
+
+	for (i = 0; i < overflow->count; i++) {
+		start_block = be32_to_cpu(overflow->extents[i].start_block);
+		count = be32_to_cpu(overflow->extents[i].block_count);
+		if (off < count)
+			return start_block + off;
+		off -= count;
+	}
+	return 0;
+}
+
 /* Get a block at iblock for inode, possibly allocating if create */
 int hfsplus_get_block(struct inode *inode, sector_t iblock,
 		      struct buffer_head *bh_result, int create)
@@ -258,9 +355,22 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
 		goto done;
 	}
 
-	if (inode->i_ino == HFSPLUS_EXT_CNID)
+	switch (inode->i_ino) {
+	case HFSPLUS_EXT_CNID:
 		return -EIO;
 
+	case HFSPLUS_ALLOC_CNID:
+		dblock = hfsplus_ext_alloc_file_block(sb, ablock);
+		if (!dblock)
+			return -EIO;
+		else
+			goto done;
+
+	default:
+		/* continue logic */
+		break;
+	}
+
 	mutex_lock(&hip->extents_lock);
 
 	/*
@@ -303,7 +413,7 @@ static void hfsplus_dump_extent(struct hfsplus_extent *extent)
 	int i;
 
 	hfs_dbg("extent   ");
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; i++)
 		hfs_dbg(" start_block %u, block_count %u",
 			be32_to_cpu(extent[i].start_block),
 			be32_to_cpu(extent[i].block_count));
@@ -317,7 +427,7 @@ static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
 	int i;
 
 	hfsplus_dump_extent(extent);
-	for (i = 0; i < 8; extent++, i++) {
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; extent++, i++) {
 		count = be32_to_cpu(extent->block_count);
 		if (offset == count) {
 			start = be32_to_cpu(extent->start_block);
@@ -347,7 +457,7 @@ static int hfsplus_free_extents(struct super_block *sb,
 	int err = 0;
 
 	hfsplus_dump_extent(extent);
-	for (i = 0; i < 8; extent++, i++) {
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; extent++, i++) {
 		count = be32_to_cpu(extent->block_count);
 		if (offset == count)
 			goto found;
@@ -405,7 +515,7 @@ int hfsplus_free_fork(struct super_block *sb, u32 cnid,
 		return 0;
 
 	blocks = 0;
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < HFSPLUS_FORK_EXTENT_COUNT; i++)
 		blocks += be32_to_cpu(fork->extents[i].block_count);
 
 	res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
@@ -445,11 +555,11 @@ int hfsplus_file_extend(struct inode *inode, bool zeroout)
 	u32 start, len, goal;
 	int res;
 
-	if (sbi->alloc_file->i_size * 8 <
+	if (sbi->alloc_file.inode->i_size * 8 <
 	    sbi->total_blocks - sbi->free_blocks + 8) {
 		/* extend alloc file */
 		pr_err_ratelimited("extend alloc file! (%llu,%u,%u)\n",
-				   sbi->alloc_file->i_size * 8,
+				   sbi->alloc_file.inode->i_size * 8,
 				   sbi->total_blocks, sbi->free_blocks);
 		return -ENOSPC;
 	}
@@ -517,7 +627,7 @@ int hfsplus_file_extend(struct inode *inode, bool zeroout)
 	if (!res) {
 		hip->alloc_blocks += len;
 		mutex_unlock(&hip->extents_lock);
-		hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file,
+		hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file.inode,
 					 HFSPLUS_I_ALLOC_DIRTY);
 		hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
 		return 0;
@@ -637,7 +747,7 @@ void hfsplus_file_truncate(struct inode *inode)
 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
 		sb->s_blocksize_bits;
 	inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
-	hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file,
+	hfsplus_mark_inode_dirty(HFSPLUS_SB(sb)->alloc_file.inode,
 				 HFSPLUS_I_ALLOC_DIRTY);
 	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
 }
diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
index dd88053cc9b3..9a304d946297 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -109,6 +109,17 @@ struct hfs_bnode {
 struct hfsplus_vh;
 struct hfs_btree;
 
+/* Extents of a special file that are not in the volume header */
+struct hfsplus_overflow_extents {
+	struct hfsplus_extent *extents;
+	u32 count;
+};
+
+struct hfsplus_alloc_file {
+	struct inode *inode;
+	struct hfsplus_overflow_extents overflow;
+};
+
 struct hfsplus_sb_info {
 	void *s_vhdr_buf;
 	struct hfsplus_vh *s_vhdr;
@@ -118,7 +129,7 @@ struct hfsplus_sb_info {
 	struct hfs_btree *cat_tree;
 	struct hfs_btree *attr_tree;
 	atomic_t attr_tree_state;
-	struct inode *alloc_file;
+	struct hfsplus_alloc_file alloc_file;
 	struct inode *hidden_dir;
 	struct nls_table *nls;
 
@@ -440,6 +451,7 @@ extern const struct file_operations hfsplus_dir_operations;
 int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
 			const hfsplus_btree_key *k2);
 int hfsplus_ext_write_extent(struct inode *inode);
+int hfsplus_ext_load_alloc_file(struct super_block *sb);
 int hfsplus_get_block(struct inode *inode, sector_t iblock,
 		      struct buffer_head *bh_result, int create);
 int hfsplus_free_fork(struct super_block *sb, u32 cnid,
diff --git a/fs/hfsplus/inode.c b/fs/hfsplus/inode.c
index 8073fb99217d..e231da2d8a2b 100644
--- a/fs/hfsplus/inode.c
+++ b/fs/hfsplus/inode.c
@@ -410,9 +410,9 @@ int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
 	}
 
 	if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY,
-				&HFSPLUS_I(sbi->alloc_file)->flags)) {
+				&HFSPLUS_I(sbi->alloc_file.inode)->flags)) {
 		clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags);
-		error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
+		error2 = filemap_write_and_wait(sbi->alloc_file.inode->i_mapping);
 		if (!error)
 			error = error2;
 	}
diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c
index 33594e1f8367..e568a5508b5e 100644
--- a/fs/hfsplus/super.c
+++ b/fs/hfsplus/super.c
@@ -298,7 +298,7 @@ static int hfsplus_sync_fs(struct super_block *sb, int wait)
 		if (!error)
 			error = error2;
 	}
-	error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
+	error2 = filemap_write_and_wait(sbi->alloc_file.inode->i_mapping);
 	if (!error)
 		error = error2;
 
@@ -325,7 +325,7 @@ static void delayed_sync_fs(struct work_struct *work)
 	sbi->work_queued = 0;
 	spin_unlock(&sbi->work_lock);
 
-	err = hfsplus_sync_fs(sbi->alloc_file->i_sb, 1);
+	err = hfsplus_sync_fs(sbi->alloc_file.inode->i_sb, 1);
 	if (err)
 		pr_err("delayed sync fs err %d\n", err);
 }
@@ -399,7 +399,8 @@ static void hfsplus_put_super(struct super_block *sb)
 	if (!sb_rdonly(sb) && sbi->s_vhdr)
 		hfsplus_commit_clean_volume_header(sb);
 
-	iput(sbi->alloc_file);
+	iput(sbi->alloc_file.inode);
+	kfree(sbi->alloc_file.overflow.extents);
 	iput(sbi->hidden_dir);
 	hfs_btree_close(sbi->attr_tree);
 	hfs_btree_close(sbi->cat_tree);
@@ -754,7 +755,11 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc)
 		err = PTR_ERR(inode);
 		goto out_close_attr_tree;
 	}
-	sbi->alloc_file = inode;
+	sbi->alloc_file.inode = inode;
+
+	err = hfsplus_ext_load_alloc_file(sb);
+	if (err)
+		goto out_put_alloc_file;
 
 	/* Load the root directory */
 	root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
@@ -816,7 +821,10 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc)
 	dput(sb->s_root);
 	sb->s_root = NULL;
 out_put_alloc_file:
-	iput(sbi->alloc_file);
+	iput(sbi->alloc_file.inode);
+	kfree(sbi->alloc_file.overflow.extents);
+	sbi->alloc_file.overflow.extents = NULL;
+	sbi->alloc_file.overflow.count = 0;
 out_close_attr_tree:
 	hfs_btree_close(sbi->attr_tree);
 out_close_cat_tree:
-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] hfsplus: give the special files their own extents_lock lockdep class
  2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
  2026-11-19 23:41 ` [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount Viacheslav Dubeyko
  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 ` 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
  4 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko

Writing a new extent record of a file can grow the extents B-tree. The
B-tree file is then extended with the file's extents_lock and the
B-tree's tree_lock held, and takes its own extents_lock:

  hfsplus_file_extend()                    file's extents_lock
    hfsplus_ext_read_extent()
      __hfsplus_ext_write_extent()         extents B-tree's tree_lock
        hfs_bmap_reserve()
          hfsplus_file_extend(ext_tree->inode)
                                           B-tree file's extents_lock

All inodes read by hfsplus_iget() share one lockdep class for
extents_lock, so lockdep sees the file's extents_lock -> tree_lock ->
extents_lock chain as a cycle:

  WARNING: possible circular locking dependency detected
  dd/1553 is trying to acquire lock:
  (&tree->tree_lock/1){+.+.}-{4:4}, at: hfsplus_find_init
  but task is already holding lock:
  (&HFSPLUS_I(inode)->extents_lock){+.+.}-{4:4}, at: hfsplus_file_extend

This is a false positive: the B-tree file's extents_lock always nests
inside the other file's, never the other way around. But lockdep turns
itself off after the first report, which hides any later real problem.

Give the extents_lock of each special file its own named lockdep class
when its inode is read from the volume header.

The report is reproduced on a valid volume created with "mkfs.hfsplus
-c e=1,d=1", whose extents B-tree has no free node, by interleaving
appends to two files that were created before a remount until a file
needs an extent record in the extents B-tree. The remount matters,
because hfsplus_new_inode() initializes extents_lock in a different
lockdep class than hfsplus_iget().

Tested on a KASAN and lockdep kernel: the report is gone and generic/001,
002, 013, 075 and 112 pass.

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 | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c
index e568a5508b5e..d23594d12064 100644
--- a/fs/hfsplus/super.c
+++ b/fs/hfsplus/super.c
@@ -50,30 +50,48 @@ static int hfsplus_system_read_fork(struct inode *inode,
 	return 0;
 }
 
+static struct lock_class_key hfsplus_ext_extents_lock_key;
+static struct lock_class_key hfsplus_cat_extents_lock_key;
+static struct lock_class_key hfsplus_alloc_extents_lock_key;
+static struct lock_class_key hfsplus_start_extents_lock_key;
+static struct lock_class_key hfsplus_attr_extents_lock_key;
+
 static int hfsplus_system_read_inode(struct inode *inode)
 {
 	struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
+	struct lock_class_key *key;
+	const char *name;
 	int err;
 
 	switch (inode->i_ino) {
 	case HFSPLUS_EXT_CNID:
 		err = hfsplus_system_read_fork(inode, &vhdr->ext_file);
 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
+		key = &hfsplus_ext_extents_lock_key;
+		name = "ext_file_extents_lock";
 		break;
 	case HFSPLUS_CAT_CNID:
 		err = hfsplus_system_read_fork(inode, &vhdr->cat_file);
 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
+		key = &hfsplus_cat_extents_lock_key;
+		name = "cat_file_extents_lock";
 		break;
 	case HFSPLUS_ALLOC_CNID:
 		err = hfsplus_system_read_fork(inode, &vhdr->alloc_file);
 		inode->i_mapping->a_ops = &hfsplus_aops;
+		key = &hfsplus_alloc_extents_lock_key;
+		name = "alloc_file_extents_lock";
 		break;
 	case HFSPLUS_START_CNID:
 		err = hfsplus_system_read_fork(inode, &vhdr->start_file);
+		key = &hfsplus_start_extents_lock_key;
+		name = "start_file_extents_lock";
 		break;
 	case HFSPLUS_ATTR_CNID:
 		err = hfsplus_system_read_fork(inode, &vhdr->attr_file);
 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
+		key = &hfsplus_attr_extents_lock_key;
+		name = "attr_file_extents_lock";
 		break;
 	default:
 		return -EIO;
@@ -82,6 +100,8 @@ static int hfsplus_system_read_inode(struct inode *inode)
 	if (err)
 		return err;
 
+	lockdep_set_class_and_name(&HFSPLUS_I(inode)->extents_lock, key, name);
+
 	/*
 	 * Assign a dummy file type, for may_open() requires that
 	 * an inode has a valid file type.
-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] hfsplus: reject an allocation bitmap that does not cover the volume
  2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
                   ` (2 preceding siblings ...)
  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 ` Viacheslav Dubeyko
  2026-11-19 23:41 ` [PATCH 5/5] hfsplus: name the constants of the allocation bitmap check Viacheslav Dubeyko
  4 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko, stable

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@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 | 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] hfsplus: name the constants of the allocation bitmap check
  2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
                   ` (3 preceding siblings ...)
  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 ` Viacheslav Dubeyko
  4 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-19 23:41 UTC (permalink / raw)
  To: glaubitz, frank.li
  Cc: linux-fsdevel, linux-kernel, vdubeyko, Viacheslav Dubeyko

hfsplus_file_extend() refuses to allocate when the allocation bitmap
has fewer than eight bits beyond the blocks in use, written as

  i_size * 8 < total_blocks - free_blocks + 8

where the first 8 is the number of bits per byte and the second one is
a margin. Use BITS_PER_BYTE for the first and a new constant,
HFSPLUS_ALLOC_BITMAP_SPARE_BITS, for the second, and name the two sides
of the comparison. The number of blocks in use plus the margin is now
computed in 64 bits, as the bitmap size already was; there is no other
functional change.

Tested on a KASAN and lockdep kernel: filling a volume whose allocation
file is exactly as large as the volume needs fails with -ENOSPC at 0
free blocks and fsck.hfsplus reports the volume as OK; generic/001,
002, 013, 075 and 112 pass.

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/extents.c    | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/fs/hfsplus/extents.c b/fs/hfsplus/extents.c
index da6ab85a64ae..f910699338e5 100644
--- a/fs/hfsplus/extents.c
+++ b/fs/hfsplus/extents.c
@@ -552,15 +552,16 @@ int hfsplus_file_extend(struct inode *inode, bool zeroout)
 	struct super_block *sb = inode->i_sb;
 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
 	struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
+	u64 bitmap_bits = (u64)sbi->alloc_file.inode->i_size * BITS_PER_BYTE;
+	u32 used_blocks = sbi->total_blocks - sbi->free_blocks;
 	u32 start, len, goal;
 	int res;
 
-	if (sbi->alloc_file.inode->i_size * 8 <
-	    sbi->total_blocks - sbi->free_blocks + 8) {
+	if (bitmap_bits < (u64)used_blocks + HFSPLUS_ALLOC_BITMAP_SPARE_BITS) {
 		/* extend alloc file */
 		pr_err_ratelimited("extend alloc file! (%llu,%u,%u)\n",
-				   sbi->alloc_file.inode->i_size * 8,
-				   sbi->total_blocks, sbi->free_blocks);
+				   bitmap_bits, sbi->total_blocks,
+				   sbi->free_blocks);
 		return -ENOSPC;
 	}
 
diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
index 9a304d946297..4f604c664cb9 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -120,6 +120,12 @@ struct hfsplus_alloc_file {
 	struct hfsplus_overflow_extents overflow;
 };
 
+/*
+ * Bits the allocation bitmap must have beyond the blocks in use for
+ * hfsplus_file_extend() to allocate more blocks.
+ */
+#define HFSPLUS_ALLOC_BITMAP_SPARE_BITS	8
+
 struct hfsplus_sb_info {
 	void *s_vhdr_buf;
 	struct hfsplus_vh *s_vhdr;
-- 
2.53.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-19 23:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 23:41 [PATCH 0/5] hfsplus: fix locking and validation of the special files Viacheslav Dubeyko
2026-11-19 23:41 ` [PATCH 1/5] hfsplus: reject special files larger than their allocation at mount Viacheslav Dubeyko
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

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®