Skip to contentSkip to conversation

Fix a couple of issues in TPM related code

PATCH Superseded Stefan Berger 1 participant 11 messages Sep 24

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/10 tpm_util: Pass TPMVersion to function writing fatal error response

In reply to Stefan Berger

Have tpm_util_write_fatal_error_response write a TPM version specific error response with error code TPM_FAIL for a TPM 1.2 and TPM_RC_FAILURE for a TPM 2. Adjust the callers.

---
 backends/tpm/tpm_emulator.c    |  3 ++-
 backends/tpm/tpm_passthrough.c |  2 +-
 backends/tpm/tpm_util.c        | 17 ++++++++++++++---
 include/system/tpm_util.h      |  3 ++-
 4 files changed, 19 insertions(+), 6 deletions(-)
backends/tpm/tpm_emulator.c +2 -1
index 95d2de2f48..16ae44c8d7 100644
@@ -270,7 +270,8 @@ static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
         tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
                                   cmd->out, cmd->out_len,
                                   &cmd->selftest_done, errp) < 0) {
-        tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
+        tpm_util_write_fatal_error_response(cmd->out, cmd->out_len,
+                                            TPM_EMULATOR(tb)->tpm_version);
     }
 }
 
backends/tpm/tpm_passthrough.c +1 -1
index a9f35ab7d6..8a4f8183c7 100644
@@ -118,7 +118,7 @@ static void tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
 
 err_exit:
     if (ret < 0) {
-        tpm_util_write_fatal_error_response(out, out_len);
+        tpm_util_write_fatal_error_response(out, out_len, tpm_pt->tpm_version);
     }
 
     tpm_pt->tpm_executing = false;
backends/tpm/tpm_util.c +14 -3
index 264bff22a9..7019f7fd3e 100644
@@ -87,12 +87,23 @@ const PropertyInfo qdev_prop_tpm = {
 /*
  * Write an error message in the given output buffer.
  */
-void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
+void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len,
+                                         TPMVersion tpm_version)
 {
     if (out_len >= sizeof(struct tpm_resp_hdr)) {
-        tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
+        switch (tpm_version) {
+        case TPM_VERSION_1_2:
+            tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
+            tpm_cmd_set_error(out, TPM_FAIL);
+            break;
+        case TPM_VERSION_2_0:
+            tpm_cmd_set_tag(out, TPM2_ST_NO_SESSIONS);
+            tpm_cmd_set_error(out, TPM_RC_FAILURE);
+            break;
+        default:
+            g_assert_not_reached();
+        }
         tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
-        tpm_cmd_set_error(out, TPM_FAIL);
     }
 }
 
include/system/tpm_util.h +2 -1
index 1858693225..3ab2fe693d 100644
@@ -25,7 +25,8 @@
 #include "system/tpm.h"
 #include "qemu/bswap.h"
 
-void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len);
+void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len,
+                                         TPMVersion tpm_version);
 
 bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len);
 
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/10 tpm_emulator: Reject TPM responses whose size is too small

In reply to Stefan Berger

Reject a response from the TPM emulator whose size field indicates that the response is smaller than the size of the header.

---
 backends/tpm/tpm_emulator.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +2 -2
index 16ae44c8d7..9c95b4d878 100644
@@ -202,13 +202,13 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
      * size)
      */
     to_read = tpm_cmd_get_size(out);
-    if (to_read > out_len) {
+    if (to_read > out_len || to_read < sizeof(struct tpm_resp_hdr)) {
         if (qio_channel_shutdown(tpm_emu->data_ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
                                  &local_err) < 0) {
             error_report_err(local_err);
         }
         error_setg(errp, "tpm-emulator: Disconnected after receiving "
-                   "unacceptable large response (%u > %u)",
+                   "unacceptable response size (%u max: %u)",
                    to_read, out_len);
         return -1;
     }
Signature · 1 line
2.55.0
#9 lore raw

PATCH v2 9/10 hw/tpm: crb: Consider response_buffer->len of received response

In reply to Stefan Berger

When the CRB receives a TPM reponse from the backend, then reject responses that are shorter than the TPM_HEADER_SIZE. When determining the size of the reponse, also consider the length of the response_buffer as being possibly shorter than the negotiated backend buffer size or the size indicated in the response itself.

Fixes 2a660ad67d15 ("hw/tpm: Implement TPM CRB chunking logic")
---
 hw/tpm/tpm_crb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/tpm/tpm_crb.c +3 -1
index 92d36608d0..5c6965f447 100644
@@ -325,13 +325,15 @@ static void tpm_crb_request_completed(TPMIf *ti, int ret)
     CRBState *s = CRB(ti);
 
     ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, Start, 0);
