On Thu, Dec 03, 2026 at 10:11 PM Sechang Lim <rhkr…@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