From: Viacheslav Dubeyko <slava@dubeyko.com>
To: Shawn Shao <shawnq1@163.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,
Yangtao Li <frank.li@vivo.com>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] hfsplus: validate B-tree map chain at mount
Date: Tue, 01 Dec 2026 16:26:27 -0800 [thread overview]
Message-ID: <d76c3f22c1a14028051b46d4efb3d8c118ec693b.camel@dubeyko.com> (raw)
In-Reply-To: <179613469562.187341.18363138064359075616.1796134695.6-1-shawnq1@163.com>
On Tue, 2026-12-01 at 14:18 +0000, Shawn Shao wrote:
> Hello Slava,
>
> Thanks for the review.
Ditto. Please, send it as formal patch.
> 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 <shawnq1@163.com>
> ---
> 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;
> +}
>
Frankly speaking, it will be good to make a cleanup. We need
function(s) of getting next/prev node that can be used in multiple
places. Something like this:
diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c
index ca9813f58a6d..d91fd95f853b 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 3a1a1fa6f2e2..82705b6f5597 100644
--- a/fs/hfsplus/bnode.c
+++ b/fs/hfsplus/bnode.c
@@ -399,7 +399,7 @@ void hfs_bnode_unlink(struct hfs_bnode *node)
tree = node->tree;
if (node->prev) {
- tmp = hfs_bnode_find(tree, node->prev);
+ tmp = hfs_bnode_find_prev(node);
if (IS_ERR(tmp))
return;
tmp->next = node->next;
@@ -411,7 +411,7 @@ void hfs_bnode_unlink(struct hfs_bnode *node)
tree->leaf_head = node->next;
if (node->next) {
- tmp = hfs_bnode_find(tree, node->next);
+ tmp = hfs_bnode_find_next(node);
if (IS_ERR(tmp))
return;
tmp->prev = node->prev;
@@ -618,6 +618,89 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree
*tree, u32 num)
return ERR_PTR(-EIO);
}
+/*
+ * Leaf and index nodes are linked in both directions to the nodes of
the
+ * same type and height. The header node and the map nodes form a
chain
+ * through their next links only: hfs_bmap_new_bmap() leaves prev at
0.
+ */
+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;
+
+ if (!link)
+ return NULL;
+
+ if (link == node->this || link >= tree->node_count)
+ 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 (!next || sibling->type != HFS_NODE_MAP)
+ 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 %d) in b-tree
%u\n",
+ next ? "next" : "prev", link, node->this, node->type,
+ tree->cnid);
+ return ERR_PTR(-EIO);
+}
+
+/**
+ * hfs_bnode_find_next - get the node that follows a node at its level
+ * @node: the node, the caller holds a reference to it
+ *
+ * Follow the next link of @node and check that the node found is the
one
+ * that can follow @node: a leaf or index node at the same height that
links
+ * back to @node, or a map node if @node is the header node or a map
node.
+ *
+ * Return: the next node with a reference held, NULL if @node is the
last
+ * node of its level, or an ERR_PTR() on error.
+ */
+struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node)
+{
+ return hfs_bnode_find_sibling(node, true);
+}
+
+/**
+ * hfs_bnode_find_prev - get the node that precedes a node at its
level
+ * @node: the leaf or index node, the caller holds a reference to it
+ *
+ * Follow the prev link of @node and check that the node found is a
node
+ * of the same type and height that links back to @node.
+ *
+ * Return: the previous node with a reference held, NULL if @node is
the
+ * first node of its level, or an ERR_PTR() on error.
+ */
+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 f416e562d439..56b694c2365b 100644
--- a/fs/hfsplus/brec.c
+++ b/fs/hfsplus/brec.c
@@ -275,11 +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);
hfs_bnode_put(new_node);
diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
index 0a9b32d5c5bf..9d9181b275e2 100644
--- a/fs/hfsplus/btree.c
+++ b/fs/hfsplus/btree.c
@@ -593,13 +593,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;
@@ -638,30 +637,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 dd8f7179431a..adb0216b7db7 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -331,6 +331,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
@@ -415,6 +417,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);
> +
> +static int hfs_bmap_check_map_chain(struct hfs_btree *tree)
> +{
> + u32 max_nodes, slow = HFSPLUS_TREE_HEAD, fast =
> HFSPLUS_TREE_HEAD;
What these fast and slow mean?
> + 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;
Logic looks not clear and I don't quite follow top the logic. Please,
rework it to be more simple and clear.
> +
> + 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)) {
I think we already need a dedicated function for all corruption check.
Please, collect all checks there.
Thanks,
Slava.
> 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");
prev parent reply other threads:[~2026-12-02 0:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 8:28 [PATCH] hfsplus: bound B-tree map node traversal shaos
2026-11-30 20:38 ` Viacheslav Dubeyko
2026-12-01 14:18 ` [PATCH v2] hfsplus: validate B-tree map chain at mount Shawn Shao
2026-12-02 0:26 ` Viacheslav Dubeyko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d76c3f22c1a14028051b46d4efb3d8c118ec693b.camel@dubeyko.com \
--to=slava@dubeyko.com \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shawnq1@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®