Skip to content

Commit

Permalink
virtio-net: check the existence of peer before accessing vDPA config
Browse files Browse the repository at this point in the history
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: 108a648 ("vhost-vdpa: introduce vhost-vdpa backend")
Cc: Cindy Lu <[email protected]>
Tested-by: Cornelia Huck <[email protected]>
Reviewed-by: Cornelia Huck <[email protected]>
Signed-off-by: Jason Wang <[email protected]>
  • Loading branch information
jasowang committed Jul 28, 2020
1 parent a48aaf8 commit c546ecf
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions hw/net/virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -142,20 +143,24 @@ 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) {
/*
* 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,
n->config_size);
if (ret != -1) {
memcpy(config, &netcfg, n->config_size);
}
n->config_size);
if (ret != -1) {
memcpy(config, &netcfg, n->config_size);
}
}
}

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);

Expand All @@ -166,11 +171,14 @@ 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);
/*
* 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) {
vhost_net_set_config(get_vhost_net(nc->peer),
(uint8_t *)&netcfg, 0, n->config_size,
VHOST_SET_CONFIG_TYPE_MASTER);
}
}

Expand Down

0 comments on commit c546ecf

Please sign in to comment.