Skip to contentSkip to conversation

tls: don't hide a queued KeyUpdate from poll()

PATCH Applied Felipe Pereira Stival 5 participants 10 messages Nov 18 – Nov 26

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover tls: don't hide a queued KeyUpdate from poll()

A kTLS reader that waits for a read event before each recv() can stall after the peer sends a TLS 1.3 KeyUpdate. I ran into this while testing a kTLS patch for Erlang/OTP.

Erlang does one recv() per read event, with level-triggered epoll. When application data is followed by a KeyUpdate, one recv() can decrypt both records, return the data without an error, and leave the KeyUpdate queued. Because a rekey is now pending, poll() no longer reports the socket as readable. Without another read event, the reader never calls recv() again, never sees the -EIO for the KeyUpdate, and never provides the new key.

Patch 1 keeps the socket readable until the reader has consumed the KeyUpdate. As before, poll() doesn't report records after the KeyUpdate until userspace provides the new key. Patch 2 adds a selftest, which fails without patch 1.

I worked on this with an AI coding assistant (Claude). It wrote the first drafts of the code and the commit messages, and I reviewed and reworked them. I directed the whole process, from tracing the missing read event to tls_sk_poll() to checking how other kinds of readers are affected and testing in QEMU/KVM.

---
Felipe Pereira Stival (2):
tls: don't hide a queued KeyUpdate from poll()
selftests: tls: add a poll test for a queued KeyUpdate

 Documentation/networking/tls.rst  |  7 ++++---
 net/tls/tls_main.c                |  6 +++---
 tools/testing/selftests/net/tls.c | 40 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 6 deletions(-)

---
base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5
change-id: 20261118-v0idpwn-tls-poll-parked-keyupdate-21792b8cfe6d

Best regards,

Signature · 1 line
Felipe Pereira Stival <[email protected]>
#1 lore raw

PATCH 1/2 tls: don't hide a queued KeyUpdate from poll()

When application data and a KeyUpdate are both queued, the recv() for the data can also decrypt the KeyUpdate. That makes a rekey pending, and the KeyUpdate is left on rx_list because its record type differs. tls_sk_poll() reports no read events while a rekey is pending, so the socket doesn't poll readable although a recvmsg() would return the KeyUpdate. A reader that waits for a read event before each recv() never reads it, never provides the new key and receives nothing more.

Report read events for records on rx_list while a rekey is pending, since they are already decrypted. Keep ignoring the records still in the strparser, so once the KeyUpdate has been read there are no read events until the new key is provided, as before. Update the documentation to say so.

Found with a level-triggered epoll reader that does one recv() per read event. Without this patch it gets the data and then no read event for the KeyUpdate; with it, the KeyUpdate is reported and the rekey completes.

Fixes 47069594e67e ("tls: implement rekey for TLS1.3")
Assisted-by Claude:claude-fable-5-2
---
 Documentation/networking/tls.rst | 7 ++++---
 net/tls/tls_main.c               | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)
Documentation/networking/tls.rst +4 -3
index 980c442d716..e07c1a001b2 100644
@@ -215,9 +215,10 @@ To prevent attempting to decrypt incoming records using the wrong key,
 decryption will be paused when a KeyUpdate message is received by the
 kernel, until the new key has been provided using the TLS_RX socket
 option. Any read occurring after the KeyUpdate has been read and
-before the new key is provided will fail with EKEYEXPIRED. poll() will
-not report any read events from the socket until the new key is
-provided. There is no pausing on the transmit side.
+before the new key is provided will fail with EKEYEXPIRED. Once the
+KeyUpdate has been read, poll() will not report any read events from
+the socket until the new key is provided. There is no pausing on the
+transmit side.
 
 Userspace should make sure that the crypto_info provided has been set
 properly. In particular, the kernel will not check for key/nonce
net/tls/tls_main.c +3 -3
index b8d5a843f76..4c4c2801ae0 100644
@@ -414,9 +414,9 @@ static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
 	tls_ctx = tls_get_ctx(sk);
 	ctx = tls_sw_ctx_rx(tls_ctx);
 
-	if ((skb_queue_empty_lockless(&ctx->rx_list) &&
-	     !tls_strp_msg_ready(ctx)) ||
-	    READ_ONCE(ctx->key_update_pending))
+	if (skb_queue_empty_lockless(&ctx->rx_list) &&
+	    (!tls_strp_msg_ready(ctx) ||
+	     READ_ONCE(ctx->key_update_pending)))
 		mask &= ~(EPOLLIN | EPOLLRDNORM);
 
 	return mask;
Signature · 1 line
2.56.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] Lost wakeup: a KeyUpdate that is moved onto rx_list while another task is polling stays hidden from that poller.

--

tls: don't hide a queued KeyUpdate from poll()

This commit changes tls_sk_poll() so that records already decrypted onto rx_list show up as readable even while a rekey is pending. Records still held by the strparser stay hidden until the new key is provided. The documentation is updated to match.

