Sync up to get to minimally functional lazy journalling patches
[ext4-patch-queue.git] / fix-use-after-iput-when-fscrypt-contexts-are-inconsistent
blob24ad60b60b9cd0dfd2bfc8036831c6234faf0448
1 ext4: fix use-after-iput when fscrypt contexts are inconsistent
3 From: Eric Biggers <ebiggers@google.com>
5 In the case where the child's encryption context was inconsistent with
6 its parent directory, we were using inode->i_sb and inode->i_ino after
7 the inode had already been iput().  Fix this by doing the iput() in the
8 correct places.
10 Note: only ext4 had this bug, not f2fs and ubifs.
12 Fixes: d9cdc9033181 ("ext4 crypto: enforce context consistency")
13 Cc: stable@vger.kernel.org
14 Signed-off-by: Eric Biggers <ebiggers@google.com>
15 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
16 ---
17  fs/ext4/namei.c | 6 ++++--
18  1 file changed, 4 insertions(+), 2 deletions(-)
20 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
21 index bb880c326191..931da9d5d915 100644
22 --- a/fs/ext4/namei.c
23 +++ b/fs/ext4/namei.c
24 @@ -1618,13 +1618,15 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
25                     !fscrypt_has_permitted_context(dir, inode)) {
26                         int nokey = ext4_encrypted_inode(inode) &&
27                                 !fscrypt_has_encryption_key(inode);
28 -                       iput(inode);
29 -                       if (nokey)
30 +                       if (nokey) {
31 +                               iput(inode);
32                                 return ERR_PTR(-ENOKEY);
33 +                       }
34                         ext4_warning(inode->i_sb,
35                                      "Inconsistent encryption contexts: %lu/%lu",
36                                      (unsigned long) dir->i_ino,
37                                      (unsigned long) inode->i_ino);
38 +                       iput(inode);
39                         return ERR_PTR(-EPERM);
40                 }
41         }
42 -- 
43 2.11.0.483.g087da7b7c-goog