BPF List
 help / color / mirror / Atom feed
From: Sechang Lim <rhkrqnwk98@gmail.com>
To: bpf@vger.kernel.org, netdev@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@linux.dev, john.fastabend@gmail.com,
	jakub@cloudflare.com, jiayuan.chen@linux.dev, sd@queasysnail.net,
	kuba@kernel.org, edumazet@kernel.org, ncardwell@google.com,
	kuniyu@google.com, pabeni@redhat.com, davem@davemloft.net,
	horms@kernel.org, willemb@google.com, eddyz87@gmail.com,
	memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev,
	jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev
Subject: [PATCH bpf v2 1/2] bpf, sockmap: Reject a ULP on a socket with a psock
Date: Fri,  4 Dec 2026 06:11:01 +0000	[thread overview]
Message-ID: <20261204061110.1929596-2-rhkrqnwk98@gmail.com> (raw)
In-Reply-To: <20261204061110.1929596-1-rhkrqnwk98@gmail.com>

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


  reply	other threads:[~2026-12-04  6:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-04  6:32   ` [PATCH bpf v2 1/2] " 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204061110.1929596-2-rhkrqnwk98@gmail.com \
    --to=rhkrqnwk98@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jakub@cloudflare.com \
    --cc=jiayuan.chen@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox