Skip to contentSkip to conversation

ntfs: fix false directory corruption on case-insensitive lookup of POSIX names

PATCH Superseded Hyunchul Lee 2 participants 2 messages Nov 26 NTFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hyunchul Lee

author maintainer
#0 lore raw

PATCH 1/1 ntfs: fix false directory corruption on case-insensitive lookup of POSIX names

On a case-insensitive mount, a directory index may legitimately contain two POSIX-namespace names that differ only by letter case. During a lookup, ntfs_lookup_inode_by_name() treated any second case-insensitive match as directory corruption, failed the lookup with EIO.

Keep the first case-insensitive candidate and continue the collation and B+tree traversal so an exact match is still found.

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")
---
 fs/ntfs/dir.c   | 103 +++++++++++++++++++++++++-----------------------
 fs/ntfs/super.c |   4 ++
 2 files changed, 57 insertions(+), 50 deletions(-)
fs/ntfs/dir.c +53 -50
index a93f3566603b..2ff90852bccc 100644
@@ -54,8 +54,8 @@ static inline u64 ntfs_check_mref(u64 mref)
  *
  * Note, we look for a case sensitive match first but we also look for a case
  * insensitive match at the same time. If we find a case insensitive match, we
- * save that for the case that we don't find an exact match, where we return
- * the case insensitive match and setup @res (which we allocate!) with the mft
+ * save the first match on the search path in case we don't find an exact
+ * match, where we return it and setup @res (which we allocate!) with the mft
  * reference, the file name type, length and with a copy of the little endian
  * Unicode file name itself. If we match a file name which is in the DOS name
  * space, we only return the mft reference and file name type in @res.
@@ -91,6 +91,7 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 	struct folio *folio;
 	u8 *kaddr = NULL;
 	struct ntfs_name *name = NULL;
+	bool seen_nonposix = false;
 
 	/* Get hold of the mft record for the directory. */
 	m = map_mft_record(dir_ni);
