Skip to contentSkip to conversation

x86/vdso: Flatten and clean up struct sgx_enclave_run

PATCH New Jarkko Sakkinen 1 participant 1 message Oct 2, 2020

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

PATCH v2 1/1 x86/vdso: Flatten and clean up struct sgx_enclave_run

Reduce the struct size to 64 bytes. It makes sense to have a naturally aligning struct size and it leaves 24 bytes of spare space for possible future expansion. Having 256 bytes is over the top without any reasonable explanation, which does not exist in the commit for the SGX vDSO.

Drop struct sgx_exception. It does not serve any semantic purpose in the context of this vDSO. Doing this is also consistent with user_handler and user_data fields, as neither are they encapsulated in an embedded data structure. Use exception_vector, exception_error_code and exception_addr as the variables that store vDSO exception fixup provided information.

The documentation, selftest and the kernel source code use the terms "user handler" and "exit handler" in somewhat inconsistent manner. Use "user handler" everywhere as the callback is a caller provided, and thus we don't know what it is doing.

Remove 'exit_reason' as it is a redundant field. Instead, have just a field called 'leaf' that tells the last seen ENCLU.

Validate the whole reserved (aka padding) area that it contains only zero bytes. It is an unsafe practice to not validate the full data structure. As a side-effect, 'flags' can be removed because it can be added later as one can assume that the reserved area does not contain random bytes, but instead is always filled with zeros.

Cc Nathaniel McCallum <[email protected]>
Cc Haitao Huang <[email protected]>
CC Sean Christopherson <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Cedric Xing <[email protected]>

---
* Rename aex_handler as user handler.
* Refine exception_* naming a just a bit more in struct sgx_enclave_run.
* Add zero validation for the reserved area, as it helps to protect among other things, from kernel bugs. This was already experienced with the self-test while developing this patch version. This fully removes the need to have the 'flags' field in the structure, as formally it had the same semantics as just checking the first 4 bytes.
* Fix memory offsets in vsgx.S. There were some mistakes in the previous version. This bug was catched by the aformentioned memory validation when running the self-test.
* Refine check_result() in the selftest to report all issues before returning. Rename it as report_results() and make it a boolean function, as the POSIX error code is not used for anything.

 arch/x86/entry/vdso/vsgx.S         | 67 ++++++++++++++++--------------
 arch/x86/include/asm/enclu.h       |  1 +
 arch/x86/include/uapi/asm/sgx.h    | 62 ++++++++++-----------------
 tools/testing/selftests/sgx/main.c | 49 +++++++++++++---------
 4 files changed, 87 insertions(+), 92 deletions(-)
arch/x86/entry/vdso/vsgx.S +35 -32
index 8f8190ab9ed5..23d110aac432 100644
@@ -7,25 +7,19 @@
 
 #include "extable.h"
 
-/* Offset of 'struct sgx_enclave_run' relative to %rbp. */
-#define SGX_ENCLAVE_RUN_PTR	2*8
-
-/* Offsets into 'struct sgx_enclave_run'. */
-#define SGX_ENCLAVE_RUN_TCS		0*8
-#define SGX_ENCLAVE_RUN_FLAGS		1*8
-#define SGX_ENCLAVE_RUN_EXIT_REASON	1*8 + 4
-#define SGX_ENCLAVE_RUN_USER_HANDLER	2*8
-/* #define SGX_ENCLAVE_RUN_USER_DATA	3*8 */
-#define SGX_ENCLAVE_RUN_EXCEPTION	4*8
-
-#define SGX_SYNCHRONOUS_EXIT		0
-#define SGX_EXCEPTION_EXIT		1
-
-/* Offsets into sgx_enter_enclave.exception. */
-#define SGX_EX_LEAF			0*8
-#define SGX_EX_TRAPNR			0*8+4
-#define SGX_EX_ERROR_CODE		0*8+6
-#define SGX_EX_ADDRESS			1*8
+/* Relative to %rbp. */
+#define SGX_ENCLAVE_OFFSET_OF_RUN		16
+
+/* The offsets relative to struct sgx_enclave_run. */
+#define SGX_ENCLAVE_RUN_TCS			0
+#define SGX_ENCLAVE_RUN_USER_HANDLER		8
+#define SGX_ENCLAVE_RUN_USER_DATA		16 /* unused */
+#define SGX_ENCLAVE_RUN_LEAF			24
+#define SGX_ENCLAVE_RUN_EXCEPTION_VECTOR	28
+#define SGX_ENCLAVE_RUN_EXCEPTION_ERROR_CODE	30
+#define SGX_ENCLAVE_RUN_EXCEPTION_ADDR		32
+#define SGX_ENCLAVE_RUN_RESERVED_START		40
+#define SGX_ENCLAVE_RUN_RESERVED_END		64
 
 .code64
 .section .text, "ax"
