From: netdev-bot+sashiko@kernel.org
To: alexei.starovoitov@gmail.com
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
Subject: Re: [PATCH bpf v2 14/15] bpf, sockmap: Keep the rest of the cork after a redirect
Date: Tue, 03 Nov 2026 21:38:04 +0000 [thread overview]
Message-ID: <179374188416.488855.8465787275984951547@kernel.org> (raw)
In-Reply-To: <20261102072112.4160195-15-alexei.starovoitov@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 (1) · High: 0 · Medium: 1 (3) · Low: 0
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 fix is incomplete: the cork tail is still lost when
another sender installs a new cork while the lock is dropped.
Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but the
SK_PASS branch of `tcp_bpf_send_verdict()` pushes a message that is…
- [Medium] This problem wasn't introduced by this patch, but the selftest
`test_txmsg_cork_hangs()` in…
- [Medium] This problem wasn't introduced by this patch, but when
`tcp_bpf_sendmsg_redir()` fails for a cork in…
- [Medium] This problem wasn't introduced by this patch, but in the
__SK_REDIRECT branch of `tcp_bpf_send_verdict()`,…
--
bpf, sockmap: Keep the rest of the cork after a redirect
When a verdict covers only part of a cork after bpf_msg_apply_bytes(),
the SK_REDIRECT branch of tcp_bpf_send_verdict() used to free the bytes
that were left over along with the cork. This patch puts the cork back
into psock->cork when bytes remain, unless another thread installed a
new cork, so the sk_msg program runs again on the rest.
> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> index b1071b0960cf5..788ce08eef4d7 100644
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c
> @@ -544,6 +544,14 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
> if (!cork)
> *copied -= free;
> }
[Severity: Medium]
This is a pre-existing issue, but what happens when tcp_bpf_sendmsg_redir()
fails for a cork here?
The ret < 0 path frees the whole sk_msg. It skips the *copied adjustment
because cork is true. The new restore check is then skipped because
msg->sg.size is 0, and the if (cork) block runs:
sk_msg_free(sk, msg);
kfree(msg);
msg = NULL;
ret = 0;
tcp_bpf_sendmsg() then returns copied > 0 ? copied : err with copied
unchanged. So sendmsg() reports success for bytes that this call put into
the cork and that were then thrown away. A non-cork message with the same
failure lowers *copied and returns the error.
Failures that can get here include -EPIPE when the target has no psock,
-EINVAL/-EPIPE from tcp_bpf_redir_allowed() in tcp_bpf_push(), -ENOMEM
from bpf_tcp_ingress(), and errors from tcp_sendmsg_locked().
The commit message says this stays unchanged ("If the redirect of a cork
fails, sendmsg() still returns success and frees it."). So does the earlier
patch "bpf, sockmap: Check the target of an egress redirect before each
send".
Should the cork case also subtract the freed bytes from *copied and keep
the error?
> + /*
> + * The verdict was for tosend bytes. The rest goes back, unless
> + * another thread made a new cork while the lock was released.
> + */
> + if (cork && msg->sg.size && !psock->cork) {
[Severity: Medium]
Is the tail still lost if another sender installs a new cork while the lock
is dropped?
Earlier in the __SK_REDIRECT branch, the cork is detached and the lock is
released:
if (psock->cork == msg) {
cork = true;
psock->cork = NULL;
}
release_sock(sk);
A second thread in tcp_bpf_sendmsg() on the same socket then sees
psock->cork == NULL and uses the on-stack tmp. The first thread's call to
sk_msg_apply_bytes(psock, tosend) used up apply_bytes, so psock->eval was
reset to __SK_NONE and the prog runs on tmp. If the prog calls
bpf_msg_cork_bytes() with a size larger than tmp, the top of
tcp_bpf_send_verdict() allocates a new psock->cork.
When the first thread takes the lock again, this check is false. The
if (cork) block then frees the unsent tail with ret = 0 and does not reduce
*copied, and tcp_bpf_sendmsg() returns the positive copied count.
That is the symptom the commit message describes ("the rest is lost, and
sendmsg() reports it as sent"). The prog also never runs on those bytes,
although bpf_msg_apply_bytes() says it should.
The commit message acknowledges this ("If the psock has a new cork after
lock_sock(), the kernel still frees the rest"). The earlier patch "bpf,
sockmap: Check that the redirected message is the cork" also says corks are
not safe for two senders.
Is this case meant to stay out of scope, or should *copied at least be
reduced by the freed tail?
> + psock->cork = msg;
> + cork = false;
> + }
[Severity: Critical]
This isn't a bug introduced by this patch, but putting the tail back here
adds a new way into a race that already exists in the SK_PASS branch. Can
a restored cork be freed while tcp_bpf_push() is still using it?
After this code runs, we reach more_data with psock->cork == msg. If the
next verdict is SK_PASS:
tcp_bpf_send_verdict()
case __SK_PASS:
if (!psock->apply_bytes)
psock->eval = __SK_NONE;
...
ret = tcp_bpf_push(sk, msg, tosend, flags, true);
tcp_sendmsg_locked()
sk_stream_wait_memory()
sk_wait_event() <- drops and re-takes the socket lock
The cork stays in psock->cork during the push. A second sender in
tcp_bpf_sendmsg() can take the lock and pick it up:
if (psock->cork) {
msg_tx = psock->cork;
psock->cork_bytes is 0 and psock->eval is __SK_NONE, so the second sender
runs the prog on the shared msg:
- On SK_REDIRECT it detaches the cork, redirects it without the lock, and
later calls kfree(msg).
- On SK_PASS it pushes the same element, then calls put_page() and
sg_init_table() on it.
The first thread then wakes up in tcp_bpf_push() with stale sge, page, off
and size locals, and runs:
msg->sg.size -= ret;
sge->offset += ret;
sge->length -= ret;
...
if (!sge->length) {
put_page(page);
Could this cause a use-after-free of the kfree()d sk_msg and a double
put_page(), and send the same bytes twice?
The earlier patch "bpf, sockmap: Check that the redirected message is the
cork" already says "a cork is still not safe for two senders on one
socket".
Would it fix this to detach psock->cork in the SK_PASS branch before any
push that can drop the lock, and attach it again under the lock afterwards,
as the SK_REDIRECT branch does?
[Severity: Medium]
This is a pre-existing issue, but now that the tail goes to more_data, can
some of the restored bytes get a different verdict than the program asked
for?
In the __SK_REDIRECT branch, the whole planned tosend is charged before
the redirect runs:
sk_msg_apply_bytes(psock, tosend);
if (!psock->apply_bytes) {
/* Clean up before releasing the sock lock. */
eval = psock->eval;
psock->eval = __SK_NONE;
psock->sk_redir = NULL;
}
bpf_tcp_ingress() can move only part of it. Once one element has moved, a
later __sk_rmem_schedule() failure breaks out with ret == 0, because copied
is not zero:
if (!__sk_rmem_schedule(sk, size, false)) {
if (!copied)
ret = -ENOMEM;
break;
}
For example, take an 8192-byte cork with apply_bytes 6144 where only the
first 4096-byte element moves. The prog run at more_data then decides all
4096 remaining bytes, including 2048 that the redirect verdict covered.
Non-cork messages already have the same accounting problem. Should
sk_msg_apply_bytes() be charged with the bytes actually sent rather than
tosend?
> if (cork) {
> sk_msg_free(sk, msg);
> kfree(msg);
[Severity: Medium]
This is a pre-existing issue in the selftests, but is there a test that
would catch the data loss this patch fixes?
The third case in test_txmsg_cork_hangs() in
tools/testing/selftests/bpf/test_sockmap.c reaches this path:
txmsg_redir = 1;
txmsg_apply = 4097;
txmsg_cork = 4097;
test_send_large(opt, cgrp);
With txmsg_cork set, msg_loop() waits only 300ms per select(). An
incomplete receive becomes errno = -EIO. The rx child in sendmsg_test()
then hides that error:
if (err && err != -EDATAINTEGRITY && txmsg_cork)
err = 0;
So the test passes both when the cork tail is freed (before this patch)
and when it is put back and sent (after this patch).
Could a test be added that checks how many bytes the peer receives?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261102072112.4160195-1-alexei.starovoitov%40gmail.com
next prev parent reply other threads:[~2026-11-03 21:38 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 7:20 [PATCH bpf v2 00/15] bpf, sockmap: Fixes for verdicts, redirects, corks and recvmsg() Alexei Starovoitov
2026-11-02 7:20 ` [PATCH bpf v2 01/15] bpf, sockmap: Write sg.start when recvmsg() faults Alexei Starovoitov
2026-11-02 8:32 ` Jiayuan Chen
2026-11-02 7:20 ` [PATCH bpf v2 02/15] bpf, sockmap: Don't let an SK_DROP verdict stick Alexei Starovoitov
2026-11-02 8:45 ` Jiayuan Chen
2026-11-02 18:47 ` John Fastabend
2026-11-02 7:21 ` [PATCH bpf v2 03/15] bpf, sockmap: Don't let an SK_PASS verdict cover the next message Alexei Starovoitov
2026-11-02 18:51 ` John Fastabend
2026-11-02 22:44 ` Alexei Starovoitov
2026-11-03 21:37 ` netdev-bot+sashiko
2026-11-02 7:21 ` [PATCH bpf v2 04/15] bpf, sockmap: Subtract all dropped bytes from the return value Alexei Starovoitov
2026-11-02 11:19 ` Jiayuan Chen
2026-11-02 19:12 ` John Fastabend
2026-11-02 7:21 ` [PATCH bpf v2 05/15] bpf, sockmap: Clear the resume state of a stopped psock Alexei Starovoitov
2026-11-02 11:40 ` Jiayuan Chen
2026-11-02 19:16 ` John Fastabend
2026-11-02 7:21 ` [PATCH bpf v2 06/15] bpf, sockmap: Give no progs to a TCP listener Alexei Starovoitov
2026-11-02 12:00 ` Jiayuan Chen
2026-11-02 19:31 ` John Fastabend
2026-11-03 21:37 ` netdev-bot+sashiko
2026-11-02 7:21 ` [PATCH bpf v2 07/15] bpf, sockmap: Check the target of an egress redirect before each send Alexei Starovoitov
2026-11-02 12:32 ` Jiayuan Chen
2026-11-02 16:18 ` Alexei Starovoitov
2026-11-02 19:36 ` John Fastabend
2026-11-03 21:38 ` netdev-bot+sashiko
2026-11-02 7:21 ` [PATCH bpf v2 08/15] net: Leave sk_stream_wait_memory() once the socket is dead Alexei Starovoitov
2026-11-02 12:54 ` Jiayuan Chen
2026-11-02 7:21 ` [PATCH bpf v2 09/15] bpf, sockmap: Check the TCP state in recvmsg() Alexei Starovoitov
2026-11-02 15:15 ` Jiayuan Chen
2026-11-02 16:57 ` Matthieu Baerts
2026-11-03 21:38 ` netdev-bot+sashiko
2026-11-02 7:21 ` [PATCH bpf v2 10/15] bpf, sockmap: Reset the copy position when corking Alexei Starovoitov
2026-11-03 1:39 ` Jiayuan Chen
2026-11-02 7:21 ` [PATCH bpf v2 11/15] bpf, sockmap: Don't copy 0 bytes into a full cork Alexei Starovoitov
2026-11-03 1:41 ` Jiayuan Chen
2026-11-02 7:21 ` [PATCH bpf v2 12/15] bpf, sockmap: Clear the redirect target before the sk_msg prog runs Alexei Starovoitov
2026-11-03 1:58 ` Jiayuan Chen
2026-11-02 7:21 ` [PATCH bpf v2 13/15] bpf, sockmap: Check that the redirected message is the cork Alexei Starovoitov
2026-11-03 2:07 ` Jiayuan Chen
2026-11-02 7:21 ` [PATCH bpf v2 14/15] bpf, sockmap: Keep the rest of the cork after a redirect Alexei Starovoitov
2026-11-03 2:25 ` Jiayuan Chen
2026-11-03 21:38 ` netdev-bot+sashiko [this message]
2026-11-02 7:21 ` [PATCH bpf v2 15/15] bpf, sockmap: Clear the end mark of elements added by push and pop Alexei Starovoitov
2026-11-03 2:51 ` Jiayuan Chen
2026-11-02 20:11 ` [PATCH bpf v2 00/15] bpf, sockmap: Fixes for verdicts, redirects, corks and recvmsg() John Fastabend
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179374188416.488855.8465787275984951547@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=jakub@cloudflare.com \
--cc=jiayuan.chen@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=josef@toxicpanda.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=mason@kernel.org \
--cc=matttbe@kernel.org \
--cc=memxor@gmail.com \
--cc=mptcp@lists.linux.dev \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox