Skip to contentSkip to conversation

hfsplus: fix "Illegal name" fsck failures from non-canonical decomposition

PATCH New Viacheslav Dubeyko 1 participant 4 messages Sep 19

Subsystems (2)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

cover hfsplus: fix "Illegal name" fsck failures from non-canonical decomposition

The xfstests generic/339 (fsstress-style dirhash collision testing) leaves an HFS+ volume corrupted and, as a result, fsck.hfsplus reports a long run of "Illegal name".

The root cause is that fs/hfsplus/unicode.c's decomposition table (Apple Technote #1150) decomposes each source character entirely on its own and never does anything further with the result. Real fsck_hfs has carried a permanent "FixDecomps" fixup in CatalogCheck.c for exactly what that leaves unhandled:

- combining marks contributed by two different source characters landing adjacent must be in ascending Unicode combining-class order, not just input order;
- a small, fixed set of characters either weren't decomposed at all, or were decomposed into a sequence Apple corrected back in Mac OS X 10.2 ("Jaguar"), and this driver's table still predates that.

A name that violates either of these gets flagged "Illegal name" by any real macOS system (or fsck_hfs) that later reads the volume, even though nothing else about it is wrong.

This series makes hfsplus store, hash and compare names the same way current macOS does:

1/3 adds the data: a Unicode combining-class table generated from UCD
12.1.0, and two legacy-decomposition correction tables decoded programmatically from hfsprogs 540.1's own DecompData.h. Pure data, no behavior change.

2/3 adds the logic that uses that data: canonical reordering of
decomposed names, a fallback to the legacy decomposition table, and legacy sequence substitution, all applied in hfsplus_asc2uni() so this is what actually gets stored. hfsplus_hash_dentry() and hfsplus_compare_dentry() are reworked to canonicalize names the same way, so a lookup can't disagree with what a create() of the same name actually wrote.

3/3 adds KUnit coverage for both: reordering, stability of equal-class
marks, the legacy decomposition and sequence fixups, and that hashing/comparison agree across differently-typed-but-equivalent spellings of the same name.

Viacheslav Dubeyko (3):
hfsplus: add Unicode combining-class and legacy decomposition data
hfsplus: canonicalize decomposed catalog names like fsck_hfs expects
hfsplus: add KUnit coverage for canonical reordering and legacy fixups

 fs/hfsplus/hfsplus_fs.h   |   6 +-
 fs/hfsplus/tables.c       | 496 ++++++++++++++++++++++++++++++++++++++
 fs/hfsplus/unicode.c      | 390 ++++++++++++++++++++----------
 fs/hfsplus/unicode.h      |  63 +++++
 fs/hfsplus/unicode_test.c | 309 ++++++++++++++++++++++++
 5 files changed, 1127 insertions(+), 137 deletions(-)
 create mode 100644 fs/hfsplus/unicode.h
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/3 hfsplus: add Unicode combining-class and legacy decomposition data

macOS's own fsck_hfs rejects a catalog name as "Illegal name" in two cases this driver's decomposition tables don't currently account for:

