Skip to contentSkip to conversation

ppc/pnv: PNOR fixes

PATCH Applied Cédric Le Goater 4 participants 9 messages Jan 7, 2020 – Jan 8, 2020

lore
Share & tools
lore
#0 lore raw

cover ppc/pnv: PNOR fixes

Hello,

Coverity found a couple of issues in the PnvPNOR model. Here are the fixes.

Thanks,

C.

Cédric Le Goater (2):
ppc/pnv: check return value of blk_pwrite() ppc/pnv: fix check on return value of blk_getlength()

 include/hw/ppc/pnv_pnor.h |  2 +-
 hw/ppc/pnv_pnor.c         | 10 +++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH 1/2 ppc/pnv: check return value of blk_pwrite()

When updating the PNOR file contents, we should check for a possible failure of blk_pwrite().

Fixes Coverity issue CID 1412228.

---
 hw/ppc/pnv_pnor.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/pnv_pnor.c +6 -2
index bfb1e92b0392..0e86ae2feae6 100644
@@ -33,6 +33,7 @@ static uint64_t pnv_pnor_read(void *opaque, hwaddr addr, unsigned size)
 static void pnv_pnor_update(PnvPnor *s, int offset, int size)
 {
     int offset_end;
+    int ret;
 
     if (s->blk) {
         return;
@@ -42,8 +43,11 @@ static void pnv_pnor_update(PnvPnor *s, int offset, int size)
     offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
     offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
 
-    blk_pwrite(s->blk, offset, s->storage + offset,
-               offset_end - offset, 0);
+    ret = blk_pwrite(s->blk, offset, s->storage + offset,
+                     offset_end - offset, 0);
+    if (ret < 0) {
+        error_report("Could not update PNOR: %s", strerror(-ret));
+    }
 }
 
 static void pnv_pnor_write(void *opaque, hwaddr addr, uint64_t data,
Signature · 1 line
2.21.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] ppc/pnv: check return value of blk_pwrite()

Quoted 7 lines · On Tue, 7 Jan 2020 18:18:08 +0100 Cédric Le Goater <[email protected]> wrote:

When updating the PNOR file contents, we should check for a possible failure of blk_pwrite().

Fixes Coverity issue CID 1412228.

---

Quoted 29 lines
 hw/ppc/pnv_pnor.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/pnv_pnor.c +6 -2
index bfb1e92b0392..0e86ae2feae6 100644
@@ -33,6 +33,7 @@ static uint64_t pnv_pnor_read(void *opaque, hwaddr addr, unsigned size)
 static void pnv_pnor_update(PnvPnor *s, int offset, int size)
 {
     int offset_end;
+    int ret;
 
     if (s->blk) {
         return;
@@ -42,8 +43,11 @@ static void pnv_pnor_update(PnvPnor *s, int offset, int size)
     offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
     offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
 
-    blk_pwrite(s->blk, offset, s->storage + offset,
-               offset_end - offset, 0);
+    ret = blk_pwrite(s->blk, offset, s->storage + offset,
+                     offset_end - offset, 0);
+    if (ret < 0) {
+        error_report("Could not update PNOR: %s", strerror(-ret));
+    }
 }
 
 static void pnv_pnor_write(void *opaque, hwaddr addr, uint64_t data,
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] ppc/pnv: check return value of blk_pwrite()

In reply to Cédric Le Goater
Quoted 23 lines · Cédric Le Goater

When updating the PNOR file contents, we should check for a possible failure of blk_pwrite().

Fixes Coverity issue CID 1412228.

---
  hw/ppc/pnv_pnor.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/pnv_pnor.c +1 -0
index bfb1e92b0392..0e86ae2feae6 100644
@@ -33,6 +33,7 @@ static uint64_t pnv_pnor_read(void *opaque, hwaddr addr, unsigned size)
  static void pnv_pnor_update(PnvPnor *s, int offset, int size)
  {
      int offset_end;
+    int ret;
 
      if (s->blk) {
          return;

hw/ppc/pnv_pnor.c static void pnv_pnor_update(PnvPnor *s, int offset, int size) · line 49

Quoted 9 lines
hw/ppc/pnv_pnor.c +4 -2
@@ -42,8 +43,11 @@ static void pnv_pnor_update(PnvPnor *s, int offset, int size)
      offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
      offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
 
-    blk_pwrite(s->blk, offset, s->storage + offset,
-               offset_end - offset, 0);
+    ret = blk_pwrite(s->blk, offset, s->storage + offset,
+                     offset_end - offset, 0);
+    if (ret < 0) {
+        error_report("Could not update PNOR: %s", strerror(-ret));

Can you report the failed offset too please?

Quoted 5 lines
hw/ppc/pnv_pnor.c +1 -0
+    }
  }
 
  static void pnv_pnor_write(void *opaque, hwaddr addr, uint64_t data,
#4 lore raw

Re: [PATCH 1/2] ppc/pnv: check return value of blk_pwrite()

Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 24 lines · Cédric Le Goater

When updating the PNOR file contents, we should check for a possible failure of blk_pwrite().

Fixes Coverity issue CID 1412228.

---
  hw/ppc/pnv_pnor.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/pnv_pnor.c +1 -0
index bfb1e92b0392..0e86ae2feae6 100644
@@ -33,6 +33,7 @@ static uint64_t pnv_pnor_read(void *opaque, hwaddr addr, unsigned size)
  static void pnv_pnor_update(PnvPnor *s, int offset, int size)
  {
      int offset_end;
+    int ret;
      if (s->blk) {
          return;
@@ -42,8 +43,11 @@ static void pnv_pnor_update(PnvPnor *s, int offset, int size)
      offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
      offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
Quoted 8 lines

hw/ppc/pnv_pnor.c static void pnv_pnor_update(PnvPnor *s, int offset, int size) · line 49

Quoted 6 lines
hw/ppc/pnv_pnor.c +4 -2
-    blk_pwrite(s->blk, offset, s->storage + offset,
-               offset_end - offset, 0);
+    ret = blk_pwrite(s->blk, offset, s->storage + offset,
+                     offset_end - offset, 0);
+    if (ret < 0) {
+        error_report("Could not update PNOR: %s", strerror(-ret));

Can you report the failed offset too please?

Since this fixes a coverity warning, I'm applying now, and extending the error message can be a followup.

Quoted 8 lines
Quoted 4 lines
hw/ppc/pnv_pnor.c +1 -0
+    }
  }
  static void pnv_pnor_write(void *opaque, hwaddr addr, uint64_t data,
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#5 lore raw

PATCH 2/2 ppc/pnv: fix check on return value of blk_getlength()

In reply to Cédric Le Goater

blk_getlength() returns an int64_t but the result is stored in a uint32_t. Errors (negative values) won't be caught by the check in pnv_pnor_realize() and blk_blockalign() will allocate a very large buffer in such cases.

Fixes Coverity issue CID 1412226.

---
 include/hw/ppc/pnv_pnor.h | 2 +-
 hw/ppc/pnv_pnor.c         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/hw/ppc/pnv_pnor.h +1 -1
index c3dd28643cae..4f96abdfb402 100644
@@ -23,7 +23,7 @@ typedef struct PnvPnor {
     BlockBackend   *blk;
 
     uint8_t        *storage;
-    uint32_t       size;
+    int64_t        size;
     MemoryRegion   mmio;
 } PnvPnor;
 
hw/ppc/pnv_pnor.c +1 -1
index 0e86ae2feae6..b061106d1c0c 100644
@@ -111,7 +111,7 @@ static void pnv_pnor_realize(DeviceState *dev, Error **errp)
 }
 
 static Property pnv_pnor_properties[] = {
-    DEFINE_PROP_UINT32("size", PnvPnor, size, 128 << 20),
+    DEFINE_PROP_INT64("size", PnvPnor, size, 128 << 20),
     DEFINE_PROP_DRIVE("drive", PnvPnor, blk),
     DEFINE_PROP_END_OF_LIST(),
 };
Signature · 1 line
2.21.1
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] ppc/pnv: fix check on return value of blk_getlength()

Quoted 9 lines · On Tue, 7 Jan 2020 18:18:09 +0100 Cédric Le Goater <[email protected]> wrote:

blk_getlength() returns an int64_t but the result is stored in a uint32_t. Errors (negative values) won't be caught by the check in pnv_pnor_realize() and blk_blockalign() will allocate a very large buffer in such cases.

Fixes Coverity issue CID 1412226.

---

Quoted 30 lines
 include/hw/ppc/pnv_pnor.h | 2 +-
 hw/ppc/pnv_pnor.c         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/hw/ppc/pnv_pnor.h +1 -1
index c3dd28643cae..4f96abdfb402 100644
@@ -23,7 +23,7 @@ typedef struct PnvPnor {
     BlockBackend   *blk;
 
     uint8_t        *storage;
-    uint32_t       size;
+    int64_t        size;
     MemoryRegion   mmio;
 } PnvPnor;
 
hw/ppc/pnv_pnor.c +1 -1
index 0e86ae2feae6..b061106d1c0c 100644
@@ -111,7 +111,7 @@ static void pnv_pnor_realize(DeviceState *dev, Error **errp)
 }
 
 static Property pnv_pnor_properties[] = {
-    DEFINE_PROP_UINT32("size", PnvPnor, size, 128 << 20),
+    DEFINE_PROP_INT64("size", PnvPnor, size, 128 << 20),
     DEFINE_PROP_DRIVE("drive", PnvPnor, blk),
     DEFINE_PROP_END_OF_LIST(),
 };
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] ppc/pnv: fix check on return value of blk_getlength()

In reply to Cédric Le Goater
Quoted 31 lines · Cédric Le Goater

blk_getlength() returns an int64_t but the result is stored in a uint32_t. Errors (negative values) won't be caught by the check in pnv_pnor_realize() and blk_blockalign() will allocate a very large buffer in such cases.

Fixes Coverity issue CID 1412226.

---
  include/hw/ppc/pnv_pnor.h | 2 +-
  hw/ppc/pnv_pnor.c         | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
include/hw/ppc/pnv_pnor.h +1 -1
index c3dd28643cae..4f96abdfb402 100644
@@ -23,7 +23,7 @@ typedef struct PnvPnor {
      BlockBackend   *blk;
 
      uint8_t        *storage;
-    uint32_t       size;
+    int64_t        size;
      MemoryRegion   mmio;
  } PnvPnor;
 
hw/ppc/pnv_pnor.c +0 -0
index 0e86ae2feae6..b061106d1c0c 100644

hw/ppc/pnv_pnor.c static void pnv_pnor_realize(DeviceState *dev, Error **errp) · line 114

Quoted 5 lines
hw/ppc/pnv_pnor.c +1 -1
@@ -111,7 +111,7 @@ static void pnv_pnor_realize(DeviceState *dev, Error **errp)
  }
 
  static Property pnv_pnor_properties[] = {
-    DEFINE_PROP_UINT32("size", PnvPnor, size, 128 << 20),
+    DEFINE_PROP_INT64("size", PnvPnor, size, 128 << 20),

If you respin the series please consider using '128 * MiB' here.

Quoted 4 lines
hw/ppc/pnv_pnor.c +0 -0
      DEFINE_PROP_DRIVE("drive", PnvPnor, blk),
      DEFINE_PROP_END_OF_LIST(),
  };
Applied
#8 lore raw

Re: [PATCH 2/2] ppc/pnv: fix check on return value of blk_getlength()

In reply to Cédric Le Goater
Quoted 8 lines · Cédric Le Goater

blk_getlength() returns an int64_t but the result is stored in a uint32_t. Errors (negative values) won't be caught by the check in pnv_pnor_realize() and blk_blockalign() will allocate a very large buffer in such cases.

Fixes Coverity issue CID 1412226.

Applied to ppc-for-5.0.

Quoted 31 lines
---
 include/hw/ppc/pnv_pnor.h | 2 +-
 hw/ppc/pnv_pnor.c         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/hw/ppc/pnv_pnor.h +1 -1
index c3dd28643cae..4f96abdfb402 100644
@@ -23,7 +23,7 @@ typedef struct PnvPnor {
     BlockBackend   *blk;
 
     uint8_t        *storage;
-    uint32_t       size;
+    int64_t        size;
     MemoryRegion   mmio;
 } PnvPnor;
 
hw/ppc/pnv_pnor.c +1 -1
index 0e86ae2feae6..b061106d1c0c 100644
@@ -111,7 +111,7 @@ static void pnv_pnor_realize(DeviceState *dev, Error **errp)
 }
 
 static Property pnv_pnor_properties[] = {
-    DEFINE_PROP_UINT32("size", PnvPnor, size, 128 << 20),
+    DEFINE_PROP_INT64("size", PnvPnor, size, 128 << 20),
     DEFINE_PROP_DRIVE("drive", PnvPnor, blk),
     DEFINE_PROP_END_OF_LIST(),
 };
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson