Netdev List
 help / color / mirror / Atom feed
* [PATCH net] tls: avoid recursion in tls_rx_rec_wait()
@ 2026-11-27 14:01 Dickson Wu via B4 Relay
  2026-11-30 11:48 ` Sabrina Dubroca
  2026-12-01 20:37 ` [PATCH net v2] " Dickson Wu via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Dickson Wu via B4 Relay @ 2026-11-27 14:01 UTC (permalink / raw)
  To: netdev
  Cc: John Fastabend, Jakub Kicinski, Sabrina Dubroca, David S. Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman,
	security

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



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

* Re: [PATCH net] tls: avoid recursion in tls_rx_rec_wait()
  2026-11-27 14:01 [PATCH net] tls: avoid recursion in tls_rx_rec_wait() Dickson Wu via B4 Relay
@ 2026-11-30 11:48 ` Sabrina Dubroca
  2026-11-30 18:51   ` Dickson Wu
  2026-12-01 20:38   ` Dickson Wu
  2026-12-01 20:37 ` [PATCH net v2] " Dickson Wu via B4 Relay
  1 sibling, 2 replies; 5+ messages in thread
From: Sabrina Dubroca @ 2026-11-30 11:48 UTC (permalink / raw)
  To: dickson.wu
  Cc: netdev, John Fastabend, Jakub Kicinski, David S. Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman

I don't think you need to bother security@kernel.org since you sent
this publicly. Dropped the cc.

2026-11-27, 14:01:16 +0000, Dickson Wu via B4 Relay wrote:
> 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

I'm not clear on what "address-taken" means and why it makes a
difference to the compiler in deciding tail call vs real call.
(not sure yet if it's worth adding to the commit message)

> call. GCC 13.3 and Clang 18.1 both do this on x86_64 defconfig and on a

In https://lore.kernel.org/netdev/20250812062825.391c030c@kernel.org/
Jakub said "both clang and gcc get it right", I wonder what
changed. Or is it just old versions being a bit dumb?

> 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

Or we should disallow this for all ULPs? I think they're all going to
get confused by their data being stolen.


[...]
> 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;

nit: reverse xmas tree
https://docs.kernel.org/process/maintainer-netdev.html

>  
> +retry:
> +	ret = 0;
>  	/* a rekey is pending, let userspace deal with it */
>  	if (unlikely(ctx->key_update_pending))
>  		return -EKEYEXPIRED;

-- 
Sabrina

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

* Re: [PATCH net] tls: avoid recursion in tls_rx_rec_wait()
  2026-11-30 11:48 ` Sabrina Dubroca
@ 2026-11-30 18:51   ` Dickson Wu
  2026-12-01 20:38   ` Dickson Wu
  1 sibling, 0 replies; 5+ messages in thread
From: Dickson Wu @ 2026-11-30 18:51 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: netdev, John Fastabend, Jakub Kicinski, David S. Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman

On Mon, Nov 30, 2026 at 11:48 AM, Sabrina Dubroca wrote:
> I'm not clear on what "address-taken" means and why it makes a
> difference to the compiler in deciding tail call vs real call.
> (not sure yet if it's worth adding to the commit message)

The wait entry from DEFINE_WAIT_FUNC() lives in this frame, and its
address is handed to add_wait_queue() and sk_wait_event(). Once a
local's address has escaped like that, the compiler can't prove the
callee won't reach it, so it won't reuse the frame for a sibling call.
I'll reword that part in v2 so it doesn't lean on the jargon.

> In https://lore.kernel.org/netdev/20250812062825.391c030c@kernel.org/
> Jakub said "both clang and gcc get it right", I wonder what
> changed. Or is it just old versions being a bit dumb?

I don't know what Jakub built with. I checked GCC 13.3 and Clang 18.1
on mainline 9437ee8b6cf6, x86_64 defconfig with and without KASAN plus
a distro config: all six objects keep a real call to tls_rx_rec_wait()
at the retry, 128 to 288 bytes of stack per level. The function looked
the same in 6db015fc4b5d, wait entry included, so I don't think the
code changed under it. I haven't tried newer compilers yet. I'll check
GCC 14/15 and Clang 19/20 before v2 and include what I find.

> Or we should disallow this for all ULPs? I think they're all going to
> get confused by their data being stolen.

That would close it at the source, and I'm happy to look at it. I'd
still keep the loop, though: 6db015fc4b5d also mentions a reader that
entered before the ULP was installed, and the retry shouldn't cost
stack whichever way the data went.

> nit: reverse xmas tree

Will fix in v2.

Thanks,
Dickson

On Mon, 30 Nov 2026 12:48:00 +0100, Sabrina Dubroca <sd@queasysnail.net> wrote:
> I don't think you need to bother security@kernel.org since you sent
> this publicly. Dropped the cc.
>
> 2026-11-27, 14:01:16 +0000, Dickson Wu via B4 Relay wrote:
> > 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
>
> I'm not clear on what "address-taken" means and why it makes a
> difference to the compiler in deciding tail call vs real call.
> (not sure yet if it's worth adding to the commit message)
>
> > call. GCC 13.3 and Clang 18.1 both do this on x86_64 defconfig and on a
>
> In https://lore.kernel.org/netdev/20250812062825.391c030c@kernel.org/
> Jakub said "both clang and gcc get it right", I wonder what
> changed. Or is it just old versions being a bit dumb?
>
> > 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
>
> Or we should disallow this for all ULPs? I think they're all going to
> get confused by their data being stolen.
>
>
> [...]
> > 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;
>
> nit: reverse xmas tree
> https://docs.kernel.org/process/maintainer-netdev.html
>
> >
> > +retry:
> > +	ret = 0;
> >  	/* a rekey is pending, let userspace deal with it */
> >  	if (unlikely(ctx->key_update_pending))
> >  		return -EKEYEXPIRED;
>
> --
> Sabrina

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

* [PATCH net v2] tls: avoid recursion in tls_rx_rec_wait()
  2026-11-27 14:01 [PATCH net] tls: avoid recursion in tls_rx_rec_wait() Dickson Wu via B4 Relay
  2026-11-30 11:48 ` Sabrina Dubroca
@ 2026-12-01 20:37 ` Dickson Wu via B4 Relay
  1 sibling, 0 replies; 5+ messages in thread
From: Dickson Wu via B4 Relay @ 2026-12-01 20:37 UTC (permalink / raw)
  To: netdev
  Cc: John Fastabend, Jakub Kicinski, Sabrina Dubroca, David S. Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman,
	security

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



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

* Re: [PATCH net] tls: avoid recursion in tls_rx_rec_wait()
  2026-11-30 11:48 ` Sabrina Dubroca
  2026-11-30 18:51   ` Dickson Wu
@ 2026-12-01 20:38   ` Dickson Wu
  1 sibling, 0 replies; 5+ messages in thread
From: Dickson Wu @ 2026-12-01 20:38 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: netdev, John Fastabend, Jakub Kicinski, David S. Miller,
	Eric Dumazet, Paolo Abeni, Willem de Bruijn, Simon Horman

On Mon, 30 Nov 2026 11:48:00 +0000, Sabrina Dubroca wrote:
> I don't think you need to bother security@kernel.org since you sent
> this publicly. Dropped the cc.

Sorry, v2 still had security@kernel.org on Cc. My mistake, I'll leave
it off from here on.

v2 reorders the declarations and reworks the compiler explanation:
https://lore.kernel.org/netdev/20261201-ktls-recwait-v2-1-12d374acc0ae@certik.com/

Thanks,
Dickson


On Mon, 30 Nov 2026 12:48:00 +0100, Sabrina Dubroca <sd@queasysnail.net> wrote:
> I don't think you need to bother security@kernel.org since you sent
> this publicly. Dropped the cc.
>
> 2026-11-27, 14:01:16 +0000, Dickson Wu via B4 Relay wrote:
> > 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
>
> I'm not clear on what "address-taken" means and why it makes a
> difference to the compiler in deciding tail call vs real call.
> (not sure yet if it's worth adding to the commit message)
>
> > call. GCC 13.3 and Clang 18.1 both do this on x86_64 defconfig and on a
>
> In https://lore.kernel.org/netdev/20250812062825.391c030c@kernel.org/
> Jakub said "both clang and gcc get it right", I wonder what
> changed. Or is it just old versions being a bit dumb?
>
> > 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
>
> Or we should disallow this for all ULPs? I think they're all going to
> get confused by their data being stolen.
>
>
> [...]
> > 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;
>
> nit: reverse xmas tree
> https://docs.kernel.org/process/maintainer-netdev.html
>
> >
> > +retry:
> > +	ret = 0;
> >  	/* a rekey is pending, let userspace deal with it */
> >  	if (unlikely(ctx->key_update_pending))
> >  		return -EKEYEXPIRED;
>
> --
> Sabrina

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

end of thread, other threads:[~2026-12-01 20:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 14:01 [PATCH net] tls: avoid recursion in tls_rx_rec_wait() Dickson Wu via B4 Relay
2026-11-30 11:48 ` Sabrina Dubroca
2026-11-30 18:51   ` Dickson Wu
2026-12-01 20:38   ` Dickson Wu
2026-12-01 20:37 ` [PATCH net v2] " Dickson Wu via B4 Relay

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