mbox series

[0/5] x86/pkeys: PKRU manipulation bug fixes and cleanups

Message ID 20210527235109.B2A9F45F@viggo.jf.intel.com (mailing list archive)
Headers show
Series x86/pkeys: PKRU manipulation bug fixes and cleanups | expand

Message

Dave Hansen May 27, 2021, 11:51 p.m. UTC
Andy Lutomirski recently noted a probable bug in write_pkru(), but
it was unclear if it was user-visible.  A recent bug report in
related code[1] forced me to take a look.

Basically, manipulation of XSAVE state is too unstructured.
get_xsave_addr() gives callers the impression they can read and
write XSAVE state when there are a lot of pitfalls, like updates
to xstate.features bits.

As a result, more than one call site screws up the modification
of PKRU in the XSAVE buffer.  This series fixes that problem up
and also hopefully carves out a less error-prone path that can
be reused for other XSAVE features.

This series:
 * Moves the PKRU manipulation to a more appropriate location,
   away from the page table code
 * Wraps get_xsave_addr() with more structured, less error-prone
   interfaces.
 * Conditionally hides a pkey debugfs file, eliminating the need
   for new runtime checks to work with the new interface.
 * Add a selftest to make it more likely to catch bugs like this
   in the future.  This improved selftest catches this issue on
   Intel CPUs.  Without the improvement, it only triggers on AMD.

This has been lightly tested so far.  It probably needs a
tested-by from at least the AMD folks before anyone applies it.

1. https://lore.kernel.org/linux-kselftest/b2e0324a-9125-bb34-9e76-81817df27c48@amd.com/

--

 arch/x86/include/asm/fpu/internal.h          |    8 -
 arch/x86/include/asm/fpu/xstate.h            |  130 +++++++++++++++++++++++++++
 arch/x86/include/asm/pgtable.h               |   29 ------
 arch/x86/include/asm/processor.h             |    7 +
 arch/x86/kernel/cpu/common.c                 |    6 -
 arch/x86/kernel/fpu/xstate.c                 |    2 
 arch/x86/kernel/process_64.c                 |    1 
 arch/x86/kvm/svm/sev.c                       |    1 
 arch/x86/kvm/x86.c                           |    1 
 arch/x86/mm/pkeys.c                          |   13 +-
 tools/testing/selftests/vm/Makefile          |    4 
 tools/testing/selftests/vm/pkey-x86.h        |    1 
 tools/testing/selftests/vm/protection_keys.c |   73 +++++++++++++++
 13 files changed, 227 insertions(+), 49 deletions(-)

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Babu Moger <babu.moger@amd.com>
Cc: Dave Kleikamp <dave.kleikamp@oracle.com>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Thiago Jung Bauermann <bauerman@linux.ibm.com>

Comments

Thomas Gleixner May 28, 2021, 3:32 p.m. UTC | #1
On Thu, May 27 2021 at 16:51, Dave Hansen wrote:
> Andy Lutomirski recently noted a probable bug in write_pkru(), but
> it was unclear if it was user-visible.  A recent bug report in
> related code[1] forced me to take a look.
>
> Basically, manipulation of XSAVE state is too unstructured.
> get_xsave_addr() gives callers the impression they can read and
> write XSAVE state when there are a lot of pitfalls, like updates
> to xstate.features bits.
>
> As a result, more than one call site screws up the modification
> of PKRU in the XSAVE buffer.  This series fixes that problem up
> and also hopefully carves out a less error-prone path that can
> be reused for other XSAVE features.
>
> This series:
>  * Moves the PKRU manipulation to a more appropriate location,
>    away from the page table code
>  * Wraps get_xsave_addr() with more structured, less error-prone
>    interfaces.
>  * Conditionally hides a pkey debugfs file, eliminating the need
>    for new runtime checks to work with the new interface.
>  * Add a selftest to make it more likely to catch bugs like this
>    in the future.  This improved selftest catches this issue on
>    Intel CPUs.  Without the improvement, it only triggers on AMD.

I think all of this is fundamentaly wrong.

Contrary to FPU state, PKRU has to be updated at context switch
time. There is absolutely no point in having PKRU XSAVES managed.

It's broken in several ways. Anything which clears and loads the FPU
will load the wrong PKRU value. Go figure...

So the right thing is to disable PKRU in XCR0 and on sched out simply do

   task->thread.pkru = read_pkru();

and on sched in

   write_pkru(task->thread.pkru);

Simple, trivial and not going to be wreckaged by anything which fiddles
with xstates. We all know by now that xstates is a trainwreck and not
having stuff like that in there is making the fixes I'm doing way
simpler.

CET will have a similar issue, but we'll discuss that once we have the
existing horrors sorted.

Thanks,

        tglx
