enclave.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 "enclave.h"
  32. #include "util.h"
  33. #include "se_detect.h"
  34. #include "enclave_creator.h"
  35. #include "sgx_error.h"
  36. #include "se_error_internal.h"
  37. #include "debugger_support.h"
  38. #include "se_memory.h"
  39. #include <assert.h>
  40. using namespace std;
  41. int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread);
  42. int do_ocall(const bridge_fn_t bridge, void *ms);
  43. CEnclave::CEnclave(CLoader &ldr)
  44. : m_loader(ldr)
  45. , m_enclave_id(0)
  46. , m_start_addr(NULL)
  47. , m_size(0)
  48. , m_power_event_flag(0)
  49. , m_ref(0)
  50. , m_zombie(false)
  51. , m_thread_pool(NULL)
  52. , m_dbg_flag(false)
  53. , m_destroyed(false)
  54. {
  55. memset(&m_enclave_info, 0, sizeof(debug_enclave_info_t));
  56. se_init_rwlock(&m_rwlock);
  57. }
  58. sgx_status_t CEnclave::initialize(const se_file_t& file, const sgx_enclave_id_t enclave_id, void * const start_addr, const uint64_t enclave_size, const uint32_t tcs_policy)
  59. {
  60. uint32_t name_len = file.name_len;
  61. if (file.unicode)
  62. name_len *= (uint32_t)sizeof(wchar_t);
  63. const int buf_len = name_len + 4; //+4, because we need copy the charactor of string end ('\0').;
  64. m_enclave_info.lpFileName = calloc(1, buf_len);
  65. if (m_enclave_info.lpFileName == NULL)
  66. return SGX_ERROR_OUT_OF_MEMORY;
  67. memcpy_s(m_enclave_info.lpFileName, name_len, file.name, name_len);
  68. m_enclave_info.unicode = file.unicode?0:1;
  69. m_enclave_info.file_name_size = name_len;
  70. m_enclave_info.struct_version = DEBUG_INFO_STRUCT_VERSION;
  71. m_enclave_id = enclave_id;
  72. m_start_addr = start_addr;
  73. m_size = enclave_size;
  74. if(TCS_POLICY_BIND == tcs_policy)
  75. {
  76. m_thread_pool = new CThreadPoolBindMode();
  77. }
  78. else if(TCS_POLICY_UNBIND == tcs_policy)
  79. {
  80. //we also set it as bind mode.
  81. m_thread_pool = new CThreadPoolUnBindMode();
  82. }
  83. else
  84. {
  85. SE_TRACE(SE_TRACE_WARNING, "BUG: unknown tcs policy\n");
  86. //Should NOT run here, because we have validate the metadata before.
  87. free(m_enclave_info.lpFileName);
  88. m_enclave_info.lpFileName = NULL;
  89. return SGX_ERROR_INVALID_PARAMETER;
  90. }
  91. return SGX_SUCCESS;
  92. }
  93. CEnclave::~CEnclave()
  94. {
  95. if (m_thread_pool)
  96. {
  97. delete m_thread_pool;
  98. m_thread_pool = NULL;
  99. }
  100. destory_debug_info(&m_enclave_info);
  101. se_fini_rwlock(&m_rwlock);
  102. }
  103. void * CEnclave::get_symbol_address(const char * const symbol)
  104. {
  105. return m_loader.get_symbol_address(symbol);
  106. }
  107. sgx_enclave_id_t CEnclave::get_enclave_id()
  108. {
  109. return m_enclave_id;
  110. }
  111. sgx_status_t CEnclave::error_trts2urts(unsigned int trts_error)
  112. {
  113. if(trts_error == (unsigned int)SE_ERROR_READ_LOCK_FAIL)
  114. {
  115. return SGX_ERROR_ENCLAVE_LOST;
  116. }
  117. //tRTS may directly return the external error code, so we don't need transfer it.
  118. if(EXTERNAL_ERROR != (trts_error >> MAIN_MOD_SHIFT))
  119. {
  120. SE_TRACE(SE_TRACE_WARNING, "trts return error %x, it should be urts/trts bug\n", trts_error);
  121. return SGX_ERROR_UNEXPECTED;
  122. }
  123. return (sgx_status_t)trts_error;
  124. }
  125. sgx_status_t CEnclave::ecall(const int proc, const void *ocall_table, void *ms)
  126. {
  127. if(se_try_rdlock(&m_rwlock))
  128. {
  129. //Maybe the enclave has been destroyed after acquire/release m_rwlock. See CEnclave::destroy()
  130. if(m_destroyed)
  131. {
  132. se_rdunlock(&m_rwlock);
  133. return SGX_ERROR_ENCLAVE_LOST;
  134. }
  135. //do sgx_ecall
  136. CTrustThread *trust_thread = get_tcs();
  137. unsigned ret = SGX_ERROR_OUT_OF_TCS;
  138. {
  139. if(NULL != trust_thread) {
  140. ret = do_ecall(proc, ocall_table, ms, trust_thread);
  141. }
  142. }
  143. {
  144. put_tcs(trust_thread);
  145. //release the read/write lock, the only exception is enclave already be removed in ocall
  146. if(AbnormalTermination() || ret != SE_ERROR_READ_LOCK_FAIL)
  147. {
  148. se_rdunlock(&m_rwlock);
  149. }
  150. }
  151. return error_trts2urts(ret);
  152. } else {
  153. return SGX_ERROR_ENCLAVE_LOST;
  154. }
  155. }
  156. int CEnclave::ocall(const unsigned int proc, const sgx_ocall_table_t *ocall_table, void *ms)
  157. {
  158. int error = SGX_ERROR_UNEXPECTED;
  159. //validate the proc is within ocall_table;
  160. if(NULL == ocall_table
  161. || proc >= ocall_table->count)
  162. {
  163. return SGX_ERROR_INVALID_FUNCTION;
  164. }
  165. se_rdunlock(&m_rwlock);
  166. bridge_fn_t bridge = reinterpret_cast<bridge_fn_t>(ocall_table->ocall[proc]);
  167. error = do_ocall(bridge, ms);
  168. if (!se_try_rdlock(&m_rwlock))
  169. {
  170. //Probablly the enclave has been destroyed, so we can't get the read lock.
  171. error = SE_ERROR_READ_LOCK_FAIL;
  172. }
  173. //We have m_destroyed to determinate if the enclave has been destroyed.
  174. else if(m_destroyed)
  175. {
  176. //Enclave has been destroyed, emulate that we fail to get read lock.
  177. se_rdunlock(&m_rwlock);
  178. error = SE_ERROR_READ_LOCK_FAIL;
  179. }
  180. return error;
  181. }
  182. const debug_enclave_info_t* CEnclave::get_debug_info()
  183. {
  184. return &m_enclave_info;
  185. }
  186. CTrustThread * CEnclave::get_tcs()
  187. {
  188. CTrustThread *trust_thread = m_thread_pool->acquire_thread();
  189. return trust_thread;
  190. }
  191. void CEnclave::put_tcs(CTrustThread *trust_thread)
  192. {
  193. if(NULL == trust_thread)
  194. {
  195. return;
  196. }
  197. m_thread_pool->release_thread(trust_thread);
  198. }
  199. void CEnclave::destroy()
  200. {
  201. se_wtlock(&m_rwlock);
  202. //send debug event to debugger when enclave is debug mode or release mode
  203. debug_enclave_info_t *debug_info = const_cast<debug_enclave_info_t *>(get_debug_info());
  204. generate_enclave_debug_event(URTS_EXCEPTION_PREREMOVEENCLAVE, debug_info);
  205. get_enclave_creator()->destroy_enclave(ENCLAVE_ID_IOCTL, m_size);
  206. m_destroyed = true;
  207. //We are going to destory m_rwlock. At this point, maybe an ecall is in progress, and try to get m_rwlock.
  208. //To prevent such ecall, we use m_destroyed to identify that the no ecall should going on. See CEnclave::ecall(...).
  209. //For new ecall to the enclave, it will return with SGX_ERROR_INVALID_ENCLAVE_ID immediately.
  210. se_wtunlock(&m_rwlock);
  211. // We should not use loader to destroy encalve because loader has been removed after successful enclave loading
  212. //m_loader.destroy_enclave();
  213. }
  214. void CEnclave::add_thread(tcs_t * const tcs)
  215. {
  216. CTrustThread *trust_thread = m_thread_pool->add_thread(tcs, this);
  217. insert_debug_tcs_info_head(&m_enclave_info, trust_thread->get_debug_info());
  218. }
  219. int CEnclave::set_extra_debug_info(secs_t& secs)
  220. {
  221. void *g_peak_heap_used_addr = get_symbol_address("g_peak_heap_used");
  222. m_enclave_info.g_peak_heap_used_addr = g_peak_heap_used_addr;
  223. m_enclave_info.start_addr = secs.base;
  224. m_enclave_info.misc_select = secs.misc_select;
  225. if(g_peak_heap_used_addr == NULL)
  226. {
  227. SE_TRACE(SE_TRACE_DEBUG, "Symbol 'g_peak_heap_used' is not found\n");
  228. //This error should not break loader and debugger, so the upper layer function will ignore it.
  229. return SGX_ERROR_INVALID_ENCLAVE;
  230. }
  231. return SGX_SUCCESS;
  232. }
  233. void CEnclave::push_ocall_frame(ocall_frame_t* frame_point, CTrustThread *trust_thread)
  234. {
  235. if(NULL == trust_thread)
  236. {
  237. return;
  238. }
  239. trust_thread->push_ocall_frame(frame_point);
  240. }
  241. void CEnclave::pop_ocall_frame(CTrustThread *trust_thread)
  242. {
  243. if(NULL == trust_thread)
  244. {
  245. return;
  246. }
  247. trust_thread->pop_ocall_frame();
  248. }
  249. CEnclavePool CEnclavePool::m_instance;
  250. CEnclavePool::CEnclavePool()
  251. {
  252. m_enclave_list = NULL;
  253. se_mutex_init(&m_enclave_mutex);
  254. SE_TRACE(SE_TRACE_NOTICE, "enter CEnclavePool constructor\n");
  255. }
  256. CEnclavePool *CEnclavePool::instance()
  257. {
  258. return &m_instance;
  259. }
  260. int CEnclavePool::add_enclave(CEnclave *enclave)
  261. {
  262. int result = TRUE;
  263. se_mutex_lock(&m_enclave_mutex);
  264. if (m_enclave_list == NULL) {
  265. m_enclave_list = new Node<sgx_enclave_id_t, CEnclave*>(enclave->get_enclave_id(), enclave);
  266. } else {
  267. Node<sgx_enclave_id_t, CEnclave*>* node = new Node<sgx_enclave_id_t, CEnclave*>(enclave->get_enclave_id(), enclave);
  268. if (m_enclave_list->InsertNext(node) == false) {
  269. delete node;
  270. SE_TRACE(SE_TRACE_WARNING, "the encalve %llx has already been added\n", enclave->get_enclave_id());
  271. result = FALSE;
  272. }
  273. }
  274. se_mutex_unlock(&m_enclave_mutex);
  275. return result;
  276. }
  277. CEnclave * CEnclavePool::get_enclave(const sgx_enclave_id_t enclave_id)
  278. {
  279. se_mutex_lock(&m_enclave_mutex);
  280. Node<sgx_enclave_id_t, CEnclave*>* it = m_enclave_list->Find(enclave_id);
  281. if(it != NULL)
  282. {
  283. se_mutex_unlock(&m_enclave_mutex);
  284. return it->value;
  285. }
  286. else
  287. {
  288. se_mutex_unlock(&m_enclave_mutex);
  289. return NULL;
  290. }
  291. }
  292. CEnclave * CEnclavePool::ref_enclave(const sgx_enclave_id_t enclave_id)
  293. {
  294. se_mutex_lock(&m_enclave_mutex);
  295. Node<sgx_enclave_id_t, CEnclave*>* it = m_enclave_list->Find(enclave_id);
  296. if(it != NULL)
  297. {
  298. it->value->atomic_inc_ref();
  299. se_mutex_unlock(&m_enclave_mutex);
  300. return it->value;
  301. }
  302. else
  303. {
  304. se_mutex_unlock(&m_enclave_mutex);
  305. return NULL;
  306. }
  307. }
  308. void CEnclavePool::unref_enclave(CEnclave *enclave)
  309. {
  310. //We use enclave pool lock to protect data, the lock is big, but is more secure.
  311. se_mutex_lock(&m_enclave_mutex);
  312. //The ref is increased in ref_enclave;
  313. uint32_t ref = enclave->atomic_dec_ref();
  314. //If the enclave is in zombie state, the HW enclave must have been destroyed.
  315. //And if the enclave is not referenced, the enclave instance will not be referenced any more,
  316. //so we delete the instance.
  317. //Another code path that delete enclave instance is in function "CEnclavePool::remove_enclave"
  318. if(enclave->is_zombie() && !ref)
  319. delete enclave;
  320. se_mutex_unlock(&m_enclave_mutex);
  321. }
  322. se_handle_t CEnclavePool::get_event(const void * const tcs)
  323. {
  324. se_handle_t hevent = NULL;
  325. CEnclave *enclave = NULL;
  326. assert(tcs != NULL);
  327. se_mutex_lock(&m_enclave_mutex);
  328. Node<sgx_enclave_id_t, CEnclave*>* it = m_enclave_list;
  329. for(; it != NULL; it = it->next)
  330. {
  331. void *start = it->value->get_start_address();
  332. void *end = GET_PTR(void, start, it->value->get_size());
  333. /* check start & end */
  334. if (tcs >= start && tcs < end) {
  335. enclave = it->value;
  336. break;
  337. }
  338. }
  339. if (NULL != enclave)
  340. {
  341. CTrustThreadPool *pool = enclave->get_thread_pool();
  342. if (pool != NULL)
  343. {
  344. CTrustThread *thread = pool->get_bound_thread((const tcs_t *)tcs);
  345. if (thread != NULL)
  346. hevent = thread->get_event();
  347. }
  348. }
  349. se_mutex_unlock(&m_enclave_mutex);
  350. return hevent;
  351. }
  352. CEnclave* CEnclavePool::remove_enclave(const sgx_enclave_id_t enclave_id, sgx_status_t &status)
  353. {
  354. status = SGX_SUCCESS;
  355. se_mutex_lock(&m_enclave_mutex);
  356. CEnclave *enclave = get_enclave(enclave_id);
  357. if(NULL == enclave)
  358. {
  359. status = SGX_ERROR_INVALID_ENCLAVE_ID;
  360. SE_TRACE(SE_TRACE_WARNING, "remove an unkonwn enclave\n");
  361. se_mutex_unlock(&m_enclave_mutex);
  362. return enclave;
  363. }
  364. enclave->destroy();
  365. //the ref is not 0, maybe some thread is in sgx_ocall, so we can NOT delete enclave instance.
  366. if(enclave->get_ref())
  367. {
  368. enclave->mark_zombie();
  369. /* When destroy the enclave, all threads that are waiting/about to wait
  370. * on untrusted event need to be waked. Otherwise, they will be always
  371. * pending on the untrusted events, and app need to manually kill the threads.
  372. */
  373. CTrustThreadPool *pool = enclave->get_thread_pool();
  374. pool->wake_threads();
  375. enclave = NULL;
  376. }
  377. Node<sgx_enclave_id_t, CEnclave*>* it = m_enclave_list->Remove(enclave_id);
  378. if (it == m_enclave_list)
  379. m_enclave_list = it->next;
  380. delete it;
  381. se_mutex_unlock(&m_enclave_mutex);
  382. return enclave;
  383. }
  384. void CEnclavePool::notify_debugger()
  385. {
  386. se_mutex_lock(&m_enclave_mutex);
  387. if(m_enclave_list!= NULL)
  388. {
  389. Node<sgx_enclave_id_t, CEnclave*>* it = m_enclave_list;
  390. for(; it != NULL; it = it->next)
  391. {
  392. //send debug event to debugger when enclave is debug mode or release mode
  393. debug_enclave_info_t * debug_info = const_cast<debug_enclave_info_t*>((it->value)->get_debug_info());
  394. generate_enclave_debug_event(URTS_EXCEPTION_PREREMOVEENCLAVE, debug_info);
  395. }
  396. }
  397. se_mutex_unlock(&m_enclave_mutex);
  398. }
  399. bool CEnclave::update_trust_thread_debug_flag(void* tcs_address, uint8_t debug_flag)
  400. {
  401. uint64_t debug_flag2 = (uint64_t)debug_flag;
  402. debug_enclave_info_t *debug_info = NULL;
  403. debug_info = const_cast<debug_enclave_info_t *>(get_debug_info());
  404. pid_t pid = getpid();
  405. if(debug_info->enclave_type == ET_DEBUG)
  406. {
  407. if(!se_write_process_mem(pid, reinterpret_cast<unsigned char *>(tcs_address) + sizeof(uint64_t), &debug_flag2, sizeof(uint64_t), NULL))
  408. return FALSE;
  409. }
  410. return TRUE;
  411. }
  412. bool CEnclave::update_debug_flag(uint8_t debug_flag)
  413. {
  414. debug_tcs_info_t* tcs_list_entry = m_enclave_info.tcs_list;
  415. while(tcs_list_entry)
  416. {
  417. if(!update_trust_thread_debug_flag(tcs_list_entry->TCS_address, debug_flag))
  418. return FALSE;
  419. tcs_list_entry = tcs_list_entry->next_tcs_info;
  420. }
  421. return TRUE;
  422. }