All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] hw/tpm/tpm_tis_common.c: Assert that locty is in range
@ 2022-05-25 12:59 Stefan Berger
  2022-05-25 13:04 ` Marc-André Lureau
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Berger @ 2022-05-25 12:59 UTC (permalink / raw)
  To: qemu-devel; +Cc: marcandre.lureau, f4bug, Peter Maydell, Stefan Berger

From: Peter Maydell <peter.maydell@linaro.org>

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.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>

---

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

diff --git a/hw/tpm/tpm_tis_common.c b/hw/tpm/tpm_tis_common.c
index e700d82181..503be2a541 100644
--- a/hw/tpm/tpm_tis_common.c
+++ b/hw/tpm/tpm_tis_common.c
@@ -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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] hw/tpm/tpm_tis_common.c: Assert that locty is in range
  2022-05-25 12:59 [PATCH v2] hw/tpm/tpm_tis_common.c: Assert that locty is in range Stefan Berger
@ 2022-05-25 13:04 ` Marc-André Lureau
  0 siblings, 0 replies; 2+ messages in thread
From: Marc-André Lureau @ 2022-05-25 13:04 UTC (permalink / raw)
  To: Stefan Berger; +Cc: QEMU, Philippe Mathieu-Daudé, Peter Maydell

[-- Attachment #1: Type: text/plain, Size: 1856 bytes --]

On Wed, May 25, 2022 at 3:02 PM Stefan Berger <stefanb@linux.ibm.com> wrote:

> From: Peter Maydell <peter.maydell@linaro.org>
>
> 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.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


>
> ---
>
> 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(-)
>
> diff --git a/hw/tpm/tpm_tis_common.c b/hw/tpm/tpm_tis_common.c
> index e700d82181..503be2a541 100644
> --- a/hw/tpm/tpm_tis_common.c
> +++ b/hw/tpm/tpm_tis_common.c
> @@ -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
>
>
>

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 2814 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-25 13:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-25 12:59 [PATCH v2] hw/tpm/tpm_tis_common.c: Assert that locty is in range Stefan Berger
2022-05-25 13:04 ` Marc-André Lureau

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.