tls_rx_rec_wait() tests signal_pending() only after it sleeps, and
it skips the wait loop when a record is already parsed.
tls_sw_recvmsg() calls tls_rx_rec_wait() once per record, and a
zero-length application_data record does not advance either bound
of the receive loop. A peer that streams such records holds the
caller in recvmsg(), unresponsive to SIGKILL, for as long as they
arrive. The hang has not been reproduced.

Test for a pending signal in tls_sw_recvmsg() before it fetches the
record after an empty one. A reader that has copied data gets the
data. A nonblocking reader with a signal pending gets -EAGAIN
rather than -EINTR. Pending task work alone makes signal_pending()
true, and io_uring retries a nonblocking receive only on -EAGAIN.
The test stays out of tls_rx_rec_wait() because tls_sw_read_sock()
also calls tls_rx_rec_wait(), from callers that cannot act on a
signal.

Decrypt an empty record synchronously. The code at recv_end
overwrites err once any record has been decrypted asynchronously.
With only empty records queued, the reader then gets zero, not the
errno.

Fixes: c46234ebb4d1 ("tls: RX path for ktls")
Reported-by: sashiko-bot <[email protected]>
Closes: 
https://sashiko.dev/#/patchset/[email protected]?part=1
Signed-off-by: Chuck Lever <[email protected]>
---
 net/tls/tls_sw.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 37fcca95c046..ac4da28ed9c8 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -1824,6 +1824,7 @@ int tls_sw_recvmsg(struct sock *sk,
        bool is_peek = flags & MSG_PEEK;
        bool rx_more = false;
        bool released = true;
+       bool nodata = false;
        bool zc_capable;
 
        if (unlikely(flags & MSG_ERRQUEUE))
@@ -1858,6 +1859,18 @@ int tls_sw_recvmsg(struct sock *sk,
                struct tls_decrypt_arg darg;
                int to_decrypt, chunk;
 
+               /* A run of empty records advances neither loop bound, and
+                * tls_rx_rec_wait() tests for a signal only after it sleeps.
+                */
+               if (nodata && signal_pending(current)) {
+                       long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
+
+                       err = -EAGAIN;
+                       if (timeo)
+                               err = sock_intr_errno(timeo);
+                       goto recv_end;
+               }
+
                err = tls_rx_rec_wait(sk, flags & MSG_DONTWAIT,
                                      released, !!(decrypted + copied));
                if (err <= 0)
@@ -1874,9 +1887,12 @@ int tls_sw_recvmsg(struct sock *sk,
                    tlm->control == TLS_RECORD_TYPE_DATA)
                        darg.zc = true;
 
-               /* Do not use async mode if record is non-data */
+               /* Do not use async mode if record is non-data, or if it
+                * is empty: once a record has gone async, recv_end
+                * discards the error that ends a run of empty records.
+                */
                if (tlm->control == TLS_RECORD_TYPE_DATA)
-                       darg.async = ctx->async_capable;
+                       darg.async = ctx->async_capable && to_decrypt;
                else
                        darg.async = false;
 
@@ -1910,6 +1926,7 @@ int tls_sw_recvmsg(struct sock *sk,
                /* TLS 1.3 may have updated the length by more than overhead */
                rxm = strp_msg(darg.skb);
                chunk = rxm->full_len;
+               nodata = !chunk;
                tls_rx_rec_done(ctx);
 
                if (!darg.zc) {

-- 
2.55.0


Reply via email to