-    if (ret != 0) {
+    if (ret != 0 || s->response_buffer->len < TPM_HEADER_SIZE) {
         ARRAY_FIELD_DP32(s->regs, CRB_CTRL_STS,
                          tpmSts, 1); /* fatal error */
         tpm_crb_clear_internal_buffers(s);
     } else {
         uint32_t actual_resp_size = tpm_cmd_get_size(s->response_buffer->data);
         uint32_t total_resp_size = MIN(actual_resp_size, s->be_buffer_size);
+
+        total_resp_size = MIN(total_resp_size, s->response_buffer->len);
         g_byte_array_set_size(s->response_buffer, total_resp_size);
         s->response_offset = 0;
     }
Signature · 1 line
2.55.0
#6 lore raw

PATCH v2 6/10 hw/tpm: crb: Do not clear internal buffers when backend is busy

In reply to Stefan Berger

While the backend (thread) is accessing the request and response buffers, do not clear those buffers upon a command cancellation. Still clear them in all other cases, though.

Fixes 29315d22b259 ("hw/tpm: Add internal buffer state for chunking")
---
 hw/tpm/tpm_crb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/tpm/tpm_crb.c +2 -1
index 54fa2042b5..e5e33a3a2f 100644
@@ -218,8 +218,9 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
         if (val == CRB_CANCEL_INVOKE) {
             if (s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE) {
                 tpm_backend_cancel_cmd(s->tpmbe);
+            } else {
+                tpm_crb_clear_internal_buffers(s);
             }
-            tpm_crb_clear_internal_buffers(s);
         }
         break;
     case A_CRB_CTRL_START:
Signature · 1 line
2.55.0
#8 lore raw

PATCH v2 8/10 hw/tpm: crb: Implement tpm_ppi_uninit and use it in tpm_crb_unrealize

In reply to Stefan Berger

Implement tpm_ppi_uninit and use it for proper teardown of the PPI in tpm_crb_unrealize.

---
 hw/tpm/tpm_crb.c |  3 +++
 hw/tpm/tpm_ppi.c | 10 ++++++++++
 hw/tpm/tpm_ppi.h | 10 ++++++++++
 3 files changed, 23 insertions(+)
hw/tpm/tpm_crb.c +3 -0
index 9a9677db6b..92d36608d0 100644
@@ -16,6 +16,7 @@
 
 #include "qemu/osdep.h"
 
+#include "qemu/memalign.h"
 #include "qemu/module.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
@@ -529,6 +530,8 @@ static void tpm_crb_unrealize(DeviceState *dev)
     if (s->migration_blocker) {
         migrate_del_blocker(&s->migration_blocker);
     }
+
+    tpm_ppi_uninit(&s->ppi, get_system_memory(), OBJECT(s));
 }
 
 static void tpm_crb_class_init(ObjectClass *klass, const void *data)
hw/tpm/tpm_ppi.c +10 -0
index f0c34291dc..35baa083f2 100644
@@ -57,3 +57,13 @@ void tpm_ppi_init(TPMPPI *tpmppi, MemoryRegion *m,
 
     memory_region_add_subregion(m, addr, &tpmppi->ram);
 }