- a decomposed name whose combining marks aren't in Unicode canonical combining-class order (fsck_hfs's FixDecomps(), CatalogCheck.c);
- a small, fixed set of characters whose canonical decomposition either wasn't yet defined, or was later corrected, since the driver's existing hfsplus_decompose_table (Apple Technote #1150) predates those corrections, which macOS has applied since Mac OS X 10.2 ("Jaguar", 2002).

This is purely a data patch and adds no new behavior on its own:

- hfsplus_ccc_table: canonical combining class per BMP code point, generated from the Unicode Character Database version 12.1.0
(https://www.unicode.org/Public/12.1.0/ucd/extracted/
DerivedCombiningClass.txt), the same UCD version fs/unicode's README.utf8data documents.

- hfsplus_legacy_decomp_table and hfsplus_legacy_seq_fixups: the corrected decompositions and sequence replacements fsck_hfs itself requires, decoded from hfsprogs 540.1's fsck_hfs.tproj/dfalib/DecompData.h (upstream: Apple's diskdev_cmds,
https://opensource.apple.com/source/diskdev_cmds/).

The three new struct types and the extern declarations for all of tables.c's tables (existing and new) move into a new fs/hfsplus/ unicode.h, replacing the ad hoc externs that used to live in hfsplus_fs.h.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/hfsplus_fs.h   |   6 +-
 fs/hfsplus/tables.c       | 397 ++++++++++++++++++++++++++++++++++++++
 fs/hfsplus/unicode.c      |   1 +
 fs/hfsplus/unicode.h      |  59 ++++++
 fs/hfsplus/unicode_test.c |   1 +
 5 files changed, 459 insertions(+), 5 deletions(-)
 create mode 100644 fs/hfsplus/unicode.h
fs/hfsplus/hfsplus_fs.h +1 -5
index 1e5b58e6a13f..916e6552e3f0 100644
@@ -17,6 +17,7 @@
 #include <linux/blkdev.h>
 #include <linux/fs_context.h>
 #include "hfsplus_raw.h"
+#include "unicode.h"
 
 /* Runtime config options */
 #define HFSPLUS_DEF_CR_TYPE    0x3F3F3F3F  /* '????' */
@@ -486,11 +487,6 @@ void hfsplus_mark_mdb_dirty(struct super_block *sb);
 void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr);
 int hfsplus_commit_superblock(struct super_block *sb);
 
-/* tables.c */
-extern u16 hfsplus_case_fold_table[];
-extern u16 hfsplus_decompose_table[];
-extern u16 hfsplus_compose_table[];
-
 /* unicode.c */
 int hfsplus_strcasecmp(const struct hfsplus_unistr *s1,
 		       const struct hfsplus_unistr *s2);
fs/hfsplus/tables.c +397 -0

400 lines not on this page · raw message

fs/hfsplus/unicode.c +1 -0
index 008fec186382..93d39481e477 100644
@@ -16,6 +16,7 @@
 
 #include "hfsplus_fs.h"
 #include "hfsplus_raw.h"
+#include "unicode.h"
 
 /* Fold the case of a unicode char, given the 16 bit value */
 /* Returns folded char, or 0 if ignorable */
fs/hfsplus/unicode.h new file +59 -0

59 lines not on this page · raw message

fs/hfsplus/unicode_test.c +1 -0
index 7f6b2a3c69d9..32a64029d77b 100644
@@ -10,6 +10,7 @@
 #include <linux/dcache.h>
 #include <linux/stringhash.h>
 #include "hfsplus_fs.h"
