Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/tpm2_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6695,6 +6695,10 @@ int wolfTPM2_ResetPCR(WOLFTPM2_DEV* dev, int pcrIndex)
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 [Medium] New wrapper validation paths are not covered by regression tests · Missing Tests

The PR adds defensive validation to four public PCR/hash wrappers: wolfTPM2_ResetPCR and wolfTPM2_ExtendPCR now reject out-of-range PCR indexes (return BAD_FUNC_ARG), wolfTPM2_ReadPCR now rejects a successful empty PCR response (pcrValues.count == 0 -> TPM_RC_FAILURE) before reading digests[0], and wolfTPM2_HashStart now rejects any algorithm for which TPM2_GetHashDigestSize(hashAlg) == 0. These are observable local wrapper contracts, but the diff changes only src/tpm2_wrap.c and adds no regression tests. The existing tests only exercise happy paths (PCR reset/extend success, HashStart with TPM_ALG_SHA256) and do not assert the new negative paths: invalid PCR indexes, non-hash HashStart input, or an empty PCR read response. Severity views differ: the review mode rated this Medium while review-security rated it Info; keeping the stricter Medium. This is not a blocking security issue in the changed code, but regression coverage would prevent the behavior from being accidentally relaxed later.

Fix: Add focused regression tests in tests/unit_tests.c for the new negative paths: wolfTPM2_ResetPCR(&dev, PCR_LAST + 1) and wolfTPM2_ExtendPCR(&dev, PCR_LAST + 1, ...) returning BAD_FUNC_ARG, wolfTPM2_HashStart(&dev, &hash, TPM_ALG_RSA, NULL, 0) returning BAD_FUNC_ARG, and an empty PCR read response (pcrValues.count == 0 -> TPM_RC_FAILURE) using a harness/backend configuration that can produce or mock that TPM response.

int rc;
PCR_Reset_In pcrReset;

if (pcrIndex < (int)PCR_FIRST || pcrIndex > (int)PCR_LAST)
return BAD_FUNC_ARG;

XMEMSET(&pcrReset, 0, sizeof(pcrReset));
pcrReset.pcrHandle = pcrIndex;
rc = TPM2_PCR_Reset(&pcrReset);
Expand Down Expand Up @@ -6816,6 +6820,12 @@ int wolfTPM2_ReadPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg, byte* digest,
return rc;
}

/* A successful read must return at least one digest; otherwise the
* digests[0] access below reads uninitialized data. */
if (pcrReadOut.pcrValues.count == 0) {
return TPM_RC_FAILURE;
}

digestLen = (int)pcrReadOut.pcrValues.digests[0].size;
if (digest) {
if (pDigestLen == NULL) {
Expand Down Expand Up @@ -6852,7 +6862,8 @@ int wolfTPM2_ExtendPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg,
int rc;
PCR_Extend_In pcrExtend;

if (dev == NULL || digestLen < 0 || digestLen > TPM_MAX_DIGEST_SIZE) {
if (dev == NULL || digestLen < 0 || digestLen > TPM_MAX_DIGEST_SIZE ||
pcrIndex < (int)PCR_FIRST || pcrIndex > (int)PCR_LAST) {
return BAD_FUNC_ARG;
}

Expand Down Expand Up @@ -7712,7 +7723,9 @@ int wolfTPM2_HashStart(WOLFTPM2_DEV* dev, WOLFTPM2_HASH* hash,
HashSequenceStart_In in;
HashSequenceStart_Out out;

if (dev == NULL || hash == NULL || hashAlg == TPM_ALG_NULL ||
/* TPM2_GetHashDigestSize() is 0 for any algorithm that is not a hash,
* so this rejects them locally instead of relying on TPM_RC_HASH. */
if (dev == NULL || hash == NULL || TPM2_GetHashDigestSize(hashAlg) == 0 ||
(usageAuthSz > 0 && usageAuth == NULL)) {
return BAD_FUNC_ARG;
}
Expand Down
Loading