[PATCH v3 02/11] SecurityPkg: PlatformPKProtectionLib: Added PK protection interface


Kun Qin
 

From: Kun Qin <kuqin@...>

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3911

This patch provides an abstracted interface for platform to implement PK
variable related protection interface, which is designed to be used when
PK variable is about to be changed by UEFI firmware.

This change also provided a variable policy based library implementation
to accomodate platforms that supports variable policy for variable
protections.

Cc: Jiewen Yao <jiewen.yao@...>
Cc: Jian J Wang <jian.j.wang@...>
Cc: Min Xu <min.m.xu@...>

Signed-off-by: Kun Qin <kun.qin@...>
Reviewed-by: Jiewen Yao <Jiewen.yao@...>
Acked-by: Michael Kubacki <michael.kubacki@...>
---

Notes:
v3:
- Added reviewed-by tag [Jiewen]
- Added acked-by tag [Michael Kubacki]

SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformPKProtectionL=
ibVarPolicy.c | 51 ++++++++++++++++++++
SecurityPkg/Include/Library/PlatformPKProtectionLib.h =
| 31 ++++++++++++
SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformPKProtectionL=
ibVarPolicy.inf | 36 ++++++++++++++
SecurityPkg/SecurityPkg.dec =
| 5 ++
SecurityPkg/SecurityPkg.dsc =
| 2 +
5 files changed, 125 insertions(+)

