Skip to contentSkip to conversation

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

PATCH RESEND Superseded Zhang Chen 2 participants 4 messages Aug 9, 2022 – Aug 19, 2022 Networking

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

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

When enabled 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..2b5568fff4 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_v1_hash)) ||
+        (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

Ping.... Jason and Peter, any comments for this patch?

Thanks
Chen

Quoted 72 lines

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

When enabled 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..2b5568fff4 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_v1_hash)) ||
+        (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 41 lines · Chen

Ping.... Jason and Peter, any comments for this patch?

Thanks
Chen

Quoted 35 lines

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

When enabled 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 +1 -1
index 6b0ff562ad..2b5568fff4 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;
 
Quoted 10 lines

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

Quoted 10 lines
net/colo.c +7 -2
-    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_v1_hash)) ||
+        (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);

Nit: I think we need to be verbose here, e.g put the pkt_size here at least.

Thanks

Quoted 28 lines
Quoted 27 lines
net/colo.c +2 -5
         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 68 lines

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

Quoted 17 lines · Chen

Ping.... Jason and Peter, any comments for this patch?

Thanks
Chen

Quoted 11 lines

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

When enabled the virtio-net-pci, guest network packet will load the

vnet_hdr.

Quoted 25 lines
Quoted 25 lines

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 index 6b0ff562ad..2b5568fff4 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

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 9 lines

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

Quoted 9 lines
net/colo.c +7 -0
+    assert(data);
+
+    /* Check the received vnet_hdr_len then add the offset */
+    if ((pkt->vnet_hdr_len > sizeof(struct virtio_net_hdr_v1_hash)) ||
+        (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);
Quoted 1 line

Nit: I think we need to be verbose here, e.g put the pkt_size here at least.

OK, I will change here to:
/*
* The received remote packet maybe misconfiguration here,
* Please enable/disable filter module's the vnet_hdr flag at the same time. */
trace_colo_proxy_main_vnet_info("This received packet load wrong "

                                                               "pkt->vnet_hdr_len",  pkt->vnet_hdr_len, pkt->size);

Thanks
Chen

Quoted 31 lines

Thanks

Quoted 28 lines
Quoted 27 lines
net/colo.c +2 -5
         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