> 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 <a…@kernel.org> > Signed-off-by: Sechang Lim <rhkr…@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