* [PATCH V2] virtio-net: check the existence of peer before accessing vDPA config
@ 2020-07-27 12:51 Jason Wang
2020-07-27 13:05 ` Michael S. Tsirkin
0 siblings, 1 reply; 2+ messages in thread
From: Jason Wang @ 2020-07-27 12:51 UTC (permalink / raw)
To: mst, jasowang, qemu-devel; +Cc: cohuck, Cindy Lu
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: 108a64818e69b ("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>
---
Changes from V1:
- Tweak the log
- Add the comment in the code
---
hw/net/virtio-net.c | 30 +++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 4895af1cbe..a1fe9e9285 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -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,16 @@ 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);
+ }
}
}
@@ -156,6 +160,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 +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);
}
}
--
2.20.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH V2] virtio-net: check the existence of peer before accessing vDPA config
2020-07-27 12:51 [PATCH V2] virtio-net: check the existence of peer before accessing vDPA config Jason Wang
@ 2020-07-27 13:05 ` Michael S. Tsirkin
0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2020-07-27 13:05 UTC (permalink / raw)
To: Jason Wang; +Cc: cohuck, qemu-devel, Cindy Lu
On Mon, Jul 27, 2020 at 08:51:50PM +0800, Jason Wang wrote:
> 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: 108a64818e69b ("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>
> ---
> Changes from V1:
> - Tweak the log
> - Add the comment in the code
> ---
> hw/net/virtio-net.c | 30 +++++++++++++++++++-----------
> 1 file changed, 19 insertions(+), 11 deletions(-)
>
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index 4895af1cbe..a1fe9e9285 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -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,16 @@ 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);
> + }
> }
> }
>
> @@ -156,6 +160,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 +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);
> }
> }
Do we want a helper for this? E.g. qemu_test_peer_type?
Anyway:
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> --
> 2.20.1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-07-27 13:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-27 12:51 [PATCH V2] virtio-net: check the existence of peer before accessing vDPA config Jason Wang
2020-07-27 13:05 ` Michael S. Tsirkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).