Skip to contentSkip to conversation

virtio_net: validate device stats reply records before use

PATCH In review Michael Bommarito 2 participants 4 messages Jul 11

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_net: validate device stats reply records before use

__virtnet_get_hw_stats() walks the device statistics reply buffer with "for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size))", using each record's device-supplied hdr->size as the stride without checking that a full struct virtio_net_stats_reply_hdr remains, that hdr->size is nonzero and matches the expected size for hdr->type, or that the record fits within res_size. A backend that returns hdr->size == 0 spins the loop forever; a short or oversized size drives out-of-bounds reads in virtnet_fill_stats().

Impact: a malicious or compromised virtio-net backend hangs the CPU running the guest's device-statistics query in an infinite loop (hdr->size == 0), or drives an out-of-bounds read of the reply buffer. This matters most for a confidential guest, where the host is outside the trust boundary.

Validate each record before use: require a full header in the remaining bytes, a nonzero hdr->size that is at least the header size and matches the size expected for hdr->type, and that the record fits within res_size; stop the walk otherwise. Add virtnet_stats_reply_size() for the per-type size.

Fixes 941168f8b40e ("virtio_net: support device stats")
Assisted-by Claude:claude-opus-4-8
---
 drivers/net/virtio_net.c | 42 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +40 -2
index 3e2a5876c6c8c..9cbe40d218cc4 100644
@@ -3532,6 +3532,7 @@ static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
 	return err;
 }
 
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should
@@ -3546,6 +3547,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	bool ok;
 	int ret;
 
+
 	/* Caller should know better */
 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
 
@@ -4927,6 +4929,32 @@ static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
 	}
 }
 
+static int virtnet_stats_reply_size(u8 type)
+{
+	switch (type) {
+	case VIRTIO_NET_STATS_TYPE_REPLY_CVQ:
+		return sizeof(struct virtio_net_stats_cvq);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC:
+		return sizeof(struct virtio_net_stats_rx_basic);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM:
+		return sizeof(struct virtio_net_stats_rx_csum);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO:
+		return sizeof(struct virtio_net_stats_rx_gso);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED:
+		return sizeof(struct virtio_net_stats_rx_speed);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC:
+		return sizeof(struct virtio_net_stats_tx_basic);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM:
+		return sizeof(struct virtio_net_stats_tx_csum);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO:
+		return sizeof(struct virtio_net_stats_tx_gso);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED:
+		return sizeof(struct virtio_net_stats_tx_speed);
+	default:
+		return sizeof(struct virtio_net_stats_reply_hdr);
+	}
+}
+
 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 				  struct virtnet_stats_ctx *ctx,
 				  struct virtio_net_ctrl_queue_stats *req,
@@ -4936,7 +4964,7 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	struct scatterlist sgs_in, sgs_out;
 	void *p;
 	u32 qid;
-	int ok;
+	int hdr_size, ok, remaining;
 
 	sg_init_one(&sgs_out, req, req_size);
 	sg_init_one(&sgs_in, reply, res_size);
@@ -4948,8 +4976,17 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	if (!ok)
 		return ok;
 
-	for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
+	for (p = reply; p - reply < res_size; p += hdr_size) {
+		remaining = res_size - (p - reply);
+		if (remaining < sizeof(*hdr))
+			return -EINVAL;
+
 		hdr = p;
+		hdr_size = le16_to_cpu(hdr->size);
+		if (hdr_size < virtnet_stats_reply_size(hdr->type) ||
+		    hdr_size > remaining)
+			return -EINVAL;
+
 		qid = le16_to_cpu(hdr->vq_index);
 		virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
 	}
@@ -7305,3 +7342,4 @@ module_exit(virtio_net_driver_exit);
 MODULE_DEVICE_TABLE(virtio, id_table);
 MODULE_DESCRIPTION("Virtio network driver");
 MODULE_LICENSE("GPL");
+
Signature · 1 line
2.53.0
#1 lore raw
Quoted 9 lines · Michael Bommarito

__virtnet_get_hw_stats() walks the device statistics reply buffer with "for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size))", using each record's device-supplied hdr->size as the stride without checking that a full struct virtio_net_stats_reply_hdr remains, that hdr->size is nonzero and matches the expected size for hdr->type, or that the record fits within res_size. A backend that returns hdr->size == 0 spins the loop forever; a short or oversized size drives out-of-bounds reads in virtnet_fill_stats().

Quoted 5 lines

Impact: a malicious or compromised virtio-net backend hangs the CPU running the guest's device-statistics query in an infinite loop (hdr->size == 0), or drives an out-of-bounds read of the reply buffer. This matters most for a confidential guest, where the host is outside the trust boundary.

Why does it "matter most", or at all, there?
Host can always deny guest service. In fact, this is how cloud vendors charge their clients, by denying service to whoever did not pay them.

Quoted 4 lines

Validate each record before use: require a full header in the remaining bytes, a nonzero hdr->size that is at least the header size and matches the size expected for hdr->type, and that the record fits within res_size; stop the walk otherwise. Add virtnet_stats_reply_size() for the per-type size.

I'm all for making things easier to debug even when the device is buggy. But I'm not inclined to add tons of hard to maintain code to that end, and I would be worried broken hosts will come to rely on drivers working around them.

Quoted 22 lines
Fixes 941168f8b40e ("virtio_net: support device stats")
Assisted-by Claude:claude-opus-4-8
---
 drivers/net/virtio_net.c | 42 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +1 -0
