Re: [intel-sgx-kernel-dev] [PATCH v7 4/8] intel_sgx: driver for Intel Software Guard Extensions

From: Sean Christopherson
Date: Tue Dec 12 2017 - 16:48:58 EST


On Fri, 2017-12-08 at 07:31 -0800, Christopherson, Sean J wrote:
> Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx> wrote:
> > On Thu, Dec 07, 2017 at 02:46:39PM +0000, Christopherson, Sean J wrote:
> > > > + for (i = 0; i < 2; i++) {
> > > > +ÂÂÂÂÂÂÂÂÂva_page = list_first_entry(&encl->va_pages,
> > > > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂstruct sgx_va_page, list);
> > > > +ÂÂÂÂÂÂÂÂÂva_offset = sgx_alloc_va_slot(va_page);
> > > > +ÂÂÂÂÂÂÂÂÂif (va_offset < PAGE_SIZE)
> > > > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbreak;
> > > > +
> > > > +ÂÂÂÂÂÂÂÂÂlist_move_tail(&va_page->list, &encl->va_pages);
> > > > + }
> > >Â
> > > This is broken, there is no guarantee that the next VA page will have
> > > a free slot.ÂÂYou have to walk over all VA pages to guarantee a slot
> > > is found, e.g. this caused EWB and ELDU errors.
>
> > I did run some extensive stress tests on this and did not experience any
> > issues. Full VA pages are always put to the end. Please point me to the
> > test where this breaks so that I can fix the issue if it persists.
>Â
> Three VA pages in the enclave: A, B and C.ÂÂEvict all pages in the
> enclave, i.e. consume all slots in A, B and C.ÂÂThe list can be in
> any order at this point, but for the sake of argument let's say the
> order is C->A->B, i.e. C was originally the last VA page in the list.
> Fault in page X, whose VA is in B.ÂÂEvict X.ÂÂThis code looks at C
> and A, and finds no available slot, but continues with VA page A and
> a va_offset of PAGE_SIZE.

So it looks like you avoid the described case by moving B to the head of
the list in sgx_eldu.ÂÂThe bug I am seeing is still straightforward to
theorize:

ÂÂÂÂ1. Three VA pages.ÂÂList = A->B->C
ÂÂÂÂ2. Fill A and B, use one entry in C.ÂÂList = C->B->A
ÂÂÂÂ3. ELDU, freeing a slot in B.ÂÂList = B->C->A
ÂÂÂÂ4. EWB, consuming the last slot in B.ÂÂList = B->C->A
ÂÂÂÂ5. ELDU, freeing a slot in A.ÂÂList = A->B->C
ÂÂÂÂ6. EWB, consuming the last slot in A.ÂÂList = A->B->C
ÂÂÂÂ7. ELDU, but both A and B are full
ÂÂÂÂ8. Explode