Article View: linux.kernel.netdev
Article #25466[PATCH net v2] tls: avoid recursion in tls_rx_rec_wait()
From: Dickson Wu via B4 Relay
Date: Tue, 01 Dec 2026 20:37
Date: Tue, 01 Dec 2026 20:37
95 lines
14583 bytes
14583 bytes
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 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 <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>
---
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
Technical Details
Message-ID:
<20261201-ktls-recwait-v2-1-12d374acc0ae@certik.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375353-linux-kernel-netdev=dewy.ruReferences:
<20261127-ktls-recwait-v1-1-81155599e61e@certik.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375353-linux-kernel-netdev=dewy.ru From: devnull+dickson.wu.certik.com@kernel.org (Dickson Wu via B4 Relay) Newsgroups: linux.kernel.netdev Subject: [PATCH net v2] tls: avoid recursion in tls_rx_rec_wait() Date: Tue, 01 Dec 2026 20:37:45 +0000 Organization: Usenet Network Message-ID: <20261201-ktls-recwait-v2-1-12d374acc0ae@certik.com> References: <20261127-ktls-recwait-v1-1-81155599e61e@certik.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="1814971"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:eGgnBdQZz1Wo715zg3Rc0X1SW1o= sha256:+688k0f/j8o3jtlzeHz3iBXVTSAiBYUCUj8h3gOW/c0= sha1:pf+uX97mvDmsjZFfpv1efIEsyeU= sha256:K2qYUYZ4fq03aMtr70XH6JlhTlYpPkGXUEGXsgbk9DA= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id BF93B117498; Tue, 1 Dec 2026 23:38:14 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id f_L-H-uqZEx6; Tue, 1 Dec 2026 23:38:11 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375353-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id D7D28117493; Tue, 1 Dec 2026 23:38:02 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 34A5229482; Tue, 1 Dec 2026 20:38:01 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796157481; b=dm9WFFK795q7P/lKXde26QtUAaJWF7u90UcfKb8jJV1rysV+LqCcixg8+ceXHOiKTBWt Lde4sKu29BVWSHcs0TQvkBx8pPTtsrhjHSLLaTQUD++jvRish4KT9HZQcpHqsf0A7f+nM yMotI89gznbEPKKD8xI35mHgyaxhWkK4sOAkRawLgR1/16I1L90czBnJ7hjTjkSK/y7AJ z4O95N6Xp9TzH3zWe7l8PXTubmOs42ID7KyMcxnnQZQzqfjssHNSRtVyVtY2YMRDUhjXO AugiMjnXtABRSY477+301advx+A8ORS8DpYhjcH/2kXsi+b8VRtoLdUfMYNWWUXBmiA== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796157481; h=X-Original-To:Received:Received:DKIM-Signature:Received: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence: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=B20pEEpN1YeRmGAfZ2nz2dIDMxowsvoUpDzmrPTM7+EFJF60BXtLtB+GfA7JuWTotuKt rjcFtznOMKm5NzVaH6OZYAfASuAsJay+aOooOvWiRba5a0KI5PsNb7Rbu93YcbZmzbA8r ebitDCtxJQFoF6/RClfeVwQ+7mb0rXeRGPQKAAIKMBwgbn513g7xfTZUZyBAhCR3LUXHE IGqoMKp1eargfO2+nWAqNq+uiMJnxgyoo1lP4xUKzZhoNA9rS+VBdhxqQCPu1/k2EKa+0 IZCorM4YQJchZyAusJ6juqyvOL7sqeyGB7JhxrXhunrtP6e7aBXyVnE69WV9vsf7xWg== X-ARC-Authentication-Results: i=3; 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=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B69C606A32C; Tue, 1 Dec 2026 20:38:01 +0000 (UTC) X-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 X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-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== X-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== X-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 X-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 <netdev@vger.kernel.org>; Tue, 1 Dec 2026 20:38:00 +0000 (UTC) X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-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== X-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== X-ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-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) X-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== X-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 X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-B4-Tracking: v=1; b=H4sIAAVyDmsC/yWMSw7CMAwFr1J5jSUSBFG5CmLhJIYaqgTF4SNVv TsJLGf03iygXIQVjsMChV+iklMDuxkgTJSujBIbg93agzHW4b3OioXDm6QiOed24z6YSCO0y6P wRT6/3AkSVzj/pT79jUPtoT7zpIy+UApTV8op1pxn7B7W9Qth0mYylQAAAA== X-Change-ID: 20261127-ktls-recwait-a777395c1da9 X-In-Reply-To: <20261127-ktls-recwait-v1-1-81155599e61e@certik.com> X-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 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 <dickson.wu@certik.com> X-Reply-To: dickson.wu@certik.com