sgx_ipfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) 2022 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #if WASM_ENABLE_SGX_IPFS != 0
  6. #include "ssp_config.h"
  7. #include "bh_platform.h"
  8. #include "sgx_ipfs.h"
  9. #include <errno.h>
  10. #include "sgx_tprotected_fs.h"
  11. #define SGX_ERROR_FILE_LOWEST_ERROR_ID SGX_ERROR_FILE_BAD_STATUS
  12. #define SGX_ERROR_FILE_HIGHEST_ERROR_ID SGX_ERROR_FILE_CLOSE_FAILED
  13. // The mapping between file descriptors and IPFS file pointers.
  14. static HashMap *ipfs_file_list;
  15. // Converts an SGX error code to a POSIX error code.
  16. static __wasi_errno_t
  17. convert_sgx_errno(int error)
  18. {
  19. if (error >= SGX_ERROR_FILE_LOWEST_ERROR_ID
  20. && error <= SGX_ERROR_FILE_HIGHEST_ERROR_ID) {
  21. switch (error) {
  22. /* The file is in bad status */
  23. case SGX_ERROR_FILE_BAD_STATUS:
  24. return ENOTRECOVERABLE;
  25. /* The Key ID field is all zeros, can't re-generate the encryption
  26. * key */
  27. case SGX_ERROR_FILE_NO_KEY_ID:
  28. return EKEYREJECTED;
  29. /* The current file name is different then the original file name
  30. * (not allowed, substitution attack) */
  31. case SGX_ERROR_FILE_NAME_MISMATCH:
  32. return EIO;
  33. /* The file is not an SGX file */
  34. case SGX_ERROR_FILE_NOT_SGX_FILE:
  35. return EEXIST;
  36. /* A recovery file can't be opened, so flush operation can't
  37. * continue (only used when no EXXX is returned) */
  38. case SGX_ERROR_FILE_CANT_OPEN_RECOVERY_FILE:
  39. return EIO;
  40. /* A recovery file can't be written, so flush operation can't
  41. * continue (only used when no EXXX is returned) */
  42. case SGX_ERROR_FILE_CANT_WRITE_RECOVERY_FILE:
  43. return EIO;
  44. /* When openeing the file, recovery is needed, but the recovery
  45. * process failed */
  46. case SGX_ERROR_FILE_RECOVERY_NEEDED:
  47. return EIO;
  48. /* fflush operation (to disk) failed (only used when no EXXX is
  49. * returned) */
  50. case SGX_ERROR_FILE_FLUSH_FAILED:
  51. return EIO;
  52. /* fclose operation (to disk) failed (only used when no EXXX is
  53. * returned) */
  54. case SGX_ERROR_FILE_CLOSE_FAILED:
  55. return EIO;
  56. }
  57. }
  58. return error;
  59. }
  60. static void *
  61. fd2file(int fd)
  62. {
  63. return bh_hash_map_find(ipfs_file_list, (void *)(intptr_t)fd);
  64. }
  65. static void
  66. ipfs_file_destroy(void *sgx_file)
  67. {
  68. sgx_fclose(sgx_file);
  69. }
  70. int
  71. ipfs_init()
  72. {
  73. ipfs_file_list =
  74. bh_hash_map_create(32, true, (HashFunc)fd_hash, (KeyEqualFunc)fd_equal,
  75. NULL, (ValueDestroyFunc)ipfs_file_destroy);
  76. return ipfs_file_list != NULL ? BHT_OK : BHT_ERROR;
  77. }
  78. void
  79. ipfs_destroy()
  80. {
  81. bh_hash_map_destroy(ipfs_file_list);
  82. }
  83. int
  84. ipfs_posix_fallocate(int fd, off_t offset, size_t len)
  85. {
  86. void *sgx_file = fd2file(fd);
  87. if (!sgx_file) {
  88. return EBADF;
  89. }
  90. // The wrapper for fseek takes care of extending the file if sought beyond
  91. // the end
  92. if (ipfs_lseek(fd, offset + len, SEEK_CUR) == -1) {
  93. return errno;
  94. }
  95. // Make sure the file is allocated by flushing it
  96. if (sgx_fflush(sgx_file) != 0) {
  97. return errno;
  98. }
  99. return 0;
  100. }
  101. size_t
  102. ipfs_read(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
  103. off_t offset)
  104. {
  105. int i;
  106. off_t original_offset = 0;
  107. void *sgx_file = fd2file(fd);
  108. size_t read_result, number_of_read_bytes = 0;
  109. if (!sgx_file) {
  110. errno = EBADF;
  111. return -1;
  112. }
  113. if (has_offset) {
  114. // Save the current offset, to restore it after the read operation
  115. original_offset = (off_t)sgx_ftell(sgx_file);
  116. if (original_offset == -1) {
  117. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  118. return -1;
  119. }
  120. // Move to the desired location
  121. if (sgx_fseek(sgx_file, offset, SEEK_SET) == -1) {
  122. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  123. return -1;
  124. }
  125. }
  126. // For each element in the vector
  127. for (i = 0; i < iovcnt; i++) {
  128. if (iov[i].iov_len == 0)
  129. continue;
  130. read_result = sgx_fread(iov[i].iov_base, 1, iov[i].iov_len, sgx_file);
  131. number_of_read_bytes += read_result;
  132. if (read_result != iov[i].iov_len) {
  133. if (!sgx_feof(sgx_file)) {
  134. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  135. return -1;
  136. }
  137. }
  138. }
  139. if (has_offset) {
  140. // Restore the position of the cursor
  141. if (sgx_fseek(sgx_file, original_offset, SEEK_SET) == -1) {
  142. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  143. return -1;
  144. }
  145. }
  146. return number_of_read_bytes;
  147. }
  148. size_t
  149. ipfs_write(int fd, const struct iovec *iov, int iovcnt, bool has_offset,
  150. off_t offset)
  151. {
  152. int i;
  153. off_t original_offset = 0;
  154. void *sgx_file = fd2file(fd);
  155. size_t write_result, number_of_written_bytes = 0;
  156. if (!sgx_file) {
  157. errno = EBADF;
  158. return -1;
  159. }
  160. if (has_offset) {
  161. // Save the current offset, to restore it after the read operation
  162. original_offset = (off_t)sgx_ftell(sgx_file);
  163. if (original_offset == -1) {
  164. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  165. return -1;
  166. }
  167. // Move to the desired location
  168. if (sgx_fseek(sgx_file, offset, SEEK_SET) == -1) {
  169. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  170. return -1;
  171. }
  172. }
  173. // For each element in the vector
  174. for (i = 0; i < iovcnt; i++) {
  175. if (iov[i].iov_len == 0)
  176. continue;
  177. write_result = sgx_fwrite(iov[i].iov_base, 1, iov[i].iov_len, sgx_file);
  178. number_of_written_bytes += write_result;
  179. if (write_result != iov[i].iov_len) {
  180. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  181. return -1;
  182. }
  183. }
  184. if (has_offset) {
  185. // Restore the position of the cursor
  186. if (sgx_fseek(sgx_file, original_offset, SEEK_SET) == -1) {
  187. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  188. return -1;
  189. }
  190. }
  191. return number_of_written_bytes;
  192. }
  193. int
  194. ipfs_close(int fd)
  195. {
  196. void *sgx_file;
  197. if (!bh_hash_map_remove(ipfs_file_list, (void *)(intptr_t)fd, NULL,
  198. &sgx_file)) {
  199. errno = EBADF;
  200. return -1;
  201. }
  202. if (sgx_fclose(sgx_file)) {
  203. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. void *
  209. ipfs_fopen(int fd, const char *filename, int flags)
  210. {
  211. // Mapping back the mode
  212. const char *mode;
  213. bool must_create = (flags & O_CREAT) != 0;
  214. bool must_truncate = (flags & O_TRUNC) != 0;
  215. bool must_append = (flags & O_APPEND) != 0;
  216. bool read_only = (flags & O_ACCMODE) == O_RDONLY;
  217. bool write_only = (flags & O_ACCMODE) == O_WRONLY;
  218. bool read_write = (flags & O_ACCMODE) == O_RDWR;
  219. // The mapping of the mode are described in the table in the official
  220. // specifications:
  221. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
  222. if (read_only)
  223. mode = "r";
  224. else if (write_only && must_create && must_truncate)
  225. mode = "w";
  226. else if (write_only && must_create && must_append)
  227. mode = "a";
  228. else if (read_write && must_create && must_truncate)
  229. mode = "w+";
  230. else if (read_write && must_create && must_append)
  231. mode = "a+";
  232. else if (read_write && must_create)
  233. mode = "w+";
  234. else if (read_write)
  235. mode = "r+";
  236. else
  237. mode = NULL;
  238. // Cannot map the requested access to the SGX IPFS
  239. if (mode == NULL) {
  240. errno = __WASI_ENOTCAPABLE;
  241. return NULL;
  242. }
  243. // Opening the file
  244. void *sgx_file = sgx_fopen_auto_key(filename, mode);
  245. if (sgx_file == NULL) {
  246. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  247. return NULL;
  248. }
  249. if (!bh_hash_map_insert(ipfs_file_list, (void *)(intptr_t)fd, sgx_file)) {
  250. errno = __WASI_ECANCELED;
  251. sgx_fclose(sgx_file);
  252. os_printf("An error occurred while inserting the IPFS file pointer in "
  253. "the map.");
  254. return NULL;
  255. }
  256. return sgx_file;
  257. }
  258. int
  259. ipfs_fflush(int fd)
  260. {
  261. void *sgx_file = fd2file(fd);
  262. if (!sgx_file) {
  263. errno = EBADF;
  264. return EOF;
  265. }
  266. int ret = sgx_fflush(sgx_file);
  267. if (ret == 1) {
  268. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  269. return EOF;
  270. }
  271. return ret;
  272. }
  273. off_t
  274. ipfs_lseek(int fd, off_t offset, int nwhence)
  275. {
  276. off_t new_offset;
  277. void *sgx_file = fd2file(fd);
  278. if (!sgx_file) {
  279. errno = EBADF;
  280. return -1;
  281. }
  282. // Optimization: if the offset is 0 and the whence is SEEK_CUR,
  283. // this is equivalent of a call to ftell.
  284. if (offset == 0 && nwhence == SEEK_CUR) {
  285. int64_t ftell_result = (off_t)sgx_ftell(sgx_file);
  286. if (ftell_result == -1) {
  287. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  288. return -1;
  289. }
  290. return ftell_result;
  291. }
  292. int fseek_result = sgx_fseek(sgx_file, offset, nwhence);
  293. if (fseek_result == 0) {
  294. new_offset = (__wasi_filesize_t)sgx_ftell(sgx_file);
  295. if (new_offset == -1) {
  296. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  297. return -1;
  298. }
  299. return new_offset;
  300. }
  301. else {
  302. // In the case fseek returned an error
  303. int sgx_error = sgx_ferror(sgx_file);
  304. if (sgx_error != EINVAL) {
  305. errno = convert_sgx_errno(sgx_error);
  306. return -1;
  307. }
  308. // We must consider a difference in behavior of sgx_fseek and the POSIX
  309. // fseek. If the cursor is moved beyond the end of the file, sgx_fseek
  310. // returns an error, whereas POSIX fseek accepts the cursor move and
  311. // fill with zeroes the difference for the next write. This
  312. // implementation handle zeroes completion and moving the cursor forward
  313. // the end of the file, but does it now (during the fseek), which is
  314. // different compared to POSIX implementation, that writes zeroes on the
  315. // next write. This avoids the runtime to keep track of the cursor
  316. // manually.
  317. // Assume the error is raised because the cursor is moved beyond the end
  318. // of the file. Try to move the cursor at the end of the file.
  319. if (sgx_fseek(sgx_file, 0, SEEK_END) == -1) {
  320. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  321. return -1;
  322. }
  323. // Write the missing zeroes
  324. char zero = 0;
  325. int64_t number_of_zeroes = offset - sgx_ftell(sgx_file);
  326. if (sgx_fwrite(&zero, 1, number_of_zeroes, sgx_file) == 0) {
  327. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  328. return -1;
  329. }
  330. // Move again at the end of the file
  331. if (sgx_fseek(sgx_file, 0, SEEK_END) == -1) {
  332. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  333. return -1;
  334. }
  335. return offset;
  336. }
  337. }
  338. // The official API does not provide a way to truncate files.
  339. // Only files extension is supported.
  340. int
  341. ipfs_ftruncate(int fd, off_t len)
  342. {
  343. void *sgx_file = fd2file(fd);
  344. if (!sgx_file) {
  345. errno = EBADF;
  346. return -1;
  347. }
  348. off_t original_offset = sgx_ftell(sgx_file);
  349. // Optimization path: if the length is smaller than the offset,
  350. // IPFS does not support truncate to a smaller size.
  351. if (len < original_offset) {
  352. os_printf(
  353. "SGX IPFS does not support truncate files to smaller sizes.\n");
  354. return __WASI_ECANCELED;
  355. }
  356. // Move to the end of the file to determine whether this is
  357. // a file extension or reduction.
  358. if (sgx_fseek(sgx_file, 0, SEEK_END) == -1) {
  359. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  360. return -1;
  361. }
  362. off_t file_size = sgx_ftell(sgx_file);
  363. // Reducing the file space is not supported by IPFS.
  364. if (len < file_size) {
  365. os_printf(
  366. "SGX IPFS does not support truncate files to smaller sizes.\n");
  367. return __WASI_ECANCELED;
  368. }
  369. // Increasing the size is equal to writing from the end of the file
  370. // with null bytes.
  371. char null_byte = 0;
  372. if (sgx_fwrite(&null_byte, 1, len - file_size, sgx_file) == 0) {
  373. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  374. return -1;
  375. }
  376. // Restore the position of the cursor
  377. if (sgx_fseek(sgx_file, original_offset, SEEK_SET) == -1) {
  378. errno = convert_sgx_errno(sgx_ferror(sgx_file));
  379. return -1;
  380. }
  381. return 0;
  382. }
  383. #endif /* end of WASM_ENABLE_SGX_IPFS */