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, 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>
33 Measure PE image into TPM log based on the authenticode image hashing in
34 PE/COFF Specification 8.0 Appendix A.
36 Caution: This function may receive untrusted input.
37 PE/COFF image is external input, so this function will validate its data structure
38 within this image buffer before use.
40 @param[in] PCRIndex TPM PCR index
41 @param[in] ImageAddress Start address of image buffer.
42 @param[in] ImageSize Image size
43 @param[out] DigestList Digeest list of this image.
45 @retval EFI_SUCCESS Successfully measure image.
46 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
47 @retval other error value
50 MeasurePeImageAndExtend (
52 IN EFI_PHYSICAL_ADDRESS ImageAddress
,
54 OUT TPML_DIGEST_VALUES
*DigestList
58 EFI_IMAGE_DOS_HEADER
*DosHdr
;
59 UINT32 PeCoffHeaderOffset
;
60 EFI_IMAGE_SECTION_HEADER
*Section
;
63 UINTN SumOfBytesHashed
;
64 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
68 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
69 UINT32 NumberOfRvaAndSizes
;
71 HASH_HANDLE HashHandle
;
73 HashHandle
= 0xFFFFFFFF; // Know bad value
75 Status
= EFI_UNSUPPORTED
;
79 // Check PE/COFF image
81 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
82 PeCoffHeaderOffset
= 0;
83 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
84 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
87 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
88 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
89 Status
= EFI_UNSUPPORTED
;
94 // PE/COFF Image Measurement
96 // NOTE: The following codes/steps are based upon the authenticode image hashing in
97 // PE/COFF Specification 8.0 Appendix A.
101 // 1. Load the image header into memory.
103 // 2. Initialize a SHA hash context.
105 Status
= HashStart (&HashHandle
);
106 if (EFI_ERROR (Status
)) {
111 // Measuring PE/COFF Image Header;
112 // But CheckSum field and SECURITY data directory (certificate) are excluded
114 if (Hdr
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
116 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
117 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
118 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
119 // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
121 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
124 // Get the magic value from the PE/COFF Optional Header
126 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
130 // 3. Calculate the distance from the base of the image header to the image checksum address.
131 // 4. Hash the image header from its base to beginning of the image checksum.
133 HashBase
= (UINT8
*) (UINTN
) ImageAddress
;
134 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
138 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
139 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
144 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
145 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
148 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
149 if (EFI_ERROR (Status
)) {
154 // 5. Skip over the image checksum (it occupies a single ULONG).
156 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
158 // 6. Since there is no Cert Directory in optional header, hash everything
159 // from the end of the checksum to the end of image header.
161 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
165 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
166 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
171 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
172 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
176 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
177 if (EFI_ERROR (Status
)) {
183 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
185 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
189 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
190 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
195 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
196 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
200 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
201 if (EFI_ERROR (Status
)) {
207 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
208 // 9. Hash everything from the end of the Cert Directory to the end of image header.
210 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
214 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
215 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
220 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
221 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
225 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
226 if (EFI_ERROR (Status
)) {
233 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header
235 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
239 SumOfBytesHashed
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
244 SumOfBytesHashed
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
248 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
249 // structures in the image. The 'NumberOfSections' field of the image
250 // header indicates how big the table should be. Do not include any
251 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
253 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * Hdr
.Pe32
->FileHeader
.NumberOfSections
);
254 if (SectionHeader
== NULL
) {
255 Status
= EFI_OUT_OF_RESOURCES
;
260 // 12. Using the 'PointerToRawData' in the referenced section headers as
261 // a key, arrange the elements in the table in ascending order. In other
262 // words, sort the section headers according to the disk-file offset of
265 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
266 (UINT8
*) (UINTN
) ImageAddress
+
269 sizeof(EFI_IMAGE_FILE_HEADER
) +
270 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
272 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
274 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
275 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof(EFI_IMAGE_SECTION_HEADER
));
278 CopyMem (&SectionHeader
[Pos
], Section
, sizeof(EFI_IMAGE_SECTION_HEADER
));
283 // 13. Walk through the sorted table, bring the corresponding section
284 // into memory, and hash the entire section (using the 'SizeOfRawData'
285 // field in the section header to determine the amount of data to hash).
286 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
287 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
289 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
290 Section
= (EFI_IMAGE_SECTION_HEADER
*) &SectionHeader
[Index
];
291 if (Section
->SizeOfRawData
== 0) {
294 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ Section
->PointerToRawData
;
295 HashSize
= (UINTN
) Section
->SizeOfRawData
;
297 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
298 if (EFI_ERROR (Status
)) {
302 SumOfBytesHashed
+= HashSize
;
306 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
307 // data in the file that needs to be added to the hash. This data begins
308 // at file offset SUM_OF_BYTES_HASHED and its length is:
309 // FileSize - (CertDirectory->Size)
311 if (ImageSize
> SumOfBytesHashed
) {
312 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ SumOfBytesHashed
;
314 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
317 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
321 CertSize
= Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
326 CertSize
= Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
330 if (ImageSize
> CertSize
+ SumOfBytesHashed
) {
331 HashSize
= (UINTN
) (ImageSize
- CertSize
- SumOfBytesHashed
);
333 Status
= HashUpdate (HashHandle
, HashBase
, HashSize
);
334 if (EFI_ERROR (Status
)) {
337 } else if (ImageSize
< CertSize
+ SumOfBytesHashed
) {
338 Status
= EFI_UNSUPPORTED
;
344 // 17. Finalize the SHA hash.
346 Status
= HashCompleteAndExtend (HashHandle
, PCRIndex
, NULL
, 0, DigestList
);
347 if (EFI_ERROR (Status
)) {
352 if (SectionHeader
!= NULL
) {
353 FreePool (SectionHeader
);