Skip to contentSkip to conversation

Clean up error handling in libvhost-user memory mapping

PATCH Superseded Raphael Norwitz 4 participants 22 messages Jan 6, 2022 – Jan 11, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/5 libvhost-user: Add vu_rem_mem_reg input validation

In reply to Raphael Norwitz
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +6 -0
index 787f4d2d4f..a6dadeb637 100644
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
 
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
+        return false;
+    }
+
     DPRINT("Removing region:\n");
     DPRINT("    guest_phys_addr: 0x%016"PRIx64"\n",
            msg_region->guest_phys_addr);
Signature · 1 line
2.20.1
#7 lore raw

PATCH v2 2/5 libvhost-user: Add vu_add_mem_reg input validation

In reply to Raphael Norwitz
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +6 -0
index a6dadeb637..d61285e991 100644
@@ -690,6 +690,12 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];
     void *mmap_addr;
 
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
+        return false;
+    }
+
     /*
      * If we are in postcopy mode and we receive a u64 payload with a 0 value
      * we know all the postcopy client bases have been received, and we
Signature · 1 line
2.20.1
#10 lore raw

PATCH v2 4/5 libvhost-user: prevent over-running max RAM slots

In reply to Raphael Norwitz

When VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS support was added to libvhost-user, no guardrails were added to protect against QEMU attempting to hot-add too many RAM slots to a VM with a libvhost-user based backed attached.

This change adds the missing error handling by introducing a check on the number of RAM slots the device has available before proceeding to process the VHOST_USER_ADD_MEM_REG message.

---
 subprojects/libvhost-user/libvhost-user.c | 5 +++++
 1 file changed, 5 insertions(+)
subprojects/libvhost-user/libvhost-user.c +5 -0
index 77ddc96ddf..0fe3aa155b 100644
@@ -690,6 +690,11 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];
     void *mmap_addr;
 
+    if (dev->nregions == VHOST_USER_MAX_RAM_SLOTS) {
+        vu_panic(dev, "No free ram slots available");
+        return false;
+    }
+
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
         vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
Signature · 1 line
2.20.1
#0 lore raw

v2 cover Clean up error handling in libvhost-user memory mapping

Hey Stefan, Marc-Andre, MST, David -

As promised here is a series cleaning up error handling in the libvhost-user memory mapping path. Most of these cleanups are straightforward and have been discussed on the mailing list in threads
[1] and [2].

[1] https://lore.kernel.org/qemu-devel/20211018143319.GA11006@raphael-debian-dev/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Changes since V1:
* Checks for a single fd vu_add_mem_reg and vu_rem_mem_reg return false instead of true.
* Check for over-running max ram slots in vu_add_mem_reg returns false instead of true.
* vu_rem_mem_reg unmaps all matching regions.
* Decriment iterator variable when looping through regions in vu_rem_mem_reg to ensure matching regions aren’t missed.

Thanks,
Raphael

David Hildenbrand (1):
libvhost-user: Simplify VHOST_USER_REM_MEM_REG

Raphael Norwitz (4):
libvhost-user: Add vu_rem_mem_reg input validation
libvhost-user: Add vu_add_mem_reg input validation
libvhost-user: prevent over-running max RAM slots
libvhost-user: handle removal of identical regions

 subprojects/libvhost-user/libvhost-user.c | 51 +++++++++++++++--------
 1 file changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
2.20.1
#15 lore raw

PATCH v2 5/5 libvhost-user: handle removal of identical regions

Today if QEMU (or any other VMM) has sent multiple copies of the same region to a libvhost-user based backend and then attempts to remove the region, only one instance of the region will be removed, leaving stale copies of the region in dev->regions[].

This change resolves this by having vu_rem_mem_reg() iterate through all regions in dev->regions[] and delete all matching regions.

---
 subprojects/libvhost-user/libvhost-user.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)
subprojects/libvhost-user/libvhost-user.c +14 -12
index 0fe3aa155b..14482484d3 100644
@@ -809,6 +809,7 @@ static bool
 vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
     int i;
+    bool found = false;
 
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
@@ -835,21 +836,22 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
                 munmap(m, r->size + r->mmap_offset);
             }
 
-            break;
+            /*
+             * Shift all affected entries by 1 to close the hole at index i and
+             * zero out the last entry.
+             */
+            memmove(dev->regions + i, dev->regions + i + 1,
+                    sizeof(VuDevRegion) * (dev->nregions - i - 1));
+            memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion));
+            DPRINT("Successfully removed a region\n");
+            dev->nregions--;
+            i--;
+
+            found = true;
         }
     }
 
-    if (i < dev->nregions) {
-        /*
-         * Shift all affected entries by 1 to close the hole at index i and
-         * zero out the last entry.
-         */
-        memmove(dev->regions + i, dev->regions + i + 1,
-               sizeof(VuDevRegion) * (dev->nregions - i - 1));
-        memset(dev->regions + dev->nregions - 1, 0,
-               sizeof(VuDevRegion));
-        DPRINT("Successfully removed a region\n");
-        dev->nregions--;
+    if (found) {
         vmsg_set_reply_u64(vmsg, 0);
     } else {
         vu_panic(dev, "Specified region not found\n");
Signature · 1 line
2.20.1
#9 lore raw

PATCH v2 3/5 libvhost-user: Simplify VHOST_USER_REM_MEM_REG

In reply to Raphael Norwitz

From: David Hildenbrand <[email protected]>

Let's avoid having to manually copy all elements. Copy only the ones necessary to close the hole and perform the operation in-place without a second array.

---
 subprojects/libvhost-user/libvhost-user.c | 30 +++++++++++------------
 1 file changed, 14 insertions(+), 16 deletions(-)
subprojects/libvhost-user/libvhost-user.c +14 -16
index d61285e991..77ddc96ddf 100644
@@ -802,10 +802,8 @@ static inline bool reg_equal(VuDevRegion *vudev_reg,
 
 static bool
 vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
-    int i, j;
-    bool found = false;
-    VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
+    int i;
 
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
@@ -823,28 +821,28 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     DPRINT("    mmap_offset      0x%016"PRIx64"\n",
            msg_region->mmap_offset);
 
-    for (i = 0, j = 0; i < dev->nregions; i++) {
-        if (!reg_equal(&dev->regions[i], msg_region)) {
-            shadow_regions[j].gpa = dev->regions[i].gpa;
-            shadow_regions[j].size = dev->regions[i].size;
-            shadow_regions[j].qva = dev->regions[i].qva;
-            shadow_regions[j].mmap_addr = dev->regions[i].mmap_addr;
-            shadow_regions[j].mmap_offset = dev->regions[i].mmap_offset;
-            j++;
-        } else {
-            found = true;
+    for (i = 0; i < dev->nregions; i++) {
+        if (reg_equal(&dev->regions[i], msg_region)) {
             VuDevRegion *r = &dev->regions[i];
             void *m = (void *) (uintptr_t) r->mmap_addr;
 
             if (m) {
                 munmap(m, r->size + r->mmap_offset);
             }
+
+            break;
         }
     }
 
-    if (found) {
-        memcpy(dev->regions, shadow_regions,
-               sizeof(VuDevRegion) * VHOST_USER_MAX_RAM_SLOTS);
+    if (i < dev->nregions) {
+        /*
+         * Shift all affected entries by 1 to close the hole at index i and
+         * zero out the last entry.
+         */
+        memmove(dev->regions + i, dev->regions + i + 1,
+               sizeof(VuDevRegion) * (dev->nregions - i - 1));
+        memset(dev->regions + dev->nregions - 1, 0,
+               sizeof(VuDevRegion));
         DPRINT("Successfully removed a region\n");
         dev->nregions--;
         vmsg_set_reply_u64(vmsg, 0);
Signature · 1 line
2.20.1
Reviewed-by on v2 2/5
#8 lore raw

Re: [PATCH v2 2/5] libvhost-user: Add vu_add_mem_reg input validation

In reply to Raphael Norwitz
Quoted 12 lines · Raphael Norwitz
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index a6dadeb637..d61285e991 100644
@@ -690,6 +690,12 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];
     void *mmap_addr;

subprojects/libvhost-user/libvhost-user.c vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 701

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +6 -0
 
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
+        return false;
+    }
+
     /*
      * If we are in postcopy mode and we receive a u64 payload with a 0 value
      * we know all the postcopy client bases have been received, and we
Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] libvhost-user: Add vu_rem_mem_reg input validation

In reply to Raphael Norwitz
Quoted 12 lines · Raphael Norwitz
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 787f4d2d4f..a6dadeb637 100644
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 812

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +6 -0
 
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
+        return false;
+    }
+
     DPRINT("Removing region:\n");
     DPRINT("    guest_phys_addr: 0x%016"PRIx64"\n",
            msg_region->guest_phys_addr);
Signature · 3 lines
Thanks,

David / dhildenb
#16 lore raw

Re: [PATCH v2 5/5] libvhost-user: handle removal of identical regions

In reply to Raphael Norwitz
Quoted 33 lines · Raphael Norwitz

Today if QEMU (or any other VMM) has sent multiple copies of the same region to a libvhost-user based backend and then attempts to remove the region, only one instance of the region will be removed, leaving stale copies of the region in dev->regions[].

This change resolves this by having vu_rem_mem_reg() iterate through all regions in dev->regions[] and delete all matching regions.

---
 subprojects/libvhost-user/libvhost-user.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)
subprojects/libvhost-user/libvhost-user.c +3 -1
index 0fe3aa155b..14482484d3 100644
@@ -809,6 +809,7 @@ static bool
 vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
     int i;
+    bool found = false;
 
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
@@ -835,21 +836,22 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
                 munmap(m, r->size + r->mmap_offset);
             }
 
-            break;
+            /*
+             * Shift all affected entries by 1 to close the hole at index i and

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 850

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10 -0
+             * zero out the last entry.
+             */
+            memmove(dev->regions + i, dev->regions + i + 1,
+                    sizeof(VuDevRegion) * (dev->nregions - i - 1));
+            memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion));
+            DPRINT("Successfully removed a region\n");
+            dev->nregions--;
+            i--;
+
+            found = true;

Maybe add a comment like

/* Continue the search for eventual duplicates. */

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v2 4/5
#11 lore raw

Re: [PATCH v2 4/5] libvhost-user: prevent over-running max RAM slots

In reply to Raphael Norwitz
Quoted 21 lines · Raphael Norwitz

When VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS support was added to libvhost-user, no guardrails were added to protect against QEMU attempting to hot-add too many RAM slots to a VM with a libvhost-user based backed attached.

This change adds the missing error handling by introducing a check on the number of RAM slots the device has available before proceeding to process the VHOST_USER_ADD_MEM_REG message.

---
 subprojects/libvhost-user/libvhost-user.c | 5 +++++
 1 file changed, 5 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 77ddc96ddf..0fe3aa155b 100644
@@ -690,6 +690,11 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];

subprojects/libvhost-user/libvhost-user.c vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 700

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +5 -0
     void *mmap_addr;
 
+    if (dev->nregions == VHOST_USER_MAX_RAM_SLOTS) {
+        vu_panic(dev, "No free ram slots available");
+        return false;
+    }
+
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
         vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
Signature · 3 lines
Thanks,

David / dhildenb
#19 lore raw
In reply to Raphael Norwitz
Quoted 7 lines · Raphael Norwitz

Hey Stefan, Marc-Andre, MST, David -

As promised here is a series cleaning up error handling in the libvhost-user memory mapping path. Most of these cleanups are straightforward and have been discussed on the mailing list in threads
[1] and [2].

A note that we still want the fix in [3] upstream:

[3] https://lkml.kernel.org/r/[email protected]

Signature · 3 lines
Thanks,

David / dhildenb
#3 lore raw

Re: [PATCH v2 1/5] libvhost-user: Add vu_rem_mem_reg input validation

In reply to Raphael Norwitz
Quoted 1 line · Raphael Norwitz

Raphael any chance you can add a bit more to commit logs? E.g. what happens right now if you pass more?

Quoted 9 lines
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 787f4d2d4f..a6dadeb637 100644

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 805

Quoted 5 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
 
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {

Is there a chance someone is sending larger messages and relying on libvhost-user to ignore padding?

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 806

Quoted 1 line
subprojects/libvhost-user/libvhost-user.c +1 -0
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");

Maybe print the parameters that caused the issue?

Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +3 -0
+        return false;
+    }
+
     DPRINT("Removing region:\n");
     DPRINT("    guest_phys_addr: 0x%016"PRIx64"\n",
            msg_region->guest_phys_addr);
Signature · 1 line
2.20.1
#12 lore raw

Re: [PATCH v2 4/5] libvhost-user: prevent over-running max RAM slots

In reply to Raphael Norwitz
Quoted 20 lines · Raphael Norwitz

When VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS support was added to libvhost-user, no guardrails were added to protect against QEMU attempting to hot-add too many RAM slots to a VM with a libvhost-user based backed attached.

This change adds the missing error handling by introducing a check on the number of RAM slots the device has available before proceeding to process the VHOST_USER_ADD_MEM_REG message.

---
 subprojects/libvhost-user/libvhost-user.c | 5 +++++
 1 file changed, 5 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 77ddc96ddf..0fe3aa155b 100644

subprojects/libvhost-user/libvhost-user.c vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 694

Quoted 5 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
@@ -690,6 +690,11 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];
     void *mmap_addr;
 
+    if (dev->nregions == VHOST_USER_MAX_RAM_SLOTS) {
+        vu_panic(dev, "No free ram slots available");

A bit more verbose maybe? Describe what happened to trigger this? e.g. adding a region with no free ram slots?

Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +3 -0
+        return false;
+    }
+
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
         vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
Signature · 1 line
2.20.1
Reviewed-by on v2 4/5
#14 lore raw

Re: [PATCH v2 4/5] libvhost-user: prevent over-running max RAM slots

In reply to Raphael Norwitz
Quoted 9 lines · Raphael Norwitz

When VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS support was added to libvhost-user, no guardrails were added to protect against QEMU attempting to hot-add too many RAM slots to a VM with a libvhost-user based backed attached.

This change adds the missing error handling by introducing a check on the number of RAM slots the device has available before proceeding to process the VHOST_USER_ADD_MEM_REG message.

Quoted 5 lines
---
 subprojects/libvhost-user/libvhost-user.c | 5 +++++
 1 file changed, 5 insertions(+)
Reviewed-by on the v2 series
#21 lore raw
In reply to Raphael Norwitz
Quoted 25 lines · Raphael Norwitz

Hey Stefan, Marc-Andre, MST, David -

As promised here is a series cleaning up error handling in the libvhost-user memory mapping path. Most of these cleanups are straightforward and have been discussed on the mailing list in threads
[1] and [2].

[1] https://lore.kernel.org/qemu-devel/20211018143319.GA11006@raphael-debian-dev/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Changes since V1:
* Checks for a single fd vu_add_mem_reg and vu_rem_mem_reg return false instead of true.
* Check for over-running max ram slots in vu_add_mem_reg returns false instead of true.
* vu_rem_mem_reg unmaps all matching regions.
* Decriment iterator variable when looping through regions in vu_rem_mem_reg to ensure matching regions aren’t missed.

Thanks,
Raphael

David Hildenbrand (1):
libvhost-user: Simplify VHOST_USER_REM_MEM_REG

Quoted 11 lines

Raphael Norwitz (4):
libvhost-user: Add vu_rem_mem_reg input validation
libvhost-user: Add vu_add_mem_reg input validation
libvhost-user: prevent over-running max RAM slots
libvhost-user: handle removal of identical regions

 subprojects/libvhost-user/libvhost-user.c | 51 +++++++++++++++--------
 1 file changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
2.20.1
Reviewed-by on v2 5/5
#18 lore raw

Re: [PATCH v2 5/5] libvhost-user: handle removal of identical regions

In reply to Raphael Norwitz
Quoted 5 lines · Raphael Norwitz

Today if QEMU (or any other VMM) has sent multiple copies of the same region to a libvhost-user based backend and then attempts to remove the region, only one instance of the region will be removed, leaving stale copies of the region in dev->regions[].

Quoted 8 lines

This change resolves this by having vu_rem_mem_reg() iterate through all regions in dev->regions[] and delete all matching regions.

---
 subprojects/libvhost-user/libvhost-user.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)
#4 lore raw

Re: [PATCH v2 1/5] libvhost-user: Add vu_rem_mem_reg input validation

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 1 line · Raphael Norwitz

Raphael any chance you can add a bit more to commit logs? E.g. what happens right now if you pass more?

Sure - I'll add those details.

Quoted 12 lines
Quoted 12 lines
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 787f4d2d4f..a6dadeb637 100644
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
 
Quoted 6 lines

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 805

Quoted 2 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {

Is there a chance someone is sending larger messages and relying on libvhost-user to ignore padding?

Great point - I didn't consider padding. I'll drop the vmsg->size check here.

It looks like we are inconsistent with size checking. For example, in vu_set_log_base_exec() we also check the size.

Should we make it consistent across the library or am I missing some details about why the padding is not an issue in that case?

Quoted 4 lines

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 806

Quoted 1 line
subprojects/libvhost-user/libvhost-user.c +1 -0
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");

Maybe print the parameters that caused the issue?

Ack.

Quoted 9 lines
Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +3 -0
+        return false;
+    }
+
     DPRINT("Removing region:\n");
     DPRINT("    guest_phys_addr: 0x%016"PRIx64"\n",
            msg_region->guest_phys_addr);
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH v2 1/5] libvhost-user: Add vu_rem_mem_reg input validation

