Re: [patch V3 4/6] x86/pkru: Make PKRU=0 actually work

From: Thomas Gleixner
Date: Tue Jun 08 2021 - 15:34:25 EST


On Tue, Jun 08 2021 at 17:40, Borislav Petkov wrote:
> Just typos:
>
> On Tue, Jun 08, 2021 at 04:36:21PM +0200, Thomas Gleixner wrote:
>> So that wreckages any copy_to/from_user() on the way back to user space
>
> wrecks
>
>> which hits memory which is protected by the default PKRU value.
>>
>> Assumed that this does not fail (pure luck) then T1 goes back to user
>> space and because TIF_NEED_FPU_LOAD is set it ends up in
>>
>> switch_fpu_return()
>> __fpregs_load_activate()
>> if (!fpregs_state_valid()) {
>> load_XSTATE_from_task();
>> }
>>
>> But if nothing touched the FPU between T1 scheduling out and in the
> ^^
>
> s/in/if/ it
> seems.

No. It should be

But if nothing touched the FPU between T1 scheduling out and back in,
then the fpregs_state is still valid which means switch_fpu_return()
does nothing and just clears TIF_NEED_FPU_LOAD. Back to user space with
DEFAULT_PKRU loaded. -> FAIL #2!

>> + * XRSTOR will set PKRU to 0. If the bit is not set then
>> + * get_xsave_addr() will return NULL because the PKRU value
>> + * in memory is not valid. This means pkru_val has to be
>> + * set to 0 and not to init_pkru_value.
>> + */
>> pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
>> - if (pk)
>> - pkru_val = pk->pkru;
>> + pkru_val = pk ? pk->pkru : 0;
>> }
>
> Hohumm, let's see who cries out... :-\

Why? It was clearly wrong and I can reproduce it with a hack which
forces a schedule to a kernel thread and it fails all the way back to
user space.

I chased that because I observed sporadic failures when forcing PKRU to
init state and then observed the default key being written. I had some
extra trace_printks there to analyze something completely different :)

Thanks,

tglx