Fix a couple of issues in TPM related code
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
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: Free ppi.buf 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 | 12 ++++-- hw/tpm/tpm_crb.c | 23 +++++++--- 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 ++ 13 files changed, 196 insertions(+), 16 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/10 tpm_util: Pass TPMVersion to function writing fatal error response
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 | 12 +++++++++--- include/system/tpm_util.h | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-)
backends/tpm/tpm_emulator.c +2 -1
@@ -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
@@ -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 +9 -3
@@ -87,12 +87,18 @@ 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 ); +if (tpm_version ==TPM_VERSION_1_2 ) { +tpm_cmd_set_tag (out,TPM_TAG_RSP_COMMAND ); +tpm_cmd_set_error (out,TPM_FAIL ); + }else { +tpm_cmd_set_tag (out,TPM2_ST_NO_SESSIONS ); +tpm_cmd_set_error (out,TPM_RC_FAILURE ); + }tpm_cmd_set_size (out,sizeof (struct tpm_resp_hdr )); -tpm_cmd_set_error (out,TPM_FAIL ); } }
include/system/tpm_util.h +2 -1
@@ -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
Re: [PATCH 01/10] tpm_util: Pass TPMVersion to function writing fatal error response
Quoted 42 lines
Have
tpm_util_write_fatal_error_responsewrite a TPM version specific error response with error codeTPM_FAILfor a TPM 1.2 andTPM_RC_FAILUREfor a TPM 2. Adjust the callers.Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
backends/tpm/tpm_emulator.c +2
-1@@ -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@@ -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 +0
-0@@ -87,12 +87,18 @@ const PropertyInfo qdev_prop_tpm = {/* * Write an error message in the given output buffer. */
backends/tpm/tpm_util.c const PropertyInfo qdev_prop_tpm = { · line 97
Quoted 10 lines
backends/tpm/tpm_util.c +6
-2-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 ); +if (tpm_version ==TPM_VERSION_1_2 ) { +tpm_cmd_set_tag (out,TPM_TAG_RSP_COMMAND ); +tpm_cmd_set_error (out,TPM_FAIL ); + }else {
I would rather use a switch/case than fallback
Quoted 13 lines
backends/tpm/tpm_util.c +3
-1+tpm_cmd_set_tag (out,TPM2_ST_NO_SESSIONS ); +tpm_cmd_set_error (out,TPM_RC_FAILURE ); + }tpm_cmd_set_size (out,sizeof (struct tpm_resp_hdr )); -tpm_cmd_set_error (out,TPM_FAIL ); } }include/system/tpm_util.h +0
-0
include/system/tpm_util.h line 32
Quoted 9 lines
include/system/tpm_util.h +2
-1@@ -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
Marc-André Lureau <[email protected]>
Re: [PATCH 01/10] tpm_util: Pass TPMVersion to function writing fatal error response
Quoted 46 lines · On 9/24/26 3:41 AM, [email protected] wrote:
Quoted 46 lines
Have
tpm_util_write_fatal_error_responsewrite a TPM version specific error response with error codeTPM_FAILfor a TPM 1.2 andTPM_RC_FAILUREfor a TPM 2. Adjust the callers.Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
backends/tpm/tpm_emulator.c +2
-1@@ -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@@ -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 +2
-1@@ -87,12 +87,18 @@ 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) {
Quoted 8 lines
backends/tpm/tpm_util.c const PropertyInfo qdev_prop_tpm = { · line 97
Quoted 6 lines
backends/tpm/tpm_util.c +4
-1if (out_len >=sizeof (struct tpm_resp_hdr )) { -tpm_cmd_set_tag (out,TPM_TAG_RSP_COMMAND ); +if (tpm_version ==TPM_VERSION_1_2 ) { +tpm_cmd_set_tag (out,TPM_TAG_RSP_COMMAND ); +tpm_cmd_set_error (out,TPM_FAIL ); + }else {I would rather use a switch/case than fallback
Will change for v2.
PATCH 2/10 tpm_emulator: Reject TPM responses whose size is too small
In reply to Stefan BergerReject 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
@@ -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
Re: [PATCH 02/10] tpm_emulator: Reject TPM responses whose size is too small
Quoted 2 lines · Stefan Berger
Reject a response from the TPM emulator whose size field indicates that the response is smaller than the size of the header.
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 3/10 tpm_emulator: Reject sending commands smaller than the request header
In reply to Stefan BergerReject any TPM request from being sent to the TPM emulator that is shorter than the size of the request header.
--- backends/tpm/tpm_emulator.c | 5 +++++ 1 file changed, 5 insertions(+)
backends/tpm/tpm_emulator.c +5 -0
@@ -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
Re: [PATCH 03/10] tpm_emulator: Reject sending commands smaller than the request header
Quoted 12 lines
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")Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
backends/tpm/tpm_emulator.c +0
-0
backends/tpm/tpm_emulator.c static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, · line 187
Quoted 7 lines
backends/tpm/tpm_emulator.c +4
-0@@ -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 ; + }
It could probably be an assert() instead.
backends/tpm/tpm_emulator.c static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, · line 191
Quoted 4 lines
backends/tpm/tpm_emulator.c +1
-0+if (selftest_done) { *selftest_done = false; is_selftest =tpm_util_is_selftest (in, in_len);
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [PATCH 03/10] tpm_emulator: Reject sending commands smaller than the request header
Quoted 15 lines · On 9/24/26 3:41 AM, [email protected] wrote:
Quoted 15 lines
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")Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
backends/tpm/tpm_emulator.c +0
-0@@ -181,6 +181,11 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,uint32_t to_read;ssize_t ret;
Quoted 6 lines
backends/tpm/tpm_emulator.c static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, · line 187
Quoted 4 lines
backends/tpm/tpm_emulator.c +4
-0+if (in_len <sizeof (struct tpm_req_hdr )) { +error_setg (errp,"tpm-emulator: invalid request size %u" , in_len); +return -1 ; + }It could probably be an
assert()instead.
The TIS may actually send a command with only 6 bytes.
Quoted 8 lines
backends/tpm/tpm_emulator.c static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu, · line 191
Quoted 5 lines
backends/tpm/tpm_emulator.c +1
-0+if (selftest_done) { *selftest_done = false; is_selftest =tpm_util_is_selftest (in, in_len);Reviewed-by Marc-André Lureau <[email protected]>
PATCH 4/10 hw/tpm: tis: Only read from response buffer if rw_offset < len
In reply to Stefan BergerOnly 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
@@ -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
Re: [PATCH 04/10] hw/tpm: tis: Only read from response buffer if rw_offset < len
Quoted 1 line
Only read a byte from the response buffer, if the
rw_offset< len.
OOB read from malicious driver is CVE-level.
Quoted 16 lines
Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_tis_common.c +3
-1@@ -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 );
hw/tpm/tpm_tis_common.c static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty) · line 287
Quoted 10 lines
hw/tpm/tpm_tis_common.c +1
-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
Marc-André Lureau <[email protected]>
Re: [PATCH 04/10] hw/tpm: tis: Only read from response buffer if rw_offset < len
Quoted 3 lines · On 9/24/26 3:41 AM, [email protected] wrote:
Quoted 1 line
Only read a byte from the response buffer, if the
rw_offset< len.OOB read from malicious driver is CVE-level.
It would not read outside the allocated bounds of the s->buffer but outside of the bounds of the returned TPM response. But this is only possible if 0 bytes were returned, so it would only ever read s->buffer[0]. The foollowing line then resets the TPM_TIS_STS_DATA_AVAILABLE flag so that it would not read another byte. IMO, this is not a CVE.
tpm_tis_sts_set(&s->loc[locty], TPM_TIS_STS_VALID);
Quoted 30 lines
Quoted 17 lines
Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_tis_common.c +3
-1@@ -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 );hw/tpm/tpm_tis_common.c static uint32_t tpm_tis_data_read(TPMState *s, uint8_t locty) · line 287
Quoted 10 lines
hw/tpm/tpm_tis_common.c +1
-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;Reviewed-by Marc-André Lureau <[email protected]>
PATCH 5/10 hw/tpm: tis: Use uint32_t for len to avoid truncating size of command
In reply to Stefan BergerTo 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
@@ -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
Re: [PATCH 05/10] hw/tpm: tis: Use uint32_t for len to avoid truncating size of command
Quoted 2 lines · Stefan Berger
To avoid truncating the
uint32_treturn value fromtpm_cmd_get_size()declare len asuint32_trather thanuint16_t.
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 6/10 hw/tpm: crb: Do not clear internal buffers when backend is busy
In reply to Stefan BergerWhile 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.
--- hw/tpm/tpm_crb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/tpm/tpm_crb.c +2 -1
@@ -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
Re: [PATCH 06/10] hw/tpm: crb: Do not clear internal buffers when backend is busy
Quoted 5 lines
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")
It's the following commit that makes use of it, but ok
This may also lead to OOB read/crashes from malicious guest driver.
Quoted 8 lines
Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_crb.c +0
-0
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 226
Quoted 10 lines
hw/tpm/tpm_crb.c +2
-1@@ -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
Marc-André Lureau <[email protected]>
Re: [PATCH 06/10] hw/tpm: crb: Do not clear internal buffers when backend is busy
Quoted 9 lines · On 9/24/26 3:41 AM, [email protected] wrote:
Quoted 5 lines
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")It's the following commit that makes use of it, but ok
This may also lead to OOB read/crashes from malicious guest driver.
For example g_byte_array_set_size(s->response_buffer, 0) does not seem to deallocate the memory in s->response_buffer just because it was set to size 0.
Running the tests/qtest/tpm-crb-swtpm-test with this modification here requires me to disable asserts for expected responses but no crash actually occurs.
@@ -254,6 +254,7 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,
.out_len = s->response_buffer->len,
};
tpm_backend_deliver_request(s->tpmbe, &s->cmd);
+ tpm_crb_clear_internal_buffers(s);
} else if (val & CRB_START_NEXT_CHUNK) {
if (!s->cap_chunk) {
break;Quoted 22 lines
Quoted 9 lines
Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_crb.c +0
-0hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 226
Quoted 10 lines
hw/tpm/tpm_crb.c +2
-1@@ -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 :Reviewed-by Marc-André Lureau <[email protected]>
PATCH 7/10 hw/tpm: crb: Avoid potential integer underflow
In reply to Stefan BergerAvoid 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
@@ -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
Re: [PATCH 07/10] hw/tpm: crb: Avoid potential integer underflow
Quoted 11 lines · Stefan Berger
Avoid a potential integer underflow in
tpm_crb_fill_command_responsethat could occur ifs->response_offset>s->response_buffer->lenand the function's call sites were to change.
tpm_crb_fill_command_responsecurrently has 3 callers that either test that 's->response_offset<s->response_buffer->len' is true or ensure thats->response_offset= 0 ands->response_buffer->len>= 0. Therefore, the integer underflow cannot currently occur. In the worst case the subtraction would lead to a '0'.[...]
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 8/10 hw/tpm: crb: Free ppi.buf in tpm_crb_unrealize
In reply to Stefan BergerFix a memory leak by freeing ppi.buf in tpm_crb_unrealize.
--- hw/tpm/tpm_crb.c | 3 +++ 1 file changed, 3 insertions(+)
hw/tpm/tpm_crb.c +3 -0
@@ -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); } + +qemu_vfree (s->ppi.buf); }static void tpm_crb_class_init (ObjectClass *klass,const void *data)
Signature · 1 line
2.55.0
Re: [PATCH 08/10] hw/tpm: crb: Free ppi.buf in tpm_crb_unrealize
Quoted 18 lines
Fix a memory leak by freeing ppi.buf in
tpm_crb_unrealize.Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_crb.c +1
-0@@ -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"
hw/tpm/tpm_crb.c static void tpm_crb_unrealize(DeviceState *dev) · line 534
Quoted 5 lines
hw/tpm/tpm_crb.c +2
-0@@ -529,6 +530,8 @@ static void tpm_crb_unrealize(DeviceState *dev)if (s->migration_blocker) {migrate_del_blocker (&s->migration_blocker); } + +qemu_vfree (s->ppi.buf);
We should have tpm_ppi_uninit(), there are other things that needs to be taken care of, such add memory region and vmstate...
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 9/10 hw/tpm: crb: Consider response_buffer->len of received response
In reply to Stefan BergerWhen 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.
--- hw/tpm/tpm_crb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/tpm/tpm_crb.c +3 -1
@@ -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
Re: [PATCH 09/10] hw/tpm: crb: Consider response_buffer->len of received response
Quoted 5 lines
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 theresponse_bufferas being possibly shorter than the negotiated backend buffer size or the size indicated in the response itself.
Since the backend is considered trusted, this is not a security issue.
Quoted 16 lines
Fixes 2a660ad67d15 ("hw/tpm: Implement TPM CRB chunking logic")Signed-off-by Stefan Berger <[email protected]>Message-ID: <[email protected]>
hw/tpm/tpm_crb.c +1
-1@@ -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,
hw/tpm/tpm_crb.c static void tpm_crb_request_completed(TPMIf *ti, int ret) · line 339
Quoted 10 lines
hw/tpm/tpm_crb.c +2
-0tpmSts, 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
Marc-André Lureau <[email protected]>
PATCH 10/10 tests: Add tests with commands with bad length
In reply to Stefan BergerAdd 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH 10/10] tests: Add tests with commands with bad length
Quoted 6 lines · Stefan Berger
Add two test cases with commands with bad length:
- Send a command that indicates a very large size (
0x10000bytes) 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.
Quoted 6 lines
- 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.
[...]
Signature · 1 line
Marc-André Lureau <[email protected]>