tpm2: Initialize variable and check rc before accessing nrh.size (Coverity)

Coverity complains that nrh may not be initialize when copying nrh.size
from it into the buffer pointer to by nrhp. So resolve this by clearing
nrh at the beginning of the loop and checking 'rc' after the Unmarshal.

Previously we could have copied an uninitialized nrh.size but would have
propagated the rc error code from UINT32_Unmarshal(), so this fix doesn't
really change anything.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2021-02-21 08:04:21 -05:00 committed by Stefan Berger
parent 0f86f8b53b
commit b4ef12a69c

View File

@ -4173,6 +4173,7 @@ INDEX_ORDERLY_RAM_Unmarshal(void *array, size_t array_size,
}
while (rc == TPM_RC_SUCCESS) {
memset(&nrh, 0, sizeof(nrh)); /* coverity */
/* nrhp may point to misaligned address (ubsan)
* we read 'into' nrh and copy to nrhp at end
*/
@ -4188,7 +4189,7 @@ INDEX_ORDERLY_RAM_Unmarshal(void *array, size_t array_size,
if (rc == TPM_RC_SUCCESS) {
rc = UINT32_Unmarshal(&nrh.size, buffer, size);
if (nrh.size == 0) {
if (rc == TPM_RC_SUCCESS && nrh.size == 0) {
memcpy(nrhp, &nrh, sizeof(nrh.size));
break;
}