diff mbox series

[4/4] X86: Use KVM CR pin MSRs

Message ID 20200617190757.27081-5-john.s.andersen@intel.com (mailing list archive)
State New
Headers show
Series Paravirtualized Control Register pinning | expand

Commit Message

Andersen, John June 17, 2020, 7:07 p.m. UTC
Strengthen existing control register pinning when running
paravirtualized under KVM. Check which bits KVM supports pinning for
each control register and only pin supported bits which are already
pinned via the existing native protection. Write to KVM CR0/4 pinned
MSRs to enable pinning.

Initiate KVM assisted pinning directly following the setup of native
pinning on boot CPU. For non-boot CPUs initiate paravirtualized pinning
on CPU identification.

Identification of non-boot CPUs takes place after the boot CPU has setup
native CR pinning. Therefore, non-boot CPUs access pinned bits setup by
the boot CPU and request that those be pinned. All CPUs request
paravirtualized pinning of the same bits which are already pinned
natively.

Guests using the kexec system call currently do not support
paravirtualized control register pinning. This is due to early boot
code writing known good values to control registers, these values do
not contain the protected bits. This is due to CPU feature
identification being done at a later time, when the kernel properly
checks if it can enable protections. As such, the pv_cr_pin command line
option has been added which instructs the kernel to disable kexec in
favor of enabling paravirtualized control register pinning. crashkernel
is also disabled when the pv_cr_pin parameter is specified due to its
reliance on kexec.

When we fix kexec, we will still need a way for a kernel with support to
know if the kernel it is attempting to load has support. If a kernel
with this enabled attempts to kexec a kernel where this is not
supported, it would trigger a fault almost immediately.

Liran suggested adding a section to the built image acting as a flag to
signify support for being kexec'd by a kernel with pinning enabled.
Should that approach be implemented, it is likely that the command line
flag (pv_cr_pin) would still be desired for some deprecation period. We
wouldn't want the default behavior to change from being able to kexec
older kernels to not being able to, as this might break some users
workflows.

Signed-off-by: John Andersen <john.s.andersen@intel.com>
---
 .../admin-guide/kernel-parameters.txt         | 11 ++++++
 arch/x86/Kconfig                              | 10 +++++
 arch/x86/include/asm/kvm_para.h               | 28 +++++++++++++
 arch/x86/kernel/cpu/common.c                  |  5 +++
 arch/x86/kernel/kvm.c                         | 39 +++++++++++++++++++
 arch/x86/kernel/setup.c                       |  8 ++++
 6 files changed, 101 insertions(+)

Comments

Dave Hansen June 18, 2020, 2:41 p.m. UTC | #1
On 6/17/20 12:07 PM, John Andersen wrote:
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 89386f6f3ab6..54fb2b5ab8fc 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -3926,6 +3926,17 @@
>  			[KNL] Number of legacy pty's. Overwrites compiled-in
>  			default number.
>  
> +	pv_cr_pin	[SECURITY,X86]
> +			Enable paravirtualized control register pinning. When
> +			running paravirutalized under KVM, request that KVM not
> +			allow the guest to disable kernel protection features
> +			set in CPU control registers. Specifying this option
> +			will disable kexec (and crashkernel). If kexec support
> +			has not been compiled into the kernel and host KVM
> +			supports paravirtualized control register pinning, it
> +			will be active by default without the need to specify
> +			this parameter.

I'm writing this last in my review.  I guess I should have read this
first.  You'll see later in my review how this confused me.  This
behavior needs to be documented elsewhere.  Code comments would be best.

Let's say kexec is config'd off.  This feature is enabled by default and
crashes the kernel in early boot.  I have no way to disable this fancy
new feature.  Is that what we want?

I also think that instead of having to *enable* this explicitly when
kexec is present, maybe we should have a "disable_kexec" parameter.  If
kexec is configured out or disabled on the command-line, then you can
turn CR pinning on.

If someone fails to kexec() because of this feature, there's no way in
hell they'll ever track down "pv_cr_pin" on the command-line as the
cause.  The might have a chance of finding disable_kexec, though.

Wouldn't it also be nice to add a single printk() the first time a kexec
fails because of this feature being present?

>  	quiet		[KNL] Disable most log messages
>  
>  	r128=		[HW,DRM]
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index 67f6a40b5e93..bc0b27483001 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -800,6 +800,7 @@ config KVM_GUEST
>  	bool "KVM Guest support (including kvmclock)"
>  	depends on PARAVIRT
>  	select PARAVIRT_CLOCK
> +	select PARAVIRT_CR_PIN
>  	select ARCH_CPUIDLE_HALTPOLL
>  	default y
>  	---help---
> @@ -835,6 +836,15 @@ config PARAVIRT_TIME_ACCOUNTING
>  config PARAVIRT_CLOCK
>  	bool
>  
> +config PARAVIRT_CR_PIN
> +       bool "Paravirtual bit pinning for CR0 and CR4"
> +       depends on KVM_GUEST
> +       help
> +         Select this option to have the virtualised guest request that the
> +         hypervisor disallow it from disabling protections set in control
> +         registers. The hypervisor will prevent exploits from disabling
> +         features such as SMEP, SMAP, UMIP, and WP.

