mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viacheslav Dubeyko <slava@dubeyko.com>
To: shaos <shawnq1@163.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,
	Yangtao Li <frank.li@vivo.com>,
	linux-fsdevel@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hfsplus: bound B-tree map node traversal
Date: Mon, 30 Nov 2026 12:38:38 -0800	[thread overview]
Message-ID: <26bf2beb0e958385812405d43eed469333f31b94.camel@dubeyko.com> (raw)
In-Reply-To: <20261128082828.3075144-1-shawnq1@163.com>

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.

      reply	other threads:[~2026-11-30 20:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28  8:28 shaos
2026-11-30 20:38 ` Viacheslav Dubeyko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=26bf2beb0e958385812405d43eed469333f31b94.camel@dubeyko.com \
    --to=slava@dubeyko.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shawnq1@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®