[PATCH v2] hfsplus: reorder complete combining runs for dentry keys

From: Shawn Shao

Date: Mon Nov 30 2026 - 02:48:42 EST


Hello,

Thanks for reviewing the patch and reporting the frame-size warnings.

The iterator still reorders a complete combining run. The refill routine is
now kept out of the caller's stack frame, and the KUnit test allocates its
two stored Unicode strings with KUnit-managed memory.

Two equivalent UTF-8 spellings with a 33-mark run could produce different
dentry hashes and comparisons, although HFS+ stores them in the same
canonical order. The regression test checks hashes, comparison, and stored
Unicode forms.

Tested on:
Tested source: v7.3-23903-g7cd8495c0f162
compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
QEMU: QEMU emulator version 6.2.0

The x86-64 bzImage build completed with CONFIG_FRAME_WARN=1280 and
KCFLAGS=-Wframe-larger-than=1280. GCC reports stack usage of 1136 bytes in
hfsplus_compare_dentry(), 64 bytes in the refill routine, and 256 bytes in
the KUnit test. The hfsplus_unicode suite passed all 35 tests. The QEMU
reproducer found the alternate 33-mark spelling immediately after creation
(POS_BAD_AFTER_CREATE=1); the 31-mark control also passed. The unchanged
positive-only reproducer exits 1 on the fixed kernel after printing
ORACLE: NOT CONFIRMED. POS_BAD_AFTER_CREATE=1 confirms the alternate
spelling is found before remount, as expected for the fix. The reproducer
and image inputs are unchanged from v1.

Fixes: bbe5ebcbc006 ("hfsplus: canonicalize decomposed catalog names like fsck_hfs expects")
Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
Changes in v2:
- Keep the refill routine in a separate stack frame to reduce stack usage.
- Allocate the full-size KUnit Unicode buffers dynamically.

fs/hfsplus/unicode.c | 19 +++++-------
fs/hfsplus/unicode_test.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 80 insertions(+), 12 deletions(-)

