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; 2+ 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] 2+ 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
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-30 20:38 UTC | newest]

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

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®