Skip to contentSkip to conversation

vsock/virtio: make the credit mechanism more robust

PATCH In review Stefano Garzarella 2 participants 7 messages Oct 17, 2019 – Oct 18, 2019

Subsystems (6)
lore
Share & tools
lore

Stefano Garzarella

author maintainer
#0 lore raw

cover vsock/virtio: make the credit mechanism more robust

This series makes the credit mechanism implemented in the virtio-vsock devices more robust.
Patch 1 sends an update to the remote peer when the buf_alloc change.
Patch 2 prevents a malicious peer (especially the guest) can consume all the memory of the other peer, discarding packets when the credit available is not respected.

Stefano Garzarella (2):
vsock/virtio: send a credit update when buffer size is changed vsock/virtio: discard packets if credit is not respected

 net/vmw_vsock/virtio_transport_common.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
author maintainer
#1 lore raw

PATCH 1/2 vsock/virtio: send a credit update when buffer size is changed

When the user application set a new buffer size value, we should update the remote peer about this change, since it uses this information to calculate the credit available.

---
 net/vmw_vsock/virtio_transport_common.c | 3 +++
 1 file changed, 3 insertions(+)
net/vmw_vsock/virtio_transport_common.c +3 -0
index a666ef8fc54e..db127a69f5c3 100644
@@ -458,6 +458,9 @@ void virtio_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
 		vvs->buf_size_max = val;
 	vvs->buf_size = val;
 	vvs->buf_alloc = val;
+
+	virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_STREAM,
+					    NULL);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_set_buffer_size);
 
Signature · 1 line
2.21.0
author maintainer
#2 lore raw

PATCH 1/2 vsock/virtio: send a credit update when buffer size is changed

In reply to Stefano Garzarella

When the user application set a new buffer size value, we should update the remote peer about this change, since it uses this information to calculate the credit available.

---
 net/vmw_vsock/virtio_transport_common.c | 3 +++
 1 file changed, 3 insertions(+)
net/vmw_vsock/virtio_transport_common.c +3 -0
index a666ef8fc54e..db127a69f5c3 100644
@@ -458,6 +458,9 @@ void virtio_transport_set_buffer_size(struct vsock_sock *vsk, u64 val)
 		vvs->buf_size_max = val;
 	vvs->buf_size = val;
 	vvs->buf_alloc = val;
+
+	virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_STREAM,
+					    NULL);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_set_buffer_size);
 
Signature · 1 line
2.21.0
author maintainer
#3 lore raw

PATCH 2/2 vsock/virtio: discard packets if credit is not respected

In reply to Stefano Garzarella

If the remote peer doesn't respect the credit information (buf_alloc, fwd_cnt), sending more data than it can send, we should drop the packets to prevent a malicious peer from using all of our memory.

This is patch follows the VIRTIO spec: "VIRTIO_VSOCK_OP_RW data packets MUST only be transmitted when the peer has sufficient free buffer space for the payload"

---
 net/vmw_vsock/virtio_transport_common.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +11 -3
index db127a69f5c3..481f7f8a1655 100644
@@ -204,10 +204,14 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 	return virtio_transport_get_ops()->send_pkt(pkt);
 }
 
-static void virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
+static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
 					struct virtio_vsock_pkt *pkt)
 {
+	if (vvs->rx_bytes + pkt->len > vvs->buf_alloc)
+		return false;
+
 	vvs->rx_bytes += pkt->len;
+	return true;
 }
 
 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
@@ -879,14 +883,18 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 			      struct virtio_vsock_pkt *pkt)
 {
 	struct virtio_vsock_sock *vvs = vsk->trans;
-	bool free_pkt = false;
+	bool can_enqueue, free_pkt = false;
 
 	pkt->len = le32_to_cpu(pkt->hdr.len);
 	pkt->off = 0;
 
 	spin_lock_bh(&vvs->rx_lock);
 
-	virtio_transport_inc_rx_pkt(vvs, pkt);
+	can_enqueue = virtio_transport_inc_rx_pkt(vvs, pkt);
+	if (!can_enqueue) {
+		free_pkt = true;
+		goto out;
+	}
 
 	/* Try to copy small packets into the buffer of last packet queued,
 	 * to avoid wasting memory queueing the entire buffer with a small
Signature · 1 line
2.21.0
author maintainer
#4 lore raw

PATCH 2/2 vsock/virtio: discard packets if credit is not respected

In reply to Stefano Garzarella

If the remote peer doesn't respect the credit information (buf_alloc, fwd_cnt), sending more data than it can send, we should drop the packets to prevent a malicious peer from using all of our memory.

This is patch follows the VIRTIO spec: "VIRTIO_VSOCK_OP_RW data packets MUST only be transmitted when the peer has sufficient free buffer space for the payload"

---
 net/vmw_vsock/virtio_transport_common.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +11 -3
index db127a69f5c3..481f7f8a1655 100644
@@ -204,10 +204,14 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 	return virtio_transport_get_ops()->send_pkt(pkt);
 }
 
-static void virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
+static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
 					struct virtio_vsock_pkt *pkt)
 {
+	if (vvs->rx_bytes + pkt->len > vvs->buf_alloc)
+		return false;
+
 	vvs->rx_bytes += pkt->len;
+	return true;
 }
 
 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
@@ -879,14 +883,18 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
 			      struct virtio_vsock_pkt *pkt)
 {
 	struct virtio_vsock_sock *vvs = vsk->trans;
-	bool free_pkt = false;
+	bool can_enqueue, free_pkt = false;
 
 	pkt->len = le32_to_cpu(pkt->hdr.len);
 	pkt->off = 0;
 
 	spin_lock_bh(&vvs->rx_lock);
 
-	virtio_transport_inc_rx_pkt(vvs, pkt);
+	can_enqueue = virtio_transport_inc_rx_pkt(vvs, pkt);
+	if (!can_enqueue) {
+		free_pkt = true;
+		goto out;
+	}
 
 	/* Try to copy small packets into the buffer of last packet queued,
 	 * to avoid wasting memory queueing the entire buffer with a small
Signature · 1 line
2.21.0
maintainer
#5 lore raw
In reply to Stefano Garzarella

From: Stefano Garzarella <[email protected]>
Date: Thu, 17 Oct 2019 14:44:01 +0200

Quoted 7 lines

This series makes the credit mechanism implemented in the virtio-vsock devices more robust.
Patch 1 sends an update to the remote peer when the buf_alloc change.
Patch 2 prevents a malicious peer (especially the guest) can consume all the memory of the other peer, discarding packets when the credit available is not respected.

Series applied, thanks.

maintainer
#6 lore raw
In reply to Stefano Garzarella

From: Stefano Garzarella <[email protected]>
Date: Thu, 17 Oct 2019 14:44:01 +0200

Quoted 7 lines

This series makes the credit mechanism implemented in the virtio-vsock devices more robust.
Patch 1 sends an update to the remote peer when the buf_alloc change.
Patch 2 prevents a malicious peer (especially the guest) can consume all the memory of the other peer, discarding packets when the credit available is not respected.

Series applied, thanks.