]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg TrEEDxe: Add check for the PE/COFF image.
authorLiming Gao <liming.gao@intel.com>
Wed, 13 Jul 2016 12:28:16 +0000 (20:28 +0800)
committerLiming Gao <liming.gao@intel.com>
Thu, 14 Jul 2016 07:04:53 +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/TrEEDxe/MeasureBootPeCoff.c
SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
SecurityPkg/Tcg/TrEEDxe/TrEEDxe.inf

index e80e02994f8b2510783fef9ddffbb054c6d06656..b20fc70ee0a4b76a01accbdd1598b715b31f7b22 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) 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2013 - 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  mTrEEDxeImageSize = 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
+TrEEDxeImageRead (\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 > mTrEEDxeImageSize) {\r
+    *ReadSize = (UINT32)(mTrEEDxeImageSize - FileOffset);\r
+  }\r
+\r
+  if (FileOffset >= mTrEEDxeImageSize) {\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
+  mTrEEDxeImageSize      = ImageSize;\r
+  ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) TrEEDxeImageRead;\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, "TreeDxe: 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 a30cd5161d40210ac297124bd13be644c5b52ad1..ecafc12efe1c9d3accdf2fe57167049f878eef7f 100644 (file)
@@ -179,6 +179,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 c22e8f00045b57232c4a74bb8a4b68c0ca552966..2dd038aba3f2d9fa4224b6f99953670997ac71ed 100644 (file)
@@ -58,6 +58,7 @@
   HashLib\r
   PerformanceLib\r
   ReportStatusCodeLib\r
+  PeCoffLib\r
 \r
 [Guids]\r
   ## SOMETIMES_CONSUMES     ## Variable:L"SecureBoot"\r