+
+void tpm_ppi_uninit(TPMPPI *tpmppi, MemoryRegion *m, Object *obj)
+{
+    memory_region_del_subregion(m, &tpmppi->ram);
+
+    vmstate_unregister_ram(&tpmppi->ram, DEVICE(obj));
+
+    qemu_vfree(tpmppi->buf);
+    tpmppi->buf = NULL;
+}
hw/tpm/tpm_ppi.h +10 -0
index 88f316ee95..c173889033 100644
@@ -32,6 +32,16 @@ typedef struct TPMPPI {
 void tpm_ppi_init(TPMPPI *tpmppi, MemoryRegion *m,
                   hwaddr addr, Object *obj);
 
+/**
+ * tpm_ppi_uninit:
+ * @tpmppi: a TPMPPI
+ * @m: the address-space / MemoryRegion to use
+ * @obj: the owner object
+ *
+ * Function to unregister the TPM PPI region.
+ */
+void tpm_ppi_uninit(TPMPPI *tpmppi, MemoryRegion *m, Object *obj);
+
 /**
  * tpm_ppi_reset:
  * @tpmppi: a TPMPPI
Signature · 1 line
2.55.0
#7 lore raw

PATCH v2 7/10 hw/tpm: crb: Avoid potential integer underflow

In reply to Stefan Berger

Avoid a potential integer underflow in tpm_crb_fill_command_response that could occur if s->response_offset > s->response_buffer->len and the function's call sites were to change.

tpm_crb_fill_command_response currently has 3 callers that either test that 's->response_offset < s->response_buffer->len' is true or ensure that s->response_offset = 0 and s->response_buffer->len >= 0. Therefore, the integer underflow cannot currently occur. In the worst case the subtraction would lead to a '0'.

---
 hw/tpm/tpm_crb.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
hw/tpm/tpm_crb.c +10 -3
index e5e33a3a2f..9a9677db6b 100644
@@ -180,10 +180,17 @@ static void tpm_crb_fill_command_response(CRBState *s)
      * to the linux guest in chunks by writing it back to MMIO region.
      */
     void *mem = memory_region_get_ram_ptr(&s->cmdmem);
-    uint32_t remaining = s->response_buffer->len - s->response_offset;
-    uint32_t to_copy = MIN(CRB_CTRL_CMD_SIZE, remaining);
+    uint32_t remaining = 0;
+    uint32_t to_copy;
 
-    memcpy(mem, s->response_buffer->data + s->response_offset, to_copy);
+    if (s->response_offset < s->response_buffer->len) {
+        remaining = s->response_buffer->len - s->response_offset;
+    }
+    to_copy = MIN(CRB_CTRL_CMD_SIZE, remaining);
+
+    if (to_copy) {
+        memcpy(mem, s->response_buffer->data + s->response_offset, to_copy);
+    }
 
     if (to_copy < CRB_CTRL_CMD_SIZE) {
         memset((guint8 *)mem + to_copy, 0, CRB_CTRL_CMD_SIZE - to_copy);
Signature · 1 line
2.55.0
#5 lore raw

PATCH v2 5/10 hw/tpm: tis: Use uint32_t for len to avoid truncating size of command

In reply to Stefan Berger

To avoid truncating the uint32_t return value from tpm_cmd_get_size() declare len as uint32_t rather than uint16_t.

---
 hw/tpm/tpm_tis_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis_common.c +1 -1
index 4ba228116b..8fa1bee771 100644
@@ -481,7 +481,7 @@ static void tpm_tis_mmio_write(void *opaque, hwaddr addr,
     uint8_t locty = tpm_tis_locality_from_addr(addr);
     uint8_t active_locty, l;
     int c, set_new_locty = 1;
-    uint16_t len;
+    uint32_t len;
     uint32_t mask = (size == 1) ? 0xff : ((size == 2) ? 0xffff : ~0);
 
     trace_tpm_tis_mmio_write(size, addr, val);
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 4/10 hw/tpm: tis: Only read from response buffer if rw_offset < len

In reply to Stefan Berger

Only read a byte from the response buffer, if the rw_offset < len.

---
 hw/tpm/tpm_tis_common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/tpm/tpm_tis_common.c +4 -2
index a134d5c205..4ba228116b 100644
@@ -273,13 +273,15 @@ static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty)
         len = MIN(tpm_cmd_get_size(&s->buffer),
                   s->be_buffer_size);
 
-        ret = s->buffer[s->rw_offset++];
+        if (s->rw_offset < len) {
+            ret = s->buffer[s->rw_offset++];
+            trace_tpm_tis_data_read(ret, s->rw_offset - 1);
+        }
         if (s->rw_offset >= len) {
             /* got last byte */
             tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
             tpm_tis_raise_irq(s, locty, TPM_TIS_INT_STS_VALID);
         }
-        trace_tpm_tis_data_read(ret, s->rw_offset - 1);
     }
 
     return ret;
Signature · 1 line
2.55.0
#10 lore raw

PATCH v2 10/10 tests: Add tests with commands with bad length

In reply to Stefan Berger

Add two test cases with commands with bad length:

- Send a command that indicates a very large size (0x10000 bytes) to the TPM. This will only work with the CRB interface since the TIS wants to receive all the bytes indicated by the command header.

- Send a command that indicates that its size is only 9 bytes, which is too short to be valid since the command header alone is 10 bytes. This test only works with the TIS. The CRB silently rejects commands that are shorter than the command header.

---
 tests/qtest/tpm-crb-swtpm-test.c        | 10 +++++
 tests/qtest/tpm-tests.c                 | 56 +++++++++++++++++++++++++
 tests/qtest/tpm-tests.h                 |  8 ++++
 tests/qtest/tpm-tis-device-swtpm-test.c | 10 +++++
 tests/qtest/tpm-tis-swtpm-test.c        | 10 +++++
 tests/qtest/tpm-util.c                  | 55 ++++++++++++++++++++++++
 tests/qtest/tpm-util.h                  |  3 ++
 7 files changed, 152 insertions(+)
tests/qtest/tpm-crb-swtpm-test.c +10 -0
index 050c7b0c1f..0e83a5325f 100644
@@ -33,6 +33,14 @@ static void tpm_crb_swtpm_test(const void *data)
                         "tpm-crb", NULL);
 }
 