index 3e2a5876c6c8c..9cbe40d218cc4 100644
@@ -3532,6 +3532,7 @@ static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
 	return err;
 }
 
+
 /*
  * Send command via the control virtqueue and check status.  Commands
  * supported by the hypervisor, as indicated by feature bits, should

drivers/net/virtio_net.c static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd · line 3553

Quoted 7 lines
drivers/net/virtio_net.c +1 -0
@@ -3546,6 +3547,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	bool ok;
 	int ret;
 
+
 	/* Caller should know better */
 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
 

we don't need this.

Quoted 51 lines
drivers/net/virtio_net.c +31 -2
@@ -4927,6 +4929,32 @@ static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
 	}
 }
 
+static int virtnet_stats_reply_size(u8 type)
+{
+	switch (type) {
+	case VIRTIO_NET_STATS_TYPE_REPLY_CVQ:
+		return sizeof(struct virtio_net_stats_cvq);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC:
+		return sizeof(struct virtio_net_stats_rx_basic);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM:
+		return sizeof(struct virtio_net_stats_rx_csum);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO:
+		return sizeof(struct virtio_net_stats_rx_gso);
+	case VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED:
+		return sizeof(struct virtio_net_stats_rx_speed);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC:
+		return sizeof(struct virtio_net_stats_tx_basic);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM:
+		return sizeof(struct virtio_net_stats_tx_csum);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO:
+		return sizeof(struct virtio_net_stats_tx_gso);
+	case VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED:
+		return sizeof(struct virtio_net_stats_tx_speed);
+	default:
+		return sizeof(struct virtio_net_stats_reply_hdr);
+	}
+}
+
 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 				  struct virtnet_stats_ctx *ctx,
 				  struct virtio_net_ctrl_queue_stats *req,
@@ -4936,7 +4964,7 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	struct scatterlist sgs_in, sgs_out;
 	void *p;
 	u32 qid;
-	int ok;
+	int hdr_size, ok, remaining;
 
 	sg_init_one(&sgs_out, req, req_size);
 	sg_init_one(&sgs_in, reply, res_size);
@@ -4948,8 +4976,17 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	if (!ok)
 		return ok;
 
-	for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
+	for (p = reply; p - reply < res_size; p += hdr_size) {
+		remaining = res_size - (p - reply);
+		if (remaining < sizeof(*hdr))
+			return -EINVAL;

drivers/net/virtio_net.c static int __virtnet_get_hw_stats(struct virtnet_info *vi, · line 4992

Quoted 10 lines
drivers/net/virtio_net.c +6 -0
+
 		hdr = p;
+		hdr_size = le16_to_cpu(hdr->size);
+		if (hdr_size < virtnet_stats_reply_size(hdr->type) ||
+		    hdr_size > remaining)
+			return -EINVAL;
+
 		qid = le16_to_cpu(hdr->vq_index);
 		virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
 	}

That's a lot of fragile code for unclear benefit.

Quoted 7 lines
drivers/net/virtio_net.c +1 -0
@@ -7305,3 +7342,4 @@ module_exit(virtio_net_driver_exit);
 MODULE_DEVICE_TABLE(virtio, id_table);
 MODULE_DESCRIPTION("Virtio network driver");
 MODULE_LICENSE("GPL");
+
Signature · 1 line
2.53.0
#2 lore raw
Quoted 3 lines · Michael S. Tsirkin

Why does it "matter most", or at all, there?
Host can always deny guest service. In fact, this is how cloud vendors charge their clients, by denying service to whoever did not pay them.

...

Quoted 4 lines

I'm all for making things easier to debug even when the device is buggy. But I'm not inclined to add tons of hard to maintain code to that end, and I would be worried broken hosts will come to rely on drivers working around them.

I am always confused by the CoCo threat model to be honest, since it seems like some people care a lot about maximalist reliance on the contract and other people are more practical about how many other vectors exist anyway. No hard feelings if you want to NACK, but at least it's documented publicly now for people to consider.

Thanks,
Mike

#3 lore raw
Quoted 11 lines · Michael Bommarito
Quoted 3 lines · Michael S. Tsirkin

Why does it "matter most", or at all, there?
Host can always deny guest service. In fact, this is how cloud vendors charge their clients, by denying service to whoever did not pay them.

...

Quoted 4 lines

I'm all for making things easier to debug even when the device is buggy. But I'm not inclined to add tons of hard to maintain code to that end, and I would be worried broken hosts will come to rely on drivers working around them.

I am always confused by the CoCo threat model to be honest,

Confidential computing? It's vague at points, given the term covers a lot of different hardware. But one thing is clear - it's about confidentiality. DoS by host is empathically outside the threat model. On any virtualization platform I know without exception, host can just exit the VM, done, service denied.

Quoted 4 lines

since it
seems like some people care a lot about maximalist reliance on the contract and other people are more practical about how many other vectors exist anyway.

I don't really know what "vectors" or "the contract" are here.

Making a guest recover from a misbehaving device has as much a chance to reduce security as increase it. So the only benefit is robustness for users/developers, not security. And that has to be weighted against the maintainance cost of the change. This one is too costly, I judge.

Quoted 5 lines

No hard feelings if you want to NACK, but at
least it's documented publicly now for people to consider.

Thanks,
Mike