Skip to contentSkip to conversation

vhost: Add a defensive check in vhost_commit against wrong deallocation

PATCH In review Eric Auger 3 participants 5 messages Sep 13, 2023 – Oct 3, 2023

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

PATCH 1/1 vhost: Add a defensive check in vhost_commit against wrong deallocation

In vhost_commit(), it may happen that dev->mem_sections and dev->tmp_sections are equal, in which case, unconditionally freeing old_sections at the end of the function will also free dev->mem_sections used on subsequent call leading to a segmentation fault.

Check this situation before deallocating memory.

Fixes c44317efecb2 ("vhost: Build temporary section list and deref

after commit")
CC: QEMU Stable <[email protected]>

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/[email protected]/#r
This is most probably happening in a situation where the memory API is used in a wrong manner but well.

---
 hw/virtio/vhost.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/vhost.c +5 -0
index e2f6ffb446..c02c599ef0 100644
@@ -545,6 +545,11 @@ static void vhost_commit(MemoryListener *listener)
     dev->mem_sections = dev->tmp_sections;
     dev->n_mem_sections = dev->n_tmp_sections;
 
+    if (old_sections == dev->mem_sections) {
+        assert(n_old_sections ==  dev->n_mem_sections);
+        return;
+    }
+
     if (dev->n_mem_sections != n_old_sections) {
         changed = true;
     } else {
Signature · 1 line
2.41.0
#1 lore raw
Quoted 14 lines · Eric Auger

In vhost_commit(), it may happen that dev->mem_sections and dev->tmp_sections are equal, in which case, unconditionally freeing old_sections at the end of the function will also free dev->mem_sections used on subsequent call leading to a segmentation fault.

Check this situation before deallocating memory.

Fixes c44317efecb2 ("vhost: Build temporary section list and deref

after commit")
CC: QEMU Stable <[email protected]>

Quoted 6 lines

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/[email protected]/#r
This is most probably happening in a situation where the memory API is used in a wrong manner but well.

Any chance to move this to the memory API or we may end up with things like this in another listener?

Thanks

Quoted 23 lines
---
 hw/virtio/vhost.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/vhost.c +5 -1
index e2f6ffb446..c02c599ef0 100644
@@ -545,6 +545,11 @@ static void vhost_commit(MemoryListener *listener)
     dev->mem_sections = dev->tmp_sections;
     dev->n_mem_sections = dev->n_tmp_sections;
 
+    if (old_sections == dev->mem_sections) {
+        assert(n_old_sections ==  dev->n_mem_sections);
+        return;
+    }
+
     if (dev->n_mem_sections != n_old_sections) {
         changed = true;
     } else {
--

2.41.0

author
#2 lore raw

Hi Jason,

Quoted 14 lines · Jason Wang
Quoted 13 lines · Eric Auger

In vhost_commit(), it may happen that dev->mem_sections and dev->tmp_sections are equal, in which case, unconditionally freeing old_sections at the end of the function will also free dev->mem_sections used on subsequent call leading to a segmentation fault.

Check this situation before deallocating memory.

Fixes c44317efecb2 ("vhost: Build temporary section list and deref

after commit")
CC: QEMU Stable <[email protected]>

Quoted 8 lines
Quoted 6 lines

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/[email protected]/#r
This is most probably happening in a situation where the memory API is used in a wrong manner but well.

Any chance to move this to the memory API or we may end up with things like this in another listener?

I am not very familiar with the vhost code but aren't those tmp_sections and mem_sections really specific to the vhost device? I am not sure we can easily generalize.

Thanks

Eric

Quoted 26 lines

Thanks

Quoted 23 lines
---
 hw/virtio/vhost.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/vhost.c +5 -1
index e2f6ffb446..c02c599ef0 100644
@@ -545,6 +545,11 @@ static void vhost_commit(MemoryListener *listener)
     dev->mem_sections = dev->tmp_sections;
     dev->n_mem_sections = dev->n_tmp_sections;
 
+    if (old_sections == dev->mem_sections) {
+        assert(n_old_sections ==  dev->n_mem_sections);
+        return;
+    }
+
     if (dev->n_mem_sections != n_old_sections) {
         changed = true;
     } else {
--

2.41.0

#3 lore raw
In reply to Eric Auger
Quoted 2 lines · Eric Auger

In vhost_commit(), it may happen that dev->mem_sections and dev->tmp_sections are equal,

Could you please explain a bit more how this can happen? I don't see how.

Quoted 12 lines

in which case, unconditionally
freeing old_sections at the end of the function will also free dev->mem_sections used on subsequent call leading to a segmentation fault.

Check this situation before deallocating memory.

Fixes c44317efecb2 ("vhost: Build temporary section list and deref

after commit")
CC: QEMU Stable <[email protected]>

Quoted 6 lines

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/[email protected]/#r
This is most probably happening in a situation where the memory API is used in a wrong manner but well.

sounds like misusing the memory API can lead to all kind of mischief.

Quoted 22 lines
---
 hw/virtio/vhost.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/vhost.c +5 -0
index e2f6ffb446..c02c599ef0 100644
@@ -545,6 +545,11 @@ static void vhost_commit(MemoryListener *listener)
     dev->mem_sections = dev->tmp_sections;
     dev->n_mem_sections = dev->n_tmp_sections;
 
+    if (old_sections == dev->mem_sections) {
+        assert(n_old_sections ==  dev->n_mem_sections);
+        return;
+    }
+
     if (dev->n_mem_sections != n_old_sections) {
         changed = true;
     } else {
Signature · 1 line
2.41.0
author
#4 lore raw

Hi Michael,

Quoted 18 lines · Michael S. Tsirkin
Quoted 2 lines · Eric Auger

In vhost_commit(), it may happen that dev->mem_sections and dev->tmp_sections are equal,

Could you please explain a bit more how this can happen? I don't see how.

Quoted 12 lines

in which case, unconditionally
freeing old_sections at the end of the function will also free dev->mem_sections used on subsequent call leading to a segmentation fault.

Check this situation before deallocating memory.

Fixes c44317efecb2 ("vhost: Build temporary section list and deref

after commit")
CC: QEMU Stable <[email protected]>

Quoted 7 lines
Quoted 6 lines

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/[email protected]/#r
This is most probably happening in a situation where the memory API is used in a wrong manner but well.

sounds like misusing the memory API can lead to all kind of mischief.

This happened in a situation where I resized an [IOMMU] MR within the VFIO vfio_listener_region_add leading to recursive calls ot region_add callbacks.
The issue is it was not straightforward to find the link with vhost.

Thanks

Eric

Quoted 23 lines
Quoted 23 lines
---
 hw/virtio/vhost.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/virtio/vhost.c +5 -0
index e2f6ffb446..c02c599ef0 100644
@@ -545,6 +545,11 @@ static void vhost_commit(MemoryListener *listener)
     dev->mem_sections = dev->tmp_sections;
     dev->n_mem_sections = dev->n_tmp_sections;
 
+    if (old_sections == dev->mem_sections) {
+        assert(n_old_sections ==  dev->n_mem_sections);
+        return;
+    }
+
     if (dev->n_mem_sections != n_old_sections) {
         changed = true;
     } else {
Signature · 1 line
2.41.0