se_ptrace.c 21 KB

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