mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v2] tls: fix send() returning success after the TCP send failed
@ 2026-11-19 14:11 Hengyu Liang
  2026-11-23  2:13 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Hengyu Liang @ 2026-11-19 14:11 UTC (permalink / raw)
  To: John Fastabend, Jakub Kicinski, Sabrina Dubroca
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	Benjamin Coddington, Benjamin Coddington, Rishikesh Jethwani,
	netdev, bpf, linux-kernel

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@cs.unc.edu>
---
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@cs.unc.edu/

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH net v2] tls: fix send() returning success after the TCP send failed
  2026-11-19 14:11 [PATCH net v2] tls: fix send() returning success after the TCP send failed Hengyu Liang
@ 2026-11-23  2:13 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23  2:13 UTC (permalink / raw)
  To: hengyul
  Cc: john.fastabend, kuba, sd, davem, edumazet, pabeni, horms,
	bcodding, ben.coddington, rjethwani, netdev, bpf, linux-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] With async encryption, send() still returns success when the TCP
  send fails during that same call.

--

tls: fix send() returning success after the TCP send failed

The patch adds an abort_err field to struct tls_sw_context_tx. It is set
when tls_tx_records() aborts the connection after a failed TCP send. The
field is checked in tls_tx_records(), tls_push_record() and the
tls_sw_sendmsg_locked() loop, and bpf_exec_tx_verdict() uses it to tell
that the record was not sent and to take it out of copied.

> diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
> index 226e0f176fdc8..795cbfe5fb2a1 100644
> --- a/net/tls/tls_sw.c
> +++ b/net/tls/tls_sw.c

[ ... ]

> @@ -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;
>  }

[Severity: High]
When encryption completes asynchronously, can send() still return success
after the TCP send fails in that same call?

With an async AEAD, or on the MAY_BACKLOG -EBUSY path that ends up as
-EINPROGRESS, tls_push_record() returns before it reaches
tls_tx_records():

    rc = tls_do_encryption(sk, tls_ctx, ctx, req, ...);
    if (rc < 0) {
        if (rc != -EINPROGRESS)
            tls_err_abort(sk, -EBADMSG);
        ctx->async_capable = 1;
        return rc;
    }

That makes err == -EINPROGRESS here, so the new branch is skipped and
nothing is subtracted from *copied.

The record then goes to TCP later in the same sendmsg() call, from the
send_end block of tls_sw_sendmsg_locked():

    /* Transmit if any encryptions have completed */
    if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
        cancel_delayed_work(&ctx->tx_work.work);
        tls_tx_records(sk, msg->msg_flags);
    }

end:
    ret = sk_stream_error(sk, msg->msg_flags, ret);
    return copied > 0 ? copied : ret;

Say tcp_sendmsg_locked() fails there with -EPIPE after shutdown(SHUT_WR)
or a reset. tls_tx_records() sets ctx->abort_err, but its return value
is dropped. ret is still 0 and copied is positive, so the caller is told
that the record was sent.

The two in-loop "Transmit if any encryptions have completed" blocks, on
the zerocopy and copy paths, follow the same pattern. The next iteration
hits the new abort_err check and jumps to send_end, but copied still
counts the failed records.

Take the reproducer from the commit message on a system where gcm(aes)
completes asynchronously. The first send has async_capable == 0 and
eor == true, so it takes the zerocopy path. Then:

tls_sw_sendmsg_locked()
  bpf_exec_tx_verdict() returns -EINPROGRESS, num_async++
  send_end: tls_encrypt_async_wait() returns 0
  tls_tx_records()
    tls_push_sg()
      tcp_sendmsg_locked() returns -EPIPE
  return copied

So it looks like send() would still print 5. Later sends do now fail with
EPIPE because of the new check at the top of the loop.

tx_work_handler() also ignores the result of tls_tx_records(). There,
though, the send that queued the record has usually returned already.

The commit message says "Do not count the record as sent" and "The send()
above then fails with EPIPE". Should the tls_tx_records() error from the
send_end and in-loop transmits be propagated, or ctx->abort_err be checked
after them, so that the failed records are not counted in copied?

This first-send behaviour on the async path also existed before
b341ca51d267. Since this patch targets this class of bug, would it make
sense to cover the async path too?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119141106.840038-1-hengyul%40cs.unc.edu

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23  2:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 14:11 [PATCH net v2] tls: fix send() returning success after the TCP send failed Hengyu Liang
2026-11-23  2:13 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®