Skip to contentSkip to conversation

Fixes for TLS and ULP

PATCH In review Geliang Tang 3 participants 8 messages May 30 – Jun 1

Subsystems (4)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

cover Fixes for TLS and ULP

From: Geliang Tang <[email protected]>

This series fixes several pre-existing issues in the TLS/TOE and TCP ULP layers, found during MPTCP KTLS development.

Patches summary:

tls: add clone callback - prevent NULL pointer crash when listening socket carries TLS ULP.

tls: init mixed SW/TOE proto - avoid NULL function pointers on TOE fallback.

tls: fix concurrency in TOE device list iteration - safe two-pass snapshot to eliminate list poisoning crash.

tcp: add socket lock to TCP_ULP getsockopt - fix use-after-free in getsockopt(TCP_ULP).

tcp: fix ULP diag race by RCU - protect diag reads with RCU, paired with release barrier.

All fixes are independent of MPTCP and can be merged separately.

Geliang Tang (5):
tls: add clone callback to prevent NULL crash case
tls: init mixed SW/TOE proto to prevent NULL call
tls: fix concurrency in TOE device list iterations
tcp: add socket lock to TCP_ULP getsockopt safely
tcp: fix ULP diag race by RCU read locks properly

 net/ipv4/tcp.c      | 44 ++++++++++++++++++++-----------
 net/ipv4/tcp_diag.c | 15 ++++++-----
 net/ipv4/tcp_ulp.c  | 14 +++++-----
 net/tls/tls_main.c  | 16 ++++++++++++
 net/tls/tls_toe.c   | 63 ++++++++++++++++++++++++++++++++++++++-------
 5 files changed, 114 insertions(+), 38 deletions(-)
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH 1/5 tls: add clone callback to prevent NULL crash case

From: Geliang Tang <[email protected]>

When a listening socket unintentionally has a TLS ULP attached (e.g., via setsockopt(TCP_ULP, "tls") before accept()), inet_clone_ulp() dereferences ulp_ops->clone. Since tcp_tls_ulp_ops lacked a .clone member, this causes a NULL pointer dereference and kernel panic on accept().

Add tls_clone() that clears the icsk_ulp_data pointer on the newly cloned socket. This prevents the child socket from sharing the parent's TLS context, avoiding use-after-free and double-free issues. The parent's TLS context (used for TOE hardware offload) remains intact, while the child starts without TLS, which is safe because hardware offload handles new connections independently.

This serves as a safety net even though tls_init() normally prevents TLS on listen sockets.

---
 net/tls/tls_main.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
net/tls/tls_main.c +10 -0
index fd39acf41a61..c7ad212c97a0 100644
@@ -1082,6 +1082,15 @@ static int tls_init(struct sock *sk)
 	return rc;
 }
 
+static void tls_clone(const struct request_sock *req,
+		      struct sock *newsk,
+		      const gfp_t priority)
+{
+	struct inet_connection_sock *new_icsk = inet_csk(newsk);
+
+	rcu_assign_pointer(new_icsk->icsk_ulp_data, NULL);
+}
+
 static void tls_update(struct sock *sk, struct proto *p,
 		       void (*write_space)(struct sock *sk))
 {
@@ -1231,6 +1240,7 @@ static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
 	.name			= "tls",
 	.owner			= THIS_MODULE,
 	.init			= tls_init,
+	.clone			= tls_clone,
 	.update			= tls_update,
 	.get_info		= tls_get_info,
 	.get_info_size		= tls_get_info_size,
Signature · 1 line
2.53.0
author reviewer
#2 lore raw

PATCH 2/5 tls: init mixed SW/TOE proto to prevent NULL call

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

When a TOE socket falls back to software offload, update_sk_prot() accesses prot->prots[TLS_SW][TLS_HW_RECORD] and proto_ops[TLS_SW][TLS_HW_RECORD]. These entries were never initialized, causing NULL function pointers and kernel panic. Add the missing entries for [TLS_SW][TLS_HW_RECORD] and [TLS_HW_RECORD][TLS_SW] in both build_protos() and build_proto_ops().

---
 net/tls/tls_main.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/tls/tls_main.c +6 -0
index c7ad212c97a0..3966cda068f9 100644
@@ -965,6 +965,9 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG]
 #endif
 #ifdef CONFIG_TLS_TOE
 	ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
