Re: [PATCH] hfs: validate catalog key name length

From: Viacheslav Dubeyko

Date: Thu Dec 03 2026 - 18:48:53 EST


On Fri, 2026-12-04 at 01:53 +0900, Kyuwon Cho wrote:
> A catalog key carries both an outer key length and an embedded Pascal
> string length. hfs_brec_keylen() validates the former, but
> hfs_cat_keycmp() trusts the latter when comparing names. A corrupted
> catalog can therefore make hfs_strcmp() read beyond the key buffer.
>
> Require the two lengths to describe the same key before copying it
> out
> of the B-tree node.

Thanks for the report. The issue is real: hfs_strcmp() doesn't limit
the length at all, while the key buffers have room for HFS_NAMELEN (31)
characters only. However, this patch breaks valid HFS volumes. Please,
see my comments below.

>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
> ---
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> The reproducer creates and mounts a crafted HFS image, then resumes a
> directory read at a catalog record whose embedded name length exceeds
> the key. Mounting the image requires root in the initial namespace.
>
> The decoded trace is:
>
>   BUG: KASAN: slab-out-of-bounds in hfs_strcmp (fs/hfs/string.c:87)
>   Read of size 1 at addr ff11000011bd1ace by task test/115
>   Call Trace:
>    hfs_strcmp (fs/hfs/string.c:87)
>    __hfs_brec_find (fs/hfs/bfind.c:78)
>    hfs_brec_find (fs/hfs/bfind.c:147)
>    hfs_readdir (fs/hfs/dir.c:103)
>    iterate_dir (fs/readdir.c:114)
>    __x64_sys_getdents64 (fs/readdir.c:404 fs/readdir.c:389)
>
> The patch was tested on v7.4-rc5 and rebased onto the HFS tree commit
> a2c27efbbd43 ("hfsplus: fix UAF in hfsplus_cat_write_inode()").
>
>  fs/hfs/brec.c | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
>
> diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c
> index 1ca1ee9f73ef..aa6ac734e926 100644
> --- a/fs/hfs/brec.c
> +++ b/fs/hfs/brec.c
> @@ -59,6 +59,23 @@ u16 hfs_brec_keylen(struct hfs_bnode *node, u16
> rec)
>   }
>   }
>   }
> + if (node->tree->cnid == HFS_CAT_CNID) {

We check this multiple times in multiple places. So, we need a spacial
helper(s) fir likewise check.

This check is executed for index nodes too. But HFS catalog index keys
have fixed size: the key length byte is always max_key_len (37),
independently of the name length. Mac OS creates the index keys this
way, and so does the Linux driver:

fs/hfs/brec.c, hfs_brec_insert():
fd->search_key->key_len = tree->max_key_len;

fs/hfs/brec.c, hfs_btree_inc_height():
hfs_bnode_write_u8(new_node, 14, tree->max_key_len);

So, for any index record with a name shorter than 31 characters, the
check below fails, hfs_brec_keylen() returns 0, and __hfs_brec_find()
returns -EINVAL. As a result, any catalog lookup that goes through an
index node will fail. It means that any volume whose catalog has more
than one leaf node becomes unusable.

The existing code already handles fixed-size index keys in the first
branch of hfs_brec_keylen(), but the new check ignores it.

> + u8 key_len, name_len;
> +
> + recoff = hfs_bnode_read_u16(node, node->tree-
> >node_size -
> +     (rec + 1) * 2);
> + if (!recoff)
> + return 0;
> +
> + key_len = hfs_bnode_read_u8(node, recoff);

The code above this check distinguishes HFS_TREE_BIGKEYS, but here the
key length is always read as u8. A crafted header with BIGKEYS set
for the catalog tree makes this read the wrong byte.

Also, the record length is not checked before reading CName.len.

> + name_len = hfs_bnode_read_u8(node, recoff +
> + offsetof(struct hfs_cat_key,
> CName.len));
> + if (key_len != offsetof(struct hfs_cat_key,
> CName.name) -
> + sizeof(u8) + name_len) {

Even for leaf records, equality is stricter than we need. For memory
safety it is enough that the name fits into the key (name_len <=
key_len - 6) and name_len <= HFS_NAMELEN. I would prefer not to reject
old volumes because of the key padding.

> + pr_err("invalid catalog name length\n");

Could you please make the message more informative (node ID, record
index, key and name lengths)?

> + return 0;
> + }
> + }
>   return retval;
>  }
>  

Moreover, as with the HFS+ patch, hfs_brec_keylen() is not the only
path that copies a key from a b-tree node. hfs_bnode_read_key() reads
keys into fd->search_key in hfs_brec_insert() and
hfs_brec_update_parent() without calling hfs_brec_keylen(), and
hfs_cat_keycmp() trusts the name length of fd->search_key too.

I think the simplest and most robust fix for HFS is to limit both
name lengths to HFS_NAMELEN in hfs_cat_keycmp(), the same way as
hfs_hash_dentry() already does. The fd->key and fd->search_key buffers
are always max_key_len + 2 bytes long, so the comparison will never
go beyond the buffer, independently of which path has filled it in.
It doesn't affect fixed-size index keys at all. If we would like to
report such records as corrupted, then an additional check
(name_len <= key_len - 6) could be added for leaf records only.

And could you please share the reproducer (or the crafted image)?

Thanks,
Slava.