config_cpusvn.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /**
  32. * File: config_cpusvn.cpp
  33. * Description:
  34. * Implemente the CPUSVN upgrade/downgrade/reset simulation
  35. */
  36. #include "cpusvn_helper.h"
  37. #include "cpusvn_util.h"
  38. #include "se_trace.h"
  39. #include "rts_sim.h"
  40. #include "se_wrapper.h"
  41. #include <assert.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <string>
  46. #include <sstream>
  47. using namespace std;
  48. static void convert_cpusvn_to_string(sgx_cpu_svn_t &cpusvn, string &str)
  49. {
  50. uint32_t buffer[4];
  51. memcpy_s(&buffer, sizeof(uint32_t)*4, &cpusvn, sizeof(sgx_cpu_svn_t));
  52. stringstream ss;
  53. for(int i=0; i<4; i++)
  54. {
  55. buffer[i] = __builtin_bswap32(buffer[i]);
  56. ss<<hex<<buffer[i];
  57. }
  58. str = ss.str();
  59. return;
  60. }
  61. static bool initialize(char *file_path, uint32_t length, sgx_cpu_svn_t &cpusvn)
  62. {
  63. assert(file_path != NULL);
  64. // set file path
  65. if(get_file_path(file_path, length) == false)
  66. {
  67. return false;
  68. }
  69. // set cpusvn
  70. return read_cpusvn_file(file_path, &cpusvn);
  71. }
  72. static bool modify_cpusvn(action_t act, const char* file_path, sgx_cpu_svn_t &cpusvn)
  73. {
  74. assert(file_path != NULL);
  75. bool ret = false;
  76. string cpusvn_str = "";
  77. switch(act)
  78. {
  79. case ACTION_RESET:
  80. {
  81. memcpy_s(&cpusvn, sizeof(sgx_cpu_svn_t), &DEFAULT_CPUSVN, sizeof(sgx_cpu_svn_t));
  82. ret = true;
  83. }
  84. break;
  85. case ACTION_UPGRADE:
  86. {
  87. if(!memcmp(&cpusvn, &UPGRADED_CPUSVN, sizeof(sgx_cpu_svn_t)))
  88. {
  89. printf( "You have already upgraded CPUSVN.\n");
  90. }
  91. else if(!memcmp(&cpusvn, &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN)))
  92. {
  93. memcpy_s(&cpusvn, sizeof(sgx_cpu_svn_t), &UPGRADED_CPUSVN, sizeof(sgx_cpu_svn_t));
  94. ret = true;
  95. }
  96. else
  97. {
  98. memcpy_s(&cpusvn, sizeof(sgx_cpu_svn_t), &DEFAULT_CPUSVN, sizeof(sgx_cpu_svn_t));
  99. ret = true;
  100. }
  101. }
  102. break;
  103. case ACTION_DOWNGRADE:
  104. {
  105. if(!memcmp(&cpusvn, &DOWNGRADED_CPUSVN, sizeof(sgx_cpu_svn_t)))
  106. {
  107. printf("You have already downgraded CPUSVN.\n");
  108. }
  109. else if(!memcmp(&cpusvn, &DEFAULT_CPUSVN, sizeof(DEFAULT_CPUSVN)))
  110. {
  111. memcpy_s(&cpusvn, sizeof(sgx_cpu_svn_t), &DOWNGRADED_CPUSVN, sizeof(sgx_cpu_svn_t));
  112. ret = true;
  113. }
  114. else
  115. {
  116. memcpy_s(&cpusvn, sizeof(sgx_cpu_svn_t), &DEFAULT_CPUSVN, sizeof(sgx_cpu_svn_t));
  117. ret = true;
  118. }
  119. }
  120. break;
  121. default:
  122. {
  123. printf("Failed to configure the CPUSVN.\n");
  124. }
  125. break;
  126. }
  127. if(ret == true)
  128. {
  129. if(write_cpusvn_file(file_path, &cpusvn) == false)
  130. {
  131. printf("Failed to configure the CPUSVN.\n");
  132. return false;
  133. }
  134. }
  135. convert_cpusvn_to_string(cpusvn, cpusvn_str);
  136. printf("Current CPUSVN is: %s.\n", cpusvn_str.c_str());
  137. return ret;
  138. }
  139. int main(int argc, char *argv[])
  140. {
  141. if(argc != 2)
  142. {
  143. printf("Invalid input parameters.\n%s\n", USAGE);
  144. return -1;
  145. }
  146. action_t act;
  147. if(!strcmp(argv[1], HELP))
  148. {
  149. printf("%s\n", USAGE);
  150. return 0;
  151. }
  152. else if(!strcmp(argv[1], UPGRADE))
  153. {
  154. act = ACTION_UPGRADE;
  155. }
  156. else if(!strcmp(argv[1], DOWNGRADE))
  157. {
  158. act = ACTION_DOWNGRADE;
  159. }
  160. else if(!strcmp(argv[1], RESET))
  161. {
  162. act = ACTION_RESET;
  163. }
  164. else
  165. {
  166. printf("Invalid input parameters.\n%s\n", USAGE);
  167. return -1;
  168. }
  169. char file_path[MAX_PATH];
  170. sgx_cpu_svn_t cpusvn;
  171. memset(file_path, 0, MAX_PATH);
  172. memset(&cpusvn, 0, sizeof(sgx_cpu_svn_t));
  173. if(initialize(file_path, MAX_PATH, cpusvn) == false)
  174. {
  175. printf("Failed to get the CPUSVN.\n");
  176. return -1;
  177. }
  178. if(modify_cpusvn(act, file_path, cpusvn) == false)
  179. {
  180. return -1;
  181. }
  182. printf("SUCCESS.\n");
  183. return 0;
  184. }