Skip to contentSkip to conversation

virtio_net: fix out-of-bounds accesses and a heap info leak via the device stats reply

PATCH Applied Lianjie Wang 3 participants 10 messages Nov 16 – Nov 20

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

v3 cover virtio_net: fix out-of-bounds accesses and a heap info leak via the device stats reply

With the statistics reply, a device can make the driver write past the ethtool -S output array (patch 1), leak stale heap or loop forever (patch 2), and read past the reply buffer (patch 3). Patch 4 returns an error when the statistics command fails.

Claude Opus 5.5 was used to audit the driver and draft the patches.

v3:
- Rebase onto v7.4-rc3.
- Add patches 2 to 4 for the issues raised in the review [1].

[1] https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Lianjie Wang (4):
virtio_net: reject stats replies for queues outside the request
virtio_net: zero the stats reply buffer
virtio_net: keep reads of stats records within the reply buffer
virtio_net: report a failed stats command as an error

 drivers/net/virtio_net.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.56.1
#1 lore raw

PATCH v3 1/4 virtio_net: reject stats replies for queues outside the request

__virtnet_get_hw_stats() passes the vq_index of each record in the device's statistics reply to virtnet_fill_stats() without checking it.

For ethtool -S, virtnet_fill_stats() uses vq_index as an offset into the output array, which the ethtool core sized for curr_queue_pairs. A record with the vq_index of a higher queue can make the driver write past the end of the array.

For the per-queue netlink stats, the queue type also comes from vq_index. A receive record in reply to a request for a transmit queue makes the driver fill the struct netdev_queue_stats_tx on the stack as if it were a struct netdev_queue_stats_rx. If the device offers VIRTIO_NET_STATS_TYPE_RX_SPEED, this writes one counter to the eight bytes after the tx struct.

Fix this by rejecting the reply when the vq_index of a record lies outside the range of queues in the request. The control vq is checked separately, since the vqs of the queue pairs beyond curr_queue_pairs lie between that range and the control vq.

Claude Opus 5.5 was used to audit the driver and draft this patch. To reproduce, the virtio-net device in QEMU was modified to offer device statistics and to answer the request of ethtool -S with a record for a nonexistent receive queue. On an unmodified kernel, KASAN reports a write past the ethtool -S output array:

  BUG: KASAN: vmalloc-out-of-bounds in virtnet_fill_stats+0x6c1/0xf20
  Write of size 8 at addr ffffc90000085140 by task ethtool_stats/158
  Call Trace:
   virtnet_fill_stats+0x6c1/0xf20
   virtnet_get_hw_stats+0x5d6/0x7c0
   virtnet_get_ethtool_stats+0x11b/0x920
   ethtool_get_stats+0x150/0x1f0
   dev_ethtool+0xaaa/0x1be0
  The buggy address belongs to a 1-page vmalloc region starting at 0xffffc90000085000 allocated at ethtool_get_stats+0x120/0x1f0

With this patch, the driver rejects the same reply. When ethtool -L leaves queue pairs unused, the driver also rejects a record for the first unused receive queue, while a well-formed reply fills the statistics as before. The drivers/net/stats.py selftest passes when the device answers with well-formed records.

Fixes 941168f8b40e ("virtio_net: support device stats")
Fixes d888f04c09bb ("virtio-net: support queue stat")

---
v3:
- No functional changes.
v2: https://lore.kernel.org/all/[email protected]/
- Add a comment and a sentence on why the control vq is checked apart from the range of the other queues.
- Add the KASAN report and a test with unused queue pairs.
v1: https://lore.kernel.org/all/[email protected]/

 drivers/net/virtio_net.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +11 -2
index ed20e76fbc4c..57d7a98ecb57 100644
@@ -4973,7 +4973,8 @@ static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 				  struct virtnet_stats_ctx *ctx,
 				  struct virtio_net_ctrl_queue_stats *req,
