Skip to contentSkip to conversation

Fix integer handling issues in k230_gsdma and k230_decomp_gzip

PATCH Applied Tao Ding 2 participants 8 messages Oct 18 – Oct 19

lore
Share & tools
lore

Tao Ding

author
#0 lore raw

v3 cover Fix integer handling issues in k230_gsdma and k230_decomp_gzip

Hi, all

This series fixes integer overflow, truncation, and underflow issues reported by Coverity in the K230 GZIP decompressor, its qtest, and GSDMA.
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks.

The patchv1 series is posted in:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes in v2->v3:
- Separate global and channel register handling to prevent unsigned address underflow when calculating GSDMA channel offsets.

Tao Ding (5):
hw/misc: fix calculating output address overflow in k230_decomp_gzip hw/misc: fix calculating input address overflow in k230_decomp_gzip hw/misc: use hwaddr for the k230_decomp_gzip input address
qtest: fix k230_decomp_gzip test integer handling issues hw/dma: fix k230_gsdma integer handling issues

 hw/dma/k230_gsdma.c                 | 84 ++++++++++++++---------------
 hw/misc/k230_decomp_gzip.c          |  6 +--
 tests/qtest/k230-decomp-gzip-test.c |  2 +-
 3 files changed, 46 insertions(+), 46 deletions(-)

base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/5 hw/misc: fix calculating output address overflow in k230_decomp_gzip

Use hwaddr for address calculations to avoid 32-bit overflow.

Resolves: Coverity CID 1685515

---
 hw/misc/k230_decomp_gzip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +1 -1
