[PATCH] hfs: validate catalog key name length
From: Kyuwon Cho
Date: Thu Dec 03 2026 - 11:53:24 EST
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.
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) {
+ 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);
+ 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) {
+ pr_err("invalid catalog name length\n");
+ return 0;
+ }
+ }
return retval;
}
--
2.43.0