-
Notifications
You must be signed in to change notification settings - Fork 202
/
sgx_main.c
1199 lines (1035 loc) · 42.2 KB
/
sgx_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University
* Copyright (C) 2020 Intel Corporation
* Michał Kowalczyk <[email protected]>
* Copyright (C) 2020 Invisible Things Lab
* Michał Kowalczyk <[email protected]>
*/
#include <asm/errno.h>
#include <asm/fcntl.h>
#include <asm/socket.h>
#include <linux/fs.h>
#include "asan.h"
#include "debug_map.h"
#include "gdb_integration/sgx_gdb.h"
#include "gsgx.h"
#include "linux_utils.h"
#include "pal_internal-arch.h"
#include "pal_linux_defs.h"
#include "pal_linux_error.h"
#include "pal_rtld.h"
#include "rpc_queue.h"
#include "sgx_enclave.h"
#include "sgx_internal.h"
#include "sgx_log.h"
#include "sgx_tls.h"
#include "toml.h"
#include "toml_utils.h"
#include "topo_info.h"
const size_t g_page_size = PRESET_PAGESIZE;
char* g_pal_loader_path = NULL;
char* g_libpal_path = NULL;
pid_t g_host_pid;
bool g_vtune_profile_enabled = false;
struct pal_enclave g_pal_enclave;
static int read_file_fragment(int fd, void* buf, size_t size, off_t offset) {
ssize_t ret;
ret = DO_SYSCALL(lseek, fd, offset, SEEK_SET);
if (ret < 0)
return ret;
return read_all(fd, buf, size);
}
static int load_elf_headers(int fd, elf_ehdr_t* out_ehdr, elf_phdr_t** out_phdr) {
elf_ehdr_t ehdr;
int ret = read_file_fragment(fd, &ehdr, sizeof(ehdr), /*offset=*/0);
if (ret < 0)
return ret;
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
return -ENOEXEC;
size_t phdr_size = ehdr.e_phnum * sizeof(elf_phdr_t);
elf_phdr_t* phdr = malloc(phdr_size);
if (!phdr)
return -ENOMEM;
ret = read_file_fragment(fd, phdr, phdr_size, ehdr.e_phoff);
if (ret < 0) {
free(phdr);
return ret;
}
*out_ehdr = ehdr;
*out_phdr = phdr;
return 0;
}
static int scan_enclave_binary(int fd, unsigned long* base, unsigned long* size,
unsigned long* entry) {
int ret;
elf_ehdr_t ehdr;
elf_phdr_t* phdr;
ret = load_elf_headers(fd, &ehdr, &phdr);
if (ret < 0)
return ret;
struct loadcmd {
elf_addr_t mapstart, mapend;
} loadcmds[16], *c;
int nloadcmds = 0;
const elf_phdr_t* ph;
for (ph = phdr; ph < &phdr[ehdr.e_phnum]; ph++)
if (ph->p_type == PT_LOAD) {
if (nloadcmds == 16) {
ret = -EINVAL;
goto out;
}
c = &loadcmds[nloadcmds++];
c->mapstart = ALLOC_ALIGN_DOWN(ph->p_vaddr);
c->mapend = ALLOC_ALIGN_UP(ph->p_vaddr + ph->p_memsz);
}
*base = loadcmds[0].mapstart;
*size = loadcmds[nloadcmds - 1].mapend - loadcmds[0].mapstart;
if (entry)
*entry = ehdr.e_entry;
ret = 0;
out:
free(phdr);
return ret;
}
static int load_enclave_binary(sgx_arch_secs_t* secs, int fd, unsigned long base,
unsigned long prot) {
int ret;
elf_ehdr_t ehdr;
elf_phdr_t* phdr;
ret = load_elf_headers(fd, &ehdr, &phdr);
if (ret < 0)
return ret;
struct loadcmd {
elf_addr_t mapstart, mapend, datastart, dataend, allocend;
unsigned int mapoff;
int prot;
} loadcmds[16], *c;
int nloadcmds = 0;
elf_phdr_t* ph;
for (ph = phdr; ph < &phdr[ehdr.e_phnum]; ph++)
if (ph->p_type == PT_LOAD) {
if (nloadcmds == 16) {
ret = -EINVAL;
goto out;
}
c = &loadcmds[nloadcmds++];
c->mapstart = ALLOC_ALIGN_DOWN(ph->p_vaddr);
c->mapend = ALLOC_ALIGN_UP(ph->p_vaddr + ph->p_filesz);
c->datastart = ph->p_vaddr;
c->dataend = ph->p_vaddr + ph->p_filesz;
c->allocend = ph->p_vaddr + ph->p_memsz;
c->mapoff = ALLOC_ALIGN_DOWN(ph->p_offset);
c->prot = (ph->p_flags & PF_R ? PROT_READ : 0) | (ph->p_flags & PF_W ? PROT_WRITE : 0) |
(ph->p_flags & PF_X ? PROT_EXEC : 0) | prot;
}
base -= loadcmds[0].mapstart;
for (c = loadcmds; c < &loadcmds[nloadcmds]; c++) {
elf_addr_t zero = c->dataend;
elf_addr_t zeroend = ALLOC_ALIGN_UP(c->allocend);
elf_addr_t zeropage = ALLOC_ALIGN_UP(zero);
if (zeroend < zeropage)
zeropage = zeroend;
if (c->mapend > c->mapstart) {
void* addr = (void*)DO_SYSCALL(mmap, NULL, c->mapend - c->mapstart,
PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, c->mapoff);
if (IS_PTR_ERR(addr)) {
ret = PTR_TO_ERR(addr);
goto out;
}
if (c->datastart > c->mapstart)
memset(addr, 0, c->datastart - c->mapstart);
if (zeropage > zero)
memset(addr + zero - c->mapstart, 0, zeropage - zero);
ret = add_pages_to_enclave(secs, (void*)base + c->mapstart, addr,
c->mapend - c->mapstart,
SGX_PAGE_REG, c->prot, /*skip_eextend=*/false,
(c->prot & PROT_EXEC) ? "code" : "data");
DO_SYSCALL(munmap, addr, c->mapend - c->mapstart);
if (ret < 0)
goto out;
}
if (zeroend > zeropage) {
ret = add_pages_to_enclave(secs, (void*)base + zeropage, NULL, zeroend - zeropage,
SGX_PAGE_REG, c->prot, false, "bss");
if (ret < 0)
goto out;
}
}
ret = 0;
out:
free(phdr);
return ret;
}
static int initialize_enclave(struct pal_enclave* enclave, const char* manifest_to_measure) {
int ret = 0;
int enclave_image = -1;
int enclave_mem = -1;
sgx_arch_secs_t enclave_secs;
unsigned long enclave_entry_addr;
unsigned long enclave_heap_min;
sgx_arch_enclave_css_t enclave_sigstruct;
char* sig_path = NULL;
int sigfile_fd = -1;
/* Launch Token (aka EINITTOKEN) is used only on EPID (non-FLC-based) platforms and completely
* ignored on DCAP (FLC-based) platforms */
sgx_arch_token_t enclave_token;
char* token_path = NULL;
int token_fd = -1;
/* this array may overflow the stack, so we allocate it in BSS */
static void* tcs_addrs[MAX_DBG_THREADS];
enclave_image = DO_SYSCALL(open, enclave->libpal_uri + URI_PREFIX_FILE_LEN, O_RDONLY, 0);
if (enclave_image < 0) {
log_error("Cannot find enclave image: %s", enclave->libpal_uri);
ret = enclave_image;
goto out;
}
if (enclave->nonpie_binary) {
/* executable is non-PIE: enclave base address must cover code segment loaded at some
* hardcoded address (usually 0x400000), and heap cannot start at zero (modern OSes do not
* allow this) */
enclave->baseaddr = DEFAULT_ENCLAVE_BASE;
enclave_heap_min = MMAP_MIN_ADDR;
} else {
/* executable is PIE: enclave base address can be arbitrary (we choose it same as
* enclave_size), and heap can start immediately at this base address */
enclave->baseaddr = enclave->size;
enclave_heap_min = enclave->baseaddr;
}
sig_path = alloc_concat(g_pal_enclave.application_path, -1, ".sig", -1);
if (!sig_path) {
ret = -ENOMEM;
goto out;
}
sigfile_fd = DO_SYSCALL(open, sig_path, O_RDONLY | O_CLOEXEC, 0);
if (sigfile_fd < 0) {
log_error("Cannot open sigstruct file %s", sig_path);
ret = -EINVAL;
goto out;
}
ret = read_enclave_sigstruct(sigfile_fd, &enclave_sigstruct);
if (ret < 0) {
log_error("Reading enclave sigstruct failed: %d", ret);
goto out;
}
#ifndef SGX_DCAP
token_path = alloc_concat(g_pal_enclave.application_path, -1, ".token", -1);
if (!token_path) {
ret = -ENOMEM;
goto out;
}
token_fd = DO_SYSCALL(open, token_path, O_RDONLY | O_CLOEXEC, 0);
if (token_fd < 0) {
log_error("Cannot open token %s. Use gramine-sgx-get-token on the runtime host to create "
"the token file.", token_path);
ret = -EINVAL;
goto out;
}
log_debug("Token file: %s", token_path);
ret = read_enclave_token(token_fd, &enclave_token);
if (ret < 0) {
log_error("Reading enclave token failed: %d", ret);
goto out;
}
#endif
#ifdef DEBUG
if (enclave->profile_enable) {
if (!(enclave_sigstruct.body.attributes.flags & SGX_FLAGS_DEBUG)) {
log_error("Cannot use 'sgx.profile' with a production enclave");
ret = -EINVAL;
goto out;
}
ret = sgx_profile_init();
if (ret < 0)
goto out;
/* Report all ELFs that are already loaded (URTS and dynamic libraries used by it) */
struct debug_map* map = g_debug_map;
while (map) {
sgx_profile_report_elf(map->name, map->addr);
map = map->next;
}
}
#endif
memset(&enclave_secs, 0, sizeof(enclave_secs));
enclave_secs.base = enclave->baseaddr;
enclave_secs.size = enclave->size;
ret = create_enclave(&enclave_secs, &enclave_sigstruct);
if (ret < 0) {
log_error("Creating enclave failed: %d", ret);
goto out;
}
/* SECS contains SSA frame size in pages, convert to size in bytes */
enclave->ssa_frame_size = enclave_secs.ssa_frame_size * g_page_size;
/* Start populating enclave memory */
struct mem_area {
const char* desc;
bool skip_eextend;
enum {
ELF_FD, // read from `fd` and parse as ELF
ZERO,
BUF,
TCS,
TLS
} data_src;
union {
int fd; // valid iff data_src == ELF_FD
struct { // valid iff data_src == BUF
const char* buf;
size_t buf_size;
};
};
unsigned long addr, size, prot;
enum sgx_page_type type;
};
/*
* 10 for manifest, SSA, TCS, etc
* + enclave->thread_num for normal stack
* + enclave->thread_num for signal stack
*/
int max_area_cnt = 10 + enclave->thread_num * 2;
struct mem_area* areas = __alloca(sizeof(areas[0]) * max_area_cnt);
int area_num = 0;
/* The manifest needs to be allocated at the upper end of the enclave
* memory. That's used by pal_linux_main to find the manifest area. So add
* it first to the list with memory areas. */
size_t manifest_size = strlen(manifest_to_measure) + 1;
areas[area_num] = (struct mem_area){.desc = "manifest",
.skip_eextend = false,
.data_src = BUF,
.buf = manifest_to_measure,
.buf_size = manifest_size,
.addr = 0,
.size = ALLOC_ALIGN_UP(manifest_size),
.prot = PROT_READ,
.type = SGX_PAGE_REG};
area_num++;
areas[area_num] =
(struct mem_area){.desc = "ssa",
.skip_eextend = false,
.data_src = ZERO,
.addr = 0,
.size = enclave->thread_num * enclave->ssa_frame_size *
SSA_FRAME_NUM,
.prot = PROT_READ | PROT_WRITE,
.type = SGX_PAGE_REG};
struct mem_area* ssa_area = &areas[area_num++];
areas[area_num] = (struct mem_area){.desc = "tcs",
.skip_eextend = false,
.data_src = TCS,
.addr = 0,
.size = enclave->thread_num * g_page_size,
.prot = PROT_READ | PROT_WRITE,
.type = SGX_PAGE_TCS};
struct mem_area* tcs_area = &areas[area_num++];
areas[area_num] = (struct mem_area){.desc = "tls",
.skip_eextend = false,
.data_src = TLS,
.addr = 0,
.size = enclave->thread_num * g_page_size,
.prot = PROT_READ | PROT_WRITE,
.type = SGX_PAGE_REG};
struct mem_area* tls_area = &areas[area_num++];
struct mem_area* stack_areas = &areas[area_num]; /* memorize for later use */
for (uint32_t t = 0; t < enclave->thread_num; t++) {
areas[area_num] = (struct mem_area){.desc = "stack",
.skip_eextend = false,
.data_src = ZERO,
.addr = 0,
.size = ENCLAVE_STACK_SIZE,
.prot = PROT_READ | PROT_WRITE,
.type = SGX_PAGE_REG};
area_num++;
}
struct mem_area* sig_stack_areas = &areas[area_num]; /* memorize for later use */
for (uint32_t t = 0; t < enclave->thread_num; t++) {
areas[area_num] = (struct mem_area){.desc = "sig_stack",
.skip_eextend = false,
.data_src = ZERO,
.addr = 0,
.size = ENCLAVE_SIG_STACK_SIZE,
.prot = PROT_READ | PROT_WRITE,
.type = SGX_PAGE_REG};
area_num++;
}
areas[area_num] = (struct mem_area){.desc = "pal",
.skip_eextend = false,
.data_src = ELF_FD,
.fd = enclave_image,
/* `addr` and `size` are set below */
.prot = 0,
.type = SGX_PAGE_REG};
struct mem_area* pal_area = &areas[area_num++];
ret = scan_enclave_binary(enclave_image, &pal_area->addr, &pal_area->size, &enclave_entry_addr);
if (ret < 0) {
log_error("Scanning Pal binary (%s) failed: %d", enclave->libpal_uri, ret);
goto out;
}
uintptr_t last_populated_addr = enclave->baseaddr + enclave->size;
for (int i = 0; i < area_num; i++) {
if (areas[i].addr)
continue;
areas[i].addr = last_populated_addr - areas[i].size;
last_populated_addr = areas[i].addr;
}
enclave_entry_addr += pal_area->addr;
struct mem_area* free_area = NULL;
if (last_populated_addr > enclave_heap_min) {
areas[area_num] = (struct mem_area){.desc = "free",
.skip_eextend = true,
.data_src = ZERO,
.addr = enclave_heap_min,
.size = last_populated_addr - enclave_heap_min,
.prot = PROT_READ | PROT_WRITE | PROT_EXEC,
.type = SGX_PAGE_REG};
free_area = &areas[area_num++];
}
log_debug("Adding pages to SGX enclave, this may take some time...");
for (int i = 0; i < area_num; i++) {
if (areas[i].data_src == ELF_FD) {
ret = load_enclave_binary(&enclave_secs, areas[i].fd, areas[i].addr, areas[i].prot);
if (ret < 0) {
log_error("Loading enclave binary failed: %d", ret);
goto out;
}
continue;
}
void* data = NULL;
if (areas[i].data_src != ZERO) {
data = (void*)DO_SYSCALL(mmap, NULL, areas[i].size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (IS_PTR_ERR(data) || data == NULL) {
/* Note that Gramine currently doesn't handle 0x0 addresses */
log_error("Allocating memory failed");
ret = -ENOMEM;
goto out;
}
}
if (areas[i].data_src == TLS) {
for (uint32_t t = 0; t < enclave->thread_num; t++) {
struct enclave_tls* gs = data + g_page_size * t;
memset(gs, 0, g_page_size);
assert(sizeof(*gs) <= g_page_size);
gs->common.self = (PAL_TCB*)(tls_area->addr + g_page_size * t);
gs->common.stack_protector_canary = STACK_PROTECTOR_CANARY_DEFAULT;
gs->enclave_size = enclave->size;
gs->tcs_offset = tcs_area->addr - enclave->baseaddr + g_page_size * t;
gs->initial_stack_addr = stack_areas[t].addr + ENCLAVE_STACK_SIZE;
gs->sig_stack_low = sig_stack_areas[t].addr;
gs->sig_stack_high = sig_stack_areas[t].addr + ENCLAVE_SIG_STACK_SIZE;
gs->ssa = (void*)ssa_area->addr + enclave->ssa_frame_size * SSA_FRAME_NUM * t;
gs->gpr = gs->ssa + enclave->ssa_frame_size - sizeof(sgx_pal_gpr_t);
gs->manifest_size = manifest_size;
gs->heap_min = (void*)enclave_heap_min;
gs->heap_max = (void*)pal_area->addr;
gs->thread = NULL;
}
} else if (areas[i].data_src == TCS) {
for (uint32_t t = 0; t < enclave->thread_num; t++) {
sgx_arch_tcs_t* tcs = data + g_page_size * t;
memset(tcs, 0, g_page_size);
// .ossa, .oentry, .ofs_base and .ogs_base are offsets from enclave base, not VAs.
tcs->ossa = ssa_area->addr - enclave->baseaddr
+ enclave->ssa_frame_size * SSA_FRAME_NUM * t;
tcs->nssa = SSA_FRAME_NUM;
tcs->oentry = enclave_entry_addr - enclave->baseaddr;
tcs->ofs_base = 0;
tcs->ogs_base = tls_area->addr - enclave->baseaddr + t * g_page_size;
tcs->ofs_limit = 0xfff;
tcs->ogs_limit = 0xfff;
tcs_addrs[t] = (void*)tcs_area->addr + g_page_size * t;
}
} else if (areas[i].data_src == BUF) {
memcpy(data, areas[i].buf, areas[i].buf_size);
} else {
assert(areas[i].data_src == ZERO);
}
ret = add_pages_to_enclave(&enclave_secs, (void*)areas[i].addr, data, areas[i].size,
areas[i].type, areas[i].prot, areas[i].skip_eextend,
areas[i].desc);
if (data)
DO_SYSCALL(munmap, data, areas[i].size);
if (ret < 0) {
log_error("Adding pages (%s) to enclave failed: %d", areas[i].desc, ret);
goto out;
}
}
log_debug("Added all pages to SGX enclave");
ret = init_enclave(&enclave_secs, &enclave_sigstruct, &enclave_token);
if (ret < 0) {
log_error("Initializing enclave failed: %d", ret);
goto out;
}
create_tcs_mapper((void*)tcs_area->addr, enclave->thread_num);
struct enclave_dbginfo* dbg = (void*)DO_SYSCALL(mmap, DBGINFO_ADDR,
sizeof(struct enclave_dbginfo),
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (IS_PTR_ERR(dbg)) {
log_warning("Cannot allocate debug information (GDB will not work)");
} else {
dbg->pid = g_host_pid;
dbg->base = enclave->baseaddr;
dbg->size = enclave->size;
dbg->ssa_frame_size = enclave->ssa_frame_size;
dbg->aep = async_exit_pointer;
dbg->eresume = eresume_pointer;
dbg->thread_tids[0] = dbg->pid;
for (int i = 0; i < MAX_DBG_THREADS; i++)
dbg->tcs_addrs[i] = tcs_addrs[i];
}
if (g_sgx_enable_stats || g_vtune_profile_enabled) {
/* set TCS.FLAGS.DBGOPTIN in all enclave threads to enable perf counters, Intel PT, etc */
ret = DO_SYSCALL(open, "/proc/self/mem", O_RDWR | O_LARGEFILE, 0);
if (ret < 0) {
log_error("Setting TCS.FLAGS.DBGOPTIN failed: %d", ret);
goto out;
}
enclave_mem = ret;
for (size_t i = 0; i < enclave->thread_num; i++) {
uint64_t tcs_flags;
uint64_t* tcs_flags_ptr = tcs_addrs[i] + offsetof(sgx_arch_tcs_t, flags);
ret = DO_SYSCALL(pread64, enclave_mem, &tcs_flags, sizeof(tcs_flags),
(off_t)tcs_flags_ptr);
if (ret < 0) {
log_error("Reading TCS.FLAGS.DBGOPTIN failed: %d", ret);
goto out;
}
tcs_flags |= TCS_FLAGS_DBGOPTIN;
ret = DO_SYSCALL(pwrite64, enclave_mem, &tcs_flags, sizeof(tcs_flags),
(off_t)tcs_flags_ptr);
if (ret < 0) {
log_error("Writing TCS.FLAGS.DBGOPTIN failed: %d", ret);
goto out;
}
}
}
#ifdef DEBUG
/*
* Report libpal map. All subsequent files will be reported via DkDebugMapAdd(), but this
* one has to be handled separately.
*
* We report it here, before enclave start (as opposed to setup_pal_binary()), because we want
* both GDB integration and profiling to be active from the very beginning of enclave execution.
*/
debug_map_add(enclave->libpal_uri + URI_PREFIX_FILE_LEN, (void*)pal_area->addr);
sgx_profile_report_elf(enclave->libpal_uri + URI_PREFIX_FILE_LEN, (void*)pal_area->addr);
#endif
#ifdef ASAN
if (free_area)
asan_poison_region(free_area->addr, free_area->size, ASAN_POISON_USER);
#else
__UNUSED(free_area);
#endif
ret = 0;
out:
if (enclave_image >= 0)
DO_SYSCALL(close, enclave_image);
if (enclave_mem >= 0)
DO_SYSCALL(close, enclave_mem);
if (sigfile_fd >= 0)
DO_SYSCALL(close, sigfile_fd);
if (token_fd >= 0)
DO_SYSCALL(close, token_fd);
free(sig_path);
free(token_path);
return ret;
}
/* Parses only the information needed by the untrusted PAL to correctly initialize the enclave. */
static int parse_loader_config(char* manifest, struct pal_enclave* enclave_info) {
int ret = 0;
toml_table_t* manifest_root = NULL;
char* dummy_sigfile_str = NULL;
char* sgx_ra_client_spid_str = NULL;
char* profile_str = NULL;
#ifdef DEBUG
char* profile_mode_str = NULL;
#endif
char* log_level_str = NULL;
char* log_file = NULL;
char errbuf[256];
manifest_root = toml_parse(manifest, errbuf, sizeof(errbuf));
if (!manifest_root) {
log_error("PAL failed at parsing the manifest: %s", errbuf);
ret = -EINVAL;
goto out;
}
ret = toml_sizestring_in(manifest_root, "sgx.enclave_size", /*defaultval=*/0,
&enclave_info->size);
if (ret < 0) {
log_error("Cannot parse 'sgx.enclave_size'");
ret = -EINVAL;
goto out;
}
if (!enclave_info->size || !IS_POWER_OF_2(enclave_info->size)) {
log_error("Enclave size not a power of two (an SGX-imposed requirement)");
ret = -EINVAL;
goto out;
}
int64_t thread_num_int64;
ret = toml_int_in(manifest_root, "sgx.thread_num", /*defaultval=*/0, &thread_num_int64);
if (ret < 0) {
log_error("Cannot parse 'sgx.thread_num'");
ret = -EINVAL;
goto out;
}
if (thread_num_int64 < 0) {
log_error("Negative 'sgx.thread_num' is impossible");
ret = -EINVAL;
goto out;
}
enclave_info->thread_num = thread_num_int64;
if (!enclave_info->thread_num) {
log_warning("Number of enclave threads ('sgx.thread_num') is not specified; assumed "
"to be 1");
enclave_info->thread_num = 1;
}
if (enclave_info->thread_num > MAX_DBG_THREADS) {
log_error("Too large 'sgx.thread_num', maximum allowed is %d", MAX_DBG_THREADS);
ret = -EINVAL;
goto out;
}
int64_t rpc_thread_num_int64;
ret = toml_int_in(manifest_root, "sgx.rpc_thread_num", /*defaultval=*/0, &rpc_thread_num_int64);
if (ret < 0) {
log_error("Cannot parse 'sgx.rpc_thread_num'");
ret = -EINVAL;
goto out;
}
if (rpc_thread_num_int64 < 0) {
log_error("Negative 'sgx.rpc_thread_num' is impossible");
ret = -EINVAL;
goto out;
}
enclave_info->rpc_thread_num = rpc_thread_num_int64;
if (enclave_info->rpc_thread_num > MAX_RPC_THREADS) {
log_error("Too large 'sgx.rpc_thread_num', maximum allowed is %d", MAX_RPC_THREADS);
ret = -EINVAL;
goto out;
}
if (enclave_info->rpc_thread_num && enclave_info->thread_num > RPC_QUEUE_SIZE) {
log_error("Too many threads for exitless feature (more than capacity of RPC queue)");
ret = -EINVAL;
goto out;
}
bool nonpie_binary;
ret = toml_bool_in(manifest_root, "sgx.nonpie_binary", /*defaultval=*/false, &nonpie_binary);
if (ret < 0) {
log_error("Cannot parse 'sgx.nonpie_binary' (the value must be `true` or `false`)");
ret = -EINVAL;
goto out;
}
enclave_info->nonpie_binary = nonpie_binary;
ret = toml_bool_in(manifest_root, "sgx.enable_stats", /*defaultval=*/false,
&g_sgx_enable_stats);
if (ret < 0) {
log_error("Cannot parse 'sgx.enable_stats' (the value must be `true` or `false`)");
ret = -EINVAL;
goto out;
}
ret = toml_string_in(manifest_root, "sgx.sigfile", &dummy_sigfile_str);
if (ret < 0 || dummy_sigfile_str) {
log_error("sgx.sigfile is not supported anymore. Please update your manifest according to "
"the current documentation.");
ret = -EINVAL;
goto out;
}
bool sgx_remote_attestation_enabled;
ret = toml_bool_in(manifest_root, "sgx.remote_attestation", /*defaultval=*/false,
&sgx_remote_attestation_enabled);
if (ret < 0) {
log_error("Cannot parse 'sgx.remote_attestation' (the value must be `true` or `false`)");
ret = -EINVAL;
goto out;
}
enclave_info->remote_attestation_enabled = sgx_remote_attestation_enabled;
ret = toml_string_in(manifest_root, "sgx.ra_client_spid", &sgx_ra_client_spid_str);
if (ret < 0) {
log_error("Cannot parse 'sgx.ra_client_spid'");
ret = -EINVAL;
goto out;
}
bool sgx_ra_client_linkable_specified =
toml_key_exists(manifest_root, "sgx.ra_client_linkable");
if (!enclave_info->remote_attestation_enabled &&
(sgx_ra_client_spid_str || sgx_ra_client_linkable_specified)) {
log_error(
"Detected EPID remote attestation parameters 'ra_client_spid' and/or "
"'ra_client_linkable' in the manifest but no 'remote_attestation' parameter. "
"Please add 'sgx.remote_attestation = true' to the manifest.");
ret = -EINVAL;
goto out;
}
/* EPID is used if SPID is a non-empty string in manifest, otherwise DCAP/ECDSA */
enclave_info->use_epid_attestation = sgx_ra_client_spid_str && strlen(sgx_ra_client_spid_str);
ret = toml_string_in(manifest_root, "sgx.profile.enable", &profile_str);
if (ret < 0) {
log_error("Cannot parse 'sgx.profile.enable' "
"(the value must be \"none\", \"main\" or \"all\")");
ret = -EINVAL;
goto out;
}
ret = toml_bool_in(manifest_root, "sgx.vtune_profile", /*defaultval=*/false, &g_vtune_profile_enabled);
if (ret < 0) {
log_error("Cannot parse 'sgx.vtune_profile' (the value must be `true` or `false`)");
ret = -EINVAL;
goto out;
}
#ifndef SGX_VTUNE_PROFILE
if (g_vtune_profile_enabled)
log_always("Gramine was not built with VTune profiling support, "
"'sgx.vtune_profile' manifest option has no impact.");
#endif
#ifdef DEBUG
enclave_info->profile_enable = false;
enclave_info->profile_filename[0] = '\0';
if (!profile_str || !strcmp(profile_str, "none")) {
// do not enable
} else if (!strcmp(profile_str, "main")) {
if (enclave_info->is_first_process) {
snprintf(enclave_info->profile_filename, ARRAY_SIZE(enclave_info->profile_filename),
SGX_PROFILE_FILENAME);
enclave_info->profile_enable = true;
}
} else if (!strcmp(profile_str, "all")) {
enclave_info->profile_enable = true;
snprintf(enclave_info->profile_filename, ARRAY_SIZE(enclave_info->profile_filename),
SGX_PROFILE_FILENAME_WITH_PID, (int)g_host_pid);
} else {
log_error("Invalid 'sgx.profile.enable' "
"(the value must be \"none\", \"main\" or \"all\")");
ret = -EINVAL;
goto out;
}
ret = toml_string_in(manifest_root, "sgx.profile.mode", &profile_mode_str);
if (ret < 0) {
log_error("Cannot parse 'sgx.profile.mode' "
"(the value must be \"aex\", \"ocall_inner\" or \"ocall_outer\")");
ret = -EINVAL;
goto out;
}
if (!profile_mode_str) {
enclave_info->profile_mode = SGX_PROFILE_MODE_AEX;
} else if (!strcmp(profile_mode_str, "aex")) {
enclave_info->profile_mode = SGX_PROFILE_MODE_AEX;
} else if (!strcmp(profile_mode_str, "ocall_inner")) {
enclave_info->profile_mode = SGX_PROFILE_MODE_OCALL_INNER;
} else if (!strcmp(profile_mode_str, "ocall_outer")) {
enclave_info->profile_mode = SGX_PROFILE_MODE_OCALL_OUTER;
} else {
log_error("Invalid 'sgx.profile.mode' "
"(the value must be \"aex\", \"ocall_inner\" or \"ocall_outer\")");
ret = -EINVAL;
goto out;
}
bool profile_with_stack;
ret = toml_bool_in(manifest_root, "sgx.profile.with_stack", /*defaultval=*/false,
&profile_with_stack);
if (ret < 0) {
log_error("Cannot parse 'sgx.profile.with_stack' (the value must be `true` or `false`)");
ret = -EINVAL;
goto out;
}
enclave_info->profile_with_stack = profile_with_stack;
if (enclave_info->profile_with_stack &&
enclave_info->profile_mode == SGX_PROFILE_MODE_OCALL_OUTER) {
log_error("Invalid 'sgx.profile.mode' and 'sgx.profile.with_stack' combination "
"(\"ocall_outer\" mode cannot be used with stack)");
ret = -EINVAL;
goto out;
}
int64_t profile_frequency;
ret = toml_int_in(manifest_root, "sgx.profile.frequency", SGX_PROFILE_DEFAULT_FREQUENCY,
&profile_frequency);
if (ret < 0 || !(0 < profile_frequency && profile_frequency <= SGX_PROFILE_MAX_FREQUENCY)) {
log_error("Cannot parse 'sgx.profile.frequency' (the value must be between 1 and %d)",
SGX_PROFILE_MAX_FREQUENCY);
ret = -EINVAL;
goto out;
}
enclave_info->profile_frequency = profile_frequency;
#else
if (profile_str && strcmp(profile_str, "none")) {
log_error("Invalid 'sgx.profile.enable' "
"(SGX profiling works only when Gramine is compiled with DEBUG=1)");
ret = -EINVAL;
goto out;
}
#endif
int log_level = PAL_LOG_DEFAULT_LEVEL;
ret = toml_string_in(manifest_root, "loader.log_level", &log_level_str);
if (ret < 0) {
log_error("Cannot parse 'loader.log_level'");
ret = -EINVAL;
goto out;
}
if (log_level_str) {
if (!strcmp(log_level_str, "none")) {
log_level = LOG_LEVEL_NONE;
} else if (!strcmp(log_level_str, "error")) {
log_level = LOG_LEVEL_ERROR;
} else if (!strcmp(log_level_str, "warning")) {
log_level = LOG_LEVEL_WARNING;
} else if (!strcmp(log_level_str, "debug")) {
log_level = LOG_LEVEL_DEBUG;
} else if (!strcmp(log_level_str, "trace")) {
log_level = LOG_LEVEL_TRACE;
} else if (!strcmp(log_level_str, "all")) {
log_level = LOG_LEVEL_ALL;
} else {
log_error("Unknown 'loader.log_level'");
ret = -EINVAL;
goto out;
}
}
ret = toml_string_in(manifest_root, "loader.log_file", &log_file);
if (ret < 0) {
log_error("Cannot parse 'loader.log_file'");
ret = -EINVAL;
goto out;
}
if (log_level > LOG_LEVEL_NONE && log_file) {
ret = urts_log_init(log_file);
if (ret < 0) {
log_error("Cannot open log file: %d", ret);
goto out;
}
}
g_urts_log_level = log_level;
ret = 0;
out:
free(dummy_sigfile_str);
free(sgx_ra_client_spid_str);
free(profile_str);
#ifdef DEBUG
free(profile_mode_str);
#endif
free(log_level_str);
free(log_file);
toml_free(manifest_root);
return ret;
}
/* Warning: This function does not free up resources on failure - it assumes that the whole process
* exits after this function's failure. */
static int load_enclave(struct pal_enclave* enclave, char* args, size_t args_size, char* env,
size_t env_size, int parent_stream_fd, bool need_gsgx) {
int ret;
struct timeval tv;
struct pal_topo_info topo_info = {0};
uint64_t start_time;
DO_SYSCALL(gettimeofday, &tv, NULL);
start_time = tv.tv_sec * 1000000UL + tv.tv_usec;
if (parent_stream_fd < 0) {
/* only print during main process's startup (note that this message is always printed) */
log_always("Gramine is starting. Parsing TOML manifest file, this may take some time...");
}
ret = parse_loader_config(enclave->raw_manifest_data, enclave);
if (ret < 0) {
log_error("Parsing manifest failed");
return -EINVAL;
}
log_debug("Gramine parsed TOML manifest file successfully");
ret = open_sgx_driver(need_gsgx);
if (ret < 0)
return ret;
if (!is_wrfsbase_supported())
return -EPERM;
/* Get host topology information only for the first process. This information will be
* checkpointed and restored during forking of the child process(es). */
if (parent_stream_fd < 0) {
ret = get_topology_info(&topo_info);
if (ret < 0)
return ret;
}
enclave->libpal_uri = alloc_concat(URI_PREFIX_FILE, URI_PREFIX_FILE_LEN, g_libpal_path, -1);
if (!enclave->libpal_uri) {
log_error("Out of memory for enclave->libpal_uri");
return -ENOMEM;
}
if (enclave->libpal_uri[URI_PREFIX_FILE_LEN] != '/') {
log_error("Path to in-enclave PAL (%s) must be absolute", enclave->libpal_uri);
return -EINVAL;
}
ret = initialize_enclave(enclave, enclave->raw_manifest_data);
if (ret < 0)
return ret;
ret = sgx_signal_setup();
if (ret < 0)
return ret;
sgx_target_info_t qe_targetinfo = {0};
if (enclave->remote_attestation_enabled) {
/* initialize communication with Quoting Enclave only if app requests attestation */
bool is_epid = enclave->use_epid_attestation;
log_debug("Using SGX %s attestation", is_epid ? "EPID" : "DCAP/ECDSA");
ret = init_quoting_enclave_targetinfo(is_epid, &qe_targetinfo);
if (ret < 0)
return ret;