SecurityPkg/SecureBootConfigDxe: Handle lack of STR_SIGNATURE_* tokens
authorchenc2 <chen.a.chen@intel.com>
Wed, 18 Oct 2017 06:23:45 +0000 (14:23 +0800)
committerZhang, Chao B <chao.b.zhang@intel.com>
Thu, 19 Oct 2017 14:22:30 +0000 (22:22 +0800)
Add check to avoid NULL ptr dereference. The function HiiGetString
will return NULL pointer when the platform does not install the
appropriate string or call HiiGetString fail.(For example, HII not
support specified language.)

Cc: Zhang Chao <chao.b.zhang@intel.com>
Cc: Wu Hao <hao.a.wu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: chenc2 <chen.a.chen@intel.com>
Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>
Reviewed-by: Wu Hao <hao.a.wu@intel.com>
SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigImpl.c

index acb0dc055892bb6e3a81d2fc086d30e0a079e014..d035763106852ef8b4dde3e363a272dc81a6f9f6 100644 (file)
@@ -3572,6 +3572,9 @@ LoadSignatureList (
 {\r
   EFI_STATUS            Status;\r
   EFI_STRING_ID         ListType;\r
+  EFI_STRING            FormatNameString;\r
+  EFI_STRING            FormatHelpString;\r
+  EFI_STRING            FormatTypeString;\r
   EFI_SIGNATURE_LIST    *ListWalker;\r
   EFI_IFR_GUID_LABEL    *StartLabel;\r
   EFI_IFR_GUID_LABEL    *EndLabel;\r
@@ -3591,6 +3594,8 @@ LoadSignatureList (
   CHAR16                HelpBuffer[BUFFER_MAX_SIZE];\r
 \r
   Status                = EFI_SUCCESS;\r
+  FormatNameString      = NULL;\r
+  FormatHelpString      = NULL;\r
   StartOpCodeHandle     = NULL;\r
   EndOpCodeHandle       = NULL;\r
   StartGotoHandle       = NULL;\r
@@ -3705,6 +3710,12 @@ LoadSignatureList (
     goto ON_EXIT;\r
   }\r
 \r
+  FormatNameString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_LIST_NAME_FORMAT), NULL);\r
+  FormatHelpString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_LIST_HELP_FORMAT), NULL);\r
+  if (FormatNameString == NULL || FormatHelpString == NULL) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
   RemainingSize = DataSize;\r
   ListWalker    = (EFI_SIGNATURE_LIST *)VariableData;\r
   while ((RemainingSize > 0) && (RemainingSize >= ListWalker->SignatureListSize)) {\r
@@ -3725,21 +3736,23 @@ LoadSignatureList (
     } else {\r
       ListType = STRING_TOKEN (STR_LIST_TYPE_UNKNOWN);\r
     }\r
+    FormatTypeString = HiiGetString (PrivateData->HiiHandle, ListType, NULL);\r
+    if (FormatTypeString == NULL) {\r
+      goto ON_EXIT;\r
+    }\r
 \r
     ZeroMem (NameBuffer, sizeof (NameBuffer));\r
-    UnicodeSPrint (NameBuffer,\r
-      sizeof (NameBuffer),\r
-      HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_LIST_NAME_FORMAT), NULL),\r
-      Index + 1\r
-    );\r
+    UnicodeSPrint (NameBuffer, sizeof (NameBuffer), FormatNameString, Index + 1);\r
 \r
     ZeroMem (HelpBuffer, sizeof (HelpBuffer));\r
     UnicodeSPrint (HelpBuffer,\r
       sizeof (HelpBuffer),\r
-      HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_LIST_HELP_FORMAT), NULL),\r
-      HiiGetString (PrivateData->HiiHandle, ListType, NULL),\r
+      FormatHelpString,\r
+      FormatTypeString,\r
       SIGNATURE_DATA_COUNTS (ListWalker)\r
     );\r
+    SECUREBOOT_FREE_NON_NULL (FormatTypeString);\r
+    FormatTypeString = NULL;\r
 \r
     HiiCreateGotoOpCode (\r
       StartOpCodeHandle,\r
@@ -3777,6 +3790,8 @@ ON_EXIT:
   SECUREBOOT_FREE_NON_OPCODE (EndGotoHandle);\r
 \r
   SECUREBOOT_FREE_NON_NULL (VariableData);\r
+  SECUREBOOT_FREE_NON_NULL (FormatNameString);\r
+  SECUREBOOT_FREE_NON_NULL (FormatHelpString);\r
 \r
   PrivateData->ListCount = Index;\r
 \r
@@ -3922,6 +3937,8 @@ FormatHelpInfo (
   EFI_STATUS      Status;\r
   EFI_TIME        *Time;\r
   EFI_STRING_ID   ListTypeId;\r
+  EFI_STRING      FormatHelpString;\r
+  EFI_STRING      FormatTypeString;\r
   UINTN           DataSize;\r
   UINTN           HelpInfoIndex;\r
   UINTN           TotalSize;\r
@@ -3931,12 +3948,13 @@ FormatHelpInfo (
   CHAR16          *HelpInfoString;\r
   BOOLEAN         IsCert;\r
 \r
-  Status          = EFI_SUCCESS;\r
-  Time            = NULL;\r
-  HelpInfoIndex   = 0;\r
-  DataString      = NULL;\r
-  HelpInfoString  = NULL;\r
-  IsCert          = FALSE;\r
+  Status            = EFI_SUCCESS;\r
+  Time              = NULL;\r
+  FormatTypeString  = NULL;\r
+  HelpInfoIndex     = 0;\r
+  DataString        = NULL;\r
+  HelpInfoString    = NULL;\r
+  IsCert            = FALSE;\r
 \r
   if (CompareGuid(&ListEntry->SignatureType, &gEfiCertRsa2048Guid)) {\r
     ListTypeId = STRING_TOKEN(STR_LIST_TYPE_RSA2048_SHA256);\r
@@ -3969,6 +3987,11 @@ FormatHelpInfo (
     goto ON_EXIT;\r
   }\r
 \r
+  FormatTypeString = HiiGetString (PrivateData->HiiHandle, ListTypeId, NULL);\r
+  if (FormatTypeString == NULL) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
   TotalSize = 1024;\r
   HelpInfoString = AllocateZeroPool (TotalSize);\r
   if (HelpInfoString == NULL) {\r
@@ -3981,40 +4004,45 @@ FormatHelpInfo (
   //\r
   ZeroMem (GuidString, sizeof (GuidString));\r
   GuidToString(&DataEntry->SignatureOwner, GuidString, BUFFER_MAX_SIZE);\r
+  FormatHelpString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_GUID), NULL);\r
+  if (FormatHelpString == NULL) {\r
+    goto ON_EXIT;\r
+  }\r
   HelpInfoIndex += UnicodeSPrint (\r
                      &HelpInfoString[HelpInfoIndex],\r
                      TotalSize - sizeof(CHAR16) * HelpInfoIndex,\r
-                     HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_GUID), NULL),\r
+                     FormatHelpString,\r
                      GuidString\r
                    );\r
+  SECUREBOOT_FREE_NON_NULL (FormatHelpString);\r
+  FormatHelpString = NULL;\r
 \r
   //\r
   // Format content part, it depends on the type of signature list, hash value or CN.\r
   //\r
   if (IsCert) {\r
     GetCommonNameFromX509 (ListEntry, DataEntry, &DataString);\r
-    HelpInfoIndex += UnicodeSPrint(\r
-                       &HelpInfoString[HelpInfoIndex],\r
-                       TotalSize - sizeof(CHAR16) * HelpInfoIndex,\r
-                       HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_CN), NULL),\r
-                       HiiGetString (PrivateData->HiiHandle, ListTypeId, NULL),\r
-                       DataSize,\r
-                       DataString\r
-                     );\r
+    FormatHelpString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_CN), NULL);\r
   } else {\r
     //\r
     //  Format hash value for each signature data entry.\r
     //\r
     ParseHashValue (ListEntry, DataEntry, &DataString);\r
-    HelpInfoIndex += UnicodeSPrint (\r
-                       &HelpInfoString[HelpInfoIndex],\r
-                       TotalSize - sizeof(CHAR16) * HelpInfoIndex,\r
-                       HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_HASH), NULL),\r
-                       HiiGetString (PrivateData->HiiHandle, ListTypeId, NULL),\r
-                       DataSize,\r
-                       DataString\r
-                     );\r
+    FormatHelpString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_HASH), NULL);\r
+  }\r
+  if (FormatHelpString == NULL) {\r
+    goto ON_EXIT;\r
   }\r
