Skip to content

Commit 13114dc

Browse files
qsnkuba-moo
authored andcommitted
tls: fix use-after-free on failed backlog decryption
When the decrypt request goes to the backlog and crypto_aead_decrypt returns -EBUSY, tls_do_decryption will wait until all async decryptions have completed. If one of them fails, tls_do_decryption will return -EBADMSG and tls_decrypt_sg jumps to the error path, releasing all the pages. But the pages have been passed to the async callback, and have already been released by tls_decrypt_done. The only true async case is when crypto_aead_decrypt returns -EINPROGRESS. With -EBUSY, we already waited so we can tell tls_sw_recvmsg that the data is available for immediate copy, but we need to notify tls_decrypt_sg (via the new ->async_done flag) that the memory has already been released. Fixes: 8590541 ("net: tls: handle backlogging of crypto requests") Signed-off-by: Sabrina Dubroca <[email protected]> Link: https://lore.kernel.org/r/4755dd8d9bebdefaa19ce1439b833d6199d4364c.1709132643.git.sd@queasysnail.net Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 41532b7 commit 13114dc

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

net/tls/tls_sw.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct tls_decrypt_arg {
5252
struct_group(inargs,
5353
bool zc;
5454
bool async;
55+
bool async_done;
5556
u8 tail;
5657
);
5758

@@ -286,15 +287,18 @@ static int tls_do_decryption(struct sock *sk,
286287
}
287288

288289
ret = crypto_aead_decrypt(aead_req);
290+
if (ret == -EINPROGRESS)
291+
return 0;
292+
289293
if (ret == -EBUSY) {
290294
ret = tls_decrypt_async_wait(ctx);
291-
ret = ret ?: -EINPROGRESS;
292-
}
293-
if (ret == -EINPROGRESS) {
294-
return 0;
295-
} else if (darg->async) {
296-
atomic_dec(&ctx->decrypt_pending);
295+
darg->async_done = true;
296+
/* all completions have run, we're not doing async anymore */
297+
darg->async = false;
298+
return ret;
297299
}
300+
301+
atomic_dec(&ctx->decrypt_pending);
298302
darg->async = false;
299303

300304
return ret;
@@ -1593,8 +1597,11 @@ static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
15931597
/* Prepare and submit AEAD request */
15941598
err = tls_do_decryption(sk, sgin, sgout, dctx->iv,
15951599
data_len + prot->tail_size, aead_req, darg);
1596-
if (err)
1600+
if (err) {
1601+
if (darg->async_done)
1602+
goto exit_free_skb;
15971603
goto exit_free_pages;
1604+
}
15981605

15991606
darg->skb = clear_skb ?: tls_strp_msg(ctx);
16001607
clear_skb = NULL;
@@ -1606,6 +1613,9 @@ static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
16061613
return err;
16071614
}
16081615

1616+
if (unlikely(darg->async_done))
1617+
return 0;
1618+
16091619
if (prot->tail_size)
16101620
darg->tail = dctx->tail;
16111621

0 commit comments

Comments
 (0)