x86/sgx/virt: extract sgx_vepc_remove_page

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.

One way to do this is to simply close and reopen the /dev/sgx_vepc file
descriptor and re-mmap the virtual EPC. However, this is problematic
because it prevents sandboxing the userspace (for example forbidding
open() after the guest starts; this is doable with heavy use of SCM_RIGHTS
file descriptor passing).

In order to implement this, we will need a ioctl that performs
EREMOVE on all pages mapped by a /dev/sgx_vepc file descriptor:
other possibilities, such as closing and reopening the device,
are racy.

Start the implementation by creating a separate function with just
the __eremove wrapper.

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Reviewed-by: Dave Hansen <dave.hansen@linux.intel.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-2-pbonzini@redhat.com

authored by Paolo Bonzini and committed by Dave Hansen fd5128e6 519d8195

+7 -5
+7 -5
arch/x86/kernel/cpu/sgx/virt.c
··· 111 return 0; 112 } 113 114 - static int sgx_vepc_free_page(struct sgx_epc_page *epc_page) 115 { 116 - int ret; 117 - 118 /* 119 * Take a previously guest-owned EPC page and return it to the 120 * general EPC page pool. ··· 122 * case that a guest properly EREMOVE'd this page, a superfluous 123 * EREMOVE is harmless. 124 */ 125 - ret = __eremove(sgx_get_epc_virt_addr(epc_page)); 126 if (ret) { 127 /* 128 * Only SGX_CHILD_PRESENT is expected, which is because of ··· 147 } 148 149 sgx_free_epc_page(epc_page); 150 - 151 return 0; 152 } 153
··· 111 return 0; 112 } 113 114 + static int sgx_vepc_remove_page(struct sgx_epc_page *epc_page) 115 { 116 /* 117 * Take a previously guest-owned EPC page and return it to the 118 * general EPC page pool. ··· 124 * case that a guest properly EREMOVE'd this page, a superfluous 125 * EREMOVE is harmless. 126 */ 127 + return __eremove(sgx_get_epc_virt_addr(epc_page)); 128 + } 129 + 130 + static int sgx_vepc_free_page(struct sgx_epc_page *epc_page) 131 + { 132 + int ret = sgx_vepc_remove_page(epc_page); 133 if (ret) { 134 /* 135 * Only SGX_CHILD_PRESENT is expected, which is because of ··· 144 } 145 146 sgx_free_epc_page(epc_page); 147 return 0; 148 } 149