Re: [PATCH v2] hfs: clamp catalog key name lengths during comparison

From: Viacheslav Dubeyko

Date: Fri Dec 04 2026 - 17:34:55 EST


On Fri, 2026-12-04 at 15:35 +0900, Kyuwon Cho wrote:
> The catalog key comparison trusts the embedded Pascal string lengths.
> A corrupt length can make hfs_strcmp() read beyond the fixed-size
> name
> arrays, including on paths that copy keys through
> hfs_bnode_read_key()
> without calling hfs_brec_keylen().
>
> Clamp both operands at the format's HFS_NAMELEN limit at the
> comparison
> boundary. This also preserves valid fixed-size catalog index keys,
> whose
> outer key length does not describe the embedded name length exactly.

Thanks a lot for the quick v2.

Could you please confirm that the reproducer doesn't trigger the KASAN
report anymore with v2 applied?

Also, I think the KASAN trace from v1 is worth keeping in the commit
message, because it helps people searching for the same crash to find
the fix.

The bug goes back to the initial import (Fixes: 1da177e4c3f4), and the
fix is small and self-contained, so a Cc: stable@xxxxxxxxxxxxxxx makes
sense.

>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Suggested-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
> Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
> ---
> Changes in v2:
> - Replace the record-size equality check with Viacheslav Dubeyko's
>   suggested comparison-boundary fix.
> - Clamp both embedded name lengths to HFS_NAMELEN, covering keys read
>   through hfs_bnode_read_key() while preserving fixed-size index
> keys.
>
> v1:
> https://lore.kernel.org/all/179619831256.1663618.8521911735854986516@xxxxxxxx/
>
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> Tested with a multi-leaf catalog created by hformat; mounting,
> listing,
> and looking up files continue to work.
>
> The reproducer is attached to my reply in the v1 thread.
>
>  fs/hfs/catalog.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/fs/hfs/catalog.c b/fs/hfs/catalog.c
> index 76924783b136..ef9787d92f5d 100644
> --- a/fs/hfs/catalog.c
> +++ b/fs/hfs/catalog.c
> @@ -171,6 +171,7 @@ int hfs_cat_create(u32 cnid, struct inode *dir,
> const struct qstr *str, struct i
>  int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
>  {
>   __be32 k1p, k2p;
> + unsigned int len1, len2;
>  
>   k1p = key1->cat.ParID;
>   k2p = key2->cat.ParID;
> @@ -178,8 +179,10 @@ int hfs_cat_keycmp(const btree_key *key1, const
> btree_key *key2)
>   if (k1p != k2p)
>   return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
>  
> - return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
> -   key2->cat.CName.name, key2-
> >cat.CName.len);
> + len1 = min_t(unsigned int, key1->cat.CName.len,
> HFS_NAMELEN);
> + len2 = min_t(unsigned int, key2->cat.CName.len,
> HFS_NAMELEN);

min_t(unsigned int, ...) is fine; plain min() would also work with
today's type checks.

I am guessing... We know here that Catalog file record is corrupted.
Should we inform about corruption and recommend to run FSCK tool?

Thanks,
Slava.

> + return hfs_strcmp(key1->cat.CName.name, len1,
> +   key2->cat.CName.name, len2);
>  }
>  
>  static int hfs_cat_validate_found_cnid(struct hfs_find_data *fd, u32
> cnid)