All of lore.kernel.org
 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
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ 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] 11+ 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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ 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] 11+ 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
  2026-12-04 21:04 ` [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Alexei Starovoitov
  3 siblings, 0 replies; 11+ 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] 11+ 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
  2026-12-04 21:04 ` [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Alexei Starovoitov
  3 siblings, 0 replies; 11+ 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] 11+ messages in thread

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  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
                   ` (2 preceding siblings ...)
  2026-12-04  9:01 ` [PATCH bpf v2 3/3] bpf: check and set TCP congestion control from the same name Paul Moses
@ 2026-12-04 21:04 ` Alexei Starovoitov
  2026-12-04 21:26   ` Paul Moses
  3 siblings, 1 reply; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-04 21:04 UTC (permalink / raw)
  To: Paul Moses, bpf
  Cc: netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

On Fri, Dec 04, 2026 at 01:01 AM Paul Moses <p@1g4.org> wrote:
> 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.

tcp fixes go via net tree.
Patch 1 fixes a tcp bug. bpf is not needed to hit it.
Use setsockopt("vegas") instead of bpf_cc in the sequence from [1].
tcp_vegas_init() sets minRTT and baseRTT to 0x7fffffff.
They are at offsets 16 and 20 of icsk_ca_priv.
cdg keeps ca->gradients there.
tcp_init_transfer() skips tcp_cdg_init(), so tcp_cdg_grad() and
tcp_cdg_release() see ca->gradients == 0x7fffffff7fffffff.

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

It doesn't.
Without [2] the lsm prog can init a cc on the child before
tcp_ca_openreq_child() runs.
The reset drops that state without ->release().
cdg is the only in-tree cc with ->release(), and bpf_setsockopt()
refuses cdg. So there is nothing to free.
A bpf cc gets ->init() twice. That's all.
Patches 1 and 2 don't depend on [2]. The backports don't either.

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

In [3] you asked whether bpf_setsockopt(TCP_CONGESTION) can be limited
to .init.
No. It doesn't help.
.init of a bpf cc can write icsk_ca_priv after bpf_setsockopt() returns.

Pls resend patches 1 and 2 as [PATCH net].
Pls resend patch 3 alone as [PATCH bpf v3]. It doesn't depend on them.

