ntfs: fix false directory corruption on case-insensitive lookup of POSIX names
Hyunchul Lee
maintainerPATCH v2 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.
---
Changes in v2:
- Remove the early return from the hibernation check.
fs/ntfs/dir.c | 103 ++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 50 deletions(-)
fs/ntfs/dir.c +53 -50
@@ -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
Signature · 1 line
2.43.0
Quoted 171 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")Reported-by Sazpaimon <[email protected]>Signed-off-by Hyunchul Lee <[email protected]>---
Changes in v2:
- Remove the early return from the hibernation check.fs/ntfs/dir.c | 103 ++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 50 deletions(-)fs/ntfs/dir.c +53
-43@@ -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;
fs/ntfs/dir.c u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, const __le16 *uname, · line 498
Quoted 10 lines
fs/ntfs/dir.c +0
-7-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
Looks good to me.
Quoted 6 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.
Quoted 7 lines
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")Reported-by Sazpaimon <[email protected]>Signed-off-by Hyunchul Lee <[email protected]>
Applied it to #ntfs-next.
Thanks!