]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
SecurityPkg Variable: Implement variable quota management.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmm.c
index 0be4f254d788e848b7838d6b97c47f29120e741e..9af9eafe14df952c00da159646f80915f074760f 100644 (file)
@@ -14,7 +14,7 @@
   VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
   SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
 \r
-Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials \r
 are licensed and made available under the terms and conditions of the BSD License \r
 which accompanies this distribution.  The full text of the license may be found at \r
@@ -30,6 +30,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/SmmFaultTolerantWrite.h>\r
 #include <Protocol/SmmAccess2.h>\r
 #include <Protocol/SmmEndOfDxe.h>\r
+#include <Protocol/SmmVarCheck.h>\r
 \r
 #include <Library/SmmServicesTableLib.h>\r
 \r
@@ -50,6 +51,23 @@ UINTN                                                mVariableBufferPayloadSize;
 extern BOOLEAN                                       mEndOfDxe;\r
 extern BOOLEAN                                       mEnableLocking;\r
 \r
+/**\r
+  SecureBoot Hook for SetVariable.\r
+\r
+  @param[in] VariableName                 Name of Variable to be found.\r
+  @param[in] VendorGuid                   Variable vendor GUID.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SecureBootHook (\r
+  IN CHAR16                                 *VariableName,\r
+  IN EFI_GUID                               *VendorGuid\r
+  )\r
+{\r
+  return ;\r
+}\r
+\r
 /**\r
 \r
   This code sets variable in storage blocks (Volatile or Non-Volatile).\r
@@ -102,6 +120,10 @@ EFI_SMM_VARIABLE_PROTOCOL      gSmmVariable = {
   VariableServiceQueryVariableInfo\r
 };\r
 \r
+EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck = { VarCheckRegisterSetVariableCheckHandler,\r
+                                              VarCheckVariablePropertySet,\r
+                                              VarCheckVariablePropertyGet };\r
+\r
 /**\r
   Return TRUE if ExitBootServices () has been called.\r
   \r
@@ -503,6 +525,7 @@ SmmVariableHandler (
   SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO     *QueryVariableInfo;\r
   VARIABLE_INFO_ENTRY                              *VariableInfo;\r
   SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE           *VariableToLock;\r
+  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
   UINTN                                            InfoSize;\r
   UINTN                                            NameBufferSize;\r
   UINTN                                            CommBufferPayloadSize;\r
@@ -693,6 +716,10 @@ SmmVariableHandler (
 \r
     case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
       mEndOfDxe = TRUE;\r
+      //\r
+      // The initialization for variable quota.\r
+      //\r
+      InitializeVariableQuota ();\r
       if (AtRuntime()) {\r
         Status = EFI_UNSUPPORTED;\r
         break;\r
@@ -737,6 +764,61 @@ SmmVariableHandler (
                    );\r
       }\r
       break;\r
+    case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET:\r
+      if (mEndOfDxe) {\r
+        Status = EFI_ACCESS_DENIED;\r
+      } else {\r
+        CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) SmmVariableFunctionHeader->Data;\r
+        Status = VarCheckVariablePropertySet (\r
+                   CommVariableProperty->Name,\r
+                   &CommVariableProperty->Guid,\r
+                   &CommVariableProperty->VariableProperty\r
+                   );\r
+      }\r
+      break;\r
+    case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET:\r
+      if (CommBufferPayloadSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
+        DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));\r
+        return EFI_SUCCESS;\r
+      }\r
+      //\r
+      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
+      //\r
+      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
+      CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) mVariableBufferPayload;\r
+      if ((UINTN) (~0) - CommVariableProperty->NameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
+        //\r
+        // Prevent InfoSize overflow happen\r
+        //\r
+        Status = EFI_ACCESS_DENIED;\r
+        goto EXIT;\r
+      }\r
+      InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + CommVariableProperty->NameSize;\r
+\r
+      //\r
+      // SMRAM range check already covered before\r
+      //\r
+      if (InfoSize > CommBufferPayloadSize) {\r
+        DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));\r
+        Status = EFI_ACCESS_DENIED;\r
+        goto EXIT;\r
+      }\r
+\r
+      if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
+        //\r
+        // Make sure VariableName is A Null-terminated string.\r
+        //\r
+        Status = EFI_ACCESS_DENIED;\r
+        goto EXIT;\r
+      }\r
+\r
+      Status = VarCheckVariablePropertyGet (\r
+                 CommVariableProperty->Name,\r
+                 &CommVariableProperty->Guid,\r
+                 &CommVariableProperty->VariableProperty\r
+                 );\r
+      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
+      break;\r
 \r
     default:\r
       Status = EFI_UNSUPPORTED;\r
@@ -768,6 +850,10 @@ SmmEndOfDxeCallback (
 {\r
   DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
   mEndOfDxe = TRUE;\r
+  //\r
+  // The initialization for variable quota.\r
+  //\r
+  InitializeVariableQuota ();\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -797,6 +883,7 @@ SmmFtwNotificationEvent (
   EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvbProtocol;\r
   EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL   *FtwProtocol;\r
   EFI_PHYSICAL_ADDRESS                    NvStorageVariableBase;\r
+  UINTN                                   FtwMaxBlockSize;\r
   \r
   if (mVariableModuleGlobal->FvbInstance != NULL) {\r
     return EFI_SUCCESS;\r
@@ -810,6 +897,11 @@ SmmFtwNotificationEvent (
     return Status;\r
   }\r
 \r
+  Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
+  }\r
+\r
   //\r
   // Find the proper FVB protocol for variable.\r
   //\r
@@ -825,7 +917,9 @@ SmmFtwNotificationEvent (
   mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
   \r
   Status = VariableWriteServiceInitialize ();\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
+  }\r
  \r
   //\r
   // Notify the variable wrapper driver the variable write service is ready\r
@@ -886,6 +980,14 @@ VariableServiceInitialize (
                     );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  Status = gSmst->SmmInstallProtocolInterface (\r
+                    &VariableHandle,\r
+                    &gEdkiiSmmVarCheckProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    &mSmmVarCheck\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   //\r
   // Get SMRAM information\r
   //\r
@@ -909,7 +1011,7 @@ VariableServiceInitialize (
   mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
 \r
   mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize)) +\r
-                               OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);\r
+                               OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);\r
 \r
   Status = gSmst->SmmAllocatePool (\r
                     EfiRuntimeServicesData,\r