-
Notifications
You must be signed in to change notification settings - Fork 202
/
enclave_framework.c
1361 lines (1129 loc) · 45.1 KB
/
enclave_framework.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
#include <stdbool.h>
#include "api.h"
#include "crypto.h"
#include "enclave_tf.h"
#include "hex.h"
#include "list.h"
#include "pal_error.h"
#include "pal_internal.h"
#include "pal_linux.h"
#include "pal_linux_error.h"
#include "sgx_arch.h"
#include "spinlock.h"
#include "toml.h"
#include "toml_utils.h"
#define LOCAL_ATTESTATION_TAG_PARENT_STR "GRAMINE_LOCAL_ATTESTATION_TAG_PARENT"
#define LOCAL_ATTESTATION_TAG_CHILD_STR "GRAMINE_LOCAL_ATTESTATION_TAG_CHILD"
static int register_file(const char* uri, const char* hash_str, bool check_duplicates);
uintptr_t g_enclave_base;
uintptr_t g_enclave_top;
bool g_allowed_files_warn = false;
/*
* SGX's EGETKEY(SEAL_KEY) uses three masks as key-derivation material:
* - KEYREQUEST.ATTRIBUTESMASK.FLAGS
* - KEYREQUEST.ATTRIBUTESMASK.XFRM
* - KEYREQUEST.MISCMASK
*
* These default masks may be replaced by user-defined ones (specified in the manifest file).
* Corresponding manifest keys are:
* - `sgx.seal_key.flags_mask`
* - `sgx.seal_key.xfrm_mask`
* - `sgx.seal_key.misc_mask`
*/
static uint64_t g_seal_key_flags_mask = SGX_FLAGS_MASK_CONST;
static uint64_t g_seal_key_xfrm_mask = SGX_XFRM_MASK_CONST;
static uint32_t g_seal_key_misc_mask = SGX_MISCSELECT_MASK_CONST;
static_assert(sizeof(g_seal_key_flags_mask) + sizeof(g_seal_key_xfrm_mask) ==
sizeof(sgx_attributes_t), "wrong types");
static_assert(sizeof(g_seal_key_misc_mask) == sizeof(sgx_misc_select_t), "wrong types");
bool sgx_is_completely_within_enclave(const void* _addr, size_t size) {
uintptr_t addr = (uintptr_t)_addr;
if (addr > UINTPTR_MAX - size) {
return false;
}
return g_enclave_base <= addr && addr + size <= g_enclave_top;
}
bool sgx_is_valid_untrusted_ptr(const void* _addr, size_t size, size_t alignment) {
uintptr_t addr = (uintptr_t)_addr;
if (addr > UINTPTR_MAX - size) {
return false;
}
if (!(addr + size <= g_enclave_base || g_enclave_top <= addr)) {
return false;
}
return IS_ALIGNED(addr, alignment);
}
/*
* When DEBUG is enabled, we run sgx_profile_sample() during asynchronous enclave exit (AEX), which
* uses the stack. Make sure to update URSP so that the AEX handler does not overwrite the part of
* the stack that we just allocated.
*
* (Recall that URSP is an outside stack pointer, saved by EENTER and restored on AEX by the SGX
* hardware itself.)
*/
#ifdef DEBUG
#define UPDATE_USTACK(_ustack) \
do { \
SET_ENCLAVE_TCB(ustack, _ustack); \
GET_ENCLAVE_TCB(gpr)->ursp = (uint64_t)_ustack; \
} while(0)
#else
#define UPDATE_USTACK(_ustack) SET_ENCLAVE_TCB(ustack, _ustack)
#endif
void* sgx_prepare_ustack(void) {
void* old_ustack = GET_ENCLAVE_TCB(ustack);
void* ustack = old_ustack;
if (ustack != GET_ENCLAVE_TCB(ustack_top))
ustack -= RED_ZONE_SIZE;
UPDATE_USTACK(ustack);
return old_ustack;
}
void* sgx_alloc_on_ustack_aligned(size_t size, size_t alignment) {
assert(IS_POWER_OF_2(alignment));
void* ustack = GET_ENCLAVE_TCB(ustack) - size;
ustack = ALIGN_DOWN_PTR_POW2(ustack, alignment);
if (!sgx_is_valid_untrusted_ptr(ustack, size, alignment)) {
return NULL;
}
UPDATE_USTACK(ustack);
return ustack;
}
void* sgx_alloc_on_ustack(size_t size) {
return sgx_alloc_on_ustack_aligned(size, 1);
}
void* sgx_copy_to_ustack(const void* ptr, size_t size) {
if (!sgx_is_completely_within_enclave(ptr, size)) {
return NULL;
}
void* uptr = sgx_alloc_on_ustack(size);
if (uptr) {
memcpy(uptr, ptr, size);
}
return uptr;
}
void sgx_reset_ustack(const void* old_ustack) {
assert(old_ustack <= GET_ENCLAVE_TCB(ustack_top));
UPDATE_USTACK(old_ustack);
}
static void copy_u64s(void* dst, const void* untrusted_src, size_t count) {
assert((uintptr_t)untrusted_src % 8 == 0);
__asm__ volatile (
"rep movsq\n"
: "+D"(dst), "+S"(untrusted_src), "+c"(count)
:
: "memory", "cc"
);
}
void sgx_copy_to_enclave_verified(void* ptr, const void* uptr, size_t size) {
assert(sgx_is_valid_untrusted_ptr(uptr, size, /*alignment=*/1));
assert(sgx_is_completely_within_enclave(ptr, size));
if (size == 0) {
return;
}
/*
* This should be simple `memcpy(ptr, uptr, size)`, but CVE-2022-21233 (INTEL-SA-00657).
* To mitigate this issue, all reads of untrusted memory from within the enclave must be done
* in 8-byte chunks aligned to 8-bytes boundary. Since x64 allocates memory in pages of
* (at least) 0x1000 in size, we can safely 8-align the pointer down and the size up.
*/
size_t copy_len;
size_t prefix_misalignment = (uintptr_t)uptr & 7;
if (prefix_misalignment) {
/* Beginning of the copied range is misaligned. */
char prefix_val[8] = { 0 };
copy_u64s(prefix_val, (char*)uptr - prefix_misalignment, /*count=*/1);
copy_len = MIN(sizeof(prefix_val) - prefix_misalignment, size);
memcpy(ptr, prefix_val + prefix_misalignment, copy_len);
ptr = (char*)ptr + copy_len;
uptr = (const char*)uptr + copy_len;
size -= copy_len;
if (size == 0) {
return;
}
}
assert((uintptr_t)uptr % 8 == 0);
size_t suffix_misalignment = size & 7;
copy_len = size - suffix_misalignment;
assert(copy_len % 8 == 0);
copy_u64s(ptr, uptr, copy_len / 8);
ptr = (char*)ptr + copy_len;
uptr = (const char*)uptr + copy_len;
size -= copy_len;
assert(size == suffix_misalignment);
if (suffix_misalignment) {
/* End of the copied range is misaligned. */
char suffix_val[8] = { 0 };
copy_u64s(suffix_val, uptr, /*count=*/1);
memcpy(ptr, suffix_val, suffix_misalignment);
}
}
bool sgx_copy_to_enclave(void* ptr, size_t maxsize, const void* uptr, size_t usize) {
if (usize > maxsize ||
!sgx_is_valid_untrusted_ptr(uptr, usize, /*alignment=*/1) ||
!sgx_is_completely_within_enclave(ptr, maxsize)) {
return false;
}
sgx_copy_to_enclave_verified(ptr, uptr, usize);
return true;
}
void* sgx_import_array_to_enclave(const void* uptr, size_t elem_size, size_t elem_cnt) {
size_t size;
if (__builtin_mul_overflow(elem_size, elem_cnt, &size))
return NULL;
void* buf = malloc(size);
if (!buf) {
return NULL;
}
if (!sgx_copy_to_enclave(buf, size, uptr, size)) {
free(buf);
return NULL;
}
return buf;
}
void* sgx_import_array2d_to_enclave(const void* uptr, size_t elem_size, size_t elem_cnt1,
size_t elem_cnt2) {
size_t elem_cnt;
if (__builtin_mul_overflow(elem_cnt1, elem_cnt2, &elem_cnt))
return NULL;
return sgx_import_array_to_enclave(uptr, elem_size, elem_cnt);
}
static void print_report(sgx_report_t* r) {
char hex[64 * 2 + 1]; /* large enough to hold any of the below fields */
#define BYTES2HEX(bytes) (bytes2hex(bytes, sizeof(bytes), hex, sizeof(hex)))
log_debug(" cpu_svn: %s", BYTES2HEX(r->body.cpu_svn.svn));
log_debug(" mr_enclave: %s", BYTES2HEX(r->body.mr_enclave.m));
log_debug(" mr_signer: %s", BYTES2HEX(r->body.mr_signer.m));
log_debug(" attr.flags: %016lx", r->body.attributes.flags);
log_debug(" attr.xfrm: %016lx", r->body.attributes.xfrm);
log_debug(" isv_prod_id: %02x", r->body.isv_prod_id);
log_debug(" isv_svn: %02x", r->body.isv_svn);
log_debug(" isv_ext_prod_id: %s", BYTES2HEX(r->body.isv_ext_prod_id));
log_debug(" isv_family_id: %s", BYTES2HEX(r->body.isv_family_id));
log_debug(" report_data: %s", BYTES2HEX(r->body.report_data.d));
log_debug(" key_id: %s", BYTES2HEX(r->key_id.id));
log_debug(" mac: %s", BYTES2HEX(r->mac));
#undef BYTES2HEX
}
int sgx_get_report(const sgx_target_info_t* target_info, const sgx_report_data_t* data,
sgx_report_t* report) {
int ret = sgx_report(target_info, data, report);
if (ret) {
log_error("sgx_report failed: ret = %d", ret);
return -PAL_ERROR_DENIED;
}
return 0;
}
int sgx_verify_report(sgx_report_t* report) {
__sgx_mem_aligned sgx_key_request_t keyrequest;
memset(&keyrequest, 0, sizeof(sgx_key_request_t));
keyrequest.key_name = SGX_REPORT_KEY;
memcpy(&keyrequest.key_id, &report->key_id, sizeof(keyrequest.key_id));
sgx_key_128bit_t report_key __attribute__((aligned(sizeof(sgx_key_128bit_t))));
memset(&report_key, 0, sizeof(report_key));
int ret = sgx_getkey(&keyrequest, &report_key);
if (ret) {
log_error("Can't get report key");
return -PAL_ERROR_DENIED;
}
sgx_mac_t check_mac;
memset(&check_mac, 0, sizeof(check_mac));
// Generating the MAC with AES-CMAC using the report key. Only hash the part of the report
// BEFORE the keyid field (hence the offsetof(...) trick). ENCLU[EREPORT] does not include
// the MAC and the keyid fields when generating the MAC.
lib_AESCMAC((uint8_t*)&report_key, sizeof(report_key),
(uint8_t*)report, offsetof(sgx_report_t, key_id),
(uint8_t*)&check_mac, sizeof(check_mac));
// Clear the report key for security
erase_memory(&report_key, sizeof(report_key));
log_debug("Verify report:");
print_report(report);
if (!ct_memequal(&check_mac, &report->mac, sizeof(check_mac))) {
log_error("Report verification failed");
return -PAL_ERROR_DENIED;
}
return 0;
}
int sgx_get_seal_key(uint16_t key_policy, sgx_key_128bit_t* out_seal_key) {
assert(key_policy == SGX_KEYPOLICY_MRENCLAVE || key_policy == SGX_KEYPOLICY_MRSIGNER);
/* The keyrequest struct dictates the key derivation material used to generate the sealing key.
* It includes MRENCLAVE/MRSIGNER key policy (to allow secret migration/sealing between
* instances of the same enclave or between different enclaves of the same author/signer),
* CPU/ISV/CONFIG SVNs (to prevent secret migration to older vulnerable versions of the
* enclave), ATTRIBUTES and MISCSELECT masks (to prevent secret migration from e.g. production
* enclave to debug enclave). Note that KEYID is zero, to generate the same sealing key in
* different instances of the same enclave/same signer. */
__sgx_mem_aligned sgx_key_request_t key_request = {0};
key_request.key_name = SGX_SEAL_KEY;
key_request.key_policy = key_policy;
memcpy(&key_request.cpu_svn, &g_pal_linuxsgx_state.enclave_info.cpu_svn, sizeof(sgx_cpu_svn_t));
memcpy(&key_request.isv_svn, &g_pal_linuxsgx_state.enclave_info.isv_svn, sizeof(sgx_isv_svn_t));
memcpy(&key_request.config_svn, &g_pal_linuxsgx_state.enclave_info.config_svn,
sizeof(sgx_config_svn_t));
key_request.attribute_mask.flags = g_seal_key_flags_mask;
key_request.attribute_mask.xfrm = g_seal_key_xfrm_mask;
key_request.misc_mask = g_seal_key_misc_mask;
int ret = sgx_getkey(&key_request, out_seal_key);
if (ret) {
log_error("Failed to generate sealing key using SGX EGETKEY\n");
return -PAL_ERROR_DENIED;
}
return 0;
}
DEFINE_LISTP(trusted_file);
static LISTP_TYPE(trusted_file) g_trusted_file_list = LISTP_INIT;
static spinlock_t g_trusted_file_lock = INIT_SPINLOCK_UNLOCKED;
static int g_file_check_policy = FILE_CHECK_POLICY_STRICT;
/* assumes `path` is normalized */
static bool path_is_equal_or_subpath(const struct trusted_file* tf, const char* path,
size_t path_len) {
const char* tf_path = tf->uri + URI_PREFIX_FILE_LEN;
size_t tf_path_len = tf->uri_len - URI_PREFIX_FILE_LEN;
if (tf_path_len > path_len || memcmp(tf_path, path, tf_path_len)) {
/* tf path is not a prefix of `path` */
return false;
}
if (tf_path_len == path_len) {
/* Both are equal */
return true;
}
if (tf_path[tf_path_len - 1] == '/') {
/* tf path is a subpath of `path` (with slash), e.g. "foo/" and "foo/bar" */
return true;
}
if (path[tf_path_len] == '/') {
/* tf path is a subpath of `path` (without slash), e.g. "foo" and "foo/bar" */
return true;
}
return false;
}
struct trusted_file* get_trusted_or_allowed_file(const char* path) {
struct trusted_file* tf = NULL;
size_t path_len = strlen(path);
spinlock_lock(&g_trusted_file_lock);
struct trusted_file* tmp;
LISTP_FOR_EACH_ENTRY(tmp, &g_trusted_file_list, list) {
if (tmp->allowed) {
/* allowed files: must be a subfolder or file */
if (path_is_equal_or_subpath(tmp, path, path_len)) {
tf = tmp;
break;
}
} else {
/* trusted files: must be exactly the same URI */
const char* tf_path = tmp->uri + URI_PREFIX_FILE_LEN;
size_t tf_path_len = tmp->uri_len - URI_PREFIX_FILE_LEN;
if (tf_path_len == path_len && !memcmp(tf_path, path, path_len + 1)) {
tf = tmp;
break;
}
}
}
spinlock_unlock(&g_trusted_file_lock);
return tf;
}
int load_trusted_or_allowed_file(struct trusted_file* tf, PAL_HANDLE file, bool create,
sgx_chunk_hash_t** out_chunk_hashes, uint64_t* out_size,
void** out_umem) {
int ret;
*out_chunk_hashes = NULL;
*out_size = 0;
*out_umem = NULL;
if (create) {
assert(tf->allowed);
return register_file(tf->uri, /*hash_str=*/NULL, /*check_duplicates=*/true);
}
if (tf->allowed) {
/* allowed files: do not need any integrity, so no need for chunk hashes */
return 0;
}
/* trusted files: need integrity, so calculate chunk hashes and compare with hash in manifest */
if (!file->file.seekable) {
log_warning("Trusted file '%s' is not seekable, cannot load it", file->file.realpath);
return -PAL_ERROR_DENIED;
}
sgx_chunk_hash_t* chunk_hashes = NULL;
uint8_t* tmp_chunk = NULL; /* scratch buf to calculate whole-file and chunk-of-file hashes */
/* mmap the whole trusted file in untrusted memory for future reads/writes; it is
* caller's responsibility to unmap those areas after use */
*out_size = tf->size;
if (*out_size) {
ret = ocall_mmap_untrusted(out_umem, tf->size, PROT_READ, MAP_SHARED, file->file.fd,
/*offset=*/0);
if (ret < 0) {
*out_umem = NULL;
ret = unix_to_pal_error(ret);
goto fail;
}
}
spinlock_lock(&g_trusted_file_lock);
if (tf->chunk_hashes) {
*out_chunk_hashes = tf->chunk_hashes;
spinlock_unlock(&g_trusted_file_lock);
return 0;
}
spinlock_unlock(&g_trusted_file_lock);
chunk_hashes = malloc(sizeof(sgx_chunk_hash_t) * UDIV_ROUND_UP(tf->size, TRUSTED_CHUNK_SIZE));
if (!chunk_hashes) {
ret = -PAL_ERROR_NOMEM;
goto fail;
}
tmp_chunk = malloc(TRUSTED_CHUNK_SIZE);
if (!tmp_chunk) {
ret = -PAL_ERROR_NOMEM;
goto fail;
}
sgx_chunk_hash_t* chunk_hashes_item = chunk_hashes;
uint64_t offset = 0;
LIB_SHA256_CONTEXT file_sha;
ret = lib_SHA256Init(&file_sha);
if (ret < 0)
goto fail;
for (; offset < tf->size; offset += TRUSTED_CHUNK_SIZE, chunk_hashes_item++) {
/* For each file chunk of size TRUSTED_CHUNK_SIZE, generate 128-bit hash from SHA-256 hash
* over contents of this file chunk (we simply truncate SHA-256 hash to first 128 bits; this
* is fine for integrity purposes). Also, generate a SHA-256 hash for the whole file
* contents to compare with the manifest "reference" hash value. */
uint64_t chunk_size = MIN(tf->size - offset, TRUSTED_CHUNK_SIZE);
LIB_SHA256_CONTEXT chunk_sha;
ret = lib_SHA256Init(&chunk_sha);
if (ret < 0)
goto fail;
/* to prevent TOCTOU attacks, copy file contents into the enclave before hashing */
if (!sgx_copy_to_enclave(tmp_chunk, TRUSTED_CHUNK_SIZE, *out_umem + offset, chunk_size))
goto fail;
ret = lib_SHA256Update(&file_sha, tmp_chunk, chunk_size);
if (ret < 0)
goto fail;
ret = lib_SHA256Update(&chunk_sha, tmp_chunk, chunk_size);
if (ret < 0)
goto fail;
sgx_chunk_hash_t chunk_hash[2]; /* each chunk_hash is 128 bits in size */
static_assert(sizeof(chunk_hash) * 8 == 256, "");
ret = lib_SHA256Final(&chunk_sha, (uint8_t*)&chunk_hash[0]);
if (ret < 0)
goto fail;
/* note that we truncate SHA256 to 128 bits */
memcpy(chunk_hashes_item, &chunk_hash[0], sizeof(*chunk_hashes_item));
}
sgx_file_hash_t file_hash;
ret = lib_SHA256Final(&file_sha, file_hash.bytes);
if (ret < 0)
goto fail;
/* check the generated hash-over-whole-file against the reference hash in the manifest */
if (memcmp(&file_hash, &tf->file_hash, sizeof(file_hash))) {
log_warning("Hash of trusted file '%s' does not match with the reference hash in manifest",
file->file.realpath);
ret = -PAL_ERROR_DENIED;
goto fail;
}
spinlock_lock(&g_trusted_file_lock);
if (tf->chunk_hashes) {
*out_chunk_hashes = tf->chunk_hashes;
spinlock_unlock(&g_trusted_file_lock);
free(chunk_hashes);
free(tmp_chunk);
return 0;
}
tf->chunk_hashes = chunk_hashes;
*out_chunk_hashes = chunk_hashes;
spinlock_unlock(&g_trusted_file_lock);
free(tmp_chunk);
return 0;
fail:
if (*out_umem) {
assert(*out_size > 0);
ocall_munmap_untrusted(*out_umem, *out_size);
}
free(chunk_hashes);
free(tmp_chunk);
return ret;
}
int get_file_check_policy(void) {
return g_file_check_policy;
}
static void set_file_check_policy(int policy) {
g_file_check_policy = policy;
}
int copy_and_verify_trusted_file(const char* path, uint8_t* buf, const void* umem,
off_t aligned_offset, off_t aligned_end, off_t offset, off_t end,
sgx_chunk_hash_t* chunk_hashes, size_t file_size) {
int ret = 0;
assert(IS_ALIGNED(aligned_offset, TRUSTED_CHUNK_SIZE));
assert(offset >= aligned_offset && end <= aligned_end);
uint8_t* tmp_chunk = malloc(TRUSTED_CHUNK_SIZE);
if (!tmp_chunk) {
ret = -PAL_ERROR_NOMEM;
goto failed;
}
sgx_chunk_hash_t* chunk_hashes_item = chunk_hashes + aligned_offset / TRUSTED_CHUNK_SIZE;
uint8_t* buf_pos = buf;
off_t chunk_offset = aligned_offset;
for (; chunk_offset < aligned_end; chunk_offset += TRUSTED_CHUNK_SIZE, chunk_hashes_item++) {
size_t chunk_size = MIN(file_size - chunk_offset, TRUSTED_CHUNK_SIZE);
off_t chunk_end = chunk_offset + chunk_size;
sgx_chunk_hash_t chunk_hash[2]; /* each chunk_hash is 128 bits in size but we need 256 */
LIB_SHA256_CONTEXT chunk_sha;
ret = lib_SHA256Init(&chunk_sha);
if (ret < 0)
goto failed;
if (chunk_offset >= offset && chunk_end <= end) {
/* if current chunk-to-copy completely resides in the requested region-to-copy,
* directly copy into buf (without a scratch buffer) and hash in-place */
if (!sgx_copy_to_enclave(buf_pos, chunk_size, umem + chunk_offset, chunk_size)) {
goto failed;
}
ret = lib_SHA256Update(&chunk_sha, buf_pos, chunk_size);
if (ret < 0)
goto failed;
buf_pos += chunk_size;
} else {
/* if current chunk-to-copy only partially overlaps with the requested region-to-copy,
* read the file contents into a scratch buffer, verify hash and then copy only the part
* needed by the caller */
if (!sgx_copy_to_enclave(tmp_chunk, chunk_size, umem + chunk_offset, chunk_size)) {
goto failed;
}
ret = lib_SHA256Update(&chunk_sha, tmp_chunk, chunk_size);
if (ret < 0)
goto failed;
/* determine which part of the chunk is needed by the caller */
off_t copy_start = MAX(chunk_offset, offset);
off_t copy_end = MIN(chunk_offset + (off_t)chunk_size, end);
assert(copy_end > copy_start);
memcpy(buf_pos, tmp_chunk + copy_start - chunk_offset, copy_end - copy_start);
buf_pos += copy_end - copy_start;
}
ret = lib_SHA256Final(&chunk_sha, (uint8_t*)&chunk_hash[0]);
if (ret < 0)
goto failed;
if (memcmp(chunk_hashes_item, &chunk_hash[0], sizeof(*chunk_hashes_item))) {
log_error("Accessing file '%s' is denied: incorrect hash of file chunk at %lu-%lu.",
path, chunk_offset, chunk_end);
ret = -PAL_ERROR_DENIED;
goto failed;
}
}
free(tmp_chunk);
return 0;
failed:
free(tmp_chunk);
memset(buf, 0, end - offset);
return ret;
}
static int register_file(const char* uri, const char* hash_str, bool check_duplicates) {
if (hash_str && strlen(hash_str) != sizeof(sgx_file_hash_t) * 2) {
log_error("Hash (%s) of a trusted file %s is not a SHA256 hash", hash_str, uri);
return -PAL_ERROR_INVAL;
}
size_t uri_len = strlen(uri);
if (uri_len >= URI_MAX) {
log_error("Size of file exceeds maximum %dB: %s", URI_MAX, uri);
return -PAL_ERROR_INVAL;
}
if (check_duplicates) {
/* this check is only done during runtime (when creating a new file) and not needed during
* initialization (because manifest is assumed to have no duplicates); skipping this check
* significantly improves startup time */
spinlock_lock(&g_trusted_file_lock);
struct trusted_file* tf;
LISTP_FOR_EACH_ENTRY(tf, &g_trusted_file_list, list) {
if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
spinlock_unlock(&g_trusted_file_lock);
return 0;
}
}
spinlock_unlock(&g_trusted_file_lock);
}
struct trusted_file* new = malloc(sizeof(*new) + uri_len + 1);
if (!new)
return -PAL_ERROR_NOMEM;
INIT_LIST_HEAD(new, list);
new->size = 0;
new->chunk_hashes = NULL;
new->allowed = false;
new->uri_len = uri_len;
memcpy(new->uri, uri, uri_len + 1);
if (hash_str) {
assert(strlen(hash_str) == sizeof(sgx_file_hash_t) * 2);
char* bytes = hex2bytes(hash_str, strlen(hash_str), new->file_hash.bytes,
sizeof(new->file_hash.bytes));
if (!bytes) {
log_error("Could not parse hash of file: %s", uri);
free(new);
return -PAL_ERROR_INVAL;
}
} else {
memset(&new->file_hash, 0, sizeof(new->file_hash));
new->allowed = true;
}
spinlock_lock(&g_trusted_file_lock);
if (check_duplicates) {
/* this check is only done during runtime and not needed during initialization (see above);
* we check again because same file could have been added by another thread in meantime */
struct trusted_file* tf;
LISTP_FOR_EACH_ENTRY(tf, &g_trusted_file_list, list) {
if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
spinlock_unlock(&g_trusted_file_lock);
free(new);
return 0;
}
}
}
LISTP_ADD_TAIL(new, &g_trusted_file_list, list);
spinlock_unlock(&g_trusted_file_lock);
return 0;
}
static int normalize_and_register_file(const char* uri, const char* hash_str) {
int ret;
if (!strstartswith(uri, URI_PREFIX_FILE)) {
log_error("Invalid URI [%s]: Trusted/allowed files must start with 'file:'", uri);
return -PAL_ERROR_INVAL;
}
const size_t norm_uri_size = strlen(uri) + 1;
char* norm_uri = malloc(norm_uri_size);
if (!norm_uri) {
return -PAL_ERROR_NOMEM;
}
memcpy(norm_uri, URI_PREFIX_FILE, URI_PREFIX_FILE_LEN);
size_t norm_path_size = norm_uri_size - URI_PREFIX_FILE_LEN;
ret = get_norm_path(uri + URI_PREFIX_FILE_LEN, norm_uri + URI_PREFIX_FILE_LEN, &norm_path_size);
if (ret < 0) {
log_error("Path (%s) normalization failed: %s", uri, pal_strerror(ret));
goto out;
}
ret = register_file(norm_uri, hash_str, /*check_duplicates=*/false);
out:
free(norm_uri);
return ret;
}
int init_trusted_files(void) {
int ret;
toml_table_t* manifest_sgx = toml_table_in(g_pal_public_state.manifest_root, "sgx");
if (!manifest_sgx)
return 0;
toml_array_t* toml_trusted_files = toml_array_in(manifest_sgx, "trusted_files");
if (!toml_trusted_files)
return 0;
ssize_t toml_trusted_files_cnt = toml_array_nelem(toml_trusted_files);
if (toml_trusted_files_cnt < 0)
return -PAL_ERROR_DENIED;
if (toml_trusted_files_cnt == 0)
return 0;
char* toml_trusted_uri_str = NULL;
char* toml_trusted_sha256_str = NULL;
for (ssize_t i = 0; i < toml_trusted_files_cnt; i++) {
/* read `sgx.trusted_file = {uri = "file:foo", sha256 = "deadbeef"}` entry from manifest */
toml_table_t* toml_trusted_file = toml_table_at(toml_trusted_files, i);
if (!toml_trusted_file) {
log_error("Invalid trusted file in manifest at index %ld (not a TOML table)", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
toml_raw_t toml_trusted_uri_raw = toml_raw_in(toml_trusted_file, "uri");
if (!toml_trusted_uri_raw) {
log_error("Invalid trusted file in manifest at index %ld (no 'uri' key)", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
ret = toml_rtos(toml_trusted_uri_raw, &toml_trusted_uri_str);
if (ret < 0) {
log_error("Invalid trusted file in manifest at index %ld ('uri' is not a string)", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
toml_raw_t toml_trusted_sha256_raw = toml_raw_in(toml_trusted_file, "sha256");
if (!toml_trusted_sha256_raw) {
log_error("Invalid trusted file in manifest at index %ld (no 'sha256' key)", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
ret = toml_rtos(toml_trusted_sha256_raw, &toml_trusted_sha256_str);
if (ret < 0 || !toml_trusted_sha256_str) {
log_error("Invalid trusted file in manifest at index %ld ('sha256' is not a string)",
i);
ret = -PAL_ERROR_INVAL;
goto out;
}
ret = normalize_and_register_file(toml_trusted_uri_str, toml_trusted_sha256_str);
if (ret < 0) {
log_error("normalize_and_register_file(\"%s\", \"%s\") failed with error code %d",
toml_trusted_uri_str, toml_trusted_sha256_str, ret);
goto out;
}
free(toml_trusted_uri_str);
free(toml_trusted_sha256_str);
toml_trusted_uri_str = NULL;
toml_trusted_sha256_str = NULL;
}
ret = 0;
out:
free(toml_trusted_uri_str);
free(toml_trusted_sha256_str);
return ret;
}
static void maybe_warn_about_allowed_files_usage(void) {
if (!g_pal_common_state.parent_process)
g_allowed_files_warn = true;
}
int init_allowed_files(void) {
int ret;
toml_table_t* manifest_sgx = toml_table_in(g_pal_public_state.manifest_root, "sgx");
if (!manifest_sgx)
return 0;
toml_array_t* toml_allowed_files = toml_array_in(manifest_sgx, "allowed_files");
if (!toml_allowed_files)
return 0;
maybe_warn_about_allowed_files_usage();
ssize_t toml_allowed_files_cnt = toml_array_nelem(toml_allowed_files);
if (toml_allowed_files_cnt < 0)
return -PAL_ERROR_DENIED;
if (toml_allowed_files_cnt == 0)
return 0;
char* toml_allowed_file_str = NULL;
for (ssize_t i = 0; i < toml_allowed_files_cnt; i++) {
toml_raw_t toml_allowed_file_raw = toml_raw_at(toml_allowed_files, i);
if (!toml_allowed_file_raw) {
log_error("Invalid allowed file in manifest at index %ld", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
ret = toml_rtos(toml_allowed_file_raw, &toml_allowed_file_str);
if (ret < 0) {
log_error("Invalid allowed file in manifest at index %ld (not a string)", i);
ret = -PAL_ERROR_INVAL;
goto out;
}
ret = normalize_and_register_file(toml_allowed_file_str, /*hash_str=*/NULL);
if (ret < 0) {
log_error("normalize_and_register_file(\"%s\", NULL) failed with error code %d",
toml_allowed_file_str, ret);
goto out;
}
free(toml_allowed_file_str);
toml_allowed_file_str = NULL;
}
ret = 0;
out:
free(toml_allowed_file_str);
return ret;
}
int init_file_check_policy(void) {
int ret;
char* file_check_policy_str = NULL;
ret = toml_string_in(g_pal_public_state.manifest_root, "sgx.file_check_policy",
&file_check_policy_str);
if (ret < 0) {
log_error("Cannot parse 'sgx.file_check_policy'");
return -PAL_ERROR_INVAL;
}
if (!file_check_policy_str)
return 0;
if (!strcmp(file_check_policy_str, "strict")) {
set_file_check_policy(FILE_CHECK_POLICY_STRICT);
} else if (!strcmp(file_check_policy_str, "allow_all_but_log")) {
set_file_check_policy(FILE_CHECK_POLICY_ALLOW_ALL_BUT_LOG);
} else {
log_error("Unknown value for 'sgx.file_check_policy' "
"(allowed: `strict`, `allow_all_but_log`)'");
free(file_check_policy_str);
return -PAL_ERROR_INVAL;
}
log_debug("File check policy: %s", file_check_policy_str);
free(file_check_policy_str);
return 0;
}
static int update_seal_key_mask(const char* mask_name, uint8_t* mask_ptr, size_t mask_size) {
int ret;
char* mask_str = NULL;
ret = toml_string_in(g_pal_public_state.manifest_root, mask_name, &mask_str);
if (ret < 0) {
log_error("Cannot parse '%s'", mask_name);
return -PAL_ERROR_INVAL;
}
if (!mask_str) {
/* no mask specified in the manifest, use the default */
return 0;
}
if (strlen(mask_str) != 2 + mask_size * 2) {
log_error("Malformed '%s' value in the manifest (wrong size)", mask_name);
ret = -PAL_ERROR_INVAL;
goto out;
}
if (mask_str[0] != '0' || (mask_str[1] != 'x' && mask_str[1] != 'X')) {
log_error("Malformed '%s' value in the manifest (must start with '0x')", mask_name);
ret = -PAL_ERROR_INVAL;
goto out;
}
memset(mask_ptr, 0, mask_size);
for (size_t i = 0; i < mask_size * 2; i++) {
int8_t val = hex2dec(mask_str[i + 2]); /* skip first two chars (the '0x' prefix) */
if (val < 0) {
log_error("Malformed '%s' value in the manifest (not a hex number)", mask_name);
ret = -PAL_ERROR_INVAL;
goto out;
}
uint8_t* mask_byte = &mask_ptr[mask_size - i / 2 - 1];
*mask_byte = *mask_byte * 16 + (uint8_t)val;
}
ret = 0;
out:
free(mask_str);
return ret;
}
int init_seal_key_material(void) {
int ret;
/* below parsing of TOML strings assumes little-endianness (which is true for this SGX PAL) */
ret = update_seal_key_mask("sgx.seal_key.flags_mask", (uint8_t*)&g_seal_key_flags_mask,
sizeof(g_seal_key_flags_mask));
if (ret < 0)
return ret;
ret = update_seal_key_mask("sgx.seal_key.xfrm_mask", (uint8_t*)&g_seal_key_xfrm_mask,
sizeof(g_seal_key_xfrm_mask));
if (ret < 0)
return ret;
return update_seal_key_mask("sgx.seal_key.misc_mask", (uint8_t*)&g_seal_key_misc_mask,
sizeof(g_seal_key_misc_mask));
}
int init_enclave(void) {
/* initialize enclave information (MRENCLAVE, etc.) of current enclave (via EREPORT) */
__sgx_mem_aligned sgx_target_info_t targetinfo = {0};
__sgx_mem_aligned sgx_report_data_t reportdata = {0};
__sgx_mem_aligned sgx_report_t report;
int ret = sgx_report(&targetinfo, &reportdata, &report);
if (ret) {
log_error("Failed to get SGX report for current enclave");
return -PAL_ERROR_INVAL;
}
memcpy(&g_pal_linuxsgx_state.enclave_info, &report.body,
sizeof(g_pal_linuxsgx_state.enclave_info));
return 0;
}
static int hash_over_session_key(const PAL_SESSION_KEY* session_key, const char* tag,
size_t tag_size, sgx_report_data_t* out_hash) {
int ret;
LIB_SHA256_CONTEXT sha;
/* SGX report data is 64B in size, but SHA256 hash is only 32B in size, so pad with zeros */
memset(out_hash, 0, sizeof(*out_hash));
ret = lib_SHA256Init(&sha);
if (ret < 0)
return ret;
ret = lib_SHA256Update(&sha, (uint8_t*)session_key, sizeof(*session_key));
if (ret < 0)
return ret;
ret = lib_SHA256Update(&sha, (uint8_t*)tag, tag_size);
if (ret < 0)
return ret;
return lib_SHA256Final(&sha, (uint8_t*)out_hash);
}
int _PalStreamKeyExchange(PAL_HANDLE stream, PAL_SESSION_KEY* out_key,
sgx_report_data_t* out_parent_report_data,
sgx_report_data_t* out_child_report_data) {
int ret;
LIB_DH_CONTEXT context;
uint8_t my_public[DH_SIZE];
uint8_t peer_public[DH_SIZE];