[ ... ]

Quoted 5 lines
net/tls/tls_main.c +0 -0
index b8d5a843f7617..4c4c2801ae065 100644

net/tls/tls_main.c static __poll_t tls_sk_poll(struct file *file, struct socket *sock, · line 420

Quoted 10 lines
net/tls/tls_main.c +3 -3
@@ -414,9 +414,9 @@ static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
 	tls_ctx = tls_get_ctx(sk);
 	ctx = tls_sw_ctx_rx(tls_ctx);
 
-	if ((skb_queue_empty_lockless(&ctx->rx_list) &&
-	     !tls_strp_msg_ready(ctx)) ||
-	    READ_ONCE(ctx->key_update_pending))
+	if (skb_queue_empty_lockless(&ctx->rx_list) &&
+	    (!tls_strp_msg_ready(ctx) ||
+	     READ_ONCE(ctx->key_update_pending)))
 		mask &= ~(EPOLLIN | EPOLLRDNORM);

[Severity: Medium]
Can a poller that is already waiting still miss the KeyUpdate here?

tls_sk_poll() takes no lock. The KeyUpdate only lands on rx_list at the very end of the tls_sw_recvmsg() step that decrypts it:

tls_sw_recvmsg()
tls_rx_one_record()

    tls_check_pending_rekey()
      WRITE_ONCE(rx_ctx->key_update_pending, true);

tls_record_content_type() /* returns 0, record type differs */
tls_rx_rec_done(ctx); /* clears msg_ready / msg_announced */
__skb_queue_tail(&ctx->rx_list, darg.skb);

A tls_sk_poll() from another task can run between the point where key_update_pending is set and __skb_queue_tail(). It can also run in the earlier gap, after the data record is consumed and before the KeyUpdate is parsed, when msg_ready is 0. Either way it sees an empty rx_list and clears EPOLLIN.

Nothing seems to wake the socket after the KeyUpdate is queued. The deferred wakeup in tls_rx_reader_release() doesn't fire in this state:

net/tls/tls_sw.c:tls_rx_msg_maybe_announce() {
	...
	if (!READ_ONCE(strp->msg_ready) || strp->msg_announced)
		return;
	strp->msg_announced = 1;
	...
	ctx->saved_data_ready(strp->sk);
}

The same reader already consumed the KeyUpdate from the strparser, so msg_ready is 0 at that point. The only other wakeup, in tls_finish_key_update(), runs after userspace installs the new key.

One way to hit this is several threads sharing one level-triggered epoll instance on the TLS fd:

T1 gets the event and enters recvmsg().
The LT item is re-queued and T2 is woken.
T2's ep_item_poll() lands in the window above, gets no events and
drops the item from the ready list.
T1 queues the KeyUpdate on rx_list, returns the data and calls
epoll_wait() again. The ready list is empty, so it sleeps.

No task reads the KeyUpdate, so no new key is installed. The connection then stalls until the peer sends more data. That may never happen if the peer is waiting for a reply, for example when update_requested is set. The same wakeup is lost when a poller thread signals a separate reader thread.

The commit message says:

A reader that waits for a read event before each recv() never reads it, never provides the new key and receives nothing more.

That fix seems to hold only when the same task polls again after recvmsg() returns. This matches the single-threaded LT reader described here and the selftest added in "selftests: tls: add a poll test for a queued KeyUpdate".

Would it make sense to also call ctx->saved_data_ready(sk)? One place would be where the KeyUpdate is queued on rx_list. Another would be in tls_rx_reader_release() when rx_list is not empty and key_update_pending is set.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-v0idpwn-tls-poll-parked-keyupdate-v1-0-8e25101e7be6%40gmail.com
#3 lore raw
Quoted 1 line

Can a poller that is already waiting still miss the KeyUpdate here?

Yes, any record tls_sw_recvmsg() puts on rx_list has no wakeup for concurrent polls. This predates this patch, and I'd say it belongs in a separate discussion.

reviewer Reviewed-by
#4 lore raw
Quoted 5 lines · Felipe Stival
Quoted 1 line

Can a poller that is already waiting still miss the KeyUpdate here?

Yes, any record tls_sw_recvmsg() puts on rx_list has no wakeup for concurrent polls. This predates this patch, and I'd say it belongs in a separate discussion.

Yes, that seems reasonable to me.

maintainer Reviewed-by on v1 1/2
#5 lore raw
In reply to Felipe Pereira Stival
Quoted 21 lines · 2026-11-18, 17:35:48 -0300, Felipe Pereira Stival via B4 Relay wrote:

From: Felipe Pereira Stival <[email protected]>

When application data and a KeyUpdate are both queued, the recv() for the data can also decrypt the KeyUpdate. That makes a rekey pending, and the KeyUpdate is left on rx_list because its record type differs. tls_sk_poll() reports no read events while a rekey is pending, so the socket doesn't poll readable although a recvmsg() would return the KeyUpdate. A reader that waits for a read event before each recv() never reads it, never provides the new key and receives nothing more.

Report read events for records on rx_list while a rekey is pending, since they are already decrypted. Keep ignoring the records still in the strparser, so once the KeyUpdate has been read there are no read events until the new key is provided, as before. Update the documentation to say so.

Found with a level-triggered epoll reader that does one recv() per read event. Without this patch it gets the data and then no read event for the KeyUpdate; with it, the KeyUpdate is reported and the rekey completes.

Quoted 7 lines
Fixes 47069594e67e ("tls: implement rekey for TLS1.3")
Assisted-by Claude:claude-fable-5-2
---
 Documentation/networking/tls.rst | 7 ++++---
 net/tls/tls_main.c               | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
Sabrina
#6 lore raw

PATCH 2/2 selftests: tls: add a poll test for a queued KeyUpdate

In reply to Felipe Pereira Stival

Check that poll() reports a read event when a recv() for data has decrypted and queued a KeyUpdate. Once the KeyUpdate has been read, check that poll() doesn't report a record sent with the new key.

Assisted-by Claude:claude-fable-5-2
---
 tools/testing/selftests/net/tls.c | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
tools/testing/selftests/net/tls.c +40 -0
index bd2797f9f61..59e403b5f12 100644
@@ -2421,6 +2421,46 @@ TEST_F(tls, rekey_poll_delay)
 	}
 }
 
