Skip to contentSkip to conversation

fs: reject casefolded names that fold to ".", "..", or empty

PATCH In review Sarthak Kukreti 3 participants 10 messages Nov 19 – Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover fs: reject casefolded names that fold to ".", "..", or empty

Unicode normalization in fs/unicode strips Default_Ignorable_Code_Point characters in both UTF8_NFDI and UTF8_NFDICF. In casefolded directories, filenames consisting of 0 to 2 ASCII dots plus ignorable code points (e.g. "\u202e", ".\u202e", "..\u202e") pass VFS path walk as normal multi-byte components, but fold to "", ".", or ".." in utf8_casefold():

1. Looking up ".\u202e" folds to "." and matches the on-disk "." entry (de->inode == dir->i_ino) in generic_ci_match(), triggering EXT4_ERROR_INODE ("linked to parent dir") in ext4_lookup().
2. Looking up "..\u202e" folds to ".." and matches the on-disk ".." entry, returning -ELOOP via d_splice_alias() (or triggering EXT4_ERROR_INODE on the root directory).
3. Filenames consisting solely of ignorable characters fold to "" (0 bytes). On ext4, the first such file is created on disk and subsequent ignorable-only names collide with it (-EEXIST); on f2fs, f2fs_init_casefolded_name() treats len == 0 as a fallback to raw bytewise hashing/matching, writing multiple ignorable-only files to disk that fsck.f2fs flags with hash mismatches.

This series rejects casefolding and case-insensitive matching of names that fold to "", ".", or ".." across libfs (generic_ci_d_hash, generic_ci_match, and generic_ci_validate_strict_name), ext4 (ext4_fname_setup_ci_filename and ext4fs_dirhash), and f2fs (f2fs_init_casefolded_name), while preserving exact byte-equality matching for genuine "." and ".." lookups (such as NFS ext4_get_parent) and opaque byte fallback for invalid UTF-8 sequences in non-strict mode.

Changes in v2:
- Drop redundant generic_ci_d_compare() changes from patch 1.
- Add Fixes: tags for initial ext4 and f2fs casefolding support to patch 1.
- Clarify ext4 vs f2fs behavior for ignorable-only filenames.

Dmitry Torokhov (1):
f2fs: reject casefolded names that fold to ".", "..", or empty

Sarthak Kukreti (2):
libfs: reject casefolded names that fold to ".", "..", or empty
ext4: reject casefolded names that fold to ".", "..", or empty

 fs/ext4/hash-test.c | 14 ++++++++++++++
 fs/ext4/hash.c      |  2 +-
 fs/ext4/namei.c     |  9 +++++++--
 fs/f2fs/dir.c       |  6 +++++-
 fs/libfs.c          | 32 +++++++++++++++++++++++++++++---
 include/linux/fs.h  | 31 ++++++++++++++++++-------------
 6 files changed, 74 insertions(+), 20 deletions(-)
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#1 lore raw

PATCH v2 1/3 libfs: reject casefolded names that fold to ".", "..", or empty

Unicode normalization and casefolding (utf8_casefold()) strips code points with the Default_Ignorable_Code_Point property. As a result:

1. A filename consisting of "." or ".." followed by ignorable characters (e.g. ".\xe2\x80\xae" or "..\xe2\x80\xae") bypasses VFS special-entry handling during path walk, but casefolds to "." or "..".
2. A filename consisting solely of ignorable characters (e.g. "\xe2\x80\xae") casefolds to the empty string "" (length 0), causing any two distinct ignorable-only filenames to compare and hash as equal.

Update generic_ci_d_hash(), generic_ci_match(), and generic_ci_validate_strict_name() to reject case-insensitive hashing, matching, and creation of filenames that casefold to "", ".", or "..", while preserving exact byte-equality matches (such as NFS export lookups of literal ".."). Using a 3-byte stack buffer with utf8_casefold() detects all three cases in O(1) space without heap allocations.

