]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg/RngDxe: Check before advertising Cpu Rng algo
authorPierre Gondois <pierre.gondois@arm.com>
Fri, 28 Oct 2022 15:32:54 +0000 (17:32 +0200)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sun, 6 Nov 2022 16:32:28 +0000 (16:32 +0000)
RngGetBytes() relies on the RngLib. The RngLib might use the RNDR
instruction if the FEAT_RNG feature is present. RngGetInfo and
RngGetRNG both must check that RngGetBytes() is working before
advertising/using it.

To do so, allocate an array storing the available algorithms.
The Rng algorithm at the lowest index will be the default Rng
algorithm. The array is shared between RngGetInfo and RngGetRNG.

This array is allocated when the driver is loaded, and freed
when unloaded.

This patch also prevents from having PcdCpuRngSupportedAlgorithm
let to a zero GUID, but let the possibility to have no valid Rng
algorithm in such case.

Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com>
Acked-by: Jiewen Yao <jiewen.yao@intel.com>
SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
SecurityPkg/RandomNumberGenerator/RngDxe/Rand/RngDxe.c
SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.inf
SecurityPkg/RandomNumberGenerator/RngDxe/RngDxeInternals.h

index f9c740d761ff8fb4c07c0ce4af2b1166adafe418..09a5924a699bc3abcab3967d6d6f7b7849f35149 100644 (file)
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/RngLib.h>\r
 #include <Protocol/Rng.h>\r
 \r
 #include "RngDxeInternals.h"\r
 \r
