Skip to contentSkip to conversation

Support microcode updates affecting SGX

PATCH In review Cathy Zhang 4 participants 18 messages May 20, 2022 – May 24, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover Support microcode updates affecting SGX

v4:
https://lore.kernel.org/all/[email protected]/T/

Changes since v4:
- Add "goto err" for *if* branch and remove *else* branch for less branching for common (success) case in sgx_ioc_enclave_create(). (Jarkko Sakkinen)
- Add back the blank line removed unintenitonally in encl.h. (Jarkko Sakkinen)
- Move *-EBUSY* into one line in the three sgx_zap_* functions' annotates. (Jarkko Sakkinen).
- Remove #include <asm/sgx.h> from microcode.h which is not needed. (Borislav Petkov)

Changes since v3:
- Refine the comments when sgx_update_cpusvn_intel() is called by microcode_check(). (suggested by Borislav Petkov, Dave Hansen)
- Rename update_cpusvn_intel() as sgx_update_cpusvn_intel(). (suggested by Dave Hansen)
- Define both the 'static inline' stub *and* the declaration for sgx_update_cpusvn_intel() in sgx.h. (suggested by Dave Hansen)
- Squash patch "x86/sgx: Provide VA page non-NULL owner" and "x86/sgx: Save enclave pointer for VA page". Update commit log. (suggested by Jarkko Sakkinen)
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST. (suggested by Jarkko, Sakkinen)
- Refer to changelogs for other changes.

Changes since v2:
- Changes are made in patch "x86/sgx: Introduce mechanism to prevent new initializations of EPC pages" by moving SGX2 related changes out. It allows this series to be applied on tip/x86/sgx branch with only picking up some auxiliary changes from SGX2 series, rather than depend on the whole set. (Jarkko Sakkinen, Reinette Chatre)

Changes since v1:
- Remove the sysfs file svnupdate. (Thomas Gleixner, Dave Hansen)
- Let late microcode load path call ENCLS[EUPDATESVN] procedure directly. (Borislav Petkov)
- Update cover letter by removing saying that "...triggered by administrators via sysfs...".
- Drop the patch for documentation change.

cover letter:

== General Microcode Background ==

Historically, microcode updates are applied by the BIOS or early in boot. In recent years, several trends have made these old approaches less palatable.

First, the cadence of microcode updates has increased to deliver security mitigations. Second, the value of those updates has increased, meaning that any delay in applying them is unacceptable. Third, users have become accustomed to approaches like hot patching their kernels and have a growing aversion to reboots in general.

Users want microcode updates to behave more like a hot patching a kernel and less like a BIOS update.

== SGX Attestation Background ==

SGX enclaves have an attestation mechanism. An enclave might, for instance, need to attest to its state before it is given a special decryption key. Since SGX must trust the CPU microcode, attestation incorporates the microcode versions of all processors on the system and is affected by microcode updates. This allows the entity to which the enclave is attesting to make deployment decisions based on the microcode version. For example, an enclave might be denied a decryption key if it runs on a system that has old microcode without a specific mitigation.

Unfortunately, this attestation metric (called CPUSVN) is only a snapshot. When the kernel first uses SGX (successfully executes any ENCLS instruction), SGX inspects all CPUs in the system and incorporates a record of their microcode versions into CPUSVN. Today, that value is locked and is not updated until a reboot.

== Problems ==

This means that, although the microcode may be update, enclaves can never attest to this fact. Enclaves are stuck attesting to the old version until a reboot.

Old enclaves created before the microcode update are presumed to be compromised must not be allowed to attest with the new microcode version.

== Solution ==

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

Whenever a microcode update affects SGX, the SGX attestation architecture assumes that all running enclaves and cryptographic assets (like internal SGX encryption keys) have been compromised. To mitigate the impact of this presumed compromise, EUPDATESVN success requires that all SGX memory to be marked as "unused" and its contents destroyed. This requirement ensures that no compromised enclave can survive the EUPDATESVN procedure and provides an opportunity to generate new cryptographic assets.

This series implements the infrastructure needed to track and tear down bare-metal enclaves and then run EUPDATESVN, it will be called by the late microcode load path after the microcode update.

This is a very slow operation. It is, of course, exceedingly disruptive to enclaves but should be infrequent as microcode updates are released on the order of every few months. Also, this is not the first piece of the SGX architecture which will destroy all enclave contents.

A follow-on series will add Virtual EPC (KVM guest) support.

Here is the spec for your reference:
https://cdrdv2.intel.com/v1/dl/getContent/648682?explicitVersion=true

This is series is based on tip/x86/sgx with the following additionally applied:

"x86/sgx: Export sgx_encl_ewb_cpumask()"
https://lore.kernel.org/lkml/[email protected]/T/#mc6d998d583c9fa512f25219f477353c3fbd214a0
"x86/sgx: Rename sgx_encl_ewb_cpumask() as sgx_encl_cpumask()"
https://lore.kernel.org/lkml/[email protected]/T/#me953d1983c1749daeec25f86d0f3ff09cede6c7a
"x86/sgx: Make sgx_ipi_cb() available internally"
https://lore.kernel.org/lkml/[email protected]/T/#m5f32fbecb20b34e7745d8bd30c5d133d588b15dc
"x86/sgx: Keep record of SGX page type"
https://lore.kernel.org/lkml/[email protected]/T/#m3a218f751b2e41950068dbcccf465426d0ec771e

Cathy Zhang (9):
x86/sgx: Introduce mechanism to prevent new initializations of EPC
pages
x86/sgx: Save enclave pointer for VA page
x86/sgx: Keep record for SGX VA and Guest page type x86/sgx: Save the size of each EPC section
x86/sgx: Forced EPC page zapping for EUPDATESVN x86/sgx: Define error codes for ENCLS[EUPDATESVN] x86/sgx: Implement ENCLS[EUPDATESVN]
x86/cpu: Call ENCLS[EUPDATESVN] procedure in microcode update x86/sgx: Call ENCLS[EUPDATESVN] during SGX initialization

 arch/x86/include/asm/sgx.h      |  49 ++--
 arch/x86/kernel/cpu/sgx/encl.h  |   3 +-
 arch/x86/kernel/cpu/sgx/encls.h |  14 +
 arch/x86/kernel/cpu/sgx/sgx.h   |  23 +-
 arch/x86/kernel/cpu/common.c    |  10 +
 arch/x86/kernel/cpu/sgx/encl.c  |  39 ++-
 arch/x86/kernel/cpu/sgx/ioctl.c |  51 +++-
 arch/x86/kernel/cpu/sgx/main.c  | 456 +++++++++++++++++++++++++++++++-
 arch/x86/kernel/cpu/sgx/virt.c  |  22 ++
 9 files changed, 636 insertions(+), 31 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/9 x86/sgx: Introduce mechanism to prevent new initializations of EPC pages

== Background ==

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

The SGX hardware maintains metadata for each enclave page to help enforce its security guarantees. This includes things like a record of the enclave to which the page belongs and the type of the page:
SGX metadata like "VA" or "SECS" pages, or regular enclave pages like those that store user data.

Before an EUPDATESVN operation can be successful, all SGX memory (aka. EPC) must be marked as "unused" in the SGX hardware metadata (aka, EPCM). The SGX microcode now maintains a reference count of pages which are unused to aid in determining when all pages reach the "unused" state.

Both bare-metal and KVM guest EPC must be made unused. To increase the chance of a successful EUPDATESVN, the kernel prevents existing enclaves from creating new, valid pages and prevents new enclave creation (creating an enclave involves initializing a "SECS" page).

The entire EUPDATESVN process is very slow since it potentially affects gigabytes of enclave memory. It can potentially take seconds or minutes to complete. Userspace may encounter -EBUSY errors during the update and is expected to retry.

== Patch contents ==

Introduce mechanism to prevent new initializations of EPC pages.

Use a flag to indicate when SGX EPC pages are "locked", which means it's not allowed to allocate new EPC page for use. Check it in all paths that can initialize an EPC page. Use SRCU to ensure that the flag is visible across the system before proceeding with an update.

Add checks to all sites that call SGX instructions that can transition pages from unused to initialized to ensure that the SRCU lock is held.

---
Changes since v4:
- Add "goto err" for *if* branch and remove *else* branch for less branching for common (success) case in sgx_ioc_enclave_create(). (Jarkko Sakkinen)

Changes since v3:
- Rename lable "out" as "err" in sgx_ioc_enclave_create, update error branch accordingly. (Jarrko Sakkinen)

