* [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; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-12-03 3:59 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox