-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathmanage_metadata.cpp
1890 lines (1667 loc) · 67.1 KB
/
manage_metadata.cpp
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
/*
* Copyright (C) 2011-2021 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* File:
* manage_metadata.cpp
* Description:
* Parse the xml file to get the metadata and generate the output DLL
* with metadata.
*/
#include "metadata.h"
#include "tinyxml2.h"
#include "manage_metadata.h"
#include "se_trace.h"
#include "util_st.h"
#include "section.h"
#include "se_page_attr.h"
#include "elf_util.h"
#include "crypto_wrapper.h"
#include "global_data.h"
#include "se_version.h"
#include "ema_imp.h"
#include "bit_array_imp.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED;
using tinyxml2::XML_ERROR_FILE_NOT_FOUND;
using tinyxml2::XML_SUCCESS;
using tinyxml2::XMLElement;
using tinyxml2::XMLError;
#define ALIGN_SIZE 0x1000
static bool traverser_parameter(const char *temp_name, const char *temp_text, xml_parameter_t *parameter, int parameter_count)
{
assert(temp_name != NULL && parameter != NULL);
uint64_t temp_value=0;
if(temp_text == NULL)
{
se_trace(SE_TRACE_ERROR, LACK_VALUE_FOR_ELEMENT_ERROR, temp_name);
return false;
}
else
{
if(strchr(temp_text, '-'))
{
se_trace(SE_TRACE_ERROR, INVALID_VALUE_FOR_ELEMENT_ERROR, temp_name);
return false;
}
errno = 0;
char* endptr = NULL;
temp_value = (uint64_t)strtoull(temp_text, &endptr, 0);
if(*endptr!='\0'||errno!=0) //Invalid value or valid value but out of the representable range
{
se_trace(SE_TRACE_ERROR, INVALID_VALUE_FOR_ELEMENT_ERROR, temp_name);
return false;
}
}
//Look for the matched one
int i=0;
for(; i<parameter_count&&STRCMP(temp_name,parameter[i].name); i++);
if(i>=parameter_count) //no matched, return false
{
se_trace(SE_TRACE_ERROR, UNREC_ELEMENT_ERROR, temp_name);
return false;
}
//found one matched
if(parameter[i].flag==1) //repeated definition of XML element, return false
{
se_trace(SE_TRACE_ERROR, REPEATED_DEFINE_ERROR, temp_name);
return false;
}
parameter[i].flag = 1;
if((temp_value<parameter[i].min_value)||
(temp_value>parameter[i].max_value)) // the value is invalid, return false
{
se_trace(SE_TRACE_ERROR, VALUE_OUT_OF_RANGE_ERROR, temp_name);
return false;
}
parameter[i].value = temp_value;
SE_TRACE_DEBUG("%s = %s\n", temp_name, temp_text);
return true;
}
bool parse_metadata_file(const char *xmlpath, xml_parameter_t *parameter, int parameter_count)
{
const char* temp_name=NULL;
assert(parameter != NULL);
if(xmlpath == NULL) // user didn't define the metadata xml file.
{
se_trace(SE_TRACE_NOTICE, "Use default metadata...\n");
return true;
}
//use the metadata file that user gives us. parse xml file
tinyxml2::XMLDocument doc;
XMLError loadOkay = doc.LoadFile(xmlpath);
if(loadOkay != XML_SUCCESS)
{
if(doc.ErrorID() == XML_ERROR_FILE_COULD_NOT_BE_OPENED)
{
se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, xmlpath);
}
else if(doc.ErrorID() == XML_ERROR_FILE_NOT_FOUND)
{
se_trace(SE_TRACE_ERROR, XML_NOT_FOUND_ERROR, xmlpath);
}
else
{
se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
}
return false;
}
doc.Print();//Write the document to standard out using formatted printing ("pretty print").
XMLElement *pmetadata_element = doc.FirstChildElement("EnclaveConfiguration");
if(!pmetadata_element || pmetadata_element->GetText() != NULL)
{
se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
return false;
}
XMLElement *sub_element = NULL;
sub_element = pmetadata_element->FirstChildElement();
const char *temp_text = NULL;
while(sub_element)//parse xml node
{
if(sub_element->FirstAttribute() != NULL)
{
se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
return false;
}
temp_name = sub_element->Value();
temp_text = sub_element->GetText();
//traverse every node. Compare with the default value.
if(traverser_parameter(temp_name, temp_text, parameter, parameter_count) == false)
{
se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
return false;
}
sub_element= sub_element->NextSiblingElement();
}
return true;
}
CMetadata::CMetadata(metadata_t *metadata, BinParser *parser, bool fips_on)
: m_meta_verions(0), m_metadata(metadata), m_parser(parser)
, m_rva(0), m_gd_size(0), m_gd_template(NULL)
{
memset(m_metadata, 0, sizeof(metadata_t));
memset(&m_create_param, 0, sizeof(m_create_param));
m_create_param.fips_on = fips_on ? 1 : 0;
memset(&m_elrange_config_entry, 0, sizeof(m_elrange_config_entry));
}
CMetadata::~CMetadata()
{
}
bool CMetadata::build_metadata(const xml_parameter_t *parameter)
{
if(!modify_metadata(parameter))
{
return false;
}
//elrange config entry
if(!build_elrange_config_entry())
{
return false;
}
// extend table
if(!build_extend_table())
{
return false;
}
// layout table
if(!build_layout_table())
{
return false;
}
// patch table
if(!build_patch_table())
{
return false;
}
if(!build_layout_entries())
{
return false;
}
//vaildate the elrange config here
//at this time, enclave size should be calculated out
if(vaildate_elrange_config() == false)
{
return false;
}
if(!build_gd_template(m_gd_template, &m_gd_size))
{
return false;
}
return true;
}
#include <sstream>
#include <time.h>
bool CMetadata::get_time(uint32_t *date)
{
assert(date != NULL);
time_t rawtime = 0;
if(time( &rawtime) == -1)
return false;
struct tm *timeinfo = gmtime(&rawtime);
if(timeinfo == NULL)
return false;
uint32_t tmp_date = (timeinfo->tm_year+1900)*10000 + (timeinfo->tm_mon+1)*100 + timeinfo->tm_mday;
std::stringstream ss;
ss<<"0x"<<tmp_date;
ss>>std::hex>>tmp_date;
*date = tmp_date;
return true;
}
bool CMetadata::fill_enclave_css(const xml_parameter_t *para)
{
assert(para != NULL);
uint32_t date = 0;
if(false == get_time(&date))
return false;
//*****fill the header*******************//
uint8_t header[12] = {6, 0, 0, 0, 0xE1, 0, 0, 0, 0, 0, 1, 0};
uint8_t header2[16] = {1, 1, 0, 0, 0x60, 0, 0, 0, 0x60, 0, 0, 0, 1, 0, 0, 0};
memcpy_s(&m_metadata->enclave_css.header.header, sizeof(m_metadata->enclave_css.header.header), &header, sizeof(header));
memcpy_s(&m_metadata->enclave_css.header.header2, sizeof(m_metadata->enclave_css.header.header2), &header2, sizeof(header2));
// For 'type', signing tool clears the bit 31 for product enclaves
// and set the bit 31 for debug enclaves
m_metadata->enclave_css.header.type = (para[RELEASETYPE].value & 0x01) ? (1<<31) : 0;
m_metadata->enclave_css.header.module_vendor = (para[INTELSIGNED].value&0x01) ? 0x8086 : 0;
m_metadata->enclave_css.header.date = date;
//hardware version
m_metadata->enclave_css.header.hw_version = (uint32_t)para[HW].value;
// Misc_select/Misc_mask
m_metadata->enclave_css.body.misc_select = (uint32_t)para[MISCSELECT].value;
m_metadata->enclave_css.body.misc_mask = (uint32_t)para[MISCMASK].value;
//low 64 bit
m_metadata->enclave_css.body.attributes.flags = 0;
m_metadata->enclave_css.body.attribute_mask.flags = ~(SGX_FLAGS_DEBUG | SGX_FLAGS_NON_CHECK_BITS);
if(para[DISABLEDEBUG].value == 1)
{
m_metadata->enclave_css.body.attributes.flags &= ~SGX_FLAGS_DEBUG;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_DEBUG;
}
if(para[PROVISIONKEY].value == 1)
{
m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_PROVISION_KEY;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_PROVISION_KEY;
}
if(para[LAUNCHKEY].value == 1)
{
m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_EINITTOKEN_KEY;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_EINITTOKEN_KEY;
}
if (para[ENABLEKSS].value == 1)
{
m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_KSS;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_KSS;
}
if (para[ENABLEAEXNOTIFY].value == 1)
{
m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_AEX_NOTIFY;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_AEX_NOTIFY;
}
if (memcpy_s(&(m_metadata->enclave_css.body.isvext_prod_id), SGX_ISVEXT_PROD_ID_SIZE,
&(para[ISVEXTPRODID_L].value), sizeof(para[ISVEXTPRODID_L].value)))
{
return false;
}
if (memcpy_s((uint8_t *)&(m_metadata->enclave_css.body.isvext_prod_id) + sizeof(para[ISVEXTPRODID_L].value),
SGX_ISVEXT_PROD_ID_SIZE - sizeof(para[ISVEXTPRODID_L].value), &(para[ISVEXTPRODID_H].value), sizeof(para[ISVEXTPRODID_H].value)))
{
return false;
}
if (memcpy_s(&(m_metadata->enclave_css.body.isv_family_id), SGX_ISV_FAMILY_ID_SIZE,
&(para[ISVFAMILYID_L].value), sizeof(para[ISVFAMILYID_L].value)))
{
return false;
}
if (memcpy_s((uint8_t *)&(m_metadata->enclave_css.body.isv_family_id) + sizeof(para[ISVFAMILYID_L].value),
SGX_ISV_FAMILY_ID_SIZE - sizeof(para[ISVFAMILYID_L].value), &(para[ISVFAMILYID_H].value), sizeof(para[ISVFAMILYID_H].value)))
{
return false;
}
bin_fmt_t bf = m_parser->get_bin_format();
if(bf == BF_PE64 || bf == BF_ELF64)
{
m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_MODE64BIT;
m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_MODE64BIT;
}
// high 64 bit
//default setting
m_metadata->enclave_css.body.attributes.xfrm = SGX_XFRM_LEGACY;
m_metadata->enclave_css.body.attribute_mask.xfrm = SGX_XFRM_LEGACY | SGX_XFRM_RESERVED; // LEGACY and reservied bits would be checked.
switch(para[PKRU].value)
{
case FEATURE_MUST_BE_DISABLED:
// PKRU must be disabled
m_metadata->enclave_css.body.attributes.xfrm &= ~SGX_XFRM_PKRU;
m_metadata->enclave_css.body.attribute_mask.xfrm |= SGX_XFRM_PKRU;
break;
case FEATURE_MUST_BE_ENABLED:
// PKRU must be enabled
m_metadata->enclave_css.body.attributes.xfrm |= SGX_XFRM_PKRU;
m_metadata->enclave_css.body.attribute_mask.xfrm |= SGX_XFRM_PKRU;
break;
case FEATURE_LOADER_SELECTS:
default:
m_metadata->enclave_css.body.attributes.xfrm &= ~SGX_XFRM_PKRU;
m_metadata->enclave_css.body.attribute_mask.xfrm &= ~SGX_XFRM_PKRU;
break;
}
switch(para[AMX].value)
{
case FEATURE_MUST_BE_ENABLED:
// AMX must be enabled
m_metadata->enclave_css.body.attributes.xfrm |= SGX_XFRM_AMX;
m_metadata->enclave_css.body.attribute_mask.xfrm |= SGX_XFRM_AMX;
break;
case FEATURE_LOADER_SELECTS:
// Loader will enable this feature if it is avaiable on HW
m_metadata->enclave_css.body.attributes.xfrm &= ~SGX_XFRM_AMX;
m_metadata->enclave_css.body.attribute_mask.xfrm &= ~SGX_XFRM_AMX;
break;
case FEATURE_MUST_BE_DISABLED:
default:
// AMX must be disabled
m_metadata->enclave_css.body.attributes.xfrm &= ~SGX_XFRM_AMX;
m_metadata->enclave_css.body.attribute_mask.xfrm |= SGX_XFRM_AMX;
break;
}
m_metadata->enclave_css.body.isv_prod_id = (uint16_t)para[PRODID].value;
m_metadata->enclave_css.body.isv_svn = (uint16_t)para[ISVSVN].value;
return true;
}
bool CMetadata::modify_metadata(const xml_parameter_t *parameter)
{
assert(parameter != NULL);
if(!check_xml_parameter(parameter))
return false;
if(!fill_enclave_css(parameter))
return false;
//if elrange is set, will update the major version to SGX_2_ELRANGE_MAJOR_VERSION
if(parameter[ELRANGESIZE].value == 0)
{
m_metadata->version = META_DATA_MAKE_VERSION(MAJOR_VERSION,MINOR_VERSION );
}
else
{
m_metadata->version = META_DATA_MAKE_VERSION(SGX_2_ELRANGE_MAJOR_VERSION,MINOR_VERSION );
}
m_metadata->size = offsetof(metadata_t, data);
m_metadata->tcs_policy = (uint32_t)parameter[TCSPOLICY].value;
m_metadata->magic_num = METADATA_MAGIC;
m_metadata->desired_misc_select = (uint32_t)parameter[MISCSELECT].value;
m_metadata->tcs_min_pool = (uint32_t)parameter[TCSMINPOOL].value;
m_metadata->enclave_css.body.misc_select = (uint32_t)parameter[MISCSELECT].value &
(uint32_t)parameter[MISCMASK].value;
m_metadata->enclave_css.body.misc_mask = (uint32_t)parameter[MISCMASK].value;
//store the elrange config for further check
m_elrange_config_entry.enclave_image_address = parameter[ENCLAVEIMAGEADDRESS].value;
m_elrange_config_entry.elrange_start_address = parameter[ELRANGESTARTADDRESS].value;
m_elrange_config_entry.elrange_size = parameter[ELRANGESIZE].value;
//set metadata.attributes
//low 64 bit: it's the same as enclave_css
memset(&m_metadata->attributes, 0, sizeof(sgx_attributes_t));
m_metadata->attributes.flags = m_metadata->enclave_css.body.attributes.flags;
//high 64 bit
//set bits that will not be checked
m_metadata->attributes.xfrm = ~m_metadata->enclave_css.body.attribute_mask.xfrm;
//set bits that have been set '1' and need to be checked
m_metadata->attributes.xfrm |= (m_metadata->enclave_css.body.attributes.xfrm & m_metadata->enclave_css.body.attribute_mask.xfrm);
// set m_create_param.xsave_size and ssa_fram_size
if(false == get_xsave_size(m_metadata->attributes.xfrm, &m_create_param.xsave_size))
{
return false;
}
m_metadata->ssa_frame_size = ROUND_TO_PAGE(m_create_param.xsave_size + (uint32_t)sizeof(ssa_gpr_t)) >> SE_PAGE_SHIFT;
m_metadata->max_save_buffer_size = m_create_param.xsave_size;
bool ret = check_config();
return ret;
}
bool CMetadata::check_xml_parameter(const xml_parameter_t *parameter)
{
//stack/heap must be page-align
if( (parameter[STACKMAXSIZE].value % ALIGN_SIZE)
|| (parameter[STACKMINSIZE].value % ALIGN_SIZE) )
{
se_trace(SE_TRACE_ERROR, SET_STACK_SIZE_ERROR);
return false;
}
if(parameter[STACKMINSIZE].value > parameter[STACKMAXSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_STACK_SIZE_ERROR);
return false;
}
if( (parameter[HEAPMAXSIZE].value % ALIGN_SIZE)
|| (parameter[HEAPMINSIZE].value % ALIGN_SIZE)
|| (parameter[HEAPINITSIZE].value % ALIGN_SIZE) )
{
se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_ALIGN_ERROR);
return false;
}
if (parameter[HEAPINITSIZE].flag != 0)
{
if (parameter[HEAPINITSIZE].value > parameter[HEAPMAXSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_INIT_MAX_ERROR);
return false;
}
if (parameter[HEAPMINSIZE].value > parameter[HEAPINITSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_INIT_MIN_ERROR);
return false;
}
}
else
{
if (parameter[HEAPMINSIZE].value > parameter[HEAPMAXSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_MAX_MIN_ERROR);
return false;
}
}
if ((parameter[RSRVMAXSIZE].value % ALIGN_SIZE)
|| (parameter[RSRVMINSIZE].value % ALIGN_SIZE)
|| (parameter[RSRVINITSIZE].value % ALIGN_SIZE))
{
se_trace(SE_TRACE_ERROR, SET_RSRV_SIZE_ALIGN_ERROR);
return false;
}
if (parameter[RSRVINITSIZE].flag != 0)
{
if (parameter[RSRVINITSIZE].value > parameter[RSRVMAXSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_RSRV_SIZE_INIT_MAX_ERROR);
return false;
}
if (parameter[RSRVMINSIZE].value > parameter[RSRVINITSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_RSRV_SIZE_INIT_MIN_ERROR);
return false;
}
}
else
{
if (parameter[RSRVMINSIZE].value > parameter[RSRVMAXSIZE].value)
{
se_trace(SE_TRACE_ERROR, SET_RSRV_SIZE_MAX_MIN_ERROR);
return false;
}
}
if(parameter[RSRVEXECUTABLE].flag != 0)
{
if(parameter[RSRVEXECUTABLE].value != 0 && parameter[RSRVEXECUTABLE].value != 1)
{
se_trace(SE_TRACE_ERROR, SET_RSRV_EXECUTABLE_ERROR);
return false;
}
}
if ((parameter[USERREGIONSIZE].value % ALIGN_SIZE))
{
se_trace(SE_TRACE_ERROR, SET_USER_REGION_SIZE_ALIGN_ERROR);
return false;
}
// LE setting: HW != 0, Licensekey = 1
// Other enclave setting: HW = 0, Licensekey = 0
if((parameter[HW].value == 0 && parameter[LAUNCHKEY].value != 0) ||
(parameter[HW].value != 0 && parameter[LAUNCHKEY].value == 0))
{
se_trace(SE_TRACE_ERROR, SET_HW_LE_ERROR);
return false;
}
if (parameter[TCSMAXNUM].flag != 0)
{
if (parameter[TCSMAXNUM].value < parameter[TCSNUM].value)
{
se_trace(SE_TRACE_ERROR, SET_TCS_MAX_NUM_ERROR);
return false;
}
if ((parameter[TCSMINPOOL].flag != 0)
&& (parameter[TCSMINPOOL].value > parameter[TCSMAXNUM].value))
{
se_trace(SE_TRACE_ERROR, SET_TCS_MIN_POOL_ERROR);
return false;
}
}
else if ((parameter[TCSMINPOOL].flag != 0)
&& (parameter[TCSMINPOOL].value > parameter[TCSNUM].value))
{
se_trace(SE_TRACE_ERROR, SET_TCS_MIN_POOL_ERROR);
return false;
}
if ((parameter[ISVEXTPRODID_H].value || parameter[ISVEXTPRODID_L].value ||
parameter[ISVFAMILYID_H].value || parameter[ISVFAMILYID_L].value) &&
parameter[ENABLEKSS].value == 0)
{
se_trace(SE_TRACE_ERROR, SET_ENABLE_KSS_ERROR);
return false;
}
if(parameter[ENCLAVEIMAGEADDRESS].flag != 0 && parameter[ELRANGESIZE].flag ==0)
{
se_trace(SE_TRACE_ERROR, SET_ENCLAVEIMAGEADDRESS_SET_ERROR);
return false;
}
if(parameter[ELRANGESTARTADDRESS].flag != 0 && parameter[ELRANGESIZE].flag ==0)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGESTARTADDRESS_SET_ERROR);
return false;
}
m_create_param.heap_init_size = parameter[HEAPINITSIZE].flag ? parameter[HEAPINITSIZE].value : parameter[HEAPMAXSIZE].value;
m_create_param.heap_min_size = parameter[HEAPMINSIZE].value;
m_create_param.heap_max_size = parameter[HEAPMAXSIZE].value;
m_create_param.rsrv_init_size = parameter[RSRVINITSIZE].flag ? parameter[RSRVINITSIZE].value : parameter[RSRVMAXSIZE].value;
m_create_param.rsrv_min_size = parameter[RSRVMINSIZE].value;
m_create_param.rsrv_max_size = parameter[RSRVMAXSIZE].value;
m_create_param.rsrv_executable = parameter[RSRVEXECUTABLE].flag ? parameter[RSRVEXECUTABLE].value : 0;
m_create_param.user_region_size = parameter[USERREGIONSIZE].value;
m_create_param.stack_max_size = parameter[STACKMAXSIZE].value;
m_create_param.stack_min_size = parameter[STACKMINSIZE].value;
m_create_param.tcs_num = (uint32_t)parameter[TCSNUM].value;
m_create_param.tcs_max_num = (uint32_t)(parameter[TCSMAXNUM].flag ? parameter[TCSMAXNUM].value : parameter[TCSNUM].value);
m_create_param.tcs_min_pool = (uint32_t)parameter[TCSMINPOOL].value;
m_create_param.tcs_policy = (uint32_t)parameter[TCSPOLICY].value;
se_trace(SE_TRACE_ERROR, "tcs_num %d, tcs_max_num %d, tcs_min_pool %d\n", m_create_param.tcs_num, m_create_param.tcs_max_num, m_create_param.tcs_min_pool);
SE_TRACE_DEBUG("RSRV_MIN_SIZE = 0x%016llX\n", m_create_param.rsrv_min_size);
SE_TRACE_DEBUG("RSRV_INIT_SIZE = 0x%016llX\n", m_create_param.rsrv_init_size);
SE_TRACE_DEBUG("RSRV_MAX_SIZE = 0x%016llX\n", m_create_param.rsrv_max_size);
SE_TRACE_DEBUG("USER_REGION_SIZE = 0x%016llX\n", m_create_param.user_region_size);
return true;
}
static uint64_t edmm_aligned_overhead(uint32_t size)
{
// header size/alignment/min_block_size comes from the emalloc
// implementation in sgx-emm module
const uint64_t header_size = sizeof(uint64_t);
const uint64_t alignment = 0x8;
const uint64_t min_block_size = 0x10;
uint64_t bsize = ROUND_TO(size + header_size, alignment);
return bsize < min_block_size ? min_block_size : bsize;
}
uint64_t CMetadata::calculate_rts_bk_overhead()
{
uint64_t ema_overhead = edmm_aligned_overhead(sizeof(struct ema_t_));
uint64_t bit_array_overhead = edmm_aligned_overhead(sizeof(struct bit_array_));
// MIN heap
uint32_t page_count = (uint32_t)(m_create_param.heap_min_size >> SE_PAGE_SHIFT);
uint64_t heap_node_overhead = ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
if(m_create_param.heap_init_size > m_create_param.heap_min_size)
{
// INIT heap
page_count = (uint32_t)((m_create_param.heap_init_size - m_create_param.heap_min_size) >> SE_PAGE_SHIFT);
heap_node_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
}
if(m_create_param.heap_max_size > m_create_param.heap_init_size)
{
page_count = (uint32_t)((m_create_param.heap_max_size - m_create_param.heap_init_size) >> SE_PAGE_SHIFT);
heap_node_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
}
page_count = (uint32_t)(m_create_param.rsrv_min_size >> SE_PAGE_SHIFT);
uint64_t rsrv_node_overhead = ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
if(m_create_param.rsrv_init_size > m_create_param.rsrv_min_size)
{
// INIT RSRV
page_count = (uint32_t)((m_create_param.rsrv_init_size - m_create_param.rsrv_min_size) >> SE_PAGE_SHIFT);
rsrv_node_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
}
if(m_create_param.rsrv_max_size > m_create_param.rsrv_init_size)
{
page_count = (uint32_t)((m_create_param.rsrv_max_size - m_create_param.rsrv_init_size) >> SE_PAGE_SHIFT);
rsrv_node_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
}
// guard page | stack | guard page | TCS | SSA | guard page | TLS
// guard page
uint64_t non_removed_ctx_overhead = ema_overhead;
uint64_t removed_ctx_overhead = ema_overhead;
// stack
page_count = (uint32_t)(m_create_param.stack_min_size >> SE_PAGE_SHIFT);
non_removed_ctx_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
removed_ctx_overhead += ema_overhead;
if(m_create_param.stack_max_size > m_create_param.stack_min_size)
{
page_count = (uint32_t)((m_create_param.stack_max_size - m_create_param.stack_min_size) >> SE_PAGE_SHIFT);
non_removed_ctx_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
removed_ctx_overhead += ema_overhead;
}
// guard page
non_removed_ctx_overhead += ema_overhead;
removed_ctx_overhead += ema_overhead;
// tcs
page_count = TCS_SIZE >> SE_PAGE_SHIFT;
non_removed_ctx_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
removed_ctx_overhead += ema_overhead;
// ssa
page_count = m_metadata->ssa_frame_size * SSA_NUM;
non_removed_ctx_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
removed_ctx_overhead += ema_overhead;
// guard page
non_removed_ctx_overhead += ema_overhead;
removed_ctx_overhead += ema_overhead;
// td
page_count = 1;
const Section *section = m_parser->get_tls_section();
if(section)
{
page_count += (uint32_t)(ROUND_TO_PAGE(section->virtual_size()) >> SE_PAGE_SHIFT);
}
non_removed_ctx_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(page_count, 8) >> 3));
removed_ctx_overhead += ema_overhead;
uint32_t tcs_min_pool = 0; /* Number of static threads (EADD) */
uint32_t tcs_eremove = 0;
if(m_create_param.tcs_min_pool > m_create_param.tcs_num - 1)
{
tcs_min_pool = m_create_param.tcs_num - 1;
tcs_eremove = 0;
}
else
{
tcs_min_pool = m_create_param.tcs_min_pool;
tcs_eremove = m_create_param.tcs_num -1 - m_create_param.tcs_min_pool;
}
// static thread contexts
uint64_t total_non_removed_ctx_overhead = non_removed_ctx_overhead;
if (tcs_min_pool > 0)
{
total_non_removed_ctx_overhead += non_removed_ctx_overhead;
if (tcs_min_pool > 1)
{
total_non_removed_ctx_overhead += non_removed_ctx_overhead * (tcs_min_pool - 1);
}
}
// eremoved thread contexts
uint64_t total_removed_ctx_overhead = removed_ctx_overhead;
if (tcs_eremove > 0)
{
total_removed_ctx_overhead += removed_ctx_overhead;
if (tcs_eremove > 1)
{
total_removed_ctx_overhead += removed_ctx_overhead * (tcs_eremove - 1);
}
}
// dynamic thread contexts
if (m_create_param.tcs_max_num > tcs_min_pool + 1)
{
total_non_removed_ctx_overhead += non_removed_ctx_overhead * (m_create_param.tcs_max_num - tcs_min_pool);
}
// PT_LOAD segments
uint64_t total_sections_overhead = 0;
std::vector<Section*> sections = m_parser->get_sections();
for (auto s : sections) {
uint32_t p_count = (uint32_t)(ROUND_TO_PAGE(s->virtual_size()) >> SE_PAGE_SHIFT);
total_sections_overhead += ema_overhead + bit_array_overhead + edmm_aligned_overhead((ROUND_TO(p_count, 8) >> 3));
}
return heap_node_overhead +
rsrv_node_overhead +
total_non_removed_ctx_overhead +
total_removed_ctx_overhead +
total_sections_overhead +
ema_overhead; // potential ema node for the last guard page
}
void *CMetadata::alloc_buffer_from_metadata(uint32_t size)
{
void *addr = GET_PTR(void, m_metadata, m_metadata->size);
m_metadata->size += size;
if((m_metadata->size < size) || (m_metadata->size > METADATA_SIZE))
{
return NULL;
}
return addr;
}
bool CMetadata::build_extend_entry_fips_sig(extend_entry_t *entry)
{
uint32_t size = (uint32_t)sizeof(extend_entry_fips_sig_t);
void *buf = alloc_buffer_from_metadata(size);
if (buf == NULL)
{
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
se_trace(SE_TRACE_ERROR, "FIPS signature extend_entry could not be allocated\n");
return false;
}
entry->entry_id = EXTEND_ENTRY_ID_FIPS_SIG;
entry->offset = (uint32_t)PTR_DIFF(buf, m_metadata);
entry->size = size;
return true;
}
bool CMetadata::build_extend_table()
{
uint32_t size = 0;
uint32_t offset = 0;
uint32_t count = 0;
if((m_create_param.fips_on == 1))
{
count++;
}
if(count > 0)
{
size = (uint32_t)sizeof(extend_entry_t) * count;
extend_entry_t *extend_table = (extend_entry_t *) alloc_buffer_from_metadata(size);
if(extend_table == NULL)
{
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
se_trace(SE_TRACE_ERROR, "Extend table could not be allocated\n");
return false;
}
int index = 0;
if((m_create_param.fips_on == 1))
{
build_extend_entry_fips_sig(&extend_table[index]);
index++;
}
offset = (uint32_t)PTR_DIFF(extend_table, m_metadata);
}
m_metadata->dirs[DIR_EXTEND].offset = offset;
m_metadata->dirs[DIR_EXTEND].size = size;
return true;
}
/*
* Called within build_layout_table(), used to assign the rva to entry layout
* and load_step to group layout.
*/
bool CMetadata::update_layout_entries()
{
m_rva = calculate_sections_size();
if(m_rva == 0)
{
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
se_trace(SE_TRACE_ERROR, "Sections size is 0\n");
return false;
}
SE_TRACE_DEBUG("\n");
for(uint32_t i = 0; i < m_layouts.size(); i++)
{
if(!IS_GROUP_ID(m_layouts[i].entry.id))
{
m_layouts[i].entry.rva = m_rva;
m_rva += (((uint64_t)m_layouts[i].entry.page_count) << SE_PAGE_SHIFT);
se_trace(SE_TRACE_DEBUG, "\tEntry Id(%2u) = %4u, %-16s, ", i, m_layouts[i].entry.id, layout_id_str[m_layouts[i].entry.id]);
se_trace(SE_TRACE_DEBUG, "Page Count = %5u, ", m_layouts[i].entry.page_count);
se_trace(SE_TRACE_DEBUG, "Attributes = 0x%02X, ", m_layouts[i].entry.attributes);
se_trace(SE_TRACE_DEBUG, "Flags = 0x%016llX, ", m_layouts[i].entry.si_flags);
se_trace(SE_TRACE_DEBUG, "RVA = 0x%016llX --- 0x%016llX\n", m_layouts[i].entry.rva, m_rva-1);
}
else
{
for (uint32_t j = 0; j < m_layouts[i].group.entry_count; j++)
{
m_layouts[i].group.load_step += ((uint64_t)(m_layouts[i-j-1].entry.page_count)) << SE_PAGE_SHIFT;
}
uint64_t temp_rva = m_rva;
m_rva += m_layouts[i].group.load_times * m_layouts[i].group.load_step;
se_trace(SE_TRACE_DEBUG, "\tEntry Id(%2u) = %4u, %-16s, ", i, m_layouts[i].entry.id, layout_id_str[m_layouts[i].entry.id & ~(GROUP_FLAG)]);
se_trace(SE_TRACE_DEBUG, "Entry Count = %4u, ", m_layouts[i].group.entry_count);
se_trace(SE_TRACE_DEBUG, "Load Times = %u, ", m_layouts[i].group.load_times);
se_trace(SE_TRACE_DEBUG, "LStep = 0x%016llX, ", m_layouts[i].group.load_step);
se_trace(SE_TRACE_DEBUG, "RVA = 0x%016llX --- 0x%016llX\n", temp_rva, m_rva-1);
}
}
return true;
}
static inline bool is_power_of_two(size_t n)
{
return (n != 0) && (!(n & (n - 1)));
}
bool CMetadata::vaildate_elrange_config()
{
if(m_elrange_config_entry.elrange_size == 0)
{
return true;
}
if(m_elrange_config_entry.enclave_image_address % SE_PAGE_SIZE != 0)
{
se_trace(SE_TRACE_ERROR, SET_ENCLAVEIMAGEADDRESS_ALIGN_ERROR);
return false;
}
if(m_elrange_config_entry.elrange_start_address % SE_PAGE_SIZE != 0)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGESTARTADDRESS_PAGE_ALIGN_ERROR);
return false;
}
if(m_elrange_config_entry.elrange_size%SE_PAGE_SIZE != 0)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGE_PAGE_ALIGN_ERROR);
return false;
}
if((m_elrange_config_entry.elrange_start_address & (m_elrange_config_entry.elrange_size -1)) != 0)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGESTARTADDRESS_ALIGN_ERROR);
return false;
}
if(m_elrange_config_entry.elrange_start_address > m_elrange_config_entry.enclave_image_address)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGESTARTADDRESS_RANGE_ERROR);
return false;
}
uint64_t enclave_end = m_elrange_config_entry.enclave_image_address + m_rva;
if(enclave_end < m_elrange_config_entry.enclave_image_address || enclave_end < m_rva)
{
se_trace(SE_TRACE_ERROR, SET_ENCLAVEIMAGEADDRESS_ERROR);
return false;
}
uint64_t elrange_end = m_elrange_config_entry.elrange_start_address+ m_elrange_config_entry.elrange_size;
if(elrange_end < m_elrange_config_entry.elrange_start_address || elrange_end < m_elrange_config_entry.elrange_size)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGE_ERROR);
return false;
}
if(enclave_end > elrange_end)
{
se_trace(SE_TRACE_ERROR, SET_ELRANGE_RANGE_ERROR);
return false;
}
if(!is_power_of_two(m_elrange_config_entry.elrange_size))
{
se_trace(SE_TRACE_ERROR, SET_ELRANGE_ALIGN_ERROR);
return false;
}
return true;
}
bool CMetadata::build_elrange_config_entry()
{
if(m_elrange_config_entry.elrange_size == 0)
{
return true;
}
uint32_t size = (uint32_t)(sizeof(elrange_config_entry_t));
elrange_config_entry_t *config_table = (elrange_config_entry_t *) alloc_buffer_from_metadata(size);
if(config_table == NULL)
{
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
se_trace(SE_TRACE_ERROR, "Elrange config table could not be allocated\n");
return false;
}
config_table->elrange_size = m_elrange_config_entry.elrange_size;
config_table->elrange_start_address = m_elrange_config_entry.elrange_start_address;
config_table->enclave_image_address = m_elrange_config_entry.enclave_image_address;
m_metadata->dirs[DIR_ELRANGE].offset = (uint32_t)PTR_DIFF(config_table, m_metadata);
m_metadata->dirs[DIR_ELRANGE].size = size;
return true;
}
bool CMetadata::build_layout_entries()
{
SE_TRACE_DEBUG("\n");
// enclave virtual size
m_metadata->enclave_size = calculate_enclave_size(m_rva);
if (m_metadata->enclave_size == (uint64_t)-1)
{
se_trace(SE_TRACE_ERROR, OUT_OF_EPC_ERROR);
return false;
}
// Add extra EPC pages to round the enclave size to power of 2
// We add a layout of guard pages
if (m_metadata->enclave_size - m_rva > 0)
{
uint32_t extra_pages = (uint32_t)((m_metadata->enclave_size - m_rva) >> SE_PAGE_SHIFT);
layout_t layout;
memset(&layout, 0, sizeof(layout));
layout.entry.id = LAYOUT_ID_GUARD;
layout.entry.rva = m_rva;