Message ID | 20190725150832.1180275-3-stefanb@linux.vnet.ibm.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | tpm: Improve on error handling | expand |
On Thu, Jul 25, 2019 at 7:08 PM Stefan Berger <stefanb@linux.vnet.ibm.com> wrote: > > Implement a function to translate TPM error codes to strings so that > at least the most common error codes can be translated to human > readable strings. > > Signed-off-by: Stefan Berger <stefanb@linux.ibm.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> > --- > hw/tpm/tpm_emulator.c | 60 +++++++++++++++++++++++++++++++++++-------- > hw/tpm/tpm_int.h | 13 ++++++++++ > 2 files changed, 63 insertions(+), 10 deletions(-) > > diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c > index 1288cbcb8d..fc0b512f4f 100644 > --- a/hw/tpm/tpm_emulator.c > +++ b/hw/tpm/tpm_emulator.c > @@ -82,6 +82,40 @@ typedef struct TPMEmulator { > TPMBlobBuffers state_blobs; > } TPMEmulator; > > +struct tpm_error { > + uint32_t tpm_result; > + const char *string; > +}; > + > +static const struct tpm_error tpm_errors[] = { > + /* TPM 1.2 error codes */ > + { TPM_BAD_PARAMETER , "a parameter is bad" }, > + { TPM_FAIL , "operation failed" }, > + { TPM_KEYNOTFOUND , "key could not be found" }, > + { TPM_BAD_PARAM_SIZE , "bad parameter size"}, > + { TPM_ENCRYPT_ERROR , "encryption error" }, > + { TPM_DECRYPT_ERROR , "decryption error" }, > + { TPM_BAD_KEY_PROPERTY, "bad key property" }, > + { TPM_BAD_MODE , "bad (encryption) mode" }, > + { TPM_BAD_VERSION , "bad version identifier" }, > + { TPM_BAD_LOCALITY , "bad locality" }, > + /* TPM 2 error codes */ > + { TPM_RC_FAILURE , "operation failed" }, > + { TPM_RC_LOCALITY , "bad locality" }, > + { TPM_RC_INSUFFICIENT, "insufficient amount of data" }, > +}; > + > +static const char *tpm_emulator_strerror(uint32_t tpm_result) > +{ > + size_t i; > + > + for (i = 0; i < ARRAY_SIZE(tpm_errors); i++) { > + if (tpm_errors[i].tpm_result == tpm_result) { > + return tpm_errors[i].string; > + } > + } > + return ""; > +} > > static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg, > size_t msg_len_in, size_t msg_len_out) > @@ -264,7 +298,8 @@ static int tpm_emulator_stop_tpm(TPMBackend *tb) > > res = be32_to_cpu(res); > if (res) { > - error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res); > + error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x %s", res, > + tpm_emulator_strerror(res)); > return -1; > } > > @@ -293,8 +328,9 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb, > > psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result); > if (psbs.u.resp.tpm_result != 0) { > - error_report("tpm-emulator: TPM result for set buffer size : 0x%x", > - psbs.u.resp.tpm_result); > + error_report("tpm-emulator: TPM result for set buffer size : 0x%x %s", > + psbs.u.resp.tpm_result, > + tpm_emulator_strerror(psbs.u.resp.tpm_result)); > return -1; > } > > @@ -339,7 +375,8 @@ static int tpm_emulator_startup_tpm_resume(TPMBackend *tb, size_t buffersize, > > res = be32_to_cpu(init.u.resp.tpm_result); > if (res) { > - error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res); > + error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x %s", res, > + tpm_emulator_strerror(res)); > goto err_exit; > } > return 0; > @@ -399,8 +436,9 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb, > > res = be32_to_cpu(reset_est.u.resp.tpm_result); > if (res) { > - error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x", > - res); > + error_report( > + "tpm-emulator: TPM result for rest established flag: 0x%x %s", > + res, tpm_emulator_strerror(res)); > return -1; > } > > @@ -638,7 +676,8 @@ static int tpm_emulator_get_state_blob(TPMEmulator *tpm_emu, > res = be32_to_cpu(pgs.u.resp.tpm_result); > if (res != 0 && (res & 0x800) == 0) { > error_report("tpm-emulator: Getting the stateblob (type %d) failed " > - "with a TPM error 0x%x", type, res); > + "with a TPM error 0x%x %s", type, res, > + tpm_emulator_strerror(res)); > return -1; > } > > @@ -758,7 +797,8 @@ static int tpm_emulator_set_state_blob(TPMEmulator *tpm_emu, > tpm_result = be32_to_cpu(pss.u.resp.tpm_result); > if (tpm_result != 0) { > error_report("tpm-emulator: Setting the stateblob (type %d) failed " > - "with a TPM error 0x%x", type, tpm_result); > + "with a TPM error 0x%x %s", type, tpm_result, > + tpm_emulator_strerror(tpm_result)); > return -1; > } > > @@ -888,8 +928,8 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu) > error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s", > strerror(errno)); > } else if (res != 0) { > - error_report("tpm-emulator: TPM result for sutdown: 0x%x", > - be32_to_cpu(res)); > + error_report("tpm-emulator: TPM result for shutdown: 0x%x %s", > + be32_to_cpu(res), tpm_emulator_strerror(be32_to_cpu(res))); > } > } > > diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h > index a4c77fbd7e..3fb28a9d6c 100644 > --- a/hw/tpm/tpm_int.h > +++ b/hw/tpm/tpm_int.h > @@ -39,7 +39,16 @@ struct tpm_resp_hdr { > #define TPM_TAG_RSP_AUTH1_COMMAND 0xc5 > #define TPM_TAG_RSP_AUTH2_COMMAND 0xc6 > > +#define TPM_BAD_PARAMETER 3 > #define TPM_FAIL 9 > +#define TPM_KEYNOTFOUND 13 > +#define TPM_BAD_PARAM_SIZE 25 > +#define TPM_ENCRYPT_ERROR 32 > +#define TPM_DECRYPT_ERROR 33 > +#define TPM_BAD_KEY_PROPERTY 40 > +#define TPM_BAD_MODE 44 > +#define TPM_BAD_VERSION 46 > +#define TPM_BAD_LOCALITY 61 > > #define TPM_ORD_ContinueSelfTest 0x53 > #define TPM_ORD_GetTicks 0xf1 > @@ -59,4 +68,8 @@ struct tpm_resp_hdr { > > #define TPM2_PT_MAX_COMMAND_SIZE 0x11e > > +#define TPM_RC_INSUFFICIENT 0x9a > +#define TPM_RC_FAILURE 0x101 > +#define TPM_RC_LOCALITY 0x907 > + > #endif /* TPM_TPM_INT_H */ > -- > 2.20.1 >
diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c index 1288cbcb8d..fc0b512f4f 100644 --- a/hw/tpm/tpm_emulator.c +++ b/hw/tpm/tpm_emulator.c @@ -82,6 +82,40 @@ typedef struct TPMEmulator { TPMBlobBuffers state_blobs; } TPMEmulator; +struct tpm_error { + uint32_t tpm_result; + const char *string; +}; + +static const struct tpm_error tpm_errors[] = { + /* TPM 1.2 error codes */ + { TPM_BAD_PARAMETER , "a parameter is bad" }, + { TPM_FAIL , "operation failed" }, + { TPM_KEYNOTFOUND , "key could not be found" }, + { TPM_BAD_PARAM_SIZE , "bad parameter size"}, + { TPM_ENCRYPT_ERROR , "encryption error" }, + { TPM_DECRYPT_ERROR , "decryption error" }, + { TPM_BAD_KEY_PROPERTY, "bad key property" }, + { TPM_BAD_MODE , "bad (encryption) mode" }, + { TPM_BAD_VERSION , "bad version identifier" }, + { TPM_BAD_LOCALITY , "bad locality" }, + /* TPM 2 error codes */ + { TPM_RC_FAILURE , "operation failed" }, + { TPM_RC_LOCALITY , "bad locality" }, + { TPM_RC_INSUFFICIENT, "insufficient amount of data" }, +}; + +static const char *tpm_emulator_strerror(uint32_t tpm_result) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(tpm_errors); i++) { + if (tpm_errors[i].tpm_result == tpm_result) { + return tpm_errors[i].string; + } + } + return ""; +} static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg, size_t msg_len_in, size_t msg_len_out) @@ -264,7 +298,8 @@ static int tpm_emulator_stop_tpm(TPMBackend *tb) res = be32_to_cpu(res); if (res) { - error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res); + error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x %s", res, + tpm_emulator_strerror(res)); return -1; } @@ -293,8 +328,9 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb, psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result); if (psbs.u.resp.tpm_result != 0) { - error_report("tpm-emulator: TPM result for set buffer size : 0x%x", - psbs.u.resp.tpm_result); + error_report("tpm-emulator: TPM result for set buffer size : 0x%x %s", + psbs.u.resp.tpm_result, + tpm_emulator_strerror(psbs.u.resp.tpm_result)); return -1; } @@ -339,7 +375,8 @@ static int tpm_emulator_startup_tpm_resume(TPMBackend *tb, size_t buffersize, res = be32_to_cpu(init.u.resp.tpm_result); if (res) { - error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res); + error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x %s", res, + tpm_emulator_strerror(res)); goto err_exit; } return 0; @@ -399,8 +436,9 @@ static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb, res = be32_to_cpu(reset_est.u.resp.tpm_result); if (res) { - error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x", - res); + error_report( + "tpm-emulator: TPM result for rest established flag: 0x%x %s", + res, tpm_emulator_strerror(res)); return -1; } @@ -638,7 +676,8 @@ static int tpm_emulator_get_state_blob(TPMEmulator *tpm_emu, res = be32_to_cpu(pgs.u.resp.tpm_result); if (res != 0 && (res & 0x800) == 0) { error_report("tpm-emulator: Getting the stateblob (type %d) failed " - "with a TPM error 0x%x", type, res); + "with a TPM error 0x%x %s", type, res, + tpm_emulator_strerror(res)); return -1; } @@ -758,7 +797,8 @@ static int tpm_emulator_set_state_blob(TPMEmulator *tpm_emu, tpm_result = be32_to_cpu(pss.u.resp.tpm_result); if (tpm_result != 0) { error_report("tpm-emulator: Setting the stateblob (type %d) failed " - "with a TPM error 0x%x", type, tpm_result); + "with a TPM error 0x%x %s", type, tpm_result, + tpm_emulator_strerror(tpm_result)); return -1; } @@ -888,8 +928,8 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu) error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s", strerror(errno)); } else if (res != 0) { - error_report("tpm-emulator: TPM result for sutdown: 0x%x", - be32_to_cpu(res)); + error_report("tpm-emulator: TPM result for shutdown: 0x%x %s", + be32_to_cpu(res), tpm_emulator_strerror(be32_to_cpu(res))); } } diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index a4c77fbd7e..3fb28a9d6c 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -39,7 +39,16 @@ struct tpm_resp_hdr { #define TPM_TAG_RSP_AUTH1_COMMAND 0xc5 #define TPM_TAG_RSP_AUTH2_COMMAND 0xc6 +#define TPM_BAD_PARAMETER 3 #define TPM_FAIL 9 +#define TPM_KEYNOTFOUND 13 +#define TPM_BAD_PARAM_SIZE 25 +#define TPM_ENCRYPT_ERROR 32 +#define TPM_DECRYPT_ERROR 33 +#define TPM_BAD_KEY_PROPERTY 40 +#define TPM_BAD_MODE 44 +#define TPM_BAD_VERSION 46 +#define TPM_BAD_LOCALITY 61 #define TPM_ORD_ContinueSelfTest 0x53 #define TPM_ORD_GetTicks 0xf1 @@ -59,4 +68,8 @@ struct tpm_resp_hdr { #define TPM2_PT_MAX_COMMAND_SIZE 0x11e +#define TPM_RC_INSUFFICIENT 0x9a +#define TPM_RC_FAILURE 0x101 +#define TPM_RC_LOCALITY 0x907 + #endif /* TPM_TPM_INT_H */
Implement a function to translate TPM error codes to strings so that at least the most common error codes can be translated to human readable strings. Signed-off-by: Stefan Berger <stefanb@linux.ibm.com> --- hw/tpm/tpm_emulator.c | 60 +++++++++++++++++++++++++++++++++++-------- hw/tpm/tpm_int.h | 13 ++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-)