+static void tpm_crb_short_write_swtpm_test(const void *data)
+{
+    const TestState *ts = data;
+
+    tpm_test_short_write_swtpm_test(ts->src_tpm_path, tpm_util_crb_transfer,
+                                    "tpm-crb", NULL);
+}
+
 static void tpm_crb_chunk_swtpm_test(const void *data)
 {
     const TestState *ts = data;
@@ -62,6 +70,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_data_func("/tpm/crb-swtpm/test", &ts, tpm_crb_swtpm_test);
+    qtest_add_data_func("/tpm/crb-short-write-swtpm/test", &ts,
+                        tpm_crb_short_write_swtpm_test);
     qtest_add_data_func("/tpm/crb-chunk-swtpm/test", &ts,
                         tpm_crb_chunk_swtpm_test);
     qtest_add_data_func("/tpm/crb-swtpm-migration/test", &ts,
tests/qtest/tpm-tests.c +56 -0

62 lines not on this page · raw message

tests/qtest/tpm-tests.h +8 -0
index 07ba60d26e..7684abf8f4 100644
@@ -18,6 +18,14 @@
 void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,
                          const char *ifmodel, const char *machine_options);
 
+void tpm_test_short_write_swtpm_test(const char *src_tpm_path, tx_func *tx,
+                                     const char *ifmodel,
+                                     const char *machine_options);
+
+void tpm_test_too_short_cmd_swtpm_test(const char *src_tpm_path, tx_func *tx,
+                                       const char *ifmodel,
+                                       const char *machine_options);
+
 void tpm_test_swtpm_migration_test(const char *src_tpm_path,
                                    const char *dst_tpm_path,
                                    const char *uri, tx_func *tx,
tests/qtest/tpm-tis-device-swtpm-test.c +10 -0
index 517a077005..48a7389306 100644
@@ -38,6 +38,14 @@ static void tpm_tis_swtpm_test(const void *data)
                         "tpm-tis-device", MACHINE_OPTIONS);
 }
 
+static void tpm_tis_too_short_cmd_swtpm_test(const void *data)
+{
+    const TestState *ts = data;
+
+    tpm_test_too_short_cmd_swtpm_test(ts->src_tpm_path, tpm_tis_transfer,
+                                      "tpm-tis-device", MACHINE_OPTIONS);
+}
+
 static void tpm_tis_swtpm_migration_test(const void *data)
 {
     const TestState *ts = data;
@@ -62,6 +70,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_data_func("/tpm/tis-swtpm/test", &ts, tpm_tis_swtpm_test);
+    qtest_add_data_func("/tpm/tis-too-short-cmd-swtpm/test", &ts,
+                        tpm_tis_too_short_cmd_swtpm_test);
     qtest_add_data_func("/tpm/tis-swtpm-migration/test", &ts,
                         tpm_tis_swtpm_migration_test);
     ret = g_test_run();
tests/qtest/tpm-tis-swtpm-test.c +10 -0
index 105e42e21d..14b4cbce21 100644
@@ -36,6 +36,14 @@ static void tpm_tis_swtpm_test(const void *data)
                         "tpm-tis", NULL);
 }
 
