Netdev List
 help / color / mirror / Atom feed
From: Dickson Wu via B4 Relay <devnull+dickson.wu.certik.com@kernel.org>
To: netdev@vger.kernel.org
Cc: John Fastabend <john.fastabend@gmail.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	Sabrina Dubroca <sd@queasysnail.net>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	 Simon Horman <horms@kernel.org>,
	security@kernel.org
Subject: [PATCH net] tls: avoid recursion in tls_rx_rec_wait()
Date: Fri, 27 Nov 2026 14:01:16 +0000	[thread overview]
Message-ID: <20261127-ktls-recwait-v1-1-81155599e61e@certik.com> (raw)

From: Dickson Wu <dickson.wu@certik.com>

When a ready record disappears from the TCP receive queue while the
socket lock is released, tls_strp_msg_load() returns false and
tls_rx_rec_wait() retries by calling itself.

The self-call was expected to become a loop, but the wait entry is
address-taken and live across the call, so the compiler keeps a real
call. GCC 13.3 and Clang 18.1 both do this on x86_64 defconfig and on a
distro config, with and without KASAN, costing 128 to 288 bytes of
stack per retry. A local user can keep draining the queue with
TCP_ZEROCOPY_RECEIVE while a reader waits and overflow the kernel
stack. With CONFIG_VMAP_STACK that hits the guard page and panics.

Go back to the top of the function instead, as suggested when the
retry was added, resetting released and ret to what the recursive call
used.

Suggested-by: Paolo Abeni <pabeni@redhat.com>
Link: https://lore.kernel.org/netdev/b3a83032-8ed4-4d5d-9df9-6dbd02acac1c@redhat.com/
Fixes: 6db015fc4b5d ("tls: handle data disappearing from under the TLS ULP")
Signed-off-by: Dickson Wu <dickson.wu@certik.com>
---
Tested under x86_64 UML (VMAP_STACK, 16 KiB stack, PANIC_ON_OOPS) with a
small UML-only change so the guard page hit gets reported, networking
code otherwise stock:

- Stock and with the patch reverted: an unprivileged process (uid
  1000, also tried as uid 65534 in its own user and net namespace)
  draining a waiting kTLS reader with TCP_ZEROCOPY_RECEIVE hit the
  stack guard page within 50 retry cycles under KASAN, and within 100
  cycles on a non-KASAN GCC build.
- Patched: the same driver completed 5000 drains, up to 743 in a row
  inside a single recvmsg() call, with no panic.
- Blocking, non-blocking, MSG_WAITALL, MSG_PEEK, a 4 MiB transfer and
  EOF on close behave the same on stock and patched.
- W=1 build of net/tls/ is clean.

The tls module has to be built in or already loaded, since an
unprivileged TCP_ULP setsockopt won't autoload it. A reproducer is
available if it helps.

This was found and the fix prepared with AI coding tools (OpenAI Codex
agents, with review by Claude). I went over the analysis and the patch
before signing off.

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 226e0f176fdc..363aae36ac69 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -1104,9 +1104,11 @@ tls_rx_rec_wait(struct sock *sk, bool nonblock, bool released, bool has_copied)
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
-	int ret = 0;
+	int ret;
 	long timeo;
 
+retry:
+	ret = 0;
 	/* a rekey is pending, let userspace deal with it */
 	if (unlikely(ctx->key_update_pending))
 		return -EKEYEXPIRED;
@@ -1167,8 +1169,10 @@ tls_rx_rec_wait(struct sock *sk, bool nonblock, bool released, bool has_copied)
 			return sock_intr_errno(timeo);
 	}
 
-	if (unlikely(!tls_strp_msg_load(&ctx->strp, released)))
-		return tls_rx_rec_wait(sk, nonblock, false, has_copied);
+	if (unlikely(!tls_strp_msg_load(&ctx->strp, released))) {
+		released = false;
+		goto retry;
+	}
 
 	return 1;
 }
-- 
2.43.0



             reply	other threads:[~2026-11-27 17:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 14:01 Dickson Wu via B4 Relay [this message]
2026-11-30 11:48 ` [PATCH net] tls: avoid recursion in tls_rx_rec_wait() Sabrina Dubroca

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127-ktls-recwait-v1-1-81155599e61e@certik.com \
    --to=devnull+dickson.wu.certik.com@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dickson.wu@certik.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=security@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox