From 317e1b49449d23148c4233b508d0a1f14696eafc Mon Sep 17 00:00:00 2001 From: Luigi Semenzato Date: Fri, 23 Aug 2013 16:49:56 -0700 Subject: [PATCH] Avoid exit code overflow for tpmc. In case of a TPM error, tpmc returns the TPM error code, which can be greater than 255. In that case the error code is truncated. Some error codes, such as TPM_E_RETRY, end with a zero byte, resulting in a successful exit code. This is despicable. BUG=chromium:234357 TEST=tested with exit codes < 255. Too hard to generate the others. BRANCH=none Change-Id: I891a5c0659c06aac778449e2a0a935c5f82ccdb8 Reviewed-on: https://chromium-review.googlesource.com/66885 Reviewed-by: Luigi Semenzato Commit-Queue: Luigi Semenzato Tested-by: Luigi Semenzato --- utility/tpmc.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/utility/tpmc.c b/utility/tpmc.c index 98004e80fd..adcbf6edf7 100644 --- a/utility/tpmc.c +++ b/utility/tpmc.c @@ -19,7 +19,7 @@ #include "tpm_error_messages.h" #include "tss_constants.h" -#define OTHER_ERROR 255 +#define OTHER_ERROR 255 /* OTHER_ERROR must be the largest uint8_t value. */ typedef struct command_record { const char* name; @@ -61,9 +61,11 @@ int HexStringToUint8(const char* string, uint8_t* value) { /* TPM error check and reporting. Returns 0 if |result| is 0 (TPM_SUCCESS). * Otherwise looks up a TPM error in the error table and prints the error if - * found. + * found. Then returns min(result, OTHER_ERROR) since some error codes, such + * as TPM_E_RETRY, do not fit in a byte. */ -uint32_t ErrorCheck(uint32_t result, const char* cmd) { +uint8_t ErrorCheck(uint32_t result, const char* cmd) { + uint8_t exit_code = result > OTHER_ERROR ? OTHER_ERROR : result; if (result == 0) { return 0; } else { @@ -74,11 +76,11 @@ uint32_t ErrorCheck(uint32_t result, const char* cmd) { if (tpm_error_table[i].code == result) { fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name, tpm_error_table[i].description); - return result; + return exit_code; } } fprintf(stderr, "the TPM error code is unknown to this program\n"); - return result; + return exit_code; } }