x86/sgx/virt: implement SGX_IOC_VEPC_REMOVE ioctl

For bare-metal SGX on real hardware, the hardware provides guarantees
SGX state at reboot. For instance, all pages start out uninitialized.
The vepc driver provides a similar guarantee today for freshly-opened
vepc instances, but guests such as Windows expect all pages to be in
uninitialized state on startup, including after every guest reboot.

Some userspace implementations of virtual SGX would rather avoid having
to close and reopen the /dev/sgx_vepc file descriptor and re-mmap the
virtual EPC. For example, they could sandbox themselves after the guest
starts and forbid further calls to open(), in order to mitigate exploits
from untrusted guests.

Therefore, add a ioctl that does this with EREMOVE. Userspace can
invoke the ioctl to bring its vEPC pages back to uninitialized state.
There is a possibility that some pages fail to be removed if they are
SECS pages, and the child and SECS pages could be in separate vEPC
regions. Therefore, the ioctl returns the number of EREMOVE failures,
telling userspace to try the ioctl again after it's done with all
vEPC regions. A more verbose description of the correct usage and
the possible error conditions is documented in sgx.rst.

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lkml.kernel.org/r/20211021201155.1523989-3-pbonzini@redhat.com

authored by Paolo Bonzini and committed by Dave Hansen ae095b16 fd5128e6

+90
+35
Documentation/x86/sgx.rst
··· 250 250 on the same machine, the user should reserve enough EPC (by taking out 251 251 total virtual EPC size of all SGX VMs from the physical EPC size) for 252 252 host SGX applications so they can run with acceptable performance. 253 + 254 + Architectural behavior is to restore all EPC pages to an uninitialized 255 + state also after a guest reboot. Because this state can be reached only 256 + through the privileged ``ENCLS[EREMOVE]`` instruction, ``/dev/sgx_vepc`` 257 + provides the ``SGX_IOC_VEPC_REMOVE_ALL`` ioctl to execute the instruction 258 + on all pages in the virtual EPC. 259 + 260 + ``EREMOVE`` can fail for three reasons. Userspace must pay attention 261 + to expected failures and handle them as follows: 262 + 263 + 1. Page removal will always fail when any thread is running in the 264 + enclave to which the page belongs. In this case the ioctl will 265 + return ``EBUSY`` independent of whether it has successfully removed 266 + some pages; userspace can avoid these failures by preventing execution 267 + of any vcpu which maps the virtual EPC. 268 + 269 + 2. Page removal will cause a general protection fault if two calls to 270 + ``EREMOVE`` happen concurrently for pages that refer to the same 271 + "SECS" metadata pages. This can happen if there are concurrent 272 + invocations to ``SGX_IOC_VEPC_REMOVE_ALL``, or if a ``/dev/sgx_vepc`` 273 + file descriptor in the guest is closed at the same time as 274 + ``SGX_IOC_VEPC_REMOVE_ALL``; it will also be reported as ``EBUSY``. 275 + This can be avoided in userspace by serializing calls to the ioctl() 276 + and to close(), but in general it should not be a problem. 277 + 278 + 3. Finally, page removal will fail for SECS metadata pages which still 279 + have child pages. Child pages can be removed by executing 280 + ``SGX_IOC_VEPC_REMOVE_ALL`` on all ``/dev/sgx_vepc`` file descriptors 281 + mapped into the guest. This means that the ioctl() must be called 282 + twice: an initial set of calls to remove child pages and a subsequent 283 + set of calls to remove SECS pages. The second set of calls is only 284 + required for those mappings that returned a nonzero value from the 285 + first call. It indicates a bug in the kernel or the userspace client 286 + if any of the second round of ``SGX_IOC_VEPC_REMOVE_ALL`` calls has 287 + a return code other than 0.
+2
arch/x86/include/uapi/asm/sgx.h
··· 27 27 _IOW(SGX_MAGIC, 0x02, struct sgx_enclave_init) 28 28 #define SGX_IOC_ENCLAVE_PROVISION \ 29 29 _IOW(SGX_MAGIC, 0x03, struct sgx_enclave_provision) 30 + #define SGX_IOC_VEPC_REMOVE_ALL \ 31 + _IO(SGX_MAGIC, 0x04) 30 32 31 33 /** 32 34 * struct sgx_enclave_create - parameter structure for the
+53
arch/x86/kernel/cpu/sgx/virt.c
··· 150 150 return 0; 151 151 } 152 152 153 + static long sgx_vepc_remove_all(struct sgx_vepc *vepc) 154 + { 155 + struct sgx_epc_page *entry; 156 + unsigned long index; 157 + long failures = 0; 158 + 159 + xa_for_each(&vepc->page_array, index, entry) { 160 + int ret = sgx_vepc_remove_page(entry); 161 + if (ret) { 162 + if (ret == SGX_CHILD_PRESENT) { 163 + /* The page is a SECS, userspace will retry. */ 164 + failures++; 165 + } else { 166 + /* 167 + * Report errors due to #GP or SGX_ENCLAVE_ACT; do not 168 + * WARN, as userspace can induce said failures by 169 + * calling the ioctl concurrently on multiple vEPCs or 170 + * while one or more CPUs is running the enclave. Only 171 + * a #PF on EREMOVE indicates a kernel/hardware issue. 172 + */ 173 + WARN_ON_ONCE(encls_faulted(ret) && 174 + ENCLS_TRAPNR(ret) != X86_TRAP_GP); 175 + return -EBUSY; 176 + } 177 + } 178 + cond_resched(); 179 + } 180 + 181 + /* 182 + * Return the number of SECS pages that failed to be removed, so 183 + * userspace knows that it has to retry. 184 + */ 185 + return failures; 186 + } 187 + 153 188 static int sgx_vepc_release(struct inode *inode, struct file *file) 154 189 { 155 190 struct sgx_vepc *vepc = file->private_data; ··· 270 235 return 0; 271 236 } 272 237 238 + static long sgx_vepc_ioctl(struct file *file, 239 + unsigned int cmd, unsigned long arg) 240 + { 241 + struct sgx_vepc *vepc = file->private_data; 242 + 243 + switch (cmd) { 244 + case SGX_IOC_VEPC_REMOVE_ALL: 245 + if (arg) 246 + return -EINVAL; 247 + return sgx_vepc_remove_all(vepc); 248 + 249 + default: 250 + return -ENOTTY; 251 + } 252 + } 253 + 273 254 static const struct file_operations sgx_vepc_fops = { 274 255 .owner = THIS_MODULE, 275 256 .open = sgx_vepc_open, 257 + .unlocked_ioctl = sgx_vepc_ioctl, 258 + .compat_ioctl = sgx_vepc_ioctl, 276 259 .release = sgx_vepc_release, 277 260 .mmap = sgx_vepc_mmap, 278 261 };