mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [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; 4+ 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] 4+ 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; 4+ 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] 4+ 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; 4+ 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] 4+ 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
  0 siblings, 0 replies; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2026-12-02  0:26 UTC | newest]

Thread overview: 4+ 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

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®