+#include "unicode.h"
 
 struct test_mock_string_env {
 	struct hfsplus_unistr str1;
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 hfsplus: canonicalize decomposed catalog names like fsck_hfs expects

In reply to Viacheslav Dubeyko

hfsplus_decompose_table (Apple Technote #1150) decomposes each source character entirely on its own. It never reorders the result, and it predates a handful of later corrections to the decomposition standard. Two concrete consequences, both of which macOS's own fsck_hfs treats as "Illegal name" (CatalogCheck.c: CheckCatalogName(), FixDecomps()):

- When combining marks contributed by two different source characters end up adjacent, this driver stores them in input order rather than ascending Unicode combining-class order.
- A small, fixed set of characters either aren't decomposed at all, or are decomposed into a sequence macOS corrected back in Mac OS X 10.2 ("Jaguar").

This is reliably reproducible: xfstests generic/339 exercises dirhash collisions by creating many files with randomized Unicode names, and the resulting HFS+ volume fails _check_generic_filesystem afterward with a long run of "Illegal name" reports from fsck.hfsplus.

Fix hfsplus_asc2uni() to store names the way current macOS does, using the Unicode tables:

- hfsplus_canonical_reorder() applies the Unicode Canonical Ordering
Algorithm: each maximal run of nonzero-combining-class code units is stable-sorted into ascending class order.
- decompose_unichar() falls back to hfsplus_legacy_decompose() for the characters missing from Apple Technote #1150's table.
- hfsplus_fixup_legacy_sequences() substitutes any of fsck_hfs's known bad sequences with their corrected form.

hfsplus_decompose_str() ties these together and is now shared by hfsplus_asc2uni(), hfsplus_hash_dentry() and hfsplus_compare_dentry(), so hashing and comparison always agree with what actually gets stored - otherwise two byte-for-byte different but canonically-equivalent spellings of a name could hash differently or fail to compare equal against the catalog entry a create() of either would produce.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/tables.c  |  99 +++++++++++
 fs/hfsplus/unicode.c | 389 ++++++++++++++++++++++++++++---------------
 fs/hfsplus/unicode.h |   4 +
 3 files changed, 360 insertions(+), 132 deletions(-)
fs/hfsplus/tables.c +99 -0
index 3bbdc83debb5..2101201ab973 100644
@@ -3641,3 +3641,102 @@ struct hfsplus_legacy_seq_fixup hfsplus_legacy_seq_fixups[] = {
 	{ 3, { 0x0fb2, 0x0f80, 0x0f71 }, 1, { 0x0f77 } },
 	{ 3, { 0x0fb3, 0x0f80, 0x0f71 }, 1, { 0x0f79 } },
 };
+
+/*
+ * Look up the Unicode canonical combining class of a BMP code point.
+ * Returns 0 ("Not_Reordered") for any code point not listed in the table,
+ * which includes every ordinary base character.
+ */
+u8 hfsplus_combining_class(u16 c)
+{
+	int lo = 0, hi = ARRAY_SIZE(hfsplus_ccc_table) - 1;
+
+	while (lo <= hi) {
+		int mid = (lo + hi) / 2;
+		const struct hfsplus_ccc_range *r = &hfsplus_ccc_table[mid];
+
+		if (c < r->first)
+			hi = mid - 1;
+		else if (c > r->last)
+			lo = mid + 1;
+		else
+			return r->combining_class;
+	}
+
+	return 0;
+}
+
+/*
+ * Look up the corrected canonical decomposition for a single BMP code
+ * point that Apple Technote #1150's own decomposition table (above)
+ * lacks. Returns NULL (and leaves *size alone) if @uc isn't one of these.
+ */
+const u16 *hfsplus_legacy_decompose(u16 uc, int *size)
+{
+	int lo = 0, hi = ARRAY_SIZE(hfsplus_legacy_decomp_table) - 1;
+
+	while (lo <= hi) {
+		int mid = (lo + hi) / 2;
+		const struct hfsplus_legacy_decomp *e =
+				&hfsplus_legacy_decomp_table[mid];
+
+		if (uc < e->uc)
+			hi = mid - 1;
+		else if (uc > e->uc)
+			lo = mid + 1;
+		else {
+			*size = e->len;
+			return e->repl;
+		}
+	}
+
+	return NULL;
+}
+
+/*
+ * Scan an already decomposed and canonically-reordered code unit buffer
+ * for any of the short legacy sequences above and replace them in place
+ * with their corrected form. @len is updated to the buffer's new length.
+ */
+void hfsplus_fixup_legacy_sequences(u16 *buf, int *len)
+{
+	int i = 0;
+
+	while (i < *len) {
+		unsigned int j;
+		bool matched = false;
+
+		for (j = 0; j < ARRAY_SIZE(hfsplus_legacy_seq_fixups); j++) {
+			struct hfsplus_legacy_seq_fixup *f =
+						&hfsplus_legacy_seq_fixups[j];
+			u16 *src, *dst;
+			size_t copy_len;
+			int k;
+
+			if (i + f->match_len > *len)
+				continue;
+			for (k = 0; k < f->match_len; k++)
+				if (buf[i + k] != f->match[k])
+					break;
+			if (k != f->match_len)
+				continue;
+
+			dst = &buf[i + f->repl_len];
+			src = &buf[i + f->match_len];
+			copy_len = (*len - i - f->match_len) * sizeof(*buf);
+			memmove(dst, src, copy_len);
+
+			dst = &buf[i];
+			src = f->repl;
+			copy_len = f->repl_len * sizeof(*buf);
+			memcpy(dst, src, copy_len);
+
+			*len += f->repl_len - f->match_len;
+			matched = true;
+			break;
+		}
+
+		if (!matched)
+			i++;
+	}
+}
fs/hfsplus/unicode.c +257 -132

434 lines not on this page · raw message

fs/hfsplus/unicode.h +4 -0
index 3957ebccc232..31cc7691c916 100644
@@ -56,4 +56,8 @@ extern struct hfsplus_ccc_range hfsplus_ccc_table[];
 extern struct hfsplus_legacy_decomp hfsplus_legacy_decomp_table[];
 extern struct hfsplus_legacy_seq_fixup hfsplus_legacy_seq_fixups[];
 
+u8 hfsplus_combining_class(u16 c);
+const u16 *hfsplus_legacy_decompose(u16 uc, int *size);
+void hfsplus_fixup_legacy_sequences(u16 *buf, int *len);
+
 #endif /* _LINUX_HFSPLUS_UNICODE_H */
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/3 hfsplus: add KUnit coverage for canonical reordering and legacy fixups

In reply to Viacheslav Dubeyko

Add regression tests for checking that decomposed catalog names get canonically reordered and legacy-decomposition corrected before storage, matching what macOS's own fsck_hfs requires and previously flagged as "Illegal name" (reproducible via xfstests generic/339).

Two small helpers, test_char2uni_utf8()/test_uni2char_utf8(), wrap the real utf8_to_utf32()/utf32_to_utf8() codec: the existing test_char2uni()/test_uni2char() stand-ins only handle one raw byte per character and can't represent the multi-byte combining marks (U+0301, U+0323, ...) these tests need.

New cases:

- hfsplus_asc2uni_combining_reorder_test: two independently-typed combining marks (U+0301 class 230, U+0323 class 220) end up stored in ascending combining-class order regardless of typed order, are left alone when already canonical, and keep their relative order when their classes are equal (the sort must be stable).
- hfsplus_unicode_combining_reorder_roundtrip_test: a name stored with marks reordered still reads back as the same, valid UTF-8.
- hfsplus_asc2uni_legacy_decomp_test: U+01F8 decomposes to "N" + U+0300 even though Apple Technote #1150's table has no entry for it.
- hfsplus_asc2uni_legacy_seq_fixup_test: a Greek letter + U+030D becomes that letter + U+0301, and Bengali BA + NUKTA collapses to the single letter RA WITH MIDDLE DIAGONAL.
- hfsplus_hash_dentry_combining_reorder_test: two names differing only in typed mark order hash identically, since both canonicalize to the same stored form.
- hfsplus_compare_dentry_combining_reorder_test and hfsplus_compare_dentry_legacy_decomp_test: likewise, both compare equal - a precomposed U+01F8 and its already-decomposed "N" + U+0300 spelling must be found as the same catalog entry.

All 34 cases in the hfsplus_unicode KUnit suite (27 existing + 7 new) pass under tools/testing/kunit/kunit.py.

---
 fs/hfsplus/unicode_test.c | 308 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 308 insertions(+)
fs/hfsplus/unicode_test.c +308 -0
index 32a64029d77b..a683c6d5fc22 100644
@@ -948,6 +948,223 @@ static void hfsplus_asc2uni_decompose_test(struct kunit *test)
 	free_mock_sb(mock_sb);
 }
 
+/*
+ * Real UTF-8 <-> wchar_t conversion, mirroring what the "utf8" NLS table
+ * actually does (see char2uni()/uni2char() in fs/nls/nls_utf8.c). The
+ * test_char2uni()/test_uni2char() stand-ins above only handle one raw
+ * byte per character, which cannot represent the combining marks (e.g.
+ * U+0301, U+0323) the canonical-reordering tests below need.
+ */
+static int test_char2uni_utf8(const unsigned char *rawstring, int boundlen,
+			      wchar_t *uni)
+{
+	unicode_t u;
+	int n = utf8_to_utf32(rawstring, boundlen, &u);
+
+	if (n < 0 || u > MAX_WCHAR_T) {
+		*uni = 0x3f; /* ? */
+		return -EINVAL;
+	}
+	*uni = (wchar_t)u;
+	return n;
+}
+
+static int test_uni2char_utf8(wchar_t uni, unsigned char *out, int boundlen)
+{
+	int n = utf32_to_utf8(uni, out, boundlen);
+
+	if (n < 0) {
+		*out = '?';
+		return -EINVAL;
+	}
+	return n;
+}
+
+/*
+ * Test that hfsplus_asc2uni() brings combining marks contributed by
+ * different source characters into Unicode canonical (combining-class)
+ * order, instead of just storing them in whatever order they were typed.
+ *
+ * U+0301 (COMBINING ACUTE ACCENT) has combining class 230; U+0323
+ * (COMBINING DOT BELOW) has combining class 220. Since 220 < 230,
+ * canonical order requires the dot-below before the acute. A name
+ * created with them typed in the "wrong" order must still end up stored
+ * in canonical order - this is exactly what macOS's own fsck_hfs
+ * (FixDecomps() in CatalogCheck.c) requires and flags as "Illegal name"
+ * when it isn't true.
+ */
+static void hfsplus_asc2uni_combining_reorder_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct hfsplus_unistr ustr;
+	/* "e" + COMBINING ACUTE ACCENT (U+0301) + COMBINING DOT BELOW (U+0323) */
+	static const char wrong_order[] = "e\xcc\x81\xcc\xa3";
+	/* "e" + COMBINING DOT BELOW (U+0323) + COMBINING ACUTE ACCENT (U+0301) */
+	static const char canonical_order[] = "e\xcc\xa3\xcc\x81";
+	/* Two class-230 marks: GRAVE (U+0300) then ACUTE (U+0301) */
+	static const char same_class[] = "e\xcc\x80\xcc\x81";
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	/* Typed out of canonical order: must be reordered on storage. */
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 wrong_order, strlen(wrong_order),
+				 HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 3, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 'e', be16_to_cpu(ustr.unicode[0]));
+	KUNIT_EXPECT_EQ(test, 0x0323, be16_to_cpu(ustr.unicode[1]));
+	KUNIT_EXPECT_EQ(test, 0x0301, be16_to_cpu(ustr.unicode[2]));
+
+	/* Already in canonical order: must come out unchanged. */
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 canonical_order, strlen(canonical_order),
+				 HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 3, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 'e', be16_to_cpu(ustr.unicode[0]));
+	KUNIT_EXPECT_EQ(test, 0x0323, be16_to_cpu(ustr.unicode[1]));
+	KUNIT_EXPECT_EQ(test, 0x0301, be16_to_cpu(ustr.unicode[2]));
+
+	/* Two marks of equal combining class must keep their relative
+	 * (input) order - the sort must be stable, not just "sorted".
+	 */
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 same_class, strlen(same_class),
+				 HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 3, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 'e', be16_to_cpu(ustr.unicode[0]));
+	KUNIT_EXPECT_EQ(test, 0x0300, be16_to_cpu(ustr.unicode[1]));
+	KUNIT_EXPECT_EQ(test, 0x0301, be16_to_cpu(ustr.unicode[2]));
+
+	free_mock_sb(mock_sb);
+}
+
+/*
+ * Test that a name written with combining marks in non-canonical order
+ * still reads back as valid, correct UTF-8 once reordered - i.e. that
+ * hfsplus_asc2uni() and hfsplus_uni2asc_str() stay consistent with each
+ * other across the reordering.
+ *
+ * NODECOMPOSE is set so hfsplus_uni2asc_str() doesn't also recompose
+ * "e" + COMBINING DOT BELOW back into the precomposed U+1EB9 ("e"): that
+ * composition behavior is real (and already covered elsewhere), but it
+ * would obscure what this test is actually checking.
+ */
+static void hfsplus_unicode_combining_reorder_roundtrip_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct hfsplus_unistr ustr;
+	static const char wrong_order[] = "e\xcc\x81\xcc\xa3";
+	static const char expected[] = "e\xcc\xa3\xcc\x81"; /* canonical order */
+	char buf[32];
+	int len = sizeof(buf);
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	set_bit(HFSPLUS_SB_NODECOMPOSE, &mock_sb->sb_info.flags);
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+	mock_sb->nls.uni2char = test_uni2char_utf8;
+
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 wrong_order, strlen(wrong_order),
+				 HFS_REGULAR_NAME);
+	KUNIT_EXPECT_EQ(test, 0, result);
+
+	result = hfsplus_uni2asc_str(&mock_sb->sb, &ustr, buf, &len);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, (int)strlen(expected), len);
+	KUNIT_EXPECT_MEMEQ(test, expected, buf, len);
+
+	free_mock_sb(mock_sb);
+}
+
+/*
+ * Test that hfsplus_asc2uni() decomposes a character using the corrected
+ * (post-2002/"Jaguar") canonical decomposition even when Apple Technote
+ * #1150's own decomposition table doesn't have an entry for it.
+ *
+ * U+01F8 (LATIN CAPITAL LETTER N WITH GRAVE) is exactly one of the
+ * characters macOS's own fsck_hfs (FixDecomps() in CatalogCheck.c) has
+ * flagged "Illegal name" for since 2002 when found stored undecomposed.
+ */
+static void hfsplus_asc2uni_legacy_decomp_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct hfsplus_unistr ustr;
+	static const char input[] = "\xc7\xb8"; /* U+01F8 */
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 input, strlen(input), HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 2, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 'N', be16_to_cpu(ustr.unicode[0]));
+	KUNIT_EXPECT_EQ(test, 0x0300, be16_to_cpu(ustr.unicode[1]));
+
+	free_mock_sb(mock_sb);
+}
+
+/*
+ * Test that hfsplus_asc2uni() corrects two more of fsck_hfs's known
+ * legacy decomposition sequences once combining marks from independently
+ * typed characters end up adjacent:
+ *
+ *  - GREEK SMALL LETTER ALPHA (U+03B1) + COMBINING VERTICAL LINE ABOVE
+ *    (U+030D) must become U+03B1 + COMBINING ACUTE ACCENT (U+0301).
+ *  - BENGALI LETTER BA (U+09AC) + BENGALI SIGN NUKTA (U+09BC) must become
+ *    the single character BENGALI LETTER RA WITH MIDDLE DIAGONAL (U+09B0).
+ */
+static void hfsplus_asc2uni_legacy_seq_fixup_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct hfsplus_unistr ustr;
+	static const char greek_input[] = "\xce\xb1\xcc\x8d"; /* U+03B1 U+030D */
+	static const char bengali_input[] = "\xe0\xa6\xac\xe0\xa6\xbc"; /* U+09AC U+09BC */
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 greek_input, strlen(greek_input),
+				 HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 2, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 0x03b1, be16_to_cpu(ustr.unicode[0]));
+	KUNIT_EXPECT_EQ(test, 0x0301, be16_to_cpu(ustr.unicode[1]));
+
+	result = hfsplus_asc2uni(&mock_sb->sb, &ustr, HFSPLUS_MAX_STRLEN,
+				 bengali_input, strlen(bengali_input),
+				 HFS_REGULAR_NAME);
+
+	KUNIT_EXPECT_EQ(test, 0, result);
+	KUNIT_EXPECT_EQ(test, 1, be16_to_cpu(ustr.length));
+	KUNIT_EXPECT_EQ(test, 0x09b0, be16_to_cpu(ustr.unicode[0]));
+
+	free_mock_sb(mock_sb);
+}
+
 /* Mock dentry for testing hfsplus_hash_dentry */
 static struct dentry test_dentry;
 
