]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg Tpm2CommandLib: Fix TPM2.0 response memory overflow
authorZhang, Chao B <chao.b.zhang@intel.com>
Tue, 20 Mar 2018 08:32:11 +0000 (16:32 +0800)
committerZhang, Chao B <chao.b.zhang@intel.com>
Wed, 21 Mar 2018 08:43:34 +0000 (16:43 +0800)
TPM2.0 command lib always assumes TPM device and transmission channel can
respond correctly. But it is not true when communication channel is exploited
and wrong data is spoofed. Add more logic to prohibit memory overflow attack.

Cc: Long Qin <qin.long@intel.com>
Cc: Yao Jiewen <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chao Zhang <chao.b.zhang@intel.com>
Reviewed-by: Long Qin <qin.long@intel.com>
Reviewed-by: Yao Jiewen <jiewen.yao@intel.com>
SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2EnhancedAuthorization.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2Integrity.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2NVStorage.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2Object.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2Sequences.c
SecurityPkg/Library/Tpm2CommandLib/Tpm2Session.c

index 79e80fb7a9e791437e6177d1405c5e14eb25670a..b11a8ac3e27db800f4f879c3fe71f8ff0fac8d05 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Capability related command.\r
 \r
 /** @file\r
   Implement TPM2 Capability related command.\r
 \r
-Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -112,6 +112,14 @@ Tpm2GetCapability (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  //\r
+  // Fail if command failed\r
+  //\r
+  if (SwapBytes32(RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {\r
+    DEBUG ((EFI_D_ERROR, "Tpm2GetCapability: Response Code error! 0x%08x\r\n", SwapBytes32(RecvBuffer.Header.responseCode)));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   //\r
   // Return the response\r
   //\r
   //\r
   // Return the response\r
   //\r
@@ -329,6 +337,11 @@ Tpm2GetCapabilitySupportedAlg (
   CopyMem (AlgList, &TpmCap.data.algorithms, sizeof (TPML_ALG_PROPERTY));\r
 \r
   AlgList->count = SwapBytes32 (AlgList->count);\r
   CopyMem (AlgList, &TpmCap.data.algorithms, sizeof (TPML_ALG_PROPERTY));\r
 \r
   AlgList->count = SwapBytes32 (AlgList->count);\r
+  if (AlgList->count > MAX_CAP_ALGS) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilitySupportedAlg - AlgList->count error %x\n", AlgList->count));\r
+    return EFI_DEVICE_ERROR; \r
+  }\r
+\r
   for (Index = 0; Index < AlgList->count; Index++) {\r
     AlgList->algProperties[Index].alg = SwapBytes16 (AlgList->algProperties[Index].alg);\r
     WriteUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties)));\r
   for (Index = 0; Index < AlgList->count; Index++) {\r
     AlgList->algProperties[Index].alg = SwapBytes16 (AlgList->algProperties[Index].alg);\r
     WriteUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties, SwapBytes32 (ReadUnaligned32 ((UINT32 *)&AlgList->algProperties[Index].algProperties)));\r
@@ -476,9 +489,18 @@ Tpm2GetCapabilityPcrs (
   }\r
 \r
   Pcrs->count = SwapBytes32 (TpmCap.data.assignedPCR.count);\r
   }\r
 \r
   Pcrs->count = SwapBytes32 (TpmCap.data.assignedPCR.count);\r
+  if (Pcrs->count > HASH_COUNT) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - Pcrs->count error %x\n", Pcrs->count));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   for (Index = 0; Index < Pcrs->count; Index++) {\r
     Pcrs->pcrSelections[Index].hash = SwapBytes16 (TpmCap.data.assignedPCR.pcrSelections[Index].hash);\r
     Pcrs->pcrSelections[Index].sizeofSelect = TpmCap.data.assignedPCR.pcrSelections[Index].sizeofSelect;\r
   for (Index = 0; Index < Pcrs->count; Index++) {\r
     Pcrs->pcrSelections[Index].hash = SwapBytes16 (TpmCap.data.assignedPCR.pcrSelections[Index].hash);\r
     Pcrs->pcrSelections[Index].sizeofSelect = TpmCap.data.assignedPCR.pcrSelections[Index].sizeofSelect;\r
+    if (Pcrs->pcrSelections[Index].sizeofSelect > PCR_SELECT_MAX) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityPcrs - sizeofSelect error %x\n", Pcrs->pcrSelections[Index].sizeofSelect));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (Pcrs->pcrSelections[Index].pcrSelect, TpmCap.data.assignedPCR.pcrSelections[Index].pcrSelect, Pcrs->pcrSelections[Index].sizeofSelect);\r
   }\r
 \r
     CopyMem (Pcrs->pcrSelections[Index].pcrSelect, TpmCap.data.assignedPCR.pcrSelections[Index].pcrSelect, Pcrs->pcrSelections[Index].sizeofSelect);\r
   }\r
 \r
index 6f6b3693f861b4f48d18d5eae08a85b44f91d4e0..a7a7bf2c653901012fa784ea6d767e76262357f1 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 EnhancedAuthorization related command.\r
 \r
 /** @file\r
   Implement TPM2 EnhancedAuthorization related command.\r
 \r
-Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -180,6 +180,12 @@ Tpm2PolicySecret (
   //\r
   Buffer = (UINT8 *)&RecvBuffer.Timeout;\r
   Timeout->size = SwapBytes16(ReadUnaligned16 ((UINT16 *)Buffer));\r
   //\r
   Buffer = (UINT8 *)&RecvBuffer.Timeout;\r
   Timeout->size = SwapBytes16(ReadUnaligned16 ((UINT16 *)Buffer));\r
+  if (Timeout->size > sizeof(UINT64)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PolicySecret - Timeout->size error %x\n", Timeout->size));\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto Done;\r
+  }\r
+\r
   Buffer += sizeof(UINT16);\r
   CopyMem (Timeout->buffer, Buffer, Timeout->size);\r
 \r
   Buffer += sizeof(UINT16);\r
   CopyMem (Timeout->buffer, Buffer, Timeout->size);\r
 \r
@@ -189,6 +195,12 @@ Tpm2PolicySecret (
   Buffer += sizeof(UINT32);\r
   PolicyTicket->digest.size = SwapBytes16(ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
   Buffer += sizeof(UINT32);\r
   PolicyTicket->digest.size = SwapBytes16(ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
+  if (PolicyTicket->digest.size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PolicySecret - digest.size error %x\n", PolicyTicket->digest.size));\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto Done;\r
+  }\r
+\r
   CopyMem (PolicyTicket->digest.buffer, Buffer, PolicyTicket->digest.size);\r
 \r
 Done:\r
   CopyMem (PolicyTicket->digest.buffer, Buffer, PolicyTicket->digest.size);\r
 \r
 Done:\r
@@ -379,6 +391,11 @@ Tpm2PolicyGetDigest (
   // Return the response\r
   //\r
   PolicyHash->size = SwapBytes16 (RecvBuffer.PolicyHash.size);\r
   // Return the response\r
   //\r
   PolicyHash->size = SwapBytes16 (RecvBuffer.PolicyHash.size);\r
+  if (PolicyHash->size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PolicyGetDigest - PolicyHash->size error %x\n", PolicyHash->size));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   CopyMem (PolicyHash->buffer, &RecvBuffer.PolicyHash.buffer, PolicyHash->size);\r
 \r
   return EFI_SUCCESS;\r
   CopyMem (PolicyHash->buffer, &RecvBuffer.PolicyHash.buffer, PolicyHash->size);\r
 \r
   return EFI_SUCCESS;\r
index 180f36083096f86e76011bb195e0aa9c89fb8387..cc77785de1c040bb87812a4b3b3b4d2f06b8d95c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 help.\r
 \r
 /** @file\r
   Implement TPM2 help.\r
 \r
-Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -150,7 +150,8 @@ CopyAuthSessionCommand (
   @param [in]  AuthSessionIn   Input AuthSession data in TPM2 response buffer\r
   @param [out] AuthSessionOut  Output AuthSession data\r
 \r
   @param [in]  AuthSessionIn   Input AuthSession data in TPM2 response buffer\r
   @param [out] AuthSessionOut  Output AuthSession data\r
 \r
-  @return AuthSession size\r
+  @return 0    copy failed\r
+          else AuthSession size\r
 **/\r
 UINT32\r
 EFIAPI\r
 **/\r
 UINT32\r
 EFIAPI\r
