add patch use-dget_parent-in-ext4_d_revalidate
[ext4-patch-queue.git] / use-dget_parent-in-ext4_d_revalidate
blobbda56d2a3c866fe3b8e89e3bb0c301dc990551bf
1 ext4 crypto: use dget_parent() in ext4_d_revalidate()
3 This avoids potential problems caused by a race where the inode gets
4 renamed out from its parent directory and the parent directory is
5 deleted while ext4_d_revalidate() is running.
7 Fixes: 28b4c263961c
8 Reported-by: Al Viro <viro@ZenIV.linux.org.uk>
9 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
10 Cc: stable@vger.kernel.org
12 ---
13  fs/ext4/crypto.c | 12 ++++++++----
14  1 file changed, 8 insertions(+), 4 deletions(-)
16 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
17 index 38f7562..ecb5439 100644
18 --- a/fs/ext4/crypto.c
19 +++ b/fs/ext4/crypto.c
20 @@ -475,13 +475,16 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
21   */
22  static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
23  {
24 -       struct inode *dir = d_inode(dentry->d_parent);
25 -       struct ext4_crypt_info *ci = EXT4_I(dir)->i_crypt_info;
26 +       struct dentry *dir;
27 +       struct ext4_crypt_info *ci;
28         int dir_has_key, cached_with_key;
30 -       if (!ext4_encrypted_inode(dir))
31 +       dir = dget_parent(dentry);
32 +       if (!ext4_encrypted_inode(d_inode(dir))) {
33 +               dput(dir);
34                 return 0;
36 +       }
37 +       ci = EXT4_I(d_inode(dir))->i_crypt_info;
38         if (ci && ci->ci_keyring_key &&
39             (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
40                                           (1 << KEY_FLAG_REVOKED) |
41 @@ -491,6 +494,7 @@ static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
42         /* this should eventually be an flag in d_flags */
43         cached_with_key = dentry->d_fsdata != NULL;
44         dir_has_key = (ci != NULL);
45 +       dput(dir);
47         /*
48          * If the dentry was cached without the key, and it is a