index 0d8a3b2d2e..ac06cc1571 100644
@@ -53,7 +53,7 @@ static bool k230_decomp_gzip_write_mem(K230DecompGzipState *s, hwaddr addr,
 static hwaddr k230_decomp_gzip_output_addr(K230DecompGzipState *s)
 {
     return K230_DECOMP_GZIP_SRAM_OUT_BASE +
-           s->output.slot * K230_DECOMP_GZIP_BLOCK_SIZE +
+           (hwaddr) s->output.slot * K230_DECOMP_GZIP_BLOCK_SIZE +
            s->output.current_offset;
 }
 
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/5 hw/misc: fix calculating input address overflow in k230_decomp_gzip

In reply to Tao Ding

Use hwaddr for address calculations to avoid 32-bit overflow.

Resolves: Coverity CID 1685517

---
 hw/misc/k230_decomp_gzip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +1 -1
index ac06cc1571..da4dc6f954 100644
@@ -59,7 +59,7 @@ static hwaddr k230_decomp_gzip_output_addr(K230DecompGzipState *s)
 
 static hwaddr k230_decomp_gzip_input_addr(K230DecompGzipState *s)
 {
-    return s->input.slot * K230_DECOMP_GZIP_BLOCK_SIZE +
+    return (hwaddr) s->input.slot * K230_DECOMP_GZIP_BLOCK_SIZE +
            K230_DECOMP_GZIP_SRAM_IN_BASE;
 }
 
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v3 3/5 hw/misc: use hwaddr for the k230_decomp_gzip input address

In reply to Tao Ding

Use hwaddr for address calculations to avoid 32-bit overflow and address truncation.

---
 hw/misc/k230_decomp_gzip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +1 -1
index da4dc6f954..54303c0e7f 100644
@@ -102,7 +102,7 @@ static void k230_decomp_gzip_finish(K230DecompGzipState *s, bool crc_ok)
 }
 
 static bool k230_decomp_gzip_load_input(K230DecompGzipState *s,
-                                        uint32_t addr, uint32_t size)
+                                        hwaddr addr, uint32_t size)
 {
     s->input.current_offset = 0;
 
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v3 4/5 qtest: fix k230_decomp_gzip test integer handling issues

In reply to Tao Ding

Cast slot to hwaddr before multiplication to prevent 32-bit overflow when calculating the SRAM input address.

Resolves: Coverity CID 1685516

---
 tests/qtest/k230-decomp-gzip-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/k230-decomp-gzip-test.c +1 -1
index e80113fd28..249c8ae82c 100644
@@ -46,7 +46,7 @@ static inline hwaddr k230_sram_addr(hwaddr off)
 static inline hwaddr k230_sram_input_addr(unsigned int slot)
 {
     return k230_sram_addr(K230_DECOMP_GZIP_SRAM_IN_BASE +
-                          slot * K230_DECOMP_GZIP_BLOCK_SIZE);
+                          (hwaddr) slot * K230_DECOMP_GZIP_BLOCK_SIZE);
 }
 
 /*
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v3 5/5 hw/dma: fix k230_gsdma integer handling issues

In reply to Tao Ding

Separate global and channel register handling to prevent unsigned address underflow when calculating GSDMA channel offsets.

Resolves: Coverity CID 1685514

---
 hw/dma/k230_gsdma.c | 84 ++++++++++++++++++++++-----------------------
 1 file changed, 42 insertions(+), 42 deletions(-)
hw/dma/k230_gsdma.c +42 -42
index 7635cd19e8..bd6db9e64d 100644
@@ -304,54 +304,54 @@ static uint64_t k230_gsdma_read(void *opaque, hwaddr addr, unsigned int size)
     hwaddr ch_off;
     uint64_t ret = 0;
 
-    switch (addr) {
-    case K230_GSDMA_DMA_CH_EN:
-        ret = s->dma_ch_en;
-        break;
-    case K230_GSDMA_DMA_INT_MASK:
-        ret = s->dma_int_mask;
-        break;
-    case K230_GSDMA_DMA_INT_STAT:
-        ret = s->dma_int_stat;
-        break;
-    case K230_GSDMA_DMA_CFG:
-        ret = s->dma_cfg;
-        break;
-    case K230_GSDMA_DMA_WEIGHT:
-        ret = s->dma_weight;
-        break;
-    default:
-        if (addr < K230_GSDMA_CH_BASE) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                        "%s: not implement gdma at offset 0x%" HWADDR_PRIx
-                        "\n", __func__, addr);
-        }
-        break;
-    }
-
-    ch = (addr - K230_GSDMA_CH_BASE) / K230_GSDMA_CH_STRIDE;
-    ch_off = (addr - K230_GSDMA_CH_BASE) % K230_GSDMA_CH_STRIDE;
-    if (ch < K230_GSDMA_NUM_SDMA_CHANNELS) {
-        switch (ch_off) {
-        case K230_GSDMA_CH_CTL:
-            break;
-        case K230_GSDMA_CH_STATUS:
-            ret = s->channels[ch].status;
+    if (addr < K230_GSDMA_CH_BASE) {
+        switch (addr) {
+        case K230_GSDMA_DMA_CH_EN:
+            ret = s->dma_ch_en;
             break;
-        case K230_GSDMA_CH_CFG:
-            ret = s->channels[ch].cfg;
+        case K230_GSDMA_DMA_INT_MASK:
+            ret = s->dma_int_mask;
             break;
-        case K230_GSDMA_CH_USR_DATA:
-            ret = s->channels[ch].usr_data;
+        case K230_GSDMA_DMA_INT_STAT:
+            ret = s->dma_int_stat;
             break;
-        case K230_GSDMA_CH_LLT_SADDR:
-            ret = s->channels[ch].llt_saddr;
+        case K230_GSDMA_DMA_CFG:
+            ret = s->dma_cfg;
             break;
-        case K230_GSDMA_CH_CURRENT_LLT:
-            ret = s->channels[ch].current_llt;
+        case K230_GSDMA_DMA_WEIGHT:
+            ret = s->dma_weight;
             break;
         default:
-            break;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                        "%s: not implement gdma at offset 0x%" HWADDR_PRIx
+                        "\n", __func__, addr);
+            return ret;
+        }
+    } else {
+        ch = (addr - K230_GSDMA_CH_BASE) / K230_GSDMA_CH_STRIDE;
+        ch_off = (addr - K230_GSDMA_CH_BASE) % K230_GSDMA_CH_STRIDE;
+        if (ch < K230_GSDMA_NUM_SDMA_CHANNELS) {
+            switch (ch_off) {
+            case K230_GSDMA_CH_CTL:
+                break;
+            case K230_GSDMA_CH_STATUS:
+                ret = s->channels[ch].status;
+                break;
+            case K230_GSDMA_CH_CFG:
+                ret = s->channels[ch].cfg;
+                break;
+            case K230_GSDMA_CH_USR_DATA:
+                ret = s->channels[ch].usr_data;
+                break;
+            case K230_GSDMA_CH_LLT_SADDR:
+                ret = s->channels[ch].llt_saddr;
+                break;
+            case K230_GSDMA_CH_CURRENT_LLT:
+                ret = s->channels[ch].current_llt;
+                break;
+            default:
+                break;
+            }
         }
     }
 
Signature · 1 line
2.43.0
Acked-by on v3 5/5
#6 lore raw

Re: [PATCH v3 5/5] hw/dma: fix k230_gsdma integer handling issues

Quoted 8 lines · Tao Ding

Separate global and channel register handling to prevent unsigned address underflow
when calculating GSDMA channel offsets.

Resolves: Coverity CID 1685514

Alistair

Quoted 109 lines

---
 hw/dma/k230_gsdma.c | 84 ++++++++++++++++++++++--------------------- --

 1 file changed, 42 insertions(+), 42 deletions(-)
hw/dma/k230_gsdma.c +0 -0
index 7635cd19e8..bd6db9e64d 100644
@@ -304,54 +304,54 @@ static uint64_t k230_gsdma_read(void *opaque,
 hwaddr addr, unsigned int size)
      hwaddr ch_off;
      uint64_t ret = 0;


-    switch (addr) {
-    case K230_GSDMA_DMA_CH_EN:
-        ret = s->dma_ch_en;
-        break;
-    case K230_GSDMA_DMA_INT_MASK:
-        ret = s->dma_int_mask;
-        break;
-    case K230_GSDMA_DMA_INT_STAT:
-        ret = s->dma_int_stat;
-        break;
-    case K230_GSDMA_DMA_CFG:
-        ret = s->dma_cfg;
-        break;
-    case K230_GSDMA_DMA_WEIGHT:
-        ret = s->dma_weight;
-        break;
-    default:
-        if (addr < K230_GSDMA_CH_BASE) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                        "%s: not implement gdma at offset 0x%" HWADDR_PRIx
-                        "\n", __func__, addr);
-        }
-        break;
-    }
-
-    ch = (addr - K230_GSDMA_CH_BASE) / K230_GSDMA_CH_STRIDE; -    ch_off = (addr - K230_GSDMA_CH_BASE) % K230_GSDMA_CH_STRIDE; -    if (ch < K230_GSDMA_NUM_SDMA_CHANNELS) {
-        switch (ch_off) {
-        case K230_GSDMA_CH_CTL:
-            break;
-        case K230_GSDMA_CH_STATUS:
-            ret = s->channels[ch].status;
+    if (addr < K230_GSDMA_CH_BASE) {
+        switch (addr) {
+        case K230_GSDMA_DMA_CH_EN:
+            ret = s->dma_ch_en;
             break;
-        case K230_GSDMA_CH_CFG:
-            ret = s->channels[ch].cfg;
+        case K230_GSDMA_DMA_INT_MASK:
+            ret = s->dma_int_mask;
             break;
-        case K230_GSDMA_CH_USR_DATA:
-            ret = s->channels[ch].usr_data;
+        case K230_GSDMA_DMA_INT_STAT:
+            ret = s->dma_int_stat;
             break;
-        case K230_GSDMA_CH_LLT_SADDR:
-            ret = s->channels[ch].llt_saddr;
+        case K230_GSDMA_DMA_CFG:
+            ret = s->dma_cfg;
             break;
-        case K230_GSDMA_CH_CURRENT_LLT:
-            ret = s->channels[ch].current_llt;
+        case K230_GSDMA_DMA_WEIGHT:
+            ret = s->dma_weight;
             break;
         default:
-            break;
+            qemu_log_mask(LOG_GUEST_ERROR,
+                        "%s: not implement gdma at offset 0x%" HWADDR_PRIx
+                        "\n", __func__, addr);
+            return ret;
+        }
+    } else {
+        ch = (addr - K230_GSDMA_CH_BASE) / K230_GSDMA_CH_STRIDE; +        ch_off = (addr - K230_GSDMA_CH_BASE) % K230_GSDMA_CH_STRIDE; +        if (ch < K230_GSDMA_NUM_SDMA_CHANNELS) {
+            switch (ch_off) {
+            case K230_GSDMA_CH_CTL:
+                break;
+            case K230_GSDMA_CH_STATUS:
+                ret = s->channels[ch].status;
+                break;
+            case K230_GSDMA_CH_CFG:
+                ret = s->channels[ch].cfg;
+                break;
+            case K230_GSDMA_CH_USR_DATA:
+                ret = s->channels[ch].usr_data;
+                break;
+            case K230_GSDMA_CH_LLT_SADDR:
+                ret = s->channels[ch].llt_saddr;
+                break;
+            case K230_GSDMA_CH_CURRENT_LLT:
+                ret = s->channels[ch].current_llt;
+                break;
+            default:
+                break;
+            }
         }
     }

#7 lore raw
In reply to Tao Ding
Quoted 17 lines · Tao Ding

Hi, all

This series fixes integer overflow, truncation, and underflow issues reported by Coverity in the K230 GZIP decompressor, its qtest, and GSDMA.
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks.

The patchv1 series is posted in:
https://lore.kernel.org/qemu-devel/[email protected]/

Changes in v2->v3:
- Separate global and channel register handling to prevent unsigned address underflow
  when calculating GSDMA channel offsets.

Quoted 7 lines

Tao Ding (5):
  hw/misc: fix calculating output address overflow in k230_decomp_gzip
  hw/misc: fix calculating input address overflow in k230_decomp_gzip hw/misc: use hwaddr for the k230_decomp_gzip input address
  qtest: fix k230_decomp_gzip test integer handling issues hw/dma: fix k230_gsdma integer handling issues

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 9 lines

 hw/dma/k230_gsdma.c                 | 84 ++++++++++++++------------- --
 hw/misc/k230_decomp_gzip.c          |  6 +-- tests/qtest/k230-decomp-gzip-test.c |  2 +-

 3 files changed, 46 insertions(+), 46 deletions(-)

base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3