Quoted 30 lines · Raphael Norwitz
Quoted 7 lines · Michael S. Tsirkin
Quoted 1 line · Raphael Norwitz

Raphael any chance you can add a bit more to commit logs? E.g. what happens right now if you pass more?

Sure - I'll add those details.

Quoted 12 lines
Quoted 12 lines
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 787f4d2d4f..a6dadeb637 100644
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
 
Quoted 6 lines

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 805

Quoted 2 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {

Is there a chance someone is sending larger messages and relying on libvhost-user to ignore padding?

Quoted 8 lines

Great point - I didn't consider padding. I'll drop the vmsg->size check here.

It looks like we are inconsistent with size checking. For example, in vu_set_log_base_exec() we also check the size.

Should we make it consistent across the library or am I missing some details about why the padding is not an issue in that case?

Not sure. We don't allow arbitrary padding do we? That would require extra processing to skip padding in case it's very large. Let's try to come up with a policy and document it as part of the spec?

Quoted 16 lines
Quoted 4 lines

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 806

Quoted 1 line
subprojects/libvhost-user/libvhost-user.c +1 -0
+        vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");

Maybe print the parameters that caused the issue?

Ack.

Quoted 9 lines
Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +3 -0
+        return false;
+    }
+
     DPRINT("Removing region:\n");
     DPRINT("    guest_phys_addr: 0x%016"PRIx64"\n",
            msg_region->guest_phys_addr);