Changes since v2:
- Move out the SGX2 related change to remove the dependency. (Jarkko Sakkinen, Reinette Chatre).

---
 arch/x86/kernel/cpu/sgx/encls.h |  8 ++++++
 arch/x86/kernel/cpu/sgx/sgx.h   |  3 +++
 arch/x86/kernel/cpu/sgx/encl.c  | 18 +++++++++++++
 arch/x86/kernel/cpu/sgx/ioctl.c | 48 ++++++++++++++++++++++++++-------
 arch/x86/kernel/cpu/sgx/main.c  | 37 +++++++++++++++++++++++++
 arch/x86/kernel/cpu/sgx/virt.c  | 20 ++++++++++++++
 6 files changed, 125 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/sgx/encls.h +8 -0
index fa04a73daf9c..60321c5f5718 100644
@@ -138,6 +138,8 @@ static inline bool encls_failed(int ret)
 
 static inline int __ecreate(struct sgx_pageinfo *pginfo, void *secs)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(ECREATE, pginfo, secs);
 }
 
@@ -148,6 +150,8 @@ static inline int __eextend(void *secs, void *addr)
 
 static inline int __eadd(struct sgx_pageinfo *pginfo, void *addr)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(EADD, pginfo, addr);
 }
 
@@ -179,6 +183,8 @@ static inline int __etrack(void *addr)
 static inline int __eldu(struct sgx_pageinfo *pginfo, void *addr,
 			 void *va)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_ret_3(ELDU, pginfo, addr, va);
 }
 
@@ -191,6 +197,8 @@ static inline int __epa(void *addr)
 {
 	unsigned long rbx = SGX_PAGE_TYPE_VA;
 
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(EPA, rbx, addr);
 }
 
arch/x86/kernel/cpu/sgx/sgx.h +3 -0
index b30cee4de903..d7a1490d90bb 100644
@@ -103,4 +103,7 @@ static inline int __init sgx_vepc_init(void)
 
 void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
 
+extern struct srcu_struct sgx_lock_epc_srcu;
+bool sgx_epc_is_locked(void);
+
 #endif /* _X86_SGX_H */
arch/x86/kernel/cpu/sgx/encl.c +18 -0
index 9e01548f4586..39f7f5dc09fd 100644
@@ -284,6 +284,7 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	unsigned long phys_addr;
 	struct sgx_encl *encl;
 	vm_fault_t ret;
+	int srcu_idx;
 
 	encl = vma->vm_private_data;
 
@@ -295,11 +296,18 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	if (unlikely(!encl))
 		return VM_FAULT_SIGBUS;
 
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+	if (sgx_epc_is_locked()) {
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+		return VM_FAULT_SIGBUS;
+	}
+
 	mutex_lock(&encl->lock);
 
 	entry = sgx_encl_load_page(encl, addr, vma->vm_flags);
 	if (IS_ERR(entry)) {
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		if (PTR_ERR(entry) == -EBUSY)
 			return VM_FAULT_NOPAGE;
@@ -312,12 +320,14 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	ret = vmf_insert_pfn(vma, addr, PFN_DOWN(phys_addr));
 	if (ret != VM_FAULT_NOPAGE) {
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		return VM_FAULT_SIGBUS;
 	}
 
 	sgx_encl_test_and_clear_young(vma->vm_mm, entry);
 	mutex_unlock(&encl->lock);
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 	return VM_FAULT_NOPAGE;
 }
@@ -465,6 +475,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 	struct sgx_encl_page *entry = NULL;
 	char data[sizeof(unsigned long)];
 	unsigned long align;
+	int srcu_idx;
 	int offset;
 	int cnt;
 	int ret = 0;
@@ -481,6 +492,12 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 		return -EFAULT;
 
 	for (i = 0; i < len; i += cnt) {
+		srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+		if (sgx_epc_is_locked()) {
+			ret = -EBUSY;
+			goto out;
+		}
+
 		entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK,
 					      vma->vm_flags);
 		if (IS_ERR(entry)) {
@@ -508,6 +525,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 
 out:
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		if (ret)
 			break;
arch/x86/kernel/cpu/sgx/ioctl.c +39 -9

77 lines not on this page · raw message

arch/x86/kernel/cpu/sgx/main.c +37 -0
index 08c00c325cc6..ec2775652a50 100644
@@ -23,6 +23,17 @@ static int sgx_nr_epc_sections;
 static struct task_struct *ksgxd_tsk;
 static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);
 static DEFINE_XARRAY(sgx_epc_address_space);
+/*
+ * The flag sgx_epc_locked prevents any new SGX flows that
+ * may attempt to allocate a new EPC page.
+ */
+static bool __rcu sgx_epc_locked;
+/*
+ * By synchronizing around sgx_epc_locked SRCU ensures that any executing
+ * SGX flows have completed before proceeding with an SVN update. New SGX flows
+ * will be prevented from starting during an SVN update.
+ */
+DEFINE_SRCU(sgx_lock_epc_srcu);
 
 /*
  * These variables are part of the state of the reclaimer, and must be accessed
@@ -404,6 +415,8 @@ static bool sgx_should_reclaim(unsigned long watermark)
 
 static int ksgxd(void *p)
 {
+	int srcu_idx;
+
 	set_freezable();
 
 	/*
@@ -424,9 +437,15 @@ static int ksgxd(void *p)
 				     kthread_should_stop() ||
 				     sgx_should_reclaim(SGX_NR_HIGH_PAGES));
 
+		srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+		if (sgx_epc_is_locked())
+			goto maybe_resched;
+
 		if (sgx_should_reclaim(SGX_NR_HIGH_PAGES))
 			sgx_reclaim_pages();
 
+maybe_resched:
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 		cond_resched();
 	}
 
@@ -969,3 +988,21 @@ static int __init sgx_init(void)
 }
 
 device_initcall(sgx_init);
+
+static void sgx_lock_epc(void)
+{
+	sgx_epc_locked = true;
+	synchronize_srcu(&sgx_lock_epc_srcu);
+}
+
+static void sgx_unlock_epc(void)
+{
+	sgx_epc_locked = false;
+	synchronize_srcu(&sgx_lock_epc_srcu);
+}
+
+bool sgx_epc_is_locked(void)
+{
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+	return sgx_epc_locked;
+}
arch/x86/kernel/cpu/sgx/virt.c +20 -0
index 6a77a14eee38..e953816d7c8b 100644
@@ -75,10 +75,21 @@ static vm_fault_t sgx_vepc_fault(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	struct sgx_vepc *vepc = vma->vm_private_data;
+	int srcu_idx;
 	int ret;
 
 	mutex_lock(&vepc->lock);
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+
+	if (sgx_epc_is_locked()) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
 	ret = __sgx_vepc_fault(vepc, vma, vmf->address);
+
+out_unlock:
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 	mutex_unlock(&vepc->lock);
 
 	if (!ret)
@@ -331,6 +342,7 @@ int __init sgx_vepc_init(void)
 int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 		     int *trapnr)
 {
+	int srcu_idx;
 	int ret;
 
 	/*
@@ -347,6 +359,12 @@ int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 	if (WARN_ON_ONCE(!access_ok(secs, PAGE_SIZE)))
 		return -EINVAL;
 
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+	if (sgx_epc_is_locked()) {
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+		return -EBUSY;
+	}
+
 	__uaccess_begin();
 	ret = __ecreate(pageinfo, (void *)secs);
 	__uaccess_end();
@@ -356,6 +374,8 @@ int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 		return -EFAULT;
 	}
 
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+
 	/* ECREATE doesn't return an error code, it faults or succeeds. */
 	WARN_ON_ONCE(ret);
 	return 0;
Signature · 1 line
2.17.1
maintainer Reviewed-by on v5 1/9
#2 lore raw

Re: [PATCH v5 1/9] x86/sgx: Introduce mechanism to prevent new initializations of EPC pages

Quoted 402 lines · Cathy Zhang

== Background ==

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

The SGX hardware maintains metadata for each enclave page to help enforce its security guarantees. This includes things like a record of the enclave to which the page belongs and the type of the page:
SGX metadata like "VA" or "SECS" pages, or regular enclave pages like those that store user data.

Before an EUPDATESVN operation can be successful, all SGX memory (aka. EPC) must be marked as "unused" in the SGX hardware metadata (aka, EPCM). The SGX microcode now maintains a reference count of pages which are unused to aid in determining when all pages reach the "unused" state.