Fixes b886ee3e778e ("ext4: Support case-insensitive file name lookups")
Fixes 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups")
Fixes c843843e714c ("fs: Add standard casefolding support")
---
 fs/libfs.c         | 32 +++++++++++++++++++++++++++++---
 include/linux/fs.h | 31 ++++++++++++++++++-------------
 2 files changed, 47 insertions(+), 16 deletions(-)
fs/libfs.c +29 -3
index 46e0205154e7..77997f88e475 100644
@@ -1828,13 +1828,21 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
 	const struct inode *dir = READ_ONCE(dentry->d_inode);
 	struct super_block *sb = dentry->d_sb;
 	const struct unicode_map *um = sb->s_encoding;
+	unsigned char buf[3];
 	int ret;
 
 	if (!dir || !IS_CASEFOLDED(dir))
 		return 0;
 
 	ret = utf8_casefold_hash(um, dentry, str);
-	if (ret < 0 && sb_has_strict_encoding(sb))
+	if (ret < 0) {
+		if (sb_has_strict_encoding(sb))
+			return -EINVAL;
+		return 0;
+	}
+
+	ret = utf8_casefold(um, str, buf, sizeof(buf));
+	if (ret == 0 || (ret > 0 && name_is_dot_dotdot(buf, ret)))
 		return -EINVAL;
 	return 0;
 }
@@ -1907,10 +1915,28 @@ int generic_ci_match(const struct inode *parent,
 	    !memcmp(name->name, dirent.name, dirent.len))
 		goto out;
 
-	if (folded_name->name)
+	if (name_is_dot_dotdot(dirent.name, dirent.len) ||
+	    name_is_dot_dotdot(name->name, name->len) ||
+	    (folded_name->name &&
+	     (!folded_name->len ||
+	      name_is_dot_dotdot(folded_name->name, folded_name->len)))) {
+		res = 1;
+		goto out;
+	}
+
+	if (folded_name->name) {
 		res = utf8_strncasecmp_folded(um, folded_name, &dirent);
-	else
+	} else {
+		unsigned char buf[3];
+		int cf_len = utf8_casefold(um, name, buf, sizeof(buf));
+
+		if (cf_len == 0 ||
+		    (cf_len > 0 && name_is_dot_dotdot(buf, cf_len))) {
+			res = 1;
+			goto out;
+		}
 		res = utf8_strncasecmp(um, name, &dirent);
+	}
 
 out:
 	kfree(decrypted_name.name);
include/linux/fs.h +18 -13
index 5b3a100aba77..22c411e046ce 100644
@@ -3368,28 +3368,26 @@ int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
  * generic_ci_validate_strict_name - Check if a given name is suitable
  * for a directory
  *
- * This functions checks if the proposed filename is valid for the
- * parent directory. That means that only valid UTF-8 filenames will be
- * accepted for casefold directories from filesystems created with the
- * strict encoding flag.  That also means that any name will be
- * accepted for directories that doesn't have casefold enabled, or
- * aren't being strict with the encoding.
+ * This function checks if the proposed filename is valid for the
+ * parent directory. Names that casefold to "", ".", or ".." are
+ * rejected for all casefolded directories. When the filesystem is
+ * created with the strict encoding flag, only valid UTF-8 filenames
+ * are accepted for casefolded directories.
  *
  * @dir: inode of the directory where the new file will be created
  * @name: name of the new file
  *
  * Return:
