Skip to contentSkip to conversation

kcm: reject TLS on attached TCP sockets

PATCH Applied Rishikesh Jethwani 4 participants 4 messages Nov 16 – Nov 21

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

PATCH v8 1/1 kcm: reject TLS on attached TCP sockets

Neither kcm_attach() nor tls_init() rejects the other, so KCM and kTLS RX can be stacked on the same TCP socket in either order. This breaks in several ways:

- KCM calls read_sock() from its receive callback in atomic context. With kTLS RX this reaches tls_sw_read_sock(), which can sleep.
- Before tls_sw_read_sock() existed, read_sock was tcp_read_sock(), so KCM consumed raw ciphertext from the receive queue that TLS parses.
- Both install socket callbacks. kcm_unattach() restores the pre-KCM sk_data_ready over tls_data_ready, so TLS RX stops getting notified.

The sleep in atomic context was reproduced on an unmodified kernel with CONFIG_DEBUG_ATOMIC_SLEEP.

Reject sockets with a ULP in kcm_attach(), and make tls_init() fail while KCM is attached. A tcp_sock boolean, present when CONFIG_AF_KCM is enabled, tracks attachment. It is set and cleared under the transport socket lock.

Also switch the transport check to sk_is_tcp(). The family and protocol checks alone also accept raw IPPROTO_TCP sockets, which have no tcp_sock.

Validated with KASAN and lockdep: connected IPv4/IPv6 raw sockets, both attach orders, detach, and concurrent installation.

Backports without commit 8cc3aef0cb198 ("tcp: Do not allow buggy transitions between ehash and lhash2.") must also prevent attached sockets from becoming listeners after disconnect.

Fixes c46234ebb4d1 ("tls: RX path for ktls")
Cc <[email protected]> # needs 8cc3aef0cb198 or equivalent, see above
---
 Documentation/networking/kcm.rst |  3 +++
 include/linux/tcp.h              |  3 +++
 include/net/tcp.h                |  9 +++++++++
 net/kcm/kcmsock.c                | 16 +++++++++++++---
 net/tls/tls_main.c               |  3 +++
 5 files changed, 31 insertions(+), 3 deletions(-)
Documentation/networking/kcm.rst +3 -0
index 71f44d0beaa3..07778f29211c 100644
@@ -167,6 +167,9 @@ The kcm_attach structure contains:
   - fd: file descriptor for TCP socket being attached
   - bpf_prog_fd: file descriptor for compiled BPF program downloaded
 
+The transport socket must be a TCP stream socket with no TCP ULP installed.
+TLS cannot be enabled on a transport socket while it is attached to KCM.
+
 Unattach transport sockets
 --------------------------
 
