[PATCH] OvmfPkg/AmdSev: Erase secret area content on ExitBootServices


Dov Murik
 

The confidential computing secrets area is marked as EfiBootServicesData
region, which means it is released for the OS use when the OS EFI stub
calls ExitBootServices. However, its content is not erased, and
therefore the OS might unintentionally reuse this sensitive memory area
and expose the injected secrets.

Erase the content of the secret area on ExitBootServices so that the
memory released to the OS contains zeros. If the OS needs to keep the
secrets for its own use, it must copy the secrets area to another memory
area before calling ExitBootServices (for example in efi/libstub in
Linux).

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Min Xu <min.m.xu@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Tobin Feldman-Fitzthum <tobin@linux.ibm.com>
Signed-off-by: Dov Murik <dovmurik@linux.ibm.com>
---

Code is in: https://github.com/confidential-containers-demo/edk2/tree/erase=
-secret-area

---
OvmfPkg/AmdSev/SecretDxe/SecretDxe.inf | 2 +
OvmfPkg/AmdSev/SecretDxe/SecretDxe.c | 47 ++++++++++++++++++--
2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/OvmfPkg/AmdSev/SecretDxe/SecretDxe.inf b/OvmfPkg/AmdSev/Secret=
Dxe/SecretDxe.inf
index 40bda7ff846c..ff831afaeb66 100644
--- a/OvmfPkg/AmdSev/SecretDxe/SecretDxe.inf
+++ b/OvmfPkg/AmdSev/SecretDxe/SecretDxe.inf
@@ -23,6 +23,8 @@ [Packages]
MdePkg/MdePkg.dec=0D
=0D
[LibraryClasses]=0D
+ BaseMemoryLib=0D
+ DebugLib=0D
UefiBootServicesTableLib=0D
UefiDriverEntryPoint=0D
=0D
diff --git a/OvmfPkg/AmdSev/SecretDxe/SecretDxe.c b/OvmfPkg/AmdSev/SecretDx=
e/SecretDxe.c
index 934ad207632b..085759f0e523 100644
--- a/OvmfPkg/AmdSev/SecretDxe/SecretDxe.c
+++ b/OvmfPkg/AmdSev/SecretDxe/SecretDxe.c
@@ -5,6 +5,8 @@
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
**/=0D
#include <PiDxe.h>=0D
+#include <Library/BaseMemoryLib.h>=0D
+#include <Library/DebugLib.h>=0D
#include <Library/UefiBootServicesTableLib.h>=0D
#include <Guid/ConfidentialComputingSecret.h>=0D
=0D
@@ -13,6 +15,35 @@ STATIC CONFIDENTIAL_COMPUTING_SECRET_LOCATION mSecretDxe=
Table =3D {
FixedPcdGet32 (PcdSevLaunchSecretSize),=0D
};=0D
=0D
+STATIC EFI_EVENT mSecretDxeExitBootEvent;=0D
+=0D
+/**=0D
+ ExitBootServices event notification function for the secret table.=0D
+=0D
+ This function erases the content of the secret area so the secrets don't=
leak=0D
+ via released BootServices memory. If the OS wants to keep the secrets f=
or=0D
+ its own use, it must copy the secrets area to another memory area before=
=0D
+ calling ExitBootServices (for example in efi/libstub in Linux).=0D
+=0D
+ @param[in] Event The ExitBoot event that has been signaled.=0D
+=0D
+ @param[in] Context Unused.=0D
+**/=0D
+STATIC=0D
+VOID=0D
+EFIAPI=0D
+SecretDxeExitBoot (=0D
+ IN EFI_EVENT Event,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ ASSERT(mSecretDxeTable.Base !=3D 0);=0D
+ ASSERT(mSecretDxeTable.Size > 0);=0D
+=0D
+ ZeroMem ((VOID *) ((UINTN) mSecretDxeTable.Base), mSecretDxeTable.Size);=
=0D
+}=0D
+=0D
+=0D
EFI_STATUS=0D
EFIAPI=0D
InitializeSecretDxe(=0D
@@ -20,8 +51,16 @@ InitializeSecretDxe(
IN EFI_SYSTEM_TABLE *SystemTable=0D
)=0D
{=0D
- return gBS->InstallConfigurationTable (=0D
- &gConfidentialComputingSecretGuid,=0D
- &mSecretDxeTable=0D
- );=0D
+ EFI_STATUS Status;=0D
+=0D
+ Status =3D gBS->InstallConfigurationTable (=0D
+ &gConfidentialComputingSecretGuid,=0D
+ &mSecretDxeTable=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ return gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,=0D
+ SecretDxeExitBoot, NULL, &mSecretDxeExitBootEvent);=0D
}=0D
--=20
2.25.1

Join devel@edk2.groups.io to automatically receive all group messages.