From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
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>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Willem de Bruijn <willemb@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
Subject: [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect
Date: Tue, 1 Dec 2026 20:18:53 +0800 [thread overview]
Message-ID: <20261201122055.269577-6-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261201122055.269577-1-jiayuan.chen@linux.dev>
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 49b34941bf13c..37d90ed5d8298 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
next prev parent reply other threads:[~2026-12-01 12:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-12-01 12:18 ` Jiayuan Chen [this message]
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
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=20261201122055.269577-6-jiayuan.chen@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mattia.meleleo@coralogix.com \
--cc=memxor@gmail.com \
--cc=mhal@rbox.co \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rhkrqnwk98@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
--cc=zijianzhang@bytedance.com \
/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
all inboxes | Powered by JetHome®