]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.4.103/fscrypt-lock-mutex-before-checking-for-bounce-page-pool.patch
4.14-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 4.4.103 / fscrypt-lock-mutex-before-checking-for-bounce-page-pool.patch
1 From a0b3bc855374c50b5ea85273553485af48caf2f7 Mon Sep 17 00:00:00 2001
2 From: Eric Biggers <ebiggers@google.com>
3 Date: Sun, 29 Oct 2017 06:30:19 -0400
4 Subject: fscrypt: lock mutex before checking for bounce page pool
5
6 From: Eric Biggers <ebiggers@google.com>
7
8 commit a0b3bc855374c50b5ea85273553485af48caf2f7 upstream.
9
10 fscrypt_initialize(), which allocates the global bounce page pool when
11 an encrypted file is first accessed, uses "double-checked locking" to
12 try to avoid locking fscrypt_init_mutex. However, it doesn't use any
13 memory barriers, so it's theoretically possible for a thread to observe
14 a bounce page pool which has not been fully initialized. This is a
15 classic bug with "double-checked locking".
16
17 While "only a theoretical issue" in the latest kernel, in pre-4.8
18 kernels the pointer that was checked was not even the last to be
19 initialized, so it was easily possible for a crash (NULL pointer
20 dereference) to happen. This was changed only incidentally by the large
21 refactor to use fs/crypto/.
22
23 Solve both problems in a trivial way that can easily be backported: just
24 always take the mutex. It's theoretically less efficient, but it
25 shouldn't be noticeable in practice as the mutex is only acquired very
26 briefly once per encrypted file.
27
28 Later I'd like to make this use a helper macro like DO_ONCE(). However,
29 DO_ONCE() runs in atomic context, so we'd need to add a new macro that
30 allows blocking.
31
32 Signed-off-by: Eric Biggers <ebiggers@google.com>
33 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
34 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
35
36
37 ---
38 fs/ext4/crypto_key.c | 8 +++-----
39 1 file changed, 3 insertions(+), 5 deletions(-)
40
41 --- a/fs/ext4/crypto_key.c
42 +++ b/fs/ext4/crypto_key.c
43 @@ -129,11 +129,9 @@ int ext4_get_encryption_info(struct inod
44 if (ei->i_crypt_info)
45 return 0;
46
47 - if (!ext4_read_workqueue) {
48 - res = ext4_init_crypto();
49 - if (res)
50 - return res;
51 - }
52 + res = ext4_init_crypto();
53 + if (res)
54 + return res;
55
56 res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
57 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,