]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/Tpm2CommandLib/Tpm2NVStorage.c
SecurityPkg Tpm2CommandLib: Fix TPM2.0 response memory overflow
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2NVStorage.c
index 950802213232c80cf6df5457754b40f4e62e898d..14b10952415ec49dadb08c03d3788ffde07cf931 100644 (file)
@@ -1,7 +1,7 @@
 /** @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
@@ -234,10 +234,19 @@ Tpm2NvReadPublic (
   // 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
+  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
-    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
@@ -632,6 +641,12 @@ Tpm2NvRead (
   // 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