- * * True: if the filename is suitable for this directory. It can be
- *   true if a given name is not suitable for a strict encoding
- *   directory, but the directory being used isn't strict
- * * False if the filename isn't suitable for this directory. This only
- *   happens when a directory is casefolded and the filesystem is strict
- *   about its encoding.
+ * * True: if the filename is suitable for this directory.
+ * * False: if the filename isn't suitable for this directory.
  */
 static inline bool generic_ci_validate_strict_name(struct inode *dir,
 						   const struct qstr *name)
 {
-	if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
+	unsigned char buf[3];
+	int len;
+
+	if (!IS_CASEFOLDED(dir))
 		return true;
 
 	/*
@@ -3399,6 +3397,13 @@ static inline bool generic_ci_validate_strict_name(struct inode *dir,
 	if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
 		return true;
 
+	len = utf8_casefold(dir->i_sb->s_encoding, name, buf, sizeof(buf));
+	if (len == 0 || (len > 0 && name_is_dot_dotdot(buf, len)))
+		return false;
+
+	if (!sb_has_strict_encoding(dir->i_sb))
+		return true;
+
 	return !utf8_validate(dir->i_sb->s_encoding, name);
 }
 #else
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#2 lore raw

Re: [f2fs-dev] [PATCH v2 1/3] libfs: reject casefolded names that fold to ".", "..", or empty

Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

Unicode normalization and casefolding (utf8_casefold()) strips code points with the Default_Ignorable_Code_Point property. As a result:

1. A filename consisting of "." or ".." followed by ignorable characters (e.g. ".\xe2\x80\xae" or "..\xe2\x80\xae") bypasses VFS special-entry handling during path walk, but casefolds to "." or "..".
2. A filename consisting solely of ignorable characters (e.g. "\xe2\x80\xae") casefolds to the empty string "" (length 0), causing any two distinct ignorable-only filenames to compare and hash as equal.

Update generic_ci_d_hash(), generic_ci_match(), and generic_ci_validate_strict_name() to reject case-insensitive hashing, matching, and creation of filenames that casefold to "", ".", or "..", while preserving exact byte-equality matches (such as NFS export lookups of literal ".."). Using a 3-byte stack buffer with utf8_casefold() detects all three cases in O(1) space without heap allocations.

Quoted 6 lines
Fixes b886ee3e778e ("ext4: Support case-insensitive file name lookups")
Fixes 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups")
Fixes c843843e714c ("fs: Add standard casefolding support")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [f2fs-dev] [PATCH v2 1/3] libfs: reject casefolded names that fold to ".", "..", or empty

In reply to Sarthak Kukreti

Sarthak Kukreti via Linux-f2fs-devel

Quoted 15 lines · <[email protected]> writes:
fs/libfs.c +1 -0
index 46e0205154e7..77997f88e475 100644
@@ -1828,13 +1828,21 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
 	const struct inode *dir = READ_ONCE(dentry->d_inode);
 	struct super_block *sb = dentry->d_sb;
 	const struct unicode_map *um = sb->s_encoding;
+	unsigned char buf[3];
 	int ret;
 
 	if (!dir || !IS_CASEFOLDED(dir))
 		return 0;
 
 	ret = utf8_casefold_hash(um, dentry, str);

fs/libfs.c int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) · line 1846

Quoted 10 lines
fs/libfs.c +8 -1
-	if (ret < 0 && sb_has_strict_encoding(sb))
+	if (ret < 0) {
+		if (sb_has_strict_encoding(sb))
+			return -EINVAL;
+		return 0;
+	}
+
+	ret = utf8_casefold(um, str, buf, sizeof(buf));
+	if (ret == 0 || (ret > 0 && name_is_dot_dotdot(buf, ret)))
 		return -EINVAL;

I really dislike the idea of doing utf8_casefold here. even on a small substring. We are walking down the trie a second time just to do it again in ->d_compare.

Consider we don't have negative dentries for casefolded directories. Is there a case where variants of '.' or '..' are ever cached if ->lookup rejects them? I don't think so. By rejecting the creation of '.<empty>' ..<empty> we can guarantee there aren't any variants either.

Assuming these don't exist in the dcache, why not let utf8_casefold_hash hash to a non-existing dentry? d_compare will not find it, go to ->lookup and only the filesystem needs special handling to reject invalid names. The vast majority of real lookups will hit the dcache and never pay the cost of this check.

If we *must* check the dcache for some reason I miss, can we do it *only* in d_compare? There, we are doing some kind of utf8_casefold anyway. We can modify utf8_strncasecmp to return the casefolded string, which we can byte compare for dotdot, avoiding multiple walks over the trie.

fs/libfs.c int generic_ci_match(const struct inode *parent, · line 1923

Quoted 13 lines
fs/libfs.c +5 -1
 	return 0;
 }
@@ -1907,10 +1915,28 @@ int generic_ci_match(const struct inode *parent,
 	    !memcmp(name->name, dirent.name, dirent.len))
 		goto out;
 
-	if (folded_name->name)
+	if (name_is_dot_dotdot(dirent.name, dirent.len) ||
+	    name_is_dot_dotdot(name->name, name->len) ||
+	    (folded_name->name &&
+	     (!folded_name->len ||
+	      name_is_dot_dotdot(folded_name->name, folded_name->len))))
 {

Most of these checks seems unnecessary.

First, if name_is_dot_dotdot(dirent.name) is true, which I'm not sure can really happen, it only matters if name_is_dot_dotdot(name->name) or name_is_dot_dotdot(folded_name->name) is also true, because then we have a match. Otherwise, there's no match. Checking both seems redundant.

But also, if name_is_dot_dotdot(name->name) is true, that means we are doing an exact search for '.' or '..', which, IIUC, the VFS will handle specially and not call this code?

I think the only thing you need to check here is:

name_is_dot_dotdot(folded_name->name, folded_name->len)

if folded_name exists. If it doesn't, that's because the cached name initialization failed and you might need to do the utf8_casefold + name_is_dot_dotdot() dance you do in the hunk below:

Quoted 63 lines
fs/libfs.c +12 -1
+
+	if (folded_name->name) {
 		res = utf8_strncasecmp_folded(um, folded_name, &dirent);
-	else
+	} else {
+		unsigned char buf[3];
+		int cf_len = utf8_casefold(um, name, buf, sizeof(buf));
+
+		if (cf_len == 0 ||
+		    (cf_len > 0 && name_is_dot_dotdot(buf, cf_len))) {
+			res = 1;
+			goto out;
+		}
 		res = utf8_strncasecmp(um, name, &dirent);
+	}
 
 out:
 	kfree(decrypted_name.name);
include/linux/fs.h +11 -13
index 5b3a100aba77..22c411e046ce 100644
@@ -3368,28 +3368,26 @@ int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
  * generic_ci_validate_strict_name - Check if a given name is suitable
  * for a directory
  *
- * This functions checks if the proposed filename is valid for the
- * parent directory. That means that only valid UTF-8 filenames will be
- * accepted for casefold directories from filesystems created with the
- * strict encoding flag.  That also means that any name will be
- * accepted for directories that doesn't have casefold enabled, or
- * aren't being strict with the encoding.
+ * This function checks if the proposed filename is valid for the
+ * parent directory. Names that casefold to "", ".", or ".." are
+ * rejected for all casefolded directories. When the filesystem is
+ * created with the strict encoding flag, only valid UTF-8 filenames
+ * are accepted for casefolded directories.
  *
  * @dir: inode of the directory where the new file will be created
  * @name: name of the new file
  *
  * Return:
- * * True: if the filename is suitable for this directory. It can be
- *   true if a given name is not suitable for a strict encoding
- *   directory, but the directory being used isn't strict
- * * False if the filename isn't suitable for this directory. This only
- *   happens when a directory is casefolded and the filesystem is strict
- *   about its encoding.
+ * * True: if the filename is suitable for this directory.
+ * * False: if the filename isn't suitable for this directory.
  */
 static inline bool generic_ci_validate_strict_name(struct inode *dir,
 						   const struct qstr *name)
 {
-	if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
+	unsigned char buf[3];
+	int len;
+
+	if (!IS_CASEFOLDED(dir))
 		return true;
 
 	/*

include/linux/fs.h static inline bool generic_ci_validate_strict_name(struct inode *dir, · line 3403

Quoted 7 lines
include/linux/fs.h +4 -0
@@ -3399,6 +3397,13 @@ static inline bool generic_ci_validate_strict_name(struct inode *dir,
 	if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
 		return true;
 
+	len = utf8_casefold(dir->i_sb->s_encoding, name, buf, sizeof(buf));
+	if (len == 0 || (len > 0 && name_is_dot_dotdot(buf, len)))
+		return false;
+

With this in place we should not be creating new files that turn into '.' and '..'. If we also reject to create them during a lookup, I think it is enough to drop the dcache callback changes.

Signature · 1 line
Gabriel Krisman Bertazi
#4 lore raw

PATCH v2 2/3 ext4: reject casefolded names that fold to ".", "..", or empty

In reply to Sarthak Kukreti

Filenames consisting solely of ignorable Unicode characters fold to empty strings (causing the first such file to be created on disk and subsequent distinct ignorable-only names to collide with it), while names with leading dots and ignorable characters fold to "." or "..", colliding with on-disk "." and ".." entries and triggering EXT4_ERROR_INODE in ext4_lookup().

Skip casefolding when the lookup name is literally "." or "..", reject any non-dot names that fold to "", ".", or ".." in ext4_fname_setup_ci_filename(), and fall back to opaque byte hashing in ext4fs_dirhash() when utf8_casefold() yields "", ".", or "..".

Fixes b886ee3e778e ("ext4: Support case-insensitive file name lookups")
---
 fs/ext4/hash-test.c | 14 ++++++++++++++
 fs/ext4/hash.c      |  2 +-
 fs/ext4/namei.c     |  9 +++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)
fs/ext4/hash-test.c +14 -0
index 49b0d874c833..a8b206a6d751 100644
@@ -541,6 +541,20 @@ static void test_ext4fs_dirhash_casefold_fallback(struct kunit *test)
 	KUNIT_ASSERT_EQ(test, ret_plain, 0);
 	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
 	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
+
+	ret_cf = ext4fs_dirhash(&ei->vfs_inode, ".\xe2\x80\xae", 4, &folded_dir);
+	ret_plain = ext4fs_dirhash(plain_dir, ".\xe2\x80\xae", 4, &plain);
+	KUNIT_ASSERT_EQ(test, ret_cf, 0);
+	KUNIT_ASSERT_EQ(test, ret_plain, 0);
+	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
+	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
+
+	ret_cf = ext4fs_dirhash(&ei->vfs_inode, "\xe2\x80\xae", 3, &folded_dir);
+	ret_plain = ext4fs_dirhash(plain_dir, "\xe2\x80\xae", 3, &plain);
+	KUNIT_ASSERT_EQ(test, ret_cf, 0);
+	KUNIT_ASSERT_EQ(test, ret_plain, 0);
+	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
+	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
 }
 #endif
 
fs/ext4/hash.c +1 -1
index 978bd92da0ad..65019de4af93 100644
@@ -327,7 +327,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 			return -ENOMEM;
 
 		dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
-		if (dlen < 0) {
+		if (dlen <= 0 || name_is_dot_dotdot(buff, dlen)) {
 			kfree(buff);
 			goto opaque_seq;
 		}
fs/ext4/namei.c +7 -2
index 354fc2d22df1..cd2c9b873cc6 100644
@@ -1368,7 +1368,8 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 	int len;
 
 	if (!IS_CASEFOLDED(dir) ||
-	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
+	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir)) ||
+	    name_is_dot_dotdot(iname->name, iname->len)) {
 		cf_name->name = NULL;
 		return 0;
 	}
@@ -1378,9 +1379,13 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 		return -ENOMEM;
 
 	len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
-	if (len <= 0) {
+	if (len < 0) {
 		kfree(buf);
 		buf = NULL;
+	} else if (len == 0 || name_is_dot_dotdot(buf, len)) {
+		kfree(buf);
+		cf_name->name = NULL;
+		return -EINVAL;
 	}
 	cf_name->name = buf;
 	cf_name->len = (unsigned) len;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#5 lore raw

Re: [PATCH v2 2/3] ext4: reject casefolded names that fold to ".", "..", or empty

Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

Filenames consisting solely of ignorable Unicode characters fold to empty strings (causing the first such file to be created on disk and subsequent distinct ignorable-only names to collide with it), while names with leading dots and ignorable characters fold to "." or "..", colliding with on-disk "." and ".." entries and triggering EXT4_ERROR_INODE in ext4_lookup().

Skip casefolding when the lookup name is literally "." or "..", reject any non-dot names that fold to "", ".", or ".." in ext4_fname_setup_ci_filename(), and fall back to opaque byte hashing in ext4fs_dirhash() when utf8_casefold() yields "", ".", or "..".

Quoted 4 lines
Fixes b886ee3e778e ("ext4: Support case-insensitive file name lookups")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [f2fs-dev] [PATCH v2 2/3] ext4: reject casefolded names that fold to ".", "..", or empty

In reply to Sarthak Kukreti

Sarthak Kukreti via Linux-f2fs-devel

Quoted 65 lines · <[email protected]> writes:

Filenames consisting solely of ignorable Unicode characters fold to empty strings (causing the first such file to be created on disk and subsequent distinct ignorable-only names to collide with it), while names with leading dots and ignorable characters fold to "." or "..", colliding with on-disk "." and ".." entries and triggering EXT4_ERROR_INODE in ext4_lookup().

Skip casefolding when the lookup name is literally "." or "..", reject any non-dot names that fold to "", ".", or ".." in ext4_fname_setup_ci_filename(), and fall back to opaque byte hashing in ext4fs_dirhash() when utf8_casefold() yields "", ".", or "..".

Fixes b886ee3e778e ("ext4: Support case-insensitive file name lookups")
---
 fs/ext4/hash-test.c | 14 ++++++++++++++
 fs/ext4/hash.c      |  2 +-
 fs/ext4/namei.c     |  9 +++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)
fs/ext4/hash-test.c +14 -0
index 49b0d874c833..a8b206a6d751 100644
@@ -541,6 +541,20 @@ static void test_ext4fs_dirhash_casefold_fallback(struct kunit *test)
 	KUNIT_ASSERT_EQ(test, ret_plain, 0);
 	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
 	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
+
+	ret_cf = ext4fs_dirhash(&ei->vfs_inode, ".\xe2\x80\xae", 4, &folded_dir);
+	ret_plain = ext4fs_dirhash(plain_dir, ".\xe2\x80\xae", 4, &plain);
+	KUNIT_ASSERT_EQ(test, ret_cf, 0);
+	KUNIT_ASSERT_EQ(test, ret_plain, 0);
+	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
+	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
+
+	ret_cf = ext4fs_dirhash(&ei->vfs_inode, "\xe2\x80\xae", 3, &folded_dir);
+	ret_plain = ext4fs_dirhash(plain_dir, "\xe2\x80\xae", 3, &plain);
+	KUNIT_ASSERT_EQ(test, ret_cf, 0);
+	KUNIT_ASSERT_EQ(test, ret_plain, 0);
+	KUNIT_EXPECT_EQ(test, folded_dir.hash, plain.hash);
+	KUNIT_EXPECT_EQ(test, folded_dir.minor_hash, plain.minor_hash);
 }
 #endif
 
fs/ext4/hash.c +1 -1
index 978bd92da0ad..65019de4af93 100644
@@ -327,7 +327,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 			return -ENOMEM;
 
 		dlen = utf8_casefold(um, &qstr, buff, PATH_MAX);
-		if (dlen < 0) {
+		if (dlen <= 0 || name_is_dot_dotdot(buff, dlen)) {
 			kfree(buff);
 			goto opaque_seq;
 		}
fs/ext4/namei.c +0 -0
index 354fc2d22df1..cd2c9b873cc6 100644

fs/ext4/namei.c int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, · line 1372

Quoted 6 lines
fs/ext4/namei.c +2 -1
@@ -1368,7 +1368,8 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 	int len;
 
 	if (!IS_CASEFOLDED(dir) ||
-	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
+	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir)) ||
+	    name_is_dot_dotdot(iname->name, iname->len)) {

if name_is_dot_dotdot(iname->name, iname->len) is true, isn't that a byte-exact match to dot or dotdot? Do we actually get here for byte-exact matches?

The rest of the patch looks good to me!

Quoted 27 lines
fs/ext4/namei.c +5 -1
 		cf_name->name = NULL;
 		return 0;
 	}
@@ -1378,9 +1379,13 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 		return -ENOMEM;
 
 	len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
-	if (len <= 0) {
+	if (len < 0) {
 		kfree(buf);
 		buf = NULL;
+	} else if (len == 0 || name_is_dot_dotdot(buf, len)) {
+		kfree(buf);
+		cf_name->name = NULL;
+		return -EINVAL;
 	}
 	cf_name->name = buf;
 	cf_name->len = (unsigned) len;
Signature · 8 lines
2.56.1.750.gf70dc682cf-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Signature · 1 line
Gabriel Krisman Bertazi
#7 lore raw

PATCH v2 3/3 f2fs: reject casefolded names that fold to ".", "..", or empty

In reply to Sarthak Kukreti

From: Dmitry Torokhov <[email protected]>

Filenames consisting only of ignorable Unicode characters fold to empty strings, and names with leading dots and ignorable characters can fold to "." or "..". Allowing such names in casefolded directories can corrupt directory structure or cause confusion with special entries.

Reject any non-dot names that fold to empty, ".", or ".." during casefolded name setup, while still allowing invalid UTF-8 sequences to be handled as opaque byte sequences when strict mode is not enabled.

Fixes 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups")
---
 fs/f2fs/dir.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/f2fs/dir.c +5 -1
index 97f7fef6b068..f1d40f818b35 100644
@@ -77,13 +77,17 @@ int f2fs_init_casefolded_name(const struct inode *dir,
 
 		len = utf8_casefold(sb->s_encoding, fname->usr_fname,
 				    buf, F2FS_NAME_LEN);
-		if (len <= 0) {
+		if (len < 0) {
 			kmem_cache_free(f2fs_cf_name_slab, buf);
 			if (sb_has_strict_encoding(sb))
 				return -EINVAL;
 			/* fall back to treating name as opaque byte sequence */
 			return 0;
 		}
+		if (len == 0 || name_is_dot_dotdot(buf, len)) {
+			kmem_cache_free(f2fs_cf_name_slab, buf);
+			return -EINVAL;
+		}
 		fname->cf_name.name = buf;
 		fname->cf_name.len = len;
 	}
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#8 lore raw

