/
se_ptrace.c
749 lines (653 loc) · 21.2 KB
/
se_ptrace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
* Copyright (C) 2011-2021 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "arch.h"
#include "thread_data.h"
#include "util.h"
#include "se_trace.h"
#include "se_memory.h"
#include <unistd.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/user.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <elf.h>
#include <assert.h>
#include <signal.h>
#include <sys/wait.h>
//NOTE: Need align with thread_data_t in RTS.
#define ELF32_SSA_FS_OFFSET 0x34
#ifdef __x86_64__
#define SSA2USER_REG(to, from, name) to->r##name = from.r##name
#define USER_REG2SSA(to, from, name) to.r##name = from->r##name
#else
#define SSA2USER_REG(to, from, name) to->e##name = from.e##name
#define USER_REG2SSA(to, from, name) to.e##name = from->e##name
#endif
#define XSTATE_MAX_SIZE 832
typedef enum _direction_t
{
FORWARD,
BACKWARD
} direction_t;
typedef long int (* ptrace_t)(enum __ptrace_request request, pid_t pid,
void *addr, void *data);
typedef pid_t (*waitpid_t)(pid_t pid, int *status, int options);
static ptrace_t g_sys_ptrace = NULL;
static waitpid_t g_sys_waitpid = NULL;
__attribute__((constructor)) void init()
{
g_sys_ptrace = (ptrace_t)dlsym(RTLD_NEXT, "ptrace");
g_sys_waitpid = (waitpid_t)dlsym(RTLD_NEXT, "waitpid");
}
#ifdef SE_DEBUG
static void dump_ssa_gregs(ssa_gpr_t* gpr) __attribute__((unused));
void dump_ssa_gregs(ssa_gpr_t* gpr)
{
SE_TRACE(SE_TRACE_DEBUG, "ssa generic registers:\n");
SE_TRACE(SE_TRACE_DEBUG, "xbx = %#lx\t", gpr->REG(bx));
SE_TRACE(SE_TRACE_DEBUG, "xcx = %#lx\t", gpr->REG(cx));
SE_TRACE(SE_TRACE_DEBUG, "xdx = %#lx\t", gpr->REG(dx));
SE_TRACE(SE_TRACE_DEBUG, "xsi = %#lx\t", gpr->REG(si));
SE_TRACE(SE_TRACE_DEBUG, "xdi = %#lx\t", gpr->REG(di));
SE_TRACE(SE_TRACE_DEBUG, "xbp = %#lx\t", gpr->REG(bp));
SE_TRACE(SE_TRACE_DEBUG, "xax = %#lx\t", gpr->REG(ax));
SE_TRACE(SE_TRACE_DEBUG, "xip = %#lx\t", gpr->REG(ip));
SE_TRACE(SE_TRACE_DEBUG, "xflags = %#lx\t", gpr->REG(flags));
SE_TRACE(SE_TRACE_DEBUG, "xsp = %#lx\t", gpr->REG(sp));
}
static void dump_regs(struct user_regs_struct *regs) __attribute__((unused));
void dump_regs(struct user_regs_struct *regs)
{
SE_TRACE(SE_TRACE_DEBUG, "user regisers:\n");
SE_TRACE(SE_TRACE_DEBUG, "xbx = %#x\t", regs->REG(bx));
SE_TRACE(SE_TRACE_DEBUG, "xcx = %#x\t", regs->REG(cx));
SE_TRACE(SE_TRACE_DEBUG, "xdx = %#x\t", regs->REG(dx));
SE_TRACE(SE_TRACE_DEBUG, "xsi = %#x\t", regs->REG(si));
SE_TRACE(SE_TRACE_DEBUG, "xdi = %#x\t", regs->REG(di));
SE_TRACE(SE_TRACE_DEBUG, "xbp = %#x\t", regs->REG(bp));
SE_TRACE(SE_TRACE_DEBUG, "xax = %#x\t", regs->REG(ax));
SE_TRACE(SE_TRACE_DEBUG, "xip = %#x\t", regs->REG(ip));
SE_TRACE(SE_TRACE_DEBUG, "xflags = %#x\t", regs->eflags);
SE_TRACE(SE_TRACE_DEBUG, "xsp = %#x\t", regs->REG(sp));
}
#else
#define dump_ssa_gregs(gpr)
#define dump_regs(regs)
#endif
#ifdef __x86_64__
static int get_exec_class(pid_t pid)
{
char filename[64];
int fd = -1;
unsigned char e_ident[EI_NIDENT];
snprintf(filename, 64, "/proc/%d/exe", pid);
fd = open(filename, O_RDONLY | O_LARGEFILE);
if(fd == -1)
return ELFCLASSNONE;
if(-1 == read(fd, e_ident, EI_NIDENT))
{
close(fd);
return ELFCLASSNONE;
}
close(fd);
return e_ident[EI_CLASS];
}
#endif
static inline uint32_t get_ssa_frame_size(pid_t pid, thread_data_t* td)
{
uint32_t ssa_frame_size = ROUND_TO_PAGE(td->xsave_size) >> SE_PAGE_SHIFT;
#ifdef __x86_64__
//on x64, we may debug elf32 enclave, we need refer to different offset in td field.
if(ELFCLASS32 == get_exec_class(pid))
{
ssa_frame_size = *GET_PTR(uint32_t, td, ELF32_SSA_FS_OFFSET);
}
#else
UNUSED(pid);
#endif
//When debug trts, ssa_frame_size in TD is not initialized, so the value will be 0.
//It is a limitation to debug trts. As a workaround, set the default page to 1, so
//we can debug enclave from the start of enclave_entry. It works on most platforms
//except the enclave supports AMX.
//AMX makes the ssa_frame_size more than 1 page. If AMX is enabled, the debugger will
//only be workable after TD is initialized.
if(0 == ssa_frame_size)
ssa_frame_size = 1;
return ssa_frame_size;
}
/*
*This function get the position/offset with SSA
* @pid, process id
* @tcs_addr, TCS start address
* @dir, calculate the position from start of SSA or from the end of SSA
* @offset, offset from the start
* @size, size of data from the postion that is going to be accessed
* @pos, the result of postion that the function output
* @return, TRUE on success, FALSE on fail. The result is copied to parameter pos
* */
static int get_ssa_pos(pid_t pid, long tcs_addr, direction_t dir, long offset, long size, long *pos)
{
tcs_t tcs;
thread_data_t td;
uint32_t ssa_frame_size = 0;
long addr = 0;
//read TCS;
if(!se_read_process_mem(pid, (void *)tcs_addr, (void *)&tcs, 72, NULL))
return FALSE;
//Align with RTS. We assume TD is next to TCS
long ssa_start = tcs_addr + TCS_SIZE;
//ossa point to the start address of SSA, and fs/gs point to the start address of TD.
long td_start = ssa_start - tcs.ossa + tcs.ofs_base;
//Read thread data; On x64, sizeof(thread_data_t) of elf64 is larger than elf32,
//so it won't miss any field if it is elf32 executable;
if(!se_read_process_mem(pid, (void *)td_start, (void *)&td, sizeof(thread_data_t), NULL))
return FALSE;
ssa_frame_size = get_ssa_frame_size(pid, &td);
//The request should not exceed ssa frame boundary.
if((offset + size) > (long)ssa_frame_size * SE_PAGE_SIZE)
return FALSE;
assert(tcs.cssa > 0);
//If it is required to calculate from the start of SSA
if(FORWARD == dir)
{
addr = ssa_start + (tcs.cssa - 1) * ssa_frame_size * SE_PAGE_SIZE + offset;
}
//If it is required to calculate from the end of SSA
else if(BACKWARD == dir)
{
addr = ssa_start + tcs.cssa * ssa_frame_size * SE_PAGE_SIZE - offset;
}
else
return FALSE;
*pos = addr;
return TRUE;
}
static inline int read_ssa(pid_t pid, long tcs_addr, direction_t dir, long offset, long size, void *buf)
{
long addr = 0;
if(!get_ssa_pos(pid, tcs_addr, dir, offset, size, &addr))
return FALSE;
//read the content of ssa
if(!se_read_process_mem(pid, (void *)addr, buf, size, NULL))
return FALSE;
return TRUE;
}
static inline int write_ssa(pid_t pid, long tcs_addr, direction_t dir, long offset, long size, void *buf)
{
long addr = 0;
if(!get_ssa_pos(pid, tcs_addr, dir, offset, size, &addr))
return FALSE;
//write the content of ssa
if(!se_write_process_mem(pid, (void *)addr, buf, size, NULL))
return FALSE;
return TRUE;
}
static inline int get_ssa_gpr(pid_t pid, long tcs_addr, ssa_gpr_t* gpr)
{
//read general registers. ssa_gpr_t is elf32/elf64 independent.
return read_ssa(pid, tcs_addr, BACKWARD, sizeof(ssa_gpr_t), sizeof(ssa_gpr_t), (void *)gpr);
}
static inline int set_ssa_gpr(pid_t pid, long tcs_addr, ssa_gpr_t* gpr)
{
//read general registers. ssa_gpr_t is elf32/elf64 independent.
return write_ssa(pid, tcs_addr, BACKWARD, sizeof(ssa_gpr_t), sizeof(ssa_gpr_t), (void *)gpr);
}
static inline int get_ssa_fpregs(pid_t pid, long tcs_addr, struct user_fpregs_struct* fpregs)
{
return read_ssa(pid, tcs_addr, FORWARD, 0, sizeof(struct user_fpregs_struct), (void *)fpregs);
}
static inline int set_ssa_fpregs(pid_t pid, long tcs_addr, struct user_fpregs_struct* fpregs)
{
return write_ssa(pid, tcs_addr, FORWARD, 0, sizeof(struct user_fpregs_struct), (void *)fpregs);
}
#if !defined(__x86_64__) && !defined(__x86_64)
static inline int get_ssa_fpxregs(pid_t pid, long tcs_addr, struct user_fpxregs_struct* fpxregs)
{
return read_ssa(pid, tcs_addr, FORWARD, 0, sizeof(struct user_fpxregs_struct), (void *)fpxregs);
}
static inline int set_ssa_fpxregs(pid_t pid, long tcs_addr, struct user_fpxregs_struct* fpxregs)
{
return write_ssa(pid, tcs_addr, FORWARD, 0, sizeof(struct user_fpxregs_struct), (void *)fpxregs);
}
#else
#define get_ssa_fpxregs get_ssa_fpregs
#define set_ssa_fpxregs set_ssa_fpregs
#define user_fpxregs_struct user_fpregs_struct
#endif
static inline int get_ssa_xstate(pid_t pid, long tcs_addr, int len, char *buf)
{
return read_ssa(pid, tcs_addr, FORWARD, 0, len, buf);
}
static inline int set_ssa_xstate(pid_t pid, long tcs_addr, int len, char *buf)
{
return write_ssa(pid, tcs_addr, FORWARD, 0, len, buf);
}
static int get_enclave_gregs(pid_t pid, struct user_regs_struct *regs, long tcs_addr)
{
ssa_gpr_t gpr;
if(!get_ssa_gpr(pid, tcs_addr, &gpr))
return -1;
//convert gpr to user_regs_struct.
SSA2USER_REG(regs, gpr, bx);
SSA2USER_REG(regs, gpr, cx);
SSA2USER_REG(regs, gpr, dx);
SSA2USER_REG(regs, gpr, si);
SSA2USER_REG(regs, gpr, di);
SSA2USER_REG(regs, gpr, bp);
SSA2USER_REG(regs, gpr, ax);
SSA2USER_REG(regs, gpr, ip);
regs->eflags = gpr.REG(flags);
SSA2USER_REG(regs, gpr, sp);
#ifdef __x86_64__
SSA2USER_REG(regs, gpr, 8);
SSA2USER_REG(regs, gpr, 9);
SSA2USER_REG(regs, gpr, 10);
SSA2USER_REG(regs, gpr, 11);
SSA2USER_REG(regs, gpr, 12);
SSA2USER_REG(regs, gpr, 13);
SSA2USER_REG(regs, gpr, 14);
SSA2USER_REG(regs, gpr, 15);
#endif
return 0;
}
static int set_enclave_gregs(pid_t pid, struct user_regs_struct *regs, long tcs_addr)
{
ssa_gpr_t gpr;
//Since there is some field won't be written, we need save it first
if(!get_ssa_gpr(pid, tcs_addr, &gpr))
return -1;
//convert gpr to user_regs_struct.
USER_REG2SSA(gpr, regs, bx);
USER_REG2SSA(gpr, regs, cx);
USER_REG2SSA(gpr, regs, dx);
USER_REG2SSA(gpr, regs, si);
USER_REG2SSA(gpr, regs, di);
USER_REG2SSA(gpr, regs, bp);
USER_REG2SSA(gpr, regs, ax);
USER_REG2SSA(gpr, regs, ip);
gpr.REG(flags) = regs->eflags;
USER_REG2SSA(gpr, regs, sp);
#ifdef __x86_64__
USER_REG2SSA(gpr, regs, 8);
USER_REG2SSA(gpr, regs, 9);
USER_REG2SSA(gpr, regs, 10);
USER_REG2SSA(gpr, regs, 11);
USER_REG2SSA(gpr, regs, 12);
USER_REG2SSA(gpr, regs, 13);
USER_REG2SSA(gpr, regs, 14);
USER_REG2SSA(gpr, regs, 15);
#endif
//write general registers to ssa
if(!set_ssa_gpr(pid, tcs_addr, &gpr))
return -1;
return 0;
}
static int is_eresume(pid_t pid, struct user_regs_struct *regs)
{
unsigned int instr;
if(!se_read_process_mem(pid, (void *)regs->REG(ip), (char *)&instr, sizeof(instr), NULL))
return FALSE;
if((ENCLU == (instr & 0xffffff))
&& (SE_ERESUME == regs->REG(ax)))
return TRUE;
return FALSE;
}
static long int get_regs(pid_t pid, void* addr, void* data)
{
int ret = 0;
if(!data)
return -1;
struct user_regs_struct *regs = (struct user_regs_struct *)data;
if(-1 == (ret = g_sys_ptrace(PTRACE_GETREGS, pid, addr, data)))
return -1;
if(is_eresume(pid, regs))
{
//If it is ERESUME instruction, set the real register value
get_enclave_gregs(pid, regs, regs->REG(bx));
}
return ret;
}
typedef struct _thread_status_t {
pid_t pid;
int inside_out;
int singlestep;
struct user_regs_struct aep_regs;
struct _thread_status_t *next;
} thread_status_t;
static thread_status_t * g_thread_status = NULL;
/*
*get the thread info by pid
*return the status point if the thread info already cached
*otherwise return NULL
*
*/
static thread_status_t * get_thread_status(pid_t pid)
{
thread_status_t * thread_status = g_thread_status;
while(thread_status)
{
if(thread_status->pid == pid)
break;
else
thread_status = thread_status->next;
}
return thread_status;
}
/*
*add thread status cache
*return the cache point
*/
static thread_status_t * add_thread_status(pid_t pid)
{
thread_status_t * thread_status = (thread_status_t *)malloc(sizeof(thread_status_t));
if (thread_status == NULL)
return NULL;
memset(thread_status, 0, sizeof(thread_status_t));
thread_status->pid = pid;
thread_status->next = g_thread_status;
g_thread_status = thread_status;
return thread_status;
}
/*
*remove the thread status cache by pid
*
*/
static void remove_thread_status(pid_t pid)
{
thread_status_t * thread_status = g_thread_status;
thread_status_t * previous_link = NULL;
while(thread_status)
{
if(thread_status->pid == pid)
break;
else
{
previous_link = thread_status;
thread_status = thread_status->next;
}
}
if (thread_status != NULL)
{
if (previous_link == NULL)
{
g_thread_status = thread_status->next;
} else {
previous_link->next = thread_status->next;
}
free(thread_status);
}
}
static long int set_regs(pid_t pid, void* addr, void* data)
{
int ret = 0;
struct user_regs_struct aep_regs;
if(!data)
return -1;
if(-1 == g_sys_ptrace(PTRACE_GETREGS, pid, 0, (void*)&aep_regs))
return -1;
if(is_eresume(pid, &aep_regs))
{
struct user_regs_struct *regs = (struct user_regs_struct *)data;
//get tcs address
if(-1 == (ret = set_enclave_gregs(pid, regs, aep_regs.REG(bx))))
return -1;
else
return ret;
}
else
{
return g_sys_ptrace(PTRACE_SETREGS, pid, addr, data);
}
}
static long int get_fpregs(pid_t pid, void* addr, void* data, int extend)
{
int ret = 0;
if(!data)
return -1;
struct user_regs_struct regs;
if(-1 == (ret = g_sys_ptrace(PTRACE_GETREGS, pid, 0, ®s)))
return -1;
if(is_eresume(pid, ®s))
{
if(extend)
ret = get_ssa_fpxregs(pid, regs.REG(bx), (struct user_fpxregs_struct *)data);
else
ret = get_ssa_fpregs(pid, regs.REG(bx), (struct user_fpregs_struct *)data);
if(ret)
return 0;
else
return -1;
}
else
{
return g_sys_ptrace(PTRACE_GETFPREGS, pid, addr, data);
}
}
static long int set_fpregs(pid_t pid, void* addr, void* data, int extend)
{
int ret = 0;
if(!data)
return -1;
struct user_regs_struct regs;
if(-1 == (ret = g_sys_ptrace(PTRACE_GETREGS, pid, 0, ®s)))
return -1;
if(is_eresume(pid, ®s))
{
if(extend)
ret = set_ssa_fpxregs(pid, regs.REG(bx), (struct user_fpxregs_struct *)data);
else
ret = set_ssa_fpregs(pid, regs.REG(bx), (struct user_fpregs_struct *)data);
if(ret)
return 0;
else
return -1;
}
else
{
return g_sys_ptrace(PTRACE_GETFPREGS, pid, addr, data);
}
}
static long int get_regset(pid_t pid, void* addr, void* data)
{
int ret = 0;
unsigned long type = (unsigned long)addr;
if(!data)
return -1;
struct user_regs_struct regs;
if(-1 == (ret = g_sys_ptrace(PTRACE_GETREGS, pid, 0, ®s)))
return -1;
if(is_eresume(pid, ®s))
{
if(NT_X86_XSTATE != type)
{
SE_TRACE(SE_TRACE_WARNING, "unexpected type for PTRACE_GETREGSET\n");
return -1;
}
struct iovec *iov = (struct iovec *)data;
if(iov->iov_base && iov->iov_len
&& get_ssa_xstate(pid, regs.REG(bx), iov->iov_len, (char *)iov->iov_base))
{
return 0;
}
else
return -1;
}
else
{
return g_sys_ptrace(PTRACE_GETREGSET, pid, addr, data);
}
}
static long int set_regset(pid_t pid, void* addr, void* data)
{
int ret = 0;
unsigned long type = (unsigned long)addr;
if(!data)
return -1;
struct user_regs_struct regs;
if(-1 == (ret = g_sys_ptrace(PTRACE_GETREGS, pid, 0, ®s)))
return -1;
if(is_eresume(pid, ®s))
{
if(NT_X86_XSTATE != type)
{
SE_TRACE(SE_TRACE_WARNING, "unexpected type for PTRACE_SETREGSET\n");
return -1;
}
struct iovec *iov = (struct iovec *)data;
if(iov->iov_base && iov->iov_len
&& set_ssa_xstate(pid, regs.REG(bx), iov->iov_len, (char *)iov->iov_base))
{
return 0;
}
else
return -1;
}
else
{
return g_sys_ptrace(PTRACE_SETREGSET, pid, addr, data);
}
}
static long int do_singlestep(pid_t pid, void* addr, void* data)
{
thread_status_t * thread_status = NULL;
if ((thread_status = get_thread_status(pid)) == NULL)
thread_status = add_thread_status(pid);
if (thread_status != NULL)
thread_status->singlestep = 1;
return g_sys_ptrace(PTRACE_SINGLESTEP, pid, addr, data);
}
long int ptrace (enum __ptrace_request __request, ...)
{
pid_t pid;
void *addr, *data;
va_list ap;
va_start(ap, __request);
pid = va_arg(ap, pid_t);
addr = va_arg(ap, void *);
data = va_arg(ap, void *);
va_end(ap);
if(__request == PTRACE_GETREGS)
{
return get_regs(pid, addr, data);
}
else if(__request == PTRACE_SETREGS)
{
return set_regs(pid, addr, data);
}
#if 0
//some old system may require this command to get register
else if(__request == PTRACE_PEEKUSER)
{
}
#endif
else if(__request == PTRACE_GETFPREGS)
{
return get_fpregs(pid, addr, data, FALSE);
}
else if(__request == PTRACE_SETFPREGS)
{
return set_fpregs(pid, addr, data, FALSE);
}
else if(__request == PTRACE_GETFPXREGS)
{
return get_fpregs(pid, addr, data, TRUE);
}
else if(__request == PTRACE_SETFPXREGS)
{
return set_fpregs(pid, addr, data, TRUE);
}
//xstave for avx
else if(__request == PTRACE_GETREGSET)
{
return get_regset(pid, addr, data);
}
else if(__request == PTRACE_SETREGSET)
{
return set_regset(pid, addr, data);
}
else if(__request == PTRACE_SINGLESTEP)
{
return do_singlestep(pid, addr, data);
}
//For other request just forward it to real ptrace call;
return g_sys_ptrace(__request, pid, addr, data);
}
pid_t waitpid(pid_t pid, int *status, int options)
{
pid_t ret_pid = g_sys_waitpid(pid, status, options);
if (ret_pid == -1 || status == NULL)
return ret_pid;
if (WIFEXITED(*status) || WIFSIGNALED(*status))
{
remove_thread_status(ret_pid);
}
//if it is a TRAP, and inside enclave, fix the #BP info
if(WIFSTOPPED(*status) &&
WSTOPSIG(*status) == SIGTRAP)
{
struct user_regs_struct regs;
thread_status_t * thread_status = get_thread_status(ret_pid);
if(thread_status && thread_status->singlestep == 1)
{
thread_status->singlestep = 0;
}
else if(-1 == g_sys_ptrace(PTRACE_GETREGS, ret_pid, 0, ®s))
{
SE_TRACE(SE_TRACE_WARNING, "unexpected get context failed\n");
}
else if(is_eresume(ret_pid, ®s))
{
long tcs = regs.REG(bx);
//If it is ERESUME instruction, set the real register value
if(-1 != get_enclave_gregs(ret_pid, ®s, tcs))
{
uint8_t bp = 0;
if(!se_read_process_mem(ret_pid, (void *)regs.REG(ip), (void *)&bp, 1, NULL))
{
SE_TRACE(SE_TRACE_WARNING, "unexpected read memory failed\n");
}
else if (bp == 0xcc)
{
regs.REG(ip)++;
if ( -1 == set_enclave_gregs(ret_pid, ®s, tcs))
{
SE_TRACE(SE_TRACE_WARNING, "unexpected set registers failed\n");
}
}
}
}
}
return ret_pid;
}