Skip to contentSkip to conversation

tpm_tis: fix loop that cancels any seizure by a lower locality

PATCH Superseded Liam Merwick 3 participants 14 messages Feb 11, 2019 – Feb 12, 2019

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 tpm_tis: fix loop that cancels any seizure by a lower locality

In tpm_tis_mmio_write() if the requesting locality is seizing access, any seizure by a lower locality is cancelled. However the loop doing the seizure had an off-by-one error and the locality immediately preceding the requesting locality was not being cleared. This is fixed by adjusting the test in the for loop to check the localities up to the requesting locality.

---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index fd6bb9b59a96..61a130beef35 100644
@@ -624,7 +624,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
                 }
 
                 /* cancel any seize by a lower locality */
-                for (l = 0; l < locty - 1; l++) {
+                for (l = 0; l < locty; l++) {
                     s->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE;
                 }
 
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 2/3 tpm_tis: assert valid addr passed to tpm_tis_locality_from_addr()

Defensive check to prevent future caller passing incorrect address or catch if the MMIO address parameters were not all changed together.

---

I've been running static analysis tools on QEMU and one reports this check. While it's just theoretically correct (impossible to hit with current code), fixing this helps minimise noise and find other issues using those static analyzers as well as defending against the addition of future bugs.

 hw/tpm/tpm_tis.c | 1 +
 1 file changed, 1 insertion(+)
hw/tpm/tpm_tis.c +1 -0
index 61a130beef35..860c2ace7d99 100644
@@ -100,6 +100,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
 
 static uint8_t tpm_tis_locality_from_addr(hwaddr addr)
 {
+    assert(addr < TPM_TIS_ADDR_SIZE);
     return (uint8_t)((addr >> TPM_TIS_LOCALITY_SHIFT) & 0x7);
 }
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] tpm_tis: assert valid addr passed to tpm_tis_locality_from_addr()

Quoted 19 lines · Liam Merwick

Defensive check to prevent future caller passing incorrect address or catch if the MMIO address parameters were not all changed together.

---

I've been running static analysis tools on QEMU and one reports this check. While it's just theoretically correct (impossible to hit with current code), fixing this helps minimise noise and find other issues using those static analyzers as well as defending against the addition of future bugs.

  hw/tpm/tpm_tis.c | 1 +
  1 file changed, 1 insertion(+)
hw/tpm/tpm_tis.c +0 -0
index 61a130beef35..860c2ace7d99 100644

hw/tpm/tpm_tis.c static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr, · line 106

