selftests/x86: Add a selftest for SGX
Subsystems (4)
RFC PATCH 1/1 selftests/x86: Add a selftest for SGX
Add a selftest for SGX. It is a trivial test where a simple enclave copies one 64-bit word of memory between two memory locations given to the enclave as arguments.
---
Since this is my very first addition to selftests, I decided to roll out an RFC patch before including this to the main SGX series.
tools/testing/selftests/x86/Makefile | 10 + tools/testing/selftests/x86/sgx/Makefile | 47 ++ tools/testing/selftests/x86/sgx/encl.c | 20 + tools/testing/selftests/x86/sgx/encl.lds | 33 ++ .../selftests/x86/sgx/encl_bootstrap.S | 94 ++++ tools/testing/selftests/x86/sgx/encl_piggy.S | 16 + tools/testing/selftests/x86/sgx/encl_piggy.h | 13 + .../testing/selftests/x86/sgx/sgx-selftest.c | 146 +++++ tools/testing/selftests/x86/sgx/sgx_arch.h | 109 ++++ tools/testing/selftests/x86/sgx/sgx_call.S | 20 + tools/testing/selftests/x86/sgx/sgx_uapi.h | 100 ++++ tools/testing/selftests/x86/sgx/sgxsign.c | 503 ++++++++++++++++++ .../testing/selftests/x86/sgx/signing_key.pem | 39 ++ 13 files changed, 1150 insertions(+) create mode 100644 tools/testing/selftests/x86/sgx/Makefile create mode 100644 tools/testing/selftests/x86/sgx/encl.c create mode 100644 tools/testing/selftests/x86/sgx/encl.lds create mode 100644 tools/testing/selftests/x86/sgx/encl_bootstrap.S create mode 100644 tools/testing/selftests/x86/sgx/encl_piggy.S create mode 100644 tools/testing/selftests/x86/sgx/encl_piggy.h create mode 100644 tools/testing/selftests/x86/sgx/sgx-selftest.c create mode 100644 tools/testing/selftests/x86/sgx/sgx_arch.h create mode 100644 tools/testing/selftests/x86/sgx/sgx_call.S create mode 100644 tools/testing/selftests/x86/sgx/sgx_uapi.h create mode 100644 tools/testing/selftests/x86/sgx/sgxsign.c create mode 100644 tools/testing/selftests/x86/sgx/signing_key.pem
tools/testing/selftests/x86/Makefile +10 -0
@@ -1,4 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 + +SUBDIRS_64 := sgx + all: include ../lib.mk
@@ -67,6 +70,13 @@ all_32: $(BINARIES_32) all_64: $(BINARIES_64) +all_64: $(SUBDIRS_64) + @for DIR in $(SUBDIRS_64); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + mkdir $$BUILD_TARGET -p; \ + make OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64) $(BINARIES_32): $(OUTPUT)/%_32: %.c
tools/testing/selftests/x86/sgx/Makefile new file +47 -0
47 lines not on this page · raw message
tools/testing/selftests/x86/sgx/encl.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// Copyright(c) 2016-18 Intel Corporation. + +#include <stddef.h> +#include "sgx_arch.h" + +static void *memcpy (void *dest,const void *src,size_t n) +{ +size_t i; + +for (i =0 ; i < n; i++) + ((char *)dest)[i] = ((char *)src)[i]; + +return dest; +} + +void encl_body (void *rdi,void *rsi) +{ +memcpy (rsi, rdi,8 ); +}
tools/testing/selftests/x86/sgx/encl.lds new file +33 -0
@@ -0,0 +1,33 @@ +OUTPUT_FORMAT(elf64-x86-64) + +SECTIONS +{ + . = 0; + .tcs : { + *(.tcs*) + } + + . = ALIGN(4096); + .text : { + *(.text*) + *(.rodata*) + } + + . = ALIGN(4096); + .data : { + *(.data*) + } + + /DISCARD/ : { + *(.data*) + *(.comment*) + *(.note*) + *(.debug*) + *(.eh_frame*) + } +} + +ASSERT(!DEFINED(.altinstructions), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.altinstr_replacement), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.retpoline_safe), "RETPOLINE ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.nospec), "RETPOLINE ALTERNATIVES are not supported in the SGX LE")
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +94 -0
94 lines not on this page · raw message
tools/testing/selftests/x86/sgx/encl_piggy.S new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ +/* + * Copyright(c) 2016-18 Intel Corporation. + */ + + .section ".rodata", "a" + +encl_bin: + .globl encl_bin + .incbin "encl.bin" +encl_bin_end: + .globl encl_bin_end + +encl_ss: + .globl encl_ss + .incbin "encl.ss"
tools/testing/selftests/x86/sgx/encl_piggy.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ +/* +* Copyright(c) 2016-18 Intel Corporation. +*/ + +#ifndef ENCL_PIGGY_H +#define ENCL_PIGGY_H + +extern unsigned char encl_bin[]; +extern unsigned char encl_bin_end[]; +extern unsigned char encl_ss[]; + +#endif /* ENCL_PIGGY_H */
tools/testing/selftests/x86/sgx/sgx-selftest.c new file +146 -0
146 lines not on this page · raw message
tools/testing/selftests/x86/sgx/sgx_arch.h new file +109 -0
109 lines not on this page · raw message
tools/testing/selftests/x86/sgx/sgx_call.S new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ +/** +* Copyright(c) 2016-18 Intel Corporation. +*/ +.macro ENCLU +.byte 0x0f, 0x01, 0xd7 +.endm + + .text + + .global sgx_call +sgx_call: + push %rbx + mov $0x02, %rax + mov %rdx, %rbx + lea sgx_async_exit(%rip), %rcx +sgx_async_exit: + ENCLU + pop %rbx + ret
tools/testing/selftests/x86/sgx/sgx_uapi.h new file +100 -0
100 lines not on this page · raw message
tools/testing/selftests/x86/sgx/sgxsign.c new file +503 -0
503 lines not on this page · raw message
tools/testing/selftests/x86/sgx/signing_key.pem new file +39 -0
@@ -0,0 +1,39 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIG4wIBAAKCAYEApalGbq7Q+usM91CPtksu3D+b0Prc8gAFL6grM3mg85A5Bx8V +cfMXPgtrw8EYFwQxDAvzZWwl+9VfOX0ECrFRBkOHcOiG0SnADN8+FLj1UiNU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9rsiihbdZkccMvnPfO9334XMxl3HtBRzLstjUlbLB7Sdh+7tZ3JQidCOFNs5pE +XyWwnASPu4tKfDahH1UUTp1uJcq/6716CSWg080avYxFcn75qqsb +-----END RSA PRIVATE KEY-----
Signature · 1 line
2.19.1
Quoted 8 lines · Jarkko Sakkinen
tools/testing/selftests/x86/sgx/sgx-selftest.c new file +8
-0+int main (int argc,char **argv) +{ +unsigned long bin_size = (unsigned long )&encl_bin_end - + (unsigned long )&encl_bin; +struct sgx_secs secs; +uint64_t result =0 ; + +if (!encl_load (&secs, bin_size))
tools/testing/selftests/x86/sgx/sgx-selftest.c line 146
Quoted 10 lines
tools/testing/selftests/x86/sgx/sgx-selftest.c new file +10
-0+exit (1 ); + +sgx_call ((void *)&MAGIC , &result, (void *)secs.base); +if (result !=MAGIC ) { +fprintf (stderr,"0x%lx != 0x%lx\n" , result,MAGIC ); +exit (1 ); + } + +exit (0 ); +}
Well, I guess 100 lines of code for something a wee bit shy of hello world isn't bad. :)
In general, this looks fine, but probably needs some better commenting and probably some messages that make it a bit more clear what is going on.
It would be _nice_, for instance to try to do some CPUID detection of SGX so that the error (or success?) message can tell you whether you're missing hardware support or kernel support.
Thanks for doing this, though. It's sorely needed.
Quoted 23 lines · Dave Hansen
Quoted 8 lines · Jarkko Sakkinen
tools/testing/selftests/x86/sgx/sgx-selftest.c new file +8
-0+int main (int argc,char **argv) +{ +unsigned long bin_size = (unsigned long )&encl_bin_end - + (unsigned long )&encl_bin; +struct sgx_secs secs; +uint64_t result =0 ; + +if (!encl_load (&secs, bin_size))tools/testing/selftests/x86/sgx/sgx-selftest.c line 146
Quoted 10 lines
tools/testing/selftests/x86/sgx/sgx-selftest.c new file +10
-0+exit (1 ); + +sgx_call ((void *)&MAGIC , &result, (void *)secs.base); +if (result !=MAGIC ) { +fprintf (stderr,"0x%lx != 0x%lx\n" , result,MAGIC ); +exit (1 ); + } + +exit (0 ); +}Well, I guess 100 lines of code for something a wee bit shy of hello world isn't bad. :)
Quoted 8 lines
In general, this looks fine, but probably needs some better commenting and probably some messages that make it a bit more clear what is going on.
It would be _nice_, for instance to try to do some CPUID detection of SGX so that the error (or success?) message can tell you whether you're missing hardware support or kernel support.
Thanks for doing this, though. It's sorely needed.
Great, thanks, I see what I can do.
/Jarkko
tools/testing/selftests/x86/sgx/encl.lds line 33
Quoted 11 lines · Jarkko Sakkinen
Add a selftest for SGX. It is a trivial test where a simple enclave copies one 64-bit word of memory between two memory locations given to the enclave as arguments.
Signed-off-by Jarkko Sakkinen <jarkko.sakkinen at linux.intel.com>---
tools/testing/selftests/x86/sgx/encl.lds new file +5
-0+SUBDIRS_64 := sgx +ASSERT(!DEFINED(.altinstructions), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.altinstr_replacement), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.retpoline_safe), "RETPOLINE ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.nospec), "RETPOLINE ALTERNATIVES are not supported in the SGX LE")
Maybe this?
s/LE/Test Enclave
Quoted 11 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +5
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ +/* + * Copyright(c) 2016-18 Intel Corporation. + */ +
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 15
Quoted 10 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +10
-0+ .macro ENCLU + .byte 0x0f, 0x01, 0xd7 + .endm + + .section ".tcs", "a" + .balign 4096 + + .fill 1, 8, 0 # STATE (set by CPU) + .fill 1, 8, 0 # FLAGS + .long encl_ssa # OSSA
Any reason not to do .quad for OSSA and OENTRY?
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 26
Quoted 11 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +11
-0+ .fill 1, 4, 0 + .fill 1, 4, 0 # CSSA (set by CPU) + .fill 1, 4, 1 # NSSA + .long encl_entry # OENTRY + .fill 1, 4, 0 + .fill 1, 8, 0 # AEP (set by EENTER and ERESUME) + .fill 1, 8, 0 # OFSBASE + .fill 1, 8, 0 # OGSBASE + .fill 1, 4, 0xFFFFFFFF # FSLIMIT + .fill 1, 4, 0xFFFFFFFF # GSLIMIT + .fill 503, 8, 0 # Reserved
I'd prefer to do 1-byte fill with a size of 4024 to match the SDM.
Quoted 32 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +32
-0+ + .text + +encl_entry: + # %rbx contains the base address for TCS, which is also the first + # address inside the enclave. By adding $le_stack_end to it, we get the + # absolute address for the stack. + lea (encl_stack)(%rbx), %rax + xchg %rsp, %rax + push %rax + + push %rcx # push the address after EENTER + push %rbx # push the enclave base address + + call encl_body + + pop %rbx # pop the enclave base address + + # Restore XSAVE registers to a synthetic state. + mov $0xFFFFFFFF, %rax + mov $0xFFFFFFFF, %rdx + lea (xsave_area)(%rbx), %rdi + fxrstor (%rdi) + + # Clear GPRs + xor %rcx, %rcx + xor %rdx, %rdx + xor %rdi, %rdi + xor %rsi, %rsi + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 68
Quoted 10 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +10
-0+ xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + + # Reset status flags + add %rdx, %rdx # OF = SF = AF = CF = 0; ZF = PF = 1 + + pop %rbx # pop the address after EENTER
Probably worth expanding the comment to explain that ENCLU[EEXIT] takes the target address via %rbx, i.e. we're "returning" from the EENTER "call".
Quoted 26 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +26
-0+ + # Restore the caller stack. + pop %rax + mov %rax, %rsp + + # EEXIT + mov $4, %rax + enclu + + .section ".data", "aw" + +encl_ssa: + .space 4096 + +xsave_area: + .fill 1, 4, 0x037F # FCW + .fill 5, 4, 0 + .fill 1, 4, 0x1F80 # MXCSR + .fill 1, 4, 0xFFFF # MXCSR_MASK + .fill 123, 4, 0 + .fill 1, 4, 0x80000000 # XCOMP_BV[63] = 1, compaction mode + .fill 12, 4, 0 + + .balign 4096 + .space 8192 +encl_stack:
Quoted 15 lines · Sean Christopherson
tools/testing/selftests/x86/sgx/encl.lds line 33
Quoted 11 lines · Jarkko Sakkinen
Add a selftest for SGX. It is a trivial test where a simple enclave copies one 64-bit word of memory between two memory locations given to the enclave as arguments.
Signed-off-by Jarkko Sakkinen <jarkko.sakkinen at linux.intel.com>---
tools/testing/selftests/x86/sgx/encl.lds new file +5
-0+SUBDIRS_64 := sgx +ASSERT(!DEFINED(.altinstructions), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.altinstr_replacement), "ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.retpoline_safe), "RETPOLINE ALTERNATIVES are not supported in the SGX LE") +ASSERT(!DEFINED(.discard.nospec), "RETPOLINE ALTERNATIVES are not supported in the SGX LE")Maybe this?
Quoted 1 line
s/LE/Test Enclave
Sure.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +9
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ +/* + * Copyright(c) 2016-18 Intel Corporation. + */ + + .macro ENCLU + .byte 0x0f, 0x01, 0xd7 + .endm +
Quoted 8 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 15
Quoted 6 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +6
-0+ .section ".tcs", "a" + .balign 4096 + + .fill 1, 8, 0 # STATE (set by CPU) + .fill 1, 8, 0 # FLAGS + .long encl_ssa # OSSAAny reason not to do .quad for OSSA and OENTRY?
Probably not.
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +5
-0+ .fill 1, 4, 0 + .fill 1, 4, 0 # CSSA (set by CPU) + .fill 1, 4, 1 # NSSA + .long encl_entry # OENTRY + .fill 1, 4, 0
Quoted 8 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 26
Quoted 6 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +6
-0+ .fill 1, 8, 0 # AEP (set by EENTER and ERESUME) + .fill 1, 8, 0 # OFSBASE + .fill 1, 8, 0 # OGSBASE + .fill 1, 4, 0xFFFFFFFF # FSLIMIT + .fill 1, 4, 0xFFFFFFFF # GSLIMIT + .fill 503, 8, 0 # ReservedI'd prefer to do 1-byte fill with a size of 4024 to match the SDM.
Sure.
Quoted 37 lines
Quoted 37 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +37
-0+ + .text + +encl_entry: + # %rbx contains the base address for TCS, which is also the first + # address inside the enclave. By adding $le_stack_end to it, we get the + # absolute address for the stack. + lea (encl_stack)(%rbx), %rax + xchg %rsp, %rax + push %rax + + push %rcx # push the address after EENTER + push %rbx # push the enclave base address + + call encl_body + + pop %rbx # pop the enclave base address + + # Restore XSAVE registers to a synthetic state. + mov $0xFFFFFFFF, %rax + mov $0xFFFFFFFF, %rdx + lea (xsave_area)(%rbx), %rdi + fxrstor (%rdi) + + # Clear GPRs + xor %rcx, %rcx + xor %rdx, %rdx + xor %rdi, %rdi + xor %rsi, %rsi + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15
Quoted 8 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S line 68
Quoted 5 lines
tools/testing/selftests/x86/sgx/encl_bootstrap.S new file +5
-0+ + # Reset status flags + add %rdx, %rdx # OF = SF = AF = CF = 0; ZF = PF = 1 + + pop %rbx # pop the address after EENTERProbably worth expanding the comment to explain that ENCLU[EEXIT] takes the target address via %rbx, i.e. we're "returning" from the EENTER "call".
Thank you for the feedback. I'll do all of these updates.
/Jarkko