qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation
@ 2023-09-13  7:46 Eric Auger
  2023-09-14  3:46 ` Jason Wang
  2023-10-03 12:43 ` Michael S. Tsirkin
  0 siblings, 2 replies; 5+ messages in thread
From: Eric Auger @ 2023-09-13  7:46 UTC (permalink / raw)
  To: eric.auger.pro, eric.auger, mst, qemu-devel; +Cc: jasowang, lvivier

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.

Signed-off-by: Eric Auger <eric.auger@redhat.com>
Fixes: c44317efecb2 ("vhost: Build temporary section list and deref
after commit")
CC: QEMU Stable <qemu-stable@nongnu.org>

---

This SIGSEV condition can be reproduced with
https://lore.kernel.org/all/20230904080451.424731-1-eric.auger@redhat.com/#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(+)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index e2f6ffb446..c02c599ef0 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -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



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation
  2023-09-13  7:46 [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation Eric Auger
@ 2023-09-14  3:46 ` Jason Wang
  2023-09-14  7:45   ` Eric Auger
  2023-10-03 12:43 ` Michael S. Tsirkin
  1 sibling, 1 reply; 5+ messages in thread
From: Jason Wang @ 2023-09-14  3:46 UTC (permalink / raw)
  To: Eric Auger; +Cc: eric.auger.pro, mst, qemu-devel, lvivier

On Wed, Sep 13, 2023 at 3:47 PM Eric Auger <eric.auger@redhat.com> wrote:
>
> 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.
>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> Fixes: c44317efecb2 ("vhost: Build temporary section list and deref
> after commit")
> CC: QEMU Stable <qemu-stable@nongnu.org>
>
> ---
>
> This SIGSEV condition can be reproduced with
> https://lore.kernel.org/all/20230904080451.424731-1-eric.auger@redhat.com/#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

> ---
>  hw/virtio/vhost.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> index e2f6ffb446..c02c599ef0 100644
> --- a/hw/virtio/vhost.c
> +++ b/hw/virtio/vhost.c
> @@ -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
>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation
  2023-09-14  3:46 ` Jason Wang
@ 2023-09-14  7:45   ` Eric Auger
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Auger @ 2023-09-14  7:45 UTC (permalink / raw)
  To: Jason Wang; +Cc: eric.auger.pro, mst, qemu-devel, lvivier

Hi Jason,

On 9/14/23 05:46, Jason Wang wrote:
> On Wed, Sep 13, 2023 at 3:47 PM Eric Auger <eric.auger@redhat.com> wrote:
>> 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.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> Fixes: c44317efecb2 ("vhost: Build temporary section list and deref
>> after commit")
>> CC: QEMU Stable <qemu-stable@nongnu.org>
>>
>> ---
>>
>> This SIGSEV condition can be reproduced with
>> https://lore.kernel.org/all/20230904080451.424731-1-eric.auger@redhat.com/#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
>
> Thanks
>
>> ---
>>  hw/virtio/vhost.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
>> index e2f6ffb446..c02c599ef0 100644
>> --- a/hw/virtio/vhost.c
>> +++ b/hw/virtio/vhost.c
>> @@ -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
>>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation
  2023-09-13  7:46 [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation Eric Auger
  2023-09-14  3:46 ` Jason Wang
@ 2023-10-03 12:43 ` Michael S. Tsirkin
  2023-10-03 13:16   ` Eric Auger
  1 sibling, 1 reply; 5+ messages in thread
From: Michael S. Tsirkin @ 2023-10-03 12:43 UTC (permalink / raw)
  To: Eric Auger; +Cc: eric.auger.pro, qemu-devel, jasowang, lvivier

On Wed, Sep 13, 2023 at 09:46:57AM +0200, Eric Auger wrote:
> 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.

> 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.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> Fixes: c44317efecb2 ("vhost: Build temporary section list and deref
> after commit")
> CC: QEMU Stable <qemu-stable@nongnu.org>
> 
> ---
> 
> This SIGSEV condition can be reproduced with
> https://lore.kernel.org/all/20230904080451.424731-1-eric.auger@redhat.com/#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.

> ---
>  hw/virtio/vhost.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> index e2f6ffb446..c02c599ef0 100644
> --- a/hw/virtio/vhost.c
> +++ b/hw/virtio/vhost.c
> @@ -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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation
  2023-10-03 12:43 ` Michael S. Tsirkin
@ 2023-10-03 13:16   ` Eric Auger
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Auger @ 2023-10-03 13:16 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: eric.auger.pro, qemu-devel, jasowang, lvivier

Hi Michael,

On 10/3/23 14:43, Michael S. Tsirkin wrote:
> On Wed, Sep 13, 2023 at 09:46:57AM +0200, Eric Auger wrote:
>> 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.
>
>> 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.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>> Fixes: c44317efecb2 ("vhost: Build temporary section list and deref
>> after commit")
>> CC: QEMU Stable <qemu-stable@nongnu.org>
>>
>> ---
>>
>> This SIGSEV condition can be reproduced with
>> https://lore.kernel.org/all/20230904080451.424731-1-eric.auger@redhat.com/#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
>
>> ---
>>  hw/virtio/vhost.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
>> index e2f6ffb446..c02c599ef0 100644
>> --- a/hw/virtio/vhost.c
>> +++ b/hw/virtio/vhost.c
>> @@ -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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-10-03 13:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-13  7:46 [PATCH] vhost: Add a defensive check in vhost_commit against wrong deallocation Eric Auger
2023-09-14  3:46 ` Jason Wang
2023-09-14  7:45   ` Eric Auger
2023-10-03 12:43 ` Michael S. Tsirkin
2023-10-03 13:16   ` Eric Auger

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).