+TEST_F(tls, rekey_poll_queued)
+{
+	char const *test_str = "test_message";
+	struct tls_crypto_info_keys tls12;
+	struct pollfd pfd = { };
+	char buf[100];
+	int send_len;
+
+	if (self->notls)
+		SKIP(return, "no TLS support");
+
+	if (variant->tls_version != TLS_1_3_VERSION)
+		return;
+
+	/* data followed by a KeyUpdate */
+	send_len = strlen(test_str) + 1;
+	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
+	tls_send_keyupdate(_metadata, self->fd);
+
+	/* reading the data decrypts the KeyUpdate and leaves it queued */
+	EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_WAITALL), send_len);
+	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
+
+	/* KeyUpdate hasn't been read, expect cfd to be readable */
+	pfd.fd = self->cfd;
+	pfd.events = POLLIN;
+	EXPECT_EQ(poll(&pfd, 1, 0), 1);
+
+	/* get KeyUpdate */
+	tls_recv_keyupdate(_metadata, self->cfd, 0);
+
+	/* update TX key and send with it */
+	tls_crypto_info_init(variant->tls_version, variant->cipher_type, &tls12, 1);
+	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
+	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
+
+	/* key hasn't been updated, expect cfd to be non-readable */
+	EXPECT_EQ(poll(&pfd, 1, 0), 0);
+}
+
 struct raw_rec {
 	unsigned int plain_len;
 	unsigned char plain_data[100];
Signature · 1 line
2.56.0
reviewer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH net 2/2] selftests: tls: add a poll test for a queued KeyUpdate

Quoted 6 lines · Felipe Pereira Stival

Check that poll() reports a read event when a recv() for data has decrypted and queued a KeyUpdate. Once the KeyUpdate has been read, check that poll() doesn't report a record sent with the new key.

Assisted-by Claude:claude-fable-5-2
maintainer Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH net 2/2] selftests: tls: add a poll test for a queued KeyUpdate

In reply to Felipe Pereira Stival
Quoted 11 lines · 2026-11-18, 17:35:49 -0300, Felipe Pereira Stival via B4 Relay wrote:

From: Felipe Pereira Stival <[email protected]>

Check that poll() reports a read event when a recv() for data has decrypted and queued a KeyUpdate. Once the KeyUpdate has been read, check that poll() doesn't report a record sent with the new key.

Assisted-by Claude:claude-fable-5-2
---
 tools/testing/selftests/net/tls.c | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
Signature · 1 line
Sabrina
#9 lore raw
In reply to Felipe Pereira Stival
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 12 lines · On Wed, 18 Nov 2026 17:35:47 -0300 you wrote:

A kTLS reader that waits for a read event before each recv() can stall after the peer sends a TLS 1.3 KeyUpdate. I ran into this while testing a kTLS patch for Erlang/OTP.

Erlang does one recv() per read event, with level-triggered epoll. When application data is followed by a KeyUpdate, one recv() can decrypt both records, return the data without an error, and leave the KeyUpdate queued. Because a rekey is now pending, poll() no longer reports the socket as readable. Without another read event, the reader never calls recv() again, never sees the -EIO for the KeyUpdate, and never provides the new key.

Quoted 1 line

[...]

Here is the summary with links:
- [net,1/2] tls: don't hide a queued KeyUpdate from poll()

    https://git.kernel.org/netdev/net/c/285ca329ad4b
  - [net,2/2] selftests: tls: add a poll test for a queued KeyUpdate
    https://git.kernel.org/netdev/net/c/d26b244cdec3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html