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. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Suggested-by: Viacheslav Dubeyko Signed-off-by: Kyuwon Cho --- 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@skku.edu/ 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); + 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