Skip to contentSkip to conversation

virtio-net: check the existence of peer before accesing its config

PATCH Superseded Jason Wang 3 participants 14 messages Jul 27, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 virtio-net: check the existence of peer before accesing its config

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
---
 hw/net/virtio-net.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
 
     int ret = 0;
     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                  VIRTIO_NET_RSS_SUPPORTED_HASHES);
     memcpy(config, &netcfg, n->config_size);
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
         ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
     }
 }
 
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
 
     memcpy(&netcfg, config, n->config_size);
 
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
     }
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
       }
 }
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw

On Mon, 27 Jul 2020 15:43:28 +0800
Jason Wang <[email protected]> wrote:

typo in subject: s/accesing/accessing/

Quoted 1 line

We try to get config from peer unconditionally which may lead NULL

s/lead/lead to a/

Quoted 9 lines

pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
---
 hw/net/virtio-net.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
author
#2 lore raw
Quoted 10 lines · Cornelia Huck

On Mon, 27 Jul 2020 15:43:28 +0800
Jason Wang <[email protected]> wrote:

typo in subject: s/accesing/accessing/

Quoted 1 line

We try to get config from peer unconditionally which may lead NULL

s/lead/lead to a/

Quoted 2 lines

pointer dereference. Add a check before trying to access the config.

Quoted 8 lines
Quoted 7 lines
Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)

Applied with the above fixed.

Thanks

#3 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked when guest attempts to read the config? With no peer, what do we return to guest? A code comment might be helpful here.

Quoted 61 lines
---
 hw/net/virtio-net.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
 
     int ret = 0;
     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                  VIRTIO_NET_RSS_SUPPORTED_HASHES);
     memcpy(config, &netcfg, n->config_size);
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
         ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
     }
 }
 
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
 
     memcpy(&netcfg, config, n->config_size);
 
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
     }
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
       }
 }
 
Signature · 1 line
2.20.1
author
#4 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

With no peer, it just work as in the past (read from the qemu's own emulated config space). With a vDPA as its peer, it tries to read it from vhost-vDPA.

Quoted 1 line

A code comment might be helpful here.

Does something like above help?

Thanks

Quoted 62 lines
Quoted 62 lines
---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
 
      int ret = 0;
      memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                   VIRTIO_NET_RSS_SUPPORTED_HASHES);
      memcpy(config, &netcfg, n->config_size);
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
          ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
      }
  }
 
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
 
      memcpy(&netcfg, config, n->config_size);
 
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
          qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
      }
 
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
        }
  }
 
Signature · 1 line
2.20.1
#5 lore raw
Quoted 10 lines · Jason Wang
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
Quoted 7 lines
Quoted 3 lines

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

With no peer, it just work as in the past (read from the qemu's own emulated config space). With a vDPA as its peer, it tries to read it from vhost-vDPA.

Are these scenarios where guest would sometimes get one and sometimes another? E.g. does it happen on disconnect? If yes that might become a problem ...

Quoted 64 lines
Quoted 2 lines

A code comment might be helpful here.

Does something like above help?

Thanks

Quoted 55 lines
Quoted 55 lines
---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
      int ret = 0;
      memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                   VIRTIO_NET_RSS_SUPPORTED_HASHES);
      memcpy(config, &netcfg, n->config_size);
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
          ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
      }
  }
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
      memcpy(&netcfg, config, n->config_size);
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
          qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
      }
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
        }
  }
Signature · 1 line
2.20.1
author
#6 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Quoted 5 lines
Quoted 2 lines

With no peer, it just work as in the past (read from the qemu's own emulated config space). With a vDPA as its peer, it tries to read it from vhost-vDPA.

Are these scenarios where guest would sometimes get one and sometimes another? E.g. does it happen on disconnect? If yes that might become a problem ...

For disconnecting yes, but I wonder if we need to care about that case anyway.

Thanks

Quoted 62 lines
Quoted 62 lines
Quoted 2 lines

A code comment might be helpful here.

Does something like above help?

Thanks

Quoted 54 lines
Quoted 54 lines
---
   hw/net/virtio-net.c | 22 +++++++++++-----------
   1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
   {
       VirtIONet *n = VIRTIO_NET(vdev);
       struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
       int ret = 0;
       memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                    VIRTIO_NET_RSS_SUPPORTED_HASHES);
       memcpy(config, &netcfg, n->config_size);
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
           ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
       }
   }
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
   {
       VirtIONet *n = VIRTIO_NET(vdev);
       struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
       memcpy(&netcfg, config, n->config_size);
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
           qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
       }
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
         }
   }
Signature · 1 line
2.20.1
#7 lore raw
In reply to Jason Wang
Quoted 19 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

With no peer, it just work as in the past (read from the qemu's own emulated config space). With a vDPA as its peer, it tries to read it from vhost-vDPA.

Quoted 6 lines
Quoted 1 line

A code comment might be helpful here.

Does something like above help?

Thanks

I think this part of commit log caused confusion:

Add a check before trying to access the config.

looking more at the code, imho in fact here is a better description of what is going on:

We try to check whether a peer is VDPA in order to get config from there - with no peer, this leads to a NULL
pointer dereference. Add a check before trying to access the peer type. No peer means not VDPA.

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines

---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
      int ret = 0;
      memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                   VIRTIO_NET_RSS_SUPPORTED_HASHES);
      memcpy(config, &netcfg, n->config_size);
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
          ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
      }
  }
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
      struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
      memcpy(&netcfg, config, n->config_size);
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
          qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
      }
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
        }
  }
Signature · 1 line
2.20.1
author
#8 lore raw
Quoted 27 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

With no peer, it just work as in the past (read from the qemu's own emulated config space). With a vDPA as its peer, it tries to read it from vhost-vDPA.

Quoted 5 lines
Quoted 1 line

A code comment might be helpful here.

Does something like above help?

Thanks

I think this part of commit log caused confusion:

Add a check before trying to access the config.

Quoted 7 lines

looking more at the code, imho in fact here is a better description of what is going on:

We try to check whether a peer is VDPA in order to get config from there - with no peer, this leads to a NULL
pointer dereference. Add a check before trying to access the peer type. No peer means not VDPA.

Yes, this looks much better.

Thanks

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines

---
   hw/net/virtio-net.c | 22 +++++++++++-----------
   1 file changed, 11 insertions(+), 11 deletions(-)
hw/net/virtio-net.c +11 -11
index 4895af1cbe..935b9ef5c7 100644
@@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
   {
       VirtIONet *n = VIRTIO_NET(vdev);
       struct virtio_net_config netcfg;
+    NetClientState *nc = qemu_get_queue(n->nic);
       int ret = 0;
       memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -142,13 +143,12 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
                    VIRTIO_NET_RSS_SUPPORTED_HASHES);
       memcpy(config, &netcfg, n->config_size);
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
           ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                             n->config_size);
-    if (ret != -1) {
-        memcpy(config, &netcfg, n->config_size);
-    }
+                                   n->config_size);
+        if (ret != -1) {
+            memcpy(config, &netcfg, n->config_size);
+        }
       }
   }
@@ -156,6 +156,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
   {
       VirtIONet *n = VIRTIO_NET(vdev);
       struct virtio_net_config netcfg = {};
+    NetClientState *nc = qemu_get_queue(n->nic);
       memcpy(&netcfg, config, n->config_size);
@@ -166,11 +167,10 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
           qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
       }
-    NetClientState *nc = qemu_get_queue(n->nic);
-    if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
-                               0, n->config_size,
-                        VHOST_SET_CONFIG_TYPE_MASTER);
+    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+        vhost_net_set_config(get_vhost_net(nc->peer),
+                             (uint8_t *)&netcfg, 0, n->config_size,
+                             VHOST_SET_CONFIG_TYPE_MASTER);
         }
   }
Signature · 1 line
2.20.1
#9 lore raw
In reply to Michael S. Tsirkin
Quoted 4 lines · On Mon, 27 Jul 2020 05:41:17 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 3 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Quoted 8 lines
Quoted 4 lines
Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Same as with a non-vdpa peer? It's the dereference that needs to be guarded.

Quoted 5 lines

A code comment might be helpful here.

Quoted 3 lines
---
 hw/net/virtio-net.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
#10 lore raw
Quoted 12 lines · Cornelia Huck
Quoted 9 lines · On Mon, 27 Jul 2020 05:41:17 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
Quoted 6 lines
Quoted 3 lines

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Same as with a non-vdpa peer? It's the dereference that needs to be guarded.

So vdpa has a GET_CONFIG ioctl which to me hints that a peer needs to be notified on get config calls.
If we return config from qemu memory here, then I guess we need to call GET_CONFIG on connect and validate it -
does this make sense?

Cindy, Jason?

Quoted 5 lines
Quoted 5 lines

A code comment might be helpful here.

Quoted 3 lines
---
 hw/net/virtio-net.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
author
#11 lore raw
Quoted 17 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Mon, 27 Jul 2020 05:41:17 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
Quoted 5 lines
Quoted 3 lines

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Same as with a non-vdpa peer? It's the dereference that needs to be guarded.

Quoted 7 lines

So vdpa has a GET_CONFIG ioctl which to me hints that a peer needs to be notified on get config calls.
If we return config from qemu memory here, then I guess we need to call GET_CONFIG on connect and validate it -
does this make sense?

Cindy, Jason?

For "connect" you meant connecting virtio-net to its peer (vDPA)? AFAIK, if we start with no peer, there's no way to set a peer afterwards.

Thanks

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

A code comment might be helpful here.

Quoted 3 lines
---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
#12 lore raw
Quoted 25 lines · Jason Wang
Quoted 23 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Mon, 27 Jul 2020 05:41:17 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
Quoted 5 lines
Quoted 3 lines

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Same as with a non-vdpa peer? It's the dereference that needs to be guarded.

So vdpa has a GET_CONFIG ioctl which to me hints that a peer needs to be notified on get config calls.
If we return config from qemu memory here, then I guess we need to call GET_CONFIG on connect and validate it -
does this make sense?

Quoted 7 lines
Quoted 1 line

Cindy, Jason?

For "connect" you meant connecting virtio-net to its peer (vDPA)? AFAIK, if we start with no peer, there's no way to set a peer afterwards.

Thanks

That would be a good sentence to add in a code comment:

/*
* Is this VDPA? No peer means not VDPA: there's no way to
* disconnect/reconnect a VDPA peer.
*/

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

A code comment might be helpful here.

Quoted 3 lines
---
  hw/net/virtio-net.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
author
#13 lore raw
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Jason Wang
Quoted 17 lines · Michael S. Tsirkin
Quoted 11 lines · Cornelia Huck
Quoted 8 lines · On Mon, 27 Jul 2020 05:41:17 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 7 lines · Jason Wang

We try to get config from peer unconditionally which may lead NULL pointer dereference. Add a check before trying to access the config.

Fixes 108a64818e69b ("vhost-vdpa: introduce vhost-vdpa backend")
Cc Cindy Lu <[email protected]>
Quoted 5 lines
Quoted 3 lines

I am a bit lost here. Isn't this invoked
when guest attempts to read the config?
With no peer, what do we return to guest?

Same as with a non-vdpa peer? It's the dereference that needs to be guarded.

Quoted 7 lines

So vdpa has a GET_CONFIG ioctl which to me hints that a peer needs to be notified on get config calls.
If we return config from qemu memory here, then I guess we need to call GET_CONFIG on connect and validate it -
does this make sense?

Cindy, Jason?

For "connect" you meant connecting virtio-net to its peer (vDPA)? AFAIK, if we start with no peer, there's no way to set a peer afterwards.

Quoted 8 lines
Quoted 1 line

Thanks

That would be a good sentence to add in a code comment:

/*
* Is this VDPA? No peer means not VDPA: there's no way to
* disconnect/reconnect a VDPA peer.
*/

Sure.

Thanks

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

A code comment might be helpful here.

Quoted 3 lines
---
   hw/net/virtio-net.c | 22 +++++++++++-----------
   1 file changed, 11 insertions(+), 11 deletions(-)