[PATCH bpf 06/11] bpf, sockmap: Fix reconnect with a psock by rejecting disconnect
From: Jiayuan Chen
Date: Sat Nov 21 2026 - 05:35:32 EST
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. Do what TLS did in commit
5071a1e606b3 ("net: tls: explicitly disallow disconnect") and reject the
disconnect with -EOPNOTSUPP.
Unlike TLS, some callers have to be let through. None of them leads to
a reconnect with old data:
- A child can be added to a sockmap before accept(), e.g. by a sockops
prog. When the listener is closed, inet_child_forget() disconnects
the child and frees it right after, without looking at the return
value. Such a child has no sk_socket.
- close() with a zero linger time disconnects through sk_prot, which
is still tcp_bpf as long as somebody else holds the psock.
sock_map_close() sets SK_PSOCK_CLOSING for it.
- A listener is disconnected by shutdown(SHUT_RD) and
connect(AF_UNSPEC), and has no data.
Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
---
include/linux/skmsg.h | 1 +
net/core/sock_map.c | 1 +
net/ipv4/tcp_bpf.c | 21 +++++++++++++++++++++
3 files changed, 23 insertions(+)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index 98f71c6d0af73..7e8256b2b4d6c 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -70,6 +70,7 @@ enum sk_psock_state_bits {
SK_PSOCK_RX_STRP_ENABLED,
SK_PSOCK_STRP_DONE_PENDING,
SK_PSOCK_TX_BUSY,
+ SK_PSOCK_CLOSING,
};
struct sk_psock_link {
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 6747b3cc1cac1..6fc1d29ac6ed5 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -1763,6 +1763,7 @@ void sock_map_close(struct sock *sk, long timeout)
sock_map_remove_links(sk, psock);
rcu_read_unlock();
sk_psock_stop(psock);
+ sk_psock_set_state(psock, SK_PSOCK_CLOSING);
release_sock(sk);
cancel_delayed_work_sync(&psock->work);
sk_psock_put(sk, psock);
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 077b65927ac8d..9715716518c49 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -710,12 +710,33 @@ 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_disconnect(struct sock *sk, int flags)
+{
+ struct sk_psock *psock;
+ bool reject;
+
+ /*
+ * A new connection must not reuse the psock. Let through a child
+ * that nobody has accepted yet, a socket in close() and a listener.
+ */
+ rcu_read_lock();
+ psock = sk_psock(sk);
+ reject = psock && sk->sk_socket && sk->sk_state != TCP_LISTEN &&
+ !sk_psock_test_state(psock, SK_PSOCK_CLOSING);
+ rcu_read_unlock();
+ if (reject)
+ return -EOPNOTSUPP;
+
+ return tcp_disconnect(sk, flags);
+}
+
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].disconnect = tcp_bpf_disconnect;
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