sgx_framework.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <pal_linux.h>
  4. #include <pal_rtld.h>
  5. #include "sgx_internal.h"
  6. #include "sgx_arch.h"
  7. #include "sgx_enclave.h"
  8. #include "sgx-driver/graphene-sgx.h"
  9. #include <asm/errno.h>
  10. int gsgx_device = -1;
  11. void * zero_page;
  12. int open_gsgx(void)
  13. {
  14. int fd = INLINE_SYSCALL(open, 3, GSGX_FILE, O_RDWR, 0);
  15. if (IS_ERR(fd))
  16. return -ERRNO(fd);
  17. gsgx_device = fd;
  18. return 0;
  19. }
  20. int read_enclave_token(int token_file, sgx_arch_token_t * token)
  21. {
  22. struct stat stat;
  23. int ret;
  24. ret = INLINE_SYSCALL(fstat, 2, token_file, &stat);
  25. if (IS_ERR(ret))
  26. return -ERRNO(ret);
  27. if (stat.st_size != sizeof(sgx_arch_token_t)) {
  28. SGX_DBG(DBG_I, "size of token size does not match\n");
  29. return -EINVAL;
  30. }
  31. int bytes = INLINE_SYSCALL(read, 3, token_file, token, sizeof(sgx_arch_token_t));
  32. if (IS_ERR(bytes))
  33. return -ERRNO(bytes);
  34. return 0;
  35. }
  36. int read_enclave_sigstruct(int sigfile, sgx_arch_sigstruct_t * sig)
  37. {
  38. struct stat stat;
  39. int ret;
  40. ret = INLINE_SYSCALL(fstat, 2, sigfile, &stat);
  41. if (IS_ERR(ret))
  42. return -ERRNO(ret);
  43. if (stat.st_size < sizeof(sgx_arch_sigstruct_t)) {
  44. SGX_DBG(DBG_I, "size of sigstruct size does not match\n");
  45. return -EINVAL;
  46. }
  47. int bytes = INLINE_SYSCALL(read, 3, sigfile, sig, sizeof(sgx_arch_sigstruct_t));
  48. if (IS_ERR(bytes))
  49. return -ERRNO(bytes);
  50. return 0;
  51. }
  52. #define SE_LEAF 0x12
  53. static inline void cpuid(uint32_t leaf, uint32_t subleaf, uint32_t info[4])
  54. {
  55. asm volatile("cpuid"
  56. : "=a"(info[0]),
  57. "=b"(info[1]),
  58. "=c"(info[2]),
  59. "=d"(info[3])
  60. : "a"(leaf),
  61. "c"(subleaf));
  62. }
  63. static size_t get_ssaframesize (uint64_t xfrm)
  64. {
  65. uint32_t cpuinfo[4];
  66. uint64_t xfrm_ex;
  67. int xsave_size = 0;
  68. cpuid(SE_LEAF, 1, cpuinfo);
  69. xfrm_ex = ((uint64_t) cpuinfo[3] << 32) + cpuinfo[2];
  70. for (int i = 2; i < 64; i++)
  71. if ((xfrm & (1 << i)) || (xfrm_ex & (1 << i))) {
  72. cpuid(0xd, i, cpuinfo);
  73. if (cpuinfo[0] + cpuinfo[1] > xsave_size)
  74. xsave_size = cpuinfo[0] + cpuinfo[1];
  75. }
  76. return ALLOC_ALIGNUP(xsave_size + sizeof(sgx_arch_gpr_t) + 1);
  77. }
  78. int check_wrfsbase_support (void)
  79. {
  80. if (gsgx_device == -1)
  81. return -EACCES;
  82. uint32_t cpuinfo[4];
  83. cpuid(7, 0, cpuinfo);
  84. if (!(cpuinfo[1] & 0x1))
  85. return 0;
  86. return 1;
  87. }
  88. int create_enclave(sgx_arch_secs_t * secs,
  89. unsigned long baseaddr,
  90. unsigned long size,
  91. sgx_arch_token_t * token)
  92. {
  93. if (gsgx_device == -1)
  94. return -EACCES;
  95. if (!zero_page) {
  96. zero_page = (void *)
  97. INLINE_SYSCALL(mmap, 6, NULL, pagesize,
  98. PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS,
  99. -1, 0);
  100. if (IS_ERR_P(zero_page))
  101. return -ENOMEM;
  102. }
  103. memset(secs, 0, sizeof(sgx_arch_secs_t));
  104. secs->size = pagesize;
  105. while (secs->size < size)
  106. secs->size <<= 1;
  107. secs->ssaframesize = get_ssaframesize(token->attributes.xfrm) / pagesize;
  108. secs->miscselect = token->miscselect_mask;
  109. memcpy(&secs->attributes, &token->attributes,
  110. sizeof(sgx_arch_attributes_t));
  111. memcpy(&secs->mrenclave, &token->mrenclave, sizeof(sgx_arch_hash_t));
  112. memcpy(&secs->mrsigner, &token->mrsigner, sizeof(sgx_arch_hash_t));
  113. struct gsgx_enclave_create param;
  114. param.secs = secs;
  115. if (baseaddr)
  116. param.addr = (unsigned long) baseaddr & ~(secs->size - 1);
  117. else
  118. param.addr = GSGX_ENCLAVE_CREATE_NO_ADDR;
  119. int ret = INLINE_SYSCALL(ioctl, 3, gsgx_device, GSGX_IOCTL_ENCLAVE_CREATE,
  120. &param);
  121. if (IS_ERR(ret)) {
  122. if (ERRNO(ret) == EBADF)
  123. gsgx_device = -1;
  124. return -ERRNO(ret);
  125. }
  126. if (ret) {
  127. SGX_DBG(DBG_I, "enclave ECREATE failed\n");
  128. return -EPERM;
  129. }
  130. secs->attributes.flags |= SGX_FLAGS_INITIALIZED;
  131. SGX_DBG(DBG_I, "enclave created:\n");
  132. SGX_DBG(DBG_I, " base: 0x%016lx\n", param.addr);
  133. SGX_DBG(DBG_I, " size: 0x%x\n", secs->size);
  134. SGX_DBG(DBG_I, " attr: 0x%016lx\n", secs->attributes.flags);
  135. SGX_DBG(DBG_I, " xfrm: 0x%016lx\n", secs->attributes.xfrm);
  136. SGX_DBG(DBG_I, " ssaframesize: %ld\n", secs->ssaframesize);
  137. SGX_DBG(DBG_I, " isvprodid: 0x%08x\n", secs->isvprodid);
  138. SGX_DBG(DBG_I, " isvsvn: 0x%08x\n", secs->isvsvn);
  139. secs->baseaddr = param.addr;
  140. return 0;
  141. }
  142. int add_pages_to_enclave(sgx_arch_secs_t * secs,
  143. void * addr, void * user_addr,
  144. unsigned long size,
  145. enum sgx_page_type type, int prot,
  146. bool skip_eextend,
  147. const char * comment)
  148. {
  149. if (gsgx_device == -1)
  150. return -EACCES;
  151. struct gsgx_enclave_add_pages param;
  152. sgx_arch_secinfo_t secinfo;
  153. memset(&secinfo, 0, sizeof(sgx_arch_secinfo_t));
  154. switch (type) {
  155. case SGX_PAGE_SECS:
  156. return -EPERM;
  157. case SGX_PAGE_TCS:
  158. secinfo.flags |= SGX_SECINFO_FLAGS_TCS;
  159. break;
  160. case SGX_PAGE_REG:
  161. secinfo.flags |= SGX_SECINFO_FLAGS_REG;
  162. if (prot & PROT_READ)
  163. secinfo.flags |= SGX_SECINFO_FLAGS_R;
  164. if (prot & PROT_WRITE)
  165. secinfo.flags |= SGX_SECINFO_FLAGS_W;
  166. if (prot & PROT_EXEC)
  167. secinfo.flags |= SGX_SECINFO_FLAGS_X;
  168. break;
  169. }
  170. param.addr = secs->baseaddr + (uint64_t) addr;
  171. param.user_addr = (uint64_t) user_addr;
  172. param.size = size;
  173. param.secinfo = &secinfo;
  174. param.flags = skip_eextend ? GSGX_ENCLAVE_ADD_PAGES_SKIP_EEXTEND : 0;
  175. if (!param.user_addr) {
  176. param.user_addr = (unsigned long) zero_page;
  177. param.flags |= GSGX_ENCLAVE_ADD_PAGES_REPEAT_SRC;
  178. }
  179. char p[4] = "---";
  180. const char * t = (type == SGX_PAGE_TCS) ? "TCS" : "REG";
  181. const char * m = skip_eextend ? "" : " measured";
  182. if (type == SGX_PAGE_REG) {
  183. if (prot & PROT_READ)
  184. p[0] = 'R';
  185. if (prot & PROT_WRITE)
  186. p[1] = 'W';
  187. if (prot & PROT_EXEC)
  188. p[2] = 'X';
  189. }
  190. if (size == pagesize)
  191. SGX_DBG(DBG_I, "adding page to enclave: %016lx [%s:%s] (%s)%s\n",
  192. addr, t, p, comment, m);
  193. else
  194. SGX_DBG(DBG_I, "adding pages to enclave: %016lx-%016lx [%s:%s] (%s)%s\n",
  195. addr, addr + size, t, p, comment, m);
  196. int ret = INLINE_SYSCALL(ioctl, 3, gsgx_device,
  197. GSGX_IOCTL_ENCLAVE_ADD_PAGES,
  198. &param);
  199. if (IS_ERR(ret)) {
  200. if (ERRNO(ret) == EBADF)
  201. gsgx_device = -1;
  202. return -ERRNO(ret);
  203. }
  204. return 0;
  205. }
  206. int init_enclave(sgx_arch_secs_t * secs,
  207. sgx_arch_sigstruct_t * sigstruct,
  208. sgx_arch_token_t * token)
  209. {
  210. if (gsgx_device == -1)
  211. return -EACCES;
  212. unsigned long enclave_valid_addr =
  213. secs->baseaddr + secs->size - pagesize;
  214. SGX_DBG(DBG_I, "enclave initializing:\n");
  215. SGX_DBG(DBG_I, " enclave id: 0x%016lx\n", enclave_valid_addr);
  216. SGX_DBG(DBG_I, " enclave hash:");
  217. for (int i = 0 ; i < sizeof(sgx_arch_hash_t) ; i++)
  218. SGX_DBG(DBG_I, " %02x", sigstruct->enclave_hash[i]);
  219. SGX_DBG(DBG_I, "\n");
  220. struct gsgx_enclave_init param;
  221. param.addr = enclave_valid_addr;
  222. param.sigstruct = sigstruct;
  223. param.einittoken = token;
  224. int ret = INLINE_SYSCALL(ioctl, 3, gsgx_device, GSGX_IOCTL_ENCLAVE_INIT,
  225. &param);
  226. if (IS_ERR(ret)) {
  227. if (ERRNO(ret) == EBADF)
  228. gsgx_device = -1;
  229. return -ERRNO(ret);
  230. }
  231. if (ret) {
  232. SGX_DBG(DBG_I, "enclave EINIT failed\n");
  233. return -EPERM;
  234. }
  235. return 0;
  236. }
  237. int destroy_enclave(void * base_addr)
  238. {
  239. if (gsgx_device == -1)
  240. return -EACCES;
  241. struct gsgx_enclave_destroy param;
  242. param.addr = (unsigned long) base_addr;
  243. SGX_DBG(DBG_I, "destroying enclave...\n");
  244. int ret = INLINE_SYSCALL(ioctl, 3, gsgx_device, GSGX_IOCTL_ENCLAVE_DESTROY,
  245. &param);
  246. if (IS_ERR(ret)) {
  247. if (ERRNO(ret) == EBADF)
  248. gsgx_device = -1;
  249. return -ERRNO(ret);
  250. }
  251. if (ret) {
  252. SGX_DBG(DBG_I, "enclave EDESTROY failed\n");
  253. return -EPERM;
  254. }
  255. return 0;
  256. }