Skip to content

Commit b01531d

Browse files
ebiggerstytso
authored andcommitted
fscrypt: fix race where ->lookup() marks plaintext dentry as ciphertext
->lookup() in an encrypted directory begins as follows: 1. fscrypt_prepare_lookup(): a. Try to load the directory's encryption key. b. If the key is unavailable, mark the dentry as a ciphertext name via d_flags. 2. fscrypt_setup_filename(): a. Try to load the directory's encryption key. b. If the key is available, encrypt the name (treated as a plaintext name) to get the on-disk name. Otherwise decode the name (treated as a ciphertext name) to get the on-disk name. But if the key is concurrently added, it may be found at (2a) but not at (1a). In this case, the dentry will be wrongly marked as a ciphertext name even though it was actually treated as plaintext. This will cause the dentry to be wrongly invalidated on the next lookup, potentially causing problems. For example, if the racy ->lookup() was part of sys_mount(), then the new mount will be detached when anything tries to access it. This is despite the mountpoint having a plaintext path, which should remain valid now that the key was added. Of course, this is only possible if there's a userspace race. Still, the additional kernel-side race is confusing and unexpected. Close the kernel-side race by changing fscrypt_prepare_lookup() to also set the on-disk filename (step 2b), consistent with the d_flags update. Fixes: 28b4c26 ("ext4 crypto: revalidate dentry after adding or removing the key") Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent d456a33 commit b01531d

7 files changed

Lines changed: 139 additions & 66 deletions

File tree

‎fs/crypto/fname.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
356356
}
357357
if (!lookup)
358358
return -ENOKEY;
359+
fname->is_ciphertext_name = true;
359360

360361
/*
361362
* We don't have the key and we are doing a lookup; decode the

‎fs/crypto/hooks.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
104104
}
105105
EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
106106

107-
int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry)
107+
int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
108+
struct fscrypt_name *fname)
108109
{
109-
int err = fscrypt_get_encryption_info(dir);
110+
int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
110111

111-
if (err)
112+
if (err && err != -ENOENT)
112113
return err;
113114

114-
if (!fscrypt_has_encryption_key(dir)) {
115+
if (fname->is_ciphertext_name) {
115116
spin_lock(&dentry->d_lock);
116117
dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
117118
spin_unlock(&dentry->d_lock);
118119
d_set_d_op(dentry, &fscrypt_d_ops);
119120
}
120-
return 0;
121+
return err;
121122
}
122123
EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
123124

‎fs/ext4/ext4.h‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,23 +2287,47 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
22872287
ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
22882288

22892289
#ifdef CONFIG_FS_ENCRYPTION
2290+
static inline void ext4_fname_from_fscrypt_name(struct ext4_filename *dst,
2291+
const struct fscrypt_name *src)
2292+
{
2293+
memset(dst, 0, sizeof(*dst));
2294+
2295+
dst->usr_fname = src->usr_fname;
2296+
dst->disk_name = src->disk_name;
2297+
dst->hinfo.hash = src->hash;
2298+
dst->hinfo.minor_hash = src->minor_hash;
2299+
dst->crypto_buf = src->crypto_buf;
2300+
}
2301+
22902302
static inline int ext4_fname_setup_filename(struct inode *dir,
2291-
const struct qstr *iname,
2292-
int lookup, struct ext4_filename *fname)
2303+
const struct qstr *iname,
2304+
int lookup,
2305+
struct ext4_filename *fname)
22932306
{
22942307
struct fscrypt_name name;
22952308
int err;
22962309

2297-
memset(fname, 0, sizeof(struct ext4_filename));
2298-
22992310
err = fscrypt_setup_filename(dir, iname, lookup, &name);
2311+
if (err)
2312+
return err;
23002313

2301-
fname->usr_fname = name.usr_fname;
2302-
fname->disk_name = name.disk_name;
2303-
fname->hinfo.hash = name.hash;
2304-
fname->hinfo.minor_hash = name.minor_hash;
2305-
fname->crypto_buf = name.crypto_buf;
2306-
return err;
2314+
ext4_fname_from_fscrypt_name(fname, &name);
2315+
return 0;
2316+
}
2317+
2318+
static inline int ext4_fname_prepare_lookup(struct inode *dir,
2319+
struct dentry *dentry,
2320+
struct ext4_filename *fname)
2321+
{
2322+
struct fscrypt_name name;
2323+
int err;
2324+
2325+
err = fscrypt_prepare_lookup(dir, dentry, &name);
2326+
if (err)
2327+
return err;
2328+
2329+
ext4_fname_from_fscrypt_name(fname, &name);
2330+
return 0;
23072331
}
23082332

23092333
static inline void ext4_fname_free_filename(struct ext4_filename *fname)
@@ -2317,19 +2341,27 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname)
23172341
fname->usr_fname = NULL;
23182342
fname->disk_name.name = NULL;
23192343
}
2320-
#else
2344+
#else /* !CONFIG_FS_ENCRYPTION */
23212345
static inline int ext4_fname_setup_filename(struct inode *dir,
2322-
const struct qstr *iname,
2323-
int lookup, struct ext4_filename *fname)
2346+
const struct qstr *iname,
2347+
int lookup,
2348+
struct ext4_filename *fname)
23242349
{
23252350
fname->usr_fname = iname;
23262351
fname->disk_name.name = (unsigned char *) iname->name;
23272352
fname->disk_name.len = iname->len;
23282353
return 0;
23292354
}
2330-
static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
23312355

2332-
#endif
2356+
static inline int ext4_fname_prepare_lookup(struct inode *dir,
2357+
struct dentry *dentry,
2358+
struct ext4_filename *fname)
2359+
{
2360+
return ext4_fname_setup_filename(dir, &dentry->d_name, 1, fname);
2361+
}
2362+
2363+
static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
2364+
#endif /* !CONFIG_FS_ENCRYPTION */
23332365

23342366
/* dir.c */
23352367
extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,

‎fs/ext4/namei.c‎

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
13271327
}
13281328

13291329
/*
1330-
* ext4_find_entry()
1330+
* __ext4_find_entry()
13311331
*
13321332
* finds an entry in the specified directory with the wanted name. It
13331333
* returns the cache buffer in which the entry was found, and the entry
@@ -1337,39 +1337,32 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
13371337
* The returned buffer_head has ->b_count elevated. The caller is expected
13381338
* to brelse() it when appropriate.
13391339
*/
1340-
static struct buffer_head * ext4_find_entry (struct inode *dir,
1341-
const struct qstr *d_name,
1342-
struct ext4_dir_entry_2 **res_dir,
1343-
int *inlined)
1340+
static struct buffer_head *__ext4_find_entry(struct inode *dir,
1341+
struct ext4_filename *fname,
1342+
struct ext4_dir_entry_2 **res_dir,
1343+
int *inlined)
13441344
{
13451345
struct super_block *sb;
13461346
struct buffer_head *bh_use[NAMEI_RA_SIZE];
13471347
struct buffer_head *bh, *ret = NULL;
13481348
ext4_lblk_t start, block;
1349-
const u8 *name = d_name->name;
1349+
const u8 *name = fname->usr_fname->name;
13501350
size_t ra_max = 0; /* Number of bh's in the readahead
13511351
buffer, bh_use[] */
13521352
size_t ra_ptr = 0; /* Current index into readahead
13531353
buffer */
13541354
ext4_lblk_t nblocks;
13551355
int i, namelen, retval;
1356-
struct ext4_filename fname;
13571356

13581357
*res_dir = NULL;
13591358
sb = dir->i_sb;
1360-
namelen = d_name->len;
1359+
namelen = fname->usr_fname->len;
13611360
if (namelen > EXT4_NAME_LEN)
13621361
return NULL;
13631362

1364-
retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1365-
if (retval == -ENOENT)
1366-
return NULL;
1367-
if (retval)
1368-
return ERR_PTR(retval);
1369-
13701363
if (ext4_has_inline_data(dir)) {
13711364
int has_inline_data = 1;
1372-
ret = ext4_find_inline_entry(dir, &fname, res_dir,
1365+
ret = ext4_find_inline_entry(dir, fname, res_dir,
13731366
&has_inline_data);
13741367
if (has_inline_data) {
13751368
if (inlined)
@@ -1389,7 +1382,7 @@ static struct buffer_head * ext4_find_entry (struct inode *dir,
13891382
goto restart;
13901383
}
13911384
if (is_dx(dir)) {
1392-
ret = ext4_dx_find_entry(dir, &fname, res_dir);
1385+
ret = ext4_dx_find_entry(dir, fname, res_dir);
13931386
/*
13941387
* On success, or if the error was file not found,
13951388
* return. Otherwise, fall back to doing a search the
@@ -1453,7 +1446,7 @@ static struct buffer_head * ext4_find_entry (struct inode *dir,
14531446
goto cleanup_and_exit;
14541447
}
14551448
set_buffer_verified(bh);
1456-
i = search_dirblock(bh, dir, &fname,
1449+
i = search_dirblock(bh, dir, fname,
14571450
block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
14581451
if (i == 1) {
14591452
EXT4_I(dir)->i_dir_start_lookup = block;
@@ -1484,10 +1477,50 @@ static struct buffer_head * ext4_find_entry (struct inode *dir,
14841477
/* Clean up the read-ahead blocks */
14851478
for (; ra_ptr < ra_max; ra_ptr++)
14861479
brelse(bh_use[ra_ptr]);
1487-
ext4_fname_free_filename(&fname);
14881480
return ret;
14891481
}
14901482

1483+
static struct buffer_head *ext4_find_entry(struct inode *dir,
1484+
const struct qstr *d_name,
1485+
struct ext4_dir_entry_2 **res_dir,
1486+
int *inlined)
1487+
{
1488+
int err;
1489+
struct ext4_filename fname;
1490+
struct buffer_head *bh;
1491+
1492+
err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
1493+
if (err == -ENOENT)
1494+
return NULL;
1495+
if (err)
1496+
return ERR_PTR(err);
1497+
1498+
bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
1499+
1500+
ext4_fname_free_filename(&fname);
1501+
return bh;
1502+
}
1503+
1504+
static struct buffer_head *ext4_lookup_entry(struct inode *dir,
1505+
struct dentry *dentry,
1506+
struct ext4_dir_entry_2 **res_dir)
1507+
{
1508+
int err;
1509+
struct ext4_filename fname;
1510+
struct buffer_head *bh;
1511+
1512+
err = ext4_fname_prepare_lookup(dir, dentry, &fname);
1513+
if (err == -ENOENT)
1514+
return NULL;
1515+
if (err)
1516+
return ERR_PTR(err);
1517+
1518+
bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
1519+
1520+
ext4_fname_free_filename(&fname);
1521+
return bh;
1522+
}
1523+
14911524
static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
14921525
struct ext4_filename *fname,
14931526
struct ext4_dir_entry_2 **res_dir)
@@ -1546,16 +1579,11 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
15461579
struct inode *inode;
15471580
struct ext4_dir_entry_2 *de;
15481581
struct buffer_head *bh;
1549-
int err;
1550-
1551-
err = fscrypt_prepare_lookup(dir, dentry, flags);
1552-
if (err)
1553-
return ERR_PTR(err);
15541582

15551583
if (dentry->d_name.len > EXT4_NAME_LEN)
15561584
return ERR_PTR(-ENAMETOOLONG);
15571585

1558-
bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
1586+
bh = ext4_lookup_entry(dir, dentry, &de);
15591587
if (IS_ERR(bh))
15601588
return ERR_CAST(bh);
15611589
inode = NULL;

‎fs/f2fs/namei.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,19 +436,23 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
436436
nid_t ino = -1;
437437
int err = 0;
438438
unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
439+
struct fscrypt_name fname;
439440

440441
trace_f2fs_lookup_start(dir, dentry, flags);
441442

442-
err = fscrypt_prepare_lookup(dir, dentry, flags);
443-
if (err)
444-
goto out;
445-
446443
if (dentry->d_name.len > F2FS_NAME_LEN) {
447444
err = -ENAMETOOLONG;
448445
goto out;
449446
}
450447

451-
de = f2fs_find_entry(dir, &dentry->d_name, &page);
448+
err = fscrypt_prepare_lookup(dir, dentry, &fname);
449+
if (err == -ENOENT)
450+
goto out_splice;
451+
if (err)
452+
goto out;
453+
de = __f2fs_find_entry(dir, &fname, &page);
454+
fscrypt_free_filename(&fname);
455+
452456
if (!de) {
453457
if (IS_ERR(page)) {
454458
err = PTR_ERR(page);
@@ -488,8 +492,7 @@ static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
488492
}
489493
out_splice:
490494
new = d_splice_alias(inode, dentry);
491-
if (IS_ERR(new))
492-
err = PTR_ERR(new);
495+
err = PTR_ERR_OR_ZERO(new);
493496
trace_f2fs_lookup_end(dir, dentry, ino, err);
494497
return new;
495498
out_iput:

‎fs/ubifs/dir.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,9 @@ static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
220220

221221
dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
222222

223-
err = fscrypt_prepare_lookup(dir, dentry, flags);
224-
if (err)
225-
return ERR_PTR(err);
226-
227-
err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
223+
err = fscrypt_prepare_lookup(dir, dentry, &nm);
224+
if (err == -ENOENT)
225+
return d_splice_alias(NULL, dentry);
228226
if (err)
229227
return ERR_PTR(err);
230228

0 commit comments

Comments
 (0)