Re: [PATCH v2 3/3] f2fs: reject casefolded names that fold to ".", "..", or empty

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

From: Dmitry Torokhov <[email protected]>

Filenames consisting only of ignorable Unicode characters fold to empty strings, and names with leading dots and ignorable characters can fold to "." or "..". Allowing such names in casefolded directories can corrupt directory structure or cause confusion with special entries.

Quoted 7 lines

Reject any non-dot names that fold to empty, ".", or ".." during casefolded name setup, while still allowing invalid UTF-8 sequences to be handled as opaque byte sequences when strict mode is not enabled.

Fixes 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Reviewed-by on v2 3/3
#9 lore raw

Re: [f2fs-dev] [PATCH v2 3/3] f2fs: reject casefolded names that fold to ".", "..", or empty

In reply to Sarthak Kukreti

Sarthak Kukreti via Linux-f2fs-devel

Quoted 7 lines · <[email protected]> writes:

From: Dmitry Torokhov <[email protected]>

Filenames consisting only of ignorable Unicode characters fold to empty strings, and names with leading dots and ignorable characters can fold to "." or "..". Allowing such names in casefolded directories can corrupt directory structure or cause confusion with special entries.

Quoted 7 lines

Reject any non-dot names that fold to empty, ".", or ".." during casefolded name setup, while still allowing invalid UTF-8 sequences to be handled as opaque byte sequences when strict mode is not enabled.

Fixes 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups")

I don't have any comments on this one. Fell free to add:

Signature · 1 line
Gabriel Krisman Bertazi