Quoted 7 lines
hw/tpm/tpm_tis.c +1 -0
@@ -100,6 +100,7 @@ static uint64_t tpm_tis_mmio_read(void *opaque, hwaddr addr,
 
  static uint8_t tpm_tis_locality_from_addr(hwaddr addr)
  {
+    assert(addr < TPM_TIS_ADDR_SIZE);
      return (uint8_t)((addr >> TPM_TIS_LOCALITY_SHIFT) & 0x7);
  }
 

If we want to add a check then we should check the value of the locality that's being derived from the address here since that one serves as an index into the array. However, we shouldn't need it with the way we register the amount of MMIO memory derived from the number of localities and the way we then derive the locality from the address.

hw/tpm/tpm_tis.c +0 -0
index 61a130beef..797f107246 100644
@@ -100,7 +100,10 @@ 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 = (uint8_t)(addr >> TPM_TIS_LOCALITY_SHIFT); +    fprintf(stderr, "addr=%08lx locty=%u\n", addr, locty); +    assert(TPM_TIS_IS_VALID_LOCTY(locty));
+    return locty;
 }

 static void tpm_tis_show_buffer(const unsigned char *buffer,

#3 lore raw

PATCH v2 3/3 tpm_tis: fix format string specifier in tpm_tis_show_buffer()

In reply to Liam Merwick

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by using %u instead of %d

---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index 860c2ace7d99..395500e8a61d 100644
@@ -110,7 +110,7 @@ static void tpm_tis_show_buffer(const unsigned char *buffer,
     uint32_t len, i;
 
     len = MIN(tpm_cmd_get_size(buffer), buffer_size);
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);
     for (i = 0; i < len; i++) {
         if (i && !(i % 16)) {
             printf("\n");
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Hi Liam,

Quoted 16 lines · Liam Merwick

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by using %u instead of %d

---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +0 -0
index 860c2ace7d99..395500e8a61d 100644

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned char *buffer, · line 110

Quoted 1 line
hw/tpm/tpm_tis.c +0 -0
@@ -110,7 +110,7 @@ static void tpm_tis_show_buffer(const unsigned char *buffer,
     uint32_t len, i;

If you want to clean this, use a size_t here instead of u32.

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned char *buffer, · line 113

Quoted 4 lines
hw/tpm/tpm_tis.c +1 -1
     len = MIN(tpm_cmd_get_size(buffer), buffer_size);
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

Quoted 4 lines
hw/tpm/tpm_tis.c +0 -0
     for (i = 0; i < len; i++) {
         if (i && !(i % 16)) {
             printf("\n");
 
#5 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 8 lines · Philippe Mathieu-Daudé

Hi Liam,

Quoted 5 lines · Liam Merwick

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by using %u instead of %d

Liam, Neither gcc nor cppcheck 1.86 complains about this on my system. What version of cppcheck are you using?

Quoted 15 lines

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned char *buffer, · line 110

Quoted 12 lines
---
  hw/tpm/tpm_tis.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +0 -0
index 860c2ace7d99..395500e8a61d 100644
@@ -110,7 +110,7 @@ static void tpm_tis_show_buffer(const unsigned char *buffer,
      uint32_t len, i;

If you want to clean this, use a size_t here instead of u32.

Quoted 1 line
Quoted 6 lines

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned char *buffer, · line 113

Quoted 3 lines
      len = MIN(tpm_cmd_get_size(buffer), buffer_size);
hw/tpm/tpm_tis.c +1 -1
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

Quoted 5 lines
Quoted 5 lines
      for (i = 0; i < len; i++) {
          if (i && !(i % 16)) {
              printf("\n");
#6 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 4 lines · Stefan Berger
Quoted 3 lines · Philippe Mathieu-Daudé

Hi Liam,

On 2/11/19 4:03 PM, Liam Merwick wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by using %u instead of %d

Liam, Neither gcc nor cppcheck 1.86 complains about this on my system. What version of cppcheck are you using?

I was using 1.86 too but I had warnings enabled.

% cppcheck -j 8 --platform=unix64 --force --inline-suppr --enable=warning --error-exitcode=1 hw/tpm/tpm_tis.c Checking hw/tpm/tpm_tis.c ...
[hw/tpm/tpm_tis.c:112]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'.

Quoted 20 lines
Quoted 20 lines
Quoted 13 lines

---
  hw/tpm/tpm_tis.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +0 -0
index 860c2ace7d99..395500e8a61d 100644

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned · line 111

Quoted 2 lines
hw/tpm/tpm_tis.c +0 -0
@@ -110,7 +110,7 @@ static void tpm_tis_show_buffer(const unsigned 
 char *buffer,
       uint32_t len, i;

If you want to clean this, use a size_t here instead of u32.

Quoted 3 lines
hw/tpm/tpm_tis.c +1 -1
       len = MIN(tpm_cmd_get_size(buffer), buffer_size);
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);
Quoted 6 lines
Quoted 3 lines

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

I'd come up the attached change (but that seems like overkill).

Regards,
Liam

Quoted 10 lines
Quoted 7 lines

hw/tpm/tpm_tis.c static void tpm_tis_show_buffer(const unsigned · line 116

Quoted 7 lines
hw/tpm/tpm_tis.c +0 -0
       for (i = 0; i < len; i++) {
           if (i && !(i % 16)) {
               printf("\n");

From 5dc2333c009583f5ce7338d5d67f857a4e8f4c74 Mon Sep 17 00:00:00 2001

From: Liam Merwick <[email protected]>
Date: Mon, 11 Feb 2019 13:15:17 +0000
Subject: [PATCH v2 3/3] tpm_tis: fix format string specifier in
tpm_tis_show_buffer()

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by converting it, and the other calls to printf in tpm_tis_show_buffer(), to use trace points.

---
 hw/tpm/tpm_tis.c    | 11 ++++++-----
 hw/tpm/trace-events |  3 +++
 2 files changed, 9 insertions(+), 5 deletions(-)
hw/tpm/tpm_tis.c +6 -5
index 860c2ace7d99..5d85b1cf61b1 100644
@@ -107,17 +107,18 @@ static uint8_t tpm_tis_locality_from_addr(hwaddr addr)
 static void tpm_tis_show_buffer(const unsigned char *buffer,
                                 size_t buffer_size, const char *string)
 {
-    uint32_t len, i;
+    size_t len;
+    uint32_t i;
 
     len = MIN(tpm_cmd_get_size(buffer), buffer_size);
-    printf("tpm_tis: %s length = %d\n", string, len);
+    trace_tpm_tis_show_buffer_hdr(string, len);
     for (i = 0; i < len; i++) {
         if (i && !(i % 16)) {
-            printf("\n");
+            trace_tpm_tis_show_buffer_newline();
         }
-        printf("%.2X ", buffer[i]);
+        trace_tpm_tis_show_buffer_entry(buffer[i]);
     }
-    printf("\n");
+    trace_tpm_tis_show_buffer_newline();
 }
 
 /*
hw/tpm/trace-events +3 -0
index 920d32ad5514..41d99d489097 100644
@@ -36,6 +36,9 @@ tpm_emulator_pre_save(void) ""
 tpm_emulator_inst_init(void) ""
 
 # hw/tpm/tpm_tis.c
+tpm_tis_show_buffer_hdr(const char *string, size_t buffer_size) ": %s length = %zu"
+tpm_tis_show_buffer_entry(const unsigned char entry) "%.2X "
+tpm_tis_show_buffer_newline(void) "\n"
 tpm_tis_raise_irq(uint32_t irqmask) "Raising IRQ for flag 0x%08x"
 tpm_tis_new_active_locality(uint8_t locty) "Active locality is now %d"
 tpm_tis_abort(uint8_t locty) "New active locality is %d"
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 3 lines · Liam Merwick

I'd come up the attached change (but that seems like overkill).

I don't think we need tracing for this.

#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 4 lines · Stefan Berger
Quoted 3 lines · Liam Merwick
Quoted 2 lines · Stefan Berger
Quoted 1 line · Philippe Mathieu-Daudé

On 2/11/19 4:03 PM, Liam Merwick wrote:

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

I'd come up the attached change (but that seems like overkill).

I don't think we need tracing for this.

So if you think the code is mature enough, let's remove the DEBUG calls!

Else we prefer to convert DEBUG printf to trace events because (at least):
- no need to recompile to enable debugging
- when compiled with debugging, you don't mess with STDIO which can be used as a chardev backend.

Thanks,

Phil.

#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Stefan Berger
Quoted 3 lines · Liam Merwick
Quoted 2 lines · Stefan Berger
Quoted 1 line · Philippe Mathieu-Daudé

On 2/11/19 4:03 PM, Liam Merwick wrote:

[...]

Quoted 10 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

I'd come up the attached change (but that seems like overkill).

Quoted 7 lines
Quoted 1 line

I don't think we need tracing for this.

So if you think the code is mature enough, let's remove the DEBUG calls!

Else we prefer to convert DEBUG printf to trace events because (at least):
- no need to recompile to enable debugging
- when compiled with debugging, you don't mess with STDIO which can be used as a chardev backend.

Fine. Then I withdraw my reviewed-by.

Quoted 4 lines

Thanks,

Phil.

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 17 lines · Stefan Berger
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Stefan Berger
Quoted 3 lines · Liam Merwick
Quoted 2 lines · Stefan Berger
Quoted 1 line · Philippe Mathieu-Daudé

On 2/11/19 4:03 PM, Liam Merwick wrote:

[...]

Quoted 10 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

-    printf("tpm_tis: %s length = %d\n", string, len); +    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

I'd come up the attached change (but that seems like overkill).

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

I don't think we need tracing for this.

So if you think the code is mature enough, let's remove the DEBUG calls!

Else we prefer to convert DEBUG printf to trace events because (at least):
- no need to recompile to enable debugging
- when compiled with debugging, you don't mess with STDIO which can be used as a chardev backend.

Fine. Then I withdraw my reviewed-by.

I don't see a way of removing the DEBUG calls without adding the overhead of a call to tpm_tis_show_buffer() each time even if tracing is not enabled (the 3 trace calls are interdependent and need a for loop). Is there any example of a trace point that calls a function that then does non-trivial printing?

I could send a v3 with the patch I attached previously with the 3 printf calls changed to trace points but still wrapped in 'if (DEBUG_TIS)' and optimised out in non-debug.

Regards,
Liam

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

Quoted 35 lines · Liam Merwick
Quoted 18 lines · Stefan Berger
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Stefan Berger
Quoted 3 lines · Liam Merwick
Quoted 2 lines · Stefan Berger
Quoted 1 line · Philippe Mathieu-Daudé

On 2/11/19 4:03 PM, Liam Merwick wrote:

[...]

Quoted 11 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

-    printf("tpm_tis: %s length = %d\n", string, len); +    printf("tpm_tis: %s length = %u\n", string, len);

So here the format is '%zu'.
However in code cleanup we try go get ride of printf() calls and replace them with trace points.

This code is only used for debugging if DEBUG_TIS has been #defined. No need to add tracing here.

I'd come up the attached change (but that seems like overkill).

I don't think we need tracing for this.

Quoted 8 lines
Quoted 7 lines

So if you think the code is mature enough, let's remove the DEBUG calls!

Else we prefer to convert DEBUG printf to trace events because (at least):
- no need to recompile to enable debugging
- when compiled with debugging, you don't mess with STDIO which can be used as a chardev backend.

Fine. Then I withdraw my reviewed-by.

I don't see a way of removing the DEBUG calls without adding the overhead of a call to tpm_tis_show_buffer() each time even if tracing is not enabled (the 3 trace calls are interdependent and need a for loop). Is there any example of a trace point that calls a function that then does non-trivial printing?

Quoted 3 lines

I could send a v3 with the patch I attached previously with the 3 printf calls changed to trace points but still wrapped in 'if (DEBUG_TIS)' and optimised out in non-debug.

You can look at commit 56853498648.

Thanks!

Phil.

Reviewed-by on v2 3/3
#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] tpm_tis: fix format string specifier in tpm_tis_show_buffer()

In reply to Liam Merwick
Quoted 7 lines · Liam Merwick

cppcheck reports:

[hw/tpm/tpm_tis.c:113]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'unsigned int'

Fix this by using %u instead of %d

Quoted 17 lines
---
  hw/tpm/tpm_tis.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index 860c2ace7d99..395500e8a61d 100644
@@ -110,7 +110,7 @@ static void tpm_tis_show_buffer(const unsigned char *buffer,
      uint32_t len, i;
 
      len = MIN(tpm_cmd_get_size(buffer), buffer_size);
-    printf("tpm_tis: %s length = %d\n", string, len);
+    printf("tpm_tis: %s length = %u\n", string, len);
      for (i = 0; i < len; i++) {
          if (i && !(i % 16)) {
              printf("\n");
Reviewed-by on v2 1/3
#13 lore raw
In reply to Liam Merwick
Quoted 17 lines · Liam Merwick

In tpm_tis_mmio_write() if the requesting locality is seizing access, any seizure by a lower locality is cancelled. However the loop doing the seizure had an off-by-one error and the locality immediately preceding the requesting locality was not being cleared. This is fixed by adjusting the test in the for loop to check the localities up to the requesting locality.

---
  hw/tpm/tpm_tis.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +0 -0
index fd6bb9b59a96..61a130beef35 100644

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

Quoted 8 lines
hw/tpm/tpm_tis.c +1 -1
@@ -624,7 +624,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
                  }
 
                  /* cancel any seize by a lower locality */
-                for (l = 0; l < locty - 1; l++) {
+                for (l = 0; l < locty; l++) {
                      s->loc[l].access &= ~TPM_TIS_ACCESS_SEIZE;
                  }