virtio-net: check the existence of peer before accessing vDPA config

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.

Fixes: 108a64818e ("vhost-vdpa: introduce vhost-vdpa backend")
Cc: Cindy Lu <lulu@redhat.com>
Tested-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Jason Wang 2020-07-25 08:13:17 +08:00
parent a48aaf882b
commit c546ecf27d

View File

@ -125,6 +125,7 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
{ {
VirtIONet *n = VIRTIO_NET(vdev); VirtIONet *n = VIRTIO_NET(vdev);
struct virtio_net_config netcfg; struct virtio_net_config netcfg;
NetClientState *nc = qemu_get_queue(n->nic);
int ret = 0; int ret = 0;
memset(&netcfg, 0 , sizeof(struct virtio_net_config)); memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@ -142,8 +143,11 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
VIRTIO_NET_RSS_SUPPORTED_HASHES); VIRTIO_NET_RSS_SUPPORTED_HASHES);
memcpy(config, &netcfg, n->config_size); memcpy(config, &netcfg, n->config_size);
NetClientState *nc = qemu_get_queue(n->nic); /*
if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) { * Is this VDPA? No peer means not VDPA: there's no way to
* disconnect/reconnect a VDPA peer.
*/
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, ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
n->config_size); n->config_size);
if (ret != -1) { if (ret != -1) {
@ -156,6 +160,7 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
{ {
VirtIONet *n = VIRTIO_NET(vdev); VirtIONet *n = VIRTIO_NET(vdev);
struct virtio_net_config netcfg = {}; struct virtio_net_config netcfg = {};
NetClientState *nc = qemu_get_queue(n->nic);
memcpy(&netcfg, config, n->config_size); memcpy(&netcfg, config, n->config_size);
@ -166,10 +171,13 @@ static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); 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) { * Is this VDPA? No peer means not VDPA: there's no way to
vhost_net_set_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg, * disconnect/reconnect a VDPA peer.
0, n->config_size, */
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); VHOST_SET_CONFIG_TYPE_MASTER);
} }
} }