Skip to content

Commit 52423da

Browse files
Geliang Tangintel-lab-lkp
authored andcommitted
mptcp: implement .read_sock
nvme_tcp_try_recv() needs to call .read_sock interface of struct proto_ops, but it is not implemented in MPTCP. This patch implements it with reference to __mptcp_recvmsg_mskq(). v2: - first check the sk_state (Matt), but not look for the end of the end of a connection like TCP in __tcp_read_sock(): if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) break; This will cause a use-after-free error: BUG: KASAN: slab-use-after-free in mptcp_read_sock. v3: - Use sk->sk_rcvbuf instead of INT_MAX as the max len. v4: - invoke __mptcp_move_skbs. Reviewed-by: Hannes Reinecke <[email protected]> Signed-off-by: Geliang Tang <[email protected]>
1 parent d446696 commit 52423da

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

net/mptcp/protocol.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,72 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
39573957
return mask;
39583958
}
39593959

3960+
static int mptcp_recv_skb(struct sock *sk)
3961+
{
3962+
if (skb_queue_empty(&sk->sk_receive_queue))
3963+
__mptcp_move_skbs(sk);
3964+
3965+
return 0;
3966+
}
3967+
3968+
/*
3969+
* Note:
3970+
* - It is assumed that the socket was locked by the caller.
3971+
*/
3972+
static int mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
3973+
sk_read_actor_t recv_actor)
3974+
{
3975+
struct mptcp_sock *msk = mptcp_sk(sk);
3976+
size_t len = sk->sk_rcvbuf;
3977+
struct sk_buff *skb, *tmp;
3978+
int copied = 0;
3979+
3980+
if (sk->sk_state == TCP_LISTEN)
3981+
return -ENOTCONN;
3982+
skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
3983+
u32 offset = MPTCP_SKB_CB(skb)->offset;
3984+
u32 data_len = skb->len - offset;
3985+
u32 size = min_t(size_t, len - copied, data_len);
3986+
int count;
3987+
3988+
count = recv_actor(desc, skb, offset, size);
3989+
if (count <= 0) {
3990+
if (!copied)
3991+
copied = count;
3992+
break;
3993+
}
3994+
3995+
copied += count;
3996+
msk->bytes_consumed += count;
3997+
3998+
if (count < data_len) {
3999+
MPTCP_SKB_CB(skb)->offset += count;
4000+
MPTCP_SKB_CB(skb)->map_seq += count;
4001+
break;
4002+
}
4003+
4004+
/* avoid the indirect call, we know the destructor is sock_wfree */
4005+
skb->destructor = NULL;
4006+
atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4007+
sk_mem_uncharge(sk, skb->truesize);
4008+
sk_eat_skb(sk, skb);
4009+
4010+
mptcp_recv_skb(sk);
4011+
4012+
if (copied >= len)
4013+
break;
4014+
}
4015+
4016+
mptcp_rcv_space_adjust(msk, copied);
4017+
4018+
if (copied > 0) {
4019+
mptcp_recv_skb(sk);
4020+
mptcp_cleanup_rbuf(msk, copied);
4021+
}
4022+
4023+
return copied;
4024+
}
4025+
39604026
static const struct proto_ops mptcp_stream_ops = {
39614027
.family = PF_INET,
39624028
.owner = THIS_MODULE,
@@ -3977,6 +4043,7 @@ static const struct proto_ops mptcp_stream_ops = {
39774043
.recvmsg = inet_recvmsg,
39784044
.mmap = sock_no_mmap,
39794045
.set_rcvlowat = mptcp_set_rcvlowat,
4046+
.read_sock = mptcp_read_sock,
39804047
};
39814048

39824049
static struct inet_protosw mptcp_protosw = {
@@ -4081,6 +4148,7 @@ static const struct proto_ops mptcp_v6_stream_ops = {
40814148
.compat_ioctl = inet6_compat_ioctl,
40824149
#endif
40834150
.set_rcvlowat = mptcp_set_rcvlowat,
4151+
.read_sock = mptcp_read_sock,
40844152
};
40854153

40864154
static struct proto mptcp_v6_prot;

0 commit comments

Comments
 (0)