BPF List
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/2] bpf, sockmap: Reject a ULP on a socket with a psock
@ 2026-12-04  6:11 Sechang Lim
  2026-12-04  6:11 ` [PATCH bpf v2 1/2] " Sechang Lim
  2026-12-04  6:11 ` [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap Sechang Lim
  0 siblings, 2 replies; 6+ messages in thread
From: Sechang Lim @ 2026-12-04  6:11 UTC (permalink / raw)
  To: bpf, netdev
  Cc: ast, daniel, andrii, martin.lau, john.fastabend, jakub,
	jiayuan.chen, sd, kuba, edumazet, ncardwell, kuniyu, pabeni,
	davem, horms, willemb, eddyz87, memxor, song, yonghong.song,
	jolsa, emil, ihor.solodrai

A TLS ULP on a socket in a sockmap can make close() spin forever.
Reject the ULP on such sockets and update the selftest.

Changes in v2:
- Reject the ULP in __tcp_set_ulp() instead of keeping the TLS proto.
- Add the selftest patch.

v1: https://lore.kernel.org/bpf/20261130055650.686483-1-rhkrqnwk98@gmail.com/

Sechang Lim (2):
  bpf, sockmap: Reject a ULP on a socket with a psock
  selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap

 include/net/tcp.h                             |  5 ----
 net/ipv4/tcp_bpf.c                            | 16 ++--------
 net/ipv4/tcp_ulp.c                            | 29 +++++++++++-------
 net/tls/tls_main.c                            | 30 -------------------
 .../selftests/bpf/prog_tests/sockmap_ktls.c   | 14 +--------
 5 files changed, 22 insertions(+), 72 deletions(-)

-- 
2.56.1


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

* [PATCH bpf v2 1/2] bpf, sockmap: Reject a ULP on a socket with a psock
  2026-12-04  6:11 [PATCH bpf v2 0/2] bpf, sockmap: Reject a ULP on a socket with a psock Sechang Lim
@ 2026-12-04  6:11 ` Sechang Lim
  2026-12-04  6:32   ` sashiko-bot
  2026-12-04  6:45   ` bot+bpf-ci
  2026-12-04  6:11 ` [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap Sechang Lim
  1 sibling, 2 replies; 6+ messages in thread
From: Sechang Lim @ 2026-12-04  6:11 UTC (permalink / raw)
  To: bpf, netdev
  Cc: ast, daniel, andrii, martin.lau, john.fastabend, jakub,
	jiayuan.chen, sd, kuba, edumazet, ncardwell, kuniyu, pabeni,
	davem, horms, willemb, eddyz87, memxor, song, yonghong.song,
	jolsa, emil, ihor.solodrai

A socket in a sockmap can't get TLS keys since commit 460e6486617c
("tls: reject the combination of TLS and sockmap"), but it can still
get the TLS ULP. When the socket then leaves the map, sk_write_space
is not restored. If the map has an sk_skb program, sk_prot isn't
either, and close() spins in sock_map_close() forever.

Reject the ULP with -EBUSY on a socket with a psock. A BPF program can
add the socket to a sockmap without lock_sock(), so set icsk_ulp_ops
before ->init(), under sk_callback_lock and only if there is no psock.
sk_psock_init() checks for a ULP under the same lock.

Remove tls_update(), tcp_update_ulp() and the psock check in TLS,
which are now dead.

Fixes: e34a07c0ae39 ("sock: redo the psock vs ULP protection check")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
---
 include/net/tcp.h  |  5 -----
 net/ipv4/tcp_bpf.c | 16 +++-------------
 net/ipv4/tcp_ulp.c | 29 ++++++++++++++++++-----------
 net/tls/tls_main.c | 30 ------------------------------
 4 files changed, 21 insertions(+), 59 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index c5348b358290..d2a791810e65 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2881,9 +2881,6 @@ struct tcp_ulp_ops {
 
 	/* initialize ulp */
 	int (*init)(struct sock *sk);
-	/* update ulp */
-	void (*update)(struct sock *sk, struct proto *p,
-		       void (*write_space)(struct sock *sk));
 	/* cleanup ulp */
 	void (*release)(struct sock *sk);
 	/* diagnostic */
@@ -2901,8 +2898,6 @@ void tcp_unregister_ulp(struct tcp_ulp_ops *type);
 int tcp_set_ulp(struct sock *sk, const char *name);
 void tcp_get_available_ulp(char *buf, size_t len);
 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)
 {
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 788ce08eef4d..2fd10b003ec3 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -794,19 +794,9 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
 	}
 
 	if (restore) {
-		if (inet_csk_has_ulp(sk)) {
-			/* TLS does not have an unhash proto in SW cases,
-			 * but we need to ensure we stop using the sock_map
-			 * unhash routine because the associated psock is being
-			 * removed. So use the original unhash handler.
-			 */
-			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
-			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
-		} else {
-			WRITE_ONCE(sk->sk_write_space, psock->saved_write_space);
-			/* Pairs with lockless read in sk_clone_lock() */
-			sock_replace_proto(sk, psock->sk_proto);
-		}
+		WRITE_ONCE(sk->sk_write_space, psock->saved_write_space);
+		/* Pairs with lockless read in sk_clone_lock() */
+		sock_replace_proto(sk, psock->sk_proto);
 		return 0;
 	}
 
diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
index b58045df101e..b50ca0576352 100644
--- a/net/ipv4/tcp_ulp.c
+++ b/net/ipv4/tcp_ulp.c
@@ -12,6 +12,7 @@
 #include <linux/types.h>
 #include <linux/list.h>
 #include <linux/gfp.h>
+#include <linux/skmsg.h>
 #include <net/tcp.h>
 
 static DEFINE_SPINLOCK(tcp_ulp_list_lock);
@@ -100,15 +101,6 @@ void tcp_get_available_ulp(char *buf, size_t maxlen)
 	rcu_read_unlock();
 }
 
-void tcp_update_ulp(struct sock *sk, struct proto *proto,
-		    void (*write_space)(struct sock *sk))
-{
-	struct inet_connection_sock *icsk = inet_csk(sk);
-
-	if (icsk->icsk_ulp_ops->update)
-		icsk->icsk_ulp_ops->update(sk, proto, write_space);
-}
-
 void tcp_cleanup_ulp(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
@@ -147,11 +139,26 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
 	if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
 		goto out_err;
 
-	err = ulp_ops->init(sk);
+	/* A ULP and a psock exclude each other. sk_psock_init() checks
+	 * icsk_ulp_ops and sets the psock under the same lock.
+	 */
+	err = -EBUSY;
+	write_lock_bh(&sk->sk_callback_lock);
+	rcu_read_lock();
+	if (!sk_psock(sk)) {
+		icsk->icsk_ulp_ops = ulp_ops;
+		err = 0;
+	}
+	rcu_read_unlock();
+	write_unlock_bh(&sk->sk_callback_lock);
 	if (err)
 		goto out_err;
 
-	icsk->icsk_ulp_ops = ulp_ops;
+	err = ulp_ops->init(sk);
+	if (err) {
+		icsk->icsk_ulp_ops = NULL;
+		goto out_err;
+	}
 	return 0;
 out_err:
 	module_put(ulp_ops->owner);
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index d24e86e7f42d..ac729b569180 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -631,17 +631,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
 	int rc = 0;
 	int conf;
 
-	/* TLS and sockmap are mutually exclusive. A socket already in a
-	 * sockmap (i.e. with a psock attached) cannot be upgraded to TLS.
-	 * sockmap rejects TLS sockets already (see sk_psock_init()).
-	 */
-	rcu_read_lock();
-	if (sk_psock(sk)) {
-		rcu_read_unlock();
-		return -EINVAL;
-	}
-	rcu_read_unlock();
-
 	if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
 		return -EINVAL;
 
@@ -1087,24 +1076,6 @@ static int tls_init(struct sock *sk)
 	return rc;
 }
 
-static void tls_update(struct sock *sk, struct proto *p,
-		       void (*write_space)(struct sock *sk))
-{
-	struct tls_context *ctx;
-
-	WARN_ON_ONCE(sk->sk_prot == p);
-
-	ctx = tls_get_ctx(sk);
-	if (likely(ctx)) {
-		ctx->sk_write_space = write_space;
-		ctx->sk_proto = p;
-	} else {
-		/* Pairs with lockless read in sk_clone_lock(). */
-		WRITE_ONCE(sk->sk_prot, p);
-		sk->sk_write_space = write_space;
-	}
-}
-
 static u16 tls_user_config(struct tls_context *ctx, bool tx)
 {
 	u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
@@ -1234,7 +1205,6 @@ static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
 	.name			= "tls",
 	.owner			= THIS_MODULE,
 	.init			= tls_init,
-	.update			= tls_update,
 	.get_info		= tls_get_info,
 	.get_info_size		= tls_get_info_size,
 };
-- 
2.56.1


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

* [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap
  2026-12-04  6:11 [PATCH bpf v2 0/2] bpf, sockmap: Reject a ULP on a socket with a psock Sechang Lim
  2026-12-04  6:11 ` [PATCH bpf v2 1/2] " Sechang Lim
@ 2026-12-04  6:11 ` Sechang Lim
  2026-12-04  6:29   ` sashiko-bot
  1 sibling, 1 reply; 6+ messages in thread
From: Sechang Lim @ 2026-12-04  6:11 UTC (permalink / raw)
  To: bpf, netdev
  Cc: ast, daniel, andrii, martin.lau, john.fastabend, jakub,
	jiayuan.chen, sd, kuba, edumazet, ncardwell, kuniyu, pabeni,
	davem, horms, willemb, eddyz87, memxor, song, yonghong.song,
	jolsa, emil, ihor.solodrai

setsockopt(TCP_ULP) now fails on a socket in a sockmap. Check that
instead of setsockopt(TLS_TX).

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
---
 .../selftests/bpf/prog_tests/sockmap_ktls.c        | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
index 34737e8df6ea..749b41b743df 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
@@ -118,12 +118,6 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map
 
 static void test_sockmap_ktls_enable_fails_when_in_sockmap(int family, int map)
 {
-	struct tls12_crypto_info_aes_gcm_128 crypto = {
-		.info = {
-			.version     = TLS_1_2_VERSION,
-			.cipher_type = TLS_CIPHER_AES_GCM_128,
-		},
-	};
 	struct sockaddr_storage addr = {};
 	socklen_t len = sizeof(addr);
 	struct sockaddr_in6 *v6;
@@ -165,14 +159,8 @@ static void test_sockmap_ktls_enable_fails_when_in_sockmap(int family, int map)
 	if (!ASSERT_OK(err, "sockmap update elem"))
 		goto close;
 
-	/* Installing the TLS ULP is allowed, it does not touch the datapath. */
 	err = setsockopt(s, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls"));
-	if (!ASSERT_OK(err, "setsockopt(TCP_ULP)"))
-		goto close;
-
-	/* Enabling the TLS crypto datapath must be rejected. */
-	err = setsockopt(s, SOL_TLS, TLS_TX, &crypto, sizeof(crypto));
-	ASSERT_ERR(err, "setsockopt(TLS_TX)");
+	ASSERT_ERR(err, "setsockopt(TCP_ULP)");
 
 close:
 	close(s);
-- 
2.56.1


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

* Re: [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap
  2026-12-04  6:11 ` [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap Sechang Lim
@ 2026-12-04  6:29   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-04  6:29 UTC (permalink / raw)
  To: Sechang Lim, bpf

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] net: NULL dereference in mptcp_subflow_tcp_fallback() on
  TCP_LISTEN ULP race
- [Critical] net: NULL dereference in do_tcp_getsockopt() on
  __tcp_set_ulp() init failure
- [Medium] net: module UAF in tcp_diag and getsockopt after
  __tcp_set_ulp() init failure
- [Low] selftests/bpf: missing EBUSY errno check on setsockopt(TCP_ULP)
  failure
--

commit 465bb4ab46ca075f14fde9c7bc7c50ceeb8bf320
Author: Sechang Lim <rhkrqnwk98@gmail.com>

bpf, sockmap: Reject a ULP on a socket with a psock

This commit rejects setting a TCP ULP with -EBUSY when a psock is attached by
assigning icsk->icsk_ulp_ops under sk_callback_lock before calling
ulp_ops->init(). It also removes the now-unused tcp_update_ulp(), tls_update(),
and the psock check in TLS.

> diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
> index b58045df101e5..b50ca05763525 100644
> --- a/net/ipv4/tcp_ulp.c
> +++ b/net/ipv4/tcp_ulp.c

[ ... ]

> @@ -147,11 +139,26 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
>  	if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
>  		goto out_err;
>  
> -	err = ulp_ops->init(sk);
> +	/* A ULP and a psock exclude each other. sk_psock_init() checks
> +	 * icsk_ulp_ops and sets the psock under the same lock.
> +	 */
> +	err = -EBUSY;
> +	write_lock_bh(&sk->sk_callback_lock);
> +	rcu_read_lock();
> +	if (!sk_psock(sk)) {
> +		icsk->icsk_ulp_ops = ulp_ops;
> +		err = 0;
> +	}
> +	rcu_read_unlock();
> +	write_unlock_bh(&sk->sk_callback_lock);
>  	if (err)
>  		goto out_err;

[Severity: Critical]
Can publishing icsk->icsk_ulp_ops before ulp_ops->init() cause a NULL pointer
dereference in mptcp_subflow_tcp_fallback() on a listening socket?

Because subflow_ulp_ops defines .clone, an unprivileged
setsockopt(IPPROTO_TCP, TCP_ULP, "mptcp") on a TCP_LISTEN socket passes the
check above, publishes icsk->icsk_ulp_ops while icsk->icsk_ulp_data is still
NULL, and then fails in subflow_ulp_init() with -EOPNOTSUPP.

CPU0                            CPU1 (<softirq>)
__tcp_set_ulp()
  icsk->icsk_ulp_ops = ulp_ops
                                tcp_v4_rcv() -> ...
                                  inet_csk_clone_lock()
                                    sk_clone_lock()
                                    inet_clone_ulp()
                                      subflow_ulp_clone()
                                        mptcp_subflow_tcp_fallback(NULL)
                                          <-- NULL pointer dereference
  subflow_ulp_init() -> err
  icsk->icsk_ulp_ops = NULL

An incoming ACK completing a handshake clones the listener without taking
lock_sock() or sk_callback_lock, copies the transient subflow_ulp_ops with
NULL icsk_ulp_data, and panics the kernel in softirq when
mptcp_subflow_tcp_fallback() dereferences ctx->tcp_state_change.

> -	icsk->icsk_ulp_ops = ulp_ops;
> +	err = ulp_ops->init(sk);
> +	if (err) {
> +		icsk->icsk_ulp_ops = NULL;
> +		goto out_err;
> +	}

[Severity: Critical]
Can clearing icsk->icsk_ulp_ops to NULL on init failure cause a NULL pointer
dereference in do_tcp_getsockopt()?

do_tcp_getsockopt() for TCP_ULP runs without lock_sock() or sk_callback_lock,
checks icsk->icsk_ulp_ops, writes optlen to userspace (which can fault and
sleep), and then reloads icsk->icsk_ulp_ops->name.

CPU0                            CPU1
__tcp_set_ulp()                 do_tcp_getsockopt(TCP_ULP)
  icsk->icsk_ulp_ops = ulp_ops
                                  if (!icsk->icsk_ulp_ops) /* false */
                                  copy_to_sockptr(optlen, ...)
  tls_init() -> -ENOTCONN
  icsk->icsk_ulp_ops = NULL
                                  copy_to_sockptr(optval,
                                    icsk->icsk_ulp_ops->name, ...)
                                    <-- NULL pointer dereference

An unprivileged user can trigger this by racing setsockopt(TCP_ULP, "tls") on
an unconnected socket against getsockopt(TCP_ULP), crashing the kernel.

Additionally, this plain store runs outside sk_callback_lock and without
WRITE_ONCE(), creating a data race with sk_psock_init() checking
inet_csk_has_ulp() under sk_callback_lock as well as lockless readers.

>  	return 0;
>  out_err:
>  	module_put(ulp_ops->owner);

[Severity: Medium]
Can dropping the module reference immediately on init failure cause a
use-after-free in tcp_diag or do_tcp_getsockopt()?

tcp_diag_get_aux(), tcp_diag_get_aux_size(), and do_tcp_getsockopt(TCP_ULP)
read icsk->icsk_ulp_ops without lock_sock(), sk_callback_lock, a module
reference, or rcu_read_lock().

CPU0                            CPU1
__tcp_set_ulp()                 tcp_diag_get_aux_size()
  icsk->icsk_ulp_ops = ulp_ops
                                  ulp_ops = icsk->icsk_ulp_ops
  tls_init() -> -ENOTCONN
  icsk->icsk_ulp_ops = NULL
  module_put(ulp_ops->owner)
rmmod tls
  tcp_unregister_ulp()
    synchronize_rcu()
  /* module text freed */
                                  ulp_ops->get_info_size(...)
                                    <-- use-after-free

Because these readers are not inside rcu_read_lock(), tcp_unregister_ulp()'s
synchronize_rcu() during rmmod does not wait for them, allowing the unloaded
module's rodata and callbacks to be freed while still in use.

commit 87d52e3d519ca688662dddf41b1acf31f697108b
Author: Sechang Lim <rhkrqnwk98@gmail.com>

selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap

This commit updates test_sockmap_ktls_enable_fails_when_in_sockmap() to expect
setsockopt(TCP_ULP) to fail directly instead of setsockopt(TLS_TX).

> diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
> index 34737e8df6ea1..749b41b743df7 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c

[ ... ]

> @@ -165,14 +159,8 @@ static void test_sockmap_ktls_enable_fails_when_in_sockmap(int family, int map)
>  	if (!ASSERT_OK(err, "sockmap update elem"))
>  		goto close;
>  
> -	/* Installing the TLS ULP is allowed, it does not touch the datapath. */
>  	err = setsockopt(s, IPPROTO_TCP, TCP_ULP, "tls", strlen("tls"));
> -	if (!ASSERT_OK(err, "setsockopt(TCP_ULP)"))
> -		goto close;
> -
> -	/* Enabling the TLS crypto datapath must be rejected. */
> -	err = setsockopt(s, SOL_TLS, TLS_TX, &crypto, sizeof(crypto));
> -	ASSERT_ERR(err, "setsockopt(TLS_TX)");
> +	ASSERT_ERR(err, "setsockopt(TCP_ULP)");

[Severity: Low]
Should this also assert errno == EBUSY after setsockopt(TCP_ULP) fails?

When CONFIG_TLS is not enabled or the tls module cannot be loaded,
tcp_set_ulp() returns -ENOENT before reaching __tcp_set_ulp(), so a bare
ASSERT_ERR() silently passes without testing the psock rejection path.
Checking ASSERT_EQ(errno, EBUSY, ...) ensures the failure came from the psock
check.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204061110.1929596-1-rhkrqnwk98@gmail.com?part=2

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

* Re: [PATCH bpf v2 1/2] bpf, sockmap: Reject a ULP on a socket with a psock
  2026-12-04  6:11 ` [PATCH bpf v2 1/2] " Sechang Lim
@ 2026-12-04  6:32   ` sashiko-bot
  2026-12-04  6:45   ` bot+bpf-ci
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-04  6:32 UTC (permalink / raw)
  To: Sechang Lim, bpf

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] tcp: premature icsk_ulp_ops publish crashes
  subflow_ulp_clone() on listeners
- [Critical] tcp: icsk_ulp_ops rollback causes NULL deref and UAF in
  do_tcp_getsockopt()
- [Medium] tcp: early icsk_ulp_ops publish causes module UAF and stale
  state in tcp_diag
- [Medium] tcp: unlocked icsk_ulp_ops reset in __tcp_set_ulp() races
  with sk_psock_init()
- [Low] bpf, sockmap: Fixes tag cites e34a07c0ae39 instead of
  460e6486617c
--

commit 465bb4ab46ca075f14fde9c7bc7c50ceeb8bf320
Author: Sechang Lim <rhkrqnwk98@gmail.com>

> bpf, sockmap: Reject a ULP on a socket with a psock
>
> A socket in a sockmap can't get TLS keys since commit 460e6486617c
> ("tls: reject the combination of TLS and sockmap"), but it can still
> get the TLS ULP. When the socket then leaves the map, sk_write_space
> is not restored. If the map has an sk_skb program, sk_prot isn't
> either, and close() spins in sock_map_close() forever.
>
> Reject the ULP with -EBUSY on a socket with a psock. A BPF program can
> add the socket to a sockmap without lock_sock(), so set icsk_ulp_ops
> before ->init(), under sk_callback_lock and only if there is no psock.
> sk_psock_init() checks for a ULP under the same lock.
>
> Remove tls_update(), tcp_update_ulp() and the psock check in TLS,
> which are now dead.
>
> Fixes: e34a07c0ae39 ("sock: redo the psock vs ULP protection check")
> Suggested-by: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>

[Severity: Low]
Should the Fixes tag reference 460e6486617c ("tls: reject the combination of
TLS and sockmap") instead of e34a07c0ae39 ("sock: redo the psock vs ULP
protection check")?

That 2022 commit only moved the inet_csk_has_ulp() check back into
sk_psock_init().  This patch replaces the sk_psock() check added by
460e6486617c ("tls: reject the combination of TLS and sockmap") and removes
tls_update() and tcp_update_ulp(), so citing the older commit queues it for
stable branches where TLS and sockmap are still allowed together.

[ ... ]

> diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
> index b58045df101e5..b50ca05763525 100644
> --- a/net/ipv4/tcp_ulp.c
> +++ b/net/ipv4/tcp_ulp.c

[ ... ]

> @@ -147,11 +139,26 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
>  	if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
>  		goto out_err;
>  
> -	err = ulp_ops->init(sk);
> +	/* A ULP and a psock exclude each other. sk_psock_init() checks
> +	 * icsk_ulp_ops and sets the psock under the same lock.
> +	 */
> +	err = -EBUSY;
> +	write_lock_bh(&sk->sk_callback_lock);
> +	rcu_read_lock();
> +	if (!sk_psock(sk)) {
> +		icsk->icsk_ulp_ops = ulp_ops;
> +		err = 0;
> +	}
> +	rcu_read_unlock();
> +	write_unlock_bh(&sk->sk_callback_lock);
>  	if (err)
>  		goto out_err;

[Severity: Critical]
Can publishing icsk->icsk_ulp_ops before ulp_ops->init() crash
subflow_ulp_clone() with a NULL pointer dereference on a listening socket?

subflow_ulp_ops ("mptcp") sets .clone = subflow_ulp_clone, so
setsockopt(TCP_ULP, "mptcp") on an ordinary userspace TCP_LISTEN socket
passes the TCP_LISTEN check and publishes icsk->icsk_ulp_ops with
icsk->icsk_ulp_data still NULL before subflow_ulp_init() rejects
!sk->sk_kern_sock.  Handshake completion runs locklessly in softirq:

CPU0                                   CPU1 (<softirq>)
__tcp_set_ulp()
  icsk->icsk_ulp_ops = &subflow_ulp_ops
                                       tcp_v4_rcv() -> ...
                                         inet_csk_clone_lock()
                                           sk_clone_lock()
                                           inet_clone_ulp()
                                             subflow_ulp_clone()
                                               mptcp_subflow_tcp_fallback()
                                                 ctx->tcp_state_change
                                                 <-- NULL pointer dereference
  subflow_ulp_init() -> -EOPNOTSUPP

Because CONFIG_MPTCP is built in by default, an unprivileged local user can
trigger this Oops in softirq context.

> -	icsk->icsk_ulp_ops = ulp_ops;
> +	err = ulp_ops->init(sk);
> +	if (err) {
> +		icsk->icsk_ulp_ops = NULL;
> +		goto out_err;
> +	}

[Severity: Medium]
Should resetting icsk->icsk_ulp_ops to NULL also hold sk->sk_callback_lock?

sk_psock_init() runs without lock_sock(sk) and reads icsk->icsk_ulp_ops via
inet_csk_has_ulp(sk) under write_lock_bh(&sk->sk_callback_lock).  This
unlocked plain store races with that read, breaking the sk_callback_lock
synchronization added above.

>  	return 0;
>  out_err:
>  	module_put(ulp_ops->owner);

[Severity: Critical]
Can rolling back icsk->icsk_ulp_ops on init() failure cause a NULL pointer
dereference or use-after-free in do_tcp_getsockopt(TCP_ULP)?

tcp_getsockopt() calls do_tcp_getsockopt() without lock_sock(sk),
sk_callback_lock, or rcu_read_lock().  Previously, icsk->icsk_ulp_ops never
transitioned from non-NULL back to NULL on a live socket, nor dropped its
module reference before socket destruction.

CPU0                                CPU1
__tcp_set_ulp()
  icsk->icsk_ulp_ops = ulp_ops
                                    do_tcp_getsockopt(TCP_ULP)
                                      if (!icsk->icsk_ulp_ops) // false
                                      copy_to_sockptr(optlen, ...) // blocks
  ulp_ops->init(sk) -> -ENOTCONN
  icsk->icsk_ulp_ops = NULL
  module_put(ulp_ops->owner)
                                      copy_to_sockptr(...,
                                        icsk->icsk_ulp_ops->name, len)
                                        <-- NULL pointer dereference

An unprivileged local user can hit this on an unconnected TCP socket; on
architectures without exception fixups on kernel-source loads in
copy_to_user() (such as arm64), or under KASAN/HARDENED_USERCOPY, passing
NULL->name causes an unhandled kernel Oops.  Even if icsk_ulp_ops were
loaded once, module_put(ulp_ops->owner) allows a modular ULP such as tls.ko
to be unloaded while CPU1 is blocked, causing a use-after-free.

[Severity: Medium]
Does early publication of icsk->icsk_ulp_ops followed by module_put() on
init() failure also cause a module use-after-free in tcp_diag?

tcp_diag_dump_one() and tcp_diag_dump() call tcp_diag_get_aux() and
tcp_diag_get_aux_size() without lock_sock(sk), sk_callback_lock, or
rcu_read_lock().  Because tcp_unregister_ulp()'s synchronize_rcu() does not
wait for these readers, a concurrent rmmod tls after a failing
setsockopt(TCP_ULP, "tls") on a hashed socket can free tcp_tls_ulp_ops and
module text while tcp_diag is still using them:

CPU0                                CPU1
__tcp_set_ulp()
  icsk->icsk_ulp_ops = ulp_ops
                                    tcp_diag_dump_one() -> ...
                                      tcp_diag_get_aux()
                                        ulp_ops = icsk->icsk_ulp_ops
  tls_init(sk) -> -ENOTCONN
  icsk->icsk_ulp_ops = NULL
  module_put(ulp_ops->owner)
  rmmod tls (frees tls.ko)
                                        ulp_ops->get_info(sk, ...)
                                        <-- use-after-free

Even without module unload, tls_ctx_create() sets icsk_ulp_data before
tls_init() initializes ctx->tx_conf, ctx->rx_conf, and
ctx->tx_max_payload_len, so concurrent tls_get_info() can export those
fields as 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204061110.1929596-1-rhkrqnwk98@gmail.com?part=1

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

* Re: [PATCH bpf v2 1/2] bpf, sockmap: Reject a ULP on a socket with a psock
  2026-12-04  6:11 ` [PATCH bpf v2 1/2] " Sechang Lim
  2026-12-04  6:32   ` sashiko-bot
@ 2026-12-04  6:45   ` bot+bpf-ci
  1 sibling, 0 replies; 6+ messages in thread
From: bot+bpf-ci @ 2026-12-04  6:45 UTC (permalink / raw)
  To: rhkrqnwk98, bpf, netdev
  Cc: ast, daniel, andrii, martin.lau, john.fastabend, jakub,
	jiayuan.chen, sd, kuba, edumazet, ncardwell, kuniyu, pabeni,
	davem, horms, willemb, eddyz87, memxor, song, yonghong.song,
	jolsa, emil, ihor.solodrai, ast, andrii, daniel, martin.lau,
	eddyz87, yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 5434 bytes --]

> bpf, sockmap: Reject a ULP on a socket with a psock

TL;DR:
- Publishing icsk_ulp_ops before init may let a listener clone of the mptcp
  ULP hit a NULL pointer dereference in softirq
- Fixes: tag looks wrong

> diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
> --- a/net/ipv4/tcp_ulp.c
> +++ b/net/ipv4/tcp_ulp.c

[ ... ]

> @@ -147,11 +139,26 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
>  	if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
>  		goto out_err;
>  
> -	err = ulp_ops->init(sk);
> +	/* A ULP and a psock exclude each other. sk_psock_init() checks
> +	 * icsk_ulp_ops and sets the psock under the same lock.
> +	 */
> +	err = -EBUSY;
> +	write_lock_bh(&sk->sk_callback_lock);
> +	rcu_read_lock();
> +	if (!sk_psock(sk)) {
> +		icsk->icsk_ulp_ops = ulp_ops;
> +		err = 0;
> +	}
> +	rcu_read_unlock();
> +	write_unlock_bh(&sk->sk_callback_lock);
>  	if (err)
>  		goto out_err;
>  
> -	icsk->icsk_ulp_ops = ulp_ops;
> +	err = ulp_ops->init(sk);
> +	if (err) {
> +		icsk->icsk_ulp_ops = NULL;
> +		goto out_err;
> +	}
>  	return 0;
>  out_err:
>  	module_put(ulp_ops->owner);

Can a listener clone observe icsk_ulp_ops while icsk_ulp_data is still
NULL here?  icsk_ulp_ops is now published before ulp_ops->init() runs, and
it is cleared again if init fails.  Previously it was only stored after a
successful init.

The listener clone path runs in softirq without taking any lock on the
listener:

tcp_v4_rcv() (TCP_NEW_SYN_RECV)
  tcp_check_req()
    tcp_v4_syn_recv_sock()
      tcp_create_openreq_child()
        inet_csk_clone_lock()
          sk_clone()   /* copies icsk_ulp_ops and icsk_ulp_data */
            inet_clone_ulp():

	if (!icsk->icsk_ulp_ops)
		return;

	icsk->icsk_ulp_ops->clone(req, newsk, priority);

This looks reachable with the built-in "mptcp" ULP.  subflow_ulp_ops has
.clone, so the "!ulp_ops->clone && sk->sk_state == TCP_LISTEN" check lets
it through on a listening socket.  subflow_ulp_init() then fails with
-EOPNOTSUPP because the socket is not a kernel socket:

	if (!sk->sk_kern_sock)

In the window before that failure, icsk_ulp_ops == &subflow_ulp_ops while
icsk_ulp_data is NULL.  A connection that completes on that listener in
the window gets a child with those values, and subflow_ulp_clone() runs:

	struct mptcp_subflow_context *old_ctx = mptcp_subflow_ctx(newsk);   /* NULL */
	if (!tcp_rsk(req)->is_mptcp || ...) {   /* is_mptcp = 0 from tcp_conn_request() */
		subflow_ulp_fallback(newsk, old_ctx);

mptcp_subflow_tcp_fallback() then dereferences ctx->tcp_state_change, which
looks like a NULL pointer dereference in softirq context.

A possible trigger is, on a CONFIG_MPTCP=y kernel, looping
setsockopt(lfd, IPPROTO_TCP, TCP_ULP, "mptcp") on a userspace TCP listener
while connecting to it.  The same clone happens for syncookie and TFO
children, and write_unlock_bh() can run pending NET_RX softirqs on the same
CPU inside the window.  Before this patch, a failed init never made the
mptcp ops visible.

The commit message explains why the ops must be set before ->init(), but
other lockless readers of icsk_ulp_ops rely on a non-NULL ops implying an
initialized ULP.  Would it work to refuse clone-capable ULPs on a listener
before publishing, to publish under sk_callback_lock only after init and
recheck for a psock there, or to make the clone path tolerate a NULL ctx?
The later commit 343621ae291f in this series only updates selftests and
does not address this.

> bpf, sockmap: Reject a ULP on a socket with a psock
>
> A socket in a sockmap can't get TLS keys since commit 460e6486617c
> ("tls: reject the combination of TLS and sockmap"), but it can still
> get the TLS ULP. When the socket then leaves the map, sk_write_space
> is not restored. If the map has an sk_skb program, sk_prot isn't
> either, and close() spins in sock_map_close() forever.
> Reject the ULP with -EBUSY on a socket with a psock. A BPF program can
> add the socket to a sockmap without lock_sock(), so set icsk_ulp_ops
> before ->init(), under sk_callback_lock and only if there is no psock.
> sk_psock_init() checks for a ULP under the same lock.
> Remove tls_update(), tcp_update_ulp() and the psock check in TLS,
> which are now dead.
> Fixes: e34a07c0ae39 ("sock: redo the psock vs ULP protection check")
> Suggested-by: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>

The Fixes: tag points at e34a07c0ae39 ("sock: redo the psock vs ULP
protection check"), but that commit added the inet_csk_has_ulp() check in
sk_psock_init() for the ULP-then-psock order, plus a WARN_ON_ONCE in
tls_update().

The bug fixed here is the opposite order, where a socket already has a
psock and then gets the TLS ULP via setsockopt(TCP_ULP).  Was that path
introduced or changed by e34a07c0ae39?

The commit message ties the problem to 460e6486617c ("tls: reject the
combination of TLS and sockmap"), which also does not look like an obvious
Fixes: target.  The tls_update() ctx branch appears to date to 95fa145479fb
("bpf: sockmap/tls, close can race with map free").  Could the Fixes: tag
be re-checked against the commit that introduced the psock-then-ULP
restore problem?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47281082845

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

end of thread, other threads:[~2026-12-04  6:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  6:11 [PATCH bpf v2 0/2] bpf, sockmap: Reject a ULP on a socket with a psock Sechang Lim
2026-12-04  6:11 ` [PATCH bpf v2 1/2] " Sechang Lim
2026-12-04  6:32   ` sashiko-bot
2026-12-04  6:45   ` bot+bpf-ci
2026-12-04  6:11 ` [PATCH bpf v2 2/2] selftests/bpf: Expect TCP_ULP to fail on a socket in a sockmap Sechang Lim
2026-12-04  6:29   ` sashiko-bot

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