Re: [intel-sgx-kernel-dev] [PATCH v11 09/13] x86, sgx: basic routines for enclave page cache

From: Sean Christopherson
Date: Mon Jun 11 2018 - 11:12:55 EST


On Sat, 2018-06-09 at 22:32 -0700, Andy Lutomirski wrote:
> On Fri, Jun 8, 2018 at 10:22 AM Jarkko Sakkinen
> <jarkko.sakkinen@xxxxxxxxxxxxxxx> wrote:
> >
> >
> > SGX has a set of data structures to maintain information about the enclaves
> > and their security properties. BIOS reserves a fixed size region of
> > physical memory for these structures by setting Processor Reserved Memory
> > Range Registers (PRMRR). This memory area is called Enclave Page Cache
> > (EPC).
> >
> >
> > +/**
> > + * sgx_einit - EINIT an enclave with the appropriate LE pubkey hash
> > + * @sigstruct:ÂÂÂÂÂÂÂÂÂa pointer to the enclave's sigstruct
> > + * @token:ÂÂÂÂÂÂÂÂÂÂÂÂÂa pointer to the enclave's EINIT token
> > + * @secs_page:ÂÂÂÂÂÂÂÂÂa pointer to the enclave's SECS EPC page
> > + * @le_pubkey_hash:ÂÂÂÂthe desired LE pubkey hash for EINIT
> > + */
> > +int sgx_einit(struct sgx_sigstruct *sigstruct, struct sgx_einittoken *token,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂstruct sgx_epc_page *secs_page, u64 le_pubkey_hash[4])
> > +{
> > +ÂÂÂÂÂÂÂu64 __percpu *cache;
> > +ÂÂÂÂÂÂÂvoid *secs;
> > +ÂÂÂÂÂÂÂint i, ret;
> > +
> > +ÂÂÂÂÂÂÂsecs = sgx_get_page(secs_page);
> > +
> > +ÂÂÂÂÂÂÂif (!sgx_lc_enabled) {
> I'm confused.ÂÂWhat does this code path do?ÂÂIt kind of looks like the
> driver will load and just malfunction if we don't have write access to
> the MSRs.ÂÂWhat is the intended behavior?

The driver will also allow itself to load if the MSRs are read-only,
but only if the MSRs' pubkey hash matches that of its launch enclave,
i.e. the system has been pre-configured for the kernel's LE. ÂWhether
or not that is a valid scenario is probably a different discussion.

> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂret = __einit(sigstruct, token, secs);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂgoto out;
> > +ÂÂÂÂÂÂÂ}
> > +
> > +ÂÂÂÂÂÂÂcache = per_cpu(sgx_le_pubkey_hash_cache, smp_processor_id());
> > +
> > +ÂÂÂÂÂÂÂpreempt_disable();
> > +ÂÂÂÂÂÂÂfor (i = 0; i < 4; i++) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (le_pubkey_hash[i] == cache[i])
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂcontinue;
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂwrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, le_pubkey_hash[i]);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂcache[i] = le_pubkey_hash[i];
> > +ÂÂÂÂÂÂÂ}
> > +ÂÂÂÂÂÂÂret = __einit(sigstruct, token, secs);
> > +ÂÂÂÂÂÂÂpreempt_enable();
> > +
> > +out:
> > +ÂÂÂÂÂÂÂsgx_put_page(secs);
> > +ÂÂÂÂÂÂÂreturn ret;
> > +}
> > +EXPORT_SYMBOL(sgx_einit);
> > +