* [PATCH v2] hfs: clamp catalog key name lengths during comparison
@ 2026-12-04 6:35 Kyuwon Cho
2026-12-04 22:34 ` Viacheslav Dubeyko
0 siblings, 1 reply; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-04 6:35 UTC (permalink / raw)
To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li
Cc: linux-fsdevel, linux-kernel
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 <slava@dubeyko.com>
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] hfs: clamp catalog key name lengths during comparison
2026-12-04 6:35 [PATCH v2] hfs: clamp catalog key name lengths during comparison Kyuwon Cho
@ 2026-12-04 22:34 ` Viacheslav Dubeyko
2026-12-05 1:46 ` Kyuwon Cho
0 siblings, 1 reply; 3+ messages in thread
From: Viacheslav Dubeyko @ 2026-12-04 22:34 UTC (permalink / raw)
To: Kyuwon Cho, John Paul Adrian Glaubitz, Yangtao Li
Cc: linux-fsdevel, linux-kernel
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@vger.kernel.org makes
sense.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Suggested-by: Viacheslav Dubeyko <slava@dubeyko.com>
> Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
> ---
> 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);
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)
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] hfs: clamp catalog key name lengths during comparison
2026-12-04 22:34 ` Viacheslav Dubeyko
@ 2026-12-05 1:46 ` Kyuwon Cho
0 siblings, 0 replies; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-05 1:46 UTC (permalink / raw)
To: Viacheslav Dubeyko
Cc: John Paul Adrian Glaubitz, Yangtao Li, linux-fsdevel, linux-kernel
On Fri, Dec 04, 2026 at 02:34:42PM -0800, Viacheslav Dubeyko wrote:
> 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.
I ran the exact v1 reproducer three times on the v2 KASAN kernel. All three
runs completed without a KASAN report. The valid multi-leaf catalog test also
completed normally.
I will keep the KASAN trace in the v3 commit message, add Cc: stable, and add
a ratelimited corruption message when an embedded name length exceeds
HFS_NAMELEN so a damaged catalog does not fail silently during comparison.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 1:47 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 6:35 [PATCH v2] hfs: clamp catalog key name lengths during comparison Kyuwon Cho
2026-12-04 22:34 ` Viacheslav Dubeyko
2026-12-05 1:46 ` Kyuwon Cho
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®