security: x86/sgx: SGX vs. LSM
Subsystems (7)
cover security: x86/sgx: SGX vs. LSM
This series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at times, but I think/hope we ended up with something reasonable.
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear ALLOW_EXEC to prevent ever mapping the page with PROT_EXEC. SGX enforces the allowed perms via a new mprotect() vm_ops hook, e.g. like regular mprotect() uses MAY_{READ,WRITE,EXEC}.
ALLOW_EXEC is used to deny hings like loading an enclave from a noexec file system or from a file without EXECUTE permissions, e.g. without the ALLOW_EXEC concept, on SGX2 hardware (regardless of kernel support) userspace could EADD from a noexec file using read-only permissions, and later use mprotect() and ENCLU[EMODPE] to gain execute permissions.
ALLOW_WRITE is used in conjuction with ALLOW_EXEC to enforce SELinux's EXECMOD (or EXECMEM).
This is very much an RFC series. It's only compile tested, likely has obvious bugs, the SELinux patch could be completely harebrained, etc... My goal at this point is to get feedback at a macro level, e.g. is the core concept viable/acceptable, are there objection to hooking mprotect(), etc...
Andy and Cedric, hopefully this aligns with your general expectations based on our last discussion.
Lastly, I added a patch to allow userspace to add multiple pages in a single ioctl(). It's obviously not directly related to the security stuff, but the idea tangentially came up during earlier discussions and it's something I think the UAPI should provide (it's a tiny change). Since I was modifying the UAPI anyways, I threw it in.
Sean Christopherson (9):
x86/sgx: Remove unused local variable in sgx_encl_release() x86/sgx: Do not naturally align MAP_FIXED address x86/sgx: Allow userspace to add multiple pages in single ioctl()
mm: Introduce vm_ops->mprotect()
x86/sgx: Restrict mapping without an enclave page to PROT_NONE x86/sgx: Require userspace to provide allowed prots to ADD_PAGES x86/sgx: Enforce noexec filesystem restriction for enclaves
LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX security/selinux: Add enclave_load() implementation
arch/x86/include/uapi/asm/sgx.h | 30 ++++-- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 143 +++++++++++++++++-------- arch/x86/kernel/cpu/sgx/driver/main.c | 13 ++- arch/x86/kernel/cpu/sgx/encl.c | 31 +++++- arch/x86/kernel/cpu/sgx/encl.h | 4 + include/linux/lsm_hooks.h | 16 +++ include/linux/mm.h | 2 + include/linux/security.h | 2 + mm/mprotect.c | 15 ++- security/security.c | 8 ++ security/selinux/hooks.c | 85 +++++++++++++++ 11 files changed, 290 insertions(+), 59 deletions(-)
Signature · 1 line
2.21.0
RFC PATCH 1/9 x86/sgx: Remove unused local variable in sgx_encl_release()
--- arch/x86/kernel/cpu/sgx/encl.c | 1 - 1 file changed, 1 deletion(-)
arch/x86/kernel/cpu/sgx/encl.c +0 -1
@@ -463,7 +463,6 @@ EXPORT_SYMBOL_GPL(sgx_encl_destroy);void sgx_encl_release (struct kref *ref) { struct sgx_encl *encl = container_of(ref, struct sgx_encl, refcount); -struct sgx_encl_mm *encl_mm;if (encl->pm_notifier.notifier_call)unregister_pm_notifier (&encl->pm_notifier);
Signature · 1 line
2.21.0
Re: [RFC PATCH 1/9] x86/sgx: Remove unused local variable in sgx_encl_release()
Quoted 2 lines · Sean Christopherson
Signed-off-by Sean Christopherson <[email protected]>---
How this patch is essential to demonstrate anything?
/Jarkko
RFC PATCH 2/9 x86/sgx: Do not naturally align MAP_FIXED address
In reply to Sean ChristophersonSGX enclaves have an associated Enclave Linear Range (ELRANGE) that is tracked and enforced by the CPU using a base+mask approach, similar to how hardware range registers such as the variable MTRRs. As a result, the ELRANGE must be naturally sized and aligned.
To reduce boilerplate code that would be needed in every userspace enclave loader, the SGX driver naturally aligns the mmap() address and also requires the range to be naturally sized. Unfortunately, SGX fails to grant a waiver to the MAP_FIXED case, e.g. incorrectly rejects mmap() if userspace is attempting to map a small slice of an existing enclave.
--- arch/x86/kernel/cpu/sgx/driver/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/driver/main.c +7 -1
@@ -79,7 +79,13 @@ static unsigned long sgx_get_unmapped_area(struct file *file, unsigned long pgoff, unsigned long flags) { - if (len < 2 * PAGE_SIZE || len & (len - 1) || flags & MAP_PRIVATE) + if (flags & MAP_PRIVATE) + return -EINVAL; + +if (flags &MAP_FIXED ) +return addr; + +if (len <2 *PAGE_SIZE || len & (len -1 ))return -EINVAL ; addr = current->mm->get_unmapped_area(file, addr, 2 * len, pgoff,
Signature · 1 line
2.21.0
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 5 lines · Sean Christopherson
SGX enclaves have an associated Enclave Linear Range (ELRANGE) that is tracked and enforced by the CPU using a base+mask approach, similar to how hardware range registers such as the variable MTRRs. As a result, the ELRANGE must be naturally sized and aligned.
Quoted 7 lines
To reduce boilerplate code that would be needed in every userspace enclave loader, the SGX driver naturally aligns the
mmap()address and also requires the range to be naturally sized. Unfortunately, SGX fails to grant a waiver to theMAP_FIXEDcase, e.g. incorrectly rejectsmmap()if userspace is attempting to map a small slice of an existing enclave.Signed-off-by Sean Christopherson <[email protected]>
Why you want to allow mmap() to be called multiple times? mmap() could be allowed only once with PROT_NONE and denied afterwards. Is this for sending fd to another process that would map already existing enclave?
I don't see any checks for whether the is enclave underneath. Also, I think that in all cases mmap() callback should allow only PROT_NONE as permissions for clarity even if it could called multiple times.
/Jarkko
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 15 lines · Jarkko Sakkinen
Quoted 5 lines · Sean Christopherson
SGX enclaves have an associated Enclave Linear Range (ELRANGE) that is tracked and enforced by the CPU using a base+mask approach, similar to how hardware range registers such as the variable MTRRs. As a result, the ELRANGE must be naturally sized and aligned.
Quoted 7 lines
To reduce boilerplate code that would be needed in every userspace enclave loader, the SGX driver naturally aligns the
mmap()address and also requires the range to be naturally sized. Unfortunately, SGX fails to grant a waiver to theMAP_FIXEDcase, e.g. incorrectly rejectsmmap()if userspace is attempting to map a small slice of an existing enclave.Signed-off-by Sean Christopherson <[email protected]>
Quoted 8 lines
Why you want to allow
mmap()to be called multiple times?mmap()could be allowed only once withPROT_NONEand denied afterwards. Is this for sending fd to another process that would map already existing enclave?I don't see any checks for whether the is enclave underneath. Also, I think that in all cases
mmap()callback should allow onlyPROT_NONEas permissions for clarity even if it could called multiple times.
What's the advantage to only allowing PROT_NONE? The idea here is to allow a PROT_NONE map followed by some replacemets that overlay it for the individual segments. Admittedly, mprotect() can do the same thing, but disallowing mmap() seems at least a bit surprising.
RE: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 31 lines
From: [email protected] [mailto:linux-sgx- [email protected]] On Behalf Of Andy Lutomirski
Sent: Tuesday, June 04, 2019 1:16 PMQuoted 7 lines · Jarkko Sakkinen
Quoted 5 lines · Sean Christopherson
SGX enclaves have an associated Enclave Linear Range (ELRANGE) that is tracked and enforced by the CPU using a base+mask approach, similar to how hardware range registers such as the variable MTRRs. As a result, the ELRANGE must be naturally sized and aligned.
Quoted 5 lines
Quoted 5 lines
To reduce boilerplate code that would be needed in every userspace enclave loader, the SGX driver naturally aligns the
mmap()address and also requires the range to be naturally sized. Unfortunately, SGX fails to grant a waiver to theMAP_FIXEDcase, e.g. incorrectly rejectsmmap()if userspace is attempting to map a small slice of anexisting enclave.
Quoted 11 lines
Quoted 2 lines
Signed-off-by Sean Christopherson <[email protected]>Why you want to allow
mmap()to be called multiple times?mmap()could be allowed only once withPROT_NONEand denied afterwards. Is this for sending fd to another process that would map already existing enclave?I don't see any checks for whether the is enclave underneath. Also, I think that in all cases
mmap()callback should allow onlyPROT_NONEas permissions for clarity even if it could called multiple times.
Quoted 4 lines
What's the advantage to only allowing
PROT_NONE? The idea here is to allow aPROT_NONEmap followed by some replacemets that overlay it for the individual segments. Admittedly,mprotect()can do the same thing, but disallowingmmap()seems at least a bit surprising.
Disallowing mmap() is not only surprising but also unnecessary.
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that vma->vm_file->private_data keeps a pointer to the enclave, why do we store it again in vma->vm_private? It isn't a big deal but non-NULL vm_private does prevent mprotect() from merging adjacent VMAs.
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 4 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why do we store it again invma->vm_private? It isn't a big deal but non-NULLvm_privatedoes preventmprotect()from merging adjacent VMAs.
vma->vm_ops->close also prevents merging, and we need that to refcount the enclave and mm.
We also rely on nullifying vma->vm_private_data in the unlikely event that adding a new mm to the enclave fails on kzalloc().
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
In reply to Xing, CedricQuoted 5 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why do we store it again invma->vm_private? It isn't a big deal but non-NULLvm_privatedoes preventmprotect()from merging adjacent VMAs.
Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
/Jarkko
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 11 lines · Jarkko Sakkinen
Quoted 7 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why do we store it again invma->vm_private? It isn't a big deal but non-NULLvm_privatedoes preventmprotect()from merging adjacent VMAs.
Quoted 4 lines
Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
The file should be properly refcounted — vm_file should not go away while it’s mapped.
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 11 lines · Andy Lutomirski
Quoted 11 lines · Jarkko Sakkinen
Quoted 7 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why do we store it again invma->vm_private? It isn't a big deal but non-NULLvm_privatedoes preventmprotect()from merging adjacent VMAs.
Quoted 6 lines
Quoted 4 lines
Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
The file should be properly refcounted —
vm_fileshould not go away while it’s mapped.
Right, makes sense. It is easy one to change essentially just removing internal refcount from sgx_encl and using file for the same. I'll update this to my tree along with the changes to remove LKM/ACPI bits ASAP.
/Jarkko
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · Andy Lutomirski
Quoted 11 lines · Jarkko Sakkinen
Quoted 7 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why do we store it again invma->vm_private? It isn't a big deal but non-NULLvm_privatedoes preventmprotect()from merging adjacent VMAs.
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
The file should be properly refcounted —
vm_fileshould not go away while it’s mapped.
mm already takes care of that so vm_file does not go away. Still we need an internal refcount for enclaves to synchronize with the swapper. In summary nothing needs to be done.
Quoted 3 lines
Right, makes sense. It is easy one to change essentially just removing internal refcount from
sgx_encland using file for the same. I'll update this to my tree along with the changes to remove LKM/ACPI bits ASAP.
/Jarkko
RE: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 23 lines
From: Jarkko Sakkinen [mailto:[email protected]]
Sent: Thursday, June 13, 2019 6:48 AMQuoted 4 lines · Jarkko Sakkinen
Quoted 3 lines · Andy Lutomirski
Quoted 3 lines
On Jun 5, 2019, at 8:17 AM, Jarkko Sakkinen
Quoted 5 lines · <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why dowe store it again in
vma->vm_private?Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
It isn't a big deal but non-NULL
vm_privatedoes preventmprotect()from merging adjacent VMAs.Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
Quoted 6 lines
Quoted 1 line
Quoted 1 line
The file should be properly refcounted —
vm_fileshould not go awaywhile it’s mapped.
mm already takes care of that so
vm_filedoes not go away. Still we need an internal refcount for enclaves to synchronize with the swapper. In summary nothing needs to be done.
I don't get this. The swapper takes a read lock on mm->mmap_sem, which locks the vma, which in turn reference counts vma->vm_file. Why is the internal refcount still needed?
Quoted 7 lines
Quoted 4 lines
Right, makes sense. It is easy one to change essentially just removing internal refcount from
sgx_encland using file for the same. I'll update this to my tree along with the changes to remove LKM/ACPI bitsASAP.
/Jarkko
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 26 lines · Cedric
Quoted 26 lines
From: Jarkko Sakkinen [mailto:[email protected]]
Sent: Thursday, June 13, 2019 6:48 AMQuoted 4 lines · Jarkko Sakkinen
Quoted 3 lines · Andy Lutomirski
Quoted 3 lines
On Jun 5, 2019, at 8:17 AM, Jarkko Sakkinen
Quoted 5 lines · <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines · Cedric
A bit off topic here. This mmap()/mprotect() discussion reminds me a question (guess for Jarkko): Now that
vma->vm_file->private_datakeeps a pointer to the enclave, why dowe store it again in
vma->vm_private?Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 2 lines
It isn't a big deal but non-NULL
vm_privatedoes preventmprotect()from merging adjacent VMAs.Same semantics as with a regular mmap i.e. you can close the file and still use the mapping.
The file should be properly refcounted —
vm_fileshould not go awaywhile it’s mapped.
Quoted 7 lines
Quoted 3 lines
mm already takes care of that so
vm_filedoes not go away. Still we need an internal refcount for enclaves to synchronize with the swapper. In summary nothing needs to be done.I don't get this. The swapper takes a read lock on
mm->mmap_sem, which locks the vma, which in turn reference countsvma->vm_file. Why is the internal refcount still needed?
mmap_sem is only held when reclaim is touching PTEs, e.g. to test/clear its accessed bit and to zap the PTE. The liveliness of the enclave needs to be guaranteed for the entire duration of reclaim, e.g. we can't have the enclave disappearing when we go to do EWB. It's also worth nothing that a single reclaim may operate on more than one mmap_sem, as enclaves can be shared across processes (mm_structs).
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
Quoted 10 lines · Sean Christopherson
Quoted 3 lines
I don't get this. The swapper takes a read lock on
mm->mmap_sem, which locks the vma, which in turn reference countsvma->vm_file. Why is the internal refcount still needed?
mmap_semis only held when reclaim is touching PTEs, e.g. to test/clear its accessed bit and to zap the PTE. The liveliness of the enclave needs to be guaranteed for the entire duration of reclaim, e.g. we can't have the enclave disappearing when we go to do EWB. It's also worth nothing that a single reclaim may operate on more than onemmap_sem, as enclaves can be shared across processes (mm_structs).
Anyway, the takeaway I got from this is that encl->refcount does not need to be updated for VMAs (sent a patch to linux-sgx that I plan merge).
/Jarkko
Re: [RFC PATCH 2/9] x86/sgx: Do not naturally align MAP_FIXED address
In reply to Andy LutomirskiQuoted 26 lines · Andy Lutomirski
Quoted 15 lines · Jarkko Sakkinen
Quoted 5 lines · Sean Christopherson
SGX enclaves have an associated Enclave Linear Range (ELRANGE) that is tracked and enforced by the CPU using a base+mask approach, similar to how hardware range registers such as the variable MTRRs. As a result, the ELRANGE must be naturally sized and aligned.
Quoted 7 lines
To reduce boilerplate code that would be needed in every userspace enclave loader, the SGX driver naturally aligns the
mmap()address and also requires the range to be naturally sized. Unfortunately, SGX fails to grant a waiver to theMAP_FIXEDcase, e.g. incorrectly rejectsmmap()if userspace is attempting to map a small slice of an existing enclave.Signed-off-by Sean Christopherson <[email protected]>Quoted 8 lines
Why you want to allow
mmap()to be called multiple times?mmap()could be allowed only once withPROT_NONEand denied afterwards. Is this for sending fd to another process that would map already existing enclave?I don't see any checks for whether the is enclave underneath. Also, I think that in all cases
mmap()callback should allow onlyPROT_NONEas permissions for clarity even if it could called multiple times.
Quoted 4 lines
What's the advantage to only allowing
PROT_NONE? The idea here is to allow aPROT_NONEmap followed by some replacemets that overlay it for the individual segments. Admittedly,mprotect()can do the same thing, but disallowingmmap()seems at least a bit surprising.
I was merely wondering if it is specifically for the application where a client process would mmap(MAP_FIXED) an enclave created by a server process.
/Jarkko
RFC PATCH 3/9 x86/sgx: Allow userspace to add multiple pages in single ioctl()
In reply to Sean Christopherson...to improve performance when building enclaves by reducing the number of user<->system transitions. Rather than provide arbitrary batching, e.g. with per-page SECINFO and mrmask, take advantage of the fact that any sane enclave will have large swaths of pages with identical properties, e.g. code vs. data sections.
For simplicity and stability in the initial implementation, loop over the existing add page flow instead of taking a more agressive approach, which would require tracking transitions between VMAs and holding mmap_sem for an extended duration.
--- arch/x86/include/uapi/asm/sgx.h | 21 ++--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 104 +++++++++++++++---------- 2 files changed, 74 insertions(+), 51 deletions(-)
arch/x86/include/uapi/asm/sgx.h +11 -10
@@ -12,8 +12,8 @@#define SGX_IOC_ENCLAVE_CREATE \ _IOW(SGX_MAGIC, 0x00, struct sgx_enclave_create) -#define SGX_IOC_ENCLAVE_ADD_PAGE \ - _IOW(SGX_MAGIC, 0x01, struct sgx_enclave_add_page) +#define SGX_IOC_ENCLAVE_ADD_PAGES \ + _IOW(SGX_MAGIC, 0x01, struct sgx_enclave_add_pages)#define SGX_IOC_ENCLAVE_INIT \ _IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init)#define SGX_IOC_ENCLAVE_SET_ATTRIBUTE \
@@ -32,21 +32,22 @@ struct sgx_enclave_create { };/** -* struct sgx_enclave_add_page - parameter structure for the -* %SGX_IOC_ENCLAVE_ADD_PAGE ioctl -* @addr: address within the ELRANGE -* @src: address for the page data -* @secinfo: address for the SECINFO data -* @mrmask: bitmask for the measured 256 byte chunks +* struct sgx_enclave_add_pages - parameter structure for the +* %SGX_IOC_ENCLAVE_ADD_PAGES ioctl +* @addr: start address within the ELRANGE +* @src: start address for the pages' data +* @secinfo: address for the SECINFO data (common to all pages) +* @nr_pages: number of pages (must be virtually contiguous) +* @mrmask: bitmask for the measured 256 byte chunks (common to all pages) */ -struct sgx_enclave_add_page { +struct sgx_enclave_add_pages {__u64 addr;__u64 src;__u64 secinfo; +__u32 nr_pages;__u16 mrmask; } __attribute__((__packed__ )); -/** * struct sgx_enclave_init - parameter structure for the * %SGX_IOC_ENCLAVE_INIT ioctl
arch/x86/kernel/cpu/sgx/driver/ioctl.c +63 -41
153 lines not on this page · raw message
Signature · 1 line
2.21.0
RE: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 35 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMarch/x86/kernel/cpu/sgx/driver/ioctl.c +32
-0+/** +* sgx_ioc_enclave_add_pages - handler for %SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND). +* The range of pages must be virtually contiguous. The SECINFO and +* measurement maskare applied to all pages, i.e. pages with different +* properties must be added in separate calls. +* +* EADD and EEXTEND are done asynchronously via worker threads. A +successful +* sgx_ioc_enclave_add_page() only indicates the pages have been added +to the +* work queue, it does not guarantee adding the pages to the enclave +will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages (struct file *filep,unsigned int cmd, +unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo;
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +10
-0+unsigned int i; +int ret; + +if (copy_from_user (&secinfo, (void __user *)addp->secinfo, +sizeof (secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;
If interrupted, how would user mode code know how many pages have been EADD'ed?
Quoted 24 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +10
-3+ +if (need_resched ()) +cond_resched (); + + ret =sgx_encl_add_page (encl, addp->addr + i*PAGE_SIZE , + addp->src + i*PAGE_SIZE , + &secinfo, addp->mrmask); + }return ret; }@@ -823,8 +845,8 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)case SGX_IOC_ENCLAVE_CREATE : handler = sgx_ioc_enclave_create;break ; -case SGX_IOC_ENCLAVE_ADD_PAGE : - handler = sgx_ioc_enclave_add_page; +case SGX_IOC_ENCLAVE_ADD_PAGES : + handler = sgx_ioc_enclave_add_pages;break ;case SGX_IOC_ENCLAVE_INIT : handler = sgx_ioc_enclave_init; --2.21.0
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 39 lines · Cedric
Quoted 39 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMarch/x86/kernel/cpu/sgx/driver/ioctl.c +36
-0+/** +* sgx_ioc_enclave_add_pages - handler for %SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND). +* The range of pages must be virtually contiguous. The SECINFO and +* measurement maskare applied to all pages, i.e. pages with different +* properties must be added in separate calls. +* +* EADD and EEXTEND are done asynchronously via worker threads. A +successful +* sgx_ioc_enclave_add_page() only indicates the pages have been added +to the +* work queue, it does not guarantee adding the pages to the enclave +will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages (struct file *filep,unsigned int cmd, +unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo; +unsigned int i; +int ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo,
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6
-0+ sizeof(secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have been EADD'ed?
Hmm, updating nr_pages would be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.
Quoted 25 lines
Quoted 24 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +10
-3+ +if (need_resched ()) +cond_resched (); + + ret =sgx_encl_add_page (encl, addp->addr + i*PAGE_SIZE , + addp->src + i*PAGE_SIZE , + &secinfo, addp->mrmask); + }return ret; }@@ -823,8 +845,8 @@ long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)case SGX_IOC_ENCLAVE_CREATE : handler = sgx_ioc_enclave_create;break ; -case SGX_IOC_ENCLAVE_ADD_PAGE : - handler = sgx_ioc_enclave_add_page; +case SGX_IOC_ENCLAVE_ADD_PAGES : + handler = sgx_ioc_enclave_add_pages;break ;case SGX_IOC_ENCLAVE_INIT : handler = sgx_ioc_enclave_init; --2.21.0
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 49 lines · Sean Christopherson
Quoted 39 lines · Cedric
Quoted 39 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMarch/x86/kernel/cpu/sgx/driver/ioctl.c +36
-0+/** +* sgx_ioc_enclave_add_pages - handler for %SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND). +* The range of pages must be virtually contiguous. The SECINFO and +* measurement maskare applied to all pages, i.e. pages with different +* properties must be added in separate calls. +* +* EADD and EEXTEND are done asynchronously via worker threads. A +successful +* sgx_ioc_enclave_add_page() only indicates the pages have been added +to the +* work queue, it does not guarantee adding the pages to the enclave +will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages (struct file *filep,unsigned int cmd, +unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo; +unsigned int i; +int ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo,Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6
-0+ sizeof(secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have been EADD'ed?
Quoted 2 lines
Hmm, updating
nr_pageswould be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.
Or maybe update @addr and @src as well? That would allow userspace to re-invoke the ioctl() without having to modify the struct.
RE: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 57 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 1:40 PMQuoted 4 lines · Sean Christopherson
Quoted 3 lines · Cedric
Quoted 3 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMQuoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +11
-0+/** +* sgx_ioc_enclave_add_pages - handler for +%SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND).
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
+ * The range of pages must be virtually contiguous. The SECINFO
(quoted diff) +10
-0+and + * measurement maskare applied to all pages, i.e. pages with +different + * properties must be added in separate calls. + * + * EADD and EEXTEND are done asynchronously via worker threads. +A successful + * sgx_ioc_enclave_add_page() only indicates the pages have been +added to the + * work queue, it does not guarantee adding the pages to theQuoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +8
-0+enclave will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages(struct file *filep,unsigned int cmd,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +9
-0+ unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo; +unsigned int i; +int ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo,Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6
-0+ sizeof(secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have
Quoted 7 lines
been EADD'ed?
Quoted 3 lines
Hmm, updating
nr_pageswould be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.Or maybe update @addr and @src as well? That would allow userspace to re-invoke the
ioctl()without having to modify the struct.
How about returning the number of pages (or bytes) EADD'ed, similar to write() syscall?
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 59 lines · Cedric
Quoted 59 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 1:40 PMQuoted 4 lines · Sean Christopherson
Quoted 3 lines · Cedric
Quoted 3 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMQuoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +11
-0+/** +* sgx_ioc_enclave_add_pages - handler for +%SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND).
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
+ * The range of pages must be virtually contiguous. The SECINFO
(quoted diff) +10
-0+and + * measurement maskare applied to all pages, i.e. pages with +different + * properties must be added in separate calls. + * + * EADD and EEXTEND are done asynchronously via worker threads. +A successful + * sgx_ioc_enclave_add_page() only indicates the pages have been +added to the + * work queue, it does not guarantee adding the pages to theQuoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +8
-0+enclave will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages(struct file *filep,unsigned int cmd,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +9
-0+ unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo; +unsigned int i; +int ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo,Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6
-0+ sizeof(secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have
been EADD'ed?
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Hmm, updating
nr_pageswould be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.Or maybe update @addr and @src as well? That would allow userspace to re-invoke the
ioctl()without having to modify the struct.How about returning the number of pages (or bytes) EADD'ed, similar to
write()syscall?
I thought about that as well, but I think it'd be useful to update the offset on any failure, not just -ERESTARTSYS.
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
In reply to Sean ChristophersonQuoted 51 lines · Sean Christopherson
Quoted 49 lines · Sean Christopherson
Quoted 47 lines · Cedric
Quoted 35 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMarch/x86/kernel/cpu/sgx/driver/ioctl.c +32
-0+/** +* sgx_ioc_enclave_add_pages - handler for %SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND). +* The range of pages must be virtually contiguous. The SECINFO and +* measurement maskare applied to all pages, i.e. pages with different +* properties must be added in separate calls. +* +* EADD and EEXTEND are done asynchronously via worker threads. A +successful +* sgx_ioc_enclave_add_page() only indicates the pages have been added +to the +* work queue, it does not guarantee adding the pages to the enclave +will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages (struct file *filep,unsigned int cmd, +unsigned long arg) +{ +struct sgx_enclave_add_pages *addp = (void *)arg; +struct sgx_encl *encl = filep->private_data; +struct sgx_secinfo secinfo;arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +10
-0+unsigned int i; +int ret; + +if (copy_from_user (&secinfo, (void __user *)addp->secinfo, +sizeof (secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have been EADD'ed?
Quoted 5 lines
Quoted 2 lines
Hmm, updating
nr_pageswould be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.Or maybe update @addr and @src as well? That would allow userspace to re-invoke the
ioctl()without having to modify the struct.
If you're going to use -ERESTARTSYS, that's the way to go. -EINTR would be an alternative. A benefit of -ERESTARTSYS is that, with -EINTR, it wouldn't be that surprising for user code to simply fail to handle it.
RE: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 62 lines
From: Andy Lutomirski [mailto:[email protected]]
Sent: Tuesday, June 04, 2019 1:18 PMQuoted 6 lines · Sean Christopherson
Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Cedric
Quoted 3 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMQuoted 11 lines
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +11
-0+/** +* sgx_ioc_enclave_add_pages - handler for +%SGX_IOC_ENCLAVE_ADD_PAGES +* +* @filep: open file to /dev/sgx +* @cmd: the command value +* @arg: pointer to an &sgx_enclave_add_page instance +* +* Add a range of pages to an uninitialized enclave (EADD), and +optionally +* extend the enclave's measurement with the contents of the page (EEXTEND).
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
+ * The range of pages must be virtually contiguous. The
(quoted diff) +10
-0+SECINFO and + * measurement maskare applied to all pages, i.e. pages with +different + * properties must be added in separate calls. + * + * EADD and EEXTEND are done asynchronously via worker threads. +A successful + * sgx_ioc_enclave_add_page() only indicates the pages have +been added to the + * work queue, it does not guarantee adding the pages to theQuoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +8
-0+enclave will +* succeed. +* +* Return: +* 0 on success, +* -errno otherwise +*/ +static long sgx_ioc_enclave_add_pages(struct file *filep,unsigned int cmd,
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+ unsigned long arg) { struct +sgx_enclave_add_pages *addp = (void *)arg; struct sgx_encl +*encl = filep->private_data; struct sgx_secinfo secinfo; +unsigned int i; int ret; + + if (copy_from_user(&secinfo, (void __user *)addp->secinfo,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 641
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +6
-0+ sizeof(secinfo))) +return -EFAULT ; + +for (i =0 , ret =0 ; i < addp->nr_pages && !ret; i++) { +if (signal_pending (current)) +return -ERESTARTSYS ;If interrupted, how would user mode code know how many pages have
been EADD'ed?
Quoted 4 lines
Quoted 3 lines
Hmm, updating
nr_pageswould be fairly simple and shouldn't confuse userspace, e.g. as opposed to overloading the return value.
Quoted 6 lines
Quoted 2 lines
Or maybe update @addr and @src as well? That would allow userspace to re-invoke the
ioctl()without having to modify the struct.If you're going to use
-ERESTARTSYS, that's the way to go.-EINTRwould be an alternative. A benefit of-ERESTARTSYSis that, with-EINTR, it wouldn't be that surprising for user code to simply fail to handle it.
-EINTR means the call was interrupted before anything could be done. Am I correct?
But in this case some pages have been processed already so I guess we cannot return any error code. I think it more reasonable to return the number of pages (or bytes) processed.
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
In reply to Sean Christophersonarch/x86/include/uapi/asm/sgx.h struct sgx_enclave_create { · line 49
Quoted 8 lines · Sean Christopherson
arch/x86/include/uapi/asm/sgx.h +2
-1-struct sgx_enclave_add_page { +struct sgx_enclave_add_pages {__u64 addr;__u64 src;__u64 secinfo; +__u32 nr_pages;__u16 mrmask; } __attribute__((__packed__ ));
IMNHO this follows a user interface anti-pattern: exposing page sizes where not strictly required.
Think of how this would look to an application if page size was variable. With this interface, they always need to scale their operations by page size instead of just aligning it.
BTW, why is nr_pages a u32? Do we never envision a case where you can add more than 4TB of memory to an enclave? ;)
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 13 lines · Dave Hansen
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_create { · line 49
Quoted 8 lines · Sean Christopherson
arch/x86/include/uapi/asm/sgx.h +2
-1-struct sgx_enclave_add_page { +struct sgx_enclave_add_pages {__u64 addr;__u64 src;__u64 secinfo; +__u32 nr_pages;__u16 mrmask; } __attribute__((__packed__ ));IMNHO this follows a user interface anti-pattern: exposing page sizes where not strictly required.
Quoted 3 lines
Think of how this would look to an application if page size was variable. With this interface, they always need to scale their operations by page size instead of just aligning it.
I briefly considered taking size in bytes, but I took a shortcut because EPC pages are architecturally defined to be 4k sized and aligned. That being said, I don't necessarily disagree, especially if nr_pages isn't squeezed into a u32.
Quoted 2 lines
BTW, why is
nr_pagesa u32? Do we never envision a case where you can add more than 4TB of memory to an enclave? ;)
Heh, fair enough. IIRC, a while back someone posted about having problems building a 512gb enclave in a 92mb EPC...
How about this for the intermediate patch:
struct sgx_enclave_add_region {
__u64 addr;
__u64 src;
__u64 size;
__u64 secinfo;
__u16 mrmask;
__u16 reserved16;
__u32 reserved;
}and with the flags field:
struct sgx_enclave_add_region {
__u64 addr;
__u64 src;
__u64 size;
__u64 secinfo;
__u16 mrmask;
__u16 flags;
__u32 reserved;
}Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 5 lines · Sean Christopherson
Heh, fair enough. IIRC, a while back someone posted about having problems building a 512gb enclave in a 92mb EPC...
How about this for the intermediate patch:
Quoted 9 lines
struct sgx_enclave_add_region {
__u64 addr;
__u64 src;
__u64 size;
__u64 secinfo;
__u16 mrmask;
__u16 reserved16;
__u32 reserved;
}Looks fine to me.
RE: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
In reply to Sean ChristophersonQuoted 69 lines
-----Original Message-----
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 1:37 PM
To: Hansen, Dave <[email protected]>
Cc: Jarkko Sakkinen <[email protected]>; Andy Lutomirski <[email protected]>; Xing, Cedric <[email protected]>; Stephen Smalley <[email protected]>; James Morris <[email protected]>; Serge E . Hallyn <[email protected]>; LSM List <[email protected]>; Paul Moore <[email protected]>; Eric Paris <[email protected]>; [email protected]; Jethro Beekman <[email protected]>; Thomas Gleixner <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; LKML <[email protected]>; X86 ML <[email protected]>; [email protected]; Andrew Morton <akpm@linux- foundation.org>; [email protected]; [email protected]; Ayoun, Serge <[email protected]>; Katz-zamir, Shay <[email protected]>; Huang, Haitao <[email protected]>; Andy Shevchenko <[email protected]>; Svahn, Kai <[email protected]>; Borislav Petkov <[email protected]>; Josh Triplett <[email protected]>; Huang, Kai <[email protected]>; David Rientjes <[email protected]>; Roberts, William C <[email protected]>; Tricca, Philip B <[email protected]>
Subject: Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in singleioctl()Quoted 13 lines · Dave Hansen
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_create { · line 49
Quoted 8 lines · Sean Christopherson
arch/x86/include/uapi/asm/sgx.h +2
-1-struct sgx_enclave_add_page { +struct sgx_enclave_add_pages {__u64 addr;__u64 src;__u64 secinfo; +__u32 nr_pages;__u16 mrmask; } __attribute__((__packed__ ));IMNHO this follows a user interface anti-pattern: exposing page sizes where not strictly required.
Quoted 3 lines
Think of how this would look to an application if page size was variable. With this interface, they always need to scale their operations by page size instead of just aligning it.
I briefly considered taking size in bytes, but I took a shortcut because EPC pages are architecturally defined to be 4k sized and aligned. That being said, I don't necessarily disagree, especially if
nr_pagesisn't squeezed into a u32.Quoted 2 lines
BTW, why is
nr_pagesa u32? Do we never envision a case where you can add more than 4TB of memory to an enclave? ;)Heh, fair enough. IIRC, a while back someone posted about having problems building a 512gb enclave in a 92mb EPC...
How about this for the intermediate patch:
struct sgx_enclave_add_region { __u64 addr; __u64 src; __u64 size; __u64 secinfo; __u16 mrmask; __u16 reserved16; __u32 reserved; }and with the flags field:
Quoted 7 lines
struct sgx_enclave_add_region {
__u64 addr;
__u64 src;
__u64 size;
__u64 secinfo;
__u16 mrmask;
__u16 flags;What is "flags" here?
Quoted 2 lines
__u32 reserved; }
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
Quoted 22 lines · Cedric
Quoted 14 lines
How about this for the intermediate patch:
struct sgx_enclave_add_region { __u64 addr; __u64 src; __u64 size; __u64 secinfo; __u16 mrmask; __u16 reserved16; __u32 reserved; }and with the flags field:
Quoted 7 lines
struct sgx_enclave_add_region { __u64 addr; __u64 src; __u64 size; __u64 secinfo; __u16 mrmask; __u16 flags;
Quoted 1 line
What is "flags" here?
In the RFC, @flags holds SGX_ALLOW_{READ,WRITE,EXEC}.
Quoted 3 lines
Quoted 3 lines
__u32 reserved; }
Re: [RFC PATCH 3/9] x86/sgx: Allow userspace to add multiple pages in single ioctl()
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
...to improve performance when building enclaves by reducing the number of user<->system transitions. Rather than provide arbitrary batching, e.g. with per-page SECINFO and mrmask, take advantage of the fact that any sane enclave will have large swaths of pages with identical properties, e.g. code vs. data sections.
Quoted 6 lines
For simplicity and stability in the initial implementation, loop over the existing add page flow instead of taking a more agressive approach, which would require tracking transitions between VMAs and holding
mmap_semfor an extended duration.Signed-off-by Sean Christopherson <[email protected]>
I think this completely ruins the rest of the series. We should first get the model for security done (including documentation). I would even send v21 with just that update because this series does not even apply to the mainline.
I would request an update to the series with just the change to the security model. Also the very first should be dropped as it is completely unrelated cosmetic fix.
/Jarkko
RFC PATCH 4/9 mm: Introduce vm_ops->mprotect()
In reply to Sean ChristophersonSGX will use the mprotect() hook to prevent userspace from circumventing various security checks, i.e. Linux Security Modules.
Enclaves are built by copying data from normal memory into the Enclave Page Cache (EPC). Due to the nature of SGX, the EPC is represented by a single file that must be MAP_SHARED, i.e. mprotect() only ever sees a single MAP_SHARED vm_file. Furthermore, all enclaves will need read, write and execute pages in the EPC.
As a result, LSM policies cannot be meaningfully applied, e.g. an LSM can deny access to the EPC as a whole, but can't deny PROT_EXEC on page that originated in a non-EXECUTE file (which is long gone by the time mprotect() is called).
By hooking mprotect(), SGX can make explicit LSM upcalls while an enclave is being built, i.e. when the kernel has a handle to origin of each enclave page, and enforce the result of the LSM policy whenever userspace maps the enclave page in the future.
Alternatively, SGX could play games with MAY_{READ,WRITE,EXEC}, but that approach is quite ugly, e.g. would require userspace to call an SGX ioctl() prior to using mprotect() to extend a page's protections.
--- include/linux/mm.h | 2 ++ mm/mprotect.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/mm.h +2 -0
@@ -458,6 +458,8 @@ struct vm_operations_struct {void (*close)(struct vm_area_struct * area);int (*split)(struct vm_area_struct * area,unsigned long addr);int (*mremap)(struct vm_area_struct * area); +int (*mprotect)(struct vm_area_struct * area,unsigned long start, +unsigned long end,unsigned long prot);vm_fault_t (*fault)(struct vm_fault *vmf);vm_fault_t (*huge_fault)(struct vm_fault *vmf,enum page_entry_size pe_size);
mm/mprotect.c +11 -4
@@ -547,13 +547,20 @@ static int do_mprotect_pkey(unsigned long start, size_t len, goto out; } - error =security_file_mprotect (vma, reqprot, prot); -if (error) - goto out; - tmp = vma->vm_end;if (tmp > end) tmp = end; + +if (vma->vm_ops && vma->vm_ops->mprotect) { + error = vma->vm_ops->mprotect (vma, nstart, tmp, prot); +if (error) + goto out; + } + + error =security_file_mprotect (vma, reqprot, prot); +if (error) + goto out; + error =mprotect_fixup (vma, &prev, nstart, tmp, newflags);if (error) goto out;
Signature · 1 line
2.21.0
RE: [RFC PATCH 4/9] mm: Introduce vm_ops->mprotect()
include/linux/mm.h struct vm_operations_struct { · line 462
Quoted 13 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMinclude/linux/mm.h index 0e8834ac32b7..50a42364a885 renamed from include/linux/mm.h +0
-0100644
include/linux/mm.h +2
-0@@ -458,6 +458,8 @@ struct vm_operations_struct {void (*close)(struct vm_area_struct * area);int (*split)(struct vm_area_struct * area,unsigned long addr);int (*mremap)(struct vm_area_struct * area); +int (*mprotect)(struct vm_area_struct * area,unsigned long start, +unsigned long end,unsigned long prot);
As I commented in my reply to the cover letter, SGX driver doesn't need to intercept mprotect() if ALLOW_* flags are not spilled into it.
Quoted 3 lines
include/linux/mm.h +0
-0vm_fault_t (*fault)(struct vm_fault *vmf);vm_fault_t (*huge_fault)(struct vm_fault *vmf,enum page_entry_size pe_size);
Re: [RFC PATCH 4/9] mm: Introduce vm_ops->mprotect()
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
SGX will use the
mprotect()hook to prevent userspace from circumventing various security checks, i.e. Linux Security Modules.Enclaves are built by copying data from normal memory into the Enclave Page Cache (EPC). Due to the nature of SGX, the EPC is represented by a single file that must be
MAP_SHARED, i.e.mprotect()only ever sees a singleMAP_SHAREDvm_file. Furthermore, all enclaves will need read, write and execute pages in the EPC.
What does the last sentence is pointing out? Enclaves read, write and execute pages, so?
Quoted 4 lines
As a result, LSM policies cannot be meaningfully applied, e.g. an LSM can deny access to the EPC as a whole, but can't deny
PROT_EXECon page that originated in a non-EXECUTE file (which is long gone by the timemprotect()is called).
I'm not sure what kind of scenario this is describing where some LSM can't dent PROT_EXEC. Kind of cryptic paragraph, have to say.
Quoted 4 lines
By hooking
mprotect(), SGX can make explicit LSM upcalls while an enclave is being built, i.e. when the kernel has a handle to origin of each enclave page, and enforce the result of the LSM policy whenever userspace maps the enclave page in the future.
How does mprotect() enabled adding new LSM hooks?
Quoted 3 lines
Alternatively, SGX could play games with MAY_{READ,WRITE,EXEC}, but that approach is quite ugly, e.g. would require userspace to call an SGX
ioctl()prior to usingmprotect()to extend a page's protections.
Not really sure I got this. SGX gets page permissions in SECINFO. Also recurring comment about MAY_* constants.
Quoted 11 lines
Signed-off-by Sean Christopherson <[email protected]>--- include/linux/mm.h | 2 ++ mm/mprotect.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)include/linux/mm.h +0
-0
include/linux/mm.h struct vm_operations_struct { · line 462
Quoted 5 lines
include/linux/mm.h +2
-0@@ -458,6 +458,8 @@ struct vm_operations_struct {void (*close)(struct vm_area_struct * area);int (*split)(struct vm_area_struct * area,unsigned long addr);int (*mremap)(struct vm_area_struct * area); +int (*mprotect)(struct vm_area_struct * area,unsigned long start, +unsigned long end,unsigned long prot);
Right, the hook must be here obviously because mprotect() can be called when /dev/sgx/enclave is closed. Can you describe start and end i.e. what range they are in?
Quoted 16 lines
include/linux/mm.h +0
-0vm_fault_t (*fault)(struct vm_fault *vmf);vm_fault_t (*huge_fault)(struct vm_fault *vmf,enum page_entry_size pe_size);mm/mprotect.c +0
-4@@ -547,13 +547,20 @@ static int do_mprotect_pkey(unsigned long start, size_t len, goto out; } - error =security_file_mprotect (vma, reqprot, prot); -if (error) - goto out; - tmp = vma->vm_end;
mm/mprotect.c static int do_mprotect_pkey(unsigned long start, size_t len, · line 560
Quoted 10 lines
mm/mprotect.c +8
-0if (tmp > end) tmp = end; + +if (vma->vm_ops && vma->vm_ops->mprotect) { + error = vma->vm_ops->mprotect (vma, nstart, tmp, prot); +if (error) + goto out; + } + + error =security_file_mprotect (vma, reqprot, prot);
Why is mprotect callback called post the LSM hook?
mm/mprotect.c static int do_mprotect_pkey(unsigned long start, size_t len, · line 562
Quoted 2 lines
mm/mprotect.c +2
-0+if (error) + goto out;
/Jarkko
Re: [RFC PATCH 4/9] mm: Introduce vm_ops->mprotect()
In reply to Sean ChristophersonQuoted 59 lines · Sean Christopherson
SGX will use the
mprotect()hook to prevent userspace from circumventing various security checks, i.e. Linux Security Modules.Enclaves are built by copying data from normal memory into the Enclave Page Cache (EPC). Due to the nature of SGX, the EPC is represented by a single file that must be
MAP_SHARED, i.e.mprotect()only ever sees a singleMAP_SHAREDvm_file. Furthermore, all enclaves will need read, write and execute pages in the EPC.As a result, LSM policies cannot be meaningfully applied, e.g. an LSM can deny access to the EPC as a whole, but can't deny
PROT_EXECon page that originated in a non-EXECUTE file (which is long gone by the timemprotect()is called).By hooking
mprotect(), SGX can make explicit LSM upcalls while an enclave is being built, i.e. when the kernel has a handle to origin of each enclave page, and enforce the result of the LSM policy whenever userspace maps the enclave page in the future.Alternatively, SGX could play games with MAY_{READ,WRITE,EXEC}, but that approach is quite ugly, e.g. would require userspace to call an SGX
ioctl()prior to usingmprotect()to extend a page's protections.Signed-off-by Sean Christopherson <[email protected]>--- include/linux/mm.h | 2 ++ mm/mprotect.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-)include/linux/mm.h +2
-0@@ -458,6 +458,8 @@ struct vm_operations_struct {void (*close)(struct vm_area_struct * area);int (*split)(struct vm_area_struct * area,unsigned long addr);int (*mremap)(struct vm_area_struct * area); +int (*mprotect)(struct vm_area_struct * area,unsigned long start, +unsigned long end,unsigned long prot);vm_fault_t (*fault)(struct vm_fault *vmf);vm_fault_t (*huge_fault)(struct vm_fault *vmf,enum page_entry_size pe_size);mm/mprotect.c +1
-4@@ -547,13 +547,20 @@ static int do_mprotect_pkey(unsigned long start, size_t len, goto out; } - error =security_file_mprotect (vma, reqprot, prot); -if (error) - goto out; - tmp = vma->vm_end;if (tmp > end) tmp = end; +
mm/mprotect.c static int do_mprotect_pkey(unsigned long start, size_t len, · line 563
Quoted 10 lines
mm/mprotect.c +10
-0+if (vma->vm_ops && vma->vm_ops->mprotect) { + error = vma->vm_ops->mprotect (vma, nstart, tmp, prot); +if (error) + goto out; + } + + error =security_file_mprotect (vma, reqprot, prot); +if (error) + goto out; +
I think that, if you're going to do it like this, you need to call it mprotect_and_check_security or something. Or you could just add .may_mprotect, which is allowed to fail but, on success, falls through to call security_file_mprotect and mprotect_fixup().
--Andy
RFC PATCH 5/9 x86/sgx: Restrict mapping without an enclave page to PROT_NONE
In reply to Sean ChristophersonTo support LSM integration, SGX will require userspace to explicitly specify the allowed protections for each page. The allowed protections will be supplied to and modified by LSMs (based on their policies). To prevent userspace from circumventing the allowed protections, do not allow PROT_{READ,WRITE,EXEC} mappings to an enclave without an associated enclave page (which will track the allowed protections).
--- arch/x86/kernel/cpu/sgx/driver/main.c | 5 +++++ arch/x86/kernel/cpu/sgx/encl.c | 30 +++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/encl.h | 3 +++ 3 files changed, 38 insertions(+)
arch/x86/kernel/cpu/sgx/driver/main.c +5 -0
@@ -63,6 +63,11 @@ static long sgx_compat_ioctl(struct file *filep, unsigned int cmd,static int sgx_mmap (struct file *file,struct vm_area_struct *vma) {struct sgx_encl *encl = file->private_data; +int ret; + + ret =sgx_map_allowed (encl, vma->vm_start, vma->vm_end, vma->vm_flags); +if (ret) +return ret; vma->vm_ops = &sgx_vm_ops; vma->vm_flags |=VM_PFNMAP |VM_DONTEXPAND |VM_DONTDUMP |VM_IO ;
arch/x86/kernel/cpu/sgx/encl.c +30 -0
@@ -235,6 +235,35 @@ static void sgx_vma_close(struct vm_area_struct *vma)kref_put (&encl->refcount, sgx_encl_release); } +int sgx_map_allowed (struct sgx_encl *encl,unsigned long start, +unsigned long end,unsigned long prot) +{ +struct sgx_encl_page *page; +unsigned long addr; + + prot &= (VM_READ |VM_WRITE |VM_EXEC ); +if (!prot || !encl) +return 0 ; + +mutex_lock (&encl->lock); + +for (addr = start; addr < end; addr +=PAGE_SIZE ) { + page =radix_tree_lookup (&encl->page_tree, addr >>PAGE_SHIFT ); +if (!page) +return -EACCES ; + } + +mutex_unlock (&encl->lock); + +return 0 ; +} + +static int sgx_vma_mprotect (struct vm_area_struct *vma,unsigned long start, +unsigned long end,unsigned long prot) +{ +return sgx_map_allowed (vma->vm_private_data, start, end, prot); +} +static unsigned int sgx_vma_fault (struct vm_fault *vmf) {unsigned long addr = (unsigned long )vmf->address;
@@ -372,6 +401,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,const struct vm_operations_struct sgx_vm_ops = { .close = sgx_vma_close, .open = sgx_vma_open, + .mprotect = sgx_vma_mprotect, .fault = sgx_vma_fault, .access = sgx_vma_access, };
arch/x86/kernel/cpu/sgx/encl.h +3 -0
@@ -106,6 +106,9 @@ static inline unsigned long sgx_pcmd_offset(pgoff_t page_index)sizeof (struct sgx_pcmd ); } +int sgx_map_allowed (struct sgx_encl *encl,unsigned long start, +unsigned long end,unsigned long prot); +enum sgx_encl_mm_iter {SGX_ENCL_MM_ITER_DONE =0 ,SGX_ENCL_MM_ITER_NEXT =1 ,
Signature · 1 line
2.21.0
RE: [RFC PATCH 5/9] x86/sgx: Restrict mapping without an enclave page to PROT_NONE
Quoted 9 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMTo support LSM integration, SGX will require userspace to explicitly specify the allowed protections for each page. The allowed protections will be supplied to and modified by LSMs (based on their policies).
To prevent userspace from circumventing the allowed protections, do not allow PROT_{READ,WRITE,EXEC} mappings to an enclave without an associated enclave page (which will track the allowed protections).
This is unnecessary.
For mprotect(), LSM shall validate @prot against existing pages with applicable global flags (e.g. FILE__EXECMOD/PROCESS__EXECMEM in the case of SELinux).
For mmap(), SGX driver could invoke security_file_mprotect() explicitly to have LSM validate requested protection.
In the case where there's no page associated with an VMA, security_file_mprotect() shall still dictate whether to allow/deny the request. LSM internally is able to track existence/nonexistence of enclave pages. If there's no page, there's no conflict so the decision shall only depend on global flags (if any). Afterwards, #PF may trigger SGX driver to EAUG, in which case security_enclave_load() will be invoked and LSM could decide whether to approve/decline EAUG request.
Re: [RFC PATCH 5/9] x86/sgx: Restrict mapping without an enclave page to PROT_NONE
In reply to Sean ChristophersonQuoted 3 lines · Sean Christopherson
To support LSM integration, SGX will require userspace to explicitly specify the allowed protections for each page. The allowed protections will be supplied to and modified by LSMs (based on their policies).
How the allowed protections are modified by LSMs? AFAIK they don't touch the PROT_* flags but I could be wrong too.
Quoted 32 lines
To prevent userspace from circumventing the allowed protections, do not allow PROT_{READ,WRITE,EXEC} mappings to an enclave without an associated enclave page (which will track the allowed protections).
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/main.c | 5 +++++ arch/x86/kernel/cpu/sgx/encl.c | 30 +++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/encl.h | 3 +++ 3 files changed, 38 insertions(+)arch/x86/kernel/cpu/sgx/driver/main.c +5
-0@@ -63,6 +63,11 @@ static long sgx_compat_ioctl(struct file *filep, unsigned int cmd,static int sgx_mmap (struct file *file,struct vm_area_struct *vma) {struct sgx_encl *encl = file->private_data; +int ret; + + ret =sgx_map_allowed (encl, vma->vm_start, vma->vm_end, vma->vm_flags); +if (ret) +return ret; vma->vm_ops = &sgx_vm_ops; vma->vm_flags |=VM_PFNMAP |VM_DONTEXPAND |VM_DONTDUMP |VM_IO ;arch/x86/kernel/cpu/sgx/encl.c +0
-0
arch/x86/kernel/cpu/sgx/encl.c static void sgx_vma_close(struct vm_area_struct *vma) · line 239
Quoted 5 lines
arch/x86/kernel/cpu/sgx/encl.c +2
-0@@ -235,6 +235,35 @@ static void sgx_vma_close(struct vm_area_struct *vma)kref_put (&encl->refcount, sgx_encl_release); } +int sgx_map_allowed (struct sgx_encl *encl,unsigned long start, +unsigned long end,unsigned long prot)
Documentation missing.
Quoted 54 lines
arch/x86/kernel/cpu/sgx/encl.c +28
-0+{ +struct sgx_encl_page *page; +unsigned long addr; + + prot &= (VM_READ |VM_WRITE |VM_EXEC ); +if (!prot || !encl) +return 0 ; + +mutex_lock (&encl->lock); + +for (addr = start; addr < end; addr +=PAGE_SIZE ) { + page =radix_tree_lookup (&encl->page_tree, addr >>PAGE_SHIFT ); +if (!page) +return -EACCES ; + } + +mutex_unlock (&encl->lock); + +return 0 ; +} + +static int sgx_vma_mprotect (struct vm_area_struct *vma,unsigned long start, +unsigned long end,unsigned long prot) +{ +return sgx_map_allowed (vma->vm_private_data, start, end, prot); +} +static unsigned int sgx_vma_fault (struct vm_fault *vmf) {unsigned long addr = (unsigned long )vmf->address;@@ -372,6 +401,7 @@ static int sgx_vma_access(struct vm_area_struct *vma, unsigned long addr,const struct vm_operations_struct sgx_vm_ops = { .close = sgx_vma_close, .open = sgx_vma_open, + .mprotect = sgx_vma_mprotect, .fault = sgx_vma_fault, .access = sgx_vma_access, };arch/x86/kernel/cpu/sgx/encl.h +3
-0@@ -106,6 +106,9 @@ static inline unsigned long sgx_pcmd_offset(pgoff_t page_index)sizeof (struct sgx_pcmd ); } +int sgx_map_allowed (struct sgx_encl *encl,unsigned long start, +unsigned long end,unsigned long prot); +enum sgx_encl_mm_iter {SGX_ENCL_MM_ITER_DONE =0 ,SGX_ENCL_MM_ITER_NEXT =1 ,Signature · 1 line
2.21.0
This is missing explanation why it is OK to have a mismatch between the SECINFO flags and VM_* flags. Maybe that could be explained in sgx_map_allowed() documentation.
/Jarkko
RFC PATCH 6/9 x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
In reply to Sean Christopherson...to support (the equivalent) of existing Linux Security Module functionality.
Because SGX manually manages EPC memory, all enclave VMAs are backed by the same vm_file, i.e. /dev/sgx/enclave, so that SGX can implement the necessary hooks to move pages in/out of the EPC. And because EPC pages for any given enclave are fundamentally shared between processes, i.e. CoW semantics are not possible with EPC pages, /dev/sgx/enclave must always be MAP_SHARED. Lastly, all real world enclaves will need read, write and execute permissions to EPC pages. As a result, SGX does not play nice with existing LSM behavior as it is impossible to apply policies to enclaves with any reasonable granularity, e.g. an LSM can deny access to EPC altogether, but can't deny potentially dangerous behavior such as mapping pages RW->RW or RWX.
To give LSMs enough information to implement their policies without having to resort to ugly things, e.g. holding a reference to the vm_file of each enclave page, require userspace to explicitly state the allowed protections for each page (region), i.e. take ALLOW_{READ,WRITE,EXEC} in the ADD_PAGES ioctl.
The ALLOW_* flags will be passed to LSMs so that they can make informed decisions when the enclave is being built, i.e. when the source vm_file is available. For example, SELinux's EXECMOD permission can be required if an enclave is requesting both ALLOW_WRITE and ALLOW_EXEC.
Update the mmap()/mprotect() hooks to enforce the ALLOW_* protections, a la the standard VM_MAY{READ,WRITE,EXEC} flags.
The ALLOW_EXEC flag also has a second (important) use in that it can be used to prevent loading an enclave from a noexec file system, on SGX2 hardware (regardless of kernel support for SGX2), userspace could EADD from a noexec path using read-only permissions and later mprotect() and ENCLU[EMODPE] the page to gain execute permissions. By requiring ALLOW_EXEC up front, SGX will be able to enforce noexec paths when building the enclave.
--- arch/x86/include/uapi/asm/sgx.h | 9 ++++++++- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 23 +++++++++++++++++------ arch/x86/kernel/cpu/sgx/encl.c | 2 +- arch/x86/kernel/cpu/sgx/encl.h | 1 + 4 files changed, 27 insertions(+), 8 deletions(-)
arch/x86/include/uapi/asm/sgx.h +8 -1
@@ -31,6 +31,11 @@ struct sgx_enclave_create {__u64 src; }; +/* Supported flags for struct sgx_enclave_add_pages. */ +#define SGX_ALLOW_READ VM_READ +#define SGX_ALLOW_WRITE VM_WRITE +#define SGX_ALLOW_EXEC VM_EXEC +/** * struct sgx_enclave_add_pages - parameter structure for the * %SGX_IOC_ENCLAVE_ADD_PAGES ioctl
@@ -39,6 +44,7 @@ struct sgx_enclave_create {* @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) +* @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */ struct sgx_enclave_add_pages {__u64 addr;
@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags; +};/** * struct sgx_enclave_init - parameter structure for the
arch/x86/kernel/cpu/sgx/driver/ioctl.c +17 -6
@@ -235,7 +235,8 @@ static int sgx_validate_secs(const struct sgx_secs *secs, }static struct sgx_encl_page *sgx_encl_page_alloc (struct sgx_encl *encl, -unsigned long addr) +unsigned long addr, +unsigned long allowed_prot) {struct sgx_encl_page *encl_page;int ret;
@@ -247,6 +248,7 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,return ERR_PTR (-ENOMEM ); encl_page->desc = addr; encl_page->encl = encl; + encl_page->allowed_prot = allowed_prot; ret =radix_tree_insert (&encl->page_tree,PFN_DOWN (encl_page->desc), encl_page);if (ret) {
@@ -530,7 +532,7 @@ static int sgx_encl_queue_page(struct sgx_encl *encl,static int __sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,void *data,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned long allowed_prot) {u64 page_type = secinfo->flags &SGX_SECINFO_PAGE_TYPE_MASK ;struct sgx_encl_page *encl_page;
@@ -556,7 +558,7 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, goto out; } - encl_page =sgx_encl_page_alloc (encl, addr); + encl_page =sgx_encl_page_alloc (encl, addr, allowed_prot);if (IS_ERR (encl_page)) { ret =PTR_ERR (encl_page); goto out;
@@ -576,12 +578,20 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,static int sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,unsigned long src,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned int flags) { +unsigned long prot = secinfo->flags & (VM_READ |VM_WRITE |VM_EXEC ); +unsigned long allowed_prot = flags & (VM_READ |VM_WRITE |VM_EXEC );struct page *data_page;void *data;int ret; +BUILD_BUG_ON (SGX_SECINFO_R !=VM_READ ||SGX_SECINFO_W !=VM_WRITE || +SGX_SECINFO_X !=VM_EXEC ); + +if (prot & ~allowed_prot) +return -EACCES ; + data_page =alloc_page (GFP_HIGHUSER );if (!data_page)return -ENOMEM ;
@@ -593,7 +603,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, goto out; } - ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask); + ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask, + allowed_prot); out:kunmap (data_page);__free_page (data_page);
@@ -645,7 +656,7 @@ static long sgx_ioc_enclave_add_pages(struct file *filep, unsigned int cmd, ret =sgx_encl_add_page (encl, addp->addr + i*PAGE_SIZE , addp->src + i*PAGE_SIZE , - &secinfo, addp->mrmask); + &secinfo, addp->mrmask, addp->flags); }return ret; }
arch/x86/kernel/cpu/sgx/encl.c +1 -1
@@ -249,7 +249,7 @@ int sgx_map_allowed(struct sgx_encl *encl, unsigned long start,for (addr = start; addr < end; addr +=PAGE_SIZE ) { page =radix_tree_lookup (&encl->page_tree, addr >>PAGE_SHIFT ); -if (!page) +if (!page || (prot & ~page->allowed_prot))return -EACCES ; }
arch/x86/kernel/cpu/sgx/encl.h +1 -0
@@ -41,6 +41,7 @@ enum sgx_encl_page_desc {struct sgx_encl_page {unsigned long desc; +unsigned long allowed_prot;struct sgx_epc_page *epc_page;struct sgx_va_page *va_page;struct sgx_encl *encl;
Signature · 1 line
2.21.0
RE: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
Quoted 29 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PM...to support (the equivalent) of existing Linux Security Module functionality.
Because SGX manually manages EPC memory, all enclave VMAs are backed by the same
vm_file, i.e./dev/sgx/enclave, so that SGX can implement the necessary hooks to move pages in/out of the EPC. And because EPC pages for any given enclave are fundamentally shared between processes, i.e.
CoW semantics are not possible with EPC pages,/dev/sgx/enclavemust always beMAP_SHARED. Lastly, all real world enclaves will need read, write and execute permissions to EPC pages. As a result, SGX does not play nice with existing LSM behavior as it is impossible to apply policies to enclaves with any reasonable granularity, e.g. an LSM can deny access to EPC altogether, but can't deny potentially dangerous behavior such as mapping pagesRW->RWor RWX.To give LSMs enough information to implement their policies without having to resort to ugly things, e.g. holding a reference to the
vm_fileof each enclave page, require userspace to explicitly state the allowed protections for each page (region), i.e. take ALLOW_{READ,WRITE,EXEC} in theADD_PAGESioctl.The ALLOW_* flags will be passed to LSMs so that they can make informed decisions when the enclave is being built, i.e. when the source
vm_fileis available. For example, SELinux's EXECMOD permission can be required if an enclave is requesting bothALLOW_WRITEandALLOW_EXEC.Update the mmap()/mprotect() hooks to enforce the ALLOW_* protections, a la the standard VM_MAY{READ,WRITE,EXEC} flags.
Quoted 6 lines
The
ALLOW_EXECflag also has a second (important) use in that it can be used to prevent loading an enclave from a noexec file system, on
SGX2 hardware (regardless of kernel support for SGX2), userspace could EADD from a noexec path using read-only permissions and latermprotect()and ENCLU[EMODPE] the page to gain execute permissions. By requiringALLOW_EXECup front, SGX will be able to enforce noexec paths when building the enclave.
ALLOW_* flags shall be kept internal to LSM.
This patch is completely unnecessary.
Re: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
In reply to Sean ChristophersonQuoted 2 lines · Sean Christopherson
...to support (the equivalent) of existing Linux Security Module functionality.
Long and short descriptions should be separate. Also this does not make any sense. LSM is a framework with a set of hook to make access decisions and there various implementations of it.
How this replicates LSMs and why that even would be a goal?
My guess is that you are trying to do something else. I'm just saying that the idea to do equivalent of LSMs to another subsystems would be insane if it was done.
Quoted 6 lines
always be
MAP_SHARED. Lastly, all real world enclaves will need read, write and execute permissions to EPC pages. As a result, SGX does not play nice with existing LSM behavior as it is impossible to apply policies to enclaves with any reasonable granularity, e.g. an LSM can deny access to EPC altogether, but can't deny potentially dangerous behavior such as mapping pagesRW->RWor RWX.
The mapping must be shared given that it is iomem but why enclave pages would need RWX for all pages? The information that is missing from this paragraph is the explanation why an LSM could not deny dangerous behavior in PTE level.
Quoted 5 lines
To give LSMs enough information to implement their policies without having to resort to ugly things, e.g. holding a reference to the
vm_fileof each enclave page, require userspace to explicitly state the allowed protections for each page (region), i.e. take ALLOW_{READ,WRITE,EXEC} in theADD_PAGESioctl.
I would keep descriptions such as "ugly things" away from commit messages as it is easy way to be not clear and explicit what you are trying to say.
Quoted 4 lines
The ALLOW_* flags will be passed to LSMs so that they can make informed decisions when the enclave is being built, i.e. when the source
vm_fileis available. For example, SELinux's EXECMOD permission can be required if an enclave is requesting bothALLOW_WRITEandALLOW_EXEC.
There should be some explanation what ALLOW_* flag are. It is now like as it was in common knowledge. SECINFO already has protection flags to name an example and without any explanation all of this is just very confusing.
This should address SECINFO and ALLOW_* relationship and differences.
Quoted 24 lines
Update the mmap()/mprotect() hooks to enforce the ALLOW_* protections, a la the standard VM_MAY{READ,WRITE,EXEC} flags.
The
ALLOW_EXECflag also has a second (important) use in that it can be used to prevent loading an enclave from a noexec file system, on SGX2 hardware (regardless of kernel support for SGX2), userspace could EADD from a noexec path using read-only permissions and latermprotect()and ENCLU[EMODPE] the page to gain execute permissions. By requiringALLOW_EXECup front, SGX will be able to enforce noexec paths when building the enclave.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 9 ++++++++- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 23 +++++++++++++++++------ arch/x86/kernel/cpu/sgx/encl.c | 2 +- arch/x86/kernel/cpu/sgx/encl.h | 1 + 4 files changed, 27 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +0
-0
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_create { · line 37
Quoted 7 lines
arch/x86/include/uapi/asm/sgx.h +4
-0@@ -31,6 +31,11 @@ struct sgx_enclave_create {__u64 src; }; +/* Supported flags for struct sgx_enclave_add_pages. */ +#define SGX_ALLOW_READ VM_READ +#define SGX_ALLOW_WRITE VM_WRITE +#define SGX_ALLOW_EXEC VM_EXEC
Why these flags are even defined if they are the same as VM_* flags?
Quoted 63 lines
arch/x86/include/uapi/asm/sgx.h +4
-1+/** * struct sgx_enclave_add_pages - parameter structure for the * %SGX_IOC_ENCLAVE_ADD_PAGES ioctl @@ -39,6 +44,7 @@ struct sgx_enclave_create {* @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) +* @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */ struct sgx_enclave_add_pages {__u64 addr;@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags; +};/** * struct sgx_enclave_init - parameter structure for the arch/x86/kernel/cpu/sgx/driver/ioctl.c +5
-3@@ -235,7 +235,8 @@ static int sgx_validate_secs(const struct sgx_secs *secs, }static struct sgx_encl_page *sgx_encl_page_alloc (struct sgx_encl *encl, -unsigned long addr) +unsigned long addr, +unsigned long allowed_prot) {struct sgx_encl_page *encl_page;int ret;@@ -247,6 +248,7 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,return ERR_PTR (-ENOMEM ); encl_page->desc = addr; encl_page->encl = encl; + encl_page->allowed_prot = allowed_prot; ret =radix_tree_insert (&encl->page_tree,PFN_DOWN (encl_page->desc), encl_page);if (ret) {@@ -530,7 +532,7 @@ static int sgx_encl_queue_page(struct sgx_encl *encl,static int __sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,void *data,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned long allowed_prot) {u64 page_type = secinfo->flags &SGX_SECINFO_PAGE_TYPE_MASK ;struct sgx_encl_page *encl_page;@@ -556,7 +558,7 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, goto out; } - encl_page =sgx_encl_page_alloc (encl, addr); + encl_page =sgx_encl_page_alloc (encl, addr, allowed_prot);if (IS_ERR (encl_page)) { ret =PTR_ERR (encl_page); goto out;
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 583
Quoted 7 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2
-1@@ -576,12 +578,20 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,static int sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,unsigned long src,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned int flags) { +unsigned long prot = secinfo->flags & (VM_READ |VM_WRITE |VM_EXEC );
Even if the secinfo flags have the exactly the same values you should not do this as they are kind of from different type. This is confusing to read.
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 584
Quoted 1 line
arch/x86/kernel/cpu/sgx/driver/ioctl.c +1
-0+unsigned long allowed_prot = flags & (VM_READ |VM_WRITE |VM_EXEC );
Why you take the trouble defining those macros and do not then use them even yourself?
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 590
Quoted 6 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +2
-0struct page *data_page;void *data;int ret; +BUILD_BUG_ON (SGX_SECINFO_R !=VM_READ ||SGX_SECINFO_W !=VM_WRITE || +SGX_SECINFO_X !=VM_EXEC );
Why this check?
Quoted 31 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +7
-2+ +if (prot & ~allowed_prot) +return -EACCES ; + data_page =alloc_page (GFP_HIGHUSER );if (!data_page)return -ENOMEM ;@@ -593,7 +603,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, goto out; } - ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask); + ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask, + allowed_prot); out:kunmap (data_page);__free_page (data_page);@@ -645,7 +656,7 @@ static long sgx_ioc_enclave_add_pages(struct file *filep, unsigned int cmd, ret =sgx_encl_add_page (encl, addp->addr + i*PAGE_SIZE , addp->src + i*PAGE_SIZE , - &secinfo, addp->mrmask); + &secinfo, addp->mrmask, addp->flags); }return ret; }arch/x86/kernel/cpu/sgx/encl.c +0
-0
arch/x86/kernel/cpu/sgx/encl.c int sgx_map_allowed(struct sgx_encl *encl, unsigned long start, · line 254
Quoted 7 lines
arch/x86/kernel/cpu/sgx/encl.c +1
-1@@ -249,7 +249,7 @@ int sgx_map_allowed(struct sgx_encl *encl, unsigned long start,for (addr = start; addr < end; addr +=PAGE_SIZE ) { page =radix_tree_lookup (&encl->page_tree, addr >>PAGE_SHIFT ); -if (!page) +if (!page || (prot & ~page->allowed_prot))return -EACCES ; }
However this goes it would be good idea to have only ony patch in the patch set that fully defines this function. Impossible to review properly with this split.
Quoted 17 lines
arch/x86/kernel/cpu/sgx/encl.h +1
-0@@ -41,6 +41,7 @@ enum sgx_encl_page_desc {struct sgx_encl_page {unsigned long desc; +unsigned long allowed_prot;struct sgx_epc_page *epc_page;struct sgx_va_page *va_page;struct sgx_encl *encl;Signature · 1 line
2.21.0
This patch left me very confused. I don't get it.
/Jarkko
Re: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
Quoted 8 lines · Jarkko Sakkinen
Quoted 2 lines · Sean Christopherson
...to support (the equivalent) of existing Linux Security Module functionality.
Long and short descriptions should be separate. Also this does not make any sense. LSM is a framework with a set of hook to make access decisions and there various implementations of it.
Quoted 5 lines
How this replicates LSMs and why that even would be a goal?
My guess is that you are trying to do something else. I'm just saying that the idea to do equivalent of LSMs to another subsystems would be insane if it was done.
Heh, yeah, it's not duplicating LSM functionality. What I was trying to say is that this patch allows LSMs to implement policies that are equivalent to their existing functionality, e.g. paves the way to add security_enclave_load() as an equivalent to security_file_mprotect().
Quoted 11 lines
Quoted 6 lines
always be
MAP_SHARED. Lastly, all real world enclaves will need read, write and execute permissions to EPC pages. As a result, SGX does not play nice with existing LSM behavior as it is impossible to apply policies to enclaves with any reasonable granularity, e.g. an LSM can deny access to EPC altogether, but can't deny potentially dangerous behavior such as mapping pagesRW->RWor RWX.The mapping must be shared given that it is iomem but why enclave pages would need RWX for all pages? The information that is missing from this paragraph is the explanation why an LSM could not deny dangerous behavior in PTE level.
I'll add that.
Quoted 15 lines
Quoted 5 lines
To give LSMs enough information to implement their policies without having to resort to ugly things, e.g. holding a reference to the
vm_fileof each enclave page, require userspace to explicitly state the allowed protections for each page (region), i.e. take ALLOW_{READ,WRITE,EXEC} in theADD_PAGESioctl.I would keep descriptions such as "ugly things" away from commit messages as it is easy way to be not clear and explicit what you are trying to say.
Quoted 4 lines
The ALLOW_* flags will be passed to LSMs so that they can make informed decisions when the enclave is being built, i.e. when the source
vm_fileis available. For example, SELinux's EXECMOD permission can be required if an enclave is requesting bothALLOW_WRITEandALLOW_EXEC.
Quoted 4 lines
There should be some explanation what ALLOW_* flag are. It is now like as it was in common knowledge. SECINFO already has protection flags to name an example and without any explanation all of this is just very confusing.
Noted.
Quoted 29 lines
This should address SECINFO and ALLOW_* relationship and differences.
Quoted 27 lines
Update the mmap()/mprotect() hooks to enforce the ALLOW_* protections, a la the standard VM_MAY{READ,WRITE,EXEC} flags.
The
ALLOW_EXECflag also has a second (important) use in that it can be used to prevent loading an enclave from a noexec file system, on SGX2 hardware (regardless of kernel support for SGX2), userspace could EADD from a noexec path using read-only permissions and latermprotect()and ENCLU[EMODPE] the page to gain execute permissions. By requiringALLOW_EXECup front, SGX will be able to enforce noexec paths when building the enclave.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 9 ++++++++- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 23 +++++++++++++++++------ arch/x86/kernel/cpu/sgx/encl.c | 2 +- arch/x86/kernel/cpu/sgx/encl.h | 1 + 4 files changed, 27 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +0
-0@@ -31,6 +31,11 @@ struct sgx_enclave_create {__u64 src; };
Quoted 6 lines
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_create { · line 37
Quoted 4 lines
arch/x86/include/uapi/asm/sgx.h +4
-0+/* Supported flags for struct sgx_enclave_add_pages. */ +#define SGX_ALLOW_READ VM_READ +#define SGX_ALLOW_WRITE VM_WRITE +#define SGX_ALLOW_EXEC VM_EXECWhy these flags are even defined if they are the same as VM_* flags?
Brain fart. Flags can just take PROT_{READ,WRITE,EXEC}.
Quoted 20 lines
Quoted 20 lines
arch/x86/include/uapi/asm/sgx.h +4
-1+/** * struct sgx_enclave_add_pages - parameter structure for the * %SGX_IOC_ENCLAVE_ADD_PAGES ioctl @@ -39,6 +44,7 @@ struct sgx_enclave_create {* @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) +* @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */ struct sgx_enclave_add_pages {__u64 addr;@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags; +};
...
Quoted 12 lines
arch/x86/include/uapi/asm/sgx.h static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 583
Quoted 8 lines
arch/x86/include/uapi/asm/sgx.h +2
-1@@ -576,12 +578,20 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,static int sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,unsigned long src,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned int flags) { +unsigned long prot = secinfo->flags & (VM_READ |VM_WRITE |VM_EXEC );Even if the secinfo flags have the exactly the same values you should not do this as they are kind of from different type. This is confusing to read.
I can add a dummy helper to translate flags and encapsulate the below assert.
Quoted 4 lines
arch/x86/include/uapi/asm/sgx.h static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 584
Quoted 1 line
arch/x86/include/uapi/asm/sgx.h +1
-0+unsigned long allowed_prot = flags & (VM_READ |VM_WRITE |VM_EXEC );Why you take the trouble defining those macros and do not then use them even yourself?
The original thought was to define them for userspace, but that's broken because VM_* aren't defined for userspace.
Quoted 8 lines
arch/x86/include/uapi/asm/sgx.h static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 590
Quoted 6 lines
arch/x86/include/uapi/asm/sgx.h +2
-0struct page *data_page;void *data;int ret; +BUILD_BUG_ON (SGX_SECINFO_R !=VM_READ ||SGX_SECINFO_W !=VM_WRITE || +SGX_SECINFO_X !=VM_EXEC );Why this check?
To assert that the hardware defined SECINFO flags are interchangeable with Linux's software defined flags, i.e. don't need to be translated.
Quoted 33 lines
Quoted 33 lines
arch/x86/include/uapi/asm/sgx.h +7
-2+ +if (prot & ~allowed_prot) +return -EACCES ; + data_page =alloc_page (GFP_HIGHUSER );if (!data_page)return -ENOMEM ;@@ -593,7 +603,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, goto out; } - ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask); + ret =__sgx_encl_add_page (encl, addr, data, secinfo, mrmask, + allowed_prot); out:kunmap (data_page);__free_page (data_page);@@ -645,7 +656,7 @@ static long sgx_ioc_enclave_add_pages(struct file *filep, unsigned int cmd, ret =sgx_encl_add_page (encl, addp->addr + i*PAGE_SIZE , addp->src + i*PAGE_SIZE , - &secinfo, addp->mrmask); + &secinfo, addp->mrmask, addp->flags); }return ret; }arch/x86/kernel/cpu/sgx/encl.c +0
-0@@ -249,7 +249,7 @@ int sgx_map_allowed(struct sgx_encl *encl, unsigned long start,
Quoted 10 lines
arch/x86/kernel/cpu/sgx/encl.c int sgx_map_allowed(struct sgx_encl *encl, unsigned long start, · line 254
Quoted 6 lines
arch/x86/kernel/cpu/sgx/encl.c +1
-1for (addr = start; addr < end; addr +=PAGE_SIZE ) { page =radix_tree_lookup (&encl->page_tree, addr >>PAGE_SHIFT ); -if (!page) +if (!page || (prot & ~page->allowed_prot))return -EACCES ; }However this goes it would be good idea to have only ony patch in the patch set that fully defines this function. Impossible to review properly with this split.
Sorry, I don't understand what you're suggesting.
Quoted 22 lines
Quoted 18 lines
arch/x86/kernel/cpu/sgx/encl.h +1
-0@@ -41,6 +41,7 @@ enum sgx_encl_page_desc {struct sgx_encl_page {unsigned long desc; +unsigned long allowed_prot;struct sgx_epc_page *epc_page;struct sgx_va_page *va_page;struct sgx_encl *encl;Signature · 1 line
2.21.0This patch left me very confused. I don't get it.
/Jarkko
Re: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
Quoted 4 lines · Sean Christopherson
Heh, yeah, it's not duplicating LSM functionality. What I was trying to say is that this patch allows LSMs to implement policies that are equivalent to their existing functionality, e.g. paves the way to add
security_enclave_load()as an equivalent tosecurity_file_mprotect().
I would suggest describing explicitly in the commit message what you want to do, which you said here e.g. "I do this because I want to add LSM hooks". This also relevant information for the LKM discussion.
Lets see how the next version looks like now that you have some feedback.
In the whole scope of the patch set, in order to make it more readable, I'll give following suggestions on how it is organized:
1. Leave out anything that is not strictly necessary (cosmetic fix, batch operation if possible). Better to focus one thing at a time.
2. Try to organize it so that each function is fully defined in the scope of one patch even if it would mean larger patches.
3. Do not add one call site helpers unless there is a good reason to do so. A good reason would be something like needing to extensive work in error rollback, which would make the caller a mess.
/Jarkko
Re: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
In reply to Sean ChristophersonQuoted 71 lines · Sean Christopherson
...to support (the equivalent) of existing Linux Security Module functionality.
Because SGX manually manages EPC memory, all enclave VMAs are backed by the same
vm_file, i.e./dev/sgx/enclave, so that SGX can implement the necessary hooks to move pages in/out of the EPC. And because EPC pages for any given enclave are fundamentally shared between processes, i.e. CoW semantics are not possible with EPC pages,/dev/sgx/enclavemust always beMAP_SHARED. Lastly, all real world enclaves will need read, write and execute permissions to EPC pages. As a result, SGX does not play nice with existing LSM behavior as it is impossible to apply policies to enclaves with any reasonable granularity, e.g. an LSM can deny access to EPC altogether, but can't deny potentially dangerous behavior such as mapping pagesRW->RWor RWX.To give LSMs enough information to implement their policies without having to resort to ugly things, e.g. holding a reference to the
vm_fileof each enclave page, require userspace to explicitly state the allowed protections for each page (region), i.e. take ALLOW_{READ,WRITE,EXEC} in theADD_PAGESioctl.The ALLOW_* flags will be passed to LSMs so that they can make informed decisions when the enclave is being built, i.e. when the source
vm_fileis available. For example, SELinux's EXECMOD permission can be required if an enclave is requesting bothALLOW_WRITEandALLOW_EXEC.Update the mmap()/mprotect() hooks to enforce the ALLOW_* protections, a la the standard VM_MAY{READ,WRITE,EXEC} flags.
The
ALLOW_EXECflag also has a second (important) use in that it can be used to prevent loading an enclave from a noexec file system, on SGX2 hardware (regardless of kernel support for SGX2), userspace could EADD from a noexec path using read-only permissions and latermprotect()and ENCLU[EMODPE] the page to gain execute permissions. By requiringALLOW_EXECup front, SGX will be able to enforce noexec paths when building the enclave.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/uapi/asm/sgx.h | 9 ++++++++- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 23 +++++++++++++++++------ arch/x86/kernel/cpu/sgx/encl.c | 2 +- arch/x86/kernel/cpu/sgx/encl.h | 1 + 4 files changed, 27 insertions(+), 8 deletions(-)arch/x86/include/uapi/asm/sgx.h +6
-0@@ -31,6 +31,11 @@ struct sgx_enclave_create {__u64 src; }; +/* Supported flags for struct sgx_enclave_add_pages. */ +#define SGX_ALLOW_READ VM_READ +#define SGX_ALLOW_WRITE VM_WRITE +#define SGX_ALLOW_EXEC VM_EXEC +/** * struct sgx_enclave_add_pages - parameter structure for the * %SGX_IOC_ENCLAVE_ADD_PAGES ioctl @@ -39,6 +44,7 @@ struct sgx_enclave_create {* @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) +* @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */ struct sgx_enclave_add_pages {__u64 addr;
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_add_pages { · line 55
Quoted 5 lines
arch/x86/include/uapi/asm/sgx.h +1
-1@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags;
Minor nit: I would use at least u32 for flags. It's not like the size saving is important.
Quoted 7 lines
arch/x86/include/uapi/asm/sgx.h +3
-1static int sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,unsigned long src,struct sgx_secinfo *secinfo, -unsigned int mrmask) +unsigned int mrmask,unsigned int flags) { +unsigned long prot = secinfo->flags & (VM_READ |VM_WRITE |VM_EXEC ); +unsigned long allowed_prot = flags & (VM_READ |VM_WRITE |VM_EXEC );
...
arch/x86/include/uapi/asm/sgx.h
Quoted 2 lines
arch/x86/include/uapi/asm/sgx.h +2
-0+if (prot & ~allowed_prot) +return -EACCES ;
This seems like a well-intentioned sanity check, but it doesn't really accomplish anything with SGX2, so I tend to think it would be better to omit it.
RE: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
In reply to Sean ChristophersonQuoted 17 lines
From: Christopherson, Sean J
Sent: Saturday, June 01, 2019 02:32/**
* structsgx_enclave_add_pages- parameter structure for the
* %SGX_IOC_ENCLAVE_ADD_PAGES ioctl(quoted diff) +1
-0@@ -39,6 +44,7 @@ struct sgx_enclave_create { * @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) + * @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */struct
sgx_enclave_add_pages{arch/x86/include/uapi/asm/sgx.h +0
-0__u64 addr;
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_add_pages { · line 56
Quoted 6 lines
arch/x86/include/uapi/asm/sgx.h +2
-1@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags; +};
You are adding a flags member. The secinfo structure has already a flags member in it. Why do you need both - they are both coming from user mode. What kind of scenario would require having different values. Seems confusing.
---------------------------------------------------------------------
Intel Israel (74) Limited
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
Re: [RFC PATCH 6/9] x86/sgx: Require userspace to provide allowed prots to ADD_PAGES
Quoted 19 lines · Serge
Quoted 19 lines
From: Christopherson, Sean J
Sent: Saturday, June 01, 2019 02:32/**
* structsgx_enclave_add_pages- parameter structure for the
* %SGX_IOC_ENCLAVE_ADD_PAGES ioctl(quoted diff) +1
-0@@ -39,6 +44,7 @@ struct sgx_enclave_create { * @secinfo: address for the SECINFO data (common to all pages) * @nr_pages: number of pages (must be virtually contiguous) * @mrmask: bitmask for the measured 256 byte chunks (common to all pages) + * @flags: flags, e.g. SGX_ALLOW_{READ,WRITE,EXEC} (common to all pages) */struct
sgx_enclave_add_pages{arch/x86/include/uapi/asm/sgx.h +0
-0__u64 addr;@@ -46,7 +52,8 @@ struct sgx_enclave_add_pages {__u64 secinfo;__u32 nr_pages;
Quoted 8 lines
arch/x86/include/uapi/asm/sgx.h struct sgx_enclave_add_pages { · line 56
Quoted 4 lines
arch/x86/include/uapi/asm/sgx.h +2
-1__u16 mrmask; -} __attribute__((__packed__)); +__u16 flags; +};You are adding a flags member. The secinfo structure has already a flags member in it. Why do you need both - they are both coming from user mode. What kind of scenario would require having different values. Seems confusing.
The format of SECINFO.FLAGS is hardware defined, e.g. we can't add a flag to tag the page as being a zero page for optimization purposes, at least not without breaking future compatibility or doing tricky overloading.
If you're specifically talking about SECINFO.FLAGS.RWX, due to SGX2 there are scenarios where userspace will initially want the page to be RW, and will later want to convert the page to RX. Making decisions based solely on the initial EPCM permissions would either create a security hole or force SGX to track "dirty" pages along with a implementing a pre-check scheme for LSMs (or restricting LSMs to tieing permissions to the host process and not the enclave).
RFC PATCH 7/9 x86/sgx: Enforce noexec filesystem restriction for enclaves
In reply to Sean ChristophersonDo not allow an enclave page to be mapped with PROT_EXEC if the source page is backed by a file on a noexec file system.
--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +24 -2
@@ -576,6 +576,27 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,return ret; } +static int sgx_encl_page_protect (unsigned long src,unsigned long prot, +unsigned long *allowed_prot) +{ +struct vm_area_struct *vma; + +if (!(*allowed_prot &VM_EXEC )) + goto do_check; + +down_read (¤t->mm->mmap_sem); + vma =find_vma (current->mm, src); +if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) + *allowed_prot &= ~VM_EXEC ; +up_read (¤t->mm->mmap_sem); + +do_check: +if (prot & ~*allowed_prot) +return -EACCES ; + +return 0 ; +} +static int sgx_encl_add_page (struct sgx_encl *encl,unsigned long addr,unsigned long src,struct sgx_secinfo *secinfo,unsigned int mrmask,unsigned int flags)
@@ -589,8 +610,9 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,BUILD_BUG_ON (SGX_SECINFO_R !=VM_READ ||SGX_SECINFO_W !=VM_WRITE ||SGX_SECINFO_X !=VM_EXEC ); -if (prot & ~allowed_prot) -return -EACCES ; + ret =sgx_encl_page_protect (src, prot, &allowed_prot); +if (ret) +return ret; data_page =alloc_page (GFP_HIGHUSER );if (!data_page)
Signature · 1 line
2.21.0
RE: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
Quoted 23 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMDo not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +2
-0@@ -576,6 +576,27 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, return ret; } +static int sgx_encl_page_protect (unsigned long src,unsigned long prot, +unsigned long *allowed_prot)
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long · line 591
Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +10
-0+{ +struct vm_area_struct *vma; + +if (!(*allowed_prot &VM_EXEC )) + goto do_check; + +down_read (¤t->mm->mmap_sem); + vma =find_vma (current->mm, src); +if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) + *allowed_prot &= ~VM_EXEC ;
Testing (vma->vm_flags & VM_MAYEXEC) == 0 should be a better approach.
Moreover, it looks like the check is done per page, so say 100 pages would cause this test to run 100 times even if they are within the same VMA. Wouldn't that be a bit inefficient?
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +8
-0+up_read (¤t->mm->mmap_sem); + +do_check: +if (prot & ~*allowed_prot) +return -EACCES ; + +return 0 ; +}
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
Quoted 22 lines · Cedric
Quoted 22 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMDo not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +0
-0@@ -576,6 +576,27 @@ static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, return ret; }
Quoted 14 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int __sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr, · line 590
Quoted 12 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +12
-0+static int sgx_encl_page_protect (unsigned long src,unsigned long prot, +unsigned long *allowed_prot) +{ +struct vm_area_struct *vma; + +if (!(*allowed_prot &VM_EXEC )) + goto do_check; + +down_read (¤t->mm->mmap_sem); + vma =find_vma (current->mm, src); +if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) + *allowed_prot &= ~VM_EXEC ;Testing (
vma->vm_flags&VM_MAYEXEC) == 0 should be a better approach.
I think I agree, although that would need a comment explaining why it works.
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>
Why don't you just check in sgx_encl_add_page() that whether the path comes from noexec and deny if SECINFO contains X?
/Jarkko
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
Quoted 10 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>Why don't you just check in
sgx_encl_add_page()that whether the path comes from noexec and deny if SECINFO contains X?
SECINFO seems almost entirely useless for this kind of thing because of SGX2. I'm thinking that SECINFO should be completely ignored for anything other than its required architectural purpose.
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
Quoted 9 lines · Andy Lutomirski
Quoted 7 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>
Quoted 7 lines
Quoted 3 lines
Why don't you just check in
sgx_encl_add_page()that whether the path comes from noexec and deny if SECINFO contains X?SECINFO seems almost entirely useless for this kind of thing because of SGX2. I'm thinking that SECINFO should be completely ignored for anything other than its required architectural purpose.
Agreed.
I've already (somewhat unknowingly) reworked the SELinux patch such that it ignores @prot (the SECINFO protections) and only looks at @allowed_prot (the declared protections). If the kernel ignores SECINFO protections entirely then the LSM hook can simply be:
int selinux_enclave_load(struct vm_area_struct *vma, unsigned long prot)
I.e. LSMs can be blissfully unaware that @prot isn't technically what's going into the PTEs *now*.
RE: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
In reply to Andy LutomirskiQuoted 13 lines
From: Andy Lutomirski [mailto:[email protected]]
Sent: Tuesday, June 04, 2019 1:25 PM
To: Jarkko Sakkinen <[email protected]>Quoted 7 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>
Quoted 7 lines
Quoted 3 lines
Why don't you just check in
sgx_encl_add_page()that whether the path comes from noexec and deny if SECINFO contains X?SECINFO seems almost entirely useless for this kind of thing because of SGX2. I'm thinking that SECINFO should be completely ignored for anything other than its required architectural purpose.
For the purpose of allowing/denying EADD/EAUG, SECINFO is useless.
But SECINFO contains also the page type. What's coming as new feature of SGX2 is CONFIGID, which is a 512-bit value inside SECS, provided by untrusted code at ECREATE. Usually CONFIGID is a hash of something that would affect the behavior of the enclave. For example, the "main" enclave could be a JVM with the actual applet being loaded hashed into SECS.CONFIGID. In that case the enclave's measurements (MRENCLAVE) will stay the same for all applets yet individual applet will have distinct CONFIGID and receive distinct keys. When it comes to LSM, a policy may want to whitelist/blacklist applets for a JVM so a hook at ECREATE may be desirable. We could either define a new hook, or overload security_enclave_load() by providing SECINFO as one of its parameters.
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
In reply to Andy LutomirskiQuoted 9 lines · Andy Lutomirski
Quoted 7 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>
Quoted 7 lines
Quoted 3 lines
Why don't you just check in
sgx_encl_add_page()that whether the path comes from noexec and deny if SECINFO contains X?SECINFO seems almost entirely useless for this kind of thing because of SGX2. I'm thinking that SECINFO should be completely ignored for anything other than its required architectural purpose.
Not exactly sure why using it to pass the RWX bits to EADD ioctl would cause anything to SGX2 support.
/Jarkko
Re: [RFC PATCH 7/9] x86/sgx: Enforce noexec filesystem restriction for enclaves
Quoted 14 lines · Jarkko Sakkinen
Quoted 13 lines · Andy Lutomirski
Quoted 10 lines · Jarkko Sakkinen
Quoted 4 lines · Sean Christopherson
Do not allow an enclave page to be mapped with
PROT_EXECif the source page is backed by a file on a noexec file system.Signed-off-by Sean Christopherson <[email protected]>Why don't you just check in
sgx_encl_add_page()that whether the path comes from noexec and deny if SECINFO contains X?
Quoted 6 lines
Quoted 3 lines
SECINFO seems almost entirely useless for this kind of thing because of SGX2. I'm thinking that SECINFO should be completely ignored for anything other than its required architectural purpose.
Not exactly sure why using it to pass the RWX bits to EADD ioctl would cause anything to SGX2 support.
Andy was pointing out that with SGX2 the enclave can do ENCLU[EMODPE] to make the page executable, e.g. add the page with SECINFO.R and then mprotect() the enclave VMA (whose vm_file == /dev/sgx/enclave) PROT_EXEC. We could hard enforce SECINFO, i.e. set the enclave page's protection bits directly from SECINFO, but that would neuter SGX2, e.g. would break converting RW to RX.
RFC PATCH 8/9 LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
In reply to Sean Christophersonenclave_load() is roughly analogous to the existing file_mprotect().
Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave VMAs are backed by a single file, i.e. /dev/sgx/enclave, that must be MAP_SHARED. Furthermore, all enclaves need read, write and execute VMAs. As a result, file_mprotect() does not provide any meaningful security for enclaves since an LSM can only deny/grant access to the EPC as a whole.
security_enclave_load() is called when SGX is first loading an enclave page, i.e. copying a page from normal memory into the EPC. The notable difference from file_mprotect() is the allowed_prot parameter, which is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} flags. The purpose of allowed_prot is to enable checks such as SELinux's FILE__EXECMOD permission without having to track and update VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't overstep its bounds simply by restricting an enclave VMA's protections by vetting what is maximally allowed during build time.
An alternative to the allowed_prot approach would be to use an enclave's SIGSTRUCT (a smallish structure that can uniquely identify an enclave) as a proxy for the enclave. For example, SGX could take and hold a reference to the file containing the SIGSTRUCT (if it's in a file) and call security_enclave_load() during mprotect(). While the SIGSTRUCT approach would provide better precision, the actual value added was deemed to be negligible. On the other hand, pinning a file for the lifetime of the enclave is ugly, and essentially caching LSM policies in each page's allowed_prot avoids having to make an extra LSM upcall during mprotect().
Note, extensive discussion yielded no sane alternative to some form of SGX specific LSM hook[1].
[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com
--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- include/linux/lsm_hooks.h | 16 ++++++++++++++++ include/linux/security.h | 2 ++ security/security.c | 8 ++++++++ 4 files changed, 35 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/driver/ioctl.c +9 -5
@@ -8,6 +8,7 @@#include <linux/highmem.h> #include <linux/ratelimit.h> #include <linux/sched/signal.h> +#include <linux/security.h> #include <linux/shmem_fs.h> #include <linux/slab.h> #include <linux/suspend.h>
@@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, unsigned long *allowed_prot) {struct vm_area_struct *vma; +int ret =0 ; -if (!(*allowed_prot &VM_EXEC )) +if (!(*allowed_prot &VM_EXEC ) && !IS_ENABLED (CONFIG_SECURITY )) goto do_check;down_read (¤t->mm->mmap_sem); vma =find_vma (current->mm, src);if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) *allowed_prot &= ~VM_EXEC ; +#ifdef CONFIG_SECURITY + ret =security_enclave_load (vma, prot, allowed_prot); +#endif up_read (¤t->mm->mmap_sem); do_check: -if (prot & ~*allowed_prot) -return -EACCES ; - -return 0 ; +if (!ret && (prot & ~*allowed_prot)) + ret = -EACCES ; +return ret; } static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
include/linux/lsm_hooks.h +16 -0
@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves. +* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded. +* @prot: the initial protection of the enclave page. +* @allowed_prot: the maximum protections of the enclave page. +* Return 0 if permission is granted. */ union security_list_options {int (*binder_set_context_mgr)(struct task_struct *mgr);
@@ -1807,6 +1815,11 @@ union security_list_options {int (*bpf_prog_alloc_security)(struct bpf_prog_aux *aux);void (*bpf_prog_free_security)(struct bpf_prog_aux *aux);#endif /* CONFIG_BPF_SYSCALL */ + +#ifdef CONFIG_INTEL_SGX +int (*enclave_load)(struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot); +#endif /* CONFIG_INTEL_SGX */ };struct security_hook_heads {
@@ -2046,6 +2059,9 @@ struct security_hook_heads {struct hlist_head bpf_prog_alloc_security;struct hlist_head bpf_prog_free_security;#endif /* CONFIG_BPF_SYSCALL */ +#ifdef CONFIG_INTEL_SGX +struct hlist_head enclave_load; +#endif /* CONFIG_INTEL_SGX */ } __randomize_layout;/*
include/linux/security.h +2 -0
@@ -392,6 +392,8 @@ void security_inode_invalidate_secctx(struct inode *inode);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); +int security_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot);#else /* CONFIG_SECURITY */ static inline int call_lsm_notifier (enum lsm_event event,void *data)
security/security.c +8 -0
@@ -2359,3 +2359,11 @@ void security_bpf_prog_free(struct bpf_prog_aux *aux) call_void_hook(bpf_prog_free_security, aux); }#endif /* CONFIG_BPF_SYSCALL */ + +#ifdef CONFIG_INTEL_SGX +int security_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot) +{ +return call_int_hook (enclave_load,0 , vma, prot, allowed_prot); +} +#endif /* CONFIG_INTEL_SGX */
Signature · 1 line
2.21.0
RE: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
include/linux/security.h void security_inode_invalidate_secctx(struct inode *inode); · line 396
Quoted 13 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMinclude/linux/security.h index renamed from include/linux/security.h +0
-0
659071c2e57c..2f7925eeef7e100644include/linux/security.h +0
-0@@ -392,6 +392,8 @@ void security_inode_invalidate_secctx(struct inode *inode); int
security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen); intsecurity_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen); intsecurity_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen);include/linux/security.h +2
-0+int security_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot);
Per my comments to the cover letter, security_enclave_load() should have a signature similar to the following:
int security_enclave_load(struct file *enclave_fd, unsigned long linear_address, unsigned long nr_pages, int prot, struct vm_area_struct *source_vma);
@enclave_fd identifies the enclave to which new pages are being added.
@linear_address/@nr_pages specifies the linear range of pages being added.
@prot specifies the initial protection of those newly added pages. It is taken from the vma covering the target range.
@source_vma covers the source pages in the case of EADD. An LSM is expected to make sure security_file_mprotect(source_vma, prot, prot) would succeed before checking anything else, unless @source_vma is NULL, indicating pages are being EAUG'ed. In all cases, LSM is expected to "remember" @prot for all those pages to be checked in future security_file_mprotect() invocations.
Quoted 1 line
#else /*
CONFIG_SECURITY*/
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
In reply to Sean ChristophersonQuoted 62 lines · Sean Christopherson
enclave_load()is roughly analogous to the existingfile_mprotect().Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave VMAs are backed by a single file, i.e.
/dev/sgx/enclave, that must beMAP_SHARED. Furthermore, all enclaves need read, write and execute VMAs. As a result,file_mprotect()does not provide any meaningful security for enclaves since an LSM can only deny/grant access to the EPC as a whole.
security_enclave_load()is called when SGX is first loading an enclave page, i.e. copying a page from normal memory into the EPC. The notable difference fromfile_mprotect()is theallowed_protparameter, which is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} flags. The purpose ofallowed_protis to enable checks such as SELinux'sFILE__EXECMODpermission without having to track and update VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't overstep its bounds simply by restricting an enclave VMA's protections by vetting what is maximally allowed during build time.An alternative to the
allowed_protapproach would be to use an enclave's SIGSTRUCT (a smallish structure that can uniquely identify an enclave) as a proxy for the enclave. For example, SGX could take and hold a reference to the file containing the SIGSTRUCT (if it's in a file) and callsecurity_enclave_load()duringmprotect(). While the SIGSTRUCT approach would provide better precision, the actual value added was deemed to be negligible. On the other hand, pinning a file for the lifetime of the enclave is ugly, and essentially caching LSM policies in each page'sallowed_protavoids having to make an extra LSM upcall duringmprotect().Note, extensive discussion yielded no sane alternative to some form of SGX specific LSM hook[1].
[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- include/linux/lsm_hooks.h | 16 ++++++++++++++++ include/linux/security.h | 2 ++ security/security.c | 8 ++++++++ 4 files changed, 35 insertions(+), 5 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +2
-1@@ -8,6 +8,7 @@#include <linux/highmem.h> #include <linux/ratelimit.h> #include <linux/sched/signal.h> +#include <linux/security.h> #include <linux/shmem_fs.h> #include <linux/slab.h> #include <linux/suspend.h> @@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, unsigned long *allowed_prot) {struct vm_area_struct *vma; +int ret =0 ; -if (!(*allowed_prot &VM_EXEC ))
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_page_protect(unsigned long src, unsigned long prot, · line 595
Quoted 10 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +4
-0+if (!(*allowed_prot &VM_EXEC ) && !IS_ENABLED (CONFIG_SECURITY )) goto do_check;down_read (¤t->mm->mmap_sem); vma =find_vma (current->mm, src);if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) *allowed_prot &= ~VM_EXEC ; +#ifdef CONFIG_SECURITY + ret =security_enclave_load (vma, prot, allowed_prot); +#endif
Normally you'd define a static inline stub for the hook in the #else clause for CONFIG_SECURITY in include/linux/security.h and avoid any ifdef here.
What ensures that the mapping referenced by src can't be changed to an entirely different one (with a different vm_file) between the time of check (here) and the time of use?
Quoted 84 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-4up_read (¤t->mm->mmap_sem); do_check: -if (prot & ~*allowed_prot) -return -EACCES ; - -return 0 ; +if (!ret && (prot & ~*allowed_prot)) + ret = -EACCES ; +return ret; } static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,include/linux/lsm_hooks.h +16
-0@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves. +* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded. +* @prot: the initial protection of the enclave page. +* @allowed_prot: the maximum protections of the enclave page. +* Return 0 if permission is granted. */ union security_list_options {int (*binder_set_context_mgr)(struct task_struct *mgr);@@ -1807,6 +1815,11 @@ union security_list_options {int (*bpf_prog_alloc_security)(struct bpf_prog_aux *aux);void (*bpf_prog_free_security)(struct bpf_prog_aux *aux);#endif /* CONFIG_BPF_SYSCALL */ + +#ifdef CONFIG_INTEL_SGX +int (*enclave_load)(struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot); +#endif /* CONFIG_INTEL_SGX */ };struct security_hook_heads {@@ -2046,6 +2059,9 @@ struct security_hook_heads {struct hlist_head bpf_prog_alloc_security;struct hlist_head bpf_prog_free_security;#endif /* CONFIG_BPF_SYSCALL */ +#ifdef CONFIG_INTEL_SGX +struct hlist_head enclave_load; +#endif /* CONFIG_INTEL_SGX */ } __randomize_layout;/* include/linux/security.h +2
-0@@ -392,6 +392,8 @@ void security_inode_invalidate_secctx(struct inode *inode);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); +int security_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot);#else /* CONFIG_SECURITY */ static inline int call_lsm_notifier (enum lsm_event event,void *data)security/security.c +8
-0@@ -2359,3 +2359,11 @@ void security_bpf_prog_free(struct bpf_prog_aux *aux) call_void_hook(bpf_prog_free_security, aux); }#endif /* CONFIG_BPF_SYSCALL */ + +#ifdef CONFIG_INTEL_SGX +int security_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot) +{ +return call_int_hook (enclave_load,0 , vma, prot, allowed_prot); +} +#endif /* CONFIG_INTEL_SGX */
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
Quoted 66 lines · Stephen Smalley
Quoted 65 lines · Sean Christopherson
enclave_load()is roughly analogous to the existingfile_mprotect().Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave VMAs are backed by a single file, i.e.
/dev/sgx/enclave, that must beMAP_SHARED. Furthermore, all enclaves need read, write and execute VMAs. As a result,file_mprotect()does not provide any meaningful security for enclaves since an LSM can only deny/grant access to the EPC as a whole.
security_enclave_load()is called when SGX is first loading an enclave page, i.e. copying a page from normal memory into the EPC. The notable difference fromfile_mprotect()is theallowed_protparameter, which is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} flags. The purpose ofallowed_protis to enable checks such as SELinux'sFILE__EXECMODpermission without having to track and update VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't overstep its bounds simply by restricting an enclave VMA's protections by vetting what is maximally allowed during build time.An alternative to the
allowed_protapproach would be to use an enclave's SIGSTRUCT (a smallish structure that can uniquely identify an enclave) as a proxy for the enclave. For example, SGX could take and hold a reference to the file containing the SIGSTRUCT (if it's in a file) and callsecurity_enclave_load()duringmprotect(). While the SIGSTRUCT approach would provide better precision, the actual value added was deemed to be negligible. On the other hand, pinning a file for the lifetime of the enclave is ugly, and essentially caching LSM policies in each page'sallowed_protavoids having to make an extra LSM upcall duringmprotect().Note, extensive discussion yielded no sane alternative to some form of SGX specific LSM hook[1].
[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- include/linux/lsm_hooks.h | 16 ++++++++++++++++ include/linux/security.h | 2 ++ security/security.c | 8 ++++++++ 4 files changed, 35 insertions(+), 5 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-1@@ -8,6 +8,7 @@#include <linux/highmem.h> #include <linux/ratelimit.h> #include <linux/sched/signal.h> +#include <linux/security.h> #include <linux/shmem_fs.h> #include <linux/slab.h> #include <linux/suspend.h> @@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, unsigned long *allowed_prot) {struct vm_area_struct *vma; +int ret =0 ; -if (!(*allowed_prot &VM_EXEC )) +if (!(*allowed_prot &VM_EXEC ) && !IS_ENABLED (CONFIG_SECURITY )) goto do_check;down_read (¤t->mm->mmap_sem); vma =find_vma (current->mm, src);
Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_page_protect(unsigned long src, unsigned long prot, · line 595
Quoted 5 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-0if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) *allowed_prot &= ~VM_EXEC ; +#ifdef CONFIG_SECURITY + ret =security_enclave_load (vma, prot, allowed_prot); +#endif Normally you'd define a static inline stub for the hook in the #else clause for
CONFIG_SECURITYininclude/linux/security.hand avoid any ifdef here.
Ah, right.
Quoted 3 lines
What ensures that the mapping referenced by src can't be changed to an entirely different one (with a different
vm_file) between the time of check (here) and the time of use?
Nothing. Holding mmap_sem across copy_from_user() would suffice, correct?
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-4up_read (¤t->mm->mmap_sem); do_check: -if (prot & ~*allowed_prot) -return -EACCES ; - -return 0 ; +if (!ret && (prot & ~*allowed_prot)) + ret = -EACCES ; +return ret; } static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
Quoted 76 lines · Sean Christopherson
Quoted 66 lines · Stephen Smalley
Quoted 65 lines · Sean Christopherson
enclave_load()is roughly analogous to the existingfile_mprotect().Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave VMAs are backed by a single file, i.e.
/dev/sgx/enclave, that must beMAP_SHARED. Furthermore, all enclaves need read, write and execute VMAs. As a result,file_mprotect()does not provide any meaningful security for enclaves since an LSM can only deny/grant access to the EPC as a whole.
security_enclave_load()is called when SGX is first loading an enclave page, i.e. copying a page from normal memory into the EPC. The notable difference fromfile_mprotect()is theallowed_protparameter, which is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} flags. The purpose ofallowed_protis to enable checks such as SELinux'sFILE__EXECMODpermission without having to track and update VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't overstep its bounds simply by restricting an enclave VMA's protections by vetting what is maximally allowed during build time.An alternative to the
allowed_protapproach would be to use an enclave's SIGSTRUCT (a smallish structure that can uniquely identify an enclave) as a proxy for the enclave. For example, SGX could take and hold a reference to the file containing the SIGSTRUCT (if it's in a file) and callsecurity_enclave_load()duringmprotect(). While the SIGSTRUCT approach would provide better precision, the actual value added was deemed to be negligible. On the other hand, pinning a file for the lifetime of the enclave is ugly, and essentially caching LSM policies in each page'sallowed_protavoids having to make an extra LSM upcall duringmprotect().Note, extensive discussion yielded no sane alternative to some form of SGX specific LSM hook[1].
[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- include/linux/lsm_hooks.h | 16 ++++++++++++++++ include/linux/security.h | 2 ++ security/security.c | 8 ++++++++ 4 files changed, 35 insertions(+), 5 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-1@@ -8,6 +8,7 @@#include <linux/highmem.h> #include <linux/ratelimit.h> #include <linux/sched/signal.h> +#include <linux/security.h> #include <linux/shmem_fs.h> #include <linux/slab.h> #include <linux/suspend.h> @@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, unsigned long *allowed_prot) {struct vm_area_struct *vma; +int ret =0 ; -if (!(*allowed_prot &VM_EXEC )) +if (!(*allowed_prot &VM_EXEC ) && !IS_ENABLED (CONFIG_SECURITY )) goto do_check;down_read (¤t->mm->mmap_sem); vma =find_vma (current->mm, src);Quoted 8 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c static int sgx_encl_page_protect(unsigned long src, unsigned long prot, · line 595
Quoted 5 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-0if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) *allowed_prot &= ~VM_EXEC ; +#ifdef CONFIG_SECURITY + ret =security_enclave_load (vma, prot, allowed_prot); +#endif Normally you'd define a static inline stub for the hook in the #else clause for
CONFIG_SECURITYininclude/linux/security.hand avoid any ifdef here.
Quoted 7 lines
Ah, right.
Quoted 3 lines
What ensures that the mapping referenced by src can't be changed to an entirely different one (with a different
vm_file) between the time of check (here) and the time of use?Nothing. Holding
mmap_semacrosscopy_from_user()would suffice, correct?
I don't believe you can do that; copy_from_user() could stall indefinitely. Not sure how to do what you want here or if it requires changing the interface.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/sgx/driver/ioctl.c +3
-4up_read (¤t->mm->mmap_sem); do_check: -if (prot & ~*allowed_prot) -return -EACCES ; - -return 0 ; +if (!ret && (prot & ~*allowed_prot)) + ret = -EACCES ; +return ret; } static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
...
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
What ensures that the mapping referenced by src can't be changed to an entirely different one (with a different
vm_file) between the time of check (here) and the time of use?Nothing. Holding
mmap_semacrosscopy_from_user()would suffice, correct?I don't believe you can do that;
copy_from_user()could stall indefinitely. Not sure how to do what you want here or if it requires changing the interface.
Holding mmap_sem for *read* is OK since you can handle page faults underneath it. Holding it for write is not.
But, holding it for read also locks out the writers which might be messing with vm_file or other parts of the VMA.
Holding it for read for a long time is OK. It's obviously not ideal, but it is something we do widely today.
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
In reply to Sean ChristophersonQuoted 91 lines · Sean Christopherson
enclave_load()is roughly analogous to the existingfile_mprotect().Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave VMAs are backed by a single file, i.e.
/dev/sgx/enclave, that must beMAP_SHARED. Furthermore, all enclaves need read, write and execute VMAs. As a result,file_mprotect()does not provide any meaningful security for enclaves since an LSM can only deny/grant access to the EPC as a whole.
security_enclave_load()is called when SGX is first loading an enclave page, i.e. copying a page from normal memory into the EPC. The notable difference fromfile_mprotect()is theallowed_protparameter, which is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} flags. The purpose ofallowed_protis to enable checks such as SELinux'sFILE__EXECMODpermission without having to track and update VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't overstep its bounds simply by restricting an enclave VMA's protections by vetting what is maximally allowed during build time.An alternative to the
allowed_protapproach would be to use an enclave's SIGSTRUCT (a smallish structure that can uniquely identify an enclave) as a proxy for the enclave. For example, SGX could take and hold a reference to the file containing the SIGSTRUCT (if it's in a file) and callsecurity_enclave_load()duringmprotect(). While the SIGSTRUCT approach would provide better precision, the actual value added was deemed to be negligible. On the other hand, pinning a file for the lifetime of the enclave is ugly, and essentially caching LSM policies in each page'sallowed_protavoids having to make an extra LSM upcall duringmprotect().Note, extensive discussion yielded no sane alternative to some form of SGX specific LSM hook[1].
[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- include/linux/lsm_hooks.h | 16 ++++++++++++++++ include/linux/security.h | 2 ++ security/security.c | 8 ++++++++ 4 files changed, 35 insertions(+), 5 deletions(-)arch/x86/kernel/cpu/sgx/driver/ioctl.c +9
-5@@ -8,6 +8,7 @@#include <linux/highmem.h> #include <linux/ratelimit.h> #include <linux/sched/signal.h> +#include <linux/security.h> #include <linux/shmem_fs.h> #include <linux/slab.h> #include <linux/suspend.h> @@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, unsigned long *allowed_prot) {struct vm_area_struct *vma; +int ret =0 ; -if (!(*allowed_prot &VM_EXEC )) +if (!(*allowed_prot &VM_EXEC ) && !IS_ENABLED (CONFIG_SECURITY )) goto do_check;down_read (¤t->mm->mmap_sem); vma =find_vma (current->mm, src);if (!vma || (vma->vm_file &&path_noexec (&vma->vm_file->f_path))) *allowed_prot &= ~VM_EXEC ; +#ifdef CONFIG_SECURITY + ret =security_enclave_load (vma, prot, allowed_prot); +#endif up_read (¤t->mm->mmap_sem); do_check: -if (prot & ~*allowed_prot) -return -EACCES ; - -return 0 ; +if (!ret && (prot & ~*allowed_prot)) + ret = -EACCES ; +return ret; } static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,include/linux/lsm_hooks.h +0
-0
include/linux/lsm_hooks.h line 1454
Quoted 9 lines
include/linux/lsm_hooks.h +6
-0@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves. +* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded. +* @prot: the initial protection of the enclave page.
What do you mean "initial"? The page is always mapped PROT_NONE when this is called, right? I feel like I must be missing something here.
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Sean Christopherson
static int sgx_encl_add_page(struct
sgx_encl*encl, unsigned long addr,include/linux/lsm_hooks.h +1
-0@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves.
Quoted 8 lines
include/linux/lsm_hooks.h line 1454
Quoted 5 lines
include/linux/lsm_hooks.h +5
-0+* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded. +* @prot: the initial protection of the enclave page. What do you mean "initial"? The page is always mapped
PROT_NONEwhen this is called, right? I feel like I must be missing something here.
Initial protection in the EPCM. Yet another reason to ignore SECINFO.
RE: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
Quoted 21 lines
From: Christopherson, Sean J
Sent: Tuesday, June 04, 2019 1:37 PMQuoted 16 lines · Andy Lutomirski
Quoted 6 lines · Sean Christopherson
static int sgx_encl_add_page(struct
sgx_encl*encl, unsigned long
addr, diff --gita/include/linux/lsm_hooks.hb/include/linux/lsm_hooks.hindex47f58cfb6a19..0562775424a0100644include/linux/lsm_hooks.h line 1453
Quoted 8 lines
include/linux/lsm_hooks.h +5
-0@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves. +* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded.
Quoted 6 lines
Quoted 4 lines
include/linux/lsm_hooks.h line 1454
Quoted 1 line
include/linux/lsm_hooks.h +1
-0+* @prot: the initial protection of the enclave page. What do you mean "initial"? The page is always mapped
PROT_NONEwhen this is called, right? I feel like I must be missing something here.Initial protection in the EPCM. Yet another reason to ignore SECINFO.
I know you guys are talking in the background that all pages are mmap()'ed PROT_NONE. But that's an unnecessary limitation. And @prot here should be @target_vma->vm_flags&(VM_READ|VM_WRITE|VM_EXEC).
Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX
Quoted 23 lines · Cedric
Quoted 23 lines
From: Christopherson, Sean J
Sent: Tuesday, June 04, 2019 1:37 PMQuoted 16 lines · Andy Lutomirski
Quoted 6 lines · Sean Christopherson
static int sgx_encl_add_page(struct
sgx_encl*encl, unsigned long
addr, diff --gita/include/linux/lsm_hooks.hb/include/linux/lsm_hooks.hindex47f58cfb6a19..0562775424a0100644include/linux/lsm_hooks.h line 1453
Quoted 8 lines
include/linux/lsm_hooks.h +5
-0@@ -1446,6 +1446,14 @@* @bpf_prog_free_security: * Clean up the security information stored inside bpf prog. * +* Security hooks for Intel SGX enclaves. +* +* @enclave_load: +* On success, returns 0 and optionally adjusts @allowed_prot +* @vma: the source memory region of the enclave page being loaded.
Quoted 2 lines
Quoted 1 line
include/linux/lsm_hooks.h +1
-0+* @prot: the initial protection of the enclave page.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
What do you mean "initial"? The page is always mapped
PROT_NONEwhen this is called, right? I feel like I must be missing something here.Initial protection in the EPCM. Yet another reason to ignore SECINFO.
I know you guys are talking in the background that all pages are mmap()'ed
PROT_NONE. But that's an unnecessary limitation.
Not all pages have to be mmap()'d PROT_NONE, only pages that do not have an associated enclave page.
Quoted 1 line
And @prot here should be @target_vma->vm_flags&(VM_READ|VM_WRITE|VM_EXEC).
I don't follow, there is no target_vma at this point.
RFC PATCH 9/9 security/selinux: Add enclave_load() implementation
In reply to Sean ChristophersonThe goal of selinux_enclave_load() is to provide a facsimile of the existing selinux_file_mprotect() and file_map_prot_check() policies, but tailored to the unique properties of SGX.
For example, an enclave page is technically backed by a MAP_SHARED file, but the "file" is essentially shared memory that is never persisted anywhere and also requires execute permissions (for some pages).
The basic concept is to require appropriate execute permissions on the source of the enclave for pages that are requesting PROT_EXEC, e.g. if an enclave page is being loaded from a regular file, require FILE__EXECUTE and/or FILE__EXECMOND, and if it's coming from an anonymous/private mapping, require PROCESS__EXECMEM since the process is essentially executing from the mapping, albeit in a roundabout way.
Note, FILE__READ and FILE__WRITE are intentionally not required even if the source page is backed by a regular file. Writes to the enclave page are contained to the EPC, i.e. never hit the original file, and read permissions have already been vetted (or the VMA doesn't have PROT_READ, in which case loading the page into the enclave will fail).
--- security/selinux/hooks.c | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
security/selinux/hooks.c +85 -0
@@ -6726,6 +6726,87 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) }#endif +#ifdef CONFIG_INTEL_SGX +int selinux_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot) +{ +const struct cred *cred =current_cred (); +u32 sid =cred_sid (cred); +int rc; + +/* SGX is supported only in 64-bit kernels. */ +WARN_ON_ONCE (!default_noexec); + +/* +* SGX is responsible for checking @prot vs @allowed_prot, and SELinux +* only cares about execute related permissions for enclaves. +*/ +if (!(*allowed_prot &PROT_EXEC )) +return 0 ; + +/* +* Loading an executable enclave page from a VMA that is not executable +* itself requires EXECUTE permissions on the source file, or if there +* is no regular source file, EXECMEM since the page is being loaded +* from a non-executable anonymous mapping. +*/ +if (!(vma->vm_flags &VM_EXEC )) { +if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECUTE ); +else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL ); + +/* +* Reject the load if the enclave *needs* the page to be +* executable, otherwise prevent it from becoming executable. +*/ +if (rc) { +if (prot &PROT_EXEC ) +return rc; + + *allowed_prot &= ~PROT_EXEC ; + } + } + +/* +* An enclave page that may do RW->RX or W+X requires EXECMOD (backed +* by a regular file) or EXECMEM (loaded from an anonymous mapping). +* Note, this hybrid EXECMOD and EXECMEM behavior is intentional and +* reflects the nature of enclaves and the EPC, e.g. EPC is effectively +* a non-persistent shared file, but each enclave is a private domain +* within that shared file, so delegate to the source of the enclave. +*/ +if ((*allowed_prot &PROT_EXEC ) && (*allowed_prot &PROT_WRITE )) { +if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECMOD ); +else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL ); +/* +* Clear ALLOW_EXEC instead of ALLOWED_WRITE if permissions are +* lacking and @prot has neither PROT_WRITE or PROT_EXEC. If +* userspace wanted RX they would have requested RX, and due to +* lack of permissions they can never get RW->RX, i.e. the only +* useful transition is R->RW. +*/ +if (rc) { +if ((prot &PROT_EXEC ) && (prot &PROT_WRITE )) +return rc; + +if (prot &PROT_EXEC ) + *allowed_prot &= ~PROT_WRITE ; +else + *allowed_prot &= ~PROT_EXEC ; + } + } + +return 0 ; +} +#endif + struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { .lbs_cred =sizeof (struct task_security_struct ), .lbs_file =sizeof (struct file_security_struct ),
@@ -6968,6 +7049,10 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),#endif + +#ifdef CONFIG_INTEL_SGX + LSM_HOOK_INIT(enclave_load, selinux_enclave_load), +#endif };static __init int selinux_init (void )
Signature · 1 line
2.21.0
Re: [RFC PATCH 9/9] security/selinux: Add enclave_load() implementation
Quoted 51 lines · Sean Christopherson
The goal of
selinux_enclave_load()is to provide a facsimile of the existingselinux_file_mprotect()andfile_map_prot_check()policies, but tailored to the unique properties of SGX.For example, an enclave page is technically backed by a
MAP_SHAREDfile, but the "file" is essentially shared memory that is never persisted anywhere and also requires execute permissions (for some pages).The basic concept is to require appropriate execute permissions on the source of the enclave for pages that are requesting
PROT_EXEC, e.g. if an enclave page is being loaded from a regular file, requireFILE__EXECUTEand/orFILE__EXECMOND, and if it's coming from an anonymous/private mapping, requirePROCESS__EXECMEMsince the process is essentially executing from the mapping, albeit in a roundabout way.Note,
FILE__READandFILE__WRITEare intentionally not required even if the source page is backed by a regular file. Writes to the enclave page are contained to the EPC, i.e. never hit the original file, and read permissions have already been vetted (or the VMA doesn't havePROT_READ, in which case loading the page into the enclave will fail).Signed-off-by Sean Christopherson <[email protected]>--- security/selinux/hooks.c | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)security/selinux/hooks.c +17
-0@@ -6726,6 +6726,87 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) }#endif +#ifdef CONFIG_INTEL_SGX +int selinux_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot) +{ +const struct cred *cred =current_cred (); +u32 sid =cred_sid (cred); +int rc; + +/* SGX is supported only in 64-bit kernels. */ +WARN_ON_ONCE (!default_noexec); + +/* +* SGX is responsible for checking @prot vs @allowed_prot, and SELinux +* only cares about execute related permissions for enclaves. +*/ +if (!(*allowed_prot &PROT_EXEC )) +return 0 ;
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6755
Quoted 10 lines
security/selinux/hooks.c +10
-0+ +/* +* Loading an executable enclave page from a VMA that is not executable +* itself requires EXECUTE permissions on the source file, or if there +* is no regular source file, EXECMEM since the page is being loaded +* from a non-executable anonymous mapping. +*/ +if (!(vma->vm_flags &VM_EXEC )) { +if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECUTE );
We might need an EXECMOD check here as well if (vma->vm_file && vma->anon_vma). The scenario would be that the host application mapped the file with PROT_WRITE, modified it, but haven't mapped it PROT_EXEC. Now the enclave loader requests PROT_EXEC without PROT_WRITE or allows it. FILE__EXECUTE is insufficient for this case.
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6759
Quoted 4 lines
security/selinux/hooks.c +4
-0+else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL );
These calls will audit FILE__EXECUTE or PROCESS__EXECMEM denials even when userspace never asked for PROT_EXEC. Possibly we should use avc_has_perm_noaudit() and only call avc_audit() if (prot & PROT_EXEC)? And similarly introduce file_has_perm_noaudit() -> inode_has_perm_noaudit() -> avc_has_perm_noaudit() or inline here and switch to avc_has_perm_noaudit() throughout?
Quoted 6 lines
security/selinux/hooks.c +6
-0+ +/* +* Reject the load if the enclave *needs* the page to be +* executable, otherwise prevent it from becoming executable. +*/ +if (rc) {
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6775
Quoted 10 lines
security/selinux/hooks.c +10
-0+if (prot &PROT_EXEC ) +return rc; + + *allowed_prot &= ~PROT_EXEC ; + } + } + +/* +* An enclave page that may do RW->RX or W+X requires EXECMOD (backed +* by a regular file) or EXECMEM (loaded from an anonymous mapping).
At present EXECMEM is also triggered for W+X private file mappings, to allow denying W+X while permitting exceptions for W->X for text relocations.
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6787
Quoted 12 lines
security/selinux/hooks.c +12
-0+* Note, this hybrid EXECMOD and EXECMEM behavior is intentional and +* reflects the nature of enclaves and the EPC, e.g. EPC is effectively +* a non-persistent shared file, but each enclave is a private domain +* within that shared file, so delegate to the source of the enclave. +*/ +if ((*allowed_prot &PROT_EXEC ) && (*allowed_prot &PROT_WRITE )) { +if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECMOD ); +else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL );
Same issue wrt auditing here. Could also potentially skip the EXECMEM check this time if we performed it above (if so, then we must have passed it because *allowed_prot still had PROT_EXEC set).
Quoted 37 lines
security/selinux/hooks.c +26
-0+/* +* Clear ALLOW_EXEC instead of ALLOWED_WRITE if permissions are +* lacking and @prot has neither PROT_WRITE or PROT_EXEC. If +* userspace wanted RX they would have requested RX, and due to +* lack of permissions they can never get RW->RX, i.e. the only +* useful transition is R->RW. +*/ +if (rc) { +if ((prot &PROT_EXEC ) && (prot &PROT_WRITE )) +return rc; + +if (prot &PROT_EXEC ) + *allowed_prot &= ~PROT_WRITE ; +else + *allowed_prot &= ~PROT_EXEC ; + } + } + +return 0 ; +} +#endif + struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { .lbs_cred =sizeof (struct task_security_struct ), .lbs_file =sizeof (struct file_security_struct ),@@ -6968,6 +7049,10 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),#endif + +#ifdef CONFIG_INTEL_SGX + LSM_HOOK_INIT(enclave_load, selinux_enclave_load), +#endif };static __init int selinux_init (void )
Re: [RFC PATCH 9/9] security/selinux: Add enclave_load() implementation
Quoted 59 lines · Stephen Smalley
Quoted 58 lines · Sean Christopherson
The goal of
selinux_enclave_load()is to provide a facsimile of the existingselinux_file_mprotect()andfile_map_prot_check()policies, but tailored to the unique properties of SGX.For example, an enclave page is technically backed by a
MAP_SHAREDfile, but the "file" is essentially shared memory that is never persisted anywhere and also requires execute permissions (for some pages).The basic concept is to require appropriate execute permissions on the source of the enclave for pages that are requesting
PROT_EXEC, e.g. if an enclave page is being loaded from a regular file, requireFILE__EXECUTEand/orFILE__EXECMOND, and if it's coming from an anonymous/private mapping, requirePROCESS__EXECMEMsince the process is essentially executing from the mapping, albeit in a roundabout way.Note,
FILE__READandFILE__WRITEare intentionally not required even if the source page is backed by a regular file. Writes to the enclave page are contained to the EPC, i.e. never hit the original file, and read permissions have already been vetted (or the VMA doesn't havePROT_READ, in which case loading the page into the enclave will fail).Signed-off-by Sean Christopherson <[email protected]>--- security/selinux/hooks.c | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)security/selinux/hooks.c +25
-0@@ -6726,6 +6726,87 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) }#endif +#ifdef CONFIG_INTEL_SGX +int selinux_enclave_load (struct vm_area_struct *vma,unsigned long prot, +unsigned long *allowed_prot) +{ +const struct cred *cred =current_cred (); +u32 sid =cred_sid (cred); +int rc; + +/* SGX is supported only in 64-bit kernels. */ +WARN_ON_ONCE (!default_noexec); + +/* +* SGX is responsible for checking @prot vs @allowed_prot, and SELinux +* only cares about execute related permissions for enclaves. +*/ +if (!(*allowed_prot &PROT_EXEC )) +return 0 ; + +/* +* Loading an executable enclave page from a VMA that is not executable +* itself requires EXECUTE permissions on the source file, or if there +* is no regular source file, EXECMEM since the page is being loaded +* from a non-executable anonymous mapping. +*/ +if (!(vma->vm_flags &VM_EXEC )) {
Quoted 8 lines
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6755
Quoted 2 lines
security/selinux/hooks.c +2
-0+if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECUTE );We might need an EXECMOD check here as well if (
vma->vm_file&&vma->anon_vma). The scenario would be that the host application mapped the file withPROT_WRITE, modified it, but haven't mapped itPROT_EXEC. Now the enclave loader requestsPROT_EXECwithoutPROT_WRITEor allows it.FILE__EXECUTEis insufficient for this case.
Ya, agreed.
Quoted 11 lines
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6759
Quoted 4 lines
security/selinux/hooks.c +4
-0+else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL );These calls will audit
FILE__EXECUTEorPROCESS__EXECMEMdenials even when userspace never asked forPROT_EXEC. Possibly we should useavc_has_perm_noaudit()and only callavc_audit()if (prot &PROT_EXEC)? And similarly introducefile_has_perm_noaudit()->inode_has_perm_noaudit()->avc_has_perm_noaudit()or inline here and switch toavc_has_perm_noaudit()throughout?
Hmm, I think we want to audit the denials even if userspace technically hasn't requested PROT_EXEC, yet... The idea of @allowed_prot is for userspace to declare its intent, i.e. for all intents and purposes, userspace is asking for PROT_EXEC by declaring PROT_EXEC in @allowed_prot.
For SGX1 enclaves, where permissions are fixed at enclave build time, declaring PROT_EXEC but not actually mapping the page as PROT_EXEC is a userspace bug, and a fairly egregious one at that.
For SGX2 enclaves, where enclaves can convert RW->RX (among others), not auditing would make it difficult for userspace to debug failures due to the denial. E.g. a properly written application that declared PROT_EXEC on a RW page with the intent of converting it to RX would eventually fail at mprotect() due to PROT_EXEC being cleared. Without the audit, there wouldn't be any indication that mprotect() failed with -EACCES due to LSM restrictions.
Quoted 11 lines
Quoted 11 lines
security/selinux/hooks.c +11
-0+ +/* +* Reject the load if the enclave *needs* the page to be +* executable, otherwise prevent it from becoming executable. +*/ +if (rc) { +if (prot &PROT_EXEC ) +return rc; + + *allowed_prot &= ~PROT_EXEC ; + }
Quoted 8 lines
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6775
Quoted 5 lines
security/selinux/hooks.c +5
-0+ } + +/* +* An enclave page that may do RW->RX or W+X requires EXECMOD (backed +* by a regular file) or EXECMEM (loaded from an anonymous mapping). At present EXECMEM is also triggered for W+X private file mappings, to allow denying W+X while permitting exceptions for
W->Xfor text relocations.
Doh, missed that one. Thanks!
Quoted 8 lines
Quoted 8 lines
security/selinux/hooks.c +8
-0+* Note, this hybrid EXECMOD and EXECMEM behavior is intentional and +* reflects the nature of enclaves and the EPC, e.g. EPC is effectively +* a non-persistent shared file, but each enclave is a private domain +* within that shared file, so delegate to the source of the enclave. +*/ +if ((*allowed_prot &PROT_EXEC ) && (*allowed_prot &PROT_WRITE )) { +if (vma->vm_file && !IS_PRIVATE (file_inode (vma->vm_file))) + rc =file_has_perm (cred, vma->vm_file,FILE__EXECMOD );
Quoted 8 lines
security/selinux/hooks.c static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · line 6759
Quoted 4 lines
security/selinux/hooks.c +4
-0+else + rc =avc_has_perm (&selinux_state, + sid, sid,SECCLASS_PROCESS , +PROCESS__EXECMEM ,NULL );Same issue wrt auditing here. Could also potentially skip the EXECMEM check this time if we performed it above (if so, then we must have passed it because
*allowed_protstill hadPROT_EXECset).
Skipping the second EXECMEM check crossed my mind as well. I'll play with the code to see if I can come up with clean way of avoiding multiple EXECMEM (or EXECMOD) checks.
Quoted 37 lines
Quoted 36 lines
security/selinux/hooks.c +26
-0+/* +* Clear ALLOW_EXEC instead of ALLOWED_WRITE if permissions are +* lacking and @prot has neither PROT_WRITE or PROT_EXEC. If +* userspace wanted RX they would have requested RX, and due to +* lack of permissions they can never get RW->RX, i.e. the only +* useful transition is R->RW. +*/ +if (rc) { +if ((prot &PROT_EXEC ) && (prot &PROT_WRITE )) +return rc; + +if (prot &PROT_EXEC ) + *allowed_prot &= ~PROT_WRITE ; +else + *allowed_prot &= ~PROT_EXEC ; + } + } + +return 0 ; +} +#endif + struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { .lbs_cred =sizeof (struct task_security_struct ), .lbs_file =sizeof (struct file_security_struct ),@@ -6968,6 +7049,10 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),#endif + +#ifdef CONFIG_INTEL_SGX + LSM_HOOK_INIT(enclave_load, selinux_enclave_load), +#endif };static __init int selinux_init (void )
Hi Sean,
Quoted 21 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMThis series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at times, but I think/hope we ended up with something reasonable.
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear
ALLOW_EXECto prevent ever mapping the page withPROT_EXEC. SGX enforces the allowed perms via a newmprotect()vm_opshook, e.g. like regularmprotect()uses MAY_{READ,WRITE,EXEC}.
ALLOW_EXECis used to deny hings like loading an enclave from a noexec file system or from a file without EXECUTE permissions, e.g. without theALLOW_EXECconcept, on SGX2 hardware (regardless of kernel support) userspace could EADD from a noexec file using read-only permissions, and later usemprotect()and ENCLU[EMODPE] to gain execute permissions.
ALLOW_WRITEis used in conjuction withALLOW_EXECto enforce SELinux's EXECMOD (or EXECMEM).
Quoted 7 lines
This is very much an RFC series. It's only compile tested, likely has obvious bugs, the SELinux patch could be completely harebrained, etc...
My goal at this point is to get feedback at a macro level, e.g. is the core concept viable/acceptable, are there objection to hookingmprotect(), etc...Andy and Cedric, hopefully this aligns with your general expectations based on our last discussion.
I couldn't understand the real intentions of ALLOW_* flags until I saw them in code. I have to say C is more expressive than English in that regard :)
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at runtime.
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted PROT_WRITE), which will be used to decide whether to grant PROT_EXEC when requested in future. Given the fact that all mprotect() goes through LSM and mmap() is limited to PROT_NONE, it's easy for LSM to capture that flag by itself instead of asking user mode code to provide it.
That said, here is the summary of what I think is a better approach.
* In hook security_file_alloc(), if @file is an enclave, allocate some data structure to store for every page, the WRITTEN flag as described above. WRITTEN is cleared initially for all pages.
Open: Given a file of type struct file *, how to tell if it is an enclave (i.e. /dev/sgx/enclave)?
* In hook security_mmap_file(), if @file is an enclave, make sure @prot can only be PROT_NONE. This is to force all protection changes to go through security_file_mprotect().
* In the newly introduced hook security_enclave_load(), set WRITTEN for pages that are requested PROT_WRITE.
* In hook security_file_mprotect(), if @vma->vm_file is an enclave, look up and use WRITTEN flags for all pages within @vma, along with other global flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux) to decide on allowing/rejecting @prot.
* In hook security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.
I'll try to make more detailed comments in my replies to individual patches sometime tomorrow.
Quoted 30 lines
Lastly, I added a patch to allow userspace to add multiple pages in a single
ioctl(). It's obviously not directly related to the security stuff, but the idea tangentially came up during earlier discussions and it's something I think the UAPI should provide (it's a tiny change). Since I was modifying the UAPI anyways, I threw it in.Sean Christopherson (9):
x86/sgx: Remove unused local variable insgx_encl_release()x86/sgx: Do not naturally alignMAP_FIXEDaddress x86/sgx: Allow userspace to add multiple pages in singleioctl()
mm: Introducevm_ops->mprotect()
x86/sgx: Restrict mapping without an enclave page toPROT_NONEx86/sgx: Require userspace to provide allowed prots toADD_PAGESx86/sgx: Enforce noexec filesystem restriction for enclaves
LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGXsecurity/selinux: Addenclave_load()implementationarch/x86/include/uapi/asm/sgx.h | 30 ++++-- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 143 +++++++++++++++++-------- arch/x86/kernel/cpu/sgx/driver/main.c | 13 ++- arch/x86/kernel/cpu/sgx/encl.c | 31 +++++- arch/x86/kernel/cpu/sgx/encl.h | 4 + include/linux/lsm_hooks.h | 16 +++ include/linux/mm.h | 2 + include/linux/security.h | 2 + mm/mprotect.c | 15 ++- security/security.c | 8 ++ security/selinux/hooks.c | 85 +++++++++++++++ 11 files changed, 290 insertions(+), 59 deletions(-)
Quoted 2 lines
--
2.21.0
-Cedric
Quoted 34 lines · Cedric
Hi Sean,
Quoted 21 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMThis series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at times, but I think/hope we ended up with something reasonable.
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear
ALLOW_EXECto prevent ever mapping the page withPROT_EXEC. SGX enforces the allowed perms via a newmprotect()vm_opshook, e.g. like regularmprotect()uses MAY_{READ,WRITE,EXEC}.
ALLOW_EXECis used to deny hings like loading an enclave from a noexec file system or from a file without EXECUTE permissions, e.g. without theALLOW_EXECconcept, on SGX2 hardware (regardless of kernel support) userspace could EADD from a noexec file using read-only permissions, and later usemprotect()and ENCLU[EMODPE] to gain execute permissions.
ALLOW_WRITEis used in conjuction withALLOW_EXECto enforce SELinux's EXECMOD (or EXECMEM).Quoted 7 lines
This is very much an RFC series. It's only compile tested, likely has obvious bugs, the SELinux patch could be completely harebrained, etc...
My goal at this point is to get feedback at a macro level, e.g. is the core concept viable/acceptable, are there objection to hookingmprotect(), etc...Andy and Cedric, hopefully this aligns with your general expectations based on our last discussion.
I couldn't understand the real intentions of ALLOW_* flags until I saw them in code. I have to say C is more expressive than English in that regard :)
Quoted 6 lines
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at runtime.
In this case, the host process should tag *all* pages it *might* convert to executable as ALLOW_EXEC. LSMs can (and should/will) be written in such a way that denying ALLOW_EXEC is fatal to the enclave if and only if the enclave actually attempts mprotect(PROT_EXEC).
Take the SELinux path for example. The only scenario in which PROT_WRITE is cleared from @allowed_prot is if the page *starts* with PROT_EXEC. If PROT_EXEC is denied on a page that starts RW, e.g. an EAUG'd page, then PROT_EXEC will be cleared from @allowed_prot.
As Stephen pointed out, auditing the denials on @allowed_prot means the log will contain false positives of a sort. But this is more of a noise issue than true false positives. E.g. there are three possible outcomes for the enclave.
- The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. Requesting ALLOW_EXEC is either a straightforward a userspace bug or a poorly written generic enclave loader.
- The enclave conditionally performs EMODPE[PROT_EXEC]. In this case the denial is a true false positive.
- The enclave does EMODPE[PROT_EXEC] and its host userspace then fails on mprotect(PROT_EXEC), i.e. the LSM denial is working as intended. The audit log will be noisy, but viewed as a whole the denials aren't false positives.
The potential for noisy audit logs and/or false positives is unfortunate, but it's (by far) the lesser of many evils.
Quoted 11 lines
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead of asking user mode code to provide it.That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate some data structure to store for every page, the WRITTEN flag as described above. WRITTEN is cleared initially for all pages.
This would effectively require *every* LSM to duplicate the SGX driver's functionality, e.g. track per-page metadata, implement locking to prevent races between multiple mm structs, etc...
Quoted 6 lines
Open: Given a file of type struct file *, how to tell if it is an enclave (i.e.
/dev/sgx/enclave)?
* In hooksecurity_mmap_file(), if @file is an enclave, make sure @prot can
only bePROT_NONE. This is to force all protection changes to go throughsecurity_file_mprotect().
* In the newly introduced hooksecurity_enclave_load(), set WRITTEN for pages
that are requestedPROT_WRITE.
How would an LSM associate a page with a specific enclave? vma->vm_file will point always point at /dev/sgx/enclave. vma->vm_mm is useless because we're allowing multiple processes to map a single enclave, not to mention that by mm would require holding a reference to the mm.
Quoted 4 lines
* In hook
security_file_mprotect(), if @vma->vm_file is an enclave, look up and use WRITTEN flags for all pages within @vma, along with other global flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux) to decide on allowing/rejecting @prot.
vma->vm_file will always be /dev/sgx/enclave at this point, which means LSMs don't have the necessary anchor back to the source file, e.g. to enforce FILE__EXECUTE. The noexec file system case is also unaddressed.
Quoted 2 lines
* In hook
security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.
Quoted 12 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 10:16 AMQuoted 8 lines · Cedric
Hi Sean,
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at
Quoted 6 lines
runtime.
In this case, the host process should tag *all* pages it *might* convert to executable as
ALLOW_EXEC. LSMs can (and should/will) be written in such a way that denyingALLOW_EXECis fatal to the enclave if and only if the enclave actually attemptsmprotect(PROT_EXEC).
What if those pages contain self-modifying code but the host doesn't know ahead of time? Would it require ALLOW_WRITE|ALLOW_EXEC at EADD? Then would it prevent those pages to start with PROT_EXEC?
Anyway, my point is that it is unnecessary even if it works.
Quoted 16 lines
Take the SELinux path for example. The only scenario in which
PROT_WRITEis cleared from @allowed_prot is if the page *starts* withPROT_EXEC.
IfPROT_EXECis denied on a page that starts RW, e.g. an EAUG'd page, thenPROT_EXECwill be cleared from @allowed_prot.As Stephen pointed out, auditing the denials on @allowed_prot means the log will contain false positives of a sort. But this is more of a noise issue than true false positives. E.g. there are three possible outcomes for the enclave.
- The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. Requesting
ALLOW_EXECis either a straightforward a userspace bug or a poorly written generic enclave loader.
Quoted 8 lines
- The enclave conditionally performs EMODPE[PROT_EXEC]. In this case the denial is a true false positive.
- The enclave does EMODPE[PROT_EXEC] and its host userspace then fails on
mprotect(PROT_EXEC), i.e. the LSM denial is working as intended. The audit log will be noisy, but viewed as a whole the denials
aren't
false positives.
What I was talking about was EMODPE[PROT_WRITE] on an RX page.
Quoted 14 lines
The potential for noisy audit logs and/or false positives is unfortunate, but it's (by far) the lesser of many evils.
Quoted 6 lines
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead ofasking user mode code to provide it.
Quoted 3 lines
That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate some
Quoted 8 lines
data
Quoted 1 line
structure to store for every page, the WRITTEN flag as described
above.
Quoted 1 line
WRITTEN is cleared initially for all pages.
This would effectively require *every* LSM to duplicate the SGX driver's functionality, e.g. track per-page metadata, implement locking to prevent races between multiple mm structs, etc...
Architecturally we shouldn't dictate how LSM makes decisions. ALLOW_* are no difference than PROCESS__* or FILE__* flags, which are just artifacts to assist particular LSMs in decision making. They are never considered part of the LSM interface, even if other LSMs than SELinux may adopt the same/similar approach.
If code duplication is what you are worrying about, you can put them in a library, or implement/export them in some new file (maybe security/enclave.c?) as utility functions. But spilling them into user mode is what I think is unacceptable.
Quoted 9 lines
Quoted 2 lines
Open: Given a file of type struct file *, how to tell if it is an
enclave (i.e.
/dev/sgx/enclave)?Quoted 1 line
* In hook
security_mmap_file(), if @file is an enclave, make sure@prot can
Quoted 1 line
only be
PROT_NONE. This is to force all protection changes to gothrough
Quoted 2 lines
security_file_mprotect().
* In the newly introduced hooksecurity_enclave_load(), set WRITTEN
Quoted 7 lines
for pages
Quoted 1 line
that are requested
PROT_WRITE.How would an LSM associate a page with a specific enclave?
vma->vm_filewill point always point at/dev/sgx/enclave.vma->vm_mmis useless because we're allowing multiple processes to map a single enclave, not to mention that by mm would require holding a reference to the mm.
Each open("/dev/sgx/enclave") syscall creates a *new* instance of struct file to uniquely identify one enclave instance. What I mean is @vma->vm_file, not @vma->vm_file->f_path or @vma->vm_file->f_inode.
Quoted 4 lines
Quoted 2 lines
* In hook
security_file_mprotect(), if @vma->vm_file is an enclave,look up
Quoted 1 line
and use WRITTEN flags for all pages within @vma, along with other
Quoted 8 lines
global
Quoted 1 line
flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux)
to decide
Quoted 1 line
on allowing/rejecting @prot.
vma->vm_filewill always be/dev/sgx/enclaveat this point, which means LSMs don't have the necessary anchor back to the source file, e.g. to enforceFILE__EXECUTE. The noexec file system case is also unaddressed.
vma->vm_file identifies an enclave instance uniquely. FILE__EXECUTE is checked by security_enclave_load() using @source_vma->vm_file. Once a page has been EADD'ed, whether to allow RW->RX depends on .sigstruct file (more precisely, the file backing SIGSTRUCT), whose FILE__* attributes could be cached in vma->vm_file->f_security by security_enclave_init().
The noexec case should be addressed in IOC_ADD_PAGES by testing @source_vma->vm_flags & VM_MAYEXEC.
Quoted 3 lines
Quoted 3 lines
* In hook
security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.
Quoted 14 lines · Cedric
Quoted 14 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 10:16 AMQuoted 8 lines · Cedric
Hi Sean,
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at
runtime.
Quoted 8 lines
Quoted 4 lines
In this case, the host process should tag *all* pages it *might* convert to executable as
ALLOW_EXEC. LSMs can (and should/will) be written in such a way that denyingALLOW_EXECis fatal to the enclave if and only if the enclave actually attemptsmprotect(PROT_EXEC).What if those pages contain self-modifying code but the host doesn't know ahead of time? Would it require ALLOW_WRITE|ALLOW_EXEC at EADD? Then would it prevent those pages to start with
PROT_EXEC?
Without ALLOW_WRITE+ALLOW_EXEC, the enclave would build and launch, but fail at mprotect(..., PROT_WRITE), e.g. when it attempted to gain write access to do self-modifying code. And it would would fail irrespective of LSM restrictions.
Quoted 1 line
Anyway, my point is that it is unnecessary even if it works.
Unnecessary in an ideal world, yes. Realistically, it's the least bad option.
Quoted 18 lines
Quoted 18 lines
Take the SELinux path for example. The only scenario in which
PROT_WRITEis cleared from @allowed_prot is if the page *starts* withPROT_EXEC.
IfPROT_EXECis denied on a page that starts RW, e.g. an EAUG'd page, thenPROT_EXECwill be cleared from @allowed_prot.As Stephen pointed out, auditing the denials on @allowed_prot means the log will contain false positives of a sort. But this is more of a noise issue than true false positives. E.g. there are three possible outcomes for the enclave.
- The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. Requesting
ALLOW_EXECis either a straightforward a userspace bug or a poorly written generic enclave loader.- The enclave conditionally performs EMODPE[PROT_EXEC]. In this case the denial is a true false positive.
Quoted 7 lines
Quoted 5 lines
- The enclave does EMODPE[PROT_EXEC] and its host userspace then fails on
mprotect(PROT_EXEC), i.e. the LSM denial is working as intended. The audit log will be noisy, but viewed as a whole the denials
aren't
false positives.What I was talking about was EMODPE[PROT_WRITE] on an RX page.
As above, mprotect(..., PROT_WRITE) would fail without ALLOW_WRITE.
Quoted 22 lines
Quoted 13 lines
The potential for noisy audit logs and/or false positives is unfortunate, but it's (by far) the lesser of many evils.
Quoted 6 lines
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead ofasking user mode code to provide it.
Quoted 3 lines
That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate someQuoted 8 lines
data
Quoted 1 line
structure to store for every page, the WRITTEN flag as described
above.
Quoted 1 line
WRITTEN is cleared initially for all pages.
This would effectively require *every* LSM to duplicate the SGX driver's functionality, e.g. track per-page metadata, implement locking to prevent races between multiple mm structs, etc...
Quoted 5 lines
Architecturally we shouldn't dictate how LSM makes decisions. ALLOW_* are no difference than PROCESS__* or FILE__* flags, which are just artifacts to assist particular LSMs in decision making. They are never considered part of the LSM interface, even if other LSMs than SELinux may adopt the same/similar approach.
No, the flags are tracked and managed by SGX. We are not dictating LSM behavior in any way, e.g. an LSM could completely ignore @allowed_prot and nothing would break.
Quoted 3 lines
If code duplication is what you are worrying about, you can put them in a library, or implement/export them in some new file (maybe
security/enclave.c?) as utility functions.
Code duplication is the least of my concerns. Tracking file pointers would require a global list/tree of some form, along with a locking and/or RCU scheme to protect accesses to that container. Another lock would be needed to prevent races between mprotect() calls from different processes.
Quoted 1 line
But spilling them into user mode is what I think is unacceptable.
Why is it unacceptable? There's effectively no cost to userspace for SGX1. The ALLOW_* flags only come into play in the event of a noexec or LSM restriction, i.e. worst case scenario an enclave that wants to do arbitrary self-modifying code can declare RWX on everything.
Quoted 102 lines · Cedric
Quoted 12 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 10:16 AMQuoted 8 lines · Cedric
Hi Sean,
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at
Quoted 6 lines
runtime.
In this case, the host process should tag *all* pages it *might* convert to executable as
ALLOW_EXEC. LSMs can (and should/will) be written in such a way that denyingALLOW_EXECis fatal to the enclave if and only if the enclave actually attemptsmprotect(PROT_EXEC).What if those pages contain self-modifying code but the host doesn't know ahead of time? Would it require ALLOW_WRITE|ALLOW_EXEC at EADD? Then would it prevent those pages to start with
PROT_EXEC?Anyway, my point is that it is unnecessary even if it works.
Quoted 16 lines
Take the SELinux path for example. The only scenario in which
PROT_WRITEis cleared from @allowed_prot is if the page *starts* withPROT_EXEC.
IfPROT_EXECis denied on a page that starts RW, e.g. an EAUG'd page, thenPROT_EXECwill be cleared from @allowed_prot.As Stephen pointed out, auditing the denials on @allowed_prot means the log will contain false positives of a sort. But this is more of a noise issue than true false positives. E.g. there are three possible outcomes for the enclave.
- The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. Requesting
ALLOW_EXECis either a straightforward a userspace bug or a poorly written generic enclave loader.Quoted 8 lines
- The enclave conditionally performs EMODPE[PROT_EXEC]. In this case the denial is a true false positive.
- The enclave does EMODPE[PROT_EXEC] and its host userspace then fails on
mprotect(PROT_EXEC), i.e. the LSM denial is working as intended. The audit log will be noisy, but viewed as a whole the denials
aren't
false positives.What I was talking about was EMODPE[PROT_WRITE] on an RX page.
Quoted 14 lines
The potential for noisy audit logs and/or false positives is unfortunate, but it's (by far) the lesser of many evils.
Quoted 6 lines
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead ofasking user mode code to provide it.
Quoted 3 lines
That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate someQuoted 8 lines
data
Quoted 1 line
structure to store for every page, the WRITTEN flag as described
above.
Quoted 1 line
WRITTEN is cleared initially for all pages.
This would effectively require *every* LSM to duplicate the SGX driver's functionality, e.g. track per-page metadata, implement locking to prevent races between multiple mm structs, etc...
Architecturally we shouldn't dictate how LSM makes decisions. ALLOW_* are no difference than PROCESS__* or FILE__* flags, which are just artifacts to assist particular LSMs in decision making. They are never considered part of the LSM interface, even if other LSMs than SELinux may adopt the same/similar approach.
If code duplication is what you are worrying about, you can put them in a library, or implement/export them in some new file (maybe
security/enclave.c?) as utility functions. But spilling them into user mode is what I think is unacceptable.Quoted 9 lines
Quoted 2 lines
Open: Given a file of type struct file *, how to tell if it is an
enclave (i.e.
/dev/sgx/enclave)?Quoted 1 line
* In hook
security_mmap_file(), if @file is an enclave, make sure@prot can
Quoted 1 line
only be
PROT_NONE. This is to force all protection changes to gothrough
Quoted 2 lines
security_file_mprotect().
* In the newly introduced hooksecurity_enclave_load(), set WRITTENQuoted 7 lines
for pages
Quoted 1 line
that are requested
PROT_WRITE.How would an LSM associate a page with a specific enclave?
vma->vm_filewill point always point at/dev/sgx/enclave.vma->vm_mmis useless because we're allowing multiple processes to map a single enclave, not to mention that by mm would require holding a reference to the mm.Each
open("/dev/sgx/enclave")syscall creates a *new* instance of struct file to uniquely identify one enclave instance. What I mean is @vma->vm_file, not @vma->vm_file->f_path or @vma->vm_file->f_inode.Quoted 6 lines
Quoted 2 lines
* In hook
security_file_mprotect(), if @vma->vm_file is an enclave,look up
Quoted 1 line
and use WRITTEN flags for all pages within @vma, along with other
global
Quoted 1 line
flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux)
Quoted 8 lines
Quoted 6 lines
to decide
Quoted 1 line
on allowing/rejecting @prot.
vma->vm_filewill always be/dev/sgx/enclaveat this point, which means LSMs don't have the necessary anchor back to the source file, e.g. to enforceFILE__EXECUTE. The noexec file system case is also unaddressed.
vma->vm_fileidentifies an enclave instance uniquely.FILE__EXECUTEis checked bysecurity_enclave_load()using @source_vma->vm_file. Once a page has been EADD'ed, whether to allowRW->RXdepends on .sigstruct file (more precisely, the file backing SIGSTRUCT), whose FILE__* attributes could be cached invma->vm_file->f_securitybysecurity_enclave_init().
The RFC series seemed to dispense with the use of the sigstruct file and just used the source file throughout IIUC. That allowed for reuse of FILE__* permissions without ambiguity rather than introducing separate ENCLAVE__* permissions or using /dev/sgx/enclave inode as the target of all checks.
Regardless, IIUC, your approach requires that we always check FILE__EXECMOD, and FILE__EXECUTE up front during security_enclave_load() irrespective of prot so that we can save the result in the f_security for later use by the mprotect hook. This may generate many spurious audit messages for cases where PROT_EXEC will never be requested, and users will be prone to just always allowing it since they cannot tell when it was actually needed.
Quoted 6 lines
The noexec case should be addressed in
IOC_ADD_PAGESby testing @source_vma->vm_flags &VM_MAYEXEC.Quoted 3 lines
Quoted 3 lines
* In hook
security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.
Quoted 5 lines · Stephen Smalley
The RFC series seemed to dispense with the use of the sigstruct file and just used the source file throughout IIUC. That allowed for reuse of FILE__* permissions without ambiguity rather than introducing separate ENCLAVE__* permissions or using
/dev/sgx/enclaveinode as the target of all checks.
Drat, I meant to explicitly call that out in the cover letter. Yes, the concept of using sigstruct as a proxy was dropped for this RFC. The primary motivation was to avoid having to take a hold a reference to the sigstruct file for the lifetime of the enclave, and in general so that userspace isn't forced to put sigstruct into a file.
Quoted 4 lines
Regardless, IIUC, your approach requires that we always check
FILE__EXECMOD, andFILE__EXECUTEup front duringsecurity_enclave_load()irrespective of prot so that we can save the result in thef_securityfor later use by the mprotect hook.
Correct, this approach requires up front checks.
Quoted 3 lines
This may generate many spurious audit messages for cases where
PROT_EXECwill never be requested, and users will be prone to just always allowing it since they cannot tell when it was actually needed.
Userspace will be able to understand when PROT_EXEC is actually needed as mprotect() will (eventually) fail. Of course that assumes userspace is being intelligent and isn't blindly declaring permissions they don't need, e.g. declaring RWX on all pages even though the enclave never actually maps a RWX or RW->RX page.
One thought for handling this in a more user friendly fashion would be to immediately return -EACCES instead of modifying @allowed_prot. An enclave that truly needs the permission would fail immediately.
An enclave loader that wants/needs to speculatively declare PROT_EXEC, e.g. because the exact requirements of the enclave are unknown, could handle -EACCESS gracefully by retrying the SGX ioctl() with different @allowed_prot, e.g.:
region.flags = SGX_ALLOW_READ | SGX_ALLOW_WRITE | SGX_ALLOW_EXEC;
ret = ioctl(fd, SGX_IOC_ENCLAVE_ADD_REGION, ®ion);
if (ret && errno == EACCES && !(prot & PROT_EXEC)) {
region.flags &= ~SGX_ALLOW_EXEC;
ret = ioctl(fd, SGX_IOC_ENCLAVE_ADD_REGION, ®ion);
}This type of enclave loader would still generate spurious audit messages, but the spurious messages would be limited to enclave loaders that are deliberately probing the allowed permissions.
Quoted 7 lines
Quoted 6 lines
The noexec case should be addressed in
IOC_ADD_PAGESby testing @source_vma->vm_flags &VM_MAYEXEC.Quoted 3 lines
Quoted 3 lines
* In hook
security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.
Hi Stephen,
Quoted 152 lines
From: [email protected] [mailto:linux-sgx- [email protected]] On Behalf Of Stephen Smalley
Sent: Tuesday, June 04, 2019 8:34 AMQuoted 12 lines · Cedric
Quoted 12 lines
From: Christopherson, Sean J
Sent: Monday, June 03, 2019 10:16 AMQuoted 8 lines · Cedric
Hi Sean,
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at
Quoted 3 lines
Quoted 3 lines
runtime.
In this case, the host process should tag *all* pages it *might*
convert
Quoted 1 line
Quoted 1 line
to executable as
ALLOW_EXEC. LSMs can (and should/will) be writtenin
Quoted 1 line
Quoted 1 line
such a way that denying
ALLOW_EXECis fatal to the enclave if andonly
Quoted 3 lines
Quoted 1 line
if the enclave actually attempts
mprotect(PROT_EXEC).What if those pages contain self-modifying code but the host doesn't
know ahead of time? Would it require ALLOW_WRITE|ALLOW_EXEC at EADD? Then would it prevent those pages to start with
PROT_EXEC?Quoted 11 lines
Anyway, my point is that it is unnecessary even if it works.
Quoted 8 lines
Take the SELinux path for example. The only scenario in which
PROT_WRITEis cleared from @allowed_prot is if the page *starts* withPROT_EXEC.
IfPROT_EXECis denied on a page that starts RW, e.g. an EAUG'd page, thenPROT_EXECwill be cleared from @allowed_prot.As Stephen pointed out, auditing the denials on @allowed_prot means
the
Quoted 1 line
Quoted 1 line
log will contain false positives of a sort. But this is more of a
noise
Quoted 1 line
Quoted 1 line
issue than true false positives. E.g. there are three possible
outcomes
Quoted 4 lines
Quoted 4 lines
for the enclave.
- The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. Requesting
ALLOW_EXECis either a straightforward a userspacebug or
Quoted 3 lines
Quoted 3 lines
a poorly written generic enclave loader.
- The enclave conditionally performs EMODPE[PROT_EXEC]. In this
case
Quoted 3 lines
Quoted 3 lines
the denial is a true false positive.
- The enclave does EMODPE[PROT_EXEC] and its host userspace then
fails
Quoted 1 line
Quoted 1 line
on
mprotect(PROT_EXEC), i.e. the LSM denial is working asintended.
Quoted 8 lines
Quoted 3 lines
The audit log will be noisy, but viewed as a whole the denials
aren't
false positives.What I was talking about was EMODPE[PROT_WRITE] on an RX page.
Quoted 2 lines
The potential for noisy audit logs and/or false positives is
unfortunate,
Quoted 3 lines
Quoted 3 lines
but it's (by far) the lesser of many evils.
Quoted 1 line
Theoretically speaking, what you really need is a per page flag
(let's
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used todecide
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
whether to grant
PROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself insteadof
Quoted 4 lines
Quoted 4 lines
asking user mode code to provide it.
Quoted 3 lines
That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocatesome
Quoted 6 lines
Quoted 6 lines
data
Quoted 1 line
structure to store for every page, the WRITTEN flag as described
above.
Quoted 1 line
WRITTEN is cleared initially for all pages.
This would effectively require *every* LSM to duplicate the SGX
driver's
Quoted 4 lines
Quoted 2 lines
functionality, e.g. track per-page metadata, implement locking to prevent races between multiple mm structs, etc...
Architecturally we shouldn't dictate how LSM makes decisions. ALLOW_*
are no difference than PROCESS__* or FILE__* flags, which are just artifacts to assist particular LSMs in decision making. They are never considered part of the LSM interface, even if other LSMs than SELinux may adopt the same/similar approach.
Quoted 2 lines
If code duplication is what you are worrying about, you can put them
in a library, or implement/export them in some new file (maybe
security/enclave.c?) as utility functions. But spilling them into user mode is what I think is unacceptable.Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Open: Given a file of type struct file *, how to tell if it is an
enclave (i.e.
/dev/sgx/enclave)?Quoted 1 line
* In hook
security_mmap_file(), if @file is an enclave, make sure@prot can
Quoted 1 line
only be
PROT_NONE. This is to force all protection changes to goQuoted 10 lines
Quoted 7 lines
through
Quoted 2 lines
security_file_mprotect().
* In the newly introduced hooksecurity_enclave_load(), set WRITTENfor pages
Quoted 1 line
that are requested
PROT_WRITE.How would an LSM associate a page with a specific enclave? vma-
vm_fileQuoted 2 lines
will point always point at
/dev/sgx/enclave.vma->vm_mmis useless because we're allowing multiple processes to map a single enclave,not
Quoted 3 lines
Quoted 1 line
to mention that by mm would require holding a reference to the mm.
Each
open("/dev/sgx/enclave")syscall creates a *new* instance ofstruct file to uniquely identify one enclave instance. What I mean is @vma->vm_file, not @vma->vm_file->f_path or @vma->vm_file->f_inode.
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
* In hook
security_file_mprotect(), if @vma->vm_file is an enclave,look up
Quoted 1 line
and use WRITTEN flags for all pages within @vma, along with other
global
Quoted 1 line
flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux)
to decide
Quoted 1 line
on allowing/rejecting @prot.
vma->vm_filewill always be/dev/sgx/enclaveat this point, whichmeans
Quoted 2 lines
Quoted 2 lines
LSMs don't have the necessary anchor back to the source file, e.g. to enforce
FILE__EXECUTE. The noexec file system case is alsounaddressed.
Quoted 2 lines
vma->vm_fileidentifies an enclave instance uniquely.FILE__EXECUTEischecked by
security_enclave_load()using @source_vma->vm_file. Once a page has been EADD'ed, whether to allowRW->RXdepends on .sigstruct file (more precisely, the file backing SIGSTRUCT), whose FILE__* attributes could be cached invma->vm_file->f_securitybysecurity_enclave_init().
Quoted 5 lines
The RFC series seemed to dispense with the use of the sigstruct file and just used the source file throughout IIUC. That allowed for reuse of FILE__* permissions without ambiguity rather than introducing separate ENCLAVE__* permissions or using
/dev/sgx/enclaveinode as the target of all checks.
That's right. But that's not the distinction between Sean's patch and my proposal.
My point here is, from the perspective of LSM architecture, LSM hooks shall be defined to pass adequate information to allow *all* possible implementations to make reasonable decisions. In particular, all parameters to EADD (i.e. target linear address, SECINFO, source page) could affect (current and future) decisions but Sean's definition of security_enclave_load() passes only the source, which limits the possibility of other implementations. Another point I'm trying to make is, different LSM implementations may need different information at any given decision point, therefore it's *not* possible to always pass "right" information at "right" time. And that's why I think .f_security was added to struct file to allow stateful LSMs. Sean's patch however is trying the opposite, as ALLOW_* flags should otherwise be part of internal state of LSMs, but they are spilled into SGX driver and also userspace.
Quoted 8 lines
Regardless, IIUC, your approach requires that we always check
FILE__EXECMOD, andFILE__EXECUTEup front duringsecurity_enclave_load()irrespective of prot so that we can save the result in thef_securityfor later use by the mprotect hook. This may generate many spurious audit messages for cases wherePROT_EXECwill never be requested, and users will be prone to just always allowing it since they cannot tell when it was actually needed.
Yes and no.
For those not following this discussion closely, here's the prototype of security_enclave_load() that I proposed in one of my earlier emails.
int security_enclave_load(struct file *enclave_fd, unsigned long linear_address, unsigned long nr_pages, int prot, struct vm_area_struct *source_vma);
@enclave_fd identifies the enclave to which new pages are being added.
@linear_address/@nr_pages specifies the linear range of pages being added.
@prot specifies the initial protection of those newly added pages. It is taken from the vma covering the target range.
@source_vma covers the source pages in the case of EADD. An LSM is expected to make sure security_file_mprotect(source_vma, prot, prot) would succeed before checking anything else, unless @source_vma is NULL, indicating pages are being EAUG'ed. In all cases, LSM is expected to "remember" @prot for all those pages to be checked in future security_file_mprotect() invocations.
Architecture wise, the idea here is for SGX driver to pass in all information relevant for an LSM's decision.
Implementation wise, LSM may allow PROT_EXEC depending on FILE__EXECUTE of the source file (@source_vma->vm_file), or the sigstruct file (will be provided to LSM at security_enclave_init), or /dev/sgx/enclave. It makes most sense to me to use the source file, hence the check would most likely be done here. For future security_file_mprotect(), the source file's FILE__EXECMOD could also be cached here, or it could use sigstruct file's FILE__EXECMOD instead. Given the fact that no EPC pages could be accessed before EINIT, the major purpose of security_enclave_load() is for LSM to cache whatever information deemed appropriate for the pages being EADD'ed (i.e. @source_vma != NULL) so that it has necessary information to make decisions in security_file_mprotect() in future. And in that regard the parameter @prot is unnecessary but I decided to have it here for 2 reasons: 1) Pages may be EADD'ed within an existing VMA (so no upcoming mprotect) so LSM's decision is needed right away and 2) @source_vma may not be able to mprotect(@prot) and in that case it'd be better to fail EADD instead of failing at mprotect() later.
Quoted 9 lines
Quoted 3 lines
The noexec case should be addressed in
IOC_ADD_PAGESby testing@source_vma->vm_flags &
VM_MAYEXEC.Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
* In hook
security_file_free(), if @file is an enclave, freestorage
Quoted 1 line
Quoted 1 line
Quoted 1 line
allocated for WRITTEN flags.
Quoted 35 lines · Cedric
Hi Sean,
Quoted 21 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMThis series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at times, but I think/hope we ended up with something reasonable.
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear
ALLOW_EXECto prevent ever mapping the page withPROT_EXEC. SGX enforces the allowed perms via a newmprotect()vm_opshook, e.g. like regularmprotect()uses MAY_{READ,WRITE,EXEC}.
ALLOW_EXECis used to deny hings like loading an enclave from a noexec file system or from a file without EXECUTE permissions, e.g. without theALLOW_EXECconcept, on SGX2 hardware (regardless of kernel support) userspace could EADD from a noexec file using read-only permissions, and later usemprotect()and ENCLU[EMODPE] to gain execute permissions.
ALLOW_WRITEis used in conjuction withALLOW_EXECto enforce SELinux's EXECMOD (or EXECMEM).Quoted 7 lines
This is very much an RFC series. It's only compile tested, likely has obvious bugs, the SELinux patch could be completely harebrained, etc...
My goal at this point is to get feedback at a macro level, e.g. is the core concept viable/acceptable, are there objection to hookingmprotect(), etc...Andy and Cedric, hopefully this aligns with your general expectations based on our last discussion.
I couldn't understand the real intentions of ALLOW_* flags until I saw them in code. I have to say C is more expressive than English in that regard :)
Generally I agree with your direction but think ALLOW_* flags are completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at runtime.
Quoted 8 lines
Theoretically speaking, what you really need is a per page flag (let's name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead of asking user mode code to provide it.That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate some data structure to store for every page, the WRITTEN flag as described above. WRITTEN is cleared initially for all pages.
Open: Given a file of type struct file *, how to tell if it is an enclave (i.e./dev/sgx/enclave)?
* In hooksecurity_mmap_file(), if @file is an enclave, make sure @prot can only bePROT_NONE. This is to force all protection changes to go throughsecurity_file_mprotect().
* In the newly introduced hooksecurity_enclave_load(), set WRITTEN for pages that are requestedPROT_WRITE.
* In hooksecurity_file_mprotect(), if @vma->vm_file is an enclave, look up and use WRITTEN flags for all pages within @vma, along with other global flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux) to decide on
allowing/rejecting @prot.
At this point we have no knowledge of the source vma/file, right? So what do we check FILE__EXECUTE and/or FILE__EXECMOD against? vma->vm_file at this point is /dev/sgx/enclave, right?
Quoted 39 lines
* In hook
security_file_free(), if @file is an enclave, free storage allocated for WRITTEN flags.I'll try to make more detailed comments in my replies to individual patches sometime tomorrow.
Quoted 30 lines
Lastly, I added a patch to allow userspace to add multiple pages in a single
ioctl(). It's obviously not directly related to the security stuff, but the idea tangentially came up during earlier discussions and it's something I think the UAPI should provide (it's a tiny change). Since I was modifying the UAPI anyways, I threw it in.Sean Christopherson (9):
x86/sgx: Remove unused local variable insgx_encl_release()x86/sgx: Do not naturally alignMAP_FIXEDaddress x86/sgx: Allow userspace to add multiple pages in singleioctl()
mm: Introducevm_ops->mprotect()
x86/sgx: Restrict mapping without an enclave page toPROT_NONEx86/sgx: Require userspace to provide allowed prots toADD_PAGESx86/sgx: Enforce noexec filesystem restriction for enclaves
LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGXsecurity/selinux: Addenclave_load()implementationarch/x86/include/uapi/asm/sgx.h | 30 ++++-- arch/x86/kernel/cpu/sgx/driver/ioctl.c | 143 +++++++++++++++++-------- arch/x86/kernel/cpu/sgx/driver/main.c | 13 ++- arch/x86/kernel/cpu/sgx/encl.c | 31 +++++- arch/x86/kernel/cpu/sgx/encl.h | 4 + include/linux/lsm_hooks.h | 16 +++ include/linux/mm.h | 2 + include/linux/security.h | 2 + mm/mprotect.c | 15 ++- security/security.c | 8 ++ security/selinux/hooks.c | 85 +++++++++++++++ 11 files changed, 290 insertions(+), 59 deletions(-)Quoted 2 lines
--
2.21.0-Cedric
Quoted 78 lines
From: [email protected] [mailto:linux-sgx- [email protected]] On Behalf Of Stephen Smalley
Sent: Monday, June 03, 2019 10:47 AMQuoted 9 lines · Cedric
Hi Sean,
Quoted 7 lines
From: Christopherson, Sean J
Sent: Friday, May 31, 2019 4:32 PMThis series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at
times, but I think/hope we ended up with something reasonable.
Quoted 6 lines
Quoted 6 lines
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear
ALLOW_EXECto prevent ever mapping the pagewith
PROT_EXEC. SGX enforces the allowed perms via a newmprotect()vm_opshook, e.g. like regularmprotect()uses MAY_{READ,WRITE,EXEC}.Quoted 5 lines
Quoted 5 lines
ALLOW_EXECis used to deny hings like loading an enclave from a noexec file system or from a file without EXECUTE permissions, e.g. without theALLOW_EXECconcept, on SGX2 hardware (regardless of kernel support) userspace could EADD from a noexec file using read-only permissions, and later use
mprotect()and ENCLU[EMODPE] to gain execute permissions.Quoted 2 lines
Quoted 2 lines
ALLOW_WRITEis used in conjuction withALLOW_EXECto enforceSELinux's EXECMOD (or EXECMEM).
Quoted 3 lines
Quoted 3 lines
This is very much an RFC series. It's only compile tested, likely has obvious bugs, the SELinux patch could be completely harebrained,
etc...
Quoted 2 lines
Quoted 2 lines
My goal at this point is to get feedback at a macro level, e.g. is the core concept viable/acceptable, are there objection to hooking
mprotect(), etc...Quoted 9 lines
Quoted 3 lines
Andy and Cedric, hopefully this aligns with your general expectations based on our last discussion.
I couldn't understand the real intentions of ALLOW_* flags until I saw them in code. I have to say C is more expressive than English in that regard :)
Generally I agree with your direction but think ALLOW_* flags are
completely internal to LSM because they can be both produced and consumed inside an LSM module. So spilling them into SGX driver and also user mode code makes the solution ugly and in some cases impractical because not every enclave host process has a priori knowledge on whether or not an enclave page would be EMODPE'd at runtime.
Quoted 2 lines
Theoretically speaking, what you really need is a per page flag (let's
name it WRITTEN?) indicating whether a page has ever been written to (or more precisely, granted
PROT_WRITE), which will be used to decide whether to grantPROT_EXECwhen requested in future. Given the fact that allmprotect()goes through LSM andmmap()is limited toPROT_NONE, it's easy for LSM to capture that flag by itself instead of asking user mode code to provide it.Quoted 3 lines
That said, here is the summary of what I think is a better approach.
* In hooksecurity_file_alloc(), if @file is an enclave, allocate somedata structure to store for every page, the WRITTEN flag as described above. WRITTEN is cleared initially for all pages.
Quoted 1 line
Open: Given a file of type struct file *, how to tell if it is an
enclave (i.e.
/dev/sgx/enclave)?Quoted 1 line
* In hook
security_mmap_file(), if @file is an enclave, make sure@prot can only be
PROT_NONE. This is to force all protection changes to go throughsecurity_file_mprotect().Quoted 1 line
* In the newly introduced hook
security_enclave_load(), set WRITTENfor pages that are requested
PROT_WRITE.Quoted 4 lines
* In hook
security_file_mprotect(), if @vma->vm_file is an enclave, look up and use WRITTEN flags for all pages within @vma, along with other global flags (e.g. PROCESS__EXECMEM/FILE__EXECMOD in the case of SELinux) to decide on
Quoted 5 lines
allowing/rejecting @prot.
At this point we have no knowledge of the source vma/file, right? So what do we check
FILE__EXECUTEand/orFILE__EXECMODagainst?vma->vm_fileat this point is/dev/sgx/enclave, right?
My apology to the confusions here.
Yes, vma->vm_file is always /dev/sgx/enclave, but each open("/dev/sgx/enclave") returns a *new* file struct (let's denote it as @enclave_fd) that uniquely identifies one enclave instance, and the expectation is that @enclave_fd->f_security would be used by LSM to store enclave specific information, including ALLOW_* flags and whatever deemed appropriate by an LSM module.
In the case of SELinux, and if the choice is to use FILE__EXECMOD of .sigstruct file to authorize RW->RX at runtime, then SELinux could cache that flag in @enclave_fd->f_security upon security_enclave_init().
Quoted 5 lines · Sean Christopherson
This series is the result of a rather absurd amount of discussion over how to get SGX to play nice with LSM policies, without having to resort to evil shenanigans or put undue burden on userspace. The discussion definitely wandered into completely insane territory at times, but I think/hope we ended up with something reasonable.
By definition this is a broken series because it does not apply to mainline. Even RFC series should at least apply. Would be better idea to discuss design ideas and use snippets instead. Now you have to take original v20 and apply to these patches to evaluate anything.
Quoted 7 lines
The basic gist of the approach is to require userspace to declare what protections are maximally allowed for any given page, e.g. add a flags field for loading enclave pages that takes ALLOW_{READ,WRITE,EXEC}. LSMs can then adjust the allowed protections, e.g. clear
ALLOW_EXECto prevent ever mapping the page withPROT_EXEC. SGX enforces the allowed perms via a newmprotect()vm_opshook, e.g. like regularmprotect()uses MAY_{READ,WRITE,EXEC}.
mprotect() does not use MAY_{READ,WRITE,EXEC} constants. It uses VM_MAY{READ,WRITE,EXEC,SHARED} constants.
What are ALLOW_{READ,WRITE,EXEC} and how they are used? What does the hook do and why it is in vm_ops and not in file_operations? Are they arguments to the ioctl or internal variables that are set based on SECINFO?
/Jarkko