Skip to contentSkip to conversation

x86/sgx: Enable PROT_EXEC for EAUG'd pages

PATCH In review Jarkko Sakkinen 2 participants 7 messages Mar 7, 2022 – Mar 8, 2022

Subsystems (2)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH 1/1 x86/sgx: Enable PROT_EXEC for EAUG'd pages

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1
author maintainer
#1 lore raw
Quoted 41 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

BR, Jarkko

author maintainer
#2 lore raw
Quoted 47 lines · Jarkko Sakkinen
Quoted 41 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

Quoted 3 lines

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

I.e. once signed content has passed EINIT ioctl, and whatever checks there are now or in future (e.g. LSM hooks), the system has accepted the enclave behaviour, and it includes also the use of EACCEPT opcode.

It's the exec or no-exec decision point. The thing that these patches do is making an obfuscated mess of all this. When EINIT has passed, it has been decided that the enclave can do its workload. Let's not throw stick in front of it, and make everyones life misserable.

BR, Jarkko

#3 lore raw

Hi Jarkko,

Quoted 11 lines · Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Linux has mechanisms to enforce what can be executed. For example, with SELinux a process can be required to have PROCESS__EXECHEAP or PROCESS__EXECSTACK before it can be allowed to execute writable memory.

A few SGX runtimes enables unmodified executables to be run within SGX enclaves.

Does a change like this not enable executables prevented by existing security mechanisms to circumvent such restrictions by running within a SGX enclave?

Quoted 40 lines
Quoted 36 lines
Quoted 31 lines

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

Quoted 3 lines

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

Quoted 8 lines

I.e. once signed content has passed EINIT ioctl, and whatever checks there are now or in future (e.g. LSM hooks), the system has accepted the enclave behaviour, and it includes also the use of EACCEPT opcode.

It's the exec or no-exec decision point. The thing that these patches do is making an obfuscated mess of all this. When EINIT has passed, it has been decided that the enclave can do its workload. Let's not throw stick in front of it, and make everyones life misserable.

A common use for these dynamically added pages is to increase the heap and stack. Always allowing PTEs of RWX on these pages irrespective whether it will be used for heap, stack, or relocatable code does not match with how the kernel manages protections.

As I said before I am not comfortable with such a change and cannot sign off on this. I would defer to the maintainers to choose the direction.

Reinette

author maintainer
#4 lore raw
Quoted 19 lines · Reinette Chatre

Hi Jarkko,

Quoted 11 lines · Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Linux has mechanisms to enforce what can be executed. For example, with SELinux a process can be required to have PROCESS__EXECHEAP or PROCESS__EXECSTACK before it can be allowed to execute writable memory.

Quoted 5 lines

A few SGX runtimes enables unmodified executables to be run within SGX enclaves.

Does a change like this not enable executables prevented by existing security mechanisms to circumvent such restrictions by running within a SGX enclave?

It does not open any extra exposure as the existing policies apply for the enclave content created before initialization.

And I'm not sure what kind of circumvention scenario we are talking about.

Quoted 54 lines
Quoted 40 lines
Quoted 36 lines
Quoted 31 lines

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

Quoted 3 lines

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

Quoted 8 lines

I.e. once signed content has passed EINIT ioctl, and whatever checks there are now or in future (e.g. LSM hooks), the system has accepted the enclave behaviour, and it includes also the use of EACCEPT opcode.

It's the exec or no-exec decision point. The thing that these patches do is making an obfuscated mess of all this. When EINIT has passed, it has been decided that the enclave can do its workload. Let's not throw stick in front of it, and make everyones life misserable.

A common use for these dynamically added pages is to increase the heap and stack. Always allowing PTEs of RWX on these pages irrespective whether it will be used for heap, stack, or relocatable code does not match with how the kernel manages protections.

Quoted 5 lines

As I said before I am not comfortable with such a change and cannot sign off on this. I would defer to the maintainers to choose the direction.

Reinette

My choice is to not use this existing mechanism for dynamically created pages because otherwise the implementation overally is just crippled.

Something unusable is for sure as secure as you can get.

BR, Jarkko

#5 lore raw

Hi Jarkko,

Quoted 90 lines · Jarkko Sakkinen
Quoted 19 lines · Reinette Chatre

Hi Jarkko,

Quoted 11 lines · Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Linux has mechanisms to enforce what can be executed. For example, with SELinux a process can be required to have PROCESS__EXECHEAP or PROCESS__EXECSTACK before it can be allowed to execute writable memory.

Quoted 5 lines

A few SGX runtimes enables unmodified executables to be run within SGX enclaves.

Does a change like this not enable executables prevented by existing security mechanisms to circumvent such restrictions by running within a SGX enclave?

It does not open any extra exposure as the existing policies apply for the enclave content created before initialization.

And I'm not sure what kind of circumvention scenario we are talking about.

Quoted 58 lines
Quoted 40 lines
Quoted 36 lines
Quoted 31 lines

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

Quoted 3 lines

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

Quoted 8 lines

