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.
  28. while (true) {
  29. sgx_ocall(OCALL_EXIT, ms);
  30. }
  31. }
  32. int ocall_mmap_untrusted (int fd, uint64_t offset,
  33. uint64_t size, unsigned short prot,
  34. void ** mem)
  35. {
  36. int retval = 0;
  37. ms_ocall_mmap_untrusted_t * ms;
  38. ms = sgx_alloc_on_ustack(sizeof(*ms));
  39. if (!ms) {
  40. sgx_reset_ustack();
  41. return -EPERM;
  42. }
  43. ms->ms_fd = fd;
  44. ms->ms_offset = offset;
  45. ms->ms_size = size;
  46. ms->ms_prot = prot;
  47. retval = sgx_ocall(OCALL_MMAP_UNTRUSTED, ms);
  48. if (!retval) {
  49. if (!sgx_copy_ptr_to_enclave(mem, ms->ms_mem, size)) {
  50. sgx_reset_ustack();
  51. return -EPERM;
  52. }
  53. }
  54. sgx_reset_ustack();
  55. return retval;
  56. }
  57. int ocall_munmap_untrusted (const void * mem, uint64_t size)
  58. {
  59. int retval = 0;
  60. ms_ocall_munmap_untrusted_t * ms;
  61. if (!sgx_is_completely_outside_enclave(mem, size)) {
  62. sgx_reset_ustack();
  63. return -EINVAL;
  64. }
  65. ms = sgx_alloc_on_ustack(sizeof(*ms));
  66. if (!ms) {
  67. sgx_reset_ustack();
  68. return -EPERM;
  69. }
  70. ms->ms_mem = mem;
  71. ms->ms_size = size;
  72. retval = sgx_ocall(OCALL_MUNMAP_UNTRUSTED, ms);
  73. sgx_reset_ustack();
  74. return retval;
  75. }
  76. int ocall_cpuid (unsigned int leaf, unsigned int subleaf,
  77. unsigned int values[4])
  78. {
  79. int retval = 0;
  80. ms_ocall_cpuid_t * ms;
  81. ms = sgx_alloc_on_ustack(sizeof(*ms));
  82. if (!ms) {
  83. sgx_reset_ustack();
  84. return -EPERM;
  85. }
  86. ms->ms_leaf = leaf;
  87. ms->ms_subleaf = subleaf;
  88. retval = sgx_ocall(OCALL_CPUID, ms);
  89. if (!retval) {
  90. values[0] = ms->ms_values[0];
  91. values[1] = ms->ms_values[1];
  92. values[2] = ms->ms_values[2];
  93. values[3] = ms->ms_values[3];
  94. }
  95. sgx_reset_ustack();
  96. return retval;
  97. }
  98. int ocall_open (const char * pathname, int flags, unsigned short mode)
  99. {
  100. int retval = 0;
  101. int len = pathname ? strlen(pathname) + 1 : 0;
  102. ms_ocall_open_t * ms;
  103. ms = sgx_alloc_on_ustack(sizeof(*ms));
  104. if (!ms) {
  105. sgx_reset_ustack();
  106. return -EPERM;
  107. }
  108. ms->ms_flags = flags;
  109. ms->ms_mode = mode;
  110. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  111. if (!ms->ms_pathname) {
  112. sgx_reset_ustack();
  113. return -EPERM;
  114. }
  115. retval = sgx_ocall(OCALL_OPEN, ms);
  116. sgx_reset_ustack();
  117. return retval;
  118. }
  119. int ocall_close (int fd)
  120. {
  121. int retval = 0;
  122. ms_ocall_close_t *ms;
  123. ms = sgx_alloc_on_ustack(sizeof(*ms));
  124. if (!ms) {
  125. sgx_reset_ustack();
  126. return -EPERM;
  127. }
  128. ms->ms_fd = fd;
  129. retval = sgx_ocall(OCALL_CLOSE, ms);
  130. sgx_reset_ustack();
  131. return retval;
  132. }
  133. int ocall_read (int fd, void * buf, unsigned int count)
  134. {
  135. int retval = 0;
  136. void * obuf = NULL;
  137. ms_ocall_read_t * ms;
  138. if (count > MAX_UNTRUSTED_STACK_BUF) {
  139. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  140. if (IS_ERR(retval))
  141. return retval;
  142. }
  143. ms = sgx_alloc_on_ustack(sizeof(*ms));
  144. if (!ms) {
  145. retval = -EPERM;
  146. goto out;
  147. }
  148. ms->ms_fd = fd;
  149. ms->ms_count = count;
  150. if (obuf)
  151. ms->ms_buf = obuf;
  152. else
  153. ms->ms_buf = sgx_alloc_on_ustack(count);
  154. if (!ms->ms_buf) {
  155. retval = -EPERM;
  156. goto out;
  157. }
  158. retval = sgx_ocall(OCALL_READ, ms);
  159. if (retval > 0) {
  160. if (!sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
  161. retval = -EPERM;
  162. goto out;
  163. }
  164. }
  165. out:
  166. sgx_reset_ustack();
  167. if (obuf)
  168. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  169. return retval;
  170. }
  171. int ocall_write (int fd, const void * buf, unsigned int count)
  172. {
  173. int retval = 0;
  174. void * obuf = NULL;
  175. ms_ocall_write_t * ms;
  176. if (sgx_is_completely_outside_enclave(buf, count)) {
  177. /* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
  178. obuf = (void*)buf;
  179. } else if (sgx_is_completely_within_enclave(buf, count)) {
  180. /* typical case of buf inside of enclave memory */
  181. if (count > MAX_UNTRUSTED_STACK_BUF) {
  182. /* buf is too big and may overflow untrusted stack, so use untrusted heap */
  183. retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
  184. if (IS_ERR(retval))
  185. return retval;
  186. memcpy(obuf, buf, count);
  187. }
  188. } else {
  189. /* buf is partially in/out of enclave memory */
  190. return -EPERM;
  191. }
  192. ms = sgx_alloc_on_ustack(sizeof(*ms));
  193. if (!ms) {
  194. retval = -EPERM;
  195. goto out;
  196. }
  197. ms->ms_fd = fd;
  198. ms->ms_count = count;
  199. if (obuf)
  200. ms->ms_buf = obuf;
  201. else
  202. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  203. if (!ms->ms_buf) {
  204. retval = -EPERM;
  205. goto out;
  206. }
  207. retval = sgx_ocall(OCALL_WRITE, ms);
  208. out:
  209. sgx_reset_ustack();
  210. if (obuf && obuf != buf)
  211. ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
  212. return retval;
  213. }
  214. int ocall_fstat (int fd, struct stat * buf)
  215. {
  216. int retval = 0;
  217. ms_ocall_fstat_t * ms;
  218. ms = sgx_alloc_on_ustack(sizeof(*ms));
  219. if (!ms) {
  220. sgx_reset_ustack();
  221. return -EPERM;
  222. }
  223. ms->ms_fd = fd;
  224. retval = sgx_ocall(OCALL_FSTAT, ms);
  225. if (!retval)
  226. memcpy(buf, &ms->ms_stat, sizeof(struct stat));
  227. sgx_reset_ustack();
  228. return retval;
  229. }
  230. int ocall_fionread (int fd)
  231. {
  232. int retval = 0;
  233. ms_ocall_fionread_t * ms;
  234. ms = sgx_alloc_on_ustack(sizeof(*ms));
  235. if (!ms) {
  236. sgx_reset_ustack();
  237. return -EPERM;
  238. }
  239. ms->ms_fd = fd;
  240. retval = sgx_ocall(OCALL_FIONREAD, ms);
  241. sgx_reset_ustack();
  242. return retval;
  243. }
  244. int ocall_fsetnonblock (int fd, int nonblocking)
  245. {
  246. int retval = 0;
  247. ms_ocall_fsetnonblock_t * ms;
  248. ms = sgx_alloc_on_ustack(sizeof(*ms));
  249. if (!ms) {
  250. sgx_reset_ustack();
  251. return -EPERM;
  252. }
  253. ms->ms_fd = fd;
  254. ms->ms_nonblocking = nonblocking;
  255. retval = sgx_ocall(OCALL_FSETNONBLOCK, ms);
  256. sgx_reset_ustack();
  257. return retval;
  258. }
  259. int ocall_fchmod (int fd, unsigned short mode)
  260. {
  261. int retval = 0;
  262. ms_ocall_fchmod_t * ms;
  263. ms = sgx_alloc_on_ustack(sizeof(*ms));
  264. if (!ms) {
  265. sgx_reset_ustack();
  266. return -EPERM;
  267. }
  268. ms->ms_fd = fd;
  269. ms->ms_mode = mode;
  270. retval = sgx_ocall(OCALL_FCHMOD, ms);
  271. sgx_reset_ustack();
  272. return retval;
  273. }
  274. int ocall_fsync (int fd)
  275. {
  276. int retval = 0;
  277. ms_ocall_fsync_t * ms;
  278. ms = sgx_alloc_on_ustack(sizeof(*ms));
  279. if (!ms) {
  280. sgx_reset_ustack();
  281. return -EPERM;
  282. }
  283. ms->ms_fd = fd;
  284. retval = sgx_ocall(OCALL_FSYNC, ms);
  285. sgx_reset_ustack();
  286. return retval;
  287. }
  288. int ocall_ftruncate (int fd, uint64_t length)
  289. {
  290. int retval = 0;
  291. ms_ocall_ftruncate_t * ms;
  292. ms = sgx_alloc_on_ustack(sizeof(*ms));
  293. if (!ms) {
  294. sgx_reset_ustack();
  295. return -EPERM;
  296. }
  297. ms->ms_fd = fd;
  298. ms->ms_length = length;
  299. retval = sgx_ocall(OCALL_FTRUNCATE, ms);
  300. sgx_reset_ustack();
  301. return retval;
  302. }
  303. int ocall_lseek(int fd, uint64_t offset, int whence) {
  304. int retval = 0;
  305. ms_ocall_lseek_t* ms;
  306. ms = sgx_alloc_on_ustack(sizeof(*ms));
  307. if (!ms) {
  308. sgx_reset_ustack();
  309. return -EPERM;
  310. }
  311. ms->ms_fd = fd;
  312. ms->ms_offset = offset;
  313. ms->ms_whence = whence;
  314. retval = sgx_ocall(OCALL_LSEEK, ms);
  315. sgx_reset_ustack();
  316. return retval;
  317. }
  318. int ocall_mkdir (const char * pathname, unsigned short mode)
  319. {
  320. int retval = 0;
  321. int len = pathname ? strlen(pathname) + 1 : 0;
  322. ms_ocall_mkdir_t * ms;
  323. ms = sgx_alloc_on_ustack(sizeof(*ms));
  324. if (!ms) {
  325. sgx_reset_ustack();
  326. return -EPERM;
  327. }
  328. ms->ms_mode = mode;
  329. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  330. if (!ms->ms_pathname) {
  331. sgx_reset_ustack();
  332. return -EPERM;
  333. }
  334. retval = sgx_ocall(OCALL_MKDIR, ms);
  335. sgx_reset_ustack();
  336. return retval;
  337. }
  338. int ocall_getdents (int fd, struct linux_dirent64 * dirp, unsigned int size)
  339. {
  340. int retval = 0;
  341. ms_ocall_getdents_t * ms;
  342. ms = sgx_alloc_on_ustack(sizeof(*ms));
  343. if (!ms) {
  344. sgx_reset_ustack();
  345. return -EPERM;
  346. }
  347. ms->ms_fd = fd;
  348. ms->ms_size = size;
  349. ms->ms_dirp = sgx_alloc_on_ustack(size);
  350. if (!ms->ms_dirp) {
  351. sgx_reset_ustack();
  352. return -EPERM;
  353. }
  354. retval = sgx_ocall(OCALL_GETDENTS, ms);
  355. if (retval > 0) {
  356. if (!sgx_copy_to_enclave(dirp, size, ms->ms_dirp, retval)) {
  357. sgx_reset_ustack();
  358. return -EPERM;
  359. }
  360. }
  361. sgx_reset_ustack();
  362. return retval;
  363. }
  364. int ocall_resume_thread (void * tcs)
  365. {
  366. return sgx_ocall(OCALL_RESUME_THREAD, tcs);
  367. }
  368. int ocall_clone_thread (void)
  369. {
  370. void* dummy = NULL;
  371. return sgx_ocall(OCALL_CLONE_THREAD, dummy);
  372. }
  373. int ocall_create_process(const char* uri, int nargs, const char** args, int procfds[3],
  374. unsigned int* pid) {
  375. int retval = 0;
  376. int ulen = uri ? strlen(uri) + 1 : 0;
  377. ms_ocall_create_process_t * ms;
  378. ms = sgx_alloc_on_ustack(sizeof(*ms) + nargs * sizeof(char *));
  379. if (!ms) {
  380. sgx_reset_ustack();
  381. return -EPERM;
  382. }
  383. ms->ms_uri = uri ? sgx_copy_to_ustack(uri, ulen) : NULL;
  384. if (uri && !ms->ms_uri) {
  385. sgx_reset_ustack();
  386. return -EPERM;
  387. }
  388. ms->ms_nargs = nargs;
  389. for (int i = 0 ; i < nargs ; i++) {
  390. int len = args[i] ? strlen(args[i]) + 1 : 0;
  391. ms->ms_args[i] = args[i] ? sgx_copy_to_ustack(args[i], len) : NULL;
  392. if (args[i] && !ms->ms_args[i]) {
  393. sgx_reset_ustack();
  394. return -EPERM;
  395. }
  396. }
  397. retval = sgx_ocall(OCALL_CREATE_PROCESS, ms);
  398. if (!retval) {
  399. if (pid)
  400. *pid = ms->ms_pid;
  401. procfds[0] = ms->ms_proc_fds[0];
  402. procfds[1] = ms->ms_proc_fds[1];
  403. procfds[2] = ms->ms_proc_fds[2];
  404. }
  405. sgx_reset_ustack();
  406. return retval;
  407. }
  408. int ocall_futex(int* futex, int op, int val, int64_t timeout_us) {
  409. int retval = 0;
  410. ms_ocall_futex_t * ms;
  411. if (!sgx_is_completely_outside_enclave(futex, sizeof(int))) {
  412. sgx_reset_ustack();
  413. return -EINVAL;
  414. }
  415. ms = sgx_alloc_on_ustack(sizeof(*ms));
  416. if (!ms) {
  417. sgx_reset_ustack();
  418. return -EPERM;
  419. }
  420. ms->ms_futex = futex;
  421. ms->ms_op = op;
  422. ms->ms_val = val;
  423. ms->ms_timeout_us = timeout_us;
  424. retval = sgx_ocall(OCALL_FUTEX, ms);
  425. sgx_reset_ustack();
  426. return retval;
  427. }
  428. int ocall_socketpair (int domain, int type, int protocol,
  429. int sockfds[2])
  430. {
  431. int retval = 0;
  432. ms_ocall_socketpair_t * ms;
  433. ms = sgx_alloc_on_ustack(sizeof(*ms));
  434. if (!ms) {
  435. sgx_reset_ustack();
  436. return -EPERM;
  437. }
  438. ms->ms_domain = domain;
  439. ms->ms_type = type;
  440. ms->ms_protocol = protocol;
  441. retval = sgx_ocall(OCALL_SOCKETPAIR, ms);
  442. if (!retval) {
  443. sockfds[0] = ms->ms_sockfds[0];
  444. sockfds[1] = ms->ms_sockfds[1];
  445. }
  446. sgx_reset_ustack();
  447. return retval;
  448. }
  449. int ocall_listen (int domain, int type, int protocol,
  450. struct sockaddr * addr, unsigned int * addrlen,
  451. struct sockopt * sockopt)
  452. {
  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_addrlen = len;
  466. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  467. if (addr && len && !ms->ms_addr) {
  468. sgx_reset_ustack();
  469. return -EPERM;
  470. }
  471. retval = sgx_ocall(OCALL_LISTEN, ms);
  472. if (retval >= 0) {
  473. if (addr && len) {
  474. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  475. if (!copied) {
  476. sgx_reset_ustack();
  477. return -EPERM;
  478. }
  479. *addrlen = copied;
  480. }
  481. if (sockopt) {
  482. *sockopt = ms->ms_sockopt;
  483. }
  484. }
  485. sgx_reset_ustack();
  486. return retval;
  487. }
  488. int ocall_accept (int sockfd, struct sockaddr * addr,
  489. unsigned int * addrlen, struct sockopt * sockopt)
  490. {
  491. int retval = 0;
  492. unsigned int copied;
  493. unsigned int len = addrlen ? *addrlen : 0;
  494. ms_ocall_accept_t * ms;
  495. ms = sgx_alloc_on_ustack(sizeof(*ms));
  496. if (!ms) {
  497. sgx_reset_ustack();
  498. return -EPERM;
  499. }
  500. ms->ms_sockfd = sockfd;
  501. ms->ms_addrlen = len;
  502. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  503. if (addr && len && !ms->ms_addr) {
  504. sgx_reset_ustack();
  505. return -EPERM;
  506. }
  507. retval = sgx_ocall(OCALL_ACCEPT, ms);
  508. if (retval >= 0) {
  509. if (addr && len) {
  510. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  511. if (!copied) {
  512. sgx_reset_ustack();
  513. return -EPERM;
  514. }
  515. *addrlen = copied;
  516. }
  517. if (sockopt) {
  518. *sockopt = ms->ms_sockopt;
  519. }
  520. }
  521. sgx_reset_ustack();
  522. return retval;
  523. }
  524. int ocall_connect (int domain, int type, int protocol,
  525. const struct sockaddr * addr,
  526. unsigned int addrlen,
  527. struct sockaddr * bind_addr,
  528. unsigned int * bind_addrlen, struct sockopt * sockopt)
  529. {
  530. int retval = 0;
  531. unsigned int copied;
  532. unsigned int bind_len = bind_addrlen ? *bind_addrlen : 0;
  533. ms_ocall_connect_t * ms;
  534. ms = sgx_alloc_on_ustack(sizeof(*ms));
  535. if (!ms) {
  536. sgx_reset_ustack();
  537. return -EPERM;
  538. }
  539. ms->ms_domain = domain;
  540. ms->ms_type = type;
  541. ms->ms_protocol = protocol;
  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. }