Improve handling of response buffer size in tpm_emulator
v4 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
Stefan Berger (2):
tpm_emulator: Reject a buffer size different than what was requested
tpm_emulator: Disconnect if response exceeds negotiated buffer size
backends/tpm/tpm_emulator.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.0
PATCH v4 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 happen 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
@@ -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" , + 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.53.0
Re: [PATCH v4 1/2] tpm_emulator: Reject a buffer size different than what was requested
Quoted 12 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 happen 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.
Quoted 2 lines
Fixes 9375c44fdfc0 ("tpm: tpm_emulator: get and set buffer size of device")Signed-off-by Stefan Berger <[email protected]>
Quoted 39 lines
--- backends/tpm/tpm_emulator.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)backends/tpm/tpm_emulator.c +12
-2@@ -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" , + 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( --2.53.0
PATCH v4 2/2 tpm_emulator: Disconnect if response exceeds negotiated buffer size
In reply to Stefan BergerDisconnect from the emulator if a response was to exceed the negotiated buffer size.
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 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +20 -2
@@ -176,8 +176,10 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,bool *selftest_done, Error **errp) { - ssize_t ret; bool is_selftest = false; +Error *local_err =NULL ; +uint32_t to_read; +ssize_t ret;if (selftest_done) { *selftest_done = false;
@@ -195,9 +197,25 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,return -1 ; } +/* +* Size of response from emulator must be <= out_len (= negotiated buffer +* size) +*/ + to_read =tpm_cmd_get_size (out); +if (to_read > out_len) { +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)" , + to_read, out_len); +return -1 ; + } + 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); + to_read -sizeof (struct tpm_resp_hdr ), errp);if (ret !=0 ) {return -1 ; }
Signature · 1 line
2.53.0
Re: [PATCH v4 2/2] tpm_emulator: Disconnect if response exceeds negotiated buffer size
Quoted 10 lines · Stefan Berger
Disconnect from the emulator if a response was to exceed the negotiated buffer size.
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")Signed-off-by Stefan Berger <[email protected]>
Quoted 50 lines
--- backends/tpm/tpm_emulator.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)backends/tpm/tpm_emulator.c +20
-3@@ -176,8 +176,10 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,bool *selftest_done, Error **errp) { - ssize_t ret; bool is_selftest = false; +Error *local_err =NULL ; +uint32_t to_read; +ssize_t ret;if (selftest_done) { *selftest_done = false;@@ -195,9 +197,25 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,return -1 ; } +/* +* Size of response from emulator must be <= out_len (= negotiated buffer +* size) +*/ + to_read =tpm_cmd_get_size (out); +if (to_read > out_len) { +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)" , + to_read, out_len); +return -1 ; + } + 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); + to_read -sizeof (struct tpm_resp_hdr ), errp);if (ret !=0 ) {return -1 ; } --2.53.0