I'm confused.  Does this add support for ""Paravirtual bit pinning", or
actually tell the guest to request pinning by default?

It says "Select this option to have the virtualised guest request...",
which makes it sound like it affects the default rather than the
availability of the option.


> +#ifdef CONFIG_PARAVIRT_CR_PIN
> +void __init kvm_paravirt_cr_pinning_init(void);
> +void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> +				   unsigned long cr4_pinned_bits);
> +#else
> +static inline void kvm_paravirt_cr_pinning_init(void)
> +{
> +	return;
> +}
> +
> +static inline void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> +						 unsigned long cr4_pinned_bits)
> +{
> +	return;
> +}
> +#endif /* CONFIG_PARAVIRT_CR_PIN */

For stuff like this that isn't the least bit performance sensitive, I
usually don't bother with header stubs.  Just do the function
declaration and then check the config option in the .c code.  It saves
#ifdef noise in the header.

>  #endif /* _ASM_X86_KVM_PARA_H */
> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> index 921e67086a00..ee17223b1fa8 100644
> --- a/arch/x86/kernel/cpu/common.c
> +++ b/arch/x86/kernel/cpu/common.c
> @@ -21,6 +21,7 @@
>  #include <linux/smp.h>
>  #include <linux/io.h>
>  #include <linux/syscore_ops.h>
> +#include <linux/kvm_para.h>
>  
>  #include <asm/stackprotector.h>
>  #include <asm/perf_event.h>
> @@ -416,6 +417,8 @@ static void __init setup_cr_pinning(void)
>  	mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP);
>  	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask;
>  	static_key_enable(&cr_pinning.key);
> +
> +	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
>  }
>  
>  /*
> @@ -1551,6 +1554,8 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)
>  	mtrr_ap_init();
>  	validate_apic_and_package_id(c);
>  	x86_spec_ctrl_setup_ap();
> +
> +	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
>  }

WP looks like it get special handling here.  But, why it is special goes
unmentioned in the changelog or comments.

Why is it special?

> +#ifdef CONFIG_PARAVIRT_CR_PIN
> +static int kvm_paravirt_cr_pinning_enabled __ro_after_init;
> +
> +void __init kvm_paravirt_cr_pinning_init(void)
> +{
> +#ifdef CONFIG_KEXEC_CORE
> +	if (!cmdline_find_option_bool(boot_command_line, "pv_cr_pin"))
> +		return;
> +
> +	/* Paravirtualized CR pinning is currently incompatible with kexec */
> +	kexec_load_disabled = 1;
> +#endif
> +
> +	kvm_paravirt_cr_pinning_enabled = 1;
> +}

This is why we don't like #ifdefs in .c files.  The CONFIG_KEXEC_CORE
one really makes this unreadable.

This is really confusing because it says, if "CONFIG_KEXEC_CORE" is off,
don't bother with looking for "pv_cr_pin" on the command-line before
setting kvm_paravirt_cr_pinning_enabled=1.  That doesn't make any sense
to me.

> +void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> +				   unsigned long cr4_pinned_bits)
> +{
> +	u64 mask;
> +
> +	if (!kvm_paravirt_cr_pinning_enabled)
> +		return;
> +
> +	if (!kvm_para_has_feature(KVM_FEATURE_CR_PIN))
> +		return;

So, if we compiled this whole mess in and got the new command-line
parameter and we got all the way here and the host doesn't support it,
we silently return?

Seems like it would at least deserve a pr_info().

> +	rdmsrl(MSR_KVM_CR0_PIN_ALLOWED, mask);
> +	wrmsrl(MSR_KVM_CR0_PINNED_HIGH, cr0_pinned_bits & mask);
> +
> +	rdmsrl(MSR_KVM_CR4_PIN_ALLOWED, mask);
> +	wrmsrl(MSR_KVM_CR4_PINNED_HIGH, cr4_pinned_bits & mask);
> +}
> +#endif
> +
>  #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
>  
>  static void kvm_disable_host_haltpoll(void *i)
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index d9c678b37a9b..ed3bcc85d40d 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -27,6 +27,9 @@
>  #include <asm/apic.h>
>  #include <asm/bios_ebda.h>
>  #include <asm/bugs.h>
> +#include <asm/kasan.h>
> +#include <asm/cmdline.h>
> +
>  #include <asm/cpu.h>
>  #include <asm/efi.h>
>  #include <asm/gart.h>
> @@ -502,6 +505,11 @@ static void __init reserve_crashkernel(void)
>  		return;
>  	}
>  
> +	if (cmdline_find_option_bool(boot_command_line, "pv_cr_pin")) {
> +		pr_info("Ignoring crashkernel since pv_cr_pin present in cmdline\n");
> +		return;
> +	}