pw-bot: cr

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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-04 21:04 ` [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Alexei Starovoitov
@ 2026-12-04 21:26   ` Paul Moses
  2026-12-05  1:36     ` Alexei Starovoitov
  0 siblings, 1 reply; 11+ messages in thread
From: Paul Moses @ 2026-12-04 21:26 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

> > Patch 2 needs Jiayuan's fix [2] so the clone hook runs after the
> > child's CA state is cleared.
> 
> It doesn't.
> Without [2] the lsm prog can init a cc on the child before
> tcp_ca_openreq_child() runs.
> The reset drops that state without ->release().
> cdg is the only in-tree cc with ->release(), and bpf_setsockopt()
> refuses cdg. So there is nothing to free.
> A bpf cc gets ->init() twice. That's all.
> Patches 1 and 2 don't depend on [2]. The backports don't either.
> 

Sorry, I should have been more clear why it's needed.

1. Listener selects ca_noswitch → listener owns the only struct_ops map reference.
2. Link and map fd are closed.
3. Child inherits icsk_ca_ops = ca_noswitch but takes no reference.
4. LSM hook switches child to cubic → tcp_cleanup_congestion_control() drops the
   listener’s map reference.
5. Map is freed.
6. Listener close touches the dangling icsk_ca_ops

Run /init as init process
BPF_LSM_CGROUP attach ret=0 errno=22
listener ca_noswitch ret=0 errno=22
CA LINK/OBJ CLOSED
connect ret=0 errno=22
accept ret=5 errno=22
CHILD_CC=cubic
sleep before listener close
listener closed==================================================================
BUG: KASAN: slab-use-after-free in tcp_cleanup_congestion_control+0xcc/0x170

Read of size 8 at addr ffff000005bcdbb0 by task lsm_mapreg/55

CPU: 0 UID: 0 PID: 55 Comm: lsm_mapreg Not tainted 7.4.0-rc5+ #13 PREEMPT 
DONEHardware name: linux,dummy-virt (DT)
Call trace:

 show_stack+0x1c/0x30 (C)
 __dump_stack+0x24/0x30
 dump_stack_lvl+0x60/0x80
 print_report+0xd4/0x248
 kasan_report+0x88/0xc0
 __asan_report_load8_noabort+0x1c/0x28
 tcp_cleanup_congestion_control+0xcc/0x170
 tcp_v4_destroy_sock+0x14c/0x410
 inet_csk_destroy_sock+0x104/0x210
 __tcp_close+0x77c/0x9e0
 tcp_close+0x30/0xf8
 inet_release+0x114/0x180
 __sock_release+0x9c/0x1b8
 sock_close+0x1c/0x30
 __fput+0x140/0x3c0
 fput_close_sync+0x94/0x1c0
 __arm64_sys_close+0x7c/0xc8
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Allocated by task 55:
 kasan_save_track+0x3c/0x80
 kasan_save_alloc_info+0x3c/0x48
 __kasan_kmalloc+0x98/0xb0
 __kmalloc_node_noprof+0x2e8/0x5f0
 __bpf_map_area_alloc+0x80/0x188
 bpf_map_area_alloc+0x18/0x28
 bpf_struct_ops_map_alloc+0x1b8/0x580
 map_create+0x610/0xfe8
 __sys_bpf+0x190/0x538
 __arm64_sys_bpf+0xd0/0xf0
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Freed by task 41:
 kasan_save_track+0x3c/0x80
 kasan_save_free_info+0x50/0x68
 __kasan_slab_free+0x90/0xc0
 kfree+0x160/0x558
 kvfree+0x34/0x48
 bpf_map_area_free+0x14/0x28
 __bpf_struct_ops_map_free+0x158/0x1b0
 bpf_struct_ops_map_free+0x100/0x130
 bpf_map_free+0x170/0x3f8
 bpf_map_free_deferred+0x98/0xe0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

Last potentially related work creation:
 kasan_save_stack+0x3c/0x68
 __kasan_record_aux_stack+0xbc/0xd0
 kasan_record_aux_stack+0x10/0x18
 insert_work+0x40/0x150
 __queue_work+0x950/0xea0
 queue_work_on+0x90/0xc8
 bpf_map_free_in_work+0x100/0x120
 bpf_map_free_rcu_gp+0x18/0x28
 srcu_invoke_callbacks+0x22c/0x4d0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

The buggy address belongs to the object at ffff000005bcd800
 which belongs to the cache kmalloc-1k of size 1024
The buggy address is located 944 bytes inside of
 freed 1024-byte region [ffff000005bcd800, ffff000005bcdc00)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x45bc8
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0x40(head|zone=0)
page_type: f5(slab)
raw: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
raw: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
head: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000003 fffffdffc016f201 00000000ffffffff 00000000ffffffff
head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000008
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff000005bcda80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff000005bcdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff000005bcdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                     ^
 ffff000005bcdc00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff000005bcdc80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================
Disabling lock debugging due to kernel taint
==================================================================
BUG: KASAN: slab-use-after-free in bpf_map_put+0x2c/0x1a0
Write of size 8 at addr ffff000005bcd918 by task lsm_mapreg/55

CPU: 0 UID: 0 PID: 55 Comm: lsm_mapreg Tainted: G    B               7.4.0-rc5+ #13 PREEMPT 
Tainted: [B]=BAD_PAGE
Hardware name: linux,dummy-virt (DT)
Call trace:
 show_stack+0x1c/0x30 (C)
 __dump_stack+0x24/0x30
 dump_stack_lvl+0x60/0x80
 print_report+0xd4/0x248
 kasan_report+0x88/0xc0
 kasan_check_range+0x174/0x1a8
 __kasan_check_write+0x1c/0x28
 bpf_map_put+0x2c/0x1a0
 bpf_struct_ops_put+0x18/0x28
 tcp_cleanup_congestion_control+0x108/0x170
 tcp_v4_destroy_sock+0x14c/0x410
 inet_csk_destroy_sock+0x104/0x210
 __tcp_close+0x77c/0x9e0
 tcp_close+0x30/0xf8
 inet_release+0x114/0x180
 __sock_release+0x9c/0x1b8
 sock_close+0x1c/0x30
 __fput+0x140/0x3c0
 fput_close_sync+0x94/0x1c0
 __arm64_sys_close+0x7c/0xc8
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Allocated by task 55:
 kasan_save_track+0x3c/0x80
 kasan_save_alloc_info+0x3c/0x48
 __kasan_kmalloc+0x98/0xb0
 __kmalloc_node_noprof+0x2e8/0x5f0
 __bpf_map_area_alloc+0x80/0x188
 bpf_map_area_alloc+0x18/0x28
 bpf_struct_ops_map_alloc+0x1b8/0x580
 map_create+0x610/0xfe8
 __sys_bpf+0x190/0x538
 __arm64_sys_bpf+0xd0/0xf0
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Freed by task 41:
 kasan_save_track+0x3c/0x80
 kasan_save_free_info+0x50/0x68
 __kasan_slab_free+0x90/0xc0
 kfree+0x160/0x558
 kvfree+0x34/0x48
 bpf_map_area_free+0x14/0x28
 __bpf_struct_ops_map_free+0x158/0x1b0
 bpf_struct_ops_map_free+0x100/0x130
 bpf_map_free+0x170/0x3f8
 bpf_map_free_deferred+0x98/0xe0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

Last potentially related work creation:
 kasan_save_stack+0x3c/0x68
 __kasan_record_aux_stack+0xbc/0xd0
 kasan_record_aux_stack+0x10/0x18
 insert_work+0x40/0x150
 __queue_work+0x950/0xea0
 queue_work_on+0x90/0xc8
 bpf_map_free_in_work+0x100/0x120
 bpf_map_free_rcu_gp+0x18/0x28
 srcu_invoke_callbacks+0x22c/0x4d0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

The buggy address belongs to the object at ffff000005bcd800
 which belongs to the cache kmalloc-1k of size 1024
The buggy address is located 280 bytes inside of
 freed 1024-byte region [ffff000005bcd800, ffff000005bcdc00)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x45bc8
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0x40(head|zone=0)
page_type: f5(slab)
raw: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
raw: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
head: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000003 fffffdffc016f201 00000000ffffffff 00000000ffffffff
head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000008
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff000005bcd800: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff000005bcd880: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff000005bcd900: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                            ^
 ffff000005bcd980: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff000005bcda00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================
LSM_MAPREG_EXIT=0
 dump_stack_lvl+0x60/0x80
 print_report+0xd4/0x248
 kasan_report+0x88/0xc0
 __asan_report_load8_noabort+0x1c/0x28
 tcp_cleanup_congestion_control+0xcc/0x170
 tcp_v4_destroy_sock+0x14c/0x410
 inet_csk_destroy_sock+0x104/0x210
 __tcp_close+0x77c/0x9e0
 tcp_close+0x30/0xf8
 inet_release+0x114/0x180
 __sock_release+0x9c/0x1b8
 sock_close+0x1c/0x30
 __fput+0x140/0x3c0
 fput_close_sync+0x94/0x1c0
 __arm64_sys_close+0x7c/0xc8
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Allocated by task 55:
 kasan_save_track+0x3c/0x80
 kasan_save_alloc_info+0x3c/0x48
 __kasan_kmalloc+0x98/0xb0
 __kmalloc_node_noprof+0x2e8/0x5f0
 __bpf_map_area_alloc+0x80/0x188
 bpf_map_area_alloc+0x18/0x28
 bpf_struct_ops_map_alloc+0x1b8/0x580
 map_create+0x610/0xfe8
 __sys_bpf+0x190/0x538
 __arm64_sys_bpf+0xd0/0xf0
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Freed by task 41:
 kasan_save_track+0x3c/0x80
 kasan_save_free_info+0x50/0x68
 __kasan_slab_free+0x90/0xc0
 kfree+0x160/0x558
 kvfree+0x34/0x48
 bpf_map_area_free+0x14/0x28
 __bpf_struct_ops_map_free+0x158/0x1b0
 bpf_struct_ops_map_free+0x100/0x130
 bpf_map_free+0x170/0x3f8
 bpf_map_free_deferred+0x98/0xe0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

Last potentially related work creation:
 kasan_save_stack+0x3c/0x68
 __kasan_record_aux_stack+0xbc/0xd0
 kasan_record_aux_stack+0x10/0x18
 insert_work+0x40/0x150
 __queue_work+0x950/0xea0
 queue_work_on+0x90/0xc8
 bpf_map_free_in_work+0x100/0x120
 bpf_map_free_rcu_gp+0x18/0x28
 srcu_invoke_callbacks+0x22c/0x4d0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

The buggy address belongs to the object at ffff000005bcd800
 which belongs to the cache kmalloc-1k of size 1024
The buggy address is located 944 bytes inside of
 freed 1024-byte region [ffff000005bcd800, ffff000005bcdc00)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x45bc8
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0x40(head|zone=0)
page_type: f5(slab)
raw: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
raw: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000040 ffff0000048030c0 fffffdffc0315e10 fffffdffc0345010
head: 0000000000000000 00000000000a000a 00000000f5000000 0000000000000000
head: 0000000000000003 fffffdffc016f201 00000000ffffffff 00000000ffffffff
head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000008
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff000005bcda80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff000005bcdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff000005bcdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                     ^
 ffff000005bcdc00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff000005bcdc80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================
Disabling lock debugging due to kernel taint
==================================================================
BUG: KASAN: slab-use-after-free in bpf_map_put+0x2c/0x1a0
Write of size 8 at addr ffff000005bcd918 by task lsm_mapreg/55

CPU: 0 UID: 0 PID: 55 Comm: lsm_mapreg Tainted: G    B               7.4.0-rc5+ #13 PREEMPT 
Tainted: [B]=BAD_PAGE
Hardware name: linux,dummy-virt (DT)
Call trace:
 show_stack+0x1c/0x30 (C)
 __dump_stack+0x24/0x30
 dump_stack_lvl+0x60/0x80
 print_report+0xd4/0x248
 kasan_report+0x88/0xc0
 kasan_check_range+0x174/0x1a8
 __kasan_check_write+0x1c/0x28
 bpf_map_put+0x2c/0x1a0
 bpf_struct_ops_put+0x18/0x28
 tcp_cleanup_congestion_control+0x108/0x170
 tcp_v4_destroy_sock+0x14c/0x410
 inet_csk_destroy_sock+0x104/0x210
 __tcp_close+0x77c/0x9e0
 tcp_close+0x30/0xf8
 inet_release+0x114/0x180
 __sock_release+0x9c/0x1b8
 sock_close+0x1c/0x30
 __fput+0x140/0x3c0
 fput_close_sync+0x94/0x1c0
 __arm64_sys_close+0x7c/0xc8
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Allocated by task 55:
 kasan_save_track+0x3c/0x80
 kasan_save_alloc_info+0x3c/0x48
 __kasan_kmalloc+0x98/0xb0
 __kmalloc_node_noprof+0x2e8/0x5f0
 __bpf_map_area_alloc+0x80/0x188
 bpf_map_area_alloc+0x18/0x28
 bpf_struct_ops_map_alloc+0x1b8/0x580
 map_create+0x610/0xfe8
 __sys_bpf+0x190/0x538
 __arm64_sys_bpf+0xd0/0xf0
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158

Freed by task 41:
 kasan_save_track+0x3c/0x80
 kasan_save_free_info+0x50/0x68
 __kasan_slab_free+0x90/0xc0
 kfree+0x160/0x558
 kvfree+0x34/0x48
 bpf_map_area_free+0x14/0x28
 __bpf_struct_ops_map_free+0x158/0x1b0
 bpf_struct_ops_map_free+0x100/0x130
 bpf_map_free+0x170/0x3f8
 bpf_map_free_deferred+0x98/0xe0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20

Last potentially related work creation:
 kasan_save_stack+0x3c/0x68
 __kasan_record_aux_stack+0xbc/0xd0
 kasan_record_aux_stack+0x10/0x18
 insert_work+0x40/0x150
 __queue_work+0x950/0xea0
 queue_work_on+0x90/0xc8
 bpf_map_free_in_work+0x100/0x120
 bpf_map_free_rcu_gp+0x18/0x28
 srcu_invoke_callbacks+0x22c/0x4d0
 process_scheduled_works+0x794/0xf80
 worker_thread+0x660/0xa10
 kthread+0x2ac/0x3b0
 ret_from_fork+0x10/0x20


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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-04 21:26   ` Paul Moses
@ 2026-12-05  1:36     ` Alexei Starovoitov
  2026-12-05  2:22       ` Paul Moses
  0 siblings, 1 reply; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  1:36 UTC (permalink / raw)
  To: Paul Moses
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

