From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.125 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222210; b=aJAlW8b7JbpKIgpyfDHdn3g5R1hrO+AP+wZwdfW7IL6W4mXDz5spGoIBEhxN4GCgPK79 XoMbbZvZPq2wJbqm/RElbUzxljRkqiKpMvjWextJXd6kmACJJaQPrNGatXdRi1EcLJjmR z20V5r4Od/c6I8CppOeSS5dsN9i4YwcWeTU9lr8bqDUGUPplvEqf86ZyjKJC9gHTTyABz pM0wwYJsoaMA06IdbIKfhPuA6U8CWtfG2rjtWuk2eXdnBUxEEfCrrn8PgSfLUxZ01uaw8 CWE0tT8/f8Q8vjFD3Ra8h0+7ezThNIf7gePyhBhDHunC1ZS0jVeBpsMQC7Z5gQsDLwA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222210; 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=b3LIW4slZ8Dx4nuQ0DtInKt2rN2Z0+ZIsO8G0KhV1MiunPtzDt+zz5YGwh5lm/LvCHjT ub09tSfhGQaAmHhuEPXjtBjq+XGwn0ndaJiEESs5Apr6zt3gTwu899bjrKctIC1CAf5+c 3prFBz2fvVoNf44QBqWpsC2mRkwjiS6w+At7C93haDqScw5/835dlPVjm61OfL/HRkX2r Cqsxq+FjUFIcpwPfN1MQQXDcC8/+RZHLfQuGNzNwm2JWGWhYD/akKZUZyzwCmkKkE5WtE eI6krSCwqnNfhgy/4nvN31OiOhauCjYJuV0f55H/r9RD2Khx94GbW7/C3WuAvKcEtig== 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.125 Received: from mta0.migadu.com (out-125.mta0.migadu.com [91.218.175.125]) (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 0985E606A31A for ; Wed, 2 Dec 2026 14:36:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0985E606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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-Envelope-To: bpf@vger.kernel.org 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: bpf@vger.kernel.org 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 From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , John Fastabend , Jakub Sitnicki , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Kuniyuki Iwashima , Neal Cardwell , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Sechang Lim , Michal Luczaj , Mattia Meleleo , Zijian Zhang , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org 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 Message-ID: <20261202143337.400831-6-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202143337.400831-1-jiayuan.chen@linux.dev> References: <20261202143337.400831-1-jiayuan.chen@linux.dev> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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