Isn't it a bit mean to ignore crashkernel if the kernel has
CONFIG_PARAVIRT_CR_PIN=n?
Andersen, John June 18, 2020, 3:26 p.m. UTC | #2
On Thu, Jun 18, 2020 at 07:41:04AM -0700, Dave Hansen wrote:
> On 6/17/20 12:07 PM, John Andersen wrote:
> > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> > index 89386f6f3ab6..54fb2b5ab8fc 100644
> > --- a/Documentation/admin-guide/kernel-parameters.txt
> > +++ b/Documentation/admin-guide/kernel-parameters.txt
> > @@ -3926,6 +3926,17 @@
> >  			[KNL] Number of legacy pty's. Overwrites compiled-in
> >  			default number.
> >  
> > +	pv_cr_pin	[SECURITY,X86]
> > +			Enable paravirtualized control register pinning. When
> > +			running paravirutalized under KVM, request that KVM not
> > +			allow the guest to disable kernel protection features
> > +			set in CPU control registers. Specifying this option
> > +			will disable kexec (and crashkernel). If kexec support
> > +			has not been compiled into the kernel and host KVM
> > +			supports paravirtualized control register pinning, it
> > +			will be active by default without the need to specify
> > +			this parameter.
> 
> I'm writing this last in my review.  I guess I should have read this
> first.  You'll see later in my review how this confused me.  This
> behavior needs to be documented elsewhere.  Code comments would be best.
> 

Will do. Sorry for the confusion.

> Let's say kexec is config'd off.  This feature is enabled by default and
> crashes the kernel in early boot.  I have no way to disable this fancy
> new feature.  Is that what we want?
> 
> I also think that instead of having to *enable* this explicitly when
> kexec is present, maybe we should have a "disable_kexec" parameter.  If
> kexec is configured out or disabled on the command-line, then you can
> turn CR pinning on.
> 
> If someone fails to kexec() because of this feature, there's no way in
> hell they'll ever track down "pv_cr_pin" on the command-line as the
> cause.  The might have a chance of finding disable_kexec, though.
> 
> Wouldn't it also be nice to add a single printk() the first time a kexec
> fails because of this feature being present?
> 

That sounds like a good plan. I'll change pv_cr_pin to disable_kexec, and add a
disable_pv_cr_pin option in case it's being on by default via the compile time
option breaks a users workflow at runtime.

In this case, I'm assuming we can do away with the kconfig option then.

Just have it enabled by default. If kexec is present, it's disabled by default,
unless kexec is disabled, in which case, pinning is enabled unless
disable_pv_cr_pin is set.

> >  	quiet		[KNL] Disable most log messages
> >  
> >  	r128=		[HW,DRM]
> > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> > index 67f6a40b5e93..bc0b27483001 100644
> > --- a/arch/x86/Kconfig
> > +++ b/arch/x86/Kconfig
> > @@ -800,6 +800,7 @@ config KVM_GUEST
> >  	bool "KVM Guest support (including kvmclock)"
> >  	depends on PARAVIRT
> >  	select PARAVIRT_CLOCK
> > +	select PARAVIRT_CR_PIN
> >  	select ARCH_CPUIDLE_HALTPOLL
> >  	default y
> >  	---help---
> > @@ -835,6 +836,15 @@ config PARAVIRT_TIME_ACCOUNTING
> >  config PARAVIRT_CLOCK
> >  	bool
> >  
> > +config PARAVIRT_CR_PIN
> > +       bool "Paravirtual bit pinning for CR0 and CR4"
> > +       depends on KVM_GUEST
> > +       help
> > +         Select this option to have the virtualised guest request that the
> > +         hypervisor disallow it from disabling protections set in control
> > +         registers. The hypervisor will prevent exploits from disabling
> > +         features such as SMEP, SMAP, UMIP, and WP.
> 
> I'm confused.  Does this add support for ""Paravirtual bit pinning", or
> actually tell the guest to request pinning by default?
> 
> It says "Select this option to have the virtualised guest request...",
> which makes it sound like it affects the default rather than the
> availability of the option.
> 

How about this

Select this option to request protection of SMEP, SMAP, UMIP, and WP
control register bits when running paravirtualized under KVM. Protection will
be active provided the feature is available host side and kexec is disabled via
kconfig or the command line for the guest requesting protection.

> > +#ifdef CONFIG_PARAVIRT_CR_PIN
> > +void __init kvm_paravirt_cr_pinning_init(void);
> > +void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> > +				   unsigned long cr4_pinned_bits);
> > +#else
> > +static inline void kvm_paravirt_cr_pinning_init(void)
> > +{
> > +	return;
> > +}
> > +
> > +static inline void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> > +						 unsigned long cr4_pinned_bits)
> > +{
> > +	return;
> > +}
> > +#endif /* CONFIG_PARAVIRT_CR_PIN */
> 
> For stuff like this that isn't the least bit performance sensitive, I
> usually don't bother with header stubs.  Just do the function
> declaration and then check the config option in the .c code.  It saves
> #ifdef noise in the header.
> 

Sounds good

