Skip to contentSkip to conversation

virtio: move sanity checks to ifdef DEBUG

PATCH In review Michael S. Tsirkin 2 participants 2 messages Mar 18, 2015 – Mar 20, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: move sanity checks to ifdef DEBUG

All that happens when virtqueue_fill is invoked incorrectly is that we corrupt guest memory, so this check is not a security measure. Move the check to ifdef DEBUG to make sure we don't introduce new crashes close to release.
Array scans aren't free either, so it's a good idea from performance point of view, too.

---
 hw/virtio/virtio.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/virtio/virtio.c +11 -4
index 27429c2..37fb2ee 100644
@@ -243,15 +243,22 @@ int virtio_queue_empty(VirtQueue *vq)
 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len_written, unsigned int idx)
 {
-    unsigned int offset, tot_wlen;
+    unsigned int offset;
     int i;
 
     trace_virtqueue_fill(vq, elem, len_written, idx);
 
-    for (tot_wlen = i = 0; i < elem->in_num; i++) {
-        tot_wlen += elem->in_sg[i].iov_len;
+#ifdef DEBUG_VIRTIO
+    {
+        /* Check that len_written is <= the writable length. */
+        unsigned int tot_wlen;
+
+        for (tot_wlen = i = 0; i < elem->in_num; i++) {
+            tot_wlen += elem->in_sg[i].iov_len;
+        }
+        assert(len_written <= tot_wlen);
     }
-    assert(len_written <= tot_wlen);
+#endif
 
     offset = 0;
     for (i = 0; i < elem->in_num; i++) {
Signature · 1 line
MST
#1 lore raw
Quoted 4 lines · On Wed, 18 Mar 2015 12:42:46 +0100 "Michael S. Tsirkin" <[email protected]> wrote:

All that happens when virtqueue_fill is invoked incorrectly is that we corrupt guest memory, so this check is not a security measure. Move the check to ifdef DEBUG to make sure we don't introduce new crashes close to release.

I don't really disagree with this patch, but is anybody actually making regular runs with VIRTIO_DEBUG activated?

Quoted 40 lines

Array scans aren't free either, so it's a good idea from performance point of view, too.

---
 hw/virtio/virtio.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/virtio/virtio.c +11 -4
index 27429c2..37fb2ee 100644
@@ -243,15 +243,22 @@ int virtio_queue_empty(VirtQueue *vq)
 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                     unsigned int len_written, unsigned int idx)
 {
-    unsigned int offset, tot_wlen;
+    unsigned int offset;
     int i;
 
     trace_virtqueue_fill(vq, elem, len_written, idx);
 
-    for (tot_wlen = i = 0; i < elem->in_num; i++) {
-        tot_wlen += elem->in_sg[i].iov_len;
+#ifdef DEBUG_VIRTIO
+    {
+        /* Check that len_written is <= the writable length. */
+        unsigned int tot_wlen;
+
+        for (tot_wlen = i = 0; i < elem->in_num; i++) {
+            tot_wlen += elem->in_sg[i].iov_len;
+        }
+        assert(len_written <= tot_wlen);
     }
-    assert(len_written <= tot_wlen);
+#endif
 
     offset = 0;
     for (i = 0; i < elem->in_num; i++) {