enclave_ocalls.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. * This is for enclave to make ocalls to untrusted runtime.
  3. */
  4. #include "pal_linux.h"
  5. #include "pal_internal.h"
  6. #include "pal_debug.h"
  7. #include "enclave_ocalls.h"
  8. #include "ocall_types.h"
  9. #include "ecall_types.h"
  10. #include <api.h>
  11. #include <asm/errno.h>
  12. /* Check against this limit if the buffer to be allocated fits on the untrusted stack; if not,
  13. * buffer will be allocated on untrusted heap. Conservatively set this limit to 1/4 of the
  14. * actual stack size. Currently THREAD_STACK_SIZE = 2MB, so this limit is 512KB.
  15. * Note that the main thread is special in that it is handled by Linux, with the typical stack
  16. * size of 8MB. Thus, 512KB limit also works well for the main thread. */
  17. #define MAX_UNTRUSTED_STACK_BUF (THREAD_STACK_SIZE / 4)
  18. noreturn void ocall_exit(int exitcode, int is_exitgroup)
  19. {
  20. ms_ocall_exit_t * ms;
  21. ms = sgx_alloc_on_ustack(sizeof(*ms));
  22. ms->ms_exitcode = exitcode;
  23. ms->ms_is_exitgroup = is_exitgroup;
  24. // There are two reasons for this loop:
  25. // 1. Ocalls can be interuppted.
  26. // 2. We can't trust the outside to actually exit, so we need to ensure
  27. // that we never return even when the outside tries to trick us (this
  28. // case should be already catched by enclave_entry.S).
  29. while (true) {
  30. sgx_ocall(OCALL_EXIT, ms);
  31. }
  32. }
  33. int ocall_mmap_untrusted (int fd, uint64_t offset,
  34. uint64_t size, unsigned short prot,
  35. void ** mem)
  36. {
  37. int retval = 0;
  38. ms_ocall_mmap_untrusted_t * ms;
  39. ms = sgx_alloc_on_ustack(sizeof(*ms));
  40. if (!ms) {
  41. sgx_reset_ustack();
  42. return -EPERM;
  43. }
  44. ms->ms_fd = fd;
  45. ms->ms_offset = offset;
  46. ms->ms_size = size;
  47. ms->ms_prot = prot;
  48. retval = sgx_ocall(OCALL_MMAP_UNTRUSTED, ms);
  49. if (!retval) {
  50. if (!sgx_copy_ptr_to_enclave(mem, ms->ms_mem, size)) {
  51. sgx_reset_ustack();
  52. return -EPERM;
  53. }
  54. }
  55. sgx_reset_ustack();
  56. return retval;
  57. }
  58. int ocall_munmap_untrusted (const void * mem, uint64_t size)
  59. {
  60. int retval = 0;
  61. ms_ocall_munmap_untrusted_t * ms;
  62. if (!sgx_is_completely_outside_enclave(mem, size)) {
  63. sgx_reset_ustack();
  64. return -EINVAL;
  65. }
  66. ms = sgx_alloc_on_ustack(sizeof(*ms));
  67. if (!ms) {
  68. sgx_reset_ustack();
  69. return -EPERM;
  70. }
  71. ms->ms_mem = mem;
  72. ms->ms_size = size;
  73. retval = sgx_ocall(OCALL_MUNMAP_UNTRUSTED, ms);
  74. sgx_reset_ustack();
  75. return retval;
  76. }
  77. int ocall_cpuid (unsigned int leaf, unsigned int subleaf,
  78. unsigned int values[4])
  79. {
  80. int retval = 0;
  81. ms_ocall_cpuid_t * ms;
  82. ms = sgx_alloc_on_ustack(sizeof(*ms));
  83. if (!ms) {
  84. sgx_reset_ustack();
  85. return -EPERM;
  86. }
  87. ms->ms_leaf = leaf;
  88. ms->ms_subleaf = subleaf;
  89. retval = sgx_ocall(OCALL_CPUID, ms);
  90. if (!retval) {
  91. values[0] = ms->ms_values[0];
  92. values[1] = ms->ms_values[1];
  93. values[2] = ms->ms_values[2];
  94. values[3] = ms->ms_values[3];
  95. }
  96. sgx_reset_ustack();
  97. return retval;
  98. }
  99. int ocall_open (const char * pathname, int flags, unsigned short mode)
  100. {
  101. int retval = 0;
  102. int len = pathname ? strlen(pathname) + 1 : 0;
  103. ms_ocall_open_t * ms;
  104. ms = sgx_alloc_on_ustack(sizeof(*ms));
  105. if (!ms) {
  106. sgx_reset_ustack();
  107. return -EPERM;
  108. }
  109. ms->ms_flags = flags;
  110. ms->ms_mode = mode;
  111. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  112. if (!ms->ms_pathname) {
  113. sgx_reset_ustack();
  114. return -EPERM;
  115. }
  116. retval = sgx_ocall(OCALL_OPEN, ms);
  117. sgx_reset_ustack();
  118. return retval;
  119. }
  120. int ocall_close (int fd)
  121. {
  122. int retval = 0;
  123. ms_ocall_close_t *ms;
  124. ms = sgx_alloc_on_ustack(sizeof(*ms));
  125. if (!ms) {
  126. sgx_reset_ustack();
  127. return -EPERM;
  128. }
  129. ms->ms_fd = fd;
  130. retval = sgx_ocall(OCALL_CLOSE, ms);
  131. sgx_reset_ustack();
  132. return retval;
  133. }
  134. int ocall_read (int fd, void * buf, unsigned int count)
  135. {
  136. int retval = 0;
  137. void * obuf = NULL;
  138. ms_ocall_read_t * ms;
  139. if (count > MAX_UNTRUSTED_STACK_BUF) {
  140. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  141. if (IS_ERR(retval))
  142. return retval;
  143. }
  144. ms = sgx_alloc_on_ustack(sizeof(*ms));
  145. if (!ms) {
  146. retval = -EPERM;
  147. goto out;
  148. }
  149. ms->ms_fd = fd;
  150. ms->ms_count = count;
  151. if (obuf)
  152. ms->ms_buf = obuf;
  153. else
  154. ms->ms_buf = sgx_alloc_on_ustack(count);
  155. if (!ms->ms_buf) {
  156. retval = -EPERM;
  157. goto out;
  158. }
  159. retval = sgx_ocall(OCALL_READ, ms);
  160. if (retval > 0) {
  161. if (!sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
  162. retval = -EPERM;
  163. goto out;
  164. }
  165. }
  166. out:
  167. sgx_reset_ustack();
  168. if (obuf)
  169. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  170. return retval;
  171. }
  172. int ocall_write (int fd, const void * buf, unsigned int count)
  173. {
  174. int retval = 0;
  175. void * obuf = NULL;
  176. ms_ocall_write_t * ms;
  177. if (sgx_is_completely_outside_enclave(buf, count)) {
  178. /* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
  179. obuf = (void*)buf;
  180. } else if (sgx_is_completely_within_enclave(buf, count)) {
  181. /* typical case of buf inside of enclave memory */
  182. if (count > MAX_UNTRUSTED_STACK_BUF) {
  183. /* buf is too big and may overflow untrusted stack, so use untrusted heap */
  184. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  185. if (IS_ERR(retval))
  186. return retval;
  187. memcpy(obuf, buf, count);
  188. }
  189. } else {
  190. /* buf is partially in/out of enclave memory */
  191. return -EPERM;
  192. }
  193. ms = sgx_alloc_on_ustack(sizeof(*ms));
  194. if (!ms) {
  195. retval = -EPERM;
  196. goto out;
  197. }
  198. ms->ms_fd = fd;
  199. ms->ms_count = count;
  200. if (obuf)
  201. ms->ms_buf = obuf;
  202. else
  203. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  204. if (!ms->ms_buf) {
  205. retval = -EPERM;
  206. goto out;
  207. }
  208. retval = sgx_ocall(OCALL_WRITE, ms);
  209. out:
  210. sgx_reset_ustack();
  211. if (obuf && obuf != buf)
  212. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  213. return retval;
  214. }
  215. int ocall_fstat (int fd, struct stat * buf)
  216. {
  217. int retval = 0;
  218. ms_ocall_fstat_t * ms;
  219. ms = sgx_alloc_on_ustack(sizeof(*ms));
  220. if (!ms) {
  221. sgx_reset_ustack();
  222. return -EPERM;
  223. }
  224. ms->ms_fd = fd;
  225. retval = sgx_ocall(OCALL_FSTAT, ms);
  226. if (!retval)
  227. memcpy(buf, &ms->ms_stat, sizeof(struct stat));
  228. sgx_reset_ustack();
  229. return retval;
  230. }
  231. int ocall_fionread (int fd)
  232. {
  233. int retval = 0;
  234. ms_ocall_fionread_t * ms;
  235. ms = sgx_alloc_on_ustack(sizeof(*ms));
  236. if (!ms) {
  237. sgx_reset_ustack();
  238. return -EPERM;
  239. }
  240. ms->ms_fd = fd;
  241. retval = sgx_ocall(OCALL_FIONREAD, ms);
  242. sgx_reset_ustack();
  243. return retval;
  244. }
  245. int ocall_fsetnonblock (int fd, int nonblocking)
  246. {
  247. int retval = 0;
  248. ms_ocall_fsetnonblock_t * ms;
  249. ms = sgx_alloc_on_ustack(sizeof(*ms));
  250. if (!ms) {
  251. sgx_reset_ustack();
  252. return -EPERM;
  253. }
  254. ms->ms_fd = fd;
  255. ms->ms_nonblocking = nonblocking;
  256. retval = sgx_ocall(OCALL_FSETNONBLOCK, ms);
  257. sgx_reset_ustack();
  258. return retval;
  259. }
  260. int ocall_fchmod (int fd, unsigned short mode)
  261. {
  262. int retval = 0;
  263. ms_ocall_fchmod_t * ms;
  264. ms = sgx_alloc_on_ustack(sizeof(*ms));
  265. if (!ms) {
  266. sgx_reset_ustack();
  267. return -EPERM;
  268. }
  269. ms->ms_fd = fd;
  270. ms->ms_mode = mode;
  271. retval = sgx_ocall(OCALL_FCHMOD, ms);
  272. sgx_reset_ustack();
  273. return retval;
  274. }
  275. int ocall_fsync (int fd)
  276. {
  277. int retval = 0;
  278. ms_ocall_fsync_t * ms;
  279. ms = sgx_alloc_on_ustack(sizeof(*ms));
  280. if (!ms) {
  281. sgx_reset_ustack();
  282. return -EPERM;
  283. }
  284. ms->ms_fd = fd;
  285. retval = sgx_ocall(OCALL_FSYNC, ms);
  286. sgx_reset_ustack();
  287. return retval;
  288. }
  289. int ocall_ftruncate (int fd, uint64_t length)
  290. {
  291. int retval = 0;
  292. ms_ocall_ftruncate_t * ms;
  293. ms = sgx_alloc_on_ustack(sizeof(*ms));
  294. if (!ms) {
  295. sgx_reset_ustack();
  296. return -EPERM;
  297. }
  298. ms->ms_fd = fd;
  299. ms->ms_length = length;
  300. retval = sgx_ocall(OCALL_FTRUNCATE, ms);
  301. sgx_reset_ustack();
  302. return retval;
  303. }
  304. int ocall_lseek(int fd, uint64_t offset, int whence) {
  305. int retval = 0;
  306. ms_ocall_lseek_t* ms;
  307. ms = sgx_alloc_on_ustack(sizeof(*ms));
  308. if (!ms) {
  309. sgx_reset_ustack();
  310. return -EPERM;
  311. }
  312. ms->ms_fd = fd;
  313. ms->ms_offset = offset;
  314. ms->ms_whence = whence;
  315. retval = sgx_ocall(OCALL_LSEEK, ms);
  316. sgx_reset_ustack();
  317. return retval;
  318. }
  319. int ocall_mkdir (const char * pathname, unsigned short mode)
  320. {
  321. int retval = 0;
  322. int len = pathname ? strlen(pathname) + 1 : 0;
  323. ms_ocall_mkdir_t * ms;
  324. ms = sgx_alloc_on_ustack(sizeof(*ms));
  325. if (!ms) {
  326. sgx_reset_ustack();
  327. return -EPERM;
  328. }
  329. ms->ms_mode = mode;
  330. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  331. if (!ms->ms_pathname) {
  332. sgx_reset_ustack();
  333. return -EPERM;
  334. }
  335. retval = sgx_ocall(OCALL_MKDIR, ms);
  336. sgx_reset_ustack();
  337. return retval;
  338. }
  339. int ocall_getdents (int fd, struct linux_dirent64 * dirp, unsigned int size)
  340. {
  341. int retval = 0;
  342. ms_ocall_getdents_t * ms;
  343. ms = sgx_alloc_on_ustack(sizeof(*ms));
  344. if (!ms) {
  345. sgx_reset_ustack();
  346. return -EPERM;
  347. }
  348. ms->ms_fd = fd;
  349. ms->ms_size = size;
  350. ms->ms_dirp = sgx_alloc_on_ustack(size);
  351. if (!ms->ms_dirp) {
  352. sgx_reset_ustack();
  353. return -EPERM;
  354. }
  355. retval = sgx_ocall(OCALL_GETDENTS, ms);
  356. if (retval > 0) {
  357. if (!sgx_copy_to_enclave(dirp, size, ms->ms_dirp, retval)) {
  358. sgx_reset_ustack();
  359. return -EPERM;
  360. }
  361. }
  362. sgx_reset_ustack();
  363. return retval;
  364. }
  365. int ocall_resume_thread (void * tcs)
  366. {
  367. return sgx_ocall(OCALL_RESUME_THREAD, tcs);
  368. }
  369. int ocall_clone_thread (void)
  370. {
  371. void* dummy = NULL;
  372. return sgx_ocall(OCALL_CLONE_THREAD, dummy);
  373. }
  374. int ocall_create_process(const char* uri, int nargs, const char** args, int* stream_fd,
  375. int* cargo_fd, unsigned int* pid) {
  376. int retval = 0;
  377. int ulen = uri ? strlen(uri) + 1 : 0;
  378. ms_ocall_create_process_t * ms;
  379. ms = sgx_alloc_on_ustack(sizeof(*ms) + nargs * sizeof(char *));
  380. if (!ms) {
  381. sgx_reset_ustack();
  382. return -EPERM;
  383. }
  384. ms->ms_uri = uri ? sgx_copy_to_ustack(uri, ulen) : NULL;
  385. if (uri && !ms->ms_uri) {
  386. sgx_reset_ustack();
  387. return -EPERM;
  388. }
  389. ms->ms_nargs = nargs;
  390. for (int i = 0 ; i < nargs ; i++) {
  391. int len = args[i] ? strlen(args[i]) + 1 : 0;
  392. ms->ms_args[i] = args[i] ? sgx_copy_to_ustack(args[i], len) : NULL;
  393. if (args[i] && !ms->ms_args[i]) {
  394. sgx_reset_ustack();
  395. return -EPERM;
  396. }
  397. }
  398. retval = sgx_ocall(OCALL_CREATE_PROCESS, ms);
  399. if (!retval) {
  400. if (pid)
  401. *pid = ms->ms_pid;
  402. if (stream_fd)
  403. *stream_fd = ms->ms_stream_fd;
  404. if (cargo_fd)
  405. *cargo_fd = ms->ms_cargo_fd;
  406. }
  407. sgx_reset_ustack();
  408. return retval;
  409. }
  410. int ocall_futex(int* futex, int op, int val, int64_t timeout_us) {
  411. int retval = 0;
  412. ms_ocall_futex_t * ms;
  413. if (!sgx_is_completely_outside_enclave(futex, sizeof(int))) {
  414. sgx_reset_ustack();
  415. return -EINVAL;
  416. }
  417. ms = sgx_alloc_on_ustack(sizeof(*ms));
  418. if (!ms) {
  419. sgx_reset_ustack();
  420. return -EPERM;
  421. }
  422. ms->ms_futex = futex;
  423. ms->ms_op = op;
  424. ms->ms_val = val;
  425. ms->ms_timeout_us = timeout_us;
  426. retval = sgx_ocall(OCALL_FUTEX, ms);
  427. sgx_reset_ustack();
  428. return retval;
  429. }
  430. int ocall_socketpair (int domain, int type, int protocol,
  431. int sockfds[2])
  432. {
  433. int retval = 0;
  434. ms_ocall_socketpair_t * ms;
  435. ms = sgx_alloc_on_ustack(sizeof(*ms));
  436. if (!ms) {
  437. sgx_reset_ustack();
  438. return -EPERM;
  439. }
  440. ms->ms_domain = domain;
  441. ms->ms_type = type;
  442. ms->ms_protocol = protocol;
  443. retval = sgx_ocall(OCALL_SOCKETPAIR, ms);
  444. if (!retval) {
  445. sockfds[0] = ms->ms_sockfds[0];
  446. sockfds[1] = ms->ms_sockfds[1];
  447. }
  448. sgx_reset_ustack();
  449. return retval;
  450. }
  451. int ocall_listen(int domain, int type, int protocol, int ipv6_v6only,
  452. struct sockaddr* addr, unsigned int* addrlen, struct sockopt* sockopt) {
  453. int retval = 0;
  454. unsigned int copied;
  455. unsigned int len = addrlen ? *addrlen : 0;
  456. ms_ocall_listen_t* ms;
  457. ms = sgx_alloc_on_ustack(sizeof(*ms));
  458. if (!ms) {
  459. sgx_reset_ustack();
  460. return -EPERM;
  461. }
  462. ms->ms_domain = domain;
  463. ms->ms_type = type;
  464. ms->ms_protocol = protocol;
  465. ms->ms_ipv6_v6only = ipv6_v6only;
  466. ms->ms_addrlen = len;
  467. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  468. if (addr && len && !ms->ms_addr) {
  469. sgx_reset_ustack();
  470. return -EPERM;
  471. }
  472. retval = sgx_ocall(OCALL_LISTEN, ms);
  473. if (retval >= 0) {
  474. if (addr && len) {
  475. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  476. if (!copied) {
  477. sgx_reset_ustack();
  478. return -EPERM;
  479. }
  480. *addrlen = copied;
  481. }
  482. if (sockopt) {
  483. *sockopt = ms->ms_sockopt;
  484. }
  485. }
  486. sgx_reset_ustack();
  487. return retval;
  488. }
  489. int ocall_accept (int sockfd, struct sockaddr * addr,
  490. unsigned int * addrlen, struct sockopt * sockopt)
  491. {
  492. int retval = 0;
  493. unsigned int copied;
  494. unsigned int len = addrlen ? *addrlen : 0;
  495. ms_ocall_accept_t * ms;
  496. ms = sgx_alloc_on_ustack(sizeof(*ms));
  497. if (!ms) {
  498. sgx_reset_ustack();
  499. return -EPERM;
  500. }
  501. ms->ms_sockfd = sockfd;
  502. ms->ms_addrlen = len;
  503. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  504. if (addr && len && !ms->ms_addr) {
  505. sgx_reset_ustack();
  506. return -EPERM;
  507. }
  508. retval = sgx_ocall(OCALL_ACCEPT, ms);
  509. if (retval >= 0) {
  510. if (addr && len) {
  511. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  512. if (!copied) {
  513. sgx_reset_ustack();
  514. return -EPERM;
  515. }
  516. *addrlen = copied;
  517. }
  518. if (sockopt) {
  519. *sockopt = ms->ms_sockopt;
  520. }
  521. }
  522. sgx_reset_ustack();
  523. return retval;
  524. }
  525. int ocall_connect(int domain, int type, int protocol, int ipv6_v6only,
  526. const struct sockaddr* addr, unsigned int addrlen,
  527. struct sockaddr* bind_addr, unsigned int* bind_addrlen,
  528. struct sockopt* sockopt) {
  529. int retval = 0;
  530. unsigned int copied;
  531. unsigned int bind_len = bind_addrlen ? *bind_addrlen : 0;
  532. ms_ocall_connect_t* ms;
  533. ms = sgx_alloc_on_ustack(sizeof(*ms));
  534. if (!ms) {
  535. sgx_reset_ustack();
  536. return -EPERM;
  537. }
  538. ms->ms_domain = domain;
  539. ms->ms_type = type;
  540. ms->ms_protocol = protocol;
  541. ms->ms_ipv6_v6only = ipv6_v6only;
  542. ms->ms_addrlen = addrlen;
  543. ms->ms_bind_addrlen = bind_len;
  544. ms->ms_addr = addr ? sgx_copy_to_ustack(addr, addrlen) : NULL;
  545. ms->ms_bind_addr = bind_addr ? sgx_copy_to_ustack(bind_addr, bind_len) : NULL;
  546. if ((addr && !ms->ms_addr) || (bind_addr && !ms->ms_bind_addr)) {
  547. sgx_reset_ustack();
  548. return -EPERM;
  549. }
  550. retval = sgx_ocall(OCALL_CONNECT, ms);
  551. if (retval >= 0) {
  552. if (bind_addr && bind_len) {
  553. copied = sgx_copy_to_enclave(bind_addr, bind_len, ms->ms_bind_addr, ms->ms_bind_addrlen);
  554. if (!copied) {
  555. sgx_reset_ustack();
  556. return -EPERM;
  557. }
  558. *bind_addrlen = copied;
  559. }
  560. if (sockopt) {
  561. *sockopt = ms->ms_sockopt;
  562. }
  563. }
  564. sgx_reset_ustack();
  565. return retval;
  566. }
  567. int ocall_recv (int sockfd, void * buf, unsigned int count,
  568. struct sockaddr * addr, unsigned int * addrlenptr,
  569. void * control, uint64_t * controllenptr)
  570. {
  571. int retval = 0;
  572. void * obuf = NULL;
  573. unsigned int copied;
  574. unsigned int addrlen = addrlenptr ? *addrlenptr : 0;
  575. uint64_t controllen = controllenptr ? *controllenptr : 0;
  576. ms_ocall_recv_t * ms;
  577. if ((count + addrlen + controllen) > MAX_UNTRUSTED_STACK_BUF) {
  578. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  579. if (IS_ERR(retval))
  580. return retval;
  581. }
  582. ms = sgx_alloc_on_ustack(sizeof(*ms));
  583. if (!ms) {
  584. retval = -EPERM;
  585. goto out;
  586. }
  587. ms->ms_sockfd = sockfd;
  588. ms->ms_count = count;
  589. ms->ms_addrlen = addrlen;
  590. ms->ms_addr = addr ? sgx_alloc_on_ustack(addrlen) : NULL;
  591. ms->ms_controllen = controllen;
  592. ms->ms_control = control ? sgx_alloc_on_ustack(controllen) : NULL;
  593. if (obuf)
  594. ms->ms_buf = obuf;
  595. else
  596. ms->ms_buf = sgx_alloc_on_ustack(count);
  597. if (!ms->ms_buf || (addr && !ms->ms_addr)) {
  598. retval = -EPERM;
  599. goto out;
  600. }
  601. retval = sgx_ocall(OCALL_RECV, ms);
  602. if (retval >= 0) {
  603. if (addr && addrlen) {
  604. copied = sgx_copy_to_enclave(addr, addrlen, ms->ms_addr, ms->ms_addrlen);
  605. if (!copied) {
  606. retval = -EPERM;
  607. goto out;
  608. }
  609. *addrlenptr = copied;
  610. }
  611. if (control && controllen) {
  612. copied = sgx_copy_to_enclave(control, controllen, ms->ms_control, ms->ms_controllen);
  613. if (!copied) {
  614. retval = -EPERM;
  615. goto out;
  616. }
  617. *controllenptr = copied;
  618. }
  619. if (retval > 0 && !sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
  620. retval = -EPERM;
  621. goto out;
  622. }
  623. }
  624. out:
  625. sgx_reset_ustack();
  626. if (obuf)
  627. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  628. return retval;
  629. }
  630. int ocall_send (int sockfd, const void * buf, unsigned int count,
  631. const struct sockaddr * addr, unsigned int addrlen,
  632. void * control, uint64_t controllen)
  633. {
  634. int retval = 0;
  635. void * obuf = NULL;
  636. ms_ocall_send_t * ms;
  637. if (sgx_is_completely_outside_enclave(buf, count)) {
  638. /* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
  639. obuf = (void*)buf;
  640. } else if (sgx_is_completely_within_enclave(buf, count)) {
  641. /* typical case of buf inside of enclave memory */
  642. if ((count + addrlen + controllen) > MAX_UNTRUSTED_STACK_BUF) {
  643. /* buf is too big and may overflow untrusted stack, so use untrusted heap */
  644. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  645. if (IS_ERR(retval))
  646. return retval;
  647. memcpy(obuf, buf, count);
  648. }
  649. } else {
  650. /* buf is partially in/out of enclave memory */
  651. return -EPERM;
  652. }
  653. ms = sgx_alloc_on_ustack(sizeof(*ms));
  654. if (!ms) {
  655. retval = -EPERM;
  656. goto out;
  657. }
  658. ms->ms_sockfd = sockfd;
  659. ms->ms_count = count;
  660. ms->ms_addrlen = addrlen;
  661. ms->ms_addr = addr ? sgx_copy_to_ustack(addr, addrlen) : NULL;
  662. ms->ms_controllen = controllen;
  663. ms->ms_control = control ? sgx_copy_to_ustack(control, controllen) : NULL;
  664. if (obuf)
  665. ms->ms_buf = obuf;
  666. else
  667. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  668. if (!ms->ms_buf || (addr && !ms->ms_addr)) {
  669. retval = -EPERM;
  670. goto out;
  671. }
  672. retval = sgx_ocall(OCALL_SEND, ms);
  673. out:
  674. sgx_reset_ustack();
  675. if (obuf && obuf != buf)
  676. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  677. return retval;
  678. }
  679. int ocall_setsockopt (int sockfd, int level, int optname,
  680. const void * optval, unsigned int optlen)
  681. {
  682. int retval = 0;
  683. ms_ocall_setsockopt_t * ms;
  684. ms = sgx_alloc_on_ustack(sizeof(*ms));
  685. if (!ms) {
  686. sgx_reset_ustack();
  687. return -EPERM;
  688. }
  689. ms->ms_sockfd = sockfd;
  690. ms->ms_level = level;
  691. ms->ms_optname = optname;
  692. ms->ms_optlen = 0;
  693. ms->ms_optval = NULL;
  694. if (optval && optlen > 0) {
  695. ms->ms_optlen = optlen;
  696. ms->ms_optval = sgx_copy_to_ustack(optval, optlen);
  697. if (!ms->ms_optval) {
  698. sgx_reset_ustack();
  699. return -EPERM;
  700. }
  701. }
  702. retval = sgx_ocall(OCALL_SETSOCKOPT, ms);
  703. sgx_reset_ustack();
  704. return retval;
  705. }
  706. int ocall_shutdown (int sockfd, int how)
  707. {
  708. int retval = 0;
  709. ms_ocall_shutdown_t * ms;
  710. ms = sgx_alloc_on_ustack(sizeof(*ms));
  711. if (!ms) {
  712. sgx_reset_ustack();
  713. return -EPERM;
  714. }
  715. ms->ms_sockfd = sockfd;
  716. ms->ms_how = how;
  717. retval = sgx_ocall(OCALL_SHUTDOWN, ms);
  718. sgx_reset_ustack();
  719. return retval;
  720. }
  721. int ocall_gettime (unsigned long * microsec)
  722. {
  723. int retval = 0;
  724. ms_ocall_gettime_t * ms;
  725. ms = sgx_alloc_on_ustack(sizeof(*ms));
  726. if (!ms) {
  727. sgx_reset_ustack();
  728. return -EPERM;
  729. }
  730. do {
  731. retval = sgx_ocall(OCALL_GETTIME, ms);
  732. } while(retval == -EINTR);
  733. if (!retval)
  734. *microsec = ms->ms_microsec;
  735. sgx_reset_ustack();
  736. return retval;
  737. }
  738. int ocall_sleep (unsigned long * microsec)
  739. {
  740. int retval = 0;
  741. ms_ocall_sleep_t * ms;
  742. ms = sgx_alloc_on_ustack(sizeof(*ms));
  743. if (!ms) {
  744. sgx_reset_ustack();
  745. return -EPERM;
  746. }
  747. ms->ms_microsec = microsec ? *microsec : 0;
  748. retval = sgx_ocall(OCALL_SLEEP, ms);
  749. if (microsec) {
  750. if (!retval)
  751. *microsec = 0;
  752. else if (retval == -EINTR)
  753. *microsec = ms->ms_microsec;
  754. }
  755. sgx_reset_ustack();
  756. return retval;
  757. }
  758. int ocall_poll(struct pollfd* fds, int nfds, int64_t timeout_us) {
  759. int retval = 0;
  760. unsigned int nfds_bytes = nfds * sizeof(struct pollfd);
  761. ms_ocall_poll_t * ms;
  762. ms = sgx_alloc_on_ustack(sizeof(*ms));
  763. if (!ms) {
  764. sgx_reset_ustack();
  765. return -EPERM;
  766. }
  767. ms->ms_nfds = nfds;
  768. ms->ms_timeout_us = timeout_us;
  769. ms->ms_fds = sgx_copy_to_ustack(fds, nfds_bytes);
  770. if (!ms->ms_fds) {
  771. sgx_reset_ustack();
  772. return -EPERM;
  773. }
  774. retval = sgx_ocall(OCALL_POLL, ms);
  775. if (retval >= 0) {
  776. if (!sgx_copy_to_enclave(fds, nfds_bytes, ms->ms_fds, nfds_bytes)) {
  777. sgx_reset_ustack();
  778. return -EPERM;
  779. }
  780. }
  781. sgx_reset_ustack();
  782. return retval;
  783. }
  784. int ocall_rename (const char * oldpath, const char * newpath)
  785. {
  786. int retval = 0;
  787. int oldlen = oldpath ? strlen(oldpath) + 1 : 0;
  788. int newlen = newpath ? strlen(newpath) + 1 : 0;
  789. ms_ocall_rename_t * ms;
  790. ms = sgx_alloc_on_ustack(sizeof(*ms));
  791. if (!ms) {
  792. sgx_reset_ustack();
  793. return -EPERM;
  794. }
  795. ms->ms_oldpath = sgx_copy_to_ustack(oldpath, oldlen);
  796. ms->ms_newpath = sgx_copy_to_ustack(newpath, newlen);
  797. if (!ms->ms_oldpath || !ms->ms_newpath) {
  798. sgx_reset_ustack();
  799. return -EPERM;
  800. }
  801. retval = sgx_ocall(OCALL_RENAME, ms);
  802. sgx_reset_ustack();
  803. return retval;
  804. }
  805. int ocall_delete (const char * pathname)
  806. {
  807. int retval = 0;
  808. int len = pathname ? strlen(pathname) + 1 : 0;
  809. ms_ocall_delete_t * ms;
  810. ms = sgx_alloc_on_ustack(sizeof(*ms));
  811. if (!ms) {
  812. sgx_reset_ustack();
  813. return -EPERM;
  814. }
  815. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  816. if (!ms->ms_pathname) {
  817. sgx_reset_ustack();
  818. return -EPERM;
  819. }
  820. retval = sgx_ocall(OCALL_DELETE, ms);
  821. sgx_reset_ustack();
  822. return retval;
  823. }
  824. int ocall_load_debug(const char * command)
  825. {
  826. int retval = 0;
  827. int len = strlen(command) + 1;
  828. const char * ms = sgx_copy_to_ustack(command, len);
  829. if (!ms) {
  830. sgx_reset_ustack();
  831. return -EPERM;
  832. }
  833. retval = sgx_ocall(OCALL_LOAD_DEBUG, (void *) ms);
  834. sgx_reset_ustack();
  835. return retval;
  836. }
  837. /*
  838. * ocall_get_attestation() triggers remote attestation in untrusted PAL (see sgx_platform.c:
  839. * retrieve_verified_quote()). If the OCall returns successfully, the function returns
  840. * attestation data required for platform verification (i.e., sgx_attestation_t). Except the
  841. * QE report, most data fields of the attestation need to be copied into the enclave.
  842. *
  843. * @spid: The client SPID registered with the IAS.
  844. * @subkey: SPID subscription key.
  845. * @linkable: Whether the SPID is linkable.
  846. * @report: Local attestation report for the quoting enclave.
  847. * @nonce: Randomly-generated nonce for freshness.
  848. * @attestation: Returns the attestation data (QE report, quote, IAS report, signature,
  849. * and certificate chain).
  850. */
  851. int ocall_get_attestation (const sgx_spid_t* spid, const char* subkey, bool linkable,
  852. const sgx_report_t* report, const sgx_quote_nonce_t* nonce,
  853. sgx_attestation_t* attestation) {
  854. ms_ocall_get_attestation_t * ms;
  855. int retval = -EPERM;
  856. ms = sgx_alloc_on_ustack(sizeof(*ms));
  857. if (!ms)
  858. goto reset;
  859. memcpy(&ms->ms_spid, spid, sizeof(sgx_spid_t));
  860. ms->ms_subkey = sgx_copy_to_ustack(subkey, strlen(subkey) + 1);
  861. memcpy(&ms->ms_report, report, sizeof(sgx_report_t));
  862. memcpy(&ms->ms_nonce, nonce, sizeof(sgx_quote_nonce_t));
  863. ms->ms_linkable = linkable;
  864. retval = sgx_ocall(OCALL_GET_ATTESTATION, ms);
  865. if (retval >= 0) {
  866. // First, try to copy the whole ms->ms_attestation inside
  867. if (!sgx_copy_to_enclave(attestation, sizeof(sgx_attestation_t), &ms->ms_attestation,
  868. sizeof(sgx_attestation_t))) {
  869. retval = -EACCES;
  870. goto reset;
  871. }
  872. // For calling ocall_munmap_untrusted, need to reset the untrusted stack
  873. sgx_reset_ustack();
  874. // Copy each field inside and free the untrusted buffers
  875. if (attestation->quote) {
  876. size_t len = attestation->quote_len;
  877. sgx_quote_t* quote = malloc(len);
  878. if (!sgx_copy_to_enclave(quote, len, attestation->quote, len))
  879. retval = -EACCES;
  880. ocall_munmap_untrusted(attestation->quote, ALLOC_ALIGN_UP(len));
  881. attestation->quote = quote;
  882. }
  883. if (attestation->ias_report) {
  884. size_t len = attestation->ias_report_len;
  885. char* ias_report = malloc(len + 1);
  886. if (!sgx_copy_to_enclave(ias_report, len, attestation->ias_report, len))
  887. retval = -EACCES;
  888. ocall_munmap_untrusted(attestation->ias_report, ALLOC_ALIGN_UP(len));
  889. ias_report[len] = 0; // Ensure null-ending
  890. attestation->ias_report = ias_report;
  891. }
  892. if (attestation->ias_sig) {
  893. size_t len = attestation->ias_sig_len;
  894. uint8_t* ias_sig = malloc(len);
  895. if (!sgx_copy_to_enclave(ias_sig, len, attestation->ias_sig, len))
  896. retval = -EACCES;
  897. ocall_munmap_untrusted(attestation->ias_sig, ALLOC_ALIGN_UP(len));
  898. attestation->ias_sig = ias_sig;
  899. }
  900. if (attestation->ias_certs) {
  901. size_t len = attestation->ias_certs_len;
  902. char* ias_certs = malloc(len + 1);
  903. if (!sgx_copy_to_enclave(ias_certs, len, attestation->ias_certs, len))
  904. retval = -EACCES;
  905. ocall_munmap_untrusted(attestation->ias_certs, ALLOC_ALIGN_UP(len));
  906. ias_certs[len] = 0; // Ensure null-ending
  907. attestation->ias_certs = ias_certs;
  908. }
  909. // At this point, no field should point to outside the enclave
  910. if (retval < 0) {
  911. if (attestation->quote) free(attestation->quote);
  912. if (attestation->ias_report) free(attestation->ias_report);
  913. if (attestation->ias_sig) free(attestation->ias_sig);
  914. if (attestation->ias_certs) free(attestation->ias_certs);
  915. }
  916. goto out;
  917. }
  918. reset:
  919. sgx_reset_ustack();
  920. out:
  921. return retval;
  922. }
  923. int ocall_eventfd (unsigned int initval, int flags)
  924. {
  925. int retval = 0;
  926. ms_ocall_eventfd_t * ms;
  927. ms = sgx_alloc_on_ustack(sizeof(*ms));
  928. if (!ms) {
  929. sgx_reset_ustack();
  930. return -EPERM;
  931. }
  932. ms->ms_initval = initval;
  933. ms->ms_flags = flags;
  934. retval = sgx_ocall(OCALL_EVENTFD, ms);
  935. sgx_reset_ustack();
  936. return retval;
  937. }