cpusvn_util.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "cpusvn_util.h"
  32. #include "se_wrapper.h"
  33. #include "sgx_error.h"
  34. #include "rts_sim.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39. #include <assert.h>
  40. #include <errno.h>
  41. #define MAX_FILE_PATH 260
  42. extern "C" bool get_file_path(char *config_path, uint32_t length)
  43. {
  44. if(config_path == NULL || length == 0)
  45. {
  46. return false;
  47. }
  48. char *local_path = getenv(ENV_PAR);
  49. if(local_path == NULL)
  50. return false;
  51. snprintf(config_path, length, "%s%s", local_path, FILE_NAME);
  52. return true;
  53. }
  54. extern "C" bool write_cpusvn_file(const char *file_path, const sgx_cpu_svn_t *cpusvn)
  55. {
  56. if(file_path == NULL || cpusvn == NULL)
  57. {
  58. return false;
  59. }
  60. FILE *fp = NULL;
  61. if(NULL == (fp = fopen(file_path, "wb")))
  62. {
  63. return false;
  64. }
  65. if(fwrite(cpusvn, 1, sizeof(sgx_cpu_svn_t), fp) != sizeof(sgx_cpu_svn_t))
  66. {
  67. fclose(fp);
  68. return false;
  69. }
  70. fclose(fp);
  71. return true;
  72. }
  73. extern "C" bool read_cpusvn_file(const char *config_path, sgx_cpu_svn_t *cpusvn_ptr)
  74. {
  75. if(config_path == NULL || cpusvn_ptr == NULL)
  76. return false;
  77. FILE *fp = NULL;
  78. long fsize = 0;
  79. sgx_cpu_svn_t temp_cpusvn = {{0}};
  80. size_t result = 0;
  81. if(NULL == (fp = fopen(config_path, "rb")))
  82. {
  83. SE_TRACE(SE_TRACE_DEBUG, "Couldn't find/open the configuration file %s.\n", config_path);
  84. memcpy_s(cpusvn_ptr, sizeof(sgx_cpu_svn_t), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  85. return true;
  86. }
  87. //check and read configure file format
  88. if(fseek(fp, 0, SEEK_END))
  89. {
  90. memcpy_s(&temp_cpusvn, sizeof(temp_cpusvn), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  91. goto clean_return;
  92. }
  93. fsize = ftell(fp);
  94. rewind(fp);
  95. if(fsize != sizeof(sgx_cpu_svn_t))
  96. {
  97. SE_TRACE(SE_TRACE_DEBUG, "The configuration file format is not correct. Using default CPUSVN value.\n");
  98. memcpy_s(&temp_cpusvn, sizeof(temp_cpusvn), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  99. goto clean_return;
  100. }
  101. result = fread(&temp_cpusvn, 1, fsize, fp);
  102. if(result != (size_t)fsize)
  103. {
  104. SE_TRACE(SE_TRACE_DEBUG, "Failed to read configuration file. Using default CPUSVN value.\n");
  105. memcpy_s(&temp_cpusvn, sizeof(temp_cpusvn), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  106. goto clean_return;
  107. }
  108. if(memcmp(&temp_cpusvn, &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN)) &&
  109. memcmp(&temp_cpusvn, &UPGRADED_CPUSVN, sizeof(UPGRADED_CPUSVN)) &&
  110. memcmp(&temp_cpusvn, &DOWNGRADED_CPUSVN, sizeof(DOWNGRADED_CPUSVN)))
  111. {
  112. SE_TRACE(SE_TRACE_DEBUG, "The configuration file format is not correct. Using default CPUSVN value.\n");
  113. memcpy_s(&temp_cpusvn, sizeof(temp_cpusvn), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  114. goto clean_return;
  115. }
  116. clean_return:
  117. fclose(fp);
  118. memcpy_s(cpusvn_ptr, sizeof(*cpusvn_ptr), &temp_cpusvn, sizeof(temp_cpusvn));
  119. return true;
  120. }
  121. extern "C" int get_cpusvn(sgx_cpu_svn_t *cpu_svn)
  122. {
  123. if( cpu_svn == NULL)
  124. return SGX_ERROR_INVALID_PARAMETER;
  125. sgx_cpu_svn_t temp_cpusvn = {{0}};
  126. char config_path[MAX_FILE_PATH];
  127. memset(config_path, 0, MAX_FILE_PATH);
  128. if((get_file_path(config_path, MAX_FILE_PATH)) == false)
  129. {
  130. SE_TRACE(SE_TRACE_DEBUG, "Get configuration file path failed. Using default CPUSVN value\n");
  131. memcpy_s(cpu_svn, sizeof(*cpu_svn), &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN));
  132. return SGX_SUCCESS;
  133. }
  134. bool r = read_cpusvn_file(config_path, &temp_cpusvn);
  135. (void)r, assert (r);
  136. memcpy_s(cpu_svn, sizeof(*cpu_svn), &temp_cpusvn, sizeof(temp_cpusvn));
  137. return SGX_SUCCESS;
  138. }