Skip to contentSkip to conversation

Fix integer handling issues in k230_gsdma and k230_decomp_gzip

PATCH Superseded Tao Ding 2 participants 14 messages Oct 2 – Oct 18

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Ding

author
#0 lore raw

v2 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]/T

Changes in v1->v2:
- Split the original k230_decomp_gzip patch into three patches, one for each integer handling issue.
- Remove the modifications "ch" in patchv1 3/3, which is now patchv2 5/5.

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                 | 4 ++++
 hw/misc/k230_decomp_gzip.c          | 6 +++---
 tests/qtest/k230-decomp-gzip-test.c | 2 +-
 3 files changed, 8 insertions(+), 4 deletions(-)

base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 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
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] hw/misc: fix calculating output address overflow in k230_decomp_gzip

Quoted 6 lines · Tao Ding

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

Resolves: Coverity CID 1685515

Alistair

Quoted 18 lines

---
 hw/misc/k230_decomp_gzip.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +0 -0
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;
 }

author
#3 lore raw

PATCH v2 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
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] hw/misc: fix calculating input address overflow in k230_decomp_gzip

Quoted 6 lines · Tao Ding

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

Resolves: Coverity CID 1685517

Alistair

Quoted 18 lines

---
 hw/misc/k230_decomp_gzip.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +0 -0
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;
 }

author
#5 lore raw

PATCH v2 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
Reviewed-by on v2 3/5
#6 lore raw

Re: [PATCH v2 3/5] hw/misc: use hwaddr for the k230_decomp_gzip input address

Quoted 4 lines · Tao Ding

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

Alistair

Quoted 19 lines

---
 hw/misc/k230_decomp_gzip.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/k230_decomp_gzip.c +0 -0
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;

author
#7 lore raw

PATCH v2 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
Reviewed-by on v2 4/5
#8 lore raw

Re: [PATCH v2 4/5] qtest: fix k230_decomp_gzip test integer handling issues

Quoted 8 lines · Tao Ding

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

Resolves: Coverity CID 1685516

Alistair

Quoted 19 lines

---
 tests/qtest/k230-decomp-gzip-test.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/k230- renamed from tests/qtest/k230-decomp-gzip-test.c +0 -0

decomp-gzip-test.c
index e80113fd28..249c8ae82c 100644

tests/qtest/k230-decomp-gzip-test.c +0 -0
@@ -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);
 }

 /*

author
#9 lore raw

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

In reply to Tao Ding

Validate the address before subtracting the channel base and use hwaddr for channel calculations to prevent unsigned underflow and truncation.

Resolves: Coverity CID 1685514

---
Compared to patchv1, the modification of ch has been removed. Because ch is an unsigned int, it is sufficient.

 hw/dma/k230_gsdma.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/dma/k230_gsdma.c +4 -0
index 7635cd19e8..e422b436a2 100644
@@ -329,6 +329,9 @@ static uint64_t k230_gsdma_read(void *opaque, hwaddr addr, unsigned int size)
         break;
     }
 
+    if (addr < K230_GSDMA_CH_BASE) {
+        goto out;
+    }
     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) {
@@ -355,6 +358,7 @@ static uint64_t k230_gsdma_read(void *opaque, hwaddr addr, unsigned int size)
         }
     }
 
+out:
     trace_k230_gsdma_read(addr, size, ret);
     return ret;
 }
Signature · 1 line
2.43.0
#10 lore raw

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

Quoted 24 lines · Tao Ding

Validate the address before subtracting the channel base and use hwaddr
for channel calculations to prevent unsigned underflow and truncation.

Resolves: Coverity CID 1685514

---
Compared to patchv1, the modification of ch has been removed. Because ch is an unsigned int, it is sufficient.

 hw/dma/k230_gsdma.c | 4 ++++

 1 file changed, 4 insertions(+)
hw/dma/k230_gsdma.c +0 -0
index 7635cd19e8..e422b436a2 100644
@@ -329,6 +329,9 @@ static uint64_t k230_gsdma_read(void *opaque,
 hwaddr addr, unsigned int size)
          break;
      }
Quoted 4 lines


+    if (addr < K230_GSDMA_CH_BASE) {
+        goto out;
+    }

This isn't right, you already have a if (addr < K230_GSDMA_CH_BASE) check above, just use that but return.

That way we don't log a read that doesn't actually happen, and can avoid a goto

Alistair

Quoted 12 lines

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

hw/dma/k230_gsdma.c +0 -0
@@ -355,6 +358,7 @@ static uint64_t k230_gsdma_read(void *opaque,
 hwaddr addr, unsigned int size)
          }
      }


+out:
     trace_k230_gsdma_read(addr, size, ret);
     return ret;
 }

author
#11 lore raw

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

Quoted 32 lines · Alistair Francis
Quoted 21 lines · Tao Ding

Validate the address before subtracting the channel base and use hwaddr
for channel calculations to prevent unsigned underflow and truncation.

Resolves: Coverity CID 1685514

---
Compared to patchv1, the modification of ch has been removed. Because ch is an unsigned int, it is sufficient.

  hw/dma/k230_gsdma.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/dma/k230_gsdma.c +0 -0
index 7635cd19e8..e422b436a2 100644

hw/dma/k230_gsdma.c static uint64_t k230_gsdma_read(void *opaque, · line 335

Quoted 7 lines
hw/dma/k230_gsdma.c +3 -0
@@ -329,6 +329,9 @@ static uint64_t k230_gsdma_read(void *opaque,
 hwaddr addr, unsigned int size)
          break;
      }
 
+    if (addr < K230_GSDMA_CH_BASE) {
+        goto out;
+    }

This isn't right, you already have a if (addr < K230_GSDMA_CH_BASE) check above, just use that but return.

Quoted 4 lines

That way we don't log a read that doesn't actually happen, and can avoid a goto

Alistair

Yes, we can return in the if in the default above.
But some addresses arrive here through other branches of the switch, and these addresses are smaller than K230_SSDMA_CH_SASE. If there is no goto, overflow errors will occur when calculating "ch".
The next patch will use conditional judgment to avoid using goto.

Quoted 13 lines
Quoted 13 lines
hw/dma/k230_gsdma.c +0 -0
      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) {

hw/dma/k230_gsdma.c +1 -0
@@ -355,6 +358,7 @@ static uint64_t k230_gsdma_read(void *opaque,
 hwaddr addr, unsigned int size)
          }
      }
 
+out:
      trace_k230_gsdma_read(addr, size, ret);
      return ret;

 }

#12 lore raw
In reply to Tao Ding
Quoted 6 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]/

What about 1685513?

Alistair

Quoted 28 lines

Thanks.

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

Changes in v1->v2:
- Split the original k230_decomp_gzip patch into three patches, one for
  each integer handling issue.
- Remove the modifications "ch" in patchv1 3/3, which is now patchv2 5/5.

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                 | 4 ++++
 hw/misc/k230_decomp_gzip.c          | 6 +++---
 tests/qtest/k230-decomp-gzip-test.c | 2 +-

 3 files changed, 8 insertions(+), 4 deletions(-)

base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3

author
#13 lore raw

CID 1685513 appears to be an existing issue rather than one introduced by my patch.
I’m also not familiar with this part of the code, so it may be better handled by the relevant maintainer.

Quoted 38 lines · Alistair Francis
Quoted 6 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]/

What about 1685513?

Alistair

Quoted 27 lines

Thanks.

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

Changes in v1->v2:
- Split the original k230_decomp_gzip patch into three patches, one for
  each integer handling issue.
- Remove the modifications "ch" in patchv1 3/3, which is now patchv2 5/5.

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                 | 4 ++++
  hw/misc/k230_decomp_gzip.c          | 6 +++---
  tests/qtest/k230-decomp-gzip-test.c | 2 +-
  3 files changed, 8 insertions(+), 4 deletions(-)

base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3