diff --git a/fs/hfsplus/unicode.c b/fs/hfsplus/unicode.c
index 59b8788a69a9e..840a115609aa0 100644
--- a/fs/hfsplus/unicode.c
+++ b/fs/hfsplus/unicode.c
@@ -577,21 +577,15 @@ int hfsplus_asc2uni(struct super_block *sb,
EXPORT_SYMBOL_IF_KUNIT(hfsplus_asc2uni);

/*
- * Maximum length of a single maximal run of nonzero-combining-class code
- * units that hfsplus_decompose_iter_next() below will canonically
- * reorder. Every code unit with combining class 0 starts a new run, so
- * this only bounds how many *consecutive* combining marks between two
- * base characters get sorted - real text, and even deliberately
- * adversarial "Zalgo" text, essentially never approaches this. Keeping
- * it small means hfsplus_hash_dentry() and hfsplus_compare_dentry() only
- * ever need a tiny amount of lookahead state, rather than buffering an
- * entire (up to 255-unit) name.
+ * A maximal combining run can contain every code unit in an HFS+ catalog
+ * name. Keep the iterator limit aligned with the name limit so it can
+ * reorder the complete run like hfsplus_decompose_str().
*/
-#define HFSPLUS_CCC_RUN_MAX 32
+#define HFSPLUS_CCC_RUN_MAX HFSPLUS_MAX_STRLEN

/*
* Iterator that produces the canonically-ordered, decomposed code units
- * of a Linux name one at a time, without materializing the whole name.
+ * of a Linux name one combining run at a time.
* hfsplus_hash_dentry() and hfsplus_compare_dentry() use this so that
* hashing and comparison always agree with what hfsplus_asc2uni() would
* actually store, while still comparing lazily (stopping at the first
@@ -630,7 +624,8 @@ static void hfsplus_decompose_iter_init(struct hfsplus_decompose_iter *it,
* we know the class of the first unit it produces, so decoding it is
* speculative until that's decided.
*/
-static bool hfsplus_decompose_iter_refill(struct hfsplus_decompose_iter *it)
+static noinline_for_stack bool
+hfsplus_decompose_iter_refill(struct hfsplus_decompose_iter *it)
{
it->run_pos = 0;
it->run_len = 0;
diff --git a/fs/hfsplus/unicode_test.c b/fs/hfsplus/unicode_test.c
index a683c6d5fc221..9a50e3fcb7764 100644
--- a/fs/hfsplus/unicode_test.c
+++ b/fs/hfsplus/unicode_test.c
@@ -1479,6 +1479,78 @@ static void hfsplus_hash_dentry_combining_reorder_test(struct kunit *test)
free_mock_sb(mock_sb);
}

+static void hfsplus_dentry_combining_reorder_long_test(struct kunit *test)
+{
+ static const char acute[] = "\xcc\x81";
+ static const char dot_below[] = "\xcc\x96";
+ enum { NR_ACUTE = 32, NAME_LEN = 1 + (NR_ACUTE + 1) * 2 + 1 };
+ char acute_first[NAME_LEN], dot_first[NAME_LEN];
+ struct hfsplus_unistr *stored1, *stored2;
+ struct test_mock_sb *mock_sb;
+ struct qstr str1, str2;
+ char *p;
+ int i, result;
+
+ mock_sb = setup_mock_sb();
+ KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+ stored1 = kunit_kzalloc(test, sizeof(*stored1), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, stored1);
+ stored2 = kunit_kzalloc(test, sizeof(*stored2), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, stored2);
+
+ setup_mock_dentry(&mock_sb->sb);
+ mock_sb->nls.char2uni = test_char2uni_utf8;
+
+ p = acute_first;
+ *p++ = 'e';
+ for (i = 0; i < NR_ACUTE; i++) {
+ memcpy(p, acute, sizeof(acute) - 1);
+ p += sizeof(acute) - 1;
+ }
+ memcpy(p, dot_below, sizeof(dot_below) - 1);
+ p += sizeof(dot_below) - 1;
+ *p = '\0';
+
+ p = dot_first;
+ *p++ = 'e';
+ memcpy(p, dot_below, sizeof(dot_below) - 1);
+ p += sizeof(dot_below) - 1;
+ for (i = 0; i < NR_ACUTE; i++) {
+ memcpy(p, acute, sizeof(acute) - 1);
+ p += sizeof(acute) - 1;
+ }
+ *p = '\0';
+
+ create_qstr(&str1, acute_first);
+ create_qstr(&str2, dot_first);
+ result = hfsplus_hash_dentry(&test_dentry, &str1);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ result = hfsplus_hash_dentry(&test_dentry, &str2);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ KUNIT_EXPECT_EQ(test, str1.hash, str2.hash);
+
+ KUNIT_EXPECT_EQ(test, 0,
+ hfsplus_compare_dentry(&test_dentry, str1.len,
+ str1.name, &str2));
+
+ result = hfsplus_asc2uni(&mock_sb->sb, stored1, HFSPLUS_MAX_STRLEN,
+ str1.name, str1.len, HFS_REGULAR_NAME);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ result = hfsplus_asc2uni(&mock_sb->sb, stored2, HFSPLUS_MAX_STRLEN,
+ str2.name, str2.len, HFS_REGULAR_NAME);
+ KUNIT_ASSERT_EQ(test, 0, result);
+ KUNIT_EXPECT_EQ(test, be16_to_cpu(stored1->length),
+ be16_to_cpu(stored2->length));
+ KUNIT_EXPECT_EQ(test, 34, be16_to_cpu(stored1->length));
+ KUNIT_EXPECT_EQ(test,
+ memcmp(stored1->unicode, stored2->unicode,
+ be16_to_cpu(stored1->length) * sizeof(stored1->unicode[0])),
+ 0);
+
+ free_mock_sb(mock_sb);
+}
+
/* Test hfsplus_compare_dentry basic functionality */
static void hfsplus_compare_dentry_basic_test(struct kunit *test)
{
@@ -1880,6 +1952,7 @@ static struct kunit_case hfsplus_unicode_test_cases[] = {
KUNIT_CASE(hfsplus_hash_dentry_consistency_test),
KUNIT_CASE(hfsplus_hash_dentry_edge_cases_test),
KUNIT_CASE(hfsplus_hash_dentry_combining_reorder_test),
+ KUNIT_CASE(hfsplus_dentry_combining_reorder_long_test),
KUNIT_CASE(hfsplus_compare_dentry_basic_test),
KUNIT_CASE(hfsplus_compare_dentry_casefold_test),
KUNIT_CASE(hfsplus_compare_dentry_special_chars_test),
--
2.34.1