Skip to content

Commit 36b62df

Browse files
mrpreMartin KaFai Lau
authored andcommitted
bpf: Fix wrong copied_seq calculation
'sk->copied_seq' was updated in the tcp_eat_skb() function when the action of a BPF program was SK_REDIRECT. For other actions, like SK_PASS, the update logic for 'sk->copied_seq' was moved to tcp_bpf_recvmsg_parser() to ensure the accuracy of the 'fionread' feature. It works for a single stream_verdict scenario, as it also modified sk_data_ready->sk_psock_verdict_data_ready->tcp_read_skb to remove updating 'sk->copied_seq'. However, for programs where both stream_parser and stream_verdict are active (strparser purpose), tcp_read_sock() was used instead of tcp_read_skb() (sk_data_ready->strp_data_ready->tcp_read_sock). tcp_read_sock() now still updates 'sk->copied_seq', leading to duplicate updates. In summary, for strparser + SK_PASS, copied_seq is redundantly calculated in both tcp_read_sock() and tcp_bpf_recvmsg_parser(). The issue causes incorrect copied_seq calculations, which prevent correct data reads from the recv() interface in user-land. We do not want to add new proto_ops to implement a new version of tcp_read_sock, as this would introduce code complexity [1]. We could have added noack and copied_seq to desc, and then called ops->read_sock. However, unfortunately, other modules didn’t fully initialize desc to zero. So, for now, we are directly calling tcp_read_sock_noack() in tcp_bpf.c. [1]: https://lore.kernel.org/bpf/[email protected] Fixes: e5c6de5 ("bpf, sockmap: Incorrectly handling copied_seq") Suggested-by: Jakub Sitnicki <[email protected]> Signed-off-by: Jiayuan Chen <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 0532a79 commit 36b62df

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

include/linux/skmsg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct sk_psock {
9191
struct sk_psock_progs progs;
9292
#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
9393
struct strparser strp;
94+
u32 copied_seq;
95+
u32 ingress_bytes;
9496
#endif
9597
struct sk_buff_head ingress_skb;
9698
struct list_head ingress_msg;

include/net/tcp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ void tcp_get_info(struct sock *, struct tcp_info *);
729729
/* Read 'sendfile()'-style from a TCP socket */
730730
int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
731731
sk_read_actor_t recv_actor);
732+
int tcp_read_sock_noack(struct sock *sk, read_descriptor_t *desc,
733+
sk_read_actor_t recv_actor, bool noack,
734+
u32 *copied_seq);
732735
int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
733736
struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off);
734737
void tcp_read_done(struct sock *sk, size_t len);
@@ -2599,6 +2602,11 @@ struct sk_psock;
25992602
#ifdef CONFIG_BPF_SYSCALL
26002603
int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
26012604
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
2605+
#ifdef CONFIG_BPF_STREAM_PARSER
2606+
struct strparser;
2607+
int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
2608+
sk_read_actor_t recv_actor);
2609+
#endif /* CONFIG_BPF_STREAM_PARSER */
26022610
#endif /* CONFIG_BPF_SYSCALL */
26032611

26042612
#ifdef CONFIG_INET

net/core/skmsg.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
549549
return num_sge;
550550
}
551551

552+
#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
553+
psock->ingress_bytes += len;
554+
#endif
552555
copied = len;
553556
msg->sg.start = 0;
554557
msg->sg.size = copied;
@@ -1144,6 +1147,10 @@ int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
11441147
if (!ret)
11451148
sk_psock_set_state(psock, SK_PSOCK_RX_STRP_ENABLED);
11461149

1150+
if (sk_is_tcp(sk)) {
1151+
psock->strp.cb.read_sock = tcp_bpf_strp_read_sock;
1152+
psock->copied_seq = tcp_sk(sk)->copied_seq;
1153+
}
11471154
return ret;
11481155
}
11491156

net/ipv4/tcp.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,12 +1565,13 @@ EXPORT_SYMBOL(tcp_recv_skb);
15651565
* or for 'peeking' the socket using this routine
15661566
* (although both would be easy to implement).
15671567
*/
1568-
int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
1569-
sk_read_actor_t recv_actor)
1568+
static int __tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
1569+
sk_read_actor_t recv_actor, bool noack,
1570+
u32 *copied_seq)
15701571
{
15711572
struct sk_buff *skb;
15721573
struct tcp_sock *tp = tcp_sk(sk);
1573-
u32 seq = tp->copied_seq;
1574+
u32 seq = *copied_seq;
15741575
u32 offset;
15751576
int copied = 0;
15761577

@@ -1624,9 +1625,12 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
16241625
tcp_eat_recv_skb(sk, skb);
16251626
if (!desc->count)
16261627
break;
1627-
WRITE_ONCE(tp->copied_seq, seq);
1628+
WRITE_ONCE(*copied_seq, seq);
16281629
}
1629-
WRITE_ONCE(tp->copied_seq, seq);
1630+
WRITE_ONCE(*copied_seq, seq);
1631+
1632+
if (noack)
1633+
goto out;
16301634

16311635
tcp_rcv_space_adjust(sk);
16321636

@@ -1635,10 +1639,25 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
16351639
tcp_recv_skb(sk, seq, &offset);
16361640
tcp_cleanup_rbuf(sk, copied);
16371641
}
1642+
out:
16381643
return copied;
16391644
}
1645+
1646+
int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
1647+
sk_read_actor_t recv_actor)
1648+
{
1649+
return __tcp_read_sock(sk, desc, recv_actor, false,
1650+
&tcp_sk(sk)->copied_seq);
1651+
}
16401652
EXPORT_SYMBOL(tcp_read_sock);
16411653

1654+
int tcp_read_sock_noack(struct sock *sk, read_descriptor_t *desc,
1655+
sk_read_actor_t recv_actor, bool noack,
1656+
u32 *copied_seq)
1657+
{
1658+
return __tcp_read_sock(sk, desc, recv_actor, noack, copied_seq);
1659+
}
1660+
16421661
int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
16431662
{
16441663
struct sk_buff *skb;

net/ipv4/tcp_bpf.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,42 @@ static int tcp_bpf_assert_proto_ops(struct proto *ops)
646646
ops->sendmsg == tcp_sendmsg ? 0 : -ENOTSUPP;
647647
}
648648

649+
#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
650+
int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
651+
sk_read_actor_t recv_actor)
652+
{
653+
struct sock *sk = strp->sk;
654+
struct sk_psock *psock;
655+
struct tcp_sock *tp;
656+
int copied = 0;
657+
658+
tp = tcp_sk(sk);
659+
rcu_read_lock();
660+
psock = sk_psock(sk);
661+
if (WARN_ON_ONCE(!psock)) {
662+
desc->error = -EINVAL;
663+
goto out;
664+
}
665+
666+
psock->ingress_bytes = 0;
667+
copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
668+
&psock->copied_seq);
669+
if (copied < 0)
670+
goto out;
671+
/* recv_actor may redirect skb to another socket (SK_REDIRECT) or
672+
* just put skb into ingress queue of current socket (SK_PASS).
673+
* For SK_REDIRECT, we need to ack the frame immediately but for
674+
* SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
675+
*/
676+
tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
677+
tcp_rcv_space_adjust(sk);
678+
__tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
679+
out:
680+
rcu_read_unlock();
681+
return copied;
682+
}
683+
#endif /* CONFIG_BPF_STREAM_PARSER */
684+
649685
int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
650686
{
651687
int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;

0 commit comments

Comments
 (0)