Re: [PATCH bpf 09/11] bpf, sockmap: Fix apply_bytes when a redirect sends only a part

From: bot+bpf-ci

Date: Sat Nov 21 2026 - 06:11:24 EST


> bpf, sockmap: Fix apply_bytes when a redirect sends only a part

TL;DR: The apply_bytes give-back in tcp_bpf_send_verdict() may leave a source
socket permanently failing sendmsg() with -EPIPE after its redirect target
closes, since the restored redirect verdict is never cleared.

> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c

[ ... ]

> @@ -566,6 +563,21 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
> msg = NULL;
> ret = 0;
> }
> + /*
> + * Bytes that were not sent and come back, in msg or in the
> + * next sendmsg(), are still covered by the verdict.
> + */
> + if (apply && sent < tosend && !cork) {
> + psock->apply_bytes += tosend - sent;
> + if (eval == __SK_REDIRECT) {
> + psock->eval = eval;
> + psock->sk_redir = sk_redir;
> + eval = __SK_NONE;
> + }
> + }
> + if (eval == __SK_REDIRECT)
> + sock_put(sk_redir);
> + sock_put(sk_redir);
> break;

Can this give-back leave the source socket unable to send after the redirect
target goes away?

The give-back runs for every short redirect, whatever the error. Some
errors from tcp_bpf_sendmsg_redir() are permanent and send nothing
(sent == 0):

if (!READ_ONCE(sk->sk_socket))
return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
psock = sk_psock_get(sk);
if (unlikely(!psock))
return -EPIPE;

They happen when the target was closed (__tcp_close() -> sock_orphan()) or
removed from the sockmap. The same goes for an egress target whose
connection was reset, where tcp_sendmsg_locked() returns -EPIPE.

In those cases sk_msg_apply_bytes(psock, tosend) takes off tosend bytes and
the new code adds tosend - 0 back, so psock->apply_bytes ends up at its old
value. When apply_bytes had reached 0, the code also puts back
psock->eval = __SK_REDIRECT and psock->sk_redir = <dead target>.

Nothing else clears that state. The only writers of psock->eval,
apply_bytes and sk_redir are this function, sk_psock_msg_verdict() (which
runs only when psock->eval == __SK_NONE), the cork_bytes reset in
tcp_bpf_sendmsg(), and sk_psock_destroy(). So every later sendmsg() on the
source socket follows the same path:

tcp_bpf_sendmsg() -> tcp_bpf_send_verdict()
psock->eval == __SK_REDIRECT, prog not run
tcp_bpf_sendmsg_redir(dead target) -> -EPIPE, sent = 0
apply_bytes restored, verdict restored
-> sk_stream_error() -> -EPIPE (plus SIGPIPE unless MSG_NOSIGNAL)

The source socket's own connection is healthy, but it can no longer send
while it is in the sockmap. The sk_msg program never runs again, so it
cannot pick a new target or drop. The dead target's struct sock also stays
pinned by psock->sk_redir until the psock is destroyed.

Before this patch the verdict always used up tosend bytes, even on failure.
A redirect to a dead target failed at most about apply_bytes / tosend times.
Then psock->eval went back to __SK_NONE, the prog ran again, and with the
closed target gone from the map the lookup failed and the prog returned
SK_DROP (or chose another target).

The give-back is right for short sends the user is expected to retry
(-EAGAIN, -ENOMEM from bpf_tcp_ingress(), a partial ret == 0). Should it be
skipped, or should the verdict be cleared, when the target cannot take data
any more (for example -EPIPE)?

No later commit in the series changes net/ipv4/tcp_bpf.c; the rest of the
series only adds selftests.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44896626218