On Fri, Dec 04, 2026 at 01:26 PM Paul Moses <p@1g4.org> wrote:
> Sorry, I should have been more clear why it's needed.
>
> 1. Listener selects ca_noswitch → listener owns the only struct_ops map reference.
> 2. Link and map fd are closed.
> 3. Child inherits icsk_ca_ops = ca_noswitch but takes no reference.
> 4. LSM hook switches child to cubic → tcp_cleanup_congestion_control() drops the
>    listener’s map reference.
> 5. Map is freed.
> 6. Listener close touches the dangling icsk_ca_ops

That's the bug that [2] fixes. Its commit log has it:
"TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
reference on."
The put in step 4 happens in inet_csk_clone_lock(), before
tcp_ca_openreq_child() runs.
Patch 2 writes icsk_ca_initialized and icsk_ca_priv only.
It doesn't get or put icsk_ca_ops.
Steps 1-6 are the same with and without patch 2.
So [2] is needed on its own. Patch 2 still doesn't depend on it.

Pls drop the "Backport prereq" note from patch 2 when you resend.

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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-05  1:36     ` Alexei Starovoitov
@ 2026-12-05  2:22       ` Paul Moses
  2026-12-05  2:36         ` Alexei Starovoitov
  0 siblings, 1 reply; 11+ messages in thread
From: Paul Moses @ 2026-12-05  2:22 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen





On Friday, December 4th, 2026 at 7:36 PM, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:

> On Fri, Dec 04, 2026 at 01:26 PM Paul Moses <p@1g4.org> wrote:
> > Sorry, I should have been more clear why it's needed.
> >
> > 1. Listener selects ca_noswitch → listener owns the only struct_ops map reference.
> > 2. Link and map fd are closed.
> > 3. Child inherits icsk_ca_ops = ca_noswitch but takes no reference.
> > 4. LSM hook switches child to cubic → tcp_cleanup_congestion_control() drops the
> >    listener’s map reference.
> > 5. Map is freed.
> > 6. Listener close touches the dangling icsk_ca_ops
> 
> That's the bug that [2] fixes. Its commit log has it:
> "TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
> reference on."
> The put in step 4 happens in inet_csk_clone_lock(), before
> tcp_ca_openreq_child() runs.
> Patch 2 writes icsk_ca_initialized and icsk_ca_priv only.
> It doesn't get or put icsk_ca_ops.
> Steps 1-6 are the same with and without patch 2.
> So [2] is needed on its own. Patch 2 still doesn't depend on it.
> 
> Pls drop the "Backport prereq" note from patch 2 when you resend.
> 

Yes, the refcount bug is independent. My concern is that patch 2 clears CA state 
already initialized by the hook.

    unpatched (#14):          init=1 release=1
    all patches without [2]:  init=2 release=1
    all patches with [2]:     init=1 release=1

Without [2], the child's CA is initialized twice without a release in between.

I can move the reset before the hook instead and drop the backport prereq note,
but I feel like we're making this more complicated than it needs to be.

#ifdef COUNT_BPF

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#define SOL_TCP		6
#define TCP_CONGESTION	13

char _license[] SEC("license") = "GPL";

struct stats {
	__u64 init;
	__u64 release;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, __u32);
	__type(value, struct stats);
} stats SEC(".maps");

static __always_inline struct stats *get(void)
{
	__u32 z = 0;

	return bpf_map_lookup_elem(&stats, &z);
}

SEC("struct_ops")
void BPF_PROG(ca_count_init, struct sock *sk)
{
	struct stats *s = get();

	if (s)
		s->init++;
}

SEC("struct_ops")
void BPF_PROG(ca_count_release, struct sock *sk)
{
	struct stats *s = get();

	if (s)
		s->release++;
}

SEC("struct_ops")
__u32 BPF_PROG(ca_count_ssthresh, struct sock *sk)
{
	return 2;
}

SEC("struct_ops")
void BPF_PROG(ca_count_cong_avoid, struct sock *sk,
	      __u32 ack, __u32 acked)
{
}

SEC("struct_ops")
__u32 BPF_PROG(ca_count_undo_cwnd, struct sock *sk)
{
	return 2;
}

SEC(".struct_ops")
struct tcp_congestion_ops ca_count_ops = {
	.init		= (void *)ca_count_init,
	.release	= (void *)ca_count_release,
	.ssthresh	= (void *)ca_count_ssthresh,
	.cong_avoid	= (void *)ca_count_cong_avoid,
	.undo_cwnd	= (void *)ca_count_undo_cwnd,
	.name		= "ca_count",
	.flags		= 1,
};

SEC("lsm_cgroup/inet_csk_clone")
int BPF_PROG(clone, struct sock *newsk,
	     const struct request_sock *req)
{
	char name[] = "ca_count";

	if (newsk)
		bpf_setsockopt(newsk, SOL_TCP, TCP_CONGESTION,
			       name, sizeof(name));

	return 1;
}

#else

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>

struct stats {
	unsigned long long init;
	unsigned long long release;
};

static int libbpf_print_fn(enum libbpf_print_level level,
			   const char *format, va_list args)
{
	if (level == LIBBPF_DEBUG)
		return 0;

	return vfprintf(stderr, format, args);
}

static int join_cgroup(const char *path)
{
	char b[64];
	int fd;

	snprintf(b, sizeof(b), "%s/cgroup.procs", path);

	fd = open(b, O_WRONLY);
	if (fd < 0)
		return -1;

	snprintf(b, sizeof(b), "%d\n", getpid());

	if (write(fd, b, strlen(b)) < 0) {
		close(fd);
		return -1;
	}

	close(fd);
	return 0;
}

int main(void)
{
	const char *cgp = "/sys/fs/cgroup/count_single";
	struct bpf_map *st = NULL, *sm = NULL, *m;
	struct bpf_program *prog;
	int lfd, cfd, afd, cg, r;
	struct bpf_object *obj;
	struct sockaddr_in sa;
	struct bpf_link *link;
	struct stats v = {};
	unsigned int k = 0;

	setvbuf(stderr, NULL, _IONBF, 0);
	libbpf_set_print(libbpf_print_fn);

	if (mkdir(cgp, 0755) && errno != EEXIST) {
		perror("mkdir");
		return 1;
	}

	if (join_cgroup(cgp)) {
		perror("join");
		return 1;
	}

	cg = open(cgp, O_RDONLY | O_DIRECTORY);
	if (cg < 0) {
		perror("open cg");
		return 1;
	}

	obj = bpf_object__open_file("/count_single.bpf.o", NULL);
	if (!obj || bpf_object__load(obj)) {
		perror("load bpf");
		return 1;
	}

	bpf_object__for_each_map(m, obj) {
		if (bpf_map__type(m) == BPF_MAP_TYPE_STRUCT_OPS)
			st = m;
		else if (!strcmp(bpf_map__name(m), "stats"))
			sm = m;
	}

	prog = bpf_object__find_program_by_name(obj, "clone");
	if (!st || !sm || !prog) {
		fprintf(stderr, "missing objects\n");
		return 1;
	}

	link = bpf_map__attach_struct_ops(st);
	if (!link) {
		perror("attach ca");
		return 1;
	}

	r = bpf_prog_attach(bpf_program__fd(prog), cg,
			    BPF_LSM_CGROUP, 0);

	fprintf(stderr, "LSM attach=%d errno=%d\n", r, errno);
	if (r)
		return 1;

	lfd = socket(AF_INET, SOCK_STREAM, 0);
	if (lfd < 0) {
		perror("lsock");
		return 1;
	}

	setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,
		   &(int){1}, sizeof(int));

	memset(&sa, 0, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(25033);

	inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr);

	if (bind(lfd, (struct sockaddr *)&sa, sizeof(sa))) {
		perror("bind");
		return 1;
	}

	if (listen(lfd, 8)) {
		perror("listen");
		return 1;
	}

	cfd = socket(AF_INET, SOCK_STREAM, 0);

	r = connect(cfd, (struct sockaddr *)&sa, sizeof(sa));
	fprintf(stderr, "connect=%d errno=%d\n", r, errno);

	afd = accept(lfd, NULL, NULL);
	fprintf(stderr, "accept=%d errno=%d\n", afd, errno);

	if (afd >= 0) {
		char cc[32] = {};
		socklen_t cl = sizeof(cc) - 1;

		if (getsockopt(afd, IPPROTO_TCP, TCP_CONGESTION,
			       cc, &cl) == 0)
			fprintf(stderr, "CHILD_CC=%s\n", cc);
	}

	close(afd);
	close(cfd);
	close(lfd);

	usleep(200000);

	if (bpf_map_lookup_elem(bpf_map__fd(sm), &k, &v) == 0)
		fprintf(stderr, "STATS init=%llu release=%llu\n",
			v.init, v.release);

	close(cg);

	fprintf(stderr, "DONE\n");
	sleep(2);

	return 0;
}

#endif

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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-05  2:22       ` Paul Moses
@ 2026-12-05  2:36         ` Alexei Starovoitov
  2026-12-05  4:50           ` Paul Moses
  0 siblings, 1 reply; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  2:36 UTC (permalink / raw)
  To: Paul Moses
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

