Skip to contentSkip to conversation

hw/ide: check null block pointer before blk_drain

PATCH Superseded P J P 2 participants 5 messages Aug 27, 2020 – Sep 3, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 hw/ide: check null block pointer before blk_drain

From: Prasad J Pandit <[email protected]>

While cancelling an i/o operation via ide_cancel_dma_sync(), check for null block pointer before calling blk_drain(). Avoid null pointer dereference.

-> https://ruhr-uni-bochum.sciebo.de/s/NNWP2GfwzYKeKwE?path=%2Fide_nullptr1

    ==1803100==Hint: address points to the zero page.
    #0 blk_bs ../block/block-backend.c:714
    #1 blk_drain ../block/block-backend.c:1715
    #2 ide_cancel_dma_sync ../hw/ide/core.c:723
    #3 bmdma_cmd_writeb ../hw/ide/pci.c:298
    #4 bmdma_write ../hw/ide/piix.c:75
    #5 memory_region_write_accessor ../softmmu/memory.c:483
    #6 access_with_adjusted_size ../softmmu/memory.c:544
    #7 memory_region_dispatch_write ../softmmu/memory.c:1465
    #8 flatview_write_continue ../exec.c:3176
    ...
---
 hw/ide/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/core.c +1 -1
index d997a78e47..038af1cd6b 100644
@@ -718,7 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
      * whole DMA operation will be submitted to disk with a single
      * aio operation with preadv/pwritev.
      */
-    if (s->bus->dma->aiocb) {
+    if (s->blk && s->bus->dma->aiocb) {
         trace_ide_cancel_dma_sync_remaining();
         blk_drain(s->blk);
         assert(s->bus->dma->aiocb == NULL);
Signature · 1 line
2.26.2
#1 lore raw
Quoted 31 lines · P J P

From: Prasad J Pandit <[email protected]>

While cancelling an i/o operation via ide_cancel_dma_sync(), check for null block pointer before calling blk_drain(). Avoid null pointer dereference.

->
https://ruhr-uni-bochum.sciebo.de/s/NNWP2GfwzYKeKwE?path=%2Fide_nullptr1

    ==1803100==Hint: address points to the zero page.
    #0 blk_bs ../block/block-backend.c:714
    #1 blk_drain ../block/block-backend.c:1715
    #2 ide_cancel_dma_sync ../hw/ide/core.c:723
    #3 bmdma_cmd_writeb ../hw/ide/pci.c:298
    #4 bmdma_write ../hw/ide/piix.c:75
    #5 memory_region_write_accessor ../softmmu/memory.c:483
    #6 access_with_adjusted_size ../softmmu/memory.c:544
    #7 memory_region_dispatch_write ../softmmu/memory.c:1465
    #8 flatview_write_continue ../exec.c:3176
    ...
---
 hw/ide/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ide/core.c +0 -0
index d997a78e47..038af1cd6b 100644

hw/ide/core.c void ide_cancel_dma_sync(IDEState *s) · line 722

Quoted 6 lines
hw/ide/core.c +1 -1
@@ -718,7 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
      * whole DMA operation will be submitted to disk with a single
      * aio operation with preadv/pwritev.
      */
-    if (s->bus->dma->aiocb) {
+    if (s->blk && s->bus->dma->aiocb) {
 

But s->blk mustn't be null here... IMHO we should assert() here and add a check earlier.

Don't we already have a Launchpad bug for this BTW?

         trace_ide_cancel_dma_sync_remaining();
Quoted 7 lines
hw/ide/core.c +0 -1
         blk_drain(s->blk);
         assert(s->bus->dma->aiocb == NULL);
--

2.26.2

author
#2 lore raw
+-- On Mon, 31 Aug 2020, Philippe Mathieu-Daudé wrote --+
| > +++ b/hw/ide/core.c
| > @@ -718,7 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
| > -    if (s->bus->dma->aiocb) {
| > +    if (s->blk && s->bus->dma->aiocb) {
| 
| But s->blk mustn't be null here... IMHO we should assert() here and add a
| check earlier.

===

hw/ide/core.c +1 -0
index f76f7e5234..8105187f49 100644
@@ -718,6 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
      * whole DMA operation will be submitted to disk with a single
      * aio operation with preadv/pwritev.
      */
+    assert(s->blk);
     if (s->bus->dma->aiocb) {
         trace_ide_cancel_dma_sync_remaining();
         blk_drain(s->blk);
hw/ide/pci.c +5 -2
index b50091b615..51bb9c9abc 100644
@@ -295,8 +295,11 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)
     /* Ignore writes to SSBM if it keeps the old value */
     if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
         if (!(val & BM_CMD_START)) {
-            ide_cancel_dma_sync(idebus_active_if(bm->bus));
-            bm->status &= ~BM_STATUS_DMAING;
+            IDEState *s = idebus_active_if(bm->bus);
+            if (s->blk) {
+                ide_cancel_dma_sync(s);
+                bm->status &= ~BM_STATUS_DMAING;
+            }
         } else {
             bm->cur_addr = bm->addr;
             if (!(bm->status & BM_STATUS_DMAING)) {

===

Does it look okay?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#3 lore raw
Quoted 27 lines · P J P
+-- On Mon, 31 Aug 2020, Philippe Mathieu-Daudé wrote --+
| > +++ b/hw/ide/core.c
| > @@ -718,7 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
| > -    if (s->bus->dma->aiocb) {
| > +    if (s->blk && s->bus->dma->aiocb) {
| 
| But s->blk mustn't be null here... IMHO we should assert() here and add a
| check earlier.

===

hw/ide/core.c +1 -0
index f76f7e5234..8105187f49 100644
@@ -718,6 +718,7 @@ void ide_cancel_dma_sync(IDEState *s)
      * whole DMA operation will be submitted to disk with a single
      * aio operation with preadv/pwritev.
      */
+    assert(s->blk);
     if (s->bus->dma->aiocb) {
         trace_ide_cancel_dma_sync_remaining();
         blk_drain(s->blk);
hw/ide/pci.c +0 -0
index b50091b615..51bb9c9abc 100644

hw/ide/pci.c void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) · line 301

Quoted 9 lines
hw/ide/pci.c +4 -2
@@ -295,8 +295,11 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)
     /* Ignore writes to SSBM if it keeps the old value */
     if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
         if (!(val & BM_CMD_START)) {
-            ide_cancel_dma_sync(idebus_active_if(bm->bus));
-            bm->status &= ~BM_STATUS_DMAING;
+            IDEState *s = idebus_active_if(bm->bus);
+            if (s->blk) {
+                ide_cancel_dma_sync(s);
+                bm->status &= ~BM_STATUS_DMAING;

If you don't clear this bit the guest might keep retrying (looping).

Quoted 14 lines
hw/ide/pci.c +1 -0
+            }
         } else {
             bm->cur_addr = bm->addr;
             if (!(bm->status & BM_STATUS_DMAING)) {

===

Does it look okay?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#4 lore raw
+-- On Thu, 3 Sep 2020, Philippe Mathieu-Daudé wrote --+
| > +            if (s->blk) {
| > +                ide_cancel_dma_sync(s);
| > +                bm->status &= ~BM_STATUS_DMAING;
| 
| If you don't clear this bit the guest might keep retrying (looping).

Oh, okay will keep it out of the if(s->blk) block.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D