-				  int req_size, void *reply, int res_size)
+				  int req_size, void *reply, int res_size,
+				  int first_vq, int last_vq, bool enable_cvq)
 {
 	struct virtio_net_stats_reply_hdr *hdr;
 	struct scatterlist sgs_in, sgs_out;
@@ -4994,6 +4995,13 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
 		hdr = p;
 		qid = le16_to_cpu(hdr->vq_index);
+		/* The vqs of the queue pairs beyond curr_queue_pairs lie
+		 * between last_vq and the control vq.
+		 */
+		if ((qid < first_vq || qid > last_vq) &&
+		    !(enable_cvq && qid == vi->max_queue_pairs * 2))
+			return -EINVAL;
+
 		virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
 	}
 
@@ -5073,7 +5081,8 @@ static int virtnet_get_hw_stats(struct virtnet_info *vi,
 	if (enable_cvq)
 		virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
 
-	ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
+	ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply,
+				    res_size, first_vq, last_vq, enable_cvq);
 
 	kfree(req);
 	kfree(reply);
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH net v3 1/4] virtio_net: reject stats replies for queues outside the request

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

__virtnet_get_hw_stats() passes the vq_index of each record in the device's statistics reply to virtnet_fill_stats() without checking it.

For ethtool -S, virtnet_fill_stats() uses vq_index as an offset into the output array, which the ethtool core sized for curr_queue_pairs. A record with the vq_index of a higher queue can make the driver write past the end of the array.

For the per-queue netlink stats, the queue type also comes from vq_index. A receive record in reply to a request for a transmit queue makes the driver fill the struct netdev_queue_stats_tx on the stack as if it were a struct netdev_queue_stats_rx. If the device offers VIRTIO_NET_STATS_TYPE_RX_SPEED, this writes one counter to the eight bytes after the tx struct.

Quoted 6 lines

[ ... ]

Fixes 941168f8b40e ("virtio_net: support device stats")
Fixes d888f04c09bb ("virtio-net: support queue stat")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/4 virtio_net: zero the stats reply buffer

In reply to Lianjie Wang

virtnet_get_hw_stats() allocates the reply buffer of the device statistics command with kmalloc(), and __virtnet_get_hw_stats() parses the whole buffer. When the device writes less than the buffer holds, stale heap bytes are reported as counters through ethtool -S and the per-queue netlink statistics.

Allocate the buffer with kzalloc(), so that the counters the device didn't write are zeroed. An unwritten record header then has size 0, so reject it when walking on that header to avoid an infinite loop.

The used length cannot replace the zeroing here, since the device can also report a larger value than it wrote.

Claude Opus 5.5 was used to audit the driver and draft this patch. To reproduce, the virtio-net device in QEMU was modified to offer device statistics and to leave the counters of a receive record unwritten. On an unmodified kernel, ethtool -S reported kernel pointers as rx0 counters. With this patch, the counters are zeroed, and a reply with missing records or a record of size 0 is rejected. The drivers/net/stats.py selftest passes when the device answers with well-formed records.

---
v3:
- New patch.

 drivers/net/virtio_net.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +4 -1
index 57d7a98ecb57..498014773242 100644
@@ -4994,6 +4994,9 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 
 	for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
 		hdr = p;
