]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsMisc.c
IntelFrameworkModulePkg GenericBdsLib: Potential read over memory boudary
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsMisc.c
index 15a47e1917ea98893f35b887a43ad1967b43b890..24c1998a1415de2773f20416bc68ef659408da78 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Misc BDS library function\r
 \r
-Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 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
@@ -168,6 +168,7 @@ BdsLibGetFreeOptionNumber (
     if (OptionBuffer == NULL) {\r
       break;\r
     }\r
+    FreePool(OptionBuffer);\r
     Index++;\r
   } while (TRUE);\r
 \r
@@ -217,6 +218,9 @@ BdsLibRegisterNewOption (
   UINT16                    BootOrderEntry;\r
   UINTN                     OrderItemNum;\r
 \r
+  if (DevicePath == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
   OptionPtr             = NULL;\r
   OptionSize            = 0;\r
@@ -257,6 +261,15 @@ BdsLibRegisterNewOption (
     if (OptionPtr == NULL) {\r
       continue;\r
     }\r
+\r
+    //\r
+    // Validate the variable.\r
+    //\r
+    if (!ValidateOption(OptionPtr, OptionSize)) {\r
+      FreePool(OptionPtr);\r
+      continue;\r
+    }\r
+\r
     TempPtr         =   OptionPtr;\r
     TempPtr         +=  sizeof (UINT32) + sizeof (UINT16);\r
     Description     =   (CHAR16 *) TempPtr;\r
@@ -425,7 +438,7 @@ GetDevicePathSizeEx (
   Size = 0;\r
   while (!IsDevicePathEnd (DevicePath)) {\r
     NodeSize = DevicePathNodeLength (DevicePath);\r
-    if (NodeSize == 0) {\r
+    if (NodeSize < END_DEVICE_PATH_LENGTH) {\r
       return 0;\r
     }\r
     Size += NodeSize;\r
@@ -447,7 +460,7 @@ GetDevicePathSizeEx (
   bigger than MaxStringLen, return length 0 to indicate that this is an \r
   invalidate string.\r
 \r
-  This function returns the number of Unicode characters in the Null-terminated\r
+  This function returns the byte length of Unicode characters in the Null-terminated\r
   Unicode string specified by String. \r
 \r
   If String is NULL, then ASSERT().\r
@@ -471,13 +484,76 @@ StrSizeEx (
   ASSERT (String != NULL && MaxStringLen != 0);\r
   ASSERT (((UINTN) String & BIT0) == 0);\r
 \r
-  for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length++);\r
+  for (Length = 0; *String != L'\0' && MaxStringLen != Length; String++, Length+=2);\r
 \r
   if (*String != L'\0' && MaxStringLen == Length) {\r
     return 0;\r
   }\r
 \r
-  return (Length + 1) * sizeof (*String);\r
+  return Length + 2;\r
+}\r
+\r
+/**\r
+  Validate the EFI Boot#### variable (VendorGuid/Name)\r
+\r
+  @param  Variable              Boot#### variable data.\r
+  @param  VariableSize          Returns the size of the EFI variable that was read\r
+\r
+  @retval TRUE                  The variable data is correct.\r
+  @retval FALSE                 The variable data is corrupted.\r
+\r
+**/\r
+BOOLEAN \r
+ValidateOption (\r
+  UINT8                     *Variable,\r
+  UINTN                     VariableSize\r
+  )\r
+{\r
+  UINT16                    FilePathSize;\r
+  UINT8                     *TempPtr;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  UINTN                     TempSize;\r
+\r
+  if (VariableSize <= sizeof (UINT16) + sizeof (UINT32)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Skip the option attribute\r
+  //\r
+  TempPtr    = Variable;\r
+  TempPtr   += sizeof (UINT32);\r
+\r
+  //\r
+  // Get the option's device path size\r
+  //\r
+  FilePathSize  = *(UINT16 *) TempPtr;\r
+  TempPtr      += sizeof (UINT16);\r
+\r
+  //\r
+  // Get the option's description string size\r
+  //\r
+  TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize - sizeof (UINT16) - sizeof (UINT32));\r
+  TempPtr += TempSize;\r
+\r
+  //\r
+  // Get the option's device path\r
+  //\r
+  DevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
+  TempPtr   += FilePathSize;\r
+\r
+  //\r
+  // Validation boot option variable.\r
+  //\r
+  if ((FilePathSize == 0) || (TempSize == 0)) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (TempSize + FilePathSize + sizeof (UINT16) + sizeof (UINT32) > VariableSize) {\r
+    return FALSE;\r
+  }\r
+\r
+  return (BOOLEAN) (GetDevicePathSizeEx (DevicePath, FilePathSize) != 0);\r
 }\r
 \r
 /**\r
@@ -530,13 +606,12 @@ BdsLibVariableToOption (
   UINT8                     *TempPtr;\r
   UINTN                     VariableSize;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL  *TempPath;\r
   BDS_COMMON_OPTION         *Option;\r
   VOID                      *LoadOptions;\r
   UINT32                    LoadOptionsSize;\r
   CHAR16                    *Description;\r
   UINT8                     NumOff;\r
-  UINTN                     TempSize;\r
+\r
   //\r
   // Read the variable. We will never free this data.\r
   //\r
@@ -548,6 +623,15 @@ BdsLibVariableToOption (
   if (Variable == NULL) {\r
     return NULL;\r
   }\r
+\r
+  //\r
+  // Validate Boot#### variable data.\r
+  //\r
+  if (!ValidateOption(Variable, VariableSize)) {\r
+    FreePool (Variable);\r
+    return NULL;\r
+  }\r
+\r
   //\r
   // Notes: careful defined the variable of Boot#### or\r
   // Driver####, consider use some macro to abstract the code\r
@@ -573,11 +657,7 @@ BdsLibVariableToOption (
   //\r
   // Get the option's description string size\r
   //\r
-  TempSize = StrSizeEx ((CHAR16 *) TempPtr, VariableSize);\r
-  if (TempSize == 0) {\r
-    return NULL;\r
-  }\r
-  TempPtr += TempSize;\r
+  TempPtr += StrSize((CHAR16 *) TempPtr);\r
 \r
   //\r
   // Get the option's device path\r
@@ -585,26 +665,10 @@ BdsLibVariableToOption (
   DevicePath =  (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
   TempPtr    += FilePathSize;\r
 \r
-  //\r
-  // Validation device path.\r
-  //\r
-  TempPath       = DevicePath;\r
-  while (FilePathSize > 0) {\r
-    TempSize = GetDevicePathSizeEx (TempPath, FilePathSize);\r
-    if (TempSize == 0) {\r
-      return NULL;\r
-    }\r
-    FilePathSize = (UINT16) (FilePathSize - TempSize);\r
-    TempPath    += TempSize;\r
-  }\r
-\r
   //\r
   // Get load opion data.\r
   //\r
   LoadOptions     = TempPtr;\r
-  if (VariableSize < (UINTN)(TempPtr - Variable)) {\r
-    return NULL;\r
-  }\r
   LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r
 \r
   //\r
@@ -613,6 +677,7 @@ BdsLibVariableToOption (
   //\r
   Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));\r
   if (Option == NULL) {\r
+    FreePool (Variable);\r
     return NULL;\r
   }\r
 \r
@@ -643,19 +708,9 @@ BdsLibVariableToOption (
                + (UINT16) (CharToUint (VariableName[NumOff+2]) * 0x10)\r
                + (UINT16) (CharToUint (VariableName[NumOff+3]) * 0x1);\r
   }\r
-  //\r
-  // Insert active entry to BdsDeviceList\r
-  //\r
-  if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r
-    InsertTailList (BdsCommonOptionList, &Option->Link);\r
-    FreePool (Variable);\r
-    return Option;\r
-  }\r
-\r
+  InsertTailList (BdsCommonOptionList, &Option->Link);\r
   FreePool (Variable);\r
-  FreePool (Option);\r
-  return NULL;\r
-\r
+  return Option;\r
 }\r
 \r
 /**\r
@@ -777,6 +832,7 @@ BdsLibGetVariableAndSize (
     //\r
     Buffer = AllocateZeroPool (BufferSize);\r
     if (Buffer == NULL) {\r
+      *VariableSize = 0;\r
       return NULL;\r
     }\r
     //\r
@@ -784,10 +840,15 @@ BdsLibGetVariableAndSize (
     //\r
     Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
     if (EFI_ERROR (Status)) {\r
+      FreePool (Buffer);\r
       BufferSize = 0;\r
+      Buffer     = NULL;\r
     }\r
   }\r
 \r
+  ASSERT (((Buffer == NULL) && (BufferSize == 0)) ||\r
+          ((Buffer != NULL) && (BufferSize != 0))\r
+          );\r
   *VariableSize = BufferSize;\r
   return Buffer;\r
 }\r
@@ -1070,24 +1131,27 @@ SetupResetReminder (
       ASSERT (StringBuffer1 != NULL);\r
       StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r
       ASSERT (StringBuffer2 != NULL);\r
-      StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");\r
-      StrCpy (StringBuffer2, L"Enter (YES)  /   Esc (NO)");\r
+      StrCpyS (\r
+        StringBuffer1,\r
+        MAX_STRING_LEN,\r
+        L"Configuration changed. Reset to apply it Now."\r
+        );\r
+      StrCpyS (\r
+        StringBuffer2,\r
+        MAX_STRING_LEN,\r
+        L"Press ENTER to reset"\r
+        );\r
       //\r
       // Popup a menu to notice user\r
       //\r
       do {\r
         CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, StringBuffer1, StringBuffer2, NULL);\r
-      } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r
+      } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);\r
 \r
       FreePool (StringBuffer1);\r
       FreePool (StringBuffer2);\r
-      //\r
-      // If the user hits the YES Response key, reset\r
-      //\r
-      if (Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {\r
-        gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
-      }\r
-      gST->ConOut->ClearScreen (gST->ConOut);\r
+\r
+      gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
     }\r
   }\r
 }\r
@@ -1234,10 +1298,8 @@ BdsLibGetImageHeader (
 }\r
 \r
 /**\r
-  This routine adjusts the memory information for different memory type and \r
-  saves them into the variables for next boot. It conditionally resets the\r
-  system when the memory information changes. Platform can reserve memory \r
-  large enough (125% of actual requirement) to avoid the reset in the first boot.\r
+  This routine adjust the memory information for different memory type and \r
+  save them into the variables for next boot.\r
 **/\r
 VOID\r
 BdsSetMemoryTypeInformationVariable (\r
@@ -1343,13 +1405,14 @@ BdsSetMemoryTypeInformationVariable (
     Next     = Previous;\r
 \r
     //\r
-    // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r
+    // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
+    // Write next varible to 125% * current when the pre-allocated memory is:\r
+    //  1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
+    //  2. Less than the needed memory\r
     //\r
-    if (Current < Previous) {\r
+    if ((Current + (Current >> 1)) < Previous) {\r
       if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
         Next = Current + (Current >> 2);\r
-      } else if (!MemoryTypeInformationVariableExists) {\r
-        Next = MAX (Current + (Current >> 2), Previous);\r
       }\r
     } else if (Current > Previous) {\r
       Next = Current + (Current >> 2);\r
@@ -1371,22 +1434,26 @@ BdsSetMemoryTypeInformationVariable (
   // Or create the variable in first boot.\r
   //\r
   if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
-    Status = gRT->SetVariable (\r
-                    EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
-                    &gEfiMemoryTypeInformationGuid,\r
-                    EFI_VARIABLE_NON_VOLATILE  | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
-                    VariableSize,\r
-                    PreviousMemoryTypeInformation\r
-                    );\r
+    Status = SetVariableAndReportStatusCodeOnError (\r
+               EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
+               &gEfiMemoryTypeInformationGuid,\r
+               EFI_VARIABLE_NON_VOLATILE  | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+               VariableSize,\r
+               PreviousMemoryTypeInformation\r
+               );\r
 \r
-    //\r
-    // If the Memory Type Information settings have been modified, then reset the platform\r
-    // so the new Memory Type Information setting will be used to guarantee that an S4\r
-    // entry/resume cycle will not fail.\r
-    //\r
-    if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
-      DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
-      gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // If the Memory Type Information settings have been modified, then reset the platform\r
+      // so the new Memory Type Information setting will be used to guarantee that an S4\r
+      // entry/resume cycle will not fail.\r
+      //\r
+      if (MemoryTypeInformationModified && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
+        DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
+        gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+      }\r
+    } else {\r
+      DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
     }\r
   }\r
 }\r
@@ -1434,3 +1501,89 @@ BdsLibUserIdentify (
   return Manager->Identify (Manager, User);\r
 }\r
 \r
+/**\r
+  Set the variable and report the error through status code upon failure.\r
+\r
+  @param  VariableName           A Null-terminated string that is the name of the vendor's variable.\r
+                                 Each VariableName is unique for each VendorGuid. VariableName must\r
+                                 contain 1 or more characters. If VariableName is an empty string,\r
+                                 then EFI_INVALID_PARAMETER is returned.\r
+  @param  VendorGuid             A unique identifier for the vendor.\r
+  @param  Attributes             Attributes bitmask to set for the variable.\r
+  @param  DataSize               The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
+                                 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
+                                 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
+                                 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
+                                 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
+                                 the variable value (the timestamp associated with the variable may be updated however \r
+                                 even if no new data value is provided,see the description of the \r
+                                 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
+                                 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
+  @param  Data                   The contents for the variable.\r
+\r
+  @retval EFI_SUCCESS            The firmware has successfully stored the variable and its data as\r
+                                 defined by the Attributes.\r
+  @retval EFI_INVALID_PARAMETER  An invalid combination of attribute bits, name, and GUID was supplied, or the\r
+                                 DataSize exceeds the maximum allowed.\r
+  @retval EFI_INVALID_PARAMETER  VariableName is an empty string.\r
+  @retval EFI_OUT_OF_RESOURCES   Not enough storage is available to hold the variable and its data.\r
+  @retval EFI_DEVICE_ERROR       The variable could not be retrieved due to a hardware error.\r
+  @retval EFI_WRITE_PROTECTED    The variable in question is read-only.\r
+  @retval EFI_WRITE_PROTECTED    The variable in question cannot be deleted.\r
+  @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
+                                 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
+                                 does NOT pass the validation check carried out by the firmware.\r
+\r
+  @retval EFI_NOT_FOUND          The variable trying to be updated or deleted was not found.\r
+**/\r
+EFI_STATUS\r
+SetVariableAndReportStatusCodeOnError (\r
+  IN CHAR16     *VariableName,\r
+  IN EFI_GUID   *VendorGuid,\r
+  IN UINT32     Attributes,\r
+  IN UINTN      DataSize,\r
+  IN VOID       *Data\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+  EDKII_SET_VARIABLE_STATUS  *SetVariableStatus;\r
+  UINTN                      NameSize;\r
+\r
+  Status = gRT->SetVariable (\r
+                  VariableName,\r
+                  VendorGuid,\r
+                  Attributes,\r
+                  DataSize,\r
+                  Data\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    NameSize = StrSize (VariableName);\r
+    SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
+    if (SetVariableStatus != NULL) {\r
+      CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
+      SetVariableStatus->NameSize   = NameSize;\r
+      SetVariableStatus->DataSize   = DataSize;\r
+      SetVariableStatus->SetStatus  = Status;\r
+      SetVariableStatus->Attributes = Attributes;\r
+      CopyMem (SetVariableStatus + 1,                          VariableName, NameSize);\r
+      if ((Data != NULL) && (DataSize != 0)) {\r
+        CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data,         DataSize);\r
+      }\r
+\r
+      REPORT_STATUS_CODE_EX (\r
+        EFI_ERROR_CODE,\r
+        PcdGet32 (PcdErrorCodeSetVariable),\r
+        0,\r
+        NULL,\r
+        &gEdkiiStatusCodeDataTypeVariableGuid,\r
+        SetVariableStatus,\r
+        sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
+        );\r
+\r
+      FreePool (SetVariableStatus);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r