> >  #endif /* _ASM_X86_KVM_PARA_H */
> > diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
> > index 921e67086a00..ee17223b1fa8 100644
> > --- a/arch/x86/kernel/cpu/common.c
> > +++ b/arch/x86/kernel/cpu/common.c
> > @@ -21,6 +21,7 @@
> >  #include <linux/smp.h>
> >  #include <linux/io.h>
> >  #include <linux/syscore_ops.h>
> > +#include <linux/kvm_para.h>
> >  
> >  #include <asm/stackprotector.h>
> >  #include <asm/perf_event.h>
> > @@ -416,6 +417,8 @@ static void __init setup_cr_pinning(void)
> >  	mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP);
> >  	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask;
> >  	static_key_enable(&cr_pinning.key);
> > +
> > +	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
> >  }
> >  
> >  /*
> > @@ -1551,6 +1554,8 @@ void identify_secondary_cpu(struct cpuinfo_x86 *c)
> >  	mtrr_ap_init();
> >  	validate_apic_and_package_id(c);
> >  	x86_spec_ctrl_setup_ap();
> > +
> > +	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
> >  }
> 
> WP looks like it get special handling here.  But, why it is special goes
> unmentioned in the changelog or comments.
> 
> Why is it special?
> 

We're copying the behavior of native_write_cr0() which assumes we always have
WP and can set it / it always must be set.

With CR4 we leverage the fact that setup_cr_pinning() initializes
cr4_pinned_bits that contains bits enabled during feature identification
and masked with what native pinning cares about. This ensures we pin the same
bits with paravirtualized pinning that we're already pinning natively.

I'll mention it in the commit message for v2.

> > +#ifdef CONFIG_PARAVIRT_CR_PIN
> > +static int kvm_paravirt_cr_pinning_enabled __ro_after_init;
> > +
> > +void __init kvm_paravirt_cr_pinning_init(void)
> > +{
> > +#ifdef CONFIG_KEXEC_CORE
> > +	if (!cmdline_find_option_bool(boot_command_line, "pv_cr_pin"))
> > +		return;
> > +
> > +	/* Paravirtualized CR pinning is currently incompatible with kexec */
> > +	kexec_load_disabled = 1;
> > +#endif
> > +
> > +	kvm_paravirt_cr_pinning_enabled = 1;
> > +}
> 
> This is why we don't like #ifdefs in .c files.  The CONFIG_KEXEC_CORE
> one really makes this unreadable.
> 
> This is really confusing because it says, if "CONFIG_KEXEC_CORE" is off,
> don't bother with looking for "pv_cr_pin" on the command-line before
> setting kvm_paravirt_cr_pinning_enabled=1.  That doesn't make any sense
> to me.
> 

I think this will be clearer when we change the command like options to
disable_. I'll be sure to use IS_ENABLED next time, my bad I forgot here.

> > +void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
> > +				   unsigned long cr4_pinned_bits)
> > +{
> > +	u64 mask;
> > +
> > +	if (!kvm_paravirt_cr_pinning_enabled)
> > +		return;
> > +
> > +	if (!kvm_para_has_feature(KVM_FEATURE_CR_PIN))
> > +		return;
> 
> So, if we compiled this whole mess in and got the new command-line
> parameter and we got all the way here and the host doesn't support it,
> we silently return?
> 
> Seems like it would at least deserve a pr_info().
> 

Will do. I'll probably use a rate limited variant because this happens for each
CPU.

> > +	rdmsrl(MSR_KVM_CR0_PIN_ALLOWED, mask);
> > +	wrmsrl(MSR_KVM_CR0_PINNED_HIGH, cr0_pinned_bits & mask);
> > +
> > +	rdmsrl(MSR_KVM_CR4_PIN_ALLOWED, mask);
> > +	wrmsrl(MSR_KVM_CR4_PINNED_HIGH, cr4_pinned_bits & mask);
> > +}
> > +#endif
> > +
> >  #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
> >  
> >  static void kvm_disable_host_haltpoll(void *i)
> > diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> > index d9c678b37a9b..ed3bcc85d40d 100644
> > --- a/arch/x86/kernel/setup.c
> > +++ b/arch/x86/kernel/setup.c
> > @@ -27,6 +27,9 @@
> >  #include <asm/apic.h>
> >  #include <asm/bios_ebda.h>
> >  #include <asm/bugs.h>
> > +#include <asm/kasan.h>
> > +#include <asm/cmdline.h>
> > +
> >  #include <asm/cpu.h>
> >  #include <asm/efi.h>
> >  #include <asm/gart.h>
> > @@ -502,6 +505,11 @@ static void __init reserve_crashkernel(void)
> >  		return;
> >  	}
> >  
> > +	if (cmdline_find_option_bool(boot_command_line, "pv_cr_pin")) {
> > +		pr_info("Ignoring crashkernel since pv_cr_pin present in cmdline\n");
> > +		return;
> > +	}
> 
> Isn't it a bit mean to ignore crashkernel if the kernel has
> CONFIG_PARAVIRT_CR_PIN=n?
> 

