enclave_ocalls.c 30 KB

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