load.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_Shdr *sections;
  115. Elf64_Sym *symtab;
  116. Elf64_Ehdr *ehdr;
  117. char *sym_names;
  118. int num_sym;
  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. for (i = 0; i < num_sym; i++) {
  136. Elf64_Sym *sym = &symtab[i];
  137. if (!strcmp(symbol, sym_names + sym->st_name))
  138. return (uint64_t)sym->st_value;
  139. }
  140. return 0;
  141. }
  142. bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
  143. {
  144. const char device_path[] = "/dev/sgx_enclave";
  145. struct encl_segment *seg;
  146. Elf64_Phdr *phdr_tbl;
  147. off_t src_offset;
  148. Elf64_Ehdr *ehdr;
  149. struct stat sb;
  150. void *ptr;
  151. int i, j;
  152. int ret;
  153. int fd = -1;
  154. memset(encl, 0, sizeof(*encl));
  155. fd = open(device_path, O_RDWR);
  156. if (fd < 0) {
  157. perror("Unable to open /dev/sgx_enclave");
  158. goto err;
  159. }
  160. ret = stat(device_path, &sb);
  161. if (ret) {
  162. perror("device file stat()");
  163. goto err;
  164. }
  165. ptr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
  166. if (ptr == (void *)-1) {
  167. perror("mmap for read");
  168. goto err;
  169. }
  170. munmap(ptr, PAGE_SIZE);
  171. #define ERR_MSG \
  172. "mmap() succeeded for PROT_READ, but failed for PROT_EXEC.\n" \
  173. " Check that /dev does not have noexec set:\n" \
  174. " \tmount | grep \"/dev .*noexec\"\n" \
  175. " If so, remount it executable: mount -o remount,exec /dev\n\n"
  176. ptr = mmap(NULL, PAGE_SIZE, PROT_EXEC, MAP_SHARED, fd, 0);
  177. if (ptr == (void *)-1) {
  178. fprintf(stderr, ERR_MSG);
  179. goto err;
  180. }
  181. munmap(ptr, PAGE_SIZE);
  182. encl->fd = fd;
  183. if (!encl_map_bin(path, encl))
  184. goto err;
  185. ehdr = encl->bin;
  186. phdr_tbl = encl->bin + ehdr->e_phoff;
  187. encl->nr_segments = 1; /* one for the heap */
  188. for (i = 0; i < ehdr->e_phnum; i++) {
  189. Elf64_Phdr *phdr = &phdr_tbl[i];
  190. if (phdr->p_type == PT_LOAD)
  191. encl->nr_segments++;
  192. }
  193. encl->segment_tbl = calloc(encl->nr_segments,
  194. sizeof(struct encl_segment));
  195. if (!encl->segment_tbl)
  196. goto err;
  197. for (i = 0, j = 0; i < ehdr->e_phnum; i++) {
  198. Elf64_Phdr *phdr = &phdr_tbl[i];
  199. unsigned int flags = phdr->p_flags;
  200. if (phdr->p_type != PT_LOAD)
  201. continue;
  202. seg = &encl->segment_tbl[j];
  203. if (!!(flags & ~(PF_R | PF_W | PF_X))) {
  204. fprintf(stderr,
  205. "%d has invalid segment flags 0x%02x.\n", i,
  206. phdr->p_flags);
  207. goto err;
  208. }
  209. if (j == 0 && flags != (PF_R | PF_W)) {
  210. fprintf(stderr,
  211. "TCS has invalid segment flags 0x%02x.\n",
  212. phdr->p_flags);
  213. goto err;
  214. }
  215. if (j == 0) {
  216. src_offset = phdr->p_offset & PAGE_MASK;
  217. encl->src = encl->bin + src_offset;
  218. seg->prot = PROT_READ | PROT_WRITE;
  219. seg->flags = SGX_PAGE_TYPE_TCS << 8;
  220. } else {
  221. seg->prot = (phdr->p_flags & PF_R) ? PROT_READ : 0;
  222. seg->prot |= (phdr->p_flags & PF_W) ? PROT_WRITE : 0;
  223. seg->prot |= (phdr->p_flags & PF_X) ? PROT_EXEC : 0;
  224. seg->flags = (SGX_PAGE_TYPE_REG << 8) | seg->prot;
  225. }
  226. seg->offset = (phdr->p_offset & PAGE_MASK) - src_offset;
  227. seg->size = (phdr->p_filesz + PAGE_SIZE - 1) & PAGE_MASK;
  228. seg->src = encl->src + seg->offset;
  229. seg->measure = true;
  230. j++;
  231. }
  232. assert(j == encl->nr_segments - 1);
  233. seg = &encl->segment_tbl[j];
  234. seg->offset = encl->segment_tbl[j - 1].offset + encl->segment_tbl[j - 1].size;
  235. seg->size = heap_size;
  236. seg->src = mmap(NULL, heap_size, PROT_READ | PROT_WRITE,
  237. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  238. seg->prot = PROT_READ | PROT_WRITE;
  239. seg->flags = (SGX_PAGE_TYPE_REG << 8) | seg->prot;
  240. seg->measure = false;
  241. if (seg->src == MAP_FAILED)
  242. goto err;
  243. encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;
  244. for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
  245. encl->encl_size <<= 1;
  246. return true;
  247. err:
  248. if (fd != -1)
  249. close(fd);
  250. encl_delete(encl);
  251. return false;
  252. }
  253. static bool encl_map_area(struct encl *encl)
  254. {
  255. size_t encl_size = encl->encl_size;
  256. void *area;
  257. area = mmap(NULL, encl_size * 2, PROT_NONE,
  258. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  259. if (area == MAP_FAILED) {
  260. perror("reservation mmap()");
  261. return false;
  262. }
  263. encl->encl_base = ((uint64_t)area + encl_size - 1) & ~(encl_size - 1);
  264. munmap(area, encl->encl_base - (uint64_t)area);
  265. munmap((void *)(encl->encl_base + encl_size),
  266. (uint64_t)area + encl_size - encl->encl_base);
  267. return true;
  268. }
  269. bool encl_build(struct encl *encl)
  270. {
  271. struct sgx_enclave_init ioc;
  272. int ret;
  273. int i;
  274. if (!encl_map_area(encl))
  275. return false;
  276. if (!encl_ioc_create(encl))
  277. return false;
  278. /*
  279. * Pages must be added before mapping VMAs because their permissions
  280. * cap the VMA permissions.
  281. */
  282. for (i = 0; i < encl->nr_segments; i++) {
  283. struct encl_segment *seg = &encl->segment_tbl[i];
  284. if (!encl_ioc_add_pages(encl, seg))
  285. return false;
  286. }
  287. ioc.sigstruct = (uint64_t)&encl->sigstruct;
  288. ret = ioctl(encl->fd, SGX_IOC_ENCLAVE_INIT, &ioc);
  289. if (ret) {
  290. perror("SGX_IOC_ENCLAVE_INIT failed");
  291. return false;
  292. }
  293. return true;
  294. }