Skip to content

Commit a9fbcd6

Browse files
committed
Merge tag 'fscrypt_for_linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fscrypt updates from Ted Ts'o: "Clean up fscrypt's dcache revalidation support, and other miscellaneous cleanups" * tag 'fscrypt_for_linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: fscrypt: cache decrypted symlink target in ->i_link vfs: use READ_ONCE() to access ->i_link fscrypt: fix race where ->lookup() marks plaintext dentry as ciphertext fscrypt: only set dentry_operations on ciphertext dentries fs, fscrypt: clear DCACHE_ENCRYPTED_NAME when unaliasing directory fscrypt: fix race allowing rename() and link() of ciphertext dentries fscrypt: clean up and improve dentry revalidation fscrypt: use READ_ONCE() to access ->i_crypt_info fscrypt: remove WARN_ON_ONCE() when decryption fails fscrypt: drop inode argument from fscrypt_get_ctx()
2 parents 5abe379 + 2c58d54 commit a9fbcd6

File tree

18 files changed

+294
-144
lines changed

18 files changed

+294
-144
lines changed

fs/crypto/bio.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
3636
int ret = fscrypt_decrypt_page(page->mapping->host, page,
3737
PAGE_SIZE, 0, page->index);
3838

39-
if (ret) {
40-
WARN_ON_ONCE(1);
39+
if (ret)
4140
SetPageError(page);
42-
} else if (done) {
41+
else if (done)
4342
SetPageUptodate(page);
44-
}
4543
if (done)
4644
unlock_page(page);
4745
}
@@ -103,7 +101,7 @@ int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
103101

104102
BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
105103

106-
ctx = fscrypt_get_ctx(inode, GFP_NOFS);
104+
ctx = fscrypt_get_ctx(GFP_NOFS);
107105
if (IS_ERR(ctx))
108106
return PTR_ERR(ctx);
109107

fs/crypto/crypto.c

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,17 @@ EXPORT_SYMBOL(fscrypt_release_ctx);
8787

