🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25838

[PATCH bpf v3 5/9] bpf, sockmap: Fix reconnect with a psock by rejecting connect

#25838
From: Jiayuan Chen
Date: Wed, 02 Dec 2026 14:30
69 lines
12231 bytes
Commit 8cc3aef0cb19 ("tcp: Do not allow buggy transitions between ehash
and lhash2.") rejects connect() -> disconnect -> listen(), but
connect() -> disconnect -> connect() is still allowed.

A socket in a sockmap keeps its psock across tcp_disconnect(), and the
new connection runs into what the old one left there:

 - recvmsg() returns the old data still queued on psock->ingress_msg.
 - psock->copied_seq and psock->ingress_bytes still account for it, so
   copied_seq runs ahead of rcv_nxt for the rest of the connection.
 - sk_psock_backlog() does not take the socket lock for ingress, so it
   can queue old skbs from psock->ingress_skb after the reconnect.

Cleaning all of this up on disconnect is hard.

TLS rejects the disconnect instead, see commit 5071a1e606b3 ("net: tls:
explicitly disallow disconnect"). That does not work here.
sk_prot->disconnect is not only called for connect(AF_UNSPEC). close()
with a zero linger time and inet_child_forget() call it too and do not
look at the return value. tcp_bpf_disconnect() has no good way to tell
them apart. A bit set on the psock by sock_map_close() is not enough,
a BPF prog can add the socket back to a map in the middle of close()
and it gets a new psock. Rejecting the disconnect of close() by mistake
leaves a socket that is never reset and never freed.

So let the disconnect through and reject the connect() after it. TCP
does the same for listen() in the commit above, the disconnect succeeds
and it is the listen() after it that fails. A TCP socket in TCP_CLOSE
can't be added to a sockmap, and __inet_stream_connect() only calls
sk_prot->connect in TCP_CLOSE. A socket with a psock gets there only
after a disconnect, so no other connect() is affected.

Fixes: b8b8315e39ff ("bpf, sockmap: Remove unhash handler for BPF sockmap usage")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 net/ipv4/tcp_bpf.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 49b34941bf13..37d90ed5d829 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -711,12 +711,23 @@ static struct proto *tcpv6_prot_saved __read_mostly;
 static DEFINE_SPINLOCK(tcpv6_prot_lock);
 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
 
+static int tcp_bpf_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
+			   int addr_len)
+{
+	/*
+	 * Only a socket that was disconnected gets here with a psock. A new
+	 * connection must not reuse it.
+	 */
+	return -EOPNOTSUPP;
+}
+
 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
 				   struct proto *base)
 {
 	prot[TCP_BPF_BASE]			= *base;
 	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
 	prot[TCP_BPF_BASE].close		= sock_map_close;
+	prot[TCP_BPF_BASE].connect		= tcp_bpf_connect;
 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
 	prot[TCP_BPF_BASE].ioctl		= tcp_bpf_ioctl;
-- 
2.43.0
Technical Details
Message-ID: <20261202143337.400831-6-jiayuan.chen@linux.dev>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375855-linux-kernel-netdev=dewy.ru
References: <20261202143337.400831-1-jiayuan.chen@linux.dev>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375855-linux-kernel-netdev=dewy.ru
From: jiayuan.chen@linux.dev (Jiayuan Chen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf v3 5/9] bpf, sockmap: Fix reconnect with a psock by rejecting connect
Date: Wed,  2 Dec 2026 22:30:44 +0800
Organization: Usenet Network
Message-ID: <20261202143337.400831-6-jiayuan.chen@linux.dev>
References: <20261202143337.400831-1-jiayuan.chen@linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1990915"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:80pR9fp1dHn74LwZqW7JtL74dNU= sha256:Ia89/bL7qRNH4XPxfzL49RoPGY2ngQJadRzGyw6VJmg=
	sha1:aXvmiIxb7tpE8Kqof5ExWbHVV9U= sha256:zMI0Tqezl65Ml2w/zWWgmfb21PNlBMBmdflPUy6VVOg=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5B948117674; Wed,  2 Dec 2026 17:37:11 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.051
X-Spam-Status: No, score=-2.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	HK_RANDOM_FROM=1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,
	SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id JIcBb1k6wMeJ;
	Wed,  2 Dec 2026 17:37:08 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-375855-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 2648A117647; Wed,  2 Dec 2026 17:37:00 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id D52BA25A03; Wed,  2 Dec 2026 14:36:57 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796222216;
	b=t0Mw/nXqyOx3RzHbqIrHe59PeMv9sD2BCMzoQea2zTqgX/fUdYYeuADazvexLb9Zfh5l
	 mSNtNzVK7JtQ7E4K+g23LYM2EAjUdMw+PAnzU5t/vfQMVzbUDV1j0UaEp+uhX2Ld0lHia
	 76uOOA+jczPIdJnxe0MmRY1rMjaw3HQFKltWhHGquyaK9h2RkQn8ngOo1XWCfq5GuimAg
	 qBdzO8qrZSkzx3doBaWLYxfdAzO2fwosBnHPy3YborkGbVY8hnmWsN8hlYRU+Uf+bW79M
	 iVS0HUUBAi97BamNIb9gVdD0ZQ7vhP1kzjd4mR+d5gSaJo5KrXrriGOhJkpaK7FII3Q==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796222216;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=y5zbOfP1WkuyfEpX2k9qxE0g0L7m6TCEehGYn8hjuD8=;
	b=mzdkEN2Y0Ie2psZEoXu3aa/4O6Mw3kiLYedIrWYl0yaQj8d/VumsRCr7kR1tJx1Magpb
	 Iz5csMBb1bSb9zQAsWzvIHRfgJPSiYNMdII8RG1gG01GHZ0D+0rD6xo7CwhxiPdyN2lA+
	 j99lqMigX4S6JljH+9jHQCrRyPBfKNaj/Ge5UI/OswW3W7+U/bvFMUE1BfrLsAN0vd39J
	 OxLWgXgUfMf2D/moQ/YEIAl3cek0pt5bWGYnu6/blqm0aipMKPfzwQ5jrK61Kc6Unk/to
	 xwQ0e7L9pW36kOySi9GiU64X4Vppp6BzJ2sQkP6A3liA3EUQ6RoByVPUU8Qiz6M66DQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=q+97rbP5;
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id C3AC0606A33C;
	Wed,  2 Dec 2026 14:36:56 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=q+97rbP5
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.132
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796222211;
	b=fu9CCK49m8+pHMFC7yeVi/r7WMfjWuKBVk9eVIHjd09DIecnhiHI/cYOp80oCmL9JPcQ
	 /cBxch7AM68I2UHFRPyF6IFwf+3sPKB7UfRtztzRcHO6EDXn9EI0TcYzHV9XR4lOTtFcO
	 vumWgW0o/Cu2EFXufGFbBaMm4wp6TWwlSkrU+X0Pwxe1QkdfrhTZvdiHLxoQmbvZdb5ha
	 i/EjkNitcxhUUJ8QjmwkX93vp9P9JlKJgG1qmsZQ1O9KWi559+4s4V1ZRb7clHm2mIEWV
	 4wrTovK4vz+4Qp/FVhpQPnuQeRpvwgXBufz1uXxXeh3c0kAjvlwBXSzgJcbSAmL0VYQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796222211;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=y5zbOfP1WkuyfEpX2k9qxE0g0L7m6TCEehGYn8hjuD8=;
	b=sLdOlaNixg0AyA5uJdb4mYat3azf8ftNpLG7uUIcVcPXYpaQuWBGr8hdAQgPecRqmjj2
	 005AI/J+KDcLPH84NS1SyLDKl5gR+PYeKobw4ZSbTuTWpO6rOAayN357KS0G5+q82zMAw
	 drLhd8INpgS747R2A5DolKGGA16RLm9qqaEpszJ5DTStSlXVL716b8kxozBf4d6nTyKyp
	 cDhzEG7NtE7cxIRKS2+tewAAd3G2p77NmfPLYOfZbwrUMCtVmcTD58o7/7ITFimz0Ubyo
	 6Dtq/KGt5wo3Sl3CNghETAbphQhr2Y3ex0OiTXJ2a3V86vS0CCk7/piw8LQWiEdnDxg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=q+97rbP5;
	arc=none smtp.remote-ip=91.218.175.132
X-Received: from mta0.migadu.com (out-132.mta0.migadu.com [91.218.175.132])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DF98606A322
	for <netdev@vger.kernel.org>; Wed,  2 Dec 2026 14:36:50 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4DF98606A322
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=JZPejC4XUeq/DRrGXohSdRYWgp8YkeoY0fHVoOVc7d0=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1796222208; v=1; x=1796827008;
 b=q+97rbP55JzcmqHjyQyrFq4X9d/lStJm/rjqNR2w6DKZWhdczQ0eWKgqq51OXpu/AeJx3qkp
 //DQjf19vOcFTmROQY0fO/jcds3CcuVdt9SritnT1JGoP88PAFOcbFGtq0vc5pn9gOZAkaZydPW
 1m9CdFNgxVuoWXQqoqrmbQWs=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id f9cba4b66c16aca9;
	Wed, 02 Dec 2026 14:36:47 +0000
X-Mizu-Trace-ID: f9cba4b66c16aca9
X-Migadu-Flow: FLOW_OUT
X-Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Jakub Sitnicki <jakub@cloudflare.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Neal Cardwell <ncardwell@google.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Shuah Khan <shuah@kernel.org>,
	Sechang Lim <rhkrqnwk98@gmail.com>,
	Michal Luczaj <mhal@rbox.co>,
	Mattia Meleleo <mattia.meleleo@coralogix.com>,
	Zijian Zhang <zijianzhang@bytedance.com>,
	linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org
X-Mailer: git-send-email 2.43.0
X-In-Reply-To: <20261202143337.400831-1-jiayuan.chen@linux.dev>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list