Fix integer handling issues in k230_gsdma and k230_decomp_gzip
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
Tao Ding (3):
hw/misc: fix k230_decomp_gzip integer handling issues
qtest: fix k230_decomp_gzip test integer handling issues hw/dma: fix k230_gsdma integer handling issues
hw/dma/k230_gsdma.c | 6 +++++- hw/misc/k230_decomp_gzip.c | 6 +++--- tests/qtest/k230-decomp-gzip-test.c | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-)
base-commit: 0a1ebd448b38afaa118597cbb633dbec24ef74d3
Signature · 1 line
2.43.0
PATCH 1/3 hw/misc: fix k230_decomp_gzip integer handling issues
Use hwaddr for address calculations to avoid 32-bit overflow and address truncation.
Resolves: Coverity CID 1685515, 1685517
--- hw/misc/k230_decomp_gzip.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
hw/misc/k230_decomp_gzip.c +3 -3
@@ -53,13 +53,13 @@ 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; }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 ; }
@@ -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 1/3] hw/misc: fix k230_decomp_gzip integer handling issues
Quoted 19 lines · Tao Ding
Use hwaddr for address calculations to avoid 32-bit overflow and address truncation.
Resolves: Coverity CID 1685515, 1685517
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>---
hw/misc/k230_decomp_gzip.c | 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)hw/misc/k230_decomp_gzip.c +0
-0@@ -53,13 +53,13 @@ static bool k230_decomp_gzip_write_mem(K230DecompGzipState *s, hwaddr addr, static hwaddr k230_decomp_gzip_output_addr(K230DecompGzipState *s) {
Quoted 3 lines
return
K230_DECOMP_GZIP_SRAM_OUT_BASE+
-s->output.slot*K230_DECOMP_GZIP_BLOCK_SIZE+ + (hwaddr)s->output.slot*K230_DECOMP_GZIP_BLOCK_SIZE+
Isn't K230_DECOMP_GZIP_BLOCK_SIZE still a 32-bit value, should it be a ULL?
Quoted 13 lines
s->output.current_offset;
}
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;
}hw/misc/k230_decomp_gzip.c +0
-0@@ -102,7 +102,7 @@ static void k230_decomp_gzip_finish(K230DecompGzipState *s, bool crc_ok) }
Quoted 5 lines
static bool k230_decomp_gzip_load_input(K230DecompGzipState *s, -uint32_taddr,uint32_tsize)
+ hwaddr addr,uint32_tsize)
This should probably be it's own patch
Alistair
Quoted 3 lines
{
s->input.current_offset= 0;
Re: [PATCH 1/3] hw/misc: fix k230_decomp_gzip integer handling issues
Quoted 12 lines · Alistair Francis
Quoted 11 lines · Tao Ding
Use hwaddr for address calculations to avoid 32-bit overflow and address truncation.
Resolves: Coverity CID 1685515, 1685517
Reported-by Alistair Francis<[email protected]>Signed-off-by Tao Ding<[email protected]>--- hw/misc/k230_decomp_gzip.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 14 lines
hw/misc/k230_decomp_gzip.c static bool · line 57
Quoted 11 lines
hw/misc/k230_decomp_gzip.c +1
-1@@ -53,13 +53,13 @@ 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 +Isn't
K230_DECOMP_GZIP_BLOCK_SIZEstill a 32-bit value, should it be a ULL?
The cast of |s->output.slot|to |hwaddr|causes the multiplication to be performed as |hwaddr|, so |K230_DECOMP_GZIP_BLOCK_SIZE|does not need a |ULL|suffix.
Quoted 14 lines
Quoted 14 lines
hw/misc/k230_decomp_gzip.c +1
-1s->output.current_offset; } 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; }@@ -102,7 +102,7 @@ static void k230_decomp_gzip_finish(K230DecompGzipState *s, bool crc_ok) }
Quoted 5 lines
hw/misc/k230_decomp_gzip.c static void k230_decomp_gzip_finish(K230DecompGzipState *s, bool crc_ok) · line 105
Quoted 4 lines
hw/misc/k230_decomp_gzip.c +1
-1static bool k230_decomp_gzip_load_input(K230DecompGzipState *s, - uint32_t addr, uint32_t size) + hwaddr addr, uint32_t size)This should probably be it's own patch
|Agreed. I'll split the k230_decomp_gzip_load_input() parameter type change into a separate patch in v2.|
Quoted 5 lines
Alistair
Quoted 3 lines
hw/misc/k230_decomp_gzip.c +0
-0{
s->input.current_offset= 0;
PATCH 2/3 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 1685514
--- 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 2/3] qtest: fix k230_decomp_gzip test integer handling issues
Quoted 20 lines · Tao Ding
Cast slot to hwaddr before multiplication to prevent 32-bit overflow when
calculating the SRAM input address.Resolves: Coverity CID 1685514
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>---
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) {
Quoted 4 lines
return k230_sram_addr(K230_DECOMP_GZIP_SRAM_IN_BASE + - slot *
K230_DECOMP_GZIP_BLOCK_SIZE); + (hwaddr) slot *K230_DECOMP_GZIP_BLOCK_SIZE);
What is the point of this function? It's only ever passed 0
Alistair
Quoted 3 lines
}
/*
Re: [PATCH 2/3] qtest: fix k230_decomp_gzip test integer handling issues
Quoted 13 lines · Alistair Francis
Quoted 12 lines · Tao Ding
Cast slot to hwaddr before multiplication to prevent 32-bit overflow when
calculating the SRAM input address.Resolves: Coverity CID 1685514
Reported-by Alistair Francis<[email protected]>Signed-off-by Tao Ding<[email protected]>--- tests/qtest/k230-decomp-gzip-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
tests/qtest/k230-decomp-gzip-test.c static inline hwaddr k230_sram_addr(hwaddr off) · line 50
Quoted 12 lines
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 +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 );What is the point of this function? It's only ever passed 0
The hardware has two input SRAM slots (|K230_DECOMP_GZIP_INPUT_SLOTS| is 2). This helper is intended to calculate the address for either slot. The current test only uses slot 0 because its input in a single block.
Quoted 5 lines
Alistair
Quoted 3 lines
tests/qtest/k230-decomp-gzip-test.c +0
-0}/*
PATCH 3/3 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 1685516
--- hw/dma/k230_gsdma.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
hw/dma/k230_gsdma.c +5 -1
@@ -300,7 +300,7 @@ static const VMStateDescription vmstate_k230_gsdma = {static uint64_t k230_gsdma_read (void *opaque,hwaddr addr,unsigned int size) {K230GSDMAState *s =K230_GSDMA (opaque); -unsigned int ch; +hwaddr ch;hwaddr ch_off;uint64_t ret =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 3/3] hw/dma: fix k230_gsdma integer handling issues
Quoted 4 lines · Tao Ding
Validate the address before subtracting the channel base and use hwaddr
for channel calculations to prevent unsigned underflow and truncation.
Thanks for sending fixes. You are missing a cover letter for a multi- patch series
Quoted 2 lines
Resolves: Coverity CID 1685516
Should be 1685514
Quoted 26 lines
Reported-by Alistair Francis <[email protected]>Signed-off-by Tao Ding <[email protected]>---
hw/dma/k230_gsdma.c | 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)hw/dma/k230_gsdma.c +0
-0@@ -300,7 +300,7 @@ static const VMStateDescription vmstate_k230_gsdma = {static
uint64_tk230_gsdma_read(void *opaque, hwaddr addr, unsigned int size)
{
K230GSDMAState *s =K230_GSDMA(opaque);
- unsigned int ch;
+ hwaddr ch;
hwaddrch_off;
uint64_tret = 0;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;
+ }
Why add the complexity of a goto? You already have a addr < K230_GSDMA_CH_BASE check (which will never trigger after this change) where you log an error, just return there.
Quoted 10 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);
Why do you want to log a failed read as successful?
Alistair
Quoted 2 lines
return ret;
}
Re: [PATCH 3/3] hw/dma: fix k230_gsdma integer handling issues
Quoted 10 lines · Alistair Francis
Quoted 4 lines · Tao Ding
Validate the address before subtracting the channel base and use hwaddr
for channel calculations to prevent unsigned underflow and truncation.Thanks for sending fixes. You are missing a cover letter for a multi- patch series
Quoted 1 line
Resolves: Coverity CID 1685516
Should be 1685514
Yes
Quoted 26 lines
Quoted 26 lines
Reported-by Alistair Francis<[email protected]>Signed-off-by Tao Ding<[email protected]>--- hw/dma/k230_gsdma.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)hw/dma/k230_gsdma.c +1
-1@@ -300,7 +300,7 @@ static const VMStateDescription vmstate_k230_gsdma = { static uint64_t k230_gsdma_read(void *opaque, hwaddr addr, unsignedint size) { K230GSDMAState *s = K230_GSDMA(opaque); - unsigned int ch; + hwaddr ch; hwaddr ch_off;
uint64_tret = 0;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 6 lines
hw/dma/k230_gsdma.c static uint64_t k230_gsdma_read(void *opaque, hwaddr addr, unsigned int size) · line 334
Quoted 3 lines
hw/dma/k230_gsdma.c +3
-0+ if (addr < K230_GSDMA_CH_BASE) { + goto out; + }Why add the complexity of a goto? You already have a
addr < K230_GSDMA_CH_BASEcheck (which will never trigger after this change) where you log an error, just return there.
The use of goto here is intended to skip the following code, which deals with addresses greater than K230_GSDMA_CH_SASE. Another way, the trace function can also be called here and returned.
Quoted 11 lines
hw/dma/k230_gsdma.c static uint64_t k230_gsdma_read(void *opaque, · line 363
Quoted 10 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);Why do you want to log a failed read as successful?
I also a bit confused here, whether trace can only record successful reads and writes. I think it's better to use trace to record all accesses,
otherwise we would need -d guest_errors to record failed accesses.
Quoted 4 lines
Alistair
Quoted 2 lines
hw/dma/k230_gsdma.c +0
-0return ret;}