Skip to contentSkip to conversation

hw/tpm/tpm_tis_common.c: Assert that locty is in range

PATCH In review Peter Maydell 3 participants 4 messages May 13, 2022 – May 18, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/tpm/tpm_tis_common.c: Assert that locty is in range

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 assertions 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.

---
Tested with 'make check' only...

 hw/tpm/tpm_tis_common.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/tpm/tpm_tis_common.c +6 -0
index e700d821816..81edae410c8 100644
@@ -295,6 +295,8 @@ static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
     uint8_t locty = tpm_tis_locality_from_addr(addr);
     hwaddr base = addr & ~0xfff;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     printf("tpm_tis: active locality      : %d\n"
            "tpm_tis: state of locality %d : %d\n"
            "tpm_tis: register dump:\n",
@@ -336,6 +338,8 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
     uint32_t avail;
     uint8_t v;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     if (tpm_backend_had_startup_error(s->be_driver)) {
         return 0;
     }
@@ -458,6 +462,8 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
     uint16_t len;
     uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     trace_tpm_tis_mmio_write(size, addr, val);
 
     if (locty == 4) {
Signature · 1 line
2.25.1
Reviewed-by Philippe Mathieu-Daudé on v1 1/1
#1 lore raw
Quoted 25 lines · Peter Maydell

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 assertions 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.

---
Tested with 'make check' only...

 hw/tpm/tpm_tis_common.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/tpm/tpm_tis_common.c +0 -0
index e700d821816..81edae410c8 100644

hw/tpm/tpm_tis_common.c static void tpm_tis_dump_state(TPMState *s, hwaddr addr) · line 302

Quoted 8 lines
hw/tpm/tpm_tis_common.c +2 -0
@@ -295,6 +295,8 @@ static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
     uint8_t locty = tpm_tis_locality_from_addr(addr);
     hwaddr base = addr & ~0xfff;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     printf("tpm_tis: active locality      : %d\n"
            "tpm_tis: state of locality %d : %d\n"
            "tpm_tis: register dump:\n",

This one was here ...:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 10 lines
hw/tpm/tpm_tis_common.c +2 -0
@@ -336,6 +338,8 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
     uint32_t avail;
     uint8_t v;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     if (tpm_backend_had_startup_error(s->be_driver)) {
         return 0;
     }

hw/tpm/tpm_tis_common.c static void tpm_tis_mmio_write(void *opaque, hwaddr addr, · line 469

Quoted 8 lines
hw/tpm/tpm_tis_common.c +2 -0
@@ -458,6 +462,8 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
     uint16_t len;
     uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
     trace_tpm_tis_mmio_write(size, addr, val);
 
     if (locty == 4) {

... but not these, so:

Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

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

Quoted 5 lines

Add assertions 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.

I trust that the 3 fixes resolve the 5 CIDs.

Quoted 29 lines

---
Tested with 'make check' only...

  hw/tpm/tpm_tis_common.c | 6 ++++++
  1 file changed, 6 insertions(+)
hw/tpm/tpm_tis_common.c +4 -0
index e700d821816..81edae410c8 100644
@@ -295,6 +295,8 @@ static void tpm_tis_dump_state(TPMState *s, hwaddr addr)
      uint8_t locty = tpm_tis_locality_from_addr(addr);
      hwaddr base = addr & ~0xfff;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
      printf("tpm_tis: active locality      : %d\n"
             "tpm_tis: state of locality %d : %d\n"
             "tpm_tis: register dump:\n",
@@ -336,6 +338,8 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
      uint32_t avail;
      uint8_t v;
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
      if (tpm_backend_had_startup_error(s->be_driver)) {
          return 0;
      }

hw/tpm/tpm_tis_common.c static void tpm_tis_mmio_write(void *opaque, hwaddr addr, · line 469

Quoted 8 lines
hw/tpm/tpm_tis_common.c +2 -0
@@ -458,6 +462,8 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
      uint16_t len;
      uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
 
+    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+
      trace_tpm_tis_mmio_write(size, addr, val);
 
      if (locty == 4) {

All 3 of your fixes below are after the 3 existing calls to tpm_tis_locality_from_addr(). Would Coverity be happy if we were to move the asserts into that one function? I am fine with this patch, though.

#3 lore raw
Quoted 13 lines · Stefan Berger
Quoted 7 lines · Peter Maydell

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

Quoted 3 lines

All 3 of your fixes below are after the 3 existing calls to tpm_tis_locality_from_addr(). Would Coverity be happy if we were to move the asserts into that one function? I am fine with this patch, though.

Yes, I think Coverity would be happy either way. There's not a lot in it, but I picked this way round because in theory one might want in a hypothetical future situation to have a different kind of error checking for a callsite that did an address-to-locality
lookup: it's not inherently of itself never possible it can fail.

thanks
-- PMM