ext4 crypto: revalidate dentry after adding or removing the key
Subsystems (2)
PATCH 1/3 ext4 crypto: revalidate dentry after adding or removing the key
From: Theodore Ts'o <[email protected]>
commit 28b4c263961c47da84ed8b5be0b5116bad1133eb upstream. Please apply to 4.4-stable. [This fixes a bug where encrypted files cannot be opened due to dcache pollution.]
Add a validation check for dentries for encrypted directory to make sure we're not caching stale data after a key has been added or removed.
Also check to make sure that status of the encryption key is updated when readdir(2) is executed.
--- fs/ext4/crypto.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ext4/dir.c | 6 ++++++ fs/ext4/ext4.h | 1 + fs/ext4/namei.c | 18 ++++++++++++++++++ 4 files changed, 75 insertions(+)
fs/ext4/crypto.c +50 -0
@@ -469,3 +469,53 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)return size;return 0 ; } + +/* +* Validate dentries for encrypted directories to make sure we aren't +* potentially caching stale data after a key has been added or +* removed. +*/ +static int ext4_d_revalidate (struct dentry *dentry,unsigned int flags) +{ +struct inode *dir =d_inode (dentry->d_parent); +struct ext4_crypt_info *ci =EXT4_I (dir)->i_crypt_info; +int dir_has_key, cached_with_key; + +if (!ext4_encrypted_inode (dir)) +return 0 ; + +/* this should eventually be an flag in d_flags */ + cached_with_key = dentry->d_fsdata !=NULL ; + dir_has_key = (ci !=NULL ); + +/* +* If the dentry was cached without the key, and it is a +* negative dentry, it might be a valid name. We can't check +* if the key has since been made available due to locking +* reasons, so we fail the validation so ext4_lookup() can do +* this check. +* +* We also fail the validation if the dentry was created with +* the key present, but we no longer have the key, or vice versa. +*/ +if ((!cached_with_key &&d_is_negative (dentry)) || + (!cached_with_key && dir_has_key) || + (cached_with_key && !dir_has_key)) { +#if 0 /* Revalidation debug */ +char buf[80 ]; +char *cp =simple_dname (dentry, buf,sizeof (buf)); + +if (IS_ERR (cp)) + cp = (char *)"???" ; +pr_err ("revalidate: %s %p %d %d %d\n" , cp, dentry->d_fsdata, + cached_with_key,d_is_negative (dentry), + dir_has_key); +#endif +return 0 ; + } +return 1 ; +} + +const struct dentry_operations ext4_encrypted_d_ops = { + .d_revalidate = ext4_d_revalidate, +};
fs/ext4/dir.c +6 -0
@@ -111,6 +111,12 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)int dir_has_error =0 ;struct ext4_str fname_crypto_str = {.name =NULL , .len =0 }; +if (ext4_encrypted_inode (inode)) { + err =ext4_get_encryption_info (inode); +if (err && err != -ENOKEY ) +return err; + } +if (is_dx_dir (inode)) { err =ext4_dx_readdir (file, ctx);if (err !=ERR_BAD_DX_DIR ) {
fs/ext4/ext4.h +1 -0
@@ -2268,6 +2268,7 @@ struct page *ext4_encrypt(struct inode *inode, struct page *plaintext_page);int ext4_decrypt (struct page *page);int ext4_encrypted_zeroout (struct inode *inode,struct ext4_extent *ex); +extern const struct dentry_operations ext4_encrypted_d_ops;#ifdef CONFIG_EXT4_FS_ENCRYPTION int ext4_init_crypto (void );
fs/ext4/namei.c +18 -0
@@ -1557,6 +1557,24 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsistruct ext4_dir_entry_2 *de;struct buffer_head *bh; +if (ext4_encrypted_inode (dir)) { +int res =ext4_get_encryption_info (dir); + +/* +* This should be a properly defined flag for +* dentry->d_flags when we uplift this to the VFS. +* d_fsdata is set to (void *) 1 if if the dentry is +* created while the directory was encrypted and we +* don't have access to the key. +*/ + dentry->d_fsdata =NULL ; +if (ext4_encryption_info (dir)) + dentry->d_fsdata = (void *)1 ; +d_set_d_op (dentry, &ext4_encrypted_d_ops); +if (res && res != -ENOKEY ) +return ERR_PTR (res); + } +if (dentry->d_name.len >EXT4_NAME_LEN )return ERR_PTR (-ENAMETOOLONG );
Signature · 1 line
2.13.0.rc1.294.g07d810a77f-goog
PATCH 2/3 ext4 crypto: use dget_parent() in ext4_d_revalidate()
From: Theodore Ts'o <[email protected]>
commit 3d43bcfef5f0548845a425365011c499875491b0 upstream. Please apply to 4.4-stable.
This avoids potential problems caused by a race where the inode gets renamed out from its parent directory and the parent directory is deleted while ext4_d_revalidate() is running.
--- fs/ext4/crypto.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/ext4/crypto.c +8 -3
@@ -477,16 +477,21 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)*/ static int ext4_d_revalidate (struct dentry *dentry,unsigned int flags) { -struct inode *dir =d_inode (dentry->d_parent); -struct ext4_crypt_info *ci =EXT4_I (dir)->i_crypt_info; +struct dentry *dir; +struct ext4_crypt_info *ci;int dir_has_key, cached_with_key; -if (!ext4_encrypted_inode (dir)) + dir =dget_parent (dentry); +if (!ext4_encrypted_inode (d_inode (dir))) { +dput (dir);return 0 ; + } + ci =EXT4_I (d_inode (dir))->i_crypt_info;/* this should eventually be an flag in d_flags */ cached_with_key = dentry->d_fsdata !=NULL ; dir_has_key = (ci !=NULL ); +dput (dir);/* * If the dentry was cached without the key, and it is a
Signature · 1 line
2.13.0.rc1.294.g07d810a77f-goog
PATCH 3/3 ext4/fscrypto: avoid RCU lookup in d_revalidate
In reply to Eric BiggersFrom: Jaegeuk Kim <[email protected]>
commit 03a8bb0e53d9562276045bdfcf2b5de2e4cff5a1 upstream. Please apply to 4.4-stable.
As Al pointed, d_revalidate should return RCU lookup before using d_inode. This was originally introduced by:
commit 34286d666230 ("fs: rcu-walk aware d_revalidate method").
--- fs/ext4/crypto.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/ext4/crypto.c +4 -0
@@ -34,6 +34,7 @@#include <linux/random.h> #include <linux/scatterlist.h> #include <linux/spinlock_types.h> +#include <linux/namei.h> #include "ext4_extents.h" #include "xattr.h"
@@ -481,6 +482,9 @@ static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)struct ext4_crypt_info *ci;int dir_has_key, cached_with_key; +if (flags &LOOKUP_RCU ) +return -ECHILD ; + dir =dget_parent (dentry);if (!ext4_encrypted_inode (d_inode (dir))) {dput (dir);
Signature · 1 line
2.13.0.rc1.294.g07d810a77f-goog
Greg Kroah-Hartman
maintainerRe: [PATCH 3/3] ext4/fscrypto: avoid RCU lookup in d_revalidate
Quoted 4 lines · Eric Biggers
From: Jaegeuk Kim <[email protected]>
commit
03a8bb0e53d9562276045bdfcf2b5de2e4cff5a1upstream. Please apply to 4.4-stable.
Thanks for all 3 of these, now applied.
greg k-h