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") Co-developed-by: Dmitry Torokhov Signed-off-by: Dmitry Torokhov Signed-off-by: Sarthak Kukreti --- fs/ext4/hash-test.c | 14 ++++++++++++++ fs/ext4/hash.c | 2 +- fs/ext4/namei.c | 9 +++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fs/ext4/hash-test.c b/fs/ext4/hash-test.c index 49b0d874c833..a8b206a6d751 100644 --- a/fs/ext4/hash-test.c +++ b/fs/ext4/hash-test.c @@ -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 diff --git a/fs/ext4/hash.c b/fs/ext4/hash.c index 978bd92da0ad..65019de4af93 100644 --- a/fs/ext4/hash.c +++ b/fs/ext4/hash.c @@ -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; } diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 354fc2d22df1..cd2c9b873cc6 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -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; -- 2.56.1.750.gf70dc682cf-goog