Fix integer handling issues in k230_gsdma and k230_decomp_gzip
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
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
@@ -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
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
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>
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@@ -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;
}
PATCH v2 2/5 hw/misc: fix calculating input address overflow in k230_decomp_gzip
In reply to Tao DingUse 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
@@ -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
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
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>
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@@ -59,7 +59,7 @@ static hwaddrk230_decomp_gzip_output_addr (K230DecompGzipState *s)
static hwaddrk230_decomp_gzip_input_addr(K230DecompGzipState *s){
- returns->input.slot*K230_DECOMP_GZIP_BLOCK_SIZE+ + return (hwaddr)s->input.slot*K230_DECOMP_GZIP_BLOCK_SIZE+K230_DECOMP_GZIP_SRAM_IN_BASE;
}
PATCH v2 3/5 hw/misc: use hwaddr for the k230_decomp_gzip input address
In reply to Tao DingUse 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
@@ -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
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.
Signed-off-by Tao Ding <[email protected]>
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@@ -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_taddr,uint32_tsize)
+ hwaddr addr,uint32_tsize) {
s->input.current_offset= 0;
PATCH v2 4/5 qtest: fix k230_decomp_gzip test integer handling issues
In reply to Tao DingCast 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
@@ -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
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
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>
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
indexe80113fd28..249c8ae82c100644tests/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);
}
/*
PATCH v2 5/5 hw/dma: fix k230_gsdma integer handling issues
In reply to Tao DingValidate 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
@@ -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
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
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>---
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@@ -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;
}
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
Reported-by Alistair Francis<[email protected]>Signed-off-by Tao Ding<[email protected]>---
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
-0hw/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
-0ch = (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;}
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]/TChanges in
v1->v2:
- Split the originalk230_decomp_gzippatch 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 ink230_decomp_gzip
hw/misc: fix calculating input address overflow ink230_decomp_gziphw/misc: use hwaddr for thek230_decomp_gzipinput address
qtest: fixk230_decomp_gziptest integer handling issues hw/dma: fixk230_gsdmainteger handling issueshw/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
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]/TChanges in
v1->v2:
- Split the originalk230_decomp_gzippatch 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 ink230_decomp_gzip
hw/misc: fix calculating input address overflow ink230_decomp_gziphw/misc: use hwaddr for thek230_decomp_gzipinput address
qtest: fixk230_decomp_gziptest integer handling issues hw/dma: fixk230_gsdmainteger handling issueshw/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