security: tcg-2.0: Ignore data payload for errors, fix Cr50 boot mode

This patch improves the response buffer handling for TPM 2.0. Previously
we would allow any command to return no payload, but if there was a
payload we would always try to unmarshal it according to the normal
success response. This was sort of relying on the fact that the TPM
usually returns no additional data after the header for error responses,
but in practice that is not always true. It also means that commands
without a response payload accidentally work by default even though we
did not explicitly add unmarshallig support for them, which seems
undesirable. Adding explicit unmarshalling support for TPM2_SelfTest
which was only supported through this loophole before.

This patch changes the behavior to always accept any amount of payload
data for error responses but not unmarshal any of it. None of our use
cases actually care about payload data for errors, so it seems safer to
not even try to interpret it. For success responses, on the other hand,
we always require support for the command to be explicitly added.

This fixes a problem with the Cr50 GET_BOOT_MODE command where an error
response would only return the subcommand code but no data after that.
Also add support for a second, slightly different NO_SUCH_COMMAND error
code that was added in Cr50 recently.

Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: Ib85032d85482d5484180be6fd105f2467f393cd2
Reviewed-on: https://review.coreboot.org/c/coreboot/+/41100
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Julius Werner 2020-05-06 11:11:03 -07:00 committed by Patrick Georgi
parent f8f5650873
commit 71a131415e
3 changed files with 17 additions and 8 deletions

View File

@ -587,17 +587,23 @@ struct tpm2_response *tpm_unmarshal_response(TPM_CC command, struct ibuf *ib)
if (rc != 0) if (rc != 0)
return NULL; return NULL;
if (ibuf_remaining(ib) == 0) { if (ibuf_capacity(ib) != tpm2_static_resp.hdr.tpm_size) {
if (tpm2_static_resp.hdr.tpm_size != ibuf_nr_read(ib))
printk(BIOS_ERR, printk(BIOS_ERR,
"%s: size mismatch in response to command %#x\n", "%s: size mismatch in response to command %#x\n",
__func__, command); __func__, command);
return &tpm2_static_resp; return NULL;
} }
/* On errors, we're not sure what the TPM is returning. None of the
commands we use actually expect useful data payloads for errors, so
just ignore any data after the header. */
if (tpm2_static_resp.hdr.tpm_code != TPM2_RC_SUCCESS)
return &tpm2_static_resp;
switch (command) { switch (command) {
case TPM2_Startup: case TPM2_Startup:
case TPM2_Shutdown: case TPM2_Shutdown:
case TPM2_SelfTest:
break; break;
case TPM2_GetCapability: case TPM2_GetCapability:

View File

@ -89,7 +89,8 @@ uint32_t tlcl_cr50_get_tpm_mode(uint8_t *tpm_mode)
return TPM_E_MUST_REBOOT; return TPM_E_MUST_REBOOT;
} }
if (response->hdr.tpm_code == VENDOR_RC_NO_SUCH_COMMAND) { if (response->hdr.tpm_code == VENDOR_RC_NO_SUCH_COMMAND ||
response->hdr.tpm_code == VENDOR_RC_NO_SUCH_SUBCOMMAND) {
/* /*
* Explicitly inform caller when command is not supported * Explicitly inform caller when command is not supported
*/ */
@ -119,7 +120,8 @@ uint32_t tlcl_cr50_get_boot_mode(uint8_t *boot_mode)
if (!response) if (!response)
return TPM_E_IOERROR; return TPM_E_IOERROR;
if (response->hdr.tpm_code == VENDOR_RC_NO_SUCH_COMMAND) if (response->hdr.tpm_code == VENDOR_RC_NO_SUCH_COMMAND ||
response->hdr.tpm_code == VENDOR_RC_NO_SUCH_SUBCOMMAND)
/* Explicitly inform caller when command is not supported */ /* Explicitly inform caller when command is not supported */
return TPM_E_NO_SUCH_COMMAND; return TPM_E_NO_SUCH_COMMAND;

View File

@ -21,6 +21,7 @@
#define VENDOR_RC_ERR 0x00000500 #define VENDOR_RC_ERR 0x00000500
enum cr50_vendor_rc { enum cr50_vendor_rc {
VENDOR_RC_INTERNAL_ERROR = (VENDOR_RC_ERR | 6), VENDOR_RC_INTERNAL_ERROR = (VENDOR_RC_ERR | 6),
VENDOR_RC_NO_SUCH_SUBCOMMAND = (VENDOR_RC_ERR | 8),
VENDOR_RC_NO_SUCH_COMMAND = (VENDOR_RC_ERR | 127), VENDOR_RC_NO_SUCH_COMMAND = (VENDOR_RC_ERR | 127),
}; };