Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions src/tpm12/tpm_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,20 @@ TPM_RESULT TPM_Sha1Context_Load(void **context,
if ((rc== 0) && contextPresent) {
rc = TPM_Load32(&(sha_ctx->num), stream, stream_size);
}
/* 'num' is the number of bytes buffered in sha_ctx->data and is used by
OpenSSL as the write offset in SHA1_Update()/SHA1_Final(); it must be
less than the block size, else a later update walks off the context. */
if ((rc== 0) && contextPresent) {
if (sha_ctx->num >= SHA_CBLOCK) {
printf("TPM_Sha1Context_Load: Error, num %u out of range\n",
sha_ctx->num);
rc = TPM_BAD_PARAMETER;
}
}
if ((rc != 0) && contextPresent && (*context != NULL)) {
free(*context);
*context = NULL;
}
return rc;
}

Expand Down Expand Up @@ -2862,9 +2876,10 @@ TPM_RESULT TPM_SymmetricKeyData_Decrypt(unsigned char **decrypt_data, /* outpu
(TPM_SYMMETRIC_KEY_DATA *)tpm_symmetric_key_token;

printf(" TPM_SymmetricKeyData_Decrypt: Length %u\n", encrypt_length);
/* sanity check encrypted length */
/* sanity check encrypted length; CBC requires a whole number of blocks,
otherwise AES_cbc_encrypt() reads past the end of the input buffer */
if (rc == 0) {
if (encrypt_length < TPM_AES_BLOCK_SIZE) {
if ((encrypt_length % TPM_AES_BLOCK_SIZE) != 0) {
printf("TPM_SymmetricKeyData_Decrypt: Error, bad length\n");
rc = TPM_DECRYPT_ERROR;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tpm12/tpm_crypto_freebl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2323,9 +2323,9 @@ TPM_RESULT TPM_SymmetricKeyData_Decrypt(unsigned char **decrypt_data, /* outpu
printf(" TPM_SymmetricKeyData_Decrypt: Length %u\n", encrypt_length);
cx = NULL; /* freed @1 */

/* sanity check encrypted length */
/* sanity check encrypted length; CBC requires a whole number of blocks */
if (rc == 0) {
if (encrypt_length < TPM_AES_BLOCK_SIZE) {
if ((encrypt_length % TPM_AES_BLOCK_SIZE) != 0) {
printf("TPM_SymmetricKeyData_Decrypt: Error, bad length\n");
rc = TPM_DECRYPT_ERROR;
}
Expand Down
13 changes: 13 additions & 0 deletions src/tpm2/NVDynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ void NvReadObject(NV_REF ref, // IN: points to NV where index is located

return; // libtpms changed end
}
// libtpms added begin
static void NvReadObjectAttributes(NV_REF ref, // IN: points to NV where index is located,
OBJECT_ATTRIBUTES* attributes // OUT: place to receive the object attributes
)
{
OBJECT obj = { 0 };

NvReadObject(ref, &obj);
*attributes = obj.attributes;
} // libtpms added end

//*** NvFindEvict()
// This function will return the NV offset of an evict object
Expand Down Expand Up @@ -1414,10 +1424,13 @@ NvFlushHierarchy(TPMI_RH_HIERARCHY hierarchy // IN: hierarchy to be flushed.
{
OBJECT_ATTRIBUTES attributes;
//
#if 0 // libtpms changed begin
NvRead(&attributes,
(UINT32)(currentAddr + sizeof(TPM_HANDLE)
+ offsetof(OBJECT, attributes)),
sizeof(OBJECT_ATTRIBUTES));
#endif
NvReadObjectAttributes(currentAddr, &attributes); // libtpms changed end
// If the evict object belongs to the hierarchy to be flushed...
if((hierarchy == TPM_RH_PLATFORM && attributes.ppsHierarchy == SET)
|| (hierarchy == TPM_RH_OWNER && attributes.spsHierarchy == SET)
Expand Down
12 changes: 11 additions & 1 deletion src/tpm2/NVMarshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4148,6 +4148,9 @@ PERSISTENT_DATA_PPList_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, INT32 *si

if (rc == TPM_RC_SUCCESS) {
rc = UINT16_Unmarshal(&array_size, buffer, size);
/* check array_size for a reasonable maximum that should never be reached */
if (rc == TPM_RC_SUCCESS && array_size > 1024)
rc = TPM_RC_SIZE;
}
if (rc == TPM_RC_SUCCESS) {
BYTE buf[array_size];
Expand Down Expand Up @@ -4216,6 +4219,9 @@ PERSISTENT_DATA_AuditCommands_Unmarshal(PERSISTENT_DATA *data, BYTE **buffer, IN

if (rc == TPM_RC_SUCCESS) {
rc = UINT16_Unmarshal(&array_size, buffer, size);
/* check array_size for a reasonable maximum that should never be reached */
if (rc == TPM_RC_SUCCESS && array_size > 1024)
rc = TPM_RC_SIZE;
}
if (rc == TPM_RC_SUCCESS) {
BYTE buf[array_size];
Expand Down Expand Up @@ -5002,7 +5008,11 @@ USER_NVRAM_Unmarshal(BYTE **buffer, INT32 *size)

memset(&obj, 0, sizeof(obj));
rc = ANY_OBJECT_Unmarshal(&obj, buffer, size, true);
pAssert(rc == TPM_RC_SUCCESS);
if (rc != TPM_RC_SUCCESS) {
TPMLIB_LogTPM2Error("USER_NVRAM: Failed to unmarshal ANY_OBJECT: 0x%08x\n",
rc);
return rc;
}
// convert the OBJECT into a buffer to copy into NVRAM
marshalledObjectSize = NvObjectToBuffer(&obj, objBuffer, sizeof(objBuffer));

Expand Down
2 changes: 1 addition & 1 deletion src/tpm2/RuntimeAlgorithm.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ RuntimeAlgorithmSwitchProfile(struct RuntimeAlgorithm *RuntimeAlgorithm,
&stateFormatLevel, maxStateFormatLevel);
if (retVal != TPM_RC_SUCCESS) {
RuntimeAlgorithmSetProfile(RuntimeAlgorithm, *oldProfile,
&stateFormatLevel, maxStateFormatLevel);
&stateFormatLevel, ~0);
*oldProfile = NULL;
}
return retVal;
Expand Down
2 changes: 1 addition & 1 deletion src/tpm2/RuntimeAttributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ RuntimeAttributesSwitchProfile(struct RuntimeAttributes *RuntimeAttributes,
&stateFormatLevel, maxStateFormatLevel);
if (retVal != TPM_RC_SUCCESS) {
RuntimeAttributesSetProfile(RuntimeAttributes, *oldProfile,
&stateFormatLevel, maxStateFormatLevel);
&stateFormatLevel, ~0);
*oldProfile = NULL;
}
return retVal;
Expand Down
4 changes: 2 additions & 2 deletions src/tpm2/RuntimeCommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ RuntimeCommandsSwitchProfile(struct RuntimeCommands *RuntimeCommands,
&stateFormatLevel, maxStateFormatLevel);
if (retVal != TPM_RC_SUCCESS) {
RuntimeCommandsSetProfile(RuntimeCommands, *oldProfile,
&stateFormatLevel, maxStateFormatLevel);
&stateFormatLevel, ~0);
*oldProfile = NULL;
}
return retVal;
Expand Down Expand Up @@ -490,7 +490,7 @@ RuntimeCommandsGetArraySize(struct RuntimeCommands *RuntimeCommands)

/* search for byte where command with highest code is enabled */
for (i = sizeof(RuntimeCommands->enabledCommandsByIdx) - 1;
i >= 0;
(ssize_t)i >= 0;
i--) {
if (RuntimeCommands->enabledCommandsByIdx[i])
return i + 1;
Expand Down
3 changes: 3 additions & 0 deletions src/tpm2/RuntimeProfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ GetParametersFromJSON(const char *jsonProfile,
return TPM_RC_SUCCESS;
}

if (strlen(jsonProfile) > MAX_PROFILE_SIZE)
return TPM_RC_SIZE;

retVal = RuntimeProfileCheckJSON(jsonProfile);
if (retVal != TPM_RC_SUCCESS)
return retVal;
Expand Down
2 changes: 2 additions & 0 deletions src/tpm2/RuntimeProfile_fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "RuntimeCommands_fp.h"
#include "RuntimeAttributes_fp.h"

#define MAX_PROFILE_SIZE (32 * 1024)

struct RuntimeProfile {
struct RuntimeAlgorithm RuntimeAlgorithm;
struct RuntimeCommands RuntimeCommands;
Expand Down
15 changes: 8 additions & 7 deletions src/tpm2/Unmarshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
TPM_RC
UINT8_Unmarshal(UINT8 *target, BYTE **buffer, INT32 *size)
{
if ((UINT32)*size < sizeof(UINT8)) {
if (*size < 0 || (UINT32)*size < sizeof(UINT8)) {
return TPM_RC_INSUFFICIENT;
}
*target = (*buffer)[0];
Expand All @@ -65,7 +65,7 @@ INT8_Unmarshal(INT8 *target, BYTE **buffer, INT32 *size)
TPM_RC
UINT16_Unmarshal(UINT16 *target, BYTE **buffer, INT32 *size)
{
if ((UINT32)*size < sizeof(UINT16)) {
if (*size < 0 || (UINT32)*size < sizeof(UINT16)) {
return TPM_RC_INSUFFICIENT;
}
*target = ((UINT16)((*buffer)[0]) << 8) |
Expand All @@ -78,7 +78,7 @@ UINT16_Unmarshal(UINT16 *target, BYTE **buffer, INT32 *size)
TPM_RC
UINT32_Unmarshal(UINT32 *target, BYTE **buffer, INT32 *size)
{
if ((UINT32)*size < sizeof(UINT32)) {
if (*size < 0 || (UINT32)*size < sizeof(UINT32)) {
return TPM_RC_INSUFFICIENT;
}
*target = ((UINT32)((*buffer)[0]) << 24) |
Expand All @@ -93,7 +93,7 @@ UINT32_Unmarshal(UINT32 *target, BYTE **buffer, INT32 *size)
TPM_RC
UINT64_Unmarshal(UINT64 *target, BYTE **buffer, INT32 *size)
{
if ((UINT32)*size < sizeof(UINT64)) {
if (*size < 0 || (UINT32)*size < sizeof(UINT64)) {
return TPM_RC_INSUFFICIENT;
}
*target = ((UINT64)((*buffer)[0]) << 56) |
Expand All @@ -114,7 +114,7 @@ Array_Unmarshal(BYTE *targetBuffer, UINT16 targetSize, BYTE **buffer, INT32 *siz
{
TPM_RC rc = TPM_RC_SUCCESS;

if (targetSize > *size) {
if (*size < 0 || targetSize > (UINT32)*size) {
rc = TPM_RC_INSUFFICIENT;
}
else {
Expand Down Expand Up @@ -249,9 +249,10 @@ TPM_ECC_CURVE_Unmarshal(TPM_ECC_CURVE *target, BYTE **buffer, INT32 *size)
# endif // ECC_CURVE_448
if (*target != TPM_ECC_NONE && // libtpms added begin
!CryptEccIsCurveRuntimeUsable(*target)) {
rc = TPM_RC_CURVE;
rc = TPM_RC_CURVE;
}
if (!RuntimeAlgorithmKeySizeCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,
if (rc == TPM_RC_SUCCESS && *target != TPM_ECC_NONE &&
!RuntimeAlgorithmKeySizeCheckEnabled(&g_RuntimeProfile.RuntimeAlgorithm,
TPM_ALG_ECC,
CryptEccGetKeySizeForCurve(*target),
*target,
Expand Down
Loading