+
+	ops[TLS_SW][TLS_HW_RECORD] = *base;
+	ops[TLS_HW_RECORD][TLS_SW] = *base;
 #endif
 }
 
@@ -1041,6 +1044,9 @@ static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
 	prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
 	prot[TLS_HW_RECORD][TLS_HW_RECORD].hash		= tls_toe_hash;
 	prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash	= tls_toe_unhash;
+
+	prot[TLS_SW][TLS_HW_RECORD] = prot[TLS_SW][TLS_SW];
+	prot[TLS_HW_RECORD][TLS_SW] = prot[TLS_BASE][TLS_SW];
 #endif
 }
 
Signature · 1 line
2.53.0
author reviewer
#3 lore raw

PATCH 3/5 tls: fix concurrency in TOE device list iterations

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

tls_toe_hash() and tls_toe_unhash() iterate over the global device list while temporarily dropping the spinlock to call the device callbacks. If a concurrent tls_toe_unregister_device() removes a device during this window, the list pointers become poisoned (LIST_POISON1/LIST_POISON2), causing a crash when the iteration resumes even if the device object is still referenced via kref.

Fix by replacing the lock-dropping iteration with a safe two-pass
approach: first count the number of devices that need the operation, then allocate a dynamic array, collect device pointers under the lock with a reference increment, and finally call the callbacks after the lock is released. This eliminates the window where the list could be mutated while iteration is in progress.

---
 net/tls/tls_toe.c | 63 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 10 deletions(-)
net/tls/tls_toe.c +53 -10
index 825669e1ab47..c588c702296f 100644
@@ -84,41 +84,84 @@ int tls_toe_bypass(struct sock *sk)
 void tls_toe_unhash(struct sock *sk)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
-	struct tls_toe_device *dev;
+	struct tls_toe_device *dev, **devs;
+	int ndev = 0, i = 0;
+
+	spin_lock_bh(&device_spinlock);
+	list_for_each_entry(dev, &device_list, dev_list) {
+		if (dev->unhash)
+			ndev++;
+	}
+	spin_unlock_bh(&device_spinlock);
+
+	if (!ndev)
+		goto out;
+
+	devs = kmalloc_array(ndev, sizeof(*devs), GFP_KERNEL);
+	if (!devs)
+		goto out;
 
 	spin_lock_bh(&device_spinlock);
 	list_for_each_entry(dev, &device_list, dev_list) {
 		if (dev->unhash) {
 			kref_get(&dev->kref);
-			spin_unlock_bh(&device_spinlock);
-			dev->unhash(dev, sk);
-			kref_put(&dev->kref, dev->release);
-			spin_lock_bh(&device_spinlock);
+			devs[i++] = dev;
 		}
 	}
 	spin_unlock_bh(&device_spinlock);
+
+	for (i = 0; i < ndev; i++) {
+		dev = devs[i];
+		dev->unhash(dev, sk);
+		kref_put(&dev->kref, dev->release);
+	}
+	kfree(devs);
+
+out:
 	ctx->sk_proto->unhash(sk);
 }
 
 int tls_toe_hash(struct sock *sk)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
-	struct tls_toe_device *dev;
+	struct tls_toe_device *dev, **devs;
+	int ndev = 0, i = 0;
 	int err;
 
 	err = ctx->sk_proto->hash(sk);
+	spin_lock_bh(&device_spinlock);
+	list_for_each_entry(dev, &device_list, dev_list) {
+		if (dev->hash)
+			ndev++;
+	}
+	spin_unlock_bh(&device_spinlock);
+
+	if (!ndev)
+		goto out;
+
+	devs = kmalloc_array(ndev, sizeof(*devs), GFP_KERNEL);
+	if (!devs) {
+		err = -ENOMEM;
+		goto out;
+	}
+
 	spin_lock_bh(&device_spinlock);
 	list_for_each_entry(dev, &device_list, dev_list) {
 		if (dev->hash) {
 			kref_get(&dev->kref);
-			spin_unlock_bh(&device_spinlock);
-			err |= dev->hash(dev, sk);
-			kref_put(&dev->kref, dev->release);
-			spin_lock_bh(&device_spinlock);
+			devs[i++] = dev;
 		}
 	}
 	spin_unlock_bh(&device_spinlock);
 
+	for (i = 0; i < ndev; i++) {
+		dev = devs[i];
+		err |= dev->hash(dev, sk);
+		kref_put(&dev->kref, dev->release);
+	}
+	kfree(devs);
+
+out:
 	if (err)
 		tls_toe_unhash(sk);
 	return err;
Signature · 1 line
2.53.0
author reviewer
#4 lore raw

PATCH 4/5 tcp: add socket lock to TCP_ULP getsockopt safely

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

When handling TCP_ULP getsockopt, do_tcp_getsockopt() accesses icsk->icsk_ulp_ops without holding the socket lock. A concurrent connect(AF_UNSPEC) (or other operation that calls tcp_cleanup_ulp()) can set icsk_ulp_ops to NULL and release the module reference. If this occurs between the NULL check and the dereference of ->name, the kernel may access a NULL pointer or a stale pointer, leading to a crash or use-after-free.

Extract the TCP_ULP getsockopt logic into a helper tcp_sock_get_ulp() and wrap the call with lock_sock()/release_sock() to ensure the ULP operation is protected against concurrent modification.

---
 net/ipv4/tcp.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
net/ipv4/tcp.c +29 -15
index a058f350a759..c01f97d5dfe8 100644
@@ -4481,6 +4481,28 @@ struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk,
 	return stats;
 }
 
