@@ -1390,62 +1390,11 @@ static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1390
1390
}
1391
1391
1392
1392
#if IS_ENABLED (CONFIG_UNICODE )
1393
- /*
1394
- * Test whether a case-insensitive directory entry matches the filename
1395
- * being searched for. If quick is set, assume the name being looked up
1396
- * is already in the casefolded form.
1397
- *
1398
- * Returns: 0 if the directory entry matches, more than 0 if it
1399
- * doesn't match or less than zero on error.
1400
- */
1401
- static int ext4_ci_compare (const struct inode * parent , const struct qstr * name ,
1402
- u8 * de_name , size_t de_name_len , bool quick )
1403
- {
1404
- const struct super_block * sb = parent -> i_sb ;
1405
- const struct unicode_map * um = sb -> s_encoding ;
1406
- struct fscrypt_str decrypted_name = FSTR_INIT (NULL , de_name_len );
1407
- struct qstr entry = QSTR_INIT (de_name , de_name_len );
1408
- int ret ;
1409
-
1410
- if (IS_ENCRYPTED (parent )) {
1411
- const struct fscrypt_str encrypted_name =
1412
- FSTR_INIT (de_name , de_name_len );
1413
-
1414
- decrypted_name .name = kmalloc (de_name_len , GFP_KERNEL );
1415
- if (!decrypted_name .name )
1416
- return - ENOMEM ;
1417
- ret = fscrypt_fname_disk_to_usr (parent , 0 , 0 , & encrypted_name ,
1418
- & decrypted_name );
1419
- if (ret < 0 )
1420
- goto out ;
1421
- entry .name = decrypted_name .name ;
1422
- entry .len = decrypted_name .len ;
1423
- }
1424
-
1425
- if (quick )
1426
- ret = utf8_strncasecmp_folded (um , name , & entry );
1427
- else
1428
- ret = utf8_strncasecmp (um , name , & entry );
1429
- if (ret < 0 ) {
1430
- /* Handle invalid character sequence as either an error
1431
- * or as an opaque byte sequence.
1432
- */
1433
- if (sb_has_strict_encoding (sb ))
1434
- ret = - EINVAL ;
1435
- else if (name -> len != entry .len )
1436
- ret = 1 ;
1437
- else
1438
- ret = !!memcmp (name -> name , entry .name , entry .len );
1439
- }
1440
- out :
1441
- kfree (decrypted_name .name );
1442
- return ret ;
1443
- }
1444
-
1445
1393
int ext4_fname_setup_ci_filename (struct inode * dir , const struct qstr * iname ,
1446
1394
struct ext4_filename * name )
1447
1395
{
1448
- struct fscrypt_str * cf_name = & name -> cf_name ;
1396
+ struct qstr * cf_name = & name -> cf_name ;
1397
+ unsigned char * buf ;
1449
1398
struct dx_hash_info * hinfo = & name -> hinfo ;
1450
1399
int len ;
1451
1400
@@ -1455,18 +1404,18 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
1455
1404
return 0 ;
1456
1405
}
1457
1406
1458
- cf_name -> name = kmalloc (EXT4_NAME_LEN , GFP_NOFS );
1459
- if (!cf_name -> name )
1407
+ buf = kmalloc (EXT4_NAME_LEN , GFP_NOFS );
1408
+ if (!buf )
1460
1409
return - ENOMEM ;
1461
1410
1462
- len = utf8_casefold (dir -> i_sb -> s_encoding ,
1463
- iname , cf_name -> name ,
1464
- EXT4_NAME_LEN );
1411
+ len = utf8_casefold (dir -> i_sb -> s_encoding , iname , buf , EXT4_NAME_LEN );
1465
1412
if (len <= 0 ) {
1466
- kfree (cf_name -> name );
1467
- cf_name -> name = NULL ;
1413
+ kfree (buf );
1414
+ buf = NULL ;
1468
1415
}
1416
+ cf_name -> name = buf ;
1469
1417
cf_name -> len = (unsigned ) len ;
1418
+
1470
1419
if (!IS_ENCRYPTED (dir ))
1471
1420
return 0 ;
1472
1421
@@ -1502,22 +1451,29 @@ static bool ext4_match(struct inode *parent,
1502
1451
#if IS_ENABLED (CONFIG_UNICODE )
1503
1452
if (IS_CASEFOLDED (parent ) &&
1504
1453
(!IS_ENCRYPTED (parent ) || fscrypt_has_encryption_key (parent ))) {
1505
- if (fname -> cf_name .name ) {
1506
- struct qstr cf = {.name = fname -> cf_name .name ,
1507
- .len = fname -> cf_name .len };
1508
- if (IS_ENCRYPTED (parent )) {
1509
- if (fname -> hinfo .hash != EXT4_DIRENT_HASH (de ) ||
1510
- fname -> hinfo .minor_hash !=
1511
- EXT4_DIRENT_MINOR_HASH (de )) {
1512
-
1513
- return false;
1514
- }
1515
- }
1516
- return !ext4_ci_compare (parent , & cf , de -> name ,
1517
- de -> name_len , true);
1518
- }
1519
- return !ext4_ci_compare (parent , fname -> usr_fname , de -> name ,
1520
- de -> name_len , false);
1454
+ /*
1455
+ * Just checking IS_ENCRYPTED(parent) below is not
1456
+ * sufficient to decide whether one can use the hash for
1457
+ * skipping the string comparison, because the key might
1458
+ * have been added right after
1459
+ * ext4_fname_setup_ci_filename(). In this case, a hash
1460
+ * mismatch will be a false negative. Therefore, make
1461
+ * sure cf_name was properly initialized before
1462
+ * considering the calculated hash.
1463
+ */
1464
+ if (IS_ENCRYPTED (parent ) && fname -> cf_name .name &&
1465
+ (fname -> hinfo .hash != EXT4_DIRENT_HASH (de ) ||
1466
+ fname -> hinfo .minor_hash != EXT4_DIRENT_MINOR_HASH (de )))
1467
+ return false;
1468
+ /*
1469
+ * Treat comparison errors as not a match. The
1470
+ * only case where it happens is on a disk
1471
+ * corruption or ENOMEM.
1472
+ */
1473
+
1474
+ return generic_ci_match (parent , fname -> usr_fname ,
1475
+ & fname -> cf_name , de -> name ,
1476
+ de -> name_len ) > 0 ;
1521
1477
}
1522
1478
#endif
1523
1479
@@ -1869,16 +1825,15 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
1869
1825
}
1870
1826
}
1871
1827
1872
- #if IS_ENABLED (CONFIG_UNICODE )
1873
- if (!inode && IS_CASEFOLDED (dir )) {
1828
+ if (IS_ENABLED (CONFIG_UNICODE ) && !inode && IS_CASEFOLDED (dir )) {
1874
1829
/* Eventually we want to call d_add_ci(dentry, NULL)
1875
1830
* for negative dentries in the encoding case as
1876
1831
* well. For now, prevent the negative dentry
1877
1832
* from being cached.
1878
1833
*/
1879
1834
return NULL ;
1880
1835
}
1881
- #endif
1836
+
1882
1837
return d_splice_alias (inode , dentry );
1883
1838
}
1884
1839
@@ -3208,16 +3163,14 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
3208
3163
ext4_fc_track_unlink (handle , dentry );
3209
3164
retval = ext4_mark_inode_dirty (handle , dir );
3210
3165
3211
- #if IS_ENABLED (CONFIG_UNICODE )
3212
3166
/* VFS negative dentries are incompatible with Encoding and
3213
3167
* Case-insensitiveness. Eventually we'll want avoid
3214
3168
* invalidating the dentries here, alongside with returning the
3215
3169
* negative dentries at ext4_lookup(), when it is better
3216
3170
* supported by the VFS for the CI case.
3217
3171
*/
3218
- if (IS_CASEFOLDED (dir ))
3172
+ if (IS_ENABLED ( CONFIG_UNICODE ) && IS_CASEFOLDED (dir ))
3219
3173
d_invalidate (dentry );
3220
- #endif
3221
3174
3222
3175
end_rmdir :
3223
3176
brelse (bh );
@@ -3319,16 +3272,15 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)
3319
3272
goto out_trace ;
3320
3273
3321
3274
retval = __ext4_unlink (dir , & dentry -> d_name , d_inode (dentry ), dentry );
3322
- #if IS_ENABLED ( CONFIG_UNICODE )
3275
+
3323
3276
/* VFS negative dentries are incompatible with Encoding and
3324
3277
* Case-insensitiveness. Eventually we'll want avoid
3325
3278
* invalidating the dentries here, alongside with returning the
3326
3279
* negative dentries at ext4_lookup(), when it is better
3327
3280
* supported by the VFS for the CI case.
3328
3281
*/
3329
- if (IS_CASEFOLDED (dir ))
3282
+ if (IS_ENABLED ( CONFIG_UNICODE ) && IS_CASEFOLDED (dir ))
3330
3283
d_invalidate (dentry );
3331
- #endif
3332
3284
3333
3285
out_trace :
3334
3286
trace_ext4_unlink_exit (dentry , retval );
0 commit comments