bpf, sockmap: Do not inc copied_seq when PEEK flag set

[ Upstream commit da9e915eaf ]

When data is peek'd off the receive queue we shouldn't considered it
copied from tcp_sock side. When we increment copied_seq this will confuse
tcp_data_ready() because copied_seq can be arbitrarily increased. From
application side it results in poll() operations not waking up when
expected.

Notice tcp stack without BPF recvmsg programs also does not increment
copied_seq.

We broke this when we moved copied_seq into recvmsg to only update when
actual copy was happening. But, it wasn't working correctly either before
because the tcp_data_ready() tried to use the copied_seq value to see
if data was read by user yet. See fixes tags.

Fixes: e5c6de5fa0 ("bpf, sockmap: Incorrectly handling copied_seq")
Fixes: 04919bed94 ("tcp: Introduce tcp_read_skb()")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Link: https://lore.kernel.org/bpf/20230926035300.135096-3-john.fastabend@gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
John Fastabend 2023-09-25 20:52:59 -07:00 committed by Greg Kroah-Hartman
parent 46052a9885
commit c024db9603
1 changed files with 3 additions and 1 deletions

View File

@ -217,6 +217,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
int *addr_len) int *addr_len)
{ {
struct tcp_sock *tcp = tcp_sk(sk); struct tcp_sock *tcp = tcp_sk(sk);
int peek = flags & MSG_PEEK;
u32 seq = tcp->copied_seq; u32 seq = tcp->copied_seq;
struct sk_psock *psock; struct sk_psock *psock;
int copied = 0; int copied = 0;
@ -306,7 +307,8 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
copied = -EAGAIN; copied = -EAGAIN;
} }
out: out:
WRITE_ONCE(tcp->copied_seq, seq); if (!peek)
WRITE_ONCE(tcp->copied_seq, seq);
tcp_rcv_space_adjust(sk); tcp_rcv_space_adjust(sk);
if (copied > 0) if (copied > 0)
__tcp_cleanup_rbuf(sk, copied); __tcp_cleanup_rbuf(sk, copied);