Signature · 1 line
2.20.1
#20 lore raw
In reply to David Hildenbrand
Quoted 9 lines · David Hildenbrand
Quoted 7 lines · Raphael Norwitz

Hey Stefan, Marc-Andre, MST, David -

As promised here is a series cleaning up error handling in the libvhost-user memory mapping path. Most of these cleanups are straightforward and have been discussed on the mailing list in threads
[1] and [2].

Quoted 3 lines

A note that we still want the fix in [3] upstream:

[3] https://lore.kernel.org/all/[email protected]/T/#u

Ah I thought it was merged.

I'll add it to the series to ensure it doesn't get lost.

Quoted 7 lines
Signature · 4 lines
-- 
Thanks,

David / dhildenb
#13 lore raw

Re: [PATCH v2 4/5] libvhost-user: prevent over-running max RAM slots

In reply to Michael S. Tsirkin
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Raphael Norwitz

When VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS support was added to libvhost-user, no guardrails were added to protect against QEMU attempting to hot-add too many RAM slots to a VM with a libvhost-user based backed attached.

This change adds the missing error handling by introducing a check on the number of RAM slots the device has available before proceeding to process the VHOST_USER_ADD_MEM_REG message.

---
 subprojects/libvhost-user/libvhost-user.c | 5 +++++
 1 file changed, 5 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 77ddc96ddf..0fe3aa155b 100644
