Skip to contentSkip to conversation

Avoid sending zero-size packets

PATCH In review Oleinik, Alexander 3 participants 7 messages Jul 22, 2019 – Nov 21, 2019 Networking

lore
Share & tools
lore
#0 lore raw

v2 cover Avoid sending zero-size packets

While fuzzing the virtio-net tx vq, I ran into an assertion failure due to iov_copy offsets larger than the total iov size. Though there is a check to cover this, it does not execute when !n->has_vnet_hdr. This patch tries to fix this.
The call stack for the assertion failure:

#8 in __assert_fail (libc.so.6+0x300f1)
#9 in iov_copy iov.c:266:5
#10 in virtio_net_flush_tx virtio-net.c:2073:23 #11 in virtio_net_tx_bh virtio-net.c:2197:11
#12 in aio_bh_poll async.c:118:13
#13 in aio_dispatch aio-posix.c:460:5
#14 in aio_ctx_dispatch async.c:261:5
#15 in g_main_context_dispatch (libglib-2.0.so.0+0x4df2d) #16 in glib_pollfds_poll main-loop.c:213:9
#17 in os_host_main_loop_wait main-loop.c:236 #18 in main_loop_wait main-loop.c:512
#19 in virtio_net_tx_fuzz virtio-net-fuzz.c:160:3

v2: add details to comment for the change to qemu_sendv_packet_async

Alexander Oleinik (2):
net: assert that tx packets have nonzero size
virtio-net: check that tx packet has positive size

 hw/net/virtio-net.c | 15 +++++++++------
 net/net.c           |  9 +++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/2 net: assert that tx packets have nonzero size

Virtual devices should not try to send zero-sized packets. The caller should check the size prior to calling qemu_sendv_packet_async.

---
v2:
* Improve the comment to explain the rationale for adding the assert.

 net/net.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/net.c +9 -0
index 7d4098254f..4ad21df36f 100644
@@ -741,6 +741,15 @@ ssize_t qemu_sendv_packet_async(NetClientState *sender,
     size_t size = iov_size(iov, iovcnt);
     int ret;
 
+    /*
+     * Since this function returns the size of the sent packets, and a return
+     * value of zero indicates that the packet will be sent asynchronously,
+     * there is currently no way to report that a 0-sized packet has been sent
+     * successfully. Forbid it for now, and if someone needs this functionality
+     * later, the API will require a change.
+     */
+    assert(size);
+
     if (size > NET_BUFSIZE) {
         return size;
     }
Signature · 1 line
2.20.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] net: assert that tx packets have nonzero size

Quoted 16 lines · Alexander

Virtual devices should not try to send zero-sized packets. The caller should check the size prior to calling qemu_sendv_packet_async.

---
v2:
* Improve the comment to explain the rationale for adding the assert.

  net/net.c | 9 +++++++++
  1 file changed, 9 insertions(+)
net/net.c +0 -0
index 7d4098254f..4ad21df36f 100644
@@ -741,6 +741,15 @@ ssize_t qemu_sendv_packet_async(NetClientState *sender,
      size_t size = iov_size(iov, iovcnt);

net/net.c ssize_t qemu_sendv_packet_async(NetClientState *sender, · line 751

Quoted 10 lines
net/net.c +8 -0
      int ret;
 
+    /*
+     * Since this function returns the size of the sent packets, and a return
+     * value of zero indicates that the packet will be sent asynchronously,
+     * there is currently no way to report that a 0-sized packet has been sent
+     * successfully. Forbid it for now, and if someone needs this functionality
+     * later, the API will require a change.
+     */
+    assert(size);

This probably will make the assertion triggerable from guest. Is this better to warn and return NET_BUFSIZE + 1 here?

Thanks

Quoted 4 lines
net/net.c +1 -0
+
      if (size > NET_BUFSIZE) {
          return size;
      }
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] net: assert that tx packets have nonzero size

Quoted 23 lines · Jason Wang
Quoted 21 lines · Alexander

Virtual devices should not try to send zero-sized packets. The caller should check the size prior to calling qemu_sendv_packet_async.

---
v2:
* Improve the comment to explain the rationale for adding the assert.

  net/net.c | 9 +++++++++
  1 file changed, 9 insertions(+)
net/net.c +4 -0
index 7d4098254f..4ad21df36f 100644
@@ -741,6 +741,15 @@ ssize_t qemu_sendv_packet_async(NetClientState *sender,
      size_t size = iov_size(iov, iovcnt);
      int ret;
+    /*
+     * Since this function returns the size of the sent packets, and a return
+     * value of zero indicates that the packet will be sent asynchronously,
+     * there is currently no way to report that a 0-sized packet has been sent
Quoted 8 lines

net/net.c ssize_t qemu_sendv_packet_async(NetClientState *sender, · line 751

Quoted 4 lines
net/net.c +4 -0
+     * successfully. Forbid it for now, and if someone needs this functionality
+     * later, the API will require a change.
+     */
+    assert(size);

This probably will make the assertion triggerable from guest. Is this better to warn and return NET_BUFSIZE + 1 here?

Will do. I'll send a v3 out with this change.
Thank you

Quoted 8 lines

Thanks

Quoted 4 lines
net/net.c +1 -0
+
      if (size > NET_BUFSIZE) {
          return size;
      }
#4 lore raw

PATCH v2 2/2 virtio-net: check that tx packet has positive size

In reply to Oleinik, Alexander

virtio_net_flush_tx does not check that the packet size is nonzero, which causes q->aysnc_tx.elem to be set. Then, when the device is reset, there is an assertion failure since q->aysnc_tx.elem must be flushed/cleared. Zero-sized packets are unsupported - check packet size, prior to sending.

Found while fuzzing virtio-net. The relevant stack-trace:
#8 in __assert_fail (libc.so.6+0x30101)
#9 in virtio_net_reset virtio-net.c:520:13
#10 in virtio_reset virtio.c:1214:9
#11 in virtio_pci_reset virtio-pci.c:1810:5
#12 in qdev_reset_one qdev.c:259:5
#13 in qdev_walk_children qdev.c:575:15
#14 in qbus_walk_children bus.c:52:15
#15 in qdev_walk_children qdev.c:567:15
#16 in qbus_walk_children bus.c:52:15
#17 in qemu_devices_reset reset.c:69:9
#18 in pc_machine_reset pc.c:2628:5
#19 in qemu_system_reset vl.c:1621:9
#20 in LLVMFuzzerTestOneInput fuzz.c:184:4

---
 hw/net/virtio-net.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
hw/net/virtio-net.c +9 -6
index b9e1cd71cf..743f1c827c 100644
@@ -2074,12 +2074,15 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
             out_sg = sg;
         }
 
-        ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
-                                      out_sg, out_num, virtio_net_tx_complete);
-        if (ret == 0) {
-            virtio_queue_set_notification(q->tx_vq, 0);
-            q->async_tx.elem = elem;
-            return -EBUSY;
+        /* Do not try to send 0-sized packets */
+        if (iov_size(out_sg, out_num)) {
+            ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic,
+                        queue_index), out_sg, out_num, virtio_net_tx_complete);
+            if (ret == 0) {
+                virtio_queue_set_notification(q->tx_vq, 0);
+                q->async_tx.elem = elem;
+                return -EBUSY;
+            }
         }
 
 drop:
Signature · 1 line
2.20.1
Reviewed-by on the v2 series
#5 lore raw
In reply to Oleinik, Alexander
Quoted 21 lines · Alexander

While fuzzing the virtio-net tx vq, I ran into an assertion failure due to iov_copy offsets larger than the total iov size. Though there is a check to cover this, it does not execute when !n->has_vnet_hdr. This patch tries to fix this.
The call stack for the assertion failure:

#8 in __assert_fail (libc.so.6+0x300f1)
#9 in iov_copy iov.c:266:5
#10 in virtio_net_flush_tx virtio-net.c:2073:23 #11 in virtio_net_tx_bh virtio-net.c:2197:11
#12 in aio_bh_poll async.c:118:13
#13 in aio_dispatch aio-posix.c:460:5
#14 in aio_ctx_dispatch async.c:261:5
#15 in g_main_context_dispatch (libglib-2.0.so.0+0x4df2d) #16 in glib_pollfds_poll main-loop.c:213:9
#17 in os_host_main_loop_wait main-loop.c:236 #18 in main_loop_wait main-loop.c:512
#19 in virtio_net_tx_fuzz virtio-net-fuzz.c:160:3

v2: add details to comment for the change to qemu_sendv_packet_async

Quoted 12 lines

Alexander Oleinik (2):
net: assert that tx packets have nonzero size
virtio-net: check that tx packet has positive size

 hw/net/virtio-net.c | 15 +++++++++------
 net/net.c           |  9 +++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
Signature · 1 line
2.20.1
#6 lore raw
In reply to Oleinik, Alexander
Quoted 21 lines · Alexander

While fuzzing the virtio-net tx vq, I ran into an assertion failure due to iov_copy offsets larger than the total iov size. Though there is a check to cover this, it does not execute when !n->has_vnet_hdr. This patch tries to fix this.
The call stack for the assertion failure:

#8 in __assert_fail (libc.so.6+0x300f1)
#9 in iov_copy iov.c:266:5
#10 in virtio_net_flush_tx virtio-net.c:2073:23 #11 in virtio_net_tx_bh virtio-net.c:2197:11
#12 in aio_bh_poll async.c:118:13
#13 in aio_dispatch aio-posix.c:460:5
#14 in aio_ctx_dispatch async.c:261:5
#15 in g_main_context_dispatch (libglib-2.0.so.0+0x4df2d) #16 in glib_pollfds_poll main-loop.c:213:9
#17 in os_host_main_loop_wait main-loop.c:236 #18 in main_loop_wait main-loop.c:512
#19 in virtio_net_tx_fuzz virtio-net-fuzz.c:160:3

v2: add details to comment for the change to qemu_sendv_packet_async

Quoted 10 lines

Alexander Oleinik (2):
net: assert that tx packets have nonzero size
virtio-net: check that tx packet has positive size

 hw/net/virtio-net.c | 15 +++++++++------
 net/net.c           |  9 +++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
Signature · 1 line
2.20.1

Ping