Yes that is mean :) I will change this. There is of course no need to check for
this option if the config isn't enabled.
Dave Hansen June 18, 2020, 3:38 p.m. UTC | #3
On 6/18/20 8:26 AM, Andersen, John wrote:
> On Thu, Jun 18, 2020 at 07:41:04AM -0700, Dave Hansen wrote:
>> Let's say kexec is config'd off.  This feature is enabled by default and
>> crashes the kernel in early boot.  I have no way to disable this fancy
>> new feature.  Is that what we want?
>>
>> I also think that instead of having to *enable* this explicitly when
>> kexec is present, maybe we should have a "disable_kexec" parameter.  If
>> kexec is configured out or disabled on the command-line, then you can
>> turn CR pinning on.
>>
>> If someone fails to kexec() because of this feature, there's no way in
>> hell they'll ever track down "pv_cr_pin" on the command-line as the
>> cause.  The might have a chance of finding disable_kexec, though.
>>
>> Wouldn't it also be nice to add a single printk() the first time a kexec
>> fails because of this feature being present?
> 
> That sounds like a good plan. I'll change pv_cr_pin to disable_kexec, and add a
> disable_pv_cr_pin option in case it's being on by default via the compile time
> option breaks a users workflow at runtime.
> 
> In this case, I'm assuming we can do away with the kconfig option then.
> 
> Just have it enabled by default. If kexec is present, it's disabled by default,
> unless kexec is disabled, in which case, pinning is enabled unless
> disable_pv_cr_pin is set.

Yes, that sounds good to me.

...
>>> +config PARAVIRT_CR_PIN
>>> +       bool "Paravirtual bit pinning for CR0 and CR4"
>>> +       depends on KVM_GUEST
>>> +       help
>>> +         Select this option to have the virtualised guest request that the
>>> +         hypervisor disallow it from disabling protections set in control
>>> +         registers. The hypervisor will prevent exploits from disabling
>>> +         features such as SMEP, SMAP, UMIP, and WP.
>>
>> I'm confused.  Does this add support for ""Paravirtual bit pinning", or
>> actually tell the guest to request pinning by default?
>>
>> It says "Select this option to have the virtualised guest request...",
>> which makes it sound like it affects the default rather than the
>> availability of the option.
> 
> How about this
> 
> Select this option to request protection of SMEP, SMAP, UMIP, and WP
> control register bits when running paravirtualized under KVM. Protection will
> be active provided the feature is available host side and kexec is disabled via
> kconfig or the command line for the guest requesting protection.

It still isn't very clear to me.

Let's pull the config option out of this patch.  Enable the feature by
default and do the command-line processing in this patch.

If you still think a Kconfig option is helpful, add it in a separate
patch calling out the deficiencies with the boot-time options.
Andersen, John June 18, 2020, 3:49 p.m. UTC | #4
On Thu, Jun 18, 2020 at 08:38:06AM -0700, Dave Hansen wrote:
> On 6/18/20 8:26 AM, Andersen, John wrote:
> > On Thu, Jun 18, 2020 at 07:41:04AM -0700, Dave Hansen wrote:
> >>> +config PARAVIRT_CR_PIN
> >>> +       bool "Paravirtual bit pinning for CR0 and CR4"
> >>> +       depends on KVM_GUEST
> >>> +       help
> >>> +         Select this option to have the virtualised guest request that the
> >>> +         hypervisor disallow it from disabling protections set in control
> >>> +         registers. The hypervisor will prevent exploits from disabling
> >>> +         features such as SMEP, SMAP, UMIP, and WP.
> >>
> >> I'm confused.  Does this add support for ""Paravirtual bit pinning", or
> >> actually tell the guest to request pinning by default?
> >>
> >> It says "Select this option to have the virtualised guest request...",
> >> which makes it sound like it affects the default rather than the
> >> availability of the option.
> > 
> > How about this
> > 
> > Select this option to request protection of SMEP, SMAP, UMIP, and WP
> > control register bits when running paravirtualized under KVM. Protection will
> > be active provided the feature is available host side and kexec is disabled via
> > kconfig or the command line for the guest requesting protection.
> 
> It still isn't very clear to me.
> 
> Let's pull the config option out of this patch.  Enable the feature by
> default and do the command-line processing in this patch.
> 
> If you still think a Kconfig option is helpful, add it in a separate
> patch calling out the deficiencies with the boot-time options.

That's right we're going to pull it out anyway and just disable if the
disable_pv_cr_pin command line option is set. Oops. That solves that.

Thank you very much for your review Dave
Andy Lutomirski June 20, 2020, 5:13 a.m. UTC | #5
On Wed, Jun 17, 2020 at 12:05 PM John Andersen
<john.s.andersen@intel.com> wrote:
> Guests using the kexec system call currently do not support
> paravirtualized control register pinning. This is due to early boot
> code writing known good values to control registers, these values do
> not contain the protected bits. This is due to CPU feature
> identification being done at a later time, when the kernel properly
> checks if it can enable protections. As such, the pv_cr_pin command line
> option has been added which instructs the kernel to disable kexec in
> favor of enabling paravirtualized control register pinning. crashkernel
> is also disabled when the pv_cr_pin parameter is specified due to its
> reliance on kexec.

Is there a plan for fixing this for real?  I'm wondering if there is a
sane weakening of this feature that still allows things like kexec.

What happens if a guest tries to reset?  For that matter, what happens
when a guest vCPU sends SIPI to another guest vCPU?  The target CPU
starts up in real mode, right?  There's no SMEP or SMAP in real mode,
and real mode has basically no security mitigations at all.

