Skip to contentSkip to conversation

libvhost-user: honor the offset of VHOST_USER_GET_CONFIG

PATCH New Changyuan Lyu 1 participant 1 message Nov 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libvhost-user: honor the offset of VHOST_USER_GET_CONFIG

In the vhost-user specification, the payload of VHOST_USER_GET_CONFIG carries an offset into the config space of the virtio device, an access size in bytes, and a byte array of that size with the contents of the config space (see "Virtio device config space" in docs/interop/vhost-user.rst). So a front-end may read any part of the config space.

vu_get_config() hands the payload buffer and the size of the request to the get_config() callback of the device, and ignores the offset. The callback takes no offset and fills in the config space from its start, so the reply always carries the first bytes of the config space, whatever offset the front-end asked for.

QEMU does not notice, because it always reads the whole config space from offset 0. A front-end that forwards each config space access of the guest driver gets wrong data for every access at a non-zero offset. For example, with a vhost-user-blk export of qemu-storage-daemon, a Linux guest gets the low bytes of the capacity in place of num_queues and fails to probe the device:

virtio_blk virtio0: MQ advertised but zero queues reported virtio_blk virtio0: probe with driver virtio_blk failed with error -22

Ask the callback for the config space up to the end of the requested range, and move the range to the start of the reply. This keeps the callback as it is, so no device has to change. Reply with an error, a zero-length payload as the specification says, if the range does not fit in VHOST_USER_MAX_CONFIG_SIZE. The callbacks already reject a length beyond the end of their config space.

Fixes 0bc24d831e49 ("contrib/libvhost-user: enable virtio config space messages")
---
 subprojects/libvhost-user/libvhost-user.c | 17 ++++++++++++++---
 subprojects/libvhost-user/libvhost-user.h |  2 +-
 2 files changed, 15 insertions(+), 4 deletions(-)
subprojects/libvhost-user/libvhost-user.c +14 -3
index 52817cc733..a55899919c 100644
@@ -1902,16 +1902,27 @@ vu_set_backend_req_fd(VuDev *dev, VhostUserMsg *vmsg)
 static bool
 vu_get_config(VuDev *dev, VhostUserMsg *vmsg)
 {
+    uint32_t offset = vmsg->payload.config.offset;
+    uint32_t size = vmsg->payload.config.size;
+    uint8_t *region = vmsg->payload.config.region;
     int ret = -1;
 
-    if (dev->iface->get_config) {
-        ret = dev->iface->get_config(dev, vmsg->payload.config.region,
-                                     vmsg->payload.config.size);
+    /*
+     * The get_config() callback takes no offset and fills in the config
+     * space from its start. So get everything up to the end of the
+     * requested range, and move the range to the start of the reply.
+     */
+    if (dev->iface->get_config &&
+        offset <= VHOST_USER_MAX_CONFIG_SIZE &&
+        size <= VHOST_USER_MAX_CONFIG_SIZE - offset) {
+        ret = dev->iface->get_config(dev, region, offset + size);
     }
 
     if (ret) {
         /* resize to zero to indicate an error to frontend */
         vmsg->size = 0;
+    } else if (offset > 0) {
+        memmove(region, region + offset, size);
     }
 
     return true;
subprojects/libvhost-user/libvhost-user.h +1 -1
index a534d70771..163adad719 100644
@@ -290,7 +290,7 @@ typedef struct VuDevIface {
      * on unmanaged exit/crash.
      */
     vu_queue_is_processed_in_order_cb queue_is_processed_in_order;
-    /* get the config space of the device */
+    /* get the first len bytes of the config space of the device */
     vu_get_config_cb get_config;
     /* set the config space of the device */
     vu_set_config_cb set_config;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog