Re: [PATCH v3 15/37] x86/mm: Check Shadow Stack page fault errors

Edgecombe, Rick P
2022-11-15T20:04:08Z

On Tue, 2022-11-15 at 12:47 +0100, Peter Zijlstra wrote:
> On Fri, Nov 04, 2022 at 03:35:42PM -0700, Rick Edgecombe wrote:
> > @@ -1331,6 +1345,18 @@ void do_user_addr_fault(struct pt_regs
> > *regs,
> >
> > perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
> >
> > + /*
> > + * To service shadow stack read faults, unlike normal read
> > faults, the
> > + * fault handler needs to create a type of memory that will
> > also be
> > + * writable (with instructions that generate shadow stack
> > writes).
> > + * In the case of COW memory, the COW needs to take place
> > even with
> > + * a shadow stack read. Otherwise the shared page will be
> > left (shadow
> > + * stack) writable in userspace. So to trigger the
> > appropriate behavior
> > + * by setting FAULT_FLAG_WRITE for shadow stack accesses,
> > even if the
> > + * access was a shadow stack read.
> > + */
>
> Clear as mud... So SS pages are 'Write=0,Dirty=1', which, per
> construction, lack a RW bit. And these pages are writable (WRUSS).
>
> pte_wrprotect() seems to do: _PAGE_DIRTY->_PAGE_COW (which is really
> weird in this situation), resulting in: 'Write=0,Dirty=0,Cow=1'.
>
> That's regular RO memory and won't raise read-faults.
>
> But I'm thinking RET will trip #PF here when it tries to read the SS
> because the SSP is not a proper shadow stack page?
>
> And in that case you want to tickle pte_mkwrite() to undo the
> pte_wrprotect() above?
>
> So while the #PF is a 'read' fault due to RET not actually writing to
> the shadow stack, you want to force a write fault so it will re-
> instate
> the SS page.
>
> Did I get that right?

That's right. I think the assumption that needs to be broken in the
readers head is that you can satisfy a read fault with read-only PTE.
This is kind of baked in all over the place with the zero-pfn, COW,
etc. Maybe I should try to start with that.