@@ -20,6 +20,8 @@ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
20
20
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
21
21
SPDX-License-Identifier: BSD-2-Clause-Patent
22
22
23
+ Copyright (c) Microsoft Corporation.<BR>
24
+ SPDX-License-Identifier: BSD-2-Clause-Patent
23
25
**/
24
26
25
27
#include <PiDxe.h>
@@ -44,6 +46,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
44
46
#include <Library/HobLib.h>
45
47
#include <Protocol/CcMeasurement.h>
46
48
49
+ #include "DxeTpm2MeasureBootLibSanitization.h"
50
+
47
51
typedef struct {
48
52
EFI_TCG2_PROTOCOL * Tcg2Protocol ;
49
53
EFI_CC_MEASUREMENT_PROTOCOL * CcProtocol ;
@@ -144,10 +148,11 @@ Tcg2MeasureGptTable (
144
148
EFI_TCG2_EVENT * Tcg2Event ;
145
149
EFI_CC_EVENT * CcEvent ;
146
150
EFI_GPT_DATA * GptData ;
147
- UINT32 EventSize ;
151
+ UINT32 TcgEventSize ;
148
152
EFI_TCG2_PROTOCOL * Tcg2Protocol ;
149
153
EFI_CC_MEASUREMENT_PROTOCOL * CcProtocol ;
150
154
EFI_CC_MR_INDEX MrIndex ;
155
+ UINT32 AllocSize ;
151
156
152
157
if (mTcg2MeasureGptCount > 0 ) {
153
158
return EFI_SUCCESS ;
@@ -195,25 +200,22 @@ Tcg2MeasureGptTable (
195
200
BlockIo -> Media -> BlockSize ,
196
201
(UINT8 * )PrimaryHeader
197
202
);
198
- if (EFI_ERROR (Status )) {
199
- DEBUG ((DEBUG_ERROR , "Failed to Read Partition Table Header!\n" ));
203
+ if (EFI_ERROR (Status ) || EFI_ERROR ( SanitizeEfiPartitionTableHeader ( PrimaryHeader , BlockIo )) ) {
204
+ DEBUG ((DEBUG_ERROR , "Failed to read Partition Table Header or invalid Partition Table Header!\n" ));
200
205
FreePool (PrimaryHeader );
201
206
return EFI_DEVICE_ERROR ;
202
207
}
203
208
204
209
//
205
- // PrimaryHeader->SizeOfPartitionEntry should not be zero
210
+ // Read the partition entry.
206
211
//
207
- if (PrimaryHeader -> SizeOfPartitionEntry == 0 ) {
208
- DEBUG (( DEBUG_ERROR , "SizeOfPartitionEntry should not be zero!\n" ));
212
+ Status = SanitizePrimaryHeaderAllocationSize (PrimaryHeader , & AllocSize );
213
+ if ( EFI_ERROR ( Status )) {
209
214
FreePool (PrimaryHeader );
210
215
return EFI_BAD_BUFFER_SIZE ;
211
216
}
212
217
213
- //
214
- // Read the partition entry.
215
- //
216
- EntryPtr = (UINT8 * )AllocatePool (PrimaryHeader -> NumberOfPartitionEntries * PrimaryHeader -> SizeOfPartitionEntry );
218
+ EntryPtr = (UINT8 * )AllocatePool (AllocSize );
217
219
if (EntryPtr == NULL ) {
218
220
FreePool (PrimaryHeader );
219
221
return EFI_OUT_OF_RESOURCES ;
@@ -223,7 +225,7 @@ Tcg2MeasureGptTable (
223
225
DiskIo ,
224
226
BlockIo -> Media -> MediaId ,
225
227
MultU64x32 (PrimaryHeader -> PartitionEntryLBA , BlockIo -> Media -> BlockSize ),
226
- PrimaryHeader -> NumberOfPartitionEntries * PrimaryHeader -> SizeOfPartitionEntry ,
228
+ AllocSize ,
227
229
EntryPtr
228
230
);
229
231
if (EFI_ERROR (Status )) {
@@ -248,16 +250,21 @@ Tcg2MeasureGptTable (
248
250
//
249
251
// Prepare Data for Measurement (CcProtocol and Tcg2Protocol)
250
252
//
251
- EventSize = (UINT32 )(sizeof (EFI_GPT_DATA ) - sizeof (GptData -> Partitions )
252
- + NumberOfPartition * PrimaryHeader -> SizeOfPartitionEntry );
253
- EventPtr = (UINT8 * )AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT ) - sizeof (Tcg2Event -> Event ));
253
+ Status = SanitizePrimaryHeaderGptEventSize (PrimaryHeader , NumberOfPartition , & TcgEventSize );
254
+ if (EFI_ERROR (Status )) {
255
+ FreePool (PrimaryHeader );
256
+ FreePool (EntryPtr );
257
+ return EFI_DEVICE_ERROR ;
258
+ }
259
+
260
+ EventPtr = (UINT8 * )AllocateZeroPool (TcgEventSize );
254
261
if (EventPtr == NULL ) {
255
262
Status = EFI_OUT_OF_RESOURCES ;
256
263
goto Exit ;
257
264
}
258
265
259
266
Tcg2Event = (EFI_TCG2_EVENT * )EventPtr ;
260
- Tcg2Event -> Size = EventSize + sizeof ( EFI_TCG2_EVENT ) - sizeof ( Tcg2Event -> Event ) ;
267
+ Tcg2Event -> Size = TcgEventSize ;
261
268
Tcg2Event -> Header .HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER );
262
269
Tcg2Event -> Header .HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION ;
263
270
Tcg2Event -> Header .PCRIndex = 5 ;
@@ -310,7 +317,7 @@ Tcg2MeasureGptTable (
310
317
CcProtocol ,
311
318
0 ,
312
319
(EFI_PHYSICAL_ADDRESS )(UINTN )(VOID * )GptData ,
313
- (UINT64 )EventSize ,
320
+ (UINT64 )TcgEventSize - OFFSET_OF ( EFI_TCG2_EVENT , Event ) ,
314
321
CcEvent
315
322
);
316
323
if (!EFI_ERROR (Status )) {
@@ -326,7 +333,7 @@ Tcg2MeasureGptTable (
326
333
Tcg2Protocol ,
327
334
0 ,
328
335
(EFI_PHYSICAL_ADDRESS )(UINTN )(VOID * )GptData ,
329
- (UINT64 )EventSize ,
336
+ (UINT64 )TcgEventSize - OFFSET_OF ( EFI_TCG2_EVENT , Event ) ,
330
337
Tcg2Event
331
338
);
332
339
if (!EFI_ERROR (Status )) {
@@ -443,11 +450,13 @@ Tcg2MeasurePeImage (
443
450
Tcg2Event -> Header .PCRIndex = 2 ;
444
451
break ;
445
452
default :
446
- DEBUG ((
447
- DEBUG_ERROR ,
448
- "Tcg2MeasurePeImage: Unknown subsystem type %d" ,
449
- ImageType
450
- ));
453
+ DEBUG (
454
+ (
455
+ DEBUG_ERROR ,
456
+ "Tcg2MeasurePeImage: Unknown subsystem type %d" ,
457
+ ImageType
458
+ )
459
+ );
451
460
goto Finish ;
452
461
}
453
462
@@ -515,7 +524,7 @@ Tcg2MeasurePeImage (
515
524
516
525
@param MeasureBootProtocols Pointer to the located measure boot protocol instances.
517
526
518
- @retval EFI_SUCCESS Sucessfully locate the measure boot protocol instances (at least one instance).
527
+ @retval EFI_SUCCESS Successfully locate the measure boot protocol instances (at least one instance).
519
528
@retval EFI_UNSUPPORTED Measure boot is not supported.
520
529
**/
521
530
EFI_STATUS
@@ -646,12 +655,14 @@ DxeTpm2MeasureBootHandler (
646
655
return EFI_SUCCESS ;
647
656
}
648
657
649
- DEBUG ((
650
- DEBUG_INFO ,
651
- "Tcg2Protocol = %p, CcMeasurementProtocol = %p\n" ,
652
- MeasureBootProtocols .Tcg2Protocol ,
653
- MeasureBootProtocols .CcProtocol
654
- ));
658
+ DEBUG (
659
+ (
660
+ DEBUG_INFO ,
661
+ "Tcg2Protocol = %p, CcMeasurementProtocol = %p\n" ,
662
+ MeasureBootProtocols .Tcg2Protocol ,
663
+ MeasureBootProtocols .CcProtocol
664
+ )
665
+ );
655
666
656
667
//
657
668
// Copy File Device Path
0 commit comments