2 This module implements measuring PeCoff image for Tcg2 Protocol.
4 Caution: This file requires additional review when modified.
5 This driver will have external input - PE/COFF image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Library/BaseLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/PeCoffLib.h>
29 #include <Library/Tpm2CommandLib.h>
30 #include <Library/HashLib.h>
32 UINTN mTcg2DxeImageSize
= 0;
35 Reads contents of a PE/COFF image in memory buffer.
37 Caution: This function may receive untrusted input.
38 PE/COFF image is external input, so this function will make sure the PE/COFF image content
39 read is within the image buffer.
41 @param FileHandle Pointer to the file handle to read the PE/COFF image.
42 @param FileOffset Offset into the PE/COFF image to begin the read operation.
43 @param ReadSize On input, the size in bytes of the requested read operation.
44 On output, the number of bytes actually read.
45 @param Buffer Output buffer that contains the data read from the PE/COFF image.
47 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
54 IN OUT UINTN
*ReadSize
,
60 if (FileHandle
== NULL
|| ReadSize
== NULL
|| Buffer
== NULL
) {
61 return EFI_INVALID_PARAMETER
;
64 if (MAX_ADDRESS
- FileOffset
< *ReadSize
) {
65 return EFI_INVALID_PARAMETER
;
68 EndPosition
= FileOffset
+ *ReadSize
;
69 if (EndPosition
> mTcg2DxeImageSize
) {
70 *ReadSize
= (UINT32
)(mTcg2DxeImageSize
- FileOffset
);
73 if (FileOffset
>= mTcg2DxeImageSize
) {
77 CopyMem (Buffer
, (UINT8
*)((UINTN
) FileHandle
+ FileOffset
), *ReadSize
);
83 Measure PE image into TPM log based on the authenticode image hashing in
84 PE/COFF Specification 8.0 Appendix A.
86 Caution: This function may receive untrusted input.
87 PE/COFF image is external input, so this function will validate its data structure
88 within this image buffer before use.
90 Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
92 @param[in] PCRIndex TPM PCR index
93 @param[in] ImageAddress Start address of image buffer.
94 @param[in] ImageSize Image size
95 @param[out] DigestList Digeest list of this image.
97 @retval EFI_SUCCESS Successfully measure image.
98 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
99 @retval other error value
102 MeasurePeImageAndExtend (
104 IN EFI_PHYSICAL_ADDRESS ImageAddress
,
106 OUT TPML_DIGEST_VALUES
*DigestList
110 EFI_IMAGE_DOS_HEADER
*DosHdr
;
111 UINT32 PeCoffHeaderOffset
;
112 EFI_IMAGE_SECTION_HEADER
*Section
;
115 UINTN SumOfBytesHashed
;
116 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
120 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
121 UINT32 NumberOfRvaAndSizes
;
123 HASH_HANDLE HashHandle
;
124 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
126 HashHandle
= 0xFFFFFFFF; // Know bad value
128 Status
= EFI_UNSUPPORTED
;
129 SectionHeader
= NULL
;
132 // Check PE/COFF image
134 ZeroMem (&ImageContext
, sizeof (ImageContext
));
135 ImageContext
.Handle
= (VOID
*) (UINTN
) ImageAddress
;
136 mTcg2DxeImageSize
= ImageSize
;
137 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) Tcg2DxeImageRead
;
140 // Get information about the image being loaded
142 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
143 if (EFI_ERROR (Status
)) {
145 // The information can't be got from the invalid PeImage
147 DEBUG ((DEBUG_INFO
, "Tcg2Dxe: PeImage invalid. Cannot retrieve image information.\n"));
151 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
152 PeCoffHeaderOffset
= 0;
153 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
154 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
157 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
158 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
159 Status
= EFI_UNSUPPORTED
;
164 // PE/COFF Image Measurement
166 // NOTE: The following codes/steps are based upon the authenticode image hashing in
167 // PE/COFF Specification 8.0 Appendix A.
171 // 1. Load the image header into memory.
173 // 2. Initialize a SHA hash context.
175 Status
= HashStart (&HashHandle
);
176 if (EFI_ERROR (Status
)) {
181 // Measuring PE/COFF Image Header;
182 // But CheckSum field and SECURITY data directory (certificate) are excluded
184 if (Hdr
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
186 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
187 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
188 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
189 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
191 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
194 // Get the magic value from the PE/COFF Optional Header
196 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
200 // 3. Calculate the distance from the base of the image header to the image checksum address.
201 // 4. Hash the image header from its base to beginning of the image checksum.
203 HashBase
= (UINT8
*) (UINTN
) ImageAddress
;
204 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
208 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
209 HashSize
= (UINTN
) (&Hdr
.Pe32
->OptionalHeader
.CheckSum
) - (UINTN
) HashBase
;
214 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
215 HashSize
= (UINTN
) (&Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
) - (UINTN
) HashBase
;
218 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
219 if (EFI_ERROR (Status
)) {
224 // 5. Skip over the image checksum (it occupies a single ULONG).
226 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
228 // 6. Since there is no Cert Directory in optional header, hash everything
229 // from the end of the checksum to the end of image header.
231 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
235 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
236 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
241 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
242 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
246 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
247 if (EFI_ERROR (Status
)) {
253 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
255 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
259 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
260 HashSize
= (UINTN
) (&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - (UINTN
) HashBase
;
265 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
266 HashSize
= (UINTN
) (&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - (UINTN
) HashBase
;
270 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
271 if (EFI_ERROR (Status
)) {
277 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
278 // 9. Hash everything from the end of the Cert Directory to the end of image header.
280 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
284 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
285 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
290 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
291 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
295 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
296 if (EFI_ERROR (Status
)) {
303 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header
305 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
309 SumOfBytesHashed
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
314 SumOfBytesHashed
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
318 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
319 // structures in the image. The 'NumberOfSections' field of the image
320 // header indicates how big the table should be. Do not include any
321 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
323 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * Hdr
.Pe32
->FileHeader
.NumberOfSections
);
324 if (SectionHeader
== NULL
) {
325 Status
= EFI_OUT_OF_RESOURCES
;
330 // 12. Using the 'PointerToRawData' in the referenced section headers as
331 // a key, arrange the elements in the table in ascending order. In other
332 // words, sort the section headers according to the disk-file offset of
335 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
336 (UINT8
*) (UINTN
) ImageAddress
+
339 sizeof(EFI_IMAGE_FILE_HEADER
) +
340 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
342 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
344 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
345 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof(EFI_IMAGE_SECTION_HEADER
));
348 CopyMem (&SectionHeader
[Pos
], Section
, sizeof(EFI_IMAGE_SECTION_HEADER
));
353 // 13. Walk through the sorted table, bring the corresponding section
354 // into memory, and hash the entire section (using the 'SizeOfRawData'
355 // field in the section header to determine the amount of data to hash).
356 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
357 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
359 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
360 Section
= (EFI_IMAGE_SECTION_HEADER
*) &SectionHeader
[Index
];
361 if (Section
->SizeOfRawData
== 0) {
364 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ Section
->PointerToRawData
;
365 HashSize
= (UINTN
) Section
->SizeOfRawData
;
367 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
368 if (EFI_ERROR (Status
)) {
372 SumOfBytesHashed
+= HashSize
;
376 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
377 // data in the file that needs to be added to the hash. This data begins
378 // at file offset SUM_OF_BYTES_HASHED and its length is:
379 // FileSize - (CertDirectory->Size)
381 if (ImageSize
> SumOfBytesHashed
) {
382 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ SumOfBytesHashed
;
384 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
387 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
391 CertSize
= Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
396 CertSize
= Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
400 if (ImageSize
> CertSize
+ SumOfBytesHashed
) {
401 HashSize
= (UINTN
) (ImageSize
- CertSize
- SumOfBytesHashed
);
403 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
404 if (EFI_ERROR (Status
)) {
407 } else if (ImageSize
< CertSize
+ SumOfBytesHashed
) {
408 Status
= EFI_UNSUPPORTED
;
414 // 17. Finalize the SHA hash.
416 Status
= HashCompleteAndExtend (HashHandle
, PCRIndex
, NULL
, 0, DigestList
);
417 if (EFI_ERROR (Status
)) {
422 if (SectionHeader
!= NULL
) {
423 FreePool (SectionHeader
);