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@xxxxxxxxx> --- arch/x86/include/asm/microcode.h | 5 ++++ arch/x86/include/asm/sgx.h | 5 ++++ arch/x86/kernel/cpu/microcode/core.c | 44 ++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/main.c | 40 +++++++++++++++++++++++++ 4 files changed, 94 insertions(+) diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h index d6bfdfb0f0af..233e8cada691 100644 --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -3,6 +3,7 @@ #define _ASM_X86_MICROCODE_H #include <asm/cpu.h> +#include <asm/sgx.h> #include <linux/earlycpio.h> #include <linux/initrd.h> @@ -137,4 +138,8 @@ static inline void load_ucode_ap(void) { } static inline void reload_early_microcode(void) { } #endif +#ifndef update_cpusvn_intel +static inline int update_cpusvn_intel(void) { return -EINVAL; } +#endif + #endif /* _ASM_X86_MICROCODE_H */ diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h index d5942d0848ec..72b853fedf0c 100644 --- a/arch/x86/include/asm/sgx.h +++ b/arch/x86/include/asm/sgx.h @@ -412,4 +412,9 @@ 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 +int update_cpusvn_intel(void); +#define update_cpusvn_intel update_cpusvn_intel +#endif + #endif /* _ASM_X86_SGX_H */ diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c index f955d25076ba..3a78a6fa0787 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -39,6 +39,7 @@ #include <asm/processor.h> #include <asm/cmdline.h> #include <asm/setup.h> +#include <asm/sgx.h> #define DRIVER_VERSION "2.2" @@ -803,6 +804,33 @@ static int mc_cpu_down_prep(unsigned int cpu) return 0; } +static ssize_t svnupdate_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + unsigned long val; + ssize_t ret = 0; + + ret = kstrtoul(buf, 0, &val); + if (ret) + return ret; + + if (val != 1) + return size; + + mutex_lock(µcode_mutex); + + ret = update_cpusvn_intel(); + + mutex_unlock(µcode_mutex); + + if (ret == 0) + ret = size; + + return ret; +} +static DEVICE_ATTR_WO(svnupdate); + static struct attribute *cpu_root_microcode_attrs[] = { &dev_attr_reload.attr, NULL @@ -856,6 +884,22 @@ static int __init microcode_init(void) goto out_driver; } + /* + * If SGX driver is enabled, and CPUID bit for EUPDATESVN + * is on, allow svnupdate to occur. + */ + if (IS_ENABLED(CONFIG_X86_SGX) && + (cpuid_eax(SGX_CPUID) & SGX_CPUID_EUPDATESVN)) { + error = sysfs_add_file_to_group(&cpu_subsys.dev_root->kobj, + &dev_attr_svnupdate.attr, + "microcode"); + + if (error) { + pr_err("Error creating microcode svnupdate file!\n"); + goto out_ucode_group; + } + } + error = microcode_dev_init(); if (error) goto out_ucode_group; diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 7a464c8ac959..431a19e0ea41 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -1359,3 +1359,43 @@ static int sgx_updatesvn(void) return ret; } + +int update_cpusvn_intel(void) +{ + int ret; + + sgx_lock_epc(); + ret = sgx_zap_pages(); + if (ret) + goto out; + + ret = sgx_updatesvn(); + + switch (ret) { + case 0: + case SGX_NO_UPDATE: + ret = 0; + break; + case SGX_EPC_NOT_READY: + ret = -EBUSY; + break; + case SGX_INSUFFICIENT_ENTROPY: + pr_info("CPUSVN update is failed due to Insufficient entropy in RNG," + "please try it later.\n"); + ret = -EINVAL; + break; + case SGX_EPC_PAGE_CONFLICT: + pr_info("CPUSVN update is failed due to concurrency violation, please" + "stop running any other ENCLS leaf and try it later.\n"); + ret = -EINVAL; + break; + default: + ret = -EINVAL; + break; + } + +out: + sgx_unlock_epc(); + + return ret; +} -- 2.17.1