Dave Hansen May 28, 2021, 4:11 p.m. UTC | #2
On 5/28/21 8:32 AM, Thomas Gleixner wrote:
>>
>> This series:
>>  * Moves the PKRU manipulation to a more appropriate location,
>>    away from the page table code
>>  * Wraps get_xsave_addr() with more structured, less error-prone
>>    interfaces.
>>  * Conditionally hides a pkey debugfs file, eliminating the need
>>    for new runtime checks to work with the new interface.
>>  * Add a selftest to make it more likely to catch bugs like this
>>    in the future.  This improved selftest catches this issue on
>>    Intel CPUs.  Without the improvement, it only triggers on AMD.
> I think all of this is fundamentaly wrong.
> 
> Contrary to FPU state, PKRU has to be updated at context switch
> time. There is absolutely no point in having PKRU XSAVES managed.
> 
> It's broken in several ways. Anything which clears and loads the FPU
> will load the wrong PKRU value. Go figure...
> 
> So the right thing is to disable PKRU in XCR0 and on sched out simply do
> 
>    task->thread.pkru = read_pkru();
> 
> and on sched in
> 
>    write_pkru(task->thread.pkru);
> 
> Simple, trivial and not going to be wreckaged by anything which fiddles
> with xstates. We all know by now that xstates is a trainwreck and not
> having stuff like that in there is making the fixes I'm doing way
> simpler.

As for the general sentiment that PKRU is not suitable for management
with XSAVE, I'm with you.

I have a few concerns about moving away from XSAVE management, though.
I'm not nixing the whole idea, but there are some things we need to resolve.

First is that there _may_ be ABI concerns.  The pkey selftest, for
instance, manipulates the PKRU state on the signal stack and expects
PKRU to be set in XCR0 so that it can do this.  I wouldn't be shocked if
some other pkey user depended on the XSAVE signal stack ABI this way.

There are also the usual concerns that folks doing user-level context
switching or other insanity get PKRU context switching for "free" when
it's XSAVE-managed.  Moving away from that could break them.

I'll ask around.  We could also pretty trivially put some surveillance
in the sigreturn code to look for PKRU changes.

Second, the XSAVE/FPU abomination is actually really handy for pkeys:
1. It establishes a *different* state upon signal delivery.  Like
   sigaltstack, this means that the signal handler can recover from
   what would normally be a fatal condition like WRPKRU(0x3).  I *think*
   this is OK today even if the signal entry XRSTOR did not touch PKRU
   since there's another write_pkru() in this path.
2. It allows the signal handler to inspect the interrupted context's
   PKRU value. (used in the selftest)
3. It allows the signal handler to *override* the PKRU value of the
   interrupted context.  This is used in the selftest as an easy way
   to let a memory access instruction execute that initially causes
   a pkey-induced page fault as opposed to messing with RIP.

None of this is insurmountable.  For the selftest, I need to go looking
at how important that functionality is and look for some alternatives.
Andy Lutomirski May 28, 2021, 5:13 p.m. UTC | #3
On Fri, May 28, 2021, at 9:11 AM, Dave Hansen wrote:
> On 5/28/21 8:32 AM, Thomas Gleixner wrote:
> >>
> >> This series:
> >>  * Moves the PKRU manipulation to a more appropriate location,
> >>    away from the page table code
> >>  * Wraps get_xsave_addr() with more structured, less error-prone
> >>    interfaces.
> >>  * Conditionally hides a pkey debugfs file, eliminating the need
> >>    for new runtime checks to work with the new interface.
> >>  * Add a selftest to make it more likely to catch bugs like this
> >>    in the future.  This improved selftest catches this issue on
> >>    Intel CPUs.  Without the improvement, it only triggers on AMD.
> > I think all of this is fundamentaly wrong.
> > 
> > Contrary to FPU state, PKRU has to be updated at context switch
> > time. There is absolutely no point in having PKRU XSAVES managed.
> > 
> > It's broken in several ways. Anything which clears and loads the FPU
> > will load the wrong PKRU value. Go figure...
> > 
> > So the right thing is to disable PKRU in XCR0 and on sched out simply do
> > 
> >    task->thread.pkru = read_pkru();
> > 
> > and on sched in
> > 
> >    write_pkru(task->thread.pkru);
> > 
> > Simple, trivial and not going to be wreckaged by anything which fiddles
> > with xstates. We all know by now that xstates is a trainwreck and not
> > having stuff like that in there is making the fixes I'm doing way
> > simpler.
> 
> As for the general sentiment that PKRU is not suitable for management
> with XSAVE, I'm with you.
> 
> I have a few concerns about moving away from XSAVE management, though.
> I'm not nixing the whole idea, but there are some things we need to resolve.
> 
> First is that there _may_ be ABI concerns.  

I tend to think that, for -stable, we should fix the bug without an ABI change.
Thomas Gleixner May 28, 2021, 5:19 p.m. UTC | #4
On Fri, May 28 2021 at 09:11, Dave Hansen wrote:
> On 5/28/21 8:32 AM, Thomas Gleixner wrote:
> There are also the usual concerns that folks doing user-level context
> switching or other insanity get PKRU context switching for "free" when
> it's XSAVE-managed.  Moving away from that could break them.

Both issues are trivial to solve.

We can have pkru enabled in xcr0 and just do not restore it when
returning to user space (clear the mask bit).

When we restore it in sigrestore via xrstor then we read it via rdpkru
afterwards and update task->thread.pkru.

Thanks,

        tglx
Thomas Gleixner May 28, 2021, 5:19 p.m. UTC | #5
On Fri, May 28 2021 at 10:13, Andy Lutomirski wrote:
> On Fri, May 28, 2021, at 9:11 AM, Dave Hansen wrote:
>> I have a few concerns about moving away from XSAVE management, though.
>> I'm not nixing the whole idea, but there are some things we need to resolve.
>> 
>> First is that there _may_ be ABI concerns.  
>
> I tend to think that, for -stable, we should fix the bug without an ABI change.

See my other mail. It's trivial enough to do.