From: "PierreGondois" <pierre.gondois@arm.com>
To: devel@edk2.groups.io
Cc: Sami Mujawar <sami.mujawar@arm.com>,
Leif Lindholm <quic_llindhol@quicinc.com>,
Ard Biesheuvel <ardb+tianocore@kernel.org>,
Rebecca Cran <rebecca@bsdio.com>,
Michael D Kinney <michael.d.kinney@intel.com>,
Liming Gao <gaoliming@byosoft.com.cn>,
Jiewen Yao <jiewen.yao@intel.com>,
Jian J Wang <jian.j.wang@intel.com>
Subject: [PATCH v3 17/22] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo
Date: Wed, 29 Jun 2022 17:02:32 +0200 [thread overview]
Message-ID: <20220629150241.2597898-24-Pierre.Gondois@arm.com> (raw)
In-Reply-To: <20220629150241.2597898-1-Pierre.Gondois@arm.com>
From: Pierre Gondois <pierre.gondois@arm.com>
RngGetBytes() relies on the RngLib. The RngLib might use the RNDR
instruction if the FEAT_RNG feature is present. Check RngGetBytes() is
working before advertising it via RngGetInfo().
To only check this one time, create a static array that is shared
between RngGetInfo and RngGetRNG. This array contains GUIDs.
The Rng algorithm with the lowest GUID and that has been checked
will be the default Rng algorithm.
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>
---
.../RngDxe/AArch64/RngDxe.c | 77 +++++++++++++++++--
1 file changed, 69 insertions(+), 8 deletions(-)
diff --git a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
index f9c740d761ff..d8b696bbea5f 100644
--- a/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
+++ b/SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
@@ -23,10 +23,44 @@
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
+#include <Library/RngLib.h>
#include <Protocol/Rng.h>
#include "RngDxeInternals.h"
+//
+// Static array containing the validated Rng algorithm.
+// This array is used by RngGetInfo and RngGetRNG and needs to be
+// populated only once.
+// The valid entry with the lowest index will be the default algorithm.
+//
+#define RNG_AVAILABLE_ALGO_MAX 1
+STATIC BOOLEAN mAvailableAlgoArrayInit = FALSE;
+STATIC UINTN mAvailableAlgoArrayCount;
+STATIC EFI_RNG_ALGORITHM mAvailableAlgoArray[RNG_AVAILABLE_ALGO_MAX];
+
+/** Initialize mAvailableAlgoArray with the available Rng algorithms.
+**/
+STATIC
+VOID
+EFIAPI
+RngInitAvailableAlgoArray (
+ VOID
+ )
+{
+ // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
+ if (!EFI_ERROR (RngGetBytes (sizeof (Rand), (UINT8 *)&Rand))) {
+ CopyMem (
+ &mAvailableAlgoArray[mAvailableAlgoArrayCount],
+ PcdGetPtr (PcdCpuRngSupportedAlgorithm),
+ sizeof (EFI_RNG_ALGORITHM)
+ );
+ mAvailableAlgoArrayCount++;
+ }
+
+ mAvailableAlgoArrayInit = TRUE;
+}
+
/**
Produces and returns an RNG value using either the default or specified RNG algorithm.
@@ -59,18 +93,35 @@ RngGetRNG (
)
{
EFI_STATUS Status;
+ UINTN Index;
if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
return EFI_INVALID_PARAMETER;
}
+ if (!mAvailableAlgoArrayInit) {
+ RngInitAvailableAlgoArray ();
+ }
+
if (RNGAlgorithm == NULL) {
//
// Use the default RNG algorithm if RNGAlgorithm is NULL.
//
- RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
+ for (Index = 0; Index < RNG_AVAILABLE_ALGO_MAX; Index++) {
+ if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
+ RNGAlgorithm = &mAvailableAlgoArray[Index];
+ goto FoundAlgo;
+ }
+ }
+
+ if (Index == RNG_AVAILABLE_ALGO_MAX) {
+ // No algorithm available.
+ ASSERT (Index != RNG_AVAILABLE_ALGO_MAX);
+ return EFI_DEVICE_ERROR;
+ }
}
+FoundAlgo:
if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
Status = RngGetBytes (RNGValueLength, RNGValue);
return Status;
@@ -113,24 +164,34 @@ RngGetInfo (
OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
)
{
- UINTN RequiredSize;
- EFI_RNG_ALGORITHM *CpuRngSupportedAlgorithm;
-
- RequiredSize = sizeof (EFI_RNG_ALGORITHM);
+ UINTN RequiredSize;
if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
return EFI_INVALID_PARAMETER;
}
+ if (!mAvailableAlgoArrayInit) {
+ RngInitAvailableAlgoArray ();
+ }
+
+ RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
+
+ if (RequiredSize == 0) {
+ // No supported algorithms found.
+ return EFI_UNSUPPORTED;
+ }
+
if (*RNGAlgorithmListSize < RequiredSize) {
*RNGAlgorithmListSize = RequiredSize;
return EFI_BUFFER_TOO_SMALL;
}
- CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
-
- CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
+ if (RNGAlgorithmList == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+ // There is no gap in the array, so copy the block.
+ CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
*RNGAlgorithmListSize = RequiredSize;
return EFI_SUCCESS;
}
--
2.25.1
next prev parent reply other threads:[~2022-06-29 15:04 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-29 15:02 [PATCH v3 00/22] Add Raw algorithm support using Arm FW-TRNG interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 01/22] ArmPkg: PCD to select conduit for monitor calls PierreGondois
2022-06-29 15:02 ` [PATCH v3 02/22] ArmPkg/ArmMonitorLib: Definition for ArmMonitorLib library class PierreGondois
2022-06-29 15:02 ` [PATCH v3 03/22] ArmPkg/ArmMonitorLib: Add ArmMonitorLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 04/22] ArmPkg/ArmHvcNullLib: Add NULL instance of ArmHvcLib PierreGondois
2022-06-29 15:02 ` [PATCH v3 05/22] MdePkg/TrngLib: Definition for TRNG library class interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 06/22] MdePkg/TrngLib: Add NULL instance of TRNG Library PierreGondois
2022-06-29 15:02 ` [PATCH v3 07/22] ArmPkg: Add FID definitions for Firmware TRNG PierreGondois
2022-06-29 15:02 ` [PATCH v3 08/22] ArmPkg/TrngLib: Add Arm Firmware TRNG library PierreGondois
2022-06-29 15:02 ` [PATCH v3 09/22] MdePkg/BaseRngLib: Rename ArmReadIdIsar0() to ArmGetFeatRng() PierreGondois
2022-06-29 15:02 ` [PATCH v3 10/22] ArmPkg/ArmLib: Add ArmReadIdIsar0() helper PierreGondois
2022-06-29 15:02 ` [PATCH v3 11/22] ArmPkg/ArmLib: Add ArmHasRngExt() PierreGondois
2022-06-29 15:02 ` [PATCH v3 12/21] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
2022-06-29 15:02 ` [PATCH v3 12/22] SecurityPkg: Update Securitypkg.ci.yaml PierreGondois
2022-06-29 15:02 ` [PATCH v3 13/22] SecurityPkg/RngDxe: Rename RdRandGenerateEntropy to generic name PierreGondois
2022-06-29 15:02 ` [PATCH v3 13/21] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
2022-06-29 15:02 ` [PATCH v3 14/21] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
2022-06-29 15:02 ` [PATCH v3 14/22] SecurityPkg/RngDxe: Replace Pcd with Sp80090Ctr256Guid PierreGondois
2022-07-21 10:51 ` Yao, Jiewen
2022-06-29 15:02 ` [PATCH v3 15/21] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
2022-06-29 15:02 ` [PATCH v3 15/22] SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms() PierreGondois
2022-06-29 15:02 ` [PATCH v3 16/21] SecurityPkg/RngDxe: Check before advertising Cpu Rng algo PierreGondois
2022-06-29 15:02 ` [PATCH v3 16/22] SecurityPkg/RngDxe: Documentation/include/parameter cleanup PierreGondois
2022-06-29 15:02 ` [PATCH v3 17/21] SecurityPkg/RngDxe: Add AArch64 RawAlgorithm support through TrngLib PierreGondois
2022-06-29 15:02 ` PierreGondois [this message]
2022-06-29 15:02 ` [PATCH v3 18/22] " PierreGondois
2022-06-29 15:02 ` [PATCH v3 18/21] SecurityPkg/RngDxe: Add debug warning for NULL PcdCpuRngSupportedAlgorithm PierreGondois
2022-06-29 15:02 ` [PATCH v3 19/22] " PierreGondois
2022-06-29 15:02 ` [PATCH v3 19/21] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
2022-06-29 15:02 ` [PATCH v3 20/21] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
2022-07-21 10:47 ` Yao, Jiewen
2022-06-29 15:02 ` [PATCH v3 20/22] SecurityPkg/RngDxe: Rename AArch64/RngDxe.c PierreGondois
2022-06-29 15:02 ` [PATCH v3 21/21] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
2022-06-29 15:02 ` [PATCH v3 21/22] SecurityPkg/RngDxe: Add Arm support of RngDxe PierreGondois
2022-06-29 15:02 ` [PATCH v3 22/22] ArmVirtPkg: Kvmtool: Add RNG support using FW-TRNG interface PierreGondois
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220629150241.2597898-24-Pierre.Gondois@arm.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox