load.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016-20 Intel Corporation. */
  3. #include <assert.h>
  4. #include <elf.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include "defines.h"
  19. #include "main.h"
  20. void encl_delete(struct encl *encl)
  21. {
  22. struct encl_segment *heap_seg;
  23. if (encl->encl_base)
  24. munmap((void *)encl->encl_base, encl->encl_size);
  25. if (encl->bin)
  26. munmap(encl->bin, encl->bin_size);
  27. if (encl->fd)
  28. close(encl->fd);
  29. if (encl->segment_tbl) {
  30. heap_seg = &encl->segment_tbl[encl->nr_segments - 1];
  31. munmap(heap_seg->src, heap_seg->size);
  32. free(encl->segment_tbl);
  33. }
  34. memset(encl, 0, sizeof(*encl));
  35. }
  36. static bool encl_map_bin(const char *path, struct encl *encl)
  37. {
  38. struct stat sb;
  39. void *bin;
  40. int ret;
  41. int fd;
  42. fd = open(path, O_RDONLY);
  43. if (fd == -1) {
  44. perror("enclave executable open()");
  45. return false;
  46. }
  47. ret = stat(path, &sb);
  48. if (ret) {
  49. perror("enclave executable stat()");
  50. goto err;
  51. }
  52. bin = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  53. if (bin == MAP_FAILED) {
  54. perror("enclave executable mmap()");
  55. goto err;
  56. }
  57. encl->bin = bin;
  58. encl->bin_size = sb.st_size;
  59. close(fd);
  60. return true;
  61. err:
  62. close(fd);
  63. return false;
  64. }
  65. static bool encl_ioc_create(struct encl *encl)
  66. {
  67. struct sgx_secs *secs = &encl->secs;
  68. struct sgx_enclave_create ioc;
  69. int rc;
  70. assert(encl->encl_base != 0);
  71. memset(secs, 0, sizeof(*secs));
  72. secs->ssa_frame_size = 1;
  73. secs->attributes = SGX_ATTR_MODE64BIT;
  74. secs->xfrm = 3;
  75. secs->base = encl->encl_base;
  76. secs->size = encl->encl_size;
  77. ioc.src = (unsigned long)secs;
  78. rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_CREATE, &ioc);
  79. if (rc) {
  80. perror("SGX_IOC_ENCLAVE_CREATE failed");
  81. munmap((void *)secs->base, encl->encl_size);
  82. return false;
  83. }
  84. return true;
  85. }
  86. static bool encl_ioc_add_pages(struct encl *encl, struct encl_segment *seg)
  87. {
  88. struct sgx_enclave_add_pages ioc;
  89. struct sgx_secinfo secinfo;
  90. int rc;
  91. memset(&secinfo, 0, sizeof(secinfo));
  92. secinfo.flags = seg->flags;
  93. ioc.src = (uint64_t)seg->src;
  94. ioc.offset = seg->offset;
  95. ioc.length = seg->size;
  96. ioc.secinfo = (unsigned long)&secinfo;
  97. if (seg->measure)
  98. ioc.flags = SGX_PAGE_MEASURE;
  99. else
  100. ioc.flags = 0;
  101. rc = ioctl(encl->fd, SGX_IOC_ENCLAVE_ADD_PAGES, &ioc);
  102. if (rc < 0) {
  103. perror("SGX_IOC_ENCLAVE_ADD_PAGES failed");
  104. return false;
  105. }
  106. return true;
  107. }
  108. /*
  109. * Parse the enclave code's symbol table to locate and return address of
  110. * the provided symbol
  111. */
  112. uint64_t encl_get_entry(struct encl *encl, const char *symbol)
  113. {
  114. Elf64_Sym *symtab = NULL;
  115. char *sym_names = NULL;
  116. Elf64_Shdr *sections;
  117. Elf64_Ehdr *ehdr;
  118. int num_sym = 0;
  119. int i;
  120. ehdr = encl->bin;
  121. sections = encl->bin + ehdr->e_shoff;
  122. for (i = 0; i < ehdr->e_shnum; i++) {
  123. if (sections[i].sh_type == SHT_SYMTAB) {
  124. symtab = (Elf64_Sym *)((char *)encl->bin + sections[i].sh_offset);
  125. num_sym = sections[i].sh_size / sections[i].sh_entsize;
  126. break;
  127. }
  128. }
  129. for (i = 0; i < ehdr->e_shnum; i++) {
  130. if (sections[i].sh_type == SHT_STRTAB) {
  131. sym_names = (char *)encl->bin + sections[i].sh_offset;
  132. break;
  133. }
  134. }
  135. if (!symtab || !sym_names)
  136. return 0;
  137. for (i = 0; i < num_sym; i++) {
  138. Elf64_Sym *sym = &symtab[i];
  139. if (!strcmp(symbol, sym_names + sym->st_name))
  140. return (uint64_t)sym->st_value;
  141. }
  142. return 0;
  143. }
  144. bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
  145. {
  146. const char device_path[] = "/dev/sgx_enclave";
  147. struct encl_segment *seg;
  148. Elf64_Phdr *phdr_tbl;
  149. off_t src_offset;
  150. Elf64_Ehdr *ehdr;
  151. struct stat sb;
  152. void *ptr;
  153. int i, j;
  154. int ret;
  155. int fd = -1;
  156. memset(encl, 0, sizeof(*encl));
  157. fd = open(device_path, O_RDWR);
  158. if (fd < 0) {
  159. perror("Unable to open /dev/sgx_enclave");
  160. goto err;
  161. }
  162. ret = stat(device_path, &sb);
  163. if (ret) {
  164. perror("device file stat()");
  165. goto err;
  166. }
  167. ptr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
  168. if (ptr == (void *)-1) {
  169. perror("mmap for read");
  170. goto err;
  171. }
  172. munmap(ptr, PAGE_SIZE);
  173. #define ERR_MSG \
  174. "mmap() succeeded for PROT_READ, but failed for PROT_EXEC.\n" \
  175. " Check that /dev does not have noexec set:\n" \
  176. " \tmount | grep \"/dev .*noexec\"\n" \
  177. " If so, remount it executable: mount -o remount,exec /dev\n\n"
  178. ptr = mmap(NULL, PAGE_SIZE, PROT_EXEC, MAP_SHARED, fd, 0);
  179. if (ptr == (void *)-1) {
  180. fprintf(stderr, ERR_MSG);
  181. goto err;
  182. }
  183. munmap(ptr, PAGE_SIZE);
  184. encl->fd = fd;
  185. if (!encl_map_bin(path, encl))
  186. goto err;
  187. ehdr = encl->bin;
  188. phdr_tbl = encl->bin + ehdr->e_phoff;
  189. encl->nr_segments = 1; /* one for the heap */
  190. for (i = 0; i < ehdr->e_phnum; i++) {
  191. Elf64_Phdr *phdr = &phdr_tbl[i];
  192. if (phdr->p_type == PT_LOAD)
  193. encl->nr_segments++;
  194. }
  195. encl->segment_tbl = calloc(encl->nr_segments,
  196. sizeof(struct encl_segment));
  197. if (!encl->segment_tbl)
  198. goto err;
  199. for (i = 0, j = 0; i < ehdr->e_phnum; i++) {
  200. Elf64_Phdr *phdr = &phdr_tbl[i];
  201. unsigned int flags = phdr->p_flags;
  202. if (phdr->p_type != PT_LOAD)
  203. continue;
  204. seg = &encl->segment_tbl[j];
  205. if (!!(flags & ~(PF_R | PF_W | PF_X))) {
  206. fprintf(stderr,
  207. "%d has invalid segment flags 0x%02x.\n", i,
  208. phdr->p_flags);
  209. goto err;
  210. }
  211. if (j == 0 && flags != (PF_R | PF_W)) {
  212. fprintf(stderr,
  213. "TCS has invalid segment flags 0x%02x.\n",
  214. phdr->p_flags);
  215. goto err;
  216. }
  217. if (j == 0) {
  218. src_offset = phdr->p_offset & PAGE_MASK;
  219. encl->src = encl->bin + src_offset;
  220. seg->prot = PROT_READ | PROT_WRITE;
  221. seg->flags = SGX_PAGE_TYPE_TCS << 8;
  222. } else {
  223. seg->prot = (phdr->p_flags & PF_R) ? PROT_READ : 0;
  224. seg->prot |= (phdr->p_flags & PF_W) ? PROT_WRITE : 0;
  225. seg->prot |= (phdr->p_flags & PF_X) ? PROT_EXEC : 0;
  226. seg->flags = (SGX_PAGE_TYPE_REG << 8) | seg->prot;
  227. }
  228. seg->offset = (phdr->p_offset & PAGE_MASK) - src_offset;
  229. seg->size = (phdr->p_filesz + PAGE_SIZE - 1) & PAGE_MASK;
  230. seg->src = encl->src + seg->offset;
  231. seg->measure = true;
  232. j++;
  233. }
  234. assert(j == encl->nr_segments - 1);
  235. seg = &encl->segment_tbl[j];
  236. seg->offset = encl->segment_tbl[j - 1].offset + encl->segment_tbl[j - 1].size;
  237. seg->size = heap_size;
  238. seg->src = mmap(NULL, heap_size, PROT_READ | PROT_WRITE,
  239. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  240. seg->prot = PROT_READ | PROT_WRITE;
  241. seg->flags = (SGX_PAGE_TYPE_REG << 8) | seg->prot;
  242. seg->measure = false;
  243. if (seg->src == MAP_FAILED)
  244. goto err;
  245. encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;
  246. for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
  247. encl->encl_size <<= 1;
  248. return true;
  249. err:
  250. if (fd != -1)
  251. close(fd);
  252. encl_delete(encl);
  253. return false;
  254. }
  255. static bool encl_map_area(struct encl *encl)
  256. {
  257. size_t encl_size = encl->encl_size;
  258. void *area;
  259. area = mmap(NULL, encl_size * 2, PROT_NONE,
  260. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  261. if (area == MAP_FAILED) {
  262. perror("reservation mmap()");
  263. return false;
  264. }
  265. encl->encl_base = ((uint64_t)area + encl_size - 1) & ~(encl_size - 1);
  266. munmap(area, encl->encl_base - (uint64_t)area);
  267. munmap((void *)(encl->encl_base + encl_size),
  268. (uint64_t)area + encl_size - encl->encl_base);
  269. return true;
  270. }
  271. bool encl_build(struct encl *encl)
  272. {
  273. struct sgx_enclave_init ioc;
  274. int ret;
  275. int i;
  276. if (!encl_map_area(encl))
  277. return false;
  278. if (!encl_ioc_create(encl))
  279. return false;
  280. /*
  281. * Pages must be added before mapping VMAs because their permissions
  282. * cap the VMA permissions.
  283. */
  284. for (i = 0; i < encl->nr_segments; i++) {
  285. struct encl_segment *seg = &encl->segment_tbl[i];
  286. if (!encl_ioc_add_pages(encl, seg))
  287. return false;
  288. }
  289. ioc.sigstruct = (uint64_t)&encl->sigstruct;
  290. ret = ioctl(encl->fd, SGX_IOC_ENCLAVE_INIT, &ioc);
  291. if (ret) {
  292. perror("SGX_IOC_ENCLAVE_INIT failed");
  293. return false;
  294. }
  295. return true;
  296. }