I.e. once signed content has passed EINIT ioctl, and whatever checks there are now or in future (e.g. LSM hooks), the system has accepted the enclave behaviour, and it includes also the use of EACCEPT opcode.

It's the exec or no-exec decision point. The thing that these patches do is making an obfuscated mess of all this. When EINIT has passed, it has been decided that the enclave can do its workload. Let's not throw stick in front of it, and make everyones life misserable.

A common use for these dynamically added pages is to increase the heap and stack. Always allowing PTEs of RWX on these pages irrespective whether it will be used for heap, stack, or relocatable code does not match with how the kernel manages protections.

As I said before I am not comfortable with such a change and cannot sign off on this. I would defer to the maintainers to choose the direction.

Quoted 7 lines
Quoted 1 line

Reinette

My choice is to not use this existing mechanism for dynamically created pages because otherwise the implementation overally is just crippled.

Something unusable is for sure as secure as you can get.

ok, I will proceed with your guidance here and include your snippet from this patch into the next version.

One question, regarding "MRSIGNATURE" - did you perhaps mean "MRENCLAVE"?

Reinette

author maintainer
#6 lore raw
Quoted 95 lines · Reinette Chatre

Hi Jarkko,

Quoted 92 lines · Jarkko Sakkinen
Quoted 19 lines · Reinette Chatre

Hi Jarkko,

Quoted 11 lines · Jarkko Sakkinen
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Jarkko Sakkinen

vm_max_permissions was created to control the pre-initialization content that contributes to MRSIGNATURE. It was never meant to be as a limit to dynamically added pages.

E.g. static content could be used as a hook for LSM's to decide whether certain signature is qualified for EINIT. Dynamic content has nothing to do with that. The current mechanisms only add to the complexity on how to control PTE and EPCM permissions, and do not add anything else than obfuscity to security side of things.

Linux has mechanisms to enforce what can be executed. For example, with SELinux a process can be required to have PROCESS__EXECHEAP or PROCESS__EXECSTACK before it can be allowed to execute writable memory.

Quoted 5 lines

A few SGX runtimes enables unmodified executables to be run within SGX enclaves.

Does a change like this not enable executables prevented by existing security mechanisms to circumvent such restrictions by running within a SGX enclave?

It does not open any extra exposure as the existing policies apply for the enclave content created before initialization.

And I'm not sure what kind of circumvention scenario we are talking about.

Quoted 59 lines
Quoted 40 lines
Quoted 36 lines
Quoted 31 lines

Thus add PROT_EXEC to the permissions assigned by the #PF handler.

---
 arch/x86/kernel/cpu/sgx/encl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +4 -5
index 79e39bd99c09..0256918b2c2f 100644
@@ -160,12 +160,11 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
 	encl_page->encl = encl;
 
 	/*
-	 * Adding a regular page that is architecturally allowed to only
-	 * be created with RW permissions.
-	 * TBD: Interface with user space policy to support max permissions
-	 * of RWX.
+	 * Dynamic pages do not contribute to MRSIGNATURE, i.e. they are
+	 * controlled only by PTE and EPCM permissions. Thus, the no limit
+	 * is set here.
 	 */
-	prot = PROT_READ | PROT_WRITE;
+	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
 	encl_page->vm_run_prot_bits = calc_vm_prot_bits(prot, 0);
 	encl_page->vm_max_prot_bits = encl_page->vm_run_prot_bits;
 
Signature · 1 line
2.35.1

This is really a show stopper. I think here's a logical mistake on for what purpose vm_max_prot_bits are used for. They are meant for the static and also signed content of the enclave.

Quoted 3 lines

These changes in the patch set that are related to vm_max_prot_bits only messes up what already exists, and make incredibly hard to implement anything decent on top of SGX2 features.

Quoted 8 lines

I.e. once signed content has passed EINIT ioctl, and whatever checks there are now or in future (e.g. LSM hooks), the system has accepted the enclave behaviour, and it includes also the use of EACCEPT opcode.

It's the exec or no-exec decision point. The thing that these patches do is making an obfuscated mess of all this. When EINIT has passed, it has been decided that the enclave can do its workload. Let's not throw stick in front of it, and make everyones life misserable.

A common use for these dynamically added pages is to increase the heap and stack. Always allowing PTEs of RWX on these pages irrespective whether it will be used for heap, stack, or relocatable code does not match with how the kernel manages protections.

As I said before I am not comfortable with such a change and cannot sign off on this. I would defer to the maintainers to choose the direction.

Reinette

Quoted 8 lines
Quoted 5 lines

My choice is to not use this existing mechanism for dynamically created pages because otherwise the implementation overally is just crippled.

Something unusable is for sure as secure as you can get.

ok, I will proceed with your guidance here and include your snippet from this patch into the next version.

Thank you.

Quoted 1 line

One question, regarding "MRSIGNATURE" - did you perhaps mean "MRENCLAVE"?

It could either both define "static root of trust". Does not really all that much which one you use in the comment.

Quoted 1 line

Reinette

BR, Jarkko