@@ -191,13 +192,11 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 		}
 		/*
 		 * For a case insensitive mount, we also perform a case
-		 * insensitive comparison (provided the file name is not in the
-		 * POSIX namespace). If the comparison matches, and the name is
-		 * in the WIN32 namespace, we cache the filename in *res so
-		 * that the caller, ntfs_lookup(), can work on it. If the
-		 * comparison matches, and the name is in the DOS namespace, we
-		 * only cache the mft reference and the file name type (we set
-		 * the name length to zero for simplicity).
+		 * insensitive comparison. DOS names are matched this way even
+		 * on a case sensitive mount. If the comparison matches and no
+		 * candidate is cached, we cache the filename in *res for
+		 * ntfs_lookup(). For a DOS name, we only cache the mft reference
+		 * and the file name type (we set the name length to zero).
 		 */
 		if ((!NVolCaseSensitive(vol) ||
 		     ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
@@ -210,29 +209,32 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 			u8 type = ie->key.file_name.file_name_type;
 			u8 len = ie->key.file_name.file_name_length;
 
-			/* Only one case insensitive matching name allowed. */
-			if (name) {
+			/* Only one imperfect non-POSIX match is allowed. */
+			if (type != FILE_NAME_POSIX && seen_nonposix) {
 				ntfs_error(sb,
 					"Found already allocated name in phase 1. Please run chkdsk");
 				goto dir_err_out;
 			}
-
-			if (type != FILE_NAME_DOS)
-				name_size += len * sizeof(__le16);
-			name = kmalloc(name_size, GFP_NOFS);
+			if (type != FILE_NAME_POSIX)
+				seen_nonposix = true;
 			if (!name) {
-				err = -ENOMEM;
-				goto err_out;
+				if (type != FILE_NAME_DOS)
+					name_size += len * sizeof(__le16);
+				name = kmalloc(name_size, GFP_NOFS);
+				if (!name) {
+					err = -ENOMEM;
+					goto err_out;
+				}
+				name->mref = le64_to_cpu(ie->data.dir.indexed_file);
+				name->type = type;
+				if (type != FILE_NAME_DOS) {
+					name->len = len;
+					memcpy(name->name, ie->key.file_name.file_name,
+							len * sizeof(__le16));
+				} else
+					name->len = 0;
+				*res = name;
 			}
-			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
-			name->type = type;
-			if (type != FILE_NAME_DOS) {
-				name->len = len;
-				memcpy(name->name, ie->key.file_name.file_name,
-						len * sizeof(__le16));
-			} else
-				name->len = 0;
-			*res = name;
 		}
 		/*
 		 * Not a perfect match, need to do full blown collation so we
@@ -447,13 +449,11 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 		}
 		/*
 		 * For a case insensitive mount, we also perform a case
-		 * insensitive comparison (provided the file name is not in the
-		 * POSIX namespace). If the comparison matches, and the name is
-		 * in the WIN32 namespace, we cache the filename in *res so
-		 * that the caller, ntfs_lookup(), can work on it. If the
-		 * comparison matches, and the name is in the DOS namespace, we
-		 * only cache the mft reference and the file name type (we set
-		 * the name length to zero for simplicity).
+		 * insensitive comparison. DOS names are matched this way even
+		 * on a case sensitive mount. If the comparison matches and no
+		 * candidate is cached, we cache the filename in *res for
+		 * ntfs_lookup(). For a DOS name, we only cache the mft reference
+		 * and the file name type (we set the name length to zero).
 		 */
 		if ((!NVolCaseSensitive(vol) ||
 		     ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
@@ -466,30 +466,33 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
 			u8 type = ie->key.file_name.file_name_type;
 			u8 len = ie->key.file_name.file_name_length;
 
-			/* Only one case insensitive matching name allowed. */
-			if (name) {
+			/* Only one imperfect non-POSIX match is allowed. */
+			if (type != FILE_NAME_POSIX && seen_nonposix) {
 				ntfs_error(sb,
 					"Found already allocated name in phase 2. Please run chkdsk");
 				kfree(kaddr);
 				goto dir_err_out;
 			}
-
-			if (type != FILE_NAME_DOS)
-				name_size += len * sizeof(__le16);
-			name = kmalloc(name_size, GFP_NOFS);
+			if (type != FILE_NAME_POSIX)
+				seen_nonposix = true;
 			if (!name) {
-				err = -ENOMEM;
-				goto unm_err_out;
+				if (type != FILE_NAME_DOS)
+					name_size += len * sizeof(__le16);
+				name = kmalloc(name_size, GFP_NOFS);
+				if (!name) {
+					err = -ENOMEM;
+					goto unm_err_out;
+				}
+				name->mref = le64_to_cpu(ie->data.dir.indexed_file);
+				name->type = type;
+				if (type != FILE_NAME_DOS) {
+					name->len = len;
+					memcpy(name->name, ie->key.file_name.file_name,
+							len * sizeof(__le16));
+				} else
+					name->len = 0;
+				*res = name;
 			}
-			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
-			name->type = type;
-			if (type != FILE_NAME_DOS) {
-				name->len = len;
-				memcpy(name->name, ie->key.file_name.file_name,
-						len * sizeof(__le16));
-			} else
-				name->len = 0;
-			*res = name;
 		}
 		/*
 		 * Not a perfect match, need to do full blown collation so we
fs/ntfs/super.c +4 -0
index 39ff56cecf62..a87b47adb28d 100644
@@ -1380,6 +1380,10 @@ static int check_windows_hibernation_status(struct ntfs_volume *vol)
 	mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
 			&name);
 	inode_unlock(vol->root_ino);
+	if (!IS_ERR_MREF(mref) && name) {
+		kfree(name);
+		return 0;
+	}
 	kfree(name);
 	if (IS_ERR_MREF(mref)) {
 		ret = MREF_ERR(mref);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 184 lines · Hyunchul Lee

On a case-insensitive mount, a directory index may legitimately contain two POSIX-namespace names that differ only by letter case. During a lookup, ntfs_lookup_inode_by_name() treated any second case-insensitive match as directory corruption, failed the lookup with EIO.

Keep the first case-insensitive candidate and continue the collation and B+tree traversal so an exact match is still found.

Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")
---
  fs/ntfs/dir.c   | 103 +++++++++++++++++++++++++-----------------------
  fs/ntfs/super.c |   4 ++
  2 files changed, 57 insertions(+), 50 deletions(-)
fs/ntfs/dir.c +53 -50
index a93f3566603b..2ff90852bccc 100644
@@ -54,8 +54,8 @@ static inline u64 ntfs_check_mref(u64 mref)
   *
   * Note, we look for a case sensitive match first but we also look for a case
   * insensitive match at the same time. If we find a case insensitive match, we
- * save that for the case that we don't find an exact match, where we return
- * the case insensitive match and setup @res (which we allocate!) with the mft
+ * save the first match on the search path in case we don't find an exact
+ * match, where we return it and setup @res (which we allocate!) with the mft
   * reference, the file name type, length and with a copy of the little endian
   * Unicode file name itself. If we match a file name which is in the DOS name
   * space, we only return the mft reference and file name type in @res.
@@ -91,6 +91,7 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
  	struct folio *folio;
  	u8 *kaddr = NULL;
  	struct ntfs_name *name = NULL;
+	bool seen_nonposix = false;
 
  	/* Get hold of the mft record for the directory. */
  	m = map_mft_record(dir_ni);
@@ -191,13 +192,11 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
  		}
  		/*
  		 * For a case insensitive mount, we also perform a case
-		 * insensitive comparison (provided the file name is not in the
-		 * POSIX namespace). If the comparison matches, and the name is
-		 * in the WIN32 namespace, we cache the filename in *res so
-		 * that the caller, ntfs_lookup(), can work on it. If the
-		 * comparison matches, and the name is in the DOS namespace, we
-		 * only cache the mft reference and the file name type (we set
-		 * the name length to zero for simplicity).
+		 * insensitive comparison. DOS names are matched this way even
+		 * on a case sensitive mount. If the comparison matches and no
+		 * candidate is cached, we cache the filename in *res for
+		 * ntfs_lookup(). For a DOS name, we only cache the mft reference
+		 * and the file name type (we set the name length to zero).
  		 */
  		if ((!NVolCaseSensitive(vol) ||
  		     ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
@@ -210,29 +209,32 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
  			u8 type = ie->key.file_name.file_name_type;
  			u8 len = ie->key.file_name.file_name_length;
 
-			/* Only one case insensitive matching name allowed. */
-			if (name) {
+			/* Only one imperfect non-POSIX match is allowed. */
+			if (type != FILE_NAME_POSIX && seen_nonposix) {
  				ntfs_error(sb,
  					"Found already allocated name in phase 1. Please run chkdsk");
  				goto dir_err_out;
  			}
-
-			if (type != FILE_NAME_DOS)
-				name_size += len * sizeof(__le16);
-			name = kmalloc(name_size, GFP_NOFS);
+			if (type != FILE_NAME_POSIX)
+				seen_nonposix = true;
  			if (!name) {
-				err = -ENOMEM;
-				goto err_out;
+				if (type != FILE_NAME_DOS)
+					name_size += len * sizeof(__le16);
+				name = kmalloc(name_size, GFP_NOFS);
+				if (!name) {
+					err = -ENOMEM;
+					goto err_out;
+				}
+				name->mref = le64_to_cpu(ie->data.dir.indexed_file);
+				name->type = type;
+				if (type != FILE_NAME_DOS) {
+					name->len = len;
+					memcpy(name->name, ie->key.file_name.file_name,
+							len * sizeof(__le16));
+				} else
+					name->len = 0;
+				*res = name;
  			}
-			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
-			name->type = type;
-			if (type != FILE_NAME_DOS) {
-				name->len = len;
-				memcpy(name->name, ie->key.file_name.file_name,
-						len * sizeof(__le16));
-			} else
-				name->len = 0;
-			*res = name;
  		}
  		/*
  		 * Not a perfect match, need to do full blown collation so we
@@ -447,13 +449,11 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
  		}
  		/*
  		 * For a case insensitive mount, we also perform a case
-		 * insensitive comparison (provided the file name is not in the
-		 * POSIX namespace). If the comparison matches, and the name is
-		 * in the WIN32 namespace, we cache the filename in *res so
-		 * that the caller, ntfs_lookup(), can work on it. If the
-		 * comparison matches, and the name is in the DOS namespace, we
-		 * only cache the mft reference and the file name type (we set
-		 * the name length to zero for simplicity).
+		 * insensitive comparison. DOS names are matched this way even
+		 * on a case sensitive mount. If the comparison matches and no
+		 * candidate is cached, we cache the filename in *res for
+		 * ntfs_lookup(). For a DOS name, we only cache the mft reference
+		 * and the file name type (we set the name length to zero).
  		 */
  		if ((!NVolCaseSensitive(vol) ||
  		     ie->key.file_name.file_name_type == FILE_NAME_DOS) &&
@@ -466,30 +466,33 @@ u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname,
  			u8 type = ie->key.file_name.file_name_type;
  			u8 len = ie->key.file_name.file_name_length;
 
-			/* Only one case insensitive matching name allowed. */
-			if (name) {
+			/* Only one imperfect non-POSIX match is allowed. */
+			if (type != FILE_NAME_POSIX && seen_nonposix) {
  				ntfs_error(sb,
  					"Found already allocated name in phase 2. Please run chkdsk");
  				kfree(kaddr);
  				goto dir_err_out;
  			}
-
-			if (type != FILE_NAME_DOS)
-				name_size += len * sizeof(__le16);
-			name = kmalloc(name_size, GFP_NOFS);
+			if (type != FILE_NAME_POSIX)
+				seen_nonposix = true;
  			if (!name) {
-				err = -ENOMEM;
-				goto unm_err_out;
+				if (type != FILE_NAME_DOS)
+					name_size += len * sizeof(__le16);
+				name = kmalloc(name_size, GFP_NOFS);
+				if (!name) {
+					err = -ENOMEM;
+					goto unm_err_out;
+				}
+				name->mref = le64_to_cpu(ie->data.dir.indexed_file);
+				name->type = type;
+				if (type != FILE_NAME_DOS) {
+					name->len = len;
+					memcpy(name->name, ie->key.file_name.file_name,
+							len * sizeof(__le16));
+				} else
+					name->len = 0;
+				*res = name;
  			}
-			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
-			name->type = type;
-			if (type != FILE_NAME_DOS) {
-				name->len = len;
-				memcpy(name->name, ie->key.file_name.file_name,
-						len * sizeof(__le16));
-			} else
-				name->len = 0;
-			*res = name;
  		}
  		/*
  		 * Not a perfect match, need to do full blown collation so we
fs/ntfs/super.c +0 -0
index 39ff56cecf62..a87b47adb28d 100644

fs/ntfs/super.c static int check_windows_hibernation_status(struct ntfs_volume *vol) · line 1386

Quoted 7 lines
fs/ntfs/super.c +4 -0
@@ -1380,6 +1380,10 @@ static int check_windows_hibernation_status(struct ntfs_volume *vol)
  	mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
  			&name);
  	inode_unlock(vol->root_ino);
+	if (!IS_ERR_MREF(mref) && name) {
+		kfree(name);
+		return 0;
+	}

Hi Hyunchul,
Could we remove the four lines above and keep the hibernation check?
name can also be set when the lookup finds a WIN32 filename that differs only in case.
For example, with -o nocase, looking up hiberfil.sys can find a WIN32
entry named HIBERFIL.SYS and return a non-NULL name.
This early return would skip the file size and header checks and
treat the volume as not hibernated, even if the header contains the hibr signature.
That could allow a read-write mount instead of applying the existing
read-only protection.

With those four lines removed, the rest looks good to me.

Thanks,
Baolin.

Quoted 3 lines
fs/ntfs/super.c +0 -0
  	kfree(name);
  	if (IS_ERR_MREF(mref)) {
  		ret = MREF_ERR(mref);