qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] ide: ahci: add check before calling dma_memory_unmap
@ 2016-01-28 19:48 P J P
  2016-01-28 19:57 ` John Snow
  0 siblings, 1 reply; 3+ messages in thread
From: P J P @ 2016-01-28 19:48 UTC (permalink / raw)
  To: QEMU Developers; +Cc: Peter Maydell, John Snow, Prasad J Pandit, Zuozhi fzz

From: Prasad J Pandit <pjp@fedoraproject.org>

When IDE AHCI emulation uses Frame Information Structures(FIS)
engine for data transfer, the mapped FIS buffer address is stored
in a static 'bounce.buffer'. When a request is made to map another
memory region, address_space_map() returns NULL because
'bounce.buffer' is in_use. It leads to a null pointer dereference
error while doing 'dma_memory_unmap'. Add a check to avoid it.

Reported-by: Zuozhi fzz <zuozhi.fzz@alibaba-inc.com>
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
---
 hw/ide/ahci.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Update as per review
  -> https://lists.gnu.org/archive/html/qemu-devel/2016-01/msg05715.html

diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 17f1cbd..f413a59 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -661,9 +661,11 @@ static bool ahci_map_fis_address(AHCIDevice *ad)
 
 static void ahci_unmap_fis_address(AHCIDevice *ad)
 {
-    dma_memory_unmap(ad->hba->as, ad->res_fis, 256,
-                     DMA_DIRECTION_FROM_DEVICE, 256);
-    ad->res_fis = NULL;
+    if (ad->res_fis) {
+        dma_memory_unmap(ad->hba->as, ad->res_fis, 256,
+                         DMA_DIRECTION_FROM_DEVICE, 256);
+        ad->res_fis = NULL;
+    }
 }
 
 static bool ahci_map_clb_address(AHCIDevice *ad)
@@ -677,9 +679,11 @@ static bool ahci_map_clb_address(AHCIDevice *ad)
 
 static void ahci_unmap_clb_address(AHCIDevice *ad)
 {
-    dma_memory_unmap(ad->hba->as, ad->lst, 1024,
-                     DMA_DIRECTION_FROM_DEVICE, 1024);
-    ad->lst = NULL;
+    if (ad->lst) {
+        dma_memory_unmap(ad->hba->as, ad->lst, 1024,
+                         DMA_DIRECTION_FROM_DEVICE, 1024);
+        ad->lst = NULL;
+    }
 }
 
 static void ahci_write_fis_sdb(AHCIState *s, NCQTransferState *ncq_tfs)
-- 
2.5.0

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

* Re: [Qemu-devel] [PATCH v2] ide: ahci: add check before calling dma_memory_unmap
  2016-01-28 19:48 [Qemu-devel] [PATCH v2] ide: ahci: add check before calling dma_memory_unmap P J P
@ 2016-01-28 19:57 ` John Snow
  2016-01-28 20:41   ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: John Snow @ 2016-01-28 19:57 UTC (permalink / raw)
  To: P J P, QEMU Developers; +Cc: Peter Maydell, Prasad J Pandit, Zuozhi fzz



On 01/28/2016 02:48 PM, P J P wrote:
> From: Prasad J Pandit <pjp@fedoraproject.org>
> 
> When IDE AHCI emulation uses Frame Information Structures(FIS)
> engine for data transfer, the mapped FIS buffer address is stored
> in a static 'bounce.buffer'. When a request is made to map another
> memory region, address_space_map() returns NULL because
> 'bounce.buffer' is in_use. It leads to a null pointer dereference
> error while doing 'dma_memory_unmap'. Add a check to avoid it.
> 
> Reported-by: Zuozhi fzz <zuozhi.fzz@alibaba-inc.com>
> Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
> ---
>  hw/ide/ahci.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> Update as per review
>   -> https://lists.gnu.org/archive/html/qemu-devel/2016-01/msg05715.html
> 
> diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
> index 17f1cbd..f413a59 100644
> --- a/hw/ide/ahci.c
> +++ b/hw/ide/ahci.c
> @@ -661,9 +661,11 @@ static bool ahci_map_fis_address(AHCIDevice *ad)
>  
>  static void ahci_unmap_fis_address(AHCIDevice *ad)
>  {
> -    dma_memory_unmap(ad->hba->as, ad->res_fis, 256,
> -                     DMA_DIRECTION_FROM_DEVICE, 256);
> -    ad->res_fis = NULL;
> +    if (ad->res_fis) {
> +        dma_memory_unmap(ad->hba->as, ad->res_fis, 256,
> +                         DMA_DIRECTION_FROM_DEVICE, 256);
> +        ad->res_fis = NULL;
> +    }
>  }
>  
>  static bool ahci_map_clb_address(AHCIDevice *ad)
> @@ -677,9 +679,11 @@ static bool ahci_map_clb_address(AHCIDevice *ad)
>  
>  static void ahci_unmap_clb_address(AHCIDevice *ad)
>  {
> -    dma_memory_unmap(ad->hba->as, ad->lst, 1024,
> -                     DMA_DIRECTION_FROM_DEVICE, 1024);
> -    ad->lst = NULL;
> +    if (ad->lst) {
> +        dma_memory_unmap(ad->hba->as, ad->lst, 1024,
> +                         DMA_DIRECTION_FROM_DEVICE, 1024);
> +        ad->lst = NULL;
> +    }
>  }
>  
>  static void ahci_write_fis_sdb(AHCIState *s, NCQTransferState *ncq_tfs)
> 

This is fine for now as it protects us against doing something stupid in
a mechanical fashion, but I still wonder under which case we are
"starting" the FIS or CLB engines without getting a valid address. (The
unmap should only be happening when we /stop/ the engines, which implies
they were started -- which points to a bug somewhere else, too.)

Do you have a reproducer for the original issue?

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

* Re: [Qemu-devel] [PATCH v2] ide: ahci: add check before calling dma_memory_unmap
  2016-01-28 19:57 ` John Snow
@ 2016-01-28 20:41   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2016-01-28 20:41 UTC (permalink / raw)
  To: John Snow, P J P, QEMU Developers
  Cc: Peter Maydell, Prasad J Pandit, Zuozhi fzz



On 28/01/2016 20:57, John Snow wrote:
> This is fine for now as it protects us against doing something stupid in
> a mechanical fashion, but I still wonder under which case we are
> "starting" the FIS or CLB engines without getting a valid address. (The
> unmap should only be happening when we /stop/ the engines, which implies
> they were started -- which points to a bug somewhere else, too.)

It can happen if both of them are registered to an MMIO address.  The
second map will fail.

Perhaps it's a better fix to switch FIS and CLB to
address_space_read/write, but this works as a simple fix for the SEGV.

Paolo

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

end of thread, other threads:[~2016-01-28 20:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-28 19:48 [Qemu-devel] [PATCH v2] ide: ahci: add check before calling dma_memory_unmap P J P
2016-01-28 19:57 ` John Snow
2016-01-28 20:41   ` Paolo Bonzini

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