hw/tpm/tpm_tis_common.c: Assert that locty is in range
PATCH v2 1/1 hw/tpm/tpm_tis_common.c: Assert that locty is in range
From: Peter Maydell <[email protected]>
In tpm_tis_mmio_read(), tpm_tis_mmio_write() and tpm_tis_dump_state(), we calculate a locality index with tpm_tis_locality_from_addr() and then use it as an index into the s->loc[] array. In all these cases, the array index can't overflow because the MemoryRegion is sized to be TPM_TIS_NUM_LOCALITIES << TPM_TIS_LOCALITY_SHIFT bytes. However, Coverity can't see that, and it complains (CID 1487138, 1487180, 1487188, 1487198, 1487240).
Add an assertion to tpm_tis_locality_from_addr() that the calculated locality index is valid, which will help Coverity and also catch any potential future bug where the MemoryRegion isn't sized exactly.
---
v2:
- Moved 3 asserts from 3 callsites of tpm_tis_locality_from_addr() into tpm_tis_locality_from_addr()
--- hw/tpm/tpm_tis_common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/tpm/tpm_tis_common.c +6 -1
@@ -50,7 +50,12 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,static uint8_t tpm_tis_locality_from_addr (hwaddr addr) { -return (uint8_t )((addr >>TPM_TIS_LOCALITY_SHIFT ) &0x7 ); +uint8_t locty; + + locty = (uint8_t )((addr >>TPM_TIS_LOCALITY_SHIFT ) &0x7 ); +assert (TPM_TIS_IS_VALID_LOCTY (locty)); + +return locty; }
Signature · 1 line
2.36.1
Quoted 10 lines · Stefan Berger
From: Peter Maydell <[email protected]>
In
tpm_tis_mmio_read(),tpm_tis_mmio_write()andtpm_tis_dump_state(), we calculate a locality index withtpm_tis_locality_from_addr()and then use it as an index into the s->loc[] array. In all these cases, the array index can't overflow because the MemoryRegion is sized to beTPM_TIS_NUM_LOCALITIES<<TPM_TIS_LOCALITY_SHIFTbytes. However, Coverity can't see that, and it complains (CID 1487138, 1487180, 1487188, 1487198, 1487240).
Quoted 7 lines
Add an assertion to
tpm_tis_locality_from_addr()that the calculated locality index is valid, which will help Coverity and also catch any potential future bug where the MemoryRegion isn't sized exactly.Signed-off-by Peter Maydell <[email protected]>Signed-off-by Stefan Berger <[email protected]>
Quoted 34 lines
---
v2:
- Moved 3 asserts from 3 callsites oftpm_tis_locality_from_addr()intotpm_tis_locality_from_addr()--- hw/tpm/tpm_tis_common.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/tpm/tpm_tis_common.c +6
-2@@ -50,7 +50,12 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, static uint8_t tpm_tis_locality_from_addr(hwaddr addr) { -return (uint8_t )((addr >>TPM_TIS_LOCALITY_SHIFT ) &0x7 ); +uint8_t locty; + + locty = (uint8_t )((addr >>TPM_TIS_LOCALITY_SHIFT ) &0x7 ); +assert (TPM_TIS_IS_VALID_LOCTY (locty)); + +return locty; } --2.36.1
Signature · 1 line
Marc-André Lureau