Re: [PATCH] hfsplus: bound hfs_bnode_read_key() against the per-tree key buffer size
From: Viacheslav Dubeyko
Date: Tue Nov 17 2026 - 17:22:38 EST
On Mon, 2026-11-16 at 22:30 +0800, Guikun Yang wrote:
> Hi Slava,
>
> The mismatch is between the declared type of fd->search_key and the
> size it is actually allocated with.
>
> hfs_find_init() allocates the key buffers from the per-tree key
> length, not from the union size (fs/hfsplus/bfind.c:22):
>
> ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
> fd->search_key = ptr;
> fd->key = ptr + tree->max_key_len + 2;
>
> So search_key holds max_key_len + 2 bytes. For the extents tree,
> max_key_len is forced to HFSPLUS_EXT_KEYLEN - sizeof(u16) = 10 at
> mount time (btree.c:318), which gives search_key a 12-byte buffer.
> The declared pointer type hfsplus_btree_key * suggests 518 bytes,
> but only 12 bytes are actually there.
>
> Now in hfs_bnode_read_key(), the existing bound is the union size
> (bnode.c:87):
>
> if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
> memset(key, 0, sizeof(hfsplus_btree_key));
> ...
> }
> hfs_bnode_read(node, key, off, key_len);
>
> key_len is attacker-controlled in the leaf/VARIDXKEYS/attr branch
> (u16 read from the node plus 2). Anything up to 518 passes this
> check, and hfs_bnode_read() writes it into the search_key of an
> extents-tree hfs_find_data (a 24-byte kzalloc total, of which
> search_key is the first 12). That is the slab-out-of-bounds write
> from my report:
>
> 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
> ...
> Allocated by task 1:
> hfs_find_init (kzalloc 24)
>
> reached via hfs_brec_insert() -> hfs_bnode_read_key(new_node,
> fd->search_key, 14) on an extents tree (brec.c:169); the parent-key
> update path at brec.c:151 reads into the same buffer the same way.
>
> So memset(key, 0, sizeof(hfsplus_btree_key)) is unsafe in this
> function too: the 518-byte clear runs off the same 12-byte
> search_key, which is why the patch bounds both the read and the
> clear with the same expression.
>
> About "what if max_key_len is corrupted": it cannot reach the tree
> at all. hfs_btree_open() validates it against fixed constants per
> tree id (btree.c:318/331/350):
>
> case HFSPLUS_EXT_CNID:
> if (tree->max_key_len != HFSPLUS_EXT_KEYLEN -
> sizeof(u16))
> goto fail_page;
> case HFSPLUS_CAT_CNID:
> if (tree->max_key_len != HFSPLUS_CAT_KEYLEN -
> sizeof(u16))
> goto fail_page;
> case HFSPLUS_ATTR_CNID:
> if (tree->max_key_len != HFSPLUS_ATTR_KEYLEN -
> sizeof(u16))
> goto fail_page;
>
> A corrupted max_key_len fails the mount. At runtime it is always
> 10, 516 or 266. And the bound stays consistent even in the
> hypothetical case: search_key is allocated from max_key_len itself,
> so a bound derived from max_key_len can never exceed the buffer,
> whatever the value is.
>
> That is also why I would argue max_key_len + 2 is the opposite of a
> hardcoded value: sizeof(hfsplus_btree_key) = 518 is the constant
> that has no relation to the allocation - it matches the catalog
> tree only by coincidence (516 + 2) and overshoots extents by 506
> bytes and attributes by 250 bytes. max_key_len + 2 is the only
> expression that tracks the kzalloc() in hfs_find_init() one to one.
>
> Both sizes in the patch come from the same source: the check
> (key_len > max_key_len + 2) and the memset use the same expression
> as the allocation. If you prefer to avoid recomputing it, storing
> key_buf_len = max_key_len + 2 in struct hfs_btree at mount time
> would make the correspondence explicit - happy to rework in that
> shape if you prefer.
>
The real problem is in function declaration:
void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
We should not assume the capacity of buffer but we should be informed
about it. Probably, we need to have such declaration:
void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off, u32
key_size)
For any operations with b-tree, we need to prepare the struct
hfs_find_data:
struct hfs_find_data {
/* filled by caller */
hfsplus_btree_key *search_key;
hfsplus_btree_key *key;
/* filled by find */
struct hfs_btree *tree;
struct hfs_bnode *bnode;
/* filled by findrec */
int record;
int keyoffset, keylength;
int entryoffset, entrylength;
};
So, we need to store the key capacity size in this structure during
hfs_find_init() call. And, then we need to provide this value for every
hfs_bnode_read_key() call. And if we can rework likewise constructions
tree->max_key_len * 2 + 4, it will be really great too.
Does it makes sense for you?
Thanks,
Slava.