Both bare-metal and KVM guest EPC must be made unused. To increase the chance of a successful EUPDATESVN, the kernel prevents existing enclaves from creating new, valid pages and prevents new enclave creation (creating an enclave involves initializing a "SECS" page).

The entire EUPDATESVN process is very slow since it potentially affects gigabytes of enclave memory. It can potentially take seconds or minutes to complete. Userspace may encounter -EBUSY errors during the update and is expected to retry.

== Patch contents ==

Introduce mechanism to prevent new initializations of EPC pages.

Use a flag to indicate when SGX EPC pages are "locked", which means it's not allowed to allocate new EPC page for use. Check it in all paths that can initialize an EPC page. Use SRCU to ensure that the flag is visible across the system before proceeding with an update.

Add checks to all sites that call SGX instructions that can transition pages from unused to initialized to ensure that the SRCU lock is held.

---
Changes since v4:
- Add "goto err" for *if* branch and remove *else* branch for less branching for common (success) case in sgx_ioc_enclave_create(). (Jarkko Sakkinen)

Changes since v3:
- Rename lable "out" as "err" in sgx_ioc_enclave_create, update error branch accordingly. (Jarrko Sakkinen)

Changes since v2:
- Move out the SGX2 related change to remove the dependency. (Jarkko Sakkinen, Reinette Chatre).

---
 arch/x86/kernel/cpu/sgx/encls.h |  8 ++++++
 arch/x86/kernel/cpu/sgx/sgx.h   |  3 +++
 arch/x86/kernel/cpu/sgx/encl.c  | 18 +++++++++++++
 arch/x86/kernel/cpu/sgx/ioctl.c | 48 ++++++++++++++++++++++++++-------
 arch/x86/kernel/cpu/sgx/main.c  | 37 +++++++++++++++++++++++++
 arch/x86/kernel/cpu/sgx/virt.c  | 20 ++++++++++++++
 6 files changed, 125 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/sgx/encls.h +8 -0
index fa04a73daf9c..60321c5f5718 100644
@@ -138,6 +138,8 @@ static inline bool encls_failed(int ret)
 
 static inline int __ecreate(struct sgx_pageinfo *pginfo, void *secs)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(ECREATE, pginfo, secs);
 }
 
@@ -148,6 +150,8 @@ static inline int __eextend(void *secs, void *addr)
 
 static inline int __eadd(struct sgx_pageinfo *pginfo, void *addr)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(EADD, pginfo, addr);
 }
 
@@ -179,6 +183,8 @@ static inline int __etrack(void *addr)
 static inline int __eldu(struct sgx_pageinfo *pginfo, void *addr,
 			 void *va)
 {
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_ret_3(ELDU, pginfo, addr, va);
 }
 
@@ -191,6 +197,8 @@ static inline int __epa(void *addr)
 {
 	unsigned long rbx = SGX_PAGE_TYPE_VA;
 
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+
 	return __encls_2(EPA, rbx, addr);
 }
 
arch/x86/kernel/cpu/sgx/sgx.h +3 -0
index b30cee4de903..d7a1490d90bb 100644
@@ -103,4 +103,7 @@ static inline int __init sgx_vepc_init(void)
 
 void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
 
+extern struct srcu_struct sgx_lock_epc_srcu;
+bool sgx_epc_is_locked(void);
+
 #endif /* _X86_SGX_H */
arch/x86/kernel/cpu/sgx/encl.c +18 -0
index 9e01548f4586..39f7f5dc09fd 100644
@@ -284,6 +284,7 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	unsigned long phys_addr;
 	struct sgx_encl *encl;
 	vm_fault_t ret;
+	int srcu_idx;
 
 	encl = vma->vm_private_data;
 
@@ -295,11 +296,18 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	if (unlikely(!encl))
 		return VM_FAULT_SIGBUS;
 
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+	if (sgx_epc_is_locked()) {
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+		return VM_FAULT_SIGBUS;
+	}
+
 	mutex_lock(&encl->lock);
 
 	entry = sgx_encl_load_page(encl, addr, vma->vm_flags);
 	if (IS_ERR(entry)) {
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		if (PTR_ERR(entry) == -EBUSY)
 			return VM_FAULT_NOPAGE;
@@ -312,12 +320,14 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	ret = vmf_insert_pfn(vma, addr, PFN_DOWN(phys_addr));
 	if (ret != VM_FAULT_NOPAGE) {
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		return VM_FAULT_SIGBUS;
 	}
 
 	sgx_encl_test_and_clear_young(vma->vm_mm, entry);
 	mutex_unlock(&encl->lock);
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 	return VM_FAULT_NOPAGE;
 }
@@ -465,6 +475,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 	struct sgx_encl_page *entry = NULL;
 	char data[sizeof(unsigned long)];
 	unsigned long align;
+	int srcu_idx;
 	int offset;
 	int cnt;
 	int ret = 0;
@@ -481,6 +492,12 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 		return -EFAULT;
 
 	for (i = 0; i < len; i += cnt) {
+		srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+		if (sgx_epc_is_locked()) {
+			ret = -EBUSY;
+			goto out;
+		}
+
 		entry = sgx_encl_reserve_page(encl, (addr + i) & PAGE_MASK,
 					      vma->vm_flags);
 		if (IS_ERR(entry)) {
@@ -508,6 +525,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,
 
 out:
 		mutex_unlock(&encl->lock);
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 
 		if (ret)
 			break;
arch/x86/kernel/cpu/sgx/ioctl.c +39 -9
index b3c2e8d58142..d62720ed1728 100644
@@ -147,24 +147,43 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 static long sgx_ioc_enclave_create(struct sgx_encl *encl, void __user *arg)
 {
 	struct sgx_enclave_create create_arg;
+	int srcu_idx;
 	void *secs;
 	int ret;
 
-	if (test_bit(SGX_ENCL_CREATED, &encl->flags))
-		return -EINVAL;
+	if (test_bit(SGX_ENCL_CREATED, &encl->flags)) {
+		ret = -EINVAL;
+		goto err;
+	}
 
-	if (copy_from_user(&create_arg, arg, sizeof(create_arg)))
-		return -EFAULT;
+	if (copy_from_user(&create_arg, arg, sizeof(create_arg))) {
+		ret = -EFAULT;
+		goto err;
+	}
 
 	secs = kmalloc(PAGE_SIZE, GFP_KERNEL);
-	if (!secs)
-		return -ENOMEM;
+	if (!secs) {
+		ret = -ENOMEM;
+		goto err;
+	}
 
-	if (copy_from_user(secs, (void __user *)create_arg.src, PAGE_SIZE))
+	if (copy_from_user(secs, (void __user *)create_arg.src, PAGE_SIZE)) {
 		ret = -EFAULT;
-	else
-		ret = sgx_encl_create(encl, secs);
+		goto err;
+	}
 
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+	if (sgx_epc_is_locked()) {
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+		ret = -EBUSY;
+		goto err;
+	}
+
+	ret = sgx_encl_create(encl, secs);
+
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+
+err:
 	kfree(secs);
 	return ret;
 }
@@ -418,6 +437,7 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)
 	struct sgx_enclave_add_pages add_arg;
 	struct sgx_secinfo secinfo;
 	unsigned long c;
+	int srcu_idx;
 	int ret;
 
 	if (!test_bit(SGX_ENCL_CREATED, &encl->flags) ||
@@ -455,8 +475,18 @@ static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)
 		if (need_resched())
 			cond_resched();
 
+		srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+		if (sgx_epc_is_locked()) {
+			ret = -EBUSY;
+			srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+			break;
+		}
+
 		ret = sgx_encl_add_page(encl, add_arg.src + c, add_arg.offset + c,
 					&secinfo, add_arg.flags);
+
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+
 		if (ret)
 			break;
 	}
arch/x86/kernel/cpu/sgx/main.c +37 -0
index 08c00c325cc6..ec2775652a50 100644
@@ -23,6 +23,17 @@ static int sgx_nr_epc_sections;
 static struct task_struct *ksgxd_tsk;
 static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);
 static DEFINE_XARRAY(sgx_epc_address_space);
