sgx_enclave.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #include "ocall_types.h"
  2. #include "ecall_types.h"
  3. #include "sgx_internal.h"
  4. #include "sgx_enclave.h"
  5. #include "pal_security.h"
  6. #include "pal_linux_error.h"
  7. #include <asm/mman.h>
  8. #include <asm/ioctls.h>
  9. #include <asm/socket.h>
  10. #include <linux/fs.h>
  11. #include <linux/in.h>
  12. #include <linux/in6.h>
  13. #include <math.h>
  14. #include <asm/errno.h>
  15. #ifndef SOL_IPV6
  16. # define SOL_IPV6 41
  17. #endif
  18. #define ODEBUG(code, ms) do {} while (0)
  19. static int sgx_ocall_exit(void* pms)
  20. {
  21. ms_ocall_exit_t * ms = (ms_ocall_exit_t *) pms;
  22. ODEBUG(OCALL_EXIT, NULL);
  23. if (ms->ms_exitcode != (int) ((uint8_t) ms->ms_exitcode)) {
  24. SGX_DBG(DBG_E, "Saturation error in exit code %d, getting rounded down to %u\n",
  25. ms->ms_exitcode, (uint8_t) ms->ms_exitcode);
  26. ms->ms_exitcode = 255;
  27. }
  28. /* exit the whole process if exit_group() */
  29. if (ms->ms_is_exitgroup)
  30. INLINE_SYSCALL(exit_group, 1, (int)ms->ms_exitcode);
  31. /* otherwise call SGX-related thread reset and exit this thread */
  32. block_async_signals(true);
  33. ecall_thread_reset();
  34. unmap_tcs();
  35. thread_exit((int)ms->ms_exitcode);
  36. return 0;
  37. }
  38. static int sgx_ocall_mmap_untrusted(void * pms)
  39. {
  40. ms_ocall_mmap_untrusted_t * ms = (ms_ocall_mmap_untrusted_t *) pms;
  41. void * addr;
  42. ODEBUG(OCALL_MMAP_UNTRUSTED, ms);
  43. addr = (void *) INLINE_SYSCALL(mmap, 6, NULL, ms->ms_size,
  44. ms->ms_prot,
  45. (ms->ms_fd == -1) ? MAP_ANONYMOUS | MAP_PRIVATE
  46. : MAP_FILE | MAP_SHARED,
  47. ms->ms_fd, ms->ms_offset);
  48. if (IS_ERR_P(addr))
  49. return -ERRNO_P(addr);
  50. ms->ms_mem = addr;
  51. return 0;
  52. }
  53. static int sgx_ocall_munmap_untrusted(void * pms)
  54. {
  55. ms_ocall_munmap_untrusted_t * ms = (ms_ocall_munmap_untrusted_t *) pms;
  56. ODEBUG(OCALL_MUNMAP_UNTRUSTED, ms);
  57. INLINE_SYSCALL(munmap, 2, ALLOC_ALIGN_DOWN_PTR(ms->ms_mem),
  58. ALLOC_ALIGN_UP_PTR(ms->ms_mem + ms->ms_size) -
  59. ALLOC_ALIGN_DOWN_PTR(ms->ms_mem));
  60. return 0;
  61. }
  62. static int sgx_ocall_cpuid(void * pms)
  63. {
  64. ms_ocall_cpuid_t * ms = (ms_ocall_cpuid_t *) pms;
  65. ODEBUG(OCALL_CPUID, ms);
  66. __asm__ volatile ("cpuid"
  67. : "=a"(ms->ms_values[0]),
  68. "=b"(ms->ms_values[1]),
  69. "=c"(ms->ms_values[2]),
  70. "=d"(ms->ms_values[3])
  71. : "a"(ms->ms_leaf), "c"(ms->ms_subleaf) : "memory");
  72. return 0;
  73. }
  74. static int sgx_ocall_open(void * pms)
  75. {
  76. ms_ocall_open_t * ms = (ms_ocall_open_t *) pms;
  77. int ret;
  78. ODEBUG(OCALL_OPEN, ms);
  79. ret = INLINE_SYSCALL(open, 3, ms->ms_pathname, ms->ms_flags|O_CLOEXEC,
  80. ms->ms_mode);
  81. return ret;
  82. }
  83. static int sgx_ocall_close(void * pms)
  84. {
  85. ms_ocall_close_t * ms = (ms_ocall_close_t *) pms;
  86. ODEBUG(OCALL_CLOSE, ms);
  87. INLINE_SYSCALL(close, 1, ms->ms_fd);
  88. return 0;
  89. }
  90. static int sgx_ocall_read(void * pms)
  91. {
  92. ms_ocall_read_t * ms = (ms_ocall_read_t *) pms;
  93. int ret;
  94. ODEBUG(OCALL_READ, ms);
  95. ret = INLINE_SYSCALL(read, 3, ms->ms_fd, ms->ms_buf, ms->ms_count);
  96. return ret;
  97. }
  98. static int sgx_ocall_write(void * pms)
  99. {
  100. ms_ocall_write_t * ms = (ms_ocall_write_t *) pms;
  101. int ret;
  102. ODEBUG(OCALL_WRITE, ms);
  103. ret = INLINE_SYSCALL(write, 3, ms->ms_fd, ms->ms_buf, ms->ms_count);
  104. return ret;
  105. }
  106. static int sgx_ocall_fstat(void * pms)
  107. {
  108. ms_ocall_fstat_t * ms = (ms_ocall_fstat_t *) pms;
  109. int ret;
  110. ODEBUG(OCALL_FSTAT, ms);
  111. ret = INLINE_SYSCALL(fstat, 2, ms->ms_fd, &ms->ms_stat);
  112. return ret;
  113. }
  114. static int sgx_ocall_fionread(void * pms)
  115. {
  116. ms_ocall_fionread_t * ms = (ms_ocall_fionread_t *) pms;
  117. int ret, val;
  118. ODEBUG(OCALL_FIONREAD, ms);
  119. ret = INLINE_SYSCALL(ioctl, 3, ms->ms_fd, FIONREAD, &val);
  120. return IS_ERR(ret) ? ret : val;
  121. }
  122. static int sgx_ocall_fsetnonblock(void * pms)
  123. {
  124. ms_ocall_fsetnonblock_t * ms = (ms_ocall_fsetnonblock_t *) pms;
  125. int ret, flags;
  126. ODEBUG(OCALL_FSETNONBLOCK, ms);
  127. ret = INLINE_SYSCALL(fcntl, 2, ms->ms_fd, F_GETFL);
  128. if (IS_ERR(ret))
  129. return ret;
  130. flags = ret;
  131. if (ms->ms_nonblocking) {
  132. if (!(flags & O_NONBLOCK))
  133. ret = INLINE_SYSCALL(fcntl, 3, ms->ms_fd, F_SETFL,
  134. flags | O_NONBLOCK);
  135. } else {
  136. if (flags & O_NONBLOCK)
  137. ret = INLINE_SYSCALL(fcntl, 3, ms->ms_fd, F_SETFL,
  138. flags & ~O_NONBLOCK);
  139. }
  140. return ret;
  141. }
  142. static int sgx_ocall_fchmod(void * pms)
  143. {
  144. ms_ocall_fchmod_t * ms = (ms_ocall_fchmod_t *) pms;
  145. int ret;
  146. ODEBUG(OCALL_FCHMOD, ms);
  147. ret = INLINE_SYSCALL(fchmod, 2, ms->ms_fd, ms->ms_mode);
  148. return ret;
  149. }
  150. static int sgx_ocall_fsync(void * pms)
  151. {
  152. ms_ocall_fsync_t * ms = (ms_ocall_fsync_t *) pms;
  153. ODEBUG(OCALL_FSYNC, ms);
  154. INLINE_SYSCALL(fsync, 1, ms->ms_fd);
  155. return 0;
  156. }
  157. static int sgx_ocall_ftruncate(void * pms)
  158. {
  159. ms_ocall_ftruncate_t * ms = (ms_ocall_ftruncate_t *) pms;
  160. int ret;
  161. ODEBUG(OCALL_FTRUNCATE, ms);
  162. ret = INLINE_SYSCALL(ftruncate, 2, ms->ms_fd, ms->ms_length);
  163. return ret;
  164. }
  165. static int sgx_ocall_lseek(void* pms) {
  166. ms_ocall_lseek_t* ms = (ms_ocall_lseek_t*)pms;
  167. int ret;
  168. ODEBUG(OCALL_LSEEK, ms);
  169. ret = INLINE_SYSCALL(lseek, 3, ms->ms_fd, ms->ms_offset, ms->ms_whence);
  170. return ret;
  171. }
  172. static int sgx_ocall_mkdir(void * pms)
  173. {
  174. ms_ocall_mkdir_t * ms = (ms_ocall_mkdir_t *) pms;
  175. int ret;
  176. ODEBUG(OCALL_MKDIR, ms);
  177. ret = INLINE_SYSCALL(mkdir, 2, ms->ms_pathname, ms->ms_mode);
  178. return ret;
  179. }
  180. static int sgx_ocall_getdents(void * pms)
  181. {
  182. ms_ocall_getdents_t * ms = (ms_ocall_getdents_t *) pms;
  183. int ret;
  184. ODEBUG(OCALL_GETDENTS, ms);
  185. ret = INLINE_SYSCALL(getdents64, 3, ms->ms_fd, ms->ms_dirp, ms->ms_size);
  186. return ret;
  187. }
  188. static int sgx_ocall_resume_thread(void * pms)
  189. {
  190. ODEBUG(OCALL_RESUME_THREAD, pms);
  191. return interrupt_thread(pms);
  192. }
  193. static int sgx_ocall_clone_thread(void * pms)
  194. {
  195. __UNUSED(pms);
  196. ODEBUG(OCALL_CLONE_THREAD, pms);
  197. return clone_thread();
  198. }
  199. static int sgx_ocall_create_process(void * pms)
  200. {
  201. ms_ocall_create_process_t * ms = (ms_ocall_create_process_t *) pms;
  202. ODEBUG(OCALL_CREATE_PROCESS, ms);
  203. int ret = sgx_create_process(ms->ms_uri, ms->ms_nargs, ms->ms_args, ms->ms_proc_fds);
  204. if (ret < 0)
  205. return ret;
  206. ms->ms_pid = ret;
  207. return 0;
  208. }
  209. static int sgx_ocall_futex(void * pms)
  210. {
  211. ms_ocall_futex_t * ms = (ms_ocall_futex_t *) pms;
  212. int ret;
  213. ODEBUG(OCALL_FUTEX, ms);
  214. struct timespec* ts = NULL;
  215. if (ms->ms_timeout_us >= 0) {
  216. ts = __alloca(sizeof(struct timespec));
  217. ts->tv_sec = ms->ms_timeout_us / 1000000;
  218. ts->tv_nsec = (ms->ms_timeout_us - ts->tv_sec * 1000000) * 1000;
  219. }
  220. ret = INLINE_SYSCALL(futex, 6, ms->ms_futex, ms->ms_op, ms->ms_val,
  221. ts, NULL, 0);
  222. return ret;
  223. }
  224. static int sgx_ocall_socketpair(void * pms)
  225. {
  226. ms_ocall_socketpair_t * ms = (ms_ocall_socketpair_t *) pms;
  227. int ret;
  228. ODEBUG(OCALL_SOCKETPAIR, ms);
  229. ret = INLINE_SYSCALL(socketpair, 4, ms->ms_domain,
  230. ms->ms_type|SOCK_CLOEXEC,
  231. ms->ms_protocol, &ms->ms_sockfds);
  232. return ret;
  233. }
  234. static int sock_getopt(int fd, struct sockopt * opt)
  235. {
  236. SGX_DBG(DBG_M, "sock_getopt (fd = %d, sockopt addr = %p) is not implemented \
  237. always returns 0\n", fd, opt);
  238. /* initialize *opt with constant */
  239. *opt = (struct sockopt){0};
  240. opt->reuseaddr = 1;
  241. return 0;
  242. }
  243. static int sgx_ocall_listen(void * pms)
  244. {
  245. ms_ocall_listen_t * ms = (ms_ocall_listen_t *) pms;
  246. int ret, fd;
  247. ODEBUG(OCALL_LISTEN, ms);
  248. ret = INLINE_SYSCALL(socket, 3, ms->ms_domain,
  249. ms->ms_type|SOCK_CLOEXEC,
  250. ms->ms_protocol);
  251. if (IS_ERR(ret))
  252. goto err;
  253. fd = ret;
  254. if (ms->ms_addr->sa_family == AF_INET6) {
  255. int ipv6only = 1;
  256. INLINE_SYSCALL(setsockopt, 5, fd, SOL_IPV6, IPV6_V6ONLY, &ipv6only,
  257. sizeof(int));
  258. }
  259. /* must set the socket to be reuseable */
  260. int reuseaddr = 1;
  261. INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
  262. sizeof(int));
  263. ret = INLINE_SYSCALL(bind, 3, fd, ms->ms_addr, ms->ms_addrlen);
  264. if (IS_ERR(ret))
  265. goto err_fd;
  266. if (ms->ms_addr) {
  267. socklen_t addrlen = ms->ms_addrlen;
  268. ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_addr, &addrlen);
  269. if (IS_ERR(ret))
  270. goto err_fd;
  271. ms->ms_addrlen = addrlen;
  272. }
  273. if (ms->ms_type & SOCK_STREAM) {
  274. ret = INLINE_SYSCALL(listen, 2, fd, DEFAULT_BACKLOG);
  275. if (IS_ERR(ret))
  276. goto err_fd;
  277. }
  278. ret = sock_getopt(fd, &ms->ms_sockopt);
  279. if (IS_ERR(ret))
  280. goto err_fd;
  281. return fd;
  282. err_fd:
  283. INLINE_SYSCALL(close, 1, fd);
  284. err:
  285. return ret;
  286. }
  287. static int sgx_ocall_accept(void * pms)
  288. {
  289. ms_ocall_accept_t * ms = (ms_ocall_accept_t *) pms;
  290. int ret, fd;
  291. ODEBUG(OCALL_ACCEPT, ms);
  292. socklen_t addrlen = ms->ms_addrlen;
  293. ret = INLINE_SYSCALL(accept4, 4, ms->ms_sockfd, ms->ms_addr,
  294. &addrlen, O_CLOEXEC);
  295. if (IS_ERR(ret))
  296. goto err;
  297. fd = ret;
  298. ret = sock_getopt(fd, &ms->ms_sockopt);
  299. if (IS_ERR(ret))
  300. goto err_fd;
  301. ms->ms_addrlen = addrlen;
  302. return fd;
  303. err_fd:
  304. INLINE_SYSCALL(close, 1, fd);
  305. err:
  306. return ret;
  307. }
  308. static int sgx_ocall_connect(void * pms)
  309. {
  310. ms_ocall_connect_t * ms = (ms_ocall_connect_t *) pms;
  311. int ret, fd;
  312. ODEBUG(OCALL_CONNECT, ms);
  313. ret = INLINE_SYSCALL(socket, 3, ms->ms_domain,
  314. ms->ms_type|SOCK_CLOEXEC,
  315. ms->ms_protocol);
  316. if (IS_ERR(ret))
  317. goto err;
  318. fd = ret;
  319. if (ms->ms_addr && ms->ms_addr->sa_family == AF_INET6) {
  320. int ipv6only = 1;
  321. INLINE_SYSCALL(setsockopt, 5, fd, SOL_IPV6, IPV6_V6ONLY, &ipv6only,
  322. sizeof(int));
  323. }
  324. if (ms->ms_bind_addr && ms->ms_bind_addr->sa_family) {
  325. ret = INLINE_SYSCALL(bind, 3, fd, ms->ms_bind_addr,
  326. ms->ms_bind_addrlen);
  327. if (IS_ERR(ret))
  328. goto err_fd;
  329. }
  330. if (ms->ms_addr) {
  331. ret = INLINE_SYSCALL(connect, 3, fd, ms->ms_addr, ms->ms_addrlen);
  332. if (IS_ERR(ret) && ERRNO(ret) == EINPROGRESS) {
  333. do {
  334. struct pollfd pfd = { .fd = fd, .events = POLLOUT, .revents = 0, };
  335. ret = INLINE_SYSCALL(ppoll, 4, &pfd, 1, NULL, NULL);
  336. } while (IS_ERR(ret) &&
  337. ERRNO(ret) == -EWOULDBLOCK);
  338. }
  339. if (IS_ERR(ret))
  340. goto err_fd;
  341. }
  342. if (ms->ms_bind_addr && !ms->ms_bind_addr->sa_family) {
  343. socklen_t addrlen = ms->ms_bind_addrlen;
  344. ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_bind_addr,
  345. &addrlen);
  346. if (IS_ERR(ret))
  347. goto err_fd;
  348. ms->ms_bind_addrlen = addrlen;
  349. }
  350. ret = sock_getopt(fd, &ms->ms_sockopt);
  351. if (IS_ERR(ret))
  352. goto err_fd;
  353. return fd;
  354. err_fd:
  355. INLINE_SYSCALL(close, 1, fd);
  356. err:
  357. return ret;
  358. }
  359. static int sgx_ocall_recv(void * pms)
  360. {
  361. ms_ocall_recv_t * ms = (ms_ocall_recv_t *) pms;
  362. int ret;
  363. ODEBUG(OCALL_RECV, ms);
  364. struct sockaddr * addr = ms->ms_addr;
  365. socklen_t addrlen = ms->ms_addr ? ms->ms_addrlen : 0;
  366. if (ms->ms_sockfd == pal_enclave.pal_sec.mcast_srv) {
  367. addr = NULL;
  368. addrlen = 0;
  369. }
  370. struct msghdr hdr;
  371. struct iovec iov[1];
  372. iov[0].iov_base = ms->ms_buf;
  373. iov[0].iov_len = ms->ms_count;
  374. hdr.msg_name = addr;
  375. hdr.msg_namelen = addrlen;
  376. hdr.msg_iov = iov;
  377. hdr.msg_iovlen = 1;
  378. hdr.msg_control = ms->ms_control;
  379. hdr.msg_controllen = ms->ms_controllen;
  380. hdr.msg_flags = 0;
  381. ret = INLINE_SYSCALL(recvmsg, 3, ms->ms_sockfd, &hdr, 0);
  382. if (!IS_ERR(ret) && hdr.msg_name) {
  383. /* note that ms->ms_addr is filled by recvmsg() itself */
  384. ms->ms_addrlen = hdr.msg_namelen;
  385. }
  386. if (!IS_ERR(ret) && hdr.msg_control) {
  387. /* note that ms->ms_control is filled by recvmsg() itself */
  388. ms->ms_controllen = hdr.msg_controllen;
  389. }
  390. return ret;
  391. }
  392. static int sgx_ocall_send(void * pms)
  393. {
  394. ms_ocall_send_t * ms = (ms_ocall_send_t *) pms;
  395. int ret;
  396. ODEBUG(OCALL_SEND, ms);
  397. const struct sockaddr * addr = ms->ms_addr;
  398. socklen_t addrlen = ms->ms_addr ? ms->ms_addrlen : 0;
  399. struct sockaddr_in mcast_addr;
  400. if (ms->ms_sockfd == pal_enclave.pal_sec.mcast_srv) {
  401. mcast_addr.sin_family = AF_INET;
  402. inet_pton4(MCAST_GROUP, sizeof(MCAST_GROUP), &mcast_addr.sin_addr.s_addr);
  403. mcast_addr.sin_port = htons(pal_enclave.pal_sec.mcast_port);
  404. addr = (struct sockaddr *) &mcast_addr;
  405. addrlen = sizeof(struct sockaddr_in);
  406. }
  407. struct msghdr hdr;
  408. struct iovec iov[1];
  409. iov[0].iov_base = (void*)ms->ms_buf;
  410. iov[0].iov_len = ms->ms_count;
  411. hdr.msg_name = (void*)addr;
  412. hdr.msg_namelen = addrlen;
  413. hdr.msg_iov = iov;
  414. hdr.msg_iovlen = 1;
  415. hdr.msg_control = ms->ms_control;
  416. hdr.msg_controllen = ms->ms_controllen;
  417. hdr.msg_flags = 0;
  418. ret = INLINE_SYSCALL(sendmsg, 3, ms->ms_sockfd, &hdr, MSG_NOSIGNAL);
  419. return ret;
  420. }
  421. static int sgx_ocall_setsockopt(void * pms)
  422. {
  423. ms_ocall_setsockopt_t * ms = (ms_ocall_setsockopt_t *) pms;
  424. int ret;
  425. ODEBUG(OCALL_SETSOCKOPT, ms);
  426. ret = INLINE_SYSCALL(setsockopt, 5,
  427. ms->ms_sockfd, ms->ms_level, ms->ms_optname,
  428. ms->ms_optval, ms->ms_optlen);
  429. return ret;
  430. }
  431. static int sgx_ocall_shutdown(void * pms)
  432. {
  433. ms_ocall_shutdown_t * ms = (ms_ocall_shutdown_t *) pms;
  434. ODEBUG(OCALL_SHUTDOWN, ms);
  435. INLINE_SYSCALL(shutdown, 2, ms->ms_sockfd, ms->ms_how);
  436. return 0;
  437. }
  438. static int sgx_ocall_gettime(void * pms)
  439. {
  440. ms_ocall_gettime_t * ms = (ms_ocall_gettime_t *) pms;
  441. ODEBUG(OCALL_GETTIME, ms);
  442. struct timeval tv;
  443. INLINE_SYSCALL(gettimeofday, 2, &tv, NULL);
  444. ms->ms_microsec = tv.tv_sec * 1000000UL + tv.tv_usec;
  445. return 0;
  446. }
  447. static int sgx_ocall_sleep(void * pms)
  448. {
  449. ms_ocall_sleep_t * ms = (ms_ocall_sleep_t *) pms;
  450. int ret;
  451. ODEBUG(OCALL_SLEEP, ms);
  452. if (!ms->ms_microsec) {
  453. INLINE_SYSCALL(sched_yield, 0);
  454. return 0;
  455. }
  456. struct timespec req, rem;
  457. unsigned long microsec = ms->ms_microsec;
  458. const unsigned long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  459. if (ms->ms_microsec > VERY_LONG_TIME_IN_US) {
  460. /* avoid overflow with time_t */
  461. req.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  462. req.tv_nsec = 0;
  463. } else {
  464. req.tv_sec = ms->ms_microsec / 1000000;
  465. req.tv_nsec = (microsec - req.tv_sec * 1000000) * 1000;
  466. }
  467. ret = INLINE_SYSCALL(nanosleep, 2, &req, &rem);
  468. if (IS_ERR(ret) && ERRNO(ret) == EINTR)
  469. ms->ms_microsec = rem.tv_sec * 1000000UL + rem.tv_nsec / 1000UL;
  470. return ret;
  471. }
  472. static int sgx_ocall_poll(void * pms)
  473. {
  474. ms_ocall_poll_t * ms = (ms_ocall_poll_t *) pms;
  475. int ret;
  476. ODEBUG(OCALL_POLL, ms);
  477. struct timespec * ts = NULL;
  478. if (ms->ms_timeout_us >= 0) {
  479. ts = __alloca(sizeof(struct timespec));
  480. ts->tv_sec = ms->ms_timeout_us / 1000000;
  481. ts->tv_nsec = (ms->ms_timeout_us - ts->tv_sec * 1000000) * 1000;
  482. }
  483. ret = INLINE_SYSCALL(ppoll, 4, ms->ms_fds, ms->ms_nfds, ts, NULL);
  484. return ret;
  485. }
  486. static int sgx_ocall_rename(void * pms)
  487. {
  488. ms_ocall_rename_t * ms = (ms_ocall_rename_t *) pms;
  489. int ret;
  490. ODEBUG(OCALL_RENAME, ms);
  491. ret = INLINE_SYSCALL(rename, 2, ms->ms_oldpath, ms->ms_newpath);
  492. return ret;
  493. }
  494. static int sgx_ocall_delete(void * pms)
  495. {
  496. ms_ocall_delete_t * ms = (ms_ocall_delete_t *) pms;
  497. int ret;
  498. ODEBUG(OCALL_DELETE, ms);
  499. ret = INLINE_SYSCALL(unlink, 1, ms->ms_pathname);
  500. if (IS_ERR(ret) && ERRNO(ret) == EISDIR)
  501. ret = INLINE_SYSCALL(rmdir, 1, ms->ms_pathname);
  502. return ret;
  503. }
  504. static int sgx_ocall_eventfd (void * pms)
  505. {
  506. ms_ocall_eventfd_t * ms = (ms_ocall_eventfd_t *) pms;
  507. int ret;
  508. ODEBUG(OCALL_EVENTFD, ms);
  509. ret = INLINE_SYSCALL(eventfd2, 2, ms->ms_initval, ms->ms_flags);
  510. return ret;
  511. }
  512. void load_gdb_command (const char * command);
  513. static int sgx_ocall_load_debug(void * pms)
  514. {
  515. const char * command = (const char *) pms;
  516. ODEBUG(OCALL_LOAD_DEBUG, (void *) command);
  517. load_gdb_command(command);
  518. return 0;
  519. }
  520. static int sgx_ocall_get_attestation(void* pms) {
  521. ms_ocall_get_attestation_t * ms = (ms_ocall_get_attestation_t *) pms;
  522. ODEBUG(OCALL_GET_ATTESTATION, ms);
  523. return retrieve_verified_quote(&ms->ms_spid, ms->ms_subkey, ms->ms_linkable, &ms->ms_report,
  524. &ms->ms_nonce, &ms->ms_attestation);
  525. }
  526. sgx_ocall_fn_t ocall_table[OCALL_NR] = {
  527. [OCALL_EXIT] = sgx_ocall_exit,
  528. [OCALL_MMAP_UNTRUSTED] = sgx_ocall_mmap_untrusted,
  529. [OCALL_MUNMAP_UNTRUSTED] = sgx_ocall_munmap_untrusted,
  530. [OCALL_CPUID] = sgx_ocall_cpuid,
  531. [OCALL_OPEN] = sgx_ocall_open,
  532. [OCALL_CLOSE] = sgx_ocall_close,
  533. [OCALL_READ] = sgx_ocall_read,
  534. [OCALL_WRITE] = sgx_ocall_write,
  535. [OCALL_FSTAT] = sgx_ocall_fstat,
  536. [OCALL_FIONREAD] = sgx_ocall_fionread,
  537. [OCALL_FSETNONBLOCK] = sgx_ocall_fsetnonblock,
  538. [OCALL_FCHMOD] = sgx_ocall_fchmod,
  539. [OCALL_FSYNC] = sgx_ocall_fsync,
  540. [OCALL_FTRUNCATE] = sgx_ocall_ftruncate,
  541. [OCALL_LSEEK] = sgx_ocall_lseek,
  542. [OCALL_MKDIR] = sgx_ocall_mkdir,
  543. [OCALL_GETDENTS] = sgx_ocall_getdents,
  544. [OCALL_RESUME_THREAD] = sgx_ocall_resume_thread,
  545. [OCALL_CLONE_THREAD] = sgx_ocall_clone_thread,
  546. [OCALL_CREATE_PROCESS] = sgx_ocall_create_process,
  547. [OCALL_FUTEX] = sgx_ocall_futex,
  548. [OCALL_SOCKETPAIR] = sgx_ocall_socketpair,
  549. [OCALL_LISTEN] = sgx_ocall_listen,
  550. [OCALL_ACCEPT] = sgx_ocall_accept,
  551. [OCALL_CONNECT] = sgx_ocall_connect,
  552. [OCALL_RECV] = sgx_ocall_recv,
  553. [OCALL_SEND] = sgx_ocall_send,
  554. [OCALL_SETSOCKOPT] = sgx_ocall_setsockopt,
  555. [OCALL_SHUTDOWN] = sgx_ocall_shutdown,
  556. [OCALL_GETTIME] = sgx_ocall_gettime,
  557. [OCALL_SLEEP] = sgx_ocall_sleep,
  558. [OCALL_POLL] = sgx_ocall_poll,
  559. [OCALL_RENAME] = sgx_ocall_rename,
  560. [OCALL_DELETE] = sgx_ocall_delete,
  561. [OCALL_LOAD_DEBUG] = sgx_ocall_load_debug,
  562. [OCALL_GET_ATTESTATION] = sgx_ocall_get_attestation,
  563. [OCALL_EVENTFD] = sgx_ocall_eventfd,
  564. };
  565. #define EDEBUG(code, ms) do {} while (0)
  566. int ecall_enclave_start (char * args, size_t args_size, char * env, size_t env_size)
  567. {
  568. ms_ecall_enclave_start_t ms;
  569. ms.ms_args = args;
  570. ms.ms_args_size = args_size;
  571. ms.ms_env = env;
  572. ms.ms_env_size = env_size;
  573. ms.ms_sec_info = &pal_enclave.pal_sec;
  574. EDEBUG(ECALL_ENCLAVE_START, &ms);
  575. return sgx_ecall(ECALL_ENCLAVE_START, &ms);
  576. }
  577. int ecall_thread_start (void)
  578. {
  579. EDEBUG(ECALL_THREAD_START, NULL);
  580. return sgx_ecall(ECALL_THREAD_START, NULL);
  581. }
  582. int ecall_thread_reset(void) {
  583. EDEBUG(ECALL_THREAD_RESET, NULL);
  584. return sgx_ecall(ECALL_THREAD_RESET, NULL);
  585. }
  586. noreturn void __abort(void) {
  587. INLINE_SYSCALL(exit_group, 1, -1);
  588. while (true) {
  589. /* nothing */;
  590. }
  591. }