Message ID | 20220309104050.18207-10-cathy.zhang@intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Support microcode updates affecting SGX | expand |
On all your patches for the future: don't forget to Cc LKML. On Wed, Mar 09, 2022 at 06:40:48PM +0800, Cathy Zhang wrote: > 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> > --- > arch/x86/include/asm/microcode.h | 5 ++++ > arch/x86/include/asm/sgx.h | 5 ++++ > arch/x86/kernel/cpu/microcode/core.c | 44 ++++++++++++++++++++++++++++ Why is all this code here at all? What does that have *actually* to do with microcode loading? AFAICT, you want to hook into microcode_check() which runs after the microcode update and do your EUPDATESVN there...
On 3/9/22 03:20, Borislav Petkov wrote: > AFAICT, you want to hook into microcode_check() which runs after the > microcode update and do your EUPDATESVN there... There's a little bit in the cover letter that _implies_ why EUPDATESVN isn't called during the actual microcode update: > This series implements the infrastructure needed to track and tear > down bare-metal enclaves and then run EUPDATESVN. This is expected > to be triggered by administrators via sysfs at some convenient time > after a microcode update, probably by the microcode update tooling > itself. This allows the (non-destructive) ucode update and the destructive EUPDATESVN procedure to happen at different times. If we just want to make the ucode update itself call EUPDATESVN via microcode_check(), that makes the ucode update itself destructive to SGX enclaves. That's not the end of the world, but this series is going to some amount of trouble (including new ABI) to avoid it. Perhaps we need to hear more about why this is so much of an issue.
On Wed, Mar 09, 2022 at 07:42:21AM -0800, Dave Hansen wrote: > There's a little bit in the cover letter that _implies_ why EUPDATESVN > isn't called during the actual microcode update: > > > This series implements the infrastructure needed to track and tear > > down bare-metal enclaves and then run EUPDATESVN. This is expected > > to be triggered by administrators via sysfs at some convenient time > > after a microcode update, probably by the microcode update tooling > > itself. > > This allows the (non-destructive) ucode update and the destructive > EUPDATESVN procedure to happen at different times. Which means, that this has even less to do with the microcode loader. That whole glue can be somewhere in arch/x86/...sgx/ land and be completely independent. > If we just want to make the ucode update itself call EUPDATESVN via > microcode_check(), that makes the ucode update itself destructive to SGX > enclaves. That's not the end of the world, but this series is going to > some amount of trouble (including new ABI) to avoid it. > > Perhaps we need to hear more about why this is so much of an issue. Yah, it all sounds weird.
> On all your patches for the future: don't forget to Cc LKML.
Thanks Boris! I will do it.
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; +}
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> --- 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(+)