[PATCH v5 8/9] x86/cpu: Call ENCLS[EUPDATESVN] procedure in microcode update

Cathy Zhang
2022-05-20T10:41:41Z

EUPDATESVN is the SGX instruction which allows enclave attestation
to include information about updated microcode without a reboot.

Microcode updates which affect SGX require two phases:

1. Do the main microcode update
2. Make the new CPUSVN available for enclave attestation via
EUPDATESVN.

Before a EUPDATESVN can succeed, all enclave pages (EPC) must be
marked as unused in the SGX metadata (EPCM). This operation destroys
all preexisting SGX enclave data and metadata. This is by design and
mitigates the impact of vulnerabilities that may have compromised
enclaves or the SGX hardware itself prior to the update.

Signed-off-by: Cathy Zhang <cathy.zhang@intel.com>

---
Changes since v4:
- Remove #include <asm/sgx.h> from microcode.h which is not needed
in this file. (Borislav Petkov)

Changes since v3:
- Rename update_cpusvn_intel() as sgx_update_cpusvn_intel().
(Dave Hansen)
- Refine the comments when sgx_update_cpusvn_intel is called by
microcode_check(). (Borislav Petkov, Dave Hansen)
- Define both the 'static inline' stub *and* the declaration for
sgx_update_cpusvn_intel() in sgx.h. (Dave Hansen)

Changes since v1:
- Remove the sysfs file svnupdate. (Thomas Gleixner, Dave Hansen)
- Let late microcode load path call ENCLS[EUPDATESVN] procedure
directly. (Borislav Petkov)
- Redefine update_cpusvn_intel() to return void instead of int.
---
arch/x86/include/asm/sgx.h | 6 ++++++
arch/x86/kernel/cpu/common.c | 10 ++++++++++
arch/x86/kernel/cpu/sgx/main.c | 12 ++++++++++++
3 files changed, 28 insertions(+)

diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h
index 74bcb6841a4b..1321670a6338 100644
--- a/arch/x86/include/asm/sgx.h
+++ b/arch/x86/include/asm/sgx.h
@@ -409,4 +409,10 @@ int sgx_virt_einit(void __user *sigstruct, void __user *token,
int sgx_set_attribute(unsigned long *allowed_attributes,
unsigned int attribute_fd);

+#ifdef CONFIG_X86_SGX
+extern void sgx_update_cpusvn_intel(void);
+#else
+static inline void sgx_update_cpusvn_intel(void) {}
+#endif
+
#endif /* _ASM_X86_SGX_H */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index e342ae4db3c4..a50e0e183139 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -59,6 +59,7 @@
#include <asm/cpu_device_id.h>
#include <asm/uv/uv.h>
#include <asm/sigframe.h>
+#include <asm/sgx.h>
#include <asm/traps.h>

#include "cpu.h"
@@ -2224,6 +2225,15 @@ void microcode_check(void)

perf_check_microcode();

+ /*
+ * SGX attestation incorporates the microcode versions of all processors
+ * on the system and is affected by microcode updates. So, update SGX
+ * attestation metric (called CPUSVN) to ensure enclaves attest to the
+ * new version after microcode update.
+ */
+ if (IS_ENABLED(CONFIG_X86_SGX) && (cpuid_eax(SGX_CPUID) & SGX_CPUID_EUPDATESVN))
+ sgx_update_cpusvn_intel();
+
/* Reload CPUID max function as it might've changed. */
info.cpuid_level = cpuid_eax(0);

diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 43dd2b34e040..0f541d01e561 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -1403,3 +1403,15 @@ static int sgx_updatesvn(void)

return ret;
}
+
+void sgx_update_cpusvn_intel(void)
+{
+ sgx_lock_epc();
+ if (sgx_zap_pages())
+ goto out;
+
+ sgx_updatesvn();
+
+out:
+ sgx_unlock_epc();
+}
--
2.17.1