Skip to content

Commit c6c2adc

Browse files
haitaohuanghansendc
authored andcommitted
x86/sgx: Resolves SECS reclaim vs. page fault for EAUG race
The SGX EPC reclaimer (ksgxd) may reclaim the SECS EPC page for an enclave and set secs.epc_page to NULL. The SECS page is used for EAUG and ELDU in the SGX page fault handler. However, the NULL check for secs.epc_page is only done for ELDU, not EAUG before being used. Fix this by doing the same NULL check and reloading of the SECS page as needed for both EAUG and ELDU. The SECS page holds global enclave metadata. It can only be reclaimed when there are no other enclave pages remaining. At that point, virtually nothing can be done with the enclave until the SECS page is paged back in. An enclave can not run nor generate page faults without a resident SECS page. But it is still possible for a #PF for a non-SECS page to race with paging out the SECS page: when the last resident non-SECS page A triggers a #PF in a non-resident page B, and then page A and the SECS both are paged out before the #PF on B is handled. Hitting this bug requires that race triggered with a #PF for EAUG. Following is a trace when it happens. BUG: kernel NULL pointer dereference, address: 0000000000000000 RIP: 0010:sgx_encl_eaug_page+0xc7/0x210 Call Trace: ? __kmem_cache_alloc_node+0x16a/0x440 ? xa_load+0x6e/0xa0 sgx_vma_fault+0x119/0x230 __do_fault+0x36/0x140 do_fault+0x12f/0x400 __handle_mm_fault+0x728/0x1110 handle_mm_fault+0x105/0x310 do_user_addr_fault+0x1ee/0x750 ? __this_cpu_preempt_check+0x13/0x20 exc_page_fault+0x76/0x180 asm_exc_page_fault+0x27/0x30 Fixes: 5a90d2c ("x86/sgx: Support adding of pages to an initialized enclave") Signed-off-by: Haitao Huang <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Reviewed-by: Kai Huang <[email protected]> Acked-by: Reinette Chatre <[email protected]> Cc:[email protected] Link: https://lore.kernel.org/all/20230728051024.33063-1-haitao.huang%40linux.intel.com
1 parent a5ef7d6 commit c6c2adc

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

arch/x86/kernel/cpu/sgx/encl.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx_encl_page *encl_page,
235235
return epc_page;
236236
}
237237

238+
/*
239+
* Ensure the SECS page is not swapped out. Must be called with encl->lock
240+
* to protect the enclave states including SECS and ensure the SECS page is
241+
* not swapped out again while being used.
242+
*/
243+
static struct sgx_epc_page *sgx_encl_load_secs(struct sgx_encl *encl)
244+
{
245+
struct sgx_epc_page *epc_page = encl->secs.epc_page;
246+
247+
if (!epc_page)
248+
epc_page = sgx_encl_eldu(&encl->secs, NULL);
249+
250+
return epc_page;
251+
}
252+
238253
static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl,
239254
struct sgx_encl_page *entry)
240255
{
@@ -248,11 +263,9 @@ static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl,
248263
return entry;
249264
}
250265

251-
if (!(encl->secs.epc_page)) {
252-
epc_page = sgx_encl_eldu(&encl->secs, NULL);
253-
if (IS_ERR(epc_page))
254-
return ERR_CAST(epc_page);
255-
}
266+
epc_page = sgx_encl_load_secs(encl);
267+
if (IS_ERR(epc_page))
268+
return ERR_CAST(epc_page);
256269

257270
epc_page = sgx_encl_eldu(entry, encl->secs.epc_page);
258271
if (IS_ERR(epc_page))
@@ -339,6 +352,13 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma,
339352

340353
mutex_lock(&encl->lock);
341354

355+
epc_page = sgx_encl_load_secs(encl);
356+
if (IS_ERR(epc_page)) {
357+
if (PTR_ERR(epc_page) == -EBUSY)
358+
vmret = VM_FAULT_NOPAGE;
359+
goto err_out_unlock;
360+
}
361+
342362
epc_page = sgx_alloc_epc_page(encl_page, false);
343363
if (IS_ERR(epc_page)) {
344364
if (PTR_ERR(epc_page) == -EBUSY)

0 commit comments

Comments
 (0)