+		if (!hdr->size)
+			return -EINVAL;
+
 		qid = le16_to_cpu(hdr->vq_index);
 		/* The vqs of the queue pairs beyond curr_queue_pairs lie
 		 * between last_vq and the control vq.
@@ -5068,7 +5071,7 @@ static int virtnet_get_hw_stats(struct virtnet_info *vi,
 	if (!req)
 		return -ENOMEM;
 
-	reply = kmalloc(res_size, GFP_KERNEL);
+	reply = kzalloc(res_size, GFP_KERNEL);
 	if (!reply) {
 		kfree(req);
 		return -ENOMEM;
Signature · 1 line
2.56.1
#4 lore raw

Re: [PATCH net v3 2/4] virtio_net: zero the stats reply buffer

Sashiko has reviewed this patch and found no issues.
Quoted 22 lines

virtnet_get_hw_stats() allocates the reply buffer of the device statistics command with kmalloc(), and __virtnet_get_hw_stats() parses the whole buffer. When the device writes less than the buffer holds, stale heap bytes are reported as counters through ethtool -S and the per-queue netlink statistics.

Allocate the buffer with kzalloc(), so that the counters the device didn't write are zeroed. An unwritten record header then has size 0, so reject it when walking on that header to avoid an infinite loop.

The used length cannot replace the zeroing here, since the device can also report a larger value than it wrote.

Claude Opus 5.5 was used to audit the driver and draft this patch. To reproduce, the virtio-net device in QEMU was modified to offer device statistics and to leave the counters of a receive record unwritten. On an unmodified kernel, ethtool -S reported kernel pointers as rx0 counters. With this patch, the counters are zeroed, and a reply with missing records or a record of size 0 is rejected. The drivers/net/stats.py selftest passes when the device answers with well-formed records.

Quoted 7 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v3 3/4 virtio_net: keep reads of stats records within the reply buffer

In reply to Lianjie Wang

__virtnet_get_hw_stats() only checks that each record of the statistics reply starts within the reply buffer, while virtnet_fill_stats() reads the whole payload of the record's type, so a record near the end of the buffer can make the driver read past it and report the heap bytes through ethtool -S and the per-queue netlink statistics.

Fix this by reserving zeroed space of the largest ctx->size[] after the reply, so the buffer can hold any type of payload.

Records of an unexpected size are still parsed, since every record starts within the reply and cannot cause out-of-bounds reads, and rejecting them with checks per type was judged costly to maintain [1].

Claude Opus 5.5 was used to audit the driver and draft this patch. To reproduce, the virtio-net device in QEMU was modified to offer device statistics and to end its reply with a truncated receive record. On an unmodified kernel, KASAN reports:

  BUG: KASAN: slab-out-of-bounds in virtnet_fill_stats+0x66d/0xf20
  Read of size 8 at addr ffff8880036f5940 by task ethtool_stats/159
  Call Trace:
   virtnet_fill_stats+0x66d/0xf20
   virtnet_get_hw_stats+0x5d6/0x7c0
   virtnet_get_ethtool_stats+0x11b/0x920
   ethtool_get_stats+0x150/0x1f0
   dev_ethtool+0xaaa/0x1be0
  Allocated by task 159:
   __kmalloc_noprof+0x1b7/0x4a0
   virtnet_get_hw_stats+0x25e/0x7c0
  The buggy address is located 8 bytes to the right of
   allocated 56-byte region [ffff8880036f5900, ffff8880036f5938)

With this patch, the same reply triggers no KASAN report. The drivers/net/stats.py selftest passes when the device answers with well-formed records.

---
v3:
- New patch.

 drivers/net/virtio_net.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +7 -1
index 498014773242..b9b6c915b6f3 100644
@@ -5071,7 +5071,13 @@ static int virtnet_get_hw_stats(struct virtnet_info *vi,
 	if (!req)
 		return -ENOMEM;
 
-	reply = kzalloc(res_size, GFP_KERNEL);
+	/* virtnet_fill_stats() parses the whole payload of a record's type,
+	 * so reserve zeroed space after the reply to hold any payload.
+	 */
+	reply = kzalloc(res_size + max3(ctx->size[VIRTNET_Q_TYPE_RX],
+					ctx->size[VIRTNET_Q_TYPE_TX],
+					ctx->size[VIRTNET_Q_TYPE_CQ]),
+			GFP_KERNEL);
 	if (!reply) {
 		kfree(req);
 		return -ENOMEM;
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH net v3 3/4] virtio_net: keep reads of stats records within the reply buffer

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

