sgx_main.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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_tls.h"
  7. #include "sgx_enclave.h"
  8. #include "debugger/sgx_gdb.h"
  9. #include <asm/fcntl.h>
  10. #include <asm/socket.h>
  11. #include <linux/fs.h>
  12. #include <linux/in.h>
  13. #include <linux/in6.h>
  14. #include <asm/errno.h>
  15. #include <sysdep.h>
  16. #include <sysdeps/generic/ldsodefs.h>
  17. unsigned long pagesize = PRESET_PAGESIZE;
  18. unsigned long pagemask = ~(PRESET_PAGESIZE - 1);
  19. unsigned long pageshift = PRESET_PAGESIZE - 1;
  20. static inline
  21. char * alloc_concat(const char * p, int plen,
  22. const char * s, int slen)
  23. {
  24. plen = (plen != -1) ? plen : (p ? strlen(p) : 0);
  25. slen = (slen != -1) ? slen : (s ? strlen(s) : 0);
  26. char * buf = malloc(plen + slen + 1);
  27. if (plen)
  28. memcpy(buf, p, plen);
  29. if (slen)
  30. memcpy(buf + plen, s, slen);
  31. buf[plen + slen] = '\0';
  32. return buf;
  33. }
  34. static unsigned long parse_int (const char * str)
  35. {
  36. unsigned long num = 0;
  37. int radix = 10;
  38. char c;
  39. if (str[0] == '0') {
  40. str++;
  41. radix = 8;
  42. if (str[0] == 'x') {
  43. str++;
  44. radix = 16;
  45. }
  46. }
  47. while ((c = *(str++))) {
  48. int val;
  49. if (c >= 'A' && c <= 'F')
  50. val = c - 'A' + 10;
  51. else if (c >= 'a' && c <= 'f')
  52. val = c - 'a' + 10;
  53. else if (c >= '0' && c <= '9')
  54. val = c - '0';
  55. else
  56. break;
  57. if (val >= radix)
  58. break;
  59. num = num * radix + val;
  60. }
  61. if (c == 'G' || c == 'g')
  62. num *= 1024 * 1024 * 1024;
  63. else if (c == 'M' || c == 'm')
  64. num *= 1024 * 1024;
  65. else if (c == 'K' || c == 'k')
  66. num *= 1024;
  67. return num;
  68. }
  69. static const char * resolve_uri (const char * uri, const char ** errstring)
  70. {
  71. if (!strpartcmp_static(uri, "file:")) {
  72. *errstring = "Invalid URI";
  73. return NULL;
  74. }
  75. char path_buf[URI_MAX];
  76. int len = get_norm_path(uri + 5, path_buf, 0, URI_MAX);
  77. if (len < 0) {
  78. *errstring = "Invalid URI";
  79. return NULL;
  80. }
  81. return alloc_concat("file:", static_strlen("file:"), path_buf, len);
  82. }
  83. static
  84. int scan_enclave_binary (int fd, unsigned long * base, unsigned long * size,
  85. unsigned long * entry)
  86. {
  87. int ret = 0;
  88. if (IS_ERR(ret = INLINE_SYSCALL(lseek, 3, fd, 0, SEEK_SET)))
  89. return -ERRNO(ret);
  90. char filebuf[FILEBUF_SIZE];
  91. ret = INLINE_SYSCALL(read, 3, fd, filebuf, FILEBUF_SIZE);
  92. if (IS_ERR(ret))
  93. return -ERRNO(ret);
  94. const ElfW(Ehdr) * header = (void *) filebuf;
  95. const ElfW(Phdr) * phdr = (void *) filebuf + header->e_phoff;
  96. const ElfW(Phdr) * ph;
  97. struct loadcmd {
  98. ElfW(Addr) mapstart, mapend;
  99. } loadcmds[16], *c;
  100. int nloadcmds = 0;
  101. for (ph = phdr ; ph < &phdr[header->e_phnum] ; ph++)
  102. if (ph->p_type == PT_LOAD) {
  103. if (nloadcmds == 16)
  104. return -EINVAL;
  105. c = &loadcmds[nloadcmds++];
  106. c->mapstart = ALLOC_ALIGNDOWN(ph->p_vaddr);
  107. c->mapend = ALLOC_ALIGNUP(ph->p_vaddr + ph->p_memsz);
  108. }
  109. *base = loadcmds[0].mapstart;
  110. *size = loadcmds[nloadcmds - 1].mapend - loadcmds[0].mapstart;
  111. if (entry)
  112. *entry = header->e_entry;
  113. return 0;
  114. }
  115. static
  116. int load_enclave_binary (sgx_arch_secs_t * secs, int fd,
  117. unsigned long base, unsigned long prot)
  118. {
  119. int ret = 0;
  120. if (IS_ERR(ret = INLINE_SYSCALL(lseek, 3, fd, 0, SEEK_SET)))
  121. return -ERRNO(ret);
  122. char filebuf[FILEBUF_SIZE];
  123. ret = INLINE_SYSCALL(read, 3, fd, filebuf, FILEBUF_SIZE);
  124. if (IS_ERR(ret))
  125. return -ERRNO(ret);
  126. const ElfW(Ehdr) * header = (void *) filebuf;
  127. const ElfW(Phdr) * phdr = (void *) filebuf + header->e_phoff;
  128. const ElfW(Phdr) * ph;
  129. struct loadcmd {
  130. ElfW(Addr) mapstart, mapend, datastart, dataend, allocend;
  131. unsigned int mapoff;
  132. int prot;
  133. } loadcmds[16], *c;
  134. int nloadcmds = 0;
  135. for (ph = phdr ; ph < &phdr[header->e_phnum] ; ph++)
  136. if (ph->p_type == PT_LOAD) {
  137. if (nloadcmds == 16)
  138. return -EINVAL;
  139. c = &loadcmds[nloadcmds++];
  140. c->mapstart = ALLOC_ALIGNDOWN(ph->p_vaddr);
  141. c->mapend = ALLOC_ALIGNUP(ph->p_vaddr + ph->p_filesz);
  142. c->datastart = ph->p_vaddr;
  143. c->dataend = ph->p_vaddr + ph->p_filesz;
  144. c->allocend = ph->p_vaddr + ph->p_memsz;
  145. c->mapoff = ALLOC_ALIGNDOWN(ph->p_offset);
  146. c->prot = (ph->p_flags & PF_R ? PROT_READ : 0)|
  147. (ph->p_flags & PF_W ? PROT_WRITE : 0)|
  148. (ph->p_flags & PF_X ? PROT_EXEC : 0)|prot;
  149. }
  150. base -= loadcmds[0].mapstart;
  151. for (c = loadcmds; c < &loadcmds[nloadcmds] ; c++) {
  152. ElfW(Addr) zero = c->dataend;
  153. ElfW(Addr) zeroend = ALLOC_ALIGNUP(c->allocend);
  154. ElfW(Addr) zeropage = ALLOC_ALIGNUP(zero);
  155. if (zeroend < zeropage)
  156. zeropage = zeroend;
  157. if (c->mapend > c->mapstart) {
  158. void * addr = (void *) INLINE_SYSCALL(mmap, 6, NULL,
  159. c->mapend - c->mapstart,
  160. PROT_READ|PROT_WRITE,
  161. MAP_PRIVATE | MAP_FILE,
  162. fd, c->mapoff);
  163. if (IS_ERR_P(addr))
  164. return -ERRNO_P(addr);
  165. if (c->datastart > c->mapstart)
  166. memset(addr, 0, c->datastart - c->mapstart);
  167. if (zeropage > zero)
  168. memset(addr + zero - c->mapstart, 0, zeropage - zero);
  169. ret = add_pages_to_enclave(secs, (void *) base + c->mapstart, addr,
  170. c->mapend - c->mapstart,
  171. SGX_PAGE_REG, c->prot, 0,
  172. (c->prot & PROT_EXEC) ? "code" : "data");
  173. INLINE_SYSCALL(munmap, 2, addr, c->mapend - c->mapstart);
  174. if (ret < 0)
  175. return ret;
  176. }
  177. if (zeroend > zeropage) {
  178. ret = add_pages_to_enclave(secs, (void *) base + zeropage, NULL,
  179. zeroend - zeropage,
  180. SGX_PAGE_REG, c->prot, false, "bss");
  181. if (ret < 0)
  182. return ret;
  183. }
  184. }
  185. return 0;
  186. }
  187. int initialize_enclave (struct pal_enclave * enclave)
  188. {
  189. int ret = 0;
  190. int enclave_image;
  191. int enclave_thread_num = 1;
  192. sgx_arch_token_t enclave_token;
  193. sgx_arch_sigstruct_t enclave_sigstruct;
  194. sgx_arch_secs_t enclave_secs;
  195. unsigned long enclave_entry_addr;
  196. void * tcs_addrs[MAX_DBG_THREADS];
  197. unsigned long heap_min = DEAFULT_HEAP_MIN;
  198. #define TRY(func, ...) \
  199. ({ \
  200. ret = func(__VA_ARGS__); \
  201. if (ret < 0) { \
  202. SGX_DBG(DBG_E, "initializing enclave failed: " #func ": %d\n", \
  203. -ret); \
  204. return ret; \
  205. } ret; \
  206. })
  207. enclave_image = INLINE_SYSCALL(open, 3, ENCLAVE_FILENAME, O_RDONLY, 0);
  208. if (IS_ERR(enclave_image)) {
  209. SGX_DBG(DBG_E, "cannot find %s\n", ENCLAVE_FILENAME);
  210. return -ERRNO(ret);
  211. }
  212. char cfgbuf[CONFIG_MAX];
  213. /* Reading sgx.enclave_size from manifest */
  214. if (get_config(enclave->config, "sgx.enclave_size", cfgbuf, CONFIG_MAX) <= 0) {
  215. SGX_DBG(DBG_E, "enclave_size is not specified\n");
  216. return -EINVAL;
  217. }
  218. enclave->size = parse_int(cfgbuf);
  219. /* DEP 1/21/17: SGX currently only supports power-of-two enclaves.
  220. * Give users a better warning about this. */
  221. if (enclave->size & (enclave->size - 1)) {
  222. SGX_DBG(DBG_E, "Enclave size not a power of two. SGX requires power-of-two enclaves.\n");
  223. return -EINVAL;
  224. }
  225. /* Reading sgx.thread_num from manifest */
  226. if (get_config(enclave->config, "sgx.thread_num", cfgbuf, CONFIG_MAX) > 0)
  227. enclave->thread_num = parse_int(cfgbuf);
  228. if (enclave_thread_num > MAX_DBG_THREADS) {
  229. SGX_DBG(DBG_E, "Too many threads to debug\n");
  230. return -EINVAL;
  231. }
  232. /* Reading sgx.static_address from manifest */
  233. if (get_config(enclave->config, "sgx.static_address", cfgbuf, CONFIG_MAX) > 0 &&
  234. cfgbuf[0] == '1')
  235. enclave->baseaddr = heap_min;
  236. else
  237. enclave->baseaddr = heap_min = 0;
  238. TRY(read_enclave_token, enclave->token, &enclave_token);
  239. TRY(read_enclave_sigstruct, enclave->sigfile, &enclave_sigstruct);
  240. TRY(create_enclave,
  241. &enclave_secs, enclave->baseaddr, enclave->size, &enclave_token);
  242. enclave->baseaddr = enclave_secs.baseaddr;
  243. enclave->size = enclave_secs.size;
  244. enclave->ssaframesize = enclave_secs.ssaframesize * pagesize;
  245. struct stat stat;
  246. ret = INLINE_SYSCALL(fstat, 2, enclave->manifest, &stat);
  247. if (IS_ERR(ret))
  248. return -ERRNO(ret);
  249. int manifest_size = stat.st_size;
  250. /* Start populating enclave memory */
  251. struct mem_area {
  252. const char * desc;
  253. bool skip_eextend;
  254. bool is_binary;
  255. int fd;
  256. unsigned long addr, size, prot;
  257. enum sgx_page_type type;
  258. };
  259. struct mem_area * areas =
  260. __alloca(sizeof(areas[0]) * (10 + enclave->thread_num));
  261. int area_num = 0;
  262. #define SET_AREA(_desc, _skip_eextend, _is_binary, _fd, _addr, _size, _prot, _type) \
  263. ({ \
  264. struct mem_area* _a = &areas[area_num++]; \
  265. _a->desc = _desc; \
  266. _a->skip_eextend = _skip_eextend; \
  267. _a->is_binary = _is_binary; \
  268. _a->fd = _fd; \
  269. _a->addr = _addr; \
  270. _a->size = _size; \
  271. _a->prot = _prot; \
  272. _a->type = _type; \
  273. _a; \
  274. })
  275. /* The manifest needs to be allocated at the upper end of the enclave
  276. * memory. That's used by pal_linux_main to find the manifest area. So add
  277. * it first to the list with memory areas. */
  278. SET_AREA("manifest", false, false, enclave->manifest,
  279. 0, ALLOC_ALIGNUP(manifest_size),
  280. PROT_READ, SGX_PAGE_REG);
  281. struct mem_area * ssa_area =
  282. SET_AREA("ssa", false, false, -1, 0,
  283. enclave->thread_num * enclave->ssaframesize * SSAFRAMENUM,
  284. PROT_READ|PROT_WRITE, SGX_PAGE_REG);
  285. struct mem_area * tcs_area =
  286. SET_AREA("tcs", false, false, -1, 0, enclave->thread_num * pagesize,
  287. 0, SGX_PAGE_TCS);
  288. struct mem_area * tls_area =
  289. SET_AREA("tls", false, false, -1, 0, enclave->thread_num * pagesize,
  290. PROT_READ|PROT_WRITE, SGX_PAGE_REG);
  291. struct mem_area * stack_areas = &areas[area_num];
  292. for (int t = 0 ; t < enclave->thread_num ; t++)
  293. SET_AREA("stack", false, false, -1, 0, ENCLAVE_STACK_SIZE,
  294. PROT_READ|PROT_WRITE, SGX_PAGE_REG);
  295. struct mem_area * pal_area =
  296. SET_AREA("pal", false, true, enclave_image, 0, 0, 0, SGX_PAGE_REG);
  297. TRY(scan_enclave_binary,
  298. enclave_image, &pal_area->addr, &pal_area->size, &enclave_entry_addr);
  299. struct mem_area * exec_area = NULL;
  300. if (enclave->exec != -1) {
  301. exec_area = SET_AREA("exec", false, true, enclave->exec, 0, 0,
  302. PROT_WRITE, SGX_PAGE_REG);
  303. TRY(scan_enclave_binary,
  304. enclave->exec, &exec_area->addr, &exec_area->size, NULL);
  305. }
  306. unsigned long populating = enclave->size;
  307. for (int i = 0 ; i < area_num ; i++) {
  308. if (areas[i].addr)
  309. continue;
  310. areas[i].addr = populating - areas[i].size;
  311. if (&areas[i] == exec_area)
  312. populating = areas[i].addr;
  313. else
  314. populating = areas[i].addr - MEMORY_GAP;
  315. }
  316. enclave_entry_addr += pal_area->addr;
  317. if (exec_area) {
  318. if (exec_area->addr + exec_area->size > pal_area->addr)
  319. return -EINVAL;
  320. if (exec_area->addr + exec_area->size < populating) {
  321. if (populating > heap_min) {
  322. unsigned long addr = exec_area->addr + exec_area->size;
  323. if (addr < heap_min)
  324. addr = heap_min;
  325. SET_AREA("free", true, false, -1, addr, populating - addr,
  326. PROT_READ|PROT_WRITE|PROT_EXEC, SGX_PAGE_REG);
  327. }
  328. populating = exec_area->addr;
  329. }
  330. }
  331. if (populating > heap_min) {
  332. SET_AREA("free", true, false, -1, heap_min, populating - heap_min,
  333. PROT_READ|PROT_WRITE|PROT_EXEC, SGX_PAGE_REG);
  334. }
  335. for (int i = 0 ; i < area_num ; i++) {
  336. if (areas[i].fd != -1 && areas[i].is_binary) {
  337. TRY(load_enclave_binary,
  338. &enclave_secs, areas[i].fd, areas[i].addr, areas[i].prot);
  339. continue;
  340. }
  341. void * data = NULL;
  342. if (strcmp_static(areas[i].desc, "tls")) {
  343. data = (void *) INLINE_SYSCALL(mmap, 6, NULL, areas[i].size,
  344. PROT_READ|PROT_WRITE,
  345. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  346. for (int t = 0 ; t < enclave->thread_num ; t++) {
  347. struct enclave_tls * gs = data + pagesize * t;
  348. gs->enclave_size = enclave->size;
  349. gs->tcs_offset = tcs_area->addr + pagesize * t;
  350. gs->initial_stack_offset =
  351. stack_areas[t].addr + ENCLAVE_STACK_SIZE;
  352. gs->ssa = (void *) ssa_area->addr +
  353. enclave->ssaframesize * SSAFRAMENUM * t +
  354. enclave_secs.baseaddr;
  355. gs->gpr = gs->ssa +
  356. enclave->ssaframesize - sizeof(sgx_arch_gpr_t);
  357. gs->manifest_size = manifest_size;
  358. gs->heap_min = (void *) enclave_secs.baseaddr + heap_min;
  359. gs->heap_max = (void *) enclave_secs.baseaddr + pal_area->addr - MEMORY_GAP;
  360. if (exec_area) {
  361. gs->exec_addr = (void *) enclave_secs.baseaddr + exec_area->addr;
  362. gs->exec_size = exec_area->size;
  363. }
  364. }
  365. } else if (strcmp_static(areas[i].desc, "tcs")) {
  366. data = (void *) INLINE_SYSCALL(mmap, 6, NULL, areas[i].size,
  367. PROT_READ|PROT_WRITE,
  368. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  369. for (int t = 0 ; t < enclave->thread_num ; t++) {
  370. sgx_arch_tcs_t * tcs = data + pagesize * t;
  371. memset(tcs, 0, pagesize);
  372. tcs->ossa = ssa_area->addr +
  373. enclave->ssaframesize * SSAFRAMENUM * t;
  374. tcs->nssa = SSAFRAMENUM;
  375. tcs->oentry = enclave_entry_addr;
  376. tcs->ofsbasgx = 0;
  377. tcs->ogsbasgx = tls_area->addr + t * pagesize;
  378. tcs->fslimit = 0xfff;
  379. tcs->gslimit = 0xfff;
  380. tcs_addrs[t] = (void *) enclave_secs.baseaddr + tcs_area->addr
  381. + pagesize * t;
  382. }
  383. } else if (areas[i].fd != -1)
  384. data = (void *) INLINE_SYSCALL(mmap, 6, NULL, areas[i].size,
  385. PROT_READ,
  386. MAP_FILE|MAP_PRIVATE,
  387. areas[i].fd, 0);
  388. TRY(add_pages_to_enclave,
  389. &enclave_secs, (void *) areas[i].addr, data, areas[i].size,
  390. areas[i].type, areas[i].prot, areas[i].skip_eextend,
  391. areas[i].desc);
  392. if (data)
  393. INLINE_SYSCALL(munmap, 2, data, areas[i].size);
  394. }
  395. TRY(init_enclave, &enclave_secs, &enclave_sigstruct, &enclave_token);
  396. create_tcs_mapper((void *) enclave_secs.baseaddr + tcs_area->addr,
  397. enclave->thread_num);
  398. struct enclave_dbginfo * dbg = (void *)
  399. INLINE_SYSCALL(mmap, 6, DBGINFO_ADDR,
  400. sizeof(struct enclave_dbginfo),
  401. PROT_READ|PROT_WRITE,
  402. MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,
  403. -1, 0);
  404. if (IS_ERR_P(dbg)) {
  405. SGX_DBG(DBG_E, "Cannot allocate debug info\n");
  406. return 0;
  407. }
  408. dbg->pid = INLINE_SYSCALL(getpid, 0);
  409. dbg->base = enclave->baseaddr;
  410. dbg->size = enclave->size;
  411. dbg->ssaframesize = enclave->ssaframesize;
  412. dbg->aep = async_exit_pointer;
  413. dbg->thread_tids[0] = dbg->pid;
  414. for (int i = 0 ; i < MAX_DBG_THREADS ; i++)
  415. dbg->tcs_addrs[i] = tcs_addrs[i];
  416. return 0;
  417. #undef SET_AREA
  418. #undef TRY
  419. }
  420. static int mcast_s (int port)
  421. {
  422. struct sockaddr_in addr;
  423. int ret = 0;
  424. addr.sin_family = AF_INET;
  425. addr.sin_addr.s_addr = INADDR_ANY;
  426. addr.sin_port = htons(port);
  427. int fd = INLINE_SYSCALL(socket, 3, AF_INET, SOCK_DGRAM, 0);
  428. if (IS_ERR(fd))
  429. return -PAL_ERROR_DENIED;
  430. ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_MULTICAST_IF,
  431. &addr.sin_addr.s_addr, sizeof(addr.sin_addr.s_addr));
  432. if (IS_ERR(ret))
  433. return -PAL_ERROR_DENIED;
  434. return fd;
  435. }
  436. static int mcast_c (int port)
  437. {
  438. int ret = 0, fd;
  439. struct sockaddr_in addr;
  440. addr.sin_family = AF_INET;
  441. addr.sin_addr.s_addr = INADDR_ANY;
  442. addr.sin_port = htons(port);
  443. fd = INLINE_SYSCALL(socket, 3, AF_INET, SOCK_DGRAM, 0);
  444. if (IS_ERR(fd))
  445. return -PAL_ERROR_DENIED;
  446. int reuse = 1;
  447. INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_REUSEADDR,
  448. &reuse, sizeof(reuse));
  449. ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr));
  450. if (IS_ERR(ret))
  451. return -PAL_ERROR_DENIED;
  452. ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_MULTICAST_IF,
  453. &addr.sin_addr.s_addr, sizeof(addr.sin_addr.s_addr));
  454. if (IS_ERR(ret))
  455. return -PAL_ERROR_DENIED;
  456. inet_pton4(MCAST_GROUP, sizeof(MCAST_GROUP) - 1,
  457. &addr.sin_addr.s_addr);
  458. struct ip_mreq group;
  459. group.imr_multiaddr.s_addr = addr.sin_addr.s_addr;
  460. group.imr_interface.s_addr = INADDR_ANY;
  461. ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  462. &group, sizeof(group));
  463. if (IS_ERR(ret))
  464. return -PAL_ERROR_DENIED;
  465. return fd;
  466. }
  467. static unsigned long randval = 0;
  468. void getrand (void * buffer, size_t size)
  469. {
  470. size_t bytes = 0;
  471. while (bytes + sizeof(uint64_t) <= size) {
  472. *(uint64_t*) (buffer + bytes) = randval;
  473. randval = hash64(randval);
  474. bytes += sizeof(uint64_t);
  475. }
  476. if (bytes < size) {
  477. memcpy(buffer + bytes, &randval, size - bytes);
  478. randval = hash64(randval);
  479. }
  480. }
  481. static void create_instance (struct pal_sec * pal_sec)
  482. {
  483. unsigned int id;
  484. getrand(&id, sizeof(id));
  485. snprintf(pal_sec->pipe_prefix, sizeof(pal_sec->pipe_prefix),
  486. "/graphene/%x/", id);
  487. pal_sec->instance_id = id;
  488. }
  489. int load_manifest (int fd, struct config_store ** config_ptr)
  490. {
  491. int retval = -EINVAL;
  492. int nbytes = INLINE_SYSCALL(lseek, 3, fd, 0, SEEK_END);
  493. if (IS_ERR(nbytes))
  494. return -ERRNO(nbytes);
  495. struct config_store * config = malloc(sizeof(struct config_store));
  496. if (!config)
  497. return -ENOMEM;
  498. void * config_raw = (void *)
  499. INLINE_SYSCALL(mmap, 6, NULL, nbytes,
  500. PROT_READ, MAP_PRIVATE,
  501. fd, 0);
  502. if (IS_ERR_P(config_raw)) {
  503. retval = -ERRNO_P(config_raw);
  504. goto finalize;
  505. }
  506. config->raw_data = config_raw;
  507. config->raw_size = nbytes;
  508. config->malloc = malloc;
  509. config->free = NULL;
  510. const char * errstring = NULL;
  511. int ret = read_config(config, NULL, &errstring);
  512. if (ret < 0) {
  513. SGX_DBG(DBG_E, "can't read manifest: %s\n", errstring);
  514. retval = ret;
  515. goto finalize;
  516. }
  517. *config_ptr = config;
  518. return 0;
  519. finalize:
  520. if (config) {
  521. free(config);
  522. }
  523. if (!IS_ERR_P(config_raw)) {
  524. INLINE_SYSCALL(munmap, 2, config_raw, nbytes);
  525. }
  526. return retval;
  527. }
  528. static int load_enclave (struct pal_enclave * enclave,
  529. const char * manifest_uri,
  530. const char * exec_uri,
  531. char * args, uint64_t args_size,
  532. char * env, uint64_t env_size,
  533. bool exec_uri_inferred)
  534. {
  535. struct pal_sec * pal_sec = &enclave->pal_sec;
  536. int ret;
  537. const char * errstring;
  538. struct timeval tv;
  539. #if PRINT_ENCLAVE_STAT == 1
  540. INLINE_SYSCALL(gettimeofday, 2, &tv, NULL);
  541. pal_sec->start_time = tv.tv_sec * 1000000UL + tv.tv_usec;
  542. #endif
  543. ret = open_gsgx();
  544. if (ret < 0)
  545. return ret;
  546. if (!is_wrfsbase_supported())
  547. return -EPERM;
  548. INLINE_SYSCALL(gettimeofday, 2, &tv, NULL);
  549. randval = tv.tv_sec * 1000000UL + tv.tv_usec;
  550. pal_sec->pid = INLINE_SYSCALL(getpid, 0);
  551. pal_sec->uid = INLINE_SYSCALL(getuid, 0);
  552. pal_sec->gid = INLINE_SYSCALL(getgid, 0);
  553. #ifdef DEBUG
  554. int env_i = 0;
  555. while (env_i < env_size) {
  556. if (strcmp_static(&env[env_i], "IN_GDB=1")) {
  557. SGX_DBG(DBG_I, "being GDB'ed!!!\n");
  558. pal_sec->in_gdb = true;
  559. }
  560. if (strcmp_static(&env[env_i], "LD_PRELOAD=")) {
  561. uint64_t env_i_size = strnlen(&env[env_i], env_size - env_i) + 1;
  562. memmove(&env[env_i], &env[env_i + env_i_size], env_size - env_i - env_i_size);
  563. env_size -= env_i_size;
  564. continue;
  565. }
  566. env_i += strnlen(&env[env_i], env_size - env_i) + 1;
  567. }
  568. #endif
  569. char cfgbuf[CONFIG_MAX];
  570. enclave->manifest = INLINE_SYSCALL(open, 3, manifest_uri + 5,
  571. O_RDONLY|O_CLOEXEC, 0);
  572. if (IS_ERR(enclave->manifest)) {
  573. SGX_DBG(DBG_E, "cannot open manifest %s\n", manifest_uri);
  574. return -EINVAL;
  575. }
  576. ret = load_manifest(enclave->manifest, &enclave->config);
  577. if (ret < 0) {
  578. SGX_DBG(DBG_E, "invalid manifest: %s\n", manifest_uri);
  579. return -EINVAL;
  580. }
  581. // A manifest can specify an executable with a different base name
  582. // than the manifest itself. Always give the exec field of the manifest
  583. // precedence if specified.
  584. if (get_config(enclave->config, "loader.exec", cfgbuf, CONFIG_MAX) > 0) {
  585. exec_uri = resolve_uri(cfgbuf, &errstring);
  586. exec_uri_inferred = false;
  587. if (!exec_uri) {
  588. SGX_DBG(DBG_E, "%s: %s\n", errstring, cfgbuf);
  589. return -EINVAL;
  590. }
  591. }
  592. if (exec_uri) {
  593. enclave->exec = INLINE_SYSCALL(open, 3,
  594. exec_uri + static_strlen("file:"),
  595. O_RDONLY|O_CLOEXEC, 0);
  596. if (IS_ERR(enclave->exec)) {
  597. if (exec_uri_inferred) {
  598. // It is valid for an enclave not to have an executable.
  599. // We need to catch the case where we inferred the executable
  600. // from the manifest file name, but it doesn't exist, and let
  601. // the enclave go a bit further. Go ahead and warn the user,
  602. // though.
  603. SGX_DBG(DBG_I, "Inferred executable cannot be opened: %s. This may be ok, or may represent a manifest misconfiguration. This typically represents advanced usage, and if it is not what you intended, try setting the loader.exec field in the manifest.\n", exec_uri);
  604. enclave->exec = -1;
  605. } else {
  606. SGX_DBG(DBG_E, "cannot open executable %s\n", exec_uri);
  607. return -EINVAL;
  608. }
  609. }
  610. } else {
  611. enclave->exec = -1;
  612. }
  613. if (get_config(enclave->config, "sgx.sigfile", cfgbuf, CONFIG_MAX) < 0) {
  614. SGX_DBG(DBG_E, "sigstruct file not found. Must have \'sgx.sigfile\' in the manifest\n");
  615. return -EINVAL;
  616. }
  617. const char * uri = resolve_uri(cfgbuf, &errstring);
  618. if (!uri) {
  619. SGX_DBG(DBG_E, "%s: %s\n", errstring, cfgbuf);
  620. return -EINVAL;
  621. }
  622. if (!strcmp_static(uri + strlen(uri) - 4, ".sig")) {
  623. SGX_DBG(DBG_E, "Invalid sigstruct file URI as %s\n", cfgbuf);
  624. return -EINVAL;
  625. }
  626. enclave->sigfile = INLINE_SYSCALL(open, 3, uri + 5, O_RDONLY|O_CLOEXEC, 0);
  627. if (IS_ERR(enclave->sigfile)) {
  628. SGX_DBG(DBG_E, "cannot open sigstruct file %s\n", uri);
  629. return -EINVAL;
  630. }
  631. uri = alloc_concat(uri, strlen(uri) - 4, ".token", -1);
  632. enclave->token = INLINE_SYSCALL(open, 3, uri + 5, O_RDONLY|O_CLOEXEC, 0);
  633. if (IS_ERR(enclave->token)) {
  634. SGX_DBG(DBG_E, "cannot open token \'%s\'. Use \'"
  635. PAL_FILE("pal-sgx-get-token")
  636. "\' on the runtime host, or run \'make SGX_RUN=1\' "
  637. "in the Graphene source, to create the token file.\n",
  638. uri);
  639. return -EINVAL;
  640. }
  641. SGX_DBG(DBG_I, "token file: %s\n", uri);
  642. /* Initialize the enclave */
  643. ret = initialize_enclave(enclave);
  644. if (ret < 0)
  645. return ret;
  646. if (!pal_sec->instance_id)
  647. create_instance(&enclave->pal_sec);
  648. memcpy(pal_sec->manifest_name, manifest_uri, strlen(manifest_uri) + 1);
  649. if (enclave->exec == -1) {
  650. memset(pal_sec->exec_name, 0, sizeof(PAL_SEC_STR));
  651. } else {
  652. memcpy(pal_sec->exec_name, exec_uri, strlen(exec_uri) + 1);
  653. }
  654. if (!pal_sec->mcast_port) {
  655. unsigned short mcast_port;
  656. getrand(&mcast_port, sizeof(unsigned short));
  657. pal_sec->mcast_port = mcast_port > 1024 ? mcast_port : mcast_port + 1024;
  658. }
  659. if ((ret = mcast_s(pal_sec->mcast_port)) >= 0) {
  660. pal_sec->mcast_srv = ret;
  661. if ((ret = mcast_c(pal_sec->mcast_port)) >= 0) {
  662. pal_sec->mcast_cli = ret;
  663. } else {
  664. INLINE_SYSCALL(close, 1, pal_sec->mcast_srv);
  665. pal_sec->mcast_srv = 0;
  666. }
  667. }
  668. /* setup signal handling */
  669. ret = sgx_signal_setup();
  670. if (ret < 0)
  671. return ret;
  672. current_enclave = enclave;
  673. map_tcs(INLINE_SYSCALL(gettid, 0));
  674. /* start running trusted PAL */
  675. ecall_enclave_start(args, args_size, env, env_size);
  676. #if PRINT_ENCLAVE_STAT == 1
  677. PAL_NUM exit_time = 0;
  678. INLINE_SYSCALL(gettimeofday, 2, &tv, NULL);
  679. exit_time = tv.tv_sec * 1000000UL + tv.tv_usec;
  680. #endif
  681. unmap_tcs();
  682. INLINE_SYSCALL(exit, 0);
  683. return 0;
  684. }
  685. int main (int argc, char ** argv, char ** envp)
  686. {
  687. const char * manifest_uri = NULL;
  688. char * exec_uri = NULL;
  689. const char * pal_loader = argv[0];
  690. int retval = -EINVAL;
  691. bool exec_uri_inferred = false; // Handle the case where the exec uri is
  692. // inferred from the manifest name somewhat
  693. // differently
  694. argc--;
  695. argv++;
  696. struct pal_enclave * enclave = malloc(sizeof(struct pal_enclave));
  697. if (!enclave)
  698. return -ENOMEM;
  699. memset(enclave, 0, sizeof(struct pal_enclave));
  700. int is_child = sgx_init_child_process(&enclave->pal_sec);
  701. if (is_child < 0) {
  702. retval = is_child;
  703. goto finalize;
  704. }
  705. if (!is_child) {
  706. /* occupy PROC_INIT_FD so no one will use it */
  707. INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
  708. if (!argc)
  709. goto usage;
  710. if (strcmp_static(argv[0], "file:")) {
  711. exec_uri = alloc_concat(argv[0], -1, NULL, -1);
  712. } else {
  713. exec_uri = alloc_concat("file:", -1, argv[0], -1);
  714. }
  715. } else {
  716. exec_uri = alloc_concat(enclave->pal_sec.exec_name, -1, NULL, -1);
  717. }
  718. int fd = INLINE_SYSCALL(open, 3, exec_uri + 5, O_RDONLY|O_CLOEXEC, 0);
  719. if (IS_ERR(fd)) {
  720. SGX_DBG(DBG_E, "Executable not found\n");
  721. SGX_DBG(DBG_E, "USAGE: <pal> [executable|manifest] args ...\n");
  722. retval = -ERRNO(fd);
  723. goto finalize;
  724. }
  725. char filebuf[4];
  726. /* check if the first argument is a executable. If it is, try finding
  727. all the possible manifest files */
  728. INLINE_SYSCALL(read, 3, fd, filebuf, 4);
  729. INLINE_SYSCALL(close, 1, fd);
  730. char sgx_manifest[URI_MAX];
  731. int len = get_base_name(exec_uri + static_strlen("file:"), sgx_manifest,
  732. URI_MAX);
  733. if (len < 0) {
  734. retval = len;
  735. goto finalize;
  736. }
  737. if (strcmp_static(sgx_manifest + len - strlen(".manifest"), ".manifest")) {
  738. strcpy_static(sgx_manifest + len, ".sgx", URI_MAX - len);
  739. } else if (!strcmp_static(sgx_manifest + len - strlen(".manifest.sgx"),
  740. ".manifest.sgx")) {
  741. strcpy_static(sgx_manifest + len, ".manifest.sgx", URI_MAX - len);
  742. }
  743. if (memcmp(filebuf, "\177ELF", 4)) {
  744. // In this case the manifest is given as the executable. Set
  745. // manifest_uri to sgx_manifest (should be the same), and
  746. // and drop the .manifest* from exec_uri, so that the program
  747. // loads properly.
  748. manifest_uri = sgx_manifest;
  749. size_t exec_len = strlen(exec_uri);
  750. if (strcmp_static(exec_uri + exec_len - strlen(".manifest"), ".manifest")) {
  751. exec_uri[exec_len - strlen(".manifest")] = '\0';
  752. exec_uri_inferred = true;
  753. } else if (strcmp_static(exec_uri + exec_len - strlen(".manifest.sgx"), ".manifest.sgx")) {
  754. exec_uri[exec_len - strlen(".manifest.sgx")] = '\0';
  755. exec_uri_inferred = true;
  756. }
  757. }
  758. fd = INLINE_SYSCALL(open, 3, sgx_manifest, O_RDONLY|O_CLOEXEC, 0);
  759. if (!IS_ERR(fd)) {
  760. manifest_uri = alloc_concat("file:", static_strlen("file:"),
  761. sgx_manifest, -1);
  762. INLINE_SYSCALL(close, 1, fd);
  763. } else if (!manifest_uri) {
  764. SGX_DBG(DBG_E, "cannot open manifest file: %s\n", sgx_manifest);
  765. goto usage;
  766. }
  767. SGX_DBG(DBG_I, "manifest file: %s\n", manifest_uri);
  768. if (exec_uri)
  769. SGX_DBG(DBG_I, "executable file: %s\n", exec_uri);
  770. else
  771. SGX_DBG(DBG_I, "executable file not found\n");
  772. /*
  773. * While C does not guarantee that the argv[i] and envp[i] strings are
  774. * continuous we know that we are running on Linux, which does this. This
  775. * saves us creating a copy of all argv and envp strings.
  776. */
  777. char * args = argv[0];
  778. uint64_t args_size = argc > 0 ? (argv[argc - 1] - argv[0]) + strlen(argv[argc - 1]) + 1: 0;
  779. int envc = 0;
  780. while (envp[envc] != NULL) {
  781. envc++;
  782. }
  783. char * env = envp[0];
  784. uint64_t env_size = envc > 0 ? (envp[envc - 1] - envp[0]) + strlen(envp[envc - 1]) + 1: 0;
  785. return load_enclave(enclave, manifest_uri, exec_uri,
  786. args, args_size, env, env_size,
  787. exec_uri_inferred);
  788. usage:
  789. SGX_DBG(DBG_E, "USAGE: %s [executable|manifest] args ...\n", pal_loader);
  790. retval = -EINVAL;
  791. goto finalize;
  792. finalize:
  793. if (enclave) {
  794. free(enclave);
  795. }
  796. return retval;
  797. }