mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nadav Amit <nadav.amit@gmail.com>
To: Andy Lutomirski <luto@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Willy Tarreau <w@1wt.eu>, X86 ML <x86@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH RFC v2 4/6] x86: Disable PTI on compatibility mode
Date: Thu, 15 Feb 2018 12:58:38 -0800	[thread overview]
Message-ID: <9EB804CA-0EC9-4CBB-965A-F3C8520201E7@gmail.com> (raw)
In-Reply-To: <CALCETrUY4uZeG2fz=OQWGuyCOcBBNbep6TaJz82CZSP89=g3-A@mail.gmail.com>

Andy Lutomirski <luto@kernel.org> wrote:

> On Thu, Feb 15, 2018 at 4:36 PM, Nadav Amit <namit@vmware.com> wrote:
>> Based on the understanding that there should be no way for userspace to
>> address the kernel-space from compatibility mode, disable it while
>> running in compatibility mode as long as the 64-bit code segment of the
>> user is not used.
>> 
>> Reenabling PTI is performed by restoring NX-bits to the userspace
>> mappings, flushing the TLBs, and notifying all the CPUs that use the
>> affected mm to disable PTI. Each core responds by removing the present
>> bit for the 64-bit code-segment, and marking that PTI is disabled on
>> that core.
> 
> I dislike this patch because it's conflating two things.  The patch
> claims to merely disable PTI for compat tasks, whatever those are.
> But it's also introducing a much stronger concept of what a compat
> task is.  The kernel currently mostly doesn't care whether a task is
> "compat" or not, and I think that most remaining code paths that do
> care are buggy and should be removed.
> 
> I think the right way to approach this is to add a new arch_prctl()
> that changes allowable bitness, like this:
> 
> arch_prctl(ARCH_SET_ALLOWED_GDT_CS, X86_ALLOW_CS32 | X86_ALLOW_CS64);
> 
> this would set the current task to work the normal way, where 32-bit
> and 64-bit CS are available.  You could set just X86_ALLOW_CS32 to
> deny 64-bit mode and just X86_ALLOW_CS64 to deny 32-bit mode.  This
> would make nice attack surface reduction tools for the more paranoid
> sandbox users to use.  Doing arch_prctl(ARCH_SET_ALLOWED_GDT_CS, 0)
> would return -EINVAL.
> 
> A separate patch would turn PTI off if you set X86_ALLOW_CS32.
> 
> This has the downside that old code doesn't get the benefit without
> some code change, but that's not the end of the world.
> 
>> +static void pti_cpu_update_func(void *info)
>> +{
>> +       struct mm_struct *mm = (struct mm_struct *)info;
>> +
>> +       if (mm != this_cpu_read(cpu_tlbstate.loaded_mm))
>> +               return;
>> +
>> +       /*
>> +        * Keep CS64 and CPU settings in sync despite potential concurrent
>> +        * updates.
>> +        */
>> +       set_cpu_pti_disable(READ_ONCE(mm->context.pti_disable));
>> +}
> 
> I don't like this at all.  IMO a sane implementation should never
> change PTI status on a remote CPU.  Just track it per task.

>From your comments in the past, I understood that this requirement
(enabling/disabling PTI per mm) came from Linus. I can do it per task, but
that means that the NX-bit will always be cleared on the kernel page-table
for user mappings.

>> +void __pti_reenable(void)
>> +{
>> +       struct mm_struct *mm = current->mm;
>> +       int cpu;
>> +
>> +       if (!mm_pti_disable(mm))
>> +               return;
>> +
>> +       /*
>> +        * Prevent spurious page-fault storm while we set the NX-bit and have
>> +        * yet not updated the per-CPU pti_disable flag.
>> +        */
>> +       down_write(&mm->mmap_sem);
>> +
>> +       if (!mm_pti_disable(mm))
>> +               goto out;
>> +
>> +       /*
>> +        * First, mark the PTI is enabled. Although we do anything yet, we are
>> +        * safe as long as we do not reenable CS64. Since we did not update the
>> +        * page tables yet, this may lead to spurious page-faults, but we need
>> +        * the pti_disable in mm to be set for __pti_set_user_pgd() to do the
>> +        * right thing.  Holding mmap_sem would ensure matter we hold the
>> +        * mmap_sem to prevent them from swamping the system.
>> +        */
>> +       mm->context.pti_disable = PTI_DISABLE_OFF;
>> +
>> +       /* Second, restore the NX bits. */
>> +       pti_update_user_pgds(mm, true);
> 
> You're holding mmap_sem, but there are code paths that touch page
> tables that don't hold mmap_sem, such as the stack extension code.

Do they change user mappings? These are the only ones pti_update_user_pgds()
changes.

>> +
>> +bool pti_handle_segment_not_present(long error_code)
>> +{
>> +       if (!static_cpu_has(X86_FEATURE_PTI))
>> +               return false;
>> +
>> +       if ((unsigned short)error_code != GDT_ENTRY_DEFAULT_USER_CS << 3)
>> +               return false;
>> +
>> +       pti_reenable();
>> +       return true;
>> +}
> 
> Please don't.  You're trying to emulate the old behavior here, but
> you're emulating it wrong.  In particular, you won't trap on LAR.

Yes, I thought I’ll manage to address LAR, but failed. I thought you said
this is not a “show-stopper”. I’ll adapt your approach of using prctl, although
it really limits the benefit of this mechanism.

  reply	other threads:[~2018-02-15 20:58 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 16:35 [PATCH RFC v2 0/6] x86: Disabling PTI in " Nadav Amit
2018-02-15 16:35 ` [PATCH RFC v2 1/6] x86: Skip PTI when disable indication is set Nadav Amit
2018-02-15 18:10   ` Dave Hansen
2018-02-15 19:51   ` Andy Lutomirski
2018-02-15 20:51     ` Nadav Amit
2018-02-15 23:35       ` Andy Lutomirski
2018-02-15 16:35 ` [PATCH RFC v2 2/6] x86: Save pti_disable for each mm_context Nadav Amit
2018-02-15 16:35 ` [PATCH RFC v2 3/6] x86: Switching page-table isolation Nadav Amit
2018-02-15 16:36 ` [PATCH RFC v2 4/6] x86: Disable PTI on compatibility mode Nadav Amit
2018-02-15 20:02   ` Andy Lutomirski
2018-02-15 20:58     ` Nadav Amit [this message]
2018-02-15 23:29       ` Andy Lutomirski
2018-02-16  0:08         ` Linus Torvalds
2018-02-16  0:22           ` Nadav Amit
2018-02-16  0:42             ` Linus Torvalds
2018-02-16  3:03               ` Andy Lutomirski
2018-02-16  4:55                 ` Nadav Amit
2018-02-16  0:35           ` Andrew Cooper
2018-02-16 15:20           ` Andy Lutomirski
2018-02-16  7:11         ` Cyrill Gorcunov
2018-02-16 22:07           ` Dmitry Safonov
2018-02-16 22:11             ` Nadav Amit
2018-02-16 16:25     ` Dmitry Safonov
2018-02-15 16:36 ` [PATCH RFC v2 5/6] x86: Use global pages when PTI is disabled Nadav Amit
2018-02-15 16:54   ` Dave Hansen
2018-02-15 17:36     ` Nadav Amit
2018-02-15 17:47     ` Nadav Amit
2018-02-15 18:08       ` Dave Hansen
2018-02-15 19:53   ` Andy Lutomirski
2018-02-15 20:32     ` Dave Hansen
2018-02-15 20:45       ` Nadav Amit
2018-02-15 16:36 ` [PATCH RFC v2 6/6] selftest: x86: test using CS64 on compatibility-mode Nadav Amit
2018-02-16  0:21 ` [PATCH RFC v2 0/6] x86: Disabling PTI in compatibility mode Dave Hansen
2018-02-16  0:25   ` Nadav Amit
2018-02-16  0:42     ` Dave Hansen
2018-02-16  0:48       ` Nadav Amit
2018-02-16  0:45     ` Andrew Cooper
2018-02-16  0:51       ` Nadav Amit
2018-02-16  1:04         ` Andrew Cooper
2018-02-16  3:05         ` Andy Lutomirski

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=9EB804CA-0EC9-4CBB-965A-F3C8520201E7@gmail.com \
    --to=nadav.amit@gmail.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=w@1wt.eu \
    --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®