tcp_ca_dst_init() drops the reference to the old CA and installs the route's CA without calling the old release callback or clearing icsk_ca_priv. If icsk_ca_initialized is set, tcp_init_transfer() skips initialization of the new CA. When the route selects CDG, tcp_cdg_release() can then free a pointer CDG never allocated. Run tcp_cleanup_congestion_control() and clear icsk_ca_priv before installing different ops. If the route selects the current ops, drop only the extra reference and keep the socket's state. Fixes: 81164413ad09 ("net: tcp: add per route congestion control") Reported-by: Alexei Starovoitov Link: https://lore.kernel.org/netdev/DNASOXUN4KAE.1IJL3LGF4E1DO@gmail.com/ Cc: stable@vger.kernel.org Signed-off-by: Paul Moses --- net/ipv4/tcp_output.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 043838d99640..fd78a35edb85 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -4159,9 +4159,15 @@ static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst) rcu_read_lock(); ca = tcp_ca_find_key(ca_key); if (likely(ca && bpf_try_module_get(ca, ca->owner))) { - bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); + if (ca == icsk->icsk_ca_ops) { + bpf_module_put(ca, ca->owner); + } else { + tcp_cleanup_congestion_control(sk); + memset(icsk->icsk_ca_priv, 0, + sizeof(icsk->icsk_ca_priv)); + WRITE_ONCE(icsk->icsk_ca_ops, ca); + } icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); - WRITE_ONCE(icsk->icsk_ca_ops, ca); } rcu_read_unlock(); } -- 2.56.0.lol.gf7de81456940 tcp_create_openreq_child() copies icsk_ca_priv and icsk_ca_initialized from the listener. tcp_ca_openreq_child() can select a different CA for the child without clearing either field. If the child uses CDG, tcp_cdg_init() allocates a new gradients array but leaves the copied tail index in place. That index can be outside the new array. Clear icsk_ca_priv and reset icsk_ca_initialized before selecting the child's CA and calling tcp_set_ca_state(). Do not run the listener's release callback on the copy. Fixes: 81164413ad09 ("net: tcp: add per route congestion control") Reported-by: Alexei Starovoitov Link: https://lore.kernel.org/netdev/DNASOXUN4KAE.1IJL3LGF4E1DO@gmail.com/ Cc: stable@vger.kernel.org Signed-off-by: Paul Moses --- Backport prereq: apply Jiayuan Chen's clone-hook ordering fix first, so the child cannot already own CA state at the reset below: https://lore.kernel.org/bpf/20261130041156.295524-1-jiayuan.chen@linux.dev/ net/ipv4/tcp_minisocks.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c index 934012cdac27..2f9f84bf8768 100644 --- a/net/ipv4/tcp_minisocks.c +++ b/net/ipv4/tcp_minisocks.c @@ -500,6 +500,9 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst) u32 ca_key = dst_metric(dst, RTAX_CC_ALGO); bool ca_got_dst = false; + icsk->icsk_ca_initialized = 0; + memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv)); + if (ca_key != TCP_CA_UNSPEC) { const struct tcp_congestion_ops *ca; -- 2.56.0.lol.gf7de81456940 sol_tcp_sockopt_congestion() checks the name in optval and rejects "cdg". do_tcp_setsockopt() then copies the name from optval for lookup. If optval points into a shared BPF map, an update between the check and the copy can select CDG even though the check passed. Copy the name into a bounded local buffer and use that copy for both the CDG check and the TCP_CONGESTION request. Keep the check before the recursion guard so a request for CDG still returns -ENOTSUPP. Fixes: eb18b49ea758 ("bpf: tcp: Allow bpf-tcp-cc to call bpf_(get|set)sockopt") Cc: stable@vger.kernel.org Signed-off-by: Paul Moses --- net/core/filter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index b7d38b075ef3..87d4191339b5 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5692,8 +5692,9 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname, static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, int *optlen, bool getopt) { + char name[TCP_CA_NAME_MAX]; struct tcp_sock *tp; - int ret; + int len, ret; if (*optlen < 2) return -EINVAL; @@ -5708,11 +5709,15 @@ static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, KERNEL_SOCKPTR(optlen)); } + len = min_t(int, *optlen, sizeof(name) - 1); + memcpy(name, optval, len); + name[len] = '\0'; + /* "cdg" is the only cc that alloc a ptr * in inet_csk_ca area. The bpf-tcp-cc may * overwrite this ptr after switching to cdg. */ - if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen)) + if (!strcmp(name, "cdg")) return -ENOTSUPP; /* It stops this looping @@ -5737,7 +5742,7 @@ static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, tp->bpf_chg_cc_inprogress = 1; ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION, - KERNEL_SOCKPTR(optval), *optlen); + KERNEL_SOCKPTR(name), len); tp->bpf_chg_cc_inprogress = 0; return ret; } -- 2.56.0.lol.gf7de81456940