Skip to content

Commit f1fe999

Browse files
kuba-moogregkh
authored andcommitted
tls: handle data disappearing from under the TLS ULP
[ Upstream commit 6db015f ] TLS expects that it owns the receive queue of the TCP socket. This cannot be guaranteed in case the reader of the TCP socket entered before the TLS ULP was installed, or uses some non-standard read API (eg. zerocopy ones). Replace the WARN_ON() and a buggy early exit (which leaves anchor pointing to a freed skb) with real error handling. Wipe the parsing state and tell the reader to retry. We already reload the anchor every time we (re)acquire the socket lock, so the only condition we need to avoid is an out of bounds read (not having enough bytes in the socket for previously parsed record len). If some data was read from under TLS but there's enough in the queue we'll reload and decrypt what is most likely not a valid TLS record. Leading to some undefined behavior from TLS perspective (corrupting a stream? missing an alert? missing an attack?) but no kernel crash should take place. Reported-by: William Liu <will@willsroot.io> Reported-by: Savino Dicanosa <savy@syst3mfailure.io> Link: https://lore.kernel.org/tFjq_kf7sWIG3A7CrCg_egb8CVsT_gsmHAK0_wxDPJXfIzxFAMxqmLwp3MlU5EHiet0AwwJldaaFdgyHpeIUCS-3m3llsmRzp9xIOBR4lAI=@syst3mfailure.io Fixes: 84c61fe ("tls: rx: do not use the standard strparser") Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20250807232907.600366-1-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent acfb4da commit f1fe999

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

net/tls/tls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void tls_strp_msg_done(struct tls_strparser *strp);
192192
int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb);
193193
void tls_rx_msg_ready(struct tls_strparser *strp);
194194

195-
void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
195+
bool tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
196196
int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
197197
struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
198198
int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst);

net/tls/tls_strp.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static void tls_strp_load_anchor_with_queue(struct tls_strparser *strp, int len)
474474
strp->stm.offset = offset;
475475
}
476476

477-
void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
477+
bool tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
478478
{
479479
struct strp_msg *rxm;
480480
struct tls_msg *tlm;
@@ -483,8 +483,11 @@ void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
483483
DEBUG_NET_WARN_ON_ONCE(!strp->stm.full_len);
484484

485485
if (!strp->copy_mode && force_refresh) {
486-
if (WARN_ON(tcp_inq(strp->sk) < strp->stm.full_len))
487-
return;
486+
if (unlikely(tcp_inq(strp->sk) < strp->stm.full_len)) {
487+
WRITE_ONCE(strp->msg_ready, 0);
488+
memset(&strp->stm, 0, sizeof(strp->stm));
489+
return false;
490+
}
488491

489492
tls_strp_load_anchor_with_queue(strp, strp->stm.full_len);
490493
}
@@ -494,6 +497,8 @@ void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
494497
rxm->offset = strp->stm.offset;
495498
tlm = tls_msg(strp->anchor);
496499
tlm->control = strp->mark;
500+
501+
return true;
497502
}
498503

499504
/* Called with lock held on lower socket */

net/tls/tls_sw.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,8 @@ tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
13801380
return sock_intr_errno(timeo);
13811381
}
13821382

1383-
tls_strp_msg_load(&ctx->strp, released);
1383+
if (unlikely(!tls_strp_msg_load(&ctx->strp, released)))
1384+
return tls_rx_rec_wait(sk, psock, nonblock, false);
13841385

13851386
return 1;
13861387
}

0 commit comments

Comments
 (0)