file_init.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 "sgx_tprotected_fs.h"
  32. #include "sgx_tprotected_fs_t.h"
  33. #include "protected_fs_file.h"
  34. #include <sgx_utils.h>
  35. // remove the file path if it's there, leave only the filename, null terminated
  36. bool protected_fs_file::cleanup_filename(const char* src, char* dest)
  37. {
  38. const char* p = src;
  39. const char* name = src;
  40. while ((*p) != '\0')
  41. {
  42. if ((*p) == '\\' || (*p) == '/')
  43. name = p+1;
  44. p++;
  45. }
  46. if (strnlen(name, FILENAME_MAX_LEN) >= FILENAME_MAX_LEN-1)
  47. {
  48. last_error = ENAMETOOLONG;
  49. return false;
  50. }
  51. strncpy(dest, name, FILENAME_MAX_LEN-1);
  52. dest[FILENAME_MAX_LEN-1] = '\0';
  53. if (strnlen(dest, 1) == 0)
  54. {
  55. last_error = EINVAL;
  56. return false;
  57. }
  58. return true;
  59. }
  60. protected_fs_file::protected_fs_file(const char* filename, const char* mode, const sgx_aes_gcm_128bit_key_t* import_key, const sgx_aes_gcm_128bit_key_t* kdk_key)
  61. {
  62. sgx_status_t status = SGX_SUCCESS;
  63. uint8_t result = 0;
  64. int32_t result32 = 0;
  65. init_fields();
  66. if (filename == NULL || mode == NULL ||
  67. strnlen(filename, 1) == 0 || strnlen(mode, 1) == 0)
  68. {
  69. last_error = EINVAL;
  70. return;
  71. }
  72. if (strnlen(filename, FULLNAME_MAX_LEN) >= FULLNAME_MAX_LEN - 1)
  73. {
  74. last_error = ENAMETOOLONG;
  75. return;
  76. }
  77. if (import_key != NULL && kdk_key != NULL)
  78. {// import key is used only with auto generated keys
  79. last_error = EINVAL;
  80. return;
  81. }
  82. status = sgx_create_report(NULL, NULL, &report);
  83. if (status != SGX_SUCCESS)
  84. {
  85. last_error = status;
  86. return;
  87. }
  88. result32 = sgx_thread_mutex_init(&mutex, NULL);
  89. if (result32 != 0)
  90. {
  91. last_error = result32;
  92. return;
  93. }
  94. if (init_session_master_key() == false)
  95. // last_error already set
  96. return;
  97. if (kdk_key != NULL)
  98. {
  99. // for new file, this value will later be saved in the meta data plain part (init_new_file)
  100. // for existing file, we will later compare this value with the value from the file (init_existing_file)
  101. use_user_kdk_key = 1;
  102. memcpy(user_kdk_key, kdk_key, sizeof(sgx_aes_gcm_128bit_key_t));
  103. }
  104. // get the clean file name (original name might be clean or with relative path or with absolute path...)
  105. char clean_filename[FILENAME_MAX_LEN];
  106. if (cleanup_filename(filename, clean_filename) == false)
  107. // last_error already set
  108. return;
  109. if (import_key != NULL)
  110. {// verify the key is not empty - note from SAFE review
  111. sgx_aes_gcm_128bit_key_t empty_aes_key = {0};
  112. if (consttime_memequal(import_key, &empty_aes_key, sizeof(sgx_aes_gcm_128bit_key_t)) == 1)
  113. {
  114. last_error = EINVAL;
  115. return;
  116. }
  117. }
  118. if (parse_mode(mode) == false)
  119. {
  120. last_error = EINVAL;
  121. return;
  122. }
  123. status = u_sgxprotectedfs_check_if_file_exists(&result, filename); // if result == 1 --> file exists
  124. if (status != SGX_SUCCESS)
  125. {
  126. last_error = status;
  127. return;
  128. }
  129. if (open_mode.write == 1 && result == 1)
  130. {// try to delete existing file
  131. int32_t saved_errno = 0;
  132. result32 = remove(filename);
  133. if (result32 != 0)
  134. {
  135. // either can't delete or the file was already deleted by someone else
  136. saved_errno = errno;
  137. errno = 0;
  138. }
  139. // re-check
  140. status = u_sgxprotectedfs_check_if_file_exists(&result, filename);
  141. if (status != SGX_SUCCESS || result == 1)
  142. {
  143. last_error = (status != SGX_SUCCESS) ? status :
  144. (saved_errno != 0) ? saved_errno : EACCES;
  145. return;
  146. }
  147. }
  148. if (open_mode.read == 1 && result == 0)
  149. {// file must exists
  150. last_error = ENOENT;
  151. return;
  152. }
  153. if (import_key != NULL && result == 0)
  154. {// file must exists - otherwise the user key is not used
  155. last_error = ENOENT;
  156. return;
  157. }
  158. // now open the file
  159. read_only = (open_mode.read == 1 && open_mode.update == 0); // read only files can be opened simultaneously by many enclaves
  160. do {
  161. status = u_sgxprotectedfs_exclusive_file_open(&file, filename, read_only, &real_file_size, &result32);
  162. if (status != SGX_SUCCESS || file == NULL)
  163. {
  164. last_error = (status != SGX_SUCCESS) ? status :
  165. (result32 != 0) ? result32 : EACCES;
  166. break;
  167. }
  168. if (real_file_size < 0)
  169. {
  170. last_error = EINVAL;
  171. break;
  172. }
  173. if (real_file_size % NODE_SIZE != 0)
  174. {
  175. last_error = SGX_ERROR_FILE_NOT_SGX_FILE;
  176. break;
  177. }
  178. strncpy(recovery_filename, filename, FULLNAME_MAX_LEN - 1); // copy full file name
  179. recovery_filename[FULLNAME_MAX_LEN - 1] = '\0'; // just to be safe
  180. size_t full_name_len = strnlen(recovery_filename, RECOVERY_FILE_MAX_LEN);
  181. strncpy(&recovery_filename[full_name_len], "_recovery", 10);
  182. if (real_file_size > 0)
  183. {// existing file
  184. if (open_mode.write == 1) // redundant check, just in case
  185. {
  186. last_error = EACCES;
  187. break;
  188. }
  189. if (init_existing_file(filename, clean_filename, import_key) == false)
  190. break;
  191. if (open_mode.append == 1 && open_mode.update == 0)
  192. offset = encrypted_part_plain.size;
  193. }
  194. else
  195. {// new file
  196. if (init_new_file(clean_filename) == false)
  197. break;
  198. }
  199. file_status = SGX_FILE_STATUS_OK;
  200. } while(0);
  201. if (file_status != SGX_FILE_STATUS_OK)
  202. {
  203. if (file != NULL)
  204. {
  205. u_sgxprotectedfs_fclose(&result32, file); // we don't care about the result
  206. file = NULL;
  207. }
  208. }
  209. }
  210. void protected_fs_file::init_fields()
  211. {
  212. meta_data_node_number = 0;
  213. memset(&file_meta_data, 0, sizeof(meta_data_node_t));
  214. memset(&encrypted_part_plain, 0, sizeof(meta_data_encrypted_t));
  215. memset(&empty_iv, 0, sizeof(sgx_iv_t));
  216. memset(&root_mht, 0, sizeof(file_mht_node_t));
  217. root_mht.type = FILE_MHT_NODE_TYPE;
  218. root_mht.physical_node_number = 1;
  219. root_mht.mht_node_number = 0;
  220. root_mht.new_node = true;
  221. root_mht.need_writing = false;
  222. offset = 0;
  223. file = NULL;
  224. end_of_file = false;
  225. need_writing = false;
  226. read_only = 0;
  227. file_status = SGX_FILE_STATUS_NOT_INITIALIZED;
  228. last_error = SGX_SUCCESS;
  229. real_file_size = 0;
  230. open_mode.raw = 0;
  231. use_user_kdk_key = 0;
  232. master_key_count = 0;
  233. recovery_filename[0] = '\0';
  234. memset(&mutex, 0, sizeof(sgx_thread_mutex_t));
  235. // set hash size to fit MAX_PAGES_IN_CACHE
  236. cache.rehash(MAX_PAGES_IN_CACHE);
  237. }
  238. #define MAX_MODE_STRING_LEN 5
  239. bool protected_fs_file::parse_mode(const char* mode)
  240. {
  241. if (mode == NULL) // re-check
  242. return false;
  243. size_t mode_len = strnlen(mode, MAX_MODE_STRING_LEN+1);
  244. if (mode_len > MAX_MODE_STRING_LEN)
  245. return false;
  246. for (size_t i = 0 ; i < mode_len ; i++)
  247. {
  248. switch (mode[i])
  249. {
  250. case 'r':
  251. if (open_mode.write == 1 || open_mode.read == 1 || open_mode.append == 1)
  252. return false;
  253. open_mode.read = 1;
  254. break;
  255. case 'w':
  256. if (open_mode.write == 1 || open_mode.read == 1 || open_mode.append == 1)
  257. return false;
  258. open_mode.write = 1;
  259. break;
  260. case 'a':
  261. if (open_mode.write == 1 || open_mode.read == 1 || open_mode.append == 1)
  262. return false;
  263. open_mode.append = 1;
  264. break;
  265. case 'b':
  266. if (open_mode.binary == 1)
  267. return false;
  268. open_mode.binary = 1;
  269. break;
  270. case '+':
  271. if (open_mode.update == 1)
  272. return false;
  273. open_mode.update = 1;
  274. break;
  275. default:
  276. return false;
  277. }
  278. }
  279. if (open_mode.write == 0 && open_mode.read == 0 && open_mode.append == 0)
  280. return false;
  281. return true;
  282. }
  283. bool protected_fs_file::file_recovery(const char* filename)
  284. {
  285. sgx_status_t status = SGX_SUCCESS;
  286. int32_t result32 = 0;
  287. int64_t new_file_size = 0;
  288. status = u_sgxprotectedfs_fclose(&result32, file);
  289. if (status != SGX_SUCCESS || result32 != 0)
  290. {
  291. last_error = (status != SGX_SUCCESS) ? status :
  292. (result32 != -1) ? result32 : EINVAL;
  293. return false;
  294. }
  295. file = NULL;
  296. status = u_sgxprotectedfs_do_file_recovery(&result32, filename, recovery_filename, NODE_SIZE);
  297. if (status != SGX_SUCCESS || result32 != 0)
  298. {
  299. last_error = (status != SGX_SUCCESS) ? status :
  300. (result32 != -1) ? result32 : EINVAL;
  301. return false;
  302. }
  303. status = u_sgxprotectedfs_exclusive_file_open(&file, filename, read_only, &new_file_size, &result32);
  304. if (status != SGX_SUCCESS || file == NULL)
  305. {
  306. last_error = (status != SGX_SUCCESS) ? status :
  307. (result32 != 0) ? result32 : EACCES;
  308. return false;
  309. }
  310. // recovery only change existing data, it does not shrink or grow the file
  311. if (new_file_size != real_file_size)
  312. {
  313. last_error = SGX_ERROR_UNEXPECTED;
  314. return false;
  315. }
  316. status = u_sgxprotectedfs_fread_node(&result32, file, 0, (uint8_t*)&file_meta_data, NODE_SIZE);
  317. if (status != SGX_SUCCESS || result32 != 0)
  318. {
  319. last_error = (status != SGX_SUCCESS) ? status :
  320. (result32 != -1) ? result32 : EIO;
  321. return false;
  322. }
  323. return true;
  324. }
  325. bool protected_fs_file::init_existing_file(const char* filename, const char* clean_filename, const sgx_aes_gcm_128bit_key_t* import_key)
  326. {
  327. sgx_status_t status;
  328. int32_t result32;
  329. // read meta-data node
  330. status = u_sgxprotectedfs_fread_node(&result32, file, 0, (uint8_t*)&file_meta_data, NODE_SIZE);
  331. if (status != SGX_SUCCESS || result32 != 0)
  332. {
  333. last_error = (status != SGX_SUCCESS) ? status :
  334. (result32 != -1) ? result32 : EIO;
  335. return false;
  336. }
  337. if (file_meta_data.plain_part.file_id != SGX_FILE_ID)
  338. {// such a file exists, but it is not an SGX file
  339. last_error = SGX_ERROR_FILE_NOT_SGX_FILE;
  340. return false;
  341. }
  342. if (file_meta_data.plain_part.major_version != SGX_FILE_MAJOR_VERSION)
  343. {
  344. last_error = ENOTSUP;
  345. return false;
  346. }
  347. if (file_meta_data.plain_part.update_flag == 1)
  348. {// file was in the middle of an update, must do a recovery
  349. if (file_recovery(filename) == false)
  350. {// override internal error
  351. last_error = SGX_ERROR_FILE_RECOVERY_NEEDED;
  352. return false;
  353. }
  354. if (file_meta_data.plain_part.update_flag == 1) // recovery failed, flag is still set!
  355. {// recovery didn't clear the flag
  356. last_error = SGX_ERROR_FILE_RECOVERY_NEEDED;
  357. return false;
  358. }
  359. // re-check after recovery
  360. if (file_meta_data.plain_part.major_version != SGX_FILE_MAJOR_VERSION)
  361. {
  362. last_error = ENOTSUP;
  363. return false;
  364. }
  365. }
  366. if (file_meta_data.plain_part.use_user_kdk_key != use_user_kdk_key)
  367. {
  368. last_error = EINVAL;
  369. return false;
  370. }
  371. if (restore_current_meta_data_key(import_key) == false)
  372. return false;
  373. // decrypt the encrypted part of the meta-data
  374. status = sgx_rijndael128GCM_decrypt(&cur_key,
  375. (const uint8_t*)file_meta_data.encrypted_part, sizeof(meta_data_encrypted_blob_t), (uint8_t*)&encrypted_part_plain,
  376. empty_iv, SGX_AESGCM_IV_SIZE,
  377. NULL, 0,
  378. &file_meta_data.plain_part.meta_data_gmac);
  379. if (status != SGX_SUCCESS)
  380. {
  381. last_error = status;
  382. return false;
  383. }
  384. if (strncmp(clean_filename, encrypted_part_plain.clean_filename, FILENAME_MAX_LEN) != 0)
  385. {
  386. last_error = SGX_ERROR_FILE_NAME_MISMATCH;
  387. return false;
  388. }
  389. /*
  390. sgx_mc_uuid_t empty_mc_uuid = {0};
  391. // check if the file contains an active monotonic counter
  392. if (consttime_memequal(&empty_mc_uuid, &encrypted_part_plain.mc_uuid, sizeof(sgx_mc_uuid_t)) == 0)
  393. {
  394. uint32_t mc_value = 0;
  395. status = sgx_read_monotonic_counter(&encrypted_part_plain.mc_uuid, &mc_value);
  396. if (status != SGX_SUCCESS)
  397. {
  398. last_error = status;
  399. return false;
  400. }
  401. if (encrypted_part_plain.mc_value < mc_value)
  402. {
  403. last_error = SGX_ERROR_FILE_MONOTONIC_COUNTER_IS_BIGGER;
  404. return false;
  405. }
  406. if (encrypted_part_plain.mc_value == mc_value + 1) // can happen if AESM failed - file value stayed one higher
  407. {
  408. sgx_status_t status = sgx_increment_monotonic_counter(&encrypted_part_plain.mc_uuid, &mc_value);
  409. if (status != SGX_SUCCESS)
  410. {
  411. file_status = SGX_FILE_STATUS_MC_NOT_INCREMENTED;
  412. last_error = status;
  413. return false;
  414. }
  415. }
  416. if (encrypted_part_plain.mc_value != mc_value)
  417. {
  418. file_status = SGX_FILE_STATUS_CORRUPTED;
  419. last_error = SGX_ERROR_UNEXPECTED;
  420. return false;
  421. }
  422. }
  423. else
  424. {
  425. assert(encrypted_part_plain.mc_value == 0);
  426. encrypted_part_plain.mc_value = 0; // do this anyway for release...
  427. }
  428. */
  429. if (encrypted_part_plain.size > MD_USER_DATA_SIZE)
  430. {
  431. // read the root node of the mht
  432. status = u_sgxprotectedfs_fread_node(&result32, file, 1, root_mht.encrypted.cipher, NODE_SIZE);
  433. if (status != SGX_SUCCESS || result32 != 0)
  434. {
  435. last_error = (status != SGX_SUCCESS) ? status :
  436. (result32 != -1) ? result32 : EIO;
  437. return false;
  438. }
  439. // this also verifies the root mht gmac against the gmac in the meta-data encrypted part
  440. status = sgx_rijndael128GCM_decrypt(&encrypted_part_plain.mht_key,
  441. root_mht.encrypted.cipher, NODE_SIZE, (uint8_t*)&root_mht.plain,
  442. empty_iv, SGX_AESGCM_IV_SIZE, NULL, 0, &encrypted_part_plain.mht_gmac);
  443. if (status != SGX_SUCCESS)
  444. {
  445. last_error = status;
  446. return false;
  447. }
  448. root_mht.new_node = false;
  449. }
  450. return true;
  451. }
  452. bool protected_fs_file::init_new_file(const char* clean_filename)
  453. {
  454. file_meta_data.plain_part.file_id = SGX_FILE_ID;
  455. file_meta_data.plain_part.major_version = SGX_FILE_MAJOR_VERSION;
  456. file_meta_data.plain_part.minor_version = SGX_FILE_MINOR_VERSION;
  457. file_meta_data.plain_part.use_user_kdk_key = use_user_kdk_key;
  458. strncpy(encrypted_part_plain.clean_filename, clean_filename, FILENAME_MAX_LEN);
  459. need_writing = true;
  460. return true;
  461. }
  462. protected_fs_file::~protected_fs_file()
  463. {
  464. void* data;
  465. while ((data = cache.get_last()) != NULL)
  466. {
  467. if (((file_data_node_t*)data)->type == FILE_DATA_NODE_TYPE) // type is in the same offset in both node types, need to scrub the plaintext
  468. {
  469. file_data_node_t* file_data_node = (file_data_node_t*)data;
  470. memset_s(&file_data_node->plain, sizeof(data_node_t), 0, sizeof(data_node_t));
  471. delete file_data_node;
  472. }
  473. else
  474. {
  475. file_mht_node_t* file_mht_node = (file_mht_node_t*)data;
  476. memset_s(&file_mht_node->plain, sizeof(mht_node_t), 0, sizeof(mht_node_t));
  477. delete file_mht_node;
  478. }
  479. cache.remove_last();
  480. }
  481. // scrub the last encryption key and the session key
  482. memset_s(&cur_key, sizeof(sgx_aes_gcm_128bit_key_t), 0, sizeof(sgx_aes_gcm_128bit_key_t));
  483. memset_s(&session_master_key, sizeof(sgx_aes_gcm_128bit_key_t), 0, sizeof(sgx_aes_gcm_128bit_key_t));
  484. // scrub first 3KB of user data and the gmac_key
  485. memset_s(&encrypted_part_plain, sizeof(meta_data_encrypted_t), 0, sizeof(meta_data_encrypted_t));
  486. sgx_thread_mutex_destroy(&mutex);
  487. }
  488. bool protected_fs_file::pre_close(sgx_key_128bit_t* key, bool import)
  489. {
  490. int32_t result32 = 0;
  491. bool retval = true;
  492. sgx_status_t status = SGX_SUCCESS;
  493. sgx_thread_mutex_lock(&mutex);
  494. if (import == true)
  495. {
  496. if (use_user_kdk_key == 1) // import file is only needed for auto-key
  497. retval = false;
  498. else
  499. need_writing = true; // will re-encrypt the neta-data node with local key
  500. }
  501. if (file_status != SGX_FILE_STATUS_OK)
  502. {
  503. sgx_thread_mutex_unlock(&mutex);
  504. clear_error(); // last attempt to fix it
  505. sgx_thread_mutex_lock(&mutex);
  506. }
  507. else // file_status == SGX_FILE_STATUS_OK
  508. {
  509. internal_flush(/*false,*/ true);
  510. }
  511. if (file_status != SGX_FILE_STATUS_OK)
  512. retval = false;
  513. if (file != NULL)
  514. {
  515. status = u_sgxprotectedfs_fclose(&result32, file);
  516. if (status != SGX_SUCCESS || result32 != 0)
  517. {
  518. last_error = (status != SGX_SUCCESS) ? status :
  519. (result32 != -1) ? result32 : SGX_ERROR_FILE_CLOSE_FAILED;
  520. retval = false;
  521. }
  522. file = NULL;
  523. }
  524. if (file_status == SGX_FILE_STATUS_OK &&
  525. last_error == SGX_SUCCESS) // else...maybe something bad happened and the recovery file will be needed
  526. erase_recovery_file();
  527. if (key != NULL)
  528. {
  529. if (use_user_kdk_key == 1) // export key is only used for auto-key
  530. {
  531. retval = false;
  532. }
  533. else
  534. {
  535. if (restore_current_meta_data_key(NULL) == true)
  536. memcpy(key, cur_key, sizeof(sgx_key_128bit_t));
  537. else
  538. retval = false;
  539. }
  540. }
  541. file_status = SGX_FILE_STATUS_CLOSED;
  542. sgx_thread_mutex_unlock(&mutex);
  543. return retval;
  544. }