All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Check and set TCP congestion control from the same name
@ 2026-11-30  6:57 Paul Moses
  2026-12-03  3:58 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Moses @ 2026-11-30  6:57 UTC (permalink / raw)
  To: bpf; +Cc: netdev, daniel, martin.lau, ast, john.fastabend, sdf

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>
---
Found while auditing/modifying tcp cong.

CAP_BPF/CAP_PERFMON and CONFIG_TCP_CONG_CDG=y is sufficient, but =m with 
tcp_cdg already loaded also counts. CDG doesn't need to be the default.
---
BUG: KASAN: invalid-free in tcp_cdg_release+0x38/0xac
Free of addr ffff000005966808 by task fun/46
CPU: 0 UID: 0 PID: 46 Comm: fun Not tainted 7.4.0-rc4 #1 PREEMPT 

Hardware name: linux,dummy-virt (DT)
Call trace:
 show_stack+0x14/0x1c (C)
 __dump_stack+0x24/0x30
 dump_stack_lvl+0x50/0x64
 print_report+0xd4/0x238
 kasan_report_invalid_free+0x70/0x90
 check_slab_allocation+0xe4/0x108
 __kasan_slab_pre_free+0x30/0x3c
 kfree+0xdc/0x358
 tcp_cdg_release+0x38/0xac
 tcp_cleanup_congestion_control+0x88/0x128
 tcp_v4_destroy_sock+0x94/0x1f0
 inet_csk_destroy_sock+0xfc/0x1fc
 __tcp_close+0x608/0x8e4
 tcp_close+0x28/0xe4
 inet_release+0xe4/0x12c
 __sock_release+0x74/0x17c
 sock_close+0x14/0x20
 __fput+0x11c/0x48c
 fput_close_sync+0x8c/0x1ac
 filp_close_sync+0x20/0x30
 put_files_struct+0x12c/0x1e8
 switch_files_struct+0x58/0x68
 exit_files+0x44/0x50
 do_exit+0x424/0x1638
 do_group_exit+0x160/0x200
 pid_child_should_wake+0x0/0x158
 do_el0_svc+0x12c/0x1cc
 el0_svc+0x20/0x54
 el0t_64_sync_handler+0x40/0xfc
 el0t_64_sync+0x154/0x158

Allocated by task 46:
 kasan_save_track+0x3c/0x7c
 kasan_save_alloc_info+0x3c/0x48
 __kasan_slab_alloc+0x6c/0x80
 kmem_cache_alloc_noprof+0x130/0x294
 prepare_creds+0x28/0x438
 cap_task_prctl+0x248/0x4a0
 __arm64_sys_prctl+0xe0/0xa70
 do_el0_svc+0x12c/0x1cc
 el0_svc+0x20/0x54
 el0t_64_sync_handler+0x40/0xfc
 el0t_64_sync+0x154/0x158

The buggy address belongs to the object at ffff000005966800
 which belongs to the cache cred of size 176
The buggy address is located 8 bytes inside of
 176-byte region [ffff000005966800, ffff0000059668b0)

The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x45966
flags: 0x0(zone=0)
page_type: f5(slab)
raw: 0000000000000000 ffff000001e77280 dead000000000122 0000000000000000
raw: 0000000000000000 0000000200100010 00000000f5000000 0000000000000000
page dumped because: kasan: bad access detected

Memory state around the buggy address:
 ffff000005966700: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff000005966780: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
>ffff000005966800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                      ^
 ffff000005966880: 00 00 00 00 00 00 fc fc fc fc fc fc fc fc fc fc
 ffff000005966900: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
---
 net/core/filter.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2be65c..80a26fc5df7c 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,16 @@ static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
 					 KERNEL_SOCKPTR(optlen));
 	}
 
+	/* optval may point into a shared BPF map. */
+	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 +5743,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

* Re: [PATCH bpf] bpf: Check and set TCP congestion control from the same name
  2026-11-30  6:57 [PATCH bpf] bpf: Check and set TCP congestion control from the same name Paul Moses
@ 2026-12-03  3:58 ` Alexei Starovoitov
  2026-12-03  6:01   ` Paul Moses
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2026-12-03  3:58 UTC (permalink / raw)
  To: Paul Moses, bpf; +Cc: netdev, daniel, martin.lau, john.fastabend, sdf

On Sun, Nov 29, 2026 at 10:57 PM Paul Moses <p@1g4.org> wrote:
> BUG: KASAN: invalid-free in tcp_cdg_release+0x38/0xac

A route with "congctl cdg" gives the same splat without a race.
tcp_ca_dst_init() replaces icsk_ca_ops and keeps the state of the old cc:

		bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
		icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
		WRITE_ONCE(icsk->icsk_ca_ops, ca);

