X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FSmmCorePerformanceLib%2FSmmCorePerformanceLib.c;h=e59cc28d537fa8b1db93443a3ec06b6371dc20d6;hp=214d044bcb4dad1c2ebdba74fa0ee582aa39b868;hb=579b5ef204947defbd6fc60c11bdd740ad09d6e9;hpb=b504f51998e839691e0d8c68f3f3093907575594 diff --git a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c index 214d044bcb..e59cc28d53 100644 --- a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c +++ b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c @@ -9,7 +9,14 @@ This library is mainly used by SMM Core to start performance logging to ensure that SMM Performance and PerformanceEx Protocol are installed at the very beginning of SMM phase. -Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.
+ Caution: This module requires additional review when modified. + This driver will have external input - performance data and communicate buffer in SMM mode. + This external input must be validated carefully to avoid security issue like + buffer overflow, integer overflow. + + SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted input and do basic validation. + +Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -44,9 +51,6 @@ BOOLEAN mPerformanceMeasurementEnabled; SPIN_LOCK mSmmPerfLock; -EFI_SMRAM_DESCRIPTOR *mSmramRanges; -UINTN mSmramRangeCount; - // // Interfaces for SMM Performance Protocol. // @@ -199,10 +203,10 @@ StartGaugeEx ( GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle; if (Token != NULL) { - AsciiStrnCpy (GaugeEntryExArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH); + AsciiStrnCpyS (GaugeEntryExArray[Index].Token, SMM_PERFORMANCE_STRING_SIZE, Token, SMM_PERFORMANCE_STRING_LENGTH); } if (Module != NULL) { - AsciiStrnCpy (GaugeEntryExArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH); + AsciiStrnCpyS (GaugeEntryExArray[Index].Module, SMM_PERFORMANCE_STRING_SIZE, Module, SMM_PERFORMANCE_STRING_LENGTH); } GaugeEntryExArray[Index].EndTimeStamp = 0; @@ -443,37 +447,13 @@ GetGauge ( return EFI_SUCCESS; } -/** - This function check if the address is in SMRAM. - - @param Buffer the buffer address to be checked. - @param Length the buffer length to be checked. - - @retval TRUE this address is in SMRAM. - @retval FALSE this address is NOT in SMRAM. -**/ -BOOLEAN -IsAddressInSmram ( - IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length - ) -{ - UINTN Index; - - for (Index = 0; Index < mSmramRangeCount; Index ++) { - if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) || - ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) { - return TRUE; - } - } - - return FALSE; -} - /** Communication service SMI Handler entry. This SMI handler provides services for the performance wrapper driver. + + Caution: This function may receive untrusted input. + Communicate buffer and buffer size are external input, so this function will do basic validation. @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). @param[in] RegisterContext Points to an optional handler context which was specified when the @@ -502,12 +482,33 @@ SmmPerformanceHandlerEx ( EFI_STATUS Status; SMM_PERF_COMMUNICATE_EX *SmmPerfCommData; GAUGE_DATA_ENTRY_EX *GaugeEntryExArray; - UINTN DataSize; + UINT64 DataSize; + UINTN Index; + GAUGE_DATA_ENTRY_EX *GaugeDataEx; + UINTN NumberOfEntries; + UINTN LogEntryKey; + UINTN TempCommBufferSize; GaugeEntryExArray = NULL; - ASSERT (CommBuffer != NULL); + // + // If input is invalid, stop processing this SMI + // + if (CommBuffer == NULL || CommBufferSize == NULL) { + return EFI_SUCCESS; + } + + TempCommBufferSize = *CommBufferSize; + + if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE_EX)) { + return EFI_SUCCESS; + } + if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) { + DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n")); + return EFI_SUCCESS; + } + SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)CommBuffer; switch (SmmPerfCommData->Function) { @@ -517,8 +518,11 @@ SmmPerformanceHandlerEx ( break; case SMM_PERF_FUNCTION_GET_GAUGE_DATA : - if ( SmmPerfCommData->GaugeDataEx == NULL || SmmPerfCommData->NumberOfEntries == 0 || - (SmmPerfCommData->LogEntryKey + SmmPerfCommData->NumberOfEntries) > mGaugeData->NumberOfEntries) { + GaugeDataEx = SmmPerfCommData->GaugeDataEx; + NumberOfEntries = SmmPerfCommData->NumberOfEntries; + LogEntryKey = SmmPerfCommData->LogEntryKey; + if (GaugeDataEx == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries || + NumberOfEntries > mGaugeData->NumberOfEntries || LogEntryKey > (mGaugeData->NumberOfEntries - NumberOfEntries)) { Status = EFI_INVALID_PARAMETER; break; } @@ -526,28 +530,32 @@ SmmPerformanceHandlerEx ( // // Sanity check // - DataSize = SmmPerfCommData->NumberOfEntries * sizeof(GAUGE_DATA_ENTRY_EX); - if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmPerfCommData->GaugeDataEx, DataSize)) { - DEBUG ((EFI_D_ERROR, "Smm Performance Data buffer is in SMRAM!\n")); + DataSize = MultU64x32 (NumberOfEntries, sizeof(GAUGE_DATA_ENTRY_EX)); + if (!SmmIsBufferOutsideSmmValid ((UINTN) GaugeDataEx, DataSize)) { + DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n")); Status = EFI_ACCESS_DENIED; - break ; + break; } GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1); - CopyMem( - (UINT8 *) (SmmPerfCommData->GaugeDataEx), - (UINT8 *) &GaugeEntryExArray[SmmPerfCommData->LogEntryKey], - DataSize - ); + + for (Index = 0; Index < NumberOfEntries; Index++) { + CopyMem ( + (UINT8 *) &GaugeDataEx[Index], + (UINT8 *) &GaugeEntryExArray[LogEntryKey++], + sizeof (GAUGE_DATA_ENTRY_EX) + ); + } Status = EFI_SUCCESS; break; default: - ASSERT (FALSE); Status = EFI_UNSUPPORTED; } + SmmPerfCommData->ReturnStatus = Status; + return EFI_SUCCESS; } @@ -556,6 +564,9 @@ SmmPerformanceHandlerEx ( This SMI handler provides services for the performance wrapper driver. + Caution: This function may receive untrusted input. + Communicate buffer and buffer size are external input, so this function will do basic validation. + @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). @param[in] RegisterContext Points to an optional handler context which was specified when the handler was registered. @@ -583,13 +594,32 @@ SmmPerformanceHandler ( EFI_STATUS Status; SMM_PERF_COMMUNICATE *SmmPerfCommData; GAUGE_DATA_ENTRY_EX *GaugeEntryExArray; - UINTN DataSize; + UINT64 DataSize; UINTN Index; + GAUGE_DATA_ENTRY *GaugeData; + UINTN NumberOfEntries; UINTN LogEntryKey; + UINTN TempCommBufferSize; GaugeEntryExArray = NULL; - ASSERT (CommBuffer != NULL); + // + // If input is invalid, stop processing this SMI + // + if (CommBuffer == NULL || CommBufferSize == NULL) { + return EFI_SUCCESS; + } + + TempCommBufferSize = *CommBufferSize; + + if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE)) { + return EFI_SUCCESS; + } + + if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) { + DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n")); + return EFI_SUCCESS; + } SmmPerfCommData = (SMM_PERF_COMMUNICATE *)CommBuffer; @@ -600,8 +630,11 @@ SmmPerformanceHandler ( break; case SMM_PERF_FUNCTION_GET_GAUGE_DATA : - if ( SmmPerfCommData->GaugeData == NULL || SmmPerfCommData->NumberOfEntries == 0 || - (SmmPerfCommData->LogEntryKey + SmmPerfCommData->NumberOfEntries) > mGaugeData->NumberOfEntries) { + GaugeData = SmmPerfCommData->GaugeData; + NumberOfEntries = SmmPerfCommData->NumberOfEntries; + LogEntryKey = SmmPerfCommData->LogEntryKey; + if (GaugeData == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries || + NumberOfEntries > mGaugeData->NumberOfEntries || LogEntryKey > (mGaugeData->NumberOfEntries - NumberOfEntries)) { Status = EFI_INVALID_PARAMETER; break; } @@ -609,19 +642,18 @@ SmmPerformanceHandler ( // // Sanity check // - DataSize = SmmPerfCommData->NumberOfEntries * sizeof(GAUGE_DATA_ENTRY); - if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmPerfCommData->GaugeData, DataSize)) { - DEBUG ((EFI_D_ERROR, "Smm Performance Data buffer is in SMRAM!\n")); + DataSize = MultU64x32 (NumberOfEntries, sizeof(GAUGE_DATA_ENTRY)); + if (!SmmIsBufferOutsideSmmValid ((UINTN) GaugeData, DataSize)) { + DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n")); Status = EFI_ACCESS_DENIED; - break ; + break; } GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1); - LogEntryKey = SmmPerfCommData->LogEntryKey; - for (Index = 0; Index < SmmPerfCommData->NumberOfEntries; Index++) { - CopyMem( - (UINT8 *) &(SmmPerfCommData->GaugeData[Index]), + for (Index = 0; Index < NumberOfEntries; Index++) { + CopyMem ( + (UINT8 *) &GaugeData[Index], (UINT8 *) &GaugeEntryExArray[LogEntryKey++], sizeof (GAUGE_DATA_ENTRY) ); @@ -630,11 +662,12 @@ SmmPerformanceHandler ( break; default: - ASSERT (FALSE); Status = EFI_UNSUPPORTED; } + SmmPerfCommData->ReturnStatus = Status; + return EFI_SUCCESS; } @@ -655,9 +688,6 @@ InitializeSmmCorePerformanceLib ( { EFI_STATUS Status; EFI_HANDLE Handle; - EFI_SMM_ACCESS2_PROTOCOL *SmmAccess; - UINTN Size; - // // Initialize spin lock @@ -669,28 +699,6 @@ InitializeSmmCorePerformanceLib ( mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords)); ASSERT (mGaugeData != NULL); - // - // Get SMRAM information - // - Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess); - ASSERT_EFI_ERROR (Status); - - Size = 0; - Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL); - ASSERT (Status == EFI_BUFFER_TOO_SMALL); - - Status = gSmst->SmmAllocatePool ( - EfiRuntimeServicesData, - Size, - (VOID **)&mSmramRanges - ); - ASSERT_EFI_ERROR (Status); - - Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges); - ASSERT_EFI_ERROR (Status); - - mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR); - // // Install the protocol interfaces. //