+// Maximum number of Rng algorithms.\r
+#define RNG_AVAILABLE_ALGO_MAX  1\r
+\r
+/** Allocate and initialize mAvailableAlgoArray with the available\r
+    Rng algorithms. Also update mAvailableAlgoArrayCount.\r
+\r
+  @retval EFI_SUCCESS             The function completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetAvailableAlgorithms (\r
+  VOID\r
+  )\r
+{\r
+  UINT64  DummyRand;\r
+\r
+  // Allocate RNG_AVAILABLE_ALGO_MAX entries to avoid evaluating\r
+  // Rng algorithms 2 times, one for the allocation, one to populate.\r
+  mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);\r
+  if (mAvailableAlgoArray == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.\r
+  if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {\r
+    CopyMem (\r
+      &mAvailableAlgoArray[mAvailableAlgoArrayCount],\r
+      PcdGetPtr (PcdCpuRngSupportedAlgorithm),\r
+      sizeof (EFI_RNG_ALGORITHM)\r
+      );\r
+    mAvailableAlgoArrayCount++;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/** Free mAvailableAlgoArray.\r
+**/\r
+VOID\r
+EFIAPI\r
+FreeAvailableAlgorithms (\r
+  VOID\r
+  )\r
+{\r
+  FreePool (mAvailableAlgoArray);\r
+  return;\r
+}\r
+\r
 /**\r
   Produces and returns an RNG value using either the default or specified RNG algorithm.\r
 \r
@@ -59,6 +111,7 @@ RngGetRNG (
   )\r
 {\r
   EFI_STATUS  Status;\r
+  UINTN       Index;\r
 \r
   if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -68,9 +121,21 @@ RngGetRNG (
     //\r
     // Use the default RNG algorithm if RNGAlgorithm is NULL.\r
     //\r
-    RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);\r
+    for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {\r
+      if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {\r
+        RNGAlgorithm = &mAvailableAlgoArray[Index];\r
+        goto FoundAlgo;\r
+      }\r
+    }\r
+\r
+    if (Index == mAvailableAlgoArrayCount) {\r
+      // No algorithm available.\r
+      ASSERT (Index != mAvailableAlgoArrayCount);\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
   }\r
 \r
+FoundAlgo:\r
   if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {\r
     Status = RngGetBytes (RNGValueLength, RNGValue);\r
     return Status;\r
@@ -113,24 +178,30 @@ RngGetInfo (
   OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList\r
   )\r
 {\r
-  UINTN              RequiredSize;\r
-  EFI_RNG_ALGORITHM  *CpuRngSupportedAlgorithm;\r
-\r
-  RequiredSize = sizeof (EFI_RNG_ALGORITHM);\r
+  UINTN  RequiredSize;\r
 \r
   if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);\r
+\r
+  if (RequiredSize == 0) {\r
+    // No supported algorithms found.\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   if (*RNGAlgorithmListSize < RequiredSize) {\r
     *RNGAlgorithmListSize = RequiredSize;\r
     return EFI_BUFFER_TOO_SMALL;\r
   }\r
 \r
-  CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);\r
-\r
-  CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));\r
+  if (RNGAlgorithmList == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
+  // There is no gap in the array, so copy the block.\r
+  CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);\r
   *RNGAlgorithmListSize = RequiredSize;\r
   return EFI_SUCCESS;\r
 }\r
index 8f5d8e740f5e41fe93fa447d151878b4882dfb24..677600bed7ab92bdff6a2bf9c3629f1b0f12bbe3 100644 (file)
 \r
 #include "RngDxeInternals.h"\r
 \r
+/** Allocate and initialize mAvailableAlgoArray with the available\r
+    Rng algorithms. Also update mAvailableAlgoArrayCount.\r
+\r
+  @retval EFI_SUCCESS             The function completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetAvailableAlgorithms (\r
+  VOID\r
+  )\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/** Free mAvailableAlgoArray.\r
+**/\r
+VOID\r
+EFIAPI\r
+FreeAvailableAlgorithms (\r
+  VOID\r
+  )\r
+{\r
+  return;\r
+}\r
+\r
 /**\r
   Produces and returns an RNG value using either the default or specified RNG algorithm.\r
 \r
index d7905a7f4d727761d008c30c462a5a7fe9fd2ff4..421abb52b8bf14fd39c1c2c59ea7a48b0b795805 100644 (file)
@@ -27,6 +27,13 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 \r
 #include "RngDxeInternals.h"\r
 \r
+//\r
+// Array containing the validated Rng algorithm.\r
+// The entry with the lowest index will be the default algorithm.\r
+//\r
+UINTN              mAvailableAlgoArrayCount;\r
+EFI_RNG_ALGORITHM  *mAvailableAlgoArray;\r
+\r
 //\r
 // The Random Number Generator (RNG) protocol\r
 //\r
@@ -66,8 +73,39 @@ RngDriverEntry (
                   &mRngRdRand,\r
                   NULL\r
                   );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Get the list of available algorithm.\r
+  //\r
+  return GetAvailableAlgorithms ();\r
+}\r
+\r
+/**\r
+  This is the unload handle for RndgDxe module.\r
+\r
+  Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
+  Uninstall all the protocols installed in the driver entry point.\r
 \r
-  return Status;\r
+  @param[in] ImageHandle           The drivers' driver image.\r
+\r
+  @retval    EFI_SUCCESS           The image is unloaded.\r
+  @retval    Others                Failed to unload the image.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RngDriverUnLoad (\r
+  IN EFI_HANDLE  ImageHandle\r
+  )\r
+{\r
+  //\r
+  // Free the list of available algorithm.\r
+  //\r
+  FreeAvailableAlgorithms ();\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
index 60efb5562ee0b083abada0015340b472ed91885b..1985dfbb461921fe8bfd9216d8ad24301440d382 100644 (file)
@@ -22,6 +22,7 @@
   MODULE_TYPE                    = DXE_DRIVER\r
   VERSION_STRING                 = 1.0\r
   ENTRY_POINT                    = RngDriverEntry\r
+  UNLOAD_IMAGE                   = RngDriverUnLoad\r
   MODULE_UNI_FILE                = RngDxe.uni\r
 \r
 #\r
index 7ecab140483def8505b34dd2d8998441b8ade430..f75140260820ab16e29e96ab5a6fe4f1f3643112 100644 (file)
 \r
 #include <Protocol/Rng.h>\r
 \r
+//\r
+// Array containing the validated Rng algorithm.\r
+// The entry with the lowest index will be the default algorithm.\r
+//\r
+extern UINTN              mAvailableAlgoArrayCount;\r
+extern EFI_RNG_ALGORITHM  *mAvailableAlgoArray;\r
+\r
+/** Allocate and initialize mAvailableAlgoArray with the available\r
+    Rng algorithms. Also update mAvailableAlgoArrayCount.\r
+\r
+  @retval EFI_SUCCESS             The function completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Could not allocate memory.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetAvailableAlgorithms (\r
+  VOID\r
+  );\r
+\r
+/** Free mAvailableAlgoArray.\r
+**/\r
+VOID\r
+EFIAPI\r
+FreeAvailableAlgorithms (\r
+  VOID\r
+  );\r
+\r
 /**\r
   Returns information about the random number generation implementation.\r
 \r