32
Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen, Ilya Alexandrovich, IGai AnaH, Dror Caspi, Simon Johnson, Rebekah Leslie-Hurd, Carlos Rozas Intel CorporaHon Saeid Mofrad

Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

Intel®So*wareGuardExtensions(Intel®SGX)SupportforDynamicMemoryManagementInsideanEnclave

FrankMcKeen,IlyaAlexandrovich,IGaiAnaH,DrorCaspi,SimonJohnson,RebekahLeslie-Hurd,CarlosRozasIntelCorporaHon

SaeidMofrad

Page 2: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

1-INTRODUCTION:

SGX:So*wareGuardExtensionsprovidesthecapabilitytoprotectspecifiedareasofanapplicaHonfromoutsideaccess.TheareaiscalledanenclaveandhardwareprovidesconfidenHalityandintegrityforthespecifiedarea.SGXallowsso*waredeveloperstobuildtrustedmodulesinsideanapplicaHontoprotectsecrets.Aso*waredeveloperspecifiesthecontentsofanenclaveandarelyingpartycanconfirmthattheareaisinstanHatedcorrectlyonaremotemachine.

Page 3: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,
Page 4: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,
Page 5: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,
Page 6: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ApplicaHondevelopmentconsideraHon:

Page 7: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ApplicaHondevelopmentconsideraHon[3]:

Page 8: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ApplicaHondevelopmentconsideraHon[3]:

Page 9: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ApplicaHondevelopmentconsideraHon[3]:

Page 10: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

MOTIVATION OF SGX-2: THREE SHORTCOMINGS WITH THE SGX1

•  Firstallenclavememorymustbecommi2edatenclavebuild4me.Thisincreasesthebuild4me.Commi\ngmemoryplacespressureontheenclavepagecache(EPC),theenclavedevelopermustallocatememoryforworst-casememoryconsumpHonofanyworkload.Otherwise,theenclavedeveloperwillneedtoreleasesenclavesdesignedfordifferentsizeworkloads.

•  Thesecondshortcomingisrelatedtothemanagementofaccesspermissionsassociatedwithanenclavepage.SGXextendstheaccesspermissionmodelbyassociaHnganaddiHonalsetofaccesspermissionswithenclavepagethatarestoredinaSGXstructurecalledtheEnclavePageCacheMap(EPCM)

•  ThelastshortcomingisrelatedtolibraryOSsupportwheresecureexcepHonalhandlingandlazyloadingcodeinsideanenclaveareimportantfeatures.SGX-1didn’thaveinformaHonrecordedwhenageneralprotecHonfaultorpagefaultoccursinsideanenclave

•  Toaddresstheseproblemssixnewinstruc4onsandnewexcep4onbehaviorwereaddedtotheSGXarchitectureknownasSGX2

Page 11: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

2 SGX2 CONSIDERATIONS & REQUIREMENTS: •  ManipulaHngmemoryandpermissionsofanenclavemustbedonewiththeknowledgeandconsentoftheenclave.

•  Ifenclavecodeischangedincorrectlyorwithoutknowledgeoftheenclave,execuHonshouldbesuspendedunHlthecondiHonisresolved.Itenablestheenclavetomanageitsownsecurity

•  Thesystemresourcemanager(OSorVMM)mustbeabletomanageandallocatetheresourcesasrequestedusingstandardtechniquesandprioriHes.

•  ManipulaHonofmemorypermissionsinvolvesboththesystempermissionsandtheEPCMpermissions.EPCMpermissionsallowtheenclavedevelopertospecifytherestricHonsandaccesscontrolfortheenclave

Page 12: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

•  SGX2memorymanagement->(systemmanager)whichmanagesthesystemresources

•  internalenclaveresourcemanager(internalmanager)whichmanagestheenclavememoryfrominsidetheenclave.

•  AprotocolwhichconsistsofcommunicaHonbetweenthesystemmanagerandaninternalmanageristhis:

•  Thesystemmemorymanager•  allocaHngmemory->pagingmemory->changing

permissions,->changingpagetypes.->managingthepagetableentrypermissions->iniHaHngEPCMpermissionsoftheenclaves(bycallinginstrucHon.)

•  Theinternalmanager•  star4ngmemorychangerequests->verifying

thatthesystemmanagerhasprocessedtherequestscorrectly.

•  Theinternalmanagerdoesnothavedirectaccesstothepagetablesandmustrequestthesystemmanagertomakechangesinpagetableentry(PTE)permissions.

Page 13: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

