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. Report the catalog corruption and recommend fsck.hfs when a length exceeds that limit. This preserves valid fixed-size catalog index keys, whose outer key length does not describe the embedded name length exactly. The malformed-volume reproducer reports: 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 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) Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Suggested-by: Viacheslav Dubeyko Cc: stable@vger.kernel.org Signed-off-by: Kyuwon Cho --- Changes in v3: - Report overlong catalog keys with a ratelimited corruption message and recommend fsck.hfs, as suggested by Viacheslav Dubeyko. - Include the decoded KASAN trace and add Cc: stable@vger.kernel.org. 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. Before (decoded with scripts/decode_stacktrace.sh; unreliable frames removed): BUG: KASAN: slab-out-of-bounds in hfs_strcmp (fs/hfs/string.c:87) Read of size 1 in hfs_strcmp 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) do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) After (the v2 fix, retained here): no sanitizer trace was emitted and the same reproducer completed cleanly. The new corruption diagnostic was also observed. v1: https://lore.kernel.org/all/179619831256.1663618.8521911735854986516@skku.edu/ v2: https://lore.kernel.org/all/179636379461.1106205.15423253231491526241@skku.edu/ Review reply: https://lore.kernel.org/all/179643157798.1720718.7241695804078445905@skku.edu/ The issue was found by an LLM-guided test generator that we are developing as a research prototype. A reproducer is available on request. fs/hfs/catalog.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fs/hfs/catalog.c b/fs/hfs/catalog.c index 76924783b136..5235189566bf 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,14 @@ 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); + if (key1->cat.CName.len > HFS_NAMELEN || + key2->cat.CName.len > HFS_NAMELEN) + pr_err_ratelimited("corrupted catalog key, run fsck.hfs\n"); + + len1 = min_t(unsigned int, key1->cat.CName.len, HFS_NAMELEN); + len2 = min_t(unsigned int, key2->cat.CName.len, HFS_NAMELEN); + 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) -- 2.43.0