@@ -49,12 +43,23 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
 	cmp	$ERESUME, %eax
 	ja	.Linvalid_input
 
-	mov	SGX_ENCLAVE_RUN_PTR(%rbp), %rcx
+	mov	SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rcx
 
-	/* No flags are currently defined/supported. */
-	cmpl	$0, SGX_ENCLAVE_RUN_FLAGS(%rcx)
+	/* Validate that the reserved area contains only zeros. */
+	push	%rax
+	push	%rbx
+	mov	$SGX_ENCLAVE_RUN_RESERVED_START, %rbx
+1:
+	mov	(%rcx, %rbx), %rax
+	cmpq	$0, %rax
 	jne	.Linvalid_input
 
+	add	$8, %rbx
+	cmpq	$SGX_ENCLAVE_RUN_RESERVED_END, %rbx
+	jne	1b
+	pop	%rbx
+	pop	%rax
+
 	/* Load TCS and AEP */
 	mov	SGX_ENCLAVE_RUN_TCS(%rcx), %rbx
 	lea	.Lasync_exit_pointer(%rip), %rcx
@@ -65,10 +70,10 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
 	enclu
 
 	/* EEXIT jumps here unless the enclave is doing something fancy. */
-	mov	SGX_ENCLAVE_RUN_PTR(%rbp), %rbx
+	mov	SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rbx
 
 	/* Set exit_reason. */
-	movl	$SGX_SYNCHRONOUS_EXIT, SGX_ENCLAVE_RUN_EXIT_REASON(%rbx)
+	movl	$EEXIT, SGX_ENCLAVE_RUN_LEAF(%rbx)
 
 	/* Invoke userspace's exit handler if one was provided. */
 .Lhandle_exit:
@@ -92,15 +97,13 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
 	jmp	.Lout
 
 .Lhandle_exception:
-	mov	SGX_ENCLAVE_RUN_PTR(%rbp), %rbx
-
-	/* Set the exit_reason and exception info. */
-	movl	$SGX_EXCEPTION_EXIT, SGX_ENCLAVE_RUN_EXIT_REASON(%rbx)
+	mov	SGX_ENCLAVE_OFFSET_OF_RUN(%rbp), %rbx
 
-	mov	%eax, (SGX_ENCLAVE_RUN_EXCEPTION + SGX_EX_LEAF)(%rbx)
-	mov	%di,  (SGX_ENCLAVE_RUN_EXCEPTION + SGX_EX_TRAPNR)(%rbx)
-	mov	%si,  (SGX_ENCLAVE_RUN_EXCEPTION + SGX_EX_ERROR_CODE)(%rbx)
-	mov	%rdx, (SGX_ENCLAVE_RUN_EXCEPTION + SGX_EX_ADDRESS)(%rbx)
+	/* Set the exception info. */
+	mov	%eax, (SGX_ENCLAVE_RUN_LEAF)(%rbx)
+	mov	%di,  (SGX_ENCLAVE_RUN_EXCEPTION_VECTOR)(%rbx)
+	mov	%si,  (SGX_ENCLAVE_RUN_EXCEPTION_ERROR_CODE)(%rbx)
+	mov	%rdx, (SGX_ENCLAVE_RUN_EXCEPTION_ADDR)(%rbx)
 	jmp	.Lhandle_exit
 
 .Linvoke_userspace_handler:
arch/x86/include/asm/enclu.h +1 -0
index 06157b3e9ede..b1314e41a744 100644
@@ -4,5 +4,6 @@
 
 #define EENTER	0x02
 #define ERESUME	0x03
+#define EEXIT	0x04
 
 #endif /* _ASM_X86_ENCLU_H */
arch/x86/include/uapi/asm/sgx.h +21 -41

110 lines not on this page · raw message

tools/testing/selftests/sgx/main.c +30 -19

87 lines not on this page · raw message

Signature · 1 line
2.25.1