diff --git a/SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformP=
KProtectionLibVarPolicy.c b/SecurityPkg/Library/PlatformPKProtectionLibVarP=
olicy/PlatformPKProtectionLibVarPolicy.c
new file mode 100644
index 000000000000..a2649242246f
--- /dev/null
+++ b/SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformPKProtec=
tionLibVarPolicy.c
@@ -0,0 +1,51 @@
+/** @file=0D
+ Provides an abstracted interface for configuring PK related variable pro=
tection.=0D
+=0D
+ Copyright (c) Microsoft Corporation.=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+#include <Uefi.h>=0D
+#include <Protocol/VariablePolicy.h>=0D
+=0D
+#include <Library/DebugLib.h>=0D
+#include <Library/UefiBootServicesTableLib.h>=0D
+=0D
+/**=0D
+ Disable any applicable protection against variable 'PK'. The implementat=
ion=0D
+ of this interface is platform specific, depending on the protection tech=
niques=0D
+ used per platform.=0D
+=0D
+ Note: It is the platform's responsibility to conduct cautious operation =
after=0D
+ disabling this protection.=0D
+=0D
+ @retval EFI_SUCCESS State has been successfully updated.=
=0D
+ @retval Others Error returned from implementation s=
pecific=0D
+ underying APIs.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+DisablePKProtection (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy;=0D
+=0D
+ DEBUG ((DEBUG_INFO, "%a() Entry...\n", __FUNCTION__));=0D
+=0D
+ // IMPORTANT NOTE: This operation is sticky and leaves variable protecti=
ons disabled.=0D
+ // The system *MUST* be reset after performing this ope=
ration.=0D
+ Status =3D gBS->LocateProtocol (&gEdkiiVariablePolicyProtocolGuid, NULL,=
(VOID **)&VariablePolicy);=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D VariablePolicy->DisableVariablePolicy ();=0D
+ // EFI_ALREADY_STARTED means that everything is currently disabled.=0D
+ // This should be considered SUCCESS.=0D
+ if (Status =3D=3D EFI_ALREADY_STARTED) {=0D
+ Status =3D EFI_SUCCESS;=0D
+ }=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
diff --git a/SecurityPkg/Include/Library/PlatformPKProtectionLib.h b/Securi=
tyPkg/Include/Library/PlatformPKProtectionLib.h
new file mode 100644
index 000000000000..3586a47b77c2
--- /dev/null
+++ b/SecurityPkg/Include/Library/PlatformPKProtectionLib.h
@@ -0,0 +1,31 @@
+/** @file=0D
+ Provides an abstracted interface for configuring PK related variable pro=
tection.=0D
+=0D
+ Copyright (c) Microsoft Corporation.=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#ifndef PLATFORM_PK_PROTECTION_LIB_H_=0D
+#define PLATFORM_PK_PROTECTION_LIB_H_=0D
+=0D
+/**=0D
+ Disable any applicable protection against variable 'PK'. The implementat=
ion=0D
+ of this interface is platform specific, depending on the protection tech=
niques=0D
+ used per platform.=0D
+=0D
+ Note: It is the platform's responsibility to conduct cautious operation =
after=0D
+ disabling this protection.=0D
+=0D
+ @retval EFI_SUCCESS State has been successfully updated.=
=0D
+ @retval Others Error returned from implementation s=
pecific=0D
+ underying APIs.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+DisablePKProtection (=0D
+ VOID=0D
+ );=0D
+=0D
+#endif=0D
diff --git a/SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformP=
KProtectionLibVarPolicy.inf b/SecurityPkg/Library/PlatformPKProtectionLibVa=
rPolicy/PlatformPKProtectionLibVarPolicy.inf
new file mode 100644
index 000000000000..df42ce06c019
--- /dev/null
+++ b/SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformPKProtec=
tionLibVarPolicy.inf
@@ -0,0 +1,36 @@
+## @file=0D
+# Provides an abstracted interface for configuring PK related variable pr=
otection.=0D
+#=0D
+# Copyright (c) Microsoft Corporation.=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010005=0D
+ BASE_NAME =3D PlatformPKProtectionLibVarPolicy=0D
+ FILE_GUID =3D AE0C5992-526C-4518-93BA-3C2611B801E0=
=0D
+ MODULE_TYPE =3D DXE_DRIVER=0D
+ VERSION_STRING =3D 1.0=0D
+ LIBRARY_CLASS =3D PlatformPKProtectionLib|DXE_DRIVER DX=
E_RUNTIME_DRIVER UEFI_APPLICATION=0D
+=0D
+#=0D
+# The following information is for reference only and not required by the =
build tools.=0D
+#=0D
+# VALID_ARCHITECTURES =3D IA32 X64 AARCH64=0D
+#=0D
+=0D
+[Sources]=0D
+ PlatformPKProtectionLibVarPolicy.c=0D
+=0D
+[Packages]=0D
+ MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
+ SecurityPkg/SecurityPkg.dec=0D
+=0D
+[LibraryClasses]=0D
+ DebugLib=0D
+ UefiBootServicesTableLib=0D
+=0D
+[Protocols]=0D
+ gEdkiiVariablePolicyProtocolGuid=0D
diff --git a/SecurityPkg/SecurityPkg.dec b/SecurityPkg/SecurityPkg.dec
index 0ee75efc1a97..7ecf9565d98c 100644
--- a/SecurityPkg/SecurityPkg.dec
+++ b/SecurityPkg/SecurityPkg.dec
@@ -99,6 +99,11 @@ [LibraryClasses]
## @libraryclass Provides support to enroll Secure Boot keys.=0D
#=0D
SecureBootVariableProvisionLib|Include/Library/SecureBootVariableProvisi=
onLib.h=0D
+=0D
+ ## @libraryclass Provides support to manage variable 'PK' related prote=
ctions.=0D
+ #=0D
+ PlatformPKProtectionLib|Include/Library/PlatformPKProtectionLib.h=0D
+=0D
[Guids]=0D
## Security package token space guid.=0D
# Include/Guid/SecurityPkgTokenSpace.h=0D
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index d883747474e4..f48187650f2f 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -71,6 +71,7 @@ [LibraryClasses]
TcgEventLogRecordLib|SecurityPkg/Library/TcgEventLogRecordLib/TcgEventLo=
gRecordLib.inf=0D
MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibN=
ull.inf=0D
SecureBootVariableLib|SecurityPkg/Library/SecureBootVariableLib/SecureBo=
otVariableLib.inf=0D
+ PlatformPKProtectionLib|SecurityPkg/Library/PlatformPKProtectionLibVarPo=
licy/PlatformPKProtectionLibVarPolicy.inf=0D
SecureBootVariableProvisionLib|SecurityPkg/Library/SecureBootVariablePro=
visionLib/SecureBootVariableProvisionLib.inf=0D
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf=0D
=0D
@@ -261,6 +262,7 @@ [Components]
#=0D
SecurityPkg/Library/VariableKeyLibNull/VariableKeyLibNull.inf=0D
SecurityPkg/Library/RpmcLibNull/RpmcLibNull.inf=0D
+ SecurityPkg/Library/PlatformPKProtectionLibVarPolicy/PlatformPKProtectio=
nLibVarPolicy.inf=0D
=0D
#=0D
# Other=0D
--=20
2.36.0.windows.1