@@ -1231,6 +1448,37 @@ static void hfsplus_hash_dentry_edge_cases_test(struct kunit *test)
 	free_mock_sb(mock_sb);
 }
 
+/*
+ * Test that hfsplus_hash_dentry() hashes two names identically when they
+ * differ only in the (non-canonical) typed order of the same combining
+ * marks - both must canonicalize to the same stored form, so they must
+ * hash the same or dcache lookups would spuriously miss.
+ */
+static void hfsplus_hash_dentry_combining_reorder_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct qstr str1, str2;
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	setup_mock_dentry(&mock_sb->sb);
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	create_qstr(&str1, "e\xcc\x81\xcc\xa3"); /* acute, then dot-below */
+	result = hfsplus_hash_dentry(&test_dentry, &str1);
+	KUNIT_EXPECT_EQ(test, 0, result);
+
+	create_qstr(&str2, "e\xcc\xa3\xcc\x81"); /* dot-below, then acute */
+	result = hfsplus_hash_dentry(&test_dentry, &str2);
+	KUNIT_EXPECT_EQ(test, 0, result);
+
+	KUNIT_EXPECT_EQ(test, str1.hash, str2.hash);
+
+	free_mock_sb(mock_sb);
+}
+
 /* Test hfsplus_compare_dentry basic functionality */
 static void hfsplus_compare_dentry_basic_test(struct kunit *test)
 {
@@ -1553,6 +1801,59 @@ static void hfsplus_compare_dentry_combined_flags_test(struct kunit *test)
 	free_mock_sb(mock_sb);
 }
 
