Re: [PATCH] hfsplus: bound hfs_bnode_read_key() against the per-tree key buffer size
From: Viacheslav Dubeyko
Date: Mon Nov 16 2026 - 15:57:34 EST
On Sat, 2026-11-14 at 18:00 +0800, Guikun Yang wrote:
> hfs_find_init() sizes the per-tree key buffers from max_key_len:
>
> ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
>
> which is 24 bytes for the extents tree (max_key_len 10) and 1036 for
> the catalog tree. hfs_bnode_read_key() however bounds both the copy
> and the reject-path memset against the generic key union:
>
> if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
> memset(key, 0, sizeof(hfsplus_btree_key));
> pr_err("hfsplus: Invalid key length: %u\n",
> key_len);
> return;
> }
>
> hfs_bnode_read(node, key, off, key_len);
>
> sizeof(hfsplus_btree_key) is 518 (the catalog key size; the union of
> cat/ext/attr keys). For the extents tree the bound is larger than the
> 24-byte buffer it protects, so the check is inert there: an on-disk
> key length between 10 and 516 passes it and the memcpy overflows the
> allocation, and a "rejected" key length takes the memset path, which
> writes 518 bytes into the same 24-byte allocation. Both paths were
> observed as KASAN slab-out-of-bounds writes on the writeback path of
> a crafted image:
>
> BUG: KASAN: slab-out-of-bounds in hfsplus_bnode_read+0x30/0x5f0
> Write of size 502 at addr ... by task init/1
> hfsplus_bnode_read -> hfs_bnode_read_key
> hfs_brec_insert -> __hfsplus_ext_write_extent
> hfsplus_ext_write_extent -> hfsplus_write_inode ->
> hfsplus_file_fsync
> allocated 24-byte region (kmalloc-32) by hfsplus_find_init
>
> Bound both the copy and the memset against the actual buffer size,
> tree->max_key_len + 2, which is the natural limit for every tree
> (the extents tree can never see a key above HFSPLUS_EXT_KEYLEN on a
> well-formed volume; the catalog and attribute trees already allocate
> buffers that cover their full key sizes).
>
> Tested with the reproducer from the bug report: the extents-tree
> image that previously wrote 502 bytes into the 24-byte allocation now
> fails the bound and logs "Invalid key length"; the negative control
> (NUL-terminated short key) still parses and writes normally.
>
> Reported-by: Guikun Yang <yangguikun19@xxxxxx>
> Signed-off-by: Guikun Yang <yangguikun19@xxxxxx>
> ---
> diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
> index 3a1a1fa6f..0774b945c 100644
> --- a/fs/hfsplus/bnode.c
> +++ b/fs/hfsplus/bnode.c
> @@ -84,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
> void *key, u32 off)
> else
> key_len = tree->max_key_len + 2;
>
> - if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
> - memset(key, 0, sizeof(hfsplus_btree_key));
> + if (key_len > tree->max_key_len + 2 || key_len < 1) {
> + memset(key, 0, tree->max_key_len + 2);
I still don't follow what is the problem you are trying to fix. And you
fix it in much worse way. Before we had sizeof(hfsplus_btree_key) and
now you suggested tree->max_key_len + 2. I dislike the hardcoded
values. I tried to understand your explanation of the problem but the
explanation doesn't make sense to me.
Usually, we call hfs_bnode_read_key() for struct hfs_find_data and it
contains hfsplus_btree_key:
typedef union {
__be16 key_len;
struct hfsplus_cat_key cat;
struct hfsplus_ext_key ext;
struct hfsplus_attr_key attr;
} __packed hfsplus_btree_key;
So, we should have enough memory for executing:
memset(key, 0, sizeof(hfsplus_btree_key));
So, what's the problem are you trying to fix? Why tree->max_key_len + 2
is better? What if volume contains corrupted max_key_len?
Thanks,
Slava.
> pr_err("hfsplus: Invalid key length: %u\n",
> key_len);
> return;
> }