icsk_ca_priv[2] is ca->gradients in cdg.
With TCP_FASTOPEN_CONNECT and TCP_FASTOPEN_NO_COOKIE:

  connect()              TCP_SYN_SENT, tcp_connect() is deferred
  setsockopt("bpf_cc")   ->init() sets icsk_ca_priv[2] = X
                         icsk_ca_initialized = 1
  write()                tcp_ca_dst_init() sets icsk_ca_ops = cdg
  SYN-ACK                tcp_init_transfer() skips tcp_cdg_init()
  close()                tcp_cdg_release() does kfree(X)

tcp_ca_openreq_child() doesn't clear icsk_ca_priv either.
bpf_cc->set_state() can write it in tcp_disconnect() before listen().
The child gets a copy and tcp_cdg_init() doesn't reset ca->tail.
With tail == 255 tcp_cdg_grad() writes past the end of ca->gradients.

Both should reset icsk_ca_priv and icsk_ca_initialized like
tcp_reinit_congestion_control() does.

pw-bot: cr

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

* Re: [PATCH bpf] bpf: Check and set TCP congestion control from the same name
  2026-12-03  3:58 ` Alexei Starovoitov
@ 2026-12-03  6:01   ` Paul Moses
  2026-12-04  3:23     ` Paul Moses
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Moses @ 2026-12-03  6:01 UTC (permalink / raw)
  To: Alexei Starovoitov, jiayuan.chen@linux.dev
  Cc: bpf, netdev, daniel, martin.lau, john.fastabend, sdf

On Wednesday, December 2nd, 2026 at 9:59 PM, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:

> On Sun, Nov 29, 2026 at 10:57 PM Paul Moses <p@1g4.org> wrote:
> > BUG: KASAN: invalid-free in tcp_cdg_release+0x38/0xac
> 
> A route with "congctl cdg" gives the same splat without a race.
> tcp_ca_dst_init() replaces icsk_ca_ops and keeps the state of the old cc:
> 
> 		bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
> 		icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
> 		WRITE_ONCE(icsk->icsk_ca_ops, ca);
> 
> icsk_ca_priv[2] is ca->gradients in cdg.
> With TCP_FASTOPEN_CONNECT and TCP_FASTOPEN_NO_COOKIE:
> 
>   connect()              TCP_SYN_SENT, tcp_connect() is deferred
>   setsockopt("bpf_cc")   ->init() sets icsk_ca_priv[2] = X
>                          icsk_ca_initialized = 1
>   write()                tcp_ca_dst_init() sets icsk_ca_ops = cdg
>   SYN-ACK                tcp_init_transfer() skips tcp_cdg_init()
>   close()                tcp_cdg_release() does kfree(X)
> 
> tcp_ca_openreq_child() doesn't clear icsk_ca_priv either.
> bpf_cc->set_state() can write it in tcp_disconnect() before listen().
> The child gets a copy and tcp_cdg_init() doesn't reset ca->tail.
> With tail == 255 tcp_cdg_grad() writes past the end of ca->gradients.
> 
> Both should reset icsk_ca_priv and icsk_ca_initialized like
> tcp_reinit_congestion_control() does.
> 
> pw-bot: cr
> 

Oh wow, thanks. 

tcp_ca_openreq_child fix looks dependent on this, should it be one series or count on
it being merged by the time I finish and describe dependency in cover?

https://lore.kernel.org/bpf/20261130041156.295524-1-jiayuan.chen@linux.dev/


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

* Re: [PATCH bpf] bpf: Check and set TCP congestion control from the same name
  2026-12-03  6:01   ` Paul Moses
@ 2026-12-04  3:23     ` Paul Moses
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Moses @ 2026-12-04  3:23 UTC (permalink / raw)
  To: Alexei Starovoitov, jiayuan.chen@linux.dev, Eric Dumazet
  Cc: bpf, netdev, daniel, martin.lau, john.fastabend, sdf

On Thursday, December 3rd, 2026 at 12:01 AM, Paul Moses <p@1g4.org> wrote:

> On Wednesday, December 2nd, 2026 at 9:59 PM, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> 
> > On Sun, Nov 29, 2026 at 10:57 PM Paul Moses <p@1g4.org> wrote:
> > > BUG: KASAN: invalid-free in tcp_cdg_release+0x38/0xac
> >
> > A route with "congctl cdg" gives the same splat without a race.
> > tcp_ca_dst_init() replaces icsk_ca_ops and keeps the state of the old cc:
> >
> > 		bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
> > 		icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
> > 		WRITE_ONCE(icsk->icsk_ca_ops, ca);
> >
> > icsk_ca_priv[2] is ca->gradients in cdg.
> > With TCP_FASTOPEN_CONNECT and TCP_FASTOPEN_NO_COOKIE:
> >
> >   connect()              TCP_SYN_SENT, tcp_connect() is deferred
> >   setsockopt("bpf_cc")   ->init() sets icsk_ca_priv[2] = X
> >                          icsk_ca_initialized = 1
> >   write()                tcp_ca_dst_init() sets icsk_ca_ops = cdg
> >   SYN-ACK                tcp_init_transfer() skips tcp_cdg_init()
> >   close()                tcp_cdg_release() does kfree(X)
> >
> > tcp_ca_openreq_child() doesn't clear icsk_ca_priv either.
> > bpf_cc->set_state() can write it in tcp_disconnect() before listen().
> > The child gets a copy and tcp_cdg_init() doesn't reset ca->tail.
> > With tail == 255 tcp_cdg_grad() writes past the end of ca->gradients.
> >
> > Both should reset icsk_ca_priv and icsk_ca_initialized like
> > tcp_reinit_congestion_control() does.
> >
> > pw-bot: cr
> >
> 
> Oh wow, thanks.
> 
> tcp_ca_openreq_child fix looks dependent on this, should it be one series or count on
> it being merged by the time I finish and describe dependency in cover?
> 
> https://lore.kernel.org/bpf/20261130041156.295524-1-jiayuan.chen@linux.dev/
> 

Unless Eric or someone else is still working on this, I'll take a stab
at it.

Can we restrict bpf_setsockopt(TCP_CONGESTION) to .init for BPF CAs?
Does anything need it from the other callbacks?

It's not just cubic, I also got the highspeed splat below.

https://lore.kernel.org/netdev/20261109172356.3113732-1-edumazet@kernel.org/

Unable to handle kernel paging request at virtual address ffff70011033cb63
KASAN: probably wild-memory-access in range [0xffff8008819e5b18-0xffff8008819e5b1f]
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
[ffff70011033cb63] pgd=0000000000000000, p4d=000000013f5ed003, pud=0000000000000000
Internal error: Oops: 0000000096000005 [#1]  SMP
Modules linked in:
CPU: 0 UID: 0 PID: 54 Comm: ca_highspeed Not tainted 7.4.0-rc5+ #10 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 : ffff800084317450
x29: ffff8000843174a0 x28: 1ffff00010862ec7 x27: ffff800084317638
x26: 1fffe00001a132b2 x25: ffff00000d099590 x24: ffff800084317620
x23: ffff00000d098dc0 x22: 0000000000041404 x21: 0000000000000001
x20: ffff8000821dc680 x19: 0000000000000003 x18: 0000000052efde4c
x17: 1ffff0011033cb63 x16: 00000000ffffffff x15: ffff8000819e5b20
x14: 1fffe00001a132bd x13: ffff00000d0995e8 x12: 0000000000000000
x11: 1fffe00001a132cf x10: ffff00000d09967c x9 : dfff800000000000
x8 : ffff8008819e5b18 x7 : 0000000000000000 x6 : 0000000000000000
x5 : ffff800084317620 x4 : 00000000000008a0 x3 : 0000000000041404
x2 : 0000000000000001 x1 : 000000001dcf62c9 x0 : ffff00000d098dc0
Call trace:
 hstcp_cong_avoid+0xec/0x898 (P)
 tcp_ack+0x2b3c/0x4568
 tcp_rcv_established+0xe34/0x2250
 tcp_v4_do_rcv+0x198/0xab0
 tcp_v4_rcv+0x15b4/0x1d68
 ip_protocol_deliver_rcu+0xb8/0x2b0
 ip_local_deliver_finish+0x248/0x498
 NF_HOOK+0x18c/0x1f0
 ip_local_deliver+0xfc/0x160
 ip_rcv_finish+0xd8/0xf8
 NF_HOOK+0x18c/0x1f0
 ip_rcv+0x74/0x98
 __netif_receive_skb_one_core+0xb4/0xd8
 __netif_receive_skb+0x28/0x110
 process_backlog+0x3fc/0xed0
 __napi_poll+0x9c/0x420
 net_rx_action+0x3b8/0x850
 handle_softirqs+0x30c/0xc20
 __do_softirq+0x18/0x20
 ____do_softirq+0x14/0x28
 call_on_irq_stack+0x30/0x58
 do_softirq_own_stack+0x20/0x30
 do_softirq+0x88/0xe8
 __local_bh_enable_ip+0x190/0x258
 __dev_queue_xmit+0xaf4/0x1bd8
 ip_finish_output2+0x884/0xaf8
 __ip_finish_output+0xbc/0x238
 ip_finish_output+0x24/0x278
 ip_output+0x1b0/0x2a8
 ip_local_out+0xb0/0xd8
 __ip_queue_xmit+0xa40/0x12e8
 ip_queue_xmit+0x40/0x78
 __tcp_transmit_skb+0x14d8/0x2a90
 __tcp_send_ack+0x1f8/0x4f0
 tcp_send_ack+0x48/0x78
 __tcp_cleanup_rbuf+0x1d4/0x358
 tcp_cleanup_rbuf+0x94/0x430
 tcp_recvmsg_locked+0x157c/0x17e0
 tcp_recvmsg+0x180/0x380
 inet_recvmsg+0xa4/0xc0
 sock_recvmsg+0xc0/0x140
 sock_read_iter+0x128/0x1a0
 vfs_read+0x2f4/0x3e0
 ksys_read+0xac/0x128
 __arm64_sys_read+0x84/0xa0
 do_el0_svc+0x134/0x1e0
 el0_svc+0x40/0x100
 el0t_64_sync_handler+0x40/0x100
 el0t_64_sync+0x154/0x158
Code: d000250f 912c81ef 8b100de8 d343fd11 (38e96a31)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception in interrupt
-------------------------------------------------------------------
watchdog: BUG: soft lockup - CPU#1 stuck for 26s! [swapper/1:0]
Modules linked in:
irq event stamp: 65975
hardirqs last  enabled at (65974): [<ffff800081718834>] arm64_exit_to_kernel_mode_preempt+0x24/0x30
hardirqs last disabled at (65975): [<ffff800081716d4c>] el1_interrupt+0x24/0x58
softirqs last  enabled at (14116): [<ffff8000800102a0>] __do_softirq+0x18/0x20
softirqs last disabled at (14141): [<ffff8000800102a0>] __do_softirq+0x18/0x20
CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Not tainted 7.4.0-rc5+ #9 PREEMPT 
Hardware name: linux,dummy-virt (DT)
pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : cubictcp_cong_avoid+0x538/0xb60
lr : cubictcp_cong_avoid+0x424/0xb60
sp : ffff800084327430
x29: ffff800084327450 x28: ffff00000d989608 x27: 0000000000000000
x26: 1fffe00001b312c1 x25: 0000000000000002 x24: 0000000000000000
x23: 00000000b33cdfee x22: ffff00000d98960c x21: dfff800000000000
x20: 0000000000000001 x19: ffff00000d988dc0 x18: 00000000c282dd2b
x17: 1fffe00001b314a5 x16: 0000000000000007 x15: dfff800000000000
x14: 0000000000000002 x13: 0000000000000003 x12: ffff8000821bd280
x11: ffff00000d9895e8 x10: 0000000000000000 x9 : 0000000000000000
x8 : 0000000000000000 x7 : 0000000000000000 x6 : 0000000000000000
x5 : ffff800084327620 x4 : 0000000000000155 x3 : 0000000000045706
x2 : 0000000000000001 x1 : 00000000c283962b x0 : 0000000000000000
Call trace:
 cubictcp_cong_avoid+0x538/0xb60 (P)
 tcp_ack+0x2b3c/0x4568
 tcp_rcv_established+0x508/0x2250
 tcp_v4_do_rcv+0x198/0xab0
 tcp_v4_rcv+0x15b4/0x1d68
 ip_protocol_deliver_rcu+0xb8/0x2b0
 ip_local_deliver_finish+0x248/0x498
 NF_HOOK+0x18c/0x1f0
 ip_local_deliver+0xfc/0x160
 ip_rcv_finish+0xd8/0xf8
 NF_HOOK+0x18c/0x1f0
 ip_rcv+0x74/0x98
 __netif_receive_skb_one_core+0xb4/0xd8
 __netif_receive_skb+0x28/0x110
 process_backlog+0x3fc/0xed0
 __napi_poll+0x9c/0x420
 net_rx_action+0x3b8/0x850
 handle_softirqs+0x30c/0xc20
 __do_softirq+0x18/0x20
 ____do_softirq+0x14/0x28
 call_on_irq_stack+0x30/0x58
 do_softirq_own_stack+0x20/0x30
 __irq_exit_rcu+0xec/0x2a8
 irq_exit_rcu+0x18/0x78
 el1_interrupt+0x3c/0x58
 el1h_64_irq_handler+0x14/0x20
 el1h_64_irq+0x78/0x80
 default_idle_call+0x98/0xb0 (P)
 do_idle+0xdc/0x1b8
 cpu_startup_entry+0x60/0x80
 secondary_start_kernel+0x1d4/0x208
 __secondary_switched+0x74/0x78
Kernel panic - not syncing: softlockup: hung tasks



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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  6:57 [PATCH bpf] bpf: Check and set TCP congestion control from the same name Paul Moses
2026-12-03  3:58 ` Alexei Starovoitov
2026-12-03  6:01   ` Paul Moses
2026-12-04  3:23     ` Paul Moses

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.