Re: PKRU issue while using alternate signal stack

From: Dave Hansen
Date: Wed Feb 21 2024 - 15:46:09 EST


On 2/21/24 11:54, Aruna Ramakrishna wrote:
> If the pkru_write_default() call were to move up the flow here, before
> copy_fpstate_to_sigframe(), then the signal handling would work as
> expected. But this code/flow is quite complicated, and we’d appreciate
> some expert opinion.

First, I think you're not the first ones to report this, or want the
behavior tweaked. I can't seem to find the thread at the moment, but
you might want to search to see if you have some fellow travelers here.

This is a bit of a chicken-and-egg problem. We used to have some
complicated code to munge the (compacted+supervisor) kernel fpstate into
the (uncompacted+user) userspace sigframe. That sucked, so we
simplified it to always use XSAVE to write the uncompacted+user format.

But that implementation choice fundamentally means that the register
state *MUST* match sigframe contents, at least at the time of XSAVE.
That's in direct conflict to your requirement that the sigframe be
written with different PKRU contents than what was in place at the time
that the exception happened.

That means we either need to abandon the xsave_to_user_sigframe()
approach, or we need to do something like:

tmp_pkru = rdpkru();
wrpkru(0);
xsave_to_user_sigframe();
put_user(pkru_sigframe_addr, tmp_pkru);

Which is horrid.

There are other games you could play with get_user_pages(), vmap() and
XSAVE but those would be even more horrid.

The simplest option is to just leave the altstacks writeable by all.