Skip to content

Commit 8ad2c6a

Browse files
shardulsdk-mpiricdubeyko
authored andcommitted
hfsplus: validate b-tree node 0 bitmap at mount time
Syzkaller reported an issue with corrupted HFS+ images where the b-tree allocation bitmap indicates that the header node (Node 0) is free. Node 0 must always be allocated as it contains the b-tree header record and the allocation bitmap itself. Violating this invariant leads to allocator corruption, which cascades into kernel panics or undefined behavior when the filesystem attempts to allocate blocks. Prevent trusting a corrupted allocator state by adding a validation check during hfs_btree_open(). Introduce the hfs_bmap_test_bit() helper (utilizing the newly added map-access API) to safely verify that the MSB of the first bitmap byte (representing Node 0) is marked as allocated. The helper returns a boolean, allowing the caller to safely catch both structural IO errors and illegally cleared bits in a single check. If corruption is detected, print a warning identifying the specific corrupted tree and force the filesystem to mount read-only (SB_RDONLY). This prevents kernel panics from corrupted images while enabling data recovery. As a minor cleanup to support the warning logs, replace the verbose CNID logic with cleaner macro definitions (using official structural names like "Extents Overflow File") and a dedicated string lookup helper. Reported-by: [email protected] Link: https://syzkaller.appspot.com/bug?extid=1c8ff72d0cd8a50dfeaa Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Shardul Bankar <[email protected]> Reviewed-by: Viacheslav Dubeyko <[email protected]> Tested-by: Viacheslav Dubeyko <[email protected]> Signed-off-by: Viacheslav Dubeyko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Viacheslav Dubeyko <[email protected]>
1 parent a8eed0b commit 8ad2c6a

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

fs/hfsplus/btree.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,32 @@ static struct page *hfs_bmap_get_map_page(struct hfs_bnode *node, struct hfs_bma
185185
return node->page[ctx->page_idx];
186186
}
187187

188+
/**
189+
* hfs_bmap_test_bit - test a bit in the b-tree map
190+
* @node: the b-tree node containing the map record
191+
* @node_bit_idx: the relative bit index within the node's map record
192+
*
193+
* Returns true if set, false if clear or on failure.
194+
*/
195+
static bool hfs_bmap_test_bit(struct hfs_bnode *node, u32 node_bit_idx)
196+
{
197+
struct hfs_bmap_ctx ctx;
198+
struct page *page;
199+
u8 *bmap, byte, mask;
200+
201+
page = hfs_bmap_get_map_page(node, &ctx, node_bit_idx / BITS_PER_BYTE);
202+
if (IS_ERR(page))
203+
return false;
204+
205+
bmap = kmap_local_page(page);
206+
byte = bmap[ctx.off];
207+
kunmap_local(bmap);
208+
209+
mask = 1 << (7 - (node_bit_idx % BITS_PER_BYTE));
210+
return (byte & mask) != 0;
211+
}
212+
213+
188214
/**
189215
* hfs_bmap_clear_bit - clear a bit in the b-tree map
190216
* @node: the b-tree node containing the map record
@@ -218,12 +244,32 @@ static int hfs_bmap_clear_bit(struct hfs_bnode *node, u32 node_bit_idx)
218244
return 0;
219245
}
220246

247+
#define HFS_EXTENT_TREE_NAME "Extents Overflow File"
248+
#define HFS_CATALOG_TREE_NAME "Catalog File"
249+
#define HFS_ATTR_TREE_NAME "Attributes File"
250+
#define HFS_UNKNOWN_TREE_NAME "Unknown B-tree"
251+
252+
static const char *hfs_btree_name(u32 cnid)
253+
{
254+
switch (cnid) {
255+
case HFSPLUS_EXT_CNID:
256+
return HFS_EXTENT_TREE_NAME;
257+
case HFSPLUS_CAT_CNID:
258+
return HFS_CATALOG_TREE_NAME;
259+
case HFSPLUS_ATTR_CNID:
260+
return HFS_ATTR_TREE_NAME;
261+
default:
262+
return HFS_UNKNOWN_TREE_NAME;
263+
}
264+
}
265+
221266
/* Get a reference to a B*Tree and do some initial checks */
222267
struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
223268
{
224269
struct hfs_btree *tree;
225270
struct hfs_btree_header_rec *head;
226271
struct address_space *mapping;
272+
struct hfs_bnode *node;
227273
struct inode *inode;
228274
struct page *page;
229275
unsigned int size;
@@ -331,6 +377,20 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
331377

332378
kunmap_local(head);
333379
put_page(page);
380+
381+
node = hfs_bnode_find(tree, HFSPLUS_TREE_HEAD);
382+
if (IS_ERR(node))
383+
goto free_inode;
384+
385+
if (!hfs_bmap_test_bit(node, 0)) {
386+
pr_warn("(%s): %s (cnid 0x%x) map record invalid or bitmap corruption detected, forcing read-only.\n",
387+
sb->s_id, hfs_btree_name(id), id);
388+
pr_warn("Run fsck.hfsplus to repair.\n");
389+
sb->s_flags |= SB_RDONLY;
390+
}
391+
392+
hfs_bnode_put(node);
393+
334394
return tree;
335395

336396
fail_page:

0 commit comments

Comments
 (0)