🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25837

[PATCH bpf v3 4/9] bpf, sockmap: Fix concurrent senders on one psock

#25837
From: Jiayuan Chen
Date: Wed, 02 Dec 2026 14:30
115 lines
13836 bytes
tcp_bpf_sendmsg() keeps the state of the current send in the psock:
the verdict in eval, the byte budget in apply_bytes, the redirect
target in sk_redir and the message being built in cork. The socket lock
does not protect that state all the way through. tcp_bpf_push() drops
it in sk_stream_wait_memory(), and the SK_REDIRECT branch of
tcp_bpf_send_verdict() drops it around tcp_bpf_sendmsg_redir(). A
second sendmsg() on the same socket then runs with the state of the
first one.

The cork is the worst case. The SK_PASS branch pushes psock->cork while
it is still attached. When the push sleeps, the second sender picks the
same cork up through psock->cork, appends to it, runs the prog on it,
and on SK_REDIRECT frees it. The first sender wakes up in
tcp_bpf_push() and writes to a freed sk_msg. The other fields are not
freed, but the second sender sends without a verdict, or with the
verdict and the budget of the first one.

Plain TCP has no state of this kind, and two senders on one stream
socket only get their bytes interleaved. Add a mutex to the psock and
hold it for the whole tcp_bpf_sendmsg(). A second sender waits for it.

tls_sw_sendmsg() does the same with tx_lock, and the wait is the same
as there. The mutex is taken with MSG_DONTWAIT as well, and SO_SNDTIMEO
does not apply to it. A trylock that returns -EAGAIN has nothing to wake
the sender up later, since the send buffer is not full.

sk_psock_backlog() is a sender too when an skb is redirected to the
egress of a socket with an sk_msg prog. So a bit in psock->state that
fails the second sender with -EBUSY is smaller, but it breaks the
backlog and a single user sender that runs into it.

Redirects to another socket go through tcp_bpf_push_locked() and
bpf_tcp_ingress() and do not touch the target's send state, so they are
not affected.

tcp_bpf_send_verdict() already has a few checks for a second sender,
added by earlier fixes. They do no harm with the mutex and are left
as they are, comments included, to keep this patch small.

Fixes: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 include/linux/skmsg.h |  1 +
 net/core/skmsg.c      |  2 ++
 net/ipv4/tcp_bpf.c    | 14 +++++++++++++-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index f0e27b4b929f..a86d58088f2a 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -118,6 +118,7 @@ struct sk_psock {
 				     bool restore);
 	struct proto			*sk_proto;
 	struct mutex			work_mutex;
+	struct mutex			tx_mutex;
 	struct sk_psock_work_state	work_state;
 	struct delayed_work		work;
 	struct sock			*sk_pair;
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index 5c730eb56bc0..2b42b13e3840 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -801,6 +801,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)
 
 	INIT_DELAYED_WORK(&psock->work, sk_psock_backlog);
 	mutex_init(&psock->work_mutex);
+	mutex_init(&psock->tx_mutex);
 	INIT_LIST_HEAD(&psock->ingress_msg);
 	spin_lock_init(&psock->ingress_lock);
 	skb_queue_head_init(&psock->ingress_skb);
@@ -889,6 +890,7 @@ static void sk_psock_destroy(struct work_struct *work)
 	cancel_delayed_work_sync(&psock->work);
 	__sk_psock_zap_ingress(psock);
 	mutex_destroy(&psock->work_mutex);
+	mutex_destroy(&psock->tx_mutex);
 
 	psock_progs_drop(&psock->progs);
 
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 7191b95c7b1c..49b34941bf13 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -602,8 +602,19 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	if (unlikely(!psock))
 		return tcp_sendmsg(sk, msg, size);
 
-	lock_sock(sk);
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
+	/*
+	 * The verdict, apply_bytes, sk_redir and the cork live in the psock
+	 * and span the places where the socket lock is dropped below. They
+	 * can only serve one sender at a time.
+	 */
+	err = mutex_lock_interruptible(&psock->tx_mutex);
+	if (err) {
+		sk_psock_put(sk, psock);
+		return sock_intr_errno(timeo);
+	}
+
+	lock_sock(sk);
 	while (msg_data_left(msg)) {
 		bool enospc = false;
 		u32 copy, osize;
@@ -677,6 +688,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	if (err < 0)
 		err = sk_stream_error(sk, msg->msg_flags, err);
 	release_sock(sk);
+	mutex_unlock(&psock->tx_mutex);
 	sk_psock_put(sk, psock);
 	return copied > 0 ? copied : err;
 }
-- 
2.43.0
Technical Details
Message-ID: <20261202143337.400831-5-jiayuan.chen@linux.dev>
Path: mbox-mo1.pugleaf.net!news-fra.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer05.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375854-linux-kernel-netdev=dewy.ru
References: <20261202143337.400831-1-jiayuan.chen@linux.dev>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-fra.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer05.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375854-linux-kernel-netdev=dewy.ru
From: jiayuan.chen@linux.dev (Jiayuan Chen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf v3 4/9] bpf, sockmap: Fix concurrent senders on one psock
Date: Wed,  2 Dec 2026 22:30:43 +0800
Organization: Usenet Network
Message-ID: <20261202143337.400831-5-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="1990834"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:oJpPEqHMFWrNwa3JWMZQRthpmq0= sha256:oxsiWWotm0M6E4ZkgsjTAyrvfTbeW+pY0i+rDr6fsD8= sha1:8PhwQaSauwbF/AdY34ohZ1irbKs= sha256:NBwNxMWs/tIMUUU89xOfUTgy7ls0dq7hxMG23fI7VmY=
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 89BC5117673; Wed,  2 Dec 2026 17:36:48 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.05
X-Spam-Status: No, score=-2.05 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, URIBL_BLOCKED=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 XrNYVrsmAJbe; Wed,  2 Dec 2026 17:36:45 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375854-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 47227117647; Wed,  2 Dec 2026 17:36:37 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 993462D6A6; Wed,  2 Dec 2026 14:36:28 +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=1796222188; b=UyLRdJv+gaD8V8ze01DMDEOCaucbs0QxrRZzDviJRQw8y5s+Lx7Krvmd2J7HcsCgozYN q4LxVW0HmBAszSkoVNRuGcXfpLCOgIh0TPTIcnaGGvImqbm0PEOKxrD2sYjqNva/xgJ8C 4xhgiB9giP2qhQMUjVGOunjuwNPFd/aQ2O08X+dIxTDo5pQXm6Z0w25Bvoo4qNQ+3MMoL TV1yZddktpzhYbs00heGSXcKyAapDcQPREnC0uZPAraoVdvJBfz/9UEfiOTaAuFtuHNxm 0iMpXzAXNgld1coWnIYil+4X72O1RDN1CImnj/DfXAC85Yw0lZ+WoMHJCh1EFBxVBnQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222188; 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=Wwx/2/SKDaQaEB6yuD6HR6qTTFPUhra8BWaCufL75Ko=; b=JZMEqbJ2JahdI30aR031wGTN4j5HWmTjvBiYpgHRXIeeLcY9CdmoATiJZqJMH+kVikh6 RqjibBTEFV/HC2h4t/DaRwD5d1gHMFPRm8jZ0ksfAPyfsj+btpch5C4F4A9BiRfNUjGf8 bwOesarv8dySa64v1pHKLEFuM8nYNti0YxN1uTLA663EbpZrsJsKTHZpXQ91rfd+E1iFJ QoMYAlzwKKs3pHZtCTQRpv7FL8U+O8ubeBeM8k7kdvIiHENDHkL9HVBY7ud7AxP5zN/Rw gQalcXYf9S9knPS3+k04LjoN69yX3JQxzP/pgKmj3L9mVNXT78Cbu2H+xZLCjTRvvaQ==
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=qySL+1Cb; 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 6FA46606A325; Wed,  2 Dec 2026 14:36:28 +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=qySL+1Cb
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.172
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222182; b=IdOllXVye/E+isOqFu6jW1L0a4QdU3+czqWSFiAfYT8sVnwml4xdhQu66eaRKl0xyyyf KDtrrMamud7uzZ9fU5M/5Kj9+NmOy5HuDAMiXk//RSSf8FlyPl/1DMV9JBvzOLWXf0yjn WZUnuGKqprkjxYBUGihas92VorRVOLoFo50JLyIeR7SfD+G4hA/T5oBLKwOadxUI80AP0 KrHm9JVxsqnKeqPQO+c+sn0cNDL1TZtQF0OXV53ov9+bYrIuIvPyH+ZszOcIohfysW0YD lrfb3WvIQoKiAbVcBtpAF2+t7Qm7124wmc3Law57Z5YOAuvjUUSfJZSUSap1UZu0KMw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222182; 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=Wwx/2/SKDaQaEB6yuD6HR6qTTFPUhra8BWaCufL75Ko=; b=oF6qo1nU6QHbwzpjB1izD6CUCwojGlT7rMj522Rk3a7SbyyuROUo3L3mwv+5QAUp+KW7 XSU0R3sZMtIHx/mtcI+81Jmu3MM5ujvhHi3G0F7kUWbR0Ztl90B/gsWNidTAwttw0nBXC J2EFsn/ztqtQpgKaNTG8Z4IGOvYPfRi2qrtvcahshMblEXxbSCCkLXrdxJIsXNZeeygyI n3nLZ/gqaxpkTMC6MuP204i6DpI1DIyDOQexCo2CeEzPWAzwKGRDkuyIK9oT6iP9cxVGj RKRtaeZk2ypmNXcUltkLgh2HDfTyHxJ5i1EigF6cb1PKV0Hclf08xzysaAN1cmDLZNw==
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=qySL+1Cb; arc=none smtp.remote-ip=95.215.58.172
X-Received: from mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 E4AEA606A323 for <netdev@vger.kernel.org>; Wed,  2 Dec 2026 14:36:21 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E4AEA606A323
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=IBoJL8spZxuRfBSfkXQmVzzGffpo4P1mGPfoTav+Esg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796222180; v=1; x=1796826980; b=qySL+1CbFevSmsL89HJuHNExOiPDm1lWWdP6AMOLdfzOxwWqQzWoFAkK3TJv/L1vdX5QF4f4 vth6ygdsDCQnNDj0e/Ge4pP8PrDwlegAsIVngrCKlDqJ8K7W82bgO/SyU0XV7OO3G05DSjU57fG wayL6wC9D9EFh4XWnxffmAGk=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id ff4ce595a82dc1f7; Wed, 02 Dec 2026 14:36:19 +0000
X-Mizu-Trace-ID: ff4ce595a82dc1f7
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>, Andrii Nakryiko <andrii@kernel.org>, Eduard Zingerman <eddyz87@gmail.com>, Ihor Solodrai <ihor.solodrai@linux.dev>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, 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>, Shuah Khan <shuah@kernel.org>, Sechang Lim <rhkrqnwk98@gmail.com>, Mattia Meleleo <mattia.meleleo@coralogix.com>, Michal Luczaj <mhal@rbox.co>, 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
X-Received-Bytes: 13503