On Fri, Dec 04, 2026 at 06:22 PM Paul Moses <p@1g4.org> wrote:
> Yes, the refcount bug is independent. My concern is that patch 2 clears CA state
> already initialized by the hook.
>
>     unpatched (#14):          init=1 release=1
>     all patches without [2]:  init=2 release=1
>     all patches with [2]:     init=1 release=1
>
> Without [2], the child's CA is initialized twice without a release in between.

Yes. ->init() runs twice. It's harmless.
The second ->init() starts from zeroed icsk_ca_priv, like the first one.
Rows 1 and 2 have a real bug. They leak a reference on ca_count.
The lsm prog takes one. tcp_ca_openreq_child() takes one more, because
icsk_ca_setsockopt == 1. close() puts one.
So this prog needs [2] with and without patch 2.

> I can move the reset before the hook instead and drop the backport prereq note,
> but I feel like we're making this more complicated than it needs to be.

Pls don't move it. Without [2] the moved reset misses this case:

  inet_csk_clone_lock()
    reset
    lsm prog: bpf_setsockopt("vegas")   tcp_vegas_init()
                                        icsk_ca_initialized = 1
  tcp_ca_openreq_child()                route has "congctl cdg"
                                        icsk_ca_ops = cdg
  tcp_init_transfer()                   skips tcp_cdg_init()

cdg sees ca->gradients == 0x7fffffff7fffffff again.
The reset in tcp_ca_openreq_child() covers it with and without [2].

Keep patch 2 as-is. Drop the note only.

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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-05  2:36         ` Alexei Starovoitov
@ 2026-12-05  4:50           ` Paul Moses
  2026-12-05  7:14             ` Alexei Starovoitov
  0 siblings, 1 reply; 11+ messages in thread
From: Paul Moses @ 2026-12-05  4:50 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

On Friday, December 4th, 2026 at 8:36 PM, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:

> On Fri, Dec 04, 2026 at 06:22 PM Paul Moses <p@1g4.org> wrote:
> > Yes, the refcount bug is independent. My concern is that patch 2 clears CA state
> > already initialized by the hook.
> >
> >     unpatched (#14):          init=1 release=1
> >     all patches without [2]:  init=2 release=1
> >     all patches with [2]:     init=1 release=1
> >
> > Without [2], the child's CA is initialized twice without a release in between.
> 
> Yes. ->init() runs twice. It's harmless.
> The second ->init() starts from zeroed icsk_ca_priv, like the first one.
> Rows 1 and 2 have a real bug. They leak a reference on ca_count.
> The lsm prog takes one. tcp_ca_openreq_child() takes one more, because
> icsk_ca_setsockopt == 1. close() puts one.
> So this prog needs [2] with and without patch 2.
> 
> > I can move the reset before the hook instead and drop the backport prereq note,
> > but I feel like we're making this more complicated than it needs to be.
> 
> Pls don't move it. Without [2] the moved reset misses this case:
> 
>   inet_csk_clone_lock()
>     reset
>     lsm prog: bpf_setsockopt("vegas")   tcp_vegas_init()
>                                         icsk_ca_initialized = 1
>   tcp_ca_openreq_child()                route has "congctl cdg"
>                                         icsk_ca_ops = cdg
>   tcp_init_transfer()                   skips tcp_cdg_init()
> 
> cdg sees ca->gradients == 0x7fffffff7fffffff again.
> The reset in tcp_ca_openreq_child() covers it with and without [2].
> 
> Keep patch 2 as-is. Drop the note only.
> 

This is same highspeed splat on a different path than before which maybe 
makes it more clear. I admit this is still rather benign and may not change
your mind, but worth sharing that it's effects are not 0.

1. The LSM hook selects nv and runs tcpnv_init() with bpf_chg_cc_inprogress 
   set. The nested CA change returns -EBUSY.
2. Patch 2 clears icsk_ca_initialized in tcp_ca_openreq_child().
3. tcp_init_transfer() calls tcpnv_init() again, this time without the guard 
   set.
4. The nested CA change succeeds, but tcpnv_init() continues writing to 
   icsk_ca_priv after the CA has been replaced, corrupting the new CA's state.

    Run /init as init process
    NV_BEGIN
    insmod=0
    reno bbr bic cubic dctcp westwood highspeed htcp nv
    LSM attach=0 errno=22
    SOCKOPS attach=0 errno=22
    accept=11 errno=22
    CHILD_CC=highspeed
    Unable to handle kernel paging request at virtual address ffff7001102f431f
    KASAN: probably wild-memory-access in range [0xffff8008817a18f8-0xffff8008817a18ff]
    Mem abort info:
      ESR = 0x0000000096000005
      EC = 0x25: DABT (current EL), IL = 32 bits
      SET = 0, FnV = 0
      EA = 0, S1PTW = 0
      FSC = 0x05: level 1 translation fault
    Data abort info:
      ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
      CM = 0, WnR = 0, TnD = 0, TagAccess = 0
      GCS = 0, Overlay = 0, DirtyBit = 0
    swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000000042256000
    [ffff7001102f431f] pgd=0000000000000000, p4d=00000000ff7ee003, pud=0000000000000000
    Internal error: Oops: 0000000096000005 [#1]  SMP
    Modules linked in: tcp_nv
    CPU: 0 UID: 0 PID: 56 Comm: nvclone Not tainted 7.4.0-rc5+ #13 PREEMPT 
    Hardware name: linux,dummy-virt (DT)
    pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
    pc : hstcp_cong_avoid+0xec/0x898
    lr : tcp_ack+0x2b3c/0x4568
    sp : ffff8000847b7720
    x29: ffff8000847b7770 x28: 1ffff000108f6f21 x27: ffff8000847b7908
    x26: 1fffe00001799462 x25: ffff00000bcca310 x24: ffff8000847b78f0
    x23: ffff00000bcc9b40 x22: 0000000000055506 x21: 0000000000000001
    x20: ffff8000821dc580 x19: 0000000000000003 x18: ffff800024cfa000
    x17: 1ffff001102f431f x16: 00000000fffb77c3 x15: ffff8000819e5ae0
    x14: 1fffe0000179946d x13: ffff00000bcca368 x12: 0000000000000002
    x11: 1fffe0000179947f x10: ffff00000bcca3fc x9 : dfff800000000000
    x8 : ffff8008817a18f8 x7 : 0000000000000000 x6 : 0000000000000000
    x5 : 0000000000000001 x4 : 0000000000000000 x3 : 0000000000055506
    x2 : 0000000000000001 x1 : 000000006efcd038 x0 : ffff00000bcc9b40
    Call trace:
     hstcp_cong_avoid+0xec/0x898 (P)
     tcp_ack+0x2b3c/0x4568
     tcp_rcv_established+0x508/0x2250
     tcp_v4_do_rcv+0x198/0xab0
     __release_sock+0xd0/0x1b0
     release_sock+0x12c/0x1b8
     tcp_sendmsg+0x48/0x68
     inet_sendmsg+0x9c/0xc0
     __sock_sendmsg+0xb4/0x148
     __sys_sendto+0x194/0x208
     __arm64_sys_sendto+0xec/0x110
     do_el0_svc+0x134/0x1e0
     el0_svc+0x40/0x100
     el0t_64_sync_handler+0x40/0x100
     el0t_64_sync+0x154/0x158
    Code: 9000252f 912b81ef 8b100de8 d343fd11 (38e96a31) 

This does not happen on unpatched or with [2] applied.

What about oot modules? bpf is incredibly intricate and I'd imagine is forced 
to be less considerate, but it's less the case for net.

More historical context:
https://github.com/torvalds/linux/commit/8919a9b31eb4fb4c0a93e5fb350a626924302aa6
https://github.com/torvalds/linux/commit/be5d1b61a2ad28c7e57fe8bfa277373e8ecffcdc



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

* Re: [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check
  2026-12-05  4:50           ` Paul Moses
@ 2026-12-05  7:14             ` Alexei Starovoitov
  0 siblings, 0 replies; 11+ messages in thread
From: Alexei Starovoitov @ 2026-12-05  7:14 UTC (permalink / raw)
  To: Paul Moses
  Cc: bpf, netdev, daniel, martin.lau, edumazet, ncardwell, kuniyu,
	jiayuan.chen

On Fri, Dec 04, 2026 at 08:50 PM Paul Moses <p@1g4.org> wrote:
> 1. The LSM hook selects nv and runs tcpnv_init() with bpf_chg_cc_inprogress
>    set. The nested CA change returns -EBUSY.
> 2. Patch 2 clears icsk_ca_initialized in tcp_ca_openreq_child().
> 3. tcp_init_transfer() calls tcpnv_init() again, this time without the guard
>    set.
> 4. The nested CA change succeeds, but tcpnv_init() continues writing to
>    icsk_ca_priv after the CA has been replaced, corrupting the new CA's state.

The steps are correct, but step 4 is the bug.
It doesn't need steps 1-3.
bpf_chg_cc_inprogress is set only when bpf_setsockopt() selects nv.
Every other way to select nv runs tcpnv_init() without it.
Remove the lsm prog and keep the sockops prog:

  setsockopt(lfd, TCP_CONGESTION, "nv")   before listen()
  tcp_init_transfer() of the child        bpf_chg_cc_inprogress == 0
    tcpnv_init()
      BPF_SOCK_OPS_BASE_RTT prog
        bpf_setsockopt("highspeed")       hstcp_init(): ca->ai = 0
      ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ   <- ca->ai
  hstcp_cong_avoid()                      hstcp_aimd_vals[ca->ai] <- oops

Both fields are at offset 0 of icsk_ca_priv.

> This does not happen on unpatched or with [2] applied.

This sequence is the same on all three kernels.
It's the issue from [3]: ->init() writes icsk_ca_priv after
bpf_setsockopt() returns. Here ->init() is tcpnv_init().
It needs its own fix.
bpf_sock_ops_setsockopt() can refuse TCP_CONGESTION in
BPF_SOCK_OPS_BASE_RTT, like it refuses TCP_NODELAY in
BPF_SOCK_OPS_HDR_OPT_LEN_CB.

> What about oot modules? bpf is incredibly intricate and I'd imagine is forced
> to be less considerate, but it's less the case for net.

Out of tree modules are not a concern. That's the same in net.
sol_tcp_sockopt_congestion() refuses "cdg" by name.
An oot cc with a pointer in icsk_ca_priv is not on that list.
A bpf cc can switch to it and overwrite the pointer today.

> More historical context:
> https://github.com/torvalds/linux/commit/8919a9b31eb4fb4c0a93e5fb350a626924302aa6
> https://github.com/torvalds/linux/commit/be5d1b61a2ad28c7e57fe8bfa277373e8ecffcdc

be5d1b61a2ad fixed a leak in cdg.
User space sets cdg in TCP_SYN_SENT. The 2nd tcp_cdg_init() loses
ca->gradients.
Only the lsm prog runs between the clone and tcp_ca_openreq_child().
It can't set cdg.

Nothing changes for patch 2.

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

end of thread, other threads:[~2026-12-05  7:14 UTC | newest]

Thread overview: 11+ 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
2026-12-04 21:04 ` [PATCH bpf v2 0/3] tcp, bpf: fix CA state on open and the TCP_CONGESTION name check Alexei Starovoitov
2026-12-04 21:26   ` Paul Moses
2026-12-05  1:36     ` Alexei Starovoitov
2026-12-05  2:22       ` Paul Moses
2026-12-05  2:36         ` Alexei Starovoitov
2026-12-05  4:50           ` Paul Moses
2026-12-05  7:14             ` Alexei Starovoitov

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.