sgx_uprotected_fs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2011-2017 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. #ifdef _PROTECTEDFS_VALIDATION
  42. #include "validation_hook_linux.h"
  43. #endif
  44. #ifdef DEBUG
  45. #define DEBUG_PRINT(fmt, args...) fprintf(stderr, "[sgx_uprotected_fs.h:%d] " fmt, __LINE__, ##args)
  46. #else
  47. #define DEBUG_PRINT(...)
  48. #endif
  49. void* u_sgxprotectedfs_exclusive_file_open(const char* filename, uint8_t read_only, int64_t* file_size, int32_t* error_code)
  50. {
  51. FILE* f = NULL;
  52. int result = 0;
  53. int fd = -1;
  54. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  55. struct stat stat_st;
  56. memset(&stat_st, 0, sizeof(struct stat));
  57. if (filename == NULL || strnlen(filename, 1) == 0)
  58. {
  59. DEBUG_PRINT("filename is NULL or empty\n");
  60. *error_code = EINVAL;
  61. return NULL;
  62. }
  63. // open the file with OS API so we can 'lock' the file and get exclusive access to it
  64. 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
  65. if (fd == -1)
  66. {
  67. DEBUG_PRINT("open returned %d, errno %d\n", result, errno);
  68. *error_code = errno;
  69. return NULL;
  70. }
  71. // this lock is advisory only and programs with high priviliges can ignore it
  72. // it is set to help the user avoid mistakes, but it won't prevent intensional DOS attack from priviliged user
  73. result = flock(fd, (read_only ? LOCK_SH : LOCK_EX) | LOCK_NB); // NB - non blocking
  74. if (result != 0)
  75. {
  76. DEBUG_PRINT("flock returned %d, errno %d\n", result, errno);
  77. *error_code = errno;
  78. result = close(fd);
  79. assert(result == 0);
  80. return NULL;
  81. }
  82. result = fstat(fd, &stat_st);
  83. if (result != 0)
  84. {
  85. DEBUG_PRINT("fstat returned %d, errno %d\n", result, errno);
  86. *error_code = errno;
  87. flock(fd, LOCK_UN);
  88. result = close(fd);
  89. assert(result == 0);
  90. return NULL;
  91. }
  92. // convert the file handle to standard 'C' API file pointer
  93. f = fdopen(fd, read_only ? "rb" : "r+b");
  94. if (f == NULL)
  95. {
  96. DEBUG_PRINT("fdopen returned NULL\n");
  97. *error_code = errno;
  98. flock(fd, LOCK_UN);
  99. result = close(fd);
  100. assert(result == 0);
  101. return NULL;
  102. }
  103. if (file_size != NULL)
  104. *file_size = stat_st.st_size;
  105. return f;
  106. }
  107. uint8_t u_sgxprotectedfs_check_if_file_exists(const char* filename)
  108. {
  109. struct stat stat_st;
  110. memset(&stat_st, 0, sizeof(struct stat));
  111. if (filename == NULL || strnlen(filename, 1) == 0)
  112. {
  113. DEBUG_PRINT("filename is NULL or empty\n");
  114. return 1;
  115. }
  116. return (stat(filename, &stat_st) == 0);
  117. }
  118. int32_t u_sgxprotectedfs_fread_node(void* f, uint64_t node_number, uint8_t* buffer, uint32_t node_size)
  119. {
  120. FILE* file = (FILE*)f;
  121. uint64_t offset = node_number * node_size;
  122. int result = 0;
  123. size_t size = 0;
  124. if (file == NULL)
  125. {
  126. DEBUG_PRINT("file is NULL\n");
  127. return -1;
  128. }
  129. if ((result = fseeko(file, offset, SEEK_SET)) != 0)
  130. {
  131. DEBUG_PRINT("fseeko returned %d\n", result);
  132. if (errno != 0)
  133. {
  134. int err = errno;
  135. return err;
  136. }
  137. else
  138. return -1;
  139. }
  140. if ((size = fread(buffer, node_size, 1, file)) != 1)
  141. {
  142. int err = ferror(file);
  143. if (err != 0)
  144. {
  145. DEBUG_PRINT("fread returned %ld [!= 1], ferror: %d\n", size, err);
  146. return err;
  147. }
  148. else if (errno != 0)
  149. {
  150. err = errno;
  151. DEBUG_PRINT("fread returned %ld [!= 1], errno: %d\n", size, err);
  152. return err;
  153. }
  154. else
  155. {
  156. DEBUG_PRINT("fread returned %ld [!= 1], no error code\n", size);
  157. return -1;
  158. }
  159. }
  160. return 0;
  161. }
  162. int32_t u_sgxprotectedfs_fwrite_node(void* f, uint64_t node_number, uint8_t* buffer, uint32_t node_size)
  163. {
  164. FILE* file = (FILE*)f;
  165. uint64_t offset = node_number * node_size;
  166. int result = 0;
  167. size_t size = 0;
  168. if (file == NULL)
  169. {
  170. DEBUG_PRINT("file is NULL\n");
  171. return -1;
  172. }
  173. if ((result = fseeko(file, offset, SEEK_SET)) != 0)
  174. {
  175. DEBUG_PRINT("fseeko returned %d\n", result);
  176. if (errno != 0)
  177. {
  178. int err = errno;
  179. return err;
  180. }
  181. else
  182. return -1;
  183. }
  184. if ((size = fwrite(buffer, node_size, 1, file)) != 1)
  185. {
  186. DEBUG_PRINT("fwrite returned %ld [!= 1]\n", size);
  187. int err = ferror(file);
  188. if (err != 0)
  189. return err;
  190. else if (errno != 0)
  191. {
  192. err = errno;
  193. return err;
  194. }
  195. else
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. int32_t u_sgxprotectedfs_fclose(void* f)
  201. {
  202. FILE* file = (FILE*)f;
  203. int result = 0;
  204. int fd = 0;
  205. if (file == NULL)
  206. {
  207. DEBUG_PRINT("file is NULL\n");
  208. return -1;
  209. }
  210. // closing the file handle should also remove the lock, but we try to remove it explicitly
  211. fd = fileno(file);
  212. if (fd == -1)
  213. DEBUG_PRINT("fileno returned -1\n");
  214. else
  215. flock(fd, LOCK_UN);
  216. if ((result = fclose(file)) != 0)
  217. {
  218. if (errno != 0)
  219. {
  220. int err = errno;
  221. DEBUG_PRINT("fclose returned %d, errno: %d\n", result, err);
  222. return err;
  223. }
  224. DEBUG_PRINT("fclose returned %d\n", result);
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. uint8_t u_sgxprotectedfs_fflush(void* f)
  230. {
  231. FILE* file = (FILE*)f;
  232. int result;
  233. if (file == NULL)
  234. {
  235. DEBUG_PRINT("file is NULL\n");
  236. return 1;
  237. }
  238. if ((result = fflush(file)) != 0)
  239. {
  240. DEBUG_PRINT("fflush returned %d\n", result);
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. int32_t u_sgxprotectedfs_remove(const char* filename)
  246. {
  247. int result;
  248. if (filename == NULL || strnlen(filename, 1) == 0)
  249. {
  250. DEBUG_PRINT("filename is NULL or empty\n");
  251. return -1;
  252. }
  253. if ((result = remove(filename)) != 0)
  254. {// 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...
  255. //DEBUG_PRINT("remove returned %d\n", result);
  256. if (errno != 0)
  257. return errno;
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. #define MILISECONDS_SLEEP_FOPEN 10
  263. #define MAX_FOPEN_RETRIES 10
  264. void* u_sgxprotectedfs_recovery_file_open(const char* filename)
  265. {
  266. FILE* f = NULL;
  267. if (filename == NULL || strnlen(filename, 1) == 0)
  268. {
  269. DEBUG_PRINT("recovery filename is NULL or empty\n");
  270. return NULL;
  271. }
  272. for (int i = 0; i < MAX_FOPEN_RETRIES; i++)
  273. {
  274. f = fopen(filename, "wb");
  275. if (f != NULL)
  276. break;
  277. usleep(MILISECONDS_SLEEP_FOPEN);
  278. }
  279. if (f == NULL)
  280. {
  281. DEBUG_PRINT("fopen (%s) returned NULL\n", filename);
  282. return NULL;
  283. }
  284. return f;
  285. }
  286. uint8_t u_sgxprotectedfs_fwrite_recovery_node(void* f, uint8_t* data, uint32_t data_length)
  287. {
  288. FILE* file = (FILE*)f;
  289. if (file == NULL)
  290. {
  291. DEBUG_PRINT("file is NULL\n");
  292. return 1;
  293. }
  294. // recovery nodes are written sequentially
  295. size_t count = fwrite(data, 1, data_length, file);
  296. if (count != data_length)
  297. {
  298. DEBUG_PRINT("fwrite returned %ld instead of %d\n", count, data_length);
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. int32_t u_sgxprotectedfs_do_file_recovery(const char* filename, const char* recovery_filename, uint32_t node_size)
  304. {
  305. FILE* recovery_file = NULL;
  306. FILE* source_file = NULL;
  307. int32_t ret = -1;
  308. uint32_t nodes_count = 0;
  309. uint32_t recovery_node_size = (uint32_t)(sizeof(uint64_t)) + node_size; // node offset + data
  310. uint64_t file_size = 0;
  311. int err = 0;
  312. int result = 0;
  313. size_t count = 0;
  314. uint8_t* recovery_node = NULL;
  315. uint32_t i = 0;
  316. do
  317. {
  318. if (filename == NULL || strnlen(filename, 1) == 0)
  319. {
  320. DEBUG_PRINT("filename is NULL or empty\n");
  321. return (int32_t)NULL;
  322. }
  323. if (recovery_filename == NULL || strnlen(recovery_filename, 1) == 0)
  324. {
  325. DEBUG_PRINT("recovery filename is NULL or empty\n");
  326. return (int32_t)NULL;
  327. }
  328. recovery_file = fopen(recovery_filename, "rb");
  329. if (recovery_file == NULL)
  330. {
  331. DEBUG_PRINT("fopen of recovery file returned NULL - no recovery file exists\n");
  332. ret = -1;
  333. break;
  334. }
  335. if ((result = fseeko(recovery_file, 0, SEEK_END)) != 0)
  336. {
  337. DEBUG_PRINT("fseeko returned %d\n", result);
  338. if (errno != 0)
  339. ret = errno;
  340. break;
  341. }
  342. file_size = ftello(recovery_file);
  343. if ((result = fseeko(recovery_file, 0, SEEK_SET)) != 0)
  344. {
  345. DEBUG_PRINT("fseeko returned %d\n", result);
  346. if (errno != 0)
  347. ret = errno;
  348. break;
  349. }
  350. if (file_size % recovery_node_size != 0)
  351. {
  352. // corrupted recovery file
  353. DEBUG_PRINT("recovery file size is not the right size [%lu]\n", file_size);
  354. ret = ENOTSUP;
  355. break;
  356. }
  357. nodes_count = (uint32_t)(file_size / recovery_node_size);
  358. recovery_node = (uint8_t*)malloc(recovery_node_size);
  359. if (recovery_node == NULL)
  360. {
  361. DEBUG_PRINT("malloc failed\n");
  362. ret = ENOMEM;
  363. break;
  364. }
  365. source_file = fopen(filename, "r+b");
  366. if (source_file == NULL)
  367. {
  368. DEBUG_PRINT("fopen returned NULL\n");
  369. ret = -1;
  370. break;
  371. }
  372. for (i = 0 ; i < nodes_count ; i++)
  373. {
  374. if ((count = fread(recovery_node, recovery_node_size, 1, recovery_file)) != 1)
  375. {
  376. DEBUG_PRINT("fread returned %ld [!= 1]\n", count);
  377. err = ferror(recovery_file);
  378. if (err != 0)
  379. ret = err;
  380. else if (errno != 0)
  381. ret = errno;
  382. break;
  383. }
  384. // seek the regular file to the required offset
  385. if ((result = fseeko(source_file, (*((uint64_t*)recovery_node)) * node_size, SEEK_SET)) != 0)
  386. {
  387. DEBUG_PRINT("fseeko returned %d\n", result);
  388. if (errno != 0)
  389. ret = errno;
  390. break;
  391. }
  392. // write down the original data from the recovery file
  393. if ((count = fwrite(&recovery_node[sizeof(uint64_t)], node_size, 1, source_file)) != 1)
  394. {
  395. DEBUG_PRINT("fwrite returned %ld [!= 1]\n", count);
  396. err = ferror(source_file);
  397. if (err != 0)
  398. ret = err;
  399. else if (errno != 0)
  400. ret = errno;
  401. break;
  402. }
  403. }
  404. if (i != nodes_count) // the 'for' loop exited with error
  405. break;
  406. if ((result = fflush(source_file)) != 0)
  407. {
  408. DEBUG_PRINT("fflush returned %d\n", result);
  409. ret = result;
  410. break;
  411. }
  412. ret = 0;
  413. } while(0);
  414. if (recovery_node != NULL)
  415. free(recovery_node);
  416. if (source_file != NULL)
  417. {
  418. result = fclose(source_file);
  419. assert(result == 0);
  420. }
  421. if (recovery_file != NULL)
  422. {
  423. result = fclose(recovery_file);
  424. assert(result == 0);
  425. }
  426. if (ret == 0)
  427. remove(recovery_filename);
  428. return ret;
  429. }