+  HelpInfoIndex += UnicodeSPrint (\r
+                     &HelpInfoString[HelpInfoIndex],\r
+                     TotalSize - sizeof (CHAR16) * HelpInfoIndex,\r
+                     FormatHelpString,\r
+                     FormatTypeString,\r
+                     DataSize,\r
+                     DataString\r
+                   );\r
+  SECUREBOOT_FREE_NON_NULL (FormatHelpString);\r
+  FormatHelpString = NULL;\r
 \r
   //\r
   // Format revocation time part.\r
@@ -4032,13 +4060,18 @@ FormatHelpInfo (
       Time->Minute,\r
       Time->Second\r
     );\r
-\r
+    FormatHelpString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_TIME), NULL);\r
+    if (FormatHelpString == NULL) {\r
+      goto ON_EXIT;\r
+    }\r
     UnicodeSPrint (\r
       &HelpInfoString[HelpInfoIndex],\r
       TotalSize - sizeof (CHAR16) * HelpInfoIndex,\r
-      HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_HELP_FORMAT_TIME), NULL),\r
+      FormatHelpString,\r
       TimeString\r
     );\r
+    SECUREBOOT_FREE_NON_NULL (FormatHelpString);\r
+    FormatHelpString = NULL;\r
   }\r
 \r
   *StringId = HiiSetString (PrivateData->HiiHandle, 0, HelpInfoString, NULL);\r
@@ -4046,6 +4079,8 @@ ON_EXIT:
   SECUREBOOT_FREE_NON_NULL (DataString);\r
   SECUREBOOT_FREE_NON_NULL (HelpInfoString);\r
 \r
+  SECUREBOOT_FREE_NON_NULL (FormatTypeString);\r
+\r
   return Status;\r
 }\r
 \r
@@ -4076,6 +4111,7 @@ LoadSignatureData (
   EFI_IFR_GUID_LABEL    *StartLabel;\r
   EFI_IFR_GUID_LABEL    *EndLabel;\r
   EFI_STRING_ID         HelpStringId;\r
+  EFI_STRING            FormatNameString;\r
   VOID                  *StartOpCodeHandle;\r
   VOID                  *EndOpCodeHandle;\r
   UINTN                 DataSize;\r
@@ -4086,6 +4122,7 @@ LoadSignatureData (
   CHAR16                NameBuffer[BUFFER_MAX_SIZE];\r
 \r
   Status              = EFI_SUCCESS;\r
+  FormatNameString    = NULL;\r
   StartOpCodeHandle   = NULL;\r
   EndOpCodeHandle     = NULL;\r
   Index               = 0;\r
@@ -4167,17 +4204,18 @@ LoadSignatureData (
     ListWalker = (EFI_SIGNATURE_LIST *)((UINT8 *)ListWalker + ListWalker->SignatureListSize);\r
   }\r
 \r
+  FormatNameString = HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_NAME_FORMAT), NULL);\r
+  if (FormatNameString == NULL) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
   DataWalker = (EFI_SIGNATURE_DATA *)((UINT8 *)ListWalker + sizeof(EFI_SIGNATURE_LIST) + ListWalker->SignatureHeaderSize);\r
   for (Index = 0; Index < SIGNATURE_DATA_COUNTS(ListWalker); Index = Index + 1) {\r
     //\r
     // Format name buffer.\r
     //\r
     ZeroMem (NameBuffer, sizeof (NameBuffer));\r
-    UnicodeSPrint (NameBuffer,\r
-      sizeof (NameBuffer),\r
-      HiiGetString (PrivateData->HiiHandle, STRING_TOKEN (STR_SIGNATURE_DATA_NAME_FORMAT), NULL),\r
-      Index + 1\r
-    );\r
+    UnicodeSPrint (NameBuffer, sizeof (NameBuffer), FormatNameString, Index + 1);\r
 \r
     //\r
     // Format help info buffer.\r
@@ -4221,6 +4259,7 @@ ON_EXIT:
   SECUREBOOT_FREE_NON_OPCODE (EndOpCodeHandle);\r
 \r
   SECUREBOOT_FREE_NON_NULL (VariableData);\r
+  SECUREBOOT_FREE_NON_NULL (FormatNameString);\r
 \r
   return Status;\r
 }\r