]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
SecurityPkg Tpm2CommandLib: Fix TPM2.0 response memory overflow
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Capability.c
index 9aab17f9345fdb7a71f225470274661a41ed35c1..b11a8ac3e27db800f4f879c3fe71f8ff0fac8d05 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Capability 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
@@ -112,6 +112,14 @@ Tpm2GetCapability (
     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
@@ -189,7 +197,7 @@ Tpm2GetCapabilityManufactureID (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  *ManufactureId = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);\r
+  *ManufactureId = TpmCap.data.tpmProperties.tpmProperty->value;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -329,6 +337,11 @@ Tpm2GetCapabilitySupportedAlg (
   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
@@ -476,9 +489,18 @@ Tpm2GetCapabilityPcrs (
   }\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
+    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