@@ -171,6 +172,10 @@ CopyAuthSessionResponse (
   // nonce\r
   AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
   // nonce\r
   AuthSessionOut->nonce.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
+  if (AuthSessionOut->nonce.size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - nonce.size error %x\n", AuthSessionOut->nonce.size));\r
+    return 0;\r
+  }\r
 \r
   CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r
   Buffer += AuthSessionOut->nonce.size;\r
 \r
   CopyMem (AuthSessionOut->nonce.buffer, Buffer, AuthSessionOut->nonce.size);\r
   Buffer += AuthSessionOut->nonce.size;\r
@@ -182,6 +187,10 @@ CopyAuthSessionResponse (
   // hmac\r
   AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
   // hmac\r
   AuthSessionOut->hmac.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
+  if (AuthSessionOut->hmac.size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "CopyAuthSessionResponse - hmac.size error %x\n", AuthSessionOut->hmac.size));\r
+    return 0;\r
+  }\r
 \r
   CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
   Buffer += AuthSessionOut->hmac.size;\r
 \r
   CopyMem (AuthSessionOut->hmac.buffer, Buffer, AuthSessionOut->hmac.size);\r
   Buffer += AuthSessionOut->hmac.size;\r
index 8eacfe6c137c051a72f543a32db92942aa273e4d..af52322c275bc8f7622b5cb11f3e2165deb24cb0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Integrity related command.\r
 \r
 /** @file\r
   Implement TPM2 Integrity related command.\r
 \r
-Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -279,6 +279,11 @@ Tpm2PcrEvent (
   Buffer = (UINT8 *)&Res.Digests;\r
 \r
   Digests->count = SwapBytes32 (ReadUnaligned32 ((UINT32 *)Buffer));\r
   Buffer = (UINT8 *)&Res.Digests;\r
 \r
   Digests->count = SwapBytes32 (ReadUnaligned32 ((UINT32 *)Buffer));\r
+  if (Digests->count > HASH_COUNT) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PcrEvent - Digests->count error %x\n", Digests->count));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   Buffer += sizeof(UINT32);\r
   for (Index = 0; Index < Digests->count; Index++) {\r
     Digests->digests[Index].hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT32);\r
   for (Index = 0; Index < Digests->count; Index++) {\r
     Digests->digests[Index].hashAlg = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
@@ -383,6 +388,11 @@ Tpm2PcrRead (
     return EFI_DEVICE_ERROR;\r
   }\r
   PcrSelectionOut->count = SwapBytes32(RecvBuffer.PcrSelectionOut.count);\r
     return EFI_DEVICE_ERROR;\r
   }\r
   PcrSelectionOut->count = SwapBytes32(RecvBuffer.PcrSelectionOut.count);\r
+  if (PcrSelectionOut->count > HASH_COUNT) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrSelectionOut->count error %x\n", PcrSelectionOut->count));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count) {\r
     DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", RecvBufferSize));\r
     return EFI_DEVICE_ERROR;\r
   if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count) {\r
     DEBUG ((EFI_D_ERROR, "Tpm2PcrRead - RecvBufferSize Error - %x\n", RecvBufferSize));\r
     return EFI_DEVICE_ERROR;\r
@@ -390,6 +400,9 @@ Tpm2PcrRead (
   for (Index = 0; Index < PcrSelectionOut->count; Index++) {\r
     PcrSelectionOut->pcrSelections[Index].hash = SwapBytes16(RecvBuffer.PcrSelectionOut.pcrSelections[Index].hash);\r
     PcrSelectionOut->pcrSelections[Index].sizeofSelect = RecvBuffer.PcrSelectionOut.pcrSelections[Index].sizeofSelect;\r
   for (Index = 0; Index < PcrSelectionOut->count; Index++) {\r
     PcrSelectionOut->pcrSelections[Index].hash = SwapBytes16(RecvBuffer.PcrSelectionOut.pcrSelections[Index].hash);\r
     PcrSelectionOut->pcrSelections[Index].sizeofSelect = RecvBuffer.PcrSelectionOut.pcrSelections[Index].sizeofSelect;\r
+    if (PcrSelectionOut->pcrSelections[Index].sizeofSelect > PCR_SELECT_MAX) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (&PcrSelectionOut->pcrSelections[Index].pcrSelect, &RecvBuffer.PcrSelectionOut.pcrSelections[Index].pcrSelect, PcrSelectionOut->pcrSelections[Index].sizeofSelect);\r
   }\r
 \r
     CopyMem (&PcrSelectionOut->pcrSelections[Index].pcrSelect, &RecvBuffer.PcrSelectionOut.pcrSelections[Index].pcrSelect, PcrSelectionOut->pcrSelections[Index].sizeofSelect);\r
   }\r
 \r
@@ -398,9 +411,20 @@ Tpm2PcrRead (
   //\r
   PcrValuesOut = (TPML_DIGEST *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count);\r
   PcrValues->count = SwapBytes32(PcrValuesOut->count);\r
   //\r
   PcrValuesOut = (TPML_DIGEST *)((UINT8 *)&RecvBuffer + sizeof (TPM2_RESPONSE_HEADER) + sizeof(RecvBuffer.PcrUpdateCounter) + sizeof(RecvBuffer.PcrSelectionOut.count) + sizeof(RecvBuffer.PcrSelectionOut.pcrSelections[0]) * PcrSelectionOut->count);\r
   PcrValues->count = SwapBytes32(PcrValuesOut->count);\r
+  //\r
+  // The number of digests in list is not greater than 8 per TPML_DIGEST definition\r
+  //\r
+  if (PcrValues->count > 8) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - PcrValues->count error %x\n", PcrValues->count));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
   Digests = PcrValuesOut->digests;\r
   for (Index = 0; Index < PcrValues->count; Index++) {\r
     PcrValues->digests[Index].size = SwapBytes16(Digests->size);\r
   Digests = PcrValuesOut->digests;\r
   for (Index = 0; Index < PcrValues->count; Index++) {\r
     PcrValues->digests[Index].size = SwapBytes16(Digests->size);\r
+    if (PcrValues->digests[Index].size > sizeof(TPMU_HA)) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2PcrRead - Digest.size error %x\n", PcrValues->digests[Index].size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (&PcrValues->digests[Index].buffer, &Digests->buffer, PcrValues->digests[Index].size);\r
     Digests = (TPM2B_DIGEST *)((UINT8 *)Digests + sizeof(Digests->size) + PcrValues->digests[Index].size);\r
   }\r
     CopyMem (&PcrValues->digests[Index].buffer, &Digests->buffer, PcrValues->digests[Index].size);\r
     Digests = (TPM2B_DIGEST *)((UINT8 *)Digests + sizeof(Digests->size) + PcrValues->digests[Index].size);\r
   }\r
index 950802213232c80cf6df5457754b40f4e62e898d..14b10952415ec49dadb08c03d3788ffde07cf931 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 NVStorage related command.\r
 \r
 /** @file\r
   Implement TPM2 NVStorage related command.\r
 \r
-Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -234,10 +234,19 @@ Tpm2NvReadPublic (
   // Basic check\r
   //\r
   NvPublicSize = SwapBytes16 (RecvBuffer.NvPublic.size);\r
   // Basic check\r
   //\r
   NvPublicSize = SwapBytes16 (RecvBuffer.NvPublic.size);\r
+  if (NvPublicSize > sizeof(TPMS_NV_PUBLIC)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - NvPublic.size error %x\n", NvPublicSize));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   NvNameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + NvPublicSize)));\r
   NvNameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + NvPublicSize)));\r
+  if (NvNameSize > sizeof(TPMU_NAME)){\r
+    DEBUG ((DEBUG_ERROR, "Tpm2NvReadPublic - NvNameSize error %x\n", NvNameSize));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
 \r
   if (RecvBufferSize != sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + NvPublicSize + sizeof(UINT16) + NvNameSize) {\r
 \r
   if (RecvBufferSize != sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + NvPublicSize + sizeof(UINT16) + NvNameSize) {\r
-    DEBUG ((EFI_D_ERROR, "Tpm2NvReadPublic - RecvBufferSize Error - NvPublicSize %x, NvNameSize %x\n", RecvBufferSize, NvNameSize));\r
+    DEBUG ((EFI_D_ERROR, "Tpm2NvReadPublic - RecvBufferSize Error - NvPublicSize %x\n", RecvBufferSize));\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
     return EFI_NOT_FOUND;\r
   }\r
 \r
@@ -632,6 +641,12 @@ Tpm2NvRead (
   // Return the response\r
   //\r
   OutData->size = SwapBytes16 (RecvBuffer.Data.size);\r
   // Return the response\r
   //\r
   OutData->size = SwapBytes16 (RecvBuffer.Data.size);\r
+  if (OutData->size > MAX_DIGEST_BUFFER) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2NvRead - OutData->size error %x\n", OutData->size));\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto Done;\r
+  }\r
+\r
   CopyMem (OutData->buffer, &RecvBuffer.Data.buffer, OutData->size);\r
   \r
 Done:\r
   CopyMem (OutData->buffer, &RecvBuffer.Data.buffer, OutData->size);\r
   \r
 Done:\r
index e070ff2f74a4f13a46648983a5c152af0becb737..89f30a2c6e6a24a2430ba62ab82251ad5e05f273 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Object related command.\r
 \r
 /** @file\r
   Implement TPM2 Object related command.\r
 \r
-Copyright (c) 2017, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -109,11 +109,25 @@ Tpm2ReadPublic (
   // Basic check\r
   //\r
   OutPublicSize = SwapBytes16 (RecvBuffer.OutPublic.size);\r
   // Basic check\r
   //\r
   OutPublicSize = SwapBytes16 (RecvBuffer.OutPublic.size);\r
+  if (OutPublicSize > sizeof(TPMT_PUBLIC)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - OutPublicSize error %x\n", OutPublicSize));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   NameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) +\r
                           sizeof(UINT16) + OutPublicSize)));\r
   NameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) +\r
                           sizeof(UINT16) + OutPublicSize)));\r
+  if (NameSize > sizeof(TPMU_NAME)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - NameSize error %x\n", NameSize));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   QualifiedNameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) +\r
                                    sizeof(UINT16) + OutPublicSize +\r
                                    sizeof(UINT16) + NameSize)));\r
   QualifiedNameSize = SwapBytes16 (ReadUnaligned16 ((UINT16 *)((UINT8 *)&RecvBuffer + sizeof(TPM2_RESPONSE_HEADER) +\r
                                    sizeof(UINT16) + OutPublicSize +\r
                                    sizeof(UINT16) + NameSize)));\r
+  if (QualifiedNameSize > sizeof(TPMU_NAME)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - QualifiedNameSize error %x\n", QualifiedNameSize));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
 \r
   if (RecvBufferSize != sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + OutPublicSize + sizeof(UINT16) + NameSize + sizeof(UINT16) + QualifiedNameSize) {\r
     DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - RecvBufferSize %x Error - OutPublicSize %x, NameSize %x, QualifiedNameSize %x\n", RecvBufferSize, OutPublicSize, NameSize, QualifiedNameSize));\r
 \r
   if (RecvBufferSize != sizeof(TPM2_RESPONSE_HEADER) + sizeof(UINT16) + OutPublicSize + sizeof(UINT16) + NameSize + sizeof(UINT16) + QualifiedNameSize) {\r
     DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - RecvBufferSize %x Error - OutPublicSize %x, NameSize %x, QualifiedNameSize %x\n", RecvBufferSize, OutPublicSize, NameSize, QualifiedNameSize));\r
@@ -132,6 +146,11 @@ Tpm2ReadPublic (
   Buffer = (UINT8 *)&RecvBuffer.OutPublic.publicArea.authPolicy;\r
   OutPublic->publicArea.authPolicy.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
   Buffer = (UINT8 *)&RecvBuffer.OutPublic.publicArea.authPolicy;\r
   OutPublic->publicArea.authPolicy.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
   Buffer += sizeof(UINT16);\r
+  if (OutPublic->publicArea.authPolicy.size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - authPolicy.size error %x\n", OutPublic->publicArea.authPolicy.size));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   CopyMem (OutPublic->publicArea.authPolicy.buffer, Buffer, OutPublic->publicArea.authPolicy.size);\r
   Buffer += OutPublic->publicArea.authPolicy.size;\r
 \r
   CopyMem (OutPublic->publicArea.authPolicy.buffer, Buffer, OutPublic->publicArea.authPolicy.size);\r
   Buffer += OutPublic->publicArea.authPolicy.size;\r
 \r
@@ -307,28 +326,48 @@ Tpm2ReadPublic (
   case TPM_ALG_KEYEDHASH:\r
     OutPublic->publicArea.unique.keyedHash.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
   case TPM_ALG_KEYEDHASH:\r
     OutPublic->publicArea.unique.keyedHash.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
+    if(OutPublic->publicArea.unique.keyedHash.size > sizeof(TPMU_HA)) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - keyedHash.size error %x\n", OutPublic->publicArea.unique.keyedHash.size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (OutPublic->publicArea.unique.keyedHash.buffer, Buffer, OutPublic->publicArea.unique.keyedHash.size);\r
     Buffer += OutPublic->publicArea.unique.keyedHash.size;\r
     break;\r
   case TPM_ALG_SYMCIPHER:\r
     OutPublic->publicArea.unique.sym.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
     CopyMem (OutPublic->publicArea.unique.keyedHash.buffer, Buffer, OutPublic->publicArea.unique.keyedHash.size);\r
     Buffer += OutPublic->publicArea.unique.keyedHash.size;\r
     break;\r
   case TPM_ALG_SYMCIPHER:\r
     OutPublic->publicArea.unique.sym.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
+    if(OutPublic->publicArea.unique.sym.size > sizeof(TPMU_HA)) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - sym.size error %x\n", OutPublic->publicArea.unique.sym.size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (OutPublic->publicArea.unique.sym.buffer, Buffer, OutPublic->publicArea.unique.sym.size);\r
     Buffer += OutPublic->publicArea.unique.sym.size;\r
     break;\r
   case TPM_ALG_RSA:\r
     OutPublic->publicArea.unique.rsa.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
     CopyMem (OutPublic->publicArea.unique.sym.buffer, Buffer, OutPublic->publicArea.unique.sym.size);\r
     Buffer += OutPublic->publicArea.unique.sym.size;\r
     break;\r
   case TPM_ALG_RSA:\r
     OutPublic->publicArea.unique.rsa.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
+    if(OutPublic->publicArea.unique.rsa.size > MAX_RSA_KEY_BYTES) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - rsa.size error %x\n", OutPublic->publicArea.unique.rsa.size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (OutPublic->publicArea.unique.rsa.buffer, Buffer, OutPublic->publicArea.unique.rsa.size);\r
     Buffer += OutPublic->publicArea.unique.rsa.size;\r
     break;\r
   case TPM_ALG_ECC:\r
     OutPublic->publicArea.unique.ecc.x.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
     CopyMem (OutPublic->publicArea.unique.rsa.buffer, Buffer, OutPublic->publicArea.unique.rsa.size);\r
     Buffer += OutPublic->publicArea.unique.rsa.size;\r
     break;\r
   case TPM_ALG_ECC:\r
     OutPublic->publicArea.unique.ecc.x.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
+    if (OutPublic->publicArea.unique.ecc.x.size > MAX_ECC_KEY_BYTES) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - ecc.x.size error %x\n", OutPublic->publicArea.unique.ecc.x.size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (OutPublic->publicArea.unique.ecc.x.buffer, Buffer, OutPublic->publicArea.unique.ecc.x.size);\r
     Buffer += OutPublic->publicArea.unique.ecc.x.size;\r
     OutPublic->publicArea.unique.ecc.y.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
     CopyMem (OutPublic->publicArea.unique.ecc.x.buffer, Buffer, OutPublic->publicArea.unique.ecc.x.size);\r
     Buffer += OutPublic->publicArea.unique.ecc.x.size;\r
     OutPublic->publicArea.unique.ecc.y.size = SwapBytes16 (ReadUnaligned16 ((UINT16 *)Buffer));\r
     Buffer += sizeof(UINT16);\r
+    if (OutPublic->publicArea.unique.ecc.y.size > MAX_ECC_KEY_BYTES) {\r
+      DEBUG ((DEBUG_ERROR, "Tpm2ReadPublic - ecc.y.size error %x\n", OutPublic->publicArea.unique.ecc.y.size));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
     CopyMem (OutPublic->publicArea.unique.ecc.y.buffer, Buffer, OutPublic->publicArea.unique.ecc.y.size);\r
     Buffer += OutPublic->publicArea.unique.ecc.y.size;\r
     break;\r
     CopyMem (OutPublic->publicArea.unique.ecc.y.buffer, Buffer, OutPublic->publicArea.unique.ecc.y.size);\r
     Buffer += OutPublic->publicArea.unique.ecc.y.size;\r
     break;\r
index 305b6f20784a9132f38566afb112b87d88a8640f..90877768d3afcc53e9291865ae2c924f2b1633bf 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Sequences related command.\r
 \r
 /** @file\r
   Implement TPM2 Sequences related command.\r
 \r
-Copyright (c) 2013, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -375,6 +375,11 @@ Tpm2EventSequenceComplete (
 \r
   // count\r
   Results->count = SwapBytes32(ReadUnaligned32 ((UINT32 *)BufferPtr));\r
 \r
   // count\r
   Results->count = SwapBytes32(ReadUnaligned32 ((UINT32 *)BufferPtr));\r
+  if (Results->count > HASH_COUNT) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2EventSequenceComplete - Results->count error %x\n", Results->count));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   BufferPtr += sizeof(UINT32);\r
 \r
   for (Index = 0; Index < Results->count; Index++) {\r
   BufferPtr += sizeof(UINT32);\r
 \r
   for (Index = 0; Index < Results->count; Index++) {\r
@@ -496,6 +501,11 @@ Tpm2SequenceComplete (
 \r
   // digestSize\r
   Result->size = SwapBytes16(ReadUnaligned16 ((UINT16 *)BufferPtr));\r
 \r
   // digestSize\r
   Result->size = SwapBytes16(ReadUnaligned16 ((UINT16 *)BufferPtr));\r
+  if (Result->size > sizeof(TPMU_HA)){\r
+    DEBUG ((DEBUG_ERROR, "Tpm2SequenceComplete - Result->size error %x\n", Result->size));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   BufferPtr += sizeof(UINT16);\r
 \r
   CopyMem(\r
   BufferPtr += sizeof(UINT16);\r
 \r
   CopyMem(\r
index f03b6689ac02454e09e170469a8b376805b806a1..35ad86a17af9d5bba9a1b5e4f2a136599838f107 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Session related command.\r
 \r
 /** @file\r
   Implement TPM2 Session related command.\r
 \r
-Copyright (c) 2014, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved. <BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -163,6 +163,11 @@ Tpm2StartAuthSession (
   //\r
   *SessionHandle = SwapBytes32 (RecvBuffer.SessionHandle);\r
   NonceTPM->size = SwapBytes16 (RecvBuffer.NonceTPM.size);\r
   //\r
   *SessionHandle = SwapBytes32 (RecvBuffer.SessionHandle);\r
   NonceTPM->size = SwapBytes16 (RecvBuffer.NonceTPM.size);\r
+  if (NonceTPM->size > sizeof(TPMU_HA)) {\r
+    DEBUG ((DEBUG_ERROR, "Tpm2StartAuthSession - NonceTPM->size error %x\n", NonceTPM->size));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   CopyMem (NonceTPM->buffer, &RecvBuffer.NonceTPM.buffer, NonceTPM->size);\r
 \r
   return EFI_SUCCESS;\r
   CopyMem (NonceTPM->buffer, &RecvBuffer.NonceTPM.buffer, NonceTPM->size);\r
 \r
   return EFI_SUCCESS;\r