From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796157480; b=MM0EsEfDF1BGnBG7+pqo42aV+GtJIUz+9Ct7+NNQ4LCWStQf3ZScr/FHXUBGwAfV+t2W Fw9gTHN7TMcQVRa8xmuV+WbFB6SZww8fyxvrMo+Gl89xaKuVQuKxGYrBI5RHmfXhzHPfN gZFWE3AVgp49EUF7izlwZcd8akCI3j/SA1g/VPOIIEDtUjLE9jzDUsoDRUfd9GJncHhaF 92wNadSz+J0mXhfCk/opzs9TpfkUO+3bl2G6ogR/EVta1VVlW+thEFuAeRu7+a4rYdlID HZPWqkH7Nttz+LpOyojD39WUDExoI91H0vzTu9mIAQDcL+NePeWbsdBqdCdixn1/FVg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796157480; h=Received:DKIM-Signature:Received:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:From:Date:Subject:In-Reply-To: References:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To: Content-Transfer-Encoding; bh=+uzDH4CkD65Gi9BBa0OJoDf138ZoGLrXyYPr041HsBM=; b=ZOQKt/Kse2YFfm/N/sW9W33UhBY7knHg/w7mXrZ03oghoh/DV/+Sjd9r9IHevUaO+fQR 6ga3zfRHokHX6KSataLz9g8a7hwcZeCrYpJKJRR2dPTxcBmHjjBQoNdfFcjOjNe/dLetN jQ4776dF3hUAjHXd1pq7Gcv/qabHxJWGqnnQAx7iGfOS9yPUljaVhH0xnuC/41jt4cVy/ 2iEAKOEo082A9NiA+EJmFVg7T1cg7kx/MLRueAJ/tIPEicUm+Feb0htkLPmXHyLEnel4i 61D6yl3n7DWQitzrWKbWutPH28SBKTm45LELTlDr6i83xOxGscSF6E3Br0wI/1zWPVg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Gd3FYnkr; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2713D606A31A for ; Tue, 1 Dec 2026 20:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Gd3FYnkr Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796157480; b=RulC0WvOR3TebB3lUHN1vKBUbpGSaZ99VBWTQbdEP0vneNYVMg5agugNYCyEFbtGYMVn 9x7Oj6C8Vt4+vc81BTOwIbB4De0qucZqmtzpS/28SwmoQHQX9lNhl8rSI9/wD851JNrDj v3DFJADS626waApN+KCqVnRmNE5fk2iVN3Geaqkm6CPlGw3AlERVDdcg9WatX974fw64g LgvTOP524osBMosjaOd6nWaBPyjMRHYFZL3Vot8g/tsqrO6jhqc2Li9pIUYE/HWFBeo3z 8JdyUzVklCC85ouagJWb1p9Fa5R1/kSEEvj0elcUTkTzojSCEr/aHtOR/W/3sumNe7w== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796157480; h=DKIM-Signature:Received:MIME-Version:Content-Type:Message-Id: X-B4-Tracking:X-Change-ID:From:Date:Subject:In-Reply-To:References:To: Cc:X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+uzDH4CkD65Gi9BBa0OJoDf138ZoGLrXyYPr041HsBM=; b=KmR+j+b9/yYpJ2SLvzPzu+AcKGpLC9GX44bNj3MjXGyxKOTYEEUs2Z62FhL0/Mbw5AaM UBwY7/iD1h7QyZSd7ksl50UTh/a2xwyZHLHnSyZe6yCYjdf4Ov1BI32jjCxcO2YUP5Gl4 ZhyoYfK7uF9V+K5IW3lAtlBnUvNTRDSspsPT7WdIk8CPaLVhq6jfKANx7ID5DCoLJ10gT BtEhnMdyV7QB4FmkFuMw0pk5m1UPv9QKR5yIJX5bgTeahHxE8B6v8gLBbOET80zg9yz7d xWmakihnqJJNZ8ufG4utElyD7eBzyx37o+dcOoAUPOLsUO7h5uOLMrxHe5QvLn5/QNQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 7EF011F00893; Tue, 1 Dec 2026 20:37:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796157479; bh=+uzDH4CkD65Gi9BBa0OJoDf138ZoGLrXyYPr041HsBM=; h=From:Date:Subject:In-Reply-To:References:To:Cc:Reply-To; b=Gd3FYnkrBsv7/ywabgGMzLRt462UEyHMmKXYr/oprPjOpRixICdL0Yb1Eb7SohEqC dh6g1XMefoh5HuuS1lKxpKipTMgDp+93+gQ1ucqQlXBZ12KX6BehN+Q2gKGwlSQPK0 SW8W5wJCoHsVMXsBinWcFCgd6vx7vqdOo5D7nPizI6cDw1xnPzgu1rMJFNA/lfGl1S 2x8BYNdLcMMEcBvvsLRd5B27nrqd7bemGnXm6hJ5HZ/5bhhgj2S1cj8Lunz/pLNbMp WPxGpr5xMq+tXuQhR7l1t5rbijk0lEMLgMHwMWyD58SL0qGP/UfUUgEcYEchfJZyHV 8M9sHX3vVFmnQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0D4D74F0A7; Tue, 1 Dec 2026 20:37:59 +0000 (UTC) X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-ktls-recwait-v2-1-12d374acc0ae@certik.com> X-B4-Tracking: v=1; b=H4sIAAVyDmsC/yWMSw7CMAwFr1J5jSUSBFG5CmLhJIYaqgTF4SNVv TsJLGf03iygXIQVjsMChV+iklMDuxkgTJSujBIbg93agzHW4b3OioXDm6QiOed24z6YSCO0y6P wRT6/3AkSVzj/pT79jUPtoT7zpIy+UApTV8op1pxn7B7W9Qth0mYylQAAAA== X-Change-ID: 20261127-ktls-recwait-a777395c1da9 From: Dickson Wu via B4 Relay Date: Tue, 01 Dec 2026 20:37:45 +0000 Subject: [PATCH net v2] tls: avoid recursion in tls_rx_rec_wait() In-Reply-To: <20261127-ktls-recwait-v1-1-81155599e61e@certik.com> References: <20261127-ktls-recwait-v1-1-81155599e61e@certik.com> To: netdev@vger.kernel.org Cc: John Fastabend , Jakub Kicinski , Sabrina Dubroca , "David S. Miller" , Eric Dumazet , Paolo Abeni , Willem de Bruijn , Simon Horman , security@kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796108805; l=3681; i=dickson.wu@certik.com; s=dickson-b4; h=from:subject:message-id; bh=W1dw1HRURDLpJqoyRghdOrlHgY23pLvFQfccEvB6uJY=; b=1rOYFjuaKj9AK8iAE3cFWpKXr0NL0Uzz7m+I61I2KDpTFzaWeGivZALqVUIN8H0GsHXPD1fxP /kv6Qvo1y1LDLsnnnIn9ZxG2h/fRHpYKOIbd/q88bgBCWNRfYf/vpB0 X-Developer-Key: i=dickson.wu@certik.com; a=ed25519; pk=fxH7FS4FdojeJSGLJzgKdfO51Rvr4EW30yinj2mc1vg= X-Endpoint-Received: by B4 Relay for dickson.wu@certik.com/dickson-b4 with auth_id=1647 X-Original-From: Dickson Wu Reply-To: dickson.wu@certik.com Content-Transfer-Encoding: 7bit From: Dickson Wu 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 wait-queue helpers receive a pointer to a wait entry on this function's stack. The compiler cannot tell whether they still have that pointer after the entry is removed, so it keeps the stack frame when the function calls itself again. GCC 14.2.0 and 15.3.0 and Clang 19.1.7 and 20.1.8 all emit a call on x86_64 defconfig with CONFIG_TLS=y, both with and without KASAN. Each retry adds 128 to 288 bytes to the kernel stack. 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 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 --- Changes in v2: - Order the local declarations from longest to shortest. - Explain why the compiler keeps the wait entry's stack frame. - Report GCC 14/15 and Clang 19/20 results with and without KASAN. 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. net/tls/tls_sw.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 226e0f1..17d39d5 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -1101,12 +1101,13 @@ unlock: static int 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); + struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_get_ctx(sk)); DEFINE_WAIT_FUNC(wait, woken_wake_function); - int ret = 0; long timeo; + int ret; +retry: + ret = 0; /* a rekey is pending, let userspace deal with it */ if (unlikely(ctx->key_update_pending)) return -EKEYEXPIRED; @@ -1167,8 +1168,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