sgx_main.c 29 KB

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