From: "Andy Lutomirski" <luto@kernel.org>
To: "Fangfei Yang" <yangff1@gmail.com>,
"Dave Hansen" <dave.hansen@intel.com>
Cc: "Dave Hansen" <dave.hansen@linux.intel.com>,
"Kees Cook" <keescook@chromium.org>,
"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
"Stephen Röttger" <sroettger@google.com>,
"the arch/x86 maintainers" <x86@kernel.org>
Subject: Re: PKU usage improvements for threads
Date: Mon, 05 Sep 2022 21:34:30 -0700 [thread overview]
Message-ID: <e1c67217-1143-4643-a7a3-ca3f34f28d30@www.fastmail.com> (raw)
In-Reply-To: <20220903001412.17015-1-yangff1@gmail.com>
On Fri, Sep 2, 2022, at 5:14 PM, Fangfei Yang wrote:
> I guess the question here is whether the code to call sigaltstack and
> signal handler is considered part of the security code (sigreturn
> obviously has to be, since the kernel has to restore the PKRU based on
> the saved fpu).
> I think to a large extent this is necessary, at least for the signal
> handler to be able to access the relevant registers at the time of the
> interrupt, which may contain data that the handler should not have
> access to. Even specifying a PKRU at the time of signal registration
> would make the system functionally sound and safe since the relevant
> calls must be protected.
>
> It's just that the design here should be such as to minimize the ways
> in which the interface can be abused (e.g., accidental override access)
> as well as to simplify the difficulty of writing secure code. It might
> be reasonable, then, to save the PKRU when the `sigaltstack` is called.
>
> The main purpose is to simplify the design of the handler entry point
> without adding new system calls, while not accidentally gaining
> privileges that do not belong to the current PKRU because of the system
> call, whether immediately, or later in signal delivery.
I think you might be so much more familiar with the system you’re working on than anyone else that you’re not explaining some basics and we’re all lost.
How is PKRU a “privilege” and what do you mean my “immediately”? I can’t follow this.
>
> This is because this part of the design can be largely made easier if
> additional source checking and PKRU switching by the handler at the
> entry point can be avoided.
Why would the entry point check a source? Or change PKRU? What would its PKRU logic be and why?
As I see it, the handler can (awkwardly, perhaps) manage PKRU just fine for all purposes except kernel access to the signal stack.
>
> As `WRPKRU` can be abused, if the handler uses this instruction,
> additional SP as well as PKRU checks must be performed to prevent
> malicious programs from forging signals, and the check must get
> multiplex among all threads. However, for the kernel, it takes very
> little code to avoid these checks by giving the handler the PKRU it
> wants.
Can you elaborate? Of course WRPKRU can be abused to fully bypass PKRU protection.
>
> If only one PKEY is specified, then it is likely that `WRPKRU` is still
> needed, since the TCB itself may occupy multiple PKEYs, or, the handler
> need to access the memory of other PKEYs (e.g., complex multi-domain
> signal designs).
>
> And, logically, it makes sense for a signal context (sigaltstack) to
> have the same PKRU when it is registered, and when it is used in the
> future. Thus, a special flag in `ss_flags & SS_SAVEPKRU` to ask the
> kernel to save the current PKRU would be sufficient.
This isn’t logical at all to me. It makes some sense as an API simplification to avoid a new syscall, and it makes sense in a bizarre (to me) world in which user code can control access to PKRU but not to sigaltstack(), but why do we live in that world?
>
> From the security side, if the current PKRU does not have access to the
> signal stack, then a future signal occurring when the kernel uses this
> PKRU to write will also result in an segfault, thus avoiding unwanted
> access through sigaltstack.
Do you mean in current kernels?
> This is also more accurate than checking the PKEY of the page when
> registering the signal stack (if we restricted the PKRU when
What do you mean “accurate”?
> registering the sigaltstack). Consider a possible error: a page is
> accidentally unmaped after being registered as a signal stack, and then
> another page that should not have been accessed by this PKRU is mapped
> to the same location, thus causing an override during signal delivery.
>
>> I also bet we could do this with minimal new ABI. There's already a
>> ->ss_flags field. We could assign a flag to mean that stack_t doesn't
>> end at '->ss_size' and that there's a pkey value *after* ss_size. I do
>> think having a single pkey that is made accessible before signal entry
>> is a more flexible ABI than taking an explicit PKRU value.
>
> Agreed, the most flexible way should be allow setting the PKRU to any
> subset of the current PKRU. So we can check `(~new_pkru) & current_pkru
> == 0` when calling sigaltstack.
>
> However, no matter how it is done, one of the more disgusting thing is
> that code like this appears in the program that handles the signal.
> ```
> old_pkru = read_pkru();
> write_pkru(stack_pkru);
> do_xsave();
> *(fpu_saved + pkru_offset()) = old_pkru; // this may be an argument of
> fpu function call
> ```
> And when restoring, you also need
> ```
> old_pkru = *(fpu_saved + pkru_offset());
> *(fpu_saved + pkru_offset()) = stack_pkru;
> do_xstor();
> write_pkru(old_pkru);
Sorry, what code does XSAVE here?
> ```
> These plus the testing of the current runtime environment (MPK) are
> truly disgusting. It's just structually ugly.
next prev parent reply other threads:[~2022-09-06 4:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-22 20:40 Kees Cook
2022-08-22 21:11 ` Dave Hansen
2022-08-23 11:08 ` Stephen Röttger
2022-08-23 18:12 ` Dave Hansen
2022-08-23 18:24 ` Andy Lutomirski
2022-08-24 8:51 ` Stephen Röttger
2022-08-24 16:28 ` Dave Hansen
2022-08-24 16:45 ` Andy Lutomirski
2022-08-25 12:30 ` Stephen Röttger
2022-08-25 14:36 ` Dave Hansen
2022-09-02 17:18 ` Andy Lutomirski
2022-09-03 0:16 ` Fangfei Yang
2022-09-03 0:14 ` Fangfei Yang
2022-09-06 4:34 ` Andy Lutomirski [this message]
2022-09-06 5:58 ` Fangfei Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e1c67217-1143-4643-a7a3-ca3f34f28d30@www.fastmail.com \
--to=luto@kernel.org \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sroettger@google.com \
--cc=x86@kernel.org \
--cc=yangff1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®