2.1 SECURITY CONSIDERATIONS

•  Mustensurethatchangesinpermissiondonotaffectthesecurityoftheenclave.

•  Whenrestrictpagepermissions->checkpermissionrestric4onsarecompleteandthepreviouscachedaddresstranslaHonsorcachedpermissionsareremoved.SGX2checksoldpermissionsareremovedfromtheTLBs

•  SGX1allowsthesystemmemorymanagertoremovepagesfromanenclaveusingtheEREMOVEleaffuncHon.However,sincetheenclavedoesn’tparHcipateinthisprocessitdoesn’tknowifthepageremoved.

Page 14: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

2.2 SOFTWARE CONSIDERATIONS

•  Internalmemorymanagerwantstoreallocatethememoryresources:addathread;mustallocatedasThreadControlStructure(TCS),StateSaveArea(SSA)pages.Addmorememorytoenclave.

•  Excep4onRepor4ngInsideanEnclave:forLibraryOSusage.InthiscasetheexcepHoncondiHonshouldbereportedinsidetheenclave.SGX2addsseveralexcepHoncondiHonstotheSSAframewhenexiHnganenclave.Theyincludepagefaults(#PF)andgeneralprotecHonviolaHons(#GP).

•  DemandLoadingofLibraryPages:TheinternalmanagermusthaveamechanismtoloadthepagewithoutallowingaccessunHlthecopyiscomplete.SGX2addsaleaffuncHontoperformthecopysecurely.

Page 15: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

3.1 ENCLAVE MALLOC

•  Thefollowingisprotocol:

•  1.Internalmanagerrequestsmemory->enclaverunHmesystemfromitsinternalpoolofmemory.memorypoollowtheinternalmanager->requeststhesystemmanagertoallocatemorememory.

•  2.Thesystemmanagerallocatesvirtualaddressspacebutdoesnotcommitmemoryand->returnsareferencetothevirtualaddressspacetotheinternalmanager

•  3.Theenclaveinternalmanager->returnsareferencetotheenclave.Whentheenclaveaccessesthenewlyallocatedmemory,->apagefaultisgeneratedasmemoryhasnotbeencommiGed.

•  4.TheOSpagefaulthandlerdetectsthatthevirtualaddresshasbeenallocatedbutmemoryhasnotbeencommiGed.->TheOScommitsmemorybyusingEAUGandmapsthecommiGedbutpendingpageintotheenclaveaddressspace->TheOSthensendsasignaltotheenclaveinternalmanager.

•  5.Theinternalmanagerreceivesthe->TheinternalmanagerchecksthatthevirtualaddresshasbeencommiGed->theinternalmanagerexecutesEACCEPTwhichallowstheenclavetoaccessthependingpage.->ThesignalhandlerreturnsbacktotheapplicaHonwhicheventuallyresultsintheenclaveexecuHonresuming.

Page 16: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

3.2 ENCLAVE FREE

•  Thefollowingisanexampleprotocol:

•  1.Theenclavereleasesmemory->internalmanagerreleaseaddressspacebacktotheOS.

•  2.ThesystemmanagerexecutesEMODTonallpages->changethepagetypetoPT_TRIMand->cleartheEPCMaccesspermissionbits.Thisbeginstheprocessofdecommi\ngmemory.ThesystemmanagerthenexecutesETRACKontheSECSofthecallingenclaveandthensendsIPIstologicalprocessorswhichmaycontainTLBmappingstothepagesthathadbeentrimmed.

•  3.OncealllogicalprocessorsrespondedtotheIPI,controlisreturnedtotheinternalmanager.

•  4.TheinternalmanagerverifiesthatcommiGedmemoryhasbeendecommiGedbyexecuHngEACCEPTtoverifythatthepagestrimmedandallstaleTLBmappingshavebeenflushed.TheinternalmanagerneedstoupdateitstrackinginformaHonthatthevirtualaddresshasnocommiGedmemory.

•  5.ThesystemmanagercanlaterreclaimthecommiGedmemorybyexecuHngEREMOVEonthetrimmedpages.

Page 17: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

3.3 CHANGING PAGE PERMISSIONS

•  Changeispermissivethenthefollowingprotocol:

•  1.internalmanagerrunsEMODPEtoextendthepagepermissionsintheEPCM.

•  2.Theinternalmanagerrequeststhesystemmanagertoextendpagepermissionsinthepagetables.

•  IfthechangeinpermissionisrestricHvethenthefollowingprotocol:

•  1.Theinternalmanagerrequeststhatthesystemmanagertorestrictpermissionsonapage.

•  2.ThesystemmanagerexecutesEMODPRandupdatespagetablepermissions.A*erpermissionshavebeenupdated,thesystemmanagerexecutesETRACKontheSECSofthecallingenclaveandsendsIPIstoallprocessorsthatmaybeexecuHnginsidetheenclavetoflushTLBmappings.

•  3.A*erallIPIshavebeenacknowledged,controlisreturnedtotheinternalmanager.TheinternalmanagerverifiesthatpagepermissionsrestrictedandTLBmappingsflushedbyexecuHngEACCEPT

Page 18: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

3.4 THREAD CONTROL STRUCTURE ALLOCATION

•  1.InternalmanageriniHalizesfromaregularEPCpagewithappropriateTCSvalues.IftheenclavememoryhasnotbeencommiGedtheninternalmanagerwillneedtoperformarequesttoallocatememoryasdescribedinsecHon3.1.,theninternalmanagerrequeststhatthesystemmanagerconvertthepagetoaTCS.

•  2.ThesystemmanagerexecutesEMODTtosetthepagetypetoPT_TCSandtocleartheEPCMaccesspermissionbits.ThepageisalsomarkedmodifiedwhichpreventsthepagefrombeingusedasaTCS.

•  3.ThesystemmanagerthenexecutesETRACK.ThesystemmanagersendsIPIstoflushalloldmappingstothepageandreturnscontroltotheinternalmanager.

•  4.TheinternalmanagerexecutesEACCEPTonthemodifiedTCSpage.EACCEPTwillverifythatTLBmappingsflushedandperformconsistencychecksontheTCSpagethenclearingthemodifiedbitandmakingthepageavailabletoEENTER.

Page 19: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

DYNAMIC LOADING OF MODULES

•  SGX2providesEACCEPTCOPYwhichallowstheinternalmanagertoatomicallyiniHalizethecontentsandpermissionofapage.

•  1.theinternalmanagerindicatestothesystemmanagerthatavirtualaddressspaceallocatedbutnotcommiGed(sameasin3.1).

•  2.WhenanenclaveaGemptstoaccessapageinthisvirtualaddress,apagefaultisgeneratedandthesystemmanagercommitsmemorybyexecuHngEAUGandsignalstheinternalmanager.

•  3.TheinternalmanageridenHfiesthevirtualaddressasbelongingtoamodulepagetobeloaded.ThesystemmanagermayloadthecontentsofthepageintoregularmemoryortheenclaverunHmesystemmayneedtorequestthecontentbeloadedintoregularmemory.

•  4.Theinternalmanagerthencopiesthecontentsofthemoduleintoprivateenclavememory.TheinternalmanagershouldverifytheintegrityofthecontentsandapplyanyrequiredrelocaHons.Finally,theinternalmanagercopiesthecontentsandiniHalizespermissionsusingEACCEPTCOPY.

Page 20: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

3.6 LIBRARY OS SUPPORT

1.  TheprocessbeginswithanexcepHongeneratedinsideanenclave.TheprocessorrecordsexcepHoninformaHonintheSSAanddeliverstheexcepHontotheOSexcepHonhandler.

2.  IftheOScannothandletheexcepHon,theOSsignalstheLibOSPAL(PlamormAdaptaHonLayer)excepHonhandler.

3.  3.TheLibOSPALexecutesEENTERtoinvoketheLibOSexcepHonhandlerinsidetheenclave.

4.  4.TheLibOSexcepHonhandlerreadstheexcepHoninformaHonthengeneratesanOSspecificexcepHoncontext,andinvokestheapplicaHonexcepHonhandlerinsidetheSGXenabledLibOS.

Page 21: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,
Page 22: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

4.1 SGX2 ISA, ENCLS LEAF FUNCTIONS , EAUG •  EAUGaugmentstheenclavewithapageofEPCmemory->associatesthatpagewithanSECSpage,andupdaHngthelinearaddressandsecurityaGributesinthepage’sEPCM->putsthepagein“Pending”state.

•  twoinputparameters,apointertothedesHnaHonpageinEPC,andapointertotheenclave’sSECSpage.

•  Whilein“Pending”state,thepagecannotbeaccessedbyanyone,includingtheenclave.Onlya*ertheenclaveapprovesthepagebyusingtheENCLU[EACCEPT]thepagebeaccessibletotheenclave.

Page 23: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLS LEAF FUNCTIONS , EMODT

•  EMODTmodifiesthetypeofanEPCpageandputsthepagein“Modified”state.AllowedpagetypesarePT_TCSandPT_TRIM.TheoperaHonreceivestwoinputparameters,apointertothetargetpageinEPC,andapointertothepage’snewsecurityaGributes.Whilein“Modified”state,thepagecannotbeaccessedbyanyone,includingtheenclave.Onlya*ertheenclaveapprovesthepagebyusingtheENCLU[EACCEPT]leaffuncHon,willthepagebeaccessibletotheenclave.

Page 24: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLS LEAF FUNCTIONS , EMODPR

•  EMODPRThisleaffuncHonrestrictstheaccessrightsassociatedwithanEPCpageofaniniHalizedenclaveandputsthepagein“PermissionRestricHon”state.TheoperaHonreceivestwoinputparameters,apointertothetargetpageinEPC,andapointertothepage’snewsecurityaGributes.TheoperaHonwillfailifitaGemptstoextendthepermissionsofthepage.Whilein“PermissionRestricHon”state,thepagecannotbeaccessedbyanyone,includingtheenclave.Onlya*ertheenclaveapprovesthepagebyusingtheENCLU[EACCEPT]leaffuncHon,willthepagebeaccessibletotheenclave.

Page 25: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLU LEAF FUNCTIONS, EACCEPT

•  ThisleaffuncHonmustbeexecutedfromwithinanenclave.ItacceptschangestoapageintherunningenclavebyverifyingthatthesecurityaGributesspecifiedinSECINFOmatchthepage’ssecurityaGributesinEPCM.TheoperaHonreceivestwoinputparameters,apointertothetargetpageinEPC,andapointertothepage’sapprovednewsecurityaGributes.A*erasuccessfulexecuHonofEACCEPTthepage’s“Pending”,“Modified”,or“PermissionRestricHon”stateisclearedandthepagebecomesaccessibletotheenclave.

Page 26: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLU LEAF FUNCTIONS ,EACCEPTCOPY

•  ThisleaffuncHonmustbeexecutedfromwithinanenclave.ItcopiesthecontentsofanexisHngEPCpageintoanuniniHalizedEPCpagethatwascreatedbyEAUG.TheoperaHonreceivesthreeinputparameters,apointertothetargetpageinEPC,apointertothepage’snewsecurityaGributes,andapointertothepage’snewcontent.A*erasuccessfulexecuHonofEACCEPTCOPYthepage’s“Pending”stateisclearedandthepagebecomesaccessiblefortheenclave

Page 27: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLU LEAF FUNCTIONS, EMODPE

•  ThisleaffuncHonmustbeexecutedfromwithinanenclave.ItextendstheaccessrightsassociatedwithanexisHngEPCpageintherunningenclave.TheoperaHonreceivestwoinputparameters,apointertothetargetpageinEPC,andapointertothepage’snewsecurityaGributes.TheoperaHonwillfailifitaGemptstorestrictpermissionsofthepage.SincetheexecuHonhappensfromwithintheenclave,it’strustedandtakeseffectimmediately.

Page 28: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

MANAGING PAGE TABLE TRANSLATIONS

Page 29: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

ENCLAVE EXCEPTION HANDLING ENHANCEMENTS

•  thecauseoftheAEXisstoredintheEXITINFOfieldintheSSA.

•  IfSECS.MISCSELECT.EXINFObitissetbyenclavewriter,theprocessorsaves#PFand#GPinformaHonintotheEXINFOstructure

Page 30: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

4.5 EPCM-INDUCED MEMORY FAULT REPORTING

•  A#PFexcepHonisgenerated•  AbitinthePageFaultErrorCode(PFEC)indicatesthatthepagefaultwasduetoEPCMaccesschecks.ThisbitislocatedatbitposiHon15andcalled“SGX”

Page 31: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

SUMMARY AND RELATED WORK

•  NewinstrucHonstotheSGX1providebeGerso*waredevelopmentenvironmentwhilemaintainingthesecurityoftheenclave.TheSGX2instrucHonsenablebeGerprotecHonofproprietarycodewhichcanbeloadedandthenprotectedusingtheEPCM.

•  Allowfordynamicmemoryandthreadingsupport•  SupportdynamicallocaHonoflibrarypagesinthelibraryOSenvironment.

Page 32: Intel® So*ware Guard Extensions (Intel® SGX) Support for ... · Intel® So*ware Guard Extensions (Intel® SGX) Support for Dynamic Memory Management Inside an Enclave Frank McKeen,

EndofPresenta4on