+static int tcp_sock_get_ulp(struct sock *sk, sockptr_t optval,
+			    sockptr_t optlen)
+{
+	struct inet_connection_sock *icsk = inet_csk(sk);
+	int len;
+
+	if (copy_from_sockptr(&len, optlen, sizeof(int)))
+		return -EFAULT;
+	len = min_t(unsigned int, len, TCP_ULP_NAME_MAX);
+	if (!icsk->icsk_ulp_ops) {
+		len = 0;
+		if (copy_to_sockptr(optlen, &len, sizeof(int)))
+			return -EFAULT;
+		return 0;
+	}
+	if (copy_to_sockptr(optlen, &len, sizeof(int)))
+		return -EFAULT;
+	if (copy_to_sockptr(optval, icsk->icsk_ulp_ops->name, len))
+		return -EFAULT;
+	return 0;
+}
+
 int do_tcp_getsockopt(struct sock *sk, int level,
 		      int optname, sockptr_t optval, sockptr_t optlen)
 {
@@ -4589,22 +4611,14 @@ int do_tcp_getsockopt(struct sock *sk, int level,
 			return -EFAULT;
 		return 0;
 
-	case TCP_ULP:
-		if (copy_from_sockptr(&len, optlen, sizeof(int)))
-			return -EFAULT;
-		len = min_t(unsigned int, len, TCP_ULP_NAME_MAX);
-		if (!icsk->icsk_ulp_ops) {
-			len = 0;
-			if (copy_to_sockptr(optlen, &len, sizeof(int)))
-				return -EFAULT;
-			return 0;
-		}
-		if (copy_to_sockptr(optlen, &len, sizeof(int)))
-			return -EFAULT;
-		if (copy_to_sockptr(optval, icsk->icsk_ulp_ops->name, len))
-			return -EFAULT;
-		return 0;
+	case TCP_ULP: {
+		int err;
 
+		lock_sock(sk);
+		err = tcp_sock_get_ulp(sk, optval, optlen);
+		release_sock(sk);
+		return err;
+	}
 	case TCP_FASTOPEN_KEY: {
 		u64 key[TCP_FASTOPEN_KEY_BUF_LENGTH / sizeof(u64)];
 		unsigned int key_len;
Signature · 1 line
2.53.0
author reviewer
#5 lore raw

PATCH 5/5 tcp: fix ULP diag race by RCU read locks properly

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Protect icsk->icsk_ulp_ops access in TCP diag with RCU read lock and rcu_dereference(). Use smp_store_release() when clearing the pointer in tcp_cleanup_ulp() to pair with readers, preventing use-after-free when a socket is aborted concurrently with a diag dump.

---
 net/ipv4/tcp_diag.c | 15 ++++++++-------
 net/ipv4/tcp_ulp.c  | 14 ++++++++------
 2 files changed, 16 insertions(+), 13 deletions(-)
net/ipv4/tcp_diag.c +8 -7
index ba1fdbe9807f..6580bb8d190a 100644
@@ -133,14 +133,13 @@ static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
 	}
 #endif
 
-	ulp_ops = icsk->icsk_ulp_ops;
-	if (ulp_ops) {
+	rcu_read_lock();
+	ulp_ops = rcu_dereference(icsk->icsk_ulp_ops);
+	if (ulp_ops)
 		err = tcp_diag_put_ulp(skb, sk, ulp_ops, net_admin);
-		if (err < 0)
-			return err;
-	}
+	rcu_read_unlock();
 
-	return 0;
+	return err;
 }
 
 static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
@@ -169,13 +168,15 @@ static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
 	if (sk_fullsock(sk)) {
 		const struct tcp_ulp_ops *ulp_ops;
 
-		ulp_ops = icsk->icsk_ulp_ops;
+		rcu_read_lock();
+		ulp_ops = rcu_dereference(icsk->icsk_ulp_ops);
 		if (ulp_ops) {
 			size += nla_total_size(0) +
 				nla_total_size(TCP_ULP_NAME_MAX);
 			if (ulp_ops->get_info_size)
 				size += ulp_ops->get_info_size(sk, net_admin);
 		}
+		rcu_read_unlock();
 	}
 
 	return size
net/ipv4/tcp_ulp.c +8 -6
index 2aa442128630..aa7fed4ffff2 100644
@@ -112,19 +112,21 @@ void tcp_update_ulp(struct sock *sk, struct proto *proto,
 void tcp_cleanup_ulp(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
+	const struct tcp_ulp_ops *ulp_ops;
 
 	/* No sock_owned_by_me() check here as at the time the
 	 * stack calls this function, the socket is dead and
 	 * about to be destroyed.
 	 */
-	if (!icsk->icsk_ulp_ops)
+	ulp_ops = icsk->icsk_ulp_ops;
+	if (!ulp_ops)
 		return;
 
-	if (icsk->icsk_ulp_ops->release)
-		icsk->icsk_ulp_ops->release(sk);
-	module_put(icsk->icsk_ulp_ops->owner);
-
-	icsk->icsk_ulp_ops = NULL;
+	if (ulp_ops->release)
+		ulp_ops->release(sk);
+	/* Pairs with rcu_dereference() in tcp_diag readers. */
+	smp_store_release(&icsk->icsk_ulp_ops, NULL);
+	module_put(ulp_ops->owner);
 }
 
 static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
Signature · 1 line
2.53.0
#6 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Critical: 1 Call Trace(s) ❌
- KVM Validation: debug (only selftest_mptcp_join): Critical: 19 Call Trace(s) ❌
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Critical: 18 Call Trace(s) ❌
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/26678939084

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/fea5f9852187
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1103235

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

maintainer
#7 lore raw
In reply to Geliang Tang

Hi Geliang,

Quoted 1 line · Geliang Tang

All fixes are independent of MPTCP and can be merged separately.

Then, feel free to send them directly to netdev, for net directly. Don't forget the Fixes tag (and fix the issues reported by the CI and AI tools).

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.