Article View: linux.kernel.netdev
Article #10648Re: [PATCH bpf v2 07/15] bpf, sockmap: Check the target of an egress redirect before each send
From: netdev-bot+sashiko
Date: Tue, 03 Nov 2026 21:38
Date: Tue, 03 Nov 2026 21:38
255 lines
17934 bytes
17934 bytes
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 (2) · Low: 3
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The claim: tcp_bpf_redir_allowed() runs only before
tcp_sendmsg_locked().
- [Low] Non-blocking egress redirects to a connecting target now fail
with EINVAL instead of EAGAIN, and the commit message does not say so.
- [Low] The claim: tcp_bpf_sendmsg() strips MSG_FASTOPEN for every psock
send, including __SK_PASS to the sender's own socket, while the…
- [Low] The comment in tcp_bpf_sendmsg() is now inaccurate.
Pre-existing issues:
- [Medium] An egress-redirect sender sleeping in sk_stream_wait_memory()
on a target's wait queue is not woken when the target's owner closes…
- [Medium] tcp_bpf_push_locked() (and bpf_tcp_ingress()) call lock_sock()
on a redirect target that may be a child still in a listener's accept…
--
bpf, sockmap: Check the target of an egress redirect before each send
This adds tcp_bpf_redir_allowed() and calls it in tcp_bpf_push() before
each tcp_sendmsg_locked() call made for an egress redirect. The redirect
fails with -EINVAL when the target has no struct socket or is still
connecting, and with -EPIPE after close(). It also clears MSG_FASTOPEN in
tcp_bpf_sendmsg(), so a redirect cannot enter the target's connect code.
> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> index fd443350f9b81..ef2e15de0fe3b 100644
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c
> @@ -88,6 +88,24 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
> return ret;
> }
>
> +/*
> + * The target of a redirect belongs to somebody else. A child that nobody has
> + * accepted yet, and an orphan, have no struct socket and no wait queue.
> + * tcp_sendmsg_locked() needs both. See sendmsg_locked() in skbuff.c.
> + * The sender holds no reference to the file of the target, which can be closed
> + * whenever tcp_sendmsg_locked() sleeps. sk_stream_wait_connect() and
> + * inet_wait_for_connect() go back to the wait queue when they wake up, so the
> + * sender must not wait for a connect.
> + */
[Severity: Medium]
This isn't a bug introduced by this patch, but the comment mentions
unaccepted children as redirect targets. Can tcp_bpf_push_locked() hold
the lock of such a child while the listener tears it down?
A sockops BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB program can put a child that
is still in the accept queue into a sockmap. sock_map_sk_state_allowed()
accepts TCP_ESTABLISHED:
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
The redirect sender then does:
tcp_bpf_push_locked()
lock_sock(sk);
ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
release_sock(sk);
If the listener is closed in that window, inet_csk_listen_stop() does:
bh_lock_sock(child);
WARN_ON(sock_owned_by_user(child));
sock_hold(child);
Then inet_child_forget() disconnects, orphans and destroys the child
without waiting for the lock owner.
Would the sender's release_sock() then run __release_sock() and
tcp_release_cb() on the torn-down child?
bpf_tcp_ingress() takes lock_sock() on the target in the same way. This
patch makes the egress window shorter, because the lock is now held only
long enough to find sk_socket == NULL and return -EINVAL. The race itself
seems to remain.
> +static bool tcp_bpf_redir_allowed(const struct sock *sk)
> +{
> + if (!sk->sk_socket)
> + return false;
> + if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
> + return tcp_passive_fastopen(sk);
[Severity: Low]
Does this change the errno that non-blocking senders see? Every target in
TCP_SYN_SENT is rejected, whatever the sender's timeout is. One example is
a sockmap member that disconnected and then reconnected.
Before this patch, a sender using MSG_DONTWAIT got timeo == 0 in
tcp_sendmsg_locked(). sk_stream_wait_connect() then returned before
add_wait_queue():
net/core/stream.c:sk_stream_wait_connect() {
...
if (!*timeo_p)
return -EAGAIN;
...
}
So this case never reached the remove_wait_queue() path. Now tcp_bpf_push()
returns -EINVAL before it calls tcp_sendmsg_locked(), so a retryable EAGAIN
becomes EINVAL.
The commit message says the redirect fails "when the sender has to wait
for the connection". A non-blocking sender never waits, though.
Could non-blocking senders keep getting -EAGAIN here? If not, could the
commit message mention the errno change?
> + return true;
> +}
> +
> static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
> int flags, bool uncharge)
> {
> @@ -109,6 +127,10 @@ static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
> page = sg_page(sge);
>
> retry:
> + /* Only a redirect sends data which is not charged to sk */
> + if (!uncharge && unlikely(!tcp_bpf_redir_allowed(sk)))
> + return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
[Severity: Medium]
This check only runs before each tcp_sendmsg_locked() call. Suppose
tcp_sendmsg_locked() sleeps in sk_stream_wait_memory() on an accepted
ESTABLISHED target, and the owner calls close(). sock_orphan() then clears
sk_wq:
write_lock_bh(&sk->sk_callback_lock);
sock_set_flag(sk, SOCK_DEAD);
sk_set_socket(sk, NULL);
sk->sk_wq = NULL;
On wakeup, the loop in sk_stream_wait_memory() goes back to:
while (1) {
sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Can this dereference a NULL sk_wq_raw when SOCK_FASYNC is set on the
target?
The commit message says so: "sk_stream_wait_memory() has this test, but
it can still use sk_wq after the sleep. This patch does not change that."
The next patch in the series, "net: Leave sk_stream_wait_memory() once the
socket is dead", adds this right after sk_wait_event():
if (ret < 0 || sock_flag(sk, SOCK_DEAD))
goto do_error;
After that, the retry check here returns -EPIPE. So the window exists only
at this commit, or in a backport that takes this patch without the next
one.
Could the series be reordered so that fix lands first?
The out: label also skips remove_wait_queue() for a dead socket, which
leaves the on-stack wait entry on the old queue. That skip predates this
series, and no waker was found that could still walk that queue.
[Severity: Medium]
This is a pre-existing issue, but it is related to the sleep above. Is a
redirect sender that sleeps in sk_stream_wait_memory() on the target woken
at all when the target's owner closes it?
In the default close path, __tcp_close() does:
WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
Then it calls tcp_close_state() and tcp_send_fin(), or tcp_set_state(TCP_CLOSE)
plus a reset when data was unread, and then sock_orphan(). None of these
wakes sk_sleep(sk). Only the tcp_disconnect() paths call
sk_error_report().
After sock_orphan(), sk_stream_write_space() does nothing:
if (__sk_stream_is_writeable(sk, 1) && sock) {
tcp_done() also skips sk_state_change() for a SOCK_DEAD socket.
The sender would then stay in TASK_INTERRUPTIBLE until the target's
sk_sndtimeo expires (MAX_SCHEDULE_TIMEOUT by default) or a signal arrives.
All that time it pins the target sock (sock_hold() in
tcp_bpf_send_verdict()) and its psock (sk_psock_get() in
tcp_bpf_sendmsg_redir()).
The check added here runs before tcp_sendmsg_locked(), so it can't help a
sender that is already asleep inside it. The SOCK_DEAD exit added later in
the series only helps after a wakeup, so the hang seems to remain once the
whole series is applied.
> +
> msghdr.msg_flags = flags | MSG_SPLICE_PAGES;
> has_tx_ulp = tls_sw_has_ctx_tx(sk);
> if (has_tx_ulp)
> @@ -555,7 +577,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
> int flags;
>
> /* Don't let internal flags through */
> - flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
> + flags = (msg->msg_flags & ~(MSG_SENDPAGE_DECRYPTED | MSG_FASTOPEN));
[Severity: Low]
This clears MSG_FASTOPEN for every psock send. That includes __SK_PASS to
the sender's own socket in tcp_bpf_send_verdict():
ret = tcp_bpf_push(sk, msg, tosend, flags, true);
The fallback without a psock keeps the flag:
if (unlikely(!psock))
return tcp_sendmsg(sk, msg, size);
So the same sendmsg(MSG_FASTOPEN) on a connected socket returns -EISCONN
without a psock and succeeds with one. Is that difference intended?
The commit message does describe it ("send() with MSG_FASTOPEN sends the
data. Today it fails with EISCONN."). It also matches the behaviour before
c5c37af6ecad, when do_tcp_sendpages() ignored MSG_FASTOPEN.
tcp_bpf_push() builds its msghdr with no msg_name, so a fastopen connect
could never succeed on this path anyway. This may well be fine as is.
[Severity: Low]
This isn't a bug, but the comment above still says "Don't let internal
flags through". The mask now also clears MSG_FASTOPEN, which is a UAPI
send flag rather than an internal one.
The reason for clearing it is that the target's connect code uses
sk_socket and sk_wq without a file reference. That reason is only in the
commit message, and the comment on tcp_bpf_redir_allowed() does not
mention MSG_FASTOPEN.
Could the comment be updated to say why MSG_FASTOPEN is cleared?
> flags |= MSG_NO_SHARED_FRAGS;
>
> psock = sk_psock_get(sk);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261102072112.4160195-1-alexei.starovoitov%40gmail.com
Technical Details
Message-ID:
<179374188061.488855.10389706003415159913@kernel.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-355988-linux-kernel-netdev=dewy.ruReferences:
<20261102072112.4160195-8-alexei.starovoitov@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-355988-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev Subject: Re: [PATCH bpf v2 07/15] bpf, sockmap: Check the target of an egress redirect before each send Date: Tue, 03 Nov 2026 21:38:00 +0000 Organization: Usenet Network Message-ID: <179374188061.488855.10389706003415159913@kernel.org> References: <20261102072112.4160195-8-alexei.starovoitov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="3267509"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:CEIQa37/QSWX0XEgS8VYvGwoo0I= sha256:4p0U1kub2BsD2chWaRyPCQO6Ewv7Q0covNeSc8QOQDI= sha1:oRJ+mYao3xFmL/fWW/kUkPB2+3Y= sha256:lRohiGwqgxTU6pZtV7K2KDC5nS8c3i214kxvJnxpPDE= 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 238F0112A24; Wed, 4 Nov 2026 00:41:31 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org 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 bEd0N0fN4oTD; Wed, 4 Nov 2026 00:41:28 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-355988-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id A409E112032; Wed, 4 Nov 2026 00:41:19 +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 DA4E62CE56; Tue, 3 Nov 2026 21:38:08 +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=1793741888; b=DgxxAm/tfKoRvjxGqXFyun53mvKyWeqXVPASZbjs11+AIkmdLvD0kNP4Wl3IvEI6q5Df s0dA8lx4hhHlo1S+zEZyKAw1nNLf6eFEoED1J0ZpblvbhmnAucggKB6HPnorn3Ai51IBb bi5SNQWXGZ8+DDzWdmz+n6+/UgrV9C/N+SSn6eMdBhQDgMBvv9UoV7IfVi0CB0AFWwSEa uh8dcXTqDwoG0mPTFtWINtzCLDSDp3WNSfpl4p4zQANqupMTj33YIfy3okSiMUDtheKI8 qn42NYmu6uIXaDL1FQfjpFoEY4h3t719ZD22dOZTGKP2gOdRy7PskQ7sslNrcVEwddw== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793741888; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=qT2LBx85XctbDsC5iPBV6BjNvzAGZkSFB96TXC2ej8Y=; b=iNYpOPzgfK+ZWB8oigmByipzLeGWQxhoXvefig233O1fQztSwoQpNTqxnfwkzDn4xW8J 4APsVHkoCfKO38xAQrnlzZjf09US6D2antcyaMFLZ8QFuNs/PCUr7E/9ZOZLLCol94hYw N7I0W71GYupSCjuaTZZYJfWLDeZ0c0T9mR7w+gsJT9UZkfaLQnPV+8zmuFGPNVYP9N0ha 6mHrfy4Jc6i2xyiUWb08VYyYXc9GksGp+DRFh37Yk7f3UMN4I3+liDqG9tNNBOtaLrvcf 69X5djyOqjA465vfn2M/pfiQqXJrDL6h4+amLCMw2ng7y/I0mQMH/WFb+4XhnBlJPmw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hc7YS2us; 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 B15D2606A32F; Tue, 3 Nov 2026 21:38:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hc7YS2us X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793741882; b=XvZ6hTrdAex3q+4KaBQGOJp2QTmvGgFZkKMDEJYEVZNS30focfZ4iJyG/62KlRkUUCys zbluTtvMwsNUXHaWW6dSycFXlcKdz2y12tdPonnoS9dBOTU9MUdoREogw9iGF0hCcjLSj PqkpfDsfy2Iu1atrJ7lVLZj5nOktjA//MtZutKVcBmihWe73goY8svqmkBzGyY5GgrPpl vLsXhKbRg4zuSNWGpgAh8aBBQlNZos39IkTA0DVK25je5+kU2k5YFM5w5g+hXEisGIvQT 50MK4PPjjw4m+wwhMDVVUWbQz69UD03SORRCWxGdNpgx7osa4DTbBca6DWaJEEDz08w== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793741882; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=qT2LBx85XctbDsC5iPBV6BjNvzAGZkSFB96TXC2ej8Y=; b=kxjzc7fsMwh0RfCVZiCcjaozowv2q2J36R9+LOLZ38G40YR5KXvCrFZahy6qw5L6RdL0 6sZPsMA66pe5yzWm+8HglemRNooFI/+ghBzwWJ36w45Kw9lhfGjaHlkJhDTiZgT3qf3Zw 2km9WWAzSpf8EHy0mtroOTryIqQDwVqGvpZkL5LxCIY83Tdknp9yiJBqnluhBuzUdyrLd 2CU5EtsDcJD4kvHeOkRDZiozr28SMP4pxMJ4YLeLCHOAEVCMwzZ2SQ1nr5ABymitBe6kn FHeOQPRvhfnT+yJ1RlqKZp/MblT/JM4h5l4QbPuyCA45EEt2n2SisBeTD4k4iFcPW/Q== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hc7YS2us; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80482606A329; Tue, 3 Nov 2026 21:38:02 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1AE881F000C1; Tue, 3 Nov 2026 21:38:01 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793741882; bh=qT2LBx85XctbDsC5iPBV6BjNvzAGZkSFB96TXC2ej8Y=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=hc7YS2usCDXg+HZI0HNVy6w1PHaBMOjd5GZC5J5F/0w63jXcBILQdfy62z5h8SAbp svWQrn7PJ+l+vGcOWTzcDaZtO0NlGXW+keU6nT5vNKYXDZIzADtqiRGwmLgyq8ecoz NWhVLmi08uH0wBJHpukxhQBfRKlYCD0FCzzSl7iMFLirwVe/LhDufmndhez3Qcg9h+ PoErElIo7yZ4f95LlQnglrPGmQVptjkoQYS6Vrm17luYvfroL05Ta9d+QZGpz0ND0n 4cxEOGZC3S5oQNGhwDOt1G+sPGDSil6MvK8hyCGxn6K2O3wZa/ArPXKlcNqYWLZoBr Mj5pALxLkigJw== X-Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, john.fastabend@gmail.com, jakub@cloudflare.com, jiayuan.chen@linux.dev, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, kuniyu@google.com, willemb@google.com, matttbe@kernel.org, mptcp@lists.linux.dev, mason@kernel.org, josef@toxicpanda.com X-In-Reply-To: <20261102072112.4160195-8-alexei.starovoitov@gmail.com> X-sashiko-severity: Medium 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