+/*
+ * The flag sgx_epc_locked prevents any new SGX flows that
+ * may attempt to allocate a new EPC page.
+ */
+static bool __rcu sgx_epc_locked;
+/*
+ * By synchronizing around sgx_epc_locked SRCU ensures that any executing
+ * SGX flows have completed before proceeding with an SVN update. New SGX flows
+ * will be prevented from starting during an SVN update.
+ */
+DEFINE_SRCU(sgx_lock_epc_srcu);
 
 /*
  * These variables are part of the state of the reclaimer, and must be accessed
@@ -404,6 +415,8 @@ static bool sgx_should_reclaim(unsigned long watermark)
 
 static int ksgxd(void *p)
 {
+	int srcu_idx;
+
 	set_freezable();
 
 	/*
@@ -424,9 +437,15 @@ static int ksgxd(void *p)
 				     kthread_should_stop() ||
 				     sgx_should_reclaim(SGX_NR_HIGH_PAGES));
 
+		srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+		if (sgx_epc_is_locked())
+			goto maybe_resched;
+
 		if (sgx_should_reclaim(SGX_NR_HIGH_PAGES))
 			sgx_reclaim_pages();
 
+maybe_resched:
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 		cond_resched();
 	}
 
@@ -969,3 +988,21 @@ static int __init sgx_init(void)
 }
 
 device_initcall(sgx_init);
+
+static void sgx_lock_epc(void)
+{
+	sgx_epc_locked = true;
+	synchronize_srcu(&sgx_lock_epc_srcu);
+}
+
+static void sgx_unlock_epc(void)
+{
+	sgx_epc_locked = false;
+	synchronize_srcu(&sgx_lock_epc_srcu);
+}
+
+bool sgx_epc_is_locked(void)
+{
+	lockdep_assert_held(&sgx_lock_epc_srcu);
+	return sgx_epc_locked;
+}
arch/x86/kernel/cpu/sgx/virt.c +20 -0
index 6a77a14eee38..e953816d7c8b 100644
@@ -75,10 +75,21 @@ static vm_fault_t sgx_vepc_fault(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	struct sgx_vepc *vepc = vma->vm_private_data;
+	int srcu_idx;
 	int ret;
 
 	mutex_lock(&vepc->lock);
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+
+	if (sgx_epc_is_locked()) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
 	ret = __sgx_vepc_fault(vepc, vma, vmf->address);
+
+out_unlock:
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
 	mutex_unlock(&vepc->lock);
 
 	if (!ret)
@@ -331,6 +342,7 @@ int __init sgx_vepc_init(void)
 int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 		     int *trapnr)
 {
+	int srcu_idx;
 	int ret;
 
 	/*
@@ -347,6 +359,12 @@ int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 	if (WARN_ON_ONCE(!access_ok(secs, PAGE_SIZE)))
 		return -EINVAL;
 
+	srcu_idx = srcu_read_lock(&sgx_lock_epc_srcu);
+	if (sgx_epc_is_locked()) {
+		srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+		return -EBUSY;
+	}
+
 	__uaccess_begin();
 	ret = __ecreate(pageinfo, (void *)secs);
 	__uaccess_end();
@@ -356,6 +374,8 @@ int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 		return -EFAULT;
 	}
 
+	srcu_read_unlock(&sgx_lock_epc_srcu, srcu_idx);
+
 	/* ECREATE doesn't return an error code, it faults or succeeds. */
 	WARN_ON_ONCE(ret);
 	return 0;
Signature · 1 line
2.17.1

BR, Jarkko

#3 lore raw

PATCH v5 2/9 x86/sgx: Save enclave pointer for VA page

In reply to Cathy Zhang

Tearing down all enclaves is required by SGX SVN update, which involves running the ENCLS[EREMOVE] instruction on every EPC page. This (tearing down all enclaves) should be coordinated with any enclaves that may be in the process of existing and thus already be running ENCLS[EREMOVE] as part of enclave release.

In support of this coordination, it is required to know which enclave owns each in-use EPC page. It is already possible to locate the owning enclave of SECS and regular pages but not for VA pages.

Make the following changes for VA pages' location:
1) Make epc->owner type-agnostic by changing its type to 'void *'. So, besides "struct sgx_encl_page", it can have other types, like "struct sgx_va_page".
2) Save the enclave pointer for each VA page to support locating its owning enclave.

Note: to track 2T EPC memory, this scheme of tracking will use additional 8M memory.

---
Changes since v4:
- Add back the blank line removed unintenitonally in encl.h. (Jarkko Sakkinen)

Changes since v3:
- Squash patch "x86/sgx: Provide VA page non-NULL owner" and "x86/sgx: Save enclave pointer for VA page". Update commit log. (Suggested by Jarkko Sakkinen)

---
 arch/x86/kernel/cpu/sgx/encl.h  | 3 ++-
 arch/x86/kernel/cpu/sgx/sgx.h   | 2 +-
 arch/x86/kernel/cpu/sgx/encl.c  | 5 +++--
 arch/x86/kernel/cpu/sgx/ioctl.c | 3 ++-
 4 files changed, 8 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.h +2 -1
index 7311bc40bd1b..52e0a3e52598 100644
@@ -76,6 +76,7 @@ struct sgx_va_page {
 	struct sgx_epc_page *epc_page;
 	DECLARE_BITMAP(slots, SGX_VA_SLOT_COUNT);
 	struct list_head list;
+	struct sgx_encl *encl;
 };
 
 struct sgx_backing {
@@ -113,7 +114,7 @@ void sgx_encl_put_backing(struct sgx_backing *backing);
 int sgx_encl_test_and_clear_young(struct mm_struct *mm,
 				  struct sgx_encl_page *page);
 
-struct sgx_epc_page *sgx_alloc_va_page(void);
+struct sgx_epc_page *sgx_alloc_va_page(struct sgx_va_page *va_page);
 unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
 void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
 bool sgx_va_page_full(struct sgx_va_page *va_page);
arch/x86/kernel/cpu/sgx/sgx.h +1 -1
index d7a1490d90bb..f8ed9deac18b 100644
@@ -33,7 +33,7 @@ struct sgx_epc_page {
 	unsigned int section;
 	u16 flags;
 	u16 poison;
-	struct sgx_encl_page *owner;
+	void *owner;
 	struct list_head list;
 };
 
arch/x86/kernel/cpu/sgx/encl.c +3 -2
index 39f7f5dc09fd..08f39fa03a39 100644
@@ -888,6 +888,7 @@ int sgx_encl_test_and_clear_young(struct mm_struct *mm,
 
 /**
  * sgx_alloc_va_page() - Allocate a Version Array (VA) page
+ * @va_page:	struct sgx_va_page connected to this VA page
  *
  * Allocate a free EPC page and convert it to a Version Array (VA) page.
  *
@@ -895,12 +896,12 @@ int sgx_encl_test_and_clear_young(struct mm_struct *mm,
  *   a VA page,
  *   -errno otherwise
  */
-struct sgx_epc_page *sgx_alloc_va_page(void)
+struct sgx_epc_page *sgx_alloc_va_page(struct sgx_va_page *va_page)
 {
 	struct sgx_epc_page *epc_page;
 	int ret;
 
-	epc_page = sgx_alloc_epc_page(NULL, true);
+	epc_page = sgx_alloc_epc_page(va_page, true);
 	if (IS_ERR(epc_page))
 		return ERR_CAST(epc_page);
 
arch/x86/kernel/cpu/sgx/ioctl.c +2 -1
index d62720ed1728..939008c63a84 100644
@@ -30,7 +30,8 @@ static struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl)
 		if (!va_page)
 			return ERR_PTR(-ENOMEM);
 
-		va_page->epc_page = sgx_alloc_va_page();
+		va_page->encl = encl;
+		va_page->epc_page = sgx_alloc_va_page(va_page);
 		if (IS_ERR(va_page->epc_page)) {
 			err = ERR_CAST(va_page->epc_page);
 			kfree(va_page);
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH v5 2/9] x86/sgx: Save enclave pointer for VA page

Quoted 10 lines · Cathy Zhang

Tearing down all enclaves is required by SGX SVN update, which involves running the ENCLS[EREMOVE] instruction on every EPC page. This (tearing down all enclaves) should be coordinated with any enclaves that may be in the process of existing and thus already be running ENCLS[EREMOVE] as part of enclave release.

In support of this coordination, it is required to know which enclave owns each in-use EPC page. It is already possible to locate the owning enclave of SECS and regular pages but not for VA pages.

Quoted 6 lines

Make the following changes for VA pages' location:
1) Make epc->owner type-agnostic by changing its type to 'void *'. So, besides "struct sgx_encl_page", it can have other types, like "struct sgx_va_page".
2) Save the enclave pointer for each VA page to support locating its owning enclave.

