Skip to content

Commit 2244465

Browse files
Douglas Flick [MSFT]mergify[bot]
Douglas Flick [MSFT]
authored andcommitted
SecurityPkg: DxeTpm2MeasureBootLib: SECURITY PATCH 4117 - CVE 2022-36763
This commit contains the patch files and tests for DxeTpm2MeasureBootLib CVE 2022-36763. Cc: Jiewen Yao <[email protected]> Signed-off-by: Doug Flick [MSFT] <[email protected]>
1 parent a4b8944 commit 2244465

8 files changed

+764
-30
lines changed

SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c

+40-29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
2020
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
2121
SPDX-License-Identifier: BSD-2-Clause-Patent
2222
23+
Copyright (c) Microsoft Corporation.<BR>
24+
SPDX-License-Identifier: BSD-2-Clause-Patent
2325
**/
2426

2527
#include <PiDxe.h>
@@ -44,6 +46,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
4446
#include <Library/HobLib.h>
4547
#include <Protocol/CcMeasurement.h>
4648

49+
#include "DxeTpm2MeasureBootLibSanitization.h"
50+
4751
typedef struct {
4852
EFI_TCG2_PROTOCOL *Tcg2Protocol;
4953
EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol;
@@ -144,10 +148,11 @@ Tcg2MeasureGptTable (
144148
EFI_TCG2_EVENT *Tcg2Event;
145149
EFI_CC_EVENT *CcEvent;
146150
EFI_GPT_DATA *GptData;
147-
UINT32 EventSize;
151+
UINT32 TcgEventSize;
148152
EFI_TCG2_PROTOCOL *Tcg2Protocol;
149153
EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol;
150154
EFI_CC_MR_INDEX MrIndex;
155+
UINT32 AllocSize;
151156

152157
if (mTcg2MeasureGptCount > 0) {
153158
return EFI_SUCCESS;
@@ -195,25 +200,22 @@ Tcg2MeasureGptTable (
195200
BlockIo->Media->BlockSize,
196201
(UINT8 *)PrimaryHeader
197202
);
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"));
200205
FreePool (PrimaryHeader);
201206
return EFI_DEVICE_ERROR;
202207
}
203208

204209
//
205-
// PrimaryHeader->SizeOfPartitionEntry should not be zero
210+
// Read the partition entry.
206211
//
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)) {
209214
FreePool (PrimaryHeader);
210215
return EFI_BAD_BUFFER_SIZE;
211216
}
212217

213-
//
214-
// Read the partition entry.
215-
//
216-
EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);
218+
EntryPtr = (UINT8 *)AllocatePool (AllocSize);
217219
if (EntryPtr == NULL) {
218220
FreePool (PrimaryHeader);
219221
return EFI_OUT_OF_RESOURCES;
@@ -223,7 +225,7 @@ Tcg2MeasureGptTable (
223225
DiskIo,
224226
BlockIo->Media->MediaId,
225227
MultU64x32 (PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),
226-
PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry,
228+
AllocSize,
227229
EntryPtr
228230
);
229231
if (EFI_ERROR (Status)) {
@@ -248,16 +250,21 @@ Tcg2MeasureGptTable (
248250
//
249251
// Prepare Data for Measurement (CcProtocol and Tcg2Protocol)
250252
//
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);
254261
if (EventPtr == NULL) {
255262
Status = EFI_OUT_OF_RESOURCES;
256263
goto Exit;
257264
}
258265

259266
Tcg2Event = (EFI_TCG2_EVENT *)EventPtr;
260-
Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);
267+
Tcg2Event->Size = TcgEventSize;
261268
Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);
262269
Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
263270
Tcg2Event->Header.PCRIndex = 5;
@@ -310,7 +317,7 @@ Tcg2MeasureGptTable (
310317
CcProtocol,
311318
0,
312319
(EFI_PHYSICAL_ADDRESS)(UINTN)(VOID *)GptData,
313-
(UINT64)EventSize,
320+
(UINT64)TcgEventSize - OFFSET_OF (EFI_TCG2_EVENT, Event),
314321
CcEvent
315322
);
316323
if (!EFI_ERROR (Status)) {
@@ -326,7 +333,7 @@ Tcg2MeasureGptTable (
326333
Tcg2Protocol,
327334
0,
328335
(EFI_PHYSICAL_ADDRESS)(UINTN)(VOID *)GptData,
329-
(UINT64)EventSize,
336+
(UINT64)TcgEventSize - OFFSET_OF (EFI_TCG2_EVENT, Event),
330337
Tcg2Event
331338
);
332339
if (!EFI_ERROR (Status)) {
@@ -443,11 +450,13 @@ Tcg2MeasurePeImage (
443450
Tcg2Event->Header.PCRIndex = 2;
444451
break;
445452
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+
);
451460
goto Finish;
452461
}
453462

@@ -515,7 +524,7 @@ Tcg2MeasurePeImage (
515524
516525
@param MeasureBootProtocols Pointer to the located measure boot protocol instances.
517526
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).
519528
@retval EFI_UNSUPPORTED Measure boot is not supported.
520529
**/
521530
EFI_STATUS
@@ -646,12 +655,14 @@ DxeTpm2MeasureBootHandler (
646655
return EFI_SUCCESS;
647656
}
648657

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+
);
655666

656667
//
657668
// Copy File Device Path

SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.inf

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
[Sources]
3939
DxeTpm2MeasureBootLib.c
40+
DxeTpm2MeasureBootLibSanitization.c
41+
DxeTpm2MeasureBootLibSanitization.h
4042

4143
[Packages]
4244
MdePkg/MdePkg.dec
@@ -46,6 +48,7 @@
4648

4749
[LibraryClasses]
4850
BaseMemoryLib
51+
SafeIntLib
4952
DebugLib
5053
MemoryAllocationLib
5154
DevicePathLib
@@ -65,4 +68,3 @@
6568
gEfiFirmwareVolumeBlockProtocolGuid ## SOMETIMES_CONSUMES
6669
gEfiBlockIoProtocolGuid ## SOMETIMES_CONSUMES
6770
gEfiDiskIoProtocolGuid ## SOMETIMES_CONSUMES
68-

0 commit comments

Comments
 (0)