enclave_ocalls.c 33 KB

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