x86/livepatch: Make livepatching compatible with CET Shadow Stacks
PATCH v2 1/1 x86/livepatch: Make livepatching compatible with CET Shadow Stacks
Just like the alternatives infrastructure, the livepatch infrastructure disables CR0.WP to perform patching, which is not permitted with CET active.
Modify arch_livepatch_{quiesce,revive}() to disable CET before disabling WP, and reset the dirty bits on all virtual regions before re-enabling CET.
One complication is that arch_livepatch_revive() has to fix up the top of the shadow stack. This depends on the functions not being inlined, even under LTO. Another limitation is that reset_virtual_region_perms() may shatter the final superpage of .text depending on alignment.
This logic, and its downsides, are temporary until the patching infrastructure can be adjusted to not use CR0.WP.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Pawel Wieczorkiewicz <[email protected]>
CC: Paul Durrant <[email protected]>
For 4.14. This is a bug in a 4.14 feature, with a very low risk to non-CET usecases.
v2:
* nolinline, and extra ifdefary
* Expand comments
--- xen/arch/x86/livepatch.c | 35 +++++++++++++++++++++++++++++++++-- xen/common/virtual_region.c | 15 +++++++++++++++ xen/include/xen/virtual_region.h | 1 + 3 files changed, 49 insertions(+), 2 deletions(-)
xen/arch/x86/livepatch.c +33 -2
@@ -12,6 +12,7 @@#include <xen/livepatch.h> #include <xen/sched.h> #include <xen/vm_event.h> +#include <xen/virtual_region.h> #include <asm/fixmap.h> #include <asm/nmi.h>
@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk ) +write_cr4 (read_cr4 () & ~X86_CR4_CET ); +/* Disable WP to allow changes to read-only pages. */ write_cr0 (read_cr0 () & ~X86_CR0_WP );return 0 ; } -void arch_livepatch_revive (void ) +void noinline arch_livepatch_revive(void) {/* Reinstate WP. */ write_cr0 (read_cr0 () |X86_CR0_WP ); + +/* Clobber dirty bits and reinstate CET, if applicable. */ +if (IS_ENABLED (CONFIG_XEN_SHSTK ) && cpu_has_xen_shstk ) + { +unsigned long tmp; + +reset_virtual_region_perms (); + +write_cr4 (read_cr4 () |X86_CR4_CET ); + +/* +* Fix up the return address on the shadow stack, which currently +* points at arch_livepatch_quiesce()'s caller. +* +* Note: this is somewhat fragile, and depends on both +* arch_livepatch_{quiesce,revive}() being called from the same +* function, which is currently the case. +* +* Any error will result in Xen dying with #CP, and its too late to +* recover in any way. +*/ + asmvolatile ("rdsspq %[ssp];" +"wrssq %[addr], (%[ssp]);" + : [ssp]"=&r" (tmp) + : [addr]"r" (__builtin_return_address (0 ))); + } }int arch_livepatch_verify_func (const struct livepatch_func *func)
xen/common/virtual_region.c +15 -0
@@ -4,6 +4,7 @@#include <xen/init.h> #include <xen/kernel.h> +#include <xen/mm.h> #include <xen/rcupdate.h> #include <xen/spinlock.h> #include <xen/virtual_region.h>
@@ -91,6 +92,20 @@ void unregister_virtual_region(struct virtual_region *r)remove_virtual_region (r); } +#if defined(CONFIG_LIVEPATCH ) && defined(CONFIG_XEN_SHSTK ) +void reset_virtual_region_perms (void ) +{ +const struct virtual_region *region; + +rcu_read_lock (&rcu_virtual_region_lock); +list_for_each_entry_rcu ( region, &virtual_region_list, list ) +modify_xen_mappings ((unsigned long )region->start, +ROUNDUP ((unsigned long )region->end,PAGE_SIZE ), +PAGE_HYPERVISOR_RX ); +rcu_read_unlock (&rcu_virtual_region_lock); +} +#endif +void __init unregister_init_virtual_region (void ) {BUG_ON (system_state != SYS_STATE_active);
xen/include/xen/virtual_region.h +1 -0
@@ -33,6 +33,7 @@ void setup_virtual_regions(const struct exception_table_entry *start,void unregister_init_virtual_region (void );void register_virtual_region (struct virtual_region *r);void unregister_virtual_region (struct virtual_region *r); +void reset_virtual_region_perms (void );#endif /* __XEN_VIRTUAL_REGION_H__ */
Signature · 1 line
2.11.0
Quoted 6 lines · Andrew Cooper
Just like the alternatives infrastructure, the livepatch infrastructure disables CR0.WP to perform patching, which is not permitted with CET active.
Modify arch_livepatch_{quiesce,revive}() to disable CET before disabling WP, and reset the dirty bits on all virtual regions before re-enabling CET.
Quoted 7 lines
One complication is that
arch_livepatch_revive()has to fix up the top of the shadow stack. This depends on the functions not being inlined, even under LTO. Another limitation is thatreset_virtual_region_perms()may shatter the final superpage of .text depending on alignment.This logic, and its downsides, are temporary until the patching infrastructure can be adjusted to not use CR0.WP.
In particular on this basis ...
Quoted 1 line
Signed-off-by Andrew Cooper <[email protected]>
Jan
Quoted 21 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 26 June 2020 14:15
To: Andrew Cooper <[email protected]>
Cc: Xen-devel <[email protected]>; Wei Liu <[email protected]>; Roger Pau Monné <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>; Pawel Wieczorkiewicz <[email protected]>; Paul Durrant <[email protected]>
Subject: Re: [PATCH v2 for-4.14] x86/livepatch: Make livepatching compatible with CET Shadow StacksQuoted 11 lines · Andrew Cooper
Just like the alternatives infrastructure, the livepatch infrastructure disables CR0.WP to perform patching, which is not permitted with CET active.
Modify arch_livepatch_{quiesce,revive}() to disable CET before disabling WP, and reset the dirty bits on all virtual regions before re-enabling CET.
One complication is that
arch_livepatch_revive()has to fix up the top of the shadow stack. This depends on the functions not being inlined, even under LTO. Another limitation is thatreset_virtual_region_perms()may shatter the final superpage of .text depending on alignment.
Quoted 8 lines
Quoted 2 lines
This logic, and its downsides, are temporary until the patching infrastructure can be adjusted to not use CR0.WP.
In particular on this basis ...
Quoted 1 line
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Quoted 2 lines
Jan
Quoted 49 lines · Andrew Cooper
Just like the alternatives infrastructure, the livepatch infrastructure disables CR0.WP to perform patching, which is not permitted with CET active.
Modify arch_livepatch_{quiesce,revive}() to disable CET before disabling WP, and reset the dirty bits on all virtual regions before re-enabling CET.
One complication is that
arch_livepatch_revive()has to fix up the top of the shadow stack. This depends on the functions not being inlined, even under LTO. Another limitation is thatreset_virtual_region_perms()may shatter the final superpage of .text depending on alignment.This logic, and its downsides, are temporary until the patching infrastructure can be adjusted to not use CR0.WP.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Pawel Wieczorkiewicz <[email protected]>
CC: Paul Durrant <[email protected]>For 4.14. This is a bug in a 4.14 feature, with a very low risk to non-CET usecases.
v2:
* nolinline, and extra ifdefary
* Expand comments--- xen/arch/x86/livepatch.c | 35 +++++++++++++++++++++++++++++++++-- xen/common/virtual_region.c | 15 +++++++++++++++ xen/include/xen/virtual_region.h | 1 + 3 files changed, 49 insertions(+), 2 deletions(-)xen/arch/x86/livepatch.c +1
-0@@ -12,6 +12,7 @@#include <xen/livepatch.h> #include <xen/sched.h> #include <xen/vm_event.h> +#include <xen/virtual_region.h> #include <asm/fixmap.h> #include <asm/nmi.h>
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 8 lines
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )
Should this be:
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )
to match arch_livepatch_revive?
Ross
Quoted 12 lines · Ross Lagerwall
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 9 lines · Andrew Cooper
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )Should this be:
Quoted 3 lines
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )to match
arch_livepatch_revive?
While it may look a little asymmetric, I think it's preferable to is IS_ENABLED() only where really needed, i.e. here it guarding code that otherwise may not build.
Jan
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Ross Lagerwall
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 9 lines · Andrew Cooper
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )Should this be:
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )
Quoted 4 lines
Quoted 1 line
to match
arch_livepatch_revive?While it may look a little asymmetric, I think it's preferable to is
IS_ENABLED()only where really needed, i.e. here it guarding code that otherwise may not build.
The reason for the asymmetry is because of the asm() block, which needs compiling out when we detect that we don't have a compatible assembler.
I was wondering whether I should make cpu_has_xen_shstk be false for !CONFIG_XEN_SHSTK, but that would be 4.15 work at this point.
~Andrew
Quoted 20 lines · Andrew Cooper
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Ross Lagerwall
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 9 lines · Andrew Cooper
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )Should this be:
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )Quoted 4 lines
Quoted 1 line
to match
arch_livepatch_revive?While it may look a little asymmetric, I think it's preferable to is
IS_ENABLED()only where really needed, i.e. here it guarding code that otherwise may not build.
Quoted 5 lines
The reason for the asymmetry is because of the
asm()block, which needs compiling out when we detect that we don't have a compatible assembler.I was wondering whether I should make
cpu_has_xen_shstkbe false for !CONFIG_XEN_SHSTK, but that would be 4.15 work at this point.
Ah yes, this might then help with other code as well.
Jan
Ross Lagerwall
Reviewed-byQuoted 15 lines · Andrew Cooper
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Ross Lagerwall
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 9 lines · Andrew Cooper
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )Should this be:
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
to match
arch_livepatch_revive?While it may look a little asymmetric, I think it's preferable to is
IS_ENABLED()only where really needed, i.e. here it guarding code that otherwise may not build.The reason for the asymmetry is because of the
asm()block, which needs compiling out when we detect that we don't have a compatible assembler.
In that case,
Thanks
Quoted 20 lines · Ross Lagerwall
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich
Quoted 11 lines · Ross Lagerwall
xen/arch/x86/livepatch.c int arch_livepatch_safety_check(void) · line 63
Quoted 9 lines · Andrew Cooper
xen/arch/x86/livepatch.c +3
-1@@ -56,18 +57,48 @@ int arch_livepatch_safety_check(void)return -EBUSY ; } -int arch_livepatch_quiesce (void ) +int noinline arch_livepatch_quiesce(void) { +/* If Shadow Stacks are in use, disable CR4.CET so we can modify CR0.WP. */ +if ( cpu_has_xen_shstk )Should this be:
Quoted 3 lines
if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )to match
arch_livepatch_revive?While it may look a little asymmetric, I think it's preferable to is
IS_ENABLED()only where really needed, i.e. here it guarding code that otherwise may not build.
Quoted 6 lines
Quoted 3 lines
The reason for the asymmetry is because of the
asm()block, which needs compiling out when we detect that we don't have a compatible assembler.In that case,
Reviewed-by Ross Lagerwall <[email protected]>
Thanks. I've decided to clean this up in the (growing) series of 4.15 changes.
~Andrew