Enumeration lists imply that you would better to split this into two patches.

BR, Jarkko

#5 lore raw

PATCH v5 3/9 x86/sgx: Keep record for SGX VA and Guest page type

In reply to Cathy Zhang

Regular enclave EPC pages have sgx_encl_page as their owner, but SGX VA page and KVM guest EPC page are maintained by different owner structures.

SGX CPUSVN update requires to know the EPC page owner's status and then decide how to handle the page.

Keep a record of page type for SGX VA and KVM guest page while the other EPC pages already have their type tracked, so that CPUSVN update can get EPC page's owner by type and handle it then.

---
Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST. (Suggested by Jarkko, Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 4 ++++
 arch/x86/kernel/cpu/sgx/encl.c | 2 ++
 arch/x86/kernel/cpu/sgx/virt.c | 2 ++
 3 files changed, 8 insertions(+)
arch/x86/kernel/cpu/sgx/sgx.h +4 -0
index f8ed9deac18b..4ad0e5396eef 100644
@@ -28,6 +28,10 @@
 
 /* Pages on free list */
 #define SGX_EPC_PAGE_IS_FREE		BIT(1)
+/* VA page */
+#define SGX_EPC_PAGE_VA			BIT(2)
+/* Pages allocated for KVM guest */
+#define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
 
 struct sgx_epc_page {
 	unsigned int section;
arch/x86/kernel/cpu/sgx/encl.c +2 -0
index 08f39fa03a39..383abd538ac9 100644
@@ -912,6 +912,8 @@ struct sgx_epc_page *sgx_alloc_va_page(struct sgx_va_page *va_page)
 		return ERR_PTR(-EFAULT);
 	}
 
+	epc_page->flags |= SGX_EPC_PAGE_VA;
+
 	return epc_page;
 }
 
arch/x86/kernel/cpu/sgx/virt.c +2 -0
index e953816d7c8b..104487b72fb8 100644
@@ -50,6 +50,8 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,
 	if (IS_ERR(epc_page))
 		return PTR_ERR(epc_page);
 
+	epc_page->flags |= SGX_EPC_PAGE_KVM_GUEST;
+
 	ret = xa_err(xa_store(&vepc->page_array, index, epc_page, GFP_KERNEL));
 	if (ret)
 		goto err_free;
Signature · 1 line
2.17.1
maintainer
#6 lore raw

Re: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Quoted 56 lines · Cathy Zhang

Regular enclave EPC pages have sgx_encl_page as their owner, but SGX VA page and KVM guest EPC page are maintained by different owner structures.

SGX CPUSVN update requires to know the EPC page owner's status and then decide how to handle the page.

Keep a record of page type for SGX VA and KVM guest page while the other EPC pages already have their type tracked, so that CPUSVN update can get EPC page's owner by type and handle it then.

---
Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST. (Suggested by Jarkko, Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 4 ++++
 arch/x86/kernel/cpu/sgx/encl.c | 2 ++
 arch/x86/kernel/cpu/sgx/virt.c | 2 ++
 3 files changed, 8 insertions(+)
arch/x86/kernel/cpu/sgx/sgx.h +4 -0
index f8ed9deac18b..4ad0e5396eef 100644
@@ -28,6 +28,10 @@
 
 /* Pages on free list */
 #define SGX_EPC_PAGE_IS_FREE		BIT(1)
+/* VA page */
+#define SGX_EPC_PAGE_VA			BIT(2)
+/* Pages allocated for KVM guest */
+#define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
 
 struct sgx_epc_page {
 	unsigned int section;
arch/x86/kernel/cpu/sgx/encl.c +2 -0
index 08f39fa03a39..383abd538ac9 100644
@@ -912,6 +912,8 @@ struct sgx_epc_page *sgx_alloc_va_page(struct sgx_va_page *va_page)
 		return ERR_PTR(-EFAULT);
 	}
 
+	epc_page->flags |= SGX_EPC_PAGE_VA;
+
 	return epc_page;
 }
 