+/*
+ * Test that hfsplus_compare_dentry() treats two names as equal when they
+ * differ only in the (non-canonical) typed order of the same combining
+ * marks, since both refer to the same canonically-ordered catalog entry.
+ */
+static void hfsplus_compare_dentry_combining_reorder_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct qstr name;
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	setup_mock_dentry(&mock_sb->sb);
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	create_qstr(&name, "e\xcc\xa3\xcc\x81"); /* dot-below, then acute */
+	result = hfsplus_compare_dentry(&test_dentry, 5, "e\xcc\x81\xcc\xa3",
+					&name); /* acute, then dot-below */
+	KUNIT_EXPECT_EQ(test, 0, result);
+
+	free_mock_sb(mock_sb);
+}
+
+/*
+ * Test that a precomposed character using one of fsck_hfs's known legacy
+ * decompositions compares equal to the already-decomposed form of the
+ * same character - i.e. that hfsplus_compare_dentry() applies the same
+ * legacy-decomposition correction as hfsplus_asc2uni() does on storage,
+ * so a lookup finds the entry regardless of which form was typed.
+ */
+static void hfsplus_compare_dentry_legacy_decomp_test(struct kunit *test)
+{
+	struct test_mock_sb *mock_sb;
+	struct qstr name;
+	int result;
+
+	mock_sb = setup_mock_sb();
+	KUNIT_ASSERT_NOT_NULL(test, mock_sb);
+
+	setup_mock_dentry(&mock_sb->sb);
+	mock_sb->nls.char2uni = test_char2uni_utf8;
+
+	/* "N" + COMBINING GRAVE ACCENT (U+0300), already decomposed */
+	create_qstr(&name, "N\xcc\x80");
+	/* U+01F8, precomposed */
+	result = hfsplus_compare_dentry(&test_dentry, 2, "\xc7\xb8", &name);
+	KUNIT_EXPECT_EQ(test, 0, result);
+
+	free_mock_sb(mock_sb);
+}
+
 static struct kunit_case hfsplus_unicode_test_cases[] = {
 	KUNIT_CASE(hfsplus_strcasecmp_test),
 	KUNIT_CASE(hfsplus_strcmp_test),
@@ -1568,12 +1869,17 @@ static struct kunit_case hfsplus_unicode_test_cases[] = {
 	KUNIT_CASE(hfsplus_asc2uni_buffer_limits_test),
 	KUNIT_CASE(hfsplus_asc2uni_edge_cases_test),
 	KUNIT_CASE(hfsplus_asc2uni_decompose_test),
+	KUNIT_CASE(hfsplus_asc2uni_combining_reorder_test),
+	KUNIT_CASE(hfsplus_unicode_combining_reorder_roundtrip_test),
+	KUNIT_CASE(hfsplus_asc2uni_legacy_decomp_test),
+	KUNIT_CASE(hfsplus_asc2uni_legacy_seq_fixup_test),
 	KUNIT_CASE(hfsplus_hash_dentry_basic_test),
 	KUNIT_CASE(hfsplus_hash_dentry_casefold_test),
 	KUNIT_CASE(hfsplus_hash_dentry_special_chars_test),
 	KUNIT_CASE(hfsplus_hash_dentry_decompose_test),
 	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_compare_dentry_basic_test),
 	KUNIT_CASE(hfsplus_compare_dentry_casefold_test),
 	KUNIT_CASE(hfsplus_compare_dentry_special_chars_test),
@@ -1581,6 +1887,8 @@ static struct kunit_case hfsplus_unicode_test_cases[] = {
 	KUNIT_CASE(hfsplus_compare_dentry_decompose_test),
 	KUNIT_CASE(hfsplus_compare_dentry_edge_cases_test),
 	KUNIT_CASE(hfsplus_compare_dentry_combined_flags_test),
+	KUNIT_CASE(hfsplus_compare_dentry_combining_reorder_test),
+	KUNIT_CASE(hfsplus_compare_dentry_legacy_decomp_test),
 	{}
 };
 
Signature · 1 line
2.43.0