Skip to contentSkip to conversation

hfsplus: fix locking and validation of the special files

PATCH New Viacheslav Dubeyko 1 participant 6 messages Nov 19

Subsystems (3)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

cover hfsplus: fix locking and validation of the special files

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(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/5 hfsplus: reject special files larger than their allocation at mount

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.

---
 fs/hfsplus/super.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
fs/hfsplus/super.c +33 -5
index f3283e9b42c8..33594e1f8367 100644
@@ -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.
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/5 hfsplus: map the allocation file without the extents B-tree

In reply to Viacheslav Dubeyko

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.

---
 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(-)
fs/hfsplus/bitmap.c +2 -2
index 30178ea47362..62c39849e381 100644
@@ -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))
fs/hfsplus/extents.c +123 -13

215 lines not on this page · raw message

fs/hfsplus/hfsplus_fs.h +13 -1
index dd88053cc9b3..9a304d946297 100644
@@ -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,
fs/hfsplus/inode.c +2 -2
index 8073fb99217d..e231da2d8a2b 100644
@@ -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;
 	}
fs/hfsplus/super.c +13 -5
index 33594e1f8367..e568a5508b5e 100644
@@ -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:
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/5 hfsplus: give the special files their own extents_lock lockdep class

In reply to 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 <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/super.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/hfsplus/super.c +20 -0
index e568a5508b5e..d23594d12064 100644
@@ -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.
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/5 hfsplus: reject an allocation bitmap that does not cover the volume

In reply to Viacheslav Dubeyko

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 John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/super.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/hfsplus/super.c +13 -0
index d23594d12064..6e4068e53b4f 100644
@@ -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;
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/5 hfsplus: name the constants of the allocation bitmap check

In reply to 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 <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/extents.c    | 9 +++++----
 fs/hfsplus/hfsplus_fs.h | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
fs/hfsplus/extents.c +5 -4
index da6ab85a64ae..f910699338e5 100644
@@ -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;
 	}
 
fs/hfsplus/hfsplus_fs.h +6 -0
index 9a304d946297..4f604c664cb9 100644
@@ -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;
Signature · 1 line
2.53.0