Skip to contentSkip to conversation

virtio-net: correct header length math

PATCH In review Michael S. Tsirkin 3 participants 5 messages Jun 23, 2010 – Jun 24, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-net: correct header length math

We were requesting too much when checking buffer
length: size already includes host header length.

---
 hw/virtio-net.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)
hw/virtio-net.c +11 -9
index 06ba481..2646c87 100644
@@ -527,17 +527,18 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
 {
     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
     struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
-    size_t hdr_len, offset, i;
+    size_t guest_hdr_len, offset, i, host_hdr_len;
 
     if (!virtio_net_can_receive(&n->nic->nc))
         return -1;
 
     /* hdr_len refers to the header we supply to the guest */
-    hdr_len = n->mergeable_rx_bufs ?
+    guest_hdr_len = n->mergeable_rx_bufs ?
         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
 
 
-    if (!virtio_net_has_buffers(n, size + hdr_len))
+    host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
+    if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
         return 0;
 
     if (!receive_filter(n, buf, size))
@@ -556,9 +557,10 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
             virtqueue_pop(n->rx_vq, &elem) == 0) {
             if (i == 0)
                 return -1;
-            fprintf(stderr, "virtio-net truncating packet: "
-                    "offset %zd, size %zd, hdr_len %zd\n",
-                    offset, size, hdr_len);
+            fprintf(stderr, "virtio-net truncating packet: i %d mergeable %d "
+                    "offset %zd, size %zd, guest hdr %zd, host hdr %zd\n",
+		    i, n->mergeable_rx_bufs,
+                    offset, size, guest_hdr_len, host_hdr_len);
             exit(1);
         }
 
@@ -567,7 +569,7 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
             exit(1);
         }
 
-        if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
+        if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
             fprintf(stderr, "virtio-net header not in first element\n");
             exit(1);
         }
@@ -579,8 +581,8 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
                 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
 
             offset += receive_header(n, sg, elem.in_num,
-                                     buf + offset, size - offset, hdr_len);
-            total += hdr_len;
+                                     buf + offset, size - offset, guest_hdr_len);
+            total += guest_hdr_len;
         }
 
         /* copy in packet.  ugh */
Signature · 1 line
1.7.1.12.g42b7f
#1 lore raw
Quoted 4 lines · Michael S. Tsirkin

We were requesting too much when checking buffer
length: size already includes host header length.

All changes here are ok, but we are still missing more checks.

Quoted 14 lines
---
 hw/virtio-net.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)
hw/virtio-net.c +1 -1
index 06ba481..2646c87 100644
@@ -527,17 +527,18 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
 {
     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
     struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
-    size_t hdr_len, offset, i;
+    size_t guest_hdr_len, offset, i, host_hdr_len;

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 539

Quoted 10 lines
hw/virtio-net.c +1 -1
 
     if (!virtio_net_can_receive(&n->nic->nc))
         return -1;
 
     /* hdr_len refers to the header we supply to the guest */
-    hdr_len = n->mergeable_rx_bufs ?
+    guest_hdr_len = n->mergeable_rx_bufs ?
         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
 
 

nitpit: empty extra line

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 544

Quoted 6 lines
hw/virtio-net.c +2 -1
-    if (!virtio_net_has_buffers(n, size + hdr_len))
+    host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
+    if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
         return 0;
 
     if (!receive_filter(n, buf, size))

size is not used by receive_filter. We are assuming that size is at least 16 + sizeof(struct virtio_net_hdr).

    while (offset < size) {

we are still testing offset with size, but we read headers from there also :(

Later, Juan.

#2 lore raw
Quoted 40 lines · Juan Quintela
Quoted 4 lines · Michael S. Tsirkin

We were requesting too much when checking buffer
length: size already includes host header length.

All changes here are ok, but we are still missing more checks.

Quoted 14 lines
---
 hw/virtio-net.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)
hw/virtio-net.c +1 -1
index 06ba481..2646c87 100644
@@ -527,17 +527,18 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
 {
     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
     struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
-    size_t hdr_len, offset, i;
+    size_t guest_hdr_len, offset, i, host_hdr_len;

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 539

Quoted 10 lines
hw/virtio-net.c +1 -1
 
     if (!virtio_net_can_receive(&n->nic->nc))
         return -1;
 
     /* hdr_len refers to the header we supply to the guest */
-    hdr_len = n->mergeable_rx_bufs ?
+    guest_hdr_len = n->mergeable_rx_bufs ?
         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
 
 

nitpit: empty extra line

Quoted 5 lines
hw/virtio-net.c +2 -1
-    if (!virtio_net_has_buffers(n, size + hdr_len))
+    host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
+    if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
         return 0;
 
Quoted 4 lines

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 544

Quoted 1 line
hw/virtio-net.c +0 -0
     if (!receive_filter(n, buf, size))

size is not used by receive_filter. We are assuming that size is at least 16 + sizeof(struct virtio_net_hdr).

True, I guess receive_filter should be fixed.

Quoted 9 lines
    while (offset < size) {

we are still testing offset with size, but we read headers from there also :(

From where? As far as I can tell we always read size bytes from buf. No?

Later, Juan.

#3 lore raw
In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin

We were requesting too much when checking buffer
length: size already includes host header length.

---
 hw/virtio-net.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

Couple of minor comments. But I'll still provide the

ACK

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 563

Quoted 11 lines
hw/virtio-net.c +4 -3
@@ -556,9 +557,10 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
             virtqueue_pop(n->rx_vq, &elem) == 0) {
             if (i == 0)
                 return -1;
-            fprintf(stderr, "virtio-net truncating packet: "
-                    "offset %zd, size %zd, hdr_len %zd\n",
-                    offset, size, hdr_len);
+            fprintf(stderr, "virtio-net truncating packet: i %d mergeable %d "
+                    "offset %zd, size %zd, guest hdr %zd, host hdr %zd\n",
+		    i, n->mergeable_rx_bufs,
+                    offset, size, guest_hdr_len, host_hdr_len);

Again, tab usage and 'guest hdr len' and 'host hdr len' make better sense than 'guest hdr' and 'host hdr'

Review static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 564

Quoted 1 line
(quoted diff) +0 -0
             exit(1);

Can we drop the packet(s) instead of exiting?

Amit

#4 lore raw
Quoted 28 lines · Amit Shah
Quoted 7 lines · Michael S. Tsirkin

We were requesting too much when checking buffer
length: size already includes host header length.

---
 hw/virtio-net.c |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

Couple of minor comments. But I'll still provide the

ACK

hw/virtio-net.c static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 563

Quoted 11 lines
hw/virtio-net.c +4 -3
@@ -556,9 +557,10 @@ static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_
             virtqueue_pop(n->rx_vq, &elem) == 0) {
             if (i == 0)
                 return -1;
-            fprintf(stderr, "virtio-net truncating packet: "
-                    "offset %zd, size %zd, hdr_len %zd\n",
-                    offset, size, hdr_len);
+            fprintf(stderr, "virtio-net truncating packet: i %d mergeable %d "
+                    "offset %zd, size %zd, guest hdr %zd, host hdr %zd\n",
+		    i, n->mergeable_rx_bufs,
+                    offset, size, guest_hdr_len, host_hdr_len);

Again, tab usage and 'guest hdr len' and 'host hdr len' make better sense than 'guest hdr' and 'host hdr'

Quoted 5 lines

Review static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_ · line 564

Quoted 1 line
(quoted diff) +0 -0
             exit(1);

Can we drop the packet(s) instead of exiting?

Amit

This is a guest bug in the same way as moving indexes incorrectly. But since we do have such buggy guests in the field (old virtio), I guess we should.

Signature · 1 line
MST