From fa84be67b5bc2999c6962e18c5deee2e90483bfb Mon Sep 17 00:00:00 2001 From: Alexei Starovoitov Date: Mon, 2 Nov 2026 07:21:01 +0000 Subject: bpf, sockmap: Subtract all dropped bytes from the return value When an sk_msg prog returns SK_DROP, tcp_bpf_send_verdict() frees the full message. It then subtracts tosend bytes from *copied, the return value of sendmsg(). But tosend is the number of bytes that the verdict applies to. After bpf_msg_apply_bytes() tosend can be less than the message size. Then sendmsg() reports dropped bytes as sent. Subtract the number of bytes that sk_msg_free() frees. After an error the SK_PASS branch already does this. The SK_REDIRECT branch does the same if the message is not the cork. Nothing changes when tosend is equal to the message size. Otherwise sendmsg() now returns -EACCES if it sends nothing. Fixes: ca70b8baf2bd ("tcp_bpf: Fix the sk_mem_uncharge logic in tcp_bpf_sendmsg") Signed-off-by: Alexei Starovoitov Reviewed-by: Jiayuan Chen Reviewed-by: John Fastabend Link: https://patch.msgid.link/20261102072112.4160195-5-alexei.starovoitov@gmail.com --- net/ipv4/tcp_bpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index b016aa419fb2..36d1bf46353a 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -521,12 +521,12 @@ more_data: break; case __SK_DROP: default: - sk_msg_free(sk, msg); + /* All of it is thrown away, not only tosend bytes */ + *copied -= sk_msg_free(sk, msg) + delta; sk_msg_apply_bytes(psock, tosend); /* The verdict holds for apply_bytes, not for ever */ if (!psock->apply_bytes) psock->eval = __SK_NONE; - *copied -= (tosend + delta); return -EACCES; } -- cgit v1.2.3