X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FVariable%2FRuntimeDxe%2FVariableSmm.c;h=e495d971a08b1d8df69fddf05d92c9e80d9e40fd;hp=7541f6ae1d2606144c1b4778f48469df162a4637;hb=09808bd39b0c76559354253639766458ec24da79;hpb=d17c4eac5625e40fa3715aee14286f45797b3999 diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index 7541f6ae1d..e495d971a0 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -1,7 +1,6 @@ /** @file - - The sample implementation for SMM variable protocol. And this driver - implements an SMI handler to communicate with the DXE runtime driver + The sample implementation for SMM variable protocol. And this driver + implements an SMI handler to communicate with the DXE runtime driver to provide variable services. Caution: This module requires additional review when modified. @@ -11,51 +10,118 @@ SmmVariableHandler() will receive untrusted input and do basic validation. - Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), - VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), + Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), + VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), SmmVariableGetStatistics() should also do validation based on its own knowledge. -Copyright (c) 2010 - 2012, 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 +Copyright (c) 2010 - 2016, 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 http://opensource.org/licenses/bsd-license.php -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ + #include #include #include -#include +#include +#include #include +#include -#include #include #include "Variable.h" -EFI_SMRAM_DESCRIPTOR *mSmramRanges; -UINTN mSmramRangeCount; - extern VARIABLE_INFO_ENTRY *gVariableInfo; EFI_HANDLE mSmmVariableHandle = NULL; EFI_HANDLE mVariableHandle = NULL; BOOLEAN mAtRuntime = FALSE; -EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; - +UINT8 *mVariableBufferPayload = NULL; +UINTN mVariableBufferPayloadSize; +extern BOOLEAN mEndOfDxe; +extern VAR_CHECK_REQUEST_SOURCE mRequestSource; + +/** + SecureBoot Hook for SetVariable. + + @param[in] VariableName Name of Variable to be found. + @param[in] VendorGuid Variable vendor GUID. + +**/ +VOID +EFIAPI +SecureBootHook ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + return ; +} + +/** + + This code sets variable in storage blocks (Volatile or Non-Volatile). + + @param VariableName Name of Variable to be found. + @param VendorGuid Variable vendor GUID. + @param Attributes Attribute value of the variable found + @param DataSize Size of Data found. If size is less than the + data, this value contains the required size. + @param Data Data pointer. + + @return EFI_INVALID_PARAMETER Invalid parameter. + @return EFI_SUCCESS Set successfully. + @return EFI_OUT_OF_RESOURCES Resource not enough to set variable. + @return EFI_NOT_FOUND Not found. + @return EFI_WRITE_PROTECTED Variable is read-only. + +**/ +EFI_STATUS +EFIAPI +SmmVariableSetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_STATUS Status; + + // + // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL. + // + mRequestSource = VarCheckFromTrusted; + Status = VariableServiceSetVariable ( + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + mRequestSource = VarCheckFromUntrusted; + return Status; +} + EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = { VariableServiceGetVariable, VariableServiceGetNextVariableName, - VariableServiceSetVariable, + SmmVariableSetVariable, VariableServiceQueryVariableInfo }; +EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck = { VarCheckRegisterSetVariableCheckHandler, + VarCheckVariablePropertySet, + VarCheckVariablePropertyGet }; /** Return TRUE if ExitBootServices () has been called. - + @retval TRUE If ExitBootServices () has been called. **/ BOOLEAN @@ -66,39 +132,11 @@ AtRuntime ( return mAtRuntime; } -/** - 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 -InternalIsAddressInSmram ( - 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; -} - - /** Initializes a basic mutual exclusion lock. - This function initializes a basic mutual exclusion lock to the released state - and returns the lock. Each lock provides mutual exclusion access at its task + This function initializes a basic mutual exclusion lock to the released state + and returns the lock. Each lock provides mutual exclusion access at its task priority level. Since there is no preemption or multiprocessor support in EFI, acquiring the lock only consists of raising to the locks TPL. If Lock is NULL, then ASSERT(). @@ -161,7 +199,7 @@ ReleaseLockOnlyAtBootTime ( } /** - Retrive the SMM Fault Tolerent Write protocol interface. + Retrieve the SMM Fault Tolerent Write protocol interface. @param[out] FtwProtocol The interface of SMM Ftw protocol @@ -181,8 +219,8 @@ GetFtwProtocol ( // Locate Smm Fault Tolerent Write protocol // Status = gSmst->SmmLocateProtocol ( - &gEfiSmmFaultTolerantWriteProtocolGuid, - NULL, + &gEfiSmmFaultTolerantWriteProtocolGuid, + NULL, FtwProtocol ); return Status; @@ -190,7 +228,7 @@ GetFtwProtocol ( /** - Retrive the SMM FVB protocol interface by HANDLE. + Retrieve the SMM FVB protocol interface by HANDLE. @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for reading, writing, and erasing the target block. @@ -220,7 +258,7 @@ GetFvbByHandle ( /** Function returns an array of handles that support the SMM FVB protocol - in a buffer allocated from pool. + in a buffer allocated from pool. @param[out] NumberHandles The number of handles returned in Buffer. @param[out] Buffer A pointer to the buffer to return the requested @@ -276,6 +314,8 @@ GetFvbCountAndBuffer ( *NumberHandles = BufferSize / sizeof(EFI_HANDLE); if (EFI_ERROR(Status)) { *NumberHandles = 0; + FreePool (*Buffer); + *Buffer = NULL; } return Status; @@ -289,16 +329,17 @@ GetFvbCountAndBuffer ( InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime. @param[in, out] InfoEntry A pointer to the buffer of variable information entry. - On input, point to the variable information returned last time. if + On input, point to the variable information returned last time. if InfoEntry->VendorGuid is zero, return the first information. On output, point to the next variable information. @param[in, out] InfoSize On input, the size of the variable information buffer. On output, the returned variable information size. - @retval EFI_SUCCESS The variable information is found and returned successfully. - @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The - PcdVariableCollectStatistics should be set TRUE to support it. - @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information. + @retval EFI_SUCCESS The variable information is found and returned successfully. + @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The + PcdVariableCollectStatistics should be set TRUE to support it. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information. + @retval EFI_INVALID_PARAMETER Input parameter is invalid. **/ EFI_STATUS @@ -308,29 +349,43 @@ SmmVariableGetStatistics ( ) { VARIABLE_INFO_ENTRY *VariableInfo; - UINTN NameLength; + UINTN NameSize; UINTN StatisticsInfoSize; CHAR16 *InfoName; - - ASSERT (InfoEntry != NULL); - VariableInfo = gVariableInfo; + UINTN InfoNameMaxSize; + EFI_GUID VendorGuid; + + if (InfoEntry == NULL) { + return EFI_INVALID_PARAMETER; + } + + VariableInfo = gVariableInfo; if (VariableInfo == NULL) { return EFI_UNSUPPORTED; } - StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name); + StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY); if (*InfoSize < StatisticsInfoSize) { *InfoSize = StatisticsInfoSize; return EFI_BUFFER_TOO_SMALL; } InfoName = (CHAR16 *)(InfoEntry + 1); + InfoNameMaxSize = (*InfoSize - sizeof (VARIABLE_INFO_ENTRY)); - if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) { + CopyGuid (&VendorGuid, &InfoEntry->VendorGuid); + + if (IsZeroGuid (&VendorGuid)) { // // Return the first variable info // + NameSize = StrSize (VariableInfo->Name); + StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + NameSize; + if (*InfoSize < StatisticsInfoSize) { + *InfoSize = StatisticsInfoSize; + return EFI_BUFFER_TOO_SMALL; + } CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY)); - CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name)); + CopyMem (InfoName, VariableInfo->Name, NameSize); *InfoSize = StatisticsInfoSize; return EFI_SUCCESS; } @@ -339,10 +394,10 @@ SmmVariableGetStatistics ( // Get the next variable info // while (VariableInfo != NULL) { - if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) { - NameLength = StrSize (VariableInfo->Name); - if (NameLength == StrSize (InfoName)) { - if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) { + if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) { + NameSize = StrSize (VariableInfo->Name); + if (NameSize <= InfoNameMaxSize) { + if (CompareMem (VariableInfo->Name, InfoName, NameSize) == 0) { // // Find the match one // @@ -353,7 +408,7 @@ SmmVariableGetStatistics ( } VariableInfo = VariableInfo->Next; }; - + if (VariableInfo == NULL) { *InfoSize = 0; return EFI_SUCCESS; @@ -362,16 +417,17 @@ SmmVariableGetStatistics ( // // Output the new variable info // - StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name); + NameSize = StrSize (VariableInfo->Name); + StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + NameSize; if (*InfoSize < StatisticsInfoSize) { *InfoSize = StatisticsInfoSize; return EFI_BUFFER_TOO_SMALL; } CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY)); - CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name)); + CopyMem (InfoName, VariableInfo->Name, NameSize); *InfoSize = StatisticsInfoSize; - + return EFI_SUCCESS; } @@ -383,8 +439,8 @@ SmmVariableGetStatistics ( Caution: This function may receive untrusted input. This variable data and communicate buffer are external input, so this function will do basic validation. - Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), - VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), + Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), + VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), SmmVariableGetStatistics() should also do validation based on its own knowledge. @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister(). @@ -394,11 +450,11 @@ SmmVariableGetStatistics ( be conveyed from a non-SMM environment into an SMM environment. @param[in, out] CommBufferSize The size of the CommBuffer. - @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers + @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called. - @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should + @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should still be called. - @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still + @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still be called. @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced. **/ @@ -416,8 +472,14 @@ SmmVariableHandler ( SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader; SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName; SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo; + SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; VARIABLE_INFO_ENTRY *VariableInfo; + SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock; + SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty; UINTN InfoSize; + UINTN NameBufferSize; + UINTN CommBufferPayloadSize; + UINTN TempCommBufferSize; // // If input is invalid, stop processing this SMI @@ -426,27 +488,59 @@ SmmVariableHandler ( return EFI_SUCCESS; } - if (*CommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + TempCommBufferSize = *CommBufferSize; + + if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE; + if (CommBufferPayloadSize > mVariableBufferPayloadSize) { + DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n")); return EFI_SUCCESS; } - if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBuffer, *CommBufferSize)) { - DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n")); + if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) { + DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n")); return EFI_SUCCESS; } SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer; switch (SmmVariableFunctionHeader->Function) { case SMM_VARIABLE_FUNCTION_GET_VARIABLE: - SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data; - InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) { + DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + // + // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload. + // + CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize); + SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload; + if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) || + ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) { + // + // Prevent InfoSize overflow happen + // + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize; // // SMRAM range check already covered before // - if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { - DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n")); + if (InfoSize > CommBufferPayloadSize) { + DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n")); + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + + if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') { + // + // Make sure VariableName is A Null-terminated string. + // Status = EFI_ACCESS_DENIED; goto EXIT; } @@ -458,17 +552,42 @@ SmmVariableHandler ( &SmmVariableHeader->DataSize, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize ); + CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize); break; - + case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME: - GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data; + if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) { + DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + // + // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload. + // + CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize); + GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload; + if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) { + // + // Prevent InfoSize overflow happen + // + Status = EFI_ACCESS_DENIED; + goto EXIT; + } InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize; // // SMRAM range check already covered before // - if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { - DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n")); + if (InfoSize > CommBufferPayloadSize) { + DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n")); + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + + NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name); + if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') { + // + // Make sure input VariableName is A Null-terminated string. + // Status = EFI_ACCESS_DENIED; goto EXIT; } @@ -478,10 +597,27 @@ SmmVariableHandler ( GetNextVariableName->Name, &GetNextVariableName->Guid ); + CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize); break; - + case SMM_VARIABLE_FUNCTION_SET_VARIABLE: - SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data; + if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) { + DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + // + // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload. + // + CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize); + SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload; + if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) || + ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) { + // + // Prevent InfoSize overflow happen + // + Status = EFI_ACCESS_DENIED; + goto EXIT; + } InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize; @@ -489,8 +625,16 @@ SmmVariableHandler ( // SMRAM range check already covered before // Data buffer should not contain SMM range // - if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { - DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n")); + if (InfoSize > CommBufferPayloadSize) { + DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n")); + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + + if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') { + // + // Make sure VariableName is A Null-terminated string. + // Status = EFI_ACCESS_DENIED; goto EXIT; } @@ -503,19 +647,13 @@ SmmVariableHandler ( (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize ); break; - - case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO: - QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data; - InfoSize = sizeof(SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO); - // - // SMRAM range check already covered before - // - if (InfoSize > *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { - DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n")); - Status = EFI_ACCESS_DENIED; - goto EXIT; + case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) { + DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; } + QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data; Status = VariableServiceQueryVariableInfo ( QueryVariableInfo->Attributes, @@ -525,15 +663,34 @@ SmmVariableHandler ( ); break; + case SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE)) { + DEBUG ((EFI_D_ERROR, "GetPayloadSize: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + GetPayloadSize = (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *) SmmVariableFunctionHeader->Data; + GetPayloadSize->VariablePayloadSize = mVariableBufferPayloadSize; + Status = EFI_SUCCESS; + break; + case SMM_VARIABLE_FUNCTION_READY_TO_BOOT: if (AtRuntime()) { Status = EFI_UNSUPPORTED; break; } + if (!mEndOfDxe) { + MorLockInitAtEndOfDxe (); + mEndOfDxe = TRUE; + VarCheckLibInitializeAtEndOfDxe (NULL); + // + // The initialization for variable quota. + // + InitializeVariableQuota (); + } ReclaimForOS (); Status = EFI_SUCCESS; break; - + case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE: mAtRuntime = TRUE; Status = EFI_SUCCESS; @@ -541,23 +698,90 @@ SmmVariableHandler ( case SMM_VARIABLE_FUNCTION_GET_STATISTICS: VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data; - InfoSize = *CommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE; + InfoSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE; // - // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. - // It is covered by previous CommBuffer check + // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. + // It is covered by previous CommBuffer check + // + + // + // Do not need to check CommBufferSize buffer as it should point to SMRAM + // that was used by SMM core to cache CommSize from SmmCommunication protocol. // - - if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) { - DEBUG ((EFI_D_ERROR, "SMM communication buffer size is in SMRAM!\n")); - Status = EFI_ACCESS_DENIED; - goto EXIT; - } Status = SmmVariableGetStatistics (VariableInfo, &InfoSize); *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE; break; + case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE: + if (mEndOfDxe) { + Status = EFI_ACCESS_DENIED; + } else { + VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data; + Status = VariableLockRequestToLock ( + NULL, + VariableToLock->Name, + &VariableToLock->Guid + ); + } + break; + case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET: + if (mEndOfDxe) { + Status = EFI_ACCESS_DENIED; + } else { + CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) SmmVariableFunctionHeader->Data; + Status = VarCheckVariablePropertySet ( + CommVariableProperty->Name, + &CommVariableProperty->Guid, + &CommVariableProperty->VariableProperty + ); + } + break; + case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET: + if (CommBufferPayloadSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) { + DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n")); + return EFI_SUCCESS; + } + // + // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload. + // + CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize); + CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) mVariableBufferPayload; + if ((UINTN) (~0) - CommVariableProperty->NameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) { + // + // Prevent InfoSize overflow happen + // + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + CommVariableProperty->NameSize; + + // + // SMRAM range check already covered before + // + if (InfoSize > CommBufferPayloadSize) { + DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n")); + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + + if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') { + // + // Make sure VariableName is A Null-terminated string. + // + Status = EFI_ACCESS_DENIED; + goto EXIT; + } + + Status = VarCheckVariablePropertyGet ( + CommVariableProperty->Name, + &CommVariableProperty->Guid, + &CommVariableProperty->VariableProperty + ); + CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize); + break; + default: Status = EFI_UNSUPPORTED; } @@ -569,20 +793,52 @@ EXIT: return EFI_SUCCESS; } +/** + SMM END_OF_DXE protocol notification event handler. + + @param Protocol Points to the protocol's unique identifier + @param Interface Points to the interface instance + @param Handle The handle on which the interface was installed + + @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully + +**/ +EFI_STATUS +EFIAPI +SmmEndOfDxeCallback ( + IN CONST EFI_GUID *Protocol, + IN VOID *Interface, + IN EFI_HANDLE Handle + ) +{ + DEBUG ((EFI_D_INFO, "[Variable]SMM_END_OF_DXE is signaled\n")); + MorLockInitAtEndOfDxe (); + mEndOfDxe = TRUE; + VarCheckLibInitializeAtEndOfDxe (NULL); + // + // The initialization for variable quota. + // + InitializeVariableQuota (); + if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) { + ReclaimForOS (); + } + + return EFI_SUCCESS; +} /** SMM Fault Tolerant Write protocol notification event handler. - Non-Volatile variable write may needs FTW protocol to reclaim when + Non-Volatile variable write may needs FTW protocol to reclaim when writting variable. - + @param Protocol Points to the protocol's unique identifier @param Interface Points to the interface instance @param Handle The handle on which the interface was installed @retval EFI_SUCCESS SmmEventCallback runs successfully @retval EFI_NOT_FOUND The Fvb protocol for variable is not found. - + **/ EFI_STATUS EFIAPI @@ -596,7 +852,8 @@ SmmFtwNotificationEvent ( EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol; EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol; EFI_PHYSICAL_ADDRESS NvStorageVariableBase; - + UINTN FtwMaxBlockSize; + if (mVariableModuleGlobal->FvbInstance != NULL) { return EFI_SUCCESS; } @@ -609,6 +866,11 @@ SmmFtwNotificationEvent ( return Status; } + Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize); + if (!EFI_ERROR (Status)) { + ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize); + } + // // Find the proper FVB protocol for variable. // @@ -622,10 +884,12 @@ SmmFtwNotificationEvent ( } mVariableModuleGlobal->FvbInstance = FvbProtocol; - + Status = VariableWriteServiceInitialize (); - ASSERT_EFI_ERROR (Status); - + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status)); + } + // // Notify the variable wrapper driver the variable write service is ready // @@ -636,20 +900,20 @@ SmmFtwNotificationEvent ( NULL ); ASSERT_EFI_ERROR (Status); - + return EFI_SUCCESS; } /** Variable Driver main entry point. The Variable driver places the 4 EFI - runtime services in the EFI System Table and installs arch protocols + runtime services in the EFI System Table and installs arch protocols for variable read and write services being available. It also registers a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event. - @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] ImageHandle The firmware allocated handle for the EFI image. @param[in] SystemTable A pointer to the EFI System Table. - + @retval EFI_SUCCESS Variable service successfully initialized. **/ @@ -663,8 +927,7 @@ VariableServiceInitialize ( EFI_STATUS Status; EFI_HANDLE VariableHandle; VOID *SmmFtwRegistration; - EFI_SMM_ACCESS2_PROTOCOL *SmmAccess; - UINTN Size; + VOID *SmmEndOfDxeRegistration; // // Variable initialize. @@ -684,35 +947,31 @@ VariableServiceInitialize ( ); ASSERT_EFI_ERROR (Status); - // - // Get SMRAM information - // - Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess); + Status = gSmst->SmmInstallProtocolInterface ( + &VariableHandle, + &gEdkiiSmmVarCheckProtocolGuid, + EFI_NATIVE_INTERFACE, + &mSmmVarCheck + ); ASSERT_EFI_ERROR (Status); - Size = 0; - Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL); - ASSERT (Status == EFI_BUFFER_TOO_SMALL); + mVariableBufferPayloadSize = GetMaxVariableSize () + + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - GetVariableHeaderSize (); Status = gSmst->SmmAllocatePool ( EfiRuntimeServicesData, - Size, - (VOID **)&mSmramRanges + mVariableBufferPayloadSize, + (VOID **)&mVariableBufferPayload ); ASSERT_EFI_ERROR (Status); - Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges); - ASSERT_EFI_ERROR (Status); - - mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR); - /// /// Register SMM variable SMI handler /// VariableHandle = NULL; Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle); ASSERT_EFI_ERROR (Status); - + // // Notify the variable wrapper driver the variable service is ready // @@ -723,10 +982,20 @@ VariableServiceInitialize ( &gSmmVariable ); ASSERT_EFI_ERROR (Status); - + + // + // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function. + // + Status = gSmst->SmmRegisterProtocolNotify ( + &gEfiSmmEndOfDxeProtocolGuid, + SmmEndOfDxeCallback, + &SmmEndOfDxeRegistration + ); + ASSERT_EFI_ERROR (Status); + // // Register FtwNotificationEvent () notify function. - // + // Status = gSmst->SmmRegisterProtocolNotify ( &gEfiSmmFaultTolerantWriteProtocolGuid, SmmFtwNotificationEvent, @@ -735,7 +1004,7 @@ VariableServiceInitialize ( ASSERT_EFI_ERROR (Status); SmmFtwNotificationEvent (NULL, NULL, NULL); - + return EFI_SUCCESS; }