Skip to contentSkip to conversation

x86/sgx: Do not limit EAUG'd pages by pre-initialization policy

PATCH In review Jarkko Sakkinen 2 participants 3 messages Mar 4, 2022 – Mar 5, 2022

Subsystems (2)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v3 1/1 x86/sgx: Do not limit EAUG'd pages by pre-initialization policy

Pre-initialization policy is meant for EADD'd pages because they are part of the enclave identity. It's a good practice to not let touch the permissions after initialization, and does provide guarantees to e.g. LSM's about the enclave.

For EAUG'd pages it should be sufficient to let mmap(), mprotect() and SGX opcodes to control the permissions. Thus effectively disable pre-initialization policy by setting vm_max_prot_bits to RWX.

Then, remove vm_run_prot_bits. For EADD'd pages the roof is where it was during construction, for EAUG'd we don't simply care. This hard to keep in-sync variable adds only a layer of complexity and nothing else.

Without vm_run_prot_bits existing, SGX_IOC_ENCLAVE_RELAX_PERMISSIONS does absolutely nothing. Therefore, it can be safely removed.

---
v3:
Remove SGX_IOC_ENCLAVE_RELAX_PERMISSIONS.
v2:
Remove vm_run_prot_bits.

---
 arch/x86/include/uapi/asm/sgx.h |  19 ----
 arch/x86/kernel/cpu/sgx/encl.c  |  17 +---
 arch/x86/kernel/cpu/sgx/encl.h  |   1 -
 arch/x86/kernel/cpu/sgx/ioctl.c | 168 +-------------------------------
 4 files changed, 9 insertions(+), 196 deletions(-)
arch/x86/include/uapi/asm/sgx.h +0 -19
index db969a2a1874..6cfdd89d3076 100644
@@ -29,8 +29,6 @@ enum sgx_page_flags {
 	_IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision)
 #define SGX_IOC_VEPC_REMOVE_ALL \
 	_IO(SGX_MAGIC, 0x04)
-#define SGX_IOC_ENCLAVE_RELAX_PERMISSIONS \
-	_IOWR(SGX_MAGIC, 0x05, struct sgx_enclave_relax_perm)
 #define SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS \
 	_IOWR(SGX_MAGIC, 0x06, struct sgx_enclave_restrict_perm)
 #define SGX_IOC_ENCLAVE_MODIFY_TYPE \
@@ -84,23 +82,6 @@ struct sgx_enclave_provision {
 	__u64 fd;
 };
 
-/**
- * struct sgx_enclave_relax_perm - parameters for ioctl
- *                                 %SGX_IOC_ENCLAVE_RELAX_PERMISSIONS
- * @offset:	starting page offset (page aligned relative to enclave base
- *		address defined in SECS)
- * @length:	length of memory (multiple of the page size)
- * @secinfo:	address for the SECINFO data containing the new permission bits
- *		for pages in range described by @offset and @length
- * @count:	(output) bytes successfully changed (multiple of page size)
- */
-struct sgx_enclave_relax_perm {
-	__u64 offset;
-	__u64 length;
-	__u64 secinfo;
-	__u64 count;
-};
-
 /**
  * struct sgx_enclave_restrict_perm - parameters for ioctl
  *                                    %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS
arch/x86/kernel/cpu/sgx/encl.c +5 -12
index 5fe7189eac9d..c350fb987a11 100644
@@ -200,15 +200,8 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->desc = addr;
 	encl_page->encl = encl;
 
-	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
-	 */
-	prot = PROT_READ | PROT_WRITE;
-	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
-	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
+	encl_page->vm_max_prot_bits = calc_vm_prot_bits(prot, 0);
 
 	epc_page = sgx_alloc_epc_page(encl_page, true);
 	if (IS_ERR(epc_page)) {
@@ -338,7 +331,7 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *vmf)
 	 * exceed the VMA permissions.
 	 */
 	vm_prot_bits = vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC);
-	page_prot_bits = entry->vm_run_prot_bits & vm_prot_bits;
+	page_prot_bits = entry->vm_max_prot_bits & vm_prot_bits;
 	/*
 	 * Add VM_SHARED so that PTE is made writable right away if VMA
 	 * and EPCM are writable (no COW in SGX).
@@ -391,7 +384,7 @@ static vm_fault_t sgx_vma_pfn_mkwrite(struct vm_fault *vmf)
 		goto out;
 	}
 
-	if (!(entry->vm_run_prot_bits & VM_WRITE))
+	if (!(entry->vm_max_prot_bits & VM_WRITE))
 		ret = VM_FAULT_SIGBUS;
 
 out:
@@ -459,7 +452,7 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start,
 	mutex_lock(&encl->lock);
 	xas_lock(&xas);
 	xas_for_each(&xas, page, PFN_DOWN(end - 1)) {
-		if (~page->vm_run_prot_bits & vm_prot_bits) {
+		if (~page->vm_max_prot_bits & vm_prot_bits) {
 			ret = -EACCES;
 			break;
 		}
arch/x86/kernel/cpu/sgx/encl.h +0 -1
index 1b6ce1da7c92..241e302e7a72 100644
@@ -28,7 +28,6 @@
 struct sgx_encl_page {
 	unsigned long desc;
 	unsigned long vm_max_prot_bits:8;
-	unsigned long vm_run_prot_bits:8;
 	enum sgx_page_type type:16;
 	struct sgx_epc_page *epc_page;
 	struct sgx_encl *encl;
arch/x86/kernel/cpu/sgx/ioctl.c +4 -164

246 lines not on this page · raw message

Signature · 1 line
2.35.1
#1 lore raw

Hi Jarkko,

Quoted 4 lines · Jarkko Sakkinen

Pre-initialization policy is meant for EADD'd pages because they are part of the enclave identity. It's a good practice to not let touch the permissions after initialization, and does provide guarantees to e.g. LSM's about the enclave.

I disagree. There are scenarios where it is indeed good practice to modify the permissions after initialization. For example, pages that may be used for relocatable code can start with RWX permissions but once the pages have been populated with the code they should be able to restrict permissions to RX only. It is not good practice to require RWX permission over their entire lifetime. Ideally pages should only have the lowest permissions possible.

Supporting the modification of permissions after initialization enables the security conscious enclave owner to support the security principle of least privilege.

Quoted 4 lines

For EAUG'd pages it should be sufficient to let mmap(), mprotect() and SGX opcodes to control the permissions. Thus effectively disable pre-initialization policy by setting vm_max_prot_bits to RWX.

Setting vm_max_prot_bits of EAUG pages to RWX results in dynamically added (RW) pages to allow RWX PTEs!

Please correct me if I am wrong but I do not see how mmap() and mprotect() will control permissions here. Will these flows be able to prevent a RWX mapping of dynamically added RW EPCM pages? From what I understand SGX controls the permissions here with sgx_encl_may_map() called during mmap() and mprotect() and it will allow such mappings after this patch. RWX mapping (VMA) with RWX PTEs of RW EPCM memory will be allowed, correct?

That means, an enclave using EAUG to expand its heap will have enclave pages with RW EPCM but yet could have RWX PTEs pointing to them after the user creates a RWX mmap().

From what I understand this circumvents the kernel's security mechanisms.

This also breaks one of the original rules of SGX as per:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/x86/sgx.rst#n74

"EPCM permissions are separate from the normal page tables. This prevents the kernel from, for instance, allowing writes to data which an enclave wishes to remain read-only."

In these changes the kernel allows all dynamically added pages to be executable - even when the enclave wishes them not to be.

Quoted 7 lines

Then, remove vm_run_prot_bits. For EADD'd pages the roof is where it was during construction, for EAUG'd we don't simply care. This hard to keep in-sync variable adds only a layer of complexity and nothing else.

Without vm_run_prot_bits existing, SGX_IOC_ENCLAVE_RELAX_PERMISSIONS does absolutely nothing. Therefore, it can be safely removed.

Removing vm_run_prot_bits cripples SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS. With this removal SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS will support the modification of EPCM permissions but PTE and VMA permissions will continue to allow the maximum access possible for the page, whether the enclave page supports the permission or not.

I find it risky to circumvent the kernel's security mechanisms and I am not comfortable signing off on this. Or am I just not understanding it correctly?

Reinette

author maintainer
#2 lore raw
Quoted 8 lines · Reinette Chatre

Hi Jarkko,

Quoted 4 lines · Jarkko Sakkinen

Pre-initialization policy is meant for EADD'd pages because they are part of the enclave identity. It's a good practice to not let touch the permissions after initialization, and does provide guarantees to e.g. LSM's about the enclave.

Quoted 7 lines

I disagree. There are scenarios where it is indeed good practice to modify the permissions after initialization. For example, pages that may be used for relocatable code can start with RWX permissions but once the pages have been populated with the code they should be able to restrict permissions to RX only. It is not good practice to require RWX permission over their entire lifetime. Ideally pages should only have the lowest permissions possible.

The only permissions kernel has real control is PTE permissions when the enclave has been initialized.

You are introducing an artificial limitation with vm_run_prot_bits that makes e.g. EMODPE more costly for no good reason, and in-kernel variable has nothing to do with the permissions. They are located in EPCM.

Quoted 3 lines

Supporting the modification of permissions after initialization enables the security conscious enclave owner to support the security principle of least privilege.

1. Kernel has the control of PTE permissions.
2. Enclave has the control of EPCM permissions.

vm_run_prot_bits does not help making anything more secure.

BR, Jarkko