Swap and fault all the pages of the test enclave binary
Subsystems (4)
Jarkko Sakkinen
maintainerv2 cover Swap and fault all the pages of the test enclave binary
Create a heap for the test enclave, which has the same size as all available Enclave Page Cache (EPC) pages in the system. This will guarantee that all test_encl.elf pages *and* SGX Enclave Control Structure (SECS) have been swapped out by the page reclaimer during the load time. Actually, this adds a bit more stress than that since part of the EPC gets reserved for the Version Array (VA) pages.
For each test, the page fault handler gets triggered in two occasions:
- When SGX_IOC_ENCLAVE_INIT is performed, SECS gets swapped in by the page fault handler.
- During the execution, each page that is referenced gets swapped in by the page fault handler.
Jarkko Sakkinen (3):
x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem selftests/sgx: Assign source for each segment selftests/sgx: Trigger the reclaimer and #PF handler
Tianjia Zhang (1):
selftests/sgx: Fix calculations for sub-maximum field sizes
Documentation/x86/sgx.rst | 6 +++ arch/x86/kernel/cpu/sgx/main.c | 10 ++++- tools/testing/selftests/sgx/load.c | 38 ++++++++++++++---- tools/testing/selftests/sgx/main.c | 42 +++++++++++++++++++- tools/testing/selftests/sgx/main.h | 4 +- tools/testing/selftests/sgx/sigstruct.c | 53 +++++++++++++------------ 6 files changed, 117 insertions(+), 36 deletions(-)
Signature · 1 line
2.32.0
Jarkko Sakkinen
maintainerPATCH v2 1/4 x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem
Create /sys/kernel/debug/x86/sgx_total_mem, after the driver is initialized, describing the total amount of SGX reserved memory (also known as Enclave Page Cache (EPC)) available in the system.
This can be used to implement stress that trigger the page reclaimer, and it is probably useful information when admistering a system running enclaves anyhow.
---
v2:
* sgx_nr_all_pages -> sgx_total_mem
Documentation/x86/sgx.rst | 6 ++++++ arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
Documentation/x86/sgx.rst +6 -0
@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests on the same machine, the user should reserve enough EPC (by taking out total virtual EPC size of all SGX VMs from the physical EPC size) for host SGX applications so they can run with acceptable performance. + +SGX debugging +============= + +*/sys/kernel/debug/x86/sgx_total_mem* describes the total available +SGX mwemory available in the system.
arch/x86/kernel/cpu/sgx/main.c +9 -1
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2016-20 Intel Corporation. */ +#include <linux/debugfs.h> #include <linux/file.h> #include <linux/freezer.h> #include <linux/highmem.h>
@@ -28,7 +29,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);static LIST_HEAD (sgx_active_page_list);static DEFINE_SPINLOCK (sgx_reclaimer_lock); -/* The free page list lock protected variables prepend the lock. */ +/* Total EPC memory available in bytes. */ +static unsigned long sgx_total_mem; + +/* The number of free EPC pages in all nodes. */ static unsigned long sgx_nr_free_pages;/* Nodes with one or more EPC sections. */
@@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,list_add_tail (§ion->pages[i].list, &sgx_dirty_page_list); } + sgx_total_mem += nr_pages *PAGE_SIZE ; +return true; }
@@ -823,6 +829,8 @@ static int __init sgx_init(void)if (sgx_vepc_init () && ret) goto err_provision; +debugfs_create_ulong ("sgx_total_mem" ,0444 , arch_debugfs_dir, &sgx_total_mem); +return 0 ; err_provision:
Signature · 1 line
2.32.0
Re: [PATCH v2 1/4] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem
Quoted 7 lines · Jarkko Sakkinen
Create
/sys/kernel/debug/x86/sgx_total_mem, after the driver is initialized, describing the total amount of SGX reserved memory (also known as Enclave Page Cache (EPC)) available in the system.This can be used to implement stress that trigger the page reclaimer, and it is probably useful information when admistering a system running enclaves anyhow.
How about this as a rewrite?
Just like normal memory, SGX memory can be overcommitted. SGX has its own reclaim mechanism which kicks in when physical SGX memory (Enclave Page Cache / EPC) is exhausted. That reclaim mechanism is relatively rarely exercised and needs selftests to poke at it.
The amount of EPC on the system is determined by the BIOS and it varies wildly between systems. It can be dozens of MB on desktops, or many GB on servers.
To run in a reasonable amount of time, the selftest needs to know how much EPC there is in the system.
Introduce a new debugfs file to export that information.
Quoted 5 lines
Documentation/x86/sgx.rst +0
-0
Documentation/x86/sgx.rst user wants to deploy SGX applications both on the host and in guests · line 258
Quoted 9 lines
Documentation/x86/sgx.rst +6
-0@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests on the same machine, the user should reserve enough EPC (by taking out total virtual EPC size of all SGX VMs from the physical EPC size) for host SGX applications so they can run with acceptable performance. + +SGX debugging +============= + +*/sys/kernel/debug/x86/sgx_total_mem* describes the total available +SGX mwemory available in the system.
^ memory
It would also be nice to say what the units are here, even if they are bytes.
Quoted 35 lines
arch/x86/kernel/cpu/sgx/main.c +7
-1@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2016-20 Intel Corporation. */ +#include <linux/debugfs.h> #include <linux/file.h> #include <linux/freezer.h> #include <linux/highmem.h> @@ -28,7 +29,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);static LIST_HEAD (sgx_active_page_list);static DEFINE_SPINLOCK (sgx_reclaimer_lock); -/* The free page list lock protected variables prepend the lock. */ +/* Total EPC memory available in bytes. */ +static unsigned long sgx_total_mem; + +/* The number of free EPC pages in all nodes. */ static unsigned long sgx_nr_free_pages;/* Nodes with one or more EPC sections. */ @@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,list_add_tail (§ion->pages[i].list, &sgx_dirty_page_list); } + sgx_total_mem += nr_pages *PAGE_SIZE ; +return true; }
arch/x86/kernel/cpu/sgx/main.c static int __init sgx_init(void) · line 836
Quoted 8 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0@@ -823,6 +829,8 @@ static int __init sgx_init(void)if (sgx_vepc_init () && ret) goto err_provision; +debugfs_create_ulong ("sgx_total_mem" ,0444 , arch_debugfs_dir, &sgx_total_mem); +return 0 ; err_provision:
Assuming the new changelog is adopted and typos fixed:
Jarkko Sakkinen
maintainerRe: [PATCH v2 1/4] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem
Quoted 86 lines · Dave Hansen
Quoted 7 lines · Jarkko Sakkinen
Create
/sys/kernel/debug/x86/sgx_total_mem, after the driver is initialized, describing the total amount of SGX reserved memory (also known as Enclave Page Cache (EPC)) available in the system.This can be used to implement stress that trigger the page reclaimer, and it is probably useful information when admistering a system running enclaves anyhow.
How about this as a rewrite?
Just like normal memory, SGX memory can be overcommitted. SGX has its own reclaim mechanism which kicks in when physical SGX memory (Enclave Page Cache / EPC) is exhausted. That reclaim mechanism is relatively rarely exercised and needs selftests to poke at it.
The amount of EPC on the system is determined by the BIOS and it varies wildly between systems. It can be dozens of MB on desktops, or many GB on servers.
To run in a reasonable amount of time, the selftest needs to know how much EPC there is in the system.
Introduce a new debugfs file to export that information.
Quoted 5 lines
Documentation/x86/sgx.rst +0
-0Documentation/x86/sgx.rst user wants to deploy SGX applications both on the host and in guests · line 258
Quoted 9 lines
Documentation/x86/sgx.rst +6
-0@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests on the same machine, the user should reserve enough EPC (by taking out total virtual EPC size of all SGX VMs from the physical EPC size) for host SGX applications so they can run with acceptable performance. + +SGX debugging +============= + +*/sys/kernel/debug/x86/sgx_total_mem* describes the total available +SGX mwemory available in the system.^ memory
It would also be nice to say what the units are here, even if they are bytes.
Quoted 41 lines
arch/x86/kernel/cpu/sgx/main.c +9
-1@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2016-20 Intel Corporation. */ +#include <linux/debugfs.h> #include <linux/file.h> #include <linux/freezer.h> #include <linux/highmem.h> @@ -28,7 +29,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);static LIST_HEAD (sgx_active_page_list);static DEFINE_SPINLOCK (sgx_reclaimer_lock); -/* The free page list lock protected variables prepend the lock. */ +/* Total EPC memory available in bytes. */ +static unsigned long sgx_total_mem; + +/* The number of free EPC pages in all nodes. */ static unsigned long sgx_nr_free_pages;/* Nodes with one or more EPC sections. */ @@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,list_add_tail (§ion->pages[i].list, &sgx_dirty_page_list); } + sgx_total_mem += nr_pages *PAGE_SIZE ; +return true; }@@ -823,6 +829,8 @@ static int __init sgx_init(void)if (sgx_vepc_init () && ret) goto err_provision; +debugfs_create_ulong ("sgx_total_mem" ,0444 , arch_debugfs_dir, &sgx_total_mem); +return 0 ;
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c static int __init sgx_init(void) · line 836
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +0
-0err_provision:Assuming the new changelog is adopted and typos fixed:
Acked-by Dave Hansen <[email protected]>
Thanks, I'll commit these changes and include to the next version.
/Jarkko
Jarkko Sakkinen
maintainerPATCH v2 2/4 selftests/sgx: Fix calculations for sub-maximum field sizes
In reply to Jarkko SakkinenFrom: Tianjia Zhang <[email protected]>
Q1 and Q2 are numbers with *maximum* length of 384 bytes. If the calculated length of Q1 and Q2 is less than 384 bytes, things will go wrong.
E.g. if Q2 is 383 bytes, then
1. The bytes of q2 are copied to sigstruct->q2 in calc_q1q2().
2. The entire sigstruct->q2 is reversed, which results it being 256 * Q2, given that the last byte of sigstruct->q2 is added to before the bytes given by calc_q1q2().
Either change in key or measurement can trigger the bug. E.g. an unmeasured heap could cause a devastating change in Q1 or Q2.
Reverse exactly the bytes of Q1 and Q2 in calc_q1q2() before returning to the caller.
The original patch did a bad job explaining the code change but it turned out making sense. I wrote a new description.
---
v4:
- Updated the short summary according to:
https://lore.kernel.org/linux-sgx/[email protected]/
v3:
- Adjusted the fixes tag to refer to the correct commit
v2:
- Added a fixes tag.
tools/testing/selftests/sgx/sigstruct.c | 41 +++++++++++++------------ 1 file changed, 21 insertions(+), 20 deletions(-)
tools/testing/selftests/sgx/sigstruct.c +21 -20
@@ -55,10 +55,27 @@ static bool alloc_q1q2_ctx(const uint8_t *s, const uint8_t *m,return true; } +static void reverse_bytes (void *data,int length) +{ +int i =0 ; +int j = length -1 ; +uint8_t temp; +uint8_t *ptr = data; + +while (i < j) { + temp = ptr[i]; + ptr[i] = ptr[j]; + ptr[j] = temp; + i++; + j--; + } +} +static bool calc_q1q2 (const uint8_t *s,const uint8_t *m,uint8_t *q1,uint8_t *q2) {struct q1q2_ctx ctx; +int len;if (!alloc_q1q2_ctx (s, m, &ctx)) {fprintf (stderr,"Not enough memory for Q1Q2 calculation\n" );
@@ -89,8 +106,10 @@ static bool calc_q1q2(const uint8_t *s, const uint8_t *m, uint8_t *q1, goto out; } -BN_bn2bin (ctx.q1, q1); -BN_bn2bin (ctx.q2, q2); + len =BN_bn2bin (ctx.q1, q1); +reverse_bytes (q1, len); + len =BN_bn2bin (ctx.q2, q2); +reverse_bytes (q2, len);free_q1q2_ctx (&ctx);return true;
@@ -152,22 +171,6 @@ static RSA *gen_sign_key(void)return key; } -static void reverse_bytes (void *data,int length) -{ -int i =0 ; -int j = length -1 ; -uint8_t temp; -uint8_t *ptr = data; - -while (i < j) { - temp = ptr[i]; - ptr[i] = ptr[j]; - ptr[j] = temp; - i++; - j--; - } -} -enum mrtags {MRECREATE =0x0045544145524345 ,MREADD =0x0000000044444145 ,
@@ -367,8 +370,6 @@ bool encl_measure(struct encl *encl)/* BE -> LE */ reverse_bytes (sigstruct->signature,SGX_MODULUS_SIZE );reverse_bytes (sigstruct->modulus,SGX_MODULUS_SIZE ); -reverse_bytes (sigstruct->q1,SGX_MODULUS_SIZE ); -reverse_bytes (sigstruct->q2,SGX_MODULUS_SIZE );EVP_MD_CTX_destroy (ctx);RSA_free (key);
Signature · 1 line
2.32.0
Jarkko Sakkinen
maintainerPATCH v2 3/4 selftests/sgx: Assign source for each segment
In reply to Jarkko SakkinenDefine source per segment so that enclave pages can be added from different sources, e.g. anonymous VMA for zero pages. In other words, add 'src' field to struct encl_segment, and assign it to 'encl->src' for pages inherited from the enclave binary.
--- tools/testing/selftests/sgx/load.c | 5 +++-- tools/testing/selftests/sgx/main.h | 1 + tools/testing/selftests/sgx/sigstruct.c | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/sgx/load.c +3 -2
@@ -107,7 +107,7 @@ static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg)memset (&secinfo,0 ,sizeof (secinfo)); secinfo.flags = seg->flags; - ioc.src = (uint64_t )encl->src + seg->offset; + ioc.src = (uint64_t )seg->src; ioc.offset = seg->offset; ioc.length = seg->size; ioc.secinfo = (unsigned long )&secinfo;
@@ -226,6 +226,7 @@ bool encl_load(const char *path, struct encl *encl)if (j ==0 ) { src_offset = phdr->p_offset &PAGE_MASK ; + encl->src = encl->bin + src_offset; seg->prot =PROT_READ |PROT_WRITE ; seg->flags =SGX_PAGE_TYPE_TCS <<8 ;
@@ -238,13 +239,13 @@ bool encl_load(const char *path, struct encl *encl) seg->offset = (phdr->p_offset &PAGE_MASK ) - src_offset; seg->size = (phdr->p_filesz +PAGE_SIZE -1 ) &PAGE_MASK ; + seg->src = encl->src + seg->offset; j++; }assert (j == encl->nr_segments); - encl->src = encl->bin + src_offset; encl->src_size = encl->segment_tbl[j -1 ].offset + encl->segment_tbl[j -1 ].size;
tools/testing/selftests/sgx/main.h +1 -0
@@ -7,6 +7,7 @@#define MAIN_H struct encl_segment { +void *src;off_t offset;size_t size;unsigned int prot;
tools/testing/selftests/sgx/sigstruct.c +4 -4
@@ -289,14 +289,14 @@ static bool mrenclave_eextend(EVP_MD_CTX *ctx, uint64_t offset,static bool mrenclave_segment (EVP_MD_CTX *ctx,struct encl *encl,struct encl_segment *seg) { -uint64_t end = seg->offset + seg->size; +uint64_t end = seg->size;uint64_t offset; -for (offset = seg->offset; offset < end; offset +=PAGE_SIZE ) { -if (!mrenclave_eadd (ctx, offset, seg->flags)) +for (offset =0 ; offset < end; offset +=PAGE_SIZE ) { +if (!mrenclave_eadd (ctx, seg->offset + offset, seg->flags))return false; -if (!mrenclave_eextend (ctx, offset, encl->src + offset)) +if (!mrenclave_eextend (ctx, seg->offset + offset, seg->src + offset))return false; }
Signature · 1 line
2.32.0
Jarkko Sakkinen
maintainerPATCH v2 4/4 selftests/sgx: Trigger the reclaimer and #PF handler
In reply to Jarkko SakkinenCreate a heap for the test enclave, which has the same size as all available Enclave Page Cache (EPC) pages in the system. This will guarantee that all test_encl.elf pages *and* SGX Enclave Control Structure (SECS) have been swapped out by the page reclaimer during the load time. Actually, this adds a bit more stress than that since part of the EPC gets reserved for the Version Array (VA) pages.
For each test, the page fault handler gets triggered in two occasions:
- When SGX_IOC_ENCLAVE_INIT is performed, SECS gets swapped in by the page fault handler.
- During the execution, each page that is referenced gets swapped in by the page fault handler.
In addition, add two new checks:
1. All of the EPC memory must be unused before the test execution starts.
2. Before SGX_IOC_ENCLAVE_INIT, the number of free EPC pages must be zero.
---
v2:
* sgx_nr_all_pages -> sgx_total_mem
tools/testing/selftests/sgx/load.c | 33 +++++++++++++++---- tools/testing/selftests/sgx/main.c | 42 ++++++++++++++++++++++++- tools/testing/selftests/sgx/main.h | 3 +- tools/testing/selftests/sgx/sigstruct.c | 6 ++-- 4 files changed, 74 insertions(+), 10 deletions(-)
tools/testing/selftests/sgx/load.c +27 -6
@@ -21,6 +21,8 @@void encl_delete (struct encl *encl) { +struct encl_segment *heap_seg = &encl->segment_tbl[encl->nr_segments -1 ]; +if (encl->encl_base)munmap ((void *)encl->encl_base, encl->encl_size);
@@ -30,6 +32,8 @@ void encl_delete(struct encl *encl)if (encl->fd)close (encl->fd); +munmap (heap_seg->src, heap_seg->size); +if (encl->segment_tbl)free (encl->segment_tbl);
@@ -111,7 +115,10 @@ static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg) ioc.offset = seg->offset; ioc.length = seg->size; ioc.secinfo = (unsigned long )&secinfo; - ioc.flags =SGX_PAGE_MEASURE ; +if (seg->measure) + ioc.flags =SGX_PAGE_MEASURE ; +else + ioc.flags =0 ; rc =ioctl (encl->fd,SGX_IOC_ENCLAVE_ADD_PAGES , &ioc);if (rc <0 ) {
@@ -124,9 +131,10 @@ static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg) -bool encl_load (const char *path,struct encl *encl) +bool encl_load (const char *path,struct encl *encl,unsigned long heap_size) {const char device_path[] ="/dev/sgx_enclave" ; +struct encl_segment *seg;Elf64_Phdr *phdr_tbl;off_t src_offset;Elf64_Ehdr *ehdr;
@@ -188,6 +196,8 @@ bool encl_load(const char *path, struct encl *encl) ehdr = encl->bin; phdr_tbl = encl->bin + ehdr->e_phoff; + encl->nr_segments =1 ;/* one for the heap */ +for (i =0 ; i < ehdr->e_phnum; i++) {Elf64_Phdr *phdr = &phdr_tbl[i];
@@ -203,7 +213,6 @@ bool encl_load(const char *path, struct encl *encl)for (i =0 , j =0 ; i < ehdr->e_phnum; i++) {Elf64_Phdr *phdr = &phdr_tbl[i];unsigned int flags = phdr->p_flags; -struct encl_segment *seg;if (phdr->p_type !=PT_LOAD )continue ;
@@ -240,14 +249,26 @@ bool encl_load(const char *path, struct encl *encl) seg->offset = (phdr->p_offset &PAGE_MASK ) - src_offset; seg->size = (phdr->p_filesz +PAGE_SIZE -1 ) &PAGE_MASK ; seg->src = encl->src + seg->offset; + seg->measure = true; j++; } -assert (j == encl->nr_segments); +assert (j == encl->nr_segments -1 ); + + seg = &encl->segment_tbl[j]; + seg->offset = encl->segment_tbl[j -1 ].offset + encl->segment_tbl[j -1 ].size; + seg->size = heap_size; + seg->src =mmap (NULL , heap_size,PROT_READ |PROT_WRITE , +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); + seg->prot =PROT_READ |PROT_WRITE ; + seg->flags = (SGX_PAGE_TYPE_REG <<8 ) | seg->prot; + seg->measure = false; + +if (seg->src ==MAP_FAILED ) + goto err; - encl->src_size = encl->segment_tbl[j -1 ].offset + - encl->segment_tbl[j -1 ].size; + encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;for (encl->encl_size =4096 ; encl->encl_size < encl->src_size; ) encl->encl_size <<=1 ;
tools/testing/selftests/sgx/main.c +41 -1
58 lines not on this page · raw message
tools/testing/selftests/sgx/main.h +2 -1
@@ -12,6 +12,7 @@ struct encl_segment {size_t size;unsigned int prot;unsigned int flags; +bool measure; };struct encl {
@@ -32,7 +33,7 @@ extern unsigned char sign_key[];extern unsigned char sign_key_end[];void encl_delete (struct encl *ctx); -bool encl_load (const char *path,struct encl *encl); +bool encl_load (const char *path,struct encl *encl,unsigned long heap_size);bool encl_measure (struct encl *encl);bool encl_build (struct encl *encl);
tools/testing/selftests/sgx/sigstruct.c +4 -2
@@ -296,8 +296,10 @@ static bool mrenclave_segment(EVP_MD_CTX *ctx, struct encl *encl,if (!mrenclave_eadd (ctx, seg->offset + offset, seg->flags))return false; -if (!mrenclave_eextend (ctx, seg->offset + offset, seg->src + offset)) -return false; +if (seg->measure) { +if (!mrenclave_eextend (ctx, seg->offset + offset, seg->src + offset)) +return false; + } }return true;
Signature · 1 line
2.32.0