Skip to contentSkip to conversation

Improve handling of response buffer size in tpm_emulator

PATCH Superseded Stefan Berger 2 participants 6 messages Apr 30 – May 11

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Improve handling of response buffer size in tpm_emulator

This series addresses some issues with the handling of the negotiated buffer size that the TPM frontends tell the tpm_emulator (swtpm) to use. Also, it improves on limiting the number of bytes read from the packet sent from swtpm.

Stefan

v3:
- Changed 1/2 to require exact match of requested buffer size
- Fixed to_read

Stefan Berger (2):
tpm_emulator: Reject a buffer size different than what was requested
tpm_emulator: Limit number of bytes read to negotiated buffer size

 backends/tpm/tpm_emulator.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 tpm_emulator: Reject a buffer size different than what was requested

When the TIS, SPAPR, or CRB frontends negotiate a buffer size with the TPM backend, then the tpm_emulator (swtpm) could still adjust this size of the buffer to within bounds supported by swtpm+libtpms if the chosen size was outside the acceptable range. This could theoretically lead to the TPM 2 using a bigger buffer than what was requested and memory allocated for. In practice this would not happend since the requested size of 4096 bytes for TIS and SPAPR and 3968 bytes for CRB are in the (currently) supported range of ~2.5kb to 4096 bytes. With PQC support the range will have an upper bound of 8kb and a lower bound that will support the (pre-PQC) CRB with 3968 bytes.

---
 backends/tpm/tpm_emulator.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
