All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Sechang Lim" <rhkrqnwk98@gmail.com>, <bpf@vger.kernel.org>,
	<netdev@vger.kernel.org>
Cc: <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: Re: [PATCH bpf v2 1/2] bpf, sockmap: Reject a ULP on a socket with a psock
Date: Sat, 05 Dec 2026 04:19:48 +0000	[thread overview]
Message-ID: <DNCIDZ692S9D.2D3LOO21BE7PI@gmail.com> (raw)
In-Reply-To: <20261204061110.1929596-2-rhkrqnwk98@gmail.com>

On Thu, Dec 03, 2026 at 10:11 PM Sechang Lim <rhkrqnwk98@gmail.com> wrote:
> @@ -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;

My diff had this bug and the next one, sorry.

TCP clones a listener without lock_sock().
"mptcp" has ->clone, so it passes the TCP_LISTEN check.
Any user can call setsockopt(TCP_ULP, "mptcp") on a plain TCP listener:

  CPU 0                              CPU 1, last ACK of a handshake
  icsk_ulp_ops = &subflow_ulp_ops
                                     tcp_check_req()
                                      inet_csk_clone_lock()
                                       sk_clone_lock() copies the ops
                                       subflow_ulp_clone()
                                        old_ctx is NULL
                                        mptcp_subflow_tcp_fallback()
                                         ctx->tcp_state_change <- oops
  subflow_ulp_init(): -EOPNOTSUPP
  icsk_ulp_ops = NULL

Without the patch icsk_ulp_ops of that listener stays NULL.

No ULP needs a socket that listens already.
mptcp_subflow_create_socket() calls tcp_set_ulp() on a new socket.
Lets refuse TCP_LISTEN for every ULP.
"mptcp" on a listener then returns -ENOTCONN, not -EOPNOTSUPP.
mptcp_connect.c makes that call and checks for -1 only.

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

icsk_ulp_ops of a live socket can go back to NULL now.
getsockopt(TCP_ULP) takes no lock and loads it twice.
The socket is not connected:

  thread 1: setsockopt(TCP_ULP, "tls")   thread 2: getsockopt(TCP_ULP)
  icsk_ulp_ops = &tcp_tls_ulp_ops
                                         if (!icsk->icsk_ulp_ops)
  tls_init(): -ENOTCONN
  icsk_ulp_ops = NULL
                                         icsk->icsk_ulp_ops->name <- NULL

Lets copy the name under rcu_read_lock(), like TCP_CONGESTION does.
tcp_unregister_ulp() calls synchronize_rcu().
So the ops stay valid after module_put() in the error path.

Both fixes are below, against the bpf tree without your patch.

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 80de376..ba38480 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -4609,22 +4609,29 @@ int do_tcp_getsockopt(struct sock *sk, int level,
 			return -EFAULT;
 		return 0;
 	}
-	case TCP_ULP:
+	case TCP_ULP: {
+		const struct tcp_ulp_ops *ulp_ops;
+		char ulp_name[TCP_ULP_NAME_MAX] = {};
+
 		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) {
+
+		/* __tcp_set_ulp() clears icsk_ulp_ops when ->init() fails */
+		rcu_read_lock();
+		ulp_ops = READ_ONCE(icsk->icsk_ulp_ops);
+		if (ulp_ops)
+			memcpy(ulp_name, ulp_ops->name, sizeof(ulp_name));
+		else
 			len = 0;
-			if (copy_to_sockptr(optlen, &len, sizeof(int)))
-				return -EFAULT;
-			return 0;
-		}
+		rcu_read_unlock();
+
 		if (copy_to_sockptr(optlen, &len, sizeof(int)))
 			return -EFAULT;
-		if (copy_to_sockptr(optval, icsk->icsk_ulp_ops->name, len))
+		if (copy_to_sockptr(optval, ulp_name, len))
 			return -EFAULT;
 		return 0;
-
+	}
 	case TCP_FASTOPEN_KEY: {
 		u64 key[TCP_FASTOPEN_KEY_BUF_LENGTH / sizeof(u64)];
 		unsigned int key_len;
diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
index b58045d..a1c17d1 100644
--- a/net/ipv4/tcp_ulp.c
+++ b/net/ipv4/tcp_ulp.c
@@ -144,7 +144,7 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
 		clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);

 	err = -ENOTCONN;
-	if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
+	if (sk->sk_state == TCP_LISTEN)
 		goto out_err;

 	err = ulp_ops->init(sk);

But the fixes to sockmap don't seem to end.
So let's disable it in kconfig.

pw-bot: cr

  parent reply	other threads:[~2026-12-05  4:19 UTC|newest]

Thread overview: 7+ 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 ` [PATCH bpf v2 1/2] " Sechang Lim
2026-12-04  6:32   ` sashiko-bot
2026-12-04  6:45   ` bot+bpf-ci
2026-12-05  4:19   ` Alexei Starovoitov [this message]
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=DNCIDZ692S9D.2D3LOO21BE7PI@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@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=rhkrqnwk98@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.