+static void tpm_tis_too_short_cmd_swtpm_test(const void *data)
+{
+    const TestState *ts = data;
+
+    tpm_test_too_short_cmd_swtpm_test(ts->src_tpm_path, tpm_tis_transfer,
+                                      "tpm-tis", NULL);
+}
+
 static void tpm_tis_swtpm_migration_test(const void *data)
 {
     const TestState *ts = data;
@@ -57,6 +65,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_data_func("/tpm/tis-swtpm/test", &ts, tpm_tis_swtpm_test);
+    qtest_add_data_func("/tpm/tis-too-short-cmd-swtpm/test", &ts,
+                        tpm_tis_too_short_cmd_swtpm_test);
     qtest_add_data_func("/tpm/tis-swtpm-migration/test", &ts,
                         tpm_tis_swtpm_migration_test);
     ret = g_test_run();
tests/qtest/tpm-util.c +55 -0

61 lines not on this page · raw message

tests/qtest/tpm-util.h +3 -0
index 681544e7d8..70c2fc09c6 100644
@@ -38,6 +38,9 @@ void tpm_util_pcrextend(QTestState *s, tx_func *tx);
 void tpm_util_pcrread(QTestState *s, tx_func *tx,
                       const unsigned char *exp_resp, size_t exp_resp_size);
 
+void tpm_util_short_write(QTestState *s, tx_func *tx);
+void tpm_util_too_short_cmd(QTestState *s, tx_func *tx);
+
 bool tpm_util_swtpm_has_tpm2(void);
 
 gboolean tpm_util_swtpm_start(const char *path, GPid *pid,
Signature · 1 line
2.55.0
#0 lore raw

v2 cover Fix a couple of issues in TPM related code

This series of patches fixes various issues in TPM-related code, such as memory leaks, potential integer underflows, clearing of buffers while they are in use, and the proper handling of sizes of commands or responses.

Regards,
Stefan

v2:
- Address issues pointed out by Marc-André

Stefan Berger (10):
tpm_util: Pass TPMVersion to function writing fatal error response
tpm_emulator: Reject TPM responses whose size is too small
tpm_emulator: Reject sending commands smaller than the request header hw/tpm: tis: Only read from response buffer if rw_offset < len hw/tpm: tis: Use uint32_t for len to avoid truncating size of command hw/tpm: crb: Do not clear internal buffers when backend is busy hw/tpm: crb: Avoid potential integer underflow
hw/tpm: crb: Implement tpm_ppi_uninit and use it in tpm_crb_unrealize hw/tpm: crb: Consider response_buffer->len of received response
tests: Add tests with commands with bad length

 backends/tpm/tpm_emulator.c             | 12 ++++--
 backends/tpm/tpm_passthrough.c          |  2 +-
 backends/tpm/tpm_util.c                 | 17 ++++++--
 hw/tpm/tpm_crb.c                        | 23 +++++++---
 hw/tpm/tpm_ppi.c                        | 10 +++++
 hw/tpm/tpm_ppi.h                        | 10 +++++
 hw/tpm/tpm_tis_common.c                 |  8 ++--
 include/system/tpm_util.h               |  3 +-
 tests/qtest/tpm-crb-swtpm-test.c        | 10 +++++
 tests/qtest/tpm-tests.c                 | 56 +++++++++++++++++++++++++
 tests/qtest/tpm-tests.h                 |  8 ++++
 tests/qtest/tpm-tis-device-swtpm-test.c | 10 +++++
 tests/qtest/tpm-tis-swtpm-test.c        | 10 +++++
 tests/qtest/tpm-util.c                  | 55 ++++++++++++++++++++++++
 tests/qtest/tpm-util.h                  |  3 ++
 15 files changed, 221 insertions(+), 16 deletions(-)
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/10 tpm_emulator: Reject sending commands smaller than the request header

Reject any TPM request from being sent to the TPM emulator that is shorter than the size of the request header.

Fixes f4ede81eed29 ("tpm: Added support for TPM emulator")
---
 backends/tpm/tpm_emulator.c | 5 +++++
 1 file changed, 5 insertions(+)
backends/tpm/tpm_emulator.c +5 -0
index 9c95b4d878..0c9e32f36a 100644
@@ -181,6 +181,11 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
     uint32_t to_read;
     ssize_t ret;
 
+    if (in_len < sizeof(struct tpm_req_hdr)) {
+        error_setg(errp, "tpm-emulator: invalid request size %u", in_len);
+        return -1;
+    }
+
     if (selftest_done) {
         *selftest_done = false;
         is_selftest = tpm_util_is_selftest(in, in_len);
Signature · 1 line
2.55.0