Skip to content

Commit 2ebdef6

Browse files
committed
fscrypt: move fscrypt_d_revalidate() to fname.c
fscrypt_d_revalidate() and fscrypt_d_ops really belong in fname.c, since they're specific to filenames encryption. crypto.c is for contents encryption and general fs/crypto/ initialization and utilities. Link: https://lore.kernel.org/r/20191209204359.228544-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent 8a4ab0b commit 2ebdef6

3 files changed

Lines changed: 50 additions & 51 deletions

File tree

‎fs/crypto/crypto.c‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <linux/module.h>
2626
#include <linux/scatterlist.h>
2727
#include <linux/ratelimit.h>
28-
#include <linux/dcache.h>
29-
#include <linux/namei.h>
3028
#include <crypto/skcipher.h>
3129
#include "fscrypt_private.h"
3230

@@ -286,54 +284,6 @@ int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
286284
}
287285
EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
288286

289-
/*
290-
* Validate dentries in encrypted directories to make sure we aren't potentially
291-
* caching stale dentries after a key has been added.
292-
*/
293-
static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
294-
{
295-
struct dentry *dir;
296-
int err;
297-
int valid;
298-
299-
/*
300-
* Plaintext names are always valid, since fscrypt doesn't support
301-
* reverting to ciphertext names without evicting the directory's inode
302-
* -- which implies eviction of the dentries in the directory.
303-
*/
304-
if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
305-
return 1;
306-
307-
/*
308-
* Ciphertext name; valid if the directory's key is still unavailable.
309-
*
310-
* Although fscrypt forbids rename() on ciphertext names, we still must
311-
* use dget_parent() here rather than use ->d_parent directly. That's
312-
* because a corrupted fs image may contain directory hard links, which
313-
* the VFS handles by moving the directory's dentry tree in the dcache
314-
* each time ->lookup() finds the directory and it already has a dentry
315-
* elsewhere. Thus ->d_parent can be changing, and we must safely grab
316-
* a reference to some ->d_parent to prevent it from being freed.
317-
*/
318-
319-
if (flags & LOOKUP_RCU)
320-
return -ECHILD;
321-
322-
dir = dget_parent(dentry);
323-
err = fscrypt_get_encryption_info(d_inode(dir));
324-
valid = !fscrypt_has_encryption_key(d_inode(dir));
325-
dput(dir);
326-
327-
if (err < 0)
328-
return err;
329-
330-
return valid;
331-
}
332-
333-
const struct dentry_operations fscrypt_d_ops = {
334-
.d_revalidate = fscrypt_d_revalidate,
335-
};
336-
337287
/**
338288
* fscrypt_initialize() - allocate major buffers for fs encryption.
339289
* @cop_flags: fscrypt operations flags

‎fs/crypto/fname.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* This has not yet undergone a rigorous security audit.
1212
*/
1313

14+
#include <linux/namei.h>
1415
#include <linux/scatterlist.h>
1516
#include <crypto/skcipher.h>
1617
#include "fscrypt_private.h"
@@ -400,3 +401,51 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
400401
return ret;
401402
}
402403
EXPORT_SYMBOL(fscrypt_setup_filename);
404+
405+
/*
406+
* Validate dentries in encrypted directories to make sure we aren't potentially
407+
* caching stale dentries after a key has been added.
408+
*/
409+
static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
410+
{
411+
struct dentry *dir;
412+
int err;
413+
int valid;
414+
415+
/*
416+
* Plaintext names are always valid, since fscrypt doesn't support
417+
* reverting to ciphertext names without evicting the directory's inode
418+
* -- which implies eviction of the dentries in the directory.
419+
*/
420+
if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
421+
return 1;
422+
423+
/*
424+
* Ciphertext name; valid if the directory's key is still unavailable.
425+
*
426+
* Although fscrypt forbids rename() on ciphertext names, we still must
427+
* use dget_parent() here rather than use ->d_parent directly. That's
428+
* because a corrupted fs image may contain directory hard links, which
429+
* the VFS handles by moving the directory's dentry tree in the dcache
430+
* each time ->lookup() finds the directory and it already has a dentry
431+
* elsewhere. Thus ->d_parent can be changing, and we must safely grab
432+
* a reference to some ->d_parent to prevent it from being freed.
433+
*/
434+
435+
if (flags & LOOKUP_RCU)
436+
return -ECHILD;
437+
438+
dir = dget_parent(dentry);
439+
err = fscrypt_get_encryption_info(d_inode(dir));
440+
valid = !fscrypt_has_encryption_key(d_inode(dir));
441+
dput(dir);
442+
443+
if (err < 0)
444+
return err;
445+
446+
return valid;
447+
}
448+
449+
const struct dentry_operations fscrypt_d_ops = {
450+
.d_revalidate = fscrypt_d_revalidate,
451+
};

‎fs/crypto/fscrypt_private.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ extern int fscrypt_crypt_block(const struct inode *inode,
233233
unsigned int len, unsigned int offs,
234234
gfp_t gfp_flags);
235235
extern struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
236-
extern const struct dentry_operations fscrypt_d_ops;
237236

238237
extern void __printf(3, 4) __cold
239238
fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
@@ -265,6 +264,7 @@ extern int fname_encrypt(const struct inode *inode, const struct qstr *iname,
265264
extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
266265
u32 orig_len, u32 max_len,
267266
u32 *encrypted_len_ret);
267+
extern const struct dentry_operations fscrypt_d_ops;
268268

269269
/* hkdf.c */
270270

0 commit comments

Comments
 (0)