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

From: Shawn Shao

Date: Tue Dec 01 2026 - 09:18:57 EST


Hello,

Thanks for asking for a clearer explanation. HFSPLUS_MAX_STRLEN is the
maximum number of UTF-16 code units in an HFS+ catalog name. The catalog
conversion decomposes and canonically reorders names within this bound.
Dentry hashing and comparison must use the same ordering for every name
that the catalog conversion accepts.

The iterator's 32-unit limit splits a longer combining run. If a later
mark has a lower combining class than marks in the earlier chunk, it
cannot be moved before those marks. Two spellings that convert to the
same catalog key can then get different dentry hashes or compare unequal.
Using HFSPLUS_MAX_STRLEN lets the iterator reorder any run within the
accepted catalog-name limit; it does not increase that limit.

The KUnit test uses two spellings that differ only in the order of a
33-mark combining run. `hfsplus_asc2uni()` stores both as the same
canonical key, so hashing and dentry comparison must also treat them as
equal. With the old 32-unit limit, the iterator sorts the run in two
pieces and the test catches the resulting hash/comparison mismatch.
This exercises the exact boundary that caused the bug without needing a
mounted image.
The QEMU reproducer also checks the lookup behavior before and after the
change.

Tested source:
HEAD commit: d536283b12c4597f8fc8cc65a034edb8753a9880
Git tree: 7399834a6b1da5c9c4c420886536a105b63eff89
Source: HFS for-next, Linux 7.4.0-rc1
Compiler: gcc 11.4.0, GNU ld 2.38
QEMU: 6.2.0, x86-64 TCG

Reproducer and test results:
https://gist.github.com/shaos26/73b61181ce48283492d2a10f69bb11eb

console output:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/console-output.txt
kernel configuration:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kernel-config.txt
userspace reproducer:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/poc.c
HFS+ image builder:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/make_hfsplus.c
initramfs init:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/init
initramfs builder:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/build-initramfs.sh
QEMU runner:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/run-qemu.sh
KUnit configuration:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kunit-config.txt
KUnit results:
https://gist.githubusercontent.com/shaos26/73b61181ce48283492d2a10f69bb11eb/raw/e7d209d77bd88674ea5306b437539d8f1c02693b/kunit-results.txt

Build the HFS for-next tree at the tested revision and run the baseline:

git clone \
https://git.kernel.org/pub/scm/linux/kernel/git/vdubeyko/hfs.git linux
cd linux
git checkout d536283b12c4597f8fc8cc65a034edb8753a9880
mkdir build
cp ../kernel-config.txt build/.config
make O=build olddefconfig
make O=build -j"$(nproc)" bzImage
cd ..
gcc -O2 -Wall -Wextra -Werror -o make_hfsplus make_hfsplus.c
./make_hfsplus hfsplus.img
sh ./build-initramfs.sh initramfs.cpio.gz
sh ./run-qemu.sh linux/build/arch/x86/boot/bzImage \
initramfs.cpio.gz hfsplus.img baseline.log vulnerable

The baseline reports POS_BAD_AFTER_CREATE=0 and
POS_BAD_AFTER_REMOUNT=1; the 31-mark control resolves immediately.
After applying the patch below and rebuilding, run:

sh ./run-qemu.sh linux/build/arch/x86/boot/bzImage \
initramfs.cpio.gz hfsplus.img fixed.log fixed

The fixed run reports POS_BAD_AFTER_CREATE=1 and the fixed oracle passes.
The x86-64 build also completed with CONFIG_FRAME_WARN=1280 and
KCFLAGS=-Wframe-larger-than=1280 without frame-size warnings. Run the
KUnit regression suite with:

cd linux
python3 tools/testing/kunit/kunit.py run \
--build_dir=build-kunit --kunitconfig=../kunit-config.txt \
--jobs="$(nproc)" hfsplus_unicode --summary

The hfsplus_unicode suite passed all 36 tests.

Changes in v3:
- Explain why the iterator limit follows HFSPLUS_MAX_STRLEN.
- Base the patch on the HFS for-next tree and use its renamed KUnit file.
- Make the reproducer report separate vulnerable and fixed oracles.

Fixes: bbe5ebcbc006 ("hfsplus: canonicalize decomposed catalog names like fsck_hfs expects")
Thanks,
Shawn Shao

Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
fs/hfsplus/hfsplus_unicode_test.c | 73 +++++++++++++++++++++++++++++++++++++++
fs/hfsplus/unicode.c | 19 +++++------
2 files changed, 80 insertions(+), 12 deletions(-)

diff --git a/fs/hfsplus/hfsplus_unicode_test.c b/fs/hfsplus/hfsplus_unicode_test.c
index cb237a40c..80ba1d499 100644
--- a/fs/hfsplus/hfsplus_unicode_test.c
+++ b/fs/hfsplus/hfsplus_unicode_test.c
@@ -1533,6 +1533,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)
{
@@ -1935,6 +2007,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),
diff --git a/fs/hfsplus/unicode.c b/fs/hfsplus/unicode.c
index 59b8788a6..840a11560 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;
--
2.34.1