enclave_ocalls.c 28 KB

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