]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
UEFI 2.4 X509 Certificate Hash and RFC3161 Timestamp Verification support for Secure...
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / Variable.c
index 6b9ca960b6b34eb1c942398892fa26ea3883491c..432531f6dfc94087cec5d53f3e69da97250346a1 100644 (file)
@@ -218,18 +218,24 @@ UpdateVariableInfo (
 \r
   This code checks if variable header is valid or not.\r
 \r
-  @param Variable        Pointer to the Variable Header.\r
+  @param Variable           Pointer to the Variable Header.\r
+  @param VariableStoreEnd   Pointer to the Variable Store End.\r
 \r
-  @retval TRUE           Variable header is valid.\r
-  @retval FALSE          Variable header is not valid.\r
+  @retval TRUE              Variable header is valid.\r
+  @retval FALSE             Variable header is not valid.\r
 \r
 **/\r
 BOOLEAN\r
 IsValidVariableHeader (\r
-  IN  VARIABLE_HEADER   *Variable\r
+  IN  VARIABLE_HEADER       *Variable,\r
+  IN  VARIABLE_HEADER       *VariableStoreEnd\r
   )\r
 {\r
-  if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
+  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
+    //\r
+    // Variable is NULL or has reached the end of variable store,\r
+    // or the StartId is not correct.\r
+    //\r
     return FALSE;\r
   }\r
 \r
@@ -529,10 +535,6 @@ GetNextVariablePtr (
 {\r
   UINTN Value;\r
 \r
-  if (!IsValidVariableHeader (Variable)) {\r
-    return NULL;\r
-  }\r
-\r
   Value =  (UINTN) GetVariableDataPtr (Variable);\r
   Value += DataSizeOfVariable (Variable);\r
   Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
@@ -590,14 +592,14 @@ GetEndPointer (
 \r
   Check the PubKeyIndex is a valid key or not.\r
 \r
-  This function will iterate the NV storage to see if this PubKeyIndex is still referenced \r
+  This function will iterate the NV storage to see if this PubKeyIndex is still referenced\r
   by any valid count-based auth variabe.\r
-  \r
+\r
   @param[in]  PubKeyIndex     Index of the public key in public key store.\r
 \r
   @retval     TRUE            The PubKeyIndex is still in use.\r
   @retval     FALSE           The PubKeyIndex is not referenced by any count-based auth variabe.\r
-  \r
+\r
 **/\r
 BOOLEAN\r
 IsValidPubKeyIndex (\r
@@ -605,28 +607,30 @@ IsValidPubKeyIndex (
   )\r
 {\r
   VARIABLE_HEADER          *Variable;\r
+  VARIABLE_HEADER          *VariableStoreEnd;\r
 \r
   if (PubKeyIndex > mPubKeyNumber) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   Variable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-  \r
-  while (IsValidVariableHeader (Variable)) {\r
-    if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) && \r
+  VariableStoreEnd = GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
+\r
+  while (IsValidVariableHeader (Variable, VariableStoreEnd)) {\r
+    if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
         Variable->PubKeyIndex == PubKeyIndex) {\r
       return TRUE;\r
     }\r
     Variable = GetNextVariablePtr (Variable);\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
 \r
 /**\r
 \r
   Get the number of valid public key in PubKeyStore.\r
-  \r
+\r
   @param[in]  PubKeyNumber     Number of the public key in public key store.\r
 \r
   @return     Number of valid public key in PubKeyStore.\r
@@ -641,13 +645,13 @@ GetValidPubKeyNumber (
   UINT32       Counter;\r
 \r
   Counter = 0;\r
-  \r
+\r
   for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) {\r
     if (IsValidPubKeyIndex (PubKeyIndex)) {\r
       Counter++;\r
     }\r
   }\r
-  \r
+\r
   return Counter;\r
 }\r
 \r
@@ -655,7 +659,7 @@ GetValidPubKeyNumber (
 \r
   Filter the useless key in public key store.\r
 \r
-  This function will find out all valid public keys in public key database, save them in new allocated \r
+  This function will find out all valid public keys in public key database, save them in new allocated\r
   buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer\r
   NewPubKeyIndex and NewPubKeyStore with FreePool().\r
 \r
@@ -664,10 +668,10 @@ GetValidPubKeyNumber (
   @param[out]  NewPubKeyIndex       Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.\r
   @param[out]  NewPubKeyStore       Saved all valid public keys in PubKeyStore.\r
   @param[out]  NewPubKeySize        Buffer size of the NewPubKeyStore.\r
-  \r
+\r
   @retval  EFI_SUCCESS              Trim operation is complete successfully.\r
   @retval  EFI_OUT_OF_RESOURCES     No enough memory resources, or no useless key in PubKeyStore.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 PubKeyStoreFilter (\r
@@ -681,7 +685,7 @@ PubKeyStoreFilter (
   UINT32        PubKeyIndex;\r
   UINT32        CopiedKey;\r
   UINT32        NewPubKeyNumber;\r
-  \r
+\r
   NewPubKeyNumber = GetValidPubKeyNumber (PubKeyNumber);\r
   if (NewPubKeyNumber == PubKeyNumber) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -735,7 +739,7 @@ PubKeyStoreFilter (
   @param[in]      NewVariable             Pointer to new variable.\r
   @param[in]      NewVariableSize         New variable size.\r
   @param[in]      ReclaimPubKeyStore      Reclaim for public key database or not.\r
-  \r
+\r
   @return EFI_SUCCESS                  Reclaim operation has finished successfully.\r
   @return EFI_OUT_OF_RESOURCES         No enough memory resources or variable space.\r
   @return EFI_DEVICE_ERROR             The public key database doesn't exist.\r
@@ -799,7 +803,7 @@ Reclaim (
     Variable          = GetStartPointer (VariableStoreHeader);\r
     MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
 \r
-    while (IsValidVariableHeader (Variable)) {\r
+    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
       NextVariable = GetNextVariablePtr (Variable);\r
       if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
           Variable != UpdatingVariable &&\r
@@ -866,10 +870,10 @@ Reclaim (
     // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).\r
     //\r
     Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable)) {\r
+    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
       NextVariable = GetNextVariablePtr (Variable);\r
       if (Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-        if ((StrCmp (GetVariableNamePtr (Variable), AUTHVAR_KEYDB_NAME) == 0) && \r
+        if ((StrCmp (GetVariableNamePtr (Variable), AUTHVAR_KEYDB_NAME) == 0) &&\r
             (CompareGuid (&Variable->VendorGuid, &gEfiAuthenticatedVariableGuid))) {\r
           //\r
           // Skip the public key database, it will be reinstalled later.\r
@@ -878,7 +882,7 @@ Reclaim (
           Variable = NextVariable;\r
           continue;\r
         }\r
-        \r
+\r
         VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
         CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
         ((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex];\r
@@ -905,14 +909,14 @@ Reclaim (
     Variable->DataSize = NewPubKeySize;\r
     StrCpy (GetVariableNamePtr (Variable), GetVariableNamePtr (PubKeyHeader));\r
     CopyMem (GetVariableDataPtr (Variable), NewPubKeyStore, NewPubKeySize);\r
-    CurrPtr = (UINT8*) GetNextVariablePtr (Variable); \r
+    CurrPtr = (UINT8*) GetNextVariablePtr (Variable);\r
     CommonVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable;\r
   } else {\r
     //\r
     // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
     //\r
     Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable)) {\r
+    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
       NextVariable = GetNextVariablePtr (Variable);\r
       if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r
         VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
@@ -931,7 +935,7 @@ Reclaim (
     // Reinstall all in delete transition variables.\r
     //\r
     Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable)) {\r
+    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
       NextVariable = GetNextVariablePtr (Variable);\r
       if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
 \r
@@ -943,7 +947,7 @@ Reclaim (
 \r
         FoundAdded = FALSE;\r
         AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
-        while (IsValidVariableHeader (AddedVariable)) {\r
+        while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r
           NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
           NameSize = NameSizeOfVariable (AddedVariable);\r
           if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
@@ -1036,7 +1040,7 @@ Reclaim (
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
     } else {\r
       NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
-      while (IsValidVariableHeader (NextVariable)) {\r
+      while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
         VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
         if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
           mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
@@ -1102,7 +1106,7 @@ FindVariableEx (
   InDeletedVariable  = NULL;\r
 \r
   for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
-      ; (PtrTrack->CurrPtr < PtrTrack->EndPtr) && IsValidVariableHeader (PtrTrack->CurrPtr)\r
+      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
       ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
       ) {\r
     if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
@@ -1520,7 +1524,7 @@ VariableGetBestLanguage (
 \r
   @param[in] Attributes         Variable attributes for Variable entries.\r
   @param ...                    The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
-                                A NULL terminates the list. The VariableSize of \r
+                                A NULL terminates the list. The VariableSize of\r
                                 VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
                                 It will be changed to variable total size as output.\r
 \r
@@ -1799,7 +1803,7 @@ AutoUpdateLangVariable (
         VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
         VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
         VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
-        \r
+\r
         VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
         VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
         VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
@@ -2005,7 +2009,7 @@ UpdateVariable (
         Status = EFI_INVALID_PARAMETER;\r
         goto Done;\r
       }\r
-      \r
+\r
       //\r
       // Only variable that have RT attributes can be updated/deleted in Runtime.\r
       //\r
@@ -2099,20 +2103,21 @@ UpdateVariable (
         CopyMem (BufferForMerge, (UINT8 *) ((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize);\r
 \r
         //\r
-        // Set Max Common Variable Data Size as default MaxDataSize \r
+        // Set Max Common Variable Data Size as default MaxDataSize\r
         //\r
         MaxDataSize = PcdGet32 (PcdMaxVariableSize) - DataOffset;\r
 \r
         if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
-            ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) ||\r
-            (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
+            ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
+             (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))) ||\r
+             (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
           //\r
           // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of\r
           // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
           //\r
           Status = AppendSignatureList (\r
                      BufferForMerge,\r
-                     Variable->CurrPtr->DataSize, \r
+                     Variable->CurrPtr->DataSize,\r
                      MaxDataSize - Variable->CurrPtr->DataSize,\r
                      Data,\r
                      DataSize,\r
@@ -2526,8 +2531,8 @@ Done:
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
 \r
-  This function checks if a Unicode character is a \r
-  hexadecimal character.  The valid hexadecimal character is \r
+  This function checks if a Unicode character is a\r
+  hexadecimal character.  The valid hexadecimal character is\r
   L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
 \r
 \r
@@ -2697,7 +2702,7 @@ VariableLockRequestToLock (
 \r
   @retval TRUE      This variable is read-only variable.\r
   @retval FALSE     This variable is NOT read-only variable.\r
-  \r
+\r
 **/\r
 BOOLEAN\r
 IsReadOnlyVariable (\r
@@ -2718,7 +2723,7 @@ IsReadOnlyVariable (
       return TRUE;\r
     }\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
 \r
@@ -2867,10 +2872,7 @@ VariableServiceGetNextVariableName (
     //\r
     // Switch from Volatile to HOB, to Non-Volatile.\r
     //\r
-    while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
-           (Variable.CurrPtr == NULL)            ||\r
-           !IsValidVariableHeader (Variable.CurrPtr)\r
-          ) {\r
+    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
       //\r
       // Find current storage index\r
       //\r
@@ -3076,8 +3078,8 @@ VariableServiceSetVariable (
 \r
   if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
     //\r
-    // Prevent whole variable size overflow \r
-    // \r
+    // Prevent whole variable size overflow\r
+    //\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -3119,8 +3121,7 @@ VariableServiceSetVariable (
     // Parse non-volatile variable data and get last variable offset.\r
     //\r
     NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
-    while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))\r
-        && IsValidVariableHeader (NextVariable)) {\r
+    while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
       NextVariable = GetNextVariablePtr (NextVariable);\r
     }\r
     mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
@@ -3184,8 +3185,9 @@ VariableServiceSetVariable (
     Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
   } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
     Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
-  } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && \r
-          ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) {\r
+  } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
+          ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))\r
+           || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2)) == 0) {\r
     Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
     if (EFI_ERROR (Status)) {\r
       Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
@@ -3294,7 +3296,7 @@ VariableServiceQueryVariableInfoInternal (
   //\r
   // Now walk through the related variable store.\r
   //\r
-  while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
+  while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
     NextVariable = GetNextVariablePtr (Variable);\r
     VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
 \r
@@ -3594,7 +3596,7 @@ InitNonVolatileVariableStore (
   // Parse non-volatile variable data and get last variable offset.\r
   //\r
   NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
-  while (IsValidVariableHeader (NextVariable)) {\r
+  while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
     VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
     if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
       mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
@@ -3640,7 +3642,7 @@ FlushHobVariableToFlash (
     //\r
     mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
     for ( Variable = GetStartPointer (VariableStoreHeader)\r
-        ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable))\r
+        ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
         ; Variable = GetNextVariablePtr (Variable)\r
         ) {\r
       if (Variable->State != VAR_ADDED) {\r
@@ -3937,4 +3939,3 @@ GetFvbInfoByAddress (
 \r
   return Status;\r
 }\r
-\r