Hello Slava, Thanks for the review. I moved validation of the B-tree node map chain into `hfs_btree_open()`, so it runs while the volume is being mounted. The check bounds node indices by both the B-tree header's node count and the number of complete nodes in the tree file, verifies the header/map node types, and rejects out-of-range links and cycles. If the map chain is malformed, the filesystem now emits a warning, recommends `fsck.hfsplus`, and sets `SB_RDONLY` before returning the tree. The cycle fixture completes mounting read-only instead of looping; the normal-map control still mounts writable. Tested on Linux 7.4-rc4, commit 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0. The x86-64 bzImage build completed. The reproducer, kernel configuration, patch, and fixed-kernel console logs are collected here: https://gist.github.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e Changes in v2: - Validate the B-tree map chain during mount. - Force a read-only mount and recommend `fsck.hfsplus` on map corruption. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Thanks, Shawn Shao Signed-off-by: Shawn Shao --- diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c index bd4dbdbc8f98c..54d22755707a2 100644 --- a/fs/hfsplus/btree.c +++ b/fs/hfsplus/btree.c @@ -211,6 +211,63 @@ static bool hfs_bmap_test_bit(struct hfs_bnode *node, u32 node_bit_idx) return (byte & mask) != 0; } +static int hfs_bmap_next_node(struct hfs_btree *tree, u32 index, + u32 max_nodes, u32 *next) +{ + struct hfs_bnode *node; + int err = 0; + + if (index >= max_nodes) + return -EIO; + + node = hfs_bnode_find(tree, index); + if (IS_ERR(node)) + return PTR_ERR(node); + + if ((index == HFSPLUS_TREE_HEAD && node->type != HFS_NODE_HEADER) || + (index != HFSPLUS_TREE_HEAD && node->type != HFS_NODE_MAP)) { + err = -EIO; + goto out; + } + + *next = node->next; + if (*next >= max_nodes && *next) + err = -EIO; +out: + hfs_bnode_put(node); + return err; +} + +static int hfs_bmap_check_map_chain(struct hfs_btree *tree) +{ + u32 max_nodes, slow = HFSPLUS_TREE_HEAD, fast = HFSPLUS_TREE_HEAD; + u32 steps; + int err; + + max_nodes = min_t(u64, tree->node_count, + tree->inode->i_size >> tree->node_size_shift); + if (!max_nodes || tree->node_count > max_nodes) + return -EIO; + + for (steps = 0; steps < max_nodes; steps++) { + err = hfs_bmap_next_node(tree, slow, max_nodes, &slow); + if (err || !slow) + return err; + + err = hfs_bmap_next_node(tree, fast, max_nodes, &fast); + if (err || !fast) + return err; + err = hfs_bmap_next_node(tree, fast, max_nodes, &fast); + if (err || !fast) + return err; + + if (slow == fast) + return -EIO; + } + + return -EIO; +} + /** * hfs_bmap_clear_bit - clear a bit in the b-tree map @@ -274,6 +331,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) struct inode *inode; struct page *page; unsigned int size; + int err; tree = kzalloc_obj(*tree); if (!tree) @@ -385,7 +443,12 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) if (IS_ERR(node)) goto free_inode; - if (!hfs_bmap_test_bit(node, 0)) { + err = hfs_bmap_check_map_chain(tree); + if (err == -ENOMEM) { + hfs_bnode_put(node); + goto free_inode; + } + if (err || !hfs_bmap_test_bit(node, 0)) { pr_warn("(%s): %s (cnid 0x%x) map record invalid or bitmap corruption detected, forcing read-only.\n", sb->s_id, hfs_btree_name(id), id); pr_warn("Run fsck.hfsplus to repair.\n");