From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=79.135.106.105 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796021850; b=HO13uiXWIKx8691usJ4Al6ixoihC2/lF7TbhveSz+RAJ2L5ANVPFp6BqeMWU0EcH5g0H XP0QFWMfjqzlQwiCxxxh5mOmC6t1lrycdwfOAasAdcU2t5pmX91Apr8gCElxdiO2hmbqb 6K0GpO0c9bwRIJ7ep0WaJxBlMDVLwbtGevt9ndT63q1fwgsdnvQc1gIDRTIUvEGCq7zqv IiCy+bo0Nv4nOZHR4O/9tr+F2lL3do7lFl3I4Oo3CTLG9Pi3HN6AWGDqRjSGROm4ZAIUa 8qFMYkFwp9eEhT94r+akldyGgGbT4ZrISrN5U94scIuazbud84vCLJmSHu+2UETb7qA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796021850; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: Feedback-ID:X-Pm-Message-ID:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=uOA+Glno92AbDDnUFHgfwGTaEf13CXZCqsOTQiNALac=; b=YfM3XFxdw5IurHBoT3ZqwR+lgycq1tjPxJRCHIXHW3uJiZnjpJxbVOf9470adtsLna4S LsCppn4vyB8oNsE4N5rzQtxf4zfPwNxrmWunLK7FAr09byU8oB8hKV62KeUz/mBjewjoa fQI0caJ4J+539WwaPAG9HJSxE/4v/KJ02qloZt36pMrMS4QmMQZbyCFR93/UnQUr7Mrv1 hZHGF2U5Zweo4NC1qK7kOS4PBFMZMq4XLKRy5dug1ZSw4hssRBRsmfwHyxXSIRKlPl7lL eRVeJwW6KfmrL1c8zc8AHczKHJPTcwyRbzyFC4wh65qnmzEuFjtdRuY9Gm4lUy1TZvQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=1g4.org; spf=pass smtp.mailfrom=1g4.org; dkim=pass header.d=1g4.org header.i=@1g4.org header.a=rsa-sha256 header.s=protonmail header.b=AJERAVhe; arc=none smtp.remote-ip=79.135.106.105 Received: from mail-106105.protonmail.ch (mail-106105.protonmail.ch [79.135.106.105]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF512606A325 for ; Mon, 30 Nov 2026 06:57:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BF512606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=1g4.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1g4.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=1g4.org header.i=@1g4.org header.a=rsa-sha256 header.s=protonmail header.b=AJERAVhe DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1g4.org; s=protonmail; t=1796021844; x=1796281044; bh=uOA+Glno92AbDDnUFHgfwGTaEf13CXZCqsOTQiNALac=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=AJERAVhe11NRwmJw6HGVHBKnv5eTddsrNlykOh2MxFyPMygCS+UWTU2jhIDO9M8uk gLdNhtiSfoLg6sP8nMpNrMfqQJVXvVZUScXcBwuvV5zCEIRB/wGEdsBVzgMas85MJX ZJqEFjG8idtE5tlyCUkwh9oO4+2j1sSAjytDvvjKJAqXL/MnuX6gEquLSERqwuy6Pf zVw4V7ZFDilhISbrjUHjhcSgbqisloCEqRXoCwj2FuQ24d3fMHCEDhPocEqM//8kei DbdejGRVY9HD2MEQnDV2AJQ+crITFHEcEwLIlMXq6PYYzpvHg8Dq1al00GjA91y7xj YCV4hF5at2l/Q== Date: Mon, 30 Nov 2026 06:57:19 +0000 To: bpf@vger.kernel.org From: Paul Moses 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 Message-ID: <20261130065659.621451-1-p@1g4.org> Feedback-ID: 8253658:user:proton X-Pm-Message-ID: a57320109f430e5f63e5791b67f6944e2684e04b X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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)socko= pt") Cc: stable@vger.kernel.org Signed-off-by: Paul Moses --- Found while auditing/modifying tcp cong. CAP_BPF/CAP_PERFMON and CONFIG_TCP_CONG_CDG=3Dy is sufficient, but =3Dm wit= h=20 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=20 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=3D0) 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, in= t optname, static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, =09=09=09=09 int *optlen, bool getopt) { +=09char name[TCP_CA_NAME_MAX]; =09struct tcp_sock *tp; -=09int ret; +=09int len, ret; =20 =09if (*optlen < 2) =09=09return -EINVAL; @@ -5708,11 +5709,16 @@ static int sol_tcp_sockopt_congestion(struct sock *= sk, char *optval, =09=09=09=09=09 KERNEL_SOCKPTR(optlen)); =09} =20 +=09/* optval may point into a shared BPF map. */ +=09len =3D min_t(int, *optlen, sizeof(name) - 1); +=09memcpy(name, optval, len); +=09name[len] =3D '\0'; + =09/* "cdg" is the only cc that alloc a ptr =09 * in inet_csk_ca area. The bpf-tcp-cc may =09 * overwrite this ptr after switching to cdg. =09 */ -=09if (*optlen >=3D sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen)) +=09if (!strcmp(name, "cdg")) =09=09return -ENOTSUPP; =20 =09/* It stops this looping @@ -5737,7 +5743,7 @@ static int sol_tcp_sockopt_congestion(struct sock *sk= , char *optval, =20 =09tp->bpf_chg_cc_inprogress =3D 1; =09ret =3D do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION, -=09=09=09=09KERNEL_SOCKPTR(optval), *optlen); +=09=09=09=09KERNEL_SOCKPTR(name), len); =09tp->bpf_chg_cc_inprogress =3D 0; =09return ret; } --=20 2.56.0.lol.gf7de81456940