From: Paul Moses <p@1g4.org>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, daniel@iogearbox.net,
martin.lau@linux.dev, ast@kernel.org, john.fastabend@gmail.com,
sdf@fomichev.me
Subject: [PATCH bpf] bpf: Check and set TCP congestion control from the same name
Date: Mon, 30 Nov 2026 06:57:19 +0000 [thread overview]
Message-ID: <20261130065659.621451-1-p@1g4.org> (raw)
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
next reply other threads:[~2026-11-30 6:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 6:57 Paul Moses [this message]
2026-12-03 3:58 ` [PATCH bpf] bpf: Check and set TCP congestion control from the same name Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130065659.621451-1-p@1g4.org \
--to=p@1g4.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=martin.lau@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=sdf@fomichev.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox