Skip to content

Commit 50b822e

Browse files
rchatrehansendc
authored andcommitted
selftests/sgx: Test faulty enclave behavior
Removing a page from an initialized enclave involves three steps: first the user requests changing the page type to SGX_PAGE_TYPE_TRIM via an ioctl(), on success the ENCLU[EACCEPT] instruction needs to be run from within the enclave to accept the page removal, finally the user requests page removal to be completed via an ioctl(). Only after acceptance (ENCLU[EACCEPT]) from within the enclave can the kernel remove the page from a running enclave. Test the behavior when the user's request to change the page type succeeds, but the ENCLU[EACCEPT] instruction is not run before the ioctl() requesting page removal is run. This should not be permitted. 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/fa5da30ebac108b7517194c3038b52995602b996.1652137848.git.reinette.chatre@intel.com
1 parent 33c5aac commit 50b822e

File tree

1 file changed

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

1 file changed

+114
-0
lines changed

tools/testing/selftests/sgx/main.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,4 +1433,118 @@ TEST_F(enclave, tcs_create)
14331433
munmap(addr, 3 * PAGE_SIZE);
14341434
}
14351435

1436+
/*
1437+
* Ensure sane behavior if user requests page removal, does not run
1438+
* EACCEPT from within enclave but still attempts to finalize page removal
1439+
* with the SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl(). The latter should fail
1440+
* because the removal was not EACCEPTed from within the enclave.
1441+
*/
1442+
TEST_F(enclave, remove_added_page_no_eaccept)
1443+
{
1444+
struct sgx_enclave_remove_pages remove_ioc;
1445+
struct encl_op_get_from_addr get_addr_op;
1446+
struct sgx_enclave_modify_types modt_ioc;
1447+
struct encl_op_put_to_addr put_addr_op;
1448+
unsigned long data_start;
1449+
int ret, errno_save;
1450+
1451+
ASSERT_TRUE(setup_test_encl(ENCL_HEAP_SIZE_DEFAULT, &self->encl, _metadata));
1452+
1453+
memset(&self->run, 0, sizeof(self->run));
1454+
self->run.tcs = self->encl.encl_base;
1455+
1456+
/*
1457+
* Hardware (SGX2) and kernel support is needed for this test. Start
1458+
* with check that test has a chance of succeeding.
1459+
*/
1460+
memset(&modt_ioc, 0, sizeof(modt_ioc));
1461+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc);
1462+
1463+
if (ret == -1) {
1464+
if (errno == ENOTTY)
1465+
SKIP(return,
1466+
"Kernel does not support SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl()");
1467+
else if (errno == ENODEV)
1468+
SKIP(return, "System does not support SGX2");
1469+
}
1470+
1471+
/*
1472+
* Invalid parameters were provided during sanity check,
1473+
* expect command to fail.
1474+
*/
1475+
EXPECT_EQ(ret, -1);
1476+
1477+
/*
1478+
* Page that will be removed is the second data page in the .data
1479+
* segment. This forms part of the local encl_buffer within the
1480+
* enclave.
1481+
*/
1482+
data_start = self->encl.encl_base +
1483+
encl_get_data_offset(&self->encl) + PAGE_SIZE;
1484+
1485+
/*
1486+
* Sanity check that page at @data_start is writable before
1487+
* removing it.
1488+
*
1489+
* Start by writing MAGIC to test page.
1490+
*/
1491+
put_addr_op.value = MAGIC;
1492+
put_addr_op.addr = data_start;
1493+
put_addr_op.header.type = ENCL_OP_PUT_TO_ADDRESS;
1494+
1495+
EXPECT_EQ(ENCL_CALL(&put_addr_op, &self->run, true), 0);
1496+
1497+
EXPECT_EEXIT(&self->run);
1498+
EXPECT_EQ(self->run.exception_vector, 0);
1499+
EXPECT_EQ(self->run.exception_error_code, 0);
1500+
EXPECT_EQ(self->run.exception_addr, 0);
1501+
1502+
/*
1503+
* Read memory that was just written to, confirming that data
1504+
* previously written (MAGIC) is present.
1505+
*/
1506+
get_addr_op.value = 0;
1507+
get_addr_op.addr = data_start;
1508+
get_addr_op.header.type = ENCL_OP_GET_FROM_ADDRESS;
1509+
1510+
EXPECT_EQ(ENCL_CALL(&get_addr_op, &self->run, true), 0);
1511+
1512+
EXPECT_EQ(get_addr_op.value, MAGIC);
1513+
EXPECT_EEXIT(&self->run);
1514+
EXPECT_EQ(self->run.exception_vector, 0);
1515+
EXPECT_EQ(self->run.exception_error_code, 0);
1516+
EXPECT_EQ(self->run.exception_addr, 0);
1517+
1518+
/* Start page removal by requesting change of page type to PT_TRIM */
1519+
memset(&modt_ioc, 0, sizeof(modt_ioc));
1520+
1521+
modt_ioc.offset = encl_get_data_offset(&self->encl) + PAGE_SIZE;
1522+
modt_ioc.length = PAGE_SIZE;
1523+
modt_ioc.page_type = SGX_PAGE_TYPE_TRIM;
1524+
1525+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_MODIFY_TYPES, &modt_ioc);
1526+
errno_save = ret == -1 ? errno : 0;
1527+
1528+
EXPECT_EQ(ret, 0);
1529+
EXPECT_EQ(errno_save, 0);
1530+
EXPECT_EQ(modt_ioc.result, 0);
1531+
EXPECT_EQ(modt_ioc.count, 4096);
1532+
1533+
/* Skip EACCEPT */
1534+
1535+
/* Send final ioctl() to complete page removal */
1536+
memset(&remove_ioc, 0, sizeof(remove_ioc));
1537+
1538+
remove_ioc.offset = encl_get_data_offset(&self->encl) + PAGE_SIZE;
1539+
remove_ioc.length = PAGE_SIZE;
1540+
1541+
ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_REMOVE_PAGES, &remove_ioc);
1542+
errno_save = ret == -1 ? errno : 0;
1543+
1544+
/* Operation not permitted since EACCEPT was omitted. */
1545+
EXPECT_EQ(ret, -1);
1546+
EXPECT_EQ(errno_save, EPERM);
1547+
EXPECT_EQ(remove_ioc.count, 0);
1548+
}
1549+
14361550
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)