Basic KVM SGX Virtualization support
cover Basic KVM SGX Virtualization support
Hi,
This RFC series are KVM part of Basic KVM SGX virtualization support (KVM SGX EPC static partitioning + Launch Control + SGX2 support). Qemu also needs to be changed to support KVM SGX virtualization and Qemu part will be sent out separately in the future.
You can also find this series and Qemu changes at below github repos:
https://github.com/01org/kvm-sgx.git
https://github.com/01org/qemu-sgx.git
KVM SGX virtualization needs to work with host SGX driver (explained below, which has not been upstreamed yet), therefore part of this series will depend on SGX driver. You can find the SGX driver at below repo on github.
https://github.com/jsakkine-intel/linux-sgx
The SGX specification can be found in latest Intel SDM as Volume D(below).
https://software.intel.com/sites/default/files/managed/7c/f1/332831-sdm-vol-3d.pdf
SGX is relatively more complicated on specification (entire Volume D) and it is unrealistic to list all hardware details here. Below is the brief SGX overview (which I think is mandatory to talk about design) and high level design. Please help to review and give comments. Thanks!
============================ SGX Overview ===========================
- Enclave
Intel Software Guard Extensions (SGX) is a set of instructions and mechanisms for memory accesses in order to provide security accesses for sensitive applications and data. SGX allows an application to use it's pariticular address space as an *enclave*, which is a protected area provides confidentiality and integrity even in the presence of privileged malware. Accesses to the enclave memory area from any software not resident in the enclave are prevented, including those from privileged software. Below diagram illustrates the presence of Enclave in application.
|-----------------------| | | | |---------------| | | | OS kernel | | |-----------------------| | |---------------| | | | | | | | | |---------------| | | |---------------| | | | Entry table | | | | Enclave |---|-----> | |---------------| | | |---------------| | | | Enclave stack | | | | App code | | | |---------------| | | |---------------| | | | Enclave heap | | | | Enclave | | | |---------------| | | |---------------| | | | Enclave code | | | | App code | | | |---------------| | | |---------------| | | | | | | |-----------------------| |-----------------------|
SGX supports SGX1 and SGX2 extensions. SGX1 provides basic enclave support, and SGX2 allows additional flexibility in runtime management of enclave resources and thread execution within an enclave.
- Enclave Page Cache
Enclave Page Cache (EPC) is the physical resource used to commit to enclave. EPC is divided to 4K pages. An EPC page is 4K in size and always aligned to 4K boundary. Hardware performs additional access control checks to restrict access to the EPC page. The Enclave Page Cache Map (EPCM) is a secure structure which holds one entry for each EPC page, and is used by hardware to track the status of each EPC page (invisibe to software). Typically EPC and EPCM are reserved by BIOS as Processor Reserved Memory but the actual amount, size, and layout of EPC are model-specific, and dependent on BIOS settings. EPC is enumerated via new SGX CPUID, and is reported as reserved memory.
EPC pages can either be invalid or valid. There are 4 valid EPC types in SGX1:
regular EPC page, SGX Enclave Control Structure (SECS) page, Thread Control Structure (TCS) page, and Version Array (VA) page. SGX2 adds Trimmed EPC page. Each enclave is associated with one SECS page. Each thread in enclave is associated with one TCS page. VA page is used in EPC page eviction and reload. Trimmed EPC page is used when particular 4K page in enclave is going to be freed (trimmed).
- ENCLS and ENCLU
Two new instructions ENCLS and ENCLU are introduced to manage enclave and EPC. ENCLS can only run in ring 0, while ENCLU can only run in ring 3. Both ENCLS and ENCLU have multiple leaf functions, with EAX indicating the specific leaf function. Specification of ENCLS and ENCLU can be found at SDM Chapter 41 SGX Instruction References.
- Discovering SGX capability
CPUID.0x7.0:EBX.SGX[bit 2] reports the availability of SGX, and detailed SGX info can be enumerated via new CPUID 0x12.
CPUID.0x12.0x0 enumerates SGX capablity (ex, SGX1, SGX2), including enclave instruction opcode support. CPUID.0x12.0x1 enumerates SGX capability of processor state configuration and enclave configuration in the SECS structure. CPUID.0x12.0x2 (and following indexes if they are valid) enumerates EPC resources. Starting from CPUID.0x12.0x2, each index reports one valid EPC section (base, size), until CPUID reports invalid EPC. Typically multiple EPC sections only exist on multiple sockets server machines (which currently don't exist), and client machine or single socket server just reports one EPC.
Please refer to Chapter 37.7.2 Intel SGX Resource Enumeration Leaves for detailed info of SGX CPUID.
On processor that supports SGX, SGX can also be opt-in{out} via SGX_ENABLE bit (bit 18) of IA32_FEATURE_CONTROL MSR. If SGX_ENABLE bit is cleared while IA32_FEATURE_CONTROL is locked then SGX is disabled on processor. The SGX CPUID 0x12 is still available if SGX is opted out via IA32_FEATURE_CONTROL. The SDM doesn't specify the exact info that SGX CPUID 0x12 will report in this case, but likely they will report invalid SGX info. If SGX is opted in, then SGX CPUID 0x12 reports valid SGX info. ENCLS and ENCLU will either #UD or #GP, depending on the value of CPUID.0x7.0:EBX.SGX, IA32_FEATURE_CONTROL.SGX_ENABLE and IA32_FEATURE_CONTROL.LOCK.
Please refer to Chapter 37.7.1 Intel SGX Opt-in Configuration for detailed info.
- SGX Launch Control
On processor that supports SGX, IA32_SGXLEPUBKEYHASH[0-3] MSRs contains the hash of RSA public key. The Launch Enclave (LE) can be only run if it is signed with the related RSA private key. Without SGX Launch Control, hardware can only run Launch Enclave (LE) that signed with Intel's RSA key. SGX Launch Control allows software to be able to change IA32_SGXLEPUBKEYHASHn at runtime, allowing processor to run 3rd party's LE.
SGX Launch Control adds a new SGX_LAUNCH_CONTROL_ENABLE bit (bit 17) to IA32_FEATURE_CONTROL MSR. If SGX_LAUNCH_CONTROL_ENABLE[bit 17] is 1, IA32_SGXLEPUBKEYHASHn are writable at runtime after IA32_FEATURE_CONTROL.LOCK is set. Otherwise they are readonly. Typically BIOS allows user to setup 3rd party's IA32_SGXLEPUBKEYHASHn before IA32_FEATURE_CONTROL is locked, and allows user to choose whether to allow IA32_SGXLEPUBKEYHASHn to be changed at runtime as well. However this depends on BIOS's implementation.
The CPUID.0x7.0:ECX[bit 30] reports availability of bit 17 of IA32_FEATURE_CONTROL, meaning processor only support SGX Launch Policy when CPUID.0x7.0:ECX[bit 30] is 1.
- SGX interaction with VMX
A new 64-bit ENCLS-exiting bitmap control field is added to VMCS (encoding 0202EH) to control VMEXIT on ENCLS leaf functions. And a new "Enable ENCLS exiting" control bit (bit 15) is defined in secondary processor based vm execution control. 1-Setting of "Enable ENCLS exiting" enables ENCLS-exiting bitmap control.
Support for the 1-setting of "Enable ENCLS exiting" control is enumrated from IA32_VMX_PROCBASED_CTLS2[bit 47]. IA32_VMX_PROCBASED_CTLS2[bit 47] monitors CPUID.[EAX=0x7,ECX=0].EBX.SGX.
A new ENCLS VM exit reason (60) is also defined to Basic Exit Reason.
Below code shows how above execution control works:
IF ( (in VMX non-root operation) and ( Enable_ENCLS_EXITING = 1) )
Then
IF ( ((EAX < 63) and (ENCLS_EXITING_Bitmap[EAX] = 1)) or
(EAX> 62 and ENCLS_EXITING_Bitmap[63] = 1) )
Then
Set VMCS.EXIT_REASON = ENCLS;
Deliver VM exit;
FI;
FI;VM exits that originate within an enclave set the following two bits before delivering the VM exit to the VMM:
- Bit 27 in the Exit reason filed of Basic VM-exit information.
- Bit 4 in the Interruptibility State of Guest Non-Register State of VMCS.
Refer to 42.5 Interactions with VMX, 27.2.1 Basic VM-Exit Information, and 27.3.4 Saving Non-Register.
========================= High Level Design ==========================
- Qemu Changes
EPC is limited resource. Typically the EPC and EPCM together are 32M, 64M, or 128M configurable in BIOS. In order to use EPC more efficiently between different KVM guests, we add additional Qemu parameters to allow administrator to specify guest's EPC size when it is created. we also add additional two parameters for SGX Launch Control. Specifically, below SGX parameters are added:
# qemu-system-x86_64 -sgx epc=<size>,lehash='256-bit value string',lewr
In which 'epc' parameter specifies guest's EPC size. Any MB aligned value is supported. 'lehash' is used to specify guest's IA32_SGXLEPUBKEYHASHn initial value, and 'lewr' is used to specify whether guest's IA32_SGXLEPUBKEYHASHn are writable for guest OS. 'epc' is mandatory and both 'lehash' and 'lewr' are optional. Normally with 'lewr' specified, 'lehash' is not needed (and default value is Intel's hash) as guest OS is able to change IA32_SGXLEPUBKEYHASHn as it wishs.
With 'epc' parameter, Qemu is responsible for notifying KVM guest's EPC base and size. EPC base address will be calculated by Qemu internally (according to chip type, memory size, etc).
With 'lehash' specified, Qemu sets guest's IA32_SGXLEPUBKEYHASHn to the value specified. With 'lewr' specified, Qemu sets guest's IA32_FEATURE_CONTROL bit 17 to be 1.
- Expose SGX to guest
SGX feature is exposed to guest via SGX CPUID. Looking at SGX CPUID, we can report the same CPUID info to guest as on native for most of SGX CPUID. With reporting the same CPUID guest is able to use full capacity of SGX, and KVM doesn't need to emulate those info.
There are two exceptions: the first is obviously KVM cannot report physical EPC to guest, but should report guest's (virtual) EPC base and size (which will be notified from Qemu as we mentioned above). The second one is SECS.ATTRIBUTES, which is reported by CPUID.0x12.1:EAX-EDX. Particularly, it is SECS.ATTRIBUTES.XFRM(bit 127:64] that needs emulation. It reports which XFRM bits can be set when creating enclave by using ENCLS[ECREATE]. As guest may not support all XFRM bits that supported by hardware, CPUID.0x12.0x1:[ECX-EDX] should also only reports guest's supported XFRM bits.
All other CPUID info can be reported to guest just as the same as on native.
And we only report one EPC section to guset (only CPUID.0x12.0x2 is valid).
- Initializing SGX for guest
As mentioned above guest's EPC base and size are determined by Qemu, and KVM needs Qemu to notify such info to it before it can initialize SGX for guest. To avoid new IOCTL for such purpose (ex, KVM_SET_EPC), KVM will initialize guest's SGX in KVM_SET_CPUID2, where Qemu will pass guest's SGX CPUID where guest's EPC base and size will be included.
Also the SDM says SGX CPUID is actually thread-specific. Software cannot assume all logical processor will report the same SGX CPUID. Initializing guest's SGX in KVM_SET_CPUID2 provides an opportunity for KVM to check whether SGX CPUID passed by Qemu are valid and consistent within for all VCPUs.
- EPC management
On host side there's SGX driver which serves host SGX applications from userspace. It detects SGX features and manages all EPC pages. To work with SGX driver simultaneously, we have to use 'unified model', in which SGX driver still manages EPC and KVM calls driver's APIs to allocate/free EPC page, etc. However KVM cannot call driver's APIs directly, as on machines without SGX feature, SGX driver won't be loaded, and calling driver's APIs directly will make KVM unable to be loaded either. Instead, KVM uses symbol_get to get driver's APIs at runtime to avoids this issue.
For KVM guests, there are two approaches in terms of managing EPC: static partitioning and oversubscription. In static partitioning all EPC pages are are allocated to guest when it is created and are freed only when guest is destroyed. In oversubscription, EPC pages are allocated to guest on demand, and EPC pages allocated to guest can be evicted out by KVM, and reassigned to other guests. Accessing to guest EPC page where there's no physical EPC mapped causes EPT violation (or PF in case of shadowing), in which physical EPC page will be allocated to guest (and reloaded to enclave if required).
-- Static partitioning
Static partitioning is an simple appproach. KVM only needs to allocate all EPC pages when guest is created and set up mapping. All ENCLS leaf functions will run perfectly in guest, so KVM doesn't need to turn on ENCLS VMEXIT.
However KVM needs to turn on ENCLS VMEXIT if KVM doesn't expose SGX to guest, or guest has turned off SGX via IA32_FEATURE_CONTROL.SGX_ENABLE, as in such cases ENCLS run in guest may have different behavior from on native, as on hardware SGX is indeed enabled, but accroding to SDM, running ENCLS in guest while SGX environment is abnormal in guest should cause #UD or #GP. KVM needs to trap ENCLS to emulate such behavior.
-- Oversubscription
While oversubscription is better in terms of functionality, it needs more complicated implementation. Below is the brief explanation of what needs to be done in order to support EPC oversubscription between guests.
Below is the sequence to evict regular EPC page:
1) Select one or multiple regular EPC pages from one enclave
2) Remove EPT/PT mapping for selected EPC pages
3) Send IPIs to remote CPUs to flush TLB of selected EPC pages
4) EBLOCK on selected EPC pages
5) ETRACK on enclave's SECS page
6) allocate one available slot (8-byte) in VA page
7) EWB on selected EPC pages
With EWB taking:
- VA slot, to restore eviction version info.
- one normal 4K page in memory, to store encrypted content of EPC page.
- one struct PCMD in memory, to store meta data.
And below is the sequence to evict an SECS page or VA page:
1) locate SECS (or VA) page
2) remove EPT/PT mapping for SECS (or VA) page
3) Send IPIs to remote CPUs
6) allocate one available slot (8-byte) in VA page
4) EWB on SECS (or) page
And for evicting SECS page, all regular EPC pages that belongs to that SECS must be evicted out prior, otherwise EWB returns SGX_CHILD_PRESENT error.
And to reload an EPC page:
1) ELDU/ELDB on EPC page
2) setup EPT/PT mapping
With ELDU/ELDB taking:
- location of SECS page
- linear address of enclave's 4K page (that we are going to reload to)
- VA slot (used in EWB)
- 4K page in memory (used in EWB)
- struct PCMD in memory (used in EWB)
Therefore, to support EPC oversubscription for guests, KVM needs to know:
1) EPC page type (SECS, regular page, VA page, etc)
2) EPC status (whether blocked) -- guest may already have run EBLOCK
3) location of SECS page -- both eviction & reload need it.
Besides above, KVM also needs to manage allocation of VA slot, which itself is also EPC page and could potentially trigger EPC oversubscription.
To get above info, KVM needs to trap ENCLS from all guests, and maintain info of all EPC pages and all enclaves from all guests. Specifically, KVM needs to turn on ENCLS VMEXIT for all guests, and upon ENCLS VMEXIT, KVM needs to parse ENCLS parameters (so that we can update EPC/enclave info according to which ENCLS leaf guest is running, and it's parameters). KVM also needs to either run ENCLS on behalf of guest (and skip this ENCLS), or using MTF to return to guest and let guest run this ENCLS again. For the formar, KVM needs to reconstruct guest's ENCLS parameters and remap guest's virtual address to KVM kernel address (as all addresses in guest's ENCLS parameters are guest virtual address), and run ENCLS in KVM on behalf for guest. For the latter, upon ENCLS VMEXIT, KVM needs to temporary turn off ENCLS VMEXIT, turn on MTF VMEXIT, and enter guest to allow guest run this ENCLS again. This time ENCLS VMEXIT won't happen and MTF VMEXIT will happen after ENCLS is executed. Upon MTF VMEXIT, we turn on ENCLS VMEXIT and turn off MTF VMEXIT again.
Below diagrams compares the two approaches: Run ENCLS in KVM, and Using MTF.
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
| /|\
ENCLS VMEXIT | | VMENTRY
| |
\|/ |
1) parse ENCLS parameters
2) reconstruct(remap) guest's ENCLS parameters
3) run ENCLS on behalf of guest (and skip ENCLS)
4) on success, update EPC/enclave info, or inject error
1) Run ENCLS in KVM
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
| /|\ |/|\
ENCLS | | VMENTRY MTF | | VMENTRY
VMEXIT | | VMEXIT | |
\|/ | \|/|
1) Turn off EMCLS VMEXIT 1) Turn off MTF VMEXIT
2) turn on MTF VMEXIT 2) Turn on ENCLS VMEXIT
3) cache ENCLS parameters 3) check ENCLS succeeds or not, and
(as ENCLS will change RAX-RDX) only on success, parse cached
ENCLS parameters, and update EPC/enclave info
2) Using MTF
Note in using MTF, checking ENCLS status (whether succeeds or not) is tricky, as ENCLS can both return error via EAX register, or just cause #UD or #GP. For the formar case it's relatively easier for KVM to check but for the latter KVM needs to trap #UD and #GP from guest, and also needs to check whether the #UD or #GP happened while running ENCLS.
In this patch series we only support 'static partitioning'. 'oversubscription' can be supported when it is required. Currently we do support nested SGX (mentioned below) and in 'oversubscription' supporting nested SGX will be very complicated.
- Guest's EPC memory slot implementation
Guest's (virtual) EPC is implemented as private memory slot in KVM. Qemu will not be aware the existence of such EPC slot. Using private slot, we can avoid mmap in Qemu for getting EPC slot's host virtual address, and KVM doesn't need to handle such mmap from Qemu for EPC slot. And we don't want to implement such mmap support in SGX driver either.
A dedicated kvm_epc_ops is added for VMA of EPC slot, and EPC page will be allocated via vma->vm_ops->fault. This is the natual way to support 'oversubscription' (if we need to support in the future) and works for 'static partitioning' nicely as well.
- Nested SGX
Currently for 'static partitioning' nested SGX is also supported. As mentioned above in normal case KVM (L0) doesn't need to turn on ENCLS VMEXIT, but KVM cannot assume L1 hypervisor's behavior, so if ENCLS VMEXIT is turned on in L1, KVM (L0) must also turn on ENCLS VMEXIT but let L1 to handle such ENCLS VMEXIT from L2 guest.
Supporting nested SGX in 'oversubscription' will be very complicated, as both L0 and L1 may turn on ENCLS VMEXIT, and both L0 and L1 needs to maintain and update EPC/enclave info from guests as explained above.
Kai Huang (10):
x86: add SGX Launch Control definition to cpufeature
kvm: vmx: add ENCLS VMEXIT detection
kvm: vmx: detect presence of host SGX driver
kvm: sgx: new functions to init and destory SGX for guest
kvm: x86: add KVM_GET_SUPPORTED_CPUID SGX support
kvm: x86: add KVM_SET_CPUID2 SGX support
kvm: vmx: add SGX IA32_FEATURE_CONTROL MSR emulation
kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
kvm: vmx: handle ENCLS VMEXIT
kvm: vmx: handle VMEXIT from SGX Enclave
arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/kvm_host.h | 9 +- arch/x86/include/asm/msr-index.h | 7 + arch/x86/include/asm/vmx.h | 4 + arch/x86/include/uapi/asm/vmx.h | 5 +- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/cpuid.c | 21 +- arch/x86/kvm/cpuid.h | 22 ++ arch/x86/kvm/sgx.c | 463 +++++++++++++++++++++++ arch/x86/kvm/sgx.h | 105 ++++++ arch/x86/kvm/svm.c | 11 +- arch/x86/kvm/vmx.c | 752 +++++++++++++++++++++++++++++++++++-- 12 files changed, 1362 insertions(+), 40 deletions(-) create mode 100644 arch/x86/kvm/sgx.c create mode 100644 arch/x86/kvm/sgx.h
Signature · 1 line
2.11.0
PATCH 1/10 x86: add SGX Launch Control definition to cpufeature
For Intel CPU that supports SGX Launch Control, CPUID.0x7.0:ECX[bit 30] reports the availability of 1-setting of bit 17 of IA32_FEATURE_CONTROL MSR, which enables runtime configuration of SGX Launch Control via IA32_SGXLEPUBKEYHASHn.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -292,6 +292,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_LAUNCH_CONTROL (16*32+30)/* SGX Launch Control */ /* 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.11.0
PATCH 2/10 kvm: vmx: add ENCLS VMEXIT detection
In reply to Kai HuangThis patch detects whether ENCLS VMEXIT is supported. A new bool parameter 'enable_sgx' is also added to control enable SGX virtualization or not. SGX virtualization is disabled if hardware doesn't support ENCLS VMEXIT.
ENCLS VMEXIT is disabled in vmx_secondary_exec_control, and when to turn on or off is done in further patch.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx.c | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -72,6 +72,7 @@#define SECONDARY_EXEC_PAUSE_LOOP_EXITING 0x00000400#define SECONDARY_EXEC_ENABLE_INVPCID 0x00001000#define SECONDARY_EXEC_SHADOW_VMCS 0x00004000 +#define SECONDARY_EXEC_ENCLS_EXITING 0x00008000#define SECONDARY_EXEC_ENABLE_PML 0x00020000#define SECONDARY_EXEC_XSAVES 0x00100000#define SECONDARY_EXEC_TSC_SCALING 0x02000000
arch/x86/kvm/vmx.c +21 -1
@@ -108,6 +108,9 @@ static u64 __read_mostly host_xss;static bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO ); +static bool __read_mostly enable_sgx =1 ; +module_param_named (sgx, enable_sgx, bool,S_IRUGO ); +#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
@@ -1123,6 +1126,12 @@ static inline bool cpu_has_vmx_virtual_intr_delivery(void)SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY ; } +static inline bool cpu_has_vmx_encls_vmexit (void ) +{ +return vmcs_config.cpu_based_2nd_exec_ctrl & +SECONDARY_EXEC_ENCLS_EXITING ; +} +/* * Comment's format: document - errata name - stepping - processor name. * Refer from
@@ -3585,7 +3594,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)SECONDARY_EXEC_SHADOW_VMCS |SECONDARY_EXEC_XSAVES |SECONDARY_EXEC_ENABLE_PML | -SECONDARY_EXEC_TSC_SCALING ; +SECONDARY_EXEC_TSC_SCALING | +SECONDARY_EXEC_ENCLS_EXITING ;if (adjust_vmx_controls (min2, opt2,MSR_IA32_VMX_PROCBASED_CTLS2 , &_cpu_based_2nd_exec_control) <0 )
@@ -5160,6 +5170,13 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)if (!enable_pml) exec_control &= ~SECONDARY_EXEC_ENABLE_PML ; +/* +* ENCLS VMEXIT is controlled in vmx_cpuid_update. This function is +* called in many places. We don't want ENCLS VMEXIT get enabled +* surprisingly. +*/ + exec_control &= ~SECONDARY_EXEC_ENCLS_EXITING ; +return exec_control; }
@@ -6655,6 +6672,9 @@ static __init int hardware_setup(void) kvm_mce_cap_supported |=MCG_LMCE_P ; +if (!cpu_has_vmx_encls_vmexit ()) + enable_sgx =0 ; +return alloc_kvm_area (); out:
Signature · 1 line
2.11.0
PATCH 3/10 kvm: vmx: detect presence of host SGX driver
In reply to Kai HuangAt host side there's SGX driver which serves host SGX applications. It detects SGX feature and manages all EPC pages. KVM needs to co-work with SGX driver in terms of EPC management, because they are both EPC consumers. We should go for 'unified model', in which SGX driver manages all EPC pages and KVM simply calls driver's APIs to allocate/free EPC page, etc. However KVM cannot call driver's APIs directly, as on machines without SGX feature, SGX driver won't be loaded, and calling driver's APIs directly will make KVM unable to be loaded either. Instead, KVM uses symbol_get to get driver's APIs at runtime thus avoids this issue.
This patch adds new functions to initialize and destroy KVM SGX support, where currently KVM simply calls symbol_get{put} for all necessary driver's APIs. The symbols will only be released when KVM exits to prevent SGX driver being unloaded during KVM's lifetime. Note KVM compeletely trusts SGX driver in SGX feature and EPC resource detection and won't detect SGX by itself.
Two new files arch/x86/kvm/sgx.c{h} are added for holding bulk of KVM SGX code.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/sgx.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/sgx.h | 34 +++++++++++ arch/x86/kvm/vmx.c | 10 ++++ 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kvm/sgx.c create mode 100644 arch/x86/kvm/sgx.h
arch/x86/kvm/Makefile +1 -1
@@ -17,7 +17,7 @@ kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += assigned-dev.o iommu.o -kvm-intel-y += vmx.o pmu_intel.o +kvm-intel-y += vmx.o pmu_intel.o sgx.o kvm-amd-y += svm.o pmu_amd.o obj-$(CONFIG_KVM) += kvm.o
arch/x86/kvm/sgx.c new file +163 -0
163 lines not on this page · raw message
arch/x86/kvm/sgx.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* KVM SGX Virtualization support. +* +* Copyright (c) 2015, Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope 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. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., 59 Temple +* Place - Suite 330, Boston, MA 02111-1307 USA. +* +* Author: Kai Huang <[email protected]> +*/ + +#ifndef ARCH_X86_KVM_SGX_H +#define ARCH_X86_KVM_SGX_H + +#include <linux/types.h> +#include <linux/list.h> +#include <linux/bitops.h> +#include <linux/kvm_host.h> +#include <asm/sgx.h> + +int sgx_init (void ); +void sgx_destroy (void ); + +#endif
arch/x86/kvm/vmx.c +10 -0
@@ -52,6 +52,8 @@#include "trace.h" #include "pmu.h" +#include "sgx.h" +#define __ex (x) __kvm_handle_fault_on_reboot(x)#define __ex_clear (x, reg) \ ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
@@ -11657,6 +11659,11 @@ static int __init vmx_init(void)if (r)return r; +if (enable_sgx) { +if (sgx_init ()) + enable_sgx =0 ; + } +#ifdef CONFIG_KEXEC_CORE rcu_assign_pointer (crash_vmclear_loaded_vmcss, crash_vmclear_local_loaded_vmcss);
@@ -11672,6 +11679,9 @@ static void __exit vmx_exit(void)synchronize_rcu ();#endif +if (enable_sgx) +sgx_destroy (); +kvm_exit (); }
Signature · 1 line
2.11.0
PATCH 4/10 kvm: sgx: new functions to init and destory SGX for guest
In reply to Kai HuangAdd new kvm_sgx structure to keep per-guest SGX staff, including guest's CPUID and EPC slot info. The initialization function checks consistency of SGX cpuid info from Qemu (return error in case Qemu did something wrong) and creates EPC slot (only once when firstly called). If anything goes wrong by returning error to Qemu, it is able to stop creating vcpu or just kill guest.
EPC slot is implemented as private memory slot by KVM. It is the easiest way as we don't expose a 'file' to userspace to let Qemu issue mmap to get userspace virtual address for EPC slot, and we don't want to use SGX driver's mmap for this purpose either.
EPC page is actually allocated via vma->vm_ops->fault associated to EPC slot's vma, to comply with current hva_to_pfn implementation, so that hva_to_pfn works for EPC as well.
A new kvm_epc structure is also added to represent EPC slot info for guest, and a new kvm_epc_page structure is added to track each guest's EPC page status. It is used to keep all physical EPC pages allocated to guest, which is needed when KVM wants to free all EPC pages allocated to guest when guest is destroyed. Btw SGX driver doesn't have sgx_epc_pfn_to_page so KVM needs to do the bookkeeping. What's more, we can expend it in the future, ex, to support EPC oversubscription between KVM guests (where we will have more status of guest's EPC page).
--- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/sgx.c | 300 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/sgx.h | 71 ++++++++++ 3 files changed, 374 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +3 -1
@@ -40,7 +40,7 @@#define KVM_MAX_VCPU_ID 1023#define KVM_USER_MEM_SLOTS 509/* memory slots that are not exposed to userspace */ -#define KVM_PRIVATE_MEM_SLOTS 3 +#define KVM_PRIVATE_MEM_SLOTS 4#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)#define KVM_PIO_PAGE_OFFSET 1
@@ -817,6 +817,8 @@ struct kvm_arch {bool x2apic_format;bool x2apic_broadcast_quirk_disabled; + +void *priv;/* x86 vendor specific data */ };struct kvm_vm_stat {
arch/x86/kvm/sgx.c +300 -0
306 lines not on this page · raw message
arch/x86/kvm/sgx.h +71 -0
79 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH 5/10 kvm: x86: add KVM_GET_SUPPORTED_CPUID SGX support
In reply to Kai HuangThis patch adds SGX CPUID support for KVM_GET_SUPPORTED_CPUID IOCTL. We need to only expose SGX CPUID when enable_sgx is valid, as enable_sgx may be false, for example, when user deliberately disables SGX, or when SGX initialization fails, in which case hardware will still reports valid SGX CPUID.
As enable_sgx is not exposed to arch/x86/kvm/cpuid.c, we need to do SGX related CPUID in vmx.c, for which kvm_x86_ops->set_supported_cpuid is extended to meet SGX's need, and do_cpuid_1_ent is also exposed to VMX.
--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/cpuid.c | 13 ++++---- arch/x86/kvm/cpuid.h | 2 ++ arch/x86/kvm/svm.c | 5 ++- arch/x86/kvm/vmx.c | 71 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -978,7 +978,8 @@ struct kvm_x86_ops {void (*set_tdp_cr3)(struct kvm_vcpu *vcpu,unsigned long cr3); -void (*set_supported_cpuid)(u32 func,struct kvm_cpuid_entry2 *entry); +int (*set_supported_cpuid)(u32 func,u32 index, +struct kvm_cpuid_entry2 *entry,int *nent,int maxnent);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/cpuid.c +7 -6
@@ -274,7 +274,7 @@ static void cpuid_mask(u32 *word, int wordnum) *word &= boot_cpu_data.x86_capability[wordnum]; } -static void do_cpuid_1_ent (struct kvm_cpuid_entry2 *entry,u32 function, +void do_cpuid_1_ent (struct kvm_cpuid_entry2 *entry,u32 function,u32 index) { entry->function = function;
@@ -283,6 +283,7 @@ static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); entry->flags =0 ; } +EXPORT_SYMBOL_GPL (do_cpuid_1_ent);static int __do_cpuid_ent_emulated (struct kvm_cpuid_entry2 *entry,u32 func,u32 index,int *nent,int maxnent)
@@ -402,7 +403,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,switch (function) {case 0 : - entry->eax =min (entry->eax, (u32 )0xd ); + entry->eax =min (entry->eax, (u32 )0x12 );break ;case 1 : entry->edx &= kvm_cpuid_1_edx_x86_features;
@@ -573,6 +574,9 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, }break ; } +case 0x12 : +/* Intel SGX CPUID. Passthrough to VMX to handle. */ +break ;case KVM_CPUID_SIGNATURE : {static const char signature[12 ] ="KVMKVMKVM\0\0" ;const u32 *sigptr = (const u32 *)signature;
@@ -651,10 +655,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,break ; } - kvm_x86_ops->set_supported_cpuid (function, entry); - - r =0 ; - + r = kvm_x86_ops->set_supported_cpuid (function, index, entry, nent, maxnent); out:put_cpu ();
arch/x86/kvm/cpuid.h +2 -0
@@ -6,6 +6,8 @@int kvm_update_cpuid (struct kvm_vcpu *vcpu);bool kvm_mpx_supported (void ); +void do_cpuid_1_ent (struct kvm_cpuid_entry2 *entry,u32 function, +u32 index);struct kvm_cpuid_entry2 *kvm_find_cpuid_entry (struct kvm_vcpu *vcpu,u32 function,u32 index); int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
arch/x86/kvm/svm.c +4 -1
@@ -4988,7 +4988,8 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) entry->ecx &= ~bit (X86_FEATURE_X2APIC ); } -static void svm_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry) +static int svm_set_supported_cpuid (u32 func,u32 index, +struct kvm_cpuid_entry2 *entry,int *nent,int maxnent) {switch (func) {case 0x1 :
@@ -5017,6 +5018,8 @@ static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)break ; } + +return 0 ; }static int svm_get_lpage_level (void )
arch/x86/kvm/vmx.c +68 -3
78 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH 6/10 kvm: x86: add KVM_SET_CPUID2 SGX support
In reply to Kai HuangThis patch adds SGX CPUID support for KVM_SET_CPUID2. Besides setting up guest's SGX CPUID, guest's SGX is initialized in KVM_SET_CPUID2 as well. This is because, to avoid adding a new IOCTL to set guest's EPC base & size, we need to get such info from KVM_SET_CPUID2 (where userspace will setup guest's EPC base & size), and guest's SGX can only be initialized after KVM knows such info.
Initializing guest's SGX may fail, so kvm_x86_ops->cpuid_update is changed to return integer to reflect whether guest's SGX is initialized successfully or not. kvm_update_cpuid is also moved to be called before kvm_x86_ops->cpuid_update, as guest's SGX CPUID.0x12.1 needs to depends on vcpu->arch.guest_supported_xcr0 (which is set in kvm_update_cpuid).
Also a new kvm_x86_ops->vm_destroy is added for VMX, in which guest's SGX is destroyed.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/cpuid.c | 8 ++- arch/x86/kvm/cpuid.h | 20 +++++++ arch/x86/kvm/svm.c | 6 ++- arch/x86/kvm/vmx.c | 113 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 143 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -894,7 +894,7 @@ struct kvm_x86_ops {void (*hardware_unsetup)(void );/* __exit */ bool (*cpu_has_accelerated_tpr)(void );bool (*cpu_has_high_real_mode_segbase)(void ); -void (*cpuid_update)(struct kvm_vcpu *vcpu); +int (*cpuid_update)(struct kvm_vcpu *vcpu);int (*vm_init)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm);
arch/x86/kvm/cpuid.c +6 -2
@@ -220,8 +220,10 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, vcpu->arch.cpuid_nent = cpuid->nent;cpuid_fix_nx_cap (vcpu);kvm_apic_set_version (vcpu); - kvm_x86_ops->cpuid_update (vcpu); r =kvm_update_cpuid (vcpu); +if (r) + goto out; + r = kvm_x86_ops->cpuid_update (vcpu); out:vfree (cpuid_entries);
@@ -243,8 +245,10 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, goto out; vcpu->arch.cpuid_nent = cpuid->nent;kvm_apic_set_version (vcpu); - kvm_x86_ops->cpuid_update (vcpu); r =kvm_update_cpuid (vcpu); +if (r) + goto out; + r = kvm_x86_ops->cpuid_update (vcpu); out:return r; }
arch/x86/kvm/cpuid.h +20 -0
@@ -155,6 +155,26 @@ static inline bool guest_cpuid_has_rdtscp(struct kvm_vcpu *vcpu) }/* +* Only checks CPUID.0x7.0x0:EBX.SGX. SDM says if this bit is 1, logical +* processor supports SGX CPUID 0x12. +*/ +static inline bool guest_cpuid_has_sgx (struct kvm_vcpu *vcpu) +{ +struct kvm_cpuid_entry2 *best; + + best =kvm_find_cpuid_entry (vcpu,0x7 ,0 ); +return best && (best->ebx &bit (X86_FEATURE_SGX )); +} + +static inline bool guest_cpuid_has_sgx_launch_control (struct kvm_vcpu *vcpu) +{ +struct kvm_cpuid_entry2 *best; + + best =kvm_find_cpuid_entry (vcpu,0x7 ,0 ); +return best && (best->ecx &bit (X86_FEATURE_SGX_LAUNCH_CONTROL )); +} + +/* * NRIPS is provided through cpuidfn 0x8000000a.edx bit 3 */ #define BIT_NRIPS 3
arch/x86/kvm/svm.c +4 -2
@@ -4972,7 +4972,7 @@ static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)return 0 ; } -static void svm_cpuid_update (struct kvm_vcpu *vcpu) +static int svm_cpuid_update (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);struct kvm_cpuid_entry2 *entry;
@@ -4981,11 +4981,13 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) svm->nrips_enabled = !!guest_cpuid_has_nrips (&svm->vcpu);if (!kvm_vcpu_apicv_active (vcpu)) -return ; +return 0 ; entry =kvm_find_cpuid_entry (vcpu,1 ,0 );if (entry) entry->ecx &= ~bit (X86_FEATURE_X2APIC ); + +return 0 ; } static int svm_set_supported_cpuid(u32 func, u32 index,
arch/x86/kvm/vmx.c +112 -1
141 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH 7/10 kvm: vmx: add SGX IA32_FEATURE_CONTROL MSR emulation
In reply to Kai HuangIf CPUID.0x7.0:EBX.SGX == 1, IA32_FEATURE_CONTROL bit 18 (SGX enable) is valid. If CPUID.0x7.0:ECX[bit30] = 1, IA32_FEATURE_CONTROL bit 17 (SGX Launch Control) is valid. This patch emulate the two new bits for IA32_FEATURE_CONTROL MSR.
--- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kvm/vmx.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
@@ -422,6 +422,8 @@#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_LAUNCH_CONTROL_ENABLE (1<<17) +#define FEATURE_CONTROL_SGX_ENABLE (1<<18)#define FEATURE_CONTROL_LMCE (1<<20)#define MSR_IA32_APICBASE 0x0000001b
arch/x86/kvm/vmx.c +28 -0
@@ -3329,6 +3329,20 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vmx->msr_ia32_feature_control = data;if (msr_info->host_initiated && data ==0 )vmx_leave_nested (vcpu); +/* +* If guest's FEATURE_CONTROL_SGX_ENABLE is disabled, shall +* we also clear vcpu's SGX CPUID? SDM (chapter 37.7.7.1) +* says FEATURE_CONTROL_SGX_ENABLE bit doesn't reflect SGX +* CPUID but in reality seems if FEATURE_CONTROL_SGX_ENABLE +* is disabled, SGX CPUID will reports (at least) invalid EPC. +* But looks we cannot just simply clear vcpu's SGX CPUID, +* as Qemu may write IA32_FEATURE_CONTROL *before* or *after* +* KVM_SET_CPUID2. If KVM_SET_CPUID2 is called first, and we +* clear vcpu's SGX CPUID here, we will not be able to enable +* SGX again as SGX CPUID info has already lost. Therefore do +* nothing here. We assume guest will always check whether +* SGX has been enabled in BIOS before using SGX. +*/ break ; case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:if (!msr_info->host_initiated)
@@ -9594,6 +9608,20 @@ static int vmx_cpuid_update(struct kvm_vcpu *vcpu)if (r)return r; +if (guest_cpuid_has_sgx (vcpu)) { +/* +* If CPUID.0x7.0:EBX.SGX = 1, SGX can be opt-in{out} in BIOS +* via IA32_FEATURE_CONTROL bit 18. If CPUID.0x7.0:ECX[bit30] +* = 1, IA32_FEATURE_CONTROL bit 17 is valid to enable runtime +* SGX Launch Control. +*/ +to_vmx (vcpu)->msr_ia32_feature_control_valid_bits |= +FEATURE_CONTROL_SGX_ENABLE ; +if (guest_cpuid_has_sgx_launch_control (vcpu)) +to_vmx (vcpu)->msr_ia32_feature_control_valid_bits |= +FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE ; + } +return 0 ; }
Signature · 1 line
2.11.0
PATCH 8/10 kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Kai HuangIf SGX runtime launch control is enabled on host (IA32_FEATURE_CONTROL[17] is set), KVM can support running multiple guests with each running LE signed with different RSA pubkey. KVM traps IA32_SGXLEPUBKEYHASHn MSR write from and keeps the values to vcpu internally, and when vcpu is scheduled in, KVM write those values to real IA32_SGXLEPUBKEYHASHn MSR.
--- arch/x86/include/asm/msr-index.h | 5 ++ arch/x86/kvm/vmx.c | 123 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+)
arch/x86/include/asm/msr-index.h +5 -0
@@ -417,6 +417,11 @@#define MSR_IA32_TSC_ADJUST 0x0000003b#define MSR_IA32_BNDCFGS 0x00000d90 +#define MSR_IA32_SGXLEPUBKEYHASH0 0x0000008c +#define MSR_IA32_SGXLEPUBKEYHASH1 0x0000008d +#define MSR_IA32_SGXLEPUBKEYHASH2 0x0000008e +#define MSR_IA32_SGXLEPUBKEYHASH3 0x0000008f +#define MSR_IA32_XSS 0x00000da0#define FEATURE_CONTROL_LOCKED (1<<0)
arch/x86/kvm/vmx.c +123 -0
159 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Hi Paolo/Radim,
I'd like to start a discussion regarding to IA32_SGXLEPUBKEYHASHn handling here. I also copied SGX driver mailing list (which looks like I should do when sending out this series, sorry) and Sean, Haim and Haitao from Intel to have a better discussion.
Basically IA32_SGXLEPUBKEYHASHn (or more generally speaking, SGX Launch Control) allows us to run different Launch Enclave (LE) signed with different RSA keys. Only when the value of IA32_SGXLEPUBKEYHASHn matches the key used to sign the LE, the LE can be initialized, specifically, by EINIT, successfully. So before calling EINIT for LE, we have to make sure IA32_SGXLEPUBKEYHASHn contain the matched value. One fact is only EINIT uses IA32_SGXLEPUBKEYHASHn, and after EINIT, other ENCLS/ENCLU (ex, EGETKEY) runs correctly even the MSRs are changed to other values.
To support KVM guests to run their own LEs inside guests, KVM traps IA32_SGXLEPUBKEYHASHn MSR write and keep the value to vcpu internally, and KVM needs to write the cached value to real MSRs before guest runs EINIT. The problem at host side, we also run LE, probably multiple LEs (it seems currently SGX driver plans to run single in-kernel LE but I am not familiar with details, and IMO we should not assume host will only run one LE), therefore if KVM changes the physical MSRs for guest, host may not be able to run LE as it may not re-write the right MSRs back. There are two approaches to make host and KVM guests work together:
1. Anyone who wants to run LE is responsible for writing the correct value to IA32_SGXLEPUBKEYHASHn.
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
One argument against this approach is KVM guest should never have impact on host side, meaning host should not be aware of such MSR change, in which case, if host do some performance optimization thing that won't update MSRs actively, when host run EINIT, the physical MSRs may contain incorrect value. Instead, KVM should be responsible for restoring the original MSRs, which brings us to approach 2 below.
2. KVM should restore MSRs after changing for guest.
To do this, the simplest way for KVM is: 1) to save the original physical MSRs and update to guest's MSRs before VMENTRY; 2) in VMEXIT rewrite the original value to physical MSRs.
To me this approach is also arguable, as KVM guest is actually just a normal process (OK, maybe not that normal), and KVM guest should be treated as the same as other processes which runs LE, which means approach 1 is also reasonable.
And approach 2 will have more performance impact than approach 1 for KVM, as it read/write IA32_SGXLEPUBKEYHASHn during each VMEXIT/VMENTRY, while approach 1 only write MSRs when vcpu is scheduled in, which is less frequent.
I'd like to hear all your comments and hopefully we can have some agreement on this.
Another thing is, not quite related to selecting which approach above, and either we choose approach 1 or approach 2, KVM still suffers the performance loss of writing (and/or reading) to IA32_SGXLEPUBKEYHASHn MSRs, either when vcpu scheduled in or during each VMEXIT/VMENTRY. Given the fact that the IA32_SGXLEPUBKEYHASHn will only be used by EINIT, We can actually do some optimization by trapping EINIT from guest and only update MSRs in EINIT VMEXIT. This works for approach 1, but for approach 2 we have to do some tricky thing during VMEXIT/VMENTRY to check whether MSRs have been changed by EINIT VMEXIT, and only restore the original value if EINIT VMEXIT has happened. Guest's LE continues to run even physical MSRs are changed back to original.
But trapping ENCLS requires either 1) KVM to run ENCLS on hebalf of guest, in which case we have to reconstruct and remap guest's ENCLS parameters and skip the ENCLS for guest; 2) using MTF to let guest to run ENCLS again, while still trapping ENCLS. Either case would introduce more complicated code and potentially be more buggy, and I don't think we should do this to save some time of writing MSRs. If we need to turn on ENCLS VMEXIT anyway we can optimize this.
Thank you in advance.
Thanks,
-Kai
Quoted 198 lines · Kai Huang
If SGX runtime launch control is enabled on host (IA32_FEATURE_CONTROL[17] is set), KVM can support running multiple guests with each running LE signed with different RSA pubkey. KVM traps
IA32_SGXLEPUBKEYHASHnMSR write from and keeps the values to vcpu internally, and when vcpu is scheduled in, KVM write those values to realIA32_SGXLEPUBKEYHASHnMSR.Signed-off-by Kai Huang <[email protected]>--- arch/x86/include/asm/msr-index.h | 5 ++ arch/x86/kvm/vmx.c | 123 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+)arch/x86/include/asm/msr-index.h +5
-0@@ -417,6 +417,11 @@#define MSR_IA32_TSC_ADJUST 0x0000003b#define MSR_IA32_BNDCFGS 0x00000d90 +#define MSR_IA32_SGXLEPUBKEYHASH0 0x0000008c +#define MSR_IA32_SGXLEPUBKEYHASH1 0x0000008d +#define MSR_IA32_SGXLEPUBKEYHASH2 0x0000008e +#define MSR_IA32_SGXLEPUBKEYHASH3 0x0000008f +#define MSR_IA32_XSS 0x00000da0#define FEATURE_CONTROL_LOCKED (1<<0)arch/x86/kvm/vmx.c +123
-0@@ -656,6 +656,9 @@ struct vcpu_vmx {*/ u64 msr_ia32_feature_control;u64 msr_ia32_feature_control_valid_bits; + +/* SGX Launch Control public key hash */ +u64 msr_ia32_sgxlepubkeyhash[4 ]; };enum segment_cache_field {@@ -2244,6 +2247,70 @@ static void decache_tsc_multiplier(struct vcpu_vmx *vmx)vmcs_write64 (TSC_MULTIPLIER , vmx->current_tsc_ratio); } +static bool cpu_sgx_lepubkeyhash_writable (void ) +{ +u64 val, sgx_lc_enabled_mask = (FEATURE_CONTROL_LOCKED | +FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE ); + +rdmsrl (MSR_IA32_FEATURE_CONTROL , val); + +return ((val & sgx_lc_enabled_mask) == sgx_lc_enabled_mask); +} + +static bool vmx_sgx_lc_disabled_in_bios (struct kvm_vcpu *vcpu) +{ +return (to_vmx (vcpu)->msr_ia32_feature_control &FEATURE_CONTROL_LOCKED ) + && (!(to_vmx (vcpu)->msr_ia32_feature_control & +FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE )); +} + +#define SGX_INTEL_DEFAULT_LEPUBKEYHASH0 0xa6053e051270b7ac +#define SGX_INTEL_DEFAULT_LEPUBKEYHASH1 0x6cfbe8ba8b3b413d +#define SGX_INTEL_DEFAULT_LEPUBKEYHASH2 0xc4916d99f2b3735d +#define SGX_INTEL_DEFAULT_LEPUBKEYHASH3 0xd4f8c05909f9bb3b + +static void vmx_sgx_init_lepubkeyhash (struct kvm_vcpu *vcpu) +{ +u64 h0, h1, h2, h3; + +/* +* If runtime launch control is enabled (IA32_SGXLEPUBKEYHASHn is +* writable), we set guest's default value to be Intel's default +* hash (which is fixed value and can be hard-coded). Otherwise, +* guest can only use machine's IA32_SGXLEPUBKEYHASHn so set guest's +* default to that. +*/ +if (cpu_sgx_lepubkeyhash_writable ()) { + h0 =SGX_INTEL_DEFAULT_LEPUBKEYHASH0 ; + h1 =SGX_INTEL_DEFAULT_LEPUBKEYHASH1 ; + h2 =SGX_INTEL_DEFAULT_LEPUBKEYHASH2 ; + h3 =SGX_INTEL_DEFAULT_LEPUBKEYHASH3 ; + } +else { +rdmsrl (MSR_IA32_SGXLEPUBKEYHASH0 , h0); +rdmsrl (MSR_IA32_SGXLEPUBKEYHASH1 , h1); +rdmsrl (MSR_IA32_SGXLEPUBKEYHASH2 , h2); +rdmsrl (MSR_IA32_SGXLEPUBKEYHASH3 , h3); + } + +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[0 ] = h0; +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[1 ] = h1; +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[2 ] = h2; +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[3 ] = h3; +} + +static void vmx_sgx_lepubkeyhash_load (struct kvm_vcpu *vcpu) +{ +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH0 , +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[0 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH1 , +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[1 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH2 , +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[2 ]); +wrmsrl (MSR_IA32_SGXLEPUBKEYHASH3 , +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[3 ]); +} +/* * Switches to specified vcpu, until a matching vcpu_put(), but assumes * vcpu mutex is already taken. @@ -2316,6 +2383,14 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)vmx_vcpu_pi_load (vcpu, cpu); vmx->host_pkru =read_pkru (); + +/* +* Load guset's SGX LE pubkey hash if runtime launch control is +* enabled. +*/ +if (guest_cpuid_has_sgx_launch_control (vcpu) && +cpu_sgx_lepubkeyhash_writable ()) +vmx_sgx_lepubkeyhash_load (vcpu); }static void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu)@@ -3225,6 +3300,19 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_FEATURE_CONTROL : msr_info->data =to_vmx (vcpu)->msr_ia32_feature_control;break ; + case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3: +/* +* SDM 35.1 Model-Specific Registers, table 35-2. +* Read permitted if CPUID.0x12.0:EAX[0] = 1. (We have +* guaranteed this will be true if guest_cpuid_has_sgx +* is true.) +*/ +if (!guest_cpuid_has_sgx (vcpu)) +return 1 ; + msr_info->data = +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[msr_info->index - +MSR_IA32_SGXLEPUBKEYHASH0 ]; +break ; case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:if (!nested_vmx_allowed (vcpu))return 1 ;@@ -3344,6 +3432,37 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* SGX has been enabled in BIOS before using SGX. */ break ; + case MSR_IA32_SGXLEPUBKEYHASH0 ... MSR_IA32_SGXLEPUBKEYHASH3: +/* +* SDM 35.1 Model-Specific Registers, table 35-2. +* - If CPUID.0x7.0:ECX[30] = 1, FEATURE_CONTROL[17] is +* available. +* - Write permitted if CPUID.0x12.0:EAX[0] = 1 && +* FEATURE_CONTROL[17] = 1 && FEATURE_CONTROL[0] = 1. +*/ +if (!guest_cpuid_has_sgx (vcpu) || + !guest_cpuid_has_sgx_launch_control (vcpu)) +return 1 ; +/* +* Don't let userspace set guest's IA32_SGXLEPUBKEYHASHn, +* if machine's IA32_SGXLEPUBKEYHASHn cannot be changed at +* runtime. Note to_vmx(vcpu)->msr_ia32_sgxlepubkeyhash are +* set to default in vmx_create_vcpu therefore guest is able +* to get the machine's IA32_SGXLEPUBKEYHASHn by rdmsr in +* guest. +*/ +if (!cpu_sgx_lepubkeyhash_writable ()) +return 1 ; +/* +* If guest's FEATURE_CONTROL[17] is not set, guest's +* IA32_SGXLEPUBKEYHASHn are not writeable from guest. +*/ +if (!vmx_sgx_lc_disabled_in_bios (vcpu) && + !msr_info->host_initiated) +return 1 ; +to_vmx (vcpu)->msr_ia32_sgxlepubkeyhash[msr_index - +MSR_IA32_SGXLEPUBKEYHASH0 ] = data; +break ; case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:if (!msr_info->host_initiated)return 1 ;/* they are read-only */ @@ -9305,6 +9424,10 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) vmx->nested.vpid02 =allocate_vpid (); } +/* Set vcpu's default IA32_SGXLEPUBKEYHASHn */ +if (enable_sgx &&boot_cpu_has (X86_FEATURE_SGX_LAUNCH_CONTROL )) +vmx_sgx_init_lepubkeyhash (&vmx->vcpu); + vmx->nested.posted_intr_nv =-1 ; vmx->nested.current_vmptr =-1ull ; vmx->nested.current_vmcs12 =NULL ;
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 4 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host. Also, some day Intel may fix its architectural design flaw [1] by allowing EINIT to personalize the enclave's keying, and, if it's done by a new argument to EINIT instead of an MSR, KVM will have to trap EINIT to handle it.
Quoted 3 lines
One argument against this approach is KVM guest should never have impact on host side, meaning host should not be aware of such MSR change
As a somewhat generic comment, I don't like this approach to KVM development. KVM mucks with lots of important architectural control registers, and, in all too many cases, it tries to do so independently of the other arch/x86 code. This ends up causing all kinds of grief.
Can't KVM and the real x86 arch code cooperate for real? The host and the KVM code are in arch/x86 in the same source tree.
Quoted 2 lines
2. KVM should restore MSRs after changing for guest.
No, this is IMO silly. Don't restore it on each exit, in the user return hook, or anywhere else. Just make sure the host knows it was changed.
Quoted 7 lines
Another thing is, not quite related to selecting which approach above, and either we choose approach 1 or approach 2, KVM still suffers the performance loss of writing (and/or reading) to
IA32_SGXLEPUBKEYHASHnMSRs, either when vcpu scheduled in or during each VMEXIT/VMENTRY. Given the fact that theIA32_SGXLEPUBKEYHASHnwill only be used by EINIT, We can actually do some optimization by trapping EINIT from guest and only update MSRs in EINIT VMEXIT.
Yep.
Quoted 4 lines
But trapping ENCLS requires either 1) KVM to run ENCLS on hebalf of guest, in which case we have to reconstruct and remap guest's ENCLS parameters and skip the ENCLS for guest; 2) using MTF to let guest to run ENCLS again, while still trapping ENCLS.
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
If necessary for some reason, trap EINIT when the SGXLEPUBKEYKASH is wrong and then clear the exit flag once the MSRs are in sync. You'll need to be careful to avoid races in which the host's value leaks into the guest. I think you'll find that this is more complicated, less flexible, and less performant than just handling ENCLS[EINIT] directly in the host.
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Hi Andy,
Thanks for your helpful comments! See my reply below, and some questions as well.
Quoted 11 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 4 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing vmcs_load, writing to several MSRs, etc.
Quoted 10 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
Quoted 4 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
I am not very convinced why "what enclaves are allowed" in host would apply to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).
Quoted 4 lines
Also, some day Intel may fix its architectural design flaw [1] by allowing EINIT to personalize the enclave's keying, and, if it's done by a new argument to EINIT instead of an MSR, KVM will have to trap EINIT to handle it.
Looks this flaw is not the same issue as above (host enclave policy applies to guest)?
Quoted 5 lines
Quoted 4 lines
One argument against this approach is KVM guest should never have impact on host side, meaning host should not be aware of such MSR change
Quoted 7 lines
As a somewhat generic comment, I don't like this approach to KVM development. KVM mucks with lots of important architectural control registers, and, in all too many cases, it tries to do so independently of the other
arch/x86code. This ends up causing all kinds of grief.Can't KVM and the real x86 arch code cooperate for real? The host and the KVM code are in
arch/x86in the same source tree.
Currently on host SGX driver, which is pretty much self-contained, implements all SGX related staff.
Quoted 23 lines
Quoted 3 lines
2. KVM should restore MSRs after changing for guest.
No, this is IMO silly. Don't restore it on each exit, in the user return hook, or anywhere else. Just make sure the host knows it was changed.
Quoted 7 lines
Another thing is, not quite related to selecting which approach above, and either we choose approach 1 or approach 2, KVM still suffers the performance loss of writing (and/or reading) to
IA32_SGXLEPUBKEYHASHnMSRs, either when vcpu scheduled in or during each VMEXIT/VMENTRY. Given the fact that theIA32_SGXLEPUBKEYHASHnwill only be used by EINIT, We can actually do some optimization by trapping EINIT from guest and only update MSRs in EINIT VMEXIT.Yep.
Quoted 4 lines
But trapping ENCLS requires either 1) KVM to run ENCLS on hebalf of guest, in which case we have to reconstruct and remap guest's ENCLS parameters and skip the ENCLS for guest; 2) using MTF to let guest to run ENCLS again, while still trapping ENCLS.
Quoted 4 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
Quoted 7 lines
If necessary for some reason, trap EINIT when the SGXLEPUBKEYKASH is wrong and then clear the exit flag once the MSRs are in sync. You'll need to be careful to avoid races in which the host's value leaks into the guest. I think you'll find that this is more complicated, less flexible, and less performant than just handling ENCLS[EINIT] directly in the host.
Sorry I don't quite follow this part. Why would host's value leaks into guest? I suppose the *value* means host's IA32_SGXLEPUBKEYHASHn? guest's MSR read/write is always trapped and emulated by KVM.
Quoted 9 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Looks potentially only guest's IA32_SGXLEPUBKEYHASHn may be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?
Thanks,
-Kai
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 3 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.
I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 13 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 5 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
I think the SGX driver should probably live in arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).
Quoted 7 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 5 lines
I am not very convinced why "what enclaves are allowed" in host would apply to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).
I strongly disagree. I can imagine two classes of sensible policies for launch control:
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
Quoted 9 lines
Quoted 5 lines
Also, some day Intel may fix its architectural design flaw [1] by allowing EINIT to personalize the enclave's keying, and, if it's done by a new argument to EINIT instead of an MSR, KVM will have to trap EINIT to handle it.
Looks this flaw is not the same issue as above (host enclave policy applies to guest)?
It's related. Without this flaw, it might make sense to apply looser policy in the guest as in the host. With this flaw, I think your policy fails to have any real effect if you don't enforce it on guests.
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
One argument against this approach is KVM guest should never have impact on
host side, meaning host should not be aware of such MSR changeAs a somewhat generic comment, I don't like this approach to KVM development. KVM mucks with lots of important architectural control registers, and, in all too many cases, it tries to do so independently of the other
arch/x86code. This ends up causing all kinds of grief.
Quoted 6 lines
Quoted 2 lines
Can't KVM and the real x86 arch code cooperate for real? The host and the KVM code are in
arch/x86in the same source tree.Currently on host SGX driver, which is pretty much self-contained, implements all SGX related staff.
I will probably NAK this if it comes my way for inclusion upstream. Just because it can be self-contained doesn't mean it should be self-contained.
Quoted 7 lines
Quoted 5 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
Quoted 7 lines
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
I was partially mis-remembering how this worked. It looks like SIGSTRUCT and EINITTOKEN could be copied but SECS would have to be mapped. If KVM applied some policy to the launchable enclaves, it would want to make sure that it only looks at fields that are copied to make sure that the enclave that gets launched is the one it verified. The locking issue I'm imagining is that the SECS (or whatever else might be mapped) doesn't disappear and get reused for something else while it's mapped in the host. Presumably KVM has an existing mechanism for this, but maybe SECS is special because it's not quite normal memory IIRC.
Quoted 10 lines
Quoted 8 lines
If necessary for some reason, trap EINIT when the SGXLEPUBKEYKASH is wrong and then clear the exit flag once the MSRs are in sync. You'll need to be careful to avoid races in which the host's value leaks into the guest. I think you'll find that this is more complicated, less flexible, and less performant than just handling ENCLS[EINIT] directly in the host.
Quoted 3 lines
Sorry I don't quite follow this part. Why would host's value leaks into guest? I suppose the *value* means host's
IA32_SGXLEPUBKEYHASHn? guest's MSR read/write is always trapped and emulated by KVM.
You'd need to make sure that this sequence of events doesn't happen:
- Guest does EINIT and it exits.
- Host updates the MSRs and the ENCLS-exiting bitmap.
- Guest is preempted before it retries EINIT.
- A different host thread launches an enclave, thus changing the MSRs.
- Guest resumes and runs EINIT without exiting with the wrong MSR values.
Quoted 11 lines
Quoted 10 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Quoted 2 lines
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?
Not sure what you mean. Are you asking about the lack of guest personalization?
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 8 lines · Andy Lutomirski
Quoted 3 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Good speculation :) We've been told to expect that writing the hash MSRs will be at least 2.5x slower than normal MSRs.
Quoted 19 lines
Quoted 13 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 5 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
Quoted 4 lines
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).
Agreed, this would make life easier for future SGX code that can't be self-contained in the driver, e.g. EPC cgroup. Future architectural enhancements might also require tighter integration with the kernel.
Quoted 15 lines
Quoted 7 lines
Quoted 5 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
Quoted 7 lines
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
Quoted 11 lines
I was partially mis-remembering how this worked. It looks like SIGSTRUCT and EINITTOKEN could be copied but SECS would have to be mapped. If KVM applied some policy to the launchable enclaves, it would want to make sure that it only looks at fields that are copied to make sure that the enclave that gets launched is the one it verified. The locking issue I'm imagining is that the SECS (or whatever else might be mapped) doesn't disappear and get reused for something else while it's mapped in the host. Presumably KVM has an existing mechanism for this, but maybe SECS is special because it's not quite normal memory IIRC.
Mapping the SECS in the host should not be an issue, AFAIK there aren't any restrictions on the VA passed to EINIT as long as it resolves to a SECS page in the EPCM, e.g. the SGX driver maps the SECS for EINIT with an arbitrary VA.
I don't think emulating EINIT introduces any TOCTOU race conditions that wouldn't already exist. Evicting the SECS or modifying the page tables on a different thread while executing EINIT is either a guest kernel bug or bizarre behavior that the guest can already handle. Similarly, KVM would need special handling for evicting a guest's SECS, regardless of EINIT emulation.
Quoted 15 lines
Quoted 11 lines
Quoted 8 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?Not sure what you mean. Are you asking about the lack of guest personalization?
Quoted 9 lines
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
I know this issue has been raised internally as well, but I don't know the status of the situation. I'll follow up and provide any information I can.
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 28 lines · Sean J
Quoted 17 lines · Andy Lutomirski
On Thu, May 11, 2017 at 9:56 PM, Huang, Kai <[email protected]>
Quoted 9 lines
Quoted 8 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Quoted 3 lines
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?Not sure what you mean. Are you asking about the lack of guest personalization?
Quoted 9 lines
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
Quoted 3 lines
I know this issue has been raised internally as well, but I don't know the status of the situation. I'll follow up and provide any information I can.
So, the key players are well aware of the value added by per-VM keys, but, ultimately, shipping this feature is dependent on having strong requests from customers.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Christopherson, Sean JQuoted 32 lines · Sean J
Quoted 8 lines · Andy Lutomirski
Quoted 3 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Good speculation :) We've been told to expect that writing the hash MSRs will be at least 2.5x slower than normal MSRs.
Quoted 19 lines
Quoted 18 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
Quoted 37 lines
Quoted 4 lines
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Agreed, this would make life easier for future SGX code that can't be self-contained in the driver, e.g. EPC cgroup. Future architectural enhancements might also require tighter integration with the kernel.
Quoted 25 lines
Quoted 7 lines
Quoted 5 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
Quoted 7 lines
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
I was partially mis-remembering how this worked. It looks like SIGSTRUCT and EINITTOKEN could be copied but SECS would have to be mapped. If KVM applied some policy to the launchable enclaves, it would want to make sure that it only looks at fields that are copied to make sure that the enclave that gets launched is the one it verified. The locking issue I'm imagining is that the SECS (or whatever else might be mapped) doesn't disappear and get reused for something else while it's mapped in the host. Presumably KVM has an existing mechanism for this, but maybe SECS is special because it's not quite normal memory IIRC.
I am thinking we might not need to check value in SIGSTRUCT or EINITTOKEN, as KVM needs to emulate guest IA32_SGXLEPUBKEYHASHn write. If we decide we should apply host's policy to KVM guest, it seems we can do the check when trapping guest write to IA32_SGXLEPUBKEYHASHn, and only host approved values (may be a white-list or something?) are allowed to be written in the guest.
Thanks,
-Kai
Quoted 7 lines
Quoted 1 line
Mapping the SECS in the host should not be an issue, AFAIK there aren't any restrictions on the VA passed to EINIT as long as it resolves to a SECS page in the EPCM, e.g. the SGX driver maps the SECS for EINIT with an arbitrary VA.
Quoted 6 lines
I don't think emulating EINIT introduces any TOCTOU race conditions that wouldn't already exist. Evicting the SECS or modifying the page tables on a different thread while executing EINIT is either a guest kernel bug or bizarre behavior that the guest can already handle. Similarly, KVM would need special handling for evicting a guest's SECS, regardless of EINIT emulation.
Agreed.
Quoted 30 lines
Quoted 16 lines
Quoted 10 lines
Quoted 9 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Quoted 2 lines
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?Not sure what you mean. Are you asking about the lack of guest personalization?
Quoted 9 lines
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
I know this issue has been raised internally as well, but I don't know the status of the situation. I'll follow up and provide any information I can.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Christopherson, Sean JQuoted 32 lines · Sean J
Quoted 8 lines · Andy Lutomirski
Quoted 3 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Good speculation :) We've been told to expect that writing the hash MSRs will be at least 2.5x slower than normal MSRs.
Quoted 19 lines
Quoted 18 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
Quoted 8 lines
Quoted 4 lines
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Agreed, this would make life easier for future SGX code that can't be self-contained in the driver, e.g. EPC cgroup. Future architectural enhancements might also require tighter integration with the kernel.
I think this is better as well. In this way we can leverage SGX code more easily. Some SGX detection code can be done in arch/x86/ as well, so that other code can access, ex, SGX capabilities, quickly.
Another thing is actually SDK says SGX CPUID is per-thread, and we should not assume SGX CPUID will report the same info on all processors. I think it's better to check this as well. Moving SGX detection to identify_cpu can make this work more easily.
Thanks,
-Kai
Quoted 70 lines
Quoted 17 lines
Quoted 9 lines
Quoted 7 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
Quoted 7 lines
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
Quoted 11 lines
I was partially mis-remembering how this worked. It looks like SIGSTRUCT and EINITTOKEN could be copied but SECS would have to be mapped. If KVM applied some policy to the launchable enclaves, it would want to make sure that it only looks at fields that are copied to make sure that the enclave that gets launched is the one it verified. The locking issue I'm imagining is that the SECS (or whatever else might be mapped) doesn't disappear and get reused for something else while it's mapped in the host. Presumably KVM has an existing mechanism for this, but maybe SECS is special because it's not quite normal memory IIRC.
Mapping the SECS in the host should not be an issue, AFAIK there aren't any restrictions on the VA passed to EINIT as long as it resolves to a SECS page in the EPCM, e.g. the SGX driver maps the SECS for EINIT with an arbitrary VA.
I don't think emulating EINIT introduces any TOCTOU race conditions that wouldn't already exist. Evicting the SECS or modifying the page tables on a different thread while executing EINIT is either a guest kernel bug or bizarre behavior that the guest can already handle. Similarly, KVM would need special handling for evicting a guest's SECS, regardless of EINIT emulation.
Quoted 15 lines
Quoted 11 lines
Quoted 8 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?Not sure what you mean. Are you asking about the lack of guest personalization?
Quoted 9 lines
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
I know this issue has been raised internally as well, but I don't know the status of the situation. I'll follow up and provide any information I can.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Andy LutomirskiQuoted 47 lines · Andy Lutomirski
Quoted 3 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 13 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 5 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 7 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 5 lines
I am not very convinced why "what enclaves are allowed" in host would apply to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).
I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything works for KVM so lets skip this. Are we going to support allowing several LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.
I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we can create a KVM guest with runtime change to IA32_SGXLEPUBKEYHASHn either enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose. Actually I introduced below Qemu SGX parameters for creating guest:
-sgx epc=<size>,lehash='SHA-256 hash',lewr
where 'epc' specifies guest's EPC size, lehash specifies (initial) value of guest's IA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed to change guest's IA32_SGXLEPUBKEYHASHn at runtime.
If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to IA32_SGXLEPUBKEYHASHn disabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and has IA32_SGXLEPUBKEYHASHn with default value to be host allowed (single) hash.
If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change IA32_SGXLEPUBKEYHASHn to some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).
I'd like to hear comments from you guys.
Paolo, do you also have comments here from KVM's side?
Thanks,
-Kai
Quoted 114 lines
Quoted 10 lines
Quoted 6 lines
Also, some day Intel may fix its architectural design flaw [1] by allowing EINIT to personalize the enclave's keying, and, if it's done by a new argument to EINIT instead of an MSR, KVM will have to trap EINIT to handle it.
Looks this flaw is not the same issue as above (host enclave policy applies to guest)?
It's related. Without this flaw, it might make sense to apply looser policy in the guest as in the host. With this flaw, I think your policy fails to have any real effect if you don't enforce it on guests.
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
One argument against this approach is KVM guest should never have impact on
host side, meaning host should not be aware of such MSR changeAs a somewhat generic comment, I don't like this approach to KVM development. KVM mucks with lots of important architectural control registers, and, in all too many cases, it tries to do so independently of the other
arch/x86code. This ends up causing all kinds of grief.Quoted 6 lines
Quoted 2 lines
Can't KVM and the real x86 arch code cooperate for real? The host and the KVM code are in
arch/x86in the same source tree.Currently on host SGX driver, which is pretty much self-contained, implements all SGX related staff.
I will probably NAK this if it comes my way for inclusion upstream. Just because it can be self-contained doesn't mean it should be self-contained.
Quoted 7 lines
Quoted 5 lines
I would advocate for the former approach. (But you can't remap the parameters due to TOCTOU issues, locking, etc. Just copy them. I don't see why this is any more complicated than emulating any other instruction that accesses memory.)
Quoted 7 lines
No you cannot just copy. Because all address in guest's ENCLS parameters are guest's virtual address, we cannot use them to execute ENCLS in KVM. If any guest virtual addresses is used in ENCLS parameters, for example, PAGEINFO.SECS, PAGEINFO.SECINFO/PCMD, etc, you have to remap them to KVM's virtual address.
Btw, what is TOCTOU issue? would you also elaborate locking issue?
I was partially mis-remembering how this worked. It looks like SIGSTRUCT and EINITTOKEN could be copied but SECS would have to be mapped. If KVM applied some policy to the launchable enclaves, it would want to make sure that it only looks at fields that are copied to make sure that the enclave that gets launched is the one it verified. The locking issue I'm imagining is that the SECS (or whatever else might be mapped) doesn't disappear and get reused for something else while it's mapped in the host. Presumably KVM has an existing mechanism for this, but maybe SECS is special because it's not quite normal memory IIRC.
Quoted 10 lines
Quoted 8 lines
If necessary for some reason, trap EINIT when the SGXLEPUBKEYKASH is wrong and then clear the exit flag once the MSRs are in sync. You'll need to be careful to avoid races in which the host's value leaks into the guest. I think you'll find that this is more complicated, less flexible, and less performant than just handling ENCLS[EINIT] directly in the host.
Quoted 3 lines
Sorry I don't quite follow this part. Why would host's value leaks into guest? I suppose the *value* means host's
IA32_SGXLEPUBKEYHASHn? guest's MSR read/write is always trapped and emulated by KVM.You'd need to make sure that this sequence of events doesn't happen:
- Guest does EINIT and it exits.
- Host updates the MSRs and the ENCLS-exiting bitmap.
- Guest is preempted before it retries EINIT.
- A different host thread launches an enclave, thus changing the MSRs.
- Guest resumes and runs EINIT without exiting with the wrong MSR values.Quoted 11 lines
Quoted 10 lines
[1] Guests that steal sealed data from each other or from the host can manipulate that data without compromising the hypervisor by simply loading the same enclave that its rightful owner would use. If you're trying to use SGX to protect your crypto credentials so that, if stolen, they can't be used outside the guest, I would consider this to be a major flaw. It breaks the security model in a multi-tenant cloud situation. I've complained about it before.
Quoted 2 lines
Looks potentially only guest's
IA32_SGXLEPUBKEYHASHnmay be leaked? In this case even it is leaked looks we cannot dig anything out just the hash value?Not sure what you mean. Are you asking about the lack of guest personalization?
Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!
Paolo Bonzini
maintainerRe: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 20 lines · Kai
If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).
Quoted 3 lines
I'd like to hear comments from you guys.
Paolo, do you also have comments here from KVM's side?
I would start with read-only LE hash (same as the host), which is a valid configuration anyway. Then later we can trap EINIT to emulate IA32_SGXLEPUBKEYHASHn.
Paolo
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 23 lines · Paolo Bonzini
Quoted 20 lines · Kai
If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).
Quoted 7 lines
Quoted 3 lines
I'd like to hear comments from you guys.
Paolo, do you also have comments here from KVM's side?
I would start with read-only LE hash (same as the host), which is a valid configuration anyway. Then later we can trap EINIT to emulate
IA32_SGXLEPUBKEYHASHn.
You mean we can start with creating guest without Qemu 'lewr' parameter support, and always disallowing guest to change IA32_SGXLEPUBKEYHASHn?
Even in this way, KVM still needs to emulate IA32_SGXLEPUBKEYHASHn (just allow MSR reading but not writing), and write guest's value to physical MSRs when running guest (trapping EINIT and write MSRs during EINIT is really just performance optimization). Because host can run multiple LEs and change MSRs. Your suggestion only works when runtime change to IA32_SGXLEPUBKEYHASHn is disabled on host (meaning physical machine).
Thanks,
-Kai
Quoted 3 lines
Paolo
Paolo Bonzini
maintainerRe: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 5 lines · Kai
Quoted 4 lines
I would start with read-only LE hash (same as the host), which is a valid configuration anyway. Then later we can trap EINIT to emulate
IA32_SGXLEPUBKEYHASHn.
Quoted 7 lines
You mean we can start with creating guest without Qemu 'lewr' parameter support, and always disallowing guest to change
IA32_SGXLEPUBKEYHASHn? Even in this way, KVM still needs to emulateIA32_SGXLEPUBKEYHASHn(just allow MSR reading but not writing), and write guest's value to physical MSRs when running guest (trapping EINIT and write MSRs during EINIT is really just performance optimization). Because host can run multiple LEs and change MSRs.
Oh, I didn't know this. So I guess there isn't much benefit in skipping the trapping of EINIT.
Paolo
Quoted 2 lines
Your suggestion only works when runtime change to
IA32_SGXLEPUBKEYHASHnis disabled on host (meaning physical machine).
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 93 lines · Kai
Quoted 59 lines · Andy Lutomirski
Quoted 5 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 14 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 6 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 8 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 7 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything works for KVM so lets skip this. Are we going to support allowing several LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.
I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we can create a KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHneither enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose. Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of guest's
IA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.
Quoted 9 lines
If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).
I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written. Also, the only way to fail an MSR write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs. ENCLS[EINIT], on the other hand, returns an actual error code. I'm not sure that a sensible error code exists ("SGX_HYPERVISOR_SAID_NO?", perhaps), but SGX_INVALID_EINITTOKEN seems to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return SGX_INVALID_EINITTOKEN if the guest tries to EINIT an LE that isn't in the list.
--Andy
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 116 lines · Andy Lutomirski
Quoted 93 lines · Kai
Quoted 59 lines · Andy Lutomirski
Quoted 5 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 14 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 6 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 8 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 7 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything works for KVM so lets skip this. Are we going to support allowing several LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.
I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we can create a KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHneither enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose. Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of guest's
IA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 9 lines
If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's IA32_SGXLEPUBKEYHASHn disabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.
We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 4 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 2 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),
Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but SGX_INVALID_EINITTOKEN seems
Quoted 6 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.
But this would be inconsistent with HW behavior. If the hash value in guest's IA32_SGXLEPUBKEYHASHn is matched with the one passed by EINIT, EINIT is not supposed to return SGX_INVALID_EINITTOKEN.
I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Quoted 3 lines
--Andy
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 153 lines · Kai
Quoted 116 lines · Andy Lutomirski
Quoted 93 lines · Kai
Quoted 59 lines · Andy Lutomirski
Quoted 5 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 14 lines
Quoted 11 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 6 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 8 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 7 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything works for KVM so lets skip this. Are we going to support allowing several LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.
I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we can create a KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHneither enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose. Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of guest's
IA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 9 lines
If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 4 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 2 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 6 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.
Quoted 5 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Thanks,
-Kai
Quoted 4 lines
Quoted 4 lines
--Andy
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 229 lines · Kai
Quoted 209 lines · Kai
Quoted 152 lines · Andy Lutomirski
Quoted 120 lines · Kai
Quoted 74 lines · Andy Lutomirski
Quoted 7 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 19 lines
Quoted 8 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 7 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 9 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 9 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change toIA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 15 lines
If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 5 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 3 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 7 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Quoted 5 lines
Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
I'm not at all convinced that, going forward, Linux's host-side launch control policy will be entirely contained in the LE. I'm also not convinced that non-Linux guests will function at all under this type of policy -- what if FreeBSD's LE is different for whatever reason?
Quoted 14 lines
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Thanks,
-KaiQuoted 4 lines
Quoted 4 lines
--Andy
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 232 lines · Andy Lutomirski
Quoted 231 lines · Kai
Quoted 209 lines · Kai
Quoted 152 lines · Andy Lutomirski
Quoted 120 lines · Kai
Quoted 74 lines · Andy Lutomirski
Quoted 7 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 19 lines
Quoted 8 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 7 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 9 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 9 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change toIA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 15 lines
If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 5 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 3 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 7 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Andy,
Quoted 8 lines
Quoted 3 lines
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
I'm not at all convinced that, going forward, Linux's host-side launch control policy will be entirely contained in the LE. I'm also not convinced that non-Linux guests will function at all under this type of policy -- what if FreeBSD's LE is different for whatever reason?
I am not convinced either. I think we need Jarkko to elaborate how is host side launch control policy implemented, or is there any policy at all. I also tried to read SGX driver code but looks I couldn't find any implementation regarding to this.
Hi Jarkko,
Can you elaborate on this?
Thanks,
-Kai
Quoted 16 lines
Quoted 15 lines
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Thanks,
-KaiQuoted 4 lines
Quoted 4 lines
--Andy
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 253 lines · Kai
Quoted 30 lines · Andy Lutomirski
Quoted 29 lines · Kai
Quoted 26 lines · Kai
Quoted 22 lines · Andy Lutomirski
Quoted 18 lines · Kai
Quoted 14 lines · Andy Lutomirski
Quoted 6 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing to several MSRs, etc.Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 3 lines
Quoted 3 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH
along
Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 87 lines
Quoted 54 lines
Quoted 16 lines
Quoted 5 lines
with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 7 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 9 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 9 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHnQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
disabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared andhas
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
IA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto
Quoted 71 lines
Quoted 71 lines
Quoted 71 lines
Quoted 23 lines
Quoted 9 lines
some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 5 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 3 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 4 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT,EINIT
Quoted 17 lines
Quoted 17 lines
Quoted 6 lines
is not supposed to return
SGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Andy,
Quoted 8 lines
Quoted 3 lines
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
I'm not at all convinced that, going forward, Linux's host-side launch control policy will be entirely contained in the LE. I'm also not convinced that non-Linux guests will function at all under this type of policy -- what if FreeBSD's LE is different for whatever reason?
I am not convinced either. I think we need Jarkko to elaborate how is host side launch control policy implemented, or is there any policy at all. I also tried to read SGX driver code but looks I couldn't find any implementation regarding to this.
Quoted 6 lines
Hi Jarkko,
Can you elaborate on this?
Thanks,
-Kai
I don't think the kernel's LE policy is relevant here.
As long as you allow the guest OS to set the IA32_SGXLEPUBKEYHASHn MSRs, either directly or by the VMM 'lehash' value, you don't need a support for more than on LE in the kernel. The host kernel will have one LE and the guest kernel has another "one" LE that may have a different hash value.
I agree with Andy that different OS distributions are likely to have different LE hash values - so the guest OS may require different hash setting.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Thanks,
-KaiQuoted 4 lines
Quoted 4 lines
--Andy
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 216 lines · Kai
Quoted 191 lines · Kai
Quoted 141 lines · Andy Lutomirski
Quoted 111 lines · Kai
Quoted 67 lines · Andy Lutomirski
Quoted 6 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 18 lines
Quoted 8 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to
the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 7 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 8 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 9 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change toIA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 15 lines
If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 4 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 2 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 6 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
Quoted 7 lines
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Thanks,
-Kai
So. I have pass through LE. It creates EINITTOKEN for anything. Couldn't VMM keep virtual values for MSRs and ask host side LE create token when it needs to?
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 222 lines · Jarkko Sakkinen
Quoted 221 lines · Kai
Quoted 191 lines · Kai
Quoted 141 lines · Andy Lutomirski
Quoted 111 lines · Kai
Quoted 67 lines · Andy Lutomirski
Quoted 6 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 18 lines
Quoted 8 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to
the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 7 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 8 lines
Quoted 5 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 9 lines
I am not very convinced why "what enclaves are allowed" in host would apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change toIA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 15 lines
If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 4 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 2 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 6 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Quoted 6 lines
Quoted 2 lines
Thanks,
-KaiSo. I have pass through LE. It creates EINITTOKEN for anything. Couldn't VMM keep virtual values for MSRs and ask host side LE create token when it needs to?
Hi Jarkko,
Thanks for replying. VMM doesn't need driver to generate EINITTOKEN. The EINITTOKEN is from guest too, upon VMM traps EINIT.
I think Andy's comments is, if host SGX driver only allows someone's LE to run (ex, LE from Intel, Redhat, etc...), then SGX driver should also govern LEs from KVM guests as well, by checking SIGSTRUCT from KVM guest (the SIGSTRUCT is provided by KVM by trapping guest's EINIT).
In my understanding, although you only allows one LE in kernel, but you won't limit who's LE can be run (basically kernel can run LE signed by anyone, but just one LE when kernel is running), so I don't see there is any limitation to KVM guests here.
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);
for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Am I understanding correctly? Does this make sense to you?
Thanks,
-Kai
Quoted 3 lines
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 292 lines · Kai
Quoted 261 lines · Jarkko Sakkinen
Quoted 259 lines · Kai
Quoted 225 lines · Kai
Quoted 167 lines · Andy Lutomirski
Quoted 134 lines · Kai
Quoted 83 lines · Andy Lutomirski
Quoted 7 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely*
slow, as when vcpu is schedule in, KVM is already doingvmcs_load, writing
to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 21 lines
Quoted 10 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along
with whatever lock is needed (probably just a mutex). Users of EINIT
will take the mutex, compare the percpu variable to the desired value,
and, if it's different, do WRMSR and update the percpu variable.Quoted 7 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory
state but *not* changing the MSRs. KVM will trap and emulate EINIT to
support the same handling as the host. There is no action required at
all on KVM guest entry and exit.Quoted 9 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to
have,
but I am not sure whether it is mandatory, as IMO EINIT is not even in
performance critical path. We can simply read old value from MSRs out and
compare whether the old equals to the new.I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the
main kernel image, not from a module).Quoted 11 lines
Quoted 7 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for
other reasons: someone is going to want to implement policy for what
enclaves are allowed that applies to guests as well as the host.Quoted 10 lines
I am not very convinced why "what enclaves are allowed" in host would
apply
to guest. Can you elaborate? I mean in general virtualization just focus
emulating hardware behavior. If a native machine is able to run any LE,
the
virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 7 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of
restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?What's the current SGX driver launch control policy? Yes allow everything
works for KVM so lets skip this. Are we going to support allowing several
LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE
staff but I don't know details.I am trying to find a way that we can both not break host launch control
policy, and be consistent to HW behavior (from guest's view). Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither
enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed
to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only
allows to create KVM guest with runtime change toIA32_SGXLEPUBKEYHASHn
disabled, so that only host allowed (single) hash can be used by guest. From
guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 16 lines
If host allows several LEs (not but everything), and if we create guest with
'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to
tell guest that you are only allowed to changeIA32_SGXLEPUBKEYHASHnto some
specific values. One compromise solution is we don't allow to create guest
with 'lewr' specified, and at the meantime, only allow to create guest with
host approved hashes specified in 'lehash'. This will make guest's behavior
consistent to HW behavior but only allows guest to run one LE (which is
specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.
Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 5 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 3 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 7 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I
am not sure whether he is going to extend in the future or not. I think this
might also depend on requirements from customers.Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you comments more?
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Quoted 7 lines
Quoted 2 lines
Thanks,
-KaiSo. I have pass through LE. It creates EINITTOKEN for anything. Couldn't VMM keep virtual values for MSRs and ask host side LE create token when it needs to?
Hi Jarkko,
Thanks for replying. VMM doesn't need driver to generate EINITTOKEN. The EINITTOKEN is from guest too, upon VMM traps EINIT.
I think Andy's comments is, if host SGX driver only allows someone's LE to run (ex, LE from Intel, Redhat, etc...), then SGX driver should also govern LEs from KVM guests as well, by checking SIGSTRUCT from KVM guest (the SIGSTRUCT is provided by KVM by trapping guest's EINIT).
In my understanding, although you only allows one LE in kernel, but you won't limit who's LE can be run (basically kernel can run LE signed by anyone, but just one LE when kernel is running), so I don't see there is any limitation to KVM guests here.
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);
Quoted 7 lines
for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Am I understanding correctly? Does this make sense to you?
My understanding is that the kernel will not (at least initially) allow users to supply an LE. The kernel will handle launches all by itself. How it does so is an implementation detail, and it seems to me that it will cause compatibility issues if guests have to use the host's LE.
sgx_validate_sigstruct(...) sounds like it could be a good approach to me.
Quoted 8 lines
Thanks,
-KaiQuoted 3 lines
/Jarkko
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 271 lines · Andy Lutomirski
Quoted 223 lines · Kai
Quoted 220 lines · Jarkko Sakkinen
Quoted 218 lines · Kai
Quoted 197 lines · Kai
Quoted 139 lines · Andy Lutomirski
Quoted 110 lines · Kai
Quoted 68 lines · Andy Lutomirski
Quoted 5 lines · Kai
I am not sure whether the cost of writing to 4 MSRs would be *extremely* slow, as when vcpu is schedule in, KVM is already doing
vmcs_load, writing to several MSRs, etc.I'm speculating that these MSRs may be rather unoptimized and hence unusualy slow.
Quoted 17 lines
Quoted 8 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
Quoted 5 lines
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 6 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).Quoted 10 lines
Quoted 6 lines
FWIW, I think that KVM will, in the long run, want to trap EINIT for other reasons: someone is going to want to implement policy for what enclaves are allowed that applies to guests as well as the host.
Quoted 6 lines
I am not very convinced why "what enclaves are allowed" in host would apply to guest. Can you elaborate? I mean in general virtualization just focus emulating hardware behavior. If a native machine is able to run any LE, the virtual machine should be able to as well (of course, with guest's IA32_FEATURE_CONTROL[bit 17] set).
I strongly disagree. I can imagine two classes of sensible policies for launch control:
Quoted 6 lines
1. Allow everything. This seems quite sensible to me.
2. Allow some things, and make sure that VMs have at least as restrictive a policy as host root has. After all, what's the point of restricting enclaves in the host if host code can simply spawn a little VM to run otherwise-disallowed enclaves?
What's the current SGX driver launch control policy? Yes allow everything works for KVM so lets skip this. Are we going to support allowing several LEs, or just allowing one single LE? I know Jarkko is doing in-kernel LE staff but I don't know details.
I am trying to find a way that we can both not break host launch control policy, and be consistent to HW behavior (from guest's view).
Currently we
can create a KVM guest with runtime change toIA32_SGXLEPUBKEYHASHneither enabled or disabled. I introduced an Qemu parameter 'lewr' for this purpose.
Actually I introduced below Qemu SGX parameters for creating guest:-sgx epc=<size>,lehash='SHA-256 hash',lewrwhere 'epc' specifies guest's EPC size, lehash specifies (initial) value of
guest'sIA32_SGXLEPUBKEYHASHn, and 'lewr' specifies whether guest is allowed to change guest'sIA32_SGXLEPUBKEYHASHnat runtime.If host only allows one single LE to run, KVM can add a restrict that only allows to create KVM guest with runtime change to
IA32_SGXLEPUBKEYHASHndisabled, so that only host allowed (single) hash can be used by guest. From guest's view, it simply has IA32_FEATURE_CONTROL[bit17] cleared and hasIA32_SGXLEPUBKEYHASHnwith default value to be host allowed (single) hash.Quoted 11 lines
If host allows several LEs (not but everything), and if we create guest with 'lewr', then the behavior is not consistent with HW behavior, as from guest's hardware's point of view, we can actually run any LE but we have to tell guest that you are only allowed to change
IA32_SGXLEPUBKEYHASHnto some specific values. One compromise solution is we don't allow to create guest with 'lewr' specified, and at the meantime, only allow to create guest with host approved hashes specified in 'lehash'. This will make guest's behavior consistent to HW behavior but only allows guest to run one LE (which is specified by 'lehash' when guest is created).I'm not sure I entirely agree for a couple reasons.
1. I wouldn't be surprised if the kernel ends up implementing a policy in which it checks all enclaves (not just LEs) for acceptability. In fact, if the kernel sticks with the "no LE at all or just kernel-internal LE", then checking enclaves directly against some
admin- or distro-provided signer list seems reasonable. This type of policy can't be forwarded to a guest by restricting allowed LE signers. But this is mostly speculation since AFAIK no one has seriously proposed any particular policy support and the plan was to not have this for the initial implementation.Quoted 5 lines
2. While matching hardware behavior is nice in principle, there doesn't seem to be useful hardware behavior to match here. If the host had a list of five allowed LE signers, how exactly would it restrict the MSRs? They're not written atomically, so you can't directly tell what's being written.
In this case I actually plan to just allow creating guest with guest's
IA32_SGXLEPUBKEYHASHndisabled (without 'lewr' specified). If 'lewr' is specified, creating guest will fail. And we only allow creating guest with host allowed hash values (with 'lehash=hash-value'), and if 'hash-value' specified by 'lehash' is not allowed by host, we also fail to create guest.We can only allow creating guest with 'lewr' specified when host allows anything.
But in this way, we are restricting guest OS's ability to run LE, as only one LE, that is specified by 'lehash' parameter, can be run. But I think this won't hurt much, as multiple guests still are able to run different LEs?
Also, the only way to fail an MSR
Quoted 5 lines
write is to send #GP, and Windows (and even Linux) may not expect that. Linux doesn't panic due to #GP on MSR writes these days, but you still get a big fat warning. I wouldn't be at all surprised if Windows BSODs.
We cannot allow writing some particular value to MSRs successfully, while injecting #GP when writing other values to the same MSRs. So #GP is not option.
ENCLS[EINIT], on the other hand, returns an actual
Quoted 3 lines
error code. I'm not sure that a sensible error code exists ("
SGX_HYPERVISOR_SAID_NO?", perhaps),Looks no such error code exists. And we cannot return such error code to guest as such error code is only supposed to be valid when ENCLS is run in hypervisor.
but
SGX_INVALID_EINITTOKENseemsQuoted 7 lines
to mean, more or less, "the CPU thinks you're not authorized to do this", so forcing that error code could be entirely reasonable.
If the host policy is to allow a list of LE signers, you could return
SGX_INVALID_EINITTOKENif the guest tries to EINIT an LE that isn't in the list.Quoted 8 lines
But this would be inconsistent with HW behavior. If the hash value in guest's
IA32_SGXLEPUBKEYHASHnis matched with the one passed by EINIT, EINIT is not supposed to returnSGX_INVALID_EINITTOKEN.I think from VMM's perspective, emulating HW behavior to be consistent with real HW behavior is very important.
Paolo, would you provide your comments?
Hi all,
This has been quite for a while and I'd like to start discussion again. Jarkko told me that currently he only supports one LE in SGX driver, but I am not sure whether he is going to extend in the future or not. I think this might also depend on requirements from customers.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Andy,
If we only support one LE in driver, then we can only support the same LE for all KVM guests, according to your comments that host kernel launch control policy should also apply to KVM guests? WOuld you
comments more?
Quoted 40 lines
Quoted 6 lines
Quoted 6 lines
Jarkko,
Could you help to clarify the whole launch control policy in host side so that we can have a better understanding together?
Quoted 7 lines
Quoted 2 lines
Thanks,
-KaiSo. I have pass through LE. It creates EINITTOKEN for anything. Couldn't VMM keep virtual values for MSRs and ask host side LE create token when it needs to?
Hi Jarkko,
Thanks for replying. VMM doesn't need driver to generate EINITTOKEN. The EINITTOKEN is from guest too, upon VMM traps EINIT.
I think Andy's comments is, if host SGX driver only allows someone's LE to run (ex, LE from Intel, Redhat, etc...), then SGX driver should also govern LEs from KVM guests as well, by checking SIGSTRUCT from KVM guest (the SIGSTRUCT is provided by KVM by trapping guest's EINIT).
In my understanding, although you only allows one LE in kernel, but you won't limit who's LE can be run (basically kernel can run LE signed by anyone, but just one LE when kernel is running), so I don't see there is any limitation to KVM guests here.
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Quoted 9 lines
Quoted 1 line
Am I understanding correctly? Does this make sense to you?
My understanding is that the kernel will not (at least initially) allow users to supply an LE. The kernel will handle launches all by itself. How it does so is an implementation detail, and it seems to me that it will cause compatibility issues if guests have to use the host's LE.
sgx_validate_sigstruct(...)sounds like it could be a good approach to me.
I don't think the kernel needs to support more than one LE.
For my understanding the guest kernel may include different LE and will require different LE hash, either by configuring 'lehash' when launching the guest, or by the guest kernel setting the hash value. I assume that the VMM should not block this MSR writes and allow the guest to set the hash values for its own LE (by caching the MSR writes and setting the values on EINIT).
So I don't see why the host kernel should provide EINITTOKEN for the guests.
Moreover, I guess that the approach of LE providing tokens to any enclave is only the initial implementation, and different distributions may decide to add some logic to the token generation, so it might be that the host LE or the guest LE will not provide token to some enclave, and I don't think we'll want to override this kernel logic.
Quoted 8 lines
Quoted 8 lines
Thanks,
-KaiQuoted 3 lines
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 13 lines · Kai
In my understanding, although you only allows one LE in kernel, but you won't limit who's LE can be run (basically kernel can run LE signed by anyone, but just one LE when kernel is running), so I don't see there is any limitation to KVM guests here.
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Quoted 6 lines
Andy,
Am I understanding correctly? Does this make sense to you?
Thanks,
-Kai
Nope. I don't even understand the *beginnings* what that function would do. I don't understand what the validation means here and what VMM would do if that functions reports "success".
How that would work on a system where MSRs cannot be changed?
In that kind of system the host OS must generate EINITTOKEN for the LE running on inside the guest and maintain completely virtualized MSR values for the guest.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 16 lines · Jarkko Sakkinen
Quoted 15 lines · Kai
In my understanding, although you only allows one LE in kernel, but you won't limit who's LE can be run (basically kernel can run LE signed by anyone, but just one LE when kernel is running), so I don't see there is any limitation to KVM guests here.
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Quoted 8 lines
Quoted 4 lines
Am I understanding correctly? Does this make sense to you?
Thanks,
-KaiNope. I don't even understand the *beginnings* what that function would do. I don't understand what the validation means here and what VMM would do if that functions reports "success".
The validation means either the sigstruct->modulus or SHA256(sigstruct->modulus) should be in a 'approved white-list' maintained by kernel (which I know doesn't exist now, but Andy some kind suggested we may or should have, in the future I guess), otherwise the function returns error to indicate the LE from guest is "unapproved by host kernel/driver".
Andy, would you explain here?
Quoted 2 lines
How that would work on a system where MSRs cannot be changed?
This is simple, we simply won't allow guest to choose its own IA32_SGXLEPUBKEYHASHn by specifying 'lehash' value in Qemu parameter when creating the guest.
To elaborate, currently in my design Qemu has below new parameters to support SGX:
# qemu-system-x86_64 -sgx, epc=<size>,lehash=<sha-256 hash>,lewr
The 'epc=<size>' specifies guest's EPC size obviously, lehash specifies guest's initial IA32_SGXLEPUBKEYHASHn (similar to the value configured in BIOS for real machine), and 'lewr' specifies whether guest's IA32_SGXLEPUBKEYHASHn can be changed by OS at runtime. The 'lehash' and 'lewr' are optional.
If MSRs cannot be changed on physical machine, then we will fail to create guest if either 'lehash' or 'lewr' is specified when creating the guest.
Quoted 4 lines
In that kind of system the host OS must generate EINITTOKEN for the LE running on inside the guest and maintain completely virtualized MSR values for the guest.
The host OS will not generate EINITTOKEN for guest in any circumstances, as EINITTOKEN will always be from guest's EINIT instruction. KVM traps EINIT from guest and gets both SIGSTRUCT and EINITTOKEN from the EINIT leaf, update MSRs, and run EINIT on behalf of guest.
Btw the purpose for KVM to trap EINIT is to update guest's virtual IA32_SGXLEPUBKEYHASHn to physical MSRs, before running EINIT. In fact KVM even doesn't need to trap EINIT but simply updating guest's MSRs to real MSRs when vcpu is scheduled in, if SGX driver can update host LE's hash to MSRs before EINIT in host. KVM are not trying to guarantee running EINIT successfully here, but simply to emulate guest's IA32_SGXLEPUBKEYHASHn and EINIT in the guest.
Thanks,
-Kai
Quoted 3 lines
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 19 lines · Kai
Quoted 10 lines
Quoted 10 lines
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Quoted 8 lines
Quoted 4 lines
Am I understanding correctly? Does this make sense to you?
Thanks,
-KaiNope. I don't even understand the *beginnings* what that function would do. I don't understand what the validation means here and what VMM would do if that functions reports "success".
Quoted 7 lines
The validation means either the
sigstruct->modulusorSHA256(sigstruct->modulus)should be in a 'approved white-list' maintained by kernel (which I know doesn't exist now, but Andy some kind suggested we may or should have, in the future I guess), otherwise the function returns error to indicate the LE from guest is "unapproved by host kernel/driver".Andy, would you explain here?
That can be considered but I still have zero idea what this function is and what its relation to whitelist would be.
Quoted 5 lines
Quoted 1 line
How that would work on a system where MSRs cannot be changed?
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.
Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
Quoted 15 lines
To elaborate, currently in my design Qemu has below new parameters to support SGX:
# qemu-system-x86_64 -sgx, epc=<size>,lehash=<sha-256 hash>,lewrThe 'epc=<size>' specifies guest's EPC size obviously, lehash specifies guest's initial
IA32_SGXLEPUBKEYHASHn(similar to the value configured in BIOS for real machine), and 'lewr' specifies whether guest'sIA32_SGXLEPUBKEYHASHncan be changed by OS at runtime. The 'lehash' and 'lewr' are optional.If MSRs cannot be changed on physical machine, then we will fail to create guest if either 'lehash' or 'lewr' is specified when creating the guest.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
In that kind of system the host OS must generate EINITTOKEN for the LE running on inside the guest and maintain completely virtualized MSR values for the guest.
The host OS will not generate EINITTOKEN for guest in any circumstances, as EINITTOKEN will always be from guest's EINIT instruction. KVM traps EINIT from guest and gets both SIGSTRUCT and EINITTOKEN from the EINIT leaf, update MSRs, and run EINIT on behalf of guest.
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 26 lines · Jarkko Sakkinen
Quoted 25 lines · Kai
Quoted 10 lines
Quoted 10 lines
But it may still be better if SGX driver can provide function like:
int sgx_validate_sigstruct(struct sigstruct *sig);for KVM to call, in case driver is changed (ex, to only allows LEs from some particular ones to run), but this is not necessary now. KVM changes can be done later when driver make the changes.
Andy,
Quoted 8 lines
Quoted 4 lines
Am I understanding correctly? Does this make sense to you?
Thanks,
-KaiNope. I don't even understand the *beginnings* what that function would do. I don't understand what the validation means here and what VMM would do if that functions reports "success".
The validation means either the
sigstruct->modulusorSHA256(sigstruct->modulus)should be in a 'approved white-list' maintained by kernel (which I know doesn't exist now, but Andy some kind suggested we may or should have, in the future I guess), otherwise the function returns error to indicate the LE from guest is "unapproved by host kernel/driver".
Quoted 4 lines
Quoted 1 line
Andy, would you explain here?
That can be considered but I still have zero idea what this function is and what its relation to whitelist would be.
The relation is this function only returns success when sigstruct->modulus (or SHA256(sigstruct->modulus)) matches the value in the 'white-list'...
VMM does nothing if the function returns success, it continues to run EINIT on behalf of guest, and inject the result to guest (either success or failure, VMM doesn't care the result but need to report the result to guest to reflect HW behavior).
If this function returns error, VMM needs to report some error code to guest. VMM even doesn't need to run EINIT anymore.
But IMO we don't have add this function now. If your driver choose to have such 'white-list', we can do this in the future.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
How that would work on a system where MSRs cannot be changed?
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Virtualization is only about to emulate hardware's behavior, but not to assume, or depend on, or change guest's SW behavior. We are not trying to make sure EINIT will run successfully in guest, instead, we are trying to make sure EINIT will be just the same behavior as it runs on physical machine -- either success or failure, according to guest's sigstruct and token.
If EINIT in guest is supposed to fail (ex, incorrect token is generated by guest's LE), KVM will need to inject the supposed error to guest, to reflect the real hardware behavior. One more example, if KVM even chooses not to trap EINIT, how can you provide token generated by host LE to guest? You simply cannot. Host LE will never generate token for guest, in any circumstance.
Quoted 20 lines
Quoted 20 lines
To elaborate, currently in my design Qemu has below new parameters to support SGX:
# qemu-system-x86_64 -sgx, epc=<size>,lehash=<sha-256 hash>,lewrThe 'epc=<size>' specifies guest's EPC size obviously, lehash specifies guest's initial
IA32_SGXLEPUBKEYHASHn(similar to the value configured in BIOS for real machine), and 'lewr' specifies whether guest'sIA32_SGXLEPUBKEYHASHncan be changed by OS at runtime. The 'lehash' and 'lewr' are optional.If MSRs cannot be changed on physical machine, then we will fail to create guest if either 'lehash' or 'lewr' is specified when creating the guest.
Quoted 4 lines
In that kind of system the host OS must generate EINITTOKEN for the LE running on inside the guest and maintain completely virtualized MSR values for the guest.
Quoted 8 lines
Quoted 4 lines
The host OS will not generate EINITTOKEN for guest in any circumstances, as EINITTOKEN will always be from guest's EINIT instruction. KVM traps EINIT from guest and gets both SIGSTRUCT and EINITTOKEN from the EINIT leaf, update MSRs, and run EINIT on behalf of guest.
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
And let me explain the purpose of trapping EINIT again here.
When guest is about to run EINIT, if guest's SHA256(sigstruct->modulus) matches guest's virtual IA32_SGXLEPUBKEYHASHn (and if others are correctly populated in sigstruct and token as well), KVM needs to make sure that EINIT will run successfully in guest, even physical IA32_SGXLEPUBKEYHASHn are not equal to guest's virtual MSRs at this particular time. This is because given the same condition, the EINIT will run successfully on physical machine. KVM needs to emulate the right HW behavior.
How to make sure guest's EINIT will run successfully in this case? We need to update guest's virtual MSRs to physical MSRs before guest runs EINIT. The whole purpose for KVM to trap EINIT from guest, is to guarantee that before EINIT, KVM can update guest's virtual MSRs to physical MSRs.
Like I said before, KVM even doesn't need to trap EINIT for this purpose, ex, KVM can update guest's virtual MSRs to real MSRs when vcpu is scheduled in (which is exactly what I did in this patch, btw), but this is considered to be performance unfriendly, so Andy, Sean suggested we should do this when trapping EINIT from guest.
Another problem of updating MSRs when vcpu is scheduled in is, this will break SGX driver if SGX driver does some performance optimization in terms of updating MSRs, such as keeping per_cpu variables for MSR current value, and only updating MSRs if per_cpu value is not equal to the value you want to update. Because of this, Andy, Sean also suggested that SGX driver can provide a function to handle updating MSRs and run EINIT together with mutex protected, and KVM simply calls that function (of course KVM will provide the sigstruct and token by trapping EINIT from guest). This can cover MSRs updating and EINIT for both host and KVM guests. IMO this is a good idea as well, and you should consider to do in this way.
Thanks,
-Kai
Quoted 3 lines
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
On Mon, Jun 12, 2017 at 2:53 AM, Huang, Kai <[email protected]> wrote:
\
Quoted 17 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines
How that would work on a system where MSRs cannot be changed?
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Quoted 6 lines
Virtualization is only about to emulate hardware's behavior, but not to assume, or depend on, or change guest's SW behavior. We are not trying to make sure EINIT will run successfully in guest, instead, we are trying to make sure EINIT will be just the same behavior as it runs on physical machine -- either success or failure, according to guest's sigstruct and token.
I disagree. Virtualization can do whatever it wants, but a pretty strong constraint is that the guest should be at least reasonably function. A Windows guest, for example, shouldn't BSOD. But the host most certainly can restrict what the guest can do. If a guest is given pass-through access to a graphics card, the host is well within its rights to impose thermal policy, prevent reflashing, etc. Similarly, if a guest is given access to SGX, the host can and should impose required policy on the guest. If this means that an EINIT that would have succeeded at host CPL 0 fails in the guest, so be it.
Of course, there isn't much in the way of host policy right now, so this may not require any particular action until interesting host policy shows up.
Quoted 10 lines
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
And let me explain the purpose of trapping EINIT again here.
When guest is about to run EINIT, if guest's
SHA256(sigstruct->modulus)matches guest's virtualIA32_SGXLEPUBKEYHASHn(and if others are correctly populated in sigstruct and token as well), KVM needs to make sure that EINIT will run successfully in guest, even physicalIA32_SGXLEPUBKEYHASHnare not equal to guest's virtual MSRs at this particular time.
True, so long as it doesn't contradict host policy to do so.
Quoted 3 lines
This is because given
the same condition, the EINIT will run successfully on physical machine. KVM needs to emulate the right HW behavior.
No. The host needs to do this because KVM needs to work and be useful, not because KVM needs to precisely match CPU behavior as seen by VMX root.
To avoid confusion, I don't believe I've ever said that guests should be restricted in which LEs they can use. The types of restrictions I'm talking about are that, if the host prevents user code from running, say, a provisioning enclave that isn't whitelisted, then the guest should have the same restriction applied. This type of restriction *can't* be usefully done by restricting acceptable MSR values, but it's trivial by trapping EINIT.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 26 lines · Andy Lutomirski
On Mon, Jun 12, 2017 at 2:53 AM, Huang, Kai <[email protected]> wrote:
\Quoted 17 lines
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines
How that would work on a system where MSRs cannot be changed?
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Quoted 6 lines
Virtualization is only about to emulate hardware's behavior, but not to assume, or depend on, or change guest's SW behavior. We are not trying to make sure EINIT will run successfully in guest, instead, we are trying to make sure EINIT will be just the same behavior as it runs on physical machine -- either success or failure, according to guest's sigstruct and token.
Quoted 3 lines
I disagree. Virtualization can do whatever it wants, but a pretty strong constraint is that the guest should be at least reasonably function.
Virtualization only can do whatever it wants on the part that it can trap and emulate, and such *whatever* should not break HW behavior presented to guest. This is fundamental thing for virtualization. I don't know whether there are some *minor* case that the HW behavior emulation is not fully respected but I believe, if they exist, those cases are extremely rare, and we certainly have a good reason why such cases are OK.
Anyway I'll leave this part to KVM maintainers.
Paolo, Radim,
Sorry this thread is a little bit long till now. Can you comments on this?
A Windows guest, for example, shouldn't BSOD. But the host
Quoted 3 lines
most certainly can restrict what the guest can do. If a guest is given pass-through access to a graphics card, the host is well within its rights to impose thermal policy, prevent reflashing, etc.
You need to see whether those policies are provided by PCIE configration space or by device's registers. If policies are implemented via PCIE configuration space (which is trapped and emulated by VMM/Qemu), you certainly can apply restrict on it by emulating PCIE configuration space access. But if those policies are done by device registers, which driver will control totally, it's totally up to driver and host cannot apply any policies. You can choose to or not to pass through device's specific BARs (ex, you cannot passthrough the BARs contains MSI), but once the BARs are passthroughed to guest, you cannot control guest's behavior.
Quoted 3 lines
Similarly, if a guest is given access to SGX, the host can and should impose required policy on the guest. If this means that an EINIT that would have succeeded at host CPL 0 fails in the guest, so be it.
This is completely different. And I disagree. If EINIT can run at host CPL 0, it can be run in guest's CPL 0 as well. Unless hardware doesn't support this, in which case we even cannot support SGX virtualization.
The exception is, if HW provides, for example, some specific capability bits that can be used to control whether EINIT can be run in CPL 0 or not, and hypervisor is able to trap those bits, then hypervisor can manipulate those bits to make guest think HW doesn't allow EINIT to run in CPL 0, in which case it is quite reasonable in guest EINIT cannot run in CPL 0 (because it is HW behavior).
Quoted 18 lines
Of course, there isn't much in the way of host policy right now, so this may not require any particular action until interesting host policy shows up.
Quoted 10 lines
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
And let me explain the purpose of trapping EINIT again here.
When guest is about to run EINIT, if guest's
SHA256(sigstruct->modulus)matches guest's virtualIA32_SGXLEPUBKEYHASHn(and if others are correctly populated in sigstruct and token as well), KVM needs to make sure that EINIT will run successfully in guest, even physicalIA32_SGXLEPUBKEYHASHnare not equal to guest's virtual MSRs at this particular time.True, so long as it doesn't contradict host policy to do so.
Quoted 7 lines
Quoted 3 lines
This is because given
the same condition, the EINIT will run successfully on physical machine. KVM needs to emulate the right HW behavior.No. The host needs to do this because KVM needs to work and be useful, not because KVM needs to precisely match CPU behavior as seen by VMX root.
If we need to break HW behavior to make SGX useful. The maintainer may choose not to support SGX virtualization, as from HW this feature simply cannot be virtualized.
Anyway I'll leave this to KVM maintainers to determine.
Quoted 8 lines
To avoid confusion, I don't believe I've ever said that guests should be restricted in which LEs they can use. The types of restrictions I'm talking about are that, if the host prevents user code from running, say, a provisioning enclave that isn't whitelisted, then the guest should have the same restriction applied. This type of restriction *can't* be usefully done by restricting acceptable MSR values, but it's trivial by trapping EINIT.
OK. Sorry I didn't get your point before. I thought it was about restrict LE.
I don't know whether SGX driver will have restrict on running provisioning enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I don't think we even need to talk about this topic at current stage.
Thanks,
-Kai
Quoted 1 line
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 8 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I don't think we even need to talk about this topic at current stage.
The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
For the time being, I don't expect either policy to be implemented right away, but I bet that something like this will eventually happen.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 13 lines · Andy Lutomirski
Quoted 8 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I don't think we even need to talk about this topic at current stage.
The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Hi Andy,
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different vendor, for example, host runs intel's provisioning enclave, and guest runs other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Assuming the enclave hash means measurement of enclave, and assuming we have a policy that we only allow enclave from one signer to run, would you also elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such host, I think that typically we will have the same policy in the guest (vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
Thanks,
-Kai
Quoted 4 lines
For the time being, I don't expect either policy to be implemented right away, but I bet that something like this will eventually happen.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 30 lines · Kai
Quoted 18 lines · Andy Lutomirski
Quoted 11 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Quoted 8 lines
Hi Andy,
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different vendor, for example, host runs intel's provisioning enclave, and guest runs other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?
There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 12 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 5 lines
Assuming the enclave hash means measurement of enclave, and assuming we have a policy that we only allow enclave from one signer to run, would you also elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such host, I think that typically we will have the same policy in the guest
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if /dev/kvm is used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 73 lines · Andy Lutomirski
Quoted 30 lines · Kai
Quoted 18 lines · Andy Lutomirski
Quoted 11 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Quoted 8 lines
Hi Andy,
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different vendor, for example, host runs intel's provisioning enclave, and guest runs other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?
There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 12 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 5 lines
Assuming the enclave hash means measurement of enclave, and assuming we have a policy that we only allow enclave from one signer to run, would you also elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such host, I think that typically we will have the same policy in the guest
Quoted 6 lines
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a
I agree.
Quoted 8 lines
policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if
/dev/kvmis used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.
This is theoretical thing. I think your statement makes sense only if we have specific example that can prove there's actual risk when allowing guest to exceed X approved by host.
I will dig more in your previous emails to see whether you have listed such real cases (I some kind forgot sorry) but if you don't mind, you can list such cases here.
Thanks,
-Kai
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 95 lines · Kai
Quoted 74 lines · Andy Lutomirski
Quoted 31 lines · Kai
Quoted 18 lines · Andy Lutomirski
Quoted 11 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that Jarkko can know whether he will support those restricts or not? Otherwise I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 8 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering
EINIT.Quoted 8 lines
Hi Andy,
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different vendor, for example, host runs intel's provisioning enclave, and guest runs other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?
There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 12 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 5 lines
Assuming the enclave hash means measurement of enclave, and assuming we have a policy that we only allow enclave from one signer to run, would you also elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such host, I think that typically we will have the same policy in the guest
Quoted 6 lines
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a
I agree.
Quoted 8 lines
policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if
/dev/kvmis used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.
Quoted 3 lines
This is theoretical thing. I think your statement makes sense only if we have specific example that can prove there's actual risk when allowing guest to exceed X approved by host.
I will dig more in your previous emails to see whether you have listed such real cases (I some kind forgot sorry) but if you don't mind, you can list such cases here.
Hi Andy,
I can find an example you listed in your previous email but it is not related to host policy but related to SGX's key architecture issue. I quoted below:
"Concretely, imagine I write an enclave that seals my TLS client certificate's private key and offers an API to sign TLS certificate requests with it. This way, if my system is compromised, an attacker can use the certificate only so long as they have access to my machine. If I kick them out or if they merely get the ability to read the sealed data but not to execute code, the private key should still be safe. But, if this system is a VM guest, the attacker could run the exact same enclave on another guest on the same physical CPU and sign using my key. Whoops!"
I think you will have this problem even you apply the most strict policy at both host and guest -- only allow one enclave from one signer to run. This is indeed a flaw but virtualization cannot do anything to solve this -- unless we don't support virtualization at all :)
Sorry I am just trying to find out whether there's real case that really require we apply host's policy to guest and will have problem if we don't.
Thanks,
-Kai
Quoted 3 lines
Thanks,
-Kai
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 113 lines · Kai
Quoted 90 lines · Andy Lutomirski
Quoted 38 lines · Kai
Quoted 22 lines · Andy Lutomirski
Quoted 14 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that
Jarkko can know whether he will support those restricts or not? Otherwise
I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Hi Andy,
Quoted 8 lines
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different
vendor, for example, host runs intel's provisioning enclave, and guest runs
other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 13 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 8 lines
Assuming the enclave hash means measurement of enclave, and assuming we have
a policy that we only allow enclave from one signer to run, would you also
elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such
host, I think that typically we will have the same policy in the guestQuoted 7 lines
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a
I agree.
Quoted 8 lines
policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if
/dev/kvmis used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.
Quoted 3 lines
This is theoretical thing. I think your statement makes sense only if we have specific example that can prove there's actual risk when allowing guest to exceed X approved by host.
I would turn this around. Can you come up with any example where the host would have a restrictive policy but that the policy should not be enforced for guests?
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 113 lines · Kai
Quoted 90 lines · Andy Lutomirski
Quoted 38 lines · Kai
Quoted 22 lines · Andy Lutomirski
Quoted 14 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that
Jarkko can know whether he will support those restricts or not? Otherwise
I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Hi Andy,
Quoted 8 lines
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different
vendor, for example, host runs intel's provisioning enclave, and guest runs
other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 13 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 8 lines
Assuming the enclave hash means measurement of enclave, and assuming we have
a policy that we only allow enclave from one signer to run, would you also
elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such
host, I think that typically we will have the same policy in the guestQuoted 7 lines
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a
I agree.
Quoted 8 lines
policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if
/dev/kvmis used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.
Quoted 7 lines
This is theoretical thing. I think your statement makes sense only if we have specific example that can prove there's actual risk when allowing guest to exceed X approved by host.
I will dig more in your previous emails to see whether you have listed such real cases (I some kind forgot sorry) but if you don't mind, you can list such cases here.
I'm operating under the assumption that some kind of policy exists in the first place. I can imagine everything working fairly well without any real policy, but apparently there are vendors who want restrictive policies. What I can't imagine is anyone who wants a restrictive policy but is then okay with the host only partially enforcing it.
Quoted 3 lines
Thanks,
-Kai
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 122 lines · Andy Lutomirski
Quoted 113 lines · Kai
Quoted 90 lines · Andy Lutomirski
Quoted 38 lines · Kai
Quoted 22 lines · Andy Lutomirski
Quoted 14 lines · Kai
I don't know whether SGX driver will have restrict on running provisioning
enclave. In my understanding provisioning enclave is always from Intel. However I am not expert here and probably be wrong. Can you point out *exactly* what restricts in host must/should be applied to guest so that
Jarkko can know whether he will support those restricts or not? Otherwise
I
don't think we even need to talk about this topic at current stage.The whole point is that I don't know. But here are two types of restriction I can imagine demand for:
Quoted 7 lines
1. Only a particular approved provisioning enclave may run (be it Intel's or otherwise -- with a non-Intel LE, I think you can launch a non-Intel provisioning enclave). This would be done to restrict what types of remote attestation can be done. (Intel supplies a remote attestation service that uses some contractual policy that I don't know. Maybe a system owner wants a different policy applied to ISVs.) Imposing this policy on guests more or less requires filtering EINIT.
Hi Andy,
Quoted 8 lines
Sorry for late reply.
What is the issue if host and guest run provisioning enclave from different
vendor, for example, host runs intel's provisioning enclave, and guest runs
other vendor's provisioning enclave? Or different guests run provisioning enclaves from different vendors?There's no issue unless someone has tried to impose a policy. There is clearly at least some interest in having policies that affect what enclaves can run -- otherwise there wouldn't be LEs in the first place.
Quoted 5 lines
One reason I am asking is that, on Xen (where we don't have concept of *host*), it's likely that we won't apply any policy at Xen hypervisor at all, and guests will be able to run any enclave from any signer as their wish.
That seems entirely reasonable. Someone may eventually ask Xen to add support for SGX enclave restrictions, in which case you'll either have to tell them that it won't happen or implement it.
Quoted 13 lines
Sorry that I don't understand (or kind of forgot) the issues here.
Quoted 8 lines
2. For kiosk-ish or single-purpose applications, I can imagine that you would want to allow a specific list of enclave signers or even enclave hashes. Maybe you would allow exactly one enclave hash. You could kludge this up with a restrictive LE policy, but you could also do it for real by implementing the specific restriction in the kernel. Then you'd want to impose it on the guest, and you'd do it by filtering EINIT.
Quoted 8 lines
Assuming the enclave hash means measurement of enclave, and assuming we have
a policy that we only allow enclave from one signer to run, would you also
elaborate the issue that, if host and guest run enclaves from different signer? If host has such policy, and we are allowing creating guests on such
host, I think that typically we will have the same policy in the guestQuoted 7 lines
Yes, I presume this too, but.
Quoted 2 lines
(vetted by guest's kernel). The owner of that host should be aware of the risk (if there's any) by creating guest and run enclave inside it.
No. The host does not trust the guest in general. If the host has a
I agree.
Quoted 8 lines
policy that the only enclave that shall run is X, that doesn't mean that the host shall reject all enclaves requested by the normal userspace API except X but that, if
/dev/kvmis used, then the user is magically trusted to not load a guest that fails to respect the host policy. It means that the only enclave that shall run is X regardless of what interface is used. The host must only allow X to be loaded by its userspace and the host must only allow X to be loaded by a guest.Quoted 7 lines
This is theoretical thing. I think your statement makes sense only if we have specific example that can prove there's actual risk when allowing guest to exceed X approved by host.
I will dig more in your previous emails to see whether you have listed such real cases (I some kind forgot sorry) but if you don't mind, you can list such cases here.
Quoted 5 lines
I'm operating under the assumption that some kind of policy exists in the first place. I can imagine everything working fairly well without any real policy, but apparently there are vendors who want restrictive policies. What I can't imagine is anyone who wants a restrictive policy but is then okay with the host only partially enforcing it.
I think there is a certain amount of inception going on here, i.e. the only reason we're discussing LE enforced policies in the kernel is because the LE architecture exists and can't be disabled. The LE, as originally designed, is intended to be a way for *userspace* to control what code can run on the system, e.g. to provide a hook for anti-virus/malware to inspect an enclave since it's impossible to inspect an enclave once it is running.
The kernel doesn't need an LE to restrict what enclaves can run, e.g. it can perform inspection at any point during the initialization process. This is true for guest enclaves as well since the kernel can trap EINIT. By making the LE kernel-only we've bastardized the concept of the LE and have negated the primary value provided by an LE[1][2]. In my opinion, the discussion of the kernel's launch policies is much ado about nothing, e.g. if supported by hardware, I think we'd opt to disable launch control completely.
[1] On a system with unlocked IA32_SGXLEPUBKEYHASH MSRs, the only value added by a using a LE to enforce the kernel's policies is defense-in-depth, e.g. an attacker can't hide malicious code in an enclave even if it gains control of the kernel. I think this is a very minor benefit since running in an enclave doesn't grant any new privileges and doesn't persist across system reset.
[2] I think it's safe to assume that any use case that requires locked hash MSRs is out of scope for this discussion, given that the upstream kernel will require unlocked MSRs.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 7 lines · Sean J
I think there is a certain amount of inception going on here, i.e. the only reason we're discussing LE enforced policies in the kernel is because the LE architecture exists and can't be disabled. The LE, as originally designed, is intended to be a way for *userspace* to control what code can run on the system, e.g. to provide a hook for anti-virus/malware to inspect an enclave since it's impossible to inspect an enclave once it is running.
Quoted 7 lines
The kernel doesn't need an LE to restrict what enclaves can run, e.g. it can perform inspection at any point during the initialization process. This is true for guest enclaves as well since the kernel can trap EINIT. By making the LE kernel-only we've bastardized the concept of the LE and have negated the primary value provided by an LE[1][2]. In my opinion, the discussion of the kernel's launch policies is much ado about nothing, e.g. if supported by hardware, I think we'd opt to disable launch control completely.
Agreed.
I don't think I've ever said that the kernel should implement restrictions on what enclaves should run [1]. All I've said is that
(a) if the kernel does implement restrictions like this, it should apply them to guests as well and (b) that the kernel should probably trap EINIT because that's the most sensible way to deal with the MSRs.
[1] With the possible exception of provisioning enclaves. I'm still not convinced that anyone except root should be allowed to run an exclave with the provision bit set, as that bit gives access to the provisioning key, which is rather special. From memory, it bypasses the owner epoch, and it may have privacy issues. Maybe this is a nonissue, but I'd like to see someone seriously analyze how provisioning enclaves that may not be signed by Intel affect the overall security of the system and how Linux should handle them. SGX was designed under the assumption that provisioning enclaves would only ever be signed by Intel, and that's not the case any more, and dealing with this intelligently may require some thought.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 10 lines · Kai
Quoted 6 lines
Quoted 3 lines
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Why can't host LE generate the token without guest knowning it and supply it with EINIT?
Quoted 6 lines
Quoted 3 lines
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
I'm being now totally honest to your: your explanation makes absolutely zero sense to me. You don't need a 1000+ words to explain the scenarios where "host as a delegate LE" approach would go wrong.
Please just pinpoint the scenarios where it goes wrong. I'll ignore the text below.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 21 lines · Jarkko Sakkinen
Quoted 10 lines · Kai
Quoted 6 lines
Quoted 3 lines
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Why can't host LE generate the token without guest knowning it and supply it with EINIT?
Quoted 6 lines
Quoted 3 lines
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
Quoted 8 lines
I'm being now totally honest to your: your explanation makes absolutely zero sense to me. You don't need a 1000+ words to explain the scenarios where "host as a delegate LE" approach would go wrong.
Please just pinpoint the scenarios where it goes wrong. I'll ignore the text below.
/Jarkko
When I've been reading this discussion the biggest lesson for me has been that this is a new argument for having in-kernel LE in addition to what Andy has stated before: the MSRs *never* need to be updated on behalf of the guest.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 33 lines · Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen
Quoted 7 lines · Kai
Quoted 7 lines
Quoted 7 lines
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when
creating the guest.Quoted 6 lines
Quoted 2 lines
Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
The guest has its own LE running inside, and guest's LE will generate token
for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.Why can't host LE generate the token without guest knowning it and supply it with EINIT?
Quoted 8 lines
Quoted 5 lines
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this
part please see my explanation above.I'm being now totally honest to your: your explanation makes absolutely zero sense to me. You don't need a 1000+ words to explain the scenarios where "host as a delegate LE" approach would go wrong.
Please just pinpoint the scenarios where it goes wrong. I'll ignore the text below.
Quoted 7 lines
Quoted 1 line
/Jarkko
When I've been reading this discussion the biggest lesson for me has been that this is a new argument for having in-kernel LE in addition to what Andy has stated before: the MSRs *never* need to be updated on behalf of the guest.
/Jarkko
The MSRs need to be written to run a LE in the guest, EINITTOKEN can't be used to EINIT an enclave that is requesting access to the EINITTOKENKEY, i.e. a LE. Preventing the guest from running its own LE is not an option, as the owner of the LE, e.g. guest kernel or userspace daemon, will likely disable SGX if its LE fails to run (including any ECALLS into the LE). Allowing a guest to run a LE doesn't mean the host can't ignore/discard the guest's EINITTOKENs, assuming the host traps EINIT.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 7 lines · Sean Christopherson
The MSRs need to be written to run a LE in the guest, EINITTOKEN can't be used to EINIT an enclave that is requesting access to the EINITTOKENKEY, i.e. a LE. Preventing the guest from running its own LE is not an option, as the owner of the LE, e.g. guest kernel or userspace daemon, will likely disable SGX if its LE fails to run (including any ECALLS into the LE). Allowing a guest to run a LE doesn't mean the host can't ignore/discard the guest's EINITTOKENs, assuming the host traps EINIT.
[I started one week leave today but will peek MLs seldomly so except some delay in my follow up responses]
Please, lets not use the term ECALL in these discussions. It's neither hardware nor kernel specific concept. It's abstraction that exists only in the Intel SDK. I have neither ECALLs nor OCALLs in my LE for example. There are enough moving parts without such abstraction.
I'm looking at the section "EINIT - Initialize an Enclave for Execution" from the SDM. I'm not seeing a branch in the pseudo code that checks for ATTRIBUTES.EINITTOKENKEY.
39.1.4 states that "Only Launch Enclaves are allowed to launch without a valid token." I'm not sure what I should deduce from that because that statement is *incorrect*. If you control the MSRs, you can launch anything you want to launch. I guess we should make a bug report of this section as it's complete nonsense?
The table 41-56 does not show any key material bound to key hash defined in the MSRs.
Instead of teaching me stuff that I already know I would just like to get pinpointed where is the "side-effect" that makes the constraint that you are claiming. I can then update the documentation so that we don't have to go through this discussion anymore :-)
/Jarkko
RE: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 13 lines · Jarkko Sakkinen
Quoted 7 lines · Sean Christopherson
The MSRs need to be written to run a LE in the guest, EINITTOKEN can't be used to EINIT an enclave that is requesting access to the EINITTOKENKEY, i.e. a LE. Preventing the guest from running its own LE is not an option, as the owner of the LE, e.g. guest kernel or userspace daemon, will likely disable SGX if its LE fails to run (including any ECALLS into the LE). Allowing a guest to run a LE doesn't mean the host can't ignore/discard the guest's EINITTOKENs, assuming the host traps EINIT.
[I started one week leave today but will peek MLs seldomly so except some delay in my follow up responses]
Quoted 8 lines
Please, lets not use the term ECALL in these discussions. It's neither hardware nor kernel specific concept. It's abstraction that exists only in the Intel SDK. I have neither ECALLs nor OCALLs in my LE for example. There are enough moving parts without such abstraction.
I'm looking at the section "EINIT - Initialize an Enclave for Execution" from the SDM. I'm not seeing a branch in the pseudo code that checks for ATTRIBUTES.EINITTOKENKEY.
(* if controlled ATTRIBUTES are set, SIGSTRUCT must be signed using an authorized key *) CONTROLLED_ATTRIBUTES <- 0000000000000020H;
IF (((DS:RCX.ATTRIBUTES & CONTROLLED_ATTRIBUTES) != 0) and (TMP_MRSIGNER != IA32_SGXLEPUBKEYHASH))
RFLAG.ZF <- 1;
RAX <- SGX_INVALID_ATTRIBUTE;
GOTO EXIT;FI;
Bit 5, i.e. 20H, corresponds to the EINITTOKENKEY. This is also covered in the text description under Intel SGX Launch Control Configuration - "The hash of the public key used to sign the SIGSTRUCT of the Launch Enclave must equal the value in the IA32_SGXLEPUBKEYHASH MSRs."
Quoted 5 lines
39.1.4 states that "Only Launch Enclaves are allowed to launch without a valid token." I'm not sure what I should deduce from that because that statement is *incorrect*. If you control the MSRs, you can launch anything you want to launch. I guess we should make a bug report of this section as it's complete nonsense?
I wouldn't call it complete nonsense, there are far more egregious ambiguities in the SDM. If you read the statement in the context of someone learning about SGX, it makes perfect sense: if it's not a launch enclave, it needs a token. Sure, rewording the statement to something like "Only enclaves whose public key hash equals the value in the IA32_SGXLEPUBKEYHASH MSRs are allowed to launch without a token." is technically more accurate, but I wouldn't describe the current wording as "complete nonsense".
Quoted 9 lines
The table 41-56 does not show any key material bound to key hash defined in the MSRs.
Instead of teaching me stuff that I already know I would just like to get pinpointed where is the "side-effect" that makes the constraint that you are claiming. I can then update the documentation so that we don't have to go through this discussion anymore :-)
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 29 lines · Sean J
Quoted 13 lines · Jarkko Sakkinen
Quoted 7 lines · Sean Christopherson
The MSRs need to be written to run a LE in the guest, EINITTOKEN can't be used to EINIT an enclave that is requesting access to the EINITTOKENKEY, i.e. a LE. Preventing the guest from running its own LE is not an option, as the owner of the LE, e.g. guest kernel or userspace daemon, will likely disable SGX if its LE fails to run (including any ECALLS into the LE). Allowing a guest to run a LE doesn't mean the host can't ignore/discard the guest's EINITTOKENs, assuming the host traps EINIT.
[I started one week leave today but will peek MLs seldomly so except some delay in my follow up responses]
Quoted 8 lines
Please, lets not use the term ECALL in these discussions. It's neither hardware nor kernel specific concept. It's abstraction that exists only in the Intel SDK. I have neither ECALLs nor OCALLs in my LE for example. There are enough moving parts without such abstraction.
I'm looking at the section "EINIT - Initialize an Enclave for Execution" from the SDM. I'm not seeing a branch in the pseudo code that checks for ATTRIBUTES.EINITTOKENKEY.
(* if controlled ATTRIBUTES are set, SIGSTRUCT must be signed using an authorized key *)
CONTROLLED_ATTRIBUTES<- 0000000000000020H;
IF (((DS:RCX.ATTRIBUTES &CONTROLLED_ATTRIBUTES) != 0) and (TMP_MRSIGNER!=IA32_SGXLEPUBKEYHASH))RFLAG.ZF <- 1; RAX <- SGX_INVALID_ATTRIBUTE; GOTO EXIT;
Quoted 6 lines
FI;
Bit 5, i.e. 20H, corresponds to the EINITTOKENKEY. This is also covered in the text description under Intel SGX Launch Control Configuration - "The hash of the public key used to sign the SIGSTRUCT of the Launch Enclave must equal the value in the
IA32_SGXLEPUBKEYHASHMSRs."
Thanks. I wonder by the naming is ambiguous (the value is exactly the same as the value of ATTRIBUTES.EINITTOKENKEY but the name is different) but there it is.
Quoted 6 lines
Quoted 5 lines
39.1.4 states that "Only Launch Enclaves are allowed to launch without a valid token." I'm not sure what I should deduce from that because that statement is *incorrect*. If you control the MSRs, you can launch anything you want to launch. I guess we should make a bug report of this section as it's complete nonsense?
Quoted 7 lines
I wouldn't call it complete nonsense, there are far more egregious ambiguities in the SDM. If you read the statement in the context of someone learning about SGX, it makes perfect sense: if it's not a launch enclave, it needs a token. Sure, rewording the statement to something like "Only enclaves whose public key hash equals the value in the
IA32_SGXLEPUBKEYHASHMSRs are allowed to launch without a token." is technically more accurate, but I wouldn't describe the current wording as "complete nonsense".
Agreed! That was a harsh overstatement.
I think that in this kind of stuff the accurancy still would make sense when cryptography is involved.
I'll make updates to intel_sgx.rst. It's good to have it documented when virtualization stuff is upstreamed.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Jarkko SakkinenQuoted 8 lines · Jarkko Sakkinen
Quoted 7 lines · Kai
Quoted 6 lines
Quoted 3 lines
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
Quoted 6 lines
Quoted 3 lines
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Why can't host LE generate the token without guest knowning it and supply it with EINIT?
I have said many times, virtualization is only about to emulate hardware behavior. The same software runs in native machine is supposed to run in guest. I don't care on host how you implement -- whether LE delegates token for other enclave or not. Your implementation works on host, the exact driver works in guest. I said several times, I even don't have to trap EINIT, in which case you simply cannot generate token for EINIT from guest, because there is no EINIT from guest!
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
I'm being now totally honest to your: your explanation makes absolutely zero sense to me. You don't need a 1000+ words to explain the scenarios where "host as a delegate LE" approach would go wrong.
This doesn't require 1000+ words.. This is simple, and I have explained:
This is not constraint, but KVM has to emulate hardware correctly.
The additional 1000+ words that I spent lots of time on typing is trying to explain to you -- why we (at least Andy, Sean, Haim I believe) agreed to choose to trap EINIT.
Quoted 3 lines
Please just pinpoint the scenarios where it goes wrong. I'll ignore the text below.
Of course you can, if you already understand why we agreed to choose to trap EINIT.
I think Andy's comments just made things more complicated. He is trying to solve problems that don't exist in your implementation, and I believe we can handle this in the future, if those problems come up.
So let's focus on how to handle updating MSRs and EINIT. I believe Andy, me, and Sean are all on the same page. Not sure whether you are or not.
For me I am perfectly fine not to trap EINIT (which is exactly this patch did), but you have to guarantee the correctness of host side.
Quoted 3 lines
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 58 lines · Kai
Quoted 8 lines · Jarkko Sakkinen
Quoted 7 lines · Kai
Quoted 6 lines
Quoted 3 lines
This is simple, we simply won't allow guest to choose its own
IA32_SGXLEPUBKEYHASHnby specifying 'lehash' value in Qemu parameter when creating the guest.Why not? You could have virtual MSRs and ask host LE to generate token if they match to modulus.
Quoted 6 lines
Quoted 3 lines
The guest has its own LE running inside, and guest's LE will generate token for enclaves in guest. The host will not generate token for guest in any circumstances, because this is totally guest's behavior.
Why can't host LE generate the token without guest knowning it and supply it with EINIT?
I have said many times, virtualization is only about to emulate hardware behavior. The same software runs in native machine is supposed to run in guest. I don't care on host how you implement -- whether LE delegates token for other enclave or not. Your implementation works on host, the exact driver works in guest. I said several times, I even don't have to trap EINIT, in which case you simply cannot generate token for EINIT from guest, because there is no EINIT from guest!
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
Seriously sounds like a stupid constraint or I'm not getting something (which also might be the case). If you anyway trap EINIT, you could create a special case for guest LE.
This is not constraint, but KVM has to emulate hardware correctly. For this part please see my explanation above.
I'm being now totally honest to your: your explanation makes absolutely zero sense to me. You don't need a 1000+ words to explain the scenarios where "host as a delegate LE" approach would go wrong.
This doesn't require 1000+ words.. This is simple, and I have explained:
This is not constraint, but KVM has to emulate hardware correctly.
The additional 1000+ words that I spent lots of time on typing is trying to explain to you -- why we (at least Andy, Sean, Haim I believe) agreed to choose to trap EINIT.
Quoted 3 lines
Please just pinpoint the scenarios where it goes wrong. I'll ignore the text below.
Of course you can, if you already understand why we agreed to choose to trap EINIT.
I think Andy's comments just made things more complicated. He is trying to solve problems that don't exist in your implementation, and I believe we can handle this in the future, if those problems come up.
Quoted 9 lines
So let's focus on how to handle updating MSRs and EINIT. I believe Andy, me, and Sean are all on the same page. Not sure whether you are or not.
For me I am perfectly fine not to trap EINIT (which is exactly this patch did), but you have to guarantee the correctness of host side.
Quoted 3 lines
/Jarkko
I'm not yet seeing why MSRs would ever would need to be updated.
See my response to Sean for details.
There's probably some detail in the SDM that I'm not observing.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Andy LutomirskiQuoted 12 lines · Andy Lutomirski
Quoted 11 lines · Kai
Quoted 10 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
KVM will implement writes to SGXLEPUBKEYHASH by updating its in-memory state but *not* changing the MSRs. KVM will trap and emulate EINIT to support the same handling as the host. There is no action required at all on KVM guest entry and exit.
Quoted 8 lines
Quoted 5 lines
This is doable, but SGX driver needs to do those things and expose interfaces for KVM to use. In terms of the percpu data, it is nice to have, but I am not sure whether it is mandatory, as IMO EINIT is not even in performance critical path. We can simply read old value from MSRs out and compare whether the old equals to the new.
I think the SGX driver should probably live in
arch/x86, and the interface could be a simple percpu variable that is exported (from the main kernel image, not from a module).
Jarkko, what are your thoughts on moving the SGX code into arch/x86 and removing the option to build it as a module? This would simplify the KVM and EPC cgroup implementations.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Andy LutomirskiQuoted 9 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 4 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
Quoted 7 lines
This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
I think this would be the best starting point.
/Jarkko
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 18 lines · Jarkko Sakkinen
Quoted 9 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 4 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
Quoted 7 lines
This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
Quoted 4 lines
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
I think this would be the best starting point.
OK. Assuming we are going to have this percpu variable for IA32_SGXLEPUBKEYHASHn, I suppose KVM also will update guest's value to this percpu variable after KVM writes guest's value to hardware MSR? And host (SGX driver) need to do the same thing (check the value and write conditionally), correct?
Thanks,
-Kai
Quoted 3 lines
/Jarkko
Paolo Bonzini
maintainerRe: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 9 lines · Kai
Quoted 9 lines
Quoted 5 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
Quoted 7 lines
Quoted 1 line
I think this would be the best starting point.
OK. Assuming we are going to have this percpu variable for
IA32_SGXLEPUBKEYHASHn, I suppose KVM also will update guest's value to this percpu variable after KVM writes guest's value to hardware MSR? And host (SGX driver) need to do the same thing (check the value and write conditionally), correct?
The percpu variable is just an optimization. If EINIT is not performance critical, you could even do the WRMSR unconditionally; what matters is having a mutex that covers both WRMSR and EINIT.
Thanks,
Paolo
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 28 lines · Kai
Quoted 18 lines · Jarkko Sakkinen
Quoted 16 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 7 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we
will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT
for LE.This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Quoted 8 lines
Quoted 4 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
I think this would be the best starting point.
Quoted 8 lines
OK. Assuming we are going to have this percpu variable for
IA32_SGXLEPUBKEYHASHn, I suppose KVM also will update guest's value to this percpu variable after KVM writes guest's value to hardware MSR? And host (SGX driver) need to do the same thing (check the value and write conditionally), correct?Thanks,
-Kai
Yes, the percpu variable is simply a cache so that the kernel doesn't have to do four RDMSRs every time it wants to do EINIT. KVM would still maintain shadow copies of the MSRs for each vcpu for emulating RDMSR, WRMSR and EINIT. I don't think KVM would even need to be aware of the percpu variable, i.e. the entire lock->(rd/wr)msr->EINIT->unlock sequence can probably be encapsulated in a single function that is called from both the primary SGX driver and from KVM.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 38 lines · Sean Christopherson
Quoted 28 lines · Kai
Quoted 18 lines · Jarkko Sakkinen
Quoted 16 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 7 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we
will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT
for LE.This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Quoted 8 lines
Quoted 4 lines
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
I think this would be the best starting point.
Quoted 8 lines
OK. Assuming we are going to have this percpu variable for
IA32_SGXLEPUBKEYHASHn, I suppose KVM also will update guest's value to this percpu variable after KVM writes guest's value to hardware MSR? And host (SGX driver) need to do the same thing (check the value and write conditionally), correct?Thanks,
-Kai
Quoted 7 lines
Yes, the percpu variable is simply a cache so that the kernel doesn't have to do four RDMSRs every time it wants to do EINIT. KVM would still maintain shadow copies of the MSRs for each vcpu for emulating RDMSR, WRMSR and EINIT. I don't think KVM would even need to be aware of the percpu variable, i.e. the entire lock->(rd/wr)msr->EINIT->unlock sequence can probably be encapsulated in a single function that is called from both the primary SGX driver and from KVM.
You are making assumption that KVM will run ENCLS on behalf of guest. :)
If we don't need to look into guest's SIGSTRUCT, EINITTOKEN, etc, then I actually prefer to using MTF, as with MTF we don't have to do all the remapping guest's virtual address to KVM's virtual address thing, if we don't need to look into guest's ENCLS parameter. But if we need to look into guest's ENCLS parameters, for example, to locate physical SECS page, or to update physical EPC page's info (that KVM needs to maintain), maybe we can choose running ENCLS on behalf of guest.
But if we are going to run ENCLS on behalf of guest, I think providing a single function which does write MSR and EINIT for KVM should be a good idea.
Thanks,
-Kai
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 9 lines · Kai
You are making assumption that KVM will run ENCLS on behalf of guest. :)
If we don't need to look into guest's SIGSTRUCT, EINITTOKEN, etc, then I actually prefer to using MTF, as with MTF we don't have to do all the remapping guest's virtual address to KVM's virtual address thing, if we don't need to look into guest's ENCLS parameter. But if we need to look into guest's ENCLS parameters, for example, to locate physical SECS page, or to update physical EPC page's info (that KVM needs to maintain), maybe we can choose running ENCLS on behalf of guest.
After thinking about this a bit, I don't see how MTF helps. Currently, KVM works kind of like this:
local_irq_disable();
set up stuff;
VMRESUME;
restore some host state;local_irq_enable();
If the guest is going to run with the EINIT-exiting bit clear, the only way I see this working is to modify KVM along the lines of:
local_irq_disable();
set up stuff;
if (condition here) {
WRMSR to SGXLEPUBKEYHASH;
update percpu shadow copyl
clear EINIT-exiting bit;
} else {
set EINIT-exiting bit;
}
VMRESUME;
restore some host state;local_irq_enable();
where "condition here" might be something like "the last VMRESUME exited due to EINIT".
I don't see how MTF helps much. And if I were the KVM maintainer, I would probably prefer to trap EINIT instead of adding a special case to the main vm entry code.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 36 lines · Andy Lutomirski
Quoted 9 lines · Kai
You are making assumption that KVM will run ENCLS on behalf of guest. :)
If we don't need to look into guest's SIGSTRUCT, EINITTOKEN, etc, then I actually prefer to using MTF, as with MTF we don't have to do all the remapping guest's virtual address to KVM's virtual address thing, if we don't need to look into guest's ENCLS parameter. But if we need to look into guest's ENCLS parameters, for example, to locate physical SECS page, or to update physical EPC page's info (that KVM needs to maintain), maybe we can choose running ENCLS on behalf of guest.
After thinking about this a bit, I don't see how MTF helps. Currently, KVM works kind of like this:
local_irq_disable();
set up stuff;
VMRESUME;
restore some host state;local_irq_enable();If the guest is going to run with the EINIT-exiting bit clear, the only way I see this working is to modify KVM along the lines of:
local_irq_disable();
set up stuff;
if (condition here) {
WRMSR to SGXLEPUBKEYHASH;
update percpu shadow copyl
clear EINIT-exiting bit;
} else {
set EINIT-exiting bit;
}
VMRESUME;
restore some host state;local_irq_enable();
Quoted 7 lines
where "condition here" might be something like "the last VMRESUME exited due to EINIT".
I don't see how MTF helps much. And if I were the KVM maintainer, I would probably prefer to trap EINIT instead of adding a special case to the main vm entry code.
Hi Andy,
Thanks for your comments. However I didn't intend to use MTF in your way. The idea of using MTF (along with ENCLS VMEXIT) is, by turning on MTF VMEXIT upon ENCLS VMEXIT, we are able to mark a single step VMEXIT after ENCLS so that ENCLS can run in guest as single step.
Let me explain how the two approaches work below in general, so that we can decide which is better. Only trapping EINIT in order to update IA32_SGXLEPUBKEYHASHn is relatively simpler but I'd compare the two in more general way, assuming we may want to trap more ENCLS in order to, ex, track EPC/Enclave status/info, in the future to support, ex, EPC oversubscription between KVM guests.
Below diagram shows the basic idea of the two approaches.
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
| /|\
ENCLS VMEXIT | | VMENTRY
| |
\|/ |
1) identify which ENCLS leaf (RAX)
2) reconstruct/remap guest's ENCLS parameters, ex:
- remap any guest VA (virtual address) to KVM VA
- reconstruct PAGEINFO
3) do whatever needed before ENCLS, ex:
- updating MSRs before EINIT
4) run ENCLS on behalf of guest, and skip ENCLS
5) emulate ENCLS result (succeeded or not)
- update guest's RAX-RDX.
- and/or inject #GP (or #UD).
6) do whatever needed after ENCLS, ex:
- updating EPC/Enclave status/info
1) Run ENCLS on behalf of guest
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
|/|\ |/|\
ENCLS VMEXIT | | VMENTRY MTF VMEXIT | | VMENTRY
| | | |
\|/| \|/|
1) Turn off EMCLS VMEXIT 1) Turn off MTF VMEXIT
2) turn on MTF VMEXIT 2) Turn on ENCLS VMEXIT
3) cache ENCLS parameters 3) check whether ENCLS has run
(ENCLS changes RAX) 4) check whether ENCLS succeeded
4) do whatever needed before or not.
ENCLS 5) do whatever needed after ENCLS
2) Using MTF
The concern of running ENCLS on behalf of guest is emulating ENCLS error. KVM needs to *correctly* emulate ENCLS error to guest so that the error we inject to guest can reflect the right behavior as if ENCLS run in guest. Running ENCLS in root-mode may be potentially different running ENCLS in non-root mode, therefore we have to go through all possible error codes to make sure we can emulate. And for some error code, ex, SGX_LOCKFAIL, we can handle it in KVM and don't have to inject error to guest. So the point is we have to go through all error code to make sure KVM can emulate ENCLS error code correctly for guest. Another argument is Intel may add new error codes in the future when more SGX functionalities are introduced, so emulating error code may be a burden.
Using MTF is also a little bit tricky, as when we turn on MTF VMEXIT upon ENCLS VMEXIT, the MTF won't be absolutely pending at end of that ENCLS. For example, MTF may be pending at end of interrupt (cannot recall exactly) if event is pending during VMENTRY from ENCLS VMEXIT. Therefore we have to do additional thing to check whether this MTF VMEXIT really happens after ENCLS run (step 3 above). And depending on what we need to do, we may need to check whether ENCLS succeeded or not in guest, which is also tricky, as ENCLS can fail in either setting error code in RAX, or generating #GP or #UD (step 4 above). We may still need to do gva->gpa->hpa, ex, in order to locate EPC/SECS page and update status, depending on the purpose of trapping ENCLS.
But by using MTF, we don't have to worry about ENCLS error emulation, as ENCLS runs in guest, thus we don't need to worry about this root-mode and non-root mode difference. I think this is the major reason that we want to use MTF.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 68 lines · Kai
Quoted 40 lines · Andy Lutomirski
Quoted 12 lines · Kai
You are making assumption that KVM will run ENCLS on behalf of guest. :)
If we don't need to look into guest's SIGSTRUCT, EINITTOKEN, etc, then I actually prefer to using MTF, as with MTF we don't have to do all the remapping guest's virtual address to KVM's virtual address thing, if we don't need to look into guest's ENCLS parameter. But if we need to look into
guest's ENCLS parameters, for example, to locate physical SECS page, or to
update physical EPC page's info (that KVM needs to maintain), maybe we can
choose running ENCLS on behalf of guest.After thinking about this a bit, I don't see how MTF helps. Currently, KVM works kind of like this:
local_irq_disable();
set up stuff;
VMRESUME;
restore some host state;local_irq_enable();If the guest is going to run with the EINIT-exiting bit clear, the only way I see this working is to modify KVM along the lines of:
local_irq_disable();
set up stuff;
if (condition here) {
WRMSR to SGXLEPUBKEYHASH;
update percpu shadow copyl
clear EINIT-exiting bit;
} else {
set EINIT-exiting bit;
}
VMRESUME;
restore some host state;local_irq_enable();Quoted 7 lines
where "condition here" might be something like "the last VMRESUME exited due to EINIT".
I don't see how MTF helps much. And if I were the KVM maintainer, I would probably prefer to trap EINIT instead of adding a special case to the main vm entry code.
Hi Andy,
Thanks for your comments. However I didn't intend to use MTF in your way. The idea of using MTF (along with ENCLS VMEXIT) is, by turning on MTF VMEXIT upon ENCLS VMEXIT, we are able to mark a single step VMEXIT after ENCLS so that ENCLS can run in guest as single step.
Let me explain how the two approaches work below in general, so that we can decide which is better. Only trapping EINIT in order to update
IA32_SGXLEPUBKEYHASHnis relatively simpler but I'd compare the two in more general way, assuming we may want to trap more ENCLS in order to, ex, track EPC/Enclave status/info, in the future to support, ex, EPC oversubscription between KVM guests.Below diagram shows the basic idea of the two approaches.
Quoted 7 lines
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
| /|\
ENCLS VMEXIT | | VMENTRY
| |
\|/ |Looks the diagrams are broken. Sorry. I need to verify before sending next time. But looks they are still understandable?
Thanks,
Kai
Quoted 63 lines
1) identify which ENCLS leaf (RAX)
2) reconstruct/remap guest's ENCLS parameters, ex:
- remap any guest VA (virtual address) to KVM VA
- reconstruct PAGEINFO
3) do whatever needed before ENCLS, ex:
- updating MSRs before EINIT
4) run ENCLS on behalf of guest, and skip ENCLS
5) emulate ENCLS result (succeeded or not)
- update guest's RAX-RDX.
- and/or inject #GP (or #UD).
6) do whatever needed after ENCLS, ex:
- updating EPC/Enclave status/info1) Run ENCLS on behalf of guest
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
|/|\ |/|\
ENCLS VMEXIT | | VMENTRY MTF VMEXIT | | VMENTRY
| | | |
\|/| \|/|
1) Turn off EMCLS VMEXIT 1) Turn off MTF VMEXIT
2) turn on MTF VMEXIT 2) Turn on ENCLS VMEXIT
3) cache ENCLS parameters 3) check whether ENCLS has run
(ENCLS changes RAX) 4) check whether ENCLS succeeded
4) do whatever needed before or not.
ENCLS 5) do whatever needed after ENCLS2) Using MTF
The concern of running ENCLS on behalf of guest is emulating ENCLS error. KVM needs to *correctly* emulate ENCLS error to guest so that the error we inject to guest can reflect the right behavior as if ENCLS run in guest. Running ENCLS in root-mode may be potentially different running ENCLS in non-root mode, therefore we have to go through all possible error codes to make sure we can emulate. And for some error code, ex,
SGX_LOCKFAIL, we can handle it in KVM and don't have to inject error to guest. So the point is we have to go through all error code to make sure KVM can emulate ENCLS error code correctly for guest. Another argument is Intel may add new error codes in the future when more SGX functionalities are introduced, so emulating error code may be a burden.Using MTF is also a little bit tricky, as when we turn on MTF VMEXIT upon ENCLS VMEXIT, the MTF won't be absolutely pending at end of that ENCLS. For example, MTF may be pending at end of interrupt (cannot recall exactly) if event is pending during VMENTRY from ENCLS VMEXIT. Therefore we have to do additional thing to check whether this MTF VMEXIT really happens after ENCLS run (step 3 above). And depending on what we need to do, we may need to check whether ENCLS succeeded or not in guest, which is also tricky, as ENCLS can fail in either setting error code in RAX, or generating #GP or #UD (step 4 above). We may still need to do
gva->gpa->hpa, ex, in order to locate EPC/SECS page and update status, depending on the purpose of trapping ENCLS.But by using MTF, we don't have to worry about ENCLS error emulation, as ENCLS runs in guest, thus we don't need to worry about this root-mode and non-root mode difference. I think this is the major reason that we want to use MTF.
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 70 lines · Kai
Quoted 43 lines · Andy Lutomirski
Quoted 13 lines · Kai
You are making assumption that KVM will run ENCLS on behalf of guest. :)
If we don't need to look into guest's SIGSTRUCT, EINITTOKEN, etc, then I actually prefer to using MTF, as with MTF we don't have to do all the remapping guest's virtual address to KVM's virtual address thing, if we don't need to look into guest's ENCLS parameter. But if we need to look into
guest's ENCLS parameters, for example, to locate physical SECS page, or to
update physical EPC page's info (that KVM needs to maintain), maybe we can
choose running ENCLS on behalf of guest.After thinking about this a bit, I don't see how MTF helps. Currently, KVM works kind of like this:
local_irq_disable();
set up stuff;
VMRESUME;
restore some host state;local_irq_enable();If the guest is going to run with the EINIT-exiting bit clear, the only way I see this working is to modify KVM along the lines of:
local_irq_disable();
set up stuff;
if (condition here) {
WRMSR to SGXLEPUBKEYHASH;
update percpu shadow copyl
clear EINIT-exiting bit;
} else {
set EINIT-exiting bit;
}
VMRESUME;
restore some host state;local_irq_enable();Quoted 7 lines
where "condition here" might be something like "the last VMRESUME exited due to EINIT".
I don't see how MTF helps much. And if I were the KVM maintainer, I would probably prefer to trap EINIT instead of adding a special case to the main vm entry code.
Hi Andy,
Thanks for your comments. However I didn't intend to use MTF in your way. The idea of using MTF (along with ENCLS VMEXIT) is, by turning on MTF VMEXIT upon ENCLS VMEXIT, we are able to mark a single step VMEXIT after ENCLS so that ENCLS can run in guest as single step.
Let me explain how the two approaches work below in general, so that we can decide which is better. Only trapping EINIT in order to update
IA32_SGXLEPUBKEYHASHnis relatively simpler but I'd compare the two in more general way, assuming we may want to trap more ENCLS in order to, ex, track EPC/Enclave status/info, in the future to support, ex, EPC oversubscription between KVM guests.Below diagram shows the basic idea of the two approaches.
...
Quoted 17 lines
--------------------------------------------------------------
| ENCLS |
--------------------------------------------------------------
|/|\ |/|\
ENCLS VMEXIT | | VMENTRY MTF VMEXIT | | VMENTRY
| | | |
\|/| \|/|
1) Turn off EMCLS VMEXIT 1) Turn off MTF VMEXIT
2) turn on MTF VMEXIT 2) Turn on ENCLS VMEXIT
3) cache ENCLS parameters 3) check whether ENCLS has run
(ENCLS changes RAX) 4) check whether ENCLS succeeded
4) do whatever needed before or not.
ENCLS 5) do whatever needed after ENCLS2) Using MTF
...
Quoted 11 lines
Using MTF is also a little bit tricky, as when we turn on MTF VMEXIT upon ENCLS VMEXIT, the MTF won't be absolutely pending at end of that ENCLS. For example, MTF may be pending at end of interrupt (cannot recall exactly) if event is pending during VMENTRY from ENCLS VMEXIT. Therefore we have to do additional thing to check whether this MTF VMEXIT really happens after ENCLS run (step 3 above). And depending on what we need to do, we may need to check whether ENCLS succeeded or not in guest, which is also tricky, as ENCLS can fail in either setting error code in RAX, or generating #GP or #UD (step 4 above). We may still need to do
gva->gpa->hpa, ex, in order to locate EPC/SECS page and update status, depending on the purpose of trapping ENCLS.
I think there are some issues here.
First, you're making a big assumption that, when you resume the guest with MTF set, the instruction that gets executed is still ENCLS[EINIT]. That's not guaranteed as is -- you could race against another vCPU that changes the instruction, the instruction could be in IO space, host userspace could be messing with you, etc. Second, I don't think there's any precedent at all in KVM for doing this. Third, you still need to make sure that the MSRs retain the value you want them to have by the time ENCLS happens. I think that, by the time you resolve all of these issues, it'll look a lot like the pseudocode I emailed out, and MTF won't be necessary any more.
Quoted 5 lines
But by using MTF, we don't have to worry about ENCLS error emulation, as ENCLS runs in guest, thus we don't need to worry about this root-mode and non-root mode difference. I think this is the major reason that we want to use MTF.
I don't see why error emulation is hard. If the host does ENCLS on behalf of the guest and it returns an error, can't you return exactly the same error to the guest with no further processing? The only tricky case is where the host rejects due to its own policy and you have to choose an error code.
--Andy
Paolo Bonzini
maintainerRe: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 14 lines · Andy Lutomirski
Quoted 12 lines
Using MTF is also a little bit tricky, as when we turn on MTF VMEXIT upon ENCLS VMEXIT, the MTF won't be absolutely pending at end of that ENCLS. For example, MTF may be pending at end of interrupt (cannot recall exactly) if event is pending during VMENTRY from ENCLS VMEXIT. Therefore we have to do additional thing to check whether this MTF VMEXIT really happens after ENCLS run (step 3 above). And depending on what we need to do, we may need to check whether ENCLS succeeded or not in guest, which is also tricky, as ENCLS can fail in either setting error code in RAX, or generating #GP or #UD (step 4 above). We may still need to do
gva->gpa->hpa, ex, in order to locate EPC/SECS page and update status, depending on the purpose of trapping ENCLS.I think there are some issues here.
Quoted 10 lines
First, you're making a big assumption that, when you resume the guest with MTF set, the instruction that gets executed is still ENCLS[EINIT]. That's not guaranteed as is -- you could race against another vCPU that changes the instruction, the instruction could be in IO space, host userspace could be messing with you, etc. Second, I don't think there's any precedent at all in KVM for doing this. Third, you still need to make sure that the MSRs retain the value you want them to have by the time ENCLS happens. I think that, by the time you resolve all of these issues, it'll look a lot like the pseudocode I emailed out, and MTF won't be necessary any more.
Agreed. Emulation in the host is better.
Paolo
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
Quoted 28 lines · Paolo Bonzini
Quoted 14 lines · Andy Lutomirski
Quoted 12 lines
Using MTF is also a little bit tricky, as when we turn on MTF VMEXIT upon ENCLS VMEXIT, the MTF won't be absolutely pending at end of that ENCLS. For example, MTF may be pending at end of interrupt (cannot recall exactly) if event is pending during VMENTRY from ENCLS VMEXIT. Therefore we have to do additional thing to check whether this MTF VMEXIT really happens after ENCLS run (step 3 above). And depending on what we need to do, we may need to check whether ENCLS succeeded or not in guest, which is also tricky, as ENCLS can fail in either setting error code in RAX, or generating #GP or #UD (step 4 above). We may still need to do
gva->gpa->hpa, ex, in order to locate EPC/SECS page and update status, depending on the purpose of trapping ENCLS.I think there are some issues here.
Quoted 10 lines
First, you're making a big assumption that, when you resume the guest with MTF set, the instruction that gets executed is still ENCLS[EINIT]. That's not guaranteed as is -- you could race against another vCPU that changes the instruction, the instruction could be in IO space, host userspace could be messing with you, etc. Second, I don't think there's any precedent at all in KVM for doing this. Third, you still need to make sure that the MSRs retain the value you want them to have by the time ENCLS happens. I think that, by the time you resolve all of these issues, it'll look a lot like the pseudocode I emailed out, and MTF won't be necessary any more.
Quoted 1 line
Agreed. Emulation in the host is better.
Hi Andy/Paolo,
Thanks for comments. I'll follow your suggestion in v2.
Thanks,
-Kai
Quoted 3 lines
Paolo
Re: [intel-sgx-kernel-dev] [PATCH 08/10] kvm: vmx: add guest's IA32_SGXLEPUBKEYHASHn runtime switch support
In reply to Huang, KaiQuoted 26 lines · Kai
Quoted 18 lines · Jarkko Sakkinen
Quoted 9 lines · Andy Lutomirski
[resending due to some kind of kernel.org glitch -- sorry if anyone gets duplicates]
Quoted 4 lines · Kai
My current patch is based on this assumption. For KVM guest, naturally, we will write the cached value to real MSRs when vcpu is scheduled in. For host, SGX driver should write its own value to MSRs when it performs EINIT for LE.
Quoted 7 lines
This seems unnecessarily slow (perhaps *extremely* slow) to me. I would propose a totally different solution:
Have a percpu variable that stores the current SGXLEPUBKEYHASH along with whatever lock is needed (probably just a mutex). Users of EINIT will take the mutex, compare the percpu variable to the desired value, and, if it's different, do WRMSR and update the percpu variable.
Quoted 4 lines
This is exactly what I've been suggesting internally: trap EINIT and check the value and write conditionally.
I think this would be the best starting point.
Quoted 8 lines
OK. Assuming we are going to have this percpu variable for
IA32_SGXLEPUBKEYHASHn, I suppose KVM also will update guest's value to this percpu variable after KVM writes guest's value to hardware MSR? And host (SGX driver) need to do the same thing (check the value and write conditionally), correct?Thanks,
-Kai
This how I would understand it, yes.
/Jarkko
PATCH 9/10 kvm: vmx: handle ENCLS VMEXIT
In reply to Kai HuangThis patch handles ENCLS VMEXIT. ENCLS VMEXIT doesn't need to be always turned on, actually it should not be turned on in most cases, as guest can run ENCLS perfectly in non-root mode. However there are some cases we need to trap ENCLS and emulate as in those cases ENCLS in guest may behavor differently with in native (for example, when hardware supports SGX but SGX is not exposed to guest, and if guest runs ENCLS deliberately, it may have different behavior to on native).
In case of nested SGX support, we need to turn on ENCLS VMEXIT if L1 hypervisor has turned on ENCLS VMEXIT, and such ENCLS VMEXIT from L2 (nested guest) will be handled by L1 hypervisor.
--- arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx.c | 265 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 270 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +2 -0
@@ -202,6 +202,8 @@ enum vmcs_field {XSS_EXIT_BITMAP_HIGH =0x0000202D ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +ENCLS_EXITING_BITMAP =0x0000202E , +ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,
arch/x86/include/uapi/asm/vmx.h +3 -1
@@ -77,6 +77,7 @@#define EXIT_REASON_XSETBV 55#define EXIT_REASON_APIC_WRITE 56#define EXIT_REASON_INVPCID 58 +#define EXIT_REASON_ENCLS 60#define EXIT_REASON_PML_FULL 62#define EXIT_REASON_XSAVES 63#define EXIT_REASON_XRSTORS 64
@@ -130,7 +131,8 @@ { EXIT_REASON_INVVPID, "INVVPID" }, \ { EXIT_REASON_INVPCID, "INVPCID" }, \ { EXIT_REASON_XSAVES, "XSAVES" }, \ - { EXIT_REASON_XRSTORS, "XRSTORS" } + { EXIT_REASON_XRSTORS, "XRSTORS" }, \ + { EXIT_REASON_ENCLS, "ENCLS" } #define VMX_ABORT_SAVE_GUEST_MSR_FAIL 1#define VMX_ABORT_LOAD_HOST_PDPTE_FAIL 2
arch/x86/kvm/vmx.c +265 -0
345 lines not on this page · raw message
Signature · 1 line
2.11.0
Paolo Bonzini
maintainerRe: [PATCH 09/10] kvm: vmx: handle ENCLS VMEXIT
Quoted 12 lines · Kai Huang
This patch handles ENCLS VMEXIT. ENCLS VMEXIT doesn't need to be always turned on, actually it should not be turned on in most cases, as guest can run ENCLS perfectly in non-root mode. However there are some cases we need to trap ENCLS and emulate as in those cases ENCLS in guest may behavor differently with in native (for example, when hardware supports SGX but SGX is not exposed to guest, and if guest runs ENCLS deliberately, it may have different behavior to on native).
In case of nested SGX support, we need to turn on ENCLS VMEXIT if L1 hypervisor has turned on ENCLS VMEXIT, and such ENCLS VMEXIT from L2 (nested guest) will be handled by L1 hypervisor.
Quoted 5 lines
Signed-off-by Kai Huang <[email protected]>--- arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx.c | 265 ++++++++++++++++++++++++++++++++++++++++
Please try to move more code to sgx.c.
Paolo
Quoted 37 lines
3 files changed, 270 insertions(+), 1 deletion(-)arch/x86/include/asm/vmx.h +2
-0@@ -202,6 +202,8 @@ enum vmcs_field {XSS_EXIT_BITMAP_HIGH =0x0000202D ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +ENCLS_EXITING_BITMAP =0x0000202E , +ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,arch/x86/include/uapi/asm/vmx.h +3
-1@@ -77,6 +77,7 @@#define EXIT_REASON_XSETBV 55#define EXIT_REASON_APIC_WRITE 56#define EXIT_REASON_INVPCID 58 +#define EXIT_REASON_ENCLS 60#define EXIT_REASON_PML_FULL 62#define EXIT_REASON_XSAVES 63#define EXIT_REASON_XRSTORS 64@@ -130,7 +131,8 @@ { EXIT_REASON_INVVPID, "INVVPID" }, \ { EXIT_REASON_INVPCID, "INVPCID" }, \ { EXIT_REASON_XSAVES, "XSAVES" }, \ - { EXIT_REASON_XRSTORS, "XRSTORS" } + { EXIT_REASON_XRSTORS, "XRSTORS" }, \ + { EXIT_REASON_ENCLS, "ENCLS" } #define VMX_ABORT_SAVE_GUEST_MSR_FAIL 1#define VMX_ABORT_LOAD_HOST_PDPTE_FAIL 2
Re: [PATCH 09/10] kvm: vmx: handle ENCLS VMEXIT
Quoted 15 lines · Paolo Bonzini
Quoted 12 lines · Kai Huang
This patch handles ENCLS VMEXIT. ENCLS VMEXIT doesn't need to be always turned on, actually it should not be turned on in most cases, as guest can run ENCLS perfectly in non-root mode. However there are some cases we need to trap ENCLS and emulate as in those cases ENCLS in guest may behavor differently with in native (for example, when hardware supports SGX but SGX is not exposed to guest, and if guest runs ENCLS deliberately, it may have different behavior to on native).
In case of nested SGX support, we need to turn on ENCLS VMEXIT if L1 hypervisor has turned on ENCLS VMEXIT, and such ENCLS VMEXIT from L2 (nested guest) will be handled by L1 hypervisor.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Kai Huang <[email protected]>--- arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx.c | 265 ++++++++++++++++++++++++++++++++++++++++Please try to move more code to
sgx.c.
Hi Paolo,
Thanks for comments. Will try to do this in next version.
Thanks,
-Kai
Quoted 41 lines
Paolo
Quoted 37 lines
3 files changed, 270 insertions(+), 1 deletion(-)arch/x86/include/asm/vmx.h +2
-0@@ -202,6 +202,8 @@ enum vmcs_field {XSS_EXIT_BITMAP_HIGH =0x0000202D ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +ENCLS_EXITING_BITMAP =0x0000202E , +ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,arch/x86/include/uapi/asm/vmx.h +3
-1@@ -77,6 +77,7 @@#define EXIT_REASON_XSETBV 55#define EXIT_REASON_APIC_WRITE 56#define EXIT_REASON_INVPCID 58 +#define EXIT_REASON_ENCLS 60#define EXIT_REASON_PML_FULL 62#define EXIT_REASON_XSAVES 63#define EXIT_REASON_XRSTORS 64@@ -130,7 +131,8 @@ { EXIT_REASON_INVVPID, "INVVPID" }, \ { EXIT_REASON_INVPCID, "INVPCID" }, \ { EXIT_REASON_XSAVES, "XSAVES" }, \ - { EXIT_REASON_XRSTORS, "XRSTORS" } + { EXIT_REASON_XRSTORS, "XRSTORS" }, \ + { EXIT_REASON_ENCLS, "ENCLS" } #define VMX_ABORT_SAVE_GUEST_MSR_FAIL 1#define VMX_ABORT_LOAD_HOST_PDPTE_FAIL 2
PATCH 10/10 kvm: vmx: handle VMEXIT from SGX Enclave
In reply to Kai HuangVMX adds new bit to both exit_reason and GUEST_INTERRUPT_STATE to indicate whether VMEXIT happens in Enclave. Several instructions are also invalid or behave differently in enclave according to SDM. This patch handles those cases.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/include/uapi/asm/vmx.h | 1 + arch/x86/kvm/vmx.c | 120 +++++++++++++++++++++++++++++++++------- 3 files changed, 103 insertions(+), 19 deletions(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -351,6 +351,7 @@ enum vmcs_field {#define GUEST_INTR_STATE_MOV_SS 0x00000002#define GUEST_INTR_STATE_SMI 0x00000004#define GUEST_INTR_STATE_NMI 0x00000008 +#define GUEST_INTR_STATE_ENCLAVE_INTR 0x00000010/* GUEST_ACTIVITY_STATE flags */ #define GUEST_ACTIVITY_ACTIVE 0
arch/x86/include/uapi/asm/vmx.h +1 -0
@@ -26,6 +26,7 @@#define VMX_EXIT_REASONS_FAILED_VMENTRY 0x80000000 +#define VMX_EXIT_REASON_FROM_ENCLAVE 0x08000000#define EXIT_REASON_EXCEPTION_NMI 0#define EXIT_REASON_EXTERNAL_INTERRUPT 1
arch/x86/kvm/vmx.c +101 -19
168 lines not on this page · raw message
Signature · 1 line
2.11.0
Paolo Bonzini
maintainerRe: [PATCH 10/10] kvm: vmx: handle VMEXIT from SGX Enclave
Quoted 5 lines · Kai Huang
arch/x86/kvm/vmx.c +1
-0@@ -6977,6 +7042,31 @@ static __exit void hardware_unsetup(void)*/ static int handle_pause (struct kvm_vcpu *vcpu) { +/*
arch/x86/kvm/vmx.c static __exit void hardware_unsetup(void) · line 7055
Quoted 10 lines
arch/x86/kvm/vmx.c +10
-0+* SDM 39.6.3 PAUSE Instruction. +* +* SDM suggests, if VMEXIT caused by 'PAUSE-loop exiting', VMM should +* disable 'PAUSE-loop exiting' so PAUSE can be executed in Enclave +* again without further PAUSE-looping VMEXIT. +* +* SDM suggests, if VMEXIT caused by 'PAUSE exiting', VMM should disable +* 'PAUSE exiting' so PAUSE can be executed in Enclave again without +* further PAUSE VMEXIT. +*/
How is PLE re-enabled?
I don't understand the interaction of the internal control registers (paragraph 41.1.4) with VMX. How can you migrate the VM between EENTER and EEXIT?
In addition, paragraph 41.1.4 does not include the parts of CR_SAVE_FS* and CR_SAVE_GS* (base, limit, access rights) and does not include CR_ENCLAVE_ENTRY_IP.
Paolo
Quoted 14 lines
arch/x86/kvm/vmx.c +14
-0+if (vmx_exit_from_enclave (vcpu)) { +u32 exec_ctl, secondary_exec_ctl; + + exec_ctl =vmx_exec_control (to_vmx (vcpu)); + exec_ctl &= ~CPU_BASED_PAUSE_EXITING ; +vmcs_write32 (CPU_BASED_VM_EXEC_CONTROL , exec_ctl); + + secondary_exec_ctl =vmx_secondary_exec_control (to_vmx (vcpu)); + secondary_exec_ctl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING ; +vmcs_set_secondary_exec_control (secondary_exec_ctl); + +return 1 ; + } +
Re: [PATCH 10/10] kvm: vmx: handle VMEXIT from SGX Enclave
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Kai Huang
arch/x86/kvm/vmx.c +5
-0@@ -6977,6 +7042,31 @@ static __exit void hardware_unsetup(void)*/ static int handle_pause (struct kvm_vcpu *vcpu) { +/* +* SDM 39.6.3 PAUSE Instruction. +* +* SDM suggests, if VMEXIT caused by 'PAUSE-loop exiting', VMM should +* disable 'PAUSE-loop exiting' so PAUSE can be executed in Enclave
Quoted 8 lines
arch/x86/kvm/vmx.c static __exit void hardware_unsetup(void) · line 7055
Quoted 6 lines
arch/x86/kvm/vmx.c +6
-0+* again without further PAUSE-looping VMEXIT. +* +* SDM suggests, if VMEXIT caused by 'PAUSE exiting', VMM should disable +* 'PAUSE exiting' so PAUSE can be executed in Enclave again without +* further PAUSE VMEXIT. +*/ How is PLE re-enabled?
Currently it will not be enabled again. Probably we can re-enable it at another VMEXIT, if that VMEXIT is not PLE VMEXIT?
Quoted 4 lines
I don't understand the interaction of the internal control registers (paragraph 41.1.4) with VMX. How can you migrate the VM between EENTER and EEXIT?
Current SGX hardware architecture doesn't support live migration, as the key architecture of SGX is not migratable. For example, some keys are persistent and bound to hardware (sealing and attestation). Therefore right now if SGX is exposed to guest, live migration is not supposed.
Quoted 4 lines
In addition, paragraph 41.1.4 does not include the parts of CR_SAVE_FS* and CR_SAVE_GS* (base, limit, access rights) and does not include
CR_ENCLAVE_ENTRY_IP.
CPU can exit enclave via EEXIT, or by Asynchronous Enclave Exit (AEX). All non-EEXIT enclave exit are referred as AEX. When AEX happens, a so called "synthetic state" is created on CPU to prevent any software from trying to observe *secret* from CPU status in AEX. What exactly will be pushed in "synthetic state" is in SDM 40.3.
So in my understanding, CPU won't put something like "CR_ENCLAVE_ENTRY_IP" to RIP. Actually during AEX, Asynchronous Exit Pointer (AEP), which is in normal memory, will be pushed to stack and IRET will return to AEP to continue to run. AEP typically points to some small piece of code which basically calls ERESUME so that we can go back to enclave to run.
Hope my reply answered your questions?
Thanks,
-Kai
Quoted 18 lines
Paolo
Quoted 14 lines
arch/x86/kvm/vmx.c +14
-0+if (vmx_exit_from_enclave (vcpu)) { +u32 exec_ctl, secondary_exec_ctl; + + exec_ctl =vmx_exec_control (to_vmx (vcpu)); + exec_ctl &= ~CPU_BASED_PAUSE_EXITING ; +vmcs_write32 (CPU_BASED_VM_EXEC_CONTROL , exec_ctl); + + secondary_exec_ctl =vmx_secondary_exec_control (to_vmx (vcpu)); + secondary_exec_ctl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING ; +vmcs_set_secondary_exec_control (secondary_exec_ctl); + +return 1 ; + } +
Re: [PATCH 10/10] kvm: vmx: handle VMEXIT from SGX Enclave
Quoted 27 lines · Kai
Quoted 15 lines · Paolo Bonzini
Quoted 12 lines · Kai Huang
arch/x86/kvm/vmx.c +4
-0@@ -6977,6 +7042,31 @@ static __exit void hardware_unsetup(void)*/ static int handle_pause (struct kvm_vcpu *vcpu) { +/* +* SDM 39.6.3 PAUSE Instruction. +* +* SDM suggests, if VMEXIT caused by 'PAUSE-loop exiting', VMM should
arch/x86/kvm/vmx.c +3
-0+* disable 'PAUSE-loop exiting' so PAUSE can be executed in Enclave +* again without further PAUSE-looping VMEXIT. +* Quoted 8 lines
arch/x86/kvm/vmx.c static __exit void hardware_unsetup(void) · line 7055
Quoted 6 lines
+ * SDM suggests, if VMEXIT caused by 'PAUSE exiting', VMM should
disable
+ * 'PAUSE exiting' so PAUSE can be executed in Enclave again
withoutarch/x86/kvm/vmx.c +2
-0+* further PAUSE VMEXIT. +*/ How is PLE re-enabled?
Quoted 1 line
Currently it will not be enabled again. Probably we can re-enable it at another VMEXIT, if that VMEXIT is not PLE VMEXIT?
Hi Paolo, all,
Sorry for reply late.
Do you think it is feasible to turn on PLE again on another further VMEXIT? Or another VMEXIT that is not from enclave?
Any suggestions so that I can improve in next version RFC?
Quoted 7 lines
Quoted 5 lines
I don't understand the interaction of the internal control registers (paragraph 41.1.4) with VMX. How can you migrate the VM between EENTER and EEXIT?
Current SGX hardware architecture doesn't support live migration, as the key architecture of SGX is not migratable. For example, some keys are persistent and bound to hardware (sealing and attestation). Therefore right now if SGX is exposed to guest, live migration is not supposed.
We recently had a discussion on this. We figured out that we are able to support SGX live migration with some kind of workaround -- basically the idea is we can ignore source VM's EPC and depend on destination VM's SGX driver and userspace SW stack to handle *sudden lose of EPC*, but this will cause some inconsistence with HW behavior, and will need to depend on driver's ability. I'll elaborate this in next version design and RFC and we can have a discussion whether to support it or not (along with snapshot support). But maybe we can also have a detailed discussion if you want to start now?
Thanks,
-Kai
Quoted 34 lines
Quoted 5 lines
In addition, paragraph 41.1.4 does not include the parts of CR_SAVE_FS* and CR_SAVE_GS* (base, limit, access rights) and does not include
CR_ENCLAVE_ENTRY_IP.CPU can exit enclave via EEXIT, or by Asynchronous Enclave Exit (AEX). All non-EEXIT enclave exit are referred as AEX. When AEX happens, a so called "synthetic state" is created on CPU to prevent any software from trying to observe *secret* from CPU status in AEX. What exactly will be pushed in "synthetic state" is in SDM 40.3.
So in my understanding, CPU won't put something like "
CR_ENCLAVE_ENTRY_IP" to RIP. Actually during AEX, Asynchronous Exit Pointer (AEP), which is in normal memory, will be pushed to stack and IRET will return to AEP to continue to run. AEP typically points to some small piece of code which basically calls ERESUME so that we can go back to enclave to run.Hope my reply answered your questions?
Thanks,
-KaiQuoted 18 lines
Paolo
Quoted 14 lines
arch/x86/kvm/vmx.c +14
-0+if (vmx_exit_from_enclave (vcpu)) { +u32 exec_ctl, secondary_exec_ctl; + + exec_ctl =vmx_exec_control (to_vmx (vcpu)); + exec_ctl &= ~CPU_BASED_PAUSE_EXITING ; +vmcs_write32 (CPU_BASED_VM_EXEC_CONTROL , exec_ctl); + + secondary_exec_ctl =vmx_secondary_exec_control (to_vmx (vcpu)); + secondary_exec_ctl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING ; +vmcs_set_secondary_exec_control (secondary_exec_ctl); + +return 1 ; + } +
Re: [PATCH 10/10] kvm: vmx: handle VMEXIT from SGX Enclave
2017-06-19 17:02+1200, Huang, Kai:
Quoted 3 lines
Hi Paolo, all,
Sorry for reply late.
I'm sorry as well,
Quoted 4 lines
Do you think it is feasible to turn on PLE again on another further VMEXIT? Or another VMEXIT that is not from enclave?
Any suggestions so that I can improve in next version RFC?
KVM doesn't enable "PAUSE exiting", it enables "PAUSE loop exiting". SDM recommends disabling "PAUSE exiting" because the VM exits (fault-like) on every PAUSE and needs intervention in order to progress, but SDM doesn't say to disable "PAUSE loop exiting".
Being inside an enclave doesn't change the usefulness of PLE (yielding the CPU to a task that isn't blocked), so I think it would be best to do nothing with it, thanks.
Re: [PATCH 10/10] kvm: vmx: handle VMEXIT from SGX Enclave
Quoted 12 lines · Radim Krčmář
2017-06-19 17:02+1200, Huang, Kai:
Quoted 3 lines
Hi Paolo, all,
Sorry for reply late.
I'm sorry as well,
Quoted 4 lines
Do you think it is feasible to turn on PLE again on another further VMEXIT? Or another VMEXIT that is not from enclave?
Any suggestions so that I can improve in next version RFC?
Quoted 8 lines
KVM doesn't enable "PAUSE exiting", it enables "PAUSE loop exiting". SDM recommends disabling "PAUSE exiting" because the VM exits (fault-like) on every PAUSE and needs intervention in order to progress, but SDM doesn't say to disable "PAUSE loop exiting".
Being inside an enclave doesn't change the usefulness of PLE (yielding the CPU to a task that isn't blocked), so I think it would be best to do nothing with it, thanks.
Hi Radim,
Thanks for feedback. You are right, and obviously I didn't read SDM carefully. Will do in next version. :)
Thanks,
-Kai
Quoted 1 line
PATCH 11/11 kvm: vmx: workaround FEATURE_CONTROL[17] is not set by BIOS
In reply to Kai Huangeven this bit is not set by BIOS, current ucode patch allows write to IA32_SGXLEPUBKEYHASHn.
--- arch/x86/kvm/vmx.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kvm/vmx.c +8 -0
@@ -2254,12 +2254,20 @@ static void decache_tsc_multiplier(struct vcpu_vmx *vmx)vmcs_write64 (TSC_MULTIPLIER , vmx->current_tsc_ratio); } +#define UCODE_PATCH static bool cpu_sgx_lepubkeyhash_writable (void ) {u64 val, sgx_lc_enabled_mask = (FEATURE_CONTROL_LOCKED |FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE );rdmsrl (MSR_IA32_FEATURE_CONTROL , val); +#ifdef UCODE_PATCH +/* +* current ucode patch can support write to IA32_SGXLEPUBKEYHASHn +* even if FEATURE_CONTROL[17] is not set. +*/ + val |=FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE ; +#endif return ((val & sgx_lc_enabled_mask) == sgx_lc_enabled_mask); }
Signature · 1 line
2.11.0
Re: [PATCH 11/11] kvm: vmx: workaround FEATURE_CONTROL[17] is not set by BIOS
Oops.. Please ignore this patch :)
Thanks,
-Kai
Quoted 32 lines · Kai Huang
even this bit is not set by BIOS, current ucode patch allows write to
IA32_SGXLEPUBKEYHASHn.--- arch/x86/kvm/vmx.c | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/kvm/vmx.c +8
-0@@ -2254,12 +2254,20 @@ static void decache_tsc_multiplier(struct vcpu_vmx *vmx)vmcs_write64 (TSC_MULTIPLIER , vmx->current_tsc_ratio); } +#define UCODE_PATCH static bool cpu_sgx_lepubkeyhash_writable (void ) {u64 val, sgx_lc_enabled_mask = (FEATURE_CONTROL_LOCKED |FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE );rdmsrl (MSR_IA32_FEATURE_CONTROL , val); +#ifdef UCODE_PATCH +/* +* current ucode patch can support write to IA32_SGXLEPUBKEYHASHn +* even if FEATURE_CONTROL[17] is not set. +*/ + val |=FEATURE_CONTROL_SGX_LAUNCH_CONTROL_ENABLE ; +#endif return ((val & sgx_lc_enabled_mask) == sgx_lc_enabled_mask); }