303 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3===============================4Software Guard eXtensions (SGX)5===============================6 7Overview8========9 10Software Guard eXtensions (SGX) hardware enables for user space applications11to set aside private memory regions of code and data:12 13* Privileged (ring-0) ENCLS functions orchestrate the construction of the14 regions.15* Unprivileged (ring-3) ENCLU functions allow an application to enter and16 execute inside the regions.17 18These memory regions are called enclaves. An enclave can be only entered at a19fixed set of entry points. Each entry point can hold a single hardware thread20at a time. While the enclave is loaded from a regular binary file by using21ENCLS functions, only the threads inside the enclave can access its memory. The22region is denied from outside access by the CPU, and encrypted before it leaves23from LLC.24 25The support can be determined by26 27 ``grep sgx /proc/cpuinfo``28 29SGX must both be supported in the processor and enabled by the BIOS. If SGX30appears to be unsupported on a system which has hardware support, ensure31support is enabled in the BIOS. If a BIOS presents a choice between "Enabled"32and "Software Enabled" modes for SGX, choose "Enabled".33 34Enclave Page Cache35==================36 37SGX utilizes an *Enclave Page Cache (EPC)* to store pages that are associated38with an enclave. It is contained in a BIOS-reserved region of physical memory.39Unlike pages used for regular memory, pages can only be accessed from outside of40the enclave during enclave construction with special, limited SGX instructions.41 42Only a CPU executing inside an enclave can directly access enclave memory.43However, a CPU executing inside an enclave may access normal memory outside the44enclave.45 46The kernel manages enclave memory similar to how it treats device memory.47 48Enclave Page Types49------------------50 51**SGX Enclave Control Structure (SECS)**52 Enclave's address range, attributes and other global data are defined53 by this structure.54 55**Regular (REG)**56 Regular EPC pages contain the code and data of an enclave.57 58**Thread Control Structure (TCS)**59 Thread Control Structure pages define the entry points to an enclave and60 track the execution state of an enclave thread.61 62**Version Array (VA)**63 Version Array pages contain 512 slots, each of which can contain a version64 number for a page evicted from the EPC.65 66Enclave Page Cache Map67----------------------68 69The processor tracks EPC pages in a hardware metadata structure called the70*Enclave Page Cache Map (EPCM)*. The EPCM contains an entry for each EPC page71which describes the owning enclave, access rights and page type among the other72things.73 74EPCM permissions are separate from the normal page tables. This prevents the75kernel from, for instance, allowing writes to data which an enclave wishes to76remain read-only. EPCM permissions may only impose additional restrictions on77top of normal x86 page permissions.78 79For all intents and purposes, the SGX architecture allows the processor to80invalidate all EPCM entries at will. This requires that software be prepared to81handle an EPCM fault at any time. In practice, this can happen on events like82power transitions when the ephemeral key that encrypts enclave memory is lost.83 84Application interface85=====================86 87Enclave build functions88-----------------------89 90In addition to the traditional compiler and linker build process, SGX has a91separate enclave “build” process. Enclaves must be built before they can be92executed (entered). The first step in building an enclave is opening the93**/dev/sgx_enclave** device. Since enclave memory is protected from direct94access, special privileged instructions are then used to copy data into enclave95pages and establish enclave page permissions.96 97.. kernel-doc:: arch/x86/kernel/cpu/sgx/ioctl.c98 :functions: sgx_ioc_enclave_create99 sgx_ioc_enclave_add_pages100 sgx_ioc_enclave_init101 sgx_ioc_enclave_provision102 103Enclave runtime management104--------------------------105 106Systems supporting SGX2 additionally support changes to initialized107enclaves: modifying enclave page permissions and type, and dynamically108adding and removing of enclave pages. When an enclave accesses an address109within its address range that does not have a backing page then a new110regular page will be dynamically added to the enclave. The enclave is111still required to run EACCEPT on the new page before it can be used.112 113.. kernel-doc:: arch/x86/kernel/cpu/sgx/ioctl.c114 :functions: sgx_ioc_enclave_restrict_permissions115 sgx_ioc_enclave_modify_types116 sgx_ioc_enclave_remove_pages117 118Enclave vDSO119------------120 121Entering an enclave can only be done through SGX-specific EENTER and ERESUME122functions, and is a non-trivial process. Because of the complexity of123transitioning to and from an enclave, enclaves typically utilize a library to124handle the actual transitions. This is roughly analogous to how glibc125implementations are used by most applications to wrap system calls.126 127Another crucial characteristic of enclaves is that they can generate exceptions128as part of their normal operation that need to be handled in the enclave or are129unique to SGX.130 131Instead of the traditional signal mechanism to handle these exceptions, SGX132can leverage special exception fixup provided by the vDSO. The kernel-provided133vDSO function wraps low-level transitions to/from the enclave like EENTER and134ERESUME. The vDSO function intercepts exceptions that would otherwise generate135a signal and return the fault information directly to its caller. This avoids136the need to juggle signal handlers.137 138.. kernel-doc:: arch/x86/include/uapi/asm/sgx.h139 :functions: vdso_sgx_enter_enclave_t140 141ksgxd142=====143 144SGX support includes a kernel thread called *ksgxd*.145 146EPC sanitization147----------------148 149ksgxd is started when SGX initializes. Enclave memory is typically ready150for use when the processor powers on or resets. However, if SGX has been in151use since the reset, enclave pages may be in an inconsistent state. This might152occur after a crash and kexec() cycle, for instance. At boot, ksgxd153reinitializes all enclave pages so that they can be allocated and re-used.154 155The sanitization is done by going through EPC address space and applying the156EREMOVE function to each physical page. Some enclave pages like SECS pages have157hardware dependencies on other pages which prevents EREMOVE from functioning.158Executing two EREMOVE passes removes the dependencies.159 160Page reclaimer161--------------162 163Similar to the core kswapd, ksgxd, is responsible for managing the164overcommitment of enclave memory. If the system runs out of enclave memory,165*ksgxd* “swaps” enclave memory to normal memory.166 167Launch Control168==============169 170SGX provides a launch control mechanism. After all enclave pages have been171copied, kernel executes EINIT function, which initializes the enclave. Only after172this the CPU can execute inside the enclave.173 174EINIT function takes an RSA-3072 signature of the enclave measurement. The function175checks that the measurement is correct and signature is signed with the key176hashed to the four **IA32_SGXLEPUBKEYHASH{0, 1, 2, 3}** MSRs representing the177SHA256 of a public key.178 179Those MSRs can be configured by the BIOS to be either readable or writable.180Linux supports only writable configuration in order to give full control to the181kernel on launch control policy. Before calling EINIT function, the driver sets182the MSRs to match the enclave's signing key.183 184Encryption engines185==================186 187In order to conceal the enclave data while it is out of the CPU package, the188memory controller has an encryption engine to transparently encrypt and decrypt189enclave memory.190 191In CPUs prior to Ice Lake, the Memory Encryption Engine (MEE) is used to192encrypt pages leaving the CPU caches. MEE uses a n-ary Merkle tree with root in193SRAM to maintain integrity of the encrypted data. This provides integrity and194anti-replay protection but does not scale to large memory sizes because the time195required to update the Merkle tree grows logarithmically in relation to the196memory size.197 198CPUs starting from Icelake use Total Memory Encryption (TME) in the place of199MEE. TME-based SGX implementations do not have an integrity Merkle tree, which200means integrity and replay-attacks are not mitigated. B, it includes201additional changes to prevent cipher text from being returned and SW memory202aliases from being created.203 204DMA to enclave memory is blocked by range registers on both MEE and TME systems205(SDM section 41.10).206 207Usage Models208============209 210Shared Library211--------------212 213Sensitive data and the code that acts on it is partitioned from the application214into a separate library. The library is then linked as a DSO which can be loaded215into an enclave. The application can then make individual function calls into216the enclave through special SGX instructions. A run-time within the enclave is217configured to marshal function parameters into and out of the enclave and to218call the correct library function.219 220Application Container221---------------------222 223An application may be loaded into a container enclave which is specially224configured with a library OS and run-time which permits the application to run.225The enclave run-time and library OS work together to execute the application226when a thread enters the enclave.227 228Impact of Potential Kernel SGX Bugs229===================================230 231EPC leaks232---------233 234When EPC page leaks happen, a WARNING like this is shown in dmesg:235 236"EREMOVE returned ... and an EPC page was leaked. SGX may become unusable..."237 238This is effectively a kernel use-after-free of an EPC page, and due239to the way SGX works, the bug is detected at freeing. Rather than240adding the page back to the pool of available EPC pages, the kernel241intentionally leaks the page to avoid additional errors in the future.242 243When this happens, the kernel will likely soon leak more EPC pages, and244SGX will likely become unusable because the memory available to SGX is245limited. However, while this may be fatal to SGX, the rest of the kernel246is unlikely to be impacted and should continue to work.247 248As a result, when this happens, user should stop running any new249SGX workloads, (or just any new workloads), and migrate all valuable250workloads. Although a machine reboot can recover all EPC memory, the bug251should be reported to Linux developers.252 253 254Virtual EPC255===========256 257The implementation has also a virtual EPC driver to support SGX enclaves258in guests. Unlike the SGX driver, an EPC page allocated by the virtual259EPC driver doesn't have a specific enclave associated with it. This is260because KVM doesn't track how a guest uses EPC pages.261 262As a result, the SGX core page reclaimer doesn't support reclaiming EPC263pages allocated to KVM guests through the virtual EPC driver. If the264user wants to deploy SGX applications both on the host and in guests265on the same machine, the user should reserve enough EPC (by taking out266total virtual EPC size of all SGX VMs from the physical EPC size) for267host SGX applications so they can run with acceptable performance.268 269Architectural behavior is to restore all EPC pages to an uninitialized270state also after a guest reboot. Because this state can be reached only271through the privileged ``ENCLS[EREMOVE]`` instruction, ``/dev/sgx_vepc``272provides the ``SGX_IOC_VEPC_REMOVE_ALL`` ioctl to execute the instruction273on all pages in the virtual EPC.274 275``EREMOVE`` can fail for three reasons. Userspace must pay attention276to expected failures and handle them as follows:277 2781. Page removal will always fail when any thread is running in the279 enclave to which the page belongs. In this case the ioctl will280 return ``EBUSY`` independent of whether it has successfully removed281 some pages; userspace can avoid these failures by preventing execution282 of any vcpu which maps the virtual EPC.283 2842. Page removal will cause a general protection fault if two calls to285 ``EREMOVE`` happen concurrently for pages that refer to the same286 "SECS" metadata pages. This can happen if there are concurrent287 invocations to ``SGX_IOC_VEPC_REMOVE_ALL``, or if a ``/dev/sgx_vepc``288 file descriptor in the guest is closed at the same time as289 ``SGX_IOC_VEPC_REMOVE_ALL``; it will also be reported as ``EBUSY``.290 This can be avoided in userspace by serializing calls to the ioctl()291 and to close(), but in general it should not be a problem.292 2933. Finally, page removal will fail for SECS metadata pages which still294 have child pages. Child pages can be removed by executing295 ``SGX_IOC_VEPC_REMOVE_ALL`` on all ``/dev/sgx_vepc`` file descriptors296 mapped into the guest. This means that the ioctl() must be called297 twice: an initial set of calls to remove child pages and a subsequent298 set of calls to remove SECS pages. The second set of calls is only299 required for those mappings that returned a nonzero value from the300 first call. It indicates a bug in the kernel or the userspace client301 if any of the second round of ``SGX_IOC_VEPC_REMOVE_ALL`` calls has302 a return code other than 0.303