enclave_ocalls.c 29 KB

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