Skip to content

Commit 0471b10

Browse files
qsndavem330
authored andcommitted
tls: block decryption when a rekey is pending
When a TLS handshake record carrying a KeyUpdate message is received, all subsequent records will be encrypted with a new key. We need to stop decrypting incoming records with the old key, and wait until userspace provides a new key. Make a note of this in the RX context just after decrypting that record, and stop recvmsg/splice calls with EKEYEXPIRED until the new key is available. key_update_pending can't be combined with the existing bitfield, because we will read it locklessly in ->poll. Signed-off-by: Sabrina Dubroca <sd@queasysnail.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 92c932b commit 0471b10

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

include/net/tls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ struct tls_rec;
5959

6060
#define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type)
6161

62+
#define TLS_HANDSHAKE_KEYUPDATE 24 /* rfc8446 B.3: Key update */
63+
6264
#define TLS_AAD_SPACE_SIZE 13
6365

6466
#define TLS_MAX_IV_SIZE 16
@@ -130,6 +132,7 @@ struct tls_sw_context_rx {
130132
u8 async_capable:1;
131133
u8 zc_capable:1;
132134
u8 reader_contended:1;
135+
bool key_update_pending;
133136

134137
struct tls_strparser strp;
135138

net/tls/tls_sw.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,10 @@ tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
13141314
int ret = 0;
13151315
long timeo;
13161316

1317+
/* a rekey is pending, let userspace deal with it */
1318+
if (unlikely(ctx->key_update_pending))
1319+
return -EKEYEXPIRED;
1320+
13171321
timeo = sock_rcvtimeo(sk, nonblock);
13181322

13191323
while (!tls_strp_msg_ready(ctx)) {
@@ -1720,6 +1724,34 @@ tls_decrypt_device(struct sock *sk, struct msghdr *msg,
17201724
return 1;
17211725
}
17221726

1727+
static int tls_check_pending_rekey(struct tls_context *ctx, struct sk_buff *skb)
1728+
{
1729+
const struct strp_msg *rxm = strp_msg(skb);
1730+
const struct tls_msg *tlm = tls_msg(skb);
1731+
char hs_type;
1732+
int err;
1733+
1734+
if (likely(tlm->control != TLS_RECORD_TYPE_HANDSHAKE))
1735+
return 0;
1736+
1737+
if (rxm->full_len < 1)
1738+
return 0;
1739+
1740+
err = skb_copy_bits(skb, rxm->offset, &hs_type, 1);
1741+
if (err < 0) {
1742+
DEBUG_NET_WARN_ON_ONCE(1);
1743+
return err;
1744+
}
1745+
1746+
if (hs_type == TLS_HANDSHAKE_KEYUPDATE) {
1747+
struct tls_sw_context_rx *rx_ctx = ctx->priv_ctx_rx;
1748+
1749+
WRITE_ONCE(rx_ctx->key_update_pending, true);
1750+
}
1751+
1752+
return 0;
1753+
}
1754+
17231755
static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
17241756
struct tls_decrypt_arg *darg)
17251757
{
@@ -1739,7 +1771,7 @@ static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
17391771
rxm->full_len -= prot->overhead_size;
17401772
tls_advance_record_sn(sk, prot, &tls_ctx->rx);
17411773

1742-
return 0;
1774+
return tls_check_pending_rekey(tls_ctx, darg->skb);
17431775
}
17441776

17451777
int decrypt_skb(struct sock *sk, struct scatterlist *sgout)
@@ -2719,6 +2751,7 @@ int tls_set_sw_offload(struct sock *sk, int tx)
27192751
crypto_info = &ctx->crypto_recv.info;
27202752
cctx = &ctx->rx;
27212753
aead = &sw_ctx_rx->aead_recv;
2754+
sw_ctx_rx->key_update_pending = false;
27222755
}
27232756

27242757
cipher_desc = get_cipher_desc(crypto_info->cipher_type);

0 commit comments

Comments
 (0)