sgx_enclave.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "ocall_types.h"
  4. #include "ecall_types.h"
  5. #include "sgx_internal.h"
  6. #include "pal_security.h"
  7. #include "pal_linux_error.h"
  8. #include <asm/mman.h>
  9. #include <asm/ioctls.h>
  10. #include <asm/socket.h>
  11. #include <linux/fs.h>
  12. #include <linux/in.h>
  13. #include <linux/in6.h>
  14. #include <math.h>
  15. #include <asm/errno.h>
  16. #ifndef SOL_IPV6
  17. # define SOL_IPV6 41
  18. #endif
  19. #define ODEBUG(code, ms) do {} while (0)
  20. static int sgx_ocall_exit(void* prv)
  21. {
  22. int64_t rv = (int64_t) prv;
  23. ODEBUG(OCALL_EXIT, NULL);
  24. if (rv != (int64_t) ((uint8_t) rv)) {
  25. SGX_DBG(DBG_E, "Saturation error in exit code %ld, getting rounded down to %u\n", rv, (uint8_t) rv);
  26. rv = 255;
  27. }
  28. INLINE_SYSCALL(exit, 1, (int)rv);
  29. return 0;
  30. }
  31. static int sgx_ocall_print_string(void * pms)
  32. {
  33. ms_ocall_print_string_t * ms = (ms_ocall_print_string_t *) pms;
  34. INLINE_SYSCALL(write, 3, 2, ms->ms_str, ms->ms_length);
  35. return 0;
  36. }
  37. static int sgx_ocall_alloc_untrusted(void * pms)
  38. {
  39. ms_ocall_alloc_untrusted_t * ms = (ms_ocall_alloc_untrusted_t *) pms;
  40. void * addr;
  41. ODEBUG(OCALL_ALLOC_UNTRUSTED, ms);
  42. addr = (void *) INLINE_SYSCALL(mmap, 6, NULL, ms->ms_size,
  43. PROT_READ|PROT_WRITE,
  44. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  45. if (IS_ERR_P(addr))
  46. return -ERRNO_P(addr);
  47. ms->ms_mem = addr;
  48. return 0;
  49. }
  50. static int sgx_ocall_map_untrusted(void * pms)
  51. {
  52. ms_ocall_map_untrusted_t * ms = (ms_ocall_map_untrusted_t *) pms;
  53. void * addr;
  54. ODEBUG(OCALL_MAP_UNTRUSTED, ms);
  55. addr = (void *) INLINE_SYSCALL(mmap, 6, NULL, ms->ms_size,
  56. ms->ms_prot,
  57. MAP_FILE|MAP_SHARED,
  58. ms->ms_fd, ms->ms_offset);
  59. if (IS_ERR_P(addr))
  60. return -ERRNO_P(addr);
  61. ms->ms_mem = addr;
  62. return 0;
  63. }
  64. static int sgx_ocall_unmap_untrusted(void * pms)
  65. {
  66. ms_ocall_unmap_untrusted_t * ms = (ms_ocall_unmap_untrusted_t *) pms;
  67. ODEBUG(OCALL_UNMAP_UNTRUSTED, ms);
  68. INLINE_SYSCALL(munmap, 2, ALLOC_ALIGNDOWN(ms->ms_mem),
  69. ALLOC_ALIGNUP(ms->ms_mem + ms->ms_size) -
  70. ALLOC_ALIGNDOWN(ms->ms_mem));
  71. return 0;
  72. }
  73. static int sgx_ocall_cpuid(void * pms)
  74. {
  75. ms_ocall_cpuid_t * ms = (ms_ocall_cpuid_t *) pms;
  76. ODEBUG(OCALL_CPUID, ms);
  77. __asm__ volatile ("cpuid"
  78. : "=a"(ms->ms_values[0]),
  79. "=b"(ms->ms_values[1]),
  80. "=c"(ms->ms_values[2]),
  81. "=d"(ms->ms_values[3])
  82. : "a"(ms->ms_leaf), "c"(ms->ms_subleaf) : "memory");
  83. return 0;
  84. }
  85. static int sgx_ocall_open(void * pms)
  86. {
  87. ms_ocall_open_t * ms = (ms_ocall_open_t *) pms;
  88. int ret;
  89. ODEBUG(OCALL_OPEN, ms);
  90. ret = INLINE_SYSCALL(open, 3, ms->ms_pathname, ms->ms_flags|O_CLOEXEC,
  91. ms->ms_mode);
  92. return ret;
  93. }
  94. static int sgx_ocall_close(void * pms)
  95. {
  96. ms_ocall_close_t * ms = (ms_ocall_close_t *) pms;
  97. ODEBUG(OCALL_CLOSE, ms);
  98. INLINE_SYSCALL(close, 1, ms->ms_fd);
  99. return 0;
  100. }
  101. static int sgx_ocall_read(void * pms)
  102. {
  103. ms_ocall_read_t * ms = (ms_ocall_read_t *) pms;
  104. int ret;
  105. ODEBUG(OCALL_READ, ms);
  106. ret = INLINE_SYSCALL(read, 3, ms->ms_fd, ms->ms_buf, ms->ms_count);
  107. return ret;
  108. }
  109. static int sgx_ocall_write(void * pms)
  110. {
  111. ms_ocall_write_t * ms = (ms_ocall_write_t *) pms;
  112. int ret;
  113. ODEBUG(OCALL_WRITE, ms);
  114. ret = INLINE_SYSCALL(write, 3, ms->ms_fd, ms->ms_buf, ms->ms_count);
  115. return ret;
  116. }
  117. static int sgx_ocall_fstat(void * pms)
  118. {
  119. ms_ocall_fstat_t * ms = (ms_ocall_fstat_t *) pms;
  120. int ret;
  121. ODEBUG(OCALL_FSTAT, ms);
  122. ret = INLINE_SYSCALL(fstat, 2, ms->ms_fd, &ms->ms_stat);
  123. return ret;
  124. }
  125. static int sgx_ocall_fionread(void * pms)
  126. {
  127. ms_ocall_fionread_t * ms = (ms_ocall_fionread_t *) pms;
  128. int ret, val;
  129. ODEBUG(OCALL_FIONREAD, ms);
  130. ret = INLINE_SYSCALL(ioctl, 3, ms->ms_fd, FIONREAD, &val);
  131. return IS_ERR(ret) ? ret : val;
  132. }
  133. static int sgx_ocall_fsetnonblock(void * pms)
  134. {
  135. ms_ocall_fsetnonblock_t * ms = (ms_ocall_fsetnonblock_t *) pms;
  136. int ret, flags;
  137. ODEBUG(OCALL_FSETNONBLOCK, ms);
  138. ret = INLINE_SYSCALL(fcntl, 2, ms->ms_fd, F_GETFL);
  139. if (IS_ERR(ret))
  140. return ret;
  141. flags = ret;
  142. if (ms->ms_nonblocking) {
  143. if (!(flags & O_NONBLOCK))
  144. ret = INLINE_SYSCALL(fcntl, 3, ms->ms_fd, F_SETFL,
  145. flags | O_NONBLOCK);
  146. } else {
  147. if (flags & O_NONBLOCK)
  148. ret = INLINE_SYSCALL(fcntl, 3, ms->ms_fd, F_SETFL,
  149. flags & ~O_NONBLOCK);
  150. }
  151. return ret;
  152. }
  153. static int sgx_ocall_fchmod(void * pms)
  154. {
  155. ms_ocall_fchmod_t * ms = (ms_ocall_fchmod_t *) pms;
  156. int ret;
  157. ODEBUG(OCALL_FCHMOD, ms);
  158. ret = INLINE_SYSCALL(fchmod, 2, ms->ms_fd, ms->ms_mode);
  159. return ret;
  160. }
  161. static int sgx_ocall_fsync(void * pms)
  162. {
  163. ms_ocall_fsync_t * ms = (ms_ocall_fsync_t *) pms;
  164. ODEBUG(OCALL_FSYNC, ms);
  165. INLINE_SYSCALL(fsync, 1, ms->ms_fd);
  166. return 0;
  167. }
  168. static int sgx_ocall_ftruncate(void * pms)
  169. {
  170. ms_ocall_ftruncate_t * ms = (ms_ocall_ftruncate_t *) pms;
  171. int ret;
  172. ODEBUG(OCALL_FTRUNCATE, ms);
  173. ret = INLINE_SYSCALL(ftruncate, 2, ms->ms_fd, ms->ms_length);
  174. return ret;
  175. }
  176. static int sgx_ocall_mkdir(void * pms)
  177. {
  178. ms_ocall_mkdir_t * ms = (ms_ocall_mkdir_t *) pms;
  179. int ret;
  180. ODEBUG(OCALL_MKDIR, ms);
  181. ret = INLINE_SYSCALL(mkdir, 2, ms->ms_pathname, ms->ms_mode);
  182. return ret;
  183. }
  184. static int sgx_ocall_getdents(void * pms)
  185. {
  186. ms_ocall_getdents_t * ms = (ms_ocall_getdents_t *) pms;
  187. int ret;
  188. ODEBUG(OCALL_GETDENTS, ms);
  189. ret = INLINE_SYSCALL(getdents64, 3, ms->ms_fd, ms->ms_dirp, ms->ms_size);
  190. return ret;
  191. }
  192. static int sgx_ocall_wake_thread(void * pms)
  193. {
  194. ODEBUG(OCALL_WAKE_THREAD, pms);
  195. return pms ? interrupt_thread(pms) : clone_thread();
  196. }
  197. int sgx_create_process (const char * uri,
  198. int nargs, const char ** args,
  199. int * retfds);
  200. static int sgx_ocall_create_process(void * pms)
  201. {
  202. ms_ocall_create_process_t * ms = (ms_ocall_create_process_t *) pms;
  203. ODEBUG(OCALL_CREATE_PROCESS, ms);
  204. int ret = sgx_create_process(ms->ms_uri, ms->ms_nargs, ms->ms_args,
  205. ms->ms_proc_fds);
  206. if (IS_ERR(ret))
  207. return ret;
  208. ms->ms_pid = ret;
  209. return 0;
  210. }
  211. static int sgx_ocall_futex(void * pms)
  212. {
  213. ms_ocall_futex_t * ms = (ms_ocall_futex_t *) pms;
  214. int ret;
  215. ODEBUG(OCALL_FUTEX, ms);
  216. struct timespec * ts = NULL;
  217. if (ms->ms_timeout != OCALL_NO_TIMEOUT) {
  218. ts = __alloca(sizeof(struct timespec));
  219. ts->tv_sec = ms->ms_timeout / 1000000;
  220. ts->tv_nsec = (ms->ms_timeout - ts->tv_sec * 1000000) * 1000;
  221. }
  222. ret = INLINE_SYSCALL(futex, 6, ms->ms_futex, ms->ms_op, ms->ms_val,
  223. ts, NULL, 0);
  224. return ret;
  225. }
  226. static int sgx_ocall_socketpair(void * pms)
  227. {
  228. ms_ocall_socketpair_t * ms = (ms_ocall_socketpair_t *) pms;
  229. int ret;
  230. ODEBUG(OCALL_SOCKETPAIR, ms);
  231. ret = INLINE_SYSCALL(socketpair, 4, ms->ms_domain,
  232. ms->ms_type|SOCK_CLOEXEC,
  233. ms->ms_protocol, &ms->ms_sockfds);
  234. return ret;
  235. }
  236. static int sock_getopt(int fd, struct sockopt * opt)
  237. {
  238. SGX_DBG(DBG_M, "sock_getopt (fd = %d, sockopt addr = %p) is not implemented \
  239. always returns 0\n", fd, opt);
  240. return 0;
  241. }
  242. static int sgx_ocall_sock_listen(void * pms)
  243. {
  244. ms_ocall_sock_listen_t * ms = (ms_ocall_sock_listen_t *) pms;
  245. int ret, fd;
  246. ODEBUG(OCALL_SOCK_LISTEN, ms);
  247. ret = INLINE_SYSCALL(socket, 3, ms->ms_domain,
  248. ms->ms_type|SOCK_CLOEXEC,
  249. ms->ms_protocol);
  250. if (IS_ERR(ret))
  251. goto err;
  252. fd = ret;
  253. if (ms->ms_addr->sa_family == AF_INET6) {
  254. int ipv6only = 1;
  255. INLINE_SYSCALL(setsockopt, 5, fd, SOL_IPV6, IPV6_V6ONLY, &ipv6only,
  256. sizeof(int));
  257. }
  258. /* must set the socket to be reuseable */
  259. int reuseaddr = 1;
  260. INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
  261. sizeof(int));
  262. ret = INLINE_SYSCALL(bind, 3, fd, ms->ms_addr, ms->ms_addrlen);
  263. if (IS_ERR(ret))
  264. goto err_fd;
  265. if (ms->ms_addr) {
  266. socklen_t addrlen;
  267. ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_addr, &addrlen);
  268. if (IS_ERR(ret))
  269. goto err_fd;
  270. ms->ms_addrlen = addrlen;
  271. }
  272. if (ms->ms_type & SOCK_STREAM) {
  273. ret = INLINE_SYSCALL(listen, 2, fd, DEFAULT_BACKLOG);
  274. if (IS_ERR(ret))
  275. goto err_fd;
  276. }
  277. ret = sock_getopt(fd, &ms->ms_sockopt);
  278. if (IS_ERR(ret))
  279. goto err_fd;
  280. return fd;
  281. err_fd:
  282. INLINE_SYSCALL(close, 1, fd);
  283. err:
  284. return ret;
  285. }
  286. static int sgx_ocall_sock_accept(void * pms)
  287. {
  288. ms_ocall_sock_accept_t * ms = (ms_ocall_sock_accept_t *) pms;
  289. int ret, fd;
  290. ODEBUG(OCALL_SOCK_ACCEPT, ms);
  291. socklen_t addrlen = ms->ms_addrlen;
  292. ret = INLINE_SYSCALL(accept4, 4, ms->ms_sockfd, ms->ms_addr,
  293. &addrlen, O_CLOEXEC);
  294. if (IS_ERR(ret))
  295. goto err;
  296. fd = ret;
  297. ret = sock_getopt(fd, &ms->ms_sockopt);
  298. if (IS_ERR(ret))
  299. goto err_fd;
  300. ms->ms_addrlen = addrlen;
  301. return fd;
  302. err_fd:
  303. INLINE_SYSCALL(close, 1, fd);
  304. err:
  305. return ret;
  306. }
  307. static int sgx_ocall_sock_connect(void * pms)
  308. {
  309. ms_ocall_sock_connect_t * ms = (ms_ocall_sock_connect_t *) pms;
  310. int ret, fd;
  311. ODEBUG(OCALL_SOCK_CONNECT, ms);
  312. ret = INLINE_SYSCALL(socket, 3, ms->ms_domain,
  313. ms->ms_type|SOCK_CLOEXEC,
  314. ms->ms_protocol);
  315. if (IS_ERR(ret))
  316. goto err;
  317. fd = ret;
  318. if (ms->ms_addr && ms->ms_addr->sa_family == AF_INET6) {
  319. int ipv6only = 1;
  320. INLINE_SYSCALL(setsockopt, 5, fd, SOL_IPV6, IPV6_V6ONLY, &ipv6only,
  321. sizeof(int));
  322. }
  323. if (ms->ms_bind_addr && ms->ms_bind_addr->sa_family) {
  324. ret = INLINE_SYSCALL(bind, 3, fd, ms->ms_bind_addr,
  325. ms->ms_bind_addrlen);
  326. if (IS_ERR(ret))
  327. goto err_fd;
  328. }
  329. if (ms->ms_addr) {
  330. ret = INLINE_SYSCALL(connect, 3, fd, ms->ms_addr, ms->ms_addrlen);
  331. if (IS_ERR(ret) && ERRNO(ret) == EINPROGRESS) {
  332. do {
  333. struct pollfd pfd = { .fd = fd, .events = POLLOUT, .revents = 0, };
  334. ret = INLINE_SYSCALL(ppoll, 4, &pfd, 1, NULL, NULL);
  335. } while (IS_ERR(ret) &&
  336. ERRNO(ret) == -EWOULDBLOCK);
  337. }
  338. if (IS_ERR(ret))
  339. goto err_fd;
  340. }
  341. if (ms->ms_bind_addr && !ms->ms_bind_addr->sa_family) {
  342. socklen_t addrlen;
  343. ret = INLINE_SYSCALL(getsockname, 3, fd, ms->ms_bind_addr,
  344. &addrlen);
  345. if (IS_ERR(ret))
  346. goto err_fd;
  347. ms->ms_bind_addrlen = addrlen;
  348. }
  349. ret = sock_getopt(fd, &ms->ms_sockopt);
  350. if (IS_ERR(ret))
  351. goto err_fd;
  352. return fd;
  353. err_fd:
  354. INLINE_SYSCALL(close, 1, fd);
  355. err:
  356. return ret;
  357. }
  358. static int sgx_ocall_sock_recv(void * pms)
  359. {
  360. ms_ocall_sock_recv_t * ms = (ms_ocall_sock_recv_t *) pms;
  361. int ret;
  362. ODEBUG(OCALL_SOCK_RECV, ms);
  363. struct sockaddr * addr = ms->ms_addr;
  364. socklen_t addrlen = ms->ms_addr ? ms->ms_addrlen : 0;
  365. if (ms->ms_sockfd == PAL_SEC()->mcast_srv)
  366. addr = NULL;
  367. ret = INLINE_SYSCALL(recvfrom, 6,
  368. ms->ms_sockfd, ms->ms_buf, ms->ms_count, 0,
  369. addr, addr ? &addrlen : NULL);
  370. if (!IS_ERR(ret) && addr)
  371. ms->ms_addrlen = addrlen;
  372. return ret;
  373. }
  374. static int sgx_ocall_sock_send(void * pms)
  375. {
  376. ms_ocall_sock_send_t * ms = (ms_ocall_sock_send_t *) pms;
  377. int ret;
  378. ODEBUG(OCALL_SOCK_SEND, ms);
  379. const struct sockaddr * addr = ms->ms_addr;
  380. socklen_t addrlen = ms->ms_addr ? ms->ms_addrlen : 0;
  381. struct sockaddr_in mcast_addr;
  382. if (ms->ms_sockfd == PAL_SEC()->mcast_srv) {
  383. mcast_addr.sin_family = AF_INET;
  384. inet_pton4(MCAST_GROUP, sizeof(MCAST_GROUP), &mcast_addr.sin_addr.s_addr);
  385. mcast_addr.sin_port = htons(PAL_SEC()->mcast_port);
  386. addr = (struct sockaddr *) &mcast_addr;
  387. addrlen = sizeof(struct sockaddr_in);
  388. }
  389. ret = INLINE_SYSCALL(sendto, 6,
  390. ms->ms_sockfd, ms->ms_buf, ms->ms_count, MSG_NOSIGNAL,
  391. addr, addrlen);
  392. return ret;
  393. }
  394. static int sgx_ocall_sock_recv_fd(void * pms)
  395. {
  396. ms_ocall_sock_recv_fd_t * ms = (ms_ocall_sock_recv_fd_t *) pms;
  397. int ret;
  398. ODEBUG(OCALL_SOCK_RECV_FD, ms);
  399. struct msghdr hdr;
  400. struct iovec iov[1];
  401. // receive PAL_HANDLE contents in the body
  402. char cbuf[sizeof(struct cmsghdr) + ms->ms_nfds * sizeof(int)];
  403. iov[0].iov_base = ms->ms_buf;
  404. iov[0].iov_len = ms->ms_count;
  405. // clear body memory
  406. memset(&hdr, 0, sizeof(struct msghdr));
  407. // set message header values
  408. hdr.msg_iov = iov;
  409. hdr.msg_iovlen = 1;
  410. hdr.msg_control = cbuf;
  411. hdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) *
  412. ms->ms_nfds;
  413. hdr.msg_flags = 0;
  414. ret = INLINE_SYSCALL(recvmsg, 3, ms->ms_sockfd, &hdr, 0);
  415. if (!IS_ERR(ret)) {
  416. struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
  417. if (chdr &&
  418. chdr->cmsg_type == SCM_RIGHTS) {
  419. ms->ms_nfds = (chdr->cmsg_len - sizeof(struct cmsghdr)) /
  420. sizeof(int);
  421. memcpy(ms->ms_fds, CMSG_DATA(chdr), sizeof(int) * ms->ms_nfds);
  422. } else {
  423. ms->ms_nfds = 0;
  424. }
  425. }
  426. return ret;
  427. }
  428. static int sgx_ocall_sock_send_fd(void * pms)
  429. {
  430. ms_ocall_sock_send_fd_t * ms = (ms_ocall_sock_send_fd_t *) pms;
  431. int ret;
  432. ODEBUG(OCALL_SOCK_SEND_FD, ms);
  433. // Declare variables required for sending the message
  434. struct msghdr hdr; // message header
  435. struct cmsghdr * chdr; //control message header
  436. struct iovec iov[1]; // IO Vector
  437. /* Message Body Composition:
  438. IOVEC[0]: PAL_HANDLE
  439. IOVEC[1..n]: Additional handle member follow
  440. Control Message: file descriptors */
  441. // Control message buffer with added space for 2 fds (ie. max size
  442. // that it will have)
  443. char cbuf[sizeof(struct cmsghdr) + ms->ms_nfds * sizeof(int)];
  444. iov[0].iov_base = (void *) ms->ms_buf;
  445. iov[0].iov_len = ms->ms_count;
  446. hdr.msg_name = NULL;
  447. hdr.msg_namelen = 0;
  448. hdr.msg_iov = iov;
  449. hdr.msg_iovlen = 1;
  450. hdr.msg_flags = 0;
  451. hdr.msg_control = cbuf; // Control Message Buffer
  452. hdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * ms->ms_nfds;
  453. // Fill control message infomation for the file descriptors
  454. // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
  455. // cbuf, which is redundant based on the above code as we have
  456. // statically allocated memory.
  457. // or (struct cmsghdr*) cbuf
  458. chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
  459. chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
  460. chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
  461. // Length of control message = sizeof(struct cmsghdr) + nfds
  462. chdr->cmsg_len = CMSG_LEN(sizeof(int) * ms->ms_nfds);
  463. // Copy the fds below control header
  464. memcpy(CMSG_DATA(chdr), ms->ms_fds, sizeof(int) * ms->ms_nfds);
  465. // Also, Update main header with control message length (duplicate)
  466. hdr.msg_controllen = chdr->cmsg_len;
  467. ret = INLINE_SYSCALL(sendmsg, 3, ms->ms_sockfd, &hdr, MSG_NOSIGNAL);
  468. return ret;
  469. }
  470. static int sgx_ocall_sock_setopt(void * pms)
  471. {
  472. ms_ocall_sock_setopt_t * ms = (ms_ocall_sock_setopt_t *) pms;
  473. int ret;
  474. ODEBUG(OCALL_SOCK_SETOPT, ms);
  475. ret = INLINE_SYSCALL(setsockopt, 5,
  476. ms->ms_sockfd, ms->ms_level, ms->ms_optname,
  477. ms->ms_optval, ms->ms_optlen);
  478. return ret;
  479. }
  480. static int sgx_ocall_sock_shutdown(void * pms)
  481. {
  482. ms_ocall_sock_shutdown_t * ms = (ms_ocall_sock_shutdown_t *) pms;
  483. ODEBUG(OCALL_SOCK_SHUTDOWN, ms);
  484. INLINE_SYSCALL(shutdown, 2, ms->ms_sockfd, ms->ms_how);
  485. return 0;
  486. }
  487. static int sgx_ocall_gettime(void * pms)
  488. {
  489. ms_ocall_gettime_t * ms = (ms_ocall_gettime_t *) pms;
  490. ODEBUG(OCALL_GETTIME, ms);
  491. struct timeval tv;
  492. INLINE_SYSCALL(gettimeofday, 2, &tv, NULL);
  493. ms->ms_microsec = tv.tv_sec * 1000000UL + tv.tv_usec;
  494. return 0;
  495. }
  496. static int sgx_ocall_sleep(void * pms)
  497. {
  498. ms_ocall_sleep_t * ms = (ms_ocall_sleep_t *) pms;
  499. int ret;
  500. ODEBUG(OCALL_SLEEP, ms);
  501. if (!ms->ms_microsec) {
  502. INLINE_SYSCALL(sched_yield, 0);
  503. return 0;
  504. }
  505. struct timespec req, rem;
  506. unsigned long microsec = ms->ms_microsec;
  507. const long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
  508. if (ms->ms_microsec > VERY_LONG_TIME_IN_US) {
  509. /* avoid overflow with time_t */
  510. req.tv_sec = VERY_LONG_TIME_IN_US / 1000000;
  511. req.tv_nsec = 0;
  512. } else {
  513. req.tv_sec = ms->ms_microsec / 1000000;
  514. req.tv_nsec = (microsec - req.tv_sec * 1000000) * 1000;
  515. }
  516. ret = INLINE_SYSCALL(nanosleep, 2, &req, &rem);
  517. if (IS_ERR(ret) && ERRNO(ret) == EINTR)
  518. ms->ms_microsec = rem.tv_sec * 1000000UL + rem.tv_nsec / 1000UL;
  519. return ret;
  520. }
  521. static int sgx_ocall_poll(void * pms)
  522. {
  523. ms_ocall_poll_t * ms = (ms_ocall_poll_t *) pms;
  524. int ret;
  525. ODEBUG(OCALL_POLL, ms);
  526. struct timespec * ts = NULL;
  527. if (ms->ms_timeout != OCALL_NO_TIMEOUT) {
  528. ts = __alloca(sizeof(struct timespec));
  529. ts->tv_sec = ms->ms_timeout / 1000000;
  530. ts->tv_nsec = (ms->ms_timeout - ts->tv_sec * 1000000) * 1000;
  531. }
  532. ret = INLINE_SYSCALL(ppoll, 4, ms->ms_fds, ms->ms_nfds, ts, NULL);
  533. return ret;
  534. }
  535. static int sgx_ocall_rename(void * pms)
  536. {
  537. ms_ocall_rename_t * ms = (ms_ocall_rename_t *) pms;
  538. int ret;
  539. ODEBUG(OCALL_RENAME, ms);
  540. ret = INLINE_SYSCALL(rename, 2, ms->ms_oldpath, ms->ms_newpath);
  541. return ret;
  542. }
  543. static int sgx_ocall_delete(void * pms)
  544. {
  545. ms_ocall_delete_t * ms = (ms_ocall_delete_t *) pms;
  546. int ret;
  547. ODEBUG(OCALL_DELETE, ms);
  548. ret = INLINE_SYSCALL(unlink, 1, ms->ms_pathname);
  549. if (IS_ERR(ret) && ERRNO(ret) == EISDIR)
  550. ret = INLINE_SYSCALL(rmdir, 1, ms->ms_pathname);
  551. return ret;
  552. }
  553. void load_gdb_command (const char * command);
  554. static int sgx_ocall_load_debug(void * pms)
  555. {
  556. const char * command = (const char *) pms;
  557. ODEBUG(OCALL_LOAD_DEBUG, (void *) command);
  558. load_gdb_command(command);
  559. return 0;
  560. }
  561. sgx_ocall_fn_t ocall_table[OCALL_NR] = {
  562. [OCALL_EXIT] = sgx_ocall_exit,
  563. [OCALL_PRINT_STRING] = sgx_ocall_print_string,
  564. [OCALL_ALLOC_UNTRUSTED] = sgx_ocall_alloc_untrusted,
  565. [OCALL_MAP_UNTRUSTED] = sgx_ocall_map_untrusted,
  566. [OCALL_UNMAP_UNTRUSTED] = sgx_ocall_unmap_untrusted,
  567. [OCALL_CPUID] = sgx_ocall_cpuid,
  568. [OCALL_OPEN] = sgx_ocall_open,
  569. [OCALL_CLOSE] = sgx_ocall_close,
  570. [OCALL_READ] = sgx_ocall_read,
  571. [OCALL_WRITE] = sgx_ocall_write,
  572. [OCALL_FSTAT] = sgx_ocall_fstat,
  573. [OCALL_FIONREAD] = sgx_ocall_fionread,
  574. [OCALL_FSETNONBLOCK] = sgx_ocall_fsetnonblock,
  575. [OCALL_FCHMOD] = sgx_ocall_fchmod,
  576. [OCALL_FSYNC] = sgx_ocall_fsync,
  577. [OCALL_FTRUNCATE] = sgx_ocall_ftruncate,
  578. [OCALL_MKDIR] = sgx_ocall_mkdir,
  579. [OCALL_GETDENTS] = sgx_ocall_getdents,
  580. [OCALL_WAKE_THREAD] = sgx_ocall_wake_thread,
  581. [OCALL_CREATE_PROCESS] = sgx_ocall_create_process,
  582. [OCALL_FUTEX] = sgx_ocall_futex,
  583. [OCALL_SOCKETPAIR] = sgx_ocall_socketpair,
  584. [OCALL_SOCK_LISTEN] = sgx_ocall_sock_listen,
  585. [OCALL_SOCK_ACCEPT] = sgx_ocall_sock_accept,
  586. [OCALL_SOCK_CONNECT] = sgx_ocall_sock_connect,
  587. [OCALL_SOCK_RECV] = sgx_ocall_sock_recv,
  588. [OCALL_SOCK_SEND] = sgx_ocall_sock_send,
  589. [OCALL_SOCK_RECV_FD] = sgx_ocall_sock_recv_fd,
  590. [OCALL_SOCK_SEND_FD] = sgx_ocall_sock_send_fd,
  591. [OCALL_SOCK_SETOPT] = sgx_ocall_sock_setopt,
  592. [OCALL_SOCK_SHUTDOWN] = sgx_ocall_sock_shutdown,
  593. [OCALL_GETTIME] = sgx_ocall_gettime,
  594. [OCALL_SLEEP] = sgx_ocall_sleep,
  595. [OCALL_POLL] = sgx_ocall_poll,
  596. [OCALL_RENAME] = sgx_ocall_rename,
  597. [OCALL_DELETE] = sgx_ocall_delete,
  598. [OCALL_LOAD_DEBUG] = sgx_ocall_load_debug,
  599. };
  600. #define EDEBUG(code, ms) do {} while (0)
  601. int ecall_enclave_start (char * args, uint64_t args_size, char * env, uint64_t env_size)
  602. {
  603. ms_ecall_enclave_start_t ms;
  604. ms.ms_args = args;
  605. ms.ms_args_size = args_size;
  606. ms.ms_env = env;
  607. ms.ms_env_size = env_size;
  608. ms.ms_sec_info = PAL_SEC();
  609. EDEBUG(ECALL_ENCLAVE_START, &ms);
  610. return sgx_ecall(ECALL_ENCLAVE_START, &ms);
  611. }
  612. int ecall_thread_start (void)
  613. {
  614. EDEBUG(ECALL_THREAD_START, NULL);
  615. return sgx_ecall(ECALL_THREAD_START, NULL);
  616. }
  617. void __abort(void) {
  618. INLINE_SYSCALL(exit_group, 1, -1);
  619. }