]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg Tcg2Dxe: Add check for the PE/COFF image
authorLiming Gao <liming.gao@intel.com>
Wed, 13 Jul 2016 12:28:17 +0000 (20:28 +0800)
committerLiming Gao <liming.gao@intel.com>
Thu, 14 Jul 2016 07:04:54 +0000 (15:04 +0800)
Use BasePeCoffLib PeCoffLoaderGetImageInfo() to check the PE/COFF image.

In V2, add specific ImageRead() to make sure the PE/COFF image content
read is within the image buffer.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>
SecurityPkg/Tcg/Tcg2Dxe/MeasureBootPeCoff.c
SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.c
SecurityPkg/Tcg/Tcg2Dxe/Tcg2Dxe.inf

index e2d763a54894f585f99851a4b0d485d8992017e8..de55ed9923b6225efa77b2fe1bb5eafe6ef8f5ac 100644 (file)
@@ -6,7 +6,7 @@
   This external input must be validated carefully to avoid security issue like\r
   buffer overflow, integer overflow.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2016, 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
@@ -29,6 +29,56 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/Tpm2CommandLib.h>\r
 #include <Library/HashLib.h>\r
 \r
+UINTN  mTcg2DxeImageSize = 0;\r
+\r
+/**\r
+  Reads contents of a PE/COFF image in memory buffer.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  PE/COFF image is external input, so this function will make sure the PE/COFF image content\r
+  read is within the image buffer.\r
+\r
+  @param  FileHandle      Pointer to the file handle to read the PE/COFF image.\r
+  @param  FileOffset      Offset into the PE/COFF image to begin the read operation.\r
+  @param  ReadSize        On input, the size in bytes of the requested read operation.\r
+                          On output, the number of bytes actually read.\r
+  @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
+\r
+  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tcg2DxeImageRead (\r
+  IN     VOID    *FileHandle,\r
+  IN     UINTN   FileOffset,\r
+  IN OUT UINTN   *ReadSize,\r
+  OUT    VOID    *Buffer\r
+  )\r
+{\r
+  UINTN               EndPosition;\r
+\r
+  if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (MAX_ADDRESS - FileOffset < *ReadSize) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  EndPosition = FileOffset + *ReadSize;\r
+  if (EndPosition > mTcg2DxeImageSize) {\r
+    *ReadSize = (UINT32)(mTcg2DxeImageSize - FileOffset);\r
+  }\r
+\r
+  if (FileOffset >= mTcg2DxeImageSize) {\r
+    *ReadSize = 0;\r
+  }\r
+\r
+  CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Measure PE image into TPM log based on the authenticode image hashing in\r
   PE/COFF Specification 8.0 Appendix A.\r
@@ -37,6 +87,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
   PE/COFF image is external input, so this function will validate its data structure\r
   within this image buffer before use.\r
 \r
+  Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().\r
+\r
   @param[in]  PCRIndex       TPM PCR index\r
   @param[in]  ImageAddress   Start address of image buffer.\r
   @param[in]  ImageSize      Image size\r
@@ -69,6 +121,7 @@ MeasurePeImageAndExtend (
   UINT32                               NumberOfRvaAndSizes;\r
   UINT32                               CertSize;\r
   HASH_HANDLE                          HashHandle;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT         ImageContext;\r
 \r
   HashHandle = 0xFFFFFFFF; // Know bad value\r
 \r
@@ -78,6 +131,23 @@ MeasurePeImageAndExtend (
   //\r
   // Check PE/COFF image\r
   //\r
+  ZeroMem (&ImageContext, sizeof (ImageContext));\r
+  ImageContext.Handle    = (VOID *) (UINTN) ImageAddress;\r
+  mTcg2DxeImageSize      = ImageSize;\r
+  ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) Tcg2DxeImageRead;\r
+\r
+  //\r
+  // Get information about the image being loaded\r
+  //\r
+  Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The information can't be got from the invalid PeImage\r
+    //\r
+    DEBUG ((DEBUG_INFO, "Tcg2Dxe: PeImage invalid. Cannot retrieve image information.\n"));\r
+    goto Finish;\r
+  }\r
+\r
   DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;\r
   PeCoffHeaderOffset = 0;\r
   if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
index 7720c2708db9128c66f1370c081151b7d32e9074..95219c0fca3877d454e0edc531cc8ffbd1177698 100644 (file)
@@ -127,6 +127,8 @@ EFI_HANDLE mImageHandle;
   PE/COFF image is external input, so this function will validate its data structure\r
   within this image buffer before use.\r
 \r
+  Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().\r
+\r
   @param[in]  PCRIndex       TPM PCR index\r
   @param[in]  ImageAddress   Start address of image buffer.\r
   @param[in]  ImageSize      Image size\r
index fd120e55383414581cf0a1384a7e0898509abc11..6b4c15ffdd7d41299453dbc0ec919f19ab604085 100644 (file)
@@ -59,6 +59,7 @@
   PerformanceLib\r
   ReportStatusCodeLib\r
   Tcg2PhysicalPresenceLib\r
+  PeCoffLib\r
 \r
 [Guids]\r
   ## SOMETIMES_CONSUMES     ## Variable:L"SecureBoot"\r