backends/tpm/tpm_emulator.c +12 -1
index 75c33d290e..653989ac0e 100644
@@ -364,6 +364,7 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb,
 {
     TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
     ptm_setbuffersize psbs;
+    size_t tpm_buffersize;
 
     if (tpm_emulator_stop_tpm(tb, errp) < 0) {
         return -1;
@@ -387,8 +388,18 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb,
         return -1;
     }
 
+    tpm_buffersize = be32_to_cpu(psbs.u.resp.buffersize);
+    /* Reject different buffer size used by the TPM than what was requested. */
+    if (wanted_size != 0 && wanted_size != tpm_buffersize) {
+        error_setg(errp,
+                   "tpm-emulator: TPM did not accept the requested buffer size "
+                   "of %zu bytes but adjusted it to %zu bytes\n",
+                   wanted_size, tpm_buffersize);
+        return -1;
+    }
+
     if (actual_size) {
-        *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
+        *actual_size = tpm_buffersize;
     }
 
     trace_tpm_emulator_set_buffer_size(
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/2] tpm_emulator: Reject a buffer size different than what was requested

Quoted 7 lines · Stefan Berger

When the TIS, SPAPR, or CRB frontends negotiate a buffer size with the TPM backend, then the tpm_emulator (swtpm) could still adjust this size of the buffer to within bounds supported by swtpm+libtpms if the chosen size was outside the acceptable range. This could theoretically lead to the TPM 2 using a bigger buffer than what was requested and memory allocated for. In practice this would not happend since the requested size of 4096 bytes for TIS and SPAPR and 3968 bytes for CRB are in the

happen

Quoted 7 lines
backends/tpm/tpm_emulator.c +0 -0
index 75c33d290e7..653989ac0e3 100644

backends/tpm/tpm_emulator.c static int tpm_emulator_set_buffer_size(TPMBackend *tb, · line 396

Quoted 9 lines
backends/tpm/tpm_emulator.c +6 -0
@@ -387,8 +388,18 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb,
         return -1;
     }
 
+    tpm_buffersize = be32_to_cpu(psbs.u.resp.buffersize);
+    /* Reject different buffer size used by the TPM than what was requested. */
+    if (wanted_size != 0 && wanted_size != tpm_buffersize) {
+        error_setg(errp,
+                   "tpm-emulator: TPM did not accept the requested buffer size "
+                   "of %zu bytes but adjusted it to %zu bytes\n",

Remove the trailing \n in error_setg() format string.

Signature · 1 line
Marc-André Lureau <[email protected]>
#3 lore raw

PATCH v3 2/2 tpm_emulator: Limit number of bytes read to negotiated buffer size

In reply to Stefan Berger

Limit the number of bytes read from the TPM response to the size of the receiving buffer, which is the same as the size of the buffer negotiated with swtpm.

The TPM TIS and SPAPR use 4096 bytes and the CRB 3968 bytes. There are currently no TPM 2 responses using this size of a buffer and therefore no response will be sent that is exceeding this size.

---
 backends/tpm/tpm_emulator.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
backends/tpm/tpm_emulator.c +8 -3
index 653989ac0e..050a1f4225 100644
@@ -176,8 +176,9 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
                                      bool *selftest_done,
                                      Error **errp)
 {
-    ssize_t ret;
     bool is_selftest = false;
+    size_t to_read;
+    ssize_t ret;
 
     if (selftest_done) {
         *selftest_done = false;
@@ -195,9 +196,13 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
         return -1;
     }
 
+    /*
+     * Size of response from swtpm must be <= out_len (= negotiated buffer size)
+     */
+    to_read = MIN(tpm_cmd_get_size(out), out_len) - sizeof(struct tpm_resp_hdr);
+
     ret = qio_channel_read_all(tpm_emu->data_ioc,
-              (char *)out + sizeof(struct tpm_resp_hdr),
-              tpm_cmd_get_size(out) - sizeof(struct tpm_resp_hdr), errp);
+              (char *)out + sizeof(struct tpm_resp_hdr), to_read, errp);
     if (ret != 0) {
         return -1;
     }
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v3 2/2] tpm_emulator: Limit number of bytes read to negotiated buffer size

Quoted 8 lines · Stefan Berger

Limit the number of bytes read from the TPM response to the size of the receiving buffer, which is the same as the size of the buffer negotiated with swtpm.

The TPM TIS and SPAPR use 4096 bytes and the CRB 3968 bytes. There are currently no TPM 2 responses using this size of a buffer and therefore no response will be sent that is exceeding this size.

Fixes f4ede81eed29 ("tpm: Added support for TPM emulator")
Quoted 35 lines

---> backends/tpm/tpm_emulator.c | 11 ++++++++---

  1 file changed, 8 insertions(+), 3 deletions(-)
backends/tpm/tpm_emulator.c +8 -3
index 653989ac0e..050a1f4225 100644
@@ -176,8 +176,9 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
                                       bool *selftest_done,
                                       Error **errp)
  {
-    ssize_t ret;
      bool is_selftest = false;
+    size_t to_read;
+    ssize_t ret;
 
      if (selftest_done) {
          *selftest_done = false;
@@ -195,9 +196,13 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
          return -1;
      }
 
+    /*
+     * Size of response from swtpm must be <= out_len (= negotiated buffer size)
+     */
+    to_read = MIN(tpm_cmd_get_size(out), out_len) - sizeof(struct tpm_resp_hdr);
+
      ret = qio_channel_read_all(tpm_emu->data_ioc,
-              (char *)out + sizeof(struct tpm_resp_hdr),
-              tpm_cmd_get_size(out) - sizeof(struct tpm_resp_hdr), errp);
+              (char *)out + sizeof(struct tpm_resp_hdr), to_read, errp);
      if (ret != 0) {
          return -1;
      }
#5 lore raw

Re: [PATCH v3 2/2] tpm_emulator: Limit number of bytes read to negotiated buffer size

In reply to Stefan Berger

backends/tpm/tpm_emulator.c static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, · line 202

Quoted 12 lines · Stefan Berger
backends/tpm/tpm_emulator.c +4 -0
index 653989ac0e3..050a1f4225c 100644
@@ -195,9 +196,13 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
         return -1;
     }
 
+    /*
+     * Size of response from swtpm must be <= out_len (= negotiated buffer size)
+     */
+    to_read = MIN(tpm_cmd_get_size(out), out_len) - sizeof(struct tpm_resp_hdr);

I would check tpm_cmd_get_size(out) <= out_len explicitly and return an error (probaby then disconnect) rather than silently corrupting the stream.

Signature · 1 line
Marc-André Lureau <[email protected]>