__virtnet_get_hw_stats() only checks that each record of the statistics reply starts within the reply buffer, while virtnet_fill_stats() reads the whole payload of the record's type, so a record near the end of the buffer can make the driver read past it and report the heap bytes through ethtool -S and the per-queue netlink statistics.

Fix this by reserving zeroed space of the largest ctx->size[] after the reply, so the buffer can hold any type of payload.

Records of an unexpected size are still parsed, since every record starts within the reply and cannot cause out-of-bounds reads, and rejecting them with checks per type was judged costly to maintain [1].

Quoted 8 lines

Claude Opus 5.5 was used to audit the driver and draft this patch. To reproduce, the virtio-net device in QEMU was modified to offer device

[ ... ]
Fixes 941168f8b40e ("virtio_net: support device stats")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

PATCH v3 4/4 virtio_net: report a failed stats command as an error

In reply to Lianjie Wang

__virtnet_get_hw_stats() returns 0 when the VIRTIO_NET_CTRL_STATS_GET command fails, so ethtool -S reports every hardware counter as 0 without the "Failed to get hw stats." warning.

Return -EINVAL instead, as virtnet_probe() does for VIRTIO_NET_CTRL_STATS_QUERY.

Claude Opus 5.5 was used to draft this patch. To test, the virtio-net device in QEMU was modified to offer device statistics and to fail the command. With this patch, ethtool -S triggers the warning.

Fixes 941168f8b40e ("virtio_net: support device stats")

---
v3:
- New patch.

 drivers/net/virtio_net.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/net/virtio_net.c +3 -6
index b9b6c915b6f3..185ac95b9b1b 100644
@@ -4980,17 +4980,14 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi,
 	struct scatterlist sgs_in, sgs_out;
 	void *p;
 	u32 qid;
-	int ok;
 
 	sg_init_one(&sgs_out, req, req_size);
 	sg_init_one(&sgs_in, reply, res_size);
 
-	ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
+	if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
 					VIRTIO_NET_CTRL_STATS_GET,
-					&sgs_out, &sgs_in);
-
-	if (!ok)
-		return ok;
+					&sgs_out, &sgs_in))
+		return -EINVAL;
 
 	for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
 		hdr = p;
Signature · 1 line
2.56.1
#8 lore raw

Re: [PATCH net v3 4/4] virtio_net: report a failed stats command as an error

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

__virtnet_get_hw_stats() returns 0 when the VIRTIO_NET_CTRL_STATS_GET command fails, so ethtool -S reports every hardware counter as 0 without the "Failed to get hw stats." warning.

Return -EINVAL instead, as virtnet_probe() does for VIRTIO_NET_CTRL_STATS_QUERY.

Quoted 8 lines

Claude Opus 5.5 was used to draft this patch. To test, the virtio-net device in QEMU was modified to offer device statistics and to fail the command. With this patch, ethtool -S triggers the warning.

Fixes 941168f8b40e ("virtio_net: support device stats")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw
In reply to Lianjie Wang
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Mon, 16 Nov 2026 18:24:41 +0900 you wrote:

With the statistics reply, a device can make the driver write past the ethtool -S output array (patch 1), leak stale heap or loop forever (patch 2), and read past the reply buffer (patch 3). Patch 4 returns an error when the statistics command fails.

Claude Opus 5.5 was used to audit the driver and draft the patches.

[...]

Here is the summary with links:
- [net,v3,1/4] virtio_net: reject stats replies for queues outside the request

    https://git.kernel.org/netdev/net-next/c/8c99fb84b512
  - [net,v3,2/4] virtio_net: zero the stats reply buffer
    https://git.kernel.org/netdev/net-next/c/48147635243a
  - [net,v3,3/4] virtio_net: keep reads of stats records within the reply buffer
    https://git.kernel.org/netdev/net-next/c/96b229723bb5
  - [net,v3,4/4] virtio_net: report a failed stats command as an error
    https://git.kernel.org/netdev/net-next/c/942d862f65b9

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html