Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: pcrypt - Delay write to padata->info

These three events can race when pcrypt is used multiple times in a
template ("pcrypt(pcrypt(...))"):

1. [taskA] The caller makes the crypto request via crypto_aead_encrypt()
2. [kworkerB] padata serializes the inner pcrypt request
3. [kworkerC] padata serializes the outer pcrypt request

3 might finish before the call to crypto_aead_encrypt() returns in 1,
resulting in two possible issues.

First, a use-after-free of the crypto request's memory when, for
example, taskA writes to the outer pcrypt request's padata->info in
pcrypt_aead_enc() after kworkerC completes the request.

Second, the outer pcrypt request overwrites the inner pcrypt request's
return code with -EINPROGRESS, making a successful request appear to
fail. For instance, kworkerB writes the outer pcrypt request's
padata->info in pcrypt_aead_done() and then taskA overwrites it
in pcrypt_aead_enc().

Avoid both situations by delaying the write of padata->info until after
the inner crypto request's return code is checked. This prevents the
use-after-free by not touching the crypto request's memory after the
next-inner crypto request is made, and stops padata->info from being
overwritten.

Fixes: 5068c7a883d16 ("crypto: pcrypt - Add pcrypt crypto parallelization wrapper")
Reported-by: syzbot+b187b77c8474f9648fae@syzkaller.appspotmail.com
Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Daniel Jordan and committed by
Herbert Xu
68b6dea8 83bff109

+8 -4
+8 -4
crypto/pcrypt.c
··· 78 78 { 79 79 struct pcrypt_request *preq = pcrypt_padata_request(padata); 80 80 struct aead_request *req = pcrypt_request_ctx(preq); 81 + int ret; 81 82 82 - padata->info = crypto_aead_encrypt(req); 83 + ret = crypto_aead_encrypt(req); 83 84 84 - if (padata->info == -EINPROGRESS) 85 + if (ret == -EINPROGRESS) 85 86 return; 86 87 88 + padata->info = ret; 87 89 padata_do_serial(padata); 88 90 } 89 91 ··· 125 123 { 126 124 struct pcrypt_request *preq = pcrypt_padata_request(padata); 127 125 struct aead_request *req = pcrypt_request_ctx(preq); 126 + int ret; 128 127 129 - padata->info = crypto_aead_decrypt(req); 128 + ret = crypto_aead_decrypt(req); 130 129 131 - if (padata->info == -EINPROGRESS) 130 + if (ret == -EINPROGRESS) 132 131 return; 133 132 133 + padata->info = ret; 134 134 padata_do_serial(padata); 135 135 } 136 136