PCID is an odd case.  I see no good reason to pin it, and pinning PCID
on prevents use of 32-bit mode.
Andersen, John June 23, 2020, 8:03 p.m. UTC | #6
On Fri, Jun 19, 2020 at 10:13:25PM -0700, Andy Lutomirski wrote:
> On Wed, Jun 17, 2020 at 12:05 PM John Andersen
> <john.s.andersen@intel.com> wrote:
> > Guests using the kexec system call currently do not support
> > paravirtualized control register pinning. This is due to early boot
> > code writing known good values to control registers, these values do
> > not contain the protected bits. This is due to CPU feature
> > identification being done at a later time, when the kernel properly
> > checks if it can enable protections. As such, the pv_cr_pin command line
> > option has been added which instructs the kernel to disable kexec in
> > favor of enabling paravirtualized control register pinning. crashkernel
> > is also disabled when the pv_cr_pin parameter is specified due to its
> > reliance on kexec.
> 
> Is there a plan for fixing this for real?  I'm wondering if there is a
> sane weakening of this feature that still allows things like kexec.
> 

I'm pretty sure kexec can be fixed. I had it working at one point, I'm
currently in the process of revalidating this. The issue was though that
kexec only worked within the guest, not on the physical host, which I suspect
is related to the need for supervisor pages to be mapped, which seems to be
required before enabling SMAP (based on what I'd seen with the selftests and
unittests). I was also just blindly turning on the bits without checking for
support when I'd tried this, so that could have been the issue too.

I think most of the changes for just blindly enabling the bits were in
relocate_kernel, secondary_startup_64, and startup_32.

> What happens if a guest tries to reset?  For that matter, what happens
> when a guest vCPU sends SIPI to another guest vCPU?  The target CPU
> starts up in real mode, right?
>

In this case we hit kvm_vcpu_reset, where we clear pinning. Yes I believe it
starts up in real mode.

> There's no SMEP or SMAP in real mode, and real mode has basically no security
> mitigations at all.
> 

We'd thought about the switch to real mode being a case where we'd want to drop
pinning. However, we weren't sure how much weaker, if at all, it makes this
protection.

Unless someone knows, I'll probably need to do some digging into what an
exploit might look like that tries switching to real mode and switching back as
a way around this protection.

If we can use the switch to real mode as a drop pinning trigger then I think
that might just solve the kexec problem.

> PCID is an odd case.  I see no good reason to pin it, and pinning PCID
> on prevents use of 32-bit mode.

Maybe it makes sense to default to the values we have, but allow host userspace
to overwrite the allowed values, in case some other guest OS wants to do
something that Linux doesn't with PCID or other bits.
Andersen, John July 3, 2020, 9:48 p.m. UTC | #7
> > Is there a plan for fixing this for real?  I'm wondering if there is a
> > sane weakening of this feature that still allows things like kexec.
> > 
> 
> I'm pretty sure kexec can be fixed. I had it working at one point, I'm
> currently in the process of revalidating this. The issue was though that
> kexec only worked within the guest, not on the physical host, which I suspect
> is related to the need for supervisor pages to be mapped, which seems to be
> required before enabling SMAP (based on what I'd seen with the selftests and
> unittests). I was also just blindly turning on the bits without checking for
> support when I'd tried this, so that could have been the issue too.
> 
> I think most of the changes for just blindly enabling the bits were in
> relocate_kernel, secondary_startup_64, and startup_32.
> 

So I have a naive fix for kexec which has only been tested to work under KVM.
When tested on a physical host, it did not boot when SMAP or UMIP were set.
Undoubtedly it's not the correct way to do this, as it skips CPU feature
identification, opting instead for blindly setting the bits. The physical host
I tested this on does not have UMIP so that's likely why it failed to boot when
UMIP gets set blindly. Within kvm-unit-tests, the test for SMAP maps memory as
supervisor pages before enabling SMAP. I suspect this is why setting SMAP
blindly causes the physical host not to boot.

Within trampoline_32bit_src() if I add more instructions I get an error
about "attempt to move .org backwards", which as I understand it means
there are only so many instructions allowed in each of those functions.

My suspicion is that someone with more knowledge of this area has a good
idea on how best to handle this. Feedback would be much appreciated.

> > There's no SMEP or SMAP in real mode, and real mode has basically no security
> > mitigations at all.
> > 
> 
> We'd thought about the switch to real mode being a case where we'd want to drop
> pinning. However, we weren't sure how much weaker, if at all, it makes this
> protection.
> 
> Unless someone knows, I'll probably need to do some digging into what an
> exploit might look like that tries switching to real mode and switching back as
> a way around this protection.
> 

TL;DR We probably shouldn't use the switch to real mode as a trigger to drop
pinning.

This protection assumes that the attacker is at the point where they have the
ability to write a payload for a ROP/JOP attack and gain control of execution.

For this case where we are going to switch to real mode we need to add an
assumption that the attacker has a write primitive that allows them to write
part of their payload to memory that will be addressable within 16 bit mode.

