sgx_uprotected_fs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <malloc.h>
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <sys/file.h>
  37. #include <sys/stat.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include "sgx_tprotected_fs_u.h"
  41. #include <uprotected_fs.h>
  42. #ifdef DEBUG
  43. #define DEBUG_PRINT(fmt, args...) fprintf(stderr, "[sgx_uprotected_fs.h:%d] " fmt, __LINE__, ##args)
  44. #else
  45. #define DEBUG_PRINT(...)
  46. #endif
  47. void* u_sgxprotectedfs_exclusive_file_open(const char* filename, uint8_t read_only, int64_t* file_size, int32_t* error_code)
  48. {
  49. FILE* f = NULL;
  50. int result = 0;
  51. int fd = -1;
  52. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  53. struct stat stat_st;
  54. memset(&stat_st, 0, sizeof(struct stat));
  55. if (filename == NULL || strnlen(filename, 1) == 0)
  56. {
  57. DEBUG_PRINT("filename is NULL or empty\n");
  58. *error_code = EINVAL;
  59. return NULL;
  60. }
  61. // open the file with OS API so we can 'lock' the file and get exclusive access to it
  62. fd = open(filename, O_CREAT | (read_only ? O_RDONLY : O_RDWR) | O_LARGEFILE, mode); // create the file if it doesn't exists, read-only/read-write
  63. if (fd == -1)
  64. {
  65. DEBUG_PRINT("open returned %d, errno %d\n", result, errno);
  66. *error_code = errno;
  67. return NULL;
  68. }
  69. // this lock is advisory only and programs with high priviliges can ignore it
  70. // it is set to help the user avoid mistakes, but it won't prevent intensional DOS attack from priviliged user
  71. result = flock(fd, (read_only ? LOCK_SH : LOCK_EX) | LOCK_NB); // NB - non blocking
  72. if (result != 0)
  73. {
  74. DEBUG_PRINT("flock returned %d, errno %d\n", result, errno);
  75. *error_code = errno;
  76. result = close(fd);
  77. assert(result == 0);
  78. return NULL;
  79. }
  80. result = fstat(fd, &stat_st);
  81. if (result != 0)
  82. {
  83. DEBUG_PRINT("fstat returned %d, errno %d\n", result, errno);
  84. *error_code = errno;
  85. flock(fd, LOCK_UN);
  86. result = close(fd);
  87. assert(result == 0);
  88. return NULL;
  89. }
  90. // convert the file handle to standard 'C' API file pointer
  91. f = fdopen(fd, read_only ? "rb" : "r+b");
  92. if (f == NULL)
  93. {
  94. DEBUG_PRINT("fdopen returned NULL\n");
  95. *error_code = errno;
  96. flock(fd, LOCK_UN);
  97. result = close(fd);
  98. assert(result == 0);
  99. return NULL;
  100. }
  101. if (file_size != NULL)
  102. *file_size = stat_st.st_size;
  103. return f;
  104. }
  105. uint8_t u_sgxprotectedfs_check_if_file_exists(const char* filename)
  106. {
  107. struct stat stat_st;
  108. memset(&stat_st, 0, sizeof(struct stat));
  109. if (filename == NULL || strnlen(filename, 1) == 0)
  110. {
  111. DEBUG_PRINT("filename is NULL or empty\n");
  112. return 1;
  113. }
  114. return (stat(filename, &stat_st) == 0);
  115. }
  116. int32_t u_sgxprotectedfs_fread_node(void* f, uint64_t node_number, uint8_t* buffer, uint32_t node_size)
  117. {
  118. FILE* file = (FILE*)f;
  119. uint64_t offset = node_number * node_size;
  120. int result = 0;
  121. size_t size = 0;
  122. if (file == NULL)
  123. {
  124. DEBUG_PRINT("file is NULL\n");
  125. return -1;
  126. }
  127. if ((result = fseeko(file, offset, SEEK_SET)) != 0)
  128. {
  129. DEBUG_PRINT("fseeko returned %d\n", result);
  130. if (errno != 0)
  131. {
  132. int err = errno;
  133. return err;
  134. }
  135. else
  136. return -1;
  137. }
  138. if ((size = fread(buffer, node_size, 1, file)) != 1)
  139. {
  140. int err = ferror(file);
  141. if (err != 0)
  142. {
  143. DEBUG_PRINT("fread returned %ld [!= 1], ferror: %d\n", size, err);
  144. return err;
  145. }
  146. else if (errno != 0)
  147. {
  148. err = errno;
  149. DEBUG_PRINT("fread returned %ld [!= 1], errno: %d\n", size, err);
  150. return err;
  151. }
  152. else
  153. {
  154. DEBUG_PRINT("fread returned %ld [!= 1], no error code\n", size);
  155. return -1;
  156. }
  157. }
  158. return 0;
  159. }
  160. int32_t u_sgxprotectedfs_fwrite_node(void* f, uint64_t node_number, uint8_t* buffer, uint32_t node_size)
  161. {
  162. FILE* file = (FILE*)f;
  163. uint64_t offset = node_number * node_size;
  164. int result = 0;
  165. size_t size = 0;
  166. if (file == NULL)
  167. {
  168. DEBUG_PRINT("file is NULL\n");
  169. return -1;
  170. }
  171. if ((result = fseeko(file, offset, SEEK_SET)) != 0)
  172. {
  173. DEBUG_PRINT("fseeko returned %d\n", result);
  174. if (errno != 0)
  175. {
  176. int err = errno;
  177. return err;
  178. }
  179. else
  180. return -1;
  181. }
  182. if ((size = fwrite(buffer, node_size, 1, file)) != 1)
  183. {
  184. DEBUG_PRINT("fwrite returned %ld [!= 1]\n", size);
  185. int err = ferror(file);
  186. if (err != 0)
  187. return err;
  188. else if (errno != 0)
  189. {
  190. err = errno;
  191. return err;
  192. }
  193. else
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. int32_t u_sgxprotectedfs_fclose(void* f)
  199. {
  200. FILE* file = (FILE*)f;
  201. int result = 0;
  202. int fd = 0;
  203. if (file == NULL)
  204. {
  205. DEBUG_PRINT("file is NULL\n");
  206. return -1;
  207. }
  208. // closing the file handle should also remove the lock, but we try to remove it explicitly
  209. fd = fileno(file);
  210. if (fd == -1)
  211. DEBUG_PRINT("fileno returned -1\n");
  212. else
  213. flock(fd, LOCK_UN);
  214. if ((result = fclose(file)) != 0)
  215. {
  216. if (errno != 0)
  217. {
  218. int err = errno;
  219. DEBUG_PRINT("fclose returned %d, errno: %d\n", result, err);
  220. return err;
  221. }
  222. DEBUG_PRINT("fclose returned %d\n", result);
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. uint8_t u_sgxprotectedfs_fflush(void* f)
  228. {
  229. FILE* file = (FILE*)f;
  230. int result;
  231. if (file == NULL)
  232. {
  233. DEBUG_PRINT("file is NULL\n");
  234. return 1;
  235. }
  236. if ((result = fflush(file)) != 0)
  237. {
  238. DEBUG_PRINT("fflush returned %d\n", result);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. int32_t u_sgxprotectedfs_remove(const char* filename)
  244. {
  245. int result;
  246. if (filename == NULL || strnlen(filename, 1) == 0)
  247. {
  248. DEBUG_PRINT("filename is NULL or empty\n");
  249. return -1;
  250. }
  251. if ((result = remove(filename)) != 0)
  252. {// this function is called from the destructor which is called when calling fclose, if there were no writes, there is no recovery file...we don't want endless prints...
  253. //DEBUG_PRINT("remove returned %d\n", result);
  254. if (errno != 0)
  255. return errno;
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. #define MILISECONDS_SLEEP_FOPEN 10
  261. #define MAX_FOPEN_RETRIES 10
  262. void* u_sgxprotectedfs_recovery_file_open(const char* filename)
  263. {
  264. FILE* f = NULL;
  265. if (filename == NULL || strnlen(filename, 1) == 0)
  266. {
  267. DEBUG_PRINT("recovery filename is NULL or empty\n");
  268. return NULL;
  269. }
  270. for (int i = 0; i < MAX_FOPEN_RETRIES; i++)
  271. {
  272. f = fopen(filename, "wb");
  273. if (f != NULL)
  274. break;
  275. usleep(MILISECONDS_SLEEP_FOPEN);
  276. }
  277. if (f == NULL)
  278. {
  279. DEBUG_PRINT("fopen (%s) returned NULL\n", filename);
  280. return NULL;
  281. }
  282. return f;
  283. }
  284. uint8_t u_sgxprotectedfs_fwrite_recovery_node(void* f, uint8_t* data, uint32_t data_length)
  285. {
  286. FILE* file = (FILE*)f;
  287. if (file == NULL)
  288. {
  289. DEBUG_PRINT("file is NULL\n");
  290. return 1;
  291. }
  292. // recovery nodes are written sequentially
  293. size_t count = fwrite(data, 1, data_length, file);
  294. if (count != data_length)
  295. {
  296. DEBUG_PRINT("fwrite returned %ld instead of %d\n", count, data_length);
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. int32_t u_sgxprotectedfs_do_file_recovery(const char* filename, const char* recovery_filename, uint32_t node_size)
  302. {
  303. FILE* recovery_file = NULL;
  304. FILE* source_file = NULL;
  305. int32_t ret = -1;
  306. uint32_t nodes_count = 0;
  307. uint32_t recovery_node_size = (uint32_t)(sizeof(uint64_t)) + node_size; // node offset + data
  308. uint64_t file_size = 0;
  309. int err = 0;
  310. int result = 0;
  311. size_t count = 0;
  312. uint8_t* recovery_node = NULL;
  313. uint32_t i = 0;
  314. do
  315. {
  316. if (filename == NULL || strnlen(filename, 1) == 0)
  317. {
  318. DEBUG_PRINT("filename is NULL or empty\n");
  319. return (int32_t)NULL;
  320. }
  321. if (recovery_filename == NULL || strnlen(recovery_filename, 1) == 0)
  322. {
  323. DEBUG_PRINT("recovery filename is NULL or empty\n");
  324. return (int32_t)NULL;
  325. }
  326. recovery_file = fopen(recovery_filename, "rb");
  327. if (recovery_file == NULL)
  328. {
  329. DEBUG_PRINT("fopen of recovery file returned NULL - no recovery file exists\n");
  330. ret = -1;
  331. break;
  332. }
  333. if ((result = fseeko(recovery_file, 0, SEEK_END)) != 0)
  334. {
  335. DEBUG_PRINT("fseeko returned %d\n", result);
  336. if (errno != 0)
  337. ret = errno;
  338. break;
  339. }
  340. file_size = ftello(recovery_file);
  341. if ((result = fseeko(recovery_file, 0, SEEK_SET)) != 0)
  342. {
  343. DEBUG_PRINT("fseeko returned %d\n", result);
  344. if (errno != 0)
  345. ret = errno;
  346. break;
  347. }
  348. if (file_size % recovery_node_size != 0)
  349. {
  350. // corrupted recovery file
  351. DEBUG_PRINT("recovery file size is not the right size [%lu]\n", file_size);
  352. ret = ENOTSUP;
  353. break;
  354. }
  355. nodes_count = (uint32_t)(file_size / recovery_node_size);
  356. recovery_node = (uint8_t*)malloc(recovery_node_size);
  357. if (recovery_node == NULL)
  358. {
  359. DEBUG_PRINT("malloc failed\n");
  360. ret = ENOMEM;
  361. break;
  362. }
  363. source_file = fopen(filename, "r+b");
  364. if (source_file == NULL)
  365. {
  366. DEBUG_PRINT("fopen returned NULL\n");
  367. ret = -1;
  368. break;
  369. }
  370. for (i = 0 ; i < nodes_count ; i++)
  371. {
  372. if ((count = fread(recovery_node, recovery_node_size, 1, recovery_file)) != 1)
  373. {
  374. DEBUG_PRINT("fread returned %ld [!= 1]\n", count);
  375. err = ferror(recovery_file);
  376. if (err != 0)
  377. ret = err;
  378. else if (errno != 0)
  379. ret = errno;
  380. break;
  381. }
  382. // seek the regular file to the required offset
  383. if ((result = fseeko(source_file, (*((uint64_t*)recovery_node)) * node_size, SEEK_SET)) != 0)
  384. {
  385. DEBUG_PRINT("fseeko returned %d\n", result);
  386. if (errno != 0)
  387. ret = errno;
  388. break;
  389. }
  390. // write down the original data from the recovery file
  391. if ((count = fwrite(&recovery_node[sizeof(uint64_t)], node_size, 1, source_file)) != 1)
  392. {
  393. DEBUG_PRINT("fwrite returned %ld [!= 1]\n", count);
  394. err = ferror(source_file);
  395. if (err != 0)
  396. ret = err;
  397. else if (errno != 0)
  398. ret = errno;
  399. break;
  400. }
  401. }
  402. if (i != nodes_count) // the 'for' loop exited with error
  403. break;
  404. if ((result = fflush(source_file)) != 0)
  405. {
  406. DEBUG_PRINT("fflush returned %d\n", result);
  407. ret = result;
  408. break;
  409. }
  410. ret = 0;
  411. } while(0);
  412. if (recovery_node != NULL)
  413. free(recovery_node);
  414. if (source_file != NULL)
  415. {
  416. result = fclose(source_file);
  417. assert(result == 0);
  418. }
  419. if (recovery_file != NULL)
  420. {
  421. result = fclose(recovery_file);
  422. assert(result == 0);
  423. }
  424. if (ret == 0)
  425. remove(recovery_filename);
  426. return ret;
  427. }