edmm_utility.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "edmm_utility.h"
  32. #include "se_trace.h"
  33. #include "isgx_user.h"
  34. #include "cpuid.h"
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #include <sys/ioctl.h>
  40. #include <errno.h>
  41. #include <stdio.h>
  42. #define SGX_URTS_CMD "strings /usr/lib/libsgx_urts.so 2> /dev/null | grep SGX_URTS_VERSION_2"
  43. #define SGX_CPUID 0x12
  44. /* is_urts_support_edmm()
  45. * Parameters:
  46. * None.
  47. * Return Value:
  48. * true - uRTS supports EDMM.
  49. * false - uRTS does not support EDMM.
  50. */
  51. static bool is_urts_support_edmm()
  52. {
  53. FILE* pipe = popen(SGX_URTS_CMD, "r");
  54. if (NULL == pipe)
  55. {
  56. SE_TRACE(SE_TRACE_WARNING, "Failed to open pipe.\n");
  57. return false;
  58. }
  59. char line[1024];
  60. if (NULL == fgets(line, sizeof(line), pipe))
  61. return false;
  62. if (-1 == pclose(pipe))
  63. {
  64. SE_TRACE(SE_TRACE_WARNING, "Failed to close pipe.\n");
  65. }
  66. return true;
  67. }
  68. /* open_se_device()
  69. * Parameters:
  70. * hdevice [out] - The device handle as returned from the open_se_device API.
  71. * in_kernel_driver [out] - Indicate it is in-kernel driver or not.
  72. * Return Value:
  73. * true - The function succeeds.
  74. * false - The function fails.
  75. */
  76. extern "C" bool open_se_device(int *hdevice, bool *in_kernel_driver)
  77. {
  78. if (NULL == hdevice)
  79. return false;
  80. int hdev = open("/dev/isgx", O_RDWR);
  81. if (-1 == hdev)
  82. {
  83. hdev = open("/dev/sgx", O_RDWR);
  84. if (-1 == hdev)
  85. {
  86. SE_TRACE(SE_TRACE_WARNING, "Failed to open Intel SGX device.\n");
  87. return false;
  88. }
  89. if (NULL != in_kernel_driver)
  90. *in_kernel_driver = true;
  91. }
  92. *hdevice = hdev;
  93. return true;
  94. }
  95. /* close_se_device()
  96. * Parameters:
  97. * hdevice [in out] - The device handle will be set to -1 if it is closed successfully.
  98. * Return Value:
  99. * true - The function succeeds.
  100. * false - The function fails.
  101. */
  102. extern "C" bool close_se_device(int *hdevice)
  103. {
  104. if (NULL == hdevice)
  105. return false;
  106. if (-1 != *hdevice && 0 != close(*hdevice))
  107. {
  108. SE_TRACE(SE_TRACE_WARNING, "Failed to close Intel SGX device.\n");
  109. return false;
  110. }
  111. *hdevice = -1;
  112. return true;
  113. }
  114. /* is_cpu_support_edmm()
  115. * Parameters:
  116. * None.
  117. * Return Value:
  118. * true - CPU supports EDMM.
  119. * false - CPU does not support EDMM.
  120. */
  121. extern "C" bool is_cpu_support_edmm()
  122. {
  123. int a[4] = {0,0,0,0};
  124. //Check CPU EDMM capability by CPUID
  125. __cpuid(a, 0);
  126. if (a[0] < SGX_CPUID)
  127. return false;
  128. __cpuidex(a, SGX_CPUID, 0);
  129. if (!(a[0] & 1))
  130. return false;
  131. return ((a[0] & 2) != 0);
  132. }
  133. /* is_driver_support_edmm()
  134. * Parameters:
  135. * hdevice [in] - The device handle used to communicate with driver.
  136. * Return Value:
  137. * true - Driver supports EDMM.
  138. * false - Driver does not support EDMM.
  139. */
  140. extern "C" bool is_driver_support_edmm(int hdevice)
  141. {
  142. if (-1 == hdevice)
  143. return false;
  144. sgx_modification_param param;
  145. param.flags = 0;
  146. param.range.start_addr = 0;
  147. param.range.nr_pages = 0;
  148. int ret = ioctl(hdevice, SGX_IOC_ENCLAVE_EMODPR, &param);
  149. if ((-1 == ret) && (errno == ENOTTY))
  150. return false;
  151. return true;
  152. }
  153. /* is_support_edmm()
  154. * Parameters:
  155. * None.
  156. * Return Value:
  157. * true - CPU/driver/uRTS supports EDMM.
  158. * false - Either of CPU/driver/uRTS does not support EDMM.
  159. */
  160. extern "C" bool is_support_edmm()
  161. {
  162. if (false == is_cpu_support_edmm())
  163. return false;
  164. int hdevice = -1;
  165. if (false == open_se_device(&hdevice, NULL))
  166. return false;
  167. if (false == is_driver_support_edmm(hdevice))
  168. {
  169. close_se_device(&hdevice);
  170. return false;
  171. }
  172. close_se_device(&hdevice);
  173. if (false == is_urts_support_edmm())
  174. return false;
  175. return true;
  176. }