include/linux/tcp.h +3 -0
index e6b79cf4798f..d4b07fbae2d5 100644
@@ -516,6 +516,9 @@ struct tcp_sock {
 #if IS_ENABLED(CONFIG_MPTCP)
 	bool	is_mptcp;
 #endif
+#if IS_ENABLED(CONFIG_AF_KCM)
+	bool	kcm_attached;
+#endif
 #if IS_ENABLED(CONFIG_SMC)
 	bool	syn_smc;	/* SYN includes SMC */
 	bool	(*smc_hs_congested)(const struct sock *sk);
include/net/tcp.h +9 -0
index 5c9203aae834..5e99d3142f5e 100644
@@ -2882,6 +2882,15 @@ void tcp_cleanup_ulp(struct sock *sk);
 void tcp_update_ulp(struct sock *sk, struct proto *p,
 		    void (*write_space)(struct sock *sk));
 
+static inline bool tcp_kcm_attached(const struct sock *sk)
+{
+#if IS_ENABLED(CONFIG_AF_KCM)
+	return tcp_sk(sk)->kcm_attached;
+#else
+	return false;
+#endif
+}
+
 #define MODULE_ALIAS_TCP_ULP(name)				\
 	MODULE_INFO(alias, name);		\
 	MODULE_INFO(alias, "tcp-ulp-" name)
net/kcm/kcmsock.c +13 -3
index a7f94d42036f..ca789812124e 100644
@@ -30,6 +30,7 @@
 #include <net/kcm.h>
 #include <net/netns/generic.h>
 #include <net/sock.h>
+#include <net/tcp.h>
 #include <uapi/linux/kcm.h>
 #include <trace/events/sock.h>
 
@@ -1259,9 +1260,10 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 
 	lock_sock(csk);
 
-	/* Only allow TCP sockets to be attached for now */
-	if ((csk->sk_family != AF_INET && csk->sk_family != AF_INET6) ||
-	    csk->sk_protocol != IPPROTO_TCP) {
+	/* Only allow TCP sockets to be attached for now. A raw socket
+	 * created with IPPROTO_TCP has that sk_protocol but no icsk.
+	 */
+	if (!sk_is_tcp(csk)) {
 		err = -EOPNOTSUPP;
 		goto out;
 	}
@@ -1272,6 +1274,12 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 		goto out;
 	}
 
+	/* KCM and ULPs cannot share the receive path and socket callbacks. */
+	if (inet_csk_has_ulp(csk)) {
+		err = -EOPNOTSUPP;
+		goto out;
+	}
+
 	psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL);
 	if (!psock) {
 		err = -ENOMEM;
@@ -1305,6 +1313,7 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
 	psock->save_write_space = csk->sk_write_space;
 	psock->save_state_change = csk->sk_state_change;
 	csk->sk_user_data = psock;
+	tcp_sk(csk)->kcm_attached = true;
 	WRITE_ONCE(csk->sk_data_ready, psock_data_ready);
 	WRITE_ONCE(csk->sk_write_space, psock_write_space);
 	csk->sk_state_change = psock_state_change;
@@ -1386,6 +1395,7 @@ static void kcm_unattach(struct kcm_psock *psock)
 	WRITE_ONCE(csk->sk_write_space, psock->save_write_space);
 	csk->sk_state_change = psock->save_state_change;
 	strp_stop(&psock->strp);
+	tcp_sk(csk)->kcm_attached = false;
 
 	if (WARN_ON(psock->rx_kcm)) {
 		write_unlock_bh(&csk->sk_callback_lock);
net/tls/tls_main.c +3 -0
index b8d5a843f761..fa71c75c2861 100644
@@ -1067,6 +1067,9 @@ static int tls_init(struct sock *sk)
 	if (sk->sk_state != TCP_ESTABLISHED)
 		return -ENOTCONN;
 
+	if (tcp_kcm_attached(sk))
+		return -EBUSY;
+
 	/* allocate tls context */
 	write_lock_bh(&sk->sk_callback_lock);
 	ctx = tls_ctx_create(sk);
Signature · 1 line
2.50.1
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
Quoted 14 lines · On Mon, Nov 16, 2026 at 11:02 AM <[email protected]> wrote:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

Quoted 5 lines

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages. --
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

Found by code review. The sleep in atomic context was reproduced on an unmodified kernel with CONFIG_DEBUG_ATOMIC_SLEEP.

#3 lore raw
In reply to Rishikesh Jethwani
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 4 lines · On Mon, 16 Nov 2026 12:01:08 -0700 you wrote:

Neither kcm_attach() nor tls_init() rejects the other, so KCM and kTLS RX can be stacked on the same TCP socket in either order. This breaks in several ways:

Quoted 8 lines

- KCM calls read_sock() from its receive callback in atomic context. With kTLS RX this reaches tls_sw_read_sock(), which can sleep.
- Before tls_sw_read_sock() existed, read_sock was tcp_read_sock(), so KCM consumed raw ciphertext from the receive queue that TLS parses.
- Both install socket callbacks. kcm_unattach() restores the pre-KCM sk_data_ready over tls_data_ready, so TLS RX stops getting notified.

[...]

Here is the summary with links:
- [net,v8] kcm: reject TLS on attached TCP sockets

    https://git.kernel.org/netdev/net/c/746dbdc5fa60

You are awesome, thank you!

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