|
1 | 1 | /** @file
|
2 |
| -This file includes the unit test cases for the DxeTpmMeasureBootLibSanitizationTest.c. |
| 2 | + This file includes the unit test cases for the DxeTpmMeasureBootLibSanitizationTest.c. |
3 | 3 |
|
4 |
| -Copyright (c) Microsoft Corporation.<BR> |
5 |
| -SPDX-License-Identifier: BSD-2-Clause-Patent |
| 4 | + Copyright (c) Microsoft Corporation.<BR> |
| 5 | + SPDX-License-Identifier: BSD-2-Clause-Patent |
6 | 6 | **/
|
7 | 7 |
|
8 | 8 | #include <Uefi.h>
|
@@ -186,9 +186,6 @@ TestSanitizePrimaryHeaderGptEventSize (
|
186 | 186 | EFI_STATUS Status;
|
187 | 187 | EFI_PARTITION_TABLE_HEADER PrimaryHeader;
|
188 | 188 | UINTN NumberOfPartition;
|
189 |
| - EFI_GPT_DATA *GptData; |
190 |
| - |
191 |
| - GptData = NULL; |
192 | 189 |
|
193 | 190 | // Test that a normal PrimaryHeader passes validation
|
194 | 191 | PrimaryHeader.NumberOfPartitionEntries = 5;
|
@@ -222,6 +219,94 @@ TestSanitizePrimaryHeaderGptEventSize (
|
222 | 219 | return UNIT_TEST_PASSED;
|
223 | 220 | }
|
224 | 221 |
|
| 222 | +/** |
| 223 | + This function tests the SanitizePeImageEventSize function. |
| 224 | + It's intent is to test that the untrusted input from a file path for an |
| 225 | + EFI_IMAGE_LOAD_EVENT structure will not cause an overflow when calculating |
| 226 | + the event size when allocating space. |
| 227 | +
|
| 228 | + @param[in] Context The unit test context. |
| 229 | +
|
| 230 | + @retval UNIT_TEST_PASSED The test passed. |
| 231 | + @retval UNIT_TEST_ERROR_TEST_FAILED The test failed. |
| 232 | +**/ |
| 233 | +UNIT_TEST_STATUS |
| 234 | +EFIAPI |
| 235 | +TestSanitizePeImageEventSize ( |
| 236 | + IN UNIT_TEST_CONTEXT Context |
| 237 | + ) |
| 238 | +{ |
| 239 | + UINT32 EventSize; |
| 240 | + UINTN ExistingLogicEventSize; |
| 241 | + UINT32 FilePathSize; |
| 242 | + EFI_STATUS Status; |
| 243 | + EFI_DEVICE_PATH_PROTOCOL DevicePath; |
| 244 | + EFI_IMAGE_LOAD_EVENT *ImageLoadEvent; |
| 245 | + UNIT_TEST_STATUS TestStatus; |
| 246 | + |
| 247 | + TestStatus = UNIT_TEST_ERROR_TEST_FAILED; |
| 248 | + |
| 249 | + // Generate EFI_DEVICE_PATH_PROTOCOL test data |
| 250 | + DevicePath.Type = 0; |
| 251 | + DevicePath.SubType = 0; |
| 252 | + DevicePath.Length[0] = 0; |
| 253 | + DevicePath.Length[1] = 0; |
| 254 | + |
| 255 | + // Generate EFI_IMAGE_LOAD_EVENT test data |
| 256 | + ImageLoadEvent = AllocateZeroPool (sizeof (EFI_IMAGE_LOAD_EVENT) + sizeof (EFI_DEVICE_PATH_PROTOCOL)); |
| 257 | + if (ImageLoadEvent == NULL) { |
| 258 | + DEBUG ((DEBUG_ERROR, "%a: AllocateZeroPool failed\n", __func__)); |
| 259 | + goto Exit; |
| 260 | + } |
| 261 | + |
| 262 | + // Populate EFI_IMAGE_LOAD_EVENT54 test data |
| 263 | + ImageLoadEvent->ImageLocationInMemory = (EFI_PHYSICAL_ADDRESS)0x12345678; |
| 264 | + ImageLoadEvent->ImageLengthInMemory = 0x1000; |
| 265 | + ImageLoadEvent->ImageLinkTimeAddress = (UINTN)ImageLoadEvent; |
| 266 | + ImageLoadEvent->LengthOfDevicePath = sizeof (EFI_DEVICE_PATH_PROTOCOL); |
| 267 | + CopyMem (ImageLoadEvent->DevicePath, &DevicePath, sizeof (EFI_DEVICE_PATH_PROTOCOL)); |
| 268 | + |
| 269 | + FilePathSize = 255; |
| 270 | + |
| 271 | + // Test that a normal PE image passes validation |
| 272 | + Status = SanitizePeImageEventSize (FilePathSize, &EventSize); |
| 273 | + if (EFI_ERROR (Status)) { |
| 274 | + UT_LOG_ERROR ("SanitizePeImageEventSize failed with %r\n", Status); |
| 275 | + goto Exit; |
| 276 | + } |
| 277 | + |
| 278 | + // Test that the event size is correct compared to the existing logic |
| 279 | + ExistingLogicEventSize = OFFSET_OF (EFI_IMAGE_LOAD_EVENT, DevicePath) + FilePathSize; |
| 280 | + ExistingLogicEventSize += sizeof (TCG_PCR_EVENT_HDR); |
| 281 | + |
| 282 | + if (EventSize != ExistingLogicEventSize) { |
| 283 | + UT_LOG_ERROR ("SanitizePeImageEventSize returned an incorrect event size. Expected %u, got %u\n", ExistingLogicEventSize, EventSize); |
| 284 | + goto Exit; |
| 285 | + } |
| 286 | + |
| 287 | + // Test that the event size may not overflow |
| 288 | + Status = SanitizePeImageEventSize (MAX_UINT32, &EventSize); |
| 289 | + if (Status != EFI_BAD_BUFFER_SIZE) { |
| 290 | + UT_LOG_ERROR ("SanitizePeImageEventSize succeded when it was supposed to fail with %r\n", Status); |
| 291 | + goto Exit; |
| 292 | + } |
| 293 | + |
| 294 | + TestStatus = UNIT_TEST_PASSED; |
| 295 | +Exit: |
| 296 | + |
| 297 | + if (ImageLoadEvent != NULL) { |
| 298 | + FreePool (ImageLoadEvent); |
| 299 | + } |
| 300 | + |
| 301 | + if (TestStatus == UNIT_TEST_ERROR_TEST_FAILED) { |
| 302 | + DEBUG ((DEBUG_ERROR, "%a: Test failed\n", __func__)); |
| 303 | + } else { |
| 304 | + DEBUG ((DEBUG_INFO, "%a: Test passed\n", __func__)); |
| 305 | + } |
| 306 | + |
| 307 | + return TestStatus; |
| 308 | +} |
| 309 | + |
225 | 310 | // *--------------------------------------------------------------------*
|
226 | 311 | // * Unit Test Code Main Function
|
227 | 312 | // *--------------------------------------------------------------------*
|
@@ -265,6 +350,7 @@ UefiTestMain (
|
265 | 350 | AddTestCase (TcgMeasureBootLibValidationTestSuite, "Tests Validating EFI Partition Table", "Common.TcgMeasureBootLibValidation", TestSanitizeEfiPartitionTableHeader, NULL, NULL, NULL);
|
266 | 351 | AddTestCase (TcgMeasureBootLibValidationTestSuite, "Tests Primary header gpt event checks for overflow", "Common.TcgMeasureBootLibValidation", TestSanitizePrimaryHeaderAllocationSize, NULL, NULL, NULL);
|
267 | 352 | AddTestCase (TcgMeasureBootLibValidationTestSuite, "Tests Primary header allocation size checks for overflow", "Common.TcgMeasureBootLibValidation", TestSanitizePrimaryHeaderGptEventSize, NULL, NULL, NULL);
|
| 353 | + AddTestCase (TcgMeasureBootLibValidationTestSuite, "Tests PE Image and FileSize checks for overflow", "Common.TcgMeasureBootLibValidation", TestSanitizePeImageEventSize, NULL, NULL, NULL); |
268 | 354 |
|
269 | 355 | Status = RunAllTestSuites (Framework);
|
270 | 356 |
|
|
0 commit comments