@@ -690,6 +690,11 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion *dev_region = &dev->regions[dev->nregions];
     void *mmap_addr;
 
Quoted 6 lines

subprojects/libvhost-user/libvhost-user.c vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 694

Quoted 2 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
+    if (dev->nregions == VHOST_USER_MAX_RAM_SLOTS) {
+        vu_panic(dev, "No free ram slots available");

A bit more verbose maybe? Describe what happened to trigger this? e.g. adding a region with no free ram slots?

Ack

Quoted 9 lines
Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +3 -0
+        return false;
+    }
+
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
         vu_panic(dev, "VHOST_USER_REM_MEM_REG received multiple regions");
Signature · 1 line
2.20.1
#17 lore raw

Re: [PATCH v2 5/5] libvhost-user: handle removal of identical regions

In reply to David Hildenbrand
Quoted 47 lines · David Hildenbrand
Quoted 33 lines · Raphael Norwitz

Today if QEMU (or any other VMM) has sent multiple copies of the same region to a libvhost-user based backend and then attempts to remove the region, only one instance of the region will be removed, leaving stale copies of the region in dev->regions[].

This change resolves this by having vu_rem_mem_reg() iterate through all regions in dev->regions[] and delete all matching regions.

---
 subprojects/libvhost-user/libvhost-user.c | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)
subprojects/libvhost-user/libvhost-user.c +3 -1
index 0fe3aa155b..14482484d3 100644
@@ -809,6 +809,7 @@ static bool
 vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
     int i;
+    bool found = false;
 
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.memreg)) {
@@ -835,21 +836,22 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
                 munmap(m, r->size + r->mmap_offset);
             }
 
-            break;
+            /*
+             * Shift all affected entries by 1 to close the hole at index i and

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 850

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10 -0
+             * zero out the last entry.
+             */
+            memmove(dev->regions + i, dev->regions + i + 1,
+                    sizeof(VuDevRegion) * (dev->nregions - i - 1));
+            memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion));
+            DPRINT("Successfully removed a region\n");
+            dev->nregions--;
+            i--;
+
+            found = true;

Maybe add a comment like

Quoted 1 line

/* Continue the search for eventual duplicates. */

Ack - I'll add it.

Quoted 7 lines
Signature · 4 lines
-- 
Thanks,

David / dhildenb
#6 lore raw

Re: [PATCH v2 1/5] libvhost-user: Add vu_rem_mem_reg input validation

In reply to Raphael Norwitz
Quoted 30 lines · Raphael Norwitz
Quoted 7 lines · Michael S. Tsirkin
Quoted 1 line · Raphael Norwitz

Raphael any chance you can add a bit more to commit logs? E.g. what happens right now if you pass more?

Sure - I'll add those details.

Quoted 12 lines
Quoted 12 lines
---
 subprojects/libvhost-user/libvhost-user.c | 6 ++++++
 1 file changed, 6 insertions(+)
subprojects/libvhost-user/libvhost-user.c +0 -0
index 787f4d2d4f..a6dadeb637 100644
@@ -801,6 +801,12 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
     VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
     VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
 
Quoted 6 lines

subprojects/libvhost-user/libvhost-user.c vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { · line 805

Quoted 2 lines
subprojects/libvhost-user/libvhost-user.c +2 -0
+    if (vmsg->fd_num != 1 ||
+        vmsg->size != sizeof(vmsg->payload.memreg)) {

Is there a chance someone is sending larger messages and relying on libvhost-user to ignore padding?

Quoted 2 lines

Great point - I didn't consider padding. I'll drop the vmsg->size check here.

A vmsg->size > sizeof(vmsg->payload.memreg) check is still reasonable.

Without a check we'll treat the 0-initialized vmsg bytes as input, which should be okay as long as there is input validation later on. In some cases 0s may really be valid input and we'll perform some operation. So in the end, I think checking vmsg->size is safest, just ignore extra bytes.

Stefan