Skip to content

Commit 08ceab2

Browse files
rchatrehansendc
authored andcommitted
selftests/sgx: Test reclaiming of untouched page
Removing a page from an initialized enclave involves three steps: (1) the user requests changing the page type to PT_TRIM via the SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl() (2) on success the ENCLU[EACCEPT] instruction is run from within the enclave to accept the page removal (3) the user initiates the actual removal of the page via the SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl(). Remove a page that has never been accessed. This means that when the first ioctl() requesting page removal arrives, there will be no page table entry, yet a valid page table entry needs to exist for the ENCLU[EACCEPT] function to succeed. In this test it is verified that a page table entry can still be installed for a page that is in the process of being removed. Suggested-by: Haitao Huang <haitao.huang@intel.com> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Jarkko Sakkinen <jarkko@kernel.org> Link: https://lkml.kernel.org/r/45e1b2a2fcd8c14597d04e40af5d8a9c1c5b017e.1652137848.git.reinette.chatre@intel.com
1 parent 35c7e6d commit 08ceab2

File tree

1 file changed

+80
-0
lines changed
  • tools/testing/selftests/sgx

1 file changed

+80
-0
lines changed

tools/testing/selftests/sgx/main.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,4 +1790,84 @@ TEST_F(enclave, remove_added_page_invalid_access_after_eaccept)
17901790
EXPECT_EQ(self->run.exception_addr, data_start);
17911791
}
17921792

1793+
TEST_F(enclave, remove_untouched_page)
1794+
{
1795+
struct sgx_enclave_remove_pages remove_ioc;
1796+
struct sgx_enclave_modify_types modt_ioc;
1797+
struct encl_op_eaccept eaccept_op;
1798+
unsigned long data_start;
1799+
int ret, errno_save;
1800+
1801+
ASSERT_TRUE(setup_test_encl(ENCL_HEAP_SIZE_DEFAULT, &self->encl, _metadata));
1802+
1803+
/*
1804+
* Hardware (SGX2) and kernel support is needed for this test. Start
1805+
* with check that test has a chance of succeeding.
1806+
*/
1807+
memset(&modt_ioc, 0, sizeof(modt_ioc));
1808+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc);
1809+
1810+
if (ret == -1) {
1811+
if (errno == ENOTTY)
1812+
SKIP(return,
1813+
"Kernel does not support SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl()");
1814+
else if (errno == ENODEV)
1815+
SKIP(return, "System does not support SGX2");
1816+
}
1817+
1818+
/*
1819+
* Invalid parameters were provided during sanity check,
1820+
* expect command to fail.
1821+
*/
1822+
EXPECT_EQ(ret, -1);
1823+
1824+
/* SGX2 is supported by kernel and hardware, test can proceed. */
1825+
memset(&self->run, 0, sizeof(self->run));
1826+
self->run.tcs = self->encl.encl_base;
1827+
1828+
data_start = self->encl.encl_base +
1829+
encl_get_data_offset(&self->encl) + PAGE_SIZE;
1830+
1831+
memset(&modt_ioc, 0, sizeof(modt_ioc));
1832+
1833+
modt_ioc.offset = encl_get_data_offset(&self->encl) + PAGE_SIZE;
1834+
modt_ioc.length = PAGE_SIZE;
1835+
modt_ioc.page_type = SGX_PAGE_TYPE_TRIM;
1836+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc);
1837+
errno_save = ret == -1 ? errno : 0;
1838+
1839+
EXPECT_EQ(ret, 0);
1840+
EXPECT_EQ(errno_save, 0);
1841+
EXPECT_EQ(modt_ioc.result, 0);
1842+
EXPECT_EQ(modt_ioc.count, 4096);
1843+
1844+
/*
1845+
* Enter enclave via TCS #1 and approve page removal by sending
1846+
* EACCEPT for removed page.
1847+
*/
1848+
1849+
eaccept_op.epc_addr = data_start;
1850+
eaccept_op.flags = SGX_SECINFO_TRIM | SGX_SECINFO_MODIFIED;
1851+
eaccept_op.ret = 0;
1852+
eaccept_op.header.type = ENCL_OP_EACCEPT;
1853+
1854+
EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
1855+
EXPECT_EEXIT(&self->run);
1856+
EXPECT_EQ(self->run.exception_vector, 0);
1857+
EXPECT_EQ(self->run.exception_error_code, 0);
1858+
EXPECT_EQ(self->run.exception_addr, 0);
1859+
EXPECT_EQ(eaccept_op.ret, 0);
1860+
1861+
memset(&remove_ioc, 0, sizeof(remove_ioc));
1862+
1863+
remove_ioc.offset = encl_get_data_offset(&self->encl) + PAGE_SIZE;
1864+
remove_ioc.length = PAGE_SIZE;
1865+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_REMOVE_PAGES, &remove_ioc);
1866+
errno_save = ret == -1 ? errno : 0;
1867+
1868+
EXPECT_EQ(ret, 0);
1869+
EXPECT_EQ(errno_save, 0);
1870+
EXPECT_EQ(remove_ioc.count, 4096);
1871+
}
1872+
17931873
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)