Intel(R) SGX Driver
Subsystems (5)
v4 cover Intel(R) SGX Driver
Intel(R) SGX is a set of CPU instructions that can be used by applications to set aside private regions of code and data. The code outside the enclave is disallowed to access the memory inside the enclave by the CPU access control. In a way you can think that SGX provides inverted sandbox. It protects the application from a malicious host.
There is a new hardware unit in the processor called Memory Encryption Engine (MEE) starting from the Skylake microachitecture. BIOS can define one or many MEE regions that can hold enclave data by configuring them with PRMRR registers.
The MEE automatically encrypts the data leaving the processor package to the MEE regions. The data is encrypted using a random key whose life-time is exactly one power cycle.
You can tell if your CPU supports SGX by looking into ``/proc/cpuinfo``:
``cat /proc/cpuinfo | grep ' sgx '``
The GIT repositoy for SGX driver resides in
https://github.com/jsakkine-intel/linux-sgx.git
'le' branch contains the upstream candidate patches.
'master' branch contains the same patches with the following differences:
* top-level patch modifies the ioctl API to be SDK compatible
* does not use flexible launch control but instead relies on SDK provided Intel launch enclave.
'master' is just maintained to allow people to test the code.
v4:
* Tied life-cycle of the sgx_le_proxy process to /dev/sgx.
* Removed __exit annotation from sgx_drv_subsys_exit().
* Fixed a leak of a backing page in sgx_process_add_page_req() in the case when vm_insert_pfn() fails.
* Removed unused symbol exports for sgx_page_cache.c.
* Updated sgx_alloc_page() to require encl parameter and documented the behavior(Sean Christopherson).
* Refactored a more lean API for sgx_encl_find() and documented the behavior.
* Moved #PF handler to sgx_fault.c.
* Replaced subsys_system_register() with plain bus_register().
* Retry EINIT 2nd time only if MSRs are not locked.
v3:
* Check that FEATURE_CONTROL_LOCKED and FEATURE_CONTROL_SGX_ENABLE are set.
* Return -ERESTARTSYS in __sgx_encl_add_page() when sgx_alloc_page() fails.
* Use unused bits in epc_page->pa to store the bank number.
* Removed #ifdef for WQ_NONREENTRANT.
* If mmu_notifier_register() fails with -EINTR, return -ERESTARTSYS.
* Added --remove-section=.got.plt to objcopy flags in order to prevent a dummy .got.plt, which will cause an inconsistent size for the LE.
* Documented sgx_encl_* functions.
* Added remark about AES implementation used inside the LE.
* Removed redundant sgx_sys_exit() from le/main.c.
* Fixed struct sgx_secinfo alignment from 128 to 64 bytes.
* Validate miscselect in sgx_encl_create().
* Fixed SSA frame size calculation to take the misc region into account.
* Implemented consistent exception handling to __encls() and __encls_ret().
* Implemented a proper device model in order to allow sysfs attributes and in-kernel API.
* Cleaned up various "find enclave" implementations to the unified sgx_encl_find().
* Validate that vm_pgoff is zero.
* Discard backing pages with shmem_truncate_range() after EADD.
* Added missing EEXTEND operations to LE signing and launch.
* Fixed SSA size for GPRS region from 168 to 184 bytes.
* Fixed the checks for TCS flags. Now DBGOPTIN is allowed.
* Check that TCS addresses are in ELRANGE and not just page aligned.
* Require kernel to be compiled with X64_64 and CPU_SUP_INTEL.
* Fixed an incorrect value for SGX_ATTR_DEBUG from 0x01 to 0x02.
v2:
* get_rand_uint32() changed the value of the pointer instead of value where it is pointing at.
* Launch enclave incorrectly used sigstruct attributes-field instead of enclave attributes-field.
* Removed unused struct sgx_add_page_req from sgx_ioctl.c
* Removed unused sgx_has_sgx2.
* Updated arch/x86/include/asm/sgx.h so that it provides stub implementations when sgx in not enabled.
* Removed cruft rdmsr-calls from sgx_set_pubkeyhash_msrs().
* return -ENOMEM in sgx_alloc_page() when VA pages consume too much space
* removed unused global sgx_nr_pids
* moved sgx_encl_release to sgx_encl.c
* return -ERESTARTSYS instead of -EINTR in sgx_encl_init()
Haim Cohen (1):
x86: add SGX MSRs to msr-index.h
Jarkko Sakkinen (9):
intel_sgx: updated MAINTAINERS
x86: define the feature control MSR's SGX launch control bit fs/pipe.c: export create_pipe_files() and replace_fd()
intel_sgx: driver for Intel Software Guard Extensions
intel_sgx: ptrace() support
intel_sgx: driver documentation
intel_sgx: in-kernel launch enclave
intel_sgx: glue code for in-kernel LE
intel_sgx: update IA32_SGXLEPUBKEYHASH* MSRs
Kai Huang (1):
x86: add SGX definition to cpufeature
Sean Christopherson (1):
x86: define the feature control MSR's SGX enable bit
Documentation/index.rst | 1 + Documentation/x86/intel_sgx.rst | 131 +++ MAINTAINERS | 5 + arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/msr-index.h | 8 + arch/x86/include/asm/sgx.h | 233 +++++ arch/x86/include/asm/sgx_arch.h | 268 ++++++ arch/x86/include/uapi/asm/sgx.h | 147 +++ drivers/platform/x86/Kconfig | 2 + drivers/platform/x86/Makefile | 1 + drivers/platform/x86/intel_sgx/Kconfig | 34 + drivers/platform/x86/intel_sgx/Makefile | 32 + drivers/platform/x86/intel_sgx/le/Makefile | 26 + drivers/platform/x86/intel_sgx/le/enclave/Makefile | 46 + .../x86/intel_sgx/le/enclave/aes_encrypt.c | 191 ++++ .../platform/x86/intel_sgx/le/enclave/cmac_mode.c | 254 +++++ .../x86/intel_sgx/le/enclave/encl_bootstrap.S | 163 ++++ .../intel_sgx/le/enclave/include/tinycrypt/aes.h | 133 +++ .../le/enclave/include/tinycrypt/cmac_mode.h | 194 ++++ .../le/enclave/include/tinycrypt/constants.h | 59 ++ .../intel_sgx/le/enclave/include/tinycrypt/utils.h | 95 ++ drivers/platform/x86/intel_sgx/le/enclave/main.c | 203 ++++ .../platform/x86/intel_sgx/le/enclave/sgx_le.lds | 28 + .../platform/x86/intel_sgx/le/enclave/sgxsign.c | 537 +++++++++++ drivers/platform/x86/intel_sgx/le/enclave/utils.c | 78 ++ drivers/platform/x86/intel_sgx/le/entry.S | 117 +++ .../platform/x86/intel_sgx/le/include/sgx_asm.h | 64 ++ .../platform/x86/intel_sgx/le/include/sgx_encl.h | 105 ++ drivers/platform/x86/intel_sgx/le/main.c | 219 +++++ drivers/platform/x86/intel_sgx/le/sgx_le_piggy.S | 15 + drivers/platform/x86/intel_sgx/sgx.h | 271 ++++++ drivers/platform/x86/intel_sgx/sgx_encl.c | 1007 ++++++++++++++++++++ drivers/platform/x86/intel_sgx/sgx_ioctl.c | 282 ++++++ drivers/platform/x86/intel_sgx/sgx_le.c | 313 ++++++ .../platform/x86/intel_sgx/sgx_le_proxy_piggy.S | 15 + drivers/platform/x86/intel_sgx/sgx_main.c | 506 ++++++++++ drivers/platform/x86/intel_sgx/sgx_page_cache.c | 568 +++++++++++ drivers/platform/x86/intel_sgx/sgx_util.c | 397 ++++++++ drivers/platform/x86/intel_sgx/sgx_vma.c | 232 +++++ fs/file.c | 1 + fs/pipe.c | 1 + 41 files changed, 6984 insertions(+) create mode 100644 Documentation/x86/intel_sgx.rst create mode 100644 arch/x86/include/asm/sgx.h create mode 100644 arch/x86/include/asm/sgx_arch.h create mode 100644 arch/x86/include/uapi/asm/sgx.h create mode 100644 drivers/platform/x86/intel_sgx/Kconfig create mode 100644 drivers/platform/x86/intel_sgx/Makefile create mode 100644 drivers/platform/x86/intel_sgx/le/Makefile create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/Makefile create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/aes_encrypt.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/cmac_mode.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/encl_bootstrap.S create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/aes.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/cmac_mode.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/constants.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/utils.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/main.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/sgx_le.lds create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/sgxsign.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/utils.c create mode 100644 drivers/platform/x86/intel_sgx/le/entry.S create mode 100644 drivers/platform/x86/intel_sgx/le/include/sgx_asm.h create mode 100644 drivers/platform/x86/intel_sgx/le/include/sgx_encl.h create mode 100644 drivers/platform/x86/intel_sgx/le/main.c create mode 100644 drivers/platform/x86/intel_sgx/le/sgx_le_piggy.S create mode 100644 drivers/platform/x86/intel_sgx/sgx.h create mode 100644 drivers/platform/x86/intel_sgx/sgx_encl.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_ioctl.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_le.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_le_proxy_piggy.S create mode 100644 drivers/platform/x86/intel_sgx/sgx_main.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_page_cache.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_util.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_vma.c
Signature · 1 line
2.14.1
PATCH v4 1/12 intel_sgx: updated MAINTAINERS
--- MAINTAINERS | 5 +++++ 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
@@ -14932,6 +14932,11 @@ L: [email protected] S: Maintained F: mm/zswap.c +INTEL SGX +M: Jarkko Sakkinen <[email protected]> +L: [email protected] +Q: https://patchwork.kernel.org/project/intel-sgx/list/ + THE REST M: Linus Torvalds <[email protected]> L: [email protected]
Signature · 1 line
2.14.1
PATCH v4 2/12 x86: add SGX definition to cpufeature
In reply to Jarkko SakkinenFrom: Kai Huang <[email protected]>
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -219,6 +219,7 @@/* Intel-defined CPU features, CPUID level 0x00000007:0 (ebx), word 9 */ #define X86_FEATURE_FSGSBASE ( 9*32+ 0)/* {RD/WR}{FS/GS}BASE instructions*/ #define X86_FEATURE_TSC_ADJUST ( 9*32+ 1)/* TSC adjustment MSR 0x3b */ +#define X86_FEATURE_SGX ( 9*32+ 2)/* Software Guard Extensions */ #define X86_FEATURE_BMI1 ( 9*32+ 3)/* 1st group bit manipulation extensions */ #define X86_FEATURE_HLE ( 9*32+ 4)/* Hardware Lock Elision */ #define X86_FEATURE_AVX2 ( 9*32+ 5)/* AVX2 instructions */
Signature · 1 line
2.14.1
PATCH v4 3/12 x86: define the feature control MSR's SGX enable bit
In reply to Jarkko SakkinenFrom: Sean Christopherson <[email protected]>
--- arch/x86/include/asm/msr-index.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/msr-index.h +1 -0
@@ -435,6 +435,7 @@#define FEATURE_CONTROL_LOCKED (1<<0)#define FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX (1<<1)#define FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX (1<<2) +#define FEATURE_CONTROL_SGX_ENABLE (1<<18)#define FEATURE_CONTROL_LMCE (1<<20)#define MSR_IA32_APICBASE 0x0000001b
Signature · 1 line
2.14.1
PATCH v4 4/12 x86: define the feature control MSR's SGX launch control bit
In reply to Jarkko Sakkinen--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -298,6 +298,7 @@#define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */ +#define X86_FEATURE_SGX_LC (16*32+30)/* supports SGX launch configuration */ /* AMD-defined CPU features, CPUID level 0x80000007 (ebx), word 17 */ #define X86_FEATURE_OVERFLOW_RECOV (17*32+0)/* MCA overflow recovery support */
Signature · 1 line
2.14.1
PATCH v4 5/12 x86: add SGX MSRs to msr-index.h
In reply to Jarkko SakkinenFrom: Haim Cohen <[email protected]>
These MSRs hold the SHA256 checksum of the currently configured launch enclave public key.
--- arch/x86/include/asm/msr-index.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/msr-index.h +7 -0
@@ -436,6 +436,7 @@#define FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX (1<<1)#define FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX (1<<2)#define FEATURE_CONTROL_SGX_ENABLE (1<<18) +#define FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE (1<<17)#define FEATURE_CONTROL_LMCE (1<<20)#define MSR_IA32_APICBASE 0x0000001b
@@ -502,6 +503,12 @@#define PACKAGE_THERM_INT_LOW_ENABLE (1 << 1)#define PACKAGE_THERM_INT_PLN_ENABLE (1 << 24) +/* Intel SGX MSRs */ +#define MSR_IA32_SGXLEPUBKEYHASH0 0x0000008C +#define MSR_IA32_SGXLEPUBKEYHASH1 0x0000008D +#define MSR_IA32_SGXLEPUBKEYHASH2 0x0000008E +#define MSR_IA32_SGXLEPUBKEYHASH3 0x0000008F +/* Thermal Thresholds Support */ #define THERM_INT_THRESHOLD0_ENABLE (1 << 15)#define THERM_SHIFT_THRESHOLD0 8
Signature · 1 line
2.14.1
PATCH v4 6/12 fs/pipe.c: export create_pipe_files() and replace_fd()
In reply to Jarkko SakkinenExport create_pipe_files() and replace_fd() so that the SGX driver is able to create stdin and stdout pipes.
--- fs/file.c | 1 + fs/pipe.c | 1 + 2 files changed, 2 insertions(+)
fs/file.c +1 -0
@@ -871,6 +871,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)spin_unlock (&files->file_lock);return err; } +EXPORT_SYMBOL_GPL (replace_fd); SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags) {
fs/pipe.c +1 -0
@@ -784,6 +784,7 @@ int create_pipe_files(struct file **res, int flags)iput (inode);return err; } +EXPORT_SYMBOL_GPL (create_pipe_files);static int __do_pipe_flags (int *fd,struct file **files,int flags) {
Signature · 1 line
2.14.1
Re: [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 2 lines · Jarkko Sakkinen
Export
create_pipe_files()andreplace_fd()so that the SGX driver is able to create stdin and stdout pipes.
Err, no way.
Re: [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Jarkko Sakkinen
Export
create_pipe_files()andreplace_fd()so that the SGX driver is able to create stdin and stdout pipes.Err, no way.
What would be a better alternative?
/Jarkko
Re: [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 8 lines · Jarkko Sakkinen
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Jarkko Sakkinen
Export
create_pipe_files()andreplace_fd()so that the SGX driver is able to create stdin and stdout pipes.Err, no way.
What would be a better alternative?
Don't abuse pipes for your interface.
Re: [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Jarkko Sakkinen
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Jarkko Sakkinen
Export
create_pipe_files()andreplace_fd()so that the SGX driver is able to create stdin and stdout pipes.Err, no way.
What would be a better alternative?
Don't abuse pipes for your interface.
coredump uses pipes in a similar way. Maybe you could elaborate a bit why this is abusing? That could help me to find something more acceptable.
The only reasonable alternative that comes into mind would be netlink socket.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 2 lines · Dave Hansen
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
For communication with the process hosting the launch enclave.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 4 lines · Jarkko Sakkinen
Quoted 2 lines · Dave Hansen
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
For communication with the process hosting the launch enclave.
But, why pipes? Why does the kernel have to be the one setting these up? Why is this communication necessary in the first place?
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Jarkko Sakkinen
Quoted 2 lines · Dave Hansen
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
For communication with the process hosting the launch enclave.
But, why pipes? Why does the kernel have to be the one setting these up? Why is this communication necessary in the first place?
1. Kernel gives a SIGSTRUCT instance to the LE hosting process.
2. LE hosting process gives the SIGSTRUCT to the LE.
3. LE gives EINITTOKEN to the LE hosting process after generating it.
4. LE hosting process gives it back to the kernel.
I do not understand why using pipes for this is such a big crime to implement this. I do have an alternative proposal if it is.
What I can do is to use one struct shmem_file instance and assing it to a file descriptor instead. Kernel and LE hosting process can then use that for communication.
It would simplify the infrastructure so I will vote that anyhow even if using pipes would turn out to be acceptable. And does this solution does not require new exports.
I would still like to hear a better explanation than Christoph gave why using pipes is a crime and why coredump still uses them if it is.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Jarkko Sakkinen
Quoted 2 lines · Dave Hansen
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
For communication with the process hosting the launch enclave.
Quoted 7 lines
Quoted 2 lines
But, why pipes? Why does the kernel have to be the one setting these up? Why is this communication necessary in the first place?
1. Kernel gives a SIGSTRUCT instance to the LE hosting process.
2. LE hosting process gives the SIGSTRUCT to the LE.
3. LE gives EINITTOKEN to the LE hosting process after generating it.
4. LE hosting process gives it back to the kernel.
Let me see if I can turn that into english. Enclaves are all rooted in a chain of trust. To run an enclave, you need to have that enclave blessed by the hardware or blessed by a "launch enclave" (aka. LE). The LE is hosted inside a normal process, just as the enclave we are trying to launch is hosted in a normal process. In order to launch a normal enclave, we talk to the LE which gives us a token that allows us to start a new enclave.
These pipes are the mechanism that we use so that the process starting the new process can talk to the launch enclave.
How's that?
Quoted 2 lines
I do not understand why using pipes for this is such a big crime to implement this. I do have an alternative proposal if it is.
The crime is not writing a good changelog to explain what you are doing and why you need to do it.
Quoted 3 lines
What I can do is to use one struct
shmem_fileinstance and assing it to a file descriptor instead. Kernel and LE hosting process can then use that for communication.
Could you explain a bit about what kind of information needs to go back and forth? Is it just "give me a launch key" followed by "here you go", or is it more complicated than that?
Re: [intel-sgx-kernel-dev] [PATCH v4 06/12] fs/pipe.c: export create_pipe_files() and replace_fd()
Quoted 16 lines · Dave Hansen
Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Jarkko Sakkinen
Quoted 2 lines · Dave Hansen
I've always been curious, and the changelog and thread are curiously oblique on this topic: what the heck does this driver use pipes *for*?
For communication with the process hosting the launch enclave.
Quoted 7 lines
Quoted 2 lines
But, why pipes? Why does the kernel have to be the one setting these up? Why is this communication necessary in the first place?
1. Kernel gives a SIGSTRUCT instance to the LE hosting process.
2. LE hosting process gives the SIGSTRUCT to the LE.
3. LE gives EINITTOKEN to the LE hosting process after generating it.
4. LE hosting process gives it back to the kernel.
Quoted 7 lines
Let me see if I can turn that into english. Enclaves are all rooted in a chain of trust. To run an enclave, you need to have that enclave blessed by the hardware or blessed by a "launch enclave" (aka. LE). The LE is hosted inside a normal process, just as the enclave we are trying to launch is hosted in a normal process. In order to launch a normal enclave, we talk to the LE which gives us a token that allows us to start a new enclave.
In a kworker turned in-the ring-3 by the user mode helper framework.
Quoted 4 lines
These pipes are the mechanism that we use so that the process starting the new process can talk to the launch enclave.
How's that?
Client process talks to the kernel with ioctl and passes address and SIGSTRUCT for an enclave it wants to launch. Kernel talks to the LE hosting process with pipes currently.
For architectural enclaves such as LE that are bound to the root key hash you don't need the token. You can pass SGX_ENCLAVE_INIT_ARCH flag to the SGX_ICO_ENCLAVE_INIT if you are launching such an enclave. That is what kernel does for LE. There's no recursive loops :-)
Actually I just realized that we do not need SGX_ENCLAVE_INIT_ARCH. Kernel can deduce this by information inside the SIGSTRUCT and value of the root key hash that it knows. Much more full proof than a flag.
Quoted 5 lines
Quoted 2 lines
I do not understand why using pipes for this is such a big crime to implement this. I do have an alternative proposal if it is.
The crime is not writing a good changelog to explain what you are doing and why you need to do it.
I will refine the changelog. Hard to find the weak spots before you expose the code for review :-) Thank you.
Quoted 8 lines
Quoted 4 lines
What I can do is to use one struct
shmem_fileinstance and assing it to a file descriptor instead. Kernel and LE hosting process can then use that for communication.Could you explain a bit about what kind of information needs to go back and forth? Is it just "give me a launch key" followed by "here you go", or is it more complicated than that?
Input:
1. SHA256 hash of the enclave signer public key modulus
2. SIGSTRUCT containing measurements, signature, modulus of the signer etc.
Output:
3. EINITTOKEN which cryptographic token generated with hardware generated license key (a different for each boot cycle).
I think shmem_file would be a nice alternative, better than using pipes for this use case.
/Jarkko
PATCH v4 7/12 intel_sgx: driver for Intel Software Guard Extensions
In reply to Jarkko SakkinenIntel SGX is a set of CPU instructions that can be used by applications to set aside private regions of code and data. The code outside the enclave is disallowed to access the memory inside the enclave by the CPU access control.
SGX driver provides a ioctl API for loading and initializing enclaves. Address range for enclaves is reserved with mmap() and they are destroyed with munmap(). Enclave construction, measurement and initialization is done with the provided the ioctl API.
The driver implements also a swapper thread ksgxswapd for EPC pages backed by a private shmem file. Currently it has a limitation of not swapping VA pages but there is nothing preventing to implement it later on. Now it was scoped out in order to keep the implementation simple.
The parameter struct for SGX_IOC_ENCLAVE_INIT does not contain a parameter to supply a launch token. Generating and using tokens is best to be kept in the control of the kernel because it has direct binding to the IA32_SGXPUBKEYHASHx MSRs (a core must have MSRs set to the same value as the signer of token).
By giving user space any role in the launch process is a risk for introducing bottlenecks as kernel must exhibit behavior that user space launch daemon depends on, properietary risks (closed launch daemons on closed platforms) and stability risks as there would be division of semantics between user space and kernel.
--- arch/x86/include/asm/sgx.h | 233 ++++++ arch/x86/include/asm/sgx_arch.h | 268 +++++++ arch/x86/include/uapi/asm/sgx.h | 147 ++++ drivers/platform/x86/Kconfig | 2 + drivers/platform/x86/Makefile | 1 + drivers/platform/x86/intel_sgx/Kconfig | 19 + drivers/platform/x86/intel_sgx/Makefile | 13 + drivers/platform/x86/intel_sgx/sgx.h | 252 ++++++ drivers/platform/x86/intel_sgx/sgx_encl.c | 988 ++++++++++++++++++++++++ drivers/platform/x86/intel_sgx/sgx_ioctl.c | 277 +++++++ drivers/platform/x86/intel_sgx/sgx_main.c | 456 +++++++++++ drivers/platform/x86/intel_sgx/sgx_page_cache.c | 568 ++++++++++++++ drivers/platform/x86/intel_sgx/sgx_util.c | 372 +++++++++ drivers/platform/x86/intel_sgx/sgx_vma.c | 117 +++ 14 files changed, 3713 insertions(+) create mode 100644 arch/x86/include/asm/sgx.h create mode 100644 arch/x86/include/asm/sgx_arch.h create mode 100644 arch/x86/include/uapi/asm/sgx.h create mode 100644 drivers/platform/x86/intel_sgx/Kconfig create mode 100644 drivers/platform/x86/intel_sgx/Makefile create mode 100644 drivers/platform/x86/intel_sgx/sgx.h create mode 100644 drivers/platform/x86/intel_sgx/sgx_encl.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_ioctl.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_main.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_page_cache.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_util.c create mode 100644 drivers/platform/x86/intel_sgx/sgx_vma.c
arch/x86/include/asm/sgx.h new file +233 -0
@@ -0,0 +1,233 @@ +/* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2016-2017 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* Contact Information: +* Jarkko Sakkinen <[email protected]> +* Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo +* +* BSD LICENSE +* +* Copyright(c) 2016-2017 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +* Authors: +* +* Jarkko Sakkinen <[email protected]> +* Suresh Siddha <[email protected]> +*/ + +#ifndef _ASM_X86_SGX_H +#define _ASM_X86_SGX_H + +#include <asm/sgx_arch.h> +#include <asm/asm.h> +#include <linux/bitops.h> +#include <linux/err.h> +#include <linux/types.h> + +#define SGX_CPUID 0x12 + +enum sgx_cpuid { +SGX_CPUID_CAPABILITIES =0 , +SGX_CPUID_ATTRIBUTES =1 , +SGX_CPUID_EPC_BANKS =2 , +}; + +enum sgx_commands { +ECREATE =0x0 , +EADD =0x1 , +EINIT =0x2 , +EREMOVE =0x3 , +EDGBRD =0x4 , +EDGBWR =0x5 , +EEXTEND =0x6 , +ELDU =0x8 , +EBLOCK =0x9 , +EPA =0xA , +EWB =0xB , +ETRACK =0xC , +EAUG =0xD , +EMODPR =0xE , +EMODT =0xF , +}; + +#ifdef CONFIG_X86_64 +#define XAX "%%rax" +#else +#define XAX "%%eax" +#endif + +#define __encls_ret (rax, rbx, rcx, rdx) \ + ({ \ + int ret; \ + asm volatile( \ + "1: .byte 0x0f, 0x01, 0xcf;\n\t" \ + "2:\n" \ + ".section .fixup,\"ax\"\n" \ + "3: mov $-14,"XAX"\n" \ + " jmp 2b\n" \ + ".previous\n" \ + _ASM_EXTABLE(1b, 3b) \ + : "=a"(ret) \ + : "a"(rax), "b"(rbx), "c"(rcx), "d"(rdx) \ + : "memory"); \ + ret; \ + }) + +#define __encls(rax, rbx, rcx, rdx...) \ + ({ \ + int ret; \ + asm volatile( \ + "1: .byte 0x0f, 0x01, 0xcf;\n\t" \ + " xor "XAX","XAX"\n" \ + "2:\n" \ + ".section .fixup,\"ax\"\n" \ + "3: mov $-14,"XAX"\n" \ + " jmp 2b\n" \ + ".previous\n" \ + _ASM_EXTABLE(1b, 3b) \ + : "=a"(ret), "=b"(rbx), "=c"(rcx) \ + : "a"(rax), "b"(rbx), "c"(rcx), rdx \ + : "memory"); \ + ret; \ + }) + +static inline unsigned long __ecreate (struct sgx_pageinfo *pginfo,void *secs) +{ +return __encls (ECREATE , pginfo, secs,"d" (0 )); +} + +static inline int __eextend (void *secs,void *epc) +{ +return __encls (EEXTEND , secs, epc,"d" (0 )); +} + +static inline int __eadd (struct sgx_pageinfo *pginfo,void *epc) +{ +return __encls (EADD , pginfo, epc,"d" (0 )); +} + +static inline int __einit (void *sigstruct,struct sgx_einittoken *einittoken, +void *secs) +{ +return __encls_ret (EINIT , sigstruct, secs, einittoken); +} + +static inline int __eremove (void *epc) +{ +unsigned long rbx =0 ; +unsigned long rdx =0 ; + +return __encls_ret (EREMOVE , rbx, epc, rdx); +} + +static inline int __edbgwr (void *epc,unsigned long *data) +{ +return __encls (EDGBWR , *data, epc,"d" (0 )); +} + +static inline int __edbgrd (void *epc,unsigned long *data) +{ +unsigned long rbx =0 ; +int ret; + + ret =__encls (EDGBRD , rbx, epc,"d" (0 )); +if (!ret) + *(unsigned long *) data = rbx; + +return ret; +} + +static inline int __etrack (void *epc) +{ +unsigned long rbx =0 ; +unsigned long rdx =0 ; + +return __encls_ret (ETRACK , rbx, epc, rdx); +} + +static inline int __eldu (unsigned long rbx,unsigned long rcx, +unsigned long rdx) +{ +return __encls_ret (ELDU , rbx, rcx, rdx); +} + +static inline int __eblock (unsigned long rcx) +{ +unsigned long rbx =0 ; +unsigned long rdx =0 ; + +return __encls_ret (EBLOCK , rbx, rcx, rdx); +} + +static inline int __epa (void *epc) +{ +unsigned long rbx =SGX_PAGE_TYPE_VA ; + +return __encls (EPA , rbx, epc,"d" (0 )); +} + +static inline int __ewb (struct sgx_pageinfo *pginfo,void *epc,void *va) +{ +return __encls_ret (EWB , pginfo, epc, va); +} + +static inline int __eaug (struct sgx_pageinfo *pginfo,void *epc) +{ +return __encls (EAUG , pginfo, epc,"d" (0 )); +} + +static inline int __emodpr (struct sgx_secinfo *secinfo,void *epc) +{ +unsigned long rdx =0 ; + +return __encls_ret (EMODPR , secinfo, epc, rdx); +} + +static inline int __emodt (struct sgx_secinfo *secinfo,void *epc) +{ +unsigned long rdx =0 ; + +return __encls_ret (EMODT , secinfo, epc, rdx); +} + +#endif /* _ASM_X86_SGX_H */
arch/x86/include/asm/sgx_arch.h new file +268 -0
268 lines not on this page · raw message
arch/x86/include/uapi/asm/sgx.h new file +147 -0
147 lines not on this page · raw message
drivers/platform/x86/Kconfig +2 -0
@@ -1139,6 +1139,8 @@ config SILEAD_DMI with the OS-image for the device. This option supplies the missing information. Enable this for x86 tablets with Silead touchscreens. +source "drivers/platform/x86/intel_sgx/Kconfig" + endif # X86_PLATFORM_DEVICES config PMC_ATOM
drivers/platform/x86/Makefile +1 -0
@@ -83,3 +83,4 @@ obj-$(CONFIG_PMC_ATOM) += pmc_atom.o obj-$(CONFIG_MLX_PLATFORM) += mlx-platform.o obj-$(CONFIG_MLX_CPLD_PLATFORM) += mlxcpld-hotplug.o obj-$(CONFIG_INTEL_TURBO_MAX_3) += intel_turbo_max_3.o +obj-$(CONFIG_INTEL_SGX) += intel_sgx/
drivers/platform/x86/intel_sgx/Kconfig new file +19 -0
@@ -0,0 +1,19 @@ +# +# Intel SGX +# + +config INTEL_SGX + tristate "Intel(R) SGX Driver" + default n + depends on X86_64 && CPU_SUP_INTEL + select MMU_NOTIFIER + ---help--- + Intel(R) SGX is a set of CPU instructions that can be used by + applications to set aside private regions of code and data. The code + outside the enclave is disallowed to access the memory inside the + enclave by the CPU access control. + + The firmware uses PRMRR registers to reserve an area of physical memory + called Enclave Page Cache (EPC). There is a hardware unit in the + processor called Memory Encryption Engine. The MEE encrypts and decrypts + the EPC pages as they enter and leave the processor package.
drivers/platform/x86/intel_sgx/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# +# Intel SGX +# + +obj-$(CONFIG_INTEL_SGX) += intel_sgx.o + +intel_sgx-$(CONFIG_INTEL_SGX) += \ + sgx_ioctl.o \ + sgx_encl.o \ + sgx_main.o \ + sgx_page_cache.o \ + sgx_util.o \ + sgx_vma.o \
drivers/platform/x86/intel_sgx/sgx.h new file +252 -0
252 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_encl.c new file +988 -0
988 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_ioctl.c new file +277 -0
277 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_main.c new file +456 -0
456 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_page_cache.c new file +568 -0
335 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_util.c new file +372 -0
372 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_vma.c new file +117 -0
117 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.14.1
Part of the diff is not shown here. View the raw message.
Re: [PATCH v4 07/12] intel_sgx: driver for Intel Software Guard Extensions
drivers/platform/x86/intel_sgx/sgx_encl.c line 580
Quoted 13 lines · Jarkko Sakkinen
drivers/platform/x86/intel_sgx/sgx_encl.c new file +13
-0+int sgx_encl_create (struct sgx_secs *secs) +{ +struct sgx_pageinfo pginfo; +struct sgx_secinfo secinfo; +struct sgx_encl *encl; +struct sgx_epc_page *secs_epc; +struct vm_area_struct *vma; +void *secs_vaddr; +long ret; + + encl =sgx_encl_alloc (secs); +if (IS_ERR (secs)) +return PTR_ERR (encl);
if (IS_ERR(encl))
/Jarkko
PATCH v4 8/12 intel_sgx: ptrace() support
In reply to Jarkko SakkinenImplemented VMA callbacks in order to ptrace() debug enclaves.
--- drivers/platform/x86/intel_sgx/sgx_vma.c | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)
drivers/platform/x86/intel_sgx/sgx_vma.c +115 -0
@@ -110,8 +110,123 @@ static int sgx_vma_fault(struct vm_fault *vmf)return VM_FAULT_SIGBUS ; } +static inline int sgx_vma_access_word (struct sgx_encl *encl, +unsigned long addr, +void *buf, +int len, +int write, +struct sgx_encl_page *encl_page, +int i) +{ +char data[sizeof (unsigned long )]; +int align, cnt, offset; +void *vaddr; +int ret; + + offset = ((addr + i) & (PAGE_SIZE -1 )) & ~(sizeof (unsigned long ) -1 ); + align = (addr + i) & (sizeof (unsigned long ) -1 ); + cnt =sizeof (unsigned long ) - align; + cnt =min (cnt, len - i); + +if (write) { +if (encl_page->flags &SGX_ENCL_PAGE_TCS && + (offset <8 || (offset + (len - i)) >16 )) +return -ECANCELED ; + +if (align || (cnt !=sizeof (unsigned long ))) { + vaddr =sgx_get_page (encl_page->epc_page); + ret =__edbgrd ((void *)((unsigned long )vaddr + offset), + (unsigned long *)data); +sgx_put_page (vaddr); +if (ret) { +sgx_dbg (encl,"EDBGRD returned %d\n" , ret); +return -EFAULT ; + } + } + +memcpy (data + align, buf + i, cnt); + vaddr =sgx_get_page (encl_page->epc_page); + ret =__edbgwr ((void *)((unsigned long )vaddr + offset), + (unsigned long *)data); +sgx_put_page (vaddr); +if (ret) { +sgx_dbg (encl,"EDBGWR returned %d\n" , ret); +return -EFAULT ; + } + }else { +if (encl_page->flags &SGX_ENCL_PAGE_TCS && + (offset + (len - i)) >72 ) +return -ECANCELED ; + + vaddr =sgx_get_page (encl_page->epc_page); + ret =__edbgrd ((void *)((unsigned long )vaddr + offset), + (unsigned long *)data); +sgx_put_page (vaddr); +if (ret) { +sgx_dbg (encl,"EDBGRD returned %d\n" , ret); +return -EFAULT ; + } + +memcpy (buf + i, data + align, cnt); + } + +return cnt; +} + +static int sgx_vma_access (struct vm_area_struct *vma,unsigned long addr, +void *buf,int len,int write) +{ +struct sgx_encl *encl = vma->vm_private_data; +struct sgx_encl_page *entry =NULL ; +const char *op_str = write ?"EDBGWR" :"EDBGRD" ; +int ret =0 ; +int i; + +/* If process was forked, VMA is still there but vm_private_data is set +* to NULL. +*/ +if (!encl) +return -EFAULT ; + +if (!(encl->flags &SGX_ENCL_DEBUG ) || + !(encl->flags &SGX_ENCL_INITIALIZED ) || + (encl->flags &SGX_ENCL_DEAD )) +return -EFAULT ; + +sgx_dbg (encl,"%s addr=0x%lx, len=%d\n" , op_str, addr, len); + +for (i =0 ; i < len; i += ret) { +if (!entry || !((addr + i) & (PAGE_SIZE -1 ))) { +if (entry) + entry->flags &= ~SGX_ENCL_PAGE_RESERVED ; + + entry =sgx_fault_page (vma, (addr + i) &PAGE_MASK , +SGX_FAULT_RESERVE ); +if (IS_ERR (entry)) { + ret =PTR_ERR (entry); + entry =NULL ; +break ; + } + } + +/* No locks are needed because used fields are immutable after +* intialization. +*/ + ret =sgx_vma_access_word (encl, addr, buf, len, write, + entry, i); +if (ret <0 ) +break ; + } + +if (entry) + entry->flags &= ~SGX_ENCL_PAGE_RESERVED ; + +return (ret <0 && ret != -ECANCELED ) ? ret : i; +} +const struct vm_operations_struct sgx_vm_ops = { .close = sgx_vma_close, .open = sgx_vma_open, .fault = sgx_vma_fault, + .access = sgx_vma_access, };
Signature · 1 line
2.14.1
PATCH v4 9/12 intel_sgx: driver documentation
In reply to Jarkko Sakkinen--- Documentation/index.rst | 1 + Documentation/x86/intel_sgx.rst | 131 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 Documentation/x86/intel_sgx.rst
Documentation/index.rst +1 -0
@@ -86,6 +86,7 @@ implementation. :maxdepth: 2 sh/index + x86/index Korean translations -------------------
Documentation/x86/intel_sgx.rst new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.14.1
Randy Dunlap
reviewerRe: [PATCH v4 09/12] intel_sgx: driver documentation
Quoted 18 lines · Jarkko Sakkinen
Signed-off-by Jarkko Sakkinen <[email protected]>--- Documentation/index.rst | 1 + Documentation/x86/intel_sgx.rst | 131 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 Documentation/x86/intel_sgx.rstDocumentation/x86/intel_sgx.rst new file +5
-0@@ -0,0 +1,131 @@ +=================== +Intel(R) SGX driver +=================== + +Introduction
Documentation/x86/intel_sgx.rst line 15
Quoted 10 lines
Documentation/x86/intel_sgx.rst new file +10
-0+============ + +Intel(R) SGX is a set of CPU instructions that can be used by applications to +set aside private regions of code and data. The code outside the enclave is +disallowed to access the memory inside the enclave by the CPU access control. +In a way you can think that SGX provides inverted sandbox. It protects the +application from a malicious host. + +There is a new hardware unit in the processor called Memory Encryption Engine +(MEE) starting from the Skylake microachitecture. BIOS can define one or many
microarchitecture.
Documentation/x86/intel_sgx.rst line 24
Quoted 9 lines
Documentation/x86/intel_sgx.rst new file +9
-0+MEE regions that can hold enclave data by configuring them with PRMRR registers. + +The MEE automatically encrypts the data leaving the processor package to the MEE +regions. The data is encrypted using a random key whose life-time is exactly one +power cycle. + +You can tell if your CPU supports SGX by looking into ``/proc/cpuinfo``: + + ``cat /proc/cpuinfo | grep ' sgx '``
Are you sure about a space both before and after 'sgx'?
Quoted 30 lines
Documentation/x86/intel_sgx.rst new file +30
-0+ +Enclave data types +================== + +SGX defines new data types to maintain information about the enclaves and their +security properties. + +The following data structures exist in MEE regions: + +* **Enclave Page Cache (EPC):** memory pages for protected code and data +* **Enclave Page Cache Map (EPCM):** meta-data for each EPC page + +The Enclave Page Cache holds following types of pages: + +* **SGX Enclave Control Structure (SECS)**: meta-data defining the global + properties of an enclave such as range of addresses it can access. +* **Regular (REG):** containing code and data for the enclave. +* **Thread Control Structure (TCS):** defines an entry point for a hardware + thread to enter into the enclave. The enclave can only be entered through + these entry points. +* **Version Array (VA)**: an EPC page receives a unique 8 byte version number + when it is swapped, which is then stored into a VA page. A VA page can hold up + to 512 version numbers. + +Launch control +============== + +For launching an enclave, two structures must be provided for ENCLS(EINIT): + +1. **SIGSTRUCT:** a signed measurement of the enclave binary.
Documentation/x86/intel_sgx.rst line 64
Quoted 10 lines
Documentation/x86/intel_sgx.rst new file +10
-0+2. **EINITTOKEN:** the measurement, the public key of the signer and various + enclave attributes. This structure contains a MAC of its contents using + hardware derived symmetric key called *launch key*. + +The hardware platform contains a root key pair for signing the SIGTRUCT +for a *launch enclave* that is able to acquire the *launch key* for +creating EINITTOKEN's for other enclaves. For the launch enclave +EINITTOKEN is not needed because it is signed with the private root key. + +There are two feature control bits associate with launch control
associated control:
Quoted 52 lines
Documentation/x86/intel_sgx.rst new file +52
-0+ +* **IA32_FEATURE_CONTROL[0]**: locks down the feature control register +* **IA32_FEATURE_CONTROL[17]**: allow runtime reconfiguration of + IA32_SGXLEPUBKEYHASHn MSRs that define MRSIGNER hash for the launch + enclave. Essentially they define a signing key that does not require + EINITTOKEN to be let run. + +The BIOS can configure IA32_SGXLEPUBKEYHASHn MSRs before feature control +register is locked. + +It could be tempting to implement launch control by writing the MSRs +every time when an enclave is launched. This does not scale because for +generic case because BIOS might lock down the MSRs before handover to +the OS. + +Debug enclaves +-------------- + +Enclave can be set as a *debug enclave* of which memory can be read or written +by using the ENCLS(EDBGRD) and ENCLS(EDBGWR) opcodes. The Intel provided launch +enclave provides them always a valid EINITTOKEN and therefore they are a low +hanging fruit way to try out SGX. + +Virtualization +============== + +Launch control +-------------- + +The values for IA32_SGXLEPUBKEYHASHn MSRs cannot be emulated for a virtual +machine guest. It would easily seem feasible to hold virtual values for these +MSRs, trap ENCLS(EINIT) and use the host LE to generate a token when a guest LE +is initialized. + +However, looking at the pseudo code of ENCLS(EINIT) from the SDM there is a +constraint that the instruction will fail if ATTRIBUTES.EINITTOKENKEY is set +(the documentation does not tell the reason why the constraint exists but it +exists). + +Thus, only when the MSRs are left unlocked before handover to the OS the +setting of these MSRs can be supported for VM guests. + +Suspend and resume +------------------ + +If the host suspends and resumes, the enclave memory for the VM guest could +become invalid. This can make ENCLS leaf operations suddenly fail. + +The driver has a graceful fallback mechanism to manage this situation. If any of +the ENCLS leaf operations fail, the driver will fallback by kicking threads out +of the enclave, removing the TCS entries and marking enclave as invalid. After +this no new pages can be allocated for the enclave and no entry can be done.
Signature · 1 line
~Randy
Re: [PATCH v4 09/12] intel_sgx: driver documentation
Quoted 32 lines · Randy Dunlap
Quoted 18 lines · Jarkko Sakkinen
Signed-off-by Jarkko Sakkinen <[email protected]>--- Documentation/index.rst | 1 + Documentation/x86/intel_sgx.rst | 131 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 Documentation/x86/intel_sgx.rstDocumentation/x86/intel_sgx.rst new file +5
-0@@ -0,0 +1,131 @@ +=================== +Intel(R) SGX driver +=================== + +IntroductionDocumentation/x86/intel_sgx.rst line 15
Quoted 10 lines
Documentation/x86/intel_sgx.rst new file +10
-0+============ + +Intel(R) SGX is a set of CPU instructions that can be used by applications to +set aside private regions of code and data. The code outside the enclave is +disallowed to access the memory inside the enclave by the CPU access control. +In a way you can think that SGX provides inverted sandbox. It protects the +application from a malicious host. + +There is a new hardware unit in the processor called Memory Encryption Engine +(MEE) starting from the Skylake microachitecture. BIOS can define one or manymicroarchitecture.
Quoted 11 lines
Documentation/x86/intel_sgx.rst line 24
Quoted 9 lines
Documentation/x86/intel_sgx.rst new file +9
-0+MEE regions that can hold enclave data by configuring them with PRMRR registers. + +The MEE automatically encrypts the data leaving the processor package to the MEE +regions. The data is encrypted using a random key whose life-time is exactly one +power cycle. + +You can tell if your CPU supports SGX by looking into ``/proc/cpuinfo``: + + ``cat /proc/cpuinfo | grep ' sgx '``Are you sure about a space both before and after 'sgx'?
Only before would make sense.
Quoted 35 lines
Quoted 35 lines
Documentation/x86/intel_sgx.rst new file +34
-0+ +Enclave data types +================== + +SGX defines new data types to maintain information about the enclaves and their +security properties. + +The following data structures exist in MEE regions: + +* **Enclave Page Cache (EPC):** memory pages for protected code and data +* **Enclave Page Cache Map (EPCM):** meta-data for each EPC page + +The Enclave Page Cache holds following types of pages: + +* **SGX Enclave Control Structure (SECS)**: meta-data defining the global + properties of an enclave such as range of addresses it can access. +* **Regular (REG):** containing code and data for the enclave. +* **Thread Control Structure (TCS):** defines an entry point for a hardware + thread to enter into the enclave. The enclave can only be entered through + these entry points. +* **Version Array (VA)**: an EPC page receives a unique 8 byte version number + when it is swapped, which is then stored into a VA page. A VA page can hold up + to 512 version numbers. + +Launch control +============== + +For launching an enclave, two structures must be provided for ENCLS(EINIT): + +1. **SIGSTRUCT:** a signed measurement of the enclave binary. +2. **EINITTOKEN:** the measurement, the public key of the signer and various + enclave attributes. This structure contains a MAC of its contents using + hardware derived symmetric key called *launch key*. +
Quoted 8 lines
Documentation/x86/intel_sgx.rst line 64
Quoted 6 lines
Documentation/x86/intel_sgx.rst new file +6
-0+The hardware platform contains a root key pair for signing the SIGTRUCT +for a *launch enclave* that is able to acquire the *launch key* for +creating EINITTOKEN's for other enclaves. For the launch enclave +EINITTOKEN is not needed because it is signed with the private root key. + +There are two feature control bits associate with launch controlassociated control:
Agreed.
Quoted 45 lines
Quoted 45 lines
Documentation/x86/intel_sgx.rst new file +44
-0+ +* **IA32_FEATURE_CONTROL[0]**: locks down the feature control register +* **IA32_FEATURE_CONTROL[17]**: allow runtime reconfiguration of + IA32_SGXLEPUBKEYHASHn MSRs that define MRSIGNER hash for the launch + enclave. Essentially they define a signing key that does not require + EINITTOKEN to be let run. + +The BIOS can configure IA32_SGXLEPUBKEYHASHn MSRs before feature control +register is locked. + +It could be tempting to implement launch control by writing the MSRs +every time when an enclave is launched. This does not scale because for +generic case because BIOS might lock down the MSRs before handover to +the OS. + +Debug enclaves +-------------- + +Enclave can be set as a *debug enclave* of which memory can be read or written +by using the ENCLS(EDBGRD) and ENCLS(EDBGWR) opcodes. The Intel provided launch +enclave provides them always a valid EINITTOKEN and therefore they are a low +hanging fruit way to try out SGX. + +Virtualization +============== + +Launch control +-------------- + +The values for IA32_SGXLEPUBKEYHASHn MSRs cannot be emulated for a virtual +machine guest. It would easily seem feasible to hold virtual values for these +MSRs, trap ENCLS(EINIT) and use the host LE to generate a token when a guest LE +is initialized. + +However, looking at the pseudo code of ENCLS(EINIT) from the SDM there is a +constraint that the instruction will fail if ATTRIBUTES.EINITTOKENKEY is set +(the documentation does not tell the reason why the constraint exists but it +exists). + +Thus, only when the MSRs are left unlocked before handover to the OS the +setting of these MSRs can be supported for VM guests. + +Suspend and resume +------------------
Quoted 13 lines
Quoted 8 lines
Documentation/x86/intel_sgx.rst new file +8
-0+ +If the host suspends and resumes, the enclave memory for the VM guest could +become invalid. This can make ENCLS leaf operations suddenly fail. + +The driver has a graceful fallback mechanism to manage this situation. If any of +the ENCLS leaf operations fail, the driver will fallback by kicking threads out +of the enclave, removing the TCS entries and marking enclave as invalid. After +this no new pages can be allocated for the enclave and no entry can be done.Signature · 1 line
~Randy
Thank you for the feedback.
/Jarkko
PATCH v4 10/12 intel_sgx: in-kernel launch enclave
In reply to Jarkko SakkinenThis commits implements the in-kernel launch enclave. It is wrapped into a user space program that reads SIGSTRUCT instances from stdin and outputs launch tokens to stdout.
The commit also adds enclave signing tool that is used by kbuild to measure and sign the lauch enclave.
CONFIG_INTEL_SGX_SIGNING_KEY points to a PEM-file for the 3072-bit RSA key that is used as the LE public key pair. The default location is:
drivers/platform/x86/intel_sgx/intel_sgx_signing_key.pem
If the default key does not exist kbuild will generate a random key and place it to this location. KBUILD_SGX_SIGN_PIN can be used to specify the passphrase for the LE public key.
TinyCrypt (https://github.com/01org/tinycrypt) is used as AES implementation, which is not timing resistant. Eventually this needs to be replaced with AES-NI based implementation that could be either
- re-use existing AES-NI code in the kernel
- have its own hand written code
--- drivers/platform/x86/intel_sgx/Kconfig | 13 + drivers/platform/x86/intel_sgx/Makefile | 18 + drivers/platform/x86/intel_sgx/le/Makefile | 26 + drivers/platform/x86/intel_sgx/le/enclave/Makefile | 46 ++ .../x86/intel_sgx/le/enclave/aes_encrypt.c | 191 ++++++++ .../platform/x86/intel_sgx/le/enclave/cmac_mode.c | 254 ++++++++++ .../x86/intel_sgx/le/enclave/encl_bootstrap.S | 163 +++++++ .../intel_sgx/le/enclave/include/tinycrypt/aes.h | 133 +++++ .../le/enclave/include/tinycrypt/cmac_mode.h | 194 ++++++++ .../le/enclave/include/tinycrypt/constants.h | 59 +++ .../intel_sgx/le/enclave/include/tinycrypt/utils.h | 95 ++++ drivers/platform/x86/intel_sgx/le/enclave/main.c | 203 ++++++++ .../platform/x86/intel_sgx/le/enclave/sgx_le.lds | 28 ++ .../platform/x86/intel_sgx/le/enclave/sgxsign.c | 537 +++++++++++++++++++++ drivers/platform/x86/intel_sgx/le/enclave/utils.c | 78 +++ drivers/platform/x86/intel_sgx/le/entry.S | 117 +++++ .../platform/x86/intel_sgx/le/include/sgx_asm.h | 64 +++ .../platform/x86/intel_sgx/le/include/sgx_encl.h | 105 ++++ drivers/platform/x86/intel_sgx/le/main.c | 219 +++++++++ drivers/platform/x86/intel_sgx/le/sgx_le_piggy.S | 15 + .../platform/x86/intel_sgx/sgx_le_proxy_piggy.S | 11 + 21 files changed, 2569 insertions(+) create mode 100644 drivers/platform/x86/intel_sgx/le/Makefile create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/Makefile create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/aes_encrypt.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/cmac_mode.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/encl_bootstrap.S create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/aes.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/cmac_mode.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/constants.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/utils.h create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/main.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/sgx_le.lds create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/sgxsign.c create mode 100644 drivers/platform/x86/intel_sgx/le/enclave/utils.c create mode 100644 drivers/platform/x86/intel_sgx/le/entry.S create mode 100644 drivers/platform/x86/intel_sgx/le/include/sgx_asm.h create mode 100644 drivers/platform/x86/intel_sgx/le/include/sgx_encl.h create mode 100644 drivers/platform/x86/intel_sgx/le/main.c create mode 100644 drivers/platform/x86/intel_sgx/le/sgx_le_piggy.S create mode 100644 drivers/platform/x86/intel_sgx/sgx_le_proxy_piggy.S
drivers/platform/x86/intel_sgx/Kconfig +13 -0
@@ -2,6 +2,8 @@ # Intel SGX # +menu "Intel SGX" + config INTEL_SGX tristate "Intel(R) SGX Driver" default n
@@ -17,3 +19,14 @@ config INTEL_SGX called Enclave Page Cache (EPC). There is a hardware unit in the processor called Memory Encryption Engine. The MEE encrypts and decrypts the EPC pages as they enter and leave the processor package. + + +config INTEL_SGX_SIGNING_KEY + string "Path to the Intel SGX LE signing key" + default "drivers/platform/x86/intel_sgx/intel_sgx_signing_key.pem" + depends on INTEL_SGX + ---help--- + Provide a path to a 3072-bit RSA private key that will be used to + sign the launch enclave. + +endmenu
drivers/platform/x86/intel_sgx/Makefile +18 -0
@@ -11,3 +11,21 @@ intel_sgx-$(CONFIG_INTEL_SGX) += \ sgx_page_cache.o \ sgx_util.o \ sgx_vma.o \ + sgx_le_proxy_piggy.o + +$(eval $(call config_filename,INTEL_SGX_SIGNING_KEY)) + +INTEL_SGX_SIGNING_KEY_PATH := \ + $(INTEL_SGX_SIGNING_KEY_SRCPREFIX)$(INTEL_SGX_SIGNING_KEY_FILENAME) + +ifeq ($(CONFIG_INTEL_SGX_SIGNING_KEY),"drivers/platform/x86/intel_sgx/intel_sgx_signing_key.pem") +$(INTEL_SGX_SIGNING_KEY_PATH): + $(Q)openssl genrsa -3 -out $(INTEL_SGX_SIGNING_KEY_PATH) 3072 +endif + +$(obj)/sgx_le_proxy_piggy.o: $(INTEL_SGX_SIGNING_KEY_PATH) \ + $(obj)/le/sgx_le_proxy +$(obj)/le/sgx_le_proxy: FORCE + $(Q)$(MAKE) $(build)=$(obj)/le $@ + +export INTEL_SGX_SIGNING_KEY_PATH
drivers/platform/x86/intel_sgx/le/Makefile new file +26 -0
@@ -0,0 +1,26 @@ +KASAN_SANITIZE := n +OBJECT_FILES_NON_STANDARD := y +KCOV_INSTRUMENT := n +KBUILD_CFLAGS := -Wall -Werror -static -nostdlib -nostartfiles -I$(obj)/include +KBUILD_AFLAGS += -I$(obj)/include + +subdir- := enclave + +always := sgx_le_proxy +clean-files := sgx_le_proxy + +# +# sgx_le_proxy +# + +sgx_le_proxy-y += main.o entry.o sgx_le_piggy.o +targets += $(sgx_le_proxy-y) +SGX_LE_PROXY_OBJS = $(addprefix $(obj)/,$(sgx_le_proxy-y)) + +$(obj)/sgx_le_piggy.o: $(obj)/enclave/sgx_le.bin $(obj)/enclave/sgx_le.ss +$(obj)/enclave/sgx_le.bin $(obj)/enclave/sgx_le.ss: FORCE + $(Q)$(MAKE) $(build)=$(obj)/enclave $@ + +targets += sgx_le_proxy +$(obj)/sgx_le_proxy: $(SGX_LE_PROXY_OBJS) + $(call if_changed,ld)
drivers/platform/x86/intel_sgx/le/enclave/Makefile new file +46 -0
46 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/aes_encrypt.c new file +191 -0
191 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/cmac_mode.c new file +254 -0
254 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/encl_bootstrap.S new file +163 -0
163 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/aes.h new file +133 -0
133 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/cmac_mode.h new file +194 -0
194 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/constants.h new file +59 -0
59 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/include/tinycrypt/utils.h new file +95 -0
95 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/main.c new file +203 -0
203 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/sgx_le.lds new file +28 -0
@@ -0,0 +1,28 @@ +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*) + } +}
drivers/platform/x86/intel_sgx/le/enclave/sgxsign.c new file +537 -0
537 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/enclave/utils.c new file +78 -0
78 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/entry.S new file +117 -0
117 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/include/sgx_asm.h new file +64 -0
64 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/include/sgx_encl.h new file +105 -0
105 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/main.c new file +219 -0
219 lines not on this page · raw message
drivers/platform/x86/intel_sgx/le/sgx_le_piggy.S new file +15 -0
@@ -0,0 +1,15 @@ +#include <linux/linkage.h> +#include <asm/page_types.h> + + .section ".rodata","a" + .balign PAGE_SIZE + +GLOBAL(sgx_le_blob) + .incbin "drivers/platform/x86/intel_sgx/le/enclave/sgx_le.bin" +END(sgx_le_blob) + +GLOBAL(sgx_le_blob_end); + +GLOBAL(sgx_le_ss) + .incbin "drivers/platform/x86/intel_sgx/le/enclave/sgx_le.ss" +END(sgx_le_ss)
drivers/platform/x86/intel_sgx/sgx_le_proxy_piggy.S new file +11 -0
@@ -0,0 +1,11 @@ +#include <linux/linkage.h> +#include <asm/export.h> +#include <asm/page_types.h> + + .section ".rodata","a" + +GLOBAL(sgx_le_proxy) + .incbin "drivers/platform/x86/intel_sgx/le/sgx_le_proxy" +END(sgx_le_proxy) + +GLOBAL(sgx_le_proxy_end)
Signature · 1 line
2.14.1
PATCH v4 11/12 intel_sgx: glue code for in-kernel LE
In reply to Jarkko SakkinenImplements a simple framework for hosting in-kernel LE by using the user space helper framework.
--- drivers/platform/x86/intel_sgx/Kconfig | 2 + drivers/platform/x86/intel_sgx/Makefile | 1 + drivers/platform/x86/intel_sgx/sgx.h | 17 ++ drivers/platform/x86/intel_sgx/sgx_ioctl.c | 13 +- drivers/platform/x86/intel_sgx/sgx_le.c | 313 +++++++++++++++++++++++++++++ drivers/platform/x86/intel_sgx/sgx_main.c | 43 +++- drivers/platform/x86/intel_sgx/sgx_util.c | 25 +++ 7 files changed, 408 insertions(+), 6 deletions(-) create mode 100644 drivers/platform/x86/intel_sgx/sgx_le.c
drivers/platform/x86/intel_sgx/Kconfig +2 -0
@@ -9,6 +9,8 @@ config INTEL_SGX default n depends on X86_64 && CPU_SUP_INTEL select MMU_NOTIFIER + select CRYPTO + select CRYPTO_SHA256 ---help--- Intel(R) SGX is a set of CPU instructions that can be used by applications to set aside private regions of code and data. The code
drivers/platform/x86/intel_sgx/Makefile +1 -0
@@ -11,6 +11,7 @@ intel_sgx-$(CONFIG_INTEL_SGX) += \ sgx_page_cache.o \ sgx_util.o \ sgx_vma.o \ + sgx_le.o \ sgx_le_proxy_piggy.o $(eval $(call config_filename,INTEL_SGX_SIGNING_KEY))
drivers/platform/x86/intel_sgx/sgx.h +17 -0
@@ -68,6 +68,7 @@#include <linux/workqueue.h> #include <linux/mmu_notifier.h> #include <linux/radix-tree.h> +#include <crypto/hash.h> #include <asm/sgx.h> #define SGX_EINIT_SPIN_COUNT 20
@@ -177,6 +178,7 @@ extern u64 sgx_xfrm_mask;extern u32 sgx_misc_reserved;extern u32 sgx_xsave_size_tbl[64 ]; +extern const struct file_operations sgx_fops;extern const struct vm_operations_struct sgx_vm_ops;#define sgx_pr_ratelimited (level, encl, fmt, ...) \
@@ -235,6 +237,9 @@ struct sgx_encl_page *sgx_fault_page(struct vm_area_struct *vma, unsigned int flags); +int sgx_get_key_hash (struct crypto_shash *tfm,const void *modulus,void *hash); +int sgx_get_key_hash_simple (const void *modulus,void *hash); +extern struct mutex sgx_tgid_ctx_mutex;extern struct list_head sgx_tgid_ctx_list;extern atomic_t sgx_va_pages_cnt;
@@ -249,4 +254,16 @@ void sgx_put_page(void *epc_page_vaddr);void sgx_eblock (struct sgx_encl *encl,struct sgx_epc_page *epc_page);void sgx_etrack (struct sgx_encl *encl); +extern struct sgx_le_ctx sgx_le_ctx; + +int sgx_le_init (struct sgx_le_ctx *ctx); +void sgx_le_exit (struct sgx_le_ctx *ctx); +void sgx_le_stop (struct sgx_le_ctx *ctx); +int sgx_le_start (struct sgx_le_ctx *ctx); + +int sgx_le_get_token (struct sgx_le_ctx *ctx, +const struct sgx_encl *encl, +const struct sgx_sigstruct *sigstruct, +struct sgx_einittoken *token); +#endif /* __ARCH_X86_INTEL_SGX_H__ */
drivers/platform/x86/intel_sgx/sgx_ioctl.c +9 -4
@@ -69,7 +69,7 @@#include <linux/hashtable.h> #include <linux/shmem_fs.h> -static int sgx_get_encl (unsigned long addr,struct sgx_encl **encl) +static int sgx_encl_get (unsigned long addr,struct sgx_encl **encl) {struct mm_struct *mm = current->mm;struct vm_area_struct *vma;
@@ -156,7 +156,7 @@ static long sgx_ioc_enclave_add_page(struct file *filep, unsigned int cmd,void *data;int ret; - ret =sgx_get_encl (addp->addr, &encl); + ret =sgx_encl_get (addp->addr, &encl);if (ret)return ret;
@@ -227,11 +227,16 @@ static long sgx_ioc_enclave_init(struct file *filep, unsigned int cmd,if (ret) goto out; - ret =sgx_get_encl (encl_id, &encl); + ret =sgx_encl_get (encl_id, &encl);if (ret) goto out; - ret =sgx_encl_init (encl, sigstruct, einittoken); +if (!(initp->flags &&SGX_ENCLAVE_INIT_ARCH )) + ret =sgx_le_get_token (&sgx_le_ctx, encl, sigstruct, + einittoken); + +if (!ret) + ret =sgx_encl_init (encl, sigstruct, einittoken);kref_put (&encl->refcount, sgx_encl_release);
drivers/platform/x86/intel_sgx/sgx_le.c new file +313 -0
313 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_main.c +41 -2
72 lines not on this page · raw message
drivers/platform/x86/intel_sgx/sgx_util.c +25 -0
@@ -370,3 +370,28 @@ void sgx_etrack(struct sgx_encl *encl)sgx_invalidate (encl, true); } } + +int sgx_get_key_hash (struct crypto_shash *tfm,const void *modulus,void *hash) +{ +SHASH_DESC_ON_STACK (shash, tfm); + + shash->tfm = tfm; + shash->flags =CRYPTO_TFM_REQ_MAY_SLEEP ; + +return crypto_shash_digest (shash, modulus,SGX_MODULUS_SIZE , hash); +} + +int sgx_get_key_hash_simple (const void *modulus,void *hash) +{ +struct crypto_shash *tfm; +int ret; + + tfm =crypto_alloc_shash ("sha256" ,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); + + ret =sgx_get_key_hash (tfm, modulus, hash); + +crypto_free_shash (tfm); +return ret; +}
Signature · 1 line
2.14.1
PATCH v4 12/12 intel_sgx: update IA32_SGXLEPUBKEYHASH* MSRs
In reply to Jarkko SakkinenCheck if IA32_SGXLEPUBKEYHASH* MSRs match. If they do not match, allow the driver initialization to continue only if they are writable. In this case update them with the MRSIGNER of the launch enclave.
--- drivers/platform/x86/intel_sgx/sgx.h | 2 ++ drivers/platform/x86/intel_sgx/sgx_encl.c | 19 +++++++++++++++++++ drivers/platform/x86/intel_sgx/sgx_le_proxy_piggy.S | 4 ++++ drivers/platform/x86/intel_sgx/sgx_main.c | 13 ++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-)
drivers/platform/x86/intel_sgx/sgx.h +2 -0
@@ -177,6 +177,8 @@ extern u64 sgx_encl_size_max_64;extern u64 sgx_xfrm_mask;extern u32 sgx_misc_reserved;extern u32 sgx_xsave_size_tbl[64 ]; +extern u64 sgx_le_pubkeyhash[4 ]; +extern bool sgx_unlocked_msrs;extern const struct file_operations sgx_fops;extern const struct vm_operations_struct sgx_vm_ops;
drivers/platform/x86/intel_sgx/sgx_encl.c +19 -0
@@ -68,6 +68,7 @@#include <linux/slab.h> #include <linux/hashtable.h> #include <linux/shmem_fs.h> +#include <linux/percpu.h> struct sgx_add_page_req {struct sgx_encl *encl;
@@ -873,6 +874,14 @@ static int sgx_einit(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,return ret; } +static void sgx_update_pubkeyhash (void ) +{ +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH0 , sgx_le_pubkeyhash[0 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH1 , sgx_le_pubkeyhash[1 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH2 , sgx_le_pubkeyhash[2 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH3 , sgx_le_pubkeyhash[3 ]); +} +/** * sgx_encl_init - perform EINIT for the given enclave *
@@ -908,6 +917,16 @@ int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,for (j =0 ; j <SGX_EINIT_SPIN_COUNT ; j++) { ret =sgx_einit (encl, sigstruct, token); +if (ret ==SGX_INVALID_ATTRIBUTE || + ret ==SGX_INVALID_EINITTOKEN ) { +if (sgx_unlocked_msrs) { +preempt_disable (); +sgx_update_pubkeyhash (); + ret =sgx_einit (encl, sigstruct, token); +preempt_enable (); + } + } +if (ret ==SGX_UNMASKED_EVENT )continue ;else
drivers/platform/x86/intel_sgx/sgx_le_proxy_piggy.S +4 -0
@@ -9,3 +9,7 @@ GLOBAL(sgx_le_proxy) END(sgx_le_proxy) GLOBAL(sgx_le_proxy_end) + +GLOBAL(sgx_le_ss) + .incbin "drivers/platform/x86/intel_sgx/le/enclave/sgx_le.ss" +END(sgx_le_ss)
drivers/platform/x86/intel_sgx/sgx_main.c +12 -1
@@ -80,6 +80,7 @@ MODULE_VERSION(DRV_VERSION);* Global data. */ +extern struct sgx_sigstruct sgx_le_ss;struct workqueue_struct *sgx_add_page_wq;#define SGX_MAX_EPC_BANKS 8struct sgx_epc_bank sgx_epc_banks[SGX_MAX_EPC_BANKS ];
@@ -89,6 +90,8 @@ u64 sgx_encl_size_max_64;u64 sgx_xfrm_mask =0x3 ;u32 sgx_misc_reserved;u32 sgx_xsave_size_tbl[64 ]; +bool sgx_unlocked_msrs; +u64 sgx_le_pubkeyhash[4 ];static DECLARE_RWSEM (sgx_file_sem);
@@ -267,6 +270,7 @@ static int sgx_dev_init(struct device *parent) {struct sgx_context *sgx_dev;unsigned int eax, ebx, ecx, edx; +unsigned long fc;unsigned long pa;unsigned long size;int ret;
@@ -276,6 +280,10 @@ static int sgx_dev_init(struct device *parent) sgx_dev =sgxm_ctx_alloc (parent); +rdmsrl (MSR_IA32_FEATURE_CONTROL , fc); +if (fc &FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE ) + sgx_unlocked_msrs = true; +cpuid_count (SGX_CPUID ,SGX_CPUID_CAPABILITIES , &eax, &ebx, &ecx, &edx);/* Only allow misc bits supported by the driver. */ sgx_misc_reserved = ~ebx |SGX_MISC_RESERVED_MASK ;
@@ -296,6 +304,10 @@ static int sgx_dev_init(struct device *parent) } } + ret =sgx_get_key_hash_simple (sgx_le_ss.modulus, sgx_le_pubkeyhash); +if (ret) +return ret; +for (i =0 ; i <SGX_MAX_EPC_BANKS ; i++) {cpuid_count (SGX_CPUID , i +SGX_CPUID_EPC_BANKS , &eax, &ebx, &ecx, &edx);
@@ -384,7 +396,6 @@ static int sgx_drv_probe(struct platform_device *pdev) }rdmsrl (MSR_IA32_FEATURE_CONTROL , fc); -if (!(fc &FEATURE_CONTROL_LOCKED )) {pr_err ("intel_sgx: the feature control MSR is not locked\n" );return -ENODEV ;
Signature · 1 line
2.14.1