mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
To: "jeffxu@chromium.org" <jeffxu@chromium.org>
Cc: Andrew Brownsword <andrew.brownsword@oracle.com>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Matthias Neugschwandtner <matthias.neugschwandtner@oracle.com>,
	"tglx@linutronix.de" <tglx@linutronix.de>,
	"jeffxu@google.com" <jeffxu@google.com>,
	"jannh@google.com" <jannh@google.com>,
	"sroettger@google.com" <sroettger@google.com>,
	"x86@kernel.org" <x86@kernel.org>
Subject: Re: [RFC PATCH v2 1/1] x86/pkeys: update PKRU to enable pkey 0 before XSAVE
Date: Thu, 25 Apr 2024 22:49:29 +0000	[thread overview]
Message-ID: <7054B528-C603-4EAC-B48D-784480DFE4B6@oracle.com> (raw)
In-Reply-To: <20240425210540.3265342-1-jeffxu@chromium.org>



> On Apr 25, 2024, at 2:05 PM, jeffxu@chromium.org wrote:
> 
> From: Jeff Xu <jeffxu@chromium.org>
> 
> On 3/21/24 14:56, Aruna Ramakrishna wrote:
>> Enabling both the non-zero pkey (for the thread) and pkey zero (in
>> userspace) will not work for us. We cannot have the alt stack writeable
>> by all - the rationale here is that the code running in that thread
>> (using a non-zero pkey) is untrusted and should not have access to the
>> alternate signal stack (that uses pkey zero), to prevent the return
>> address of a function from being changed. The expectation is that kernel
>> should be able to set up the alternate signal stack and deliver the
>> signal to the application even if pkey zero is explicitly disabled by
>> the application. The signal handler accessibility should not be dictated
>> by the PKRU value that the thread sets up.
>> 
> We have a similar threat model that we don't want "untrusted threads" to
> access altstack. I think this patch need not be restricted to the
> use case of zero pkey for altstack, i.e. application can also set
> non-zero pkey to altstack and expect the same.

Agreed. In the latest version of this patchset, this assumption has been removed.

Link here:
https://lore.kernel.org/lkml/20240425180542.1042933-1-aruna.ramakrishna@oracle.com/T/#t

> 
>> Solution:
>> The PKRU register is managed by XSAVE, which means the sigframe contents
>> must match the register contents - which is not the case here. We want
>> the sigframe to contain the user-defined PKRU value (so that it is
>> restored correctly from sigcontext) but the actual register must be
>> reset to init_pkru so that the alt stack is accessible and the signal
>> can be delivered to the application. It seems that the proper fix here
>> would be to remove PKRU from the XSAVE framework and manage it
>> separately, which is quite complicated. As a workaround, this patch does
>> something like this:
>> 
>>       orig_pkru = rdpkru();
>>       wrpkru(init_pkru & orig_pkru);
>>       xsave_to_user_sigframe();
>>       put_user(pkru_sigframe_addr, orig_pkru)
>> 
> The default PKRU of thread [1] is set as 01 (disable access) for each PKEY
> from 1 to 15, and 00 (RW) for PKEY 0.
> 
> Let's use pkey 1 as an example:
> The init_pkru is 01, if the thread has PKRU (orig_pkru) as 10 (disable write
> but have read) then new_pkru from (init_pkru & orig_pkru) is 00, which gives
> RW access to the pkey 1.
> 
> When the thread has orig_pkru as 01 (disable access) or 00 (RW), new_pkru is
> unchanged from orig_pkru.
> 
> Now take pkey 0:
> the init_pkru is 00, regardless what threads has, new_pkru will always be 00.
> 
> This seems to work out well for pkey 1 to 15, i.e. signal handing code in
> kernel only give write access when the thread alrady has read access to the
> PKEY that is used by the altstack. The threat model interesting here is to
> prevent untrusted threads from writing to altstack, and read is probably less
> of a problem.
> 

This piece of code assumed that the init PKRU value allows writes to the alternative
signal stack. As you mentioned earlier, that may not always be true - a non-zero pkey
can be used for the altstack.

So the new version simply does write_pkru(0) (i.e. enabled all pkeys) before XSAVE.
Is this more reasonable? 

> 
> Does this meet what you want? (Note the pkey 0 is different than 1-15)
> 
> Suppose someone also like to disable all access to altstack, then there is one
> more place to mind: in sigreturn(), it calls restore_altstack(), and requires
> read access to altstack. However, at the time, PKRU is already restored from
> sigframe, so SEGV will raise (the value in sigframe doesn't have read access
> to the PKEY).
> 
> Without changing sigreturn, using wrpkru(0) here might not be necessary:
> the dispatch to user space works fine, only to crash at sigreturn step.
> 
> [1] defined by init_pkru_value in pkeys.c
> 
> Best regards,
> -Jeff


I see what you're saying. In rt_sigreturn():

        if (!restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags)) <— restores PKRU, disabling access to altstack
                goto badframe;
...
        if (restore_altstack(&frame->uc.uc_stack)) <— needs read access to altstack
                goto badframe;
 

I’m wary about reordering anything in here. Also, this code is not aware of the altstack permissions. I’m wondering if wrpkru(0) is needed here too.

Thanks,
Aruna



  reply	other threads:[~2024-04-25 22:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 21:56 [RFC PATCH v2 0/1] x86/pkeys: update PKRU to enable pkey 0 Aruna Ramakrishna
2024-03-21 21:56 ` [RFC PATCH v2 1/1] x86/pkeys: update PKRU to enable pkey 0 before XSAVE Aruna Ramakrishna
2024-03-22  9:46   ` Ingo Molnar
2024-03-22 18:30     ` Aruna Ramakrishna
2024-04-25 22:03     ` jeffxu
2024-03-22 15:40   ` Dave Hansen
2024-03-22 18:28     ` Aruna Ramakrishna
2024-04-25 21:05   ` jeffxu
2024-04-25 22:49     ` Aruna Ramakrishna [this message]
2024-04-26  0:12       ` Jeff Xu
2024-04-26 16:13         ` Jeff Xu
2024-04-26 16:33           ` Edgecombe, Rick P
2024-04-26 17:13             ` Jeff Xu
2024-04-25 21:58   ` jeffxu

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=7054B528-C603-4EAC-B48D-784480DFE4B6@oracle.com \
    --to=aruna.ramakrishna@oracle.com \
    --cc=andrew.brownsword@oracle.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=jannh@google.com \
    --cc=jeffxu@chromium.org \
    --cc=jeffxu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthias.neugschwandtner@oracle.com \
    --cc=sroettger@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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®