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