With strparser, read_sock advances tp->copied_seq when it pulls skbs off sk_receive_queue, and recvmsg() advances it again when the user reads from ingress_msg. For a skb that the verdict passes back to the same socket, this counted the bytes twice, so commit 36b62df5683c ("bpf: Fix wrong copied_seq calculation") added psock->ingress_bytes to record how many bytes went to self and take them out of the read_sock side. That works for RPC style traffic, where each reply is read before the next one arrives. But psock->copied_seq is advanced on every read_sock call while ingress_bytes is reset on every call, so tp->copied_seq moves forward whenever a new skb arrives, even if the user never calls recvmsg(). Once the parser runs twice before a read, the SK_PASS bytes of the first run are no longer subtracted and tp->copied_seq already covers them. recvmsg() then adds them again and copied_seq passes rcv_nxt. The socket now looks empty to TCP, the parser is not woken for new data anymore and recvmsg() keeps returning -EAGAIN while the skbs sit on sk_receive_queue. tcp_bpf_recvmsg_parser() has the same problem from the other side. It snapshots tp->copied_seq at entry and writes the snapshot plus copied_from_self back at exit. In between, tcp_msg_wait_data() releases the socket lock and tcp_data_ready() runs the verdict of read_skb under it. Both can advance copied_seq after the snapshot. Writing the snapshot back rewinds that progress and leaves copied_seq behind the queued data. tp->copied_seq should only cover bytes that have left the socket: what the verdict redirected or dropped, plus what the user has read. SK_PASS bytes still queued in ingress_msg must not count until recvmsg() hands them out. So let both sides add only their own delta to the current tp->copied_seq instead of recomputing it from a saved value. The read_sock side advances by what this call consumed minus what it passed to self, taking the delta from psock->copied_seq rather than the return value so that the FIN byte is still counted. The recvmsg() side adds copied_from_self, and counts the FIN there as well. Either change alone moves the mismatch to the other side, so they go together. ingress_bytes is also bumped from sk_psock_skb_ingress_enqueue(), which the backlog work reaches without the socket lock for skbs redirected from other sockets. Those bytes are not part of this socket's stream and recvmsg() never adds them back, so subtracting them leaves copied_seq behind. Count only in sk_psock_skb_ingress_self(), where msg->sk is set, which is what recvmsg() checks. A strparser skb has no owner when the verdict runs. sk_psock_skb_ingress_self() sets one with skb_set_owner_r(), and when the enqueue fails after that, the skb is left to the backlog with skb->sk still set. It is not in ingress_bytes, so read_sock has already counted it as consumed. But the backlog picks the path by skb->sk: sk_psock_backlog sk_psock_handle_skb sk_psock_skb_ingress if (skb->sk == sk) sk_psock_skb_ingress_self <- sets msg->sk and recvmsg() counts the bytes again. So once a strparser skb is left to the backlog, it must not take the skb->sk == sk path. Drop the owner with skb_orphan() when the enqueue fails, in sk_psock_skb_ingress() as well, where the backlog sets the owner before the enqueue. This also gives the rmem charge back, and the backlog charges the skb again like any other strparser skb queued there. Picking the path by skb->sk is nasty and will be reworked later. The drop path clears the strparser bit before tcp_eat_skb(), so tcp_eat_skb() no longer skips the skb and adds skb->len to tp->copied_seq. The old code overwrote that at the end of read_sock. With the delta it would stay, so call tcp_eat_skb() first. Fixes: 36b62df5683c ("bpf: Fix wrong copied_seq calculation") Fixes: e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq") Cc: stable@vger.kernel.org Reported-by: Vega Suggested-by: Alexei Starovoitov Co-developed-by: Zhiling Zou Signed-off-by: Zhiling Zou Signed-off-by: Jiayuan Chen --- The BUG_ON(offset < 0) in tcp_collapse() when copied_seq trails the head of sk_receive_queue is not addressed here. A message that spans several segments moves copied_seq back when it is passed. That is fine, the old code did the same and recvmsg() adds the bytes back. --- net/core/skmsg.c | 26 ++++++++++++++++++++------ net/ipv4/tcp_bpf.c | 12 ++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 8a3b8536c0c21..b81abb3da379a 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -574,9 +574,6 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb, return num_sge; } -#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) - psock->ingress_bytes += len; -#endif copied = len; msg->sg.start = 0; msg->sg.size = copied; @@ -616,8 +613,11 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, */ skb_set_owner_r(skb, sk); err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, true); - if (err < 0) + if (err < 0) { kfree(msg); + /* Same as in sk_psock_skb_ingress_self() */ + skb_orphan(skb); + } return err; } @@ -642,8 +642,21 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb */ msg->sk = sk; err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, take_ref); - if (err < 0) + if (err < 0) { kfree(msg); + /* + * A strparser skb comes in without an owner and read_sock counts + * it as consumed once it is left to the backlog. Drop the owner + * again, or sk_psock_skb_ingress() takes the skb->sk == sk path + * and recvmsg() counts it a second time. + */ + if (skb_bpf_strparser(skb)) + skb_orphan(skb); + } +#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) + else + psock->ingress_bytes += len; +#endif return err; } @@ -1061,8 +1074,9 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, case __SK_DROP: default: out_free: - skb_bpf_redirect_clear(skb); + /* tcp_eat_skb() checks the strparser bit, clear it after */ tcp_eat_skb(psock->sk, skb); + skb_bpf_redirect_clear(skb); sock_drop(psock->sk, skb); } diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index 788ce08eef4d7..e59103e77aa5f 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -249,7 +249,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk, struct tcp_sock *tcp; int copied_from_self = 0; int copied = 0; - u32 seq; if (unlikely(flags & MSG_ERRQUEUE)) return inet_recv_error(sk, msg, len); @@ -263,7 +262,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk, lock_sock(sk); tcp = tcp_sk(sk); - seq = tcp->copied_seq; /* We may have received data on the sk_receive_queue pre-accept and * then we can not use read_skb in this context because we haven't * assigned a sk_socket yet so have no link to the ops. The work-around @@ -295,11 +293,10 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk, if (is_fin) { copied = 0; - seq++; + copied_from_self++; goto out; } } - seq += copied_from_self; if (!copied) { long timeo; int data; @@ -341,7 +338,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk, } out: if (!peek) - WRITE_ONCE(tcp->copied_seq, seq); + WRITE_ONCE(tcp->copied_seq, tcp->copied_seq + copied_from_self); tcp_rcv_space_adjust(sk); /* Data on the psock outlives the connection that it came in on */ if (copied > 0 && tcp_can_send_ack(sk)) @@ -756,6 +753,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc, struct sk_psock *psock; struct tcp_sock *tp; int copied = 0; + u32 seq; tp = tcp_sk(sk); rcu_read_lock(); @@ -765,6 +763,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc, goto out; } + seq = psock->copied_seq; psock->ingress_bytes = 0; copied = tcp_read_sock_noack(sk, desc, recv_actor, true, &psock->copied_seq); @@ -775,7 +774,8 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc, * For SK_REDIRECT, we need to ack the frame immediately but for * SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser(). */ - tp->copied_seq = psock->copied_seq - psock->ingress_bytes; + WRITE_ONCE(tp->copied_seq, tp->copied_seq + psock->copied_seq - seq - + psock->ingress_bytes); tcp_rcv_space_adjust(sk); __tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes); out: -- 2.43.0