enclave_ocalls.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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 (count > PRESET_PAGESIZE) {
  214. retval = ocall_alloc_untrusted(ALLOC_ALIGNUP(count), &obuf);
  215. if (IS_ERR(retval))
  216. return retval;
  217. }
  218. ms = sgx_alloc_on_ustack(sizeof(*ms));
  219. if (!ms) {
  220. retval = -EPERM;
  221. goto out;
  222. }
  223. ms->ms_fd = fd;
  224. ms->ms_count = count;
  225. if (obuf) {
  226. ms->ms_buf = obuf;
  227. memcpy(obuf, buf, count);
  228. } else {
  229. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  230. }
  231. if (!ms->ms_buf) {
  232. retval = -EPERM;
  233. goto out;
  234. }
  235. retval = sgx_ocall(OCALL_WRITE, ms);
  236. out:
  237. sgx_reset_ustack();
  238. if (obuf)
  239. ocall_unmap_untrusted(obuf, ALLOC_ALIGNUP(count));
  240. return retval;
  241. }
  242. int ocall_fstat (int fd, struct stat * buf)
  243. {
  244. int retval = 0;
  245. ms_ocall_fstat_t * ms;
  246. ms = sgx_alloc_on_ustack(sizeof(*ms));
  247. if (!ms) {
  248. sgx_reset_ustack();
  249. return -EPERM;
  250. }
  251. ms->ms_fd = fd;
  252. retval = sgx_ocall(OCALL_FSTAT, ms);
  253. if (!retval)
  254. memcpy(buf, &ms->ms_stat, sizeof(struct stat));
  255. sgx_reset_ustack();
  256. return retval;
  257. }
  258. int ocall_fionread (int fd)
  259. {
  260. int retval = 0;
  261. ms_ocall_fionread_t * ms;
  262. ms = sgx_alloc_on_ustack(sizeof(*ms));
  263. if (!ms) {
  264. sgx_reset_ustack();
  265. return -EPERM;
  266. }
  267. ms->ms_fd = fd;
  268. retval = sgx_ocall(OCALL_FIONREAD, ms);
  269. sgx_reset_ustack();
  270. return retval;
  271. }
  272. int ocall_fsetnonblock (int fd, int nonblocking)
  273. {
  274. int retval = 0;
  275. ms_ocall_fsetnonblock_t * ms;
  276. ms = sgx_alloc_on_ustack(sizeof(*ms));
  277. if (!ms) {
  278. sgx_reset_ustack();
  279. return -EPERM;
  280. }
  281. ms->ms_fd = fd;
  282. ms->ms_nonblocking = nonblocking;
  283. retval = sgx_ocall(OCALL_FSETNONBLOCK, ms);
  284. sgx_reset_ustack();
  285. return retval;
  286. }
  287. int ocall_fchmod (int fd, unsigned short mode)
  288. {
  289. int retval = 0;
  290. ms_ocall_fchmod_t * ms;
  291. ms = sgx_alloc_on_ustack(sizeof(*ms));
  292. if (!ms) {
  293. sgx_reset_ustack();
  294. return -EPERM;
  295. }
  296. ms->ms_fd = fd;
  297. ms->ms_mode = mode;
  298. retval = sgx_ocall(OCALL_FCHMOD, ms);
  299. sgx_reset_ustack();
  300. return retval;
  301. }
  302. int ocall_fsync (int fd)
  303. {
  304. int retval = 0;
  305. ms_ocall_fsync_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. retval = sgx_ocall(OCALL_FSYNC, ms);
  313. sgx_reset_ustack();
  314. return retval;
  315. }
  316. int ocall_ftruncate (int fd, uint64_t length)
  317. {
  318. int retval = 0;
  319. ms_ocall_ftruncate_t * ms;
  320. ms = sgx_alloc_on_ustack(sizeof(*ms));
  321. if (!ms) {
  322. sgx_reset_ustack();
  323. return -EPERM;
  324. }
  325. ms->ms_fd = fd;
  326. ms->ms_length = length;
  327. retval = sgx_ocall(OCALL_FTRUNCATE, ms);
  328. sgx_reset_ustack();
  329. return retval;
  330. }
  331. int ocall_mkdir (const char * pathname, unsigned short mode)
  332. {
  333. int retval = 0;
  334. int len = pathname ? strlen(pathname) + 1 : 0;
  335. ms_ocall_mkdir_t * ms;
  336. ms = sgx_alloc_on_ustack(sizeof(*ms));
  337. if (!ms) {
  338. sgx_reset_ustack();
  339. return -EPERM;
  340. }
  341. ms->ms_mode = mode;
  342. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  343. if (!ms->ms_pathname) {
  344. sgx_reset_ustack();
  345. return -EPERM;
  346. }
  347. retval = sgx_ocall(OCALL_MKDIR, ms);
  348. sgx_reset_ustack();
  349. return retval;
  350. }
  351. int ocall_getdents (int fd, struct linux_dirent64 * dirp, unsigned int size)
  352. {
  353. int retval = 0;
  354. ms_ocall_getdents_t * ms;
  355. ms = sgx_alloc_on_ustack(sizeof(*ms));
  356. if (!ms) {
  357. sgx_reset_ustack();
  358. return -EPERM;
  359. }
  360. ms->ms_fd = fd;
  361. ms->ms_size = size;
  362. ms->ms_dirp = sgx_alloc_on_ustack(size);
  363. if (!ms->ms_dirp) {
  364. sgx_reset_ustack();
  365. return -EPERM;
  366. }
  367. retval = sgx_ocall(OCALL_GETDENTS, ms);
  368. if (retval > 0) {
  369. if (!sgx_copy_to_enclave(dirp, size, ms->ms_dirp, retval)) {
  370. sgx_reset_ustack();
  371. return -EPERM;
  372. }
  373. }
  374. sgx_reset_ustack();
  375. return retval;
  376. }
  377. int ocall_wake_thread (void * tcs)
  378. {
  379. return sgx_ocall(OCALL_WAKE_THREAD, tcs);
  380. }
  381. int ocall_create_process(const char* uri, int nargs, const char** args, int procfds[3],
  382. unsigned int* pid) {
  383. int retval = 0;
  384. int ulen = uri ? strlen(uri) + 1 : 0;
  385. ms_ocall_create_process_t * ms;
  386. ms = sgx_alloc_on_ustack(sizeof(*ms) + nargs * sizeof(char *));
  387. if (!ms) {
  388. sgx_reset_ustack();
  389. return -EPERM;
  390. }
  391. ms->ms_uri = uri ? sgx_copy_to_ustack(uri, ulen) : NULL;
  392. if (uri && !ms->ms_uri) {
  393. sgx_reset_ustack();
  394. return -EPERM;
  395. }
  396. ms->ms_nargs = nargs;
  397. for (int i = 0 ; i < nargs ; i++) {
  398. int len = args[i] ? strlen(args[i]) + 1 : 0;
  399. ms->ms_args[i] = args[i] ? sgx_copy_to_ustack(args[i], len) : NULL;
  400. if (args[i] && !ms->ms_args[i]) {
  401. sgx_reset_ustack();
  402. return -EPERM;
  403. }
  404. }
  405. retval = sgx_ocall(OCALL_CREATE_PROCESS, ms);
  406. if (!retval) {
  407. if (pid)
  408. *pid = ms->ms_pid;
  409. procfds[0] = ms->ms_proc_fds[0];
  410. procfds[1] = ms->ms_proc_fds[1];
  411. procfds[2] = ms->ms_proc_fds[2];
  412. }
  413. sgx_reset_ustack();
  414. return retval;
  415. }
  416. int ocall_futex(int* futex, int op, int val, int64_t timeout_us) {
  417. int retval = 0;
  418. ms_ocall_futex_t * ms;
  419. if (!sgx_is_completely_outside_enclave(futex, sizeof(int))) {
  420. sgx_reset_ustack();
  421. return -EINVAL;
  422. }
  423. ms = sgx_alloc_on_ustack(sizeof(*ms));
  424. if (!ms) {
  425. sgx_reset_ustack();
  426. return -EPERM;
  427. }
  428. ms->ms_futex = futex;
  429. ms->ms_op = op;
  430. ms->ms_val = val;
  431. ms->ms_timeout_us = timeout_us;
  432. retval = sgx_ocall(OCALL_FUTEX, ms);
  433. sgx_reset_ustack();
  434. return retval;
  435. }
  436. int ocall_socketpair (int domain, int type, int protocol,
  437. int sockfds[2])
  438. {
  439. int retval = 0;
  440. ms_ocall_socketpair_t * ms;
  441. ms = sgx_alloc_on_ustack(sizeof(*ms));
  442. if (!ms) {
  443. sgx_reset_ustack();
  444. return -EPERM;
  445. }
  446. ms->ms_domain = domain;
  447. ms->ms_type = type;
  448. ms->ms_protocol = protocol;
  449. retval = sgx_ocall(OCALL_SOCKETPAIR, ms);
  450. if (!retval) {
  451. sockfds[0] = ms->ms_sockfds[0];
  452. sockfds[1] = ms->ms_sockfds[1];
  453. }
  454. sgx_reset_ustack();
  455. return retval;
  456. }
  457. int ocall_sock_listen (int domain, int type, int protocol,
  458. struct sockaddr * addr, unsigned int * addrlen,
  459. struct sockopt * sockopt)
  460. {
  461. int retval = 0;
  462. unsigned int copied;
  463. unsigned int len = addrlen ? *addrlen : 0;
  464. ms_ocall_sock_listen_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. ms->ms_addrlen = len;
  474. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  475. if (addr && len && !ms->ms_addr) {
  476. sgx_reset_ustack();
  477. return -EPERM;
  478. }
  479. retval = sgx_ocall(OCALL_SOCK_LISTEN, ms);
  480. if (retval >= 0) {
  481. if (addr && len) {
  482. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  483. if (!copied) {
  484. sgx_reset_ustack();
  485. return -EPERM;
  486. }
  487. *addrlen = copied;
  488. }
  489. if (sockopt) {
  490. *sockopt = ms->ms_sockopt;
  491. }
  492. }
  493. sgx_reset_ustack();
  494. return retval;
  495. }
  496. int ocall_sock_accept (int sockfd, struct sockaddr * addr,
  497. unsigned int * addrlen, struct sockopt * sockopt)
  498. {
  499. int retval = 0;
  500. unsigned int copied;
  501. unsigned int len = addrlen ? *addrlen : 0;
  502. ms_ocall_sock_accept_t * ms;
  503. ms = sgx_alloc_on_ustack(sizeof(*ms));
  504. if (!ms) {
  505. sgx_reset_ustack();
  506. return -EPERM;
  507. }
  508. ms->ms_sockfd = sockfd;
  509. ms->ms_addrlen = len;
  510. ms->ms_addr = (addr && len) ? sgx_copy_to_ustack(addr, len) : NULL;
  511. if (addr && len && !ms->ms_addr) {
  512. sgx_reset_ustack();
  513. return -EPERM;
  514. }
  515. retval = sgx_ocall(OCALL_SOCK_ACCEPT, ms);
  516. if (retval >= 0) {
  517. if (addr && len) {
  518. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  519. if (!copied) {
  520. sgx_reset_ustack();
  521. return -EPERM;
  522. }
  523. *addrlen = copied;
  524. }
  525. if (sockopt) {
  526. *sockopt = ms->ms_sockopt;
  527. }
  528. }
  529. sgx_reset_ustack();
  530. return retval;
  531. }
  532. int ocall_sock_connect (int domain, int type, int protocol,
  533. const struct sockaddr * addr,
  534. unsigned int addrlen,
  535. struct sockaddr * bind_addr,
  536. unsigned int * bind_addrlen, struct sockopt * sockopt)
  537. {
  538. int retval = 0;
  539. unsigned int copied;
  540. unsigned int bind_len = bind_addrlen ? *bind_addrlen : 0;
  541. ms_ocall_sock_connect_t * ms;
  542. ms = sgx_alloc_on_ustack(sizeof(*ms));
  543. if (!ms) {
  544. sgx_reset_ustack();
  545. return -EPERM;
  546. }
  547. ms->ms_domain = domain;
  548. ms->ms_type = type;
  549. ms->ms_protocol = protocol;
  550. ms->ms_addrlen = addrlen;
  551. ms->ms_bind_addrlen = bind_len;
  552. ms->ms_addr = addr ? sgx_copy_to_ustack(addr, addrlen) : NULL;
  553. ms->ms_bind_addr = bind_addr ? sgx_copy_to_ustack(bind_addr, bind_len) : NULL;
  554. if ((addr && !ms->ms_addr) || (bind_addr && !ms->ms_bind_addr)) {
  555. sgx_reset_ustack();
  556. return -EPERM;
  557. }
  558. retval = sgx_ocall(OCALL_SOCK_CONNECT, ms);
  559. if (retval >= 0) {
  560. if (bind_addr && bind_len) {
  561. copied = sgx_copy_to_enclave(bind_addr, bind_len, ms->ms_bind_addr, ms->ms_bind_addrlen);
  562. if (!copied) {
  563. sgx_reset_ustack();
  564. return -EPERM;
  565. }
  566. *bind_addrlen = copied;
  567. }
  568. if (sockopt) {
  569. *sockopt = ms->ms_sockopt;
  570. }
  571. }
  572. sgx_reset_ustack();
  573. return retval;
  574. }
  575. int ocall_sock_recv (int sockfd, void * buf, unsigned int count,
  576. struct sockaddr * addr, unsigned int * addrlen)
  577. {
  578. int retval = 0;
  579. void * obuf = NULL;
  580. unsigned int copied;
  581. unsigned int len = addrlen ? *addrlen : 0;
  582. ms_ocall_sock_recv_t * ms;
  583. if ((count + len) > PRESET_PAGESIZE) {
  584. retval = ocall_alloc_untrusted(ALLOC_ALIGNUP(count), &obuf);
  585. if (IS_ERR(retval))
  586. return retval;
  587. }
  588. ms = sgx_alloc_on_ustack(sizeof(*ms));
  589. if (!ms) {
  590. retval = -EPERM;
  591. goto out;
  592. }
  593. ms->ms_sockfd = sockfd;
  594. ms->ms_count = count;
  595. ms->ms_addrlen = len;
  596. ms->ms_addr = addr ? sgx_alloc_on_ustack(len) : NULL;
  597. if (obuf)
  598. ms->ms_buf = obuf;
  599. else
  600. ms->ms_buf = sgx_alloc_on_ustack(count);
  601. if (!ms->ms_buf || (addr && !ms->ms_addr)) {
  602. retval = -EPERM;
  603. goto out;
  604. }
  605. retval = sgx_ocall(OCALL_SOCK_RECV, ms);
  606. if (retval >= 0) {
  607. if (addr && len) {
  608. copied = sgx_copy_to_enclave(addr, len, ms->ms_addr, ms->ms_addrlen);
  609. if (!copied) {
  610. retval = -EPERM;
  611. goto out;
  612. }
  613. *addrlen = copied;
  614. }
  615. if (!sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
  616. retval = -EPERM;
  617. goto out;
  618. }
  619. }
  620. out:
  621. sgx_reset_ustack();
  622. if (obuf)
  623. ocall_unmap_untrusted(obuf, ALLOC_ALIGNUP(count));
  624. return retval;
  625. }
  626. int ocall_sock_send (int sockfd, const void * buf, unsigned int count,
  627. const struct sockaddr * addr, unsigned int addrlen)
  628. {
  629. int retval = 0;
  630. void * obuf = NULL;
  631. ms_ocall_sock_send_t * ms;
  632. if ((count + addrlen) > PRESET_PAGESIZE) {
  633. retval = ocall_alloc_untrusted(ALLOC_ALIGNUP(count), &obuf);
  634. if (IS_ERR(retval))
  635. return retval;
  636. }
  637. ms = sgx_alloc_on_ustack(sizeof(*ms));
  638. if (!ms) {
  639. retval = -EPERM;
  640. goto out;
  641. }
  642. ms->ms_sockfd = sockfd;
  643. ms->ms_count = count;
  644. ms->ms_addrlen = addrlen;
  645. ms->ms_addr = addr ? sgx_copy_to_ustack(addr, addrlen) : NULL;
  646. if (obuf) {
  647. ms->ms_buf = obuf;
  648. memcpy(obuf, buf, count);
  649. } else {
  650. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  651. }
  652. if (!ms->ms_buf || (addr && !ms->ms_addr)) {
  653. retval = -EPERM;
  654. goto out;
  655. }
  656. retval = sgx_ocall(OCALL_SOCK_SEND, ms);
  657. out:
  658. sgx_reset_ustack();
  659. if (obuf)
  660. ocall_unmap_untrusted(obuf, ALLOC_ALIGNUP(count));
  661. return retval;
  662. }
  663. int ocall_sock_recv_fd (int sockfd, void * buf, unsigned int count,
  664. unsigned int * fds, unsigned int * nfds)
  665. {
  666. int retval = 0;
  667. unsigned int copied;
  668. unsigned int max_nfds_bytes = (*nfds) * sizeof(int);
  669. ms_ocall_sock_recv_fd_t * ms;
  670. ms = sgx_alloc_on_ustack(sizeof(*ms));
  671. if (!ms) {
  672. sgx_reset_ustack();
  673. return -EPERM;
  674. }
  675. ms->ms_sockfd = sockfd;
  676. ms->ms_count = count;
  677. ms->ms_nfds = *nfds;
  678. ms->ms_buf = sgx_alloc_on_ustack(count);
  679. ms->ms_fds = sgx_alloc_on_ustack(max_nfds_bytes);
  680. if (!ms->ms_buf || !ms->ms_fds) {
  681. sgx_reset_ustack();
  682. return -EPERM;
  683. }
  684. retval = sgx_ocall(OCALL_SOCK_RECV_FD, ms);
  685. if (retval >= 0) {
  686. if (!sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
  687. sgx_reset_ustack();
  688. return -EPERM;
  689. }
  690. copied = sgx_copy_to_enclave(fds, max_nfds_bytes, ms->ms_fds, ms->ms_nfds * sizeof(int));
  691. if (!copied) {
  692. sgx_reset_ustack();
  693. return -EPERM;
  694. }
  695. *nfds = copied / sizeof(int);
  696. }
  697. sgx_reset_ustack();
  698. return retval;
  699. }
  700. int ocall_sock_send_fd (int sockfd, const void * buf, unsigned int count,
  701. const unsigned int * fds, unsigned int nfds)
  702. {
  703. int retval = 0;
  704. ms_ocall_sock_send_fd_t * ms;
  705. ms = sgx_alloc_on_ustack(sizeof(*ms));
  706. if (!ms) {
  707. sgx_reset_ustack();
  708. return -EPERM;
  709. }
  710. ms->ms_sockfd = sockfd;
  711. ms->ms_count = count;
  712. ms->ms_nfds = nfds;
  713. ms->ms_buf = sgx_copy_to_ustack(buf, count);
  714. ms->ms_fds = sgx_copy_to_ustack(fds, nfds * sizeof(int));
  715. if (!ms->ms_buf || !ms->ms_fds) {
  716. sgx_reset_ustack();
  717. return -EPERM;
  718. }
  719. retval = sgx_ocall(OCALL_SOCK_SEND_FD, ms);
  720. sgx_reset_ustack();
  721. return retval;
  722. }
  723. int ocall_sock_setopt (int sockfd, int level, int optname,
  724. const void * optval, unsigned int optlen)
  725. {
  726. int retval = 0;
  727. ms_ocall_sock_setopt_t * ms;
  728. ms = sgx_alloc_on_ustack(sizeof(*ms));
  729. if (!ms) {
  730. sgx_reset_ustack();
  731. return -EPERM;
  732. }
  733. ms->ms_sockfd = sockfd;
  734. ms->ms_level = level;
  735. ms->ms_optname = optname;
  736. ms->ms_optlen = 0;
  737. ms->ms_optval = NULL;
  738. if (optval && optlen > 0) {
  739. ms->ms_optlen = optlen;
  740. ms->ms_optval = sgx_copy_to_ustack(optval, optlen);
  741. if (!ms->ms_optval) {
  742. sgx_reset_ustack();
  743. return -EPERM;
  744. }
  745. }
  746. retval = sgx_ocall(OCALL_SOCK_SETOPT, ms);
  747. sgx_reset_ustack();
  748. return retval;
  749. }
  750. int ocall_sock_shutdown (int sockfd, int how)
  751. {
  752. int retval = 0;
  753. ms_ocall_sock_shutdown_t * ms;
  754. ms = sgx_alloc_on_ustack(sizeof(*ms));
  755. if (!ms) {
  756. sgx_reset_ustack();
  757. return -EPERM;
  758. }
  759. ms->ms_sockfd = sockfd;
  760. ms->ms_how = how;
  761. retval = sgx_ocall(OCALL_SOCK_SHUTDOWN, ms);
  762. sgx_reset_ustack();
  763. return retval;
  764. }
  765. int ocall_gettime (unsigned long * microsec)
  766. {
  767. int retval = 0;
  768. ms_ocall_gettime_t * ms;
  769. ms = sgx_alloc_on_ustack(sizeof(*ms));
  770. if (!ms) {
  771. sgx_reset_ustack();
  772. return -EPERM;
  773. }
  774. do {
  775. retval = sgx_ocall(OCALL_GETTIME, ms);
  776. } while(retval == -EINTR);
  777. if (!retval)
  778. *microsec = ms->ms_microsec;
  779. sgx_reset_ustack();
  780. return retval;
  781. }
  782. int ocall_sleep (unsigned long * microsec)
  783. {
  784. int retval = 0;
  785. ms_ocall_sleep_t * ms;
  786. ms = sgx_alloc_on_ustack(sizeof(*ms));
  787. if (!ms) {
  788. sgx_reset_ustack();
  789. return -EPERM;
  790. }
  791. ms->ms_microsec = microsec ? *microsec : 0;
  792. retval = sgx_ocall(OCALL_SLEEP, ms);
  793. if (microsec) {
  794. if (!retval)
  795. *microsec = 0;
  796. else if (retval == -EINTR)
  797. *microsec = ms->ms_microsec;
  798. }
  799. sgx_reset_ustack();
  800. return retval;
  801. }
  802. int ocall_poll(struct pollfd* fds, int nfds, int64_t timeout_us) {
  803. int retval = 0;
  804. unsigned int nfds_bytes = nfds * sizeof(struct pollfd);
  805. ms_ocall_poll_t * ms;
  806. ms = sgx_alloc_on_ustack(sizeof(*ms));
  807. if (!ms) {
  808. sgx_reset_ustack();
  809. return -EPERM;
  810. }
  811. ms->ms_nfds = nfds;
  812. ms->ms_timeout_us = timeout_us;
  813. ms->ms_fds = sgx_copy_to_ustack(fds, nfds_bytes);
  814. if (!ms->ms_fds) {
  815. sgx_reset_ustack();
  816. return -EPERM;
  817. }
  818. retval = sgx_ocall(OCALL_POLL, ms);
  819. if (retval >= 0) {
  820. if (!sgx_copy_to_enclave(fds, nfds_bytes, ms->ms_fds, nfds_bytes)) {
  821. sgx_reset_ustack();
  822. return -EPERM;
  823. }
  824. }
  825. sgx_reset_ustack();
  826. return retval;
  827. }
  828. int ocall_rename (const char * oldpath, const char * newpath)
  829. {
  830. int retval = 0;
  831. int oldlen = oldpath ? strlen(oldpath) + 1 : 0;
  832. int newlen = newpath ? strlen(newpath) + 1 : 0;
  833. ms_ocall_rename_t * ms;
  834. ms = sgx_alloc_on_ustack(sizeof(*ms));
  835. if (!ms) {
  836. sgx_reset_ustack();
  837. return -EPERM;
  838. }
  839. ms->ms_oldpath = sgx_copy_to_ustack(oldpath, oldlen);
  840. ms->ms_newpath = sgx_copy_to_ustack(newpath, newlen);
  841. if (!ms->ms_oldpath || !ms->ms_newpath) {
  842. sgx_reset_ustack();
  843. return -EPERM;
  844. }
  845. retval = sgx_ocall(OCALL_RENAME, ms);
  846. sgx_reset_ustack();
  847. return retval;
  848. }
  849. int ocall_delete (const char * pathname)
  850. {
  851. int retval = 0;
  852. int len = pathname ? strlen(pathname) + 1 : 0;
  853. ms_ocall_delete_t * ms;
  854. ms = sgx_alloc_on_ustack(sizeof(*ms));
  855. if (!ms) {
  856. sgx_reset_ustack();
  857. return -EPERM;
  858. }
  859. ms->ms_pathname = sgx_copy_to_ustack(pathname, len);
  860. if (!ms->ms_pathname) {
  861. sgx_reset_ustack();
  862. return -EPERM;
  863. }
  864. retval = sgx_ocall(OCALL_DELETE, ms);
  865. sgx_reset_ustack();
  866. return retval;
  867. }
  868. int ocall_load_debug(const char * command)
  869. {
  870. int retval = 0;
  871. int len = strlen(command) + 1;
  872. const char * ms = sgx_copy_to_ustack(command, len);
  873. if (!ms) {
  874. sgx_reset_ustack();
  875. return -EPERM;
  876. }
  877. retval = sgx_ocall(OCALL_LOAD_DEBUG, (void *) ms);
  878. sgx_reset_ustack();
  879. return retval;
  880. }
  881. /*
  882. * ocall_get_attestation() triggers remote attestation in untrusted PAL (see sgx_platform.c:
  883. * retrieve_verified_quote()). If the OCall returns successfully, the function returns
  884. * attestation data required for platform verification (i.e., sgx_attestation_t). Except the
  885. * QE report, most data fields of the attestation need to be copied into the enclave.
  886. *
  887. * @spid: The client SPID registered with the IAS.
  888. * @subkey: SPID subscription key.
  889. * @linkable: Whether the SPID is linkable.
  890. * @report: Local attestation report for the quoting enclave.
  891. * @nonce: Randomly-generated nonce for freshness.
  892. * @attestation: Returns the attestation data (QE report, quote, IAS report, signature,
  893. * and certificate chain).
  894. */
  895. int ocall_get_attestation (const sgx_spid_t* spid, const char* subkey, bool linkable,
  896. const sgx_arch_report_t* report, const sgx_quote_nonce_t* nonce,
  897. sgx_attestation_t* attestation) {
  898. ms_ocall_get_attestation_t * ms;
  899. int retval = -EPERM;
  900. ms = sgx_alloc_on_ustack(sizeof(*ms));
  901. if (!ms)
  902. goto reset;
  903. memcpy(&ms->ms_spid, spid, sizeof(sgx_spid_t));
  904. ms->ms_subkey = sgx_copy_to_ustack(subkey, strlen(subkey) + 1);
  905. memcpy(&ms->ms_report, report, sizeof(sgx_arch_report_t));
  906. memcpy(&ms->ms_nonce, nonce, sizeof(sgx_quote_nonce_t));
  907. ms->ms_linkable = linkable;
  908. retval = sgx_ocall(OCALL_GET_ATTESTATION, ms);
  909. if (retval >= 0) {
  910. // First, try to copy the whole ms->ms_attestation inside
  911. if (!sgx_copy_to_enclave(attestation, sizeof(sgx_attestation_t), &ms->ms_attestation,
  912. sizeof(sgx_attestation_t))) {
  913. retval = -EACCES;
  914. goto reset;
  915. }
  916. // For calling ocall_unmap_untrusted, need to reset the untrusted stack
  917. sgx_reset_ustack();
  918. // Copy each field inside and free the untrusted buffers
  919. if (attestation->quote) {
  920. size_t len = attestation->quote_len;
  921. sgx_quote_t* quote = malloc(len);
  922. if (!sgx_copy_to_enclave(quote, len, attestation->quote, len))
  923. retval = -EACCES;
  924. ocall_unmap_untrusted(attestation->quote, ALLOC_ALIGNUP(len));
  925. attestation->quote = quote;
  926. }
  927. if (attestation->ias_report) {
  928. size_t len = attestation->ias_report_len;
  929. char* ias_report = malloc(len + 1);
  930. if (!sgx_copy_to_enclave(ias_report, len, attestation->ias_report, len))
  931. retval = -EACCES;
  932. ocall_unmap_untrusted(attestation->ias_report, ALLOC_ALIGNUP(len));
  933. ias_report[len] = 0; // Ensure null-ending
  934. attestation->ias_report = ias_report;
  935. }
  936. if (attestation->ias_sig) {
  937. size_t len = attestation->ias_sig_len;
  938. uint8_t* ias_sig = malloc(len);
  939. if (!sgx_copy_to_enclave(ias_sig, len, attestation->ias_sig, len))
  940. retval = -EACCES;
  941. ocall_unmap_untrusted(attestation->ias_sig, ALLOC_ALIGNUP(len));
  942. attestation->ias_sig = ias_sig;
  943. }
  944. if (attestation->ias_certs) {
  945. size_t len = attestation->ias_certs_len;
  946. char* ias_certs = malloc(len + 1);
  947. if (!sgx_copy_to_enclave(ias_certs, len, attestation->ias_certs, len))
  948. retval = -EACCES;
  949. ocall_unmap_untrusted(attestation->ias_certs, ALLOC_ALIGNUP(len));
  950. ias_certs[len] = 0; // Ensure null-ending
  951. attestation->ias_certs = ias_certs;
  952. }
  953. // At this point, no field should point to outside the enclave
  954. if (retval < 0) {
  955. if (attestation->quote) free(attestation->quote);
  956. if (attestation->ias_report) free(attestation->ias_report);
  957. if (attestation->ias_sig) free(attestation->ias_sig);
  958. if (attestation->ias_certs) free(attestation->ias_certs);
  959. }
  960. goto out;
  961. }
  962. reset:
  963. sgx_reset_ustack();
  964. out:
  965. return retval;
  966. }