Netdev List
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
@ 2026-12-04  9:01 Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 1/3] tcp: reset congestion-control state when the route selects a new CA Paul Moses
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Paul Moses @ 2026-12-04  9:01 UTC (permalink / raw)
  To: bpf
  Cc: netdev, ast, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen, Paul Moses

Patches 1 and 2 fix the cases Alexei reported [1]: stale CA state
when a route changes the algorithm or a child inherits the listener's
private state. Patch 3 uses the same name for the CDG check and lookup.

Patch 2 needs Jiayuan's fix [2] so the clone hook runs after the
child's CA state is cleared.

I left the callback issue [3] out after reading more historical context.

v1..v2: Add the two TCP fixes. The original BPF fix is patch 3.
v1: https://lore.kernel.org/bpf/20261130065659.621451-1-p@1g4.org/

splats:
p1:  BUG: KASAN: invalid-free in tcp_cdg_release+0x38/0xac
p2:  BUG: KASAN: slab-out-of-bounds in tcp_cdg_cong_avoid+0xe14/0x11c4
p3:  BUG: KASAN: double-free in tcp_cdg_release+0x38/0xac

refs:
[1] https://lore.kernel.org/netdev/DNASOXUN4KAE.1IJL3LGF4E1DO@gmail.com/
[2] https://lore.kernel.org/bpf/20261130041156.295524-1-jiayuan.chen@linux.dev/
[3] https://lore.kernel.org/bpf/2R3IejGOudqccITwe2YD7Iz6tMgFlLhGy12eQ3HWVSx-QgFb0uWBGA_WKHCHq0967iKpsbRJL4sEpxGciBlGFKs6kwBBauQLCWEiza-qes8=@1g4.org/

Paul Moses (3):
  tcp: reset congestion-control state when the route selects a new CA
  tcp: clear inherited congestion-control state for a cloned child
  bpf: check and set TCP congestion control from the same name

 net/core/filter.c        | 11 ++++++++---
 net/ipv4/tcp_minisocks.c |  3 +++
 net/ipv4/tcp_output.c    | 10 ++++++++--
 3 files changed, 19 insertions(+), 5 deletions(-)


base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
prerequisite-patch-id: ccd3c308caecec9d14cd15ceef9b817171f77b1f
-- 
2.56.0.lol.gf7de81456940



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf v2 1/3] tcp: reset congestion-control state when the route selects a new CA
  2026-12-04  9:01 [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Paul Moses
@ 2026-12-04  9:01 ` Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 2/3] tcp: clear inherited congestion-control state for a cloned child Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 3/3] bpf: check and set TCP congestion control from the same name Paul Moses
  2 siblings, 0 replies; 4+ messages in thread
From: Paul Moses @ 2026-12-04  9:01 UTC (permalink / raw)
  To: bpf
  Cc: netdev, ast, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen, Paul Moses, stable

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 <ast@kernel.org>
Link: https://lore.kernel.org/netdev/DNASOXUN4KAE.1IJL3LGF4E1DO@gmail.com/
Cc: stable@vger.kernel.org
Signed-off-by: Paul Moses <p@1g4.org>
---
 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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf v2 2/3] tcp: clear inherited congestion-control state for a cloned child
  2026-12-04  9:01 [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 1/3] tcp: reset congestion-control state when the route selects a new CA Paul Moses
@ 2026-12-04  9:01 ` Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 3/3] bpf: check and set TCP congestion control from the same name Paul Moses
  2 siblings, 0 replies; 4+ messages in thread
From: Paul Moses @ 2026-12-04  9:01 UTC (permalink / raw)
  To: bpf
  Cc: netdev, ast, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen, Paul Moses, stable

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 <ast@kernel.org>
Link: https://lore.kernel.org/netdev/DNASOXUN4KAE.1IJL3LGF4E1DO@gmail.com/
Cc: stable@vger.kernel.org
Signed-off-by: Paul Moses <p@1g4.org>
---
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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf v2 3/3] bpf: check and set TCP congestion control from the same name
  2026-12-04  9:01 [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 1/3] tcp: reset congestion-control state when the route selects a new CA Paul Moses
  2026-12-04  9:01 ` [PATCH bpf v2 2/3] tcp: clear inherited congestion-control state for a cloned child Paul Moses
@ 2026-12-04  9:01 ` Paul Moses
  2 siblings, 0 replies; 4+ messages in thread
From: Paul Moses @ 2026-12-04  9:01 UTC (permalink / raw)
  To: bpf
  Cc: netdev, ast, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen, Paul Moses, stable

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 <p@1g4.org>
---
 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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04  9:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:01 [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Paul Moses
2026-12-04  9:01 ` [PATCH bpf v2 1/3] tcp: reset congestion-control state when the route selects a new CA Paul Moses
2026-12-04  9:01 ` [PATCH bpf v2 2/3] tcp: clear inherited congestion-control state for a cloned child Paul Moses
2026-12-04  9:01 ` [PATCH bpf v2 3/3] bpf: check and set TCP congestion control from the same name Paul Moses

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox