[PATCH bpf 05/11] bpf, sockmap: Fix concurrent senders on one psock
From: Jiayuan Chen
Date: Sat Nov 21 2026 - 05:34:55 EST
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. There is no use for it with
sockmap either, so refuse it. Mark the psock busy for the whole
tcp_bpf_sendmsg() and return -EBUSY to a second sender.
The second sender could wait instead, but that takes a wait queue with
signal and timeout handling. Concurrent sends on one socket are rare, so
keep the fix simple.
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.
Fixes: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
---
include/linux/skmsg.h | 1 +
net/ipv4/tcp_bpf.c | 11 +++++++++++
2 files changed, 12 insertions(+)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index 276692e060d53..98f71c6d0af73 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -69,6 +69,7 @@ enum sk_psock_state_bits {
SK_PSOCK_TX_ENABLED,
SK_PSOCK_RX_STRP_ENABLED,
SK_PSOCK_STRP_DONE_PENDING,
+ SK_PSOCK_TX_BUSY,
};
struct sk_psock_link {
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 7191b95c7b1c7..077b65927ac8d 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -602,6 +602,16 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
if (unlikely(!psock))
return tcp_sendmsg(sk, msg, size);
+ /*
+ * 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.
+ */
+ if (test_and_set_bit(SK_PSOCK_TX_BUSY, &psock->state)) {
+ sk_psock_put(sk, psock);
+ return -EBUSY;
+ }
+
lock_sock(sk);
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
while (msg_data_left(msg)) {
@@ -677,6 +687,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);
+ clear_bit(SK_PSOCK_TX_BUSY, &psock->state);
sk_psock_put(sk, psock);
return copied > 0 ? copied : err;
}
--
2.43.0