[PATCH net v2] tls: fix send() returning success after the TCP send failed

From: Hengyu Liang

Date: Thu Nov 19 2026 - 09:11:36 EST


Commit b341ca51d267 ("tls: Fix tls_sw_sendmsg error handling") made
tls_tx_records() pass the error of the TCP send to tls_err_abort()
instead of -EBADMSG.

However, the transmit path relied on EBADMSG in two places.
bpf_exec_tx_verdict() looks for it in sk_err to tell that the record
was not sent, and sk_stream_error() leaves it in sk_err, so later sends
failed early. Neither holds for EPIPE. As of now, send() on a kTLS
socket returns success after the TCP connection can no longer send, for
example after shutdown(SHUT_WR) or after a reset. Nothing reaches the
peer, the caller is not told, and every send queues one more record
until the socket is closed.

The issue can be reproduced with:

python3 - <<'EOF'
import socket, struct
l = socket.create_server(("127.0.0.1", 0))
c = socket.create_connection(l.getsockname())
c.setsockopt(socket.SOL_TCP, 31, b"tls")
c.setsockopt(282, 1, struct.pack("HH", 0x0303, 51) + bytes(36))
c.shutdown(socket.SHUT_WR)
print(c.send(b"hello"))
EOF

Before commit b341ca51d267 ("tls: Fix tls_sw_sendmsg error handling"),
the result is:

OSError: [Errno 74] Bad message

After that commit, the result is:

5

Record the error in the TX context when tls_tx_records() aborts the
connection. Do not count the record as sent, and fail later sends with
that error before they take any data. The send() above then fails with
EPIPE, as it does on a plain TCP socket.

Found while comparing the behaviour of different kernel versions. The
program above reproduces it on an unmodified kernel. Tested with that
program and with the tls selftests.

Fixes: b341ca51d267 ("tls: Fix tls_sw_sendmsg error handling")
Signed-off-by: Hengyu Liang <hengyul@xxxxxxxxxx>
---
I found this while comparing the behaviour of different kernel versions.
I used an LLM to analyse the problem and to draft the patch and this
message, and I analyzed the result myself.

Changes in v2:
- Keep the abort in the TX context, so that later sends fail and queue
nothing. v1 only fixed the send that hit the error.
- Tell that the record was not sent from that state, not from sk_err.
- Cc the address of the fixed commit's author and the bpf list.
v1: https://lore.kernel.org/netdev/20261113193649.3822306-1-hengyul@xxxxxxxxxx/

With 20000 sends of 16 KiB after shutdown(SHUT_WR), net reports 19856
of them as sent and TCP memory grows from 15 to 158882 pages. With this
patch, all of them fail with EPIPE and TCP memory stays at 0 pages.

Rishikesh's "tls: make the record number wrap abort final" adds a
similar state for another abort. I can rebase on it if it goes in first.

include/net/tls.h | 1 +
net/tls/tls_sw.c | 22 +++++++++++++++++++---
2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/include/net/tls.h b/include/net/tls.h
index 0b001b8ab1c3..7d43ffe46f33 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -100,6 +100,7 @@ struct tls_sw_context_tx {
struct tls_rec *open_rec;
struct list_head tx_list;
atomic_t encrypt_pending;
+ int abort_err; /* error of the send that aborted TX, never cleared */
u8 async_capable:1;

#define BIT_TX_SCHEDULED 0
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 226e0f176fdc..795cbfe5fb2a 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -413,6 +413,9 @@ int tls_tx_records(struct sock *sk, int flags)
struct sk_msg *msg_en;
int tx_flags, rc = 0;

+ if (unlikely(ctx->abort_err))
+ return ctx->abort_err;
+
if (tls_is_partially_sent_record(tls_ctx)) {
rec = list_first_entry(&ctx->tx_list,
struct tls_rec, list);
@@ -459,8 +462,11 @@ int tls_tx_records(struct sock *sk, int flags)
}

tx_err:
- if (rc < 0 && rc != -EAGAIN && rc != -EINTR && rc != -ERESTARTSYS)
+ if (rc < 0 && rc != -EAGAIN && rc != -EINTR && rc != -ERESTARTSYS) {
+ /* sk_err is cleared when it is reported, keep the abort */
+ ctx->abort_err = rc;
tls_err_abort(sk, rc);
+ }

return rc;
}
@@ -636,6 +642,9 @@ static int tls_push_record(struct sock *sk, int flags,
if (!rec)
return 0;

+ if (unlikely(ctx->abort_err))
+ return ctx->abort_err;
+
msg_pl = &rec->msg_plaintext;
msg_en = &rec->msg_encrypted;

@@ -708,13 +717,15 @@ static int tls_push_record(struct sock *sk, int flags,
static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
u8 record_type, ssize_t *copied, int flags)
{
+ struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_get_ctx(sk));
int err;

err = tls_push_record(sk, flags, record_type);
- if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) {
+ if (err && err != -EINPROGRESS &&
+ (ctx->abort_err || sk->sk_err == EBADMSG)) {
*copied -= sk_msg_free(sk, msg);
tls_free_open_rec(sk);
- err = -sk->sk_err;
+ err = ctx->abort_err ?: -EBADMSG;
}
return err;
}
@@ -807,6 +818,11 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
}

while (msg_data_left(msg)) {
+ if (unlikely(ctx->abort_err)) {
+ ret = ctx->abort_err;
+ goto send_end;
+ }
+
if (sk->sk_err) {
ret = -sk->sk_err;
goto send_end;

base-commit: a8aeac0b1883206cf72461258e6bfd991589049e
--
2.53.0