@@ -72,10 +72,10 @@ TestSanitizeEfiPartitionTableHeader (
72
72
PrimaryHeader .Header .Revision = DEFAULT_PRIMARY_TABLE_HEADER_REVISION ;
73
73
PrimaryHeader .Header .HeaderSize = sizeof (EFI_PARTITION_TABLE_HEADER );
74
74
PrimaryHeader .MyLBA = 1 ;
75
- PrimaryHeader .AlternateLBA = 2 ;
76
- PrimaryHeader .FirstUsableLBA = 3 ;
77
- PrimaryHeader .LastUsableLBA = 4 ;
78
- PrimaryHeader .PartitionEntryLBA = 5 ;
75
+ PrimaryHeader .PartitionEntryLBA = 2 ;
76
+ PrimaryHeader .AlternateLBA = 3 ;
77
+ PrimaryHeader .FirstUsableLBA = 4 ;
78
+ PrimaryHeader .LastUsableLBA = 5 ;
79
79
PrimaryHeader .NumberOfPartitionEntries = DEFAULT_PRIMARY_TABLE_HEADER_NUMBER_OF_PARTITION_ENTRIES ;
80
80
PrimaryHeader .SizeOfPartitionEntry = DEFAULT_PRIMARY_TABLE_HEADER_SIZE_OF_PARTITION_ENTRY ;
81
81
PrimaryHeader .PartitionEntryArrayCRC32 = 0 ; // Purposely invalid
@@ -187,11 +187,6 @@ TestSanitizePrimaryHeaderGptEventSize (
187
187
EFI_STATUS Status ;
188
188
EFI_PARTITION_TABLE_HEADER PrimaryHeader ;
189
189
UINTN NumberOfPartition ;
190
- EFI_GPT_DATA * GptData ;
191
- EFI_TCG2_EVENT * Tcg2Event ;
192
-
193
- Tcg2Event = NULL ;
194
- GptData = NULL ;
195
190
196
191
// Test that a normal PrimaryHeader passes validation
197
192
PrimaryHeader .NumberOfPartitionEntries = 5 ;
@@ -225,6 +220,52 @@ TestSanitizePrimaryHeaderGptEventSize (
225
220
return UNIT_TEST_PASSED ;
226
221
}
227
222
223
+ /**
224
+ This function tests the SanitizePeImageEventSize function.
225
+ It's intent is to test that the untrusted input from a file path when generating a
226
+ EFI_IMAGE_LOAD_EVENT structure will not cause an overflow when calculating
227
+ the event size when allocating space
228
+
229
+ @param[in] Context The unit test context.
230
+
231
+ @retval UNIT_TEST_PASSED The test passed.
232
+ @retval UNIT_TEST_ERROR_TEST_FAILED The test failed.
233
+ **/
234
+ UNIT_TEST_STATUS
235
+ EFIAPI
236
+ TestSanitizePeImageEventSize (
237
+ IN UNIT_TEST_CONTEXT Context
238
+ )
239
+ {
240
+ UINT32 EventSize ;
241
+ UINTN ExistingLogicEventSize ;
242
+ UINT32 FilePathSize ;
243
+ EFI_STATUS Status ;
244
+
245
+ FilePathSize = 255 ;
246
+
247
+ // Test that a normal PE image passes validation
248
+ Status = SanitizePeImageEventSize (FilePathSize , & EventSize );
249
+ UT_ASSERT_EQUAL (Status , EFI_SUCCESS );
250
+
251
+ // Test that the event size is correct compared to the existing logic
252
+ ExistingLogicEventSize = OFFSET_OF (EFI_IMAGE_LOAD_EVENT , DevicePath ) + FilePathSize ;
253
+ ExistingLogicEventSize += OFFSET_OF (EFI_TCG2_EVENT , Event );
254
+
255
+ if (EventSize != ExistingLogicEventSize ) {
256
+ UT_LOG_ERROR ("SanitizePeImageEventSize returned an incorrect event size. Expected %u, got %u\n" , ExistingLogicEventSize , EventSize );
257
+ return UNIT_TEST_ERROR_TEST_FAILED ;
258
+ }
259
+
260
+ // Test that the event size may not overflow
261
+ Status = SanitizePeImageEventSize (MAX_UINT32 , & EventSize );
262
+ UT_ASSERT_EQUAL (Status , EFI_BAD_BUFFER_SIZE );
263
+
264
+ DEBUG ((DEBUG_INFO , "%a: Test passed\n" , __func__ ));
265
+
266
+ return UNIT_TEST_PASSED ;
267
+ }
268
+
228
269
// *--------------------------------------------------------------------*
229
270
// * Unit Test Code Main Function
230
271
// *--------------------------------------------------------------------*
@@ -267,6 +308,7 @@ UefiTestMain (
267
308
AddTestCase (Tcg2MeasureBootLibValidationTestSuite , "Tests Validating EFI Partition Table" , "Common.Tcg2MeasureBootLibValidation" , TestSanitizeEfiPartitionTableHeader , NULL , NULL , NULL );
268
309
AddTestCase (Tcg2MeasureBootLibValidationTestSuite , "Tests Primary header gpt event checks for overflow" , "Common.Tcg2MeasureBootLibValidation" , TestSanitizePrimaryHeaderAllocationSize , NULL , NULL , NULL );
269
310
AddTestCase (Tcg2MeasureBootLibValidationTestSuite , "Tests Primary header allocation size checks for overflow" , "Common.Tcg2MeasureBootLibValidation" , TestSanitizePrimaryHeaderGptEventSize , NULL , NULL , NULL );
311
+ AddTestCase (Tcg2MeasureBootLibValidationTestSuite , "Tests PE Image and FileSize checks for overflow" , "Common.Tcg2MeasureBootLibValidation" , TestSanitizePeImageEventSize , NULL , NULL , NULL );
270
312
271
313
Status = RunAllTestSuites (Framework );
272
314
0 commit comments