Skip to content

Commit

Permalink
fscrypt: remove unnecessary calls to fscrypt_require_key()
Browse files Browse the repository at this point in the history
In an encrypted directory, a regular dentry (one that doesn't have the
no-key name flag) can only be created if the directory's encryption key
is available.

Therefore the calls to fscrypt_require_key() in __fscrypt_prepare_link()
and __fscrypt_prepare_rename() are unnecessary, as these functions
already check that the dentries they're given aren't no-key names.

Remove these unnecessary calls to fscrypt_require_key().

Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Eric Biggers <[email protected]>
  • Loading branch information
ebiggers committed Nov 24, 2020
1 parent 76786a0 commit 234f1b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
26 changes: 8 additions & 18 deletions fs/crypto/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ EXPORT_SYMBOL_GPL(fscrypt_file_open);
int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
struct dentry *dentry)
{
int err;

err = fscrypt_require_key(dir);
if (err)
return err;

/* ... in case we looked up no-key name before key was added */
if (fscrypt_is_nokey_name(dentry))
return -ENOKEY;
/*
* We don't need to separately check that the directory inode's key is
* available, as it's implied by the dentry not being a no-key name.
*/

if (!fscrypt_has_permitted_context(dir, inode))
return -EXDEV;
Expand All @@ -75,20 +72,13 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry,
unsigned int flags)
{
int err;

err = fscrypt_require_key(old_dir);
if (err)
return err;

err = fscrypt_require_key(new_dir);
if (err)
return err;

/* ... in case we looked up no-key name(s) before key was added */
if (fscrypt_is_nokey_name(old_dentry) ||
fscrypt_is_nokey_name(new_dentry))
return -ENOKEY;
/*
* We don't need to separately check that the directory inodes' keys are
* available, as it's implied by the dentries not being no-key names.
*/

if (old_dir != new_dir) {
if (IS_ENCRYPTED(new_dir) &&
Expand Down
3 changes: 1 addition & 2 deletions include/linux/fscrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,7 @@ static inline int fscrypt_require_key(struct inode *inode)
*
* A new link can only be added to an encrypted directory if the directory's
* encryption key is available --- since otherwise we'd have no way to encrypt
* the filename. Therefore, we first set up the directory's encryption key (if
* not already done) and return an error if it's unavailable.
* the filename.
*
* We also verify that the link will not violate the constraint that all files
* in an encrypted directory tree use the same encryption policy.
Expand Down

0 comments on commit 234f1b7

Please sign in to comment.