aesm_util.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2011-2016 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 "memset_s.h"
  32. #include "persistent_storage_info.h"
  33. #include "oal/aesm_persistent_storage.h"
  34. #include "internal/se_stdio.h"
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/stat.h>
  39. #include <dlfcn.h>
  40. static ae_error_t aesm_get_path(
  41. const char *p_file_name,
  42. char *p_file_path,
  43. size_t buf_size)
  44. {
  45. if(!p_file_name || !p_file_path)
  46. return OAL_PARAMETER_ERROR;
  47. Dl_info dl_info;
  48. if(0==dladdr(__builtin_return_address(0), &dl_info)||
  49. NULL==dl_info.dli_fname)
  50. return AE_FAILURE;
  51. if(strlen(dl_info.dli_fname)>buf_size-1)
  52. return OAL_PATHNAME_BUFFER_OVERFLOW_ERROR;
  53. strncpy(p_file_path,dl_info.dli_fname,buf_size);
  54. char* p_last_slash = strrchr(p_file_path, '/' );
  55. if ( p_last_slash != NULL )
  56. {
  57. p_last_slash++; //increment beyond the last slash
  58. *p_last_slash = '\0'; //null terminate the string
  59. }
  60. else p_file_path[0] = '\0';
  61. if(strlen(p_file_path)+strlen(p_file_name)+sizeof(char)>buf_size)
  62. return OAL_PATHNAME_BUFFER_OVERFLOW_ERROR;
  63. strncat(p_file_path,p_file_name, strlen(p_file_name));
  64. return AE_SUCCESS;
  65. }
  66. #define AESM_DATA_FOLDER "/var/opt/aesmd/data/"
  67. static ae_error_t aesm_get_data_path(
  68. const char *p_file_name,
  69. char *p_file_path,
  70. size_t buf_size)
  71. {
  72. if(!p_file_name || !p_file_path)
  73. return OAL_PARAMETER_ERROR;
  74. if(strlen(AESM_DATA_FOLDER)+strlen(p_file_name)+sizeof(char)>buf_size)
  75. return OAL_PATHNAME_BUFFER_OVERFLOW_ERROR;
  76. strcpy(p_file_path, AESM_DATA_FOLDER);
  77. strncat(p_file_path,p_file_name, strlen(p_file_name));
  78. return AE_SUCCESS;
  79. }
  80. static ae_error_t aesm_write_file(
  81. const uint8_t *p_buf,
  82. uint32_t buf_size,
  83. char *p_file_name,
  84. bool is_full_path)
  85. {
  86. ae_error_t ret = AE_FAILURE;
  87. uint32_t write_length = 0;
  88. FILE* p_file = NULL;
  89. char p_full_path[MAX_PATH]= {0};
  90. if(is_full_path){
  91. if(strlen(p_file_name)>MAX_PATH-1){
  92. ret = OAL_PATHNAME_BUFFER_OVERFLOW_ERROR;
  93. goto CLEANUP;
  94. }
  95. strcpy(p_full_path, p_file_name);
  96. }else{
  97. if((ret=aesm_get_data_path(p_file_name, p_full_path, MAX_PATH)) != AE_SUCCESS)
  98. goto CLEANUP;
  99. }
  100. if(NULL == (p_file = fopen(p_full_path, "wb"))){
  101. ret = OAL_FILE_ACCESS_ERROR;
  102. goto CLEANUP;
  103. }
  104. write_length = (uint32_t)fwrite(p_buf, 1, buf_size, p_file);
  105. if(buf_size != write_length){
  106. ret = OAL_FILE_ACCESS_ERROR;
  107. goto CLEANUP;
  108. }
  109. ret = AE_SUCCESS;
  110. CLEANUP:
  111. if(p_file)
  112. fclose(p_file);
  113. return ret;
  114. }
  115. static ae_error_t aesm_read_file(
  116. uint8_t *p_buf,
  117. uint32_t& buf_size,
  118. char *p_file_name,
  119. bool is_full_path)
  120. {
  121. ae_error_t ret = AE_FAILURE;
  122. FILE* p_file = NULL;
  123. char p_full_path[MAX_PATH]= {0};
  124. if(is_full_path){
  125. if(strlen(p_file_name)>MAX_PATH-1){
  126. ret = OAL_PATHNAME_BUFFER_OVERFLOW_ERROR;
  127. goto CLEANUP;
  128. }
  129. strcpy(p_full_path, p_file_name);
  130. }else{
  131. if((ret=aesm_get_data_path(p_file_name, p_full_path, MAX_PATH)) != AE_SUCCESS)
  132. goto CLEANUP;
  133. }
  134. if(NULL == (p_file = fopen(p_full_path, "rb"))){
  135. ret = OAL_FILE_ACCESS_ERROR;
  136. goto CLEANUP;
  137. }
  138. buf_size = (uint32_t)fread(p_buf, 1, buf_size, p_file);
  139. ret = AE_SUCCESS;
  140. CLEANUP:
  141. if(p_file)
  142. fclose(p_file);
  143. return ret;
  144. }
  145. ae_error_t aesm_get_pathname(aesm_data_type_t type, aesm_data_id_t id, char *buf, uint32_t buf_size)
  146. {
  147. const persistent_storage_info_t *info = get_persistent_storage_info(id);
  148. if(info == NULL)
  149. return OAL_PARAMETER_ERROR;
  150. if(info->type != type)
  151. return OAL_PARAMETER_ERROR;
  152. if(info->type == FT_ENCLAVE_NAME){
  153. char local_info_name[MAX_PATH];
  154. sprintf(local_info_name,"libsgx_%s.signed.so",info->name);
  155. return aesm_get_path(local_info_name, buf, buf_size);
  156. }else if(info->loc == AESM_LOCATION_DATA){
  157. return aesm_get_data_path(info->name, buf, buf_size);
  158. }else{//info->loc == AESM_LOCATION_EXE_FOLDER
  159. return aesm_get_path(info->name, buf, buf_size);
  160. }
  161. }
  162. //alias function for aesm_get_pathname
  163. ae_error_t aesm_get_cpathname(aesm_data_type_t type, aesm_data_id_t id, char *buf, uint32_t buf_size)
  164. {
  165. return aesm_get_pathname(type, id, buf, buf_size);
  166. }
  167. ae_error_t aesm_query_data_size(aesm_data_type_t type, aesm_data_id_t data_id, uint32_t *p_size)
  168. {
  169. char pathname[MAX_PATH];
  170. ae_error_t ret = AE_SUCCESS;
  171. const persistent_storage_info_t *info = get_persistent_storage_info(data_id);
  172. if(info == NULL)
  173. return OAL_PARAMETER_ERROR;
  174. if(info->access == AESM_FILE_ACCESS_PATH_ONLY)
  175. return OAL_PARAMETER_ERROR;
  176. ret = aesm_get_pathname(type, data_id, pathname, MAX_PATH);
  177. if(ret != AE_SUCCESS)
  178. return ret;
  179. struct stat stat_info;
  180. if(stat(pathname, &stat_info)!=0){//Maybe file has not been created
  181. *p_size = 0;
  182. return AE_SUCCESS;
  183. }
  184. *p_size = (uint32_t)stat_info.st_size;
  185. return AE_SUCCESS;
  186. }
  187. ae_error_t aesm_read_data(aesm_data_type_t type, aesm_data_id_t data_id, uint8_t *buf, uint32_t *p_size)
  188. {
  189. char pathname[MAX_PATH];
  190. ae_error_t ret = AE_SUCCESS;
  191. const persistent_storage_info_t *info = get_persistent_storage_info(data_id);
  192. if(info == NULL)
  193. return OAL_PARAMETER_ERROR;
  194. if(info->access == AESM_FILE_ACCESS_PATH_ONLY)
  195. return OAL_PARAMETER_ERROR;
  196. ret = aesm_get_pathname(type, data_id, pathname, MAX_PATH);
  197. if(ret != AE_SUCCESS)
  198. return ret;
  199. if((ret=aesm_read_file(buf, *p_size, pathname, true))!=AE_SUCCESS)
  200. return ret;
  201. return AE_SUCCESS;
  202. }
  203. ae_error_t aesm_write_data(aesm_data_type_t type, aesm_data_id_t data_id, const uint8_t *buf, uint32_t size)
  204. {
  205. char pathname[MAX_PATH];
  206. ae_error_t ret = AE_SUCCESS;
  207. const persistent_storage_info_t *info = get_persistent_storage_info(data_id);
  208. if(info == NULL)
  209. return OAL_PARAMETER_ERROR;
  210. if(info->access != AESM_FILE_ACCESS_ALL)
  211. return OAL_PARAMETER_ERROR;
  212. ret = aesm_get_pathname(type, data_id, pathname, MAX_PATH);
  213. if(ret != AE_SUCCESS)
  214. return ret;
  215. if((ret=aesm_write_file(buf, size, pathname, true))!=AE_SUCCESS)
  216. return ret;
  217. return AE_SUCCESS;
  218. }