vhost: Add a defensive check in vhost_commit against wrong deallocation
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.
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
@@ -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
Quoted 14 lines · Eric Auger
In
vhost_commit(), it may happen thatdev->mem_sectionsanddev->tmp_sectionsare equal, in which case, unconditionally freeingold_sectionsat the end of the function will also freedev->mem_sectionsused on subsequent call leading to a segmentation fault.Check this situation before deallocating memory.
Signed-off-by Eric Auger <[email protected]>Fixes c44317efecb2 ("vhost: Build temporary section list and derefafter 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@@ -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
Hi Jason,
Quoted 14 lines · Jason Wang
Quoted 13 lines · Eric Auger
In
vhost_commit(), it may happen thatdev->mem_sectionsanddev->tmp_sectionsare equal, in which case, unconditionally freeingold_sectionsat the end of the function will also freedev->mem_sectionsused on subsequent call leading to a segmentation fault.Check this situation before deallocating memory.
Signed-off-by Eric Auger <[email protected]>Fixes c44317efecb2 ("vhost: Build temporary section list and derefafter 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@@ -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
Quoted 2 lines · Eric Auger
In
vhost_commit(), it may happen thatdev->mem_sectionsanddev->tmp_sectionsare equal,
Could you please explain a bit more how this can happen? I don't see how.
Quoted 12 lines
in which case, unconditionally
freeingold_sectionsat the end of the function will also freedev->mem_sectionsused on subsequent call leading to a segmentation fault.Check this situation before deallocating memory.
Signed-off-by Eric Auger <[email protected]>Fixes c44317efecb2 ("vhost: Build temporary section list and derefafter 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@@ -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
Hi Michael,
Quoted 18 lines · Michael S. Tsirkin
Quoted 2 lines · Eric Auger
In
vhost_commit(), it may happen thatdev->mem_sectionsanddev->tmp_sectionsare equal,Could you please explain a bit more how this can happen? I don't see how.
Quoted 12 lines
in which case, unconditionally
freeingold_sectionsat the end of the function will also freedev->mem_sectionsused on subsequent call leading to a segmentation fault.Check this situation before deallocating memory.
Signed-off-by Eric Auger <[email protected]>Fixes c44317efecb2 ("vhost: Build temporary section list and derefafter 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@@ -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