8888
/**
8989
* fscrypt_get_ctx() - Gets an encryption context
90-
* @inode: The inode for which we are doing the crypto
9190
* @gfp_flags: The gfp flag for memory allocation
9291
*
9392
* Allocates and initializes an encryption context.
9493
*
95-
* Return: An allocated and initialized encryption context on success; error
96-
* value or NULL otherwise.
94+
* Return: A new encryption context on success; an ERR_PTR() otherwise.
9795
*/
98-
struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode, gfp_t gfp_flags)
96+
struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
9997
{
100-
struct fscrypt_ctx *ctx = NULL;
101-
struct fscrypt_info *ci = inode->i_crypt_info;
98+
struct fscrypt_ctx *ctx;
10299
unsigned long flags;
103100

104-
if (ci == NULL)
105-
return ERR_PTR(-ENOKEY);
106-
107101
/*
108102
* We first try getting the ctx from a free list because in
109103
* the common case the ctx will have an allocated and
@@ -258,9 +252,9 @@ struct page *fscrypt_encrypt_page(const struct inode *inode,
258252

259253
BUG_ON(!PageLocked(page));
260254

261-
ctx = fscrypt_get_ctx(inode, gfp_flags);
255+
ctx = fscrypt_get_ctx(gfp_flags);
262256
if (IS_ERR(ctx))
263-
return (struct page *)ctx;
257+
return ERR_CAST(ctx);
264258

265259
/* The encryption operation will require a bounce page. */
266260
ciphertext_page = fscrypt_alloc_bounce_page(ctx, gfp_flags);
@@ -313,45 +307,47 @@ int fscrypt_decrypt_page(const struct inode *inode, struct page *page,
313307
EXPORT_SYMBOL(fscrypt_decrypt_page);
314308

315309
/*
316-
* Validate dentries for encrypted directories to make sure we aren't
317-
* potentially caching stale data after a key has been added or
318-
* removed.
310+
* Validate dentries in encrypted directories to make sure we aren't potentially
311+
* caching stale dentries after a key has been added.
319312
*/
320313
static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
321314
{
322315
struct dentry *dir;
323-
int dir_has_key, cached_with_key;
316+
int err;
317+
int valid;
318+
319+
/*
320+
* Plaintext names are always valid, since fscrypt doesn't support
321+
* reverting to ciphertext names without evicting the directory's inode
322+
* -- which implies eviction of the dentries in the directory.
323+
*/
324+
if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
325+
return 1;
326+
327+
/*
328+
* Ciphertext name; valid if the directory's key is still unavailable.
329+
*
330+
* Although fscrypt forbids rename() on ciphertext names, we still must
331+
* use dget_parent() here rather than use ->d_parent directly. That's
332+
* because a corrupted fs image may contain directory hard links, which
333+
* the VFS handles by moving the directory's dentry tree in the dcache
334+
* each time ->lookup() finds the directory and it already has a dentry
335+
* elsewhere. Thus ->d_parent can be changing, and we must safely grab
336+
* a reference to some ->d_parent to prevent it from being freed.
337+
*/
324338

325339
if (flags & LOOKUP_RCU)
326340
return -ECHILD;
327341

328342
dir = dget_parent(dentry);
329-
if (!IS_ENCRYPTED(d_inode(dir))) {
330-
dput(dir);
331-
return 0;
332-
}
333-
334-
spin_lock(&dentry->d_lock);
335-
cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY;
336-
spin_unlock(&dentry->d_lock);
337-
dir_has_key = (d_inode(dir)->i_crypt_info != NULL);
343+
err = fscrypt_get_encryption_info(d_inode(dir));
344+
valid = !fscrypt_has_encryption_key(d_inode(dir));
338345
dput(dir);
339346

340-
/*
341-
* If the dentry was cached without the key, and it is a
342-
* negative dentry, it might be a valid name. We can't check
343-
* if the key has since been made available due to locking
344-
* reasons, so we fail the validation so ext4_lookup() can do
345-
* this check.
346-
*
347-
* We also fail the validation if the dentry was created with
348-
* the key present, but we no longer have the key, or vice versa.
349-
*/
350-
if ((!cached_with_key && d_is_negative(dentry)) ||
351-
(!cached_with_key && dir_has_key) ||
352-
(cached_with_key && !dir_has_key))
353-
return 0;
354-
return 1;
347+
if (err < 0)
348+
return err;
349+
350+
return valid;
355351
}
356352

357353
const struct dentry_operations fscrypt_d_ops = {

fs/crypto/fname.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
269269
if (iname->len < FS_CRYPTO_BLOCK_SIZE)
270270
return -EUCLEAN;
271271

272-
if (inode->i_crypt_info)
272+
if (fscrypt_has_encryption_key(inode))
273273
return fname_decrypt(inode, iname, oname);
274274

275275
if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
@@ -336,7 +336,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
336336
if (ret)
337337
return ret;
338338

339-
if (dir->i_crypt_info) {
339+
if (fscrypt_has_encryption_key(dir)) {
340340
if (!fscrypt_fname_encrypted_size(dir, iname->len,
341341
dir->i_sb->s_cop->max_namelen,
342342
&fname->crypto_buf.len))
@@ -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: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ int fscrypt_file_open(struct inode *inode, struct file *filp)
4949
}
5050
EXPORT_SYMBOL_GPL(fscrypt_file_open);
5151

52-
int __fscrypt_prepare_link(struct inode *inode, struct inode *dir)
52+
int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
53+
struct dentry *dentry)
5354
{
5455
int err;
5556

5657
err = fscrypt_require_key(dir);
5758
if (err)
5859
return err;
5960

61+
/* ... in case we looked up ciphertext name before key was added */
62+
if (dentry->d_flags & DCACHE_ENCRYPTED_NAME)
63+
return -ENOKEY;
64+
6065
if (!fscrypt_has_permitted_context(dir, inode))
6166
return -EXDEV;
6267

@@ -78,6 +83,11 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
7883
if (err)
7984
return err;
8085

86+
/* ... in case we looked up ciphertext name(s) before key was added */
87+
if ((old_dentry->d_flags | new_dentry->d_flags) &
88+
DCACHE_ENCRYPTED_NAME)
89+
return -ENOKEY;
90+
8191
if (old_dir != new_dir) {
8292
if (IS_ENCRYPTED(new_dir) &&
8393
!fscrypt_has_permitted_context(new_dir,
@@ -94,21 +104,21 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
94104
}
95105
EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename);
96106

97-
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)
98109
{
99-
int err = fscrypt_get_encryption_info(dir);
110+
int err = fscrypt_setup_filename(dir, &dentry->d_name, 1, fname);
100111

101-
if (err)
112+
if (err && err != -ENOENT)
102113
return err;
103114

104-
if (fscrypt_has_encryption_key(dir)) {
115+
if (fname->is_ciphertext_name) {
105116
spin_lock(&dentry->d_lock);
106-
dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
117+
dentry->d_flags |= DCACHE_ENCRYPTED_NAME;
107118
spin_unlock(&dentry->d_lock);
119+
d_set_d_op(dentry, &fscrypt_d_ops);
108120
}
109-
110-
d_set_d_op(dentry, &fscrypt_d_ops);
111-
return 0;
121+
return err;
112122
}
113123
EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
114124

@@ -179,21 +189,30 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
179189
sd->len = cpu_to_le16(ciphertext_len);
180190

181191
err = fname_encrypt(inode, &iname, sd->encrypted_path, ciphertext_len);
182-
if (err) {
183-
if (!disk_link->name)
184-
kfree(sd);
185-
return err;
186-
}
192+
if (err)
193+
goto err_free_sd;
194+
187195
/*
188196
* Null-terminating the ciphertext doesn't make sense, but we still
189197
* count the null terminator in the length, so we might as well
190198
* initialize it just in case the filesystem writes it out.
191199
*/
192200
sd->encrypted_path[ciphertext_len] = '\0';
193201

202+
/* Cache the plaintext symlink target for later use by get_link() */
203+
err = -ENOMEM;
204+
inode->i_link = kmemdup(target, len + 1, GFP_NOFS);
205+
if (!inode->i_link)
206+
goto err_free_sd;
207+
194208
if (!disk_link->name)
195209
disk_link->name = (unsigned char *)sd;
196210
return 0;
211+
212+
err_free_sd:
213+
if (!disk_link->name)
214+
kfree(sd);
215+
return err;
197216
}
198217
EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
199218

@@ -202,7 +221,7 @@ EXPORT_SYMBOL_GPL(__fscrypt_encrypt_symlink);
202221
* @inode: the symlink inode
203222
* @caddr: the on-disk contents of the symlink
204223
* @max_size: size of @caddr buffer
205-
* @done: if successful, will be set up to free the returned target
224+
* @done: if successful, will be set up to free the returned target if needed
206225
*
207226
* If the symlink's encryption key is available, we decrypt its target.
208227
* Otherwise, we encode its target for presentation.
@@ -217,19 +236,26 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
217236
{
218237
const struct fscrypt_symlink_data *sd;
219238
struct fscrypt_str cstr, pstr;
239+
bool has_key;
220240
int err;
221241

222242
/* This is for encrypted symlinks only */
223243
if (WARN_ON(!IS_ENCRYPTED(inode)))
224244
return ERR_PTR(-EINVAL);
225245

246+
/* If the decrypted target is already cached, just return it. */
247+
pstr.name = READ_ONCE(inode->i_link);
248+
if (pstr.name)
249+
return pstr.name;
250+
226251
/*
227252
* Try to set up the symlink's encryption key, but we can continue
228253
* regardless of whether the key is available or not.
229254
*/
230255
err = fscrypt_get_encryption_info(inode);
231256
if (err)
232257
return ERR_PTR(err);
258+
has_key = fscrypt_has_encryption_key(inode);
233259

234260
/*
235261
* For historical reasons, encrypted symlink targets are prefixed with
@@ -261,7 +287,17 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
261287
goto err_kfree;
262288

263289
pstr.name[pstr.len] = '\0';
264-
set_delayed_call(done, kfree_link, pstr.name);
290+
291+
/*
292+
* Cache decrypted symlink targets in i_link for later use. Don't cache
293+
* symlink targets encoded without the key, since those become outdated
294+
* once the key is added. This pairs with the READ_ONCE() above and in
295+
* the VFS path lookup code.
296+
*/
297+
if (!has_key ||
298+
cmpxchg_release(&inode->i_link, NULL, pstr.name) != NULL)
299+
set_delayed_call(done, kfree_link, pstr.name);
300+
265301
return pstr.name;
266302

267303
err_kfree:

fs/crypto/keyinfo.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ int fscrypt_get_encryption_info(struct inode *inode)
508508
u8 *raw_key = NULL;
509509
int res;
510510

511-
if (inode->i_crypt_info)
511+
if (fscrypt_has_encryption_key(inode))
512512
return 0;
513513

514514
res = fscrypt_initialize(inode->i_sb->s_cop->flags);
@@ -572,7 +572,7 @@ int fscrypt_get_encryption_info(struct inode *inode)
572572
if (res)
573573
goto out;
574574

575-
if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
575+
if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL)
576576
crypt_info = NULL;
577577
out:
578578
if (res == -ENOKEY)
@@ -583,9 +583,30 @@ int fscrypt_get_encryption_info(struct inode *inode)
583583
}
584584
EXPORT_SYMBOL(fscrypt_get_encryption_info);
585585

586+
/**
587+
* fscrypt_put_encryption_info - free most of an inode's fscrypt data
588+
*
589+
* Free the inode's fscrypt_info. Filesystems must call this when the inode is
590+
* being evicted. An RCU grace period need not have elapsed yet.
591+
*/
586592
void fscrypt_put_encryption_info(struct inode *inode)
587593
{
588594
put_crypt_info(inode->i_crypt_info);
589595
inode->i_crypt_info = NULL;
590596
}
591597
EXPORT_SYMBOL(fscrypt_put_encryption_info);
598+
599+
/**
600+
* fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
601+
*
602+
* Free the inode's cached decrypted symlink target, if any. Filesystems must
603+
* call this after an RCU grace period, just before they free the inode.
604+
*/
605+
void fscrypt_free_inode(struct inode *inode)
606+
{
607+
if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
608+
kfree(inode->i_link);
609+
inode->i_link = NULL;
610+
}
611+
}
612+
EXPORT_SYMBOL(fscrypt_free_inode);

fs/crypto/policy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
194194
res = fscrypt_get_encryption_info(child);
195195
if (res)
196196
return 0;
197-
parent_ci = parent->i_crypt_info;
198-
child_ci = child->i_crypt_info;
197+
parent_ci = READ_ONCE(parent->i_crypt_info);
198+
child_ci = READ_ONCE(child->i_crypt_info);
199199

200200
if (parent_ci && child_ci) {
201201
return memcmp(parent_ci->ci_master_key_descriptor,
@@ -246,7 +246,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
246246
if (res < 0)
247247
return res;
248248

249-
ci = parent->i_crypt_info;
249+
ci = READ_ONCE(parent->i_crypt_info);
250250
if (ci == NULL)
251251
return -ENOKEY;
252252

0 commit comments

Comments
 (0)