If the attacker has this write primitive, the attack becomes write payloads,
within the first stage, switch to real mode, use stage two within real mode via
JOP or just machine code (since there's we don't have to worry NX) to setup
protected mode and jump back into the kernel with protections disabled.

> > PCID is an odd case.  I see no good reason to pin it, and pinning PCID
> > on prevents use of 32-bit mode.
> 
> Maybe it makes sense to default to the values we have, but allow host userspace
> to overwrite the allowed values, in case some other guest OS wants to do
> something that Linux doesn't with PCID or other bits.

In the next version of this patchset I've made it so that the default allowed
values are WP, SMEP, SMAP, and UMIP. However, a write to the allowed MSR from
the host VMM (QEMU) can change which bits are allowed.
Arvind Sankar July 4, 2020, 3:11 p.m. UTC | #8
On Fri, Jul 03, 2020 at 09:48:14PM +0000, Andersen, John wrote:
> > > Is there a plan for fixing this for real?  I'm wondering if there is a
> > > sane weakening of this feature that still allows things like kexec.
> > > 
> > 
> > I'm pretty sure kexec can be fixed. I had it working at one point, I'm
> > currently in the process of revalidating this. The issue was though that
> > kexec only worked within the guest, not on the physical host, which I suspect
> > is related to the need for supervisor pages to be mapped, which seems to be
> > required before enabling SMAP (based on what I'd seen with the selftests and
> > unittests). I was also just blindly turning on the bits without checking for
> > support when I'd tried this, so that could have been the issue too.
> > 
> > I think most of the changes for just blindly enabling the bits were in
> > relocate_kernel, secondary_startup_64, and startup_32.
> > 
> 
> So I have a naive fix for kexec which has only been tested to work under KVM.
> When tested on a physical host, it did not boot when SMAP or UMIP were set.
> Undoubtedly it's not the correct way to do this, as it skips CPU feature
> identification, opting instead for blindly setting the bits. The physical host
> I tested this on does not have UMIP so that's likely why it failed to boot when
> UMIP gets set blindly. Within kvm-unit-tests, the test for SMAP maps memory as
> supervisor pages before enabling SMAP. I suspect this is why setting SMAP
> blindly causes the physical host not to boot.
> 
> Within trampoline_32bit_src() if I add more instructions I get an error
> about "attempt to move .org backwards", which as I understand it means
> there are only so many instructions allowed in each of those functions.
> 
> My suspicion is that someone with more knowledge of this area has a good
> idea on how best to handle this. Feedback would be much appreciated.

You can simply increase the value of TRAMPOLINE_32BIT_CODE_SIZE in
pgtable.h, assuming you don't need a very large increase. There's one
page available for code + stack at present.
diff mbox series

Patch

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 89386f6f3ab6..54fb2b5ab8fc 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3926,6 +3926,17 @@ 
 			[KNL] Number of legacy pty's. Overwrites compiled-in
 			default number.
 
+	pv_cr_pin	[SECURITY,X86]
+			Enable paravirtualized control register pinning. When
+			running paravirutalized under KVM, request that KVM not
+			allow the guest to disable kernel protection features
+			set in CPU control registers. Specifying this option
+			will disable kexec (and crashkernel). If kexec support
+			has not been compiled into the kernel and host KVM
+			supports paravirtualized control register pinning, it
+			will be active by default without the need to specify
+			this parameter.
+
 	quiet		[KNL] Disable most log messages
 
 	r128=		[HW,DRM]
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 67f6a40b5e93..bc0b27483001 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -800,6 +800,7 @@  config KVM_GUEST
 	bool "KVM Guest support (including kvmclock)"
 	depends on PARAVIRT
 	select PARAVIRT_CLOCK
+	select PARAVIRT_CR_PIN
 	select ARCH_CPUIDLE_HALTPOLL
 	default y
 	---help---
@@ -835,6 +836,15 @@  config PARAVIRT_TIME_ACCOUNTING
 config PARAVIRT_CLOCK
 	bool
 
+config PARAVIRT_CR_PIN
+       bool "Paravirtual bit pinning for CR0 and CR4"
+       depends on KVM_GUEST
+       help
+         Select this option to have the virtualised guest request that the
+         hypervisor disallow it from disabling protections set in control
+         registers. The hypervisor will prevent exploits from disabling
+         features such as SMEP, SMAP, UMIP, and WP.
+
 config JAILHOUSE_GUEST
 	bool "Jailhouse non-root cell support"
 	depends on X86_64 && PCI
diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h
index 57fd1966c4ea..f021531e98dc 100644
--- a/arch/x86/include/asm/kvm_para.h
+++ b/arch/x86/include/asm/kvm_para.h
@@ -112,6 +112,23 @@  static inline void kvm_spinlock_init(void)
 }
 #endif /* CONFIG_PARAVIRT_SPINLOCKS */
 
+#ifdef CONFIG_PARAVIRT_CR_PIN
+void __init kvm_paravirt_cr_pinning_init(void);
+void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
+				   unsigned long cr4_pinned_bits);
+#else
+static inline void kvm_paravirt_cr_pinning_init(void)
+{
+	return;
+}
+
+static inline void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
+						 unsigned long cr4_pinned_bits)
+{
+	return;
+}
+#endif /* CONFIG_PARAVIRT_CR_PIN */
+
 #else /* CONFIG_KVM_GUEST */
 #define kvm_async_pf_task_wait_schedule(T) do {} while(0)
 #define kvm_async_pf_task_wake(T) do {} while(0)
@@ -145,6 +162,17 @@  static inline bool kvm_handle_async_pf(struct pt_regs *regs, u32 token)
 {
 	return false;
 }
+
+static inline void kvm_paravirt_cr_pinning_init(void)
+{
+	return;
+}
+
+static inline void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
+						 unsigned long cr4_pinned_bits)
+{
+	return;
+}
 #endif
 
 #endif /* _ASM_X86_KVM_PARA_H */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 921e67086a00..ee17223b1fa8 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -21,6 +21,7 @@ 
 #include <linux/smp.h>
 #include <linux/io.h>
 #include <linux/syscore_ops.h>
+#include <linux/kvm_para.h>
 
 #include <asm/stackprotector.h>
 #include <asm/perf_event.h>
@@ -416,6 +417,8 @@  static void __init setup_cr_pinning(void)
 	mask = (X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_UMIP);
 	cr4_pinned_bits = this_cpu_read(cpu_tlbstate.cr4) & mask;
 	static_key_enable(&cr_pinning.key);
+
+	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
 }
 
 /*
@@ -1551,6 +1554,8 @@  void identify_secondary_cpu(struct cpuinfo_x86 *c)
 	mtrr_ap_init();
 	validate_apic_and_package_id(c);
 	x86_spec_ctrl_setup_ap();
+
+	kvm_setup_paravirt_cr_pinning(X86_CR0_WP, cr4_pinned_bits);
 }
 
 static __init int setup_noclflush(char *arg)
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 7e6403a8d861..def913b86a99 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -23,6 +23,8 @@ 
 #include <linux/kprobes.h>
 #include <linux/nmi.h>
 #include <linux/swait.h>
+#include <linux/init.h>
+#include <linux/kexec.h>
 #include <asm/timer.h>
 #include <asm/cpu.h>
 #include <asm/traps.h>
@@ -33,6 +35,7 @@ 
 #include <asm/hypervisor.h>
 #include <asm/tlb.h>
 #include <asm/cpuidle_haltpoll.h>
+#include <asm/cmdline.h>
 
 DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
 
@@ -723,6 +726,7 @@  static void __init kvm_apic_init(void)
 static void __init kvm_init_platform(void)
 {
 	kvmclock_init();
+	kvm_paravirt_cr_pinning_init();
 	x86_platform.apic_post_init = kvm_apic_init;
 }
 
@@ -877,6 +881,41 @@  void __init kvm_spinlock_init(void)
 
 #endif	/* CONFIG_PARAVIRT_SPINLOCKS */
 
+#ifdef CONFIG_PARAVIRT_CR_PIN
+static int kvm_paravirt_cr_pinning_enabled __ro_after_init;
+
+void __init kvm_paravirt_cr_pinning_init(void)
+{
+#ifdef CONFIG_KEXEC_CORE
+	if (!cmdline_find_option_bool(boot_command_line, "pv_cr_pin"))
+		return;
+
+	/* Paravirtualized CR pinning is currently incompatible with kexec */
+	kexec_load_disabled = 1;
+#endif
+
+	kvm_paravirt_cr_pinning_enabled = 1;
+}
+
+void kvm_setup_paravirt_cr_pinning(unsigned long cr0_pinned_bits,
+				   unsigned long cr4_pinned_bits)
+{
+	u64 mask;
+
+	if (!kvm_paravirt_cr_pinning_enabled)
+		return;
+
+	if (!kvm_para_has_feature(KVM_FEATURE_CR_PIN))
+		return;
+
+	rdmsrl(MSR_KVM_CR0_PIN_ALLOWED, mask);
+	wrmsrl(MSR_KVM_CR0_PINNED_HIGH, cr0_pinned_bits & mask);
+
+	rdmsrl(MSR_KVM_CR4_PIN_ALLOWED, mask);
+	wrmsrl(MSR_KVM_CR4_PINNED_HIGH, cr4_pinned_bits & mask);
+}
+#endif
+
 #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
 
 static void kvm_disable_host_haltpoll(void *i)
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index d9c678b37a9b..ed3bcc85d40d 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -27,6 +27,9 @@ 
 #include <asm/apic.h>
 #include <asm/bios_ebda.h>
 #include <asm/bugs.h>
+#include <asm/kasan.h>
+#include <asm/cmdline.h>
+
 #include <asm/cpu.h>
 #include <asm/efi.h>
 #include <asm/gart.h>
@@ -502,6 +505,11 @@  static void __init reserve_crashkernel(void)
 		return;
 	}
 
+	if (cmdline_find_option_bool(boot_command_line, "pv_cr_pin")) {
+		pr_info("Ignoring crashkernel since pv_cr_pin present in cmdline\n");
+		return;
+	}
+
 	/* 0 means: find the address automatically */
 	if (!crash_base) {
 		/*