Skip to contentSkip to conversation

net/colo.c: Fix the pointer issue reported by Coverity.

PATCH Superseded Zhang Chen 2 participants 5 messages Aug 9, 2022 Networking

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

PATCH v3 1/1 net/colo.c: Fix the pointer issue reported by Coverity.

When enable the virtio-net-pci, guest network packet will load the vnet_hdr. In COLO status, the primary VM's network packet maybe redirect to another VM, it need filter-redirect enable the vnet_hdr flag at the same time, COLO-proxy will correctly parse the original network packet. If have any misconfiguration here, the vnet_hdr_len is wrong for parse the packet, the data+offset will point to wrong place.

---
 net/colo.c | 18 ++++++++++--------
 net/colo.h |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
net/colo.c +10 -8
index 6b0ff562ad..4f1b4a61f6 100644
@@ -44,21 +44,23 @@ int parse_packet_early(Packet *pkt)
 {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data + pkt->vnet_hdr_len;
+    uint8_t *data = pkt->data;
     uint16_t l3_proto;
     ssize_t l2hdr_len;
 
-    if (data == NULL) {
-        trace_colo_proxy_main_vnet_info("This packet is not parsed correctly, "
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr)) ||
+        (pkt->size < sizeof(struct eth_header) + sizeof(struct vlan_header)
+        + pkt->vnet_hdr_len)) {
+        trace_colo_proxy_main_vnet_info("This packet may be load wrong "
                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
         return 1;
     }
-    l2hdr_len = eth_get_l2_hdr_length(data);
+    data += pkt->vnet_hdr_len;
 
-    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
-        trace_colo_proxy_main("pkt->size < ETH_HLEN");
-        return 1;
-    }
+    l2hdr_len = eth_get_l2_hdr_length(data);
 
     /*
      * TODO: support vlan.
net/colo.h +1 -0
index 8b3e8d5a83..22fc3031f7 100644
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "standard-headers/linux/virtio_net.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 24 lines · Zhang Chen

When enable the virtio-net-pci, guest network packet will load the vnet_hdr. In COLO status, the primary VM's network packet maybe redirect to another VM, it need filter-redirect enable the vnet_hdr flag at the same time, COLO-proxy will correctly parse the original network packet. If have any misconfiguration here, the vnet_hdr_len is wrong for parse the packet, the data+offset will point to wrong place.

---
 net/colo.c | 18 ++++++++++--------
 net/colo.h |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
net/colo.c +0 -1
index 6b0ff562ad..4f1b4a61f6 100644
@@ -44,21 +44,23 @@ int parse_packet_early(Packet *pkt)
 {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data + pkt->vnet_hdr_len;

net/colo.c int parse_packet_early(Packet *pkt) · line 54

Quoted 10 lines
net/colo.c +5 -2
+    uint8_t *data = pkt->data;
     uint16_t l3_proto;
     ssize_t l2hdr_len;
 
-    if (data == NULL) {
-        trace_colo_proxy_main_vnet_info("This packet is not parsed correctly, "
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr)) ||

Virtio-net starts to support RSS so it means the vnet_hdr size could be greater than virtio_net_hdr now.

Or did you actually mean "<" here?

Thanks

Quoted 32 lines
net/colo.c +5 -5
+        (pkt->size < sizeof(struct eth_header) + sizeof(struct vlan_header)
+        + pkt->vnet_hdr_len)) {
+        trace_colo_proxy_main_vnet_info("This packet may be load wrong "
                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
         return 1;
     }
-    l2hdr_len = eth_get_l2_hdr_length(data);
+    data += pkt->vnet_hdr_len;
 
-    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
-        trace_colo_proxy_main("pkt->size < ETH_HLEN");
-        return 1;
-    }
+    l2hdr_len = eth_get_l2_hdr_length(data);
 
     /*
      * TODO: support vlan.
net/colo.h +1 -1
index 8b3e8d5a83..22fc3031f7 100644
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "standard-headers/linux/virtio_net.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
--

2.25.1

#2 lore raw
Quoted 47 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, August 9, 2022 4:12 PM
To: Zhang, Chen <[email protected]>
Cc: Peter Maydell <[email protected]>; Li Zhijian <[email protected]>; qemu-dev <[email protected]>
Subject: Re: [PATCH V3] net/colo.c: Fix the pointer issue reported by Coverity.

Quoted 22 lines · Zhang Chen

When enable the virtio-net-pci, guest network packet will load the vnet_hdr. In COLO status, the primary VM's network packet maybe redirect to another VM, it need filter-redirect enable the vnet_hdr flag at the same time, COLO-proxy will correctly parse the original network packet. If have any misconfiguration here, the vnet_hdr_len is wrong for parse the packet, the data+offset will point to wrong place.

---
 net/colo.c | 18 ++++++++++--------
 net/colo.h |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
net/colo.c +0 -1
index 6b0ff562ad..4f1b4a61f6 100644
@@ -44,21 +44,23 @@ int parse_packet_early(Packet *pkt)  {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data + pkt->vnet_hdr_len;

net/colo.c int parse_packet_early(Packet *pkt) { · line 53

Quoted 10 lines
net/colo.c +5 -2
+    uint8_t *data = pkt->data;
     uint16_t l3_proto;
     ssize_t l2hdr_len;
 
-    if (data == NULL) {
-        trace_colo_proxy_main_vnet_info("This packet is not parsed correctly, "
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr)) ||

Virtio-net starts to support RSS so it means the vnet_hdr size could be greater than virtio_net_hdr now.

Quoted 1 line

Or did you actually mean "<" here?

No, I just try to avoid overflow with a maliciously over-large vnet_hdr_len value as Peter's comments.
If enabled the RSS, how to get the maximum of vnet_hdr size?

Thanks
Chen

Quoted 35 lines

Thanks

Quoted 32 lines
net/colo.c +5 -5
+        (pkt->size < sizeof(struct eth_header) + sizeof(struct vlan_header)
+        + pkt->vnet_hdr_len)) {
+        trace_colo_proxy_main_vnet_info("This packet may be load wrong "
                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
         return 1;
     }
-    l2hdr_len = eth_get_l2_hdr_length(data);
+    data += pkt->vnet_hdr_len;
 
-    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
-        trace_colo_proxy_main("pkt->size < ETH_HLEN");
-        return 1;
-    }
+    l2hdr_len = eth_get_l2_hdr_length(data);
 
     /*
      * TODO: support vlan.
net/colo.h +1 -1
index 8b3e8d5a83..22fc3031f7 100644
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "standard-headers/linux/virtio_net.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
--

2.25.1

#3 lore raw
Quoted 48 lines · Chen
Quoted 48 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, August 9, 2022 4:12 PM
To: Zhang, Chen <[email protected]>
Cc: Peter Maydell <[email protected]>; Li Zhijian <[email protected]>; qemu-dev <[email protected]>
Subject: Re: [PATCH V3] net/colo.c: Fix the pointer issue reported by Coverity.

Quoted 22 lines · Zhang Chen

When enable the virtio-net-pci, guest network packet will load the vnet_hdr. In COLO status, the primary VM's network packet maybe redirect to another VM, it need filter-redirect enable the vnet_hdr flag at the same time, COLO-proxy will correctly parse the original network packet. If have any misconfiguration here, the vnet_hdr_len is wrong for parse the packet, the data+offset will point to wrong place.

---
 net/colo.c | 18 ++++++++++--------
 net/colo.h |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
net/colo.c +0 -1
index 6b0ff562ad..4f1b4a61f6 100644
@@ -44,21 +44,23 @@ int parse_packet_early(Packet *pkt)  {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data + pkt->vnet_hdr_len;

net/colo.c int parse_packet_early(Packet *pkt) { · line 53

Quoted 10 lines
net/colo.c +5 -2
+    uint8_t *data = pkt->data;
     uint16_t l3_proto;
     ssize_t l2hdr_len;
 
-    if (data == NULL) {
-        trace_colo_proxy_main_vnet_info("This packet is not parsed correctly, "
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr)) ||

Virtio-net starts to support RSS so it means the vnet_hdr size could be greater than virtio_net_hdr now.

Quoted 5 lines
Quoted 1 line

Or did you actually mean "<" here?

No, I just try to avoid overflow with a maliciously over-large vnet_hdr_len value as Peter's comments.
If enabled the RSS, how to get the maximum of vnet_hdr size?

With hash_report the maximum is sizeof(virtio_net_hdr_v1_hash). But it might be extended in the future.

We can probably start from it.

Thanks

Quoted 40 lines

Thanks
Chen

Quoted 35 lines

Thanks

Quoted 32 lines
net/colo.c +5 -5
+        (pkt->size < sizeof(struct eth_header) + sizeof(struct vlan_header)
+        + pkt->vnet_hdr_len)) {
+        trace_colo_proxy_main_vnet_info("This packet may be load wrong "
                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
         return 1;
     }
-    l2hdr_len = eth_get_l2_hdr_length(data);
+    data += pkt->vnet_hdr_len;
 
-    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
-        trace_colo_proxy_main("pkt->size < ETH_HLEN");
-        return 1;
-    }
+    l2hdr_len = eth_get_l2_hdr_length(data);
 
     /*
      * TODO: support vlan.
net/colo.h +1 -1
index 8b3e8d5a83..22fc3031f7 100644
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "standard-headers/linux/virtio_net.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
--

2.25.1

#4 lore raw
Quoted 64 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, August 9, 2022 4:43 PM
To: Zhang, Chen <[email protected]>
Cc: Peter Maydell <[email protected]>; Li Zhijian <[email protected]>; qemu-dev <[email protected]>
Subject: Re: [PATCH V3] net/colo.c: Fix the pointer issue reported by Coverity.

Quoted 10 lines · Chen
Quoted 10 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, August 9, 2022 4:12 PM
To: Zhang, Chen <[email protected]>
Cc: Peter Maydell <[email protected]>; Li Zhijian <[email protected]>; qemu-dev <[email protected]>
Subject: Re: [PATCH V3] net/colo.c: Fix the pointer issue reported by

Coverity.

Quoted 2 lines
Quoted 2 lines

On Tue, Aug 9, 2022 at 11:05 AM Zhang Chen <[email protected]>

Quoted 7 lines · wrote:
Quoted 7 lines
Quoted 7 lines

When enable the virtio-net-pci, guest network packet will load the vnet_hdr. In COLO status, the primary VM's network packet maybe redirect to another VM, it need filter-redirect enable the vnet_hdr flag at the same time, COLO-proxy will correctly parse the original network packet. If have any misconfiguration here, the vnet_hdr_len is wrong for parse the packet, the data+offset will point

to wrong place.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
---
 net/colo.c | 18 ++++++++++--------  net/colo.h |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
net/colo.c index 6b0ff562ad..4f1b4a61f6 renamed from net/colo.c +0 -0

100644

net/colo.c +1 -1
@@ -44,21 +44,23 @@ int parse_packet_early(Packet *pkt)  {
     int network_length;
     static const uint8_t vlan[] = {0x81, 0x00};
-    uint8_t *data = pkt->data + pkt->vnet_hdr_len;
+    uint8_t *data = pkt->data;
     uint16_t l3_proto;
     ssize_t l2hdr_len;
 
Quoted 2 lines
Quoted 2 lines

net/colo.c int parse_packet_early(Packet *pkt) { · line 51

Quoted 2 lines
net/colo.c +0 -2
-    if (data == NULL) {
-        trace_colo_proxy_main_vnet_info("This packet is not parsed

correctly, "

Quoted 10 lines
Quoted 9 lines

net/colo.c int parse_packet_early(Packet *pkt) · line 54

Quoted 4 lines
net/colo.c +4 -0
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr)) ||

Virtio-net starts to support RSS so it means the vnet_hdr size could be greater than virtio_net_hdr now.

Or did you actually mean "<" here?

Quoted 8 lines
Quoted 3 lines

No, I just try to avoid overflow with a maliciously over-large vnet_hdr_len value as Peter's comments.
If enabled the RSS, how to get the maximum of vnet_hdr size?

With hash_report the maximum is sizeof(virtio_net_hdr_v1_hash). But it might be extended in the future.

We can probably start from it.

Thanks, I will change here to " if ((pkt->vnet_hdr_len > sizeof(virtio_net_hdr_v1_hash)) ||" in next version.

Thanks
Chen

Quoted 44 lines

Thanks

Quoted 10 lines

Thanks
Chen

Quoted 6 lines

Thanks

Quoted 3 lines
net/colo.c +3 -0
+        (pkt->size < sizeof(struct eth_header) + sizeof(struct vlan_header)
+        + pkt->vnet_hdr_len)) {
+        trace_colo_proxy_main_vnet_info("This packet may be load wrong

"

Quoted 30 lines
Quoted 29 lines
Quoted 29 lines
net/colo.c +2 -5
                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
         return 1;
     }
-    l2hdr_len = eth_get_l2_hdr_length(data);
+    data += pkt->vnet_hdr_len;
 
-    if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
-        trace_colo_proxy_main("pkt->size < ETH_HLEN");
-        return 1;
-    }
+    l2hdr_len = eth_get_l2_hdr_length(data);
 
     /*
      * TODO: support vlan.
net/colo.h index 8b3e8d5a83..22fc3031f7 renamed from net/colo.h +0 -0

100644

net/colo.h +1 -1
@@ -18,6 +18,7 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "standard-headers/linux/virtio_net.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
--

2.25.1