arch/x86/kernel/cpu/sgx/virt.c +0 -0
index e953816d7c8b..104487b72fb8 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_vepc_fault(struct sgx_vepc *vepc, · line 53

Quoted 4 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -50,6 +50,8 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,
 	if (IS_ERR(epc_page))
 		return PTR_ERR(epc_page);
 
+	epc_page->flags |= SGX_EPC_PAGE_KVM_GUEST;

This would need to be synced up with SGX_EPC_IS_VEPC:

https://lore.kernel.org/linux-sgx/[email protected]/T/#t

Otherwise, we have a chaos.

Can you sync up with Zhiquan Li and find some common approach. IMHO, it might even sense to merge these patch sets into one. Then it would be easier to review them as a whole.

BR, Jarkko

#7 lore raw

RE: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Hi Jarkko,

Quoted 81 lines

-----Original Message-----
From: Jarkko Sakkinen <[email protected]>
Sent: Saturday, May 21, 2022 3:11 AM
To: Zhang, Cathy <[email protected]>; Li, Zhiquan1 <[email protected]>
Cc: [email protected]; [email protected]; Chatre, Reinette <[email protected]>; Hansen, Dave <[email protected]>; Raj, Ashok <[email protected]>; [email protected]; Zhong, Yang <[email protected]>
Subject: Re: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Quoted 11 lines · Cathy Zhang

Regular enclave EPC pages have sgx_encl_page as their owner, but SGX VA page and KVM guest EPC page are maintained by different owner structures.

SGX CPUSVN update requires to know the EPC page owner's status and then decide how to handle the page.

Keep a record of page type for SGX VA and KVM guest page while the other EPC pages already have their type tracked, so that CPUSVN update can get EPC page's owner by type and handle it then.

Quoted 5 lines

---
Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST.

(Suggested by

arch/x86/kernel/cpu/sgx/encl.c struct sgx_epc_page *sgx_alloc_va_page(struct

Quoted 29 lines

Jarkko, Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 4 ++++
arch/x86/kernel/cpu/sgx/encl.c | 2 ++  arch/x86/kernel/cpu/sgx/virt.c
| 2 ++
 3 files changed, 8 insertions(+)
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/sgx.h index f8ed9deac18b..4ad0e5396eef 100644

arch/x86/kernel/cpu/sgx/sgx.h +4 -0
@@ -28,6 +28,10 @@
 
 /* Pages on free list */
 #define SGX_EPC_PAGE_IS_FREE		BIT(1)
+/* VA page */
+#define SGX_EPC_PAGE_VA			BIT(2)
+/* Pages allocated for KVM guest */
+#define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
 
 struct sgx_epc_page {
 	unsigned int section;
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/encl.c index 08f39fa03a39..383abd538ac9 100644

arch/x86/kernel/cpu/sgx/encl.c +0 -0
@@ -912,6 +912,8 @@ struct sgx_epc_page *sgx_alloc_va_page(struct

sgx_va_page *va_page)

Quoted 14 lines
arch/x86/kernel/cpu/sgx/encl.c +2 -0
 		return ERR_PTR(-EFAULT);
 	}
 
+	epc_page->flags |= SGX_EPC_PAGE_VA;
+
 	return epc_page;
 }
 
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/virt.c index e953816d7c8b..104487b72fb8 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_vepc_fault(struct sgx_vepc *vepc, · line 53

Quoted 4 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -50,6 +50,8 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,
 	if (IS_ERR(epc_page))
 		return PTR_ERR(epc_page);
 
+	epc_page->flags |= SGX_EPC_PAGE_KVM_GUEST;

This would need to be synced up with SGX_EPC_IS_VEPC:

Quoted 8 lines

https://lore.kernel.org/linux-sgx/694234d7-6a0d-e85f-f2f9-
[email protected]/T/#t

Otherwise, we have a chaos.

Can you sync up with Zhiquan Li and find some common approach. IMHO, it might even sense to merge these patch sets into one. Then it would be easier to review them as a whole.

Thanks for showing the above related patches, Jarkko! I just take a quick look at that series, AFAIK, __sgx_vepc_fault() will only be called during VM boot up to allocate physical EPC pages. Then, host does not know how VM works with those pages. VM SGX driver will handle any errors itself. So with the approach in that series, what it actually tracks is the vaddr in QEMU address space for all EPC pages allocated to the VM. Does host need to track such information? QEMU side should provide it easily I think. @Li, Zhiquan1.

Quoted 2 lines

BR, Jarkko

#8 lore raw

RE: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Hi Jarkko,

Quoted 105 lines

-----Original Message-----
From: Zhang, Cathy
Sent: Monday, May 23, 2022 8:07 AM
To: 'Jarkko Sakkinen' <[email protected]>; Li, Zhiquan1 <[email protected]>
Cc: [email protected]; [email protected]; Chatre, Reinette <[email protected]>; Hansen, Dave <[email protected]>; Raj, Ashok <[email protected]>; [email protected]; Zhong, Yang <[email protected]>
Subject: RE: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Hi Jarkko,

Quoted 82 lines

-----Original Message-----
From: Jarkko Sakkinen <[email protected]>
Sent: Saturday, May 21, 2022 3:11 AM
To: Zhang, Cathy <[email protected]>; Li, Zhiquan1 <[email protected]>
Cc: [email protected]; [email protected]; Chatre, Reinette <[email protected]>; Hansen, Dave <[email protected]>; Raj, Ashok <[email protected]>; [email protected]; Zhong, Yang <[email protected]>
Subject: Re: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Quoted 11 lines · Cathy Zhang

Regular enclave EPC pages have sgx_encl_page as their owner, but SGX VA page and KVM guest EPC page are maintained by different owner structures.

SGX CPUSVN update requires to know the EPC page owner's status and then decide how to handle the page.

Keep a record of page type for SGX VA and KVM guest page while the other EPC pages already have their type tracked, so that CPUSVN update can get EPC page's owner by type and handle it then.

Quoted 5 lines

---
Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST.

(Suggested by

arch/x86/kernel/cpu/sgx/encl.c struct sgx_epc_page *sgx_alloc_va_page(struct

Quoted 30 lines

Jarkko, Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 4 ++++
arch/x86/kernel/cpu/sgx/encl.c | 2 ++
arch/x86/kernel/cpu/sgx/virt.c
| 2 ++
 3 files changed, 8 insertions(+)
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/sgx.h index f8ed9deac18b..4ad0e5396eef 100644

arch/x86/kernel/cpu/sgx/sgx.h +4 -0
@@ -28,6 +28,10 @@
 
 /* Pages on free list */
 #define SGX_EPC_PAGE_IS_FREE		BIT(1)
+/* VA page */
+#define SGX_EPC_PAGE_VA			BIT(2)
+/* Pages allocated for KVM guest */
+#define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
 
 struct sgx_epc_page {
 	unsigned int section;
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/encl.c index 08f39fa03a39..383abd538ac9 100644

arch/x86/kernel/cpu/sgx/encl.c +0 -0
@@ -912,6 +912,8 @@ struct sgx_epc_page *sgx_alloc_va_page(struct

sgx_va_page *va_page)

Quoted 14 lines
arch/x86/kernel/cpu/sgx/encl.c +2 -0
 		return ERR_PTR(-EFAULT);
 	}
 
+	epc_page->flags |= SGX_EPC_PAGE_VA;
+
 	return epc_page;
 }
 
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/virt.c index e953816d7c8b..104487b72fb8 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_vepc_fault(struct sgx_vepc *vepc, · line 53

Quoted 4 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -50,6 +50,8 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,
 	if (IS_ERR(epc_page))
 		return PTR_ERR(epc_page);
 
+	epc_page->flags |= SGX_EPC_PAGE_KVM_GUEST;

This would need to be synced up with SGX_EPC_IS_VEPC:

Quoted 8 lines

https://lore.kernel.org/linux-sgx/694234d7-6a0d-e85f-f2f9-
[email protected]/T/#t

Otherwise, we have a chaos.

Can you sync up with Zhiquan Li and find some common approach. IMHO, it might even sense to merge these patch sets into one. Then it would be easier to review them as a whole.

Quoted 10 lines

Thanks for showing the above related patches, Jarkko! I just take a quick look at that series, AFAIK, __sgx_vepc_fault() will only be called during VM boot up to allocate physical EPC pages.
Then, host does not know how VM works with those pages. VM SGX driver will handle any errors itself.
So with the approach in that series, what it actually tracks is the vaddr in QEMU address space for all EPC pages allocated to the VM. Does host need to track such information? QEMU side should provide it easily I think. @Li, Zhiquan1.

I've synced with Zhiquan Li and got the whole picture of his patch logic. I suggest to follow the way how we handle the same change between EDMM and Seamless, that is Zhiquan Li will apply patch 3 from Seamless and then add his left patches. Please let's know if you have other suggestion. Thanks!

Quoted 2 lines
Quoted 2 lines

BR, Jarkko

maintainer
#9 lore raw

Re: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Quoted 118 lines · Cathy

Hi Jarkko,

Quoted 105 lines

-----Original Message-----
From: Zhang, Cathy
Sent: Monday, May 23, 2022 8:07 AM
To: 'Jarkko Sakkinen' <[email protected]>; Li, Zhiquan1 <[email protected]>
Cc: [email protected]; [email protected]; Chatre, Reinette <[email protected]>; Hansen, Dave <[email protected]>; Raj, Ashok <[email protected]>; [email protected]; Zhong, Yang <[email protected]>
Subject: RE: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Hi Jarkko,

Quoted 82 lines

-----Original Message-----
From: Jarkko Sakkinen <[email protected]>
Sent: Saturday, May 21, 2022 3:11 AM
To: Zhang, Cathy <[email protected]>; Li, Zhiquan1 <[email protected]>
Cc: [email protected]; [email protected]; Chatre, Reinette <[email protected]>; Hansen, Dave <[email protected]>; Raj, Ashok <[email protected]>; [email protected]; Zhong, Yang <[email protected]>
Subject: Re: [PATCH v5 3/9] x86/sgx: Keep record for SGX VA and Guest page type

Quoted 11 lines · Cathy Zhang

Regular enclave EPC pages have sgx_encl_page as their owner, but SGX VA page and KVM guest EPC page are maintained by different owner structures.

SGX CPUSVN update requires to know the EPC page owner's status and then decide how to handle the page.

Keep a record of page type for SGX VA and KVM guest page while the other EPC pages already have their type tracked, so that CPUSVN update can get EPC page's owner by type and handle it then.

Quoted 5 lines

---
Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST.

(Suggested by

arch/x86/kernel/cpu/sgx/encl.c struct sgx_epc_page *sgx_alloc_va_page(struct

Quoted 30 lines

Jarkko, Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 4 ++++
arch/x86/kernel/cpu/sgx/encl.c | 2 ++
arch/x86/kernel/cpu/sgx/virt.c
| 2 ++
 3 files changed, 8 insertions(+)
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/sgx.h index f8ed9deac18b..4ad0e5396eef 100644

arch/x86/kernel/cpu/sgx/sgx.h +4 -0
@@ -28,6 +28,10 @@
 
 /* Pages on free list */
 #define SGX_EPC_PAGE_IS_FREE		BIT(1)
+/* VA page */
+#define SGX_EPC_PAGE_VA			BIT(2)
+/* Pages allocated for KVM guest */
+#define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
 
 struct sgx_epc_page {
 	unsigned int section;
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/encl.c index 08f39fa03a39..383abd538ac9 100644

arch/x86/kernel/cpu/sgx/encl.c +0 -0
@@ -912,6 +912,8 @@ struct sgx_epc_page *sgx_alloc_va_page(struct

sgx_va_page *va_page)

Quoted 14 lines
arch/x86/kernel/cpu/sgx/encl.c +2 -0
 		return ERR_PTR(-EFAULT);
 	}
 
+	epc_page->flags |= SGX_EPC_PAGE_VA;
+
 	return epc_page;
 }
 
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/virt.c index e953816d7c8b..104487b72fb8 100644

arch/x86/kernel/cpu/sgx/virt.c static int __sgx_vepc_fault(struct sgx_vepc *vepc, · line 53

Quoted 4 lines
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -50,6 +50,8 @@ static int __sgx_vepc_fault(struct sgx_vepc *vepc,
 	if (IS_ERR(epc_page))
 		return PTR_ERR(epc_page);
 
+	epc_page->flags |= SGX_EPC_PAGE_KVM_GUEST;

This would need to be synced up with SGX_EPC_IS_VEPC:

Quoted 8 lines

https://lore.kernel.org/linux-sgx/694234d7-6a0d-e85f-f2f9-
[email protected]/T/#t

Otherwise, we have a chaos.

Can you sync up with Zhiquan Li and find some common approach. IMHO, it might even sense to merge these patch sets into one. Then it would be easier to review them as a whole.

Quoted 10 lines

Thanks for showing the above related patches, Jarkko! I just take a quick look at that series, AFAIK, __sgx_vepc_fault() will only be called during VM boot up to allocate physical EPC pages.
Then, host does not know how VM works with those pages. VM SGX driver will handle any errors itself.
So with the approach in that series, what it actually tracks is the vaddr in QEMU address space for all EPC pages allocated to the VM. Does host need to track such information? QEMU side should provide it easily I think. @Li, Zhiquan1.

Quoted 4 lines

I've synced with Zhiquan Li and got the whole picture of his patch logic. I suggest to follow the way how we handle the same change between EDMM and Seamless, that is Zhiquan Li will apply patch 3 from Seamless and then add his left patches. Please let's know if you have other suggestion. Thanks!

Sounds reasonable!

BR, Jarkko

#10 lore raw

PATCH v5 4/9 x86/sgx: Save the size of each EPC section

In reply to Cathy Zhang

SGX CPUSVN update process should check all EPC pages to ensure they are marked as unused. For EPC pages are stored in EPC sections, it's required to save the size of each section, as the indicator for the end of each section's traversing to unuse EPC pages.

---
Changes since v3:
- Update commit log to explain clearly why record the size. (Suggested by Jarkko Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  | 1 +
 arch/x86/kernel/cpu/sgx/main.c | 1 +
 2 files changed, 2 insertions(+)
arch/x86/kernel/cpu/sgx/sgx.h +1 -0
index 4ad0e5396eef..775477e0b8af 100644
@@ -63,6 +63,7 @@ struct sgx_epc_section {
 	void *virt_addr;
 	struct sgx_epc_page *pages;
 	struct sgx_numa_node *node;
+	u64 size;
 };
 
 extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
arch/x86/kernel/cpu/sgx/main.c +1 -0
index ec2775652a50..5fbad67d529f 100644
@@ -662,6 +662,7 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 	}
 
 	section->phys_addr = phys_addr;
+	section->size = size;
 	xa_store_range(&sgx_epc_address_space, section->phys_addr,
 		       phys_addr + size - 1, section, GFP_KERNEL);
 
Signature · 1 line
2.17.1
#11 lore raw

PATCH v5 5/9 x86/sgx: Forced EPC page zapping for EUPDATESVN

In reply to Cathy Zhang

Before an EUPDATESVN instruction can be successful, all enclave pages (EPC) must be marked as unused in the SGX hardware metadata (EPCM).

A page becomes unused when an issued EREMOVE instruction succeeds. To prepare for EUPDATESVN, loop over all SGX pages and attempt to EREMOVE them. This is fatal to running enclaves and destroys all enclave state and memory contents. This destruction is by design and mitigates any compromise of enclaves or the SGX hardware itself which occurred before the microcode update.

An EREMOVE operation on a page may fail for a few reasons. Each has its own mitigations.

First, EREMOVE will fail if an enclave that uses the page is executing. Send an IPI to all CPUs that might be running the enclave to force it out of the enclave long enough to EREMOVE the page. Other CPUs might enter the enclave in the meantime, so this is not a rock-solid guarantee.

Second, EREMOVE can fail on special SGX metadata pages, such as SECS. EREMOVE will work on them only after the normal SGX pages that depend on them have been EREMOVE'd. Defer handling those pages and repeat EREMOVE after the dependency has been addressed.

---
Changes since v4:
- Move *-EBUSY* into one line for the three sgx_zap_* functions' annotate. (Jarkko Sakkinen).

Changes since v3:
- Rename SGX_EPC_PAGE_GUEST as SGX_EPC_PAGE_KVM_GUEST (Suggested by Jarkko Sakkinen)
- Remove "VA" from sentence "Second, EREMOVE can fail on special SGX metadata...", for except concurrency rules, only SECS pages might be EREMOVEd failed and will be tracked for a deferred handling. (Suggested by Jarkko Sakkinen)

---
 arch/x86/kernel/cpu/sgx/sgx.h  |  13 ++
 arch/x86/kernel/cpu/sgx/encl.c |  14 +-
 arch/x86/kernel/cpu/sgx/main.c | 344 +++++++++++++++++++++++++++++++++
 3 files changed, 370 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/sgx.h +13 -0
index 775477e0b8af..d90532957181 100644
@@ -32,6 +32,17 @@
 #define SGX_EPC_PAGE_VA			BIT(2)
 /* Pages allocated for KVM guest */
 #define SGX_EPC_PAGE_KVM_GUEST		BIT(3)
+/*
+ * Pages, failed to be zapped (EREMOVED)
+ * by SGX CPUSVN update process.
+ */
+#define SGX_EPC_PAGE_ZAP_TRACKED	BIT(4)
+/*
+ * Pages, the associated enclave is being
+ * released while SGX CPUSVN update is
+ * running.
+ */
+#define SGX_EPC_PAGE_IN_RELEASE		BIT(5)
 
 struct sgx_epc_page {
 	unsigned int section;
@@ -110,5 +121,7 @@ void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
 
 extern struct srcu_struct sgx_lock_epc_srcu;
 bool sgx_epc_is_locked(void);
+void sgx_zap_wakeup(void);
+void sgx_zap_abort(void);
 
 #endif /* _X86_SGX_H */
arch/x86/kernel/cpu/sgx/encl.c +13 -1
index 383abd538ac9..4dbcfc959eb2 100644
@@ -600,6 +600,12 @@ void sgx_encl_release(struct kref *ref)
 	WARN_ON_ONCE(encl->secs_child_cnt);
 	WARN_ON_ONCE(encl->secs.epc_page);
 
+	/*
+	 * EPC pages were freed and EREMOVE was executed. Wake
+	 * up any zappers which were waiting for this.
+	 */
+	sgx_zap_wakeup();
+
 	kfree(encl);
 }
 
@@ -975,8 +981,14 @@ void sgx_encl_free_epc_page(struct sgx_epc_page *page)
 	WARN_ON_ONCE(page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED);
 
 	ret = __eremove(sgx_get_epc_virt_addr(page));
-	if (WARN_ONCE(ret, EREMOVE_ERROR_MESSAGE, ret, ret))
+	if (WARN_ONCE(ret, EREMOVE_ERROR_MESSAGE, ret, ret)) {
+		/*
+		 * The EREMOVE failed. If a CPUSVN is in progress,
+		 * it is now expected to fail. Notify it.
+		 */
+		sgx_zap_abort();
 		return;
+	}
 
 	sgx_free_epc_page(page);
 }
arch/x86/kernel/cpu/sgx/main.c +344 -0

359 lines not on this page · raw message

Signature · 1 line
2.17.1
#12 lore raw

PATCH v5 6/9 x86/sgx: Define error codes for ENCLS[EUPDATESVN]

In reply to Cathy Zhang

Add error codes for ENCLS[EUPDATESVN], then SGX CPUSVN update process can know the execution state of EUPDATESVN and notify userspace.

---
 arch/x86/include/asm/sgx.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/include/asm/sgx.h +10 -0
index 03ec7e32659c..4a8ca7281229 100644
@@ -65,17 +65,27 @@ enum sgx_encls_function {
 
 /**
  * enum sgx_return_code - The return code type for ENCLS, ENCLU and ENCLV
+ * %SGX_EPC_PAGE_CONFLICT	Page is being written by other ENCLS function.
  * %SGX_NOT_TRACKED:		Previous ETRACK's shootdown sequence has not
  *				been completed yet.
  * %SGX_CHILD_PRESENT		SECS has child pages present in the EPC.
  * %SGX_INVALID_EINITTOKEN:	EINITTOKEN is invalid and enclave signer's
  *				public key does not match IA32_SGXLEPUBKEYHASH.
+ * %SGX_INSUFFICIENT_ENTROPY:	Insufficient entropy in RNG.
+ * %SGX_EPC_NOT_READY:		EPC is not ready for SVN update.
+ * %SGX_NO_UPDATE:		EUPDATESVN was successful, but CPUSVN was not
+ *				updated because current SVN was not newer than
+ *				CPUSVN.
  * %SGX_UNMASKED_EVENT:		An unmasked event, e.g. INTR, was received
  */
 enum sgx_return_code {
+	SGX_EPC_PAGE_CONFLICT		= 7,
 	SGX_NOT_TRACKED			= 11,
 	SGX_CHILD_PRESENT		= 13,
 	SGX_INVALID_EINITTOKEN		= 16,
+	SGX_INSUFFICIENT_ENTROPY	= 29,
+	SGX_EPC_NOT_READY		= 30,
+	SGX_NO_UPDATE			= 31,
 	SGX_UNMASKED_EVENT		= 128,
 };
 
Signature · 1 line
2.17.1
#13 lore raw

PATCH v5 7/9 x86/sgx: Implement ENCLS[EUPDATESVN]

In reply to Cathy Zhang

The SGX attestation architecture assumes a compromise of all running enclaves and cryptographic assets (like internal SGX encryption keys) whenever a microcode update affects SGX. To mitigate the impact of this presumed compromise, a new supervisor SGX instruction:
ENCLS[EUPDATESVN], is introduced to update SGX microcode version and generate new cryptographic assets in runtime after SGX microcode update.

EUPDATESVN requires that SGX memory to be marked as "unused" before it will succeed. This ensures that no compromised enclave can survive the process and provides an opportunity to generate new cryptographic assets.

---
Changes since v1:
- Print message for each return code to notify userspace the ENCLS[EUPDATESVN] execution status.

---
 arch/x86/include/asm/sgx.h      | 33 +++++++++++----------
 arch/x86/kernel/cpu/sgx/encls.h |  6 ++++
 arch/x86/kernel/cpu/sgx/main.c  | 52 +++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 15 deletions(-)
arch/x86/include/asm/sgx.h +18 -15
index 4a8ca7281229..74bcb6841a4b 100644
@@ -26,23 +26,26 @@
 #define SGX_CPUID_EPC_SECTION	0x1
 /* The bitmask for the EPC section type. */
 #define SGX_CPUID_EPC_MASK	GENMASK(3, 0)
+/* EUPDATESVN presence indication */
+#define SGX_CPUID_EUPDATESVN	BIT(10)
 
 enum sgx_encls_function {
-	ECREATE	= 0x00,
-	EADD	= 0x01,
-	EINIT	= 0x02,
-	EREMOVE	= 0x03,
-	EDGBRD	= 0x04,
-	EDGBWR	= 0x05,
-	EEXTEND	= 0x06,
-	ELDU	= 0x08,
-	EBLOCK	= 0x09,
-	EPA	= 0x0A,
-	EWB	= 0x0B,
-	ETRACK	= 0x0C,
-	EAUG	= 0x0D,
-	EMODPR	= 0x0E,
-	EMODT	= 0x0F,
+	ECREATE		= 0x00,
+	EADD		= 0x01,
+	EINIT		= 0x02,
+	EREMOVE		= 0x03,
+	EDGBRD		= 0x04,
+	EDGBWR		= 0x05,
+	EEXTEND		= 0x06,
+	ELDU		= 0x08,
+	EBLOCK		= 0x09,
+	EPA		= 0x0A,
+	EWB		= 0x0B,
+	ETRACK		= 0x0C,
+	EAUG		= 0x0D,
+	EMODPR		= 0x0E,
+	EMODT		= 0x0F,
+	EUPDATESVN	= 0x18,
 };
 
 /**
arch/x86/kernel/cpu/sgx/encls.h +6 -0
index 60321c5f5718..8455f385e817 100644
@@ -208,4 +208,10 @@ static inline int __ewb(struct sgx_pageinfo *pginfo, void *addr,
 	return __encls_ret_3(EWB, pginfo, addr, va);
 }
 
+/* Update CPUSVN at runtime. */
+static inline int __eupdatesvn(void)
+{
+	return __encls_ret_1(EUPDATESVN, "");
+}
+
 #endif /* _X86_ENCLS_H */
arch/x86/kernel/cpu/sgx/main.c +52 -0

55 lines not on this page · raw message

Signature · 1 line
2.17.1
#14 lore raw

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

In reply to Cathy Zhang

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.

---
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(+)
arch/x86/include/asm/sgx.h +6 -0
index 74bcb6841a4b..1321670a6338 100644
@@ -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 */
arch/x86/kernel/cpu/common.c +10 -0
index e342ae4db3c4..a50e0e183139 100644
@@ -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);
 
arch/x86/kernel/cpu/sgx/main.c +12 -0
index 43dd2b34e040..0f541d01e561 100644
@@ -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();
+}
Signature · 1 line
2.17.1
#15 lore raw

PATCH v5 9/9 x86/sgx: Call ENCLS[EUPDATESVN] during SGX initialization

In reply to Cathy Zhang

A snapshot of the processor microcode SVN is taken each boot cycle at the time when Intel SGX is first used. This results in microcode updates being loadable at any time, fixing microcode issues. However, if system boot up through kexec() from error recovery, no hardware reset happens, any SGX leaf execution during boot up is not assumed as the first use in such case, and no snapshot of SVN is taken. So, it's necessary to call ENCLS[EUPDATESVN] to update SVN automatically, rather than waiting for the admin to do it when he/she is even not aware of that.

Call ENCLS[EUPDATESVN] after sanitizing pages will increase the chance of success, for it requires that EPC is empty.

---
Changes since v3:
- Rename as sgx_update_cpusvn_intel().

Changes since v1:
- Update accordingly for update_cpusvn_intel() return *void*.

---
 arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +9 -1
index 0f541d01e561..aea6b237fda0 100644
@@ -423,6 +423,7 @@ static bool sgx_should_reclaim(unsigned long watermark)
 	       !list_empty(&sgx_active_page_list);
 }
 
+void sgx_update_cpusvn_intel(void);
 static int ksgxd(void *p)
 {
 	int srcu_idx;
@@ -437,7 +438,14 @@ static int ksgxd(void *p)
 	__sgx_sanitize_pages(&sgx_dirty_page_list);
 
 	/* sanity check: */
-	WARN_ON(!list_empty(&sgx_dirty_page_list));
+	if (!WARN_ON(!list_empty(&sgx_dirty_page_list))) {
+		/*
+		 * Do SVN update for kexec(). It should complete without error, for
+		 * all EPC pages are unused at this point.
+		 */
+		if (cpuid_eax(SGX_CPUID) & SGX_CPUID_EUPDATESVN)
+			sgx_update_cpusvn_intel();
+	}
 
 	while (!kthread_should_stop()) {
 		if (try_to_freeze())
Signature · 1 line
2.17.1
#16 lore raw
In reply to Cathy Zhang

Cathy,

Quoted 8 lines · Cathy Zhang

First, the cadence of microcode updates has increased to deliver security mitigations. Second, the value of those updates has increased, meaning that any delay in applying them is unacceptable. Third, users have become accustomed to approaches like hot patching their kernels and have a growing aversion to reboots in general.

Users want microcode updates to behave more like a hot patching a kernel and less like a BIOS update.

please don't take this personaly.

What users want and what's technically correct are two different things.

Fact is that late microcode updates especially those which change features, add/remove functionality are simply broken. This has been discussed to death already and I'm not going to find all the various threads which provided that information. lore.kernel.org has excellent search capabilities.

As a summary, there is a long standing request that for late loading microcode needs to come with machine readable information about the nature of the update which tells the kernel whether there are changes which cannot be applied post boot.

This was agreed on by Intel folks and until this materializes any attempt to load microcode late has to be considered as unsupported. This is going on for years now and has been ignored.

As a consequence we are not adding a special SGX workaround for something which is known to be broken.

What we are going to do and I'm fasttracking this is:

https://lore.kernel.org/all/[email protected]

which make the SGX workaround moot.

Thanks

tglx

maintainer
#17 lore raw
Quoted 3 lines · Thomas Gleixner

Cathy,

On Fri, May 20 2022 at 18:38, Cathy Zhang wrote:

Btw, this mail has this here too:

Quoted 3 lines

Historically, microcode updates are applied by the BIOS or early in boot. In recent years, several trends have made these old approaches less palatable.

Actually, late loading is the old method. Early came after it.

Quoted 5 lines
Quoted 5 lines

First, the cadence of microcode updates has increased to deliver security mitigations. Second, the value of those updates has increased, meaning that any delay in applying them is unacceptable. Third, users have become accustomed to approaches like hot patching their kernels and have a growing aversion to reboots in general.

I had missed that argument: so how do those users update their kernels? Livepatching? I don't think you can replace a whole live kernel - that would be magic. Unless you kexec but then you can early load microcode too.

So if you reboot your kernel because you've installed a new one, you can just as well update microcode.

So sorry but I'm not buying this argument.

For cloud vendors who cannot reboot because they've promised their users ponies, that's their problem. They might have a somewhat ok-ish argument.

But not for normal users - they can just as well reboot their machines and do kernel updates together with microcode.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette