Re: [PATCH v3] hfsplus: validate B-tree map chain at mount

From: Viacheslav Dubeyko

Date: Wed Dec 02 2026 - 16:03:36 EST


On Wed, 2026-12-02 at 07:56 +0000, Shawn Shao wrote:
> HFS+ map traversal follows the next links in the B-tree header and
> map
> nodes. A malformed link can make a map walk repeat indefinitely or
> reach
> a node outside the tree file. Checking only the first bitmap bit does
> not
> validate the chain used by later map operations.
>
> Validate the header and complete map-node chain while opening the B-
> tree.
> Bound traversal by the declared node count and the complete nodes in
> the
> tree file, and require the map records to cover the declared node
> count.
> Use shared next/prev helpers to validate node type, file bounds, and
> reciprocal links in leaf and index chains, and use them for B-tree
> cursor
> movement, node unlink/split, and map allocation/free.
>
> When the map chain is corrupt, warn and keep the filesystem read-only
> so
> that it can be repaired with fsck.hfsplus.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
>
> ---
> Changes in v3:
> - Replace the fast/slow pointer walk with a bounded map-chain check
> at
>   B-tree open.
> - Add shared validated next/prev helpers and use them in node
> traversal,
>   split/unlink, and map allocation/free paths.
> - Check physical node bounds and bitmap coverage; force read-only on
> a
>   malformed map chain and recommend fsck.hfsplus.
>
> Testing:
> - x86-64 bzImage build passed with CONFIG_HFSPLUS_FS=y.
> - Fixed cycle fixture mounted read-only and printed the fsck.hfsplus
>   recommendation; its oracle reported CYCLE_MOUNTED_READ_ONLY.
> - Normal-map control mounted writable; its oracle reported
>   CONTROL_MOUNTED_WRITABLE.
> - The initramfs fixture compiled with -static -O2 -Wall -Wextra -
> Werror.
> - git apply --check, git diff --check, and checkpatch --strict
> passed.
>
> Reproducer, configuration, and both console logs:
> https://gist.github.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e
>
>  fs/hfsplus/bfind.c      |  32 +++++------
>  fs/hfsplus/bnode.c      | 145
> ++++++++++++++++++++++++++++++++++++++++--------
>  fs/hfsplus/brec.c       |   5 +-
>  fs/hfsplus/btree.c      | 108 ++++++++++++++++++++++++++----------
>  fs/hfsplus/hfsplus_fs.h |   4 ++
>  5 files changed, 220 insertions(+), 74 deletions(-)
>
> diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c
> index ca9813f58a6dd..d91fd95f853bc 100644
> --- a/fs/hfsplus/bfind.c
> +++ b/fs/hfsplus/bfind.c
> @@ -227,49 +227,45 @@ int hfs_brec_read(struct hfs_find_data *fd,
> void *rec, u32 rec_len)
>  
>  int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
>  {
> - struct hfs_btree *tree;
> - struct hfs_bnode *bnode;
> - int idx, res = 0;
> + struct hfs_bnode *bnode, *sibling;
> + int res = 0;
>   u16 off, len, keylen;
>  
>   bnode = fd->bnode;
> - tree = bnode->tree;
>  
>   if (cnt < 0) {
>   cnt = -cnt;
>   while (cnt > fd->record) {
>   cnt -= fd->record + 1;
>   fd->record = bnode->num_recs - 1;
> - idx = bnode->prev;
> - if (!idx) {
> + sibling = hfs_bnode_find_prev(bnode);
> + if (!sibling) {
>   res = -ENOENT;
>   goto out;
>   }
> - hfs_bnode_put(bnode);
> - bnode = hfs_bnode_find(tree, idx);
> - if (IS_ERR(bnode)) {
> - res = PTR_ERR(bnode);
> - bnode = NULL;
> + if (IS_ERR(sibling)) {
> + res = PTR_ERR(sibling);
>   goto out;
>   }
> + hfs_bnode_put(bnode);
> + bnode = sibling;
>   }
>   fd->record -= cnt;
>   } else {
>   while (cnt >= bnode->num_recs - fd->record) {
>   cnt -= bnode->num_recs - fd->record;
>   fd->record = 0;
> - idx = bnode->next;
> - if (!idx) {
> + sibling = hfs_bnode_find_next(bnode);
> + if (!sibling) {
>   res = -ENOENT;
>   goto out;
>   }
> - hfs_bnode_put(bnode);
> - bnode = hfs_bnode_find(tree, idx);
> - if (IS_ERR(bnode)) {
> - res = PTR_ERR(bnode);
> - bnode = NULL;
> + if (IS_ERR(sibling)) {
> + res = PTR_ERR(sibling);
>   goto out;
>   }
> + hfs_bnode_put(bnode);
> + bnode = sibling;
>   }
>   fd->record += cnt;
>   }
> diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
> index 3a1a1fa6f2e25..20dfe5493bbdd 100644
> --- a/fs/hfsplus/bnode.c
> +++ b/fs/hfsplus/bnode.c
> @@ -393,34 +393,39 @@ void hfs_bnode_dump(struct hfs_bnode *node)
>  
>  void hfs_bnode_unlink(struct hfs_bnode *node)
>  {
> - struct hfs_btree *tree;
> - struct hfs_bnode *tmp;
> + struct hfs_btree *tree = node->tree;
> + struct hfs_bnode *prev, *next;
>   __be32 cnid;
>  
> - tree = node->tree;
> - if (node->prev) {
> - tmp = hfs_bnode_find(tree, node->prev);
> - if (IS_ERR(tmp))
> - return;
> - tmp->next = node->next;
> - cnid = cpu_to_be32(tmp->next);
> - hfs_bnode_write(tmp, &cnid,
> - offsetof(struct hfs_bnode_desc, next), 4);
> - hfs_bnode_put(tmp);
> - } else if (node->type == HFS_NODE_LEAF)
> + prev = hfs_bnode_find_prev(node);
> + if (IS_ERR(prev))
> + return;
> + next = hfs_bnode_find_next(node);
> + if (IS_ERR(next)) {
> + hfs_bnode_put(prev);
> + return;
> + }
> +
> + if (prev) {
> + prev->next = node->next;
> + cnid = cpu_to_be32(prev->next);
> + hfs_bnode_write(prev, &cnid,
> + offsetof(struct hfs_bnode_desc,
> next), 4);
> + } else if (node->type == HFS_NODE_LEAF) {
>   tree->leaf_head = node->next;
> + }
>  
> - if (node->next) {
> - tmp = hfs_bnode_find(tree, node->next);
> - if (IS_ERR(tmp))
> - return;
> - tmp->prev = node->prev;
> - cnid = cpu_to_be32(tmp->prev);
> - hfs_bnode_write(tmp, &cnid,
> - offsetof(struct hfs_bnode_desc, prev), 4);
> - hfs_bnode_put(tmp);
> - } else if (node->type == HFS_NODE_LEAF)
> + if (next) {
> + next->prev = node->prev;
> + cnid = cpu_to_be32(next->prev);
> + hfs_bnode_write(next, &cnid,
> + offsetof(struct hfs_bnode_desc,
> prev), 4);
> + } else if (node->type == HFS_NODE_LEAF) {
>   tree->leaf_tail = node->prev;
> + }
> +
> + hfs_bnode_put(prev);
> + hfs_bnode_put(next);
>  
>   /* move down? */
>   if (!node->prev && !node->next)
> @@ -618,6 +623,100 @@ struct hfs_bnode *hfs_bnode_find(struct
> hfs_btree *tree, u32 num)
>   return ERR_PTR(-EIO);
>  }
>  
> +/*
> + * Header and map nodes use a forward-only chain. Leaf and index
> nodes
> + * have reciprocal links to nodes of the same type and height.
> + */
> +static struct hfs_bnode *hfs_bnode_find_sibling(struct hfs_bnode
> *node,
> + bool next)
> +{
> + struct hfs_btree *tree = node->tree;
> + struct hfs_bnode *sibling;
> + u32 link = next ? node->next : node->prev;
> + u32 back_link;
> + u64 file_nodes;
> + u64 node_limit;
> +
> + file_nodes = (u64)tree->inode->i_size >> tree-
> >node_size_shift;
> + node_limit = min_t(u64, tree->node_count, file_nodes);
> + if (!node_limit || node->this >= node_limit)
> + goto invalid_link;
> +
> + switch (node->type) {
> + case HFS_NODE_HEADER:
> + case HFS_NODE_MAP:
> + if (!next || node->prev)
> + goto invalid_link;
> + break;
> + case HFS_NODE_LEAF:
> + case HFS_NODE_INDEX:
> + break;
> + default:
> + goto invalid_link;
> + }
> +
> + if (!link)
> + return NULL;
> + if (link == node->this || link >= node_limit)
> + goto invalid_link;
> +
> + sibling = hfs_bnode_find(tree, link);
> + if (IS_ERR(sibling))
> + return sibling;
> +
> + switch (node->type) {
> + case HFS_NODE_HEADER:
> + case HFS_NODE_MAP:
> + if (sibling->type != HFS_NODE_MAP || sibling->prev)
> + goto invalid_sibling;
> + break;
> + case HFS_NODE_LEAF:
> + case HFS_NODE_INDEX:
> + back_link = next ? sibling->prev : sibling->next;
> + if (sibling->type != node->type ||
> +     sibling->height != node->height ||
> +     back_link != node->this)
> + goto invalid_sibling;
> + break;
> + default:
> + goto invalid_sibling;
> + }
> +
> + return sibling;
> +
> +invalid_sibling:
> + hfs_bnode_put(sibling);
> +invalid_link:
> + pr_err("invalid %s link %u of node %u (type %u) in b-tree
> %u\n",
> +        next ? "next" : "prev", link, node->this, node->type,
> +        tree->cnid);
> + return ERR_PTR(-EIO);
> +}
> +
> +/**
> + * hfs_bnode_find_next - get a node linked after a B-tree node
> + * @node: node whose next link is followed
> + *
> + * Return: the linked node with a reference held, NULL if there is
> no
> + * next node, or an ERR_PTR() if the link is invalid.
> + */
> +struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node)
> +{
> + return hfs_bnode_find_sibling(node, true);
> +}
> +
> +/**
> + * hfs_bnode_find_prev - get a node linked before a B-tree node
> + * @node: leaf or index node whose previous link is followed
> + *
> + * Return: the linked node with a reference held, NULL if there is
> no
> + * previous node, or an ERR_PTR() if the link is invalid.
> + */
> +struct hfs_bnode *hfs_bnode_find_prev(struct hfs_bnode *node)
> +{
> + return hfs_bnode_find_sibling(node, false);
> +}
> +
>  void hfs_bnode_free(struct hfs_bnode *node)
>  {
>   int i;
> diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
> index f416e562d4397..3bb7b282d85bb 100644
> --- a/fs/hfsplus/brec.c
> +++ b/fs/hfsplus/brec.c
> @@ -275,10 +275,7 @@ static struct hfs_bnode *hfs_bnode_split(struct
> hfs_find_data *fd)
>   new_node->type = node->type;
>   new_node->height = node->height;
>  
> - if (node->next)
> - next_node = hfs_bnode_find(tree, node->next);
> - else
> - next_node = NULL;
> + next_node = hfs_bnode_find_next(node);
>  
>   if (IS_ERR(next_node)) {
>   hfs_bnode_put(node);
> diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
> index bd4dbdbc8f98c..1294200b2f600 100644
> --- a/fs/hfsplus/btree.c
> +++ b/fs/hfsplus/btree.c
> @@ -212,6 +212,70 @@ static bool hfs_bmap_test_bit(struct hfs_bnode
> *node, u32 node_bit_idx)
>  }
>  
>  
> +/* Validate the header and the complete forward chain of map nodes.
> */
> +static int hfs_btree_check_map_chain(struct hfs_btree *tree)
> +{
> + struct hfs_bmap_ctx ctx;
> + struct hfs_bnode *node, *next;
> + struct page *page;
> + u64 file_nodes;
> + u64 map_bits = 0;
> + u64 nodes;
> + int err;
> +
> + file_nodes = tree->inode->i_size >> tree->node_size_shift;
> + if (!tree->node_count || tree->node_count > file_nodes)
> + return -EIO;
> +
> + node = hfs_bnode_find(tree, HFSPLUS_TREE_HEAD);
> + if (IS_ERR(node))
> + return PTR_ERR(node);
> +
> + if (node->type != HFS_NODE_HEADER || node->prev ||
> +     !hfs_bmap_test_bit(node, 0)) {
> + err = -EIO;
> + goto out;
> + }
> + page = hfs_bmap_get_map_page(node, &ctx, 0);
> + if (IS_ERR(page)) {
> + err = PTR_ERR(page);
> + goto out;
> + }
> + map_bits = (u64)ctx.len * BITS_PER_BYTE;
> +
> + for (nodes = 1; node->next; nodes++) {
> + if (nodes >= tree->node_count) {
> + err = -EIO;
> + goto out;
> + }
> +
> + next = hfs_bnode_find_next(node);
> + if (IS_ERR(next)) {
> + err = PTR_ERR(next);
> + goto out;
> + }
> + if (!next) {
> + err = -EIO;
> + goto out;
> + }
> +
> + hfs_bnode_put(node);
> + node = next;
> +
> + page = hfs_bmap_get_map_page(node, &ctx, 0);
> + if (IS_ERR(page)) {
> + err = PTR_ERR(page);
> + goto out;
> + }
> + map_bits += (u64)ctx.len * BITS_PER_BYTE;
> + }
> +
> + err = map_bits < tree->node_count ? -EIO : 0;
> +out:
> + hfs_bnode_put(node);
> + return err;
> +}
> +
>  /**
>   * hfs_bmap_clear_bit - clear a bit in the b-tree map
>   * @node: the b-tree node containing the map record
> @@ -270,10 +334,10 @@ struct hfs_btree *hfs_btree_open(struct
> super_block *sb, u32 id)
>   struct hfs_btree *tree;
>   struct hfs_btree_header_rec *head;
>   struct address_space *mapping;
> - struct hfs_bnode *node;
>   struct inode *inode;
>   struct page *page;
>   unsigned int size;
> + int err;
>  
>   tree = kzalloc_obj(*tree);
>   if (!tree)
> @@ -381,19 +445,16 @@ struct hfs_btree *hfs_btree_open(struct
> super_block *sb, u32 id)
>   kunmap_local(head);
>   put_page(page);
>  
> - node = hfs_bnode_find(tree, HFSPLUS_TREE_HEAD);
> - if (IS_ERR(node))
> + err = hfs_btree_check_map_chain(tree);
> + if (err == -ENOMEM)
>   goto free_inode;
> -
> - if (!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);
> + if (err) {
> + pr_warn("(%s): %s (cnid 0x%x) map chain is invalid,
> forcing read-only.\n",
> + sb->s_id, hfs_btree_name(id), id);
>   pr_warn("Run fsck.hfsplus to repair.\n");
>   sb->s_flags |= SB_RDONLY;
>   }
>  
> - hfs_bnode_put(node);
> -
>   return tree;
>  
>   fail_page:
> @@ -587,13 +648,12 @@ struct hfs_bnode *hfs_bmap_alloc(struct
> hfs_btree *tree)
>   ctx.len--;
>   }
>   kunmap_local(data);
> - nidx = node->next;
> - if (!nidx) {
> + next_node = hfs_bnode_find_next(node);
> + if (!next_node) {
>   hfs_dbg("create new bmap node\n");
>   next_node = hfs_bmap_new_bmap(node, idx);
>   hfs_btree_write(tree);
> - } else
> - next_node = hfs_bnode_find(tree, nidx);
> + }
>   hfs_bnode_put(node);
>   if (IS_ERR(next_node))
>   return next_node;
> @@ -632,30 +692,20 @@ void hfs_bmap_free(struct hfs_bnode *node)
>   return;
>   }
>   while (nidx >= len * 8) {
> - u32 i;
> + struct hfs_bnode *next_node;
>  
>   nidx -= len * 8;
> - i = node->next;
> - if (!i) {
> - /* panic */;
> - pr_crit("unable to free bnode %u. "
> - "bmap not found!\n",
> + next_node = hfs_bnode_find_next(node);
> + if (!next_node) {
> + pr_crit("unable to free bnode %u. bmap not
> found!\n",
>   node->this);
>   hfs_bnode_put(node);
>   return;
>   }
>   hfs_bnode_put(node);
> - node = hfs_bnode_find(tree, i);
> - if (IS_ERR(node))
> - return;
> - if (node->type != HFS_NODE_MAP) {
> - /* panic */;
> - pr_crit("invalid bmap found! "
> - "(%u,%d)\n",
> - node->this, node->type);
> - hfs_bnode_put(node);
> + if (IS_ERR(next_node))
>   return;
> - }
> + node = next_node;
>   len = hfs_brec_lenoff(node, 0, &off);
>   if (hfs_brec_len_invalid(node, len)) {
>   pr_err("invalid bmap record length: node %u,
> len %u\n",
> diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
> index 6f240f87f8ff1..1ebbd78b3e30b 100644
> --- a/fs/hfsplus/hfsplus_fs.h
> +++ b/fs/hfsplus/hfsplus_fs.h
> @@ -303,6 +303,8 @@ static inline unsigned short
> hfsplus_min_io_size(struct super_block *sb)
>  #define hfs_bnode_unlink hfsplus_bnode_unlink
>  #define hfs_bnode_findhash hfsplus_bnode_findhash
>  #define hfs_bnode_find hfsplus_bnode_find
> +#define hfs_bnode_find_next hfsplus_bnode_find_next
> +#define hfs_bnode_find_prev hfsplus_bnode_find_prev
>  #define hfs_bnode_unhash hfsplus_bnode_unhash
>  #define hfs_bnode_free hfsplus_bnode_free
>  #define hfs_bnode_create hfsplus_bnode_create
> @@ -384,6 +386,8 @@ void hfs_bnode_unlink(struct hfs_bnode *node);
>  struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32
> cnid);
>  void hfs_bnode_unhash(struct hfs_bnode *node);
>  struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num);
> +struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node);
> +struct hfs_bnode *hfs_bnode_find_prev(struct hfs_bnode *node);
>  void hfs_bnode_free(struct hfs_bnode *node);
>  struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num);
>  void hfs_bnode_get(struct hfs_bnode *node);

Sorry, I am going to reject your patch. Because, you cannot write and
test all this code alone for several hours. I can ask my Claude AI
agent to fix all of these issues. Why do I need your patches then?
Don't get me wrong. I am not against of using AI to help us to fix
bugs, to learn and to be more productive. But kernel work is not about
asking AI to generate something without any understanding of this.

Thanks,
Slava.