* [PATCH] hfsplus: bound B-tree map node traversal
@ 2026-11-28 8:28 shaos
2026-11-30 20:38 ` Viacheslav Dubeyko
0 siblings, 1 reply; 6+ messages in thread
From: shaos @ 2026-11-28 8:28 UTC (permalink / raw)
To: slava; +Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
Hello,
hfs_bmap_alloc() follows the next pointer of each B-tree map node without
checking for a cycle or an upper bound. A crafted HFS+ image can make a
full map node point to itself. Mounting the image then loops in the
allocation-map scan and consumes CPU; the mount task remains alive after
SIGKILL.
The reproducer creates the image and tests a self-cycle, a non-cyclic
full map, and a normal map. The cycle hung in two vulnerable runs. The
non-cyclic full-map fixture returned -ENOMEM when it reached node 8192,
without hanging; the normal-map control mounted successfully. With this
patch, mounting the cycle returns -EIO promptly, and the normal-map
control still mounts successfully.
The issue requires a privileged writable mount of a crafted HFS+ image.
It demonstrates a local denial of service, not memory corruption or
privilege escalation.
Tested source revision:
HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4
compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0,
GNU ld 2.38
console output:
https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/131205d3de25b8c5fe42864362c6e2e1009f90a1/console-output.txt
kernel config:
https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d63b446bf570fc993d04b3ffeae979b3db4a6eec/kernel-config.txt
reproducer:
https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d734030b8907670c9aefd748bd6663d894d828a2/poc.c
initramfs init:
https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/ce28f3f2f53dce541ad5cb7f5b110a378af9e2e6/init
patched-kernel config:
https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d204ab19c4a8824c83293b94741f80cc0fa52b0a/patched-kernel-config.txt
Build the kernel at the tested revision with the attached configuration.
Build poc.c statically, place it at /poc in a minimal initramfs, and use
the supplied init script as /init. The initramfs needs a static BusyBox
at /bin/busybox. Run the cycle case with:
gcc -O2 -Wall -Wextra -static -o poc poc.c
mkdir -p rootfs/bin
cp poc rootfs/poc
cp init rootfs/init
cp <static-busybox> rootfs/bin/busybox
chmod 0755 rootfs/init rootfs/poc rootfs/bin/busybox
(cd rootfs && find . -print0 | cpio --null -o -H newc) |
gzip -9 > initramfs.cpio.gz
qemu-system-x86_64 -m 2G -smp 2 \
-kernel <linux-tree>/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-append 'console=ttyS0 rdinit=/init pocmode=cycle lsm=landlock' \
-nographic -monitor none -no-reboot
The init script runs /poc with the value of pocmode. Repeat with
pocmode=normal for the successful mount control. The no-cycle fixture
is also available as pocmode=nocycle.
Bound traversal by both the B-tree header's node count and the number of
complete nodes that fit in the tree file. This prevents a malformed
header from permitting an unbounded walk:
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: shaos <shawnq1@163.com>
---
fs/hfsplus/btree.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
index bd4dbdbc8f98c..7aff41420c098 100644
--- a/fs/hfsplus/btree.c
+++ b/fs/hfsplus/btree.c
@@ -534,6 +534,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
struct hfs_bmap_ctx ctx;
struct page *page;
u32 nidx, idx;
+ u32 max_nodes, nodes_seen = 1;
u8 *data, byte, m;
int i, res;
@@ -543,6 +544,9 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
if (res)
return ERR_PTR(res);
+ max_nodes = min_t(u64, tree->node_count,
+ tree->inode->i_size >> tree->node_size_shift);
+
nidx = 0;
node = hfs_bnode_find(tree, nidx);
if (IS_ERR(node))
@@ -592,8 +596,15 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
hfs_dbg("create new bmap node\n");
next_node = hfs_bmap_new_bmap(node, idx);
hfs_btree_write(tree);
- } else
+ } else {
+ if (nodes_seen >= max_nodes) {
+ pr_err("invalid btree map node chain\n");
+ hfs_bnode_put(node);
+ return ERR_PTR(-EIO);
+ }
+ nodes_seen++;
next_node = hfs_bnode_find(tree, nidx);
+ }
hfs_bnode_put(node);
if (IS_ERR(next_node))
return next_node;
--
2.34.1
Thanks,
shaos
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] hfsplus: bound B-tree map node traversal
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
0 siblings, 1 reply; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-11-30 20:38 UTC (permalink / raw)
To: shaos; +Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
On Sat, 2026-11-28 at 08:28 +0000, shaos wrote:
> Hello,
>
> hfs_bmap_alloc() follows the next pointer of each B-tree map node
> without
> checking for a cycle or an upper bound. A crafted HFS+ image can
> make a
> full map node point to itself. Mounting the image then loops in the
> allocation-map scan and consumes CPU; the mount task remains alive
> after
> SIGKILL.
>
> The reproducer creates the image and tests a self-cycle, a non-cyclic
> full map, and a normal map. The cycle hung in two vulnerable runs.
> The
> non-cyclic full-map fixture returned -ENOMEM when it reached node
> 8192,
> without hanging; the normal-map control mounted successfully. With
> this
> patch, mounting the cycle returns -EIO promptly, and the normal-map
> control still mounts successfully.
>
> The issue requires a privileged writable mount of a crafted HFS+
> image.
> It demonstrates a local denial of service, not memory corruption or
> privilege escalation.
>
> Tested source revision:
> HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
> git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
> source version: Linux 7.4.0-rc4
> compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0,
> GNU ld 2.38
>
> console output:
> https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/131205d3de25b8c5fe42864362c6e2e1009f90a1/console-output.txt
> kernel config:
> https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d63b446bf570fc993d04b3ffeae979b3db4a6eec/kernel-config.txt
> reproducer:
> https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d734030b8907670c9aefd748bd6663d894d828a2/poc.c
> initramfs init:
> https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/ce28f3f2f53dce541ad5cb7f5b110a378af9e2e6/init
> patched-kernel config:
> https://gist.githubusercontent.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e/raw/d204ab19c4a8824c83293b94741f80cc0fa52b0a/patched-kernel-config.txt
>
> Build the kernel at the tested revision with the attached
> configuration.
> Build poc.c statically, place it at /poc in a minimal initramfs, and
> use
> the supplied init script as /init. The initramfs needs a static
> BusyBox
> at /bin/busybox. Run the cycle case with:
>
> gcc -O2 -Wall -Wextra -static -o poc poc.c
> mkdir -p rootfs/bin
> cp poc rootfs/poc
> cp init rootfs/init
> cp <static-busybox> rootfs/bin/busybox
> chmod 0755 rootfs/init rootfs/poc rootfs/bin/busybox
> (cd rootfs && find . -print0 | cpio --null -o -H newc) |
> gzip -9 > initramfs.cpio.gz
>
> qemu-system-x86_64 -m 2G -smp 2 \
> -kernel <linux-tree>/arch/x86/boot/bzImage \
> -initrd initramfs.cpio.gz \
> -append 'console=ttyS0 rdinit=/init pocmode=cycle
> lsm=landlock' \
> -nographic -monitor none -no-reboot
>
> The init script runs /poc with the value of pocmode. Repeat with
> pocmode=normal for the successful mount control. The no-cycle
> fixture
> is also available as pocmode=nocycle.
>
> Bound traversal by both the B-tree header's node count and the number
> of
> complete nodes that fit in the tree file. This prevents a malformed
> header from permitting an unbounded walk:
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: shaos <shawnq1@163.com>
> ---
> fs/hfsplus/btree.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
> index bd4dbdbc8f98c..7aff41420c098 100644
> --- a/fs/hfsplus/btree.c
> +++ b/fs/hfsplus/btree.c
> @@ -534,6 +534,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree
> *tree)
> struct hfs_bmap_ctx ctx;
> struct page *page;
> u32 nidx, idx;
> + u32 max_nodes, nodes_seen = 1;
> u8 *data, byte, m;
> int i, res;
>
> @@ -543,6 +544,9 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree
> *tree)
> if (res)
> return ERR_PTR(res);
>
> + max_nodes = min_t(u64, tree->node_count,
> + tree->inode->i_size >> tree-
> >node_size_shift);
> +
> nidx = 0;
> node = hfs_bnode_find(tree, nidx);
> if (IS_ERR(node))
> @@ -592,8 +596,15 @@ struct hfs_bnode *hfs_bmap_alloc(struct
> hfs_btree *tree)
> hfs_dbg("create new bmap node\n");
> next_node = hfs_bmap_new_bmap(node, idx);
> hfs_btree_write(tree);
> - } else
> + } else {
> + if (nodes_seen >= max_nodes) {
> + pr_err("invalid btree map node
> chain\n");
> + hfs_bnode_put(node);
> + return ERR_PTR(-EIO);
> + }
> + nodes_seen++;
> next_node = hfs_bnode_find(tree, nidx);
> + }
> hfs_bnode_put(node);
> if (IS_ERR(next_node))
> return next_node;
If we have a corrupted HFS+ volume (Allocation File corruption here),
then we should check this during the volume mount. This fix has the
wrong place. If volume corrupted, then we cannon operate with the
volume. We should mount the corrupted volume in RO mode or reject to
mount it for the case of severe corruption and to recommend to run fsck
tool.
Thanks,
Slava.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] hfsplus: validate B-tree map chain at mount
2026-11-30 20:38 ` Viacheslav Dubeyko
@ 2026-12-01 14:18 ` Shawn Shao
2026-12-02 0:26 ` Viacheslav Dubeyko
0 siblings, 1 reply; 6+ messages in thread
From: Shawn Shao @ 2026-12-01 14:18 UTC (permalink / raw)
To: Viacheslav Dubeyko
Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
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 <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;
+}
+
+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");
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] hfsplus: validate B-tree map chain at mount
2026-12-01 14:18 ` [PATCH v2] hfsplus: validate B-tree map chain at mount Shawn Shao
@ 2026-12-02 0:26 ` Viacheslav Dubeyko
2026-12-02 7:56 ` [PATCH v3] " Shawn Shao
0 siblings, 1 reply; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-12-02 0:26 UTC (permalink / raw)
To: Shawn Shao
Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
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");
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3] hfsplus: validate B-tree map chain at mount
2026-12-02 0:26 ` Viacheslav Dubeyko
@ 2026-12-02 7:56 ` Shawn Shao
2026-12-02 21:03 ` Viacheslav Dubeyko
0 siblings, 1 reply; 6+ messages in thread
From: Shawn Shao @ 2026-12-02 7:56 UTC (permalink / raw)
To: slava; +Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
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@163.com>
---
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);
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3] hfsplus: validate B-tree map chain at mount
2026-12-02 7:56 ` [PATCH v3] " Shawn Shao
@ 2026-12-02 21:03 ` Viacheslav Dubeyko
0 siblings, 0 replies; 6+ messages in thread
From: Viacheslav Dubeyko @ 2026-12-02 21:03 UTC (permalink / raw)
To: Shawn Shao
Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
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@163.com>
>
> ---
> 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.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 21:03 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2026-12-02 7:56 ` [PATCH v3] " Shawn Shao
2026-12-02 21:03 ` Viacheslav Dubeyko
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®