Skip to contentSkip to conversation

fsck: detect case-insensitive duplicate filenames

PATCH In review Yang Wen 2 participants 3 messages Nov 26 – Nov 27

Subsystems (2)
lore
Share & tools
lore

Yang Wen

author
#0 lore raw

PATCH v2 1/1 fsck: detect case-insensitive duplicate filenames

exFAT file names are case-insensitive, but filter_lookup_file() compares them with memcmp(). This makes fsck miss duplicate names that differ only in case.

Keep memcmp() as the fast path and fall back to the volume upcase table. Add a regression image for case-insensitive duplicate names.

---
 lib/exfat_dir.c                               |  26 ++++++++++++++++--
 .../duplicated_name_casefold/exfat.img.tar.xz | Bin 0 -> 3808 bytes
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 tests/duplicated_name_casefold/exfat.img.tar.xz
lib/exfat_dir.c +24 -2
index 9b63b04..bcea7f4 100644
@@ -551,6 +551,7 @@ static int filter_lookup_file(struct exfat_de_iter *de_iter,
 
 	for (i = 2; i <= file_de->dentry.file.num_ext && name_len > 0; i++) {
 		int len;
+		int j;
 
 		retval = exfat_de_iter_get(de_iter, i, &name_de);
 		if (retval || name_de->type != EXFAT_NAME)
@@ -558,8 +559,29 @@ static int filter_lookup_file(struct exfat_de_iter *de_iter,
 
 		len = MIN(name_len + 1, ENTRY_NAME_MAX);
 		if (memcmp(name_de->dentry.name.unicode_0_14,
-			   name, len * 2) != 0)
-			return 1;
+			   name, len * sizeof(*name)) != 0) {
+			if (!de_iter->exfat->upcase_table)
+				return 1;
+
+			__le16 dentry_name[ENTRY_NAME_MAX];
+			memcpy(dentry_name, name_de->dentry.name.unicode_0_14,
+			       sizeof(dentry_name));
+			/*
+			 * exFAT file names are case-insensitive according to the
+			 * volume upcase table. Keep the exact comparison as the fast
+			 * path, then compare the canonical characters when the on-disk
+			 * spelling differs.
+			 */
+			for (j = 0; j < len; j++) {
+				__u16 dentry_ch = de_iter->exfat->upcase_table[
+					le16_to_cpu(dentry_name[j])];
+				__u16 name_ch = de_iter->exfat->upcase_table[
+					le16_to_cpu(name[j])];
+
+				if (dentry_ch != name_ch)
+					return 1;
+			}
+		}
 
 		name += len;
 		name_len -= len;
tests/duplicated_name_casefold/exfat.img.tar.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..d941e14ffd6e60a09aaba928ad02519bc7ce6f4e
GIT binary patch
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Yang Wen

exFAT file names are case-insensitive, but filter_lookup_file() compares them with memcmp(). This makes fsck miss duplicate names that differ only in case.

Keep memcmp() as the fast path and fall back to the volume upcase table. Add a regression image for case-insensitive duplicate names.

Wait. Hold up.

What does the spec say? Does CHKDSK.EXE fix this? Have you checked? If not, we shouldn't either.

Let me check and report back.

Davo

Tested-by
#2 lore raw

G'day everyone!

Quoted 5 lines · David Timber
Quoted 4 lines · Yang Wen

exFAT file names are case-insensitive, but filter_lookup_file() compares them with memcmp(). This makes fsck miss duplicate names that differ only in case.

Quoted 8 lines
Quoted 2 lines

Keep memcmp() as the fast path and fall back to the volume upcase table. Add a regression image for case-insensitive duplicate names.

Wait. Hold up.

What does the spec say? Does CHKDSK.EXE fix this? Have you checked? If not, we shouldn't either.

Let me check and report back.

Oooookay. Checked the image on Win XP and the latest build.

The first thing I noticed that all versions of CHKDSK.EXE do treat the condition as an error and fix it. EXFAT.sys, however, doesn't seem to mind, although the WinAPI will probably open one of the dupes and all the other files are practicall unreachable(see my comments in
https://github.com/exfatprogs/exfatprogs/issues/313). So, not so critical but worth fixing to match with the CHKDSK.EXE implementation.

I see no problem with your patch.

However, unrelated to this issue, I've found that fsck.exfat renames the files to the ones incomprehensible to human! If run with fsck.exfat -a(by initd via fstab), it randomly generates the new file names("/FILE0000001.CHK"), completely destroying the information! See:

exfat_repair_rename_ask() -> generate_rename()

The Windows implementation renames the files by appending -1 and -2. So, the files in the test image becomes ...

/Duplicated-filename-test-1
/dUplicated-filename-test-2

after running CHKDSK.EXE. If the file name is over 255 characters? idk. It will probably have to write over the characters at the end.

Anyhow, I think that fsck.exfat is destroying information. I think that it needs to be fixed. generate_rename() should be used only for rescuing orphan clusters. I'll open up a GH issue and get back to this later.

Davo